blob: a36a532c056df4bbc86061c2f77208e5b18bba45 [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;
Teng Qinfcbc8d02017-04-24 19:00:37 -0700329 int i = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800330
331 WARN_ON_ONCE(!rcu_read_lock_held());
332
333 key_size = map->key_size;
334
Teng Qinfcbc8d02017-04-24 19:00:37 -0700335 if (!key)
336 goto find_first_elem;
337
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800338 hash = htab_map_hash(key, key_size);
339
340 head = select_bucket(htab, hash);
341
342 /* lookup the key */
343 l = lookup_elem_raw(head, hash, key, key_size);
344
Teng Qinfcbc8d02017-04-24 19:00:37 -0700345 if (!l)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800346 goto find_first_elem;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800347
348 /* key was found, get next key in the same bucket */
349 next_l = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&l->hash_node)),
350 struct htab_elem, hash_node);
351
352 if (next_l) {
353 /* if next elem in this hash list is non-zero, just return it */
354 memcpy(next_key, next_l->key, key_size);
355 return 0;
356 }
357
358 /* no more elements in this hash list, go to the next bucket */
359 i = hash & (htab->n_buckets - 1);
360 i++;
361
362find_first_elem:
363 /* iterate over buckets */
364 for (; i < htab->n_buckets; i++) {
365 head = select_bucket(htab, i);
366
367 /* pick first element in the bucket */
368 next_l = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
369 struct htab_elem, hash_node);
370 if (next_l) {
371 /* if it's not empty, just return it */
372 memcpy(next_key, next_l->key, key_size);
373 return 0;
374 }
375 }
376
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800377 /* iterated over all buckets and all elements */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800378 return -ENOENT;
379}
380
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800381static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800382{
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800383 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
384 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800385 kfree(l);
386}
387
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800388static void htab_elem_free_rcu(struct rcu_head *head)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800389{
390 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800391 struct bpf_htab *htab = l->htab;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800392
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800393 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
394 * we're calling kfree, otherwise deadlock is possible if kprobes
395 * are placed somewhere inside of slub
396 */
397 preempt_disable();
398 __this_cpu_inc(bpf_prog_active);
399 htab_elem_free(htab, l);
400 __this_cpu_dec(bpf_prog_active);
401 preempt_enable();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800402}
403
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800404static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800405{
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700406 if (l->state == HTAB_EXTRA_ELEM_USED) {
407 l->state = HTAB_EXTRA_ELEM_FREE;
408 return;
409 }
410
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800411 if (!(htab->map.map_flags & BPF_F_NO_PREALLOC)) {
412 pcpu_freelist_push(&htab->freelist, &l->fnode);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800413 } else {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800414 atomic_dec(&htab->count);
415 l->htab = htab;
416 call_rcu(&l->rcu, htab_elem_free_rcu);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800417 }
418}
419
420static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
421 void *value, u32 key_size, u32 hash,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700422 bool percpu, bool onallcpus,
423 bool old_elem_exists)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800424{
425 u32 size = htab->map.value_size;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800426 bool prealloc = !(htab->map.map_flags & BPF_F_NO_PREALLOC);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800427 struct htab_elem *l_new;
428 void __percpu *pptr;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700429 int err = 0;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800430
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800431 if (prealloc) {
432 l_new = (struct htab_elem *)pcpu_freelist_pop(&htab->freelist);
433 if (!l_new)
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700434 err = -E2BIG;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800435 } else {
436 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
437 atomic_dec(&htab->count);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700438 err = -E2BIG;
439 } else {
440 l_new = kmalloc(htab->elem_size,
441 GFP_ATOMIC | __GFP_NOWARN);
442 if (!l_new)
443 return ERR_PTR(-ENOMEM);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800444 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700445 }
446
447 if (err) {
448 if (!old_elem_exists)
449 return ERR_PTR(err);
450
451 /* if we're updating the existing element and the hash table
452 * is full, use per-cpu extra elems
453 */
454 l_new = this_cpu_ptr(htab->extra_elems);
455 if (l_new->state != HTAB_EXTRA_ELEM_FREE)
456 return ERR_PTR(-E2BIG);
457 l_new->state = HTAB_EXTRA_ELEM_USED;
458 } else {
459 l_new->state = HTAB_NOT_AN_EXTRA_ELEM;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800460 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800461
462 memcpy(l_new->key, key, key_size);
463 if (percpu) {
464 /* round up value_size to 8 bytes */
465 size = round_up(size, 8);
466
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800467 if (prealloc) {
468 pptr = htab_elem_get_ptr(l_new, key_size);
469 } else {
470 /* alloc_percpu zero-fills */
471 pptr = __alloc_percpu_gfp(size, 8,
472 GFP_ATOMIC | __GFP_NOWARN);
473 if (!pptr) {
474 kfree(l_new);
475 return ERR_PTR(-ENOMEM);
476 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800477 }
478
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800479 if (!onallcpus) {
480 /* copy true value_size bytes */
481 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
482 } else {
483 int off = 0, cpu;
484
485 for_each_possible_cpu(cpu) {
486 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
487 value + off, size);
488 off += size;
489 }
490 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800491 if (!prealloc)
492 htab_elem_set_ptr(l_new, key_size, pptr);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800493 } else {
494 memcpy(l_new->key + round_up(key_size, 8), value, size);
495 }
496
497 l_new->hash = hash;
498 return l_new;
499}
500
501static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
502 u64 map_flags)
503{
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800504 if (l_old && map_flags == BPF_NOEXIST)
505 /* elem already exists */
506 return -EEXIST;
507
508 if (!l_old && map_flags == BPF_EXIST)
509 /* elem doesn't exist, cannot update it */
510 return -ENOENT;
511
512 return 0;
513}
514
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800515/* Called from syscall or from eBPF program */
516static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
517 u64 map_flags)
518{
519 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800520 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800521 struct hlist_head *head;
522 unsigned long flags;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800523 struct bucket *b;
524 u32 key_size, hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800525 int ret;
526
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800527 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800528 /* unknown flags */
529 return -EINVAL;
530
531 WARN_ON_ONCE(!rcu_read_lock_held());
532
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800533 key_size = map->key_size;
534
535 hash = htab_map_hash(key, key_size);
536
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800537 b = __select_bucket(htab, hash);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800538 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800539
540 /* bpf_map_update_elem() can be called in_irq() */
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800541 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800542
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800543 l_old = lookup_elem_raw(head, hash, key, key_size);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800544
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800545 ret = check_flags(htab, l_old, map_flags);
546 if (ret)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800547 goto err;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800548
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700549 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
550 !!l_old);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800551 if (IS_ERR(l_new)) {
552 /* all pre-allocated elements are in use or memory exhausted */
553 ret = PTR_ERR(l_new);
554 goto err;
555 }
556
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800557 /* add new element to the head of the list, so that
558 * concurrent search will find it before old elem
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800559 */
560 hlist_add_head_rcu(&l_new->hash_node, head);
561 if (l_old) {
562 hlist_del_rcu(&l_old->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800563 free_htab_elem(htab, l_old);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800564 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800565 ret = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800566err:
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800567 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800568 return ret;
569}
570
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800571static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
572 void *value, u64 map_flags,
573 bool onallcpus)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800574{
575 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
576 struct htab_elem *l_new = NULL, *l_old;
577 struct hlist_head *head;
578 unsigned long flags;
579 struct bucket *b;
580 u32 key_size, hash;
581 int ret;
582
583 if (unlikely(map_flags > BPF_EXIST))
584 /* unknown flags */
585 return -EINVAL;
586
587 WARN_ON_ONCE(!rcu_read_lock_held());
588
589 key_size = map->key_size;
590
591 hash = htab_map_hash(key, key_size);
592
593 b = __select_bucket(htab, hash);
594 head = &b->head;
595
596 /* bpf_map_update_elem() can be called in_irq() */
597 raw_spin_lock_irqsave(&b->lock, flags);
598
599 l_old = lookup_elem_raw(head, hash, key, key_size);
600
601 ret = check_flags(htab, l_old, map_flags);
602 if (ret)
603 goto err;
604
605 if (l_old) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800606 void __percpu *pptr = htab_elem_get_ptr(l_old, key_size);
607 u32 size = htab->map.value_size;
608
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800609 /* per-cpu hash map can update value in-place */
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800610 if (!onallcpus) {
611 memcpy(this_cpu_ptr(pptr), value, size);
612 } else {
613 int off = 0, cpu;
614
615 size = round_up(size, 8);
616 for_each_possible_cpu(cpu) {
617 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
618 value + off, size);
619 off += size;
620 }
621 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800622 } else {
623 l_new = alloc_htab_elem(htab, key, value, key_size,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700624 hash, true, onallcpus, false);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800625 if (IS_ERR(l_new)) {
626 ret = PTR_ERR(l_new);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800627 goto err;
628 }
629 hlist_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800630 }
631 ret = 0;
632err:
633 raw_spin_unlock_irqrestore(&b->lock, flags);
634 return ret;
635}
636
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800637static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
638 void *value, u64 map_flags)
639{
640 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
641}
642
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800643/* Called from syscall or from eBPF program */
644static int htab_map_delete_elem(struct bpf_map *map, void *key)
645{
646 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
647 struct hlist_head *head;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800648 struct bucket *b;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800649 struct htab_elem *l;
650 unsigned long flags;
651 u32 hash, key_size;
652 int ret = -ENOENT;
653
654 WARN_ON_ONCE(!rcu_read_lock_held());
655
656 key_size = map->key_size;
657
658 hash = htab_map_hash(key, key_size);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800659 b = __select_bucket(htab, hash);
660 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800661
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800662 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800663
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800664 l = lookup_elem_raw(head, hash, key, key_size);
665
666 if (l) {
667 hlist_del_rcu(&l->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800668 free_htab_elem(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800669 ret = 0;
670 }
671
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800672 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800673 return ret;
674}
675
676static void delete_all_elements(struct bpf_htab *htab)
677{
678 int i;
679
680 for (i = 0; i < htab->n_buckets; i++) {
681 struct hlist_head *head = select_bucket(htab, i);
682 struct hlist_node *n;
683 struct htab_elem *l;
684
685 hlist_for_each_entry_safe(l, n, head, hash_node) {
686 hlist_del_rcu(&l->hash_node);
Daniel Borkmann483bed22016-11-04 00:01:19 +0100687 if (l->state != HTAB_EXTRA_ELEM_USED)
688 htab_elem_free(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800689 }
690 }
691}
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800692/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
693static void htab_map_free(struct bpf_map *map)
694{
695 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
696
697 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
698 * so the programs (can be more than one that used this map) were
699 * disconnected from events. Wait for outstanding critical sections in
700 * these programs to complete
701 */
702 synchronize_rcu();
703
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800704 /* some of free_htab_elem() callbacks for elements of this map may
705 * not have executed. Wait for them.
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800706 */
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800707 rcu_barrier();
708 if (htab->map.map_flags & BPF_F_NO_PREALLOC) {
709 delete_all_elements(htab);
710 } else {
711 htab_free_elems(htab);
712 pcpu_freelist_destroy(&htab->freelist);
713 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700714 free_percpu(htab->extra_elems);
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100715 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800716 kfree(htab);
717}
718
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100719static const struct bpf_map_ops htab_ops = {
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800720 .map_alloc = htab_map_alloc,
721 .map_free = htab_map_free,
722 .map_get_next_key = htab_map_get_next_key,
723 .map_lookup_elem = htab_map_lookup_elem,
724 .map_update_elem = htab_map_update_elem,
725 .map_delete_elem = htab_map_delete_elem,
726};
727
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100728static struct bpf_map_type_list htab_type __read_mostly = {
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800729 .ops = &htab_ops,
730 .type = BPF_MAP_TYPE_HASH,
731};
732
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800733/* Called from eBPF program */
734static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
735{
736 struct htab_elem *l = __htab_map_lookup_elem(map, key);
737
738 if (l)
739 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
740 else
741 return NULL;
742}
743
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800744int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
745{
746 struct htab_elem *l;
747 void __percpu *pptr;
748 int ret = -ENOENT;
749 int cpu, off = 0;
750 u32 size;
751
752 /* per_cpu areas are zero-filled and bpf programs can only
753 * access 'value_size' of them, so copying rounded areas
754 * will not leak any kernel data
755 */
756 size = round_up(map->value_size, 8);
757 rcu_read_lock();
758 l = __htab_map_lookup_elem(map, key);
759 if (!l)
760 goto out;
761 pptr = htab_elem_get_ptr(l, map->key_size);
762 for_each_possible_cpu(cpu) {
763 bpf_long_memcpy(value + off,
764 per_cpu_ptr(pptr, cpu), size);
765 off += size;
766 }
767 ret = 0;
768out:
769 rcu_read_unlock();
770 return ret;
771}
772
773int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
774 u64 map_flags)
775{
Sasha Levin6bbd9a02016-02-19 13:53:10 -0500776 int ret;
777
778 rcu_read_lock();
779 ret = __htab_percpu_map_update_elem(map, key, value, map_flags, true);
780 rcu_read_unlock();
781
782 return ret;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800783}
784
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800785static const struct bpf_map_ops htab_percpu_ops = {
786 .map_alloc = htab_map_alloc,
787 .map_free = htab_map_free,
788 .map_get_next_key = htab_map_get_next_key,
789 .map_lookup_elem = htab_percpu_map_lookup_elem,
790 .map_update_elem = htab_percpu_map_update_elem,
791 .map_delete_elem = htab_map_delete_elem,
792};
793
794static struct bpf_map_type_list htab_percpu_type __read_mostly = {
795 .ops = &htab_percpu_ops,
796 .type = BPF_MAP_TYPE_PERCPU_HASH,
797};
798
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800799static int __init register_htab_map(void)
800{
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100801 bpf_register_map_type(&htab_type);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800802 bpf_register_map_type(&htab_percpu_type);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800803 return 0;
804}
805late_initcall(register_htab_map);