Skip to contentSkip to conversation

selftests/mm: refactor common code and improve test skipping in guard_region

PATCH In review wang lian 4 participants 6 messages Jul 14, 2025 – Jul 15, 2025

Subsystems (3)
lore
Share & tools
lore

wang lian

author
#0 lore raw

PATCH 1/1 selftests/mm: refactor common code and improve test skipping in guard_region

Move the generic FORCE_READ macro from guard-regions.c to the shared vm_util.h header to promote code reuse.

In guard-regions.c, replace ksft_exit_skip() with the SKIP() macro to ensure only the current test is skipped on permission failure, instead of terminating the entire test binary.

---
 tools/testing/selftests/mm/guard-regions.c | 9 +--------
 tools/testing/selftests/mm/vm_util.h       | 7 +++++++
 2 files changed, 8 insertions(+), 8 deletions(-)
tools/testing/selftests/mm/guard-regions.c +1 -8
index 93af3d3760f9..b0d42eb04e3a 100644
@@ -35,13 +35,6 @@
 static volatile sig_atomic_t signal_jump_set;
 static sigjmp_buf signal_jmp_buf;
 
-/*
- * Ignore the checkpatch warning, we must read from x but don't want to do
- * anything with it in order to trigger a read page fault. We therefore must use
- * volatile to stop the compiler from optimising this away.
- */
-#define FORCE_READ(x) (*(volatile typeof(x) *)x)
-
 /*
  * How is the test backing the mapping being tested?
  */
@@ -582,7 +575,7 @@ TEST_F(guard_regions, process_madvise)
 
 	/* OK we don't have permission to do this, skip. */
 	if (count == -1 && errno == EPERM)
-		ksft_exit_skip("No process_madvise() permissions, try running as root.\n");
+		SKIP(return, "No process_madvise() permissions, try running as root.\n");
 
 	/* Returns the number of bytes advised. */
 	ASSERT_EQ(count, 6 * page_size);
tools/testing/selftests/mm/vm_util.h +7 -0
index 2b154c287591..c20298ae98ea 100644
@@ -18,6 +18,13 @@
 #define PM_SWAP                       BIT_ULL(62)
 #define PM_PRESENT                    BIT_ULL(63)
 
+/*
+ * Ignore the checkpatch warning, we must read from x but don't want to do
+ * anything with it in order to trigger a read page fault. We therefore must use
+ * volatile to stop the compiler from optimising this away.
+ */
+#define FORCE_READ(x) (*(volatile typeof(x) *)x)
+
 extern unsigned int __page_size;
 extern unsigned int __page_shift;
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 6 lines · wang lian

Move the generic FORCE_READ macro from guard-regions.c to the shared vm_util.h header to promote code reuse.

In guard-regions.c, replace ksft_exit_skip() with the SKIP() macro to ensure only the current test is skipped on permission failure, instead of terminating the entire test binary.

These two changes look fine but they're not really related so should be separate patches. Looking briefly at guard-regions.c I see a bunch more use of ksft_exit_ functions that ought to be fixed as well, but your fix is good.

#2 lore raw
Quoted 5 lines · Mark Brown
Quoted 3 lines · wang lian

Move the generic FORCE_READ macro from guard-regions.c to the shared vm_util.h header to promote code reuse.

Quoted 8 lines
Quoted 3 lines

In guard-regions.c, replace ksft_exit_skip() with the SKIP() macro to ensure only the current test is skipped on permission failure, instead of terminating the entire test binary.

These two changes look fine but they're not really related so should be separate patches. Looking briefly at guard-regions.c I see a bunch more use of ksft_exit_ functions that ought to be fixed as well, but your fix is good.

The FORCE_READ() could be factored out separately, and as part of the same patch, replace the "asm volatile("" : "+r" (XXX));" usage in

* cow.c
* hugetlb-madvise.c
* migration.c
* pagemap_ioctl.c
* split_huge_page_test.c

Signature · 3 lines
Cheers,

David / dhildenb
#3 lore raw
Quoted 15 lines · David Hildenbrand
Quoted 5 lines · Mark Brown
Quoted 3 lines · wang lian

Move the generic FORCE_READ macro from guard-regions.c to the shared vm_util.h header to promote code reuse.

Quoted 8 lines
Quoted 3 lines

In guard-regions.c, replace ksft_exit_skip() with the SKIP() macro to ensure only the current test is skipped on permission failure, instead of terminating the entire test binary.

These two changes look fine but they're not really related so should be separate patches. Looking briefly at guard-regions.c I see a bunch more use of ksft_exit_ functions that ought to be fixed as well, but your fix is good.

Quoted 8 lines

The FORCE_READ() could be factored out separately, and as part of the same patch, replace the "asm volatile("" : "+r" (XXX));" usage in

* cow.c
* hugetlb-madvise.c
* migration.c
* pagemap_ioctl.c
* split_huge_page_test.c

Wang - Feel free to put my Reviewed-by in any such series assuming you do a straight up replace.

Quoted 6 lines

--
Cheers,

David / dhildenb

Cheers, Lorenzo

author
#4 lore raw
Quoted 29 lines · David Hildenbrand
Quoted 15 lines · Mark Brown
Quoted 5 lines · wang lian
Quoted 4 lines

Move the generic FORCE_READ macro from guard-regions.c to the shared vm_util.h header to promote code reuse.

Quoted 8 lines
Quoted 3 lines

In guard-regions.c, replace ksft_exit_skip() with the SKIP() macro to ensure only the current test is skipped on permission failure, instead of terminating the entire test binary.

These two changes look fine but they're not really related so should be separate patches. Looking briefly at guard-regions.c I see a bunch more use of ksft_exit_ functions that ought to be fixed as well, but your fix is good.

Quoted 8 lines

The FORCE_READ() could be factored out separately, and as part of the same patch, replace the "asm volatile("" : "+r" (XXX));" usage in

* cow.c
* hugetlb-madvise.c
* migration.c
* pagemap_ioctl.c
* split_huge_page_test.c

Wang - Feel free to put my Reviewed-by in any such series assuming you do a straight up replace.

Quoted 1 line
Quoted 7 lines
Quoted 5 lines

--
Cheers,

David / dhildenb

Cheers, Lorenzo

Thanks all for the helpful feedback and encouragement!

I'll prepare include the straightforward replacements in the other tests as suggested.

Thanks again!

Best regards,
wang lian

Reviewed-by on v1 1/1
#5 lore raw
In reply to wang lian
Quoted 8 lines · wang lian

Move the generic FORCE_READ macro from guard-regions.c to the shared vm_util.h header to promote code reuse.

In guard-regions.c, replace ksft_exit_skip() with the SKIP() macro to ensure only the current test is skipped on permission failure, instead of terminating the entire test binary.

LGTM, so:

Quoted 24 lines
---
 tools/testing/selftests/mm/guard-regions.c | 9 +--------
 tools/testing/selftests/mm/vm_util.h       | 7 +++++++
 2 files changed, 8 insertions(+), 8 deletions(-)
tools/testing/selftests/mm/guard-regions.c +0 -7
index 93af3d3760f9..b0d42eb04e3a 100644
@@ -35,13 +35,6 @@
 static volatile sig_atomic_t signal_jump_set;
 static sigjmp_buf signal_jmp_buf;
 
-/*
- * Ignore the checkpatch warning, we must read from x but don't want to do
- * anything with it in order to trigger a read page fault. We therefore must use
- * volatile to stop the compiler from optimising this away.
- */
-#define FORCE_READ(x) (*(volatile typeof(x) *)x)
-
 /*
  * How is the test backing the mapping being tested?
  */

tools/testing/selftests/mm/guard-regions.c TEST_F(guard_regions, process_madvise) · line 578

Quoted 5 lines
tools/testing/selftests/mm/guard-regions.c +1 -1
@@ -582,7 +575,7 @@ TEST_F(guard_regions, process_madvise)
 
 	/* OK we don't have permission to do this, skip. */
 	if (count == -1 && errno == EPERM)
-		ksft_exit_skip("No process_madvise() permissions, try running as root.\n");
+		SKIP(return, "No process_madvise() permissions, try running as root.\n");

Lol oops! :P yes thi is better.

Quoted 8 lines
tools/testing/selftests/mm/guard-regions.c +0 -0
 	/* Returns the number of bytes advised. */
 	ASSERT_EQ(count, 6 * page_size);
tools/testing/selftests/mm/vm_util.h +0 -0
index 2b154c287591..c20298ae98ea 100644

tools/testing/selftests/mm/vm_util.h line 27

Quoted 10 lines
tools/testing/selftests/mm/vm_util.h +7 -0
@@ -18,6 +18,13 @@
 #define PM_SWAP                       BIT_ULL(62)
 #define PM_PRESENT                    BIT_ULL(63)
 
+/*
+ * Ignore the checkpatch warning, we must read from x but don't want to do
+ * anything with it in order to trigger a read page fault. We therefore must use
+ * volatile to stop the compiler from optimising this away.
+ */
+#define FORCE_READ(x) (*(volatile typeof(x) *)x)
+

Cool makes sense to share this.

If this contradicts previous review, take this review to be the correct one :P

Quoted 6 lines
tools/testing/selftests/mm/vm_util.h +0 -1
 extern unsigned int __page_size;
 extern unsigned int __page_shift;
 
--

2.43.0