Linux内核模块采用专用的地址空间,有一定的固定大小,具体可以通过起机dmesg信息里的字段来确认
Memory: 245540K/262144K available (3043K kernel code, 1665K rwdata, 1112K rodata, 176K init, 6356K bss, 16604K reserved, 0K cma-reserved)
Virtual kernel memory layout:
vector : 0xffff0000 - 0xffff1000 ( 4 kB)
fixmap : 0xffc00000 - 0xfff00000 (3072 kB)
vmalloc : 0xd0800000 - 0xff000000 ( 744 MB)
lowmem : 0xc0000000 - 0xd0000000 ( 256 MB)
modules : 0xbe600000 - 0xc0000000 ( 26 MB) <<< 模块地址空间
.text : 0xc0018000 - 0xc04272b0 (4157 kB)
.init : 0xc0428000 - 0xc0454000 ( 176 kB)
.data : 0xc0454000 - 0xc05f45c0 (1666 kB)
.bss : 0xc05f45c0 - 0xc0c295ec (6357 kB)
所需空间大小
模块在加载时,会调用module_alloc()来申请一块内存来存放模块的内容,需要的大小如下:
代码段(.text) + 未初始化全局或静态变量(.bss) + 已初始化全局或静态变量(.data)
关联源码
模块在加载时,内核会调用module_alloc()来申请足够的内存来存放模块内容。module_alloc有2处定义:
-
kernel/module.c: 定义一个弱符号module_alloc(),若arch下没有定义强符号,就使用它,实际基本没什么用
void * __weak module_alloc(unsigned long size) { return vmalloc_exec(size); }
-
arch/arm/kernel/module.c: 基于CPU平台来定义module_alloc(),属于强符号
#ifdef CONFIG_MMU void *module_alloc(unsigned long size) { return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END, GFP_KERNEL, PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE, __builtin_return_address(0)); } #endif
-
实际范围为MODULES_VADDR~MODULES_END
#define MODULES_END (PAGE_OFFSET) #define MODULES_VADDR (MODULES_END - 16*1048576)
对于arm平台,默认模块地址空间为16M,修改内核代码,可以扩大到28M
https://patchwork.kernel.org/project/linux-arm-kernel/patch/002001cf07a1$fd4bdc10$f7e39430$@lge.com/
diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index c1f1a7e..cf1fb55 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -2257,6 +2257,10 @@ config ARM_CPU_SUSPEND
endmenu
+config MODULES_AREA_SIZE
+ int
+ default 0x1000000
+
source "net/Kconfig"
source "drivers/Kconfig"
diff --git a/arch/arm/include/asm/memory.h b/arch/arm/include/asm/memory.h
index 6976b03..3396758 100644
--- a/arch/arm/include/asm/memory.h
+++ b/arch/arm/include/asm/memory.h
@@ -32,13 +32,17 @@
#ifdef CONFIG_MMU
+#if CONFIG_MODULES_AREA_SIZE > SZ_32M
+#error Too much space for modules
+#endif
+
/*
* PAGE_OFFSET - the virtual address of the start of the kernel image
* TASK_SIZE - the maximum size of a user space task.
* TASK_UNMAPPED_BASE - the lower boundary of the mmap VM area
*/
-#define PAGE_OFFSET UL(CONFIG_PAGE_OFFSET)
-#define TASK_SIZE (UL(CONFIG_PAGE_OFFSET) - UL(SZ_16M))
+#define PAGE_OFFSET UL(CONFIG_PAGE_OFFSET)
+#define TASK_SIZE (UL(CONFIG_PAGE_OFFSET) -
UL(CONFIG_MODULES_AREA_SIZE))
#define TASK_UNMAPPED_BASE ALIGN(TASK_SIZE / 3, SZ_16M)
MODULE_PLTS
Linux新的内核引入module PLT(Procedure Link Table)机制,让模块加载使用vmalloc空间的方法,解决模块空间不够用的问题。
变更履历
- 内核版本4.5.0支持arm64
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fd045f6cd98ec4953147b318418bd45e441e52a3 - 内核版本4.8.0导入arm
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=35fa91eed817d2c65c59ef5a9737011313be6ac0
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=05123fef098220323e60834d5520b15d277e0415
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1031a7e674d1de481d641c3723d5f53b776f621f - arch/arm/kernel/module.c
#ifdef CONFIG_MMU
void *module_alloc(unsigned long size)
{
gfp_t gfp_mask = GFP_KERNEL;
void *p;
/* Silence the initial allocation */
if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS))
gfp_mask |= __GFP_NOWARN;
p = __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
gfp_mask, PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
__builtin_return_address(0));
if (!IS_ENABLED(CONFIG_ARM_MODULE_PLTS) || p)
return p;
return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
GFP_KERNEL, PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
__builtin_return_address(0));
}
#endif
使用方法
- 配置CONFIG_ARM_MODULE_PLTS=y
- 配置CONFIG_ARM64_MODULE_PLTS=y
-
最终的效果如下图所示,模块的地址并不是常见的0xbf打头,而是落在vmalloc区域
模块空间占用裁减
- 通过objdump -t命令可以查看模块的所有符号
- 识别所有符号里的.bss和.data部分,确认是否有大块的变量符号
- 整改大块的变量符号,将其转变为动态申请的内存形式