Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'modules-6.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux

Pull module updates from Luis Chamberlain:
"The summary of the changes for this pull requests is:

- Song Liu's new struct module_memory replacement

- Nick Alcock's MODULE_LICENSE() removal for non-modules

- My cleanups and enhancements to reduce the areas where we vmalloc
module memory for duplicates, and the respective debug code which
proves the remaining vmalloc pressure comes from userspace.

Most of the changes have been in linux-next for quite some time except
the minor fixes I made to check if a module was already loaded prior
to allocating the final module memory with vmalloc and the respective
debug code it introduces to help clarify the issue. Although the
functional change is small it is rather safe as it can only *help*
reduce vmalloc space for duplicates and is confirmed to fix a bootup
issue with over 400 CPUs with KASAN enabled. I don't expect stable
kernels to pick up that fix as the cleanups would have also had to
have been picked up. Folks on larger CPU systems with modules will
want to just upgrade if vmalloc space has been an issue on bootup.

Given the size of this request, here's some more elaborate details:

The functional change change in this pull request is the very first
patch from Song Liu which replaces the 'struct module_layout' with a
new 'struct module_memory'. The old data structure tried to put
together all types of supported module memory types in one data
structure, the new one abstracts the differences in memory types in a
module to allow each one to provide their own set of details. This
paves the way in the future so we can deal with them in a cleaner way.
If you look at changes they also provide a nice cleanup of how we
handle these different memory areas in a module. This change has been
in linux-next since before the merge window opened for v6.3 so to
provide more than a full kernel cycle of testing. It's a good thing as
quite a bit of fixes have been found for it.

Jason Baron then made dynamic debug a first class citizen module user
by using module notifier callbacks to allocate / remove module
specific dynamic debug information.

Nick Alcock has done quite a bit of work cross-tree to remove module
license tags from things which cannot possibly be module at my request
so to:

a) help him with his longer term tooling goals which require a
deterministic evaluation if a piece a symbol code could ever be
part of a module or not. But quite recently it is has been made
clear that tooling is not the only one that would benefit.
Disambiguating symbols also helps efforts such as live patching,
kprobes and BPF, but for other reasons and R&D on this area is
active with no clear solution in sight.

b) help us inch closer to the now generally accepted long term goal
of automating all the MODULE_LICENSE() tags from SPDX license tags

In so far as a) is concerned, although module license tags are a no-op
for non-modules, tools which would want create a mapping of possible
modules can only rely on the module license tag after the commit
8b41fc4454e ("kbuild: create modules.builtin without
Makefile.modbuiltin or tristate.conf").

Nick has been working on this *for years* and AFAICT I was the only
one to suggest two alternatives to this approach for tooling. The
complexity in one of my suggested approaches lies in that we'd need a
possible-obj-m and a could-be-module which would check if the object
being built is part of any kconfig build which could ever lead to it
being part of a module, and if so define a new define
-DPOSSIBLE_MODULE [0].

A more obvious yet theoretical approach I've suggested would be to
have a tristate in kconfig imply the same new -DPOSSIBLE_MODULE as
well but that means getting kconfig symbol names mapping to modules
always, and I don't think that's the case today. I am not aware of
Nick or anyone exploring either of these options. Quite recently Josh
Poimboeuf has pointed out that live patching, kprobes and BPF would
benefit from resolving some part of the disambiguation as well but for
other reasons. The function granularity KASLR (fgkaslr) patches were
mentioned but Joe Lawrence has clarified this effort has been dropped
with no clear solution in sight [1].

In the meantime removing module license tags from code which could
never be modules is welcomed for both objectives mentioned above. Some
developers have also welcomed these changes as it has helped clarify
when a module was never possible and they forgot to clean this up, and
so you'll see quite a bit of Nick's patches in other pull requests for
this merge window. I just picked up the stragglers after rc3. LWN has
good coverage on the motivation behind this work [2] and the typical
cross-tree issues he ran into along the way. The only concrete blocker
issue he ran into was that we should not remove the MODULE_LICENSE()
tags from files which have no SPDX tags yet, even if they can never be
modules. Nick ended up giving up on his efforts due to having to do
this vetting and backlash he ran into from folks who really did *not
understand* the core of the issue nor were providing any alternative /
guidance. I've gone through his changes and dropped the patches which
dropped the module license tags where an SPDX license tag was missing,
it only consisted of 11 drivers. To see if a pull request deals with a
file which lacks SPDX tags you can just use:

./scripts/spdxcheck.py -f \
$(git diff --name-only commid-id | xargs echo)

You'll see a core module file in this pull request for the above, but
that's not related to his changes. WE just need to add the SPDX
license tag for the kernel/module/kmod.c file in the future but it
demonstrates the effectiveness of the script.

Most of Nick's changes were spread out through different trees, and I
just picked up the slack after rc3 for the last kernel was out. Those
changes have been in linux-next for over two weeks.

The cleanups, debug code I added and final fix I added for modules
were motivated by David Hildenbrand's report of boot failing on a
systems with over 400 CPUs when KASAN was enabled due to running out
of virtual memory space. Although the functional change only consists
of 3 lines in the patch "module: avoid allocation if module is already
present and ready", proving that this was the best we can do on the
modules side took quite a bit of effort and new debug code.

The initial cleanups I did on the modules side of things has been in
linux-next since around rc3 of the last kernel, the actual final fix
for and debug code however have only been in linux-next for about a
week or so but I think it is worth getting that code in for this merge
window as it does help fix / prove / evaluate the issues reported with
larger number of CPUs. Userspace is not yet fixed as it is taking a
bit of time for folks to understand the crux of the issue and find a
proper resolution. Worst come to worst, I have a kludge-of-concept [3]
of how to make kernel_read*() calls for modules unique / converge
them, but I'm currently inclined to just see if userspace can fix this
instead"

Link: https://lore.kernel.org/all/Y/kXDqW+7d71C4wz@bombadil.infradead.org/ [0]
Link: https://lkml.kernel.org/r/025f2151-ce7c-5630-9b90-98742c97ac65@redhat.com [1]
Link: https://lwn.net/Articles/927569/ [2]
Link: https://lkml.kernel.org/r/20230414052840.1994456-3-mcgrof@kernel.org [3]

* tag 'modules-6.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux: (121 commits)
module: add debugging auto-load duplicate module support
module: stats: fix invalid_mod_bytes typo
module: remove use of uninitialized variable len
module: fix building stats for 32-bit targets
module: stats: include uapi/linux/module.h
module: avoid allocation if module is already present and ready
module: add debug stats to help identify memory pressure
module: extract patient module check into helper
modules/kmod: replace implementation with a semaphore
Change DEFINE_SEMAPHORE() to take a number argument
module: fix kmemleak annotations for non init ELF sections
module: Ignore L0 and rename is_arm_mapping_symbol()
module: Move is_arm_mapping_symbol() to module_symbol.h
module: Sync code of is_arm_mapping_symbol()
scripts/gdb: use mem instead of core_layout to get the module address
interconnect: remove module-related code
interconnect: remove MODULE_LICENSE in non-modules
zswap: remove MODULE_LICENSE in non-modules
zpool: remove MODULE_LICENSE in non-modules
x86/mm/dump_pagetables: remove MODULE_LICENSE in non-modules
...

+1989 -1041
+6
Documentation/admin-guide/kernel-parameters.txt
··· 3363 3363 specified, <module>.async_probe takes precedence for 3364 3364 the specific module. 3365 3365 3366 + module.enable_dups_trace 3367 + [KNL] When CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS is set, 3368 + this means that duplicate request_module() calls will 3369 + trigger a WARN_ON() instead of a pr_warn(). Note that 3370 + if MODULE_DEBUG_AUTOLOAD_DUPS_TRACE is set, WARN_ON()s 3371 + will always be issued and this option does nothing. 3366 3372 module.sig_enforce 3367 3373 [KNL] When CONFIG_MODULE_SIG is set, this means that 3368 3374 modules without (valid) signatures will fail to load.
+21 -3
Documentation/core-api/kernel-api.rst
··· 220 220 Module Support 221 221 ============== 222 222 223 - Module Loading 224 - -------------- 223 + Kernel module auto-loading 224 + -------------------------- 225 225 226 - .. kernel-doc:: kernel/kmod.c 226 + .. kernel-doc:: kernel/module/kmod.c 227 227 :export: 228 + 229 + Module debugging 230 + ---------------- 231 + 232 + .. kernel-doc:: kernel/module/stats.c 233 + :doc: module debugging statistics overview 234 + 235 + dup_failed_modules - tracks duplicate failed modules 236 + **************************************************** 237 + 238 + .. kernel-doc:: kernel/module/stats.c 239 + :doc: dup_failed_modules - tracks duplicate failed modules 240 + 241 + module statistics debugfs counters 242 + ********************************** 243 + 244 + .. kernel-doc:: kernel/module/stats.c 245 + :doc: module statistics debugfs counters 228 246 229 247 Inter Module support 230 248 --------------------
+1 -1
Documentation/translations/zh_CN/core-api/kernel-api.rst
··· 226 226 227 227 该API在以下内核代码中: 228 228 229 - kernel/kmod.c 229 + kernel/module/kmod.c 230 230 231 231 模块接口支持 232 232 ------------
+3 -10
MAINTAINERS
··· 11601 11601 F: mm/kmemleak.c 11602 11602 F: samples/kmemleak/kmemleak-test.c 11603 11603 11604 - KMOD KERNEL MODULE LOADER - USERMODE HELPER 11605 - M: Luis Chamberlain <mcgrof@kernel.org> 11606 - L: linux-kernel@vger.kernel.org 11607 - L: linux-modules@vger.kernel.org 11608 - S: Maintained 11609 - F: include/linux/kmod.h 11610 - F: kernel/kmod.c 11611 - F: lib/test_kmod.c 11612 - F: tools/testing/selftests/kmod/ 11613 - 11614 11604 KMSAN 11615 11605 M: Alexander Potapenko <glider@google.com> 11616 11606 R: Marco Elver <elver@google.com> ··· 14176 14186 S: Maintained 14177 14187 T: git git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git modules-next 14178 14188 F: include/linux/module.h 14189 + F: include/linux/kmod.h 14179 14190 F: kernel/module/ 14180 14191 F: scripts/module* 14192 + F: lib/test_kmod.c 14193 + F: tools/testing/selftests/kmod/ 14181 14194 14182 14195 MONOLITHIC POWER SYSTEM PMIC DRIVER 14183 14196 M: Saravanan Sekar <sravanhome@gmail.com>
+7 -5
arch/arc/kernel/unwind.c
··· 369 369 unsigned long table_size) 370 370 { 371 371 struct unwind_table *table; 372 + struct module_memory *core_text; 373 + struct module_memory *init_text; 372 374 373 375 if (table_size <= 0) 374 376 return NULL; ··· 379 377 if (!table) 380 378 return NULL; 381 379 382 - init_unwind_table(table, module->name, 383 - module->core_layout.base, module->core_layout.size, 384 - module->init_layout.base, module->init_layout.size, 385 - table_start, table_size, 386 - NULL, 0); 380 + core_text = &module->mem[MOD_TEXT]; 381 + init_text = &module->mem[MOD_INIT_TEXT]; 382 + 383 + init_unwind_table(table, module->name, core_text->base, core_text->size, 384 + init_text->base, init_text->size, table_start, table_size, NULL, 0); 387 385 388 386 init_unwind_hdr(table, unw_hdr_alloc); 389 387
+1
arch/arm/boot/compressed/decompress.c
··· 31 31 /* Not needed, but used in some headers pulled in by decompressors */ 32 32 extern char * strstr(const char * s1, const char *s2); 33 33 extern size_t strlen(const char *s); 34 + extern int strcmp(const char *cs, const char *ct); 34 35 extern int memcmp(const void *cs, const void *ct, size_t count); 35 36 extern char * strchrnul(const char *, int); 36 37
+2 -7
arch/arm/kernel/module-plts.c
··· 28 28 #endif 29 29 }; 30 30 31 - static bool in_init(const struct module *mod, unsigned long loc) 32 - { 33 - return loc - (u32)mod->init_layout.base < mod->init_layout.size; 34 - } 35 - 36 31 static void prealloc_fixed(struct mod_plt_sec *pltsec, struct plt_entries *plt) 37 32 { 38 33 int i; ··· 45 50 46 51 u32 get_module_plt(struct module *mod, unsigned long loc, Elf32_Addr val) 47 52 { 48 - struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core : 49 - &mod->arch.init; 53 + struct mod_plt_sec *pltsec = !within_module_init(loc, mod) ? 54 + &mod->arch.core : &mod->arch.init; 50 55 struct plt_entries *plt; 51 56 int idx; 52 57
+4 -9
arch/arm64/kernel/module-plts.c
··· 65 65 (q + aarch64_insn_adrp_get_offset(le32_to_cpu(b->adrp))); 66 66 } 67 67 68 - static bool in_init(const struct module *mod, void *loc) 69 - { 70 - return (u64)loc - (u64)mod->init_layout.base < mod->init_layout.size; 71 - } 72 - 73 68 u64 module_emit_plt_entry(struct module *mod, Elf64_Shdr *sechdrs, 74 69 void *loc, const Elf64_Rela *rela, 75 70 Elf64_Sym *sym) 76 71 { 77 - struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core : 78 - &mod->arch.init; 72 + struct mod_plt_sec *pltsec = !within_module_init((unsigned long)loc, mod) ? 73 + &mod->arch.core : &mod->arch.init; 79 74 struct plt_entry *plt = (struct plt_entry *)sechdrs[pltsec->plt_shndx].sh_addr; 80 75 int i = pltsec->plt_num_entries; 81 76 int j = i - 1; ··· 100 105 u64 module_emit_veneer_for_adrp(struct module *mod, Elf64_Shdr *sechdrs, 101 106 void *loc, u64 val) 102 107 { 103 - struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core : 104 - &mod->arch.init; 108 + struct mod_plt_sec *pltsec = !within_module_init((unsigned long)loc, mod) ? 109 + &mod->arch.core : &mod->arch.init; 105 110 struct plt_entry *plt = (struct plt_entry *)sechdrs[pltsec->plt_shndx].sh_addr; 106 111 int i = pltsec->plt_num_entries++; 107 112 u32 br;
+14 -10
arch/ia64/kernel/module.c
··· 485 485 return 0; 486 486 } 487 487 488 - static inline int 488 + static inline bool 489 489 in_init (const struct module *mod, uint64_t addr) 490 490 { 491 - return addr - (uint64_t) mod->init_layout.base < mod->init_layout.size; 491 + return within_module_init(addr, mod); 492 492 } 493 493 494 - static inline int 494 + static inline bool 495 495 in_core (const struct module *mod, uint64_t addr) 496 496 { 497 - return addr - (uint64_t) mod->core_layout.base < mod->core_layout.size; 497 + return within_module_core(addr, mod); 498 498 } 499 499 500 - static inline int 500 + static inline bool 501 501 is_internal (const struct module *mod, uint64_t value) 502 502 { 503 503 return in_init(mod, value) || in_core(mod, value); ··· 677 677 break; 678 678 679 679 case RV_BDREL: 680 - val -= (uint64_t) (in_init(mod, val) ? mod->init_layout.base : mod->core_layout.base); 680 + val -= (uint64_t) (in_init(mod, val) ? mod->mem[MOD_INIT_TEXT].base : 681 + mod->mem[MOD_TEXT].base); 681 682 break; 682 683 683 684 case RV_LTV: ··· 813 812 * addresses have been selected... 814 813 */ 815 814 uint64_t gp; 816 - if (mod->core_layout.size > MAX_LTOFF) 815 + struct module_memory *mod_mem; 816 + 817 + mod_mem = &mod->mem[MOD_DATA]; 818 + if (mod_mem->size > MAX_LTOFF) 817 819 /* 818 820 * This takes advantage of fact that SHF_ARCH_SMALL gets allocated 819 821 * at the end of the module. 820 822 */ 821 - gp = mod->core_layout.size - MAX_LTOFF / 2; 823 + gp = mod_mem->size - MAX_LTOFF / 2; 822 824 else 823 - gp = mod->core_layout.size / 2; 824 - gp = (uint64_t) mod->core_layout.base + ((gp + 7) & -8); 825 + gp = mod_mem->size / 2; 826 + gp = (uint64_t) mod_mem->base + ((gp + 7) & -8); 825 827 mod->arch.gp = gp; 826 828 DEBUGP("%s: placing gp at 0x%lx\n", __func__, gp); 827 829 }
+1 -1
arch/mips/cavium-octeon/setup.c
··· 72 72 static unsigned long long max_memory = ULLONG_MAX; 73 73 static unsigned long long reserve_low_mem; 74 74 75 - DEFINE_SEMAPHORE(octeon_bootbus_sem); 75 + DEFINE_SEMAPHORE(octeon_bootbus_sem, 1); 76 76 EXPORT_SYMBOL(octeon_bootbus_sem); 77 77 78 78 static struct octeon_boot_descriptor *octeon_boot_desc_ptr;
+5 -6
arch/mips/kernel/vpe.c
··· 199 199 for (m = 0; m < ARRAY_SIZE(masks); ++m) { 200 200 for (i = 0; i < hdr->e_shnum; ++i) { 201 201 Elf_Shdr *s = &sechdrs[i]; 202 + struct module_memory *mod_mem; 203 + 204 + mod_mem = &mod->mem[MOD_TEXT]; 202 205 203 206 if ((s->sh_flags & masks[m][0]) != masks[m][0] 204 207 || (s->sh_flags & masks[m][1]) 205 208 || s->sh_entsize != ~0UL) 206 209 continue; 207 210 s->sh_entsize = 208 - get_offset((unsigned long *)&mod->core_layout.size, s); 211 + get_offset((unsigned long *)&mod_mem->size, s); 209 212 } 210 - 211 - if (m == 0) 212 - mod->core_layout.text_size = mod->core_layout.size; 213 - 214 213 } 215 214 } 216 215 ··· 640 641 layout_sections(&mod, hdr, sechdrs, secstrings); 641 642 } 642 643 643 - v->load_addr = alloc_progmem(mod.core_layout.size); 644 + v->load_addr = alloc_progmem(mod.mem[MOD_TEXT].size); 644 645 if (!v->load_addr) 645 646 return -ENOMEM; 646 647
+17 -34
arch/parisc/kernel/module.c
··· 27 27 * We are not doing SEGREL32 handling correctly. According to the ABI, we 28 28 * should do a value offset, like this: 29 29 * if (in_init(me, (void *)val)) 30 - * val -= (uint32_t)me->init_layout.base; 30 + * val -= (uint32_t)me->mem[MOD_INIT_TEXT].base; 31 31 * else 32 - * val -= (uint32_t)me->core_layout.base; 32 + * val -= (uint32_t)me->mem[MOD_TEXT].base; 33 33 * However, SEGREL32 is used only for PARISC unwind entries, and we want 34 34 * those entries to have an absolute address, and not just an offset. 35 35 * ··· 75 75 * which gives us a maximum positive displacement of 0x7fff, and as such 76 76 * allows us to allocate up to 4095 GOT entries. */ 77 77 #define MAX_GOTS 4095 78 - 79 - /* three functions to determine where in the module core 80 - * or init pieces the location is */ 81 - static inline int in_init(struct module *me, void *loc) 82 - { 83 - return (loc >= me->init_layout.base && 84 - loc <= (me->init_layout.base + me->init_layout.size)); 85 - } 86 - 87 - static inline int in_core(struct module *me, void *loc) 88 - { 89 - return (loc >= me->core_layout.base && 90 - loc <= (me->core_layout.base + me->core_layout.size)); 91 - } 92 - 93 - static inline int in_local(struct module *me, void *loc) 94 - { 95 - return in_init(me, loc) || in_core(me, loc); 96 - } 97 78 98 79 #ifndef CONFIG_64BIT 99 80 struct got_entry { ··· 283 302 { 284 303 unsigned long gots = 0, fdescs = 0, len; 285 304 unsigned int i; 305 + struct module_memory *mod_mem; 286 306 287 307 len = hdr->e_shnum * sizeof(me->arch.section[0]); 288 308 me->arch.section = kzalloc(len, GFP_KERNEL); ··· 328 346 me->arch.section[s].stub_entries += count; 329 347 } 330 348 349 + mod_mem = &me->mem[MOD_TEXT]; 331 350 /* align things a bit */ 332 - me->core_layout.size = ALIGN(me->core_layout.size, 16); 333 - me->arch.got_offset = me->core_layout.size; 334 - me->core_layout.size += gots * sizeof(struct got_entry); 351 + mod_mem->size = ALIGN(mod_mem->size, 16); 352 + me->arch.got_offset = mod_mem->size; 353 + mod_mem->size += gots * sizeof(struct got_entry); 335 354 336 - me->core_layout.size = ALIGN(me->core_layout.size, 16); 337 - me->arch.fdesc_offset = me->core_layout.size; 338 - me->core_layout.size += fdescs * sizeof(Elf_Fdesc); 355 + mod_mem->size = ALIGN(mod_mem->size, 16); 356 + me->arch.fdesc_offset = mod_mem->size; 357 + mod_mem->size += fdescs * sizeof(Elf_Fdesc); 339 358 340 359 me->arch.got_max = gots; 341 360 me->arch.fdesc_max = fdescs; ··· 354 371 355 372 BUG_ON(value == 0); 356 373 357 - got = me->core_layout.base + me->arch.got_offset; 374 + got = me->mem[MOD_TEXT].base + me->arch.got_offset; 358 375 for (i = 0; got[i].addr; i++) 359 376 if (got[i].addr == value) 360 377 goto out; ··· 372 389 #ifdef CONFIG_64BIT 373 390 static Elf_Addr get_fdesc(struct module *me, unsigned long value) 374 391 { 375 - Elf_Fdesc *fdesc = me->core_layout.base + me->arch.fdesc_offset; 392 + Elf_Fdesc *fdesc = me->mem[MOD_TEXT].base + me->arch.fdesc_offset; 376 393 377 394 if (!value) { 378 395 printk(KERN_ERR "%s: zero OPD requested!\n", me->name); ··· 390 407 391 408 /* Create new one */ 392 409 fdesc->addr = value; 393 - fdesc->gp = (Elf_Addr)me->core_layout.base + me->arch.got_offset; 410 + fdesc->gp = (Elf_Addr)me->mem[MOD_TEXT].base + me->arch.got_offset; 394 411 return (Elf_Addr)fdesc; 395 412 } 396 413 #endif /* CONFIG_64BIT */ ··· 725 742 loc, val); 726 743 val += addend; 727 744 /* can we reach it locally? */ 728 - if (in_local(me, (void *)val)) { 745 + if (within_module(val, me)) { 729 746 /* this is the case where the symbol is local 730 747 * to the module, but in a different section, 731 748 * so stub the jump in case it's more than 22 ··· 784 801 break; 785 802 case R_PARISC_FPTR64: 786 803 /* 64-bit function address */ 787 - if(in_local(me, (void *)(val + addend))) { 804 + if (within_module(val + addend, me)) { 788 805 *loc64 = get_fdesc(me, val+addend); 789 806 pr_debug("FDESC for %s at %llx points to %llx\n", 790 807 strtab + sym->st_name, *loc64, ··· 822 839 823 840 table = (unsigned char *)sechdrs[me->arch.unwind_section].sh_addr; 824 841 end = table + sechdrs[me->arch.unwind_section].sh_size; 825 - gp = (Elf_Addr)me->core_layout.base + me->arch.got_offset; 842 + gp = (Elf_Addr)me->mem[MOD_TEXT].base + me->arch.got_offset; 826 843 827 844 pr_debug("register_unwind_table(), sect = %d at 0x%p - 0x%p (gp=0x%lx)\n", 828 845 me->arch.unwind_section, table, end, gp); ··· 960 977 #ifdef CONFIG_64BIT 961 978 void *dereference_module_function_descriptor(struct module *mod, void *ptr) 962 979 { 963 - unsigned long start_opd = (Elf64_Addr)mod->core_layout.base + 980 + unsigned long start_opd = (Elf64_Addr)mod->mem[MOD_TEXT].base + 964 981 mod->arch.fdesc_offset; 965 982 unsigned long end_opd = start_opd + 966 983 mod->arch.fdesc_count * sizeof(Elf64_Fdesc);
+3 -4
arch/powerpc/kernel/module_32.c
··· 163 163 164 164 pr_debug("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location); 165 165 /* Init, or core PLT? */ 166 - if (location >= mod->core_layout.base 167 - && location < mod->core_layout.base + mod->core_layout.size) 166 + if (within_module_core((unsigned long)location, mod)) 168 167 entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr; 169 168 else 170 169 entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr; ··· 321 322 322 323 int module_finalize_ftrace(struct module *module, const Elf_Shdr *sechdrs) 323 324 { 324 - module->arch.tramp = do_plt_call(module->core_layout.base, 325 + module->arch.tramp = do_plt_call(module->mem[MOD_TEXT].base, 325 326 (unsigned long)ftrace_caller, 326 327 sechdrs, module); 327 328 if (!module->arch.tramp) 328 329 return -ENOENT; 329 330 330 331 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS 331 - module->arch.tramp_regs = do_plt_call(module->core_layout.base, 332 + module->arch.tramp_regs = do_plt_call(module->mem[MOD_TEXT].base, 332 333 (unsigned long)ftrace_regs_caller, 333 334 sechdrs, module); 334 335 if (!module->arch.tramp_regs)
+14 -12
arch/s390/kernel/module.c
··· 126 126 Elf_Rela *rela; 127 127 char *strings; 128 128 int nrela, i, j; 129 + struct module_memory *mod_mem; 129 130 130 131 /* Find symbol table and string table. */ 131 132 symtab = NULL; ··· 174 173 175 174 /* Increase core size by size of got & plt and set start 176 175 offsets for got and plt. */ 177 - me->core_layout.size = ALIGN(me->core_layout.size, 4); 178 - me->arch.got_offset = me->core_layout.size; 179 - me->core_layout.size += me->arch.got_size; 180 - me->arch.plt_offset = me->core_layout.size; 176 + mod_mem = &me->mem[MOD_TEXT]; 177 + mod_mem->size = ALIGN(mod_mem->size, 4); 178 + me->arch.got_offset = mod_mem->size; 179 + mod_mem->size += me->arch.got_size; 180 + me->arch.plt_offset = mod_mem->size; 181 181 if (me->arch.plt_size) { 182 182 if (IS_ENABLED(CONFIG_EXPOLINE) && !nospec_disable) 183 183 me->arch.plt_size += PLT_ENTRY_SIZE; 184 - me->core_layout.size += me->arch.plt_size; 184 + mod_mem->size += me->arch.plt_size; 185 185 } 186 186 return 0; 187 187 } ··· 306 304 case R_390_GOTPLT64: /* 64 bit offset to jump slot. */ 307 305 case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */ 308 306 if (info->got_initialized == 0) { 309 - Elf_Addr *gotent = me->core_layout.base + 307 + Elf_Addr *gotent = me->mem[MOD_TEXT].base + 310 308 me->arch.got_offset + 311 309 info->got_offset; 312 310 ··· 331 329 rc = apply_rela_bits(loc, val, 0, 64, 0, write); 332 330 else if (r_type == R_390_GOTENT || 333 331 r_type == R_390_GOTPLTENT) { 334 - val += (Elf_Addr) me->core_layout.base - loc; 332 + val += (Elf_Addr) me->mem[MOD_TEXT].base - loc; 335 333 rc = apply_rela_bits(loc, val, 1, 32, 1, write); 336 334 } 337 335 break; ··· 347 345 char *plt_base; 348 346 char *ip; 349 347 350 - plt_base = me->core_layout.base + me->arch.plt_offset; 348 + plt_base = me->mem[MOD_TEXT].base + me->arch.plt_offset; 351 349 ip = plt_base + info->plt_offset; 352 350 *(int *)insn = 0x0d10e310; /* basr 1,0 */ 353 351 *(int *)&insn[4] = 0x100c0004; /* lg 1,12(1) */ ··· 377 375 val - loc + 0xffffUL < 0x1ffffeUL) || 378 376 (r_type == R_390_PLT32DBL && 379 377 val - loc + 0xffffffffULL < 0x1fffffffeULL))) 380 - val = (Elf_Addr) me->core_layout.base + 378 + val = (Elf_Addr) me->mem[MOD_TEXT].base + 381 379 me->arch.plt_offset + 382 380 info->plt_offset; 383 381 val += rela->r_addend - loc; ··· 399 397 case R_390_GOTOFF32: /* 32 bit offset to GOT. */ 400 398 case R_390_GOTOFF64: /* 64 bit offset to GOT. */ 401 399 val = val + rela->r_addend - 402 - ((Elf_Addr) me->core_layout.base + me->arch.got_offset); 400 + ((Elf_Addr) me->mem[MOD_TEXT].base + me->arch.got_offset); 403 401 if (r_type == R_390_GOTOFF16) 404 402 rc = apply_rela_bits(loc, val, 0, 16, 0, write); 405 403 else if (r_type == R_390_GOTOFF32) ··· 409 407 break; 410 408 case R_390_GOTPC: /* 32 bit PC relative offset to GOT. */ 411 409 case R_390_GOTPCDBL: /* 32 bit PC rel. off. to GOT shifted by 1. */ 412 - val = (Elf_Addr) me->core_layout.base + me->arch.got_offset + 410 + val = (Elf_Addr) me->mem[MOD_TEXT].base + me->arch.got_offset + 413 411 rela->r_addend - loc; 414 412 if (r_type == R_390_GOTPC) 415 413 rc = apply_rela_bits(loc, val, 1, 32, 0, write); ··· 517 515 !nospec_disable && me->arch.plt_size) { 518 516 unsigned int *ij; 519 517 520 - ij = me->core_layout.base + me->arch.plt_offset + 518 + ij = me->mem[MOD_TEXT].base + me->arch.plt_offset + 521 519 me->arch.plt_size - PLT_ENTRY_SIZE; 522 520 ij[0] = 0xc6000000; /* exrl %r0,.+10 */ 523 521 ij[1] = 0x0005a7f4; /* j . */
+1 -4
arch/x86/crypto/blake2s-glue.c
··· 8 8 #include <linux/types.h> 9 9 #include <linux/jump_label.h> 10 10 #include <linux/kernel.h> 11 - #include <linux/module.h> 12 11 #include <linux/sizes.h> 13 12 14 13 #include <asm/cpufeature.h> ··· 71 72 return 0; 72 73 } 73 74 74 - module_init(blake2s_mod_init); 75 - 76 - MODULE_LICENSE("GPL v2"); 75 + subsys_initcall(blake2s_mod_init);
+2 -2
arch/x86/kernel/callthunks.c
··· 330 330 struct module *mod) 331 331 { 332 332 struct core_text ct = { 333 - .base = (unsigned long)mod->core_layout.base, 334 - .end = (unsigned long)mod->core_layout.base + mod->core_layout.size, 333 + .base = (unsigned long)mod->mem[MOD_TEXT].base, 334 + .end = (unsigned long)mod->mem[MOD_TEXT].base + mod->mem[MOD_TEXT].size, 335 335 .name = mod->name, 336 336 }; 337 337
+1 -1
arch/x86/kernel/cpu/intel.c
··· 1177 1177 static struct ratelimit_state bld_ratelimit; 1178 1178 1179 1179 static unsigned int sysctl_sld_mitigate = 1; 1180 - static DEFINE_SEMAPHORE(buslock_sem); 1180 + static DEFINE_SEMAPHORE(buslock_sem, 1); 1181 1181 1182 1182 #ifdef CONFIG_PROC_SYSCTL 1183 1183 static struct ctl_table sld_sysctls[] = {
+2 -2
arch/x86/kernel/module.c
··· 362 362 } 363 363 if (locks) { 364 364 void *lseg = (void *)locks->sh_addr; 365 - void *text = me->core_layout.base; 366 - void *text_end = text + me->core_layout.text_size; 365 + void *text = me->mem[MOD_TEXT].base; 366 + void *text_end = text + me->mem[MOD_TEXT].size; 367 367 alternatives_smp_module_add(me, me->name, 368 368 lseg, lseg + locks->sh_size, 369 369 text, text_end);
-1
arch/x86/mm/debug_pagetables.c
··· 71 71 72 72 module_init(pt_dump_debug_init); 73 73 module_exit(pt_dump_debug_exit); 74 - MODULE_LICENSE("GPL"); 75 74 MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>"); 76 75 MODULE_DESCRIPTION("Kernel debugging helper that dumps pagetables");
-1
crypto/asymmetric_keys/asymmetric_type.c
··· 17 17 #include <keys/user-type.h> 18 18 #include "asymmetric_keys.h" 19 19 20 - MODULE_LICENSE("GPL"); 21 20 22 21 const char *const key_being_used_for[NR__KEY_BEING_USED_FOR] = { 23 22 [VERIFYING_MODULE_SIGNATURE] = "mod sig",
-1
drivers/accessibility/braille/braille_console.c
··· 24 24 25 25 MODULE_AUTHOR("samuel.thibault@ens-lyon.org"); 26 26 MODULE_DESCRIPTION("braille device"); 27 - MODULE_LICENSE("GPL"); 28 27 29 28 /* 30 29 * Braille device support part.
-1
drivers/bus/arm-integrator-lm.c
··· 126 126 module_platform_driver(integrator_ap_lm_driver); 127 127 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>"); 128 128 MODULE_DESCRIPTION("Integrator AP Logical Module driver"); 129 - MODULE_LICENSE("GPL v2");
-1
drivers/bus/bt1-apb.c
··· 416 416 417 417 MODULE_AUTHOR("Serge Semin <Sergey.Semin@baikalelectronics.ru>"); 418 418 MODULE_DESCRIPTION("Baikal-T1 APB-bus driver"); 419 - MODULE_LICENSE("GPL v2");
-1
drivers/bus/bt1-axi.c
··· 309 309 310 310 MODULE_AUTHOR("Serge Semin <Sergey.Semin@baikalelectronics.ru>"); 311 311 MODULE_DESCRIPTION("Baikal-T1 AXI-bus driver"); 312 - MODULE_LICENSE("GPL v2");
-1
drivers/bus/intel-ixp4xx-eb.c
··· 423 423 module_platform_driver(ixp4xx_exp_driver); 424 424 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>"); 425 425 MODULE_DESCRIPTION("Intel IXP4xx external bus driver"); 426 - MODULE_LICENSE("GPL");
-1
drivers/bus/qcom-ebi2.c
··· 403 403 module_platform_driver(qcom_ebi2_driver); 404 404 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>"); 405 405 MODULE_DESCRIPTION("Qualcomm EBI2 driver"); 406 - MODULE_LICENSE("GPL");
-1
drivers/bus/qcom-ssc-block-bus.c
··· 386 386 387 387 MODULE_DESCRIPTION("A driver for handling the init sequence needed for accessing the SSC block on (some) qcom SoCs over AHB"); 388 388 MODULE_AUTHOR("Michael Srba <Michael.Srba@seznam.cz>"); 389 - MODULE_LICENSE("GPL v2");
+1 -1
drivers/bus/simple-pm-bus.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 1 2 /* 2 3 * Simple Power-Managed Bus Driver 3 4 * ··· 139 138 140 139 MODULE_DESCRIPTION("Simple Power-Managed Bus Driver"); 141 140 MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>"); 142 - MODULE_LICENSE("GPL v2");
-1
drivers/clk/clk-bm1880.c
··· 949 949 950 950 MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>"); 951 951 MODULE_DESCRIPTION("Clock driver for Bitmain BM1880 SoC"); 952 - MODULE_LICENSE("GPL v2");
-1
drivers/clk/microchip/clk-mpfs.c
··· 513 513 MODULE_AUTHOR("Padmarao Begari <padmarao.begari@microchip.com>"); 514 514 MODULE_AUTHOR("Daire McNamara <daire.mcnamara@microchip.com>"); 515 515 MODULE_AUTHOR("Conor Dooley <conor.dooley@microchip.com>"); 516 - MODULE_LICENSE("GPL");
-1
drivers/clk/renesas/renesas-cpg-mssr.c
··· 1127 1127 } 1128 1128 1129 1129 MODULE_DESCRIPTION("Renesas CPG/MSSR Driver"); 1130 - MODULE_LICENSE("GPL v2");
-1
drivers/clk/renesas/rzg2l-cpg.c
··· 1440 1440 subsys_initcall(rzg2l_cpg_init); 1441 1441 1442 1442 MODULE_DESCRIPTION("Renesas RZ/G2L CPG Driver"); 1443 - MODULE_LICENSE("GPL v2");
-1
drivers/clocksource/em_sti.c
··· 363 363 364 364 MODULE_AUTHOR("Magnus Damm"); 365 365 MODULE_DESCRIPTION("Renesas Emma Mobile STI Timer Driver"); 366 - MODULE_LICENSE("GPL v2");
-1
drivers/clocksource/sh_cmt.c
··· 1174 1174 1175 1175 MODULE_AUTHOR("Magnus Damm"); 1176 1176 MODULE_DESCRIPTION("SuperH CMT Timer Driver"); 1177 - MODULE_LICENSE("GPL v2");
-1
drivers/clocksource/sh_mtu2.c
··· 530 530 531 531 MODULE_AUTHOR("Magnus Damm"); 532 532 MODULE_DESCRIPTION("SuperH MTU2 Timer Driver"); 533 - MODULE_LICENSE("GPL v2");
-1
drivers/clocksource/sh_tmu.c
··· 674 674 675 675 MODULE_AUTHOR("Magnus Damm"); 676 676 MODULE_DESCRIPTION("SuperH TMU Timer Driver"); 677 - MODULE_LICENSE("GPL v2");
-1
drivers/clocksource/timer-stm32-lp.c
··· 218 218 219 219 MODULE_ALIAS("platform:stm32-lptimer-timer"); 220 220 MODULE_DESCRIPTION("STMicroelectronics STM32 clockevent low power driver"); 221 - MODULE_LICENSE("GPL v2");
-1
drivers/clocksource/timer-tegra186.c
··· 511 511 512 512 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>"); 513 513 MODULE_DESCRIPTION("NVIDIA Tegra186 timers driver"); 514 - MODULE_LICENSE("GPL v2");
-1
drivers/clocksource/timer-ti-dm.c
··· 1283 1283 module_platform_driver(omap_dm_timer_driver); 1284 1284 1285 1285 MODULE_DESCRIPTION("OMAP Dual-Mode Timer Driver"); 1286 - MODULE_LICENSE("GPL"); 1287 1286 MODULE_AUTHOR("Texas Instruments Inc");
-1
drivers/dma-buf/heaps/cma_heap.c
··· 407 407 } 408 408 module_init(add_default_cma_heap); 409 409 MODULE_DESCRIPTION("DMA-BUF CMA Heap"); 410 - MODULE_LICENSE("GPL v2");
-1
drivers/dma-buf/heaps/system_heap.c
··· 440 440 return 0; 441 441 } 442 442 module_init(system_heap_create); 443 - MODULE_LICENSE("GPL v2");
-1
drivers/dma-buf/udmabuf.c
··· 430 430 module_exit(udmabuf_dev_exit) 431 431 432 432 MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>"); 433 - MODULE_LICENSE("GPL v2");
-1
drivers/dma/ep93xx_dma.c
··· 1431 1431 1432 1432 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>"); 1433 1433 MODULE_DESCRIPTION("EP93xx DMA driver"); 1434 - MODULE_LICENSE("GPL");
-1
drivers/dma/ipu/ipu_idmac.c
··· 1797 1797 subsys_initcall(ipu_init); 1798 1798 1799 1799 MODULE_DESCRIPTION("IPU core driver"); 1800 - MODULE_LICENSE("GPL v2"); 1801 1800 MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>"); 1802 1801 MODULE_ALIAS("platform:ipu-core");
-1
drivers/dma/mv_xor_v2.c
··· 917 917 module_platform_driver(mv_xor_v2_driver); 918 918 919 919 MODULE_DESCRIPTION("DMA engine driver for Marvell's Version 2 of XOR engine"); 920 - MODULE_LICENSE("GPL");
-1
drivers/dma/sh/shdma-base.c
··· 1047 1047 } 1048 1048 module_exit(shdma_exit); 1049 1049 1050 - MODULE_LICENSE("GPL v2"); 1051 1050 MODULE_DESCRIPTION("SH-DMA driver base library"); 1052 1051 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
-1
drivers/dma/stm32-dmamux.c
··· 398 398 MODULE_DESCRIPTION("DMA Router driver for STM32 DMA MUX"); 399 399 MODULE_AUTHOR("M'boumba Cedric Madianga <cedric.madianga@gmail.com>"); 400 400 MODULE_AUTHOR("Pierre-Yves Mordret <pierre-yves.mordret@st.com>"); 401 - MODULE_LICENSE("GPL v2");
-1
drivers/dma/stm32-mdma.c
··· 1814 1814 MODULE_DESCRIPTION("Driver for STM32 MDMA controller"); 1815 1815 MODULE_AUTHOR("M'boumba Cedric Madianga <cedric.madianga@gmail.com>"); 1816 1816 MODULE_AUTHOR("Pierre-Yves Mordret <pierre-yves.mordret@st.com>"); 1817 - MODULE_LICENSE("GPL v2");
-1
drivers/firmware/broadcom/bcm47xx_nvram.c
··· 255 255 } 256 256 EXPORT_SYMBOL(bcm47xx_nvram_get_contents); 257 257 258 - MODULE_LICENSE("GPL v2");
+1 -1
drivers/firmware/efi/runtime-wrappers.c
··· 158 158 * none of the remaining functions are actually ever called at runtime. 159 159 * So let's just use a single lock to serialize all Runtime Services calls. 160 160 */ 161 - static DEFINE_SEMAPHORE(efi_runtime_lock); 161 + static DEFINE_SEMAPHORE(efi_runtime_lock, 1); 162 162 163 163 /* 164 164 * Expose the EFI runtime lock to the UV platform
+1 -1
drivers/firmware/efi/vars.c
··· 21 21 /* Private pointer to registered efivars */ 22 22 static struct efivars *__efivars; 23 23 24 - static DEFINE_SEMAPHORE(efivars_lock); 24 + static DEFINE_SEMAPHORE(efivars_lock, 1); 25 25 26 26 static efi_status_t check_var_size(bool nonblocking, u32 attributes, 27 27 unsigned long size)
-1
drivers/hwspinlock/hwspinlock_core.c
··· 949 949 } 950 950 EXPORT_SYMBOL_GPL(devm_hwspin_lock_request_specific); 951 951 952 - MODULE_LICENSE("GPL v2"); 953 952 MODULE_DESCRIPTION("Hardware spinlock interface"); 954 953 MODULE_AUTHOR("Ohad Ben-Cohen <ohad@wizery.com>");
-5
drivers/infiniband/ulp/srp/ib_srp.c
··· 62 62 MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator"); 63 63 MODULE_LICENSE("Dual BSD/GPL"); 64 64 65 - #if !defined(CONFIG_DYNAMIC_DEBUG) 66 - #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) 67 - #define DYNAMIC_DEBUG_BRANCH(descriptor) false 68 - #endif 69 - 70 65 static unsigned int srp_sg_tablesize; 71 66 static unsigned int cmd_sg_entries; 72 67 static unsigned int indirect_sg_entries;
-5
drivers/interconnect/core.c
··· 13 13 #include <linux/interconnect.h> 14 14 #include <linux/interconnect-provider.h> 15 15 #include <linux/list.h> 16 - #include <linux/module.h> 17 16 #include <linux/mutex.h> 18 17 #include <linux/slab.h> 19 18 #include <linux/of.h> ··· 1105 1106 } 1106 1107 1107 1108 device_initcall(icc_init); 1108 - 1109 - MODULE_AUTHOR("Georgi Djakov <georgi.djakov@linaro.org>"); 1110 - MODULE_DESCRIPTION("Interconnect Driver Core"); 1111 - MODULE_LICENSE("GPL v2");
-1
drivers/iommu/sun50i-iommu.c
··· 1076 1076 MODULE_DESCRIPTION("Allwinner H6 IOMMU driver"); 1077 1077 MODULE_AUTHOR("Maxime Ripard <maxime@cerno.tech>"); 1078 1078 MODULE_AUTHOR("zhuxianbin <zhuxianbin@allwinnertech.com>"); 1079 - MODULE_LICENSE("Dual BSD/GPL");
-1
drivers/irqchip/irq-al-fic.c
··· 26 26 27 27 MODULE_AUTHOR("Talel Shenhar"); 28 28 MODULE_DESCRIPTION("Amazon's Annapurna Labs Interrupt Controller Driver"); 29 - MODULE_LICENSE("GPL v2"); 30 29 31 30 enum al_fic_state { 32 31 AL_FIC_UNCONFIGURED = 0,
-1
drivers/irqchip/irq-ls-scfg-msi.c
··· 430 430 431 431 MODULE_AUTHOR("Minghuan Lian <Minghuan.Lian@nxp.com>"); 432 432 MODULE_DESCRIPTION("Freescale Layerscape SCFG MSI controller driver"); 433 - MODULE_LICENSE("GPL v2");
-1
drivers/irqchip/irq-mbigen.c
··· 392 392 393 393 MODULE_AUTHOR("Jun Ma <majun258@huawei.com>"); 394 394 MODULE_AUTHOR("Yun Wu <wuyun.wu@huawei.com>"); 395 - MODULE_LICENSE("GPL"); 396 395 MODULE_DESCRIPTION("HiSilicon MBI Generator driver");
-1
drivers/irqchip/irq-mchp-eic.c
··· 276 276 IRQCHIP_PLATFORM_DRIVER_END(mchp_eic) 277 277 278 278 MODULE_DESCRIPTION("Microchip External Interrupt Controller"); 279 - MODULE_LICENSE("GPL v2"); 280 279 MODULE_AUTHOR("Claudiu Beznea <claudiu.beznea@microchip.com>");
-1
drivers/irqchip/irq-renesas-intc-irqpin.c
··· 608 608 609 609 MODULE_AUTHOR("Magnus Damm"); 610 610 MODULE_DESCRIPTION("Renesas INTC External IRQ Pin Driver"); 611 - MODULE_LICENSE("GPL v2");
-1
drivers/irqchip/irq-renesas-irqc.c
··· 270 270 271 271 MODULE_AUTHOR("Magnus Damm"); 272 272 MODULE_DESCRIPTION("Renesas IRQC Driver"); 273 - MODULE_LICENSE("GPL v2");
-1
drivers/irqchip/irq-renesas-rza1.c
··· 281 281 282 282 MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>"); 283 283 MODULE_DESCRIPTION("Renesas RZ/A1 IRQC Driver"); 284 - MODULE_LICENSE("GPL v2");
-1
drivers/irqchip/irq-renesas-rzg2l.c
··· 390 390 IRQCHIP_PLATFORM_DRIVER_END(rzg2l_irqc) 391 391 MODULE_AUTHOR("Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>"); 392 392 MODULE_DESCRIPTION("Renesas RZ/G2L IRQC Driver"); 393 - MODULE_LICENSE("GPL");
-1
drivers/irqchip/irq-sl28cpld.c
··· 92 92 93 93 MODULE_DESCRIPTION("sl28cpld Interrupt Controller Driver"); 94 94 MODULE_AUTHOR("Michael Walle <michael@walle.cc>"); 95 - MODULE_LICENSE("GPL");
-1
drivers/irqchip/irq-ti-sci-inta.c
··· 743 743 744 744 MODULE_AUTHOR("Lokesh Vutla <lokeshvutla@ti.com>"); 745 745 MODULE_DESCRIPTION("K3 Interrupt Aggregator driver over TI SCI protocol"); 746 - MODULE_LICENSE("GPL v2");
-1
drivers/irqchip/irq-ti-sci-intr.c
··· 303 303 304 304 MODULE_AUTHOR("Lokesh Vutla <lokeshvutla@ticom>"); 305 305 MODULE_DESCRIPTION("K3 Interrupt Router driver over TI SCI protocol"); 306 - MODULE_LICENSE("GPL v2");
+1 -1
drivers/macintosh/adb.c
··· 80 80 BLOCKING_NOTIFIER_HEAD(adb_client_list); 81 81 static int adb_got_sleep; 82 82 static int adb_inited; 83 - static DEFINE_SEMAPHORE(adb_probe_mutex); 83 + static DEFINE_SEMAPHORE(adb_probe_mutex, 1); 84 84 static int sleepy_trackpad; 85 85 static int autopoll_devs; 86 86 int __adb_probe_sync;
+1 -1
drivers/mailbox/Kconfig
··· 259 259 during the vblank. 260 260 261 261 config ZYNQMP_IPI_MBOX 262 - bool "Xilinx ZynqMP IPI Mailbox" 262 + tristate "Xilinx ZynqMP IPI Mailbox" 263 263 depends on ARCH_ZYNQMP && OF 264 264 help 265 265 Say yes here to add support for Xilinx IPI mailbox driver.
-1
drivers/mailbox/rockchip-mailbox.c
··· 254 254 255 255 module_platform_driver(rockchip_mbox_driver); 256 256 257 - MODULE_LICENSE("GPL v2"); 258 257 MODULE_DESCRIPTION("Rockchip mailbox: communicate between CPU cores and MCU"); 259 258 MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>"); 260 259 MODULE_AUTHOR("Caesar Wang <wxt@rock-chips.com>");
-1
drivers/mfd/altera-sysmgr.c
··· 198 198 199 199 MODULE_AUTHOR("Thor Thayer <>"); 200 200 MODULE_DESCRIPTION("SOCFPGA System Manager driver"); 201 - MODULE_LICENSE("GPL v2");
+1 -1
drivers/net/ethernet/broadcom/bnx2x/bnx2x_main.c
··· 297 297 298 298 /* Global resources for unloading a previously loaded device */ 299 299 #define BNX2X_PREV_WAIT_NEEDED 1 300 - static DEFINE_SEMAPHORE(bnx2x_prev_sem); 300 + static DEFINE_SEMAPHORE(bnx2x_prev_sem, 1); 301 301 static LIST_HEAD(bnx2x_prev_list); 302 302 303 303 /* Forward declaration */
-1
drivers/nvmem/core.c
··· 2111 2111 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org"); 2112 2112 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com"); 2113 2113 MODULE_DESCRIPTION("nvmem Driver Core"); 2114 - MODULE_LICENSE("GPL v2");
-1
drivers/phy/intel/phy-intel-lgm-combo.c
··· 616 616 module_platform_driver(intel_cbphy_driver); 617 617 618 618 MODULE_DESCRIPTION("Intel Combo-phy driver"); 619 - MODULE_LICENSE("GPL v2");
-1
drivers/pinctrl/actions/pinctrl-s500.c
··· 1724 1724 MODULE_AUTHOR("Actions Semi Inc."); 1725 1725 MODULE_AUTHOR("Cristian Ciocaltea <cristian.ciocaltea@gmail.com>"); 1726 1726 MODULE_DESCRIPTION("Actions Semi S500 SoC Pinctrl Driver"); 1727 - MODULE_LICENSE("GPL");
-1
drivers/pinctrl/actions/pinctrl-s700.c
··· 1908 1908 1909 1909 MODULE_AUTHOR("Actions Semi Inc."); 1910 1910 MODULE_DESCRIPTION("Actions Semi S700 Soc Pinctrl Driver"); 1911 - MODULE_LICENSE("GPL");
-1
drivers/pinctrl/actions/pinctrl-s900.c
··· 1827 1827 MODULE_AUTHOR("Actions Semi Inc."); 1828 1828 MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>"); 1829 1829 MODULE_DESCRIPTION("Actions Semi S900 SoC Pinctrl Driver"); 1830 - MODULE_LICENSE("GPL");
-1
drivers/pinctrl/bcm/pinctrl-ns.c
··· 299 299 module_platform_driver(ns_pinctrl_driver); 300 300 301 301 MODULE_AUTHOR("Rafał Miłecki"); 302 - MODULE_LICENSE("GPL v2"); 303 302 MODULE_DEVICE_TABLE(of, ns_pinctrl_of_match_table);
-1
drivers/pinctrl/mediatek/pinctrl-mt8188.c
··· 1670 1670 1671 1671 arch_initcall(mt8188_pinctrl_init); 1672 1672 1673 - MODULE_LICENSE("GPL"); 1674 1673 MODULE_DESCRIPTION("MediaTek MT8188 Pinctrl Driver");
-1
drivers/pinctrl/mediatek/pinctrl-mt8192.c
··· 1431 1431 } 1432 1432 arch_initcall(mt8192_pinctrl_init); 1433 1433 1434 - MODULE_LICENSE("GPL v2"); 1435 1434 MODULE_DESCRIPTION("MediaTek MT8192 Pinctrl Driver");
-1
drivers/pinctrl/mediatek/pinctrl-mt8365.c
··· 495 495 } 496 496 arch_initcall(mtk_pinctrl_init); 497 497 498 - MODULE_LICENSE("GPL"); 499 498 MODULE_DESCRIPTION("MediaTek MT8365 Pinctrl Driver"); 500 499 MODULE_AUTHOR("Zhiyong Tao <zhiyong.tao@mediatek.com>");
-1
drivers/pinctrl/nuvoton/pinctrl-npcm7xx.c
··· 2046 2046 } 2047 2047 arch_initcall(npcm7xx_pinctrl_register); 2048 2048 2049 - MODULE_LICENSE("GPL v2"); 2050 2049 MODULE_AUTHOR("jordan_hargrave@dell.com"); 2051 2050 MODULE_AUTHOR("tomer.maimon@nuvoton.com"); 2052 2051 MODULE_DESCRIPTION("Nuvoton NPCM7XX Pinctrl and GPIO driver");
-1
drivers/pinctrl/pinctrl-amd.c
··· 1213 1213 1214 1214 module_platform_driver(amd_gpio_driver); 1215 1215 1216 - MODULE_LICENSE("GPL v2"); 1217 1216 MODULE_AUTHOR("Ken Xue <Ken.Xue@amd.com>, Jeff Wu <Jeff.Wu@amd.com>"); 1218 1217 MODULE_DESCRIPTION("AMD GPIO pinctrl driver");
-1
drivers/pinctrl/renesas/pinctrl-rza1.c
··· 1407 1407 1408 1408 MODULE_AUTHOR("Jacopo Mondi <jacopo+renesas@jmondi.org"); 1409 1409 MODULE_DESCRIPTION("Pin and gpio controller driver for Reneas RZ/A1 SoC"); 1410 - MODULE_LICENSE("GPL v2");
-1
drivers/pinctrl/renesas/pinctrl-rza2.c
··· 514 514 515 515 MODULE_AUTHOR("Chris Brandt <chris.brandt@renesas.com>"); 516 516 MODULE_DESCRIPTION("Pin and gpio controller driver for RZ/A2 SoC"); 517 - MODULE_LICENSE("GPL v2");
-1
drivers/pinctrl/renesas/pinctrl-rzg2l.c
··· 1570 1570 1571 1571 MODULE_AUTHOR("Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>"); 1572 1572 MODULE_DESCRIPTION("Pin and gpio controller driver for RZ/G2L family"); 1573 - MODULE_LICENSE("GPL v2");
-1
drivers/pinctrl/renesas/pinctrl-rzn1.c
··· 952 952 953 953 MODULE_AUTHOR("Phil Edworthy <phil.edworthy@renesas.com>"); 954 954 MODULE_DESCRIPTION("Renesas RZ/N1 pinctrl driver"); 955 - MODULE_LICENSE("GPL v2");
-1
drivers/pinctrl/renesas/pinctrl-rzv2m.c
··· 1117 1117 1118 1118 MODULE_AUTHOR("Phil Edworthy <phil.edworthy@renesas.com>"); 1119 1119 MODULE_DESCRIPTION("Pin and gpio controller driver for RZ/V2M"); 1120 - MODULE_LICENSE("GPL");
+1 -1
drivers/platform/x86/intel/ifs/sysfs.c
··· 13 13 * Protects against simultaneous tests on multiple cores, or 14 14 * reloading can file while a test is in progress 15 15 */ 16 - static DEFINE_SEMAPHORE(ifs_sem); 16 + static DEFINE_SEMAPHORE(ifs_sem, 1); 17 17 18 18 /* 19 19 * The sysfs interface to check additional details of last test
-1
drivers/power/reset/as3722-poweroff.c
··· 84 84 MODULE_DESCRIPTION("Power off driver for ams AS3722 PMIC Device"); 85 85 MODULE_ALIAS("platform:as3722-power-off"); 86 86 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); 87 - MODULE_LICENSE("GPL v2");
-1
drivers/power/reset/gpio-poweroff.c
··· 105 105 106 106 MODULE_AUTHOR("Jamie Lentin <jm@lentin.co.uk>"); 107 107 MODULE_DESCRIPTION("GPIO poweroff driver"); 108 - MODULE_LICENSE("GPL v2"); 109 108 MODULE_ALIAS("platform:poweroff-gpio");
-1
drivers/power/reset/gpio-restart.c
··· 139 139 140 140 MODULE_AUTHOR("David Riley <davidriley@chromium.org>"); 141 141 MODULE_DESCRIPTION("GPIO restart driver"); 142 - MODULE_LICENSE("GPL");
-1
drivers/power/reset/keystone-reset.c
··· 169 169 170 170 MODULE_AUTHOR("Ivan Khoronzhuk <ivan.khoronzhuk@ti.com>"); 171 171 MODULE_DESCRIPTION("Texas Instruments keystone reset driver"); 172 - MODULE_LICENSE("GPL v2"); 173 172 MODULE_ALIAS("platform:" KBUILD_MODNAME);
-1
drivers/power/reset/ltc2952-poweroff.c
··· 317 317 318 318 MODULE_AUTHOR("René Moll <rene.moll@xsens.com>"); 319 319 MODULE_DESCRIPTION("LTC PowerPath power-off driver"); 320 - MODULE_LICENSE("GPL v2");
-1
drivers/power/reset/mt6323-poweroff.c
··· 97 97 98 98 MODULE_DESCRIPTION("Poweroff driver for MT6323 PMIC"); 99 99 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>"); 100 - MODULE_LICENSE("GPL v2");
-1
drivers/power/reset/regulator-poweroff.c
··· 79 79 80 80 MODULE_AUTHOR("Michael Klein <michael@fossekall.de>"); 81 81 MODULE_DESCRIPTION("Regulator poweroff driver"); 82 - MODULE_LICENSE("GPL v2"); 83 82 MODULE_ALIAS("platform:poweroff-regulator");
-1
drivers/power/reset/restart-poweroff.c
··· 59 59 60 60 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch"); 61 61 MODULE_DESCRIPTION("restart poweroff driver"); 62 - MODULE_LICENSE("GPL v2"); 63 62 MODULE_ALIAS("platform:poweroff-restart");
-1
drivers/power/reset/tps65086-restart.c
··· 95 95 96 96 MODULE_AUTHOR("Emil Renner Berthing <kernel@esmil.dk>"); 97 97 MODULE_DESCRIPTION("TPS65086 restart driver"); 98 - MODULE_LICENSE("GPL v2");
-1
drivers/power/supply/power_supply_core.c
··· 1485 1485 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, " 1486 1486 "Szabolcs Gyurko, " 1487 1487 "Anton Vorontsov <cbou@mail.ru>"); 1488 - MODULE_LICENSE("GPL");
-1
drivers/power/supply/wm97xx_battery.c
··· 271 271 272 272 module_platform_driver(wm97xx_bat_driver); 273 273 274 - MODULE_LICENSE("GPL"); 275 274 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>"); 276 275 MODULE_DESCRIPTION("WM97xx battery driver");
-1
drivers/regulator/stm32-pwr.c
··· 183 183 184 184 MODULE_DESCRIPTION("STM32MP1 PWR voltage regulator driver"); 185 185 MODULE_AUTHOR("Pascal Paillet <p.paillet@st.com>"); 186 - MODULE_LICENSE("GPL v2");
-1
drivers/remoteproc/remoteproc_core.c
··· 2766 2766 } 2767 2767 module_exit(remoteproc_exit); 2768 2768 2769 - MODULE_LICENSE("GPL v2"); 2770 2769 MODULE_DESCRIPTION("Generic Remote Processor Framework");
-1
drivers/reset/reset-lantiq.c
··· 207 207 208 208 MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>"); 209 209 MODULE_DESCRIPTION("Lantiq XWAY RCU Reset Controller Driver"); 210 - MODULE_LICENSE("GPL");
-1
drivers/reset/reset-microchip-sparx5.c
··· 179 179 180 180 MODULE_DESCRIPTION("Microchip Sparx5 switch reset driver"); 181 181 MODULE_AUTHOR("Steen Hegelund <steen.hegelund@microchip.com>"); 182 - MODULE_LICENSE("Dual MIT/GPL");
-1
drivers/reset/reset-mpfs.c
··· 153 153 154 154 MODULE_DESCRIPTION("Microchip PolarFire SoC Reset Driver"); 155 155 MODULE_AUTHOR("Conor Dooley <conor.dooley@microchip.com>"); 156 - MODULE_LICENSE("GPL"); 157 156 MODULE_IMPORT_NS(MCHP_CLK_MPFS);
+1 -1
drivers/scsi/esas2r/esas2r_ioctl.c
··· 56 56 u32 esas2r_buffered_ioctl_size; 57 57 struct pci_dev *esas2r_buffered_ioctl_pcid; 58 58 59 - static DEFINE_SEMAPHORE(buffered_ioctl_semaphore); 59 + static DEFINE_SEMAPHORE(buffered_ioctl_semaphore, 1); 60 60 typedef int (*BUFFERED_IOCTL_CALLBACK)(struct esas2r_adapter *, 61 61 struct esas2r_request *, 62 62 struct esas2r_sg_context *,
-1
drivers/soc/apple/apple-pmgr-pwrstate.c
··· 322 322 323 323 MODULE_AUTHOR("Hector Martin <marcan@marcan.st>"); 324 324 MODULE_DESCRIPTION("PMGR power state driver for Apple SoCs"); 325 - MODULE_LICENSE("GPL v2"); 326 325 327 326 module_platform_driver(apple_pmgr_ps_driver);
-1
drivers/soc/fujitsu/a64fx-diag.c
··· 149 149 150 150 module_platform_driver(a64fx_diag_driver); 151 151 152 - MODULE_LICENSE("GPL v2"); 153 152 MODULE_AUTHOR("Hitomi Hasegawa <hasegawa-hitomi@fujitsu.com>"); 154 153 MODULE_DESCRIPTION("A64FX diag driver");
+1 -1
drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
··· 149 149 static char *g_free_fragments; 150 150 static struct semaphore g_free_fragments_sema; 151 151 152 - static DEFINE_SEMAPHORE(g_free_fragments_mutex); 152 + static DEFINE_SEMAPHORE(g_free_fragments_mutex, 1); 153 153 154 154 static int 155 155 vchiq_blocking_bulk_transfer(struct vchiq_instance *instance, unsigned int handle, void *data,
-1
drivers/video/fbdev/wm8505fb.c
··· 403 403 404 404 MODULE_AUTHOR("Ed Spiridonov <edo.rus@gmail.com>"); 405 405 MODULE_DESCRIPTION("Framebuffer driver for WMT WM8505"); 406 - MODULE_LICENSE("GPL v2"); 407 406 MODULE_DEVICE_TABLE(of, wmt_dt_ids);
-1
drivers/video/fbdev/wmt_ge_rops.c
··· 169 169 MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>"); 170 170 MODULE_DESCRIPTION("Accelerators for raster operations using " 171 171 "WonderMedia Graphics Engine"); 172 - MODULE_LICENSE("GPL v2"); 173 172 MODULE_DEVICE_TABLE(of, wmt_dt_ids);
-1
fs/binfmt_elf.c
··· 2174 2174 2175 2175 core_initcall(init_elf_binfmt); 2176 2176 module_exit(exit_elf_binfmt); 2177 - MODULE_LICENSE("GPL"); 2178 2177 2179 2178 #ifdef CONFIG_BINFMT_ELF_KUNIT_TEST 2180 2179 #include "binfmt_elf_test.c"
-1
fs/nfs_common/nfs_ssc.c
··· 12 12 #include <linux/nfs_ssc.h> 13 13 #include "../nfs/nfs4_fs.h" 14 14 15 - MODULE_LICENSE("GPL"); 16 15 17 16 struct nfs_ssc_client_ops_tbl nfs_ssc_client_tbl; 18 17 EXPORT_SYMBOL_GPL(nfs_ssc_client_tbl);
-1
fs/unicode/utf8-core.c
··· 214 214 } 215 215 EXPORT_SYMBOL(utf8_unload); 216 216 217 - MODULE_LICENSE("GPL v2");
+35 -33
include/linux/dynamic_debug.h
··· 128 128 const struct ddebug_class_map *map; 129 129 }; 130 130 131 - #if defined(CONFIG_DYNAMIC_DEBUG_CORE) 131 + /* 132 + * pr_debug() and friends are globally enabled or modules have selectively 133 + * enabled them. 134 + */ 135 + #if defined(CONFIG_DYNAMIC_DEBUG) || \ 136 + (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE)) 132 137 133 - int ddebug_add_module(struct _ddebug_info *dyndbg, const char *modname); 134 - 135 - extern int ddebug_remove_module(const char *mod_name); 136 138 extern __printf(2, 3) 137 139 void __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...); 138 - 139 - extern int ddebug_dyndbg_module_param_cb(char *param, char *val, 140 - const char *modname); 141 140 142 141 struct device; 143 142 ··· 286 287 KERN_DEBUG, prefix_str, prefix_type, \ 287 288 rowsize, groupsize, buf, len, ascii) 288 289 289 - struct kernel_param; 290 - int param_set_dyndbg_classes(const char *instr, const struct kernel_param *kp); 291 - int param_get_dyndbg_classes(char *buffer, const struct kernel_param *kp); 292 - 293 290 /* for test only, generally expect drm.debug style macro wrappers */ 294 291 #define __pr_debug_cls(cls, fmt, ...) do { \ 295 292 BUILD_BUG_ON_MSG(!__builtin_constant_p(cls), \ ··· 293 298 dynamic_pr_debug_cls(cls, fmt, ##__VA_ARGS__); \ 294 299 } while (0) 295 300 296 - #else /* !CONFIG_DYNAMIC_DEBUG_CORE */ 301 + #else /* !(CONFIG_DYNAMIC_DEBUG || (CONFIG_DYNAMIC_DEBUG_CORE && DYNAMIC_DEBUG_MODULE)) */ 297 302 298 303 #include <linux/string.h> 299 304 #include <linux/errno.h> 300 305 #include <linux/printk.h> 301 306 302 - static inline int ddebug_add_module(struct _ddebug_info *dinfo, const char *modname) 303 - { 304 - return 0; 305 - } 307 + #define DEFINE_DYNAMIC_DEBUG_METADATA(name, fmt) 308 + #define DYNAMIC_DEBUG_BRANCH(descriptor) false 306 309 307 - static inline int ddebug_remove_module(const char *mod) 308 - { 309 - return 0; 310 - } 310 + #define dynamic_pr_debug(fmt, ...) \ 311 + do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0) 312 + #define dynamic_dev_dbg(dev, fmt, ...) \ 313 + do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0) 314 + #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \ 315 + groupsize, buf, len, ascii) \ 316 + do { if (0) \ 317 + print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, \ 318 + rowsize, groupsize, buf, len, ascii); \ 319 + } while (0) 320 + 321 + #endif /* CONFIG_DYNAMIC_DEBUG || (CONFIG_DYNAMIC_DEBUG_CORE && DYNAMIC_DEBUG_MODULE) */ 322 + 323 + 324 + #ifdef CONFIG_DYNAMIC_DEBUG_CORE 325 + 326 + extern int ddebug_dyndbg_module_param_cb(char *param, char *val, 327 + const char *modname); 328 + struct kernel_param; 329 + int param_set_dyndbg_classes(const char *instr, const struct kernel_param *kp); 330 + int param_get_dyndbg_classes(char *buffer, const struct kernel_param *kp); 331 + 332 + #else 311 333 312 334 static inline int ddebug_dyndbg_module_param_cb(char *param, char *val, 313 335 const char *modname) ··· 338 326 return -EINVAL; 339 327 } 340 328 341 - #define dynamic_pr_debug(fmt, ...) \ 342 - do { if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); } while (0) 343 - #define dynamic_dev_dbg(dev, fmt, ...) \ 344 - do { if (0) dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__); } while (0) 345 - #define dynamic_hex_dump(prefix_str, prefix_type, rowsize, \ 346 - groupsize, buf, len, ascii) \ 347 - do { if (0) \ 348 - print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, \ 349 - rowsize, groupsize, buf, len, ascii); \ 350 - } while (0) 351 - 352 329 struct kernel_param; 353 330 static inline int param_set_dyndbg_classes(const char *instr, const struct kernel_param *kp) 354 331 { return 0; } 355 332 static inline int param_get_dyndbg_classes(char *buffer, const struct kernel_param *kp) 356 333 { return 0; } 357 334 358 - #endif /* !CONFIG_DYNAMIC_DEBUG_CORE */ 335 + #endif 336 + 359 337 360 338 extern const struct kernel_param_ops param_ops_dyndbg_classes; 361 339 362 - #endif 340 + #endif /* _DYNAMIC_DEBUG_H */
+3 -4
include/linux/kallsyms.h
··· 67 67 68 68 #ifdef CONFIG_KALLSYMS 69 69 unsigned long kallsyms_sym_address(int idx); 70 - int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *, 71 - unsigned long), 70 + int kallsyms_on_each_symbol(int (*fn)(void *, const char *, unsigned long), 72 71 void *data); 73 72 int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long), 74 73 const char *name, void *data); ··· 165 166 return false; 166 167 } 167 168 168 - static inline int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *, 169 - unsigned long), void *data) 169 + static inline int kallsyms_on_each_symbol(int (*fn)(void *, const char *, unsigned long), 170 + void *data) 170 171 { 171 172 return -EOPNOTSUPP; 172 173 }
+108 -33
include/linux/module.h
··· 27 27 #include <linux/tracepoint-defs.h> 28 28 #include <linux/srcu.h> 29 29 #include <linux/static_call_types.h> 30 + #include <linux/dynamic_debug.h> 30 31 31 32 #include <linux/percpu.h> 32 33 #include <asm/module.h> ··· 321 320 struct latch_tree_node node; 322 321 }; 323 322 324 - struct module_layout { 325 - /* The actual code + data. */ 323 + enum mod_mem_type { 324 + MOD_TEXT = 0, 325 + MOD_DATA, 326 + MOD_RODATA, 327 + MOD_RO_AFTER_INIT, 328 + MOD_INIT_TEXT, 329 + MOD_INIT_DATA, 330 + MOD_INIT_RODATA, 331 + 332 + MOD_MEM_NUM_TYPES, 333 + MOD_INVALID = -1, 334 + }; 335 + 336 + #define mod_mem_type_is_init(type) \ 337 + ((type) == MOD_INIT_TEXT || \ 338 + (type) == MOD_INIT_DATA || \ 339 + (type) == MOD_INIT_RODATA) 340 + 341 + #define mod_mem_type_is_core(type) (!mod_mem_type_is_init(type)) 342 + 343 + #define mod_mem_type_is_text(type) \ 344 + ((type) == MOD_TEXT || \ 345 + (type) == MOD_INIT_TEXT) 346 + 347 + #define mod_mem_type_is_data(type) (!mod_mem_type_is_text(type)) 348 + 349 + #define mod_mem_type_is_core_data(type) \ 350 + (mod_mem_type_is_core(type) && \ 351 + mod_mem_type_is_data(type)) 352 + 353 + #define for_each_mod_mem_type(type) \ 354 + for (enum mod_mem_type (type) = 0; \ 355 + (type) < MOD_MEM_NUM_TYPES; (type)++) 356 + 357 + #define for_class_mod_mem_type(type, class) \ 358 + for_each_mod_mem_type(type) \ 359 + if (mod_mem_type_is_##class(type)) 360 + 361 + struct module_memory { 326 362 void *base; 327 - /* Total size. */ 328 363 unsigned int size; 329 - /* The size of the executable code. */ 330 - unsigned int text_size; 331 - /* Size of RO section of the module (text+rodata) */ 332 - unsigned int ro_size; 333 - /* Size of RO after init section */ 334 - unsigned int ro_after_init_size; 335 364 336 365 #ifdef CONFIG_MODULES_TREE_LOOKUP 337 366 struct mod_tree_node mtn; ··· 370 339 371 340 #ifdef CONFIG_MODULES_TREE_LOOKUP 372 341 /* Only touch one cacheline for common rbtree-for-core-layout case. */ 373 - #define __module_layout_align ____cacheline_aligned 342 + #define __module_memory_align ____cacheline_aligned 374 343 #else 375 - #define __module_layout_align 344 + #define __module_memory_align 376 345 #endif 377 346 378 347 struct mod_kallsyms { ··· 457 426 /* Startup function. */ 458 427 int (*init)(void); 459 428 460 - /* Core layout: rbtree is accessed frequently, so keep together. */ 461 - struct module_layout core_layout __module_layout_align; 462 - struct module_layout init_layout; 463 - #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 464 - struct module_layout data_layout; 465 - #endif 429 + struct module_memory mem[MOD_MEM_NUM_TYPES] __module_memory_align; 466 430 467 431 /* Arch-specific module values */ 468 432 struct mod_arch_specific arch; ··· 580 554 struct error_injection_entry *ei_funcs; 581 555 unsigned int num_ei_funcs; 582 556 #endif 557 + #ifdef CONFIG_DYNAMIC_DEBUG_CORE 558 + struct _ddebug_info dyndbg_info; 559 + #endif 583 560 } ____cacheline_aligned __randomize_layout; 584 561 #ifndef MODULE_ARCH_INIT 585 562 #define MODULE_ARCH_INIT {} ··· 610 581 bool is_module_percpu_address(unsigned long addr); 611 582 bool is_module_text_address(unsigned long addr); 612 583 584 + static inline bool within_module_mem_type(unsigned long addr, 585 + const struct module *mod, 586 + enum mod_mem_type type) 587 + { 588 + unsigned long base, size; 589 + 590 + base = (unsigned long)mod->mem[type].base; 591 + size = mod->mem[type].size; 592 + return addr - base < size; 593 + } 594 + 613 595 static inline bool within_module_core(unsigned long addr, 614 596 const struct module *mod) 615 597 { 616 - #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 617 - if ((unsigned long)mod->data_layout.base <= addr && 618 - addr < (unsigned long)mod->data_layout.base + mod->data_layout.size) 619 - return true; 620 - #endif 621 - return (unsigned long)mod->core_layout.base <= addr && 622 - addr < (unsigned long)mod->core_layout.base + mod->core_layout.size; 598 + for_class_mod_mem_type(type, core) { 599 + if (within_module_mem_type(addr, mod, type)) 600 + return true; 601 + } 602 + return false; 623 603 } 624 604 625 605 static inline bool within_module_init(unsigned long addr, 626 606 const struct module *mod) 627 607 { 628 - return (unsigned long)mod->init_layout.base <= addr && 629 - addr < (unsigned long)mod->init_layout.base + mod->init_layout.size; 608 + for_class_mod_mem_type(type, init) { 609 + if (within_module_mem_type(addr, mod, type)) 610 + return true; 611 + } 612 + return false; 630 613 } 631 614 632 615 static inline bool within_module(unsigned long addr, const struct module *mod) ··· 663 622 to handle the error case (which only happens with rmmod --wait). */ 664 623 extern void __module_get(struct module *module); 665 624 666 - /* This is the Right Way to get a module: if it fails, it's being removed, 667 - * so pretend it's not there. */ 625 + /** 626 + * try_module_get() - take module refcount unless module is being removed 627 + * @module: the module we should check for 628 + * 629 + * Only try to get a module reference count if the module is not being removed. 630 + * This call will fail if the module is already being removed. 631 + * 632 + * Care must also be taken to ensure the module exists and is alive prior to 633 + * usage of this call. This can be gauranteed through two means: 634 + * 635 + * 1) Direct protection: you know an earlier caller must have increased the 636 + * module reference through __module_get(). This can typically be achieved 637 + * by having another entity other than the module itself increment the 638 + * module reference count. 639 + * 640 + * 2) Implied protection: there is an implied protection against module 641 + * removal. An example of this is the implied protection used by kernfs / 642 + * sysfs. The sysfs store / read file operations are guaranteed to exist 643 + * through the use of kernfs's active reference (see kernfs_active()) and a 644 + * sysfs / kernfs file removal cannot happen unless the same file is not 645 + * active. Therefore, if a sysfs file is being read or written to the module 646 + * which created it must still exist. It is therefore safe to use 647 + * try_module_get() on module sysfs store / read ops. 648 + * 649 + * One of the real values to try_module_get() is the module_is_live() check 650 + * which ensures that the caller of try_module_get() can yield to userspace 651 + * module removal requests and gracefully fail if the module is on its way out. 652 + * 653 + * Returns true if the reference count was successfully incremented. 654 + */ 668 655 extern bool try_module_get(struct module *module); 669 656 657 + /** 658 + * module_put() - release a reference count to a module 659 + * @module: the module we should release a reference count for 660 + * 661 + * If you successfully bump a reference count to a module with try_module_get(), 662 + * when you are finished you must call module_put() to release that reference 663 + * count. 664 + */ 670 665 extern void module_put(struct module *module); 671 666 672 667 #else /*!CONFIG_MODULE_UNLOAD*/ ··· 859 782 860 783 #ifdef CONFIG_SYSFS 861 784 extern struct kset *module_kset; 862 - extern struct kobj_type module_ktype; 785 + extern const struct kobj_type module_ktype; 863 786 #endif /* CONFIG_SYSFS */ 864 787 865 788 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x) ··· 913 836 914 837 #if defined(CONFIG_MODULES) && defined(CONFIG_KALLSYMS) 915 838 int module_kallsyms_on_each_symbol(const char *modname, 916 - int (*fn)(void *, const char *, 917 - struct module *, unsigned long), 839 + int (*fn)(void *, const char *, unsigned long), 918 840 void *data); 919 841 920 842 /* For kallsyms to ask for address resolution. namebuf should be at ··· 946 870 #else /* CONFIG_MODULES && CONFIG_KALLSYMS */ 947 871 948 872 static inline int module_kallsyms_on_each_symbol(const char *modname, 949 - int (*fn)(void *, const char *, 950 - struct module *, unsigned long), 873 + int (*fn)(void *, const char *, unsigned long), 951 874 void *data) 952 875 { 953 876 return -EOPNOTSUPP;
+17
include/linux/module_symbol.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + #ifndef _LINUX_MODULE_SYMBOL_H 3 + #define _LINUX_MODULE_SYMBOL_H 4 + 5 + /* This ignores the intensely annoying "mapping symbols" found in ELF files. */ 6 + static inline int is_mapping_symbol(const char *str) 7 + { 8 + if (str[0] == '.' && str[1] == 'L') 9 + return true; 10 + if (str[0] == 'L' && str[1] == '0') 11 + return true; 12 + return str[0] == '$' && 13 + (str[1] == 'a' || str[1] == 'd' || str[1] == 't' || str[1] == 'x') 14 + && (str[2] == '\0' || str[2] == '.'); 15 + } 16 + 17 + #endif /* _LINUX_MODULE_SYMBOL_H */
+8 -2
include/linux/semaphore.h
··· 25 25 .wait_list = LIST_HEAD_INIT((name).wait_list), \ 26 26 } 27 27 28 - #define DEFINE_SEMAPHORE(name) \ 29 - struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1) 28 + /* 29 + * Unlike mutexes, binary semaphores do not have an owner, so up() can 30 + * be called in a different thread from the one which called down(). 31 + * It is also safe to call down_trylock() and up() from interrupt 32 + * context. 33 + */ 34 + #define DEFINE_SEMAPHORE(_name, _n) \ 35 + struct semaphore _name = __SEMAPHORE_INITIALIZER(_name, _n) 30 36 31 37 static inline void sema_init(struct semaphore *sem, int val) 32 38 {
-1
kernel/Makefile
··· 13 13 async.o range.o smpboot.o ucount.o regset.o 14 14 15 15 obj-$(CONFIG_USERMODE_DRIVER) += usermode_driver.o 16 - obj-$(CONFIG_MODULES) += kmod.o 17 16 obj-$(CONFIG_MULTIUSER) += groups.o 18 17 obj-$(CONFIG_VHOST_TASK) += vhost_task.o 19 18
-1
kernel/dma/map_benchmark.c
··· 356 356 357 357 MODULE_AUTHOR("Barry Song <song.bao.hua@hisilicon.com>"); 358 358 MODULE_DESCRIPTION("dma_map benchmark driver"); 359 - MODULE_LICENSE("GPL");
-1
kernel/events/hw_breakpoint_test.c
··· 329 329 330 330 kunit_test_suites(&hw_breakpoint_test_suite); 331 331 332 - MODULE_LICENSE("GPL"); 333 332 MODULE_AUTHOR("Marco Elver <elver@google.com>");
+2 -3
kernel/kallsyms.c
··· 288 288 * Iterate over all symbols in vmlinux. For symbols from modules use 289 289 * module_kallsyms_on_each_symbol instead. 290 290 */ 291 - int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *, 292 - unsigned long), 291 + int kallsyms_on_each_symbol(int (*fn)(void *, const char *, unsigned long), 293 292 void *data) 294 293 { 295 294 char namebuf[KSYM_NAME_LEN]; ··· 298 299 299 300 for (i = 0, off = 0; i < kallsyms_num_syms; i++) { 300 301 off = kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); 301 - ret = fn(data, namebuf, NULL, kallsyms_sym_address(i)); 302 + ret = fn(data, namebuf, kallsyms_sym_address(i)); 302 303 if (ret != 0) 303 304 return ret; 304 305 cond_resched();
+3 -3
kernel/kallsyms_selftest.c
··· 95 95 96 96 static char stub_name[KSYM_NAME_LEN]; 97 97 98 - static int stat_symbol_len(void *data, const char *name, struct module *mod, unsigned long addr) 98 + static int stat_symbol_len(void *data, const char *name, unsigned long addr) 99 99 { 100 100 *(u32 *)data += strlen(name); 101 101 ··· 154 154 pr_info(" ---------------------------------------------------------\n"); 155 155 } 156 156 157 - static int lookup_name(void *data, const char *name, struct module *mod, unsigned long addr) 157 + static int lookup_name(void *data, const char *name, unsigned long addr) 158 158 { 159 159 u64 t0, t1, t; 160 160 struct test_stat *stat = (struct test_stat *)data; ··· 207 207 return !strncmp(s, name, len); 208 208 } 209 209 210 - static int find_symbol(void *data, const char *name, struct module *mod, unsigned long addr) 210 + static int find_symbol(void *data, const char *name, unsigned long addr) 211 211 { 212 212 struct test_stat *stat = (struct test_stat *)data; 213 213
+26 -23
kernel/kmod.c kernel/module/kmod.c
··· 1 1 /* 2 2 * kmod - the kernel module loader 3 + * 4 + * Copyright (C) 2023 Luis Chamberlain <mcgrof@kernel.org> 3 5 */ 6 + 4 7 #include <linux/module.h> 5 8 #include <linux/sched.h> 6 9 #include <linux/sched/task.h> ··· 30 27 #include <linux/uaccess.h> 31 28 32 29 #include <trace/events/module.h> 30 + #include "internal.h" 33 31 34 32 /* 35 33 * Assuming: ··· 44 40 * effect. Systems like these are very unlikely if modules are enabled. 45 41 */ 46 42 #define MAX_KMOD_CONCURRENT 50 47 - static atomic_t kmod_concurrent_max = ATOMIC_INIT(MAX_KMOD_CONCURRENT); 48 - static DECLARE_WAIT_QUEUE_HEAD(kmod_wq); 43 + static DEFINE_SEMAPHORE(kmod_concurrent_max, MAX_KMOD_CONCURRENT); 49 44 50 45 /* 51 46 * This is a restriction on having *all* MAX_KMOD_CONCURRENT threads ··· 69 66 kfree(info->argv); 70 67 } 71 68 72 - static int call_modprobe(char *module_name, int wait) 69 + static int call_modprobe(char *orig_module_name, int wait) 73 70 { 74 71 struct subprocess_info *info; 75 72 static char *envp[] = { ··· 78 75 "PATH=/sbin:/usr/sbin:/bin:/usr/bin", 79 76 NULL 80 77 }; 78 + char *module_name; 79 + int ret; 81 80 82 81 char **argv = kmalloc(sizeof(char *[5]), GFP_KERNEL); 83 82 if (!argv) 84 83 goto out; 85 84 86 - module_name = kstrdup(module_name, GFP_KERNEL); 85 + module_name = kstrdup(orig_module_name, GFP_KERNEL); 87 86 if (!module_name) 88 87 goto free_argv; 89 88 ··· 100 95 if (!info) 101 96 goto free_module_name; 102 97 103 - return call_usermodehelper_exec(info, wait | UMH_KILLABLE); 98 + ret = call_usermodehelper_exec(info, wait | UMH_KILLABLE); 99 + kmod_dup_request_announce(orig_module_name, ret); 100 + return ret; 104 101 105 102 free_module_name: 106 103 kfree(module_name); 107 104 free_argv: 108 105 kfree(argv); 109 106 out: 107 + kmod_dup_request_announce(orig_module_name, -ENOMEM); 110 108 return -ENOMEM; 111 109 } 112 110 ··· 133 125 { 134 126 va_list args; 135 127 char module_name[MODULE_NAME_LEN]; 136 - int ret; 128 + int ret, dup_ret; 137 129 138 130 /* 139 131 * We don't allow synchronous module loading from async. Module ··· 156 148 if (ret) 157 149 return ret; 158 150 159 - if (atomic_dec_if_positive(&kmod_concurrent_max) < 0) { 160 - pr_warn_ratelimited("request_module: kmod_concurrent_max (%u) close to 0 (max_modprobes: %u), for module %s, throttling...", 161 - atomic_read(&kmod_concurrent_max), 162 - MAX_KMOD_CONCURRENT, module_name); 163 - ret = wait_event_killable_timeout(kmod_wq, 164 - atomic_dec_if_positive(&kmod_concurrent_max) >= 0, 165 - MAX_KMOD_ALL_BUSY_TIMEOUT * HZ); 166 - if (!ret) { 167 - pr_warn_ratelimited("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now", 168 - module_name, MAX_KMOD_CONCURRENT, MAX_KMOD_ALL_BUSY_TIMEOUT); 169 - return -ETIME; 170 - } else if (ret == -ERESTARTSYS) { 171 - pr_warn_ratelimited("request_module: sigkill sent for modprobe %s, giving up", module_name); 172 - return ret; 173 - } 151 + ret = down_timeout(&kmod_concurrent_max, MAX_KMOD_ALL_BUSY_TIMEOUT * HZ); 152 + if (ret) { 153 + pr_warn_ratelimited("request_module: modprobe %s cannot be processed, kmod busy with %d threads for more than %d seconds now", 154 + module_name, MAX_KMOD_CONCURRENT, MAX_KMOD_ALL_BUSY_TIMEOUT); 155 + return ret; 174 156 } 175 157 176 158 trace_module_request(module_name, wait, _RET_IP_); 177 159 160 + if (kmod_dup_request_exists_wait(module_name, wait, &dup_ret)) { 161 + ret = dup_ret; 162 + goto out; 163 + } 164 + 178 165 ret = call_modprobe(module_name, wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC); 179 166 180 - atomic_inc(&kmod_concurrent_max); 181 - wake_up(&kmod_wq); 167 + out: 168 + up(&kmod_concurrent_max); 182 169 183 170 return ret; 184 171 }
+1 -2
kernel/livepatch/core.c
··· 142 142 return 0; 143 143 } 144 144 145 - static int klp_find_callback(void *data, const char *name, 146 - struct module *mod, unsigned long addr) 145 + static int klp_find_callback(void *data, const char *name, unsigned long addr) 147 146 { 148 147 struct klp_find_arg *args = data; 149 148
+99 -1
kernel/module/Kconfig
··· 22 22 23 23 if MODULES 24 24 25 + config MODULE_DEBUGFS 26 + bool 27 + 28 + config MODULE_DEBUG 29 + bool "Module debugging" 30 + depends on DEBUG_FS 31 + help 32 + Allows you to enable / disable features which can help you debug 33 + modules. You don't need these options on production systems. 34 + 35 + if MODULE_DEBUG 36 + 37 + config MODULE_STATS 38 + bool "Module statistics" 39 + depends on DEBUG_FS 40 + select MODULE_DEBUGFS 41 + help 42 + This option allows you to maintain a record of module statistics. 43 + For example, size of all modules, average size, text size, a list 44 + of failed modules and the size for each of those. For failed 45 + modules we keep track of modules which failed due to either the 46 + existing module taking too long to load or that module was already 47 + loaded. 48 + 49 + You should enable this if you are debugging production loads 50 + and want to see if userspace or the kernel is doing stupid things 51 + with loading modules when it shouldn't or if you want to help 52 + optimize userspace / kernel space module autoloading schemes. 53 + You might want to do this because failed modules tend to use 54 + up significant amount of memory, and so you'd be doing everyone a 55 + favor in avoiding these failures proactively. 56 + 57 + This functionality is also useful for those experimenting with 58 + module .text ELF section optimization. 59 + 60 + If unsure, say N. 61 + 62 + config MODULE_DEBUG_AUTOLOAD_DUPS 63 + bool "Debug duplicate modules with auto-loading" 64 + help 65 + Module autoloading allows in-kernel code to request modules through 66 + the *request_module*() API calls. This in turn just calls userspace 67 + modprobe. Although modprobe checks to see if a module is already 68 + loaded before trying to load a module there is a small time window in 69 + which multiple duplicate requests can end up in userspace and multiple 70 + modprobe calls race calling finit_module() around the same time for 71 + duplicate modules. The finit_module() system call can consume in the 72 + worst case more than twice the respective module size in virtual 73 + memory for each duplicate module requests. Although duplicate module 74 + requests are non-fatal virtual memory is a limited resource and each 75 + duplicate module request ends up just unnecessarily straining virtual 76 + memory. 77 + 78 + This debugging facility will create pr_warn() splats for duplicate 79 + module requests to help identify if module auto-loading may be the 80 + culprit to your early boot virtual memory pressure. Since virtual 81 + memory abuse caused by duplicate module requests could render a 82 + system unusable this functionality will also converge races in 83 + requests for the same module to a single request. You can boot with 84 + the module.enable_dups_trace=1 kernel parameter to use WARN_ON() 85 + instead of the pr_warn(). 86 + 87 + If the first module request used request_module_nowait() we cannot 88 + use that as the anchor to wait for duplicate module requests, since 89 + users of request_module() do want a proper return value. If a call 90 + for the same module happened earlier with request_module() though, 91 + then a duplicate request_module_nowait() would be detected. The 92 + non-wait request_module() call is synchronous and waits until modprobe 93 + completes. Subsequent auto-loading requests for the same module do 94 + not trigger a new finit_module() calls and do not strain virtual 95 + memory, and so as soon as modprobe successfully completes we remove 96 + tracking for duplicates for that module. 97 + 98 + Enable this functionality to try to debug virtual memory abuse during 99 + boot on systems which are failing to boot or if you suspect you may be 100 + straining virtual memory during boot, and you want to identify if the 101 + abuse was due to module auto-loading. These issues are currently only 102 + known to occur on systems with many CPUs (over 400) and is likely the 103 + result of udev issuing duplicate module requests for each CPU, and so 104 + module auto-loading is not the culprit. There may very well still be 105 + many duplicate module auto-loading requests which could be optimized 106 + for and this debugging facility can be used to help identify them. 107 + 108 + Only enable this for debugging system functionality, never have it 109 + enabled on real systems. 110 + 111 + config MODULE_DEBUG_AUTOLOAD_DUPS_TRACE 112 + bool "Force full stack trace when duplicates are found" 113 + depends on MODULE_DEBUG_AUTOLOAD_DUPS 114 + help 115 + Enabling this will force a full stack trace for duplicate module 116 + auto-loading requests using WARN_ON() instead of pr_warn(). You 117 + should keep this disabled at all times unless you are a developer 118 + and are doing a manual inspection and want to debug exactly why 119 + these duplicates occur. 120 + 121 + endif # MODULE_DEBUG 122 + 25 123 config MODULE_FORCE_LOAD 26 124 bool "Forced module loading" 27 125 default n ··· 149 51 config MODULE_UNLOAD_TAINT_TRACKING 150 52 bool "Tainted module unload tracking" 151 53 depends on MODULE_UNLOAD 152 - default n 54 + select MODULE_DEBUGFS 153 55 help 154 56 This option allows you to maintain a record of each unloaded 155 57 module that tainted the kernel. In addition to displaying a
+5 -1
kernel/module/Makefile
··· 7 7 # and produce insane amounts of uninteresting coverage. 8 8 KCOV_INSTRUMENT_module.o := n 9 9 10 - obj-y += main.o strict_rwx.o 10 + obj-y += main.o 11 + obj-y += strict_rwx.o 12 + obj-y += kmod.o 13 + obj-$(CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS) += dups.o 11 14 obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o 12 15 obj-$(CONFIG_MODULE_SIG) += signing.o 13 16 obj-$(CONFIG_LIVEPATCH) += livepatch.o ··· 22 19 obj-$(CONFIG_KGDB_KDB) += kdb.o 23 20 obj-$(CONFIG_MODVERSIONS) += version.o 24 21 obj-$(CONFIG_MODULE_UNLOAD_TAINT_TRACKING) += tracking.o 22 + obj-$(CONFIG_MODULE_STATS) += stats.o
+5 -1
kernel/module/decompress.c
··· 267 267 zstd_dec.size = PAGE_SIZE; 268 268 269 269 ret = zstd_decompress_stream(dstream, &zstd_dec, &zstd_buf); 270 - kunmap(page); 270 + kunmap_local(zstd_dec.dst); 271 271 retval = zstd_get_error_code(ret); 272 272 if (retval) 273 273 break; ··· 296 296 unsigned int n_pages; 297 297 ssize_t data_size; 298 298 int error; 299 + 300 + #if defined(CONFIG_MODULE_STATS) 301 + info->compressed_len = size; 302 + #endif 299 303 300 304 /* 301 305 * Start with number of pages twice as big as needed for
+246
kernel/module/dups.c
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* 3 + * kmod dups - the kernel module autoloader duplicate suppressor 4 + * 5 + * Copyright (C) 2023 Luis Chamberlain <mcgrof@kernel.org> 6 + */ 7 + 8 + #define pr_fmt(fmt) "module: " fmt 9 + 10 + #include <linux/module.h> 11 + #include <linux/sched.h> 12 + #include <linux/sched/task.h> 13 + #include <linux/binfmts.h> 14 + #include <linux/syscalls.h> 15 + #include <linux/unistd.h> 16 + #include <linux/kmod.h> 17 + #include <linux/slab.h> 18 + #include <linux/completion.h> 19 + #include <linux/cred.h> 20 + #include <linux/file.h> 21 + #include <linux/fdtable.h> 22 + #include <linux/workqueue.h> 23 + #include <linux/security.h> 24 + #include <linux/mount.h> 25 + #include <linux/kernel.h> 26 + #include <linux/init.h> 27 + #include <linux/resource.h> 28 + #include <linux/notifier.h> 29 + #include <linux/suspend.h> 30 + #include <linux/rwsem.h> 31 + #include <linux/ptrace.h> 32 + #include <linux/async.h> 33 + #include <linux/uaccess.h> 34 + 35 + #undef MODULE_PARAM_PREFIX 36 + #define MODULE_PARAM_PREFIX "module." 37 + static bool enable_dups_trace = IS_ENABLED(CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS_TRACE); 38 + module_param(enable_dups_trace, bool_enable_only, 0644); 39 + 40 + /* 41 + * Protects dup_kmod_reqs list, adds / removals with RCU. 42 + */ 43 + static DEFINE_MUTEX(kmod_dup_mutex); 44 + static LIST_HEAD(dup_kmod_reqs); 45 + 46 + struct kmod_dup_req { 47 + struct list_head list; 48 + char name[MODULE_NAME_LEN]; 49 + struct completion first_req_done; 50 + struct work_struct complete_work; 51 + struct delayed_work delete_work; 52 + int dup_ret; 53 + }; 54 + 55 + static struct kmod_dup_req *kmod_dup_request_lookup(char *module_name) 56 + { 57 + struct kmod_dup_req *kmod_req; 58 + 59 + list_for_each_entry_rcu(kmod_req, &dup_kmod_reqs, list, 60 + lockdep_is_held(&kmod_dup_mutex)) { 61 + if (strlen(kmod_req->name) == strlen(module_name) && 62 + !memcmp(kmod_req->name, module_name, strlen(module_name))) { 63 + return kmod_req; 64 + } 65 + } 66 + 67 + return NULL; 68 + } 69 + 70 + static void kmod_dup_request_delete(struct work_struct *work) 71 + { 72 + struct kmod_dup_req *kmod_req; 73 + kmod_req = container_of(to_delayed_work(work), struct kmod_dup_req, delete_work); 74 + 75 + /* 76 + * The typical situation is a module successully loaded. In that 77 + * situation the module will be present already in userspace. If 78 + * new requests come in after that, userspace will already know the 79 + * module is loaded so will just return 0 right away. There is still 80 + * a small chance right after we delete this entry new request_module() 81 + * calls may happen after that, they can happen. These heuristics 82 + * are to protect finit_module() abuse for auto-loading, if modules 83 + * are still tryign to auto-load even if a module is already loaded, 84 + * that's on them, and those inneficiencies should not be fixed by 85 + * kmod. The inneficies there are a call to modprobe and modprobe 86 + * just returning 0. 87 + */ 88 + mutex_lock(&kmod_dup_mutex); 89 + list_del_rcu(&kmod_req->list); 90 + synchronize_rcu(); 91 + mutex_unlock(&kmod_dup_mutex); 92 + kfree(kmod_req); 93 + } 94 + 95 + static void kmod_dup_request_complete(struct work_struct *work) 96 + { 97 + struct kmod_dup_req *kmod_req; 98 + 99 + kmod_req = container_of(work, struct kmod_dup_req, complete_work); 100 + 101 + /* 102 + * This will ensure that the kernel will let all the waiters get 103 + * informed its time to check the return value. It's time to 104 + * go home. 105 + */ 106 + complete_all(&kmod_req->first_req_done); 107 + 108 + /* 109 + * Now that we have allowed prior request_module() calls to go on 110 + * with life, let's schedule deleting this entry. We don't have 111 + * to do it right away, but we *eventually* want to do it so to not 112 + * let this linger forever as this is just a boot optimization for 113 + * possible abuses of vmalloc() incurred by finit_module() thrashing. 114 + */ 115 + queue_delayed_work(system_wq, &kmod_req->delete_work, 60 * HZ); 116 + } 117 + 118 + bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) 119 + { 120 + struct kmod_dup_req *kmod_req, *new_kmod_req; 121 + int ret; 122 + 123 + /* 124 + * Pre-allocate the entry in case we have to use it later 125 + * to avoid contention with the mutex. 126 + */ 127 + new_kmod_req = kzalloc(sizeof(*new_kmod_req), GFP_KERNEL); 128 + if (!new_kmod_req) 129 + return false; 130 + 131 + memcpy(new_kmod_req->name, module_name, strlen(module_name)); 132 + INIT_WORK(&new_kmod_req->complete_work, kmod_dup_request_complete); 133 + INIT_DELAYED_WORK(&new_kmod_req->delete_work, kmod_dup_request_delete); 134 + init_completion(&new_kmod_req->first_req_done); 135 + 136 + mutex_lock(&kmod_dup_mutex); 137 + 138 + kmod_req = kmod_dup_request_lookup(module_name); 139 + if (!kmod_req) { 140 + /* 141 + * If the first request that came through for a module 142 + * was with request_module_nowait() we cannot wait for it 143 + * and share its return value with other users which may 144 + * have used request_module() and need a proper return value 145 + * so just skip using them as an anchor. 146 + * 147 + * If a prior request to this one came through with 148 + * request_module() though, then a request_module_nowait() 149 + * would benefit from duplicate detection. 150 + */ 151 + if (!wait) { 152 + kfree(new_kmod_req); 153 + pr_debug("New request_module_nowait() for %s -- cannot track duplicates for this request\n", module_name); 154 + mutex_unlock(&kmod_dup_mutex); 155 + return false; 156 + } 157 + 158 + /* 159 + * There was no duplicate, just add the request so we can 160 + * keep tab on duplicates later. 161 + */ 162 + pr_debug("New request_module() for %s\n", module_name); 163 + list_add_rcu(&new_kmod_req->list, &dup_kmod_reqs); 164 + mutex_unlock(&kmod_dup_mutex); 165 + return false; 166 + } 167 + mutex_unlock(&kmod_dup_mutex); 168 + 169 + /* We are dealing with a duplicate request now */ 170 + kfree(new_kmod_req); 171 + 172 + /* 173 + * To fix these try to use try_then_request_module() instead as that 174 + * will check if the component you are looking for is present or not. 175 + * You could also just queue a single request to load the module once, 176 + * instead of having each and everything you need try to request for 177 + * the module. 178 + * 179 + * Duplicate request_module() calls can cause quite a bit of wasted 180 + * vmalloc() space when racing with userspace. 181 + */ 182 + if (enable_dups_trace) 183 + WARN(1, "module-autoload: duplicate request for module %s\n", module_name); 184 + else 185 + pr_warn("module-autoload: duplicate request for module %s\n", module_name); 186 + 187 + if (!wait) { 188 + /* 189 + * If request_module_nowait() was used then the user just 190 + * wanted to issue the request and if another module request 191 + * was already its way with the same name we don't care for 192 + * the return value either. Let duplicate request_module_nowait() 193 + * calls bail out right away. 194 + */ 195 + *dup_ret = 0; 196 + return true; 197 + } 198 + 199 + /* 200 + * If a duplicate request_module() was used they *may* care for 201 + * the return value, so we have no other option but to wait for 202 + * the first caller to complete. If the first caller used 203 + * the request_module_nowait() call, subsquent callers will 204 + * deal with the comprmise of getting a successful call with this 205 + * optimization enabled ... 206 + */ 207 + ret = wait_for_completion_state(&kmod_req->first_req_done, 208 + TASK_UNINTERRUPTIBLE | TASK_KILLABLE); 209 + if (ret) { 210 + *dup_ret = ret; 211 + return true; 212 + } 213 + 214 + /* Now the duplicate request has the same exact return value as the first request */ 215 + *dup_ret = kmod_req->dup_ret; 216 + 217 + return true; 218 + } 219 + 220 + void kmod_dup_request_announce(char *module_name, int ret) 221 + { 222 + struct kmod_dup_req *kmod_req; 223 + 224 + mutex_lock(&kmod_dup_mutex); 225 + 226 + kmod_req = kmod_dup_request_lookup(module_name); 227 + if (!kmod_req) 228 + goto out; 229 + 230 + kmod_req->dup_ret = ret; 231 + 232 + /* 233 + * If we complete() here we may allow duplicate threads 234 + * to continue before the first one that submitted the 235 + * request. We're in no rush also, given that each and 236 + * every bounce back to userspace is slow we avoid that 237 + * with a slight delay here. So queueue up the completion 238 + * and let duplicates suffer, just wait a tad bit longer. 239 + * There is no rush. But we also don't want to hold the 240 + * caller up forever or introduce any boot delays. 241 + */ 242 + queue_work(system_wq, &kmod_req->complete_work); 243 + 244 + out: 245 + mutex_unlock(&kmod_dup_mutex); 246 + }
+115 -25
kernel/module/internal.h
··· 3 3 * 4 4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. 5 5 * Written by David Howells (dhowells@redhat.com) 6 + * Copyright (C) 2023 Luis Chamberlain <mcgrof@kernel.org> 6 7 */ 7 8 8 9 #include <linux/elf.h> ··· 18 17 #define ARCH_SHF_SMALL 0 19 18 #endif 20 19 21 - /* If this is set, the section belongs in the init part of the module */ 22 - #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG - 1)) 20 + /* 21 + * Use highest 4 bits of sh_entsize to store the mod_mem_type of this 22 + * section. This leaves 28 bits for offset on 32-bit systems, which is 23 + * about 256 MiB (WARN_ON_ONCE if we exceed that). 24 + */ 25 + 26 + #define SH_ENTSIZE_TYPE_BITS 4 27 + #define SH_ENTSIZE_TYPE_SHIFT (BITS_PER_LONG - SH_ENTSIZE_TYPE_BITS) 28 + #define SH_ENTSIZE_TYPE_MASK ((1UL << SH_ENTSIZE_TYPE_BITS) - 1) 29 + #define SH_ENTSIZE_OFFSET_MASK ((1UL << (BITS_PER_LONG - SH_ENTSIZE_TYPE_BITS)) - 1) 30 + 23 31 /* Maximum number of characters written by module_flags() */ 24 32 #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4) 25 - 26 - #ifndef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 27 - #define data_layout core_layout 28 - #endif 29 - 30 - /* 31 - * Modules' sections will be aligned on page boundaries 32 - * to ensure complete separation of code and data, but 33 - * only when CONFIG_STRICT_MODULE_RWX=y 34 - */ 35 - static inline unsigned int strict_align(unsigned int size) 36 - { 37 - if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) 38 - return PAGE_ALIGN(size); 39 - else 40 - return size; 41 - } 42 33 43 34 extern struct mutex module_mutex; 44 35 extern struct list_head modules; ··· 46 53 extern const s32 __start___kcrctab[]; 47 54 extern const s32 __start___kcrctab_gpl[]; 48 55 49 - #include <linux/dynamic_debug.h> 50 56 struct load_info { 51 57 const char *name; 52 58 /* pointer to module in temporary copy, freed at end of load_module() */ ··· 55 63 Elf_Shdr *sechdrs; 56 64 char *secstrings, *strtab; 57 65 unsigned long symoffs, stroffs, init_typeoffs, core_typeoffs; 58 - struct _ddebug_info dyndbg; 59 66 bool sig_ok; 60 67 #ifdef CONFIG_KALLSYMS 61 68 unsigned long mod_kallsyms_init_off; 62 69 #endif 63 70 #ifdef CONFIG_MODULE_DECOMPRESS 71 + #ifdef CONFIG_MODULE_STATS 72 + unsigned long compressed_len; 73 + #endif 64 74 struct page **pages; 65 75 unsigned int max_pages; 66 76 unsigned int used_pages; ··· 95 101 bool find_symbol(struct find_symbol_arg *fsa); 96 102 struct module *find_module_all(const char *name, size_t len, bool even_unformed); 97 103 int cmp_name(const void *name, const void *sym); 98 - long module_get_offset(struct module *mod, unsigned int *size, Elf_Shdr *sechdr, 99 - unsigned int section); 104 + long module_get_offset_and_type(struct module *mod, enum mod_mem_type type, 105 + Elf_Shdr *sechdr, unsigned int section); 100 106 char *module_flags(struct module *mod, char *buf, bool show_state); 101 107 size_t module_flags_taint(unsigned long taints, char *buf); 108 + 109 + char *module_next_tag_pair(char *string, unsigned long *secsize); 110 + 111 + #define for_each_modinfo_entry(entry, info, name) \ 112 + for (entry = get_modinfo(info, name); entry; entry = get_next_modinfo(info, name, entry)) 102 113 103 114 static inline void module_assert_mutex_or_preempt(void) 104 115 { ··· 147 148 #endif 148 149 } 149 150 151 + /** 152 + * enum fail_dup_mod_reason - state at which a duplicate module was detected 153 + * 154 + * @FAIL_DUP_MOD_BECOMING: the module is read properly, passes all checks but 155 + * we've determined that another module with the same name is already loaded 156 + * or being processed on our &modules list. This happens on early_mod_check() 157 + * right before layout_and_allocate(). The kernel would have already 158 + * vmalloc()'d space for the entire module through finit_module(). If 159 + * decompression was used two vmap() spaces were used. These failures can 160 + * happen when userspace has not seen the module present on the kernel and 161 + * tries to load the module multiple times at same time. 162 + * @FAIL_DUP_MOD_LOAD: the module has been read properly, passes all validation 163 + * checks and the kernel determines that the module was unique and because 164 + * of this allocated yet another private kernel copy of the module space in 165 + * layout_and_allocate() but after this determined in add_unformed_module() 166 + * that another module with the same name is already loaded or being processed. 167 + * These failures should be mitigated as much as possible and are indicative 168 + * of really fast races in loading modules. Without module decompression 169 + * they waste twice as much vmap space. With module decompression three 170 + * times the module's size vmap space is wasted. 171 + */ 172 + enum fail_dup_mod_reason { 173 + FAIL_DUP_MOD_BECOMING = 0, 174 + FAIL_DUP_MOD_LOAD, 175 + }; 176 + 177 + #ifdef CONFIG_MODULE_DEBUGFS 178 + extern struct dentry *mod_debugfs_root; 179 + #endif 180 + 181 + #ifdef CONFIG_MODULE_STATS 182 + 183 + #define mod_stat_add_long(count, var) atomic_long_add(count, var) 184 + #define mod_stat_inc(name) atomic_inc(name) 185 + 186 + extern atomic_long_t total_mod_size; 187 + extern atomic_long_t total_text_size; 188 + extern atomic_long_t invalid_kread_bytes; 189 + extern atomic_long_t invalid_decompress_bytes; 190 + 191 + extern atomic_t modcount; 192 + extern atomic_t failed_kreads; 193 + extern atomic_t failed_decompress; 194 + struct mod_fail_load { 195 + struct list_head list; 196 + char name[MODULE_NAME_LEN]; 197 + atomic_long_t count; 198 + unsigned long dup_fail_mask; 199 + }; 200 + 201 + int try_add_failed_module(const char *name, enum fail_dup_mod_reason reason); 202 + void mod_stat_bump_invalid(struct load_info *info, int flags); 203 + void mod_stat_bump_becoming(struct load_info *info, int flags); 204 + 205 + #else 206 + 207 + #define mod_stat_add_long(name, var) 208 + #define mod_stat_inc(name) 209 + 210 + static inline int try_add_failed_module(const char *name, 211 + enum fail_dup_mod_reason reason) 212 + { 213 + return 0; 214 + } 215 + 216 + static inline void mod_stat_bump_invalid(struct load_info *info, int flags) 217 + { 218 + } 219 + 220 + static inline void mod_stat_bump_becoming(struct load_info *info, int flags) 221 + { 222 + } 223 + 224 + #endif /* CONFIG_MODULE_STATS */ 225 + 226 + #ifdef CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS 227 + bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret); 228 + void kmod_dup_request_announce(char *module_name, int ret); 229 + #else 230 + static inline bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) 231 + { 232 + return false; 233 + } 234 + 235 + static inline void kmod_dup_request_announce(char *module_name, int ret) 236 + { 237 + } 238 + #endif 239 + 150 240 #ifdef CONFIG_MODULE_UNLOAD_TAINT_TRACKING 151 241 struct mod_unload_taint { 152 242 struct list_head list; ··· 278 190 #endif 279 191 unsigned long addr_min; 280 192 unsigned long addr_max; 193 + #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 194 + unsigned long data_addr_min; 195 + unsigned long data_addr_max; 196 + #endif 281 197 }; 282 198 283 199 extern struct mod_tree_root mod_tree; 284 - extern struct mod_tree_root mod_data_tree; 285 200 286 201 #ifdef CONFIG_MODULES_TREE_LOOKUP 287 202 void mod_tree_insert(struct module *mod); ··· 315 224 void module_enable_x(const struct module *mod); 316 225 int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, 317 226 char *secstrings, struct module *mod); 318 - bool module_check_misalignment(const struct module *mod); 319 227 320 228 #ifdef CONFIG_MODULE_SIG 321 229 int module_sig_check(struct load_info *info, int flags);
+37 -41
kernel/module/kallsyms.c
··· 6 6 */ 7 7 8 8 #include <linux/module.h> 9 + #include <linux/module_symbol.h> 9 10 #include <linux/kallsyms.h> 10 11 #include <linux/buildid.h> 11 12 #include <linux/bsearch.h> ··· 79 78 unsigned int shnum, unsigned int pcpundx) 80 79 { 81 80 const Elf_Shdr *sec; 81 + enum mod_mem_type type; 82 82 83 83 if (src->st_shndx == SHN_UNDEF || 84 84 src->st_shndx >= shnum || ··· 92 90 #endif 93 91 94 92 sec = sechdrs + src->st_shndx; 93 + type = sec->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT; 95 94 if (!(sec->sh_flags & SHF_ALLOC) 96 95 #ifndef CONFIG_KALLSYMS_ALL 97 96 || !(sec->sh_flags & SHF_EXECINSTR) 98 97 #endif 99 - || (sec->sh_entsize & INIT_OFFSET_MASK)) 98 + || mod_mem_type_is_init(type)) 100 99 return false; 101 100 102 101 return true; ··· 116 113 Elf_Shdr *strsect = info->sechdrs + info->index.str; 117 114 const Elf_Sym *src; 118 115 unsigned int i, nsrc, ndst, strtab_size = 0; 116 + struct module_memory *mod_mem_data = &mod->mem[MOD_DATA]; 117 + struct module_memory *mod_mem_init_data = &mod->mem[MOD_INIT_DATA]; 119 118 120 119 /* Put symbol section at end of init part of module. */ 121 120 symsect->sh_flags |= SHF_ALLOC; 122 - symsect->sh_entsize = module_get_offset(mod, &mod->init_layout.size, symsect, 123 - info->index.sym) | INIT_OFFSET_MASK; 121 + symsect->sh_entsize = module_get_offset_and_type(mod, MOD_INIT_DATA, 122 + symsect, info->index.sym); 124 123 pr_debug("\t%s\n", info->secstrings + symsect->sh_name); 125 124 126 125 src = (void *)info->hdr + symsect->sh_offset; ··· 139 134 } 140 135 141 136 /* Append room for core symbols at end of core part. */ 142 - info->symoffs = ALIGN(mod->data_layout.size, symsect->sh_addralign ?: 1); 143 - info->stroffs = mod->data_layout.size = info->symoffs + ndst * sizeof(Elf_Sym); 144 - mod->data_layout.size += strtab_size; 137 + info->symoffs = ALIGN(mod_mem_data->size, symsect->sh_addralign ?: 1); 138 + info->stroffs = mod_mem_data->size = info->symoffs + ndst * sizeof(Elf_Sym); 139 + mod_mem_data->size += strtab_size; 145 140 /* Note add_kallsyms() computes strtab_size as core_typeoffs - stroffs */ 146 - info->core_typeoffs = mod->data_layout.size; 147 - mod->data_layout.size += ndst * sizeof(char); 148 - mod->data_layout.size = strict_align(mod->data_layout.size); 141 + info->core_typeoffs = mod_mem_data->size; 142 + mod_mem_data->size += ndst * sizeof(char); 149 143 150 144 /* Put string table section at end of init part of module. */ 151 145 strsect->sh_flags |= SHF_ALLOC; 152 - strsect->sh_entsize = module_get_offset(mod, &mod->init_layout.size, strsect, 153 - info->index.str) | INIT_OFFSET_MASK; 146 + strsect->sh_entsize = module_get_offset_and_type(mod, MOD_INIT_DATA, 147 + strsect, info->index.str); 154 148 pr_debug("\t%s\n", info->secstrings + strsect->sh_name); 155 149 156 150 /* We'll tack temporary mod_kallsyms on the end. */ 157 - mod->init_layout.size = ALIGN(mod->init_layout.size, 158 - __alignof__(struct mod_kallsyms)); 159 - info->mod_kallsyms_init_off = mod->init_layout.size; 160 - mod->init_layout.size += sizeof(struct mod_kallsyms); 161 - info->init_typeoffs = mod->init_layout.size; 162 - mod->init_layout.size += nsrc * sizeof(char); 163 - mod->init_layout.size = strict_align(mod->init_layout.size); 151 + mod_mem_init_data->size = ALIGN(mod_mem_init_data->size, 152 + __alignof__(struct mod_kallsyms)); 153 + info->mod_kallsyms_init_off = mod_mem_init_data->size; 154 + 155 + mod_mem_init_data->size += sizeof(struct mod_kallsyms); 156 + info->init_typeoffs = mod_mem_init_data->size; 157 + mod_mem_init_data->size += nsrc * sizeof(char); 164 158 } 165 159 166 160 /* ··· 175 171 char *s; 176 172 Elf_Shdr *symsec = &info->sechdrs[info->index.sym]; 177 173 unsigned long strtab_size; 174 + void *data_base = mod->mem[MOD_DATA].base; 175 + void *init_data_base = mod->mem[MOD_INIT_DATA].base; 178 176 179 177 /* Set up to point into init section. */ 180 - mod->kallsyms = (void __rcu *)mod->init_layout.base + 178 + mod->kallsyms = (void __rcu *)init_data_base + 181 179 info->mod_kallsyms_init_off; 182 180 183 181 rcu_read_lock(); ··· 189 183 /* Make sure we get permanent strtab: don't use info->strtab. */ 190 184 rcu_dereference(mod->kallsyms)->strtab = 191 185 (void *)info->sechdrs[info->index.str].sh_addr; 192 - rcu_dereference(mod->kallsyms)->typetab = mod->init_layout.base + info->init_typeoffs; 186 + rcu_dereference(mod->kallsyms)->typetab = init_data_base + info->init_typeoffs; 193 187 194 188 /* 195 189 * Now populate the cut down core kallsyms for after init 196 190 * and set types up while we still have access to sections. 197 191 */ 198 - mod->core_kallsyms.symtab = dst = mod->data_layout.base + info->symoffs; 199 - mod->core_kallsyms.strtab = s = mod->data_layout.base + info->stroffs; 200 - mod->core_kallsyms.typetab = mod->data_layout.base + info->core_typeoffs; 192 + mod->core_kallsyms.symtab = dst = data_base + info->symoffs; 193 + mod->core_kallsyms.strtab = s = data_base + info->stroffs; 194 + mod->core_kallsyms.typetab = data_base + info->core_typeoffs; 201 195 strtab_size = info->core_typeoffs - info->stroffs; 202 196 src = rcu_dereference(mod->kallsyms)->symtab; 203 197 for (ndst = i = 0; i < rcu_dereference(mod->kallsyms)->num_symtab; i++) { ··· 244 238 } 245 239 #endif 246 240 247 - /* 248 - * This ignores the intensely annoying "mapping symbols" found 249 - * in ARM ELF files: $a, $t and $d. 250 - */ 251 - static inline int is_arm_mapping_symbol(const char *str) 252 - { 253 - if (str[0] == '.' && str[1] == 'L') 254 - return true; 255 - return str[0] == '$' && strchr("axtd", str[1]) && 256 - (str[2] == '\0' || str[2] == '.'); 257 - } 258 - 259 241 static const char *kallsyms_symbol_name(struct mod_kallsyms *kallsyms, unsigned int symnum) 260 242 { 261 243 return kallsyms->strtab + kallsyms->symtab[symnum].st_name; ··· 261 267 unsigned int i, best = 0; 262 268 unsigned long nextval, bestval; 263 269 struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms); 270 + struct module_memory *mod_mem; 264 271 265 272 /* At worse, next value is at end of module */ 266 273 if (within_module_init(addr, mod)) 267 - nextval = (unsigned long)mod->init_layout.base + mod->init_layout.text_size; 274 + mod_mem = &mod->mem[MOD_INIT_TEXT]; 268 275 else 269 - nextval = (unsigned long)mod->core_layout.base + mod->core_layout.text_size; 276 + mod_mem = &mod->mem[MOD_TEXT]; 277 + 278 + nextval = (unsigned long)mod_mem->base + mod_mem->size; 270 279 271 280 bestval = kallsyms_symbol_value(&kallsyms->symtab[best]); 272 281 ··· 289 292 * and inserted at a whim. 290 293 */ 291 294 if (*kallsyms_symbol_name(kallsyms, i) == '\0' || 292 - is_arm_mapping_symbol(kallsyms_symbol_name(kallsyms, i))) 295 + is_mapping_symbol(kallsyms_symbol_name(kallsyms, i))) 293 296 continue; 294 297 295 298 if (thisval <= addr && thisval > bestval) { ··· 502 505 } 503 506 504 507 int module_kallsyms_on_each_symbol(const char *modname, 505 - int (*fn)(void *, const char *, 506 - struct module *, unsigned long), 508 + int (*fn)(void *, const char *, unsigned long), 507 509 void *data) 508 510 { 509 511 struct module *mod; ··· 531 535 continue; 532 536 533 537 ret = fn(data, kallsyms_symbol_name(kallsyms, i), 534 - mod, kallsyms_symbol_value(sym)); 538 + kallsyms_symbol_value(sym)); 535 539 if (ret != 0) 536 540 goto out; 537 541 }
+9 -8
kernel/module/kdb.c
··· 26 26 if (mod->state == MODULE_STATE_UNFORMED) 27 27 continue; 28 28 29 - kdb_printf("%-20s%8u", mod->name, mod->core_layout.size); 30 - #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 31 - kdb_printf("/%8u", mod->data_layout.size); 32 - #endif 29 + kdb_printf("%-20s%8u", mod->name, mod->mem[MOD_TEXT].size); 30 + kdb_printf("/%8u", mod->mem[MOD_RODATA].size); 31 + kdb_printf("/%8u", mod->mem[MOD_RO_AFTER_INIT].size); 32 + kdb_printf("/%8u", mod->mem[MOD_DATA].size); 33 + 33 34 kdb_printf(" 0x%px ", (void *)mod); 34 35 #ifdef CONFIG_MODULE_UNLOAD 35 36 kdb_printf("%4d ", module_refcount(mod)); ··· 41 40 kdb_printf(" (Loading)"); 42 41 else 43 42 kdb_printf(" (Live)"); 44 - kdb_printf(" 0x%px", mod->core_layout.base); 45 - #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 46 - kdb_printf("/0x%px", mod->data_layout.base); 47 - #endif 43 + kdb_printf(" 0x%px", mod->mem[MOD_TEXT].base); 44 + kdb_printf("/0x%px", mod->mem[MOD_RODATA].base); 45 + kdb_printf("/0x%px", mod->mem[MOD_RO_AFTER_INIT].base); 46 + kdb_printf("/0x%px", mod->mem[MOD_DATA].base); 48 47 49 48 #ifdef CONFIG_MODULE_UNLOAD 50 49 {
+619 -486
kernel/module/main.c
··· 2 2 /* 3 3 * Copyright (C) 2002 Richard Henderson 4 4 * Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM. 5 + * Copyright (C) 2023 Luis Chamberlain <mcgrof@kernel.org> 5 6 */ 6 7 7 8 #define INCLUDE_VERMAGIC ··· 56 55 #include <linux/dynamic_debug.h> 57 56 #include <linux/audit.h> 58 57 #include <linux/cfi.h> 58 + #include <linux/debugfs.h> 59 59 #include <uapi/linux/module.h> 60 60 #include "internal.h" 61 61 ··· 82 80 .addr_min = -1UL, 83 81 }; 84 82 85 - #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 86 - struct mod_tree_root mod_data_tree __cacheline_aligned = { 87 - .addr_min = -1UL, 88 - }; 89 - #endif 90 - 91 83 struct symsearch { 92 84 const struct kernel_symbol *start, *stop; 93 85 const s32 *crcs; ··· 89 93 }; 90 94 91 95 /* 92 - * Bounds of module text, for speeding up __module_address. 96 + * Bounds of module memory, for speeding up __module_address. 93 97 * Protected by module_mutex. 94 98 */ 95 - static void __mod_update_bounds(void *base, unsigned int size, struct mod_tree_root *tree) 99 + static void __mod_update_bounds(enum mod_mem_type type __maybe_unused, void *base, 100 + unsigned int size, struct mod_tree_root *tree) 96 101 { 97 102 unsigned long min = (unsigned long)base; 98 103 unsigned long max = min + size; 99 104 105 + #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 106 + if (mod_mem_type_is_core_data(type)) { 107 + if (min < tree->data_addr_min) 108 + tree->data_addr_min = min; 109 + if (max > tree->data_addr_max) 110 + tree->data_addr_max = max; 111 + return; 112 + } 113 + #endif 100 114 if (min < tree->addr_min) 101 115 tree->addr_min = min; 102 116 if (max > tree->addr_max) ··· 115 109 116 110 static void mod_update_bounds(struct module *mod) 117 111 { 118 - __mod_update_bounds(mod->core_layout.base, mod->core_layout.size, &mod_tree); 119 - if (mod->init_layout.size) 120 - __mod_update_bounds(mod->init_layout.base, mod->init_layout.size, &mod_tree); 121 - #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 122 - __mod_update_bounds(mod->data_layout.base, mod->data_layout.size, &mod_data_tree); 123 - #endif 112 + for_each_mod_mem_type(type) { 113 + struct module_memory *mod_mem = &mod->mem[type]; 114 + 115 + if (mod_mem->size) 116 + __mod_update_bounds(type, mod_mem->base, mod_mem->size, &mod_tree); 117 + } 124 118 } 125 119 126 120 /* Block module loading/unloading? */ ··· 565 559 struct module_use *use; 566 560 567 561 list_for_each_entry(use, &b->source_list, source_list) { 568 - if (use->source == a) { 569 - pr_debug("%s uses %s!\n", a->name, b->name); 562 + if (use->source == a) 570 563 return 1; 571 - } 572 564 } 573 565 pr_debug("%s does not use %s!\n", a->name, b->name); 574 566 return 0; ··· 930 926 static ssize_t show_coresize(struct module_attribute *mattr, 931 927 struct module_kobject *mk, char *buffer) 932 928 { 933 - return sprintf(buffer, "%u\n", mk->mod->core_layout.size); 929 + unsigned int size = mk->mod->mem[MOD_TEXT].size; 930 + 931 + if (!IS_ENABLED(CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC)) { 932 + for_class_mod_mem_type(type, core_data) 933 + size += mk->mod->mem[type].size; 934 + } 935 + return sprintf(buffer, "%u\n", size); 934 936 } 935 937 936 938 static struct module_attribute modinfo_coresize = ··· 946 936 static ssize_t show_datasize(struct module_attribute *mattr, 947 937 struct module_kobject *mk, char *buffer) 948 938 { 949 - return sprintf(buffer, "%u\n", mk->mod->data_layout.size); 939 + unsigned int size = 0; 940 + 941 + for_class_mod_mem_type(type, core_data) 942 + size += mk->mod->mem[type].size; 943 + return sprintf(buffer, "%u\n", size); 950 944 } 951 945 952 946 static struct module_attribute modinfo_datasize = ··· 960 946 static ssize_t show_initsize(struct module_attribute *mattr, 961 947 struct module_kobject *mk, char *buffer) 962 948 { 963 - return sprintf(buffer, "%u\n", mk->mod->init_layout.size); 949 + unsigned int size = 0; 950 + 951 + for_class_mod_mem_type(type, init) 952 + size += mk->mod->mem[type].size; 953 + return sprintf(buffer, "%u\n", size); 964 954 } 965 955 966 956 static struct module_attribute modinfo_initsize = ··· 1016 998 #endif 1017 999 } 1018 1000 1019 - static char *get_modinfo(const struct load_info *info, const char *tag); 1001 + /* Parse tag=value strings from .modinfo section */ 1002 + char *module_next_tag_pair(char *string, unsigned long *secsize) 1003 + { 1004 + /* Skip non-zero chars */ 1005 + while (string[0]) { 1006 + string++; 1007 + if ((*secsize)-- <= 1) 1008 + return NULL; 1009 + } 1010 + 1011 + /* Skip any zero padding. */ 1012 + while (!string[0]) { 1013 + string++; 1014 + if ((*secsize)-- <= 1) 1015 + return NULL; 1016 + } 1017 + return string; 1018 + } 1019 + 1020 1020 static char *get_next_modinfo(const struct load_info *info, const char *tag, 1021 - char *prev); 1021 + char *prev) 1022 + { 1023 + char *p; 1024 + unsigned int taglen = strlen(tag); 1025 + Elf_Shdr *infosec = &info->sechdrs[info->index.info]; 1026 + unsigned long size = infosec->sh_size; 1027 + 1028 + /* 1029 + * get_modinfo() calls made before rewrite_section_headers() 1030 + * must use sh_offset, as sh_addr isn't set! 1031 + */ 1032 + char *modinfo = (char *)info->hdr + infosec->sh_offset; 1033 + 1034 + if (prev) { 1035 + size -= prev - modinfo; 1036 + modinfo = module_next_tag_pair(prev, &size); 1037 + } 1038 + 1039 + for (p = modinfo; p; p = module_next_tag_pair(p, &size)) { 1040 + if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') 1041 + return p + taglen + 1; 1042 + } 1043 + return NULL; 1044 + } 1045 + 1046 + static char *get_modinfo(const struct load_info *info, const char *tag) 1047 + { 1048 + return get_next_modinfo(info, tag, NULL); 1049 + } 1022 1050 1023 1051 static int verify_namespace_is_imported(const struct load_info *info, 1024 1052 const struct kernel_symbol *sym, ··· 1075 1011 1076 1012 namespace = kernel_symbol_namespace(sym); 1077 1013 if (namespace && namespace[0]) { 1078 - imported_namespace = get_modinfo(info, "import_ns"); 1079 - while (imported_namespace) { 1014 + for_each_modinfo_entry(imported_namespace, info, "import_ns") { 1080 1015 if (strcmp(namespace, imported_namespace) == 0) 1081 1016 return 0; 1082 - imported_namespace = get_next_modinfo( 1083 - info, "import_ns", imported_namespace); 1084 1017 } 1085 1018 #ifdef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS 1086 1019 pr_warn( ··· 1204 1143 { 1205 1144 } 1206 1145 1146 + static bool mod_mem_use_vmalloc(enum mod_mem_type type) 1147 + { 1148 + return IS_ENABLED(CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC) && 1149 + mod_mem_type_is_core_data(type); 1150 + } 1151 + 1152 + static void *module_memory_alloc(unsigned int size, enum mod_mem_type type) 1153 + { 1154 + if (mod_mem_use_vmalloc(type)) 1155 + return vzalloc(size); 1156 + return module_alloc(size); 1157 + } 1158 + 1159 + static void module_memory_free(void *ptr, enum mod_mem_type type) 1160 + { 1161 + if (mod_mem_use_vmalloc(type)) 1162 + vfree(ptr); 1163 + else 1164 + module_memfree(ptr); 1165 + } 1166 + 1167 + static void free_mod_mem(struct module *mod) 1168 + { 1169 + for_each_mod_mem_type(type) { 1170 + struct module_memory *mod_mem = &mod->mem[type]; 1171 + 1172 + if (type == MOD_DATA) 1173 + continue; 1174 + 1175 + /* Free lock-classes; relies on the preceding sync_rcu(). */ 1176 + lockdep_free_key_range(mod_mem->base, mod_mem->size); 1177 + if (mod_mem->size) 1178 + module_memory_free(mod_mem->base, type); 1179 + } 1180 + 1181 + /* MOD_DATA hosts mod, so free it at last */ 1182 + lockdep_free_key_range(mod->mem[MOD_DATA].base, mod->mem[MOD_DATA].size); 1183 + module_memory_free(mod->mem[MOD_DATA].base, MOD_DATA); 1184 + } 1185 + 1207 1186 /* Free a module, remove from lists, etc. */ 1208 1187 static void free_module(struct module *mod) 1209 1188 { ··· 1258 1157 mutex_lock(&module_mutex); 1259 1158 mod->state = MODULE_STATE_UNFORMED; 1260 1159 mutex_unlock(&module_mutex); 1261 - 1262 - /* Remove dynamic debug info */ 1263 - ddebug_remove_module(mod->name); 1264 1160 1265 1161 /* Arch-specific cleanup. */ 1266 1162 module_arch_cleanup(mod); ··· 1287 1189 1288 1190 /* This may be empty, but that's OK */ 1289 1191 module_arch_freeing_init(mod); 1290 - module_memfree(mod->init_layout.base); 1291 1192 kfree(mod->args); 1292 1193 percpu_modfree(mod); 1293 1194 1294 - /* Free lock-classes; relies on the preceding sync_rcu(). */ 1295 - lockdep_free_key_range(mod->data_layout.base, mod->data_layout.size); 1296 - 1297 - /* Finally, free the core (containing the module structure) */ 1298 - module_memfree(mod->core_layout.base); 1299 - #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 1300 - vfree(mod->data_layout.base); 1301 - #endif 1195 + free_mod_mem(mod); 1302 1196 } 1303 1197 1304 1198 void *__symbol_get(const char *symbol) ··· 1393 1303 1394 1304 case SHN_ABS: 1395 1305 /* Don't need to do anything */ 1396 - pr_debug("Absolute symbol: 0x%08lx\n", 1397 - (long)sym[i].st_value); 1306 + pr_debug("Absolute symbol: 0x%08lx %s\n", 1307 + (long)sym[i].st_value, name); 1398 1308 break; 1399 1309 1400 1310 case SHN_LIVEPATCH: ··· 1477 1387 return 0; 1478 1388 } 1479 1389 1480 - /* Update size with this section: return offset. */ 1481 - long module_get_offset(struct module *mod, unsigned int *size, 1482 - Elf_Shdr *sechdr, unsigned int section) 1390 + long module_get_offset_and_type(struct module *mod, enum mod_mem_type type, 1391 + Elf_Shdr *sechdr, unsigned int section) 1483 1392 { 1484 - long ret; 1393 + long offset; 1394 + long mask = ((unsigned long)(type) & SH_ENTSIZE_TYPE_MASK) << SH_ENTSIZE_TYPE_SHIFT; 1485 1395 1486 - *size += arch_mod_section_prepend(mod, section); 1487 - ret = ALIGN(*size, sechdr->sh_addralign ?: 1); 1488 - *size = ret + sechdr->sh_size; 1489 - return ret; 1396 + mod->mem[type].size += arch_mod_section_prepend(mod, section); 1397 + offset = ALIGN(mod->mem[type].size, sechdr->sh_addralign ?: 1); 1398 + mod->mem[type].size = offset + sechdr->sh_size; 1399 + 1400 + WARN_ON_ONCE(offset & mask); 1401 + return offset | mask; 1490 1402 } 1491 1403 1492 1404 static bool module_init_layout_section(const char *sname) ··· 1500 1408 return module_init_section(sname); 1501 1409 } 1502 1410 1503 - /* 1504 - * Lay out the SHF_ALLOC sections in a way not dissimilar to how ld 1505 - * might -- code, read-only data, read-write data, small data. Tally 1506 - * sizes, and place the offsets into sh_entsize fields: high bit means it 1507 - * belongs in init. 1508 - */ 1509 - static void layout_sections(struct module *mod, struct load_info *info) 1411 + static void __layout_sections(struct module *mod, struct load_info *info, bool is_init) 1510 1412 { 1511 - static unsigned long const masks[][2] = { 1413 + unsigned int m, i; 1414 + 1415 + static const unsigned long masks[][2] = { 1512 1416 /* 1513 1417 * NOTE: all executable code must be the first section 1514 1418 * in this array; otherwise modify the text_size ··· 1516 1428 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL }, 1517 1429 { ARCH_SHF_SMALL | SHF_ALLOC, 0 } 1518 1430 }; 1519 - unsigned int m, i; 1431 + static const int core_m_to_mem_type[] = { 1432 + MOD_TEXT, 1433 + MOD_RODATA, 1434 + MOD_RO_AFTER_INIT, 1435 + MOD_DATA, 1436 + MOD_INVALID, /* This is needed to match the masks array */ 1437 + }; 1438 + static const int init_m_to_mem_type[] = { 1439 + MOD_INIT_TEXT, 1440 + MOD_INIT_RODATA, 1441 + MOD_INVALID, 1442 + MOD_INIT_DATA, 1443 + MOD_INVALID, /* This is needed to match the masks array */ 1444 + }; 1520 1445 1521 - for (i = 0; i < info->hdr->e_shnum; i++) 1522 - info->sechdrs[i].sh_entsize = ~0UL; 1523 - 1524 - pr_debug("Core section allocation order:\n"); 1525 1446 for (m = 0; m < ARRAY_SIZE(masks); ++m) { 1526 - for (i = 0; i < info->hdr->e_shnum; ++i) { 1527 - Elf_Shdr *s = &info->sechdrs[i]; 1528 - const char *sname = info->secstrings + s->sh_name; 1529 - unsigned int *sizep; 1447 + enum mod_mem_type type = is_init ? init_m_to_mem_type[m] : core_m_to_mem_type[m]; 1530 1448 1531 - if ((s->sh_flags & masks[m][0]) != masks[m][0] 1532 - || (s->sh_flags & masks[m][1]) 1533 - || s->sh_entsize != ~0UL 1534 - || module_init_layout_section(sname)) 1535 - continue; 1536 - sizep = m ? &mod->data_layout.size : &mod->core_layout.size; 1537 - s->sh_entsize = module_get_offset(mod, sizep, s, i); 1538 - pr_debug("\t%s\n", sname); 1539 - } 1540 - switch (m) { 1541 - case 0: /* executable */ 1542 - mod->core_layout.size = strict_align(mod->core_layout.size); 1543 - mod->core_layout.text_size = mod->core_layout.size; 1544 - break; 1545 - case 1: /* RO: text and ro-data */ 1546 - mod->data_layout.size = strict_align(mod->data_layout.size); 1547 - mod->data_layout.ro_size = mod->data_layout.size; 1548 - break; 1549 - case 2: /* RO after init */ 1550 - mod->data_layout.size = strict_align(mod->data_layout.size); 1551 - mod->data_layout.ro_after_init_size = mod->data_layout.size; 1552 - break; 1553 - case 4: /* whole core */ 1554 - mod->data_layout.size = strict_align(mod->data_layout.size); 1555 - break; 1556 - } 1557 - } 1558 - 1559 - pr_debug("Init section allocation order:\n"); 1560 - for (m = 0; m < ARRAY_SIZE(masks); ++m) { 1561 1449 for (i = 0; i < info->hdr->e_shnum; ++i) { 1562 1450 Elf_Shdr *s = &info->sechdrs[i]; 1563 1451 const char *sname = info->secstrings + s->sh_name; ··· 1541 1477 if ((s->sh_flags & masks[m][0]) != masks[m][0] 1542 1478 || (s->sh_flags & masks[m][1]) 1543 1479 || s->sh_entsize != ~0UL 1544 - || !module_init_layout_section(sname)) 1480 + || is_init != module_init_layout_section(sname)) 1545 1481 continue; 1546 - s->sh_entsize = (module_get_offset(mod, &mod->init_layout.size, s, i) 1547 - | INIT_OFFSET_MASK); 1482 + 1483 + if (WARN_ON_ONCE(type == MOD_INVALID)) 1484 + continue; 1485 + 1486 + s->sh_entsize = module_get_offset_and_type(mod, type, s, i); 1548 1487 pr_debug("\t%s\n", sname); 1549 - } 1550 - switch (m) { 1551 - case 0: /* executable */ 1552 - mod->init_layout.size = strict_align(mod->init_layout.size); 1553 - mod->init_layout.text_size = mod->init_layout.size; 1554 - break; 1555 - case 1: /* RO: text and ro-data */ 1556 - mod->init_layout.size = strict_align(mod->init_layout.size); 1557 - mod->init_layout.ro_size = mod->init_layout.size; 1558 - break; 1559 - case 2: 1560 - /* 1561 - * RO after init doesn't apply to init_layout (only 1562 - * core_layout), so it just takes the value of ro_size. 1563 - */ 1564 - mod->init_layout.ro_after_init_size = mod->init_layout.ro_size; 1565 - break; 1566 - case 4: /* whole init */ 1567 - mod->init_layout.size = strict_align(mod->init_layout.size); 1568 - break; 1569 1488 } 1570 1489 } 1571 1490 } 1572 1491 1573 - static void set_license(struct module *mod, const char *license) 1492 + /* 1493 + * Lay out the SHF_ALLOC sections in a way not dissimilar to how ld 1494 + * might -- code, read-only data, read-write data, small data. Tally 1495 + * sizes, and place the offsets into sh_entsize fields: high bit means it 1496 + * belongs in init. 1497 + */ 1498 + static void layout_sections(struct module *mod, struct load_info *info) 1499 + { 1500 + unsigned int i; 1501 + 1502 + for (i = 0; i < info->hdr->e_shnum; i++) 1503 + info->sechdrs[i].sh_entsize = ~0UL; 1504 + 1505 + pr_debug("Core section allocation order for %s:\n", mod->name); 1506 + __layout_sections(mod, info, false); 1507 + 1508 + pr_debug("Init section allocation order for %s:\n", mod->name); 1509 + __layout_sections(mod, info, true); 1510 + } 1511 + 1512 + static void module_license_taint_check(struct module *mod, const char *license) 1574 1513 { 1575 1514 if (!license) 1576 1515 license = "unspecified"; ··· 1585 1518 add_taint_module(mod, TAINT_PROPRIETARY_MODULE, 1586 1519 LOCKDEP_NOW_UNRELIABLE); 1587 1520 } 1588 - } 1589 - 1590 - /* Parse tag=value strings from .modinfo section */ 1591 - static char *next_string(char *string, unsigned long *secsize) 1592 - { 1593 - /* Skip non-zero chars */ 1594 - while (string[0]) { 1595 - string++; 1596 - if ((*secsize)-- <= 1) 1597 - return NULL; 1598 - } 1599 - 1600 - /* Skip any zero padding. */ 1601 - while (!string[0]) { 1602 - string++; 1603 - if ((*secsize)-- <= 1) 1604 - return NULL; 1605 - } 1606 - return string; 1607 - } 1608 - 1609 - static char *get_next_modinfo(const struct load_info *info, const char *tag, 1610 - char *prev) 1611 - { 1612 - char *p; 1613 - unsigned int taglen = strlen(tag); 1614 - Elf_Shdr *infosec = &info->sechdrs[info->index.info]; 1615 - unsigned long size = infosec->sh_size; 1616 - 1617 - /* 1618 - * get_modinfo() calls made before rewrite_section_headers() 1619 - * must use sh_offset, as sh_addr isn't set! 1620 - */ 1621 - char *modinfo = (char *)info->hdr + infosec->sh_offset; 1622 - 1623 - if (prev) { 1624 - size -= prev - modinfo; 1625 - modinfo = next_string(prev, &size); 1626 - } 1627 - 1628 - for (p = modinfo; p; p = next_string(p, &size)) { 1629 - if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') 1630 - return p + taglen + 1; 1631 - } 1632 - return NULL; 1633 - } 1634 - 1635 - static char *get_modinfo(const struct load_info *info, const char *tag) 1636 - { 1637 - return get_next_modinfo(info, tag, NULL); 1638 1521 } 1639 1522 1640 1523 static void setup_modinfo(struct module *mod, struct load_info *info) ··· 1607 1590 if (attr->free) 1608 1591 attr->free(mod); 1609 1592 } 1610 - } 1611 - 1612 - static void dynamic_debug_setup(struct module *mod, struct _ddebug_info *dyndbg) 1613 - { 1614 - if (!dyndbg->num_descs) 1615 - return; 1616 - ddebug_add_module(dyndbg, mod->name); 1617 - } 1618 - 1619 - static void dynamic_debug_remove(struct module *mod, struct _ddebug_info *dyndbg) 1620 - { 1621 - if (dyndbg->num_descs) 1622 - ddebug_remove_module(mod->name); 1623 1593 } 1624 1594 1625 1595 void * __weak module_alloc(unsigned long size) ··· 1646 1642 } 1647 1643 1648 1644 /* 1649 - * Sanity checks against invalid binaries, wrong arch, weird elf version. 1645 + * Check userspace passed ELF module against our expectations, and cache 1646 + * useful variables for further processing as we go. 1650 1647 * 1651 - * Also do basic validity checks against section offsets and sizes, the 1648 + * This does basic validity checks against section offsets and sizes, the 1652 1649 * section name string table, and the indices used for it (sh_name). 1650 + * 1651 + * As a last step, since we're already checking the ELF sections we cache 1652 + * useful variables which will be used later for our convenience: 1653 + * 1654 + * o pointers to section headers 1655 + * o cache the modinfo symbol section 1656 + * o cache the string symbol section 1657 + * o cache the module section 1658 + * 1659 + * As a last step we set info->mod to the temporary copy of the module in 1660 + * info->hdr. The final one will be allocated in move_module(). Any 1661 + * modifications we make to our copy of the module will be carried over 1662 + * to the final minted module. 1653 1663 */ 1654 - static int elf_validity_check(struct load_info *info) 1664 + static int elf_validity_cache_copy(struct load_info *info, int flags) 1655 1665 { 1656 1666 unsigned int i; 1657 1667 Elf_Shdr *shdr, *strhdr; 1658 1668 int err; 1669 + unsigned int num_mod_secs = 0, mod_idx; 1670 + unsigned int num_info_secs = 0, info_idx; 1671 + unsigned int num_sym_secs = 0, sym_idx; 1659 1672 1660 1673 if (info->len < sizeof(*(info->hdr))) { 1661 1674 pr_err("Invalid ELF header len %lu\n", info->len); ··· 1776 1755 info->hdr->e_shnum); 1777 1756 goto no_exec; 1778 1757 } 1758 + num_sym_secs++; 1759 + sym_idx = i; 1779 1760 fallthrough; 1780 1761 default: 1781 1762 err = validate_section_offset(info, shdr); ··· 1785 1762 pr_err("Invalid ELF section in module (section %u type %u)\n", 1786 1763 i, shdr->sh_type); 1787 1764 return err; 1765 + } 1766 + if (strcmp(info->secstrings + shdr->sh_name, 1767 + ".gnu.linkonce.this_module") == 0) { 1768 + num_mod_secs++; 1769 + mod_idx = i; 1770 + } else if (strcmp(info->secstrings + shdr->sh_name, 1771 + ".modinfo") == 0) { 1772 + num_info_secs++; 1773 + info_idx = i; 1788 1774 } 1789 1775 1790 1776 if (shdr->sh_flags & SHF_ALLOC) { ··· 1806 1774 break; 1807 1775 } 1808 1776 } 1777 + 1778 + if (num_info_secs > 1) { 1779 + pr_err("Only one .modinfo section must exist.\n"); 1780 + goto no_exec; 1781 + } else if (num_info_secs == 1) { 1782 + /* Try to find a name early so we can log errors with a module name */ 1783 + info->index.info = info_idx; 1784 + info->name = get_modinfo(info, "name"); 1785 + } 1786 + 1787 + if (num_sym_secs != 1) { 1788 + pr_warn("%s: module has no symbols (stripped?)\n", 1789 + info->name ?: "(missing .modinfo section or name field)"); 1790 + goto no_exec; 1791 + } 1792 + 1793 + /* Sets internal symbols and strings. */ 1794 + info->index.sym = sym_idx; 1795 + shdr = &info->sechdrs[sym_idx]; 1796 + info->index.str = shdr->sh_link; 1797 + info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset; 1798 + 1799 + /* 1800 + * The ".gnu.linkonce.this_module" ELF section is special. It is 1801 + * what modpost uses to refer to __this_module and let's use rely 1802 + * on THIS_MODULE to point to &__this_module properly. The kernel's 1803 + * modpost declares it on each modules's *.mod.c file. If the struct 1804 + * module of the kernel changes a full kernel rebuild is required. 1805 + * 1806 + * We have a few expectaions for this special section, the following 1807 + * code validates all this for us: 1808 + * 1809 + * o Only one section must exist 1810 + * o We expect the kernel to always have to allocate it: SHF_ALLOC 1811 + * o The section size must match the kernel's run time's struct module 1812 + * size 1813 + */ 1814 + if (num_mod_secs != 1) { 1815 + pr_err("module %s: Only one .gnu.linkonce.this_module section must exist.\n", 1816 + info->name ?: "(missing .modinfo section or name field)"); 1817 + goto no_exec; 1818 + } 1819 + 1820 + shdr = &info->sechdrs[mod_idx]; 1821 + 1822 + /* 1823 + * This is already implied on the switch above, however let's be 1824 + * pedantic about it. 1825 + */ 1826 + if (shdr->sh_type == SHT_NOBITS) { 1827 + pr_err("module %s: .gnu.linkonce.this_module section must have a size set\n", 1828 + info->name ?: "(missing .modinfo section or name field)"); 1829 + goto no_exec; 1830 + } 1831 + 1832 + if (!(shdr->sh_flags & SHF_ALLOC)) { 1833 + pr_err("module %s: .gnu.linkonce.this_module must occupy memory during process execution\n", 1834 + info->name ?: "(missing .modinfo section or name field)"); 1835 + goto no_exec; 1836 + } 1837 + 1838 + if (shdr->sh_size != sizeof(struct module)) { 1839 + pr_err("module %s: .gnu.linkonce.this_module section size must match the kernel's built struct module size at run time\n", 1840 + info->name ?: "(missing .modinfo section or name field)"); 1841 + goto no_exec; 1842 + } 1843 + 1844 + info->index.mod = mod_idx; 1845 + 1846 + /* This is temporary: point mod into copy of data. */ 1847 + info->mod = (void *)info->hdr + shdr->sh_offset; 1848 + 1849 + /* 1850 + * If we didn't load the .modinfo 'name' field earlier, fall back to 1851 + * on-disk struct mod 'name' field. 1852 + */ 1853 + if (!info->name) 1854 + info->name = info->mod->name; 1855 + 1856 + if (flags & MODULE_INIT_IGNORE_MODVERSIONS) 1857 + info->index.vers = 0; /* Pretend no __versions section! */ 1858 + else 1859 + info->index.vers = find_sec(info, "__versions"); 1860 + 1861 + info->index.pcpu = find_pcpusec(info); 1809 1862 1810 1863 return 0; 1811 1864 ··· 1921 1804 /* Nothing more to do */ 1922 1805 return 0; 1923 1806 1924 - if (set_livepatch_module(mod)) { 1925 - add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK); 1926 - pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n", 1927 - mod->name); 1807 + if (set_livepatch_module(mod)) 1928 1808 return 0; 1929 - } 1930 1809 1931 1810 pr_err("%s: module is marked as livepatch module, but livepatch support is disabled", 1932 1811 mod->name); ··· 2005 1892 } 2006 1893 2007 1894 /* 2008 - * Set up our basic convenience variables (pointers to section headers, 2009 - * search for module section index etc), and do some basic section 2010 - * verification. 2011 - * 2012 - * Set info->mod to the temporary copy of the module in info->hdr. The final one 2013 - * will be allocated in move_module(). 2014 - */ 2015 - static int setup_load_info(struct load_info *info, int flags) 1895 + * These calls taint the kernel depending certain module circumstances */ 1896 + static void module_augment_kernel_taints(struct module *mod, struct load_info *info) 2016 1897 { 2017 - unsigned int i; 1898 + int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE); 2018 1899 2019 - /* Try to find a name early so we can log errors with a module name */ 2020 - info->index.info = find_sec(info, ".modinfo"); 2021 - if (info->index.info) 2022 - info->name = get_modinfo(info, "name"); 2023 - 2024 - /* Find internal symbols and strings. */ 2025 - for (i = 1; i < info->hdr->e_shnum; i++) { 2026 - if (info->sechdrs[i].sh_type == SHT_SYMTAB) { 2027 - info->index.sym = i; 2028 - info->index.str = info->sechdrs[i].sh_link; 2029 - info->strtab = (char *)info->hdr 2030 - + info->sechdrs[info->index.str].sh_offset; 2031 - break; 2032 - } 1900 + if (!get_modinfo(info, "intree")) { 1901 + if (!test_taint(TAINT_OOT_MODULE)) 1902 + pr_warn("%s: loading out-of-tree module taints kernel.\n", 1903 + mod->name); 1904 + add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK); 2033 1905 } 2034 1906 2035 - if (info->index.sym == 0) { 2036 - pr_warn("%s: module has no symbols (stripped?)\n", 2037 - info->name ?: "(missing .modinfo section or name field)"); 2038 - return -ENOEXEC; 1907 + check_modinfo_retpoline(mod, info); 1908 + 1909 + if (get_modinfo(info, "staging")) { 1910 + add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK); 1911 + pr_warn("%s: module is from the staging directory, the quality " 1912 + "is unknown, you have been warned.\n", mod->name); 2039 1913 } 2040 1914 2041 - info->index.mod = find_sec(info, ".gnu.linkonce.this_module"); 2042 - if (!info->index.mod) { 2043 - pr_warn("%s: No module found in object\n", 2044 - info->name ?: "(missing .modinfo section or name field)"); 2045 - return -ENOEXEC; 1915 + if (is_livepatch_module(mod)) { 1916 + add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK); 1917 + pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n", 1918 + mod->name); 2046 1919 } 2047 - /* This is temporary: point mod into copy of data. */ 2048 - info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset; 1920 + 1921 + module_license_taint_check(mod, get_modinfo(info, "license")); 1922 + 1923 + if (get_modinfo(info, "test")) { 1924 + if (!test_taint(TAINT_TEST)) 1925 + pr_warn("%s: loading test module taints kernel.\n", 1926 + mod->name); 1927 + add_taint_module(mod, TAINT_TEST, LOCKDEP_STILL_OK); 1928 + } 1929 + #ifdef CONFIG_MODULE_SIG 1930 + mod->sig_ok = info->sig_ok; 1931 + if (!mod->sig_ok) { 1932 + pr_notice_once("%s: module verification failed: signature " 1933 + "and/or required key missing - tainting " 1934 + "kernel\n", mod->name); 1935 + add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK); 1936 + } 1937 + #endif 2049 1938 2050 1939 /* 2051 - * If we didn't load the .modinfo 'name' field earlier, fall back to 2052 - * on-disk struct mod 'name' field. 1940 + * ndiswrapper is under GPL by itself, but loads proprietary modules. 1941 + * Don't use add_taint_module(), as it would prevent ndiswrapper from 1942 + * using GPL-only symbols it needs. 2053 1943 */ 2054 - if (!info->name) 2055 - info->name = info->mod->name; 1944 + if (strcmp(mod->name, "ndiswrapper") == 0) 1945 + add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE); 2056 1946 2057 - if (flags & MODULE_INIT_IGNORE_MODVERSIONS) 2058 - info->index.vers = 0; /* Pretend no __versions section! */ 2059 - else 2060 - info->index.vers = find_sec(info, "__versions"); 1947 + /* driverloader was caught wrongly pretending to be under GPL */ 1948 + if (strcmp(mod->name, "driverloader") == 0) 1949 + add_taint_module(mod, TAINT_PROPRIETARY_MODULE, 1950 + LOCKDEP_NOW_UNRELIABLE); 2061 1951 2062 - info->index.pcpu = find_pcpusec(info); 1952 + /* lve claims to be GPL but upstream won't provide source */ 1953 + if (strcmp(mod->name, "lve") == 0) 1954 + add_taint_module(mod, TAINT_PROPRIETARY_MODULE, 1955 + LOCKDEP_NOW_UNRELIABLE); 2063 1956 2064 - return 0; 1957 + if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE)) 1958 + pr_warn("%s: module license taints kernel.\n", mod->name); 1959 + 2065 1960 } 2066 1961 2067 1962 static int check_modinfo(struct module *mod, struct load_info *info, int flags) ··· 2091 1970 return -ENOEXEC; 2092 1971 } 2093 1972 2094 - if (!get_modinfo(info, "intree")) { 2095 - if (!test_taint(TAINT_OOT_MODULE)) 2096 - pr_warn("%s: loading out-of-tree module taints kernel.\n", 2097 - mod->name); 2098 - add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK); 2099 - } 2100 - 2101 - check_modinfo_retpoline(mod, info); 2102 - 2103 - if (get_modinfo(info, "staging")) { 2104 - add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK); 2105 - pr_warn("%s: module is from the staging directory, the quality " 2106 - "is unknown, you have been warned.\n", mod->name); 2107 - } 2108 - 2109 1973 err = check_modinfo_livepatch(mod, info); 2110 1974 if (err) 2111 1975 return err; 2112 - 2113 - /* Set up license info based on the info section */ 2114 - set_license(mod, get_modinfo(info, "license")); 2115 - 2116 - if (get_modinfo(info, "test")) { 2117 - if (!test_taint(TAINT_TEST)) 2118 - pr_warn("%s: loading test module taints kernel.\n", 2119 - mod->name); 2120 - add_taint_module(mod, TAINT_TEST, LOCKDEP_STILL_OK); 2121 - } 2122 1976 2123 1977 return 0; 2124 1978 } ··· 2206 2110 if (section_addr(info, "__obsparm")) 2207 2111 pr_warn("%s: Ignoring obsolete parameters\n", mod->name); 2208 2112 2209 - info->dyndbg.descs = section_objs(info, "__dyndbg", 2210 - sizeof(*info->dyndbg.descs), &info->dyndbg.num_descs); 2211 - info->dyndbg.classes = section_objs(info, "__dyndbg_classes", 2212 - sizeof(*info->dyndbg.classes), &info->dyndbg.num_classes); 2113 + #ifdef CONFIG_DYNAMIC_DEBUG_CORE 2114 + mod->dyndbg_info.descs = section_objs(info, "__dyndbg", 2115 + sizeof(*mod->dyndbg_info.descs), 2116 + &mod->dyndbg_info.num_descs); 2117 + mod->dyndbg_info.classes = section_objs(info, "__dyndbg_classes", 2118 + sizeof(*mod->dyndbg_info.classes), 2119 + &mod->dyndbg_info.num_classes); 2120 + #endif 2213 2121 2214 2122 return 0; 2215 2123 } ··· 2222 2122 { 2223 2123 int i; 2224 2124 void *ptr; 2125 + enum mod_mem_type t = 0; 2126 + int ret = -ENOMEM; 2225 2127 2226 - /* Do the allocs. */ 2227 - ptr = module_alloc(mod->core_layout.size); 2228 - /* 2229 - * The pointer to this block is stored in the module structure 2230 - * which is inside the block. Just mark it as not being a 2231 - * leak. 2232 - */ 2233 - kmemleak_not_leak(ptr); 2234 - if (!ptr) 2235 - return -ENOMEM; 2236 - 2237 - memset(ptr, 0, mod->core_layout.size); 2238 - mod->core_layout.base = ptr; 2239 - 2240 - if (mod->init_layout.size) { 2241 - ptr = module_alloc(mod->init_layout.size); 2242 - /* 2243 - * The pointer to this block is stored in the module structure 2244 - * which is inside the block. This block doesn't need to be 2245 - * scanned as it contains data and code that will be freed 2246 - * after the module is initialized. 2247 - */ 2248 - kmemleak_ignore(ptr); 2249 - if (!ptr) { 2250 - module_memfree(mod->core_layout.base); 2251 - return -ENOMEM; 2128 + for_each_mod_mem_type(type) { 2129 + if (!mod->mem[type].size) { 2130 + mod->mem[type].base = NULL; 2131 + continue; 2252 2132 } 2253 - memset(ptr, 0, mod->init_layout.size); 2254 - mod->init_layout.base = ptr; 2255 - } else 2256 - mod->init_layout.base = NULL; 2257 - 2258 - #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 2259 - /* Do the allocs. */ 2260 - ptr = vzalloc(mod->data_layout.size); 2261 - /* 2262 - * The pointer to this block is stored in the module structure 2263 - * which is inside the block. Just mark it as not being a 2264 - * leak. 2265 - */ 2266 - kmemleak_not_leak(ptr); 2267 - if (!ptr) { 2268 - module_memfree(mod->core_layout.base); 2269 - module_memfree(mod->init_layout.base); 2270 - return -ENOMEM; 2133 + mod->mem[type].size = PAGE_ALIGN(mod->mem[type].size); 2134 + ptr = module_memory_alloc(mod->mem[type].size, type); 2135 + /* 2136 + * The pointer to these blocks of memory are stored on the module 2137 + * structure and we keep that around so long as the module is 2138 + * around. We only free that memory when we unload the module. 2139 + * Just mark them as not being a leak then. The .init* ELF 2140 + * sections *do* get freed after boot so we *could* treat them 2141 + * slightly differently with kmemleak_ignore() and only grey 2142 + * them out as they work as typical memory allocations which 2143 + * *do* eventually get freed, but let's just keep things simple 2144 + * and avoid *any* false positives. 2145 + */ 2146 + kmemleak_not_leak(ptr); 2147 + if (!ptr) { 2148 + t = type; 2149 + goto out_enomem; 2150 + } 2151 + memset(ptr, 0, mod->mem[type].size); 2152 + mod->mem[type].base = ptr; 2271 2153 } 2272 2154 2273 - mod->data_layout.base = ptr; 2274 - #endif 2275 2155 /* Transfer each section which specifies SHF_ALLOC */ 2276 - pr_debug("final section addresses:\n"); 2156 + pr_debug("Final section addresses for %s:\n", mod->name); 2277 2157 for (i = 0; i < info->hdr->e_shnum; i++) { 2278 2158 void *dest; 2279 2159 Elf_Shdr *shdr = &info->sechdrs[i]; 2160 + enum mod_mem_type type = shdr->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT; 2280 2161 2281 2162 if (!(shdr->sh_flags & SHF_ALLOC)) 2282 2163 continue; 2283 2164 2284 - if (shdr->sh_entsize & INIT_OFFSET_MASK) 2285 - dest = mod->init_layout.base 2286 - + (shdr->sh_entsize & ~INIT_OFFSET_MASK); 2287 - else if (!(shdr->sh_flags & SHF_EXECINSTR)) 2288 - dest = mod->data_layout.base + shdr->sh_entsize; 2289 - else 2290 - dest = mod->core_layout.base + shdr->sh_entsize; 2165 + dest = mod->mem[type].base + (shdr->sh_entsize & SH_ENTSIZE_OFFSET_MASK); 2291 2166 2292 - if (shdr->sh_type != SHT_NOBITS) 2167 + if (shdr->sh_type != SHT_NOBITS) { 2168 + /* 2169 + * Our ELF checker already validated this, but let's 2170 + * be pedantic and make the goal clearer. We actually 2171 + * end up copying over all modifications made to the 2172 + * userspace copy of the entire struct module. 2173 + */ 2174 + if (i == info->index.mod && 2175 + (WARN_ON_ONCE(shdr->sh_size != sizeof(struct module)))) { 2176 + ret = -ENOEXEC; 2177 + goto out_enomem; 2178 + } 2293 2179 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size); 2294 - /* Update sh_addr to point to copy in image. */ 2180 + } 2181 + /* 2182 + * Update the userspace copy's ELF section address to point to 2183 + * our newly allocated memory as a pure convenience so that 2184 + * users of info can keep taking advantage and using the newly 2185 + * minted official memory area. 2186 + */ 2295 2187 shdr->sh_addr = (unsigned long)dest; 2296 - pr_debug("\t0x%lx %s\n", 2297 - (long)shdr->sh_addr, info->secstrings + shdr->sh_name); 2188 + pr_debug("\t0x%lx 0x%.8lx %s\n", (long)shdr->sh_addr, 2189 + (long)shdr->sh_size, info->secstrings + shdr->sh_name); 2298 2190 } 2299 2191 2300 2192 return 0; 2193 + out_enomem: 2194 + for (t--; t >= 0; t--) 2195 + module_memory_free(mod->mem[t].base, t); 2196 + return ret; 2301 2197 } 2302 2198 2303 - static int check_module_license_and_versions(struct module *mod) 2199 + static int check_export_symbol_versions(struct module *mod) 2304 2200 { 2305 - int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE); 2306 - 2307 - /* 2308 - * ndiswrapper is under GPL by itself, but loads proprietary modules. 2309 - * Don't use add_taint_module(), as it would prevent ndiswrapper from 2310 - * using GPL-only symbols it needs. 2311 - */ 2312 - if (strcmp(mod->name, "ndiswrapper") == 0) 2313 - add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE); 2314 - 2315 - /* driverloader was caught wrongly pretending to be under GPL */ 2316 - if (strcmp(mod->name, "driverloader") == 0) 2317 - add_taint_module(mod, TAINT_PROPRIETARY_MODULE, 2318 - LOCKDEP_NOW_UNRELIABLE); 2319 - 2320 - /* lve claims to be GPL but upstream won't provide source */ 2321 - if (strcmp(mod->name, "lve") == 0) 2322 - add_taint_module(mod, TAINT_PROPRIETARY_MODULE, 2323 - LOCKDEP_NOW_UNRELIABLE); 2324 - 2325 - if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE)) 2326 - pr_warn("%s: module license taints kernel.\n", mod->name); 2327 - 2328 2201 #ifdef CONFIG_MODVERSIONS 2329 2202 if ((mod->num_syms && !mod->crcs) || 2330 2203 (mod->num_gpl_syms && !mod->gpl_crcs)) { ··· 2315 2242 * Do it before processing of module parameters, so the module 2316 2243 * can provide parameter accessor functions of its own. 2317 2244 */ 2318 - if (mod->init_layout.base) 2319 - flush_icache_range((unsigned long)mod->init_layout.base, 2320 - (unsigned long)mod->init_layout.base 2321 - + mod->init_layout.size); 2322 - flush_icache_range((unsigned long)mod->core_layout.base, 2323 - (unsigned long)mod->core_layout.base + mod->core_layout.size); 2245 + for_each_mod_mem_type(type) { 2246 + const struct module_memory *mod_mem = &mod->mem[type]; 2247 + 2248 + if (mod_mem->size) { 2249 + flush_icache_range((unsigned long)mod_mem->base, 2250 + (unsigned long)mod_mem->base + mod_mem->size); 2251 + } 2252 + } 2324 2253 } 2325 2254 2326 2255 bool __weak module_elf_check_arch(Elf_Ehdr *hdr) ··· 2364 2289 struct module *mod; 2365 2290 unsigned int ndx; 2366 2291 int err; 2367 - 2368 - err = check_modinfo(info->mod, info, flags); 2369 - if (err) 2370 - return ERR_PTR(err); 2371 2292 2372 2293 /* Allow arches to frob section contents and sizes. */ 2373 2294 err = module_frob_arch_sections(info->hdr, info->sechdrs, ··· 2421 2350 { 2422 2351 percpu_modfree(mod); 2423 2352 module_arch_freeing_init(mod); 2424 - module_memfree(mod->init_layout.base); 2425 - module_memfree(mod->core_layout.base); 2426 - #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 2427 - vfree(mod->data_layout.base); 2428 - #endif 2353 + 2354 + free_mod_mem(mod); 2429 2355 } 2430 2356 2431 2357 int __weak module_finalize(const Elf_Ehdr *hdr, ··· 2448 2380 return module_finalize(info->hdr, info->sechdrs, mod); 2449 2381 } 2450 2382 2451 - /* Is this module of this name done loading? No locks held. */ 2452 - static bool finished_loading(const char *name) 2453 - { 2454 - struct module *mod; 2455 - bool ret; 2456 - 2457 - /* 2458 - * The module_mutex should not be a heavily contended lock; 2459 - * if we get the occasional sleep here, we'll go an extra iteration 2460 - * in the wait_event_interruptible(), which is harmless. 2461 - */ 2462 - sched_annotate_sleep(); 2463 - mutex_lock(&module_mutex); 2464 - mod = find_module_all(name, strlen(name), true); 2465 - ret = !mod || mod->state == MODULE_STATE_LIVE 2466 - || mod->state == MODULE_STATE_GOING; 2467 - mutex_unlock(&module_mutex); 2468 - 2469 - return ret; 2470 - } 2471 - 2472 2383 /* Call module constructors. */ 2473 2384 static void do_mod_ctors(struct module *mod) 2474 2385 { ··· 2462 2415 /* For freeing module_init on success, in case kallsyms traversing */ 2463 2416 struct mod_initfree { 2464 2417 struct llist_node node; 2465 - void *module_init; 2418 + void *init_text; 2419 + void *init_data; 2420 + void *init_rodata; 2466 2421 }; 2467 2422 2468 2423 static void do_free_init(struct work_struct *w) ··· 2478 2429 2479 2430 llist_for_each_safe(pos, n, list) { 2480 2431 initfree = container_of(pos, struct mod_initfree, node); 2481 - module_memfree(initfree->module_init); 2432 + module_memfree(initfree->init_text); 2433 + module_memfree(initfree->init_data); 2434 + module_memfree(initfree->init_rodata); 2482 2435 kfree(initfree); 2483 2436 } 2484 2437 } ··· 2501 2450 { 2502 2451 int ret = 0; 2503 2452 struct mod_initfree *freeinit; 2453 + #if defined(CONFIG_MODULE_STATS) 2454 + unsigned int text_size = 0, total_size = 0; 2455 + 2456 + for_each_mod_mem_type(type) { 2457 + const struct module_memory *mod_mem = &mod->mem[type]; 2458 + if (mod_mem->size) { 2459 + total_size += mod_mem->size; 2460 + if (type == MOD_TEXT || type == MOD_INIT_TEXT) 2461 + text_size += mod_mem->size; 2462 + } 2463 + } 2464 + #endif 2504 2465 2505 2466 freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL); 2506 2467 if (!freeinit) { 2507 2468 ret = -ENOMEM; 2508 2469 goto fail; 2509 2470 } 2510 - freeinit->module_init = mod->init_layout.base; 2471 + freeinit->init_text = mod->mem[MOD_INIT_TEXT].base; 2472 + freeinit->init_data = mod->mem[MOD_INIT_DATA].base; 2473 + freeinit->init_rodata = mod->mem[MOD_INIT_RODATA].base; 2511 2474 2512 2475 do_mod_ctors(mod); 2513 2476 /* Start the module */ ··· 2557 2492 if (!mod->async_probe_requested) 2558 2493 async_synchronize_full(); 2559 2494 2560 - ftrace_free_mem(mod, mod->init_layout.base, mod->init_layout.base + 2561 - mod->init_layout.size); 2495 + ftrace_free_mem(mod, mod->mem[MOD_INIT_TEXT].base, 2496 + mod->mem[MOD_INIT_TEXT].base + mod->mem[MOD_INIT_TEXT].size); 2562 2497 mutex_lock(&module_mutex); 2563 2498 /* Drop initial reference. */ 2564 2499 module_put(mod); ··· 2570 2505 module_enable_ro(mod, true); 2571 2506 mod_tree_remove_init(mod); 2572 2507 module_arch_freeing_init(mod); 2573 - mod->init_layout.base = NULL; 2574 - mod->init_layout.size = 0; 2575 - mod->init_layout.ro_size = 0; 2576 - mod->init_layout.ro_after_init_size = 0; 2577 - mod->init_layout.text_size = 0; 2508 + for_class_mod_mem_type(type, init) { 2509 + mod->mem[type].base = NULL; 2510 + mod->mem[type].size = 0; 2511 + } 2512 + 2578 2513 #ifdef CONFIG_DEBUG_INFO_BTF_MODULES 2579 2514 /* .BTF is not SHF_ALLOC and will get removed, so sanitize pointer */ 2580 2515 mod->btf_data = NULL; ··· 2598 2533 mutex_unlock(&module_mutex); 2599 2534 wake_up_all(&module_wq); 2600 2535 2536 + mod_stat_add_long(text_size, &total_text_size); 2537 + mod_stat_add_long(total_size, &total_mod_size); 2538 + 2539 + mod_stat_inc(&modcount); 2540 + 2601 2541 return 0; 2602 2542 2603 2543 fail_free_freeinit: ··· 2618 2548 ftrace_release_mod(mod); 2619 2549 free_module(mod); 2620 2550 wake_up_all(&module_wq); 2551 + 2621 2552 return ret; 2622 2553 } 2623 2554 ··· 2630 2559 return 0; 2631 2560 } 2632 2561 2562 + /* Is this module of this name done loading? No locks held. */ 2563 + static bool finished_loading(const char *name) 2564 + { 2565 + struct module *mod; 2566 + bool ret; 2567 + 2568 + /* 2569 + * The module_mutex should not be a heavily contended lock; 2570 + * if we get the occasional sleep here, we'll go an extra iteration 2571 + * in the wait_event_interruptible(), which is harmless. 2572 + */ 2573 + sched_annotate_sleep(); 2574 + mutex_lock(&module_mutex); 2575 + mod = find_module_all(name, strlen(name), true); 2576 + ret = !mod || mod->state == MODULE_STATE_LIVE 2577 + || mod->state == MODULE_STATE_GOING; 2578 + mutex_unlock(&module_mutex); 2579 + 2580 + return ret; 2581 + } 2582 + 2583 + /* Must be called with module_mutex held */ 2584 + static int module_patient_check_exists(const char *name, 2585 + enum fail_dup_mod_reason reason) 2586 + { 2587 + struct module *old; 2588 + int err = 0; 2589 + 2590 + old = find_module_all(name, strlen(name), true); 2591 + if (old == NULL) 2592 + return 0; 2593 + 2594 + if (old->state == MODULE_STATE_COMING || 2595 + old->state == MODULE_STATE_UNFORMED) { 2596 + /* Wait in case it fails to load. */ 2597 + mutex_unlock(&module_mutex); 2598 + err = wait_event_interruptible(module_wq, 2599 + finished_loading(name)); 2600 + mutex_lock(&module_mutex); 2601 + if (err) 2602 + return err; 2603 + 2604 + /* The module might have gone in the meantime. */ 2605 + old = find_module_all(name, strlen(name), true); 2606 + } 2607 + 2608 + if (try_add_failed_module(name, reason)) 2609 + pr_warn("Could not add fail-tracking for module: %s\n", name); 2610 + 2611 + /* 2612 + * We are here only when the same module was being loaded. Do 2613 + * not try to load it again right now. It prevents long delays 2614 + * caused by serialized module load failures. It might happen 2615 + * when more devices of the same type trigger load of 2616 + * a particular module. 2617 + */ 2618 + if (old && old->state == MODULE_STATE_LIVE) 2619 + return -EEXIST; 2620 + return -EBUSY; 2621 + } 2622 + 2633 2623 /* 2634 2624 * We try to place it in the list now to make sure it's unique before 2635 2625 * we dedicate too many resources. In particular, temporary percpu ··· 2699 2567 static int add_unformed_module(struct module *mod) 2700 2568 { 2701 2569 int err; 2702 - struct module *old; 2703 2570 2704 2571 mod->state = MODULE_STATE_UNFORMED; 2705 2572 2706 2573 mutex_lock(&module_mutex); 2707 - old = find_module_all(mod->name, strlen(mod->name), true); 2708 - if (old != NULL) { 2709 - if (old->state == MODULE_STATE_COMING 2710 - || old->state == MODULE_STATE_UNFORMED) { 2711 - /* Wait in case it fails to load. */ 2712 - mutex_unlock(&module_mutex); 2713 - err = wait_event_interruptible(module_wq, 2714 - finished_loading(mod->name)); 2715 - if (err) 2716 - goto out_unlocked; 2717 - 2718 - /* The module might have gone in the meantime. */ 2719 - mutex_lock(&module_mutex); 2720 - old = find_module_all(mod->name, strlen(mod->name), 2721 - true); 2722 - } 2723 - 2724 - /* 2725 - * We are here only when the same module was being loaded. Do 2726 - * not try to load it again right now. It prevents long delays 2727 - * caused by serialized module load failures. It might happen 2728 - * when more devices of the same type trigger load of 2729 - * a particular module. 2730 - */ 2731 - if (old && old->state == MODULE_STATE_LIVE) 2732 - err = -EEXIST; 2733 - else 2734 - err = -EBUSY; 2574 + err = module_patient_check_exists(mod->name, FAIL_DUP_MOD_LOAD); 2575 + if (err) 2735 2576 goto out; 2736 - } 2577 + 2737 2578 mod_update_bounds(mod); 2738 2579 list_add_rcu(&mod->list, &modules); 2739 2580 mod_tree_insert(mod); ··· 2714 2609 2715 2610 out: 2716 2611 mutex_unlock(&module_mutex); 2717 - out_unlocked: 2718 2612 return err; 2719 2613 } 2720 2614 ··· 2732 2628 module_bug_finalize(info->hdr, info->sechdrs, mod); 2733 2629 module_cfi_finalize(info->hdr, info->sechdrs, mod); 2734 2630 2735 - if (module_check_misalignment(mod)) 2736 - goto out_misaligned; 2737 - 2738 2631 module_enable_ro(mod, false); 2739 2632 module_enable_nx(mod); 2740 2633 module_enable_x(mod); ··· 2745 2644 2746 2645 return 0; 2747 2646 2748 - out_misaligned: 2749 - err = -EINVAL; 2750 2647 out: 2751 2648 mutex_unlock(&module_mutex); 2752 2649 return err; ··· 2787 2688 return 0; 2788 2689 } 2789 2690 2691 + /* Module within temporary copy, this doesn't do any allocation */ 2692 + static int early_mod_check(struct load_info *info, int flags) 2693 + { 2694 + int err; 2695 + 2696 + /* 2697 + * Now that we know we have the correct module name, check 2698 + * if it's blacklisted. 2699 + */ 2700 + if (blacklisted(info->name)) { 2701 + pr_err("Module %s is blacklisted\n", info->name); 2702 + return -EPERM; 2703 + } 2704 + 2705 + err = rewrite_section_headers(info, flags); 2706 + if (err) 2707 + return err; 2708 + 2709 + /* Check module struct version now, before we try to use module. */ 2710 + if (!check_modstruct_version(info, info->mod)) 2711 + return -ENOEXEC; 2712 + 2713 + err = check_modinfo(info->mod, info, flags); 2714 + if (err) 2715 + return err; 2716 + 2717 + mutex_lock(&module_mutex); 2718 + err = module_patient_check_exists(info->mod->name, FAIL_DUP_MOD_BECOMING); 2719 + mutex_unlock(&module_mutex); 2720 + 2721 + return err; 2722 + } 2723 + 2790 2724 /* 2791 2725 * Allocate and load the module: note that size of section 0 is always 2792 2726 * zero, and we rely on this for optional sections. ··· 2828 2696 int flags) 2829 2697 { 2830 2698 struct module *mod; 2699 + bool module_allocated = false; 2831 2700 long err = 0; 2832 2701 char *after_dashes; 2833 2702 ··· 2850 2717 2851 2718 /* 2852 2719 * Do basic sanity checks against the ELF header and 2853 - * sections. 2720 + * sections. Cache useful sections and set the 2721 + * info->mod to the userspace passed struct module. 2854 2722 */ 2855 - err = elf_validity_check(info); 2723 + err = elf_validity_cache_copy(info, flags); 2856 2724 if (err) 2857 2725 goto free_copy; 2858 2726 2859 - /* 2860 - * Everything checks out, so set up the section info 2861 - * in the info structure. 2862 - */ 2863 - err = setup_load_info(info, flags); 2727 + err = early_mod_check(info, flags); 2864 2728 if (err) 2865 2729 goto free_copy; 2866 - 2867 - /* 2868 - * Now that we know we have the correct module name, check 2869 - * if it's blacklisted. 2870 - */ 2871 - if (blacklisted(info->name)) { 2872 - err = -EPERM; 2873 - pr_err("Module %s is blacklisted\n", info->name); 2874 - goto free_copy; 2875 - } 2876 - 2877 - err = rewrite_section_headers(info, flags); 2878 - if (err) 2879 - goto free_copy; 2880 - 2881 - /* Check module struct version now, before we try to use module. */ 2882 - if (!check_modstruct_version(info, info->mod)) { 2883 - err = -ENOEXEC; 2884 - goto free_copy; 2885 - } 2886 2730 2887 2731 /* Figure out module layout, and allocate all the memory. */ 2888 2732 mod = layout_and_allocate(info, flags); ··· 2868 2758 goto free_copy; 2869 2759 } 2870 2760 2761 + module_allocated = true; 2762 + 2871 2763 audit_log_kern_module(mod->name); 2872 2764 2873 2765 /* Reserve our place in the list. */ ··· 2877 2765 if (err) 2878 2766 goto free_module; 2879 2767 2880 - #ifdef CONFIG_MODULE_SIG 2881 - mod->sig_ok = info->sig_ok; 2882 - if (!mod->sig_ok) { 2883 - pr_notice_once("%s: module verification failed: signature " 2884 - "and/or required key missing - tainting " 2885 - "kernel\n", mod->name); 2886 - add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK); 2887 - } 2888 - #endif 2768 + /* 2769 + * We are tainting your kernel if your module gets into 2770 + * the modules linked list somehow. 2771 + */ 2772 + module_augment_kernel_taints(mod, info); 2889 2773 2890 2774 /* To avoid stressing percpu allocator, do this once we're unique. */ 2891 2775 err = percpu_modalloc(mod, info); ··· 2903 2795 if (err) 2904 2796 goto free_unload; 2905 2797 2906 - err = check_module_license_and_versions(mod); 2798 + err = check_export_symbol_versions(mod); 2907 2799 if (err) 2908 2800 goto free_unload; 2909 2801 ··· 2933 2825 } 2934 2826 2935 2827 init_build_id(mod, info); 2936 - dynamic_debug_setup(mod, &info->dyndbg); 2937 2828 2938 2829 /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */ 2939 2830 ftrace_module_init(mod); ··· 2996 2889 2997 2890 ddebug_cleanup: 2998 2891 ftrace_release_mod(mod); 2999 - dynamic_debug_remove(mod, &info->dyndbg); 3000 2892 synchronize_rcu(); 3001 2893 kfree(mod->args); 3002 2894 free_arch_cleanup: ··· 3014 2908 synchronize_rcu(); 3015 2909 mutex_unlock(&module_mutex); 3016 2910 free_module: 2911 + mod_stat_bump_invalid(info, flags); 3017 2912 /* Free lock-classes; relies on the preceding sync_rcu() */ 3018 - lockdep_free_key_range(mod->data_layout.base, mod->data_layout.size); 2913 + for_class_mod_mem_type(type, core_data) { 2914 + lockdep_free_key_range(mod->mem[type].base, 2915 + mod->mem[type].size); 2916 + } 3019 2917 3020 2918 module_deallocate(mod, info); 3021 2919 free_copy: 2920 + /* 2921 + * The info->len is always set. We distinguish between 2922 + * failures once the proper module was allocated and 2923 + * before that. 2924 + */ 2925 + if (!module_allocated) 2926 + mod_stat_bump_becoming(info, flags); 3022 2927 free_copy(info, flags); 3023 2928 return err; 3024 2929 } ··· 3048 2931 umod, len, uargs); 3049 2932 3050 2933 err = copy_module_from_user(umod, len, &info); 3051 - if (err) 2934 + if (err) { 2935 + mod_stat_inc(&failed_kreads); 2936 + mod_stat_add_long(len, &invalid_kread_bytes); 3052 2937 return err; 2938 + } 3053 2939 3054 2940 return load_module(&info, uargs, 0); 3055 2941 } ··· 3077 2957 3078 2958 len = kernel_read_file_from_fd(fd, 0, &buf, INT_MAX, NULL, 3079 2959 READING_MODULE); 3080 - if (len < 0) 2960 + if (len < 0) { 2961 + mod_stat_inc(&failed_kreads); 2962 + mod_stat_add_long(len, &invalid_kread_bytes); 3081 2963 return len; 2964 + } 3082 2965 3083 2966 if (flags & MODULE_INIT_COMPRESSED_FILE) { 3084 2967 err = module_decompress(&info, buf, len); 3085 2968 vfree(buf); /* compressed data is no longer needed */ 3086 - if (err) 2969 + if (err) { 2970 + mod_stat_inc(&failed_decompress); 2971 + mod_stat_add_long(len, &invalid_decompress_bytes); 3087 2972 return err; 2973 + } 3088 2974 } else { 3089 2975 info.hdr = buf; 3090 2976 info.len = len; 3091 2977 } 3092 2978 3093 2979 return load_module(&info, uargs, flags); 3094 - } 3095 - 3096 - static inline int within(unsigned long addr, void *start, unsigned long size) 3097 - { 3098 - return ((void *)addr >= start && (void *)addr < start + size); 3099 2980 } 3100 2981 3101 2982 /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */ ··· 3181 3060 struct module *__module_address(unsigned long addr) 3182 3061 { 3183 3062 struct module *mod; 3184 - struct mod_tree_root *tree; 3185 3063 3186 3064 if (addr >= mod_tree.addr_min && addr <= mod_tree.addr_max) 3187 - tree = &mod_tree; 3188 - #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 3189 - else if (addr >= mod_data_tree.addr_min && addr <= mod_data_tree.addr_max) 3190 - tree = &mod_data_tree; 3191 - #endif 3192 - else 3193 - return NULL; 3065 + goto lookup; 3194 3066 3067 + #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 3068 + if (addr >= mod_tree.data_addr_min && addr <= mod_tree.data_addr_max) 3069 + goto lookup; 3070 + #endif 3071 + 3072 + return NULL; 3073 + 3074 + lookup: 3195 3075 module_assert_mutex_or_preempt(); 3196 3076 3197 - mod = mod_find(addr, tree); 3077 + mod = mod_find(addr, &mod_tree); 3198 3078 if (mod) { 3199 3079 BUG_ON(!within_module(addr, mod)); 3200 3080 if (mod->state == MODULE_STATE_UNFORMED) ··· 3235 3113 struct module *mod = __module_address(addr); 3236 3114 if (mod) { 3237 3115 /* Make sure it's within the text section. */ 3238 - if (!within(addr, mod->init_layout.base, mod->init_layout.text_size) 3239 - && !within(addr, mod->core_layout.base, mod->core_layout.text_size)) 3116 + if (!within_module_mem_type(addr, mod, MOD_TEXT) && 3117 + !within_module_mem_type(addr, mod, MOD_INIT_TEXT)) 3240 3118 mod = NULL; 3241 3119 } 3242 3120 return mod; ··· 3264 3142 last_unloaded_module.taints); 3265 3143 pr_cont("\n"); 3266 3144 } 3145 + 3146 + #ifdef CONFIG_MODULE_DEBUGFS 3147 + struct dentry *mod_debugfs_root; 3148 + 3149 + static int module_debugfs_init(void) 3150 + { 3151 + mod_debugfs_root = debugfs_create_dir("modules", NULL); 3152 + return 0; 3153 + } 3154 + module_init(module_debugfs_init); 3155 + #endif
+11 -5
kernel/module/procfs.c
··· 62 62 mutex_unlock(&module_mutex); 63 63 } 64 64 65 + static unsigned int module_total_size(struct module *mod) 66 + { 67 + int size = 0; 68 + 69 + for_each_mod_mem_type(type) 70 + size += mod->mem[type].size; 71 + return size; 72 + } 73 + 65 74 static int m_show(struct seq_file *m, void *p) 66 75 { 67 76 struct module *mod = list_entry(p, struct module, list); ··· 82 73 if (mod->state == MODULE_STATE_UNFORMED) 83 74 return 0; 84 75 85 - size = mod->init_layout.size + mod->core_layout.size; 86 - #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 87 - size += mod->data_layout.size; 88 - #endif 76 + size = module_total_size(mod); 89 77 seq_printf(m, "%s %u", mod->name, size); 90 78 print_unload_info(m, mod); 91 79 ··· 92 86 mod->state == MODULE_STATE_COMING ? "Loading" : 93 87 "Live"); 94 88 /* Used by oprofile and other similar tools. */ 95 - value = m->private ? NULL : mod->core_layout.base; 89 + value = m->private ? NULL : mod->mem[MOD_TEXT].base; 96 90 seq_printf(m, " 0x%px", value); 97 91 98 92 /* Taints info */
+430
kernel/module/stats.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Debugging module statistics. 4 + * 5 + * Copyright (C) 2023 Luis Chamberlain <mcgrof@kernel.org> 6 + */ 7 + 8 + #include <linux/module.h> 9 + #include <uapi/linux/module.h> 10 + #include <linux/string.h> 11 + #include <linux/printk.h> 12 + #include <linux/slab.h> 13 + #include <linux/list.h> 14 + #include <linux/debugfs.h> 15 + #include <linux/rculist.h> 16 + #include <linux/math.h> 17 + 18 + #include "internal.h" 19 + 20 + /** 21 + * DOC: module debugging statistics overview 22 + * 23 + * Enabling CONFIG_MODULE_STATS enables module debugging statistics which 24 + * are useful to monitor and root cause memory pressure issues with module 25 + * loading. These statistics are useful to allow us to improve production 26 + * workloads. 27 + * 28 + * The current module debugging statistics supported help keep track of module 29 + * loading failures to enable improvements either for kernel module auto-loading 30 + * usage (request_module()) or interactions with userspace. Statistics are 31 + * provided to track all possible failures in the finit_module() path and memory 32 + * wasted in this process space. Each of the failure counters are associated 33 + * to a type of module loading failure which is known to incur a certain amount 34 + * of memory allocation loss. In the worst case loading a module will fail after 35 + * a 3 step memory allocation process: 36 + * 37 + * a) memory allocated with kernel_read_file_from_fd() 38 + * b) module decompression processes the file read from 39 + * kernel_read_file_from_fd(), and vmap() is used to map 40 + * the decompressed module to a new local buffer which represents 41 + * a copy of the decompressed module passed from userspace. The buffer 42 + * from kernel_read_file_from_fd() is freed right away. 43 + * c) layout_and_allocate() allocates space for the final resting 44 + * place where we would keep the module if it were to be processed 45 + * successfully. 46 + * 47 + * If a failure occurs after these three different allocations only one 48 + * counter will be incremented with the summation of the allocated bytes freed 49 + * incurred during this failure. Likewise, if module loading failed only after 50 + * step b) a separate counter is used and incremented for the bytes freed and 51 + * not used during both of those allocations. 52 + * 53 + * Virtual memory space can be limited, for example on x86 virtual memory size 54 + * defaults to 128 MiB. We should strive to limit and avoid wasting virtual 55 + * memory allocations when possible. These module debugging statistics help 56 + * to evaluate how much memory is being wasted on bootup due to module loading 57 + * failures. 58 + * 59 + * All counters are designed to be incremental. Atomic counters are used so to 60 + * remain simple and avoid delays and deadlocks. 61 + */ 62 + 63 + /** 64 + * DOC: dup_failed_modules - tracks duplicate failed modules 65 + * 66 + * Linked list of modules which failed to be loaded because an already existing 67 + * module with the same name was already being processed or already loaded. 68 + * The finit_module() system call incurs heavy virtual memory allocations. In 69 + * the worst case an finit_module() system call can end up allocating virtual 70 + * memory 3 times: 71 + * 72 + * 1) kernel_read_file_from_fd() call uses vmalloc() 73 + * 2) optional module decompression uses vmap() 74 + * 3) layout_and allocate() can use vzalloc() or an arch specific variation of 75 + * vmalloc to deal with ELF sections requiring special permissions 76 + * 77 + * In practice on a typical boot today most finit_module() calls fail due to 78 + * the module with the same name already being loaded or about to be processed. 79 + * All virtual memory allocated to these failed modules will be freed with 80 + * no functional use. 81 + * 82 + * To help with this the dup_failed_modules allows us to track modules which 83 + * failed to load due to the fact that a module was already loaded or being 84 + * processed. There are only two points at which we can fail such calls, 85 + * we list them below along with the number of virtual memory allocation 86 + * calls: 87 + * 88 + * a) FAIL_DUP_MOD_BECOMING: at the end of early_mod_check() before 89 + * layout_and_allocate(). 90 + * - with module decompression: 2 virtual memory allocation calls 91 + * - without module decompression: 1 virtual memory allocation calls 92 + * b) FAIL_DUP_MOD_LOAD: after layout_and_allocate() on add_unformed_module() 93 + * - with module decompression 3 virtual memory allocation calls 94 + * - without module decompression 2 virtual memory allocation calls 95 + * 96 + * We should strive to get this list to be as small as possible. If this list 97 + * is not empty it is a reflection of possible work or optimizations possible 98 + * either in-kernel or in userspace. 99 + */ 100 + static LIST_HEAD(dup_failed_modules); 101 + 102 + /** 103 + * DOC: module statistics debugfs counters 104 + * 105 + * The total amount of wasted virtual memory allocation space during module 106 + * loading can be computed by adding the total from the summation: 107 + * 108 + * * @invalid_kread_bytes + 109 + * @invalid_decompress_bytes + 110 + * @invalid_becoming_bytes + 111 + * @invalid_mod_bytes 112 + * 113 + * The following debugfs counters are available to inspect module loading 114 + * failures: 115 + * 116 + * * total_mod_size: total bytes ever used by all modules we've dealt with on 117 + * this system 118 + * * total_text_size: total bytes of the .text and .init.text ELF section 119 + * sizes we've dealt with on this system 120 + * * invalid_kread_bytes: bytes allocated and then freed on failures which 121 + * happen due to the initial kernel_read_file_from_fd(). kernel_read_file_from_fd() 122 + * uses vmalloc(). These should typically not happen unless your system is 123 + * under memory pressure. 124 + * * invalid_decompress_bytes: number of bytes allocated and freed due to 125 + * memory allocations in the module decompression path that use vmap(). 126 + * These typically should not happen unless your system is under memory 127 + * pressure. 128 + * * invalid_becoming_bytes: total number of bytes allocated and freed used 129 + * used to read the kernel module userspace wants us to read before we 130 + * promote it to be processed to be added to our @modules linked list. These 131 + * failures can happen if we had a check in between a successful kernel_read_file_from_fd() 132 + * call and right before we allocate the our private memory for the module 133 + * which would be kept if the module is successfully loaded. The most common 134 + * reason for this failure is when userspace is racing to load a module 135 + * which it does not yet see loaded. The first module to succeed in 136 + * add_unformed_module() will add a module to our &modules list and 137 + * subsequent loads of modules with the same name will error out at the 138 + * end of early_mod_check(). The check for module_patient_check_exists() 139 + * at the end of early_mod_check() prevents duplicate allocations 140 + * on layout_and_allocate() for modules already being processed. These 141 + * duplicate failed modules are non-fatal, however they typically are 142 + * indicative of userspace not seeing a module in userspace loaded yet and 143 + * unnecessarily trying to load a module before the kernel even has a chance 144 + * to begin to process prior requests. Although duplicate failures can be 145 + * non-fatal, we should try to reduce vmalloc() pressure proactively, so 146 + * ideally after boot this will be close to as 0 as possible. If module 147 + * decompression was used we also add to this counter the cost of the 148 + * initial kernel_read_file_from_fd() of the compressed module. If module 149 + * decompression was not used the value represents the total allocated and 150 + * freed bytes in kernel_read_file_from_fd() calls for these type of 151 + * failures. These failures can occur because: 152 + * 153 + * * module_sig_check() - module signature checks 154 + * * elf_validity_cache_copy() - some ELF validation issue 155 + * * early_mod_check(): 156 + * 157 + * * blacklisting 158 + * * failed to rewrite section headers 159 + * * version magic 160 + * * live patch requirements didn't check out 161 + * * the module was detected as being already present 162 + * 163 + * * invalid_mod_bytes: these are the total number of bytes allocated and 164 + * freed due to failures after we did all the sanity checks of the module 165 + * which userspace passed to us and after our first check that the module 166 + * is unique. A module can still fail to load if we detect the module is 167 + * loaded after we allocate space for it with layout_and_allocate(), we do 168 + * this check right before processing the module as live and run its 169 + * initialization routines. Note that you have a failure of this type it 170 + * also means the respective kernel_read_file_from_fd() memory space was 171 + * also freed and not used, and so we increment this counter with twice 172 + * the size of the module. Additionally if you used module decompression 173 + * the size of the compressed module is also added to this counter. 174 + * 175 + * * modcount: how many modules we've loaded in our kernel life time 176 + * * failed_kreads: how many modules failed due to failed kernel_read_file_from_fd() 177 + * * failed_decompress: how many failed module decompression attempts we've had. 178 + * These really should not happen unless your compression / decompression 179 + * might be broken. 180 + * * failed_becoming: how many modules failed after we kernel_read_file_from_fd() 181 + * it and before we allocate memory for it with layout_and_allocate(). This 182 + * counter is never incremented if you manage to validate the module and 183 + * call layout_and_allocate() for it. 184 + * * failed_load_modules: how many modules failed once we've allocated our 185 + * private space for our module using layout_and_allocate(). These failures 186 + * should hopefully mostly be dealt with already. Races in theory could 187 + * still exist here, but it would just mean the kernel had started processing 188 + * two threads concurrently up to early_mod_check() and one thread won. 189 + * These failures are good signs the kernel or userspace is doing something 190 + * seriously stupid or that could be improved. We should strive to fix these, 191 + * but it is perhaps not easy to fix them. A recent example are the modules 192 + * requests incurred for frequency modules, a separate module request was 193 + * being issued for each CPU on a system. 194 + */ 195 + 196 + atomic_long_t total_mod_size; 197 + atomic_long_t total_text_size; 198 + atomic_long_t invalid_kread_bytes; 199 + atomic_long_t invalid_decompress_bytes; 200 + static atomic_long_t invalid_becoming_bytes; 201 + static atomic_long_t invalid_mod_bytes; 202 + atomic_t modcount; 203 + atomic_t failed_kreads; 204 + atomic_t failed_decompress; 205 + static atomic_t failed_becoming; 206 + static atomic_t failed_load_modules; 207 + 208 + static const char *mod_fail_to_str(struct mod_fail_load *mod_fail) 209 + { 210 + if (test_bit(FAIL_DUP_MOD_BECOMING, &mod_fail->dup_fail_mask) && 211 + test_bit(FAIL_DUP_MOD_LOAD, &mod_fail->dup_fail_mask)) 212 + return "Becoming & Load"; 213 + if (test_bit(FAIL_DUP_MOD_BECOMING, &mod_fail->dup_fail_mask)) 214 + return "Becoming"; 215 + if (test_bit(FAIL_DUP_MOD_LOAD, &mod_fail->dup_fail_mask)) 216 + return "Load"; 217 + return "Bug-on-stats"; 218 + } 219 + 220 + void mod_stat_bump_invalid(struct load_info *info, int flags) 221 + { 222 + atomic_long_add(info->len * 2, &invalid_mod_bytes); 223 + atomic_inc(&failed_load_modules); 224 + #if defined(CONFIG_MODULE_DECOMPRESS) 225 + if (flags & MODULE_INIT_COMPRESSED_FILE) 226 + atomic_long_add(info->compressed_len, &invalid_mod_bytes); 227 + #endif 228 + } 229 + 230 + void mod_stat_bump_becoming(struct load_info *info, int flags) 231 + { 232 + atomic_inc(&failed_becoming); 233 + atomic_long_add(info->len, &invalid_becoming_bytes); 234 + #if defined(CONFIG_MODULE_DECOMPRESS) 235 + if (flags & MODULE_INIT_COMPRESSED_FILE) 236 + atomic_long_add(info->compressed_len, &invalid_becoming_bytes); 237 + #endif 238 + } 239 + 240 + int try_add_failed_module(const char *name, enum fail_dup_mod_reason reason) 241 + { 242 + struct mod_fail_load *mod_fail; 243 + 244 + list_for_each_entry_rcu(mod_fail, &dup_failed_modules, list, 245 + lockdep_is_held(&module_mutex)) { 246 + if (!strcmp(mod_fail->name, name)) { 247 + atomic_long_inc(&mod_fail->count); 248 + __set_bit(reason, &mod_fail->dup_fail_mask); 249 + goto out; 250 + } 251 + } 252 + 253 + mod_fail = kzalloc(sizeof(*mod_fail), GFP_KERNEL); 254 + if (!mod_fail) 255 + return -ENOMEM; 256 + memcpy(mod_fail->name, name, strlen(name)); 257 + __set_bit(reason, &mod_fail->dup_fail_mask); 258 + atomic_long_inc(&mod_fail->count); 259 + list_add_rcu(&mod_fail->list, &dup_failed_modules); 260 + out: 261 + return 0; 262 + } 263 + 264 + /* 265 + * At 64 bytes per module and assuming a 1024 bytes preamble we can fit the 266 + * 112 module prints within 8k. 267 + * 268 + * 1024 + (64*112) = 8k 269 + */ 270 + #define MAX_PREAMBLE 1024 271 + #define MAX_FAILED_MOD_PRINT 112 272 + #define MAX_BYTES_PER_MOD 64 273 + static ssize_t read_file_mod_stats(struct file *file, char __user *user_buf, 274 + size_t count, loff_t *ppos) 275 + { 276 + struct mod_fail_load *mod_fail; 277 + unsigned int len, size, count_failed = 0; 278 + char *buf; 279 + u32 live_mod_count, fkreads, fdecompress, fbecoming, floads; 280 + unsigned long total_size, text_size, ikread_bytes, ibecoming_bytes, 281 + idecompress_bytes, imod_bytes, total_virtual_lost; 282 + 283 + live_mod_count = atomic_read(&modcount); 284 + fkreads = atomic_read(&failed_kreads); 285 + fdecompress = atomic_read(&failed_decompress); 286 + fbecoming = atomic_read(&failed_becoming); 287 + floads = atomic_read(&failed_load_modules); 288 + 289 + total_size = atomic_long_read(&total_mod_size); 290 + text_size = atomic_long_read(&total_text_size); 291 + ikread_bytes = atomic_long_read(&invalid_kread_bytes); 292 + idecompress_bytes = atomic_long_read(&invalid_decompress_bytes); 293 + ibecoming_bytes = atomic_long_read(&invalid_becoming_bytes); 294 + imod_bytes = atomic_long_read(&invalid_mod_bytes); 295 + 296 + total_virtual_lost = ikread_bytes + idecompress_bytes + ibecoming_bytes + imod_bytes; 297 + 298 + size = MAX_PREAMBLE + min((unsigned int)(floads + fbecoming), 299 + (unsigned int)MAX_FAILED_MOD_PRINT) * MAX_BYTES_PER_MOD; 300 + buf = kzalloc(size, GFP_KERNEL); 301 + if (buf == NULL) 302 + return -ENOMEM; 303 + 304 + /* The beginning of our debug preamble */ 305 + len = scnprintf(buf, size, "%25s\t%u\n", "Mods ever loaded", live_mod_count); 306 + 307 + len += scnprintf(buf + len, size - len, "%25s\t%u\n", "Mods failed on kread", fkreads); 308 + 309 + len += scnprintf(buf + len, size - len, "%25s\t%u\n", "Mods failed on decompress", 310 + fdecompress); 311 + len += scnprintf(buf + len, size - len, "%25s\t%u\n", "Mods failed on becoming", fbecoming); 312 + 313 + len += scnprintf(buf + len, size - len, "%25s\t%u\n", "Mods failed on load", floads); 314 + 315 + len += scnprintf(buf + len, size - len, "%25s\t%lu\n", "Total module size", total_size); 316 + len += scnprintf(buf + len, size - len, "%25s\t%lu\n", "Total mod text size", text_size); 317 + 318 + len += scnprintf(buf + len, size - len, "%25s\t%lu\n", "Failed kread bytes", ikread_bytes); 319 + 320 + len += scnprintf(buf + len, size - len, "%25s\t%lu\n", "Failed decompress bytes", 321 + idecompress_bytes); 322 + 323 + len += scnprintf(buf + len, size - len, "%25s\t%lu\n", "Failed becoming bytes", ibecoming_bytes); 324 + 325 + len += scnprintf(buf + len, size - len, "%25s\t%lu\n", "Failed kmod bytes", imod_bytes); 326 + 327 + len += scnprintf(buf + len, size - len, "%25s\t%lu\n", "Virtual mem wasted bytes", total_virtual_lost); 328 + 329 + if (live_mod_count && total_size) { 330 + len += scnprintf(buf + len, size - len, "%25s\t%lu\n", "Average mod size", 331 + DIV_ROUND_UP(total_size, live_mod_count)); 332 + } 333 + 334 + if (live_mod_count && text_size) { 335 + len += scnprintf(buf + len, size - len, "%25s\t%lu\n", "Average mod text size", 336 + DIV_ROUND_UP(text_size, live_mod_count)); 337 + } 338 + 339 + /* 340 + * We use WARN_ON_ONCE() for the counters to ensure we always have parity 341 + * for keeping tabs on a type of failure with one type of byte counter. 342 + * The counters for imod_bytes does not increase for fkreads failures 343 + * for example, and so on. 344 + */ 345 + 346 + WARN_ON_ONCE(ikread_bytes && !fkreads); 347 + if (fkreads && ikread_bytes) { 348 + len += scnprintf(buf + len, size - len, "%25s\t%lu\n", "Avg fail kread bytes", 349 + DIV_ROUND_UP(ikread_bytes, fkreads)); 350 + } 351 + 352 + WARN_ON_ONCE(ibecoming_bytes && !fbecoming); 353 + if (fbecoming && ibecoming_bytes) { 354 + len += scnprintf(buf + len, size - len, "%25s\t%lu\n", "Avg fail becoming bytes", 355 + DIV_ROUND_UP(ibecoming_bytes, fbecoming)); 356 + } 357 + 358 + WARN_ON_ONCE(idecompress_bytes && !fdecompress); 359 + if (fdecompress && idecompress_bytes) { 360 + len += scnprintf(buf + len, size - len, "%25s\t%lu\n", "Avg fail decomp bytes", 361 + DIV_ROUND_UP(idecompress_bytes, fdecompress)); 362 + } 363 + 364 + WARN_ON_ONCE(imod_bytes && !floads); 365 + if (floads && imod_bytes) { 366 + len += scnprintf(buf + len, size - len, "%25s\t%lu\n", "Average fail load bytes", 367 + DIV_ROUND_UP(imod_bytes, floads)); 368 + } 369 + 370 + /* End of our debug preamble header. */ 371 + 372 + /* Catch when we've gone beyond our expected preamble */ 373 + WARN_ON_ONCE(len >= MAX_PREAMBLE); 374 + 375 + if (list_empty(&dup_failed_modules)) 376 + goto out; 377 + 378 + len += scnprintf(buf + len, size - len, "Duplicate failed modules:\n"); 379 + len += scnprintf(buf + len, size - len, "%25s\t%15s\t%25s\n", 380 + "Module-name", "How-many-times", "Reason"); 381 + mutex_lock(&module_mutex); 382 + 383 + 384 + list_for_each_entry_rcu(mod_fail, &dup_failed_modules, list) { 385 + if (WARN_ON_ONCE(++count_failed >= MAX_FAILED_MOD_PRINT)) 386 + goto out_unlock; 387 + len += scnprintf(buf + len, size - len, "%25s\t%15lu\t%25s\n", mod_fail->name, 388 + atomic_long_read(&mod_fail->count), mod_fail_to_str(mod_fail)); 389 + } 390 + out_unlock: 391 + mutex_unlock(&module_mutex); 392 + out: 393 + kfree(buf); 394 + return simple_read_from_buffer(user_buf, count, ppos, buf, len); 395 + } 396 + #undef MAX_PREAMBLE 397 + #undef MAX_FAILED_MOD_PRINT 398 + #undef MAX_BYTES_PER_MOD 399 + 400 + static const struct file_operations fops_mod_stats = { 401 + .read = read_file_mod_stats, 402 + .open = simple_open, 403 + .owner = THIS_MODULE, 404 + .llseek = default_llseek, 405 + }; 406 + 407 + #define mod_debug_add_ulong(name) debugfs_create_ulong(#name, 0400, mod_debugfs_root, (unsigned long *) &name.counter) 408 + #define mod_debug_add_atomic(name) debugfs_create_atomic_t(#name, 0400, mod_debugfs_root, &name) 409 + static int __init module_stats_init(void) 410 + { 411 + mod_debug_add_ulong(total_mod_size); 412 + mod_debug_add_ulong(total_text_size); 413 + mod_debug_add_ulong(invalid_kread_bytes); 414 + mod_debug_add_ulong(invalid_decompress_bytes); 415 + mod_debug_add_ulong(invalid_becoming_bytes); 416 + mod_debug_add_ulong(invalid_mod_bytes); 417 + 418 + mod_debug_add_atomic(modcount); 419 + mod_debug_add_atomic(failed_kreads); 420 + mod_debug_add_atomic(failed_decompress); 421 + mod_debug_add_atomic(failed_becoming); 422 + mod_debug_add_atomic(failed_load_modules); 423 + 424 + debugfs_create_file("stats", 0400, mod_debugfs_root, mod_debugfs_root, &fops_mod_stats); 425 + 426 + return 0; 427 + } 428 + #undef mod_debug_add_ulong 429 + #undef mod_debug_add_atomic 430 + module_init(module_stats_init);
+18 -81
kernel/module/strict_rwx.c
··· 11 11 #include <linux/set_memory.h> 12 12 #include "internal.h" 13 13 14 - /* 15 - * LKM RO/NX protection: protect module's text/ro-data 16 - * from modification and any data from execution. 17 - * 18 - * General layout of module is: 19 - * [text] [read-only-data] [ro-after-init] [writable data] 20 - * text_size -----^ ^ ^ ^ 21 - * ro_size ------------------------| | | 22 - * ro_after_init_size -----------------------------| | 23 - * size -----------------------------------------------------------| 24 - * 25 - * These values are always page-aligned (as is base) when 26 - * CONFIG_STRICT_MODULE_RWX is set. 27 - */ 14 + static void module_set_memory(const struct module *mod, enum mod_mem_type type, 15 + int (*set_memory)(unsigned long start, int num_pages)) 16 + { 17 + const struct module_memory *mod_mem = &mod->mem[type]; 18 + 19 + set_vm_flush_reset_perms(mod_mem->base); 20 + set_memory((unsigned long)mod_mem->base, mod_mem->size >> PAGE_SHIFT); 21 + } 28 22 29 23 /* 30 24 * Since some arches are moving towards PAGE_KERNEL module allocations instead 31 - * of PAGE_KERNEL_EXEC, keep frob_text() and module_enable_x() independent of 25 + * of PAGE_KERNEL_EXEC, keep module_enable_x() independent of 32 26 * CONFIG_STRICT_MODULE_RWX because they are needed regardless of whether we 33 27 * are strict. 34 28 */ 35 - static void frob_text(const struct module_layout *layout, 36 - int (*set_memory)(unsigned long start, int num_pages)) 37 - { 38 - set_memory((unsigned long)layout->base, 39 - PAGE_ALIGN(layout->text_size) >> PAGE_SHIFT); 40 - } 41 - 42 - static void frob_rodata(const struct module_layout *layout, 43 - int (*set_memory)(unsigned long start, int num_pages)) 44 - { 45 - set_memory((unsigned long)layout->base + layout->text_size, 46 - (layout->ro_size - layout->text_size) >> PAGE_SHIFT); 47 - } 48 - 49 - static void frob_ro_after_init(const struct module_layout *layout, 50 - int (*set_memory)(unsigned long start, int num_pages)) 51 - { 52 - set_memory((unsigned long)layout->base + layout->ro_size, 53 - (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT); 54 - } 55 - 56 - static void frob_writable_data(const struct module_layout *layout, 57 - int (*set_memory)(unsigned long start, int num_pages)) 58 - { 59 - set_memory((unsigned long)layout->base + layout->ro_after_init_size, 60 - (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT); 61 - } 62 - 63 - static bool layout_check_misalignment(const struct module_layout *layout) 64 - { 65 - return WARN_ON(!PAGE_ALIGNED(layout->base)) || 66 - WARN_ON(!PAGE_ALIGNED(layout->text_size)) || 67 - WARN_ON(!PAGE_ALIGNED(layout->ro_size)) || 68 - WARN_ON(!PAGE_ALIGNED(layout->ro_after_init_size)) || 69 - WARN_ON(!PAGE_ALIGNED(layout->size)); 70 - } 71 - 72 - bool module_check_misalignment(const struct module *mod) 73 - { 74 - if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) 75 - return false; 76 - 77 - return layout_check_misalignment(&mod->core_layout) || 78 - layout_check_misalignment(&mod->data_layout) || 79 - layout_check_misalignment(&mod->init_layout); 80 - } 81 - 82 29 void module_enable_x(const struct module *mod) 83 30 { 84 - if (!PAGE_ALIGNED(mod->core_layout.base) || 85 - !PAGE_ALIGNED(mod->init_layout.base)) 86 - return; 87 - 88 - frob_text(&mod->core_layout, set_memory_x); 89 - frob_text(&mod->init_layout, set_memory_x); 31 + for_class_mod_mem_type(type, text) 32 + module_set_memory(mod, type, set_memory_x); 90 33 } 91 34 92 35 void module_enable_ro(const struct module *mod, bool after_init) ··· 41 98 return; 42 99 #endif 43 100 44 - set_vm_flush_reset_perms(mod->core_layout.base); 45 - set_vm_flush_reset_perms(mod->init_layout.base); 46 - frob_text(&mod->core_layout, set_memory_ro); 47 - 48 - frob_rodata(&mod->data_layout, set_memory_ro); 49 - frob_text(&mod->init_layout, set_memory_ro); 50 - frob_rodata(&mod->init_layout, set_memory_ro); 101 + module_set_memory(mod, MOD_TEXT, set_memory_ro); 102 + module_set_memory(mod, MOD_INIT_TEXT, set_memory_ro); 103 + module_set_memory(mod, MOD_RODATA, set_memory_ro); 104 + module_set_memory(mod, MOD_INIT_RODATA, set_memory_ro); 51 105 52 106 if (after_init) 53 - frob_ro_after_init(&mod->data_layout, set_memory_ro); 107 + module_set_memory(mod, MOD_RO_AFTER_INIT, set_memory_ro); 54 108 } 55 109 56 110 void module_enable_nx(const struct module *mod) ··· 55 115 if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) 56 116 return; 57 117 58 - frob_rodata(&mod->data_layout, set_memory_nx); 59 - frob_ro_after_init(&mod->data_layout, set_memory_nx); 60 - frob_writable_data(&mod->data_layout, set_memory_nx); 61 - frob_rodata(&mod->init_layout, set_memory_nx); 62 - frob_writable_data(&mod->init_layout, set_memory_nx); 118 + for_class_mod_mem_type(type, data) 119 + module_set_memory(mod, type, set_memory_nx); 63 120 } 64 121 65 122 int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
+2 -5
kernel/module/tracking.c
··· 15 15 #include "internal.h" 16 16 17 17 static LIST_HEAD(unloaded_tainted_modules); 18 + extern struct dentry *mod_debugfs_root; 18 19 19 20 int try_add_tainted_module(struct module *mod) 20 21 { ··· 121 120 122 121 static int __init unloaded_tainted_modules_init(void) 123 122 { 124 - struct dentry *dir; 125 - 126 - dir = debugfs_create_dir("modules", NULL); 127 - debugfs_create_file("unloaded_tainted", 0444, dir, NULL, 123 + debugfs_create_file("unloaded_tainted", 0444, mod_debugfs_root, NULL, 128 124 &unloaded_tainted_modules_fops); 129 - 130 125 return 0; 131 126 } 132 127 module_init(unloaded_tainted_modules_init);
+17 -22
kernel/module/tree_lookup.c
··· 21 21 22 22 static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n) 23 23 { 24 - struct module_layout *layout = container_of(n, struct module_layout, mtn.node); 24 + struct module_memory *mod_mem = container_of(n, struct module_memory, mtn.node); 25 25 26 - return (unsigned long)layout->base; 26 + return (unsigned long)mod_mem->base; 27 27 } 28 28 29 29 static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n) 30 30 { 31 - struct module_layout *layout = container_of(n, struct module_layout, mtn.node); 31 + struct module_memory *mod_mem = container_of(n, struct module_memory, mtn.node); 32 32 33 - return (unsigned long)layout->size; 33 + return (unsigned long)mod_mem->size; 34 34 } 35 35 36 36 static __always_inline bool ··· 77 77 */ 78 78 void mod_tree_insert(struct module *mod) 79 79 { 80 - mod->core_layout.mtn.mod = mod; 81 - mod->init_layout.mtn.mod = mod; 82 - 83 - __mod_tree_insert(&mod->core_layout.mtn, &mod_tree); 84 - if (mod->init_layout.size) 85 - __mod_tree_insert(&mod->init_layout.mtn, &mod_tree); 86 - 87 - #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 88 - mod->data_layout.mtn.mod = mod; 89 - __mod_tree_insert(&mod->data_layout.mtn, &mod_data_tree); 90 - #endif 80 + for_each_mod_mem_type(type) { 81 + mod->mem[type].mtn.mod = mod; 82 + if (mod->mem[type].size) 83 + __mod_tree_insert(&mod->mem[type].mtn, &mod_tree); 84 + } 91 85 } 92 86 93 87 void mod_tree_remove_init(struct module *mod) 94 88 { 95 - if (mod->init_layout.size) 96 - __mod_tree_remove(&mod->init_layout.mtn, &mod_tree); 89 + for_class_mod_mem_type(type, init) { 90 + if (mod->mem[type].size) 91 + __mod_tree_remove(&mod->mem[type].mtn, &mod_tree); 92 + } 97 93 } 98 94 99 95 void mod_tree_remove(struct module *mod) 100 96 { 101 - __mod_tree_remove(&mod->core_layout.mtn, &mod_tree); 102 - mod_tree_remove_init(mod); 103 - #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC 104 - __mod_tree_remove(&mod->data_layout.mtn, &mod_data_tree); 105 - #endif 97 + for_each_mod_mem_type(type) { 98 + if (mod->mem[type].size) 99 + __mod_tree_remove(&mod->mem[type].mtn, &mod_tree); 100 + } 106 101 } 107 102 108 103 struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
+1 -1
kernel/params.c
··· 948 948 complete(mk->kobj_completion); 949 949 } 950 950 951 - struct kobj_type module_ktype = { 951 + const struct kobj_type module_ktype = { 952 952 .release = module_kobj_release, 953 953 .sysfs_ops = &module_sysfs_ops, 954 954 };
+1 -1
kernel/printk/printk.c
··· 89 89 * console_sem protects updates to console->seq and console_suspended, 90 90 * and also provides serialization for console printing. 91 91 */ 92 - static DEFINE_SEMAPHORE(console_sem); 92 + static DEFINE_SEMAPHORE(console_sem, 1); 93 93 HLIST_HEAD(console_list); 94 94 EXPORT_SYMBOL_GPL(console_list); 95 95 DEFINE_STATIC_SRCU(console_srcu);
+1 -2
kernel/trace/ftrace.c
··· 8006 8006 * and returns 1 in case we resolved all the requested symbols, 8007 8007 * 0 otherwise. 8008 8008 */ 8009 - static int kallsyms_callback(void *data, const char *name, 8010 - struct module *mod, unsigned long addr) 8009 + static int kallsyms_callback(void *data, const char *name, unsigned long addr) 8011 8010 { 8012 8011 struct kallsyms_data *args = data; 8013 8012 const char **sym;
-1
kernel/trace/rv/reactor_panic.c
··· 38 38 module_init(register_react_panic); 39 39 module_exit(unregister_react_panic); 40 40 41 - MODULE_LICENSE("GPL"); 42 41 MODULE_AUTHOR("Daniel Bristot de Oliveira"); 43 42 MODULE_DESCRIPTION("panic rv reactor: panic if an exception is found.");
-1
kernel/trace/rv/reactor_printk.c
··· 37 37 module_init(register_react_printk); 38 38 module_exit(unregister_react_printk); 39 39 40 - MODULE_LICENSE("GPL"); 41 40 MODULE_AUTHOR("Daniel Bristot de Oliveira"); 42 41 MODULE_DESCRIPTION("printk rv reactor: printk if an exception is hit.");
-1
kernel/watch_queue.c
··· 29 29 30 30 MODULE_DESCRIPTION("Watch queue"); 31 31 MODULE_AUTHOR("Red Hat, Inc."); 32 - MODULE_LICENSE("GPL"); 33 32 34 33 #define WATCH_QUEUE_NOTE_SIZE 128 35 34 #define WATCH_QUEUE_NOTES_PER_PAGE (PAGE_SIZE / WATCH_QUEUE_NOTE_SIZE)
+3
lib/Makefile
··· 231 231 obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) += syscall.o 232 232 233 233 obj-$(CONFIG_DYNAMIC_DEBUG_CORE) += dynamic_debug.o 234 + #ensure exported functions have prototypes 235 + CFLAGS_dynamic_debug.o := -DDYNAMIC_DEBUG_MODULE 236 + 234 237 obj-$(CONFIG_SYMBOLIC_ERRNAME) += errname.o 235 238 236 239 obj-$(CONFIG_NLATTR) += nlattr.o
-1
lib/btree.c
··· 794 794 795 795 MODULE_AUTHOR("Joern Engel <joern@logfs.org>"); 796 796 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>"); 797 - MODULE_LICENSE("GPL");
-5
lib/crypto/blake2s-generic.c
··· 12 12 #include <linux/types.h> 13 13 #include <linux/string.h> 14 14 #include <linux/kernel.h> 15 - #include <linux/module.h> 16 15 #include <linux/init.h> 17 16 #include <linux/bug.h> 18 17 #include <asm/unaligned.h> ··· 108 109 } 109 110 110 111 EXPORT_SYMBOL(blake2s_compress_generic); 111 - 112 - MODULE_LICENSE("GPL v2"); 113 - MODULE_DESCRIPTION("BLAKE2s hash function"); 114 - MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");
-1
lib/crypto/blake2s.c
··· 67 67 } 68 68 69 69 module_init(blake2s_mod_init); 70 - MODULE_LICENSE("GPL v2"); 71 70 MODULE_DESCRIPTION("BLAKE2s hash function"); 72 71 MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");
+41 -10
lib/dynamic_debug.c
··· 1223 1223 * Allocate a new ddebug_table for the given module 1224 1224 * and add it to the global list. 1225 1225 */ 1226 - static int __ddebug_add_module(struct _ddebug_info *di, unsigned int base, 1227 - const char *modname) 1226 + static int ddebug_add_module(struct _ddebug_info *di, const char *modname) 1228 1227 { 1229 1228 struct ddebug_table *dt; 1230 1229 ··· 1260 1261 1261 1262 vpr_info("%3u debug prints in module %s\n", di->num_descs, modname); 1262 1263 return 0; 1263 - } 1264 - 1265 - int ddebug_add_module(struct _ddebug_info *di, const char *modname) 1266 - { 1267 - return __ddebug_add_module(di, 0, modname); 1268 1264 } 1269 1265 1270 1266 /* helper for ddebug_dyndbg_(boot|module)_param_cb */ ··· 1308 1314 kfree(dt); 1309 1315 } 1310 1316 1317 + #ifdef CONFIG_MODULES 1318 + 1311 1319 /* 1312 1320 * Called in response to a module being unloaded. Removes 1313 1321 * any ddebug_table's which point at the module. 1314 1322 */ 1315 - int ddebug_remove_module(const char *mod_name) 1323 + static int ddebug_remove_module(const char *mod_name) 1316 1324 { 1317 1325 struct ddebug_table *dt, *nextdt; 1318 1326 int ret = -ENOENT; ··· 1332 1336 v2pr_info("removed module \"%s\"\n", mod_name); 1333 1337 return ret; 1334 1338 } 1339 + 1340 + static int ddebug_module_notify(struct notifier_block *self, unsigned long val, 1341 + void *data) 1342 + { 1343 + struct module *mod = data; 1344 + int ret = 0; 1345 + 1346 + switch (val) { 1347 + case MODULE_STATE_COMING: 1348 + ret = ddebug_add_module(&mod->dyndbg_info, mod->name); 1349 + if (ret) 1350 + WARN(1, "Failed to allocate memory: dyndbg may not work properly.\n"); 1351 + break; 1352 + case MODULE_STATE_GOING: 1353 + ddebug_remove_module(mod->name); 1354 + break; 1355 + } 1356 + 1357 + return notifier_from_errno(ret); 1358 + } 1359 + 1360 + static struct notifier_block ddebug_module_nb = { 1361 + .notifier_call = ddebug_module_notify, 1362 + .priority = 0, /* dynamic debug depends on jump label */ 1363 + }; 1364 + 1365 + #endif /* CONFIG_MODULES */ 1335 1366 1336 1367 static void ddebug_remove_all_tables(void) 1337 1368 { ··· 1411 1388 .num_classes = __stop___dyndbg_classes - __start___dyndbg_classes, 1412 1389 }; 1413 1390 1391 + #ifdef CONFIG_MODULES 1392 + ret = register_module_notifier(&ddebug_module_nb); 1393 + if (ret) { 1394 + pr_warn("Failed to register dynamic debug module notifier\n"); 1395 + return ret; 1396 + } 1397 + #endif /* CONFIG_MODULES */ 1398 + 1414 1399 if (&__start___dyndbg == &__stop___dyndbg) { 1415 1400 if (IS_ENABLED(CONFIG_DYNAMIC_DEBUG)) { 1416 1401 pr_warn("_ddebug table is empty in a CONFIG_DYNAMIC_DEBUG build\n"); ··· 1439 1408 mod_ct++; 1440 1409 di.num_descs = mod_sites; 1441 1410 di.descs = iter_mod_start; 1442 - ret = __ddebug_add_module(&di, i - mod_sites, modname); 1411 + ret = ddebug_add_module(&di, modname); 1443 1412 if (ret) 1444 1413 goto out_err; 1445 1414 ··· 1450 1419 } 1451 1420 di.num_descs = mod_sites; 1452 1421 di.descs = iter_mod_start; 1453 - ret = __ddebug_add_module(&di, i - mod_sites, modname); 1422 + ret = ddebug_add_module(&di, modname); 1454 1423 if (ret) 1455 1424 goto out_err; 1456 1425
-1
lib/pldmfw/pldmfw.c
··· 875 875 EXPORT_SYMBOL(pldmfw_flash_image); 876 876 877 877 MODULE_AUTHOR("Jacob Keller <jacob.e.keller@intel.com>"); 878 - MODULE_LICENSE("GPL v2"); 879 878 MODULE_DESCRIPTION("PLDM firmware flash update library");
-1
lib/test_fprobe.c
··· 168 168 169 169 kunit_test_suites(&fprobe_test_suite); 170 170 171 - MODULE_LICENSE("GPL");
-1
mm/zpool.c
··· 395 395 return zpool->driver->sleep_mapped; 396 396 } 397 397 398 - MODULE_LICENSE("GPL"); 399 398 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>"); 400 399 MODULE_DESCRIPTION("Common API for compressed memory storage");
-1
mm/zswap.c
··· 1540 1540 /* must be late so crypto has time to come up */ 1541 1541 late_initcall(init_zswap); 1542 1542 1543 - MODULE_LICENSE("GPL"); 1544 1543 MODULE_AUTHOR("Seth Jennings <sjennings@variantweb.net>"); 1545 1544 MODULE_DESCRIPTION("Compressed cache for swap pages");
+2 -4
net/rxrpc/call_object.c
··· 40 40 41 41 struct kmem_cache *rxrpc_call_jar; 42 42 43 - static struct semaphore rxrpc_call_limiter = 44 - __SEMAPHORE_INITIALIZER(rxrpc_call_limiter, 1000); 45 - static struct semaphore rxrpc_kernel_call_limiter = 46 - __SEMAPHORE_INITIALIZER(rxrpc_kernel_call_limiter, 1000); 43 + static DEFINE_SEMAPHORE(rxrpc_call_limiter, 1000); 44 + static DEFINE_SEMAPHORE(rxrpc_kernel_call_limiter, 1000); 47 45 48 46 void rxrpc_poke_call(struct rxrpc_call *call, enum rxrpc_call_poke_trace what) 49 47 {
+3
scripts/gdb/linux/constants.py.in
··· 54 54 /* linux/htimer.h */ 55 55 LX_GDBPARSED(hrtimer_resolution) 56 56 57 + /* linux/module.h */ 58 + LX_GDBPARSED(MOD_TEXT) 59 + 57 60 /* linux/mount.h */ 58 61 LX_VALUE(MNT_NOSUID) 59 62 LX_VALUE(MNT_NODEV)
+2 -2
scripts/gdb/linux/modules.py
··· 13 13 14 14 import gdb 15 15 16 - from linux import cpus, utils, lists 16 + from linux import cpus, utils, lists, constants 17 17 18 18 19 19 module_type = utils.CachedType("struct module") ··· 73 73 " " if utils.get_long_type().sizeof == 8 else "")) 74 74 75 75 for module in module_list(): 76 - layout = module['core_layout'] 76 + layout = module['mem'][constants.LX_MOD_TEXT] 77 77 gdb.write("{address} {name:<19} {size:>8} {ref}".format( 78 78 address=str(layout['base']).split()[0], 79 79 name=module['name'].string(),
+2 -2
scripts/gdb/linux/symbols.py
··· 15 15 import os 16 16 import re 17 17 18 - from linux import modules, utils 18 + from linux import modules, utils, constants 19 19 20 20 21 21 if hasattr(gdb, 'Breakpoint'): ··· 109 109 110 110 def load_module_symbols(self, module): 111 111 module_name = module['name'].string() 112 - module_addr = str(module['core_layout']['base']).split()[0] 112 + module_addr = str(module['mem'][constants.LX_MOD_TEXT]['base']).split()[0] 113 113 114 114 module_file = self._get_module_file(module_name) 115 115 if not module_file and not self.module_files_updated:
+3 -9
scripts/mod/modpost.c
··· 22 22 #include <errno.h> 23 23 #include "modpost.h" 24 24 #include "../../include/linux/license.h" 25 + #include "../../include/linux/module_symbol.h" 25 26 26 27 /* Are we using CONFIG_MODVERSIONS? */ 27 28 static bool modversions; ··· 1113 1112 return 1; 1114 1113 } 1115 1114 1116 - static inline int is_arm_mapping_symbol(const char *str) 1117 - { 1118 - return str[0] == '$' && 1119 - (str[1] == 'a' || str[1] == 'd' || str[1] == 't' || str[1] == 'x') 1120 - && (str[2] == '\0' || str[2] == '.'); 1121 - } 1122 - 1123 1115 /* 1124 1116 * If there's no name there, ignore it; likewise, ignore it if it's 1125 - * one of the magic symbols emitted used by current ARM tools. 1117 + * one of the magic symbols emitted used by current tools. 1126 1118 * 1127 1119 * Otherwise if find_symbols_between() returns those symbols, they'll 1128 1120 * fail the whitelist tests and cause lots of false alarms ... fixable ··· 1128 1134 1129 1135 if (!name || !strlen(name)) 1130 1136 return 0; 1131 - return !is_arm_mapping_symbol(name); 1137 + return !is_mapping_symbol(name); 1132 1138 } 1133 1139 1134 1140 /**