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_ACCESS_MASK | BPF_F_ZERO_SEED)
29 struct hlist_nulls_head head;
35 struct bucket *buckets;
38 struct pcpu_freelist freelist;
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 */
48 /* each htab element is struct htab_elem + key + value */
51 struct hlist_nulls_node hash_node;
55 struct bpf_htab *htab;
56 struct pcpu_freelist_node fnode;
62 struct bpf_lru_node lru_node;
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,
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)
112 if (!htab_is_percpu(htab))
115 for (i = 0; i < htab->map.max_entries; i++) {
118 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
124 bpf_map_area_free(htab->elems);
127 static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
130 struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
134 l = container_of(node, struct htab_elem, lru_node);
135 memcpy(l->key, key, htab->map.key_size);
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);
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);
162 pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
165 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
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,
179 err = pcpu_freelist_init(&htab->freelist);
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);
189 pcpu_freelist_populate(&htab->freelist,
190 htab->elems + offsetof(struct htab_elem, fnode),
191 htab->elem_size, num_entries);
196 htab_free_elems(htab);
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);
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;
216 pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8,
217 GFP_USER | __GFP_NOWARN);
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;
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 bool zero_seed = (attr->map_flags & BPF_F_ZERO_SEED);
248 int numa_node = bpf_map_attr_numa_node(attr);
250 BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
251 offsetof(struct htab_elem, hash_node.pprev));
252 BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
253 offsetof(struct htab_elem, hash_node.pprev));
255 if (lru && !capable(CAP_SYS_ADMIN))
256 /* LRU implementation is much complicated than other
257 * maps. Hence, limit to CAP_SYS_ADMIN for now.
261 if (zero_seed && !capable(CAP_SYS_ADMIN))
262 /* Guard against local DoS, and discourage production use. */
265 if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK ||
266 !bpf_map_flags_access_ok(attr->map_flags))
269 if (!lru && percpu_lru)
272 if (lru && !prealloc)
275 if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
278 /* check sanity of attributes.
279 * value_size == 0 may be allowed in the future to use map as a set
281 if (attr->max_entries == 0 || attr->key_size == 0 ||
282 attr->value_size == 0)
285 if (attr->key_size > MAX_BPF_STACK)
286 /* eBPF programs initialize keys on stack, so they cannot be
287 * larger than max stack size
291 if (attr->value_size >= KMALLOC_MAX_SIZE -
292 MAX_BPF_STACK - sizeof(struct htab_elem))
293 /* if value_size is bigger, the user space won't be able to
294 * access the elements via bpf syscall. This check also makes
295 * sure that the elem_size doesn't overflow and it's
296 * kmalloc-able later in htab_map_update_elem()
303 static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
305 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
306 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
307 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
308 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
309 /* percpu_lru means each cpu has its own LRU list.
310 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
311 * the map's value itself is percpu. percpu_lru has
312 * nothing to do with the map's value.
314 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
315 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
316 struct bpf_htab *htab;
320 htab = kzalloc(sizeof(*htab), GFP_USER);
322 return ERR_PTR(-ENOMEM);
324 bpf_map_init_from_attr(&htab->map, attr);
327 /* ensure each CPU's lru list has >=1 elements.
328 * since we are at it, make each lru list has the same
329 * number of elements.
331 htab->map.max_entries = roundup(attr->max_entries,
332 num_possible_cpus());
333 if (htab->map.max_entries < attr->max_entries)
334 htab->map.max_entries = rounddown(attr->max_entries,
335 num_possible_cpus());
338 /* hash table size must be power of 2 */
339 htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
341 htab->elem_size = sizeof(struct htab_elem) +
342 round_up(htab->map.key_size, 8);
344 htab->elem_size += sizeof(void *);
346 htab->elem_size += round_up(htab->map.value_size, 8);
349 /* prevent zero size kmalloc and check for u32 overflow */
350 if (htab->n_buckets == 0 ||
351 htab->n_buckets > U32_MAX / sizeof(struct bucket))
354 cost = (u64) htab->n_buckets * sizeof(struct bucket) +
355 (u64) htab->elem_size * htab->map.max_entries;
358 cost += (u64) round_up(htab->map.value_size, 8) *
359 num_possible_cpus() * htab->map.max_entries;
361 cost += (u64) htab->elem_size * num_possible_cpus();
363 /* if map size is larger than memlock limit, reject it */
364 err = bpf_map_charge_init(&htab->map.memory, cost);
369 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
370 sizeof(struct bucket),
371 htab->map.numa_node);
375 if (htab->map.map_flags & BPF_F_ZERO_SEED)
378 htab->hashrnd = get_random_int();
380 for (i = 0; i < htab->n_buckets; i++) {
381 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
382 raw_spin_lock_init(&htab->buckets[i].lock);
386 err = prealloc_init(htab);
390 if (!percpu && !lru) {
391 /* lru itself can remove the least used element, so
392 * there is no need for an extra elem during map_update.
394 err = alloc_extra_elems(htab);
403 prealloc_destroy(htab);
405 bpf_map_area_free(htab->buckets);
407 bpf_map_charge_finish(&htab->map.memory);
413 static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
415 return jhash(key, key_len, hashrnd);
418 static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
420 return &htab->buckets[hash & (htab->n_buckets - 1)];
423 static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
425 return &__select_bucket(htab, hash)->head;
428 /* this lookup function can only be called with bucket lock taken */
429 static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
430 void *key, u32 key_size)
432 struct hlist_nulls_node *n;
435 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
436 if (l->hash == hash && !memcmp(&l->key, key, key_size))
442 /* can be called without bucket lock. it will repeat the loop in
443 * the unlikely event when elements moved from one bucket into another
444 * while link list is being walked
446 static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
448 u32 key_size, u32 n_buckets)
450 struct hlist_nulls_node *n;
454 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
455 if (l->hash == hash && !memcmp(&l->key, key, key_size))
458 if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
464 /* Called from syscall or from eBPF program directly, so
465 * arguments have to match bpf_map_lookup_elem() exactly.
466 * The return value is adjusted by BPF instructions
467 * in htab_map_gen_lookup().
469 static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
471 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
472 struct hlist_nulls_head *head;
476 /* Must be called with rcu_read_lock. */
477 WARN_ON_ONCE(!rcu_read_lock_held());
479 key_size = map->key_size;
481 hash = htab_map_hash(key, key_size, htab->hashrnd);
483 head = select_bucket(htab, hash);
485 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
490 static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
492 struct htab_elem *l = __htab_map_lookup_elem(map, key);
495 return l->key + round_up(map->key_size, 8);
500 /* inline bpf_map_lookup_elem() call.
503 * bpf_map_lookup_elem
504 * map->ops->map_lookup_elem
505 * htab_map_lookup_elem
506 * __htab_map_lookup_elem
509 * __htab_map_lookup_elem
511 static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
513 struct bpf_insn *insn = insn_buf;
514 const int ret = BPF_REG_0;
516 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
517 (void *(*)(struct bpf_map *map, void *key))NULL));
518 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
519 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
520 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
521 offsetof(struct htab_elem, key) +
522 round_up(map->key_size, 8));
523 return insn - insn_buf;
526 static __always_inline void *__htab_lru_map_lookup_elem(struct bpf_map *map,
527 void *key, const bool mark)
529 struct htab_elem *l = __htab_map_lookup_elem(map, key);
533 bpf_lru_node_set_ref(&l->lru_node);
534 return l->key + round_up(map->key_size, 8);
540 static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
542 return __htab_lru_map_lookup_elem(map, key, true);
545 static void *htab_lru_map_lookup_elem_sys(struct bpf_map *map, void *key)
547 return __htab_lru_map_lookup_elem(map, key, false);
550 static u32 htab_lru_map_gen_lookup(struct bpf_map *map,
551 struct bpf_insn *insn_buf)
553 struct bpf_insn *insn = insn_buf;
554 const int ret = BPF_REG_0;
555 const int ref_reg = BPF_REG_1;
557 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
558 (void *(*)(struct bpf_map *map, void *key))NULL));
559 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
560 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
561 *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
562 offsetof(struct htab_elem, lru_node) +
563 offsetof(struct bpf_lru_node, ref));
564 *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
565 *insn++ = BPF_ST_MEM(BPF_B, ret,
566 offsetof(struct htab_elem, lru_node) +
567 offsetof(struct bpf_lru_node, ref),
569 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
570 offsetof(struct htab_elem, key) +
571 round_up(map->key_size, 8));
572 return insn - insn_buf;
575 /* It is called from the bpf_lru_list when the LRU needs to delete
576 * older elements from the htab.
578 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
580 struct bpf_htab *htab = (struct bpf_htab *)arg;
581 struct htab_elem *l = NULL, *tgt_l;
582 struct hlist_nulls_head *head;
583 struct hlist_nulls_node *n;
587 tgt_l = container_of(node, struct htab_elem, lru_node);
588 b = __select_bucket(htab, tgt_l->hash);
591 raw_spin_lock_irqsave(&b->lock, flags);
593 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
595 hlist_nulls_del_rcu(&l->hash_node);
599 raw_spin_unlock_irqrestore(&b->lock, flags);
604 /* Called from syscall */
605 static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
607 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
608 struct hlist_nulls_head *head;
609 struct htab_elem *l, *next_l;
613 WARN_ON_ONCE(!rcu_read_lock_held());
615 key_size = map->key_size;
618 goto find_first_elem;
620 hash = htab_map_hash(key, key_size, htab->hashrnd);
622 head = select_bucket(htab, hash);
625 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
628 goto find_first_elem;
630 /* key was found, get next key in the same bucket */
631 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
632 struct htab_elem, hash_node);
635 /* if next elem in this hash list is non-zero, just return it */
636 memcpy(next_key, next_l->key, key_size);
640 /* no more elements in this hash list, go to the next bucket */
641 i = hash & (htab->n_buckets - 1);
645 /* iterate over buckets */
646 for (; i < htab->n_buckets; i++) {
647 head = select_bucket(htab, i);
649 /* pick first element in the bucket */
650 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
651 struct htab_elem, hash_node);
653 /* if it's not empty, just return it */
654 memcpy(next_key, next_l->key, key_size);
659 /* iterated over all buckets and all elements */
663 static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
665 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
666 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
670 static void htab_elem_free_rcu(struct rcu_head *head)
672 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
673 struct bpf_htab *htab = l->htab;
675 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
676 * we're calling kfree, otherwise deadlock is possible if kprobes
677 * are placed somewhere inside of slub
680 __this_cpu_inc(bpf_prog_active);
681 htab_elem_free(htab, l);
682 __this_cpu_dec(bpf_prog_active);
686 static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
688 struct bpf_map *map = &htab->map;
690 if (map->ops->map_fd_put_ptr) {
691 void *ptr = fd_htab_map_get_ptr(map, l);
693 map->ops->map_fd_put_ptr(ptr);
696 if (htab_is_prealloc(htab)) {
697 __pcpu_freelist_push(&htab->freelist, &l->fnode);
699 atomic_dec(&htab->count);
701 call_rcu(&l->rcu, htab_elem_free_rcu);
705 static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
706 void *value, bool onallcpus)
709 /* copy true value_size bytes */
710 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
712 u32 size = round_up(htab->map.value_size, 8);
715 for_each_possible_cpu(cpu) {
716 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
723 static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
725 return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
729 static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
730 void *value, u32 key_size, u32 hash,
731 bool percpu, bool onallcpus,
732 struct htab_elem *old_elem)
734 u32 size = htab->map.value_size;
735 bool prealloc = htab_is_prealloc(htab);
736 struct htab_elem *l_new, **pl_new;
741 /* if we're updating the existing element,
742 * use per-cpu extra elems to avoid freelist_pop/push
744 pl_new = this_cpu_ptr(htab->extra_elems);
748 struct pcpu_freelist_node *l;
750 l = __pcpu_freelist_pop(&htab->freelist);
752 return ERR_PTR(-E2BIG);
753 l_new = container_of(l, struct htab_elem, fnode);
756 if (atomic_inc_return(&htab->count) > htab->map.max_entries)
758 /* when map is full and update() is replacing
759 * old element, it's ok to allocate, since
760 * old element will be freed immediately.
761 * Otherwise return an error
763 l_new = ERR_PTR(-E2BIG);
766 l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
767 htab->map.numa_node);
769 l_new = ERR_PTR(-ENOMEM);
772 check_and_init_map_lock(&htab->map,
773 l_new->key + round_up(key_size, 8));
776 memcpy(l_new->key, key, key_size);
778 size = round_up(size, 8);
780 pptr = htab_elem_get_ptr(l_new, key_size);
782 /* alloc_percpu zero-fills */
783 pptr = __alloc_percpu_gfp(size, 8,
784 GFP_ATOMIC | __GFP_NOWARN);
787 l_new = ERR_PTR(-ENOMEM);
792 pcpu_copy_value(htab, pptr, value, onallcpus);
795 htab_elem_set_ptr(l_new, key_size, pptr);
796 } else if (fd_htab_map_needs_adjust(htab)) {
797 size = round_up(size, 8);
798 memcpy(l_new->key + round_up(key_size, 8), value, size);
800 copy_map_value(&htab->map,
801 l_new->key + round_up(key_size, 8),
808 atomic_dec(&htab->count);
812 static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
815 if (l_old && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
816 /* elem already exists */
819 if (!l_old && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
820 /* elem doesn't exist, cannot update it */
826 /* Called from syscall or from eBPF program */
827 static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
830 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
831 struct htab_elem *l_new = NULL, *l_old;
832 struct hlist_nulls_head *head;
838 if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST))
842 WARN_ON_ONCE(!rcu_read_lock_held());
844 key_size = map->key_size;
846 hash = htab_map_hash(key, key_size, htab->hashrnd);
848 b = __select_bucket(htab, hash);
851 if (unlikely(map_flags & BPF_F_LOCK)) {
852 if (unlikely(!map_value_has_spin_lock(map)))
854 /* find an element without taking the bucket lock */
855 l_old = lookup_nulls_elem_raw(head, hash, key, key_size,
857 ret = check_flags(htab, l_old, map_flags);
861 /* grab the element lock and update value in place */
862 copy_map_value_locked(map,
863 l_old->key + round_up(key_size, 8),
867 /* fall through, grab the bucket lock and lookup again.
868 * 99.9% chance that the element won't be found,
869 * but second lookup under lock has to be done.
873 /* bpf_map_update_elem() can be called in_irq() */
874 raw_spin_lock_irqsave(&b->lock, flags);
876 l_old = lookup_elem_raw(head, hash, key, key_size);
878 ret = check_flags(htab, l_old, map_flags);
882 if (unlikely(l_old && (map_flags & BPF_F_LOCK))) {
883 /* first lookup without the bucket lock didn't find the element,
884 * but second lookup with the bucket lock found it.
885 * This case is highly unlikely, but has to be dealt with:
886 * grab the element lock in addition to the bucket lock
887 * and update element in place
889 copy_map_value_locked(map,
890 l_old->key + round_up(key_size, 8),
896 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
899 /* all pre-allocated elements are in use or memory exhausted */
900 ret = PTR_ERR(l_new);
904 /* add new element to the head of the list, so that
905 * concurrent search will find it before old elem
907 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
909 hlist_nulls_del_rcu(&l_old->hash_node);
910 if (!htab_is_prealloc(htab))
911 free_htab_elem(htab, l_old);
915 raw_spin_unlock_irqrestore(&b->lock, flags);
919 static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
922 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
923 struct htab_elem *l_new, *l_old = NULL;
924 struct hlist_nulls_head *head;
930 if (unlikely(map_flags > BPF_EXIST))
934 WARN_ON_ONCE(!rcu_read_lock_held());
936 key_size = map->key_size;
938 hash = htab_map_hash(key, key_size, htab->hashrnd);
940 b = __select_bucket(htab, hash);
943 /* For LRU, we need to alloc before taking bucket's
944 * spinlock because getting free nodes from LRU may need
945 * to remove older elements from htab and this removal
946 * operation will need a bucket lock.
948 l_new = prealloc_lru_pop(htab, key, hash);
951 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
953 /* bpf_map_update_elem() can be called in_irq() */
954 raw_spin_lock_irqsave(&b->lock, flags);
956 l_old = lookup_elem_raw(head, hash, key, key_size);
958 ret = check_flags(htab, l_old, map_flags);
962 /* add new element to the head of the list, so that
963 * concurrent search will find it before old elem
965 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
967 bpf_lru_node_set_ref(&l_new->lru_node);
968 hlist_nulls_del_rcu(&l_old->hash_node);
973 raw_spin_unlock_irqrestore(&b->lock, flags);
976 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
978 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
983 static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
984 void *value, u64 map_flags,
987 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
988 struct htab_elem *l_new = NULL, *l_old;
989 struct hlist_nulls_head *head;
995 if (unlikely(map_flags > BPF_EXIST))
999 WARN_ON_ONCE(!rcu_read_lock_held());
1001 key_size = map->key_size;
1003 hash = htab_map_hash(key, key_size, htab->hashrnd);
1005 b = __select_bucket(htab, hash);
1008 /* bpf_map_update_elem() can be called in_irq() */
1009 raw_spin_lock_irqsave(&b->lock, flags);
1011 l_old = lookup_elem_raw(head, hash, key, key_size);
1013 ret = check_flags(htab, l_old, map_flags);
1018 /* per-cpu hash map can update value in-place */
1019 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1022 l_new = alloc_htab_elem(htab, key, value, key_size,
1023 hash, true, onallcpus, NULL);
1024 if (IS_ERR(l_new)) {
1025 ret = PTR_ERR(l_new);
1028 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1032 raw_spin_unlock_irqrestore(&b->lock, flags);
1036 static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1037 void *value, u64 map_flags,
1040 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1041 struct htab_elem *l_new = NULL, *l_old;
1042 struct hlist_nulls_head *head;
1043 unsigned long flags;
1048 if (unlikely(map_flags > BPF_EXIST))
1052 WARN_ON_ONCE(!rcu_read_lock_held());
1054 key_size = map->key_size;
1056 hash = htab_map_hash(key, key_size, htab->hashrnd);
1058 b = __select_bucket(htab, hash);
1061 /* For LRU, we need to alloc before taking bucket's
1062 * spinlock because LRU's elem alloc may need
1063 * to remove older elem from htab and this removal
1064 * operation will need a bucket lock.
1066 if (map_flags != BPF_EXIST) {
1067 l_new = prealloc_lru_pop(htab, key, hash);
1072 /* bpf_map_update_elem() can be called in_irq() */
1073 raw_spin_lock_irqsave(&b->lock, flags);
1075 l_old = lookup_elem_raw(head, hash, key, key_size);
1077 ret = check_flags(htab, l_old, map_flags);
1082 bpf_lru_node_set_ref(&l_old->lru_node);
1084 /* per-cpu hash map can update value in-place */
1085 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1088 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
1090 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1095 raw_spin_unlock_irqrestore(&b->lock, flags);
1097 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1101 static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1102 void *value, u64 map_flags)
1104 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
1107 static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1108 void *value, u64 map_flags)
1110 return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
1114 /* Called from syscall or from eBPF program */
1115 static int htab_map_delete_elem(struct bpf_map *map, void *key)
1117 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1118 struct hlist_nulls_head *head;
1120 struct htab_elem *l;
1121 unsigned long flags;
1125 WARN_ON_ONCE(!rcu_read_lock_held());
1127 key_size = map->key_size;
1129 hash = htab_map_hash(key, key_size, htab->hashrnd);
1130 b = __select_bucket(htab, hash);
1133 raw_spin_lock_irqsave(&b->lock, flags);
1135 l = lookup_elem_raw(head, hash, key, key_size);
1138 hlist_nulls_del_rcu(&l->hash_node);
1139 free_htab_elem(htab, l);
1143 raw_spin_unlock_irqrestore(&b->lock, flags);
1147 static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1149 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1150 struct hlist_nulls_head *head;
1152 struct htab_elem *l;
1153 unsigned long flags;
1157 WARN_ON_ONCE(!rcu_read_lock_held());
1159 key_size = map->key_size;
1161 hash = htab_map_hash(key, key_size, htab->hashrnd);
1162 b = __select_bucket(htab, hash);
1165 raw_spin_lock_irqsave(&b->lock, flags);
1167 l = lookup_elem_raw(head, hash, key, key_size);
1170 hlist_nulls_del_rcu(&l->hash_node);
1174 raw_spin_unlock_irqrestore(&b->lock, flags);
1176 bpf_lru_push_free(&htab->lru, &l->lru_node);
1180 static void delete_all_elements(struct bpf_htab *htab)
1184 for (i = 0; i < htab->n_buckets; i++) {
1185 struct hlist_nulls_head *head = select_bucket(htab, i);
1186 struct hlist_nulls_node *n;
1187 struct htab_elem *l;
1189 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1190 hlist_nulls_del_rcu(&l->hash_node);
1191 htab_elem_free(htab, l);
1196 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1197 static void htab_map_free(struct bpf_map *map)
1199 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1201 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1202 * so the programs (can be more than one that used this map) were
1203 * disconnected from events. Wait for outstanding critical sections in
1204 * these programs to complete
1208 /* some of free_htab_elem() callbacks for elements of this map may
1209 * not have executed. Wait for them.
1212 if (!htab_is_prealloc(htab))
1213 delete_all_elements(htab);
1215 prealloc_destroy(htab);
1217 free_percpu(htab->extra_elems);
1218 bpf_map_area_free(htab->buckets);
1222 static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1229 value = htab_map_lookup_elem(map, key);
1235 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1237 btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1243 const struct bpf_map_ops htab_map_ops = {
1244 .map_alloc_check = htab_map_alloc_check,
1245 .map_alloc = htab_map_alloc,
1246 .map_free = htab_map_free,
1247 .map_get_next_key = htab_map_get_next_key,
1248 .map_lookup_elem = htab_map_lookup_elem,
1249 .map_update_elem = htab_map_update_elem,
1250 .map_delete_elem = htab_map_delete_elem,
1251 .map_gen_lookup = htab_map_gen_lookup,
1252 .map_seq_show_elem = htab_map_seq_show_elem,
1255 const struct bpf_map_ops htab_lru_map_ops = {
1256 .map_alloc_check = htab_map_alloc_check,
1257 .map_alloc = htab_map_alloc,
1258 .map_free = htab_map_free,
1259 .map_get_next_key = htab_map_get_next_key,
1260 .map_lookup_elem = htab_lru_map_lookup_elem,
1261 .map_lookup_elem_sys_only = htab_lru_map_lookup_elem_sys,
1262 .map_update_elem = htab_lru_map_update_elem,
1263 .map_delete_elem = htab_lru_map_delete_elem,
1264 .map_gen_lookup = htab_lru_map_gen_lookup,
1265 .map_seq_show_elem = htab_map_seq_show_elem,
1268 /* Called from eBPF program */
1269 static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1271 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1274 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1279 static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1281 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1284 bpf_lru_node_set_ref(&l->lru_node);
1285 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1291 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1293 struct htab_elem *l;
1294 void __percpu *pptr;
1299 /* per_cpu areas are zero-filled and bpf programs can only
1300 * access 'value_size' of them, so copying rounded areas
1301 * will not leak any kernel data
1303 size = round_up(map->value_size, 8);
1305 l = __htab_map_lookup_elem(map, key);
1308 /* We do not mark LRU map element here in order to not mess up
1309 * eviction heuristics when user space does a map walk.
1311 pptr = htab_elem_get_ptr(l, map->key_size);
1312 for_each_possible_cpu(cpu) {
1313 bpf_long_memcpy(value + off,
1314 per_cpu_ptr(pptr, cpu), size);
1323 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1326 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1330 if (htab_is_lru(htab))
1331 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1334 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1341 static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
1344 struct htab_elem *l;
1345 void __percpu *pptr;
1350 l = __htab_map_lookup_elem(map, key);
1356 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1357 seq_puts(m, ": {\n");
1358 pptr = htab_elem_get_ptr(l, map->key_size);
1359 for_each_possible_cpu(cpu) {
1360 seq_printf(m, "\tcpu%d: ", cpu);
1361 btf_type_seq_show(map->btf, map->btf_value_type_id,
1362 per_cpu_ptr(pptr, cpu), m);
1370 const struct bpf_map_ops htab_percpu_map_ops = {
1371 .map_alloc_check = htab_map_alloc_check,
1372 .map_alloc = htab_map_alloc,
1373 .map_free = htab_map_free,
1374 .map_get_next_key = htab_map_get_next_key,
1375 .map_lookup_elem = htab_percpu_map_lookup_elem,
1376 .map_update_elem = htab_percpu_map_update_elem,
1377 .map_delete_elem = htab_map_delete_elem,
1378 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
1381 const struct bpf_map_ops htab_lru_percpu_map_ops = {
1382 .map_alloc_check = htab_map_alloc_check,
1383 .map_alloc = htab_map_alloc,
1384 .map_free = htab_map_free,
1385 .map_get_next_key = htab_map_get_next_key,
1386 .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1387 .map_update_elem = htab_lru_percpu_map_update_elem,
1388 .map_delete_elem = htab_lru_map_delete_elem,
1389 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
1392 static int fd_htab_map_alloc_check(union bpf_attr *attr)
1394 if (attr->value_size != sizeof(u32))
1396 return htab_map_alloc_check(attr);
1399 static void fd_htab_map_free(struct bpf_map *map)
1401 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1402 struct hlist_nulls_node *n;
1403 struct hlist_nulls_head *head;
1404 struct htab_elem *l;
1407 for (i = 0; i < htab->n_buckets; i++) {
1408 head = select_bucket(htab, i);
1410 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1411 void *ptr = fd_htab_map_get_ptr(map, l);
1413 map->ops->map_fd_put_ptr(ptr);
1420 /* only called from syscall */
1421 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
1426 if (!map->ops->map_fd_sys_lookup_elem)
1430 ptr = htab_map_lookup_elem(map, key);
1432 *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
1440 /* only called from syscall */
1441 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1442 void *key, void *value, u64 map_flags)
1446 u32 ufd = *(u32 *)value;
1448 ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
1450 return PTR_ERR(ptr);
1452 ret = htab_map_update_elem(map, key, &ptr, map_flags);
1454 map->ops->map_fd_put_ptr(ptr);
1459 static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
1461 struct bpf_map *map, *inner_map_meta;
1463 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1464 if (IS_ERR(inner_map_meta))
1465 return inner_map_meta;
1467 map = htab_map_alloc(attr);
1469 bpf_map_meta_free(inner_map_meta);
1473 map->inner_map_meta = inner_map_meta;
1478 static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
1480 struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
1485 return READ_ONCE(*inner_map);
1488 static u32 htab_of_map_gen_lookup(struct bpf_map *map,
1489 struct bpf_insn *insn_buf)
1491 struct bpf_insn *insn = insn_buf;
1492 const int ret = BPF_REG_0;
1494 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
1495 (void *(*)(struct bpf_map *map, void *key))NULL));
1496 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
1497 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
1498 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
1499 offsetof(struct htab_elem, key) +
1500 round_up(map->key_size, 8));
1501 *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
1503 return insn - insn_buf;
1506 static void htab_of_map_free(struct bpf_map *map)
1508 bpf_map_meta_free(map->inner_map_meta);
1509 fd_htab_map_free(map);
1512 const struct bpf_map_ops htab_of_maps_map_ops = {
1513 .map_alloc_check = fd_htab_map_alloc_check,
1514 .map_alloc = htab_of_map_alloc,
1515 .map_free = htab_of_map_free,
1516 .map_get_next_key = htab_map_get_next_key,
1517 .map_lookup_elem = htab_of_map_lookup_elem,
1518 .map_delete_elem = htab_map_delete_elem,
1519 .map_fd_get_ptr = bpf_map_fd_get_ptr,
1520 .map_fd_put_ptr = bpf_map_fd_put_ptr,
1521 .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
1522 .map_gen_lookup = htab_of_map_gen_lookup,
1523 .map_check_btf = map_check_no_btf,