* [PATCH] bpf: defer reuse of LRU hash elements until after an RCU grace period
@ 2026-11-28 13:39 Zheng Shuhao
2026-11-28 14:24 ` sashiko-bot
2026-11-28 17:32 ` Alexei Starovoitov
0 siblings, 2 replies; 3+ messages in thread
From: Zheng Shuhao @ 2026-11-28 13:39 UTC (permalink / raw)
To: bpf
Cc: ast, daniel, andrii, eddyz87, memxor, martin.lau, song,
yonghong.song, jolsa, emil, ihor.solodrai, john.fastabend,
syzbot+542d2bc50fc9545a27d6
syzbot reported a KCSAN data race between bcmp() in
htab_lru_map_update_elem() and a concurrent LRU hash update:
The lookup path compares an element's key while holding an RCU read-side
critical section. After hlist_nulls_del_rcu() removes an old element, an
RCU reader may still hold a pointer to it. The LRU path can return that
element to the free pool immediately, allowing prealloc_lru_pop() to
overwrite its key while the reader is still comparing it. The report
records a 280-byte key write racing with an 8-byte read in bcmp().
The report does not include a syz or C reproducer. My local C stress
reproducer is not identical: it issues repeated BPF_MAP_UPDATE_ELEM calls
from a writer thread, while the writer in the syzbot report goes through
generic_map_update_batch() (BPF_MAP_UPDATE_BATCH). Both exercise concurrent
LRU hash updates and lookups, but through different update paths. I can
provide my local reproducer if it would be useful.
This patch defers returning a removed element to the LRU free pool until
after an RCU grace period. It queues pending elements on the LRU, marks
them reclaimable from the RCU callback, and lets the shrink path reuse
only elements whose grace period has completed. The syscall map-update
path waits and retries if it gets -ENOMEM while deferred elements are
pending. BPF program helper updates do not use that syscall retry path
and may still see a transient -ENOMEM.
Testing:
- CONFIG_KCSAN=y, x86_64 QEMU with 2 vCPUs:
tools/testing/selftests/bpf/test_lru_map passed with exit status 0.
- A 30-second local stress run with max_entries=32 and a 64-key
working set completed 15,343,746 lookups and 5,351 updates, with no
mismatches, update errors, or -ENOMEM returns. This checked regression
behavior but did not reproduce the exact syzbot report.
- In the same synthetic workload, two baseline runs completed 19,819
and 20,092 updates in 30 seconds. The patched run completed 5,351,
about 73% fewer than the baseline average. This is one patched run,
not a production benchmark, but it indicates a substantial
performance cost.
I do not think this is a good final approach: it adds per-LRU RCU
queues and callback machinery, and syscall updates can block and retry
while BPF helper updates may still fail temporarily. Is there a simpler
or more appropriate way to keep an LRU element's key stable until
existing RCU readers have finished? I would appreciate suggestions on
how to improve this patch.
Reported-by: syzbot+542d2bc50fc9545a27d6@syzkaller.appspotmail.com
Link: https://syzkaller.appspot.com/bug?extid=542d2bc50fc9545a27d6
Signed-off-by: Zheng Shuhao <viennaoffh@gmail.com>
---
include/linux/bpf.h | 1 +
kernel/bpf/bpf_lru_list.c | 245 ++++++++++++++++++++++++++++++++++++++--------
kernel/bpf/bpf_lru_list.h | 23 +++-
kernel/bpf/hashtab.c | 47 ++++++---
kernel/bpf/syscall.c | 10 +-
5 files changed, 264 insertions(+), 62 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 4e9a4d15a..c68aed7e1 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -779,6 +779,7 @@ static inline void *bpf_arena_alloc_pages_sleepable(void *p__map, void *addr__ig
#endif
extern const struct bpf_map_ops bpf_map_offload_ops;
+bool bpf_lru_hash_wait_for_rcu(struct bpf_map *map);
/* bpf_type_flag contains a set of flags that are applicable to the values of
* arg_type, ret_type and reg_type. For example, a pointer value may be null,
diff --git a/kernel/bpf/bpf_lru_list.c b/kernel/bpf/bpf_lru_list.c
index 3858886b0..b8a9a39f4 100644
--- a/kernel/bpf/bpf_lru_list.c
+++ b/kernel/bpf/bpf_lru_list.c
@@ -2,6 +2,7 @@
/* Copyright (c) 2016 Facebook
*/
#include <linux/cpumask.h>
+#include <linux/rcupdate.h>
#include <linux/spinlock.h>
#include <linux/percpu.h>
@@ -26,6 +27,77 @@ static void bpf_lru_node_clear_ref(struct bpf_lru_node *node)
WRITE_ONCE(node->ref, 0);
}
+static u8 bpf_lru_node_pending_free(const struct bpf_lru_node *node)
+{
+ /* Pair with the release store after the RCU grace period. */
+ return smp_load_acquire(&node->pending_free);
+}
+
+static void bpf_lru_node_set_free_ready(struct bpf_lru_node *node)
+{
+ /* Publish readiness only after the RCU grace period has elapsed. */
+ smp_store_release(&node->pending_free, BPF_LRU_FREE_READY);
+}
+
+static void bpf_lru_rcu_done(struct rcu_head *rcu)
+{
+ struct bpf_lru *lru = container_of(rcu, struct bpf_lru, rcu);
+ struct llist_node *node, *next;
+
+ node = llist_del_all(&lru->rcu_waiting);
+ while (node) {
+ struct bpf_lru_node *lru_node;
+
+ next = node->next;
+ lru_node = container_of(node, struct bpf_lru_node, rcu_node);
+ bpf_lru_node_set_free_ready(lru_node);
+ node = next;
+ }
+
+ atomic_set(&lru->rcu_in_progress, 0);
+ wake_up_all(&lru->rcu_wait);
+ if (!llist_empty(&lru->rcu_pending))
+ irq_work_queue(&lru->rcu_work);
+}
+
+static void bpf_lru_rcu_work(struct irq_work *work)
+{
+ struct bpf_lru *lru = container_of(work, struct bpf_lru, rcu_work);
+ struct llist_node *node, *next;
+
+ if (atomic_xchg(&lru->rcu_in_progress, 1))
+ return;
+
+ node = llist_del_all(&lru->rcu_pending);
+ if (!node) {
+ atomic_set(&lru->rcu_in_progress, 0);
+ wake_up_all(&lru->rcu_wait);
+ return;
+ }
+
+ next = node->next;
+ while (node) {
+ struct llist_node *current_node = node;
+
+ node = next;
+ next = node ? node->next : NULL;
+ llist_add(current_node, &lru->rcu_waiting);
+ }
+
+ call_rcu_hurry(&lru->rcu, bpf_lru_rcu_done);
+}
+
+static void bpf_lru_node_defer_free(struct bpf_lru *lru,
+ struct bpf_lru_node *node)
+{
+ if (cmpxchg(&node->pending_free, BPF_LRU_FREE_NONE,
+ BPF_LRU_FREE_PENDING) != BPF_LRU_FREE_NONE)
+ return;
+
+ llist_add(&node->rcu_node, &lru->rcu_pending);
+ irq_work_queue(&lru->rcu_work);
+}
+
static void bpf_lru_list_count_inc(struct bpf_lru_list *l,
enum bpf_lru_list_type type)
{
@@ -57,7 +129,7 @@ static void __bpf_lru_node_move_to_free(struct bpf_lru_list *l,
bpf_lru_list_count_dec(l, node->type);
node->type = tgt_free_type;
- WRITE_ONCE(node->pending_free, 0);
+ WRITE_ONCE(node->pending_free, BPF_LRU_FREE_NONE);
list_move(&node->list, free_list);
}
@@ -75,7 +147,7 @@ static void __bpf_lru_node_move_in(struct bpf_lru_list *l,
bpf_lru_node_clear_ref(node);
/* Reset pending_free only when moving to the free list */
if (tgt_type == BPF_LRU_LIST_T_FREE)
- WRITE_ONCE(node->pending_free, 0);
+ WRITE_ONCE(node->pending_free, BPF_LRU_FREE_NONE);
list_move(&node->list, &l->lists[tgt_type]);
}
@@ -107,6 +179,50 @@ static void __bpf_lru_node_move(struct bpf_lru_list *l,
list_move(&node->list, &l->lists[tgt_type]);
}
+static unsigned int
+__bpf_lru_list_reclaim_ready(struct bpf_lru_list *l,
+ unsigned int tgt_nshrink,
+ struct list_head *free_list,
+ enum bpf_lru_list_type tgt_free_type,
+ bool *has_pending)
+{
+ struct bpf_lru_node *node, *tmp_node;
+ unsigned int nshrinked = 0;
+ int type;
+
+ *has_pending = false;
+ for (type = BPF_LRU_LIST_T_ACTIVE; type <= BPF_LRU_LIST_T_INACTIVE;
+ type++) {
+ list_for_each_entry_safe_reverse(node, tmp_node,
+ &l->lists[type], list) {
+ u8 pending_free = bpf_lru_node_pending_free(node);
+
+ if (pending_free != BPF_LRU_FREE_READY)
+ continue;
+
+ __bpf_lru_node_move_to_free(l, node, free_list,
+ tgt_free_type);
+ if (++nshrinked == tgt_nshrink)
+ return nshrinked;
+ }
+ }
+
+ if (!nshrinked) {
+ for (type = BPF_LRU_LIST_T_ACTIVE;
+ type <= BPF_LRU_LIST_T_INACTIVE; type++) {
+ list_for_each_entry(node, &l->lists[type], list) {
+ if (bpf_lru_node_pending_free(node) ==
+ BPF_LRU_FREE_PENDING) {
+ *has_pending = true;
+ return 0;
+ }
+ }
+ }
+ }
+
+ return nshrinked;
+}
+
static bool bpf_lru_list_inactive_low(const struct bpf_lru_list *l)
{
return l->counts[BPF_LRU_LIST_T_INACTIVE] <
@@ -201,15 +317,21 @@ __bpf_lru_list_shrink_inactive(struct bpf_lru *lru,
unsigned int i = 0;
list_for_each_entry_safe_reverse(node, tmp_node, inactive, list) {
- if (bpf_lru_node_is_ref(node) &&
- !READ_ONCE(node->pending_free)) {
- __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_ACTIVE);
- } else if (READ_ONCE(node->pending_free) ||
- lru->del_from_htab(lru->del_arg, node)) {
+ u8 pending_free = bpf_lru_node_pending_free(node);
+
+ if (pending_free == BPF_LRU_FREE_PENDING) {
+ return nshrinked + 1;
+ } else if (pending_free == BPF_LRU_FREE_READY) {
__bpf_lru_node_move_to_free(l, node, free_list,
tgt_free_type);
if (++nshrinked == tgt_nshrink)
break;
+ } else if (bpf_lru_node_is_ref(node)) {
+ __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_ACTIVE);
+ } else if (lru->del_from_htab(lru->del_arg, node)) {
+ bpf_lru_node_defer_free(lru, node);
+ if (++nshrinked == tgt_nshrink)
+ break;
}
if (++i == lru->nr_scans)
@@ -250,6 +372,12 @@ static unsigned int __bpf_lru_list_shrink(struct bpf_lru *lru,
struct bpf_lru_node *node, *tmp_node;
struct list_head *force_shrink_list;
unsigned int nshrinked;
+ bool has_pending;
+
+ nshrinked = __bpf_lru_list_reclaim_ready(l, tgt_nshrink, free_list,
+ tgt_free_type, &has_pending);
+ if (nshrinked || has_pending)
+ return nshrinked ? nshrinked : 1;
nshrinked = __bpf_lru_list_shrink_inactive(lru, l, tgt_nshrink,
free_list, tgt_free_type);
@@ -264,11 +392,17 @@ static unsigned int __bpf_lru_list_shrink(struct bpf_lru *lru,
list_for_each_entry_safe_reverse(node, tmp_node, force_shrink_list,
list) {
- if (READ_ONCE(node->pending_free) ||
- lru->del_from_htab(lru->del_arg, node)) {
+ u8 pending_free = bpf_lru_node_pending_free(node);
+
+ if (pending_free == BPF_LRU_FREE_PENDING) {
+ return 1;
+ } else if (pending_free == BPF_LRU_FREE_READY) {
__bpf_lru_node_move_to_free(l, node, free_list,
tgt_free_type);
return 1;
+ } else if (lru->del_from_htab(lru->del_arg, node)) {
+ bpf_lru_node_defer_free(lru, node);
+ return 1;
}
}
@@ -283,7 +417,9 @@ static void __local_list_flush(struct bpf_lru_list *l,
list_for_each_entry_safe_reverse(node, tmp_node,
&loc_l->pending_list, list) {
- if (READ_ONCE(node->pending_free))
+ if (bpf_lru_node_pending_free(node) == BPF_LRU_FREE_PENDING)
+ continue;
+ else if (bpf_lru_node_pending_free(node) == BPF_LRU_FREE_READY)
__bpf_lru_node_move_in(l, node, BPF_LRU_LIST_T_FREE);
else if (bpf_lru_node_is_ref(node))
__bpf_lru_node_move_in(l, node, BPF_LRU_LIST_T_ACTIVE);
@@ -293,19 +429,6 @@ static void __local_list_flush(struct bpf_lru_list *l,
}
}
-static void bpf_lru_list_push_free(struct bpf_lru_list *l,
- struct bpf_lru_node *node)
-{
- unsigned long flags;
-
- if (raw_res_spin_lock_irqsave(&l->lock, flags)) {
- WRITE_ONCE(node->pending_free, 1);
- return;
- }
- __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_FREE);
- raw_res_spin_unlock_irqrestore(&l->lock, flags);
-}
-
static void bpf_lru_list_pop_free_to_local(struct bpf_lru *lru,
struct bpf_lru_locallist *loc_l)
{
@@ -355,7 +478,7 @@ static void __local_list_add_pending(struct bpf_lru *lru,
*(u32 *)((void *)node + lru->hash_offset) = hash;
node->cpu = cpu;
node->type = BPF_LRU_LOCAL_LIST_T_PENDING;
- WRITE_ONCE(node->pending_free, 0);
+ WRITE_ONCE(node->pending_free, BPF_LRU_FREE_NONE);
bpf_lru_node_clear_ref(node);
list_add(&node->list, &loc_l->pending_list);
}
@@ -381,12 +504,20 @@ __local_list_pop_pending(struct bpf_lru *lru, struct bpf_lru_locallist *loc_l)
ignore_ref:
/* Get from the tail (i.e. older element) of the pending list. */
list_for_each_entry_reverse(node, &loc_l->pending_list, list) {
+ if (bpf_lru_node_pending_free(node) == BPF_LRU_FREE_PENDING)
+ continue;
+
if ((!bpf_lru_node_is_ref(node) || force) &&
- (READ_ONCE(node->pending_free) ||
- lru->del_from_htab(lru->del_arg, node))) {
+ bpf_lru_node_pending_free(node) == BPF_LRU_FREE_READY) {
list_del(&node->list);
return node;
}
+
+ if ((!bpf_lru_node_is_ref(node) || force) &&
+ lru->del_from_htab(lru->del_arg, node)) {
+ bpf_lru_node_defer_free(lru, node);
+ return NULL;
+ }
}
if (!force) {
@@ -501,7 +632,7 @@ static struct bpf_lru_node *bpf_common_lru_pop_free(struct bpf_lru *lru,
*/
node->type = BPF_LRU_LOCAL_LIST_T_FREE;
bpf_lru_node_clear_ref(node);
- WRITE_ONCE(node->pending_free, 0);
+ WRITE_ONCE(node->pending_free, BPF_LRU_FREE_NONE);
llist_add(&node->llist, &loc_l->free_llist);
return NULL;
}
@@ -533,30 +664,30 @@ static void bpf_common_lru_push_free(struct bpf_lru *lru,
struct bpf_lru_locallist *loc_l;
loc_l = per_cpu_ptr(lru->common_lru.local_list, node->cpu);
-
if (raw_res_spin_lock_irqsave(&loc_l->lock, flags)) {
- WRITE_ONCE(node->pending_free, 1);
+ WRITE_ONCE(node->pending_free, BPF_LRU_FREE_READY);
return;
}
-
if (unlikely(READ_ONCE(node->type) != BPF_LRU_LOCAL_LIST_T_PENDING)) {
- raw_res_spin_unlock_irqrestore(&loc_l->lock,
- flags);
+ raw_res_spin_unlock_irqrestore(&loc_l->lock, flags);
goto check_lru_list;
}
-
node->type = BPF_LRU_LOCAL_LIST_T_FREE;
bpf_lru_node_clear_ref(node);
list_del(&node->list);
-
raw_res_spin_unlock_irqrestore(&loc_l->lock, flags);
-
llist_add(&node->llist, &loc_l->free_llist);
return;
}
check_lru_list:
- bpf_lru_list_push_free(&lru->common_lru.lru_list, node);
+ if (raw_res_spin_lock_irqsave(&lru->common_lru.lru_list.lock, flags)) {
+ WRITE_ONCE(node->pending_free, BPF_LRU_FREE_READY);
+ return;
+ }
+ __bpf_lru_node_move(&lru->common_lru.lru_list, node,
+ BPF_LRU_LIST_T_FREE);
+ raw_res_spin_unlock_irqrestore(&lru->common_lru.lru_list.lock, flags);
}
static void bpf_percpu_lru_push_free(struct bpf_lru *lru,
@@ -566,14 +697,11 @@ static void bpf_percpu_lru_push_free(struct bpf_lru *lru,
unsigned long flags;
l = per_cpu_ptr(lru->percpu_lru, node->cpu);
-
if (raw_res_spin_lock_irqsave(&l->lock, flags)) {
- WRITE_ONCE(node->pending_free, 1);
+ WRITE_ONCE(node->pending_free, BPF_LRU_FREE_READY);
return;
}
-
__bpf_lru_node_move(l, node, BPF_LRU_LIST_T_FREE);
-
raw_res_spin_unlock_irqrestore(&l->lock, flags);
}
@@ -585,6 +713,25 @@ void bpf_lru_push_free(struct bpf_lru *lru, struct bpf_lru_node *node)
bpf_common_lru_push_free(lru, node);
}
+void bpf_lru_defer_free(struct bpf_lru *lru, struct bpf_lru_node *node)
+{
+ bpf_lru_node_defer_free(lru, node);
+}
+
+bool bpf_lru_wait_for_rcu(struct bpf_lru *lru)
+{
+ if (!atomic_read(&lru->rcu_in_progress) &&
+ llist_empty(&lru->rcu_pending) &&
+ llist_empty(&lru->rcu_waiting))
+ return false;
+
+ wait_event(lru->rcu_wait,
+ !atomic_read(&lru->rcu_in_progress) &&
+ llist_empty(&lru->rcu_pending) &&
+ llist_empty(&lru->rcu_waiting));
+ return true;
+}
+
static void bpf_common_lru_populate(struct bpf_lru *lru, void *buf,
u32 node_offset, u32 elem_size,
u32 nr_elems)
@@ -597,7 +744,7 @@ static void bpf_common_lru_populate(struct bpf_lru *lru, void *buf,
node = (struct bpf_lru_node *)(buf + node_offset);
node->type = BPF_LRU_LIST_T_FREE;
- node->pending_free = 0;
+ node->pending_free = BPF_LRU_FREE_NONE;
bpf_lru_node_clear_ref(node);
list_add(&node->list, &l->lists[BPF_LRU_LIST_T_FREE]);
buf += elem_size;
@@ -627,7 +774,7 @@ static void bpf_percpu_lru_populate(struct bpf_lru *lru, void *buf,
node = (struct bpf_lru_node *)(buf + node_offset);
node->cpu = cpu;
node->type = BPF_LRU_LIST_T_FREE;
- node->pending_free = 0;
+ node->pending_free = BPF_LRU_FREE_NONE;
bpf_lru_node_clear_ref(node);
list_add(&node->list, &l->lists[BPF_LRU_LIST_T_FREE]);
i++;
@@ -680,6 +827,12 @@ int bpf_lru_init(struct bpf_lru *lru, bool percpu, u32 hash_offset,
{
int cpu;
+ init_llist_head(&lru->rcu_pending);
+ init_llist_head(&lru->rcu_waiting);
+ init_irq_work(&lru->rcu_work, bpf_lru_rcu_work);
+ atomic_set(&lru->rcu_in_progress, 0);
+ init_waitqueue_head(&lru->rcu_wait);
+
if (percpu) {
lru->percpu_lru = alloc_percpu(struct bpf_lru_list);
if (!lru->percpu_lru)
@@ -720,6 +873,14 @@ int bpf_lru_init(struct bpf_lru *lru, bool percpu, u32 hash_offset,
void bpf_lru_destroy(struct bpf_lru *lru)
{
+ do {
+ irq_work_sync(&lru->rcu_work);
+ rcu_barrier();
+ irq_work_sync(&lru->rcu_work);
+ } while (atomic_read(&lru->rcu_in_progress) ||
+ !llist_empty(&lru->rcu_pending) ||
+ !llist_empty(&lru->rcu_waiting));
+
if (lru->percpu)
free_percpu(lru->percpu_lru);
else
diff --git a/kernel/bpf/bpf_lru_list.h b/kernel/bpf/bpf_lru_list.h
index 8d0ee6162..a0f36a8c5 100644
--- a/kernel/bpf/bpf_lru_list.h
+++ b/kernel/bpf/bpf_lru_list.h
@@ -5,8 +5,11 @@
#define __BPF_LRU_LIST_H_
#include <linux/cache.h>
+#include <linux/irq_work.h>
#include <linux/list.h>
#include <linux/llist.h>
+#include <linux/rcupdate.h>
+#include <linux/wait.h>
#include <asm/rqspinlock.h>
#define NR_BPF_LRU_LIST_T (3)
@@ -33,11 +36,15 @@ struct bpf_lru_node {
u16 cpu;
u8 type;
u8 ref;
- /*
- * Marks nodes whose *_push_free() lock acquire failed; reclaimed
- * by flush/shrink which honor the flag instead of del_from_htab().
- */
+ /* Tracks whether this node is waiting for, or ready after, an RCU grace period. */
u8 pending_free;
+ struct llist_node rcu_node;
+};
+
+enum bpf_lru_free_state {
+ BPF_LRU_FREE_NONE,
+ BPF_LRU_FREE_PENDING,
+ BPF_LRU_FREE_READY,
};
struct bpf_lru_list {
@@ -74,6 +81,12 @@ struct bpf_lru {
unsigned int target_free;
unsigned int nr_scans;
bool percpu;
+ struct llist_head rcu_pending;
+ struct llist_head rcu_waiting;
+ struct irq_work rcu_work;
+ struct rcu_head rcu;
+ atomic_t rcu_in_progress;
+ wait_queue_head_t rcu_wait;
};
static inline void bpf_lru_node_set_ref(struct bpf_lru_node *node)
@@ -87,7 +100,9 @@ int bpf_lru_init(struct bpf_lru *lru, bool percpu, u32 hash_offset,
void bpf_lru_populate(struct bpf_lru *lru, void *buf, u32 node_offset,
u32 elem_size, u32 nr_elems);
void bpf_lru_destroy(struct bpf_lru *lru);
+bool bpf_lru_wait_for_rcu(struct bpf_lru *lru);
struct bpf_lru_node *bpf_lru_pop_free(struct bpf_lru *lru, u32 hash);
void bpf_lru_push_free(struct bpf_lru *lru, struct bpf_lru_node *node);
+void bpf_lru_defer_free(struct bpf_lru *lru, struct bpf_lru_node *node);
#endif
diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index a2d0bfd4c..d46f962d9 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -207,6 +207,18 @@ static bool htab_is_lru(const struct bpf_htab *htab)
return htab_map_is_lru(&htab->map);
}
+bool bpf_lru_hash_wait_for_rcu(struct bpf_map *map)
+{
+ struct bpf_htab *htab;
+
+ if (map->map_type != BPF_MAP_TYPE_LRU_HASH &&
+ map->map_type != BPF_MAP_TYPE_LRU_PERCPU_HASH)
+ return false;
+
+ htab = container_of(map, struct bpf_htab, map);
+ return bpf_lru_wait_for_rcu(&htab->lru);
+}
+
static bool htab_is_percpu(const struct bpf_htab *htab)
{
return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
@@ -484,12 +496,13 @@ static int prealloc_init(struct bpf_htab *htab)
static void prealloc_destroy(struct bpf_htab *htab)
{
- htab_free_elems(htab);
-
- if (htab_is_lru(htab))
+ if (htab_is_lru(htab)) {
bpf_lru_destroy(&htab->lru);
- else
+ htab_free_elems(htab);
+ } else {
+ htab_free_elems(htab);
pcpu_freelist_destroy(&htab->freelist);
+ }
}
static int alloc_extra_elems(struct bpf_htab *htab)
@@ -1505,11 +1518,15 @@ static long htab_map_update_elem(struct bpf_map *map, void *key, void *value,
return ret;
}
-static void htab_lru_push_free(struct bpf_htab *htab, struct htab_elem *elem)
+static void htab_lru_free_elem(struct bpf_htab *htab, struct htab_elem *elem,
+ bool defer)
{
check_and_cancel_fields(htab, elem);
bpf_map_dec_elem_count(&htab->map);
- bpf_lru_push_free(&htab->lru, htab_elem_lru_node(elem));
+ if (defer)
+ bpf_lru_defer_free(&htab->lru, htab_elem_lru_node(elem));
+ else
+ bpf_lru_push_free(&htab->lru, htab_elem_lru_node(elem));
}
static long htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
@@ -1572,9 +1589,9 @@ static long htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value
err_lock_bucket:
if (ret)
- htab_lru_push_free(htab, &l_new->elem);
+ htab_lru_free_elem(htab, &l_new->elem, false);
else if (l_old)
- htab_lru_push_free(htab, l_old);
+ htab_lru_free_elem(htab, l_old, true);
return ret;
}
@@ -1803,7 +1820,7 @@ static long htab_lru_map_delete_elem(struct bpf_map *map, void *key)
htab_unlock_bucket(b, flags);
if (l)
- htab_lru_push_free(htab, l);
+ htab_lru_free_elem(htab, l, true);
return ret;
}
@@ -1967,7 +1984,7 @@ static int __htab_map_lookup_and_delete_elem(struct bpf_map *map, void *key,
if (l) {
if (is_lru_map)
- htab_lru_push_free(htab, l);
+ htab_lru_free_elem(htab, l, true);
else
free_htab_elem(htab, l);
}
@@ -2181,10 +2198,10 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *map,
if (do_delete) {
hlist_nulls_del_rcu(&l->hash_node);
- /* bpf_lru_push_free() will acquire lru_lock, which
- * may cause deadlock. See comments in function
- * prealloc_lru_pop(). Let us do bpf_lru_push_free()
- * after releasing the bucket lock.
+ /* Keep LRU element cleanup outside the bucket lock.
+ * The immediate-free path takes lru_lock, and special-field
+ * cleanup may synchronize with other contexts. See the lock
+ * ordering documented above prealloc_lru_pop().
*
* For htab of maps, htab_put_fd_value() in
* free_htab_elem() may acquire a spinlock with bucket
@@ -2205,7 +2222,7 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *map,
l = node_to_free;
node_to_free = node_to_free->batch_flink;
if (is_lru_map)
- htab_lru_push_free(htab, l);
+ htab_lru_free_elem(htab, l, true);
else
free_htab_elem(htab, l);
}
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index ec8e78792..4ef40b333 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -267,7 +267,7 @@ static int bpf_map_update_value(struct bpf_map *map, struct file *map_file,
return bpf_fd_array_map_update_elem(map, map_file, key, value,
flags);
}
-
+retry_update:
bpf_disable_instrumentation();
if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
@@ -303,6 +303,14 @@ static int bpf_map_update_value(struct bpf_map *map, struct file *map_file,
}
bpf_enable_instrumentation();
+ if (err == -ENOMEM &&
+ (map->map_type == BPF_MAP_TYPE_LRU_HASH ||
+ map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) &&
+ bpf_lru_hash_wait_for_rcu(map)) {
+ cond_resched();
+ goto retry_update;
+ }
+
return err;
}
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] bpf: defer reuse of LRU hash elements until after an RCU grace period
2026-11-28 13:39 [PATCH] bpf: defer reuse of LRU hash elements until after an RCU grace period Zheng Shuhao
@ 2026-11-28 14:24 ` sashiko-bot
2026-11-28 17:32 ` Alexei Starovoitov
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-28 14:24 UTC (permalink / raw)
To: Zheng Shuhao, bpf
Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [High] bpf: deferred RCU eviction in bpf_lru_pop_free() breaks BPF
helper updates
- [High] bpf: deferred free on pending_list in bpf_common_lru_pop_free()
over-evicts
- [High] bpf: race in bpf_lru_rcu_work() and bpf_lru_rcu_done() strands
rcu_pending
- [High] bpf: uninterruptible wait_event() starvation in
bpf_lru_wait_for_rcu()
- [Medium] bpf: TOCTOU race in bpf_lru_wait_for_rcu() causes spurious
-ENOMEM
- [Medium] bpf: unbounded O(N) scans in __bpf_lru_list_reclaim_ready()
under spinlock
- [Medium] bpf: unconditional rcu_barrier() on every LRU map teardown in
bpf_lru_destroy()
- [Medium] bpf: element size growth and struct bpf_lru cacheline false
sharing
- [Low] bpf: multi-line comment style violation in
__htab_map_lookup_and_delete_batch()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Critical] bpf: leak of BTF special fields in free_htab_elem() [1]
--
commit 6f012b4323f692e697fe8368116995d6c2073ba1
Author: Zheng Shuhao <viennaoffh@gmail.com>
bpf: defer reuse of LRU hash elements until after an RCU grace period
This commit defers returning removed LRU hash map elements to the free pool
until after an RCU grace period, and adds a wait-and-retry path for syscall
map updates that encounter -ENOMEM while deferred elements are pending.
Link: https://syzkaller.appspot.com/bug?extid=542d2bc50fc9545a27d6
> diff --git a/kernel/bpf/bpf_lru_list.c b/kernel/bpf/bpf_lru_list.c
> index 3858886b0232f..b8a9a39f40952 100644
> --- a/kernel/bpf/bpf_lru_list.c
> +++ b/kernel/bpf/bpf_lru_list.c
> @@ -2,6 +2,7 @@
> /* Copyright (c) 2016 Facebook
> */
> #include <linux/cpumask.h>
> +#include <linux/rcupdate.h>
> #include <linux/spinlock.h>
> #include <linux/percpu.h>
>
> @@ -26,6 +27,77 @@ static void bpf_lru_node_clear_ref(struct bpf_lru_node *node)
> WRITE_ONCE(node->ref, 0);
> }
>
> +static u8 bpf_lru_node_pending_free(const struct bpf_lru_node *node)
> +{
> + /* Pair with the release store after the RCU grace period. */
> + return smp_load_acquire(&node->pending_free);
> +}
> +
> +static void bpf_lru_node_set_free_ready(struct bpf_lru_node *node)
> +{
> + /* Publish readiness only after the RCU grace period has elapsed. */
> + smp_store_release(&node->pending_free, BPF_LRU_FREE_READY);
> +}
> +
> +static void bpf_lru_rcu_done(struct rcu_head *rcu)
> +{
> + struct bpf_lru *lru = container_of(rcu, struct bpf_lru, rcu);
> + struct llist_node *node, *next;
> +
> + node = llist_del_all(&lru->rcu_waiting);
> + while (node) {
> + struct bpf_lru_node *lru_node;
> +
> + next = node->next;
> + lru_node = container_of(node, struct bpf_lru_node, rcu_node);
> + bpf_lru_node_set_free_ready(lru_node);
> + node = next;
> + }
> +
> + atomic_set(&lru->rcu_in_progress, 0);
> + wake_up_all(&lru->rcu_wait);
> + if (!llist_empty(&lru->rcu_pending))
> + irq_work_queue(&lru->rcu_work);
> +}
> +
> +static void bpf_lru_rcu_work(struct irq_work *work)
> +{
> + struct bpf_lru *lru = container_of(work, struct bpf_lru, rcu_work);
> + struct llist_node *node, *next;
> +
> + if (atomic_xchg(&lru->rcu_in_progress, 1))
> + return;
> +
> + node = llist_del_all(&lru->rcu_pending);
> + if (!node) {
> + atomic_set(&lru->rcu_in_progress, 0);
> + wake_up_all(&lru->rcu_wait);
> + return;
> + }
[Severity: High]
Can a concurrent bpf_lru_node_defer_free() race with the !node exit path in
bpf_lru_rcu_work() and leave nodes stranded on lru->rcu_pending?
CPU0 CPU1
bpf_lru_rcu_work()
atomic_xchg(&lru->rcu_in_progress, 1) -> 0
llist_del_all(&lru->rcu_pending) -> NULL
bpf_lru_node_defer_free()
llist_add(..., &lru->rcu_pending)
irq_work_queue(&lru->rcu_work)
<irq_work>
bpf_lru_rcu_work()
atomic_xchg(..., 1) -> 1
return;
atomic_set(&lru->rcu_in_progress, 0)
return; <-- rcu_pending non-empty, no work queued
The same race also exists in bpf_lru_rcu_done() because atomic_set() is not
ordered by a full memory barrier before llist_empty(&lru->rcu_pending).
Once a node is stranded on lru->rcu_pending with rcu_in_progress == 0,
callers of bpf_lru_wait_for_rcu() sleep forever in wait_event() and
bpf_lru_destroy() spins indefinitely on map teardown.
[ ... ]
> @@ -107,6 +179,50 @@ static void __bpf_lru_node_move(struct bpf_lru_list *l,
> list_move(&node->list, &l->lists[tgt_type]);
> }
>
> +static unsigned int
> +__bpf_lru_list_reclaim_ready(struct bpf_lru_list *l,
> + unsigned int tgt_nshrink,
> + struct list_head *free_list,
> + enum bpf_lru_list_type tgt_free_type,
> + bool *has_pending)
> +{
> + struct bpf_lru_node *node, *tmp_node;
> + unsigned int nshrinked = 0;
> + int type;
> +
> + *has_pending = false;
> + for (type = BPF_LRU_LIST_T_ACTIVE; type <= BPF_LRU_LIST_T_INACTIVE;
> + type++) {
> + list_for_each_entry_safe_reverse(node, tmp_node,
> + &l->lists[type], list) {
> + u8 pending_free = bpf_lru_node_pending_free(node);
> +
> + if (pending_free != BPF_LRU_FREE_READY)
> + continue;
> +
> + __bpf_lru_node_move_to_free(l, node, free_list,
> + tgt_free_type);
> + if (++nshrinked == tgt_nshrink)
> + return nshrinked;
> + }
> + }
> +
> + if (!nshrinked) {
> + for (type = BPF_LRU_LIST_T_ACTIVE;
> + type <= BPF_LRU_LIST_T_INACTIVE; type++) {
> + list_for_each_entry(node, &l->lists[type], list) {
> + if (bpf_lru_node_pending_free(node) ==
> + BPF_LRU_FREE_PENDING) {
> + *has_pending = true;
> + return 0;
> + }
> + }
> + }
> + }
> +
> + return nshrinked;
> +}
[Severity: Medium]
Does __bpf_lru_list_reclaim_ready() walk the entire active and inactive
lists up to twice while holding l->lock with interrupts disabled?
Unlike __bpf_lru_list_rotate_active(), __bpf_lru_list_rotate_inactive(),
and __bpf_lru_list_shrink_inactive(), which bound their scans to
lru->nr_scans, this function traverses up to 2 * max_entries nodes under
raw spinlocks. In addition, returning early in __bpf_lru_list_shrink()
whenever has_pending is true or when nshrinked is non-zero but less than
tgt_nshrink prevents __bpf_lru_list_shrink_inactive() from evicting
inactive entries or refilling a full target_free batch as soon as a single
node is pending or ready.
[ ... ]
> @@ -201,15 +317,21 @@ __bpf_lru_list_shrink_inactive(struct bpf_lru *lru,
> unsigned int i = 0;
>
> list_for_each_entry_safe_reverse(node, tmp_node, inactive, list) {
> - if (bpf_lru_node_is_ref(node) &&
> - !READ_ONCE(node->pending_free)) {
> - __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_ACTIVE);
> - } else if (READ_ONCE(node->pending_free) ||
> - lru->del_from_htab(lru->del_arg, node)) {
> + u8 pending_free = bpf_lru_node_pending_free(node);
> +
> + if (pending_free == BPF_LRU_FREE_PENDING) {
> + return nshrinked + 1;
> + } else if (pending_free == BPF_LRU_FREE_READY) {
> __bpf_lru_node_move_to_free(l, node, free_list,
> tgt_free_type);
> if (++nshrinked == tgt_nshrink)
> break;
> + } else if (bpf_lru_node_is_ref(node)) {
> + __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_ACTIVE);
> + } else if (lru->del_from_htab(lru->del_arg, node)) {
> + bpf_lru_node_defer_free(lru, node);
> + if (++nshrinked == tgt_nshrink)
> + break;
> }
>
> if (++i == lru->nr_scans)
> @@ -250,6 +372,12 @@ static unsigned int __bpf_lru_list_shrink(struct bpf_lru *lru,
> struct bpf_lru_node *node, *tmp_node;
> struct list_head *force_shrink_list;
> unsigned int nshrinked;
> + bool has_pending;
> +
> + nshrinked = __bpf_lru_list_reclaim_ready(l, tgt_nshrink, free_list,
> + tgt_free_type, &has_pending);
> + if (nshrinked || has_pending)
> + return nshrinked ? nshrinked : 1;
>
> nshrinked = __bpf_lru_list_shrink_inactive(lru, l, tgt_nshrink,
> free_list, tgt_free_type);
> @@ -264,11 +392,17 @@ static unsigned int __bpf_lru_list_shrink(struct bpf_lru *lru,
>
> list_for_each_entry_safe_reverse(node, tmp_node, force_shrink_list,
> list) {
> - if (READ_ONCE(node->pending_free) ||
> - lru->del_from_htab(lru->del_arg, node)) {
> + u8 pending_free = bpf_lru_node_pending_free(node);
> +
> + if (pending_free == BPF_LRU_FREE_PENDING) {
> + return 1;
> + } else if (pending_free == BPF_LRU_FREE_READY) {
> __bpf_lru_node_move_to_free(l, node, free_list,
> tgt_free_type);
> return 1;
> + } else if (lru->del_from_htab(lru->del_arg, node)) {
> + bpf_lru_node_defer_free(lru, node);
> + return 1;
> }
> }
[Severity: High]
Does deferring newly evicted nodes in __bpf_lru_list_shrink_inactive(),
__bpf_lru_list_shrink(), and __local_list_pop_pending() without adding any
node to free_list cause bpf_map_update_elem() called from BPF programs to
fail with -ENOMEM once the map is full?
When an LRU map has no pre-existing BPF_LRU_FREE_READY nodes, eviction only
queues deleted entries to lru->rcu_pending and leaves free_list empty, so
prealloc_lru_pop() returns NULL. Because non-sleepable BPF programs run
inside an RCU read-side critical section and do not use the syscall retry
loop in bpf_map_update_value(), the grace period for the evicted entries
cannot finish during program execution, causing BPF helper updates to evict
live entries and fail with -ENOMEM.
> @@ -283,7 +417,9 @@ static void __local_list_flush(struct bpf_lru_list *l,
>
> list_for_each_entry_safe_reverse(node, tmp_node,
> &loc_l->pending_list, list) {
> - if (READ_ONCE(node->pending_free))
> + if (bpf_lru_node_pending_free(node) == BPF_LRU_FREE_PENDING)
> + continue;
> + else if (bpf_lru_node_pending_free(node) == BPF_LRU_FREE_READY)
> __bpf_lru_node_move_in(l, node, BPF_LRU_LIST_T_FREE);
> else if (bpf_lru_node_is_ref(node))
> __bpf_lru_node_move_in(l, node, BPF_LRU_LIST_T_ACTIVE);
[ ... ]
> @@ -381,12 +504,20 @@ __local_list_pop_pending(struct bpf_lru *lru, struct bpf_lru_locallist *loc_l)
> ignore_ref:
> /* Get from the tail (i.e. older element) of the pending list. */
> list_for_each_entry_reverse(node, &loc_l->pending_list, list) {
> + if (bpf_lru_node_pending_free(node) == BPF_LRU_FREE_PENDING)
> + continue;
> +
> if ((!bpf_lru_node_is_ref(node) || force) &&
> - (READ_ONCE(node->pending_free) ||
> - lru->del_from_htab(lru->del_arg, node))) {
> + bpf_lru_node_pending_free(node) == BPF_LRU_FREE_READY) {
> list_del(&node->list);
> return node;
> }
> +
> + if ((!bpf_lru_node_is_ref(node) || force) &&
> + lru->del_from_htab(lru->del_arg, node)) {
> + bpf_lru_node_defer_free(lru, node);
> + return NULL;
> + }
> }
[Severity: High]
When __bpf_lru_list_shrink() defers nodes on the global list and leaves
tmp_free empty, doesn't bpf_common_lru_pop_free() fall through into its
cross-CPU steal loop and evict one live pending entry from every CPU?
kernel/bpf/bpf_lru_list.c:bpf_common_lru_pop_free() {
...
do {
steal_loc_l = per_cpu_ptr(clru->local_list, steal);
if (!raw_res_spin_lock_irqsave(&steal_loc_l->lock, flags)) {
node = __local_list_pop_free(steal_loc_l);
if (!node)
node = __local_list_pop_pending(lru, steal_loc_l);
raw_res_spin_unlock_irqrestore(&steal_loc_l->lock, flags);
}
steal = cpumask_next_wrap(steal, cpu_possible_mask);
} while (!node && steal != first_steal);
...
}
Because __local_list_pop_pending() returns NULL after unlinking a live node
via lru->del_from_htab(), the do-while loop does not stop after one
eviction and instead sweeps all possible CPUs. In addition,
__local_list_flush() leaves BPF_LRU_FREE_PENDING nodes on
loc_l->pending_list where __bpf_lru_list_reclaim_ready() never scans them,
and __local_list_pop_pending() skips BPF_LRU_FREE_READY nodes that still
have node->ref set on the !force pass while evicting older unreferenced
live nodes ahead of newer ready nodes.
[ ... ]
> @@ -585,6 +713,25 @@ void bpf_lru_push_free(struct bpf_lru *lru, struct bpf_lru_node *node)
> bpf_common_lru_push_free(lru, node);
> }
>
> +void bpf_lru_defer_free(struct bpf_lru *lru, struct bpf_lru_node *node)
> +{
> + bpf_lru_node_defer_free(lru, node);
> +}
> +
> +bool bpf_lru_wait_for_rcu(struct bpf_lru *lru)
> +{
> + if (!atomic_read(&lru->rcu_in_progress) &&
> + llist_empty(&lru->rcu_pending) &&
> + llist_empty(&lru->rcu_waiting))
> + return false;
> +
> + wait_event(lru->rcu_wait,
> + !atomic_read(&lru->rcu_in_progress) &&
> + llist_empty(&lru->rcu_pending) &&
> + llist_empty(&lru->rcu_waiting));
> + return true;
> +}
[Severity: High]
Can wait_event() in bpf_lru_wait_for_rcu() starve a syscall thread in
TASK_UNINTERRUPTIBLE sleep under steady concurrent map updates or deletes?
If new nodes are queued onto lru->rcu_pending while an RCU grace period is
in flight, llist_empty(&lru->rcu_pending) is already false when
bpf_lru_rcu_done() sets rcu_in_progress to 0 and calls wake_up_all(), and
bpf_lru_rcu_work() sets rcu_in_progress back to 1 before draining
rcu_pending into rcu_waiting. As a result, the three conditions are never
simultaneously true across back-to-back grace periods even though the
batch the caller needed has already become BPF_LRU_FREE_READY.
[ ... ]
> @@ -720,6 +873,14 @@ int bpf_lru_init(struct bpf_lru *lru, bool percpu, u32 hash_offset,
>
> void bpf_lru_destroy(struct bpf_lru *lru)
> {
> + do {
> + irq_work_sync(&lru->rcu_work);
> + rcu_barrier();
> + irq_work_sync(&lru->rcu_work);
> + } while (atomic_read(&lru->rcu_in_progress) ||
> + !llist_empty(&lru->rcu_pending) ||
> + !llist_empty(&lru->rcu_waiting));
> +
> if (lru->percpu)
> free_percpu(lru->percpu_lru);
> else
[Severity: Medium]
Does this do-while loop force a system-wide rcu_barrier() on every LRU map
destruction even when no RCU callback is pending?
Checking atomic_read(&lru->rcu_in_progress), llist_empty(&lru->rcu_pending),
and llist_empty(&lru->rcu_waiting) after an initial irq_work_sync() in a
while loop would avoid stalling map teardown and bpf_map_free_deferred
workers when no grace period is in flight.
> diff --git a/kernel/bpf/bpf_lru_list.h b/kernel/bpf/bpf_lru_list.h
> index 8d0ee61622af0..a0f36a8c5b270 100644
> --- a/kernel/bpf/bpf_lru_list.h
> +++ b/kernel/bpf/bpf_lru_list.h
> @@ -33,11 +36,15 @@ struct bpf_lru_node {
> u16 cpu;
> u8 type;
> u8 ref;
> - /*
> - * Marks nodes whose *_push_free() lock acquire failed; reclaimed
> - * by flush/shrink which honor the flag instead of del_from_htab().
> - */
> + /* Tracks whether this node is waiting for, or ready after, an RCU grace period. */
> u8 pending_free;
> + struct llist_node rcu_node;
> +};
> +
> +enum bpf_lru_free_state {
> + BPF_LRU_FREE_NONE,
> + BPF_LRU_FREE_PENDING,
> + BPF_LRU_FREE_READY,
> };
>
> struct bpf_lru_list {
> @@ -74,6 +81,12 @@ struct bpf_lru {
> unsigned int target_free;
> unsigned int nr_scans;
> bool percpu;
> + struct llist_head rcu_pending;
> + struct llist_head rcu_waiting;
> + struct irq_work rcu_work;
> + struct rcu_head rcu;
> + atomic_t rcu_in_progress;
> + wait_queue_head_t rcu_wait;
> };
[Severity: Medium]
Does adding rcu_node to struct bpf_lru_node and placing rcu_pending right
after percpu in struct bpf_lru introduce cacheline false sharing and push
64-byte LRU elements across two cachelines?
Growing struct bpf_lru_node from 24 to 32 bytes increases
offsetof(struct htab_elem_lru, elem) from 32 to 40 bytes, so every
preallocated element with an 8-byte key and 8-byte value grows from 64 to
72 bytes. In struct bpf_lru, rcu_pending, rcu_waiting, and rcu_work also
land in the same 64-byte cacheline as the read-mostly configuration fields
(del_from_htab, del_arg, hash_offset, target_free, nr_scans, and percpu),
so atomic updates in bpf_lru_node_defer_free() dirty the cacheline read on
every bpf_lru_pop_free().
> diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
> index b8491105430a9..8c5a42348df91 100644
> --- a/kernel/bpf/hashtab.c
> +++ b/kernel/bpf/hashtab.c
[ ... ]
> @@ -1505,11 +1518,15 @@ static long htab_map_update_elem(struct bpf_map *map, void *key, void *value,
> return ret;
> }
>
> -static void htab_lru_push_free(struct bpf_htab *htab, struct htab_elem *elem)
> +static void htab_lru_free_elem(struct bpf_htab *htab, struct htab_elem *elem,
> + bool defer)
> {
> check_and_cancel_fields(htab, elem);
> bpf_map_dec_elem_count(&htab->map);
> - bpf_lru_push_free(&htab->lru, htab_elem_lru_node(elem));
> + if (defer)
> + bpf_lru_defer_free(&htab->lru, htab_elem_lru_node(elem));
> + else
> + bpf_lru_push_free(&htab->lru, htab_elem_lru_node(elem));
> }
[Severity: Critical]
This problem wasn't introduced by this patch, but when preallocated elements
are recycled in htab_lru_free_elem() and free_htab_elem() [1], does
calling only check_and_cancel_fields() without bpf_obj_free_fields() leak
BTF special fields such as kptrs and list heads?
check_and_cancel_fields() only cancels timers, workqueues, and task_work,
and copy_map_value() skips overwriting special fields when the element is
reallocated from the freelist, allowing the recycled element to inherit
stale references that can lead to a use-after-free or double free.
[ ... ]
> @@ -2181,10 +2198,10 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *map,
> if (do_delete) {
> hlist_nulls_del_rcu(&l->hash_node);
>
> - /* bpf_lru_push_free() will acquire lru_lock, which
> - * may cause deadlock. See comments in function
> - * prealloc_lru_pop(). Let us do bpf_lru_push_free()
> - * after releasing the bucket lock.
> + /* Keep LRU element cleanup outside the bucket lock.
> + * The immediate-free path takes lru_lock, and special-field
> + * cleanup may synchronize with other contexts. See the lock
> + * ordering documented above prealloc_lru_pop().
[Severity: Low]
This isn't a bug, but BPF multi-line comment style puts the opening /* on
its own line with the comment text starting on the next line.
> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index 198438b958495..16216060bb102 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -303,6 +303,14 @@ static int bpf_map_update_value(struct bpf_map *map, struct file *map_file,
> }
> bpf_enable_instrumentation();
>
> + if (err == -ENOMEM &&
> + (map->map_type == BPF_MAP_TYPE_LRU_HASH ||
> + map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) &&
> + bpf_lru_hash_wait_for_rcu(map)) {
> + cond_resched();
> + goto retry_update;
> + }
> +
> return err;
> }
[Severity: Medium]
Can bpf_lru_hash_wait_for_rcu() return false and cause
bpf_map_update_value() to fail with a spurious -ENOMEM when the RCU grace
period finishes just before bpf_lru_wait_for_rcu() checks the queues?
If bpf_lru_rcu_done() completes on another CPU after prealloc_lru_pop()
failed to find a ready node (or if a concurrent bpf_lru_node_defer_free()
set node->pending_free to BPF_LRU_FREE_PENDING via cmpxchg() but has not
yet executed llist_add(&node->rcu_node, &lru->rcu_pending)),
rcu_in_progress, rcu_pending, and rcu_waiting are all empty when
bpf_lru_wait_for_rcu() runs, so it returns false without retrying to pop
the newly ready nodes.
[1] https://sashiko.dev/#/bug/linux-57e88db9-923a-496e-bab2-71962f45f071
--
Sashiko AI review · https://sashiko.dev/#/patchset/179587316462.32208.2314387285324230199@gmail.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] bpf: defer reuse of LRU hash elements until after an RCU grace period
2026-11-28 13:39 [PATCH] bpf: defer reuse of LRU hash elements until after an RCU grace period Zheng Shuhao
2026-11-28 14:24 ` sashiko-bot
@ 2026-11-28 17:32 ` Alexei Starovoitov
1 sibling, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2026-11-28 17:32 UTC (permalink / raw)
To: Zheng Shuhao, bpf
Cc: daniel, andrii, eddyz87, memxor, martin.lau, song, yonghong.song,
jolsa, emil, ihor.solodrai, john.fastabend,
syzbot+542d2bc50fc9545a27d6
On Sat, Nov 28, 2026 at 05:39 AM Zheng Shuhao <viennaoffh@gmail.com> wrote:
> while BPF helper updates may still fail temporarily. Is there a simpler
> or more appropriate way to keep an LRU element's key stable until
> existing RCU readers have finished? I would appreciate suggestions on
> how to improve this patch.
No. There is nothing to fix.
All htab maps reuse elements without a grace period.
pw-bot: cr
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-28 17:32 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 13:39 [PATCH] bpf: defer reuse of LRU hash elements until after an RCU grace period Zheng Shuhao
2026-11-28 14:24 ` sashiko-bot
2026-11-28 17: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