Skip to contentSkip to conversation

module: core code clean up

PATCH Superseded Aaron Tomlin 7 participants 48 messages Feb 28, 2022 – Mar 7, 2022

Subsystems (3)

v9 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v9 cover module: core code clean up

Hi Luis,

As per your suggestion [1], this is an attempt to refactor and split optional code out of core module support code into separate components. This version is based on Linus' commit 7993e65fdd0f ("Merge tag 'mtd/fixes-for-5.17-rc5' of
git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux").

Petr,

I decided to use preempt_disable() instead to remain consistent with the rest of the file. Unfortunately, I did not make time to boot test etc.

Changes since v8 [2]

- Resolved the reported lockdep warnings in kernel/module/kallsyms.c (Petr Mladek)

Changes since v7 [3]

- Removed redundant ifdef CONFIG_MODULES and endif pairing from kernel/module/Makefile

Changes since v6 [4]

- Moved KCOV_INSTRUMENT_module.o out of kernel/Makefile into kernel/module/Makefile (Christophe Leroy)
- Moved kernel/module/signature.c back into kernel/ (Christophe Leroy)
- Fixed Oops in add_kallsyms() due to an invalid pointer assignment (Christophe Leroy)

Changes since v5 [5]:

- Updated MAINTAINERS to include the entire kernel/module/ directory (Christophe Leroy)
- Reintroduce commit a97ac8cb24a3 ("module: fix signature check failures when using in-kernel decompression") (Michal Suchánek)
- Refactored code to address some (i.e. --ignore=MULTIPLE_ASSIGNMENTS,ASSIGN_IN_IF was used) style violations e.g. "Alignment should match open parenthesis", reported by scripts/checkpatch.pl --strict (Christophe Leroy)
- Used PAGE_ALIGN() and PAGE_ALIGNED() instead (Christophe Leroy)
- Removed sig_enforce from include/linux/module.h as it is only used in kernel/module/signing.c (Christophe Leroy)
- Added static keyword for anything not used outside a source file (Christophe Leroy)
- Moved mod_sysfs_teardown() to kernel/module/sysfs.c (Christophe Leroy)
- Removed kdb_modules from kernel/debug/kdb/kdb_private.h (Christophe Leroy)

Changes since v4 [6]:

- Moved is_livepatch_module() and set_livepatch_module() to kernel/module/livepatch.c
- Addressed minor compiler warning concerning kernel/module/internal.h (0-day)
- Resolved style violations reported by scripts/checkpatch.pl
- Dropped patch 5 [7] so external patch [8] can be applied at a later date post merge into module-next (Christophe Leroy)

Changes since v3 [9]:

- Refactored both is_livepatch_module() and set_livepatch_module(), respectively, to use IS_ENABLED(CONFIG_LIVEPATCH) (Joe Perches)
- Addressed various compiler warnings e.g., no previous prototype (0-day)

Changes since v2 [10]:

- Moved module decompress support to a separate file
- Made check_modinfo_livepatch() generic (Petr Mladek)
- Removed filename from each newly created file (Luis Chamberlain)
- Addressed some (i.e. --ignore=ASSIGN_IN_IF,AVOID_BUG was used) minor scripts/checkpatch.pl concerns e.g., use strscpy over strlcpy and missing a blank line after declarations (Allen)

Changes since v1 [11]:

- Moved module version support code into a new file

[1]: https://lore.kernel.org/lkml/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/
[3]: https://lore.kernel.org/lkml/[email protected]/
[4]: https://lore.kernel.org/lkml/[email protected]/
[5]: https://lore.kernel.org/lkml/[email protected]/
[6]: https://lore.kernel.org/lkml/[email protected]/
[7]: https://lore.kernel.org/lkml/[email protected]/
[8]: https://lore.kernel.org/lkml/203348805c9ac9851d8939d15cb9802ef047b5e2.1643919758.git.christophe.leroy@csgroup.eu/
[9]: https://lore.kernel.org/lkml/[email protected]/
[10]: https://lore.kernel.org/lkml/[email protected]/
[11]: https://lore.kernel.org/lkml/[email protected]/

Aaron Tomlin (14):
module: Move all into module/
module: Simple refactor in preparation for split
module: Make internal.h and decompress.c more compliant
module: Move livepatch support to a separate file
module: Move latched RB-tree support to a separate file
module: Move strict rwx support to a separate file
module: Move extra signature support out of core code
module: Move kmemleak support to a separate file
module: Move kallsyms support into a separate file
module: kallsyms: Fix suspicious rcu usage
module: Move procfs support into a separate file
module: Move sysfs support into a separate file
module: Move kdb_modules list out of core code
module: Move version support into a separate file

 MAINTAINERS                                   |    2 +-
 include/linux/module.h                        |    9 +-
 kernel/Makefile                               |    5 +-
 kernel/debug/kdb/kdb_main.c                   |    5 +
 kernel/debug/kdb/kdb_private.h                |    4 -
 kernel/module-internal.h                      |   50 -
 kernel/module/Makefile                        |   20 +
 kernel/module/debug_kmemleak.c                |   30 +
 .../decompress.c}                             |    5 +-
 kernel/module/internal.h                      |  275 +++
 kernel/module/kallsyms.c                      |  512 +++++
 kernel/module/livepatch.c                     |   74 +
 kernel/{module.c => module/main.c}            | 1856 +----------------
 kernel/module/procfs.c                        |  142 ++
 kernel/module/signing.c                       |  122 ++
 kernel/module/strict_rwx.c                    |   85 +
 kernel/module/sysfs.c                         |  436 ++++
 kernel/module/tree_lookup.c                   |  109 +
 kernel/module/version.c                       |  109 +
 kernel/module_signing.c                       |   45 -
 20 files changed, 2013 insertions(+), 1882 deletions(-)
 delete mode 100644 kernel/module-internal.h
 create mode 100644 kernel/module/Makefile
 create mode 100644 kernel/module/debug_kmemleak.c
 rename kernel/{module_decompress.c => module/decompress.c} (99%)
 create mode 100644 kernel/module/internal.h
 create mode 100644 kernel/module/kallsyms.c
 create mode 100644 kernel/module/livepatch.c
 rename kernel/{module.c => module/main.c} (64%)
 create mode 100644 kernel/module/procfs.c
 create mode 100644 kernel/module/signing.c
 create mode 100644 kernel/module/strict_rwx.c
 create mode 100644 kernel/module/sysfs.c
 create mode 100644 kernel/module/tree_lookup.c
 create mode 100644 kernel/module/version.c
 delete mode 100644 kernel/module_signing.c

base-commit: 7993e65fdd0fe07beb9f36f998f9bbef2c0ee391

Signature · 1 line
2.34.1
#1 lore raw

PATCH v9 1/14 module: Move all into module/

No functional changes.

This patch moves all module related code into a separate directory, modifies each file name and creates a new Makefile. Note: this effort is in preparation to refactor core module code.

---
 MAINTAINERS                                         |  2 +-
 kernel/Makefile                                     |  5 +----
 kernel/module/Makefile                              | 12 ++++++++++++
 kernel/{module_decompress.c => module/decompress.c} |  2 +-
 kernel/{module-internal.h => module/internal.h}     |  0
 kernel/{module.c => module/main.c}                  |  2 +-
 kernel/{module_signing.c => module/signing.c}       |  2 +-
 7 files changed, 17 insertions(+), 8 deletions(-)
 create mode 100644 kernel/module/Makefile
 rename kernel/{module_decompress.c => module/decompress.c} (99%)
 rename kernel/{module-internal.h => module/internal.h} (100%)
 rename kernel/{module.c => module/main.c} (99%)
 rename kernel/{module_signing.c => module/signing.c} (97%)
MAINTAINERS +1 -1
index bd86ed9fbc79..463bdb829db4 100644
@@ -13012,7 +13012,7 @@ L:	[email protected]
 S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git modules-next
 F:	include/linux/module.h
-F:	kernel/module.c
+F:	kernel/module/
 
 MONOLITHIC POWER SYSTEM PMIC DRIVER
 M:	Saravanan Sekar <[email protected]>
kernel/Makefile +1 -4
index 56f4ee97f328..717075b65deb 100644
@@ -29,7 +29,6 @@ KCOV_INSTRUMENT_softirq.o := n
 KCSAN_SANITIZE_softirq.o = n
 # These are called from save_stack_trace() on slub debug path,
 # and produce insane amounts of uninteresting coverage.
-KCOV_INSTRUMENT_module.o := n
 KCOV_INSTRUMENT_extable.o := n
 KCOV_INSTRUMENT_stacktrace.o := n
 # Don't self-instrument.
@@ -53,6 +52,7 @@ obj-y += rcu/
 obj-y += livepatch/
 obj-y += dma/
 obj-y += entry/
+obj-$(CONFIG_MODULES) += module/
 
 obj-$(CONFIG_KCMP) += kcmp.o
 obj-$(CONFIG_FREEZER) += freezer.o
@@ -66,9 +66,6 @@ ifneq ($(CONFIG_SMP),y)
 obj-y += up.o
 endif
 obj-$(CONFIG_UID16) += uid16.o
-obj-$(CONFIG_MODULES) += module.o
-obj-$(CONFIG_MODULE_DECOMPRESS) += module_decompress.o
-obj-$(CONFIG_MODULE_SIG) += module_signing.o
 obj-$(CONFIG_MODULE_SIG_FORMAT) += module_signature.o
 obj-$(CONFIG_KALLSYMS) += kallsyms.o
 obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o
kernel/module/Makefile new file +12 -0
new file mode 100644
index 000000000000..cdd5c61b8c7f
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Makefile for linux kernel module support
+#
+
+# These are called from save_stack_trace() on slub debug path,
+# and produce insane amounts of uninteresting coverage.
+KCOV_INSTRUMENT_module.o := n
+
+obj-y += main.o
+obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o
+obj-$(CONFIG_MODULE_SIG) += signing.o
kernel/module/decompress.c renamed from kernel/module_decompress.c +1 -1
similarity index 99%
rename from kernel/module_decompress.c
rename to kernel/module/decompress.c
index ffef98a20320..d14d6443225a 100644
@@ -12,7 +12,7 @@
 #include <linux/sysfs.h>
 #include <linux/vmalloc.h>
 
-#include "module-internal.h"
+#include "internal.h"
 
 static int module_extend_max_pages(struct load_info *info, unsigned int extent)
 {
kernel/module/internal.h renamed from kernel/module-internal.h +0 -0
similarity index 100%
rename from kernel/module-internal.h
rename to kernel/module/internal.h
kernel/module/main.c renamed from kernel/module.c +1 -1
similarity index 99%
rename from kernel/module.c
rename to kernel/module/main.c
index 46a5c2ed1928..34a2b0cf3c3e 100644
@@ -58,7 +58,7 @@
 #include <linux/dynamic_debug.h>
 #include <linux/audit.h>
 #include <uapi/linux/module.h>
-#include "module-internal.h"
+#include "internal.h"
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/module.h>
kernel/module/signing.c renamed from kernel/module_signing.c +1 -1
similarity index 97%
rename from kernel/module_signing.c
rename to kernel/module/signing.c
index 8723ae70ea1f..8aeb6d2ee94b 100644
@@ -12,7 +12,7 @@
 #include <linux/string.h>
 #include <linux/verification.h>
 #include <crypto/public_key.h>
-#include "module-internal.h"
+#include "internal.h"
 
 /*
  * Verify the signature on a module.
Signature · 1 line
2.34.1
#2 lore raw

PATCH v9 2/14 module: Simple refactor in preparation for split

In reply to Aaron Tomlin

No functional change.

This patch makes it possible to move non-essential code out of core module code.

---
 kernel/module/internal.h | 21 +++++++++++++++++++++
 kernel/module/main.c     | 22 ++--------------------
 2 files changed, 23 insertions(+), 20 deletions(-)
kernel/module/internal.h +21 -0
index 8c381c99062f..ea8c4c02614c 100644
@@ -7,6 +7,27 @@
 
 #include <linux/elf.h>
 #include <asm/module.h>
+#include <linux/mutex.h>
+
+#ifndef ARCH_SHF_SMALL
+#define ARCH_SHF_SMALL 0
+#endif
+
+/* If this is set, the section belongs in the init part of the module */
+#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG - 1))
+/* Maximum number of characters written by module_flags() */
+#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
+
+extern struct mutex module_mutex;
+extern struct list_head modules;
+
+/* Provided by the linker */
+extern const struct kernel_symbol __start___ksymtab[];
+extern const struct kernel_symbol __stop___ksymtab[];
+extern const struct kernel_symbol __start___ksymtab_gpl[];
+extern const struct kernel_symbol __stop___ksymtab_gpl[];
+extern const s32 __start___kcrctab[];
+extern const s32 __start___kcrctab_gpl[];
 
 struct load_info {
 	const char *name;
kernel/module/main.c +2 -20
index 34a2b0cf3c3e..5f5e21f972dd 100644
@@ -63,10 +63,6 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/module.h>
 
-#ifndef ARCH_SHF_SMALL
-#define ARCH_SHF_SMALL 0
-#endif
-
 /*
  * Modules' sections will be aligned on page boundaries
  * to ensure complete separation of code and data, but
@@ -78,9 +74,6 @@
 # define debug_align(X) (X)
 #endif
 
-/* If this is set, the section belongs in the init part of the module */
-#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
-
 /*
  * Mutex protects:
  * 1) List of modules (also safely readable with preempt_disable),
@@ -88,8 +81,8 @@
  * 3) module_addr_min/module_addr_max.
  * (delete and add uses RCU list operations).
  */
-static DEFINE_MUTEX(module_mutex);
-static LIST_HEAD(modules);
+DEFINE_MUTEX(module_mutex);
+LIST_HEAD(modules);
 
 /* Work queue for freeing init sections in success case */
 static void do_free_init(struct work_struct *w);
@@ -408,14 +401,6 @@ static __maybe_unused void *any_section_objs(const struct load_info *info,
 	return (void *)info->sechdrs[sec].sh_addr;
 }
 
-/* Provided by the linker */
-extern const struct kernel_symbol __start___ksymtab[];
-extern const struct kernel_symbol __stop___ksymtab[];
-extern const struct kernel_symbol __start___ksymtab_gpl[];
-extern const struct kernel_symbol __stop___ksymtab_gpl[];
-extern const s32 __start___kcrctab[];
-extern const s32 __start___kcrctab_gpl[];
-
 #ifndef CONFIG_MODVERSIONS
 #define symversion(base, idx) NULL
 #else
@@ -4542,9 +4527,6 @@ static void cfi_cleanup(struct module *mod)
 #endif
 }
 
-/* Maximum number of characters written by module_flags() */
-#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
-
 /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
 static char *module_flags(struct module *mod, char *buf)
 {
Signature · 1 line
2.34.1
#3 lore raw

PATCH v9 3/14 module: Make internal.h and decompress.c more compliant

In reply to Aaron Tomlin

This patch will address the following warning and style violations generated by ./scripts/checkpatch.pl in strict mode:

WARNING: Use #include <linux/module.h> instead of <asm/module.h> #10: FILE: kernel/module/internal.h:10:
+#include <asm/module.h>

CHECK: spaces preferred around that '-' (ctx:VxV) #18: FILE: kernel/module/internal.h:18:
+#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))

CHECK: Please use a blank line after function/struct/union/enum declarations #69: FILE: kernel/module/internal.h:69:
+}
+static inline void module_decompress_cleanup(struct load_info *info)

						   ^

CHECK: extern prototypes should be avoided in .h files #84: FILE: kernel/module/internal.h:84:
+extern int mod_verify_sig(const void *mod, struct load_info *info);

WARNING: Missing a blank line after declarations #116: FILE: kernel/module/decompress.c:116:
+ struct page *page = module_get_next_page(info);
+ if (!page) {

WARNING: Missing a blank line after declarations #174: FILE: kernel/module/decompress.c:174:
+ struct page *page = module_get_next_page(info);
+ if (!page) {

CHECK: Please use a blank line after function/struct/union/enum declarations #258: FILE: kernel/module/decompress.c:258:
+}
+static struct kobj_attribute module_compression_attr = __ATTR_RO(compression);

Note: Fortunately, the multiple-include optimisation found in include/linux/module.h will prevent duplication/or inclusion more than once.

Fixes f314dfea16a ("modsign: log module name in the event of an error")
---
 kernel/module/decompress.c | 3 +++
 kernel/module/internal.h   | 6 ++++--
 2 files changed, 7 insertions(+), 2 deletions(-)
kernel/module/decompress.c +3 -0
index d14d6443225a..2fc7081dd7c1 100644
@@ -113,6 +113,7 @@ static ssize_t module_gzip_decompress(struct load_info *info,
 
 	do {
 		struct page *page = module_get_next_page(info);
+
 		if (!page) {
 			retval = -ENOMEM;
 			goto out_inflate_end;
@@ -171,6 +172,7 @@ static ssize_t module_xz_decompress(struct load_info *info,
 
 	do {
 		struct page *page = module_get_next_page(info);
+
 		if (!page) {
 			retval = -ENOMEM;
 			goto out;
@@ -256,6 +258,7 @@ static ssize_t compression_show(struct kobject *kobj,
 {
 	return sysfs_emit(buf, "%s\n", __stringify(MODULE_COMPRESSION));
 }
+
 static struct kobj_attribute module_compression_attr = __ATTR_RO(compression);
 
 static int __init module_decompress_sysfs_init(void)
kernel/module/internal.h +4 -2
index ea8c4c02614c..e0775e66bcf7 100644
@@ -6,7 +6,8 @@
  */
 
 #include <linux/elf.h>
-#include <asm/module.h>
+#include <linux/compiler.h>
+#include <linux/module.h>
 #include <linux/mutex.h>
 
 #ifndef ARCH_SHF_SMALL
@@ -54,7 +55,7 @@ struct load_info {
 	} index;
 };
 
-extern int mod_verify_sig(const void *mod, struct load_info *info);
+int mod_verify_sig(const void *mod, struct load_info *info);
 
 #ifdef CONFIG_MODULE_DECOMPRESS
 int module_decompress(struct load_info *info, const void *buf, size_t size);
@@ -65,6 +66,7 @@ static inline int module_decompress(struct load_info *info,
 {
 	return -EOPNOTSUPP;
 }
+
 static inline void module_decompress_cleanup(struct load_info *info)
 {
 }
Signature · 1 line
2.34.1
#4 lore raw

PATCH v9 4/14 module: Move livepatch support to a separate file

In reply to Aaron Tomlin

No functional change.

This patch migrates livepatch support (i.e. used during module add/or load and remove/or deletion) from core module code into kernel/module/livepatch.c. At the moment it contains code to persist Elf information about a given livepatch module, only.

---
 include/linux/module.h    |   9 ++--
 kernel/module/Makefile    |   1 +
 kernel/module/internal.h  |  22 ++++++++
 kernel/module/livepatch.c |  74 +++++++++++++++++++++++++++
 kernel/module/main.c      | 102 ++++----------------------------------
 5 files changed, 110 insertions(+), 98 deletions(-)
 create mode 100644 kernel/module/livepatch.c
include/linux/module.h +3 -6
index 1e135fd5c076..7ec9715de7dc 100644
@@ -663,17 +663,14 @@ static inline bool module_requested_async_probing(struct module *module)
 	return module && module->async_probe_requested;
 }
 
-#ifdef CONFIG_LIVEPATCH
 static inline bool is_livepatch_module(struct module *mod)
 {
+#ifdef CONFIG_LIVEPATCH
 	return mod->klp;
-}
-#else /* !CONFIG_LIVEPATCH */
-static inline bool is_livepatch_module(struct module *mod)
-{
+#else
 	return false;
+#endif
 }
-#endif /* CONFIG_LIVEPATCH */
 
 bool is_module_sig_enforced(void);
 void set_module_sig_enforced(void);
kernel/module/Makefile +1 -0
index cdd5c61b8c7f..ed3aacb04f17 100644
@@ -10,3 +10,4 @@ KCOV_INSTRUMENT_module.o := n
 obj-y += main.o
 obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o
 obj-$(CONFIG_MODULE_SIG) += signing.o
+obj-$(CONFIG_LIVEPATCH) += livepatch.o
kernel/module/internal.h +22 -0
index e0775e66bcf7..ad7a444253ed 100644
@@ -57,6 +57,28 @@ struct load_info {
 
 int mod_verify_sig(const void *mod, struct load_info *info);
 
+#ifdef CONFIG_LIVEPATCH
+int copy_module_elf(struct module *mod, struct load_info *info);
+void free_module_elf(struct module *mod);
+#else /* !CONFIG_LIVEPATCH */
+static inline int copy_module_elf(struct module *mod, struct load_info *info)
+{
+	return 0;
+}
+
+static inline void free_module_elf(struct module *mod) { }
+#endif /* CONFIG_LIVEPATCH */
+
+static inline bool set_livepatch_module(struct module *mod)
+{
+#ifdef CONFIG_LIVEPATCH
+	mod->klp = true;
+	return true;
+#else
+	return false;
+#endif
+}
+
 #ifdef CONFIG_MODULE_DECOMPRESS
 int module_decompress(struct load_info *info, const void *buf, size_t size);
 void module_decompress_cleanup(struct load_info *info);
kernel/module/livepatch.c new file +74 -0

74 lines not on this page · raw message

kernel/module/main.c +10 -92

121 lines not on this page · raw message

Signature · 1 line
2.34.1
#5 lore raw

PATCH v9 5/14 module: Move latched RB-tree support to a separate file

In reply to Aaron Tomlin

No functional change.

This patch migrates module latched RB-tree support (e.g. see __module_address()) from core module code into kernel/module/tree_lookup.c.

---
 kernel/module/Makefile      |   1 +
 kernel/module/internal.h    |  33 +++++++++
 kernel/module/main.c        | 130 ++----------------------------------
 kernel/module/tree_lookup.c | 109 ++++++++++++++++++++++++++++++
 4 files changed, 147 insertions(+), 126 deletions(-)
 create mode 100644 kernel/module/tree_lookup.c
kernel/module/Makefile +1 -0
index ed3aacb04f17..88774e386276 100644
@@ -11,3 +11,4 @@ obj-y += main.o
 obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o
 obj-$(CONFIG_MODULE_SIG) += signing.o
 obj-$(CONFIG_LIVEPATCH) += livepatch.o
+obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o
kernel/module/internal.h +33 -0
index ad7a444253ed..f1682e3677be 100644
@@ -9,6 +9,7 @@
 #include <linux/compiler.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
+#include <linux/rculist.h>
 
 #ifndef ARCH_SHF_SMALL
 #define ARCH_SHF_SMALL 0
@@ -93,3 +94,35 @@ static inline void module_decompress_cleanup(struct load_info *info)
 {
 }
 #endif
+
+#ifdef CONFIG_MODULES_TREE_LOOKUP
+struct mod_tree_root {
+	struct latch_tree_root root;
+	unsigned long addr_min;
+	unsigned long addr_max;
+};
+
+extern struct mod_tree_root mod_tree;
+
+void mod_tree_insert(struct module *mod);
+void mod_tree_remove_init(struct module *mod);
+void mod_tree_remove(struct module *mod);
+struct module *mod_find(unsigned long addr);
+#else /* !CONFIG_MODULES_TREE_LOOKUP */
+
+static inline void mod_tree_insert(struct module *mod) { }
+static inline void mod_tree_remove_init(struct module *mod) { }
+static inline void mod_tree_remove(struct module *mod) { }
+static inline struct module *mod_find(unsigned long addr)
+{
+	struct module *mod;
+
+	list_for_each_entry_rcu(mod, &modules, list,
+				lockdep_is_held(&module_mutex)) {
+		if (within_module(addr, mod))
+			return mod;
+	}
+
+	return NULL;
+}
+#endif /* CONFIG_MODULES_TREE_LOOKUP */
kernel/module/main.c +4 -126

142 lines not on this page · raw message

kernel/module/tree_lookup.c new file +109 -0

109 lines not on this page · raw message

Signature · 1 line
2.34.1
#6 lore raw

PATCH v9 6/14 module: Move strict rwx support to a separate file

In reply to Aaron Tomlin

No functional change.

This patch migrates code that makes module text
and rodata memory read-only and non-text memory
non-executable from core module code into
kernel/module/strict_rwx.c.

---
 kernel/module/Makefile     |  1 +
 kernel/module/internal.h   | 32 ++++++++++++
 kernel/module/main.c       | 99 +-------------------------------------
 kernel/module/strict_rwx.c | 85 ++++++++++++++++++++++++++++++++
 4 files changed, 120 insertions(+), 97 deletions(-)
 create mode 100644 kernel/module/strict_rwx.c
kernel/module/Makefile +1 -0
index 88774e386276..d313c8472cb3 100644
@@ -12,3 +12,4 @@ obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o
 obj-$(CONFIG_MODULE_SIG) += signing.o
 obj-$(CONFIG_LIVEPATCH) += livepatch.o
 obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o
+obj-$(CONFIG_STRICT_MODULE_RWX) += strict_rwx.o
kernel/module/internal.h +32 -0
index f1682e3677be..a6895bb5598a 100644
@@ -20,6 +20,17 @@
 /* Maximum number of characters written by module_flags() */
 #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
 
+/*
+ * Modules' sections will be aligned on page boundaries
+ * to ensure complete separation of code and data, but
+ * only when CONFIG_ARCH_HAS_STRICT_MODULE_RWX=y
+ */
+#ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
+# define debug_align(X) PAGE_ALIGN(X)
+#else
+# define debug_align(X) (X)
+#endif
+
 extern struct mutex module_mutex;
 extern struct list_head modules;
 
@@ -126,3 +137,24 @@ static inline struct module *mod_find(unsigned long addr)
 	return NULL;
 }
 #endif /* CONFIG_MODULES_TREE_LOOKUP */
+
+#ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX
+void frob_text(const struct module_layout *layout, int (*set_memory)(unsigned long start,
+								     int num_pages));
+#endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
+
+#ifdef CONFIG_STRICT_MODULE_RWX
+void module_enable_ro(const struct module *mod, bool after_init);
+void module_enable_nx(const struct module *mod);
+int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
+				char *secstrings, struct module *mod);
+
+#else /* !CONFIG_STRICT_MODULE_RWX */
+static inline void module_enable_nx(const struct module *mod) { }
+static inline void module_enable_ro(const struct module *mod, bool after_init) {}
+static inline int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
+				       char *secstrings, struct module *mod)
+{
+	return 0;
+}
+#endif /* CONFIG_STRICT_MODULE_RWX */
kernel/module/main.c +2 -97

117 lines not on this page · raw message

kernel/module/strict_rwx.c new file +85 -0

85 lines not on this page · raw message

Signature · 1 line
2.34.1
#7 lore raw

PATCH v9 7/14 module: Move extra signature support out of core code

In reply to Aaron Tomlin

No functional change.

This patch migrates additional module signature check code from core module code into kernel/module/signing.c.

---
 kernel/module/internal.h |  9 +++++
 kernel/module/main.c     | 87 ----------------------------------------
 kernel/module/signing.c  | 77 +++++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 87 deletions(-)
kernel/module/internal.h +9 -0
index a6895bb5598a..d6f646a5da41 100644
@@ -158,3 +158,12 @@ static inline int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
 	return 0;
 }
 #endif /* CONFIG_STRICT_MODULE_RWX */
+
+#ifdef CONFIG_MODULE_SIG
+int module_sig_check(struct load_info *info, int flags);
+#else /* !CONFIG_MODULE_SIG */
+static inline int module_sig_check(struct load_info *info, int flags)
+{
+	return 0;
+}
+#endif /* !CONFIG_MODULE_SIG */
kernel/module/main.c +0 -87

105 lines not on this page · raw message

kernel/module/signing.c +77 -0

89 lines not on this page · raw message

Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH v9 07/14] module: Move extra signature support out of core code

Le 01/03/2022 à 00:43, Aaron Tomlin a écrit :

Quoted 29 lines

No functional change.

This patch migrates additional module signature check code from core module code into kernel/module/signing.c.

---
  kernel/module/internal.h |  9 +++++
  kernel/module/main.c     | 87 ----------------------------------------
  kernel/module/signing.c  | 77 +++++++++++++++++++++++++++++++++++
  3 files changed, 86 insertions(+), 87 deletions(-)
kernel/module/signing.c +4 -0
index 8aeb6d2ee94b..85c8999dfecf 100644
@@ -11,9 +11,29 @@
  #include <linux/module_signature.h>
  #include <linux/string.h>
  #include <linux/verification.h>
+#include <linux/security.h>
  #include <crypto/public_key.h>
+#include <uapi/linux/module.h>
  #include "internal.h"
 
+static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE);
+module_param(sig_enforce, bool_enable_only, 0644);

kernel/module/signing.c line 30

Quoted 10 lines
kernel/module/signing.c +10 -0
+
+/*
+ * Export sig_enforce kernel cmdline parameter to allow other subsystems rely
+ * on that instead of directly to CONFIG_MODULE_SIG_FORCE config.
+ */
+bool is_module_sig_enforced(void)
+{
+	return sig_enforce;
+}
+EXPORT_SYMBOL(is_module_sig_enforced);

As reported by the test robot, that's not enough.

When it was in main.c, is_module_sig_enforced() was build as soon as CONFIG_MODULES was set.

Now it is only built when CONFIG_MODULE_SIG is selected, so you have to modify include/linux/modules.h and have the stub is_module_sig_enforced() when CONFIG_MODULE_SIG is not selected and not only when CONFIG_MODULES is not selected.

kernel/module/signing.c line 39

Quoted 9 lines
kernel/module/signing.c +6 -0
+
+void set_module_sig_enforced(void)
+{
+	sig_enforce = true;
+}
+
  /*
   * Verify the signature on a module.
   */

Christophe

#9 lore raw

Re: [PATCH v9 07/14] module: Move extra signature support out of core code

Quoted 7 lines · Christophe Leroy

kernel/module/signing.c line 30

Quoted 5 lines
kernel/module/signing.c +5 -0
+bool is_module_sig_enforced(void)
+{
+	return sig_enforce;
+}
+EXPORT_SYMBOL(is_module_sig_enforced);

As reported by the test robot, that's not enough.

Hi Christophe,

Thanks for testing this.

Quoted 3 lines

When it was in main.c, is_module_sig_enforced() was build as soon as CONFIG_MODULES was set.
Now it is only built when CONFIG_MODULE_SIG is selected,

Agreed.

Quoted 3 lines

so you have to modify include/linux/modules.h and have the stub is_module_sig_enforced() when CONFIG_MODULE_SIG is not selected and not only when CONFIG_MODULES is not selected.

Sure: when Kconfig CONFIG_MODULE_SIG is not selected.

Luis,

I can see that the latest series is in mcgrof/modules-testing. Should I address the above as a separate patch with "Fixes:" or provide a whole new series, with the fix within the same patch? In my opinion, another iteration would be cleaner.

Kind regards,

Signature · 1 line
Aaron Tomlin
#10 lore raw

Re: [PATCH v9 07/14] module: Move extra signature support out of core code

Le 02/03/2022 à 14:33, Aaron Tomlin a écrit :

Quoted 23 lines · Christophe Leroy
Quoted 7 lines

kernel/module/signing.c line 30

Quoted 5 lines
kernel/module/signing.c +5 -0
+bool is_module_sig_enforced(void)
+{
+	return sig_enforce;
+}
+EXPORT_SYMBOL(is_module_sig_enforced);

As reported by the test robot, that's not enough.

Hi Christophe,

Thanks for testing this.

Quoted 3 lines

When it was in main.c, is_module_sig_enforced() was build as soon as CONFIG_MODULES was set.
Now it is only built when CONFIG_MODULE_SIG is selected,

Agreed.

Quoted 3 lines

so you have to modify include/linux/modules.h and have the stub is_module_sig_enforced() when CONFIG_MODULE_SIG is not selected and not only when CONFIG_MODULES is not selected.

Quoted 10 lines

Sure: when Kconfig CONFIG_MODULE_SIG is not selected.

Luis,

I can see that the latest series is in mcgrof/modules-testing. Should I address the above as a separate patch with "Fixes:" or provide a whole new series, with the fix within the same patch? In my opinion, another iteration would be cleaner.

On the powerpc list, usually for this kind of stuff, if the fixup doesn't impact the other commits of the series, we provide an incremental fixup and Michael squashes it in the faulty commit while rebasing.

That way you get the advantage of a new iteration without the disadvantages.

Up to Luis to tell what he prefers.

Christophe

#11 lore raw

Re: [PATCH v9 07/14] module: Move extra signature support out of core code

In reply to Christophe Leroy
Quoted 7 lines · Christophe Leroy

When it was in main.c, is_module_sig_enforced() was build as soon as CONFIG_MODULES was set.

Now it is only built when CONFIG_MODULE_SIG is selected, so you have to modify include/linux/modules.h and have the stub is_module_sig_enforced() when CONFIG_MODULE_SIG is not selected and not only when CONFIG_MODULES is not selected.

Hi Christophe,

Looking at this again, perhaps I'm missing something. If I understand correctly, Kconfig CONFIG_MODULE_SIG cannot be selected without CONFIG_MODULES; also CONFIG_MODULE_SIG depends on CONFIG_MODULES, no? So, what is present is enough right i.e. the stub when CONFIG_MODULES is not enabled?

Kind regards,

Signature · 1 line
Aaron Tomlin
#12 lore raw

Re: [PATCH v9 07/14] module: Move extra signature support out of core code

Le 05/03/2022 à 21:37, Aaron Tomlin a écrit :

Quoted 9 lines · Christophe Leroy
Quoted 7 lines

When it was in main.c, is_module_sig_enforced() was build as soon as CONFIG_MODULES was set.

Now it is only built when CONFIG_MODULE_SIG is selected, so you have to modify include/linux/modules.h and have the stub is_module_sig_enforced() when CONFIG_MODULE_SIG is not selected and not only when CONFIG_MODULES is not selected.

Quoted 8 lines

Hi Christophe,

Looking at this again, perhaps I'm missing something. If I understand correctly, Kconfig CONFIG_MODULE_SIG cannot be selected without CONFIG_MODULES; also CONFIG_MODULE_SIG depends on CONFIG_MODULES, no? So, what is present is enough right i.e. the stub when CONFIG_MODULES is not enabled?

There are three possibilities:
1/ CONFIG_MODULES=n, CONFIG_MODULE_SIG=n
2/ CONFIG_MODULES=y, CONFIG_MODULE_SIG=n
3/ CONFIG_MODULES=y, CONFIG_MODULE_SIG=y

Case 1/, is_module_sig_enforced() is a static inline stub in linux/modules.h returning always false

Case 3/, is_module_sig_enforced() is in kernel/module/signing.c

Case 2/, is_module_sig_enforced() is nowhere.

In linux/modules.h, you have:

#ifdef CONFIG_MODULES
....
a lot of stuff
...
bool is_module_sig_enforced(void);
void set_module_sig_enforced(void);

#else
....
a lot of stuff
...
static inline bool is_module_sig_enforced(void)

{
	return false;
}

static inline void set_module_sig_enforced(void)
{
}
...
some more stuff
#endif

You must take is_module_sig_enforced() out of that #ifdef CONFIG_MODULES / #else / #endif and add after the #endif /* CONFIG_MODULES */:

#ifdef CONFIG_MODULE_SIG
bool is_module_sig_enforced(void);
#else
static inline bool is_module_sig_enforced(void)

{
	return false;
}

#endif

Christophe

#13 lore raw

Re: [PATCH v9 07/14] module: Move extra signature support out of core code

Quoted 1 line · Christophe Leroy

2/ CONFIG_MODULES=y, CONFIG_MODULE_SIG=n

Christophe,

I see, I did not notice that the above is even possible.

Quoted 1 line

Case 2/, is_module_sig_enforced() is nowhere.

Understood.

Luis,

I'll send a v10 later today.

Kind regards,

Signature · 1 line
Aaron Tomlin
#14 lore raw

PATCH v9 8/14 module: Move kmemleak support to a separate file

In reply to Aaron Tomlin

No functional change.

This patch migrates kmemleak code out of core module code into kernel/module/debug_kmemleak.c

---
 kernel/module/Makefile         |  1 +
 kernel/module/debug_kmemleak.c | 30 ++++++++++++++++++++++++++++++
 kernel/module/internal.h       |  7 +++++++
 kernel/module/main.c           | 27 ---------------------------
 4 files changed, 38 insertions(+), 27 deletions(-)
 create mode 100644 kernel/module/debug_kmemleak.c
kernel/module/Makefile +1 -0
index d313c8472cb3..12388627725c 100644
@@ -13,3 +13,4 @@ obj-$(CONFIG_MODULE_SIG) += signing.o
 obj-$(CONFIG_LIVEPATCH) += livepatch.o
 obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o
 obj-$(CONFIG_STRICT_MODULE_RWX) += strict_rwx.o
+obj-$(CONFIG_DEBUG_KMEMLEAK) += debug_kmemleak.o
kernel/module/debug_kmemleak.c new file +30 -0
new file mode 100644
index 000000000000..12a569d361e8
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Module kmemleak support
+ *
+ * Copyright (C) 2009 Catalin Marinas
+ */
+
+#include <linux/module.h>
+#include <linux/kmemleak.h>
+#include "internal.h"
+
+void kmemleak_load_module(const struct module *mod,
+			  const struct load_info *info)
+{
+	unsigned int i;
+
+	/* only scan the sections containing data */
+	kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
+
+	for (i = 1; i < info->hdr->e_shnum; i++) {
+		/* Scan all writable sections that's not executable */
+		if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
+		    !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
+		    (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
+			continue;
+
+		kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
+				   info->sechdrs[i].sh_size, GFP_KERNEL);
+	}
+}
kernel/module/internal.h +7 -0
index d6f646a5da41..b0c360839f63 100644
@@ -167,3 +167,10 @@ static inline int module_sig_check(struct load_info *info, int flags)
 	return 0;
 }
 #endif /* !CONFIG_MODULE_SIG */
+
+#ifdef CONFIG_DEBUG_KMEMLEAK
+void kmemleak_load_module(const struct module *mod, const struct load_info *info);
+#else /* !CONFIG_DEBUG_KMEMLEAK */
+static inline void kmemleak_load_module(const struct module *mod,
+					const struct load_info *info) { }
+#endif /* CONFIG_DEBUG_KMEMLEAK */
kernel/module/main.c +0 -27
index c63e10c61694..7dd283959c5c 100644
@@ -2519,33 +2519,6 @@ bool __weak module_exit_section(const char *name)
 	return strstarts(name, ".exit");
 }
 
-#ifdef CONFIG_DEBUG_KMEMLEAK
-static void kmemleak_load_module(const struct module *mod,
-				 const struct load_info *info)
-{
-	unsigned int i;
-
-	/* only scan the sections containing data */
-	kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
-
-	for (i = 1; i < info->hdr->e_shnum; i++) {
-		/* Scan all writable sections that's not executable */
-		if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
-		    !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
-		    (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
-			continue;
-
-		kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
-				   info->sechdrs[i].sh_size, GFP_KERNEL);
-	}
-}
-#else
-static inline void kmemleak_load_module(const struct module *mod,
-					const struct load_info *info)
-{
-}
-#endif
-
 static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr)
 {
 #if defined(CONFIG_64BIT)
Signature · 1 line
2.34.1
#15 lore raw

PATCH v9 9/14 module: Move kallsyms support into a separate file

In reply to Aaron Tomlin

No functional change.

This patch migrates kallsyms code out of core module code kernel/module/kallsyms.c

---
 kernel/module/Makefile   |   1 +
 kernel/module/internal.h |  29 +++
 kernel/module/kallsyms.c | 502 ++++++++++++++++++++++++++++++++++++
 kernel/module/main.c     | 531 +--------------------------------------
 4 files changed, 538 insertions(+), 525 deletions(-)
 create mode 100644 kernel/module/kallsyms.c
kernel/module/Makefile +1 -0
index 12388627725c..9901bed3ab5b 100644
@@ -14,3 +14,4 @@ obj-$(CONFIG_LIVEPATCH) += livepatch.o
 obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o
 obj-$(CONFIG_STRICT_MODULE_RWX) += strict_rwx.o
 obj-$(CONFIG_DEBUG_KMEMLEAK) += debug_kmemleak.o
+obj-$(CONFIG_KALLSYMS) += kallsyms.o
kernel/module/internal.h +29 -0
index b0c360839f63..44ca05b9eb8f 100644
@@ -68,6 +68,19 @@ struct load_info {
 };
 
 int mod_verify_sig(const void *mod, struct load_info *info);
+struct module *find_module_all(const char *name, size_t len, bool even_unformed);
+int cmp_name(const void *name, const void *sym);
+long module_get_offset(struct module *mod, unsigned int *size, Elf_Shdr *sechdr,
+		       unsigned int section);
+
+static inline unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
+{
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+	return (unsigned long)offset_to_ptr(&sym->value_offset);
+#else
+	return sym->value;
+#endif
+}
 
 #ifdef CONFIG_LIVEPATCH
 int copy_module_elf(struct module *mod, struct load_info *info);
@@ -174,3 +187,19 @@ void kmemleak_load_module(const struct module *mod, const struct load_info *info
 static inline void kmemleak_load_module(const struct module *mod,
 					const struct load_info *info) { }
 #endif /* CONFIG_DEBUG_KMEMLEAK */
+
+#ifdef CONFIG_KALLSYMS
+void init_build_id(struct module *mod, const struct load_info *info);
+void layout_symtab(struct module *mod, struct load_info *info);
+void add_kallsyms(struct module *mod, const struct load_info *info);
+unsigned long find_kallsyms_symbol_value(struct module *mod, const char *name);
+
+static inline bool sect_empty(const Elf_Shdr *sect)
+{
+	return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
+}
+#else /* !CONFIG_KALLSYMS */
+static inline void init_build_id(struct module *mod, const struct load_info *info) { }
+static inline void layout_symtab(struct module *mod, struct load_info *info) { }
+static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
+#endif /* CONFIG_KALLSYMS */
kernel/module/kallsyms.c new file +502 -0

502 lines not on this page · raw message

kernel/module/main.c +6 -525

585 lines not on this page · raw message

Signature · 1 line
2.34.1
#16 lore raw

PATCH v9 10/14 module: kallsyms: Fix suspicious rcu usage

In reply to Aaron Tomlin

No functional change.

The purpose of this patch is to address the various Sparse warnings due to the incorrect dereference/or access of an __rcu pointer.

---
 kernel/module/kallsyms.c | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)
kernel/module/kallsyms.c +22 -12
index 1b0780e20aab..a3da0686a2a6 100644
@@ -171,14 +171,17 @@ void add_kallsyms(struct module *mod, const struct load_info *info)
 	Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
 
 	/* Set up to point into init section. */
-	mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
+	mod->kallsyms = (void __rcu *)mod->init_layout.base +
+		info->mod_kallsyms_init_off;
 
+	preempt_disable();
 	/* The following is safe since this pointer cannot change */
-	mod->kallsyms->symtab = (void *)symsec->sh_addr;
-	mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
+	rcu_dereference_sched(mod->kallsyms)->symtab = (void *)symsec->sh_addr;
+	rcu_dereference_sched(mod->kallsyms)->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
 	/* Make sure we get permanent strtab: don't use info->strtab. */
-	mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
-	mod->kallsyms->typetab = mod->init_layout.base + info->init_typeoffs;
+	rcu_dereference_sched(mod->kallsyms)->strtab =
+		(void *)info->sechdrs[info->index.str].sh_addr;
+	rcu_dereference_sched(mod->kallsyms)->typetab = mod->init_layout.base + info->init_typeoffs;
 
 	/*
 	 * Now populate the cut down core kallsyms for after init
@@ -187,20 +190,22 @@ void add_kallsyms(struct module *mod, const struct load_info *info)
 	mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs;
 	mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs;
 	mod->core_kallsyms.typetab = mod->core_layout.base + info->core_typeoffs;
-	src = mod->kallsyms->symtab;
-	for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
-		mod->kallsyms->typetab[i] = elf_type(src + i, info);
+	src = rcu_dereference_sched(mod->kallsyms)->symtab;
+	for (ndst = i = 0; i < rcu_dereference_sched(mod->kallsyms)->num_symtab; i++) {
+		rcu_dereference_sched(mod->kallsyms)->typetab[i] = elf_type(src + i, info);
 		if (i == 0 || is_livepatch_module(mod) ||
 		    is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum,
 				   info->index.pcpu)) {
 			mod->core_kallsyms.typetab[ndst] =
-			    mod->kallsyms->typetab[i];
+			    rcu_dereference_sched(mod->kallsyms)->typetab[i];
 			dst[ndst] = src[i];
 			dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
-			s += strscpy(s, &mod->kallsyms->strtab[src[i].st_name],
+			s += strscpy(s,
+				     &rcu_dereference_sched(mod->kallsyms)->strtab[src[i].st_name],
 				     KSYM_NAME_LEN) + 1;
 		}
 	}
+	preempt_enable();
 	mod->core_kallsyms.num_symtab = ndst;
 }
 
@@ -478,11 +483,16 @@ int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
 
 	mutex_lock(&module_mutex);
 	list_for_each_entry(mod, &modules, list) {
-		/* We hold module_mutex: no need for rcu_dereference_sched */
-		struct mod_kallsyms *kallsyms = mod->kallsyms;
+		struct mod_kallsyms *kallsyms;
 
 		if (mod->state == MODULE_STATE_UNFORMED)
 			continue;
+
+		/* Use rcu_dereference_sched() to remain compliant with the sparse tool */
+		preempt_disable();
+		kallsyms = rcu_dereference_sched(mod->kallsyms);
+		preempt_enable();
+
 		for (i = 0; i < kallsyms->num_symtab; i++) {
 			const Elf_Sym *sym = &kallsyms->symtab[i];
 
Signature · 1 line
2.34.1
reviewer
#17 lore raw

Re: [PATCH v9 10/14] module: kallsyms: Fix suspicious rcu usage

Quoted 9 lines · Aaron Tomlin

No functional change.

The purpose of this patch is to address the various Sparse warnings due to the incorrect dereference/or access of an __rcu pointer.

---
 kernel/module/kallsyms.c | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)

Petr,

Any concerns?

Unfortunately, I didn't make enough time to test.

Kind regards,

Signature · 1 line
Aaron Tomlin
#18 lore raw

Re: [PATCH v9 10/14] module: kallsyms: Fix suspicious rcu usage

Quoted 5 lines · Aaron Tomlin

Petr,

Any concerns?

Unfortunately, I didn't make enough time to test.

Petr,

$ sudo ./test-livepatch.sh
[sudo] password for atomlin:
TEST: basic function patching ... ok
TEST: multiple livepatches ... ok
TEST: atomic replace livepatch ... ok

- dmesg output excerpt

[  209.354909] ===== TEST: basic function patching =====
[  209.435376] % modprobe test_klp_livepatch
[  209.467226] test_klp_livepatch: tainting kernel with TAINT_LIVEPATCH
[  209.539749] livepatch: enabling patch 'test_klp_livepatch'
[  209.539785] livepatch: 'test_klp_livepatch': initializing patching transition
[  209.543671] livepatch: 'test_klp_livepatch': starting patching transition
[  210.716486] livepatch: 'test_klp_livepatch': completing patching transition
[  210.717405] livepatch: 'test_klp_livepatch': patching complete
[  210.766777] % echo 0 > /sys/kernel/livepatch/test_klp_livepatch/enabled
[  210.767446] livepatch: 'test_klp_livepatch': initializing unpatching transition
[  210.767548] livepatch: 'test_klp_livepatch': starting unpatching transition
[  211.739802] livepatch: 'test_klp_livepatch': completing unpatching transition
[  211.778846] livepatch: 'test_klp_livepatch': unpatching complete
[  211.862102] % rmmod test_klp_livepatch

Kind regards,

Signature · 1 line
Aaron Tomlin
maintainer
#19 lore raw

Re: [PATCH v9 10/14] module: kallsyms: Fix suspicious rcu usage

Quoted 9 lines · Aaron Tomlin
Quoted 5 lines · Aaron Tomlin

Petr,

Any concerns?

Unfortunately, I didn't make enough time to test.

Petr,

Quoted 7 lines
$ sudo ./test-livepatch.sh
[sudo] password for atomlin:
TEST: basic function patching ... ok
TEST: multiple livepatches ... ok
TEST: atomic replace livepatch ... ok

- dmesg output excerpt

Since you're going to send a v10 can you also add the livepatch to MAINTAINERS? :) Likewise if you see other files which can use help in maintenance review, let's go for that.

We're in no rush for the merge window as we'll be skiping the next merge window. So all this just needs to be thought well and tested well.

Luis

#20 lore raw

PATCH v9 11/14 module: Move procfs support into a separate file

In reply to Aaron Tomlin

No functional change.

This patch migrates code that allows one to generate a list of loaded/or linked modules via /proc when procfs support is enabled into kernel/module/procfs.c.

---
 kernel/module/Makefile   |   1 +
 kernel/module/internal.h |   1 +
 kernel/module/main.c     | 131 +-----------------------------------
 kernel/module/procfs.c   | 142 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 145 insertions(+), 130 deletions(-)
 create mode 100644 kernel/module/procfs.c
kernel/module/Makefile +1 -0
index 9901bed3ab5b..94296c98a67f 100644
@@ -15,3 +15,4 @@ obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o
 obj-$(CONFIG_STRICT_MODULE_RWX) += strict_rwx.o
 obj-$(CONFIG_DEBUG_KMEMLEAK) += debug_kmemleak.o
 obj-$(CONFIG_KALLSYMS) += kallsyms.o
+obj-$(CONFIG_PROC_FS) += procfs.o
kernel/module/internal.h +1 -0
index 44ca05b9eb8f..6af40c2d145f 100644
@@ -72,6 +72,7 @@ struct module *find_module_all(const char *name, size_t len, bool even_unformed)
 int cmp_name(const void *name, const void *sym);
 long module_get_offset(struct module *mod, unsigned int *size, Elf_Shdr *sechdr,
 		       unsigned int section);
+char *module_flags(struct module *mod, char *buf);
 
 static inline unsigned long kernel_symbol_value(const struct kernel_symbol *sym)
 {
kernel/module/main.c +1 -130

161 lines not on this page · raw message

kernel/module/procfs.c new file +142 -0

142 lines not on this page · raw message

Signature · 1 line
2.34.1
#21 lore raw

PATCH v9 12/14 module: Move sysfs support into a separate file

In reply to Aaron Tomlin

No functional change.

This patch migrates module sysfs support out of core code into kernel/module/sysfs.c. In addition simple code refactoring to make this possible.

---
 kernel/module/Makefile   |   1 +
 kernel/module/internal.h |  21 ++
 kernel/module/main.c     | 469 +--------------------------------------
 kernel/module/sysfs.c    | 436 ++++++++++++++++++++++++++++++++++++
 4 files changed, 461 insertions(+), 466 deletions(-)
 create mode 100644 kernel/module/sysfs.c
kernel/module/Makefile +1 -0
index 94296c98a67f..cf8dcdc6b55f 100644
@@ -16,3 +16,4 @@ obj-$(CONFIG_STRICT_MODULE_RWX) += strict_rwx.o
 obj-$(CONFIG_DEBUG_KMEMLEAK) += debug_kmemleak.o
 obj-$(CONFIG_KALLSYMS) += kallsyms.o
 obj-$(CONFIG_PROC_FS) += procfs.o
+obj-$(CONFIG_SYSFS) += sysfs.o
kernel/module/internal.h +21 -0
index 6af40c2d145f..62d749ef695e 100644
@@ -34,6 +34,9 @@
 extern struct mutex module_mutex;
 extern struct list_head modules;
 
+extern struct module_attribute *modinfo_attrs[];
+extern size_t modinfo_attrs_count;
+
 /* Provided by the linker */
 extern const struct kernel_symbol __start___ksymtab[];
 extern const struct kernel_symbol __stop___ksymtab[];
@@ -204,3 +207,21 @@ static inline void init_build_id(struct module *mod, const struct load_info *inf
 static inline void layout_symtab(struct module *mod, struct load_info *info) { }
 static inline void add_kallsyms(struct module *mod, const struct load_info *info) { }
 #endif /* CONFIG_KALLSYMS */
+
+#ifdef CONFIG_SYSFS
+int mod_sysfs_setup(struct module *mod, const struct load_info *info,
+		    struct kernel_param *kparam, unsigned int num_params);
+void mod_sysfs_teardown(struct module *mod);
+void init_param_lock(struct module *mod);
+#else /* !CONFIG_SYSFS */
+static inline int mod_sysfs_setup(struct module *mod,
+			   	  const struct load_info *info,
+			   	  struct kernel_param *kparam,
+			   	  unsigned int num_params)
+{
+	return 0;
+}
+
+static inline void mod_sysfs_teardown(struct module *mod) { }
+static inline void init_param_lock(struct module *mod) { }
+#endif /* CONFIG_SYSFS */
kernel/module/main.c +3 -466

494 lines not on this page · raw message

kernel/module/sysfs.c new file +436 -0

436 lines not on this page · raw message

Signature · 1 line
2.34.1
#22 lore raw

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

In reply to Aaron Tomlin

No functional change.

This patch migrates kdb_modules list to core kdb code since the list of added/or loaded modules is no longer private.

---
 kernel/debug/kdb/kdb_main.c    | 5 +++++
 kernel/debug/kdb/kdb_private.h | 4 ----
 kernel/module/main.c           | 4 ----
 3 files changed, 5 insertions(+), 8 deletions(-)
kernel/debug/kdb/kdb_main.c +5 -0
index 0852a537dad4..5369bf45c5d4 100644
@@ -59,6 +59,11 @@ EXPORT_SYMBOL(kdb_grepping_flag);
 int kdb_grep_leading;
 int kdb_grep_trailing;
 
+#ifdef CONFIG_MODULES
+extern struct list_head modules;
+static struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
+#endif /* CONFIG_MODULES */
+
 /*
  * Kernel debugger state flags
  */
kernel/debug/kdb/kdb_private.h +0 -4
index 0d2f9feea0a4..1f8c519a5f81 100644
@@ -226,10 +226,6 @@ extern void kdb_kbd_cleanup_state(void);
 #define kdb_kbd_cleanup_state()
 #endif /* ! CONFIG_KDB_KEYBOARD */
 
-#ifdef CONFIG_MODULES
-extern struct list_head *kdb_modules;
-#endif /* CONFIG_MODULES */
-
 extern char kdb_prompt_str[];
 
 #define	KDB_WORD_SIZE	((int)sizeof(unsigned long))
kernel/module/main.c +0 -4
index b8a59b5c3e3a..bcc4f7a82649 100644
@@ -108,10 +108,6 @@ static void mod_update_bounds(struct module *mod)
 		__mod_update_bounds(mod->init_layout.base, mod->init_layout.size);
 }
 
-#ifdef CONFIG_KGDB_KDB
-struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
-#endif /* CONFIG_KGDB_KDB */
-
 static void module_assert_mutex_or_preempt(void)
 {
 #ifdef CONFIG_LOCKDEP
Signature · 1 line
2.34.1
#23 lore raw

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

Quoted 19 lines · Aaron Tomlin

No functional change.

This patch migrates kdb_modules list to core kdb code since the list of added/or loaded modules is no longer private.

---
 kernel/debug/kdb/kdb_main.c    | 5 +++++
 kernel/debug/kdb/kdb_private.h | 4 ----
 kernel/module/main.c           | 4 ----
 3 files changed, 5 insertions(+), 8 deletions(-)
kernel/debug/kdb/kdb_main.c +0 -0
index 0852a537dad4..5369bf45c5d4 100644

kernel/debug/kdb/kdb_main.c EXPORT_SYMBOL(kdb_grepping_flag); · line 64

Quoted 6 lines
kernel/debug/kdb/kdb_main.c +3 -0
@@ -59,6 +59,11 @@ EXPORT_SYMBOL(kdb_grepping_flag);
 int kdb_grep_leading;
 int kdb_grep_trailing;
 
+#ifdef CONFIG_MODULES
+extern struct list_head modules;
+static struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */

If modules is no longer static then why do we kdb_modules at all? kdb_modules is used exactly once and it can now simply be replaced with &modules.

Daniel.

#24 lore raw

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

Quoted 23 lines · Daniel Thompson
Quoted 22 lines · Aaron Tomlin

No functional change.

This patch migrates kdb_modules list to core kdb code since the list of added/or loaded modules is no longer private.

---
 kernel/debug/kdb/kdb_main.c    | 5 +++++
 kernel/debug/kdb/kdb_private.h | 4 ----
 kernel/module/main.c           | 4 ----
 3 files changed, 5 insertions(+), 8 deletions(-)
kernel/debug/kdb/kdb_main.c +0 -0
index 0852a537dad4..5369bf45c5d4 100644
@@ -59,6 +59,11 @@ EXPORT_SYMBOL(kdb_grepping_flag);
 int kdb_grep_leading;
 int kdb_grep_trailing;
 
Quoted 2 lines

kernel/debug/kdb/kdb_main.c EXPORT_SYMBOL(kdb_grepping_flag); · line 63

Quoted 2 lines
kernel/debug/kdb/kdb_main.c +2 -0
+#ifdef CONFIG_MODULES
+extern struct list_head modules;

Actually thinking a bit harder and trying
git grep '#include .*[.][.]' kernel/ (which finds some prior art) I wonder if we even want the extern or whether
#include "../../module/internal.h" would be more robust.

Daniel.

Quoted 8 lines

kernel/debug/kdb/kdb_main.c EXPORT_SYMBOL(kdb_grepping_flag); · line 64

Quoted 1 line
kernel/debug/kdb/kdb_main.c +1 -0
+static struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */

If modules is no longer static then why do we kdb_modules at all? kdb_modules is used exactly once and it can now simply be replaced with &modules.

Daniel.

#25 lore raw

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

In reply to Daniel Thompson
Quoted 23 lines · Daniel Thompson
Quoted 22 lines · Aaron Tomlin

No functional change.

This patch migrates kdb_modules list to core kdb code since the list of added/or loaded modules is no longer private.

---
 kernel/debug/kdb/kdb_main.c    | 5 +++++
 kernel/debug/kdb/kdb_private.h | 4 ----
 kernel/module/main.c           | 4 ----
 3 files changed, 5 insertions(+), 8 deletions(-)
kernel/debug/kdb/kdb_main.c +0 -0
index 0852a537dad4..5369bf45c5d4 100644
@@ -59,6 +59,11 @@ EXPORT_SYMBOL(kdb_grepping_flag);
 int kdb_grep_leading;
 int kdb_grep_trailing;
 
Quoted 3 lines

kernel/debug/kdb/kdb_main.c EXPORT_SYMBOL(kdb_grepping_flag); · line 64

Quoted 3 lines
kernel/debug/kdb/kdb_main.c +3 -0
+#ifdef CONFIG_MODULES
+extern struct list_head modules;
+static struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */

Hi Daniel,

Quoted 3 lines

If modules is no longer static then why do we kdb_modules at all? kdb_modules is used exactly once and it can now simply be replaced with &modules.

In my opinion, I would prefer to avoid an explicit include of "internal.h" in kernel/module. By definition it should be reserved for internal use to kernel/module only. Please keep to the above logic.

Christophe, Luis,

Thoughts?

Kind regards,

Signature · 1 line
Aaron Tomlin
#26 lore raw

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

Le 02/03/2022 à 21:31, Aaron Tomlin a écrit :

Quoted 34 lines · Daniel Thompson
Quoted 23 lines · Aaron Tomlin
Quoted 22 lines

No functional change.

This patch migrates kdb_modules list to core kdb code since the list of added/or loaded modules is no longer private.

---
  kernel/debug/kdb/kdb_main.c    | 5 +++++
  kernel/debug/kdb/kdb_private.h | 4 ----
  kernel/module/main.c           | 4 ----
  3 files changed, 5 insertions(+), 8 deletions(-)
kernel/debug/kdb/kdb_main.c +0 -0
index 0852a537dad4..5369bf45c5d4 100644
@@ -59,6 +59,11 @@ EXPORT_SYMBOL(kdb_grepping_flag);
  int kdb_grep_leading;
  int kdb_grep_trailing;
 
Quoted 3 lines

kernel/debug/kdb/kdb_main.c EXPORT_SYMBOL(kdb_grepping_flag); · line 64

Quoted 3 lines
kernel/debug/kdb/kdb_main.c +3 -0
+#ifdef CONFIG_MODULES
+extern struct list_head modules;
+static struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */

Hi Daniel,

Quoted 3 lines

If modules is no longer static then why do we kdb_modules at all? kdb_modules is used exactly once and it can now simply be replaced with &modules.

Quoted 8 lines

In my opinion, I would prefer to avoid an explicit include of "internal.h" in kernel/module. By definition it should be reserved for internal use to kernel/module only. Please keep to the above logic.

Christophe, Luis,

Thoughts?

Do we really want to hide the 'struct list_head modules' from external world ?

Otherwise we could declare it in include/linux/module.h ?

Christophe

maintainer
#27 lore raw

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

Quoted 43 lines · Christophe Leroy

Le 02/03/2022 à 21:31, Aaron Tomlin a écrit :

Quoted 40 lines · Daniel Thompson
Quoted 23 lines · Aaron Tomlin
Quoted 22 lines

No functional change.

This patch migrates kdb_modules list to core kdb code since the list of added/or loaded modules is no longer private.

---
  kernel/debug/kdb/kdb_main.c    | 5 +++++
  kernel/debug/kdb/kdb_private.h | 4 ----
  kernel/module/main.c           | 4 ----
  3 files changed, 5 insertions(+), 8 deletions(-)
kernel/debug/kdb/kdb_main.c +0 -0
index 0852a537dad4..5369bf45c5d4 100644
@@ -59,6 +59,11 @@ EXPORT_SYMBOL(kdb_grepping_flag);
  int kdb_grep_leading;
  int kdb_grep_trailing;
 
Quoted 3 lines

kernel/debug/kdb/kdb_main.c EXPORT_SYMBOL(kdb_grepping_flag); · line 64

Quoted 3 lines
kernel/debug/kdb/kdb_main.c +3 -0
+#ifdef CONFIG_MODULES
+extern struct list_head modules;
+static struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */

Hi Daniel,

Quoted 3 lines

If modules is no longer static then why do we kdb_modules at all? kdb_modules is used exactly once and it can now simply be replaced with &modules.

In my opinion, I would prefer to avoid an explicit include of "internal.h" in kernel/module. By definition it should be reserved for internal use to kernel/module only. Please keep to the above logic.

Christophe, Luis,

Quoted 7 lines
Quoted 2 lines

Thoughts?

Do we really want to hide the 'struct list_head modules' from external world ?

Otherwise we could declare it in include/linux/module.h ?

Since we are doing this to help with the cleaning this crap up the natural thing to do is have the code be a helper which only built-in code can use, so writing a helper starting with list_for_each_entry() which prints the modules out. I'm surprised we have no other users of this. There is nothing kdb specific about the functionality in that code. So it should just be moved.

Exposing just the list_head was a bad idea to begin with. So let's do away with that. This can be a preamble change to the series.

Luis

#28 lore raw

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

Quoted 7 lines · Luis Chamberlain

Since we are doing this to help with the cleaning this crap up the natural thing to do is have the code be a helper which only built-in code can use, so writing a helper starting with list_for_each_entry() which prints the modules out. I'm surprised we have no other users of this. There is nothing kdb specific about the functionality in that code. So it should just be moved.

Hi Luis,

Good point, albeit is it really worth it since the only external user is kernel/debug/kdb/ code?

Kind regards,

Signature · 1 line
Aaron Tomlin
maintainer
#29 lore raw

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

Quoted 9 lines · Aaron Tomlin
Quoted 7 lines · Luis Chamberlain

Since we are doing this to help with the cleaning this crap up the natural thing to do is have the code be a helper which only built-in code can use, so writing a helper starting with list_for_each_entry() which prints the modules out. I'm surprised we have no other users of this. There is nothing kdb specific about the functionality in that code. So it should just be moved.

Quoted 4 lines

Hi Luis,

Good point, albeit is it really worth it since the only external user is kernel/debug/kdb/ code?

Yes, no need to be exposing that list out anywhere else. And if the list is needed better to have a helper for the users.

Luis

#30 lore raw

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

In reply to Christophe Leroy
Quoted 4 lines · Christophe Leroy

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.

#31 lore raw

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

Quoted 9 lines · Christoph Hellwig
Quoted 4 lines · Christophe Leroy

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.

kernel/module.c +9 -1
index 84a9141a5e159..ab43ee23cdba0 100644
@@ -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)

#32 lore raw

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

Quoted 11 lines · Daniel Thompson

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.

Displaying seq_files sounds nice to have, but in the short term I'm just thinking of something like this:

include/linux/kdb.h +1 -0
index ea0f5e580fac2..07dfb6a20a1c4 100644
@@ -222,5 +222,6 @@ enum {
 
 extern int kdbgetintenv(const char *, int *);
 extern int kdb_set(int, const char **);
+int kdb_lsmod(int argc, const char **argv);
 
 #endif	/* !_KDB_H */
kernel/debug/kdb/kdb_main.c +0 -48

54 lines not on this page · raw message

kernel/debug/kdb/kdb_private.h +0 -4
index 0d2f9feea0a46..1f8c519a5f81c 100644
@@ -226,10 +226,6 @@ extern void kdb_kbd_cleanup_state(void);
 #define kdb_kbd_cleanup_state()
 #endif /* ! CONFIG_KDB_KEYBOARD */
 
-#ifdef CONFIG_MODULES
-extern struct list_head *kdb_modules;
-#endif /* CONFIG_MODULES */
-
 extern char kdb_prompt_str[];
 
 #define	KDB_WORD_SIZE	((int)sizeof(unsigned long))
kernel/module.c +43 -4

62 lines not on this page · raw message

#33 lore raw

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

Le 03/03/2022 à 18:54, Christoph Hellwig a écrit :

Quoted 13 lines · Daniel Thompson
Quoted 11 lines

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.

Quoted 2 lines

Displaying seq_files sounds nice to have, but in the short term I'm just thinking of something like this:

Well .... The idea at the first place was to get rid of the #ifdef CONFIG_KGDB_KDB in modules.

Here you propose it the other way round. Why not, in that case that would mean a dedicated file in kernel/module/ as part of the series
https://patchwork.kernel.org/project/linux-modules/list/?series=618917&state=*

Christophe

#34 lore raw

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

Quoted 6 lines · Christophe Leroy

Well .... The idea at the first place was to get rid of the #ifdef CONFIG_KGDB_KDB in modules.

Here you propose it the other way round. Why not, in that case that would mean a dedicated file in kernel/module/ as part of the series
https://patchwork.kernel.org/project/linux-modules/list/?series=618917&state=*

With the series you can of course move it to a new kernel/module/kdb.c. But I don't have a tree with the series applied at hand and just want to show how kdb_lsmod can live outside of kernel/debug easily.

maintainer
#35 lore raw

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

In reply to Christoph Hellwig
Quoted 21 lines · Christoph Hellwig
Quoted 11 lines · Daniel Thompson

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.

Displaying seq_files sounds nice to have, but in the short term I'm just thinking of something like this:

include/linux/kdb.h +0 -0
index ea0f5e580fac2..07dfb6a20a1c4 100644

include/linux/kdb.h enum { · line 225

Quoted 4 lines
include/linux/kdb.h +1 -0
@@ -222,5 +222,6 @@ enum {
 
 extern int kdbgetintenv(const char *, int *);
 extern int kdb_set(int, const char **);
+int kdb_lsmod(int argc, const char **argv);

Yes exactly.

Quoted 99 lines
include/linux/kdb.h +0 -0
 #endif	/* !_KDB_H */
kernel/debug/kdb/kdb_main.c +0 -48
index 0852a537dad4c..292a407118a4f 100644
@@ -2004,54 +2004,6 @@ static int kdb_ef(int argc, const char **argv)
 	return 0;
 }
 
-#if defined(CONFIG_MODULES)
-/*
- * kdb_lsmod - This function implements the 'lsmod' command.  Lists
- *	currently loaded kernel modules.
- *	Mostly taken from userland lsmod.
- */
-static int kdb_lsmod(int argc, const char **argv)
-{
-	struct module *mod;
-
-	if (argc != 0)
-		return KDB_ARGCOUNT;
-
-	kdb_printf("Module                  Size  modstruct     Used by\n");
-	list_for_each_entry(mod, kdb_modules, list) {
-		if (mod->state == MODULE_STATE_UNFORMED)
-			continue;
-
-		kdb_printf("%-20s%8u  0x%px ", mod->name,
-			   mod->core_layout.size, (void *)mod);
-#ifdef CONFIG_MODULE_UNLOAD
-		kdb_printf("%4d ", module_refcount(mod));
-#endif
-		if (mod->state == MODULE_STATE_GOING)
-			kdb_printf(" (Unloading)");
-		else if (mod->state == MODULE_STATE_COMING)
-			kdb_printf(" (Loading)");
-		else
-			kdb_printf(" (Live)");
-		kdb_printf(" 0x%px", mod->core_layout.base);
-
-#ifdef CONFIG_MODULE_UNLOAD
-		{
-			struct module_use *use;
-			kdb_printf(" [ ");
-			list_for_each_entry(use, &mod->source_list,
-					    source_list)
-				kdb_printf("%s ", use->target->name);
-			kdb_printf("]\n");
-		}
-#endif
-	}
-
-	return 0;
-}
-
-#endif	/* CONFIG_MODULES */
-
 /*
  * kdb_env - This function implements the 'env' command.  Display the
  *	current environment variables.
kernel/debug/kdb/kdb_private.h +0 -4
index 0d2f9feea0a46..1f8c519a5f81c 100644
@@ -226,10 +226,6 @@ extern void kdb_kbd_cleanup_state(void);
 #define kdb_kbd_cleanup_state()
 #endif /* ! CONFIG_KDB_KEYBOARD */
 
-#ifdef CONFIG_MODULES
-extern struct list_head *kdb_modules;
-#endif /* CONFIG_MODULES */
-
 extern char kdb_prompt_str[];
 
 #define	KDB_WORD_SIZE	((int)sizeof(unsigned long))
kernel/module.c +1 -4
index 6cea788fd965c..754ec20aab4f1 100644
@@ -57,6 +57,7 @@
 #include <linux/bsearch.h>
 #include <linux/dynamic_debug.h>
 #include <linux/audit.h>
+#include <linux/kdb.h>
 #include <uapi/linux/module.h>
 #include "module-internal.h"
 
@@ -252,10 +253,6 @@ static void mod_update_bounds(struct module *mod)
 		__mod_update_bounds(mod->init_layout.base, mod->init_layout.size);
 }
 
-#ifdef CONFIG_KGDB_KDB
-struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
-#endif /* CONFIG_KGDB_KDB */
-
 static void module_assert_mutex_or_preempt(void)
 {
 #ifdef CONFIG_LOCKDEP

kernel/module.c void module_layout(struct module *mod, · line 4809

Quoted 5 lines
kernel/module.c +2 -0
@@ -4808,3 +4805,45 @@ void module_layout(struct module *mod,
 }
 EXPORT_SYMBOL(module_layout);
 #endif
+
+#ifdef CONFIG_KGDB_KDB

Yes! And then when we move all this crap to its own files on kernel/module/Makefile:

obj-$(CONFIG_KGDB_KDB) += kdb.o

kernel/module.c void module_layout(struct module *mod, · line 4817

Quoted 8 lines
kernel/module.c +8 -0
+int kdb_lsmod(int argc, const char **argv)
+{
+	struct module *mod;
+
+	if (argc != 0)
+		return KDB_ARGCOUNT;
+
+	kdb_printf("Module                  Size  modstruct     Used by\n");

Indeed, we need to do it this way because kdb_printf() which emits messages directly to I/O drivers, bypassing the kernel log. Perhaps this info should be added to the top of kernel/module/kdb.c

kernel/module.c void module_layout(struct module *mod, · line 4826

Quoted 9 lines
kernel/module.c +9 -0
+	list_for_each_entry(mod, &modules, list) {
+		if (mod->state == MODULE_STATE_UNFORMED)
+			continue;
+
+		kdb_printf("%-20s%8u  0x%px ", mod->name,
+			   mod->core_layout.size, (void *)mod);
+#ifdef CONFIG_MODULE_UNLOAD
+		kdb_printf("%4d ", module_refcount(mod));
+#endif

Yes and later this can be if IS_ENABLED(CONFIG_MODULE_UNLOAD)

kernel/module.c void module_layout(struct module *mod, · line 4835

Quoted 9 lines
kernel/module.c +9 -0
+		if (mod->state == MODULE_STATE_GOING)
+			kdb_printf(" (Unloading)");
+		else if (mod->state == MODULE_STATE_COMING)
+			kdb_printf(" (Loading)");
+		else
+			kdb_printf(" (Live)");
+		kdb_printf(" 0x%px", mod->core_layout.base);
+
+#ifdef CONFIG_MODULE_UNLOAD

and if IS_ENABLED(CONFIG_MODULE_UNLOAD) later

Quoted 4 lines
kernel/module.c +4 -0
+		{
+			struct module_use *use;
+			kdb_printf(" [ ");
+			list_for_each_entry(use, &mod->source_list,

kernel/module.c void module_layout(struct module *mod, · line 4849

Quoted 10 lines
kernel/module.c +10 -0
+					    source_list)
+				kdb_printf("%s ", use->target->name);
+			kdb_printf("]\n");
+		}
+#endif
+	}
+
+	return 0;
+}
+#endif	/* CONFIG_KGDB_KDB */

Luis

#36 lore raw

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

In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig
Quoted 4 lines · Christophe Leroy

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.

Hi Christoph,

This is a great idea. I'll do this instead.

Kind regards,

Signature · 1 line
Aaron Tomlin
#37 lore raw

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

Quoted 5 lines · Aaron Tomlin
Quoted 4 lines · Christoph Hellwig
Quoted 3 lines · Christophe Leroy

Do we really want to hide the 'struct list_head modules' from external world ?

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

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.

Hi Christoph,

This is a great idea. I'll do this instead.

Adding this as a new file is fine for me.

There were proposed changes to this function this kernel cycle (CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC) which I Acked already. However it looks like all involved with that are already particpating in this thread so I assume an merge issues with that are already in hand.

Aaron: Are you OK to add the new kdb file to the "KGDB / KDB /debug_core" file list in MAINTAINERS? Mostly I'd expect to just Ack changes and move on... but I'd like to be in the loop.

Daniel.

#38 lore raw

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

Quoted 3 lines · Daniel Thompson

Aaron: Are you OK to add the new kdb file to the "KGDB / KDB /debug_core" file list in MAINTAINERS? Mostly I'd expect to just Ack changes and move on... but I'd like to be in the loop.

Hi Daniel,

Sure - no problem.

Kind regards,

Signature · 1 line
Aaron Tomlin
#39 lore raw

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

In reply to Aaron Tomlin
Quoted 30 lines · Aaron Tomlin
Quoted 23 lines · Daniel Thompson
Quoted 22 lines · Aaron Tomlin

No functional change.

This patch migrates kdb_modules list to core kdb code since the list of added/or loaded modules is no longer private.

---
 kernel/debug/kdb/kdb_main.c    | 5 +++++
 kernel/debug/kdb/kdb_private.h | 4 ----
 kernel/module/main.c           | 4 ----
 3 files changed, 5 insertions(+), 8 deletions(-)
kernel/debug/kdb/kdb_main.c +0 -0
index 0852a537dad4..5369bf45c5d4 100644
@@ -59,6 +59,11 @@ EXPORT_SYMBOL(kdb_grepping_flag);
 int kdb_grep_leading;
 int kdb_grep_trailing;
 
Quoted 3 lines

kernel/debug/kdb/kdb_main.c EXPORT_SYMBOL(kdb_grepping_flag); · line 64

Quoted 3 lines
kernel/debug/kdb/kdb_main.c +3 -0
+#ifdef CONFIG_MODULES
+extern struct list_head modules;
+static struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */

Hi Daniel,

Quoted 7 lines
Quoted 3 lines

If modules is no longer static then why do we kdb_modules at all? kdb_modules is used exactly once and it can now simply be replaced with &modules.

In my opinion, I would prefer to avoid an explicit include of "internal.h" in kernel/module. By definition it should be reserved for internal use to kernel/module only. Please keep to the above logic.

Are you sure you are replying the right e-mail here? The quoted text doesn't propose what you are replying to (although my other e-mail did).

To be clear there are two bits of feedback here and I don't think "please keep to the above logic" is a sufficient answer.

If I remove the proposal on how to fix the second issue get get:

1. Remove kdb_modules because it is pointless (kdb_main.c can just use &modules directly lower down the file).
2. Having an extern in a kdb C file that duplicatively declares something from an alien header file is gross ;-) .

Daniel.

#40 lore raw

PATCH v9 14/14 module: Move version support into a separate file

In reply to Aaron Tomlin

No functional change.

This patch migrates module version support out of core code into kernel/module/version.c. In addition simple code refactoring to make this possible.

---
 kernel/module/Makefile   |   1 +
 kernel/module/internal.h |  48 ++++++++++++
 kernel/module/main.c     | 156 ++-------------------------------------
 kernel/module/version.c  | 109 +++++++++++++++++++++++++++
 4 files changed, 166 insertions(+), 148 deletions(-)
 create mode 100644 kernel/module/version.c
kernel/module/Makefile +1 -0
index cf8dcdc6b55f..a46e6361017f 100644
@@ -17,3 +17,4 @@ obj-$(CONFIG_DEBUG_KMEMLEAK) += debug_kmemleak.o
 obj-$(CONFIG_KALLSYMS) += kallsyms.o
 obj-$(CONFIG_PROC_FS) += procfs.o
 obj-$(CONFIG_SYSFS) += sysfs.o
+obj-$(CONFIG_MODVERSIONS) += version.o
kernel/module/internal.h +48 -0

58 lines not on this page · raw message

kernel/module/main.c +8 -148

189 lines not on this page · raw message

kernel/module/version.c new file +109 -0

109 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#41 lore raw
In reply to Aaron Tomlin
Quoted 8 lines · Aaron Tomlin

Hi Luis,

As per your suggestion [1], this is an attempt to refactor and split optional code out of core module support code into separate components. This version is based on Linus' commit 7993e65fdd0f ("Merge tag 'mtd/fixes-for-5.17-rc5' of
git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux").

Quoted 4 lines

Petr,

I decided to use preempt_disable() instead to remain consistent with the rest of the file. Unfortunately, I did not make time to boot test etc.

Aaron, thanks so much for doing this!

Since no boot tests are performed yet, I just pushed this to modules-testing for now, after we get some boot test results I'll push to modules-next.

We should run kmod tests as well.

Luis

#42 lore raw

Hi Luis,

Le 01/03/2022 à 01:21, Luis Chamberlain a écrit :

Quoted 14 lines · Aaron Tomlin
Quoted 8 lines

Hi Luis,

As per your suggestion [1], this is an attempt to refactor and split optional code out of core module support code into separate components. This version is based on Linus' commit 7993e65fdd0f ("Merge tag 'mtd/fixes-for-5.17-rc5' of
git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux").

Quoted 4 lines

Petr,

I decided to use preempt_disable() instead to remain consistent with the rest of the file. Unfortunately, I did not make time to boot test etc.

Quoted 7 lines

Aaron, thanks so much for doing this!

Since no boot tests are performed yet, I just pushed this to modules-testing for now, after we get some boot test results I'll push to modules-next.

We should run kmod tests as well.

My Kconfig patch and my two series still cleanly apply on v9. I don't need to rebase/resend them.

boot test is OK for me with Aaron's v9.

Christophe

maintainer
#43 lore raw
Quoted 26 lines · Christophe Leroy

Hi Luis,

Le 01/03/2022 à 01:21, Luis Chamberlain a écrit :

Quoted 14 lines · Aaron Tomlin
Quoted 8 lines

Hi Luis,

As per your suggestion [1], this is an attempt to refactor and split optional code out of core module support code into separate components. This version is based on Linus' commit 7993e65fdd0f ("Merge tag 'mtd/fixes-for-5.17-rc5' of
git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux").

Quoted 4 lines

Petr,

I decided to use preempt_disable() instead to remain consistent with the rest of the file. Unfortunately, I did not make time to boot test etc.

Quoted 7 lines

Aaron, thanks so much for doing this!

Since no boot tests are performed yet, I just pushed this to modules-testing for now, after we get some boot test results I'll push to modules-next.

We should run kmod tests as well.

Quoted 5 lines

My Kconfig patch and my two series still cleanly apply on v9. I don't need to rebase/resend them.

boot test is OK for me with Aaron's v9.

Great thanks yes, I intend on applying them as soon as I get some test confirmations on modules-testing. It's all about testing now.

Luis

#44 lore raw
In reply to Luis Chamberlain

Le 01/03/2022 à 01:21, Luis Chamberlain a écrit :

Quoted 3 lines

We should run kmod tests as well.

I tried to build kmod tests, but I get a crazy result:

$ ./configure --host=ppc-linux --prefix=/usr/local
$ make
$ cd testsuite
$ make
$ file test-list
test-list: ELF 32-bit MSB executable, PowerPC or cisco 4500, version 1 
(SYSV), dynamically linked, interpreter /lib/ld.so.1, for GNU/Linux 
3.2.0, with debug_info, not stripped
$ file module-playground/mod-loop-a.ko
module-playground/mod-loop-a.ko: ELF 64-bit LSB relocatable, x86-64, 
version 1 (SYSV), 
BuildID[sha1]=d46956a4fd36d8d3467806c31831c81217a573f5, with debug_info, 
not stripped

How do I get it to crossbuild proper PowerPC module ?

Thanks
Christophe

maintainer
#45 lore raw
Quoted 31 lines · Christophe Leroy

Le 01/03/2022 à 01:21, Luis Chamberlain a écrit :

Quoted 3 lines

We should run kmod tests as well.

I tried to build kmod tests, but I get a crazy result:

$ ./configure --host=ppc-linux --prefix=/usr/local
$ make
$ cd testsuite
$ make
$ file test-list
test-list: ELF 32-bit MSB executable, PowerPC or cisco 4500, version 1 
(SYSV), dynamically linked, interpreter /lib/ld.so.1, for GNU/Linux 
3.2.0, with debug_info, not stripped
$ file module-playground/mod-loop-a.ko
module-playground/mod-loop-a.ko: ELF 64-bit LSB relocatable, x86-64, 
version 1 (SYSV), 
BuildID[sha1]=d46956a4fd36d8d3467806c31831c81217a573f5, with debug_info, 
not stripped
Quoted 1 line

How do I get it to crossbuild proper PowerPC module ?

Lucas?

Luis

#46 lore raw
Quoted 32 lines · Luis Chamberlain
Quoted 31 lines · Christophe Leroy

Le 01/03/2022 à 01:21, Luis Chamberlain a écrit :

Quoted 3 lines

We should run kmod tests as well.

I tried to build kmod tests, but I get a crazy result:

$ ./configure --host=ppc-linux --prefix=/usr/local
$ make
$ cd testsuite
$ make
$ file test-list
test-list: ELF 32-bit MSB executable, PowerPC or cisco 4500, version 1
(SYSV), dynamically linked, interpreter /lib/ld.so.1, for GNU/Linux
3.2.0, with debug_info, not stripped
$ file module-playground/mod-loop-a.ko
module-playground/mod-loop-a.ko: ELF 64-bit LSB relocatable, x86-64,
version 1 (SYSV),
BuildID[sha1]=d46956a4fd36d8d3467806c31831c81217a573f5, with debug_info,
not stripped
Quoted 1 line
Quoted 1 line

How do I get it to crossbuild proper PowerPC module ?

do I understand correctly that you want to crossbuild kmod + kernel modules to do your test? why?

If you really need it, then beware we just chainload the kernel build for the out-of-tree modules when compiling the test modules. Something like this should work:

make V=1 KDIR=$HOME/p/gfx-internal/linux-arm64/ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- check

But running the cross built binaries is probably not what you want?

Another thing is that unless you are patching kmod binaries or libkmod, the testsuite won't test much. kmod's testsuite don't test anything on the kernel side... the kernel part is mocked by the testsuite itself. Adding proper integration with the kernel part is possible, but not something ready.

Lucas De Marchi

Quoted 4 lines

Lucas?

Luis

#47 lore raw

Le 01/03/2022 à 18:15, Lucas De Marchi a écrit :

Quoted 33 lines · Luis Chamberlain
Quoted 32 lines · Christophe Leroy
Quoted 31 lines

Le 01/03/2022 à 01:21, Luis Chamberlain a écrit :

Quoted 3 lines

We should run kmod tests as well.

I tried to build kmod tests, but I get a crazy result:

$ ./configure --host=ppc-linux --prefix=/usr/local
$ make
$ cd testsuite
$ make
$ file test-list
test-list: ELF 32-bit MSB executable, PowerPC or cisco 4500, version 1
(SYSV), dynamically linked, interpreter /lib/ld.so.1, for GNU/Linux
3.2.0, with debug_info, not stripped
$ file module-playground/mod-loop-a.ko
module-playground/mod-loop-a.ko: ELF 64-bit LSB relocatable, x86-64,
version 1 (SYSV),
BuildID[sha1]=d46956a4fd36d8d3467806c31831c81217a573f5, with debug_info,
not stripped
Quoted 4 lines
Quoted 1 line
Quoted 1 line

How do I get it to crossbuild proper PowerPC module ?

do I understand correctly that you want to crossbuild kmod + kernel modules to do your test? why?

Yes, I want to build the tests on my PC and run them on a powerpc board.

Quoted 9 lines

If you really need it, then beware we just chainload the kernel build for the out-of-tree modules when compiling the test modules. Something like this should work:

    make V=1 KDIR=$HOME/p/gfx-internal/linux-arm64/ ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu- check

But running the cross built binaries is probably not what you want?

I want to run them on the board.

Quoted 6 lines

Another thing is that unless you are patching kmod binaries or libkmod, the testsuite won't test much. kmod's testsuite don't test anything on the kernel side... the kernel part is mocked by the testsuite itself. Adding proper integration with the kernel part is possible, but not something ready.

Ah, I see.

I made non trivial modification on kernel modules handling (see series at
https://patchwork.kernel.org/project/linux-modules/cover/[email protected]/)
and Luis suggested to run kmod test suite to perform regression testing.

Christophe