Skip to content

Commit 45199f7

Browse files
Dev Jainakpm00
authored andcommitted
mm: split can_change_pte_writable() into private and shared parts
In preparation for patch 6 and modularizing the code in general, split can_change_pte_writable() into private and shared VMA parts. No functional change intended. Link: https://lkml.kernel.org/r/20250718090244.21092-6-dev.jain@arm.com Signed-off-by: Dev Jain <dev.jain@arm.com> Suggested-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Reviewed-by: Zi Yan <ziy@nvidia.com> Cc: Anshuman Khandual <anshuman.khandual@arm.com> Cc: Barry Song <baohua@kernel.org> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Christophe Leroy <christophe.leroy@csgroup.eu> Cc: David Hildenbrand <david@redhat.com> Cc: Hugh Dickins <hughd@google.com> Cc: Jann Horn <jannh@google.com> Cc: Joey Gouly <joey.gouly@arm.com> Cc: Kevin Brodsky <kevin.brodsky@arm.com> Cc: Lance Yang <ioworker0@gmail.com> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Peter Xu <peterx@redhat.com> Cc: Ryan Roberts <ryan.roberts@arm.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Will Deacon <will@kernel.org> Cc: Yang Shi <yang@os.amperecomputing.com> Cc: Yicong Yang <yangyicong@hisilicon.com> Cc: Zhenhua Huang <quic_zhenhuah@quicinc.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 57fae93 commit 45199f7

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

mm/mprotect.c

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,8 @@
4040

4141
#include "internal.h"
4242

43-
bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
44-
pte_t pte)
43+
static bool maybe_change_pte_writable(struct vm_area_struct *vma, pte_t pte)
4544
{
46-
struct page *page;
47-
4845
if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
4946
return false;
5047

@@ -60,16 +57,32 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
6057
if (userfaultfd_pte_wp(vma, pte))
6158
return false;
6259

63-
if (!(vma->vm_flags & VM_SHARED)) {
64-
/*
65-
* Writable MAP_PRIVATE mapping: We can only special-case on
66-
* exclusive anonymous pages, because we know that our
67-
* write-fault handler similarly would map them writable without
68-
* any additional checks while holding the PT lock.
69-
*/
70-
page = vm_normal_page(vma, addr, pte);
71-
return page && PageAnon(page) && PageAnonExclusive(page);
72-
}
60+
return true;
61+
}
62+
63+
static bool can_change_private_pte_writable(struct vm_area_struct *vma,
64+
unsigned long addr, pte_t pte)
65+
{
66+
struct page *page;
67+
68+
if (!maybe_change_pte_writable(vma, pte))
69+
return false;
70+
71+
/*
72+
* Writable MAP_PRIVATE mapping: We can only special-case on
73+
* exclusive anonymous pages, because we know that our
74+
* write-fault handler similarly would map them writable without
75+
* any additional checks while holding the PT lock.
76+
*/
77+
page = vm_normal_page(vma, addr, pte);
78+
return page && PageAnon(page) && PageAnonExclusive(page);
79+
}
80+
81+
static bool can_change_shared_pte_writable(struct vm_area_struct *vma,
82+
pte_t pte)
83+
{
84+
if (!maybe_change_pte_writable(vma, pte))
85+
return false;
7386

7487
VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte));
7588

@@ -83,6 +96,15 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
8396
return pte_dirty(pte);
8497
}
8598

99+
bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
100+
pte_t pte)
101+
{
102+
if (!(vma->vm_flags & VM_SHARED))
103+
return can_change_private_pte_writable(vma, addr, pte);
104+
105+
return can_change_shared_pte_writable(vma, pte);
106+
}
107+
86108
static int mprotect_folio_pte_batch(struct folio *folio, pte_t *ptep,
87109
pte_t pte, int max_nr_ptes)
88110
{

0 commit comments

Comments
 (0)