module: Limit ELF includes in linux/module.h to uapi/linux/elf.h
Petr Pavlu
maintainerv2 cover module: Limit ELF includes in linux/module.h to uapi/linux/elf.h
linux/module.h appears in roughly 15k #include directives across the kernel. This makes it a "hot" header, so it should avoid pulling in unnecessary definitions.
The header currently includes linux/elf.h but this is largely unnecessary because it requires only the base ELF structures, such as Elf_Ehdr and Elf_Sym, which are declared in uapi/linux/elf.h.
Replace the linux/elf.h include with uapi/linux/elf.h. On x86_64_defconfig, including linux/module.h alone results in 2.2 MB of preprocessed data. This change reduces that to 1.5 MB.
The first five patches are cleanups to avoid relying on declarations being indirectly provided through linux/module.h -> linux/elf.h. These patches are mostly independent and can go through their respective trees if preferred, except for the cpufeature patch, which depends on the preceding s390 cleanup. The final module patch depends on all of these changes.
It is hard to identify all files that indirectly reference something only through linux/module.h -> linux/elf.h. The changes were build-tested with allmodconfig on arm, arm64, m68k, mips, powerpc64, riscv, s390, x86_64.
---
Changes in v2:
- Rebase on top of v7.3-rc2.
- Add the missing include of linux/slab.h in drivers/virt/coco/arm-cca-guest/arm-cca-guest.c.
- Remove the asm/mmu_context.h include from asm/elf.h on s390.
- Add the missing include of linux/elf.h in linux/cpufeature.h, reported by the kernel test robot.
- Clean up commit descriptions.
- Link to v1: https://patch.msgid.link/[email protected]
---
Petr Pavlu (6):
x86: Avoid indirect includes through linux/module.h -> linux/elf.h
s390: Avoid indirect includes through linux/module.h -> linux/elf.h
drivers: Include linux/slab.h for kmalloc() functions
s390: Remove the asm/mmu_context.h include from asm/elf.h
cpufeature: Include asm/elf.h for ELF_PLATFORM
module: Limit ELF includes in linux/module.h to uapi/linux/elf.h
arch/s390/include/asm/elf.h | 1 - arch/s390/kernel/nospec-branch.c | 1 + arch/s390/kernel/processor.c | 1 + arch/s390/mm/mmap.c | 1 + arch/x86/entry/entry_fred.c | 1 + arch/x86/entry/syscall_32.c | 1 + arch/x86/kernel/cpu/intel.c | 1 + arch/x86/kernel/fpu/xstate.c | 2 +- arch/x86/kernel/process.c | 1 + arch/x86/kernel/signal.c | 1 + drivers/char/tpm/tpm_crb_ffa.c | 1 + drivers/s390/char/uvdevice.c | 2 ++ drivers/virt/coco/arm-cca-guest/arm-cca-guest.c | 1 + include/linux/cpufeature.h | 1 + include/linux/module.h | 4 ++-- 15 files changed, 16 insertions(+), 4 deletions(-)
---
base-commit: df2908090cda368b01ff43709f51890076c56157
change-id: 20260816-module-include-elf-a9c10c041867
Petr Pavlu
maintainerPATCH v2 1/6 x86: Avoid indirect includes through linux/module.h -> linux/elf.h
Several x86 files use ELF-related declarations and currently rely on them being provided indirectly through linux/module.h -> linux/elf.h:
* arch/x86/entry/entry_fred.c: ia32_enabled() -> asm/ia32.h,
* arch/x86/entry/syscall_32.c: vdso32_image -> asm/vdso.h,
* arch/x86/kernel/cpu/intel.c: ELF_HWCAP2 -> asm/elf.h,
* arch/x86/kernel/fpu/xstate.c: elf_coredump_extra_notes_write(), elf_coredump_extra_notes_size(), NT_X86_XSAVE_LAYOUT, elf_note -> linux/elf.h,
* arch/x86/kernel/process.c: mmap_is_ia32() -> asm/elf.h,
* arch/x86/kernel/signal.c: get_sigframe_size() -> asm/elf.h.
Add the missing includes to these files in preparation for removing the linux/elf.h include from linux/module.h.
--- arch/x86/entry/entry_fred.c | 1 + arch/x86/entry/syscall_32.c | 1 + arch/x86/kernel/cpu/intel.c | 1 + arch/x86/kernel/fpu/xstate.c | 2 +- arch/x86/kernel/process.c | 1 + arch/x86/kernel/signal.c | 1 + 6 files changed, 6 insertions(+), 1 deletion(-)
arch/x86/entry/entry_fred.c +1 -0
@@ -9,6 +9,7 @@#include <asm/desc.h> #include <asm/fred.h> +#include <asm/ia32.h> #include <asm/idtentry.h> #include <asm/syscall.h> #include <asm/trapnr.h>
arch/x86/entry/syscall_32.c +1 -0
@@ -12,6 +12,7 @@#include <asm/traps.h> #include <asm/cpufeature.h> #include <asm/syscall.h> +#include <asm/vdso.h> #ifdef CONFIG_IA32_EMULATION #define __SYSCALL_WITH_COMPAT (nr, native, compat) __SYSCALL(nr, compat)
arch/x86/kernel/cpu/intel.c +1 -0
@@ -17,6 +17,7 @@#include <asm/cpufeature.h> #include <asm/cpu.h> #include <asm/cpuid/api.h> +#include <asm/elf.h> #include <asm/hwcap2.h> #include <asm/intel-family.h> #include <asm/microcode.h>
arch/x86/kernel/fpu/xstate.c +1 -1
@@ -7,6 +7,7 @@#include <linux/bitops.h> #include <linux/compat.h> #include <linux/cpu.h> +#include <linux/elf.h> #include <linux/mman.h> #include <linux/kvm_types.h> #include <linux/nospec.h>
@@ -26,7 +27,6 @@#include <asm/msr.h> #include <asm/tlbflush.h> #include <asm/prctl.h> -#include <asm/elf.h> #include <uapi/asm/elf.h>
arch/x86/kernel/process.c +1 -0
@@ -54,6 +54,7 @@#include <asm/mmu_context.h> #include <asm/msr.h> #include <asm/shstk.h> +#include <asm/elf.h> #include "process.h"
arch/x86/kernel/signal.c +1 -0
@@ -29,6 +29,7 @@#include <linux/syscalls.h> #include <linux/rseq.h> +#include <asm/elf.h> #include <asm/processor.h> #include <asm/ucontext.h> #include <asm/fpu/signal.h>
Signature · 1 line
2.55.0
Petr Pavlu
maintainerPATCH v2 2/6 s390: Avoid indirect includes through linux/module.h -> linux/elf.h
In reply to Petr PavluSeveral s390 files use declarations that are provided indirectly through linux/module.h -> linux/elf.h:
* arch/s390/kernel/nospec-branch.c: s390_kernel_write() -> linux/uaccess.h,
* drivers/s390/char/uvdevice.c: copy_to_user(), copy_from_user(), put_user(), get_user() -> linux/uaccess.h; file_operations -> linux/fs.h.
Add the missing includes to these files in preparation for removing the linux/elf.h include from linux/module.h.
--- arch/s390/kernel/nospec-branch.c | 1 + drivers/s390/char/uvdevice.c | 2 ++ 2 files changed, 3 insertions(+)
arch/s390/kernel/nospec-branch.c +1 -0
@@ -2,6 +2,7 @@#include <linux/module.h> #include <linux/device.h> #include <linux/cpu.h> +#include <linux/uaccess.h> #include <asm/nospec-branch.h> int nobp =IS_ENABLED (CONFIG_KERNEL_NOBP );
drivers/s390/char/uvdevice.c +2 -0
@@ -28,6 +28,8 @@#include <linux/vmalloc.h> #include <linux/slab.h> #include <linux/cpufeature.h> +#include <linux/fs.h> +#include <linux/uaccess.h> #include <asm/uvdevice.h> #include <asm/uv.h>
Signature · 1 line
2.55.0
Petr Pavlu
maintainerPATCH v2 3/6 drivers: Include linux/slab.h for kmalloc() functions
In reply to Petr Pavludrivers/char/tpm/tpm_crb_ffa.c and drivers/virt/coco/arm-cca-guest/arm-cca-guest.c use kmalloc() functions, which are declared in linux/slab.h. They currently rely on this header being included indirectly through linux/module.h -> linux/elf.h.
Add the missing includes in preparation for removing the linux/elf.h include from linux/module.h.
--- drivers/char/tpm/tpm_crb_ffa.c | 1 + drivers/virt/coco/arm-cca-guest/arm-cca-guest.c | 1 + 2 files changed, 2 insertions(+)
drivers/char/tpm/tpm_crb_ffa.c +1 -0
@@ -12,6 +12,7 @@#include <linux/arm_ffa.h> #include <linux/delay.h> #include <linux/moduleparam.h> +#include <linux/slab.h> #include "tpm_crb_ffa.h" static unsigned int busy_timeout_ms =2000 ;
drivers/virt/coco/arm-cca-guest/arm-cca-guest.c +1 -0
@@ -8,6 +8,7 @@#include <linux/kernel.h> #include <linux/device-id/platform.h> #include <linux/module.h> +#include <linux/slab.h> #include <linux/smp.h> #include <linux/tsm.h> #include <linux/types.h>
Signature · 1 line
2.55.0
Petr Pavlu
maintainerPATCH v2 4/6 s390: Remove the asm/mmu_context.h include from asm/elf.h
In reply to Petr PavluThe asm/mmu_context.h include was added to asm/elf.h by commit 146e4b3c8b92 ("[S390] 1K/2K page table pages.") in 2008, when the new macro elf_read_implies_exec() used disable_noexec() / current->mm->context.noexec. Commit 043d07084b53 ("[S390] Remove data execution protection") removed that macro in 2011 and asm/elf.h no longer needs asm/mmu_context.h.
Several s390 files currently depend on declarations provided indirectly through asm/elf.h -> asm/mmu_context.h:
* arch/s390/mm/mmap.c: check_asce_limit() -> asm/pgalloc.h,
* arch/s390/kernel/processor.c: enter_lazy_tlb() -> asm/mmu_context.h.
Add the missing includes to these files and remove the asm/mmu_context.h include from asm/elf.h.
This is a preparatory cleanup for adding the asm/elf.h include in linux/cpufeature.h, which would otherwise create the following cycle:
linux/pgtable.h -> asm/pgtable.h -> linux/cpufeature.h -> asm/elf.h -> asm/mmu_context.h -> asm/pgalloc.h -> linux/mm.h -> linux/pgtable.h
--- arch/s390/include/asm/elf.h | 1 - arch/s390/kernel/processor.c | 1 + arch/s390/mm/mmap.c | 1 + 3 files changed, 2 insertions(+), 1 deletion(-)
arch/s390/include/asm/elf.h +0 -1
@@ -170,7 +170,6 @@ typedef s390_fp_regs elf_fpregset_t;typedef s390_regs elf_gregset_t ;#include <linux/sched/mm.h> /* for task_struct */ -#include <asm/mmu_context.h> /* * This is used to ensure we don't load something for the wrong architecture.
arch/s390/kernel/processor.c +1 -0
@@ -27,6 +27,7 @@#include <asm/param.h> #include <asm/sclp.h> #include <asm/smp.h> +#include <asm/mmu_context.h> unsigned long __read_mostly elf_hwcap;char elf_platform[ELF_PLATFORM_SIZE ];
arch/s390/mm/mmap.c +1 -0
@@ -18,6 +18,7 @@#include <linux/security.h> #include <linux/hugetlb.h> #include <asm/elf.h> +#include <asm/pgalloc.h> static unsigned long stack_maxrandom_size (void ) {
Signature · 1 line
2.55.0
Petr Pavlu
maintainerPATCH v2 5/6 cpufeature: Include asm/elf.h for ELF_PLATFORM
In reply to Petr Pavlulinux/cpufeature.h uses ELF_PLATFORM, which is defined in asm/elf.h. It currently relies on that header being included indirectly through other files, specifically, drivers/base/cpu.c gets it via linux/module.h -> linux/elf.h.
Add the missing include in preparation for removing the linux/elf.h include from linux/module.h.
--- include/linux/cpufeature.h | 1 + 1 file changed, 1 insertion(+)
include/linux/cpufeature.h +1 -0
@@ -11,6 +11,7 @@#include <linux/init.h> #include <linux/mod_devicetable.h> #include <asm/cpufeature.h> +#include <asm/elf.h> /* * Macros imported from <asm/cpufeature.h>:
Signature · 1 line
2.55.0
Petr Pavlu
maintainerPATCH v2 6/6 module: Limit ELF includes in linux/module.h to uapi/linux/elf.h
In reply to Petr Pavlulinux/module.h appears in roughly 15k #include directives across the kernel. This makes it a "hot" header, so it should avoid pulling in unnecessary definitions.
The header currently includes linux/elf.h but this is largely unnecessary because it requires only the base ELF structures, such as Elf_Ehdr and Elf_Sym, which are declared in uapi/linux/elf.h.
Replace the linux/elf.h include with uapi/linux/elf.h. On x86_64_defconfig, including linux/module.h alone results in 2.2 MB of preprocessed data. This change reduces that to 1.5 MB.
--- include/linux/module.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/module.h +2 -2
@@ -17,7 +17,6 @@#include <linux/cleanup.h> #include <linux/kmod.h> #include <linux/init.h> -#include <linux/elf.h> #include <linux/stringify.h> #include <linux/kobject.h> #include <linux/moduleparam.h>
@@ -29,8 +28,9 @@#include <linux/srcu.h> #include <linux/static_call_types.h> #include <linux/dynamic_debug.h> -#include <linux/percpu.h> + +#include <uapi/linux/elf.h> #include <asm/module.h> #define MODULE_NAME_LEN __MODULE_NAME_LEN
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v2 3/6] drivers: Include linux/slab.h for kmalloc() functions
In reply to Petr PavluSashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
drivers/char/tpm/tpm_crb_ffa.canddrivers/virt/coco/arm-cca-guest/arm-cca-guest.cusekmalloc()functions, which are declared inlinux/slab.h. They currently rely on this header being included indirectly throughlinux/module.h->linux/elf.h.Add the missing includes in preparation for removing the
linux/elf.hinclude fromlinux/module.h.Signed-off-by Petr Pavlu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [PATCH v2 1/6] x86: Avoid indirect includes through linux/module.h -> linux/elf.h
In reply to Petr PavluSashiko has reviewed this patch and found no issues. It looks great!
Quoted 12 lines
Several x86 files use ELF-related declarations and currently rely on them being provided indirectly through
linux/module.h->linux/elf.h:*
arch/x86/entry/entry_fred.c:ia32_enabled()->asm/ia32.h,
*arch/x86/entry/syscall_32.c:vdso32_image->asm/vdso.h,
*arch/x86/kernel/cpu/intel.c:ELF_HWCAP2->asm/elf.h,
*arch/x86/kernel/fpu/xstate.c:elf_coredump_extra_notes_write(),elf_coredump_extra_notes_size(),NT_X86_XSAVE_LAYOUT,elf_note->linux/elf.h,
*arch/x86/kernel/process.c:mmap_is_ia32()->asm/elf.h,
*arch/x86/kernel/signal.c:get_sigframe_size()->asm/elf.h.
Quoted 4 lines
Add the missing includes to these files in preparation for removing the
linux/elf.hinclude fromlinux/module.h.Signed-off-by Petr Pavlu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH v2 2/6] s390: Avoid indirect includes through linux/module.h -> linux/elf.h
In reply to Petr PavluSashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
Several s390 files use declarations that are provided indirectly through
linux/module.h->linux/elf.h:*
arch/s390/kernel/nospec-branch.c:s390_kernel_write()->linux/uaccess.h,
*drivers/s390/char/uvdevice.c:copy_to_user(),copy_from_user(),put_user(),get_user()->linux/uaccess.h;file_operations->linux/fs.h.Add the missing includes to these files in preparation for removing the
linux/elf.hinclude fromlinux/module.h.Signed-off-by Petr Pavlu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH v2 5/6] cpufeature: Include asm/elf.h for ELF_PLATFORM
In reply to Petr PavluSashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
linux/cpufeature.husesELF_PLATFORM, which is defined inasm/elf.h. It currently relies on that header being included indirectly through other files, specifically,drivers/base/cpu.cgets it vialinux/module.h->linux/elf.h.Add the missing include in preparation for removing the
linux/elf.hinclude fromlinux/module.h.Reported-by kernel test robot <[email protected]>Signed-off-by Petr Pavlu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewRe: [PATCH v2 4/6] s390: Remove the asm/mmu_context.h include from asm/elf.h
In reply to Petr PavluSashiko has reviewed this patch and found no issues. It looks great!
Quoted 16 lines
The
asm/mmu_context.hinclude was added toasm/elf.hby commit146e4b3c8b92("[S390] 1K/2K page table pages.") in 2008, when the new macroelf_read_implies_exec()useddisable_noexec()/current->mm->context.noexec. Commit043d07084b53("[S390] Remove data execution protection") removed that macro in 2011 andasm/elf.hno longer needsasm/mmu_context.h.Several s390 files currently depend on declarations provided indirectly through
asm/elf.h->asm/mmu_context.h:*
arch/s390/mm/mmap.c:check_asce_limit()->asm/pgalloc.h,
*arch/s390/kernel/processor.c:enter_lazy_tlb()->asm/mmu_context.h.Add the missing includes to these files and remove the
asm/mmu_context.hinclude fromasm/elf.h.
Quoted 7 lines
This is a preparatory cleanup for adding the
asm/elf.hinclude inlinux/cpufeature.h, which would otherwise create the following cycle:
linux/pgtable.h->asm/pgtable.h->linux/cpufeature.h->asm/elf.h->asm/mmu_context.h->asm/pgalloc.h->linux/mm.h->linux/pgtable.hSigned-off-by Petr Pavlu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
linux/module.happears in roughly 15k #include directives across the kernel. This makes it a "hot" header, so it should avoid pulling in unnecessary definitions.The header currently includes
linux/elf.hbut this is largely unnecessary because it requires only the base ELF structures, such asElf_EhdrandElf_Sym, which are declared inuapi/linux/elf.h.
Quoted 5 lines
Replace the
linux/elf.hinclude withuapi/linux/elf.h. Onx86_64_defconfig, includinglinux/module.halone results in 2.2 MB of preprocessed data. This change reduces that to 1.5 MB.Signed-off-by Petr Pavlu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Petr Pavlu
maintainerQuoted 18 lines · Petr Pavlu
linux/module.happears in roughly 15k #include directives across the kernel. This makes it a "hot" header, so it should avoid pulling in unnecessary definitions.The header currently includes
linux/elf.hbut this is largely unnecessary because it requires only the base ELF structures, such asElf_EhdrandElf_Sym, which are declared inuapi/linux/elf.h.Replace the
linux/elf.hinclude withuapi/linux/elf.h. Onx86_64_defconfig, includinglinux/module.halone results in 2.2 MB of preprocessed data. This change reduces that to 1.5 MB.The first five patches are cleanups to avoid relying on declarations being indirectly provided through
linux/module.h->linux/elf.h. These patches are mostly independent and can go through their respective trees if preferred, except for the cpufeature patch, which depends on the preceding s390 cleanup. The final module patch depends on all of these changes.
Quoted 3 lines
It is hard to identify all files that indirectly reference something only through
linux/module.h->linux/elf.h. The changes were build-tested with allmodconfig on arm, arm64, m68k, mips, powerpc64, riscv, s390,x86_64.
I plan to pick up this series on modules-next by the end of the week. Acked-by tags from the relevant maintainers would be appreciated.
Signature · 2 lines
Thanks, Petr
Re: [PATCH v2 1/6] x86: Avoid indirect includes through linux/module.h -> linux/elf.h
In reply to Petr PavluQuoted 16 lines · Petr Pavlu
Several x86 files use ELF-related declarations and currently rely on them being provided indirectly through
linux/module.h->linux/elf.h:*
arch/x86/entry/entry_fred.c:ia32_enabled()->asm/ia32.h,
*arch/x86/entry/syscall_32.c:vdso32_image->asm/vdso.h,
*arch/x86/kernel/cpu/intel.c:ELF_HWCAP2->asm/elf.h,
*arch/x86/kernel/fpu/xstate.c:elf_coredump_extra_notes_write(),elf_coredump_extra_notes_size(),NT_X86_XSAVE_LAYOUT,elf_note->linux/elf.h,
*arch/x86/kernel/process.c:mmap_is_ia32()->asm/elf.h,
*arch/x86/kernel/signal.c:get_sigframe_size()->asm/elf.h.Add the missing includes to these files in preparation for removing the
linux/elf.hinclude fromlinux/module.h.Signed-off-by Petr Pavlu <[email protected]>
Quoted 8 lines
--- arch/x86/entry/entry_fred.c | 1 + arch/x86/entry/syscall_32.c | 1 + arch/x86/kernel/cpu/intel.c | 1 + arch/x86/kernel/fpu/xstate.c | 2 +- arch/x86/kernel/process.c | 1 + arch/x86/kernel/signal.c | 1 + 6 files changed, 6 insertions(+), 1 deletion(-)
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette