Re: [PATCH bpf v2 1/4] rhashtable: Add trylock_irqsoff for callers that can't spin
From: Herbert Xu
Date: Thu Dec 03 2026 - 23:48:17 EST
On Sun, Nov 22, 2026 at 07:25:12PM -0500, Mani Tofigh via B4 Relay wrote:
> From: Mani Tofigh <mt4022@xxxxxxxxxxxx>
>
> 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@xxxxxxxxxx>
> Signed-off-by: Mani Tofigh <mt4022@xxxxxxxxxxxx>
> ---
> include/linux/rhashtable-types.h | 3 +++
> include/linux/rhashtable.h | 34 ++++++++++++++++++++++++++++++++--
> lib/rhashtable.c | 3 ++-
> 3 files changed, 37 insertions(+), 3 deletions(-)
I have to say that this looks horrible.
> 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;
Please call it something more obvious like used_in_nmi.
But I have to say that I'd rather draw a line and say that rhashtable
should not be used in NMI handlers and if you guys need that please
design your own data structure.
Thanks,
--
Email: Herbert Xu <herbert@xxxxxxxxxxxxxxxxxxx>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt