Skip to contentSkip to conversation

module: replace module_layout with module_memory

PATCH Superseded Song Liu 4 participants 9 messages Jan 26, 2023 – Jan 30, 2023

Subsystems (11)
Arm (32-bit) x86 PowerPC MIPS +7 more subsystems

v3 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Song Liu

author
#0 lore raw

PATCH v3 1/1 module: replace module_layout with module_memory

module_layout manages different types of memory (text, data, rodata, etc.) in one allocation, which is problematic for some reasons:

1. It is hard to enable CONFIG_STRICT_MODULE_RWX.
2. It is hard to use huge pages in modules (and not break strict rwx).
3. Many archs uses module_layout for arch-specific data, but it is not obvious how these data are used (are they RO, RX, or RW?)

Improve the scenario by replacing 2 (or 3) module_layout per module with up to 7 module_memory per module:

MOD_MEM_TYPE_TEXT,
MOD_MEM_TYPE_DATA,
MOD_MEM_TYPE_RODATA,
MOD_MEM_TYPE_RO_AFTER_INIT,
MOD_MEM_TYPE_INIT_TEXT,
MOD_MEM_TYPE_INIT_DATA,
MOD_MEM_TYPE_INIT_RODATA,

and allocating them separately. This adds slightly more entries to mod_tree (from up to 3 entries per module, to up to 7 entries per module). However, this at most adds a small constant overhead to __module_address(), which is expected to be fast.

Various archs use module_layout for different data. These data are put into different module_memory based on their location in module_layout. IOW, data that used to go with text is allocated with MOD_MEM_TYPE_TEXT; data that used to go with data is allocated with MOD_MEM_TYPE_DATA, etc.

module_memory simplifies quite some of the module code. For example, ARCH_WANTS_MODULES_DATA_IN_VMALLOC is a lot cleaner, as it just uses a different allocator for the data. kernel/module/strict_rwx.c is also much cleaner with module_memory.

Cc Luis Chamberlain <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Guenter Roeck <[email protected]>

---

This is the preparation work for the type aware module_alloc() discussed in [1]. While this work is not covered much in the discussion, it is a critical step of the effort.

As this part grows pretty big (~1000 lines, + and -), I would like get some feedback on it, so that I know it is on the right track.

Please share your comments. Thanks!

Test coverage: Tested on x86_64.
Build tested by kernel test bot in [2]. The only regression in [2] was a typo in parisc, which is also fixed.

[1] https://lore.kernel.org/linux-mm/[email protected]/T/#u
[2] https://lore.kernel.org/linux-raid/63b8827e.clJQX2wg+I+tiX7m%[email protected]/T/#u

Changes v1 => v2:
1. Add data_addr_[min|max] for CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC case.

Changes v2 => v3:
1. Fix and remove the last use of INIT_OFFSET_MASK.
2. Add more information in the commit log. (Luis Chamberlain)
3. Rebase and fix issues in x86/calltrunks.
4. Minor cleanup.

---
 arch/alpha/kernel/module.c      |   3 +-
 arch/arc/kernel/unwind.c        |   9 +-
 arch/arm/kernel/module-plts.c   |   5 +-
 arch/arm64/kernel/module-plts.c |   5 +-
 arch/ia64/kernel/module.c       |  31 ++-
 arch/mips/kernel/vpe.c          |  11 +-
 arch/parisc/kernel/module.c     |  53 +++--
 arch/powerpc/kernel/module_32.c |  10 +-
 arch/s390/kernel/module.c       |  30 +--
 arch/x86/kernel/callthunks.c    |   5 +-
 arch/x86/kernel/module.c        |   4 +-
 include/linux/module.h          |  65 +++---
 kernel/module/internal.h        |  47 ++--
 kernel/module/kallsyms.c        |  60 ++---
 kernel/module/kdb.c             |  17 +-
 kernel/module/main.c            | 378 +++++++++++++++++++-------------
 kernel/module/procfs.c          |  17 +-
 kernel/module/strict_rwx.c      | 113 +++-------
 kernel/module/tree_lookup.c     |  43 ++--
 19 files changed, 497 insertions(+), 409 deletions(-)
arch/alpha/kernel/module.c +2 -1
index 5b60c248de9e..905af4e8d835 100644
@@ -148,7 +148,8 @@ apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab,
 
 	/* The small sections were sorted to the end of the segment.
 	   The following should definitely cover them.  */
-	gp = (u64)me->core_layout.base + me->core_layout.size - 0x8000;
+	gp = (u64)me->mod_mem[MOD_MEM_TYPE_DATA].base +
+		me->mod_mem[MOD_MEM_TYPE_DATA].size - 0x8000;
 	got = sechdrs[me->arch.gotsecindex].sh_addr;
 
 	for (i = 0; i < n; i++) {
arch/arc/kernel/unwind.c +7 -2
index 200270a94558..2c55f6215f79 100644
@@ -369,6 +369,8 @@ void *unwind_add_table(struct module *module, const void *table_start,
 		       unsigned long table_size)
 {
 	struct unwind_table *table;
+	struct module_memory *mod_mem_core_text;
+	struct module_memory *mod_mem_init_text;
 
 	if (table_size <= 0)
 		return NULL;
@@ -377,9 +379,12 @@ void *unwind_add_table(struct module *module, const void *table_start,
 	if (!table)
 		return NULL;
 
+	mod_mem_core_text = &module->mod_mem[MOD_MEM_TYPE_TEXT];
+	mod_mem_init_text = &module->mod_mem[MOD_MEM_TYPE_INIT_TEXT];
+
 	init_unwind_table(table, module->name,
-			  module->core_layout.base, module->core_layout.size,
-			  module->init_layout.base, module->init_layout.size,
+			  mod_mem_core_text->base, mod_mem_core_text->size,
+			  mod_mem_init_text->base, mod_mem_init_text->size,
 			  table_start, table_size,
 			  NULL, 0);
 
arch/arm/kernel/module-plts.c +4 -1
index af7c322ebed6..2fa4d85bf1cc 100644
@@ -30,7 +30,10 @@ static const u32 fixed_plts[] = {
 
 static bool in_init(const struct module *mod, unsigned long loc)
 {
-	return loc - (u32)mod->init_layout.base < mod->init_layout.size;
+	const struct module_memory *mod_mem;
+
+	mod_mem = &mod->mod_mem[MOD_MEM_TYPE_INIT_TEXT];
+	return loc - (u32)mod_mem->base < mod_mem->size;
 }
 
 static void prealloc_fixed(struct mod_plt_sec *pltsec, struct plt_entries *plt)
arch/arm64/kernel/module-plts.c +4 -1
index 5a0a8f552a61..2f1ca5e2294b 100644
@@ -67,7 +67,10 @@ static bool plt_entries_equal(const struct plt_entry *a,
 
 static bool in_init(const struct module *mod, void *loc)
 {
-	return (u64)loc - (u64)mod->init_layout.base < mod->init_layout.size;
+	const struct module_memory *mod_mem;
+
+	mod_mem = &mod->mod_mem[MOD_MEM_TYPE_INIT_TEXT];
+	return (u64)loc - (u64)mod_mem->base < mod_mem->size;
 }
 
 u64 module_emit_plt_entry(struct module *mod, Elf64_Shdr *sechdrs,
arch/ia64/kernel/module.c +21 -10
index 8f62cf97f691..966a8997424d 100644
@@ -485,19 +485,25 @@ module_frob_arch_sections (Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, char *secstrings,
 	return 0;
 }
 
-static inline int
+static inline bool
 in_init (const struct module *mod, uint64_t addr)
 {
-	return addr - (uint64_t) mod->init_layout.base < mod->init_layout.size;
+	const struct module_memory *mod_mem;
+
+	mod_mem = &mod->mod_mem[MOD_MEM_TYPE_INIT_TEXT];
+	return addr - (uint64_t)mod_mem->base < mod_mem->size;
 }
 
-static inline int
+static inline bool
 in_core (const struct module *mod, uint64_t addr)
 {
-	return addr - (uint64_t) mod->core_layout.base < mod->core_layout.size;
+	const struct module_memory *mod_mem;
+
+	mod_mem = &mod->mod_mem[MOD_MEM_TYPE_TEXT];
+	return addr - (uint64_t)mod_mem->base < mod_mem->size;
 }
 
-static inline int
+static inline bool
 is_internal (const struct module *mod, uint64_t value)
 {
 	return in_init(mod, value) || in_core(mod, value);
@@ -677,7 +683,9 @@ do_reloc (struct module *mod, uint8_t r_type, Elf64_Sym *sym, uint64_t addend,
 		break;
 
 	      case RV_BDREL:
-		val -= (uint64_t) (in_init(mod, val) ? mod->init_layout.base : mod->core_layout.base);
+		val -= (uint64_t) (in_init(mod, val) ?
+				   mod->mod_mem[MOD_MEM_TYPE_INIT_TEXT].base :
+				   mod->mod_mem[MOD_MEM_TYPE_TEXT].base);
 		break;
 
 	      case RV_LTV:
@@ -812,15 +820,18 @@ apply_relocate_add (Elf64_Shdr *sechdrs, const char *strtab, unsigned int symind
 		 *     addresses have been selected...
 		 */
 		uint64_t gp;
-		if (mod->core_layout.size > MAX_LTOFF)
+		struct module_memory *mod_mem;
+
+		mod_mem = &mod->mod_mem[MOD_MEM_TYPE_DATA];
+		if (mod_mem->size > MAX_LTOFF)
 			/*
 			 * This takes advantage of fact that SHF_ARCH_SMALL gets allocated
 			 * at the end of the module.
 			 */
-			gp = mod->core_layout.size - MAX_LTOFF / 2;
+			gp = mod_mem->size - MAX_LTOFF / 2;
 		else
-			gp = mod->core_layout.size / 2;
-		gp = (uint64_t) mod->core_layout.base + ((gp + 7) & -8);
+			gp = mod_mem->size / 2;
+		gp = (uint64_t) mod_mem->base + ((gp + 7) & -8);
 		mod->arch.gp = gp;
 		DEBUGP("%s: placing gp at 0x%lx\n", __func__, gp);
 	}
arch/mips/kernel/vpe.c +5 -6
index 13294972707b..b13363a1f6a3 100644
@@ -199,18 +199,17 @@ static void layout_sections(struct module *mod, const Elf_Ehdr *hdr,
 	for (m = 0; m < ARRAY_SIZE(masks); ++m) {
 		for (i = 0; i < hdr->e_shnum; ++i) {
 			Elf_Shdr *s = &sechdrs[i];
+			struct module_memory *mod_mem;
+
+			mod_mem = &mod->mod_mem[MOD_MEM_TYPE_TEXT];
 
 			if ((s->sh_flags & masks[m][0]) != masks[m][0]
 			    || (s->sh_flags & masks[m][1])
 			    || s->sh_entsize != ~0UL)
 				continue;
 			s->sh_entsize =
-				get_offset((unsigned long *)&mod->core_layout.size, s);
+				get_offset((unsigned long *)&mod_mem->size, s);
 		}
-
-		if (m == 0)
-			mod->core_layout.text_size = mod->core_layout.size;
-
 	}
 }
 
@@ -641,7 +640,7 @@ static int vpe_elfload(struct vpe *v)
 		layout_sections(&mod, hdr, sechdrs, secstrings);
 	}
 
-	v->load_addr = alloc_progmem(mod.core_layout.size);
+	v->load_addr = alloc_progmem(mod.mod_mem[MOD_MEM_TYPE_TEXT].size);
 	if (!v->load_addr)
 		return -ENOMEM;
 
arch/parisc/kernel/module.c +26 -27

113 lines not on this page · raw message

arch/powerpc/kernel/module_32.c +6 -4
index ea6536171778..1496551c9e9e 100644
@@ -160,11 +160,13 @@ static uint32_t do_plt_call(void *location,
 			    struct module *mod)
 {
 	struct ppc_plt_entry *entry;
+	struct module_memory *mod_mem;
 
 	pr_debug("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location);
+
+	mod_mem = &mod->mod_mem[MOD_MEM_TYPE_TEXT];
 	/* Init, or core PLT? */
-	if (location >= mod->core_layout.base
-	    && location < mod->core_layout.base + mod->core_layout.size)
+	if (location >= mod_mem->base && location < mod_mem->base + mod_mem->size)
 		entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
 	else
 		entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
@@ -322,14 +324,14 @@ notrace int module_trampoline_target(struct module *mod, unsigned long addr,
 
 int module_finalize_ftrace(struct module *module, const Elf_Shdr *sechdrs)
 {
-	module->arch.tramp = do_plt_call(module->core_layout.base,
+	module->arch.tramp = do_plt_call(module->mod_mem[MOD_MEM_TYPE_TEXT].base,
 					 (unsigned long)ftrace_caller,
 					 sechdrs, module);
 	if (!module->arch.tramp)
 		return -ENOENT;
 
 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
-	module->arch.tramp_regs = do_plt_call(module->core_layout.base,
+	module->arch.tramp_regs = do_plt_call(module->mod_mem[MOD_MEM_TYPE_TEXT].base,
 					      (unsigned long)ftrace_regs_caller,
 					      sechdrs, module);
 	if (!module->arch.tramp_regs)
arch/s390/kernel/module.c +16 -14
index 2d159b32885b..5f9c45fa2ffd 100644
@@ -108,7 +108,7 @@ static void check_rela(Elf_Rela *rela, struct module *me)
 	case R_390_GLOB_DAT:
 	case R_390_JMP_SLOT:
 	case R_390_RELATIVE:
-		/* Only needed if we want to support loading of 
+		/* Only needed if we want to support loading of
 		   modules linked with -shared. */
 		break;
 	}
@@ -126,6 +126,7 @@ int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
 	Elf_Rela *rela;
 	char *strings;
 	int nrela, i, j;
+	struct module_memory *mod_mem;
 
 	/* Find symbol table and string table. */
 	symtab = NULL;
@@ -173,14 +174,15 @@ int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
 
 	/* Increase core size by size of got & plt and set start
 	   offsets for got and plt. */
-	me->core_layout.size = ALIGN(me->core_layout.size, 4);
-	me->arch.got_offset = me->core_layout.size;
-	me->core_layout.size += me->arch.got_size;
-	me->arch.plt_offset = me->core_layout.size;
+	mod_mem = &me->mod_mem[MOD_MEM_TYPE_TEXT];
+	mod_mem->size = ALIGN(mod_mem->size, 4);
+	me->arch.got_offset = mod_mem->size;
+	mod_mem->size += me->arch.got_size;
+	me->arch.plt_offset = mod_mem->size;
 	if (me->arch.plt_size) {
 		if (IS_ENABLED(CONFIG_EXPOLINE) && !nospec_disable)
 			me->arch.plt_size += PLT_ENTRY_SIZE;
-		me->core_layout.size += me->arch.plt_size;
+		mod_mem->size += me->arch.plt_size;
 	}
 	return 0;
 }
@@ -304,7 +306,7 @@ static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
 	case R_390_GOTPLT64:	/* 64 bit offset to jump slot.	*/
 	case R_390_GOTPLTENT:	/* 32 bit rel. offset to jump slot >> 1. */
 		if (info->got_initialized == 0) {
-			Elf_Addr *gotent = me->core_layout.base +
+			Elf_Addr *gotent = me->mod_mem[MOD_MEM_TYPE_TEXT].base +
 					   me->arch.got_offset +
 					   info->got_offset;
 
@@ -329,7 +331,7 @@ static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
 			rc = apply_rela_bits(loc, val, 0, 64, 0, write);
 		else if (r_type == R_390_GOTENT ||
 			 r_type == R_390_GOTPLTENT) {
-			val += (Elf_Addr) me->core_layout.base - loc;
+			val += (Elf_Addr) me->mod_mem[MOD_MEM_TYPE_TEXT].base - loc;
 			rc = apply_rela_bits(loc, val, 1, 32, 1, write);
 		}
 		break;
@@ -345,7 +347,7 @@ static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
 			char *plt_base;
 			char *ip;
 
-			plt_base = me->core_layout.base + me->arch.plt_offset;
+			plt_base = me->mod_mem[MOD_MEM_TYPE_TEXT].base + me->arch.plt_offset;
 			ip = plt_base + info->plt_offset;
 			*(int *)insn = 0x0d10e310;	/* basr 1,0  */
 			*(int *)&insn[4] = 0x100c0004;	/* lg	1,12(1) */
@@ -375,7 +377,7 @@ static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
 			       val - loc + 0xffffUL < 0x1ffffeUL) ||
 			      (r_type == R_390_PLT32DBL &&
 			       val - loc + 0xffffffffULL < 0x1fffffffeULL)))
-				val = (Elf_Addr) me->core_layout.base +
+				val = (Elf_Addr) me->mod_mem[MOD_MEM_TYPE_TEXT].base +
 					me->arch.plt_offset +
 					info->plt_offset;
 			val += rela->r_addend - loc;
@@ -397,7 +399,7 @@ static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
 	case R_390_GOTOFF32:	/* 32 bit offset to GOT.  */
 	case R_390_GOTOFF64:	/* 64 bit offset to GOT. */
 		val = val + rela->r_addend -
-			((Elf_Addr) me->core_layout.base + me->arch.got_offset);
+			((Elf_Addr) me->mod_mem[MOD_MEM_TYPE_TEXT].base + me->arch.got_offset);
 		if (r_type == R_390_GOTOFF16)
 			rc = apply_rela_bits(loc, val, 0, 16, 0, write);
 		else if (r_type == R_390_GOTOFF32)
@@ -407,7 +409,7 @@ static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
 		break;
 	case R_390_GOTPC:	/* 32 bit PC relative offset to GOT. */
 	case R_390_GOTPCDBL:	/* 32 bit PC rel. off. to GOT shifted by 1. */
-		val = (Elf_Addr) me->core_layout.base + me->arch.got_offset +
+		val = (Elf_Addr) me->mod_mem[MOD_MEM_TYPE_TEXT].base + me->arch.got_offset +
 			rela->r_addend - loc;
 		if (r_type == R_390_GOTPC)
 			rc = apply_rela_bits(loc, val, 1, 32, 0, write);
@@ -418,7 +420,7 @@ static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
 	case R_390_GLOB_DAT:	/* Create GOT entry.  */
 	case R_390_JMP_SLOT:	/* Create PLT entry.  */
 	case R_390_RELATIVE:	/* Adjust by program base.  */
-		/* Only needed if we want to support loading of 
+		/* Only needed if we want to support loading of
 		   modules linked with -shared. */
 		return -ENOEXEC;
 	default:
@@ -515,7 +517,7 @@ int module_finalize(const Elf_Ehdr *hdr,
 	    !nospec_disable && me->arch.plt_size) {
 		unsigned int *ij;
 
-		ij = me->core_layout.base + me->arch.plt_offset +
+		ij = me->mod_mem[MOD_MEM_TYPE_TEXT].base + me->arch.plt_offset +
 			me->arch.plt_size - PLT_ENTRY_SIZE;
 		ij[0] = 0xc6000000;	/* exrl	%r0,.+10	*/
 		ij[1] = 0x0005a7f4;	/* j	.		*/
arch/x86/kernel/callthunks.c +3 -2
index ffea98f9064b..2f104cede64b 100644
@@ -330,8 +330,9 @@ void noinline callthunks_patch_module_calls(struct callthunk_sites *cs,
 					    struct module *mod)
 {
 	struct core_text ct = {
-		.base = (unsigned long)mod->core_layout.base,
-		.end  = (unsigned long)mod->core_layout.base + mod->core_layout.size,
+		.base = (unsigned long)mod->mod_mem[MOD_MEM_TYPE_TEXT].base,
+		.end  = (unsigned long)mod->mod_mem[MOD_MEM_TYPE_TEXT].base +
+			mod->mod_mem[MOD_MEM_TYPE_TEXT].size,
 		.name = mod->name,
 	};
 
arch/x86/kernel/module.c +2 -2
index 705fb2a41d7d..2087b04808ae 100644
@@ -339,8 +339,8 @@ int module_finalize(const Elf_Ehdr *hdr,
 	}
 	if (locks) {
 		void *lseg = (void *)locks->sh_addr;
-		void *text = me->core_layout.base;
-		void *text_end = text + me->core_layout.text_size;
+		void *text = me->mod_mem[MOD_MEM_TYPE_TEXT].base;
+		void *text_end = text + me->mod_mem[MOD_MEM_TYPE_TEXT].size;
 		alternatives_smp_module_add(me, me->name,
 					    lseg, lseg + locks->sh_size,
 					    text, text_end);
include/linux/module.h +39 -26

100 lines not on this page · raw message

kernel/module/internal.h +27 -20

72 lines not on this page · raw message

kernel/module/kallsyms.c +35 -25

127 lines not on this page · raw message

kernel/module/kdb.c +9 -8
index f4317f92e189..33f595454488 100644
@@ -26,10 +26,11 @@ int kdb_lsmod(int argc, const char **argv)
 		if (mod->state == MODULE_STATE_UNFORMED)
 			continue;
 
-		kdb_printf("%-20s%8u", mod->name, mod->core_layout.size);
-#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
-		kdb_printf("/%8u", mod->data_layout.size);
-#endif
+		kdb_printf("%-20s%8u", mod->name, mod->mod_mem[MOD_MEM_TYPE_TEXT].size);
+		kdb_printf("/%8u", mod->mod_mem[MOD_MEM_TYPE_RODATA].size);
+		kdb_printf("/%8u", mod->mod_mem[MOD_MEM_TYPE_RO_AFTER_INIT].size);
+		kdb_printf("/%8u", mod->mod_mem[MOD_MEM_TYPE_DATA].size);
+
 		kdb_printf("  0x%px ", (void *)mod);
 #ifdef CONFIG_MODULE_UNLOAD
 		kdb_printf("%4d ", module_refcount(mod));
@@ -40,10 +41,10 @@ int kdb_lsmod(int argc, const char **argv)
 			kdb_printf(" (Loading)");
 		else
 			kdb_printf(" (Live)");
-		kdb_printf(" 0x%px", mod->core_layout.base);
-#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
-		kdb_printf("/0x%px", mod->data_layout.base);
-#endif
+		kdb_printf(" 0x%px", mod->mod_mem[MOD_MEM_TYPE_TEXT].base);
+		kdb_printf("/0x%px", mod->mod_mem[MOD_MEM_TYPE_RODATA].base);
+		kdb_printf("/0x%px", mod->mod_mem[MOD_MEM_TYPE_RO_AFTER_INIT].base);
+		kdb_printf("/0x%px", mod->mod_mem[MOD_MEM_TYPE_DATA].base);
 
 #ifdef CONFIG_MODULE_UNLOAD
 		{
kernel/module/main.c +222 -156

598 lines not on this page · raw message

kernel/module/procfs.c +12 -5
index cf5b9f1e6ec4..1fce4209aeb2 100644
@@ -62,6 +62,16 @@ static void m_stop(struct seq_file *m, void *p)
 	mutex_unlock(&module_mutex);
 }
 
+static unsigned int module_total_size(struct module *mod)
+{
+	int size = 0;
+	enum mod_mem_type type;
+
+	for_each_mod_mem_type(type)
+		size += mod->mod_mem[type].size;
+	return size;
+}
+
 static int m_show(struct seq_file *m, void *p)
 {
 	struct module *mod = list_entry(p, struct module, list);
@@ -73,10 +83,7 @@ static int m_show(struct seq_file *m, void *p)
 	if (mod->state == MODULE_STATE_UNFORMED)
 		return 0;
 
-	size = mod->init_layout.size + mod->core_layout.size;
-#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
-	size += mod->data_layout.size;
-#endif
+	size = module_total_size(mod);
 	seq_printf(m, "%s %u", mod->name, size);
 	print_unload_info(m, mod);
 
@@ -86,7 +93,7 @@ static int m_show(struct seq_file *m, void *p)
 		   mod->state == MODULE_STATE_COMING ? "Loading" :
 		   "Live");
 	/* Used by oprofile and other similar tools. */
-	value = m->private ? NULL : mod->core_layout.base;
+	value = m->private ? NULL : mod->mod_mem[MOD_MEM_TYPE_TEXT].base;
 	seq_printf(m, " 0x%px", value);
 
 	/* Taints info */
kernel/module/strict_rwx.c +35 -78

145 lines not on this page · raw message

kernel/module/tree_lookup.c +22 -21

70 lines not on this page · raw message

Signature · 1 line
2.30.2
maintainer Reviewed-by on v3 1/1
#1 lore raw
Quoted 34 lines · Song Liu

module_layout manages different types of memory (text, data, rodata, etc.) in one allocation, which is problematic for some reasons:

1. It is hard to enable CONFIG_STRICT_MODULE_RWX.
2. It is hard to use huge pages in modules (and not break strict rwx).
3. Many archs uses module_layout for arch-specific data, but it is not obvious how these data are used (are they RO, RX, or RW?)

Improve the scenario by replacing 2 (or 3) module_layout per module with up to 7 module_memory per module:

MOD_MEM_TYPE_TEXT,
MOD_MEM_TYPE_DATA,
MOD_MEM_TYPE_RODATA,
MOD_MEM_TYPE_RO_AFTER_INIT,
MOD_MEM_TYPE_INIT_TEXT,
MOD_MEM_TYPE_INIT_DATA,
MOD_MEM_TYPE_INIT_RODATA,

and allocating them separately. This adds slightly more entries to mod_tree (from up to 3 entries per module, to up to 7 entries per module). However, this at most adds a small constant overhead to __module_address(), which is expected to be fast.

Various archs use module_layout for different data. These data are put into different module_memory based on their location in module_layout. IOW, data that used to go with text is allocated with MOD_MEM_TYPE_TEXT; data that used to go with data is allocated with MOD_MEM_TYPE_DATA, etc.

module_memory simplifies quite some of the module code. For example, ARCH_WANTS_MODULES_DATA_IN_VMALLOC is a lot cleaner, as it just uses a different allocator for the data. kernel/module/strict_rwx.c is also much cleaner with module_memory.

Quoted 5 lines
Cc Luis Chamberlain <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Guenter Roeck <[email protected]>

I'll add this new version to modules-testing. If we get any other Reviewed-by, I'll throw onto modules-next.

Luis

#2 lore raw
In reply to Song Liu
Quoted 34 lines · Song Liu

module_layout manages different types of memory (text, data, rodata, etc.) in one allocation, which is problematic for some reasons:

1. It is hard to enable CONFIG_STRICT_MODULE_RWX.
2. It is hard to use huge pages in modules (and not break strict rwx).
3. Many archs uses module_layout for arch-specific data, but it is not obvious how these data are used (are they RO, RX, or RW?)

Improve the scenario by replacing 2 (or 3) module_layout per module with up to 7 module_memory per module:

MOD_MEM_TYPE_TEXT,
MOD_MEM_TYPE_DATA,
MOD_MEM_TYPE_RODATA,
MOD_MEM_TYPE_RO_AFTER_INIT,
MOD_MEM_TYPE_INIT_TEXT,
MOD_MEM_TYPE_INIT_DATA,
MOD_MEM_TYPE_INIT_RODATA,

and allocating them separately. This adds slightly more entries to mod_tree (from up to 3 entries per module, to up to 7 entries per module). However, this at most adds a small constant overhead to __module_address(), which is expected to be fast.

Various archs use module_layout for different data. These data are put into different module_memory based on their location in module_layout. IOW, data that used to go with text is allocated with MOD_MEM_TYPE_TEXT; data that used to go with data is allocated with MOD_MEM_TYPE_DATA, etc.

module_memory simplifies quite some of the module code. For example, ARCH_WANTS_MODULES_DATA_IN_VMALLOC is a lot cleaner, as it just uses a different allocator for the data. kernel/module/strict_rwx.c is also much cleaner with module_memory.

Quoted 7 lines
Cc Luis Chamberlain <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Guenter Roeck <[email protected]>

---

Build reference: v6.2-rc5-52-gc96fb00 Compiler version: powerpc64-linux-gcc (GCC) 11.3.0 Assembler version: GNU assembler (GNU Binutils) 2.39

Building powerpc:defconfig ... passed
Building powerpc:allmodconfig ... passed
Building powerpc:ppc32_allmodconfig ... failed
--------------
Error log:
kernel/module/main.c: In function 'show_coresize':
kernel/module/main.c:938:22: error: unused variable 'size' [-Werror=unused-variable]
938 | unsigned int size = 0;

That is the only failure reported by my test system.

Guenter

author
#3 lore raw

Hi Guenter,

Thanks for running the test!

Quoted 48 lines · Guenter Roeck
Quoted 34 lines · Song Liu

module_layout manages different types of memory (text, data, rodata, etc.) in one allocation, which is problematic for some reasons:

1. It is hard to enable CONFIG_STRICT_MODULE_RWX.
2. It is hard to use huge pages in modules (and not break strict rwx).
3. Many archs uses module_layout for arch-specific data, but it is not obvious how these data are used (are they RO, RX, or RW?)

Improve the scenario by replacing 2 (or 3) module_layout per module with up to 7 module_memory per module:

MOD_MEM_TYPE_TEXT,
MOD_MEM_TYPE_DATA,
MOD_MEM_TYPE_RODATA,
MOD_MEM_TYPE_RO_AFTER_INIT,
MOD_MEM_TYPE_INIT_TEXT,
MOD_MEM_TYPE_INIT_DATA,
MOD_MEM_TYPE_INIT_RODATA,

and allocating them separately. This adds slightly more entries to mod_tree (from up to 3 entries per module, to up to 7 entries per module). However, this at most adds a small constant overhead to __module_address(), which is expected to be fast.

Various archs use module_layout for different data. These data are put into different module_memory based on their location in module_layout. IOW, data that used to go with text is allocated with MOD_MEM_TYPE_TEXT; data that used to go with data is allocated with MOD_MEM_TYPE_DATA, etc.

module_memory simplifies quite some of the module code. For example, ARCH_WANTS_MODULES_DATA_IN_VMALLOC is a lot cleaner, as it just uses a different allocator for the data. kernel/module/strict_rwx.c is also much cleaner with module_memory.

Quoted 7 lines
Cc Luis Chamberlain <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Guenter Roeck <[email protected]>

---

Build reference: v6.2-rc5-52-gc96fb00 Compiler version: powerpc64-linux-gcc (GCC) 11.3.0 Assembler version: GNU assembler (GNU Binutils) 2.39

Quoted 8 lines

Building powerpc:defconfig ... passed
Building powerpc:allmodconfig ... passed
Building powerpc:ppc32_allmodconfig ... failed
--------------
Error log:
kernel/module/main.c: In function 'show_coresize':
kernel/module/main.c:938:22: error: unused variable 'size' [-Werror=unused-variable]
938 | unsigned int size = 0;

Fixed this in my local branch:

kernel/module/main.c +0 -2
index 32f63c6eaa61..da2bca75e4a8 100644
@@ -935,8 +935,6 @@ struct module_attribute module_uevent =
 static ssize_t show_coresize(struct module_attribute *mattr,
                             struct module_kobject *mk, char *buffer)
 {
-       unsigned int size = 0;
-
        return sprintf(buffer, "%u\n",
                       mk->mod->mod_mem[MOD_MEM_TYPE_TEXT].size);
 }

Song

#5 lore raw
Quoted 5 lines · Luis Chamberlain
Quoted 1 line · Guenter Roeck

That is the only failure reported by my test system.

I thought your rig does some form of boot / runtime tests too?

Here are the detailed results:

Build results:

	total: 155 pass: 154 fail: 1
Failed builds:
	powerpc:ppc32_allmodconfig
Qemu test results:
	total: 504 pass: 504 fail: 0

It does run boot tests, but it doesn't test module functionality (it doesn't try to load modules).

Guenter

maintainer
#6 lore raw
Quoted 10 lines · Guenter Roeck
Quoted 5 lines · Luis Chamberlain
Quoted 1 line · Guenter Roeck

That is the only failure reported by my test system.

I thought your rig does some form of boot / runtime tests too?

Here are the detailed results:

Build results:

Quoted 8 lines
	total: 155 pass: 154 fail: 1
Failed builds:
	powerpc:ppc32_allmodconfig
Qemu test results:
	total: 504 pass: 504 fail: 0

It does run boot tests, but it doesn't test module functionality (it doesn't try to load modules).

Ah bummer, but thanks the tests!

Luis

#7 lore raw
In reply to Song Liu

Le 27/01/2023 à 00:36, Song Liu a écrit :

Quoted 102 lines

module_layout manages different types of memory (text, data, rodata, etc.) in one allocation, which is problematic for some reasons:

1. It is hard to enable CONFIG_STRICT_MODULE_RWX.
2. It is hard to use huge pages in modules (and not break strict rwx).
3. Many archs uses module_layout for arch-specific data, but it is not obvious how these data are used (are they RO, RX, or RW?)

Improve the scenario by replacing 2 (or 3) module_layout per module with up to 7 module_memory per module:

MOD_MEM_TYPE_TEXT,
MOD_MEM_TYPE_DATA,
MOD_MEM_TYPE_RODATA,
MOD_MEM_TYPE_RO_AFTER_INIT,
MOD_MEM_TYPE_INIT_TEXT,
MOD_MEM_TYPE_INIT_DATA,
MOD_MEM_TYPE_INIT_RODATA,

and allocating them separately. This adds slightly more entries to mod_tree (from up to 3 entries per module, to up to 7 entries per module). However, this at most adds a small constant overhead to __module_address(), which is expected to be fast.

Various archs use module_layout for different data. These data are put into different module_memory based on their location in module_layout. IOW, data that used to go with text is allocated with MOD_MEM_TYPE_TEXT; data that used to go with data is allocated with MOD_MEM_TYPE_DATA, etc.

module_memory simplifies quite some of the module code. For example, ARCH_WANTS_MODULES_DATA_IN_VMALLOC is a lot cleaner, as it just uses a different allocator for the data. kernel/module/strict_rwx.c is also much cleaner with module_memory.

Cc Luis Chamberlain <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Guenter Roeck <[email protected]>

---

This is the preparation work for the type aware module_alloc() discussed in [1]. While this work is not covered much in the discussion, it is a critical step of the effort.

As this part grows pretty big (~1000 lines, + and -), I would like get some feedback on it, so that I know it is on the right track.

Please share your comments. Thanks!

Test coverage: Tested on x86_64.
Build tested by kernel test bot in [2]. The only regression in [2] was a typo in parisc, which is also fixed.

[1] https://lore.kernel.org/linux-mm/[email protected]/T/#u
[2] https://lore.kernel.org/linux-raid/63b8827e.clJQX2wg+I+tiX7m%[email protected]/T/#u

Changes v1 => v2:
1. Add data_addr_[min|max] for CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC case.

Changes v2 => v3:
1. Fix and remove the last use of INIT_OFFSET_MASK.
2. Add more information in the commit log. (Luis Chamberlain)
3. Rebase and fix issues in x86/calltrunks.
4. Minor cleanup.

---
  arch/alpha/kernel/module.c      |   3 +-
  arch/arc/kernel/unwind.c        |   9 +-
  arch/arm/kernel/module-plts.c   |   5 +-
  arch/arm64/kernel/module-plts.c |   5 +-
  arch/ia64/kernel/module.c       |  31 ++-
  arch/mips/kernel/vpe.c          |  11 +-
  arch/parisc/kernel/module.c     |  53 +++--
  arch/powerpc/kernel/module_32.c |  10 +-
  arch/s390/kernel/module.c       |  30 +--
  arch/x86/kernel/callthunks.c    |   5 +-
  arch/x86/kernel/module.c        |   4 +-
  include/linux/module.h          |  65 +++---
  kernel/module/internal.h        |  47 ++--
  kernel/module/kallsyms.c        |  60 ++---
  kernel/module/kdb.c             |  17 +-
  kernel/module/main.c            | 378 +++++++++++++++++++-------------
  kernel/module/procfs.c          |  17 +-
  kernel/module/strict_rwx.c      | 113 +++-------
  kernel/module/tree_lookup.c     |  43 ++--
  19 files changed, 497 insertions(+), 409 deletions(-)
include/linux/module.h +2 -2
index 8c5909c0076c..c5e78cb47e13 100644
@@ -320,17 +320,22 @@ struct mod_tree_node {
  	struct latch_tree_node node;
  };
 
-struct module_layout {
-	/* The actual code + data. */
+enum mod_mem_type {
+	MOD_MEM_TYPE_TEXT,

include/linux/module.h struct mod_tree_node { · line 334

Quoted 10 lines
include/linux/module.h +10 -0
+	MOD_MEM_TYPE_DATA,
+	MOD_MEM_TYPE_RODATA,
+	MOD_MEM_TYPE_RO_AFTER_INIT,
+	MOD_MEM_TYPE_INIT_TEXT,
+	MOD_MEM_TYPE_INIT_DATA,
+	MOD_MEM_TYPE_INIT_RODATA,
+
+	MOD_MEM_NUM_TYPES,
+	MOD_MEM_TYPE_INVALID = -1,
+};

Ok, so we agreed to keep it as a table with enums. Fair enough.

However, can we try to make it less ugly and more readable ?

I don't thing the enums needs to be prefixed by MOD_MEM_TYPE_ Would be enough with MOD_TEXT, MOD_DATA, MOD_RODATA, MOD_RO_AFTER_INIT, MOD_INIT_TEXT, MOD_INIT_DATA, MOD_INIT_RODATA, MOD_INVALID.

Quoted 28 lines
include/linux/module.h +4 -9
+
+struct module_memory {
  	void *base;
-	/* Total size. */
  	unsigned int size;
-	/* The size of the executable code.  */
-	unsigned int text_size;
-	/* Size of RO section of the module (text+rodata) */
-	unsigned int ro_size;
-	/* Size of RO after init section */
-	unsigned int ro_after_init_size;
 
  #ifdef CONFIG_MODULES_TREE_LOOKUP
  	struct mod_tree_node mtn;
@@ -339,9 +344,9 @@ struct module_layout {
 
  #ifdef CONFIG_MODULES_TREE_LOOKUP
  /* Only touch one cacheline for common rbtree-for-core-layout case. */
-#define __module_layout_align ____cacheline_aligned
+#define __module_memory_align ____cacheline_aligned
  #else
-#define __module_layout_align
+#define __module_memory_align
  #endif
 
  struct mod_kallsyms {
@@ -418,12 +423,8 @@ struct module {
  	/* Startup function. */

include/linux/module.h struct module { · line 427

Quoted 10 lines
include/linux/module.h +2 -6
  	int (*init)(void);
 
-	/* Core layout: rbtree is accessed frequently, so keep together. */
-	struct module_layout core_layout __module_layout_align;
-	struct module_layout init_layout;
-#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
-	struct module_layout data_layout;
-#endif
+	/* rbtree is accessed frequently, so keep together. */
+	struct module_memory mod_mem[MOD_MEM_NUM_TYPES] __module_memory_align;

We are already in a struct called module, so the module_memory struct could be called mem[MOD_MEM_NUM_TYPES]

Quoted 4 lines
include/linux/module.h +0 -0
  	/* Arch-specific module values */
  	struct mod_arch_specific arch;

include/linux/module.h bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr); · line 583

Quoted 10 lines
include/linux/module.h +7 -0
@@ -573,23 +574,35 @@ bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr);
  bool is_module_percpu_address(unsigned long addr);
  bool is_module_text_address(unsigned long addr);
 
+static inline bool within_module_mem_type(unsigned long addr,
+					  const struct module *mod,
+					  enum mod_mem_type type)
+{
+	const struct module_memory *mod_mem;
+
+	if (WARN_ON_ONCE(type < MOD_MEM_TYPE_TEXT || type >= MOD_MEM_NUM_TYPES))

Here I would rather use 0 instead of MOD_MEM_TYPE_TEXT because MOD_MEM_TYPE_TEXT may change in the future.

include/linux/module.h bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr); · line 586

Quoted 3 lines
include/linux/module.h +3 -0
+		return false;
+
+	mod_mem = &mod->mod_mem[type];

I can't see the added value of the mod_ prefix.

Would read better as

	mem = &mod->mem[type];

	return (unsigned long)mem->base <= addr && addr < (unsigned 

long)mem->base + mem->size;

And could be even more readable as:

	unsigned long base, size;

	base = (unsigned long)mod->mod_mem[type].base;
	size = mod->mod_mem[type].size;

	return base <= addr && addr < base + size;
Quoted 37 lines
include/linux/module.h +11 -9
+	return (unsigned long)mod_mem->base <= addr &&
+		addr < (unsigned long)mod_mem->base + mod_mem->size;
+}
+
  static inline bool within_module_core(unsigned long addr,
  				      const struct module *mod)
  {
-#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
-	if ((unsigned long)mod->data_layout.base <= addr &&
-	    addr < (unsigned long)mod->data_layout.base + mod->data_layout.size)
-		return true;
-#endif
-	return (unsigned long)mod->core_layout.base <= addr &&
-	       addr < (unsigned long)mod->core_layout.base + mod->core_layout.size;
+	return within_module_mem_type(addr, mod, MOD_MEM_TYPE_TEXT) ||
+		within_module_mem_type(addr, mod, MOD_MEM_TYPE_DATA) ||
+		within_module_mem_type(addr, mod, MOD_MEM_TYPE_RODATA) ||
+		within_module_mem_type(addr, mod, MOD_MEM_TYPE_RO_AFTER_INIT);
  }
 
  static inline bool within_module_init(unsigned long addr,
  				      const struct module *mod)
  {
-	return (unsigned long)mod->init_layout.base <= addr &&
-	       addr < (unsigned long)mod->init_layout.base + mod->init_layout.size;
+	return within_module_mem_type(addr, mod, MOD_MEM_TYPE_INIT_TEXT) ||
+		within_module_mem_type(addr, mod, MOD_MEM_TYPE_INIT_DATA) ||
+		within_module_mem_type(addr, mod, MOD_MEM_TYPE_INIT_RODATA);
  }
 
  static inline bool within_module(unsigned long addr, const struct module *mod)
kernel/module/kallsyms.c +0 -0
index 4523f99b0358..9ed233de312a 100644

kernel/module/kallsyms.c void layout_symtab(struct module *mod, struct load_info *info) · line 121

Quoted 5 lines
kernel/module/kallsyms.c +2 -0
@@ -113,11 +117,13 @@ void layout_symtab(struct module *mod, struct load_info *info)
  	Elf_Shdr *strsect = info->sechdrs + info->index.str;
  	const Elf_Sym *src;
  	unsigned int i, nsrc, ndst, strtab_size = 0;
+	struct module_memory *mod_mem_data = &mod->mod_mem[MOD_MEM_TYPE_DATA];
+	struct module_memory *mod_mem_init_data = &mod->mod_mem[MOD_MEM_TYPE_INIT_DATA];

One more exemple of something that would be more readable as:

	struct module_memory *mod_data = &mod->mem[MOD_DATA];
	struct module_memory *mod_init_data = &mod->mod[MOD_INIT_DATA];
Quoted 17 lines
kernel/module/kallsyms.c +2 -2
  	/* Put symbol section at end of init part of module. */
  	symsect->sh_flags |= SHF_ALLOC;
-	symsect->sh_entsize = module_get_offset(mod, &mod->init_layout.size, symsect,
-						info->index.sym) | INIT_OFFSET_MASK;
+	symsect->sh_entsize = module_get_offset_and_type(mod, MOD_MEM_TYPE_INIT_DATA,
+							 symsect, info->index.sym);
  	pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
 
  	src = (void *)info->hdr + symsect->sh_offset;
 
kernel/module/main.c +0 -0
index d3be89de706d..32f63c6eaa61 100644

kernel/module/main.c static ssize_t store_uevent(struct module_attribute *mattr, · line 938

Quoted 9 lines
kernel/module/main.c +6 -0
@@ -923,11 +930,30 @@ static ssize_t store_uevent(struct module_attribute *mattr,
  struct module_attribute module_uevent =
  	__ATTR(uevent, 0200, NULL, store_uevent);
 
+#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
+
+static ssize_t show_coresize(struct module_attribute *mattr,
+			     struct module_kobject *mk, char *buffer)
+{
+	unsigned int size = 0;

size is not used

kernel/module/main.c static ssize_t store_uevent(struct module_attribute *mattr, · line 941

Quoted 3 lines
kernel/module/main.c +3 -0
+
+	return sprintf(buffer, "%u\n",
+		       mk->mod->mod_mem[MOD_MEM_TYPE_TEXT].size);

Wouldn't it read better as:

	return sprintf(buffer, "%u\n", mk->mod->mem[MOD_TEXT].size);
Quoted 9 lines
kernel/module/main.c +5 -1
+}
+
+#else
+
  static ssize_t show_coresize(struct module_attribute *mattr,
  			     struct module_kobject *mk, char *buffer)
  {
-	return sprintf(buffer, "%u\n", mk->mod->core_layout.size);
+	enum mod_mem_type type;

kernel/module/main.c static ssize_t store_uevent(struct module_attribute *mattr, · line 959

Quoted 10 lines
kernel/module/main.c +6 -0
+	unsigned int size = 0;
+
+	for_core_mod_mem_type(type)
+		size += mk->mod->mod_mem[type].size;
+	return sprintf(buffer, "%u\n", size);
  }
+#endif
 
  static struct module_attribute modinfo_coresize =
  	__ATTR(coresize, 0444, show_coresize, NULL);

Christophe

author
#8 lore raw

On Fri, Jan 27, 2023 at 11:43 PM Christophe Leroy
<[email protected]> wrote:
[...]

Quoted 22 lines
Quoted 4 lines
include/linux/module.h +2 -2
-struct module_layout {
-     /* The actual code + data. */
+enum mod_mem_type {
+     MOD_MEM_TYPE_TEXT,

include/linux/module.h struct mod_tree_node { · line 334

Quoted 10 lines
include/linux/module.h +10 -0
+     MOD_MEM_TYPE_DATA,
+     MOD_MEM_TYPE_RODATA,
+     MOD_MEM_TYPE_RO_AFTER_INIT,
+     MOD_MEM_TYPE_INIT_TEXT,
+     MOD_MEM_TYPE_INIT_DATA,
+     MOD_MEM_TYPE_INIT_RODATA,
+
+     MOD_MEM_NUM_TYPES,
+     MOD_MEM_TYPE_INVALID = -1,
+};

Ok, so we agreed to keep it as a table with enums. Fair enough.

However, can we try to make it less ugly and more readable ?

I don't thing the enums needs to be prefixed by MOD_MEM_TYPE_ Would be enough with MOD_TEXT, MOD_DATA, MOD_RODATA, MOD_RO_AFTER_INIT, MOD_INIT_TEXT, MOD_INIT_DATA, MOD_INIT_RODATA, MOD_INVALID.

[...]

Quoted 43 lines

include/linux/module.h struct module { · line 427

Quoted 8 lines
include/linux/module.h +2 -6
-     /* Core layout: rbtree is accessed frequently, so keep together. */
-     struct module_layout core_layout __module_layout_align;
-     struct module_layout init_layout;
-#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
-     struct module_layout data_layout;
-#endif
+     /* rbtree is accessed frequently, so keep together. */
+     struct module_memory mod_mem[MOD_MEM_NUM_TYPES] __module_memory_align;

We are already in a struct called module, so the module_memory struct could be called mem[MOD_MEM_NUM_TYPES]

Quoted 4 lines
      /* Arch-specific module values */
      struct mod_arch_specific arch;

include/linux/module.h bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr); · line 583

Quoted 10 lines
include/linux/module.h +7 -0
@@ -573,23 +574,35 @@ bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr);
  bool is_module_percpu_address(unsigned long addr);
  bool is_module_text_address(unsigned long addr);
 
+static inline bool within_module_mem_type(unsigned long addr,
+                                       const struct module *mod,
+                                       enum mod_mem_type type)
+{
+     const struct module_memory *mod_mem;
+
+     if (WARN_ON_ONCE(type < MOD_MEM_TYPE_TEXT || type >= MOD_MEM_NUM_TYPES))

Here I would rather use 0 instead of MOD_MEM_TYPE_TEXT because MOD_MEM_TYPE_TEXT may change in the future.

include/linux/module.h bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr); · line 586

Quoted 3 lines
include/linux/module.h +3 -0
+             return false;
+
+     mod_mem = &mod->mod_mem[type];

I can't see the added value of the mod_ prefix.

Would read better as

        mem = &mod->mem[type];

        return (unsigned long)mem->base <= addr && addr < (unsigned

long)mem->base + mem->size;

Quoted 8 lines

And could be even more readable as:

        unsigned long base, size;

        base = (unsigned long)mod->mod_mem[type].base;
        size = mod->mod_mem[type].size;

        return base <= addr && addr < base + size;

Yeah, the code does look better with shorter names.

If there is no objection from folks, I will send v4 with these suggestions next week.

Thanks,
Song