Skip to contentSkip to conversation

Fix up bpf_jit_limit some more

PATCH Applied Lorenz Bauer 3 participants 6 messages Oct 14, 2021 – Oct 21, 2021

Subsystems (9)
lore
Share & tools
lore
#0 lore raw

v3 cover Fix up bpf_jit_limit some more

Fix some inconsistencies of bpf_jit_limit on non-x86 platforms. I've dropped exposing bpf_jit_current since we couldn't agree on file modes, correct names, etc.

Lorenz Bauer (3):
bpf: define bpf_jit_alloc_exec_limit for riscv JIT
bpf: define bpf_jit_alloc_exec_limit for arm64 JIT
bpf: prevent increasing bpf_jit_limit above max

 arch/arm64/net/bpf_jit_comp.c | 5 +++++
 arch/riscv/net/bpf_jit_core.c | 5 +++++
 include/linux/filter.h        | 1 +
 kernel/bpf/core.c             | 4 +++-
 net/core/sysctl_net_core.c    | 2 +-
 5 files changed, 15 insertions(+), 2 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v3 1/3 bpf: define bpf_jit_alloc_exec_limit for riscv JIT

Expose the maximum amount of useable memory from the riscv JIT.

---
 arch/riscv/net/bpf_jit_core.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/riscv/net/bpf_jit_core.c +5 -0
index fed86f42dfbe..0fee2cbaaf53 100644
@@ -166,6 +166,11 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
 	return prog;
 }
 
+u64 bpf_jit_alloc_exec_limit(void)
+{
+	return BPF_JIT_REGION_SIZE;
+}
+
 void *bpf_jit_alloc_exec(unsigned long size)
 {
 	return __vmalloc_node_range(size, PAGE_SIZE, BPF_JIT_REGION_START,
Signature · 1 line
2.30.2
Acked-by on v3 1/3
#2 lore raw

Re: [PATCH v3 1/3] bpf: define bpf_jit_alloc_exec_limit for riscv JIT

Quoted 5 lines · Lorenz Bauer

Expose the maximum amount of useable memory from the riscv JIT.

Quoted 23 lines
---
 arch/riscv/net/bpf_jit_core.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/riscv/net/bpf_jit_core.c +5 -1
index fed86f42dfbe..0fee2cbaaf53 100644
@@ -166,6 +166,11 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
        return prog;
 }
 
+u64 bpf_jit_alloc_exec_limit(void)
+{
+       return BPF_JIT_REGION_SIZE;
+}
+
 void *bpf_jit_alloc_exec(unsigned long size)
 {
        return __vmalloc_node_range(size, PAGE_SIZE, BPF_JIT_REGION_START,
--

2.30.2

#3 lore raw

PATCH v3 2/3 bpf: define bpf_jit_alloc_exec_limit for arm64 JIT

In reply to Lorenz Bauer

Expose the maximum amount of useable memory from the arm64 JIT.

---
 arch/arm64/net/bpf_jit_comp.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm64/net/bpf_jit_comp.c +5 -0
index 41c23f474ea6..803e7773fa86 100644
@@ -1136,6 +1136,11 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
 	return prog;
 }
 
+u64 bpf_jit_alloc_exec_limit(void)
+{
+	return BPF_JIT_REGION_SIZE;
+}
+
 void *bpf_jit_alloc_exec(unsigned long size)
 {
 	return __vmalloc_node_range(size, PAGE_SIZE, BPF_JIT_REGION_START,
Signature · 1 line
2.30.2
#4 lore raw

PATCH v3 3/3 bpf: prevent increasing bpf_jit_limit above max

In reply to Lorenz Bauer

Restrict bpf_jit_limit to the maximum supported by the arch's JIT.

---
 include/linux/filter.h     | 1 +
 kernel/bpf/core.c          | 4 +++-
 net/core/sysctl_net_core.c | 2 +-
 3 files changed, 5 insertions(+), 2 deletions(-)
include/linux/filter.h +1 -0
index 47f80adbe744..8231a6a257f6 100644
@@ -1050,6 +1050,7 @@ extern int bpf_jit_enable;
 extern int bpf_jit_harden;
 extern int bpf_jit_kallsyms;
 extern long bpf_jit_limit;
+extern long bpf_jit_limit_max;
 
 typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
 
kernel/bpf/core.c +3 -1
index b6c72af64d5d..ab84b3816339 100644
@@ -525,6 +525,7 @@ int bpf_jit_enable   __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON);
 int bpf_jit_kallsyms __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON);
 int bpf_jit_harden   __read_mostly;
 long bpf_jit_limit   __read_mostly;
+long bpf_jit_limit_max __read_mostly;
 
 static void
 bpf_prog_ksym_set_addr(struct bpf_prog *prog)
@@ -818,7 +819,8 @@ u64 __weak bpf_jit_alloc_exec_limit(void)
 static int __init bpf_jit_charge_init(void)
 {
 	/* Only used as heuristic here to derive limit. */
-	bpf_jit_limit = min_t(u64, round_up(bpf_jit_alloc_exec_limit() >> 2,
+	bpf_jit_limit_max = bpf_jit_alloc_exec_limit();
+	bpf_jit_limit = min_t(u64, round_up(bpf_jit_limit_max >> 2,
 					    PAGE_SIZE), LONG_MAX);
 	return 0;
 }
net/core/sysctl_net_core.c +1 -1
index c8496c1142c9..5f88526ad61c 100644
@@ -419,7 +419,7 @@ static struct ctl_table net_core_table[] = {
 		.mode		= 0600,
 		.proc_handler	= proc_dolongvec_minmax_bpf_restricted,
 		.extra1		= &long_one,
-		.extra2		= &long_max,
+		.extra2		= &bpf_jit_limit_max,
 	},
 #endif
 	{
Signature · 1 line
2.30.2
#5 lore raw
In reply to Lorenz Bauer
Quoted 4 lines · Lorenz Bauer

Fix some inconsistencies of bpf_jit_limit on non-x86 platforms. I've dropped exposing bpf_jit_current since we couldn't agree on file modes, correct names, etc.

Applied to bpf tree. Seems more appropriate there.