From: Yuan Chen <[email protected]>
irq_work_is_busy() cannot see the per-CPU send_signal_work while
it is being filled: the check only matches after irq_work_queue()
has claimed the work. An NMI interrupting the fill therefore passes
it, both callers race for the same irq_work, and the loser's signal
is silently lost along with its task reference while the queued
work runs with a mix of both callers' fields.
Claim the work with an atomic gate before touching any of its
fields and release it only after the callback has consumed them. A
context finding the work claimed returns the documented -EBUSY,
and the return value of irq_work_queue() is now handled.
Fixes: 1bc7896e9ef4 ("bpf: Fix deadlock with rq_lock in bpf_send_signal()")
Signed-off-by: Yuan Chen <[email protected]>
---
kernel/trace/bpf_trace.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 891897f8a1b3..03552637aab9 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -823,6 +823,8 @@ const struct bpf_func_proto bpf_task_pt_regs_proto = {
struct send_signal_irq_work {
struct irq_work irq_work;
+ /* Covers the fill-to-run span which irq_work_is_busy() cannot see. */
+ atomic_t claimed;
struct task_struct *task;
u32 sig;
enum pid_type type;
@@ -842,6 +844,8 @@ static void do_bpf_send_signal(struct irq_work *entry)
group_send_sig_info(work->sig, siginfo, work->task, work->type);
put_task_struct(work->task);
+ /* Release once the fields are consumed. */
+ atomic_set_release(&work->claimed, 0);
}
static int bpf_send_signal_common(u32 sig, enum pid_type type, struct
task_struct *task, u64 value)
@@ -885,7 +889,7 @@ static int bpf_send_signal_common(u32 sig, enum pid_type
type, struct task_struc
return -EINVAL;
work = this_cpu_ptr(&send_signal_work);
- if (irq_work_is_busy(&work->irq_work))
+ if (atomic_xchg(&work->claimed, 1))
return -EBUSY;
/* Add the current task, which is the target of sending signal,
@@ -898,7 +902,12 @@ static int bpf_send_signal_common(u32 sig, enum pid_type
type, struct task_struc
copy_siginfo(&work->info, &info);
work->sig = sig;
work->type = type;
- irq_work_queue(&work->irq_work);
+ if (unlikely(!irq_work_queue(&work->irq_work))) {
+ /* Unreachable while the claim is held. */
+ put_task_struct(task);
+ atomic_set_release(&work->claimed, 0);
+ return -EBUSY;
+ }
return 0;
}
--
2.54.0