1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 * Copyright (c) 2016 Facebook
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 #include <linux/bpf.h>
14 #include <linux/jhash.h>
15 #include <linux/filter.h>
16 #include <linux/rculist_nulls.h>
17 #include "percpu_freelist.h"
18 #include "bpf_lru_list.h"
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 */
39 /* each htab element is struct htab_elem + key + value */
42 struct hlist_nulls_node hash_node
;
46 struct bpf_htab
*htab
;
47 struct pcpu_freelist_node fnode
;
53 struct bpf_lru_node lru_node
;
56 char key
[0] __aligned(8);
59 static bool htab_lru_map_delete_node(void *arg
, struct bpf_lru_node
*node
);
61 static bool htab_is_lru(const struct bpf_htab
*htab
)
63 return htab
->map
.map_type
== BPF_MAP_TYPE_LRU_HASH
||
64 htab
->map
.map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
;
67 static bool htab_is_percpu(const struct bpf_htab
*htab
)
69 return htab
->map
.map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
70 htab
->map
.map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
;
73 static bool htab_is_prealloc(const struct bpf_htab
*htab
)
75 return !(htab
->map
.map_flags
& BPF_F_NO_PREALLOC
);
78 static inline void htab_elem_set_ptr(struct htab_elem
*l
, u32 key_size
,
81 *(void __percpu
**)(l
->key
+ key_size
) = pptr
;
84 static inline void __percpu
*htab_elem_get_ptr(struct htab_elem
*l
, u32 key_size
)
86 return *(void __percpu
**)(l
->key
+ key_size
);
89 static struct htab_elem
*get_htab_elem(struct bpf_htab
*htab
, int i
)
91 return (struct htab_elem
*) (htab
->elems
+ i
* htab
->elem_size
);
94 static void htab_free_elems(struct bpf_htab
*htab
)
98 if (!htab_is_percpu(htab
))
101 for (i
= 0; i
< htab
->map
.max_entries
; i
++) {
104 pptr
= htab_elem_get_ptr(get_htab_elem(htab
, i
),
109 bpf_map_area_free(htab
->elems
);
112 static struct htab_elem
*prealloc_lru_pop(struct bpf_htab
*htab
, void *key
,
115 struct bpf_lru_node
*node
= bpf_lru_pop_free(&htab
->lru
, hash
);
119 l
= container_of(node
, struct htab_elem
, lru_node
);
120 memcpy(l
->key
, key
, htab
->map
.key_size
);
127 static int prealloc_init(struct bpf_htab
*htab
)
129 u32 num_entries
= htab
->map
.max_entries
;
130 int err
= -ENOMEM
, i
;
132 if (!htab_is_percpu(htab
) && !htab_is_lru(htab
))
133 num_entries
+= num_possible_cpus();
135 htab
->elems
= bpf_map_area_alloc(htab
->elem_size
* num_entries
);
139 if (!htab_is_percpu(htab
))
140 goto skip_percpu_elems
;
142 for (i
= 0; i
< num_entries
; i
++) {
143 u32 size
= round_up(htab
->map
.value_size
, 8);
146 pptr
= __alloc_percpu_gfp(size
, 8, GFP_USER
| __GFP_NOWARN
);
149 htab_elem_set_ptr(get_htab_elem(htab
, i
), htab
->map
.key_size
,
154 if (htab_is_lru(htab
))
155 err
= bpf_lru_init(&htab
->lru
,
156 htab
->map
.map_flags
& BPF_F_NO_COMMON_LRU
,
157 offsetof(struct htab_elem
, hash
) -
158 offsetof(struct htab_elem
, lru_node
),
159 htab_lru_map_delete_node
,
162 err
= pcpu_freelist_init(&htab
->freelist
);
167 if (htab_is_lru(htab
))
168 bpf_lru_populate(&htab
->lru
, htab
->elems
,
169 offsetof(struct htab_elem
, lru_node
),
170 htab
->elem_size
, num_entries
);
172 pcpu_freelist_populate(&htab
->freelist
,
173 htab
->elems
+ offsetof(struct htab_elem
, fnode
),
174 htab
->elem_size
, num_entries
);
179 htab_free_elems(htab
);
183 static void prealloc_destroy(struct bpf_htab
*htab
)
185 htab_free_elems(htab
);
187 if (htab_is_lru(htab
))
188 bpf_lru_destroy(&htab
->lru
);
190 pcpu_freelist_destroy(&htab
->freelist
);
193 static int alloc_extra_elems(struct bpf_htab
*htab
)
195 struct htab_elem
*__percpu
*pptr
, *l_new
;
196 struct pcpu_freelist_node
*l
;
199 pptr
= __alloc_percpu_gfp(sizeof(struct htab_elem
*), 8,
200 GFP_USER
| __GFP_NOWARN
);
204 for_each_possible_cpu(cpu
) {
205 l
= pcpu_freelist_pop(&htab
->freelist
);
206 /* pop will succeed, since prealloc_init()
207 * preallocated extra num_possible_cpus elements
209 l_new
= container_of(l
, struct htab_elem
, fnode
);
210 *per_cpu_ptr(pptr
, cpu
) = l_new
;
212 htab
->extra_elems
= pptr
;
216 /* Called from syscall */
217 static struct bpf_map
*htab_map_alloc(union bpf_attr
*attr
)
219 bool percpu
= (attr
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
220 attr
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
);
221 bool lru
= (attr
->map_type
== BPF_MAP_TYPE_LRU_HASH
||
222 attr
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
);
223 /* percpu_lru means each cpu has its own LRU list.
224 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
225 * the map's value itself is percpu. percpu_lru has
226 * nothing to do with the map's value.
228 bool percpu_lru
= (attr
->map_flags
& BPF_F_NO_COMMON_LRU
);
229 bool prealloc
= !(attr
->map_flags
& BPF_F_NO_PREALLOC
);
230 struct bpf_htab
*htab
;
234 BUILD_BUG_ON(offsetof(struct htab_elem
, htab
) !=
235 offsetof(struct htab_elem
, hash_node
.pprev
));
236 BUILD_BUG_ON(offsetof(struct htab_elem
, fnode
.next
) !=
237 offsetof(struct htab_elem
, hash_node
.pprev
));
239 if (lru
&& !capable(CAP_SYS_ADMIN
))
240 /* LRU implementation is much complicated than other
241 * maps. Hence, limit to CAP_SYS_ADMIN for now.
243 return ERR_PTR(-EPERM
);
245 if (attr
->map_flags
& ~(BPF_F_NO_PREALLOC
| BPF_F_NO_COMMON_LRU
))
246 /* reserved bits should not be used */
247 return ERR_PTR(-EINVAL
);
249 if (!lru
&& percpu_lru
)
250 return ERR_PTR(-EINVAL
);
252 if (lru
&& !prealloc
)
253 return ERR_PTR(-ENOTSUPP
);
255 htab
= kzalloc(sizeof(*htab
), GFP_USER
);
257 return ERR_PTR(-ENOMEM
);
259 /* mandatory map attributes */
260 htab
->map
.map_type
= attr
->map_type
;
261 htab
->map
.key_size
= attr
->key_size
;
262 htab
->map
.value_size
= attr
->value_size
;
263 htab
->map
.max_entries
= attr
->max_entries
;
264 htab
->map
.map_flags
= attr
->map_flags
;
266 /* check sanity of attributes.
267 * value_size == 0 may be allowed in the future to use map as a set
270 if (htab
->map
.max_entries
== 0 || htab
->map
.key_size
== 0 ||
271 htab
->map
.value_size
== 0)
275 /* ensure each CPU's lru list has >=1 elements.
276 * since we are at it, make each lru list has the same
277 * number of elements.
279 htab
->map
.max_entries
= roundup(attr
->max_entries
,
280 num_possible_cpus());
281 if (htab
->map
.max_entries
< attr
->max_entries
)
282 htab
->map
.max_entries
= rounddown(attr
->max_entries
,
283 num_possible_cpus());
286 /* hash table size must be power of 2 */
287 htab
->n_buckets
= roundup_pow_of_two(htab
->map
.max_entries
);
290 if (htab
->map
.key_size
> MAX_BPF_STACK
)
291 /* eBPF programs initialize keys on stack, so they cannot be
292 * larger than max stack size
296 if (htab
->map
.value_size
>= KMALLOC_MAX_SIZE
-
297 MAX_BPF_STACK
- sizeof(struct htab_elem
))
298 /* if value_size is bigger, the user space won't be able to
299 * access the elements via bpf syscall. This check also makes
300 * sure that the elem_size doesn't overflow and it's
301 * kmalloc-able later in htab_map_update_elem()
305 if (percpu
&& round_up(htab
->map
.value_size
, 8) > PCPU_MIN_UNIT_SIZE
)
306 /* make sure the size for pcpu_alloc() is reasonable */
309 htab
->elem_size
= sizeof(struct htab_elem
) +
310 round_up(htab
->map
.key_size
, 8);
312 htab
->elem_size
+= sizeof(void *);
314 htab
->elem_size
+= round_up(htab
->map
.value_size
, 8);
316 /* prevent zero size kmalloc and check for u32 overflow */
317 if (htab
->n_buckets
== 0 ||
318 htab
->n_buckets
> U32_MAX
/ sizeof(struct bucket
))
321 cost
= (u64
) htab
->n_buckets
* sizeof(struct bucket
) +
322 (u64
) htab
->elem_size
* htab
->map
.max_entries
;
325 cost
+= (u64
) round_up(htab
->map
.value_size
, 8) *
326 num_possible_cpus() * htab
->map
.max_entries
;
328 cost
+= (u64
) htab
->elem_size
* num_possible_cpus();
330 if (cost
>= U32_MAX
- PAGE_SIZE
)
331 /* make sure page count doesn't overflow */
334 htab
->map
.pages
= round_up(cost
, PAGE_SIZE
) >> PAGE_SHIFT
;
336 /* if map size is larger than memlock limit, reject it early */
337 err
= bpf_map_precharge_memlock(htab
->map
.pages
);
342 htab
->buckets
= bpf_map_area_alloc(htab
->n_buckets
*
343 sizeof(struct bucket
));
347 for (i
= 0; i
< htab
->n_buckets
; i
++) {
348 INIT_HLIST_NULLS_HEAD(&htab
->buckets
[i
].head
, i
);
349 raw_spin_lock_init(&htab
->buckets
[i
].lock
);
353 err
= prealloc_init(htab
);
357 if (!percpu
&& !lru
) {
358 /* lru itself can remove the least used element, so
359 * there is no need for an extra elem during map_update.
361 err
= alloc_extra_elems(htab
);
370 prealloc_destroy(htab
);
372 bpf_map_area_free(htab
->buckets
);
378 static inline u32
htab_map_hash(const void *key
, u32 key_len
)
380 return jhash(key
, key_len
, 0);
383 static inline struct bucket
*__select_bucket(struct bpf_htab
*htab
, u32 hash
)
385 return &htab
->buckets
[hash
& (htab
->n_buckets
- 1)];
388 static inline struct hlist_nulls_head
*select_bucket(struct bpf_htab
*htab
, u32 hash
)
390 return &__select_bucket(htab
, hash
)->head
;
393 /* this lookup function can only be called with bucket lock taken */
394 static struct htab_elem
*lookup_elem_raw(struct hlist_nulls_head
*head
, u32 hash
,
395 void *key
, u32 key_size
)
397 struct hlist_nulls_node
*n
;
400 hlist_nulls_for_each_entry_rcu(l
, n
, head
, hash_node
)
401 if (l
->hash
== hash
&& !memcmp(&l
->key
, key
, key_size
))
407 /* can be called without bucket lock. it will repeat the loop in
408 * the unlikely event when elements moved from one bucket into another
409 * while link list is being walked
411 static struct htab_elem
*lookup_nulls_elem_raw(struct hlist_nulls_head
*head
,
413 u32 key_size
, u32 n_buckets
)
415 struct hlist_nulls_node
*n
;
419 hlist_nulls_for_each_entry_rcu(l
, n
, head
, hash_node
)
420 if (l
->hash
== hash
&& !memcmp(&l
->key
, key
, key_size
))
423 if (unlikely(get_nulls_value(n
) != (hash
& (n_buckets
- 1))))
429 /* Called from syscall or from eBPF program */
430 static void *__htab_map_lookup_elem(struct bpf_map
*map
, void *key
)
432 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
433 struct hlist_nulls_head
*head
;
437 /* Must be called with rcu_read_lock. */
438 WARN_ON_ONCE(!rcu_read_lock_held());
440 key_size
= map
->key_size
;
442 hash
= htab_map_hash(key
, key_size
);
444 head
= select_bucket(htab
, hash
);
446 l
= lookup_nulls_elem_raw(head
, hash
, key
, key_size
, htab
->n_buckets
);
451 static void *htab_map_lookup_elem(struct bpf_map
*map
, void *key
)
453 struct htab_elem
*l
= __htab_map_lookup_elem(map
, key
);
456 return l
->key
+ round_up(map
->key_size
, 8);
461 static void *htab_lru_map_lookup_elem(struct bpf_map
*map
, void *key
)
463 struct htab_elem
*l
= __htab_map_lookup_elem(map
, key
);
466 bpf_lru_node_set_ref(&l
->lru_node
);
467 return l
->key
+ round_up(map
->key_size
, 8);
473 /* It is called from the bpf_lru_list when the LRU needs to delete
474 * older elements from the htab.
476 static bool htab_lru_map_delete_node(void *arg
, struct bpf_lru_node
*node
)
478 struct bpf_htab
*htab
= (struct bpf_htab
*)arg
;
479 struct htab_elem
*l
= NULL
, *tgt_l
;
480 struct hlist_nulls_head
*head
;
481 struct hlist_nulls_node
*n
;
485 tgt_l
= container_of(node
, struct htab_elem
, lru_node
);
486 b
= __select_bucket(htab
, tgt_l
->hash
);
489 raw_spin_lock_irqsave(&b
->lock
, flags
);
491 hlist_nulls_for_each_entry_rcu(l
, n
, head
, hash_node
)
493 hlist_nulls_del_rcu(&l
->hash_node
);
497 raw_spin_unlock_irqrestore(&b
->lock
, flags
);
502 /* Called from syscall */
503 static int htab_map_get_next_key(struct bpf_map
*map
, void *key
, void *next_key
)
505 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
506 struct hlist_nulls_head
*head
;
507 struct htab_elem
*l
, *next_l
;
511 WARN_ON_ONCE(!rcu_read_lock_held());
513 key_size
= map
->key_size
;
515 hash
= htab_map_hash(key
, key_size
);
517 head
= select_bucket(htab
, hash
);
520 l
= lookup_nulls_elem_raw(head
, hash
, key
, key_size
, htab
->n_buckets
);
524 goto find_first_elem
;
527 /* key was found, get next key in the same bucket */
528 next_l
= hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l
->hash_node
)),
529 struct htab_elem
, hash_node
);
532 /* if next elem in this hash list is non-zero, just return it */
533 memcpy(next_key
, next_l
->key
, key_size
);
537 /* no more elements in this hash list, go to the next bucket */
538 i
= hash
& (htab
->n_buckets
- 1);
542 /* iterate over buckets */
543 for (; i
< htab
->n_buckets
; i
++) {
544 head
= select_bucket(htab
, i
);
546 /* pick first element in the bucket */
547 next_l
= hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head
)),
548 struct htab_elem
, hash_node
);
550 /* if it's not empty, just return it */
551 memcpy(next_key
, next_l
->key
, key_size
);
556 /* iterated over all buckets and all elements */
560 static void htab_elem_free(struct bpf_htab
*htab
, struct htab_elem
*l
)
562 if (htab
->map
.map_type
== BPF_MAP_TYPE_PERCPU_HASH
)
563 free_percpu(htab_elem_get_ptr(l
, htab
->map
.key_size
));
567 static void htab_elem_free_rcu(struct rcu_head
*head
)
569 struct htab_elem
*l
= container_of(head
, struct htab_elem
, rcu
);
570 struct bpf_htab
*htab
= l
->htab
;
572 /* must increment bpf_prog_active to avoid kprobe+bpf triggering while
573 * we're calling kfree, otherwise deadlock is possible if kprobes
574 * are placed somewhere inside of slub
577 __this_cpu_inc(bpf_prog_active
);
578 htab_elem_free(htab
, l
);
579 __this_cpu_dec(bpf_prog_active
);
583 static void free_htab_elem(struct bpf_htab
*htab
, struct htab_elem
*l
)
585 if (htab_is_prealloc(htab
)) {
586 pcpu_freelist_push(&htab
->freelist
, &l
->fnode
);
588 atomic_dec(&htab
->count
);
590 call_rcu(&l
->rcu
, htab_elem_free_rcu
);
594 static void pcpu_copy_value(struct bpf_htab
*htab
, void __percpu
*pptr
,
595 void *value
, bool onallcpus
)
598 /* copy true value_size bytes */
599 memcpy(this_cpu_ptr(pptr
), value
, htab
->map
.value_size
);
601 u32 size
= round_up(htab
->map
.value_size
, 8);
604 for_each_possible_cpu(cpu
) {
605 bpf_long_memcpy(per_cpu_ptr(pptr
, cpu
),
612 static struct htab_elem
*alloc_htab_elem(struct bpf_htab
*htab
, void *key
,
613 void *value
, u32 key_size
, u32 hash
,
614 bool percpu
, bool onallcpus
,
615 struct htab_elem
*old_elem
)
617 u32 size
= htab
->map
.value_size
;
618 bool prealloc
= htab_is_prealloc(htab
);
619 struct htab_elem
*l_new
, **pl_new
;
624 /* if we're updating the existing element,
625 * use per-cpu extra elems to avoid freelist_pop/push
627 pl_new
= this_cpu_ptr(htab
->extra_elems
);
631 struct pcpu_freelist_node
*l
;
633 l
= pcpu_freelist_pop(&htab
->freelist
);
635 return ERR_PTR(-E2BIG
);
636 l_new
= container_of(l
, struct htab_elem
, fnode
);
639 if (atomic_inc_return(&htab
->count
) > htab
->map
.max_entries
)
641 /* when map is full and update() is replacing
642 * old element, it's ok to allocate, since
643 * old element will be freed immediately.
644 * Otherwise return an error
646 atomic_dec(&htab
->count
);
647 return ERR_PTR(-E2BIG
);
649 l_new
= kmalloc(htab
->elem_size
, GFP_ATOMIC
| __GFP_NOWARN
);
651 return ERR_PTR(-ENOMEM
);
654 memcpy(l_new
->key
, key
, key_size
);
656 /* round up value_size to 8 bytes */
657 size
= round_up(size
, 8);
660 pptr
= htab_elem_get_ptr(l_new
, key_size
);
662 /* alloc_percpu zero-fills */
663 pptr
= __alloc_percpu_gfp(size
, 8,
664 GFP_ATOMIC
| __GFP_NOWARN
);
667 return ERR_PTR(-ENOMEM
);
671 pcpu_copy_value(htab
, pptr
, value
, onallcpus
);
674 htab_elem_set_ptr(l_new
, key_size
, pptr
);
676 memcpy(l_new
->key
+ round_up(key_size
, 8), value
, size
);
683 static int check_flags(struct bpf_htab
*htab
, struct htab_elem
*l_old
,
686 if (l_old
&& map_flags
== BPF_NOEXIST
)
687 /* elem already exists */
690 if (!l_old
&& map_flags
== BPF_EXIST
)
691 /* elem doesn't exist, cannot update it */
697 /* Called from syscall or from eBPF program */
698 static int htab_map_update_elem(struct bpf_map
*map
, void *key
, void *value
,
701 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
702 struct htab_elem
*l_new
= NULL
, *l_old
;
703 struct hlist_nulls_head
*head
;
709 if (unlikely(map_flags
> BPF_EXIST
))
713 WARN_ON_ONCE(!rcu_read_lock_held());
715 key_size
= map
->key_size
;
717 hash
= htab_map_hash(key
, key_size
);
719 b
= __select_bucket(htab
, hash
);
722 /* bpf_map_update_elem() can be called in_irq() */
723 raw_spin_lock_irqsave(&b
->lock
, flags
);
725 l_old
= lookup_elem_raw(head
, hash
, key
, key_size
);
727 ret
= check_flags(htab
, l_old
, map_flags
);
731 l_new
= alloc_htab_elem(htab
, key
, value
, key_size
, hash
, false, false,
734 /* all pre-allocated elements are in use or memory exhausted */
735 ret
= PTR_ERR(l_new
);
739 /* add new element to the head of the list, so that
740 * concurrent search will find it before old elem
742 hlist_nulls_add_head_rcu(&l_new
->hash_node
, head
);
744 hlist_nulls_del_rcu(&l_old
->hash_node
);
745 if (!htab_is_prealloc(htab
))
746 free_htab_elem(htab
, l_old
);
750 raw_spin_unlock_irqrestore(&b
->lock
, flags
);
754 static int htab_lru_map_update_elem(struct bpf_map
*map
, void *key
, void *value
,
757 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
758 struct htab_elem
*l_new
, *l_old
= NULL
;
759 struct hlist_nulls_head
*head
;
765 if (unlikely(map_flags
> BPF_EXIST
))
769 WARN_ON_ONCE(!rcu_read_lock_held());
771 key_size
= map
->key_size
;
773 hash
= htab_map_hash(key
, key_size
);
775 b
= __select_bucket(htab
, hash
);
778 /* For LRU, we need to alloc before taking bucket's
779 * spinlock because getting free nodes from LRU may need
780 * to remove older elements from htab and this removal
781 * operation will need a bucket lock.
783 l_new
= prealloc_lru_pop(htab
, key
, hash
);
786 memcpy(l_new
->key
+ round_up(map
->key_size
, 8), value
, map
->value_size
);
788 /* bpf_map_update_elem() can be called in_irq() */
789 raw_spin_lock_irqsave(&b
->lock
, flags
);
791 l_old
= lookup_elem_raw(head
, hash
, key
, key_size
);
793 ret
= check_flags(htab
, l_old
, map_flags
);
797 /* add new element to the head of the list, so that
798 * concurrent search will find it before old elem
800 hlist_nulls_add_head_rcu(&l_new
->hash_node
, head
);
802 bpf_lru_node_set_ref(&l_new
->lru_node
);
803 hlist_nulls_del_rcu(&l_old
->hash_node
);
808 raw_spin_unlock_irqrestore(&b
->lock
, flags
);
811 bpf_lru_push_free(&htab
->lru
, &l_new
->lru_node
);
813 bpf_lru_push_free(&htab
->lru
, &l_old
->lru_node
);
818 static int __htab_percpu_map_update_elem(struct bpf_map
*map
, void *key
,
819 void *value
, u64 map_flags
,
822 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
823 struct htab_elem
*l_new
= NULL
, *l_old
;
824 struct hlist_nulls_head
*head
;
830 if (unlikely(map_flags
> BPF_EXIST
))
834 WARN_ON_ONCE(!rcu_read_lock_held());
836 key_size
= map
->key_size
;
838 hash
= htab_map_hash(key
, key_size
);
840 b
= __select_bucket(htab
, hash
);
843 /* bpf_map_update_elem() can be called in_irq() */
844 raw_spin_lock_irqsave(&b
->lock
, flags
);
846 l_old
= lookup_elem_raw(head
, hash
, key
, key_size
);
848 ret
= check_flags(htab
, l_old
, map_flags
);
853 /* per-cpu hash map can update value in-place */
854 pcpu_copy_value(htab
, htab_elem_get_ptr(l_old
, key_size
),
857 l_new
= alloc_htab_elem(htab
, key
, value
, key_size
,
858 hash
, true, onallcpus
, NULL
);
860 ret
= PTR_ERR(l_new
);
863 hlist_nulls_add_head_rcu(&l_new
->hash_node
, head
);
867 raw_spin_unlock_irqrestore(&b
->lock
, flags
);
871 static int __htab_lru_percpu_map_update_elem(struct bpf_map
*map
, void *key
,
872 void *value
, u64 map_flags
,
875 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
876 struct htab_elem
*l_new
= NULL
, *l_old
;
877 struct hlist_nulls_head
*head
;
883 if (unlikely(map_flags
> BPF_EXIST
))
887 WARN_ON_ONCE(!rcu_read_lock_held());
889 key_size
= map
->key_size
;
891 hash
= htab_map_hash(key
, key_size
);
893 b
= __select_bucket(htab
, hash
);
896 /* For LRU, we need to alloc before taking bucket's
897 * spinlock because LRU's elem alloc may need
898 * to remove older elem from htab and this removal
899 * operation will need a bucket lock.
901 if (map_flags
!= BPF_EXIST
) {
902 l_new
= prealloc_lru_pop(htab
, key
, hash
);
907 /* bpf_map_update_elem() can be called in_irq() */
908 raw_spin_lock_irqsave(&b
->lock
, flags
);
910 l_old
= lookup_elem_raw(head
, hash
, key
, key_size
);
912 ret
= check_flags(htab
, l_old
, map_flags
);
917 bpf_lru_node_set_ref(&l_old
->lru_node
);
919 /* per-cpu hash map can update value in-place */
920 pcpu_copy_value(htab
, htab_elem_get_ptr(l_old
, key_size
),
923 pcpu_copy_value(htab
, htab_elem_get_ptr(l_new
, key_size
),
925 hlist_nulls_add_head_rcu(&l_new
->hash_node
, head
);
930 raw_spin_unlock_irqrestore(&b
->lock
, flags
);
932 bpf_lru_push_free(&htab
->lru
, &l_new
->lru_node
);
936 static int htab_percpu_map_update_elem(struct bpf_map
*map
, void *key
,
937 void *value
, u64 map_flags
)
939 return __htab_percpu_map_update_elem(map
, key
, value
, map_flags
, false);
942 static int htab_lru_percpu_map_update_elem(struct bpf_map
*map
, void *key
,
943 void *value
, u64 map_flags
)
945 return __htab_lru_percpu_map_update_elem(map
, key
, value
, map_flags
,
949 /* Called from syscall or from eBPF program */
950 static int htab_map_delete_elem(struct bpf_map
*map
, void *key
)
952 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
953 struct hlist_nulls_head
*head
;
960 WARN_ON_ONCE(!rcu_read_lock_held());
962 key_size
= map
->key_size
;
964 hash
= htab_map_hash(key
, key_size
);
965 b
= __select_bucket(htab
, hash
);
968 raw_spin_lock_irqsave(&b
->lock
, flags
);
970 l
= lookup_elem_raw(head
, hash
, key
, key_size
);
973 hlist_nulls_del_rcu(&l
->hash_node
);
974 free_htab_elem(htab
, l
);
978 raw_spin_unlock_irqrestore(&b
->lock
, flags
);
982 static int htab_lru_map_delete_elem(struct bpf_map
*map
, void *key
)
984 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
985 struct hlist_nulls_head
*head
;
992 WARN_ON_ONCE(!rcu_read_lock_held());
994 key_size
= map
->key_size
;
996 hash
= htab_map_hash(key
, key_size
);
997 b
= __select_bucket(htab
, hash
);
1000 raw_spin_lock_irqsave(&b
->lock
, flags
);
1002 l
= lookup_elem_raw(head
, hash
, key
, key_size
);
1005 hlist_nulls_del_rcu(&l
->hash_node
);
1009 raw_spin_unlock_irqrestore(&b
->lock
, flags
);
1011 bpf_lru_push_free(&htab
->lru
, &l
->lru_node
);
1015 static void delete_all_elements(struct bpf_htab
*htab
)
1019 for (i
= 0; i
< htab
->n_buckets
; i
++) {
1020 struct hlist_nulls_head
*head
= select_bucket(htab
, i
);
1021 struct hlist_nulls_node
*n
;
1022 struct htab_elem
*l
;
1024 hlist_nulls_for_each_entry_safe(l
, n
, head
, hash_node
) {
1025 hlist_nulls_del_rcu(&l
->hash_node
);
1026 htab_elem_free(htab
, l
);
1030 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1031 static void htab_map_free(struct bpf_map
*map
)
1033 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
1035 /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
1036 * so the programs (can be more than one that used this map) were
1037 * disconnected from events. Wait for outstanding critical sections in
1038 * these programs to complete
1042 /* some of free_htab_elem() callbacks for elements of this map may
1043 * not have executed. Wait for them.
1046 if (!htab_is_prealloc(htab
))
1047 delete_all_elements(htab
);
1049 prealloc_destroy(htab
);
1051 free_percpu(htab
->extra_elems
);
1052 bpf_map_area_free(htab
->buckets
);
1056 static const struct bpf_map_ops htab_ops
= {
1057 .map_alloc
= htab_map_alloc
,
1058 .map_free
= htab_map_free
,
1059 .map_get_next_key
= htab_map_get_next_key
,
1060 .map_lookup_elem
= htab_map_lookup_elem
,
1061 .map_update_elem
= htab_map_update_elem
,
1062 .map_delete_elem
= htab_map_delete_elem
,
1065 static struct bpf_map_type_list htab_type __ro_after_init
= {
1067 .type
= BPF_MAP_TYPE_HASH
,
1070 static const struct bpf_map_ops htab_lru_ops
= {
1071 .map_alloc
= htab_map_alloc
,
1072 .map_free
= htab_map_free
,
1073 .map_get_next_key
= htab_map_get_next_key
,
1074 .map_lookup_elem
= htab_lru_map_lookup_elem
,
1075 .map_update_elem
= htab_lru_map_update_elem
,
1076 .map_delete_elem
= htab_lru_map_delete_elem
,
1079 static struct bpf_map_type_list htab_lru_type __ro_after_init
= {
1080 .ops
= &htab_lru_ops
,
1081 .type
= BPF_MAP_TYPE_LRU_HASH
,
1084 /* Called from eBPF program */
1085 static void *htab_percpu_map_lookup_elem(struct bpf_map
*map
, void *key
)
1087 struct htab_elem
*l
= __htab_map_lookup_elem(map
, key
);
1090 return this_cpu_ptr(htab_elem_get_ptr(l
, map
->key_size
));
1095 static void *htab_lru_percpu_map_lookup_elem(struct bpf_map
*map
, void *key
)
1097 struct htab_elem
*l
= __htab_map_lookup_elem(map
, key
);
1100 bpf_lru_node_set_ref(&l
->lru_node
);
1101 return this_cpu_ptr(htab_elem_get_ptr(l
, map
->key_size
));
1107 int bpf_percpu_hash_copy(struct bpf_map
*map
, void *key
, void *value
)
1109 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
1110 struct htab_elem
*l
;
1111 void __percpu
*pptr
;
1116 /* per_cpu areas are zero-filled and bpf programs can only
1117 * access 'value_size' of them, so copying rounded areas
1118 * will not leak any kernel data
1120 size
= round_up(map
->value_size
, 8);
1122 l
= __htab_map_lookup_elem(map
, key
);
1125 if (htab_is_lru(htab
))
1126 bpf_lru_node_set_ref(&l
->lru_node
);
1127 pptr
= htab_elem_get_ptr(l
, map
->key_size
);
1128 for_each_possible_cpu(cpu
) {
1129 bpf_long_memcpy(value
+ off
,
1130 per_cpu_ptr(pptr
, cpu
), size
);
1139 int bpf_percpu_hash_update(struct bpf_map
*map
, void *key
, void *value
,
1142 struct bpf_htab
*htab
= container_of(map
, struct bpf_htab
, map
);
1146 if (htab_is_lru(htab
))
1147 ret
= __htab_lru_percpu_map_update_elem(map
, key
, value
,
1150 ret
= __htab_percpu_map_update_elem(map
, key
, value
, map_flags
,
1157 static const struct bpf_map_ops htab_percpu_ops
= {
1158 .map_alloc
= htab_map_alloc
,
1159 .map_free
= htab_map_free
,
1160 .map_get_next_key
= htab_map_get_next_key
,
1161 .map_lookup_elem
= htab_percpu_map_lookup_elem
,
1162 .map_update_elem
= htab_percpu_map_update_elem
,
1163 .map_delete_elem
= htab_map_delete_elem
,
1166 static struct bpf_map_type_list htab_percpu_type __ro_after_init
= {
1167 .ops
= &htab_percpu_ops
,
1168 .type
= BPF_MAP_TYPE_PERCPU_HASH
,
1171 static const struct bpf_map_ops htab_lru_percpu_ops
= {
1172 .map_alloc
= htab_map_alloc
,
1173 .map_free
= htab_map_free
,
1174 .map_get_next_key
= htab_map_get_next_key
,
1175 .map_lookup_elem
= htab_lru_percpu_map_lookup_elem
,
1176 .map_update_elem
= htab_lru_percpu_map_update_elem
,
1177 .map_delete_elem
= htab_lru_map_delete_elem
,
1180 static struct bpf_map_type_list htab_lru_percpu_type __ro_after_init
= {
1181 .ops
= &htab_lru_percpu_ops
,
1182 .type
= BPF_MAP_TYPE_LRU_PERCPU_HASH
,
1185 static int __init
register_htab_map(void)
1187 bpf_register_map_type(&htab_type
);
1188 bpf_register_map_type(&htab_percpu_type
);
1189 bpf_register_map_type(&htab_lru_type
);
1190 bpf_register_map_type(&htab_lru_percpu_type
);
1193 late_initcall(register_htab_map
);