Skip to contentSkip to conversation

powerpc/syscall: Avoid atomic test_and_clear_thread_flag on fast path

PATCH Applied Mukesh Kumar Chaurasiya (IBM) 4 participants 4 messages Sep 29 – Oct 14 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc/syscall: Avoid atomic test_and_clear_thread_flag on fast path

In system_call_exception(), test_and_clear_thread_flag(TIF_SYSCALL_RET) performs an atomic bit test-and-clear operation on every system call entry. This introduces an unnecessary performance regression on the syscall fast path.

Combine the TIF_SYSCALL_RET check with the unlikely error condition from syscall_enter_from_user_mode_randomize_stack() using non-atomic test_thread_flag(). If the flag is set or entry failed, only then clear the flag via clear_thread_flag() and return the error value.

---
 arch/powerpc/kernel/syscall.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/powerpc/kernel/syscall.c +2 -4
index fbefe1927b10..8529827d697f 100644
@@ -18,14 +18,12 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
 	long ret;
 	syscall_fn f;
 
-	if (unlikely(!syscall_enter_from_user_mode_randomize_stack(regs, &r0))) {
+	if (unlikely(!syscall_enter_from_user_mode_randomize_stack(regs, &r0) ||
+			test_thread_flag(TIF_SYSCALL_RET))) {
 		clear_thread_flag(TIF_SYSCALL_RET);
 		return syscall_get_error(current, regs);
 	}
 
-	if (unlikely(test_and_clear_thread_flag(TIF_SYSCALL_RET)))
-		return syscall_get_error(current, regs);
-
 	if (unlikely(r0 >= NR_syscalls)) {
 		if (unlikely(trap_is_unsupported_scv(regs))) {
 			/* Unsupported scv vector */
Signature · 1 line
2.55.0
#1 lore raw
Quoted 5 lines · Mukesh Kumar Chaurasiya (IBM)

In system_call_exception(), test_and_clear_thread_flag(TIF_SYSCALL_RET) performs an atomic bit test-and-clear operation on every system call entry. This introduces an unnecessary performance regression on the syscall fast path.

Do you have numbers?

Quoted 5 lines

Combine the TIF_SYSCALL_RET check with the unlikely error condition from syscall_enter_from_user_mode_randomize_stack() using non-atomic test_thread_flag(). If the flag is set or entry failed, only then clear the flag via clear_thread_flag() and return the error value.

It probably needs a fixes tag.

Quoted 11 lines
---
  arch/powerpc/kernel/syscall.c | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
arch/powerpc/kernel/syscall.c +0 -0
index fbefe1927b10..8529827d697f 100644
@@ -18,14 +18,12 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
  	long ret;

arch/powerpc/kernel/syscall.c notrace long system_call_exception(struct pt_regs *regs, unsigned long r0) · line 26

Quoted 10 lines
arch/powerpc/kernel/syscall.c +2 -2
  	syscall_fn f;
 
-	if (unlikely(!syscall_enter_from_user_mode_randomize_stack(regs, &r0))) {
+	if (unlikely(!syscall_enter_from_user_mode_randomize_stack(regs, &r0) ||
+			test_thread_flag(TIF_SYSCALL_RET))) {
  		clear_thread_flag(TIF_SYSCALL_RET);
  		return syscall_get_error(current, regs);
  	}
 
-	if (unlikely(test_and_clear_thread_flag(TIF_SYSCALL_RET)))

I am bit surprised that test_and_clear_bit does
atomic operations even when bit is not set.

Quoted 5 lines
arch/powerpc/kernel/syscall.c +0 -2
-		return syscall_get_error(current, regs);
-
  	if (unlikely(r0 >= NR_syscalls)) {
  		if (unlikely(trap_is_unsupported_scv(regs))) {
  			/* Unsupported scv vector */
#2 lore raw

Le 29/09/2026 à 11:50, Shrikanth Hegde a écrit :

Quoted 11 lines · Mukesh Kumar Chaurasiya (IBM)
Quoted 5 lines

In system_call_exception(), test_and_clear_thread_flag(TIF_SYSCALL_RET) performs an atomic bit test-and-clear operation on every system call entry. This introduces an unnecessary performance regression on the syscall fast path.

Do you have numbers?

Quoted 7 lines
Quoted 5 lines

Combine the TIF_SYSCALL_RET check with the unlikely error condition from syscall_enter_from_user_mode_randomize_stack() using non-atomic test_thread_flag(). If the flag is set or entry failed, only then clear the flag via clear_thread_flag() and return the error value.

It probably needs a fixes tag.

It is not a bug, unless we can figure out a quantified regression.

Quoted 18 lines
Quoted 18 lines

---
  arch/powerpc/kernel/syscall.c | 6 ++----

  1 file changed, 2 insertions(+), 4 deletions(-)
arch/powerpc/kernel/syscall.c +1 -1
index fbefe1927b10..8529827d697f 100644
@@ -18,14 +18,12 @@ notrace long system_call_exception(struct pt_regs 
 *regs, unsigned long r0)
       long ret;
       syscall_fn f;
-    if (unlikely(!syscall_enter_from_user_mode_randomize_stack(regs, 
 &r0))) {
+    if (unlikely(!syscall_enter_from_user_mode_randomize_stack(regs, 

&r0) ||

Quoted 8 lines
Quoted 5 lines

+            test_thread_flag(TIF_SYSCALL_RET))) { clear_thread_flag(TIF_SYSCALL_RET);
          return syscall_get_error(current, regs);
      }
-    if (unlikely(test_and_clear_thread_flag(TIF_SYSCALL_RET)))

I am bit surprised that test_and_clear_bit does
atomic operations even when bit is not set.

It does (on ppc64 smp)

sync
1: ldarx
andc
stdcx.
bne 1b
sync

It is optimised to clear a bit that is set. When the bit is already unset you still get the full sequence allthough it does nothing.

Quoted 7 lines
Quoted 6 lines

-        return syscall_get_error(current, regs); -
      if (unlikely(r0 >= NR_syscalls)) {
          if (unlikely(trap_is_unsupported_scv(regs))) { /* Unsupported scv vector */

maintainer Applied
#3 lore raw
In reply to Mukesh Kumar Chaurasiya (IBM)
Quoted 11 lines · Mukesh Kumar Chaurasiya (IBM)

In system_call_exception(), test_and_clear_thread_flag(TIF_SYSCALL_RET) performs an atomic bit test-and-clear operation on every system call entry. This introduces an unnecessary performance regression on the syscall fast path.

Combine the TIF_SYSCALL_RET check with the unlikely error condition from syscall_enter_from_user_mode_randomize_stack() using non-atomic test_thread_flag(). If the flag is set or entry failed, only then clear the flag via clear_thread_flag() and return the error value.

[...]

Applied to powerpc/next.

[1/1] powerpc/syscall: Avoid atomic test_and_clear_thread_flag on fast path

      https://git.kernel.org/powerpc/c/66236558dfac63d9bea5b0b6a66a4d717529c118

cheers