Skip to content

Commit c317ab7

Browse files
menglongdongAlexei Starovoitov
authored andcommitted
bpf: Compute map_btf_id during build time
For now, the field 'map_btf_id' in 'struct bpf_map_ops' for all map types are computed during vmlinux-btf init: btf_parse_vmlinux() -> btf_vmlinux_map_ids_init() It will lookup the btf_type according to the 'map_btf_name' field in 'struct bpf_map_ops'. This process can be done during build time, thanks to Jiri's resolve_btfids. selftest of map_ptr has passed: $96 map_ptr:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED Reported-by: kernel test robot <[email protected]> Signed-off-by: Menglong Dong <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 367590b commit c317ab7

19 files changed

+62
-129
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ struct bpf_map_ops {
148148
bpf_callback_t callback_fn,
149149
void *callback_ctx, u64 flags);
150150

151-
/* BTF name and id of struct allocated by map_alloc */
152-
const char * const map_btf_name;
151+
/* BTF id of struct allocated by map_alloc */
153152
int *map_btf_id;
154153

155154
/* bpf_iter info used to open a seq_file */

kernel/bpf/arraymap.c

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/perf_event.h>
1212
#include <uapi/linux/btf.h>
1313
#include <linux/rcupdate_trace.h>
14+
#include <linux/btf_ids.h>
1415

1516
#include "map_in_map.h"
1617

@@ -690,7 +691,7 @@ static int bpf_for_each_array_elem(struct bpf_map *map, bpf_callback_t callback_
690691
return num_elems;
691692
}
692693

693-
static int array_map_btf_id;
694+
BTF_ID_LIST_SINGLE(array_map_btf_ids, struct, bpf_array)
694695
const struct bpf_map_ops array_map_ops = {
695696
.map_meta_equal = array_map_meta_equal,
696697
.map_alloc_check = array_map_alloc_check,
@@ -711,12 +712,10 @@ const struct bpf_map_ops array_map_ops = {
711712
.map_update_batch = generic_map_update_batch,
712713
.map_set_for_each_callback_args = map_set_for_each_callback_args,
713714
.map_for_each_callback = bpf_for_each_array_elem,
714-
.map_btf_name = "bpf_array",
715-
.map_btf_id = &array_map_btf_id,
715+
.map_btf_id = &array_map_btf_ids[0],
716716
.iter_seq_info = &iter_seq_info,
717717
};
718718

719-
static int percpu_array_map_btf_id;
720719
const struct bpf_map_ops percpu_array_map_ops = {
721720
.map_meta_equal = bpf_map_meta_equal,
722721
.map_alloc_check = array_map_alloc_check,
@@ -732,8 +731,7 @@ const struct bpf_map_ops percpu_array_map_ops = {
732731
.map_update_batch = generic_map_update_batch,
733732
.map_set_for_each_callback_args = map_set_for_each_callback_args,
734733
.map_for_each_callback = bpf_for_each_array_elem,
735-
.map_btf_name = "bpf_array",
736-
.map_btf_id = &percpu_array_map_btf_id,
734+
.map_btf_id = &array_map_btf_ids[0],
737735
.iter_seq_info = &iter_seq_info,
738736
};
739737

@@ -1112,7 +1110,6 @@ static void prog_array_map_free(struct bpf_map *map)
11121110
* Thus, prog_array_map cannot be used as an inner_map
11131111
* and map_meta_equal is not implemented.
11141112
*/
1115-
static int prog_array_map_btf_id;
11161113
const struct bpf_map_ops prog_array_map_ops = {
11171114
.map_alloc_check = fd_array_map_alloc_check,
11181115
.map_alloc = prog_array_map_alloc,
@@ -1128,8 +1125,7 @@ const struct bpf_map_ops prog_array_map_ops = {
11281125
.map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem,
11291126
.map_release_uref = prog_array_map_clear,
11301127
.map_seq_show_elem = prog_array_map_seq_show_elem,
1131-
.map_btf_name = "bpf_array",
1132-
.map_btf_id = &prog_array_map_btf_id,
1128+
.map_btf_id = &array_map_btf_ids[0],
11331129
};
11341130

11351131
static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
@@ -1218,7 +1214,6 @@ static void perf_event_fd_array_map_free(struct bpf_map *map)
12181214
fd_array_map_free(map);
12191215
}
12201216

1221-
static int perf_event_array_map_btf_id;
12221217
const struct bpf_map_ops perf_event_array_map_ops = {
12231218
.map_meta_equal = bpf_map_meta_equal,
12241219
.map_alloc_check = fd_array_map_alloc_check,
@@ -1231,8 +1226,7 @@ const struct bpf_map_ops perf_event_array_map_ops = {
12311226
.map_fd_put_ptr = perf_event_fd_array_put_ptr,
12321227
.map_release = perf_event_fd_array_release,
12331228
.map_check_btf = map_check_no_btf,
1234-
.map_btf_name = "bpf_array",
1235-
.map_btf_id = &perf_event_array_map_btf_id,
1229+
.map_btf_id = &array_map_btf_ids[0],
12361230
};
12371231

12381232
#ifdef CONFIG_CGROUPS
@@ -1255,7 +1249,6 @@ static void cgroup_fd_array_free(struct bpf_map *map)
12551249
fd_array_map_free(map);
12561250
}
12571251

1258-
static int cgroup_array_map_btf_id;
12591252
const struct bpf_map_ops cgroup_array_map_ops = {
12601253
.map_meta_equal = bpf_map_meta_equal,
12611254
.map_alloc_check = fd_array_map_alloc_check,
@@ -1267,8 +1260,7 @@ const struct bpf_map_ops cgroup_array_map_ops = {
12671260
.map_fd_get_ptr = cgroup_fd_array_get_ptr,
12681261
.map_fd_put_ptr = cgroup_fd_array_put_ptr,
12691262
.map_check_btf = map_check_no_btf,
1270-
.map_btf_name = "bpf_array",
1271-
.map_btf_id = &cgroup_array_map_btf_id,
1263+
.map_btf_id = &array_map_btf_ids[0],
12721264
};
12731265
#endif
12741266

@@ -1342,7 +1334,6 @@ static int array_of_map_gen_lookup(struct bpf_map *map,
13421334
return insn - insn_buf;
13431335
}
13441336

1345-
static int array_of_maps_map_btf_id;
13461337
const struct bpf_map_ops array_of_maps_map_ops = {
13471338
.map_alloc_check = fd_array_map_alloc_check,
13481339
.map_alloc = array_of_map_alloc,
@@ -1355,6 +1346,5 @@ const struct bpf_map_ops array_of_maps_map_ops = {
13551346
.map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
13561347
.map_gen_lookup = array_of_map_gen_lookup,
13571348
.map_check_btf = map_check_no_btf,
1358-
.map_btf_name = "bpf_array",
1359-
.map_btf_id = &array_of_maps_map_btf_id,
1349+
.map_btf_id = &array_map_btf_ids[0],
13601350
};

kernel/bpf/bloom_filter.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <linux/err.h>
88
#include <linux/jhash.h>
99
#include <linux/random.h>
10+
#include <linux/btf_ids.h>
1011

1112
#define BLOOM_CREATE_FLAG_MASK \
1213
(BPF_F_NUMA_NODE | BPF_F_ZERO_SEED | BPF_F_ACCESS_MASK)
@@ -192,7 +193,7 @@ static int bloom_map_check_btf(const struct bpf_map *map,
192193
return btf_type_is_void(key_type) ? 0 : -EINVAL;
193194
}
194195

195-
static int bpf_bloom_map_btf_id;
196+
BTF_ID_LIST_SINGLE(bpf_bloom_map_btf_ids, struct, bpf_bloom_filter)
196197
const struct bpf_map_ops bloom_filter_map_ops = {
197198
.map_meta_equal = bpf_map_meta_equal,
198199
.map_alloc = bloom_map_alloc,
@@ -205,6 +206,5 @@ const struct bpf_map_ops bloom_filter_map_ops = {
205206
.map_update_elem = bloom_map_update_elem,
206207
.map_delete_elem = bloom_map_delete_elem,
207208
.map_check_btf = bloom_map_check_btf,
208-
.map_btf_name = "bpf_bloom_filter",
209-
.map_btf_id = &bpf_bloom_map_btf_id,
209+
.map_btf_id = &bpf_bloom_map_btf_ids[0],
210210
};

kernel/bpf/bpf_inode_storage.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ static void inode_storage_map_free(struct bpf_map *map)
245245
bpf_local_storage_map_free(smap, NULL);
246246
}
247247

248-
static int inode_storage_map_btf_id;
248+
BTF_ID_LIST_SINGLE(inode_storage_map_btf_ids, struct,
249+
bpf_local_storage_map)
249250
const struct bpf_map_ops inode_storage_map_ops = {
250251
.map_meta_equal = bpf_map_meta_equal,
251252
.map_alloc_check = bpf_local_storage_map_alloc_check,
@@ -256,8 +257,7 @@ const struct bpf_map_ops inode_storage_map_ops = {
256257
.map_update_elem = bpf_fd_inode_storage_update_elem,
257258
.map_delete_elem = bpf_fd_inode_storage_delete_elem,
258259
.map_check_btf = bpf_local_storage_map_check_btf,
259-
.map_btf_name = "bpf_local_storage_map",
260-
.map_btf_id = &inode_storage_map_btf_id,
260+
.map_btf_id = &inode_storage_map_btf_ids[0],
261261
.map_owner_storage_ptr = inode_storage_ptr,
262262
};
263263

kernel/bpf/bpf_struct_ops.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/seq_file.h>
1111
#include <linux/refcount.h>
1212
#include <linux/mutex.h>
13+
#include <linux/btf_ids.h>
1314

1415
enum bpf_struct_ops_state {
1516
BPF_STRUCT_OPS_STATE_INIT,
@@ -612,7 +613,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
612613
return map;
613614
}
614615

615-
static int bpf_struct_ops_map_btf_id;
616+
BTF_ID_LIST_SINGLE(bpf_struct_ops_map_btf_ids, struct, bpf_struct_ops_map)
616617
const struct bpf_map_ops bpf_struct_ops_map_ops = {
617618
.map_alloc_check = bpf_struct_ops_map_alloc_check,
618619
.map_alloc = bpf_struct_ops_map_alloc,
@@ -622,8 +623,7 @@ const struct bpf_map_ops bpf_struct_ops_map_ops = {
622623
.map_delete_elem = bpf_struct_ops_map_delete_elem,
623624
.map_update_elem = bpf_struct_ops_map_update_elem,
624625
.map_seq_show_elem = bpf_struct_ops_map_seq_show_elem,
625-
.map_btf_name = "bpf_struct_ops_map",
626-
.map_btf_id = &bpf_struct_ops_map_btf_id,
626+
.map_btf_id = &bpf_struct_ops_map_btf_ids[0],
627627
};
628628

629629
/* "const void *" because some subsystem is

kernel/bpf/bpf_task_storage.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ static void task_storage_map_free(struct bpf_map *map)
307307
bpf_local_storage_map_free(smap, &bpf_task_storage_busy);
308308
}
309309

310-
static int task_storage_map_btf_id;
310+
BTF_ID_LIST_SINGLE(task_storage_map_btf_ids, struct, bpf_local_storage_map)
311311
const struct bpf_map_ops task_storage_map_ops = {
312312
.map_meta_equal = bpf_map_meta_equal,
313313
.map_alloc_check = bpf_local_storage_map_alloc_check,
@@ -318,8 +318,7 @@ const struct bpf_map_ops task_storage_map_ops = {
318318
.map_update_elem = bpf_pid_task_storage_update_elem,
319319
.map_delete_elem = bpf_pid_task_storage_delete_elem,
320320
.map_check_btf = bpf_local_storage_map_check_btf,
321-
.map_btf_name = "bpf_local_storage_map",
322-
.map_btf_id = &task_storage_map_btf_id,
321+
.map_btf_id = &task_storage_map_btf_ids[0],
323322
.map_owner_storage_ptr = task_storage_ptr,
324323
};
325324

kernel/bpf/btf.c

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5025,41 +5025,6 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
50255025
return ctx_type;
50265026
}
50275027

5028-
static const struct bpf_map_ops * const btf_vmlinux_map_ops[] = {
5029-
#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
5030-
#define BPF_LINK_TYPE(_id, _name)
5031-
#define BPF_MAP_TYPE(_id, _ops) \
5032-
[_id] = &_ops,
5033-
#include <linux/bpf_types.h>
5034-
#undef BPF_PROG_TYPE
5035-
#undef BPF_LINK_TYPE
5036-
#undef BPF_MAP_TYPE
5037-
};
5038-
5039-
static int btf_vmlinux_map_ids_init(const struct btf *btf,
5040-
struct bpf_verifier_log *log)
5041-
{
5042-
const struct bpf_map_ops *ops;
5043-
int i, btf_id;
5044-
5045-
for (i = 0; i < ARRAY_SIZE(btf_vmlinux_map_ops); ++i) {
5046-
ops = btf_vmlinux_map_ops[i];
5047-
if (!ops || (!ops->map_btf_name && !ops->map_btf_id))
5048-
continue;
5049-
if (!ops->map_btf_name || !ops->map_btf_id) {
5050-
bpf_log(log, "map type %d is misconfigured\n", i);
5051-
return -EINVAL;
5052-
}
5053-
btf_id = btf_find_by_name_kind(btf, ops->map_btf_name,
5054-
BTF_KIND_STRUCT);
5055-
if (btf_id < 0)
5056-
return btf_id;
5057-
*ops->map_btf_id = btf_id;
5058-
}
5059-
5060-
return 0;
5061-
}
5062-
50635028
static int btf_translate_to_vmlinux(struct bpf_verifier_log *log,
50645029
struct btf *btf,
50655030
const struct btf_type *t,
@@ -5125,11 +5090,6 @@ struct btf *btf_parse_vmlinux(void)
51255090
/* btf_parse_vmlinux() runs under bpf_verifier_lock */
51265091
bpf_ctx_convert.t = btf_type_by_id(btf, bpf_ctx_convert_btf_id[0]);
51275092

5128-
/* find bpf map structs for map_ptr access checking */
5129-
err = btf_vmlinux_map_ids_init(btf, log);
5130-
if (err < 0)
5131-
goto errout;
5132-
51335093
bpf_struct_ops_init(btf, log);
51345094

51355095
refcount_set(&btf->refcnt, 1);

kernel/bpf/cpumap.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <linux/kthread.h>
2828
#include <linux/capability.h>
2929
#include <trace/events/xdp.h>
30+
#include <linux/btf_ids.h>
3031

3132
#include <linux/netdevice.h> /* netif_receive_skb_list */
3233
#include <linux/etherdevice.h> /* eth_type_trans */
@@ -673,7 +674,7 @@ static int cpu_map_redirect(struct bpf_map *map, u32 ifindex, u64 flags)
673674
__cpu_map_lookup_elem);
674675
}
675676

676-
static int cpu_map_btf_id;
677+
BTF_ID_LIST_SINGLE(cpu_map_btf_ids, struct, bpf_cpu_map)
677678
const struct bpf_map_ops cpu_map_ops = {
678679
.map_meta_equal = bpf_map_meta_equal,
679680
.map_alloc = cpu_map_alloc,
@@ -683,8 +684,7 @@ const struct bpf_map_ops cpu_map_ops = {
683684
.map_lookup_elem = cpu_map_lookup_elem,
684685
.map_get_next_key = cpu_map_get_next_key,
685686
.map_check_btf = map_check_no_btf,
686-
.map_btf_name = "bpf_cpu_map",
687-
.map_btf_id = &cpu_map_btf_id,
687+
.map_btf_id = &cpu_map_btf_ids[0],
688688
.map_redirect = cpu_map_redirect,
689689
};
690690

kernel/bpf/devmap.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <net/xdp.h>
4949
#include <linux/filter.h>
5050
#include <trace/events/xdp.h>
51+
#include <linux/btf_ids.h>
5152

5253
#define DEV_CREATE_FLAG_MASK \
5354
(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
@@ -1005,7 +1006,7 @@ static int dev_hash_map_redirect(struct bpf_map *map, u32 ifindex, u64 flags)
10051006
__dev_map_hash_lookup_elem);
10061007
}
10071008

1008-
static int dev_map_btf_id;
1009+
BTF_ID_LIST_SINGLE(dev_map_btf_ids, struct, bpf_dtab)
10091010
const struct bpf_map_ops dev_map_ops = {
10101011
.map_meta_equal = bpf_map_meta_equal,
10111012
.map_alloc = dev_map_alloc,
@@ -1015,12 +1016,10 @@ const struct bpf_map_ops dev_map_ops = {
10151016
.map_update_elem = dev_map_update_elem,
10161017
.map_delete_elem = dev_map_delete_elem,
10171018
.map_check_btf = map_check_no_btf,
1018-
.map_btf_name = "bpf_dtab",
1019-
.map_btf_id = &dev_map_btf_id,
1019+
.map_btf_id = &dev_map_btf_ids[0],
10201020
.map_redirect = dev_map_redirect,
10211021
};
10221022

1023-
static int dev_map_hash_map_btf_id;
10241023
const struct bpf_map_ops dev_map_hash_ops = {
10251024
.map_meta_equal = bpf_map_meta_equal,
10261025
.map_alloc = dev_map_alloc,
@@ -1030,8 +1029,7 @@ const struct bpf_map_ops dev_map_hash_ops = {
10301029
.map_update_elem = dev_map_hash_update_elem,
10311030
.map_delete_elem = dev_map_hash_delete_elem,
10321031
.map_check_btf = map_check_no_btf,
1033-
.map_btf_name = "bpf_dtab",
1034-
.map_btf_id = &dev_map_hash_map_btf_id,
1032+
.map_btf_id = &dev_map_btf_ids[0],
10351033
.map_redirect = dev_hash_map_redirect,
10361034
};
10371035

0 commit comments

Comments
 (0)