Skip to contentSkip to conversation

bpf: trace: send signals asynchronously if !preemptible

PATCH Applied Puranjay Mohan 4 participants 4 messages Jan 15, 2025 – Jan 21, 2025

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

PATCH 1/1 bpf: trace: send signals asynchronously if !preemptible

BPF programs can execute in all kinds of contexts and when a program running in a non-preemptible context uses the bpf_send_signal() kfunc, it will cause issues because this kfunc can sleep.

So change irqs_disabled() to !preemptible() that covers all edge
cases: preempt_count() == 0 and irqs_disabled()

Reported-by [email protected]
Fixes 1bc7896e9ef4 ("bpf: Fix deadlock with rq_lock in bpf_send_signal()")
---
 kernel/trace/bpf_trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/bpf_trace.c +1 -1
index 1b8db5aee9d3..d162c87e09a8 100644
@@ -853,7 +853,7 @@ static int bpf_send_signal_common(u32 sig, enum pid_type type, struct task_struc
 	if (unlikely(is_global_init(task)))
 		return -EPERM;
 
-	if (irqs_disabled()) {
+	if (!preemptible()) {
 		/* Do an early check on signal validity. Otherwise,
 		 * the error is lost in deferred irq_work.
 		 */
Signature · 1 line
2.40.1
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Puranjay Mohan

BPF programs can execute in all kinds of contexts and when a program running in a non-preemptible context uses the bpf_send_signal() kfunc, it will cause issues because this kfunc can sleep.

So change irqs_disabled() to !preemptible() that covers all edge
cases: preempt_count() == 0 and irqs_disabled()

Reported-by [email protected]
Fixes 1bc7896e9ef4 ("bpf: Fix deadlock with rq_lock in bpf_send_signal()")
#2 lore raw
In reply to Puranjay Mohan
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 8 lines · On Wed, 15 Jan 2025 10:36:47 +0000 you wrote:

BPF programs can execute in all kinds of contexts and when a program running in a non-preemptible context uses the bpf_send_signal() kfunc, it will cause issues because this kfunc can sleep.

So change irqs_disabled() to !preemptible() that covers all edge
cases: preempt_count() == 0 and irqs_disabled()

[...]

Here is the summary with links:
- [bpf] bpf: trace: send signals asynchronously if !preemptible

    https://git.kernel.org/bpf/bpf-next/c/87c544108b61

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#3 lore raw
In reply to Puranjay Mohan

Hi,

Quoted 20 lines · Puranjay Mohan

BPF programs can execute in all kinds of contexts and when a program running in a non-preemptible context uses the bpf_send_signal() kfunc, it will cause issues because this kfunc can sleep.

So change irqs_disabled() to !preemptible() that covers all edge
cases: preempt_count() == 0 and irqs_disabled()

Reported-by [email protected]
Fixes 1bc7896e9ef4 ("bpf: Fix deadlock with rq_lock in bpf_send_signal()")
---
 kernel/trace/bpf_trace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/bpf_trace.c +0 -0
index 1b8db5aee9d3..d162c87e09a8 100644

kernel/trace/bpf_trace.c static int bpf_send_signal_common(u32 sig, enum pid_type type, struct task_struc · line 856

Quoted 5 lines
kernel/trace/bpf_trace.c +1 -1
@@ -853,7 +853,7 @@ static int bpf_send_signal_common(u32 sig, enum pid_type type, struct task_struc
 	if (unlikely(is_global_init(task)))
 		return -EPERM;
 
-	if (irqs_disabled()) {
+	if (!preemptible()) {

Should we unfold preemptible() to "preempt_count() == 0 && !irqs_disabled()" instead, because when preemption is disabled, preemptible() will evaluate as 0 and the irq_disabled() case will be skipped ?

Quoted 3 lines
kernel/trace/bpf_trace.c +0 -0
 		/* Do an early check on signal validity. Otherwise,
 		 * the error is lost in deferred irq_work.
 		 */