Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / kernel / bpf / hashtab.c
blob583df5cb302d7f37f54822be1b7e2fd667e57003
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3 * Copyright (c) 2016 Facebook
4 */
5 #include <linux/bpf.h>
6 #include <linux/btf.h>
7 #include <linux/jhash.h>
8 #include <linux/filter.h>
9 #include <linux/rculist_nulls.h>
10 #include <linux/random.h>
11 #include <uapi/linux/btf.h>
12 #include "percpu_freelist.h"
13 #include "bpf_lru_list.h"
14 #include "map_in_map.h"
16 #define HTAB_CREATE_FLAG_MASK \
17 (BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE | \
18 BPF_F_ACCESS_MASK | BPF_F_ZERO_SEED)
20 struct bucket {
21 struct hlist_nulls_head head;
22 raw_spinlock_t lock;
25 struct bpf_htab {
26 struct bpf_map map;
27 struct bucket *buckets;
28 void *elems;
29 union {
30 struct pcpu_freelist freelist;
31 struct bpf_lru lru;
33 struct htab_elem *__percpu *extra_elems;
34 atomic_t count; /* number of elements in this hashtable */
35 u32 n_buckets; /* number of hash buckets */
36 u32 elem_size; /* size of each element in bytes */
37 u32 hashrnd;
40 /* each htab element is struct htab_elem + key + value */
41 struct htab_elem {
42 union {
43 struct hlist_nulls_node hash_node;
44 struct {
45 void *padding;
46 union {
47 struct bpf_htab *htab;
48 struct pcpu_freelist_node fnode;
52 union {
53 struct rcu_head rcu;
54 struct bpf_lru_node lru_node;
56 u32 hash;
57 char key[0] __aligned(8);
60 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
62 static bool htab_is_lru(const struct bpf_htab *htab)
64 return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
65 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
68 static bool htab_is_percpu(const struct bpf_htab *htab)
70 return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
71 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
74 static bool htab_is_prealloc(const struct bpf_htab *htab)
76 return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
79 static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
80 void __percpu *pptr)
82 *(void __percpu **)(l->key + key_size) = pptr;
85 static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
87 return *(void __percpu **)(l->key + key_size);
90 static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l)
92 return *(void **)(l->key + roundup(map->key_size, 8));
95 static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
97 return (struct htab_elem *) (htab->elems + i * htab->elem_size);
100 static void htab_free_elems(struct bpf_htab *htab)
102 int i;
104 if (!htab_is_percpu(htab))
105 goto free_elems;
107 for (i = 0; i < htab->map.max_entries; i++) {
108 void __percpu *pptr;
110 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
111 htab->map.key_size);
112 free_percpu(pptr);
113 cond_resched();
115 free_elems:
116 bpf_map_area_free(htab->elems);
119 static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
120 u32 hash)
122 struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
123 struct htab_elem *l;
125 if (node) {
126 l = container_of(node, struct htab_elem, lru_node);
127 memcpy(l->key, key, htab->map.key_size);
128 return l;
131 return NULL;
134 static int prealloc_init(struct bpf_htab *htab)
136 u32 num_entries = htab->map.max_entries;
137 int err = -ENOMEM, i;
139 if (!htab_is_percpu(htab) && !htab_is_lru(htab))
140 num_entries += num_possible_cpus();
142 htab->elems = bpf_map_area_alloc(htab->elem_size * num_entries,
143 htab->map.numa_node);
144 if (!htab->elems)
145 return -ENOMEM;
147 if (!htab_is_percpu(htab))
148 goto skip_percpu_elems;
150 for (i = 0; i < num_entries; i++) {
151 u32 size = round_up(htab->map.value_size, 8);
152 void __percpu *pptr;
154 pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
155 if (!pptr)
156 goto free_elems;
157 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
158 pptr);
159 cond_resched();
162 skip_percpu_elems:
163 if (htab_is_lru(htab))
164 err = bpf_lru_init(&htab->lru,
165 htab->map.map_flags & BPF_F_NO_COMMON_LRU,
166 offsetof(struct htab_elem, hash) -
167 offsetof(struct htab_elem, lru_node),
168 htab_lru_map_delete_node,
169 htab);
170 else
171 err = pcpu_freelist_init(&htab->freelist);
173 if (err)
174 goto free_elems;
176 if (htab_is_lru(htab))
177 bpf_lru_populate(&htab->lru, htab->elems,
178 offsetof(struct htab_elem, lru_node),
179 htab->elem_size, num_entries);
180 else
181 pcpu_freelist_populate(&htab->freelist,
182 htab->elems + offsetof(struct htab_elem, fnode),
183 htab->elem_size, num_entries);
185 return 0;
187 free_elems:
188 htab_free_elems(htab);
189 return err;
192 static void prealloc_destroy(struct bpf_htab *htab)
194 htab_free_elems(htab);
196 if (htab_is_lru(htab))
197 bpf_lru_destroy(&htab->lru);
198 else
199 pcpu_freelist_destroy(&htab->freelist);
202 static int alloc_extra_elems(struct bpf_htab *htab)
204 struct htab_elem *__percpu *pptr, *l_new;
205 struct pcpu_freelist_node *l;
206 int cpu;
208 pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8,
209 GFP_USER | __GFP_NOWARN);
210 if (!pptr)
211 return -ENOMEM;
213 for_each_possible_cpu(cpu) {
214 l = pcpu_freelist_pop(&htab->freelist);
215 /* pop will succeed, since prealloc_init()
216 * preallocated extra num_possible_cpus elements
218 l_new = container_of(l, struct htab_elem, fnode);
219 *per_cpu_ptr(pptr, cpu) = l_new;
221 htab->extra_elems = pptr;
222 return 0;
225 /* Called from syscall */
226 static int htab_map_alloc_check(union bpf_attr *attr)
228 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
229 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
230 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
231 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
232 /* percpu_lru means each cpu has its own LRU list.
233 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
234 * the map's value itself is percpu. percpu_lru has
235 * nothing to do with the map's value.
237 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
238 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
239 bool zero_seed = (attr->map_flags & BPF_F_ZERO_SEED);
240 int numa_node = bpf_map_attr_numa_node(attr);
242 BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
243 offsetof(struct htab_elem, hash_node.pprev));
244 BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
245 offsetof(struct htab_elem, hash_node.pprev));
247 if (lru && !capable(CAP_SYS_ADMIN))
248 /* LRU implementation is much complicated than other
249 * maps. Hence, limit to CAP_SYS_ADMIN for now.
251 return -EPERM;
253 if (zero_seed && !capable(CAP_SYS_ADMIN))
254 /* Guard against local DoS, and discourage production use. */
255 return -EPERM;
257 if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK ||
258 !bpf_map_flags_access_ok(attr->map_flags))
259 return -EINVAL;
261 if (!lru && percpu_lru)
262 return -EINVAL;
264 if (lru && !prealloc)
265 return -ENOTSUPP;
267 if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
268 return -EINVAL;
270 /* check sanity of attributes.
271 * value_size == 0 may be allowed in the future to use map as a set
273 if (attr->max_entries == 0 || attr->key_size == 0 ||
274 attr->value_size == 0)
275 return -EINVAL;
277 if (attr->key_size > MAX_BPF_STACK)
278 /* eBPF programs initialize keys on stack, so they cannot be
279 * larger than max stack size
281 return -E2BIG;
283 if (attr->value_size >= KMALLOC_MAX_SIZE -
284 MAX_BPF_STACK - sizeof(struct htab_elem))
285 /* if value_size is bigger, the user space won't be able to
286 * access the elements via bpf syscall. This check also makes
287 * sure that the elem_size doesn't overflow and it's
288 * kmalloc-able later in htab_map_update_elem()
290 return -E2BIG;
292 return 0;
295 static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
297 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
298 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
299 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
300 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
301 /* percpu_lru means each cpu has its own LRU list.
302 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
303 * the map's value itself is percpu. percpu_lru has
304 * nothing to do with the map's value.
306 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
307 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
308 struct bpf_htab *htab;
309 int err, i;
310 u64 cost;
312 htab = kzalloc(sizeof(*htab), GFP_USER);
313 if (!htab)
314 return ERR_PTR(-ENOMEM);
316 bpf_map_init_from_attr(&htab->map, attr);
318 if (percpu_lru) {
319 /* ensure each CPU's lru list has >=1 elements.
320 * since we are at it, make each lru list has the same
321 * number of elements.
323 htab->map.max_entries = roundup(attr->max_entries,
324 num_possible_cpus());
325 if (htab->map.max_entries < attr->max_entries)
326 htab->map.max_entries = rounddown(attr->max_entries,
327 num_possible_cpus());
330 /* hash table size must be power of 2 */
331 htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
333 htab->elem_size = sizeof(struct htab_elem) +
334 round_up(htab->map.key_size, 8);
335 if (percpu)
336 htab->elem_size += sizeof(void *);
337 else
338 htab->elem_size += round_up(htab->map.value_size, 8);
340 err = -E2BIG;
341 /* prevent zero size kmalloc and check for u32 overflow */
342 if (htab->n_buckets == 0 ||
343 htab->n_buckets > U32_MAX / sizeof(struct bucket))
344 goto free_htab;
346 cost = (u64) htab->n_buckets * sizeof(struct bucket) +
347 (u64) htab->elem_size * htab->map.max_entries;
349 if (percpu)
350 cost += (u64) round_up(htab->map.value_size, 8) *
351 num_possible_cpus() * htab->map.max_entries;
352 else
353 cost += (u64) htab->elem_size * num_possible_cpus();
355 if (cost >= U32_MAX - PAGE_SIZE)
356 /* make sure page count doesn't overflow */
357 goto free_htab;
359 htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
361 /* if map size is larger than memlock limit, reject it early */
362 err = bpf_map_precharge_memlock(htab->map.pages);
363 if (err)
364 goto free_htab;
366 err = -ENOMEM;
367 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
368 sizeof(struct bucket),
369 htab->map.numa_node);
370 if (!htab->buckets)
371 goto free_htab;
373 if (htab->map.map_flags & BPF_F_ZERO_SEED)
374 htab->hashrnd = 0;
375 else
376 htab->hashrnd = get_random_int();
378 for (i = 0; i < htab->n_buckets; i++) {
379 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
380 raw_spin_lock_init(&htab->buckets[i].lock);
383 if (prealloc) {
384 err = prealloc_init(htab);
385 if (err)
386 goto free_buckets;
388 if (!percpu && !lru) {
389 /* lru itself can remove the least used element, so
390 * there is no need for an extra elem during map_update.
392 err = alloc_extra_elems(htab);
393 if (err)
394 goto free_prealloc;
398 return &htab->map;
400 free_prealloc:
401 prealloc_destroy(htab);
402 free_buckets:
403 bpf_map_area_free(htab->buckets);
404 free_htab:
405 kfree(htab);
406 return ERR_PTR(err);
409 static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
411 return jhash(key, key_len, hashrnd);
414 static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
416 return &htab->buckets[hash & (htab->n_buckets - 1)];
419 static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
421 return &__select_bucket(htab, hash)->head;
424 /* this lookup function can only be called with bucket lock taken */
425 static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
426 void *key, u32 key_size)
428 struct hlist_nulls_node *n;
429 struct htab_elem *l;
431 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
432 if (l->hash == hash && !memcmp(&l->key, key, key_size))
433 return l;
435 return NULL;
438 /* can be called without bucket lock. it will repeat the loop in
439 * the unlikely event when elements moved from one bucket into another
440 * while link list is being walked
442 static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
443 u32 hash, void *key,
444 u32 key_size, u32 n_buckets)
446 struct hlist_nulls_node *n;
447 struct htab_elem *l;
449 again:
450 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
451 if (l->hash == hash && !memcmp(&l->key, key, key_size))
452 return l;
454 if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
455 goto again;
457 return NULL;
460 /* Called from syscall or from eBPF program directly, so
461 * arguments have to match bpf_map_lookup_elem() exactly.
462 * The return value is adjusted by BPF instructions
463 * in htab_map_gen_lookup().
465 static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
467 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
468 struct hlist_nulls_head *head;
469 struct htab_elem *l;
470 u32 hash, key_size;
472 /* Must be called with rcu_read_lock. */
473 WARN_ON_ONCE(!rcu_read_lock_held());
475 key_size = map->key_size;
477 hash = htab_map_hash(key, key_size, htab->hashrnd);
479 head = select_bucket(htab, hash);
481 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
483 return l;
486 static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
488 struct htab_elem *l = __htab_map_lookup_elem(map, key);
490 if (l)
491 return l->key + round_up(map->key_size, 8);
493 return NULL;
496 /* inline bpf_map_lookup_elem() call.
497 * Instead of:
498 * bpf_prog
499 * bpf_map_lookup_elem
500 * map->ops->map_lookup_elem
501 * htab_map_lookup_elem
502 * __htab_map_lookup_elem
503 * do:
504 * bpf_prog
505 * __htab_map_lookup_elem
507 static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
509 struct bpf_insn *insn = insn_buf;
510 const int ret = BPF_REG_0;
512 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
513 (void *(*)(struct bpf_map *map, void *key))NULL));
514 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
515 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
516 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
517 offsetof(struct htab_elem, key) +
518 round_up(map->key_size, 8));
519 return insn - insn_buf;
522 static __always_inline void *__htab_lru_map_lookup_elem(struct bpf_map *map,
523 void *key, const bool mark)
525 struct htab_elem *l = __htab_map_lookup_elem(map, key);
527 if (l) {
528 if (mark)
529 bpf_lru_node_set_ref(&l->lru_node);
530 return l->key + round_up(map->key_size, 8);
533 return NULL;
536 static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
538 return __htab_lru_map_lookup_elem(map, key, true);
541 static void *htab_lru_map_lookup_elem_sys(struct bpf_map *map, void *key)
543 return __htab_lru_map_lookup_elem(map, key, false);
546 static u32 htab_lru_map_gen_lookup(struct bpf_map *map,
547 struct bpf_insn *insn_buf)
549 struct bpf_insn *insn = insn_buf;
550 const int ret = BPF_REG_0;
551 const int ref_reg = BPF_REG_1;
553 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
554 (void *(*)(struct bpf_map *map, void *key))NULL));
555 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
556 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
557 *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
558 offsetof(struct htab_elem, lru_node) +
559 offsetof(struct bpf_lru_node, ref));
560 *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
561 *insn++ = BPF_ST_MEM(BPF_B, ret,
562 offsetof(struct htab_elem, lru_node) +
563 offsetof(struct bpf_lru_node, ref),
565 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
566 offsetof(struct htab_elem, key) +
567 round_up(map->key_size, 8));
568 return insn - insn_buf;
571 /* It is called from the bpf_lru_list when the LRU needs to delete
572 * older elements from the htab.
574 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
576 struct bpf_htab *htab = (struct bpf_htab *)arg;
577 struct htab_elem *l = NULL, *tgt_l;
578 struct hlist_nulls_head *head;
579 struct hlist_nulls_node *n;
580 unsigned long flags;
581 struct bucket *b;
583 tgt_l = container_of(node, struct htab_elem, lru_node);
584 b = __select_bucket(htab, tgt_l->hash);
585 head = &b->head;
587 raw_spin_lock_irqsave(&b->lock, flags);
589 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
590 if (l == tgt_l) {
591 hlist_nulls_del_rcu(&l->hash_node);
592 break;
595 raw_spin_unlock_irqrestore(&b->lock, flags);
597 return l == tgt_l;
600 /* Called from syscall */
601 static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
603 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
604 struct hlist_nulls_head *head;
605 struct htab_elem *l, *next_l;
606 u32 hash, key_size;
607 int i = 0;
609 WARN_ON_ONCE(!rcu_read_lock_held());
611 key_size = map->key_size;
613 if (!key)
614 goto find_first_elem;
616 hash = htab_map_hash(key, key_size, htab->hashrnd);
618 head = select_bucket(htab, hash);
620 /* lookup the key */
621 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
623 if (!l)
624 goto find_first_elem;
626 /* key was found, get next key in the same bucket */
627 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
628 struct htab_elem, hash_node);
630 if (next_l) {
631 /* if next elem in this hash list is non-zero, just return it */
632 memcpy(next_key, next_l->key, key_size);
633 return 0;
636 /* no more elements in this hash list, go to the next bucket */
637 i = hash & (htab->n_buckets - 1);
638 i++;
640 find_first_elem:
641 /* iterate over buckets */
642 for (; i < htab->n_buckets; i++) {
643 head = select_bucket(htab, i);
645 /* pick first element in the bucket */
646 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
647 struct htab_elem, hash_node);
648 if (next_l) {
649 /* if it's not empty, just return it */
650 memcpy(next_key, next_l->key, key_size);
651 return 0;
655 /* iterated over all buckets and all elements */
656 return -ENOENT;
659 static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
661 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
662 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
663 kfree(l);
666 static void htab_elem_free_rcu(struct rcu_head *head)
668 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
669 struct bpf_htab *htab = l->htab;
671 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
672 * we're calling kfree, otherwise deadlock is possible if kprobes
673 * are placed somewhere inside of slub
675 preempt_disable();
676 __this_cpu_inc(bpf_prog_active);
677 htab_elem_free(htab, l);
678 __this_cpu_dec(bpf_prog_active);
679 preempt_enable();
682 static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
684 struct bpf_map *map = &htab->map;
686 if (map->ops->map_fd_put_ptr) {
687 void *ptr = fd_htab_map_get_ptr(map, l);
689 map->ops->map_fd_put_ptr(ptr);
692 if (htab_is_prealloc(htab)) {
693 __pcpu_freelist_push(&htab->freelist, &l->fnode);
694 } else {
695 atomic_dec(&htab->count);
696 l->htab = htab;
697 call_rcu(&l->rcu, htab_elem_free_rcu);
701 static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
702 void *value, bool onallcpus)
704 if (!onallcpus) {
705 /* copy true value_size bytes */
706 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
707 } else {
708 u32 size = round_up(htab->map.value_size, 8);
709 int off = 0, cpu;
711 for_each_possible_cpu(cpu) {
712 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
713 value + off, size);
714 off += size;
719 static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
721 return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
722 BITS_PER_LONG == 64;
725 static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
726 void *value, u32 key_size, u32 hash,
727 bool percpu, bool onallcpus,
728 struct htab_elem *old_elem)
730 u32 size = htab->map.value_size;
731 bool prealloc = htab_is_prealloc(htab);
732 struct htab_elem *l_new, **pl_new;
733 void __percpu *pptr;
735 if (prealloc) {
736 if (old_elem) {
737 /* if we're updating the existing element,
738 * use per-cpu extra elems to avoid freelist_pop/push
740 pl_new = this_cpu_ptr(htab->extra_elems);
741 l_new = *pl_new;
742 *pl_new = old_elem;
743 } else {
744 struct pcpu_freelist_node *l;
746 l = __pcpu_freelist_pop(&htab->freelist);
747 if (!l)
748 return ERR_PTR(-E2BIG);
749 l_new = container_of(l, struct htab_elem, fnode);
751 } else {
752 if (atomic_inc_return(&htab->count) > htab->map.max_entries)
753 if (!old_elem) {
754 /* when map is full and update() is replacing
755 * old element, it's ok to allocate, since
756 * old element will be freed immediately.
757 * Otherwise return an error
759 l_new = ERR_PTR(-E2BIG);
760 goto dec_count;
762 l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
763 htab->map.numa_node);
764 if (!l_new) {
765 l_new = ERR_PTR(-ENOMEM);
766 goto dec_count;
768 check_and_init_map_lock(&htab->map,
769 l_new->key + round_up(key_size, 8));
772 memcpy(l_new->key, key, key_size);
773 if (percpu) {
774 size = round_up(size, 8);
775 if (prealloc) {
776 pptr = htab_elem_get_ptr(l_new, key_size);
777 } else {
778 /* alloc_percpu zero-fills */
779 pptr = __alloc_percpu_gfp(size, 8,
780 GFP_ATOMIC | __GFP_NOWARN);
781 if (!pptr) {
782 kfree(l_new);
783 l_new = ERR_PTR(-ENOMEM);
784 goto dec_count;
788 pcpu_copy_value(htab, pptr, value, onallcpus);
790 if (!prealloc)
791 htab_elem_set_ptr(l_new, key_size, pptr);
792 } else if (fd_htab_map_needs_adjust(htab)) {
793 size = round_up(size, 8);
794 memcpy(l_new->key + round_up(key_size, 8), value, size);
795 } else {
796 copy_map_value(&htab->map,
797 l_new->key + round_up(key_size, 8),
798 value);
801 l_new->hash = hash;
802 return l_new;
803 dec_count:
804 atomic_dec(&htab->count);
805 return l_new;
808 static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
809 u64 map_flags)
811 if (l_old && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
812 /* elem already exists */
813 return -EEXIST;
815 if (!l_old && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
816 /* elem doesn't exist, cannot update it */
817 return -ENOENT;
819 return 0;
822 /* Called from syscall or from eBPF program */
823 static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
824 u64 map_flags)
826 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
827 struct htab_elem *l_new = NULL, *l_old;
828 struct hlist_nulls_head *head;
829 unsigned long flags;
830 struct bucket *b;
831 u32 key_size, hash;
832 int ret;
834 if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST))
835 /* unknown flags */
836 return -EINVAL;
838 WARN_ON_ONCE(!rcu_read_lock_held());
840 key_size = map->key_size;
842 hash = htab_map_hash(key, key_size, htab->hashrnd);
844 b = __select_bucket(htab, hash);
845 head = &b->head;
847 if (unlikely(map_flags & BPF_F_LOCK)) {
848 if (unlikely(!map_value_has_spin_lock(map)))
849 return -EINVAL;
850 /* find an element without taking the bucket lock */
851 l_old = lookup_nulls_elem_raw(head, hash, key, key_size,
852 htab->n_buckets);
853 ret = check_flags(htab, l_old, map_flags);
854 if (ret)
855 return ret;
856 if (l_old) {
857 /* grab the element lock and update value in place */
858 copy_map_value_locked(map,
859 l_old->key + round_up(key_size, 8),
860 value, false);
861 return 0;
863 /* fall through, grab the bucket lock and lookup again.
864 * 99.9% chance that the element won't be found,
865 * but second lookup under lock has to be done.
869 /* bpf_map_update_elem() can be called in_irq() */
870 raw_spin_lock_irqsave(&b->lock, flags);
872 l_old = lookup_elem_raw(head, hash, key, key_size);
874 ret = check_flags(htab, l_old, map_flags);
875 if (ret)
876 goto err;
878 if (unlikely(l_old && (map_flags & BPF_F_LOCK))) {
879 /* first lookup without the bucket lock didn't find the element,
880 * but second lookup with the bucket lock found it.
881 * This case is highly unlikely, but has to be dealt with:
882 * grab the element lock in addition to the bucket lock
883 * and update element in place
885 copy_map_value_locked(map,
886 l_old->key + round_up(key_size, 8),
887 value, false);
888 ret = 0;
889 goto err;
892 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
893 l_old);
894 if (IS_ERR(l_new)) {
895 /* all pre-allocated elements are in use or memory exhausted */
896 ret = PTR_ERR(l_new);
897 goto err;
900 /* add new element to the head of the list, so that
901 * concurrent search will find it before old elem
903 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
904 if (l_old) {
905 hlist_nulls_del_rcu(&l_old->hash_node);
906 if (!htab_is_prealloc(htab))
907 free_htab_elem(htab, l_old);
909 ret = 0;
910 err:
911 raw_spin_unlock_irqrestore(&b->lock, flags);
912 return ret;
915 static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
916 u64 map_flags)
918 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
919 struct htab_elem *l_new, *l_old = NULL;
920 struct hlist_nulls_head *head;
921 unsigned long flags;
922 struct bucket *b;
923 u32 key_size, hash;
924 int ret;
926 if (unlikely(map_flags > BPF_EXIST))
927 /* unknown flags */
928 return -EINVAL;
930 WARN_ON_ONCE(!rcu_read_lock_held());
932 key_size = map->key_size;
934 hash = htab_map_hash(key, key_size, htab->hashrnd);
936 b = __select_bucket(htab, hash);
937 head = &b->head;
939 /* For LRU, we need to alloc before taking bucket's
940 * spinlock because getting free nodes from LRU may need
941 * to remove older elements from htab and this removal
942 * operation will need a bucket lock.
944 l_new = prealloc_lru_pop(htab, key, hash);
945 if (!l_new)
946 return -ENOMEM;
947 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
949 /* bpf_map_update_elem() can be called in_irq() */
950 raw_spin_lock_irqsave(&b->lock, flags);
952 l_old = lookup_elem_raw(head, hash, key, key_size);
954 ret = check_flags(htab, l_old, map_flags);
955 if (ret)
956 goto err;
958 /* add new element to the head of the list, so that
959 * concurrent search will find it before old elem
961 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
962 if (l_old) {
963 bpf_lru_node_set_ref(&l_new->lru_node);
964 hlist_nulls_del_rcu(&l_old->hash_node);
966 ret = 0;
968 err:
969 raw_spin_unlock_irqrestore(&b->lock, flags);
971 if (ret)
972 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
973 else if (l_old)
974 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
976 return ret;
979 static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
980 void *value, u64 map_flags,
981 bool onallcpus)
983 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
984 struct htab_elem *l_new = NULL, *l_old;
985 struct hlist_nulls_head *head;
986 unsigned long flags;
987 struct bucket *b;
988 u32 key_size, hash;
989 int ret;
991 if (unlikely(map_flags > BPF_EXIST))
992 /* unknown flags */
993 return -EINVAL;
995 WARN_ON_ONCE(!rcu_read_lock_held());
997 key_size = map->key_size;
999 hash = htab_map_hash(key, key_size, htab->hashrnd);
1001 b = __select_bucket(htab, hash);
1002 head = &b->head;
1004 /* bpf_map_update_elem() can be called in_irq() */
1005 raw_spin_lock_irqsave(&b->lock, flags);
1007 l_old = lookup_elem_raw(head, hash, key, key_size);
1009 ret = check_flags(htab, l_old, map_flags);
1010 if (ret)
1011 goto err;
1013 if (l_old) {
1014 /* per-cpu hash map can update value in-place */
1015 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1016 value, onallcpus);
1017 } else {
1018 l_new = alloc_htab_elem(htab, key, value, key_size,
1019 hash, true, onallcpus, NULL);
1020 if (IS_ERR(l_new)) {
1021 ret = PTR_ERR(l_new);
1022 goto err;
1024 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1026 ret = 0;
1027 err:
1028 raw_spin_unlock_irqrestore(&b->lock, flags);
1029 return ret;
1032 static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1033 void *value, u64 map_flags,
1034 bool onallcpus)
1036 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1037 struct htab_elem *l_new = NULL, *l_old;
1038 struct hlist_nulls_head *head;
1039 unsigned long flags;
1040 struct bucket *b;
1041 u32 key_size, hash;
1042 int ret;
1044 if (unlikely(map_flags > BPF_EXIST))
1045 /* unknown flags */
1046 return -EINVAL;
1048 WARN_ON_ONCE(!rcu_read_lock_held());
1050 key_size = map->key_size;
1052 hash = htab_map_hash(key, key_size, htab->hashrnd);
1054 b = __select_bucket(htab, hash);
1055 head = &b->head;
1057 /* For LRU, we need to alloc before taking bucket's
1058 * spinlock because LRU's elem alloc may need
1059 * to remove older elem from htab and this removal
1060 * operation will need a bucket lock.
1062 if (map_flags != BPF_EXIST) {
1063 l_new = prealloc_lru_pop(htab, key, hash);
1064 if (!l_new)
1065 return -ENOMEM;
1068 /* bpf_map_update_elem() can be called in_irq() */
1069 raw_spin_lock_irqsave(&b->lock, flags);
1071 l_old = lookup_elem_raw(head, hash, key, key_size);
1073 ret = check_flags(htab, l_old, map_flags);
1074 if (ret)
1075 goto err;
1077 if (l_old) {
1078 bpf_lru_node_set_ref(&l_old->lru_node);
1080 /* per-cpu hash map can update value in-place */
1081 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1082 value, onallcpus);
1083 } else {
1084 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
1085 value, onallcpus);
1086 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1087 l_new = NULL;
1089 ret = 0;
1090 err:
1091 raw_spin_unlock_irqrestore(&b->lock, flags);
1092 if (l_new)
1093 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1094 return ret;
1097 static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1098 void *value, u64 map_flags)
1100 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
1103 static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1104 void *value, u64 map_flags)
1106 return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
1107 false);
1110 /* Called from syscall or from eBPF program */
1111 static int htab_map_delete_elem(struct bpf_map *map, void *key)
1113 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1114 struct hlist_nulls_head *head;
1115 struct bucket *b;
1116 struct htab_elem *l;
1117 unsigned long flags;
1118 u32 hash, key_size;
1119 int ret = -ENOENT;
1121 WARN_ON_ONCE(!rcu_read_lock_held());
1123 key_size = map->key_size;
1125 hash = htab_map_hash(key, key_size, htab->hashrnd);
1126 b = __select_bucket(htab, hash);
1127 head = &b->head;
1129 raw_spin_lock_irqsave(&b->lock, flags);
1131 l = lookup_elem_raw(head, hash, key, key_size);
1133 if (l) {
1134 hlist_nulls_del_rcu(&l->hash_node);
1135 free_htab_elem(htab, l);
1136 ret = 0;
1139 raw_spin_unlock_irqrestore(&b->lock, flags);
1140 return ret;
1143 static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1145 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1146 struct hlist_nulls_head *head;
1147 struct bucket *b;
1148 struct htab_elem *l;
1149 unsigned long flags;
1150 u32 hash, key_size;
1151 int ret = -ENOENT;
1153 WARN_ON_ONCE(!rcu_read_lock_held());
1155 key_size = map->key_size;
1157 hash = htab_map_hash(key, key_size, htab->hashrnd);
1158 b = __select_bucket(htab, hash);
1159 head = &b->head;
1161 raw_spin_lock_irqsave(&b->lock, flags);
1163 l = lookup_elem_raw(head, hash, key, key_size);
1165 if (l) {
1166 hlist_nulls_del_rcu(&l->hash_node);
1167 ret = 0;
1170 raw_spin_unlock_irqrestore(&b->lock, flags);
1171 if (l)
1172 bpf_lru_push_free(&htab->lru, &l->lru_node);
1173 return ret;
1176 static void delete_all_elements(struct bpf_htab *htab)
1178 int i;
1180 for (i = 0; i < htab->n_buckets; i++) {
1181 struct hlist_nulls_head *head = select_bucket(htab, i);
1182 struct hlist_nulls_node *n;
1183 struct htab_elem *l;
1185 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1186 hlist_nulls_del_rcu(&l->hash_node);
1187 htab_elem_free(htab, l);
1192 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1193 static void htab_map_free(struct bpf_map *map)
1195 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1197 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1198 * so the programs (can be more than one that used this map) were
1199 * disconnected from events. Wait for outstanding critical sections in
1200 * these programs to complete
1202 synchronize_rcu();
1204 /* some of free_htab_elem() callbacks for elements of this map may
1205 * not have executed. Wait for them.
1207 rcu_barrier();
1208 if (!htab_is_prealloc(htab))
1209 delete_all_elements(htab);
1210 else
1211 prealloc_destroy(htab);
1213 free_percpu(htab->extra_elems);
1214 bpf_map_area_free(htab->buckets);
1215 kfree(htab);
1218 static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1219 struct seq_file *m)
1221 void *value;
1223 rcu_read_lock();
1225 value = htab_map_lookup_elem(map, key);
1226 if (!value) {
1227 rcu_read_unlock();
1228 return;
1231 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1232 seq_puts(m, ": ");
1233 btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1234 seq_puts(m, "\n");
1236 rcu_read_unlock();
1239 const struct bpf_map_ops htab_map_ops = {
1240 .map_alloc_check = htab_map_alloc_check,
1241 .map_alloc = htab_map_alloc,
1242 .map_free = htab_map_free,
1243 .map_get_next_key = htab_map_get_next_key,
1244 .map_lookup_elem = htab_map_lookup_elem,
1245 .map_update_elem = htab_map_update_elem,
1246 .map_delete_elem = htab_map_delete_elem,
1247 .map_gen_lookup = htab_map_gen_lookup,
1248 .map_seq_show_elem = htab_map_seq_show_elem,
1251 const struct bpf_map_ops htab_lru_map_ops = {
1252 .map_alloc_check = htab_map_alloc_check,
1253 .map_alloc = htab_map_alloc,
1254 .map_free = htab_map_free,
1255 .map_get_next_key = htab_map_get_next_key,
1256 .map_lookup_elem = htab_lru_map_lookup_elem,
1257 .map_lookup_elem_sys_only = htab_lru_map_lookup_elem_sys,
1258 .map_update_elem = htab_lru_map_update_elem,
1259 .map_delete_elem = htab_lru_map_delete_elem,
1260 .map_gen_lookup = htab_lru_map_gen_lookup,
1261 .map_seq_show_elem = htab_map_seq_show_elem,
1264 /* Called from eBPF program */
1265 static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1267 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1269 if (l)
1270 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1271 else
1272 return NULL;
1275 static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1277 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1279 if (l) {
1280 bpf_lru_node_set_ref(&l->lru_node);
1281 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1284 return NULL;
1287 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1289 struct htab_elem *l;
1290 void __percpu *pptr;
1291 int ret = -ENOENT;
1292 int cpu, off = 0;
1293 u32 size;
1295 /* per_cpu areas are zero-filled and bpf programs can only
1296 * access 'value_size' of them, so copying rounded areas
1297 * will not leak any kernel data
1299 size = round_up(map->value_size, 8);
1300 rcu_read_lock();
1301 l = __htab_map_lookup_elem(map, key);
1302 if (!l)
1303 goto out;
1304 /* We do not mark LRU map element here in order to not mess up
1305 * eviction heuristics when user space does a map walk.
1307 pptr = htab_elem_get_ptr(l, map->key_size);
1308 for_each_possible_cpu(cpu) {
1309 bpf_long_memcpy(value + off,
1310 per_cpu_ptr(pptr, cpu), size);
1311 off += size;
1313 ret = 0;
1314 out:
1315 rcu_read_unlock();
1316 return ret;
1319 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1320 u64 map_flags)
1322 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1323 int ret;
1325 rcu_read_lock();
1326 if (htab_is_lru(htab))
1327 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1328 map_flags, true);
1329 else
1330 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1331 true);
1332 rcu_read_unlock();
1334 return ret;
1337 static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
1338 struct seq_file *m)
1340 struct htab_elem *l;
1341 void __percpu *pptr;
1342 int cpu;
1344 rcu_read_lock();
1346 l = __htab_map_lookup_elem(map, key);
1347 if (!l) {
1348 rcu_read_unlock();
1349 return;
1352 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1353 seq_puts(m, ": {\n");
1354 pptr = htab_elem_get_ptr(l, map->key_size);
1355 for_each_possible_cpu(cpu) {
1356 seq_printf(m, "\tcpu%d: ", cpu);
1357 btf_type_seq_show(map->btf, map->btf_value_type_id,
1358 per_cpu_ptr(pptr, cpu), m);
1359 seq_puts(m, "\n");
1361 seq_puts(m, "}\n");
1363 rcu_read_unlock();
1366 const struct bpf_map_ops htab_percpu_map_ops = {
1367 .map_alloc_check = htab_map_alloc_check,
1368 .map_alloc = htab_map_alloc,
1369 .map_free = htab_map_free,
1370 .map_get_next_key = htab_map_get_next_key,
1371 .map_lookup_elem = htab_percpu_map_lookup_elem,
1372 .map_update_elem = htab_percpu_map_update_elem,
1373 .map_delete_elem = htab_map_delete_elem,
1374 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
1377 const struct bpf_map_ops htab_lru_percpu_map_ops = {
1378 .map_alloc_check = htab_map_alloc_check,
1379 .map_alloc = htab_map_alloc,
1380 .map_free = htab_map_free,
1381 .map_get_next_key = htab_map_get_next_key,
1382 .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1383 .map_update_elem = htab_lru_percpu_map_update_elem,
1384 .map_delete_elem = htab_lru_map_delete_elem,
1385 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
1388 static int fd_htab_map_alloc_check(union bpf_attr *attr)
1390 if (attr->value_size != sizeof(u32))
1391 return -EINVAL;
1392 return htab_map_alloc_check(attr);
1395 static void fd_htab_map_free(struct bpf_map *map)
1397 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1398 struct hlist_nulls_node *n;
1399 struct hlist_nulls_head *head;
1400 struct htab_elem *l;
1401 int i;
1403 for (i = 0; i < htab->n_buckets; i++) {
1404 head = select_bucket(htab, i);
1406 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1407 void *ptr = fd_htab_map_get_ptr(map, l);
1409 map->ops->map_fd_put_ptr(ptr);
1413 htab_map_free(map);
1416 /* only called from syscall */
1417 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
1419 void **ptr;
1420 int ret = 0;
1422 if (!map->ops->map_fd_sys_lookup_elem)
1423 return -ENOTSUPP;
1425 rcu_read_lock();
1426 ptr = htab_map_lookup_elem(map, key);
1427 if (ptr)
1428 *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
1429 else
1430 ret = -ENOENT;
1431 rcu_read_unlock();
1433 return ret;
1436 /* only called from syscall */
1437 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1438 void *key, void *value, u64 map_flags)
1440 void *ptr;
1441 int ret;
1442 u32 ufd = *(u32 *)value;
1444 ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
1445 if (IS_ERR(ptr))
1446 return PTR_ERR(ptr);
1448 ret = htab_map_update_elem(map, key, &ptr, map_flags);
1449 if (ret)
1450 map->ops->map_fd_put_ptr(ptr);
1452 return ret;
1455 static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
1457 struct bpf_map *map, *inner_map_meta;
1459 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1460 if (IS_ERR(inner_map_meta))
1461 return inner_map_meta;
1463 map = htab_map_alloc(attr);
1464 if (IS_ERR(map)) {
1465 bpf_map_meta_free(inner_map_meta);
1466 return map;
1469 map->inner_map_meta = inner_map_meta;
1471 return map;
1474 static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
1476 struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
1478 if (!inner_map)
1479 return NULL;
1481 return READ_ONCE(*inner_map);
1484 static u32 htab_of_map_gen_lookup(struct bpf_map *map,
1485 struct bpf_insn *insn_buf)
1487 struct bpf_insn *insn = insn_buf;
1488 const int ret = BPF_REG_0;
1490 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
1491 (void *(*)(struct bpf_map *map, void *key))NULL));
1492 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
1493 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
1494 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
1495 offsetof(struct htab_elem, key) +
1496 round_up(map->key_size, 8));
1497 *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
1499 return insn - insn_buf;
1502 static void htab_of_map_free(struct bpf_map *map)
1504 bpf_map_meta_free(map->inner_map_meta);
1505 fd_htab_map_free(map);
1508 const struct bpf_map_ops htab_of_maps_map_ops = {
1509 .map_alloc_check = fd_htab_map_alloc_check,
1510 .map_alloc = htab_of_map_alloc,
1511 .map_free = htab_of_map_free,
1512 .map_get_next_key = htab_map_get_next_key,
1513 .map_lookup_elem = htab_of_map_lookup_elem,
1514 .map_delete_elem = htab_map_delete_elem,
1515 .map_fd_get_ptr = bpf_map_fd_get_ptr,
1516 .map_fd_put_ptr = bpf_map_fd_put_ptr,
1517 .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
1518 .map_gen_lookup = htab_of_map_gen_lookup,
1519 .map_check_btf = map_check_no_btf,