* [PATCH bpf v2 1/4] rhashtable: Add trylock_irqsoff for callers that can't spin
2026-11-23 0:25 [PATCH bpf v2 0/4] bpf: Don't spin on rhtab bucket locks with IRQs disabled Mani Tofigh via B4 Relay
@ 2026-11-23 0:25 ` Mani Tofigh via B4 Relay
2026-11-23 0:58 ` bot+bpf-ci
2026-11-23 0:25 ` [PATCH bpf v2 2/4] rhashtable: Don't retry trylock_irqsoff lookups with IRQs disabled Mani Tofigh via B4 Relay
` (2 subsequent siblings)
3 siblings, 1 reply; 7+ messages in thread
From: Mani Tofigh via B4 Relay @ 2026-11-23 0:25 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Mykyta Yatsenko, Herbert Xu, Andrew Morton, Shuah Khan
Cc: Thomas Graf, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, bpf, linux-crypto, linux-kernel,
linux-kselftest, tz2294, Mani Tofigh
From: Mani Tofigh <mt4022@columbia.edu>
A caller with IRQs disabled can run inside a bucket lock's critical
section on its own CPU, e.g., in NMI or in a kprobe on the hash function
that the resize worker calls with the lock held. If it then inserts or
removes a key of that bucket it spins on the bit lock forever.
Add rhashtable_params.trylock_irqsoff. With it, inserts and removes
called with IRQs disabled only try the bucket lock and return -EBUSY if
it's held. A caller with IRQs enabled can't run inside a locked section
of its own CPU, so it still spins, and so does the resize worker. The
flag is a compile time constant in the fast paths like
insecure_elasticity, so other users don't pay for it.
On UP without DEBUG_SPINLOCK the lock bit isn't kept, so these callers
always get -EBUSY there.
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Mani Tofigh <mt4022@columbia.edu>
---
include/linux/rhashtable-types.h | 3 +++
include/linux/rhashtable.h | 34 ++++++++++++++++++++++++++++++++--
lib/rhashtable.c | 3 ++-
3 files changed, 37 insertions(+), 3 deletions(-)
diff --git a/include/linux/rhashtable-types.h b/include/linux/rhashtable-types.h
index 3576b8f08aff..a5c4e696c193 100644
--- a/include/linux/rhashtable-types.h
+++ b/include/linux/rhashtable-types.h
@@ -52,6 +52,8 @@ typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,
* @min_size: Minimum size while shrinking
* @insecure_elasticity: Set to true to disable chain length checks
* @automatic_shrinking: Enable automatic shrinking of tables
+ * @trylock_irqsoff: With irqs disabled insert and remove don't spin on
+ * the bucket lock and return -EBUSY
* @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash)
* @obj_hashfn: Function to hash object
* @obj_cmpfn: Function to compare key with object
@@ -65,6 +67,7 @@ struct rhashtable_params {
u16 min_size;
bool insecure_elasticity;
bool automatic_shrinking;
+ bool trylock_irqsoff;
rht_hashfn_t hashfn;
rht_obj_hashfn_t obj_hashfn;
rht_obj_cmpfn_t obj_cmpfn;
diff --git a/include/linux/rhashtable.h b/include/linux/rhashtable.h
index 70dac9214617..e5f5b9385cc3 100644
--- a/include/linux/rhashtable.h
+++ b/include/linux/rhashtable.h
@@ -339,6 +339,32 @@ static inline unsigned long rht_lock(struct bucket_table *tbl,
return rht_lock_nested(tbl, bkt, 0);
}
+/*
+ * A caller with irqs disabled may be inside this cpu's locked section (NMI,
+ * kprobe, tracepoint), so it only tries the lock.
+ */
+static __always_inline bool rht_lock_or_busy(struct bucket_table *tbl,
+ struct rhash_lock_head __rcu **bkt,
+ const struct rhashtable_params params,
+ unsigned long *flags)
+ __cond_acquires(true, __bitlock(0, bkt))
+{
+ if (!params.trylock_irqsoff || !irqs_disabled()) {
+ *flags = rht_lock(tbl, bkt);
+ return true;
+ }
+ /* bit_spin_trylock() always succeeds on UP without DEBUG_SPINLOCK */
+ if (!IS_ENABLED(CONFIG_SMP) && !IS_ENABLED(CONFIG_DEBUG_SPINLOCK))
+ return false;
+ local_irq_save(*flags);
+ if (!bit_spin_trylock(0, (unsigned long *)bkt)) {
+ local_irq_restore(*flags);
+ return false;
+ }
+ lock_acquire_exclusive(&tbl->dep_map, 0, 1, NULL, _THIS_IP_);
+ return true;
+}
+
static inline void rht_unlock(struct bucket_table *tbl,
struct rhash_lock_head __rcu **bkt,
unsigned long flags)
@@ -785,7 +811,9 @@ static __always_inline void *__rhashtable_insert_fast(
if (!bkt)
goto out;
pprev = NULL;
- flags = rht_lock(tbl, bkt);
+ data = ERR_PTR(-EBUSY);
+ if (!rht_lock_or_busy(tbl, bkt, params, &flags))
+ goto out;
if (unlikely(rcu_access_pointer(tbl->future_tbl))) {
slow_path:
@@ -1066,7 +1094,8 @@ static __always_inline int __rhashtable_remove_fast_one(
if (!bkt)
return -ENOENT;
pprev = NULL;
- flags = rht_lock(tbl, bkt);
+ if (!rht_lock_or_busy(tbl, bkt, params, &flags))
+ return -EBUSY;
rht_for_each_from(he, rht_ptr(bkt, tbl, hash), tbl, hash) {
struct rhlist_head *list;
@@ -1149,6 +1178,7 @@ static __always_inline int __rhashtable_remove_fast(
*/
while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
rhlist)) &&
+ err != -EBUSY &&
(tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
;
diff --git a/lib/rhashtable.c b/lib/rhashtable.c
index 500c45cd1304..2dc13f0e2549 100644
--- a/lib/rhashtable.c
+++ b/lib/rhashtable.c
@@ -650,7 +650,8 @@ static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
} else {
bool inserted;
- flags = rht_lock(tbl, bkt);
+ if (!rht_lock_or_busy(tbl, bkt, ht->p, &flags))
+ return ERR_PTR(-EBUSY);
data = rhashtable_lookup_one(ht, bkt, tbl,
hash, key, obj);
new_tbl = rhashtable_insert_one(ht, bkt, tbl,
--
2.53.0
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH bpf v2 1/4] rhashtable: Add trylock_irqsoff for callers that can't spin
2026-11-23 0:25 ` [PATCH bpf v2 1/4] rhashtable: Add trylock_irqsoff for callers that can't spin Mani Tofigh via B4 Relay
@ 2026-11-23 0:58 ` bot+bpf-ci
0 siblings, 0 replies; 7+ messages in thread
From: bot+bpf-ci @ 2026-11-23 0:58 UTC (permalink / raw)
To: mt4022, ast, daniel, andrii, yatsenko, herbert, akpm, shuah
Cc: tgraf, eddyz87, memxor, martin.lau, song, yonghong.song, jolsa,
emil, ihor.solodrai, bpf, linux-crypto, linux-kernel,
linux-kselftest, tz2294, mt4022, ast, andrii, daniel, martin.lau,
eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 1909 bytes --]
> rhashtable: Add trylock_irqsoff for callers that can't spin
TL;DR: Nits only: documentation gaps. The new -EBUSY return isn't in the
remove kernel-doc, and the member doc omits UP always returning -EBUSY with
IRQs off.
> diff --git a/include/linux/rhashtable-types.h b/include/linux/rhashtable-types.h
> --- a/include/linux/rhashtable-types.h
> +++ b/include/linux/rhashtable-types.h
> @@ -52,6 +52,8 @@ typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,
> * @min_size: Minimum size while shrinking
> * @insecure_elasticity: Set to true to disable chain length checks
> * @automatic_shrinking: Enable automatic shrinking of tables
> + * @trylock_irqsoff: With irqs disabled insert and remove don't spin on
> + * the bucket lock and return -EBUSY
This isn't a bug, but should this member doc also say that on UP without
CONFIG_DEBUG_SPINLOCK these calls always return -EBUSY when IRQs are
disabled? Only the commit message mentions it.
[ ... ]
> diff --git a/include/linux/rhashtable.h b/include/linux/rhashtable.h
> --- a/include/linux/rhashtable.h
> +++ b/include/linux/rhashtable.h
[ ... ]
> @@ -1066,7 +1094,8 @@ static __always_inline int __rhashtable_remove_fast_one(
> if (!bkt)
> return -ENOENT;
> pprev = NULL;
> - flags = rht_lock(tbl, bkt);
> + if (!rht_lock_or_busy(tbl, bkt, params, &flags))
> + return -EBUSY;
This isn't a bug, but rhashtable_remove_fast() and rhltable_remove() can now
return -EBUSY through this path, and their kernel-doc still says:
* Returns zero on success, -ENOENT if the entry could not be found.
Should the Returns lines mention -EBUSY when params.trylock_irqsoff is set?
[ ... ]
---
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/45090312341
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH bpf v2 2/4] rhashtable: Don't retry trylock_irqsoff lookups with IRQs disabled
2026-11-23 0:25 [PATCH bpf v2 0/4] bpf: Don't spin on rhtab bucket locks with IRQs disabled Mani Tofigh via B4 Relay
2026-11-23 0:25 ` [PATCH bpf v2 1/4] rhashtable: Add trylock_irqsoff for callers that can't spin Mani Tofigh via B4 Relay
@ 2026-11-23 0:25 ` Mani Tofigh via B4 Relay
2026-11-23 0:25 ` [PATCH bpf v2 3/4] bpf: Don't spin on rhtab bucket locks " Mani Tofigh via B4 Relay
2026-11-23 0:25 ` [PATCH bpf v2 4/4] selftests/bpf: Test rhtab deletes from inside a resize Mani Tofigh via B4 Relay
3 siblings, 0 replies; 7+ messages in thread
From: Mani Tofigh via B4 Relay @ 2026-11-23 0:25 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Mykyta Yatsenko, Herbert Xu, Andrew Morton, Shuah Khan
Cc: Thomas Graf, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, bpf, linux-crypto, linux-kernel,
linux-kselftest, tz2294, Mani Tofigh
From: Mani Tofigh <mt4022@columbia.edu>
The rehash links the last entry of a chain into the future table and
then unlinks it from the old chain. A lookup that walks the old chain in
between ends in the new chain and retries. A caller with IRQs disabled
can run inside that window on its own CPU, e.g., in NMI or in a kprobe,
and then the rehash never moves on and the retry never ends.
Don't retry for trylock_irqsoff callers with IRQs disabled. The lookup
moves on to the future table, which has the moved entry and all entries
moved before it. The rehash now stores entry->next with
rcu_assign_pointer(), so a lookup that walks into the new chain also
sees those earlier moves.
Suggested-by: Mykyta Yatsenko <yatsenko@meta.com>
Signed-off-by: Mani Tofigh <mt4022@columbia.edu>
---
include/linux/rhashtable.h | 11 ++++++++++-
lib/rhashtable.c | 6 +++++-
2 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/include/linux/rhashtable.h b/include/linux/rhashtable.h
index e5f5b9385cc3..746d62978215 100644
--- a/include/linux/rhashtable.h
+++ b/include/linux/rhashtable.h
@@ -638,6 +638,15 @@ static __always_inline int rhashtable_compare(struct rhashtable_compare_arg *arg
params.key_len ? : ht->p.key_len);
}
+/*
+ * A caller with irqs disabled may have interrupted the rehash on this cpu, so
+ * its retry would never end. It finds the moved entries in the future table.
+ */
+static __always_inline bool rht_lookup_retry(const struct rhashtable_params params)
+{
+ return !params.trylock_irqsoff || !irqs_disabled();
+}
+
/* Internal function, do not use. */
static __always_inline struct rhash_head *__rhashtable_lookup(
struct rhashtable *ht, const void *key,
@@ -670,7 +679,7 @@ static __always_inline struct rhash_head *__rhashtable_lookup(
/* An object might have been moved to a different hash chain,
* while we walk along it - better check and retry.
*/
- } while (he != RHT_NULLS_MARKER(bkt));
+ } while (he != RHT_NULLS_MARKER(bkt) && rht_lookup_retry(params));
/* Ensure we see any new tables. */
smp_rmb();
diff --git a/lib/rhashtable.c b/lib/rhashtable.c
index 2dc13f0e2549..619609037051 100644
--- a/lib/rhashtable.c
+++ b/lib/rhashtable.c
@@ -274,7 +274,11 @@ static int rhashtable_rehash_one(struct rhashtable *ht,
head = rht_ptr(new_tbl->buckets + new_hash, new_tbl, new_hash);
- RCU_INIT_POINTER(entry->next, head);
+ /*
+ * Pairs with smp_rmb() in __rhashtable_lookup() for lookups that
+ * don't retry.
+ */
+ rcu_assign_pointer(entry->next, head);
rht_assign_unlock(new_tbl, &new_tbl->buckets[new_hash], entry, flags);
--
2.53.0
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH bpf v2 3/4] bpf: Don't spin on rhtab bucket locks with IRQs disabled
2026-11-23 0:25 [PATCH bpf v2 0/4] bpf: Don't spin on rhtab bucket locks with IRQs disabled Mani Tofigh via B4 Relay
2026-11-23 0:25 ` [PATCH bpf v2 1/4] rhashtable: Add trylock_irqsoff for callers that can't spin Mani Tofigh via B4 Relay
2026-11-23 0:25 ` [PATCH bpf v2 2/4] rhashtable: Don't retry trylock_irqsoff lookups with IRQs disabled Mani Tofigh via B4 Relay
@ 2026-11-23 0:25 ` Mani Tofigh via B4 Relay
2026-11-23 1:13 ` bot+bpf-ci
2026-11-23 0:25 ` [PATCH bpf v2 4/4] selftests/bpf: Test rhtab deletes from inside a resize Mani Tofigh via B4 Relay
3 siblings, 1 reply; 7+ messages in thread
From: Mani Tofigh via B4 Relay @ 2026-11-23 0:25 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Mykyta Yatsenko, Herbert Xu, Andrew Morton, Shuah Khan
Cc: Thomas Graf, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, bpf, linux-crypto, linux-kernel,
linux-kselftest, tz2294, Mani Tofigh
From: Mani Tofigh <mt4022@columbia.edu>
A BPF program can spin forever on a resizable hash map's bucket lock
that its own CPU already holds. Inserts and deletes take the rhashtable
bucket lock, a bit spinlock that can't detect re-entry.
bpf_disable_instrumentation() keeps kprobe and perf_event programs out
of rhtab's own inserts and deletes, but the resize worker calls the hash
function with that lock held, so a kprobe program on the hash function
that deletes a key of the same bucket never gets the lock:
rht_deferred_worker()
rht_lock(old_tbl, bkt)
rhashtable_jhash2()
kprobe program
bpf_map_delete_elem()
rht_lock(old_tbl, bkt)
/* spins with IRQs disabled */
rcu: INFO: rcu_preempt detected stalls on CPUs/tasks:
...
NMI backtrace for cpu 0
Workqueue: events rht_deferred_worker
RIP: 0010:rhtab_delete_elem+0x15f9/0x1c80
...
Call Trace:
<TASK>
rhtab_map_delete_elem+0x61/0x120
bpf_prog_12e9295082de5b34+0xa0/0xee
trace_call_bpf+0x2ca/0x930
kprobe_perf_func+0x106/0x700
aggr_pre_handler+0x8f/0x110
kprobe_int3_handler+0x335/0x750
do_int3+0xd/0x50
exc_int3+0x165/0x340
asm_exc_int3+0x39/0x40
RIP: 0010:rhashtable_jhash2+0x5/0x360
...
Set trylock_irqsoff in both rhtab params. Only a program with IRQs
disabled can run inside a locked section of its own CPU, so its inserts
and deletes now fail with -EBUSY when the bucket is locked, by this CPU
or another one, and work as before otherwise. This also covers programs
in NMI, so bpf_disable_instrumentation() isn't needed anymore. With the
fix, the delete fails with -EBUSY and the resize finishes.
Fixes: 16b4d3e2fb24 ("bpf: Implement resizable hashmap basic functions")
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Mani Tofigh <mt4022@columbia.edu>
---
kernel/bpf/hashtab.c | 17 ++---------------
1 file changed, 2 insertions(+), 15 deletions(-)
diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index 6e2115477cb9..edc7572a641e 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -2983,6 +2983,7 @@ struct bpf_rhtab {
static const struct rhashtable_params rhtab_params = {
.head_offset = offsetof(struct rhtab_elem, node),
.key_offset = offsetof(struct rhtab_elem, data),
+ .trylock_irqsoff = true,
};
static inline void *rhtab_elem_value(struct rhtab_elem *l, u32 key_size)
@@ -3011,6 +3012,7 @@ static const struct rhashtable_params rhtab_params_long = {
.head_offset = offsetof(struct rhtab_elem, node),
.key_offset = offsetof(struct rhtab_elem, data),
.key_len = sizeof(long),
+ .trylock_irqsoff = true,
.hashfn = rhtab_hashfn_long,
.obj_cmpfn = rhtab_key_cmp_long,
};
@@ -3143,21 +3145,11 @@ static int rhtab_delete_elem(struct bpf_rhtab *rhtab, struct rhtab_elem *elem, v
{
int err;
- /*
- * disable_instrumentation() mitigates the deadlock for programs running in NMI context.
- * rhashtable locks bucket with local_irq_save(). Only NMI programs may reenter
- * rhashtable code, bpf_disable_instrumentation() disables programs running in NMI, except
- * raw tracepoints, which we don't have in rhashtable.
- */
- bpf_disable_instrumentation();
-
if (rhtab->map.key_size == sizeof(long))
err = rhashtable_remove_fast(&rhtab->ht, &elem->node, rhtab_params_long);
else
err = rhashtable_remove_fast(&rhtab->ht, &elem->node, rhtab_params);
- bpf_enable_instrumentation();
-
if (err)
return err;
@@ -3268,16 +3260,11 @@ static long rhtab_map_update_elem(struct bpf_map *map, void *key, void *value, u
memcpy(elem->data, key, map->key_size);
copy_map_value(map, rhtab_elem_value(elem, map->key_size), value);
- /* Prevent deadlock for NMI programs attempting to take bucket lock */
- bpf_disable_instrumentation();
-
if (map->key_size == sizeof(long))
tmp = rhashtable_lookup_get_insert_fast(&rhtab->ht, &elem->node, rhtab_params_long);
else
tmp = rhashtable_lookup_get_insert_fast(&rhtab->ht, &elem->node, rhtab_params);
- bpf_enable_instrumentation();
-
if (tmp) {
bpf_mem_cache_free(&rhtab->ma, elem);
if (IS_ERR(tmp))
--
2.53.0
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH bpf v2 3/4] bpf: Don't spin on rhtab bucket locks with IRQs disabled
2026-11-23 0:25 ` [PATCH bpf v2 3/4] bpf: Don't spin on rhtab bucket locks " Mani Tofigh via B4 Relay
@ 2026-11-23 1:13 ` bot+bpf-ci
0 siblings, 0 replies; 7+ messages in thread
From: bot+bpf-ci @ 2026-11-23 1:13 UTC (permalink / raw)
To: mt4022, ast, daniel, andrii, yatsenko, herbert, akpm, shuah
Cc: tgraf, eddyz87, memxor, martin.lau, song, yonghong.song, jolsa,
emil, ihor.solodrai, bpf, linux-crypto, linux-kernel,
linux-kselftest, tz2294, mt4022, ast, andrii, daniel, martin.lau,
eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 2664 bytes --]
> bpf: Don't spin on rhtab bucket locks with IRQs disabled
TL;DR: On CONFIG_SMP=n kernels without DEBUG_SPINLOCK, setting trylock_irqsoff
on rhtab may make every new-key insert and delete fail with -EBUSY when IRQs
are disabled, even if the bucket is unlocked, contrary to the commit message.
> diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
> --- a/kernel/bpf/hashtab.c
> +++ b/kernel/bpf/hashtab.c
> @@ -2983,6 +2983,7 @@ struct bpf_rhtab {
> static const struct rhashtable_params rhtab_params = {
> .head_offset = offsetof(struct rhtab_elem, node),
> .key_offset = offsetof(struct rhtab_elem, data),
> + .trylock_irqsoff = true,
> };
On CONFIG_SMP=n kernels without CONFIG_DEBUG_SPINLOCK, can this make every
insert of a new key and every delete fail with -EBUSY whenever the caller
has IRQs disabled, even when no CPU holds the bucket lock?
rht_lock_or_busy() does this:
if (!params.trylock_irqsoff || !irqs_disabled()) {
*flags = rht_lock(tbl, bkt);
return true;
}
/* bit_spin_trylock() always succeeds on UP without DEBUG_SPINLOCK */
if (!IS_ENABLED(CONFIG_SMP) && !IS_ENABLED(CONFIG_DEBUG_SPINLOCK))
return false;
The check happens before any test of the bucket, so
__rhashtable_insert_fast() returns ERR_PTR(-EBUSY) and
__rhashtable_remove_fast_one() returns -EBUSY every time.
rhtab_map_update_elem() then frees the new element and returns -EBUSY, and
rhtab_delete_elem() returns -EBUSY. Updates of existing keys still work,
because rhtab_map_update_existing() takes no lock.
Programs running with IRQs disabled (int3-based kprobes, perf_event
programs, tracepoint, fentry and struct_ops programs in IRQs-off regions,
hardirq context) could insert and delete on UP before this patch, since
bit_spin_lock() only disables preemption there and never spins. Most
programs ignore the helper's return value, so the failure may go
unnoticed.
The commit message says these operations "now fail with -EBUSY when the
bucket is locked, by this CPU or another one, and work as before
otherwise". Is that accurate on such kernels?
Only the earlier rhashtable patch mentions the UP behaviour ("On UP
without DEBUG_SPINLOCK the lock bit isn't kept, so these callers always
get -EBUSY there"). Could this patch state that limitation too, or could
the lock bit be kept on UP for trylock_irqsoff tables so that rhtab fails
only when the bucket is actually held?
---
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/45090312341
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH bpf v2 4/4] selftests/bpf: Test rhtab deletes from inside a resize
2026-11-23 0:25 [PATCH bpf v2 0/4] bpf: Don't spin on rhtab bucket locks with IRQs disabled Mani Tofigh via B4 Relay
` (2 preceding siblings ...)
2026-11-23 0:25 ` [PATCH bpf v2 3/4] bpf: Don't spin on rhtab bucket locks " Mani Tofigh via B4 Relay
@ 2026-11-23 0:25 ` Mani Tofigh via B4 Relay
3 siblings, 0 replies; 7+ messages in thread
From: Mani Tofigh via B4 Relay @ 2026-11-23 0:25 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Mykyta Yatsenko, Herbert Xu, Andrew Morton, Shuah Khan
Cc: Thomas Graf, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, bpf, linux-crypto, linux-kernel,
linux-kselftest, tz2294, Mani Tofigh
From: Mani Tofigh <mt4022@columbia.edu>
Add a kprobe program on rhashtable_jhash2() that deletes key 7 of a
resizable hash map whenever the resize worker hashes it there, while
user space keeps the table growing and shrinking. The worker holds key
7's bucket lock at that point, so the delete fails with -EBUSY. Without
the fix the CPU spins on that lock forever.
Signed-off-by: Mani Tofigh <mt4022@columbia.edu>
---
tools/testing/selftests/bpf/prog_tests/rhash.c | 34 ++++++++++++++++++++++++
tools/testing/selftests/bpf/progs/rhash_resize.c | 34 ++++++++++++++++++++++++
2 files changed, 68 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/rhash.c b/tools/testing/selftests/bpf/prog_tests/rhash.c
index 0641bd5b0a9e..5afe07b32289 100644
--- a/tools/testing/selftests/bpf/prog_tests/rhash.c
+++ b/tools/testing/selftests/bpf/prog_tests/rhash.c
@@ -5,6 +5,7 @@
#include <stdio.h>
#include "rhash.skel.h"
#include "bpf_iter_bpf_rhash_map.skel.h"
+#include "rhash_resize.skel.h"
#include <linux/bpf.h>
#include <linux/perf_event.h>
#include <sys/syscall.h>
@@ -149,6 +150,36 @@ static void rhash_iter_test(void)
bpf_iter_bpf_rhash_map__destroy(skel);
}
+static void rhash_resize_test(void)
+{
+ struct rhash_resize *skel;
+ int fd, i, n;
+ __u64 val = 0;
+
+ skel = rhash_resize__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "rhash_resize__open_and_load"))
+ return;
+
+ if (!ASSERT_OK(rhash_resize__attach(skel), "rhash_resize__attach"))
+ goto out;
+
+ fd = bpf_map__fd(skel->maps.rhmap);
+ /* let the resize worker run after each half */
+ for (n = 0; n < 5000 && !skel->bss->busy; n++) {
+ for (i = 0; i < 64; i++)
+ bpf_map_update_elem(fd, &i, &val, BPF_ANY);
+ usleep(100);
+ for (i = 0; i < 64; i++)
+ bpf_map_delete_elem(fd, &i);
+ usleep(100);
+ }
+
+ ASSERT_GT(skel->bss->hits, 0, "hits");
+ ASSERT_GT(skel->bss->busy, 0, "busy");
+out:
+ rhash_resize__destroy(skel);
+}
+
void test_rhash(void)
{
if (test__start_subtest("test_rhash_lookup_update"))
@@ -186,4 +217,7 @@ void test_rhash(void)
if (test__start_subtest("test_rhash_iter"))
rhash_iter_test();
+
+ if (test__start_subtest("test_rhash_resize"))
+ rhash_resize_test();
}
diff --git a/tools/testing/selftests/bpf/progs/rhash_resize.c b/tools/testing/selftests/bpf/progs/rhash_resize.c
new file mode 100644
index 000000000000..5a1076b30bb0
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/rhash_resize.c
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <vmlinux.h>
+#include <errno.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+struct {
+ __uint(type, BPF_MAP_TYPE_RHASH);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __uint(max_entries, 128);
+ __type(key, int);
+ __type(value, long);
+} rhmap SEC(".maps");
+
+int hits;
+int busy;
+
+/* The resize worker calls this with the key's bucket lock held */
+SEC("kprobe/rhashtable_jhash2")
+int BPF_KPROBE(rhash_resize_delete, const void *key, u32 length)
+{
+ int k;
+
+ if (length != 1 || bpf_probe_read_kernel(&k, sizeof(k), key) || k != 7)
+ return 0;
+
+ __sync_fetch_and_add(&hits, 1);
+ if (bpf_map_delete_elem(&rhmap, &k) == -EBUSY)
+ __sync_fetch_and_add(&busy, 1);
+ return 0;
+}
--
2.53.0
^ permalink raw reply [flat|nested] 7+ messages in thread