BPF List
 help / color / mirror / Atom feed
From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>, <bpf@vger.kernel.org>
Cc: "Andrii Nakryiko" <andrii@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Eduard Zingerman" <eddyz87@gmail.com>,
	"Emil Tsalapatis" <emil@etsalapatis.com>,
	"Ihor Solodrai" <ihor.solodrai@linux.dev>, <kkd@meta.com>,
	<kernel-team@meta.com>
Subject: Re: [PATCH bpf v2] bpf: net_sched: Let bpf_kfree_skb() run with irqs off
Date: Tue, 24 Nov 2026 06:32:35 +0000	[thread overview]
Message-ID: <DN38BN9BAJQS.2DSRH5XIU6EU3@gmail.com> (raw)
In-Reply-To: <a2d4276cbf11b1254aa7997874a6539d20f296cf.1795463894.git.memxor@gmail.com>

On Mon, Nov 23, 2026 at 11:58 AM Kumar Kartikeya Dwivedi <memxor@gmail.com> wrote:
> Use dev_kfree_skb_any(). With irqs on it is kfree_skb(). With irqs off the
> skb goes to the completion queue of the cpu, and NET_TX_SOFTIRQ frees it.

Bots are correct :(
With irqs off it also wakes up ksoftirqd. I missed that, sorry.
This will deadlock when the destructor runs under rq lock:

  __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

A prog that shares a list with the qdisc prog 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.
Today a UDP skb wakes nobody, unless a task waits for write space.

With irqs on it still sleeps on RT 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)

Lets go via irq_work like bpf_async_refcount_put() does.
On top of your v2 bpf_qdisc.c compiles with RT off. I didn't run it:

diff --git a/net/sched/bpf_qdisc.c b/net/sched/bpf_qdisc.c
index e041383..c569549 100644
--- a/net/sched/bpf_qdisc.c
+++ b/net/sched/bpf_qdisc.c
@@ -5,6 +5,7 @@
 #include <linux/bpf.h>
 #include <linux/btf.h>
 #include <linux/filter.h>
+#include <linux/irq_work.h>
 #include <net/pkt_sched.h>
 #include <net/pkt_cls.h>
 
@@ -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.
@@ -199,10 +213,17 @@ __bpf_kfunc u32 bpf_skb_get_hash(struct sk_buff *skb)
  *
  * 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)
 {
-	dev_kfree_skb_any(skb);
+	if (irqs_disabled() ||
+	    (IS_ENABLED(CONFIG_PREEMPT_RT) && !preemptible())) {
+		llist_add(&skb->ll_node, &bpf_skb_free_list);
+		irq_work_queue(&bpf_skb_free_work);
+		return;
+	}
+	kfree_skb(skb);
 }
 
 __bpf_kfunc void bpf_kfree_skb_dtor(void *skb)

> + * 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.

Also the drop under bpf_spin_lock has an old bug.
The node can have its own list, like struct foo in linked_list.h:

  bpf_spin_lock(&glock)               irqsave_flags = on
  bpf_list_add(&ghead, foo, prev)     prev is not in the list
    bpf_list_head_free(&foo->head)
      __bpf_spin_lock_irqsave()       irqsave_flags = off
  bpf_spin_unlock(&glock)             restores off

The prog returns with irqs off. That's a separate patch.
This helpers.c change is on top of v2 too, it builds and was never run:

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 6788489..bc0dd36 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -2302,6 +2302,7 @@ void bpf_list_head_free(const struct btf_field *field, void *list_head,
 			struct bpf_spin_lock *spin_lock)
 {
 	struct list_head *head = list_head, drain, *pos, *n;
+	unsigned long flags;
 
 	BUILD_BUG_ON(sizeof(struct list_head) > sizeof(struct bpf_list_head));
 	BUILD_BUG_ON(__alignof__(struct list_head) > __alignof__(struct bpf_list_head));
@@ -2313,7 +2314,8 @@ void bpf_list_head_free(const struct btf_field *field, void *list_head,
 	 * section, and end up doing map ops that call bpf_list_head_free for
 	 * the same map value again.
 	 */
-	__bpf_spin_lock_irqsave(spin_lock);
+	local_irq_save(flags);
+	__bpf_spin_lock(spin_lock);
 	if (!head->next || list_empty(head))
 		goto unlock;
 	list_for_each_safe(pos, n, head) {
@@ -2325,7 +2327,8 @@ void bpf_list_head_free(const struct btf_field *field, void *list_head,
 	}
 unlock:
 	INIT_LIST_HEAD(head);
-	__bpf_spin_unlock_irqrestore(spin_lock);
+	__bpf_spin_unlock(spin_lock);
+	local_irq_restore(flags);
 
 	while (!list_empty(&drain)) {
 		struct bpf_list_node_kern *node;
@@ -2362,19 +2365,22 @@ void bpf_rb_root_free(const struct btf_field *field, void *rb_root,
 	struct rb_root_cached orig_root, *root = rb_root;
 	struct bpf_rb_node_kern *node;
 	struct rb_node *pos, *n;
+	unsigned long flags;
 	void *obj;
 
 	BUILD_BUG_ON(sizeof(struct rb_root_cached) > sizeof(struct bpf_rb_root));
 	BUILD_BUG_ON(__alignof__(struct rb_root_cached) > __alignof__(struct bpf_rb_root));
 
-	__bpf_spin_lock_irqsave(spin_lock);
+	local_irq_save(flags);
+	__bpf_spin_lock(spin_lock);
 	orig_root = *root;
 	bpf_rbtree_postorder_for_each_entry_safe(pos, n, &orig_root.rb_root) {
 		node = rb_entry(pos, struct bpf_rb_node_kern, rb_node);
 		WRITE_ONCE(node->owner, BPF_PTR_POISON);
 	}
 	*root = RB_ROOT_CACHED;
-	__bpf_spin_unlock_irqrestore(spin_lock);
+	__bpf_spin_unlock(spin_lock);
+	local_irq_restore(flags);
 
 	bpf_rbtree_postorder_for_each_entry_safe(pos, n, &orig_root.rb_root) {
 		obj = pos;

pw-bot: cr

      parent reply	other threads:[~2026-11-24  6:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 19:58 [PATCH bpf v2] bpf: net_sched: Let bpf_kfree_skb() run with irqs off Kumar Kartikeya Dwivedi
2026-11-23 20:16 ` sashiko-bot
2026-11-23 20:33 ` bot+bpf-ci
2026-11-24  6:32 ` Alexei Starovoitov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DN38BN9BAJQS.2DSRH5XIU6EU3@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=kernel-team@meta.com \
    --cc=kkd@meta.com \
    --cc=memxor@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox