Re: [PATCH v9 13/14] module: Move kdb_modules list out of core code

From: Daniel Thompson
Date: Thu Mar 03 2022 - 09:59:59 EST


On Thu, Mar 03, 2022 at 05:37:29AM -0800, Christoph Hellwig wrote:
> On Wed, Mar 02, 2022 at 08:56:23PM +0000, Christophe Leroy wrote:
> > Do we really want to hide the 'struct list_head modules' from external
> > world ?
> >
> > Otherwise we could declare it in include/linux/module.h ?
>
> I'd just move the trivial code that uses it from kernel/kdb/ to
> kernel/module/ as it is tied to module internals and just uses the
> KDB interfaces exposed to other parts of the kernel.

One of the best ways that we can common up code might be to dust
off some code I wrote a while back to display seq_files from
kdb.

The basic idea worked well enough but it often needs special
start/stop operatings to ensure the start meeds kdb's rather
odd locking restrictions. If there is a willingness for
something like the below to be included in the module code then we
could replace kdb_lsmod() with something that reused the code to
format /proc/modules.


Daniel.


diff --git a/kernel/module.c b/kernel/module.c
index 84a9141a5e159..ab43ee23cdba0 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -4664,7 +4664,33 @@ static int __init proc_modules_init(void)
return 0;
}
module_init(proc_modules_init);
-#endif
+
+#ifdef CONFIG_KGDB_KDB
+static void *kdb_m_start(struct seq_file *m, loff_t *pos)
+{
+ static LIST_HEAD(empty);
+ struct list_head *modlist = &modules;
+
+ if (mutex_is_locked(&module_mutex)) {
+ pr_info("Cannot display module list because it is
locked\n");
+ modlist = empty;
+ }
+
+ return seq_list_start(modlist, *pos);
+}
+
+const struct seq_operations kdb_modules_seqops = {
+ .start = kdb_m_start,
+ .next = m_next,
+ .show = m_show
+};
+#endif /* CONFIG_KGDB_KDB */
+
+/*
+ * TODO: Need to decide if it OK to disable kdb lsmod if
+ * !CONFIG_PROC_FS... but it probably is!
+ */
+#endif /* CONFIG_PROC_FS */

/* Given an address, look for it in the module exception tables. */
const struct exception_table_entry *search_module_extables(unsigned
long addr)