xref: /openbmc/linux/kernel/bpf/hashtab.c (revision 4419617e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3  * Copyright (c) 2016 Facebook
4  */
5 #include <linux/bpf.h>
6 #include <linux/btf.h>
7 #include <linux/jhash.h>
8 #include <linux/filter.h>
9 #include <linux/rculist_nulls.h>
10 #include <linux/random.h>
11 #include <uapi/linux/btf.h>
12 #include "percpu_freelist.h"
13 #include "bpf_lru_list.h"
14 #include "map_in_map.h"
15 
16 #define HTAB_CREATE_FLAG_MASK						\
17 	(BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE |	\
18 	 BPF_F_ACCESS_MASK | BPF_F_ZERO_SEED)
19 
20 struct bucket {
21 	struct hlist_nulls_head head;
22 	raw_spinlock_t lock;
23 };
24 
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;
32 	};
33 	struct htab_elem *__percpu *extra_elems;
34 	atomic_t count;	/* number of elements in this hashtable */
35 	u32 n_buckets;	/* number of hash buckets */
36 	u32 elem_size;	/* size of each element in bytes */
37 	u32 hashrnd;
38 };
39 
40 /* each htab element is struct htab_elem + key + value */
41 struct htab_elem {
42 	union {
43 		struct hlist_nulls_node hash_node;
44 		struct {
45 			void *padding;
46 			union {
47 				struct bpf_htab *htab;
48 				struct pcpu_freelist_node fnode;
49 			};
50 		};
51 	};
52 	union {
53 		struct rcu_head rcu;
54 		struct bpf_lru_node lru_node;
55 	};
56 	u32 hash;
57 	char key[0] __aligned(8);
58 };
59 
60 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
61 
62 static bool htab_is_lru(const struct bpf_htab *htab)
63 {
64 	return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
65 		htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
66 }
67 
68 static bool htab_is_percpu(const struct bpf_htab *htab)
69 {
70 	return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
71 		htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
72 }
73 
74 static bool htab_is_prealloc(const struct bpf_htab *htab)
75 {
76 	return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
77 }
78 
79 static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
80 				     void __percpu *pptr)
81 {
82 	*(void __percpu **)(l->key + key_size) = pptr;
83 }
84 
85 static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
86 {
87 	return *(void __percpu **)(l->key + key_size);
88 }
89 
90 static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l)
91 {
92 	return *(void **)(l->key + roundup(map->key_size, 8));
93 }
94 
95 static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
96 {
97 	return (struct htab_elem *) (htab->elems + i * htab->elem_size);
98 }
99 
100 static void htab_free_elems(struct bpf_htab *htab)
101 {
102 	int i;
103 
104 	if (!htab_is_percpu(htab))
105 		goto free_elems;
106 
107 	for (i = 0; i < htab->map.max_entries; i++) {
108 		void __percpu *pptr;
109 
110 		pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
111 					 htab->map.key_size);
112 		free_percpu(pptr);
113 		cond_resched();
114 	}
115 free_elems:
116 	bpf_map_area_free(htab->elems);
117 }
118 
119 static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
120 					  u32 hash)
121 {
122 	struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
123 	struct htab_elem *l;
124 
125 	if (node) {
126 		l = container_of(node, struct htab_elem, lru_node);
127 		memcpy(l->key, key, htab->map.key_size);
128 		return l;
129 	}
130 
131 	return NULL;
132 }
133 
134 static int prealloc_init(struct bpf_htab *htab)
135 {
136 	u32 num_entries = htab->map.max_entries;
137 	int err = -ENOMEM, i;
138 
139 	if (!htab_is_percpu(htab) && !htab_is_lru(htab))
140 		num_entries += num_possible_cpus();
141 
142 	htab->elems = bpf_map_area_alloc(htab->elem_size * num_entries,
143 					 htab->map.numa_node);
144 	if (!htab->elems)
145 		return -ENOMEM;
146 
147 	if (!htab_is_percpu(htab))
148 		goto skip_percpu_elems;
149 
150 	for (i = 0; i < num_entries; i++) {
151 		u32 size = round_up(htab->map.value_size, 8);
152 		void __percpu *pptr;
153 
154 		pptr = __alloc_percpu_gfp(size, 8, GFP_USER | __GFP_NOWARN);
155 		if (!pptr)
156 			goto free_elems;
157 		htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
158 				  pptr);
159 		cond_resched();
160 	}
161 
162 skip_percpu_elems:
163 	if (htab_is_lru(htab))
164 		err = bpf_lru_init(&htab->lru,
165 				   htab->map.map_flags & BPF_F_NO_COMMON_LRU,
166 				   offsetof(struct htab_elem, hash) -
167 				   offsetof(struct htab_elem, lru_node),
168 				   htab_lru_map_delete_node,
169 				   htab);
170 	else
171 		err = pcpu_freelist_init(&htab->freelist);
172 
173 	if (err)
174 		goto free_elems;
175 
176 	if (htab_is_lru(htab))
177 		bpf_lru_populate(&htab->lru, htab->elems,
178 				 offsetof(struct htab_elem, lru_node),
179 				 htab->elem_size, num_entries);
180 	else
181 		pcpu_freelist_populate(&htab->freelist,
182 				       htab->elems + offsetof(struct htab_elem, fnode),
183 				       htab->elem_size, num_entries);
184 
185 	return 0;
186 
187 free_elems:
188 	htab_free_elems(htab);
189 	return err;
190 }
191 
192 static void prealloc_destroy(struct bpf_htab *htab)
193 {
194 	htab_free_elems(htab);
195 
196 	if (htab_is_lru(htab))
197 		bpf_lru_destroy(&htab->lru);
198 	else
199 		pcpu_freelist_destroy(&htab->freelist);
200 }
201 
202 static int alloc_extra_elems(struct bpf_htab *htab)
203 {
204 	struct htab_elem *__percpu *pptr, *l_new;
205 	struct pcpu_freelist_node *l;
206 	int cpu;
207 
208 	pptr = __alloc_percpu_gfp(sizeof(struct htab_elem *), 8,
209 				  GFP_USER | __GFP_NOWARN);
210 	if (!pptr)
211 		return -ENOMEM;
212 
213 	for_each_possible_cpu(cpu) {
214 		l = pcpu_freelist_pop(&htab->freelist);
215 		/* pop will succeed, since prealloc_init()
216 		 * preallocated extra num_possible_cpus elements
217 		 */
218 		l_new = container_of(l, struct htab_elem, fnode);
219 		*per_cpu_ptr(pptr, cpu) = l_new;
220 	}
221 	htab->extra_elems = pptr;
222 	return 0;
223 }
224 
225 /* Called from syscall */
226 static int htab_map_alloc_check(union bpf_attr *attr)
227 {
228 	bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
229 		       attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
230 	bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
231 		    attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
232 	/* percpu_lru means each cpu has its own LRU list.
233 	 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
234 	 * the map's value itself is percpu.  percpu_lru has
235 	 * nothing to do with the map's value.
236 	 */
237 	bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
238 	bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
239 	bool zero_seed = (attr->map_flags & BPF_F_ZERO_SEED);
240 	int numa_node = bpf_map_attr_numa_node(attr);
241 
242 	BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
243 		     offsetof(struct htab_elem, hash_node.pprev));
244 	BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
245 		     offsetof(struct htab_elem, hash_node.pprev));
246 
247 	if (lru && !capable(CAP_SYS_ADMIN))
248 		/* LRU implementation is much complicated than other
249 		 * maps.  Hence, limit to CAP_SYS_ADMIN for now.
250 		 */
251 		return -EPERM;
252 
253 	if (zero_seed && !capable(CAP_SYS_ADMIN))
254 		/* Guard against local DoS, and discourage production use. */
255 		return -EPERM;
256 
257 	if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK ||
258 	    !bpf_map_flags_access_ok(attr->map_flags))
259 		return -EINVAL;
260 
261 	if (!lru && percpu_lru)
262 		return -EINVAL;
263 
264 	if (lru && !prealloc)
265 		return -ENOTSUPP;
266 
267 	if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
268 		return -EINVAL;
269 
270 	/* check sanity of attributes.
271 	 * value_size == 0 may be allowed in the future to use map as a set
272 	 */
273 	if (attr->max_entries == 0 || attr->key_size == 0 ||
274 	    attr->value_size == 0)
275 		return -EINVAL;
276 
277 	if (attr->key_size > MAX_BPF_STACK)
278 		/* eBPF programs initialize keys on stack, so they cannot be
279 		 * larger than max stack size
280 		 */
281 		return -E2BIG;
282 
283 	if (attr->value_size >= KMALLOC_MAX_SIZE -
284 	    MAX_BPF_STACK - sizeof(struct htab_elem))
285 		/* if value_size is bigger, the user space won't be able to
286 		 * access the elements via bpf syscall. This check also makes
287 		 * sure that the elem_size doesn't overflow and it's
288 		 * kmalloc-able later in htab_map_update_elem()
289 		 */
290 		return -E2BIG;
291 
292 	return 0;
293 }
294 
295 static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
296 {
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.
305 	 */
306 	bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
307 	bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
308 	struct bpf_htab *htab;
309 	int err, i;
310 	u64 cost;
311 
312 	htab = kzalloc(sizeof(*htab), GFP_USER);
313 	if (!htab)
314 		return ERR_PTR(-ENOMEM);
315 
316 	bpf_map_init_from_attr(&htab->map, attr);
317 
318 	if (percpu_lru) {
319 		/* ensure each CPU's lru list has >=1 elements.
320 		 * since we are at it, make each lru list has the same
321 		 * number of elements.
322 		 */
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());
328 	}
329 
330 	/* hash table size must be power of 2 */
331 	htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
332 
333 	htab->elem_size = sizeof(struct htab_elem) +
334 			  round_up(htab->map.key_size, 8);
335 	if (percpu)
336 		htab->elem_size += sizeof(void *);
337 	else
338 		htab->elem_size += round_up(htab->map.value_size, 8);
339 
340 	err = -E2BIG;
341 	/* prevent zero size kmalloc and check for u32 overflow */
342 	if (htab->n_buckets == 0 ||
343 	    htab->n_buckets > U32_MAX / sizeof(struct bucket))
344 		goto free_htab;
345 
346 	cost = (u64) htab->n_buckets * sizeof(struct bucket) +
347 	       (u64) htab->elem_size * htab->map.max_entries;
348 
349 	if (percpu)
350 		cost += (u64) round_up(htab->map.value_size, 8) *
351 			num_possible_cpus() * htab->map.max_entries;
352 	else
353 	       cost += (u64) htab->elem_size * num_possible_cpus();
354 
355 	if (cost >= U32_MAX - PAGE_SIZE)
356 		/* make sure page count doesn't overflow */
357 		goto free_htab;
358 
359 	htab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
360 
361 	/* if map size is larger than memlock limit, reject it early */
362 	err = bpf_map_precharge_memlock(htab->map.pages);
363 	if (err)
364 		goto free_htab;
365 
366 	err = -ENOMEM;
367 	htab->buckets = bpf_map_area_alloc(htab->n_buckets *
368 					   sizeof(struct bucket),
369 					   htab->map.numa_node);
370 	if (!htab->buckets)
371 		goto free_htab;
372 
373 	if (htab->map.map_flags & BPF_F_ZERO_SEED)
374 		htab->hashrnd = 0;
375 	else
376 		htab->hashrnd = get_random_int();
377 
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);
381 	}
382 
383 	if (prealloc) {
384 		err = prealloc_init(htab);
385 		if (err)
386 			goto free_buckets;
387 
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.
391 			 */
392 			err = alloc_extra_elems(htab);
393 			if (err)
394 				goto free_prealloc;
395 		}
396 	}
397 
398 	return &htab->map;
399 
400 free_prealloc:
401 	prealloc_destroy(htab);
402 free_buckets:
403 	bpf_map_area_free(htab->buckets);
404 free_htab:
405 	kfree(htab);
406 	return ERR_PTR(err);
407 }
408 
409 static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
410 {
411 	return jhash(key, key_len, hashrnd);
412 }
413 
414 static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
415 {
416 	return &htab->buckets[hash & (htab->n_buckets - 1)];
417 }
418 
419 static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
420 {
421 	return &__select_bucket(htab, hash)->head;
422 }
423 
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)
427 {
428 	struct hlist_nulls_node *n;
429 	struct htab_elem *l;
430 
431 	hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
432 		if (l->hash == hash && !memcmp(&l->key, key, key_size))
433 			return l;
434 
435 	return NULL;
436 }
437 
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
441  */
442 static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
443 					       u32 hash, void *key,
444 					       u32 key_size, u32 n_buckets)
445 {
446 	struct hlist_nulls_node *n;
447 	struct htab_elem *l;
448 
449 again:
450 	hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
451 		if (l->hash == hash && !memcmp(&l->key, key, key_size))
452 			return l;
453 
454 	if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
455 		goto again;
456 
457 	return NULL;
458 }
459 
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().
464  */
465 static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
466 {
467 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
468 	struct hlist_nulls_head *head;
469 	struct htab_elem *l;
470 	u32 hash, key_size;
471 
472 	/* Must be called with rcu_read_lock. */
473 	WARN_ON_ONCE(!rcu_read_lock_held());
474 
475 	key_size = map->key_size;
476 
477 	hash = htab_map_hash(key, key_size, htab->hashrnd);
478 
479 	head = select_bucket(htab, hash);
480 
481 	l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
482 
483 	return l;
484 }
485 
486 static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
487 {
488 	struct htab_elem *l = __htab_map_lookup_elem(map, key);
489 
490 	if (l)
491 		return l->key + round_up(map->key_size, 8);
492 
493 	return NULL;
494 }
495 
496 /* inline bpf_map_lookup_elem() call.
497  * Instead of:
498  * bpf_prog
499  *   bpf_map_lookup_elem
500  *     map->ops->map_lookup_elem
501  *       htab_map_lookup_elem
502  *         __htab_map_lookup_elem
503  * do:
504  * bpf_prog
505  *   __htab_map_lookup_elem
506  */
507 static u32 htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
508 {
509 	struct bpf_insn *insn = insn_buf;
510 	const int ret = BPF_REG_0;
511 
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;
520 }
521 
522 static __always_inline void *__htab_lru_map_lookup_elem(struct bpf_map *map,
523 							void *key, const bool mark)
524 {
525 	struct htab_elem *l = __htab_map_lookup_elem(map, key);
526 
527 	if (l) {
528 		if (mark)
529 			bpf_lru_node_set_ref(&l->lru_node);
530 		return l->key + round_up(map->key_size, 8);
531 	}
532 
533 	return NULL;
534 }
535 
536 static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
537 {
538 	return __htab_lru_map_lookup_elem(map, key, true);
539 }
540 
541 static void *htab_lru_map_lookup_elem_sys(struct bpf_map *map, void *key)
542 {
543 	return __htab_lru_map_lookup_elem(map, key, false);
544 }
545 
546 static u32 htab_lru_map_gen_lookup(struct bpf_map *map,
547 				   struct bpf_insn *insn_buf)
548 {
549 	struct bpf_insn *insn = insn_buf;
550 	const int ret = BPF_REG_0;
551 	const int ref_reg = BPF_REG_1;
552 
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),
564 			     1);
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;
569 }
570 
571 /* It is called from the bpf_lru_list when the LRU needs to delete
572  * older elements from the htab.
573  */
574 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
575 {
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;
580 	unsigned long flags;
581 	struct bucket *b;
582 
583 	tgt_l = container_of(node, struct htab_elem, lru_node);
584 	b = __select_bucket(htab, tgt_l->hash);
585 	head = &b->head;
586 
587 	raw_spin_lock_irqsave(&b->lock, flags);
588 
589 	hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
590 		if (l == tgt_l) {
591 			hlist_nulls_del_rcu(&l->hash_node);
592 			break;
593 		}
594 
595 	raw_spin_unlock_irqrestore(&b->lock, flags);
596 
597 	return l == tgt_l;
598 }
599 
600 /* Called from syscall */
601 static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
602 {
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;
606 	u32 hash, key_size;
607 	int i = 0;
608 
609 	WARN_ON_ONCE(!rcu_read_lock_held());
610 
611 	key_size = map->key_size;
612 
613 	if (!key)
614 		goto find_first_elem;
615 
616 	hash = htab_map_hash(key, key_size, htab->hashrnd);
617 
618 	head = select_bucket(htab, hash);
619 
620 	/* lookup the key */
621 	l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
622 
623 	if (!l)
624 		goto find_first_elem;
625 
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);
629 
630 	if (next_l) {
631 		/* if next elem in this hash list is non-zero, just return it */
632 		memcpy(next_key, next_l->key, key_size);
633 		return 0;
634 	}
635 
636 	/* no more elements in this hash list, go to the next bucket */
637 	i = hash & (htab->n_buckets - 1);
638 	i++;
639 
640 find_first_elem:
641 	/* iterate over buckets */
642 	for (; i < htab->n_buckets; i++) {
643 		head = select_bucket(htab, i);
644 
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);
648 		if (next_l) {
649 			/* if it's not empty, just return it */
650 			memcpy(next_key, next_l->key, key_size);
651 			return 0;
652 		}
653 	}
654 
655 	/* iterated over all buckets and all elements */
656 	return -ENOENT;
657 }
658 
659 static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
660 {
661 	if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
662 		free_percpu(htab_elem_get_ptr(l, htab->map.key_size));
663 	kfree(l);
664 }
665 
666 static void htab_elem_free_rcu(struct rcu_head *head)
667 {
668 	struct htab_elem *l = container_of(head, struct htab_elem, rcu);
669 	struct bpf_htab *htab = l->htab;
670 
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
674 	 */
675 	preempt_disable();
676 	__this_cpu_inc(bpf_prog_active);
677 	htab_elem_free(htab, l);
678 	__this_cpu_dec(bpf_prog_active);
679 	preempt_enable();
680 }
681 
682 static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
683 {
684 	struct bpf_map *map = &htab->map;
685 
686 	if (map->ops->map_fd_put_ptr) {
687 		void *ptr = fd_htab_map_get_ptr(map, l);
688 
689 		map->ops->map_fd_put_ptr(ptr);
690 	}
691 
692 	if (htab_is_prealloc(htab)) {
693 		__pcpu_freelist_push(&htab->freelist, &l->fnode);
694 	} else {
695 		atomic_dec(&htab->count);
696 		l->htab = htab;
697 		call_rcu(&l->rcu, htab_elem_free_rcu);
698 	}
699 }
700 
701 static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
702 			    void *value, bool onallcpus)
703 {
704 	if (!onallcpus) {
705 		/* copy true value_size bytes */
706 		memcpy(this_cpu_ptr(pptr), value, htab->map.value_size);
707 	} else {
708 		u32 size = round_up(htab->map.value_size, 8);
709 		int off = 0, cpu;
710 
711 		for_each_possible_cpu(cpu) {
712 			bpf_long_memcpy(per_cpu_ptr(pptr, cpu),
713 					value + off, size);
714 			off += size;
715 		}
716 	}
717 }
718 
719 static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
720 {
721 	return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
722 	       BITS_PER_LONG == 64;
723 }
724 
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)
729 {
730 	u32 size = htab->map.value_size;
731 	bool prealloc = htab_is_prealloc(htab);
732 	struct htab_elem *l_new, **pl_new;
733 	void __percpu *pptr;
734 
735 	if (prealloc) {
736 		if (old_elem) {
737 			/* if we're updating the existing element,
738 			 * use per-cpu extra elems to avoid freelist_pop/push
739 			 */
740 			pl_new = this_cpu_ptr(htab->extra_elems);
741 			l_new = *pl_new;
742 			*pl_new = old_elem;
743 		} else {
744 			struct pcpu_freelist_node *l;
745 
746 			l = __pcpu_freelist_pop(&htab->freelist);
747 			if (!l)
748 				return ERR_PTR(-E2BIG);
749 			l_new = container_of(l, struct htab_elem, fnode);
750 		}
751 	} else {
752 		if (atomic_inc_return(&htab->count) > htab->map.max_entries)
753 			if (!old_elem) {
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
758 				 */
759 				l_new = ERR_PTR(-E2BIG);
760 				goto dec_count;
761 			}
762 		l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
763 				     htab->map.numa_node);
764 		if (!l_new) {
765 			l_new = ERR_PTR(-ENOMEM);
766 			goto dec_count;
767 		}
768 		check_and_init_map_lock(&htab->map,
769 					l_new->key + round_up(key_size, 8));
770 	}
771 
772 	memcpy(l_new->key, key, key_size);
773 	if (percpu) {
774 		size = round_up(size, 8);
775 		if (prealloc) {
776 			pptr = htab_elem_get_ptr(l_new, key_size);
777 		} else {
778 			/* alloc_percpu zero-fills */
779 			pptr = __alloc_percpu_gfp(size, 8,
780 						  GFP_ATOMIC | __GFP_NOWARN);
781 			if (!pptr) {
782 				kfree(l_new);
783 				l_new = ERR_PTR(-ENOMEM);
784 				goto dec_count;
785 			}
786 		}
787 
788 		pcpu_copy_value(htab, pptr, value, onallcpus);
789 
790 		if (!prealloc)
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);
795 	} else {
796 		copy_map_value(&htab->map,
797 			       l_new->key + round_up(key_size, 8),
798 			       value);
799 	}
800 
801 	l_new->hash = hash;
802 	return l_new;
803 dec_count:
804 	atomic_dec(&htab->count);
805 	return l_new;
806 }
807 
808 static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
809 		       u64 map_flags)
810 {
811 	if (l_old && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
812 		/* elem already exists */
813 		return -EEXIST;
814 
815 	if (!l_old && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
816 		/* elem doesn't exist, cannot update it */
817 		return -ENOENT;
818 
819 	return 0;
820 }
821 
822 /* Called from syscall or from eBPF program */
823 static int htab_map_update_elem(struct bpf_map *map, void *key, void *value,
824 				u64 map_flags)
825 {
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;
829 	unsigned long flags;
830 	struct bucket *b;
831 	u32 key_size, hash;
832 	int ret;
833 
834 	if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST))
835 		/* unknown flags */
836 		return -EINVAL;
837 
838 	WARN_ON_ONCE(!rcu_read_lock_held());
839 
840 	key_size = map->key_size;
841 
842 	hash = htab_map_hash(key, key_size, htab->hashrnd);
843 
844 	b = __select_bucket(htab, hash);
845 	head = &b->head;
846 
847 	if (unlikely(map_flags & BPF_F_LOCK)) {
848 		if (unlikely(!map_value_has_spin_lock(map)))
849 			return -EINVAL;
850 		/* find an element without taking the bucket lock */
851 		l_old = lookup_nulls_elem_raw(head, hash, key, key_size,
852 					      htab->n_buckets);
853 		ret = check_flags(htab, l_old, map_flags);
854 		if (ret)
855 			return ret;
856 		if (l_old) {
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),
860 					      value, false);
861 			return 0;
862 		}
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.
866 		 */
867 	}
868 
869 	/* bpf_map_update_elem() can be called in_irq() */
870 	raw_spin_lock_irqsave(&b->lock, flags);
871 
872 	l_old = lookup_elem_raw(head, hash, key, key_size);
873 
874 	ret = check_flags(htab, l_old, map_flags);
875 	if (ret)
876 		goto err;
877 
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
884 		 */
885 		copy_map_value_locked(map,
886 				      l_old->key + round_up(key_size, 8),
887 				      value, false);
888 		ret = 0;
889 		goto err;
890 	}
891 
892 	l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
893 				l_old);
894 	if (IS_ERR(l_new)) {
895 		/* all pre-allocated elements are in use or memory exhausted */
896 		ret = PTR_ERR(l_new);
897 		goto err;
898 	}
899 
900 	/* add new element to the head of the list, so that
901 	 * concurrent search will find it before old elem
902 	 */
903 	hlist_nulls_add_head_rcu(&l_new->hash_node, head);
904 	if (l_old) {
905 		hlist_nulls_del_rcu(&l_old->hash_node);
906 		if (!htab_is_prealloc(htab))
907 			free_htab_elem(htab, l_old);
908 	}
909 	ret = 0;
910 err:
911 	raw_spin_unlock_irqrestore(&b->lock, flags);
912 	return ret;
913 }
914 
915 static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
916 				    u64 map_flags)
917 {
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;
921 	unsigned long flags;
922 	struct bucket *b;
923 	u32 key_size, hash;
924 	int ret;
925 
926 	if (unlikely(map_flags > BPF_EXIST))
927 		/* unknown flags */
928 		return -EINVAL;
929 
930 	WARN_ON_ONCE(!rcu_read_lock_held());
931 
932 	key_size = map->key_size;
933 
934 	hash = htab_map_hash(key, key_size, htab->hashrnd);
935 
936 	b = __select_bucket(htab, hash);
937 	head = &b->head;
938 
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.
943 	 */
944 	l_new = prealloc_lru_pop(htab, key, hash);
945 	if (!l_new)
946 		return -ENOMEM;
947 	memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
948 
949 	/* bpf_map_update_elem() can be called in_irq() */
950 	raw_spin_lock_irqsave(&b->lock, flags);
951 
952 	l_old = lookup_elem_raw(head, hash, key, key_size);
953 
954 	ret = check_flags(htab, l_old, map_flags);
955 	if (ret)
956 		goto err;
957 
958 	/* add new element to the head of the list, so that
959 	 * concurrent search will find it before old elem
960 	 */
961 	hlist_nulls_add_head_rcu(&l_new->hash_node, head);
962 	if (l_old) {
963 		bpf_lru_node_set_ref(&l_new->lru_node);
964 		hlist_nulls_del_rcu(&l_old->hash_node);
965 	}
966 	ret = 0;
967 
968 err:
969 	raw_spin_unlock_irqrestore(&b->lock, flags);
970 
971 	if (ret)
972 		bpf_lru_push_free(&htab->lru, &l_new->lru_node);
973 	else if (l_old)
974 		bpf_lru_push_free(&htab->lru, &l_old->lru_node);
975 
976 	return ret;
977 }
978 
979 static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key,
980 					 void *value, u64 map_flags,
981 					 bool onallcpus)
982 {
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;
986 	unsigned long flags;
987 	struct bucket *b;
988 	u32 key_size, hash;
989 	int ret;
990 
991 	if (unlikely(map_flags > BPF_EXIST))
992 		/* unknown flags */
993 		return -EINVAL;
994 
995 	WARN_ON_ONCE(!rcu_read_lock_held());
996 
997 	key_size = map->key_size;
998 
999 	hash = htab_map_hash(key, key_size, htab->hashrnd);
1000 
1001 	b = __select_bucket(htab, hash);
1002 	head = &b->head;
1003 
1004 	/* bpf_map_update_elem() can be called in_irq() */
1005 	raw_spin_lock_irqsave(&b->lock, flags);
1006 
1007 	l_old = lookup_elem_raw(head, hash, key, key_size);
1008 
1009 	ret = check_flags(htab, l_old, map_flags);
1010 	if (ret)
1011 		goto err;
1012 
1013 	if (l_old) {
1014 		/* per-cpu hash map can update value in-place */
1015 		pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1016 				value, onallcpus);
1017 	} else {
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);
1022 			goto err;
1023 		}
1024 		hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1025 	}
1026 	ret = 0;
1027 err:
1028 	raw_spin_unlock_irqrestore(&b->lock, flags);
1029 	return ret;
1030 }
1031 
1032 static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1033 					     void *value, u64 map_flags,
1034 					     bool onallcpus)
1035 {
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;
1040 	struct bucket *b;
1041 	u32 key_size, hash;
1042 	int ret;
1043 
1044 	if (unlikely(map_flags > BPF_EXIST))
1045 		/* unknown flags */
1046 		return -EINVAL;
1047 
1048 	WARN_ON_ONCE(!rcu_read_lock_held());
1049 
1050 	key_size = map->key_size;
1051 
1052 	hash = htab_map_hash(key, key_size, htab->hashrnd);
1053 
1054 	b = __select_bucket(htab, hash);
1055 	head = &b->head;
1056 
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.
1061 	 */
1062 	if (map_flags != BPF_EXIST) {
1063 		l_new = prealloc_lru_pop(htab, key, hash);
1064 		if (!l_new)
1065 			return -ENOMEM;
1066 	}
1067 
1068 	/* bpf_map_update_elem() can be called in_irq() */
1069 	raw_spin_lock_irqsave(&b->lock, flags);
1070 
1071 	l_old = lookup_elem_raw(head, hash, key, key_size);
1072 
1073 	ret = check_flags(htab, l_old, map_flags);
1074 	if (ret)
1075 		goto err;
1076 
1077 	if (l_old) {
1078 		bpf_lru_node_set_ref(&l_old->lru_node);
1079 
1080 		/* per-cpu hash map can update value in-place */
1081 		pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1082 				value, onallcpus);
1083 	} else {
1084 		pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
1085 				value, onallcpus);
1086 		hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1087 		l_new = NULL;
1088 	}
1089 	ret = 0;
1090 err:
1091 	raw_spin_unlock_irqrestore(&b->lock, flags);
1092 	if (l_new)
1093 		bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1094 	return ret;
1095 }
1096 
1097 static int htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1098 				       void *value, u64 map_flags)
1099 {
1100 	return __htab_percpu_map_update_elem(map, key, value, map_flags, false);
1101 }
1102 
1103 static int htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1104 					   void *value, u64 map_flags)
1105 {
1106 	return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
1107 						 false);
1108 }
1109 
1110 /* Called from syscall or from eBPF program */
1111 static int htab_map_delete_elem(struct bpf_map *map, void *key)
1112 {
1113 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1114 	struct hlist_nulls_head *head;
1115 	struct bucket *b;
1116 	struct htab_elem *l;
1117 	unsigned long flags;
1118 	u32 hash, key_size;
1119 	int ret = -ENOENT;
1120 
1121 	WARN_ON_ONCE(!rcu_read_lock_held());
1122 
1123 	key_size = map->key_size;
1124 
1125 	hash = htab_map_hash(key, key_size, htab->hashrnd);
1126 	b = __select_bucket(htab, hash);
1127 	head = &b->head;
1128 
1129 	raw_spin_lock_irqsave(&b->lock, flags);
1130 
1131 	l = lookup_elem_raw(head, hash, key, key_size);
1132 
1133 	if (l) {
1134 		hlist_nulls_del_rcu(&l->hash_node);
1135 		free_htab_elem(htab, l);
1136 		ret = 0;
1137 	}
1138 
1139 	raw_spin_unlock_irqrestore(&b->lock, flags);
1140 	return ret;
1141 }
1142 
1143 static int htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1144 {
1145 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1146 	struct hlist_nulls_head *head;
1147 	struct bucket *b;
1148 	struct htab_elem *l;
1149 	unsigned long flags;
1150 	u32 hash, key_size;
1151 	int ret = -ENOENT;
1152 
1153 	WARN_ON_ONCE(!rcu_read_lock_held());
1154 
1155 	key_size = map->key_size;
1156 
1157 	hash = htab_map_hash(key, key_size, htab->hashrnd);
1158 	b = __select_bucket(htab, hash);
1159 	head = &b->head;
1160 
1161 	raw_spin_lock_irqsave(&b->lock, flags);
1162 
1163 	l = lookup_elem_raw(head, hash, key, key_size);
1164 
1165 	if (l) {
1166 		hlist_nulls_del_rcu(&l->hash_node);
1167 		ret = 0;
1168 	}
1169 
1170 	raw_spin_unlock_irqrestore(&b->lock, flags);
1171 	if (l)
1172 		bpf_lru_push_free(&htab->lru, &l->lru_node);
1173 	return ret;
1174 }
1175 
1176 static void delete_all_elements(struct bpf_htab *htab)
1177 {
1178 	int i;
1179 
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;
1184 
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);
1188 		}
1189 	}
1190 }
1191 
1192 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
1193 static void htab_map_free(struct bpf_map *map)
1194 {
1195 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1196 
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
1201 	 */
1202 	synchronize_rcu();
1203 
1204 	/* some of free_htab_elem() callbacks for elements of this map may
1205 	 * not have executed. Wait for them.
1206 	 */
1207 	rcu_barrier();
1208 	if (!htab_is_prealloc(htab))
1209 		delete_all_elements(htab);
1210 	else
1211 		prealloc_destroy(htab);
1212 
1213 	free_percpu(htab->extra_elems);
1214 	bpf_map_area_free(htab->buckets);
1215 	kfree(htab);
1216 }
1217 
1218 static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1219 				   struct seq_file *m)
1220 {
1221 	void *value;
1222 
1223 	rcu_read_lock();
1224 
1225 	value = htab_map_lookup_elem(map, key);
1226 	if (!value) {
1227 		rcu_read_unlock();
1228 		return;
1229 	}
1230 
1231 	btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1232 	seq_puts(m, ": ");
1233 	btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1234 	seq_puts(m, "\n");
1235 
1236 	rcu_read_unlock();
1237 }
1238 
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,
1249 };
1250 
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,
1262 };
1263 
1264 /* Called from eBPF program */
1265 static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1266 {
1267 	struct htab_elem *l = __htab_map_lookup_elem(map, key);
1268 
1269 	if (l)
1270 		return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1271 	else
1272 		return NULL;
1273 }
1274 
1275 static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
1276 {
1277 	struct htab_elem *l = __htab_map_lookup_elem(map, key);
1278 
1279 	if (l) {
1280 		bpf_lru_node_set_ref(&l->lru_node);
1281 		return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
1282 	}
1283 
1284 	return NULL;
1285 }
1286 
1287 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
1288 {
1289 	struct htab_elem *l;
1290 	void __percpu *pptr;
1291 	int ret = -ENOENT;
1292 	int cpu, off = 0;
1293 	u32 size;
1294 
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
1298 	 */
1299 	size = round_up(map->value_size, 8);
1300 	rcu_read_lock();
1301 	l = __htab_map_lookup_elem(map, key);
1302 	if (!l)
1303 		goto out;
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.
1306 	 */
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);
1311 		off += size;
1312 	}
1313 	ret = 0;
1314 out:
1315 	rcu_read_unlock();
1316 	return ret;
1317 }
1318 
1319 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
1320 			   u64 map_flags)
1321 {
1322 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1323 	int ret;
1324 
1325 	rcu_read_lock();
1326 	if (htab_is_lru(htab))
1327 		ret = __htab_lru_percpu_map_update_elem(map, key, value,
1328 							map_flags, true);
1329 	else
1330 		ret = __htab_percpu_map_update_elem(map, key, value, map_flags,
1331 						    true);
1332 	rcu_read_unlock();
1333 
1334 	return ret;
1335 }
1336 
1337 static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
1338 					  struct seq_file *m)
1339 {
1340 	struct htab_elem *l;
1341 	void __percpu *pptr;
1342 	int cpu;
1343 
1344 	rcu_read_lock();
1345 
1346 	l = __htab_map_lookup_elem(map, key);
1347 	if (!l) {
1348 		rcu_read_unlock();
1349 		return;
1350 	}
1351 
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);
1359 		seq_puts(m, "\n");
1360 	}
1361 	seq_puts(m, "}\n");
1362 
1363 	rcu_read_unlock();
1364 }
1365 
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,
1375 };
1376 
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,
1386 };
1387 
1388 static int fd_htab_map_alloc_check(union bpf_attr *attr)
1389 {
1390 	if (attr->value_size != sizeof(u32))
1391 		return -EINVAL;
1392 	return htab_map_alloc_check(attr);
1393 }
1394 
1395 static void fd_htab_map_free(struct bpf_map *map)
1396 {
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;
1401 	int i;
1402 
1403 	for (i = 0; i < htab->n_buckets; i++) {
1404 		head = select_bucket(htab, i);
1405 
1406 		hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1407 			void *ptr = fd_htab_map_get_ptr(map, l);
1408 
1409 			map->ops->map_fd_put_ptr(ptr);
1410 		}
1411 	}
1412 
1413 	htab_map_free(map);
1414 }
1415 
1416 /* only called from syscall */
1417 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
1418 {
1419 	void **ptr;
1420 	int ret = 0;
1421 
1422 	if (!map->ops->map_fd_sys_lookup_elem)
1423 		return -ENOTSUPP;
1424 
1425 	rcu_read_lock();
1426 	ptr = htab_map_lookup_elem(map, key);
1427 	if (ptr)
1428 		*value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
1429 	else
1430 		ret = -ENOENT;
1431 	rcu_read_unlock();
1432 
1433 	return ret;
1434 }
1435 
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)
1439 {
1440 	void *ptr;
1441 	int ret;
1442 	u32 ufd = *(u32 *)value;
1443 
1444 	ptr = map->ops->map_fd_get_ptr(map, map_file, ufd);
1445 	if (IS_ERR(ptr))
1446 		return PTR_ERR(ptr);
1447 
1448 	ret = htab_map_update_elem(map, key, &ptr, map_flags);
1449 	if (ret)
1450 		map->ops->map_fd_put_ptr(ptr);
1451 
1452 	return ret;
1453 }
1454 
1455 static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
1456 {
1457 	struct bpf_map *map, *inner_map_meta;
1458 
1459 	inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
1460 	if (IS_ERR(inner_map_meta))
1461 		return inner_map_meta;
1462 
1463 	map = htab_map_alloc(attr);
1464 	if (IS_ERR(map)) {
1465 		bpf_map_meta_free(inner_map_meta);
1466 		return map;
1467 	}
1468 
1469 	map->inner_map_meta = inner_map_meta;
1470 
1471 	return map;
1472 }
1473 
1474 static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
1475 {
1476 	struct bpf_map **inner_map  = htab_map_lookup_elem(map, key);
1477 
1478 	if (!inner_map)
1479 		return NULL;
1480 
1481 	return READ_ONCE(*inner_map);
1482 }
1483 
1484 static u32 htab_of_map_gen_lookup(struct bpf_map *map,
1485 				  struct bpf_insn *insn_buf)
1486 {
1487 	struct bpf_insn *insn = insn_buf;
1488 	const int ret = BPF_REG_0;
1489 
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);
1498 
1499 	return insn - insn_buf;
1500 }
1501 
1502 static void htab_of_map_free(struct bpf_map *map)
1503 {
1504 	bpf_map_meta_free(map->inner_map_meta);
1505 	fd_htab_map_free(map);
1506 }
1507 
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,
1520 };
1521