History

bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS

The patch introduces BPF_MAP_TYPE_STRUCT_OPS. The map value is a kernel struct with its func ptr implemented in bpf prog. This new map is the interface to register/unregister/introspect a bpf implemented kernel struct. The kernel struct is actually embedded inside another new struct (or called the "value" struct in the code). For example, "struct tcp_congestion_ops" is embbeded in: struct bpf_struct_ops_tcp_congestion_ops { refcount_t refcnt; enum bpf_struct_ops_state state; struct tcp_congestion_ops data; /* <-- kernel subsystem struct here */ } The map value is "struct bpf_struct_ops_tcp_congestion_ops". The "bpftool map dump" will then be able to show the state ("inuse"/"tobefree") and the number of subsystem's refcnt (e.g. number of tcp_sock in the tcp_congestion_ops case). This "value" struct is created automatically by a macro. Having a separate "value" struct will also make extending "struct bpf_struct_ops_XYZ" easier (e.g. adding "void (*init)(void)" to "struct bpf_struct_ops_XYZ" to do some initialization works before registering the struct_ops to the kernel subsystem). The libbpf will take care of finding and populating the "struct bpf_struct_ops_XYZ" from "struct XYZ". Register a struct_ops to a kernel subsystem: 1. Load all needed BPF_PROG_TYPE_STRUCT_OPS prog(s) 2. Create a BPF_MAP_TYPE_STRUCT_OPS with attr->btf_vmlinux_value_type_id set to the btf id "struct bpf_struct_ops_tcp_congestion_ops" of the running kernel. Instead of reusing the attr->btf_value_type_id, btf_vmlinux_value_type_id s added such that attr->btf_fd can still be used as the "user" btf which could store other useful sysadmin/debug info that may be introduced in the furture, e.g. creation-date/compiler-details/map-creator...etc. 3. Create a "struct bpf_struct_ops_tcp_congestion_ops" object as described in the running kernel btf. Populate the value of this object. The function ptr should be populated with the prog fds. 4. Call BPF_MAP_UPDATE with the object created in (3) as the map value. The key is always "0". During BPF_MAP_UPDATE, the code that saves the kernel-func-ptr's args as an array of u64 is generated. BPF_MAP_UPDATE also allows the specific struct_ops to do some final checks in "st_ops->init_member()" (e.g. ensure all mandatory func ptrs are implemented). If everything looks good, it will register this kernel struct to the kernel subsystem. The map will not allow further update from this point. Unregister a struct_ops from the kernel subsystem: BPF_MAP_DELETE with key "0". Introspect a struct_ops: BPF_MAP_LOOKUP_ELEM with key "0". The map value returned will have the prog _id_ populated as the func ptr. The map value state (enum bpf_struct_ops_state) will transit from: INIT (map created) => INUSE (map updated, i.e. reg) => TOBEFREE (map value deleted, i.e. unreg) The kernel subsystem needs to call bpf_struct_ops_get() and bpf_struct_ops_put() to manage the "refcnt" in the "struct bpf_struct_ops_XYZ". This patch uses a separate refcnt for the purose of tracking the subsystem usage. Another approach is to reuse the map->refcnt and then "show" (i.e. during map_lookup) the subsystem's usage by doing map->refcnt - map->usercnt to filter out the map-fd/pinned-map usage. However, that will also tie down the future semantics of map->refcnt and map->usercnt. The very first subsystem's refcnt (during reg()) holds one count to map->refcnt. When the very last subsystem's refcnt is gone, it will also release the map->refcnt. All bpf_prog will be freed when the map->refcnt reaches 0 (i.e. during map_free()). Here is how the bpftool map command will look like: [root@arch-fb-vm1 bpf]# bpftool map show 6: struct_ops name dctcp flags 0x0 key 4B value 256B max_entries 1 memlock 4096B btf_id 6 [root@arch-fb-vm1 bpf]# bpftool map dump id 6 [{ "value": { "refcnt": { "refs": { "counter": 1 } }, "state": 1, "data": { "list": { "next": 0, "prev": 0 }, "key": 0, "flags": 2, "init": 24, "release": 0, "ssthresh": 25, "cong_avoid": 30, "set_state": 27, "cwnd_event": 28, "in_ack_event": 26, "undo_cwnd": 29, "pkts_acked": 0, "min_tso_segs": 0, "sndbuf_expand": 0, "cong_control": 0, "get_info": 0, "name": [98,112,102,95,100,99,116,99,112,0,0,0,0,0,0,0 ], "owner": 0 } } } ] Misc Notes: * bpf_struct_ops_map_sys_lookup_elem() is added for syscall lookup. It does an inplace update on "*value" instead returning a pointer to syscall.c. Otherwise, it needs a separate copy of "zero" value for the BPF_STRUCT_OPS_STATE_INIT to avoid races. * The bpf_struct_ops_map_delete_elem() is also called without preempt_disable() from map_delete_elem(). It is because the "->unreg()" may requires sleepable context, e.g. the "tcp_unregister_congestion_control()". * "const" is added to some of the existing "struct btf_func_model *" function arg to avoid a compiler warning caused by this patch.

Martin KaFai Lau committed 6 years ago

Commit 85d33df1 parent 27ae79911 files · +642 −47Browse files

Showing the changes to include/linux (3 of 11 files). Show all

Unified · Split

3 files
  1. include/linux/bpf.h +47 −2
  2. include/linux/bpf_types.h +3 −0
  3. include/linux/btf.h +13 −0
include/linux/bpf.h+47−2
@@ -17,6 +17,7 @@ · 16 unmodified lines
17#include <linux/u64_stats_sync.h>17#include <linux/u64_stats_sync.h>
18#include <linux/refcount.h>18#include <linux/refcount.h>
19#include <linux/mutex.h>19#include <linux/mutex.h>
20#include <linux/module.h>
20 21
21struct bpf_verifier_env;22struct bpf_verifier_env;
22struct bpf_verifier_log;23struct bpf_verifier_log;
@@ -106,6 +107,7 @@ struct bpf_map { · 83 unmodified lines
106 struct btf *btf;107 struct btf *btf;
107 struct bpf_map_memory memory;108 struct bpf_map_memory memory;
108 char name[BPF_OBJ_NAME_LEN];109 char name[BPF_OBJ_NAME_LEN];
110 u32 btf_vmlinux_value_type_id;
109 bool unpriv_array;111 bool unpriv_array;
110 bool frozen; /* write-once; write-protected by freeze_mutex */112 bool frozen; /* write-once; write-protected by freeze_mutex */
111 /* 22 bytes hole */113 /* 22 bytes hole */
@@ -183,7 +185,8 @@ static inline bool bpf_map_offload_neutral(const struct bpf_map *map) · 71 unmodified lines
183 185
184static inline bool bpf_map_support_seq_show(const struct bpf_map *map)186static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
185{187{
186 return map->btf && map->ops->map_seq_show_elem;188 return (map->btf_value_type_id || map->btf_vmlinux_value_type_id) &&
189 map->ops->map_seq_show_elem;
187}190}
188 191
189int map_check_no_btf(const struct bpf_map *map,192int map_check_no_btf(const struct bpf_map *map,
@@ -441,7 +444,8 @@ struct btf_func_model { · 251 unmodified lines
441 * fentry = a set of program to run before calling original function444 * fentry = a set of program to run before calling original function
442 * fexit = a set of program to run after original function445 * fexit = a set of program to run after original function
443 */446 */
444int arch_prepare_bpf_trampoline(void *image, struct btf_func_model *m, u32 flags,447int arch_prepare_bpf_trampoline(void *image, void *image_end,
448 const struct btf_func_model *m, u32 flags,
445 struct bpf_prog **fentry_progs, int fentry_cnt,449 struct bpf_prog **fentry_progs, int fentry_cnt,
446 struct bpf_prog **fexit_progs, int fexit_cnt,450 struct bpf_prog **fexit_progs, int fexit_cnt,
447 void *orig_call);451 void *orig_call);
@@ -672,6 +676,7 @@ struct bpf_array_aux { · 224 unmodified lines
672 struct work_struct work;676 struct work_struct work;
673};677};
674 678
679struct bpf_struct_ops_value;
675struct btf_type;680struct btf_type;
676struct btf_member;681struct btf_member;
677 682
@@ -681,21 +686,61 @@ struct bpf_struct_ops { · 3 unmodified lines
681 int (*init)(struct btf *btf);686 int (*init)(struct btf *btf);
682 int (*check_member)(const struct btf_type *t,687 int (*check_member)(const struct btf_type *t,
683 const struct btf_member *member);688 const struct btf_member *member);
689 int (*init_member)(const struct btf_type *t,
690 const struct btf_member *member,
691 void *kdata, const void *udata);
692 int (*reg)(void *kdata);
693 void (*unreg)(void *kdata);
684 const struct btf_type *type;694 const struct btf_type *type;
695 const struct btf_type *value_type;
685 const char *name;696 const char *name;
686 struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];697 struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
687 u32 type_id;698 u32 type_id;
699 u32 value_id;
688};700};
689 701
690#if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)702#if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
703#define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
691const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id);704const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id);
692void bpf_struct_ops_init(struct btf *btf);705void bpf_struct_ops_init(struct btf *btf);
706bool bpf_struct_ops_get(const void *kdata);
707void bpf_struct_ops_put(const void *kdata);
708int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
709 void *value);
710static inline bool bpf_try_module_get(const void *data, struct module *owner)
711{
712 if (owner == BPF_MODULE_OWNER)
713 return bpf_struct_ops_get(data);
714 else
715 return try_module_get(owner);
716}
717static inline void bpf_module_put(const void *data, struct module *owner)
718{
719 if (owner == BPF_MODULE_OWNER)
720 bpf_struct_ops_put(data);
721 else
722 module_put(owner);
723}
693#else724#else
694static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)725static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
695{726{
696 return NULL;727 return NULL;
697}728}
698static inline void bpf_struct_ops_init(struct btf *btf) { }729static inline void bpf_struct_ops_init(struct btf *btf) { }
730static inline bool bpf_try_module_get(const void *data, struct module *owner)
731{
732 return try_module_get(owner);
733}
734static inline void bpf_module_put(const void *data, struct module *owner)
735{
736 module_put(owner);
737}
738static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
739 void *key,
740 void *value)
741{
742 return -EINVAL;
743}
699#endif744#endif
700 745
701struct bpf_array {746struct bpf_array {
include/linux/bpf_types.h+3−0
@@ -109,3 +109,6 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, reuseport_array_ops) · 108 unmodified lines
109#endif109#endif
110BPF_MAP_TYPE(BPF_MAP_TYPE_QUEUE, queue_map_ops)110BPF_MAP_TYPE(BPF_MAP_TYPE_QUEUE, queue_map_ops)
111BPF_MAP_TYPE(BPF_MAP_TYPE_STACK, stack_map_ops)111BPF_MAP_TYPE(BPF_MAP_TYPE_STACK, stack_map_ops)
112#if defined(CONFIG_BPF_JIT)
113BPF_MAP_TYPE(BPF_MAP_TYPE_STRUCT_OPS, bpf_struct_ops_map_ops)
114#endif
include/linux/btf.h+13−0
@@ -7,6 +7,8 @@ · 6 unmodified lines
7#include <linux/types.h>7#include <linux/types.h>
8#include <uapi/linux/btf.h>8#include <uapi/linux/btf.h>
9 9
10#define BTF_TYPE_EMIT(type) ((void)(type *)0)
11
10struct btf;12struct btf;
11struct btf_member;13struct btf_member;
12struct btf_type;14struct btf_type;
@@ -60,6 +62,10 @@ const struct btf_type *btf_type_resolve_ptr(const struct btf *btf, · 47 unmodified lines
60 u32 id, u32 *res_id);62 u32 id, u32 *res_id);
61const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,63const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,
62 u32 id, u32 *res_id);64 u32 id, u32 *res_id);
65const struct btf_type *
66btf_resolve_size(const struct btf *btf, const struct btf_type *type,
67 u32 *type_size, const struct btf_type **elem_type,
68 u32 *total_nelems);
63 69
64#define for_each_member(i, struct_type, member) \70#define for_each_member(i, struct_type, member) \
65 for (i = 0, member = btf_type_member(struct_type); \71 for (i = 0, member = btf_type_member(struct_type); \
@@ -106,6 +112,13 @@ static inline bool btf_type_kflag(const struct btf_type *t) · 40 unmodified lines
106 return BTF_INFO_KFLAG(t->info);112 return BTF_INFO_KFLAG(t->info);
107}113}
108 114
115static inline u32 btf_member_bit_offset(const struct btf_type *struct_type,
116 const struct btf_member *member)
117{
118 return btf_type_kflag(struct_type) ? BTF_MEMBER_BIT_OFFSET(member->offset)
119 : member->offset;
120}
121
109static inline u32 btf_member_bitfield_size(const struct btf_type *struct_type,122static inline u32 btf_member_bitfield_size(const struct btf_type *struct_type,
110 const struct btf_member *member)123 const struct btf_member *member)
111{124{

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t