1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3 * Copyright (c) 2016 Facebook
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)
21 struct hlist_nulls_head head
;
27 struct bucket
*buckets
;
30 struct pcpu_freelist freelist
;
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 */
40 /* each htab element is struct htab_elem + key + value */
43 struct hlist_nulls_node hash_node
;
47 struct bpf_htab
*htab
;
48 struct pcpu_freelist_node fnode
;
54 struct bpf_lru_node lru_node
;
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
,
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
)
104 if (!htab_is_percpu(htab
))
107 for (i
= 0; i
< htab
->map
.max_entries
; i
++) {
110 pptr
= htab_elem_get_ptr(get_htab_elem(htab
, i
),
116 bpf_map_area_free(htab
->elems
);
119 static struct htab_elem
*prealloc_lru_pop(struct bpf_htab
*htab
, void *key
,
122 struct bpf_lru_node
*node
= bpf_lru_pop_free(&htab
->lru
, hash
);
126 l
= container_of(node
, struct htab_elem
, lru_node
);
127 memcpy(l
->key
, key
, htab
->map
.key_size
);
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
);
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);
154 pptr
= __alloc_percpu_gfp(size
, 8, GFP_USER
| __GFP_NOWARN
);
157 htab_elem_set_ptr(get_htab_elem(htab
, i
), htab
->map
.key_size
,
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
,
171 err
= pcpu_freelist_init(&htab
->freelist
);
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
);
181 pcpu_freelist_populate(&htab
->freelist
,
182 htab
->elems
+ offsetof(struct htab_elem
, fnode
),
183 htab
->elem_size
, num_entries
);
188 htab_free_elems(htab
);
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
);
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
;
208 pptr
= __alloc_percpu_gfp(sizeof(struct htab_elem
*), 8,
209 GFP_USER
| __GFP_NOWARN
);
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
;
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.
253 if (zero_seed
&& !capable(CAP_SYS_ADMIN
))
254 /* Guard against local DoS, and discourage production use. */
257 if (attr
->map_flags
& ~HTAB_CREATE_FLAG_MASK
||
258 !bpf_map_flags_access_ok(attr
->map_flags
))
261 if (!lru
&& percpu_lru
)
264 if (lru
&& !prealloc
)
267 if (numa_node
!= NUMA_NO_NODE
&& (percpu
|| percpu_lru
))
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)
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
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()
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
;
312 htab
= kzalloc(sizeof(*htab
), GFP_USER
);
314 return ERR_PTR(-ENOMEM
);
316 bpf_map_init_from_attr(&htab
->map
, attr
);
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);
336 htab
->elem_size
+= sizeof(void *);
338 htab
->elem_size
+= round_up(htab
->map
.value_size
, 8);
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
))
346 cost
= (u64
) htab
->n_buckets
* sizeof(struct bucket
) +
347 (u64
) htab
->elem_size
* htab
->map
.max_entries
;
350 cost
+= (u64
) round_up(htab
->map
.value_size
, 8) *
351 num_possible_cpus() * htab
->map
.max_entries
;
353 cost
+= (u64
) htab
->elem_size
* num_possible_cpus();
355 if (cost
>= U32_MAX
- PAGE_SIZE
)
356 /* make sure page count doesn't overflow */
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
);
367 htab
->buckets
= bpf_map_area_alloc(htab
->n_buckets
*
368 sizeof(struct bucket
),
369 htab
->map
.numa_node
);
373 if (htab
->map
.map_flags
& BPF_F_ZERO_SEED
)
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
);
384 err
= prealloc_init(htab
);
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
);
401 prealloc_destroy(htab
);
403 bpf_map_area_free(htab
->buckets
);
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
;
431 hlist_nulls_for_each_entry_rcu(l
, n
, head
, hash_node
)
432 if (l
->hash
== hash
&& !memcmp(&l
->key
, key
, key_size
))
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
,
444 u32 key_size
, u32 n_buckets
)
446 struct hlist_nulls_node
*n
;
450 hlist_nulls_for_each_entry_rcu(l
, n
, head
, hash_node
)
451 if (l
->hash
== hash
&& !memcmp(&l
->key
, key
, key_size
))
454 if (unlikely(get_nulls_value(n
) != (hash
& (n_buckets
- 1))))
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
;
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
);
486 static void *htab_map_lookup_elem(struct bpf_map
*map
, void *key
)
488 struct htab_elem
*l
= __htab_map_lookup_elem(map
, key
);
491 return l
->key
+ round_up(map
->key_size
, 8);
496 /* inline bpf_map_lookup_elem() call.
499 * bpf_map_lookup_elem
500 * map->ops->map_lookup_elem
501 * htab_map_lookup_elem
502 * __htab_map_lookup_elem
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
);
529 bpf_lru_node_set_ref(&l
->lru_node
);
530 return l
->key
+ round_up(map
->key_size
, 8);
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
;
583 tgt_l
= container_of(node
, struct htab_elem
, lru_node
);
584 b
= __select_bucket(htab
, tgt_l
->hash
);
587 raw_spin_lock_irqsave(&b
->lock
, flags
);
589 hlist_nulls_for_each_entry_rcu(l
, n
, head
, hash_node
)
591 hlist_nulls_del_rcu(&l
->hash_node
);
595 raw_spin_unlock_irqrestore(&b
->lock
, flags
);
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
;
609 WARN_ON_ONCE(!rcu_read_lock_held());
611 key_size
= map
->key_size
;
614 goto find_first_elem
;
616 hash
= htab_map_hash(key
, key_size
, htab
->hashrnd
);
618 head
= select_bucket(htab
, hash
);
621 l
= lookup_nulls_elem_raw(head
, hash
, key
, key_size
, htab
->n_buckets
);
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
);
631 /* if next elem in this hash list is non-zero, just return it */
632 memcpy(next_key
, next_l
->key
, key_size
);
636 /* no more elements in this hash list, go to the next bucket */
637 i
= hash
& (htab
->n_buckets
- 1);
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
);
649 /* if it's not empty, just return it */
650 memcpy(next_key
, next_l
->key
, key_size
);
655 /* iterated over all buckets and all elements */
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
));
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
676 __this_cpu_inc(bpf_prog_active
);
677 htab_elem_free(htab
, l
);
678 __this_cpu_dec(bpf_prog_active
);
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
);
695 atomic_dec(&htab
->count
);
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
)
705 /* copy true value_size bytes */
706 memcpy(this_cpu_ptr(pptr
), value
, htab
->map
.value_size
);
708 u32 size
= round_up(htab
->map
.value_size
, 8);
711 for_each_possible_cpu(cpu
) {
712 bpf_long_memcpy(per_cpu_ptr(pptr
, cpu
),
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
&&
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
;
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
);
744 struct pcpu_freelist_node
*l
;
746 l
= __pcpu_freelist_pop(&htab
->freelist
);
748 return ERR_PTR(-E2BIG
);
749 l_new
= container_of(l
, struct htab_elem
, fnode
);
752 if (atomic_inc_return(&htab
->count
) > htab
->map
.max_entries
)
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
);
762 l_new
= kmalloc_node(htab
->elem_size
, GFP_ATOMIC
| __GFP_NOWARN
,
763 htab
->map
.numa_node
);
765 l_new
= ERR_PTR(-ENOMEM
);
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
);
774 size
= round_up(size
, 8);
776 pptr
= htab_elem_get_ptr(l_new
, key_size
);
778 /* alloc_percpu zero-fills */
779 pptr
= __alloc_percpu_gfp(size
, 8,
780 GFP_ATOMIC
| __GFP_NOWARN
);
783 l_new
= ERR_PTR(-ENOMEM
);
788 pcpu_copy_value(htab
, pptr
, value
, onallcpus
);
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
);
796 copy_map_value(&htab
->map
,
797 l_new
->key
+ round_up(key_size
, 8),
804 atomic_dec(&htab
->count
);
808 static int check_flags(struct bpf_htab
*htab
, struct htab_elem
*l_old
,
811 if (l_old
&& (map_flags
& ~BPF_F_LOCK
) == BPF_NOEXIST
)
812 /* elem already exists */
815 if (!l_old
&& (map_flags
& ~BPF_F_LOCK
) == BPF_EXIST
)
816 /* elem doesn't exist, cannot update it */
822 /* Called from syscall or from eBPF program */
823 static int htab_map_update_elem(struct bpf_map
*map
, void *key
, void *value
,
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
;
834 if (unlikely((map_flags
& ~BPF_F_LOCK
) > BPF_EXIST
))
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
);
847 if (unlikely(map_flags
& BPF_F_LOCK
)) {
848 if (unlikely(!map_value_has_spin_lock(map
)))
850 /* find an element without taking the bucket lock */
851 l_old
= lookup_nulls_elem_raw(head
, hash
, key
, key_size
,
853 ret
= check_flags(htab
, l_old
, map_flags
);
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),
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
);
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),
892 l_new
= alloc_htab_elem(htab
, key
, value
, key_size
, hash
, false, false,
895 /* all pre-allocated elements are in use or memory exhausted */
896 ret
= PTR_ERR(l_new
);
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
);
905 hlist_nulls_del_rcu(&l_old
->hash_node
);
906 if (!htab_is_prealloc(htab
))
907 free_htab_elem(htab
, l_old
);
911 raw_spin_unlock_irqrestore(&b
->lock
, flags
);
915 static int htab_lru_map_update_elem(struct bpf_map
*map
, void *key
, void *value
,
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
;
926 if (unlikely(map_flags
> BPF_EXIST
))
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
);
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
);
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
);
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
);
963 bpf_lru_node_set_ref(&l_new
->lru_node
);
964 hlist_nulls_del_rcu(&l_old
->hash_node
);
969 raw_spin_unlock_irqrestore(&b
->lock
, flags
);
972 bpf_lru_push_free(&htab
->lru
, &l_new
->lru_node
);
974 bpf_lru_push_free(&htab
->lru
, &l_old
->lru_node
);
979 static int __htab_percpu_map_update_elem(struct bpf_map
*map
, void *key
,
980 void *value
, u64 map_flags
,
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
;
991 if (unlikely(map_flags
> BPF_EXIST
))
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
);
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
);
1014 /* per-cpu hash map can update value in-place */
1015 pcpu_copy_value(htab
, htab_elem_get_ptr(l_old
, key_size
),
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
);
1024 hlist_nulls_add_head_rcu(&l_new
->hash_node
, head
);
1028 raw_spin_unlock_irqrestore(&b
->lock
, flags
);
1032 static int __htab_lru_percpu_map_update_elem(struct bpf_map
*map
, void *key
,
1033 void *value
, u64 map_flags
,
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
;
1044 if (unlikely(map_flags
> BPF_EXIST
))
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
);
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
);
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
);
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
),
1084 pcpu_copy_value(htab
, htab_elem_get_ptr(l_new
, key_size
),
1086 hlist_nulls_add_head_rcu(&l_new
->hash_node
, head
);
1091 raw_spin_unlock_irqrestore(&b
->lock
, flags
);
1093 bpf_lru_push_free(&htab
->lru
, &l_new
->lru_node
);
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
,
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
;
1116 struct htab_elem
*l
;
1117 unsigned long flags
;
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
);
1129 raw_spin_lock_irqsave(&b
->lock
, flags
);
1131 l
= lookup_elem_raw(head
, hash
, key
, key_size
);
1134 hlist_nulls_del_rcu(&l
->hash_node
);
1135 free_htab_elem(htab
, l
);
1139 raw_spin_unlock_irqrestore(&b
->lock
, flags
);
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
;
1148 struct htab_elem
*l
;
1149 unsigned long flags
;
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
);
1161 raw_spin_lock_irqsave(&b
->lock
, flags
);
1163 l
= lookup_elem_raw(head
, hash
, key
, key_size
);
1166 hlist_nulls_del_rcu(&l
->hash_node
);
1170 raw_spin_unlock_irqrestore(&b
->lock
, flags
);
1172 bpf_lru_push_free(&htab
->lru
, &l
->lru_node
);
1176 static void delete_all_elements(struct bpf_htab
*htab
)
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
1204 /* some of free_htab_elem() callbacks for elements of this map may
1205 * not have executed. Wait for them.
1208 if (!htab_is_prealloc(htab
))
1209 delete_all_elements(htab
);
1211 prealloc_destroy(htab
);
1213 free_percpu(htab
->extra_elems
);
1214 bpf_map_area_free(htab
->buckets
);
1218 static void htab_map_seq_show_elem(struct bpf_map
*map
, void *key
,
1225 value
= htab_map_lookup_elem(map
, key
);
1231 btf_type_seq_show(map
->btf
, map
->btf_key_type_id
, key
, m
);
1233 btf_type_seq_show(map
->btf
, map
->btf_value_type_id
, value
, m
);
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
);
1270 return this_cpu_ptr(htab_elem_get_ptr(l
, map
->key_size
));
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
);
1280 bpf_lru_node_set_ref(&l
->lru_node
);
1281 return this_cpu_ptr(htab_elem_get_ptr(l
, map
->key_size
));
1287 int bpf_percpu_hash_copy(struct bpf_map
*map
, void *key
, void *value
)
1289 struct htab_elem
*l
;
1290 void __percpu
*pptr
;
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);
1301 l
= __htab_map_lookup_elem(map
, key
);
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
);
1319 int bpf_percpu_hash_update(struct bpf_map
*map
, void *key
, void *value
,
1322 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
1326 if (htab_is_lru(htab
))
1327 ret
= __htab_lru_percpu_map_update_elem(map
, key
, value
,
1330 ret
= __htab_percpu_map_update_elem(map
, key
, value
, map_flags
,
1337 static void htab_percpu_map_seq_show_elem(struct bpf_map
*map
, void *key
,
1340 struct htab_elem
*l
;
1341 void __percpu
*pptr
;
1346 l
= __htab_map_lookup_elem(map
, key
);
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
);
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
))
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
;
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
);
1416 /* only called from syscall */
1417 int bpf_fd_htab_map_lookup_elem(struct bpf_map
*map
, void *key
, u32
*value
)
1422 if (!map
->ops
->map_fd_sys_lookup_elem
)
1426 ptr
= htab_map_lookup_elem(map
, key
);
1428 *value
= map
->ops
->map_fd_sys_lookup_elem(READ_ONCE(*ptr
));
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
)
1442 u32 ufd
= *(u32
*)value
;
1444 ptr
= map
->ops
->map_fd_get_ptr(map
, map_file
, ufd
);
1446 return PTR_ERR(ptr
);
1448 ret
= htab_map_update_elem(map
, key
, &ptr
, map_flags
);
1450 map
->ops
->map_fd_put_ptr(ptr
);
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
);
1465 bpf_map_meta_free(inner_map_meta
);
1469 map
->inner_map_meta
= inner_map_meta
;
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
);
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
,