From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796382067; b=q/MpvoXscj04YIzrIdFOk6Q+WrjC8Za2gHOIdF5aFGJL8gzf1fgl0O4pN6VmvDG14MeB 7bbx8nwB3alCTWlHlRchxUiKKOaz6cOVG4WeUD4oIEvF+yL0BJWxj5XH0hncG4ZOrrQkx 1+V0Sd767pGUwca2g8EMQypUvgjBmXoWo3BCpuI1CfrA0Ft5c2IcNQlJ9jU6cuMvmzpDP 6WLdHo1iCldqbCERMmHSukmDtqrHTxCGAe+cntWW3irYRscAcy8iZrNmf6/CqH6snnGqQ crPRhdl5cbaLmbElsE5DDVIHsduFVE6xrF5pth3Yf+fvmyc+46am7RldbsqhivBd83Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382067; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=TKPMM1WaID6nCsPs16GH5b+/8Kh9bmtRfLPS7VEo9WY=; b=GFyQLB7w7XyWVayF+MFtCgNU3t8jLzt0ABSpJj0L5qd5eKO7uXP/FA/1PRaB6sMVZpRI anEEkc8iCIC/tXPeGD7fRoD7ybAsz7KHI4N+K7RofgwqqPZg6hc4xWb7xmheH3N2hE3l8 fsD/ay5wAsoRmJbxO1T0wbILb3eeer0R1wlmnYwWaF8h0H4sJtYhu0espiRRrVzHVczi1 ehi+w5AxemEZmsKnbOe90xXaR7cBS3qC55nps2jRTvzztCeGEDdI9JRw6aakiPvrMKQT1 0G6svQojs68fZP9E/LNqFYbpB0Ti2ZvPCMFQ4lef6SoPu1WdDlkaaJmPldQimIaveiQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=tCmjAC7T; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C19EA606A32C; Fri, 4 Dec 2026 11:01:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=tCmjAC7T Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3F68F1F000CE; Fri, 4 Dec 2026 11:01:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382067; bh=TKPMM1WaID6nCsPs16GH5b+/8Kh9bmtRfLPS7VEo9WY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=tCmjAC7TcTwWhKLJME5ztSJv/sf7pp12tJakBWOLNTwiF7Mx1x1icclkOiaTlLO2a YbSCS7B1jul8tTl0UTd6ZUPrP/0kzJvdNT4K5WXifNdbFKgK9rhLGcT1I+GkRVFcAS UvYt9Ou0ywOv6AjlQICS/4zEPV/l1ihjU0BNaFTk= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Alexei Starovoitov , Kumar Kartikeya Dwivedi , Sasha Levin Subject: [PATCH 6.18 294/703] bpf: net_sched: Defer bpf_kfree_skb() to irq_work with irqs off Date: Fri, 4 Dec 2026 19:03:13 +0900 Message-ID: <20261204100635.608234847@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Alexei Starovoitov [ Upstream commit 73f17a98ea707b505b75c45f1ad7535f8d060195 ] 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 Link: https://patch.msgid.link/7a28c63f6b3d61442dbee1950f9a76d278a0f5be.1795550039.git.memxor@gmail.com Signed-off-by: Sasha Levin --- 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 e9bea9890777d..71a230c6fbc36 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); } -- 2.53.0