]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
kallsyms: cleanup code for appending the module buildid
authorPetr Mladek <pmladek@suse.com>
Fri, 28 Nov 2025 13:59:17 +0000 (14:59 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 11 Apr 2026 12:29:58 +0000 (14:29 +0200)
commit 8e81dac4cd5477731169b92cff7c24f8f6635950 upstream.

Put the code for appending the optional "buildid" into a helper function,
It makes __sprint_symbol() better readable.

Also print a warning when the "modname" is set and the "buildid" isn't.
It might catch a situation when some lookup function in
kallsyms_lookup_buildid() does not handle the "buildid".

Use pr_*_once() to avoid an infinite recursion when the function is called
from printk().  The recursion is rather theoretical but better be on the
safe side.

Link: https://lkml.kernel.org/r/20251128135920.217303-5-pmladek@suse.com
Signed-off-by: Petr Mladek <pmladek@suse.com>
Cc: Aaron Tomlin <atomlin@atomlin.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkman <daniel@iogearbox.net>
Cc: Daniel Gomez <da.gomez@samsung.com>
Cc: John Fastabend <john.fastabend@gmail.com>
Cc: Kees Cook <kees@kernel.org>
Cc: Luis Chamberalin <mcgrof@kernel.org>
Cc: Marc Rutland <mark.rutland@arm.com>
Cc: "Masami Hiramatsu (Google)" <mhiramat@kernel.org>
Cc: Petr Pavlu <petr.pavlu@suse.com>
Cc: Sami Tolvanen <samitolvanen@google.com>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
kernel/kallsyms.c

index cfa7076236e3bd5cec9b8f836da4578ea1856a64..0f639c907336383041e752d491a24741227666df 100644 (file)
@@ -434,6 +434,37 @@ int lookup_symbol_name(unsigned long addr, char *symname)
        return lookup_module_symbol_name(addr, symname);
 }
 
+#ifdef CONFIG_STACKTRACE_BUILD_ID
+
+static int append_buildid(char *buffer,  const char *modname,
+                         const unsigned char *buildid)
+{
+       if (!modname)
+               return 0;
+
+       if (!buildid) {
+               pr_warn_once("Undefined buildid for the module %s\n", modname);
+               return 0;
+       }
+
+       /* build ID should match length of sprintf */
+#ifdef CONFIG_MODULES
+       static_assert(sizeof(typeof_member(struct module, build_id)) == 20);
+#endif
+
+       return sprintf(buffer, " %20phN", buildid);
+}
+
+#else /* CONFIG_STACKTRACE_BUILD_ID */
+
+static int append_buildid(char *buffer,   const char *modname,
+                         const unsigned char *buildid)
+{
+       return 0;
+}
+
+#endif /* CONFIG_STACKTRACE_BUILD_ID */
+
 /* Look up a kernel symbol and return it in a text buffer. */
 static int __sprint_symbol(char *buffer, unsigned long address,
                           int symbol_offset, int add_offset, int add_buildid)
@@ -456,15 +487,8 @@ static int __sprint_symbol(char *buffer, unsigned long address,
 
        if (modname) {
                len += sprintf(buffer + len, " [%s", modname);
-#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID)
-               if (add_buildid && buildid) {
-                       /* build ID should match length of sprintf */
-#if IS_ENABLED(CONFIG_MODULES)
-                       static_assert(sizeof(typeof_member(struct module, build_id)) == 20);
-#endif
-                       len += sprintf(buffer + len, " %20phN", buildid);
-               }
-#endif
+               if (add_buildid)
+                       len += append_buildid(buffer + len, modname, buildid);
                len += sprintf(buffer + len, "]");
        }