mm: perform guard region install/remove under VMA lock
Subsystems (3)
v2 cover mm: perform guard region install/remove under VMA lock
There is no reason why can't perform guard region operations under the VMA lock, as long we take proper precautions to ensure that we do so in a safe manner.
This is fine, as VMA lock acquisition is always best-effort, so if we are unable to do so, we can simply fall back to using the mmap read lock.
Doing so will reduce mmap lock contention for callers performing guard region operations and help establish a precedent of trying to use the VMA lock where possible.
As part of this change we perform a trivial rename of page walk functions which bypass safety checks (i.e. whether or not mm_walk_ops->install_pte is specified) in order that we can keep naming consistent with the mm walk.
This is because we need to expose a VMA-specific walk that still allows us to install PTE entries.
v2:
* Rename check_ops_valid() to check_ops_safe() as per David.
* Renamed is_vma_lock_valid() to is_vma_lock_sufficient() as per David.
v1:
https://lore.kernel.org/all/[email protected]/
Lorenzo Stoakes (2):
mm: rename walk_page_range_mm()
mm/madvise: allow guard page install/remove under VMA lock
mm/internal.h | 5 ++- mm/madvise.c | 110 ++++++++++++++++++++++++++++++++++++-------------- mm/pagewalk.c | 37 ++++++++++------- 3 files changed, 105 insertions(+), 47 deletions(-)
--
2.51.0
PATCH v2 1/2 mm: rename walk_page_range_mm()
Make it clear we're referencing an unsafe variant of this function explicitly.
This is laying the foundation for exposing more such functions and maintaining a consistent naming scheme.
As a part of this change, rename check_ops_valid() to check_ops_safe() for consistency.
--- mm/internal.h | 2 +- mm/madvise.c | 4 ++-- mm/pagewalk.c | 22 +++++++++++----------- 3 files changed, 14 insertions(+), 14 deletions(-)
mm/internal.h +1 -1
@@ -1652,7 +1652,7 @@ static inline void accept_page(struct page *page)#endif /* CONFIG_UNACCEPTED_MEMORY */ /* pagewalk.c */ -int walk_page_range_mm (struct mm_struct *mm,unsigned long start, +int walk_page_range_mm_unsafe (struct mm_struct *mm,unsigned long start,unsigned long end,const struct mm_walk_ops *ops,void *private); int walk_page_range_debug(struct mm_struct *mm, unsigned long start,
mm/madvise.c +2 -2
@@ -1171,8 +1171,8 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)unsigned long nr_pages =0 ;/* Returns < 0 on error, == 0 if success, > 0 if zap needed. */ - err =walk_page_range_mm (vma->vm_mm, range->start, range->end, - &guard_install_walk_ops, &nr_pages); + err =walk_page_range_mm_unsafe (vma->vm_mm, range->start, + range->end, &guard_install_walk_ops, &nr_pages);if (err <0 )return err;
mm/pagewalk.c +11 -11
@@ -452,7 +452,7 @@ static inline void process_vma_walk_lock(struct vm_area_struct *vma,* We usually restrict the ability to install PTEs, but this functionality is * available to internal memory management code and provided in mm/internal.h. */ -int walk_page_range_mm (struct mm_struct *mm,unsigned long start, +int walk_page_range_mm_unsafe (struct mm_struct *mm,unsigned long start,unsigned long end,const struct mm_walk_ops *ops,void *private) {
@@ -518,10 +518,10 @@ int walk_page_range_mm(struct mm_struct *mm, unsigned long start,* This check is performed on all functions which are parameterised by walk * operations and exposed in include/linux/pagewalk.h. * -* Internal memory management code can use the walk_page_range_mm() function to -* be able to use all page walking operations. +* Internal memory management code can use *_unsafe() functions to be able to +* use all page walking operations. */ -static bool check_ops_valid (const struct mm_walk_ops *ops) +static bool check_ops_safe (const struct mm_walk_ops *ops) {/* * The installation of PTEs is solely under the control of memory
@@ -579,10 +579,10 @@ int walk_page_range(struct mm_struct *mm, unsigned long start, unsigned long end, const struct mm_walk_ops *ops, void *private) { - if (!check_ops_valid(ops)) + if (!check_ops_safe(ops)) return -EINVAL; -return walk_page_range_mm (mm, start, end, ops, private); +return walk_page_range_mm_unsafe (mm, start, end, ops, private); }/**
@@ -639,7 +639,7 @@ int walk_kernel_page_table_range_lockless(unsigned long start, unsigned long endif (start >= end)return -EINVAL ; -if (!check_ops_valid (ops)) +if (!check_ops_safe (ops))return -EINVAL ;return walk_pgd_range (start, end, &walk);
@@ -678,7 +678,7 @@ int walk_page_range_debug(struct mm_struct *mm, unsigned long start, pgd, private);if (start >= end || !walk.mm)return -EINVAL ; -if (!check_ops_valid (ops)) +if (!check_ops_safe (ops))return -EINVAL ;/*
@@ -709,7 +709,7 @@ int walk_page_range_vma(struct vm_area_struct *vma, unsigned long start,return -EINVAL ;if (start < vma->vm_start || end > vma->vm_end)return -EINVAL ; -if (!check_ops_valid (ops)) +if (!check_ops_safe (ops))return -EINVAL ;process_mm_walk_lock (walk.mm, ops->walk_lock);
@@ -729,7 +729,7 @@ int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops,if (!walk.mm)return -EINVAL ; -if (!check_ops_valid (ops)) +if (!check_ops_safe (ops))return -EINVAL ;process_mm_walk_lock (walk.mm, ops->walk_lock);
@@ -780,7 +780,7 @@ int walk_page_mapping(struct address_space *mapping, pgoff_t first_index,unsigned long start_addr, end_addr;int err =0 ; -if (!check_ops_valid (ops)) +if (!check_ops_safe (ops))return -EINVAL ;lockdep_assert_held (&mapping->i_mmap_rwsem);
Signature · 1 line
2.51.0
Re: [PATCH v2 1/2] mm: rename walk_page_range_mm()
Quoted 11 lines · Lorenzo Stoakes
Make it clear we're referencing an unsafe variant of this function explicitly.
This is laying the foundation for exposing more such functions and maintaining a consistent naming scheme.
As a part of this change, rename
check_ops_valid()tocheck_ops_safe()for consistency.Signed-off-by Lorenzo Stoakes <[email protected]>---
Signature · 3 lines
Cheers David
Re: [PATCH v2 1/2] mm: rename walk_page_range_mm()
In reply to Lorenzo StoakesQuoted 10 lines · Lorenzo Stoakes
Make it clear we're referencing an unsafe variant of this function explicitly.
This is laying the foundation for exposing more such functions and maintaining a consistent naming scheme.
As a part of this change, rename
check_ops_valid()tocheck_ops_safe()for consistency.Signed-off-by Lorenzo Stoakes <[email protected]>
Quoted 119 lines
--- mm/internal.h | 2 +- mm/madvise.c | 4 ++-- mm/pagewalk.c | 22 +++++++++++----------- 3 files changed, 14 insertions(+), 14 deletions(-)mm/internal.h +1
-1@@ -1652,7 +1652,7 @@ static inline void accept_page(struct page *page)#endif /* CONFIG_UNACCEPTED_MEMORY */ /* pagewalk.c */ -int walk_page_range_mm (struct mm_struct *mm,unsigned long start, +int walk_page_range_mm_unsafe (struct mm_struct *mm,unsigned long start,unsigned long end,const struct mm_walk_ops *ops,void *private); int walk_page_range_debug(struct mm_struct *mm, unsigned long start,mm/madvise.c +2
-2@@ -1171,8 +1171,8 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)unsigned long nr_pages =0 ;/* Returns < 0 on error, == 0 if success, > 0 if zap needed. */ - err =walk_page_range_mm (vma->vm_mm, range->start, range->end, - &guard_install_walk_ops, &nr_pages); + err =walk_page_range_mm_unsafe (vma->vm_mm, range->start, + range->end, &guard_install_walk_ops, &nr_pages);if (err <0 )return err;mm/pagewalk.c +11
-11@@ -452,7 +452,7 @@ static inline void process_vma_walk_lock(struct vm_area_struct *vma,* We usually restrict the ability to install PTEs, but this functionality is * available to internal memory management code and provided in mm/internal.h. */ -int walk_page_range_mm (struct mm_struct *mm,unsigned long start, +int walk_page_range_mm_unsafe (struct mm_struct *mm,unsigned long start,unsigned long end,const struct mm_walk_ops *ops,void *private) {@@ -518,10 +518,10 @@ int walk_page_range_mm(struct mm_struct *mm, unsigned long start,* This check is performed on all functions which are parameterised by walk * operations and exposed in include/linux/pagewalk.h. * -* Internal memory management code can use the walk_page_range_mm() function to -* be able to use all page walking operations. +* Internal memory management code can use *_unsafe() functions to be able to +* use all page walking operations. */ -static bool check_ops_valid (const struct mm_walk_ops *ops) +static bool check_ops_safe (const struct mm_walk_ops *ops) {/* * The installation of PTEs is solely under the control of memory @@ -579,10 +579,10 @@ int walk_page_range(struct mm_struct *mm, unsigned long start, unsigned long end, const struct mm_walk_ops *ops, void *private) { - if (!check_ops_valid(ops)) + if (!check_ops_safe(ops)) return -EINVAL; -return walk_page_range_mm (mm, start, end, ops, private); +return walk_page_range_mm_unsafe (mm, start, end, ops, private); }/** @@ -639,7 +639,7 @@ int walk_kernel_page_table_range_lockless(unsigned long start, unsigned long endif (start >= end)return -EINVAL ; -if (!check_ops_valid (ops)) +if (!check_ops_safe (ops))return -EINVAL ;return walk_pgd_range (start, end, &walk);@@ -678,7 +678,7 @@ int walk_page_range_debug(struct mm_struct *mm, unsigned long start, pgd, private);if (start >= end || !walk.mm)return -EINVAL ; -if (!check_ops_valid (ops)) +if (!check_ops_safe (ops))return -EINVAL ;/* @@ -709,7 +709,7 @@ int walk_page_range_vma(struct vm_area_struct *vma, unsigned long start,return -EINVAL ;if (start < vma->vm_start || end > vma->vm_end)return -EINVAL ; -if (!check_ops_valid (ops)) +if (!check_ops_safe (ops))return -EINVAL ;process_mm_walk_lock (walk.mm, ops->walk_lock);@@ -729,7 +729,7 @@ int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops,if (!walk.mm)return -EINVAL ; -if (!check_ops_valid (ops)) +if (!check_ops_safe (ops))return -EINVAL ;process_mm_walk_lock (walk.mm, ops->walk_lock);@@ -780,7 +780,7 @@ int walk_page_mapping(struct address_space *mapping, pgoff_t first_index,unsigned long start_addr, end_addr;int err =0 ; -if (!check_ops_valid (ops)) +if (!check_ops_safe (ops))return -EINVAL ;lockdep_assert_held (&mapping->i_mmap_rwsem);
Re: [PATCH v2 1/2] mm: rename walk_page_range_mm()
In reply to Lorenzo StoakesQuoted 10 lines · Lorenzo Stoakes
Make it clear we're referencing an unsafe variant of this function explicitly.
This is laying the foundation for exposing more such functions and maintaining a consistent naming scheme.
As a part of this change, rename
check_ops_valid()tocheck_ops_safe()for consistency.Signed-off-by Lorenzo Stoakes <[email protected]>
PATCH v2 2/2 mm/madvise: allow guard page install/remove under VMA lock
In reply to Lorenzo StoakesWe only need to keep the page table stable so we can perform this operation under the VMA lock. PTE installation is stabilised via the PTE lock.
One caveat is that, if we prepare vma->anon_vma we must hold the mmap read lock. We can account for this by adapting the VMA locking logic to explicitly check for this case and prevent a VMA lock from being acquired should it be the case.
This check is safe, as while we might be raced on anon_vma installation, this would simply make the check conservative, there's no way for us to see an anon_vma and then for it to be cleared, as doing so requires the mmap/VMA write lock.
We abstract the VMA lock validity logic to is_vma_lock_sufficient() for this purpose, and add prepares_anon_vma() to abstract the anon_vma logic.
In order to do this we need to have a way of installing page tables explicitly for an identified VMA, so we export walk_page_range_vma() in an unsafe variant - walk_page_range_vma_unsafe() and use this should the VMA read lock be taken.
We additionally update the comments in madvise_guard_install() to more accurately reflect the cases in which the logic may be reattempted, specifically THP huge pages being present.
--- mm/internal.h | 3 ++ mm/madvise.c | 110 ++++++++++++++++++++++++++++++++++++-------------- mm/pagewalk.c | 17 +++++--- 3 files changed, 94 insertions(+), 36 deletions(-)
mm/internal.h +3 -0
@@ -1655,6 +1655,9 @@ static inline void accept_page(struct page *page)int walk_page_range_mm_unsafe (struct mm_struct *mm,unsigned long start,unsigned long end,const struct mm_walk_ops *ops,void *private); +int walk_page_range_vma_unsafe (struct vm_area_struct *vma,unsigned long start, +unsigned long end,const struct mm_walk_ops *ops, +void *private);int walk_page_range_debug (struct mm_struct *mm,unsigned long start,unsigned long end,const struct mm_walk_ops *ops,pgd_t *pgd,void *private);
mm/madvise.c +79 -31
187 lines not on this page · raw message
mm/pagewalk.c +12 -5
@@ -694,9 +694,8 @@ int walk_page_range_debug(struct mm_struct *mm, unsigned long start,return walk_pgd_range (start, end, &walk); } -int walk_page_range_vma (struct vm_area_struct *vma,unsigned long start, -unsigned long end,const struct mm_walk_ops *ops, -void *private) +int walk_page_range_vma_unsafe (struct vm_area_struct *vma,unsigned long start, +unsigned long end,const struct mm_walk_ops *ops,void *private) {struct mm_walk walk = { .ops = ops,
@@ -709,14 +708,22 @@ int walk_page_range_vma(struct vm_area_struct *vma, unsigned long start,return -EINVAL ;if (start < vma->vm_start || end > vma->vm_end)return -EINVAL ; -if (!check_ops_safe (ops)) -return -EINVAL ;process_mm_walk_lock (walk.mm, ops->walk_lock);process_vma_walk_lock (vma, ops->walk_lock);return __walk_page_range (start, end, &walk); } +int walk_page_range_vma (struct vm_area_struct *vma,unsigned long start, +unsigned long end,const struct mm_walk_ops *ops, +void *private) +{ +if (!check_ops_safe (ops)) +return -EINVAL ; + +return walk_page_range_vma_unsafe (vma, start, end, ops, private); +} +int walk_page_vma (struct vm_area_struct *vma,const struct mm_walk_ops *ops,void *private) {
Signature · 1 line
2.51.0
Re: [PATCH v2 2/2] mm/madvise: allow guard page install/remove under VMA lock
Quoted 21 lines · Lorenzo Stoakes
We only need to keep the page table stable so we can perform this operation under the VMA lock. PTE installation is stabilised via the PTE lock.
One caveat is that, if we prepare
vma->anon_vmawe must hold the mmap read lock. We can account for this by adapting the VMA locking logic to explicitly check for this case and prevent a VMA lock from being acquired should it be the case.This check is safe, as while we might be raced on
anon_vmainstallation, this would simply make the check conservative, there's no way for us to see ananon_vmaand then for it to be cleared, as doing so requires the mmap/VMA write lock.We abstract the VMA lock validity logic to
is_vma_lock_sufficient()for this purpose, and addprepares_anon_vma()to abstract theanon_vmalogic.In order to do this we need to have a way of installing page tables explicitly for an identified VMA, so we export
walk_page_range_vma()in an unsafe variant -walk_page_range_vma_unsafe()and use this should the VMA read lock be taken.
Quoted 6 lines
We additionally update the comments in
madvise_guard_install()to more accurately reflect the cases in which the logic may be reattempted, specifically THP huge pages being present.Signed-off-by Lorenzo Stoakes <[email protected]>---
Looks good to me
Signature · 3 lines
Cheers David
Re: [PATCH v2 2/2] mm/madvise: allow guard page install/remove under VMA lock
In reply to Lorenzo StoakesQuoted 21 lines · Lorenzo Stoakes
We only need to keep the page table stable so we can perform this operation under the VMA lock. PTE installation is stabilised via the PTE lock.
One caveat is that, if we prepare
vma->anon_vmawe must hold the mmap read lock. We can account for this by adapting the VMA locking logic to explicitly check for this case and prevent a VMA lock from being acquired should it be the case.This check is safe, as while we might be raced on
anon_vmainstallation, this would simply make the check conservative, there's no way for us to see ananon_vmaand then for it to be cleared, as doing so requires the mmap/VMA write lock.We abstract the VMA lock validity logic to
is_vma_lock_sufficient()for this purpose, and addprepares_anon_vma()to abstract theanon_vmalogic.In order to do this we need to have a way of installing page tables explicitly for an identified VMA, so we export
walk_page_range_vma()in an unsafe variant -walk_page_range_vma_unsafe()and use this should the VMA read lock be taken.
Quoted 5 lines
We additionally update the comments in
madvise_guard_install()to more accurately reflect the cases in which the logic may be reattempted, specifically THP huge pages being present.Signed-off-by Lorenzo Stoakes <[email protected]>
Re: [PATCH v2 2/2] mm/madvise: allow guard page install/remove under VMA lock
In reply to Lorenzo StoakesQuoted 16 lines · Lorenzo Stoakes
We only need to keep the page table stable so we can perform this operation under the VMA lock. PTE installation is stabilised via the PTE lock.
One caveat is that, if we prepare
vma->anon_vmawe must hold the mmap read lock. We can account for this by adapting the VMA locking logic to explicitly check for this case and prevent a VMA lock from being acquired should it be the case.This check is safe, as while we might be raced on
anon_vmainstallation, this would simply make the check conservative, there's no way for us to see ananon_vmaand then for it to be cleared, as doing so requires the mmap/VMA write lock.We abstract the VMA lock validity logic to
is_vma_lock_sufficient()for this purpose, and addprepares_anon_vma()to abstract theanon_vmalogic.
Quoted 8 lines
In order to do this we need to have a way of installing page tables explicitly for an identified VMA, so we export
walk_page_range_vma()in an unsafe variant -walk_page_range_vma_unsafe()and use this should the VMA read lock be taken.We additionally update the comments in
madvise_guard_install()to more accurately reflect the cases in which the logic may be reattempted, specifically THP huge pages being present.
Makes sense.
Re: [PATCH v2 2/2] mm/madvise: allow guard page install/remove under VMA lock
In reply to Lorenzo StoakesQuoted 16 lines · On Mon, 10 Nov 2025 17:22:58 +0000 Lorenzo Stoakes…
We only need to keep the page table stable so we can perform this operation under the VMA lock. PTE installation is stabilised via the PTE lock.
One caveat is that, if we prepare
vma->anon_vmawe must hold the mmap read lock. We can account for this by adapting the VMA locking logic to explicitly check for this case and prevent a VMA lock from being acquired should it be the case.This check is safe, as while we might be raced on
anon_vmainstallation, this would simply make the check conservative, there's no way for us to see ananon_vmaand then for it to be cleared, as doing so requires the mmap/VMA write lock.We abstract the VMA lock validity logic to
is_vma_lock_sufficient()for this purpose, and addprepares_anon_vma()to abstract theanon_vmalogic.
Quoted 9 lines
In order to do this we need to have a way of installing page tables explicitly for an identified VMA, so we export
walk_page_range_vma()in an unsafe variant -walk_page_range_vma_unsafe()and use this should the VMA read lock be taken.We additionally update the comments in
madvise_guard_install()to more accurately reflect the cases in which the logic may be reattempted, specifically THP huge pages being present.
Hi Lorenzo, one more flagged by AI:
Quoted 2 lines
mm/madvise.c +0
-0
[ ... ]
Quoted 17 lines
mm/madvise.c +7
-2@@ -1173,8 +1180,13 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)unsigned long nr_pages =0 ;/* Returns < 0 on error, == 0 if success, > 0 if zap needed. */ - err =walk_page_range_mm_unsafe (vma->vm_mm, range->start, - range->end, &guard_install_walk_ops, &nr_pages); +if (madv_behavior->lock_mode ==MADVISE_VMA_READ_LOCK ) + err =walk_page_range_vma_unsafe (madv_behavior->vma, + range->start, range->end, &walk_ops, + &nr_pages); +else + err =walk_page_range_mm_unsafe (vma->vm_mm, range->start, + range->end, &walk_ops, &nr_pages);if (err <0 )return err;
mm/madvise.c static long madvise_guard_install(struct madvise_behavior *madv_behavior) · line 1207
Quoted 1 line
mm/madvise.c +0
-0@@ -1195,8 +1207,7 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior) }
When err > 0 (zap needed), zap_page_range_single() is called below regardless of lock_mode. If lock_mode == MADVISE_VMA_READ_LOCK (only VMA lock is held), and the zap range contains a THP PUD mapping that requires splitting, the call chain:
madvise_guard_install()
-> zap_page_range_single()
-> zap_page_range_single_batched()
-> unmap_single_vma()
-> unmap_page_range()
-> zap_p4d_range()
-> zap_pud_range()will hit mmap_assert_locked() in zap_pud_range() (mm/memory.c) when pud_trans_huge() is true and partial PUD splitting is required.
The assertion will trigger a WARN_ON because only the VMA lock is held, not the mmap lock.
Is there an additional check needed before calling zap_page_range_single() when in VMA lock mode, or should this path fall back to mmap lock when THP is possible?
mm/madvise.c static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavi · line 1771
Quoted 11 lines
mm/madvise.c +1
-1@@ -1715,9 +1763,9 @@ static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavicase MADV_POPULATE_READ :case MADV_POPULATE_WRITE :case MADV_COLLAPSE : +return MADVISE_MMAP_READ_LOCK ;case MADV_GUARD_INSTALL :case MADV_GUARD_REMOVE : -return MADVISE_MMAP_READ_LOCK ;case MADV_DONTNEED :case MADV_DONTNEED_LOCKED :case MADV_FREE :
This change moves MADV_GUARD_INSTALL to use MADVISE_VMA_READ_LOCK, but zap_page_range_single() called later in madvise_guard_install() may require the mmap lock for THP PUD splitting as noted above.
Re: [PATCH v2 2/2] mm/madvise: allow guard page install/remove under VMA lock
Quoted 67 lines · Chris Mason
Quoted 16 lines · On Mon, 10 Nov 2025 17:22:58 +0000 Lorenzo Stoakes…
We only need to keep the page table stable so we can perform this operation under the VMA lock. PTE installation is stabilised via the PTE lock.
One caveat is that, if we prepare
vma->anon_vmawe must hold the mmap read lock. We can account for this by adapting the VMA locking logic to explicitly check for this case and prevent a VMA lock from being acquired should it be the case.This check is safe, as while we might be raced on
anon_vmainstallation, this would simply make the check conservative, there's no way for us to see ananon_vmaand then for it to be cleared, as doing so requires the mmap/VMA write lock.We abstract the VMA lock validity logic to
is_vma_lock_sufficient()for this purpose, and addprepares_anon_vma()to abstract theanon_vmalogic.Quoted 9 lines
In order to do this we need to have a way of installing page tables explicitly for an identified VMA, so we export
walk_page_range_vma()in an unsafe variant -walk_page_range_vma_unsafe()and use this should the VMA read lock be taken.We additionally update the comments in
madvise_guard_install()to more accurately reflect the cases in which the logic may be reattempted, specifically THP huge pages being present.Hi Lorenzo, one more flagged by AI:
Quoted 2 lines
mm/madvise.c +0
-0[ ... ]
Quoted 17 lines
mm/madvise.c +7
-2@@ -1173,8 +1180,13 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)unsigned long nr_pages =0 ;/* Returns < 0 on error, == 0 if success, > 0 if zap needed. */ - err =walk_page_range_mm_unsafe (vma->vm_mm, range->start, - range->end, &guard_install_walk_ops, &nr_pages); +if (madv_behavior->lock_mode ==MADVISE_VMA_READ_LOCK ) + err =walk_page_range_vma_unsafe (madv_behavior->vma, + range->start, range->end, &walk_ops, + &nr_pages); +else + err =walk_page_range_mm_unsafe (vma->vm_mm, range->start, + range->end, &walk_ops, &nr_pages);if (err <0 )return err;mm/madvise.c static long madvise_guard_install(struct madvise_behavior *madv_behavior) · line 1207
Quoted 1 line
mm/madvise.c +0
-0@@ -1195,8 +1207,7 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior) }When err > 0 (zap needed),
zap_page_range_single()is called below regardless oflock_mode. Iflock_mode==MADVISE_VMA_READ_LOCK(only VMA lock is held), and the zap range contains a THP PUD mapping that requires splitting, the call chain:
madvise_guard_install()-> zap_page_range_single() -> zap_page_range_single_batched() -> unmap_single_vma() -> unmap_page_range() -> zap_p4d_range() -> zap_pud_range()
Quoted 2 lines
will hit
mmap_assert_locked()inzap_pud_range()(mm/memory.c) whenpud_trans_huge()is true and partial PUD splitting is required.
Yeah I was already aware of this, and I thought it was handled via is_valid_guard_vma().
You only get huge PUD THP for DAX or VFIO. VFIO is covered off as that's VM_SPECIAL but yeah, maybe not dev-dax...
It's a stupid situation really, I don't think we should be asserting that there but I don't necessarily want to unravel some locking horror show at this point, will put on TODO to come back to that.
Quoted 7 lines
The assertion will trigger a
WARN_ONbecause only the VMA lock is held, not the mmap lock.Is there an additional check needed before calling
zap_page_range_single()when in VMA lock mode, or should this path fall back to mmap lock when THP is possible?
is_valid_guard_vma() is the appropriate place.
I'll add a !vma_is_dax() check there for now and send a patch out.
Quoted 16 lines
mm/madvise.c static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavi · line 1771
Quoted 12 lines
mm/madvise.c +1
-1@@ -1715,9 +1763,9 @@ static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavicase MADV_POPULATE_READ :case MADV_POPULATE_WRITE :case MADV_COLLAPSE : +return MADVISE_MMAP_READ_LOCK ;case MADV_GUARD_INSTALL :case MADV_GUARD_REMOVE : -return MADVISE_MMAP_READ_LOCK ;case MADV_DONTNEED :case MADV_DONTNEED_LOCKED :case MADV_FREE :This change moves
MADV_GUARD_INSTALLto useMADVISE_VMA_READ_LOCK, butzap_page_range_single()called later inmadvise_guard_install()may require the mmap lock for THP PUD splitting as noted above.
Clever little machine ;)
Quoted 2 lines
Cheers, Lorenzo