blob: 02a189339381c00092f1891d57eea659f8333521 [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>
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070014#include <linux/btf.h>
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080015#include <linux/err.h>
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080016#include <linux/slab.h>
17#include <linux/mm.h>
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -070018#include <linux/filter.h>
Daniel Borkmann0cdf56402015-10-02 18:42:00 +020019#include <linux/perf_event.h>
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070020#include <uapi/linux/btf.h>
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080021
Martin KaFai Lau56f668d2017-03-22 10:00:33 -070022#include "map_in_map.h"
23
Chenbo Feng6e71b042017-10-18 13:00:22 -070024#define ARRAY_CREATE_FLAG_MASK \
25 (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
26
Alexei Starovoitova10423b2016-02-01 22:39:54 -080027static void bpf_array_free_percpu(struct bpf_array *array)
28{
29 int i;
30
Eric Dumazet32fff232018-02-22 08:33:24 -080031 for (i = 0; i < array->map.max_entries; i++) {
Alexei Starovoitova10423b2016-02-01 22:39:54 -080032 free_percpu(array->pptrs[i]);
Eric Dumazet32fff232018-02-22 08:33:24 -080033 cond_resched();
34 }
Alexei Starovoitova10423b2016-02-01 22:39:54 -080035}
36
37static int bpf_array_alloc_percpu(struct bpf_array *array)
38{
39 void __percpu *ptr;
40 int i;
41
42 for (i = 0; i < array->map.max_entries; i++) {
43 ptr = __alloc_percpu_gfp(array->elem_size, 8,
44 GFP_USER | __GFP_NOWARN);
45 if (!ptr) {
46 bpf_array_free_percpu(array);
47 return -ENOMEM;
48 }
49 array->pptrs[i] = ptr;
Eric Dumazet32fff232018-02-22 08:33:24 -080050 cond_resched();
Alexei Starovoitova10423b2016-02-01 22:39:54 -080051 }
52
53 return 0;
54}
55
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080056/* Called from syscall */
Jakub Kicinskiad460612018-01-17 19:13:25 -080057static int array_map_alloc_check(union bpf_attr *attr)
58{
59 bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
60 int numa_node = bpf_map_attr_numa_node(attr);
61
62 /* check sanity of attributes */
63 if (attr->max_entries == 0 || attr->key_size != 4 ||
64 attr->value_size == 0 ||
65 attr->map_flags & ~ARRAY_CREATE_FLAG_MASK ||
66 (percpu && numa_node != NUMA_NO_NODE))
67 return -EINVAL;
68
69 if (attr->value_size > KMALLOC_MAX_SIZE)
70 /* if value_size is bigger, the user space won't be able to
71 * access the elements.
72 */
73 return -E2BIG;
74
75 return 0;
76}
77
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080078static struct bpf_map *array_map_alloc(union bpf_attr *attr)
79{
Alexei Starovoitova10423b2016-02-01 22:39:54 -080080 bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
Daniel Borkmann9c2d63b2018-02-16 01:10:29 +010081 int ret, numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitovb2157392018-01-07 17:33:02 -080082 u32 elem_size, index_mask, max_entries;
83 bool unpriv = !capable(CAP_SYS_ADMIN);
Daniel Borkmann9c2d63b2018-02-16 01:10:29 +010084 u64 cost, array_size, mask64;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080085 struct bpf_array *array;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080086
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -080087 elem_size = round_up(attr->value_size, 8);
88
Alexei Starovoitovb2157392018-01-07 17:33:02 -080089 max_entries = attr->max_entries;
Alexei Starovoitovb2157392018-01-07 17:33:02 -080090
Daniel Borkmannbbeb6e42018-01-10 23:25:05 +010091 /* On 32 bit archs roundup_pow_of_two() with max_entries that has
92 * upper most bit set in u32 space is undefined behavior due to
93 * resulting 1U << 32, so do it manually here in u64 space.
94 */
95 mask64 = fls_long(max_entries - 1);
96 mask64 = 1ULL << mask64;
97 mask64 -= 1;
98
99 index_mask = mask64;
100 if (unpriv) {
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800101 /* round up array size to nearest power of 2,
102 * since cpu will speculate within index_mask limits
103 */
104 max_entries = index_mask + 1;
Daniel Borkmannbbeb6e42018-01-10 23:25:05 +0100105 /* Check for overflows. */
106 if (max_entries < attr->max_entries)
107 return ERR_PTR(-E2BIG);
108 }
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800109
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800110 array_size = sizeof(*array);
111 if (percpu)
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800112 array_size += (u64) max_entries * sizeof(void *);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800113 else
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800114 array_size += (u64) max_entries * elem_size;
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800115
116 /* make sure there is no u32 overflow later in round_up() */
Daniel Borkmann9c2d63b2018-02-16 01:10:29 +0100117 cost = array_size;
118 if (cost >= U32_MAX - PAGE_SIZE)
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800119 return ERR_PTR(-ENOMEM);
Daniel Borkmann9c2d63b2018-02-16 01:10:29 +0100120 if (percpu) {
121 cost += (u64)attr->max_entries * elem_size * num_possible_cpus();
122 if (cost >= U32_MAX - PAGE_SIZE)
123 return ERR_PTR(-ENOMEM);
124 }
125 cost = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
126
127 ret = bpf_map_precharge_memlock(cost);
128 if (ret < 0)
129 return ERR_PTR(ret);
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800130
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800131 /* allocate all map elements and zero-initialize them */
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700132 array = bpf_map_area_alloc(array_size, numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100133 if (!array)
134 return ERR_PTR(-ENOMEM);
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800135 array->index_mask = index_mask;
136 array->map.unpriv_array = unpriv;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800137
138 /* copy mandatory map attributes */
Jakub Kicinski32852642018-01-17 19:13:26 -0800139 bpf_map_init_from_attr(&array->map, attr);
Daniel Borkmann9c2d63b2018-02-16 01:10:29 +0100140 array->map.pages = cost;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800141 array->elem_size = elem_size;
142
Daniel Borkmann9c2d63b2018-02-16 01:10:29 +0100143 if (percpu && bpf_array_alloc_percpu(array)) {
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100144 bpf_map_area_free(array);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800145 return ERR_PTR(-ENOMEM);
146 }
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800147
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800148 return &array->map;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800149}
150
151/* Called from syscall or from eBPF program */
152static void *array_map_lookup_elem(struct bpf_map *map, void *key)
153{
154 struct bpf_array *array = container_of(map, struct bpf_array, map);
155 u32 index = *(u32 *)key;
156
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800157 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800158 return NULL;
159
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800160 return array->value + array->elem_size * (index & array->index_mask);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800161}
162
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700163/* emit BPF instructions equivalent to C code of array_map_lookup_elem() */
164static u32 array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
165{
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800166 struct bpf_array *array = container_of(map, struct bpf_array, map);
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700167 struct bpf_insn *insn = insn_buf;
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700168 u32 elem_size = round_up(map->value_size, 8);
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700169 const int ret = BPF_REG_0;
170 const int map_ptr = BPF_REG_1;
171 const int index = BPF_REG_2;
172
173 *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
174 *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800175 if (map->unpriv_array) {
176 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 4);
177 *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
178 } else {
179 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 3);
180 }
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700181
182 if (is_power_of_2(elem_size)) {
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700183 *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
184 } else {
185 *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
186 }
187 *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
188 *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
189 *insn++ = BPF_MOV64_IMM(ret, 0);
190 return insn - insn_buf;
191}
192
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800193/* Called from eBPF program */
194static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
195{
196 struct bpf_array *array = container_of(map, struct bpf_array, map);
197 u32 index = *(u32 *)key;
198
199 if (unlikely(index >= array->map.max_entries))
200 return NULL;
201
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800202 return this_cpu_ptr(array->pptrs[index & array->index_mask]);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800203}
204
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800205int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
206{
207 struct bpf_array *array = container_of(map, struct bpf_array, map);
208 u32 index = *(u32 *)key;
209 void __percpu *pptr;
210 int cpu, off = 0;
211 u32 size;
212
213 if (unlikely(index >= array->map.max_entries))
214 return -ENOENT;
215
216 /* per_cpu areas are zero-filled and bpf programs can only
217 * access 'value_size' of them, so copying rounded areas
218 * will not leak any kernel data
219 */
220 size = round_up(map->value_size, 8);
221 rcu_read_lock();
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800222 pptr = array->pptrs[index & array->index_mask];
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800223 for_each_possible_cpu(cpu) {
224 bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
225 off += size;
226 }
227 rcu_read_unlock();
228 return 0;
229}
230
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800231/* Called from syscall */
232static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
233{
234 struct bpf_array *array = container_of(map, struct bpf_array, map);
Teng Qin8fe45922017-04-24 19:00:37 -0700235 u32 index = key ? *(u32 *)key : U32_MAX;
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800236 u32 *next = (u32 *)next_key;
237
238 if (index >= array->map.max_entries) {
239 *next = 0;
240 return 0;
241 }
242
243 if (index == array->map.max_entries - 1)
244 return -ENOENT;
245
246 *next = index + 1;
247 return 0;
248}
249
250/* Called from syscall or from eBPF program */
251static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
252 u64 map_flags)
253{
254 struct bpf_array *array = container_of(map, struct bpf_array, map);
255 u32 index = *(u32 *)key;
256
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800257 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800258 /* unknown flags */
259 return -EINVAL;
260
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800261 if (unlikely(index >= array->map.max_entries))
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800262 /* all elements were pre-allocated, cannot insert a new one */
263 return -E2BIG;
264
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800265 if (unlikely(map_flags == BPF_NOEXIST))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800266 /* all elements already exist */
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800267 return -EEXIST;
268
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800269 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800270 memcpy(this_cpu_ptr(array->pptrs[index & array->index_mask]),
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800271 value, map->value_size);
272 else
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800273 memcpy(array->value +
274 array->elem_size * (index & array->index_mask),
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800275 value, map->value_size);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800276 return 0;
277}
278
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800279int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
280 u64 map_flags)
281{
282 struct bpf_array *array = container_of(map, struct bpf_array, map);
283 u32 index = *(u32 *)key;
284 void __percpu *pptr;
285 int cpu, off = 0;
286 u32 size;
287
288 if (unlikely(map_flags > BPF_EXIST))
289 /* unknown flags */
290 return -EINVAL;
291
292 if (unlikely(index >= array->map.max_entries))
293 /* all elements were pre-allocated, cannot insert a new one */
294 return -E2BIG;
295
296 if (unlikely(map_flags == BPF_NOEXIST))
297 /* all elements already exist */
298 return -EEXIST;
299
300 /* the user space will provide round_up(value_size, 8) bytes that
301 * will be copied into per-cpu area. bpf programs can only access
302 * value_size of it. During lookup the same extra bytes will be
303 * returned or zeros which were zero-filled by percpu_alloc,
304 * so no kernel data leaks possible
305 */
306 size = round_up(map->value_size, 8);
307 rcu_read_lock();
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800308 pptr = array->pptrs[index & array->index_mask];
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800309 for_each_possible_cpu(cpu) {
310 bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
311 off += size;
312 }
313 rcu_read_unlock();
314 return 0;
315}
316
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800317/* Called from syscall or from eBPF program */
318static int array_map_delete_elem(struct bpf_map *map, void *key)
319{
320 return -EINVAL;
321}
322
323/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
324static void array_map_free(struct bpf_map *map)
325{
326 struct bpf_array *array = container_of(map, struct bpf_array, map);
327
328 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
329 * so the programs (can be more than one that used this map) were
330 * disconnected from events. Wait for outstanding programs to complete
331 * and free the array
332 */
333 synchronize_rcu();
334
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800335 if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
336 bpf_array_free_percpu(array);
337
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100338 bpf_map_area_free(array);
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800339}
340
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700341static void array_map_seq_show_elem(struct bpf_map *map, void *key,
342 struct seq_file *m)
343{
344 void *value;
345
346 rcu_read_lock();
347
348 value = array_map_lookup_elem(map, key);
349 if (!value) {
350 rcu_read_unlock();
351 return;
352 }
353
354 seq_printf(m, "%u: ", *(u32 *)key);
355 btf_type_seq_show(map->btf, map->btf_value_id, value, m);
356 seq_puts(m, "\n");
357
358 rcu_read_unlock();
359}
360
361static int array_map_check_btf(const struct bpf_map *map, const struct btf *btf,
362 u32 btf_key_id, u32 btf_value_id)
363{
364 const struct btf_type *key_type, *value_type;
365 u32 key_size, value_size;
366 u32 int_data;
367
368 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
369 if (!key_type || BTF_INFO_KIND(key_type->info) != BTF_KIND_INT)
370 return -EINVAL;
371
372 int_data = *(u32 *)(key_type + 1);
373 /* bpf array can only take a u32 key. This check makes
374 * sure that the btf matches the attr used during map_create.
375 */
376 if (BTF_INT_BITS(int_data) != 32 || key_size != 4 ||
377 BTF_INT_OFFSET(int_data))
378 return -EINVAL;
379
380 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
381 if (!value_type || value_size > map->value_size)
382 return -EINVAL;
383
384 return 0;
385}
386
Johannes Berg40077e02017-04-11 15:34:58 +0200387const struct bpf_map_ops array_map_ops = {
Jakub Kicinskiad460612018-01-17 19:13:25 -0800388 .map_alloc_check = array_map_alloc_check,
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800389 .map_alloc = array_map_alloc,
390 .map_free = array_map_free,
391 .map_get_next_key = array_map_get_next_key,
392 .map_lookup_elem = array_map_lookup_elem,
393 .map_update_elem = array_map_update_elem,
394 .map_delete_elem = array_map_delete_elem,
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700395 .map_gen_lookup = array_map_gen_lookup,
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700396 .map_seq_show_elem = array_map_seq_show_elem,
397 .map_check_btf = array_map_check_btf,
Alexei Starovoitov28fbcfa2014-11-13 17:36:46 -0800398};
399
Johannes Berg40077e02017-04-11 15:34:58 +0200400const struct bpf_map_ops percpu_array_map_ops = {
Jakub Kicinskiad460612018-01-17 19:13:25 -0800401 .map_alloc_check = array_map_alloc_check,
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800402 .map_alloc = array_map_alloc,
403 .map_free = array_map_free,
404 .map_get_next_key = array_map_get_next_key,
405 .map_lookup_elem = percpu_array_map_lookup_elem,
406 .map_update_elem = array_map_update_elem,
407 .map_delete_elem = array_map_delete_elem,
408};
409
Jakub Kicinskiad460612018-01-17 19:13:25 -0800410static int fd_array_map_alloc_check(union bpf_attr *attr)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700411{
Wang Nan2a36f0b2015-08-06 07:02:33 +0000412 /* only file descriptors can be stored in this type of map */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700413 if (attr->value_size != sizeof(u32))
Jakub Kicinskiad460612018-01-17 19:13:25 -0800414 return -EINVAL;
415 return array_map_alloc_check(attr);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700416}
417
Wang Nan2a36f0b2015-08-06 07:02:33 +0000418static void fd_array_map_free(struct bpf_map *map)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700419{
420 struct bpf_array *array = container_of(map, struct bpf_array, map);
421 int i;
422
423 synchronize_rcu();
424
425 /* make sure it's empty */
426 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000427 BUG_ON(array->ptrs[i] != NULL);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100428
429 bpf_map_area_free(array);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700430}
431
Wang Nan2a36f0b2015-08-06 07:02:33 +0000432static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700433{
434 return NULL;
435}
436
437/* only called from syscall */
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700438int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
439{
440 void **elem, *ptr;
441 int ret = 0;
442
443 if (!map->ops->map_fd_sys_lookup_elem)
444 return -ENOTSUPP;
445
446 rcu_read_lock();
447 elem = array_map_lookup_elem(map, key);
448 if (elem && (ptr = READ_ONCE(*elem)))
449 *value = map->ops->map_fd_sys_lookup_elem(ptr);
450 else
451 ret = -ENOENT;
452 rcu_read_unlock();
453
454 return ret;
455}
456
457/* only called from syscall */
Daniel Borkmannd056a782016-06-15 22:47:13 +0200458int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
459 void *key, void *value, u64 map_flags)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700460{
461 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000462 void *new_ptr, *old_ptr;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700463 u32 index = *(u32 *)key, ufd;
464
465 if (map_flags != BPF_ANY)
466 return -EINVAL;
467
468 if (index >= array->map.max_entries)
469 return -E2BIG;
470
471 ufd = *(u32 *)value;
Daniel Borkmannd056a782016-06-15 22:47:13 +0200472 new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000473 if (IS_ERR(new_ptr))
474 return PTR_ERR(new_ptr);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700475
Wang Nan2a36f0b2015-08-06 07:02:33 +0000476 old_ptr = xchg(array->ptrs + index, new_ptr);
477 if (old_ptr)
478 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700479
480 return 0;
481}
482
Wang Nan2a36f0b2015-08-06 07:02:33 +0000483static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700484{
485 struct bpf_array *array = container_of(map, struct bpf_array, map);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000486 void *old_ptr;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700487 u32 index = *(u32 *)key;
488
489 if (index >= array->map.max_entries)
490 return -E2BIG;
491
Wang Nan2a36f0b2015-08-06 07:02:33 +0000492 old_ptr = xchg(array->ptrs + index, NULL);
493 if (old_ptr) {
494 map->ops->map_fd_put_ptr(old_ptr);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700495 return 0;
496 } else {
497 return -ENOENT;
498 }
499}
500
Daniel Borkmannd056a782016-06-15 22:47:13 +0200501static void *prog_fd_array_get_ptr(struct bpf_map *map,
502 struct file *map_file, int fd)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000503{
504 struct bpf_array *array = container_of(map, struct bpf_array, map);
505 struct bpf_prog *prog = bpf_prog_get(fd);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200506
Wang Nan2a36f0b2015-08-06 07:02:33 +0000507 if (IS_ERR(prog))
508 return prog;
509
510 if (!bpf_prog_array_compatible(array, prog)) {
511 bpf_prog_put(prog);
512 return ERR_PTR(-EINVAL);
513 }
Daniel Borkmannd056a782016-06-15 22:47:13 +0200514
Wang Nan2a36f0b2015-08-06 07:02:33 +0000515 return prog;
516}
517
518static void prog_fd_array_put_ptr(void *ptr)
519{
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200520 bpf_prog_put(ptr);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000521}
522
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700523static u32 prog_fd_array_sys_lookup_elem(void *ptr)
524{
525 return ((struct bpf_prog *)ptr)->aux->id;
526}
527
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700528/* decrement refcnt of all bpf_progs that are stored in this map */
Wang Nan2a36f0b2015-08-06 07:02:33 +0000529void bpf_fd_array_map_clear(struct bpf_map *map)
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700530{
531 struct bpf_array *array = container_of(map, struct bpf_array, map);
532 int i;
533
534 for (i = 0; i < array->map.max_entries; i++)
Wang Nan2a36f0b2015-08-06 07:02:33 +0000535 fd_array_map_delete_elem(map, &i);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700536}
537
Johannes Berg40077e02017-04-11 15:34:58 +0200538const struct bpf_map_ops prog_array_map_ops = {
Jakub Kicinskiad460612018-01-17 19:13:25 -0800539 .map_alloc_check = fd_array_map_alloc_check,
540 .map_alloc = array_map_alloc,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000541 .map_free = fd_array_map_free,
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700542 .map_get_next_key = array_map_get_next_key,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000543 .map_lookup_elem = fd_array_map_lookup_elem,
Wang Nan2a36f0b2015-08-06 07:02:33 +0000544 .map_delete_elem = fd_array_map_delete_elem,
545 .map_fd_get_ptr = prog_fd_array_get_ptr,
546 .map_fd_put_ptr = prog_fd_array_put_ptr,
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700547 .map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem,
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700548};
549
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200550static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
551 struct file *map_file)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000552{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200553 struct bpf_event_entry *ee;
554
Daniel Borkmann858d68f2016-07-16 01:15:55 +0200555 ee = kzalloc(sizeof(*ee), GFP_ATOMIC);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200556 if (ee) {
557 ee->event = perf_file->private_data;
558 ee->perf_file = perf_file;
559 ee->map_file = map_file;
560 }
561
562 return ee;
563}
564
565static void __bpf_event_entry_free(struct rcu_head *rcu)
566{
567 struct bpf_event_entry *ee;
568
569 ee = container_of(rcu, struct bpf_event_entry, rcu);
570 fput(ee->perf_file);
571 kfree(ee);
572}
573
574static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
575{
576 call_rcu(&ee->rcu, __bpf_event_entry_free);
Kaixu Xiaea317b22015-08-06 07:02:34 +0000577}
578
Daniel Borkmannd056a782016-06-15 22:47:13 +0200579static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
580 struct file *map_file, int fd)
Kaixu Xiaea317b22015-08-06 07:02:34 +0000581{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200582 struct bpf_event_entry *ee;
583 struct perf_event *event;
584 struct file *perf_file;
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700585 u64 value;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000586
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200587 perf_file = perf_event_get(fd);
588 if (IS_ERR(perf_file))
589 return perf_file;
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -0800590
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700591 ee = ERR_PTR(-EOPNOTSUPP);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200592 event = perf_file->private_data;
Yonghong Song97562632017-10-05 09:19:19 -0700593 if (perf_event_read_local(event, &value, NULL, NULL) == -EOPNOTSUPP)
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200594 goto err_out;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000595
Alexei Starovoitovf91840a2017-06-02 21:03:52 -0700596 ee = bpf_event_entry_gen(perf_file, map_file);
597 if (ee)
598 return ee;
599 ee = ERR_PTR(-ENOMEM);
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200600err_out:
601 fput(perf_file);
602 return ee;
Kaixu Xiaea317b22015-08-06 07:02:34 +0000603}
604
605static void perf_event_fd_array_put_ptr(void *ptr)
606{
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200607 bpf_event_entry_free_rcu(ptr);
608}
609
610static void perf_event_fd_array_release(struct bpf_map *map,
611 struct file *map_file)
612{
613 struct bpf_array *array = container_of(map, struct bpf_array, map);
614 struct bpf_event_entry *ee;
615 int i;
616
617 rcu_read_lock();
618 for (i = 0; i < array->map.max_entries; i++) {
619 ee = READ_ONCE(array->ptrs[i]);
620 if (ee && ee->map_file == map_file)
621 fd_array_map_delete_elem(map, &i);
622 }
623 rcu_read_unlock();
Kaixu Xiaea317b22015-08-06 07:02:34 +0000624}
625
Johannes Berg40077e02017-04-11 15:34:58 +0200626const struct bpf_map_ops perf_event_array_map_ops = {
Jakub Kicinskiad460612018-01-17 19:13:25 -0800627 .map_alloc_check = fd_array_map_alloc_check,
628 .map_alloc = array_map_alloc,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200629 .map_free = fd_array_map_free,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000630 .map_get_next_key = array_map_get_next_key,
631 .map_lookup_elem = fd_array_map_lookup_elem,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000632 .map_delete_elem = fd_array_map_delete_elem,
633 .map_fd_get_ptr = perf_event_fd_array_get_ptr,
634 .map_fd_put_ptr = perf_event_fd_array_put_ptr,
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200635 .map_release = perf_event_fd_array_release,
Kaixu Xiaea317b22015-08-06 07:02:34 +0000636};
637
Sargun Dhillon60d20f92016-08-12 08:56:52 -0700638#ifdef CONFIG_CGROUPS
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700639static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
640 struct file *map_file /* not used */,
641 int fd)
642{
643 return cgroup_get_from_fd(fd);
644}
645
646static void cgroup_fd_array_put_ptr(void *ptr)
647{
648 /* cgroup_put free cgrp after a rcu grace period */
649 cgroup_put(ptr);
650}
651
652static void cgroup_fd_array_free(struct bpf_map *map)
653{
654 bpf_fd_array_map_clear(map);
655 fd_array_map_free(map);
656}
657
Johannes Berg40077e02017-04-11 15:34:58 +0200658const struct bpf_map_ops cgroup_array_map_ops = {
Jakub Kicinskiad460612018-01-17 19:13:25 -0800659 .map_alloc_check = fd_array_map_alloc_check,
660 .map_alloc = array_map_alloc,
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700661 .map_free = cgroup_fd_array_free,
662 .map_get_next_key = array_map_get_next_key,
663 .map_lookup_elem = fd_array_map_lookup_elem,
664 .map_delete_elem = fd_array_map_delete_elem,
665 .map_fd_get_ptr = cgroup_fd_array_get_ptr,
666 .map_fd_put_ptr = cgroup_fd_array_put_ptr,
667};
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700668#endif
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700669
670static struct bpf_map *array_of_map_alloc(union bpf_attr *attr)
671{
672 struct bpf_map *map, *inner_map_meta;
673
674 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
675 if (IS_ERR(inner_map_meta))
676 return inner_map_meta;
677
Jakub Kicinskiad460612018-01-17 19:13:25 -0800678 map = array_map_alloc(attr);
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700679 if (IS_ERR(map)) {
680 bpf_map_meta_free(inner_map_meta);
681 return map;
682 }
683
684 map->inner_map_meta = inner_map_meta;
685
686 return map;
687}
688
689static void array_of_map_free(struct bpf_map *map)
690{
691 /* map->inner_map_meta is only accessed by syscall which
692 * is protected by fdget/fdput.
693 */
694 bpf_map_meta_free(map->inner_map_meta);
695 bpf_fd_array_map_clear(map);
696 fd_array_map_free(map);
697}
698
699static void *array_of_map_lookup_elem(struct bpf_map *map, void *key)
700{
701 struct bpf_map **inner_map = array_map_lookup_elem(map, key);
702
703 if (!inner_map)
704 return NULL;
705
706 return READ_ONCE(*inner_map);
707}
708
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +0200709static u32 array_of_map_gen_lookup(struct bpf_map *map,
710 struct bpf_insn *insn_buf)
711{
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800712 struct bpf_array *array = container_of(map, struct bpf_array, map);
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +0200713 u32 elem_size = round_up(map->value_size, 8);
714 struct bpf_insn *insn = insn_buf;
715 const int ret = BPF_REG_0;
716 const int map_ptr = BPF_REG_1;
717 const int index = BPF_REG_2;
718
719 *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
720 *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800721 if (map->unpriv_array) {
722 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 6);
723 *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
724 } else {
725 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 5);
726 }
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +0200727 if (is_power_of_2(elem_size))
728 *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
729 else
730 *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
731 *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
732 *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
733 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
734 *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
735 *insn++ = BPF_MOV64_IMM(ret, 0);
736
737 return insn - insn_buf;
738}
739
Johannes Berg40077e02017-04-11 15:34:58 +0200740const struct bpf_map_ops array_of_maps_map_ops = {
Jakub Kicinskiad460612018-01-17 19:13:25 -0800741 .map_alloc_check = fd_array_map_alloc_check,
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700742 .map_alloc = array_of_map_alloc,
743 .map_free = array_of_map_free,
744 .map_get_next_key = array_map_get_next_key,
745 .map_lookup_elem = array_of_map_lookup_elem,
746 .map_delete_elem = fd_array_map_delete_elem,
747 .map_fd_get_ptr = bpf_map_fd_get_ptr,
748 .map_fd_put_ptr = bpf_map_fd_put_ptr,
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700749 .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
Daniel Borkmann7b0c2a02017-08-19 03:12:46 +0200750 .map_gen_lookup = array_of_map_gen_lookup,
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700751};