[PATCH v2 16/23] tools/testing/vma: test vma_flags_count,vma[_flags]_test_single_mask

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

Update the VMA tests to assert that vma_flags_count() behaves as expected,
as well as vma_flags_test_single_mask() and vma_test_single_mask().

For the test functions we can simply update the existing vma_test(), et
al. test to also test the single_mask variants.

We also add some explicit testing of an empty VMA flag to this test to
ensure this is handled properly.

In order to test vma_flags_count() we simply take an existing set of flags
and gradually remove flags ensuring the count remains as expected
throughout.

We also update the vma[_flags]_test_all() tests to make clear the semantics
that we expect vma[_flags]_test_all(..., EMPTY_VMA_FLAGS) to return true,
as trivially, all flags of none are always set in VMA flags.

Signed-off-by: Lorenzo Stoakes (Oracle) <ljs@kernel.org>
---
tools/testing/vma/tests/vma.c | 63 ++++++++++++++++++++++++++++++-----
1 file changed, 54 insertions(+), 9 deletions(-)

diff --git a/tools/testing/vma/tests/vma.c b/tools/testing/vma/tests/vma.c
index 1395d55a1e02..c73c3a565f1d 100644
--- a/tools/testing/vma/tests/vma.c
+++ b/tools/testing/vma/tests/vma.c
@@ -174,10 +174,10 @@ static bool test_vma_flags_word(void)
/* 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
+ vma_flags_t flags = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT,
+ VMA_EXEC_BIT
#if NUM_VMA_FLAG_BITS > 64
- , 64, 65
+ , 64, 65
#endif
);
struct vm_area_desc desc = {
@@ -187,14 +187,18 @@ static bool test_vma_flags_test(void)
.flags = flags,
};

-#define do_test(_flag) \
- ASSERT_TRUE(vma_flags_test(&flags, _flag)); \
- ASSERT_TRUE(vma_test(&vma, _flag)); \
+#define do_test(_flag) \
+ ASSERT_TRUE(vma_flags_test(&flags, _flag)); \
+ ASSERT_TRUE(vma_flags_test_single_mask(&flags, mk_vma_flags(_flag))); \
+ ASSERT_TRUE(vma_test(&vma, _flag)); \
+ ASSERT_TRUE(vma_test_single_mask(&vma, mk_vma_flags(_flag))); \
ASSERT_TRUE(vma_desc_test(&desc, _flag))

-#define do_test_false(_flag) \
- ASSERT_FALSE(vma_flags_test(&flags, _flag)); \
- ASSERT_FALSE(vma_test(&vma, _flag)); \
+#define do_test_false(_flag) \
+ ASSERT_FALSE(vma_flags_test(&flags, _flag)); \
+ ASSERT_FALSE(vma_flags_test_single_mask(&flags, mk_vma_flags(_flag))); \
+ ASSERT_FALSE(vma_test(&vma, _flag)); \
+ ASSERT_FALSE(vma_test_single_mask(&vma, mk_vma_flags(_flag))); \
ASSERT_FALSE(vma_desc_test(&desc, _flag))

do_test(VMA_READ_BIT);
@@ -212,6 +216,15 @@ static bool test_vma_flags_test(void)
#undef do_test
#undef do_test_false

+ /* We define the _single_mask() variants to return false if empty. */
+ ASSERT_FALSE(vma_flags_test_single_mask(&flags, EMPTY_VMA_FLAGS));
+ ASSERT_FALSE(vma_test_single_mask(&vma, EMPTY_VMA_FLAGS));
+ /* Even when both flags and tested flag mask are empty! */
+ flags = EMPTY_VMA_FLAGS;
+ vma.flags = EMPTY_VMA_FLAGS;
+ ASSERT_FALSE(vma_flags_test_single_mask(&flags, EMPTY_VMA_FLAGS));
+ ASSERT_FALSE(vma_test_single_mask(&vma, EMPTY_VMA_FLAGS));
+
return true;
}

@@ -309,6 +322,10 @@ static bool test_vma_flags_test_any(void)
do_test(VMA_READ_BIT, VMA_WRITE_BIT, VMA_EXEC_BIT, 64, 65);
#endif

+ /* Testing all flags against none trivially succeeds. */
+ ASSERT_TRUE(vma_flags_test_all_mask(&flags, EMPTY_VMA_FLAGS));
+ ASSERT_TRUE(vma_test_all_mask(&vma, EMPTY_VMA_FLAGS));
+
#undef do_test
#undef do_test_all_true
#undef do_test_all_false
@@ -592,6 +609,33 @@ static bool test_append_vma_flags(void)
return true;
}

+/* Assert that vma_flags_count() behaves as expected. */
+static bool test_vma_flags_count(void)
+{
+ vma_flags_t flags = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT,
+ VMA_EXEC_BIT
+#if NUM_VMA_FLAG_BITS > 64
+ , 64, 65
+#endif
+ );
+
+#if NUM_VMA_FLAG_BITS > 64
+ ASSERT_EQ(vma_flags_count(&flags), 5);
+ vma_flags_clear(&flags, 64);
+ ASSERT_EQ(vma_flags_count(&flags), 4);
+ vma_flags_clear(&flags, 65);
+#endif
+ ASSERT_EQ(vma_flags_count(&flags), 3);
+ vma_flags_clear(&flags, VMA_EXEC_BIT);
+ ASSERT_EQ(vma_flags_count(&flags), 2);
+ vma_flags_clear(&flags, VMA_WRITE_BIT);
+ ASSERT_EQ(vma_flags_count(&flags), 1);
+ vma_flags_clear(&flags, VMA_READ_BIT);
+ ASSERT_EQ(vma_flags_count(&flags), 0);
+
+ return true;
+}
+
static void run_vma_tests(int *num_tests, int *num_fail)
{
TEST(copy_vma);
@@ -607,4 +651,5 @@ static void run_vma_tests(int *num_tests, int *num_fail)
TEST(vma_flags_diff);
TEST(vma_flags_and);
TEST(append_vma_flags);
+ TEST(vma_flags_count);