Skip to contentSkip to conversation

memtest: use {READ,WRITE}_ONCE in memory scanning

PATCH Applied Qiang Zhang 2 participants 3 messages Mar 12, 2024 – Mar 15, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 memtest: use {READ,WRITE}_ONCE in memory scanning

memtest failed to find bad memory when compiled with clang. So use {WRITE,READ}_ONCE to access memory to avoid compiler over optimization.

---
 mm/memtest.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/memtest.c +2 -2
index 32f3e9dda837..c2c609c39119 100644
@@ -51,10 +51,10 @@ static void __init memtest(u64 pattern, phys_addr_t start_phys, phys_addr_t size
 	last_bad = 0;
 
 	for (p = start; p < end; p++)
-		*p = pattern;
+		WRITE_ONCE(*p, pattern);
 
 	for (p = start; p < end; p++, start_phys_aligned += incr) {
-		if (*p == pattern)
+		if (READ_ONCE(*p) == pattern)
 			continue;
 		if (start_phys_aligned == last_bad + incr) {
 			last_bad += incr;
Signature · 1 line
2.39.2
#1 lore raw
Quoted 2 lines · Qiang Zhang

memtest failed to find bad memory when compiled with clang. So use {WRITE,READ}_ONCE to access memory to avoid compiler over optimization.

This commit message is severely lacking in details in my opinion, especially for a patch marked for stable. Did a kernel or LLVM change cause this (i.e., has this always been an issue or is it a recent regression)? What is the transformation that LLVM does to break the test and why is using READ_ONCE() or WRITE_ONCE() sufficient to resolve it?

Quoted 26 lines
---
 mm/memtest.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/memtest.c +2 -2
index 32f3e9dda837..c2c609c39119 100644
@@ -51,10 +51,10 @@ static void __init memtest(u64 pattern, phys_addr_t start_phys, phys_addr_t size
 	last_bad = 0;
 
 	for (p = start; p < end; p++)
-		*p = pattern;
+		WRITE_ONCE(*p, pattern);
 
 	for (p = start; p < end; p++, start_phys_aligned += incr) {
-		if (*p == pattern)
+		if (READ_ONCE(*p) == pattern)
 			continue;
 		if (start_phys_aligned == last_bad + incr) {
 			last_bad += incr;
Signature · 1 line
2.39.2
#2 lore raw

Hi, Nathan

Sorry for the incomplete commit message.

I have tried to compile with gcc and clang-{11,13,14} on Debian 12. On my test environment, hypervisor emulates a range of bad memory where writes are ignored and reads always returns all ones. Memtest compiled with all clang-{11,13,14} can't find the bad memory without this patch. But gcc works fine. So it seems not a regression in clang.
I don't have expertise in compilers. But I think {READ,WRITE}_ONCE can force the compiler to treat the iterating pointer as volatile.

Welcome more comments !

BR
Qiang

-----Original Message-----
From: Nathan Chancellor <[email protected]>
Sent: Thursday, March 14, 2024 1:22 AM
To: Zhang, Qiang4 <[email protected]>
Cc: Andrew Morton <[email protected]>; Nick Desaulniers <[email protected]>; Bill Wendling <[email protected]>; Justin Stitt <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] memtest: use {READ,WRITE}_ONCE in memory scanning

Quoted 2 lines · Qiang Zhang

memtest failed to find bad memory when compiled with clang. So use {WRITE,READ}_ONCE to access memory to avoid compiler over optimization.

This commit message is severely lacking in details in my opinion, especially for a patch marked for stable. Did a kernel or LLVM change cause this (i.e., has this always been an issue or is it a recent regression)? What is the transformation that LLVM does to break the test and why is using READ_ONCE() or WRITE_ONCE() sufficient to resolve it?

Quoted 26 lines
---
 mm/memtest.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/memtest.c index renamed from mm/memtest.c +0 -0

32f3e9dda837..c2c609c39119 100644

mm/memtest.c +2 -3
@@ -51,10 +51,10 @@ static void __init memtest(u64 pattern, phys_addr_t start_phys, phys_addr_t size
 	last_bad = 0;
 
 	for (p = start; p < end; p++)
-		*p = pattern;
+		WRITE_ONCE(*p, pattern);
 
 	for (p = start; p < end; p++, start_phys_aligned += incr) {
-		if (*p == pattern)
+		if (READ_ONCE(*p) == pattern)
 			continue;
 		if (start_phys_aligned == last_bad + incr) {
 			last_bad += incr;
--

2.39.2