mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] powerpc/syscall: Avoid atomic test_and_clear_thread_flag on fast path
@ 2026-09-29  7:17 Mukesh Kumar Chaurasiya (IBM)
  2026-09-29  9:50 ` Shrikanth Hegde
  0 siblings, 1 reply; 3+ messages in thread
From: Mukesh Kumar Chaurasiya (IBM) @ 2026-09-29  7:17 UTC (permalink / raw)
  To: maddy, mpe, npiggin, chleroy, ritesh.list, sshegde, mkchauras,
	ruanjinjie, tglx, mkchauras, ryan.roberts, linuxppc-dev,
	linux-kernel

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.

Signed-off-by: Mukesh Kumar Chaurasiya (IBM) <mkchauras@gmail.com>
---
 arch/powerpc/kernel/syscall.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/arch/powerpc/kernel/syscall.c b/arch/powerpc/kernel/syscall.c
index fbefe1927b10..8529827d697f 100644
--- a/arch/powerpc/kernel/syscall.c
+++ b/arch/powerpc/kernel/syscall.c
@@ -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 */
-- 
2.55.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] powerpc/syscall: Avoid atomic test_and_clear_thread_flag on fast path
  2026-09-29  7:17 [PATCH] powerpc/syscall: Avoid atomic test_and_clear_thread_flag on fast path Mukesh Kumar Chaurasiya (IBM)
@ 2026-09-29  9:50 ` Shrikanth Hegde
  2026-09-29 12:43   ` Christophe Leroy (CS GROUP)
  0 siblings, 1 reply; 3+ messages in thread
From: Shrikanth Hegde @ 2026-09-29  9:50 UTC (permalink / raw)
  To: Mukesh Kumar Chaurasiya (IBM)
  Cc: maddy, mpe, npiggin, chleroy, ritesh.list, ruanjinjie, tglx,
	mkchauras, ryan.roberts, linuxppc-dev, linux-kernel



On 9/29/26 12:47 PM, Mukesh Kumar Chaurasiya (IBM) wrote:
> 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?

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

> Signed-off-by: Mukesh Kumar Chaurasiya (IBM) <mkchauras@gmail.com>
> ---
>   arch/powerpc/kernel/syscall.c | 6 ++----
>   1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/powerpc/kernel/syscall.c b/arch/powerpc/kernel/syscall.c
> index fbefe1927b10..8529827d697f 100644
> --- a/arch/powerpc/kernel/syscall.c
> +++ b/arch/powerpc/kernel/syscall.c
> @@ -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)))

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

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] powerpc/syscall: Avoid atomic test_and_clear_thread_flag on fast path
  2026-09-29  9:50 ` Shrikanth Hegde
@ 2026-09-29 12:43   ` Christophe Leroy (CS GROUP)
  0 siblings, 0 replies; 3+ messages in thread
From: Christophe Leroy (CS GROUP) @ 2026-09-29 12:43 UTC (permalink / raw)
  To: Shrikanth Hegde, Mukesh Kumar Chaurasiya (IBM)
  Cc: maddy, mpe, npiggin, ritesh.list, ruanjinjie, tglx, mkchauras,
	ryan.roberts, linuxppc-dev, linux-kernel



Le 29/09/2026 à 11:50, Shrikanth Hegde a écrit :
> 
> 
> On 9/29/26 12:47 PM, Mukesh Kumar Chaurasiya (IBM) wrote:
>> 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?
> 
>> 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.

> 
>> Signed-off-by: Mukesh Kumar Chaurasiya (IBM) <mkchauras@gmail.com>
>> ---
>>   arch/powerpc/kernel/syscall.c | 6 ++----
>>   1 file changed, 2 insertions(+), 4 deletions(-)
>>
>> diff --git a/arch/powerpc/kernel/syscall.c b/arch/powerpc/kernel/ 
>> syscall.c
>> index fbefe1927b10..8529827d697f 100644
>> --- a/arch/powerpc/kernel/syscall.c
>> +++ b/arch/powerpc/kernel/syscall.c
>> @@ -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)))
> 
> 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.

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-09-29 12:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-29  7:17 [PATCH] powerpc/syscall: Avoid atomic test_and_clear_thread_flag on fast path Mukesh Kumar Chaurasiya (IBM)
2026-09-29  9:50 ` Shrikanth Hegde
2026-09-29 12:43   ` Christophe Leroy (CS GROUP)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®