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

From: Kirill A. Shutemov
Date: Mon Apr 24 2023 - 09:41:13 EST


On Mon, Apr 24, 2023 at 01:31:56PM +0100, Lorenzo Stoakes wrote:
> On Mon, Apr 24, 2023 at 03:02:47PM +0300, Kirill A. Shutemov wrote:
> > On Sat, Apr 22, 2023 at 02:37:05PM +0100, Lorenzo Stoakes wrote:
> > > @@ -959,16 +959,46 @@ static int faultin_page(struct vm_area_struct *vma,
> > > return 0;
> > > }
> > >
> > > +/*
> > > + * Writing to file-backed mappings using GUP is a fundamentally broken operation
> > > + * as kernel write access to GUP mappings may not adhere to the semantics
> > > + * expected by a file system.
> > > + *
> > > + * In most instances we disallow this broken behaviour, however there are some
> > > + * exceptions to this enforced here.
> > > + */
> > > +static inline bool can_write_file_mapping(struct vm_area_struct *vma,
> > > + unsigned long gup_flags)
> > > +{
> > > + struct file *file = vma->vm_file;
> > > +
> > > + /* If we aren't pinning then no problematic write can occur. */
> > > + if (!(gup_flags & (FOLL_GET | FOLL_PIN)))
> > > + return true;
> > > +
> > > + /* Special mappings should pose no problem. */
> > > + if (!file)
> > > + return true;
> > > +
> > > + /* Has the caller explicitly indicated this case is acceptable? */
> > > + if (gup_flags & FOLL_ALLOW_BROKEN_FILE_MAPPING)
> > > + return true;
> > > +
> > > + /* shmem and hugetlb mappings do not have problematic semantics. */
> > > + return vma_is_shmem(vma) || is_file_hugepages(file);
> >
> > Can this be generalized to any fs that doesn't have vm_ops->page_mkwrite()?
> >
>
> Something more general would be preferable, however I believe there were
> concerns broader than write notify, for instance not correctly marking the
> folio dirty after writing to it, though arguably the caller should
> certainly be ensuring that (and in many cases, do).

It doesn't make much sense to me.

Shared writable mapping without page_mkwrite (or pfn_write) will setup
writeable PTE even on read faults[1], so you will not get the page dirty,
unless you scan page table entries for dirty bit.

[1] See logic around vm_page_prot vs. vma_wants_writenotify().

--
Kiryl Shutsemau / Kirill A. Shutemov