diff mbox series

[bpf,3/4] module: introduce module_alloc_huge

Message ID 20220422051813.1989257-4-song@kernel.org (mailing list archive)
State New
Headers show
Series bpf_prog_pack and vmalloc-on-huge-page fixes | expand

Commit Message

Song Liu April 22, 2022, 5:18 a.m. UTC
Introduce module_alloc_huge, which allocates huge page backed memory in
module memory space. The primary user of this memory is bpf_prog_pack
(multiple BPF programs sharing a huge page).

Signed-off-by: Song Liu <song@kernel.org>
---
 arch/x86/kernel/module.c     | 21 +++++++++++++++++++++
 include/linux/moduleloader.h |  5 +++++
 kernel/module.c              |  8 ++++++++
 3 files changed, 34 insertions(+)

Comments

kernel test robot April 22, 2022, 9:48 a.m. UTC | #1
Hi Song,

I love your patch! Yet something to improve:

[auto build test ERROR on bpf/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Song-Liu/bpf_prog_pack-and-vmalloc-on-huge-page-fixes/20220422-133605
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git master
config: x86_64-randconfig-a004 (https://download.01.org/0day-ci/archive/20220422/202204221700.93ehQrzU-lkp@intel.com/config)
compiler: gcc-11 (Debian 11.2.0-20) 11.2.0
reproduce (this is a W=1 build):
        # https://github.com/intel-lab-lkp/linux/commit/8a0dfde5aef7e95487be2f6e3ff9487d79a30714
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Song-Liu/bpf_prog_pack-and-vmalloc-on-huge-page-fixes/20220422-133605
        git checkout 8a0dfde5aef7e95487be2f6e3ff9487d79a30714
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   arch/x86/kernel/module.c: In function 'module_alloc_huge':
>> arch/x86/kernel/module.c:100:54: error: 'VM_ALLOW_HUGE_VMAP' undeclared (first use in this function); did you mean 'VM_NO_HUGE_VMAP'?
     100 |                                  VM_DEFER_KMEMLEAK | VM_ALLOW_HUGE_VMAP,
         |                                                      ^~~~~~~~~~~~~~~~~~
         |                                                      VM_NO_HUGE_VMAP
   arch/x86/kernel/module.c:100:54: note: each undeclared identifier is reported only once for each function it appears in


vim +100 arch/x86/kernel/module.c

    88	
    89	void *module_alloc_huge(unsigned long size)
    90	{
    91		gfp_t gfp_mask = GFP_KERNEL;
    92		void *p;
    93	
    94		if (PAGE_ALIGN(size) > MODULES_LEN)
    95			return NULL;
    96	
    97		p = __vmalloc_node_range(size, MODULE_ALIGN,
    98					 MODULES_VADDR + get_module_load_offset(),
    99					 MODULES_END, gfp_mask, PAGE_KERNEL,
 > 100					 VM_DEFER_KMEMLEAK | VM_ALLOW_HUGE_VMAP,
   101					 NUMA_NO_NODE, __builtin_return_address(0));
   102		if (p && (kasan_alloc_module_shadow(p, size, gfp_mask) < 0)) {
   103			vfree(p);
   104			return NULL;
   105		}
   106	
   107		return p;
   108	}
   109
diff mbox series

Patch

diff --git a/arch/x86/kernel/module.c b/arch/x86/kernel/module.c
index b98ffcf4d250..63f6a16c70dc 100644
--- a/arch/x86/kernel/module.c
+++ b/arch/x86/kernel/module.c
@@ -86,6 +86,27 @@  void *module_alloc(unsigned long size)
 	return p;
 }
 
+void *module_alloc_huge(unsigned long size)
+{
+	gfp_t gfp_mask = GFP_KERNEL;
+	void *p;
+
+	if (PAGE_ALIGN(size) > MODULES_LEN)
+		return NULL;
+
+	p = __vmalloc_node_range(size, MODULE_ALIGN,
+				 MODULES_VADDR + get_module_load_offset(),
+				 MODULES_END, gfp_mask, PAGE_KERNEL,
+				 VM_DEFER_KMEMLEAK | VM_ALLOW_HUGE_VMAP,
+				 NUMA_NO_NODE, __builtin_return_address(0));
+	if (p && (kasan_alloc_module_shadow(p, size, gfp_mask) < 0)) {
+		vfree(p);
+		return NULL;
+	}
+
+	return p;
+}
+
 #ifdef CONFIG_X86_32
 int apply_relocate(Elf32_Shdr *sechdrs,
 		   const char *strtab,
diff --git a/include/linux/moduleloader.h b/include/linux/moduleloader.h
index 9e09d11ffe5b..d34743a88938 100644
--- a/include/linux/moduleloader.h
+++ b/include/linux/moduleloader.h
@@ -26,6 +26,11 @@  unsigned int arch_mod_section_prepend(struct module *mod, unsigned int section);
    sections.  Returns NULL on failure. */
 void *module_alloc(unsigned long size);
 
+/* Allocator used for allocating memory in module memory space. If size is
+ * greater than PMD_SIZE, allow using huge pages. Returns NULL on failure.
+ */
+void *module_alloc_huge(unsigned long size);
+
 /* Free memory returned from module_alloc. */
 void module_memfree(void *module_region);
 
diff --git a/kernel/module.c b/kernel/module.c
index 6cea788fd965..2af20ac3209c 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2839,6 +2839,14 @@  void * __weak module_alloc(unsigned long size)
 			NUMA_NO_NODE, __builtin_return_address(0));
 }
 
+void * __weak module_alloc_huge(unsigned long size)
+{
+	return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
+				    GFP_KERNEL, PAGE_KERNEL_EXEC,
+				    VM_FLUSH_RESET_PERMS | VM_ALLOW_HUGE_VMAP,
+				    NUMA_NO_NODE, __builtin_return_address(0));
+}
+
 bool __weak module_init_section(const char *name)
 {
 	return strstarts(name, ".init");