[PATCH v2 06/23] tools/testing/vma: fix VMA flag tests

Lorenzo Stoakes (Oracle)
2026-03-16T13:09:42Z

The VMA tests are incorrectly referencing NUM_VMA_FLAGS, which doesn't
exist, rather they should reference NUM_VMA_FLAG_BITS.

Additionally, remove the custom-written implementation of __mk_vma_flags()
as this means we are not testing the code as present in the kernel, rather
add the actual __mk_vma_flags() to dup.h and add #ifdef's to handle
declarations differently depending on NUM_VMA_FLAG_BITS.

Signed-off-by: Lorenzo Stoakes (Oracle) <ljs@kernel.org>
---
tools/testing/vma/include/custom.h | 19 -------
tools/testing/vma/include/dup.h | 21 ++++++-
tools/testing/vma/tests/vma.c | 88 +++++++++++++++++++++++++-----
3 files changed, 92 insertions(+), 36 deletions(-)

diff --git a/tools/testing/vma/include/custom.h b/tools/testing/vma/include/custom.h
index 7cdd0f60600a..8f33df02816a 100644
--- a/tools/testing/vma/include/custom.h
+++ b/tools/testing/vma/include/custom.h
@@ -29,8 +29,6 @@ extern unsigned long dac_mmap_min_addr;
*/
#define pr_warn_once pr_err

-#define pgtable_supports_soft_dirty() 1
-
struct anon_vma {
struct anon_vma *root;
struct rb_root_cached rb_root;
@@ -99,23 +97,6 @@ static inline void vma_lock_init(struct vm_area_struct *vma, bool reset_refcnt)
refcount_set(&vma->vm_refcnt, 0);
}

-static __always_inline vma_flags_t __mk_vma_flags(size_t count,
- const vma_flag_t *bits)
-{
- vma_flags_t flags;
- int i;
-
- /*
- * For testing purposes: allow invalid bit specification so we can
- * easily test.
- */
- vma_flags_clear_all(&flags);
- for (i = 0; i < count; i++)
- if (bits[i] < NUM_VMA_FLAG_BITS)
- vma_flags_set_flag(&flags, bits[i]);
- return flags;
-}
-
static inline unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
{
return PAGE_SIZE;
diff --git a/tools/testing/vma/include/dup.h b/tools/testing/vma/include/dup.h
index e5fdf6f5a033..64b9089a0018 100644
--- a/tools/testing/vma/include/dup.h
+++ b/tools/testing/vma/include/dup.h
@@ -854,10 +854,21 @@ static inline void vm_flags_clear(struct vm_area_struct *vma,
vma_flags_clear_word(&vma->flags, flags);
}

-static inline vma_flags_t __mk_vma_flags(size_t count, const vma_flag_t *bits);
+static __always_inline vma_flags_t __mk_vma_flags(size_t count,
+ const vma_flag_t *bits)
+{
+ vma_flags_t flags;
+ int i;
+
+ vma_flags_clear_all(&flags);
+ for (i = 0; i < count; i++)
+ vma_flags_set_flag(&flags, bits[i]);
+
+ return flags;
+}

-#define mk_vma_flags(...) __mk_vma_flags(COUNT_ARGS(__VA_ARGS__), \
- (const vma_flag_t []){__VA_ARGS__})
+#define mk_vma_flags(...) __mk_vma_flags(COUNT_ARGS(__VA_ARGS__), \
+ (const vma_flag_t []){__VA_ARGS__})

static __always_inline bool vma_flags_test(const vma_flags_t *flags,
vma_flag_t bit)
@@ -1390,3 +1401,7 @@ static inline int get_sysctl_max_map_count(void)
{
return READ_ONCE(sysctl_max_map_count);
}
+
+#ifndef pgtable_supports_soft_dirty
+#define pgtable_supports_soft_dirty() IS_ENABLED(CONFIG_MEM_SOFT_DIRTY)
+#endif
diff --git a/tools/testing/vma/tests/vma.c b/tools/testing/vma/tests/vma.c
index b2f068c3d6d0..feea6d270233 100644
--- a/tools/testing/vma/tests/vma.c
+++ b/tools/testing/vma/tests/vma.c
@@ -5,11 +5,11 @@ static bool compare_legacy_flags(vm_flags_t legacy_flags, vma_flags_t flags)
const unsigned long legacy_val = legacy_flags;
/* The lower word should contain the precise same value. */
const unsigned long flags_lower = flags.__vma_flags[0];
-#if NUM_VMA_FLAGS > BITS_PER_LONG
+#if NUM_VMA_FLAG_BITS > BITS_PER_LONG
int i;

/* All bits in higher flag values should be zero. */
- for (i = 1; i < NUM_VMA_FLAGS / BITS_PER_LONG; i++) {
+ for (i = 1; i < NUM_VMA_FLAG_BITS / BITS_PER_LONG; i++) {
if (flags.__vma_flags[i] != 0)
return false;
}
@@ -116,6 +116,7 @@ static bool test_vma_flags_cleared(void)
return true;
}

+#if NUM_VMA_FLAG_BITS > 64
/*
* Assert that VMA flag functions that operate at the system word level function
* correctly.
@@ -124,10 +125,14 @@ static bool test_vma_flags_word(void)
{
vma_flags_t flags = EMPTY_VMA_FLAGS;
const vma_flags_t comparison =
- mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT, 64, 65);
+ mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT
+
+ , 64, 65
+ );

/* Set some custom high flags. */
vma_flags_set(&flags, 64, 65);
+
/* Now overwrite the first word. */
vma_flags_overwrite_word(&flags, VM_READ | VM_WRITE);
/* Ensure they are equal. */
@@ -158,12 +163,17 @@ static bool test_vma_flags_word(void)

return true;
}
+#endif /* NUM_VMA_FLAG_BITS > 64 */

/* Ensure that vma_flags_test() and friends works correctly. */
static bool test_vma_flags_test(void)
{
const vma_flags_t flags = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT,
- VMA_EXEC_BIT, 64, 65);
+ VMA_EXEC_BIT
+#if NUM_VMA_FLAG_BITS > 64
+ , 64, 65
+#endif
+ );
struct vm_area_desc desc = {
.vma_flags = flags,
};
@@ -198,7 +208,11 @@ static bool test_vma_flags_test(void)
static bool test_vma_flags_test_any(void)
{
const vma_flags_t flags = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT,
- VMA_EXEC_BIT, 64, 65);
+ VMA_EXEC_BIT
+#if NUM_VMA_FLAG_BITS > 64
+ , 64, 65
+#endif
+ );
struct vm_area_struct vma;
struct vm_area_desc desc;

@@ -224,10 +238,12 @@ static bool test_vma_flags_test_any(void)
do_test(VMA_READ_BIT, VMA_MAYREAD_BIT, VMA_SEQ_READ_BIT);
/* However, the ...test_all() variant should NOT pass. */
do_test_all_false(VMA_READ_BIT, VMA_MAYREAD_BIT, VMA_SEQ_READ_BIT);
+#if NUM_VMA_FLAG_BITS > 64
/* But should pass for flags present. */
do_test_all_true(VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT, 64, 65);
/* Also subsets... */
do_test_all_true(VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT, 64);
+#endif
do_test_all_true(VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT);
do_test_all_true(VMA_READ_BIT, VMA_WRITE_BIT);
do_test_all_true(VMA_READ_BIT);
@@ -291,8 +307,16 @@ static bool test_vma_flags_test_any(void)
static bool test_vma_flags_clear(void)
{
vma_flags_t flags = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT,
- VMA_EXEC_BIT, 64, 65);
- vma_flags_t mask = mk_vma_flags(VMA_EXEC_BIT, 64);
+ VMA_EXEC_BIT
+#if NUM_VMA_FLAG_BITS > 64
+ , 64, 65
+#endif
+ );
+ vma_flags_t mask = mk_vma_flags(VMA_EXEC_BIT
+#if NUM_VMA_FLAG_BITS > 64
+ , 64
+#endif
+ );
struct vm_area_struct vma;
struct vm_area_desc desc;

@@ -303,6 +327,7 @@ static bool test_vma_flags_clear(void)
vma_flags_clear_mask(&flags, mask);
vma_flags_clear_mask(&vma.flags, mask);
vma_desc_clear_flags_mask(&desc, mask);
+#if NUM_VMA_FLAG_BITS > 64
ASSERT_FALSE(vma_flags_test_any(&flags, VMA_EXEC_BIT, 64));
ASSERT_FALSE(vma_flags_test_any(&vma.flags, VMA_EXEC_BIT, 64));
ASSERT_FALSE(vma_desc_test_any(&desc, VMA_EXEC_BIT, 64));
@@ -310,6 +335,7 @@ static bool test_vma_flags_clear(void)
vma_flags_set(&flags, VMA_EXEC_BIT, 64);
vma_set_flags(&vma, VMA_EXEC_BIT, 64);
vma_desc_set_flags(&desc, VMA_EXEC_BIT, 64);
+#endif

/*
* Clear the flags and assert clear worked, then reset flags back to
@@ -330,20 +356,27 @@ static bool test_vma_flags_clear(void)
do_test_and_reset(VMA_READ_BIT);
do_test_and_reset(VMA_WRITE_BIT);
do_test_and_reset(VMA_EXEC_BIT);
+#if NUM_VMA_FLAG_BITS > 64
do_test_and_reset(64);
do_test_and_reset(65);
+#endif

/* Two flags, in different orders. */
do_test_and_reset(VMA_READ_BIT, VMA_WRITE_BIT);
do_test_and_reset(VMA_READ_BIT, VMA_EXEC_BIT);
+#if NUM_VMA_FLAG_BITS > 64
do_test_and_reset(VMA_READ_BIT, 64);
do_test_and_reset(VMA_READ_BIT, 65);
+#endif
do_test_and_reset(VMA_WRITE_BIT, VMA_READ_BIT);
do_test_and_reset(VMA_WRITE_BIT, VMA_EXEC_BIT);
+#if NUM_VMA_FLAG_BITS > 64
do_test_and_reset(VMA_WRITE_BIT, 64);
do_test_and_reset(VMA_WRITE_BIT, 65);
+#endif
do_test_and_reset(VMA_EXEC_BIT, VMA_READ_BIT);
do_test_and_reset(VMA_EXEC_BIT, VMA_WRITE_BIT);
+#if NUM_VMA_FLAG_BITS > 64
do_test_and_reset(VMA_EXEC_BIT, 64);
do_test_and_reset(VMA_EXEC_BIT, 65);
do_test_and_reset(64, VMA_READ_BIT);
@@ -354,6 +387,7 @@ static bool test_vma_flags_clear(void)
do_test_and_reset(65, VMA_WRITE_BIT);
do_test_and_reset(65, VMA_EXEC_BIT);
do_test_and_reset(65, 64);
+#endif

/* Three flags. */

@@ -367,7 +401,11 @@ static bool test_vma_flags_clear(void)
static bool test_vma_flags_empty(void)
{
vma_flags_t flags = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT,
- VMA_EXEC_BIT, 64, 65);
+ VMA_EXEC_BIT
+#if NUM_VMA_FLAG_BITS > 64
+ , 64, 65
+#endif
+ );

ASSERT_FLAGS_NONEMPTY(&flags);
vma_flags_clear(&flags, VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT);
@@ -386,10 +424,19 @@ static bool test_vma_flags_empty(void)
static bool test_vma_flags_diff(void)
{
vma_flags_t flags1 = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT,
- VMA_EXEC_BIT, 64, 65);
+ VMA_EXEC_BIT
+#if NUM_VMA_FLAG_BITS > 64
+ , 64, 65
+#endif
+ );
+
vma_flags_t flags2 = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT,
VMA_EXEC_BIT, VMA_MAYWRITE_BIT,
- VMA_MAYEXEC_BIT, 64, 65, 66, 67);
+ VMA_MAYEXEC_BIT
+#if NUM_VMA_FLAG_BITS > 64
+ , 64, 65, 66, 67
+#endif
+ );
vma_flags_t diff = vma_flags_diff_pair(&flags1, &flags2);

#if NUM_VMA_FLAG_BITS > 64
@@ -432,12 +479,23 @@ static bool test_vma_flags_diff(void)
static bool test_vma_flags_and(void)
{
vma_flags_t flags1 = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT,
- VMA_EXEC_BIT, 64, 65);
+ VMA_EXEC_BIT
+#if NUM_VMA_FLAG_BITS > 64
+ , 64, 65
+#endif
+ );
vma_flags_t flags2 = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT,
VMA_EXEC_BIT, VMA_MAYWRITE_BIT,
- VMA_MAYEXEC_BIT, 64, 65, 66, 67);
- vma_flags_t flags3 = mk_vma_flags(VMA_IO_BIT, VMA_MAYBE_GUARD_BIT,
- 68, 69);
+ VMA_MAYEXEC_BIT
+#if NUM_VMA_FLAG_BITS > 64
+ , 64, 65, 66, 67
+#endif
+ );
+ vma_flags_t flags3 = mk_vma_flags(VMA_IO_BIT, VMA_MAYBE_GUARD_BIT
+#if NUM_VMA_FLAG_BITS > 64
+ , 68, 69
+#endif
+ );
vma_flags_t and = vma_flags_and_mask(&flags1, flags2);

#if NUM_VMA_FLAG_BITS > 64
@@ -502,7 +560,9 @@ static void run_vma_tests(int *num_tests, int *num_fail)
TEST(copy_vma);
TEST(vma_flags_unchanged);
TEST(vma_flags_cleared);
+#if NUM_VMA_FLAG_BITS > 64
TEST(vma_flags_word);
+#endif
TEST(vma_flags_test);
TEST(vma_flags_test_any);
TEST(vma_flags_clear);
--
2.53.0