* [PATCH bpf v1] bpf: Save irq flags on the stack when freeing a list head or rb root
@ 2026-11-24 19:54 Kumar Kartikeya Dwivedi
2026-11-25 3:10 ` patchwork-bot+netdevbpf
0 siblings, 1 reply; 2+ messages in thread
From: Kumar Kartikeya Dwivedi @ 2026-11-24 19:54 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_spin_lock() saves the irq flags in the per-cpu variable irqsave_flags,
and bpf_spin_unlock() restores them from there. One word per cpu is enough
as long as a cpu takes no second bpf_spin_lock before it releases the
first.
bpf_list_head_free() and bpf_rb_root_free() take a second one. They lock
the list or the tree that they free with __bpf_spin_lock_irqsave(), and
they can run while a program holds a bpf_spin_lock of its own.
bpf_list_add(), bpf_list_push_front(), bpf_list_push_back() and
bpf_rbtree_add() drop the node when they fail. The drop frees a node that
has no other reference, with its fields, and the node can have a list or a
tree itself, like struct foo of the selftests' 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 program returns with irqs off.
Keep the flags of the two functions in a local variable, and take the lock
with __bpf_spin_lock(). Both functions unlock before they drop the nodes,
so nothing else runs between the save and the restore.
A failed add drops the node since the commit below. Until bpf_list_add()
existed, an add failed only for a node that a list or a tree still owned,
with a reference of its own, so the drop freed the node only in a race
with the release of that reference. bpf_list_add() fails for any node when
@prev is not in the list.
Fixes: d2dcc67df910 ("bpf: Migrate bpf_rbtree_add and bpf_list_push_{front,back} to possibly fail")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
kernel/bpf/helpers.c | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 6788489a2608..bc0dd36a2799 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;
base-commit: cbaaeff649b7aa09170cb31db001e5468c2726c0
--
2.53.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH bpf v1] bpf: Save irq flags on the stack when freeing a list head or rb root
2026-11-24 19:54 [PATCH bpf v1] bpf: Save irq flags on the stack when freeing a list head or rb root Kumar Kartikeya Dwivedi
@ 2026-11-25 3:10 ` patchwork-bot+netdevbpf
0 siblings, 0 replies; 2+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-11-25 3:10 UTC (permalink / raw)
To: Kumar Kartikeya Dwivedi
Cc: bpf, ast, andrii, daniel, eddyz87, emil, ihor.solodrai, kkd,
kernel-team
Hello:
This patch was applied to bpf/bpf.git (master)
by Alexei Starovoitov <ast@kernel.org>:
On Tue, 24 Nov 2026 20:54:56 +0100 you wrote:
> From: Alexei Starovoitov <ast@kernel.org>
>
> bpf_spin_lock() saves the irq flags in the per-cpu variable irqsave_flags,
> and bpf_spin_unlock() restores them from there. One word per cpu is enough
> as long as a cpu takes no second bpf_spin_lock before it releases the
> first.
>
> [...]
Here is the summary with links:
- [bpf,v1] bpf: Save irq flags on the stack when freeing a list head or rb root
https://git.kernel.org/bpf/bpf/c/81c6c8c608b6
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-25 3:10 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 19:54 [PATCH bpf v1] bpf: Save irq flags on the stack when freeing a list head or rb root Kumar Kartikeya Dwivedi
2026-11-25 3:10 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox