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