module: replace module_layout with module_memory
Subsystems (11)
PATCH v10 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_TEXT,
MOD_DATA,
MOD_RODATA,
MOD_RO_AFTER_INIT,
MOD_INIT_TEXT,
MOD_INIT_DATA,
MOD_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.
---
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.
v9 passed build tests by kernel test bot in [2].
[1] https://lore.kernel.org/linux-mm/[email protected]/T/#u
[2] https://lore.kernel.org/linux-raid/63df0daa.eKYOEelTitBUzF+e%[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.
Changes v3 => v4:
1. Shorten enum/variable names, so that the code are easier to read. (Christophe Leroy)
2. Remove an used variable. (Guenter Roeck, Christophe Leroy)
Changes v4 => v5:
1. Simplify some code some code. (Peter Zijlstra, Christophe Leroy)
2. Remove module_check_misalignment(), which is not useful any more.
Changes v5 => v6:
1. Improve mod_mem_type_is_* and for_*mod_mem_type marcos. (Peter Zijlstra).
Changes v6 => v7:
1. Use --ignore-space-at-eol instead of -b.
Changes v7 => v8:
1. Address feedback from Peter.
Changes v8 => v9:
1. Fix a compile error for powerpc. (Christophe Leroy)
Changes v9 => v10:
1. Clean-up, style improvements, more comments. (Thomas Gleixner)
--- arch/alpha/kernel/module.c | 2 +- arch/arc/kernel/unwind.c | 9 +- arch/arm/kernel/module-plts.c | 9 +- arch/arm64/kernel/module-plts.c | 13 +- arch/ia64/kernel/module.c | 24 +- arch/mips/kernel/vpe.c | 11 +- arch/parisc/kernel/module.c | 51 ++-- arch/powerpc/kernel/module_32.c | 7 +- arch/s390/kernel/module.c | 26 +- arch/x86/kernel/callthunks.c | 4 +- arch/x86/kernel/module.c | 4 +- include/linux/module.h | 89 +++++-- kernel/module/internal.h | 40 ++-- kernel/module/kallsyms.c | 58 +++-- kernel/module/kdb.c | 17 +- kernel/module/main.c | 404 +++++++++++++++++--------------- kernel/module/procfs.c | 16 +- kernel/module/strict_rwx.c | 99 ++------ kernel/module/tree_lookup.c | 39 ++- 19 files changed, 457 insertions(+), 465 deletions(-)
arch/alpha/kernel/module.c +1 -1
@@ -148,7 +148,7 @@ 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->mem[MOD_DATA ].base + me->mem[MOD_DATA ].size -0x8000 ; got = sechdrs[me->arch.gotsecindex].sh_addr;for (i =0 ; i < n; i++) {
arch/arc/kernel/unwind.c +7 -2
@@ -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->mem[MOD_TEXT ]; + mod_mem_init_text = &module->mem[MOD_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 +2 -7
@@ -28,11 +28,6 @@ static const u32 fixed_plts[] = {#endif }; -static bool in_init (const struct module *mod,unsigned long loc) -{ -return loc - (u32 )mod->init_layout.base < mod->init_layout.size; -} -static void prealloc_fixed (struct mod_plt_sec *pltsec,struct plt_entries *plt) {int i;
@@ -50,8 +45,8 @@ static void prealloc_fixed(struct mod_plt_sec *pltsec, struct plt_entries *plt)u32 get_module_plt (struct module *mod,unsigned long loc,Elf32_Addr val) { -struct mod_plt_sec *pltsec = !in_init (mod, loc) ? &mod->arch.core : - &mod->arch.init; +struct mod_plt_sec *pltsec = !within_module_init (loc, mod) ? + &mod->arch.core : &mod->arch.init;struct plt_entries *plt;int idx;
arch/arm64/kernel/module-plts.c +4 -9
@@ -65,17 +65,12 @@ static bool plt_entries_equal(const struct plt_entry *a, (q +aarch64_insn_adrp_get_offset (le32_to_cpu (b->adrp))); } -static bool in_init (const struct module *mod,void *loc) -{ -return (u64 )loc - (u64 )mod->init_layout.base < mod->init_layout.size; -} -u64 module_emit_plt_entry (struct module *mod,Elf64_Shdr *sechdrs,void *loc,const Elf64_Rela *rela,Elf64_Sym *sym) { -struct mod_plt_sec *pltsec = !in_init (mod, loc) ? &mod->arch.core : - &mod->arch.init; +struct mod_plt_sec *pltsec = !within_module_init ((unsigned long )loc, mod) ? + &mod->arch.core : &mod->arch.init;struct plt_entry *plt = (struct plt_entry *)sechdrs[pltsec->plt_shndx].sh_addr;int i = pltsec->plt_num_entries;int j = i -1 ;
@@ -105,8 +100,8 @@ u64 module_emit_plt_entry(struct module *mod, Elf64_Shdr *sechdrs,u64 module_emit_veneer_for_adrp (struct module *mod,Elf64_Shdr *sechdrs,void *loc,u64 val) { -struct mod_plt_sec *pltsec = !in_init (mod, loc) ? &mod->arch.core : - &mod->arch.init; +struct mod_plt_sec *pltsec = !within_module_init ((unsigned long )loc, mod) ? + &mod->arch.core : &mod->arch.init;struct plt_entry *plt = (struct plt_entry *)sechdrs[pltsec->plt_shndx].sh_addr;int i = pltsec->plt_num_entries++;u32 br;
arch/ia64/kernel/module.c +14 -10
@@ -485,19 +485,19 @@ 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; +return within_module_init (addr, mod); } -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; +return within_module_core (addr, mod); } -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 +677,8 @@ 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->mem[MOD_INIT_TEXT ].base : + mod->mem[MOD_TEXT ].base);break ;case RV_LTV :
@@ -812,15 +813,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->mem[MOD_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
@@ -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->mem[MOD_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_TEXT ].size);if (!v->load_addr)return -ENOMEM ;
arch/parisc/kernel/module.c +17 -34
120 lines not on this page · raw message
arch/powerpc/kernel/module_32.c +3 -4
@@ -163,8 +163,7 @@ static uint32_t do_plt_call(void *location,pr_debug ("Doing plt for call to 0x%x at 0x%x\n" , val, (unsigned int )location);/* Init, or core PLT? */ -if (location >= mod->core_layout.base - && location < mod->core_layout.base + mod->core_layout.size) +if (within_module_core ((unsigned long )location, mod)) entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;else entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
@@ -322,14 +321,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->mem[MOD_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->mem[MOD_TEXT ].base, (unsigned long )ftrace_regs_caller, sechdrs, module);if (!module->arch.tramp_regs)
arch/s390/kernel/module.c +14 -12
@@ -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->mem[MOD_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->mem[MOD_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->mem[MOD_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->mem[MOD_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->mem[MOD_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->mem[MOD_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->mem[MOD_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);
@@ -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->mem[MOD_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 +2 -2
@@ -330,8 +330,8 @@ 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->mem[MOD_TEXT ].base, + .end = (unsigned long )mod->mem[MOD_TEXT ].base + mod->mem[MOD_TEXT ].size, .name = mod->name, };
arch/x86/kernel/module.c +2 -2
@@ -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->mem[MOD_TEXT ].base; +void *text_end = text + me->mem[MOD_TEXT ].size;alternatives_smp_module_add (me, me->name, lseg, lseg + locks->sh_size, text, text_end);
include/linux/module.h +63 -26
124 lines not on this page · raw message
kernel/module/internal.h +17 -23
@@ -17,27 +17,19 @@#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) - -#ifndef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC -#define data_layout core_layout -#endif -/* -* Modules' sections will be aligned on page boundaries -* to ensure complete separation of code and data, but -* only when CONFIG_STRICT_MODULE_RWX=y +* Use highest 4 bits of sh_entsize to store the mod_mem_type of this +* section. This leaves 28 bits for offset on 32-bit systems, which is +* about 256 MiB (WARN_ON_ONCE if we exceed that). */ -static inline unsigned int strict_align (unsigned int size) -{ -if (IS_ENABLED (CONFIG_STRICT_MODULE_RWX )) -return PAGE_ALIGN (size); -else -return size; -} + +#define SH_ENTSIZE_TYPE_BITS 4 +#define SH_ENTSIZE_TYPE_SHIFT (BITS_PER_LONG - SH_ENTSIZE_TYPE_BITS) +#define SH_ENTSIZE_TYPE_MASK ((1UL << SH_ENTSIZE_TYPE_BITS) - 1) +#define SH_ENTSIZE_OFFSET_MASK ((1UL << (BITS_PER_LONG - SH_ENTSIZE_TYPE_BITS)) - 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;
@@ -101,8 +93,8 @@ int try_to_force_load(struct module *mod, const char *reason);bool find_symbol (struct find_symbol_arg *fsa);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); +long module_get_offset_and_type (struct module *mod,enum mod_mem_type type, +Elf_Shdr *sechdr,unsigned int section);char *module_flags (struct module *mod,char *buf,bool show_state);size_t module_flags_taint (unsigned long taints,char *buf);
@@ -190,10 +182,13 @@ struct mod_tree_root {#endif unsigned long addr_min;unsigned long addr_max; +#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC +unsigned long data_addr_min; +unsigned long data_addr_max; +#endif };extern struct mod_tree_root mod_tree; -extern struct mod_tree_root mod_data_tree;#ifdef CONFIG_MODULES_TREE_LOOKUP void mod_tree_insert (struct module *mod);
@@ -224,7 +219,6 @@ void module_enable_nx(const struct module *mod);void module_enable_x (const struct module *mod);int module_enforce_rwx_sections (Elf_Ehdr *hdr,Elf_Shdr *sechdrs,char *secstrings,struct module *mod); -bool module_check_misalignment (const struct module *mod);#ifdef CONFIG_MODULE_SIG int module_sig_check (struct load_info *info,int flags);
kernel/module/kallsyms.c +33 -25
125 lines not on this page · raw message
kernel/module/kdb.c +9 -8
@@ -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->mem[MOD_TEXT ].size); +kdb_printf ("/%8u" , mod->mem[MOD_RODATA ].size); +kdb_printf ("/%8u" , mod->mem[MOD_RO_AFTER_INIT ].size); +kdb_printf ("/%8u" , mod->mem[MOD_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->mem[MOD_TEXT ].base); +kdb_printf ("/0x%px" , mod->mem[MOD_RODATA ].base); +kdb_printf ("/0x%px" , mod->mem[MOD_RO_AFTER_INIT ].base); +kdb_printf ("/0x%px" , mod->mem[MOD_DATA ].base);#ifdef CONFIG_MODULE_UNLOAD {
kernel/module/main.c +218 -186
614 lines not on this page · raw message
kernel/module/procfs.c +11 -5
@@ -62,6 +62,15 @@ 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 ; + +for_each_mod_mem_type (type) + size += 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 +82,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 +92,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->mem[MOD_TEXT ].base;seq_printf (m," 0x%px" , value);/* Taints info */
kernel/module/strict_rwx.c +18 -81
127 lines not on this page · raw message
kernel/module/tree_lookup.c +17 -22
@@ -21,16 +21,16 @@static __always_inline unsigned long __mod_tree_val (struct latch_tree_node *n) { - struct module_layout *layout = container_of(n, struct module_layout, mtn.node); + struct module_memory *mod_mem = container_of(n, struct module_memory, mtn.node); -return (unsigned long )layout->base; +return (unsigned long )mod_mem->base; }static __always_inline unsigned long __mod_tree_size (struct latch_tree_node *n) { - struct module_layout *layout = container_of(n, struct module_layout, mtn.node); + struct module_memory *mod_mem = container_of(n, struct module_memory, mtn.node); -return (unsigned long )layout->size; +return (unsigned long )mod_mem->size; }static __always_inline bool
@@ -77,32 +77,27 @@ static void __mod_tree_remove(struct mod_tree_node *node, struct mod_tree_root **/ 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); -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 +for_each_mod_mem_type (type) { + mod->mem[type].mtn.mod = mod; +if (mod->mem[type].size) +__mod_tree_insert (&mod->mem[type].mtn, &mod_tree); + } }void mod_tree_remove_init (struct module *mod) { -if (mod->init_layout.size) -__mod_tree_remove (&mod->init_layout.mtn, &mod_tree); + for_class_mod_mem_type(type, init) { +if (mod->mem[type].size) +__mod_tree_remove (&mod->mem[type].mtn, &mod_tree); + } }void mod_tree_remove (struct module *mod) { -__mod_tree_remove (&mod->core_layout.mtn, &mod_tree); -mod_tree_remove_init (mod); -#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC -__mod_tree_remove (&mod->data_layout.mtn, &mod_data_tree); -#endif +for_each_mod_mem_type (type) { +if (mod->mem[type].size) +__mod_tree_remove (&mod->mem[type].mtn, &mod_tree); + } }struct module *mod_find (unsigned long addr,struct mod_tree_root *tree)
Signature · 1 line
2.30.2
Hi Luis
Quoted 23 lines · Song Liu
(quoted diff) +21
-0+static void free_mod_mem(struct module *mod) +{ + /* free the memory in the right order to avoid use-after-free */ + static enum mod_mem_type mod_mem_free_order[MOD_MEM_NUM_TYPES] = { + /* first free init sections */ + MOD_INIT_TEXT, + MOD_INIT_DATA, + MOD_INIT_RODATA, + + /* then core sections, except rw data */ + MOD_TEXT, + MOD_RODATA, + MOD_RO_AFTER_INIT, + + /* last, rw data */ + MOD_DATA, + }; + int i; + + BUILD_BUG_ON(ARRAY_SIZE(mod_mem_free_order) != MOD_MEM_NUM_TYPES); +
Review
Quoted 10 lines
(quoted diff) +10
-0+ for (i = 0; i < MOD_MEM_NUM_TYPES; i++) { + enum mod_mem_type type = mod_mem_free_order[i]; + struct module_memory *mod_mem = &mod->mem[type]; + + /* Free lock-classes; relies on the preceding sync_rcu(). */ + lockdep_free_key_range(mod_mem->base, mod_mem->size); + if (mod_mem->size) + module_memory_free(mod_mem->base, type); + } +}
If there are no other comments, would you mind make the following change when applying the patch?
Thanks,
Song
kernel/module/main.c +0 -0
@@ -1211,7 +1211,7 @@ static void free_mod_mem(struct module *mod)
MOD_RODATA,
MOD_RO_AFTER_INIT,
- /* last, rw data */
+ /* rw data need to be freed last, as it hosts mod */
MOD_DATA, }; int i;
Quoted 34 lines · Song Liu
module_layoutmanages 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 usesmodule_layoutfor 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_layoutper module with up to 7module_memoryper module:
MOD_TEXT,
MOD_DATA,
MOD_RODATA,
MOD_RO_AFTER_INIT,
MOD_INIT_TEXT,
MOD_INIT_DATA,
MOD_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_layoutfor different data. These data are put into differentmodule_memorybased on their location inmodule_layout. IOW, data that used to go with text is allocated withMOD_MEM_TYPE_TEXT; data that used to go with data is allocated withMOD_MEM_TYPE_DATA, etc.
module_memorysimplifies quite some of the module code. For example,ARCH_WANTS_MODULES_DATA_IN_VMALLOCis a lot cleaner, as it just uses a different allocator for the data.kernel/module/strict_rwx.cis also much cleaner withmodule_memory.
Quoted 6 lines
Signed-off-by Song Liu <[email protected]>Cc Luis Chamberlain <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Guenter Roeck <[email protected]>Cc Christophe Leroy <[email protected]>
With the comment update folded in:
Luis Chamberlain
maintainerQuoted 42 lines · Thomas Gleixner
Quoted 34 lines · Song Liu
module_layoutmanages 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 usesmodule_layoutfor 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_layoutper module with up to 7module_memoryper module:
MOD_TEXT,
MOD_DATA,
MOD_RODATA,
MOD_RO_AFTER_INIT,
MOD_INIT_TEXT,
MOD_INIT_DATA,
MOD_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_layoutfor different data. These data are put into differentmodule_memorybased on their location inmodule_layout. IOW, data that used to go with text is allocated withMOD_MEM_TYPE_TEXT; data that used to go with data is allocated withMOD_MEM_TYPE_DATA, etc.
module_memorysimplifies quite some of the module code. For example,ARCH_WANTS_MODULES_DATA_IN_VMALLOCis a lot cleaner, as it just uses a different allocator for the data.kernel/module/strict_rwx.cis also much cleaner withmodule_memory.Quoted 6 lines
Signed-off-by Song Liu <[email protected]>Cc Luis Chamberlain <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Guenter Roeck <[email protected]>Cc Christophe Leroy <[email protected]>
Quoted 3 lines
With the comment update folded in:
Reviewed-by Thomas Gleixner <[email protected]>
Thanks! I've merged this onto modules-next for winder testing. It is too late for v6.2 as we're on rc7 but this just means we'll get testing on linux-next for a good while for v6.3.
If other folks do complete review please send your Reviewed-by annotation so I can ammend the commit log as we go.
Luis
Le 07/02/2023 à 01:28, Song Liu a écrit :
Quoted 131 lines
module_layoutmanages 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 usesmodule_layoutfor 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_layoutper module with up to 7module_memoryper module:
MOD_TEXT,
MOD_DATA,
MOD_RODATA,
MOD_RO_AFTER_INIT,
MOD_INIT_TEXT,
MOD_INIT_DATA,
MOD_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_layoutfor different data. These data are put into differentmodule_memorybased on their location inmodule_layout. IOW, data that used to go with text is allocated withMOD_MEM_TYPE_TEXT; data that used to go with data is allocated withMOD_MEM_TYPE_DATA, etc.
module_memorysimplifies quite some of the module code. For example,ARCH_WANTS_MODULES_DATA_IN_VMALLOCis a lot cleaner, as it just uses a different allocator for the data.kernel/module/strict_rwx.cis also much cleaner withmodule_memory.Signed-off-by Song Liu <[email protected]>Cc Luis Chamberlain <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Guenter Roeck <[email protected]>Cc Christophe Leroy <[email protected]>---
This is the preparation work for the type awaremodule_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.
v9 passed build tests by kernel test bot in [2].[1] https://lore.kernel.org/linux-mm/[email protected]/T/#u
[2] https://lore.kernel.org/linux-raid/63df0daa.eKYOEelTitBUzF+e%[email protected]/T/#uChanges v1 => v2:
1. Add data_addr_[min|max] forCONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOCcase.Changes v2 => v3:
1. Fix and remove the last use ofINIT_OFFSET_MASK.
2. Add more information in the commit log. (Luis Chamberlain)
3. Rebase and fix issues in x86/calltrunks.
4. Minor cleanup.Changes v3 => v4:
1. Shorten enum/variable names, so that the code are easier to read. (Christophe Leroy)
2. Remove an used variable. (Guenter Roeck, Christophe Leroy)Changes v4 => v5:
1. Simplify some code some code. (Peter Zijlstra, Christophe Leroy)
2. Removemodule_check_misalignment(), which is not useful any more.Changes v5 => v6:
1. Improve mod_mem_type_is_* and for_*mod_mem_type marcos. (Peter Zijlstra).Changes v6 => v7:
1. Use --ignore-space-at-eol instead of -b.Changes v7 => v8:
1. Address feedback from Peter.Changes v8 => v9:
1. Fix a compile error for powerpc. (Christophe Leroy)Changes v9 => v10:
1. Clean-up, style improvements, more comments. (Thomas Gleixner)--- arch/alpha/kernel/module.c | 2 +- arch/arc/kernel/unwind.c | 9 +- arch/arm/kernel/module-plts.c | 9 +- arch/arm64/kernel/module-plts.c | 13 +- arch/ia64/kernel/module.c | 24 +- arch/mips/kernel/vpe.c | 11 +- arch/parisc/kernel/module.c | 51 ++-- arch/powerpc/kernel/module_32.c | 7 +- arch/s390/kernel/module.c | 26 +- arch/x86/kernel/callthunks.c | 4 +- arch/x86/kernel/module.c | 4 +- include/linux/module.h | 89 +++++-- kernel/module/internal.h | 40 ++-- kernel/module/kallsyms.c | 58 +++-- kernel/module/kdb.c | 17 +- kernel/module/main.c | 404 +++++++++++++++++--------------- kernel/module/procfs.c | 16 +- kernel/module/strict_rwx.c | 99 ++------ kernel/module/tree_lookup.c | 39 ++- 19 files changed, 457 insertions(+), 465 deletions(-)arch/alpha/kernel/module.c +1
-1@@ -148,7 +148,7 @@ 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->mem[MOD_DATA ].base + me->mem[MOD_DATA ].size -0x8000 ; got = sechdrs[me->arch.gotsecindex].sh_addr;for (i =0 ; i < n; i++) {arch/arc/kernel/unwind.c +0
-0
arch/arc/kernel/unwind.c void *unwind_add_table(struct module *module, const void *table_start, · line 373
Quoted 5 lines
arch/arc/kernel/unwind.c +2
-0@@ -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;
This function is small (35 lines) so no need to have so big names for local functions, see
https://docs.kernel.org/process/coding-style.html#naming
struct module_memory *core_text; struct module_memory *init_text;
Quoted 7 lines
arch/arc/kernel/unwind.c +0
-0if (table_size <=0 )return NULL ;@@ -377,9 +379,12 @@ void *unwind_add_table(struct module *module, const void *table_start,if (!table)return NULL ;
arch/arc/kernel/unwind.c void *unwind_add_table(struct module *module, const void *table_start, · line 389
Quoted 10 lines
arch/arc/kernel/unwind.c +5
-2+ mod_mem_core_text = &module->mem[MOD_TEXT ]; + mod_mem_init_text = &module->mem[MOD_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 );
Shorter naming allows you to reduce the number of lines of the above function call:
init_unwind_table(table, module->name, core_text->base, core_text->size, init_text->base, init_text->size, table_start, table_size, NULL, 0);
Quoted 23 lines
kernel/module/main.c +1
-7@@ -80,12 +80,6 @@ struct mod_tree_root mod_tree __cacheline_aligned = { .addr_min = -1UL, }; -#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC -struct mod_tree_root mod_data_tree __cacheline_aligned = { - .addr_min =-1UL , -}; -#endif -struct symsearch {const struct kernel_symbol *start, *stop;const s32 *crcs;@@ -93,14 +87,24 @@ struct symsearch { };/* -* Bounds of module text, for speeding up __module_address. +* Bounds of module memory, for speeding up __module_address.
kernel/module/main.c struct symsearch { · line 99
Quoted 10 lines
kernel/module/main.c +3
-1* Protected by module_mutex. */ -static void __mod_update_bounds (void *base,unsigned int size,struct mod_tree_root *tree) +static void __mod_update_bounds (enum mod_mem_type type__maybe_unused ,void *base, +unsigned int size,struct mod_tree_root *tree) {unsigned long min = (unsigned long )base;unsigned long max = min + size; +#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
A #ifdef shouldn't be required. You can use IS_ENABLED() instead:
kernel/module/main.c struct symsearch { · line 100
Quoted 1 line
kernel/module/main.c +1
-0+if (mod_mem_type_is_core_data (type)) {
if (IS_ENABLED(CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC) && mod_mem_type_is_core_data(type))
Quoted 30 lines
kernel/module/main.c +13
-6+if (min < tree->data_addr_min) + tree->data_addr_min = min; +if (max > tree->data_addr_max) + tree->data_addr_max = max; + return; + } +#endif if (min < tree->addr_min) tree->addr_min = min;if (max > tree->addr_max)@@ -109,12 +113,12 @@ static void __mod_update_bounds(void *base, unsigned int size, struct mod_tree_rstatic void mod_update_bounds (struct module *mod) { -__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_tree); -#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC -__mod_update_bounds (mod->data_layout.base, mod->data_layout.size, &mod_data_tree); -#endif +for_each_mod_mem_type (type) { +struct module_memory *mod_mem = &mod->mem[type]; + +if (mod_mem->size) +__mod_update_bounds (type, mod_mem->base, mod_mem->size, &mod_tree); + } }/* Block module loading/unloading? */
kernel/module/main.c static ssize_t store_uevent(struct module_attribute *mattr, · line 930
Quoted 4 lines
kernel/module/main.c +1
-0@@ -923,12 +927,27 @@ 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
It should be possible to have only one show_coresize() function, with IS_ENABLED(CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC) ; see
https://docs.kernel.org/process/coding-style.html#conditional-compilation
Quoted 51 lines
kernel/module/main.c +25
-3+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); +return sprintf (buffer,"%u\n" , mk->mod->mem[MOD_TEXT ].size); } +#else + +static ssize_t show_coresize (struct module_attribute *mattr, +struct module_kobject *mk,char *buffer) +{ +unsigned int size =0 ; + +for_class_mod_mem_type (type, core) + size += mk->mod->mem[type].size; +return sprintf (buffer,"%u\n" , size); +} +#endif +static struct module_attribute modinfo_coresize =__ATTR (coresize,0444 , show_coresize,NULL );@@ -936,7 +955,11 @@ static struct module_attribute modinfo_coresize =static ssize_t show_datasize (struct module_attribute *mattr,struct module_kobject *mk,char *buffer) { -return sprintf (buffer,"%u\n" , mk->mod->data_layout.size); +unsigned int size =0 ; + +for_class_mod_mem_type (type, core_data) + size += mk->mod->mem[type].size; +return sprintf (buffer,"%u\n" , size); } static struct module_attribute modinfo_datasize =@@ -946,7 +969,11 @@ static struct module_attribute modinfo_datasize =static ssize_t show_initsize (struct module_attribute *mattr,struct module_kobject *mk,char *buffer) { -return sprintf (buffer,"%u\n" , mk->mod->init_layout.size); +unsigned int size =0 ; + +for_class_mod_mem_type (type, init) + size += mk->mod->mem[type].size; +return sprintf (buffer,"%u\n" , size); } static struct module_attribute modinfo_initsize =
kernel/module/main.c void __weak module_arch_freeing_init(struct module *mod) · line 1173
Quoted 4 lines
kernel/module/main.c +1
-0@@ -1143,6 +1170,65 @@ void __weak module_arch_freeing_init(struct module *mod) { } +#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
Same, could simply be
return IS_ENABLED(CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC) && mod_mem_type_is_core_data(type);
Quoted 19 lines
kernel/module/main.c +19
-0+static bool mod_mem_use_vmalloc (enum mod_mem_type type) +{ +return mod_mem_type_is_core_data (type); +} +#else +static bool mod_mem_use_vmalloc (enum mod_mem_type type) +{ +return false; +} +#endif + +static void *module_memory_alloc (unsigned int size,enum mod_mem_type type) +{ +if (mod_mem_use_vmalloc (type)) +return vzalloc (size); +return module_alloc (size); +} + +static void module_memory_free (void *ptr,enum mod_mem_type type)
kernel/module/main.c void __weak module_arch_freeing_init(struct module *mod) · line 1202
Quoted 10 lines
kernel/module/main.c +10
-0+{ +if (mod_mem_use_vmalloc (type)) +vfree (ptr); +else +module_memfree (ptr); +} + +static void free_mod_mem (struct module *mod) +{ +/* free the memory in the right order to avoid use-after-free */
Instead of 'right order', explain what the right order is. As far as I understand it is only to free MOD_DATA last. Everything else doesn't matter.
Quoted 68 lines
kernel/module/main.c +45
-6+static enum mod_mem_type mod_mem_free_order[MOD_MEM_NUM_TYPES ] = { +/* first free init sections */ +MOD_INIT_TEXT , +MOD_INIT_DATA , +MOD_INIT_RODATA , + +/* then core sections, except rw data */ +MOD_TEXT , +MOD_RODATA , +MOD_RO_AFTER_INIT , + +/* last, rw data */ +MOD_DATA , + }; +int i; + +BUILD_BUG_ON (ARRAY_SIZE (mod_mem_free_order) !=MOD_MEM_NUM_TYPES ); + +for (i =0 ; i <MOD_MEM_NUM_TYPES ; i++) { +enum mod_mem_type type = mod_mem_free_order[i]; +struct module_memory *mod_mem = &mod->mem[type]; + +/* Free lock-classes; relies on the preceding sync_rcu(). */ +lockdep_free_key_range (mod_mem->base, mod_mem->size); +if (mod_mem->size) +module_memory_free (mod_mem->base, type); + } +} +/* Free a module, remove from lists, etc. */ static void free_module (struct module *mod) {@@ -1428,84 +1504,63 @@ static void layout_sections(struct module *mod, struct load_info *info) { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL }, { ARCH_SHF_SMALL | SHF_ALLOC, 0 } }; -unsigned int m, i; - -for (i =0 ; i < info->hdr->e_shnum; i++) - info->sechdrs[i].sh_entsize = ~0UL ; +static const int core_m_to_mem_type[] = { +MOD_TEXT , +MOD_RODATA , +MOD_RO_AFTER_INIT , +MOD_DATA , +MOD_INVALID ,/* This is needed to match the masks array */ + }; +static const int init_m_to_mem_type[] = { +MOD_INIT_TEXT , +MOD_INIT_RODATA , +MOD_INVALID , +MOD_INIT_DATA , +MOD_INVALID ,/* This is needed to match the masks array */ + }; -pr_debug ("Core section allocation order:\n" );for (m =0 ; m <ARRAY_SIZE (masks); ++m) { +enum mod_mem_type type = is_init ? init_m_to_mem_type[m] : core_m_to_mem_type[m]; +for (i =0 ; i < info->hdr->e_shnum; ++i) {Elf_Shdr *s = &info->sechdrs[i];const char *sname = info->secstrings + s->sh_name; -unsigned int *sizep;if ((s->sh_flags & masks[m][0 ]) != masks[m][0 ] || (s->sh_flags & masks[m][1 ]) || s->sh_entsize != ~0UL
kernel/module/main.c static void layout_sections(struct module *mod, struct load_info *info) · line 1454
Quoted 10 lines
kernel/module/main.c +1
-8- || module_init_layout_section(sname)) + || is_init != module_init_layout_section(sname))continue ; - sizep = m ? &mod->data_layout.size : &mod->core_layout.size; - s->sh_entsize =module_get_offset (mod, sizep, s, i); -pr_debug ("\t%s\n" , sname); - } -switch (m) { -case 0 :/* executable */ - mod->core_layout.size =strict_align (mod->core_layout.size);
Where is the strict alignment done now ?
Quoted 157 lines
kernel/module/main.c +38
-87- mod->core_layout.text_size = mod->core_layout.size; -break ; -case 1 :/* RO: text and ro-data */ - mod->data_layout.size =strict_align (mod->data_layout.size); - mod->data_layout.ro_size = mod->data_layout.size; -break ; -case 2 :/* RO after init */ - mod->data_layout.size =strict_align (mod->data_layout.size); - mod->data_layout.ro_after_init_size = mod->data_layout.size; -break ; -case 4 :/* whole core */ - mod->data_layout.size =strict_align (mod->data_layout.size); -break ; - } - } -pr_debug ("Init section allocation order:\n" ); -for (m =0 ; m <ARRAY_SIZE (masks); ++m) { -for (i =0 ; i < info->hdr->e_shnum; ++i) { -Elf_Shdr *s = &info->sechdrs[i]; -const char *sname = info->secstrings + s->sh_name; - -if ((s->sh_flags & masks[m][0 ]) != masks[m][0 ] - || (s->sh_flags & masks[m][1 ]) - || s->sh_entsize != ~0UL - || !module_init_layout_section (sname)) +if (WARN_ON_ONCE (type ==MOD_INVALID ))continue ; - s->sh_entsize = (module_get_offset (mod, &mod->init_layout.size, s, i) - |INIT_OFFSET_MASK ); + + s->sh_entsize =module_get_offset_and_type (mod, type, s, i);pr_debug ("\t%s\n" , sname); } -switch (m) { -case 0 :/* executable */ - mod->init_layout.size =strict_align (mod->init_layout.size); - mod->init_layout.text_size = mod->init_layout.size; -break ; -case 1 :/* RO: text and ro-data */ - mod->init_layout.size =strict_align (mod->init_layout.size); - mod->init_layout.ro_size = mod->init_layout.size; -break ; -case 2 : -/* -* RO after init doesn't apply to init_layout (only -* core_layout), so it just takes the value of ro_size. -*/ - mod->init_layout.ro_after_init_size = mod->init_layout.ro_size; -break ; -case 4 :/* whole init */ - mod->init_layout.size =strict_align (mod->init_layout.size); -break ; - } } } +/* +* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld +* might -- code, read-only data, read-write data, small data. Tally +* sizes, and place the offsets into sh_entsize fields: high bit means it +* belongs in init. +*/ +static void layout_sections (struct module *mod,struct load_info *info) +{ +unsigned int i; + +for (i =0 ; i < info->hdr->e_shnum; i++) + info->sechdrs[i].sh_entsize = ~0UL ; + +pr_debug ("Core section allocation order:\n" ); +__layout_sections (mod, info, false); + +pr_debug ("Init section allocation order:\n" ); +__layout_sections (mod, info, true); +} +static void set_license (struct module *mod,const char *license) {if (!license)@@ -2122,72 +2177,42 @@ static int move_module(struct module *mod, struct load_info *info) {int i;void *ptr; +enum mod_mem_type t; -/* Do the allocs. */ - ptr =module_alloc (mod->core_layout.size); -/* -* The pointer to this block is stored in the module structure -* which is inside the block. Just mark it as not being a -* leak. -*/ -kmemleak_not_leak (ptr); -if (!ptr) -return -ENOMEM ; - -memset (ptr,0 , mod->core_layout.size); - mod->core_layout.base = ptr; +for_each_mod_mem_type (type) { +if (!mod->mem[type].size) { + mod->mem[type].base =NULL ; +continue ; + } + mod->mem[type].size =PAGE_ALIGN (mod->mem[type].size); + ptr =module_memory_alloc (mod->mem[type].size, type); -if (mod->init_layout.size) { - ptr =module_alloc (mod->init_layout.size);/* * The pointer to this block is stored in the module structure -* which is inside the block. This block doesn't need to be -* scanned as it contains data and code that will be freed -* after the module is initialized. +* which is inside the block. Just mark it as not being a +* leak. */ kmemleak_ignore (ptr);if (!ptr) { -module_memfree (mod->core_layout.base); -return -ENOMEM ; + t = type; + goto out_enomem; } -memset (ptr,0 , mod->init_layout.size); - mod->init_layout.base = ptr; - }else - mod->init_layout.base =NULL ; - -#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC -/* Do the allocs. */ - ptr =vzalloc (mod->data_layout.size); -/* -* The pointer to this block is stored in the module structure -* which is inside the block. Just mark it as not being a -* leak. -*/ -kmemleak_not_leak (ptr); -if (!ptr) { -module_memfree (mod->core_layout.base); -module_memfree (mod->init_layout.base); -return -ENOMEM ; +memset (ptr,0 , mod->mem[type].size); + mod->mem[type].base = ptr; } - mod->data_layout.base = ptr; -#endif /* Transfer each section which specifies SHF_ALLOC */ pr_debug ("final section addresses:\n" );for (i =0 ; i < info->hdr->e_shnum; i++) {void *dest;Elf_Shdr *shdr = &info->sechdrs[i]; +enum mod_mem_type type = shdr->sh_entsize >>SH_ENTSIZE_TYPE_SHIFT ;if (!(shdr->sh_flags &SHF_ALLOC ))continue ;
kernel/module/main.c static int move_module(struct module *mod, struct load_info *info) · line 2215
Quoted 10 lines
kernel/module/main.c +2
-7-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; + dest = mod->mem[type].base + + (shdr->sh_entsize &SH_ENTSIZE_OFFSET_MASK );
Can't that fit on a single line ?
Quoted 5 lines
kernel/module/main.c +0
-0if (shdr->sh_type !=SHT_NOBITS )memcpy (dest, (void *)shdr->sh_addr, shdr->sh_size);
kernel/module/main.c bool is_module_address(unsigned long addr) · line 3098
Quoted 10 lines
kernel/module/main.c +2
-2@@ -3060,20 +3091,21 @@ bool is_module_address(unsigned long addr)struct module *__module_address (unsigned long addr) {struct module *mod; -struct mod_tree_root *tree;if (addr >= mod_tree.addr_min && addr <= mod_tree.addr_max) - tree = &mod_tree; + goto lookup; +#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
Can we try to avoid that #ifdef ?
I know that means getting data_addr_min and data_addr_max alwyas existing, maybe through an unnamed union or a macro or a static inline helper ?
Quoted 18 lines
kernel/module/main.c +6
-5- else if (addr >= mod_data_tree.addr_min && addr <= mod_data_tree.addr_max) - tree = &mod_data_tree; +if (addr >= mod_tree.data_addr_min && addr <= mod_tree.data_addr_max) + goto lookup;#endif - else - return NULL; +return NULL ; + +lookup:module_assert_mutex_or_preempt (); - mod =mod_find (addr, tree); + mod =mod_find (addr, &mod_tree);if (mod) {BUG_ON (!within_module (addr, mod));if (mod->state ==MODULE_STATE_UNFORMED )
Quoted 3 lines · Christophe Leroy
[...]
Quoted 15 lines
arch/arc/kernel/unwind.c void *unwind_add_table(struct module *module, const void *table_start, · line 373
Quoted 10 lines
arch/arc/kernel/unwind.c +2
-0@@ -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;This function is small (35 lines) so no need to have so big names for local functions, see
https://docs.kernel.org/process/coding-style.html#naming
Quoted 2 lines
struct
module_memory*core_text;
structmodule_memory*init_text;
Will fix.
[...]
Quoted 14 lines
Quoted 14 lines
arch/arc/kernel/unwind.c +1
-1/* -* Bounds of module text, for speeding up __module_address. +* Bounds of module memory, for speeding up __module_address. * Protected by module_mutex. */
arch/arc/kernel/unwind.c +2
-1-static void __mod_update_bounds (void *base,unsigned int size,struct mod_tree_root *tree) +static void __mod_update_bounds (enum mod_mem_type type__maybe_unused ,void *base, +unsigned int size,struct mod_tree_root *tree) {unsigned long min = (unsigned long )base;unsigned long max = min + size;
Quoted 3 lines
Quoted 1 line
+#ifdef
CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOCA #ifdef shouldn't be required. You can use
IS_ENABLED()instead:
Will fix.
Quoted 7 lines
Quoted 4 lines
+ if (
mod_mem_type_is_core_data(type)) {if (
IS_ENABLED(CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC)&&
mod_mem_type_is_core_data(type))
[...]
Quoted 5 lines
Quoted 3 lines
- switch (m) {
- case 0: /* executable */
-mod->core_layout.size=strict_align(mod->core_layout.size);Where is the strict alignment done now ?
AFAICT, each of these memory regions are allocated separately, so they are always page aligned, no?
Quoted 16 lines
Quoted 16 lines
-
mod->core_layout.text_size=mod->core_layout.size;
- break;
- case 1: /* RO: text and ro-data */
-mod->data_layout.size=strict_align(mod->data_layout.size);
-mod->data_layout.ro_size=mod->data_layout.size;
- break;
- case 2: /* RO after init */
-mod->data_layout.size=strict_align(mod->data_layout.size);
-mod->data_layout.ro_after_init_size=mod->data_layout.size;
- break;
- case 4: /* whole core */
-mod->data_layout.size=strict_align(mod->data_layout.size);
- break;
- }
- }
[...]
Quoted 11 lines
Quoted 11 lines
if (shdr->sh_type != SHT_NOBITS) memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);arch/arc/kernel/unwind.c +0
-1@@ -3060,20 +3091,21 @@ bool is_module_address(unsigned long addr)struct module *__module_address (unsigned long addr) {struct module *mod; -struct mod_tree_root *tree;
Quoted 10 lines
arch/arc/kernel/unwind.c bool is_module_address(unsigned long addr) · line 3098
Quoted 5 lines
arch/arc/kernel/unwind.c +2
-1if (addr >= mod_tree.addr_min && addr <= mod_tree.addr_max) - tree = &mod_tree; + goto lookup; +#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC Can we try to avoid that #ifdef ?
I know that means gettingdata_addr_minanddata_addr_maxalwyas existing, maybe through an unnamed union or a macro or a static inline helper ?
IIUC, we want __module_address() to be as fast as possible. So #ifdef is probably the best solution here?
Thanks,
Song
Quoted 19 lines
Quoted 19 lines
arch/arc/kernel/unwind.c +6
-5- else if (addr >= mod_data_tree.addr_min && addr <= mod_data_tree.addr_max) - tree = &mod_data_tree; +if (addr >= mod_tree.data_addr_min && addr <= mod_tree.data_addr_max) + goto lookup;#endif - else - return NULL; +return NULL ; + +lookup:module_assert_mutex_or_preempt (); - mod =mod_find (addr, tree); + mod =mod_find (addr, &mod_tree);if (mod) {BUG_ON (!within_module (addr, mod));if (mod->state ==MODULE_STATE_UNFORMED )
Quoted 3 lines · Christophe Leroy
[...]
Quoted 6 lines
Quoted 5 lines
{
unsigned long min = (unsigned long)base;
unsigned long max = min + size;+#ifdef
CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
Quoted 16 lines
A #ifdef shouldn't be required. You can use
IS_ENABLED()instead:Quoted 1 line
+ if (
mod_mem_type_is_core_data(type)) {if (
IS_ENABLED(CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC)&&
mod_mem_type_is_core_data(type))Quoted 7 lines
+ if (min <
tree->data_addr_min)
+tree->data_addr_min= min;
+ if (max >tree->data_addr_max)
+tree->data_addr_max= max;
+ return;
+ }
+#endif
To use IS_ENABLED() here, we also need to keep data_addr_[min|max] around. Do we really want them?
Btw, we will think about all these details again with the type aware module_alloc().
[...]
Quoted 9 lines
kernel/module/main.c void __weak module_arch_freeing_init(struct module *mod) · line 1202
Quoted 5 lines
kernel/module/main.c +4
-0+ +static void free_mod_mem (struct module *mod) +{ +/* free the memory in the right order to avoid use-after-free */ Instead of 'right order', explain what the right order is. As far as I understand it is only to free
MOD_DATAlast. Everything else doesn't matter.
I rewrote the function as:
static void free_mod_mem(struct module *mod)
{
for_each_mod_mem_type(type) {
struct module_memory *mod_mem = &mod->mem[type];
/* free MOD_DATA at the end, as it hosts mod */
if (type == MOD_DATA)
continue;
/* Free lock-classes; relies on the preceding sync_rcu(). */
lockdep_free_key_range(mod_mem->base, mod_mem->size);
if (mod_mem->size)
module_memory_free(mod_mem->base, type);
}
/* free MOD_DATA at the end, as it hosts mod */
lockdep_free_key_range(mod->mem[MOD_DATA].base, mod->mem[MOD_DATA].size);
module_memory_free(mod->mem[MOD_DATA].base, MOD_DATA);
}Does this look good?
Thanks,
Song
[...]
Le 08/02/2023 à 22:39, Song Liu a écrit :
Quoted 58 lines
Quoted 3 lines · Christophe Leroy
[...]
Quoted 15 lines
arch/arc/kernel/unwind.c void *unwind_add_table(struct module *module, const void *table_start, · line 373
Quoted 10 lines
arch/arc/kernel/unwind.c +2
-0@@ -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;This function is small (35 lines) so no need to have so big names for local functions, see
https://docs.kernel.org/process/coding-style.html#namingQuoted 2 lines
struct
module_memory*core_text;
structmodule_memory*init_text;Will fix.
[...]
Quoted 14 lines
Quoted 14 lines
arch/arc/kernel/unwind.c +1
-1/* -* Bounds of module text, for speeding up __module_address. +* Bounds of module memory, for speeding up __module_address. * Protected by module_mutex. */arch/arc/kernel/unwind.c +2
-1-static void __mod_update_bounds (void *base,unsigned int size,struct mod_tree_root *tree) +static void __mod_update_bounds (enum mod_mem_type type__maybe_unused ,void *base, +unsigned int size,struct mod_tree_root *tree) {unsigned long min = (unsigned long )base;unsigned long max = min + size;Quoted 3 lines
Quoted 1 line
+#ifdef
CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOCA #ifdef shouldn't be required. You can use
IS_ENABLED()instead:Will fix.
Quoted 7 lines
Quoted 4 lines
+ if (
mod_mem_type_is_core_data(type)) {if (
IS_ENABLED(CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC)&&
mod_mem_type_is_core_data(type))[...]
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
- switch (m) {
- case 0: /* executable */
-mod->core_layout.size=strict_align(mod->core_layout.size);Where is the strict alignment done now ?
AFAICT, each of these memory regions are allocated separately, so they are always page aligned, no?
Ah, OK.
It should be OK then.
Quoted 42 lines
Quoted 17 lines
Quoted 17 lines
-
mod->core_layout.text_size=mod->core_layout.size;
- break;
- case 1: /* RO: text and ro-data */
-mod->data_layout.size=strict_align(mod->data_layout.size);
-mod->data_layout.ro_size=mod->data_layout.size;
- break;
- case 2: /* RO after init */
-mod->data_layout.size=strict_align(mod->data_layout.size);
-mod->data_layout.ro_after_init_size=mod->data_layout.size;
- break;
- case 4: /* whole core */
-mod->data_layout.size=strict_align(mod->data_layout.size);
- break;
- }
- }[...]
Quoted 11 lines
Quoted 11 lines
if (shdr->sh_type != SHT_NOBITS) memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);arch/arc/kernel/unwind.c +0
-1@@ -3060,20 +3091,21 @@ bool is_module_address(unsigned long addr)struct module *__module_address (unsigned long addr) {struct module *mod; -struct mod_tree_root *tree;Quoted 10 lines
arch/arc/kernel/unwind.c bool is_module_address(unsigned long addr) · line 3098
Quoted 5 lines
arch/arc/kernel/unwind.c +2
-1if (addr >= mod_tree.addr_min && addr <= mod_tree.addr_max) - tree = &mod_tree; + goto lookup; +#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC Can we try to avoid that #ifdef ?
I know that means gettingdata_addr_minanddata_addr_maxalwyas existing, maybe through an unnamed union or a macro or a static inline helper ?
Quoted 2 lines
IIUC, we want
__module_address()to be as fast as possible. So #ifdef is probably the best solution here?
An if/else with IS_ENABLED(CONFIG_SOMETHING) is folded at build time, so it makes no difference with an #ifdef in terms of performance.
However it has the advantage of making the entire code visible to the compiler at all times, which means that you don't have to build it several times with difference value for CONFIG_SOMETHING in order to see whether your code is valid or not.
As an exemple, if you had written mod_mem_use_vmalloc() with IS_ENABLED() instead of an #ifdef, you would have detected the problem in v8 even without building with CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
Quoted 24 lines
Thanks,
SongQuoted 19 lines
Quoted 19 lines
arch/arc/kernel/unwind.c +6
-5- else if (addr >= mod_data_tree.addr_min && addr <= mod_data_tree.addr_max) - tree = &mod_data_tree; +if (addr >= mod_tree.data_addr_min && addr <= mod_tree.data_addr_max) + goto lookup;#endif - else - return NULL; +return NULL ; + +lookup:module_assert_mutex_or_preempt (); - mod =mod_find (addr, tree); + mod =mod_find (addr, &mod_tree);if (mod) {BUG_ON (!within_module (addr, mod));if (mod->state ==MODULE_STATE_UNFORMED )
Le 09/02/2023 à 01:16, Song Liu a écrit :
Quoted 29 lines
Quoted 3 lines · Christophe Leroy
[...]
Quoted 6 lines
Quoted 5 lines
{ unsigned long min = (unsigned long)base; unsigned long max = min + size;+#ifdef
CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOCQuoted 16 lines
A #ifdef shouldn't be required. You can use
IS_ENABLED()instead:Quoted 1 line
+ if (
mod_mem_type_is_core_data(type)) {if (
IS_ENABLED(CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC)&&
mod_mem_type_is_core_data(type))Quoted 7 lines
+ if (min <
tree->data_addr_min)
+tree->data_addr_min= min;
+ if (max >tree->data_addr_max)
+tree->data_addr_max= max;
+ return;
+ }
+#endif
Quoted 2 lines
To use
IS_ENABLED()here, we also need to keep data_addr_[min|max] around. Do we really want them?
It is up to you. If you think it is not worth the effort, it's fine for me.
Allthough it could probably be easily fixed by doing (untested) :
struct mod_tree_root {
#ifdef CONFIG_MODULES_TREE_LOOKUP
struct latch_tree_root root;
#endif
#ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
unsigned long addr_min;
unsigned long addr_max;
unsigned long data_addr_min;
unsigned long data_addr_max;
#else
union {
unsigned long addr_min;
unsigned long data_addr_min;
};
union {
unsigned long addr_max;
unsigned long data_addr_max;
}:#endif
};
Quoted 44 lines
Btw, we will think about all these details again with the type aware
module_alloc().[...]
Quoted 9 lines
kernel/module/main.c void __weak module_arch_freeing_init(struct module *mod) · line 1202
Quoted 5 lines
kernel/module/main.c +4
-0+ +static void free_mod_mem (struct module *mod) +{ +/* free the memory in the right order to avoid use-after-free */ Instead of 'right order', explain what the right order is. As far as I understand it is only to free
MOD_DATAlast. Everything else doesn't matter.I rewrote the function as:
static void
free_mod_mem(struct module *mod){ for_each_mod_mem_type(type) { struct module_memory *mod_mem = &mod->mem[type]; /* free MOD_DATA at the end, as it hosts mod */ if (type == MOD_DATA) continue; /* Free lock-classes; relies on the preceding sync_rcu(). */ lockdep_free_key_range(mod_mem->base, mod_mem->size); if (mod_mem->size) module_memory_free(mod_mem->base, type); } /* free MOD_DATA at the end, as it hosts mod */ lockdep_free_key_range(mod->mem[MOD_DATA].base, mod->mem[MOD_DATA].size); module_memory_free(mod->mem[MOD_DATA].base, MOD_DATA); }Does this look good?
Thanks,
Song[...]
Quoted 55 lines · Christophe Leroy
Quoted 29 lines · Song Liu
Quoted 3 lines · Christophe Leroy
[...]
Quoted 6 lines
Quoted 5 lines
{
unsigned long min = (unsigned long)base;
unsigned long max = min + size;+#ifdef
CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOCQuoted 16 lines
A #ifdef shouldn't be required. You can use
IS_ENABLED()instead:Quoted 1 line
+ if (
mod_mem_type_is_core_data(type)) {if (
IS_ENABLED(CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC)&&
mod_mem_type_is_core_data(type))Quoted 7 lines
+ if (min <
tree->data_addr_min)
+tree->data_addr_min= min;
+ if (max >tree->data_addr_max)
+tree->data_addr_max= max;
+ return;
+ }
+#endifQuoted 2 lines
To use
IS_ENABLED()here, we also need to keep data_addr_[min|max] around. Do we really want them?It is up to you. If you think it is not worth the effort, it's fine for me.
Allthough it could probably be easily fixed by doing (untested) :
struct
mod_tree_root{
#ifdefCONFIG_MODULES_TREE_LOOKUP
structlatch_tree_rootroot;
#endif
#ifdefCONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
unsigned longaddr_min;
unsigned longaddr_max;
unsigned longdata_addr_min;
unsigned longdata_addr_max;
#else
union {
unsigned longaddr_min;
Quoted 8 lines
unsigned long
data_addr_min;
};
union {
unsigned longaddr_max;
unsigned longdata_addr_max;
}:
#endif
};
I think using union here will be an overkill. Let's just keep it simple with #ifdef.
Thanks,
Song