BPF List
 help / color / mirror / Atom feed
* [PATCH bpf v2] bpf: net_sched: Let bpf_kfree_skb() run with irqs off
@ 2026-11-23 19:58 Kumar Kartikeya Dwivedi
  2026-11-23 20:16 ` sashiko-bot
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-11-23 19:58 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	Eduard Zingerman, Emil Tsalapatis, Ihor Solodrai, kkd,
	kernel-team

From: Alexei Starovoitov <ast@kernel.org>

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.

Currently bpf_kfree_skb() calls kfree_skb(), which is not made for that.
The destructor of the skb may enable softirqs, or take a lock that sleeps
on PREEMPT_RT.

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.

Fixes: 870c28588afa ("bpf: net_sched: Add basic bpf qdisc kfuncs")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 net/sched/bpf_qdisc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/net/sched/bpf_qdisc.c b/net/sched/bpf_qdisc.c
index 5691c13781a8..e041383a848f 100644
--- a/net/sched/bpf_qdisc.c
+++ b/net/sched/bpf_qdisc.c
@@ -196,10 +196,13 @@ __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.
  */
 __bpf_kfunc void bpf_kfree_skb(struct sk_buff *skb)
 {
-	kfree_skb(skb);
+	dev_kfree_skb_any(skb);
 }
 
 __bpf_kfunc void bpf_kfree_skb_dtor(void *skb)

base-commit: 84afebd853437e44cb71f718be11d90e4c1a59dd
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf v2] bpf: net_sched: Let bpf_kfree_skb() run with irqs off
  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
  2 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-23 20:16 UTC (permalink / raw)
  To: Kumar Kartikeya Dwivedi, bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] bpf: net_sched: bpf_kfree_skb() calls kfree_skb_reason() when
  non-preemptible
--

commit b478044d031a8f9f97373bdfc829434fc4dd56c2
Author: Alexei Starovoitov <ast@kernel.org>

bpf: net_sched: Let bpf_kfree_skb() run with irqs off

This commit switches bpf_kfree_skb() from kfree_skb() to
dev_kfree_skb_any() so that skb kptr destructors can run when interrupts
are disabled.

> diff --git a/net/sched/bpf_qdisc.c b/net/sched/bpf_qdisc.c
> index 5691c13781a8b..e041383a848f2 100644
> --- a/net/sched/bpf_qdisc.c
> +++ b/net/sched/bpf_qdisc.c
> @@ -196,10 +196,13 @@ __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.
>   */
>  __bpf_kfunc void bpf_kfree_skb(struct sk_buff *skb)
>  {
> -	kfree_skb(skb);
> +	dev_kfree_skb_any(skb);
>  }

[Severity: Medium]
Can bpf_kfree_skb() still call kfree_skb_reason() while preemption is
disabled with interrupts enabled?

dev_kfree_skb_any_reason() only defers freeing when in_hardirq() ||
irqs_disabled().  A BPF list or rbtree can be protected by
bpf_res_spin_lock, which disables preemption and acquires a raw
rqspinlock_t without disabling interrupts (unlike bpf_spin_lock or
bpf_res_spin_lock_irqsave).  If bpf_list_add() fails while holding
bpf_res_spin_lock, __bpf_list_add() drops the node and invokes
bpf_kfree_skb_dtor() -> bpf_kfree_skb() with preemption disabled and IRQs
enabled (or a program can call bpf_kfree_skb() inside a
bpf_preempt_disable() section).

dev_kfree_skb_any() then takes the synchronous kfree_skb_reason() path,
which on PREEMPT_RT can acquire sleeping locks (spinlock_t or local_lock)
inside the skb destructor or allocator while non-preemptible and holding
a raw spinlock.  On non-PREEMPT_RT, if bpf_res_spin_lock() is taken in
process context, an skb destructor doing local_bh_enable() can also run
pending softirqs under the raw rqspinlock_t.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/a2d4276cbf11b1254aa7997874a6539d20f296cf.1795463894.git.memxor@gmail.com?part=1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf v2] bpf: net_sched: Let bpf_kfree_skb() run with irqs off
  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
  2 siblings, 0 replies; 4+ messages in thread
From: bot+bpf-ci @ 2026-11-23 20:33 UTC (permalink / raw)
  To: memxor, bpf
  Cc: ast, andrii, daniel, eddyz87, emil, ihor.solodrai, kkd,
	kernel-team, ast, andrii, daniel, martin.lau, eddyz87,
	yonghong.song, mason, ihor.solodrai

[-- Attachment #1: Type: text/plain, Size: 2346 bytes --]

> bpf: net_sched: Let bpf_kfree_skb() run with irqs off

TL;DR: dev_kfree_skb_any() in bpf_kfree_skb() may wake ksoftirqd with irqs
off, possibly deadlocking on a runqueue lock if a sched_ext callback drops an
skb kptr; irq_work deferral may be needed.

> diff --git a/net/sched/bpf_qdisc.c b/net/sched/bpf_qdisc.c
> --- a/net/sched/bpf_qdisc.c
> +++ b/net/sched/bpf_qdisc.c

[ ... ]

>  __bpf_kfunc void bpf_kfree_skb(struct sk_buff *skb)
>  {
> -	kfree_skb(skb);
> +	dev_kfree_skb_any(skb);
>  }

With irqs off outside of an interrupt, dev_kfree_skb_any() queues the skb
and raises NET_TX_SOFTIRQ, which can wake ksoftirqd:

dev_kfree_skb_any_reason()
    if (in_hardirq() || irqs_disabled())
        dev_kfree_skb_irq_reason(skb, reason);
dev_kfree_skb_irq_reason()
    raise_softirq_irqoff(NET_TX_SOFTIRQ);
raise_softirq_irqoff()
    if (!in_interrupt() && should_wake_ksoftirqd())
        wakeup_softirqd();

Can this deadlock if the destructor runs in an irqs-off context that holds
a runqueue lock?  For example, a sched_ext struct_ops callback such as
ops.running, called from scx_start_task_running() <- set_next_task_scx()
<- __schedule(), can drop a local object holding an skb kptr
(bpf_obj_drop_impl() is available to struct_ops programs):

bpf_obj_drop() -> __bpf_obj_drop_impl() -> bpf_obj_free_fields()
  -> bpf_kfree_skb_dtor() -> bpf_kfree_skb()
  -> dev_kfree_skb_any() -> dev_kfree_skb_irq_reason()
  -> raise_softirq_irqoff() -> wakeup_softirqd()
  -> try_to_wake_up(ksoftirqd/N) -> ttwu_queue()
  -> rq_lock(this_rq) while __schedule() already holds it

ksoftirqd is normally sleeping, so the wakeup would be taken.  Previously
kfree_skb() freed the skb inline in this context, and for an skb without a
socket destructor it woke no task.

Elsewhere BPF defers work for this case to irq_work, for example
defer_timer_wq_op() uses the same in_hardirq() || irqs_disabled() test and
queues an irq_work, and bpf_mem_free() does likewise.

Is the skb kptr destructor expected to run under scheduler locks?  If so,
would an irq_work based deferral be needed instead of dev_kfree_skb_any()?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45257226477

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf v2] bpf: net_sched: Let bpf_kfree_skb() run with irqs off
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Alexei Starovoitov @ 2026-11-24  6:32 UTC (permalink / raw)
  To: Kumar Kartikeya Dwivedi, bpf
  Cc: Andrii Nakryiko, Daniel Borkmann, Eduard Zingerman,
	Emil Tsalapatis, Ihor Solodrai, kkd, kernel-team

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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-24  6:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox