Skip to contentSkip to conversation

signal: avoid clearing TIF_SIGPENDING in recalc_sigpending() if unset

PATCH Applied Mateusz Guzik 3 participants 4 messages Mar 3, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 signal: avoid clearing TIF_SIGPENDING in recalc_sigpending() if unset

Clearing is an atomic op and the flag is not set most of the time.

When creating and destroying threads in the same process with the pthread family, the primary bottleneck is calls to sigprocmask which take the process-wide sighand lock.

Avoiding the atomic gives me a 2% bump in start/teardown rate at 24-core scale.

---
 kernel/signal.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/signal.c +4 -3
index 081f19a24506..4727b108d842 100644
@@ -176,9 +176,10 @@ static bool recalc_sigpending_tsk(struct task_struct *t)
 
 void recalc_sigpending(void)
 {
-	if (!recalc_sigpending_tsk(current) && !freezing(current))
-		clear_thread_flag(TIF_SIGPENDING);
-
+	if (!recalc_sigpending_tsk(current) && !freezing(current)) {
+		if (test_thread_flag(TIF_SIGPENDING))
+			clear_thread_flag(TIF_SIGPENDING);
+	}
 }
 EXPORT_SYMBOL(recalc_sigpending);
 
Signature · 1 line
2.43.0
Acked-by on v1 1/1
#1 lore raw

kernel/signal.c static bool recalc_sigpending_tsk(struct task_struct *t) · line 182

Quoted 10 lines · Mateusz Guzik

void recalc_sigpending(void)
{

kernel/signal.c +4 -3
-	if (!recalc_sigpending_tsk(current) && !freezing(current))
-		clear_thread_flag(TIF_SIGPENDING);
-
+	if (!recalc_sigpending_tsk(current) && !freezing(current)) {
+		if (test_thread_flag(TIF_SIGPENDING))
+			clear_thread_flag(TIF_SIGPENDING);
+	}
#2 lore raw
In reply to Mateusz Guzik
Quoted 9 lines · On Mon, 3 Mar 2025 14:49:08 +0100 Mateusz Guzik <[email protected]> wrote:

Clearing is an atomic op and the flag is not set most of the time.

When creating and destroying threads in the same process with the pthread family, the primary bottleneck is calls to sigprocmask which take the process-wide sighand lock.

Avoiding the atomic gives me a 2% bump in start/teardown rate at 24-core scale.

Neat. Perhaps add an unlikely() also?

kernel/signal.c renamed from kernel/signal.c~signal-avoid-clearing-tif_sigpending-in-recalc_sigpending-if-unset-fix +1 -1
@@ -177,7 +177,7 @@ static bool recalc_sigpending_tsk(struct
 void recalc_sigpending(void)
 {
 	if (!recalc_sigpending_tsk(current) && !freezing(current)) {
-		if (test_thread_flag(TIF_SIGPENDING))
+		if (unlikely(test_thread_flag(TIF_SIGPENDING)))
 			clear_thread_flag(TIF_SIGPENDING);
 	}
 }

_

#3 lore raw
Quoted 26 lines · Andrew Morton
Quoted 9 lines · On Mon, 3 Mar 2025 14:49:08 +0100 Mateusz Guzik <[email protected]> wrote:

Clearing is an atomic op and the flag is not set most of the time.

When creating and destroying threads in the same process with the pthread family, the primary bottleneck is calls to sigprocmask which take the process-wide sighand lock.

Avoiding the atomic gives me a 2% bump in start/teardown rate at 24-core scale.

Neat. Perhaps add an unlikely() also?

kernel/signal.c renamed from kernel/signal.c~signal-avoid-clearing-tif_sigpending-in-recalc_sigpending-if-unset-fix +1 -1
@@ -177,7 +177,7 @@ static bool recalc_sigpending_tsk(struct
 void recalc_sigpending(void)
 {
        if (!recalc_sigpending_tsk(current) && !freezing(current)) {
-               if (test_thread_flag(TIF_SIGPENDING))
+               if (unlikely(test_thread_flag(TIF_SIGPENDING)))
                        clear_thread_flag(TIF_SIGPENDING);
        }
 }
Quoted 2 lines

_

Ye that makes sense, I don't think Oleg is going to object :)

Thanks for a quick turn around to you both,

Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>