Skip to content

Commit bd47564

Browse files
Jakub Kicinskiborkmann
authored andcommitted
bpf: add helper for copying attrs to struct bpf_map
All map types reimplement the field-by-field copy of union bpf_attr members into struct bpf_map. Add a helper to perform this operation. Signed-off-by: Jakub Kicinski <[email protected]> Reviewed-by: Quentin Monnet <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 9328e0d commit bd47564

File tree

8 files changed

+17
-40
lines changed

8 files changed

+17
-40
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ void bpf_map_put(struct bpf_map *map);
378378
int bpf_map_precharge_memlock(u32 pages);
379379
void *bpf_map_area_alloc(size_t size, int numa_node);
380380
void bpf_map_area_free(void *base);
381+
void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
381382

382383
extern int sysctl_unprivileged_bpf_disabled;
383384

kernel/bpf/cpumap.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,7 @@ static struct bpf_map *cpu_map_alloc(union bpf_attr *attr)
9494
if (!cmap)
9595
return ERR_PTR(-ENOMEM);
9696

97-
/* mandatory map attributes */
98-
cmap->map.map_type = attr->map_type;
99-
cmap->map.key_size = attr->key_size;
100-
cmap->map.value_size = attr->value_size;
101-
cmap->map.max_entries = attr->max_entries;
102-
cmap->map.map_flags = attr->map_flags;
103-
cmap->map.numa_node = bpf_map_attr_numa_node(attr);
97+
bpf_map_init_from_attr(&cmap->map, attr);
10498

10599
/* Pre-limit array size based on NR_CPUS, not final CPU check */
106100
if (cmap->map.max_entries > NR_CPUS) {

kernel/bpf/devmap.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,7 @@ static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
9393
if (!dtab)
9494
return ERR_PTR(-ENOMEM);
9595

96-
/* mandatory map attributes */
97-
dtab->map.map_type = attr->map_type;
98-
dtab->map.key_size = attr->key_size;
99-
dtab->map.value_size = attr->value_size;
100-
dtab->map.max_entries = attr->max_entries;
101-
dtab->map.map_flags = attr->map_flags;
102-
dtab->map.numa_node = bpf_map_attr_numa_node(attr);
96+
bpf_map_init_from_attr(&dtab->map, attr);
10397

10498
/* make sure page count doesn't overflow */
10599
cost = (u64) dtab->map.max_entries * sizeof(struct bpf_dtab_netdev *);

kernel/bpf/hashtab.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,6 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
304304
*/
305305
bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
306306
bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
307-
int numa_node = bpf_map_attr_numa_node(attr);
308307
struct bpf_htab *htab;
309308
int err, i;
310309
u64 cost;
@@ -313,13 +312,7 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
313312
if (!htab)
314313
return ERR_PTR(-ENOMEM);
315314

316-
/* mandatory map attributes */
317-
htab->map.map_type = attr->map_type;
318-
htab->map.key_size = attr->key_size;
319-
htab->map.value_size = attr->value_size;
320-
htab->map.max_entries = attr->max_entries;
321-
htab->map.map_flags = attr->map_flags;
322-
htab->map.numa_node = numa_node;
315+
bpf_map_init_from_attr(&htab->map, attr);
323316

324317
if (percpu_lru) {
325318
/* ensure each CPU's lru list has >=1 elements.

kernel/bpf/lpm_trie.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -522,12 +522,7 @@ static struct bpf_map *trie_alloc(union bpf_attr *attr)
522522
return ERR_PTR(-ENOMEM);
523523

524524
/* copy mandatory map attributes */
525-
trie->map.map_type = attr->map_type;
526-
trie->map.key_size = attr->key_size;
527-
trie->map.value_size = attr->value_size;
528-
trie->map.max_entries = attr->max_entries;
529-
trie->map.map_flags = attr->map_flags;
530-
trie->map.numa_node = bpf_map_attr_numa_node(attr);
525+
bpf_map_init_from_attr(&trie->map, attr);
531526
trie->data_size = attr->key_size -
532527
offsetof(struct bpf_lpm_trie_key, data);
533528
trie->max_prefixlen = trie->data_size * 8;

kernel/bpf/sockmap.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -513,13 +513,7 @@ static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
513513
if (!stab)
514514
return ERR_PTR(-ENOMEM);
515515

516-
/* mandatory map attributes */
517-
stab->map.map_type = attr->map_type;
518-
stab->map.key_size = attr->key_size;
519-
stab->map.value_size = attr->value_size;
520-
stab->map.max_entries = attr->max_entries;
521-
stab->map.map_flags = attr->map_flags;
522-
stab->map.numa_node = bpf_map_attr_numa_node(attr);
516+
bpf_map_init_from_attr(&stab->map, attr);
523517

524518
/* make sure page count doesn't overflow */
525519
cost = (u64) stab->map.max_entries * sizeof(struct sock *);

kernel/bpf/stackmap.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,10 @@ static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
8888
if (cost >= U32_MAX - PAGE_SIZE)
8989
goto free_smap;
9090

91-
smap->map.map_type = attr->map_type;
92-
smap->map.key_size = attr->key_size;
91+
bpf_map_init_from_attr(&smap->map, attr);
9392
smap->map.value_size = value_size;
94-
smap->map.max_entries = attr->max_entries;
95-
smap->map.map_flags = attr->map_flags;
9693
smap->n_buckets = n_buckets;
9794
smap->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
98-
smap->map.numa_node = bpf_map_attr_numa_node(attr);
9995

10096
err = bpf_map_precharge_memlock(smap->map.pages);
10197
if (err)

kernel/bpf/syscall.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ void bpf_map_area_free(void *area)
143143
kvfree(area);
144144
}
145145

146+
void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
147+
{
148+
map->map_type = attr->map_type;
149+
map->key_size = attr->key_size;
150+
map->value_size = attr->value_size;
151+
map->max_entries = attr->max_entries;
152+
map->map_flags = attr->map_flags;
153+
map->numa_node = bpf_map_attr_numa_node(attr);
154+
}
155+
146156
int bpf_map_precharge_memlock(u32 pages)
147157
{
148158
struct user_struct *user = get_current_user();

0 commit comments

Comments
 (0)