Skip to content

Commit

Permalink
bpf: Use bpf_map_area_alloc consistently on bpf map creation
Browse files Browse the repository at this point in the history
Let's use the generic helper bpf_map_area_alloc() instead of the
open-coded kzalloc helpers in bpf maps creation path.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Link: https://lore.kernel.org/r/20220810151840.16394-5-laoar.shao@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Yafang Shao authored and Alexei Starovoitov committed Aug 10, 2022
1 parent 992c9e1 commit 73cf09a
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 29 deletions.
6 changes: 3 additions & 3 deletions kernel/bpf/bpf_local_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ void bpf_local_storage_map_free(struct bpf_local_storage_map *smap,
synchronize_rcu();

kvfree(smap->buckets);
kfree(smap);
bpf_map_area_free(smap);
}

int bpf_local_storage_map_alloc_check(union bpf_attr *attr)
Expand Down Expand Up @@ -610,7 +610,7 @@ struct bpf_local_storage_map *bpf_local_storage_map_alloc(union bpf_attr *attr)
unsigned int i;
u32 nbuckets;

smap = kzalloc(sizeof(*smap), GFP_USER | __GFP_NOWARN | __GFP_ACCOUNT);
smap = bpf_map_area_alloc(sizeof(*smap), NUMA_NO_NODE);
if (!smap)
return ERR_PTR(-ENOMEM);
bpf_map_init_from_attr(&smap->map, attr);
Expand All @@ -623,7 +623,7 @@ struct bpf_local_storage_map *bpf_local_storage_map_alloc(union bpf_attr *attr)
smap->buckets = kvcalloc(sizeof(*smap->buckets), nbuckets,
GFP_USER | __GFP_NOWARN | __GFP_ACCOUNT);
if (!smap->buckets) {
kfree(smap);
bpf_map_area_free(smap);
return ERR_PTR(-ENOMEM);
}

Expand Down
6 changes: 3 additions & 3 deletions kernel/bpf/cpumap.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static struct bpf_map *cpu_map_alloc(union bpf_attr *attr)
attr->map_flags & ~BPF_F_NUMA_NODE)
return ERR_PTR(-EINVAL);

cmap = kzalloc(sizeof(*cmap), GFP_USER | __GFP_NOWARN | __GFP_ACCOUNT);
cmap = bpf_map_area_alloc(sizeof(*cmap), NUMA_NO_NODE);
if (!cmap)
return ERR_PTR(-ENOMEM);

Expand All @@ -118,7 +118,7 @@ static struct bpf_map *cpu_map_alloc(union bpf_attr *attr)

return &cmap->map;
free_cmap:
kfree(cmap);
bpf_map_area_free(cmap);
return ERR_PTR(err);
}

Expand Down Expand Up @@ -623,7 +623,7 @@ static void cpu_map_free(struct bpf_map *map)
__cpu_map_entry_replace(cmap, i, NULL); /* call_rcu */
}
bpf_map_area_free(cmap->cpu_map);
kfree(cmap);
bpf_map_area_free(cmap);
}

/* Elements are kept alive by RCU; either by rcu_read_lock() (from syscall) or
Expand Down
6 changes: 3 additions & 3 deletions kernel/bpf/devmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
if (!capable(CAP_NET_ADMIN))
return ERR_PTR(-EPERM);

dtab = kzalloc(sizeof(*dtab), GFP_USER | __GFP_NOWARN | __GFP_ACCOUNT);
dtab = bpf_map_area_alloc(sizeof(*dtab), NUMA_NO_NODE);
if (!dtab)
return ERR_PTR(-ENOMEM);

err = dev_map_init_map(dtab, attr);
if (err) {
kfree(dtab);
bpf_map_area_free(dtab);
return ERR_PTR(err);
}

Expand Down Expand Up @@ -240,7 +240,7 @@ static void dev_map_free(struct bpf_map *map)
bpf_map_area_free(dtab->netdev_map);
}

kfree(dtab);
bpf_map_area_free(dtab);
}

static int dev_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
Expand Down
6 changes: 3 additions & 3 deletions kernel/bpf/hashtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
struct bpf_htab *htab;
int err, i;

htab = kzalloc(sizeof(*htab), GFP_USER | __GFP_NOWARN | __GFP_ACCOUNT);
htab = bpf_map_area_alloc(sizeof(*htab), NUMA_NO_NODE);
if (!htab)
return ERR_PTR(-ENOMEM);

Expand Down Expand Up @@ -579,7 +579,7 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
bpf_map_area_free(htab->buckets);
free_htab:
lockdep_unregister_key(&htab->lockdep_key);
kfree(htab);
bpf_map_area_free(htab);
return ERR_PTR(err);
}

Expand Down Expand Up @@ -1496,7 +1496,7 @@ static void htab_map_free(struct bpf_map *map)
for (i = 0; i < HASHTAB_MAP_LOCK_COUNT; i++)
free_percpu(htab->map_locked[i]);
lockdep_unregister_key(&htab->lockdep_key);
kfree(htab);
bpf_map_area_free(htab);
}

static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
Expand Down
5 changes: 2 additions & 3 deletions kernel/bpf/local_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,7 @@ static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
/* max_entries is not used and enforced to be 0 */
return ERR_PTR(-EINVAL);

map = kzalloc_node(sizeof(struct bpf_cgroup_storage_map),
GFP_USER | __GFP_NOWARN | __GFP_ACCOUNT, numa_node);
map = bpf_map_area_alloc(sizeof(struct bpf_cgroup_storage_map), numa_node);
if (!map)
return ERR_PTR(-ENOMEM);

Expand Down Expand Up @@ -346,7 +345,7 @@ static void cgroup_storage_map_free(struct bpf_map *_map)
WARN_ON(!RB_EMPTY_ROOT(&map->root));
WARN_ON(!list_empty(&map->list));

kfree(map);
bpf_map_area_free(map);
}

static int cgroup_storage_delete_elem(struct bpf_map *map, void *key)
Expand Down
4 changes: 2 additions & 2 deletions kernel/bpf/lpm_trie.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ static struct bpf_map *trie_alloc(union bpf_attr *attr)
attr->value_size > LPM_VAL_SIZE_MAX)
return ERR_PTR(-EINVAL);

trie = kzalloc(sizeof(*trie), GFP_USER | __GFP_NOWARN | __GFP_ACCOUNT);
trie = bpf_map_area_alloc(sizeof(*trie), NUMA_NO_NODE);
if (!trie)
return ERR_PTR(-ENOMEM);

Expand Down Expand Up @@ -609,7 +609,7 @@ static void trie_free(struct bpf_map *map)
}

out:
kfree(trie);
bpf_map_area_free(trie);
}

static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key)
Expand Down
6 changes: 3 additions & 3 deletions kernel/bpf/offload.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
attr->map_type != BPF_MAP_TYPE_HASH)
return ERR_PTR(-EINVAL);

offmap = kzalloc(sizeof(*offmap), GFP_USER | __GFP_NOWARN);
offmap = bpf_map_area_alloc(sizeof(*offmap), NUMA_NO_NODE);
if (!offmap)
return ERR_PTR(-ENOMEM);

Expand Down Expand Up @@ -404,7 +404,7 @@ struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
err_unlock:
up_write(&bpf_devs_lock);
rtnl_unlock();
kfree(offmap);
bpf_map_area_free(offmap);
return ERR_PTR(err);
}

Expand All @@ -428,7 +428,7 @@ void bpf_map_offload_map_free(struct bpf_map *map)
up_write(&bpf_devs_lock);
rtnl_unlock();

kfree(offmap);
bpf_map_area_free(offmap);
}

int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value)
Expand Down
6 changes: 3 additions & 3 deletions kernel/bpf/ringbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ static struct bpf_map *ringbuf_map_alloc(union bpf_attr *attr)
return ERR_PTR(-E2BIG);
#endif

rb_map = kzalloc(sizeof(*rb_map), GFP_USER | __GFP_NOWARN | __GFP_ACCOUNT);
rb_map = bpf_map_area_alloc(sizeof(*rb_map), NUMA_NO_NODE);
if (!rb_map)
return ERR_PTR(-ENOMEM);

bpf_map_init_from_attr(&rb_map->map, attr);

rb_map->rb = bpf_ringbuf_alloc(attr->max_entries, rb_map->map.numa_node);
if (!rb_map->rb) {
kfree(rb_map);
bpf_map_area_free(rb_map);
return ERR_PTR(-ENOMEM);
}

Expand All @@ -199,7 +199,7 @@ static void ringbuf_map_free(struct bpf_map *map)

rb_map = container_of(map, struct bpf_ringbuf_map, map);
bpf_ringbuf_free(rb_map->rb);
kfree(rb_map);
bpf_map_area_free(rb_map);
}

static void *ringbuf_map_lookup_elem(struct bpf_map *map, void *key)
Expand Down
12 changes: 6 additions & 6 deletions net/core/sock_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
return ERR_PTR(-EINVAL);

stab = kzalloc(sizeof(*stab), GFP_USER | __GFP_NOWARN | __GFP_ACCOUNT);
stab = bpf_map_area_alloc(sizeof(*stab), NUMA_NO_NODE);
if (!stab)
return ERR_PTR(-ENOMEM);

Expand All @@ -52,7 +52,7 @@ static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
sizeof(struct sock *),
stab->map.numa_node);
if (!stab->sks) {
kfree(stab);
bpf_map_area_free(stab);
return ERR_PTR(-ENOMEM);
}

Expand Down Expand Up @@ -361,7 +361,7 @@ static void sock_map_free(struct bpf_map *map)
synchronize_rcu();

bpf_map_area_free(stab->sks);
kfree(stab);
bpf_map_area_free(stab);
}

static void sock_map_release_progs(struct bpf_map *map)
Expand Down Expand Up @@ -1076,7 +1076,7 @@ static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
if (attr->key_size > MAX_BPF_STACK)
return ERR_PTR(-E2BIG);

htab = kzalloc(sizeof(*htab), GFP_USER | __GFP_NOWARN | __GFP_ACCOUNT);
htab = bpf_map_area_alloc(sizeof(*htab), NUMA_NO_NODE);
if (!htab)
return ERR_PTR(-ENOMEM);

Expand Down Expand Up @@ -1106,7 +1106,7 @@ static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)

return &htab->map;
free_htab:
kfree(htab);
bpf_map_area_free(htab);
return ERR_PTR(err);
}

Expand Down Expand Up @@ -1159,7 +1159,7 @@ static void sock_hash_free(struct bpf_map *map)
synchronize_rcu();

bpf_map_area_free(htab->buckets);
kfree(htab);
bpf_map_area_free(htab);
}

static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
Expand Down

0 comments on commit 73cf09a

Please sign in to comment.