// 文件:linux_5.10/kernel/module.c
static struct module *layout_and_allocate(struct load_info *info, int flags)
{
...
ndx = find_sec(info, ".data..ro_after_init");
if (ndx)
info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
...
ndx = find_sec(info, "__jump_table");
if (ndx)
info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
/* Determine total sizes, and put offsets in sh_entsize. For now
this is done generically; there doesn't appear to be any
special cases for the architectures. */
layout_sections(info->mod, info);
layout_symtab(info->mod, info);
/* Allocate and move to the final place */
err = move_module(info->mod, info);
if (err)
return ERR_PTR(err);
/* Module has been copied to its final place now: return it. */
mod = (void *)info->sechdrs[info->index.mod].sh_addr;
kmemleak_load_module(mod, info);
return mod;
}
static int move_module(struct module *mod, struct load_info *info)
{
...
/* Do the allocs. */
ptr = module_alloc(mod->core_layout.size);
...
if (mod->init_layout.size) {
ptr = module_alloc(mod->init_layout.size);
...
}
...
}
void * __weak module_alloc(unsigned long size)
{
// 使用vmalloc申请内存
return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS,
NUMA_NO_NODE, __builtin_return_address(0));
}
占用统计
lsmod 或 /proc/modules
lsmod打印的KO列表中有个size属性,表示KO内存占用。
lsmod 的数据来自/proc/modules。
~# lsmod
Module Size Used by Tainted: GF
xxxx_ive 234750 0
xxx_ipcm 84490 0
xxxx_tpu 26507 0
xxxx_clock_cooling 4685 0
xxxx_sys 21052 1 xxxx_ive
~# cat /proc/modules
xxxx_ive 234750 0 - Live 0xffffffdf808a5000 (FO)
xxx_ipcm 84490 0 - Live 0xffffffdf80881000 (FO)
xxxx_tpu 26507 0 - Live 0xffffffdf80873000 (FO)
xxxxx_clock_cooling 4685 0 - Live 0xffffffdf8086e000 (FO)
xxxxx_sys 21052 1 xxxx_ive, Live 0xffffffdf80863000 (FO)