aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/bpf
diff options
context:
space:
mode:
authorAlexei Starovoitov <ast@plumgrid.com>2014-11-13 20:36:46 -0500
committerDavid S. Miller <davem@davemloft.net>2014-11-18 13:43:59 -0500
commit28fbcfa08d8ed7c5a50d41a0433aad222835e8e3 (patch)
tree1ef9417be1eb51e76456b3f2dbaf080c48883c32 /kernel/bpf
parent0f8e4bd8a1fc8c4185f1630061d0a1f2d197a475 (diff)
bpf: add array type of eBPF maps
add new map type BPF_MAP_TYPE_ARRAY and its implementation - optimized for fastest possible lookup() . in the future verifier/JIT may recognize lookup() with constant key and optimize it into constant pointer. Can optimize non-constant key into direct pointer arithmetic as well, since pointers and value_size are constant for the life of the eBPF program. In other words array_map_lookup_elem() may be 'inlined' by verifier/JIT while preserving concurrent access to this map from user space - two main use cases for array type: . 'global' eBPF variables: array of 1 element with key=0 and value is a collection of 'global' variables which programs can use to keep the state between events . aggregation of tracing events into fixed set of buckets - all array elements pre-allocated and zero initialized at init time - key as an index in array and can only be 4 byte - map_delete_elem() returns EINVAL, since elements cannot be deleted - map_update_elem() replaces elements in an non-atomic way (for atomic updates hashtable type should be used instead) Signed-off-by: Alexei Starovoitov <ast@plumgrid.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'kernel/bpf')
-rw-r--r--kernel/bpf/Makefile2
-rw-r--r--kernel/bpf/arraymap.c151
2 files changed, 152 insertions, 1 deletions
diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile
index 2c0ec7f9da78..72ec98ba2d42 100644
--- a/kernel/bpf/Makefile
+++ b/kernel/bpf/Makefile
@@ -1,5 +1,5 @@
1obj-y := core.o 1obj-y := core.o
2obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o hashtab.o 2obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o hashtab.o arraymap.o
3ifdef CONFIG_TEST_BPF 3ifdef CONFIG_TEST_BPF
4obj-$(CONFIG_BPF_SYSCALL) += test_stub.o 4obj-$(CONFIG_BPF_SYSCALL) += test_stub.o
5endif 5endif
diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c
new file mode 100644
index 000000000000..58b80c137afd
--- /dev/null
+++ b/kernel/bpf/arraymap.c
@@ -0,0 +1,151 @@
1/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 */
12#include <linux/bpf.h>
13#include <linux/err.h>
14#include <linux/vmalloc.h>
15#include <linux/slab.h>
16#include <linux/mm.h>
17
18struct bpf_array {
19 struct bpf_map map;
20 u32 elem_size;
21 char value[0] __aligned(8);
22};
23
24/* Called from syscall */
25static struct bpf_map *array_map_alloc(union bpf_attr *attr)
26{
27 struct bpf_array *array;
28 u32 elem_size;
29
30 /* check sanity of attributes */
31 if (attr->max_entries == 0 || attr->key_size != 4 ||
32 attr->value_size == 0)
33 return ERR_PTR(-EINVAL);
34
35 elem_size = round_up(attr->value_size, 8);
36
37 /* allocate all map elements and zero-initialize them */
38 array = kzalloc(sizeof(*array) + attr->max_entries * elem_size,
39 GFP_USER | __GFP_NOWARN);
40 if (!array) {
41 array = vzalloc(array->map.max_entries * array->elem_size);
42 if (!array)
43 return ERR_PTR(-ENOMEM);
44 }
45
46 /* copy mandatory map attributes */
47 array->map.key_size = attr->key_size;
48 array->map.value_size = attr->value_size;
49 array->map.max_entries = attr->max_entries;
50
51 array->elem_size = elem_size;
52
53 return &array->map;
54
55}
56
57/* Called from syscall or from eBPF program */
58static void *array_map_lookup_elem(struct bpf_map *map, void *key)
59{
60 struct bpf_array *array = container_of(map, struct bpf_array, map);
61 u32 index = *(u32 *)key;
62
63 if (index >= array->map.max_entries)
64 return NULL;
65
66 return array->value + array->elem_size * index;
67}
68
69/* Called from syscall */
70static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
71{
72 struct bpf_array *array = container_of(map, struct bpf_array, map);
73 u32 index = *(u32 *)key;
74 u32 *next = (u32 *)next_key;
75
76 if (index >= array->map.max_entries) {
77 *next = 0;
78 return 0;
79 }
80
81 if (index == array->map.max_entries - 1)
82 return -ENOENT;
83
84 *next = index + 1;
85 return 0;
86}
87
88/* Called from syscall or from eBPF program */
89static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
90 u64 map_flags)
91{
92 struct bpf_array *array = container_of(map, struct bpf_array, map);
93 u32 index = *(u32 *)key;
94
95 if (map_flags > BPF_EXIST)
96 /* unknown flags */
97 return -EINVAL;
98
99 if (index >= array->map.max_entries)
100 /* all elements were pre-allocated, cannot insert a new one */
101 return -E2BIG;
102
103 if (map_flags == BPF_NOEXIST)
104 /* all elemenets already exist */
105 return -EEXIST;
106
107 memcpy(array->value + array->elem_size * index, value, array->elem_size);
108 return 0;
109}
110
111/* Called from syscall or from eBPF program */
112static int array_map_delete_elem(struct bpf_map *map, void *key)
113{
114 return -EINVAL;
115}
116
117/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
118static void array_map_free(struct bpf_map *map)
119{
120 struct bpf_array *array = container_of(map, struct bpf_array, map);
121
122 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
123 * so the programs (can be more than one that used this map) were
124 * disconnected from events. Wait for outstanding programs to complete
125 * and free the array
126 */
127 synchronize_rcu();
128
129 kvfree(array);
130}
131
132static struct bpf_map_ops array_ops = {
133 .map_alloc = array_map_alloc,
134 .map_free = array_map_free,
135 .map_get_next_key = array_map_get_next_key,
136 .map_lookup_elem = array_map_lookup_elem,
137 .map_update_elem = array_map_update_elem,
138 .map_delete_elem = array_map_delete_elem,
139};
140
141static struct bpf_map_type_list tl = {
142 .ops = &array_ops,
143 .type = BPF_MAP_TYPE_ARRAY,
144};
145
146static int __init register_array_map(void)
147{
148 bpf_register_map_type(&tl);
149 return 0;
150}
151late_initcall(register_array_map);