Subject: Re: [PATCH v2] mm/gup: disallow GUP writing to file-backed mappings by default

From: Jason Gunthorpe <[email-masked]>
Date: Mon, 24 Apr 2023 14:36:47 -0300

On Mon, Apr 24, 2023 at 03:29:57PM +0100, Lorenzo Stoakes wrote:
> On Mon, Apr 24, 2023 at 10:39:25AM -0300, Jason Gunthorpe wrote:
> > On Mon, Apr 24, 2023 at 01:38:49PM +0100, Lorenzo Stoakes wrote:
> >
> > > I was being fairly conservative in that list, though we certainly need to
> > > set the flag for /proc/$pid/mem and ptrace to avoid breaking this
> > > functionality (I observed breakpoints breaking without it which obviously
> > > is a no go :). I'm not sure if there's a more general way we could check
> > > for this though?
> >
> > More broadly we should make sure these usages of GUP safe somehow so
> > that it can reliably write to those types of pages without breaking
> > the current FS contract..
> >
> > I forget exactly, but IIRC, don't you have to hold some kind of page
> > spinlock while writing to the page memory?
> >
> 
> I think perhaps you're thinking of the mm->mmap_lock? Which will be held
> for the FOLL_GET cases and simply prevent the VMA from disappearing below
> us but not do much else.

No not mmap_lock, I want to say there is a per-page lock that
interacts with the write protect, or at worst this needs to use the
page table spinlocks.

> I wonder whether we should do this check purely for FOLL_PIN to be honest?
> As this indicates medium to long-term access without mmap_lock held. This
> would exclude the /proc/$pid/mem and ptrace paths which use gup_remote().

Everything is buggy. FOLL_PIN is part of a someday solution to solve
it.

> That and a very specific use of uprobes are the only places that use
> FOLL_GET in this instance and each of them are careful in any case to
> handle setting the dirty page flag.

That is actually the bug :) Broadly the bug is to make a page dirty
without holding the right locks to actually dirty it.

Jason