[PATCH RFC 1/5] mm/mprotect: allow clean exclusive anon pages to be writable

From: David Hildenbrand
Date: Mon Sep 26 2022 - 12:40:43 EST


From: Nadav Amit <namit@xxxxxxxxxx>

Anonymous pages might have the dirty bit clear, but this should not
prevent mprotect from making them writable if they are exclusive.
Therefore, skip the test whether the page is dirty in this case.

Note that there are already other ways to get a writable PTE mapping an
anonymous page that is clean: for example, via MADV_FREE. In an ideal
world, we'd have a different indication from the FS whether writenotify
is still required.

Signed-off-by: Nadav Amit <namit@xxxxxxxxxx>
[ comment for dirty/clean handling; return directly; update description ]
Signed-off-by: David Hildenbrand <david@xxxxxxxxxx>
---
mm/mprotect.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/mm/mprotect.c b/mm/mprotect.c
index ed013f836b4a..c6c13a0a4bcc 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -45,7 +45,7 @@ static inline bool can_change_pte_writable(struct vm_area_struct *vma,

VM_BUG_ON(!(vma->vm_flags & VM_WRITE) || pte_write(pte));

- if (pte_protnone(pte) || !pte_dirty(pte))
+ if (pte_protnone(pte))
return false;

/* Do we need write faults for softdirty tracking? */
@@ -64,11 +64,15 @@ static inline bool can_change_pte_writable(struct vm_area_struct *vma,
* the PT lock.
*/
page = vm_normal_page(vma, addr, pte);
- if (!page || !PageAnon(page) || !PageAnonExclusive(page))
- return false;
+ return page && PageAnon(page) && PageAnonExclusive(page);
}

- return true;
+ /*
+ * Shared mapping: "clean" might indicate that the FS still has to be
+ * notified via a write fault once first -- see vma_wants_writenotify().
+ * If "dirty", the assumtion is that there already was a write fault.
+ */
+ return pte_dirty(pte);
}

static unsigned long change_pte_range(struct mmu_gather *tlb,
--
2.37.3