blob: ab94d304a634a7ea96e2e9c5b806bfe8dfb6dd90 [file] [log] [blame]
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -08001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07002 * Copyright (c) 2016,2017 Facebook
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -08003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#include <linux/bpf.h>
14#include <linux/err.h>
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080015#include <linux/slab.h>
16#include <linux/mm.h>
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -070017#include <linux/filter.h>
Daniel Borkmann0cdf56402015-10-02 18:42:00 +020018#include <linux/perf_event.h>
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080019
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070020#include "map_in_map.h"
21
Chenbo Feng6e71b042017-10-18 13:00:22 -070022#define ARRAY_CREATE_FLAG_MASK \
23 (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
24
Alexei Starovoitova10423b2016-02-01 22:39:54 -080025static void bpf_array_free_percpu(struct bpf_array *array)
26{
27 int i;
28
29 for (i = 0; i < array->map.max_entries; i++)
30 free_percpu(array->pptrs[i]);
31}
32
33static 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 = __alloc_percpu_gfp(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 }
47
48 return 0;
49}
50
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080051/* Called from syscall */
52static struct bpf_map *array_map_alloc(union bpf_attr *attr)
53{
Alexei Starovoitova10423b2016-02-01 22:39:54 -080054 bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
Martin KaFai Lau96eabe72017-08-18 11:28:00 -070055 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitovb2157392018-01-07 17:33:02 -080056 u32 elem_size, index_mask, max_entries;
57 bool unpriv = !capable(CAP_SYS_ADMIN);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080058 struct bpf_array *array;
Daniel Borkmannbbeb6e42018-01-10 23:25:05 +010059 u64 array_size, mask64;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080060
61 /* check sanity of attributes */
62 if (attr->max_entries == 0 || attr->key_size != 4 ||
Chenbo Feng6e71b042017-10-18 13:00:22 -070063 attr->value_size == 0 ||
64 attr->map_flags & ~ARRAY_CREATE_FLAG_MASK ||
Martin KaFai Lau96eabe72017-08-18 11:28:00 -070065 (percpu && numa_node != NUMA_NO_NODE))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080066 return ERR_PTR(-EINVAL);
67
Michal Hocko7984c272017-01-10 16:57:30 -080068 if (attr->value_size > KMALLOC_MAX_SIZE)
Alexei Starovoitov01b3f522015-11-29 16:59:35 -080069 /* if value_size is bigger, the user space won't be able to
70 * access the elements.
71 */
72 return ERR_PTR(-E2BIG);
73
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080074 elem_size = round_up(attr->value_size, 8);
75
Alexei Starovoitovb2157392018-01-07 17:33:02 -080076 max_entries = attr->max_entries;
Alexei Starovoitovb2157392018-01-07 17:33:02 -080077
Daniel Borkmannbbeb6e42018-01-10 23:25:05 +010078 /* On 32 bit archs roundup_pow_of_two() with max_entries that has
79 * upper most bit set in u32 space is undefined behavior due to
80 * resulting 1U << 32, so do it manually here in u64 space.
81 */
82 mask64 = fls_long(max_entries - 1);
83 mask64 = 1ULL << mask64;
84 mask64 -= 1;
85
86 index_mask = mask64;
87 if (unpriv) {
Alexei Starovoitovb2157392018-01-07 17:33:02 -080088 /* round up array size to nearest power of 2,
89 * since cpu will speculate within index_mask limits
90 */
91 max_entries = index_mask + 1;
Daniel Borkmannbbeb6e42018-01-10 23:25:05 +010092 /* Check for overflows. */
93 if (max_entries < attr->max_entries)
94 return ERR_PTR(-E2BIG);
95 }
Alexei Starovoitovb2157392018-01-07 17:33:02 -080096
Alexei Starovoitova10423b2016-02-01 22:39:54 -080097 array_size = sizeof(*array);
98 if (percpu)
Alexei Starovoitovb2157392018-01-07 17:33:02 -080099 array_size += (u64) max_entries * sizeof(void *);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800100 else
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800101 array_size += (u64) max_entries * elem_size;
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800102
103 /* make sure there is no u32 overflow later in round_up() */
104 if (array_size >= U32_MAX - PAGE_SIZE)
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800105 return ERR_PTR(-ENOMEM);
106
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800107 /* allocate all map elements and zero-initialize them */
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700108 array = bpf_map_area_alloc(array_size, numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100109 if (!array)
110 return ERR_PTR(-ENOMEM);
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800111 array->index_mask = index_mask;
112 array->map.unpriv_array = unpriv;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800113
114 /* copy mandatory map attributes */
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800115 array->map.map_type = attr->map_type;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800116 array->map.key_size = attr->key_size;
117 array->map.value_size = attr->value_size;
118 array->map.max_entries = attr->max_entries;
Daniel Borkmanna3163382017-05-25 01:05:08 +0200119 array->map.map_flags = attr->map_flags;
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700120 array->map.numa_node = numa_node;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800121 array->elem_size = elem_size;
122
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800123 if (!percpu)
124 goto out;
125
126 array_size += (u64) attr->max_entries * elem_size * num_possible_cpus();
127
128 if (array_size >= U32_MAX - PAGE_SIZE ||
Daniel Borkmannbc6d5032017-10-17 16:55:54 +0200129 bpf_array_alloc_percpu(array)) {
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100130 bpf_map_area_free(array);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800131 return ERR_PTR(-ENOMEM);
132 }
133out:
134 array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
135
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800136 return &array->map;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800137}
138
139/* Called from syscall or from eBPF program */
140static void *array_map_lookup_elem(struct bpf_map *map, void *key)
141{
142 struct bpf_array *array = container_of(map, struct bpf_array, map);
143 u32 index = *(u32 *)key;
144
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800145 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800146 return NULL;
147
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800148 return array->value + array->elem_size * (index & array->index_mask);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800149}
150
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700151/* emit BPF instructions equivalent to C code of array_map_lookup_elem() */
152static u32 array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
153{
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800154 struct bpf_array *array = container_of(map, struct bpf_array, map);
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700155 struct bpf_insn *insn = insn_buf;
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700156 u32 elem_size = round_up(map->value_size, 8);
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700157 const int ret = BPF_REG_0;
158 const int map_ptr = BPF_REG_1;
159 const int index = BPF_REG_2;
160
161 *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
162 *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800163 if (map->unpriv_array) {
164 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 4);
165 *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
166 } else {
167 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 3);
168 }
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700169
170 if (is_power_of_2(elem_size)) {
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700171 *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
172 } else {
173 *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
174 }
175 *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
176 *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
177 *insn++ = BPF_MOV64_IMM(ret, 0);
178 return insn - insn_buf;
179}
180
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800181/* Called from eBPF program */
182static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
183{
184 struct bpf_array *array = container_of(map, struct bpf_array, map);
185 u32 index = *(u32 *)key;
186
187 if (unlikely(index >= array->map.max_entries))
188 return NULL;
189
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800190 return this_cpu_ptr(array->pptrs[index & array->index_mask]);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800191}
192
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800193int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
194{
195 struct bpf_array *array = container_of(map, struct bpf_array, map);
196 u32 index = *(u32 *)key;
197 void __percpu *pptr;
198 int cpu, off = 0;
199 u32 size;
200
201 if (unlikely(index >= array->map.max_entries))
202 return -ENOENT;
203
204 /* per_cpu areas are zero-filled and bpf programs can only
205 * access 'value_size' of them, so copying rounded areas
206 * will not leak any kernel data
207 */
208 size = round_up(map->value_size, 8);
209 rcu_read_lock();
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800210 pptr = array->pptrs[index & array->index_mask];
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800211 for_each_possible_cpu(cpu) {
212 bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
213 off += size;
214 }
215 rcu_read_unlock();
216 return 0;
217}
218
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800219/* Called from syscall */
220static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
221{
222 struct bpf_array *array = container_of(map, struct bpf_array, map);
Teng Qin8fe45922017-04-24 19:00:37 -0700223 u32 index = key ? *(u32 *)key : U32_MAX;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800224 u32 *next = (u32 *)next_key;
225
226 if (index >= array->map.max_entries) {
227 *next = 0;
228 return 0;
229 }
230
231 if (index == array->map.max_entries - 1)
232 return -ENOENT;
233
234 *next = index + 1;
235 return 0;
236}
237
238/* Called from syscall or from eBPF program */
239static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
240 u64 map_flags)
241{
242 struct bpf_array *array = container_of(map, struct bpf_array, map);
243 u32 index = *(u32 *)key;
244
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800245 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800246 /* unknown flags */
247 return -EINVAL;
248
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800249 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800250 /* all elements were pre-allocated, cannot insert a new one */
251 return -E2BIG;
252
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800253 if (unlikely(map_flags == BPF_NOEXIST))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800254 /* all elements already exist */
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800255 return -EEXIST;
256
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800257 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800258 memcpy(this_cpu_ptr(array->pptrs[index & array->index_mask]),
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800259 value, map->value_size);
260 else
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800261 memcpy(array->value +
262 array->elem_size * (index & array->index_mask),
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800263 value, map->value_size);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800264 return 0;
265}
266
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800267int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
268 u64 map_flags)
269{
270 struct bpf_array *array = container_of(map, struct bpf_array, map);
271 u32 index = *(u32 *)key;
272 void __percpu *pptr;
273 int cpu, off = 0;
274 u32 size;
275
276 if (unlikely(map_flags > BPF_EXIST))
277 /* unknown flags */
278 return -EINVAL;
279
280 if (unlikely(index >= array->map.max_entries))
281 /* all elements were pre-allocated, cannot insert a new one */
282 return -E2BIG;
283
284 if (unlikely(map_flags == BPF_NOEXIST))
285 /* all elements already exist */
286 return -EEXIST;
287
288 /* the user space will provide round_up(value_size, 8) bytes that
289 * will be copied into per-cpu area. bpf programs can only access
290 * value_size of it. During lookup the same extra bytes will be
291 * returned or zeros which were zero-filled by percpu_alloc,
292 * so no kernel data leaks possible
293 */
294 size = round_up(map->value_size, 8);
295 rcu_read_lock();
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800296 pptr = array->pptrs[index & array->index_mask];
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800297 for_each_possible_cpu(cpu) {
298 bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
299 off += size;
300 }
301 rcu_read_unlock();
302 return 0;
303}
304
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800305/* Called from syscall or from eBPF program */
306static int array_map_delete_elem(struct bpf_map *map, void *key)
307{
308 return -EINVAL;
309}
310
311/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
312static void array_map_free(struct bpf_map *map)
313{
314 struct bpf_array *array = container_of(map, struct bpf_array, map);
315
316 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
317 * so the programs (can be more than one that used this map) were
318 * disconnected from events. Wait for outstanding programs to complete
319 * and free the array
320 */
321 synchronize_rcu();
322
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800323 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
324 bpf_array_free_percpu(array);
325
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100326 bpf_map_area_free(array);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800327}
328
Johannes Berg40077e02017-04-11 15:34:58 +0200329const struct bpf_map_ops array_map_ops = {
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800330 .map_alloc = array_map_alloc,
331 .map_free = array_map_free,
332 .map_get_next_key = array_map_get_next_key,
333 .map_lookup_elem = array_map_lookup_elem,
334 .map_update_elem = array_map_update_elem,
335 .map_delete_elem = array_map_delete_elem,
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700336 .map_gen_lookup = array_map_gen_lookup,
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800337};
338
Johannes Berg40077e02017-04-11 15:34:58 +0200339const struct bpf_map_ops percpu_array_map_ops = {
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800340 .map_alloc = array_map_alloc,
341 .map_free = array_map_free,
342 .map_get_next_key = array_map_get_next_key,
343 .map_lookup_elem = percpu_array_map_lookup_elem,
344 .map_update_elem = array_map_update_elem,
345 .map_delete_elem = array_map_delete_elem,
346};
347
Wang Nan2a36f0b2015-08-06 07:02:33 +0000348static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700349{
Wang Nan2a36f0b2015-08-06 07:02:33 +0000350 /* only file descriptors can be stored in this type of map */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700351 if (attr->value_size != sizeof(u32))
352 return ERR_PTR(-EINVAL);
353 return array_map_alloc(attr);
354}
355
Wang Nan2a36f0b2015-08-06 07:02:33 +0000356static void fd_array_map_free(struct bpf_map *map)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700357{
358 struct bpf_array *array = container_of(map, struct bpf_array, map);
359 int i;
360
361 synchronize_rcu();
362
363 /* make sure it's empty */
364 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000365 BUG_ON(array->ptrs[i] != NULL);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100366
367 bpf_map_area_free(array);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700368}
369
Wang Nan2a36f0b2015-08-06 07:02:33 +0000370static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700371{
372 return NULL;
373}
374
375/* only called from syscall */
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700376int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
377{
378 void **elem, *ptr;
379 int ret = 0;
380
381 if (!map->ops->map_fd_sys_lookup_elem)
382 return -ENOTSUPP;
383
384 rcu_read_lock();
385 elem = array_map_lookup_elem(map, key);
386 if (elem && (ptr = READ_ONCE(*elem)))
387 *value = map->ops->map_fd_sys_lookup_elem(ptr);
388 else
389 ret = -ENOENT;
390 rcu_read_unlock();
391
392 return ret;
393}
394
395/* only called from syscall */
Daniel Borkmannd056a782016-06-15 22:47:13 +0200396int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
397 void *key, void *value, u64 map_flags)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700398{
399 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000400 void *new_ptr, *old_ptr;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700401 u32 index = *(u32 *)key, ufd;
402
403 if (map_flags != BPF_ANY)
404 return -EINVAL;
405
406 if (index >= array->map.max_entries)
407 return -E2BIG;
408
409 ufd = *(u32 *)value;
Daniel Borkmannd056a782016-06-15 22:47:13 +0200410 new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000411 if (IS_ERR(new_ptr))
412 return PTR_ERR(new_ptr);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700413
Wang Nan2a36f0b2015-08-06 07:02:33 +0000414 old_ptr = xchg(array->ptrs + index, new_ptr);
415 if (old_ptr)
416 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700417
418 return 0;
419}
420
Wang Nan2a36f0b2015-08-06 07:02:33 +0000421static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700422{
423 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000424 void *old_ptr;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700425 u32 index = *(u32 *)key;
426
427 if (index >= array->map.max_entries)
428 return -E2BIG;
429
Wang Nan2a36f0b2015-08-06 07:02:33 +0000430 old_ptr = xchg(array->ptrs + index, NULL);
431 if (old_ptr) {
432 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700433 return 0;
434 } else {
435 return -ENOENT;
436 }
437}
438
Daniel Borkmannd056a782016-06-15 22:47:13 +0200439static void *prog_fd_array_get_ptr(struct bpf_map *map,
440 struct file *map_file, int fd)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000441{
442 struct bpf_array *array = container_of(map, struct bpf_array, map);
443 struct bpf_prog *prog = bpf_prog_get(fd);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200444
Wang Nan2a36f0b2015-08-06 07:02:33 +0000445 if (IS_ERR(prog))
446 return prog;
447
448 if (!bpf_prog_array_compatible(array, prog)) {
449 bpf_prog_put(prog);
450 return ERR_PTR(-EINVAL);
451 }
Daniel Borkmannd056a782016-06-15 22:47:13 +0200452
Wang Nan2a36f0b2015-08-06 07:02:33 +0000453 return prog;
454}
455
456static void prog_fd_array_put_ptr(void *ptr)
457{
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200458 bpf_prog_put(ptr);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000459}
460
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700461static u32 prog_fd_array_sys_lookup_elem(void *ptr)
462{
463 return ((struct bpf_prog *)ptr)->aux->id;
464}
465
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700466/* decrement refcnt of all bpf_progs that are stored in this map */
Wang Nan2a36f0b2015-08-06 07:02:33 +0000467void bpf_fd_array_map_clear(struct bpf_map *map)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700468{
469 struct bpf_array *array = container_of(map, struct bpf_array, map);
470 int i;
471
472 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000473 fd_array_map_delete_elem(map, &i);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700474}
475
Johannes Berg40077e02017-04-11 15:34:58 +0200476const struct bpf_map_ops prog_array_map_ops = {
Wang Nan2a36f0b2015-08-06 07:02:33 +0000477 .map_alloc = fd_array_map_alloc,
478 .map_free = fd_array_map_free,
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700479 .map_get_next_key = array_map_get_next_key,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000480 .map_lookup_elem = fd_array_map_lookup_elem,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000481 .map_delete_elem = fd_array_map_delete_elem,
482 .map_fd_get_ptr = prog_fd_array_get_ptr,
483 .map_fd_put_ptr = prog_fd_array_put_ptr,
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700484 .map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem,
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700485};
486
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200487static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
488 struct file *map_file)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000489{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200490 struct bpf_event_entry *ee;
491
Daniel Borkmann858d68f2016-07-16 01:15:55 +0200492 ee = kzalloc(sizeof(*ee), GFP_ATOMIC);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200493 if (ee) {
494 ee->event = perf_file->private_data;
495 ee->perf_file = perf_file;
496 ee->map_file = map_file;
497 }
498
499 return ee;
500}
501
502static void __bpf_event_entry_free(struct rcu_head *rcu)
503{
504 struct bpf_event_entry *ee;
505
506 ee = container_of(rcu, struct bpf_event_entry, rcu);
507 fput(ee->perf_file);
508 kfree(ee);
509}
510
511static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
512{
513 call_rcu(&ee->rcu, __bpf_event_entry_free);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000514}
515
Daniel Borkmannd056a782016-06-15 22:47:13 +0200516static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
517 struct file *map_file, int fd)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000518{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200519 struct bpf_event_entry *ee;
520 struct perf_event *event;
521 struct file *perf_file;
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700522 u64 value;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000523
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200524 perf_file = perf_event_get(fd);
525 if (IS_ERR(perf_file))
526 return perf_file;
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -0800527
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700528 ee = ERR_PTR(-EOPNOTSUPP);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200529 event = perf_file->private_data;
Yonghong Song97562632017-10-05 09:19:19 -0700530 if (perf_event_read_local(event, &value, NULL, NULL) == -EOPNOTSUPP)
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200531 goto err_out;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000532
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700533 ee = bpf_event_entry_gen(perf_file, map_file);
534 if (ee)
535 return ee;
536 ee = ERR_PTR(-ENOMEM);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200537err_out:
538 fput(perf_file);
539 return ee;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000540}
541
542static void perf_event_fd_array_put_ptr(void *ptr)
543{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200544 bpf_event_entry_free_rcu(ptr);
545}
546
547static void perf_event_fd_array_release(struct bpf_map *map,
548 struct file *map_file)
549{
550 struct bpf_array *array = container_of(map, struct bpf_array, map);
551 struct bpf_event_entry *ee;
552 int i;
553
554 rcu_read_lock();
555 for (i = 0; i < array->map.max_entries; i++) {
556 ee = READ_ONCE(array->ptrs[i]);
557 if (ee && ee->map_file == map_file)
558 fd_array_map_delete_elem(map, &i);
559 }
560 rcu_read_unlock();
Kaixu Xiaea317b22015-08-06 07:02:34 +0000561}
562
Johannes Berg40077e02017-04-11 15:34:58 +0200563const struct bpf_map_ops perf_event_array_map_ops = {
Kaixu Xiaea317b22015-08-06 07:02:34 +0000564 .map_alloc = fd_array_map_alloc,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200565 .map_free = fd_array_map_free,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000566 .map_get_next_key = array_map_get_next_key,
567 .map_lookup_elem = fd_array_map_lookup_elem,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000568 .map_delete_elem = fd_array_map_delete_elem,
569 .map_fd_get_ptr = perf_event_fd_array_get_ptr,
570 .map_fd_put_ptr = perf_event_fd_array_put_ptr,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200571 .map_release = perf_event_fd_array_release,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000572};
573
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700574#ifdef CONFIG_CGROUPS
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700575static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
576 struct file *map_file /* not used */,
577 int fd)
578{
579 return cgroup_get_from_fd(fd);
580}
581
582static void cgroup_fd_array_put_ptr(void *ptr)
583{
584 /* cgroup_put free cgrp after a rcu grace period */
585 cgroup_put(ptr);
586}
587
588static void cgroup_fd_array_free(struct bpf_map *map)
589{
590 bpf_fd_array_map_clear(map);
591 fd_array_map_free(map);
592}
593
Johannes Berg40077e02017-04-11 15:34:58 +0200594const struct bpf_map_ops cgroup_array_map_ops = {
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700595 .map_alloc = fd_array_map_alloc,
596 .map_free = cgroup_fd_array_free,
597 .map_get_next_key = array_map_get_next_key,
598 .map_lookup_elem = fd_array_map_lookup_elem,
599 .map_delete_elem = fd_array_map_delete_elem,
600 .map_fd_get_ptr = cgroup_fd_array_get_ptr,
601 .map_fd_put_ptr = cgroup_fd_array_put_ptr,
602};
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700603#endif
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700604
605static struct bpf_map *array_of_map_alloc(union bpf_attr *attr)
606{
607 struct bpf_map *map, *inner_map_meta;
608
609 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
610 if (IS_ERR(inner_map_meta))
611 return inner_map_meta;
612
613 map = fd_array_map_alloc(attr);
614 if (IS_ERR(map)) {
615 bpf_map_meta_free(inner_map_meta);
616 return map;
617 }
618
619 map->inner_map_meta = inner_map_meta;
620
621 return map;
622}
623
624static void array_of_map_free(struct bpf_map *map)
625{
626 /* map->inner_map_meta is only accessed by syscall which
627 * is protected by fdget/fdput.
628 */
629 bpf_map_meta_free(map->inner_map_meta);
630 bpf_fd_array_map_clear(map);
631 fd_array_map_free(map);
632}
633
634static void *array_of_map_lookup_elem(struct bpf_map *map, void *key)
635{
636 struct bpf_map **inner_map = array_map_lookup_elem(map, key);
637
638 if (!inner_map)
639 return NULL;
640
641 return READ_ONCE(*inner_map);
642}
643
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +0200644static u32 array_of_map_gen_lookup(struct bpf_map *map,
645 struct bpf_insn *insn_buf)
646{
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800647 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +0200648 u32 elem_size = round_up(map->value_size, 8);
649 struct bpf_insn *insn = insn_buf;
650 const int ret = BPF_REG_0;
651 const int map_ptr = BPF_REG_1;
652 const int index = BPF_REG_2;
653
654 *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
655 *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800656 if (map->unpriv_array) {
657 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 6);
658 *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
659 } else {
660 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 5);
661 }
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +0200662 if (is_power_of_2(elem_size))
663 *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
664 else
665 *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
666 *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
667 *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
668 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
669 *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
670 *insn++ = BPF_MOV64_IMM(ret, 0);
671
672 return insn - insn_buf;
673}
674
Johannes Berg40077e02017-04-11 15:34:58 +0200675const struct bpf_map_ops array_of_maps_map_ops = {
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700676 .map_alloc = array_of_map_alloc,
677 .map_free = array_of_map_free,
678 .map_get_next_key = array_map_get_next_key,
679 .map_lookup_elem = array_of_map_lookup_elem,
680 .map_delete_elem = fd_array_map_delete_elem,
681 .map_fd_get_ptr = bpf_map_fd_get_ptr,
682 .map_fd_put_ptr = bpf_map_fd_put_ptr,
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700683 .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +0200684 .map_gen_lookup = array_of_map_gen_lookup,
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700685};