From: Alexei Starovoitov bpf_kfree_skb() is also the destructor of an skb kptr. A destructor runs wherever an object or a map value with such a field is freed. That can be with irqs off, for example: - bpf_list_add() drops the new node when @prev is not in the list, while the program holds the bpf_spin_lock of the list, which keeps irqs off. The drop frees a node that has no other reference, with its fields. - bpf_local_storage_map_free() frees the fields of each value under the lock of its bucket, which bpf_selem_unlink_nofail() takes with raw_res_spin_lock_irqsave(). A qdisc program can keep an skb in task storage. The rq lock can be held as well. A program that shares a list with the qdisc program gets there: prog drops the node with ---------------------- ---------------------------------------- sched_ext ops.dispatch bpf_obj_drop() tp_btf/sched_switch bpf_list_add(), @prev is not in the list The check from commit 94c8d1c21be4 ("bpf: Reject bpf_obj_drop() from tracing progs") catches neither. Currently bpf_kfree_skb() calls kfree_skb(), which is not made for that. The destructor of the skb may enable softirqs, take a lock that sleeps on PREEMPT_RT, or wake a task. sock_wfree() wakes the task that waits for the socket to become writable, and under the rq lock that deadlocks: __schedule() holds rq lock, irqs are off bpf_kfree_skb_dtor(skb) kfree_skb(skb) sock_wfree(skb) a task polls the socket for write __wake_up_common_lock() try_to_wake_up() the task belongs on this cpu rq_lock(rq) <- same rq dev_kfree_skb_any() does not solve it. With irqs off it leaves the skb to NET_TX_SOFTIRQ, and raising a softirq outside of an interrupt wakes ksoftirqd: __schedule() holds rq lock, irqs are off bpf_kfree_skb_dtor(skb) dev_kfree_skb_irq_reason(skb) raise_softirq_irqoff() in_interrupt() == 0 wake_up_process(ksoftirqd) ttwu_queue() cpu == this_cpu rq_lock(rq) <- same rq With irqs on it is kfree_skb(), which on PREEMPT_RT must not run with preemption off either, as after bpf_preempt_disable(): bpf_kfree_skb(skb) preempt_count() == 1 sock_wfree(skb) a writer sleeps in sk_wq __wake_up_common_lock() spin_lock_irqsave(&wq_head->lock) Free the skb from an irq_work in these contexts, like bpf_async_refcount_put() does: with irqs off, and on PREEMPT_RT whenever the caller is not preemptible. The skb waits on a lock-less list, linked through skb->ll_node. A shared skb only loses the reference of the caller: as in dev_kfree_skb_irq_reason(), the last reference is the one that may link the skb. The irq_work frees the list with dev_kfree_skb_any(). Without PREEMPT_RT it runs in hard irq context, where dev_kfree_skb_any() leaves the skb to NET_TX_SOFTIRQ and wakes nobody. With PREEMPT_RT it runs in the irq_work thread, where dev_kfree_skb_any() is kfree_skb() and may sleep. Everywhere else bpf_kfree_skb() remains kfree_skb(). Qdisc_ops::enqueue and Qdisc_ops::dequeue run with irqs on, so a qdisc pays for the test of the irq flag and no more. NET_SCH_BPF cannot be a module, so no exit path has to wait for the irq_work. Fixes: 870c28588afa ("bpf: net_sched: Add basic bpf qdisc kfuncs") Signed-off-by: Alexei Starovoitov Signed-off-by: Kumar Kartikeya Dwivedi --- net/sched/bpf_qdisc.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/net/sched/bpf_qdisc.c b/net/sched/bpf_qdisc.c index 5691c13781a8..de0c3869e403 100644 --- a/net/sched/bpf_qdisc.c +++ b/net/sched/bpf_qdisc.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -184,6 +185,19 @@ static int bpf_qdisc_gen_epilogue(struct bpf_insn *insn_buf, const struct bpf_pr return insn - insn_buf; } +static LLIST_HEAD(bpf_skb_free_list); + +static void bpf_skb_free_irq_work(struct irq_work *work) +{ + struct llist_node *list = llist_del_all(&bpf_skb_free_list); + struct sk_buff *skb, *next; + + llist_for_each_entry_safe(skb, next, list, ll_node) + dev_kfree_skb_any(skb); +} + +static DEFINE_IRQ_WORK(bpf_skb_free_work, bpf_skb_free_irq_work); + __bpf_kfunc_start_defs(); /* bpf_skb_get_hash - Get the flow hash of an skb. @@ -196,9 +210,22 @@ __bpf_kfunc u32 bpf_skb_get_hash(struct sk_buff *skb) /* bpf_kfree_skb - Release an skb's reference and drop it immediately. * @skb: The skb whose reference to be released and dropped. + * + * Also the destructor of an skb kptr, which runs wherever an object or a map + * value with such a field is freed: under a bpf_spin_lock too, with irqs off. + * rq lock can be held as well, so don't wake up ksoftirqd from here. */ __bpf_kfunc void bpf_kfree_skb(struct sk_buff *skb) { + if (irqs_disabled() || + (IS_ENABLED(CONFIG_PREEMPT_RT) && !preemptible())) { + /* Only the last reference may link the skb. */ + if (refcount_dec_not_one(&skb->users)) + return; + llist_add(&skb->ll_node, &bpf_skb_free_list); + irq_work_queue(&bpf_skb_free_work); + return; + } kfree_skb(skb); } base-commit: cbaaeff649b7aa09170cb31db001e5468c2726c0 -- 2.53.0