x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / kernel / bpf / hashtab.c
blob361a69dfe5434d6afb554477b899e91db90fb29f
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"
20 struct bucket {
21 struct hlist_nulls_head head;
22 raw_spinlock_t lock;
25 struct bpf_htab {
26 struct bpf_map map;
27 struct bucket *buckets;
28 void *elems;
29 union {
30 struct pcpu_freelist freelist;
31 struct bpf_lru lru;
33 struct htab_elem *__percpu *extra_elems;
34 atomic_t count; /* number of elements in this hashtable */
35 u32 n_buckets; /* number of hash buckets */
36 u32 elem_size; /* size of each element in bytes */
39 /* each htab element is struct htab_elem + key + value */
40 struct htab_elem {
41 union {
42 struct hlist_nulls_node hash_node;
43 struct {
44 void *padding;
45 union {
46 struct bpf_htab *htab;
47 struct pcpu_freelist_node fnode;
51 union {
52 struct rcu_head rcu;
53 struct bpf_lru_node lru_node;
55 u32 hash;
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,
79 void __percpu *pptr)
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)
96 int i;
98 if (!htab_is_percpu(htab))
99 goto free_elems;
101 for (i = 0; i < htab->map.max_entries; i++) {
102 void __percpu *pptr;
104 pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
105 htab->map.key_size);
106 free_percpu(pptr);
108 free_elems:
109 bpf_map_area_free(htab->elems);
112 static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
113 u32 hash)
115 struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
116 struct htab_elem *l;
118 if (node) {
119 l = container_of(node, struct htab_elem, lru_node);
120 memcpy(l->key, key, htab->map.key_size);
121 return l;
124 return NULL;
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);
136 if (!htab->elems)
137 return -ENOMEM;
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);
144 void __percpu *pptr;
146 pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
147 if (!pptr)
148 goto free_elems;
149 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
150 pptr);
153 skip_percpu_elems:
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,
160 htab);
161 else
162 err = pcpu_freelist_init(&htab->freelist);
164 if (err)
165 goto free_elems;
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);
171 else
172 pcpu_freelist_populate(&htab->freelist,
173 htab->elems + offsetof(struct htab_elem, fnode),
174 htab->elem_size, num_entries);
176 return 0;
178 free_elems:
179 htab_free_elems(htab);
180 return err;
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);
189 else
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;
197 int cpu;
199 pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8,
200 GFP_USER | __GFP_NOWARN);
201 if (!pptr)
202 return -ENOMEM;
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;
213 return 0;
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;
231 int err, i;
232 u64 cost;
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);
256 if (!htab)
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
269 err = -EINVAL;
270 if (htab->map.max_entries == 0 || htab->map.key_size == 0 ||
271 htab->map.value_size == 0)
272 goto free_htab;
274 if (percpu_lru) {
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);
289 err = -E2BIG;
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
294 goto free_htab;
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()
303 goto free_htab;
305 if (percpu && round_up(htab->map.value_size, 8) > PCPU_MIN_UNIT_SIZE)
306 /* make sure the size for pcpu_alloc() is reasonable */
307 goto free_htab;
309 htab->elem_size = sizeof(struct htab_elem) +
310 round_up(htab->map.key_size, 8);
311 if (percpu)
312 htab->elem_size += sizeof(void *);
313 else
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))
319 goto free_htab;
321 cost = (u64) htab->n_buckets * sizeof(struct bucket) +
322 (u64) htab->elem_size * htab->map.max_entries;
324 if (percpu)
325 cost += (u64) round_up(htab->map.value_size, 8) *
326 num_possible_cpus() * htab->map.max_entries;
327 else
328 cost += (u64) htab->elem_size * num_possible_cpus();
330 if (cost >= U32_MAX - PAGE_SIZE)
331 /* make sure page count doesn't overflow */
332 goto free_htab;
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);
338 if (err)
339 goto free_htab;
341 err = -ENOMEM;
342 htab->buckets = bpf_map_area_alloc(htab->n_buckets *
343 sizeof(struct bucket));
344 if (!htab->buckets)
345 goto free_htab;
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);
352 if (prealloc) {
353 err = prealloc_init(htab);
354 if (err)
355 goto free_buckets;
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);
362 if (err)
363 goto free_prealloc;
367 return &htab->map;
369 free_prealloc:
370 prealloc_destroy(htab);
371 free_buckets:
372 bpf_map_area_free(htab->buckets);
373 free_htab:
374 kfree(htab);
375 return ERR_PTR(err);
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;
398 struct htab_elem *l;
400 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
401 if (l->hash == hash && !memcmp(&l->key, key, key_size))
402 return l;
404 return NULL;
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,
412 u32 hash, void *key,
413 u32 key_size, u32 n_buckets)
415 struct hlist_nulls_node *n;
416 struct htab_elem *l;
418 again:
419 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
420 if (l->hash == hash && !memcmp(&l->key, key, key_size))
421 return l;
423 if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
424 goto again;
426 return NULL;
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;
434 struct htab_elem *l;
435 u32 hash, key_size;
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);
448 return l;
451 static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
453 struct htab_elem *l = __htab_map_lookup_elem(map, key);
455 if (l)
456 return l->key + round_up(map->key_size, 8);
458 return NULL;
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);
465 if (l) {
466 bpf_lru_node_set_ref(&l->lru_node);
467 return l->key + round_up(map->key_size, 8);
470 return NULL;
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;
482 unsigned long flags;
483 struct bucket *b;
485 tgt_l = container_of(node, struct htab_elem, lru_node);
486 b = __select_bucket(htab, tgt_l->hash);
487 head = &b->head;
489 raw_spin_lock_irqsave(&b->lock, flags);
491 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
492 if (l == tgt_l) {
493 hlist_nulls_del_rcu(&l->hash_node);
494 break;
497 raw_spin_unlock_irqrestore(&b->lock, flags);
499 return l == tgt_l;
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;
508 u32 hash, key_size;
509 int i;
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);
519 /* lookup the key */
520 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
522 if (!l) {
523 i = 0;
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);
531 if (next_l) {
532 /* if next elem in this hash list is non-zero, just return it */
533 memcpy(next_key, next_l->key, key_size);
534 return 0;
537 /* no more elements in this hash list, go to the next bucket */
538 i = hash & (htab->n_buckets - 1);
539 i++;
541 find_first_elem:
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);
549 if (next_l) {
550 /* if it's not empty, just return it */
551 memcpy(next_key, next_l->key, key_size);
552 return 0;
556 /* iterated over all buckets and all elements */
557 return -ENOENT;
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));
564 kfree(l);
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
576 preempt_disable();
577 __this_cpu_inc(bpf_prog_active);
578 htab_elem_free(htab, l);
579 __this_cpu_dec(bpf_prog_active);
580 preempt_enable();
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);
587 } else {
588 atomic_dec(&htab->count);
589 l->htab = htab;
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)
597 if (!onallcpus) {
598 /* copy true value_size bytes */
599 memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
600 } else {
601 u32 size = round_up(htab->map.value_size, 8);
602 int off = 0, cpu;
604 for_each_possible_cpu(cpu) {
605 bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
606 value + off, size);
607 off += size;
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;
620 void __percpu *pptr;
622 if (prealloc) {
623 if (old_elem) {
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);
628 l_new = *pl_new;
629 *pl_new = old_elem;
630 } else {
631 struct pcpu_freelist_node *l;
633 l = pcpu_freelist_pop(&htab->freelist);
634 if (!l)
635 return ERR_PTR(-E2BIG);
636 l_new = container_of(l, struct htab_elem, fnode);
638 } else {
639 if (atomic_inc_return(&htab->count) > htab->map.max_entries)
640 if (!old_elem) {
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);
650 if (!l_new)
651 return ERR_PTR(-ENOMEM);
654 memcpy(l_new->key, key, key_size);
655 if (percpu) {
656 /* round up value_size to 8 bytes */
657 size = round_up(size, 8);
659 if (prealloc) {
660 pptr = htab_elem_get_ptr(l_new, key_size);
661 } else {
662 /* alloc_percpu zero-fills */
663 pptr = __alloc_percpu_gfp(size, 8,
664 GFP_ATOMIC | __GFP_NOWARN);
665 if (!pptr) {
666 kfree(l_new);
667 return ERR_PTR(-ENOMEM);
671 pcpu_copy_value(htab, pptr, value, onallcpus);
673 if (!prealloc)
674 htab_elem_set_ptr(l_new, key_size, pptr);
675 } else {
676 memcpy(l_new->key + round_up(key_size, 8), value, size);
679 l_new->hash = hash;
680 return l_new;
683 static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
684 u64 map_flags)
686 if (l_old && map_flags == BPF_NOEXIST)
687 /* elem already exists */
688 return -EEXIST;
690 if (!l_old && map_flags == BPF_EXIST)
691 /* elem doesn't exist, cannot update it */
692 return -ENOENT;
694 return 0;
697 /* Called from syscall or from eBPF program */
698 static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
699 u64 map_flags)
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;
704 unsigned long flags;
705 struct bucket *b;
706 u32 key_size, hash;
707 int ret;
709 if (unlikely(map_flags > BPF_EXIST))
710 /* unknown flags */
711 return -EINVAL;
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);
720 head = &b->head;
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);
728 if (ret)
729 goto err;
731 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
732 l_old);
733 if (IS_ERR(l_new)) {
734 /* all pre-allocated elements are in use or memory exhausted */
735 ret = PTR_ERR(l_new);
736 goto err;
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);
743 if (l_old) {
744 hlist_nulls_del_rcu(&l_old->hash_node);
745 if (!htab_is_prealloc(htab))
746 free_htab_elem(htab, l_old);
748 ret = 0;
749 err:
750 raw_spin_unlock_irqrestore(&b->lock, flags);
751 return ret;
754 static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
755 u64 map_flags)
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;
760 unsigned long flags;
761 struct bucket *b;
762 u32 key_size, hash;
763 int ret;
765 if (unlikely(map_flags > BPF_EXIST))
766 /* unknown flags */
767 return -EINVAL;
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);
776 head = &b->head;
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);
784 if (!l_new)
785 return -ENOMEM;
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);
794 if (ret)
795 goto err;
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);
801 if (l_old) {
802 bpf_lru_node_set_ref(&l_new->lru_node);
803 hlist_nulls_del_rcu(&l_old->hash_node);
805 ret = 0;
807 err:
808 raw_spin_unlock_irqrestore(&b->lock, flags);
810 if (ret)
811 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
812 else if (l_old)
813 bpf_lru_push_free(&htab->lru, &l_old->lru_node);
815 return ret;
818 static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
819 void *value, u64 map_flags,
820 bool onallcpus)
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;
825 unsigned long flags;
826 struct bucket *b;
827 u32 key_size, hash;
828 int ret;
830 if (unlikely(map_flags > BPF_EXIST))
831 /* unknown flags */
832 return -EINVAL;
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);
841 head = &b->head;
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);
849 if (ret)
850 goto err;
852 if (l_old) {
853 /* per-cpu hash map can update value in-place */
854 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
855 value, onallcpus);
856 } else {
857 l_new = alloc_htab_elem(htab, key, value, key_size,
858 hash, true, onallcpus, NULL);
859 if (IS_ERR(l_new)) {
860 ret = PTR_ERR(l_new);
861 goto err;
863 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
865 ret = 0;
866 err:
867 raw_spin_unlock_irqrestore(&b->lock, flags);
868 return ret;
871 static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
872 void *value, u64 map_flags,
873 bool onallcpus)
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;
878 unsigned long flags;
879 struct bucket *b;
880 u32 key_size, hash;
881 int ret;
883 if (unlikely(map_flags > BPF_EXIST))
884 /* unknown flags */
885 return -EINVAL;
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);
894 head = &b->head;
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);
903 if (!l_new)
904 return -ENOMEM;
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);
913 if (ret)
914 goto err;
916 if (l_old) {
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),
921 value, onallcpus);
922 } else {
923 pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
924 value, onallcpus);
925 hlist_nulls_add_head_rcu(&l_new->hash_node, head);
926 l_new = NULL;
928 ret = 0;
929 err:
930 raw_spin_unlock_irqrestore(&b->lock, flags);
931 if (l_new)
932 bpf_lru_push_free(&htab->lru, &l_new->lru_node);
933 return ret;
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,
946 false);
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;
954 struct bucket *b;
955 struct htab_elem *l;
956 unsigned long flags;
957 u32 hash, key_size;
958 int ret = -ENOENT;
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);
966 head = &b->head;
968 raw_spin_lock_irqsave(&b->lock, flags);
970 l = lookup_elem_raw(head, hash, key, key_size);
972 if (l) {
973 hlist_nulls_del_rcu(&l->hash_node);
974 free_htab_elem(htab, l);
975 ret = 0;
978 raw_spin_unlock_irqrestore(&b->lock, flags);
979 return ret;
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;
986 struct bucket *b;
987 struct htab_elem *l;
988 unsigned long flags;
989 u32 hash, key_size;
990 int ret = -ENOENT;
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);
998 head = &b->head;
1000 raw_spin_lock_irqsave(&b->lock, flags);
1002 l = lookup_elem_raw(head, hash, key, key_size);
1004 if (l) {
1005 hlist_nulls_del_rcu(&l->hash_node);
1006 ret = 0;
1009 raw_spin_unlock_irqrestore(&b->lock, flags);
1010 if (l)
1011 bpf_lru_push_free(&htab->lru, &l->lru_node);
1012 return ret;
1015 static void delete_all_elements(struct bpf_htab *htab)
1017 int i;
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
1040 synchronize_rcu();
1042 /* some of free_htab_elem() callbacks for elements of this map may
1043 * not have executed. Wait for them.
1045 rcu_barrier();
1046 if (!htab_is_prealloc(htab))
1047 delete_all_elements(htab);
1048 else
1049 prealloc_destroy(htab);
1051 free_percpu(htab->extra_elems);
1052 bpf_map_area_free(htab->buckets);
1053 kfree(htab);
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 = {
1066 .ops = &htab_ops,
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);
1089 if (l)
1090 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1091 else
1092 return NULL;
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);
1099 if (l) {
1100 bpf_lru_node_set_ref(&l->lru_node);
1101 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1104 return NULL;
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;
1112 int ret = -ENOENT;
1113 int cpu, off = 0;
1114 u32 size;
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);
1121 rcu_read_lock();
1122 l = __htab_map_lookup_elem(map, key);
1123 if (!l)
1124 goto out;
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);
1131 off += size;
1133 ret = 0;
1134 out:
1135 rcu_read_unlock();
1136 return ret;
1139 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1140 u64 map_flags)
1142 struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1143 int ret;
1145 rcu_read_lock();
1146 if (htab_is_lru(htab))
1147 ret = __htab_lru_percpu_map_update_elem(map, key, value,
1148 map_flags, true);
1149 else
1150 ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1151 true);
1152 rcu_read_unlock();
1154 return ret;
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);
1191 return 0;
1193 late_initcall(register_htab_map);