diff options
Diffstat (limited to 'include/linux/bpf.h')
-rw-r--r-- | include/linux/bpf.h | 78 |
1 files changed, 68 insertions, 10 deletions
diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 4fb3aa2dc975..e5a309e6a400 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h | |||
@@ -66,6 +66,11 @@ struct bpf_map_ops { | |||
66 | u64 imm, u32 *off); | 66 | u64 imm, u32 *off); |
67 | }; | 67 | }; |
68 | 68 | ||
69 | struct bpf_map_memory { | ||
70 | u32 pages; | ||
71 | struct user_struct *user; | ||
72 | }; | ||
73 | |||
69 | struct bpf_map { | 74 | struct bpf_map { |
70 | /* The first two cachelines with read-mostly members of which some | 75 | /* The first two cachelines with read-mostly members of which some |
71 | * are also accessed in fast-path (e.g. ops, max_entries). | 76 | * are also accessed in fast-path (e.g. ops, max_entries). |
@@ -86,7 +91,7 @@ struct bpf_map { | |||
86 | u32 btf_key_type_id; | 91 | u32 btf_key_type_id; |
87 | u32 btf_value_type_id; | 92 | u32 btf_value_type_id; |
88 | struct btf *btf; | 93 | struct btf *btf; |
89 | u32 pages; | 94 | struct bpf_map_memory memory; |
90 | bool unpriv_array; | 95 | bool unpriv_array; |
91 | bool frozen; /* write-once */ | 96 | bool frozen; /* write-once */ |
92 | /* 48 bytes hole */ | 97 | /* 48 bytes hole */ |
@@ -94,8 +99,7 @@ struct bpf_map { | |||
94 | /* The 3rd and 4th cacheline with misc members to avoid false sharing | 99 | /* The 3rd and 4th cacheline with misc members to avoid false sharing |
95 | * particularly with refcounting. | 100 | * particularly with refcounting. |
96 | */ | 101 | */ |
97 | struct user_struct *user ____cacheline_aligned; | 102 | atomic_t refcnt ____cacheline_aligned; |
98 | atomic_t refcnt; | ||
99 | atomic_t usercnt; | 103 | atomic_t usercnt; |
100 | struct work_struct work; | 104 | struct work_struct work; |
101 | char name[BPF_OBJ_NAME_LEN]; | 105 | char name[BPF_OBJ_NAME_LEN]; |
@@ -370,6 +374,7 @@ struct bpf_prog_aux { | |||
370 | u32 id; | 374 | u32 id; |
371 | u32 func_cnt; /* used by non-func prog as the number of func progs */ | 375 | u32 func_cnt; /* used by non-func prog as the number of func progs */ |
372 | u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */ | 376 | u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */ |
377 | bool verifier_zext; /* Zero extensions has been inserted by verifier. */ | ||
373 | bool offload_requested; | 378 | bool offload_requested; |
374 | struct bpf_prog **func; | 379 | struct bpf_prog **func; |
375 | void *jit_data; /* JIT specific data. arch dependent */ | 380 | void *jit_data; /* JIT specific data. arch dependent */ |
@@ -513,17 +518,17 @@ struct bpf_prog_array { | |||
513 | }; | 518 | }; |
514 | 519 | ||
515 | struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags); | 520 | struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags); |
516 | void bpf_prog_array_free(struct bpf_prog_array __rcu *progs); | 521 | void bpf_prog_array_free(struct bpf_prog_array *progs); |
517 | int bpf_prog_array_length(struct bpf_prog_array __rcu *progs); | 522 | int bpf_prog_array_length(struct bpf_prog_array *progs); |
518 | int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *progs, | 523 | int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs, |
519 | __u32 __user *prog_ids, u32 cnt); | 524 | __u32 __user *prog_ids, u32 cnt); |
520 | 525 | ||
521 | void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *progs, | 526 | void bpf_prog_array_delete_safe(struct bpf_prog_array *progs, |
522 | struct bpf_prog *old_prog); | 527 | struct bpf_prog *old_prog); |
523 | int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array, | 528 | int bpf_prog_array_copy_info(struct bpf_prog_array *array, |
524 | u32 *prog_ids, u32 request_cnt, | 529 | u32 *prog_ids, u32 request_cnt, |
525 | u32 *prog_cnt); | 530 | u32 *prog_cnt); |
526 | int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array, | 531 | int bpf_prog_array_copy(struct bpf_prog_array *old_array, |
527 | struct bpf_prog *exclude_prog, | 532 | struct bpf_prog *exclude_prog, |
528 | struct bpf_prog *include_prog, | 533 | struct bpf_prog *include_prog, |
529 | struct bpf_prog_array **new_array); | 534 | struct bpf_prog_array **new_array); |
@@ -551,6 +556,56 @@ _out: \ | |||
551 | _ret; \ | 556 | _ret; \ |
552 | }) | 557 | }) |
553 | 558 | ||
559 | /* To be used by __cgroup_bpf_run_filter_skb for EGRESS BPF progs | ||
560 | * so BPF programs can request cwr for TCP packets. | ||
561 | * | ||
562 | * Current cgroup skb programs can only return 0 or 1 (0 to drop the | ||
563 | * packet. This macro changes the behavior so the low order bit | ||
564 | * indicates whether the packet should be dropped (0) or not (1) | ||
565 | * and the next bit is a congestion notification bit. This could be | ||
566 | * used by TCP to call tcp_enter_cwr() | ||
567 | * | ||
568 | * Hence, new allowed return values of CGROUP EGRESS BPF programs are: | ||
569 | * 0: drop packet | ||
570 | * 1: keep packet | ||
571 | * 2: drop packet and cn | ||
572 | * 3: keep packet and cn | ||
573 | * | ||
574 | * This macro then converts it to one of the NET_XMIT or an error | ||
575 | * code that is then interpreted as drop packet (and no cn): | ||
576 | * 0: NET_XMIT_SUCCESS skb should be transmitted | ||
577 | * 1: NET_XMIT_DROP skb should be dropped and cn | ||
578 | * 2: NET_XMIT_CN skb should be transmitted and cn | ||
579 | * 3: -EPERM skb should be dropped | ||
580 | */ | ||
581 | #define BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(array, ctx, func) \ | ||
582 | ({ \ | ||
583 | struct bpf_prog_array_item *_item; \ | ||
584 | struct bpf_prog *_prog; \ | ||
585 | struct bpf_prog_array *_array; \ | ||
586 | u32 ret; \ | ||
587 | u32 _ret = 1; \ | ||
588 | u32 _cn = 0; \ | ||
589 | preempt_disable(); \ | ||
590 | rcu_read_lock(); \ | ||
591 | _array = rcu_dereference(array); \ | ||
592 | _item = &_array->items[0]; \ | ||
593 | while ((_prog = READ_ONCE(_item->prog))) { \ | ||
594 | bpf_cgroup_storage_set(_item->cgroup_storage); \ | ||
595 | ret = func(_prog, ctx); \ | ||
596 | _ret &= (ret & 1); \ | ||
597 | _cn |= (ret & 2); \ | ||
598 | _item++; \ | ||
599 | } \ | ||
600 | rcu_read_unlock(); \ | ||
601 | preempt_enable(); \ | ||
602 | if (_ret) \ | ||
603 | _ret = (_cn ? NET_XMIT_CN : NET_XMIT_SUCCESS); \ | ||
604 | else \ | ||
605 | _ret = (_cn ? NET_XMIT_DROP : -EPERM); \ | ||
606 | _ret; \ | ||
607 | }) | ||
608 | |||
554 | #define BPF_PROG_RUN_ARRAY(array, ctx, func) \ | 609 | #define BPF_PROG_RUN_ARRAY(array, ctx, func) \ |
555 | __BPF_PROG_RUN_ARRAY(array, ctx, func, false) | 610 | __BPF_PROG_RUN_ARRAY(array, ctx, func, false) |
556 | 611 | ||
@@ -595,9 +650,12 @@ struct bpf_map *__bpf_map_get(struct fd f); | |||
595 | struct bpf_map * __must_check bpf_map_inc(struct bpf_map *map, bool uref); | 650 | struct bpf_map * __must_check bpf_map_inc(struct bpf_map *map, bool uref); |
596 | void bpf_map_put_with_uref(struct bpf_map *map); | 651 | void bpf_map_put_with_uref(struct bpf_map *map); |
597 | void bpf_map_put(struct bpf_map *map); | 652 | void bpf_map_put(struct bpf_map *map); |
598 | int bpf_map_precharge_memlock(u32 pages); | ||
599 | int bpf_map_charge_memlock(struct bpf_map *map, u32 pages); | 653 | int bpf_map_charge_memlock(struct bpf_map *map, u32 pages); |
600 | void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages); | 654 | void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages); |
655 | int bpf_map_charge_init(struct bpf_map_memory *mem, size_t size); | ||
656 | void bpf_map_charge_finish(struct bpf_map_memory *mem); | ||
657 | void bpf_map_charge_move(struct bpf_map_memory *dst, | ||
658 | struct bpf_map_memory *src); | ||
601 | void *bpf_map_area_alloc(size_t size, int numa_node); | 659 | void *bpf_map_area_alloc(size_t size, int numa_node); |
602 | void bpf_map_area_free(void *base); | 660 | void bpf_map_area_free(void *base); |
603 | void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr); | 661 | void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr); |