On Tue, Jul 1, 2025 at 11:17 PM Kumar Kartikeya Dwivedi <memxor@xxxxxxxxx> wrote: > > Begin reporting rqspinlock deadlocks and timeout to BPF program's > stderr. > > Signed-off-by: Kumar Kartikeya Dwivedi <memxor@xxxxxxxxx> > --- > kernel/bpf/rqspinlock.c | 23 +++++++++++++++++++++++ > 1 file changed, 23 insertions(+) > Reviewed-by: Emil Tsalapatis <emil@xxxxxxxxxxxxxxx> > diff --git a/kernel/bpf/rqspinlock.c b/kernel/bpf/rqspinlock.c > index 338305c8852c..5ab354d55d82 100644 > --- a/kernel/bpf/rqspinlock.c > +++ b/kernel/bpf/rqspinlock.c > @@ -666,6 +666,27 @@ EXPORT_SYMBOL_GPL(resilient_queued_spin_lock_slowpath); > > __bpf_kfunc_start_defs(); > > +static void bpf_prog_report_rqspinlock_violation(const char *str, void *lock, bool irqsave) > +{ > + struct rqspinlock_held *rqh = this_cpu_ptr(&rqspinlock_held_locks); > + struct bpf_stream_stage ss; > + struct bpf_prog *prog; > + > + prog = bpf_prog_find_from_stack(); > + if (!prog) > + return; > + bpf_stream_stage(ss, prog, BPF_STDERR, ({ > + bpf_stream_printk(ss, "ERROR: %s for bpf_res_spin_lock%s\n", str, irqsave ? "_irqsave" : ""); > + bpf_stream_printk(ss, "Attempted lock = 0x%px\n", lock); > + bpf_stream_printk(ss, "Total held locks = %d\n", rqh->cnt); > + for (int i = 0; i < min(RES_NR_HELD, rqh->cnt); i++) > + bpf_stream_printk(ss, "Held lock[%2d] = 0x%px\n", i, rqh->locks[i]); > + bpf_stream_dump_stack(ss); > + })); > +} > + > +#define REPORT_STR(ret) ({ (ret) == -ETIMEDOUT ? "Timeout detected" : "AA or ABBA deadlock detected"; }) > + > __bpf_kfunc int bpf_res_spin_lock(struct bpf_res_spin_lock *lock) > { > int ret; > @@ -676,6 +697,7 @@ __bpf_kfunc int bpf_res_spin_lock(struct bpf_res_spin_lock *lock) > preempt_disable(); > ret = res_spin_lock((rqspinlock_t *)lock); > if (unlikely(ret)) { > + bpf_prog_report_rqspinlock_violation(REPORT_STR(ret), lock, false); > preempt_enable(); > return ret; > } > @@ -698,6 +720,7 @@ __bpf_kfunc int bpf_res_spin_lock_irqsave(struct bpf_res_spin_lock *lock, unsign > local_irq_save(flags); > ret = res_spin_lock((rqspinlock_t *)lock); > if (unlikely(ret)) { > + bpf_prog_report_rqspinlock_violation(REPORT_STR(ret), lock, true); > local_irq_restore(flags); > preempt_enable(); > return ret; > -- > 2.47.1 >