From: Masahiro Yamada <masahiroy@kernel.org>
To: linux-kbuild@vger.kernel.org
Cc: linux-kernel@vger.kernel.org,
Peter Zijlstra <peterz@infradead.org>,
Lucas De Marchi <lucas.de.marchi@gmail.com>,
Masahiro Yamada <masahiroy@kernel.org>,
Daniel Gomez <da.gomez@samsung.com>,
Luis Chamberlain <mcgrof@kernel.org>,
Nathan Chancellor <nathan@kernel.org>,
Nicolas Schier <nicolas.schier@linux.dev>,
Petr Pavlu <petr.pavlu@suse.com>,
Sami Tolvanen <samitolvanen@google.com>,
linux-modules@vger.kernel.org
Subject: [PATCH] kbuild: stop module name mangling
Date: Mon, 2 Jun 2025 22:06:02 +0900 [thread overview]
Message-ID: <20250602130609.402581-1-masahiroy@kernel.org> (raw)
In the old days, KBUILD_MODNAME was passed to C without double quotes,
and then handled by __stringify() on the C side. This was the reason
why KBUILD_MODNAME was mangled: characters such as commas (,) and
hyphens (-) are not allowed in C identifiers, so they were replaced
with underscores (_) in Kbuild.
Since commit f83b5e323f57 ("kbuild: set correct KBUILD_MODNAME when
using well known kernel symbols as module names"), KBUILD_MODNAME has
been passed to C as a string literal, which allows any characters.
Aside from this historical behavior in the build system, there is no
longer a reason for mangling. In fact, it is rather annoying, as we
now need to convert between hyphens and underscores in some places,
but not in others. See commit 0267cbf297bf ("module: Account for the
build time module name mangling").
This commit eliminates that oddity, so the module name will now match
the filename. For example, the module name of "foo-bar.ko" will be
"foo-bar", not "foo_bar".
However, this oddity persisted for so long and also affected the
userspace. To adapt to this behavior, when a user runs "rmmod foo-bar",
kmod converts hyphens to underscores, and passes "foo_bar" to the
delete_module syscall.
Therefore, the mod_strncmp() needs to remain in find_module_all(),
otherwise, we cannot unload modules.
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---
kernel/module/main.c | 8 ++++++--
scripts/Makefile.lib | 4 ++--
2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/kernel/module/main.c b/kernel/module/main.c
index b8440b0887e3..1fa90a95e0c5 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -410,7 +410,11 @@ struct module *find_module_all(const char *name, size_t len,
lockdep_is_held(&module_mutex)) {
if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
continue;
- if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
+ /*
+ * For historical reasons, kmod passes a module name with
+ * a hyphen replaced with an underscore.
+ */
+ if (!mod_strncmp(mod->name, name, len))
return mod;
}
return NULL;
@@ -1135,7 +1139,7 @@ static bool module_match(const char *modname, const char *patterns)
if (*sep)
sep++;
- if (mod_strncmp(patterns, modname, len) == 0 && (glob || len == modlen))
+ if (strncmp(patterns, modname, len) == 0 && (glob || len == modlen))
return true;
}
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 336fb0d763c7..e37e2db5f528 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -18,8 +18,8 @@ target-stem = $(basename $(patsubst $(obj)/%,%,$@))
# end up in (or would, if it gets compiled in)
name-fix-token = $(subst $(comma),_,$(subst -,_,$1))
name-fix = $(call stringify,$(call name-fix-token,$1))
-basename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget))
-modname_flags = -DKBUILD_MODNAME=$(call name-fix,$(modname)) \
+basename_flags = -DKBUILD_BASENAME=$(call stringify,$(basetarget))
+modname_flags = -DKBUILD_MODNAME=$(call stringify,$(modname)) \
-D__KBUILD_MODNAME=kmod_$(call name-fix-token,$(modname))
modfile_flags = -DKBUILD_MODFILE=$(call stringify,$(modfile))
--
2.43.0
next reply other threads:[~2025-06-02 13:06 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-02 13:06 Masahiro Yamada [this message]
2025-06-02 19:00 ` Masahiro Yamada
2025-06-02 19:35 ` Lucas De Marchi
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=20250602130609.402581-1-masahiroy@kernel.org \
--to=masahiroy@kernel.org \
--cc=da.gomez@samsung.com \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-modules@vger.kernel.org \
--cc=lucas.de.marchi@gmail.com \
--cc=mcgrof@kernel.org \
--cc=nathan@kernel.org \
--cc=nicolas.schier@linux.dev \
--cc=peterz@infradead.org \
--cc=petr.pavlu@suse.com \
--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
all inboxes | Powered by JetHome®