From: Julian Braha <julianbraha@gmail.com>
To: nathan@kernel.org, nsc@kernel.org
Cc: samitolvanen@google.com, torvalds@linux-foundation.org,
ljs@kernel.org, david.laight.linux@gmail.com, sashal@kernel.org,
lzsx618@gmail.com, zuoan.penguin@gmail.com, petr.pavlu@suse.com,
jani.nikula@intel.com, xur@google.com, sidnayyar@google.com,
robertusdchris@gmail.com, acourbot@nvidia.com,
linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org,
linux-kernel@vger.kernel.org,
Julian Braha <julianbraha@gmail.com>
Subject: [PATCH 4/5] scripts: add bit count parameter back to the host hash table functions
Date: Fri, 27 Nov 2026 00:00:14 +0000 [thread overview]
Message-ID: <20261127000015.3662267-5-julianbraha@gmail.com> (raw)
In-Reply-To: <20261127000015.3662267-1-julianbraha@gmail.com>
This partially reverts commit 0a3128e75108 ("kconfig: add macros useful
for hashtable").
As Linus noted when reporting the 'make oldconfig' performance issue,
when the kernel's internal hash table implementation was copied over for
Kconfig to use, it also improperly simplified the parameters of
DECLARE_HASHTABLE() and DEFINE_HASHTABLE() to take "sizes" instead of the
actual number of bits, and passed '(key) % HASH_SIZE(table)' to
hash_head() instead of using the high hash bits.
Let's revert these simplifications:
1. DECLARE_HASHTABLE() and DEFINE_HASHTABLE() will take the bit count, and
2. hash_head() will call hash_min() to select a bucket using the high bits
Note that HASH_BITS should call ilog2() but that drags in a ton more
dependencies, so instead just use __builtin_ctzll() to count the leading
zeroes since the hash table size can only be a power of 2.
Add back hash_min() from the in-kernel implementation to select hash_32()
for keys of up to 4 bytes and hash_long() for wider keys.
Accordingly, update the callers of DECLARE_HASHTABLE() and
DEFINE_HASHTABLE(). In genksyms, use uint32_t for the CRC key in
__add_symbol(). This gives insertion and lookup the same key size on
64-bit hosts.
Fixes: 0a3128e75108 ("kconfig: add macros useful for hashtable")
Reported-by: Linus Torvalds <torvalds@linux-foundation.org>
Closes: https://lore.kernel.org/all/CAHk-=wi_GuySe0L-uCAM2+nScCLYGBp5BLi6UUPR1gALmNM_3w@mail.gmail.com/
Signed-off-by: Julian Braha <julianbraha@gmail.com>
---
Originally, I copied over log.h to use ilog2() as the in-kernel
HASH_BITS does, but then we need to bring over and adjust a bunch of other
header dependencies like fls.h and fls64.h and then __fls.h and so on.
If anyone hates the trailing zeroes hack, I am open to suggestions.
---
scripts/gendwarfksyms/die.c | 2 +-
scripts/gendwarfksyms/gendwarfksyms.h | 2 +-
scripts/gendwarfksyms/kabi.c | 2 +-
scripts/gendwarfksyms/symbols.c | 4 ++--
scripts/gendwarfksyms/types.c | 2 +-
scripts/genksyms/genksyms.c | 4 ++--
scripts/include/hashtable.h | 24 ++++++++++++++++++------
scripts/kconfig/expr.c | 2 +-
scripts/kconfig/internal.h | 8 ++++----
scripts/kconfig/symbol.c | 2 +-
scripts/kconfig/util.c | 2 +-
scripts/mod/modpost.c | 2 +-
12 files changed, 34 insertions(+), 22 deletions(-)
diff --git a/scripts/gendwarfksyms/die.c b/scripts/gendwarfksyms/die.c
index 79d738e69ce5..32b0be8fca78 100644
--- a/scripts/gendwarfksyms/die.c
+++ b/scripts/gendwarfksyms/die.c
@@ -9,7 +9,7 @@
#define DIE_HASH_BITS 16
/* {die->addr, state} -> struct die * */
-static DEFINE_HASHTABLE(die_map, 1 << DIE_HASH_BITS);
+static DEFINE_HASHTABLE(die_map, DIE_HASH_BITS);
static unsigned int map_hits;
static unsigned int map_misses;
diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/gendwarfksyms.h
index c987acbbf1ef..58d6b20d504e 100644
--- a/scripts/gendwarfksyms/gendwarfksyms.h
+++ b/scripts/gendwarfksyms/gendwarfksyms.h
@@ -208,7 +208,7 @@ void die_map_free(void);
/* A cache for addresses we've already seen. */
struct cache {
- DECLARE_HASHTABLE(cache, 1 << CACHE_HASH_BITS);
+ DECLARE_HASHTABLE(cache, CACHE_HASH_BITS);
};
void cache_set(struct cache *cache, unsigned long key, int value);
diff --git a/scripts/gendwarfksyms/kabi.c b/scripts/gendwarfksyms/kabi.c
index 35c70a6230d9..9589de2aaba4 100644
--- a/scripts/gendwarfksyms/kabi.c
+++ b/scripts/gendwarfksyms/kabi.c
@@ -87,7 +87,7 @@ struct rule {
};
/* { type, target } -> struct rule */
-static DEFINE_HASHTABLE(rules, 1 << RULE_HASH_BITS);
+static DEFINE_HASHTABLE(rules, RULE_HASH_BITS);
static inline unsigned int rule_values_hash(enum kabi_rule_type type,
const char *target)
diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbols.c
index a87444611bbd..2fac17ecac21 100644
--- a/scripts/gendwarfksyms/symbols.c
+++ b/scripts/gendwarfksyms/symbols.c
@@ -9,9 +9,9 @@
#define SYMBOL_HASH_BITS 12
/* struct symbol_addr -> struct symbol */
-static DEFINE_HASHTABLE(symbol_addrs, 1 << SYMBOL_HASH_BITS);
+static DEFINE_HASHTABLE(symbol_addrs, SYMBOL_HASH_BITS);
/* name -> struct symbol */
-static DEFINE_HASHTABLE(symbol_names, 1 << SYMBOL_HASH_BITS);
+static DEFINE_HASHTABLE(symbol_names, SYMBOL_HASH_BITS);
static inline unsigned int symbol_addr_hash(const struct symbol_addr *addr)
{
diff --git a/scripts/gendwarfksyms/types.c b/scripts/gendwarfksyms/types.c
index db55c8ae57f8..864ad6ee5c3e 100644
--- a/scripts/gendwarfksyms/types.c
+++ b/scripts/gendwarfksyms/types.c
@@ -100,7 +100,7 @@ static void type_expansion_append(struct type_expansion *type, const char *s,
* const char *name -> struct type_expansion *
*/
#define TYPE_HASH_BITS 12
-static DEFINE_HASHTABLE(type_map, 1 << TYPE_HASH_BITS);
+static DEFINE_HASHTABLE(type_map, TYPE_HASH_BITS);
static int __type_map_get(const char *name, struct type_expansion **res)
{
diff --git a/scripts/genksyms/genksyms.c b/scripts/genksyms/genksyms.c
index 01bea2d91d7a..fe45a9cb6938 100644
--- a/scripts/genksyms/genksyms.c
+++ b/scripts/genksyms/genksyms.c
@@ -24,7 +24,7 @@
#include "genksyms.h"
/*----------------------------------------------------------------------*/
-static DEFINE_HASHTABLE(symbol_hashtable, 1U << 12);
+static DEFINE_HASHTABLE(symbol_hashtable, 12);
static FILE *debugfile;
int cur_line = 1;
@@ -228,7 +228,7 @@ static struct symbol *__add_symbol(const char *name, enum symbol_type type,
struct string_list *defn, int is_extern,
int is_reference)
{
- unsigned long h;
+ uint32_t h;
struct symbol *sym;
enum symbol_status status = STATUS_UNCHANGED;
diff --git a/scripts/include/hashtable.h b/scripts/include/hashtable.h
index fa60976eae37..069fde323419 100644
--- a/scripts/include/hashtable.h
+++ b/scripts/include/hashtable.h
@@ -3,17 +3,29 @@
#define HASHTABLE_H
#include "array_size.h"
+#include "hash.h"
#include "list.h"
-#define HASH_SIZE(name) (ARRAY_SIZE(name))
+#define DEFINE_HASHTABLE(name, bits) \
+ struct hlist_head name[1 << (bits)] = \
+ { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
+
+#define DECLARE_HASHTABLE(name, bits) \
+ struct hlist_head name[1 << (bits)]
-#define DECLARE_HASHTABLE(name, size) struct hlist_head name[size]
+#define HASH_SIZE(name) (ARRAY_SIZE(name))
+/*
+ * HACK: Since the hash table length is always power-2, just count the
+ * trailing zeroes to avoid pulling in the log header (and dependencies.)
+ */
+#define HASH_BITS(name) __builtin_ctzll(HASH_SIZE(name))
-#define DEFINE_HASHTABLE(name, size) \
- DECLARE_HASHTABLE(name, size) = \
- { [0 ... ((size) - 1)] = HLIST_HEAD_INIT }
+/* Use hash_32 when possible to allow for fast 32bit hashing on 64bit hosts. */
+#define hash_min(val, bits) \
+ (sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
-#define hash_head(table, key) (&(table)[(key) % HASH_SIZE(table)])
+#define hash_head(table, key) \
+ (&(table)[hash_min(key, HASH_BITS(table))])
static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
{
diff --git a/scripts/kconfig/expr.c b/scripts/kconfig/expr.c
index 5938b775a3fc..8dc73acb08ad 100644
--- a/scripts/kconfig/expr.c
+++ b/scripts/kconfig/expr.c
@@ -16,7 +16,7 @@
#define DEBUG_EXPR 0
-DEFINE_HASHTABLE(expr_hashtable, EXPR_HASHSIZE);
+DEFINE_HASHTABLE(expr_hashtable, EXPR_HASH_BITS);
static struct expr *expr_eliminate_yn(struct expr *e);
diff --git a/scripts/kconfig/internal.h b/scripts/kconfig/internal.h
index f7e660dbd299..d40fa33991a6 100644
--- a/scripts/kconfig/internal.h
+++ b/scripts/kconfig/internal.h
@@ -4,16 +4,16 @@
#include <hashtable.h>
-#define SYMBOL_HASHSIZE (1U << 14)
+#define SYMBOL_HASH_BITS 14
-extern DECLARE_HASHTABLE(sym_hashtable, SYMBOL_HASHSIZE);
+extern DECLARE_HASHTABLE(sym_hashtable, SYMBOL_HASH_BITS);
#define for_all_symbols(sym) \
hash_for_each(sym_hashtable, sym, node)
-#define EXPR_HASHSIZE (1U << 14)
+#define EXPR_HASH_BITS 14
-extern DECLARE_HASHTABLE(expr_hashtable, EXPR_HASHSIZE);
+extern DECLARE_HASHTABLE(expr_hashtable, EXPR_HASH_BITS);
void expr_invalidate_all(void);
diff --git a/scripts/kconfig/symbol.c b/scripts/kconfig/symbol.c
index e46228d6348e..b3135d18fe61 100644
--- a/scripts/kconfig/symbol.c
+++ b/scripts/kconfig/symbol.c
@@ -971,7 +971,7 @@ bool sym_is_choice_value(const struct symbol *sym)
return !list_empty(&sym->choice_link);
}
-DEFINE_HASHTABLE(sym_hashtable, SYMBOL_HASHSIZE);
+DEFINE_HASHTABLE(sym_hashtable, SYMBOL_HASH_BITS);
struct symbol *sym_lookup(const char *name, int flags)
{
diff --git a/scripts/kconfig/util.c b/scripts/kconfig/util.c
index d2d17d9d37b9..5c3f6f99c453 100644
--- a/scripts/kconfig/util.c
+++ b/scripts/kconfig/util.c
@@ -14,7 +14,7 @@
#include "lkc.h"
/* hash table of all parsed Kconfig files */
-static DEFINE_HASHTABLE(file_hashtable, 1U << 11);
+static DEFINE_HASHTABLE(file_hashtable, 11);
struct file {
struct hlist_node node;
diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 92bcd2b3a08e..c46dc74d6880 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -234,7 +234,7 @@ struct symbol {
char name[];
};
-static DEFINE_HASHTABLE(symbol_hashtable, 1U << 10);
+static DEFINE_HASHTABLE(symbol_hashtable, 10);
/**
* Allocate a new symbols for use in the hash of exported symbols or
--
2.56.0
next prev parent reply other threads:[~2026-11-27 0:00 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 0:00 [PATCH 0/5] kconfig: fix slow 'make oldconfig' by reverting host header simplifications Julian Braha
2026-11-27 0:00 ` [PATCH 1/5] scripts: add bit count parameter back to host hash functions Julian Braha
2026-11-27 1:16 ` Linus Torvalds
2026-11-27 1:34 ` Linus Torvalds
2026-11-27 0:00 ` [PATCH 2/5] scripts: restore support for 64-bit pointers in " Julian Braha
2026-11-27 0:00 ` [PATCH 3/5] scripts: restore host hash table macro names Julian Braha
2026-11-27 0:00 ` Julian Braha [this message]
2026-11-27 0:00 ` [PATCH 5/5] kconfig: replace XOR with addition for expr_lookup() key generation Julian Braha
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=20261127000015.3662267-5-julianbraha@gmail.com \
--to=julianbraha@gmail.com \
--cc=acourbot@nvidia.com \
--cc=david.laight.linux@gmail.com \
--cc=jani.nikula@intel.com \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-modules@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=lzsx618@gmail.com \
--cc=nathan@kernel.org \
--cc=nsc@kernel.org \
--cc=petr.pavlu@suse.com \
--cc=robertusdchris@gmail.com \
--cc=samitolvanen@google.com \
--cc=sashal@kernel.org \
--cc=sidnayyar@google.com \
--cc=torvalds@linux-foundation.org \
--cc=xur@google.com \
--cc=zuoan.penguin@gmail.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®