bpf: pass struct btf pointer to the map_check_btf() callback
Subsystems (6)
PATCH 1/3 bpf: pass struct btf pointer to the map_check_btf() callback
If key_type or value_type are of non-trivial data types (e.g. structure or typedef), it's not possible to check them without the additional information, which can't be obtained without a pointer to the btf structure.
So, let's pass btf pointer to the map_check_btf() callbacks.
--- include/linux/bpf.h | 3 +++ kernel/bpf/arraymap.c | 1 + kernel/bpf/lpm_trie.c | 1 + kernel/bpf/syscall.c | 3 ++- 4 files changed, 7 insertions(+), 1 deletion(-)
include/linux/bpf.h +3 -0
@@ -23,6 +23,7 @@ struct bpf_prog;struct bpf_map ;struct sock ;struct seq_file ; +struct btf ;struct btf_type ;/* map is generic key/value storage optionally accesible by eBPF programs */
@@ -52,6 +53,7 @@ struct bpf_map_ops {void (*map_seq_show_elem)(struct bpf_map *map,void *key,struct seq_file *m);int (*map_check_btf)(const struct bpf_map *map, +const struct btf *btf,const struct btf_type *key_type,const struct btf_type *value_type); };
@@ -126,6 +128,7 @@ static inline bool bpf_map_support_seq_show(const struct bpf_map *map) }int map_check_no_btf (const struct bpf_map *map, +const struct btf *btf,const struct btf_type *key_type,const struct btf_type *value_type);
kernel/bpf/arraymap.c +1 -0
@@ -382,6 +382,7 @@ static void percpu_array_map_seq_show_elem(struct bpf_map *map, void *key, }static int array_map_check_btf (const struct bpf_map *map, +const struct btf *btf,const struct btf_type *key_type,const struct btf_type *value_type) {
kernel/bpf/lpm_trie.c +1 -0
@@ -728,6 +728,7 @@ static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key) }static int trie_check_btf (const struct bpf_map *map, +const struct btf *btf,const struct btf_type *key_type,const struct btf_type *value_type) {
kernel/bpf/syscall.c +2 -1
@@ -456,6 +456,7 @@ static int bpf_obj_name_cpy(char *dst, const char *src) }int map_check_no_btf (const struct bpf_map *map, +const struct btf *btf,const struct btf_type *key_type,const struct btf_type *value_type) {
@@ -478,7 +479,7 @@ static int map_check_btf(const struct bpf_map *map, const struct btf *btf,return -EINVAL ;if (map->ops->map_check_btf) - ret = map->ops->map_check_btf (map, key_type, value_type); + ret = map->ops->map_check_btf (map, btf, key_type, value_type);return ret; }
Signature · 1 line
2.19.2
PATCH 2/3 bpf: add bpffs pretty print for cgroup local storage maps
Implement bpffs pretty printing for cgroup local storage maps (both shared and per-cpu).
Output example (captured for tools/testing/selftests/bpf/netcnt_prog.c):
Shared:
$ cat /sys/fs/bpf/map_2
# WARNING!! The output is for debug purpose only
# WARNING!! The output format will change
{4294968594,1}: {9999,1039896}Per-cpu:
$ cat /sys/fs/bpf/map_1
# WARNING!! The output is for debug purpose only
# WARNING!! The output format will change
{4294968594,1}: {
cpu0: {0,0,0,0,0}
cpu1: {0,0,0,0,0}
cpu2: {1,104,0,0,0}
cpu3: {0,0,0,0,0}
}--- include/linux/btf.h | 10 +++++ kernel/bpf/local_storage.c | 90 +++++++++++++++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 1 deletion(-)
include/linux/btf.h +10 -0
@@ -5,6 +5,7 @@#define _LINUX_BTF_H 1#include <linux/types.h> +#include <uapi/linux/btf.h> struct btf ;struct btf_type ;
@@ -63,4 +64,13 @@ static inline const char *btf_name_by_offset(const struct btf *btf, }#endif +static inline const struct btf_type *btf_orig_type (const struct btf *btf, +const struct btf_type *t) +{ +while (t &&BTF_INFO_KIND (t->info) ==BTF_KIND_TYPEDEF ) + t =btf_type_by_id (btf, t->type); + +return t; +} +#endif
kernel/bpf/local_storage.c +89 -1
113 lines not on this page · raw message
Signature · 1 line
2.19.2
PATCH 3/3 selftests/bpf: add btf annotations for cgroup_local_storage maps
In reply to Roman GushchinAdd btf annotations to cgroup local storage maps (per-cpu and shared) in the network packet counting example.
--- tools/testing/selftests/bpf/netcnt_prog.c | 6 ++++++ 1 file changed, 6 insertions(+)
tools/testing/selftests/bpf/netcnt_prog.c +6 -0
@@ -16,12 +16,18 @@ struct bpf_map_def SEC("maps") percpu_netcnt = { .value_size = sizeof(struct percpu_net_cnt), }; +BPF_ANNOTATE_KV_PAIR(percpu_netcnt, struct bpf_cgroup_storage_key, + struct percpu_net_cnt); + struct bpf_map_def SEC("maps") netcnt = { .type = BPF_MAP_TYPE_CGROUP_STORAGE, .key_size = sizeof(struct bpf_cgroup_storage_key), .value_size = sizeof(struct net_cnt), }; +BPF_ANNOTATE_KV_PAIR(netcnt, struct bpf_cgroup_storage_key, + struct net_cnt); + SEC("cgroup/skb")int bpf_nextcnt (struct __sk_buff *skb) {
Signature · 1 line
2.19.2
Re: [PATCH bpf-next 2/3] bpf: add bpffs pretty print for cgroup local storage maps
In reply to Roman GushchinQuoted 42 lines · Roman Gushchin
Implement bpffs pretty printing for cgroup local storage maps (both shared and per-cpu).
Output example (captured fortools/testing/selftests/bpf/netcnt_prog.c):Shared:
$ cat /sys/fs/bpf/map_2 # WARNING!! The output is for debug purpose only # WARNING!! The output format will change {4294968594,1}: {9999,1039896}Per-cpu:
$ cat /sys/fs/bpf/map_1 # WARNING!! The output is for debug purpose only # WARNING!! The output format will change {4294968594,1}: { cpu0: {0,0,0,0,0} cpu1: {0,0,0,0,0} cpu2: {1,104,0,0,0} cpu3: {0,0,0,0,0} }Signed-off-by Roman Gushchin <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Daniel Borkmann <[email protected]>--- include/linux/btf.h | 10 +++++ kernel/bpf/local_storage.c | 90 +++++++++++++++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 1 deletion(-)include/linux/btf.h +1
-0@@ -5,6 +5,7 @@#define _LINUX_BTF_H 1#include <linux/types.h> +#include <uapi/linux/btf.h> struct btf ;struct btf_type ;
include/linux/btf.h static inline const char *btf_name_by_offset(const struct btf *btf, · line 71
Quoted 8 lines
include/linux/btf.h +5
-0@@ -63,4 +64,13 @@ static inline const char *btf_name_by_offset(const struct btf *btf, }#endif +static inline const struct btf_type *btf_orig_type (const struct btf *btf, +const struct btf_type *t) +{ +while (t &&BTF_INFO_KIND (t->info) ==BTF_KIND_TYPEDEF ) + t =btf_type_by_id (btf, t->type);
technically, type modifier "const" and "volatile" can apply to member type as well. But these modifiers really don't make sense here. Could you add a comment here to mention that they will be treated as an error since such a programming is not really recommended?
Quoted 51 lines
include/linux/btf.h +4
-0+ + return t; +} +#endif kernel/bpf/local_storage.c +26
-0@@ -1,11 +1,13 @@//SPDX-License-Identifier: GPL-2.0 #include <linux/bpf-cgroup.h> #include <linux/bpf.h> +#include <linux/btf.h> #include <linux/bug.h> #include <linux/filter.h> #include <linux/mm.h> #include <linux/rbtree.h> #include <linux/slab.h> +#include <uapi/linux/btf.h> DEFINE_PER_CPU(struct bpf_cgroup_storage*, bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);@@ -308,6 +310,91 @@ static int cgroup_storage_delete_elem(struct bpf_map *map, void *key)return -EINVAL ; } +static int cgroup_storage_check_btf (const struct bpf_map *map, +const struct btf *btf, +const struct btf_type *key_type, +const struct btf_type *value_type) +{ +const struct btf_type *st, *t; +struct btf_member *m; + +/* Key is expected to be of struct bpf_cgroup_storage_key type, +* which is: +* struct bpf_cgroup_storage_key { +* __u64 cgroup_inode_id; +* __u32 attach_type; +* }; +*/ + +/* +* Key_type must be a structure (or a typedef of a structure) with +* two members. +*/ + st =btf_orig_type (btf, key_type); +if (BTF_INFO_KIND (st->info) !=BTF_KIND_STRUCT || +BTF_INFO_VLEN (st->info) !=2 ) +return -EINVAL ;
kernel/bpf/local_storage.c static int cgroup_storage_delete_elem(struct bpf_map *map, void *key) · line 346
Quoted 10 lines
kernel/bpf/local_storage.c +10
-0+ +/* +* The first field must be a 64 bit integer at 0 offset. +*/ + m = (struct btf_member *)(st +1 ); + t =btf_orig_type (btf,btf_type_by_id (btf, m->type)); +if (!t ||BTF_INFO_KIND (t->info) !=BTF_KIND_INT || m->offset || + t->size != + FIELD_SIZEOF(struct bpf_cgroup_storage_key, cgroup_inode_id)) +return -EINVAL ;
We should not use t->size here. The "t->size" is the type size, and the real number of bits held by the member is BTF_INT_BITS(...) with the argument of the u32 int value after "t".
kernel/bpf/local_storage.c static int cgroup_storage_delete_elem(struct bpf_map *map, void *key) · line 357
Quoted 11 lines
kernel/bpf/local_storage.c +11
-0+ +/* +* The second field must be a 32 bit integer at 0 offset. +*/ + m = m +1 ; + t =btf_orig_type (btf,btf_type_by_id (btf, m->type)); +if (!t ||BTF_INFO_KIND (t->info) !=BTF_KIND_INT || + m->offset != offsetof(struct bpf_cgroup_storage_key , attach_type) * +BITS_PER_BYTE || t->size != + FIELD_SIZEOF(struct bpf_cgroup_storage_key, attach_type)) +return -EINVAL ;
The same is here. t->size should not be used.BTF_INT_BITS(...) should be used.
Quoted 54 lines
kernel/bpf/local_storage.c +42
-1+ +return 0 ; +} + +static void cgroup_storage_seq_show_elem (struct bpf_map *map,void *_key, +struct seq_file *m) +{ +enum bpf_cgroup_storage_type stype =cgroup_storage_type (map); +struct bpf_cgroup_storage_key *key = _key; +struct bpf_cgroup_storage *storage; +int cpu; + +rcu_read_lock (); + storage =cgroup_storage_lookup (map_to_storage (map), key, false); +if (!storage) { +rcu_read_unlock (); +return ; + } + +btf_type_seq_show (map->btf, map->btf_key_type_id, key, m); + stype =cgroup_storage_type (map); +if (stype ==BPF_CGROUP_STORAGE_SHARED ) { +seq_puts (m,": " ); +btf_type_seq_show (map->btf, map->btf_value_type_id, + &READ_ONCE (storage->buf)->data[0 ], m); +seq_puts (m,"\n" ); + }else { +seq_puts (m,": {\n" ); +for_each_possible_cpu (cpu) { +seq_printf (m,"\tcpu%d: " , cpu); +btf_type_seq_show (map->btf, map->btf_value_type_id, +per_cpu_ptr (storage->percpu_buf, cpu), + m); +seq_puts (m,"\n" ); + } +seq_puts (m,"}\n" ); + } +rcu_read_unlock (); +} +const struct bpf_map_ops cgroup_storage_map_ops = { .map_alloc = cgroup_storage_map_alloc, .map_free = cgroup_storage_map_free,@@ -315,7 +402,8 @@ const struct bpf_map_ops cgroup_storage_map_ops = { .map_lookup_elem = cgroup_storage_lookup_elem, .map_update_elem = cgroup_storage_update_elem, .map_delete_elem = cgroup_storage_delete_elem, - .map_check_btf = map_check_no_btf, + .map_check_btf = cgroup_storage_check_btf, + .map_seq_show_elem = cgroup_storage_seq_show_elem, };int bpf_cgroup_storage_assign (struct bpf_prog *prog,struct bpf_map *_map)
Re: [PATCH bpf-next 2/3] bpf: add bpffs pretty print for cgroup local storage maps
In reply to Roman GushchinQuoted 42 lines · Roman Gushchin
Implement bpffs pretty printing for cgroup local storage maps (both shared and per-cpu).
Output example (captured fortools/testing/selftests/bpf/netcnt_prog.c):Shared:
$ cat /sys/fs/bpf/map_2 # WARNING!! The output is for debug purpose only # WARNING!! The output format will change {4294968594,1}: {9999,1039896}Per-cpu:
$ cat /sys/fs/bpf/map_1 # WARNING!! The output is for debug purpose only # WARNING!! The output format will change {4294968594,1}: { cpu0: {0,0,0,0,0} cpu1: {0,0,0,0,0} cpu2: {1,104,0,0,0} cpu3: {0,0,0,0,0} }Signed-off-by Roman Gushchin <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Daniel Borkmann <[email protected]>--- include/linux/btf.h | 10 +++++ kernel/bpf/local_storage.c | 90 +++++++++++++++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 1 deletion(-)include/linux/btf.h +1
-0@@ -5,6 +5,7 @@#define _LINUX_BTF_H 1#include <linux/types.h> +#include <uapi/linux/btf.h> struct btf ;struct btf_type ;
include/linux/btf.h static inline const char *btf_name_by_offset(const struct btf *btf, · line 71
Quoted 8 lines
include/linux/btf.h +5
-0@@ -63,4 +64,13 @@ static inline const char *btf_name_by_offset(const struct btf *btf, }#endif +static inline const struct btf_type *btf_orig_type (const struct btf *btf, +const struct btf_type *t) +{ +while (t &&BTF_INFO_KIND (t->info) ==BTF_KIND_TYPEDEF ) + t =btf_type_by_id (btf, t->type);
Only typedef is allowed? Not even const?
If that is not the case, can btf_type_id_size() be reused? The "type following" has already been done once and then remembered in the verification time.
If cgroup_storage_check_btf() can only allow typedef, please move "btf_orig_type()" to the local_storage.c.
Quoted 24 lines
include/linux/btf.h +4
-0+ + return t; +} +#endif kernel/bpf/local_storage.c +2
-0@@ -1,11 +1,13 @@//SPDX-License-Identifier: GPL-2.0 #include <linux/bpf-cgroup.h> #include <linux/bpf.h> +#include <linux/btf.h> #include <linux/bug.h> #include <linux/filter.h> #include <linux/mm.h> #include <linux/rbtree.h> #include <linux/slab.h> +#include <uapi/linux/btf.h> DEFINE_PER_CPU(struct bpf_cgroup_storage*, bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);
kernel/bpf/local_storage.c static int cgroup_storage_delete_elem(struct bpf_map *map, void *key) · line 315
Quoted 6 lines
kernel/bpf/local_storage.c +3
-0@@ -308,6 +310,91 @@ static int cgroup_storage_delete_elem(struct bpf_map *map, void *key)return -EINVAL ; } +static int cgroup_storage_check_btf(const struct bpf_map *map, + const struct btf *btf, + const struct btf_type *key_type,
Actually, in "map_check_btf()" (just before cgroup_storage_check_btf() is called), btf_type_id_size() has already been called
to get the true size and the resolved type (i.e. BTF_INFO_KIND_STRUCT here) in order to reject "key_size != map->key_size". Hence, the key_type passed to cgroup_storage_check_btf() here will not be KIND_TYPEDEF or KIND_CONST.
Quoted 20 lines
kernel/bpf/local_storage.c +20
-0+ const struct btf_type *value_type) +{ +const struct btf_type *st, *t; +struct btf_member *m; + +/* Key is expected to be of struct bpf_cgroup_storage_key type, +* which is: +* struct bpf_cgroup_storage_key { +* __u64 cgroup_inode_id; +* __u32 attach_type; +* }; +*/ + +/* +* Key_type must be a structure (or a typedef of a structure) with +* two members. +*/ + st =btf_orig_type (btf, key_type); +if (BTF_INFO_KIND (st->info) !=BTF_KIND_STRUCT || +BTF_INFO_VLEN (st->info) !=2 )
kernel/bpf/local_storage.c static int cgroup_storage_delete_elem(struct bpf_map *map, void *key) · line 345
Quoted 10 lines
kernel/bpf/local_storage.c +10
-0+return -EINVAL ; + +/* +* The first field must be a 64 bit integer at 0 offset. +*/ + m = (struct btf_member *)(st +1 ); + t =btf_orig_type (btf,btf_type_by_id (btf, m->type)); +if (!t ||BTF_INFO_KIND (t->info) !=BTF_KIND_INT || m->offset || + t->size != + FIELD_SIZEOF(struct bpf_cgroup_storage_key, cgroup_inode_id))
Instead of t->size,BTF_INT_BITS() and BTF_INT_OFFSET() need to be checked (please refer to the key_type check in array_map_check_btf()).
I think exposing "btf_type_int_is_regular()" from btf.c will be easier here. Actually, add "btf_type_is_reg_int(t, expected_size)" to btf.h and btf.c, like (uncompiled and untested code):
/*
* Not a bit field and it must be the expected size.
*/
bool btf_type_is_reg_int(const struct btf_type *t, u32 expected_size) {
u8 nr_bits, nr_bytes;
u32 int_data;
if (!btf_type_is_int(t))
return false;
int_data = btf_type_int(t);
nr_bits = BTF_INT_BITS(int_data);
nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
if (BITS_PER_BYTE_MASKED(nr_bits) ||
BTF_INT_OFFSET(int_data) ||
nr_bytes != expected_size)
return false;
return true;
}Quoted 68 lines
kernel/bpf/local_storage.c +54
-1+return -EINVAL ; + +/* +* The second field must be a 32 bit integer at 0 offset. +*/ + m = m +1 ; + t =btf_orig_type (btf,btf_type_by_id (btf, m->type)); +if (!t ||BTF_INFO_KIND (t->info) !=BTF_KIND_INT || + m->offset != offsetof(struct bpf_cgroup_storage_key , attach_type) * +BITS_PER_BYTE || t->size != + FIELD_SIZEOF(struct bpf_cgroup_storage_key, attach_type)) +return -EINVAL ; + +return 0 ; +} + +static void cgroup_storage_seq_show_elem (struct bpf_map *map,void *_key, +struct seq_file *m) +{ +enum bpf_cgroup_storage_type stype =cgroup_storage_type (map); +struct bpf_cgroup_storage_key *key = _key; +struct bpf_cgroup_storage *storage; +int cpu; + +rcu_read_lock (); + storage =cgroup_storage_lookup (map_to_storage (map), key, false); +if (!storage) { +rcu_read_unlock (); +return ; + } + +btf_type_seq_show (map->btf, map->btf_key_type_id, key, m); + stype =cgroup_storage_type (map); +if (stype ==BPF_CGROUP_STORAGE_SHARED ) { +seq_puts (m,": " ); +btf_type_seq_show (map->btf, map->btf_value_type_id, + &READ_ONCE (storage->buf)->data[0 ], m); +seq_puts (m,"\n" ); + }else { +seq_puts (m,": {\n" ); +for_each_possible_cpu (cpu) { +seq_printf (m,"\tcpu%d: " , cpu); +btf_type_seq_show (map->btf, map->btf_value_type_id, +per_cpu_ptr (storage->percpu_buf, cpu), + m); +seq_puts (m,"\n" ); + } +seq_puts (m,"}\n" ); + } +rcu_read_unlock (); +} +const struct bpf_map_ops cgroup_storage_map_ops = { .map_alloc = cgroup_storage_map_alloc, .map_free = cgroup_storage_map_free,@@ -315,7 +402,8 @@ const struct bpf_map_ops cgroup_storage_map_ops = { .map_lookup_elem = cgroup_storage_lookup_elem, .map_update_elem = cgroup_storage_update_elem, .map_delete_elem = cgroup_storage_delete_elem, - .map_check_btf = map_check_no_btf, + .map_check_btf = cgroup_storage_check_btf, + .map_seq_show_elem = cgroup_storage_seq_show_elem, };int bpf_cgroup_storage_assign (struct bpf_prog *prog,struct bpf_map *_map)Signature · 1 line
2.19.2
Quoted 6 lines · Roman Gushchin
If
key_typeorvalue_typeare of non-trivial data types (e.g. structure or typedef), it's not possible to check them without the additional information, which can't be obtained without a pointer to the btf structure.So, let's pass btf pointer to the
map_check_btf()callbacks.
Re: [PATCH bpf-next 2/3] bpf: add bpffs pretty print for cgroup local storage maps
In reply to Martin LauQuoted 86 lines · Martin Lau
Quoted 42 lines · Roman Gushchin
Implement bpffs pretty printing for cgroup local storage maps (both shared and per-cpu).
Output example (captured fortools/testing/selftests/bpf/netcnt_prog.c):Shared:
$ cat /sys/fs/bpf/map_2 # WARNING!! The output is for debug purpose only # WARNING!! The output format will change {4294968594,1}: {9999,1039896}Per-cpu:
$ cat /sys/fs/bpf/map_1 # WARNING!! The output is for debug purpose only # WARNING!! The output format will change {4294968594,1}: { cpu0: {0,0,0,0,0} cpu1: {0,0,0,0,0} cpu2: {1,104,0,0,0} cpu3: {0,0,0,0,0} }Signed-off-by Roman Gushchin <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Daniel Borkmann <[email protected]>--- include/linux/btf.h | 10 +++++ kernel/bpf/local_storage.c | 90 +++++++++++++++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 1 deletion(-)include/linux/btf.h +1
-0@@ -5,6 +5,7 @@#define _LINUX_BTF_H 1#include <linux/types.h> +#include <uapi/linux/btf.h> struct btf ;struct btf_type ;include/linux/btf.h static inline const char *btf_name_by_offset(const struct btf *btf, · line 71
Quoted 8 lines
include/linux/btf.h +5
-0@@ -63,4 +64,13 @@ static inline const char *btf_name_by_offset(const struct btf *btf, }#endif +static inline const struct btf_type *btf_orig_type (const struct btf *btf, +const struct btf_type *t) +{ +while (t &&BTF_INFO_KIND (t->info) ==BTF_KIND_TYPEDEF ) + t =btf_type_by_id (btf, t->type);Only typedef is allowed? Not even const?
If that is not the case, canbtf_type_id_size()be reused? The "type following" has already been done once and then remembered in the verification time.If
cgroup_storage_check_btf()can only allow typedef, please move "btf_orig_type()" to thelocal_storage.c.Quoted 27 lines
include/linux/btf.h +4
-0+ + return t; +} +#endif kernel/bpf/local_storage.c +2
-0@@ -1,11 +1,13 @@//SPDX-License-Identifier: GPL-2.0 #include <linux/bpf-cgroup.h> #include <linux/bpf.h> +#include <linux/btf.h> #include <linux/bug.h> #include <linux/filter.h> #include <linux/mm.h> #include <linux/rbtree.h> #include <linux/slab.h> +#include <uapi/linux/btf.h> DEFINE_PER_CPU(struct bpf_cgroup_storage*, bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);@@ -308,6 +310,91 @@ static int cgroup_storage_delete_elem(struct bpf_map *map, void *key)return -EINVAL ; }
Quoted 9 lines
kernel/bpf/local_storage.c static int cgroup_storage_delete_elem(struct bpf_map *map, void *key) · line 315
Quoted 3 lines
kernel/bpf/local_storage.c +3
-0+static int cgroup_storage_check_btf(const struct bpf_map *map, + const struct btf *btf, + const struct btf_type *key_type,Actually, in "
map_check_btf()" (just beforecgroup_storage_check_btf()is called),btf_type_id_size()has already been called
to get the true size and the resolved type (i.e.BTF_INFO_KIND_STRUCThere) in order to reject "key_size!=map->key_size". Hence, thekey_typepassed tocgroup_storage_check_btf()here will not beKIND_TYPEDEForKIND_CONST.
So, the type here is a structure, and its fields are typedefs of ints. Looks like reusing btf_type_id_size() is the best approach.
Quoted 26 lines
Quoted 26 lines
kernel/bpf/local_storage.c +25
-0+ const struct btf_type *value_type) +{ +const struct btf_type *st, *t; +struct btf_member *m; + +/* Key is expected to be of struct bpf_cgroup_storage_key type, +* which is: +* struct bpf_cgroup_storage_key { +* __u64 cgroup_inode_id; +* __u32 attach_type; +* }; +*/ + +/* +* Key_type must be a structure (or a typedef of a structure) with +* two members. +*/ + st =btf_orig_type (btf, key_type); +if (BTF_INFO_KIND (st->info) !=BTF_KIND_STRUCT || +BTF_INFO_VLEN (st->info) !=2 ) +return -EINVAL ; + +/* +* The first field must be a 64 bit integer at 0 offset. +*/
Quoted 8 lines
kernel/bpf/local_storage.c static int cgroup_storage_delete_elem(struct bpf_map *map, void *key) · line 345
Quoted 5 lines
kernel/bpf/local_storage.c +5
-0+ m = (struct btf_member *)(st +1 ); + t =btf_orig_type (btf,btf_type_by_id (btf, m->type)); +if (!t ||BTF_INFO_KIND (t->info) !=BTF_KIND_INT || m->offset || + t->size != + FIELD_SIZEOF(struct bpf_cgroup_storage_key, cgroup_inode_id))Instead of
t->size,BTF_INT_BITS()andBTF_INT_OFFSET()need to be checked (please refer to thekey_typecheck inarray_map_check_btf()).
Gotcha.
Quoted 11 lines
I think exposing "
btf_type_int_is_regular()" frombtf.cwill be easier here. Actually, add "btf_type_is_reg_int(t, expected_size)" tobtf.handbtf.c, like (uncompiled and untested code):/*
* Not a bit field and it must be the expected size.
*/
boolbtf_type_is_reg_int(const struct btf_type *t, u32 expected_size){
Quoted 16 lines
u8 nr_bits, nr_bytes;
u32 int_data;
if (!btf_type_is_int(t))
return false;
int_data = btf_type_int(t);
nr_bits = BTF_INT_BITS(int_data);
nr_bytes = BITS_ROUNDUP_BYTES(nr_bits);
if (BITS_PER_BYTE_MASKED(nr_bits) ||
BTF_INT_OFFSET(int_data) ||
nr_bytes != expected_size)
return false;
return true;
}Looks good to me. Will implement in v2.
Thanks!
Re: [PATCH bpf-next 2/3] bpf: add bpffs pretty print for cgroup local storage maps
In reply to Yonghong SongQuoted 48 lines · Yonghong Song
Quoted 45 lines · Roman Gushchin
Implement bpffs pretty printing for cgroup local storage maps (both shared and per-cpu).
Output example (captured fortools/testing/selftests/bpf/netcnt_prog.c):Shared:
$ cat /sys/fs/bpf/map_2 # WARNING!! The output is for debug purpose only # WARNING!! The output format will change {4294968594,1}: {9999,1039896}Per-cpu:
$ cat /sys/fs/bpf/map_1 # WARNING!! The output is for debug purpose only # WARNING!! The output format will change {4294968594,1}: { cpu0: {0,0,0,0,0} cpu1: {0,0,0,0,0} cpu2: {1,104,0,0,0} cpu3: {0,0,0,0,0} }Signed-off-by Roman Gushchin <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Daniel Borkmann <[email protected]>--- include/linux/btf.h | 10 +++++ kernel/bpf/local_storage.c | 90 +++++++++++++++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 1 deletion(-)include/linux/btf.h +1
-0@@ -5,6 +5,7 @@#define _LINUX_BTF_H 1#include <linux/types.h> +#include <uapi/linux/btf.h> struct btf ;struct btf_type ;@@ -63,4 +64,13 @@ static inline const char *btf_name_by_offset(const struct btf *btf, }#endif
Quoted 9 lines
include/linux/btf.h static inline const char *btf_name_by_offset(const struct btf *btf, · line 71
Quoted 5 lines
include/linux/btf.h +5
-0+static inline const struct btf_type *btf_orig_type (const struct btf *btf, +const struct btf_type *t) +{ +while (t &&BTF_INFO_KIND (t->info) ==BTF_KIND_TYPEDEF ) + t =btf_type_by_id (btf, t->type);technically, type modifier "const" and "volatile" can apply to member type as well. But these modifiers really don't make sense here. Could you add a comment here to mention that they will be treated as an error since such a programming is not really recommended?
Switched over to btf_type_id_size() based on Martin's suggestion.
Quoted 67 lines
Quoted 52 lines
include/linux/btf.h +4
-0+ + return t; +} +#endif kernel/bpf/local_storage.c +26
-0@@ -1,11 +1,13 @@//SPDX-License-Identifier: GPL-2.0 #include <linux/bpf-cgroup.h> #include <linux/bpf.h> +#include <linux/btf.h> #include <linux/bug.h> #include <linux/filter.h> #include <linux/mm.h> #include <linux/rbtree.h> #include <linux/slab.h> +#include <uapi/linux/btf.h> DEFINE_PER_CPU(struct bpf_cgroup_storage*, bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);@@ -308,6 +310,91 @@ static int cgroup_storage_delete_elem(struct bpf_map *map, void *key)return -EINVAL ; } +static int cgroup_storage_check_btf (const struct bpf_map *map, +const struct btf *btf, +const struct btf_type *key_type, +const struct btf_type *value_type) +{ +const struct btf_type *st, *t; +struct btf_member *m; + +/* Key is expected to be of struct bpf_cgroup_storage_key type, +* which is: +* struct bpf_cgroup_storage_key { +* __u64 cgroup_inode_id; +* __u32 attach_type; +* }; +*/ + +/* +* Key_type must be a structure (or a typedef of a structure) with +* two members. +*/ + st =btf_orig_type (btf, key_type); +if (BTF_INFO_KIND (st->info) !=BTF_KIND_STRUCT || +BTF_INFO_VLEN (st->info) !=2 ) +return -EINVAL ;kernel/bpf/local_storage.c static int cgroup_storage_delete_elem(struct bpf_map *map, void *key) · line 346
Quoted 10 lines
kernel/bpf/local_storage.c +10
-0+ +/* +* The first field must be a 64 bit integer at 0 offset. +*/ + m = (struct btf_member *)(st +1 ); + t =btf_orig_type (btf,btf_type_by_id (btf, m->type)); +if (!t ||BTF_INFO_KIND (t->info) !=BTF_KIND_INT || m->offset || + t->size != + FIELD_SIZEOF(struct bpf_cgroup_storage_key, cgroup_inode_id)) +return -EINVAL ;We should not use
t->sizehere. The "t->size" is the type size, and the real number of bits held by the member isBTF_INT_BITS(...)with the argument of the u32 int value after "t".
Quoted 14 lines
kernel/bpf/local_storage.c static int cgroup_storage_delete_elem(struct bpf_map *map, void *key) · line 357
Quoted 11 lines
kernel/bpf/local_storage.c +11
-0+ +/* +* The second field must be a 32 bit integer at 0 offset. +*/ + m = m +1 ; + t =btf_orig_type (btf,btf_type_by_id (btf, m->type)); +if (!t ||BTF_INFO_KIND (t->info) !=BTF_KIND_INT || + m->offset != offsetof(struct bpf_cgroup_storage_key , attach_type) * +BITS_PER_BYTE || t->size != + FIELD_SIZEOF(struct bpf_cgroup_storage_key, attach_type)) +return -EINVAL ;The same is here.
t->sizeshould not be used.BTF_INT_BITS(...)should be used.
Fixed in v2, which I'll send soon.
Thank you for the review!