lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <c8bc83b2c70aeaad3ce01d12dc1153981ab693f8.1679456900.git.jpoimboe@kernel.org>
Date:   Tue, 21 Mar 2023 21:00:07 -0700
From:   Josh Poimboeuf <jpoimboe@...nel.org>
To:     x86@...nel.org
Cc:     linux-kernel@...r.kernel.org,
        Peter Zijlstra <peterz@...radead.org>,
        Mark Rutland <mark.rutland@....com>,
        Jason Baron <jbaron@...mai.com>,
        Steven Rostedt <rostedt@...dmis.org>,
        Ard Biesheuvel <ardb@...nel.org>,
        Christophe Leroy <christophe.leroy@...roup.eu>,
        Paolo Bonzini <pbonzini@...hat.com>,
        Sean Christopherson <seanjc@...gle.com>,
        Sami Tolvanen <samitolvanen@...gle.com>,
        Nick Desaulniers <ndesaulniers@...gle.com>,
        Will McVicker <willmcvicker@...gle.com>,
        Kees Cook <keescook@...omium.org>,
        linux-arm-kernel@...ts.infradead.org
Subject: [PATCH v2 01/11] static_call: Improve key type abstraction

Make the static_call_key union less fragile by abstracting all knowledge
about the type bit into helper functions.

Signed-off-by: Josh Poimboeuf <jpoimboe@...nel.org>
---
 include/linux/static_call_types.h       |  4 +-
 kernel/static_call_inline.c             | 51 +++++++++++++++++--------
 tools/include/linux/static_call_types.h |  4 +-
 3 files changed, 40 insertions(+), 19 deletions(-)

diff --git a/include/linux/static_call_types.h b/include/linux/static_call_types.h
index 5a00b8b2cf9f..87c3598609e8 100644
--- a/include/linux/static_call_types.h
+++ b/include/linux/static_call_types.h
@@ -63,8 +63,8 @@ struct static_call_key {
 	union {
 		/* bit 0: 0 = mods, 1 = sites */
 		unsigned long type;
-		struct static_call_mod *mods;
-		struct static_call_site *sites;
+		struct static_call_mod *_mods;
+		struct static_call_site *_sites;
 	};
 };
 
diff --git a/kernel/static_call_inline.c b/kernel/static_call_inline.c
index 639397b5491c..41f6bda6773a 100644
--- a/kernel/static_call_inline.c
+++ b/kernel/static_call_inline.c
@@ -112,15 +112,21 @@ static inline void static_call_sort_entries(struct static_call_site *start,
 
 static inline bool static_call_key_has_mods(struct static_call_key *key)
 {
-	return !(key->type & 1);
+	return !!(key->type & 1);
 }
 
-static inline struct static_call_mod *static_call_key_next(struct static_call_key *key)
+static inline struct static_call_mod *static_call_key_mods(struct static_call_key *key)
 {
 	if (!static_call_key_has_mods(key))
 		return NULL;
 
-	return key->mods;
+	return (struct static_call_mod *)(key->type & ~1);
+}
+
+static inline void static_call_key_set_mods(struct static_call_key *key, struct static_call_mod *mods)
+{
+	key->_mods = mods;
+	key->type |= 1;
 }
 
 static inline struct static_call_site *static_call_key_sites(struct static_call_key *key)
@@ -128,7 +134,12 @@ static inline struct static_call_site *static_call_key_sites(struct static_call_
 	if (static_call_key_has_mods(key))
 		return NULL;
 
-	return (struct static_call_site *)(key->type & ~1);
+	return key->_sites;
+}
+
+static inline void static_call_key_set_sites(struct static_call_key *key, struct static_call_site *sites)
+{
+	key->_sites = sites;
 }
 
 void __static_call_update(struct static_call_key *key, void *tramp, void *func)
@@ -154,7 +165,7 @@ void __static_call_update(struct static_call_key *key, void *tramp, void *func)
 		goto done;
 
 	first = (struct static_call_mod){
-		.next = static_call_key_next(key),
+		.next = static_call_key_mods(key),
 		.mod = NULL,
 		.sites = static_call_key_sites(key),
 	};
@@ -250,8 +261,7 @@ static int __static_call_init(struct module *mod,
 			 * static_call_init() before memory allocation works.
 			 */
 			if (!mod) {
-				key->sites = site;
-				key->type |= 1;
+				static_call_key_set_sites(key, site);
 				goto do_transform;
 			}
 
@@ -266,10 +276,10 @@ static int __static_call_init(struct module *mod,
 			 */
 			if (static_call_key_sites(key)) {
 				site_mod->mod = NULL;
-				site_mod->next = NULL;
 				site_mod->sites = static_call_key_sites(key);
+				site_mod->next = NULL;
 
-				key->mods = site_mod;
+				static_call_key_set_mods(key, site_mod);
 
 				site_mod = kzalloc(sizeof(*site_mod), GFP_KERNEL);
 				if (!site_mod)
@@ -278,8 +288,9 @@ static int __static_call_init(struct module *mod,
 
 			site_mod->mod = mod;
 			site_mod->sites = site;
-			site_mod->next = static_call_key_next(key);
-			key->mods = site_mod;
+			site_mod->next = static_call_key_mods(key);
+
+			static_call_key_set_mods(key, site_mod);
 		}
 
 do_transform:
@@ -406,7 +417,7 @@ static void static_call_del_module(struct module *mod)
 	struct static_call_site *stop = mod->static_call_sites +
 					mod->num_static_call_sites;
 	struct static_call_key *key, *prev_key = NULL;
-	struct static_call_mod *site_mod, **prev;
+	struct static_call_mod *site_mod, *prev;
 	struct static_call_site *site;
 
 	for (site = start; site < stop; site++) {
@@ -416,15 +427,25 @@ static void static_call_del_module(struct module *mod)
 
 		prev_key = key;
 
-		for (prev = &key->mods, site_mod = key->mods;
+		site_mod = static_call_key_mods(key);
+		if (!site_mod)
+			continue;
+
+		if (site_mod->mod == mod) {
+			static_call_key_set_mods(key, site_mod->next);
+			kfree(site_mod);
+			continue;
+		}
+
+		for (prev = site_mod, site_mod = site_mod->next;
 		     site_mod && site_mod->mod != mod;
-		     prev = &site_mod->next, site_mod = site_mod->next)
+		     prev = site_mod, site_mod = site_mod->next)
 			;
 
 		if (!site_mod)
 			continue;
 
-		*prev = site_mod->next;
+		prev->next = site_mod->next;
 		kfree(site_mod);
 	}
 }
diff --git a/tools/include/linux/static_call_types.h b/tools/include/linux/static_call_types.h
index 5a00b8b2cf9f..87c3598609e8 100644
--- a/tools/include/linux/static_call_types.h
+++ b/tools/include/linux/static_call_types.h
@@ -63,8 +63,8 @@ struct static_call_key {
 	union {
 		/* bit 0: 0 = mods, 1 = sites */
 		unsigned long type;
-		struct static_call_mod *mods;
-		struct static_call_site *sites;
+		struct static_call_mod *_mods;
+		struct static_call_site *_sites;
 	};
 };
 
-- 
2.39.2

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ