Skip to content

Commit 10f79ee

Browse files
menglongdongKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
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 Signed-off-by: Menglong Dong <[email protected]> Reported-by: kernel test robot <[email protected]>
1 parent 9a8f654 commit 10f79ee

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
@@ -147,8 +147,7 @@ struct bpf_map_ops {
147147
bpf_callback_t callback_fn,
148148
void *callback_ctx, u64 flags);
149149

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

154153
/* 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

@@ -680,7 +681,7 @@ static int bpf_for_each_array_elem(struct bpf_map *map, bpf_callback_t callback_
680681
return num_elems;
681682
}
682683

683-
static int array_map_btf_id;
684+
BTF_ID_LIST_SINGLE(array_map_btf_ids, struct, bpf_array)
684685
const struct bpf_map_ops array_map_ops = {
685686
.map_meta_equal = array_map_meta_equal,
686687
.map_alloc_check = array_map_alloc_check,
@@ -701,12 +702,10 @@ const struct bpf_map_ops array_map_ops = {
701702
.map_update_batch = generic_map_update_batch,
702703
.map_set_for_each_callback_args = map_set_for_each_callback_args,
703704
.map_for_each_callback = bpf_for_each_array_elem,
704-
.map_btf_name = "bpf_array",
705-
.map_btf_id = &array_map_btf_id,
705+
.map_btf_id = &array_map_btf_ids[0],
706706
.iter_seq_info = &iter_seq_info,
707707
};
708708

709-
static int percpu_array_map_btf_id;
710709
const struct bpf_map_ops percpu_array_map_ops = {
711710
.map_meta_equal = bpf_map_meta_equal,
712711
.map_alloc_check = array_map_alloc_check,
@@ -722,8 +721,7 @@ const struct bpf_map_ops percpu_array_map_ops = {
722721
.map_update_batch = generic_map_update_batch,
723722
.map_set_for_each_callback_args = map_set_for_each_callback_args,
724723
.map_for_each_callback = bpf_for_each_array_elem,
725-
.map_btf_name = "bpf_array",
726-
.map_btf_id = &percpu_array_map_btf_id,
724+
.map_btf_id = &array_map_btf_ids[0],
727725
.iter_seq_info = &iter_seq_info,
728726
};
729727

@@ -1102,7 +1100,6 @@ static void prog_array_map_free(struct bpf_map *map)
11021100
* Thus, prog_array_map cannot be used as an inner_map
11031101
* and map_meta_equal is not implemented.
11041102
*/
1105-
static int prog_array_map_btf_id;
11061103
const struct bpf_map_ops prog_array_map_ops = {
11071104
.map_alloc_check = fd_array_map_alloc_check,
11081105
.map_alloc = prog_array_map_alloc,
@@ -1118,8 +1115,7 @@ const struct bpf_map_ops prog_array_map_ops = {
11181115
.map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem,
11191116
.map_release_uref = prog_array_map_clear,
11201117
.map_seq_show_elem = prog_array_map_seq_show_elem,
1121-
.map_btf_name = "bpf_array",
1122-
.map_btf_id = &prog_array_map_btf_id,
1118+
.map_btf_id = &array_map_btf_ids[0],
11231119
};
11241120

11251121
static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
@@ -1208,7 +1204,6 @@ static void perf_event_fd_array_map_free(struct bpf_map *map)
12081204
fd_array_map_free(map);
12091205
}
12101206

1211-
static int perf_event_array_map_btf_id;
12121207
const struct bpf_map_ops perf_event_array_map_ops = {
12131208
.map_meta_equal = bpf_map_meta_equal,
12141209
.map_alloc_check = fd_array_map_alloc_check,
@@ -1221,8 +1216,7 @@ const struct bpf_map_ops perf_event_array_map_ops = {
12211216
.map_fd_put_ptr = perf_event_fd_array_put_ptr,
12221217
.map_release = perf_event_fd_array_release,
12231218
.map_check_btf = map_check_no_btf,
1224-
.map_btf_name = "bpf_array",
1225-
.map_btf_id = &perf_event_array_map_btf_id,
1219+
.map_btf_id = &array_map_btf_ids[0],
12261220
};
12271221

12281222
#ifdef CONFIG_CGROUPS
@@ -1245,7 +1239,6 @@ static void cgroup_fd_array_free(struct bpf_map *map)
12451239
fd_array_map_free(map);
12461240
}
12471241

1248-
static int cgroup_array_map_btf_id;
12491242
const struct bpf_map_ops cgroup_array_map_ops = {
12501243
.map_meta_equal = bpf_map_meta_equal,
12511244
.map_alloc_check = fd_array_map_alloc_check,
@@ -1257,8 +1250,7 @@ const struct bpf_map_ops cgroup_array_map_ops = {
12571250
.map_fd_get_ptr = cgroup_fd_array_get_ptr,
12581251
.map_fd_put_ptr = cgroup_fd_array_put_ptr,
12591252
.map_check_btf = map_check_no_btf,
1260-
.map_btf_name = "bpf_array",
1261-
.map_btf_id = &cgroup_array_map_btf_id,
1253+
.map_btf_id = &array_map_btf_ids[0],
12621254
};
12631255
#endif
12641256

@@ -1332,7 +1324,6 @@ static int array_of_map_gen_lookup(struct bpf_map *map,
13321324
return insn - insn_buf;
13331325
}
13341326

1335-
static int array_of_maps_map_btf_id;
13361327
const struct bpf_map_ops array_of_maps_map_ops = {
13371328
.map_alloc_check = fd_array_map_alloc_check,
13381329
.map_alloc = array_of_map_alloc,
@@ -1345,6 +1336,5 @@ const struct bpf_map_ops array_of_maps_map_ops = {
13451336
.map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
13461337
.map_gen_lookup = array_of_map_gen_lookup,
13471338
.map_check_btf = map_check_no_btf,
1348-
.map_btf_name = "bpf_array",
1349-
.map_btf_id = &array_of_maps_map_btf_id,
1339+
.map_btf_id = &array_map_btf_ids[0],
13501340
};

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
@@ -4820,41 +4820,6 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
48204820
return ctx_type;
48214821
}
48224822

4823-
static const struct bpf_map_ops * const btf_vmlinux_map_ops[] = {
4824-
#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
4825-
#define BPF_LINK_TYPE(_id, _name)
4826-
#define BPF_MAP_TYPE(_id, _ops) \
4827-
[_id] = &_ops,
4828-
#include <linux/bpf_types.h>
4829-
#undef BPF_PROG_TYPE
4830-
#undef BPF_LINK_TYPE
4831-
#undef BPF_MAP_TYPE
4832-
};
4833-
4834-
static int btf_vmlinux_map_ids_init(const struct btf *btf,
4835-
struct bpf_verifier_log *log)
4836-
{
4837-
const struct bpf_map_ops *ops;
4838-
int i, btf_id;
4839-
4840-
for (i = 0; i < ARRAY_SIZE(btf_vmlinux_map_ops); ++i) {
4841-
ops = btf_vmlinux_map_ops[i];
4842-
if (!ops || (!ops->map_btf_name && !ops->map_btf_id))
4843-
continue;
4844-
if (!ops->map_btf_name || !ops->map_btf_id) {
4845-
bpf_log(log, "map type %d is misconfigured\n", i);
4846-
return -EINVAL;
4847-
}
4848-
btf_id = btf_find_by_name_kind(btf, ops->map_btf_name,
4849-
BTF_KIND_STRUCT);
4850-
if (btf_id < 0)
4851-
return btf_id;
4852-
*ops->map_btf_id = btf_id;
4853-
}
4854-
4855-
return 0;
4856-
}
4857-
48584823
static int btf_translate_to_vmlinux(struct bpf_verifier_log *log,
48594824
struct btf *btf,
48604825
const struct btf_type *t,
@@ -4920,11 +4885,6 @@ struct btf *btf_parse_vmlinux(void)
49204885
/* btf_parse_vmlinux() runs under bpf_verifier_lock */
49214886
bpf_ctx_convert.t = btf_type_by_id(btf, bpf_ctx_convert_btf_id[0]);
49224887

4923-
/* find bpf map structs for map_ptr access checking */
4924-
err = btf_vmlinux_map_ids_init(btf, log);
4925-
if (err < 0)
4926-
goto errout;
4927-
49284888
bpf_struct_ops_init(btf, log);
49294889

49304890
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)