Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3 * Copyright (c) 2016 Facebook
4 */
5#include <linux/bpf.h>
6#include <linux/btf.h>
7#include <linux/jhash.h>
8#include <linux/filter.h>
9#include <linux/rculist_nulls.h>
10#include <linux/random.h>
11#include <uapi/linux/btf.h>
12#include "percpu_freelist.h"
13#include "bpf_lru_list.h"
14#include "map_in_map.h"
15
16#define HTAB_CREATE_FLAG_MASK \
17 (BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE | \
18 BPF_F_ACCESS_MASK | BPF_F_ZERO_SEED)
19
20struct bucket {
21 struct hlist_nulls_head head;
22 raw_spinlock_t lock;
23};
24
25struct bpf_htab {
26 struct bpf_map map;
27 struct bucket *buckets;
28 void *elems;
29 union {
30 struct pcpu_freelist freelist;
31 struct bpf_lru lru;
32 };
33 struct htab_elem *__percpu *extra_elems;
34 atomic_t count; /* number of elements in this hashtable */
35 u32 n_buckets; /* number of hash buckets */
36 u32 elem_size; /* size of each element in bytes */
37 u32 hashrnd;
38};
39
40/* each htab element is struct htab_elem + key + value */
41struct htab_elem {
42 union {
43 struct hlist_nulls_node hash_node;
44 struct {
45 void *padding;
46 union {
47 struct bpf_htab *htab;
48 struct pcpu_freelist_node fnode;
49 };
50 };
51 };
52 union {
53 struct rcu_head rcu;
54 struct bpf_lru_node lru_node;
55 };
56 u32 hash;
57 char key[0] __aligned(8);
58};
59
60static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
61
62static bool htab_is_lru(const struct bpf_htab *htab)
63{
64 return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
65 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
66}
67
68static bool htab_is_percpu(const struct bpf_htab *htab)
69{
70 return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
71 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
72}
73
74static bool htab_is_prealloc(const struct bpf_htab *htab)
75{
76 return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
77}
78
79static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
80 void __percpu *pptr)
81{
82 *(void __percpu **)(l->key + key_size) = pptr;
83}
84
85static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
86{
87 return *(void __percpu **)(l->key + key_size);
88}
89
90static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l)
91{
92 return *(void **)(l->key + roundup(map->key_size, 8));
93}
94
95static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
96{
97 return (struct htab_elem *) (htab->elems + i * htab->elem_size);
98}
99
100static void htab_free_elems(struct bpf_htab *htab)
101{
102 int i;
103
104 if (!htab_is_percpu(htab))
105 goto free_elems;
106
107 for (i = 0; i < htab->map.max_entries; i++) {
108 void __percpu *pptr;
109
110 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
111 htab->map.key_size);
112 free_percpu(pptr);
113 cond_resched();
114 }
115free_elems:
116 bpf_map_area_free(htab->elems);
117}
118
119static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
120 u32 hash)
121{
122 struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
123 struct htab_elem *l;
124
125 if (node) {
126 l = container_of(node, struct htab_elem, lru_node);
127 memcpy(l->key, key, htab->map.key_size);
128 return l;
129 }
130
131 return NULL;
132}
133
134static int prealloc_init(struct bpf_htab *htab)
135{
136 u32 num_entries = htab->map.max_entries;
137 int err = -ENOMEM, i;
138
139 if (!htab_is_percpu(htab) && !htab_is_lru(htab))
140 num_entries += num_possible_cpus();
141
142 htab->elems = bpf_map_area_alloc(htab->elem_size * num_entries,
143 htab->map.numa_node);
144 if (!htab->elems)
145 return -ENOMEM;
146
147 if (!htab_is_percpu(htab))
148 goto skip_percpu_elems;
149
150 for (i = 0; i < num_entries; i++) {
151 u32 size = round_up(htab->map.value_size, 8);
152 void __percpu *pptr;
153
154 pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
155 if (!pptr)
156 goto free_elems;
157 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
158 pptr);
159 cond_resched();
160 }
161
162skip_percpu_elems:
163 if (htab_is_lru(htab))
164 err = bpf_lru_init(&htab->lru,
165 htab->map.map_flags & BPF_F_NO_COMMON_LRU,
166 offsetof(struct htab_elem, hash) -
167 offsetof(struct htab_elem, lru_node),
168 htab_lru_map_delete_node,
169 htab);
170 else
171 err = pcpu_freelist_init(&htab->freelist);
172
173 if (err)
174 goto free_elems;
175
176 if (htab_is_lru(htab))
177 bpf_lru_populate(&htab->lru, htab->elems,
178 offsetof(struct htab_elem, lru_node),
179 htab->elem_size, num_entries);
180 else
181 pcpu_freelist_populate(&htab->freelist,
182 htab->elems + offsetof(struct htab_elem, fnode),
183 htab->elem_size, num_entries);
184
185 return 0;
186
187free_elems:
188 htab_free_elems(htab);
189 return err;
190}
191
192static void prealloc_destroy(struct bpf_htab *htab)
193{
194 htab_free_elems(htab);
195
196 if (htab_is_lru(htab))
197 bpf_lru_destroy(&htab->lru);
198 else
199 pcpu_freelist_destroy(&htab->freelist);
200}
201
202static int alloc_extra_elems(struct bpf_htab *htab)
203{
204 struct htab_elem *__percpu *pptr, *l_new;
205 struct pcpu_freelist_node *l;
206 int cpu;
207
208 pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8,
209 GFP_USER | __GFP_NOWARN);
210 if (!pptr)
211 return -ENOMEM;
212
213 for_each_possible_cpu(cpu) {
214 l = pcpu_freelist_pop(&htab->freelist);
215 /* pop will succeed, since prealloc_init()
216 * preallocated extra num_possible_cpus elements
217 */
218 l_new = container_of(l, struct htab_elem, fnode);
219 *per_cpu_ptr(pptr, cpu) = l_new;
220 }
221 htab->extra_elems = pptr;
222 return 0;
223}
224
225/* Called from syscall */
226static int htab_map_alloc_check(union bpf_attr *attr)
227{
228 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
229 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
230 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
231 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
232 /* percpu_lru means each cpu has its own LRU list.
233 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
234 * the map's value itself is percpu. percpu_lru has
235 * nothing to do with the map's value.
236 */
237 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
238 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
239 bool zero_seed = (attr->map_flags & BPF_F_ZERO_SEED);
240 int numa_node = bpf_map_attr_numa_node(attr);
241
242 BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
243 offsetof(struct htab_elem, hash_node.pprev));
244 BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
245 offsetof(struct htab_elem, hash_node.pprev));
246
247 if (lru && !capable(CAP_SYS_ADMIN))
248 /* LRU implementation is much complicated than other
249 * maps. Hence, limit to CAP_SYS_ADMIN for now.
250 */
251 return -EPERM;
252
253 if (zero_seed && !capable(CAP_SYS_ADMIN))
254 /* Guard against local DoS, and discourage production use. */
255 return -EPERM;
256
257 if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK ||
258 !bpf_map_flags_access_ok(attr->map_flags))
259 return -EINVAL;
260
261 if (!lru && percpu_lru)
262 return -EINVAL;
263
264 if (lru && !prealloc)
265 return -ENOTSUPP;
266
267 if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
268 return -EINVAL;
269
270 /* check sanity of attributes.
271 * value_size == 0 may be allowed in the future to use map as a set
272 */
273 if (attr->max_entries == 0 || attr->key_size == 0 ||
274 attr->value_size == 0)
275 return -EINVAL;
276
277 if (attr->key_size > MAX_BPF_STACK)
278 /* eBPF programs initialize keys on stack, so they cannot be
279 * larger than max stack size
280 */
281 return -E2BIG;
282
283 if (attr->value_size >= KMALLOC_MAX_SIZE -
284 MAX_BPF_STACK - sizeof(struct htab_elem))
285 /* if value_size is bigger, the user space won't be able to
286 * access the elements via bpf syscall. This check also makes
287 * sure that the elem_size doesn't overflow and it's
288 * kmalloc-able later in htab_map_update_elem()
289 */
290 return -E2BIG;
291
292 return 0;
293}
294
295static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
296{
297 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
298 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
299 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
300 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
301 /* percpu_lru means each cpu has its own LRU list.
302 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
303 * the map's value itself is percpu. percpu_lru has
304 * nothing to do with the map's value.
305 */
306 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
307 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
308 struct bpf_htab *htab;
309 int err, i;
310 u64 cost;
311
312 htab = kzalloc(sizeof(*htab), GFP_USER);
313 if (!htab)
314 return ERR_PTR(-ENOMEM);
315
316 bpf_map_init_from_attr(&htab->map, attr);
317
318 if (percpu_lru) {
319 /* ensure each CPU's lru list has >=1 elements.
320 * since we are at it, make each lru list has the same
321 * number of elements.
322 */
323 htab->map.max_entries = roundup(attr->max_entries,
324 num_possible_cpus());
325 if (htab->map.max_entries < attr->max_entries)
326 htab->map.max_entries = rounddown(attr->max_entries,
327 num_possible_cpus());
328 }
329
330 /* hash table size must be power of 2 */
331 htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
332
333 htab->elem_size = sizeof(struct htab_elem) +
334 round_up(htab->map.key_size, 8);
335 if (percpu)
336 htab->elem_size += sizeof(void *);
337 else
338 htab->elem_size += round_up(htab->map.value_size, 8);
339
340 err = -E2BIG;
341 /* prevent zero size kmalloc and check for u32 overflow */
342 if (htab->n_buckets == 0 ||
343 htab->n_buckets > U32_MAX / sizeof(struct bucket))
344 goto free_htab;
345
346 cost = (u64) htab->n_buckets * sizeof(struct bucket) +
347 (u64) htab->elem_size * htab->map.max_entries;
348
349 if (percpu)
350 cost += (u64) round_up(htab->map.value_size, 8) *
351 num_possible_cpus() * htab->map.max_entries;
352 else
353 cost += (u64) htab->elem_size * num_possible_cpus();
354
355 /* if map size is larger than memlock limit, reject it */
356 err = bpf_map_charge_init(&htab->map.memory, cost);
357 if (err)
358 goto free_htab;
359
360 err = -ENOMEM;
361 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
362 sizeof(struct bucket),
363 htab->map.numa_node);
364 if (!htab->buckets)
365 goto free_charge;
366
367 if (htab->map.map_flags & BPF_F_ZERO_SEED)
368 htab->hashrnd = 0;
369 else
370 htab->hashrnd = get_random_int();
371
372 for (i = 0; i < htab->n_buckets; i++) {
373 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
374 raw_spin_lock_init(&htab->buckets[i].lock);
375 }
376
377 if (prealloc) {
378 err = prealloc_init(htab);
379 if (err)
380 goto free_buckets;
381
382 if (!percpu && !lru) {
383 /* lru itself can remove the least used element, so
384 * there is no need for an extra elem during map_update.
385 */
386 err = alloc_extra_elems(htab);
387 if (err)
388 goto free_prealloc;
389 }
390 }
391
392 return &htab->map;
393
394free_prealloc:
395 prealloc_destroy(htab);
396free_buckets:
397 bpf_map_area_free(htab->buckets);
398free_charge:
399 bpf_map_charge_finish(&htab->map.memory);
400free_htab:
401 kfree(htab);
402 return ERR_PTR(err);
403}
404
405static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
406{
407 return jhash(key, key_len, hashrnd);
408}
409
410static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
411{
412 return &htab->buckets[hash & (htab->n_buckets - 1)];
413}
414
415static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
416{
417 return &__select_bucket(htab, hash)->head;
418}
419
420/* this lookup function can only be called with bucket lock taken */
421static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
422 void *key, u32 key_size)
423{
424 struct hlist_nulls_node *n;
425 struct htab_elem *l;
426
427 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
428 if (l->hash == hash && !memcmp(&l->key, key, key_size))
429 return l;
430
431 return NULL;
432}
433
434/* can be called without bucket lock. it will repeat the loop in
435 * the unlikely event when elements moved from one bucket into another
436 * while link list is being walked
437 */
438static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
439 u32 hash, void *key,
440 u32 key_size, u32 n_buckets)
441{
442 struct hlist_nulls_node *n;
443 struct htab_elem *l;
444
445again:
446 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
447 if (l->hash == hash && !memcmp(&l->key, key, key_size))
448 return l;
449
450 if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
451 goto again;
452
453 return NULL;
454}
455
456/* Called from syscall or from eBPF program directly, so
457 * arguments have to match bpf_map_lookup_elem() exactly.
458 * The return value is adjusted by BPF instructions
459 * in htab_map_gen_lookup().
460 */
461static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
462{
463 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
464 struct hlist_nulls_head *head;
465 struct htab_elem *l;
466 u32 hash, key_size;
467
468 /* Must be called with rcu_read_lock. */
469 WARN_ON_ONCE(!rcu_read_lock_held());
470
471 key_size = map->key_size;
472
473 hash = htab_map_hash(key, key_size, htab->hashrnd);
474
475 head = select_bucket(htab, hash);
476
477 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
478
479 return l;
480}
481
482static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
483{
484 struct htab_elem *l = __htab_map_lookup_elem(map, key);
485
486 if (l)
487 return l->key + round_up(map->key_size, 8);
488
489 return NULL;
490}
491
492/* inline bpf_map_lookup_elem() call.
493 * Instead of:
494 * bpf_prog
495 * bpf_map_lookup_elem
496 * map->ops->map_lookup_elem
497 * htab_map_lookup_elem
498 * __htab_map_lookup_elem
499 * do:
500 * bpf_prog
501 * __htab_map_lookup_elem
502 */
503static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
504{
505 struct bpf_insn *insn = insn_buf;
506 const int ret = BPF_REG_0;
507
508 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
509 (void *(*)(struct bpf_map *map, void *key))NULL));
510 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
511 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
512 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
513 offsetof(struct htab_elem, key) +
514 round_up(map->key_size, 8));
515 return insn - insn_buf;
516}
517
518static __always_inline void *__htab_lru_map_lookup_elem(struct bpf_map *map,
519 void *key, const bool mark)
520{
521 struct htab_elem *l = __htab_map_lookup_elem(map, key);
522
523 if (l) {
524 if (mark)
525 bpf_lru_node_set_ref(&l->lru_node);
526 return l->key + round_up(map->key_size, 8);
527 }
528
529 return NULL;
530}
531
532static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
533{
534 return __htab_lru_map_lookup_elem(map, key, true);
535}
536
537static void *htab_lru_map_lookup_elem_sys(struct bpf_map *map, void *key)
538{
539 return __htab_lru_map_lookup_elem(map, key, false);
540}
541
542static u32 htab_lru_map_gen_lookup(struct bpf_map *map,
543 struct bpf_insn *insn_buf)
544{
545 struct bpf_insn *insn = insn_buf;
546 const int ret = BPF_REG_0;
547 const int ref_reg = BPF_REG_1;
548
549 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
550 (void *(*)(struct bpf_map *map, void *key))NULL));
551 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
552 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
553 *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
554 offsetof(struct htab_elem, lru_node) +
555 offsetof(struct bpf_lru_node, ref));
556 *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
557 *insn++ = BPF_ST_MEM(BPF_B, ret,
558 offsetof(struct htab_elem, lru_node) +
559 offsetof(struct bpf_lru_node, ref),
560 1);
561 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
562 offsetof(struct htab_elem, key) +
563 round_up(map->key_size, 8));
564 return insn - insn_buf;
565}
566
567/* It is called from the bpf_lru_list when the LRU needs to delete
568 * older elements from the htab.
569 */
570static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
571{
572 struct bpf_htab *htab = (struct bpf_htab *)arg;
573 struct htab_elem *l = NULL, *tgt_l;
574 struct hlist_nulls_head *head;
575 struct hlist_nulls_node *n;
576 unsigned long flags;
577 struct bucket *b;
578
579 tgt_l = container_of(node, struct htab_elem, lru_node);
580 b = __select_bucket(htab, tgt_l->hash);
581 head = &b->head;
582
583 raw_spin_lock_irqsave(&b->lock, flags);
584
585 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
586 if (l == tgt_l) {
587 hlist_nulls_del_rcu(&l->hash_node);
588 break;
589 }
590
591 raw_spin_unlock_irqrestore(&b->lock, flags);
592
593 return l == tgt_l;
594}
595
596/* Called from syscall */
597static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
598{
599 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
600 struct hlist_nulls_head *head;
601 struct htab_elem *l, *next_l;
602 u32 hash, key_size;
603 int i = 0;
604
605 WARN_ON_ONCE(!rcu_read_lock_held());
606
607 key_size = map->key_size;
608
609 if (!key)
610 goto find_first_elem;
611
612 hash = htab_map_hash(key, key_size, htab->hashrnd);
613
614 head = select_bucket(htab, hash);
615
616 /* lookup the key */
617 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
618
619 if (!l)
620 goto find_first_elem;
621
622 /* key was found, get next key in the same bucket */
623 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
624 struct htab_elem, hash_node);
625
626 if (next_l) {
627 /* if next elem in this hash list is non-zero, just return it */
628 memcpy(next_key, next_l->key, key_size);
629 return 0;
630 }
631
632 /* no more elements in this hash list, go to the next bucket */
633 i = hash & (htab->n_buckets - 1);
634 i++;
635
636find_first_elem:
637 /* iterate over buckets */
638 for (; i < htab->n_buckets; i++) {
639 head = select_bucket(htab, i);
640
641 /* pick first element in the bucket */
642 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
643 struct htab_elem, hash_node);
644 if (next_l) {
645 /* if it's not empty, just return it */
646 memcpy(next_key, next_l->key, key_size);
647 return 0;
648 }
649 }
650
651 /* iterated over all buckets and all elements */
652 return -ENOENT;
653}
654
655static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
656{
657 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
658 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
659 kfree(l);
660}
661
662static void htab_elem_free_rcu(struct rcu_head *head)
663{
664 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
665 struct bpf_htab *htab = l->htab;
666
667 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
668 * we're calling kfree, otherwise deadlock is possible if kprobes
669 * are placed somewhere inside of slub
670 */
671 preempt_disable();
672 __this_cpu_inc(bpf_prog_active);
673 htab_elem_free(htab, l);
674 __this_cpu_dec(bpf_prog_active);
675 preempt_enable();
676}
677
678static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
679{
680 struct bpf_map *map = &htab->map;
681
682 if (map->ops->map_fd_put_ptr) {
683 void *ptr = fd_htab_map_get_ptr(map, l);
684
685 map->ops->map_fd_put_ptr(ptr);
686 }
687
688 if (htab_is_prealloc(htab)) {
689 __pcpu_freelist_push(&htab->freelist, &l->fnode);
690 } else {
691 atomic_dec(&htab->count);
692 l->htab = htab;
693 call_rcu(&l->rcu, htab_elem_free_rcu);
694 }
695}
696
697static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
698 void *value, bool onallcpus)
699{
700 if (!onallcpus) {
701 /* copy true value_size bytes */
702 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
703 } else {
704 u32 size = round_up(htab->map.value_size, 8);
705 int off = 0, cpu;
706
707 for_each_possible_cpu(cpu) {
708 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
709 value + off, size);
710 off += size;
711 }
712 }
713}
714
715static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
716{
717 return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
718 BITS_PER_LONG == 64;
719}
720
721static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
722 void *value, u32 key_size, u32 hash,
723 bool percpu, bool onallcpus,
724 struct htab_elem *old_elem)
725{
726 u32 size = htab->map.value_size;
727 bool prealloc = htab_is_prealloc(htab);
728 struct htab_elem *l_new, **pl_new;
729 void __percpu *pptr;
730
731 if (prealloc) {
732 if (old_elem) {
733 /* if we're updating the existing element,
734 * use per-cpu extra elems to avoid freelist_pop/push
735 */
736 pl_new = this_cpu_ptr(htab->extra_elems);
737 l_new = *pl_new;
738 *pl_new = old_elem;
739 } else {
740 struct pcpu_freelist_node *l;
741
742 l = __pcpu_freelist_pop(&htab->freelist);
743 if (!l)
744 return ERR_PTR(-E2BIG);
745 l_new = container_of(l, struct htab_elem, fnode);
746 }
747 } else {
748 if (atomic_inc_return(&htab->count) > htab->map.max_entries)
749 if (!old_elem) {
750 /* when map is full and update() is replacing
751 * old element, it's ok to allocate, since
752 * old element will be freed immediately.
753 * Otherwise return an error
754 */
755 l_new = ERR_PTR(-E2BIG);
756 goto dec_count;
757 }
758 l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
759 htab->map.numa_node);
760 if (!l_new) {
761 l_new = ERR_PTR(-ENOMEM);
762 goto dec_count;
763 }
764 check_and_init_map_lock(&htab->map,
765 l_new->key + round_up(key_size, 8));
766 }
767
768 memcpy(l_new->key, key, key_size);
769 if (percpu) {
770 size = round_up(size, 8);
771 if (prealloc) {
772 pptr = htab_elem_get_ptr(l_new, key_size);
773 } else {
774 /* alloc_percpu zero-fills */
775 pptr = __alloc_percpu_gfp(size, 8,
776 GFP_ATOMIC | __GFP_NOWARN);
777 if (!pptr) {
778 kfree(l_new);
779 l_new = ERR_PTR(-ENOMEM);
780 goto dec_count;
781 }
782 }
783
784 pcpu_copy_value(htab, pptr, value, onallcpus);
785
786 if (!prealloc)
787 htab_elem_set_ptr(l_new, key_size, pptr);
788 } else if (fd_htab_map_needs_adjust(htab)) {
789 size = round_up(size, 8);
790 memcpy(l_new->key + round_up(key_size, 8), value, size);
791 } else {
792 copy_map_value(&htab->map,
793 l_new->key + round_up(key_size, 8),
794 value);
795 }
796
797 l_new->hash = hash;
798 return l_new;
799dec_count:
800 atomic_dec(&htab->count);
801 return l_new;
802}
803
804static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
805 u64 map_flags)
806{
807 if (l_old && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
808 /* elem already exists */
809 return -EEXIST;
810
811 if (!l_old && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
812 /* elem doesn't exist, cannot update it */
813 return -ENOENT;
814
815 return 0;
816}
817
818/* Called from syscall or from eBPF program */
819static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
820 u64 map_flags)
821{
822 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
823 struct htab_elem *l_new = NULL, *l_old;
824 struct hlist_nulls_head *head;
825 unsigned long flags;
826 struct bucket *b;
827 u32 key_size, hash;
828 int ret;
829
830 if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST))
831 /* unknown flags */
832 return -EINVAL;
833
834 WARN_ON_ONCE(!rcu_read_lock_held());
835
836 key_size = map->key_size;
837
838 hash = htab_map_hash(key, key_size, htab->hashrnd);
839
840 b = __select_bucket(htab, hash);
841 head = &b->head;
842
843 if (unlikely(map_flags & BPF_F_LOCK)) {
844 if (unlikely(!map_value_has_spin_lock(map)))
845 return -EINVAL;
846 /* find an element without taking the bucket lock */
847 l_old = lookup_nulls_elem_raw(head, hash, key, key_size,
848 htab->n_buckets);
849 ret = check_flags(htab, l_old, map_flags);
850 if (ret)
851 return ret;
852 if (l_old) {
853 /* grab the element lock and update value in place */
854 copy_map_value_locked(map,
855 l_old->key + round_up(key_size, 8),
856 value, false);
857 return 0;
858 }
859 /* fall through, grab the bucket lock and lookup again.
860 * 99.9% chance that the element won't be found,
861 * but second lookup under lock has to be done.
862 */
863 }
864
865 /* bpf_map_update_elem() can be called in_irq() */
866 raw_spin_lock_irqsave(&b->lock, flags);
867
868 l_old = lookup_elem_raw(head, hash, key, key_size);
869
870 ret = check_flags(htab, l_old, map_flags);
871 if (ret)
872 goto err;
873
874 if (unlikely(l_old && (map_flags & BPF_F_LOCK))) {
875 /* first lookup without the bucket lock didn't find the element,
876 * but second lookup with the bucket lock found it.
877 * This case is highly unlikely, but has to be dealt with:
878 * grab the element lock in addition to the bucket lock
879 * and update element in place
880 */
881 copy_map_value_locked(map,
882 l_old->key + round_up(key_size, 8),
883 value, false);
884 ret = 0;
885 goto err;
886 }
887
888 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
889 l_old);
890 if (IS_ERR(l_new)) {
891 /* all pre-allocated elements are in use or memory exhausted */
892 ret = PTR_ERR(l_new);
893 goto err;
894 }
895
896 /* add new element to the head of the list, so that
897 * concurrent search will find it before old elem
898 */
899 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
900 if (l_old) {
901 hlist_nulls_del_rcu(&l_old->hash_node);
902 if (!htab_is_prealloc(htab))
903 free_htab_elem(htab, l_old);
904 }
905 ret = 0;
906err:
907 raw_spin_unlock_irqrestore(&b->lock, flags);
908 return ret;
909}
910
911static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
912 u64 map_flags)
913{
914 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
915 struct htab_elem *l_new, *l_old = NULL;
916 struct hlist_nulls_head *head;
917 unsigned long flags;
918 struct bucket *b;
919 u32 key_size, hash;
920 int ret;
921
922 if (unlikely(map_flags > BPF_EXIST))
923 /* unknown flags */
924 return -EINVAL;
925
926 WARN_ON_ONCE(!rcu_read_lock_held());
927
928 key_size = map->key_size;
929
930 hash = htab_map_hash(key, key_size, htab->hashrnd);
931
932 b = __select_bucket(htab, hash);
933 head = &b->head;
934
935 /* For LRU, we need to alloc before taking bucket's
936 * spinlock because getting free nodes from LRU may need
937 * to remove older elements from htab and this removal
938 * operation will need a bucket lock.
939 */
940 l_new = prealloc_lru_pop(htab, key, hash);
941 if (!l_new)
942 return -ENOMEM;
943 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
944
945 /* bpf_map_update_elem() can be called in_irq() */
946 raw_spin_lock_irqsave(&b->lock, flags);
947
948 l_old = lookup_elem_raw(head, hash, key, key_size);
949
950 ret = check_flags(htab, l_old, map_flags);
951 if (ret)
952 goto err;
953
954 /* add new element to the head of the list, so that
955 * concurrent search will find it before old elem
956 */
957 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
958 if (l_old) {
959 bpf_lru_node_set_ref(&l_new->lru_node);
960 hlist_nulls_del_rcu(&l_old->hash_node);
961 }
962 ret = 0;
963
964err:
965 raw_spin_unlock_irqrestore(&b->lock, flags);
966
967 if (ret)
968 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
969 else if (l_old)
970 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
971
972 return ret;
973}
974
975static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
976 void *value, u64 map_flags,
977 bool onallcpus)
978{
979 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
980 struct htab_elem *l_new = NULL, *l_old;
981 struct hlist_nulls_head *head;
982 unsigned long flags;
983 struct bucket *b;
984 u32 key_size, hash;
985 int ret;
986
987 if (unlikely(map_flags > BPF_EXIST))
988 /* unknown flags */
989 return -EINVAL;
990
991 WARN_ON_ONCE(!rcu_read_lock_held());
992
993 key_size = map->key_size;
994
995 hash = htab_map_hash(key, key_size, htab->hashrnd);
996
997 b = __select_bucket(htab, hash);
998 head = &b->head;
999
1000 /* bpf_map_update_elem() can be called in_irq() */
1001 raw_spin_lock_irqsave(&b->lock, flags);
1002
1003 l_old = lookup_elem_raw(head, hash, key, key_size);
1004
1005 ret = check_flags(htab, l_old, map_flags);
1006 if (ret)
1007 goto err;
1008
1009 if (l_old) {
1010 /* per-cpu hash map can update value in-place */
1011 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1012 value, onallcpus);
1013 } else {
1014 l_new = alloc_htab_elem(htab, key, value, key_size,
1015 hash, true, onallcpus, NULL);
1016 if (IS_ERR(l_new)) {
1017 ret = PTR_ERR(l_new);
1018 goto err;
1019 }
1020 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1021 }
1022 ret = 0;
1023err:
1024 raw_spin_unlock_irqrestore(&b->lock, flags);
1025 return ret;
1026}
1027
1028static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1029 void *value, u64 map_flags,
1030 bool onallcpus)
1031{
1032 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1033 struct htab_elem *l_new = NULL, *l_old;
1034 struct hlist_nulls_head *head;
1035 unsigned long flags;
1036 struct bucket *b;
1037 u32 key_size, hash;
1038 int ret;
1039
1040 if (unlikely(map_flags > BPF_EXIST))
1041 /* unknown flags */
1042 return -EINVAL;
1043
1044 WARN_ON_ONCE(!rcu_read_lock_held());
1045
1046 key_size = map->key_size;
1047
1048 hash = htab_map_hash(key, key_size, htab->hashrnd);
1049
1050 b = __select_bucket(htab, hash);
1051 head = &b->head;
1052
1053 /* For LRU, we need to alloc before taking bucket's
1054 * spinlock because LRU's elem alloc may need
1055 * to remove older elem from htab and this removal
1056 * operation will need a bucket lock.
1057 */
1058 if (map_flags != BPF_EXIST) {
1059 l_new = prealloc_lru_pop(htab, key, hash);
1060 if (!l_new)
1061 return -ENOMEM;
1062 }
1063
1064 /* bpf_map_update_elem() can be called in_irq() */
1065 raw_spin_lock_irqsave(&b->lock, flags);
1066
1067 l_old = lookup_elem_raw(head, hash, key, key_size);
1068
1069 ret = check_flags(htab, l_old, map_flags);
1070 if (ret)
1071 goto err;
1072
1073 if (l_old) {
1074 bpf_lru_node_set_ref(&l_old->lru_node);
1075
1076 /* per-cpu hash map can update value in-place */
1077 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1078 value, onallcpus);
1079 } else {
1080 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
1081 value, onallcpus);
1082 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1083 l_new = NULL;
1084 }
1085 ret = 0;
1086err:
1087 raw_spin_unlock_irqrestore(&b->lock, flags);
1088 if (l_new)
1089 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1090 return ret;
1091}
1092
1093static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1094 void *value, u64 map_flags)
1095{
1096 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
1097}
1098
1099static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1100 void *value, u64 map_flags)
1101{
1102 return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
1103 false);
1104}
1105
1106/* Called from syscall or from eBPF program */
1107static int htab_map_delete_elem(struct bpf_map *map, void *key)
1108{
1109 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1110 struct hlist_nulls_head *head;
1111 struct bucket *b;
1112 struct htab_elem *l;
1113 unsigned long flags;
1114 u32 hash, key_size;
1115 int ret = -ENOENT;
1116
1117 WARN_ON_ONCE(!rcu_read_lock_held());
1118
1119 key_size = map->key_size;
1120
1121 hash = htab_map_hash(key, key_size, htab->hashrnd);
1122 b = __select_bucket(htab, hash);
1123 head = &b->head;
1124
1125 raw_spin_lock_irqsave(&b->lock, flags);
1126
1127 l = lookup_elem_raw(head, hash, key, key_size);
1128
1129 if (l) {
1130 hlist_nulls_del_rcu(&l->hash_node);
1131 free_htab_elem(htab, l);
1132 ret = 0;
1133 }
1134
1135 raw_spin_unlock_irqrestore(&b->lock, flags);
1136 return ret;
1137}
1138
1139static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1140{
1141 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1142 struct hlist_nulls_head *head;
1143 struct bucket *b;
1144 struct htab_elem *l;
1145 unsigned long flags;
1146 u32 hash, key_size;
1147 int ret = -ENOENT;
1148
1149 WARN_ON_ONCE(!rcu_read_lock_held());
1150
1151 key_size = map->key_size;
1152
1153 hash = htab_map_hash(key, key_size, htab->hashrnd);
1154 b = __select_bucket(htab, hash);
1155 head = &b->head;
1156
1157 raw_spin_lock_irqsave(&b->lock, flags);
1158
1159 l = lookup_elem_raw(head, hash, key, key_size);
1160
1161 if (l) {
1162 hlist_nulls_del_rcu(&l->hash_node);
1163 ret = 0;
1164 }
1165
1166 raw_spin_unlock_irqrestore(&b->lock, flags);
1167 if (l)
1168 bpf_lru_push_free(&htab->lru, &l->lru_node);
1169 return ret;
1170}
1171
1172static void delete_all_elements(struct bpf_htab *htab)
1173{
1174 int i;
1175
1176 for (i = 0; i < htab->n_buckets; i++) {
1177 struct hlist_nulls_head *head = select_bucket(htab, i);
1178 struct hlist_nulls_node *n;
1179 struct htab_elem *l;
1180
1181 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1182 hlist_nulls_del_rcu(&l->hash_node);
1183 htab_elem_free(htab, l);
1184 }
1185 }
1186}
1187
1188/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1189static void htab_map_free(struct bpf_map *map)
1190{
1191 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1192
1193 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1194 * so the programs (can be more than one that used this map) were
1195 * disconnected from events. Wait for outstanding critical sections in
1196 * these programs to complete
1197 */
1198 synchronize_rcu();
1199
1200 /* some of free_htab_elem() callbacks for elements of this map may
1201 * not have executed. Wait for them.
1202 */
1203 rcu_barrier();
1204 if (!htab_is_prealloc(htab))
1205 delete_all_elements(htab);
1206 else
1207 prealloc_destroy(htab);
1208
1209 free_percpu(htab->extra_elems);
1210 bpf_map_area_free(htab->buckets);
1211 kfree(htab);
1212}
1213
1214static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1215 struct seq_file *m)
1216{
1217 void *value;
1218
1219 rcu_read_lock();
1220
1221 value = htab_map_lookup_elem(map, key);
1222 if (!value) {
1223 rcu_read_unlock();
1224 return;
1225 }
1226
1227 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1228 seq_puts(m, ": ");
1229 btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1230 seq_puts(m, "\n");
1231
1232 rcu_read_unlock();
1233}
1234
1235const struct bpf_map_ops htab_map_ops = {
1236 .map_alloc_check = htab_map_alloc_check,
1237 .map_alloc = htab_map_alloc,
1238 .map_free = htab_map_free,
1239 .map_get_next_key = htab_map_get_next_key,
1240 .map_lookup_elem = htab_map_lookup_elem,
1241 .map_update_elem = htab_map_update_elem,
1242 .map_delete_elem = htab_map_delete_elem,
1243 .map_gen_lookup = htab_map_gen_lookup,
1244 .map_seq_show_elem = htab_map_seq_show_elem,
1245};
1246
1247const struct bpf_map_ops htab_lru_map_ops = {
1248 .map_alloc_check = htab_map_alloc_check,
1249 .map_alloc = htab_map_alloc,
1250 .map_free = htab_map_free,
1251 .map_get_next_key = htab_map_get_next_key,
1252 .map_lookup_elem = htab_lru_map_lookup_elem,
1253 .map_lookup_elem_sys_only = htab_lru_map_lookup_elem_sys,
1254 .map_update_elem = htab_lru_map_update_elem,
1255 .map_delete_elem = htab_lru_map_delete_elem,
1256 .map_gen_lookup = htab_lru_map_gen_lookup,
1257 .map_seq_show_elem = htab_map_seq_show_elem,
1258};
1259
1260/* Called from eBPF program */
1261static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1262{
1263 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1264
1265 if (l)
1266 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1267 else
1268 return NULL;
1269}
1270
1271static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1272{
1273 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1274
1275 if (l) {
1276 bpf_lru_node_set_ref(&l->lru_node);
1277 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1278 }
1279
1280 return NULL;
1281}
1282
1283int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1284{
1285 struct htab_elem *l;
1286 void __percpu *pptr;
1287 int ret = -ENOENT;
1288 int cpu, off = 0;
1289 u32 size;
1290
1291 /* per_cpu areas are zero-filled and bpf programs can only
1292 * access 'value_size' of them, so copying rounded areas
1293 * will not leak any kernel data
1294 */
1295 size = round_up(map->value_size, 8);
1296 rcu_read_lock();
1297 l = __htab_map_lookup_elem(map, key);
1298 if (!l)
1299 goto out;
1300 /* We do not mark LRU map element here in order to not mess up
1301 * eviction heuristics when user space does a map walk.
1302 */
1303 pptr = htab_elem_get_ptr(l, map->key_size);
1304 for_each_possible_cpu(cpu) {
1305 bpf_long_memcpy(value + off,
1306 per_cpu_ptr(pptr, cpu), size);
1307 off += size;
1308 }
1309 ret = 0;
1310out:
1311 rcu_read_unlock();
1312 return ret;
1313}
1314
1315int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1316 u64 map_flags)
1317{
1318 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1319 int ret;
1320
1321 rcu_read_lock();
1322 if (htab_is_lru(htab))
1323 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1324 map_flags, true);
1325 else
1326 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1327 true);
1328 rcu_read_unlock();
1329
1330 return ret;
1331}
1332
1333static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
1334 struct seq_file *m)
1335{
1336 struct htab_elem *l;
1337 void __percpu *pptr;
1338 int cpu;
1339
1340 rcu_read_lock();
1341
1342 l = __htab_map_lookup_elem(map, key);
1343 if (!l) {
1344 rcu_read_unlock();
1345 return;
1346 }
1347
1348 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1349 seq_puts(m, ": {\n");
1350 pptr = htab_elem_get_ptr(l, map->key_size);
1351 for_each_possible_cpu(cpu) {
1352 seq_printf(m, "\tcpu%d: ", cpu);
1353 btf_type_seq_show(map->btf, map->btf_value_type_id,
1354 per_cpu_ptr(pptr, cpu), m);
1355 seq_puts(m, "\n");
1356 }
1357 seq_puts(m, "}\n");
1358
1359 rcu_read_unlock();
1360}
1361
1362const struct bpf_map_ops htab_percpu_map_ops = {
1363 .map_alloc_check = htab_map_alloc_check,
1364 .map_alloc = htab_map_alloc,
1365 .map_free = htab_map_free,
1366 .map_get_next_key = htab_map_get_next_key,
1367 .map_lookup_elem = htab_percpu_map_lookup_elem,
1368 .map_update_elem = htab_percpu_map_update_elem,
1369 .map_delete_elem = htab_map_delete_elem,
1370 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
1371};
1372
1373const struct bpf_map_ops htab_lru_percpu_map_ops = {
1374 .map_alloc_check = htab_map_alloc_check,
1375 .map_alloc = htab_map_alloc,
1376 .map_free = htab_map_free,
1377 .map_get_next_key = htab_map_get_next_key,
1378 .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1379 .map_update_elem = htab_lru_percpu_map_update_elem,
1380 .map_delete_elem = htab_lru_map_delete_elem,
1381 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
1382};
1383
1384static int fd_htab_map_alloc_check(union bpf_attr *attr)
1385{
1386 if (attr->value_size != sizeof(u32))
1387 return -EINVAL;
1388 return htab_map_alloc_check(attr);
1389}
1390
1391static void fd_htab_map_free(struct bpf_map *map)
1392{
1393 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1394 struct hlist_nulls_node *n;
1395 struct hlist_nulls_head *head;
1396 struct htab_elem *l;
1397 int i;
1398
1399 for (i = 0; i < htab->n_buckets; i++) {
1400 head = select_bucket(htab, i);
1401
1402 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1403 void *ptr = fd_htab_map_get_ptr(map, l);
1404
1405 map->ops->map_fd_put_ptr(ptr);
1406 }
1407 }
1408
1409 htab_map_free(map);
1410}
1411
1412/* only called from syscall */
1413int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
1414{
1415 void **ptr;
1416 int ret = 0;
1417
1418 if (!map->ops->map_fd_sys_lookup_elem)
1419 return -ENOTSUPP;
1420
1421 rcu_read_lock();
1422 ptr = htab_map_lookup_elem(map, key);
1423 if (ptr)
1424 *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
1425 else
1426 ret = -ENOENT;
1427 rcu_read_unlock();
1428
1429 return ret;
1430}
1431
1432/* only called from syscall */
1433int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1434 void *key, void *value, u64 map_flags)
1435{
1436 void *ptr;
1437 int ret;
1438 u32 ufd = *(u32 *)value;
1439
1440 ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
1441 if (IS_ERR(ptr))
1442 return PTR_ERR(ptr);
1443
1444 ret = htab_map_update_elem(map, key, &ptr, map_flags);
1445 if (ret)
1446 map->ops->map_fd_put_ptr(ptr);
1447
1448 return ret;
1449}
1450
1451static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
1452{
1453 struct bpf_map *map, *inner_map_meta;
1454
1455 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1456 if (IS_ERR(inner_map_meta))
1457 return inner_map_meta;
1458
1459 map = htab_map_alloc(attr);
1460 if (IS_ERR(map)) {
1461 bpf_map_meta_free(inner_map_meta);
1462 return map;
1463 }
1464
1465 map->inner_map_meta = inner_map_meta;
1466
1467 return map;
1468}
1469
1470static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
1471{
1472 struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
1473
1474 if (!inner_map)
1475 return NULL;
1476
1477 return READ_ONCE(*inner_map);
1478}
1479
1480static u32 htab_of_map_gen_lookup(struct bpf_map *map,
1481 struct bpf_insn *insn_buf)
1482{
1483 struct bpf_insn *insn = insn_buf;
1484 const int ret = BPF_REG_0;
1485
1486 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
1487 (void *(*)(struct bpf_map *map, void *key))NULL));
1488 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
1489 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
1490 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
1491 offsetof(struct htab_elem, key) +
1492 round_up(map->key_size, 8));
1493 *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
1494
1495 return insn - insn_buf;
1496}
1497
1498static void htab_of_map_free(struct bpf_map *map)
1499{
1500 bpf_map_meta_free(map->inner_map_meta);
1501 fd_htab_map_free(map);
1502}
1503
1504const struct bpf_map_ops htab_of_maps_map_ops = {
1505 .map_alloc_check = fd_htab_map_alloc_check,
1506 .map_alloc = htab_of_map_alloc,
1507 .map_free = htab_of_map_free,
1508 .map_get_next_key = htab_map_get_next_key,
1509 .map_lookup_elem = htab_of_map_lookup_elem,
1510 .map_delete_elem = htab_map_delete_elem,
1511 .map_fd_get_ptr = bpf_map_fd_get_ptr,
1512 .map_fd_put_ptr = bpf_map_fd_put_ptr,
1513 .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
1514 .map_gen_lookup = htab_of_map_gen_lookup,
1515 .map_check_btf = map_check_no_btf,
1516};
1/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 * Copyright (c) 2016 Facebook
3 *
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>
16#include "percpu_freelist.h"
17#include "bpf_lru_list.h"
18
19struct bucket {
20 struct hlist_head head;
21 raw_spinlock_t lock;
22};
23
24struct bpf_htab {
25 struct bpf_map map;
26 struct bucket *buckets;
27 void *elems;
28 union {
29 struct pcpu_freelist freelist;
30 struct bpf_lru lru;
31 };
32 void __percpu *extra_elems;
33 atomic_t count; /* number of elements in this hashtable */
34 u32 n_buckets; /* number of hash buckets */
35 u32 elem_size; /* size of each element in bytes */
36};
37
38enum extra_elem_state {
39 HTAB_NOT_AN_EXTRA_ELEM = 0,
40 HTAB_EXTRA_ELEM_FREE,
41 HTAB_EXTRA_ELEM_USED
42};
43
44/* each htab element is struct htab_elem + key + value */
45struct htab_elem {
46 union {
47 struct hlist_node hash_node;
48 struct bpf_htab *htab;
49 struct pcpu_freelist_node fnode;
50 };
51 union {
52 struct rcu_head rcu;
53 enum extra_elem_state state;
54 struct bpf_lru_node lru_node;
55 };
56 u32 hash;
57 char key[0] __aligned(8);
58};
59
60static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
61
62static bool htab_is_lru(const struct bpf_htab *htab)
63{
64 return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
65 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
66}
67
68static bool htab_is_percpu(const struct bpf_htab *htab)
69{
70 return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
71 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
72}
73
74static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
75 void __percpu *pptr)
76{
77 *(void __percpu **)(l->key + key_size) = pptr;
78}
79
80static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
81{
82 return *(void __percpu **)(l->key + key_size);
83}
84
85static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
86{
87 return (struct htab_elem *) (htab->elems + i * htab->elem_size);
88}
89
90static void htab_free_elems(struct bpf_htab *htab)
91{
92 int i;
93
94 if (!htab_is_percpu(htab))
95 goto free_elems;
96
97 for (i = 0; i < htab->map.max_entries; i++) {
98 void __percpu *pptr;
99
100 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
101 htab->map.key_size);
102 free_percpu(pptr);
103 }
104free_elems:
105 bpf_map_area_free(htab->elems);
106}
107
108static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
109 u32 hash)
110{
111 struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
112 struct htab_elem *l;
113
114 if (node) {
115 l = container_of(node, struct htab_elem, lru_node);
116 memcpy(l->key, key, htab->map.key_size);
117 return l;
118 }
119
120 return NULL;
121}
122
123static int prealloc_init(struct bpf_htab *htab)
124{
125 int err = -ENOMEM, i;
126
127 htab->elems = bpf_map_area_alloc(htab->elem_size *
128 htab->map.max_entries);
129 if (!htab->elems)
130 return -ENOMEM;
131
132 if (!htab_is_percpu(htab))
133 goto skip_percpu_elems;
134
135 for (i = 0; i < htab->map.max_entries; i++) {
136 u32 size = round_up(htab->map.value_size, 8);
137 void __percpu *pptr;
138
139 pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
140 if (!pptr)
141 goto free_elems;
142 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
143 pptr);
144 }
145
146skip_percpu_elems:
147 if (htab_is_lru(htab))
148 err = bpf_lru_init(&htab->lru,
149 htab->map.map_flags & BPF_F_NO_COMMON_LRU,
150 offsetof(struct htab_elem, hash) -
151 offsetof(struct htab_elem, lru_node),
152 htab_lru_map_delete_node,
153 htab);
154 else
155 err = pcpu_freelist_init(&htab->freelist);
156
157 if (err)
158 goto free_elems;
159
160 if (htab_is_lru(htab))
161 bpf_lru_populate(&htab->lru, htab->elems,
162 offsetof(struct htab_elem, lru_node),
163 htab->elem_size, htab->map.max_entries);
164 else
165 pcpu_freelist_populate(&htab->freelist, htab->elems,
166 htab->elem_size, htab->map.max_entries);
167
168 return 0;
169
170free_elems:
171 htab_free_elems(htab);
172 return err;
173}
174
175static void prealloc_destroy(struct bpf_htab *htab)
176{
177 htab_free_elems(htab);
178
179 if (htab_is_lru(htab))
180 bpf_lru_destroy(&htab->lru);
181 else
182 pcpu_freelist_destroy(&htab->freelist);
183}
184
185static int alloc_extra_elems(struct bpf_htab *htab)
186{
187 void __percpu *pptr;
188 int cpu;
189
190 pptr = __alloc_percpu_gfp(htab->elem_size, 8, GFP_USER | __GFP_NOWARN);
191 if (!pptr)
192 return -ENOMEM;
193
194 for_each_possible_cpu(cpu) {
195 ((struct htab_elem *)per_cpu_ptr(pptr, cpu))->state =
196 HTAB_EXTRA_ELEM_FREE;
197 }
198 htab->extra_elems = pptr;
199 return 0;
200}
201
202/* Called from syscall */
203static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
204{
205 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
206 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
207 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
208 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
209 /* percpu_lru means each cpu has its own LRU list.
210 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
211 * the map's value itself is percpu. percpu_lru has
212 * nothing to do with the map's value.
213 */
214 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
215 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
216 struct bpf_htab *htab;
217 int err, i;
218 u64 cost;
219
220 if (lru && !capable(CAP_SYS_ADMIN))
221 /* LRU implementation is much complicated than other
222 * maps. Hence, limit to CAP_SYS_ADMIN for now.
223 */
224 return ERR_PTR(-EPERM);
225
226 if (attr->map_flags & ~(BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU))
227 /* reserved bits should not be used */
228 return ERR_PTR(-EINVAL);
229
230 if (!lru && percpu_lru)
231 return ERR_PTR(-EINVAL);
232
233 if (lru && !prealloc)
234 return ERR_PTR(-ENOTSUPP);
235
236 htab = kzalloc(sizeof(*htab), GFP_USER);
237 if (!htab)
238 return ERR_PTR(-ENOMEM);
239
240 /* mandatory map attributes */
241 htab->map.map_type = attr->map_type;
242 htab->map.key_size = attr->key_size;
243 htab->map.value_size = attr->value_size;
244 htab->map.max_entries = attr->max_entries;
245 htab->map.map_flags = attr->map_flags;
246
247 /* check sanity of attributes.
248 * value_size == 0 may be allowed in the future to use map as a set
249 */
250 err = -EINVAL;
251 if (htab->map.max_entries == 0 || htab->map.key_size == 0 ||
252 htab->map.value_size == 0)
253 goto free_htab;
254
255 if (percpu_lru) {
256 /* ensure each CPU's lru list has >=1 elements.
257 * since we are at it, make each lru list has the same
258 * number of elements.
259 */
260 htab->map.max_entries = roundup(attr->max_entries,
261 num_possible_cpus());
262 if (htab->map.max_entries < attr->max_entries)
263 htab->map.max_entries = rounddown(attr->max_entries,
264 num_possible_cpus());
265 }
266
267 /* hash table size must be power of 2 */
268 htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
269
270 err = -E2BIG;
271 if (htab->map.key_size > MAX_BPF_STACK)
272 /* eBPF programs initialize keys on stack, so they cannot be
273 * larger than max stack size
274 */
275 goto free_htab;
276
277 if (htab->map.value_size >= KMALLOC_MAX_SIZE -
278 MAX_BPF_STACK - sizeof(struct htab_elem))
279 /* if value_size is bigger, the user space won't be able to
280 * access the elements via bpf syscall. This check also makes
281 * sure that the elem_size doesn't overflow and it's
282 * kmalloc-able later in htab_map_update_elem()
283 */
284 goto free_htab;
285
286 if (percpu && round_up(htab->map.value_size, 8) > PCPU_MIN_UNIT_SIZE)
287 /* make sure the size for pcpu_alloc() is reasonable */
288 goto free_htab;
289
290 htab->elem_size = sizeof(struct htab_elem) +
291 round_up(htab->map.key_size, 8);
292 if (percpu)
293 htab->elem_size += sizeof(void *);
294 else
295 htab->elem_size += round_up(htab->map.value_size, 8);
296
297 /* prevent zero size kmalloc and check for u32 overflow */
298 if (htab->n_buckets == 0 ||
299 htab->n_buckets > U32_MAX / sizeof(struct bucket))
300 goto free_htab;
301
302 cost = (u64) htab->n_buckets * sizeof(struct bucket) +
303 (u64) htab->elem_size * htab->map.max_entries;
304
305 if (percpu)
306 cost += (u64) round_up(htab->map.value_size, 8) *
307 num_possible_cpus() * htab->map.max_entries;
308 else
309 cost += (u64) htab->elem_size * num_possible_cpus();
310
311 if (cost >= U32_MAX - PAGE_SIZE)
312 /* make sure page count doesn't overflow */
313 goto free_htab;
314
315 htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
316
317 /* if map size is larger than memlock limit, reject it early */
318 err = bpf_map_precharge_memlock(htab->map.pages);
319 if (err)
320 goto free_htab;
321
322 err = -ENOMEM;
323 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
324 sizeof(struct bucket));
325 if (!htab->buckets)
326 goto free_htab;
327
328 for (i = 0; i < htab->n_buckets; i++) {
329 INIT_HLIST_HEAD(&htab->buckets[i].head);
330 raw_spin_lock_init(&htab->buckets[i].lock);
331 }
332
333 if (!percpu && !lru) {
334 /* lru itself can remove the least used element, so
335 * there is no need for an extra elem during map_update.
336 */
337 err = alloc_extra_elems(htab);
338 if (err)
339 goto free_buckets;
340 }
341
342 if (prealloc) {
343 err = prealloc_init(htab);
344 if (err)
345 goto free_extra_elems;
346 }
347
348 return &htab->map;
349
350free_extra_elems:
351 free_percpu(htab->extra_elems);
352free_buckets:
353 bpf_map_area_free(htab->buckets);
354free_htab:
355 kfree(htab);
356 return ERR_PTR(err);
357}
358
359static inline u32 htab_map_hash(const void *key, u32 key_len)
360{
361 return jhash(key, key_len, 0);
362}
363
364static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
365{
366 return &htab->buckets[hash & (htab->n_buckets - 1)];
367}
368
369static inline struct hlist_head *select_bucket(struct bpf_htab *htab, u32 hash)
370{
371 return &__select_bucket(htab, hash)->head;
372}
373
374static struct htab_elem *lookup_elem_raw(struct hlist_head *head, u32 hash,
375 void *key, u32 key_size)
376{
377 struct htab_elem *l;
378
379 hlist_for_each_entry_rcu(l, head, hash_node)
380 if (l->hash == hash && !memcmp(&l->key, key, key_size))
381 return l;
382
383 return NULL;
384}
385
386/* Called from syscall or from eBPF program */
387static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
388{
389 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
390 struct hlist_head *head;
391 struct htab_elem *l;
392 u32 hash, key_size;
393
394 /* Must be called with rcu_read_lock. */
395 WARN_ON_ONCE(!rcu_read_lock_held());
396
397 key_size = map->key_size;
398
399 hash = htab_map_hash(key, key_size);
400
401 head = select_bucket(htab, hash);
402
403 l = lookup_elem_raw(head, hash, key, key_size);
404
405 return l;
406}
407
408static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
409{
410 struct htab_elem *l = __htab_map_lookup_elem(map, key);
411
412 if (l)
413 return l->key + round_up(map->key_size, 8);
414
415 return NULL;
416}
417
418static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
419{
420 struct htab_elem *l = __htab_map_lookup_elem(map, key);
421
422 if (l) {
423 bpf_lru_node_set_ref(&l->lru_node);
424 return l->key + round_up(map->key_size, 8);
425 }
426
427 return NULL;
428}
429
430/* It is called from the bpf_lru_list when the LRU needs to delete
431 * older elements from the htab.
432 */
433static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
434{
435 struct bpf_htab *htab = (struct bpf_htab *)arg;
436 struct htab_elem *l, *tgt_l;
437 struct hlist_head *head;
438 unsigned long flags;
439 struct bucket *b;
440
441 tgt_l = container_of(node, struct htab_elem, lru_node);
442 b = __select_bucket(htab, tgt_l->hash);
443 head = &b->head;
444
445 raw_spin_lock_irqsave(&b->lock, flags);
446
447 hlist_for_each_entry_rcu(l, head, hash_node)
448 if (l == tgt_l) {
449 hlist_del_rcu(&l->hash_node);
450 break;
451 }
452
453 raw_spin_unlock_irqrestore(&b->lock, flags);
454
455 return l == tgt_l;
456}
457
458/* Called from syscall */
459static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
460{
461 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
462 struct hlist_head *head;
463 struct htab_elem *l, *next_l;
464 u32 hash, key_size;
465 int i;
466
467 WARN_ON_ONCE(!rcu_read_lock_held());
468
469 key_size = map->key_size;
470
471 hash = htab_map_hash(key, key_size);
472
473 head = select_bucket(htab, hash);
474
475 /* lookup the key */
476 l = lookup_elem_raw(head, hash, key, key_size);
477
478 if (!l) {
479 i = 0;
480 goto find_first_elem;
481 }
482
483 /* key was found, get next key in the same bucket */
484 next_l = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&l->hash_node)),
485 struct htab_elem, hash_node);
486
487 if (next_l) {
488 /* if next elem in this hash list is non-zero, just return it */
489 memcpy(next_key, next_l->key, key_size);
490 return 0;
491 }
492
493 /* no more elements in this hash list, go to the next bucket */
494 i = hash & (htab->n_buckets - 1);
495 i++;
496
497find_first_elem:
498 /* iterate over buckets */
499 for (; i < htab->n_buckets; i++) {
500 head = select_bucket(htab, i);
501
502 /* pick first element in the bucket */
503 next_l = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
504 struct htab_elem, hash_node);
505 if (next_l) {
506 /* if it's not empty, just return it */
507 memcpy(next_key, next_l->key, key_size);
508 return 0;
509 }
510 }
511
512 /* iterated over all buckets and all elements */
513 return -ENOENT;
514}
515
516static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
517{
518 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
519 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
520 kfree(l);
521}
522
523static void htab_elem_free_rcu(struct rcu_head *head)
524{
525 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
526 struct bpf_htab *htab = l->htab;
527
528 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
529 * we're calling kfree, otherwise deadlock is possible if kprobes
530 * are placed somewhere inside of slub
531 */
532 preempt_disable();
533 __this_cpu_inc(bpf_prog_active);
534 htab_elem_free(htab, l);
535 __this_cpu_dec(bpf_prog_active);
536 preempt_enable();
537}
538
539static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
540{
541 if (l->state == HTAB_EXTRA_ELEM_USED) {
542 l->state = HTAB_EXTRA_ELEM_FREE;
543 return;
544 }
545
546 if (!(htab->map.map_flags & BPF_F_NO_PREALLOC)) {
547 pcpu_freelist_push(&htab->freelist, &l->fnode);
548 } else {
549 atomic_dec(&htab->count);
550 l->htab = htab;
551 call_rcu(&l->rcu, htab_elem_free_rcu);
552 }
553}
554
555static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
556 void *value, bool onallcpus)
557{
558 if (!onallcpus) {
559 /* copy true value_size bytes */
560 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
561 } else {
562 u32 size = round_up(htab->map.value_size, 8);
563 int off = 0, cpu;
564
565 for_each_possible_cpu(cpu) {
566 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
567 value + off, size);
568 off += size;
569 }
570 }
571}
572
573static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
574 void *value, u32 key_size, u32 hash,
575 bool percpu, bool onallcpus,
576 bool old_elem_exists)
577{
578 u32 size = htab->map.value_size;
579 bool prealloc = !(htab->map.map_flags & BPF_F_NO_PREALLOC);
580 struct htab_elem *l_new;
581 void __percpu *pptr;
582 int err = 0;
583
584 if (prealloc) {
585 l_new = (struct htab_elem *)pcpu_freelist_pop(&htab->freelist);
586 if (!l_new)
587 err = -E2BIG;
588 } else {
589 if (atomic_inc_return(&htab->count) > htab->map.max_entries) {
590 atomic_dec(&htab->count);
591 err = -E2BIG;
592 } else {
593 l_new = kmalloc(htab->elem_size,
594 GFP_ATOMIC | __GFP_NOWARN);
595 if (!l_new)
596 return ERR_PTR(-ENOMEM);
597 }
598 }
599
600 if (err) {
601 if (!old_elem_exists)
602 return ERR_PTR(err);
603
604 /* if we're updating the existing element and the hash table
605 * is full, use per-cpu extra elems
606 */
607 l_new = this_cpu_ptr(htab->extra_elems);
608 if (l_new->state != HTAB_EXTRA_ELEM_FREE)
609 return ERR_PTR(-E2BIG);
610 l_new->state = HTAB_EXTRA_ELEM_USED;
611 } else {
612 l_new->state = HTAB_NOT_AN_EXTRA_ELEM;
613 }
614
615 memcpy(l_new->key, key, key_size);
616 if (percpu) {
617 /* round up value_size to 8 bytes */
618 size = round_up(size, 8);
619
620 if (prealloc) {
621 pptr = htab_elem_get_ptr(l_new, key_size);
622 } else {
623 /* alloc_percpu zero-fills */
624 pptr = __alloc_percpu_gfp(size, 8,
625 GFP_ATOMIC | __GFP_NOWARN);
626 if (!pptr) {
627 kfree(l_new);
628 return ERR_PTR(-ENOMEM);
629 }
630 }
631
632 pcpu_copy_value(htab, pptr, value, onallcpus);
633
634 if (!prealloc)
635 htab_elem_set_ptr(l_new, key_size, pptr);
636 } else {
637 memcpy(l_new->key + round_up(key_size, 8), value, size);
638 }
639
640 l_new->hash = hash;
641 return l_new;
642}
643
644static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
645 u64 map_flags)
646{
647 if (l_old && map_flags == BPF_NOEXIST)
648 /* elem already exists */
649 return -EEXIST;
650
651 if (!l_old && map_flags == BPF_EXIST)
652 /* elem doesn't exist, cannot update it */
653 return -ENOENT;
654
655 return 0;
656}
657
658/* Called from syscall or from eBPF program */
659static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
660 u64 map_flags)
661{
662 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
663 struct htab_elem *l_new = NULL, *l_old;
664 struct hlist_head *head;
665 unsigned long flags;
666 struct bucket *b;
667 u32 key_size, hash;
668 int ret;
669
670 if (unlikely(map_flags > BPF_EXIST))
671 /* unknown flags */
672 return -EINVAL;
673
674 WARN_ON_ONCE(!rcu_read_lock_held());
675
676 key_size = map->key_size;
677
678 hash = htab_map_hash(key, key_size);
679
680 b = __select_bucket(htab, hash);
681 head = &b->head;
682
683 /* bpf_map_update_elem() can be called in_irq() */
684 raw_spin_lock_irqsave(&b->lock, flags);
685
686 l_old = lookup_elem_raw(head, hash, key, key_size);
687
688 ret = check_flags(htab, l_old, map_flags);
689 if (ret)
690 goto err;
691
692 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
693 !!l_old);
694 if (IS_ERR(l_new)) {
695 /* all pre-allocated elements are in use or memory exhausted */
696 ret = PTR_ERR(l_new);
697 goto err;
698 }
699
700 /* add new element to the head of the list, so that
701 * concurrent search will find it before old elem
702 */
703 hlist_add_head_rcu(&l_new->hash_node, head);
704 if (l_old) {
705 hlist_del_rcu(&l_old->hash_node);
706 free_htab_elem(htab, l_old);
707 }
708 ret = 0;
709err:
710 raw_spin_unlock_irqrestore(&b->lock, flags);
711 return ret;
712}
713
714static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
715 u64 map_flags)
716{
717 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
718 struct htab_elem *l_new, *l_old = NULL;
719 struct hlist_head *head;
720 unsigned long flags;
721 struct bucket *b;
722 u32 key_size, hash;
723 int ret;
724
725 if (unlikely(map_flags > BPF_EXIST))
726 /* unknown flags */
727 return -EINVAL;
728
729 WARN_ON_ONCE(!rcu_read_lock_held());
730
731 key_size = map->key_size;
732
733 hash = htab_map_hash(key, key_size);
734
735 b = __select_bucket(htab, hash);
736 head = &b->head;
737
738 /* For LRU, we need to alloc before taking bucket's
739 * spinlock because getting free nodes from LRU may need
740 * to remove older elements from htab and this removal
741 * operation will need a bucket lock.
742 */
743 l_new = prealloc_lru_pop(htab, key, hash);
744 if (!l_new)
745 return -ENOMEM;
746 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
747
748 /* bpf_map_update_elem() can be called in_irq() */
749 raw_spin_lock_irqsave(&b->lock, flags);
750
751 l_old = lookup_elem_raw(head, hash, key, key_size);
752
753 ret = check_flags(htab, l_old, map_flags);
754 if (ret)
755 goto err;
756
757 /* add new element to the head of the list, so that
758 * concurrent search will find it before old elem
759 */
760 hlist_add_head_rcu(&l_new->hash_node, head);
761 if (l_old) {
762 bpf_lru_node_set_ref(&l_new->lru_node);
763 hlist_del_rcu(&l_old->hash_node);
764 }
765 ret = 0;
766
767err:
768 raw_spin_unlock_irqrestore(&b->lock, flags);
769
770 if (ret)
771 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
772 else if (l_old)
773 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
774
775 return ret;
776}
777
778static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
779 void *value, u64 map_flags,
780 bool onallcpus)
781{
782 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
783 struct htab_elem *l_new = NULL, *l_old;
784 struct hlist_head *head;
785 unsigned long flags;
786 struct bucket *b;
787 u32 key_size, hash;
788 int ret;
789
790 if (unlikely(map_flags > BPF_EXIST))
791 /* unknown flags */
792 return -EINVAL;
793
794 WARN_ON_ONCE(!rcu_read_lock_held());
795
796 key_size = map->key_size;
797
798 hash = htab_map_hash(key, key_size);
799
800 b = __select_bucket(htab, hash);
801 head = &b->head;
802
803 /* bpf_map_update_elem() can be called in_irq() */
804 raw_spin_lock_irqsave(&b->lock, flags);
805
806 l_old = lookup_elem_raw(head, hash, key, key_size);
807
808 ret = check_flags(htab, l_old, map_flags);
809 if (ret)
810 goto err;
811
812 if (l_old) {
813 /* per-cpu hash map can update value in-place */
814 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
815 value, onallcpus);
816 } else {
817 l_new = alloc_htab_elem(htab, key, value, key_size,
818 hash, true, onallcpus, false);
819 if (IS_ERR(l_new)) {
820 ret = PTR_ERR(l_new);
821 goto err;
822 }
823 hlist_add_head_rcu(&l_new->hash_node, head);
824 }
825 ret = 0;
826err:
827 raw_spin_unlock_irqrestore(&b->lock, flags);
828 return ret;
829}
830
831static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
832 void *value, u64 map_flags,
833 bool onallcpus)
834{
835 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
836 struct htab_elem *l_new = NULL, *l_old;
837 struct hlist_head *head;
838 unsigned long flags;
839 struct bucket *b;
840 u32 key_size, hash;
841 int ret;
842
843 if (unlikely(map_flags > BPF_EXIST))
844 /* unknown flags */
845 return -EINVAL;
846
847 WARN_ON_ONCE(!rcu_read_lock_held());
848
849 key_size = map->key_size;
850
851 hash = htab_map_hash(key, key_size);
852
853 b = __select_bucket(htab, hash);
854 head = &b->head;
855
856 /* For LRU, we need to alloc before taking bucket's
857 * spinlock because LRU's elem alloc may need
858 * to remove older elem from htab and this removal
859 * operation will need a bucket lock.
860 */
861 if (map_flags != BPF_EXIST) {
862 l_new = prealloc_lru_pop(htab, key, hash);
863 if (!l_new)
864 return -ENOMEM;
865 }
866
867 /* bpf_map_update_elem() can be called in_irq() */
868 raw_spin_lock_irqsave(&b->lock, flags);
869
870 l_old = lookup_elem_raw(head, hash, key, key_size);
871
872 ret = check_flags(htab, l_old, map_flags);
873 if (ret)
874 goto err;
875
876 if (l_old) {
877 bpf_lru_node_set_ref(&l_old->lru_node);
878
879 /* per-cpu hash map can update value in-place */
880 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
881 value, onallcpus);
882 } else {
883 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
884 value, onallcpus);
885 hlist_add_head_rcu(&l_new->hash_node, head);
886 l_new = NULL;
887 }
888 ret = 0;
889err:
890 raw_spin_unlock_irqrestore(&b->lock, flags);
891 if (l_new)
892 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
893 return ret;
894}
895
896static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
897 void *value, u64 map_flags)
898{
899 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
900}
901
902static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
903 void *value, u64 map_flags)
904{
905 return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
906 false);
907}
908
909/* Called from syscall or from eBPF program */
910static int htab_map_delete_elem(struct bpf_map *map, void *key)
911{
912 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
913 struct hlist_head *head;
914 struct bucket *b;
915 struct htab_elem *l;
916 unsigned long flags;
917 u32 hash, key_size;
918 int ret = -ENOENT;
919
920 WARN_ON_ONCE(!rcu_read_lock_held());
921
922 key_size = map->key_size;
923
924 hash = htab_map_hash(key, key_size);
925 b = __select_bucket(htab, hash);
926 head = &b->head;
927
928 raw_spin_lock_irqsave(&b->lock, flags);
929
930 l = lookup_elem_raw(head, hash, key, key_size);
931
932 if (l) {
933 hlist_del_rcu(&l->hash_node);
934 free_htab_elem(htab, l);
935 ret = 0;
936 }
937
938 raw_spin_unlock_irqrestore(&b->lock, flags);
939 return ret;
940}
941
942static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
943{
944 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
945 struct hlist_head *head;
946 struct bucket *b;
947 struct htab_elem *l;
948 unsigned long flags;
949 u32 hash, key_size;
950 int ret = -ENOENT;
951
952 WARN_ON_ONCE(!rcu_read_lock_held());
953
954 key_size = map->key_size;
955
956 hash = htab_map_hash(key, key_size);
957 b = __select_bucket(htab, hash);
958 head = &b->head;
959
960 raw_spin_lock_irqsave(&b->lock, flags);
961
962 l = lookup_elem_raw(head, hash, key, key_size);
963
964 if (l) {
965 hlist_del_rcu(&l->hash_node);
966 ret = 0;
967 }
968
969 raw_spin_unlock_irqrestore(&b->lock, flags);
970 if (l)
971 bpf_lru_push_free(&htab->lru, &l->lru_node);
972 return ret;
973}
974
975static void delete_all_elements(struct bpf_htab *htab)
976{
977 int i;
978
979 for (i = 0; i < htab->n_buckets; i++) {
980 struct hlist_head *head = select_bucket(htab, i);
981 struct hlist_node *n;
982 struct htab_elem *l;
983
984 hlist_for_each_entry_safe(l, n, head, hash_node) {
985 hlist_del_rcu(&l->hash_node);
986 if (l->state != HTAB_EXTRA_ELEM_USED)
987 htab_elem_free(htab, l);
988 }
989 }
990}
991/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
992static void htab_map_free(struct bpf_map *map)
993{
994 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
995
996 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
997 * so the programs (can be more than one that used this map) were
998 * disconnected from events. Wait for outstanding critical sections in
999 * these programs to complete
1000 */
1001 synchronize_rcu();
1002
1003 /* some of free_htab_elem() callbacks for elements of this map may
1004 * not have executed. Wait for them.
1005 */
1006 rcu_barrier();
1007 if (htab->map.map_flags & BPF_F_NO_PREALLOC)
1008 delete_all_elements(htab);
1009 else
1010 prealloc_destroy(htab);
1011
1012 free_percpu(htab->extra_elems);
1013 bpf_map_area_free(htab->buckets);
1014 kfree(htab);
1015}
1016
1017static const struct bpf_map_ops htab_ops = {
1018 .map_alloc = htab_map_alloc,
1019 .map_free = htab_map_free,
1020 .map_get_next_key = htab_map_get_next_key,
1021 .map_lookup_elem = htab_map_lookup_elem,
1022 .map_update_elem = htab_map_update_elem,
1023 .map_delete_elem = htab_map_delete_elem,
1024};
1025
1026static struct bpf_map_type_list htab_type __read_mostly = {
1027 .ops = &htab_ops,
1028 .type = BPF_MAP_TYPE_HASH,
1029};
1030
1031static const struct bpf_map_ops htab_lru_ops = {
1032 .map_alloc = htab_map_alloc,
1033 .map_free = htab_map_free,
1034 .map_get_next_key = htab_map_get_next_key,
1035 .map_lookup_elem = htab_lru_map_lookup_elem,
1036 .map_update_elem = htab_lru_map_update_elem,
1037 .map_delete_elem = htab_lru_map_delete_elem,
1038};
1039
1040static struct bpf_map_type_list htab_lru_type __read_mostly = {
1041 .ops = &htab_lru_ops,
1042 .type = BPF_MAP_TYPE_LRU_HASH,
1043};
1044
1045/* Called from eBPF program */
1046static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1047{
1048 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1049
1050 if (l)
1051 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1052 else
1053 return NULL;
1054}
1055
1056static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1057{
1058 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1059
1060 if (l) {
1061 bpf_lru_node_set_ref(&l->lru_node);
1062 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1063 }
1064
1065 return NULL;
1066}
1067
1068int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1069{
1070 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1071 struct htab_elem *l;
1072 void __percpu *pptr;
1073 int ret = -ENOENT;
1074 int cpu, off = 0;
1075 u32 size;
1076
1077 /* per_cpu areas are zero-filled and bpf programs can only
1078 * access 'value_size' of them, so copying rounded areas
1079 * will not leak any kernel data
1080 */
1081 size = round_up(map->value_size, 8);
1082 rcu_read_lock();
1083 l = __htab_map_lookup_elem(map, key);
1084 if (!l)
1085 goto out;
1086 if (htab_is_lru(htab))
1087 bpf_lru_node_set_ref(&l->lru_node);
1088 pptr = htab_elem_get_ptr(l, map->key_size);
1089 for_each_possible_cpu(cpu) {
1090 bpf_long_memcpy(value + off,
1091 per_cpu_ptr(pptr, cpu), size);
1092 off += size;
1093 }
1094 ret = 0;
1095out:
1096 rcu_read_unlock();
1097 return ret;
1098}
1099
1100int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1101 u64 map_flags)
1102{
1103 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1104 int ret;
1105
1106 rcu_read_lock();
1107 if (htab_is_lru(htab))
1108 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1109 map_flags, true);
1110 else
1111 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1112 true);
1113 rcu_read_unlock();
1114
1115 return ret;
1116}
1117
1118static const struct bpf_map_ops htab_percpu_ops = {
1119 .map_alloc = htab_map_alloc,
1120 .map_free = htab_map_free,
1121 .map_get_next_key = htab_map_get_next_key,
1122 .map_lookup_elem = htab_percpu_map_lookup_elem,
1123 .map_update_elem = htab_percpu_map_update_elem,
1124 .map_delete_elem = htab_map_delete_elem,
1125};
1126
1127static struct bpf_map_type_list htab_percpu_type __read_mostly = {
1128 .ops = &htab_percpu_ops,
1129 .type = BPF_MAP_TYPE_PERCPU_HASH,
1130};
1131
1132static const struct bpf_map_ops htab_lru_percpu_ops = {
1133 .map_alloc = htab_map_alloc,
1134 .map_free = htab_map_free,
1135 .map_get_next_key = htab_map_get_next_key,
1136 .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1137 .map_update_elem = htab_lru_percpu_map_update_elem,
1138 .map_delete_elem = htab_lru_map_delete_elem,
1139};
1140
1141static struct bpf_map_type_list htab_lru_percpu_type __read_mostly = {
1142 .ops = &htab_lru_percpu_ops,
1143 .type = BPF_MAP_TYPE_LRU_PERCPU_HASH,
1144};
1145
1146static int __init register_htab_map(void)
1147{
1148 bpf_register_map_type(&htab_type);
1149 bpf_register_map_type(&htab_percpu_type);
1150 bpf_register_map_type(&htab_lru_type);
1151 bpf_register_map_type(&htab_lru_percpu_type);
1152 return 0;
1153}
1154late_initcall(register_htab_map);