From: Usama Arif <usama.arif@linux.dev>
To: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Andrew Morton <akpm@linux-foundation.org>,
justinstitt@google.com, linux-crypto@vger.kernel.org,
linux-kernel@vger.kernel.org, llvm@lists.linux.dev,
morbo@google.com, nathan@kernel.org, ndesaulniers@google.com,
Thomas Graf <tgraf@suug.ch>,
nickolay.lysenko@gmail.com, peterz@infradead.org,
rostedt@goodmis.org, sched-ext@lists.linux.dev, tj@kernel.org,
yatsenko@meta.com
Subject: Re: [PATCH] rhashtable: specialize default comparison for constant parameters
Date: Fri, 25 Sep 2026 10:37:55 +0100 [thread overview]
Message-ID: <840c4c37-99a5-44dd-8b33-67057ee23731@linux.dev> (raw)
In-Reply-To: <arW6ibT1RkVCqTdT@gondor.apana.org.au>
On 25/09/2026 01:04, Herbert Xu wrote:
> On Thu, Sep 24, 2026 at 11:47:33AM -0700, Usama Arif wrote:
>> The inline lookup and insert helpers receive the rhashtable parameters by
>> value. With a static const parameter block, key_offset and key_len are
>> compile-time constants at the call site.
>>
>> The default comparison throws that information away by reading both fields
>> back from ht->p. Fixed-size keys consequently load the parameters and call
>> bcmp() for every object in the bucket, even when the compiler could use
>> scalar comparisons instead.
>>
>> The commit "sched_ext: Specialize the DSQ hashtable compare" [1] resulted
>> in a 2.9x faster DSQ lookup after replacing this path for its u64 key
>> with a scalar comparison. Doing that through obj_cmpfn requires every
>> fixed-key user to provide its own callback.
>>
>> Pass the call-site parameters to rhashtable_compare(), as
>> rht_key_get_hash() already does. Use them when key_len is a compile-time
>> constant. Retain the ht->p path for dynamic parameter blocks. A constant
>> zero key_len continues to take the runtime length from ht->p.key_len.
>>
>> This specializes the default comparison for all fixed-size users. With
>> Clang 22 on x86-64, bcmp() disappears from sched_ext, mac80211, NFSd,
>> TIPC, NFQUEUE, VFS superblock, pidfs, SysV IPC and hardware-breakpoint
>> table paths. The affected build_policy.o, sta_info.o and nfsd filecache.o
>> text shrinks by 512, 400 and 352 bytes respectively. Four- and eight-byte
>> keys become direct scalar comparisons; six-byte MAC addresses become a
>> four-byte and a two-byte comparison.
>>
>> Measure the VFS case with ustat() on a mounted ramfs. This exercises
>> user_get_super() and its super_dev lookup. Across ten interleaved baseline
>> and patched VM boot pairs, with five 3 million call samples per boot, the
>> average per-boot median latency drops from 477.2 to 462.6 ns per call, or
>> 3.0%. All ten pairs improved.
>>
>> No functional change intended.
>>
>> [1] https://lore.kernel.org/all/20260921171928.1639407-1-usama.arif@linux.dev/
>>
>> Suggested-by: Mykola Lysenko <nickolay.lysenko@gmail.com>
>> Signed-off-by: Usama Arif <usama.arif@linux.dev>
>> ---
>> include/linux/rhashtable.h | 20 +++++++++++++++-----
>> lib/rhashtable.c | 5 +++--
>> 2 files changed, 18 insertions(+), 7 deletions(-)
>
> Nice work, I'm glad that someone is looking at the assembly :)
Hi Herbert,
Thanks!
>
>> diff --git a/include/linux/rhashtable.h b/include/linux/rhashtable.h
>> index 57a2a29bef0e8..c9872965b753c 100644
>> --- a/include/linux/rhashtable.h
>> +++ b/include/linux/rhashtable.h
>> @@ -598,13 +598,23 @@ static inline void rht_assign_unlock(struct bucket_table *tbl,
>> for (pos = list; pos && rht_entry(tpos, pos, member); \
>> pos = rcu_dereference_all(pos->next))
>>
>> -static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
>> - const void *obj)
>> +/*
>> + * Use constant params from inlined callers to specialize memcmp(). If params
>> + * isn't constant, it must be equal to ht->p.
>> + */
>> +static __always_inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
>> + const void *obj,
>> + const struct rhashtable_params params)
>> {
>> struct rhashtable *ht = arg->ht;
>> const char *ptr = obj;
>>
>> - return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
>> + if (!__builtin_constant_p(params.key_len))
>> + return memcmp(ptr + ht->p.key_offset, arg->key,
>> + ht->p.key_len);
>> +
>> + return memcmp(ptr + params.key_offset, arg->key,
>> + params.key_len ? : ht->p.key_len);
>
> If there is no obj_cmpfn then there is no obj_hashfn. If there
> is no obj_hashfn then params.key_len must be non-zero. So this
> could become unconditional, perhaps with a comment.
That holds for the params given to rhashtable_init(), but not for the
params passed to the fast-path helpers. The BPF resizable hashmap
passes a constant template with no key_len and no obj_cmpfn
(rhtab_params in kernel/bpf/hashtab.c), and sets key_len only in the
copy it gives to rhashtable_init(). Without the "?: ht->p.key_len",
its lookups would memcmp() zero bytes and return the first object in
the bucket. rht_key_get_hash() has the same fallback for this user,
see c9429bf56405a. (I added Mykyta the author of that patch in CC.)
>
> Thanks,
next prev parent reply other threads:[~2026-09-25 9:38 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-24 18:47 [PATCH] rhashtable: specialize default comparison for constant parameters Usama Arif
2026-09-24 19:21 ` Tejun Heo
2026-09-24 21:14 ` David Laight
2026-09-25 9:32 ` Usama Arif
2026-09-25 0:04 ` Herbert Xu
2026-09-25 9:37 ` Usama Arif [this message]
2026-09-25 23:48 ` Herbert Xu
2026-10-02 10:13 ` Herbert Xu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=840c4c37-99a5-44dd-8b33-67057ee23731@linux.dev \
--to=usama.arif@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=herbert@gondor.apana.org.au \
--cc=justinstitt@google.com \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=nickolay.lysenko@gmail.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=sched-ext@lists.linux.dev \
--cc=tgraf@suug.ch \
--cc=tj@kernel.org \
--cc=yatsenko@meta.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.