bpf: Support access to bpf map fields
Subsystems (11)
PATCH v2 1/5 bpf: Switch btf_parse_vmlinux to btf_find_by_name_kind
In reply to Andrey Ignatovbtf_parse_vmlinux() implements manual search for struct bpf_ctx_convert since at the time of implementing btf_find_by_name_kind() was not available.
Later btf_find_by_name_kind() was introduced in 27ae7997a661 ("bpf:
Introduce BPF_PROG_TYPE_STRUCT_OPS"). It provides similar search functionality and can be leveraged in btf_parse_vmlinux(). Do it.
--- kernel/bpf/btf.c | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-)
kernel/bpf/btf.c +6 -17
@@ -3591,7 +3591,7 @@ struct btf *btf_parse_vmlinux(void)struct btf_verifier_env *env =NULL ;struct bpf_verifier_log *log;struct btf *btf =NULL ; -int err, i; +int err, btf_id; env =kzalloc (sizeof (*env),GFP_KERNEL | __GFP_NOWARN);if (!env)
@@ -3625,24 +3625,13 @@ struct btf *btf_parse_vmlinux(void) goto errout;/* find struct bpf_ctx_convert for type checking later */ -for (i =1 ; i <= btf->nr_types; i++) { -const struct btf_type *t; -const char *tname; - - t =btf_type_by_id (btf, i); -if (!__btf_type_is_struct (t)) -continue ; - tname =__btf_name_by_offset (btf, t->name_off); -if (!strcmp (tname,"bpf_ctx_convert" )) { -/* btf_parse_vmlinux() runs under bpf_verifier_lock */ - bpf_ctx_convert.t = t; -break ; - } - } -if (i > btf->nr_types) { - err = -ENOENT ; + btf_id =btf_find_by_name_kind (btf,"bpf_ctx_convert" ,BTF_KIND_STRUCT ); +if (btf_id <0 ) { + err = btf_id; goto errout; } +/* btf_parse_vmlinux() runs under bpf_verifier_lock */ + bpf_ctx_convert.t =btf_type_by_id (btf, btf_id);bpf_struct_ops_init (btf, log);
Signature · 1 line
2.24.1
v2 cover bpf: Support access to bpf map fields
v1->v2:
- move btf id cache to a new bpf_map_ops.map_btf_id field (Martin, Andrii);
- don't check btf names for collisions (Martin);
- drop btf_find_by_name_kind_next() patch since it was needed only for collision check;
- don't fall back to struct bpf_map if a map type doesn't specify both map_btf_name and map_btf_id;
This patch set adds support to access bpf map fields from bpf programs using btf_struct_access().
That way a program can cast a pointer to map to either struct bpf_map * or map type specific struct pointer such as struct bpf_array * or struct bpf_htab *, and access necessary fields, e.g. map->max_entries.
The fields, in turn, should be defined by a user provided struct with preserve_access_index attribute or included from vmlinux.h.
Please see patch 3 for more details on the feature and use-cases.
Other patches:
Patch 1 is refactoring to simplify btf_parse_vmlinux(). Patch 2 is a rename to avoid having two different struct bpf_htab.
Patch 4 enables access to map fields for all map types. Patch 5 adds selftests.
Andrey Ignatov (5):
bpf: Switch btf_parse_vmlinux to btf_find_by_name_kind
bpf: Rename bpf_htab to bpf_shtab in sock_map
bpf: Support access to bpf map fields
bpf: Set map_btf_{name,id} for all map types selftests/bpf: Test access to bpf map pointer
include/linux/bpf.h | 9 + include/linux/bpf_verifier.h | 1 + kernel/bpf/arraymap.c | 18 + kernel/bpf/bpf_struct_ops.c | 3 + kernel/bpf/btf.c | 63 +- kernel/bpf/cpumap.c | 3 + kernel/bpf/devmap.c | 6 + kernel/bpf/hashtab.c | 15 + kernel/bpf/local_storage.c | 3 + kernel/bpf/lpm_trie.c | 3 + kernel/bpf/queue_stack_maps.c | 6 + kernel/bpf/reuseport_array.c | 3 + kernel/bpf/ringbuf.c | 3 + kernel/bpf/stackmap.c | 3 + kernel/bpf/verifier.c | 82 ++- net/core/bpf_sk_storage.c | 3 + net/core/sock_map.c | 88 +-- net/xdp/xskmap.c | 3 + .../selftests/bpf/prog_tests/map_ptr.c | 32 + .../selftests/bpf/progs/map_ptr_kern.c | 686 ++++++++++++++++++ .../testing/selftests/bpf/verifier/map_ptr.c | 62 ++ .../selftests/bpf/verifier/map_ptr_mixing.c | 2 +- 22 files changed, 1030 insertions(+), 67 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/map_ptr.c create mode 100644 tools/testing/selftests/bpf/progs/map_ptr_kern.c create mode 100644 tools/testing/selftests/bpf/verifier/map_ptr.c
Signature · 1 line
2.24.1
PATCH v2 2/5 bpf: Rename bpf_htab to bpf_shtab in sock_map
There are two different struct bpf_htab in bpf code in the following files:
- kernel/bpf/hashtab.c
- net/core/sock_map.c
It makes it impossible to find proper btf_id by name = "bpf_htab" and kind = BTF_KIND_STRUCT what is needed to support access to map ptr so that bpf program can access struct bpf_htab fields.
To make it possible one of the struct-s should be renamed, sock_map.c looks like a better candidate for rename since it's specialized version of hashtab.
Rename it to bpf_shtab ("sh" stands for Sock Hash).
--- net/core/sock_map.c | 82 ++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 41 deletions(-)
net/core/sock_map.c +41 -41
@@ -655,7 +655,7 @@ const struct bpf_map_ops sock_map_ops = { .map_check_btf = map_check_no_btf, }; -struct bpf_htab_elem { +struct bpf_shtab_elem {struct rcu_head rcu;u32 hash;struct sock *sk;
@@ -663,14 +663,14 @@ struct bpf_htab_elem {u8 key[]; }; -struct bpf_htab_bucket { +struct bpf_shtab_bucket {struct hlist_head head;raw_spinlock_t lock; }; -struct bpf_htab { +struct bpf_shtab {struct bpf_map map; -struct bpf_htab_bucket *buckets; +struct bpf_shtab_bucket *buckets;u32 buckets_num;u32 elem_size;struct sk_psock_progs progs;
@@ -682,17 +682,17 @@ static inline u32 sock_hash_bucket_hash(const void *key, u32 len)return jhash (key, len,0 ); } -static struct bpf_htab_bucket *sock_hash_select_bucket (struct bpf_htab *htab, -u32 hash) +static struct bpf_shtab_bucket *sock_hash_select_bucket (struct bpf_shtab *htab, +u32 hash) {return &htab->buckets[hash & (htab->buckets_num -1 )]; } -static struct bpf_htab_elem * +static struct bpf_shtab_elem *sock_hash_lookup_elem_raw (struct hlist_head *head,u32 hash,void *key,u32 key_size) { -struct bpf_htab_elem *elem; +struct bpf_shtab_elem *elem;hlist_for_each_entry_rcu (elem, head, node) { if (elem->hash == hash &&
@@ -705,10 +705,10 @@ sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key,static struct sock *__sock_hash_lookup_elem (struct bpf_map *map,void *key) { - struct bpf_htab *htab = container_of(map, struct bpf_htab, map); + struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);u32 key_size = map->key_size, hash; -struct bpf_htab_bucket *bucket; -struct bpf_htab_elem *elem; +struct bpf_shtab_bucket *bucket; +struct bpf_shtab_elem *elem;WARN_ON_ONCE (!rcu_read_lock_held ());
@@ -719,8 +719,8 @@ static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key)return elem ? elem->sk :NULL ; } -static void sock_hash_free_elem (struct bpf_htab *htab, -struct bpf_htab_elem *elem) +static void sock_hash_free_elem (struct bpf_shtab *htab, +struct bpf_shtab_elem *elem) {atomic_dec (&htab->count);kfree_rcu (elem, rcu);
@@ -729,9 +729,9 @@ static void sock_hash_free_elem(struct bpf_htab *htab,static void sock_hash_delete_from_link (struct bpf_map *map,struct sock *sk,void *link_raw) { - struct bpf_htab *htab = container_of(map, struct bpf_htab, map); -struct bpf_htab_elem *elem_probe, *elem = link_raw; -struct bpf_htab_bucket *bucket; + struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map); +struct bpf_shtab_elem *elem_probe, *elem = link_raw; +struct bpf_shtab_bucket *bucket;WARN_ON_ONCE (!rcu_read_lock_held ()); bucket =sock_hash_select_bucket (htab, elem->hash);
@@ -753,10 +753,10 @@ static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk,static int sock_hash_delete_elem (struct bpf_map *map,void *key) { - struct bpf_htab *htab = container_of(map, struct bpf_htab, map); + struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);u32 hash, key_size = map->key_size; -struct bpf_htab_bucket *bucket; -struct bpf_htab_elem *elem; +struct bpf_shtab_bucket *bucket; +struct bpf_shtab_elem *elem;int ret = -ENOENT ; hash =sock_hash_bucket_hash (key, key_size);
@@ -774,12 +774,12 @@ static int sock_hash_delete_elem(struct bpf_map *map, void *key)return ret; } -static struct bpf_htab_elem *sock_hash_alloc_elem (struct bpf_htab *htab, -void *key,u32 key_size, -u32 hash,struct sock *sk, -struct bpf_htab_elem *old) +static struct bpf_shtab_elem *sock_hash_alloc_elem (struct bpf_shtab *htab, +void *key,u32 key_size, +u32 hash,struct sock *sk, +struct bpf_shtab_elem *old) { -struct bpf_htab_elem *new; +struct bpf_shtab_elem *new;if (atomic_inc_return (&htab->count) > htab->map.max_entries) {if (!old) {
@@ -803,10 +803,10 @@ static struct bpf_htab_elem *sock_hash_alloc_elem(struct bpf_htab *htab,static int sock_hash_update_common (struct bpf_map *map,void *key,struct sock *sk,u64 flags) { - struct bpf_htab *htab = container_of(map, struct bpf_htab, map); + struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map);u32 key_size = map->key_size, hash; -struct bpf_htab_elem *elem, *elem_new; -struct bpf_htab_bucket *bucket; +struct bpf_shtab_elem *elem, *elem_new; +struct bpf_shtab_bucket *bucket;struct sk_psock_link *link;struct sk_psock *psock;int ret;
@@ -916,8 +916,8 @@ static int sock_hash_update_elem(struct bpf_map *map, void *key,static int sock_hash_get_next_key (struct bpf_map *map,void *key,void *key_next) { - struct bpf_htab *htab = container_of(map, struct bpf_htab, map); -struct bpf_htab_elem *elem, *elem_next; + struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map); +struct bpf_shtab_elem *elem, *elem_next;u32 hash, key_size = map->key_size;struct hlist_head *head;int i =0 ;
@@ -931,7 +931,7 @@ static int sock_hash_get_next_key(struct bpf_map *map, void *key, goto find_first_elem; elem_next =hlist_entry_safe (rcu_dereference_raw (hlist_next_rcu (&elem->node)), - struct bpf_htab_elem, node); + struct bpf_shtab_elem, node);if (elem_next) {memcpy (key_next, elem_next->key, key_size);return 0 ;
@@ -943,7 +943,7 @@ static int sock_hash_get_next_key(struct bpf_map *map, void *key,for (; i < htab->buckets_num; i++) { head = &sock_hash_select_bucket (htab, i)->head; elem_next =hlist_entry_safe (rcu_dereference_raw (hlist_first_rcu (head)), - struct bpf_htab_elem, node); + struct bpf_shtab_elem, node);if (elem_next) {memcpy (key_next, elem_next->key, key_size);return 0 ;
@@ -955,7 +955,7 @@ static int sock_hash_get_next_key(struct bpf_map *map, void *key,static struct bpf_map *sock_hash_alloc (union bpf_attr *attr) { -struct bpf_htab *htab; +struct bpf_shtab *htab;int i, err;u64 cost;
@@ -977,15 +977,15 @@ static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)bpf_map_init_from_attr (&htab->map, attr); htab->buckets_num =roundup_pow_of_two (htab->map.max_entries); - htab->elem_size =sizeof (struct bpf_htab_elem ) + + htab->elem_size =sizeof (struct bpf_shtab_elem ) +round_up (htab->map.key_size,8 );if (htab->buckets_num ==0 || - htab->buckets_num >U32_MAX /sizeof (struct bpf_htab_bucket )) { + htab->buckets_num >U32_MAX /sizeof (struct bpf_shtab_bucket )) { err = -EINVAL ; goto free_htab; } - cost = (u64 ) htab->buckets_num *sizeof (struct bpf_htab_bucket ) + + cost = (u64 ) htab->buckets_num *sizeof (struct bpf_shtab_bucket ) + (u64 ) htab->elem_size * htab->map.max_entries;if (cost >=U32_MAX -PAGE_SIZE ) { err = -EINVAL ;
@@ -996,7 +996,7 @@ static struct bpf_map *sock_hash_alloc(union bpf_attr *attr) goto free_htab; htab->buckets =bpf_map_area_alloc (htab->buckets_num * -sizeof (struct bpf_htab_bucket ), +sizeof (struct bpf_shtab_bucket ), htab->map.numa_node);if (!htab->buckets) {bpf_map_charge_finish (&htab->map.memory);
@@ -1017,10 +1017,10 @@ static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)static void sock_hash_free (struct bpf_map *map) { - struct bpf_htab *htab = container_of(map, struct bpf_htab, map); -struct bpf_htab_bucket *bucket; + struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map); +struct bpf_shtab_bucket *bucket;struct hlist_head unlink_list; -struct bpf_htab_elem *elem; +struct bpf_shtab_elem *elem;struct hlist_node *node;int i;
@@ -1096,7 +1096,7 @@ static void *sock_hash_lookup(struct bpf_map *map, void *key)static void sock_hash_release_progs (struct bpf_map *map) { - psock_progs_drop(&container_of(map, struct bpf_htab, map)->progs); + psock_progs_drop(&container_of(map, struct bpf_shtab, map)->progs); } BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops,
@@ -1194,7 +1194,7 @@ static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)case BPF_MAP_TYPE_SOCKMAP : return &container_of(map, struct bpf_stab, map)->progs;case BPF_MAP_TYPE_SOCKHASH : - return &container_of(map, struct bpf_htab, map)->progs; + return &container_of(map, struct bpf_shtab, map)->progs;default :break ; }
Signature · 1 line
2.24.1
PATCH v2 3/5 bpf: Support access to bpf map fields
In reply to Andrey IgnatovThere are multiple use-cases when it's convenient to have access to bpf map fields, both struct bpf_map and map type specific struct-s such as struct bpf_array, struct bpf_htab, etc.
For example while working with sock arrays it can be necessary to calculate the key based on map->max_entries (some_hash % max_entries). Currently this is solved by communicating max_entries via "out-of-band" channel, e.g. via additional map with known key to get info about target map. That works, but is not very convenient and error-prone while working with many maps.
In other cases necessary data is dynamic (i.e. unknown at loading time) and it's impossible to get it at all. For example while working with a hash table it can be convenient to know how much capacity is already used (bpf_htab.count.counter for BPF_F_NO_PREALLOC case).
At the same time kernel knows this info and can provide it to bpf program.
Fill this gap by adding support to access bpf map fields from bpf program for both struct bpf_map and map type specific fields.
Support is implemented via btf_struct_access() so that a user can define their own struct bpf_map or map type specific struct in their program with only necessary fields and preserve_access_index attribute, cast a map to this struct and use a field.
For example:
struct bpf_map {
__u32 max_entries;
} __attribute__((preserve_access_index));
struct bpf_array {
struct bpf_map map;
__u32 elem_size;
} __attribute__((preserve_access_index));
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 4);
__type(key, __u32);
__type(value, __u32);
} m_array SEC(".maps");
SEC("cgroup_skb/egress")
int cg_skb(void *ctx)
{
struct bpf_array *array = (struct bpf_array *)&m_array;
struct bpf_map *map = (struct bpf_map *)&m_array;
/* .. use map->max_entries or array->map.max_entries .. */
}Similarly to other btf_struct_access() use-cases (e.g. struct tcp_sock in net/ipv4/bpf_tcp_ca.c) the patch allows access to any fields of corresponding struct. Only reading from map fields is supported.
For btf_struct_access() to work there should be a way to know btf id of a struct that corresponds to a map type. To get btf id there should be a way to get a stringified name of map-specific struct, such as "bpf_array", "bpf_htab", etc for a map type. Two new fields are added to struct bpf_map_ops to handle it:
* .map_btf_name keeps a btf name of a struct returned by map_alloc();
* .map_btf_id is used to cache btf id of that struct.
To make btf ids calculation cheaper they're calculated once while preparing btf_vmlinux and cached same way as it's done for btf_id field of struct bpf_func_proto
While calculating btf ids, struct names are NOT checked for collision. Collisions will be checked as a part of the work to prepare btf ids used in verifier in compile time that should land soon. The only known collision for struct bpf_htab (kernel/bpf/hashtab.c vs net/core/sock_map.c) was fixed earlier.
Both new fields .map_btf_name and .map_btf_id must be set for a map type for the feature to work. If neither is set for a map type, verifier will return ENOTSUPP on a try to access map_ptr of corresponding type. If just one of them set, it's verifier misconfiguration.
Only struct bpf_array for BPF_MAP_TYPE_ARRAY and struct bpf_htab for BPF_MAP_TYPE_HASH are supported by this patch. Other map types will be supported separately.
The feature is available only for CONFIG_DEBUG_INFO_BTF=y and gated by perfmon_capable() so that unpriv programs won't have access to bpf map fields.
--- include/linux/bpf.h | 9 ++ include/linux/bpf_verifier.h | 1 + kernel/bpf/arraymap.c | 3 + kernel/bpf/btf.c | 40 +++++++++ kernel/bpf/hashtab.c | 3 + kernel/bpf/verifier.c | 82 +++++++++++++++++-- .../selftests/bpf/verifier/map_ptr_mixing.c | 2 +- 7 files changed, 131 insertions(+), 9 deletions(-)
include/linux/bpf.h +9 -0
@@ -92,6 +92,10 @@ struct bpf_map_ops {int (*map_mmap)(struct bpf_map *map,struct vm_area_struct *vma);__poll_t (*map_poll)(struct bpf_map *map,struct file *filp,struct poll_table_struct *pts); + +/* BTF name and id of struct allocated by map_alloc */ +const char *const map_btf_name; +int *map_btf_id; };struct bpf_map_memory {
@@ -1109,6 +1113,11 @@ static inline bool bpf_allow_ptr_leaks(void)return perfmon_capable (); } +static inline bool bpf_allow_ptr_to_map_access (void ) +{ +return perfmon_capable (); +} +static inline bool bpf_bypass_spec_v1 (void ) {return perfmon_capable ();
include/linux/bpf_verifier.h +1 -0
@@ -379,6 +379,7 @@ struct bpf_verifier_env {u32 used_map_cnt;/* number of used maps */ u32 id_gen;/* used to generate unique reg IDs */ bool allow_ptr_leaks; +bool allow_ptr_to_map_access;bool bpf_capable;bool bypass_spec_v1;bool bypass_spec_v4;
kernel/bpf/arraymap.c +3 -0
@@ -494,6 +494,7 @@ static int array_map_mmap(struct bpf_map *map, struct vm_area_struct *vma) vma->vm_pgoff + pgoff); } +static int array_map_btf_id;const struct bpf_map_ops array_map_ops = { .map_alloc_check = array_map_alloc_check, .map_alloc = array_map_alloc,
@@ -510,6 +511,8 @@ const struct bpf_map_ops array_map_ops = { .map_check_btf = array_map_check_btf, .map_lookup_batch = generic_map_lookup_batch, .map_update_batch = generic_map_update_batch, + .map_btf_name = "bpf_array", + .map_btf_id = &array_map_btf_id, };const struct bpf_map_ops percpu_array_map_ops = {
kernel/bpf/btf.c +40 -0
@@ -3571,6 +3571,41 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf,return ctx_type; } +static const struct bpf_map_ops * const btf_vmlinux_map_ops[] = { +#define BPF_PROG_TYPE (_id, _name, prog_ctx_type, kern_ctx_type) +#define BPF_LINK_TYPE (_id, _name) +#define BPF_MAP_TYPE (_id, _ops) \ + [_id] = &_ops, +#include <linux/bpf_types.h> +#undef BPF_PROG_TYPE +#undef BPF_LINK_TYPE +#undef BPF_MAP_TYPE +}; + +static int btf_vmlinux_map_ids_init (const struct btf *btf, +struct bpf_verifier_log *log) +{ +const struct bpf_map_ops *ops; +int i, btf_id; + +for (i =0 ; i <ARRAY_SIZE (btf_vmlinux_map_ops); ++i) { + ops = btf_vmlinux_map_ops[i]; +if (!ops || (!ops->map_btf_name && !ops->map_btf_id)) +continue ; +if (!ops->map_btf_name || !ops->map_btf_id) { +bpf_log (log,"map type %d is misconfigured\n" , i); +return -EINVAL ; + } + btf_id =btf_find_by_name_kind (btf, ops->map_btf_name, +BTF_KIND_STRUCT ); +if (btf_id <0 ) +return btf_id; + *ops->map_btf_id = btf_id; + } + +return 0 ; +} + static int btf_translate_to_vmlinux(struct bpf_verifier_log *log, struct btf *btf, const struct btf_type *t,
@@ -3633,6 +3668,11 @@ struct btf *btf_parse_vmlinux(void)/* btf_parse_vmlinux() runs under bpf_verifier_lock */ bpf_ctx_convert.t =btf_type_by_id (btf, btf_id); +/* find bpf map structs for map_ptr access checking */ + err =btf_vmlinux_map_ids_init (btf, log); +if (err <0 ) + goto errout; +bpf_struct_ops_init (btf, log);btf_verifier_env_free (env);
kernel/bpf/hashtab.c +3 -0
@@ -1614,6 +1614,7 @@ htab_lru_map_lookup_and_delete_batch(struct bpf_map *map, true, false); } +static int htab_map_btf_id;const struct bpf_map_ops htab_map_ops = { .map_alloc_check = htab_map_alloc_check, .map_alloc = htab_map_alloc,
@@ -1625,6 +1626,8 @@ const struct bpf_map_ops htab_map_ops = { .map_gen_lookup = htab_map_gen_lookup, .map_seq_show_elem = htab_map_seq_show_elem,BATCH_OPS (htab), + .map_btf_name = "bpf_htab", + .map_btf_id = &htab_map_btf_id, }; const struct bpf_map_ops htab_lru_map_ops = {
kernel/bpf/verifier.c +74 -8
111 lines not on this page · raw message
tools/testing/selftests/bpf/verifier/map_ptr_mixing.c +1 -1
@@ -56,7 +56,7 @@ .fixup_map_in_map = { 16 }, .fixup_map_array_48b = { 13 }, .result = REJECT, - .errstr ="R0 invalid mem access 'map_ptr'" , + .errstr ="only read from bpf_array is supported" , }, { "cond: two branches returning different map pointers for lookup (tail, tail)",
Signature · 1 line
2.24.1
PATCH v2 4/5 bpf: Set map_btf_{name,id} for all map types
In reply to Andrey IgnatovSet map_btf_name and map_btf_id for all map types so that map fields can be accessed by bpf programs.
--- kernel/bpf/arraymap.c | 15 +++++++++++++++ kernel/bpf/bpf_struct_ops.c | 3 +++ kernel/bpf/cpumap.c | 3 +++ kernel/bpf/devmap.c | 6 ++++++ kernel/bpf/hashtab.c | 12 ++++++++++++ kernel/bpf/local_storage.c | 3 +++ kernel/bpf/lpm_trie.c | 3 +++ kernel/bpf/queue_stack_maps.c | 6 ++++++ kernel/bpf/reuseport_array.c | 3 +++ kernel/bpf/ringbuf.c | 3 +++ kernel/bpf/stackmap.c | 3 +++ net/core/bpf_sk_storage.c | 3 +++ net/core/sock_map.c | 6 ++++++ net/xdp/xskmap.c | 3 +++ 14 files changed, 72 insertions(+)
kernel/bpf/arraymap.c +15 -0
@@ -515,6 +515,7 @@ const struct bpf_map_ops array_map_ops = { .map_btf_id = &array_map_btf_id, }; +static int percpu_array_map_btf_id;const struct bpf_map_ops percpu_array_map_ops = { .map_alloc_check = array_map_alloc_check, .map_alloc = array_map_alloc,
@@ -525,6 +526,8 @@ const struct bpf_map_ops percpu_array_map_ops = { .map_delete_elem = array_map_delete_elem, .map_seq_show_elem = percpu_array_map_seq_show_elem, .map_check_btf = array_map_check_btf, + .map_btf_name = "bpf_array", + .map_btf_id = &percpu_array_map_btf_id, };static int fd_array_map_alloc_check (union bpf_attr *attr)
@@ -871,6 +874,7 @@ static void prog_array_map_free(struct bpf_map *map)fd_array_map_free (map); } +static int prog_array_map_btf_id;const struct bpf_map_ops prog_array_map_ops = { .map_alloc_check = fd_array_map_alloc_check, .map_alloc = prog_array_map_alloc,
@@ -886,6 +890,8 @@ const struct bpf_map_ops prog_array_map_ops = { .map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem, .map_release_uref = prog_array_map_clear, .map_seq_show_elem = prog_array_map_seq_show_elem, + .map_btf_name = "bpf_array", + .map_btf_id = &prog_array_map_btf_id, }; static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
@@ -964,6 +970,7 @@ static void perf_event_fd_array_release(struct bpf_map *map,rcu_read_unlock (); } +static int perf_event_array_map_btf_id;const struct bpf_map_ops perf_event_array_map_ops = { .map_alloc_check = fd_array_map_alloc_check, .map_alloc = array_map_alloc,
@@ -975,6 +982,8 @@ const struct bpf_map_ops perf_event_array_map_ops = { .map_fd_put_ptr = perf_event_fd_array_put_ptr, .map_release = perf_event_fd_array_release, .map_check_btf = map_check_no_btf, + .map_btf_name = "bpf_array", + .map_btf_id = &perf_event_array_map_btf_id, };#ifdef CONFIG_CGROUPS
@@ -997,6 +1006,7 @@ static void cgroup_fd_array_free(struct bpf_map *map)fd_array_map_free (map); } +static int cgroup_array_map_btf_id;const struct bpf_map_ops cgroup_array_map_ops = { .map_alloc_check = fd_array_map_alloc_check, .map_alloc = array_map_alloc,
@@ -1007,6 +1017,8 @@ const struct bpf_map_ops cgroup_array_map_ops = { .map_fd_get_ptr = cgroup_fd_array_get_ptr, .map_fd_put_ptr = cgroup_fd_array_put_ptr, .map_check_btf = map_check_no_btf, + .map_btf_name = "bpf_array", + .map_btf_id = &cgroup_array_map_btf_id, };#endif
@@ -1080,6 +1092,7 @@ static u32 array_of_map_gen_lookup(struct bpf_map *map,return insn - insn_buf; } +static int array_of_maps_map_btf_id;const struct bpf_map_ops array_of_maps_map_ops = { .map_alloc_check = fd_array_map_alloc_check, .map_alloc = array_of_map_alloc,
@@ -1092,4 +1105,6 @@ const struct bpf_map_ops array_of_maps_map_ops = { .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem, .map_gen_lookup = array_of_map_gen_lookup, .map_check_btf = map_check_no_btf, + .map_btf_name = "bpf_array", + .map_btf_id = &array_of_maps_map_btf_id, };
kernel/bpf/bpf_struct_ops.c +3 -0
@@ -611,6 +611,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)return map; } +static int bpf_struct_ops_map_btf_id;const struct bpf_map_ops bpf_struct_ops_map_ops = { .map_alloc_check = bpf_struct_ops_map_alloc_check, .map_alloc = bpf_struct_ops_map_alloc,
@@ -620,6 +621,8 @@ const struct bpf_map_ops bpf_struct_ops_map_ops = { .map_delete_elem = bpf_struct_ops_map_delete_elem, .map_update_elem = bpf_struct_ops_map_update_elem, .map_seq_show_elem = bpf_struct_ops_map_seq_show_elem, + .map_btf_name = "bpf_struct_ops_map", + .map_btf_id = &bpf_struct_ops_map_btf_id, };/* "const void *" because some subsystem is
kernel/bpf/cpumap.c +3 -0
@@ -543,6 +543,7 @@ static int cpu_map_get_next_key(struct bpf_map *map, void *key, void *next_key)return 0 ; } +static int cpu_map_btf_id;const struct bpf_map_ops cpu_map_ops = { .map_alloc = cpu_map_alloc, .map_free = cpu_map_free,
@@ -551,6 +552,8 @@ const struct bpf_map_ops cpu_map_ops = { .map_lookup_elem = cpu_map_lookup_elem, .map_get_next_key = cpu_map_get_next_key, .map_check_btf = map_check_no_btf, + .map_btf_name = "bpf_cpu_map", + .map_btf_id = &cpu_map_btf_id, };static int bq_flush_to_queue (struct xdp_bulk_queue *bq)
kernel/bpf/devmap.c +6 -0
@@ -747,6 +747,7 @@ static int dev_map_hash_update_elem(struct bpf_map *map, void *key, void *value, map, key, value, map_flags); } +static int dev_map_btf_id;const struct bpf_map_ops dev_map_ops = { .map_alloc = dev_map_alloc, .map_free = dev_map_free,
@@ -755,8 +756,11 @@ const struct bpf_map_ops dev_map_ops = { .map_update_elem = dev_map_update_elem, .map_delete_elem = dev_map_delete_elem, .map_check_btf = map_check_no_btf, + .map_btf_name = "bpf_dtab", + .map_btf_id = &dev_map_btf_id, }; +static int dev_map_hash_map_btf_id;const struct bpf_map_ops dev_map_hash_ops = { .map_alloc = dev_map_alloc, .map_free = dev_map_free,
@@ -765,6 +769,8 @@ const struct bpf_map_ops dev_map_hash_ops = { .map_update_elem = dev_map_hash_update_elem, .map_delete_elem = dev_map_hash_delete_elem, .map_check_btf = map_check_no_btf, + .map_btf_name = "bpf_dtab", + .map_btf_id = &dev_map_hash_map_btf_id, }; static void dev_map_hash_remove_netdev(struct bpf_dtab *dtab,
kernel/bpf/hashtab.c +12 -0
@@ -1630,6 +1630,7 @@ const struct bpf_map_ops htab_map_ops = { .map_btf_id = &htab_map_btf_id, }; +static int htab_lru_map_btf_id;const struct bpf_map_ops htab_lru_map_ops = { .map_alloc_check = htab_map_alloc_check, .map_alloc = htab_map_alloc,
@@ -1642,6 +1643,8 @@ const struct bpf_map_ops htab_lru_map_ops = { .map_gen_lookup = htab_lru_map_gen_lookup, .map_seq_show_elem = htab_map_seq_show_elem,BATCH_OPS (htab_lru), + .map_btf_name = "bpf_htab", + .map_btf_id = &htab_lru_map_btf_id, };/* Called from eBPF program */
@@ -1746,6 +1749,7 @@ static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,rcu_read_unlock (); } +static int htab_percpu_map_btf_id;const struct bpf_map_ops htab_percpu_map_ops = { .map_alloc_check = htab_map_alloc_check, .map_alloc = htab_map_alloc,
@@ -1756,8 +1760,11 @@ const struct bpf_map_ops htab_percpu_map_ops = { .map_delete_elem = htab_map_delete_elem, .map_seq_show_elem = htab_percpu_map_seq_show_elem,BATCH_OPS (htab_percpu), + .map_btf_name = "bpf_htab", + .map_btf_id = &htab_percpu_map_btf_id, }; +static int htab_lru_percpu_map_btf_id;const struct bpf_map_ops htab_lru_percpu_map_ops = { .map_alloc_check = htab_map_alloc_check, .map_alloc = htab_map_alloc,
@@ -1768,6 +1775,8 @@ const struct bpf_map_ops htab_lru_percpu_map_ops = { .map_delete_elem = htab_lru_map_delete_elem, .map_seq_show_elem = htab_percpu_map_seq_show_elem,BATCH_OPS (htab_lru_percpu), + .map_btf_name = "bpf_htab", + .map_btf_id = &htab_lru_percpu_map_btf_id, };static int fd_htab_map_alloc_check (union bpf_attr *attr)
@@ -1890,6 +1899,7 @@ static void htab_of_map_free(struct bpf_map *map)fd_htab_map_free (map); } +static int htab_of_maps_map_btf_id;const struct bpf_map_ops htab_of_maps_map_ops = { .map_alloc_check = fd_htab_map_alloc_check, .map_alloc = htab_of_map_alloc,
@@ -1902,4 +1912,6 @@ const struct bpf_map_ops htab_of_maps_map_ops = { .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem, .map_gen_lookup = htab_of_map_gen_lookup, .map_check_btf = map_check_no_btf, + .map_btf_name = "bpf_htab", + .map_btf_id = &htab_of_maps_map_btf_id, };
kernel/bpf/local_storage.c +3 -0
@@ -409,6 +409,7 @@ static void cgroup_storage_seq_show_elem(struct bpf_map *map, void *_key,rcu_read_unlock (); } +static int cgroup_storage_map_btf_id;const struct bpf_map_ops cgroup_storage_map_ops = { .map_alloc = cgroup_storage_map_alloc, .map_free = cgroup_storage_map_free,
@@ -418,6 +419,8 @@ const struct bpf_map_ops cgroup_storage_map_ops = { .map_delete_elem = cgroup_storage_delete_elem, .map_check_btf = cgroup_storage_check_btf, .map_seq_show_elem = cgroup_storage_seq_show_elem, + .map_btf_name = "bpf_cgroup_storage_map", + .map_btf_id = &cgroup_storage_map_btf_id, };int bpf_cgroup_storage_assign (struct bpf_prog_aux *aux,struct bpf_map *_map)
kernel/bpf/lpm_trie.c +3 -0
@@ -735,6 +735,7 @@ static int trie_check_btf(const struct bpf_map *map, -EINVAL : 0; } +static int trie_map_btf_id;const struct bpf_map_ops trie_map_ops = { .map_alloc = trie_alloc, .map_free = trie_free,
@@ -743,4 +744,6 @@ const struct bpf_map_ops trie_map_ops = { .map_update_elem = trie_update_elem, .map_delete_elem = trie_delete_elem, .map_check_btf = trie_check_btf, + .map_btf_name = "lpm_trie", + .map_btf_id = &trie_map_btf_id, };
kernel/bpf/queue_stack_maps.c +6 -0
@@ -262,6 +262,7 @@ static int queue_stack_map_get_next_key(struct bpf_map *map, void *key,return -EINVAL ; } +static int queue_map_btf_id;const struct bpf_map_ops queue_map_ops = { .map_alloc_check = queue_stack_map_alloc_check, .map_alloc = queue_stack_map_alloc,
@@ -273,8 +274,11 @@ const struct bpf_map_ops queue_map_ops = { .map_pop_elem = queue_map_pop_elem, .map_peek_elem = queue_map_peek_elem, .map_get_next_key = queue_stack_map_get_next_key, + .map_btf_name = "bpf_queue_stack", + .map_btf_id = &queue_map_btf_id, }; +static int stack_map_btf_id;const struct bpf_map_ops stack_map_ops = { .map_alloc_check = queue_stack_map_alloc_check, .map_alloc = queue_stack_map_alloc,
@@ -286,4 +290,6 @@ const struct bpf_map_ops stack_map_ops = { .map_pop_elem = stack_map_pop_elem, .map_peek_elem = stack_map_peek_elem, .map_get_next_key = queue_stack_map_get_next_key, + .map_btf_name = "bpf_queue_stack", + .map_btf_id = &stack_map_btf_id, };
kernel/bpf/reuseport_array.c +3 -0
@@ -345,6 +345,7 @@ static int reuseport_array_get_next_key(struct bpf_map *map, void *key,return 0 ; } +static int reuseport_array_map_btf_id;const struct bpf_map_ops reuseport_array_ops = { .map_alloc_check = reuseport_array_alloc_check, .map_alloc = reuseport_array_alloc,
@@ -352,4 +353,6 @@ const struct bpf_map_ops reuseport_array_ops = { .map_lookup_elem = reuseport_array_lookup_elem, .map_get_next_key = reuseport_array_get_next_key, .map_delete_elem = reuseport_array_delete_elem, + .map_btf_name = "reuseport_array", + .map_btf_id = &reuseport_array_map_btf_id, };
kernel/bpf/ringbuf.c +3 -0
@@ -294,6 +294,7 @@ static __poll_t ringbuf_map_poll(struct bpf_map *map, struct file *filp,return 0 ; } +static int ringbuf_map_btf_id;const struct bpf_map_ops ringbuf_map_ops = { .map_alloc = ringbuf_map_alloc, .map_free = ringbuf_map_free,
@@ -303,6 +304,8 @@ const struct bpf_map_ops ringbuf_map_ops = { .map_update_elem = ringbuf_map_update_elem, .map_delete_elem = ringbuf_map_delete_elem, .map_get_next_key = ringbuf_map_get_next_key, + .map_btf_name = "bpf_ringbuf_map", + .map_btf_id = &ringbuf_map_btf_id, };/* Given pointer to ring buffer record metadata and struct bpf_ringbuf itself,
kernel/bpf/stackmap.c +3 -0
@@ -613,6 +613,7 @@ static void stack_map_free(struct bpf_map *map)put_callchain_buffers (); } +static int stack_trace_map_btf_id;const struct bpf_map_ops stack_trace_map_ops = { .map_alloc = stack_map_alloc, .map_free = stack_map_free,
@@ -621,6 +622,8 @@ const struct bpf_map_ops stack_trace_map_ops = { .map_update_elem = stack_map_update_elem, .map_delete_elem = stack_map_delete_elem, .map_check_btf = map_check_no_btf, + .map_btf_name = "bpf_stack_map", + .map_btf_id = &stack_trace_map_btf_id, };static int __init stack_map_init (void )
net/core/bpf_sk_storage.c +3 -0
@@ -919,6 +919,7 @@ BPF_CALL_2(bpf_sk_storage_delete, struct bpf_map *, map, struct sock *, sk)return -ENOENT ; } +static int sk_storage_map_btf_id;const struct bpf_map_ops sk_storage_map_ops = { .map_alloc_check = bpf_sk_storage_map_alloc_check, .map_alloc = bpf_sk_storage_map_alloc,
@@ -928,6 +929,8 @@ const struct bpf_map_ops sk_storage_map_ops = { .map_update_elem = bpf_fd_sk_storage_update_elem, .map_delete_elem = bpf_fd_sk_storage_delete_elem, .map_check_btf = bpf_sk_storage_map_check_btf, + .map_btf_name = "bpf_sk_storage_map", + .map_btf_id = &sk_storage_map_btf_id, };const struct bpf_func_proto bpf_sk_storage_get_proto = {
net/core/sock_map.c +6 -0
@@ -643,6 +643,7 @@ const struct bpf_func_proto bpf_msg_redirect_map_proto = { .arg4_type = ARG_ANYTHING, }; +static int sock_map_btf_id;const struct bpf_map_ops sock_map_ops = { .map_alloc = sock_map_alloc, .map_free = sock_map_free,
@@ -653,6 +654,8 @@ const struct bpf_map_ops sock_map_ops = { .map_lookup_elem = sock_map_lookup, .map_release_uref = sock_map_release_progs, .map_check_btf = map_check_no_btf, + .map_btf_name = "bpf_stab", + .map_btf_id = &sock_map_btf_id, };struct bpf_shtab_elem {
@@ -1176,6 +1179,7 @@ const struct bpf_func_proto bpf_msg_redirect_hash_proto = { .arg4_type = ARG_ANYTHING, }; +static int sock_hash_map_btf_id;const struct bpf_map_ops sock_hash_ops = { .map_alloc = sock_hash_alloc, .map_free = sock_hash_free,
@@ -1186,6 +1190,8 @@ const struct bpf_map_ops sock_hash_ops = { .map_lookup_elem_sys_only = sock_hash_lookup_sys, .map_release_uref = sock_hash_release_progs, .map_check_btf = map_check_no_btf, + .map_btf_name = "bpf_shtab", + .map_btf_id = &sock_hash_map_btf_id, };static struct sk_psock_progs *sock_map_progs (struct bpf_map *map)
net/xdp/xskmap.c +3 -0
@@ -254,6 +254,7 @@ void xsk_map_try_sock_delete(struct xsk_map *map, struct xdp_sock *xs,spin_unlock_bh (&map->lock); } +static int xsk_map_btf_id;const struct bpf_map_ops xsk_map_ops = { .map_alloc = xsk_map_alloc, .map_free = xsk_map_free,
@@ -264,4 +265,6 @@ const struct bpf_map_ops xsk_map_ops = { .map_update_elem = xsk_map_update_elem, .map_delete_elem = xsk_map_delete_elem, .map_check_btf = map_check_no_btf, + .map_btf_name = "xsk_map", + .map_btf_id = &xsk_map_btf_id, };
Signature · 1 line
2.24.1
PATCH v2 5/5 selftests/bpf: Test access to bpf map pointer
In reply to Andrey IgnatovAdd selftests to test access to map pointers from bpf program for all map types except struct_ops (that one would need additional work).
verifier test focuses mostly on scenarios that must be rejected.
prog_tests test focuses on accessing multiple fields both scalar and a nested struct from bpf program and verifies that those fields have expected values.
--- .../selftests/bpf/prog_tests/map_ptr.c | 32 + .../selftests/bpf/progs/map_ptr_kern.c | 686 ++++++++++++++++++ .../testing/selftests/bpf/verifier/map_ptr.c | 62 ++ 3 files changed, 780 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/map_ptr.c create mode 100644 tools/testing/selftests/bpf/progs/map_ptr_kern.c create mode 100644 tools/testing/selftests/bpf/verifier/map_ptr.c
tools/testing/selftests/bpf/prog_tests/map_ptr.c new file +32 -0
@@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2020 Facebook + +#include <test_progs.h> +#include <network_helpers.h> + +#include "map_ptr_kern.skel.h" + +void test_map_ptr (void ) +{ +struct map_ptr_kern *skel; +__u32 duration =0 , retval; +char buf[128 ]; +int err; + + skel =map_ptr_kern__open_and_load (); +if (CHECK (!skel,"skel_open_load" ,"open_load failed\n" )) +return ; + + err =bpf_prog_test_run (bpf_program__fd (skel->progs.cg_skb),1 , &pkt_v4, +sizeof (pkt_v4), buf,NULL , &retval,NULL ); + +if (CHECK (err,"test_run" ,"err=%d errno=%d\n" , err, errno)) + goto cleanup; + +if (CHECK (!retval,"retval" ,"retval=%d map_type=%u line=%u\n" , retval, + skel->bss->g_map_type, skel->bss->g_line)) + goto cleanup; + +cleanup: +map_ptr_kern__destroy (skel); +}
tools/testing/selftests/bpf/progs/map_ptr_kern.c new file +686 -0
686 lines not on this page · raw message
tools/testing/selftests/bpf/verifier/map_ptr.c new file +62 -0
62 lines not on this page · raw message
Signature · 1 line
2.24.1
RE: [PATCH v2 bpf-next 1/5] bpf: Switch btf_parse_vmlinux to btf_find_by_name_kind
In reply to Andrey IgnatovQuoted 11 lines · Andrey Ignatov
btf_parse_vmlinux()implements manual search for structbpf_ctx_convertsince at the time of implementingbtf_find_by_name_kind()was not available.Later
btf_find_by_name_kind()was introduced in27ae7997a661("bpf:
IntroduceBPF_PROG_TYPE_STRUCT_OPS"). It provides similar search functionality and can be leveraged inbtf_parse_vmlinux(). Do it.Signed-off-by Andrey Ignatov <[email protected]>Acked-by Andrii Nakryiko <[email protected]>---
RE: [PATCH v2 bpf-next 2/5] bpf: Rename bpf_htab to bpf_shtab in sock_map
In reply to Andrey IgnatovQuoted 13 lines · Andrey Ignatov
There are two different
struct bpf_htabin bpf code in the following files:
-kernel/bpf/hashtab.c
-net/core/sock_map.cIt makes it impossible to find proper
btf_idby name = "bpf_htab" and kind =BTF_KIND_STRUCTwhat is needed to support access to map ptr so that bpf program can accessstruct bpf_htabfields.To make it possible one of the struct-s should be renamed,
sock_map.clooks like a better candidate for rename since it's specialized version of hashtab.
Quoted 6 lines
Rename it to
bpf_shtab("sh" stands for Sock Hash).Signed-off-by Andrey Ignatov <[email protected]>--- net/core/sock_map.c | 82 ++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 41 deletions(-)
include/linux/bpf.h
Quoted 102 lines · Andrey Ignatov
There are multiple use-cases when it's convenient to have access to bpf map fields, both
struct bpf_mapand map type specific struct-s such asstruct bpf_array,struct bpf_htab, etc.For example while working with sock arrays it can be necessary to calculate the key based on
map->max_entries(some_hash%max_entries). Currently this is solved by communicatingmax_entriesvia "out-of-band" channel, e.g. via additional map with known key to get info about target map. That works, but is not very convenient and error-prone while working with many maps.In other cases necessary data is dynamic (i.e. unknown at loading time) and it's impossible to get it at all. For example while working with a hash table it can be convenient to know how much capacity is already used (bpf_htab.count.counter for
BPF_F_NO_PREALLOCcase).At the same time kernel knows this info and can provide it to bpf program.
Fill this gap by adding support to access bpf map fields from bpf program for both
struct bpf_mapand map type specific fields.Support is implemented via
btf_struct_access()so that a user can define their ownstruct bpf_mapor map type specific struct in their program with only necessary fields andpreserve_access_indexattribute, cast a map to this struct and use a field.For example:
struct bpf_map { __u32 max_entries; } __attribute__((preserve_access_index)); struct bpf_array { struct bpf_map map; __u32 elem_size; } __attribute__((preserve_access_index)); struct { __uint(type, BPF_MAP_TYPE_ARRAY); __uint(max_entries, 4); __type(key, __u32); __type(value, __u32); } m_array SEC(".maps"); SEC("cgroup_skb/egress") int cg_skb(void *ctx) { struct bpf_array *array = (struct bpf_array *)&m_array; struct bpf_map *map = (struct bpf_map *)&m_array; /* .. use map->max_entries or array->map.max_entries .. */ }Similarly to other
btf_struct_access()use-cases (e.g. structtcp_sockinnet/ipv4/bpf_tcp_ca.c) the patch allows access to any fields of corresponding struct. Only reading from map fields is supported.For
btf_struct_access()to work there should be a way to know btf id of a struct that corresponds to a map type. To get btf id there should be a way to get a stringified name of map-specific struct, such as "bpf_array", "bpf_htab", etc for a map type. Two new fields are added tostruct bpf_map_opsto handle it:
* .map_btf_name keeps a btf name of a struct returned bymap_alloc();
* .map_btf_id is used to cache btf id of that struct.To make btf ids calculation cheaper they're calculated once while preparing
btf_vmlinuxand cached same way as it's done forbtf_idfield ofstruct bpf_func_protoWhile calculating btf ids, struct names are NOT checked for collision. Collisions will be checked as a part of the work to prepare btf ids used in verifier in compile time that should land soon. The only known collision for
struct bpf_htab(kernel/bpf/hashtab.cvsnet/core/sock_map.c) was fixed earlier.Both new fields .map_btf_name and .map_btf_id must be set for a map type for the feature to work. If neither is set for a map type, verifier will return ENOTSUPP on a try to access
map_ptrof corresponding type. If just one of them set, it's verifier misconfiguration.Only
struct bpf_arrayforBPF_MAP_TYPE_ARRAYandstruct bpf_htabforBPF_MAP_TYPE_HASHare supported by this patch. Other map types will be supported separately.The feature is available only for
CONFIG_DEBUG_INFO_BTF=yand gated byperfmon_capable()so that unpriv programs won't have access to bpf map fields.Signed-off-by Andrey Ignatov <[email protected]>--- include/linux/bpf.h | 9 ++ include/linux/bpf_verifier.h | 1 + kernel/bpf/arraymap.c | 3 + kernel/bpf/btf.c | 40 +++++++++ kernel/bpf/hashtab.c | 3 + kernel/bpf/verifier.c | 82 +++++++++++++++++-- .../selftests/bpf/verifier/map_ptr_mixing.c | 2 +- 7 files changed, 131 insertions(+), 9 deletions(-)include/linux/bpf.h +0
-0
LGTM, but any reason not to allow this with bpf_capable() it looks useful for building load balancers which might not be related to CAP_PERFMON.
Otherwise,
RE: [PATCH v2 bpf-next 4/5] bpf: Set map_btf_{name,id} for all map types
In reply to Andrey IgnatovQuoted 5 lines · Andrey Ignatov
Set
map_btf_nameandmap_btf_idfor all map types so that map fields can be accessed by bpf programs.Signed-off-by Andrey Ignatov <[email protected]>---
RE: [PATCH v2 bpf-next 5/5] selftests/bpf: Test access to bpf map pointer
In reply to Andrey IgnatovQuoted 11 lines · Andrey Ignatov
Add selftests to test access to map pointers from bpf program for all map types except
struct_ops(that one would need additional work).verifier test focuses mostly on scenarios that must be rejected.
prog_teststest focuses on accessing multiple fields both scalar and a nested struct from bpf program and verifies that those fields have expected values.Signed-off-by Andrey Ignatov <[email protected]>---
Quoted 10 lines · Andrey Ignatov
v1->v2:
- move btf id cache to a new bpf_map_ops.map_btf_id field (Martin, Andrii);
- don't check btf names for collisions (Martin);
- dropbtf_find_by_name_kind_next()patch since it was needed only for collision check;
- don't fall back tostruct bpf_mapif a map type doesn't specify bothmap_btf_nameandmap_btf_id;This patch set adds support to access bpf map fields from bpf programs using
btf_struct_access().
John Fastabend <[email protected]> [Sat, 2020-06-20 20:27 -0700]:
Quoted 1 line
Andrey Ignatov wrote:
...
Quoted 16 lines
Quoted 16 lines
The feature is available only for
CONFIG_DEBUG_INFO_BTF=yand gated byperfmon_capable()so that unpriv programs won't have access to bpf map fields.Signed-off-by Andrey Ignatov <[email protected]>--- include/linux/bpf.h | 9 ++ include/linux/bpf_verifier.h | 1 + kernel/bpf/arraymap.c | 3 + kernel/bpf/btf.c | 40 +++++++++ kernel/bpf/hashtab.c | 3 + kernel/bpf/verifier.c | 82 +++++++++++++++++-- .../selftests/bpf/verifier/map_ptr_mixing.c | 2 +- 7 files changed, 131 insertions(+), 9 deletions(-)
Quoted 6 lines
include/linux/bpf.h
Quoted 2 lines
include/linux/bpf.h +0
-0LGTM, but any reason not to allow this with
bpf_capable()it looks useful for building load balancers which might not be related toCAP_PERFMON.
Thanks for review John. I agree that this can be useful for many use-cases, incl. networking programs.
Accessing a kernel struct looks like "tracing" kind of functionality to me (that's why CAP_PERFMON), but I'm not quite sure, and using bpf_capable() looks fine as well.
Alexei, since you introduced CAP_BPF, could you clarify which cap is the right one to use here and why?
Quoted 3 lines
Otherwise,
Acked-by John Fastabend <[email protected]>
Signature · 1 line
Andrey Ignatov
Quoted 20 lines · Andrey Ignatov
v1->v2:
- move btf id cache to a new bpf_map_ops.map_btf_id field (Martin, Andrii);
- don't check btf names for collisions (Martin);
- dropbtf_find_by_name_kind_next()patch since it was needed only for collision check;
- don't fall back tostruct bpf_mapif a map type doesn't specify bothmap_btf_nameandmap_btf_id;This patch set adds support to access bpf map fields from bpf programs using
btf_struct_access().That way a program can cast a pointer to map to either
struct bpf_map *or map type specific struct pointer such asstruct bpf_array *orstruct bpf_htab *, and access necessary fields, e.g.map->max_entries.The fields, in turn, should be defined by a user provided struct with
preserve_access_indexattribute or included fromvmlinux.h.Please see patch 3 for more details on the feature and use-cases.
Quoted 8 lines
Other patches:
Patch 1 is refactoring to simplify
btf_parse_vmlinux(). Patch 2 is a rename to avoid having two differentstruct bpf_htab.Patch 4 enables access to map fields for all map types. Patch 5 adds selftests.
Applied, thanks!
Quoted 29 lines · Andrey Ignatov
John Fastabend <[email protected]> [Sat, 2020-06-20 20:27 -0700]:
Quoted 1 line
Andrey Ignatov wrote:
...
Quoted 16 lines
Quoted 16 lines
The feature is available only for
CONFIG_DEBUG_INFO_BTF=yand gated byperfmon_capable()so that unpriv programs won't have access to bpf map fields.Signed-off-by Andrey Ignatov <[email protected]>--- include/linux/bpf.h | 9 ++ include/linux/bpf_verifier.h | 1 + kernel/bpf/arraymap.c | 3 + kernel/bpf/btf.c | 40 +++++++++ kernel/bpf/hashtab.c | 3 + kernel/bpf/verifier.c | 82 +++++++++++++++++-- .../selftests/bpf/verifier/map_ptr_mixing.c | 2 +- 7 files changed, 131 insertions(+), 9 deletions(-)Quoted 6 lines
include/linux/bpf.h
Quoted 2 lines
include/linux/bpf.h +0
-0LGTM, but any reason not to allow this with
bpf_capable()it looks useful for building load balancers which might not be related toCAP_PERFMON.Thanks for review John. I agree that this can be useful for many use-cases, incl. networking programs.
Quoted 6 lines
Accessing a kernel struct looks like "tracing" kind of functionality to me (that's why
CAP_PERFMON), but I'm not quite sure, and usingbpf_capable()looks fine as well.Alexei, since you introduced
CAP_BPF, could you clarify which cap is the right one to use here and why?
It's tracing and pointers are accessed, so cap_perfmon.