blob: 9c86d5d180bb9a3f4a0b4202e1341473d1844633 [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;
Teng Qinfcbc8d02017-04-24 19:00:37 -0700331 int i = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800332
333 WARN_ON_ONCE(!rcu_read_lock_held());
334
335 key_size = map->key_size;
336
Teng Qinfcbc8d02017-04-24 19:00:37 -0700337 if (!key)
338 goto find_first_elem;
339
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800340 hash = htab_map_hash(key, key_size);
341
342 head = select_bucket(htab, hash);
343
344 /* lookup the key */
345 l = lookup_elem_raw(head, hash, key, key_size);
346
Teng Qinfcbc8d02017-04-24 19:00:37 -0700347 if (!l)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800348 goto find_first_elem;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800349
350 /* key was found, get next key in the same bucket */
351 next_l = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&l->hash_node)),
352 struct htab_elem, hash_node);
353
354 if (next_l) {
355 /* if next elem in this hash list is non-zero, just return it */
356 memcpy(next_key, next_l->key, key_size);
357 return 0;
358 }
359
360 /* no more elements in this hash list, go to the next bucket */
361 i = hash & (htab->n_buckets - 1);
362 i++;
363
364find_first_elem:
365 /* iterate over buckets */
366 for (; i < htab->n_buckets; i++) {
367 head = select_bucket(htab, i);
368
369 /* pick first element in the bucket */
370 next_l = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
371 struct htab_elem, hash_node);
372 if (next_l) {
373 /* if it's not empty, just return it */
374 memcpy(next_key, next_l->key, key_size);
375 return 0;
376 }
377 }
378
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800379 /* iterated over all buckets and all elements */
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800380 return -ENOENT;
381}
382
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800383static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800384{
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800385 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
386 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800387 kfree(l);
388}
389
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800390static void htab_elem_free_rcu(struct rcu_head *head)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800391{
392 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800393 struct bpf_htab *htab = l->htab;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800394
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800395 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
396 * we're calling kfree, otherwise deadlock is possible if kprobes
397 * are placed somewhere inside of slub
398 */
399 preempt_disable();
400 __this_cpu_inc(bpf_prog_active);
401 htab_elem_free(htab, l);
402 __this_cpu_dec(bpf_prog_active);
403 preempt_enable();
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800404}
405
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800406static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800407{
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700408 if (l->state == HTAB_EXTRA_ELEM_USED) {
409 l->state = HTAB_EXTRA_ELEM_FREE;
410 return;
411 }
412
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800413 if (!(htab->map.map_flags & BPF_F_NO_PREALLOC)) {
414 pcpu_freelist_push(&htab->freelist, &l->fnode);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800415 } else {
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800416 atomic_dec(&htab->count);
417 l->htab = htab;
418 call_rcu(&l->rcu, htab_elem_free_rcu);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800419 }
420}
421
422static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
423 void *value, u32 key_size, u32 hash,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700424 bool percpu, bool onallcpus,
425 bool old_elem_exists)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800426{
427 u32 size = htab->map.value_size;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800428 bool prealloc = !(htab->map.map_flags & BPF_F_NO_PREALLOC);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800429 struct htab_elem *l_new;
430 void __percpu *pptr;
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700431 int err = 0;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800432
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800433 if (prealloc) {
434 l_new = (struct htab_elem *)pcpu_freelist_pop(&htab->freelist);
435 if (!l_new)
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700436 err = -E2BIG;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800437 } else {
438 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
439 atomic_dec(&htab->count);
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700440 err = -E2BIG;
441 } else {
442 l_new = kmalloc(htab->elem_size,
443 GFP_ATOMIC | __GFP_NOWARN);
444 if (!l_new)
445 return ERR_PTR(-ENOMEM);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800446 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700447 }
448
449 if (err) {
450 if (!old_elem_exists)
451 return ERR_PTR(err);
452
453 /* if we're updating the existing element and the hash table
454 * is full, use per-cpu extra elems
455 */
456 l_new = this_cpu_ptr(htab->extra_elems);
457 if (l_new->state != HTAB_EXTRA_ELEM_FREE)
458 return ERR_PTR(-E2BIG);
459 l_new->state = HTAB_EXTRA_ELEM_USED;
460 } else {
461 l_new->state = HTAB_NOT_AN_EXTRA_ELEM;
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800462 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800463
464 memcpy(l_new->key, key, key_size);
465 if (percpu) {
466 /* round up value_size to 8 bytes */
467 size = round_up(size, 8);
468
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800469 if (prealloc) {
470 pptr = htab_elem_get_ptr(l_new, key_size);
471 } else {
472 /* alloc_percpu zero-fills */
473 pptr = __alloc_percpu_gfp(size, 8,
474 GFP_ATOMIC | __GFP_NOWARN);
475 if (!pptr) {
476 kfree(l_new);
477 return ERR_PTR(-ENOMEM);
478 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800479 }
480
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800481 if (!onallcpus) {
482 /* copy true value_size bytes */
483 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
484 } else {
485 int off = 0, cpu;
486
487 for_each_possible_cpu(cpu) {
488 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
489 value + off, size);
490 off += size;
491 }
492 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800493 if (!prealloc)
494 htab_elem_set_ptr(l_new, key_size, pptr);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800495 } else {
496 memcpy(l_new->key + round_up(key_size, 8), value, size);
497 }
498
499 l_new->hash = hash;
500 return l_new;
501}
502
503static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
504 u64 map_flags)
505{
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800506 if (l_old && map_flags == BPF_NOEXIST)
507 /* elem already exists */
508 return -EEXIST;
509
510 if (!l_old && map_flags == BPF_EXIST)
511 /* elem doesn't exist, cannot update it */
512 return -ENOENT;
513
514 return 0;
515}
516
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800517/* Called from syscall or from eBPF program */
518static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
519 u64 map_flags)
520{
521 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800522 struct htab_elem *l_new = NULL, *l_old;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800523 struct hlist_head *head;
524 unsigned long flags;
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800525 struct bucket *b;
526 u32 key_size, hash;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800527 int ret;
528
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800529 if (unlikely(map_flags > BPF_EXIST))
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800530 /* unknown flags */
531 return -EINVAL;
532
533 WARN_ON_ONCE(!rcu_read_lock_held());
534
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800535 key_size = map->key_size;
536
537 hash = htab_map_hash(key, key_size);
538
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800539 b = __select_bucket(htab, hash);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800540 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800541
542 /* bpf_map_update_elem() can be called in_irq() */
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800543 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800544
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800545 l_old = lookup_elem_raw(head, hash, key, key_size);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800546
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800547 ret = check_flags(htab, l_old, map_flags);
548 if (ret)
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800549 goto err;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800550
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700551 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
552 !!l_old);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800553 if (IS_ERR(l_new)) {
554 /* all pre-allocated elements are in use or memory exhausted */
555 ret = PTR_ERR(l_new);
556 goto err;
557 }
558
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800559 /* add new element to the head of the list, so that
560 * concurrent search will find it before old elem
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800561 */
562 hlist_add_head_rcu(&l_new->hash_node, head);
563 if (l_old) {
564 hlist_del_rcu(&l_old->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800565 free_htab_elem(htab, l_old);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800566 }
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800567 ret = 0;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800568err:
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800569 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800570 return ret;
571}
572
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800573static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
574 void *value, u64 map_flags,
575 bool onallcpus)
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800576{
577 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
578 struct htab_elem *l_new = NULL, *l_old;
579 struct hlist_head *head;
580 unsigned long flags;
581 struct bucket *b;
582 u32 key_size, hash;
583 int ret;
584
585 if (unlikely(map_flags > BPF_EXIST))
586 /* unknown flags */
587 return -EINVAL;
588
589 WARN_ON_ONCE(!rcu_read_lock_held());
590
591 key_size = map->key_size;
592
593 hash = htab_map_hash(key, key_size);
594
595 b = __select_bucket(htab, hash);
596 head = &b->head;
597
598 /* bpf_map_update_elem() can be called in_irq() */
599 raw_spin_lock_irqsave(&b->lock, flags);
600
601 l_old = lookup_elem_raw(head, hash, key, key_size);
602
603 ret = check_flags(htab, l_old, map_flags);
604 if (ret)
605 goto err;
606
607 if (l_old) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800608 void __percpu *pptr = htab_elem_get_ptr(l_old, key_size);
609 u32 size = htab->map.value_size;
610
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800611 /* per-cpu hash map can update value in-place */
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800612 if (!onallcpus) {
613 memcpy(this_cpu_ptr(pptr), value, size);
614 } else {
615 int off = 0, cpu;
616
617 size = round_up(size, 8);
618 for_each_possible_cpu(cpu) {
619 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
620 value + off, size);
621 off += size;
622 }
623 }
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800624 } else {
625 l_new = alloc_htab_elem(htab, key, value, key_size,
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700626 hash, true, onallcpus, false);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800627 if (IS_ERR(l_new)) {
628 ret = PTR_ERR(l_new);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800629 goto err;
630 }
631 hlist_add_head_rcu(&l_new->hash_node, head);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800632 }
633 ret = 0;
634err:
635 raw_spin_unlock_irqrestore(&b->lock, flags);
636 return ret;
637}
638
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800639static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
640 void *value, u64 map_flags)
641{
642 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
643}
644
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800645/* Called from syscall or from eBPF program */
646static int htab_map_delete_elem(struct bpf_map *map, void *key)
647{
648 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
649 struct hlist_head *head;
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800650 struct bucket *b;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800651 struct htab_elem *l;
652 unsigned long flags;
653 u32 hash, key_size;
654 int ret = -ENOENT;
655
656 WARN_ON_ONCE(!rcu_read_lock_held());
657
658 key_size = map->key_size;
659
660 hash = htab_map_hash(key, key_size);
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800661 b = __select_bucket(htab, hash);
662 head = &b->head;
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800663
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800664 raw_spin_lock_irqsave(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800665
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800666 l = lookup_elem_raw(head, hash, key, key_size);
667
668 if (l) {
669 hlist_del_rcu(&l->hash_node);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800670 free_htab_elem(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800671 ret = 0;
672 }
673
tom.leiming@gmail.com688ecfe2015-12-29 22:40:27 +0800674 raw_spin_unlock_irqrestore(&b->lock, flags);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800675 return ret;
676}
677
678static void delete_all_elements(struct bpf_htab *htab)
679{
680 int i;
681
682 for (i = 0; i < htab->n_buckets; i++) {
683 struct hlist_head *head = select_bucket(htab, i);
684 struct hlist_node *n;
685 struct htab_elem *l;
686
687 hlist_for_each_entry_safe(l, n, head, hash_node) {
688 hlist_del_rcu(&l->hash_node);
Daniel Borkmann483bed22016-11-04 00:01:19 +0100689 if (l->state != HTAB_EXTRA_ELEM_USED)
690 htab_elem_free(htab, l);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800691 }
692 }
693}
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800694/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
695static void htab_map_free(struct bpf_map *map)
696{
697 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
698
699 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
700 * so the programs (can be more than one that used this map) were
701 * disconnected from events. Wait for outstanding critical sections in
702 * these programs to complete
703 */
704 synchronize_rcu();
705
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800706 /* some of free_htab_elem() callbacks for elements of this map may
707 * not have executed. Wait for them.
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800708 */
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800709 rcu_barrier();
710 if (htab->map.map_flags & BPF_F_NO_PREALLOC) {
711 delete_all_elements(htab);
712 } else {
713 htab_free_elems(htab);
714 pcpu_freelist_destroy(&htab->freelist);
715 }
Alexei Starovoitova6ed3ea2016-08-05 14:01:27 -0700716 free_percpu(htab->extra_elems);
Daniel Borkmann251d00b2017-01-18 15:14:17 +0100717 bpf_map_area_free(htab->buckets);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800718 kfree(htab);
719}
720
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100721static const struct bpf_map_ops htab_ops = {
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800722 .map_alloc = htab_map_alloc,
723 .map_free = htab_map_free,
724 .map_get_next_key = htab_map_get_next_key,
725 .map_lookup_elem = htab_map_lookup_elem,
726 .map_update_elem = htab_map_update_elem,
727 .map_delete_elem = htab_map_delete_elem,
728};
729
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100730static struct bpf_map_type_list htab_type __read_mostly = {
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800731 .ops = &htab_ops,
732 .type = BPF_MAP_TYPE_HASH,
733};
734
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800735/* Called from eBPF program */
736static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
737{
738 struct htab_elem *l = __htab_map_lookup_elem(map, key);
739
740 if (l)
741 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
742 else
743 return NULL;
744}
745
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800746int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
747{
748 struct htab_elem *l;
749 void __percpu *pptr;
750 int ret = -ENOENT;
751 int cpu, off = 0;
752 u32 size;
753
754 /* per_cpu areas are zero-filled and bpf programs can only
755 * access 'value_size' of them, so copying rounded areas
756 * will not leak any kernel data
757 */
758 size = round_up(map->value_size, 8);
759 rcu_read_lock();
760 l = __htab_map_lookup_elem(map, key);
761 if (!l)
762 goto out;
763 pptr = htab_elem_get_ptr(l, map->key_size);
764 for_each_possible_cpu(cpu) {
765 bpf_long_memcpy(value + off,
766 per_cpu_ptr(pptr, cpu), size);
767 off += size;
768 }
769 ret = 0;
770out:
771 rcu_read_unlock();
772 return ret;
773}
774
775int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
776 u64 map_flags)
777{
Sasha Levin6bbd9a02016-02-19 13:53:10 -0500778 int ret;
779
780 rcu_read_lock();
781 ret = __htab_percpu_map_update_elem(map, key, value, map_flags, true);
782 rcu_read_unlock();
783
784 return ret;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800785}
786
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800787static const struct bpf_map_ops htab_percpu_ops = {
788 .map_alloc = htab_map_alloc,
789 .map_free = htab_map_free,
790 .map_get_next_key = htab_map_get_next_key,
791 .map_lookup_elem = htab_percpu_map_lookup_elem,
792 .map_update_elem = htab_percpu_map_update_elem,
793 .map_delete_elem = htab_map_delete_elem,
794};
795
796static struct bpf_map_type_list htab_percpu_type __read_mostly = {
797 .ops = &htab_percpu_ops,
798 .type = BPF_MAP_TYPE_PERCPU_HASH,
799};
800
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800801static int __init register_htab_map(void)
802{
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100803 bpf_register_map_type(&htab_type);
Alexei Starovoitov824bd0c2016-02-01 22:39:53 -0800804 bpf_register_map_type(&htab_percpu_type);
Alexei Starovoitov0f8e4bd2014-11-13 17:36:45 -0800805 return 0;
806}
807late_initcall(register_htab_map);