[PATCH v4 25/25] mm: simplify VMA flag tests of excluded flags

Lorenzo Stoakes (Oracle)
2026-03-20T19:39:46Z

We have implemented flag mask comparisons of the form:

if ((vm_flags & (VM_FOO|VM_BAR|VM_BAZ) == VM_FOO) { ... }

Like-for-like in the code using a bitwise-and mask via vma_flags_and() and
using vma_flags_same() to ensure the final result equals only the required
flag value.

This is fine but confusing, make things clearer by instead explicitly
excluding undesired flags and including the desired one via tests of the
form:

if (vma_flags_test(&flags, VMA_FOO_BIT) &&
!vma_flags_test_any(&flags, VMA_BAR_BIT, VMA_BAZ_BIT)) { ... }

Which makes it easier to understand what is going on.

No functional change intended.

Suggested-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
Signed-off-by: Lorenzo Stoakes (Oracle) <ljs@kernel.org>
---
mm/mprotect.c | 12 ++++--------
mm/vma.c | 7 +++----
mm/vma.h | 6 ++----
3 files changed, 9 insertions(+), 16 deletions(-)

diff --git a/mm/mprotect.c b/mm/mprotect.c
index 007d9a72b2f0..110d47a36d4b 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -784,14 +784,10 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb,
* Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
* fault on access.
*/
- if (vma_flags_test(&new_vma_flags, VMA_WRITE_BIT)) {
- const vma_flags_t mask =
- vma_flags_and(&old_vma_flags, VMA_WRITE_BIT,
- VMA_SHARED_BIT, VMA_LOCKED_BIT);
-
- if (vma_flags_same(&mask, VMA_LOCKED_BIT))
- populate_vma_page_range(vma, start, end, NULL);
- }
+ if (vma_flags_test(&new_vma_flags, VMA_WRITE_BIT) &&
+ vma_flags_test(&old_vma_flags, VMA_LOCKED_BIT) &&
+ !vma_flags_test_any(&old_vma_flags, VMA_WRITE_BIT, VMA_SHARED_BIT))
+ populate_vma_page_range(vma, start, end, NULL);

vm_stat_account(mm, vma_flags_to_legacy(old_vma_flags), -nrpages);
newflags = vma_flags_to_legacy(new_vma_flags);
diff --git a/mm/vma.c b/mm/vma.c
index c335f989586f..a4b30a069153 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -2343,7 +2343,6 @@ void mm_drop_all_locks(struct mm_struct *mm)
static bool accountable_mapping(struct mmap_state *map)
{
const struct file *file = map->file;
- vma_flags_t mask;

/*
* hugetlb has its own accounting separate from the core VM
@@ -2352,9 +2351,9 @@ static bool accountable_mapping(struct mmap_state *map)
if (file && is_file_hugepages(file))
return false;

- mask = vma_flags_and(&map->vma_flags, VMA_NORESERVE_BIT, VMA_SHARED_BIT,
- VMA_WRITE_BIT);
- return vma_flags_same(&mask, VMA_WRITE_BIT);
+ return vma_flags_test(&map->vma_flags, VMA_WRITE_BIT) &&
+ !vma_flags_test_any(&map->vma_flags, VMA_NORESERVE_BIT,
+ VMA_SHARED_BIT);
}

/*
diff --git a/mm/vma.h b/mm/vma.h
index adc18f7dd9f1..1bfe7e47f6be 100644
--- a/mm/vma.h
+++ b/mm/vma.h
@@ -529,10 +529,8 @@ static inline bool is_data_mapping(vm_flags_t flags)

static inline bool is_data_mapping_vma_flags(const vma_flags_t *vma_flags)
{
- const vma_flags_t mask = vma_flags_and(vma_flags,
- VMA_WRITE_BIT, VMA_SHARED_BIT, VMA_STACK_BIT);
-
- return vma_flags_same(&mask, VMA_WRITE_BIT);
+ return vma_flags_test(vma_flags, VMA_WRITE_BIT) &&
+ !vma_flags_test_any(vma_flags, VMA_SHARED_BIT, VMA_STACK_BIT);
}

static inline void vma_iter_config(struct vma_iterator *vmi,
--
2.53.0