blob: 27f4f2c8b1eec8f8d50a677b9ede5d6fb37ec707 [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"
Chenbo Feng4672ded2017-10-18 13:00:22 -070017#define HTAB_CREATE_FLAG_MASK \
18 (BPF_F_NO_PREALLOC | BPF_F_RDONLY | BPF_F_WRONLY)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080019
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080020struct bucket {
21 struct hlist_head head;
22 raw_spinlock_t lock;
23};
24
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080025struct bpf_htab {
26 struct bpf_map map;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +080027 struct bucket *buckets;
Alexei Starovoitov6c905982016-03-07 21:57:15 -080028 void *elems;
29 struct pcpu_freelist freelist;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070030 void __percpu *extra_elems;
tom.leiming@gmail.com6591f1e2015-12-29 22:40:25 +080031 atomic_t count; /* number of elements in this hashtable */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080032 u32 n_buckets; /* number of hash buckets */
33 u32 elem_size; /* size of each element in bytes */
34};
35
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070036enum extra_elem_state {
37 HTAB_NOT_AN_EXTRA_ELEM = 0,
38 HTAB_EXTRA_ELEM_FREE,
39 HTAB_EXTRA_ELEM_USED
40};
41
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080042/* each htab element is struct htab_elem + key + value */
43struct htab_elem {
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -080044 union {
Alexei Starovoitov6c905982016-03-07 21:57:15 -080045 struct hlist_node hash_node;
46 struct bpf_htab *htab;
47 struct pcpu_freelist_node fnode;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -080048 };
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -070049 union {
50 struct rcu_head rcu;
51 enum extra_elem_state state;
52 };
Alexei Starovoitov6c905982016-03-07 21:57:15 -080053 u32 hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -080054 char key[0] __aligned(8);
55};
56
Alexei Starovoitov6c905982016-03-07 21:57:15 -080057static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
58 void __percpu *pptr)
59{
60 *(void __percpu **)(l->key + key_size) = pptr;
61}
62
63static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
64{
65 return *(void __percpu **)(l->key + key_size);
66}
67
68static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
69{
70 return (struct htab_elem *) (htab->elems + i * htab->elem_size);
71}
72
73static void htab_free_elems(struct bpf_htab *htab)
74{
75 int i;
76
77 if (htab->map.map_type != BPF_MAP_TYPE_PERCPU_HASH)
78 goto free_elems;
79
80 for (i = 0; i < htab->map.max_entries; i++) {
81 void __percpu *pptr;
82
83 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
84 htab->map.key_size);
85 free_percpu(pptr);
86 }
87free_elems:
Daniel Borkmann251d00b2017-01-18 15:14:17 +010088 bpf_map_area_free(htab->elems);
Alexei Starovoitov6c905982016-03-07 21:57:15 -080089}
90
91static int prealloc_elems_and_freelist(struct bpf_htab *htab)
92{
93 int err = -ENOMEM, i;
94
Daniel Borkmann251d00b2017-01-18 15:14:17 +010095 htab->elems = bpf_map_area_alloc(htab->elem_size *
96 htab->map.max_entries);
Alexei Starovoitov6c905982016-03-07 21:57:15 -080097 if (!htab->elems)
98 return -ENOMEM;
99
100 if (htab->map.map_type != BPF_MAP_TYPE_PERCPU_HASH)
101 goto skip_percpu_elems;
102
103 for (i = 0; i < htab->map.max_entries; i++) {
104 u32 size = round_up(htab->map.value_size, 8);
105 void __percpu *pptr;
106
107 pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
108 if (!pptr)
109 goto free_elems;
110 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
111 pptr);
112 }
113
114skip_percpu_elems:
115 err = pcpu_freelist_init(&htab->freelist);
116 if (err)
117 goto free_elems;
118
119 pcpu_freelist_populate(&htab->freelist, htab->elems, htab->elem_size,
120 htab->map.max_entries);
121 return 0;
122
123free_elems:
124 htab_free_elems(htab);
125 return err;
126}
127
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700128static int alloc_extra_elems(struct bpf_htab *htab)
129{
130 void __percpu *pptr;
131 int cpu;
132
133 pptr = __alloc_percpu_gfp(htab->elem_size, 8, GFP_USER | __GFP_NOWARN);
134 if (!pptr)
135 return -ENOMEM;
136
137 for_each_possible_cpu(cpu) {
138 ((struct htab_elem *)per_cpu_ptr(pptr, cpu))->state =
139 HTAB_EXTRA_ELEM_FREE;
140 }
141 htab->extra_elems = pptr;
142 return 0;
143}
144
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800145/* Called from syscall */
146static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
147{
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800148 bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_HASH;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800149 struct bpf_htab *htab;
150 int err, i;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800151 u64 cost;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800152
Chenbo Feng4672ded2017-10-18 13:00:22 -0700153 if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK)
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800154 /* reserved bits should not be used */
155 return ERR_PTR(-EINVAL);
156
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800157 htab = kzalloc(sizeof(*htab), GFP_USER);
158 if (!htab)
159 return ERR_PTR(-ENOMEM);
160
161 /* mandatory map attributes */
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800162 htab->map.map_type = attr->map_type;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800163 htab->map.key_size = attr->key_size;
164 htab->map.value_size = attr->value_size;
165 htab->map.max_entries = attr->max_entries;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800166 htab->map.map_flags = attr->map_flags;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800167
168 /* check sanity of attributes.
169 * value_size == 0 may be allowed in the future to use map as a set
170 */
171 err = -EINVAL;
172 if (htab->map.max_entries == 0 || htab->map.key_size == 0 ||
173 htab->map.value_size == 0)
174 goto free_htab;
175
176 /* hash table size must be power of 2 */
177 htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
178
179 err = -E2BIG;
180 if (htab->map.key_size > MAX_BPF_STACK)
181 /* eBPF programs initialize keys on stack, so they cannot be
182 * larger than max stack size
183 */
184 goto free_htab;
185
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800186 if (htab->map.value_size >= (1 << (KMALLOC_SHIFT_MAX - 1)) -
187 MAX_BPF_STACK - sizeof(struct htab_elem))
188 /* if value_size is bigger, the user space won't be able to
189 * access the elements via bpf syscall. This check also makes
190 * sure that the elem_size doesn't overflow and it's
191 * kmalloc-able later in htab_map_update_elem()
192 */
193 goto free_htab;
194
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800195 if (percpu && round_up(htab->map.value_size, 8) > PCPU_MIN_UNIT_SIZE)
196 /* make sure the size for pcpu_alloc() is reasonable */
197 goto free_htab;
198
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800199 htab->elem_size = sizeof(struct htab_elem) +
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800200 round_up(htab->map.key_size, 8);
201 if (percpu)
202 htab->elem_size += sizeof(void *);
203 else
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800204 htab->elem_size += round_up(htab->map.value_size, 8);
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800205
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800206 /* prevent zero size kmalloc and check for u32 overflow */
207 if (htab->n_buckets == 0 ||
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800208 htab->n_buckets > U32_MAX / sizeof(struct bucket))
Alexei Starovoitovdaaf4272014-11-18 17:32:16 -0800209 goto free_htab;
210
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800211 cost = (u64) htab->n_buckets * sizeof(struct bucket) +
212 (u64) htab->elem_size * htab->map.max_entries;
213
214 if (percpu)
215 cost += (u64) round_up(htab->map.value_size, 8) *
216 num_possible_cpus() * htab->map.max_entries;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700217 else
218 cost += (u64) htab->elem_size * num_possible_cpus();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800219
220 if (cost >= U32_MAX - PAGE_SIZE)
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800221 /* make sure page count doesn't overflow */
222 goto free_htab;
223
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800224 htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800225
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800226 /* if map size is larger than memlock limit, reject it early */
227 err = bpf_map_precharge_memlock(htab->map.pages);
228 if (err)
229 goto free_htab;
230
Alexei Starovoitov01b3f522015-11-29 16:59:35 -0800231 err = -ENOMEM;
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100232 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
233 sizeof(struct bucket));
234 if (!htab->buckets)
235 goto free_htab;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800236
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800237 for (i = 0; i < htab->n_buckets; i++) {
238 INIT_HLIST_HEAD(&htab->buckets[i].head);
239 raw_spin_lock_init(&htab->buckets[i].lock);
240 }
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800241
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700242 if (!percpu) {
243 err = alloc_extra_elems(htab);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800244 if (err)
245 goto free_buckets;
246 }
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800247
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700248 if (!(attr->map_flags & BPF_F_NO_PREALLOC)) {
249 err = prealloc_elems_and_freelist(htab);
250 if (err)
251 goto free_extra_elems;
252 }
253
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800254 return &htab->map;
255
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700256free_extra_elems:
257 free_percpu(htab->extra_elems);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800258free_buckets:
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100259 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800260free_htab:
261 kfree(htab);
262 return ERR_PTR(err);
263}
264
265static inline u32 htab_map_hash(const void *key, u32 key_len)
266{
267 return jhash(key, key_len, 0);
268}
269
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800270static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800271{
272 return &htab->buckets[hash & (htab->n_buckets - 1)];
273}
274
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800275static inline struct hlist_head *select_bucket(struct bpf_htab *htab, u32 hash)
276{
277 return &__select_bucket(htab, hash)->head;
278}
279
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800280static struct htab_elem *lookup_elem_raw(struct hlist_head *head, u32 hash,
281 void *key, u32 key_size)
282{
283 struct htab_elem *l;
284
285 hlist_for_each_entry_rcu(l, head, hash_node)
286 if (l->hash == hash && !memcmp(&l->key, key, key_size))
287 return l;
288
289 return NULL;
290}
291
292/* Called from syscall or from eBPF program */
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800293static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800294{
295 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
296 struct hlist_head *head;
297 struct htab_elem *l;
298 u32 hash, key_size;
299
300 /* Must be called with rcu_read_lock. */
301 WARN_ON_ONCE(!rcu_read_lock_held());
302
303 key_size = map->key_size;
304
305 hash = htab_map_hash(key, key_size);
306
307 head = select_bucket(htab, hash);
308
309 l = lookup_elem_raw(head, hash, key, key_size);
310
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800311 return l;
312}
313
314static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
315{
316 struct htab_elem *l = __htab_map_lookup_elem(map, key);
317
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800318 if (l)
319 return l->key + round_up(map->key_size, 8);
320
321 return NULL;
322}
323
324/* Called from syscall */
325static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
326{
327 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
328 struct hlist_head *head;
329 struct htab_elem *l, *next_l;
330 u32 hash, key_size;
331 int i;
332
333 WARN_ON_ONCE(!rcu_read_lock_held());
334
335 key_size = map->key_size;
336
337 hash = htab_map_hash(key, key_size);
338
339 head = select_bucket(htab, hash);
340
341 /* lookup the key */
342 l = lookup_elem_raw(head, hash, key, key_size);
343
344 if (!l) {
345 i = 0;
346 goto find_first_elem;
347 }
348
349 /* key was found, get next key in the same bucket */
350 next_l = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&l->hash_node)),
351 struct htab_elem, hash_node);
352
353 if (next_l) {
354 /* if next elem in this hash list is non-zero, just return it */
355 memcpy(next_key, next_l->key, key_size);
356 return 0;
357 }
358
359 /* no more elements in this hash list, go to the next bucket */
360 i = hash & (htab->n_buckets - 1);
361 i++;
362
363find_first_elem:
364 /* iterate over buckets */
365 for (; i < htab->n_buckets; i++) {
366 head = select_bucket(htab, i);
367
368 /* pick first element in the bucket */
369 next_l = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
370 struct htab_elem, hash_node);
371 if (next_l) {
372 /* if it's not empty, just return it */
373 memcpy(next_key, next_l->key, key_size);
374 return 0;
375 }
376 }
377
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800378 /* iterated over all buckets and all elements */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800379 return -ENOENT;
380}
381
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800382static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800383{
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800384 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
385 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800386 kfree(l);
387}
388
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800389static void htab_elem_free_rcu(struct rcu_head *head)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800390{
391 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800392 struct bpf_htab *htab = l->htab;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800393
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800394 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
395 * we're calling kfree, otherwise deadlock is possible if kprobes
396 * are placed somewhere inside of slub
397 */
398 preempt_disable();
399 __this_cpu_inc(bpf_prog_active);
400 htab_elem_free(htab, l);
401 __this_cpu_dec(bpf_prog_active);
402 preempt_enable();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800403}
404
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800405static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800406{
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700407 if (l->state == HTAB_EXTRA_ELEM_USED) {
408 l->state = HTAB_EXTRA_ELEM_FREE;
409 return;
410 }
411
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800412 if (!(htab->map.map_flags & BPF_F_NO_PREALLOC)) {
413 pcpu_freelist_push(&htab->freelist, &l->fnode);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800414 } else {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800415 atomic_dec(&htab->count);
416 l->htab = htab;
417 call_rcu(&l->rcu, htab_elem_free_rcu);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800418 }
419}
420
421static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
422 void *value, u32 key_size, u32 hash,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700423 bool percpu, bool onallcpus,
424 bool old_elem_exists)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800425{
426 u32 size = htab->map.value_size;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800427 bool prealloc = !(htab->map.map_flags & BPF_F_NO_PREALLOC);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800428 struct htab_elem *l_new;
429 void __percpu *pptr;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700430 int err = 0;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800431
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800432 if (prealloc) {
433 l_new = (struct htab_elem *)pcpu_freelist_pop(&htab->freelist);
434 if (!l_new)
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700435 err = -E2BIG;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800436 } else {
437 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
438 atomic_dec(&htab->count);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700439 err = -E2BIG;
440 } else {
441 l_new = kmalloc(htab->elem_size,
442 GFP_ATOMIC | __GFP_NOWARN);
443 if (!l_new)
444 return ERR_PTR(-ENOMEM);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800445 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700446 }
447
448 if (err) {
449 if (!old_elem_exists)
450 return ERR_PTR(err);
451
452 /* if we're updating the existing element and the hash table
453 * is full, use per-cpu extra elems
454 */
455 l_new = this_cpu_ptr(htab->extra_elems);
456 if (l_new->state != HTAB_EXTRA_ELEM_FREE)
457 return ERR_PTR(-E2BIG);
458 l_new->state = HTAB_EXTRA_ELEM_USED;
459 } else {
460 l_new->state = HTAB_NOT_AN_EXTRA_ELEM;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800461 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800462
463 memcpy(l_new->key, key, key_size);
464 if (percpu) {
465 /* round up value_size to 8 bytes */
466 size = round_up(size, 8);
467
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800468 if (prealloc) {
469 pptr = htab_elem_get_ptr(l_new, key_size);
470 } else {
471 /* alloc_percpu zero-fills */
472 pptr = __alloc_percpu_gfp(size, 8,
473 GFP_ATOMIC | __GFP_NOWARN);
474 if (!pptr) {
475 kfree(l_new);
476 return ERR_PTR(-ENOMEM);
477 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800478 }
479
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800480 if (!onallcpus) {
481 /* copy true value_size bytes */
482 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
483 } else {
484 int off = 0, cpu;
485
486 for_each_possible_cpu(cpu) {
487 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
488 value + off, size);
489 off += size;
490 }
491 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800492 if (!prealloc)
493 htab_elem_set_ptr(l_new, key_size, pptr);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800494 } else {
495 memcpy(l_new->key + round_up(key_size, 8), value, size);
496 }
497
498 l_new->hash = hash;
499 return l_new;
500}
501
502static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
503 u64 map_flags)
504{
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800505 if (l_old && map_flags == BPF_NOEXIST)
506 /* elem already exists */
507 return -EEXIST;
508
509 if (!l_old && map_flags == BPF_EXIST)
510 /* elem doesn't exist, cannot update it */
511 return -ENOENT;
512
513 return 0;
514}
515
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800516/* Called from syscall or from eBPF program */
517static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
518 u64 map_flags)
519{
520 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800521 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800522 struct hlist_head *head;
523 unsigned long flags;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800524 struct bucket *b;
525 u32 key_size, hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800526 int ret;
527
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800528 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800529 /* unknown flags */
530 return -EINVAL;
531
532 WARN_ON_ONCE(!rcu_read_lock_held());
533
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800534 key_size = map->key_size;
535
536 hash = htab_map_hash(key, key_size);
537
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800538 b = __select_bucket(htab, hash);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800539 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800540
541 /* bpf_map_update_elem() can be called in_irq() */
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800542 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800543
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800544 l_old = lookup_elem_raw(head, hash, key, key_size);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800545
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800546 ret = check_flags(htab, l_old, map_flags);
547 if (ret)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800548 goto err;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800549
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700550 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
551 !!l_old);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800552 if (IS_ERR(l_new)) {
553 /* all pre-allocated elements are in use or memory exhausted */
554 ret = PTR_ERR(l_new);
555 goto err;
556 }
557
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800558 /* add new element to the head of the list, so that
559 * concurrent search will find it before old elem
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800560 */
561 hlist_add_head_rcu(&l_new->hash_node, head);
562 if (l_old) {
563 hlist_del_rcu(&l_old->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800564 free_htab_elem(htab, l_old);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800565 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800566 ret = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800567err:
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800568 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800569 return ret;
570}
571
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800572static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
573 void *value, u64 map_flags,
574 bool onallcpus)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800575{
576 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
577 struct htab_elem *l_new = NULL, *l_old;
578 struct hlist_head *head;
579 unsigned long flags;
580 struct bucket *b;
581 u32 key_size, hash;
582 int ret;
583
584 if (unlikely(map_flags > BPF_EXIST))
585 /* unknown flags */
586 return -EINVAL;
587
588 WARN_ON_ONCE(!rcu_read_lock_held());
589
590 key_size = map->key_size;
591
592 hash = htab_map_hash(key, key_size);
593
594 b = __select_bucket(htab, hash);
595 head = &b->head;
596
597 /* bpf_map_update_elem() can be called in_irq() */
598 raw_spin_lock_irqsave(&b->lock, flags);
599
600 l_old = lookup_elem_raw(head, hash, key, key_size);
601
602 ret = check_flags(htab, l_old, map_flags);
603 if (ret)
604 goto err;
605
606 if (l_old) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800607 void __percpu *pptr = htab_elem_get_ptr(l_old, key_size);
608 u32 size = htab->map.value_size;
609
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800610 /* per-cpu hash map can update value in-place */
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800611 if (!onallcpus) {
612 memcpy(this_cpu_ptr(pptr), value, size);
613 } else {
614 int off = 0, cpu;
615
616 size = round_up(size, 8);
617 for_each_possible_cpu(cpu) {
618 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
619 value + off, size);
620 off += size;
621 }
622 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800623 } else {
624 l_new = alloc_htab_elem(htab, key, value, key_size,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700625 hash, true, onallcpus, false);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800626 if (IS_ERR(l_new)) {
627 ret = PTR_ERR(l_new);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800628 goto err;
629 }
630 hlist_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800631 }
632 ret = 0;
633err:
634 raw_spin_unlock_irqrestore(&b->lock, flags);
635 return ret;
636}
637
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800638static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
639 void *value, u64 map_flags)
640{
641 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
642}
643
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800644/* Called from syscall or from eBPF program */
645static int htab_map_delete_elem(struct bpf_map *map, void *key)
646{
647 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
648 struct hlist_head *head;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800649 struct bucket *b;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800650 struct htab_elem *l;
651 unsigned long flags;
652 u32 hash, key_size;
653 int ret = -ENOENT;
654
655 WARN_ON_ONCE(!rcu_read_lock_held());
656
657 key_size = map->key_size;
658
659 hash = htab_map_hash(key, key_size);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800660 b = __select_bucket(htab, hash);
661 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800662
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800663 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800664
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800665 l = lookup_elem_raw(head, hash, key, key_size);
666
667 if (l) {
668 hlist_del_rcu(&l->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800669 free_htab_elem(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800670 ret = 0;
671 }
672
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800673 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800674 return ret;
675}
676
677static void delete_all_elements(struct bpf_htab *htab)
678{
679 int i;
680
681 for (i = 0; i < htab->n_buckets; i++) {
682 struct hlist_head *head = select_bucket(htab, i);
683 struct hlist_node *n;
684 struct htab_elem *l;
685
686 hlist_for_each_entry_safe(l, n, head, hash_node) {
687 hlist_del_rcu(&l->hash_node);
Daniel Borkmann483bed22016-11-04 00:01:19 +0100688 if (l->state != HTAB_EXTRA_ELEM_USED)
689 htab_elem_free(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800690 }
691 }
692}
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800693/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
694static void htab_map_free(struct bpf_map *map)
695{
696 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
697
698 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
699 * so the programs (can be more than one that used this map) were
700 * disconnected from events. Wait for outstanding critical sections in
701 * these programs to complete
702 */
703 synchronize_rcu();
704
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800705 /* some of free_htab_elem() callbacks for elements of this map may
706 * not have executed. Wait for them.
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800707 */
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800708 rcu_barrier();
709 if (htab->map.map_flags & BPF_F_NO_PREALLOC) {
710 delete_all_elements(htab);
711 } else {
712 htab_free_elems(htab);
713 pcpu_freelist_destroy(&htab->freelist);
714 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700715 free_percpu(htab->extra_elems);
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100716 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800717 kfree(htab);
718}
719
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100720static const struct bpf_map_ops htab_ops = {
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800721 .map_alloc = htab_map_alloc,
722 .map_free = htab_map_free,
723 .map_get_next_key = htab_map_get_next_key,
724 .map_lookup_elem = htab_map_lookup_elem,
725 .map_update_elem = htab_map_update_elem,
726 .map_delete_elem = htab_map_delete_elem,
727};
728
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100729static struct bpf_map_type_list htab_type __read_mostly = {
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800730 .ops = &htab_ops,
731 .type = BPF_MAP_TYPE_HASH,
732};
733
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800734/* Called from eBPF program */
735static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
736{
737 struct htab_elem *l = __htab_map_lookup_elem(map, key);
738
739 if (l)
740 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
741 else
742 return NULL;
743}
744
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800745int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
746{
747 struct htab_elem *l;
748 void __percpu *pptr;
749 int ret = -ENOENT;
750 int cpu, off = 0;
751 u32 size;
752
753 /* per_cpu areas are zero-filled and bpf programs can only
754 * access 'value_size' of them, so copying rounded areas
755 * will not leak any kernel data
756 */
757 size = round_up(map->value_size, 8);
758 rcu_read_lock();
759 l = __htab_map_lookup_elem(map, key);
760 if (!l)
761 goto out;
762 pptr = htab_elem_get_ptr(l, map->key_size);
763 for_each_possible_cpu(cpu) {
764 bpf_long_memcpy(value + off,
765 per_cpu_ptr(pptr, cpu), size);
766 off += size;
767 }
768 ret = 0;
769out:
770 rcu_read_unlock();
771 return ret;
772}
773
774int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
775 u64 map_flags)
776{
Sasha Levin6bbd9a02016-02-19 13:53:10 -0500777 int ret;
778
779 rcu_read_lock();
780 ret = __htab_percpu_map_update_elem(map, key, value, map_flags, true);
781 rcu_read_unlock();
782
783 return ret;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800784}
785
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800786static const struct bpf_map_ops htab_percpu_ops = {
787 .map_alloc = htab_map_alloc,
788 .map_free = htab_map_free,
789 .map_get_next_key = htab_map_get_next_key,
790 .map_lookup_elem = htab_percpu_map_lookup_elem,
791 .map_update_elem = htab_percpu_map_update_elem,
792 .map_delete_elem = htab_map_delete_elem,
793};
794
795static struct bpf_map_type_list htab_percpu_type __read_mostly = {
796 .ops = &htab_percpu_ops,
797 .type = BPF_MAP_TYPE_PERCPU_HASH,
798};
799
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800800static int __init register_htab_map(void)
801{
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100802 bpf_register_map_type(&htab_type);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800803 bpf_register_map_type(&htab_percpu_type);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800804 return 0;
805}
806late_initcall(register_htab_map);