bpf: Set map_btf_{name, id} for all map types
authorAndrey Ignatov <rdna@fb.com>
Fri, 19 Jun 2020 21:11:44 +0000 (14:11 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Mon, 22 Jun 2020 20:22:58 +0000 (22:22 +0200)
Set map_btf_name and map_btf_id for all map types so that map fields can
be accessed by bpf programs.

Signed-off-by: Andrey Ignatov <rdna@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/a825f808f22af52b018dbe82f1c7d29dab5fc978.1592600985.git.rdna@fb.com
14 files changed:
kernel/bpf/arraymap.c
kernel/bpf/bpf_struct_ops.c
kernel/bpf/cpumap.c
kernel/bpf/devmap.c
kernel/bpf/hashtab.c
kernel/bpf/local_storage.c
kernel/bpf/lpm_trie.c
kernel/bpf/queue_stack_maps.c
kernel/bpf/reuseport_array.c
kernel/bpf/ringbuf.c
kernel/bpf/stackmap.c
net/core/bpf_sk_storage.c
net/core/sock_map.c
net/xdp/xskmap.c

index e7caa48812fbb9d2bd9dce6324d3609bc210fba1..ec5cd11032aa50cc782ba974507286122e8b23fe 100644 (file)
@@ -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,
 };
index c6b0decaa46a5c9db5ae3fd7d44677d8447a227e..969c5d47f81fed0cd2e9e275506d1beee8996cd2 100644 (file)
@@ -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
index 27595fc6da56b18eb0dfee3fa24366c2f70ca7aa..bd8658055c16dbf2bc848f88949eb8e5fb339a73 100644 (file)
@@ -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)
index 0cbb72cdaf633d3b3fde68cd75e39d0a18a6d989..58acc46861ef2d4b691b1e19465f0d1f8fb507f6 100644 (file)
@@ -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,
index 2c5999e02060fa7bd46369ed1bb6e2f48f5a6a6e..acd06081d81dc87536f2f6dbb0bf99d1080b0240 100644 (file)
@@ -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,
 };
index 33d01866bcc2c014ace7faddaa0b4f6fbee8f8a4..51bd5a8cb01bc8553669a7ce96d3c1a693435119 100644 (file)
@@ -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)
index c8cc4e4cf98de5f9d7e0395064601449738b926c..1abd4e3f906dea45399b8267cf1e873f43fa6f69 100644 (file)
@@ -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,
 };
index 05c8e043b9d2f3172a024446b1e2e0bafdb50041..80c66a6d7c548c76562305c2f0ff015f662ede07 100644 (file)
@@ -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,
 };
index 21cde24386db4b73ed4388f8cb3c183e57a58160..a09922f656e40fbaf5772eb73a7158b61a50d256 100644 (file)
@@ -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,
 };
index 180414bb0d3e9a94d26225f15370b139d0d7689f..dbf37aff4827f87a200ca2c9bb1662facf822572 100644 (file)
@@ -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,
index 599488f25e4044cde56550c051381af592164dd8..27dc9b1b08a523af0ebd36fc046bace0271996d3 100644 (file)
@@ -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)
index 1dae4b54324353778cb4204e78bec69f895de390..6f921c4ddc2ced73824afc7069f398fb743ebdd4 100644 (file)
@@ -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 = {
index 2b884f2d562ac7f75abe7b7c7df30361c4c64fd0..4c1123c749bb773e1dee3421f3de80153a2dd639 100644 (file)
@@ -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)
index 1dc7208c71bad03f86c33128541baf1f4bb5172c..8367adbbe9df2fba1345c54c06b253ef2565ecb2 100644 (file)
@@ -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,
 };