[PATCH bpf v2 2/4] rhashtable: Don't retry trylock_irqsoff lookups with IRQs disabled

From: Mani Tofigh via B4 Relay

Date: Sun Nov 22 2026 - 19:25:33 EST


From: Mani Tofigh <mt4022@xxxxxxxxxxxx>

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@xxxxxxxx>
Signed-off-by: Mani Tofigh <mt4022@xxxxxxxxxxxx>
---
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