xref: /linux-6.15/kernel/bpf/arraymap.c (revision bbeb6e43)
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);
56b2157399SAlexei Starovoitov 	u32 elem_size, index_mask, max_entries;
57b2157399SAlexei Starovoitov 	bool unpriv = !capable(CAP_SYS_ADMIN);
5828fbcfa0SAlexei Starovoitov 	struct bpf_array *array;
59*bbeb6e43SDaniel Borkmann 	u64 array_size, mask64;
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 
76b2157399SAlexei Starovoitov 	max_entries = attr->max_entries;
77b2157399SAlexei Starovoitov 
78*bbeb6e43SDaniel Borkmann 	/* On 32 bit archs roundup_pow_of_two() with max_entries that has
79*bbeb6e43SDaniel Borkmann 	 * upper most bit set in u32 space is undefined behavior due to
80*bbeb6e43SDaniel Borkmann 	 * resulting 1U << 32, so do it manually here in u64 space.
81*bbeb6e43SDaniel Borkmann 	 */
82*bbeb6e43SDaniel Borkmann 	mask64 = fls_long(max_entries - 1);
83*bbeb6e43SDaniel Borkmann 	mask64 = 1ULL << mask64;
84*bbeb6e43SDaniel Borkmann 	mask64 -= 1;
85*bbeb6e43SDaniel Borkmann 
86*bbeb6e43SDaniel Borkmann 	index_mask = mask64;
87*bbeb6e43SDaniel Borkmann 	if (unpriv) {
88b2157399SAlexei Starovoitov 		/* round up array size to nearest power of 2,
89b2157399SAlexei Starovoitov 		 * since cpu will speculate within index_mask limits
90b2157399SAlexei Starovoitov 		 */
91b2157399SAlexei Starovoitov 		max_entries = index_mask + 1;
92*bbeb6e43SDaniel Borkmann 		/* Check for overflows. */
93*bbeb6e43SDaniel Borkmann 		if (max_entries < attr->max_entries)
94*bbeb6e43SDaniel Borkmann 			return ERR_PTR(-E2BIG);
95*bbeb6e43SDaniel Borkmann 	}
96b2157399SAlexei Starovoitov 
97a10423b8SAlexei Starovoitov 	array_size = sizeof(*array);
98a10423b8SAlexei Starovoitov 	if (percpu)
99b2157399SAlexei Starovoitov 		array_size += (u64) max_entries * sizeof(void *);
100a10423b8SAlexei Starovoitov 	else
101b2157399SAlexei Starovoitov 		array_size += (u64) max_entries * elem_size;
102a10423b8SAlexei Starovoitov 
103a10423b8SAlexei Starovoitov 	/* make sure there is no u32 overflow later in round_up() */
104a10423b8SAlexei Starovoitov 	if (array_size >= U32_MAX - PAGE_SIZE)
105daaf427cSAlexei Starovoitov 		return ERR_PTR(-ENOMEM);
106daaf427cSAlexei Starovoitov 
10728fbcfa0SAlexei Starovoitov 	/* allocate all map elements and zero-initialize them */
10896eabe7aSMartin KaFai Lau 	array = bpf_map_area_alloc(array_size, numa_node);
10928fbcfa0SAlexei Starovoitov 	if (!array)
11028fbcfa0SAlexei Starovoitov 		return ERR_PTR(-ENOMEM);
111b2157399SAlexei Starovoitov 	array->index_mask = index_mask;
112b2157399SAlexei Starovoitov 	array->map.unpriv_array = unpriv;
11328fbcfa0SAlexei Starovoitov 
11428fbcfa0SAlexei Starovoitov 	/* copy mandatory map attributes */
115a10423b8SAlexei Starovoitov 	array->map.map_type = attr->map_type;
11628fbcfa0SAlexei Starovoitov 	array->map.key_size = attr->key_size;
11728fbcfa0SAlexei Starovoitov 	array->map.value_size = attr->value_size;
11828fbcfa0SAlexei Starovoitov 	array->map.max_entries = attr->max_entries;
119a316338cSDaniel Borkmann 	array->map.map_flags = attr->map_flags;
12096eabe7aSMartin KaFai Lau 	array->map.numa_node = numa_node;
12128fbcfa0SAlexei Starovoitov 	array->elem_size = elem_size;
12228fbcfa0SAlexei Starovoitov 
123a10423b8SAlexei Starovoitov 	if (!percpu)
124a10423b8SAlexei Starovoitov 		goto out;
125a10423b8SAlexei Starovoitov 
126a10423b8SAlexei Starovoitov 	array_size += (u64) attr->max_entries * elem_size * num_possible_cpus();
127a10423b8SAlexei Starovoitov 
128a10423b8SAlexei Starovoitov 	if (array_size >= U32_MAX - PAGE_SIZE ||
129bc6d5031SDaniel Borkmann 	    bpf_array_alloc_percpu(array)) {
130d407bd25SDaniel Borkmann 		bpf_map_area_free(array);
131a10423b8SAlexei Starovoitov 		return ERR_PTR(-ENOMEM);
132a10423b8SAlexei Starovoitov 	}
133a10423b8SAlexei Starovoitov out:
134a10423b8SAlexei Starovoitov 	array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
135a10423b8SAlexei Starovoitov 
13628fbcfa0SAlexei Starovoitov 	return &array->map;
13728fbcfa0SAlexei Starovoitov }
13828fbcfa0SAlexei Starovoitov 
13928fbcfa0SAlexei Starovoitov /* Called from syscall or from eBPF program */
14028fbcfa0SAlexei Starovoitov static void *array_map_lookup_elem(struct bpf_map *map, void *key)
14128fbcfa0SAlexei Starovoitov {
14228fbcfa0SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
14328fbcfa0SAlexei Starovoitov 	u32 index = *(u32 *)key;
14428fbcfa0SAlexei Starovoitov 
145a10423b8SAlexei Starovoitov 	if (unlikely(index >= array->map.max_entries))
14628fbcfa0SAlexei Starovoitov 		return NULL;
14728fbcfa0SAlexei Starovoitov 
148b2157399SAlexei Starovoitov 	return array->value + array->elem_size * (index & array->index_mask);
14928fbcfa0SAlexei Starovoitov }
15028fbcfa0SAlexei Starovoitov 
15181ed18abSAlexei Starovoitov /* emit BPF instructions equivalent to C code of array_map_lookup_elem() */
15281ed18abSAlexei Starovoitov static u32 array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
15381ed18abSAlexei Starovoitov {
154b2157399SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
15581ed18abSAlexei Starovoitov 	struct bpf_insn *insn = insn_buf;
156fad73a1aSMartin KaFai Lau 	u32 elem_size = round_up(map->value_size, 8);
15781ed18abSAlexei Starovoitov 	const int ret = BPF_REG_0;
15881ed18abSAlexei Starovoitov 	const int map_ptr = BPF_REG_1;
15981ed18abSAlexei Starovoitov 	const int index = BPF_REG_2;
16081ed18abSAlexei Starovoitov 
16181ed18abSAlexei Starovoitov 	*insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
16281ed18abSAlexei Starovoitov 	*insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
163b2157399SAlexei Starovoitov 	if (map->unpriv_array) {
164b2157399SAlexei Starovoitov 		*insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 4);
165b2157399SAlexei Starovoitov 		*insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
166b2157399SAlexei Starovoitov 	} else {
167fad73a1aSMartin KaFai Lau 		*insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 3);
168b2157399SAlexei Starovoitov 	}
169fad73a1aSMartin KaFai Lau 
170fad73a1aSMartin KaFai Lau 	if (is_power_of_2(elem_size)) {
17181ed18abSAlexei Starovoitov 		*insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
17281ed18abSAlexei Starovoitov 	} else {
17381ed18abSAlexei Starovoitov 		*insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
17481ed18abSAlexei Starovoitov 	}
17581ed18abSAlexei Starovoitov 	*insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
17681ed18abSAlexei Starovoitov 	*insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
17781ed18abSAlexei Starovoitov 	*insn++ = BPF_MOV64_IMM(ret, 0);
17881ed18abSAlexei Starovoitov 	return insn - insn_buf;
17981ed18abSAlexei Starovoitov }
18081ed18abSAlexei Starovoitov 
181a10423b8SAlexei Starovoitov /* Called from eBPF program */
182a10423b8SAlexei Starovoitov static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
183a10423b8SAlexei Starovoitov {
184a10423b8SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
185a10423b8SAlexei Starovoitov 	u32 index = *(u32 *)key;
186a10423b8SAlexei Starovoitov 
187a10423b8SAlexei Starovoitov 	if (unlikely(index >= array->map.max_entries))
188a10423b8SAlexei Starovoitov 		return NULL;
189a10423b8SAlexei Starovoitov 
190b2157399SAlexei Starovoitov 	return this_cpu_ptr(array->pptrs[index & array->index_mask]);
191a10423b8SAlexei Starovoitov }
192a10423b8SAlexei Starovoitov 
19315a07b33SAlexei Starovoitov int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
19415a07b33SAlexei Starovoitov {
19515a07b33SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
19615a07b33SAlexei Starovoitov 	u32 index = *(u32 *)key;
19715a07b33SAlexei Starovoitov 	void __percpu *pptr;
19815a07b33SAlexei Starovoitov 	int cpu, off = 0;
19915a07b33SAlexei Starovoitov 	u32 size;
20015a07b33SAlexei Starovoitov 
20115a07b33SAlexei Starovoitov 	if (unlikely(index >= array->map.max_entries))
20215a07b33SAlexei Starovoitov 		return -ENOENT;
20315a07b33SAlexei Starovoitov 
20415a07b33SAlexei Starovoitov 	/* per_cpu areas are zero-filled and bpf programs can only
20515a07b33SAlexei Starovoitov 	 * access 'value_size' of them, so copying rounded areas
20615a07b33SAlexei Starovoitov 	 * will not leak any kernel data
20715a07b33SAlexei Starovoitov 	 */
20815a07b33SAlexei Starovoitov 	size = round_up(map->value_size, 8);
20915a07b33SAlexei Starovoitov 	rcu_read_lock();
210b2157399SAlexei Starovoitov 	pptr = array->pptrs[index & array->index_mask];
21115a07b33SAlexei Starovoitov 	for_each_possible_cpu(cpu) {
21215a07b33SAlexei Starovoitov 		bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
21315a07b33SAlexei Starovoitov 		off += size;
21415a07b33SAlexei Starovoitov 	}
21515a07b33SAlexei Starovoitov 	rcu_read_unlock();
21615a07b33SAlexei Starovoitov 	return 0;
21715a07b33SAlexei Starovoitov }
21815a07b33SAlexei Starovoitov 
21928fbcfa0SAlexei Starovoitov /* Called from syscall */
22028fbcfa0SAlexei Starovoitov static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
22128fbcfa0SAlexei Starovoitov {
22228fbcfa0SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
2238fe45924STeng Qin 	u32 index = key ? *(u32 *)key : U32_MAX;
22428fbcfa0SAlexei Starovoitov 	u32 *next = (u32 *)next_key;
22528fbcfa0SAlexei Starovoitov 
22628fbcfa0SAlexei Starovoitov 	if (index >= array->map.max_entries) {
22728fbcfa0SAlexei Starovoitov 		*next = 0;
22828fbcfa0SAlexei Starovoitov 		return 0;
22928fbcfa0SAlexei Starovoitov 	}
23028fbcfa0SAlexei Starovoitov 
23128fbcfa0SAlexei Starovoitov 	if (index == array->map.max_entries - 1)
23228fbcfa0SAlexei Starovoitov 		return -ENOENT;
23328fbcfa0SAlexei Starovoitov 
23428fbcfa0SAlexei Starovoitov 	*next = index + 1;
23528fbcfa0SAlexei Starovoitov 	return 0;
23628fbcfa0SAlexei Starovoitov }
23728fbcfa0SAlexei Starovoitov 
23828fbcfa0SAlexei Starovoitov /* Called from syscall or from eBPF program */
23928fbcfa0SAlexei Starovoitov static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
24028fbcfa0SAlexei Starovoitov 				 u64 map_flags)
24128fbcfa0SAlexei Starovoitov {
24228fbcfa0SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
24328fbcfa0SAlexei Starovoitov 	u32 index = *(u32 *)key;
24428fbcfa0SAlexei Starovoitov 
245a10423b8SAlexei Starovoitov 	if (unlikely(map_flags > BPF_EXIST))
24628fbcfa0SAlexei Starovoitov 		/* unknown flags */
24728fbcfa0SAlexei Starovoitov 		return -EINVAL;
24828fbcfa0SAlexei Starovoitov 
249a10423b8SAlexei Starovoitov 	if (unlikely(index >= array->map.max_entries))
25028fbcfa0SAlexei Starovoitov 		/* all elements were pre-allocated, cannot insert a new one */
25128fbcfa0SAlexei Starovoitov 		return -E2BIG;
25228fbcfa0SAlexei Starovoitov 
253a10423b8SAlexei Starovoitov 	if (unlikely(map_flags == BPF_NOEXIST))
254daaf427cSAlexei Starovoitov 		/* all elements already exist */
25528fbcfa0SAlexei Starovoitov 		return -EEXIST;
25628fbcfa0SAlexei Starovoitov 
257a10423b8SAlexei Starovoitov 	if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
258b2157399SAlexei Starovoitov 		memcpy(this_cpu_ptr(array->pptrs[index & array->index_mask]),
259a10423b8SAlexei Starovoitov 		       value, map->value_size);
260a10423b8SAlexei Starovoitov 	else
261b2157399SAlexei Starovoitov 		memcpy(array->value +
262b2157399SAlexei Starovoitov 		       array->elem_size * (index & array->index_mask),
263a10423b8SAlexei Starovoitov 		       value, map->value_size);
26428fbcfa0SAlexei Starovoitov 	return 0;
26528fbcfa0SAlexei Starovoitov }
26628fbcfa0SAlexei Starovoitov 
26715a07b33SAlexei Starovoitov int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
26815a07b33SAlexei Starovoitov 			    u64 map_flags)
26915a07b33SAlexei Starovoitov {
27015a07b33SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
27115a07b33SAlexei Starovoitov 	u32 index = *(u32 *)key;
27215a07b33SAlexei Starovoitov 	void __percpu *pptr;
27315a07b33SAlexei Starovoitov 	int cpu, off = 0;
27415a07b33SAlexei Starovoitov 	u32 size;
27515a07b33SAlexei Starovoitov 
27615a07b33SAlexei Starovoitov 	if (unlikely(map_flags > BPF_EXIST))
27715a07b33SAlexei Starovoitov 		/* unknown flags */
27815a07b33SAlexei Starovoitov 		return -EINVAL;
27915a07b33SAlexei Starovoitov 
28015a07b33SAlexei Starovoitov 	if (unlikely(index >= array->map.max_entries))
28115a07b33SAlexei Starovoitov 		/* all elements were pre-allocated, cannot insert a new one */
28215a07b33SAlexei Starovoitov 		return -E2BIG;
28315a07b33SAlexei Starovoitov 
28415a07b33SAlexei Starovoitov 	if (unlikely(map_flags == BPF_NOEXIST))
28515a07b33SAlexei Starovoitov 		/* all elements already exist */
28615a07b33SAlexei Starovoitov 		return -EEXIST;
28715a07b33SAlexei Starovoitov 
28815a07b33SAlexei Starovoitov 	/* the user space will provide round_up(value_size, 8) bytes that
28915a07b33SAlexei Starovoitov 	 * will be copied into per-cpu area. bpf programs can only access
29015a07b33SAlexei Starovoitov 	 * value_size of it. During lookup the same extra bytes will be
29115a07b33SAlexei Starovoitov 	 * returned or zeros which were zero-filled by percpu_alloc,
29215a07b33SAlexei Starovoitov 	 * so no kernel data leaks possible
29315a07b33SAlexei Starovoitov 	 */
29415a07b33SAlexei Starovoitov 	size = round_up(map->value_size, 8);
29515a07b33SAlexei Starovoitov 	rcu_read_lock();
296b2157399SAlexei Starovoitov 	pptr = array->pptrs[index & array->index_mask];
29715a07b33SAlexei Starovoitov 	for_each_possible_cpu(cpu) {
29815a07b33SAlexei Starovoitov 		bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
29915a07b33SAlexei Starovoitov 		off += size;
30015a07b33SAlexei Starovoitov 	}
30115a07b33SAlexei Starovoitov 	rcu_read_unlock();
30215a07b33SAlexei Starovoitov 	return 0;
30315a07b33SAlexei Starovoitov }
30415a07b33SAlexei Starovoitov 
30528fbcfa0SAlexei Starovoitov /* Called from syscall or from eBPF program */
30628fbcfa0SAlexei Starovoitov static int array_map_delete_elem(struct bpf_map *map, void *key)
30728fbcfa0SAlexei Starovoitov {
30828fbcfa0SAlexei Starovoitov 	return -EINVAL;
30928fbcfa0SAlexei Starovoitov }
31028fbcfa0SAlexei Starovoitov 
31128fbcfa0SAlexei Starovoitov /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
31228fbcfa0SAlexei Starovoitov static void array_map_free(struct bpf_map *map)
31328fbcfa0SAlexei Starovoitov {
31428fbcfa0SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
31528fbcfa0SAlexei Starovoitov 
31628fbcfa0SAlexei Starovoitov 	/* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
31728fbcfa0SAlexei Starovoitov 	 * so the programs (can be more than one that used this map) were
31828fbcfa0SAlexei Starovoitov 	 * disconnected from events. Wait for outstanding programs to complete
31928fbcfa0SAlexei Starovoitov 	 * and free the array
32028fbcfa0SAlexei Starovoitov 	 */
32128fbcfa0SAlexei Starovoitov 	synchronize_rcu();
32228fbcfa0SAlexei Starovoitov 
323a10423b8SAlexei Starovoitov 	if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
324a10423b8SAlexei Starovoitov 		bpf_array_free_percpu(array);
325a10423b8SAlexei Starovoitov 
326d407bd25SDaniel Borkmann 	bpf_map_area_free(array);
32728fbcfa0SAlexei Starovoitov }
32828fbcfa0SAlexei Starovoitov 
32940077e0cSJohannes Berg const struct bpf_map_ops array_map_ops = {
33028fbcfa0SAlexei Starovoitov 	.map_alloc = array_map_alloc,
33128fbcfa0SAlexei Starovoitov 	.map_free = array_map_free,
33228fbcfa0SAlexei Starovoitov 	.map_get_next_key = array_map_get_next_key,
33328fbcfa0SAlexei Starovoitov 	.map_lookup_elem = array_map_lookup_elem,
33428fbcfa0SAlexei Starovoitov 	.map_update_elem = array_map_update_elem,
33528fbcfa0SAlexei Starovoitov 	.map_delete_elem = array_map_delete_elem,
33681ed18abSAlexei Starovoitov 	.map_gen_lookup = array_map_gen_lookup,
33728fbcfa0SAlexei Starovoitov };
33828fbcfa0SAlexei Starovoitov 
33940077e0cSJohannes Berg const struct bpf_map_ops percpu_array_map_ops = {
340a10423b8SAlexei Starovoitov 	.map_alloc = array_map_alloc,
341a10423b8SAlexei Starovoitov 	.map_free = array_map_free,
342a10423b8SAlexei Starovoitov 	.map_get_next_key = array_map_get_next_key,
343a10423b8SAlexei Starovoitov 	.map_lookup_elem = percpu_array_map_lookup_elem,
344a10423b8SAlexei Starovoitov 	.map_update_elem = array_map_update_elem,
345a10423b8SAlexei Starovoitov 	.map_delete_elem = array_map_delete_elem,
346a10423b8SAlexei Starovoitov };
347a10423b8SAlexei Starovoitov 
3482a36f0b9SWang Nan static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
34904fd61abSAlexei Starovoitov {
3502a36f0b9SWang Nan 	/* only file descriptors can be stored in this type of map */
35104fd61abSAlexei Starovoitov 	if (attr->value_size != sizeof(u32))
35204fd61abSAlexei Starovoitov 		return ERR_PTR(-EINVAL);
35304fd61abSAlexei Starovoitov 	return array_map_alloc(attr);
35404fd61abSAlexei Starovoitov }
35504fd61abSAlexei Starovoitov 
3562a36f0b9SWang Nan static void fd_array_map_free(struct bpf_map *map)
35704fd61abSAlexei Starovoitov {
35804fd61abSAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
35904fd61abSAlexei Starovoitov 	int i;
36004fd61abSAlexei Starovoitov 
36104fd61abSAlexei Starovoitov 	synchronize_rcu();
36204fd61abSAlexei Starovoitov 
36304fd61abSAlexei Starovoitov 	/* make sure it's empty */
36404fd61abSAlexei Starovoitov 	for (i = 0; i < array->map.max_entries; i++)
3652a36f0b9SWang Nan 		BUG_ON(array->ptrs[i] != NULL);
366d407bd25SDaniel Borkmann 
367d407bd25SDaniel Borkmann 	bpf_map_area_free(array);
36804fd61abSAlexei Starovoitov }
36904fd61abSAlexei Starovoitov 
3702a36f0b9SWang Nan static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
37104fd61abSAlexei Starovoitov {
37204fd61abSAlexei Starovoitov 	return NULL;
37304fd61abSAlexei Starovoitov }
37404fd61abSAlexei Starovoitov 
37504fd61abSAlexei Starovoitov /* only called from syscall */
37614dc6f04SMartin KaFai Lau int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
37714dc6f04SMartin KaFai Lau {
37814dc6f04SMartin KaFai Lau 	void **elem, *ptr;
37914dc6f04SMartin KaFai Lau 	int ret =  0;
38014dc6f04SMartin KaFai Lau 
38114dc6f04SMartin KaFai Lau 	if (!map->ops->map_fd_sys_lookup_elem)
38214dc6f04SMartin KaFai Lau 		return -ENOTSUPP;
38314dc6f04SMartin KaFai Lau 
38414dc6f04SMartin KaFai Lau 	rcu_read_lock();
38514dc6f04SMartin KaFai Lau 	elem = array_map_lookup_elem(map, key);
38614dc6f04SMartin KaFai Lau 	if (elem && (ptr = READ_ONCE(*elem)))
38714dc6f04SMartin KaFai Lau 		*value = map->ops->map_fd_sys_lookup_elem(ptr);
38814dc6f04SMartin KaFai Lau 	else
38914dc6f04SMartin KaFai Lau 		ret = -ENOENT;
39014dc6f04SMartin KaFai Lau 	rcu_read_unlock();
39114dc6f04SMartin KaFai Lau 
39214dc6f04SMartin KaFai Lau 	return ret;
39314dc6f04SMartin KaFai Lau }
39414dc6f04SMartin KaFai Lau 
39514dc6f04SMartin KaFai Lau /* only called from syscall */
396d056a788SDaniel Borkmann int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
397d056a788SDaniel Borkmann 				 void *key, void *value, u64 map_flags)
39804fd61abSAlexei Starovoitov {
39904fd61abSAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
4002a36f0b9SWang Nan 	void *new_ptr, *old_ptr;
40104fd61abSAlexei Starovoitov 	u32 index = *(u32 *)key, ufd;
40204fd61abSAlexei Starovoitov 
40304fd61abSAlexei Starovoitov 	if (map_flags != BPF_ANY)
40404fd61abSAlexei Starovoitov 		return -EINVAL;
40504fd61abSAlexei Starovoitov 
40604fd61abSAlexei Starovoitov 	if (index >= array->map.max_entries)
40704fd61abSAlexei Starovoitov 		return -E2BIG;
40804fd61abSAlexei Starovoitov 
40904fd61abSAlexei Starovoitov 	ufd = *(u32 *)value;
410d056a788SDaniel Borkmann 	new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
4112a36f0b9SWang Nan 	if (IS_ERR(new_ptr))
4122a36f0b9SWang Nan 		return PTR_ERR(new_ptr);
41304fd61abSAlexei Starovoitov 
4142a36f0b9SWang Nan 	old_ptr = xchg(array->ptrs + index, new_ptr);
4152a36f0b9SWang Nan 	if (old_ptr)
4162a36f0b9SWang Nan 		map->ops->map_fd_put_ptr(old_ptr);
41704fd61abSAlexei Starovoitov 
41804fd61abSAlexei Starovoitov 	return 0;
41904fd61abSAlexei Starovoitov }
42004fd61abSAlexei Starovoitov 
4212a36f0b9SWang Nan static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
42204fd61abSAlexei Starovoitov {
42304fd61abSAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
4242a36f0b9SWang Nan 	void *old_ptr;
42504fd61abSAlexei Starovoitov 	u32 index = *(u32 *)key;
42604fd61abSAlexei Starovoitov 
42704fd61abSAlexei Starovoitov 	if (index >= array->map.max_entries)
42804fd61abSAlexei Starovoitov 		return -E2BIG;
42904fd61abSAlexei Starovoitov 
4302a36f0b9SWang Nan 	old_ptr = xchg(array->ptrs + index, NULL);
4312a36f0b9SWang Nan 	if (old_ptr) {
4322a36f0b9SWang Nan 		map->ops->map_fd_put_ptr(old_ptr);
43304fd61abSAlexei Starovoitov 		return 0;
43404fd61abSAlexei Starovoitov 	} else {
43504fd61abSAlexei Starovoitov 		return -ENOENT;
43604fd61abSAlexei Starovoitov 	}
43704fd61abSAlexei Starovoitov }
43804fd61abSAlexei Starovoitov 
439d056a788SDaniel Borkmann static void *prog_fd_array_get_ptr(struct bpf_map *map,
440d056a788SDaniel Borkmann 				   struct file *map_file, int fd)
4412a36f0b9SWang Nan {
4422a36f0b9SWang Nan 	struct bpf_array *array = container_of(map, struct bpf_array, map);
4432a36f0b9SWang Nan 	struct bpf_prog *prog = bpf_prog_get(fd);
444d056a788SDaniel Borkmann 
4452a36f0b9SWang Nan 	if (IS_ERR(prog))
4462a36f0b9SWang Nan 		return prog;
4472a36f0b9SWang Nan 
4482a36f0b9SWang Nan 	if (!bpf_prog_array_compatible(array, prog)) {
4492a36f0b9SWang Nan 		bpf_prog_put(prog);
4502a36f0b9SWang Nan 		return ERR_PTR(-EINVAL);
4512a36f0b9SWang Nan 	}
452d056a788SDaniel Borkmann 
4532a36f0b9SWang Nan 	return prog;
4542a36f0b9SWang Nan }
4552a36f0b9SWang Nan 
4562a36f0b9SWang Nan static void prog_fd_array_put_ptr(void *ptr)
4572a36f0b9SWang Nan {
4581aacde3dSDaniel Borkmann 	bpf_prog_put(ptr);
4592a36f0b9SWang Nan }
4602a36f0b9SWang Nan 
46114dc6f04SMartin KaFai Lau static u32 prog_fd_array_sys_lookup_elem(void *ptr)
46214dc6f04SMartin KaFai Lau {
46314dc6f04SMartin KaFai Lau 	return ((struct bpf_prog *)ptr)->aux->id;
46414dc6f04SMartin KaFai Lau }
46514dc6f04SMartin KaFai Lau 
46604fd61abSAlexei Starovoitov /* decrement refcnt of all bpf_progs that are stored in this map */
4672a36f0b9SWang Nan void bpf_fd_array_map_clear(struct bpf_map *map)
46804fd61abSAlexei Starovoitov {
46904fd61abSAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
47004fd61abSAlexei Starovoitov 	int i;
47104fd61abSAlexei Starovoitov 
47204fd61abSAlexei Starovoitov 	for (i = 0; i < array->map.max_entries; i++)
4732a36f0b9SWang Nan 		fd_array_map_delete_elem(map, &i);
47404fd61abSAlexei Starovoitov }
47504fd61abSAlexei Starovoitov 
47640077e0cSJohannes Berg const struct bpf_map_ops prog_array_map_ops = {
4772a36f0b9SWang Nan 	.map_alloc = fd_array_map_alloc,
4782a36f0b9SWang Nan 	.map_free = fd_array_map_free,
47904fd61abSAlexei Starovoitov 	.map_get_next_key = array_map_get_next_key,
4802a36f0b9SWang Nan 	.map_lookup_elem = fd_array_map_lookup_elem,
4812a36f0b9SWang Nan 	.map_delete_elem = fd_array_map_delete_elem,
4822a36f0b9SWang Nan 	.map_fd_get_ptr = prog_fd_array_get_ptr,
4832a36f0b9SWang Nan 	.map_fd_put_ptr = prog_fd_array_put_ptr,
48414dc6f04SMartin KaFai Lau 	.map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem,
48504fd61abSAlexei Starovoitov };
48604fd61abSAlexei Starovoitov 
4873b1efb19SDaniel Borkmann static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
4883b1efb19SDaniel Borkmann 						   struct file *map_file)
489ea317b26SKaixu Xia {
4903b1efb19SDaniel Borkmann 	struct bpf_event_entry *ee;
4913b1efb19SDaniel Borkmann 
492858d68f1SDaniel Borkmann 	ee = kzalloc(sizeof(*ee), GFP_ATOMIC);
4933b1efb19SDaniel Borkmann 	if (ee) {
4943b1efb19SDaniel Borkmann 		ee->event = perf_file->private_data;
4953b1efb19SDaniel Borkmann 		ee->perf_file = perf_file;
4963b1efb19SDaniel Borkmann 		ee->map_file = map_file;
4973b1efb19SDaniel Borkmann 	}
4983b1efb19SDaniel Borkmann 
4993b1efb19SDaniel Borkmann 	return ee;
5003b1efb19SDaniel Borkmann }
5013b1efb19SDaniel Borkmann 
5023b1efb19SDaniel Borkmann static void __bpf_event_entry_free(struct rcu_head *rcu)
5033b1efb19SDaniel Borkmann {
5043b1efb19SDaniel Borkmann 	struct bpf_event_entry *ee;
5053b1efb19SDaniel Borkmann 
5063b1efb19SDaniel Borkmann 	ee = container_of(rcu, struct bpf_event_entry, rcu);
5073b1efb19SDaniel Borkmann 	fput(ee->perf_file);
5083b1efb19SDaniel Borkmann 	kfree(ee);
5093b1efb19SDaniel Borkmann }
5103b1efb19SDaniel Borkmann 
5113b1efb19SDaniel Borkmann static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
5123b1efb19SDaniel Borkmann {
5133b1efb19SDaniel Borkmann 	call_rcu(&ee->rcu, __bpf_event_entry_free);
514ea317b26SKaixu Xia }
515ea317b26SKaixu Xia 
516d056a788SDaniel Borkmann static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
517d056a788SDaniel Borkmann 					 struct file *map_file, int fd)
518ea317b26SKaixu Xia {
5193b1efb19SDaniel Borkmann 	struct bpf_event_entry *ee;
5203b1efb19SDaniel Borkmann 	struct perf_event *event;
5213b1efb19SDaniel Borkmann 	struct file *perf_file;
522f91840a3SAlexei Starovoitov 	u64 value;
523ea317b26SKaixu Xia 
5243b1efb19SDaniel Borkmann 	perf_file = perf_event_get(fd);
5253b1efb19SDaniel Borkmann 	if (IS_ERR(perf_file))
5263b1efb19SDaniel Borkmann 		return perf_file;
527e03e7ee3SAlexei Starovoitov 
528f91840a3SAlexei Starovoitov 	ee = ERR_PTR(-EOPNOTSUPP);
5293b1efb19SDaniel Borkmann 	event = perf_file->private_data;
53097562633SYonghong Song 	if (perf_event_read_local(event, &value, NULL, NULL) == -EOPNOTSUPP)
5313b1efb19SDaniel Borkmann 		goto err_out;
532ea317b26SKaixu Xia 
5333b1efb19SDaniel Borkmann 	ee = bpf_event_entry_gen(perf_file, map_file);
5343b1efb19SDaniel Borkmann 	if (ee)
5353b1efb19SDaniel Borkmann 		return ee;
5363b1efb19SDaniel Borkmann 	ee = ERR_PTR(-ENOMEM);
5373b1efb19SDaniel Borkmann err_out:
5383b1efb19SDaniel Borkmann 	fput(perf_file);
5393b1efb19SDaniel Borkmann 	return ee;
540ea317b26SKaixu Xia }
541ea317b26SKaixu Xia 
542ea317b26SKaixu Xia static void perf_event_fd_array_put_ptr(void *ptr)
543ea317b26SKaixu Xia {
5443b1efb19SDaniel Borkmann 	bpf_event_entry_free_rcu(ptr);
5453b1efb19SDaniel Borkmann }
5463b1efb19SDaniel Borkmann 
5473b1efb19SDaniel Borkmann static void perf_event_fd_array_release(struct bpf_map *map,
5483b1efb19SDaniel Borkmann 					struct file *map_file)
5493b1efb19SDaniel Borkmann {
5503b1efb19SDaniel Borkmann 	struct bpf_array *array = container_of(map, struct bpf_array, map);
5513b1efb19SDaniel Borkmann 	struct bpf_event_entry *ee;
5523b1efb19SDaniel Borkmann 	int i;
5533b1efb19SDaniel Borkmann 
5543b1efb19SDaniel Borkmann 	rcu_read_lock();
5553b1efb19SDaniel Borkmann 	for (i = 0; i < array->map.max_entries; i++) {
5563b1efb19SDaniel Borkmann 		ee = READ_ONCE(array->ptrs[i]);
5573b1efb19SDaniel Borkmann 		if (ee && ee->map_file == map_file)
5583b1efb19SDaniel Borkmann 			fd_array_map_delete_elem(map, &i);
5593b1efb19SDaniel Borkmann 	}
5603b1efb19SDaniel Borkmann 	rcu_read_unlock();
561ea317b26SKaixu Xia }
562ea317b26SKaixu Xia 
56340077e0cSJohannes Berg const struct bpf_map_ops perf_event_array_map_ops = {
564ea317b26SKaixu Xia 	.map_alloc = fd_array_map_alloc,
5653b1efb19SDaniel Borkmann 	.map_free = fd_array_map_free,
566ea317b26SKaixu Xia 	.map_get_next_key = array_map_get_next_key,
567ea317b26SKaixu Xia 	.map_lookup_elem = fd_array_map_lookup_elem,
568ea317b26SKaixu Xia 	.map_delete_elem = fd_array_map_delete_elem,
569ea317b26SKaixu Xia 	.map_fd_get_ptr = perf_event_fd_array_get_ptr,
570ea317b26SKaixu Xia 	.map_fd_put_ptr = perf_event_fd_array_put_ptr,
5713b1efb19SDaniel Borkmann 	.map_release = perf_event_fd_array_release,
572ea317b26SKaixu Xia };
573ea317b26SKaixu Xia 
57460d20f91SSargun Dhillon #ifdef CONFIG_CGROUPS
5754ed8ec52SMartin KaFai Lau static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
5764ed8ec52SMartin KaFai Lau 				     struct file *map_file /* not used */,
5774ed8ec52SMartin KaFai Lau 				     int fd)
5784ed8ec52SMartin KaFai Lau {
5794ed8ec52SMartin KaFai Lau 	return cgroup_get_from_fd(fd);
5804ed8ec52SMartin KaFai Lau }
5814ed8ec52SMartin KaFai Lau 
5824ed8ec52SMartin KaFai Lau static void cgroup_fd_array_put_ptr(void *ptr)
5834ed8ec52SMartin KaFai Lau {
5844ed8ec52SMartin KaFai Lau 	/* cgroup_put free cgrp after a rcu grace period */
5854ed8ec52SMartin KaFai Lau 	cgroup_put(ptr);
5864ed8ec52SMartin KaFai Lau }
5874ed8ec52SMartin KaFai Lau 
5884ed8ec52SMartin KaFai Lau static void cgroup_fd_array_free(struct bpf_map *map)
5894ed8ec52SMartin KaFai Lau {
5904ed8ec52SMartin KaFai Lau 	bpf_fd_array_map_clear(map);
5914ed8ec52SMartin KaFai Lau 	fd_array_map_free(map);
5924ed8ec52SMartin KaFai Lau }
5934ed8ec52SMartin KaFai Lau 
59440077e0cSJohannes Berg const struct bpf_map_ops cgroup_array_map_ops = {
5954ed8ec52SMartin KaFai Lau 	.map_alloc = fd_array_map_alloc,
5964ed8ec52SMartin KaFai Lau 	.map_free = cgroup_fd_array_free,
5974ed8ec52SMartin KaFai Lau 	.map_get_next_key = array_map_get_next_key,
5984ed8ec52SMartin KaFai Lau 	.map_lookup_elem = fd_array_map_lookup_elem,
5994ed8ec52SMartin KaFai Lau 	.map_delete_elem = fd_array_map_delete_elem,
6004ed8ec52SMartin KaFai Lau 	.map_fd_get_ptr = cgroup_fd_array_get_ptr,
6014ed8ec52SMartin KaFai Lau 	.map_fd_put_ptr = cgroup_fd_array_put_ptr,
6024ed8ec52SMartin KaFai Lau };
6034ed8ec52SMartin KaFai Lau #endif
60456f668dfSMartin KaFai Lau 
60556f668dfSMartin KaFai Lau static struct bpf_map *array_of_map_alloc(union bpf_attr *attr)
60656f668dfSMartin KaFai Lau {
60756f668dfSMartin KaFai Lau 	struct bpf_map *map, *inner_map_meta;
60856f668dfSMartin KaFai Lau 
60956f668dfSMartin KaFai Lau 	inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
61056f668dfSMartin KaFai Lau 	if (IS_ERR(inner_map_meta))
61156f668dfSMartin KaFai Lau 		return inner_map_meta;
61256f668dfSMartin KaFai Lau 
61356f668dfSMartin KaFai Lau 	map = fd_array_map_alloc(attr);
61456f668dfSMartin KaFai Lau 	if (IS_ERR(map)) {
61556f668dfSMartin KaFai Lau 		bpf_map_meta_free(inner_map_meta);
61656f668dfSMartin KaFai Lau 		return map;
61756f668dfSMartin KaFai Lau 	}
61856f668dfSMartin KaFai Lau 
61956f668dfSMartin KaFai Lau 	map->inner_map_meta = inner_map_meta;
62056f668dfSMartin KaFai Lau 
62156f668dfSMartin KaFai Lau 	return map;
62256f668dfSMartin KaFai Lau }
62356f668dfSMartin KaFai Lau 
62456f668dfSMartin KaFai Lau static void array_of_map_free(struct bpf_map *map)
62556f668dfSMartin KaFai Lau {
62656f668dfSMartin KaFai Lau 	/* map->inner_map_meta is only accessed by syscall which
62756f668dfSMartin KaFai Lau 	 * is protected by fdget/fdput.
62856f668dfSMartin KaFai Lau 	 */
62956f668dfSMartin KaFai Lau 	bpf_map_meta_free(map->inner_map_meta);
63056f668dfSMartin KaFai Lau 	bpf_fd_array_map_clear(map);
63156f668dfSMartin KaFai Lau 	fd_array_map_free(map);
63256f668dfSMartin KaFai Lau }
63356f668dfSMartin KaFai Lau 
63456f668dfSMartin KaFai Lau static void *array_of_map_lookup_elem(struct bpf_map *map, void *key)
63556f668dfSMartin KaFai Lau {
63656f668dfSMartin KaFai Lau 	struct bpf_map **inner_map = array_map_lookup_elem(map, key);
63756f668dfSMartin KaFai Lau 
63856f668dfSMartin KaFai Lau 	if (!inner_map)
63956f668dfSMartin KaFai Lau 		return NULL;
64056f668dfSMartin KaFai Lau 
64156f668dfSMartin KaFai Lau 	return READ_ONCE(*inner_map);
64256f668dfSMartin KaFai Lau }
64356f668dfSMartin KaFai Lau 
6447b0c2a05SDaniel Borkmann static u32 array_of_map_gen_lookup(struct bpf_map *map,
6457b0c2a05SDaniel Borkmann 				   struct bpf_insn *insn_buf)
6467b0c2a05SDaniel Borkmann {
647b2157399SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
6487b0c2a05SDaniel Borkmann 	u32 elem_size = round_up(map->value_size, 8);
6497b0c2a05SDaniel Borkmann 	struct bpf_insn *insn = insn_buf;
6507b0c2a05SDaniel Borkmann 	const int ret = BPF_REG_0;
6517b0c2a05SDaniel Borkmann 	const int map_ptr = BPF_REG_1;
6527b0c2a05SDaniel Borkmann 	const int index = BPF_REG_2;
6537b0c2a05SDaniel Borkmann 
6547b0c2a05SDaniel Borkmann 	*insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
6557b0c2a05SDaniel Borkmann 	*insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
656b2157399SAlexei Starovoitov 	if (map->unpriv_array) {
657b2157399SAlexei Starovoitov 		*insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 6);
658b2157399SAlexei Starovoitov 		*insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
659b2157399SAlexei Starovoitov 	} else {
6607b0c2a05SDaniel Borkmann 		*insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 5);
661b2157399SAlexei Starovoitov 	}
6627b0c2a05SDaniel Borkmann 	if (is_power_of_2(elem_size))
6637b0c2a05SDaniel Borkmann 		*insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
6647b0c2a05SDaniel Borkmann 	else
6657b0c2a05SDaniel Borkmann 		*insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
6667b0c2a05SDaniel Borkmann 	*insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
6677b0c2a05SDaniel Borkmann 	*insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
6687b0c2a05SDaniel Borkmann 	*insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
6697b0c2a05SDaniel Borkmann 	*insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
6707b0c2a05SDaniel Borkmann 	*insn++ = BPF_MOV64_IMM(ret, 0);
6717b0c2a05SDaniel Borkmann 
6727b0c2a05SDaniel Borkmann 	return insn - insn_buf;
6737b0c2a05SDaniel Borkmann }
6747b0c2a05SDaniel Borkmann 
67540077e0cSJohannes Berg const struct bpf_map_ops array_of_maps_map_ops = {
67656f668dfSMartin KaFai Lau 	.map_alloc = array_of_map_alloc,
67756f668dfSMartin KaFai Lau 	.map_free = array_of_map_free,
67856f668dfSMartin KaFai Lau 	.map_get_next_key = array_map_get_next_key,
67956f668dfSMartin KaFai Lau 	.map_lookup_elem = array_of_map_lookup_elem,
68056f668dfSMartin KaFai Lau 	.map_delete_elem = fd_array_map_delete_elem,
68156f668dfSMartin KaFai Lau 	.map_fd_get_ptr = bpf_map_fd_get_ptr,
68256f668dfSMartin KaFai Lau 	.map_fd_put_ptr = bpf_map_fd_put_ptr,
68314dc6f04SMartin KaFai Lau 	.map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
6847b0c2a05SDaniel Borkmann 	.map_gen_lookup = array_of_map_gen_lookup,
68556f668dfSMartin KaFai Lau };
686