module: Add module_for_each_mod() function
authorSteven Rostedt <rostedt@goodmis.org>
Wed, 5 Mar 2025 16:45:44 +0000 (11:45 -0500)
committerSteven Rostedt (Google) <rostedt@goodmis.org>
Fri, 28 Mar 2025 12:39:27 +0000 (08:39 -0400)
The tracing system needs a way to save all the currently loaded modules
and their addresses into persistent memory so that it can evaluate the
addresses on a reboot from a crash. When the persistent memory trace
starts, it will load the module addresses and names into the persistent
memory. To do so, it will call the module_for_each_mod() function and pass
it a function and data structure to get called on each loaded module. Then
it can record the memory.

This only implements that function.

Cc: Luis Chamberlain <mcgrof@kernel.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Sami Tolvanen <samitolvanen@google.com>
Cc: Daniel Gomez <da.gomez@samsung.com>
Cc: linux-modules@vger.kernel.org
Link: https://lore.kernel.org/20250305164608.962615966@goodmis.org
Acked-by: Petr Pavlu <petr.pavlu@suse.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
include/linux/module.h
kernel/module/main.c

index 30e5b19bafa983e70e2a0509e0ccc3d7f58a7d6f..9a71dd2cb11fdcec6960589dadbc3aada4e3eb31 100644 (file)
@@ -782,6 +782,8 @@ static inline void *module_writable_address(struct module *mod, void *loc)
        return __module_writable_address(mod, loc);
 }
 
+void module_for_each_mod(int(*func)(struct module *mod, void *data), void *data);
+
 #else /* !CONFIG_MODULES... */
 
 static inline struct module *__module_address(unsigned long addr)
@@ -894,6 +896,10 @@ static inline void *module_writable_address(struct module *mod, void *loc)
 {
        return loc;
 }
+
+static inline void module_for_each_mod(int(*func)(struct module *mod, void *data), void *data)
+{
+}
 #endif /* CONFIG_MODULES */
 
 #ifdef CONFIG_SYSFS
index 1fb9ad289a6f8f328fc37c6d93730882d0e6e613..927a2e0ffd5fb54f041b04bf2b8587e9d0a86a72 100644 (file)
@@ -3809,6 +3809,19 @@ bool is_module_text_address(unsigned long addr)
        return ret;
 }
 
+void module_for_each_mod(int(*func)(struct module *mod, void *data), void *data)
+{
+       struct module *mod;
+
+       guard(rcu)();
+       list_for_each_entry_rcu(mod, &modules, list) {
+               if (mod->state == MODULE_STATE_UNFORMED)
+                       continue;
+               if (func(mod, data))
+                       break;
+       }
+}
+
 /**
  * __module_text_address() - get the module whose code contains an address.
  * @addr: the address.