xref: /linux-6.15/kernel/bpf/arraymap.c (revision b2157399)
128fbcfa0SAlexei Starovoitov /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
281ed18abSAlexei Starovoitov  * Copyright (c) 2016,2017 Facebook
328fbcfa0SAlexei Starovoitov  *
428fbcfa0SAlexei Starovoitov  * This program is free software; you can redistribute it and/or
528fbcfa0SAlexei Starovoitov  * modify it under the terms of version 2 of the GNU General Public
628fbcfa0SAlexei Starovoitov  * License as published by the Free Software Foundation.
728fbcfa0SAlexei Starovoitov  *
828fbcfa0SAlexei Starovoitov  * This program is distributed in the hope that it will be useful, but
928fbcfa0SAlexei Starovoitov  * WITHOUT ANY WARRANTY; without even the implied warranty of
1028fbcfa0SAlexei Starovoitov  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1128fbcfa0SAlexei Starovoitov  * General Public License for more details.
1228fbcfa0SAlexei Starovoitov  */
1328fbcfa0SAlexei Starovoitov #include <linux/bpf.h>
1428fbcfa0SAlexei Starovoitov #include <linux/err.h>
1528fbcfa0SAlexei Starovoitov #include <linux/slab.h>
1628fbcfa0SAlexei Starovoitov #include <linux/mm.h>
1704fd61abSAlexei Starovoitov #include <linux/filter.h>
180cdf5640SDaniel Borkmann #include <linux/perf_event.h>
1928fbcfa0SAlexei Starovoitov 
2056f668dfSMartin KaFai Lau #include "map_in_map.h"
2156f668dfSMartin KaFai Lau 
226e71b04aSChenbo Feng #define ARRAY_CREATE_FLAG_MASK \
236e71b04aSChenbo Feng 	(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
246e71b04aSChenbo Feng 
25a10423b8SAlexei Starovoitov static void bpf_array_free_percpu(struct bpf_array *array)
26a10423b8SAlexei Starovoitov {
27a10423b8SAlexei Starovoitov 	int i;
28a10423b8SAlexei Starovoitov 
29a10423b8SAlexei Starovoitov 	for (i = 0; i < array->map.max_entries; i++)
30a10423b8SAlexei Starovoitov 		free_percpu(array->pptrs[i]);
31a10423b8SAlexei Starovoitov }
32a10423b8SAlexei Starovoitov 
33a10423b8SAlexei Starovoitov static int bpf_array_alloc_percpu(struct bpf_array *array)
34a10423b8SAlexei Starovoitov {
35a10423b8SAlexei Starovoitov 	void __percpu *ptr;
36a10423b8SAlexei Starovoitov 	int i;
37a10423b8SAlexei Starovoitov 
38a10423b8SAlexei Starovoitov 	for (i = 0; i < array->map.max_entries; i++) {
39a10423b8SAlexei Starovoitov 		ptr = __alloc_percpu_gfp(array->elem_size, 8,
40a10423b8SAlexei Starovoitov 					 GFP_USER | __GFP_NOWARN);
41a10423b8SAlexei Starovoitov 		if (!ptr) {
42a10423b8SAlexei Starovoitov 			bpf_array_free_percpu(array);
43a10423b8SAlexei Starovoitov 			return -ENOMEM;
44a10423b8SAlexei Starovoitov 		}
45a10423b8SAlexei Starovoitov 		array->pptrs[i] = ptr;
46a10423b8SAlexei Starovoitov 	}
47a10423b8SAlexei Starovoitov 
48a10423b8SAlexei Starovoitov 	return 0;
49a10423b8SAlexei Starovoitov }
50a10423b8SAlexei Starovoitov 
5128fbcfa0SAlexei Starovoitov /* Called from syscall */
5228fbcfa0SAlexei Starovoitov static struct bpf_map *array_map_alloc(union bpf_attr *attr)
5328fbcfa0SAlexei Starovoitov {
54a10423b8SAlexei Starovoitov 	bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
5596eabe7aSMartin KaFai Lau 	int numa_node = bpf_map_attr_numa_node(attr);
56*b2157399SAlexei Starovoitov 	u32 elem_size, index_mask, max_entries;
57*b2157399SAlexei Starovoitov 	bool unpriv = !capable(CAP_SYS_ADMIN);
5828fbcfa0SAlexei Starovoitov 	struct bpf_array *array;
59a10423b8SAlexei Starovoitov 	u64 array_size;
6028fbcfa0SAlexei Starovoitov 
6128fbcfa0SAlexei Starovoitov 	/* check sanity of attributes */
6228fbcfa0SAlexei Starovoitov 	if (attr->max_entries == 0 || attr->key_size != 4 ||
636e71b04aSChenbo Feng 	    attr->value_size == 0 ||
646e71b04aSChenbo Feng 	    attr->map_flags & ~ARRAY_CREATE_FLAG_MASK ||
6596eabe7aSMartin KaFai Lau 	    (percpu && numa_node != NUMA_NO_NODE))
6628fbcfa0SAlexei Starovoitov 		return ERR_PTR(-EINVAL);
6728fbcfa0SAlexei Starovoitov 
687984c27cSMichal Hocko 	if (attr->value_size > KMALLOC_MAX_SIZE)
6901b3f521SAlexei Starovoitov 		/* if value_size is bigger, the user space won't be able to
7001b3f521SAlexei Starovoitov 		 * access the elements.
7101b3f521SAlexei Starovoitov 		 */
7201b3f521SAlexei Starovoitov 		return ERR_PTR(-E2BIG);
7301b3f521SAlexei Starovoitov 
7428fbcfa0SAlexei Starovoitov 	elem_size = round_up(attr->value_size, 8);
7528fbcfa0SAlexei Starovoitov 
76*b2157399SAlexei Starovoitov 	max_entries = attr->max_entries;
77*b2157399SAlexei Starovoitov 	index_mask = roundup_pow_of_two(max_entries) - 1;
78*b2157399SAlexei Starovoitov 
79*b2157399SAlexei Starovoitov 	if (unpriv)
80*b2157399SAlexei Starovoitov 		/* round up array size to nearest power of 2,
81*b2157399SAlexei Starovoitov 		 * since cpu will speculate within index_mask limits
82*b2157399SAlexei Starovoitov 		 */
83*b2157399SAlexei Starovoitov 		max_entries = index_mask + 1;
84*b2157399SAlexei Starovoitov 
85a10423b8SAlexei Starovoitov 	array_size = sizeof(*array);
86a10423b8SAlexei Starovoitov 	if (percpu)
87*b2157399SAlexei Starovoitov 		array_size += (u64) max_entries * sizeof(void *);
88a10423b8SAlexei Starovoitov 	else
89*b2157399SAlexei Starovoitov 		array_size += (u64) max_entries * elem_size;
90a10423b8SAlexei Starovoitov 
91a10423b8SAlexei Starovoitov 	/* make sure there is no u32 overflow later in round_up() */
92a10423b8SAlexei Starovoitov 	if (array_size >= U32_MAX - PAGE_SIZE)
93daaf427cSAlexei Starovoitov 		return ERR_PTR(-ENOMEM);
94daaf427cSAlexei Starovoitov 
9528fbcfa0SAlexei Starovoitov 	/* allocate all map elements and zero-initialize them */
9696eabe7aSMartin KaFai Lau 	array = bpf_map_area_alloc(array_size, numa_node);
9728fbcfa0SAlexei Starovoitov 	if (!array)
9828fbcfa0SAlexei Starovoitov 		return ERR_PTR(-ENOMEM);
99*b2157399SAlexei Starovoitov 	array->index_mask = index_mask;
100*b2157399SAlexei Starovoitov 	array->map.unpriv_array = unpriv;
10128fbcfa0SAlexei Starovoitov 
10228fbcfa0SAlexei Starovoitov 	/* copy mandatory map attributes */
103a10423b8SAlexei Starovoitov 	array->map.map_type = attr->map_type;
10428fbcfa0SAlexei Starovoitov 	array->map.key_size = attr->key_size;
10528fbcfa0SAlexei Starovoitov 	array->map.value_size = attr->value_size;
10628fbcfa0SAlexei Starovoitov 	array->map.max_entries = attr->max_entries;
107a316338cSDaniel Borkmann 	array->map.map_flags = attr->map_flags;
10896eabe7aSMartin KaFai Lau 	array->map.numa_node = numa_node;
10928fbcfa0SAlexei Starovoitov 	array->elem_size = elem_size;
11028fbcfa0SAlexei Starovoitov 
111a10423b8SAlexei Starovoitov 	if (!percpu)
112a10423b8SAlexei Starovoitov 		goto out;
113a10423b8SAlexei Starovoitov 
114a10423b8SAlexei Starovoitov 	array_size += (u64) attr->max_entries * elem_size * num_possible_cpus();
115a10423b8SAlexei Starovoitov 
116a10423b8SAlexei Starovoitov 	if (array_size >= U32_MAX - PAGE_SIZE ||
117bc6d5031SDaniel Borkmann 	    bpf_array_alloc_percpu(array)) {
118d407bd25SDaniel Borkmann 		bpf_map_area_free(array);
119a10423b8SAlexei Starovoitov 		return ERR_PTR(-ENOMEM);
120a10423b8SAlexei Starovoitov 	}
121a10423b8SAlexei Starovoitov out:
122a10423b8SAlexei Starovoitov 	array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
123a10423b8SAlexei Starovoitov 
12428fbcfa0SAlexei Starovoitov 	return &array->map;
12528fbcfa0SAlexei Starovoitov }
12628fbcfa0SAlexei Starovoitov 
12728fbcfa0SAlexei Starovoitov /* Called from syscall or from eBPF program */
12828fbcfa0SAlexei Starovoitov static void *array_map_lookup_elem(struct bpf_map *map, void *key)
12928fbcfa0SAlexei Starovoitov {
13028fbcfa0SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
13128fbcfa0SAlexei Starovoitov 	u32 index = *(u32 *)key;
13228fbcfa0SAlexei Starovoitov 
133a10423b8SAlexei Starovoitov 	if (unlikely(index >= array->map.max_entries))
13428fbcfa0SAlexei Starovoitov 		return NULL;
13528fbcfa0SAlexei Starovoitov 
136*b2157399SAlexei Starovoitov 	return array->value + array->elem_size * (index & array->index_mask);
13728fbcfa0SAlexei Starovoitov }
13828fbcfa0SAlexei Starovoitov 
13981ed18abSAlexei Starovoitov /* emit BPF instructions equivalent to C code of array_map_lookup_elem() */
14081ed18abSAlexei Starovoitov static u32 array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
14181ed18abSAlexei Starovoitov {
142*b2157399SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
14381ed18abSAlexei Starovoitov 	struct bpf_insn *insn = insn_buf;
144fad73a1aSMartin KaFai Lau 	u32 elem_size = round_up(map->value_size, 8);
14581ed18abSAlexei Starovoitov 	const int ret = BPF_REG_0;
14681ed18abSAlexei Starovoitov 	const int map_ptr = BPF_REG_1;
14781ed18abSAlexei Starovoitov 	const int index = BPF_REG_2;
14881ed18abSAlexei Starovoitov 
14981ed18abSAlexei Starovoitov 	*insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
15081ed18abSAlexei Starovoitov 	*insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
151*b2157399SAlexei Starovoitov 	if (map->unpriv_array) {
152*b2157399SAlexei Starovoitov 		*insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 4);
153*b2157399SAlexei Starovoitov 		*insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
154*b2157399SAlexei Starovoitov 	} else {
155fad73a1aSMartin KaFai Lau 		*insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 3);
156*b2157399SAlexei Starovoitov 	}
157fad73a1aSMartin KaFai Lau 
158fad73a1aSMartin KaFai Lau 	if (is_power_of_2(elem_size)) {
15981ed18abSAlexei Starovoitov 		*insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
16081ed18abSAlexei Starovoitov 	} else {
16181ed18abSAlexei Starovoitov 		*insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
16281ed18abSAlexei Starovoitov 	}
16381ed18abSAlexei Starovoitov 	*insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
16481ed18abSAlexei Starovoitov 	*insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
16581ed18abSAlexei Starovoitov 	*insn++ = BPF_MOV64_IMM(ret, 0);
16681ed18abSAlexei Starovoitov 	return insn - insn_buf;
16781ed18abSAlexei Starovoitov }
16881ed18abSAlexei Starovoitov 
169a10423b8SAlexei Starovoitov /* Called from eBPF program */
170a10423b8SAlexei Starovoitov static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
171a10423b8SAlexei Starovoitov {
172a10423b8SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
173a10423b8SAlexei Starovoitov 	u32 index = *(u32 *)key;
174a10423b8SAlexei Starovoitov 
175a10423b8SAlexei Starovoitov 	if (unlikely(index >= array->map.max_entries))
176a10423b8SAlexei Starovoitov 		return NULL;
177a10423b8SAlexei Starovoitov 
178*b2157399SAlexei Starovoitov 	return this_cpu_ptr(array->pptrs[index & array->index_mask]);
179a10423b8SAlexei Starovoitov }
180a10423b8SAlexei Starovoitov 
18115a07b33SAlexei Starovoitov int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
18215a07b33SAlexei Starovoitov {
18315a07b33SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
18415a07b33SAlexei Starovoitov 	u32 index = *(u32 *)key;
18515a07b33SAlexei Starovoitov 	void __percpu *pptr;
18615a07b33SAlexei Starovoitov 	int cpu, off = 0;
18715a07b33SAlexei Starovoitov 	u32 size;
18815a07b33SAlexei Starovoitov 
18915a07b33SAlexei Starovoitov 	if (unlikely(index >= array->map.max_entries))
19015a07b33SAlexei Starovoitov 		return -ENOENT;
19115a07b33SAlexei Starovoitov 
19215a07b33SAlexei Starovoitov 	/* per_cpu areas are zero-filled and bpf programs can only
19315a07b33SAlexei Starovoitov 	 * access 'value_size' of them, so copying rounded areas
19415a07b33SAlexei Starovoitov 	 * will not leak any kernel data
19515a07b33SAlexei Starovoitov 	 */
19615a07b33SAlexei Starovoitov 	size = round_up(map->value_size, 8);
19715a07b33SAlexei Starovoitov 	rcu_read_lock();
198*b2157399SAlexei Starovoitov 	pptr = array->pptrs[index & array->index_mask];
19915a07b33SAlexei Starovoitov 	for_each_possible_cpu(cpu) {
20015a07b33SAlexei Starovoitov 		bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
20115a07b33SAlexei Starovoitov 		off += size;
20215a07b33SAlexei Starovoitov 	}
20315a07b33SAlexei Starovoitov 	rcu_read_unlock();
20415a07b33SAlexei Starovoitov 	return 0;
20515a07b33SAlexei Starovoitov }
20615a07b33SAlexei Starovoitov 
20728fbcfa0SAlexei Starovoitov /* Called from syscall */
20828fbcfa0SAlexei Starovoitov static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
20928fbcfa0SAlexei Starovoitov {
21028fbcfa0SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
2118fe45924STeng Qin 	u32 index = key ? *(u32 *)key : U32_MAX;
21228fbcfa0SAlexei Starovoitov 	u32 *next = (u32 *)next_key;
21328fbcfa0SAlexei Starovoitov 
21428fbcfa0SAlexei Starovoitov 	if (index >= array->map.max_entries) {
21528fbcfa0SAlexei Starovoitov 		*next = 0;
21628fbcfa0SAlexei Starovoitov 		return 0;
21728fbcfa0SAlexei Starovoitov 	}
21828fbcfa0SAlexei Starovoitov 
21928fbcfa0SAlexei Starovoitov 	if (index == array->map.max_entries - 1)
22028fbcfa0SAlexei Starovoitov 		return -ENOENT;
22128fbcfa0SAlexei Starovoitov 
22228fbcfa0SAlexei Starovoitov 	*next = index + 1;
22328fbcfa0SAlexei Starovoitov 	return 0;
22428fbcfa0SAlexei Starovoitov }
22528fbcfa0SAlexei Starovoitov 
22628fbcfa0SAlexei Starovoitov /* Called from syscall or from eBPF program */
22728fbcfa0SAlexei Starovoitov static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
22828fbcfa0SAlexei Starovoitov 				 u64 map_flags)
22928fbcfa0SAlexei Starovoitov {
23028fbcfa0SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
23128fbcfa0SAlexei Starovoitov 	u32 index = *(u32 *)key;
23228fbcfa0SAlexei Starovoitov 
233a10423b8SAlexei Starovoitov 	if (unlikely(map_flags > BPF_EXIST))
23428fbcfa0SAlexei Starovoitov 		/* unknown flags */
23528fbcfa0SAlexei Starovoitov 		return -EINVAL;
23628fbcfa0SAlexei Starovoitov 
237a10423b8SAlexei Starovoitov 	if (unlikely(index >= array->map.max_entries))
23828fbcfa0SAlexei Starovoitov 		/* all elements were pre-allocated, cannot insert a new one */
23928fbcfa0SAlexei Starovoitov 		return -E2BIG;
24028fbcfa0SAlexei Starovoitov 
241a10423b8SAlexei Starovoitov 	if (unlikely(map_flags == BPF_NOEXIST))
242daaf427cSAlexei Starovoitov 		/* all elements already exist */
24328fbcfa0SAlexei Starovoitov 		return -EEXIST;
24428fbcfa0SAlexei Starovoitov 
245a10423b8SAlexei Starovoitov 	if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
246*b2157399SAlexei Starovoitov 		memcpy(this_cpu_ptr(array->pptrs[index & array->index_mask]),
247a10423b8SAlexei Starovoitov 		       value, map->value_size);
248a10423b8SAlexei Starovoitov 	else
249*b2157399SAlexei Starovoitov 		memcpy(array->value +
250*b2157399SAlexei Starovoitov 		       array->elem_size * (index & array->index_mask),
251a10423b8SAlexei Starovoitov 		       value, map->value_size);
25228fbcfa0SAlexei Starovoitov 	return 0;
25328fbcfa0SAlexei Starovoitov }
25428fbcfa0SAlexei Starovoitov 
25515a07b33SAlexei Starovoitov int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
25615a07b33SAlexei Starovoitov 			    u64 map_flags)
25715a07b33SAlexei Starovoitov {
25815a07b33SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
25915a07b33SAlexei Starovoitov 	u32 index = *(u32 *)key;
26015a07b33SAlexei Starovoitov 	void __percpu *pptr;
26115a07b33SAlexei Starovoitov 	int cpu, off = 0;
26215a07b33SAlexei Starovoitov 	u32 size;
26315a07b33SAlexei Starovoitov 
26415a07b33SAlexei Starovoitov 	if (unlikely(map_flags > BPF_EXIST))
26515a07b33SAlexei Starovoitov 		/* unknown flags */
26615a07b33SAlexei Starovoitov 		return -EINVAL;
26715a07b33SAlexei Starovoitov 
26815a07b33SAlexei Starovoitov 	if (unlikely(index >= array->map.max_entries))
26915a07b33SAlexei Starovoitov 		/* all elements were pre-allocated, cannot insert a new one */
27015a07b33SAlexei Starovoitov 		return -E2BIG;
27115a07b33SAlexei Starovoitov 
27215a07b33SAlexei Starovoitov 	if (unlikely(map_flags == BPF_NOEXIST))
27315a07b33SAlexei Starovoitov 		/* all elements already exist */
27415a07b33SAlexei Starovoitov 		return -EEXIST;
27515a07b33SAlexei Starovoitov 
27615a07b33SAlexei Starovoitov 	/* the user space will provide round_up(value_size, 8) bytes that
27715a07b33SAlexei Starovoitov 	 * will be copied into per-cpu area. bpf programs can only access
27815a07b33SAlexei Starovoitov 	 * value_size of it. During lookup the same extra bytes will be
27915a07b33SAlexei Starovoitov 	 * returned or zeros which were zero-filled by percpu_alloc,
28015a07b33SAlexei Starovoitov 	 * so no kernel data leaks possible
28115a07b33SAlexei Starovoitov 	 */
28215a07b33SAlexei Starovoitov 	size = round_up(map->value_size, 8);
28315a07b33SAlexei Starovoitov 	rcu_read_lock();
284*b2157399SAlexei Starovoitov 	pptr = array->pptrs[index & array->index_mask];
28515a07b33SAlexei Starovoitov 	for_each_possible_cpu(cpu) {
28615a07b33SAlexei Starovoitov 		bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
28715a07b33SAlexei Starovoitov 		off += size;
28815a07b33SAlexei Starovoitov 	}
28915a07b33SAlexei Starovoitov 	rcu_read_unlock();
29015a07b33SAlexei Starovoitov 	return 0;
29115a07b33SAlexei Starovoitov }
29215a07b33SAlexei Starovoitov 
29328fbcfa0SAlexei Starovoitov /* Called from syscall or from eBPF program */
29428fbcfa0SAlexei Starovoitov static int array_map_delete_elem(struct bpf_map *map, void *key)
29528fbcfa0SAlexei Starovoitov {
29628fbcfa0SAlexei Starovoitov 	return -EINVAL;
29728fbcfa0SAlexei Starovoitov }
29828fbcfa0SAlexei Starovoitov 
29928fbcfa0SAlexei Starovoitov /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
30028fbcfa0SAlexei Starovoitov static void array_map_free(struct bpf_map *map)
30128fbcfa0SAlexei Starovoitov {
30228fbcfa0SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
30328fbcfa0SAlexei Starovoitov 
30428fbcfa0SAlexei Starovoitov 	/* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
30528fbcfa0SAlexei Starovoitov 	 * so the programs (can be more than one that used this map) were
30628fbcfa0SAlexei Starovoitov 	 * disconnected from events. Wait for outstanding programs to complete
30728fbcfa0SAlexei Starovoitov 	 * and free the array
30828fbcfa0SAlexei Starovoitov 	 */
30928fbcfa0SAlexei Starovoitov 	synchronize_rcu();
31028fbcfa0SAlexei Starovoitov 
311a10423b8SAlexei Starovoitov 	if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
312a10423b8SAlexei Starovoitov 		bpf_array_free_percpu(array);
313a10423b8SAlexei Starovoitov 
314d407bd25SDaniel Borkmann 	bpf_map_area_free(array);
31528fbcfa0SAlexei Starovoitov }
31628fbcfa0SAlexei Starovoitov 
31740077e0cSJohannes Berg const struct bpf_map_ops array_map_ops = {
31828fbcfa0SAlexei Starovoitov 	.map_alloc = array_map_alloc,
31928fbcfa0SAlexei Starovoitov 	.map_free = array_map_free,
32028fbcfa0SAlexei Starovoitov 	.map_get_next_key = array_map_get_next_key,
32128fbcfa0SAlexei Starovoitov 	.map_lookup_elem = array_map_lookup_elem,
32228fbcfa0SAlexei Starovoitov 	.map_update_elem = array_map_update_elem,
32328fbcfa0SAlexei Starovoitov 	.map_delete_elem = array_map_delete_elem,
32481ed18abSAlexei Starovoitov 	.map_gen_lookup = array_map_gen_lookup,
32528fbcfa0SAlexei Starovoitov };
32628fbcfa0SAlexei Starovoitov 
32740077e0cSJohannes Berg const struct bpf_map_ops percpu_array_map_ops = {
328a10423b8SAlexei Starovoitov 	.map_alloc = array_map_alloc,
329a10423b8SAlexei Starovoitov 	.map_free = array_map_free,
330a10423b8SAlexei Starovoitov 	.map_get_next_key = array_map_get_next_key,
331a10423b8SAlexei Starovoitov 	.map_lookup_elem = percpu_array_map_lookup_elem,
332a10423b8SAlexei Starovoitov 	.map_update_elem = array_map_update_elem,
333a10423b8SAlexei Starovoitov 	.map_delete_elem = array_map_delete_elem,
334a10423b8SAlexei Starovoitov };
335a10423b8SAlexei Starovoitov 
3362a36f0b9SWang Nan static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
33704fd61abSAlexei Starovoitov {
3382a36f0b9SWang Nan 	/* only file descriptors can be stored in this type of map */
33904fd61abSAlexei Starovoitov 	if (attr->value_size != sizeof(u32))
34004fd61abSAlexei Starovoitov 		return ERR_PTR(-EINVAL);
34104fd61abSAlexei Starovoitov 	return array_map_alloc(attr);
34204fd61abSAlexei Starovoitov }
34304fd61abSAlexei Starovoitov 
3442a36f0b9SWang Nan static void fd_array_map_free(struct bpf_map *map)
34504fd61abSAlexei Starovoitov {
34604fd61abSAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
34704fd61abSAlexei Starovoitov 	int i;
34804fd61abSAlexei Starovoitov 
34904fd61abSAlexei Starovoitov 	synchronize_rcu();
35004fd61abSAlexei Starovoitov 
35104fd61abSAlexei Starovoitov 	/* make sure it's empty */
35204fd61abSAlexei Starovoitov 	for (i = 0; i < array->map.max_entries; i++)
3532a36f0b9SWang Nan 		BUG_ON(array->ptrs[i] != NULL);
354d407bd25SDaniel Borkmann 
355d407bd25SDaniel Borkmann 	bpf_map_area_free(array);
35604fd61abSAlexei Starovoitov }
35704fd61abSAlexei Starovoitov 
3582a36f0b9SWang Nan static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
35904fd61abSAlexei Starovoitov {
36004fd61abSAlexei Starovoitov 	return NULL;
36104fd61abSAlexei Starovoitov }
36204fd61abSAlexei Starovoitov 
36304fd61abSAlexei Starovoitov /* only called from syscall */
36414dc6f04SMartin KaFai Lau int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
36514dc6f04SMartin KaFai Lau {
36614dc6f04SMartin KaFai Lau 	void **elem, *ptr;
36714dc6f04SMartin KaFai Lau 	int ret =  0;
36814dc6f04SMartin KaFai Lau 
36914dc6f04SMartin KaFai Lau 	if (!map->ops->map_fd_sys_lookup_elem)
37014dc6f04SMartin KaFai Lau 		return -ENOTSUPP;
37114dc6f04SMartin KaFai Lau 
37214dc6f04SMartin KaFai Lau 	rcu_read_lock();
37314dc6f04SMartin KaFai Lau 	elem = array_map_lookup_elem(map, key);
37414dc6f04SMartin KaFai Lau 	if (elem && (ptr = READ_ONCE(*elem)))
37514dc6f04SMartin KaFai Lau 		*value = map->ops->map_fd_sys_lookup_elem(ptr);
37614dc6f04SMartin KaFai Lau 	else
37714dc6f04SMartin KaFai Lau 		ret = -ENOENT;
37814dc6f04SMartin KaFai Lau 	rcu_read_unlock();
37914dc6f04SMartin KaFai Lau 
38014dc6f04SMartin KaFai Lau 	return ret;
38114dc6f04SMartin KaFai Lau }
38214dc6f04SMartin KaFai Lau 
38314dc6f04SMartin KaFai Lau /* only called from syscall */
384d056a788SDaniel Borkmann int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
385d056a788SDaniel Borkmann 				 void *key, void *value, u64 map_flags)
38604fd61abSAlexei Starovoitov {
38704fd61abSAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
3882a36f0b9SWang Nan 	void *new_ptr, *old_ptr;
38904fd61abSAlexei Starovoitov 	u32 index = *(u32 *)key, ufd;
39004fd61abSAlexei Starovoitov 
39104fd61abSAlexei Starovoitov 	if (map_flags != BPF_ANY)
39204fd61abSAlexei Starovoitov 		return -EINVAL;
39304fd61abSAlexei Starovoitov 
39404fd61abSAlexei Starovoitov 	if (index >= array->map.max_entries)
39504fd61abSAlexei Starovoitov 		return -E2BIG;
39604fd61abSAlexei Starovoitov 
39704fd61abSAlexei Starovoitov 	ufd = *(u32 *)value;
398d056a788SDaniel Borkmann 	new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
3992a36f0b9SWang Nan 	if (IS_ERR(new_ptr))
4002a36f0b9SWang Nan 		return PTR_ERR(new_ptr);
40104fd61abSAlexei Starovoitov 
4022a36f0b9SWang Nan 	old_ptr = xchg(array->ptrs + index, new_ptr);
4032a36f0b9SWang Nan 	if (old_ptr)
4042a36f0b9SWang Nan 		map->ops->map_fd_put_ptr(old_ptr);
40504fd61abSAlexei Starovoitov 
40604fd61abSAlexei Starovoitov 	return 0;
40704fd61abSAlexei Starovoitov }
40804fd61abSAlexei Starovoitov 
4092a36f0b9SWang Nan static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
41004fd61abSAlexei Starovoitov {
41104fd61abSAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
4122a36f0b9SWang Nan 	void *old_ptr;
41304fd61abSAlexei Starovoitov 	u32 index = *(u32 *)key;
41404fd61abSAlexei Starovoitov 
41504fd61abSAlexei Starovoitov 	if (index >= array->map.max_entries)
41604fd61abSAlexei Starovoitov 		return -E2BIG;
41704fd61abSAlexei Starovoitov 
4182a36f0b9SWang Nan 	old_ptr = xchg(array->ptrs + index, NULL);
4192a36f0b9SWang Nan 	if (old_ptr) {
4202a36f0b9SWang Nan 		map->ops->map_fd_put_ptr(old_ptr);
42104fd61abSAlexei Starovoitov 		return 0;
42204fd61abSAlexei Starovoitov 	} else {
42304fd61abSAlexei Starovoitov 		return -ENOENT;
42404fd61abSAlexei Starovoitov 	}
42504fd61abSAlexei Starovoitov }
42604fd61abSAlexei Starovoitov 
427d056a788SDaniel Borkmann static void *prog_fd_array_get_ptr(struct bpf_map *map,
428d056a788SDaniel Borkmann 				   struct file *map_file, int fd)
4292a36f0b9SWang Nan {
4302a36f0b9SWang Nan 	struct bpf_array *array = container_of(map, struct bpf_array, map);
4312a36f0b9SWang Nan 	struct bpf_prog *prog = bpf_prog_get(fd);
432d056a788SDaniel Borkmann 
4332a36f0b9SWang Nan 	if (IS_ERR(prog))
4342a36f0b9SWang Nan 		return prog;
4352a36f0b9SWang Nan 
4362a36f0b9SWang Nan 	if (!bpf_prog_array_compatible(array, prog)) {
4372a36f0b9SWang Nan 		bpf_prog_put(prog);
4382a36f0b9SWang Nan 		return ERR_PTR(-EINVAL);
4392a36f0b9SWang Nan 	}
440d056a788SDaniel Borkmann 
4412a36f0b9SWang Nan 	return prog;
4422a36f0b9SWang Nan }
4432a36f0b9SWang Nan 
4442a36f0b9SWang Nan static void prog_fd_array_put_ptr(void *ptr)
4452a36f0b9SWang Nan {
4461aacde3dSDaniel Borkmann 	bpf_prog_put(ptr);
4472a36f0b9SWang Nan }
4482a36f0b9SWang Nan 
44914dc6f04SMartin KaFai Lau static u32 prog_fd_array_sys_lookup_elem(void *ptr)
45014dc6f04SMartin KaFai Lau {
45114dc6f04SMartin KaFai Lau 	return ((struct bpf_prog *)ptr)->aux->id;
45214dc6f04SMartin KaFai Lau }
45314dc6f04SMartin KaFai Lau 
45404fd61abSAlexei Starovoitov /* decrement refcnt of all bpf_progs that are stored in this map */
4552a36f0b9SWang Nan void bpf_fd_array_map_clear(struct bpf_map *map)
45604fd61abSAlexei Starovoitov {
45704fd61abSAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
45804fd61abSAlexei Starovoitov 	int i;
45904fd61abSAlexei Starovoitov 
46004fd61abSAlexei Starovoitov 	for (i = 0; i < array->map.max_entries; i++)
4612a36f0b9SWang Nan 		fd_array_map_delete_elem(map, &i);
46204fd61abSAlexei Starovoitov }
46304fd61abSAlexei Starovoitov 
46440077e0cSJohannes Berg const struct bpf_map_ops prog_array_map_ops = {
4652a36f0b9SWang Nan 	.map_alloc = fd_array_map_alloc,
4662a36f0b9SWang Nan 	.map_free = fd_array_map_free,
46704fd61abSAlexei Starovoitov 	.map_get_next_key = array_map_get_next_key,
4682a36f0b9SWang Nan 	.map_lookup_elem = fd_array_map_lookup_elem,
4692a36f0b9SWang Nan 	.map_delete_elem = fd_array_map_delete_elem,
4702a36f0b9SWang Nan 	.map_fd_get_ptr = prog_fd_array_get_ptr,
4712a36f0b9SWang Nan 	.map_fd_put_ptr = prog_fd_array_put_ptr,
47214dc6f04SMartin KaFai Lau 	.map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem,
47304fd61abSAlexei Starovoitov };
47404fd61abSAlexei Starovoitov 
4753b1efb19SDaniel Borkmann static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
4763b1efb19SDaniel Borkmann 						   struct file *map_file)
477ea317b26SKaixu Xia {
4783b1efb19SDaniel Borkmann 	struct bpf_event_entry *ee;
4793b1efb19SDaniel Borkmann 
480858d68f1SDaniel Borkmann 	ee = kzalloc(sizeof(*ee), GFP_ATOMIC);
4813b1efb19SDaniel Borkmann 	if (ee) {
4823b1efb19SDaniel Borkmann 		ee->event = perf_file->private_data;
4833b1efb19SDaniel Borkmann 		ee->perf_file = perf_file;
4843b1efb19SDaniel Borkmann 		ee->map_file = map_file;
4853b1efb19SDaniel Borkmann 	}
4863b1efb19SDaniel Borkmann 
4873b1efb19SDaniel Borkmann 	return ee;
4883b1efb19SDaniel Borkmann }
4893b1efb19SDaniel Borkmann 
4903b1efb19SDaniel Borkmann static void __bpf_event_entry_free(struct rcu_head *rcu)
4913b1efb19SDaniel Borkmann {
4923b1efb19SDaniel Borkmann 	struct bpf_event_entry *ee;
4933b1efb19SDaniel Borkmann 
4943b1efb19SDaniel Borkmann 	ee = container_of(rcu, struct bpf_event_entry, rcu);
4953b1efb19SDaniel Borkmann 	fput(ee->perf_file);
4963b1efb19SDaniel Borkmann 	kfree(ee);
4973b1efb19SDaniel Borkmann }
4983b1efb19SDaniel Borkmann 
4993b1efb19SDaniel Borkmann static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
5003b1efb19SDaniel Borkmann {
5013b1efb19SDaniel Borkmann 	call_rcu(&ee->rcu, __bpf_event_entry_free);
502ea317b26SKaixu Xia }
503ea317b26SKaixu Xia 
504d056a788SDaniel Borkmann static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
505d056a788SDaniel Borkmann 					 struct file *map_file, int fd)
506ea317b26SKaixu Xia {
5073b1efb19SDaniel Borkmann 	struct bpf_event_entry *ee;
5083b1efb19SDaniel Borkmann 	struct perf_event *event;
5093b1efb19SDaniel Borkmann 	struct file *perf_file;
510f91840a3SAlexei Starovoitov 	u64 value;
511ea317b26SKaixu Xia 
5123b1efb19SDaniel Borkmann 	perf_file = perf_event_get(fd);
5133b1efb19SDaniel Borkmann 	if (IS_ERR(perf_file))
5143b1efb19SDaniel Borkmann 		return perf_file;
515e03e7ee3SAlexei Starovoitov 
516f91840a3SAlexei Starovoitov 	ee = ERR_PTR(-EOPNOTSUPP);
5173b1efb19SDaniel Borkmann 	event = perf_file->private_data;
51897562633SYonghong Song 	if (perf_event_read_local(event, &value, NULL, NULL) == -EOPNOTSUPP)
5193b1efb19SDaniel Borkmann 		goto err_out;
520ea317b26SKaixu Xia 
5213b1efb19SDaniel Borkmann 	ee = bpf_event_entry_gen(perf_file, map_file);
5223b1efb19SDaniel Borkmann 	if (ee)
5233b1efb19SDaniel Borkmann 		return ee;
5243b1efb19SDaniel Borkmann 	ee = ERR_PTR(-ENOMEM);
5253b1efb19SDaniel Borkmann err_out:
5263b1efb19SDaniel Borkmann 	fput(perf_file);
5273b1efb19SDaniel Borkmann 	return ee;
528ea317b26SKaixu Xia }
529ea317b26SKaixu Xia 
530ea317b26SKaixu Xia static void perf_event_fd_array_put_ptr(void *ptr)
531ea317b26SKaixu Xia {
5323b1efb19SDaniel Borkmann 	bpf_event_entry_free_rcu(ptr);
5333b1efb19SDaniel Borkmann }
5343b1efb19SDaniel Borkmann 
5353b1efb19SDaniel Borkmann static void perf_event_fd_array_release(struct bpf_map *map,
5363b1efb19SDaniel Borkmann 					struct file *map_file)
5373b1efb19SDaniel Borkmann {
5383b1efb19SDaniel Borkmann 	struct bpf_array *array = container_of(map, struct bpf_array, map);
5393b1efb19SDaniel Borkmann 	struct bpf_event_entry *ee;
5403b1efb19SDaniel Borkmann 	int i;
5413b1efb19SDaniel Borkmann 
5423b1efb19SDaniel Borkmann 	rcu_read_lock();
5433b1efb19SDaniel Borkmann 	for (i = 0; i < array->map.max_entries; i++) {
5443b1efb19SDaniel Borkmann 		ee = READ_ONCE(array->ptrs[i]);
5453b1efb19SDaniel Borkmann 		if (ee && ee->map_file == map_file)
5463b1efb19SDaniel Borkmann 			fd_array_map_delete_elem(map, &i);
5473b1efb19SDaniel Borkmann 	}
5483b1efb19SDaniel Borkmann 	rcu_read_unlock();
549ea317b26SKaixu Xia }
550ea317b26SKaixu Xia 
55140077e0cSJohannes Berg const struct bpf_map_ops perf_event_array_map_ops = {
552ea317b26SKaixu Xia 	.map_alloc = fd_array_map_alloc,
5533b1efb19SDaniel Borkmann 	.map_free = fd_array_map_free,
554ea317b26SKaixu Xia 	.map_get_next_key = array_map_get_next_key,
555ea317b26SKaixu Xia 	.map_lookup_elem = fd_array_map_lookup_elem,
556ea317b26SKaixu Xia 	.map_delete_elem = fd_array_map_delete_elem,
557ea317b26SKaixu Xia 	.map_fd_get_ptr = perf_event_fd_array_get_ptr,
558ea317b26SKaixu Xia 	.map_fd_put_ptr = perf_event_fd_array_put_ptr,
5593b1efb19SDaniel Borkmann 	.map_release = perf_event_fd_array_release,
560ea317b26SKaixu Xia };
561ea317b26SKaixu Xia 
56260d20f91SSargun Dhillon #ifdef CONFIG_CGROUPS
5634ed8ec52SMartin KaFai Lau static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
5644ed8ec52SMartin KaFai Lau 				     struct file *map_file /* not used */,
5654ed8ec52SMartin KaFai Lau 				     int fd)
5664ed8ec52SMartin KaFai Lau {
5674ed8ec52SMartin KaFai Lau 	return cgroup_get_from_fd(fd);
5684ed8ec52SMartin KaFai Lau }
5694ed8ec52SMartin KaFai Lau 
5704ed8ec52SMartin KaFai Lau static void cgroup_fd_array_put_ptr(void *ptr)
5714ed8ec52SMartin KaFai Lau {
5724ed8ec52SMartin KaFai Lau 	/* cgroup_put free cgrp after a rcu grace period */
5734ed8ec52SMartin KaFai Lau 	cgroup_put(ptr);
5744ed8ec52SMartin KaFai Lau }
5754ed8ec52SMartin KaFai Lau 
5764ed8ec52SMartin KaFai Lau static void cgroup_fd_array_free(struct bpf_map *map)
5774ed8ec52SMartin KaFai Lau {
5784ed8ec52SMartin KaFai Lau 	bpf_fd_array_map_clear(map);
5794ed8ec52SMartin KaFai Lau 	fd_array_map_free(map);
5804ed8ec52SMartin KaFai Lau }
5814ed8ec52SMartin KaFai Lau 
58240077e0cSJohannes Berg const struct bpf_map_ops cgroup_array_map_ops = {
5834ed8ec52SMartin KaFai Lau 	.map_alloc = fd_array_map_alloc,
5844ed8ec52SMartin KaFai Lau 	.map_free = cgroup_fd_array_free,
5854ed8ec52SMartin KaFai Lau 	.map_get_next_key = array_map_get_next_key,
5864ed8ec52SMartin KaFai Lau 	.map_lookup_elem = fd_array_map_lookup_elem,
5874ed8ec52SMartin KaFai Lau 	.map_delete_elem = fd_array_map_delete_elem,
5884ed8ec52SMartin KaFai Lau 	.map_fd_get_ptr = cgroup_fd_array_get_ptr,
5894ed8ec52SMartin KaFai Lau 	.map_fd_put_ptr = cgroup_fd_array_put_ptr,
5904ed8ec52SMartin KaFai Lau };
5914ed8ec52SMartin KaFai Lau #endif
59256f668dfSMartin KaFai Lau 
59356f668dfSMartin KaFai Lau static struct bpf_map *array_of_map_alloc(union bpf_attr *attr)
59456f668dfSMartin KaFai Lau {
59556f668dfSMartin KaFai Lau 	struct bpf_map *map, *inner_map_meta;
59656f668dfSMartin KaFai Lau 
59756f668dfSMartin KaFai Lau 	inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
59856f668dfSMartin KaFai Lau 	if (IS_ERR(inner_map_meta))
59956f668dfSMartin KaFai Lau 		return inner_map_meta;
60056f668dfSMartin KaFai Lau 
60156f668dfSMartin KaFai Lau 	map = fd_array_map_alloc(attr);
60256f668dfSMartin KaFai Lau 	if (IS_ERR(map)) {
60356f668dfSMartin KaFai Lau 		bpf_map_meta_free(inner_map_meta);
60456f668dfSMartin KaFai Lau 		return map;
60556f668dfSMartin KaFai Lau 	}
60656f668dfSMartin KaFai Lau 
60756f668dfSMartin KaFai Lau 	map->inner_map_meta = inner_map_meta;
60856f668dfSMartin KaFai Lau 
60956f668dfSMartin KaFai Lau 	return map;
61056f668dfSMartin KaFai Lau }
61156f668dfSMartin KaFai Lau 
61256f668dfSMartin KaFai Lau static void array_of_map_free(struct bpf_map *map)
61356f668dfSMartin KaFai Lau {
61456f668dfSMartin KaFai Lau 	/* map->inner_map_meta is only accessed by syscall which
61556f668dfSMartin KaFai Lau 	 * is protected by fdget/fdput.
61656f668dfSMartin KaFai Lau 	 */
61756f668dfSMartin KaFai Lau 	bpf_map_meta_free(map->inner_map_meta);
61856f668dfSMartin KaFai Lau 	bpf_fd_array_map_clear(map);
61956f668dfSMartin KaFai Lau 	fd_array_map_free(map);
62056f668dfSMartin KaFai Lau }
62156f668dfSMartin KaFai Lau 
62256f668dfSMartin KaFai Lau static void *array_of_map_lookup_elem(struct bpf_map *map, void *key)
62356f668dfSMartin KaFai Lau {
62456f668dfSMartin KaFai Lau 	struct bpf_map **inner_map = array_map_lookup_elem(map, key);
62556f668dfSMartin KaFai Lau 
62656f668dfSMartin KaFai Lau 	if (!inner_map)
62756f668dfSMartin KaFai Lau 		return NULL;
62856f668dfSMartin KaFai Lau 
62956f668dfSMartin KaFai Lau 	return READ_ONCE(*inner_map);
63056f668dfSMartin KaFai Lau }
63156f668dfSMartin KaFai Lau 
6327b0c2a05SDaniel Borkmann static u32 array_of_map_gen_lookup(struct bpf_map *map,
6337b0c2a05SDaniel Borkmann 				   struct bpf_insn *insn_buf)
6347b0c2a05SDaniel Borkmann {
635*b2157399SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
6367b0c2a05SDaniel Borkmann 	u32 elem_size = round_up(map->value_size, 8);
6377b0c2a05SDaniel Borkmann 	struct bpf_insn *insn = insn_buf;
6387b0c2a05SDaniel Borkmann 	const int ret = BPF_REG_0;
6397b0c2a05SDaniel Borkmann 	const int map_ptr = BPF_REG_1;
6407b0c2a05SDaniel Borkmann 	const int index = BPF_REG_2;
6417b0c2a05SDaniel Borkmann 
6427b0c2a05SDaniel Borkmann 	*insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
6437b0c2a05SDaniel Borkmann 	*insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
644*b2157399SAlexei Starovoitov 	if (map->unpriv_array) {
645*b2157399SAlexei Starovoitov 		*insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 6);
646*b2157399SAlexei Starovoitov 		*insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
647*b2157399SAlexei Starovoitov 	} else {
6487b0c2a05SDaniel Borkmann 		*insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 5);
649*b2157399SAlexei Starovoitov 	}
6507b0c2a05SDaniel Borkmann 	if (is_power_of_2(elem_size))
6517b0c2a05SDaniel Borkmann 		*insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
6527b0c2a05SDaniel Borkmann 	else
6537b0c2a05SDaniel Borkmann 		*insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
6547b0c2a05SDaniel Borkmann 	*insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
6557b0c2a05SDaniel Borkmann 	*insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
6567b0c2a05SDaniel Borkmann 	*insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
6577b0c2a05SDaniel Borkmann 	*insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
6587b0c2a05SDaniel Borkmann 	*insn++ = BPF_MOV64_IMM(ret, 0);
6597b0c2a05SDaniel Borkmann 
6607b0c2a05SDaniel Borkmann 	return insn - insn_buf;
6617b0c2a05SDaniel Borkmann }
6627b0c2a05SDaniel Borkmann 
66340077e0cSJohannes Berg const struct bpf_map_ops array_of_maps_map_ops = {
66456f668dfSMartin KaFai Lau 	.map_alloc = array_of_map_alloc,
66556f668dfSMartin KaFai Lau 	.map_free = array_of_map_free,
66656f668dfSMartin KaFai Lau 	.map_get_next_key = array_map_get_next_key,
66756f668dfSMartin KaFai Lau 	.map_lookup_elem = array_of_map_lookup_elem,
66856f668dfSMartin KaFai Lau 	.map_delete_elem = fd_array_map_delete_elem,
66956f668dfSMartin KaFai Lau 	.map_fd_get_ptr = bpf_map_fd_get_ptr,
67056f668dfSMartin KaFai Lau 	.map_fd_put_ptr = bpf_map_fd_put_ptr,
67114dc6f04SMartin KaFai Lau 	.map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
6727b0c2a05SDaniel Borkmann 	.map_gen_lookup = array_of_map_gen_lookup,
67356f668dfSMartin KaFai Lau };
674