summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexei Starovoitov <ast@plumgrid.com>2014-09-26 03:17:00 -0400
committerDavid S. Miller <davem@davemloft.net>2014-09-26 15:05:14 -0400
commit09756af46893c18839062976c3252e93a1beeba7 (patch)
tree203642a5473496ecb6ff10cd22dba39b22ed5f0a
parentdb20fd2b01087bdfbe30bce314a198eefedcc42e (diff)
bpf: expand BPF syscall with program load/unload
eBPF programs are similar to kernel modules. They are loaded by the user process and automatically unloaded when process exits. Each eBPF program is a safe run-to-completion set of instructions. eBPF verifier statically determines that the program terminates and is safe to execute. The following syscall wrapper can be used to load the program: int bpf_prog_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns, int insn_cnt, const char *license) { union bpf_attr attr = { .prog_type = prog_type, .insns = ptr_to_u64(insns), .insn_cnt = insn_cnt, .license = ptr_to_u64(license), }; return bpf(BPF_PROG_LOAD, &attr, sizeof(attr)); } where 'insns' is an array of eBPF instructions and 'license' is a string that must be GPL compatible to call helper functions marked gpl_only Upon succesful load the syscall returns prog_fd. Use close(prog_fd) to unload the program. User space tests and examples follow in the later patches Signed-off-by: Alexei Starovoitov <ast@plumgrid.com> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--include/linux/bpf.h38
-rw-r--r--include/linux/filter.h8
-rw-r--r--include/uapi/linux/bpf.h26
-rw-r--r--kernel/bpf/core.c29
-rw-r--r--kernel/bpf/syscall.c165
5 files changed, 246 insertions, 20 deletions
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 2887f3f9da59..92979182be81 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -46,4 +46,42 @@ void bpf_register_map_type(struct bpf_map_type_list *tl);
46void bpf_map_put(struct bpf_map *map); 46void bpf_map_put(struct bpf_map *map);
47struct bpf_map *bpf_map_get(struct fd f); 47struct bpf_map *bpf_map_get(struct fd f);
48 48
49/* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
50 * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
51 * instructions after verifying
52 */
53struct bpf_func_proto {
54 u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
55 bool gpl_only;
56};
57
58struct bpf_verifier_ops {
59 /* return eBPF function prototype for verification */
60 const struct bpf_func_proto *(*get_func_proto)(enum bpf_func_id func_id);
61};
62
63struct bpf_prog_type_list {
64 struct list_head list_node;
65 struct bpf_verifier_ops *ops;
66 enum bpf_prog_type type;
67};
68
69void bpf_register_prog_type(struct bpf_prog_type_list *tl);
70
71struct bpf_prog;
72
73struct bpf_prog_aux {
74 atomic_t refcnt;
75 bool is_gpl_compatible;
76 enum bpf_prog_type prog_type;
77 struct bpf_verifier_ops *ops;
78 struct bpf_map **used_maps;
79 u32 used_map_cnt;
80 struct bpf_prog *prog;
81 struct work_struct work;
82};
83
84void bpf_prog_put(struct bpf_prog *prog);
85struct bpf_prog *bpf_prog_get(u32 ufd);
86
49#endif /* _LINUX_BPF_H */ 87#endif /* _LINUX_BPF_H */
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 1a0bc6d134d7..4ffc0958d85e 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -21,6 +21,7 @@
21struct sk_buff; 21struct sk_buff;
22struct sock; 22struct sock;
23struct seccomp_data; 23struct seccomp_data;
24struct bpf_prog_aux;
24 25
25/* ArgX, context and stack frame pointer register positions. Note, 26/* ArgX, context and stack frame pointer register positions. Note,
26 * Arg1, Arg2, Arg3, etc are used as argument mappings of function 27 * Arg1, Arg2, Arg3, etc are used as argument mappings of function
@@ -300,17 +301,12 @@ struct bpf_binary_header {
300 u8 image[]; 301 u8 image[];
301}; 302};
302 303
303struct bpf_work_struct {
304 struct bpf_prog *prog;
305 struct work_struct work;
306};
307
308struct bpf_prog { 304struct bpf_prog {
309 u16 pages; /* Number of allocated pages */ 305 u16 pages; /* Number of allocated pages */
310 bool jited; /* Is our filter JIT'ed? */ 306 bool jited; /* Is our filter JIT'ed? */
311 u32 len; /* Number of filter blocks */ 307 u32 len; /* Number of filter blocks */
312 struct sock_fprog_kern *orig_prog; /* Original BPF program */ 308 struct sock_fprog_kern *orig_prog; /* Original BPF program */
313 struct bpf_work_struct *work; /* Deferred free work struct */ 309 struct bpf_prog_aux *aux; /* Auxiliary fields */
314 unsigned int (*bpf_func)(const struct sk_buff *skb, 310 unsigned int (*bpf_func)(const struct sk_buff *skb,
315 const struct bpf_insn *filter); 311 const struct bpf_insn *filter);
316 /* Instructions for interpreter */ 312 /* Instructions for interpreter */
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 395cabd2ca0a..424f442016e7 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -99,12 +99,23 @@ enum bpf_cmd {
99 * returns zero and stores next key or negative error 99 * returns zero and stores next key or negative error
100 */ 100 */
101 BPF_MAP_GET_NEXT_KEY, 101 BPF_MAP_GET_NEXT_KEY,
102
103 /* verify and load eBPF program
104 * prog_fd = bpf(BPF_PROG_LOAD, union bpf_attr *attr, u32 size)
105 * Using attr->prog_type, attr->insns, attr->license
106 * returns fd or negative error
107 */
108 BPF_PROG_LOAD,
102}; 109};
103 110
104enum bpf_map_type { 111enum bpf_map_type {
105 BPF_MAP_TYPE_UNSPEC, 112 BPF_MAP_TYPE_UNSPEC,
106}; 113};
107 114
115enum bpf_prog_type {
116 BPF_PROG_TYPE_UNSPEC,
117};
118
108union bpf_attr { 119union bpf_attr {
109 struct { /* anonymous struct used by BPF_MAP_CREATE command */ 120 struct { /* anonymous struct used by BPF_MAP_CREATE command */
110 __u32 map_type; /* one of enum bpf_map_type */ 121 __u32 map_type; /* one of enum bpf_map_type */
@@ -121,6 +132,21 @@ union bpf_attr {
121 __aligned_u64 next_key; 132 __aligned_u64 next_key;
122 }; 133 };
123 }; 134 };
135
136 struct { /* anonymous struct used by BPF_PROG_LOAD command */
137 __u32 prog_type; /* one of enum bpf_prog_type */
138 __u32 insn_cnt;
139 __aligned_u64 insns;
140 __aligned_u64 license;
141 };
124} __attribute__((aligned(8))); 142} __attribute__((aligned(8)));
125 143
144/* integer value in 'imm' field of BPF_CALL instruction selects which helper
145 * function eBPF program intends to call
146 */
147enum bpf_func_id {
148 BPF_FUNC_unspec,
149 __BPF_FUNC_MAX_ID,
150};
151
126#endif /* _UAPI__LINUX_BPF_H__ */ 152#endif /* _UAPI__LINUX_BPF_H__ */
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 8b7002488251..f0c30c59b317 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -27,6 +27,7 @@
27#include <linux/random.h> 27#include <linux/random.h>
28#include <linux/moduleloader.h> 28#include <linux/moduleloader.h>
29#include <asm/unaligned.h> 29#include <asm/unaligned.h>
30#include <linux/bpf.h>
30 31
31/* Registers */ 32/* Registers */
32#define BPF_R0 regs[BPF_REG_0] 33#define BPF_R0 regs[BPF_REG_0]
@@ -71,7 +72,7 @@ struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
71{ 72{
72 gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO | 73 gfp_t gfp_flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO |
73 gfp_extra_flags; 74 gfp_extra_flags;
74 struct bpf_work_struct *ws; 75 struct bpf_prog_aux *aux;
75 struct bpf_prog *fp; 76 struct bpf_prog *fp;
76 77
77 size = round_up(size, PAGE_SIZE); 78 size = round_up(size, PAGE_SIZE);
@@ -79,14 +80,14 @@ struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags)
79 if (fp == NULL) 80 if (fp == NULL)
80 return NULL; 81 return NULL;
81 82
82 ws = kmalloc(sizeof(*ws), GFP_KERNEL | gfp_extra_flags); 83 aux = kzalloc(sizeof(*aux), GFP_KERNEL | gfp_extra_flags);
83 if (ws == NULL) { 84 if (aux == NULL) {
84 vfree(fp); 85 vfree(fp);
85 return NULL; 86 return NULL;
86 } 87 }
87 88
88 fp->pages = size / PAGE_SIZE; 89 fp->pages = size / PAGE_SIZE;
89 fp->work = ws; 90 fp->aux = aux;
90 91
91 return fp; 92 return fp;
92} 93}
@@ -110,10 +111,10 @@ struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
110 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE); 111 memcpy(fp, fp_old, fp_old->pages * PAGE_SIZE);
111 fp->pages = size / PAGE_SIZE; 112 fp->pages = size / PAGE_SIZE;
112 113
113 /* We keep fp->work from fp_old around in the new 114 /* We keep fp->aux from fp_old around in the new
114 * reallocated structure. 115 * reallocated structure.
115 */ 116 */
116 fp_old->work = NULL; 117 fp_old->aux = NULL;
117 __bpf_prog_free(fp_old); 118 __bpf_prog_free(fp_old);
118 } 119 }
119 120
@@ -123,7 +124,7 @@ EXPORT_SYMBOL_GPL(bpf_prog_realloc);
123 124
124void __bpf_prog_free(struct bpf_prog *fp) 125void __bpf_prog_free(struct bpf_prog *fp)
125{ 126{
126 kfree(fp->work); 127 kfree(fp->aux);
127 vfree(fp); 128 vfree(fp);
128} 129}
129EXPORT_SYMBOL_GPL(__bpf_prog_free); 130EXPORT_SYMBOL_GPL(__bpf_prog_free);
@@ -638,19 +639,19 @@ EXPORT_SYMBOL_GPL(bpf_prog_select_runtime);
638 639
639static void bpf_prog_free_deferred(struct work_struct *work) 640static void bpf_prog_free_deferred(struct work_struct *work)
640{ 641{
641 struct bpf_work_struct *ws; 642 struct bpf_prog_aux *aux;
642 643
643 ws = container_of(work, struct bpf_work_struct, work); 644 aux = container_of(work, struct bpf_prog_aux, work);
644 bpf_jit_free(ws->prog); 645 bpf_jit_free(aux->prog);
645} 646}
646 647
647/* Free internal BPF program */ 648/* Free internal BPF program */
648void bpf_prog_free(struct bpf_prog *fp) 649void bpf_prog_free(struct bpf_prog *fp)
649{ 650{
650 struct bpf_work_struct *ws = fp->work; 651 struct bpf_prog_aux *aux = fp->aux;
651 652
652 INIT_WORK(&ws->work, bpf_prog_free_deferred); 653 INIT_WORK(&aux->work, bpf_prog_free_deferred);
653 ws->prog = fp; 654 aux->prog = fp;
654 schedule_work(&ws->work); 655 schedule_work(&aux->work);
655} 656}
656EXPORT_SYMBOL_GPL(bpf_prog_free); 657EXPORT_SYMBOL_GPL(bpf_prog_free);
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index f94349ecaf61..0afb4eaa1887 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -14,6 +14,8 @@
14#include <linux/slab.h> 14#include <linux/slab.h>
15#include <linux/anon_inodes.h> 15#include <linux/anon_inodes.h>
16#include <linux/file.h> 16#include <linux/file.h>
17#include <linux/license.h>
18#include <linux/filter.h>
17 19
18static LIST_HEAD(bpf_map_types); 20static LIST_HEAD(bpf_map_types);
19 21
@@ -334,6 +336,166 @@ err_put:
334 return err; 336 return err;
335} 337}
336 338
339static LIST_HEAD(bpf_prog_types);
340
341static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
342{
343 struct bpf_prog_type_list *tl;
344
345 list_for_each_entry(tl, &bpf_prog_types, list_node) {
346 if (tl->type == type) {
347 prog->aux->ops = tl->ops;
348 prog->aux->prog_type = type;
349 return 0;
350 }
351 }
352 return -EINVAL;
353}
354
355void bpf_register_prog_type(struct bpf_prog_type_list *tl)
356{
357 list_add(&tl->list_node, &bpf_prog_types);
358}
359
360/* drop refcnt on maps used by eBPF program and free auxilary data */
361static void free_used_maps(struct bpf_prog_aux *aux)
362{
363 int i;
364
365 for (i = 0; i < aux->used_map_cnt; i++)
366 bpf_map_put(aux->used_maps[i]);
367
368 kfree(aux->used_maps);
369}
370
371void bpf_prog_put(struct bpf_prog *prog)
372{
373 if (atomic_dec_and_test(&prog->aux->refcnt)) {
374 free_used_maps(prog->aux);
375 bpf_prog_free(prog);
376 }
377}
378
379static int bpf_prog_release(struct inode *inode, struct file *filp)
380{
381 struct bpf_prog *prog = filp->private_data;
382
383 bpf_prog_put(prog);
384 return 0;
385}
386
387static const struct file_operations bpf_prog_fops = {
388 .release = bpf_prog_release,
389};
390
391static struct bpf_prog *get_prog(struct fd f)
392{
393 struct bpf_prog *prog;
394
395 if (!f.file)
396 return ERR_PTR(-EBADF);
397
398 if (f.file->f_op != &bpf_prog_fops) {
399 fdput(f);
400 return ERR_PTR(-EINVAL);
401 }
402
403 prog = f.file->private_data;
404
405 return prog;
406}
407
408/* called by sockets/tracing/seccomp before attaching program to an event
409 * pairs with bpf_prog_put()
410 */
411struct bpf_prog *bpf_prog_get(u32 ufd)
412{
413 struct fd f = fdget(ufd);
414 struct bpf_prog *prog;
415
416 prog = get_prog(f);
417
418 if (IS_ERR(prog))
419 return prog;
420
421 atomic_inc(&prog->aux->refcnt);
422 fdput(f);
423 return prog;
424}
425
426/* last field in 'union bpf_attr' used by this command */
427#define BPF_PROG_LOAD_LAST_FIELD license
428
429static int bpf_prog_load(union bpf_attr *attr)
430{
431 enum bpf_prog_type type = attr->prog_type;
432 struct bpf_prog *prog;
433 int err;
434 char license[128];
435 bool is_gpl;
436
437 if (CHECK_ATTR(BPF_PROG_LOAD))
438 return -EINVAL;
439
440 /* copy eBPF program license from user space */
441 if (strncpy_from_user(license, u64_to_ptr(attr->license),
442 sizeof(license) - 1) < 0)
443 return -EFAULT;
444 license[sizeof(license) - 1] = 0;
445
446 /* eBPF programs must be GPL compatible to use GPL-ed functions */
447 is_gpl = license_is_gpl_compatible(license);
448
449 if (attr->insn_cnt >= BPF_MAXINSNS)
450 return -EINVAL;
451
452 /* plain bpf_prog allocation */
453 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
454 if (!prog)
455 return -ENOMEM;
456
457 prog->len = attr->insn_cnt;
458
459 err = -EFAULT;
460 if (copy_from_user(prog->insns, u64_to_ptr(attr->insns),
461 prog->len * sizeof(struct bpf_insn)) != 0)
462 goto free_prog;
463
464 prog->orig_prog = NULL;
465 prog->jited = false;
466
467 atomic_set(&prog->aux->refcnt, 1);
468 prog->aux->is_gpl_compatible = is_gpl;
469
470 /* find program type: socket_filter vs tracing_filter */
471 err = find_prog_type(type, prog);
472 if (err < 0)
473 goto free_prog;
474
475 /* run eBPF verifier */
476 /* err = bpf_check(prog, tb); */
477
478 if (err < 0)
479 goto free_used_maps;
480
481 /* eBPF program is ready to be JITed */
482 bpf_prog_select_runtime(prog);
483
484 err = anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, O_RDWR | O_CLOEXEC);
485
486 if (err < 0)
487 /* failed to allocate fd */
488 goto free_used_maps;
489
490 return err;
491
492free_used_maps:
493 free_used_maps(prog->aux);
494free_prog:
495 bpf_prog_free(prog);
496 return err;
497}
498
337SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) 499SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
338{ 500{
339 union bpf_attr attr = {}; 501 union bpf_attr attr = {};
@@ -395,6 +557,9 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
395 case BPF_MAP_GET_NEXT_KEY: 557 case BPF_MAP_GET_NEXT_KEY:
396 err = map_get_next_key(&attr); 558 err = map_get_next_key(&attr);
397 break; 559 break;
560 case BPF_PROG_LOAD:
561 err = bpf_prog_load(&attr);
562 break;
398 default: 563 default:
399 err = -EINVAL; 564 err = -EINVAL;
400 break; 565 break;