Skip to content

Commit 1096bc9

Browse files
committed
mm: clean up populate_vma_page_range() FOLL_* flag handling
The code wasn't exactly wrong, but it was very odd, and it used FOLL_FORCE together with FOLL_WRITE when it really didn't need to (it only set FOLL_WRITE for writable mappings, so then the FOLL_FORCE was pointless). It also pointlessly called __get_user_pages() even when it knew it wouldn't populate anything because the vma wasn't accessible and it explicitly tested for and did *not* set FOLL_FORCE for inaccessible vma's. This code does need to use FOLL_FORCE, because we want to do fault in writable shared mappings, but then the mapping may not actually be readable. And we don't want to use FOLL_WRITE (which would match the permission of the vma), because that would also dirty the pages, which we don't want to do. For very similar reasons, FOLL_FORCE populates a executable-only mapping with no read permissions. We don't have a FOLL_EXEC flag. Yes, it would probably be cleaner to split FOLL_WRITE into two bits (for separate permission and dirty bit handling), and add a FOLL_EXEC flag for the "GUP executable page" case. That would allow us to avoid FOLL_FORCE entirely here. But that's not how our FOLL_xyz bits have traditionally worked, and that would be a much bigger patch. So this at least avoids the FOLL_FORCE | FOLL_WRITE combination that made one of my experimental validation patches trigger a warning. That warning was a false positive (and my experimental patch was incomplete anyway), but it all made me look at this and decide to clean at least this small case up. Signed-off-by: Linus Torvalds <[email protected]>
1 parent 0eee99d commit 1096bc9

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

mm/gup.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,20 +1653,22 @@ long populate_vma_page_range(struct vm_area_struct *vma,
16531653
if (vma->vm_flags & VM_LOCKONFAULT)
16541654
return nr_pages;
16551655

1656+
/* ... similarly, we've never faulted in PROT_NONE pages */
1657+
if (!vma_is_accessible(vma))
1658+
return -EFAULT;
1659+
16561660
gup_flags = FOLL_TOUCH;
16571661
/*
16581662
* We want to touch writable mappings with a write fault in order
16591663
* to break COW, except for shared mappings because these don't COW
16601664
* and we would not want to dirty them for nothing.
1665+
*
1666+
* Otherwise, do a read fault, and use FOLL_FORCE in case it's not
1667+
* readable (ie write-only or executable).
16611668
*/
16621669
if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
16631670
gup_flags |= FOLL_WRITE;
1664-
1665-
/*
1666-
* We want mlock to succeed for regions that have any permissions
1667-
* other than PROT_NONE.
1668-
*/
1669-
if (vma_is_accessible(vma))
1671+
else
16701672
gup_flags |= FOLL_FORCE;
16711673

16721674
if (locked)

0 commit comments

Comments
 (0)