Skip to contentSkip to conversation

selftests/mm: fix FORCE_READ to read input value correctly.

PATCH Applied Zi Yan 7 participants 12 messages Aug 5, 2025 – Aug 6, 2025

Subsystems (4)
lore
Share & tools
lore

Zi Yan

author reviewer
#0 lore raw

PATCH 1/1 selftests/mm: fix FORCE_READ to read input value correctly.

FORCE_READ() converts input value x to its pointer type then reads from address x. This is wrong. If x is a non-pointer, it would be caught it easily. But all FORCE_READ() callers are trying to read from a pointer and FORCE_READ() basically reads a pointer to a pointer instead of the original typed pointer. Almost no access violation was found, except the one from split_huge_page_test.

Fix it by implementing a simplified READ_ONCE() instead.

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

---
FORCE_READ() comes from commit 876320d71f51 ("selftests/mm: add self tests for guard page feature"). I will a separate patch to stable tree.

 tools/testing/selftests/mm/cow.c                  | 4 ++--
 tools/testing/selftests/mm/guard-regions.c        | 2 +-
 tools/testing/selftests/mm/hugetlb-madvise.c      | 4 +++-
 tools/testing/selftests/mm/migration.c            | 2 +-
 tools/testing/selftests/mm/pagemap_ioctl.c        | 2 +-
 tools/testing/selftests/mm/split_huge_page_test.c | 7 +++++--
 tools/testing/selftests/mm/vm_util.h              | 2 +-
 7 files changed, 14 insertions(+), 9 deletions(-)
tools/testing/selftests/mm/cow.c +2 -2
index d30625c18259..c744c603d688 100644
@@ -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. */
-	FORCE_READ(mem);
-	FORCE_READ(smem);
+	FORCE_READ(*mem);
+	FORCE_READ(*smem);
 
 	fn(mem, smem, pagesize);
 munmap:
tools/testing/selftests/mm/guard-regions.c +1 -1
index b0d42eb04e3a..8dd81c0a4a5a 100644
@@ -145,7 +145,7 @@ static bool try_access_buf(char *ptr, bool write)
 		if (write)
 			*ptr = 'x';
 		else
-			FORCE_READ(ptr);
+			FORCE_READ(*ptr);
 	}
 
 	signal_jump_set = false;
tools/testing/selftests/mm/hugetlb-madvise.c +3 -1
index 1afe14b9dc0c..c5940c0595be 100644
@@ -50,8 +50,10 @@ void read_fault_pages(void *addr, unsigned long nr_pages)
 	unsigned long i;
 
 	for (i = 0; i < nr_pages; i++) {
+		unsigned long *addr2 =
+			((unsigned long *)(addr + (i * huge_page_size)));
 		/* Prevent the compiler from optimizing out the entire loop: */
-		FORCE_READ(((unsigned long *)(addr + (i * huge_page_size))));
+		FORCE_READ(*addr2);
 	}
 }
 
tools/testing/selftests/mm/migration.c +1 -1
index c5a73617796a..ea945eebec2f 100644
@@ -110,7 +110,7 @@ void *access_mem(void *ptr)
 		 * the memory access actually happens and prevents the compiler
 		 * from optimizing away this entire loop.
 		 */
-		FORCE_READ((uint64_t *)ptr);
+		FORCE_READ(*(uint64_t *)ptr);
 	}
 
 	return NULL;
tools/testing/selftests/mm/pagemap_ioctl.c +1 -1
index 0d4209eef0c3..e6face7c0166 100644
@@ -1525,7 +1525,7 @@ void zeropfn_tests(void)
 
 	ret = madvise(mem, hpage_size, MADV_HUGEPAGE);
 	if (!ret) {
-		FORCE_READ(mem);
+		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 +5 -2
index 718daceb5282..3c761228e451 100644
@@ -440,8 +440,11 @@ 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++)
-		FORCE_READ((*addr + i));
+	for (size_t i = 0; i < fd_size; i++) {
+		char *addr2 = *addr + i;
+
+		FORCE_READ(*addr2);
+	}
 
 	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");
tools/testing/selftests/mm/vm_util.h +1 -1
index c20298ae98ea..b55d1809debc 100644
@@ -23,7 +23,7 @@
  * 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)
+#define FORCE_READ(x) (*(const volatile typeof(x) *)&(x))
 
 extern unsigned int __page_size;
 extern unsigned int __page_shift;
Signature · 1 line
2.47.2
#1 lore raw
Quoted 6 lines · Zi Yan

FORCE_READ() converts input value x to its pointer type then reads from address x. This is wrong. If x is a non-pointer, it would be caught it easily. But all FORCE_READ() callers are trying to read from a pointer and FORCE_READ() basically reads a pointer to a pointer instead of the original typed pointer. Almost no access violation was found, except the one from split_huge_page_test.

[...]

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

Quoted 10 lines
tools/testing/selftests/mm/vm_util.h +1 -1
index c20298ae98ea..b55d1809debc 100644
@@ -23,7 +23,7 @@
  * 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)
+#define FORCE_READ(x) (*(const volatile typeof(x) *)&(x))

So is the problem with the old code basically that it should have been something like

#define FORCE_READ(x) (*(volatile typeof(*(x)) *)(x))

to actually cast the normal pointer to a volatile pointer?

author reviewer
#2 lore raw
Quoted 22 lines · Jann Horn
Quoted 6 lines · Zi Yan

FORCE_READ() converts input value x to its pointer type then reads from address x. This is wrong. If x is a non-pointer, it would be caught it easily. But all FORCE_READ() callers are trying to read from a pointer and FORCE_READ() basically reads a pointer to a pointer instead of the original typed pointer. Almost no access violation was found, except the one from split_huge_page_test.

[...]

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

Quoted 10 lines
tools/testing/selftests/mm/vm_util.h +1 -1
index c20298ae98ea..b55d1809debc 100644
@@ -23,7 +23,7 @@
  * 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)
+#define FORCE_READ(x) (*(const volatile typeof(x) *)&(x))

So is the problem with the old code basically that it should have been something like

Quoted 3 lines

#define FORCE_READ(x) (*(volatile typeof(*(x)) *)(x))

to actually cast the normal pointer to a volatile pointer?

Yeah. That works too. I would rename it to FORCE_READ_PTR to avoid misuse. :)

Best Regards,
Yan, Zi

#3 lore raw
Quoted 24 lines · Zi Yan
Quoted 22 lines · Jann Horn
Quoted 6 lines · Zi Yan

FORCE_READ() converts input value x to its pointer type then reads from address x. This is wrong. If x is a non-pointer, it would be caught it easily. But all FORCE_READ() callers are trying to read from a pointer and FORCE_READ() basically reads a pointer to a pointer instead of the original typed pointer. Almost no access violation was found, except the one from split_huge_page_test.

[...]

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

Quoted 10 lines
tools/testing/selftests/mm/vm_util.h +1 -1
index c20298ae98ea..b55d1809debc 100644
@@ -23,7 +23,7 @@
  * 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)
+#define FORCE_READ(x) (*(const volatile typeof(x) *)&(x))

So is the problem with the old code basically that it should have been something like

Quoted 6 lines
Quoted 3 lines

#define FORCE_READ(x) (*(volatile typeof(*(x)) *)(x))

to actually cast the normal pointer to a volatile pointer?

Yeah. That works too. I would rename it to FORCE_READ_PTR to avoid misuse. :)

We were having this discussion on IRC, generally I'm fine with it as-is, though my version sort of intended to make life easier by passing a ptr, but this version makes it closer to READ_ONCE() so probably semantically a little better. :)

Quoted 3 lines

Best Regards,
Yan, Zi

Reviewed-by on v1 1/1
#4 lore raw
In reply to Zi Yan
Quoted 6 lines · Zi Yan

FORCE_READ() converts input value x to its pointer type then reads from address x. This is wrong. If x is a non-pointer, it would be caught it easily. But all FORCE_READ() callers are trying to read from a pointer and FORCE_READ() basically reads a pointer to a pointer instead of the original typed pointer. Almost no access violation was found, except the one from split_huge_page_test.

Oops, sorry about that! I had incorrectly assumed typeof() decayed the type when I wrote the guard-regions test code, and hadn't considered that we were casting to (t **) and dereffing that.

And as discussed off-list, if you deref a char array like that, and are at the end of an array, that's err... not brilliant :)

Quoted 2 lines

Fix it by implementing a simplified READ_ONCE() instead.

I sort of intended to make this easier for pointers, but the semantics of this are actually potentially a bit nicer - it's more like READ_ONCE() and you're passing in the value you're actually reading so this is probably better.

Quoted 3 lines
Fixes 3f6bfd4789a0 ("selftests/mm: reuse FORCE_READ to replace "asm volatile("" : "+r" (XXX));"")

LGTM, so:

But see nits below.

Quoted 107 lines

---
FORCE_READ() comes from commit 876320d71f51 ("selftests/mm: add self tests for guard page feature"). I will a separate patch to stable tree.

 tools/testing/selftests/mm/cow.c                  | 4 ++--
 tools/testing/selftests/mm/guard-regions.c        | 2 +-
 tools/testing/selftests/mm/hugetlb-madvise.c      | 4 +++-
 tools/testing/selftests/mm/migration.c            | 2 +-
 tools/testing/selftests/mm/pagemap_ioctl.c        | 2 +-
 tools/testing/selftests/mm/split_huge_page_test.c | 7 +++++--
 tools/testing/selftests/mm/vm_util.h              | 2 +-
 7 files changed, 14 insertions(+), 9 deletions(-)
tools/testing/selftests/mm/cow.c +2 -2
index d30625c18259..c744c603d688 100644
@@ -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. */
-	FORCE_READ(mem);
-	FORCE_READ(smem);
+	FORCE_READ(*mem);
+	FORCE_READ(*smem);
 
 	fn(mem, smem, pagesize);
 munmap:
tools/testing/selftests/mm/guard-regions.c +1 -1
index b0d42eb04e3a..8dd81c0a4a5a 100644
@@ -145,7 +145,7 @@ static bool try_access_buf(char *ptr, bool write)
 		if (write)
 			*ptr = 'x';
 		else
-			FORCE_READ(ptr);
+			FORCE_READ(*ptr);
 	}
 
 	signal_jump_set = false;
tools/testing/selftests/mm/hugetlb-madvise.c +3 -1
index 1afe14b9dc0c..c5940c0595be 100644
@@ -50,8 +50,10 @@ void read_fault_pages(void *addr, unsigned long nr_pages)
 	unsigned long i;
 
 	for (i = 0; i < nr_pages; i++) {
+		unsigned long *addr2 =
+			((unsigned long *)(addr + (i * huge_page_size)));
 		/* Prevent the compiler from optimizing out the entire loop: */
-		FORCE_READ(((unsigned long *)(addr + (i * huge_page_size))));
+		FORCE_READ(*addr2);
 	}
 }
 
tools/testing/selftests/mm/migration.c +1 -1
index c5a73617796a..ea945eebec2f 100644
@@ -110,7 +110,7 @@ void *access_mem(void *ptr)
 		 * the memory access actually happens and prevents the compiler
 		 * from optimizing away this entire loop.
 		 */
-		FORCE_READ((uint64_t *)ptr);
+		FORCE_READ(*(uint64_t *)ptr);
 	}
 
 	return NULL;
tools/testing/selftests/mm/pagemap_ioctl.c +1 -1
index 0d4209eef0c3..e6face7c0166 100644
@@ -1525,7 +1525,7 @@ void zeropfn_tests(void)
 
 	ret = madvise(mem, hpage_size, MADV_HUGEPAGE);
 	if (!ret) {
-		FORCE_READ(mem);
+		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 +5 -2
index 718daceb5282..3c761228e451 100644
@@ -440,8 +440,11 @@ 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++)
-		FORCE_READ((*addr + i));
+	for (size_t i = 0; i < fd_size; i++) {
+		char *addr2 = *addr + i;
+
+		FORCE_READ(*addr2);
+	}
 
 	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");
tools/testing/selftests/mm/vm_util.h +0 -0
index c20298ae98ea..b55d1809debc 100644

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

Quoted 5 lines
tools/testing/selftests/mm/vm_util.h +1 -1
@@ -23,7 +23,7 @@
  * 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)
+#define FORCE_READ(x) (*(const volatile typeof(x) *)&(x))

NIT: but wonder if const is necessary, and also (as discussed off-list again :) will this work with a (void) prefixed, just to a. make it clear we're reading but discarding and b. to avoid any possible compiler warning on this?

I know for some reason this form doesn't generate one currently (not sure why), but we may hit that in future.

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

2.47.2

Cheers, Lorenzo

author reviewer
#5 lore raw
Quoted 133 lines · Lorenzo Stoakes
Quoted 6 lines · Zi Yan

FORCE_READ() converts input value x to its pointer type then reads from address x. This is wrong. If x is a non-pointer, it would be caught it easily. But all FORCE_READ() callers are trying to read from a pointer and FORCE_READ() basically reads a pointer to a pointer instead of the original typed pointer. Almost no access violation was found, except the one from split_huge_page_test.

Oops, sorry about that! I had incorrectly assumed typeof() decayed the type when I wrote the guard-regions test code, and hadn't considered that we were casting to (t **) and dereffing that.

And as discussed off-list, if you deref a char array like that, and are at the end of an array, that's err... not brilliant :)

Quoted 2 lines

Fix it by implementing a simplified READ_ONCE() instead.

I sort of intended to make this easier for pointers, but the semantics of this are actually potentially a bit nicer - it's more like READ_ONCE() and you're passing in the value you're actually reading so this is probably better.

Quoted 3 lines
Fixes 3f6bfd4789a0 ("selftests/mm: reuse FORCE_READ to replace "asm volatile("" : "+r" (XXX));"")

LGTM, so:

But see nits below.

Quoted 100 lines

---
FORCE_READ() comes from commit 876320d71f51 ("selftests/mm: add self tests for guard page feature"). I will a separate patch to stable tree.

 tools/testing/selftests/mm/cow.c                  | 4 ++--
 tools/testing/selftests/mm/guard-regions.c        | 2 +-
 tools/testing/selftests/mm/hugetlb-madvise.c      | 4 +++-
 tools/testing/selftests/mm/migration.c            | 2 +-
 tools/testing/selftests/mm/pagemap_ioctl.c        | 2 +-
 tools/testing/selftests/mm/split_huge_page_test.c | 7 +++++--
 tools/testing/selftests/mm/vm_util.h              | 2 +-
 7 files changed, 14 insertions(+), 9 deletions(-)
tools/testing/selftests/mm/cow.c +2 -2
index d30625c18259..c744c603d688 100644
@@ -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. */
-	FORCE_READ(mem);
-	FORCE_READ(smem);
+	FORCE_READ(*mem);
+	FORCE_READ(*smem);
 
 	fn(mem, smem, pagesize);
 munmap:
tools/testing/selftests/mm/guard-regions.c +1 -1
index b0d42eb04e3a..8dd81c0a4a5a 100644
@@ -145,7 +145,7 @@ static bool try_access_buf(char *ptr, bool write)
 		if (write)
 			*ptr = 'x';
 		else
-			FORCE_READ(ptr);
+			FORCE_READ(*ptr);
 	}
 
 	signal_jump_set = false;
tools/testing/selftests/mm/hugetlb-madvise.c +3 -1
index 1afe14b9dc0c..c5940c0595be 100644
@@ -50,8 +50,10 @@ void read_fault_pages(void *addr, unsigned long nr_pages)
 	unsigned long i;
 
 	for (i = 0; i < nr_pages; i++) {
+		unsigned long *addr2 =
+			((unsigned long *)(addr + (i * huge_page_size)));
 		/* Prevent the compiler from optimizing out the entire loop: */
-		FORCE_READ(((unsigned long *)(addr + (i * huge_page_size))));
+		FORCE_READ(*addr2);
 	}
 }
 
tools/testing/selftests/mm/migration.c +1 -1
index c5a73617796a..ea945eebec2f 100644
@@ -110,7 +110,7 @@ void *access_mem(void *ptr)
 		 * the memory access actually happens and prevents the compiler
 		 * from optimizing away this entire loop.
 		 */
-		FORCE_READ((uint64_t *)ptr);
+		FORCE_READ(*(uint64_t *)ptr);
 	}
 
 	return NULL;
tools/testing/selftests/mm/pagemap_ioctl.c +1 -1
index 0d4209eef0c3..e6face7c0166 100644
@@ -1525,7 +1525,7 @@ void zeropfn_tests(void)
 
 	ret = madvise(mem, hpage_size, MADV_HUGEPAGE);
 	if (!ret) {
-		FORCE_READ(mem);
+		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 +5 -2
index 718daceb5282..3c761228e451 100644
@@ -440,8 +440,11 @@ 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++)
-		FORCE_READ((*addr + i));
+	for (size_t i = 0; i < fd_size; i++) {
+		char *addr2 = *addr + i;
+
+		FORCE_READ(*addr2);
+	}
 
Quoted 14 lines

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

Quoted 12 lines
tools/testing/selftests/mm/split_huge_page_test.c +0 -0
 	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");
tools/testing/selftests/mm/vm_util.h +1 -1
index c20298ae98ea..b55d1809debc 100644
@@ -23,7 +23,7 @@
  * 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)
+#define FORCE_READ(x) (*(const volatile typeof(x) *)&(x))

NIT: but wonder if const is necessary, and also (as discussed off-list

I just used READ_ONCE() code, but it is not necessary.

Quoted 3 lines

again :) will this work with a (void) prefixed, just to a. make it clear we're reading but discarding and b. to avoid any possible compiler warning on this?

Adding (void) makes no difference, at least from godbolt.

Quoted 3 lines

I know for some reason this form doesn't generate one currently (not sure why), but we may hit that in future.

Neither gcc nor clang complains without (void). My guess is that volatile is doing something there.

Best Regards,
Yan, Zi

#6 lore raw

+cc Pedro

Quoted 16 lines · Zi Yan
Quoted 13 lines · Lorenzo Stoakes

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

Quoted 10 lines · Zi Yan
tools/testing/selftests/mm/vm_util.h +1 -1
index c20298ae98ea..b55d1809debc 100644
@@ -23,7 +23,7 @@
  * 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)
+#define FORCE_READ(x) (*(const volatile typeof(x) *)&(x))

NIT: but wonder if const is necessary, and also (as discussed off-list

Quoted 1 line

I just used READ_ONCE() code, but it is not necessary.

It's not end of the world though.

Quoted 6 lines
Quoted 4 lines

again :) will this work with a (void) prefixed, just to a. make it clear we're reading but discarding and b. to avoid any possible compiler warning on this?

Adding (void) makes no difference, at least from godbolt.

Yeah I won't pretend to understand _exactly_ what the compiler is doing here, if this is working in practice across multiple compilers and read-faulting the page that's good enough for me :)

Quoted 7 lines
Quoted 4 lines

I know for some reason this form doesn't generate one currently (not sure why), but we may hit that in future.

Neither gcc nor clang complains without (void). My guess is that volatile is doing something there.

Indeed possibly, be interesting if you or Pedro who's also playing with this could nail down exactly what's going on here.

Quoted 3 lines

Best Regards,
Yan, Zi

But from my point of view this patch is fine - ship it! :)

Cheers, Lorenzo

#7 lore raw
Quoted 19 lines · Lorenzo Stoakes

+cc Pedro

Quoted 16 lines · Zi Yan
Quoted 13 lines · Lorenzo Stoakes

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

Quoted 10 lines · Zi Yan
tools/testing/selftests/mm/vm_util.h +1 -1
index c20298ae98ea..b55d1809debc 100644
@@ -23,7 +23,7 @@
  * 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)
+#define FORCE_READ(x) (*(const volatile typeof(x) *)&(x))

NIT: but wonder if const is necessary, and also (as discussed off-list

Quoted 11 lines
Quoted 1 line

I just used READ_ONCE() code, but it is not necessary.

It's not end of the world though.

Quoted 6 lines
Quoted 4 lines

again :) will this work with a (void) prefixed, just to a. make it clear we're reading but discarding and b. to avoid any possible compiler warning on this?

Adding (void) makes no difference, at least from godbolt.

I disagree with adding (void), because volatile being properly propagated into the type should hide any Wunused-value warnings (because volatile reads can have side effects, so discarding a read is most definitely valid).

And as I was seeing in https://godbolt.org/z/jnWsET1vx yesterday, GCC (and clang) can silently drop the volatile qualifier For Some Reason.

Signature · 1 line
Pedro
#8 lore raw
Quoted 29 lines · Pedro Falcato
Quoted 28 lines · Lorenzo Stoakes

+cc Pedro

Quoted 16 lines · Zi Yan
Quoted 13 lines · Lorenzo Stoakes

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

Quoted 10 lines · Zi Yan
tools/testing/selftests/mm/vm_util.h +1 -1
index c20298ae98ea..b55d1809debc 100644
@@ -23,7 +23,7 @@
  * 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)
+#define FORCE_READ(x) (*(const volatile typeof(x) *)&(x))

NIT: but wonder if const is necessary, and also (as discussed off-list

Quoted 1 line

I just used READ_ONCE() code, but it is not necessary.

It's not end of the world though.

Quoted 5 lines
Quoted 4 lines

again :) will this work with a (void) prefixed, just to a. make it clear we're reading but discarding and b. to avoid any possible compiler warning on this?

Quoted 6 lines
Quoted 2 lines
Quoted 1 line

Adding (void) makes no difference, at least from godbolt.

I disagree with adding (void), because volatile being properly propagated into the type should hide any Wunused-value warnings (because volatile reads can have side effects, so discarding a read is most definitely valid).

Yeah, I just wondered _why_.

I mean this is fine as-is. I believe Andrew's already taken the patch as a hotfix anyway.

Quoted 3 lines

And as I was seeing in https://godbolt.org/z/jnWsET1vx yesterday, GCC (and clang) can silently drop the volatile qualifier For Some Reason.

Ack, would love to know why, but don't have the time to explore so was hoping you/someone else could figure it out and tell me :P

Quoted 3 lines

--
Pedro

Cheers, Lorenzo

Reviewed-by
#9 lore raw
In reply to Zi Yan

Hi Zi Yan, Lorenzo,

Thank you for the detailed discussion. I have been following the thread closely and it has been very insightful.

Zi Yan's fix is excellent and I appreciate the rigorous analysis. Lorenzo's feedback has also deepened my own understanding of the subtleties around the FORCE_READ macro.

Out of curiosity, I also checked the (void) prefixing on Godbolt. As Zi Yan concluded, the resulting assembly appears identical.

I will be happy to join any future discussions regarding the exact behavior of volatile in this context.

For this patch, it's definitely LGTM from my side as well, so.

Thanks,
wang lian

Acked-by on v1 1/1
#10 lore raw
In reply to Zi Yan
Quoted 7 lines · Zi Yan

FORCE_READ() converts input value x to its pointer type then reads from address x. This is wrong. If x is a non-pointer, it would be caught it easily. But all FORCE_READ() callers are trying to read from a pointer and FORCE_READ() basically reads a pointer to a pointer instead of the original typed pointer. Almost no access violation was found, except the one from split_huge_page_test.

Quoted 5 lines

Fix it by implementing a simplified READ_ONCE() instead.

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

---

Ouch, thank!

Signature · 3 lines
Cheers,

David / dhildenb
Reviewed-by on v1 1/1
#11 lore raw
In reply to Zi Yan
Quoted 7 lines · Zi Yan

FORCE_READ() converts input value x to its pointer type then reads from address x. This is wrong. If x is a non-pointer, it would be caught it easily. But all FORCE_READ() callers are trying to read from a pointer and FORCE_READ() basically reads a pointer to a pointer instead of the original typed pointer. Almost no access violation was found, except the one from split_huge_page_test.

Quoted 5 lines

Fix it by implementing a simplified READ_ONCE() instead.

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

---

Signature · 2 lines
Wei Yang
Help you, Help me