Skip to contentSkip to conversation

selftests/mm: reuse FORCE_READ to replace "asm volatile("" : "+r" (XXX));"

PATCH Superseded wang lian 2 participants 5 messages Jul 17, 2025

Subsystems (4)
lore
Share & tools
lore

wang lian

author
#0 lore raw

v2 cover selftests/mm: reuse FORCE_READ to replace "asm volatile("" : "+r" (XXX));"

This series introduces a common FORCE_READ() macro to replace the cryptic asm volatile("" : "+r" (variable));
construct used in several mm selftests. This improves code readability and maintainability by removing duplicated, hard-to-understand code.

I previously sent the refactoring patch [1] as a standalone change, following a suggestion from David. As Andrew Morton and Wei Yang correctly pointed out,
that patch was incomplete as it was missing the macro definition itself,
causing build warnings. My apologies for the noise.

[1] https://lore.kernel.org/lkml/[email protected]/

This v2 series corrects that mistake by properly structuring the changes:
- The first patch introduces the FORCE_READ() macro into the shared vm_util.h header and includes a minor cleanup for another test.
- The second patch then uses this new macro to refactor the selftests.

changes to v2:
- collected Acked-by and Reviewed-by from David and Lorenzo Stoakes.

wang lian (2):
selftests/mm: refactor common code and improve test skipping in
guard_region
selftests/mm: reuse FORCE_READ to replace "asm volatile("" : "+r"

    (XXX));"
 tools/testing/selftests/mm/cow.c              | 30 +++++++++----------
 tools/testing/selftests/mm/guard-regions.c    |  9 +-----
 tools/testing/selftests/mm/hugetlb-madvise.c  |  5 +---
 tools/testing/selftests/mm/migration.c        | 13 ++++----
 tools/testing/selftests/mm/pagemap_ioctl.c    |  4 +--
 .../selftests/mm/split_huge_page_test.c       |  4 +--
 tools/testing/selftests/mm/vm_util.h          |  7 +++++
 7 files changed, 32 insertions(+), 40 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/2 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
#2 lore raw

Re: [PATCH 1/2] selftests/mm: refactor common code and improve test skipping in guard_region

Quoted 49 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.

---
  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 +0 -0
index 2b154c287591..c20298ae98ea 100644
@@ -18,6 +18,13 @@
  #define PM_SWAP                       BIT_ULL(62)
  #define PM_PRESENT                    BIT_ULL(63)
 

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

Quoted 10 lines
tools/testing/selftests/mm/vm_util.h +7 -0
+/*
+ * 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;
 

The FORCE_READ() stuff belongs into patch #2.

Signature · 3 lines
Cheers,

David / dhildenb
author
#3 lore raw

Re: [PATCH 1/2] selftests/mm: refactor common code and improve test skipping in guard_region

Quoted 1 line

The FORCE_READ() stuff belongs into patch #2.

Hi David,

I will adjust it and resend v2, thank you David.

Best regards,
wang lian

author
#4 lore raw

PATCH v2 2/2 selftests/mm: reuse FORCE_READ to replace "asm volatile("" : "+r" (XXX));"

In reply to wang lian

Several mm selftests use the asm volatile("" : "+r" (variable)); construct to force a read of a variable, preventing the compiler from optimizing away the memory access. This idiom is cryptic and duplicated across multiple test files.

Following a suggestion from David[1], this patch refactors this common pattern into a FORCE_READ() macro

[1] https://lore.kernel.org/lkml/[email protected]/

---
 tools/testing/selftests/mm/cow.c              | 30 +++++++++----------
 tools/testing/selftests/mm/hugetlb-madvise.c  |  5 +---
 tools/testing/selftests/mm/migration.c        | 13 ++++----
 tools/testing/selftests/mm/pagemap_ioctl.c    |  4 +--
 .../selftests/mm/split_huge_page_test.c       |  4 +--
 5 files changed, 24 insertions(+), 32 deletions(-)
tools/testing/selftests/mm/cow.c +15 -15
index 788e82b00b75..d30625c18259 100644
@@ -1534,7 +1534,7 @@ static void test_ro_fast_pin(char *mem, const char *smem, size_t size)
 
 static void run_with_zeropage(non_anon_test_fn fn, const char *desc)
 {
-	char *mem, *smem, tmp;
+	char *mem, *smem;
 
 	log_test_start("%s ... with shared zeropage", desc);
 
@@ -1554,8 +1554,8 @@ static void run_with_zeropage(non_anon_test_fn fn, const char *desc)
 	}
 
 	/* Read from the page to populate the shared zeropage. */
-	tmp = *mem + *smem;
-	asm volatile("" : "+r" (tmp));
+	FORCE_READ(mem);
+	FORCE_READ(smem);
 
 	fn(mem, smem, pagesize);
 munmap:
@@ -1566,7 +1566,7 @@ static void run_with_zeropage(non_anon_test_fn fn, const char *desc)
 
 static void run_with_huge_zeropage(non_anon_test_fn fn, const char *desc)
 {
-	char *mem, *smem, *mmap_mem, *mmap_smem, tmp;
+	char *mem, *smem, *mmap_mem, *mmap_smem;
 	size_t mmap_size;
 	int ret;
 
@@ -1617,8 +1617,8 @@ static void run_with_huge_zeropage(non_anon_test_fn fn, const char *desc)
 	 * the first sub-page and test if we get another sub-page populated
 	 * automatically.
 	 */
-	tmp = *mem + *smem;
-	asm volatile("" : "+r" (tmp));
+	FORCE_READ(mem);
+	FORCE_READ(smem);
 	if (!pagemap_is_populated(pagemap_fd, mem + pagesize) ||
 	    !pagemap_is_populated(pagemap_fd, smem + pagesize)) {
 		ksft_test_result_skip("Did not get THPs populated\n");
@@ -1634,7 +1634,7 @@ static void run_with_huge_zeropage(non_anon_test_fn fn, const char *desc)
 
 static void run_with_memfd(non_anon_test_fn fn, const char *desc)
 {
-	char *mem, *smem, tmp;
+	char *mem, *smem;
 	int fd;
 
 	log_test_start("%s ... with memfd", desc);
@@ -1668,8 +1668,8 @@ static void run_with_memfd(non_anon_test_fn fn, const char *desc)
 	}
 
 	/* Fault the page in. */
-	tmp = *mem + *smem;
-	asm volatile("" : "+r" (tmp));
+	FORCE_READ(mem);
+	FORCE_READ(smem);
 
 	fn(mem, smem, pagesize);
 munmap:
@@ -1682,7 +1682,7 @@ static void run_with_memfd(non_anon_test_fn fn, const char *desc)
 
 static void run_with_tmpfile(non_anon_test_fn fn, const char *desc)
 {
-	char *mem, *smem, tmp;
+	char *mem, *smem;
 	FILE *file;
 	int fd;
 
@@ -1724,8 +1724,8 @@ static void run_with_tmpfile(non_anon_test_fn fn, const char *desc)
 	}
 
 	/* Fault the page in. */
-	tmp = *mem + *smem;
-	asm volatile("" : "+r" (tmp));
+	FORCE_READ(mem);
+	FORCE_READ(smem);
 
 	fn(mem, smem, pagesize);
 munmap:
@@ -1740,7 +1740,7 @@ static void run_with_memfd_hugetlb(non_anon_test_fn fn, const char *desc,
 				   size_t hugetlbsize)
 {
 	int flags = MFD_HUGETLB;
-	char *mem, *smem, tmp;
+	char *mem, *smem;
 	int fd;
 
 	log_test_start("%s ... with memfd hugetlb (%zu kB)", desc,
@@ -1778,8 +1778,8 @@ static void run_with_memfd_hugetlb(non_anon_test_fn fn, const char *desc,
 	}
 
 	/* Fault the page in. */
-	tmp = *mem + *smem;
-	asm volatile("" : "+r" (tmp));
+	FORCE_READ(mem);
+	FORCE_READ(smem);
 
 	fn(mem, smem, hugetlbsize);
 munmap:
tools/testing/selftests/mm/hugetlb-madvise.c +1 -4
index e74107185324..1afe14b9dc0c 100644
@@ -47,14 +47,11 @@ void write_fault_pages(void *addr, unsigned long nr_pages)
 
 void read_fault_pages(void *addr, unsigned long nr_pages)
 {
-	volatile unsigned long dummy = 0;
 	unsigned long i;
 
 	for (i = 0; i < nr_pages; i++) {
-		dummy += *((unsigned long *)(addr + (i * huge_page_size)));
-
 		/* Prevent the compiler from optimizing out the entire loop: */
-		asm volatile("" : "+r" (dummy));
+		FORCE_READ(((unsigned long *)(addr + (i * huge_page_size))));
 	}
 }
 
tools/testing/selftests/mm/migration.c +6 -7
index a306f8bab087..c5a73617796a 100644
@@ -16,6 +16,7 @@
 #include <sys/types.h>
 #include <signal.h>
 #include <time.h>
+#include "vm_util.h"
 
 #define TWOMEG		(2<<20)
 #define RUNTIME		(20)
@@ -103,15 +104,13 @@ int migrate(uint64_t *ptr, int n1, int n2)
 
 void *access_mem(void *ptr)
 {
-	volatile uint64_t y = 0;
-	volatile uint64_t *x = ptr;
-
 	while (1) {
 		pthread_testcancel();
-		y += *x;
-
-		/* Prevent the compiler from optimizing out the writes to y: */
-		asm volatile("" : "+r" (y));
+		/* Force a read from the memory pointed to by ptr. This ensures
+		 * the memory access actually happens and prevents the compiler
+		 * from optimizing away this entire loop.
+		 */
+		FORCE_READ((uint64_t *)ptr);
 	}
 
 	return NULL;
tools/testing/selftests/mm/pagemap_ioctl.c +1 -3
index c2dcda78ad31..0d4209eef0c3 100644
@@ -1525,9 +1525,7 @@ void zeropfn_tests(void)
 
 	ret = madvise(mem, hpage_size, MADV_HUGEPAGE);
 	if (!ret) {
-		char tmp = *mem;
-
-		asm volatile("" : "+r" (tmp));
+		FORCE_READ(mem);
 
 		ret = pagemap_ioctl(mem, hpage_size, &vec, 1, 0,
 				    0, PAGE_IS_PFNZERO, 0, 0, PAGE_IS_PFNZERO);
tools/testing/selftests/mm/split_huge_page_test.c +1 -3
index f0d9c035641d..05de1fc0005b 100644
@@ -399,7 +399,6 @@ int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd,
 		char **addr)
 {
 	size_t i;
-	int dummy = 0;
 	unsigned char buf[1024];
 
 	srand(time(NULL));
@@ -441,8 +440,7 @@ int create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd,
 	madvise(*addr, fd_size, MADV_HUGEPAGE);
 
 	for (size_t i = 0; i < fd_size; i++)
-		dummy += *(*addr + i);
-	asm volatile("" : "+r" (dummy));
+		FORCE_READ((*addr + i));
 
 	if (!check_huge_file(*addr, fd_size / pmd_pagesize, pmd_pagesize)) {
 		ksft_print_msg("No large pagecache folio generated, please provide a filesystem supporting large folio\n");
Signature · 1 line
2.43.0