xref: /linux-6.15/kernel/bpf/arraymap.c (revision 823707b6)
128fbcfa0SAlexei Starovoitov /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
228fbcfa0SAlexei Starovoitov  *
328fbcfa0SAlexei Starovoitov  * This program is free software; you can redistribute it and/or
428fbcfa0SAlexei Starovoitov  * modify it under the terms of version 2 of the GNU General Public
528fbcfa0SAlexei Starovoitov  * License as published by the Free Software Foundation.
628fbcfa0SAlexei Starovoitov  *
728fbcfa0SAlexei Starovoitov  * This program is distributed in the hope that it will be useful, but
828fbcfa0SAlexei Starovoitov  * WITHOUT ANY WARRANTY; without even the implied warranty of
928fbcfa0SAlexei Starovoitov  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1028fbcfa0SAlexei Starovoitov  * General Public License for more details.
1128fbcfa0SAlexei Starovoitov  */
1228fbcfa0SAlexei Starovoitov #include <linux/bpf.h>
1328fbcfa0SAlexei Starovoitov #include <linux/err.h>
1428fbcfa0SAlexei Starovoitov #include <linux/vmalloc.h>
1528fbcfa0SAlexei Starovoitov #include <linux/slab.h>
1628fbcfa0SAlexei Starovoitov #include <linux/mm.h>
1704fd61abSAlexei Starovoitov #include <linux/filter.h>
180cdf5640SDaniel Borkmann #include <linux/perf_event.h>
1928fbcfa0SAlexei Starovoitov 
20a10423b8SAlexei Starovoitov static void bpf_array_free_percpu(struct bpf_array *array)
21a10423b8SAlexei Starovoitov {
22a10423b8SAlexei Starovoitov 	int i;
23a10423b8SAlexei Starovoitov 
24a10423b8SAlexei Starovoitov 	for (i = 0; i < array->map.max_entries; i++)
25a10423b8SAlexei Starovoitov 		free_percpu(array->pptrs[i]);
26a10423b8SAlexei Starovoitov }
27a10423b8SAlexei Starovoitov 
28a10423b8SAlexei Starovoitov static int bpf_array_alloc_percpu(struct bpf_array *array)
29a10423b8SAlexei Starovoitov {
30a10423b8SAlexei Starovoitov 	void __percpu *ptr;
31a10423b8SAlexei Starovoitov 	int i;
32a10423b8SAlexei Starovoitov 
33a10423b8SAlexei Starovoitov 	for (i = 0; i < array->map.max_entries; i++) {
34a10423b8SAlexei Starovoitov 		ptr = __alloc_percpu_gfp(array->elem_size, 8,
35a10423b8SAlexei Starovoitov 					 GFP_USER | __GFP_NOWARN);
36a10423b8SAlexei Starovoitov 		if (!ptr) {
37a10423b8SAlexei Starovoitov 			bpf_array_free_percpu(array);
38a10423b8SAlexei Starovoitov 			return -ENOMEM;
39a10423b8SAlexei Starovoitov 		}
40a10423b8SAlexei Starovoitov 		array->pptrs[i] = ptr;
41a10423b8SAlexei Starovoitov 	}
42a10423b8SAlexei Starovoitov 
43a10423b8SAlexei Starovoitov 	return 0;
44a10423b8SAlexei Starovoitov }
45a10423b8SAlexei Starovoitov 
4628fbcfa0SAlexei Starovoitov /* Called from syscall */
4728fbcfa0SAlexei Starovoitov static struct bpf_map *array_map_alloc(union bpf_attr *attr)
4828fbcfa0SAlexei Starovoitov {
49a10423b8SAlexei Starovoitov 	bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
5028fbcfa0SAlexei Starovoitov 	struct bpf_array *array;
51a10423b8SAlexei Starovoitov 	u64 array_size;
52a10423b8SAlexei Starovoitov 	u32 elem_size;
5328fbcfa0SAlexei Starovoitov 
5428fbcfa0SAlexei Starovoitov 	/* check sanity of attributes */
5528fbcfa0SAlexei Starovoitov 	if (attr->max_entries == 0 || attr->key_size != 4 ||
56*823707b6SAlexei Starovoitov 	    attr->value_size == 0 || attr->map_flags)
5728fbcfa0SAlexei Starovoitov 		return ERR_PTR(-EINVAL);
5828fbcfa0SAlexei Starovoitov 
5901b3f521SAlexei Starovoitov 	if (attr->value_size >= 1 << (KMALLOC_SHIFT_MAX - 1))
6001b3f521SAlexei Starovoitov 		/* if value_size is bigger, the user space won't be able to
6101b3f521SAlexei Starovoitov 		 * access the elements.
6201b3f521SAlexei Starovoitov 		 */
6301b3f521SAlexei Starovoitov 		return ERR_PTR(-E2BIG);
6401b3f521SAlexei Starovoitov 
6528fbcfa0SAlexei Starovoitov 	elem_size = round_up(attr->value_size, 8);
6628fbcfa0SAlexei Starovoitov 
67a10423b8SAlexei Starovoitov 	array_size = sizeof(*array);
68a10423b8SAlexei Starovoitov 	if (percpu)
69a10423b8SAlexei Starovoitov 		array_size += (u64) attr->max_entries * sizeof(void *);
70a10423b8SAlexei Starovoitov 	else
71a10423b8SAlexei Starovoitov 		array_size += (u64) attr->max_entries * elem_size;
72a10423b8SAlexei Starovoitov 
73a10423b8SAlexei Starovoitov 	/* make sure there is no u32 overflow later in round_up() */
74a10423b8SAlexei Starovoitov 	if (array_size >= U32_MAX - PAGE_SIZE)
75daaf427cSAlexei Starovoitov 		return ERR_PTR(-ENOMEM);
76daaf427cSAlexei Starovoitov 
77daaf427cSAlexei Starovoitov 
7828fbcfa0SAlexei Starovoitov 	/* allocate all map elements and zero-initialize them */
79daaf427cSAlexei Starovoitov 	array = kzalloc(array_size, GFP_USER | __GFP_NOWARN);
8028fbcfa0SAlexei Starovoitov 	if (!array) {
81daaf427cSAlexei Starovoitov 		array = vzalloc(array_size);
8228fbcfa0SAlexei Starovoitov 		if (!array)
8328fbcfa0SAlexei Starovoitov 			return ERR_PTR(-ENOMEM);
8428fbcfa0SAlexei Starovoitov 	}
8528fbcfa0SAlexei Starovoitov 
8628fbcfa0SAlexei Starovoitov 	/* copy mandatory map attributes */
87a10423b8SAlexei Starovoitov 	array->map.map_type = attr->map_type;
8828fbcfa0SAlexei Starovoitov 	array->map.key_size = attr->key_size;
8928fbcfa0SAlexei Starovoitov 	array->map.value_size = attr->value_size;
9028fbcfa0SAlexei Starovoitov 	array->map.max_entries = attr->max_entries;
9128fbcfa0SAlexei Starovoitov 	array->elem_size = elem_size;
9228fbcfa0SAlexei Starovoitov 
93a10423b8SAlexei Starovoitov 	if (!percpu)
94a10423b8SAlexei Starovoitov 		goto out;
95a10423b8SAlexei Starovoitov 
96a10423b8SAlexei Starovoitov 	array_size += (u64) attr->max_entries * elem_size * num_possible_cpus();
97a10423b8SAlexei Starovoitov 
98a10423b8SAlexei Starovoitov 	if (array_size >= U32_MAX - PAGE_SIZE ||
99a10423b8SAlexei Starovoitov 	    elem_size > PCPU_MIN_UNIT_SIZE || bpf_array_alloc_percpu(array)) {
100a10423b8SAlexei Starovoitov 		kvfree(array);
101a10423b8SAlexei Starovoitov 		return ERR_PTR(-ENOMEM);
102a10423b8SAlexei Starovoitov 	}
103a10423b8SAlexei Starovoitov out:
104a10423b8SAlexei Starovoitov 	array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
105a10423b8SAlexei Starovoitov 
10628fbcfa0SAlexei Starovoitov 	return &array->map;
10728fbcfa0SAlexei Starovoitov }
10828fbcfa0SAlexei Starovoitov 
10928fbcfa0SAlexei Starovoitov /* Called from syscall or from eBPF program */
11028fbcfa0SAlexei Starovoitov static void *array_map_lookup_elem(struct bpf_map *map, void *key)
11128fbcfa0SAlexei Starovoitov {
11228fbcfa0SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
11328fbcfa0SAlexei Starovoitov 	u32 index = *(u32 *)key;
11428fbcfa0SAlexei Starovoitov 
115a10423b8SAlexei Starovoitov 	if (unlikely(index >= array->map.max_entries))
11628fbcfa0SAlexei Starovoitov 		return NULL;
11728fbcfa0SAlexei Starovoitov 
11828fbcfa0SAlexei Starovoitov 	return array->value + array->elem_size * index;
11928fbcfa0SAlexei Starovoitov }
12028fbcfa0SAlexei Starovoitov 
121a10423b8SAlexei Starovoitov /* Called from eBPF program */
122a10423b8SAlexei Starovoitov static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
123a10423b8SAlexei Starovoitov {
124a10423b8SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
125a10423b8SAlexei Starovoitov 	u32 index = *(u32 *)key;
126a10423b8SAlexei Starovoitov 
127a10423b8SAlexei Starovoitov 	if (unlikely(index >= array->map.max_entries))
128a10423b8SAlexei Starovoitov 		return NULL;
129a10423b8SAlexei Starovoitov 
130a10423b8SAlexei Starovoitov 	return this_cpu_ptr(array->pptrs[index]);
131a10423b8SAlexei Starovoitov }
132a10423b8SAlexei Starovoitov 
13315a07b33SAlexei Starovoitov int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
13415a07b33SAlexei Starovoitov {
13515a07b33SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
13615a07b33SAlexei Starovoitov 	u32 index = *(u32 *)key;
13715a07b33SAlexei Starovoitov 	void __percpu *pptr;
13815a07b33SAlexei Starovoitov 	int cpu, off = 0;
13915a07b33SAlexei Starovoitov 	u32 size;
14015a07b33SAlexei Starovoitov 
14115a07b33SAlexei Starovoitov 	if (unlikely(index >= array->map.max_entries))
14215a07b33SAlexei Starovoitov 		return -ENOENT;
14315a07b33SAlexei Starovoitov 
14415a07b33SAlexei Starovoitov 	/* per_cpu areas are zero-filled and bpf programs can only
14515a07b33SAlexei Starovoitov 	 * access 'value_size' of them, so copying rounded areas
14615a07b33SAlexei Starovoitov 	 * will not leak any kernel data
14715a07b33SAlexei Starovoitov 	 */
14815a07b33SAlexei Starovoitov 	size = round_up(map->value_size, 8);
14915a07b33SAlexei Starovoitov 	rcu_read_lock();
15015a07b33SAlexei Starovoitov 	pptr = array->pptrs[index];
15115a07b33SAlexei Starovoitov 	for_each_possible_cpu(cpu) {
15215a07b33SAlexei Starovoitov 		bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
15315a07b33SAlexei Starovoitov 		off += size;
15415a07b33SAlexei Starovoitov 	}
15515a07b33SAlexei Starovoitov 	rcu_read_unlock();
15615a07b33SAlexei Starovoitov 	return 0;
15715a07b33SAlexei Starovoitov }
15815a07b33SAlexei Starovoitov 
15928fbcfa0SAlexei Starovoitov /* Called from syscall */
16028fbcfa0SAlexei Starovoitov static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
16128fbcfa0SAlexei Starovoitov {
16228fbcfa0SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
16328fbcfa0SAlexei Starovoitov 	u32 index = *(u32 *)key;
16428fbcfa0SAlexei Starovoitov 	u32 *next = (u32 *)next_key;
16528fbcfa0SAlexei Starovoitov 
16628fbcfa0SAlexei Starovoitov 	if (index >= array->map.max_entries) {
16728fbcfa0SAlexei Starovoitov 		*next = 0;
16828fbcfa0SAlexei Starovoitov 		return 0;
16928fbcfa0SAlexei Starovoitov 	}
17028fbcfa0SAlexei Starovoitov 
17128fbcfa0SAlexei Starovoitov 	if (index == array->map.max_entries - 1)
17228fbcfa0SAlexei Starovoitov 		return -ENOENT;
17328fbcfa0SAlexei Starovoitov 
17428fbcfa0SAlexei Starovoitov 	*next = index + 1;
17528fbcfa0SAlexei Starovoitov 	return 0;
17628fbcfa0SAlexei Starovoitov }
17728fbcfa0SAlexei Starovoitov 
17828fbcfa0SAlexei Starovoitov /* Called from syscall or from eBPF program */
17928fbcfa0SAlexei Starovoitov static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
18028fbcfa0SAlexei Starovoitov 				 u64 map_flags)
18128fbcfa0SAlexei Starovoitov {
18228fbcfa0SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
18328fbcfa0SAlexei Starovoitov 	u32 index = *(u32 *)key;
18428fbcfa0SAlexei Starovoitov 
185a10423b8SAlexei Starovoitov 	if (unlikely(map_flags > BPF_EXIST))
18628fbcfa0SAlexei Starovoitov 		/* unknown flags */
18728fbcfa0SAlexei Starovoitov 		return -EINVAL;
18828fbcfa0SAlexei Starovoitov 
189a10423b8SAlexei Starovoitov 	if (unlikely(index >= array->map.max_entries))
19028fbcfa0SAlexei Starovoitov 		/* all elements were pre-allocated, cannot insert a new one */
19128fbcfa0SAlexei Starovoitov 		return -E2BIG;
19228fbcfa0SAlexei Starovoitov 
193a10423b8SAlexei Starovoitov 	if (unlikely(map_flags == BPF_NOEXIST))
194daaf427cSAlexei Starovoitov 		/* all elements already exist */
19528fbcfa0SAlexei Starovoitov 		return -EEXIST;
19628fbcfa0SAlexei Starovoitov 
197a10423b8SAlexei Starovoitov 	if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
198a10423b8SAlexei Starovoitov 		memcpy(this_cpu_ptr(array->pptrs[index]),
199a10423b8SAlexei Starovoitov 		       value, map->value_size);
200a10423b8SAlexei Starovoitov 	else
201a10423b8SAlexei Starovoitov 		memcpy(array->value + array->elem_size * index,
202a10423b8SAlexei Starovoitov 		       value, map->value_size);
20328fbcfa0SAlexei Starovoitov 	return 0;
20428fbcfa0SAlexei Starovoitov }
20528fbcfa0SAlexei Starovoitov 
20615a07b33SAlexei Starovoitov int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
20715a07b33SAlexei Starovoitov 			    u64 map_flags)
20815a07b33SAlexei Starovoitov {
20915a07b33SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
21015a07b33SAlexei Starovoitov 	u32 index = *(u32 *)key;
21115a07b33SAlexei Starovoitov 	void __percpu *pptr;
21215a07b33SAlexei Starovoitov 	int cpu, off = 0;
21315a07b33SAlexei Starovoitov 	u32 size;
21415a07b33SAlexei Starovoitov 
21515a07b33SAlexei Starovoitov 	if (unlikely(map_flags > BPF_EXIST))
21615a07b33SAlexei Starovoitov 		/* unknown flags */
21715a07b33SAlexei Starovoitov 		return -EINVAL;
21815a07b33SAlexei Starovoitov 
21915a07b33SAlexei Starovoitov 	if (unlikely(index >= array->map.max_entries))
22015a07b33SAlexei Starovoitov 		/* all elements were pre-allocated, cannot insert a new one */
22115a07b33SAlexei Starovoitov 		return -E2BIG;
22215a07b33SAlexei Starovoitov 
22315a07b33SAlexei Starovoitov 	if (unlikely(map_flags == BPF_NOEXIST))
22415a07b33SAlexei Starovoitov 		/* all elements already exist */
22515a07b33SAlexei Starovoitov 		return -EEXIST;
22615a07b33SAlexei Starovoitov 
22715a07b33SAlexei Starovoitov 	/* the user space will provide round_up(value_size, 8) bytes that
22815a07b33SAlexei Starovoitov 	 * will be copied into per-cpu area. bpf programs can only access
22915a07b33SAlexei Starovoitov 	 * value_size of it. During lookup the same extra bytes will be
23015a07b33SAlexei Starovoitov 	 * returned or zeros which were zero-filled by percpu_alloc,
23115a07b33SAlexei Starovoitov 	 * so no kernel data leaks possible
23215a07b33SAlexei Starovoitov 	 */
23315a07b33SAlexei Starovoitov 	size = round_up(map->value_size, 8);
23415a07b33SAlexei Starovoitov 	rcu_read_lock();
23515a07b33SAlexei Starovoitov 	pptr = array->pptrs[index];
23615a07b33SAlexei Starovoitov 	for_each_possible_cpu(cpu) {
23715a07b33SAlexei Starovoitov 		bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
23815a07b33SAlexei Starovoitov 		off += size;
23915a07b33SAlexei Starovoitov 	}
24015a07b33SAlexei Starovoitov 	rcu_read_unlock();
24115a07b33SAlexei Starovoitov 	return 0;
24215a07b33SAlexei Starovoitov }
24315a07b33SAlexei Starovoitov 
24428fbcfa0SAlexei Starovoitov /* Called from syscall or from eBPF program */
24528fbcfa0SAlexei Starovoitov static int array_map_delete_elem(struct bpf_map *map, void *key)
24628fbcfa0SAlexei Starovoitov {
24728fbcfa0SAlexei Starovoitov 	return -EINVAL;
24828fbcfa0SAlexei Starovoitov }
24928fbcfa0SAlexei Starovoitov 
25028fbcfa0SAlexei Starovoitov /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
25128fbcfa0SAlexei Starovoitov static void array_map_free(struct bpf_map *map)
25228fbcfa0SAlexei Starovoitov {
25328fbcfa0SAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
25428fbcfa0SAlexei Starovoitov 
25528fbcfa0SAlexei Starovoitov 	/* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
25628fbcfa0SAlexei Starovoitov 	 * so the programs (can be more than one that used this map) were
25728fbcfa0SAlexei Starovoitov 	 * disconnected from events. Wait for outstanding programs to complete
25828fbcfa0SAlexei Starovoitov 	 * and free the array
25928fbcfa0SAlexei Starovoitov 	 */
26028fbcfa0SAlexei Starovoitov 	synchronize_rcu();
26128fbcfa0SAlexei Starovoitov 
262a10423b8SAlexei Starovoitov 	if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
263a10423b8SAlexei Starovoitov 		bpf_array_free_percpu(array);
264a10423b8SAlexei Starovoitov 
26528fbcfa0SAlexei Starovoitov 	kvfree(array);
26628fbcfa0SAlexei Starovoitov }
26728fbcfa0SAlexei Starovoitov 
268a2c83fffSDaniel Borkmann static const struct bpf_map_ops array_ops = {
26928fbcfa0SAlexei Starovoitov 	.map_alloc = array_map_alloc,
27028fbcfa0SAlexei Starovoitov 	.map_free = array_map_free,
27128fbcfa0SAlexei Starovoitov 	.map_get_next_key = array_map_get_next_key,
27228fbcfa0SAlexei Starovoitov 	.map_lookup_elem = array_map_lookup_elem,
27328fbcfa0SAlexei Starovoitov 	.map_update_elem = array_map_update_elem,
27428fbcfa0SAlexei Starovoitov 	.map_delete_elem = array_map_delete_elem,
27528fbcfa0SAlexei Starovoitov };
27628fbcfa0SAlexei Starovoitov 
277a2c83fffSDaniel Borkmann static struct bpf_map_type_list array_type __read_mostly = {
27828fbcfa0SAlexei Starovoitov 	.ops = &array_ops,
27928fbcfa0SAlexei Starovoitov 	.type = BPF_MAP_TYPE_ARRAY,
28028fbcfa0SAlexei Starovoitov };
28128fbcfa0SAlexei Starovoitov 
282a10423b8SAlexei Starovoitov static const struct bpf_map_ops percpu_array_ops = {
283a10423b8SAlexei Starovoitov 	.map_alloc = array_map_alloc,
284a10423b8SAlexei Starovoitov 	.map_free = array_map_free,
285a10423b8SAlexei Starovoitov 	.map_get_next_key = array_map_get_next_key,
286a10423b8SAlexei Starovoitov 	.map_lookup_elem = percpu_array_map_lookup_elem,
287a10423b8SAlexei Starovoitov 	.map_update_elem = array_map_update_elem,
288a10423b8SAlexei Starovoitov 	.map_delete_elem = array_map_delete_elem,
289a10423b8SAlexei Starovoitov };
290a10423b8SAlexei Starovoitov 
291a10423b8SAlexei Starovoitov static struct bpf_map_type_list percpu_array_type __read_mostly = {
292a10423b8SAlexei Starovoitov 	.ops = &percpu_array_ops,
293a10423b8SAlexei Starovoitov 	.type = BPF_MAP_TYPE_PERCPU_ARRAY,
294a10423b8SAlexei Starovoitov };
295a10423b8SAlexei Starovoitov 
29628fbcfa0SAlexei Starovoitov static int __init register_array_map(void)
29728fbcfa0SAlexei Starovoitov {
298a2c83fffSDaniel Borkmann 	bpf_register_map_type(&array_type);
299a10423b8SAlexei Starovoitov 	bpf_register_map_type(&percpu_array_type);
30028fbcfa0SAlexei Starovoitov 	return 0;
30128fbcfa0SAlexei Starovoitov }
30228fbcfa0SAlexei Starovoitov late_initcall(register_array_map);
30304fd61abSAlexei Starovoitov 
3042a36f0b9SWang Nan static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
30504fd61abSAlexei Starovoitov {
3062a36f0b9SWang Nan 	/* only file descriptors can be stored in this type of map */
30704fd61abSAlexei Starovoitov 	if (attr->value_size != sizeof(u32))
30804fd61abSAlexei Starovoitov 		return ERR_PTR(-EINVAL);
30904fd61abSAlexei Starovoitov 	return array_map_alloc(attr);
31004fd61abSAlexei Starovoitov }
31104fd61abSAlexei Starovoitov 
3122a36f0b9SWang Nan static void fd_array_map_free(struct bpf_map *map)
31304fd61abSAlexei Starovoitov {
31404fd61abSAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
31504fd61abSAlexei Starovoitov 	int i;
31604fd61abSAlexei Starovoitov 
31704fd61abSAlexei Starovoitov 	synchronize_rcu();
31804fd61abSAlexei Starovoitov 
31904fd61abSAlexei Starovoitov 	/* make sure it's empty */
32004fd61abSAlexei Starovoitov 	for (i = 0; i < array->map.max_entries; i++)
3212a36f0b9SWang Nan 		BUG_ON(array->ptrs[i] != NULL);
32204fd61abSAlexei Starovoitov 	kvfree(array);
32304fd61abSAlexei Starovoitov }
32404fd61abSAlexei Starovoitov 
3252a36f0b9SWang Nan static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
32604fd61abSAlexei Starovoitov {
32704fd61abSAlexei Starovoitov 	return NULL;
32804fd61abSAlexei Starovoitov }
32904fd61abSAlexei Starovoitov 
33004fd61abSAlexei Starovoitov /* only called from syscall */
3312a36f0b9SWang Nan static int fd_array_map_update_elem(struct bpf_map *map, void *key,
33204fd61abSAlexei Starovoitov 				    void *value, u64 map_flags)
33304fd61abSAlexei Starovoitov {
33404fd61abSAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
3352a36f0b9SWang Nan 	void *new_ptr, *old_ptr;
33604fd61abSAlexei Starovoitov 	u32 index = *(u32 *)key, ufd;
33704fd61abSAlexei Starovoitov 
33804fd61abSAlexei Starovoitov 	if (map_flags != BPF_ANY)
33904fd61abSAlexei Starovoitov 		return -EINVAL;
34004fd61abSAlexei Starovoitov 
34104fd61abSAlexei Starovoitov 	if (index >= array->map.max_entries)
34204fd61abSAlexei Starovoitov 		return -E2BIG;
34304fd61abSAlexei Starovoitov 
34404fd61abSAlexei Starovoitov 	ufd = *(u32 *)value;
3452a36f0b9SWang Nan 	new_ptr = map->ops->map_fd_get_ptr(map, ufd);
3462a36f0b9SWang Nan 	if (IS_ERR(new_ptr))
3472a36f0b9SWang Nan 		return PTR_ERR(new_ptr);
34804fd61abSAlexei Starovoitov 
3492a36f0b9SWang Nan 	old_ptr = xchg(array->ptrs + index, new_ptr);
3502a36f0b9SWang Nan 	if (old_ptr)
3512a36f0b9SWang Nan 		map->ops->map_fd_put_ptr(old_ptr);
35204fd61abSAlexei Starovoitov 
35304fd61abSAlexei Starovoitov 	return 0;
35404fd61abSAlexei Starovoitov }
35504fd61abSAlexei Starovoitov 
3562a36f0b9SWang Nan static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
35704fd61abSAlexei Starovoitov {
35804fd61abSAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
3592a36f0b9SWang Nan 	void *old_ptr;
36004fd61abSAlexei Starovoitov 	u32 index = *(u32 *)key;
36104fd61abSAlexei Starovoitov 
36204fd61abSAlexei Starovoitov 	if (index >= array->map.max_entries)
36304fd61abSAlexei Starovoitov 		return -E2BIG;
36404fd61abSAlexei Starovoitov 
3652a36f0b9SWang Nan 	old_ptr = xchg(array->ptrs + index, NULL);
3662a36f0b9SWang Nan 	if (old_ptr) {
3672a36f0b9SWang Nan 		map->ops->map_fd_put_ptr(old_ptr);
36804fd61abSAlexei Starovoitov 		return 0;
36904fd61abSAlexei Starovoitov 	} else {
37004fd61abSAlexei Starovoitov 		return -ENOENT;
37104fd61abSAlexei Starovoitov 	}
37204fd61abSAlexei Starovoitov }
37304fd61abSAlexei Starovoitov 
3742a36f0b9SWang Nan static void *prog_fd_array_get_ptr(struct bpf_map *map, int fd)
3752a36f0b9SWang Nan {
3762a36f0b9SWang Nan 	struct bpf_array *array = container_of(map, struct bpf_array, map);
3772a36f0b9SWang Nan 	struct bpf_prog *prog = bpf_prog_get(fd);
3782a36f0b9SWang Nan 	if (IS_ERR(prog))
3792a36f0b9SWang Nan 		return prog;
3802a36f0b9SWang Nan 
3812a36f0b9SWang Nan 	if (!bpf_prog_array_compatible(array, prog)) {
3822a36f0b9SWang Nan 		bpf_prog_put(prog);
3832a36f0b9SWang Nan 		return ERR_PTR(-EINVAL);
3842a36f0b9SWang Nan 	}
3852a36f0b9SWang Nan 	return prog;
3862a36f0b9SWang Nan }
3872a36f0b9SWang Nan 
3882a36f0b9SWang Nan static void prog_fd_array_put_ptr(void *ptr)
3892a36f0b9SWang Nan {
3902a36f0b9SWang Nan 	struct bpf_prog *prog = ptr;
3912a36f0b9SWang Nan 
3922a36f0b9SWang Nan 	bpf_prog_put_rcu(prog);
3932a36f0b9SWang Nan }
3942a36f0b9SWang Nan 
39504fd61abSAlexei Starovoitov /* decrement refcnt of all bpf_progs that are stored in this map */
3962a36f0b9SWang Nan void bpf_fd_array_map_clear(struct bpf_map *map)
39704fd61abSAlexei Starovoitov {
39804fd61abSAlexei Starovoitov 	struct bpf_array *array = container_of(map, struct bpf_array, map);
39904fd61abSAlexei Starovoitov 	int i;
40004fd61abSAlexei Starovoitov 
40104fd61abSAlexei Starovoitov 	for (i = 0; i < array->map.max_entries; i++)
4022a36f0b9SWang Nan 		fd_array_map_delete_elem(map, &i);
40304fd61abSAlexei Starovoitov }
40404fd61abSAlexei Starovoitov 
40504fd61abSAlexei Starovoitov static const struct bpf_map_ops prog_array_ops = {
4062a36f0b9SWang Nan 	.map_alloc = fd_array_map_alloc,
4072a36f0b9SWang Nan 	.map_free = fd_array_map_free,
40804fd61abSAlexei Starovoitov 	.map_get_next_key = array_map_get_next_key,
4092a36f0b9SWang Nan 	.map_lookup_elem = fd_array_map_lookup_elem,
4102a36f0b9SWang Nan 	.map_update_elem = fd_array_map_update_elem,
4112a36f0b9SWang Nan 	.map_delete_elem = fd_array_map_delete_elem,
4122a36f0b9SWang Nan 	.map_fd_get_ptr = prog_fd_array_get_ptr,
4132a36f0b9SWang Nan 	.map_fd_put_ptr = prog_fd_array_put_ptr,
41404fd61abSAlexei Starovoitov };
41504fd61abSAlexei Starovoitov 
41604fd61abSAlexei Starovoitov static struct bpf_map_type_list prog_array_type __read_mostly = {
41704fd61abSAlexei Starovoitov 	.ops = &prog_array_ops,
41804fd61abSAlexei Starovoitov 	.type = BPF_MAP_TYPE_PROG_ARRAY,
41904fd61abSAlexei Starovoitov };
42004fd61abSAlexei Starovoitov 
42104fd61abSAlexei Starovoitov static int __init register_prog_array_map(void)
42204fd61abSAlexei Starovoitov {
42304fd61abSAlexei Starovoitov 	bpf_register_map_type(&prog_array_type);
42404fd61abSAlexei Starovoitov 	return 0;
42504fd61abSAlexei Starovoitov }
42604fd61abSAlexei Starovoitov late_initcall(register_prog_array_map);
427ea317b26SKaixu Xia 
428ea317b26SKaixu Xia static void perf_event_array_map_free(struct bpf_map *map)
429ea317b26SKaixu Xia {
430ea317b26SKaixu Xia 	bpf_fd_array_map_clear(map);
431ea317b26SKaixu Xia 	fd_array_map_free(map);
432ea317b26SKaixu Xia }
433ea317b26SKaixu Xia 
434ea317b26SKaixu Xia static void *perf_event_fd_array_get_ptr(struct bpf_map *map, int fd)
435ea317b26SKaixu Xia {
436ea317b26SKaixu Xia 	struct perf_event *event;
437ea317b26SKaixu Xia 	const struct perf_event_attr *attr;
438e03e7ee3SAlexei Starovoitov 	struct file *file;
439ea317b26SKaixu Xia 
440e03e7ee3SAlexei Starovoitov 	file = perf_event_get(fd);
441e03e7ee3SAlexei Starovoitov 	if (IS_ERR(file))
442e03e7ee3SAlexei Starovoitov 		return file;
443e03e7ee3SAlexei Starovoitov 
444e03e7ee3SAlexei Starovoitov 	event = file->private_data;
445ea317b26SKaixu Xia 
446ea317b26SKaixu Xia 	attr = perf_event_attrs(event);
447ea317b26SKaixu Xia 	if (IS_ERR(attr))
44862544ce8SAlexei Starovoitov 		goto err;
449ea317b26SKaixu Xia 
45062544ce8SAlexei Starovoitov 	if (attr->inherit)
45162544ce8SAlexei Starovoitov 		goto err;
45262544ce8SAlexei Starovoitov 
45362544ce8SAlexei Starovoitov 	if (attr->type == PERF_TYPE_RAW)
454e03e7ee3SAlexei Starovoitov 		return file;
45562544ce8SAlexei Starovoitov 
45662544ce8SAlexei Starovoitov 	if (attr->type == PERF_TYPE_HARDWARE)
457e03e7ee3SAlexei Starovoitov 		return file;
45862544ce8SAlexei Starovoitov 
45962544ce8SAlexei Starovoitov 	if (attr->type == PERF_TYPE_SOFTWARE &&
46062544ce8SAlexei Starovoitov 	    attr->config == PERF_COUNT_SW_BPF_OUTPUT)
461e03e7ee3SAlexei Starovoitov 		return file;
46262544ce8SAlexei Starovoitov err:
463e03e7ee3SAlexei Starovoitov 	fput(file);
464ea317b26SKaixu Xia 	return ERR_PTR(-EINVAL);
465ea317b26SKaixu Xia }
466ea317b26SKaixu Xia 
467ea317b26SKaixu Xia static void perf_event_fd_array_put_ptr(void *ptr)
468ea317b26SKaixu Xia {
469e03e7ee3SAlexei Starovoitov 	fput((struct file *)ptr);
470ea317b26SKaixu Xia }
471ea317b26SKaixu Xia 
472ea317b26SKaixu Xia static const struct bpf_map_ops perf_event_array_ops = {
473ea317b26SKaixu Xia 	.map_alloc = fd_array_map_alloc,
474ea317b26SKaixu Xia 	.map_free = perf_event_array_map_free,
475ea317b26SKaixu Xia 	.map_get_next_key = array_map_get_next_key,
476ea317b26SKaixu Xia 	.map_lookup_elem = fd_array_map_lookup_elem,
477ea317b26SKaixu Xia 	.map_update_elem = fd_array_map_update_elem,
478ea317b26SKaixu Xia 	.map_delete_elem = fd_array_map_delete_elem,
479ea317b26SKaixu Xia 	.map_fd_get_ptr = perf_event_fd_array_get_ptr,
480ea317b26SKaixu Xia 	.map_fd_put_ptr = perf_event_fd_array_put_ptr,
481ea317b26SKaixu Xia };
482ea317b26SKaixu Xia 
483ea317b26SKaixu Xia static struct bpf_map_type_list perf_event_array_type __read_mostly = {
484ea317b26SKaixu Xia 	.ops = &perf_event_array_ops,
485ea317b26SKaixu Xia 	.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
486ea317b26SKaixu Xia };
487ea317b26SKaixu Xia 
488ea317b26SKaixu Xia static int __init register_perf_event_array_map(void)
489ea317b26SKaixu Xia {
490ea317b26SKaixu Xia 	bpf_register_map_type(&perf_event_array_type);
491ea317b26SKaixu Xia 	return 0;
492ea317b26SKaixu Xia }
493ea317b26SKaixu Xia late_initcall(register_perf_event_array_map);
494