bpf: Support access to bpf map fields
Subsystems (11)
cover bpf: Support access to bpf map fields
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 4 for more details on the feature and use-cases.
Other patches:
Patch 1 is refactoring to simplify btf_parse_vmlinux(). Patch 2 introduces a new function to simplify iteration over btf. Patch 3 is a rename to avoid having two different struct bpf_htab.
Patch 5 enables access to map fields for all map types. Patch 6 adds selftests.
Andrey Ignatov (6):
bpf: Switch btf_parse_vmlinux to btf_find_by_name_kind
bpf: Introduce btf_find_by_name_kind_next()
bpf: Rename bpf_htab to bpf_shtab in sock_map
bpf: Support access to bpf map fields
bpf: Set map_btf_name for all map types selftests/bpf: Test access to bpf map pointer
include/linux/bpf.h | 8 + include/linux/bpf_verifier.h | 1 + include/linux/btf.h | 2 + kernel/bpf/arraymap.c | 6 + kernel/bpf/bpf_struct_ops.c | 1 + kernel/bpf/btf.c | 97 ++- kernel/bpf/cpumap.c | 1 + kernel/bpf/devmap.c | 2 + kernel/bpf/hashtab.c | 5 + kernel/bpf/local_storage.c | 1 + kernel/bpf/lpm_trie.c | 1 + kernel/bpf/queue_stack_maps.c | 2 + kernel/bpf/reuseport_array.c | 1 + kernel/bpf/ringbuf.c | 1 + kernel/bpf/stackmap.c | 1 + kernel/bpf/verifier.c | 77 +- net/core/bpf_sk_storage.c | 1 + net/core/sock_map.c | 84 +-- net/xdp/xskmap.c | 1 + .../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 +- 23 files changed, 1007 insertions(+), 68 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 1/6 bpf: Switch btf_parse_vmlinux to btf_find_by_name_kind
btf_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
PATCH 3/6 bpf: Rename bpf_htab to bpf_shtab in sock_map
In reply to Andrey IgnatovThere 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 4/6 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. Such a stringified name is kept in a new field bpf_map_ops.map_btf_name, btf ids are calculated once while preparing btf_vmlinux and cached for future use.
While calculating btf ids, struct names are checked for collision so that btf_parse_vmlinux() will fail if two map types use different map struct-s with same name. The only known collision for struct bpf_htab (kernel/bpf/hashtab.c vs net/core/sock_map.c) was fixed earlier.
If map_btf_name field is not set for a map type, struct bpf_map is used by default and only common fields are available for this map type. 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 | 8 ++ include/linux/bpf_verifier.h | 1 + kernel/bpf/arraymap.c | 1 + kernel/bpf/btf.c | 66 ++++++++++++++++ kernel/bpf/hashtab.c | 1 + kernel/bpf/verifier.c | 77 +++++++++++++++++-- .../selftests/bpf/verifier/map_ptr_mixing.c | 2 +- 7 files changed, 147 insertions(+), 9 deletions(-)
include/linux/bpf.h +8 -0
@@ -92,6 +92,7 @@ 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); +const char *const map_btf_name; };struct bpf_map_memory {
@@ -136,6 +137,8 @@ struct bpf_map {u64 writecnt;/* writable mmap cnt; protected by freeze_mutex */ }; +u32 btf_vmlinux_map_id_by_type (enum bpf_map_type type); +static inline bool map_value_has_spin_lock (const struct bpf_map *map) {return map->spin_lock_off >=0 ;
@@ -1109,6 +1112,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 +1 -0
@@ -510,6 +510,7 @@ 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", };const struct bpf_map_ops percpu_array_map_ops = {
kernel/bpf/btf.c +66 -0
78 lines not on this page · raw message
kernel/bpf/hashtab.c +1 -0
@@ -1625,6 +1625,7 @@ 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", }; const struct bpf_map_ops htab_lru_map_ops = {
kernel/bpf/verifier.c +69 -8
106 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 5/6 bpf: Set map_btf_name for all map types
In reply to Andrey IgnatovSet map_btf_name for all map types so that map type specific fields (those beyond struct bpf_map) can be accessed by bpf programs.
--- kernel/bpf/arraymap.c | 5 +++++ kernel/bpf/bpf_struct_ops.c | 1 + kernel/bpf/cpumap.c | 1 + kernel/bpf/devmap.c | 2 ++ kernel/bpf/hashtab.c | 4 ++++ kernel/bpf/local_storage.c | 1 + kernel/bpf/lpm_trie.c | 1 + kernel/bpf/queue_stack_maps.c | 2 ++ kernel/bpf/reuseport_array.c | 1 + kernel/bpf/ringbuf.c | 1 + kernel/bpf/stackmap.c | 1 + net/core/bpf_sk_storage.c | 1 + net/core/sock_map.c | 2 ++ net/xdp/xskmap.c | 1 + 14 files changed, 24 insertions(+)
kernel/bpf/arraymap.c +5 -0
@@ -523,6 +523,7 @@ 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", };static int fd_array_map_alloc_check (union bpf_attr *attr)
@@ -884,6 +885,7 @@ 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", }; static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
@@ -973,6 +975,7 @@ 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", };#ifdef CONFIG_CGROUPS
@@ -1005,6 +1008,7 @@ 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", };#endif
@@ -1090,4 +1094,5 @@ 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", };
kernel/bpf/bpf_struct_ops.c +1 -0
@@ -620,6 +620,7 @@ 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", };/* "const void *" because some subsystem is
kernel/bpf/cpumap.c +1 -0
@@ -551,6 +551,7 @@ 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", };static int bq_flush_to_queue (struct xdp_bulk_queue *bq)
kernel/bpf/devmap.c +2 -0
@@ -755,6 +755,7 @@ 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", };const struct bpf_map_ops dev_map_hash_ops = {
@@ -765,6 +766,7 @@ 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", }; static void dev_map_hash_remove_netdev(struct bpf_dtab *dtab,
kernel/bpf/hashtab.c +4 -0
@@ -1640,6 +1640,7 @@ 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", };/* Called from eBPF program */
@@ -1754,6 +1755,7 @@ 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", }; const struct bpf_map_ops htab_lru_percpu_map_ops = {
@@ -1766,6 +1768,7 @@ 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", };static int fd_htab_map_alloc_check (union bpf_attr *attr)
@@ -1900,4 +1903,5 @@ 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", };
kernel/bpf/local_storage.c +1 -0
@@ -418,6 +418,7 @@ 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", };int bpf_cgroup_storage_assign (struct bpf_prog_aux *aux,struct bpf_map *_map)
kernel/bpf/lpm_trie.c +1 -0
@@ -743,4 +743,5 @@ 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", };
kernel/bpf/queue_stack_maps.c +2 -0
@@ -273,6 +273,7 @@ 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", };const struct bpf_map_ops stack_map_ops = {
@@ -286,4 +287,5 @@ 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", };
kernel/bpf/reuseport_array.c +1 -0
@@ -352,4 +352,5 @@ 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", };
kernel/bpf/ringbuf.c +1 -0
@@ -303,6 +303,7 @@ 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", };/* Given pointer to ring buffer record metadata and struct bpf_ringbuf itself,
kernel/bpf/stackmap.c +1 -0
@@ -621,6 +621,7 @@ 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", };static int __init stack_map_init (void )
net/core/bpf_sk_storage.c +1 -0
@@ -895,6 +895,7 @@ 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", };const struct bpf_func_proto bpf_sk_storage_get_proto = {
net/core/sock_map.c +2 -0
@@ -653,6 +653,7 @@ 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", };struct bpf_shtab_elem {
@@ -1186,6 +1187,7 @@ 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", };static struct sk_psock_progs *sock_map_progs (struct bpf_map *map)
net/xdp/xskmap.c +1 -0
@@ -264,4 +264,5 @@ 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", };
Signature · 1 line
2.24.1
PATCH 6/6 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
PATCH 2/6 bpf: Introduce btf_find_by_name_kind_next()
In reply to Andrey IgnatovIntroduce btf_find_by_name_kind_next() function to find btf_id by name and kind starting from specified start_id to reuse the function in finding duplicates (e.g. structs with same name) in btf.
--- include/linux/btf.h | 2 ++ kernel/bpf/btf.c | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-)
include/linux/btf.h +2 -0
@@ -56,6 +56,8 @@ bool btf_member_is_reg_int(const struct btf *btf, const struct btf_type *s,int btf_find_spin_lock (const struct btf *btf,const struct btf_type *t);bool btf_type_is_void (const struct btf_type *t);s32 btf_find_by_name_kind (const struct btf *btf,const char *name,u8 kind); +s32 btf_find_by_name_kind_next (const struct btf *btf,const char *name,u8 kind, +u32 start_id);const struct btf_type *btf_type_skip_modifiers (const struct btf *btf,u32 id,u32 *res_id); const struct btf_type *btf_type_resolve_ptr(const struct btf *btf,
kernel/bpf/btf.c +7 -1
@@ -383,12 +383,18 @@ static bool btf_type_is_datasec(const struct btf_type *t) }s32 btf_find_by_name_kind (const struct btf *btf,const char *name,u8 kind) +{ +return btf_find_by_name_kind_next (btf, name, kind,1 ); +} + +s32 btf_find_by_name_kind_next (const struct btf *btf,const char *name,u8 kind, +u32 start_id) {const struct btf_type *t;const char *tname;u32 i; -for (i =1 ; i <= btf->nr_types; i++) { +for (i = start_id; i <= btf->nr_types; i++) { t = btf->types[i];if (BTF_INFO_KIND (t->info) != kind)continue ;
Signature · 1 line
2.24.1
On Wed, Jun 17, 2020 at 01:43:45PM -0700, Andrey Ignatov wrote:
[ ... ]
Quoted 40 lines
kernel/bpf/btf.c +32
-0@@ -3577,6 +3577,67 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf,return ctx_type; } +#define BPF_PROG_TYPE (_id, _name, prog_ctx_type, kern_ctx_type) +#define BPF_LINK_TYPE (_id, _name) +static const struct bpf_map_ops *const btf_vmlinux_map_ops[] = { +#define BPF_MAP_TYPE(_id, _ops) \ + [_id] = &_ops, +#include <linux/bpf_types.h> +#undef BPF_MAP_TYPE +}; +static u32 btf_vmlinux_map_ids[] = { +#define BPF_MAP_TYPE(_id, _ops) \ + [_id] = (u32)-1 , +#include <linux/bpf_types.h> +#undef BPF_MAP_TYPE +}; +#undef BPF_PROG_TYPE +#undef BPF_LINK_TYPE + +static int btf_vmlinux_map_ids_init (const struct btf *btf, +struct bpf_verifier_log *log) +{ +int base_btf_id, btf_id, i; +const char *btf_name; + + base_btf_id =btf_find_by_name_kind (btf,"bpf_map" ,BTF_KIND_STRUCT ); +if (base_btf_id <0 ) +return base_btf_id; + +BUILD_BUG_ON (ARRAY_SIZE (btf_vmlinux_map_ops) != +ARRAY_SIZE (btf_vmlinux_map_ids)); + +for (i =0 ; i <ARRAY_SIZE (btf_vmlinux_map_ops); ++i) { +if (!btf_vmlinux_map_ops[i])
kernel/bpf/btf.c btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf, · line 3621
Quoted 10 lines
kernel/bpf/btf.c +10
-0+continue ; + btf_name = btf_vmlinux_map_ops[i]->map_btf_name; +if (!btf_name) { + btf_vmlinux_map_ids[i] = base_btf_id; +continue ; + } + btf_id =btf_find_by_name_kind (btf, btf_name,BTF_KIND_STRUCT ); +if (btf_id <0 ) +return btf_id; + btf_vmlinux_map_ids[i] = btf_id;
Since map_btf_name is already in map_ops, how about storing map_btf_id in map_ops also?btf_id 0 is "void" which is as good as -1, so there is no need to modify all map_ops to init map_btf_id to -1.
Quoted 14 lines
kernel/bpf/btf.c +14
-0+ btf_id =btf_find_by_name_kind_next (btf, btf_name, +BTF_KIND_STRUCT , + btf_id +1 ); +if (btf_id >0 ) { +bpf_log (log, +"Ambiguous btf_id for struct %s: %u, %u.\n" , + btf_name, btf_vmlinux_map_ids[i], btf_id); +return -EINVAL ; + } + } + +return 0 ; +} +
Martin KaFai Lau <[email protected]> [Wed, 2020-06-17 23:18 -0700]:
Quoted 48 lines · Andrey Ignatov
[ ... ]
Quoted 46 lines
kernel/bpf/btf.c +38
-0@@ -3577,6 +3577,67 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf,return ctx_type; } +#define BPF_PROG_TYPE (_id, _name, prog_ctx_type, kern_ctx_type) +#define BPF_LINK_TYPE (_id, _name) +static const struct bpf_map_ops *const btf_vmlinux_map_ops[] = { +#define BPF_MAP_TYPE(_id, _ops) \ + [_id] = &_ops, +#include <linux/bpf_types.h> +#undef BPF_MAP_TYPE +}; +static u32 btf_vmlinux_map_ids[] = { +#define BPF_MAP_TYPE(_id, _ops) \ + [_id] = (u32)-1 , +#include <linux/bpf_types.h> +#undef BPF_MAP_TYPE +}; +#undef BPF_PROG_TYPE +#undef BPF_LINK_TYPE + +static int btf_vmlinux_map_ids_init (const struct btf *btf, +struct bpf_verifier_log *log) +{ +int base_btf_id, btf_id, i; +const char *btf_name; + + base_btf_id =btf_find_by_name_kind (btf,"bpf_map" ,BTF_KIND_STRUCT ); +if (base_btf_id <0 ) +return base_btf_id; + +BUILD_BUG_ON (ARRAY_SIZE (btf_vmlinux_map_ops) != +ARRAY_SIZE (btf_vmlinux_map_ids)); + +for (i =0 ; i <ARRAY_SIZE (btf_vmlinux_map_ops); ++i) { +if (!btf_vmlinux_map_ops[i]) +continue ; + btf_name = btf_vmlinux_map_ops[i]->map_btf_name; +if (!btf_name) { + btf_vmlinux_map_ids[i] = base_btf_id; +continue ; + }
Quoted 8 lines
kernel/bpf/btf.c btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf, · line 3621
Quoted 4 lines
kernel/bpf/btf.c +4
-0+ btf_id =btf_find_by_name_kind (btf, btf_name,BTF_KIND_STRUCT ); +if (btf_id <0 ) +return btf_id; + btf_vmlinux_map_ids[i] = btf_id;Since
map_btf_nameis already inmap_ops, how about storingmap_btf_idinmap_opsalso?btf_id0 is "void" which is as good as -1, so there is no need to modify allmap_opsto initmap_btf_idto -1.
Yeah, btf_id == 0 being a valid id was the reason I used -1.
I realized that having a map type specific struct with btf_id == 0 should be practically impossible, but is it guaranteed to always be "void" or it just happened so and can change in the future?
If both this and having one more field in bpf_map_ops is not a problem, I'll move it to bpf_map_ops.
Signature · 1 line
Andrey Ignatov
Quoted 58 lines · Andrey Ignatov
Martin KaFai Lau <[email protected]> [Wed, 2020-06-17 23:18 -0700]:
Quoted 48 lines · Andrey Ignatov
[ ... ]
Quoted 46 lines
kernel/bpf/btf.c +38
-0@@ -3577,6 +3577,67 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf,return ctx_type; } +#define BPF_PROG_TYPE (_id, _name, prog_ctx_type, kern_ctx_type) +#define BPF_LINK_TYPE (_id, _name) +static const struct bpf_map_ops *const btf_vmlinux_map_ops[] = { +#define BPF_MAP_TYPE(_id, _ops) \ + [_id] = &_ops, +#include <linux/bpf_types.h> +#undef BPF_MAP_TYPE +}; +static u32 btf_vmlinux_map_ids[] = { +#define BPF_MAP_TYPE(_id, _ops) \ + [_id] = (u32)-1 , +#include <linux/bpf_types.h> +#undef BPF_MAP_TYPE +}; +#undef BPF_PROG_TYPE +#undef BPF_LINK_TYPE + +static int btf_vmlinux_map_ids_init (const struct btf *btf, +struct bpf_verifier_log *log) +{ +int base_btf_id, btf_id, i; +const char *btf_name; + + base_btf_id =btf_find_by_name_kind (btf,"bpf_map" ,BTF_KIND_STRUCT ); +if (base_btf_id <0 ) +return base_btf_id; + +BUILD_BUG_ON (ARRAY_SIZE (btf_vmlinux_map_ops) != +ARRAY_SIZE (btf_vmlinux_map_ids)); + +for (i =0 ; i <ARRAY_SIZE (btf_vmlinux_map_ops); ++i) { +if (!btf_vmlinux_map_ops[i]) +continue ; + btf_name = btf_vmlinux_map_ops[i]->map_btf_name; +if (!btf_name) { + btf_vmlinux_map_ids[i] = base_btf_id; +continue ; + }Quoted 8 lines
kernel/bpf/btf.c btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf, · line 3621
Quoted 4 lines
kernel/bpf/btf.c +4
-0+ btf_id =btf_find_by_name_kind (btf, btf_name,BTF_KIND_STRUCT ); +if (btf_id <0 ) +return btf_id; + btf_vmlinux_map_ids[i] = btf_id;Since
map_btf_nameis already inmap_ops, how about storingmap_btf_idinmap_opsalso?btf_id0 is "void" which is as good as -1, so there is no need to modify allmap_opsto initmap_btf_idto -1.
Quoted 5 lines
Yeah,
btf_id== 0 being a valid id was the reason I used -1.I realized that having a map type specific struct with
btf_id== 0 should be practically impossible, but is it guaranteed to always be "void" or it just happened so and can change in the future?
It is always "void" and cannot be changed in the future.
Andrey Ignatov <[email protected]> [Thu, 2020-06-18 12:42 -0700]:
Quoted 58 lines
Martin KaFai Lau <[email protected]> [Wed, 2020-06-17 23:18 -0700]:
Quoted 48 lines · Andrey Ignatov
[ ... ]
Quoted 46 lines
kernel/bpf/btf.c +38
-0@@ -3577,6 +3577,67 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf,return ctx_type; } +#define BPF_PROG_TYPE (_id, _name, prog_ctx_type, kern_ctx_type) +#define BPF_LINK_TYPE (_id, _name) +static const struct bpf_map_ops *const btf_vmlinux_map_ops[] = { +#define BPF_MAP_TYPE(_id, _ops) \ + [_id] = &_ops, +#include <linux/bpf_types.h> +#undef BPF_MAP_TYPE +}; +static u32 btf_vmlinux_map_ids[] = { +#define BPF_MAP_TYPE(_id, _ops) \ + [_id] = (u32)-1 , +#include <linux/bpf_types.h> +#undef BPF_MAP_TYPE +}; +#undef BPF_PROG_TYPE +#undef BPF_LINK_TYPE + +static int btf_vmlinux_map_ids_init (const struct btf *btf, +struct bpf_verifier_log *log) +{ +int base_btf_id, btf_id, i; +const char *btf_name; + + base_btf_id =btf_find_by_name_kind (btf,"bpf_map" ,BTF_KIND_STRUCT ); +if (base_btf_id <0 ) +return base_btf_id; + +BUILD_BUG_ON (ARRAY_SIZE (btf_vmlinux_map_ops) != +ARRAY_SIZE (btf_vmlinux_map_ids)); + +for (i =0 ; i <ARRAY_SIZE (btf_vmlinux_map_ops); ++i) { +if (!btf_vmlinux_map_ops[i]) +continue ; + btf_name = btf_vmlinux_map_ops[i]->map_btf_name; +if (!btf_name) { + btf_vmlinux_map_ids[i] = base_btf_id; +continue ; + }Quoted 8 lines
kernel/bpf/btf.c btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf, · line 3621
Quoted 4 lines
kernel/bpf/btf.c +4
-0+ btf_id =btf_find_by_name_kind (btf, btf_name,BTF_KIND_STRUCT ); +if (btf_id <0 ) +return btf_id; + btf_vmlinux_map_ids[i] = btf_id;Since
map_btf_nameis already inmap_ops, how about storingmap_btf_idinmap_opsalso?btf_id0 is "void" which is as good as -1, so there is no need to modify allmap_opsto initmap_btf_idto -1.
Quoted 8 lines
Yeah,
btf_id== 0 being a valid id was the reason I used -1.I realized that having a map type specific struct with
btf_id== 0 should be practically impossible, but is it guaranteed to always be "void" or it just happened so and can change in the future?If both this and having one more field in
bpf_map_opsis not a problem, I'll move it tobpf_map_ops.
Nope, I can't do it. All struct bpf_map_ops are global const, i.e. rodata and a try cast const away and change them causes a panic.
Simple user space repro:
% cat 1.c
#include <stdio.h>
struct map_ops {
int a;
};
const struct map_ops ops = {
.a = 1,
};
int main(void)
{
struct map_ops *ops_rw = (struct map_ops *)&ops;
printf("before a=%d\n", ops_rw->a);
ops_rw->a = 3;
printf(" afrer a=%d\n", ops_rw->a);
}
% clang -O2 -Wall -Wextra -pedantic -pedantic-errors -g 1.c && ./a.out
before a=1
Segmentation fault (core dumped)
% objdump -t a.out | grep -w ops
0000000000400600 g O .rodata 0000000000000004 opsSignature · 1 line
Andrey Ignatov
Quoted 99 lines · Andrey Ignatov
Andrey Ignatov <[email protected]> [Thu, 2020-06-18 12:42 -0700]:
Quoted 58 lines
Martin KaFai Lau <[email protected]> [Wed, 2020-06-17 23:18 -0700]:
Quoted 48 lines · Andrey Ignatov
[ ... ]
Quoted 46 lines
kernel/bpf/btf.c +38
-0@@ -3577,6 +3577,67 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf,return ctx_type; } +#define BPF_PROG_TYPE (_id, _name, prog_ctx_type, kern_ctx_type) +#define BPF_LINK_TYPE (_id, _name) +static const struct bpf_map_ops *const btf_vmlinux_map_ops[] = { +#define BPF_MAP_TYPE(_id, _ops) \ + [_id] = &_ops, +#include <linux/bpf_types.h> +#undef BPF_MAP_TYPE +}; +static u32 btf_vmlinux_map_ids[] = { +#define BPF_MAP_TYPE(_id, _ops) \ + [_id] = (u32)-1 , +#include <linux/bpf_types.h> +#undef BPF_MAP_TYPE +}; +#undef BPF_PROG_TYPE +#undef BPF_LINK_TYPE + +static int btf_vmlinux_map_ids_init (const struct btf *btf, +struct bpf_verifier_log *log) +{ +int base_btf_id, btf_id, i; +const char *btf_name; + + base_btf_id =btf_find_by_name_kind (btf,"bpf_map" ,BTF_KIND_STRUCT ); +if (base_btf_id <0 ) +return base_btf_id; + +BUILD_BUG_ON (ARRAY_SIZE (btf_vmlinux_map_ops) != +ARRAY_SIZE (btf_vmlinux_map_ids)); + +for (i =0 ; i <ARRAY_SIZE (btf_vmlinux_map_ops); ++i) { +if (!btf_vmlinux_map_ops[i]) +continue ; + btf_name = btf_vmlinux_map_ops[i]->map_btf_name; +if (!btf_name) { + btf_vmlinux_map_ids[i] = base_btf_id; +continue ; + }Quoted 8 lines
kernel/bpf/btf.c btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf, · line 3621
Quoted 4 lines
kernel/bpf/btf.c +4
-0+ btf_id =btf_find_by_name_kind (btf, btf_name,BTF_KIND_STRUCT ); +if (btf_id <0 ) +return btf_id; + btf_vmlinux_map_ids[i] = btf_id;Since
map_btf_nameis already inmap_ops, how about storingmap_btf_idinmap_opsalso?btf_id0 is "void" which is as good as -1, so there is no need to modify allmap_opsto initmap_btf_idto -1.Quoted 8 lines
Yeah,
btf_id== 0 being a valid id was the reason I used -1.I realized that having a map type specific struct with
btf_id== 0 should be practically impossible, but is it guaranteed to always be "void" or it just happened so and can change in the future?If both this and having one more field in
bpf_map_opsis not a problem, I'll move it tobpf_map_ops.Nope, I can't do it. All
struct bpf_map_opsare globalconst, i.e. rodata and a try castconstaway and change them causes a panic.Simple user space repro:
% cat 1.c #include <stdio.h> struct map_ops { int a; }; const struct map_ops ops = { .a = 1, }; int main(void) { struct map_ops *ops_rw = (struct map_ops *)&ops; printf("before a=%d\n", ops_rw->a); ops_rw->a = 3; printf(" afrer a=%d\n", ops_rw->a); } % clang -O2 -Wall -Wextra -pedantic -pedantic-errors -g 1.c && ./a.out before a=1 Segmentation fault (core dumped) % objdump -t a.out | grep -w ops 0000000000400600 g O .rodata 0000000000000004 ops
Quoted 2 lines
--
Andrey Ignatov
See the trick that helper prototypes do for BTF ids. Fictional example:
static int hash_map_btf_id;
const struct bpf_map_ops hash_map_opss = {
...
.btf_id = &hash_map_btf_id,
};
then *hash_map_ops.btf_id = <final_btf_id>;
Andrii Nakryiko <[email protected]> [Thu, 2020-06-18 17:08 -0700]:
Quoted 107 lines · Andrey Ignatov
Quoted 99 lines
Andrey Ignatov <[email protected]> [Thu, 2020-06-18 12:42 -0700]:
Quoted 58 lines
Martin KaFai Lau <[email protected]> [Wed, 2020-06-17 23:18 -0700]:
Quoted 48 lines · Andrey Ignatov
[ ... ]
Quoted 46 lines
kernel/bpf/btf.c +38
-0@@ -3577,6 +3577,67 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf,return ctx_type; } +#define BPF_PROG_TYPE (_id, _name, prog_ctx_type, kern_ctx_type) +#define BPF_LINK_TYPE (_id, _name) +static const struct bpf_map_ops *const btf_vmlinux_map_ops[] = { +#define BPF_MAP_TYPE(_id, _ops) \ + [_id] = &_ops, +#include <linux/bpf_types.h> +#undef BPF_MAP_TYPE +}; +static u32 btf_vmlinux_map_ids[] = { +#define BPF_MAP_TYPE(_id, _ops) \ + [_id] = (u32)-1 , +#include <linux/bpf_types.h> +#undef BPF_MAP_TYPE +}; +#undef BPF_PROG_TYPE +#undef BPF_LINK_TYPE + +static int btf_vmlinux_map_ids_init (const struct btf *btf, +struct bpf_verifier_log *log) +{ +int base_btf_id, btf_id, i; +const char *btf_name; + + base_btf_id =btf_find_by_name_kind (btf,"bpf_map" ,BTF_KIND_STRUCT ); +if (base_btf_id <0 ) +return base_btf_id; + +BUILD_BUG_ON (ARRAY_SIZE (btf_vmlinux_map_ops) != +ARRAY_SIZE (btf_vmlinux_map_ids)); + +for (i =0 ; i <ARRAY_SIZE (btf_vmlinux_map_ops); ++i) { +if (!btf_vmlinux_map_ops[i]) +continue ; + btf_name = btf_vmlinux_map_ops[i]->map_btf_name; +if (!btf_name) { + btf_vmlinux_map_ids[i] = base_btf_id; +continue ; + }Quoted 8 lines
kernel/bpf/btf.c btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf, · line 3621
Quoted 4 lines
kernel/bpf/btf.c +4
-0+ btf_id =btf_find_by_name_kind (btf, btf_name,BTF_KIND_STRUCT ); +if (btf_id <0 ) +return btf_id; + btf_vmlinux_map_ids[i] = btf_id;Since
map_btf_nameis already inmap_ops, how about storingmap_btf_idinmap_opsalso?btf_id0 is "void" which is as good as -1, so there is no need to modify allmap_opsto initmap_btf_idto -1.Quoted 8 lines
Yeah,
btf_id== 0 being a valid id was the reason I used -1.I realized that having a map type specific struct with
btf_id== 0 should be practically impossible, but is it guaranteed to always be "void" or it just happened so and can change in the future?If both this and having one more field in
bpf_map_opsis not a problem, I'll move it tobpf_map_ops.Nope, I can't do it. All
struct bpf_map_opsare globalconst, i.e. rodata and a try castconstaway and change them causes a panic.Simple user space repro:
% cat 1.c #include <stdio.h> struct map_ops { int a; }; const struct map_ops ops = { .a = 1, }; int main(void) { struct map_ops *ops_rw = (struct map_ops *)&ops; printf("before a=%d\n", ops_rw->a); ops_rw->a = 3; printf(" afrer a=%d\n", ops_rw->a); } % clang -O2 -Wall -Wextra -pedantic -pedantic-errors -g 1.c && ./a.out before a=1 Segmentation fault (core dumped) % objdump -t a.out | grep -w ops 0000000000400600 g O .rodata 0000000000000004 opsQuoted 2 lines
--
Andrey IgnatovSee the trick that helper prototypes do for BTF ids. Fictional example:
static int
hash_map_btf_id;
Quoted 7 lines
const struct
bpf_map_opshash_map_opss= {
...
.btf_id =&hash_map_btf_id,
};then *hash_map_ops.btf_id = <
final_btf_id>;
Yeah, it would work, but IMO it wouldn't make the implementation better since for every map type I would need to write two additional lines of code. And whoever adds new map type will need to repeat this trick.
Yeah, it can be automated with a macro, but IMO it's better to avoid the work than to automate it.
Martin, Andrii is there any strong reason to convert to map_btf_id field? Or it's "nice to have" kind of thing? If the latter, I'd prefer to stick with my approach.
Signature · 1 line
Andrey Ignatov
Quoted 114 lines · Andrey Ignatov
Andrii Nakryiko <[email protected]> [Thu, 2020-06-18 17:08 -0700]:
Quoted 113 lines · Andrey Ignatov
Quoted 99 lines
Andrey Ignatov <[email protected]> [Thu, 2020-06-18 12:42 -0700]:
Quoted 58 lines
Martin KaFai Lau <[email protected]> [Wed, 2020-06-17 23:18 -0700]:
Quoted 48 lines · Andrey Ignatov
[ ... ]
Quoted 46 lines
kernel/bpf/btf.c +38
-0@@ -3577,6 +3577,67 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf,return ctx_type; } +#define BPF_PROG_TYPE (_id, _name, prog_ctx_type, kern_ctx_type) +#define BPF_LINK_TYPE (_id, _name) +static const struct bpf_map_ops *const btf_vmlinux_map_ops[] = { +#define BPF_MAP_TYPE(_id, _ops) \ + [_id] = &_ops, +#include <linux/bpf_types.h> +#undef BPF_MAP_TYPE +}; +static u32 btf_vmlinux_map_ids[] = { +#define BPF_MAP_TYPE(_id, _ops) \ + [_id] = (u32)-1 , +#include <linux/bpf_types.h> +#undef BPF_MAP_TYPE +}; +#undef BPF_PROG_TYPE +#undef BPF_LINK_TYPE + +static int btf_vmlinux_map_ids_init (const struct btf *btf, +struct bpf_verifier_log *log) +{ +int base_btf_id, btf_id, i; +const char *btf_name; + + base_btf_id =btf_find_by_name_kind (btf,"bpf_map" ,BTF_KIND_STRUCT ); +if (base_btf_id <0 ) +return base_btf_id; + +BUILD_BUG_ON (ARRAY_SIZE (btf_vmlinux_map_ops) != +ARRAY_SIZE (btf_vmlinux_map_ids)); + +for (i =0 ; i <ARRAY_SIZE (btf_vmlinux_map_ops); ++i) { +if (!btf_vmlinux_map_ops[i]) +continue ; + btf_name = btf_vmlinux_map_ops[i]->map_btf_name; +if (!btf_name) { + btf_vmlinux_map_ids[i] = base_btf_id; +continue ; + }Quoted 8 lines
kernel/bpf/btf.c btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf, · line 3621
Quoted 4 lines
kernel/bpf/btf.c +4
-0+ btf_id =btf_find_by_name_kind (btf, btf_name,BTF_KIND_STRUCT ); +if (btf_id <0 ) +return btf_id; + btf_vmlinux_map_ids[i] = btf_id;Since
map_btf_nameis already inmap_ops, how about storingmap_btf_idinmap_opsalso?btf_id0 is "void" which is as good as -1, so there is no need to modify allmap_opsto initmap_btf_idto -1.Quoted 8 lines
Yeah,
btf_id== 0 being a valid id was the reason I used -1.I realized that having a map type specific struct with
btf_id== 0 should be practically impossible, but is it guaranteed to always be "void" or it just happened so and can change in the future?If both this and having one more field in
bpf_map_opsis not a problem, I'll move it tobpf_map_ops.Nope, I can't do it. All
struct bpf_map_opsare globalconst, i.e. rodata and a try castconstaway and change them causes a panic.Simple user space repro:
% cat 1.c #include <stdio.h> struct map_ops { int a; }; const struct map_ops ops = { .a = 1, }; int main(void) { struct map_ops *ops_rw = (struct map_ops *)&ops; printf("before a=%d\n", ops_rw->a); ops_rw->a = 3; printf(" afrer a=%d\n", ops_rw->a); } % clang -O2 -Wall -Wextra -pedantic -pedantic-errors -g 1.c && ./a.out before a=1 Segmentation fault (core dumped) % objdump -t a.out | grep -w ops 0000000000400600 g O .rodata 0000000000000004 opsQuoted 2 lines
--
Andrey IgnatovSee the trick that helper prototypes do for BTF ids. Fictional example:
static int
hash_map_btf_id;const struct
bpf_map_opshash_map_opss= {
...
.btf_id =&hash_map_btf_id,
};
Quoted 5 lines
Quoted 1 line
then *hash_map_ops.btf_id = <
final_btf_id>;Yeah, it would work, but IMO it wouldn't make the implementation better since for every map type I would need to write two additional lines of code. And whoever adds new map type will need to repeat this trick.
I think bpf_func_proto has already been doing this. Yonghong's tcp/udp iter is also doing something similar.
Quoted 7 lines
Yeah, it can be automated with a macro, but IMO it's better to avoid the work than to automate it.
Martin, Andrii is there any strong reason to convert to
map_btf_idfield? Or it's "nice to have" kind of thing? If the latter, I'd prefer to stick with my approach.
I just think it will be more natural to be able to directly access it through a map's related pointer, considering the map_btf_name is already there.
I don't feel strongly here for now. I think things will have to change anyway after quickly peeking at Jiri's patch. Also,
I take back on the collision check. I think this check should belong to Jiri's patch instead (if it is indeed needed). Renaming the sock_hash is good enough for now.
Martin KaFai Lau <[email protected]> [Thu, 2020-06-18 18:12 -0700]:
Quoted 115 lines · Andrey Ignatov
Quoted 114 lines
Andrii Nakryiko <[email protected]> [Thu, 2020-06-18 17:08 -0700]:
Quoted 113 lines · Andrey Ignatov
Quoted 99 lines
Andrey Ignatov <[email protected]> [Thu, 2020-06-18 12:42 -0700]:
Quoted 58 lines
Martin KaFai Lau <[email protected]> [Wed, 2020-06-17 23:18 -0700]:
Quoted 48 lines · Andrey Ignatov
[ ... ]
Quoted 46 lines
kernel/bpf/btf.c +38
-0@@ -3577,6 +3577,67 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf,return ctx_type; } +#define BPF_PROG_TYPE (_id, _name, prog_ctx_type, kern_ctx_type) +#define BPF_LINK_TYPE (_id, _name) +static const struct bpf_map_ops *const btf_vmlinux_map_ops[] = { +#define BPF_MAP_TYPE(_id, _ops) \ + [_id] = &_ops, +#include <linux/bpf_types.h> +#undef BPF_MAP_TYPE +}; +static u32 btf_vmlinux_map_ids[] = { +#define BPF_MAP_TYPE(_id, _ops) \ + [_id] = (u32)-1 , +#include <linux/bpf_types.h> +#undef BPF_MAP_TYPE +}; +#undef BPF_PROG_TYPE +#undef BPF_LINK_TYPE + +static int btf_vmlinux_map_ids_init (const struct btf *btf, +struct bpf_verifier_log *log) +{ +int base_btf_id, btf_id, i; +const char *btf_name; + + base_btf_id =btf_find_by_name_kind (btf,"bpf_map" ,BTF_KIND_STRUCT ); +if (base_btf_id <0 ) +return base_btf_id; + +BUILD_BUG_ON (ARRAY_SIZE (btf_vmlinux_map_ops) != +ARRAY_SIZE (btf_vmlinux_map_ids)); + +for (i =0 ; i <ARRAY_SIZE (btf_vmlinux_map_ops); ++i) { +if (!btf_vmlinux_map_ops[i]) +continue ; + btf_name = btf_vmlinux_map_ops[i]->map_btf_name; +if (!btf_name) { + btf_vmlinux_map_ids[i] = base_btf_id; +continue ; + }Quoted 8 lines
kernel/bpf/btf.c btf_get_prog_ctx_type(struct bpf_verifier_log *log, struct btf *btf, · line 3621
Quoted 4 lines
kernel/bpf/btf.c +4
-0+ btf_id =btf_find_by_name_kind (btf, btf_name,BTF_KIND_STRUCT ); +if (btf_id <0 ) +return btf_id; + btf_vmlinux_map_ids[i] = btf_id;Since
map_btf_nameis already inmap_ops, how about storingmap_btf_idinmap_opsalso?btf_id0 is "void" which is as good as -1, so there is no need to modify allmap_opsto initmap_btf_idto -1.Quoted 8 lines
Yeah,
btf_id== 0 being a valid id was the reason I used -1.I realized that having a map type specific struct with
btf_id== 0 should be practically impossible, but is it guaranteed to always be "void" or it just happened so and can change in the future?If both this and having one more field in
bpf_map_opsis not a problem, I'll move it tobpf_map_ops.Nope, I can't do it. All
struct bpf_map_opsare globalconst, i.e. rodata and a try castconstaway and change them causes a panic.Simple user space repro:
% cat 1.c #include <stdio.h> struct map_ops { int a; }; const struct map_ops ops = { .a = 1, }; int main(void) { struct map_ops *ops_rw = (struct map_ops *)&ops; printf("before a=%d\n", ops_rw->a); ops_rw->a = 3; printf(" afrer a=%d\n", ops_rw->a); } % clang -O2 -Wall -Wextra -pedantic -pedantic-errors -g 1.c && ./a.out before a=1 Segmentation fault (core dumped) % objdump -t a.out | grep -w ops 0000000000400600 g O .rodata 0000000000000004 opsQuoted 2 lines
--
Andrey IgnatovSee the trick that helper prototypes do for BTF ids. Fictional example:
static int
hash_map_btf_id;const struct
bpf_map_opshash_map_opss= {
...
.btf_id =&hash_map_btf_id,
};
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
then *hash_map_ops.btf_id = <
final_btf_id>;Yeah, it would work, but IMO it wouldn't make the implementation better since for every map type I would need to write two additional lines of code. And whoever adds new map type will need to repeat this trick.
I think
bpf_func_protohas already been doing this. Yonghong's tcp/udp iter is also doing something similar.
Right. I see bpf_func_proto.btf_id now.
Quoted 9 lines
Quoted 6 lines
Yeah, it can be automated with a macro, but IMO it's better to avoid the work than to automate it.
Martin, Andrii is there any strong reason to convert to
map_btf_idfield? Or it's "nice to have" kind of thing? If the latter, I'd prefer to stick with my approach.I just think it will be more natural to be able to directly access it through a map's related pointer, considering the
map_btf_nameis already there.
Ok, let's keep it consistent with other struct-s then. I'll switch bpf_map_ops to use same pointer trick.
Quoted 5 lines
I don't feel strongly here for now. I think things will have to change anyway after quickly peeking at Jiri's patch. Also,
I take back on the collision check. I think this check should belong to Jiri's patch instead (if it is indeed needed). Renaming thesock_hashis good enough for now.
Good point. I'll remove duplicates checking.
Signature · 1 line
Andrey Ignatov
Re: [PATCH bpf-next 1/6] 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]>---
Quoted 3 lines
kernel/bpf/btf.c | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-)
[...]