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 (cost >= U32_MAX - PAGE_SIZE)
364 /* make sure page count doesn't overflow */
367 htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
369 /* if map size is larger than memlock limit, reject it early */
370 err = bpf_map_precharge_memlock(htab->map.pages);
375 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
376 sizeof(struct bucket),
377 htab->map.numa_node);
381 if (htab->map.map_flags & BPF_F_ZERO_SEED)
384 htab->hashrnd = get_random_int();
386 for (i = 0; i < htab->n_buckets; i++) {
387 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
388 raw_spin_lock_init(&htab->buckets[i].lock);
392 err = prealloc_init(htab);
396 if (!percpu && !lru) {
397 /* lru itself can remove the least used element, so
398 * there is no need for an extra elem during map_update.
400 err = alloc_extra_elems(htab);
409 prealloc_destroy(htab);
411 bpf_map_area_free(htab->buckets);
417 static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
419 return jhash(key, key_len, hashrnd);
422 static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
424 return &htab->buckets[hash & (htab->n_buckets - 1)];
427 static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
429 return &__select_bucket(htab, hash)->head;
432 /* this lookup function can only be called with bucket lock taken */
433 static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
434 void *key, u32 key_size)
436 struct hlist_nulls_node *n;
439 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
440 if (l->hash == hash && !memcmp(&l->key, key, key_size))
446 /* can be called without bucket lock. it will repeat the loop in
447 * the unlikely event when elements moved from one bucket into another
448 * while link list is being walked
450 static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
452 u32 key_size, u32 n_buckets)
454 struct hlist_nulls_node *n;
458 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
459 if (l->hash == hash && !memcmp(&l->key, key, key_size))
462 if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
468 /* Called from syscall or from eBPF program directly, so
469 * arguments have to match bpf_map_lookup_elem() exactly.
470 * The return value is adjusted by BPF instructions
471 * in htab_map_gen_lookup().
473 static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
475 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
476 struct hlist_nulls_head *head;
480 /* Must be called with rcu_read_lock. */
481 WARN_ON_ONCE(!rcu_read_lock_held());
483 key_size = map->key_size;
485 hash = htab_map_hash(key, key_size, htab->hashrnd);
487 head = select_bucket(htab, hash);
489 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
494 static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
496 struct htab_elem *l = __htab_map_lookup_elem(map, key);
499 return l->key + round_up(map->key_size, 8);
504 /* inline bpf_map_lookup_elem() call.
507 * bpf_map_lookup_elem
508 * map->ops->map_lookup_elem
509 * htab_map_lookup_elem
510 * __htab_map_lookup_elem
513 * __htab_map_lookup_elem
515 static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
517 struct bpf_insn *insn = insn_buf;
518 const int ret = BPF_REG_0;
520 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
521 (void *(*)(struct bpf_map *map, void *key))NULL));
522 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
523 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
524 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
525 offsetof(struct htab_elem, key) +
526 round_up(map->key_size, 8));
527 return insn - insn_buf;
530 static __always_inline void *__htab_lru_map_lookup_elem(struct bpf_map *map,
531 void *key, const bool mark)
533 struct htab_elem *l = __htab_map_lookup_elem(map, key);
537 bpf_lru_node_set_ref(&l->lru_node);
538 return l->key + round_up(map->key_size, 8);
544 static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
546 return __htab_lru_map_lookup_elem(map, key, true);
549 static void *htab_lru_map_lookup_elem_sys(struct bpf_map *map, void *key)
551 return __htab_lru_map_lookup_elem(map, key, false);
554 static u32 htab_lru_map_gen_lookup(struct bpf_map *map,
555 struct bpf_insn *insn_buf)
557 struct bpf_insn *insn = insn_buf;
558 const int ret = BPF_REG_0;
559 const int ref_reg = BPF_REG_1;
561 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
562 (void *(*)(struct bpf_map *map, void *key))NULL));
563 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
564 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
565 *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
566 offsetof(struct htab_elem, lru_node) +
567 offsetof(struct bpf_lru_node, ref));
568 *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
569 *insn++ = BPF_ST_MEM(BPF_B, ret,
570 offsetof(struct htab_elem, lru_node) +
571 offsetof(struct bpf_lru_node, ref),
573 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
574 offsetof(struct htab_elem, key) +
575 round_up(map->key_size, 8));
576 return insn - insn_buf;
579 /* It is called from the bpf_lru_list when the LRU needs to delete
580 * older elements from the htab.
582 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
584 struct bpf_htab *htab = (struct bpf_htab *)arg;
585 struct htab_elem *l = NULL, *tgt_l;
586 struct hlist_nulls_head *head;
587 struct hlist_nulls_node *n;
591 tgt_l = container_of(node, struct htab_elem, lru_node);
592 b = __select_bucket(htab, tgt_l->hash);
595 raw_spin_lock_irqsave(&b->lock, flags);
597 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
599 hlist_nulls_del_rcu(&l->hash_node);
603 raw_spin_unlock_irqrestore(&b->lock, flags);
608 /* Called from syscall */
609 static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
611 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
612 struct hlist_nulls_head *head;
613 struct htab_elem *l, *next_l;
617 WARN_ON_ONCE(!rcu_read_lock_held());
619 key_size = map->key_size;
622 goto find_first_elem;
624 hash = htab_map_hash(key, key_size, htab->hashrnd);
626 head = select_bucket(htab, hash);
629 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
632 goto find_first_elem;
634 /* key was found, get next key in the same bucket */
635 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
636 struct htab_elem, hash_node);
639 /* if next elem in this hash list is non-zero, just return it */
640 memcpy(next_key, next_l->key, key_size);
644 /* no more elements in this hash list, go to the next bucket */
645 i = hash & (htab->n_buckets - 1);
649 /* iterate over buckets */
650 for (; i < htab->n_buckets; i++) {
651 head = select_bucket(htab, i);
653 /* pick first element in the bucket */
654 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
655 struct htab_elem, hash_node);
657 /* if it's not empty, just return it */
658 memcpy(next_key, next_l->key, key_size);
663 /* iterated over all buckets and all elements */
667 static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
669 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
670 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
674 static void htab_elem_free_rcu(struct rcu_head *head)
676 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
677 struct bpf_htab *htab = l->htab;
679 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
680 * we're calling kfree, otherwise deadlock is possible if kprobes
681 * are placed somewhere inside of slub
684 __this_cpu_inc(bpf_prog_active);
685 htab_elem_free(htab, l);
686 __this_cpu_dec(bpf_prog_active);
690 static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
692 struct bpf_map *map = &htab->map;
694 if (map->ops->map_fd_put_ptr) {
695 void *ptr = fd_htab_map_get_ptr(map, l);
697 map->ops->map_fd_put_ptr(ptr);
700 if (htab_is_prealloc(htab)) {
701 __pcpu_freelist_push(&htab->freelist, &l->fnode);
703 atomic_dec(&htab->count);
705 call_rcu(&l->rcu, htab_elem_free_rcu);
709 static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
710 void *value, bool onallcpus)
713 /* copy true value_size bytes */
714 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
716 u32 size = round_up(htab->map.value_size, 8);
719 for_each_possible_cpu(cpu) {
720 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
727 static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
729 return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
733 static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
734 void *value, u32 key_size, u32 hash,
735 bool percpu, bool onallcpus,
736 struct htab_elem *old_elem)
738 u32 size = htab->map.value_size;
739 bool prealloc = htab_is_prealloc(htab);
740 struct htab_elem *l_new, **pl_new;
745 /* if we're updating the existing element,
746 * use per-cpu extra elems to avoid freelist_pop/push
748 pl_new = this_cpu_ptr(htab->extra_elems);
752 struct pcpu_freelist_node *l;
754 l = __pcpu_freelist_pop(&htab->freelist);
756 return ERR_PTR(-E2BIG);
757 l_new = container_of(l, struct htab_elem, fnode);
760 if (atomic_inc_return(&htab->count) > htab->map.max_entries)
762 /* when map is full and update() is replacing
763 * old element, it's ok to allocate, since
764 * old element will be freed immediately.
765 * Otherwise return an error
767 l_new = ERR_PTR(-E2BIG);
770 l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
771 htab->map.numa_node);
773 l_new = ERR_PTR(-ENOMEM);
776 check_and_init_map_lock(&htab->map,
777 l_new->key + round_up(key_size, 8));
780 memcpy(l_new->key, key, key_size);
782 size = round_up(size, 8);
784 pptr = htab_elem_get_ptr(l_new, key_size);
786 /* alloc_percpu zero-fills */
787 pptr = __alloc_percpu_gfp(size, 8,
788 GFP_ATOMIC | __GFP_NOWARN);
791 l_new = ERR_PTR(-ENOMEM);
796 pcpu_copy_value(htab, pptr, value, onallcpus);
799 htab_elem_set_ptr(l_new, key_size, pptr);
800 } else if (fd_htab_map_needs_adjust(htab)) {
801 size = round_up(size, 8);
802 memcpy(l_new->key + round_up(key_size, 8), value, size);
804 copy_map_value(&htab->map,
805 l_new->key + round_up(key_size, 8),
812 atomic_dec(&htab->count);
816 static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
819 if (l_old && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
820 /* elem already exists */
823 if (!l_old && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
824 /* elem doesn't exist, cannot update it */
830 /* Called from syscall or from eBPF program */
831 static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
834 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
835 struct htab_elem *l_new = NULL, *l_old;
836 struct hlist_nulls_head *head;
842 if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST))
846 WARN_ON_ONCE(!rcu_read_lock_held());
848 key_size = map->key_size;
850 hash = htab_map_hash(key, key_size, htab->hashrnd);
852 b = __select_bucket(htab, hash);
855 if (unlikely(map_flags & BPF_F_LOCK)) {
856 if (unlikely(!map_value_has_spin_lock(map)))
858 /* find an element without taking the bucket lock */
859 l_old = lookup_nulls_elem_raw(head, hash, key, key_size,
861 ret = check_flags(htab, l_old, map_flags);
865 /* grab the element lock and update value in place */
866 copy_map_value_locked(map,
867 l_old->key + round_up(key_size, 8),
871 /* fall through, grab the bucket lock and lookup again.
872 * 99.9% chance that the element won't be found,
873 * but second lookup under lock has to be done.
877 /* bpf_map_update_elem() can be called in_irq() */
878 raw_spin_lock_irqsave(&b->lock, flags);
880 l_old = lookup_elem_raw(head, hash, key, key_size);
882 ret = check_flags(htab, l_old, map_flags);
886 if (unlikely(l_old && (map_flags & BPF_F_LOCK))) {
887 /* first lookup without the bucket lock didn't find the element,
888 * but second lookup with the bucket lock found it.
889 * This case is highly unlikely, but has to be dealt with:
890 * grab the element lock in addition to the bucket lock
891 * and update element in place
893 copy_map_value_locked(map,
894 l_old->key + round_up(key_size, 8),
900 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
903 /* all pre-allocated elements are in use or memory exhausted */
904 ret = PTR_ERR(l_new);
908 /* add new element to the head of the list, so that
909 * concurrent search will find it before old elem
911 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
913 hlist_nulls_del_rcu(&l_old->hash_node);
914 if (!htab_is_prealloc(htab))
915 free_htab_elem(htab, l_old);
919 raw_spin_unlock_irqrestore(&b->lock, flags);
923 static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
926 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
927 struct htab_elem *l_new, *l_old = NULL;
928 struct hlist_nulls_head *head;
934 if (unlikely(map_flags > BPF_EXIST))
938 WARN_ON_ONCE(!rcu_read_lock_held());
940 key_size = map->key_size;
942 hash = htab_map_hash(key, key_size, htab->hashrnd);
944 b = __select_bucket(htab, hash);
947 /* For LRU, we need to alloc before taking bucket's
948 * spinlock because getting free nodes from LRU may need
949 * to remove older elements from htab and this removal
950 * operation will need a bucket lock.
952 l_new = prealloc_lru_pop(htab, key, hash);
955 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
957 /* bpf_map_update_elem() can be called in_irq() */
958 raw_spin_lock_irqsave(&b->lock, flags);
960 l_old = lookup_elem_raw(head, hash, key, key_size);
962 ret = check_flags(htab, l_old, map_flags);
966 /* add new element to the head of the list, so that
967 * concurrent search will find it before old elem
969 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
971 bpf_lru_node_set_ref(&l_new->lru_node);
972 hlist_nulls_del_rcu(&l_old->hash_node);
977 raw_spin_unlock_irqrestore(&b->lock, flags);
980 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
982 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
987 static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
988 void *value, u64 map_flags,
991 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
992 struct htab_elem *l_new = NULL, *l_old;
993 struct hlist_nulls_head *head;
999 if (unlikely(map_flags > BPF_EXIST))
1003 WARN_ON_ONCE(!rcu_read_lock_held());
1005 key_size = map->key_size;
1007 hash = htab_map_hash(key, key_size, htab->hashrnd);
1009 b = __select_bucket(htab, hash);
1012 /* bpf_map_update_elem() can be called in_irq() */
1013 raw_spin_lock_irqsave(&b->lock, flags);
1015 l_old = lookup_elem_raw(head, hash, key, key_size);
1017 ret = check_flags(htab, l_old, map_flags);
1022 /* per-cpu hash map can update value in-place */
1023 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1026 l_new = alloc_htab_elem(htab, key, value, key_size,
1027 hash, true, onallcpus, NULL);
1028 if (IS_ERR(l_new)) {
1029 ret = PTR_ERR(l_new);
1032 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1036 raw_spin_unlock_irqrestore(&b->lock, flags);
1040 static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1041 void *value, u64 map_flags,
1044 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1045 struct htab_elem *l_new = NULL, *l_old;
1046 struct hlist_nulls_head *head;
1047 unsigned long flags;
1052 if (unlikely(map_flags > BPF_EXIST))
1056 WARN_ON_ONCE(!rcu_read_lock_held());
1058 key_size = map->key_size;
1060 hash = htab_map_hash(key, key_size, htab->hashrnd);
1062 b = __select_bucket(htab, hash);
1065 /* For LRU, we need to alloc before taking bucket's
1066 * spinlock because LRU's elem alloc may need
1067 * to remove older elem from htab and this removal
1068 * operation will need a bucket lock.
1070 if (map_flags != BPF_EXIST) {
1071 l_new = prealloc_lru_pop(htab, key, hash);
1076 /* bpf_map_update_elem() can be called in_irq() */
1077 raw_spin_lock_irqsave(&b->lock, flags);
1079 l_old = lookup_elem_raw(head, hash, key, key_size);
1081 ret = check_flags(htab, l_old, map_flags);
1086 bpf_lru_node_set_ref(&l_old->lru_node);
1088 /* per-cpu hash map can update value in-place */
1089 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1092 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
1094 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1099 raw_spin_unlock_irqrestore(&b->lock, flags);
1101 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1105 static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1106 void *value, u64 map_flags)
1108 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
1111 static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1112 void *value, u64 map_flags)
1114 return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
1118 /* Called from syscall or from eBPF program */
1119 static int htab_map_delete_elem(struct bpf_map *map, void *key)
1121 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1122 struct hlist_nulls_head *head;
1124 struct htab_elem *l;
1125 unsigned long flags;
1129 WARN_ON_ONCE(!rcu_read_lock_held());
1131 key_size = map->key_size;
1133 hash = htab_map_hash(key, key_size, htab->hashrnd);
1134 b = __select_bucket(htab, hash);
1137 raw_spin_lock_irqsave(&b->lock, flags);
1139 l = lookup_elem_raw(head, hash, key, key_size);
1142 hlist_nulls_del_rcu(&l->hash_node);
1143 free_htab_elem(htab, l);
1147 raw_spin_unlock_irqrestore(&b->lock, flags);
1151 static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1153 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1154 struct hlist_nulls_head *head;
1156 struct htab_elem *l;
1157 unsigned long flags;
1161 WARN_ON_ONCE(!rcu_read_lock_held());
1163 key_size = map->key_size;
1165 hash = htab_map_hash(key, key_size, htab->hashrnd);
1166 b = __select_bucket(htab, hash);
1169 raw_spin_lock_irqsave(&b->lock, flags);
1171 l = lookup_elem_raw(head, hash, key, key_size);
1174 hlist_nulls_del_rcu(&l->hash_node);
1178 raw_spin_unlock_irqrestore(&b->lock, flags);
1180 bpf_lru_push_free(&htab->lru, &l->lru_node);
1184 static void delete_all_elements(struct bpf_htab *htab)
1188 for (i = 0; i < htab->n_buckets; i++) {
1189 struct hlist_nulls_head *head = select_bucket(htab, i);
1190 struct hlist_nulls_node *n;
1191 struct htab_elem *l;
1193 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1194 hlist_nulls_del_rcu(&l->hash_node);
1195 htab_elem_free(htab, l);
1200 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1201 static void htab_map_free(struct bpf_map *map)
1203 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1205 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1206 * so the programs (can be more than one that used this map) were
1207 * disconnected from events. Wait for outstanding critical sections in
1208 * these programs to complete
1212 /* some of free_htab_elem() callbacks for elements of this map may
1213 * not have executed. Wait for them.
1216 if (!htab_is_prealloc(htab))
1217 delete_all_elements(htab);
1219 prealloc_destroy(htab);
1221 free_percpu(htab->extra_elems);
1222 bpf_map_area_free(htab->buckets);
1226 static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1233 value = htab_map_lookup_elem(map, key);
1239 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1241 btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1247 const struct bpf_map_ops htab_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_map_lookup_elem,
1253 .map_update_elem = htab_map_update_elem,
1254 .map_delete_elem = htab_map_delete_elem,
1255 .map_gen_lookup = htab_map_gen_lookup,
1256 .map_seq_show_elem = htab_map_seq_show_elem,
1259 const struct bpf_map_ops htab_lru_map_ops = {
1260 .map_alloc_check = htab_map_alloc_check,
1261 .map_alloc = htab_map_alloc,
1262 .map_free = htab_map_free,
1263 .map_get_next_key = htab_map_get_next_key,
1264 .map_lookup_elem = htab_lru_map_lookup_elem,
1265 .map_lookup_elem_sys_only = htab_lru_map_lookup_elem_sys,
1266 .map_update_elem = htab_lru_map_update_elem,
1267 .map_delete_elem = htab_lru_map_delete_elem,
1268 .map_gen_lookup = htab_lru_map_gen_lookup,
1269 .map_seq_show_elem = htab_map_seq_show_elem,
1272 /* Called from eBPF program */
1273 static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1275 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1278 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1283 static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1285 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1288 bpf_lru_node_set_ref(&l->lru_node);
1289 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1295 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1297 struct htab_elem *l;
1298 void __percpu *pptr;
1303 /* per_cpu areas are zero-filled and bpf programs can only
1304 * access 'value_size' of them, so copying rounded areas
1305 * will not leak any kernel data
1307 size = round_up(map->value_size, 8);
1309 l = __htab_map_lookup_elem(map, key);
1312 /* We do not mark LRU map element here in order to not mess up
1313 * eviction heuristics when user space does a map walk.
1315 pptr = htab_elem_get_ptr(l, map->key_size);
1316 for_each_possible_cpu(cpu) {
1317 bpf_long_memcpy(value + off,
1318 per_cpu_ptr(pptr, cpu), size);
1327 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1330 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1334 if (htab_is_lru(htab))
1335 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1338 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1345 static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
1348 struct htab_elem *l;
1349 void __percpu *pptr;
1354 l = __htab_map_lookup_elem(map, key);
1360 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1361 seq_puts(m, ": {\n");
1362 pptr = htab_elem_get_ptr(l, map->key_size);
1363 for_each_possible_cpu(cpu) {
1364 seq_printf(m, "\tcpu%d: ", cpu);
1365 btf_type_seq_show(map->btf, map->btf_value_type_id,
1366 per_cpu_ptr(pptr, cpu), m);
1374 const struct bpf_map_ops htab_percpu_map_ops = {
1375 .map_alloc_check = htab_map_alloc_check,
1376 .map_alloc = htab_map_alloc,
1377 .map_free = htab_map_free,
1378 .map_get_next_key = htab_map_get_next_key,
1379 .map_lookup_elem = htab_percpu_map_lookup_elem,
1380 .map_update_elem = htab_percpu_map_update_elem,
1381 .map_delete_elem = htab_map_delete_elem,
1382 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
1385 const struct bpf_map_ops htab_lru_percpu_map_ops = {
1386 .map_alloc_check = htab_map_alloc_check,
1387 .map_alloc = htab_map_alloc,
1388 .map_free = htab_map_free,
1389 .map_get_next_key = htab_map_get_next_key,
1390 .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1391 .map_update_elem = htab_lru_percpu_map_update_elem,
1392 .map_delete_elem = htab_lru_map_delete_elem,
1393 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
1396 static int fd_htab_map_alloc_check(union bpf_attr *attr)
1398 if (attr->value_size != sizeof(u32))
1400 return htab_map_alloc_check(attr);
1403 static void fd_htab_map_free(struct bpf_map *map)
1405 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1406 struct hlist_nulls_node *n;
1407 struct hlist_nulls_head *head;
1408 struct htab_elem *l;
1411 for (i = 0; i < htab->n_buckets; i++) {
1412 head = select_bucket(htab, i);
1414 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1415 void *ptr = fd_htab_map_get_ptr(map, l);
1417 map->ops->map_fd_put_ptr(ptr);
1424 /* only called from syscall */
1425 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
1430 if (!map->ops->map_fd_sys_lookup_elem)
1434 ptr = htab_map_lookup_elem(map, key);
1436 *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
1444 /* only called from syscall */
1445 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1446 void *key, void *value, u64 map_flags)
1450 u32 ufd = *(u32 *)value;
1452 ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
1454 return PTR_ERR(ptr);
1456 ret = htab_map_update_elem(map, key, &ptr, map_flags);
1458 map->ops->map_fd_put_ptr(ptr);
1463 static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
1465 struct bpf_map *map, *inner_map_meta;
1467 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1468 if (IS_ERR(inner_map_meta))
1469 return inner_map_meta;
1471 map = htab_map_alloc(attr);
1473 bpf_map_meta_free(inner_map_meta);
1477 map->inner_map_meta = inner_map_meta;
1482 static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
1484 struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
1489 return READ_ONCE(*inner_map);
1492 static u32 htab_of_map_gen_lookup(struct bpf_map *map,
1493 struct bpf_insn *insn_buf)
1495 struct bpf_insn *insn = insn_buf;
1496 const int ret = BPF_REG_0;
1498 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
1499 (void *(*)(struct bpf_map *map, void *key))NULL));
1500 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
1501 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
1502 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
1503 offsetof(struct htab_elem, key) +
1504 round_up(map->key_size, 8));
1505 *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
1507 return insn - insn_buf;
1510 static void htab_of_map_free(struct bpf_map *map)
1512 bpf_map_meta_free(map->inner_map_meta);
1513 fd_htab_map_free(map);
1516 const struct bpf_map_ops htab_of_maps_map_ops = {
1517 .map_alloc_check = fd_htab_map_alloc_check,
1518 .map_alloc = htab_of_map_alloc,
1519 .map_free = htab_of_map_free,
1520 .map_get_next_key = htab_map_get_next_key,
1521 .map_lookup_elem = htab_of_map_lookup_elem,
1522 .map_delete_elem = htab_map_delete_elem,
1523 .map_fd_get_ptr = bpf_map_fd_get_ptr,
1524 .map_fd_put_ptr = bpf_map_fd_put_ptr,
1525 .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
1526 .map_gen_lookup = htab_of_map_gen_lookup,
1527 .map_check_btf = map_check_no_btf,