diff options
Diffstat (limited to 'kernel/bpf/arraymap.c')
-rw-r--r-- | kernel/bpf/arraymap.c | 113 |
1 files changed, 107 insertions, 6 deletions
diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index 8a6616583f38..614bcd4c1d74 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c | |||
@@ -14,12 +14,7 @@ | |||
14 | #include <linux/vmalloc.h> | 14 | #include <linux/vmalloc.h> |
15 | #include <linux/slab.h> | 15 | #include <linux/slab.h> |
16 | #include <linux/mm.h> | 16 | #include <linux/mm.h> |
17 | 17 | #include <linux/filter.h> | |
18 | struct bpf_array { | ||
19 | struct bpf_map map; | ||
20 | u32 elem_size; | ||
21 | char value[0] __aligned(8); | ||
22 | }; | ||
23 | 18 | ||
24 | /* Called from syscall */ | 19 | /* Called from syscall */ |
25 | static struct bpf_map *array_map_alloc(union bpf_attr *attr) | 20 | static struct bpf_map *array_map_alloc(union bpf_attr *attr) |
@@ -154,3 +149,109 @@ static int __init register_array_map(void) | |||
154 | return 0; | 149 | return 0; |
155 | } | 150 | } |
156 | late_initcall(register_array_map); | 151 | late_initcall(register_array_map); |
152 | |||
153 | static struct bpf_map *prog_array_map_alloc(union bpf_attr *attr) | ||
154 | { | ||
155 | /* only bpf_prog file descriptors can be stored in prog_array map */ | ||
156 | if (attr->value_size != sizeof(u32)) | ||
157 | return ERR_PTR(-EINVAL); | ||
158 | return array_map_alloc(attr); | ||
159 | } | ||
160 | |||
161 | static void prog_array_map_free(struct bpf_map *map) | ||
162 | { | ||
163 | struct bpf_array *array = container_of(map, struct bpf_array, map); | ||
164 | int i; | ||
165 | |||
166 | synchronize_rcu(); | ||
167 | |||
168 | /* make sure it's empty */ | ||
169 | for (i = 0; i < array->map.max_entries; i++) | ||
170 | BUG_ON(array->prog[i] != NULL); | ||
171 | kvfree(array); | ||
172 | } | ||
173 | |||
174 | static void *prog_array_map_lookup_elem(struct bpf_map *map, void *key) | ||
175 | { | ||
176 | return NULL; | ||
177 | } | ||
178 | |||
179 | /* only called from syscall */ | ||
180 | static int prog_array_map_update_elem(struct bpf_map *map, void *key, | ||
181 | void *value, u64 map_flags) | ||
182 | { | ||
183 | struct bpf_array *array = container_of(map, struct bpf_array, map); | ||
184 | struct bpf_prog *prog, *old_prog; | ||
185 | u32 index = *(u32 *)key, ufd; | ||
186 | |||
187 | if (map_flags != BPF_ANY) | ||
188 | return -EINVAL; | ||
189 | |||
190 | if (index >= array->map.max_entries) | ||
191 | return -E2BIG; | ||
192 | |||
193 | ufd = *(u32 *)value; | ||
194 | prog = bpf_prog_get(ufd); | ||
195 | if (IS_ERR(prog)) | ||
196 | return PTR_ERR(prog); | ||
197 | |||
198 | if (!bpf_prog_array_compatible(array, prog)) { | ||
199 | bpf_prog_put(prog); | ||
200 | return -EINVAL; | ||
201 | } | ||
202 | |||
203 | old_prog = xchg(array->prog + index, prog); | ||
204 | if (old_prog) | ||
205 | bpf_prog_put(old_prog); | ||
206 | |||
207 | return 0; | ||
208 | } | ||
209 | |||
210 | static int prog_array_map_delete_elem(struct bpf_map *map, void *key) | ||
211 | { | ||
212 | struct bpf_array *array = container_of(map, struct bpf_array, map); | ||
213 | struct bpf_prog *old_prog; | ||
214 | u32 index = *(u32 *)key; | ||
215 | |||
216 | if (index >= array->map.max_entries) | ||
217 | return -E2BIG; | ||
218 | |||
219 | old_prog = xchg(array->prog + index, NULL); | ||
220 | if (old_prog) { | ||
221 | bpf_prog_put(old_prog); | ||
222 | return 0; | ||
223 | } else { | ||
224 | return -ENOENT; | ||
225 | } | ||
226 | } | ||
227 | |||
228 | /* decrement refcnt of all bpf_progs that are stored in this map */ | ||
229 | void bpf_prog_array_map_clear(struct bpf_map *map) | ||
230 | { | ||
231 | struct bpf_array *array = container_of(map, struct bpf_array, map); | ||
232 | int i; | ||
233 | |||
234 | for (i = 0; i < array->map.max_entries; i++) | ||
235 | prog_array_map_delete_elem(map, &i); | ||
236 | } | ||
237 | |||
238 | static const struct bpf_map_ops prog_array_ops = { | ||
239 | .map_alloc = prog_array_map_alloc, | ||
240 | .map_free = prog_array_map_free, | ||
241 | .map_get_next_key = array_map_get_next_key, | ||
242 | .map_lookup_elem = prog_array_map_lookup_elem, | ||
243 | .map_update_elem = prog_array_map_update_elem, | ||
244 | .map_delete_elem = prog_array_map_delete_elem, | ||
245 | }; | ||
246 | |||
247 | static struct bpf_map_type_list prog_array_type __read_mostly = { | ||
248 | .ops = &prog_array_ops, | ||
249 | .type = BPF_MAP_TYPE_PROG_ARRAY, | ||
250 | }; | ||
251 | |||
252 | static int __init register_prog_array_map(void) | ||
253 | { | ||
254 | bpf_register_map_type(&prog_array_type); | ||
255 | return 0; | ||
256 | } | ||
257 | late_initcall(register_prog_array_map); | ||