xref: /linux/kernel/bpf/arraymap.c (revision a3a81d247651218e47153f2d2afd7aee236726fd)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3  * Copyright (c) 2016,2017 Facebook
4  */
5 #include <linux/bpf.h>
6 #include <linux/btf.h>
7 #include <linux/err.h>
8 #include <linux/slab.h>
9 #include <linux/mm.h>
10 #include <linux/filter.h>
11 #include <linux/perf_event.h>
12 #include <uapi/linux/btf.h>
13 #include <linux/rcupdate_trace.h>
14 #include <linux/btf_ids.h>
15 #include <crypto/sha2.h>
16 
17 #include "map_in_map.h"
18 
19 #define ARRAY_CREATE_FLAG_MASK \
20 	(BPF_F_NUMA_NODE | BPF_F_MMAPABLE | BPF_F_ACCESS_MASK | \
21 	 BPF_F_PRESERVE_ELEMS | BPF_F_INNER_MAP)
22 
23 static void bpf_array_free_percpu(struct bpf_array *array)
24 {
25 	int i;
26 
27 	for (i = 0; i < array->map.max_entries; i++) {
28 		free_percpu(array->pptrs[i]);
29 		cond_resched();
30 	}
31 }
32 
33 static int bpf_array_alloc_percpu(struct bpf_array *array)
34 {
35 	void __percpu *ptr;
36 	int i;
37 
38 	for (i = 0; i < array->map.max_entries; i++) {
39 		ptr = bpf_map_alloc_percpu(&array->map, array->elem_size, 8,
40 					   GFP_USER | __GFP_NOWARN);
41 		if (!ptr) {
42 			bpf_array_free_percpu(array);
43 			return -ENOMEM;
44 		}
45 		array->pptrs[i] = ptr;
46 		cond_resched();
47 	}
48 
49 	return 0;
50 }
51 
52 /* Called from syscall */
53 int array_map_alloc_check(union bpf_attr *attr)
54 {
55 	bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
56 	int numa_node = bpf_map_attr_numa_node(attr);
57 
58 	/* check sanity of attributes */
59 	if (attr->max_entries == 0 || attr->key_size != 4 ||
60 	    attr->value_size == 0 ||
61 	    attr->map_flags & ~ARRAY_CREATE_FLAG_MASK ||
62 	    !bpf_map_flags_access_ok(attr->map_flags) ||
63 	    (percpu && numa_node != NUMA_NO_NODE))
64 		return -EINVAL;
65 
66 	if (attr->map_type != BPF_MAP_TYPE_ARRAY &&
67 	    attr->map_flags & (BPF_F_MMAPABLE | BPF_F_INNER_MAP))
68 		return -EINVAL;
69 
70 	if (attr->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY &&
71 	    attr->map_flags & BPF_F_PRESERVE_ELEMS)
72 		return -EINVAL;
73 
74 	/* avoid overflow on round_up(map->value_size) */
75 	if (attr->value_size > INT_MAX)
76 		return -E2BIG;
77 	/* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
78 	if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
79 		return -E2BIG;
80 
81 	return 0;
82 }
83 
84 static struct bpf_map *array_map_alloc(union bpf_attr *attr)
85 {
86 	bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
87 	int numa_node = bpf_map_attr_numa_node(attr);
88 	u32 elem_size, index_mask, max_entries;
89 	bool bypass_spec_v1 = bpf_bypass_spec_v1(NULL);
90 	u64 array_size, mask64;
91 	struct bpf_array *array;
92 
93 	elem_size = round_up(attr->value_size, 8);
94 
95 	max_entries = attr->max_entries;
96 
97 	/* On 32 bit archs roundup_pow_of_two() with max_entries that has
98 	 * upper most bit set in u32 space is undefined behavior due to
99 	 * resulting 1U << 32, so do it manually here in u64 space.
100 	 */
101 	mask64 = fls_long(max_entries - 1);
102 	mask64 = 1ULL << mask64;
103 	mask64 -= 1;
104 
105 	index_mask = mask64;
106 	if (!bypass_spec_v1) {
107 		/* round up array size to nearest power of 2,
108 		 * since cpu will speculate within index_mask limits
109 		 */
110 		max_entries = index_mask + 1;
111 		/* Check for overflows. */
112 		if (max_entries < attr->max_entries)
113 			return ERR_PTR(-E2BIG);
114 	}
115 
116 	array_size = sizeof(*array);
117 	if (percpu) {
118 		array_size += (u64) max_entries * sizeof(void *);
119 	} else {
120 		/* rely on vmalloc() to return page-aligned memory and
121 		 * ensure array->value is exactly page-aligned
122 		 */
123 		if (attr->map_flags & BPF_F_MMAPABLE) {
124 			array_size = PAGE_ALIGN(array_size);
125 			array_size += PAGE_ALIGN((u64) max_entries * elem_size);
126 		} else {
127 			array_size += (u64) max_entries * elem_size;
128 		}
129 	}
130 
131 	/* allocate all map elements and zero-initialize them */
132 	if (attr->map_flags & BPF_F_MMAPABLE) {
133 		void *data;
134 
135 		/* kmalloc'ed memory can't be mmap'ed, use explicit vmalloc */
136 		data = bpf_map_area_mmapable_alloc(array_size, numa_node);
137 		if (!data)
138 			return ERR_PTR(-ENOMEM);
139 		array = data + PAGE_ALIGN(sizeof(struct bpf_array))
140 			- offsetof(struct bpf_array, value);
141 	} else {
142 		array = bpf_map_area_alloc(array_size, numa_node);
143 	}
144 	if (!array)
145 		return ERR_PTR(-ENOMEM);
146 	array->index_mask = index_mask;
147 	array->map.bypass_spec_v1 = bypass_spec_v1;
148 
149 	/* copy mandatory map attributes */
150 	bpf_map_init_from_attr(&array->map, attr);
151 	array->elem_size = elem_size;
152 
153 	if (percpu && bpf_array_alloc_percpu(array)) {
154 		bpf_map_area_free(array);
155 		return ERR_PTR(-ENOMEM);
156 	}
157 
158 	return &array->map;
159 }
160 
161 static void *array_map_elem_ptr(struct bpf_array* array, u32 index)
162 {
163 	return array->value + (u64)array->elem_size * index;
164 }
165 
166 /* Called from syscall or from eBPF program */
167 static void *array_map_lookup_elem(struct bpf_map *map, void *key)
168 {
169 	struct bpf_array *array = container_of(map, struct bpf_array, map);
170 	u32 index = *(u32 *)key;
171 
172 	if (unlikely(index >= array->map.max_entries))
173 		return NULL;
174 
175 	return array->value + (u64)array->elem_size * (index & array->index_mask);
176 }
177 
178 static int array_map_get_hash(struct bpf_map *map)
179 {
180 	struct bpf_array *array = container_of(map, struct bpf_array, map);
181 
182 	sha256(array->value, (u64)array->elem_size * array->map.max_entries,
183 	       array->map.sha);
184 	return 0;
185 }
186 
187 static int array_map_direct_value_addr(const struct bpf_map *map, u64 *imm,
188 				       u32 off)
189 {
190 	struct bpf_array *array = container_of(map, struct bpf_array, map);
191 
192 	if (map->max_entries != 1)
193 		return -ENOTSUPP;
194 	if (off >= map->value_size)
195 		return -EINVAL;
196 
197 	*imm = (unsigned long)array->value;
198 	return 0;
199 }
200 
201 static int array_map_direct_value_meta(const struct bpf_map *map, u64 imm,
202 				       u32 *off)
203 {
204 	struct bpf_array *array = container_of(map, struct bpf_array, map);
205 	u64 base = (unsigned long)array->value;
206 	u64 range = array->elem_size;
207 
208 	if (map->max_entries != 1)
209 		return -ENOTSUPP;
210 	if (imm < base || imm >= base + range)
211 		return -ENOENT;
212 
213 	*off = imm - base;
214 	return 0;
215 }
216 
217 /* emit BPF instructions equivalent to C code of array_map_lookup_elem() */
218 static int array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
219 {
220 	struct bpf_array *array = container_of(map, struct bpf_array, map);
221 	struct bpf_insn *insn = insn_buf;
222 	u32 elem_size = array->elem_size;
223 	const int ret = BPF_REG_0;
224 	const int map_ptr = BPF_REG_1;
225 	const int index = BPF_REG_2;
226 
227 	if (map->map_flags & BPF_F_INNER_MAP)
228 		return -EOPNOTSUPP;
229 
230 	*insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
231 	*insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
232 	if (!map->bypass_spec_v1) {
233 		*insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 4);
234 		*insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
235 	} else {
236 		*insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 3);
237 	}
238 
239 	if (is_power_of_2(elem_size)) {
240 		*insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
241 	} else {
242 		*insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
243 	}
244 	*insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
245 	*insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
246 	*insn++ = BPF_MOV64_IMM(ret, 0);
247 	return insn - insn_buf;
248 }
249 
250 /* Called from eBPF program */
251 static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
252 {
253 	struct bpf_array *array = container_of(map, struct bpf_array, map);
254 	u32 index = *(u32 *)key;
255 
256 	if (unlikely(index >= array->map.max_entries))
257 		return NULL;
258 
259 	return this_cpu_ptr(array->pptrs[index & array->index_mask]);
260 }
261 
262 /* emit BPF instructions equivalent to C code of percpu_array_map_lookup_elem() */
263 static int percpu_array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
264 {
265 	struct bpf_array *array = container_of(map, struct bpf_array, map);
266 	struct bpf_insn *insn = insn_buf;
267 
268 	if (!bpf_jit_supports_percpu_insn())
269 		return -EOPNOTSUPP;
270 
271 	if (map->map_flags & BPF_F_INNER_MAP)
272 		return -EOPNOTSUPP;
273 
274 	BUILD_BUG_ON(offsetof(struct bpf_array, map) != 0);
275 	*insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, offsetof(struct bpf_array, pptrs));
276 
277 	*insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_2, 0);
278 	if (!map->bypass_spec_v1) {
279 		*insn++ = BPF_JMP_IMM(BPF_JGE, BPF_REG_0, map->max_entries, 6);
280 		*insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_0, array->index_mask);
281 	} else {
282 		*insn++ = BPF_JMP_IMM(BPF_JGE, BPF_REG_0, map->max_entries, 5);
283 	}
284 
285 	*insn++ = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3);
286 	*insn++ = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1);
287 	*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0);
288 	*insn++ = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);
289 	*insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
290 	*insn++ = BPF_MOV64_IMM(BPF_REG_0, 0);
291 	return insn - insn_buf;
292 }
293 
294 static void *percpu_array_map_lookup_percpu_elem(struct bpf_map *map, void *key, u32 cpu)
295 {
296 	struct bpf_array *array = container_of(map, struct bpf_array, map);
297 	u32 index = *(u32 *)key;
298 
299 	if (cpu >= nr_cpu_ids)
300 		return NULL;
301 
302 	if (unlikely(index >= array->map.max_entries))
303 		return NULL;
304 
305 	return per_cpu_ptr(array->pptrs[index & array->index_mask], cpu);
306 }
307 
308 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value, u64 map_flags)
309 {
310 	struct bpf_array *array = container_of(map, struct bpf_array, map);
311 	u32 index = *(u32 *)key;
312 	void __percpu *pptr;
313 	int cpu, off = 0;
314 	u32 size;
315 
316 	if (unlikely(index >= array->map.max_entries))
317 		return -ENOENT;
318 
319 	/* per_cpu areas are zero-filled and bpf programs can only
320 	 * access 'value_size' of them, so copying rounded areas
321 	 * will not leak any kernel data
322 	 */
323 	size = array->elem_size;
324 	rcu_read_lock();
325 	pptr = array->pptrs[index & array->index_mask];
326 	if (map_flags & BPF_F_CPU) {
327 		cpu = map_flags >> 32;
328 		copy_map_value(map, value, per_cpu_ptr(pptr, cpu));
329 		check_and_init_map_value(map, value);
330 		goto unlock;
331 	}
332 	for_each_possible_cpu(cpu) {
333 		copy_map_value_long(map, value + off, per_cpu_ptr(pptr, cpu));
334 		check_and_init_map_value(map, value + off);
335 		off += size;
336 	}
337 unlock:
338 	rcu_read_unlock();
339 	return 0;
340 }
341 
342 /* Called from syscall */
343 int bpf_array_get_next_key(struct bpf_map *map, void *key, void *next_key)
344 {
345 	u32 index = key ? *(u32 *)key : U32_MAX;
346 	u32 *next = (u32 *)next_key;
347 
348 	if (index >= map->max_entries) {
349 		*next = 0;
350 		return 0;
351 	}
352 
353 	if (index == map->max_entries - 1)
354 		return -ENOENT;
355 
356 	*next = index + 1;
357 	return 0;
358 }
359 
360 /* Called from syscall or from eBPF program */
361 static long array_map_update_elem(struct bpf_map *map, void *key, void *value,
362 				  u64 map_flags)
363 {
364 	struct bpf_array *array = container_of(map, struct bpf_array, map);
365 	u32 index = *(u32 *)key;
366 	char *val;
367 
368 	if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST))
369 		/* unknown flags */
370 		return -EINVAL;
371 
372 	if (unlikely(index >= array->map.max_entries))
373 		/* all elements were pre-allocated, cannot insert a new one */
374 		return -E2BIG;
375 
376 	if (unlikely(map_flags & BPF_NOEXIST))
377 		/* all elements already exist */
378 		return -EEXIST;
379 
380 	if (unlikely((map_flags & BPF_F_LOCK) &&
381 		     !btf_record_has_field(map->record, BPF_SPIN_LOCK)))
382 		return -EINVAL;
383 
384 	if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
385 		val = this_cpu_ptr(array->pptrs[index & array->index_mask]);
386 		copy_map_value(map, val, value);
387 		bpf_obj_cancel_fields(map, val);
388 	} else {
389 		val = array->value +
390 			(u64)array->elem_size * (index & array->index_mask);
391 		if (map_flags & BPF_F_LOCK)
392 			copy_map_value_locked(map, val, value, false);
393 		else
394 			copy_map_value(map, val, value);
395 		bpf_obj_cancel_fields(map, val);
396 	}
397 	return 0;
398 }
399 
400 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
401 			    u64 map_flags)
402 {
403 	struct bpf_array *array = container_of(map, struct bpf_array, map);
404 	u32 index = *(u32 *)key;
405 	void __percpu *pptr;
406 	void *ptr, *val;
407 	u32 size;
408 	int cpu;
409 
410 	if (unlikely((map_flags & BPF_F_LOCK) || (u32)map_flags > BPF_F_ALL_CPUS))
411 		/* unknown flags */
412 		return -EINVAL;
413 
414 	if (unlikely(index >= array->map.max_entries))
415 		/* all elements were pre-allocated, cannot insert a new one */
416 		return -E2BIG;
417 
418 	if (unlikely(map_flags == BPF_NOEXIST))
419 		/* all elements already exist */
420 		return -EEXIST;
421 
422 	/* the user space will provide round_up(value_size, 8) bytes that
423 	 * will be copied into per-cpu area. bpf programs can only access
424 	 * value_size of it. During lookup the same extra bytes will be
425 	 * returned or zeros which were zero-filled by percpu_alloc,
426 	 * so no kernel data leaks possible
427 	 */
428 	size = array->elem_size;
429 	rcu_read_lock();
430 	pptr = array->pptrs[index & array->index_mask];
431 	if (map_flags & BPF_F_CPU) {
432 		cpu = map_flags >> 32;
433 		ptr = per_cpu_ptr(pptr, cpu);
434 		copy_map_value(map, ptr, value);
435 		bpf_obj_cancel_fields(map, ptr);
436 		goto unlock;
437 	}
438 	for_each_possible_cpu(cpu) {
439 		ptr = per_cpu_ptr(pptr, cpu);
440 		val = (map_flags & BPF_F_ALL_CPUS) ? value : value + size * cpu;
441 		copy_map_value(map, ptr, val);
442 		bpf_obj_cancel_fields(map, ptr);
443 	}
444 unlock:
445 	rcu_read_unlock();
446 	return 0;
447 }
448 
449 /* Called from syscall or from eBPF program */
450 static long array_map_delete_elem(struct bpf_map *map, void *key)
451 {
452 	return -EINVAL;
453 }
454 
455 static void *array_map_vmalloc_addr(struct bpf_array *array)
456 {
457 	return (void *)round_down((unsigned long)array, PAGE_SIZE);
458 }
459 
460 static void array_map_free_internal_structs(struct bpf_map *map)
461 {
462 	struct bpf_array *array = container_of(map, struct bpf_array, map);
463 	int i;
464 
465 	/* We only free internal structs on uref dropping to zero */
466 	if (!bpf_map_has_internal_structs(map))
467 		return;
468 
469 	for (i = 0; i < array->map.max_entries; i++)
470 		bpf_map_free_internal_structs(map, array_map_elem_ptr(array, i));
471 }
472 
473 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
474 static void array_map_free(struct bpf_map *map)
475 {
476 	struct bpf_array *array = container_of(map, struct bpf_array, map);
477 	int i;
478 
479 	if (!IS_ERR_OR_NULL(map->record)) {
480 		if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
481 			for (i = 0; i < array->map.max_entries; i++) {
482 				void __percpu *pptr = array->pptrs[i & array->index_mask];
483 				int cpu;
484 
485 				for_each_possible_cpu(cpu) {
486 					bpf_obj_free_fields(map->record, per_cpu_ptr(pptr, cpu));
487 					cond_resched();
488 				}
489 			}
490 		} else {
491 			for (i = 0; i < array->map.max_entries; i++)
492 				bpf_obj_free_fields(map->record, array_map_elem_ptr(array, i));
493 		}
494 	}
495 
496 	if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
497 		bpf_array_free_percpu(array);
498 
499 	if (array->map.map_flags & BPF_F_MMAPABLE)
500 		bpf_map_area_free(array_map_vmalloc_addr(array));
501 	else
502 		bpf_map_area_free(array);
503 }
504 
505 static void array_map_seq_show_elem(struct bpf_map *map, void *key,
506 				    struct seq_file *m)
507 {
508 	void *value;
509 
510 	rcu_read_lock();
511 
512 	value = array_map_lookup_elem(map, key);
513 	if (!value) {
514 		rcu_read_unlock();
515 		return;
516 	}
517 
518 	if (map->btf_key_type_id)
519 		seq_printf(m, "%u: ", *(u32 *)key);
520 	btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
521 	seq_putc(m, '\n');
522 
523 	rcu_read_unlock();
524 }
525 
526 static void percpu_array_map_seq_show_elem(struct bpf_map *map, void *key,
527 					   struct seq_file *m)
528 {
529 	struct bpf_array *array = container_of(map, struct bpf_array, map);
530 	u32 index = *(u32 *)key;
531 	void __percpu *pptr;
532 	int cpu;
533 
534 	rcu_read_lock();
535 
536 	seq_printf(m, "%u: {\n", *(u32 *)key);
537 	pptr = array->pptrs[index & array->index_mask];
538 	for_each_possible_cpu(cpu) {
539 		seq_printf(m, "\tcpu%d: ", cpu);
540 		btf_type_seq_show(map->btf, map->btf_value_type_id,
541 				  per_cpu_ptr(pptr, cpu), m);
542 		seq_putc(m, '\n');
543 	}
544 	seq_puts(m, "}\n");
545 
546 	rcu_read_unlock();
547 }
548 
549 static int array_map_check_btf(struct bpf_map *map,
550 			       const struct btf *btf,
551 			       const struct btf_type *key_type,
552 			       const struct btf_type *value_type)
553 {
554 	/* One exception for keyless BTF: .bss/.data/.rodata map */
555 	if (btf_type_is_void(key_type)) {
556 		if (map->map_type != BPF_MAP_TYPE_ARRAY ||
557 		    map->max_entries != 1)
558 			return -EINVAL;
559 
560 		if (BTF_INFO_KIND(value_type->info) != BTF_KIND_DATASEC)
561 			return -EINVAL;
562 
563 		return 0;
564 	}
565 
566 	/*
567 	 * Bpf array can only take a u32 key. This check makes sure
568 	 * that the btf matches the attr used during map_create.
569 	 */
570 	if (!btf_type_is_i32(key_type))
571 		return -EINVAL;
572 
573 	return 0;
574 }
575 
576 static int array_map_mmap(struct bpf_map *map, struct vm_area_struct *vma)
577 {
578 	struct bpf_array *array = container_of(map, struct bpf_array, map);
579 	pgoff_t pgoff = PAGE_ALIGN(sizeof(*array)) >> PAGE_SHIFT;
580 
581 	if (!(map->map_flags & BPF_F_MMAPABLE))
582 		return -EINVAL;
583 
584 	if (vma->vm_pgoff * PAGE_SIZE + (vma->vm_end - vma->vm_start) >
585 	    PAGE_ALIGN((u64)array->map.max_entries * array->elem_size))
586 		return -EINVAL;
587 
588 	return remap_vmalloc_range(vma, array_map_vmalloc_addr(array),
589 				   vma->vm_pgoff + pgoff);
590 }
591 
592 static bool array_map_meta_equal(const struct bpf_map *meta0,
593 				 const struct bpf_map *meta1)
594 {
595 	if (!bpf_map_meta_equal(meta0, meta1))
596 		return false;
597 	return meta0->map_flags & BPF_F_INNER_MAP ? true :
598 	       meta0->max_entries == meta1->max_entries;
599 }
600 
601 struct bpf_iter_seq_array_map_info {
602 	struct bpf_map *map;
603 	void *percpu_value_buf;
604 	u32 index;
605 };
606 
607 static void *bpf_array_map_seq_start(struct seq_file *seq, loff_t *pos)
608 {
609 	struct bpf_iter_seq_array_map_info *info = seq->private;
610 	struct bpf_map *map = info->map;
611 	struct bpf_array *array;
612 	u32 index;
613 
614 	if (info->index >= map->max_entries)
615 		return NULL;
616 
617 	if (*pos == 0)
618 		++*pos;
619 	array = container_of(map, struct bpf_array, map);
620 	index = info->index & array->index_mask;
621 	if (info->percpu_value_buf)
622 		return (void *)(uintptr_t)array->pptrs[index];
623 	return array_map_elem_ptr(array, index);
624 }
625 
626 static void *bpf_array_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
627 {
628 	struct bpf_iter_seq_array_map_info *info = seq->private;
629 	struct bpf_map *map = info->map;
630 	struct bpf_array *array;
631 	u32 index;
632 
633 	++*pos;
634 	++info->index;
635 	if (info->index >= map->max_entries)
636 		return NULL;
637 
638 	array = container_of(map, struct bpf_array, map);
639 	index = info->index & array->index_mask;
640 	if (info->percpu_value_buf)
641 		return (void *)(uintptr_t)array->pptrs[index];
642 	return array_map_elem_ptr(array, index);
643 }
644 
645 static int __bpf_array_map_seq_show(struct seq_file *seq, void *v)
646 {
647 	struct bpf_iter_seq_array_map_info *info = seq->private;
648 	struct bpf_iter__bpf_map_elem ctx = {};
649 	struct bpf_map *map = info->map;
650 	struct bpf_array *array = container_of(map, struct bpf_array, map);
651 	struct bpf_iter_meta meta;
652 	struct bpf_prog *prog;
653 	int off = 0, cpu = 0;
654 	void __percpu *pptr;
655 	u32 size;
656 
657 	meta.seq = seq;
658 	prog = bpf_iter_get_info(&meta, v == NULL);
659 	if (!prog)
660 		return 0;
661 
662 	ctx.meta = &meta;
663 	ctx.map = info->map;
664 	if (v) {
665 		ctx.key = &info->index;
666 
667 		if (!info->percpu_value_buf) {
668 			ctx.value = v;
669 		} else {
670 			pptr = (void __percpu *)(uintptr_t)v;
671 			size = array->elem_size;
672 			for_each_possible_cpu(cpu) {
673 				copy_map_value_long(map, info->percpu_value_buf + off,
674 						    per_cpu_ptr(pptr, cpu));
675 				check_and_init_map_value(map, info->percpu_value_buf + off);
676 				off += size;
677 			}
678 			ctx.value = info->percpu_value_buf;
679 		}
680 	}
681 
682 	return bpf_iter_run_prog(prog, &ctx);
683 }
684 
685 static int bpf_array_map_seq_show(struct seq_file *seq, void *v)
686 {
687 	return __bpf_array_map_seq_show(seq, v);
688 }
689 
690 static void bpf_array_map_seq_stop(struct seq_file *seq, void *v)
691 {
692 	if (!v)
693 		(void)__bpf_array_map_seq_show(seq, NULL);
694 }
695 
696 static int bpf_iter_init_array_map(void *priv_data,
697 				   struct bpf_iter_aux_info *aux)
698 {
699 	struct bpf_iter_seq_array_map_info *seq_info = priv_data;
700 	struct bpf_map *map = aux->map;
701 	struct bpf_array *array = container_of(map, struct bpf_array, map);
702 	void *value_buf;
703 	u32 buf_size;
704 
705 	if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
706 		buf_size = array->elem_size * num_possible_cpus();
707 		value_buf = kmalloc(buf_size, GFP_USER | __GFP_NOWARN);
708 		if (!value_buf)
709 			return -ENOMEM;
710 
711 		seq_info->percpu_value_buf = value_buf;
712 	}
713 
714 	/* bpf_iter_attach_map() acquires a map uref, and the uref may be
715 	 * released before or in the middle of iterating map elements, so
716 	 * acquire an extra map uref for iterator.
717 	 */
718 	bpf_map_inc_with_uref(map);
719 	seq_info->map = map;
720 	return 0;
721 }
722 
723 static void bpf_iter_fini_array_map(void *priv_data)
724 {
725 	struct bpf_iter_seq_array_map_info *seq_info = priv_data;
726 
727 	bpf_map_put_with_uref(seq_info->map);
728 	kfree(seq_info->percpu_value_buf);
729 }
730 
731 static const struct seq_operations bpf_array_map_seq_ops = {
732 	.start	= bpf_array_map_seq_start,
733 	.next	= bpf_array_map_seq_next,
734 	.stop	= bpf_array_map_seq_stop,
735 	.show	= bpf_array_map_seq_show,
736 };
737 
738 static const struct bpf_iter_seq_info iter_seq_info = {
739 	.seq_ops		= &bpf_array_map_seq_ops,
740 	.init_seq_private	= bpf_iter_init_array_map,
741 	.fini_seq_private	= bpf_iter_fini_array_map,
742 	.seq_priv_size		= sizeof(struct bpf_iter_seq_array_map_info),
743 };
744 
745 static long bpf_for_each_array_elem(struct bpf_map *map, bpf_callback_t callback_fn,
746 				    void *callback_ctx, u64 flags)
747 {
748 	u32 i, key, num_elems = 0;
749 	struct bpf_array *array;
750 	bool is_percpu;
751 	u64 ret = 0;
752 	void *val;
753 
754 	cant_migrate();
755 
756 	if (flags != 0)
757 		return -EINVAL;
758 
759 	is_percpu = map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
760 	array = container_of(map, struct bpf_array, map);
761 	for (i = 0; i < map->max_entries; i++) {
762 		if (is_percpu)
763 			val = this_cpu_ptr(array->pptrs[i]);
764 		else
765 			val = array_map_elem_ptr(array, i);
766 		num_elems++;
767 		key = i;
768 		ret = callback_fn((u64)(long)map, (u64)(long)&key,
769 				  (u64)(long)val, (u64)(long)callback_ctx, 0);
770 		/* return value: 0 - continue, 1 - stop and return */
771 		if (ret)
772 			break;
773 	}
774 
775 	return num_elems;
776 }
777 
778 static u64 array_map_mem_usage(const struct bpf_map *map)
779 {
780 	struct bpf_array *array = container_of(map, struct bpf_array, map);
781 	bool percpu = map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
782 	u32 elem_size = array->elem_size;
783 	u64 entries = map->max_entries;
784 	u64 usage = sizeof(*array);
785 
786 	if (percpu) {
787 		usage += entries * sizeof(void *);
788 		usage += entries * elem_size * num_possible_cpus();
789 	} else {
790 		if (map->map_flags & BPF_F_MMAPABLE) {
791 			usage = PAGE_ALIGN(usage);
792 			usage += PAGE_ALIGN(entries * elem_size);
793 		} else {
794 			usage += entries * elem_size;
795 		}
796 	}
797 	return usage;
798 }
799 
800 BTF_ID_LIST_SINGLE(array_map_btf_ids, struct, bpf_array)
801 const struct bpf_map_ops array_map_ops = {
802 	.map_meta_equal = array_map_meta_equal,
803 	.map_alloc_check = array_map_alloc_check,
804 	.map_alloc = array_map_alloc,
805 	.map_free = array_map_free,
806 	.map_get_next_key = bpf_array_get_next_key,
807 	.map_release_uref = array_map_free_internal_structs,
808 	.map_lookup_elem = array_map_lookup_elem,
809 	.map_update_elem = array_map_update_elem,
810 	.map_delete_elem = array_map_delete_elem,
811 	.map_gen_lookup = array_map_gen_lookup,
812 	.map_direct_value_addr = array_map_direct_value_addr,
813 	.map_direct_value_meta = array_map_direct_value_meta,
814 	.map_mmap = array_map_mmap,
815 	.map_seq_show_elem = array_map_seq_show_elem,
816 	.map_check_btf = array_map_check_btf,
817 	.map_lookup_batch = generic_map_lookup_batch,
818 	.map_update_batch = generic_map_update_batch,
819 	.map_set_for_each_callback_args = map_set_for_each_callback_args,
820 	.map_for_each_callback = bpf_for_each_array_elem,
821 	.map_mem_usage = array_map_mem_usage,
822 	.map_btf_id = &array_map_btf_ids[0],
823 	.iter_seq_info = &iter_seq_info,
824 	.map_get_hash = &array_map_get_hash,
825 };
826 
827 const struct bpf_map_ops percpu_array_map_ops = {
828 	.map_meta_equal = array_map_meta_equal,
829 	.map_alloc_check = array_map_alloc_check,
830 	.map_alloc = array_map_alloc,
831 	.map_free = array_map_free,
832 	.map_get_next_key = bpf_array_get_next_key,
833 	.map_lookup_elem = percpu_array_map_lookup_elem,
834 	.map_gen_lookup = percpu_array_map_gen_lookup,
835 	.map_update_elem = array_map_update_elem,
836 	.map_delete_elem = array_map_delete_elem,
837 	.map_lookup_percpu_elem = percpu_array_map_lookup_percpu_elem,
838 	.map_seq_show_elem = percpu_array_map_seq_show_elem,
839 	.map_check_btf = array_map_check_btf,
840 	.map_lookup_batch = generic_map_lookup_batch,
841 	.map_update_batch = generic_map_update_batch,
842 	.map_set_for_each_callback_args = map_set_for_each_callback_args,
843 	.map_for_each_callback = bpf_for_each_array_elem,
844 	.map_mem_usage = array_map_mem_usage,
845 	.map_btf_id = &array_map_btf_ids[0],
846 	.iter_seq_info = &iter_seq_info,
847 };
848 
849 static int fd_array_map_alloc_check(union bpf_attr *attr)
850 {
851 	/* only file descriptors can be stored in this type of map */
852 	if (attr->value_size != sizeof(u32))
853 		return -EINVAL;
854 	/* Program read-only/write-only not supported for special maps yet. */
855 	if (attr->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG))
856 		return -EINVAL;
857 	return array_map_alloc_check(attr);
858 }
859 
860 static void fd_array_map_free(struct bpf_map *map)
861 {
862 	struct bpf_array *array = container_of(map, struct bpf_array, map);
863 	int i;
864 
865 	/* make sure it's empty */
866 	for (i = 0; i < array->map.max_entries; i++)
867 		BUG_ON(array->ptrs[i] != NULL);
868 
869 	bpf_map_area_free(array);
870 }
871 
872 static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
873 {
874 	return ERR_PTR(-EOPNOTSUPP);
875 }
876 
877 /* only called from syscall */
878 int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
879 {
880 	void **elem, *ptr;
881 	int ret =  0;
882 
883 	if (!map->ops->map_fd_sys_lookup_elem)
884 		return -ENOTSUPP;
885 
886 	rcu_read_lock();
887 	elem = array_map_lookup_elem(map, key);
888 	if (elem && (ptr = READ_ONCE(*elem)))
889 		*value = map->ops->map_fd_sys_lookup_elem(ptr);
890 	else
891 		ret = -ENOENT;
892 	rcu_read_unlock();
893 
894 	return ret;
895 }
896 
897 /* only called from syscall */
898 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
899 				 void *key, void *value, u64 map_flags)
900 {
901 	struct bpf_array *array = container_of(map, struct bpf_array, map);
902 	void *new_ptr, *old_ptr;
903 	u32 index = *(u32 *)key, ufd;
904 
905 	if (map_flags != BPF_ANY)
906 		return -EINVAL;
907 
908 	if (index >= array->map.max_entries)
909 		return -E2BIG;
910 
911 	ufd = *(u32 *)value;
912 	new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
913 	if (IS_ERR(new_ptr))
914 		return PTR_ERR(new_ptr);
915 
916 	if (map->ops->map_poke_run) {
917 		mutex_lock(&array->aux->poke_mutex);
918 		old_ptr = xchg(array->ptrs + index, new_ptr);
919 		map->ops->map_poke_run(map, index, old_ptr, new_ptr);
920 		mutex_unlock(&array->aux->poke_mutex);
921 	} else {
922 		old_ptr = xchg(array->ptrs + index, new_ptr);
923 	}
924 
925 	if (old_ptr)
926 		map->ops->map_fd_put_ptr(map, old_ptr, true);
927 	return 0;
928 }
929 
930 static long __fd_array_map_delete_elem(struct bpf_map *map, void *key, bool need_defer)
931 {
932 	struct bpf_array *array = container_of(map, struct bpf_array, map);
933 	void *old_ptr;
934 	u32 index = *(u32 *)key;
935 
936 	if (index >= array->map.max_entries)
937 		return -E2BIG;
938 
939 	if (map->ops->map_poke_run) {
940 		mutex_lock(&array->aux->poke_mutex);
941 		old_ptr = xchg(array->ptrs + index, NULL);
942 		map->ops->map_poke_run(map, index, old_ptr, NULL);
943 		mutex_unlock(&array->aux->poke_mutex);
944 	} else {
945 		old_ptr = xchg(array->ptrs + index, NULL);
946 	}
947 
948 	if (old_ptr) {
949 		map->ops->map_fd_put_ptr(map, old_ptr, need_defer);
950 		return 0;
951 	} else {
952 		return -ENOENT;
953 	}
954 }
955 
956 static long fd_array_map_delete_elem(struct bpf_map *map, void *key)
957 {
958 	return __fd_array_map_delete_elem(map, key, true);
959 }
960 
961 static void *prog_fd_array_get_ptr(struct bpf_map *map,
962 				   struct file *map_file, int fd)
963 {
964 	struct bpf_prog *prog = bpf_prog_get(fd);
965 	bool is_extended;
966 
967 	if (IS_ERR(prog))
968 		return prog;
969 
970 	if (prog->type == BPF_PROG_TYPE_EXT ||
971 	    !bpf_prog_map_compatible(map, prog)) {
972 		bpf_prog_put(prog);
973 		return ERR_PTR(-EINVAL);
974 	}
975 
976 	mutex_lock(&prog->aux->ext_mutex);
977 	is_extended = prog->aux->is_extended;
978 	if (!is_extended)
979 		prog->aux->prog_array_member_cnt++;
980 	mutex_unlock(&prog->aux->ext_mutex);
981 	if (is_extended) {
982 		/* Extended prog can not be tail callee. It's to prevent a
983 		 * potential infinite loop like:
984 		 * tail callee prog entry -> tail callee prog subprog ->
985 		 * freplace prog entry --tailcall-> tail callee prog entry.
986 		 */
987 		bpf_prog_put(prog);
988 		return ERR_PTR(-EBUSY);
989 	}
990 
991 	return prog;
992 }
993 
994 static void prog_fd_array_put_ptr(struct bpf_map *map, void *ptr, bool need_defer)
995 {
996 	struct bpf_prog *prog = ptr;
997 
998 	mutex_lock(&prog->aux->ext_mutex);
999 	prog->aux->prog_array_member_cnt--;
1000 	mutex_unlock(&prog->aux->ext_mutex);
1001 	/* bpf_prog is freed after one RCU or tasks trace grace period */
1002 	bpf_prog_put(prog);
1003 }
1004 
1005 static u32 prog_fd_array_sys_lookup_elem(void *ptr)
1006 {
1007 	return ((struct bpf_prog *)ptr)->aux->id;
1008 }
1009 
1010 /* decrement refcnt of all bpf_progs that are stored in this map */
1011 static void bpf_fd_array_map_clear(struct bpf_map *map, bool need_defer)
1012 {
1013 	struct bpf_array *array = container_of(map, struct bpf_array, map);
1014 	int i;
1015 
1016 	for (i = 0; i < array->map.max_entries; i++) {
1017 		__fd_array_map_delete_elem(map, &i, need_defer);
1018 		cond_resched();
1019 	}
1020 }
1021 
1022 static void prog_array_map_seq_show_elem(struct bpf_map *map, void *key,
1023 					 struct seq_file *m)
1024 {
1025 	void **elem, *ptr;
1026 	u32 prog_id;
1027 
1028 	rcu_read_lock();
1029 
1030 	elem = array_map_lookup_elem(map, key);
1031 	if (elem) {
1032 		ptr = READ_ONCE(*elem);
1033 		if (ptr) {
1034 			seq_printf(m, "%u: ", *(u32 *)key);
1035 			prog_id = prog_fd_array_sys_lookup_elem(ptr);
1036 			btf_type_seq_show(map->btf, map->btf_value_type_id,
1037 					  &prog_id, m);
1038 			seq_putc(m, '\n');
1039 		}
1040 	}
1041 
1042 	rcu_read_unlock();
1043 }
1044 
1045 struct prog_poke_elem {
1046 	struct list_head list;
1047 	struct bpf_prog_aux *aux;
1048 };
1049 
1050 static int prog_array_map_poke_track(struct bpf_map *map,
1051 				     struct bpf_prog_aux *prog_aux)
1052 {
1053 	struct prog_poke_elem *elem;
1054 	struct bpf_array_aux *aux;
1055 	int ret = 0;
1056 
1057 	aux = container_of(map, struct bpf_array, map)->aux;
1058 	mutex_lock(&aux->poke_mutex);
1059 	list_for_each_entry(elem, &aux->poke_progs, list) {
1060 		if (elem->aux == prog_aux)
1061 			goto out;
1062 	}
1063 
1064 	elem = kmalloc_obj(*elem);
1065 	if (!elem) {
1066 		ret = -ENOMEM;
1067 		goto out;
1068 	}
1069 
1070 	INIT_LIST_HEAD(&elem->list);
1071 	/* We must track the program's aux info at this point in time
1072 	 * since the program pointer itself may not be stable yet, see
1073 	 * also comment in prog_array_map_poke_run().
1074 	 */
1075 	elem->aux = prog_aux;
1076 
1077 	list_add_tail(&elem->list, &aux->poke_progs);
1078 out:
1079 	mutex_unlock(&aux->poke_mutex);
1080 	return ret;
1081 }
1082 
1083 static void prog_array_map_poke_untrack(struct bpf_map *map,
1084 					struct bpf_prog_aux *prog_aux)
1085 {
1086 	struct prog_poke_elem *elem, *tmp;
1087 	struct bpf_array_aux *aux;
1088 
1089 	aux = container_of(map, struct bpf_array, map)->aux;
1090 	mutex_lock(&aux->poke_mutex);
1091 	list_for_each_entry_safe(elem, tmp, &aux->poke_progs, list) {
1092 		if (elem->aux == prog_aux) {
1093 			list_del_init(&elem->list);
1094 			kfree(elem);
1095 			break;
1096 		}
1097 	}
1098 	mutex_unlock(&aux->poke_mutex);
1099 }
1100 
1101 void __weak bpf_arch_poke_desc_update(struct bpf_jit_poke_descriptor *poke,
1102 				      struct bpf_prog *new, struct bpf_prog *old)
1103 {
1104 	WARN_ON_ONCE(1);
1105 }
1106 
1107 static void prog_array_map_poke_run(struct bpf_map *map, u32 key,
1108 				    struct bpf_prog *old,
1109 				    struct bpf_prog *new)
1110 {
1111 	struct prog_poke_elem *elem;
1112 	struct bpf_array_aux *aux;
1113 
1114 	aux = container_of(map, struct bpf_array, map)->aux;
1115 	WARN_ON_ONCE(!mutex_is_locked(&aux->poke_mutex));
1116 
1117 	list_for_each_entry(elem, &aux->poke_progs, list) {
1118 		struct bpf_jit_poke_descriptor *poke;
1119 		int i;
1120 
1121 		for (i = 0; i < elem->aux->size_poke_tab; i++) {
1122 			poke = &elem->aux->poke_tab[i];
1123 
1124 			/* Few things to be aware of:
1125 			 *
1126 			 * 1) We can only ever access aux in this context, but
1127 			 *    not aux->prog since it might not be stable yet and
1128 			 *    there could be danger of use after free otherwise.
1129 			 * 2) Initially when we start tracking aux, the program
1130 			 *    is not JITed yet and also does not have a kallsyms
1131 			 *    entry. We skip these as poke->tailcall_target_stable
1132 			 *    is not active yet. The JIT will do the final fixup
1133 			 *    before setting it stable. The various
1134 			 *    poke->tailcall_target_stable are successively
1135 			 *    activated, so tail call updates can arrive from here
1136 			 *    while JIT is still finishing its final fixup for
1137 			 *    non-activated poke entries.
1138 			 * 3) Also programs reaching refcount of zero while patching
1139 			 *    is in progress is okay since we're protected under
1140 			 *    poke_mutex and untrack the programs before the JIT
1141 			 *    buffer is freed.
1142 			 */
1143 			if (!READ_ONCE(poke->tailcall_target_stable))
1144 				continue;
1145 			if (poke->reason != BPF_POKE_REASON_TAIL_CALL)
1146 				continue;
1147 			if (poke->tail_call.map != map ||
1148 			    poke->tail_call.key != key)
1149 				continue;
1150 
1151 			bpf_arch_poke_desc_update(poke, new, old);
1152 		}
1153 	}
1154 }
1155 
1156 static void prog_array_map_clear_deferred(struct work_struct *work)
1157 {
1158 	struct bpf_map *map = container_of(work, struct bpf_array_aux,
1159 					   work)->map;
1160 	bpf_fd_array_map_clear(map, true);
1161 	bpf_map_put(map);
1162 }
1163 
1164 static void prog_array_map_clear(struct bpf_map *map)
1165 {
1166 	struct bpf_array_aux *aux = container_of(map, struct bpf_array,
1167 						 map)->aux;
1168 	bpf_map_inc(map);
1169 	schedule_work(&aux->work);
1170 }
1171 
1172 static struct bpf_map *prog_array_map_alloc(union bpf_attr *attr)
1173 {
1174 	struct bpf_array_aux *aux;
1175 	struct bpf_map *map;
1176 
1177 	aux = kzalloc_obj(*aux, GFP_KERNEL_ACCOUNT);
1178 	if (!aux)
1179 		return ERR_PTR(-ENOMEM);
1180 
1181 	INIT_WORK(&aux->work, prog_array_map_clear_deferred);
1182 	INIT_LIST_HEAD(&aux->poke_progs);
1183 	mutex_init(&aux->poke_mutex);
1184 
1185 	map = array_map_alloc(attr);
1186 	if (IS_ERR(map)) {
1187 		kfree(aux);
1188 		return map;
1189 	}
1190 
1191 	container_of(map, struct bpf_array, map)->aux = aux;
1192 	aux->map = map;
1193 
1194 	return map;
1195 }
1196 
1197 static void prog_array_map_free(struct bpf_map *map)
1198 {
1199 	struct prog_poke_elem *elem, *tmp;
1200 	struct bpf_array_aux *aux;
1201 
1202 	aux = container_of(map, struct bpf_array, map)->aux;
1203 	list_for_each_entry_safe(elem, tmp, &aux->poke_progs, list) {
1204 		list_del_init(&elem->list);
1205 		kfree(elem);
1206 	}
1207 	kfree(aux);
1208 	fd_array_map_free(map);
1209 }
1210 
1211 /* prog_array->aux->{type,jited} is a runtime binding.
1212  * Doing static check alone in the verifier is not enough.
1213  * Thus, prog_array_map cannot be used as an inner_map
1214  * and map_meta_equal is not implemented.
1215  */
1216 const struct bpf_map_ops prog_array_map_ops = {
1217 	.map_alloc_check = fd_array_map_alloc_check,
1218 	.map_alloc = prog_array_map_alloc,
1219 	.map_free = prog_array_map_free,
1220 	.map_poke_track = prog_array_map_poke_track,
1221 	.map_poke_untrack = prog_array_map_poke_untrack,
1222 	.map_poke_run = prog_array_map_poke_run,
1223 	.map_get_next_key = bpf_array_get_next_key,
1224 	.map_lookup_elem = fd_array_map_lookup_elem,
1225 	.map_delete_elem = fd_array_map_delete_elem,
1226 	.map_fd_get_ptr = prog_fd_array_get_ptr,
1227 	.map_fd_put_ptr = prog_fd_array_put_ptr,
1228 	.map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem,
1229 	.map_release_uref = prog_array_map_clear,
1230 	.map_seq_show_elem = prog_array_map_seq_show_elem,
1231 	.map_mem_usage = array_map_mem_usage,
1232 	.map_btf_id = &array_map_btf_ids[0],
1233 };
1234 
1235 static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
1236 						   struct file *map_file)
1237 {
1238 	struct bpf_event_entry *ee;
1239 
1240 	ee = kzalloc_obj(*ee);
1241 	if (ee) {
1242 		ee->event = perf_file->private_data;
1243 		ee->perf_file = perf_file;
1244 		ee->map_file = map_file;
1245 	}
1246 
1247 	return ee;
1248 }
1249 
1250 static void __bpf_event_entry_free(struct rcu_head *rcu)
1251 {
1252 	struct bpf_event_entry *ee;
1253 
1254 	ee = container_of(rcu, struct bpf_event_entry, rcu);
1255 	fput(ee->perf_file);
1256 	kfree(ee);
1257 }
1258 
1259 static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
1260 {
1261 	call_rcu(&ee->rcu, __bpf_event_entry_free);
1262 }
1263 
1264 static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
1265 					 struct file *map_file, int fd)
1266 {
1267 	struct bpf_event_entry *ee;
1268 	struct perf_event *event;
1269 	struct file *perf_file;
1270 	u64 value;
1271 
1272 	perf_file = perf_event_get(fd);
1273 	if (IS_ERR(perf_file))
1274 		return perf_file;
1275 
1276 	ee = ERR_PTR(-EOPNOTSUPP);
1277 	event = perf_file->private_data;
1278 	if (perf_event_read_local(event, &value, NULL, NULL) == -EOPNOTSUPP)
1279 		goto err_out;
1280 
1281 	ee = bpf_event_entry_gen(perf_file, map_file);
1282 	if (ee)
1283 		return ee;
1284 	ee = ERR_PTR(-ENOMEM);
1285 err_out:
1286 	fput(perf_file);
1287 	return ee;
1288 }
1289 
1290 static void perf_event_fd_array_put_ptr(struct bpf_map *map, void *ptr, bool need_defer)
1291 {
1292 	/* bpf_perf_event is freed after one RCU grace period */
1293 	bpf_event_entry_free_rcu(ptr);
1294 }
1295 
1296 static void perf_event_fd_array_release(struct bpf_map *map,
1297 					struct file *map_file)
1298 {
1299 	struct bpf_array *array = container_of(map, struct bpf_array, map);
1300 	struct bpf_event_entry *ee;
1301 	int i;
1302 
1303 	if (map->map_flags & BPF_F_PRESERVE_ELEMS)
1304 		return;
1305 
1306 	rcu_read_lock();
1307 	for (i = 0; i < array->map.max_entries; i++) {
1308 		ee = READ_ONCE(array->ptrs[i]);
1309 		if (ee && ee->map_file == map_file)
1310 			__fd_array_map_delete_elem(map, &i, true);
1311 	}
1312 	rcu_read_unlock();
1313 }
1314 
1315 static void perf_event_fd_array_map_free(struct bpf_map *map)
1316 {
1317 	if (map->map_flags & BPF_F_PRESERVE_ELEMS)
1318 		bpf_fd_array_map_clear(map, false);
1319 	fd_array_map_free(map);
1320 }
1321 
1322 const struct bpf_map_ops perf_event_array_map_ops = {
1323 	.map_meta_equal = bpf_map_meta_equal,
1324 	.map_alloc_check = fd_array_map_alloc_check,
1325 	.map_alloc = array_map_alloc,
1326 	.map_free = perf_event_fd_array_map_free,
1327 	.map_get_next_key = bpf_array_get_next_key,
1328 	.map_lookup_elem = fd_array_map_lookup_elem,
1329 	.map_delete_elem = fd_array_map_delete_elem,
1330 	.map_fd_get_ptr = perf_event_fd_array_get_ptr,
1331 	.map_fd_put_ptr = perf_event_fd_array_put_ptr,
1332 	.map_release = perf_event_fd_array_release,
1333 	.map_check_btf = map_check_no_btf,
1334 	.map_mem_usage = array_map_mem_usage,
1335 	.map_btf_id = &array_map_btf_ids[0],
1336 };
1337 
1338 #ifdef CONFIG_CGROUPS
1339 static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
1340 				     struct file *map_file /* not used */,
1341 				     int fd)
1342 {
1343 	return cgroup_get_from_fd(fd);
1344 }
1345 
1346 static void cgroup_fd_array_put_ptr(struct bpf_map *map, void *ptr, bool need_defer)
1347 {
1348 	/* cgroup_put free cgrp after a rcu grace period */
1349 	cgroup_put(ptr);
1350 }
1351 
1352 static void cgroup_fd_array_free(struct bpf_map *map)
1353 {
1354 	bpf_fd_array_map_clear(map, false);
1355 	fd_array_map_free(map);
1356 }
1357 
1358 const struct bpf_map_ops cgroup_array_map_ops = {
1359 	.map_meta_equal = bpf_map_meta_equal,
1360 	.map_alloc_check = fd_array_map_alloc_check,
1361 	.map_alloc = array_map_alloc,
1362 	.map_free = cgroup_fd_array_free,
1363 	.map_get_next_key = bpf_array_get_next_key,
1364 	.map_lookup_elem = fd_array_map_lookup_elem,
1365 	.map_delete_elem = fd_array_map_delete_elem,
1366 	.map_fd_get_ptr = cgroup_fd_array_get_ptr,
1367 	.map_fd_put_ptr = cgroup_fd_array_put_ptr,
1368 	.map_check_btf = map_check_no_btf,
1369 	.map_mem_usage = array_map_mem_usage,
1370 	.map_btf_id = &array_map_btf_ids[0],
1371 };
1372 #endif
1373 
1374 static struct bpf_map *array_of_map_alloc(union bpf_attr *attr)
1375 {
1376 	struct bpf_map *map, *inner_map_meta;
1377 
1378 	inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1379 	if (IS_ERR(inner_map_meta))
1380 		return inner_map_meta;
1381 
1382 	map = array_map_alloc(attr);
1383 	if (IS_ERR(map)) {
1384 		bpf_map_meta_free(inner_map_meta);
1385 		return map;
1386 	}
1387 
1388 	map->inner_map_meta = inner_map_meta;
1389 
1390 	return map;
1391 }
1392 
1393 static void array_of_map_free(struct bpf_map *map)
1394 {
1395 	/* map->inner_map_meta is only accessed by syscall which
1396 	 * is protected by fdget/fdput.
1397 	 */
1398 	bpf_map_meta_free(map->inner_map_meta);
1399 	bpf_fd_array_map_clear(map, false);
1400 	fd_array_map_free(map);
1401 }
1402 
1403 static void *array_of_map_lookup_elem(struct bpf_map *map, void *key)
1404 {
1405 	struct bpf_map **inner_map = array_map_lookup_elem(map, key);
1406 
1407 	if (!inner_map)
1408 		return NULL;
1409 
1410 	return READ_ONCE(*inner_map);
1411 }
1412 
1413 static int array_of_map_gen_lookup(struct bpf_map *map,
1414 				   struct bpf_insn *insn_buf)
1415 {
1416 	struct bpf_array *array = container_of(map, struct bpf_array, map);
1417 	u32 elem_size = array->elem_size;
1418 	struct bpf_insn *insn = insn_buf;
1419 	const int ret = BPF_REG_0;
1420 	const int map_ptr = BPF_REG_1;
1421 	const int index = BPF_REG_2;
1422 
1423 	*insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
1424 	*insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
1425 	if (!map->bypass_spec_v1) {
1426 		*insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 6);
1427 		*insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
1428 	} else {
1429 		*insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 5);
1430 	}
1431 	if (is_power_of_2(elem_size))
1432 		*insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
1433 	else
1434 		*insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
1435 	*insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
1436 	*insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
1437 	*insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
1438 	*insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
1439 	*insn++ = BPF_MOV64_IMM(ret, 0);
1440 
1441 	return insn - insn_buf;
1442 }
1443 
1444 const struct bpf_map_ops array_of_maps_map_ops = {
1445 	.map_alloc_check = fd_array_map_alloc_check,
1446 	.map_alloc = array_of_map_alloc,
1447 	.map_free = array_of_map_free,
1448 	.map_get_next_key = bpf_array_get_next_key,
1449 	.map_lookup_elem = array_of_map_lookup_elem,
1450 	.map_delete_elem = fd_array_map_delete_elem,
1451 	.map_fd_get_ptr = bpf_map_fd_get_ptr,
1452 	.map_fd_put_ptr = bpf_map_fd_put_ptr,
1453 	.map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
1454 	.map_gen_lookup = array_of_map_gen_lookup,
1455 	.map_lookup_batch = generic_map_lookup_batch,
1456 	.map_update_batch = generic_map_update_batch,
1457 	.map_check_btf = map_check_no_btf,
1458 	.map_mem_usage = array_map_mem_usage,
1459 	.map_btf_id = &array_map_btf_ids[0],
1460 };
1461