Skip to content

Commit 40077e0

Browse files
jmberg-inteldavem330
authored andcommitted
bpf: remove struct bpf_map_type_list
There's no need to have struct bpf_map_type_list since it just contains a list_head, the type, and the ops pointer. Since the types are densely packed and not actually dynamically registered, it's much easier and smaller to have an array of type->ops pointer. Also initialize this array statically to remove code needed to initialize it. In order to save duplicating the list, move it to the types header file added by the previous patch and include it in the same fashion. Signed-off-by: Johannes Berg <johannes.berg@intel.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent be9370a commit 40077e0

7 files changed

Lines changed: 53 additions & 165 deletions

File tree

include/linux/bpf.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ struct bpf_map {
5353
struct bpf_map *inner_map_meta;
5454
};
5555

56-
struct bpf_map_type_list {
57-
struct list_head list_node;
58-
const struct bpf_map_ops *ops;
59-
enum bpf_map_type type;
60-
};
61-
6256
/* function argument constraints */
6357
enum bpf_arg_type {
6458
ARG_DONTCARE = 0, /* unused argument in helper function */
@@ -239,10 +233,11 @@ DECLARE_PER_CPU(int, bpf_prog_active);
239233

240234
#define BPF_PROG_TYPE(_id, _ops) \
241235
extern const struct bpf_verifier_ops _ops;
236+
#define BPF_MAP_TYPE(_id, _ops) \
237+
extern const struct bpf_map_ops _ops;
242238
#include <linux/bpf_types.h>
243239
#undef BPF_PROG_TYPE
244-
245-
void bpf_register_map_type(struct bpf_map_type_list *tl);
240+
#undef BPF_MAP_TYPE
246241

247242
struct bpf_prog *bpf_prog_get(u32 ufd);
248243
struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type);

include/linux/bpf_types.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,21 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_KPROBE, kprobe_prog_ops)
1616
BPF_PROG_TYPE(BPF_PROG_TYPE_TRACEPOINT, tracepoint_prog_ops)
1717
BPF_PROG_TYPE(BPF_PROG_TYPE_PERF_EVENT, perf_event_prog_ops)
1818
#endif
19+
20+
BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY, array_map_ops)
21+
BPF_MAP_TYPE(BPF_MAP_TYPE_PERCPU_ARRAY, percpu_array_map_ops)
22+
BPF_MAP_TYPE(BPF_MAP_TYPE_PROG_ARRAY, prog_array_map_ops)
23+
BPF_MAP_TYPE(BPF_MAP_TYPE_PERF_EVENT_ARRAY, perf_event_array_map_ops)
24+
#ifdef CONFIG_CGROUPS
25+
BPF_MAP_TYPE(BPF_MAP_TYPE_CGROUP_ARRAY, cgroup_array_map_ops)
26+
#endif
27+
BPF_MAP_TYPE(BPF_MAP_TYPE_HASH, htab_map_ops)
28+
BPF_MAP_TYPE(BPF_MAP_TYPE_PERCPU_HASH, htab_percpu_map_ops)
29+
BPF_MAP_TYPE(BPF_MAP_TYPE_LRU_HASH, htab_lru_map_ops)
30+
BPF_MAP_TYPE(BPF_MAP_TYPE_LRU_PERCPU_HASH, htab_lru_percpu_map_ops)
31+
BPF_MAP_TYPE(BPF_MAP_TYPE_LPM_TRIE, trie_map_ops)
32+
#ifdef CONFIG_PERF_EVENTS
33+
BPF_MAP_TYPE(BPF_MAP_TYPE_STACK_TRACE, stack_map_ops)
34+
#endif
35+
BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY_OF_MAPS, array_of_maps_map_ops)
36+
BPF_MAP_TYPE(BPF_MAP_TYPE_HASH_OF_MAPS, htab_of_maps_map_ops)

kernel/bpf/arraymap.c

Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ static void array_map_free(struct bpf_map *map)
287287
bpf_map_area_free(array);
288288
}
289289

290-
static const struct bpf_map_ops array_ops = {
290+
const struct bpf_map_ops array_map_ops = {
291291
.map_alloc = array_map_alloc,
292292
.map_free = array_map_free,
293293
.map_get_next_key = array_map_get_next_key,
@@ -297,12 +297,7 @@ static const struct bpf_map_ops array_ops = {
297297
.map_gen_lookup = array_map_gen_lookup,
298298
};
299299

300-
static struct bpf_map_type_list array_type __ro_after_init = {
301-
.ops = &array_ops,
302-
.type = BPF_MAP_TYPE_ARRAY,
303-
};
304-
305-
static const struct bpf_map_ops percpu_array_ops = {
300+
const struct bpf_map_ops percpu_array_map_ops = {
306301
.map_alloc = array_map_alloc,
307302
.map_free = array_map_free,
308303
.map_get_next_key = array_map_get_next_key,
@@ -311,19 +306,6 @@ static const struct bpf_map_ops percpu_array_ops = {
311306
.map_delete_elem = array_map_delete_elem,
312307
};
313308

314-
static struct bpf_map_type_list percpu_array_type __ro_after_init = {
315-
.ops = &percpu_array_ops,
316-
.type = BPF_MAP_TYPE_PERCPU_ARRAY,
317-
};
318-
319-
static int __init register_array_map(void)
320-
{
321-
bpf_register_map_type(&array_type);
322-
bpf_register_map_type(&percpu_array_type);
323-
return 0;
324-
}
325-
late_initcall(register_array_map);
326-
327309
static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
328310
{
329311
/* only file descriptors can be stored in this type of map */
@@ -427,7 +409,7 @@ void bpf_fd_array_map_clear(struct bpf_map *map)
427409
fd_array_map_delete_elem(map, &i);
428410
}
429411

430-
static const struct bpf_map_ops prog_array_ops = {
412+
const struct bpf_map_ops prog_array_map_ops = {
431413
.map_alloc = fd_array_map_alloc,
432414
.map_free = fd_array_map_free,
433415
.map_get_next_key = array_map_get_next_key,
@@ -437,18 +419,6 @@ static const struct bpf_map_ops prog_array_ops = {
437419
.map_fd_put_ptr = prog_fd_array_put_ptr,
438420
};
439421

440-
static struct bpf_map_type_list prog_array_type __ro_after_init = {
441-
.ops = &prog_array_ops,
442-
.type = BPF_MAP_TYPE_PROG_ARRAY,
443-
};
444-
445-
static int __init register_prog_array_map(void)
446-
{
447-
bpf_register_map_type(&prog_array_type);
448-
return 0;
449-
}
450-
late_initcall(register_prog_array_map);
451-
452422
static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
453423
struct file *map_file)
454424
{
@@ -539,7 +509,7 @@ static void perf_event_fd_array_release(struct bpf_map *map,
539509
rcu_read_unlock();
540510
}
541511

542-
static const struct bpf_map_ops perf_event_array_ops = {
512+
const struct bpf_map_ops perf_event_array_map_ops = {
543513
.map_alloc = fd_array_map_alloc,
544514
.map_free = fd_array_map_free,
545515
.map_get_next_key = array_map_get_next_key,
@@ -550,18 +520,6 @@ static const struct bpf_map_ops perf_event_array_ops = {
550520
.map_release = perf_event_fd_array_release,
551521
};
552522

553-
static struct bpf_map_type_list perf_event_array_type __ro_after_init = {
554-
.ops = &perf_event_array_ops,
555-
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
556-
};
557-
558-
static int __init register_perf_event_array_map(void)
559-
{
560-
bpf_register_map_type(&perf_event_array_type);
561-
return 0;
562-
}
563-
late_initcall(register_perf_event_array_map);
564-
565523
#ifdef CONFIG_CGROUPS
566524
static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
567525
struct file *map_file /* not used */,
@@ -582,7 +540,7 @@ static void cgroup_fd_array_free(struct bpf_map *map)
582540
fd_array_map_free(map);
583541
}
584542

585-
static const struct bpf_map_ops cgroup_array_ops = {
543+
const struct bpf_map_ops cgroup_array_map_ops = {
586544
.map_alloc = fd_array_map_alloc,
587545
.map_free = cgroup_fd_array_free,
588546
.map_get_next_key = array_map_get_next_key,
@@ -591,18 +549,6 @@ static const struct bpf_map_ops cgroup_array_ops = {
591549
.map_fd_get_ptr = cgroup_fd_array_get_ptr,
592550
.map_fd_put_ptr = cgroup_fd_array_put_ptr,
593551
};
594-
595-
static struct bpf_map_type_list cgroup_array_type __ro_after_init = {
596-
.ops = &cgroup_array_ops,
597-
.type = BPF_MAP_TYPE_CGROUP_ARRAY,
598-
};
599-
600-
static int __init register_cgroup_array_map(void)
601-
{
602-
bpf_register_map_type(&cgroup_array_type);
603-
return 0;
604-
}
605-
late_initcall(register_cgroup_array_map);
606552
#endif
607553

608554
static struct bpf_map *array_of_map_alloc(union bpf_attr *attr)
@@ -644,7 +590,7 @@ static void *array_of_map_lookup_elem(struct bpf_map *map, void *key)
644590
return READ_ONCE(*inner_map);
645591
}
646592

647-
static const struct bpf_map_ops array_of_map_ops = {
593+
const struct bpf_map_ops array_of_maps_map_ops = {
648594
.map_alloc = array_of_map_alloc,
649595
.map_free = array_of_map_free,
650596
.map_get_next_key = array_map_get_next_key,
@@ -653,15 +599,3 @@ static const struct bpf_map_ops array_of_map_ops = {
653599
.map_fd_get_ptr = bpf_map_fd_get_ptr,
654600
.map_fd_put_ptr = bpf_map_fd_put_ptr,
655601
};
656-
657-
static struct bpf_map_type_list array_of_map_type __ro_after_init = {
658-
.ops = &array_of_map_ops,
659-
.type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
660-
};
661-
662-
static int __init register_array_of_map(void)
663-
{
664-
bpf_register_map_type(&array_of_map_type);
665-
return 0;
666-
}
667-
late_initcall(register_array_of_map);

kernel/bpf/hashtab.c

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ static void htab_map_free(struct bpf_map *map)
10961096
kfree(htab);
10971097
}
10981098

1099-
static const struct bpf_map_ops htab_ops = {
1099+
const struct bpf_map_ops htab_map_ops = {
11001100
.map_alloc = htab_map_alloc,
11011101
.map_free = htab_map_free,
11021102
.map_get_next_key = htab_map_get_next_key,
@@ -1106,12 +1106,7 @@ static const struct bpf_map_ops htab_ops = {
11061106
.map_gen_lookup = htab_map_gen_lookup,
11071107
};
11081108

1109-
static struct bpf_map_type_list htab_type __ro_after_init = {
1110-
.ops = &htab_ops,
1111-
.type = BPF_MAP_TYPE_HASH,
1112-
};
1113-
1114-
static const struct bpf_map_ops htab_lru_ops = {
1109+
const struct bpf_map_ops htab_lru_map_ops = {
11151110
.map_alloc = htab_map_alloc,
11161111
.map_free = htab_map_free,
11171112
.map_get_next_key = htab_map_get_next_key,
@@ -1120,11 +1115,6 @@ static const struct bpf_map_ops htab_lru_ops = {
11201115
.map_delete_elem = htab_lru_map_delete_elem,
11211116
};
11221117

1123-
static struct bpf_map_type_list htab_lru_type __ro_after_init = {
1124-
.ops = &htab_lru_ops,
1125-
.type = BPF_MAP_TYPE_LRU_HASH,
1126-
};
1127-
11281118
/* Called from eBPF program */
11291119
static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
11301120
{
@@ -1198,7 +1188,7 @@ int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
11981188
return ret;
11991189
}
12001190

1201-
static const struct bpf_map_ops htab_percpu_ops = {
1191+
const struct bpf_map_ops htab_percpu_map_ops = {
12021192
.map_alloc = htab_map_alloc,
12031193
.map_free = htab_map_free,
12041194
.map_get_next_key = htab_map_get_next_key,
@@ -1207,12 +1197,7 @@ static const struct bpf_map_ops htab_percpu_ops = {
12071197
.map_delete_elem = htab_map_delete_elem,
12081198
};
12091199

1210-
static struct bpf_map_type_list htab_percpu_type __ro_after_init = {
1211-
.ops = &htab_percpu_ops,
1212-
.type = BPF_MAP_TYPE_PERCPU_HASH,
1213-
};
1214-
1215-
static const struct bpf_map_ops htab_lru_percpu_ops = {
1200+
const struct bpf_map_ops htab_lru_percpu_map_ops = {
12161201
.map_alloc = htab_map_alloc,
12171202
.map_free = htab_map_free,
12181203
.map_get_next_key = htab_map_get_next_key,
@@ -1221,11 +1206,6 @@ static const struct bpf_map_ops htab_lru_percpu_ops = {
12211206
.map_delete_elem = htab_lru_map_delete_elem,
12221207
};
12231208

1224-
static struct bpf_map_type_list htab_lru_percpu_type __ro_after_init = {
1225-
.ops = &htab_lru_percpu_ops,
1226-
.type = BPF_MAP_TYPE_LRU_PERCPU_HASH,
1227-
};
1228-
12291209
static struct bpf_map *fd_htab_map_alloc(union bpf_attr *attr)
12301210
{
12311211
struct bpf_map *map;
@@ -1316,7 +1296,7 @@ static void htab_of_map_free(struct bpf_map *map)
13161296
fd_htab_map_free(map);
13171297
}
13181298

1319-
static const struct bpf_map_ops htab_of_map_ops = {
1299+
const struct bpf_map_ops htab_of_maps_map_ops = {
13201300
.map_alloc = htab_of_map_alloc,
13211301
.map_free = htab_of_map_free,
13221302
.map_get_next_key = htab_map_get_next_key,
@@ -1325,19 +1305,3 @@ static const struct bpf_map_ops htab_of_map_ops = {
13251305
.map_fd_get_ptr = bpf_map_fd_get_ptr,
13261306
.map_fd_put_ptr = bpf_map_fd_put_ptr,
13271307
};
1328-
1329-
static struct bpf_map_type_list htab_of_map_type __ro_after_init = {
1330-
.ops = &htab_of_map_ops,
1331-
.type = BPF_MAP_TYPE_HASH_OF_MAPS,
1332-
};
1333-
1334-
static int __init register_htab_map(void)
1335-
{
1336-
bpf_register_map_type(&htab_type);
1337-
bpf_register_map_type(&htab_percpu_type);
1338-
bpf_register_map_type(&htab_lru_type);
1339-
bpf_register_map_type(&htab_lru_percpu_type);
1340-
bpf_register_map_type(&htab_of_map_type);
1341-
return 0;
1342-
}
1343-
late_initcall(register_htab_map);

kernel/bpf/lpm_trie.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -505,23 +505,11 @@ static int trie_get_next_key(struct bpf_map *map, void *key, void *next_key)
505505
return -ENOTSUPP;
506506
}
507507

508-
static const struct bpf_map_ops trie_ops = {
508+
const struct bpf_map_ops trie_map_ops = {
509509
.map_alloc = trie_alloc,
510510
.map_free = trie_free,
511511
.map_get_next_key = trie_get_next_key,
512512
.map_lookup_elem = trie_lookup_elem,
513513
.map_update_elem = trie_update_elem,
514514
.map_delete_elem = trie_delete_elem,
515515
};
516-
517-
static struct bpf_map_type_list trie_type __ro_after_init = {
518-
.ops = &trie_ops,
519-
.type = BPF_MAP_TYPE_LPM_TRIE,
520-
};
521-
522-
static int __init register_trie_map(void)
523-
{
524-
bpf_register_map_type(&trie_type);
525-
return 0;
526-
}
527-
late_initcall(register_trie_map);

kernel/bpf/stackmap.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -264,23 +264,11 @@ static void stack_map_free(struct bpf_map *map)
264264
put_callchain_buffers();
265265
}
266266

267-
static const struct bpf_map_ops stack_map_ops = {
267+
const struct bpf_map_ops stack_map_ops = {
268268
.map_alloc = stack_map_alloc,
269269
.map_free = stack_map_free,
270270
.map_get_next_key = stack_map_get_next_key,
271271
.map_lookup_elem = stack_map_lookup_elem,
272272
.map_update_elem = stack_map_update_elem,
273273
.map_delete_elem = stack_map_delete_elem,
274274
};
275-
276-
static struct bpf_map_type_list stack_map_type __ro_after_init = {
277-
.ops = &stack_map_ops,
278-
.type = BPF_MAP_TYPE_STACK_TRACE,
279-
};
280-
281-
static int __init register_stack_map(void)
282-
{
283-
bpf_register_map_type(&stack_map_type);
284-
return 0;
285-
}
286-
late_initcall(register_stack_map);

0 commit comments

Comments
 (0)