blob: 0d14a2a11463a0861624c11a518c068760daee07 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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"
15
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)
19
20struct bucket {
21 struct hlist_nulls_head head;
22 raw_spinlock_t lock;
23};
24
25struct 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;
32 };
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;
38};
39
40/* each htab element is struct htab_elem + key + value */
41struct 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;
49 };
50 };
51 };
52 union {
53 struct rcu_head rcu;
54 struct bpf_lru_node lru_node;
55 };
56 u32 hash;
57 char key[0] __aligned(8);
58};
59
60static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
61
62static bool htab_is_lru(const struct bpf_htab *htab)
63{
64 return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
65 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
66}
67
68static bool htab_is_percpu(const struct bpf_htab *htab)
69{
70 return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
71 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
72}
73
74static bool htab_is_prealloc(const struct bpf_htab *htab)
75{
76 return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
77}
78
79static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
80 void __percpu *pptr)
81{
82 *(void __percpu **)(l->key + key_size) = pptr;
83}
84
85static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
86{
87 return *(void __percpu **)(l->key + key_size);
88}
89
90static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l)
91{
92 return *(void **)(l->key + roundup(map->key_size, 8));
93}
94
95static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
96{
97 return (struct htab_elem *) (htab->elems + i * htab->elem_size);
98}
99
100static void htab_free_elems(struct bpf_htab *htab)
101{
102 int i;
103
104 if (!htab_is_percpu(htab))
105 goto free_elems;
106
107 for (i = 0; i < htab->map.max_entries; i++) {
108 void __percpu *pptr;
109
110 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
111 htab->map.key_size);
112 free_percpu(pptr);
113 cond_resched();
114 }
115free_elems:
116 bpf_map_area_free(htab->elems);
117}
118
119static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
120 u32 hash)
121{
122 struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
123 struct htab_elem *l;
124
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;
129 }
130
131 return NULL;
132}
133
134static int prealloc_init(struct bpf_htab *htab)
135{
136 u32 num_entries = htab->map.max_entries;
137 int err = -ENOMEM, i;
138
139 if (!htab_is_percpu(htab) && !htab_is_lru(htab))
140 num_entries += num_possible_cpus();
141
142 htab->elems = bpf_map_area_alloc(htab->elem_size * num_entries,
143 htab->map.numa_node);
144 if (!htab->elems)
145 return -ENOMEM;
146
147 if (!htab_is_percpu(htab))
148 goto skip_percpu_elems;
149
150 for (i = 0; i < num_entries; i++) {
151 u32 size = round_up(htab->map.value_size, 8);
152 void __percpu *pptr;
153
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();
160 }
161
162skip_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);
172
173 if (err)
174 goto free_elems;
175
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);
184
185 return 0;
186
187free_elems:
188 htab_free_elems(htab);
189 return err;
190}
191
192static void prealloc_destroy(struct bpf_htab *htab)
193{
194 htab_free_elems(htab);
195
196 if (htab_is_lru(htab))
197 bpf_lru_destroy(&htab->lru);
198 else
199 pcpu_freelist_destroy(&htab->freelist);
200}
201
202static int alloc_extra_elems(struct bpf_htab *htab)
203{
204 struct htab_elem *__percpu *pptr, *l_new;
205 struct pcpu_freelist_node *l;
206 int cpu;
207
208 pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8,
209 GFP_USER | __GFP_NOWARN);
210 if (!pptr)
211 return -ENOMEM;
212
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
217 */
218 l_new = container_of(l, struct htab_elem, fnode);
219 *per_cpu_ptr(pptr, cpu) = l_new;
220 }
221 htab->extra_elems = pptr;
222 return 0;
223}
224
225/* Called from syscall */
226static int htab_map_alloc_check(union bpf_attr *attr)
227{
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.
236 */
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);
241
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));
246
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.
250 */
251 return -EPERM;
252
253 if (zero_seed && !capable(CAP_SYS_ADMIN))
254 /* Guard against local DoS, and discourage production use. */
255 return -EPERM;
256
257 if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK ||
258 !bpf_map_flags_access_ok(attr->map_flags))
259 return -EINVAL;
260
261 if (!lru && percpu_lru)
262 return -EINVAL;
263
264 if (lru && !prealloc)
265 return -ENOTSUPP;
266
267 if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
268 return -EINVAL;
269
270 /* check sanity of attributes.
271 * value_size == 0 may be allowed in the future to use map as a set
272 */
273 if (attr->max_entries == 0 || attr->key_size == 0 ||
274 attr->value_size == 0)
275 return -EINVAL;
276
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
280 */
281 return -E2BIG;
282
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()
289 */
290 return -E2BIG;
291 /* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
292 if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
293 return -E2BIG;
294
295 return 0;
296}
297
298static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
299{
300 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
301 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
302 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
303 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
304 /* percpu_lru means each cpu has its own LRU list.
305 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
306 * the map's value itself is percpu. percpu_lru has
307 * nothing to do with the map's value.
308 */
309 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
310 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
311 struct bpf_htab *htab;
312 int err, i;
313 u64 cost;
314
315 htab = kzalloc(sizeof(*htab), GFP_USER);
316 if (!htab)
317 return ERR_PTR(-ENOMEM);
318
319 bpf_map_init_from_attr(&htab->map, attr);
320
321 if (percpu_lru) {
322 /* ensure each CPU's lru list has >=1 elements.
323 * since we are at it, make each lru list has the same
324 * number of elements.
325 */
326 htab->map.max_entries = roundup(attr->max_entries,
327 num_possible_cpus());
328 if (htab->map.max_entries < attr->max_entries)
329 htab->map.max_entries = rounddown(attr->max_entries,
330 num_possible_cpus());
331 }
332
333 /* hash table size must be power of 2; roundup_pow_of_two() can overflow
334 * into UB on 32-bit arches, so check that first
335 */
336 err = -E2BIG;
337 if (htab->map.max_entries > 1UL << 31)
338 goto free_htab;
339
340 htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
341
342 htab->elem_size = sizeof(struct htab_elem) +
343 round_up(htab->map.key_size, 8);
344 if (percpu)
345 htab->elem_size += sizeof(void *);
346 else
347 htab->elem_size += round_up(htab->map.value_size, 8);
348
349 /* check for u32 overflow */
350 if (htab->n_buckets > U32_MAX / sizeof(struct bucket))
351 goto free_htab;
352
353 cost = (u64) htab->n_buckets * sizeof(struct bucket) +
354 (u64) htab->elem_size * htab->map.max_entries;
355
356 if (percpu)
357 cost += (u64) round_up(htab->map.value_size, 8) *
358 num_possible_cpus() * htab->map.max_entries;
359 else
360 cost += (u64) htab->elem_size * num_possible_cpus();
361
362 /* if map size is larger than memlock limit, reject it */
363 err = bpf_map_charge_init(&htab->map.memory, cost);
364 if (err)
365 goto free_htab;
366
367 err = -ENOMEM;
368 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
369 sizeof(struct bucket),
370 htab->map.numa_node);
371 if (!htab->buckets)
372 goto free_charge;
373
374 if (htab->map.map_flags & BPF_F_ZERO_SEED)
375 htab->hashrnd = 0;
376 else
377 htab->hashrnd = get_random_int();
378
379 for (i = 0; i < htab->n_buckets; i++) {
380 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
381 raw_spin_lock_init(&htab->buckets[i].lock);
382 }
383
384 if (prealloc) {
385 err = prealloc_init(htab);
386 if (err)
387 goto free_buckets;
388
389 if (!percpu && !lru) {
390 /* lru itself can remove the least used element, so
391 * there is no need for an extra elem during map_update.
392 */
393 err = alloc_extra_elems(htab);
394 if (err)
395 goto free_prealloc;
396 }
397 }
398
399 return &htab->map;
400
401free_prealloc:
402 prealloc_destroy(htab);
403free_buckets:
404 bpf_map_area_free(htab->buckets);
405free_charge:
406 bpf_map_charge_finish(&htab->map.memory);
407free_htab:
408 kfree(htab);
409 return ERR_PTR(err);
410}
411
412static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
413{
414 return jhash(key, key_len, hashrnd);
415}
416
417static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
418{
419 return &htab->buckets[hash & (htab->n_buckets - 1)];
420}
421
422static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
423{
424 return &__select_bucket(htab, hash)->head;
425}
426
427/* this lookup function can only be called with bucket lock taken */
428static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
429 void *key, u32 key_size)
430{
431 struct hlist_nulls_node *n;
432 struct htab_elem *l;
433
434 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
435 if (l->hash == hash && !memcmp(&l->key, key, key_size))
436 return l;
437
438 return NULL;
439}
440
441/* can be called without bucket lock. it will repeat the loop in
442 * the unlikely event when elements moved from one bucket into another
443 * while link list is being walked
444 */
445static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
446 u32 hash, void *key,
447 u32 key_size, u32 n_buckets)
448{
449 struct hlist_nulls_node *n;
450 struct htab_elem *l;
451
452again:
453 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
454 if (l->hash == hash && !memcmp(&l->key, key, key_size))
455 return l;
456
457 if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
458 goto again;
459
460 return NULL;
461}
462
463/* Called from syscall or from eBPF program directly, so
464 * arguments have to match bpf_map_lookup_elem() exactly.
465 * The return value is adjusted by BPF instructions
466 * in htab_map_gen_lookup().
467 */
468static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
469{
470 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
471 struct hlist_nulls_head *head;
472 struct htab_elem *l;
473 u32 hash, key_size;
474
475 /* Must be called with rcu_read_lock. */
476 WARN_ON_ONCE(!rcu_read_lock_held());
477
478 key_size = map->key_size;
479
480 hash = htab_map_hash(key, key_size, htab->hashrnd);
481
482 head = select_bucket(htab, hash);
483
484 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
485
486 return l;
487}
488
489static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
490{
491 struct htab_elem *l = __htab_map_lookup_elem(map, key);
492
493 if (l)
494 return l->key + round_up(map->key_size, 8);
495
496 return NULL;
497}
498
499/* inline bpf_map_lookup_elem() call.
500 * Instead of:
501 * bpf_prog
502 * bpf_map_lookup_elem
503 * map->ops->map_lookup_elem
504 * htab_map_lookup_elem
505 * __htab_map_lookup_elem
506 * do:
507 * bpf_prog
508 * __htab_map_lookup_elem
509 */
510static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
511{
512 struct bpf_insn *insn = insn_buf;
513 const int ret = BPF_REG_0;
514
515 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
516 (void *(*)(struct bpf_map *map, void *key))NULL));
517 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
518 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
519 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
520 offsetof(struct htab_elem, key) +
521 round_up(map->key_size, 8));
522 return insn - insn_buf;
523}
524
525static __always_inline void *__htab_lru_map_lookup_elem(struct bpf_map *map,
526 void *key, const bool mark)
527{
528 struct htab_elem *l = __htab_map_lookup_elem(map, key);
529
530 if (l) {
531 if (mark)
532 bpf_lru_node_set_ref(&l->lru_node);
533 return l->key + round_up(map->key_size, 8);
534 }
535
536 return NULL;
537}
538
539static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
540{
541 return __htab_lru_map_lookup_elem(map, key, true);
542}
543
544static void *htab_lru_map_lookup_elem_sys(struct bpf_map *map, void *key)
545{
546 return __htab_lru_map_lookup_elem(map, key, false);
547}
548
549static u32 htab_lru_map_gen_lookup(struct bpf_map *map,
550 struct bpf_insn *insn_buf)
551{
552 struct bpf_insn *insn = insn_buf;
553 const int ret = BPF_REG_0;
554 const int ref_reg = BPF_REG_1;
555
556 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
557 (void *(*)(struct bpf_map *map, void *key))NULL));
558 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
559 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
560 *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
561 offsetof(struct htab_elem, lru_node) +
562 offsetof(struct bpf_lru_node, ref));
563 *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
564 *insn++ = BPF_ST_MEM(BPF_B, ret,
565 offsetof(struct htab_elem, lru_node) +
566 offsetof(struct bpf_lru_node, ref),
567 1);
568 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
569 offsetof(struct htab_elem, key) +
570 round_up(map->key_size, 8));
571 return insn - insn_buf;
572}
573
574/* It is called from the bpf_lru_list when the LRU needs to delete
575 * older elements from the htab.
576 */
577static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
578{
579 struct bpf_htab *htab = (struct bpf_htab *)arg;
580 struct htab_elem *l = NULL, *tgt_l;
581 struct hlist_nulls_head *head;
582 struct hlist_nulls_node *n;
583 unsigned long flags;
584 struct bucket *b;
585
586 tgt_l = container_of(node, struct htab_elem, lru_node);
587 b = __select_bucket(htab, tgt_l->hash);
588 head = &b->head;
589
590 raw_spin_lock_irqsave(&b->lock, flags);
591
592 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
593 if (l == tgt_l) {
594 hlist_nulls_del_rcu(&l->hash_node);
595 break;
596 }
597
598 raw_spin_unlock_irqrestore(&b->lock, flags);
599
600 return l == tgt_l;
601}
602
603/* Called from syscall */
604static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
605{
606 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
607 struct hlist_nulls_head *head;
608 struct htab_elem *l, *next_l;
609 u32 hash, key_size;
610 int i = 0;
611
612 WARN_ON_ONCE(!rcu_read_lock_held());
613
614 key_size = map->key_size;
615
616 if (!key)
617 goto find_first_elem;
618
619 hash = htab_map_hash(key, key_size, htab->hashrnd);
620
621 head = select_bucket(htab, hash);
622
623 /* lookup the key */
624 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
625
626 if (!l)
627 goto find_first_elem;
628
629 /* key was found, get next key in the same bucket */
630 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
631 struct htab_elem, hash_node);
632
633 if (next_l) {
634 /* if next elem in this hash list is non-zero, just return it */
635 memcpy(next_key, next_l->key, key_size);
636 return 0;
637 }
638
639 /* no more elements in this hash list, go to the next bucket */
640 i = hash & (htab->n_buckets - 1);
641 i++;
642
643find_first_elem:
644 /* iterate over buckets */
645 for (; i < htab->n_buckets; i++) {
646 head = select_bucket(htab, i);
647
648 /* pick first element in the bucket */
649 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
650 struct htab_elem, hash_node);
651 if (next_l) {
652 /* if it's not empty, just return it */
653 memcpy(next_key, next_l->key, key_size);
654 return 0;
655 }
656 }
657
658 /* iterated over all buckets and all elements */
659 return -ENOENT;
660}
661
662static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
663{
664 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
665 free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
666 kfree(l);
667}
668
669static void htab_elem_free_rcu(struct rcu_head *head)
670{
671 struct htab_elem *l = container_of(head, struct htab_elem, rcu);
672 struct bpf_htab *htab = l->htab;
673
674 htab_elem_free(htab, l);
675}
676
677static void htab_put_fd_value(struct bpf_htab *htab, struct htab_elem *l)
678{
679 struct bpf_map *map = &htab->map;
680 void *ptr;
681
682 if (map->ops->map_fd_put_ptr) {
683 ptr = fd_htab_map_get_ptr(map, l);
684 map->ops->map_fd_put_ptr(map, ptr, true);
685 }
686}
687
688static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
689{
690 htab_put_fd_value(htab, l);
691
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);
698 }
699}
700
701static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
702 void *value, bool onallcpus)
703{
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;
710
711 for_each_possible_cpu(cpu) {
712 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
713 value + off, size);
714 off += size;
715 }
716 }
717}
718
719static void pcpu_init_value(struct bpf_htab *htab, void __percpu *pptr,
720 void *value, bool onallcpus)
721{
722 /* When using prealloc and not setting the initial value on all cpus,
723 * zero-fill element values for other cpus (just as what happens when
724 * not using prealloc). Otherwise, bpf program has no way to ensure
725 * known initial values for cpus other than current one
726 * (onallcpus=false always when coming from bpf prog).
727 */
728 if (htab_is_prealloc(htab) && !onallcpus) {
729 u32 size = round_up(htab->map.value_size, 8);
730 int current_cpu = raw_smp_processor_id();
731 int cpu;
732
733 for_each_possible_cpu(cpu) {
734 if (cpu == current_cpu)
735 bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value,
736 size);
737 else
738 memset(per_cpu_ptr(pptr, cpu), 0, size);
739 }
740 } else {
741 pcpu_copy_value(htab, pptr, value, onallcpus);
742 }
743}
744
745static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
746{
747 return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
748 BITS_PER_LONG == 64;
749}
750
751static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
752 void *value, u32 key_size, u32 hash,
753 bool percpu, bool onallcpus,
754 struct htab_elem *old_elem)
755{
756 u32 size = htab->map.value_size;
757 bool prealloc = htab_is_prealloc(htab);
758 struct htab_elem *l_new, **pl_new;
759 void __percpu *pptr;
760
761 if (prealloc) {
762 if (old_elem) {
763 /* if we're updating the existing element,
764 * use per-cpu extra elems to avoid freelist_pop/push
765 */
766 pl_new = this_cpu_ptr(htab->extra_elems);
767 l_new = *pl_new;
768 htab_put_fd_value(htab, old_elem);
769 *pl_new = old_elem;
770 } else {
771 struct pcpu_freelist_node *l;
772
773 l = __pcpu_freelist_pop(&htab->freelist);
774 if (!l)
775 return ERR_PTR(-E2BIG);
776 l_new = container_of(l, struct htab_elem, fnode);
777 }
778 } else {
779 if (atomic_inc_return(&htab->count) > htab->map.max_entries)
780 if (!old_elem) {
781 /* when map is full and update() is replacing
782 * old element, it's ok to allocate, since
783 * old element will be freed immediately.
784 * Otherwise return an error
785 */
786 l_new = ERR_PTR(-E2BIG);
787 goto dec_count;
788 }
789 l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
790 htab->map.numa_node);
791 if (!l_new) {
792 l_new = ERR_PTR(-ENOMEM);
793 goto dec_count;
794 }
795 check_and_init_map_lock(&htab->map,
796 l_new->key + round_up(key_size, 8));
797 }
798
799 memcpy(l_new->key, key, key_size);
800 if (percpu) {
801 size = round_up(size, 8);
802 if (prealloc) {
803 pptr = htab_elem_get_ptr(l_new, key_size);
804 } else {
805 /* alloc_percpu zero-fills */
806 pptr = __alloc_percpu_gfp(size, 8,
807 GFP_ATOMIC | __GFP_NOWARN);
808 if (!pptr) {
809 kfree(l_new);
810 l_new = ERR_PTR(-ENOMEM);
811 goto dec_count;
812 }
813 }
814
815 pcpu_init_value(htab, pptr, value, onallcpus);
816
817 if (!prealloc)
818 htab_elem_set_ptr(l_new, key_size, pptr);
819 } else if (fd_htab_map_needs_adjust(htab)) {
820 size = round_up(size, 8);
821 memcpy(l_new->key + round_up(key_size, 8), value, size);
822 } else {
823 copy_map_value(&htab->map,
824 l_new->key + round_up(key_size, 8),
825 value);
826 }
827
828 l_new->hash = hash;
829 return l_new;
830dec_count:
831 atomic_dec(&htab->count);
832 return l_new;
833}
834
835static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
836 u64 map_flags)
837{
838 if (l_old && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
839 /* elem already exists */
840 return -EEXIST;
841
842 if (!l_old && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
843 /* elem doesn't exist, cannot update it */
844 return -ENOENT;
845
846 return 0;
847}
848
849/* Called from syscall or from eBPF program */
850static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
851 u64 map_flags)
852{
853 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
854 struct htab_elem *l_new = NULL, *l_old;
855 struct hlist_nulls_head *head;
856 unsigned long flags;
857 struct bucket *b;
858 u32 key_size, hash;
859 int ret;
860
861 if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST))
862 /* unknown flags */
863 return -EINVAL;
864
865 WARN_ON_ONCE(!rcu_read_lock_held());
866
867 key_size = map->key_size;
868
869 hash = htab_map_hash(key, key_size, htab->hashrnd);
870
871 b = __select_bucket(htab, hash);
872 head = &b->head;
873
874 if (unlikely(map_flags & BPF_F_LOCK)) {
875 if (unlikely(!map_value_has_spin_lock(map)))
876 return -EINVAL;
877 /* find an element without taking the bucket lock */
878 l_old = lookup_nulls_elem_raw(head, hash, key, key_size,
879 htab->n_buckets);
880 ret = check_flags(htab, l_old, map_flags);
881 if (ret)
882 return ret;
883 if (l_old) {
884 /* grab the element lock and update value in place */
885 copy_map_value_locked(map,
886 l_old->key + round_up(key_size, 8),
887 value, false);
888 return 0;
889 }
890 /* fall through, grab the bucket lock and lookup again.
891 * 99.9% chance that the element won't be found,
892 * but second lookup under lock has to be done.
893 */
894 }
895
896 /* bpf_map_update_elem() can be called in_irq() */
897 raw_spin_lock_irqsave(&b->lock, flags);
898
899 l_old = lookup_elem_raw(head, hash, key, key_size);
900
901 ret = check_flags(htab, l_old, map_flags);
902 if (ret)
903 goto err;
904
905 if (unlikely(l_old && (map_flags & BPF_F_LOCK))) {
906 /* first lookup without the bucket lock didn't find the element,
907 * but second lookup with the bucket lock found it.
908 * This case is highly unlikely, but has to be dealt with:
909 * grab the element lock in addition to the bucket lock
910 * and update element in place
911 */
912 copy_map_value_locked(map,
913 l_old->key + round_up(key_size, 8),
914 value, false);
915 ret = 0;
916 goto err;
917 }
918
919 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
920 l_old);
921 if (IS_ERR(l_new)) {
922 /* all pre-allocated elements are in use or memory exhausted */
923 ret = PTR_ERR(l_new);
924 goto err;
925 }
926
927 /* add new element to the head of the list, so that
928 * concurrent search will find it before old elem
929 */
930 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
931 if (l_old) {
932 hlist_nulls_del_rcu(&l_old->hash_node);
933 if (!htab_is_prealloc(htab))
934 free_htab_elem(htab, l_old);
935 }
936 ret = 0;
937err:
938 raw_spin_unlock_irqrestore(&b->lock, flags);
939 return ret;
940}
941
942static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
943 u64 map_flags)
944{
945 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
946 struct htab_elem *l_new, *l_old = NULL;
947 struct hlist_nulls_head *head;
948 unsigned long flags;
949 struct bucket *b;
950 u32 key_size, hash;
951 int ret;
952
953 if (unlikely(map_flags > BPF_EXIST))
954 /* unknown flags */
955 return -EINVAL;
956
957 WARN_ON_ONCE(!rcu_read_lock_held());
958
959 key_size = map->key_size;
960
961 hash = htab_map_hash(key, key_size, htab->hashrnd);
962
963 b = __select_bucket(htab, hash);
964 head = &b->head;
965
966 /* For LRU, we need to alloc before taking bucket's
967 * spinlock because getting free nodes from LRU may need
968 * to remove older elements from htab and this removal
969 * operation will need a bucket lock.
970 */
971 l_new = prealloc_lru_pop(htab, key, hash);
972 if (!l_new)
973 return -ENOMEM;
974 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
975
976 /* bpf_map_update_elem() can be called in_irq() */
977 raw_spin_lock_irqsave(&b->lock, flags);
978
979 l_old = lookup_elem_raw(head, hash, key, key_size);
980
981 ret = check_flags(htab, l_old, map_flags);
982 if (ret)
983 goto err;
984
985 /* add new element to the head of the list, so that
986 * concurrent search will find it before old elem
987 */
988 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
989 if (l_old) {
990 bpf_lru_node_set_ref(&l_new->lru_node);
991 hlist_nulls_del_rcu(&l_old->hash_node);
992 }
993 ret = 0;
994
995err:
996 raw_spin_unlock_irqrestore(&b->lock, flags);
997
998 if (ret)
999 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1000 else if (l_old)
1001 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
1002
1003 return ret;
1004}
1005
1006static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1007 void *value, u64 map_flags,
1008 bool onallcpus)
1009{
1010 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1011 struct htab_elem *l_new = NULL, *l_old;
1012 struct hlist_nulls_head *head;
1013 unsigned long flags;
1014 struct bucket *b;
1015 u32 key_size, hash;
1016 int ret;
1017
1018 if (unlikely(map_flags > BPF_EXIST))
1019 /* unknown flags */
1020 return -EINVAL;
1021
1022 WARN_ON_ONCE(!rcu_read_lock_held());
1023
1024 key_size = map->key_size;
1025
1026 hash = htab_map_hash(key, key_size, htab->hashrnd);
1027
1028 b = __select_bucket(htab, hash);
1029 head = &b->head;
1030
1031 /* bpf_map_update_elem() can be called in_irq() */
1032 raw_spin_lock_irqsave(&b->lock, flags);
1033
1034 l_old = lookup_elem_raw(head, hash, key, key_size);
1035
1036 ret = check_flags(htab, l_old, map_flags);
1037 if (ret)
1038 goto err;
1039
1040 if (l_old) {
1041 /* per-cpu hash map can update value in-place */
1042 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1043 value, onallcpus);
1044 } else {
1045 l_new = alloc_htab_elem(htab, key, value, key_size,
1046 hash, true, onallcpus, NULL);
1047 if (IS_ERR(l_new)) {
1048 ret = PTR_ERR(l_new);
1049 goto err;
1050 }
1051 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1052 }
1053 ret = 0;
1054err:
1055 raw_spin_unlock_irqrestore(&b->lock, flags);
1056 return ret;
1057}
1058
1059static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1060 void *value, u64 map_flags,
1061 bool onallcpus)
1062{
1063 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1064 struct htab_elem *l_new = NULL, *l_old;
1065 struct hlist_nulls_head *head;
1066 unsigned long flags;
1067 struct bucket *b;
1068 u32 key_size, hash;
1069 int ret;
1070
1071 if (unlikely(map_flags > BPF_EXIST))
1072 /* unknown flags */
1073 return -EINVAL;
1074
1075 WARN_ON_ONCE(!rcu_read_lock_held());
1076
1077 key_size = map->key_size;
1078
1079 hash = htab_map_hash(key, key_size, htab->hashrnd);
1080
1081 b = __select_bucket(htab, hash);
1082 head = &b->head;
1083
1084 /* For LRU, we need to alloc before taking bucket's
1085 * spinlock because LRU's elem alloc may need
1086 * to remove older elem from htab and this removal
1087 * operation will need a bucket lock.
1088 */
1089 if (map_flags != BPF_EXIST) {
1090 l_new = prealloc_lru_pop(htab, key, hash);
1091 if (!l_new)
1092 return -ENOMEM;
1093 }
1094
1095 /* bpf_map_update_elem() can be called in_irq() */
1096 raw_spin_lock_irqsave(&b->lock, flags);
1097
1098 l_old = lookup_elem_raw(head, hash, key, key_size);
1099
1100 ret = check_flags(htab, l_old, map_flags);
1101 if (ret)
1102 goto err;
1103
1104 if (l_old) {
1105 bpf_lru_node_set_ref(&l_old->lru_node);
1106
1107 /* per-cpu hash map can update value in-place */
1108 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1109 value, onallcpus);
1110 } else {
1111 pcpu_init_value(htab, htab_elem_get_ptr(l_new, key_size),
1112 value, onallcpus);
1113 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1114 l_new = NULL;
1115 }
1116 ret = 0;
1117err:
1118 raw_spin_unlock_irqrestore(&b->lock, flags);
1119 if (l_new)
1120 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1121 return ret;
1122}
1123
1124static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1125 void *value, u64 map_flags)
1126{
1127 return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
1128}
1129
1130static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1131 void *value, u64 map_flags)
1132{
1133 return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
1134 false);
1135}
1136
1137/* Called from syscall or from eBPF program */
1138static int htab_map_delete_elem(struct bpf_map *map, void *key)
1139{
1140 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1141 struct hlist_nulls_head *head;
1142 struct bucket *b;
1143 struct htab_elem *l;
1144 unsigned long flags;
1145 u32 hash, key_size;
1146 int ret = -ENOENT;
1147
1148 WARN_ON_ONCE(!rcu_read_lock_held());
1149
1150 key_size = map->key_size;
1151
1152 hash = htab_map_hash(key, key_size, htab->hashrnd);
1153 b = __select_bucket(htab, hash);
1154 head = &b->head;
1155
1156 raw_spin_lock_irqsave(&b->lock, flags);
1157
1158 l = lookup_elem_raw(head, hash, key, key_size);
1159
1160 if (l) {
1161 hlist_nulls_del_rcu(&l->hash_node);
1162 free_htab_elem(htab, l);
1163 ret = 0;
1164 }
1165
1166 raw_spin_unlock_irqrestore(&b->lock, flags);
1167 return ret;
1168}
1169
1170static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1171{
1172 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1173 struct hlist_nulls_head *head;
1174 struct bucket *b;
1175 struct htab_elem *l;
1176 unsigned long flags;
1177 u32 hash, key_size;
1178 int ret = -ENOENT;
1179
1180 WARN_ON_ONCE(!rcu_read_lock_held());
1181
1182 key_size = map->key_size;
1183
1184 hash = htab_map_hash(key, key_size, htab->hashrnd);
1185 b = __select_bucket(htab, hash);
1186 head = &b->head;
1187
1188 raw_spin_lock_irqsave(&b->lock, flags);
1189
1190 l = lookup_elem_raw(head, hash, key, key_size);
1191
1192 if (l) {
1193 hlist_nulls_del_rcu(&l->hash_node);
1194 ret = 0;
1195 }
1196
1197 raw_spin_unlock_irqrestore(&b->lock, flags);
1198 if (l)
1199 bpf_lru_push_free(&htab->lru, &l->lru_node);
1200 return ret;
1201}
1202
1203static void delete_all_elements(struct bpf_htab *htab)
1204{
1205 int i;
1206
1207 for (i = 0; i < htab->n_buckets; i++) {
1208 struct hlist_nulls_head *head = select_bucket(htab, i);
1209 struct hlist_nulls_node *n;
1210 struct htab_elem *l;
1211
1212 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1213 hlist_nulls_del_rcu(&l->hash_node);
1214 htab_elem_free(htab, l);
1215 }
1216 }
1217}
1218
1219/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1220static void htab_map_free(struct bpf_map *map)
1221{
1222 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1223
1224 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1225 * so the programs (can be more than one that used this map) were
1226 * disconnected from events. Wait for outstanding critical sections in
1227 * these programs to complete
1228 */
1229 synchronize_rcu();
1230
1231 /* some of free_htab_elem() callbacks for elements of this map may
1232 * not have executed. Wait for them.
1233 */
1234 rcu_barrier();
1235 if (!htab_is_prealloc(htab))
1236 delete_all_elements(htab);
1237 else
1238 prealloc_destroy(htab);
1239
1240 free_percpu(htab->extra_elems);
1241 bpf_map_area_free(htab->buckets);
1242 kfree(htab);
1243}
1244
1245static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1246 struct seq_file *m)
1247{
1248 void *value;
1249
1250 rcu_read_lock();
1251
1252 value = htab_map_lookup_elem(map, key);
1253 if (!value) {
1254 rcu_read_unlock();
1255 return;
1256 }
1257
1258 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1259 seq_puts(m, ": ");
1260 btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1261 seq_puts(m, "\n");
1262
1263 rcu_read_unlock();
1264}
1265
1266const struct bpf_map_ops htab_map_ops = {
1267 .map_alloc_check = htab_map_alloc_check,
1268 .map_alloc = htab_map_alloc,
1269 .map_free = htab_map_free,
1270 .map_get_next_key = htab_map_get_next_key,
1271 .map_lookup_elem = htab_map_lookup_elem,
1272 .map_update_elem = htab_map_update_elem,
1273 .map_delete_elem = htab_map_delete_elem,
1274 .map_gen_lookup = htab_map_gen_lookup,
1275 .map_seq_show_elem = htab_map_seq_show_elem,
1276};
1277
1278const struct bpf_map_ops htab_lru_map_ops = {
1279 .map_alloc_check = htab_map_alloc_check,
1280 .map_alloc = htab_map_alloc,
1281 .map_free = htab_map_free,
1282 .map_get_next_key = htab_map_get_next_key,
1283 .map_lookup_elem = htab_lru_map_lookup_elem,
1284 .map_lookup_elem_sys_only = htab_lru_map_lookup_elem_sys,
1285 .map_update_elem = htab_lru_map_update_elem,
1286 .map_delete_elem = htab_lru_map_delete_elem,
1287 .map_gen_lookup = htab_lru_map_gen_lookup,
1288 .map_seq_show_elem = htab_map_seq_show_elem,
1289};
1290
1291/* Called from eBPF program */
1292static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1293{
1294 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1295
1296 if (l)
1297 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1298 else
1299 return NULL;
1300}
1301
1302static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1303{
1304 struct htab_elem *l = __htab_map_lookup_elem(map, key);
1305
1306 if (l) {
1307 bpf_lru_node_set_ref(&l->lru_node);
1308 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1309 }
1310
1311 return NULL;
1312}
1313
1314int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1315{
1316 struct htab_elem *l;
1317 void __percpu *pptr;
1318 int ret = -ENOENT;
1319 int cpu, off = 0;
1320 u32 size;
1321
1322 /* per_cpu areas are zero-filled and bpf programs can only
1323 * access 'value_size' of them, so copying rounded areas
1324 * will not leak any kernel data
1325 */
1326 size = round_up(map->value_size, 8);
1327 rcu_read_lock();
1328 l = __htab_map_lookup_elem(map, key);
1329 if (!l)
1330 goto out;
1331 /* We do not mark LRU map element here in order to not mess up
1332 * eviction heuristics when user space does a map walk.
1333 */
1334 pptr = htab_elem_get_ptr(l, map->key_size);
1335 for_each_possible_cpu(cpu) {
1336 bpf_long_memcpy(value + off,
1337 per_cpu_ptr(pptr, cpu), size);
1338 off += size;
1339 }
1340 ret = 0;
1341out:
1342 rcu_read_unlock();
1343 return ret;
1344}
1345
1346int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1347 u64 map_flags)
1348{
1349 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1350 int ret;
1351
1352 rcu_read_lock();
1353 if (htab_is_lru(htab))
1354 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1355 map_flags, true);
1356 else
1357 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1358 true);
1359 rcu_read_unlock();
1360
1361 return ret;
1362}
1363
1364static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
1365 struct seq_file *m)
1366{
1367 struct htab_elem *l;
1368 void __percpu *pptr;
1369 int cpu;
1370
1371 rcu_read_lock();
1372
1373 l = __htab_map_lookup_elem(map, key);
1374 if (!l) {
1375 rcu_read_unlock();
1376 return;
1377 }
1378
1379 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1380 seq_puts(m, ": {\n");
1381 pptr = htab_elem_get_ptr(l, map->key_size);
1382 for_each_possible_cpu(cpu) {
1383 seq_printf(m, "\tcpu%d: ", cpu);
1384 btf_type_seq_show(map->btf, map->btf_value_type_id,
1385 per_cpu_ptr(pptr, cpu), m);
1386 seq_puts(m, "\n");
1387 }
1388 seq_puts(m, "}\n");
1389
1390 rcu_read_unlock();
1391}
1392
1393const struct bpf_map_ops htab_percpu_map_ops = {
1394 .map_alloc_check = htab_map_alloc_check,
1395 .map_alloc = htab_map_alloc,
1396 .map_free = htab_map_free,
1397 .map_get_next_key = htab_map_get_next_key,
1398 .map_lookup_elem = htab_percpu_map_lookup_elem,
1399 .map_update_elem = htab_percpu_map_update_elem,
1400 .map_delete_elem = htab_map_delete_elem,
1401 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
1402};
1403
1404const struct bpf_map_ops htab_lru_percpu_map_ops = {
1405 .map_alloc_check = htab_map_alloc_check,
1406 .map_alloc = htab_map_alloc,
1407 .map_free = htab_map_free,
1408 .map_get_next_key = htab_map_get_next_key,
1409 .map_lookup_elem = htab_lru_percpu_map_lookup_elem,
1410 .map_update_elem = htab_lru_percpu_map_update_elem,
1411 .map_delete_elem = htab_lru_map_delete_elem,
1412 .map_seq_show_elem = htab_percpu_map_seq_show_elem,
1413};
1414
1415static int fd_htab_map_alloc_check(union bpf_attr *attr)
1416{
1417 if (attr->value_size != sizeof(u32))
1418 return -EINVAL;
1419 return htab_map_alloc_check(attr);
1420}
1421
1422static void fd_htab_map_free(struct bpf_map *map)
1423{
1424 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1425 struct hlist_nulls_node *n;
1426 struct hlist_nulls_head *head;
1427 struct htab_elem *l;
1428 int i;
1429
1430 for (i = 0; i < htab->n_buckets; i++) {
1431 head = select_bucket(htab, i);
1432
1433 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1434 void *ptr = fd_htab_map_get_ptr(map, l);
1435
1436 map->ops->map_fd_put_ptr(map, ptr, false);
1437 }
1438 }
1439
1440 htab_map_free(map);
1441}
1442
1443/* only called from syscall */
1444int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
1445{
1446 void **ptr;
1447 int ret = 0;
1448
1449 if (!map->ops->map_fd_sys_lookup_elem)
1450 return -ENOTSUPP;
1451
1452 rcu_read_lock();
1453 ptr = htab_map_lookup_elem(map, key);
1454 if (ptr)
1455 *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
1456 else
1457 ret = -ENOENT;
1458 rcu_read_unlock();
1459
1460 return ret;
1461}
1462
1463/* only called from syscall */
1464int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
1465 void *key, void *value, u64 map_flags)
1466{
1467 void *ptr;
1468 int ret;
1469 u32 ufd = *(u32 *)value;
1470
1471 ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
1472 if (IS_ERR(ptr))
1473 return PTR_ERR(ptr);
1474
1475 ret = htab_map_update_elem(map, key, &ptr, map_flags);
1476 if (ret)
1477 map->ops->map_fd_put_ptr(map, ptr, false);
1478
1479 return ret;
1480}
1481
1482static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
1483{
1484 struct bpf_map *map, *inner_map_meta;
1485
1486 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1487 if (IS_ERR(inner_map_meta))
1488 return inner_map_meta;
1489
1490 map = htab_map_alloc(attr);
1491 if (IS_ERR(map)) {
1492 bpf_map_meta_free(inner_map_meta);
1493 return map;
1494 }
1495
1496 map->inner_map_meta = inner_map_meta;
1497
1498 return map;
1499}
1500
1501static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
1502{
1503 struct bpf_map **inner_map = htab_map_lookup_elem(map, key);
1504
1505 if (!inner_map)
1506 return NULL;
1507
1508 return READ_ONCE(*inner_map);
1509}
1510
1511static u32 htab_of_map_gen_lookup(struct bpf_map *map,
1512 struct bpf_insn *insn_buf)
1513{
1514 struct bpf_insn *insn = insn_buf;
1515 const int ret = BPF_REG_0;
1516
1517 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
1518 (void *(*)(struct bpf_map *map, void *key))NULL));
1519 *insn++ = BPF_EMIT_CALL(BPF_CAST_CALL(__htab_map_lookup_elem));
1520 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
1521 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
1522 offsetof(struct htab_elem, key) +
1523 round_up(map->key_size, 8));
1524 *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
1525
1526 return insn - insn_buf;
1527}
1528
1529static void htab_of_map_free(struct bpf_map *map)
1530{
1531 bpf_map_meta_free(map->inner_map_meta);
1532 fd_htab_map_free(map);
1533}
1534
1535const struct bpf_map_ops htab_of_maps_map_ops = {
1536 .map_alloc_check = fd_htab_map_alloc_check,
1537 .map_alloc = htab_of_map_alloc,
1538 .map_free = htab_of_map_free,
1539 .map_get_next_key = htab_map_get_next_key,
1540 .map_lookup_elem = htab_of_map_lookup_elem,
1541 .map_delete_elem = htab_map_delete_elem,
1542 .map_fd_get_ptr = bpf_map_fd_get_ptr,
1543 .map_fd_put_ptr = bpf_map_fd_put_ptr,
1544 .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
1545 .map_gen_lookup = htab_of_map_gen_lookup,
1546 .map_check_btf = map_check_no_btf,
1547};