Skip to content

Commit 8f577ca

Browse files
Alexei Starovoitovdavem330
authored andcommitted
seccomp: JIT compile seccomp filter
Take advantage of internal BPF JIT 05-sim-long_jumps.c of libseccomp was used as micro-benchmark: seccomp_rule_add_exact(ctx,... seccomp_rule_add_exact(ctx,... rc = seccomp_load(ctx); for (i = 0; i < 10000000; i++) syscall(...); $ sudo sysctl net.core.bpf_jit_enable=1 $ time ./bench real 0m2.769s user 0m1.136s sys 0m1.624s $ sudo sysctl net.core.bpf_jit_enable=0 $ time ./bench real 0m5.825s user 0m1.268s sys 0m4.548s Signed-off-by: Alexei Starovoitov <ast@plumgrid.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 6225827 commit 8f577ca

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

‎kernel/seccomp.c‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@
5454
struct seccomp_filter {
5555
atomic_t usage;
5656
struct seccomp_filter *prev;
57-
unsigned short len; /* Instruction count */
58-
struct sock_filter_int insnsi[];
57+
struct sk_filter *prog;
5958
};
6059

6160
/* Limit any path through the tree to 256KB worth of instructions. */
@@ -189,7 +188,8 @@ static u32 seccomp_run_filters(int syscall)
189188
* value always takes priority (ignoring the DATA).
190189
*/
191190
for (f = current->seccomp.filter; f; f = f->prev) {
192-
u32 cur_ret = sk_run_filter_int_seccomp(&sd, f->insnsi);
191+
u32 cur_ret = SK_RUN_FILTER(f->prog, (void *)&sd);
192+
193193
if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
194194
ret = cur_ret;
195195
}
@@ -215,7 +215,7 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
215215
return -EINVAL;
216216

217217
for (filter = current->seccomp.filter; filter; filter = filter->prev)
218-
total_insns += filter->len + 4; /* include a 4 instr penalty */
218+
total_insns += filter->prog->len + 4; /* include a 4 instr penalty */
219219
if (total_insns > MAX_INSNS_PER_PATH)
220220
return -ENOMEM;
221221

@@ -256,19 +256,27 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
256256

257257
/* Allocate a new seccomp_filter */
258258
ret = -ENOMEM;
259-
filter = kzalloc(sizeof(struct seccomp_filter) +
260-
sizeof(struct sock_filter_int) * new_len,
259+
filter = kzalloc(sizeof(struct seccomp_filter),
261260
GFP_KERNEL|__GFP_NOWARN);
262261
if (!filter)
263262
goto free_prog;
264263

265-
ret = sk_convert_filter(fp, fprog->len, filter->insnsi, &new_len);
266-
if (ret)
264+
filter->prog = kzalloc(sk_filter_size(new_len),
265+
GFP_KERNEL|__GFP_NOWARN);
266+
if (!filter->prog)
267267
goto free_filter;
268+
269+
ret = sk_convert_filter(fp, fprog->len, filter->prog->insnsi, &new_len);
270+
if (ret)
271+
goto free_filter_prog;
268272
kfree(fp);
269273

270274
atomic_set(&filter->usage, 1);
271-
filter->len = new_len;
275+
filter->prog->len = new_len;
276+
filter->prog->bpf_func = (void *)sk_run_filter_int_seccomp;
277+
278+
/* JIT internal BPF into native HW instructions */
279+
bpf_int_jit_compile(filter->prog);
272280

273281
/*
274282
* If there is an existing filter, make it the prev and don't drop its
@@ -278,6 +286,8 @@ static long seccomp_attach_filter(struct sock_fprog *fprog)
278286
current->seccomp.filter = filter;
279287
return 0;
280288

289+
free_filter_prog:
290+
kfree(filter->prog);
281291
free_filter:
282292
kfree(filter);
283293
free_prog:
@@ -330,6 +340,7 @@ void put_seccomp_filter(struct task_struct *tsk)
330340
while (orig && atomic_dec_and_test(&orig->usage)) {
331341
struct seccomp_filter *freeme = orig;
332342
orig = orig->prev;
343+
bpf_jit_free(freeme->prog);
333344
kfree(freeme);
334345
}
335346
}

0 commit comments

Comments
 (0)