Skip to contentSkip to conversation

selftests/powerpc/tm: Fix tcheck() reading uninitialised CR value

PATCH Applied Thibault Ferrante 4 participants 5 messages Sep 7 – Sep 14

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

PATCH 1/1 selftests/powerpc/tm: Fix tcheck() reading uninitialised CR value

tcheck() is used to check the current transaction state (active, suspended, doomed) via the "tcheck" instruction, which writes its result into CR field 0. The inline asm declared a GPR output operand for this result but never actually moved the CR into it.

Every caller (tcheck_doomed(), tcheck_active(), tcheck_suspended(), tcheck_transactional()) has effectively been testing bits of an unrelated, arbitrary register value since this helper was introduced.
The "& 4" mask discards the TDOOMED and TS_lsb (suspended) bits before they ever reach the callers, so tcheck_doomed() and tcheck_suspended() can never return true, and tcheck_transactional() degrades to being equivalent to tcheck_active().

Fix tcheck() to actually move CR into the output register with mfcr, and widen the mask from "& 4" to "& 0xf" so the full CR0 nibble (TDOOMED | TS_msb | TS_lsb | reserved) is preserved for the callers.

This bug has been present since tcheck() was introduced.

Fixes 8e03bd4e70b6 ("selftests/powerpc: Add TM tcheck helpers in C")
---
 tools/testing/selftests/powerpc/tm/tm.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/testing/selftests/powerpc/tm/tm.h +6 -2
index c03c6e778876..6024ce4ba6ff 100644
@@ -105,8 +105,12 @@ static inline bool failure_is_nesting(void)
 static inline int tcheck(void)
 {
 	long cr;
-	asm volatile ("tcheck 0" : "=r"(cr) : : "cr0");
-	return (cr >> 28) & 4;
+	asm volatile("tcheck 0;"
+		     "mfcr %0;"
+		     : "=r"(cr)
+		     :
+		     : "cr0");
+	return (cr >> 28) & 0xf;
 }
 
 static inline bool tcheck_doomed(void)
Signature · 1 line
2.55.0
Tested-by on v1 1/1
#1 lore raw
Quoted 21 lines · Thibault Ferrante

tcheck() is used to check the current transaction state (active, suspended, doomed) via the "tcheck" instruction, which writes its result into CR field 0. The inline asm declared a GPR output operand for this result but never actually moved the CR into it.

Every caller (tcheck_doomed(), tcheck_active(), tcheck_suspended(), tcheck_transactional()) has effectively been testing bits of an unrelated, arbitrary register value since this helper was introduced.
The "& 4" mask discards the TDOOMED and TS_lsb (suspended) bits before they ever reach the callers, so tcheck_doomed() and tcheck_suspended() can never return true, and tcheck_transactional() degrades to being equivalent to tcheck_active().

Fix tcheck() to actually move CR into the output register with mfcr, and widen the mask from "& 4" to "& 0xf" so the full CR0 nibble (TDOOMED | TS_msb | TS_lsb | reserved) is preserved for the callers.

Quoted 6 lines

This bug has been present since tcheck() was introduced.

Fixes 8e03bd4e70b6 ("selftests/powerpc: Add TM tcheck helpers in C")

---

Applied this patch on top of latest mainline and all the signal selftests are passing.

Please add below tags.

With this Patch:

# make run_tests 
# timeout set to 0
# selftests: powerpc/signal: signal
ok 1 selftests: powerpc/signal: signal
# timeout set to 0
# selftests: powerpc/signal: signal_tm
ok 2 selftests: powerpc/signal: signal_tm
# timeout set to 0
# selftests: powerpc/signal: sigfuz
ok 3 selftests: powerpc/signal: sigfuz
# timeout set to 0
# selftests: powerpc/signal: sigreturn_vdso
ok 4 selftests: powerpc/signal: sigreturn_vdso
# timeout set to 0
# selftests: powerpc/signal: sig_sc_double_restart
ok 5 selftests: powerpc/signal: sig_sc_double_restart
# timeout set to 0
# selftests: powerpc/signal: sigreturn_kernel
ok 6 selftests: powerpc/signal: sigreturn_kernel
# timeout set to 0
# selftests: powerpc/signal: sigreturn_unaligned
ok 7 selftests: powerpc/signal: sigreturn_unaligned

Regards,
Venkat.

Quoted 25 lines

tools/testing/selftests/powerpc/tm/tm.h | 8 ++++++--

1 file changed, 6 insertions(+), 2 deletions(-)
tools/testing/selftests/powerpc/tm/tm.h +6 -2
index c03c6e778876..6024ce4ba6ff 100644
@@ -105,8 +105,12 @@ static inline bool failure_is_nesting(void)
 static inline int tcheck(void)
 {
 long cr;
- asm volatile ("tcheck 0" : "=r"(cr) : : "cr0");
- return (cr >> 28) & 4;
+ asm volatile("tcheck 0;"
+     "mfcr %0;"
+     : "=r"(cr)
+     :
+     : "cr0");
+ return (cr >> 28) & 0xf;
 }
 
 static inline bool tcheck_doomed(void)
Signature · 1 line
2.55.0
#2 lore raw

Le 09/09/2026 à 07:28, Venkat a écrit :

Quoted 31 lines
Quoted 31 lines · Thibault Ferrante

tcheck() is used to check the current transaction state (active, suspended, doomed) via the "tcheck" instruction, which writes its result into CR field 0. The inline asm declared a GPR output operand for this result but never actually moved the CR into it.

Every caller (tcheck_doomed(), tcheck_active(), tcheck_suspended(), tcheck_transactional()) has effectively been testing bits of an unrelated, arbitrary register value since this helper was introduced.
The "& 4" mask discards the TDOOMED and TS_lsb (suspended) bits before they ever reach the callers, so tcheck_doomed() and tcheck_suspended() can never return true, and tcheck_transactional() degrades to being equivalent to tcheck_active().

Fix tcheck() to actually move CR into the output register with mfcr, and widen the mask from "& 4" to "& 0xf" so the full CR0 nibble (TDOOMED | TS_msb | TS_lsb | reserved) is preserved for the callers.

This bug has been present since tcheck() was introduced.

---

tools/testing/selftests/powerpc/tm/tm.h | 8 ++++++--

1 file changed, 6 insertions(+), 2 deletions(-)
Quoted 11 lines

tools/testing/selftests/powerpc/tm/tm.h static inline bool failure_is_nesting(void) · line 108

Quoted 11 lines
tools/testing/selftests/powerpc/tm/tm.h +1 -2
index c03c6e778876..6024ce4ba6ff 100644
@@ -105,8 +105,12 @@ static inline bool failure_is_nesting(void)
 static inline int tcheck(void)
 {
 long cr;
- asm volatile ("tcheck 0" : "=r"(cr) : : "cr0");
- return (cr >> 28) & 4;
+ asm volatile("tcheck 0;"

Why not use cr7 instead of using cr0 and doing cr >> 28 below ?

Quoted 12 lines
Quoted 11 lines
tools/testing/selftests/powerpc/tm/tm.h +5 -0
+     "mfcr %0;"
+     : "=r"(cr)
+     :
+     : "cr0");
+ return (cr >> 28) & 0xf;
 }
 
 static inline bool tcheck_doomed(void)
Signature · 1 line
2.55.0
#3 lore raw
Quoted 5 lines · Christophe Leroy (CS GROUP)
Quoted 5 lines
Quoted 5 lines
tools/testing/selftests/powerpc/tm/tm.h +0 -0
index c03c6e778876..6024ce4ba6ff 100644
@@ -105,8 +105,12 @@ static inline bool failure_is_nesting(void)
Quoted 8 lines
Quoted 6 lines

tools/testing/selftests/powerpc/tm/tm.h static inline bool failure_is_nesting(void) · line 108

Quoted 6 lines
tools/testing/selftests/powerpc/tm/tm.h +1 -2
 static inline int tcheck(void)
 {
 long cr;
- asm volatile ("tcheck 0" : "=r"(cr) : : "cr0");
- return (cr >> 28) & 4;
+ asm volatile("tcheck 0;"

Why not use cr7 instead of using cr0 and doing cr >> 28 below ?

That would work, but that's not consistent with other asm in selftest/powerpc: tbegin./tend./tabort. and vas_paste() all target cr0 and shift as needed.

maintainer Applied
#4 lore raw
In reply to Thibault Ferrante
Quoted 13 lines · Thibault Ferrante

tcheck() is used to check the current transaction state (active, suspended, doomed) via the "tcheck" instruction, which writes its result into CR field 0. The inline asm declared a GPR output operand for this result but never actually moved the CR into it.

Every caller (tcheck_doomed(), tcheck_active(), tcheck_suspended(), tcheck_transactional()) has effectively been testing bits of an unrelated, arbitrary register value since this helper was introduced.
The "& 4" mask discards the TDOOMED and TS_lsb (suspended) bits before they ever reach the callers, so tcheck_doomed() and tcheck_suspended() can never return true, and tcheck_transactional() degrades to being equivalent to tcheck_active().

Quoted 1 line

[...]

Applied to powerpc/fixes.

[1/1] selftests/powerpc/tm: Fix tcheck() reading uninitialised CR value

      https://git.kernel.org/powerpc/c/ed28b16eab705071d28edaace47189c2eb3aa108

cheers