[PATCH 29/39] mm/madvise: update is_valid_guard_vma() to use vma_can_merge()
From: Lorenzo Stoakes (ARM)
Date: Tue Sep 08 2026 - 16:22:12 EST
We currently disallow the installation of lightweight guard regions in VMAs
whose flags intersect VMA_SPECIAL_FLAGS or VMA_HUGETLB_BIT, or
VMA_LOCKED_BIT unless allow_locked is set.
hugetlb VMAs set VMA_DONTEXPAND_BIT so this was already redundant,
VMA_SPECIAL_FLAGS already sufficed.
However, now that VMA_IO_BIT is only set if VMA_PFNMAP or VMA_MIXEDMAP_BIT
is set, this check collapses to being the equivalent of
!vma_can_merge().
Update is_valid_guard_vma() to reflect this.
No functional change intended.
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
---
mm/madvise.c | 20 +++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)
diff --git a/mm/madvise.c b/mm/madvise.c
index 2db11c832d0f..d0b14cfe38a1 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -1055,19 +1055,25 @@ static long madvise_remove(struct madvise_behavior *madv_behavior)
return error;
}
-static bool is_valid_guard_vma(struct vm_area_struct *vma, bool allow_locked)
+static bool is_valid_guard_vma(const struct vm_area_struct *vma,
+ bool allow_locked)
{
- vm_flags_t disallowed = VM_SPECIAL | VM_HUGETLB;
-
/*
- * A user could lock after setting a guard range but that's fine, as
+ * A user could lock after setting a guard range but that's fine as
* they'd not be able to fault in. The issue arises when we try to zap
* existing locked VMAs. We don't want to do that.
*/
- if (!allow_locked)
- disallowed |= VM_LOCKED;
+ if (!allow_locked && vma_test(vma, VMA_LOCKED_BIT))
+ return false;
+ /*
+ * Guard regions require a VMA whose page tables are managed solely by
+ * the core, which is also what merging requires, so disallow any flags
+ * that would prevent a merge.
+ */
+ if (!vma_can_merge(vma))
+ return false;
- return !(vma->vm_flags & disallowed);
+ return true;
}
static bool is_guard_pte_marker(pte_t ptent)
--
2.55.0