blob: ad2f0ed754719cd7439a6e4e50bd6dc0fe148949 [file] [log] [blame]
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -08001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov6c905982016-03-07 21:57:15 -08002 * Copyright (c) 2016 Facebook
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -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/jhash.h>
15#include <linux/filter.h>
Alexei Starovoitov6c905982016-03-07 21:57:15 -080016#include "percpu_freelist.h"
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080017
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080018struct bucket {
19 struct hlist_head head;
20 raw_spinlock_t lock;
21};
22
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080023struct bpf_htab {
24 struct bpf_map map;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080025 struct bucket *buckets;
Alexei Starovoitov6c905982016-03-07 21:57:15 -080026 void *elems;
27 struct pcpu_freelist freelist;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070028 void __percpu *extra_elems;
tom.leiming@gmail.com6591f1e2015-12-29 22:40:25 +080029 atomic_t count; /* number of elements in this hashtable */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080030 u32 n_buckets; /* number of hash buckets */
31 u32 elem_size; /* size of each element in bytes */
32};
33
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070034enum extra_elem_state {
35 HTAB_NOT_AN_EXTRA_ELEM = 0,
36 HTAB_EXTRA_ELEM_FREE,
37 HTAB_EXTRA_ELEM_USED
38};
39
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080040/* each htab element is struct htab_elem + key + value */
41struct htab_elem {
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -080042 union {
Alexei Starovoitov6c905982016-03-07 21:57:15 -080043 struct hlist_node hash_node;
44 struct bpf_htab *htab;
45 struct pcpu_freelist_node fnode;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -080046 };
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070047 union {
48 struct rcu_head rcu;
49 enum extra_elem_state state;
50 };
Alexei Starovoitov6c905982016-03-07 21:57:15 -080051 u32 hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080052 char key[0] __aligned(8);
53};
54
Alexei Starovoitov6c905982016-03-07 21:57:15 -080055static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
56 void __percpu *pptr)
57{
58 *(void __percpu **)(l->key + key_size) = pptr;
59}
60
61static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
62{
63 return *(void __percpu **)(l->key + key_size);
64}
65
66static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
67{
68 return (struct htab_elem *) (htab->elems + i * htab->elem_size);
69}
70
71static void htab_free_elems(struct bpf_htab *htab)
72{
73 int i;
74
75 if (htab->map.map_type != BPF_MAP_TYPE_PERCPU_HASH)
76 goto free_elems;
77
78 for (i = 0; i < htab->map.max_entries; i++) {
79 void __percpu *pptr;
80
81 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
82 htab->map.key_size);
83 free_percpu(pptr);
84 }
85free_elems:
Daniel Borkmann251d00b2017-01-18 15:14:17 +010086 bpf_map_area_free(htab->elems);
Alexei Starovoitov6c905982016-03-07 21:57:15 -080087}
88
89static int prealloc_elems_and_freelist(struct bpf_htab *htab)
90{
91 int err = -ENOMEM, i;
92
Daniel Borkmann251d00b2017-01-18 15:14:17 +010093 htab->elems = bpf_map_area_alloc(htab->elem_size *
94 htab->map.max_entries);
Alexei Starovoitov6c905982016-03-07 21:57:15 -080095 if (!htab->elems)
96 return -ENOMEM;
97
98 if (htab->map.map_type != BPF_MAP_TYPE_PERCPU_HASH)
99 goto skip_percpu_elems;
100
101 for (i = 0; i < htab->map.max_entries; i++) {
102 u32 size = round_up(htab->map.value_size, 8);
103 void __percpu *pptr;
104
105 pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
106 if (!pptr)
107 goto free_elems;
108 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
109 pptr);
110 }
111
112skip_percpu_elems:
113 err = pcpu_freelist_init(&htab->freelist);
114 if (err)
115 goto free_elems;
116
117 pcpu_freelist_populate(&htab->freelist, htab->elems, htab->elem_size,
118 htab->map.max_entries);
119 return 0;
120
121free_elems:
122 htab_free_elems(htab);
123 return err;
124}
125
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700126static int alloc_extra_elems(struct bpf_htab *htab)
127{
128 void __percpu *pptr;
129 int cpu;
130
131 pptr = __alloc_percpu_gfp(htab->elem_size, 8, GFP_USER | __GFP_NOWARN);
132 if (!pptr)
133 return -ENOMEM;
134
135 for_each_possible_cpu(cpu) {
136 ((struct htab_elem *)per_cpu_ptr(pptr, cpu))->state =
137 HTAB_EXTRA_ELEM_FREE;
138 }
139 htab->extra_elems = pptr;
140 return 0;
141}
142
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800143/* Called from syscall */
144static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
145{
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800146 bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_HASH;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800147 struct bpf_htab *htab;
148 int err, i;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800149 u64 cost;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800150
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800151 if (attr->map_flags & ~BPF_F_NO_PREALLOC)
152 /* reserved bits should not be used */
153 return ERR_PTR(-EINVAL);
154
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800155 htab = kzalloc(sizeof(*htab), GFP_USER);
156 if (!htab)
157 return ERR_PTR(-ENOMEM);
158
159 /* mandatory map attributes */
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800160 htab->map.map_type = attr->map_type;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800161 htab->map.key_size = attr->key_size;
162 htab->map.value_size = attr->value_size;
163 htab->map.max_entries = attr->max_entries;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800164 htab->map.map_flags = attr->map_flags;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800165
166 /* check sanity of attributes.
167 * value_size == 0 may be allowed in the future to use map as a set
168 */
169 err = -EINVAL;
170 if (htab->map.max_entries == 0 || htab->map.key_size == 0 ||
171 htab->map.value_size == 0)
172 goto free_htab;
173
174 /* hash table size must be power of 2 */
175 htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
176
177 err = -E2BIG;
178 if (htab->map.key_size > MAX_BPF_STACK)
179 /* eBPF programs initialize keys on stack, so they cannot be
180 * larger than max stack size
181 */
182 goto free_htab;
183
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800184 if (htab->map.value_size >= (1 << (KMALLOC_SHIFT_MAX - 1)) -
185 MAX_BPF_STACK - sizeof(struct htab_elem))
186 /* if value_size is bigger, the user space won't be able to
187 * access the elements via bpf syscall. This check also makes
188 * sure that the elem_size doesn't overflow and it's
189 * kmalloc-able later in htab_map_update_elem()
190 */
191 goto free_htab;
192
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800193 if (percpu && round_up(htab->map.value_size, 8) > PCPU_MIN_UNIT_SIZE)
194 /* make sure the size for pcpu_alloc() is reasonable */
195 goto free_htab;
196
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800197 htab->elem_size = sizeof(struct htab_elem) +
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800198 round_up(htab->map.key_size, 8);
199 if (percpu)
200 htab->elem_size += sizeof(void *);
201 else
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800202 htab->elem_size += round_up(htab->map.value_size, 8);
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800203
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800204 /* prevent zero size kmalloc and check for u32 overflow */
205 if (htab->n_buckets == 0 ||
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800206 htab->n_buckets > U32_MAX / sizeof(struct bucket))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800207 goto free_htab;
208
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800209 cost = (u64) htab->n_buckets * sizeof(struct bucket) +
210 (u64) htab->elem_size * htab->map.max_entries;
211
212 if (percpu)
213 cost += (u64) round_up(htab->map.value_size, 8) *
214 num_possible_cpus() * htab->map.max_entries;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700215 else
216 cost += (u64) htab->elem_size * num_possible_cpus();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800217
218 if (cost >= U32_MAX - PAGE_SIZE)
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800219 /* make sure page count doesn't overflow */
220 goto free_htab;
221
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800222 htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800223
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800224 /* if map size is larger than memlock limit, reject it early */
225 err = bpf_map_precharge_memlock(htab->map.pages);
226 if (err)
227 goto free_htab;
228
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800229 err = -ENOMEM;
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100230 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
231 sizeof(struct bucket));
232 if (!htab->buckets)
233 goto free_htab;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800234
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800235 for (i = 0; i < htab->n_buckets; i++) {
236 INIT_HLIST_HEAD(&htab->buckets[i].head);
237 raw_spin_lock_init(&htab->buckets[i].lock);
238 }
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800239
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700240 if (!percpu) {
241 err = alloc_extra_elems(htab);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800242 if (err)
243 goto free_buckets;
244 }
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800245
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700246 if (!(attr->map_flags & BPF_F_NO_PREALLOC)) {
247 err = prealloc_elems_and_freelist(htab);
248 if (err)
249 goto free_extra_elems;
250 }
251
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800252 return &htab->map;
253
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700254free_extra_elems:
255 free_percpu(htab->extra_elems);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800256free_buckets:
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100257 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800258free_htab:
259 kfree(htab);
260 return ERR_PTR(err);
261}
262
263static inline u32 htab_map_hash(const void *key, u32 key_len)
264{
265 return jhash(key, key_len, 0);
266}
267
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800268static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800269{
270 return &htab->buckets[hash & (htab->n_buckets - 1)];
271}
272
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800273static inline struct hlist_head *select_bucket(struct bpf_htab *htab, u32 hash)
274{
275 return &__select_bucket(htab, hash)->head;
276}
277
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800278static struct htab_elem *lookup_elem_raw(struct hlist_head *head, u32 hash,
279 void *key, u32 key_size)
280{
281 struct htab_elem *l;
282
283 hlist_for_each_entry_rcu(l, head, hash_node)
284 if (l->hash == hash && !memcmp(&l->key, key, key_size))
285 return l;
286
287 return NULL;
288}
289
290/* Called from syscall or from eBPF program */
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800291static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800292{
293 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
294 struct hlist_head *head;
295 struct htab_elem *l;
296 u32 hash, key_size;
297
298 /* Must be called with rcu_read_lock. */
299 WARN_ON_ONCE(!rcu_read_lock_held());
300
301 key_size = map->key_size;
302
303 hash = htab_map_hash(key, key_size);
304
305 head = select_bucket(htab, hash);
306
307 l = lookup_elem_raw(head, hash, key, key_size);
308
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800309 return l;
310}
311
312static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
313{
314 struct htab_elem *l = __htab_map_lookup_elem(map, key);
315
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800316 if (l)
317 return l->key + round_up(map->key_size, 8);
318
319 return NULL;
320}
321
322/* Called from syscall */
323static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
324{
325 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
326 struct hlist_head *head;
327 struct htab_elem *l, *next_l;
328 u32 hash, key_size;
329 int i;
330
331 WARN_ON_ONCE(!rcu_read_lock_held());
332
333 key_size = map->key_size;
334
335 hash = htab_map_hash(key, key_size);
336
337 head = select_bucket(htab, hash);
338
339 /* lookup the key */
340 l = lookup_elem_raw(head, hash, key, key_size);
341
342 if (!l) {
343 i = 0;
344 goto find_first_elem;
345 }
346
347 /* key was found, get next key in the same bucket */
348 next_l = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&l->hash_node)),
349 struct htab_elem, hash_node);
350
351 if (next_l) {
352 /* if next elem in this hash list is non-zero, just return it */
353 memcpy(next_key, next_l->key, key_size);
354 return 0;
355 }
356
357 /* no more elements in this hash list, go to the next bucket */
358 i = hash & (htab->n_buckets - 1);
359 i++;
360
361find_first_elem:
362 /* iterate over buckets */
363 for (; i < htab->n_buckets; i++) {
364 head = select_bucket(htab, i);
365
366 /* pick first element in the bucket */
367 next_l = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
368 struct htab_elem, hash_node);
369 if (next_l) {
370 /* if it's not empty, just return it */
371 memcpy(next_key, next_l->key, key_size);
372 return 0;
373 }
374 }
375
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800376 /* iterated over all buckets and all elements */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800377 return -ENOENT;
378}
379
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800380static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800381{
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800382 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
383 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800384 kfree(l);
385}
386
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800387static void htab_elem_free_rcu(struct rcu_head *head)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800388{
389 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800390 struct bpf_htab *htab = l->htab;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800391
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800392 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
393 * we're calling kfree, otherwise deadlock is possible if kprobes
394 * are placed somewhere inside of slub
395 */
396 preempt_disable();
397 __this_cpu_inc(bpf_prog_active);
398 htab_elem_free(htab, l);
399 __this_cpu_dec(bpf_prog_active);
400 preempt_enable();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800401}
402
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800403static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800404{
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700405 if (l->state == HTAB_EXTRA_ELEM_USED) {
406 l->state = HTAB_EXTRA_ELEM_FREE;
407 return;
408 }
409
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800410 if (!(htab->map.map_flags & BPF_F_NO_PREALLOC)) {
411 pcpu_freelist_push(&htab->freelist, &l->fnode);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800412 } else {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800413 atomic_dec(&htab->count);
414 l->htab = htab;
415 call_rcu(&l->rcu, htab_elem_free_rcu);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800416 }
417}
418
419static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
420 void *value, u32 key_size, u32 hash,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700421 bool percpu, bool onallcpus,
422 bool old_elem_exists)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800423{
424 u32 size = htab->map.value_size;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800425 bool prealloc = !(htab->map.map_flags & BPF_F_NO_PREALLOC);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800426 struct htab_elem *l_new;
427 void __percpu *pptr;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700428 int err = 0;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800429
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800430 if (prealloc) {
431 l_new = (struct htab_elem *)pcpu_freelist_pop(&htab->freelist);
432 if (!l_new)
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700433 err = -E2BIG;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800434 } else {
435 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
436 atomic_dec(&htab->count);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700437 err = -E2BIG;
438 } else {
439 l_new = kmalloc(htab->elem_size,
440 GFP_ATOMIC | __GFP_NOWARN);
441 if (!l_new)
442 return ERR_PTR(-ENOMEM);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800443 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700444 }
445
446 if (err) {
447 if (!old_elem_exists)
448 return ERR_PTR(err);
449
450 /* if we're updating the existing element and the hash table
451 * is full, use per-cpu extra elems
452 */
453 l_new = this_cpu_ptr(htab->extra_elems);
454 if (l_new->state != HTAB_EXTRA_ELEM_FREE)
455 return ERR_PTR(-E2BIG);
456 l_new->state = HTAB_EXTRA_ELEM_USED;
457 } else {
458 l_new->state = HTAB_NOT_AN_EXTRA_ELEM;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800459 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800460
461 memcpy(l_new->key, key, key_size);
462 if (percpu) {
463 /* round up value_size to 8 bytes */
464 size = round_up(size, 8);
465
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800466 if (prealloc) {
467 pptr = htab_elem_get_ptr(l_new, key_size);
468 } else {
469 /* alloc_percpu zero-fills */
470 pptr = __alloc_percpu_gfp(size, 8,
471 GFP_ATOMIC | __GFP_NOWARN);
472 if (!pptr) {
473 kfree(l_new);
474 return ERR_PTR(-ENOMEM);
475 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800476 }
477
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800478 if (!onallcpus) {
479 /* copy true value_size bytes */
480 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
481 } else {
482 int off = 0, cpu;
483
484 for_each_possible_cpu(cpu) {
485 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
486 value + off, size);
487 off += size;
488 }
489 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800490 if (!prealloc)
491 htab_elem_set_ptr(l_new, key_size, pptr);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800492 } else {
493 memcpy(l_new->key + round_up(key_size, 8), value, size);
494 }
495
496 l_new->hash = hash;
497 return l_new;
498}
499
500static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
501 u64 map_flags)
502{
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800503 if (l_old && map_flags == BPF_NOEXIST)
504 /* elem already exists */
505 return -EEXIST;
506
507 if (!l_old && map_flags == BPF_EXIST)
508 /* elem doesn't exist, cannot update it */
509 return -ENOENT;
510
511 return 0;
512}
513
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800514/* Called from syscall or from eBPF program */
515static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
516 u64 map_flags)
517{
518 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800519 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800520 struct hlist_head *head;
521 unsigned long flags;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800522 struct bucket *b;
523 u32 key_size, hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800524 int ret;
525
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800526 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800527 /* unknown flags */
528 return -EINVAL;
529
530 WARN_ON_ONCE(!rcu_read_lock_held());
531
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800532 key_size = map->key_size;
533
534 hash = htab_map_hash(key, key_size);
535
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800536 b = __select_bucket(htab, hash);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800537 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800538
539 /* bpf_map_update_elem() can be called in_irq() */
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800540 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800541
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800542 l_old = lookup_elem_raw(head, hash, key, key_size);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800543
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800544 ret = check_flags(htab, l_old, map_flags);
545 if (ret)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800546 goto err;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800547
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700548 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
549 !!l_old);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800550 if (IS_ERR(l_new)) {
551 /* all pre-allocated elements are in use or memory exhausted */
552 ret = PTR_ERR(l_new);
553 goto err;
554 }
555
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800556 /* add new element to the head of the list, so that
557 * concurrent search will find it before old elem
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800558 */
559 hlist_add_head_rcu(&l_new->hash_node, head);
560 if (l_old) {
561 hlist_del_rcu(&l_old->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800562 free_htab_elem(htab, l_old);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800563 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800564 ret = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800565err:
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800566 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800567 return ret;
568}
569
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800570static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
571 void *value, u64 map_flags,
572 bool onallcpus)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800573{
574 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
575 struct htab_elem *l_new = NULL, *l_old;
576 struct hlist_head *head;
577 unsigned long flags;
578 struct bucket *b;
579 u32 key_size, hash;
580 int ret;
581
582 if (unlikely(map_flags > BPF_EXIST))
583 /* unknown flags */
584 return -EINVAL;
585
586 WARN_ON_ONCE(!rcu_read_lock_held());
587
588 key_size = map->key_size;
589
590 hash = htab_map_hash(key, key_size);
591
592 b = __select_bucket(htab, hash);
593 head = &b->head;
594
595 /* bpf_map_update_elem() can be called in_irq() */
596 raw_spin_lock_irqsave(&b->lock, flags);
597
598 l_old = lookup_elem_raw(head, hash, key, key_size);
599
600 ret = check_flags(htab, l_old, map_flags);
601 if (ret)
602 goto err;
603
604 if (l_old) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800605 void __percpu *pptr = htab_elem_get_ptr(l_old, key_size);
606 u32 size = htab->map.value_size;
607
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800608 /* per-cpu hash map can update value in-place */
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800609 if (!onallcpus) {
610 memcpy(this_cpu_ptr(pptr), value, size);
611 } else {
612 int off = 0, cpu;
613
614 size = round_up(size, 8);
615 for_each_possible_cpu(cpu) {
616 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
617 value + off, size);
618 off += size;
619 }
620 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800621 } else {
622 l_new = alloc_htab_elem(htab, key, value, key_size,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700623 hash, true, onallcpus, false);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800624 if (IS_ERR(l_new)) {
625 ret = PTR_ERR(l_new);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800626 goto err;
627 }
628 hlist_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800629 }
630 ret = 0;
631err:
632 raw_spin_unlock_irqrestore(&b->lock, flags);
633 return ret;
634}
635
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800636static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
637 void *value, u64 map_flags)
638{
639 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
640}
641
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800642/* Called from syscall or from eBPF program */
643static int htab_map_delete_elem(struct bpf_map *map, void *key)
644{
645 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
646 struct hlist_head *head;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800647 struct bucket *b;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800648 struct htab_elem *l;
649 unsigned long flags;
650 u32 hash, key_size;
651 int ret = -ENOENT;
652
653 WARN_ON_ONCE(!rcu_read_lock_held());
654
655 key_size = map->key_size;
656
657 hash = htab_map_hash(key, key_size);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800658 b = __select_bucket(htab, hash);
659 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800660
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800661 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800662
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800663 l = lookup_elem_raw(head, hash, key, key_size);
664
665 if (l) {
666 hlist_del_rcu(&l->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800667 free_htab_elem(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800668 ret = 0;
669 }
670
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800671 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800672 return ret;
673}
674
675static void delete_all_elements(struct bpf_htab *htab)
676{
677 int i;
678
679 for (i = 0; i < htab->n_buckets; i++) {
680 struct hlist_head *head = select_bucket(htab, i);
681 struct hlist_node *n;
682 struct htab_elem *l;
683
684 hlist_for_each_entry_safe(l, n, head, hash_node) {
685 hlist_del_rcu(&l->hash_node);
Daniel Borkmann483bed22016-11-04 00:01:19 +0100686 if (l->state != HTAB_EXTRA_ELEM_USED)
687 htab_elem_free(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800688 }
689 }
690}
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800691/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
692static void htab_map_free(struct bpf_map *map)
693{
694 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
695
696 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
697 * so the programs (can be more than one that used this map) were
698 * disconnected from events. Wait for outstanding critical sections in
699 * these programs to complete
700 */
701 synchronize_rcu();
702
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800703 /* some of free_htab_elem() callbacks for elements of this map may
704 * not have executed. Wait for them.
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800705 */
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800706 rcu_barrier();
707 if (htab->map.map_flags & BPF_F_NO_PREALLOC) {
708 delete_all_elements(htab);
709 } else {
710 htab_free_elems(htab);
711 pcpu_freelist_destroy(&htab->freelist);
712 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700713 free_percpu(htab->extra_elems);
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100714 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800715 kfree(htab);
716}
717
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100718static const struct bpf_map_ops htab_ops = {
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800719 .map_alloc = htab_map_alloc,
720 .map_free = htab_map_free,
721 .map_get_next_key = htab_map_get_next_key,
722 .map_lookup_elem = htab_map_lookup_elem,
723 .map_update_elem = htab_map_update_elem,
724 .map_delete_elem = htab_map_delete_elem,
725};
726
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100727static struct bpf_map_type_list htab_type __read_mostly = {
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800728 .ops = &htab_ops,
729 .type = BPF_MAP_TYPE_HASH,
730};
731
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800732/* Called from eBPF program */
733static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
734{
735 struct htab_elem *l = __htab_map_lookup_elem(map, key);
736
737 if (l)
738 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
739 else
740 return NULL;
741}
742
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800743int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
744{
745 struct htab_elem *l;
746 void __percpu *pptr;
747 int ret = -ENOENT;
748 int cpu, off = 0;
749 u32 size;
750
751 /* per_cpu areas are zero-filled and bpf programs can only
752 * access 'value_size' of them, so copying rounded areas
753 * will not leak any kernel data
754 */
755 size = round_up(map->value_size, 8);
756 rcu_read_lock();
757 l = __htab_map_lookup_elem(map, key);
758 if (!l)
759 goto out;
760 pptr = htab_elem_get_ptr(l, map->key_size);
761 for_each_possible_cpu(cpu) {
762 bpf_long_memcpy(value + off,
763 per_cpu_ptr(pptr, cpu), size);
764 off += size;
765 }
766 ret = 0;
767out:
768 rcu_read_unlock();
769 return ret;
770}
771
772int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
773 u64 map_flags)
774{
Sasha Levin6bbd9a02016-02-19 13:53:10 -0500775 int ret;
776
777 rcu_read_lock();
778 ret = __htab_percpu_map_update_elem(map, key, value, map_flags, true);
779 rcu_read_unlock();
780
781 return ret;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800782}
783
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800784static const struct bpf_map_ops htab_percpu_ops = {
785 .map_alloc = htab_map_alloc,
786 .map_free = htab_map_free,
787 .map_get_next_key = htab_map_get_next_key,
788 .map_lookup_elem = htab_percpu_map_lookup_elem,
789 .map_update_elem = htab_percpu_map_update_elem,
790 .map_delete_elem = htab_map_delete_elem,
791};
792
793static struct bpf_map_type_list htab_percpu_type __read_mostly = {
794 .ops = &htab_percpu_ops,
795 .type = BPF_MAP_TYPE_PERCPU_HASH,
796};
797
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800798static int __init register_htab_map(void)
799{
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100800 bpf_register_map_type(&htab_type);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800801 bpf_register_map_type(&htab_percpu_type);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800802 return 0;
803}
804late_initcall(register_htab_map);