diff mbox series

[v2,1/2] bpf: add __weak hook for allocating executable memory

Message ID 20181121131733.14910-2-ard.biesheuvel@linaro.org
State Superseded
Headers show
Series bpf: permit JIT allocations to be served outside the module region | expand

Commit Message

Ard Biesheuvel Nov. 21, 2018, 1:17 p.m. UTC
By default, BPF uses module_alloc() to allocate executable memory,
but this is not necessary on all arches and potentially undesirable
on some of them.

So break out the module_alloc() and module_memfree() calls into __weak
functions to allow them to be overridden in arch code.

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>

---
 kernel/bpf/core.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

-- 
2.17.1

Comments

kernel test robot Nov. 22, 2018, 8:01 p.m. UTC | #1
Hi Ard,

I love your patch! Yet something to improve:

[auto build test ERROR on bpf-next/master]
[also build test ERROR on v4.20-rc3 next-20181122]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Ard-Biesheuvel/bpf-permit-JIT-allocations-to-be-served-outside-the-module-region/20181123-033144
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: x86_64-randconfig-x002-201846 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All error/warnings (new ones prefixed by >>):

   kernel//bpf/core.c: In function 'bpf_jit_free_exec':
>> kernel//bpf/core.c:632:24: error: 'size' undeclared (first use in this function); did you mean 'ksize'?

     return module_memfree(size);
                           ^~~~
                           ksize
   kernel//bpf/core.c:632:24: note: each undeclared identifier is reported only once for each function it appears in
>> kernel//bpf/core.c:632:9: warning: 'return' with a value, in function returning void

     return module_memfree(size);
            ^~~~~~~~~~~~~~
   kernel//bpf/core.c:630:13: note: declared here
    void __weak bpf_jit_free_exec(const void *addr)
                ^~~~~~~~~~~~~~~~~

vim +632 kernel//bpf/core.c

   629	
   630	void __weak bpf_jit_free_exec(const void *addr)
   631	{
 > 632		return module_memfree(size);

   633	}
   634	

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
Ard Biesheuvel Nov. 22, 2018, 9:08 p.m. UTC | #2
> On 22 Nov 2018, at 21:01, kbuild test robot <lkp@intel.com> wrote:

> 

> Hi Ard,

> 

> I love your patch! Yet something to improve:

> 


Ugh, apologies for this. I’m sure i build tested /something/ but clearly not what i should have tested.

In any case, following up on the discussion I’ll have to respin this in any case. I’ll triple check next time that what i send out was tested properly.


> [auto build test ERROR on bpf-next/master]

> [also build test ERROR on v4.20-rc3 next-20181122]

> [if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

> 

> url:    https://github.com/0day-ci/linux/commits/Ard-Biesheuvel/bpf-permit-JIT-allocations-to-be-served-outside-the-module-region/20181123-033144

> base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master

> config: x86_64-randconfig-x002-201846 (attached as .config)

> compiler: gcc-7 (Debian 7.3.0-1) 7.3.0

> reproduce:

>        # save the attached .config to linux build tree

>        make ARCH=x86_64 

> 

> All error/warnings (new ones prefixed by >>):

> 

>   kernel//bpf/core.c: In function 'bpf_jit_free_exec':

>>> kernel//bpf/core.c:632:24: error: 'size' undeclared (first use in this function); did you mean 'ksize'?

>     return module_memfree(size);

>                           ^~~~

>                           ksize

>   kernel//bpf/core.c:632:24: note: each undeclared identifier is reported only once for each function it appears in

>>> kernel//bpf/core.c:632:9: warning: 'return' with a value, in function returning void

>     return module_memfree(size);

>            ^~~~~~~~~~~~~~

>   kernel//bpf/core.c:630:13: note: declared here

>    void __weak bpf_jit_free_exec(const void *addr)

>                ^~~~~~~~~~~~~~~~~

> 

> vim +632 kernel//bpf/core.c

> 

>   629    

>   630    void __weak bpf_jit_free_exec(const void *addr)

>   631    {

>> 632        return module_memfree(size);

>   633    }

>   634    

> 

> ---

> 0-DAY kernel test infrastructure                Open Source Technology Center

> https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

> <.config.gz>
diff mbox series

Patch

diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 1a796e0799ec..f28d8a5eb6b8 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -609,6 +609,16 @@  static void bpf_jit_uncharge_modmem(u32 pages)
 	atomic_long_sub(pages, &bpf_jit_current);
 }
 
+void *__weak bpf_jit_alloc_exec(unsigned long size)
+{
+	return module_alloc(size);
+}
+
+void __weak bpf_jit_free_exec(const void *addr)
+{
+	return module_memfree(size);
+}
+
 struct bpf_binary_header *
 bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
 		     unsigned int alignment,
@@ -626,7 +636,7 @@  bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
 
 	if (bpf_jit_charge_modmem(pages))
 		return NULL;
-	hdr = module_alloc(size);
+	hdr = bpf_jit_alloc_exec(size);
 	if (!hdr) {
 		bpf_jit_uncharge_modmem(pages);
 		return NULL;
@@ -650,7 +660,7 @@  void bpf_jit_binary_free(struct bpf_binary_header *hdr)
 {
 	u32 pages = hdr->pages;
 
-	module_memfree(hdr);
+	bpf_jit_free_exec(hdr);
 	bpf_jit_uncharge_modmem(pages);
 }