From: Rick Edgecombe <rick.p.edgecombe@intel.com>
To: daniel@iogearbox.net, ast@fb.com
Cc: netdev@vger.kernel.org, ard.biesheuvel@linaro.org,
dave.hansen@intel.com, kristen@linux.intel.com,
Rick Edgecombe <rick.p.edgecombe@intel.com>
Subject: [RFC PATCH 3/4] bpf: Charge bpf jit limit in bpf_jit_alloc_exec
Date: Tue, 5 Feb 2019 14:51:02 -0800 [thread overview]
Message-ID: <20190205225103.28296-4-rick.p.edgecombe@intel.com> (raw)
In-Reply-To: <20190205225103.28296-1-rick.p.edgecombe@intel.com>
This moves the calls to bpf_jit_charge_modmem and
bpf_jit_uncharge_modmem to bpf_jit_alloc_exec so the behavior can be
overridden for arch specific capabilities, for example if the arch
supports for allocating outside the module space.
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Alexei Starovoitov <ast@fb.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
---
include/linux/filter.h | 3 +++
kernel/bpf/core.c | 20 +++++++++++---------
2 files changed, 14 insertions(+), 9 deletions(-)
diff --git a/include/linux/filter.h b/include/linux/filter.h
index ad106d845b22..33c0ae5990e1 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -859,6 +859,9 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
unsigned int alignment,
bpf_jit_fill_hole_t bpf_fill_ill_insns);
void bpf_jit_binary_free(struct bpf_binary_header *hdr);
+int bpf_jit_charge_modmem(u32 pages);
+void bpf_jit_uncharge_modmem(u32 pages);
+
void bpf_jit_free(struct bpf_prog *fp);
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index f908b9356025..ce08a09839c2 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -722,7 +722,7 @@ static int __init bpf_jit_charge_init(void)
}
pure_initcall(bpf_jit_charge_init);
-static int bpf_jit_charge_modmem(u32 pages)
+int bpf_jit_charge_modmem(u32 pages)
{
if (atomic_long_add_return(pages, &bpf_jit_current) >
(bpf_jit_limit >> PAGE_SHIFT)) {
@@ -735,14 +735,22 @@ static int bpf_jit_charge_modmem(u32 pages)
return 0;
}
-static void bpf_jit_uncharge_modmem(u32 pages)
+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 *ret;
+ u32 pages = size / PAGE_SIZE;
+
+ if (bpf_jit_charge_modmem(pages))
+ return NULL;
+ ret = module_alloc(size);
+ if (!ret)
+ bpf_jit_uncharge_modmem(pages);
+ return ret;
}
void __weak bpf_jit_free_exec(void *addr)
@@ -765,13 +773,7 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,
size = round_up(proglen + sizeof(*hdr) + 128, PAGE_SIZE);
pages = size / PAGE_SIZE;
- if (bpf_jit_charge_modmem(pages))
- return NULL;
hdr = bpf_jit_alloc_exec(size);
- if (!hdr) {
- bpf_jit_uncharge_modmem(pages);
- return NULL;
- }
/* Fill space with illegal/arch-dep instructions. */
bpf_fill_ill_insns(hdr, size);
--
2.17.1
next prev parent reply other threads:[~2019-02-05 22:54 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-05 22:50 [RFC PATCH 0/4] Initial support for allocating BPF JITs in vmalloc for x86 Rick Edgecombe
2019-02-05 22:51 ` [RFC PATCH 1/4] bpf, x64: Implement BPF call retpoline Rick Edgecombe
2019-02-05 22:51 ` [RFC PATCH 2/4] bpf, x64: Increase distance for bpf calls Rick Edgecombe
2019-02-05 22:51 ` Rick Edgecombe [this message]
2019-02-05 22:51 ` [RFC PATCH 4/4] bpf, x64: Enable unprivlidged jit in vmalloc Rick Edgecombe
2019-02-06 0:35 ` [RFC PATCH 0/4] Initial support for allocating BPF JITs in vmalloc for x86 Alexei Starovoitov
2019-02-06 1:11 ` Edgecombe, Rick P
2019-02-06 1:40 ` Alexei Starovoitov
2019-02-06 15:38 ` Daniel Borkmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190205225103.28296-4-rick.p.edgecombe@intel.com \
--to=rick.p.edgecombe@intel.com \
--cc=ard.biesheuvel@linaro.org \
--cc=ast@fb.com \
--cc=daniel@iogearbox.net \
--cc=dave.hansen@intel.com \
--cc=kristen@linux.intel.com \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox