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