Skip to content

Commit e8d2bec

Browse files
committed
bpf: decouple btf from seq bpf fs dump and enable more maps
Commit a26ca7c ("bpf: btf: Add pretty print support to the basic arraymap") and 699c86d ("bpf: btf: add pretty print for hash/lru_hash maps") enabled support for BTF and dumping via BPF fs for array and hash/lru map. However, both can be decoupled from each other such that regular BPF maps can be supported for attaching BTF key/value information, while not all maps necessarily need to dump via map_seq_show_elem() callback. The basic sanity check which is a prerequisite for all maps is that key/value size has to match in any case, and some maps can have extra checks via map_check_btf() callback, e.g. probing certain types or indicating no support in general. With that we can also enable retrieving BTF info for per-cpu map types and lpm. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Yonghong Song <yhs@fb.com>
1 parent 9d6f417 commit e8d2bec

12 files changed

Lines changed: 75 additions & 44 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct bpf_prog;
2323
struct bpf_map;
2424
struct sock;
2525
struct seq_file;
26-
struct btf;
26+
struct btf_type;
2727

2828
/* map is generic key/value storage optionally accesible by eBPF programs */
2929
struct bpf_map_ops {
@@ -48,8 +48,9 @@ struct bpf_map_ops {
4848
u32 (*map_fd_sys_lookup_elem)(void *ptr);
4949
void (*map_seq_show_elem)(struct bpf_map *map, void *key,
5050
struct seq_file *m);
51-
int (*map_check_btf)(const struct bpf_map *map, const struct btf *btf,
52-
u32 key_type_id, u32 value_type_id);
51+
int (*map_check_btf)(const struct bpf_map *map,
52+
const struct btf_type *key_type,
53+
const struct btf_type *value_type);
5354
};
5455

5556
struct bpf_map {
@@ -118,9 +119,13 @@ static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
118119

119120
static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
120121
{
121-
return map->ops->map_seq_show_elem && map->ops->map_check_btf;
122+
return map->btf && map->ops->map_seq_show_elem;
122123
}
123124

125+
int map_check_no_btf(const struct bpf_map *map,
126+
const struct btf_type *key_type,
127+
const struct btf_type *value_type);
128+
124129
extern const struct bpf_map_ops bpf_map_offload_ops;
125130

126131
/* function argument constraints */

‎kernel/bpf/arraymap.c‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -358,27 +358,20 @@ static void array_map_seq_show_elem(struct bpf_map *map, void *key,
358358
rcu_read_unlock();
359359
}
360360

361-
static int array_map_check_btf(const struct bpf_map *map, const struct btf *btf,
362-
u32 btf_key_id, u32 btf_value_id)
361+
static int array_map_check_btf(const struct bpf_map *map,
362+
const struct btf_type *key_type,
363+
const struct btf_type *value_type)
363364
{
364-
const struct btf_type *key_type, *value_type;
365-
u32 key_size, value_size;
366365
u32 int_data;
367366

368-
key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
369-
if (!key_type || BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
367+
if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
370368
return -EINVAL;
371369

372370
int_data = *(u32 *)(key_type + 1);
373-
/* bpf array can only take a u32 key. This check makes
374-
* sure that the btf matches the attr used during map_create.
371+
/* bpf array can only take a u32 key. This check makes sure
372+
* that the btf matches the attr used during map_create.
375373
*/
376-
if (BTF_INT_BITS(int_data) != 32 || key_size != 4 ||
377-
BTF_INT_OFFSET(int_data))
378-
return -EINVAL;
379-
380-
value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
381-
if (!value_type || value_size != map->value_size)
374+
if (BTF_INT_BITS(int_data) != 32 || BTF_INT_OFFSET(int_data))
382375
return -EINVAL;
383376

384377
return 0;
@@ -405,6 +398,7 @@ const struct bpf_map_ops percpu_array_map_ops = {
405398
.map_lookup_elem = percpu_array_map_lookup_elem,
406399
.map_update_elem = array_map_update_elem,
407400
.map_delete_elem = array_map_delete_elem,
401+
.map_check_btf = array_map_check_btf,
408402
};
409403

410404
static int fd_array_map_alloc_check(union bpf_attr *attr)
@@ -546,6 +540,7 @@ const struct bpf_map_ops prog_array_map_ops = {
546540
.map_fd_put_ptr = prog_fd_array_put_ptr,
547541
.map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem,
548542
.map_release_uref = bpf_fd_array_map_clear,
543+
.map_check_btf = map_check_no_btf,
549544
};
550545

551546
static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
@@ -634,6 +629,7 @@ const struct bpf_map_ops perf_event_array_map_ops = {
634629
.map_fd_get_ptr = perf_event_fd_array_get_ptr,
635630
.map_fd_put_ptr = perf_event_fd_array_put_ptr,
636631
.map_release = perf_event_fd_array_release,
632+
.map_check_btf = map_check_no_btf,
637633
};
638634

639635
#ifdef CONFIG_CGROUPS
@@ -665,6 +661,7 @@ const struct bpf_map_ops cgroup_array_map_ops = {
665661
.map_delete_elem = fd_array_map_delete_elem,
666662
.map_fd_get_ptr = cgroup_fd_array_get_ptr,
667663
.map_fd_put_ptr = cgroup_fd_array_put_ptr,
664+
.map_check_btf = map_check_no_btf,
668665
};
669666
#endif
670667

@@ -749,4 +746,5 @@ const struct bpf_map_ops array_of_maps_map_ops = {
749746
.map_fd_put_ptr = bpf_map_fd_put_ptr,
750747
.map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
751748
.map_gen_lookup = array_of_map_gen_lookup,
749+
.map_check_btf = map_check_no_btf,
752750
};

‎kernel/bpf/cpumap.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ const struct bpf_map_ops cpu_map_ops = {
555555
.map_update_elem = cpu_map_update_elem,
556556
.map_lookup_elem = cpu_map_lookup_elem,
557557
.map_get_next_key = cpu_map_get_next_key,
558+
.map_check_btf = map_check_no_btf,
558559
};
559560

560561
static int bq_flush_to_queue(struct bpf_cpu_map_entry *rcpu,

‎kernel/bpf/devmap.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ const struct bpf_map_ops dev_map_ops = {
484484
.map_lookup_elem = dev_map_lookup_elem,
485485
.map_update_elem = dev_map_update_elem,
486486
.map_delete_elem = dev_map_delete_elem,
487+
.map_check_btf = map_check_no_btf,
487488
};
488489

489490
static int dev_map_notification(struct notifier_block *notifier,

‎kernel/bpf/hashtab.c‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,23 +1185,6 @@ static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
11851185
rcu_read_unlock();
11861186
}
11871187

1188-
static int htab_map_check_btf(const struct bpf_map *map, const struct btf *btf,
1189-
u32 btf_key_id, u32 btf_value_id)
1190-
{
1191-
const struct btf_type *key_type, *value_type;
1192-
u32 key_size, value_size;
1193-
1194-
key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
1195-
if (!key_type || key_size != map->key_size)
1196-
return -EINVAL;
1197-
1198-
value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
1199-
if (!value_type || value_size != map->value_size)
1200-
return -EINVAL;
1201-
1202-
return 0;
1203-
}
1204-
12051188
const struct bpf_map_ops htab_map_ops = {
12061189
.map_alloc_check = htab_map_alloc_check,
12071190
.map_alloc = htab_map_alloc,
@@ -1212,7 +1195,6 @@ const struct bpf_map_ops htab_map_ops = {
12121195
.map_delete_elem = htab_map_delete_elem,
12131196
.map_gen_lookup = htab_map_gen_lookup,
12141197
.map_seq_show_elem = htab_map_seq_show_elem,
1215-
.map_check_btf = htab_map_check_btf,
12161198
};
12171199

12181200
const struct bpf_map_ops htab_lru_map_ops = {
@@ -1225,7 +1207,6 @@ const struct bpf_map_ops htab_lru_map_ops = {
12251207
.map_delete_elem = htab_lru_map_delete_elem,
12261208
.map_gen_lookup = htab_lru_map_gen_lookup,
12271209
.map_seq_show_elem = htab_map_seq_show_elem,
1228-
.map_check_btf = htab_map_check_btf,
12291210
};
12301211

12311212
/* Called from eBPF program */
@@ -1452,4 +1433,5 @@ const struct bpf_map_ops htab_of_maps_map_ops = {
14521433
.map_fd_put_ptr = bpf_map_fd_put_ptr,
14531434
.map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
14541435
.map_gen_lookup = htab_of_map_gen_lookup,
1436+
.map_check_btf = map_check_no_btf,
14551437
};

‎kernel/bpf/inode.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg)
334334
struct bpf_map *map = arg;
335335

336336
return bpf_mkobj_ops(dentry, mode, arg, &bpf_map_iops,
337-
map->btf ? &bpffs_map_fops : &bpffs_obj_fops);
337+
bpf_map_support_seq_show(map) ?
338+
&bpffs_map_fops : &bpffs_obj_fops);
338339
}
339340

340341
static struct dentry *

‎kernel/bpf/local_storage.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ const struct bpf_map_ops cgroup_storage_map_ops = {
246246
.map_lookup_elem = cgroup_storage_lookup_elem,
247247
.map_update_elem = cgroup_storage_update_elem,
248248
.map_delete_elem = cgroup_storage_delete_elem,
249+
.map_check_btf = map_check_no_btf,
249250
};
250251

251252
int bpf_cgroup_storage_assign(struct bpf_prog *prog, struct bpf_map *_map)

‎kernel/bpf/lpm_trie.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
*/
1111

1212
#include <linux/bpf.h>
13+
#include <linux/btf.h>
1314
#include <linux/err.h>
1415
#include <linux/slab.h>
1516
#include <linux/spinlock.h>
1617
#include <linux/vmalloc.h>
1718
#include <net/ipv6.h>
19+
#include <uapi/linux/btf.h>
1820

1921
/* Intermediate node */
2022
#define LPM_TREE_NODE_FLAG_IM BIT(0)
@@ -686,11 +688,21 @@ static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key)
686688
return err;
687689
}
688690

691+
static int trie_check_btf(const struct bpf_map *map,
692+
const struct btf_type *key_type,
693+
const struct btf_type *value_type)
694+
{
695+
/* Keys must have struct bpf_lpm_trie_key embedded. */
696+
return BTF_INFO_KIND(key_type->info) != BTF_KIND_STRUCT ?
697+
-EINVAL : 0;
698+
}
699+
689700
const struct bpf_map_ops trie_map_ops = {
690701
.map_alloc = trie_alloc,
691702
.map_free = trie_free,
692703
.map_get_next_key = trie_get_next_key,
693704
.map_lookup_elem = trie_lookup_elem,
694705
.map_update_elem = trie_update_elem,
695706
.map_delete_elem = trie_delete_elem,
707+
.map_check_btf = trie_check_btf,
696708
};

‎kernel/bpf/sockmap.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2495,6 +2495,7 @@ const struct bpf_map_ops sock_map_ops = {
24952495
.map_update_elem = sock_map_update_elem,
24962496
.map_delete_elem = sock_map_delete_elem,
24972497
.map_release_uref = sock_map_release,
2498+
.map_check_btf = map_check_no_btf,
24982499
};
24992500

25002501
const struct bpf_map_ops sock_hash_ops = {
@@ -2505,6 +2506,7 @@ const struct bpf_map_ops sock_hash_ops = {
25052506
.map_update_elem = sock_hash_update_elem,
25062507
.map_delete_elem = sock_hash_delete_elem,
25072508
.map_release_uref = sock_map_release,
2509+
.map_check_btf = map_check_no_btf,
25082510
};
25092511

25102512
BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, bpf_sock,

‎kernel/bpf/stackmap.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ const struct bpf_map_ops stack_map_ops = {
607607
.map_lookup_elem = stack_map_lookup_elem,
608608
.map_update_elem = stack_map_update_elem,
609609
.map_delete_elem = stack_map_delete_elem,
610+
.map_check_btf = map_check_no_btf,
610611
};
611612

612613
static int __init stack_map_init(void)

0 commit comments

Comments
 (0)