From: Masahiro Yamada <masahiroy@kernel.org>
To: linux-kbuild@vger.kernel.org
Cc: linux-s390@vger.kernel.org, Nicolas Schier a <nicolas@fjasle.eu>,
Peter Zijlstra <peterz@infradead.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Masahiro Yamada <masahiroy@kernel.org>,
linux-um@lists.infradead.org, linux-kernel@vger.kernel.org,
clang-built-linux@googlegroups.com,
Luis Chamberlain <mcgrof@kernel.org>,
Sami Tolvanen <samitolvanen@google.com>,
linuxppc-dev@lists.ozlabs.org, Ard Biesheuvel <ardb@kernel.org>,
Kees Cook <keescook@chromium.org>
Subject: [PATCH v3 13/15] modpost: use hlist for hash table implementation
Date: Thu, 5 May 2022 16:22:42 +0900 [thread overview]
Message-ID: <20220505072244.1155033-14-masahiroy@kernel.org> (raw)
In-Reply-To: <20220505072244.1155033-1-masahiroy@kernel.org>
Import hlist macros from include/linux/list.h to implement the hash
table in a more generic way.
While I was here, I increased the hash table size from 1024 to 8192
to decrease the hash collision.
I moved ARRAY_SIZE() from file2alias.c to modpost.h because it is needed
in modpost.c as well.
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---
(no changes since v2)
Changes in v2:
- Move to the end of the series because this is now optional
scripts/mod/file2alias.c | 2 --
scripts/mod/list.h | 52 ++++++++++++++++++++++++++++++++++++++++
scripts/mod/modpost.c | 39 +++++++++++++++---------------
scripts/mod/modpost.h | 2 ++
4 files changed, 73 insertions(+), 22 deletions(-)
diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c
index 5258247d78ac..e8a9c6816fec 100644
--- a/scripts/mod/file2alias.c
+++ b/scripts/mod/file2alias.c
@@ -734,8 +734,6 @@ static int do_vio_entry(const char *filename, void *symval,
return 1;
}
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-
static void do_input(char *alias,
kernel_ulong_t *arr, unsigned int min, unsigned int max)
{
diff --git a/scripts/mod/list.h b/scripts/mod/list.h
index a924a6c4aa4d..c60dbaa70d6b 100644
--- a/scripts/mod/list.h
+++ b/scripts/mod/list.h
@@ -210,4 +210,56 @@ static inline int list_empty(const struct list_head *head)
!list_entry_is_head(pos, head, member); \
pos = n, n = list_next_entry(n, member))
+/*
+ * Double linked lists with a single pointer list head.
+ * Mostly useful for hash tables where the two pointer list head is
+ * too wasteful.
+ * You lose the ability to access the tail in O(1).
+ */
+
+struct hlist_head {
+ struct hlist_node *first;
+};
+
+struct hlist_node {
+ struct hlist_node *next, **pprev;
+};
+
+/**
+ * hlist_add_head - add a new entry at the beginning of the hlist
+ * @n: new entry to be added
+ * @h: hlist head to add it after
+ *
+ * Insert a new entry after the specified head.
+ * This is good for implementing stacks.
+ */
+static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
+{
+ struct hlist_node *first = h->first;
+
+ n->next = first;
+ if (first)
+ first->pprev = &n->next;
+ h->first = n;
+ n->pprev = &h->first;
+}
+
+#define hlist_entry(ptr, type, member) container_of(ptr, type, member)
+
+#define hlist_entry_safe(ptr, type, member) \
+ ({ typeof(ptr) ____ptr = (ptr); \
+ ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
+ })
+
+/**
+ * hlist_for_each_entry - iterate over list of given type
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the hlist_node within the struct.
+ */
+#define hlist_for_each_entry(pos, head, member) \
+ for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
+ pos; \
+ pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
+
#endif /* LIST_H */
diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 4edd5b223f49..7f7e0818940f 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -199,13 +199,8 @@ static struct module *new_module(const char *modname)
return mod;
}
-/* A hash of all exported symbols,
- * struct symbol is also used for lists of unresolved symbols */
-
-#define SYMBOL_HASH_SIZE 1024
-
struct symbol {
- struct symbol *next;
+ struct hlist_node hash_node; /* link to the hash table */
struct list_head list; /* link to module::exported_symbols or module::unresolved_symbols */
struct module *module;
char *namespace;
@@ -217,8 +212,6 @@ struct symbol {
char name[];
};
-static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
-
/* This is based on the hash algorithm from gdbm, via tdb */
static inline unsigned int tdb_hash(const char *name)
{
@@ -232,6 +225,21 @@ static inline unsigned int tdb_hash(const char *name)
return (1103515243 * value + 12345);
}
+/* useful hash macros */
+#define hash_head(table, key) (&(table)[tdb_hash(key) % ARRAY_SIZE(table)])
+
+#define hash_add_symbol(sym, table) hlist_add_head(&(sym)->hash_node, \
+ hash_head(table, (sym)->name))
+
+#define hash_for_matched_symbol(sym, table, key) \
+ hlist_for_each_entry(sym, hash_head(table, key), hash_node) \
+ if (!strcmp(sym->name, key))
+
+#define HASHTABLE_DECLARE(name, size) struct hlist_head name[size]
+
+/* hash table of all exported symbols */
+HASHTABLE_DECLARE(exported_symbols, 8192);
+
/**
* Allocate a new symbols for use in the hash of exported symbols or
* the list of unresolved symbols per module
@@ -246,15 +254,6 @@ static struct symbol *alloc_symbol(const char *name)
return s;
}
-/* For the hash of exported symbols */
-static void hash_add_symbol(struct symbol *sym)
-{
- unsigned int hash;
-
- hash = tdb_hash(sym->name) % SYMBOL_HASH_SIZE;
- sym->next = symbolhash[hash];
- symbolhash[hash] = sym;
-}
static void sym_add_unresolved(const char *name, struct module *mod, bool weak)
{
@@ -274,8 +273,8 @@ static struct symbol *sym_find_with_module(const char *name, struct module *mod)
if (name[0] == '.')
name++;
- for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
- if (strcmp(s->name, name) == 0 && (!mod || s->module == mod))
+ hash_for_matched_symbol(s, exported_symbols, name) {
+ if (!mod || s->module == mod)
return s;
}
return NULL;
@@ -379,7 +378,7 @@ static struct symbol *sym_add_exported(const char *name, struct module *mod,
s->is_static = !mod->from_dump;
s->is_gpl_only = gpl_only;
list_add_tail(&s->list, &mod->exported_symbols);
- hash_add_symbol(s);
+ hash_add_symbol(s, exported_symbols);
return s;
}
diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h
index 2e8c897e0953..0cd8eec6f59b 100644
--- a/scripts/mod/modpost.h
+++ b/scripts/mod/modpost.h
@@ -14,6 +14,8 @@
#include "list.h"
#include "elfconfig.h"
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+
/* On BSD-alike OSes elf.h defines these according to host's word size */
#undef ELF_ST_BIND
#undef ELF_ST_TYPE
--
2.32.0
next prev parent reply other threads:[~2022-05-05 7:29 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-05 7:22 [PATCH v3 00/15] kbuild: yet another series of cleanups (modpost, LTO, MODULE_REL_CRCS) Masahiro Yamada
2022-05-05 7:22 ` [PATCH v3 01/15] modpost: mitigate false-negatives for static EXPORT_SYMBOL checks Masahiro Yamada
2022-05-05 19:25 ` Nicolas Schier
2022-05-05 7:22 ` [PATCH v3 02/15] modpost: change the license of EXPORT_SYMBOL to bool type Masahiro Yamada
2022-05-05 13:48 ` Masahiro Yamada
2022-05-05 19:53 ` Nicolas Schier
2022-05-05 7:22 ` [PATCH v3 03/15] modpost: merge add_{intree_flag, retpoline, staging_flag} to add_header Masahiro Yamada
2022-05-05 19:58 ` [PATCH v3 03/15] modpost: merge add_{intree_flag,retpoline,staging_flag} " Nicolas Schier
2022-05-05 7:22 ` [PATCH v3 04/15] modpost: move *.mod.c generation to write_mod_c_files() Masahiro Yamada
2022-05-05 20:06 ` Nicolas Schier
2022-05-05 7:22 ` [PATCH v3 05/15] kbuild: generate a list of objects in vmlinux Masahiro Yamada
2022-05-05 7:22 ` [PATCH v3 06/15] kbuild: record symbol versions in *.cmd files Masahiro Yamada
2022-05-05 7:22 ` [PATCH v3 07/15] modpost: extract symbol versions from " Masahiro Yamada
2022-05-05 20:09 ` Nicolas Schier
2022-05-05 7:22 ` [PATCH v3 08/15] kbuild: link symbol CRCs at final link, removing CONFIG_MODULE_REL_CRCS Masahiro Yamada
2022-05-05 7:22 ` [PATCH v3 09/15] kbuild: stop merging *.symversions Masahiro Yamada
2022-05-05 20:19 ` Nicolas Schier
2022-05-05 7:22 ` [PATCH v3 10/15] genksyms: adjust the output format to modpost Masahiro Yamada
2022-05-05 20:22 ` Nicolas Schier
2022-05-05 7:22 ` [PATCH v3 11/15] kbuild: do not create *.prelink.o for Clang LTO or IBT Masahiro Yamada
2022-05-05 7:22 ` [PATCH v3 12/15] modpost: simplify the ->is_static initialization Masahiro Yamada
2022-05-05 20:27 ` Nicolas Schier
2022-05-05 7:22 ` Masahiro Yamada [this message]
2022-05-05 7:22 ` [PATCH v3 14/15] kbuild: make built-in.a rule robust against too long argument error Masahiro Yamada
2022-05-05 20:29 ` Nicolas Schier
2022-05-05 20:31 ` Nick Desaulniers
2022-05-05 7:22 ` [PATCH v3 15/15] kbuild: make *.mod " Masahiro Yamada
2022-05-05 20:31 ` Nicolas Schier
2022-05-05 16:49 ` [PATCH v3 00/15] kbuild: yet another series of cleanups (modpost, LTO, MODULE_REL_CRCS) Masahiro Yamada
2022-05-06 22:45 ` Nathan Chancellor
2022-05-08 18:28 ` Masahiro Yamada
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=20220505072244.1155033-14-masahiroy@kernel.org \
--to=masahiroy@kernel.org \
--cc=ardb@kernel.org \
--cc=clang-built-linux@googlegroups.com \
--cc=keescook@chromium.org \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-s390@vger.kernel.org \
--cc=linux-um@lists.infradead.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=mcgrof@kernel.org \
--cc=ndesaulniers@google.com \
--cc=nicolas@fjasle.eu \
--cc=peterz@infradead.org \
--cc=samitolvanen@google.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;
as well as URLs for NNTP newsgroup(s).