@@ -1634,9 +1634,9 @@ void bpf_map_inc_with_uref(struct bpf_map *map);
struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
void bpf_map_put_with_uref(struct bpf_map *map);
void bpf_map_put(struct bpf_map *map);
-void *bpf_map_container_alloc(u64 size, int numa_node);
-void *bpf_map_container_mmapable_alloc(u64 size, int numa_node,
- u32 align, u32 offset);
+void *bpf_map_container_alloc(union bpf_attr *attr, u64 size, int numa_node);
+void *bpf_map_container_mmapable_alloc(union bpf_attr *attr, u64 size,
+ int numa_node, u32 align, u32 offset);
void *bpf_map_area_alloc(struct bpf_map *map, u64 size, int numa_node);
void *bpf_map_pages_alloc(struct bpf_map *map, struct page **pages,
int nr_meta_pages, int nr_data_pages, int nid,
@@ -131,16 +131,17 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)
void *data;
/* kmalloc'ed memory can't be mmap'ed, use explicit vmalloc */
- data = bpf_map_container_mmapable_alloc(array_size, numa_node,
- align, offset);
- if (!data)
- return ERR_PTR(-ENOMEM);
+ data = bpf_map_container_mmapable_alloc(attr, array_size,
+ numa_node, align,
+ offset);
+ if (IS_ERR(data))
+ return data;
array = data + align - offset;
} else {
- array = bpf_map_container_alloc(array_size, numa_node);
+ array = bpf_map_container_alloc(attr, array_size, numa_node);
}
- if (!array)
- return ERR_PTR(-ENOMEM);
+ if (IS_ERR(array))
+ return ERR_CAST(array);
array->index_mask = index_mask;
array->map.bypass_spec_v1 = bypass_spec_v1;
@@ -142,10 +142,11 @@ static struct bpf_map *bloom_map_alloc(union bpf_attr *attr)
}
bitset_bytes = roundup(bitset_bytes, sizeof(unsigned long));
- bloom = bpf_map_container_alloc(sizeof(*bloom) + bitset_bytes, numa_node);
+ bloom = bpf_map_container_alloc(attr, sizeof(*bloom) + bitset_bytes,
+ numa_node);
- if (!bloom)
- return ERR_PTR(-ENOMEM);
+ if (IS_ERR(bloom))
+ return ERR_CAST(bloom);
bpf_map_init_from_attr(&bloom->map, attr);
@@ -610,9 +610,9 @@ struct bpf_local_storage_map *bpf_local_storage_map_alloc(union bpf_attr *attr)
unsigned int i;
u32 nbuckets;
- smap = bpf_map_container_alloc(sizeof(*smap), NUMA_NO_NODE);
- if (!smap)
- return ERR_PTR(-ENOMEM);
+ smap = bpf_map_container_alloc(attr, sizeof(*smap), NUMA_NO_NODE);
+ if (IS_ERR(smap))
+ return ERR_CAST(smap);
bpf_map_init_from_attr(&smap->map, attr);
nbuckets = roundup_pow_of_two(num_possible_cpus());
@@ -618,9 +618,9 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
*/
(vt->size - sizeof(struct bpf_struct_ops_value));
- st_map = bpf_map_container_alloc(st_map_size, NUMA_NO_NODE);
- if (!st_map)
- return ERR_PTR(-ENOMEM);
+ st_map = bpf_map_container_alloc(attr, st_map_size, NUMA_NO_NODE);
+ if (IS_ERR(st_map))
+ return ERR_CAST(st_map);
st_map->st_ops = st_ops;
map = &st_map->map;
@@ -97,9 +97,9 @@ static struct bpf_map *cpu_map_alloc(union bpf_attr *attr)
attr->map_flags & ~BPF_F_NUMA_NODE)
return ERR_PTR(-EINVAL);
- cmap = bpf_map_container_alloc(sizeof(*cmap), NUMA_NO_NODE);
- if (!cmap)
- return ERR_PTR(-ENOMEM);
+ cmap = bpf_map_container_alloc(attr, sizeof(*cmap), NUMA_NO_NODE);
+ if (IS_ERR(cmap))
+ return ERR_CAST(cmap);
bpf_map_init_from_attr(&cmap->map, attr);
@@ -167,9 +167,9 @@ static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
if (!capable(CAP_NET_ADMIN))
return ERR_PTR(-EPERM);
- dtab = bpf_map_container_alloc(sizeof(*dtab), NUMA_NO_NODE);
- if (!dtab)
- return ERR_PTR(-ENOMEM);
+ dtab = bpf_map_container_alloc(attr, sizeof(*dtab), NUMA_NO_NODE);
+ if (IS_ERR(dtab))
+ return ERR_CAST(dtab);
err = dev_map_init_map(dtab, attr);
if (err) {
@@ -496,9 +496,9 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
struct bpf_htab *htab;
int err, i;
- htab = bpf_map_container_alloc(sizeof(*htab), NUMA_NO_NODE);
- if (!htab)
- return ERR_PTR(-ENOMEM);
+ htab = bpf_map_container_alloc(attr, sizeof(*htab), NUMA_NO_NODE);
+ if (IS_ERR(htab))
+ return ERR_CAST(htab);
lockdep_register_key(&htab->lockdep_key);
@@ -313,9 +313,9 @@ 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 = bpf_map_container_alloc(sizeof(struct bpf_cgroup_storage_map), numa_node);
- if (!map)
- return ERR_PTR(-ENOMEM);
+ map = bpf_map_container_alloc(attr, sizeof(*map), numa_node);
+ if (IS_ERR(map))
+ return ERR_CAST(map);
/* copy mandatory map attributes */
bpf_map_init_from_attr(&map->map, attr);
@@ -558,9 +558,9 @@ static struct bpf_map *trie_alloc(union bpf_attr *attr)
attr->value_size > LPM_VAL_SIZE_MAX)
return ERR_PTR(-EINVAL);
- trie = bpf_map_container_alloc(sizeof(*trie), NUMA_NO_NODE);
- if (!trie)
- return ERR_PTR(-ENOMEM);
+ trie = bpf_map_container_alloc(attr, sizeof(*trie), NUMA_NO_NODE);
+ if (IS_ERR(trie))
+ return ERR_CAST(trie);
/* copy mandatory map attributes */
bpf_map_init_from_attr(&trie->map, attr);
@@ -372,9 +372,9 @@ struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
attr->map_type != BPF_MAP_TYPE_HASH)
return ERR_PTR(-EINVAL);
- offmap = bpf_map_container_alloc(sizeof(*offmap), NUMA_NO_NODE);
- if (!offmap)
- return ERR_PTR(-ENOMEM);
+ offmap = bpf_map_container_alloc(attr, sizeof(*offmap), NUMA_NO_NODE);
+ if (IS_ERR(offmap))
+ return ERR_CAST(offmap);
bpf_map_init_from_attr(&offmap->map, attr);
@@ -74,9 +74,9 @@ static struct bpf_map *queue_stack_map_alloc(union bpf_attr *attr)
size = (u64) attr->max_entries + 1;
queue_size = sizeof(*qs) + size * attr->value_size;
- qs = bpf_map_container_alloc(queue_size, numa_node);
- if (!qs)
- return ERR_PTR(-ENOMEM);
+ qs = bpf_map_container_alloc(attr, queue_size, numa_node);
+ if (IS_ERR(qs))
+ return ERR_CAST(qs);
bpf_map_init_from_attr(&qs->map, attr);
@@ -153,14 +153,15 @@ static struct bpf_map *reuseport_array_alloc(union bpf_attr *attr)
{
int numa_node = bpf_map_attr_numa_node(attr);
struct reuseport_array *array;
+ size_t size = struct_size(array, ptrs, attr->max_entries);
if (!bpf_capable())
return ERR_PTR(-EPERM);
/* allocate all map elements and zero-initialize them */
- array = bpf_map_container_alloc(struct_size(array, ptrs, attr->max_entries), numa_node);
- if (!array)
- return ERR_PTR(-ENOMEM);
+ array = bpf_map_container_alloc(attr, size, numa_node);
+ if (IS_ERR(array))
+ return ERR_CAST(array);
/* copy mandatory map attributes */
bpf_map_init_from_attr(&array->map, attr);
@@ -159,9 +159,9 @@ static struct bpf_map *ringbuf_map_alloc(union bpf_attr *attr)
return ERR_PTR(-E2BIG);
#endif
- rb_map = bpf_map_container_alloc(sizeof(*rb_map), NUMA_NO_NODE);
- if (!rb_map)
- return ERR_PTR(-ENOMEM);
+ rb_map = bpf_map_container_alloc(attr, sizeof(*rb_map), NUMA_NO_NODE);
+ if (IS_ERR(rb_map))
+ return ERR_CAST(rb_map);
bpf_map_init_from_attr(&rb_map->map, attr);
@@ -101,9 +101,9 @@ static struct bpf_map *stack_map_alloc(union bpf_attr *attr)
return ERR_PTR(-E2BIG);
cost = n_buckets * sizeof(struct stack_map_bucket *) + sizeof(*smap);
- smap = bpf_map_container_alloc(cost, bpf_map_attr_numa_node(attr));
- if (!smap)
- return ERR_PTR(-ENOMEM);
+ smap = bpf_map_container_alloc(attr, cost, bpf_map_attr_numa_node(attr));
+ if (IS_ERR(smap))
+ return ERR_CAST(smap);
bpf_map_init_from_attr(&smap->map, attr);
smap->n_buckets = n_buckets;
@@ -526,14 +526,14 @@ void bpf_map_area_free(void *area)
*
* It is used in map creation path.
*/
-void *bpf_map_container_alloc(u64 size, int numa_node)
+void *bpf_map_container_alloc(union bpf_attr *attr, u64 size, int numa_node)
{
struct bpf_map *map;
void *container;
container = __bpf_map_area_alloc(size, numa_node, false);
if (!container)
- return NULL;
+ return ERR_PTR(-ENOMEM);
map = (struct bpf_map *)container;
bpf_map_save_memcg(map);
@@ -541,8 +541,8 @@ void *bpf_map_container_alloc(u64 size, int numa_node)
return container;
}
-void *bpf_map_container_mmapable_alloc(u64 size, int numa_node, u32 align,
- u32 offset)
+void *bpf_map_container_mmapable_alloc(union bpf_attr *attr, u64 size,
+ int numa_node, u32 align, u32 offset)
{
struct bpf_map *map;
void *container;
@@ -551,7 +551,7 @@ void *bpf_map_container_mmapable_alloc(u64 size, int numa_node, u32 align,
/* kmalloc'ed memory can't be mmap'ed, use explicit vmalloc */
ptr = __bpf_map_area_alloc(size, numa_node, true);
if (!ptr)
- return NULL;
+ return ERR_PTR(-ENOMEM);
container = ptr + align - offset;
map = (struct bpf_map *)container;
@@ -41,9 +41,9 @@ static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
return ERR_PTR(-EINVAL);
- stab = bpf_map_container_alloc(sizeof(*stab), NUMA_NO_NODE);
- if (!stab)
- return ERR_PTR(-ENOMEM);
+ stab = bpf_map_container_alloc(attr, sizeof(*stab), NUMA_NO_NODE);
+ if (IS_ERR(stab))
+ return ERR_CAST(stab);
bpf_map_init_from_attr(&stab->map, attr);
raw_spin_lock_init(&stab->lock);
@@ -1077,9 +1077,9 @@ static struct bpf_map *sock_hash_alloc(union bpf_attr *attr)
if (attr->key_size > MAX_BPF_STACK)
return ERR_PTR(-E2BIG);
- htab = bpf_map_container_alloc(sizeof(*htab), NUMA_NO_NODE);
- if (!htab)
- return ERR_PTR(-ENOMEM);
+ htab = bpf_map_container_alloc(attr, sizeof(*htab), NUMA_NO_NODE);
+ if (IS_ERR(htab))
+ return ERR_CAST(htab);
bpf_map_init_from_attr(&htab->map, attr);
@@ -75,9 +75,9 @@ static struct bpf_map *xsk_map_alloc(union bpf_attr *attr)
numa_node = bpf_map_attr_numa_node(attr);
size = struct_size(m, xsk_map, attr->max_entries);
- m = bpf_map_container_alloc(size, numa_node);
- if (!m)
- return ERR_PTR(-ENOMEM);
+ m = bpf_map_container_alloc(attr, size, numa_node);
+ if (IS_ERR(m))
+ return ERR_CAST(m);
bpf_map_init_from_attr(&m->map, attr);
spin_lock_init(&m->lock);
Add bpf attr into bpf_map_container_alloc(), then we can get map creation related attributes in these function. Signed-off-by: Yafang Shao <laoar.shao@gmail.com> --- include/linux/bpf.h | 6 +++--- kernel/bpf/arraymap.c | 15 ++++++++------- kernel/bpf/bloom_filter.c | 7 ++++--- kernel/bpf/bpf_local_storage.c | 6 +++--- kernel/bpf/bpf_struct_ops.c | 6 +++--- kernel/bpf/cpumap.c | 6 +++--- kernel/bpf/devmap.c | 6 +++--- kernel/bpf/hashtab.c | 6 +++--- kernel/bpf/local_storage.c | 6 +++--- kernel/bpf/lpm_trie.c | 6 +++--- kernel/bpf/offload.c | 6 +++--- kernel/bpf/queue_stack_maps.c | 6 +++--- kernel/bpf/reuseport_array.c | 7 ++++--- kernel/bpf/ringbuf.c | 6 +++--- kernel/bpf/stackmap.c | 6 +++--- kernel/bpf/syscall.c | 10 +++++----- net/core/sock_map.c | 12 ++++++------ net/xdp/xskmap.c | 6 +++--- 18 files changed, 66 insertions(+), 63 deletions(-)