mm/thp: fix __split_huge_pmd_locked() for migration PMD
[linux/fpc-iii.git] / kernel / bpf / hashtab.c
blob039d64b1bfb7d472501ed06b48a84fcd5edcd102
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 map size is larger than memlock limit, reject it */
356 err = bpf_map_charge_init(&htab->map.memory, cost);
357 if (err)
358 goto free_htab;
360 err = -ENOMEM;
361 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
362 sizeof(struct bucket),
363 htab->map.numa_node);
364 if (!htab->buckets)
365 goto free_charge;
367 if (htab->map.map_flags & BPF_F_ZERO_SEED)
368 htab->hashrnd = 0;
369 else
370 htab->hashrnd = get_random_int();
372 for (i = 0; i < htab->n_buckets; i++) {
373 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
374 raw_spin_lock_init(&htab->buckets[i].lock);
377 if (prealloc) {
378 err = prealloc_init(htab);
379 if (err)
380 goto free_buckets;
382 if (!percpu && !lru) {
383 /* lru itself can remove the least used element, so
384 * there is no need for an extra elem during map_update.
386 err = alloc_extra_elems(htab);
387 if (err)
388 goto free_prealloc;
392 return &htab->map;
394 free_prealloc:
395 prealloc_destroy(htab);
396 free_buckets:
397 bpf_map_area_free(htab->buckets);
398 free_charge:
399 bpf_map_charge_finish(&htab->map.memory);
400 free_htab:
401 kfree(htab);
402 return ERR_PTR(err);
405 static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
407 return jhash(key, key_len, hashrnd);
410 static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
412 return &htab->buckets[hash & (htab->n_buckets - 1)];
415 static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
417 return &__select_bucket(htab, hash)->head;
420 /* this lookup function can only be called with bucket lock taken */
421 static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
422 void *key, u32 key_size)
424 struct hlist_nulls_node *n;
425 struct htab_elem *l;
427 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
428 if (l->hash == hash && !memcmp(&l->key, key, key_size))
429 return l;
431 return NULL;
434 /* can be called without bucket lock. it will repeat the loop in
435 * the unlikely event when elements moved from one bucket into another
436 * while link list is being walked
438 static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
439 u32 hash, void *key,
440 u32 key_size, u32 n_buckets)
442 struct hlist_nulls_node *n;
443 struct htab_elem *l;
445 again:
446 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
447 if (l->hash == hash && !memcmp(&l->key, key, key_size))
448 return l;
450 if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
451 goto again;
453 return NULL;
456 /* Called from syscall or from eBPF program directly, so
457 * arguments have to match bpf_map_lookup_elem() exactly.
458 * The return value is adjusted by BPF instructions
459 * in htab_map_gen_lookup().
461 static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
463 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
464 struct hlist_nulls_head *head;
465 struct htab_elem *l;
466 u32 hash, key_size;
468 /* Must be called with rcu_read_lock. */
469 WARN_ON_ONCE(!rcu_read_lock_held());
471 key_size = map->key_size;
473 hash = htab_map_hash(key, key_size, htab->hashrnd);
475 head = select_bucket(htab, hash);
477 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
479 return l;
482 static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
484 struct htab_elem *l = __htab_map_lookup_elem(map, key);
486 if (l)
487 return l->key + round_up(map->key_size, 8);
489 return NULL;
492 /* inline bpf_map_lookup_elem() call.
493 * Instead of:
494 * bpf_prog
495 * bpf_map_lookup_elem
496 * map->ops->map_lookup_elem
497 * htab_map_lookup_elem
498 * __htab_map_lookup_elem
499 * do:
500 * bpf_prog
501 * __htab_map_lookup_elem
503 static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
505 struct bpf_insn *insn = insn_buf;
506 const int ret = BPF_REG_0;
508 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
509 (void *(*)(struct bpf_map *map, void *key))NULL));
510 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
511 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
512 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
513 offsetof(struct htab_elem, key) +
514 round_up(map->key_size, 8));
515 return insn - insn_buf;
518 static __always_inline void *__htab_lru_map_lookup_elem(struct bpf_map *map,
519 void *key, const bool mark)
521 struct htab_elem *l = __htab_map_lookup_elem(map, key);
523 if (l) {
524 if (mark)
525 bpf_lru_node_set_ref(&l->lru_node);
526 return l->key + round_up(map->key_size, 8);
529 return NULL;
532 static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
534 return __htab_lru_map_lookup_elem(map, key, true);
537 static void *htab_lru_map_lookup_elem_sys(struct bpf_map *map, void *key)
539 return __htab_lru_map_lookup_elem(map, key, false);
542 static u32 htab_lru_map_gen_lookup(struct bpf_map *map,
543 struct bpf_insn *insn_buf)
545 struct bpf_insn *insn = insn_buf;
546 const int ret = BPF_REG_0;
547 const int ref_reg = BPF_REG_1;
549 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
550 (void *(*)(struct bpf_map *map, void *key))NULL));
551 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
552 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
553 *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
554 offsetof(struct htab_elem, lru_node) +
555 offsetof(struct bpf_lru_node, ref));
556 *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
557 *insn++ = BPF_ST_MEM(BPF_B, ret,
558 offsetof(struct htab_elem, lru_node) +
559 offsetof(struct bpf_lru_node, ref),
561 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
562 offsetof(struct htab_elem, key) +
563 round_up(map->key_size, 8));
564 return insn - insn_buf;
567 /* It is called from the bpf_lru_list when the LRU needs to delete
568 * older elements from the htab.
570 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
572 struct bpf_htab *htab = (struct bpf_htab *)arg;
573 struct htab_elem *l = NULL, *tgt_l;
574 struct hlist_nulls_head *head;
575 struct hlist_nulls_node *n;
576 unsigned long flags;
577 struct bucket *b;
579 tgt_l = container_of(node, struct htab_elem, lru_node);
580 b = __select_bucket(htab, tgt_l->hash);
581 head = &b->head;
583 raw_spin_lock_irqsave(&b->lock, flags);
585 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
586 if (l == tgt_l) {
587 hlist_nulls_del_rcu(&l->hash_node);
588 break;
591 raw_spin_unlock_irqrestore(&b->lock, flags);
593 return l == tgt_l;
596 /* Called from syscall */
597 static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
599 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
600 struct hlist_nulls_head *head;
601 struct htab_elem *l, *next_l;
602 u32 hash, key_size;
603 int i = 0;
605 WARN_ON_ONCE(!rcu_read_lock_held());
607 key_size = map->key_size;
609 if (!key)
610 goto find_first_elem;
612 hash = htab_map_hash(key, key_size, htab->hashrnd);
614 head = select_bucket(htab, hash);
616 /* lookup the key */
617 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
619 if (!l)
620 goto find_first_elem;
622 /* key was found, get next key in the same bucket */
623 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
624 struct htab_elem, hash_node);
626 if (next_l) {
627 /* if next elem in this hash list is non-zero, just return it */
628 memcpy(next_key, next_l->key, key_size);
629 return 0;
632 /* no more elements in this hash list, go to the next bucket */
633 i = hash & (htab->n_buckets - 1);
634 i++;
636 find_first_elem:
637 /* iterate over buckets */
638 for (; i < htab->n_buckets; i++) {
639 head = select_bucket(htab, i);
641 /* pick first element in the bucket */
642 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
643 struct htab_elem, hash_node);
644 if (next_l) {
645 /* if it's not empty, just return it */
646 memcpy(next_key, next_l->key, key_size);
647 return 0;
651 /* iterated over all buckets and all elements */
652 return -ENOENT;
655 static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
657 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
658 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
659 kfree(l);
662 static void htab_elem_free_rcu(struct rcu_head *head)
664 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
665 struct bpf_htab *htab = l->htab;
667 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
668 * we're calling kfree, otherwise deadlock is possible if kprobes
669 * are placed somewhere inside of slub
671 preempt_disable();
672 __this_cpu_inc(bpf_prog_active);
673 htab_elem_free(htab, l);
674 __this_cpu_dec(bpf_prog_active);
675 preempt_enable();
678 static void htab_put_fd_value(struct bpf_htab *htab, struct htab_elem *l)
680 struct bpf_map *map = &htab->map;
681 void *ptr;
683 if (map->ops->map_fd_put_ptr) {
684 ptr = fd_htab_map_get_ptr(map, l);
685 map->ops->map_fd_put_ptr(ptr);
689 static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
691 htab_put_fd_value(htab, l);
693 if (htab_is_prealloc(htab)) {
694 __pcpu_freelist_push(&htab->freelist, &l->fnode);
695 } else {
696 atomic_dec(&htab->count);
697 l->htab = htab;
698 call_rcu(&l->rcu, htab_elem_free_rcu);
702 static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
703 void *value, bool onallcpus)
705 if (!onallcpus) {
706 /* copy true value_size bytes */
707 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
708 } else {
709 u32 size = round_up(htab->map.value_size, 8);
710 int off = 0, cpu;
712 for_each_possible_cpu(cpu) {
713 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
714 value + off, size);
715 off += size;
720 static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
722 return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
723 BITS_PER_LONG == 64;
726 static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
727 void *value, u32 key_size, u32 hash,
728 bool percpu, bool onallcpus,
729 struct htab_elem *old_elem)
731 u32 size = htab->map.value_size;
732 bool prealloc = htab_is_prealloc(htab);
733 struct htab_elem *l_new, **pl_new;
734 void __percpu *pptr;
736 if (prealloc) {
737 if (old_elem) {
738 /* if we're updating the existing element,
739 * use per-cpu extra elems to avoid freelist_pop/push
741 pl_new = this_cpu_ptr(htab->extra_elems);
742 l_new = *pl_new;
743 htab_put_fd_value(htab, old_elem);
744 *pl_new = old_elem;
745 } else {
746 struct pcpu_freelist_node *l;
748 l = __pcpu_freelist_pop(&htab->freelist);
749 if (!l)
750 return ERR_PTR(-E2BIG);
751 l_new = container_of(l, struct htab_elem, fnode);
753 } else {
754 if (atomic_inc_return(&htab->count) > htab->map.max_entries)
755 if (!old_elem) {
756 /* when map is full and update() is replacing
757 * old element, it's ok to allocate, since
758 * old element will be freed immediately.
759 * Otherwise return an error
761 l_new = ERR_PTR(-E2BIG);
762 goto dec_count;
764 l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
765 htab->map.numa_node);
766 if (!l_new) {
767 l_new = ERR_PTR(-ENOMEM);
768 goto dec_count;
770 check_and_init_map_lock(&htab->map,
771 l_new->key + round_up(key_size, 8));
774 memcpy(l_new->key, key, key_size);
775 if (percpu) {
776 size = round_up(size, 8);
777 if (prealloc) {
778 pptr = htab_elem_get_ptr(l_new, key_size);
779 } else {
780 /* alloc_percpu zero-fills */
781 pptr = __alloc_percpu_gfp(size, 8,
782 GFP_ATOMIC | __GFP_NOWARN);
783 if (!pptr) {
784 kfree(l_new);
785 l_new = ERR_PTR(-ENOMEM);
786 goto dec_count;
790 pcpu_copy_value(htab, pptr, value, onallcpus);
792 if (!prealloc)
793 htab_elem_set_ptr(l_new, key_size, pptr);
794 } else if (fd_htab_map_needs_adjust(htab)) {
795 size = round_up(size, 8);
796 memcpy(l_new->key + round_up(key_size, 8), value, size);
797 } else {
798 copy_map_value(&htab->map,
799 l_new->key + round_up(key_size, 8),
800 value);
803 l_new->hash = hash;
804 return l_new;
805 dec_count:
806 atomic_dec(&htab->count);
807 return l_new;
810 static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
811 u64 map_flags)
813 if (l_old && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
814 /* elem already exists */
815 return -EEXIST;
817 if (!l_old && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
818 /* elem doesn't exist, cannot update it */
819 return -ENOENT;
821 return 0;
824 /* Called from syscall or from eBPF program */
825 static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
826 u64 map_flags)
828 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
829 struct htab_elem *l_new = NULL, *l_old;
830 struct hlist_nulls_head *head;
831 unsigned long flags;
832 struct bucket *b;
833 u32 key_size, hash;
834 int ret;
836 if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST))
837 /* unknown flags */
838 return -EINVAL;
840 WARN_ON_ONCE(!rcu_read_lock_held());
842 key_size = map->key_size;
844 hash = htab_map_hash(key, key_size, htab->hashrnd);
846 b = __select_bucket(htab, hash);
847 head = &b->head;
849 if (unlikely(map_flags & BPF_F_LOCK)) {
850 if (unlikely(!map_value_has_spin_lock(map)))
851 return -EINVAL;
852 /* find an element without taking the bucket lock */
853 l_old = lookup_nulls_elem_raw(head, hash, key, key_size,
854 htab->n_buckets);
855 ret = check_flags(htab, l_old, map_flags);
856 if (ret)
857 return ret;
858 if (l_old) {
859 /* grab the element lock and update value in place */
860 copy_map_value_locked(map,
861 l_old->key + round_up(key_size, 8),
862 value, false);
863 return 0;
865 /* fall through, grab the bucket lock and lookup again.
866 * 99.9% chance that the element won't be found,
867 * but second lookup under lock has to be done.
871 /* bpf_map_update_elem() can be called in_irq() */
872 raw_spin_lock_irqsave(&b->lock, flags);
874 l_old = lookup_elem_raw(head, hash, key, key_size);
876 ret = check_flags(htab, l_old, map_flags);
877 if (ret)
878 goto err;
880 if (unlikely(l_old && (map_flags & BPF_F_LOCK))) {
881 /* first lookup without the bucket lock didn't find the element,
882 * but second lookup with the bucket lock found it.
883 * This case is highly unlikely, but has to be dealt with:
884 * grab the element lock in addition to the bucket lock
885 * and update element in place
887 copy_map_value_locked(map,
888 l_old->key + round_up(key_size, 8),
889 value, false);
890 ret = 0;
891 goto err;
894 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
895 l_old);
896 if (IS_ERR(l_new)) {
897 /* all pre-allocated elements are in use or memory exhausted */
898 ret = PTR_ERR(l_new);
899 goto err;
902 /* add new element to the head of the list, so that
903 * concurrent search will find it before old elem
905 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
906 if (l_old) {
907 hlist_nulls_del_rcu(&l_old->hash_node);
908 if (!htab_is_prealloc(htab))
909 free_htab_elem(htab, l_old);
911 ret = 0;
912 err:
913 raw_spin_unlock_irqrestore(&b->lock, flags);
914 return ret;
917 static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
918 u64 map_flags)
920 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
921 struct htab_elem *l_new, *l_old = NULL;
922 struct hlist_nulls_head *head;
923 unsigned long flags;
924 struct bucket *b;
925 u32 key_size, hash;
926 int ret;
928 if (unlikely(map_flags > BPF_EXIST))
929 /* unknown flags */
930 return -EINVAL;
932 WARN_ON_ONCE(!rcu_read_lock_held());
934 key_size = map->key_size;
936 hash = htab_map_hash(key, key_size, htab->hashrnd);
938 b = __select_bucket(htab, hash);
939 head = &b->head;
941 /* For LRU, we need to alloc before taking bucket's
942 * spinlock because getting free nodes from LRU may need
943 * to remove older elements from htab and this removal
944 * operation will need a bucket lock.
946 l_new = prealloc_lru_pop(htab, key, hash);
947 if (!l_new)
948 return -ENOMEM;
949 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
951 /* bpf_map_update_elem() can be called in_irq() */
952 raw_spin_lock_irqsave(&b->lock, flags);
954 l_old = lookup_elem_raw(head, hash, key, key_size);
956 ret = check_flags(htab, l_old, map_flags);
957 if (ret)
958 goto err;
960 /* add new element to the head of the list, so that
961 * concurrent search will find it before old elem
963 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
964 if (l_old) {
965 bpf_lru_node_set_ref(&l_new->lru_node);
966 hlist_nulls_del_rcu(&l_old->hash_node);
968 ret = 0;
970 err:
971 raw_spin_unlock_irqrestore(&b->lock, flags);
973 if (ret)
974 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
975 else if (l_old)
976 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
978 return ret;
981 static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
982 void *value, u64 map_flags,
983 bool onallcpus)
985 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
986 struct htab_elem *l_new = NULL, *l_old;
987 struct hlist_nulls_head *head;
988 unsigned long flags;
989 struct bucket *b;
990 u32 key_size, hash;
991 int ret;
993 if (unlikely(map_flags > BPF_EXIST))
994 /* unknown flags */
995 return -EINVAL;
997 WARN_ON_ONCE(!rcu_read_lock_held());
999 key_size = map->key_size;
1001 hash = htab_map_hash(key, key_size, htab->hashrnd);
1003 b = __select_bucket(htab, hash);
1004 head = &b->head;
1006 /* bpf_map_update_elem() can be called in_irq() */
1007 raw_spin_lock_irqsave(&b->lock, flags);
1009 l_old = lookup_elem_raw(head, hash, key, key_size);
1011 ret = check_flags(htab, l_old, map_flags);
1012 if (ret)
1013 goto err;
1015 if (l_old) {
1016 /* per-cpu hash map can update value in-place */
1017 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1018 value, onallcpus);
1019 } else {
1020 l_new = alloc_htab_elem(htab, key, value, key_size,
1021 hash, true, onallcpus, NULL);
1022 if (IS_ERR(l_new)) {
1023 ret = PTR_ERR(l_new);
1024 goto err;
1026 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1028 ret = 0;
1029 err:
1030 raw_spin_unlock_irqrestore(&b->lock, flags);
1031 return ret;
1034 static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1035 void *value, u64 map_flags,
1036 bool onallcpus)
1038 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1039 struct htab_elem *l_new = NULL, *l_old;
1040 struct hlist_nulls_head *head;
1041 unsigned long flags;
1042 struct bucket *b;
1043 u32 key_size, hash;
1044 int ret;
1046 if (unlikely(map_flags > BPF_EXIST))
1047 /* unknown flags */
1048 return -EINVAL;
1050 WARN_ON_ONCE(!rcu_read_lock_held());
1052 key_size = map->key_size;
1054 hash = htab_map_hash(key, key_size, htab->hashrnd);
1056 b = __select_bucket(htab, hash);
1057 head = &b->head;
1059 /* For LRU, we need to alloc before taking bucket's
1060 * spinlock because LRU's elem alloc may need
1061 * to remove older elem from htab and this removal
1062 * operation will need a bucket lock.
1064 if (map_flags != BPF_EXIST) {
1065 l_new = prealloc_lru_pop(htab, key, hash);
1066 if (!l_new)
1067 return -ENOMEM;
1070 /* bpf_map_update_elem() can be called in_irq() */
1071 raw_spin_lock_irqsave(&b->lock, flags);
1073 l_old = lookup_elem_raw(head, hash, key, key_size);
1075 ret = check_flags(htab, l_old, map_flags);
1076 if (ret)
1077 goto err;
1079 if (l_old) {
1080 bpf_lru_node_set_ref(&l_old->lru_node);
1082 /* per-cpu hash map can update value in-place */
1083 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1084 value, onallcpus);
1085 } else {
1086 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
1087 value, onallcpus);
1088 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1089 l_new = NULL;
1091 ret = 0;
1092 err:
1093 raw_spin_unlock_irqrestore(&b->lock, flags);
1094 if (l_new)
1095 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1096 return ret;
1099 static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1100 void *value, u64 map_flags)
1102 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
1105 static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1106 void *value, u64 map_flags)
1108 return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
1109 false);
1112 /* Called from syscall or from eBPF program */
1113 static int htab_map_delete_elem(struct bpf_map *map, void *key)
1115 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1116 struct hlist_nulls_head *head;
1117 struct bucket *b;
1118 struct htab_elem *l;
1119 unsigned long flags;
1120 u32 hash, key_size;
1121 int ret = -ENOENT;
1123 WARN_ON_ONCE(!rcu_read_lock_held());
1125 key_size = map->key_size;
1127 hash = htab_map_hash(key, key_size, htab->hashrnd);
1128 b = __select_bucket(htab, hash);
1129 head = &b->head;
1131 raw_spin_lock_irqsave(&b->lock, flags);
1133 l = lookup_elem_raw(head, hash, key, key_size);
1135 if (l) {
1136 hlist_nulls_del_rcu(&l->hash_node);
1137 free_htab_elem(htab, l);
1138 ret = 0;
1141 raw_spin_unlock_irqrestore(&b->lock, flags);
1142 return ret;
1145 static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1147 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1148 struct hlist_nulls_head *head;
1149 struct bucket *b;
1150 struct htab_elem *l;
1151 unsigned long flags;
1152 u32 hash, key_size;
1153 int ret = -ENOENT;
1155 WARN_ON_ONCE(!rcu_read_lock_held());
1157 key_size = map->key_size;
1159 hash = htab_map_hash(key, key_size, htab->hashrnd);
1160 b = __select_bucket(htab, hash);
1161 head = &b->head;
1163 raw_spin_lock_irqsave(&b->lock, flags);
1165 l = lookup_elem_raw(head, hash, key, key_size);
1167 if (l) {
1168 hlist_nulls_del_rcu(&l->hash_node);
1169 ret = 0;
1172 raw_spin_unlock_irqrestore(&b->lock, flags);
1173 if (l)
1174 bpf_lru_push_free(&htab->lru, &l->lru_node);
1175 return ret;
1178 static void delete_all_elements(struct bpf_htab *htab)
1180 int i;
1182 for (i = 0; i < htab->n_buckets; i++) {
1183 struct hlist_nulls_head *head = select_bucket(htab, i);
1184 struct hlist_nulls_node *n;
1185 struct htab_elem *l;
1187 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1188 hlist_nulls_del_rcu(&l->hash_node);
1189 htab_elem_free(htab, l);
1194 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1195 static void htab_map_free(struct bpf_map *map)
1197 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1199 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1200 * so the programs (can be more than one that used this map) were
1201 * disconnected from events. Wait for outstanding critical sections in
1202 * these programs to complete
1204 synchronize_rcu();
1206 /* some of free_htab_elem() callbacks for elements of this map may
1207 * not have executed. Wait for them.
1209 rcu_barrier();
1210 if (!htab_is_prealloc(htab))
1211 delete_all_elements(htab);
1212 else
1213 prealloc_destroy(htab);
1215 free_percpu(htab->extra_elems);
1216 bpf_map_area_free(htab->buckets);
1217 kfree(htab);
1220 static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1221 struct seq_file *m)
1223 void *value;
1225 rcu_read_lock();
1227 value = htab_map_lookup_elem(map, key);
1228 if (!value) {
1229 rcu_read_unlock();
1230 return;
1233 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1234 seq_puts(m, ": ");
1235 btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1236 seq_puts(m, "\n");
1238 rcu_read_unlock();
1241 const struct bpf_map_ops htab_map_ops = {
1242 .map_alloc_check = htab_map_alloc_check,
1243 .map_alloc = htab_map_alloc,
1244 .map_free = htab_map_free,
1245 .map_get_next_key = htab_map_get_next_key,
1246 .map_lookup_elem = htab_map_lookup_elem,
1247 .map_update_elem = htab_map_update_elem,
1248 .map_delete_elem = htab_map_delete_elem,
1249 .map_gen_lookup = htab_map_gen_lookup,
1250 .map_seq_show_elem = htab_map_seq_show_elem,
1253 const struct bpf_map_ops htab_lru_map_ops = {
1254 .map_alloc_check = htab_map_alloc_check,
1255 .map_alloc = htab_map_alloc,
1256 .map_free = htab_map_free,
1257 .map_get_next_key = htab_map_get_next_key,
1258 .map_lookup_elem = htab_lru_map_lookup_elem,
1259 .map_lookup_elem_sys_only = htab_lru_map_lookup_elem_sys,
1260 .map_update_elem = htab_lru_map_update_elem,
1261 .map_delete_elem = htab_lru_map_delete_elem,
1262 .map_gen_lookup = htab_lru_map_gen_lookup,
1263 .map_seq_show_elem = htab_map_seq_show_elem,
1266 /* Called from eBPF program */
1267 static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1269 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1271 if (l)
1272 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1273 else
1274 return NULL;
1277 static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1279 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1281 if (l) {
1282 bpf_lru_node_set_ref(&l->lru_node);
1283 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1286 return NULL;
1289 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1291 struct htab_elem *l;
1292 void __percpu *pptr;
1293 int ret = -ENOENT;
1294 int cpu, off = 0;
1295 u32 size;
1297 /* per_cpu areas are zero-filled and bpf programs can only
1298 * access 'value_size' of them, so copying rounded areas
1299 * will not leak any kernel data
1301 size = round_up(map->value_size, 8);
1302 rcu_read_lock();
1303 l = __htab_map_lookup_elem(map, key);
1304 if (!l)
1305 goto out;
1306 /* We do not mark LRU map element here in order to not mess up
1307 * eviction heuristics when user space does a map walk.
1309 pptr = htab_elem_get_ptr(l, map->key_size);
1310 for_each_possible_cpu(cpu) {
1311 bpf_long_memcpy(value + off,
1312 per_cpu_ptr(pptr, cpu), size);
1313 off += size;
1315 ret = 0;
1316 out:
1317 rcu_read_unlock();
1318 return ret;
1321 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1322 u64 map_flags)
1324 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1325 int ret;
1327 rcu_read_lock();
1328 if (htab_is_lru(htab))
1329 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1330 map_flags, true);
1331 else
1332 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1333 true);
1334 rcu_read_unlock();
1336 return ret;
1339 static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
1340 struct seq_file *m)
1342 struct htab_elem *l;
1343 void __percpu *pptr;
1344 int cpu;
1346 rcu_read_lock();
1348 l = __htab_map_lookup_elem(map, key);
1349 if (!l) {
1350 rcu_read_unlock();
1351 return;
1354 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1355 seq_puts(m, ": {\n");
1356 pptr = htab_elem_get_ptr(l, map->key_size);
1357 for_each_possible_cpu(cpu) {
1358 seq_printf(m, "\tcpu%d: ", cpu);
1359 btf_type_seq_show(map->btf, map->btf_value_type_id,
1360 per_cpu_ptr(pptr, cpu), m);
1361 seq_puts(m, "\n");
1363 seq_puts(m, "}\n");
1365 rcu_read_unlock();
1368 const struct bpf_map_ops htab_percpu_map_ops = {
1369 .map_alloc_check = htab_map_alloc_check,
1370 .map_alloc = htab_map_alloc,
1371 .map_free = htab_map_free,
1372 .map_get_next_key = htab_map_get_next_key,
1373 .map_lookup_elem = htab_percpu_map_lookup_elem,
1374 .map_update_elem = htab_percpu_map_update_elem,
1375 .map_delete_elem = htab_map_delete_elem,
1376 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
1379 const struct bpf_map_ops htab_lru_percpu_map_ops = {
1380 .map_alloc_check = htab_map_alloc_check,
1381 .map_alloc = htab_map_alloc,
1382 .map_free = htab_map_free,
1383 .map_get_next_key = htab_map_get_next_key,
1384 .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1385 .map_update_elem = htab_lru_percpu_map_update_elem,
1386 .map_delete_elem = htab_lru_map_delete_elem,
1387 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
1390 static int fd_htab_map_alloc_check(union bpf_attr *attr)
1392 if (attr->value_size != sizeof(u32))
1393 return -EINVAL;
1394 return htab_map_alloc_check(attr);
1397 static void fd_htab_map_free(struct bpf_map *map)
1399 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1400 struct hlist_nulls_node *n;
1401 struct hlist_nulls_head *head;
1402 struct htab_elem *l;
1403 int i;
1405 for (i = 0; i < htab->n_buckets; i++) {
1406 head = select_bucket(htab, i);
1408 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1409 void *ptr = fd_htab_map_get_ptr(map, l);
1411 map->ops->map_fd_put_ptr(ptr);
1415 htab_map_free(map);
1418 /* only called from syscall */
1419 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
1421 void **ptr;
1422 int ret = 0;
1424 if (!map->ops->map_fd_sys_lookup_elem)
1425 return -ENOTSUPP;
1427 rcu_read_lock();
1428 ptr = htab_map_lookup_elem(map, key);
1429 if (ptr)
1430 *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
1431 else
1432 ret = -ENOENT;
1433 rcu_read_unlock();
1435 return ret;
1438 /* only called from syscall */
1439 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1440 void *key, void *value, u64 map_flags)
1442 void *ptr;
1443 int ret;
1444 u32 ufd = *(u32 *)value;
1446 ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
1447 if (IS_ERR(ptr))
1448 return PTR_ERR(ptr);
1450 ret = htab_map_update_elem(map, key, &ptr, map_flags);
1451 if (ret)
1452 map->ops->map_fd_put_ptr(ptr);
1454 return ret;
1457 static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
1459 struct bpf_map *map, *inner_map_meta;
1461 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1462 if (IS_ERR(inner_map_meta))
1463 return inner_map_meta;
1465 map = htab_map_alloc(attr);
1466 if (IS_ERR(map)) {
1467 bpf_map_meta_free(inner_map_meta);
1468 return map;
1471 map->inner_map_meta = inner_map_meta;
1473 return map;
1476 static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
1478 struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
1480 if (!inner_map)
1481 return NULL;
1483 return READ_ONCE(*inner_map);
1486 static u32 htab_of_map_gen_lookup(struct bpf_map *map,
1487 struct bpf_insn *insn_buf)
1489 struct bpf_insn *insn = insn_buf;
1490 const int ret = BPF_REG_0;
1492 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
1493 (void *(*)(struct bpf_map *map, void *key))NULL));
1494 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
1495 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
1496 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
1497 offsetof(struct htab_elem, key) +
1498 round_up(map->key_size, 8));
1499 *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
1501 return insn - insn_buf;
1504 static void htab_of_map_free(struct bpf_map *map)
1506 bpf_map_meta_free(map->inner_map_meta);
1507 fd_htab_map_free(map);
1510 const struct bpf_map_ops htab_of_maps_map_ops = {
1511 .map_alloc_check = fd_htab_map_alloc_check,
1512 .map_alloc = htab_of_map_alloc,
1513 .map_free = htab_of_map_free,
1514 .map_get_next_key = htab_map_get_next_key,
1515 .map_lookup_elem = htab_of_map_lookup_elem,
1516 .map_delete_elem = htab_map_delete_elem,
1517 .map_fd_get_ptr = bpf_map_fd_get_ptr,
1518 .map_fd_put_ptr = bpf_map_fd_put_ptr,
1519 .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
1520 .map_gen_lookup = htab_of_map_gen_lookup,
1521 .map_check_btf = map_check_no_btf,