kvm: x86: optimize dr6 restore
[linux/fpc-iii.git] / kernel / bpf / hashtab.c
blob03cc59ee9c9536b885d605027af093c3dd0634ee
1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 * Copyright (c) 2016 Facebook
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.
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.
13 #include <linux/bpf.h>
14 #include <linux/btf.h>
15 #include <linux/jhash.h>
16 #include <linux/filter.h>
17 #include <linux/rculist_nulls.h>
18 #include <linux/random.h>
19 #include <uapi/linux/btf.h>
20 #include "percpu_freelist.h"
21 #include "bpf_lru_list.h"
22 #include "map_in_map.h"
24 #define HTAB_CREATE_FLAG_MASK \
25 (BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE | \
26 BPF_F_RDONLY | BPF_F_WRONLY)
28 struct bucket {
29 struct hlist_nulls_head head;
30 raw_spinlock_t lock;
33 struct bpf_htab {
34 struct bpf_map map;
35 struct bucket *buckets;
36 void *elems;
37 union {
38 struct pcpu_freelist freelist;
39 struct bpf_lru lru;
41 struct htab_elem *__percpu *extra_elems;
42 atomic_t count; /* number of elements in this hashtable */
43 u32 n_buckets; /* number of hash buckets */
44 u32 elem_size; /* size of each element in bytes */
45 u32 hashrnd;
48 /* each htab element is struct htab_elem + key + value */
49 struct htab_elem {
50 union {
51 struct hlist_nulls_node hash_node;
52 struct {
53 void *padding;
54 union {
55 struct bpf_htab *htab;
56 struct pcpu_freelist_node fnode;
60 union {
61 struct rcu_head rcu;
62 struct bpf_lru_node lru_node;
64 u32 hash;
65 char key[0] __aligned(8);
68 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
70 static bool htab_is_lru(const struct bpf_htab *htab)
72 return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
73 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
76 static bool htab_is_percpu(const struct bpf_htab *htab)
78 return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
79 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
82 static bool htab_is_prealloc(const struct bpf_htab *htab)
84 return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
87 static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
88 void __percpu *pptr)
90 *(void __percpu **)(l->key + key_size) = pptr;
93 static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
95 return *(void __percpu **)(l->key + key_size);
98 static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l)
100 return *(void **)(l->key + roundup(map->key_size, 8));
103 static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
105 return (struct htab_elem *) (htab->elems + i * htab->elem_size);
108 static void htab_free_elems(struct bpf_htab *htab)
110 int i;
112 if (!htab_is_percpu(htab))
113 goto free_elems;
115 for (i = 0; i < htab->map.max_entries; i++) {
116 void __percpu *pptr;
118 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
119 htab->map.key_size);
120 free_percpu(pptr);
121 cond_resched();
123 free_elems:
124 bpf_map_area_free(htab->elems);
127 static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
128 u32 hash)
130 struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
131 struct htab_elem *l;
133 if (node) {
134 l = container_of(node, struct htab_elem, lru_node);
135 memcpy(l->key, key, htab->map.key_size);
136 return l;
139 return NULL;
142 static int prealloc_init(struct bpf_htab *htab)
144 u32 num_entries = htab->map.max_entries;
145 int err = -ENOMEM, i;
147 if (!htab_is_percpu(htab) && !htab_is_lru(htab))
148 num_entries += num_possible_cpus();
150 htab->elems = bpf_map_area_alloc(htab->elem_size * num_entries,
151 htab->map.numa_node);
152 if (!htab->elems)
153 return -ENOMEM;
155 if (!htab_is_percpu(htab))
156 goto skip_percpu_elems;
158 for (i = 0; i < num_entries; i++) {
159 u32 size = round_up(htab->map.value_size, 8);
160 void __percpu *pptr;
162 pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
163 if (!pptr)
164 goto free_elems;
165 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
166 pptr);
167 cond_resched();
170 skip_percpu_elems:
171 if (htab_is_lru(htab))
172 err = bpf_lru_init(&htab->lru,
173 htab->map.map_flags & BPF_F_NO_COMMON_LRU,
174 offsetof(struct htab_elem, hash) -
175 offsetof(struct htab_elem, lru_node),
176 htab_lru_map_delete_node,
177 htab);
178 else
179 err = pcpu_freelist_init(&htab->freelist);
181 if (err)
182 goto free_elems;
184 if (htab_is_lru(htab))
185 bpf_lru_populate(&htab->lru, htab->elems,
186 offsetof(struct htab_elem, lru_node),
187 htab->elem_size, num_entries);
188 else
189 pcpu_freelist_populate(&htab->freelist,
190 htab->elems + offsetof(struct htab_elem, fnode),
191 htab->elem_size, num_entries);
193 return 0;
195 free_elems:
196 htab_free_elems(htab);
197 return err;
200 static void prealloc_destroy(struct bpf_htab *htab)
202 htab_free_elems(htab);
204 if (htab_is_lru(htab))
205 bpf_lru_destroy(&htab->lru);
206 else
207 pcpu_freelist_destroy(&htab->freelist);
210 static int alloc_extra_elems(struct bpf_htab *htab)
212 struct htab_elem *__percpu *pptr, *l_new;
213 struct pcpu_freelist_node *l;
214 int cpu;
216 pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8,
217 GFP_USER | __GFP_NOWARN);
218 if (!pptr)
219 return -ENOMEM;
221 for_each_possible_cpu(cpu) {
222 l = pcpu_freelist_pop(&htab->freelist);
223 /* pop will succeed, since prealloc_init()
224 * preallocated extra num_possible_cpus elements
226 l_new = container_of(l, struct htab_elem, fnode);
227 *per_cpu_ptr(pptr, cpu) = l_new;
229 htab->extra_elems = pptr;
230 return 0;
233 /* Called from syscall */
234 static int htab_map_alloc_check(union bpf_attr *attr)
236 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
237 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
238 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
239 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
240 /* percpu_lru means each cpu has its own LRU list.
241 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
242 * the map's value itself is percpu. percpu_lru has
243 * nothing to do with the map's value.
245 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
246 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
247 int numa_node = bpf_map_attr_numa_node(attr);
249 BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
250 offsetof(struct htab_elem, hash_node.pprev));
251 BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
252 offsetof(struct htab_elem, hash_node.pprev));
254 if (lru && !capable(CAP_SYS_ADMIN))
255 /* LRU implementation is much complicated than other
256 * maps. Hence, limit to CAP_SYS_ADMIN for now.
258 return -EPERM;
260 if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK)
261 /* reserved bits should not be used */
262 return -EINVAL;
264 if (!lru && percpu_lru)
265 return -EINVAL;
267 if (lru && !prealloc)
268 return -ENOTSUPP;
270 if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
271 return -EINVAL;
273 /* check sanity of attributes.
274 * value_size == 0 may be allowed in the future to use map as a set
276 if (attr->max_entries == 0 || attr->key_size == 0 ||
277 attr->value_size == 0)
278 return -EINVAL;
280 if (attr->key_size > MAX_BPF_STACK)
281 /* eBPF programs initialize keys on stack, so they cannot be
282 * larger than max stack size
284 return -E2BIG;
286 if (attr->value_size >= KMALLOC_MAX_SIZE -
287 MAX_BPF_STACK - sizeof(struct htab_elem))
288 /* if value_size is bigger, the user space won't be able to
289 * access the elements via bpf syscall. This check also makes
290 * sure that the elem_size doesn't overflow and it's
291 * kmalloc-able later in htab_map_update_elem()
293 return -E2BIG;
295 return 0;
298 static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
300 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
301 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
302 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
303 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
304 /* percpu_lru means each cpu has its own LRU list.
305 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
306 * the map's value itself is percpu. percpu_lru has
307 * nothing to do with the map's value.
309 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
310 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
311 struct bpf_htab *htab;
312 int err, i;
313 u64 cost;
315 htab = kzalloc(sizeof(*htab), GFP_USER);
316 if (!htab)
317 return ERR_PTR(-ENOMEM);
319 bpf_map_init_from_attr(&htab->map, attr);
321 if (percpu_lru) {
322 /* ensure each CPU's lru list has >=1 elements.
323 * since we are at it, make each lru list has the same
324 * number of elements.
326 htab->map.max_entries = roundup(attr->max_entries,
327 num_possible_cpus());
328 if (htab->map.max_entries < attr->max_entries)
329 htab->map.max_entries = rounddown(attr->max_entries,
330 num_possible_cpus());
333 /* hash table size must be power of 2 */
334 htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
336 htab->elem_size = sizeof(struct htab_elem) +
337 round_up(htab->map.key_size, 8);
338 if (percpu)
339 htab->elem_size += sizeof(void *);
340 else
341 htab->elem_size += round_up(htab->map.value_size, 8);
343 err = -E2BIG;
344 /* prevent zero size kmalloc and check for u32 overflow */
345 if (htab->n_buckets == 0 ||
346 htab->n_buckets > U32_MAX / sizeof(struct bucket))
347 goto free_htab;
349 cost = (u64) htab->n_buckets * sizeof(struct bucket) +
350 (u64) htab->elem_size * htab->map.max_entries;
352 if (percpu)
353 cost += (u64) round_up(htab->map.value_size, 8) *
354 num_possible_cpus() * htab->map.max_entries;
355 else
356 cost += (u64) htab->elem_size * num_possible_cpus();
358 if (cost >= U32_MAX - PAGE_SIZE)
359 /* make sure page count doesn't overflow */
360 goto free_htab;
362 htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
364 /* if map size is larger than memlock limit, reject it early */
365 err = bpf_map_precharge_memlock(htab->map.pages);
366 if (err)
367 goto free_htab;
369 err = -ENOMEM;
370 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
371 sizeof(struct bucket),
372 htab->map.numa_node);
373 if (!htab->buckets)
374 goto free_htab;
376 htab->hashrnd = get_random_int();
377 for (i = 0; i < htab->n_buckets; i++) {
378 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
379 raw_spin_lock_init(&htab->buckets[i].lock);
382 if (prealloc) {
383 err = prealloc_init(htab);
384 if (err)
385 goto free_buckets;
387 if (!percpu && !lru) {
388 /* lru itself can remove the least used element, so
389 * there is no need for an extra elem during map_update.
391 err = alloc_extra_elems(htab);
392 if (err)
393 goto free_prealloc;
397 return &htab->map;
399 free_prealloc:
400 prealloc_destroy(htab);
401 free_buckets:
402 bpf_map_area_free(htab->buckets);
403 free_htab:
404 kfree(htab);
405 return ERR_PTR(err);
408 static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
410 return jhash(key, key_len, hashrnd);
413 static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
415 return &htab->buckets[hash & (htab->n_buckets - 1)];
418 static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
420 return &__select_bucket(htab, hash)->head;
423 /* this lookup function can only be called with bucket lock taken */
424 static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
425 void *key, u32 key_size)
427 struct hlist_nulls_node *n;
428 struct htab_elem *l;
430 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
431 if (l->hash == hash && !memcmp(&l->key, key, key_size))
432 return l;
434 return NULL;
437 /* can be called without bucket lock. it will repeat the loop in
438 * the unlikely event when elements moved from one bucket into another
439 * while link list is being walked
441 static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
442 u32 hash, void *key,
443 u32 key_size, u32 n_buckets)
445 struct hlist_nulls_node *n;
446 struct htab_elem *l;
448 again:
449 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
450 if (l->hash == hash && !memcmp(&l->key, key, key_size))
451 return l;
453 if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
454 goto again;
456 return NULL;
459 /* Called from syscall or from eBPF program directly, so
460 * arguments have to match bpf_map_lookup_elem() exactly.
461 * The return value is adjusted by BPF instructions
462 * in htab_map_gen_lookup().
464 static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
466 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
467 struct hlist_nulls_head *head;
468 struct htab_elem *l;
469 u32 hash, key_size;
471 /* Must be called with rcu_read_lock. */
472 WARN_ON_ONCE(!rcu_read_lock_held());
474 key_size = map->key_size;
476 hash = htab_map_hash(key, key_size, htab->hashrnd);
478 head = select_bucket(htab, hash);
480 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
482 return l;
485 static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
487 struct htab_elem *l = __htab_map_lookup_elem(map, key);
489 if (l)
490 return l->key + round_up(map->key_size, 8);
492 return NULL;
495 /* inline bpf_map_lookup_elem() call.
496 * Instead of:
497 * bpf_prog
498 * bpf_map_lookup_elem
499 * map->ops->map_lookup_elem
500 * htab_map_lookup_elem
501 * __htab_map_lookup_elem
502 * do:
503 * bpf_prog
504 * __htab_map_lookup_elem
506 static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
508 struct bpf_insn *insn = insn_buf;
509 const int ret = BPF_REG_0;
511 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
512 (void *(*)(struct bpf_map *map, void *key))NULL));
513 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
514 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
515 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
516 offsetof(struct htab_elem, key) +
517 round_up(map->key_size, 8));
518 return insn - insn_buf;
521 static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
523 struct htab_elem *l = __htab_map_lookup_elem(map, key);
525 if (l) {
526 bpf_lru_node_set_ref(&l->lru_node);
527 return l->key + round_up(map->key_size, 8);
530 return NULL;
533 static u32 htab_lru_map_gen_lookup(struct bpf_map *map,
534 struct bpf_insn *insn_buf)
536 struct bpf_insn *insn = insn_buf;
537 const int ret = BPF_REG_0;
538 const int ref_reg = BPF_REG_1;
540 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
541 (void *(*)(struct bpf_map *map, void *key))NULL));
542 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
543 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
544 *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
545 offsetof(struct htab_elem, lru_node) +
546 offsetof(struct bpf_lru_node, ref));
547 *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
548 *insn++ = BPF_ST_MEM(BPF_B, ret,
549 offsetof(struct htab_elem, lru_node) +
550 offsetof(struct bpf_lru_node, ref),
552 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
553 offsetof(struct htab_elem, key) +
554 round_up(map->key_size, 8));
555 return insn - insn_buf;
558 /* It is called from the bpf_lru_list when the LRU needs to delete
559 * older elements from the htab.
561 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
563 struct bpf_htab *htab = (struct bpf_htab *)arg;
564 struct htab_elem *l = NULL, *tgt_l;
565 struct hlist_nulls_head *head;
566 struct hlist_nulls_node *n;
567 unsigned long flags;
568 struct bucket *b;
570 tgt_l = container_of(node, struct htab_elem, lru_node);
571 b = __select_bucket(htab, tgt_l->hash);
572 head = &b->head;
574 raw_spin_lock_irqsave(&b->lock, flags);
576 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
577 if (l == tgt_l) {
578 hlist_nulls_del_rcu(&l->hash_node);
579 break;
582 raw_spin_unlock_irqrestore(&b->lock, flags);
584 return l == tgt_l;
587 /* Called from syscall */
588 static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
590 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
591 struct hlist_nulls_head *head;
592 struct htab_elem *l, *next_l;
593 u32 hash, key_size;
594 int i = 0;
596 WARN_ON_ONCE(!rcu_read_lock_held());
598 key_size = map->key_size;
600 if (!key)
601 goto find_first_elem;
603 hash = htab_map_hash(key, key_size, htab->hashrnd);
605 head = select_bucket(htab, hash);
607 /* lookup the key */
608 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
610 if (!l)
611 goto find_first_elem;
613 /* key was found, get next key in the same bucket */
614 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
615 struct htab_elem, hash_node);
617 if (next_l) {
618 /* if next elem in this hash list is non-zero, just return it */
619 memcpy(next_key, next_l->key, key_size);
620 return 0;
623 /* no more elements in this hash list, go to the next bucket */
624 i = hash & (htab->n_buckets - 1);
625 i++;
627 find_first_elem:
628 /* iterate over buckets */
629 for (; i < htab->n_buckets; i++) {
630 head = select_bucket(htab, i);
632 /* pick first element in the bucket */
633 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
634 struct htab_elem, hash_node);
635 if (next_l) {
636 /* if it's not empty, just return it */
637 memcpy(next_key, next_l->key, key_size);
638 return 0;
642 /* iterated over all buckets and all elements */
643 return -ENOENT;
646 static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
648 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
649 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
650 kfree(l);
653 static void htab_elem_free_rcu(struct rcu_head *head)
655 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
656 struct bpf_htab *htab = l->htab;
658 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
659 * we're calling kfree, otherwise deadlock is possible if kprobes
660 * are placed somewhere inside of slub
662 preempt_disable();
663 __this_cpu_inc(bpf_prog_active);
664 htab_elem_free(htab, l);
665 __this_cpu_dec(bpf_prog_active);
666 preempt_enable();
669 static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
671 struct bpf_map *map = &htab->map;
673 if (map->ops->map_fd_put_ptr) {
674 void *ptr = fd_htab_map_get_ptr(map, l);
676 map->ops->map_fd_put_ptr(ptr);
679 if (htab_is_prealloc(htab)) {
680 pcpu_freelist_push(&htab->freelist, &l->fnode);
681 } else {
682 atomic_dec(&htab->count);
683 l->htab = htab;
684 call_rcu(&l->rcu, htab_elem_free_rcu);
688 static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
689 void *value, bool onallcpus)
691 if (!onallcpus) {
692 /* copy true value_size bytes */
693 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
694 } else {
695 u32 size = round_up(htab->map.value_size, 8);
696 int off = 0, cpu;
698 for_each_possible_cpu(cpu) {
699 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
700 value + off, size);
701 off += size;
706 static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
708 return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
709 BITS_PER_LONG == 64;
712 static u32 htab_size_value(const struct bpf_htab *htab, bool percpu)
714 u32 size = htab->map.value_size;
716 if (percpu || fd_htab_map_needs_adjust(htab))
717 size = round_up(size, 8);
718 return size;
721 static 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)
726 u32 size = htab_size_value(htab, percpu);
727 bool prealloc = htab_is_prealloc(htab);
728 struct htab_elem *l_new, **pl_new;
729 void __percpu *pptr;
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
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;
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);
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
755 l_new = ERR_PTR(-E2BIG);
756 goto dec_count;
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;
766 memcpy(l_new->key, key, key_size);
767 if (percpu) {
768 if (prealloc) {
769 pptr = htab_elem_get_ptr(l_new, key_size);
770 } else {
771 /* alloc_percpu zero-fills */
772 pptr = __alloc_percpu_gfp(size, 8,
773 GFP_ATOMIC | __GFP_NOWARN);
774 if (!pptr) {
775 kfree(l_new);
776 l_new = ERR_PTR(-ENOMEM);
777 goto dec_count;
781 pcpu_copy_value(htab, pptr, value, onallcpus);
783 if (!prealloc)
784 htab_elem_set_ptr(l_new, key_size, pptr);
785 } else {
786 memcpy(l_new->key + round_up(key_size, 8), value, size);
789 l_new->hash = hash;
790 return l_new;
791 dec_count:
792 atomic_dec(&htab->count);
793 return l_new;
796 static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
797 u64 map_flags)
799 if (l_old && map_flags == BPF_NOEXIST)
800 /* elem already exists */
801 return -EEXIST;
803 if (!l_old && map_flags == BPF_EXIST)
804 /* elem doesn't exist, cannot update it */
805 return -ENOENT;
807 return 0;
810 /* Called from syscall or from eBPF program */
811 static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
812 u64 map_flags)
814 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
815 struct htab_elem *l_new = NULL, *l_old;
816 struct hlist_nulls_head *head;
817 unsigned long flags;
818 struct bucket *b;
819 u32 key_size, hash;
820 int ret;
822 if (unlikely(map_flags > BPF_EXIST))
823 /* unknown flags */
824 return -EINVAL;
826 WARN_ON_ONCE(!rcu_read_lock_held());
828 key_size = map->key_size;
830 hash = htab_map_hash(key, key_size, htab->hashrnd);
832 b = __select_bucket(htab, hash);
833 head = &b->head;
835 /* bpf_map_update_elem() can be called in_irq() */
836 raw_spin_lock_irqsave(&b->lock, flags);
838 l_old = lookup_elem_raw(head, hash, key, key_size);
840 ret = check_flags(htab, l_old, map_flags);
841 if (ret)
842 goto err;
844 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
845 l_old);
846 if (IS_ERR(l_new)) {
847 /* all pre-allocated elements are in use or memory exhausted */
848 ret = PTR_ERR(l_new);
849 goto err;
852 /* add new element to the head of the list, so that
853 * concurrent search will find it before old elem
855 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
856 if (l_old) {
857 hlist_nulls_del_rcu(&l_old->hash_node);
858 if (!htab_is_prealloc(htab))
859 free_htab_elem(htab, l_old);
861 ret = 0;
862 err:
863 raw_spin_unlock_irqrestore(&b->lock, flags);
864 return ret;
867 static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
868 u64 map_flags)
870 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
871 struct htab_elem *l_new, *l_old = NULL;
872 struct hlist_nulls_head *head;
873 unsigned long flags;
874 struct bucket *b;
875 u32 key_size, hash;
876 int ret;
878 if (unlikely(map_flags > BPF_EXIST))
879 /* unknown flags */
880 return -EINVAL;
882 WARN_ON_ONCE(!rcu_read_lock_held());
884 key_size = map->key_size;
886 hash = htab_map_hash(key, key_size, htab->hashrnd);
888 b = __select_bucket(htab, hash);
889 head = &b->head;
891 /* For LRU, we need to alloc before taking bucket's
892 * spinlock because getting free nodes from LRU may need
893 * to remove older elements from htab and this removal
894 * operation will need a bucket lock.
896 l_new = prealloc_lru_pop(htab, key, hash);
897 if (!l_new)
898 return -ENOMEM;
899 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
901 /* bpf_map_update_elem() can be called in_irq() */
902 raw_spin_lock_irqsave(&b->lock, flags);
904 l_old = lookup_elem_raw(head, hash, key, key_size);
906 ret = check_flags(htab, l_old, map_flags);
907 if (ret)
908 goto err;
910 /* add new element to the head of the list, so that
911 * concurrent search will find it before old elem
913 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
914 if (l_old) {
915 bpf_lru_node_set_ref(&l_new->lru_node);
916 hlist_nulls_del_rcu(&l_old->hash_node);
918 ret = 0;
920 err:
921 raw_spin_unlock_irqrestore(&b->lock, flags);
923 if (ret)
924 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
925 else if (l_old)
926 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
928 return ret;
931 static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
932 void *value, u64 map_flags,
933 bool onallcpus)
935 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
936 struct htab_elem *l_new = NULL, *l_old;
937 struct hlist_nulls_head *head;
938 unsigned long flags;
939 struct bucket *b;
940 u32 key_size, hash;
941 int ret;
943 if (unlikely(map_flags > BPF_EXIST))
944 /* unknown flags */
945 return -EINVAL;
947 WARN_ON_ONCE(!rcu_read_lock_held());
949 key_size = map->key_size;
951 hash = htab_map_hash(key, key_size, htab->hashrnd);
953 b = __select_bucket(htab, hash);
954 head = &b->head;
956 /* bpf_map_update_elem() can be called in_irq() */
957 raw_spin_lock_irqsave(&b->lock, flags);
959 l_old = lookup_elem_raw(head, hash, key, key_size);
961 ret = check_flags(htab, l_old, map_flags);
962 if (ret)
963 goto err;
965 if (l_old) {
966 /* per-cpu hash map can update value in-place */
967 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
968 value, onallcpus);
969 } else {
970 l_new = alloc_htab_elem(htab, key, value, key_size,
971 hash, true, onallcpus, NULL);
972 if (IS_ERR(l_new)) {
973 ret = PTR_ERR(l_new);
974 goto err;
976 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
978 ret = 0;
979 err:
980 raw_spin_unlock_irqrestore(&b->lock, flags);
981 return ret;
984 static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
985 void *value, u64 map_flags,
986 bool onallcpus)
988 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
989 struct htab_elem *l_new = NULL, *l_old;
990 struct hlist_nulls_head *head;
991 unsigned long flags;
992 struct bucket *b;
993 u32 key_size, hash;
994 int ret;
996 if (unlikely(map_flags > BPF_EXIST))
997 /* unknown flags */
998 return -EINVAL;
1000 WARN_ON_ONCE(!rcu_read_lock_held());
1002 key_size = map->key_size;
1004 hash = htab_map_hash(key, key_size, htab->hashrnd);
1006 b = __select_bucket(htab, hash);
1007 head = &b->head;
1009 /* For LRU, we need to alloc before taking bucket's
1010 * spinlock because LRU's elem alloc may need
1011 * to remove older elem from htab and this removal
1012 * operation will need a bucket lock.
1014 if (map_flags != BPF_EXIST) {
1015 l_new = prealloc_lru_pop(htab, key, hash);
1016 if (!l_new)
1017 return -ENOMEM;
1020 /* bpf_map_update_elem() can be called in_irq() */
1021 raw_spin_lock_irqsave(&b->lock, flags);
1023 l_old = lookup_elem_raw(head, hash, key, key_size);
1025 ret = check_flags(htab, l_old, map_flags);
1026 if (ret)
1027 goto err;
1029 if (l_old) {
1030 bpf_lru_node_set_ref(&l_old->lru_node);
1032 /* per-cpu hash map can update value in-place */
1033 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1034 value, onallcpus);
1035 } else {
1036 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
1037 value, onallcpus);
1038 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1039 l_new = NULL;
1041 ret = 0;
1042 err:
1043 raw_spin_unlock_irqrestore(&b->lock, flags);
1044 if (l_new)
1045 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1046 return ret;
1049 static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1050 void *value, u64 map_flags)
1052 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
1055 static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1056 void *value, u64 map_flags)
1058 return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
1059 false);
1062 /* Called from syscall or from eBPF program */
1063 static int htab_map_delete_elem(struct bpf_map *map, void *key)
1065 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1066 struct hlist_nulls_head *head;
1067 struct bucket *b;
1068 struct htab_elem *l;
1069 unsigned long flags;
1070 u32 hash, key_size;
1071 int ret = -ENOENT;
1073 WARN_ON_ONCE(!rcu_read_lock_held());
1075 key_size = map->key_size;
1077 hash = htab_map_hash(key, key_size, htab->hashrnd);
1078 b = __select_bucket(htab, hash);
1079 head = &b->head;
1081 raw_spin_lock_irqsave(&b->lock, flags);
1083 l = lookup_elem_raw(head, hash, key, key_size);
1085 if (l) {
1086 hlist_nulls_del_rcu(&l->hash_node);
1087 free_htab_elem(htab, l);
1088 ret = 0;
1091 raw_spin_unlock_irqrestore(&b->lock, flags);
1092 return ret;
1095 static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1097 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1098 struct hlist_nulls_head *head;
1099 struct bucket *b;
1100 struct htab_elem *l;
1101 unsigned long flags;
1102 u32 hash, key_size;
1103 int ret = -ENOENT;
1105 WARN_ON_ONCE(!rcu_read_lock_held());
1107 key_size = map->key_size;
1109 hash = htab_map_hash(key, key_size, htab->hashrnd);
1110 b = __select_bucket(htab, hash);
1111 head = &b->head;
1113 raw_spin_lock_irqsave(&b->lock, flags);
1115 l = lookup_elem_raw(head, hash, key, key_size);
1117 if (l) {
1118 hlist_nulls_del_rcu(&l->hash_node);
1119 ret = 0;
1122 raw_spin_unlock_irqrestore(&b->lock, flags);
1123 if (l)
1124 bpf_lru_push_free(&htab->lru, &l->lru_node);
1125 return ret;
1128 static void delete_all_elements(struct bpf_htab *htab)
1130 int i;
1132 for (i = 0; i < htab->n_buckets; i++) {
1133 struct hlist_nulls_head *head = select_bucket(htab, i);
1134 struct hlist_nulls_node *n;
1135 struct htab_elem *l;
1137 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1138 hlist_nulls_del_rcu(&l->hash_node);
1139 htab_elem_free(htab, l);
1144 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1145 static void htab_map_free(struct bpf_map *map)
1147 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1149 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1150 * so the programs (can be more than one that used this map) were
1151 * disconnected from events. Wait for outstanding critical sections in
1152 * these programs to complete
1154 synchronize_rcu();
1156 /* some of free_htab_elem() callbacks for elements of this map may
1157 * not have executed. Wait for them.
1159 rcu_barrier();
1160 if (!htab_is_prealloc(htab))
1161 delete_all_elements(htab);
1162 else
1163 prealloc_destroy(htab);
1165 free_percpu(htab->extra_elems);
1166 bpf_map_area_free(htab->buckets);
1167 kfree(htab);
1170 static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1171 struct seq_file *m)
1173 void *value;
1175 rcu_read_lock();
1177 value = htab_map_lookup_elem(map, key);
1178 if (!value) {
1179 rcu_read_unlock();
1180 return;
1183 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1184 seq_puts(m, ": ");
1185 btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1186 seq_puts(m, "\n");
1188 rcu_read_unlock();
1191 const struct bpf_map_ops htab_map_ops = {
1192 .map_alloc_check = htab_map_alloc_check,
1193 .map_alloc = htab_map_alloc,
1194 .map_free = htab_map_free,
1195 .map_get_next_key = htab_map_get_next_key,
1196 .map_lookup_elem = htab_map_lookup_elem,
1197 .map_update_elem = htab_map_update_elem,
1198 .map_delete_elem = htab_map_delete_elem,
1199 .map_gen_lookup = htab_map_gen_lookup,
1200 .map_seq_show_elem = htab_map_seq_show_elem,
1203 const struct bpf_map_ops htab_lru_map_ops = {
1204 .map_alloc_check = htab_map_alloc_check,
1205 .map_alloc = htab_map_alloc,
1206 .map_free = htab_map_free,
1207 .map_get_next_key = htab_map_get_next_key,
1208 .map_lookup_elem = htab_lru_map_lookup_elem,
1209 .map_update_elem = htab_lru_map_update_elem,
1210 .map_delete_elem = htab_lru_map_delete_elem,
1211 .map_gen_lookup = htab_lru_map_gen_lookup,
1212 .map_seq_show_elem = htab_map_seq_show_elem,
1215 /* Called from eBPF program */
1216 static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1218 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1220 if (l)
1221 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1222 else
1223 return NULL;
1226 static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1228 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1230 if (l) {
1231 bpf_lru_node_set_ref(&l->lru_node);
1232 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1235 return NULL;
1238 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1240 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1241 struct htab_elem *l;
1242 void __percpu *pptr;
1243 int ret = -ENOENT;
1244 int cpu, off = 0;
1245 u32 size;
1247 /* per_cpu areas are zero-filled and bpf programs can only
1248 * access 'value_size' of them, so copying rounded areas
1249 * will not leak any kernel data
1251 size = round_up(map->value_size, 8);
1252 rcu_read_lock();
1253 l = __htab_map_lookup_elem(map, key);
1254 if (!l)
1255 goto out;
1256 if (htab_is_lru(htab))
1257 bpf_lru_node_set_ref(&l->lru_node);
1258 pptr = htab_elem_get_ptr(l, map->key_size);
1259 for_each_possible_cpu(cpu) {
1260 bpf_long_memcpy(value + off,
1261 per_cpu_ptr(pptr, cpu), size);
1262 off += size;
1264 ret = 0;
1265 out:
1266 rcu_read_unlock();
1267 return ret;
1270 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1271 u64 map_flags)
1273 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1274 int ret;
1276 rcu_read_lock();
1277 if (htab_is_lru(htab))
1278 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1279 map_flags, true);
1280 else
1281 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1282 true);
1283 rcu_read_unlock();
1285 return ret;
1288 const struct bpf_map_ops htab_percpu_map_ops = {
1289 .map_alloc_check = htab_map_alloc_check,
1290 .map_alloc = htab_map_alloc,
1291 .map_free = htab_map_free,
1292 .map_get_next_key = htab_map_get_next_key,
1293 .map_lookup_elem = htab_percpu_map_lookup_elem,
1294 .map_update_elem = htab_percpu_map_update_elem,
1295 .map_delete_elem = htab_map_delete_elem,
1298 const struct bpf_map_ops htab_lru_percpu_map_ops = {
1299 .map_alloc_check = htab_map_alloc_check,
1300 .map_alloc = htab_map_alloc,
1301 .map_free = htab_map_free,
1302 .map_get_next_key = htab_map_get_next_key,
1303 .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1304 .map_update_elem = htab_lru_percpu_map_update_elem,
1305 .map_delete_elem = htab_lru_map_delete_elem,
1308 static int fd_htab_map_alloc_check(union bpf_attr *attr)
1310 if (attr->value_size != sizeof(u32))
1311 return -EINVAL;
1312 return htab_map_alloc_check(attr);
1315 static void fd_htab_map_free(struct bpf_map *map)
1317 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1318 struct hlist_nulls_node *n;
1319 struct hlist_nulls_head *head;
1320 struct htab_elem *l;
1321 int i;
1323 for (i = 0; i < htab->n_buckets; i++) {
1324 head = select_bucket(htab, i);
1326 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1327 void *ptr = fd_htab_map_get_ptr(map, l);
1329 map->ops->map_fd_put_ptr(ptr);
1333 htab_map_free(map);
1336 /* only called from syscall */
1337 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
1339 void **ptr;
1340 int ret = 0;
1342 if (!map->ops->map_fd_sys_lookup_elem)
1343 return -ENOTSUPP;
1345 rcu_read_lock();
1346 ptr = htab_map_lookup_elem(map, key);
1347 if (ptr)
1348 *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
1349 else
1350 ret = -ENOENT;
1351 rcu_read_unlock();
1353 return ret;
1356 /* only called from syscall */
1357 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1358 void *key, void *value, u64 map_flags)
1360 void *ptr;
1361 int ret;
1362 u32 ufd = *(u32 *)value;
1364 ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
1365 if (IS_ERR(ptr))
1366 return PTR_ERR(ptr);
1368 ret = htab_map_update_elem(map, key, &ptr, map_flags);
1369 if (ret)
1370 map->ops->map_fd_put_ptr(ptr);
1372 return ret;
1375 static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
1377 struct bpf_map *map, *inner_map_meta;
1379 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1380 if (IS_ERR(inner_map_meta))
1381 return inner_map_meta;
1383 map = htab_map_alloc(attr);
1384 if (IS_ERR(map)) {
1385 bpf_map_meta_free(inner_map_meta);
1386 return map;
1389 map->inner_map_meta = inner_map_meta;
1391 return map;
1394 static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
1396 struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
1398 if (!inner_map)
1399 return NULL;
1401 return READ_ONCE(*inner_map);
1404 static u32 htab_of_map_gen_lookup(struct bpf_map *map,
1405 struct bpf_insn *insn_buf)
1407 struct bpf_insn *insn = insn_buf;
1408 const int ret = BPF_REG_0;
1410 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
1411 (void *(*)(struct bpf_map *map, void *key))NULL));
1412 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
1413 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
1414 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
1415 offsetof(struct htab_elem, key) +
1416 round_up(map->key_size, 8));
1417 *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
1419 return insn - insn_buf;
1422 static void htab_of_map_free(struct bpf_map *map)
1424 bpf_map_meta_free(map->inner_map_meta);
1425 fd_htab_map_free(map);
1428 const struct bpf_map_ops htab_of_maps_map_ops = {
1429 .map_alloc_check = fd_htab_map_alloc_check,
1430 .map_alloc = htab_of_map_alloc,
1431 .map_free = htab_of_map_free,
1432 .map_get_next_key = htab_map_get_next_key,
1433 .map_lookup_elem = htab_of_map_lookup_elem,
1434 .map_delete_elem = htab_map_delete_elem,
1435 .map_fd_get_ptr = bpf_map_fd_get_ptr,
1436 .map_fd_put_ptr = bpf_map_fd_put_ptr,
1437 .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
1438 .map_gen_lookup = htab_of_map_gen_lookup,
1439 .map_check_btf = map_check_no_btf,