]> asedeno.scripts.mit.edu Git - linux.git/blob - kernel/bpf/arraymap.c
bpf: Fix and simplifications on inline map lookup
[linux.git] / kernel / bpf / arraymap.c
1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2  * Copyright (c) 2016,2017 Facebook
3  *
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>
15 #include <linux/slab.h>
16 #include <linux/mm.h>
17 #include <linux/filter.h>
18 #include <linux/perf_event.h>
19
20 static void bpf_array_free_percpu(struct bpf_array *array)
21 {
22         int i;
23
24         for (i = 0; i < array->map.max_entries; i++)
25                 free_percpu(array->pptrs[i]);
26 }
27
28 static int bpf_array_alloc_percpu(struct bpf_array *array)
29 {
30         void __percpu *ptr;
31         int i;
32
33         for (i = 0; i < array->map.max_entries; i++) {
34                 ptr = __alloc_percpu_gfp(array->elem_size, 8,
35                                          GFP_USER | __GFP_NOWARN);
36                 if (!ptr) {
37                         bpf_array_free_percpu(array);
38                         return -ENOMEM;
39                 }
40                 array->pptrs[i] = ptr;
41         }
42
43         return 0;
44 }
45
46 /* Called from syscall */
47 static struct bpf_map *array_map_alloc(union bpf_attr *attr)
48 {
49         bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
50         struct bpf_array *array;
51         u64 array_size;
52         u32 elem_size;
53
54         /* check sanity of attributes */
55         if (attr->max_entries == 0 || attr->key_size != 4 ||
56             attr->value_size == 0 || attr->map_flags)
57                 return ERR_PTR(-EINVAL);
58
59         if (attr->value_size > KMALLOC_MAX_SIZE)
60                 /* if value_size is bigger, the user space won't be able to
61                  * access the elements.
62                  */
63                 return ERR_PTR(-E2BIG);
64
65         elem_size = round_up(attr->value_size, 8);
66
67         array_size = sizeof(*array);
68         if (percpu)
69                 array_size += (u64) attr->max_entries * sizeof(void *);
70         else
71                 array_size += (u64) attr->max_entries * elem_size;
72
73         /* make sure there is no u32 overflow later in round_up() */
74         if (array_size >= U32_MAX - PAGE_SIZE)
75                 return ERR_PTR(-ENOMEM);
76
77         /* allocate all map elements and zero-initialize them */
78         array = bpf_map_area_alloc(array_size);
79         if (!array)
80                 return ERR_PTR(-ENOMEM);
81
82         /* copy mandatory map attributes */
83         array->map.map_type = attr->map_type;
84         array->map.key_size = attr->key_size;
85         array->map.value_size = attr->value_size;
86         array->map.max_entries = attr->max_entries;
87         array->elem_size = elem_size;
88
89         if (!percpu)
90                 goto out;
91
92         array_size += (u64) attr->max_entries * elem_size * num_possible_cpus();
93
94         if (array_size >= U32_MAX - PAGE_SIZE ||
95             elem_size > PCPU_MIN_UNIT_SIZE || bpf_array_alloc_percpu(array)) {
96                 bpf_map_area_free(array);
97                 return ERR_PTR(-ENOMEM);
98         }
99 out:
100         array->map.pages = round_up(array_size, PAGE_SIZE) >> PAGE_SHIFT;
101
102         return &array->map;
103 }
104
105 /* Called from syscall or from eBPF program */
106 static void *array_map_lookup_elem(struct bpf_map *map, void *key)
107 {
108         struct bpf_array *array = container_of(map, struct bpf_array, map);
109         u32 index = *(u32 *)key;
110
111         if (unlikely(index >= array->map.max_entries))
112                 return NULL;
113
114         return array->value + array->elem_size * index;
115 }
116
117 /* emit BPF instructions equivalent to C code of array_map_lookup_elem() */
118 static u32 array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
119 {
120         struct bpf_insn *insn = insn_buf;
121         u32 elem_size = round_up(map->value_size, 8);
122         const int ret = BPF_REG_0;
123         const int map_ptr = BPF_REG_1;
124         const int index = BPF_REG_2;
125
126         *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
127         *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
128         *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 3);
129
130         if (is_power_of_2(elem_size)) {
131                 *insn++ = BPF_ALU64_IMM(BPF_LSH, ret, ilog2(elem_size));
132         } else {
133                 *insn++ = BPF_ALU64_IMM(BPF_MUL, ret, elem_size);
134         }
135         *insn++ = BPF_ALU64_REG(BPF_ADD, ret, map_ptr);
136         *insn++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1);
137         *insn++ = BPF_MOV64_IMM(ret, 0);
138         return insn - insn_buf;
139 }
140
141 /* Called from eBPF program */
142 static void *percpu_array_map_lookup_elem(struct bpf_map *map, void *key)
143 {
144         struct bpf_array *array = container_of(map, struct bpf_array, map);
145         u32 index = *(u32 *)key;
146
147         if (unlikely(index >= array->map.max_entries))
148                 return NULL;
149
150         return this_cpu_ptr(array->pptrs[index]);
151 }
152
153 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value)
154 {
155         struct bpf_array *array = container_of(map, struct bpf_array, map);
156         u32 index = *(u32 *)key;
157         void __percpu *pptr;
158         int cpu, off = 0;
159         u32 size;
160
161         if (unlikely(index >= array->map.max_entries))
162                 return -ENOENT;
163
164         /* per_cpu areas are zero-filled and bpf programs can only
165          * access 'value_size' of them, so copying rounded areas
166          * will not leak any kernel data
167          */
168         size = round_up(map->value_size, 8);
169         rcu_read_lock();
170         pptr = array->pptrs[index];
171         for_each_possible_cpu(cpu) {
172                 bpf_long_memcpy(value + off, per_cpu_ptr(pptr, cpu), size);
173                 off += size;
174         }
175         rcu_read_unlock();
176         return 0;
177 }
178
179 /* Called from syscall */
180 static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
181 {
182         struct bpf_array *array = container_of(map, struct bpf_array, map);
183         u32 index = *(u32 *)key;
184         u32 *next = (u32 *)next_key;
185
186         if (index >= array->map.max_entries) {
187                 *next = 0;
188                 return 0;
189         }
190
191         if (index == array->map.max_entries - 1)
192                 return -ENOENT;
193
194         *next = index + 1;
195         return 0;
196 }
197
198 /* Called from syscall or from eBPF program */
199 static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
200                                  u64 map_flags)
201 {
202         struct bpf_array *array = container_of(map, struct bpf_array, map);
203         u32 index = *(u32 *)key;
204
205         if (unlikely(map_flags > BPF_EXIST))
206                 /* unknown flags */
207                 return -EINVAL;
208
209         if (unlikely(index >= array->map.max_entries))
210                 /* all elements were pre-allocated, cannot insert a new one */
211                 return -E2BIG;
212
213         if (unlikely(map_flags == BPF_NOEXIST))
214                 /* all elements already exist */
215                 return -EEXIST;
216
217         if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
218                 memcpy(this_cpu_ptr(array->pptrs[index]),
219                        value, map->value_size);
220         else
221                 memcpy(array->value + array->elem_size * index,
222                        value, map->value_size);
223         return 0;
224 }
225
226 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
227                             u64 map_flags)
228 {
229         struct bpf_array *array = container_of(map, struct bpf_array, map);
230         u32 index = *(u32 *)key;
231         void __percpu *pptr;
232         int cpu, off = 0;
233         u32 size;
234
235         if (unlikely(map_flags > BPF_EXIST))
236                 /* unknown flags */
237                 return -EINVAL;
238
239         if (unlikely(index >= array->map.max_entries))
240                 /* all elements were pre-allocated, cannot insert a new one */
241                 return -E2BIG;
242
243         if (unlikely(map_flags == BPF_NOEXIST))
244                 /* all elements already exist */
245                 return -EEXIST;
246
247         /* the user space will provide round_up(value_size, 8) bytes that
248          * will be copied into per-cpu area. bpf programs can only access
249          * value_size of it. During lookup the same extra bytes will be
250          * returned or zeros which were zero-filled by percpu_alloc,
251          * so no kernel data leaks possible
252          */
253         size = round_up(map->value_size, 8);
254         rcu_read_lock();
255         pptr = array->pptrs[index];
256         for_each_possible_cpu(cpu) {
257                 bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value + off, size);
258                 off += size;
259         }
260         rcu_read_unlock();
261         return 0;
262 }
263
264 /* Called from syscall or from eBPF program */
265 static int array_map_delete_elem(struct bpf_map *map, void *key)
266 {
267         return -EINVAL;
268 }
269
270 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
271 static void array_map_free(struct bpf_map *map)
272 {
273         struct bpf_array *array = container_of(map, struct bpf_array, map);
274
275         /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
276          * so the programs (can be more than one that used this map) were
277          * disconnected from events. Wait for outstanding programs to complete
278          * and free the array
279          */
280         synchronize_rcu();
281
282         if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
283                 bpf_array_free_percpu(array);
284
285         bpf_map_area_free(array);
286 }
287
288 static const struct bpf_map_ops array_ops = {
289         .map_alloc = array_map_alloc,
290         .map_free = array_map_free,
291         .map_get_next_key = array_map_get_next_key,
292         .map_lookup_elem = array_map_lookup_elem,
293         .map_update_elem = array_map_update_elem,
294         .map_delete_elem = array_map_delete_elem,
295         .map_gen_lookup = array_map_gen_lookup,
296 };
297
298 static struct bpf_map_type_list array_type __ro_after_init = {
299         .ops = &array_ops,
300         .type = BPF_MAP_TYPE_ARRAY,
301 };
302
303 static const struct bpf_map_ops percpu_array_ops = {
304         .map_alloc = array_map_alloc,
305         .map_free = array_map_free,
306         .map_get_next_key = array_map_get_next_key,
307         .map_lookup_elem = percpu_array_map_lookup_elem,
308         .map_update_elem = array_map_update_elem,
309         .map_delete_elem = array_map_delete_elem,
310 };
311
312 static struct bpf_map_type_list percpu_array_type __ro_after_init = {
313         .ops = &percpu_array_ops,
314         .type = BPF_MAP_TYPE_PERCPU_ARRAY,
315 };
316
317 static int __init register_array_map(void)
318 {
319         bpf_register_map_type(&array_type);
320         bpf_register_map_type(&percpu_array_type);
321         return 0;
322 }
323 late_initcall(register_array_map);
324
325 static struct bpf_map *fd_array_map_alloc(union bpf_attr *attr)
326 {
327         /* only file descriptors can be stored in this type of map */
328         if (attr->value_size != sizeof(u32))
329                 return ERR_PTR(-EINVAL);
330         return array_map_alloc(attr);
331 }
332
333 static void fd_array_map_free(struct bpf_map *map)
334 {
335         struct bpf_array *array = container_of(map, struct bpf_array, map);
336         int i;
337
338         synchronize_rcu();
339
340         /* make sure it's empty */
341         for (i = 0; i < array->map.max_entries; i++)
342                 BUG_ON(array->ptrs[i] != NULL);
343
344         bpf_map_area_free(array);
345 }
346
347 static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
348 {
349         return NULL;
350 }
351
352 /* only called from syscall */
353 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
354                                  void *key, void *value, u64 map_flags)
355 {
356         struct bpf_array *array = container_of(map, struct bpf_array, map);
357         void *new_ptr, *old_ptr;
358         u32 index = *(u32 *)key, ufd;
359
360         if (map_flags != BPF_ANY)
361                 return -EINVAL;
362
363         if (index >= array->map.max_entries)
364                 return -E2BIG;
365
366         ufd = *(u32 *)value;
367         new_ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
368         if (IS_ERR(new_ptr))
369                 return PTR_ERR(new_ptr);
370
371         old_ptr = xchg(array->ptrs + index, new_ptr);
372         if (old_ptr)
373                 map->ops->map_fd_put_ptr(old_ptr);
374
375         return 0;
376 }
377
378 static int fd_array_map_delete_elem(struct bpf_map *map, void *key)
379 {
380         struct bpf_array *array = container_of(map, struct bpf_array, map);
381         void *old_ptr;
382         u32 index = *(u32 *)key;
383
384         if (index >= array->map.max_entries)
385                 return -E2BIG;
386
387         old_ptr = xchg(array->ptrs + index, NULL);
388         if (old_ptr) {
389                 map->ops->map_fd_put_ptr(old_ptr);
390                 return 0;
391         } else {
392                 return -ENOENT;
393         }
394 }
395
396 static void *prog_fd_array_get_ptr(struct bpf_map *map,
397                                    struct file *map_file, int fd)
398 {
399         struct bpf_array *array = container_of(map, struct bpf_array, map);
400         struct bpf_prog *prog = bpf_prog_get(fd);
401
402         if (IS_ERR(prog))
403                 return prog;
404
405         if (!bpf_prog_array_compatible(array, prog)) {
406                 bpf_prog_put(prog);
407                 return ERR_PTR(-EINVAL);
408         }
409
410         return prog;
411 }
412
413 static void prog_fd_array_put_ptr(void *ptr)
414 {
415         bpf_prog_put(ptr);
416 }
417
418 /* decrement refcnt of all bpf_progs that are stored in this map */
419 void bpf_fd_array_map_clear(struct bpf_map *map)
420 {
421         struct bpf_array *array = container_of(map, struct bpf_array, map);
422         int i;
423
424         for (i = 0; i < array->map.max_entries; i++)
425                 fd_array_map_delete_elem(map, &i);
426 }
427
428 static const struct bpf_map_ops prog_array_ops = {
429         .map_alloc = fd_array_map_alloc,
430         .map_free = fd_array_map_free,
431         .map_get_next_key = array_map_get_next_key,
432         .map_lookup_elem = fd_array_map_lookup_elem,
433         .map_delete_elem = fd_array_map_delete_elem,
434         .map_fd_get_ptr = prog_fd_array_get_ptr,
435         .map_fd_put_ptr = prog_fd_array_put_ptr,
436 };
437
438 static struct bpf_map_type_list prog_array_type __ro_after_init = {
439         .ops = &prog_array_ops,
440         .type = BPF_MAP_TYPE_PROG_ARRAY,
441 };
442
443 static int __init register_prog_array_map(void)
444 {
445         bpf_register_map_type(&prog_array_type);
446         return 0;
447 }
448 late_initcall(register_prog_array_map);
449
450 static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
451                                                    struct file *map_file)
452 {
453         struct bpf_event_entry *ee;
454
455         ee = kzalloc(sizeof(*ee), GFP_ATOMIC);
456         if (ee) {
457                 ee->event = perf_file->private_data;
458                 ee->perf_file = perf_file;
459                 ee->map_file = map_file;
460         }
461
462         return ee;
463 }
464
465 static void __bpf_event_entry_free(struct rcu_head *rcu)
466 {
467         struct bpf_event_entry *ee;
468
469         ee = container_of(rcu, struct bpf_event_entry, rcu);
470         fput(ee->perf_file);
471         kfree(ee);
472 }
473
474 static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
475 {
476         call_rcu(&ee->rcu, __bpf_event_entry_free);
477 }
478
479 static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
480                                          struct file *map_file, int fd)
481 {
482         const struct perf_event_attr *attr;
483         struct bpf_event_entry *ee;
484         struct perf_event *event;
485         struct file *perf_file;
486
487         perf_file = perf_event_get(fd);
488         if (IS_ERR(perf_file))
489                 return perf_file;
490
491         event = perf_file->private_data;
492         ee = ERR_PTR(-EINVAL);
493
494         attr = perf_event_attrs(event);
495         if (IS_ERR(attr) || attr->inherit)
496                 goto err_out;
497
498         switch (attr->type) {
499         case PERF_TYPE_SOFTWARE:
500                 if (attr->config != PERF_COUNT_SW_BPF_OUTPUT)
501                         goto err_out;
502                 /* fall-through */
503         case PERF_TYPE_RAW:
504         case PERF_TYPE_HARDWARE:
505                 ee = bpf_event_entry_gen(perf_file, map_file);
506                 if (ee)
507                         return ee;
508                 ee = ERR_PTR(-ENOMEM);
509                 /* fall-through */
510         default:
511                 break;
512         }
513
514 err_out:
515         fput(perf_file);
516         return ee;
517 }
518
519 static void perf_event_fd_array_put_ptr(void *ptr)
520 {
521         bpf_event_entry_free_rcu(ptr);
522 }
523
524 static void perf_event_fd_array_release(struct bpf_map *map,
525                                         struct file *map_file)
526 {
527         struct bpf_array *array = container_of(map, struct bpf_array, map);
528         struct bpf_event_entry *ee;
529         int i;
530
531         rcu_read_lock();
532         for (i = 0; i < array->map.max_entries; i++) {
533                 ee = READ_ONCE(array->ptrs[i]);
534                 if (ee && ee->map_file == map_file)
535                         fd_array_map_delete_elem(map, &i);
536         }
537         rcu_read_unlock();
538 }
539
540 static const struct bpf_map_ops perf_event_array_ops = {
541         .map_alloc = fd_array_map_alloc,
542         .map_free = fd_array_map_free,
543         .map_get_next_key = array_map_get_next_key,
544         .map_lookup_elem = fd_array_map_lookup_elem,
545         .map_delete_elem = fd_array_map_delete_elem,
546         .map_fd_get_ptr = perf_event_fd_array_get_ptr,
547         .map_fd_put_ptr = perf_event_fd_array_put_ptr,
548         .map_release = perf_event_fd_array_release,
549 };
550
551 static struct bpf_map_type_list perf_event_array_type __ro_after_init = {
552         .ops = &perf_event_array_ops,
553         .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
554 };
555
556 static int __init register_perf_event_array_map(void)
557 {
558         bpf_register_map_type(&perf_event_array_type);
559         return 0;
560 }
561 late_initcall(register_perf_event_array_map);
562
563 #ifdef CONFIG_CGROUPS
564 static void *cgroup_fd_array_get_ptr(struct bpf_map *map,
565                                      struct file *map_file /* not used */,
566                                      int fd)
567 {
568         return cgroup_get_from_fd(fd);
569 }
570
571 static void cgroup_fd_array_put_ptr(void *ptr)
572 {
573         /* cgroup_put free cgrp after a rcu grace period */
574         cgroup_put(ptr);
575 }
576
577 static void cgroup_fd_array_free(struct bpf_map *map)
578 {
579         bpf_fd_array_map_clear(map);
580         fd_array_map_free(map);
581 }
582
583 static const struct bpf_map_ops cgroup_array_ops = {
584         .map_alloc = fd_array_map_alloc,
585         .map_free = cgroup_fd_array_free,
586         .map_get_next_key = array_map_get_next_key,
587         .map_lookup_elem = fd_array_map_lookup_elem,
588         .map_delete_elem = fd_array_map_delete_elem,
589         .map_fd_get_ptr = cgroup_fd_array_get_ptr,
590         .map_fd_put_ptr = cgroup_fd_array_put_ptr,
591 };
592
593 static struct bpf_map_type_list cgroup_array_type __ro_after_init = {
594         .ops = &cgroup_array_ops,
595         .type = BPF_MAP_TYPE_CGROUP_ARRAY,
596 };
597
598 static int __init register_cgroup_array_map(void)
599 {
600         bpf_register_map_type(&cgroup_array_type);
601         return 0;
602 }
603 late_initcall(register_cgroup_array_map);
604 #endif