Skip to contentSkip to conversation

Allocate module text and data separately

PATCH Superseded Christophe Leroy 3 participants 14 messages Jan 29, 2022 – Feb 8, 2022

Subsystems (5)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover Allocate module text and data separately

This series allow architectures to request having modules data in vmalloc area instead of module area.

This is required on powerpc book3s/32 in order to set data non executable, because it is not possible to set executability on page basis, this is done per 256 Mbytes segments. The module area has exec right, vmalloc area has noexec. Without this change module data remains executable regardless of CONFIG_STRICT_MODULES_RWX.

This can also be useful on other powerpc/32 in order to maximize the chance of code being close enough to kernel core to avoid branch trampolines.

Changes in v3:
- Fixed the tree for data_layout at one place (Thanks Miroslav)
- Moved removal of module_addr_min/module_addr_max macro out of patch 1 in a new patch at the end of the series to reduce churn.

Changes in v2:
- Dropped first two patches which are not necessary. They may be added back later as a follow-up series.
- Fixed the printks in GDB

Christophe Leroy (6):
modules: Always have struct mod_tree_root
modules: Prepare for handling several RB trees
modules: Introduce data_layout
modules: Add CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
modules: Remove module_addr_min and module_addr_max
powerpc: Select ARCH_WANTS_MODULES_DATA_IN_VMALLOC on book3s/32 and

    8xx
 arch/Kconfig                |   6 ++
 arch/powerpc/Kconfig        |   1 +
 include/linux/module.h      |   8 ++
 kernel/debug/kdb/kdb_main.c |  10 +-
 kernel/module.c             | 193 +++++++++++++++++++++++++-----------
 5 files changed, 156 insertions(+), 62 deletions(-)
Signature · 1 line
2.33.1
#1 lore raw

PATCH v3 1/6 modules: Always have struct mod_tree_root

In order to separate text and data, we need to setup two rb trees.

This means that struct mod_tree_root is required even without MODULES_TREE_LOOKUP.

---
 kernel/module.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
kernel/module.c +13 -13
index 24dab046e16c..080193e15d24 100644
@@ -96,6 +96,19 @@ static void do_free_init(struct work_struct *w);
 static DECLARE_WORK(init_free_wq, do_free_init);
 static LLIST_HEAD(init_free_list);
 
+static struct mod_tree_root {
+#ifdef CONFIG_MODULES_TREE_LOOKUP
+	struct latch_tree_root root;
+#endif
+	unsigned long addr_min;
+	unsigned long addr_max;
+} mod_tree __cacheline_aligned = {
+	.addr_min = -1UL,
+};
+
+#define module_addr_min mod_tree.addr_min
+#define module_addr_max mod_tree.addr_max
+
 #ifdef CONFIG_MODULES_TREE_LOOKUP
 
 /*
@@ -149,17 +162,6 @@ static const struct latch_tree_ops mod_tree_ops = {
 	.comp = mod_tree_comp,
 };
 
-static struct mod_tree_root {
-	struct latch_tree_root root;
-	unsigned long addr_min;
-	unsigned long addr_max;
-} mod_tree __cacheline_aligned = {
-	.addr_min = -1UL,
-};
-
-#define module_addr_min mod_tree.addr_min
-#define module_addr_max mod_tree.addr_max
-
 static noinline void __mod_tree_insert(struct mod_tree_node *node)
 {
 	latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops);
@@ -209,8 +211,6 @@ static struct module *mod_find(unsigned long addr)
 
 #else /* MODULES_TREE_LOOKUP */
 
-static unsigned long module_addr_min = -1UL, module_addr_max = 0;
-
 static void mod_tree_insert(struct module *mod) { }
 static void mod_tree_remove_init(struct module *mod) { }
 static void mod_tree_remove(struct module *mod) { }
Signature · 1 line
2.33.1
#2 lore raw

PATCH v3 2/6 modules: Prepare for handling several RB trees

In reply to Christophe Leroy

In order to separate text and data, we need to setup two rb trees.

Modify functions to give the tree as a parameter.

---
 kernel/module.c | 38 +++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)
kernel/module.c +19 -19
index 080193e15d24..163e32e39064 100644
@@ -162,14 +162,14 @@ static const struct latch_tree_ops mod_tree_ops = {
 	.comp = mod_tree_comp,
 };
 
-static noinline void __mod_tree_insert(struct mod_tree_node *node)
+static noinline void __mod_tree_insert(struct mod_tree_node *node, struct mod_tree_root *tree)
 {
-	latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops);
+	latch_tree_insert(&node->node, &tree->root, &mod_tree_ops);
 }
 
-static void __mod_tree_remove(struct mod_tree_node *node)
+static void __mod_tree_remove(struct mod_tree_node *node, struct mod_tree_root *tree)
 {
-	latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops);
+	latch_tree_erase(&node->node, &tree->root, &mod_tree_ops);
 }
 
 /*
@@ -181,28 +181,28 @@ static void mod_tree_insert(struct module *mod)
 	mod->core_layout.mtn.mod = mod;
 	mod->init_layout.mtn.mod = mod;
 
-	__mod_tree_insert(&mod->core_layout.mtn);
+	__mod_tree_insert(&mod->core_layout.mtn, &mod_tree);
 	if (mod->init_layout.size)
-		__mod_tree_insert(&mod->init_layout.mtn);
+		__mod_tree_insert(&mod->init_layout.mtn, &mod_tree);
 }
 
 static void mod_tree_remove_init(struct module *mod)
 {
 	if (mod->init_layout.size)
-		__mod_tree_remove(&mod->init_layout.mtn);
+		__mod_tree_remove(&mod->init_layout.mtn, &mod_tree);
 }
 
 static void mod_tree_remove(struct module *mod)
 {
-	__mod_tree_remove(&mod->core_layout.mtn);
+	__mod_tree_remove(&mod->core_layout.mtn, &mod_tree);
 	mod_tree_remove_init(mod);
 }
 
-static struct module *mod_find(unsigned long addr)
+static struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
 {
 	struct latch_tree_node *ltn;
 
-	ltn = latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops);
+	ltn = latch_tree_find((void *)addr, &tree->root, &mod_tree_ops);
 	if (!ltn)
 		return NULL;
 
@@ -215,7 +215,7 @@ static void mod_tree_insert(struct module *mod) { }
 static void mod_tree_remove_init(struct module *mod) { }
 static void mod_tree_remove(struct module *mod) { }
 
-static struct module *mod_find(unsigned long addr)
+static struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
 {
 	struct module *mod;
 
@@ -234,22 +234,22 @@ static struct module *mod_find(unsigned long addr)
  * Bounds of module text, for speeding up __module_address.
  * Protected by module_mutex.
  */
-static void __mod_update_bounds(void *base, unsigned int size)
+static void __mod_update_bounds(void *base, unsigned int size, struct mod_tree_root *tree)
 {
 	unsigned long min = (unsigned long)base;
 	unsigned long max = min + size;
 
-	if (min < module_addr_min)
-		module_addr_min = min;
-	if (max > module_addr_max)
-		module_addr_max = max;
+	if (min < tree->addr_min)
+		tree->addr_min = min;
+	if (max > tree->addr_max)
+		tree->addr_max = max;
 }
 
 static void mod_update_bounds(struct module *mod)
 {
-	__mod_update_bounds(mod->core_layout.base, mod->core_layout.size);
+	__mod_update_bounds(mod->core_layout.base, mod->core_layout.size, &mod_tree);
 	if (mod->init_layout.size)
-		__mod_update_bounds(mod->init_layout.base, mod->init_layout.size);
+		__mod_update_bounds(mod->init_layout.base, mod->init_layout.size, &mod_tree);
 }
 
 #ifdef CONFIG_KGDB_KDB
@@ -4742,7 +4742,7 @@ struct module *__module_address(unsigned long addr)
 
 	module_assert_mutex_or_preempt();
 
-	mod = mod_find(addr);
+	mod = mod_find(addr, &mod_tree);
 	if (mod) {
 		BUG_ON(!within_module(addr, mod));
 		if (mod->state == MODULE_STATE_UNFORMED)
Signature · 1 line
2.33.1
#3 lore raw

PATCH v3 3/6 modules: Introduce data_layout

In reply to Christophe Leroy

In order to allow separation of data from text, add another layout, called data_layout. For architectures requesting separation of text and data, only text will go in core_layout and data will go in data_layout.

For architectures which keep text and data together, make data_layout an alias of core_layout, that way data_layout can be used for all data manipulations, regardless of whether data is in core_layout or data_layout.

---
 kernel/module.c | 52 ++++++++++++++++++++++++++++---------------------
 1 file changed, 30 insertions(+), 22 deletions(-)
kernel/module.c +30 -22
index 163e32e39064..11f51e17fb9f 100644
@@ -81,6 +81,8 @@
 /* If this is set, the section belongs in the init part of the module */
 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
 
+#define	data_layout core_layout
+
 /*
  * Mutex protects:
  * 1) List of modules (also safely readable with preempt_disable),
@@ -2014,19 +2016,20 @@ static void module_enable_ro(const struct module *mod, bool after_init)
 	set_vm_flush_reset_perms(mod->init_layout.base);
 	frob_text(&mod->core_layout, set_memory_ro);
 
-	frob_rodata(&mod->core_layout, set_memory_ro);
+	frob_rodata(&mod->data_layout, set_memory_ro);
+
 	frob_text(&mod->init_layout, set_memory_ro);
 	frob_rodata(&mod->init_layout, set_memory_ro);
 
 	if (after_init)
-		frob_ro_after_init(&mod->core_layout, set_memory_ro);
+		frob_ro_after_init(&mod->data_layout, set_memory_ro);
 }
 
 static void module_enable_nx(const struct module *mod)
 {
-	frob_rodata(&mod->core_layout, set_memory_nx);
-	frob_ro_after_init(&mod->core_layout, set_memory_nx);
-	frob_writable_data(&mod->core_layout, set_memory_nx);
+	frob_rodata(&mod->data_layout, set_memory_nx);
+	frob_ro_after_init(&mod->data_layout, set_memory_nx);
+	frob_writable_data(&mod->data_layout, set_memory_nx);
 	frob_rodata(&mod->init_layout, set_memory_nx);
 	frob_writable_data(&mod->init_layout, set_memory_nx);
 }
@@ -2204,7 +2207,7 @@ static void free_module(struct module *mod)
 	percpu_modfree(mod);
 
 	/* Free lock-classes; relies on the preceding sync_rcu(). */
-	lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
+	lockdep_free_key_range(mod->data_layout.base, mod->data_layout.size);
 
 	/* Finally, free the core (containing the module structure) */
 	module_memfree(mod->core_layout.base);
@@ -2451,7 +2454,10 @@ static void layout_sections(struct module *mod, struct load_info *info)
 			    || s->sh_entsize != ~0UL
 			    || module_init_layout_section(sname))
 				continue;
-			s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
+			if (m)
+				s->sh_entsize = get_offset(mod, &mod->data_layout.size, s, i);
+			else
+				s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
 			pr_debug("\t%s\n", sname);
 		}
 		switch (m) {
@@ -2460,15 +2466,15 @@ static void layout_sections(struct module *mod, struct load_info *info)
 			mod->core_layout.text_size = mod->core_layout.size;
 			break;
 		case 1: /* RO: text and ro-data */
-			mod->core_layout.size = debug_align(mod->core_layout.size);
-			mod->core_layout.ro_size = mod->core_layout.size;
+			mod->data_layout.size = debug_align(mod->data_layout.size);
+			mod->data_layout.ro_size = mod->data_layout.size;
 			break;
 		case 2: /* RO after init */
-			mod->core_layout.size = debug_align(mod->core_layout.size);
-			mod->core_layout.ro_after_init_size = mod->core_layout.size;
+			mod->data_layout.size = debug_align(mod->data_layout.size);
+			mod->data_layout.ro_after_init_size = mod->data_layout.size;
 			break;
 		case 4: /* whole core */
-			mod->core_layout.size = debug_align(mod->core_layout.size);
+			mod->data_layout.size = debug_align(mod->data_layout.size);
 			break;
 		}
 	}
@@ -2721,12 +2727,12 @@ static void layout_symtab(struct module *mod, struct load_info *info)
 	}
 
 	/* Append room for core symbols at end of core part. */
-	info->symoffs = ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1);
-	info->stroffs = mod->core_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
-	mod->core_layout.size += strtab_size;
-	info->core_typeoffs = mod->core_layout.size;
-	mod->core_layout.size += ndst * sizeof(char);
-	mod->core_layout.size = debug_align(mod->core_layout.size);
+	info->symoffs = ALIGN(mod->data_layout.size, symsect->sh_addralign ?: 1);
+	info->stroffs = mod->data_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
+	mod->data_layout.size += strtab_size;
+	info->core_typeoffs = mod->data_layout.size;
+	mod->data_layout.size += ndst * sizeof(char);
+	mod->data_layout.size = debug_align(mod->data_layout.size);
 
 	/* Put string table section at end of init part of module. */
 	strsect->sh_flags |= SHF_ALLOC;
@@ -2770,9 +2776,9 @@ static void add_kallsyms(struct module *mod, const struct load_info *info)
 	 * Now populate the cut down core kallsyms for after init
 	 * and set types up while we still have access to sections.
 	 */
-	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;
+	mod->core_kallsyms.symtab = dst = mod->data_layout.base + info->symoffs;
+	mod->core_kallsyms.strtab = s = mod->data_layout.base + info->stroffs;
+	mod->core_kallsyms.typetab = mod->data_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);
@@ -3468,6 +3474,8 @@ static int move_module(struct module *mod, struct load_info *info)
 		if (shdr->sh_entsize & INIT_OFFSET_MASK)
 			dest = mod->init_layout.base
 				+ (shdr->sh_entsize & ~INIT_OFFSET_MASK);
+		else if (!(shdr->sh_flags & SHF_EXECINSTR))
+			dest = mod->data_layout.base + shdr->sh_entsize;
 		else
 			dest = mod->core_layout.base + shdr->sh_entsize;
 
@@ -4173,7 +4181,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
 	mutex_unlock(&module_mutex);
  free_module:
 	/* Free lock-classes; relies on the preceding sync_rcu() */
-	lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
+	lockdep_free_key_range(mod->data_layout.base, mod->data_layout.size);
 
 	module_deallocate(mod, info);
  free_copy:
Signature · 1 line
2.33.1
#6 lore raw

PATCH v3 4/6 modules: Add CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC

In reply to Christophe Leroy

Add CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC to allow architectures to request having modules data in vmalloc area instead of module area.

This is required on powerpc book3s/32 in order to set data non executable, because it is not possible to set executability on page basis, this is done per 256 Mbytes segments. The module area has exec right, vmalloc area has noexec.

This can also be useful on other powerpc/32 in order to maximize the chance of code being close enough to kernel core to avoid branch trampolines.

---
 arch/Kconfig                |  6 +++
 include/linux/module.h      |  8 ++++
 kernel/debug/kdb/kdb_main.c | 10 ++++-
 kernel/module.c             | 76 +++++++++++++++++++++++++++++++++++--
 4 files changed, 94 insertions(+), 6 deletions(-)
arch/Kconfig +6 -0
index 678a80713b21..b5d1f2c19c27 100644
@@ -882,6 +882,12 @@ config MODULES_USE_ELF_REL
 	  Modules only use ELF REL relocations.  Modules with ELF RELA
 	  relocations will give an error.
 
+config ARCH_WANTS_MODULES_DATA_IN_VMALLOC
+	bool
+	help
+	  For architectures like powerpc/32 which have constraints on module
+	  allocation and need to allocate module data outside of module area.
+
 config HAVE_IRQ_EXIT_ON_IRQ_STACK
 	bool
 	help
include/linux/module.h +8 -0
index 1e135fd5c076..3a892bdcbb5f 100644
@@ -422,6 +422,9 @@ struct module {
 	/* 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
 
 	/* Arch-specific module values */
 	struct mod_arch_specific arch;
@@ -569,6 +572,11 @@ bool is_module_text_address(unsigned long addr);
 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;
 }
kernel/debug/kdb/kdb_main.c +8 -2
index 0852a537dad4..85d3fd40b7fe 100644
@@ -2022,8 +2022,11 @@ static int kdb_lsmod(int argc, const char **argv)
 		if (mod->state == MODULE_STATE_UNFORMED)
 			continue;
 
-		kdb_printf("%-20s%8u  0x%px ", mod->name,
-			   mod->core_layout.size, (void *)mod);
+		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("  0x%px ", (void *)mod);
 #ifdef CONFIG_MODULE_UNLOAD
 		kdb_printf("%4d ", module_refcount(mod));
 #endif
@@ -2034,6 +2037,9 @@ static int kdb_lsmod(int argc, const char **argv)
 		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
 
 #ifdef CONFIG_MODULE_UNLOAD
 		{
kernel/module.c +72 -4

159 lines not on this page · raw message

Signature · 1 line
2.33.1
#11 lore raw

PATCH v3 5/6 modules: Remove module_addr_min and module_addr_max

In reply to Christophe Leroy

Replace module_addr_min and module_addr_max by
mod_tree.addr_min and mod_tree.addr_max

---
 kernel/module.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
kernel/module.c +3 -6
index f3758115ebaa..01fdc9c8a5e2 100644
@@ -89,7 +89,7 @@
  * Mutex protects:
  * 1) List of modules (also safely readable with preempt_disable),
  * 2) module_use links,
- * 3) module_addr_min/module_addr_max.
+ * 3) mod_tree.addr_min/mod_tree.addr_max.
  * (delete and add uses RCU list operations).
  */
 static DEFINE_MUTEX(module_mutex);
@@ -110,9 +110,6 @@ static struct mod_tree_root {
 	.addr_min = -1UL,
 };
 
-#define module_addr_min mod_tree.addr_min
-#define module_addr_max mod_tree.addr_max
-
 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
 static struct mod_tree_root mod_data_tree __cacheline_aligned = {
 	.addr_min = -1UL,
@@ -4611,14 +4608,14 @@ static void cfi_init(struct module *mod)
 		mod->exit = *exit;
 #endif
 
-	cfi_module_add(mod, module_addr_min);
+	cfi_module_add(mod, mod_tree.addr_min);
 #endif
 }
 
 static void cfi_cleanup(struct module *mod)
 {
 #ifdef CONFIG_CFI_CLANG
-	cfi_module_remove(mod, module_addr_min);
+	cfi_module_remove(mod, mod_tree.addr_min);
 #endif
 }
 
Signature · 1 line
2.33.1
#12 lore raw

PATCH v3 6/6 powerpc: Select ARCH_WANTS_MODULES_DATA_IN_VMALLOC on book3s/32 and 8xx

In reply to Christophe Leroy

book3s/32 and 8xx have a separate area for allocating modules, defined by MODULES_VADDR / MODULES_END.

On book3s/32, it is not possible to protect against execution on a page basis. A full 256M segment is either Exec or NoExec. The module area is in an Exec segment while vmalloc area is in a NoExec segment.

In order to protect module data against execution, select ARCH_WANTS_MODULES_DATA_IN_VMALLOC.

For the 8xx (and possibly other 32 bits platform in the future), there is no such constraint on Exec/NoExec protection, however there is a critical distance between kernel functions and callers that needs to remain below 32Mbytes in order to avoid costly trampolines. By allocating data outside of module area, we increase the chance for module text to remain within acceptable distance from kernel core text.

So select ARCH_WANTS_MODULES_DATA_IN_VMALLOC for 8xx as well.

Cc Michael Ellerman <[email protected]>
Cc Benjamin Herrenschmidt <[email protected]>
Cc Paul Mackerras <[email protected]>
---
 arch/powerpc/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/powerpc/Kconfig +1 -0
index b779603978e1..242eed8cedf8 100644
@@ -152,6 +152,7 @@ config PPC
 	select ARCH_WANT_IPC_PARSE_VERSION
 	select ARCH_WANT_IRQS_OFF_ACTIVATE_MM
 	select ARCH_WANT_LD_ORPHAN_WARN
+	select ARCH_WANTS_MODULES_DATA_IN_VMALLOC	if PPC_BOOK3S_32 || PPC_8xx
 	select ARCH_WEAK_RELEASE_ACQUIRE
 	select BINFMT_ELF
 	select BUILDTIME_TABLE_SORT
Signature · 1 line
2.33.1
maintainer
#4 lore raw

Re: [PATCH v3 3/6] modules: Introduce data_layout

In reply to Christophe Leroy
Quoted 14 lines · Christophe Leroy
kernel/module.c +2 -0
index 163e32e39064..11f51e17fb9f 100644
@@ -81,6 +81,8 @@
 /* If this is set, the section belongs in the init part of the module */
 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
 
+#define	data_layout core_layout
+
 /*
  * Mutex protects:
  * 1) List of modules (also safely readable with preempt_disable),

kernel/module.c static void layout_sections(struct module *mod, struct load_info *info) · line 2461

Quoted 9 lines
kernel/module.c +4 -1
@@ -2451,7 +2454,10 @@ static void layout_sections(struct module *mod, struct load_info *info)
 			    || s->sh_entsize != ~0UL
 			    || module_init_layout_section(sname))
 				continue;
-			s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
+			if (m)
+				s->sh_entsize = get_offset(mod, &mod->data_layout.size, s, i);
+			else
+				s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
 			pr_debug("\t%s\n", sname);

Huh why is this branching here, given you just used mod->data_layout in all other areas?

kernel/module.c static int move_module(struct module *mod, struct load_info *info) · line 3481

Quoted 9 lines
kernel/module.c +2 -0
@@ -3468,6 +3474,8 @@ static int move_module(struct module *mod, struct load_info *info)
 		if (shdr->sh_entsize & INIT_OFFSET_MASK)
 			dest = mod->init_layout.base
 				+ (shdr->sh_entsize & ~INIT_OFFSET_MASK);
+		else if (!(shdr->sh_flags & SHF_EXECINSTR))
+			dest = mod->data_layout.base + shdr->sh_entsize;
 		else
 			dest = mod->core_layout.base + shdr->sh_entsize;
 

Likewise here.

Luis

maintainer
#7 lore raw

Re: [PATCH v3 4/6] modules: Add CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC

In reply to Christophe Leroy
Quoted 28 lines · Christophe Leroy
kernel/module.c +8 -0
index 11f51e17fb9f..f3758115ebaa 100644
@@ -81,7 +81,9 @@
 /* If this is set, the section belongs in the init part of the module */
 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
 
+#ifndef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
 #define	data_layout core_layout
+#endif
 
 /*
  * Mutex protects:
@@ -111,6 +113,12 @@ static struct mod_tree_root {
 #define module_addr_min mod_tree.addr_min
 #define module_addr_max mod_tree.addr_max
 
+#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
+static struct mod_tree_root mod_data_tree __cacheline_aligned = {
+	.addr_min = -1UL,
+};
+#endif
+
 #ifdef CONFIG_MODULES_TREE_LOOKUP
 
 /*

kernel/module.c static void mod_tree_insert(struct module *mod) · line 201

Quoted 8 lines
kernel/module.c +5 -0
@@ -186,6 +194,11 @@ static void mod_tree_insert(struct module *mod)
 	__mod_tree_insert(&mod->core_layout.mtn, &mod_tree);
 	if (mod->init_layout.size)
 		__mod_tree_insert(&mod->init_layout.mtn, &mod_tree);
+
+#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
+	mod->data_layout.mtn.mod = mod;
+	__mod_tree_insert(&mod->data_layout.mtn, &mod_data_tree);
+#endif

kernel/ directory has quite a few files, module.c is the second to largest file, and it has tons of stuff. Aaron is doing work to split things out to make code easier to read and so that its easier to review changes. See:

https://lkml.kernel.org/r/[email protected]

I think this is a good patch example which could benefit from that work. So I'd much prefer to see that work go in first than this, so to see if we can make the below changes more compartamentalized.

Curious, how much testing has been put into this series?

Luis

maintainer
#13 lore raw
In reply to Christophe Leroy
Quoted 9 lines · Christophe Leroy

This series allow architectures to request having modules data in vmalloc area instead of module area.

This is required on powerpc book3s/32 in order to set data non executable, because it is not possible to set executability on page basis, this is done per 256 Mbytes segments. The module area has exec right, vmalloc area has noexec. Without this change module data remains executable regardless of CONFIG_STRICT_MODULES_RWX.

Quoted 4 lines

This can also be useful on other powerpc/32 in order to maximize the chance of code being close enough to kernel core to avoid branch trampolines.

This looks good, however I'd like to see Aaron's changes go in first, and then yours. Aaron's changes still need to be tested by 0-day and I need to finish review, but that's the order of how I'd prefer to see changes merged / tested. I'll try to review his changes, dump them to modules-next and then I'd like to trouble you to rebase ontop of that.

We should get all this tested early for the next release.

Luis

#5 lore raw

Re: [PATCH v3 3/6] modules: Introduce data_layout

In reply to Luis Chamberlain

Le 03/02/2022 à 00:48, Luis Chamberlain a écrit :

Quoted 19 lines · Christophe Leroy
Quoted 18 lines
kernel/module.c +2 -1
index 163e32e39064..11f51e17fb9f 100644
@@ -81,6 +81,8 @@
  /* If this is set, the section belongs in the init part of the module */
  #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
 
+#define	data_layout core_layout
+
  /*
   * Mutex protects:
   * 1) List of modules (also safely readable with preempt_disable),
@@ -2451,7 +2454,10 @@ static void layout_sections(struct module *mod, struct load_info *info)
  			    || s->sh_entsize != ~0UL
  			    || module_init_layout_section(sname))
  				continue;
-			s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
Quoted 8 lines

kernel/module.c static void layout_sections(struct module *mod, struct load_info *info) · line 2461

Quoted 5 lines
kernel/module.c +4 -0
+			if (m)
+				s->sh_entsize = get_offset(mod, &mod->data_layout.size, s, i);
+			else
+				s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
  			pr_debug("\t%s\n", sname);

Huh why is this branching here, given you just used mod->data_layout in all other areas?

The module text remains in core_layout, so the text section still needs core_layout. In the masks[][] table, it corresponds to the first line, which has flag SHF_EXECINSTR. In the loop that's when 'm' is 0.

In the following switch/case, case 0 still uses core_layout.

Quoted 12 lines

kernel/module.c static int move_module(struct module *mod, struct load_info *info) · line 3481

Quoted 10 lines
kernel/module.c +2 -0
@@ -3468,6 +3474,8 @@ static int move_module(struct module *mod, struct load_info *info)
  		if (shdr->sh_entsize & INIT_OFFSET_MASK)
  			dest = mod->init_layout.base
  				+ (shdr->sh_entsize & ~INIT_OFFSET_MASK);
+		else if (!(shdr->sh_flags & SHF_EXECINSTR))
+			dest = mod->data_layout.base + shdr->sh_entsize;
  		else
  			dest = mod->core_layout.base + shdr->sh_entsize;
 

Likewise here.

Same here, the section with flag SHF_EXECINSTR is a text section, it stays in core_layout.

Christophe

#8 lore raw

Re: [PATCH v3 4/6] modules: Add CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC

In reply to Luis Chamberlain

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

Quoted 44 lines · Christophe Leroy
Quoted 28 lines
kernel/module.c +8 -0
index 11f51e17fb9f..f3758115ebaa 100644
@@ -81,7 +81,9 @@
  /* If this is set, the section belongs in the init part of the module */
  #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
 
+#ifndef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
  #define	data_layout core_layout
+#endif
 
  /*
   * Mutex protects:
@@ -111,6 +113,12 @@ static struct mod_tree_root {
  #define module_addr_min mod_tree.addr_min
  #define module_addr_max mod_tree.addr_max
 
+#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
+static struct mod_tree_root mod_data_tree __cacheline_aligned = {
+	.addr_min = -1UL,
+};
+#endif
+
  #ifdef CONFIG_MODULES_TREE_LOOKUP
 
  /*

kernel/module.c static void mod_tree_insert(struct module *mod) · line 201

Quoted 8 lines
kernel/module.c +5 -0
@@ -186,6 +194,11 @@ static void mod_tree_insert(struct module *mod)
  	__mod_tree_insert(&mod->core_layout.mtn, &mod_tree);
  	if (mod->init_layout.size)
  		__mod_tree_insert(&mod->init_layout.mtn, &mod_tree);
+
+#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
+	mod->data_layout.mtn.mod = mod;
+	__mod_tree_insert(&mod->data_layout.mtn, &mod_data_tree);
+#endif

kernel/ directory has quite a few files, module.c is the second to largest file, and it has tons of stuff. Aaron is doing work to split things out to make code easier to read and so that its easier to review changes. See:

Quoted 7 lines

https://lkml.kernel.org/r/[email protected]

I think this is a good patch example which could benefit from that work. So I'd much prefer to see that work go in first than this, so to see if we can make the below changes more compartamentalized.

Curious, how much testing has been put into this series?

I tested the change up to (including) patch 4 to verify it doesn't introduce regression when not using CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC,

Then I tested with patch 5. I first tried with the 'hello world' test module. After that I loaded several important modules and checked I didn't get any regression, both with and without STRICT_MODULES_RWX and I checked the consistency in /proc/vmallocinfo
/proc/modules /sys/class/modules/*

I also tested with a hacked module_alloc() to force branch trampolines.

Christophe

maintainer
#9 lore raw

Re: [PATCH v3 4/6] modules: Add CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC

Quoted 60 lines · Christophe Leroy

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

Quoted 44 lines · Christophe Leroy
Quoted 28 lines
kernel/module.c +8 -0
index 11f51e17fb9f..f3758115ebaa 100644
@@ -81,7 +81,9 @@
  /* If this is set, the section belongs in the init part of the module */
  #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
 
+#ifndef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
  #define	data_layout core_layout
+#endif
 
  /*
   * Mutex protects:
@@ -111,6 +113,12 @@ static struct mod_tree_root {
  #define module_addr_min mod_tree.addr_min
  #define module_addr_max mod_tree.addr_max
 
+#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
+static struct mod_tree_root mod_data_tree __cacheline_aligned = {
+	.addr_min = -1UL,
+};
+#endif
+
  #ifdef CONFIG_MODULES_TREE_LOOKUP
 
  /*

kernel/module.c static void mod_tree_insert(struct module *mod) · line 201

Quoted 8 lines
kernel/module.c +5 -0
@@ -186,6 +194,11 @@ static void mod_tree_insert(struct module *mod)
  	__mod_tree_insert(&mod->core_layout.mtn, &mod_tree);
  	if (mod->init_layout.size)
  		__mod_tree_insert(&mod->init_layout.mtn, &mod_tree);
+
+#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
+	mod->data_layout.mtn.mod = mod;
+	__mod_tree_insert(&mod->data_layout.mtn, &mod_data_tree);
+#endif

kernel/ directory has quite a few files, module.c is the second to largest file, and it has tons of stuff. Aaron is doing work to split things out to make code easier to read and so that its easier to review changes. See:

Quoted 7 lines

https://lkml.kernel.org/r/[email protected]

I think this is a good patch example which could benefit from that work. So I'd much prefer to see that work go in first than this, so to see if we can make the below changes more compartamentalized.

Curious, how much testing has been put into this series?

I tested the change up to (including) patch 4 to verify it doesn't introduce regression when not using CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC,

Quoted 5 lines

Then I tested with patch 5. I first tried with the 'hello world' test module. After that I loaded several important modules and checked I didn't get any regression, both with and without STRICT_MODULES_RWX and I checked the consistency in /proc/vmallocinfo
/proc/modules /sys/class/modules/*

I wonder if we have a test for STRICT_MODULES_RWX.

Quoted 1 line

I also tested with a hacked module_alloc() to force branch trampolines.

So to verify that reducing these trampolines actually helps on an architecture? I wonder if we can generalize this somehow to let archs verify such strategies can help.

I was hoping for a bit more wider testing, like actually users, etc. It does not seem like so. So we can get to that by merging this soon into modules-next and having this bleed out issues with linux-next. We are in good time to do this now.

The kmod tree has tons of tests:

https://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git/

Can you use that to verify there are no regressions?

Aaron, Michal, if you can do the same that'd be appreciated.

Luis

#10 lore raw

Re: [PATCH v3 4/6] modules: Add CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC

Hello,

Quoted 78 lines · Luis Chamberlain
Quoted 58 lines · Christophe Leroy

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

Quoted 44 lines · Christophe Leroy
Quoted 28 lines
kernel/module.c +8 -0
index 11f51e17fb9f..f3758115ebaa 100644
@@ -81,7 +81,9 @@
  /* If this is set, the section belongs in the init part of the module */
  #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
 
+#ifndef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
  #define	data_layout core_layout
+#endif
 
  /*
   * Mutex protects:
@@ -111,6 +113,12 @@ static struct mod_tree_root {
  #define module_addr_min mod_tree.addr_min
  #define module_addr_max mod_tree.addr_max
 
+#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
+static struct mod_tree_root mod_data_tree __cacheline_aligned = {
+	.addr_min = -1UL,
+};
+#endif
+
  #ifdef CONFIG_MODULES_TREE_LOOKUP
 
  /*

kernel/module.c static void mod_tree_insert(struct module *mod) · line 201

Quoted 8 lines
kernel/module.c +5 -0
@@ -186,6 +194,11 @@ static void mod_tree_insert(struct module *mod)
  	__mod_tree_insert(&mod->core_layout.mtn, &mod_tree);
  	if (mod->init_layout.size)
  		__mod_tree_insert(&mod->init_layout.mtn, &mod_tree);
+
+#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
+	mod->data_layout.mtn.mod = mod;
+	__mod_tree_insert(&mod->data_layout.mtn, &mod_data_tree);
+#endif

kernel/ directory has quite a few files, module.c is the second to largest file, and it has tons of stuff. Aaron is doing work to split things out to make code easier to read and so that its easier to review changes. See:

Quoted 7 lines

https://lkml.kernel.org/r/[email protected]

I think this is a good patch example which could benefit from that work. So I'd much prefer to see that work go in first than this, so to see if we can make the below changes more compartamentalized.

Curious, how much testing has been put into this series?

I tested the change up to (including) patch 4 to verify it doesn't introduce regression when not using CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC,

Quoted 5 lines

Then I tested with patch 5. I first tried with the 'hello world' test module. After that I loaded several important modules and checked I didn't get any regression, both with and without STRICT_MODULES_RWX and I checked the consistency in /proc/vmallocinfo
/proc/modules /sys/class/modules/*

I wonder if we have a test for STRICT_MODULES_RWX.

Quoted 1 line

I also tested with a hacked module_alloc() to force branch trampolines.

So to verify that reducing these trampolines actually helps on an architecture? I wonder if we can generalize this somehow to let archs verify such strategies can help.

I was hoping for a bit more wider testing, like actually users, etc. It does not seem like so. So we can get to that by merging this soon into modules-next and having this bleed out issues with linux-next. We are in good time to do this now.

Quoted 5 lines

The kmod tree has tons of tests:

https://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git/

Can you use that to verify there are no regressions?

openSUSE has the testsuite packaged so it's easy to run on arbitrary kernel but only on ppc64(le) because there is no ppc there anymore.

So yes, it does not regress Book3S/64 as far as kmod testsuite is conderned and building s390x non-modular kernel also still worka but that's not saying much.

Thanks

Michal