Skip to contentSkip to conversation

module: Limit ELF includes in linux/module.h to uapi/linux/elf.h

PATCH In review Petr Pavlu 3 participants 15 messages Sep 11 – Sep 22

Subsystems (12)
x86 KVM arm64 s390 +8 more subsystems
lore
Share & tools
lore

Petr Pavlu

author maintainer
#0 lore raw

v2 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

author maintainer
#1 lore raw

PATCH 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
index fb3594ddf731..ea3bb4298065 100644
@@ -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
index 91123a90710c..4ace5b534f65 100644
@@ -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
index 4297ceb2cb24..7dd5a039186e 100644
@@ -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
index a7b6524a9dea..8b90bf86a742 100644
@@ -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
index 346c438ac880..e7f736b53f41 100644
@@ -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
index 2404233336ab..0fe2d7a6b1af 100644
@@ -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
#2 lore raw

Re: [PATCH v2 1/6] x86: Avoid indirect includes through linux/module.h -> linux/elf.h

Sashiko 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.h include from linux/module.h.

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
maintainer Acked-by on v2 1/6
#3 lore raw

Re: [PATCH v2 1/6] x86: Avoid indirect includes through linux/module.h -> linux/elf.h

In reply to Petr Pavlu
Quoted 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.h include from linux/module.h.

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
author maintainer
#4 lore raw

PATCH v2 2/6 s390: Avoid indirect includes through linux/module.h -> linux/elf.h

In reply to Petr Pavlu

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.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
index e11ec15960a1..b62da9e81e79 100644
@@ -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
index 4d274a2ee84d..402b8a225d6b 100644
@@ -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
#5 lore raw

Re: [PATCH v2 2/6] s390: Avoid indirect includes through linux/module.h -> linux/elf.h

Sashiko 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.h include from linux/module.h.

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
author maintainer
#6 lore raw

PATCH v2 3/6 drivers: Include linux/slab.h for kmalloc() functions

In reply to Petr Pavlu

drivers/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
index 025c4d4b17ca..af4466cdc297 100644
@@ -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
index 0eeddd1ff05b..54dcfc1681ab 100644
@@ -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
#7 lore raw

Re: [PATCH v2 3/6] drivers: Include linux/slab.h for kmalloc() functions

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

drivers/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.

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
author maintainer
#8 lore raw

PATCH v2 4/6 s390: Remove the asm/mmu_context.h include from asm/elf.h

In reply to Petr Pavlu

The 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
index bb63fa4d20bb..e22e3ab25760 100644
@@ -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
index e33a3eccda56..76b2c7478c6e 100644
@@ -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
index ef7bfc87758c..8206f347e00e 100644
@@ -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
#9 lore raw

Re: [PATCH v2 4/6] s390: Remove the asm/mmu_context.h include from asm/elf.h

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 16 lines

The 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.

Quoted 7 lines

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

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
author maintainer
#10 lore raw

PATCH v2 5/6 cpufeature: Include asm/elf.h for ELF_PLATFORM

In reply to Petr Pavlu

linux/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
index 6aff540ee9e5..66027987e9d6 100644
@@ -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
#11 lore raw

Re: [PATCH v2 5/6] cpufeature: Include asm/elf.h for ELF_PLATFORM

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

linux/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.

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
author maintainer
#12 lore raw

PATCH v2 6/6 module: Limit ELF includes in linux/module.h to uapi/linux/elf.h

In reply to Petr Pavlu

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.

---
 include/linux/module.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/module.h +2 -2
index 96cc98568eea..1ec9e121a24b 100644
@@ -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
#13 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

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.

Quoted 5 lines

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.

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
author maintainer
#14 lore raw
In reply to Petr Pavlu
Quoted 18 lines · Petr Pavlu

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.

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