kconfig: fix slow 'make oldconfig' by reverting host header simplifications
Subsystems (3)
Julian Braha
reviewercover kconfig: fix slow 'make oldconfig' by reverting host header simplifications
As Linus noted when reporting the 'make oldconfig' performance issue [1], when the kernel's internal hash table implementation was copied over for Kconfig to use, it also improperly simplified these headers.
By passing '(key) % HASH_SIZE(table)' to hash_head(), this caused a poor hash table distribution due to using the clear lower bits with power-2 hash table size, leading to a slowdown in Kconfig's expr_lookup() which makes heavy use of this.
To resolve the performance issue, let's revert the simplifications such that the signatures of the host functions match the original and takes the number of bits. Also, replace XOR in Kconfig's expr_lookup() as suggested by Linus.
Patches 1-3 are pure refactors, restoring code from the original hash.h and hashtable.h; 4 and 5 resolve the performance issue directly:
Patch 1/5 reverts the removal of the bit count parameter from hash_32() and hash_ptr() in scripts/include/hash.h (needed by hashtable.h in 3/5).
Patch 2/5 reverts the removal of the 64-bit path from hash_ptr(), so that potential future callers can safely pass higher bit counts.
Patch 3/5 restores the original names of DEFINE_HASHTABLE() and DECLARE_HASHTABLE() from the in-kernel implementation.
Patch 4/5 reverts the removal of the bit count parameter from DECLARE_HASHTABLE and DEFINE_HASHTABLE in scripts/include/hashtable.h.
Patch 5/5 replaces XOR with addition in expr_lookup().
---
Julian Braha (5):
scripts: add bit count parameter back to host hash functions
scripts: restore full 64-bit support in the host hash functions
scripts: restore host hash table macro names
scripts: add bit count parameter back to the host hash table functions
kconfig: replace XOR with addition for expr_lookup() key generation
scripts/gendwarfksyms/cache.c | 4 ++-- scripts/gendwarfksyms/die.c | 4 ++-- scripts/gendwarfksyms/gendwarfksyms.h | 4 ++-- scripts/gendwarfksyms/kabi.c | 4 ++-- scripts/gendwarfksyms/symbols.c | 6 +++--- scripts/gendwarfksyms/types.c | 2 +- scripts/genksyms/genksyms.c | 4 ++-- scripts/include/hash.h | 20 ++++++++++++++++---- scripts/include/hashtable.h | 24 ++++++++++++++++++------ scripts/kconfig/expr.c | 4 ++-- scripts/kconfig/internal.h | 8 ++++---- scripts/kconfig/symbol.c | 2 +- scripts/kconfig/util.c | 2 +- scripts/mod/modpost.c | 2 +- 14 files changed, 57 insertions(+), 33 deletions(-)
Signature · 1 line
2.56.0
Julian Braha
reviewerPATCH 1/5 scripts: add bit count parameter back to host hash functions
This partially reverts commit f93d6bfbd2f7 ("kconfig: use hash table to reuse expressions").
As reported by Linus [1], when the kernel's internal hash and hash table implementations were copied over for Kconfig to use, several functions were simplified to remove their second 'bits' parameter. But the usage of this simplified implementation led to poor hash table performance due to using the lower bits despite hash_32() putting the significant bits in the high bits of the result.
Let's revert the parameter simplification: the host versions of hash_32() and hash_ptr() will take the number of high bits, as is done in the kernel's internal implementation.
Accordingly, adjust the callers of 'hash_32()' and 'hash_ptr()' to use the additional parameter. This is a pure refactor (no functional changes), so pass 32 to preserve the existing behavior.
--- scripts/gendwarfksyms/cache.c | 4 ++-- scripts/gendwarfksyms/die.c | 2 +- scripts/gendwarfksyms/gendwarfksyms.h | 2 +- scripts/gendwarfksyms/kabi.c | 2 +- scripts/gendwarfksyms/symbols.c | 2 +- scripts/include/hash.h | 8 ++++---- scripts/kconfig/expr.c | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-)
scripts/gendwarfksyms/cache.c +2 -2
@@ -18,14 +18,14 @@ void cache_set(struct cache *cache, unsigned long key, int value) ci =xmalloc (sizeof (*ci)); ci->key = key; ci->value = value; -hash_add (cache->cache, &ci->hash,hash_32 (key)); +hash_add (cache->cache, &ci->hash,hash_32 (key,32 )); }int cache_get (struct cache *cache,unsigned long key) {struct cache_item *ci; -hash_for_each_possible (cache->cache, ci, hash,hash_32 (key)) { +hash_for_each_possible (cache->cache, ci, hash,hash_32 (key,32 )) {if (ci->key == key)return ci->value; }
scripts/gendwarfksyms/die.c +1 -1
@@ -16,7 +16,7 @@ static unsigned int map_misses;static inline unsigned int die_hash (uintptr_t addr,enum die_state state) { -return hash_32 (addr_hash (addr) ^ (unsigned int )state); +return hash_32 (addr_hash (addr) ^ (unsigned int )state,32 ); }static void init_die (struct die *cd)
scripts/gendwarfksyms/gendwarfksyms.h +1 -1
@@ -95,7 +95,7 @@ extern int symtypes;static inline unsigned int addr_hash (uintptr_t addr) { -return hash_ptr ((const void *)addr); +return hash_ptr ((const void *)addr,32 ); }enum symbol_state {
scripts/gendwarfksyms/kabi.c +1 -1
@@ -92,7 +92,7 @@ static HASHTABLE_DEFINE(rules, 1 << RULE_HASH_BITS);static inline unsigned int rule_values_hash (enum kabi_rule_type type,const char *target) { -return hash_32 (type) ^hash_str (target); +return hash_32 (type,32 ) ^hash_str (target); }static inline unsigned int rule_hash (const struct rule *rule)
scripts/gendwarfksyms/symbols.c +1 -1
@@ -15,7 +15,7 @@ static HASHTABLE_DEFINE(symbol_names, 1 << SYMBOL_HASH_BITS);static inline unsigned int symbol_addr_hash (const struct symbol_addr *addr) { -return hash_32 (addr->section ^addr_hash (addr->address)); +return hash_32 (addr->section ^addr_hash (addr->address),32 ); } static unsigned int __for_each_addr(struct symbol *sym, symbol_callback_t func,
scripts/include/hash.h +4 -4
@@ -15,14 +15,14 @@ static inline unsigned int hash_str(const char *s)/* simplified version of functions from include/linux/hash.h */ #define GOLDEN_RATIO_32 0x61C88647 -static inline unsigned int hash_32 (unsigned int val) +static inline unsigned int hash_32 (unsigned int val,unsigned int bits) { -return 0x61C88647 * val; +return bits ? (val *GOLDEN_RATIO_32 ) >> (32 - bits) :0 ; } -static inline unsigned int hash_ptr (const void *ptr) +static inline unsigned int hash_ptr (const void *ptr,unsigned int bits) { -return hash_32 ((unsigned int )(unsigned long )ptr); +return hash_32 ((unsigned int )(unsigned long )ptr, bits); }#endif /* HASH_H */
scripts/kconfig/expr.c +1 -1
@@ -35,7 +35,7 @@ static struct expr *expr_lookup(enum expr_type type, void *l, void *r)struct expr *e;int hash; - hash =hash_32 ((unsigned int )type ^hash_ptr (l) ^hash_ptr (r)); + hash =hash_32 ((unsigned int )type ^hash_ptr (l,32 ) ^hash_ptr (r,32 ),32 );hash_for_each_possible (expr_hashtable, e, node, hash) { if (e->type == type && e->left._initdata == l &&
Signature · 1 line
2.56.0
Re: [PATCH 1/5] scripts: add bit count parameter back to host hash functions
Quoted 4 lines · Julian Braha
Accordingly, adjust the callers of '
hash_32()' and 'hash_ptr()' to use the additional parameter. This is a pure refactor (no functional changes), so pass 32 to preserve the existing behavior.
I think this is the wrong approach.
I'd suggest hiding the size of the hash table in the table itself, sinc ethey are all constant sizes.
You can do it with some typedef tricks - not exactly pretty macros, but the end result ends up pretty nice from a user standpoint.
Something like the appended TOTALLY UNTESTED patch. It builds, but it's literally a quick hack.
What it does is to turn each hashtable into a 'union' type that has the hashtable head array (as 'hlist[]'), and then a special 'hash_bits[]' array.
And the 'hash_bits[]' array isn't actually used as an array, it's literally just used for its size: it's the number of bits the hash table has.
So now you can take such a hashtable type, and always turn it into the number of bits with just
sizeof(hashtable.hash_bits)
so now the hash table itself inherently carries its size in bits with it
- no need for 'ilog2()' or any other crap to calculate the bits.
And at that point, you can now just use the high bits of the hased value, and you don't need to pass in any hash table sizes.
You can actually even pass pointers to these things around, but you have to do it with that original named type, because the size is carried in the *type* itself, and it's not just an anonymous array. Nothing in the tree does that
We can't use this trick in the kernel, because the kernel hash tables aren't all fixed sizes. But for the scripting code, this should make the hashing really syntactically simple because the size is always there.
NOTE NOTE NOTE! This is untested crap. It shows the *concept*. It might even work. But it might also do unspeakable things to your pets. Don't trust my word for it. Look at the code and test it.
Linus
---
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 | 2 +- scripts/include/hashtable.h | 23 ++++++++++++----------- 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, 27 insertions(+), 26 deletions(-)
scripts/gendwarfksyms/die.c +1 -1
@@ -9,7 +9,7 @@#define DIE_HASH_BITS 16/* {die->addr, state} -> struct die * */ -static HASHTABLE_DEFINE(die_map, 1 << DIE_HASH_BITS); +static HASHTABLE_DEFINE(die_map, DIE_HASH_BITS);static unsigned int map_hits;static unsigned int map_misses;
(quoted diff) +0 -0
b/scripts/gendwarfksyms/gendwarfksyms.h
index 32cec8f7695a..ad6ff6b8a5c3 100644
scripts/gendwarfksyms/gendwarfksyms.h +1 -1
@@ -208,7 +208,7 @@ void die_map_free(void);/* A cache for addresses we've already seen. */ struct cache { - HASHTABLE_DECLARE(cache, 1 << CACHE_HASH_BITS); + HASHTABLE_DECLARE(cache, CACHE_HASH_BITS); };void cache_set (struct cache *cache,unsigned long key,int value);
scripts/gendwarfksyms/kabi.c +1 -1
@@ -87,7 +87,7 @@ struct rule { };/* { type, target } -> struct rule */ -static HASHTABLE_DEFINE(rules, 1 << RULE_HASH_BITS); +static HASHTABLE_DEFINE(rules, RULE_HASH_BITS);static inline unsigned int rule_values_hash (enum kabi_rule_type type,const char *target)
scripts/gendwarfksyms/symbols.c +2 -2
@@ -9,9 +9,9 @@#define SYMBOL_HASH_BITS 12/* struct symbol_addr -> struct symbol */ -static HASHTABLE_DEFINE(symbol_addrs, 1 << SYMBOL_HASH_BITS); +static HASHTABLE_DEFINE(symbol_addrs, SYMBOL_HASH_BITS);/* name -> struct symbol */ -static HASHTABLE_DEFINE(symbol_names, 1 << SYMBOL_HASH_BITS); +static HASHTABLE_DEFINE(symbol_names, SYMBOL_HASH_BITS);static inline unsigned int symbol_addr_hash (const struct symbol_addr *addr) {
scripts/gendwarfksyms/types.c +0 -0
@@ -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 HASHTABLE_DEFINE(type_map, 1 << TYPE_HASH_BITS); +static HASHTABLE_DEFINE(type_map, TYPE_HASH_BITS);
static int __type_map_get(const char *name, struct type_expansion **res) {
scripts/genksyms/genksyms.c +1 -1
@@ -24,7 +24,7 @@#include "genksyms.h" /*----------------------------------------------------------------------*/ -static HASHTABLE_DEFINE(symbol_hashtable, 1U << 12); +static HASHTABLE_DEFINE(symbol_hashtable, 12);static FILE *debugfile;int cur_line =1 ;
scripts/include/hashtable.h +7 -2
@@ -5,15 +5,19 @@#include "array_size.h" #include "list.h" -#define HASH_SIZE (name) (ARRAY_SIZE(name)) +#define HASHTABLE_DECLARE (name, bits) \ + union { \ + struct hlist_head hlist[1 << (bits)]; \ + char hash_bits[bits]; \ + } name -#define HASHTABLE_DECLARE (name, size) struct hlist_head name[size] +#define HASHTABLE_DEFINE (name, bits) \ + HASHTABLE_DECLARE(name, bits) = { { [0 ... ((1 << (bits)) - 1)] =
HLIST_HEAD_INIT } }
-#define HASHTABLE_DEFINE(name, size) \
- HASHTABLE_DECLARE(name, size) = \
- { [0 ... ((size) - 1)] = HLIST_HEAD_INIT }
+#define HASH_BITS(name) (sizeof((name).hash_bits)) +#define HASH_SIZE(name) (1ul << HASH_BITS(name))
-#define hash_head(table, key) (&(table)[(key) % HASH_SIZE(table)])
+#define hash_head(table, key) (&(table).hlist[(unsigned)(key) >>
(32-HASH_BITS(table))])
static inline void __hash_init(struct hlist_head *ht, unsigned int sz) {
@@ -26,9 +30,6 @@ static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
/**
* hash_init - initialize a hash table
* @table: hashtable to be initialized
- *
- * This has to be a macro since HASH_SIZE() will not work on pointers since
- * it calculates the size during preprocessing.
*/
#define hash_init(table) __hash_init(table, HASH_SIZE(table))
@@ -58,7 +59,7 @@ static inline void hash_del(struct hlist_node *node)
*/
#define hash_for_each(table, obj, member) \
for (int _bkt = 0; _bkt < HASH_SIZE(table); _bkt++) \
- hlist_for_each_entry(obj, &table[_bkt], member)
+ hlist_for_each_entry(obj, &table[_bkt].hlist, member)
/**
* hash_for_each_safe - iterate over a hashtable safe against removal of
@@ -70,7 +71,7 @@ static inline void hash_del(struct hlist_node *node)
*/
#define hash_for_each_safe(table, obj, tmp, member) \
for (int _bkt = 0; _bkt < HASH_SIZE(table); _bkt++) \
- hlist_for_each_entry_safe(obj, tmp, &table[_bkt], member)
+ hlist_for_each_entry_safe(obj, tmp, &table[_bkt].hlist, member)
/**
* hash_for_each_possible - iterate over all possible objects hashing to the
scripts/kconfig/expr.c +1 -1
@@ -16,7 +16,7 @@#define DEBUG_EXPR 0 -HASHTABLE_DEFINE (expr_hashtable,EXPR_HASHSIZE ); +HASHTABLE_DEFINE (expr_hashtable,EXPR_HASH_BITS );static struct expr *expr_eliminate_yn (struct expr *e);
scripts/kconfig/internal.h +4 -4
@@ -4,16 +4,16 @@#include <hashtable.h> -#define SYMBOL_HASHSIZE (1U << 14) +#define SYMBOL_HASH_BITS (14) -extern HASHTABLE_DECLARE(sym_hashtable, SYMBOL_HASHSIZE); +extern HASHTABLE_DECLARE(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 HASHTABLE_DECLARE(expr_hashtable, EXPR_HASHSIZE); +extern HASHTABLE_DECLARE(expr_hashtable, EXPR_HASH_BITS);void expr_invalidate_all (void );
scripts/kconfig/symbol.c +1 -1
@@ -971,7 +971,7 @@ bool sym_is_choice_value(const struct symbol *sym)return !list_empty (&sym->choice_link); } -HASHTABLE_DEFINE (sym_hashtable,SYMBOL_HASHSIZE ); +HASHTABLE_DEFINE (sym_hashtable,SYMBOL_HASH_BITS );struct symbol *sym_lookup (const char *name,int flags) {
scripts/kconfig/util.c +1 -1
@@ -14,7 +14,7 @@#include "lkc.h" /* hash table of all parsed Kconfig files */ -static HASHTABLE_DEFINE(file_hashtable, 1U << 11); +static HASHTABLE_DEFINE(file_hashtable, 11);struct file {struct hlist_node node;
scripts/mod/modpost.c +1 -1
@@ -234,7 +234,7 @@ struct symbol {char name[]; }; -static HASHTABLE_DEFINE(symbol_hashtable, 1U << 10); +static HASHTABLE_DEFINE(symbol_hashtable, 10);/** * Allocate a new symbols for use in the hash of exported symbols or
Re: [PATCH 1/5] scripts: add bit count parameter back to host hash functions
Quoted 4 lines · Linus Torvalds
NOTE NOTE NOTE! This is untested crap. It shows the *concept*. It might even work. But it might also do unspeakable things to your pets. Don't trust my word for it. Look at the code and test it.
Some more testing shows that it needs at least something like this on top..
With this it *might* actually work. But when I say "some more testing", that "some" is still carrying a lot of weight. Not a lot of, IOW.
I did say it was untested crap. I guess I'm more of a "high-level concepts guy" than detail-oriented.
Users? I like to call them "Test subjects" or "Victims".
Linus
---
scripts/include/hashtable.h +0 -1
@@ -12,10 +12,14 @@ }name #define HASHTABLE_DEFINE(name, bits) \ - HASHTABLE_DECLARE(name, bits) = { { [0 ... ((1 << (bits)) - 1)] =
HLIST_HEAD_INIT } }
+ HASHTABLE_DECLARE(name, bits) = { { [0 ... ((1 << (bits)) - 1)] =HLIST_HEAD_INIT } }
+
+/* Define a table already declared extern with HASHTABLE_DECLARE() */ +#define HASHTABLE_DEFINE_EXTERN(name) \
+ typeof(name) name = { { [0 ... HASH_SIZE(name) - 1] = HLIST_HEAD_INIT } }
#define HASH_BITS(name) (sizeof((name).hash_bits))
-#define HASH_SIZE(name) (1ul << HASH_BITS(name)) +#define HASH_SIZE(name) ARRAY_SIZE((name).hlist)
#define hash_head(table, key) (&(table).hlist[(unsigned)(key) >>
(32-HASH_BITS(table))])
@@ -31,7 +35,7 @@ static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
* hash_init - initialize a hash table
* @table: hashtable to be initialized
*/
-#define hash_init(table) __hash_init(table, HASH_SIZE(table)) +#define hash_init(table) __hash_init((table).hlist, HASH_SIZE(table))
/**
* hash_add - add an object to a hashtable
@@ -59,7 +63,7 @@ static inline void hash_del(struct hlist_node *node)
*/
#define hash_for_each(table, obj, member) \
for (int _bkt = 0; _bkt < HASH_SIZE(table); _bkt++) \
- hlist_for_each_entry(obj, &table[_bkt].hlist, member)
+ hlist_for_each_entry(obj, &(table).hlist[_bkt], member)
/**
* hash_for_each_safe - iterate over a hashtable safe against removal of
@@ -71,7 +75,7 @@ static inline void hash_del(struct hlist_node *node)
*/
#define hash_for_each_safe(table, obj, tmp, member) \
for (int _bkt = 0; _bkt < HASH_SIZE(table); _bkt++) \
- hlist_for_each_entry_safe(obj, tmp, &table[_bkt].hlist, member)
+ hlist_for_each_entry_safe(obj, tmp, &(table).hlist[_bkt], member)
/**
* hash_for_each_possible - iterate over all possible objects hashing to the
scripts/kconfig/expr.c +1 -1
@@ -16,7 +16,7 @@#define DEBUG_EXPR 0 -HASHTABLE_DEFINE (expr_hashtable,EXPR_HASH_BITS ); +HASHTABLE_DEFINE_EXTERN (expr_hashtable);static struct expr *expr_eliminate_yn (struct expr *e);
scripts/kconfig/symbol.c +1 -1
@@ -971,7 +971,7 @@ bool sym_is_choice_value(const struct symbol *sym)return !list_empty (&sym->choice_link); } -HASHTABLE_DEFINE (sym_hashtable,SYMBOL_HASH_BITS ); +HASHTABLE_DEFINE_EXTERN (sym_hashtable);struct symbol *sym_lookup (const char *name,int flags) {
Julian Braha
reviewerPATCH 2/5 scripts: restore support for 64-bit pointers in host hash functions
In reply to Julian BrahaThis partially reverts commit f93d6bfbd2f7 ("kconfig: use hash table to reuse expressions").
When the kernel's hash headers were copied over for host usage, hash_ptr() was also simplified to remove the bit count parameter, with only the "fast 32bit" path in use.
With the bit count parameter restored, let's restore support for passing a 64-bit pointer to hash_ptr(), rather than truncating to 32 bits.
Like other headers in tools/, use __SIZEOF_LONG__ to check the host word size, as opposed to the in-kernel BITS_PER_LONG.
--- scripts/include/hash.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
scripts/include/hash.h +13 -1
@@ -12,17 +12,29 @@ static inline unsigned int hash_str(const char *s) return hash; } +#if __SIZEOF_LONG__ ==4 +#define hash_long (val, bits) hash_32(val, bits) +#else +#define hash_long (val, bits) hash_64(val, bits) +#endif +/* simplified version of functions from include/linux/hash.h */ #define GOLDEN_RATIO_32 0x61C88647 +#define GOLDEN_RATIO_64 0x61C8864680B583EBullstatic inline unsigned int hash_32 (unsigned int val,unsigned int bits) {return bits ? (val *GOLDEN_RATIO_32 ) >> (32 - bits) :0 ; } +static inline unsigned int hash_64 (unsigned long long val,unsigned int bits) +{ +return bits ? (val *GOLDEN_RATIO_64 ) >> (64 - bits) :0 ; +} +static inline unsigned int hash_ptr (const void *ptr,unsigned int bits) { -return hash_32 ((unsigned int )(unsigned long )ptr, bits); +return hash_long ((unsigned long )ptr, bits); }#endif /* HASH_H */
Signature · 1 line
2.56.0
Julian Braha
reviewerPATCH 3/5 scripts: restore host hash table macro names
In reply to Julian BrahaThis partially reverts commit 0a3128e75108 ("kconfig: add macros useful for hashtable").
Rename HASHTABLE_DECLARE() to DECLARE_HASHTABLE() and HASHTABLE_DEFINE() to DEFINE_HASHTABLE() in the host scripts/include/hashtable.h, bringing the signatures closer to the original in-kernel implementation, and update the callers, accordingly.
---
This patch isn't really necessary to fix the issue, but it does make maintenance a tad easier when you're looking at the two implementations side-by-side.
--- 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 | 2 +- scripts/include/hashtable.h | 6 +++--- scripts/kconfig/expr.c | 2 +- scripts/kconfig/internal.h | 4 ++-- scripts/kconfig/symbol.c | 2 +- scripts/kconfig/util.c | 2 +- scripts/mod/modpost.c | 2 +- 12 files changed, 16 insertions(+), 16 deletions(-)
scripts/gendwarfksyms/die.c +1 -1
@@ -9,7 +9,7 @@#define DIE_HASH_BITS 16/* {die->addr, state} -> struct die * */ -static HASHTABLE_DEFINE(die_map, 1 << DIE_HASH_BITS); +static DEFINE_HASHTABLE(die_map, 1 << DIE_HASH_BITS);static unsigned int map_hits;static unsigned int map_misses;
scripts/gendwarfksyms/gendwarfksyms.h +1 -1
@@ -208,7 +208,7 @@ void die_map_free(void);/* A cache for addresses we've already seen. */ struct cache { - HASHTABLE_DECLARE(cache, 1 << CACHE_HASH_BITS); + DECLARE_HASHTABLE(cache, 1 << CACHE_HASH_BITS); };void cache_set (struct cache *cache,unsigned long key,int value);
scripts/gendwarfksyms/kabi.c +1 -1
@@ -87,7 +87,7 @@ struct rule { };/* { type, target } -> struct rule */ -static HASHTABLE_DEFINE(rules, 1 << RULE_HASH_BITS); +static DEFINE_HASHTABLE(rules, 1 << RULE_HASH_BITS);static inline unsigned int rule_values_hash (enum kabi_rule_type type,const char *target)
scripts/gendwarfksyms/symbols.c +2 -2
@@ -9,9 +9,9 @@#define SYMBOL_HASH_BITS 12/* struct symbol_addr -> struct symbol */ -static HASHTABLE_DEFINE(symbol_addrs, 1 << SYMBOL_HASH_BITS); +static DEFINE_HASHTABLE(symbol_addrs, 1 << SYMBOL_HASH_BITS);/* name -> struct symbol */ -static HASHTABLE_DEFINE(symbol_names, 1 << SYMBOL_HASH_BITS); +static DEFINE_HASHTABLE(symbol_names, 1 << SYMBOL_HASH_BITS);static inline unsigned int symbol_addr_hash (const struct symbol_addr *addr) {
scripts/gendwarfksyms/types.c +1 -1
@@ -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 HASHTABLE_DEFINE(type_map, 1 << TYPE_HASH_BITS); +static DEFINE_HASHTABLE(type_map, 1 << TYPE_HASH_BITS);static int __type_map_get (const char *name,struct type_expansion **res) {
scripts/genksyms/genksyms.c +1 -1
@@ -24,7 +24,7 @@#include "genksyms.h" /*----------------------------------------------------------------------*/ -static HASHTABLE_DEFINE(symbol_hashtable, 1U << 12); +static DEFINE_HASHTABLE(symbol_hashtable, 1U << 12);static FILE *debugfile;int cur_line =1 ;
scripts/include/hashtable.h +3 -3
@@ -7,10 +7,10 @@#define HASH_SIZE (name) (ARRAY_SIZE(name)) -#define HASHTABLE_DECLARE (name, size) struct hlist_head name[size] +#define DECLARE_HASHTABLE (name, size) struct hlist_head name[size] -#define HASHTABLE_DEFINE (name, size) \ - HASHTABLE_DECLARE(name, size) = \ +#define DEFINE_HASHTABLE (name, size) \ + DECLARE_HASHTABLE(name, size) = \ { [0 ... ((size) - 1)] = HLIST_HEAD_INIT }#define hash_head (table, key) (&(table)[(key) % HASH_SIZE(table)])
scripts/kconfig/expr.c +1 -1
@@ -16,7 +16,7 @@#define DEBUG_EXPR 0 -HASHTABLE_DEFINE (expr_hashtable,EXPR_HASHSIZE ); +DEFINE_HASHTABLE (expr_hashtable,EXPR_HASHSIZE );static struct expr *expr_eliminate_yn (struct expr *e);
scripts/kconfig/internal.h +2 -2
@@ -6,14 +6,14 @@#define SYMBOL_HASHSIZE (1U << 14) -extern HASHTABLE_DECLARE(sym_hashtable, SYMBOL_HASHSIZE); +extern DECLARE_HASHTABLE(sym_hashtable, SYMBOL_HASHSIZE);#define for_all_symbols (sym) \ hash_for_each(sym_hashtable, sym, node)#define EXPR_HASHSIZE (1U << 14) -extern HASHTABLE_DECLARE(expr_hashtable, EXPR_HASHSIZE); +extern DECLARE_HASHTABLE(expr_hashtable, EXPR_HASHSIZE);void expr_invalidate_all (void );
scripts/kconfig/symbol.c +1 -1
@@ -971,7 +971,7 @@ bool sym_is_choice_value(const struct symbol *sym)return !list_empty (&sym->choice_link); } -HASHTABLE_DEFINE (sym_hashtable,SYMBOL_HASHSIZE ); +DEFINE_HASHTABLE (sym_hashtable,SYMBOL_HASHSIZE );struct symbol *sym_lookup (const char *name,int flags) {
scripts/kconfig/util.c +1 -1
@@ -14,7 +14,7 @@#include "lkc.h" /* hash table of all parsed Kconfig files */ -static HASHTABLE_DEFINE(file_hashtable, 1U << 11); +static DEFINE_HASHTABLE(file_hashtable, 1U << 11);struct file {struct hlist_node node;
scripts/mod/modpost.c +1 -1
@@ -234,7 +234,7 @@ struct symbol {char name[]; }; -static HASHTABLE_DEFINE(symbol_hashtable, 1U << 10); +static DEFINE_HASHTABLE(symbol_hashtable, 1U << 10);/** * Allocate a new symbols for use in the hash of exported symbols or
Signature · 1 line
2.56.0
Julian Braha
reviewerPATCH 4/5 scripts: add bit count parameter back to the host hash table functions
In reply to Julian BrahaThis 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.
---
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(-)
scripts/gendwarfksyms/die.c +1 -1
@@ -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;
scripts/gendwarfksyms/gendwarfksyms.h +1 -1
@@ -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);
scripts/gendwarfksyms/kabi.c +1 -1
@@ -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)
scripts/gendwarfksyms/symbols.c +2 -2
@@ -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) {
scripts/gendwarfksyms/types.c +1 -1
@@ -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) {
scripts/genksyms/genksyms.c +2 -2
@@ -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 ;
scripts/include/hashtable.h +18 -6
@@ -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) {
scripts/kconfig/expr.c +1 -1
@@ -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);
scripts/kconfig/internal.h +4 -4
@@ -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 );
scripts/kconfig/symbol.c +1 -1
@@ -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) {
scripts/kconfig/util.c +1 -1
@@ -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;
scripts/mod/modpost.c +1 -1
@@ -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
Signature · 1 line
2.56.0
Julian Braha
reviewerPATCH 5/5 kconfig: replace XOR with addition for expr_lookup() key generation
In reply to Julian BrahaAs Linus noted when reporting the 'make oldconfig' performance issue [1], XOR of the hash_ptr() values will clear the shared bits, increasing hash table collisions when l == r. In Kconfig, this happens when two sides of an expression are equal, for example 'range 1 1' in fs/erofs/Kconfig.
Let's replace the XOR usage with addition, as Linus suggested.
--- scripts/kconfig/expr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/kconfig/expr.c +1 -1
@@ -35,7 +35,7 @@ static struct expr *expr_lookup(enum expr_type type, void *l, void *r)struct expr *e;int hash; - hash =hash_32 ((unsigned int )type ^hash_ptr (l,32 ) ^hash_ptr (r,32 ),32 ); + hash =hash_32 ((unsigned int )type +hash_ptr (l,32 ) +hash_ptr (r,32 ),32 );hash_for_each_possible (expr_hashtable, e, node, hash) { if (e->type == type && e->left._initdata == l &&
Signature · 1 line
2.56.0