xref: /linux/kernel/bpf/hashtab.c (revision 85502b2214d50ba0ddf2a5fb454e4d28a160d175)
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/rcupdate_wait.h>
11 #include <linux/random.h>
12 #include <uapi/linux/btf.h>
13 #include <linux/rcupdate_trace.h>
14 #include <linux/btf_ids.h>
15 #include "percpu_freelist.h"
16 #include "bpf_lru_list.h"
17 #include "map_in_map.h"
18 #include <linux/bpf_mem_alloc.h>
19 #include <asm/rqspinlock.h>
20 
21 #define HTAB_CREATE_FLAG_MASK						\
22 	(BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE |	\
23 	 BPF_F_ACCESS_MASK | BPF_F_ZERO_SEED)
24 
25 #define BATCH_OPS(_name)			\
26 	.map_lookup_batch =			\
27 	_name##_map_lookup_batch,		\
28 	.map_lookup_and_delete_batch =		\
29 	_name##_map_lookup_and_delete_batch,	\
30 	.map_update_batch =			\
31 	generic_map_update_batch,		\
32 	.map_delete_batch =			\
33 	generic_map_delete_batch
34 
35 /*
36  * The bucket lock has two protection scopes:
37  *
38  * 1) Serializing concurrent operations from BPF programs on different
39  *    CPUs
40  *
41  * 2) Serializing concurrent operations from BPF programs and sys_bpf()
42  *
43  * BPF programs can execute in any context including perf, kprobes and
44  * tracing. As there are almost no limits where perf, kprobes and tracing
45  * can be invoked from the lock operations need to be protected against
46  * deadlocks. Deadlocks can be caused by recursion and by an invocation in
47  * the lock held section when functions which acquire this lock are invoked
48  * from sys_bpf(). BPF recursion is prevented by incrementing the per CPU
49  * variable bpf_prog_active, which prevents BPF programs attached to perf
50  * events, kprobes and tracing to be invoked before the prior invocation
51  * from one of these contexts completed. sys_bpf() uses the same mechanism
52  * by pinning the task to the current CPU and incrementing the recursion
53  * protection across the map operation.
54  *
55  * This has subtle implications on PREEMPT_RT. PREEMPT_RT forbids certain
56  * operations like memory allocations (even with GFP_ATOMIC) from atomic
57  * contexts. This is required because even with GFP_ATOMIC the memory
58  * allocator calls into code paths which acquire locks with long held lock
59  * sections. To ensure the deterministic behaviour these locks are regular
60  * spinlocks, which are converted to 'sleepable' spinlocks on RT. The only
61  * true atomic contexts on an RT kernel are the low level hardware
62  * handling, scheduling, low level interrupt handling, NMIs etc. None of
63  * these contexts should ever do memory allocations.
64  *
65  * As regular device interrupt handlers and soft interrupts are forced into
66  * thread context, the existing code which does
67  *   spin_lock*(); alloc(GFP_ATOMIC); spin_unlock*();
68  * just works.
69  *
70  * In theory the BPF locks could be converted to regular spinlocks as well,
71  * but the bucket locks and percpu_freelist locks can be taken from
72  * arbitrary contexts (perf, kprobes, tracepoints) which are required to be
73  * atomic contexts even on RT. Before the introduction of bpf_mem_alloc,
74  * it is only safe to use raw spinlock for preallocated hash map on a RT kernel,
75  * because there is no memory allocation within the lock held sections. However
76  * after hash map was fully converted to use bpf_mem_alloc, there will be
77  * non-synchronous memory allocation for non-preallocated hash map, so it is
78  * safe to always use raw spinlock for bucket lock.
79  */
80 struct bucket {
81 	struct hlist_nulls_head head;
82 	rqspinlock_t raw_lock;
83 };
84 
85 #define HASHTAB_MAP_LOCK_COUNT 8
86 #define HASHTAB_MAP_LOCK_MASK (HASHTAB_MAP_LOCK_COUNT - 1)
87 
88 struct bpf_htab {
89 	struct bpf_map map;
90 	struct bpf_mem_alloc ma;
91 	struct bpf_mem_alloc pcpu_ma;
92 	struct bucket *buckets;
93 	void *elems;
94 	union {
95 		struct pcpu_freelist freelist;
96 		struct bpf_lru lru;
97 	};
98 	struct htab_elem *__percpu *extra_elems;
99 	/* number of elements in non-preallocated hashtable are kept
100 	 * in either pcount or count
101 	 */
102 	struct percpu_counter pcount;
103 	atomic_t count;
104 	bool use_percpu_counter;
105 	u32 n_buckets;	/* number of hash buckets */
106 	u32 elem_size;	/* size of each element in bytes */
107 	u32 hashrnd;
108 };
109 
110 /* each htab element is struct htab_elem + key + value */
111 struct htab_elem {
112 	union {
113 		struct hlist_nulls_node hash_node;
114 		struct {
115 			void *padding;
116 			union {
117 				struct pcpu_freelist_node fnode;
118 				struct htab_elem *batch_flink;
119 			};
120 		};
121 	};
122 	union {
123 		/* pointer to per-cpu pointer */
124 		void *ptr_to_pptr;
125 		struct bpf_lru_node lru_node;
126 	};
127 	u32 hash;
128 	char key[] __aligned(8);
129 };
130 
htab_is_prealloc(const struct bpf_htab * htab)131 static inline bool htab_is_prealloc(const struct bpf_htab *htab)
132 {
133 	return !(htab->map.map_flags & BPF_F_NO_PREALLOC);
134 }
135 
htab_init_buckets(struct bpf_htab * htab)136 static void htab_init_buckets(struct bpf_htab *htab)
137 {
138 	unsigned int i;
139 
140 	for (i = 0; i < htab->n_buckets; i++) {
141 		INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
142 		raw_res_spin_lock_init(&htab->buckets[i].raw_lock);
143 		cond_resched();
144 	}
145 }
146 
htab_lock_bucket(struct bucket * b,unsigned long * pflags)147 static inline int htab_lock_bucket(struct bucket *b, unsigned long *pflags)
148 {
149 	unsigned long flags;
150 	int ret;
151 
152 	ret = raw_res_spin_lock_irqsave(&b->raw_lock, flags);
153 	if (ret)
154 		return ret;
155 	*pflags = flags;
156 	return 0;
157 }
158 
htab_unlock_bucket(struct bucket * b,unsigned long flags)159 static inline void htab_unlock_bucket(struct bucket *b, unsigned long flags)
160 {
161 	raw_res_spin_unlock_irqrestore(&b->raw_lock, flags);
162 }
163 
164 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node);
165 
htab_is_lru(const struct bpf_htab * htab)166 static bool htab_is_lru(const struct bpf_htab *htab)
167 {
168 	return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH ||
169 		htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
170 }
171 
htab_is_percpu(const struct bpf_htab * htab)172 static bool htab_is_percpu(const struct bpf_htab *htab)
173 {
174 	return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH ||
175 		htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
176 }
177 
is_fd_htab(const struct bpf_htab * htab)178 static inline bool is_fd_htab(const struct bpf_htab *htab)
179 {
180 	return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS;
181 }
182 
htab_elem_value(struct htab_elem * l,u32 key_size)183 static inline void *htab_elem_value(struct htab_elem *l, u32 key_size)
184 {
185 	return l->key + round_up(key_size, 8);
186 }
187 
htab_elem_set_ptr(struct htab_elem * l,u32 key_size,void __percpu * pptr)188 static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size,
189 				     void __percpu *pptr)
190 {
191 	*(void __percpu **)htab_elem_value(l, key_size) = pptr;
192 }
193 
htab_elem_get_ptr(struct htab_elem * l,u32 key_size)194 static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size)
195 {
196 	return *(void __percpu **)htab_elem_value(l, key_size);
197 }
198 
fd_htab_map_get_ptr(const struct bpf_map * map,struct htab_elem * l)199 static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l)
200 {
201 	return *(void **)htab_elem_value(l, map->key_size);
202 }
203 
get_htab_elem(struct bpf_htab * htab,int i)204 static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i)
205 {
206 	return (struct htab_elem *) (htab->elems + i * (u64)htab->elem_size);
207 }
208 
209 /* Both percpu and fd htab support in-place update, so no need for
210  * extra elem. LRU itself can remove the least used element, so
211  * there is no need for an extra elem during map_update.
212  */
htab_has_extra_elems(struct bpf_htab * htab)213 static bool htab_has_extra_elems(struct bpf_htab *htab)
214 {
215 	return !htab_is_percpu(htab) && !htab_is_lru(htab) && !is_fd_htab(htab);
216 }
217 
htab_free_prealloced_timers_and_wq(struct bpf_htab * htab)218 static void htab_free_prealloced_timers_and_wq(struct bpf_htab *htab)
219 {
220 	u32 num_entries = htab->map.max_entries;
221 	int i;
222 
223 	if (htab_has_extra_elems(htab))
224 		num_entries += num_possible_cpus();
225 
226 	for (i = 0; i < num_entries; i++) {
227 		struct htab_elem *elem;
228 
229 		elem = get_htab_elem(htab, i);
230 		if (btf_record_has_field(htab->map.record, BPF_TIMER))
231 			bpf_obj_free_timer(htab->map.record,
232 					   htab_elem_value(elem, htab->map.key_size));
233 		if (btf_record_has_field(htab->map.record, BPF_WORKQUEUE))
234 			bpf_obj_free_workqueue(htab->map.record,
235 					       htab_elem_value(elem, htab->map.key_size));
236 		cond_resched();
237 	}
238 }
239 
htab_free_prealloced_fields(struct bpf_htab * htab)240 static void htab_free_prealloced_fields(struct bpf_htab *htab)
241 {
242 	u32 num_entries = htab->map.max_entries;
243 	int i;
244 
245 	if (IS_ERR_OR_NULL(htab->map.record))
246 		return;
247 	if (htab_has_extra_elems(htab))
248 		num_entries += num_possible_cpus();
249 	for (i = 0; i < num_entries; i++) {
250 		struct htab_elem *elem;
251 
252 		elem = get_htab_elem(htab, i);
253 		if (htab_is_percpu(htab)) {
254 			void __percpu *pptr = htab_elem_get_ptr(elem, htab->map.key_size);
255 			int cpu;
256 
257 			for_each_possible_cpu(cpu) {
258 				bpf_obj_free_fields(htab->map.record, per_cpu_ptr(pptr, cpu));
259 				cond_resched();
260 			}
261 		} else {
262 			bpf_obj_free_fields(htab->map.record,
263 					    htab_elem_value(elem, htab->map.key_size));
264 			cond_resched();
265 		}
266 		cond_resched();
267 	}
268 }
269 
htab_free_elems(struct bpf_htab * htab)270 static void htab_free_elems(struct bpf_htab *htab)
271 {
272 	int i;
273 
274 	if (!htab_is_percpu(htab))
275 		goto free_elems;
276 
277 	for (i = 0; i < htab->map.max_entries; i++) {
278 		void __percpu *pptr;
279 
280 		pptr = htab_elem_get_ptr(get_htab_elem(htab, i),
281 					 htab->map.key_size);
282 		free_percpu(pptr);
283 		cond_resched();
284 	}
285 free_elems:
286 	bpf_map_area_free(htab->elems);
287 }
288 
289 /* The LRU list has a lock (lru_lock). Each htab bucket has a lock
290  * (bucket_lock). If both locks need to be acquired together, the lock
291  * order is always lru_lock -> bucket_lock and this only happens in
292  * bpf_lru_list.c logic. For example, certain code path of
293  * bpf_lru_pop_free(), which is called by function prealloc_lru_pop(),
294  * will acquire lru_lock first followed by acquiring bucket_lock.
295  *
296  * In hashtab.c, to avoid deadlock, lock acquisition of
297  * bucket_lock followed by lru_lock is not allowed. In such cases,
298  * bucket_lock needs to be released first before acquiring lru_lock.
299  */
prealloc_lru_pop(struct bpf_htab * htab,void * key,u32 hash)300 static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key,
301 					  u32 hash)
302 {
303 	struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash);
304 	struct htab_elem *l;
305 
306 	if (node) {
307 		bpf_map_inc_elem_count(&htab->map);
308 		l = container_of(node, struct htab_elem, lru_node);
309 		memcpy(l->key, key, htab->map.key_size);
310 		return l;
311 	}
312 
313 	return NULL;
314 }
315 
prealloc_init(struct bpf_htab * htab)316 static int prealloc_init(struct bpf_htab *htab)
317 {
318 	u32 num_entries = htab->map.max_entries;
319 	int err = -ENOMEM, i;
320 
321 	if (htab_has_extra_elems(htab))
322 		num_entries += num_possible_cpus();
323 
324 	htab->elems = bpf_map_area_alloc((u64)htab->elem_size * num_entries,
325 					 htab->map.numa_node);
326 	if (!htab->elems)
327 		return -ENOMEM;
328 
329 	if (!htab_is_percpu(htab))
330 		goto skip_percpu_elems;
331 
332 	for (i = 0; i < num_entries; i++) {
333 		u32 size = round_up(htab->map.value_size, 8);
334 		void __percpu *pptr;
335 
336 		pptr = bpf_map_alloc_percpu(&htab->map, size, 8,
337 					    GFP_USER | __GFP_NOWARN);
338 		if (!pptr)
339 			goto free_elems;
340 		htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size,
341 				  pptr);
342 		cond_resched();
343 	}
344 
345 skip_percpu_elems:
346 	if (htab_is_lru(htab))
347 		err = bpf_lru_init(&htab->lru,
348 				   htab->map.map_flags & BPF_F_NO_COMMON_LRU,
349 				   offsetof(struct htab_elem, hash) -
350 				   offsetof(struct htab_elem, lru_node),
351 				   htab_lru_map_delete_node,
352 				   htab);
353 	else
354 		err = pcpu_freelist_init(&htab->freelist);
355 
356 	if (err)
357 		goto free_elems;
358 
359 	if (htab_is_lru(htab))
360 		bpf_lru_populate(&htab->lru, htab->elems,
361 				 offsetof(struct htab_elem, lru_node),
362 				 htab->elem_size, num_entries);
363 	else
364 		pcpu_freelist_populate(&htab->freelist,
365 				       htab->elems + offsetof(struct htab_elem, fnode),
366 				       htab->elem_size, num_entries);
367 
368 	return 0;
369 
370 free_elems:
371 	htab_free_elems(htab);
372 	return err;
373 }
374 
prealloc_destroy(struct bpf_htab * htab)375 static void prealloc_destroy(struct bpf_htab *htab)
376 {
377 	htab_free_elems(htab);
378 
379 	if (htab_is_lru(htab))
380 		bpf_lru_destroy(&htab->lru);
381 	else
382 		pcpu_freelist_destroy(&htab->freelist);
383 }
384 
alloc_extra_elems(struct bpf_htab * htab)385 static int alloc_extra_elems(struct bpf_htab *htab)
386 {
387 	struct htab_elem *__percpu *pptr, *l_new;
388 	struct pcpu_freelist_node *l;
389 	int cpu;
390 
391 	pptr = bpf_map_alloc_percpu(&htab->map, sizeof(struct htab_elem *), 8,
392 				    GFP_USER | __GFP_NOWARN);
393 	if (!pptr)
394 		return -ENOMEM;
395 
396 	for_each_possible_cpu(cpu) {
397 		l = pcpu_freelist_pop(&htab->freelist);
398 		/* pop will succeed, since prealloc_init()
399 		 * preallocated extra num_possible_cpus elements
400 		 */
401 		l_new = container_of(l, struct htab_elem, fnode);
402 		*per_cpu_ptr(pptr, cpu) = l_new;
403 	}
404 	htab->extra_elems = pptr;
405 	return 0;
406 }
407 
408 /* Called from syscall */
htab_map_alloc_check(union bpf_attr * attr)409 static int htab_map_alloc_check(union bpf_attr *attr)
410 {
411 	bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
412 		       attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
413 	bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH ||
414 		    attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
415 	/* percpu_lru means each cpu has its own LRU list.
416 	 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
417 	 * the map's value itself is percpu.  percpu_lru has
418 	 * nothing to do with the map's value.
419 	 */
420 	bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
421 	bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
422 	bool zero_seed = (attr->map_flags & BPF_F_ZERO_SEED);
423 	int numa_node = bpf_map_attr_numa_node(attr);
424 
425 	BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
426 		     offsetof(struct htab_elem, hash_node.pprev));
427 
428 	if (zero_seed && !capable(CAP_SYS_ADMIN))
429 		/* Guard against local DoS, and discourage production use. */
430 		return -EPERM;
431 
432 	if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK ||
433 	    !bpf_map_flags_access_ok(attr->map_flags))
434 		return -EINVAL;
435 
436 	if (!lru && percpu_lru)
437 		return -EINVAL;
438 
439 	if (lru && !prealloc)
440 		return -ENOTSUPP;
441 
442 	if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru))
443 		return -EINVAL;
444 
445 	/* check sanity of attributes.
446 	 * value_size == 0 may be allowed in the future to use map as a set
447 	 */
448 	if (attr->max_entries == 0 || attr->key_size == 0 ||
449 	    attr->value_size == 0)
450 		return -EINVAL;
451 
452 	if ((u64)attr->key_size + attr->value_size >= KMALLOC_MAX_SIZE -
453 	   sizeof(struct htab_elem))
454 		/* if key_size + value_size is bigger, the user space won't be
455 		 * able to access the elements via bpf syscall. This check
456 		 * also makes sure that the elem_size doesn't overflow and it's
457 		 * kmalloc-able later in htab_map_update_elem()
458 		 */
459 		return -E2BIG;
460 	/* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */
461 	if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE)
462 		return -E2BIG;
463 
464 	return 0;
465 }
466 
htab_map_alloc(union bpf_attr * attr)467 static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
468 {
469 	bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
470 		       attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH);
471 	/* percpu_lru means each cpu has its own LRU list.
472 	 * it is different from BPF_MAP_TYPE_PERCPU_HASH where
473 	 * the map's value itself is percpu.  percpu_lru has
474 	 * nothing to do with the map's value.
475 	 */
476 	bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
477 	bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
478 	struct bpf_htab *htab;
479 	int err;
480 
481 	htab = bpf_map_area_alloc(sizeof(*htab), NUMA_NO_NODE);
482 	if (!htab)
483 		return ERR_PTR(-ENOMEM);
484 
485 	bpf_map_init_from_attr(&htab->map, attr);
486 
487 	if (percpu_lru) {
488 		/* ensure each CPU's lru list has >=1 elements.
489 		 * since we are at it, make each lru list has the same
490 		 * number of elements.
491 		 */
492 		htab->map.max_entries = roundup(attr->max_entries,
493 						num_possible_cpus());
494 		if (htab->map.max_entries < attr->max_entries)
495 			htab->map.max_entries = rounddown(attr->max_entries,
496 							  num_possible_cpus());
497 	}
498 
499 	/* hash table size must be power of 2; roundup_pow_of_two() can overflow
500 	 * into UB on 32-bit arches, so check that first
501 	 */
502 	err = -E2BIG;
503 	if (htab->map.max_entries > 1UL << 31)
504 		goto free_htab;
505 
506 	htab->n_buckets = roundup_pow_of_two(htab->map.max_entries);
507 
508 	htab->elem_size = sizeof(struct htab_elem) +
509 			  round_up(htab->map.key_size, 8);
510 	if (percpu)
511 		htab->elem_size += sizeof(void *);
512 	else
513 		htab->elem_size += round_up(htab->map.value_size, 8);
514 
515 	/* check for u32 overflow */
516 	if (htab->n_buckets > U32_MAX / sizeof(struct bucket))
517 		goto free_htab;
518 
519 	err = bpf_map_init_elem_count(&htab->map);
520 	if (err)
521 		goto free_htab;
522 
523 	err = -ENOMEM;
524 	htab->buckets = bpf_map_area_alloc(htab->n_buckets *
525 					   sizeof(struct bucket),
526 					   htab->map.numa_node);
527 	if (!htab->buckets)
528 		goto free_elem_count;
529 
530 	if (htab->map.map_flags & BPF_F_ZERO_SEED)
531 		htab->hashrnd = 0;
532 	else
533 		htab->hashrnd = get_random_u32();
534 
535 	htab_init_buckets(htab);
536 
537 /* compute_batch_value() computes batch value as num_online_cpus() * 2
538  * and __percpu_counter_compare() needs
539  * htab->max_entries - cur_number_of_elems to be more than batch * num_online_cpus()
540  * for percpu_counter to be faster than atomic_t. In practice the average bpf
541  * hash map size is 10k, which means that a system with 64 cpus will fill
542  * hashmap to 20% of 10k before percpu_counter becomes ineffective. Therefore
543  * define our own batch count as 32 then 10k hash map can be filled up to 80%:
544  * 10k - 8k > 32 _batch_ * 64 _cpus_
545  * and __percpu_counter_compare() will still be fast. At that point hash map
546  * collisions will dominate its performance anyway. Assume that hash map filled
547  * to 50+% isn't going to be O(1) and use the following formula to choose
548  * between percpu_counter and atomic_t.
549  */
550 #define PERCPU_COUNTER_BATCH 32
551 	if (attr->max_entries / 2 > num_online_cpus() * PERCPU_COUNTER_BATCH)
552 		htab->use_percpu_counter = true;
553 
554 	if (htab->use_percpu_counter) {
555 		err = percpu_counter_init(&htab->pcount, 0, GFP_KERNEL);
556 		if (err)
557 			goto free_map_locked;
558 	}
559 
560 	if (prealloc) {
561 		err = prealloc_init(htab);
562 		if (err)
563 			goto free_map_locked;
564 
565 		if (htab_has_extra_elems(htab)) {
566 			err = alloc_extra_elems(htab);
567 			if (err)
568 				goto free_prealloc;
569 		}
570 	} else {
571 		err = bpf_mem_alloc_init(&htab->ma, htab->elem_size, false);
572 		if (err)
573 			goto free_map_locked;
574 		if (percpu) {
575 			err = bpf_mem_alloc_init(&htab->pcpu_ma,
576 						 round_up(htab->map.value_size, 8), true);
577 			if (err)
578 				goto free_map_locked;
579 		}
580 	}
581 
582 	return &htab->map;
583 
584 free_prealloc:
585 	prealloc_destroy(htab);
586 free_map_locked:
587 	if (htab->use_percpu_counter)
588 		percpu_counter_destroy(&htab->pcount);
589 	bpf_map_area_free(htab->buckets);
590 	bpf_mem_alloc_destroy(&htab->pcpu_ma);
591 	bpf_mem_alloc_destroy(&htab->ma);
592 free_elem_count:
593 	bpf_map_free_elem_count(&htab->map);
594 free_htab:
595 	bpf_map_area_free(htab);
596 	return ERR_PTR(err);
597 }
598 
htab_map_hash(const void * key,u32 key_len,u32 hashrnd)599 static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd)
600 {
601 	if (likely(key_len % 4 == 0))
602 		return jhash2(key, key_len / 4, hashrnd);
603 	return jhash(key, key_len, hashrnd);
604 }
605 
__select_bucket(struct bpf_htab * htab,u32 hash)606 static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash)
607 {
608 	return &htab->buckets[hash & (htab->n_buckets - 1)];
609 }
610 
select_bucket(struct bpf_htab * htab,u32 hash)611 static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash)
612 {
613 	return &__select_bucket(htab, hash)->head;
614 }
615 
616 /* this lookup function can only be called with bucket lock taken */
lookup_elem_raw(struct hlist_nulls_head * head,u32 hash,void * key,u32 key_size)617 static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash,
618 					 void *key, u32 key_size)
619 {
620 	struct hlist_nulls_node *n;
621 	struct htab_elem *l;
622 
623 	hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
624 		if (l->hash == hash && !memcmp(&l->key, key, key_size))
625 			return l;
626 
627 	return NULL;
628 }
629 
630 /* can be called without bucket lock. it will repeat the loop in
631  * the unlikely event when elements moved from one bucket into another
632  * while link list is being walked
633  */
lookup_nulls_elem_raw(struct hlist_nulls_head * head,u32 hash,void * key,u32 key_size,u32 n_buckets)634 static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head,
635 					       u32 hash, void *key,
636 					       u32 key_size, u32 n_buckets)
637 {
638 	struct hlist_nulls_node *n;
639 	struct htab_elem *l;
640 
641 again:
642 	hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
643 		if (l->hash == hash && !memcmp(&l->key, key, key_size))
644 			return l;
645 
646 	if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1))))
647 		goto again;
648 
649 	return NULL;
650 }
651 
652 /* Called from syscall or from eBPF program directly, so
653  * arguments have to match bpf_map_lookup_elem() exactly.
654  * The return value is adjusted by BPF instructions
655  * in htab_map_gen_lookup().
656  */
__htab_map_lookup_elem(struct bpf_map * map,void * key)657 static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
658 {
659 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
660 	struct hlist_nulls_head *head;
661 	struct htab_elem *l;
662 	u32 hash, key_size;
663 
664 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
665 		     !rcu_read_lock_bh_held());
666 
667 	key_size = map->key_size;
668 
669 	hash = htab_map_hash(key, key_size, htab->hashrnd);
670 
671 	head = select_bucket(htab, hash);
672 
673 	l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
674 
675 	return l;
676 }
677 
htab_map_lookup_elem(struct bpf_map * map,void * key)678 static void *htab_map_lookup_elem(struct bpf_map *map, void *key)
679 {
680 	struct htab_elem *l = __htab_map_lookup_elem(map, key);
681 
682 	if (l)
683 		return htab_elem_value(l, map->key_size);
684 
685 	return NULL;
686 }
687 
688 /* inline bpf_map_lookup_elem() call.
689  * Instead of:
690  * bpf_prog
691  *   bpf_map_lookup_elem
692  *     map->ops->map_lookup_elem
693  *       htab_map_lookup_elem
694  *         __htab_map_lookup_elem
695  * do:
696  * bpf_prog
697  *   __htab_map_lookup_elem
698  */
htab_map_gen_lookup(struct bpf_map * map,struct bpf_insn * insn_buf)699 static int htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
700 {
701 	struct bpf_insn *insn = insn_buf;
702 	const int ret = BPF_REG_0;
703 
704 	BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
705 		     (void *(*)(struct bpf_map *map, void *key))NULL));
706 	*insn++ = BPF_EMIT_CALL(__htab_map_lookup_elem);
707 	*insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1);
708 	*insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
709 				offsetof(struct htab_elem, key) +
710 				round_up(map->key_size, 8));
711 	return insn - insn_buf;
712 }
713 
__htab_lru_map_lookup_elem(struct bpf_map * map,void * key,const bool mark)714 static __always_inline void *__htab_lru_map_lookup_elem(struct bpf_map *map,
715 							void *key, const bool mark)
716 {
717 	struct htab_elem *l = __htab_map_lookup_elem(map, key);
718 
719 	if (l) {
720 		if (mark)
721 			bpf_lru_node_set_ref(&l->lru_node);
722 		return htab_elem_value(l, map->key_size);
723 	}
724 
725 	return NULL;
726 }
727 
htab_lru_map_lookup_elem(struct bpf_map * map,void * key)728 static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key)
729 {
730 	return __htab_lru_map_lookup_elem(map, key, true);
731 }
732 
htab_lru_map_lookup_elem_sys(struct bpf_map * map,void * key)733 static void *htab_lru_map_lookup_elem_sys(struct bpf_map *map, void *key)
734 {
735 	return __htab_lru_map_lookup_elem(map, key, false);
736 }
737 
htab_lru_map_gen_lookup(struct bpf_map * map,struct bpf_insn * insn_buf)738 static int htab_lru_map_gen_lookup(struct bpf_map *map,
739 				   struct bpf_insn *insn_buf)
740 {
741 	struct bpf_insn *insn = insn_buf;
742 	const int ret = BPF_REG_0;
743 	const int ref_reg = BPF_REG_1;
744 
745 	BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
746 		     (void *(*)(struct bpf_map *map, void *key))NULL));
747 	*insn++ = BPF_EMIT_CALL(__htab_map_lookup_elem);
748 	*insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4);
749 	*insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret,
750 			      offsetof(struct htab_elem, lru_node) +
751 			      offsetof(struct bpf_lru_node, ref));
752 	*insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1);
753 	*insn++ = BPF_ST_MEM(BPF_B, ret,
754 			     offsetof(struct htab_elem, lru_node) +
755 			     offsetof(struct bpf_lru_node, ref),
756 			     1);
757 	*insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
758 				offsetof(struct htab_elem, key) +
759 				round_up(map->key_size, 8));
760 	return insn - insn_buf;
761 }
762 
check_and_free_fields(struct bpf_htab * htab,struct htab_elem * elem)763 static void check_and_free_fields(struct bpf_htab *htab,
764 				  struct htab_elem *elem)
765 {
766 	if (IS_ERR_OR_NULL(htab->map.record))
767 		return;
768 
769 	if (htab_is_percpu(htab)) {
770 		void __percpu *pptr = htab_elem_get_ptr(elem, htab->map.key_size);
771 		int cpu;
772 
773 		for_each_possible_cpu(cpu)
774 			bpf_obj_free_fields(htab->map.record, per_cpu_ptr(pptr, cpu));
775 	} else {
776 		void *map_value = htab_elem_value(elem, htab->map.key_size);
777 
778 		bpf_obj_free_fields(htab->map.record, map_value);
779 	}
780 }
781 
782 /* It is called from the bpf_lru_list when the LRU needs to delete
783  * older elements from the htab.
784  */
htab_lru_map_delete_node(void * arg,struct bpf_lru_node * node)785 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node)
786 {
787 	struct bpf_htab *htab = arg;
788 	struct htab_elem *l = NULL, *tgt_l;
789 	struct hlist_nulls_head *head;
790 	struct hlist_nulls_node *n;
791 	unsigned long flags;
792 	struct bucket *b;
793 	int ret;
794 
795 	tgt_l = container_of(node, struct htab_elem, lru_node);
796 	b = __select_bucket(htab, tgt_l->hash);
797 	head = &b->head;
798 
799 	ret = htab_lock_bucket(b, &flags);
800 	if (ret)
801 		return false;
802 
803 	hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
804 		if (l == tgt_l) {
805 			hlist_nulls_del_rcu(&l->hash_node);
806 			bpf_map_dec_elem_count(&htab->map);
807 			break;
808 		}
809 
810 	htab_unlock_bucket(b, flags);
811 
812 	if (l == tgt_l)
813 		check_and_free_fields(htab, l);
814 	return l == tgt_l;
815 }
816 
817 /* Called from syscall */
htab_map_get_next_key(struct bpf_map * map,void * key,void * next_key)818 static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
819 {
820 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
821 	struct hlist_nulls_head *head;
822 	struct htab_elem *l, *next_l;
823 	u32 hash, key_size;
824 	int i = 0;
825 
826 	WARN_ON_ONCE(!rcu_read_lock_held());
827 
828 	key_size = map->key_size;
829 
830 	if (!key)
831 		goto find_first_elem;
832 
833 	hash = htab_map_hash(key, key_size, htab->hashrnd);
834 
835 	head = select_bucket(htab, hash);
836 
837 	/* lookup the key */
838 	l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets);
839 
840 	if (!l)
841 		goto find_first_elem;
842 
843 	/* key was found, get next key in the same bucket */
844 	next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)),
845 				  struct htab_elem, hash_node);
846 
847 	if (next_l) {
848 		/* if next elem in this hash list is non-zero, just return it */
849 		memcpy(next_key, next_l->key, key_size);
850 		return 0;
851 	}
852 
853 	/* no more elements in this hash list, go to the next bucket */
854 	i = hash & (htab->n_buckets - 1);
855 	i++;
856 
857 find_first_elem:
858 	/* iterate over buckets */
859 	for (; i < htab->n_buckets; i++) {
860 		head = select_bucket(htab, i);
861 
862 		/* pick first element in the bucket */
863 		next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)),
864 					  struct htab_elem, hash_node);
865 		if (next_l) {
866 			/* if it's not empty, just return it */
867 			memcpy(next_key, next_l->key, key_size);
868 			return 0;
869 		}
870 	}
871 
872 	/* iterated over all buckets and all elements */
873 	return -ENOENT;
874 }
875 
htab_elem_free(struct bpf_htab * htab,struct htab_elem * l)876 static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l)
877 {
878 	check_and_free_fields(htab, l);
879 
880 	if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH)
881 		bpf_mem_cache_free(&htab->pcpu_ma, l->ptr_to_pptr);
882 	bpf_mem_cache_free(&htab->ma, l);
883 }
884 
htab_put_fd_value(struct bpf_htab * htab,struct htab_elem * l)885 static void htab_put_fd_value(struct bpf_htab *htab, struct htab_elem *l)
886 {
887 	struct bpf_map *map = &htab->map;
888 	void *ptr;
889 
890 	if (map->ops->map_fd_put_ptr) {
891 		ptr = fd_htab_map_get_ptr(map, l);
892 		map->ops->map_fd_put_ptr(map, ptr, true);
893 	}
894 }
895 
is_map_full(struct bpf_htab * htab)896 static bool is_map_full(struct bpf_htab *htab)
897 {
898 	if (htab->use_percpu_counter)
899 		return __percpu_counter_compare(&htab->pcount, htab->map.max_entries,
900 						PERCPU_COUNTER_BATCH) >= 0;
901 	return atomic_read(&htab->count) >= htab->map.max_entries;
902 }
903 
inc_elem_count(struct bpf_htab * htab)904 static void inc_elem_count(struct bpf_htab *htab)
905 {
906 	bpf_map_inc_elem_count(&htab->map);
907 
908 	if (htab->use_percpu_counter)
909 		percpu_counter_add_batch(&htab->pcount, 1, PERCPU_COUNTER_BATCH);
910 	else
911 		atomic_inc(&htab->count);
912 }
913 
dec_elem_count(struct bpf_htab * htab)914 static void dec_elem_count(struct bpf_htab *htab)
915 {
916 	bpf_map_dec_elem_count(&htab->map);
917 
918 	if (htab->use_percpu_counter)
919 		percpu_counter_add_batch(&htab->pcount, -1, PERCPU_COUNTER_BATCH);
920 	else
921 		atomic_dec(&htab->count);
922 }
923 
924 
free_htab_elem(struct bpf_htab * htab,struct htab_elem * l)925 static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l)
926 {
927 	htab_put_fd_value(htab, l);
928 
929 	if (htab_is_prealloc(htab)) {
930 		bpf_map_dec_elem_count(&htab->map);
931 		check_and_free_fields(htab, l);
932 		pcpu_freelist_push(&htab->freelist, &l->fnode);
933 	} else {
934 		dec_elem_count(htab);
935 		htab_elem_free(htab, l);
936 	}
937 }
938 
pcpu_copy_value(struct bpf_htab * htab,void __percpu * pptr,void * value,bool onallcpus)939 static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
940 			    void *value, bool onallcpus)
941 {
942 	if (!onallcpus) {
943 		/* copy true value_size bytes */
944 		copy_map_value(&htab->map, this_cpu_ptr(pptr), value);
945 	} else {
946 		u32 size = round_up(htab->map.value_size, 8);
947 		int off = 0, cpu;
948 
949 		for_each_possible_cpu(cpu) {
950 			copy_map_value_long(&htab->map, per_cpu_ptr(pptr, cpu), value + off);
951 			off += size;
952 		}
953 	}
954 }
955 
pcpu_init_value(struct bpf_htab * htab,void __percpu * pptr,void * value,bool onallcpus)956 static void pcpu_init_value(struct bpf_htab *htab, void __percpu *pptr,
957 			    void *value, bool onallcpus)
958 {
959 	/* When not setting the initial value on all cpus, zero-fill element
960 	 * values for other cpus. Otherwise, bpf program has no way to ensure
961 	 * known initial values for cpus other than current one
962 	 * (onallcpus=false always when coming from bpf prog).
963 	 */
964 	if (!onallcpus) {
965 		int current_cpu = raw_smp_processor_id();
966 		int cpu;
967 
968 		for_each_possible_cpu(cpu) {
969 			if (cpu == current_cpu)
970 				copy_map_value_long(&htab->map, per_cpu_ptr(pptr, cpu), value);
971 			else /* Since elem is preallocated, we cannot touch special fields */
972 				zero_map_value(&htab->map, per_cpu_ptr(pptr, cpu));
973 		}
974 	} else {
975 		pcpu_copy_value(htab, pptr, value, onallcpus);
976 	}
977 }
978 
fd_htab_map_needs_adjust(const struct bpf_htab * htab)979 static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
980 {
981 	return is_fd_htab(htab) && BITS_PER_LONG == 64;
982 }
983 
alloc_htab_elem(struct bpf_htab * htab,void * key,void * value,u32 key_size,u32 hash,bool percpu,bool onallcpus,struct htab_elem * old_elem)984 static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
985 					 void *value, u32 key_size, u32 hash,
986 					 bool percpu, bool onallcpus,
987 					 struct htab_elem *old_elem)
988 {
989 	u32 size = htab->map.value_size;
990 	bool prealloc = htab_is_prealloc(htab);
991 	struct htab_elem *l_new, **pl_new;
992 	void __percpu *pptr;
993 
994 	if (prealloc) {
995 		if (old_elem) {
996 			/* if we're updating the existing element,
997 			 * use per-cpu extra elems to avoid freelist_pop/push
998 			 */
999 			pl_new = this_cpu_ptr(htab->extra_elems);
1000 			l_new = *pl_new;
1001 			*pl_new = old_elem;
1002 		} else {
1003 			struct pcpu_freelist_node *l;
1004 
1005 			l = __pcpu_freelist_pop(&htab->freelist);
1006 			if (!l)
1007 				return ERR_PTR(-E2BIG);
1008 			l_new = container_of(l, struct htab_elem, fnode);
1009 			bpf_map_inc_elem_count(&htab->map);
1010 		}
1011 	} else {
1012 		if (is_map_full(htab))
1013 			if (!old_elem)
1014 				/* when map is full and update() is replacing
1015 				 * old element, it's ok to allocate, since
1016 				 * old element will be freed immediately.
1017 				 * Otherwise return an error
1018 				 */
1019 				return ERR_PTR(-E2BIG);
1020 		inc_elem_count(htab);
1021 		l_new = bpf_mem_cache_alloc(&htab->ma);
1022 		if (!l_new) {
1023 			l_new = ERR_PTR(-ENOMEM);
1024 			goto dec_count;
1025 		}
1026 	}
1027 
1028 	memcpy(l_new->key, key, key_size);
1029 	if (percpu) {
1030 		if (prealloc) {
1031 			pptr = htab_elem_get_ptr(l_new, key_size);
1032 		} else {
1033 			/* alloc_percpu zero-fills */
1034 			void *ptr = bpf_mem_cache_alloc(&htab->pcpu_ma);
1035 
1036 			if (!ptr) {
1037 				bpf_mem_cache_free(&htab->ma, l_new);
1038 				l_new = ERR_PTR(-ENOMEM);
1039 				goto dec_count;
1040 			}
1041 			l_new->ptr_to_pptr = ptr;
1042 			pptr = *(void __percpu **)ptr;
1043 		}
1044 
1045 		pcpu_init_value(htab, pptr, value, onallcpus);
1046 
1047 		if (!prealloc)
1048 			htab_elem_set_ptr(l_new, key_size, pptr);
1049 	} else if (fd_htab_map_needs_adjust(htab)) {
1050 		size = round_up(size, 8);
1051 		memcpy(htab_elem_value(l_new, key_size), value, size);
1052 	} else {
1053 		copy_map_value(&htab->map, htab_elem_value(l_new, key_size), value);
1054 	}
1055 
1056 	l_new->hash = hash;
1057 	return l_new;
1058 dec_count:
1059 	dec_elem_count(htab);
1060 	return l_new;
1061 }
1062 
check_flags(struct bpf_htab * htab,struct htab_elem * l_old,u64 map_flags)1063 static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
1064 		       u64 map_flags)
1065 {
1066 	if (l_old && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST)
1067 		/* elem already exists */
1068 		return -EEXIST;
1069 
1070 	if (!l_old && (map_flags & ~BPF_F_LOCK) == BPF_EXIST)
1071 		/* elem doesn't exist, cannot update it */
1072 		return -ENOENT;
1073 
1074 	return 0;
1075 }
1076 
1077 /* Called from syscall or from eBPF program */
htab_map_update_elem(struct bpf_map * map,void * key,void * value,u64 map_flags)1078 static long htab_map_update_elem(struct bpf_map *map, void *key, void *value,
1079 				 u64 map_flags)
1080 {
1081 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1082 	struct htab_elem *l_new, *l_old;
1083 	struct hlist_nulls_head *head;
1084 	unsigned long flags;
1085 	struct bucket *b;
1086 	u32 key_size, hash;
1087 	int ret;
1088 
1089 	if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST))
1090 		/* unknown flags */
1091 		return -EINVAL;
1092 
1093 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1094 		     !rcu_read_lock_bh_held());
1095 
1096 	key_size = map->key_size;
1097 
1098 	hash = htab_map_hash(key, key_size, htab->hashrnd);
1099 
1100 	b = __select_bucket(htab, hash);
1101 	head = &b->head;
1102 
1103 	if (unlikely(map_flags & BPF_F_LOCK)) {
1104 		if (unlikely(!btf_record_has_field(map->record, BPF_SPIN_LOCK)))
1105 			return -EINVAL;
1106 		/* find an element without taking the bucket lock */
1107 		l_old = lookup_nulls_elem_raw(head, hash, key, key_size,
1108 					      htab->n_buckets);
1109 		ret = check_flags(htab, l_old, map_flags);
1110 		if (ret)
1111 			return ret;
1112 		if (l_old) {
1113 			/* grab the element lock and update value in place */
1114 			copy_map_value_locked(map,
1115 					      htab_elem_value(l_old, key_size),
1116 					      value, false);
1117 			return 0;
1118 		}
1119 		/* fall through, grab the bucket lock and lookup again.
1120 		 * 99.9% chance that the element won't be found,
1121 		 * but second lookup under lock has to be done.
1122 		 */
1123 	}
1124 
1125 	ret = htab_lock_bucket(b, &flags);
1126 	if (ret)
1127 		return ret;
1128 
1129 	l_old = lookup_elem_raw(head, hash, key, key_size);
1130 
1131 	ret = check_flags(htab, l_old, map_flags);
1132 	if (ret)
1133 		goto err;
1134 
1135 	if (unlikely(l_old && (map_flags & BPF_F_LOCK))) {
1136 		/* first lookup without the bucket lock didn't find the element,
1137 		 * but second lookup with the bucket lock found it.
1138 		 * This case is highly unlikely, but has to be dealt with:
1139 		 * grab the element lock in addition to the bucket lock
1140 		 * and update element in place
1141 		 */
1142 		copy_map_value_locked(map,
1143 				      htab_elem_value(l_old, key_size),
1144 				      value, false);
1145 		ret = 0;
1146 		goto err;
1147 	}
1148 
1149 	l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false,
1150 				l_old);
1151 	if (IS_ERR(l_new)) {
1152 		/* all pre-allocated elements are in use or memory exhausted */
1153 		ret = PTR_ERR(l_new);
1154 		goto err;
1155 	}
1156 
1157 	/* add new element to the head of the list, so that
1158 	 * concurrent search will find it before old elem
1159 	 */
1160 	hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1161 	if (l_old) {
1162 		hlist_nulls_del_rcu(&l_old->hash_node);
1163 
1164 		/* l_old has already been stashed in htab->extra_elems, free
1165 		 * its special fields before it is available for reuse.
1166 		 */
1167 		if (htab_is_prealloc(htab))
1168 			check_and_free_fields(htab, l_old);
1169 	}
1170 	htab_unlock_bucket(b, flags);
1171 	if (l_old && !htab_is_prealloc(htab))
1172 		free_htab_elem(htab, l_old);
1173 	return 0;
1174 err:
1175 	htab_unlock_bucket(b, flags);
1176 	return ret;
1177 }
1178 
htab_lru_push_free(struct bpf_htab * htab,struct htab_elem * elem)1179 static void htab_lru_push_free(struct bpf_htab *htab, struct htab_elem *elem)
1180 {
1181 	check_and_free_fields(htab, elem);
1182 	bpf_map_dec_elem_count(&htab->map);
1183 	bpf_lru_push_free(&htab->lru, &elem->lru_node);
1184 }
1185 
htab_lru_map_update_elem(struct bpf_map * map,void * key,void * value,u64 map_flags)1186 static long htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value,
1187 				     u64 map_flags)
1188 {
1189 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1190 	struct htab_elem *l_new, *l_old = NULL;
1191 	struct hlist_nulls_head *head;
1192 	unsigned long flags;
1193 	struct bucket *b;
1194 	u32 key_size, hash;
1195 	int ret;
1196 
1197 	if (unlikely(map_flags > BPF_EXIST))
1198 		/* unknown flags */
1199 		return -EINVAL;
1200 
1201 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1202 		     !rcu_read_lock_bh_held());
1203 
1204 	key_size = map->key_size;
1205 
1206 	hash = htab_map_hash(key, key_size, htab->hashrnd);
1207 
1208 	b = __select_bucket(htab, hash);
1209 	head = &b->head;
1210 
1211 	/* For LRU, we need to alloc before taking bucket's
1212 	 * spinlock because getting free nodes from LRU may need
1213 	 * to remove older elements from htab and this removal
1214 	 * operation will need a bucket lock.
1215 	 */
1216 	l_new = prealloc_lru_pop(htab, key, hash);
1217 	if (!l_new)
1218 		return -ENOMEM;
1219 	copy_map_value(&htab->map, htab_elem_value(l_new, map->key_size), value);
1220 
1221 	ret = htab_lock_bucket(b, &flags);
1222 	if (ret)
1223 		goto err_lock_bucket;
1224 
1225 	l_old = lookup_elem_raw(head, hash, key, key_size);
1226 
1227 	ret = check_flags(htab, l_old, map_flags);
1228 	if (ret)
1229 		goto err;
1230 
1231 	/* add new element to the head of the list, so that
1232 	 * concurrent search will find it before old elem
1233 	 */
1234 	hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1235 	if (l_old) {
1236 		bpf_lru_node_set_ref(&l_new->lru_node);
1237 		hlist_nulls_del_rcu(&l_old->hash_node);
1238 	}
1239 	ret = 0;
1240 
1241 err:
1242 	htab_unlock_bucket(b, flags);
1243 
1244 err_lock_bucket:
1245 	if (ret)
1246 		htab_lru_push_free(htab, l_new);
1247 	else if (l_old)
1248 		htab_lru_push_free(htab, l_old);
1249 
1250 	return ret;
1251 }
1252 
htab_map_update_elem_in_place(struct bpf_map * map,void * key,void * value,u64 map_flags,bool percpu,bool onallcpus)1253 static long htab_map_update_elem_in_place(struct bpf_map *map, void *key,
1254 					  void *value, u64 map_flags,
1255 					  bool percpu, bool onallcpus)
1256 {
1257 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1258 	struct htab_elem *l_new, *l_old;
1259 	struct hlist_nulls_head *head;
1260 	void *old_map_ptr = NULL;
1261 	unsigned long flags;
1262 	struct bucket *b;
1263 	u32 key_size, hash;
1264 	int ret;
1265 
1266 	if (unlikely(map_flags > BPF_EXIST))
1267 		/* unknown flags */
1268 		return -EINVAL;
1269 
1270 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1271 		     !rcu_read_lock_bh_held());
1272 
1273 	key_size = map->key_size;
1274 
1275 	hash = htab_map_hash(key, key_size, htab->hashrnd);
1276 
1277 	b = __select_bucket(htab, hash);
1278 	head = &b->head;
1279 
1280 	ret = htab_lock_bucket(b, &flags);
1281 	if (ret)
1282 		return ret;
1283 
1284 	l_old = lookup_elem_raw(head, hash, key, key_size);
1285 
1286 	ret = check_flags(htab, l_old, map_flags);
1287 	if (ret)
1288 		goto err;
1289 
1290 	if (l_old) {
1291 		/* Update value in-place */
1292 		if (percpu) {
1293 			pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1294 					value, onallcpus);
1295 		} else {
1296 			void **inner_map_pptr = htab_elem_value(l_old, key_size);
1297 
1298 			old_map_ptr = *inner_map_pptr;
1299 			WRITE_ONCE(*inner_map_pptr, *(void **)value);
1300 		}
1301 	} else {
1302 		l_new = alloc_htab_elem(htab, key, value, key_size,
1303 					hash, percpu, onallcpus, NULL);
1304 		if (IS_ERR(l_new)) {
1305 			ret = PTR_ERR(l_new);
1306 			goto err;
1307 		}
1308 		hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1309 	}
1310 err:
1311 	htab_unlock_bucket(b, flags);
1312 	if (old_map_ptr)
1313 		map->ops->map_fd_put_ptr(map, old_map_ptr, true);
1314 	return ret;
1315 }
1316 
__htab_lru_percpu_map_update_elem(struct bpf_map * map,void * key,void * value,u64 map_flags,bool onallcpus)1317 static long __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1318 					      void *value, u64 map_flags,
1319 					      bool onallcpus)
1320 {
1321 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1322 	struct htab_elem *l_new = NULL, *l_old;
1323 	struct hlist_nulls_head *head;
1324 	unsigned long flags;
1325 	struct bucket *b;
1326 	u32 key_size, hash;
1327 	int ret;
1328 
1329 	if (unlikely(map_flags > BPF_EXIST))
1330 		/* unknown flags */
1331 		return -EINVAL;
1332 
1333 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1334 		     !rcu_read_lock_bh_held());
1335 
1336 	key_size = map->key_size;
1337 
1338 	hash = htab_map_hash(key, key_size, htab->hashrnd);
1339 
1340 	b = __select_bucket(htab, hash);
1341 	head = &b->head;
1342 
1343 	/* For LRU, we need to alloc before taking bucket's
1344 	 * spinlock because LRU's elem alloc may need
1345 	 * to remove older elem from htab and this removal
1346 	 * operation will need a bucket lock.
1347 	 */
1348 	if (map_flags != BPF_EXIST) {
1349 		l_new = prealloc_lru_pop(htab, key, hash);
1350 		if (!l_new)
1351 			return -ENOMEM;
1352 	}
1353 
1354 	ret = htab_lock_bucket(b, &flags);
1355 	if (ret)
1356 		goto err_lock_bucket;
1357 
1358 	l_old = lookup_elem_raw(head, hash, key, key_size);
1359 
1360 	ret = check_flags(htab, l_old, map_flags);
1361 	if (ret)
1362 		goto err;
1363 
1364 	if (l_old) {
1365 		bpf_lru_node_set_ref(&l_old->lru_node);
1366 
1367 		/* per-cpu hash map can update value in-place */
1368 		pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
1369 				value, onallcpus);
1370 	} else {
1371 		pcpu_init_value(htab, htab_elem_get_ptr(l_new, key_size),
1372 				value, onallcpus);
1373 		hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1374 		l_new = NULL;
1375 	}
1376 	ret = 0;
1377 err:
1378 	htab_unlock_bucket(b, flags);
1379 err_lock_bucket:
1380 	if (l_new) {
1381 		bpf_map_dec_elem_count(&htab->map);
1382 		bpf_lru_push_free(&htab->lru, &l_new->lru_node);
1383 	}
1384 	return ret;
1385 }
1386 
htab_percpu_map_update_elem(struct bpf_map * map,void * key,void * value,u64 map_flags)1387 static long htab_percpu_map_update_elem(struct bpf_map *map, void *key,
1388 					void *value, u64 map_flags)
1389 {
1390 	return htab_map_update_elem_in_place(map, key, value, map_flags, true, false);
1391 }
1392 
htab_lru_percpu_map_update_elem(struct bpf_map * map,void * key,void * value,u64 map_flags)1393 static long htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
1394 					    void *value, u64 map_flags)
1395 {
1396 	return __htab_lru_percpu_map_update_elem(map, key, value, map_flags,
1397 						 false);
1398 }
1399 
1400 /* Called from syscall or from eBPF program */
htab_map_delete_elem(struct bpf_map * map,void * key)1401 static long htab_map_delete_elem(struct bpf_map *map, void *key)
1402 {
1403 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1404 	struct hlist_nulls_head *head;
1405 	struct bucket *b;
1406 	struct htab_elem *l;
1407 	unsigned long flags;
1408 	u32 hash, key_size;
1409 	int ret;
1410 
1411 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1412 		     !rcu_read_lock_bh_held());
1413 
1414 	key_size = map->key_size;
1415 
1416 	hash = htab_map_hash(key, key_size, htab->hashrnd);
1417 	b = __select_bucket(htab, hash);
1418 	head = &b->head;
1419 
1420 	ret = htab_lock_bucket(b, &flags);
1421 	if (ret)
1422 		return ret;
1423 
1424 	l = lookup_elem_raw(head, hash, key, key_size);
1425 	if (l)
1426 		hlist_nulls_del_rcu(&l->hash_node);
1427 	else
1428 		ret = -ENOENT;
1429 
1430 	htab_unlock_bucket(b, flags);
1431 
1432 	if (l)
1433 		free_htab_elem(htab, l);
1434 	return ret;
1435 }
1436 
htab_lru_map_delete_elem(struct bpf_map * map,void * key)1437 static long htab_lru_map_delete_elem(struct bpf_map *map, void *key)
1438 {
1439 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1440 	struct hlist_nulls_head *head;
1441 	struct bucket *b;
1442 	struct htab_elem *l;
1443 	unsigned long flags;
1444 	u32 hash, key_size;
1445 	int ret;
1446 
1447 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
1448 		     !rcu_read_lock_bh_held());
1449 
1450 	key_size = map->key_size;
1451 
1452 	hash = htab_map_hash(key, key_size, htab->hashrnd);
1453 	b = __select_bucket(htab, hash);
1454 	head = &b->head;
1455 
1456 	ret = htab_lock_bucket(b, &flags);
1457 	if (ret)
1458 		return ret;
1459 
1460 	l = lookup_elem_raw(head, hash, key, key_size);
1461 
1462 	if (l)
1463 		hlist_nulls_del_rcu(&l->hash_node);
1464 	else
1465 		ret = -ENOENT;
1466 
1467 	htab_unlock_bucket(b, flags);
1468 	if (l)
1469 		htab_lru_push_free(htab, l);
1470 	return ret;
1471 }
1472 
delete_all_elements(struct bpf_htab * htab)1473 static void delete_all_elements(struct bpf_htab *htab)
1474 {
1475 	int i;
1476 
1477 	/* It's called from a worker thread and migration has been disabled,
1478 	 * therefore, it is OK to invoke bpf_mem_cache_free() directly.
1479 	 */
1480 	for (i = 0; i < htab->n_buckets; i++) {
1481 		struct hlist_nulls_head *head = select_bucket(htab, i);
1482 		struct hlist_nulls_node *n;
1483 		struct htab_elem *l;
1484 
1485 		hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1486 			hlist_nulls_del_rcu(&l->hash_node);
1487 			htab_elem_free(htab, l);
1488 		}
1489 		cond_resched();
1490 	}
1491 }
1492 
htab_free_malloced_timers_and_wq(struct bpf_htab * htab)1493 static void htab_free_malloced_timers_and_wq(struct bpf_htab *htab)
1494 {
1495 	int i;
1496 
1497 	rcu_read_lock();
1498 	for (i = 0; i < htab->n_buckets; i++) {
1499 		struct hlist_nulls_head *head = select_bucket(htab, i);
1500 		struct hlist_nulls_node *n;
1501 		struct htab_elem *l;
1502 
1503 		hlist_nulls_for_each_entry(l, n, head, hash_node) {
1504 			/* We only free timer on uref dropping to zero */
1505 			if (btf_record_has_field(htab->map.record, BPF_TIMER))
1506 				bpf_obj_free_timer(htab->map.record,
1507 						   htab_elem_value(l, htab->map.key_size));
1508 			if (btf_record_has_field(htab->map.record, BPF_WORKQUEUE))
1509 				bpf_obj_free_workqueue(htab->map.record,
1510 						       htab_elem_value(l, htab->map.key_size));
1511 		}
1512 		cond_resched_rcu();
1513 	}
1514 	rcu_read_unlock();
1515 }
1516 
htab_map_free_timers_and_wq(struct bpf_map * map)1517 static void htab_map_free_timers_and_wq(struct bpf_map *map)
1518 {
1519 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1520 
1521 	/* We only free timer and workqueue on uref dropping to zero */
1522 	if (btf_record_has_field(htab->map.record, BPF_TIMER | BPF_WORKQUEUE)) {
1523 		if (!htab_is_prealloc(htab))
1524 			htab_free_malloced_timers_and_wq(htab);
1525 		else
1526 			htab_free_prealloced_timers_and_wq(htab);
1527 	}
1528 }
1529 
1530 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
htab_map_free(struct bpf_map * map)1531 static void htab_map_free(struct bpf_map *map)
1532 {
1533 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1534 
1535 	/* bpf_free_used_maps() or close(map_fd) will trigger this map_free callback.
1536 	 * bpf_free_used_maps() is called after bpf prog is no longer executing.
1537 	 * There is no need to synchronize_rcu() here to protect map elements.
1538 	 */
1539 
1540 	/* htab no longer uses call_rcu() directly. bpf_mem_alloc does it
1541 	 * underneath and is responsible for waiting for callbacks to finish
1542 	 * during bpf_mem_alloc_destroy().
1543 	 */
1544 	if (!htab_is_prealloc(htab)) {
1545 		delete_all_elements(htab);
1546 	} else {
1547 		htab_free_prealloced_fields(htab);
1548 		prealloc_destroy(htab);
1549 	}
1550 
1551 	bpf_map_free_elem_count(map);
1552 	free_percpu(htab->extra_elems);
1553 	bpf_map_area_free(htab->buckets);
1554 	bpf_mem_alloc_destroy(&htab->pcpu_ma);
1555 	bpf_mem_alloc_destroy(&htab->ma);
1556 	if (htab->use_percpu_counter)
1557 		percpu_counter_destroy(&htab->pcount);
1558 	bpf_map_area_free(htab);
1559 }
1560 
htab_map_seq_show_elem(struct bpf_map * map,void * key,struct seq_file * m)1561 static void htab_map_seq_show_elem(struct bpf_map *map, void *key,
1562 				   struct seq_file *m)
1563 {
1564 	void *value;
1565 
1566 	rcu_read_lock();
1567 
1568 	value = htab_map_lookup_elem(map, key);
1569 	if (!value) {
1570 		rcu_read_unlock();
1571 		return;
1572 	}
1573 
1574 	btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
1575 	seq_puts(m, ": ");
1576 	btf_type_seq_show(map->btf, map->btf_value_type_id, value, m);
1577 	seq_putc(m, '\n');
1578 
1579 	rcu_read_unlock();
1580 }
1581 
__htab_map_lookup_and_delete_elem(struct bpf_map * map,void * key,void * value,bool is_lru_map,bool is_percpu,u64 flags)1582 static int __htab_map_lookup_and_delete_elem(struct bpf_map *map, void *key,
1583 					     void *value, bool is_lru_map,
1584 					     bool is_percpu, u64 flags)
1585 {
1586 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1587 	struct hlist_nulls_head *head;
1588 	unsigned long bflags;
1589 	struct htab_elem *l;
1590 	u32 hash, key_size;
1591 	struct bucket *b;
1592 	int ret;
1593 
1594 	key_size = map->key_size;
1595 
1596 	hash = htab_map_hash(key, key_size, htab->hashrnd);
1597 	b = __select_bucket(htab, hash);
1598 	head = &b->head;
1599 
1600 	ret = htab_lock_bucket(b, &bflags);
1601 	if (ret)
1602 		return ret;
1603 
1604 	l = lookup_elem_raw(head, hash, key, key_size);
1605 	if (!l) {
1606 		ret = -ENOENT;
1607 		goto out_unlock;
1608 	}
1609 
1610 	if (is_percpu) {
1611 		u32 roundup_value_size = round_up(map->value_size, 8);
1612 		void __percpu *pptr;
1613 		int off = 0, cpu;
1614 
1615 		pptr = htab_elem_get_ptr(l, key_size);
1616 		for_each_possible_cpu(cpu) {
1617 			copy_map_value_long(&htab->map, value + off, per_cpu_ptr(pptr, cpu));
1618 			check_and_init_map_value(&htab->map, value + off);
1619 			off += roundup_value_size;
1620 		}
1621 	} else {
1622 		void *src = htab_elem_value(l, map->key_size);
1623 
1624 		if (flags & BPF_F_LOCK)
1625 			copy_map_value_locked(map, value, src, true);
1626 		else
1627 			copy_map_value(map, value, src);
1628 		/* Zeroing special fields in the temp buffer */
1629 		check_and_init_map_value(map, value);
1630 	}
1631 	hlist_nulls_del_rcu(&l->hash_node);
1632 
1633 out_unlock:
1634 	htab_unlock_bucket(b, bflags);
1635 
1636 	if (l) {
1637 		if (is_lru_map)
1638 			htab_lru_push_free(htab, l);
1639 		else
1640 			free_htab_elem(htab, l);
1641 	}
1642 
1643 	return ret;
1644 }
1645 
htab_map_lookup_and_delete_elem(struct bpf_map * map,void * key,void * value,u64 flags)1646 static int htab_map_lookup_and_delete_elem(struct bpf_map *map, void *key,
1647 					   void *value, u64 flags)
1648 {
1649 	return __htab_map_lookup_and_delete_elem(map, key, value, false, false,
1650 						 flags);
1651 }
1652 
htab_percpu_map_lookup_and_delete_elem(struct bpf_map * map,void * key,void * value,u64 flags)1653 static int htab_percpu_map_lookup_and_delete_elem(struct bpf_map *map,
1654 						  void *key, void *value,
1655 						  u64 flags)
1656 {
1657 	return __htab_map_lookup_and_delete_elem(map, key, value, false, true,
1658 						 flags);
1659 }
1660 
htab_lru_map_lookup_and_delete_elem(struct bpf_map * map,void * key,void * value,u64 flags)1661 static int htab_lru_map_lookup_and_delete_elem(struct bpf_map *map, void *key,
1662 					       void *value, u64 flags)
1663 {
1664 	return __htab_map_lookup_and_delete_elem(map, key, value, true, false,
1665 						 flags);
1666 }
1667 
htab_lru_percpu_map_lookup_and_delete_elem(struct bpf_map * map,void * key,void * value,u64 flags)1668 static int htab_lru_percpu_map_lookup_and_delete_elem(struct bpf_map *map,
1669 						      void *key, void *value,
1670 						      u64 flags)
1671 {
1672 	return __htab_map_lookup_and_delete_elem(map, key, value, true, true,
1673 						 flags);
1674 }
1675 
1676 static int
__htab_map_lookup_and_delete_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr,bool do_delete,bool is_lru_map,bool is_percpu)1677 __htab_map_lookup_and_delete_batch(struct bpf_map *map,
1678 				   const union bpf_attr *attr,
1679 				   union bpf_attr __user *uattr,
1680 				   bool do_delete, bool is_lru_map,
1681 				   bool is_percpu)
1682 {
1683 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
1684 	void *keys = NULL, *values = NULL, *value, *dst_key, *dst_val;
1685 	void __user *uvalues = u64_to_user_ptr(attr->batch.values);
1686 	void __user *ukeys = u64_to_user_ptr(attr->batch.keys);
1687 	void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1688 	u32 batch, max_count, size, bucket_size, map_id;
1689 	u32 bucket_cnt, total, key_size, value_size;
1690 	struct htab_elem *node_to_free = NULL;
1691 	u64 elem_map_flags, map_flags;
1692 	struct hlist_nulls_head *head;
1693 	struct hlist_nulls_node *n;
1694 	unsigned long flags = 0;
1695 	bool locked = false;
1696 	struct htab_elem *l;
1697 	struct bucket *b;
1698 	int ret = 0;
1699 
1700 	elem_map_flags = attr->batch.elem_flags;
1701 	if ((elem_map_flags & ~BPF_F_LOCK) ||
1702 	    ((elem_map_flags & BPF_F_LOCK) && !btf_record_has_field(map->record, BPF_SPIN_LOCK)))
1703 		return -EINVAL;
1704 
1705 	map_flags = attr->batch.flags;
1706 	if (map_flags)
1707 		return -EINVAL;
1708 
1709 	max_count = attr->batch.count;
1710 	if (!max_count)
1711 		return 0;
1712 
1713 	if (put_user(0, &uattr->batch.count))
1714 		return -EFAULT;
1715 
1716 	batch = 0;
1717 	if (ubatch && copy_from_user(&batch, ubatch, sizeof(batch)))
1718 		return -EFAULT;
1719 
1720 	if (batch >= htab->n_buckets)
1721 		return -ENOENT;
1722 
1723 	key_size = htab->map.key_size;
1724 	value_size = htab->map.value_size;
1725 	size = round_up(value_size, 8);
1726 	if (is_percpu)
1727 		value_size = size * num_possible_cpus();
1728 	total = 0;
1729 	/* while experimenting with hash tables with sizes ranging from 10 to
1730 	 * 1000, it was observed that a bucket can have up to 5 entries.
1731 	 */
1732 	bucket_size = 5;
1733 
1734 alloc:
1735 	/* We cannot do copy_from_user or copy_to_user inside
1736 	 * the rcu_read_lock. Allocate enough space here.
1737 	 */
1738 	keys = kvmalloc_array(key_size, bucket_size, GFP_USER | __GFP_NOWARN);
1739 	values = kvmalloc_array(value_size, bucket_size, GFP_USER | __GFP_NOWARN);
1740 	if (!keys || !values) {
1741 		ret = -ENOMEM;
1742 		goto after_loop;
1743 	}
1744 
1745 again:
1746 	bpf_disable_instrumentation();
1747 	rcu_read_lock();
1748 again_nocopy:
1749 	dst_key = keys;
1750 	dst_val = values;
1751 	b = &htab->buckets[batch];
1752 	head = &b->head;
1753 	/* do not grab the lock unless need it (bucket_cnt > 0). */
1754 	if (locked) {
1755 		ret = htab_lock_bucket(b, &flags);
1756 		if (ret) {
1757 			rcu_read_unlock();
1758 			bpf_enable_instrumentation();
1759 			goto after_loop;
1760 		}
1761 	}
1762 
1763 	bucket_cnt = 0;
1764 	hlist_nulls_for_each_entry_rcu(l, n, head, hash_node)
1765 		bucket_cnt++;
1766 
1767 	if (bucket_cnt && !locked) {
1768 		locked = true;
1769 		goto again_nocopy;
1770 	}
1771 
1772 	if (bucket_cnt > (max_count - total)) {
1773 		if (total == 0)
1774 			ret = -ENOSPC;
1775 		/* Note that since bucket_cnt > 0 here, it is implicit
1776 		 * that the locked was grabbed, so release it.
1777 		 */
1778 		htab_unlock_bucket(b, flags);
1779 		rcu_read_unlock();
1780 		bpf_enable_instrumentation();
1781 		goto after_loop;
1782 	}
1783 
1784 	if (bucket_cnt > bucket_size) {
1785 		bucket_size = bucket_cnt;
1786 		/* Note that since bucket_cnt > 0 here, it is implicit
1787 		 * that the locked was grabbed, so release it.
1788 		 */
1789 		htab_unlock_bucket(b, flags);
1790 		rcu_read_unlock();
1791 		bpf_enable_instrumentation();
1792 		kvfree(keys);
1793 		kvfree(values);
1794 		goto alloc;
1795 	}
1796 
1797 	/* Next block is only safe to run if you have grabbed the lock */
1798 	if (!locked)
1799 		goto next_batch;
1800 
1801 	hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
1802 		memcpy(dst_key, l->key, key_size);
1803 
1804 		if (is_percpu) {
1805 			int off = 0, cpu;
1806 			void __percpu *pptr;
1807 
1808 			pptr = htab_elem_get_ptr(l, map->key_size);
1809 			for_each_possible_cpu(cpu) {
1810 				copy_map_value_long(&htab->map, dst_val + off, per_cpu_ptr(pptr, cpu));
1811 				check_and_init_map_value(&htab->map, dst_val + off);
1812 				off += size;
1813 			}
1814 		} else {
1815 			value = htab_elem_value(l, key_size);
1816 			if (is_fd_htab(htab)) {
1817 				struct bpf_map **inner_map = value;
1818 
1819 				 /* Actual value is the id of the inner map */
1820 				map_id = map->ops->map_fd_sys_lookup_elem(*inner_map);
1821 				value = &map_id;
1822 			}
1823 
1824 			if (elem_map_flags & BPF_F_LOCK)
1825 				copy_map_value_locked(map, dst_val, value,
1826 						      true);
1827 			else
1828 				copy_map_value(map, dst_val, value);
1829 			/* Zeroing special fields in the temp buffer */
1830 			check_and_init_map_value(map, dst_val);
1831 		}
1832 		if (do_delete) {
1833 			hlist_nulls_del_rcu(&l->hash_node);
1834 
1835 			/* bpf_lru_push_free() will acquire lru_lock, which
1836 			 * may cause deadlock. See comments in function
1837 			 * prealloc_lru_pop(). Let us do bpf_lru_push_free()
1838 			 * after releasing the bucket lock.
1839 			 *
1840 			 * For htab of maps, htab_put_fd_value() in
1841 			 * free_htab_elem() may acquire a spinlock with bucket
1842 			 * lock being held and it violates the lock rule, so
1843 			 * invoke free_htab_elem() after unlock as well.
1844 			 */
1845 			l->batch_flink = node_to_free;
1846 			node_to_free = l;
1847 		}
1848 		dst_key += key_size;
1849 		dst_val += value_size;
1850 	}
1851 
1852 	htab_unlock_bucket(b, flags);
1853 	locked = false;
1854 
1855 	while (node_to_free) {
1856 		l = node_to_free;
1857 		node_to_free = node_to_free->batch_flink;
1858 		if (is_lru_map)
1859 			htab_lru_push_free(htab, l);
1860 		else
1861 			free_htab_elem(htab, l);
1862 	}
1863 
1864 next_batch:
1865 	/* If we are not copying data, we can go to next bucket and avoid
1866 	 * unlocking the rcu.
1867 	 */
1868 	if (!bucket_cnt && (batch + 1 < htab->n_buckets)) {
1869 		batch++;
1870 		goto again_nocopy;
1871 	}
1872 
1873 	rcu_read_unlock();
1874 	bpf_enable_instrumentation();
1875 	if (bucket_cnt && (copy_to_user(ukeys + total * key_size, keys,
1876 	    key_size * bucket_cnt) ||
1877 	    copy_to_user(uvalues + total * value_size, values,
1878 	    value_size * bucket_cnt))) {
1879 		ret = -EFAULT;
1880 		goto after_loop;
1881 	}
1882 
1883 	total += bucket_cnt;
1884 	batch++;
1885 	if (batch >= htab->n_buckets) {
1886 		ret = -ENOENT;
1887 		goto after_loop;
1888 	}
1889 	goto again;
1890 
1891 after_loop:
1892 	if (ret == -EFAULT)
1893 		goto out;
1894 
1895 	/* copy # of entries and next batch */
1896 	ubatch = u64_to_user_ptr(attr->batch.out_batch);
1897 	if (copy_to_user(ubatch, &batch, sizeof(batch)) ||
1898 	    put_user(total, &uattr->batch.count))
1899 		ret = -EFAULT;
1900 
1901 out:
1902 	kvfree(keys);
1903 	kvfree(values);
1904 	return ret;
1905 }
1906 
1907 static int
htab_percpu_map_lookup_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)1908 htab_percpu_map_lookup_batch(struct bpf_map *map, const union bpf_attr *attr,
1909 			     union bpf_attr __user *uattr)
1910 {
1911 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, false,
1912 						  false, true);
1913 }
1914 
1915 static int
htab_percpu_map_lookup_and_delete_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)1916 htab_percpu_map_lookup_and_delete_batch(struct bpf_map *map,
1917 					const union bpf_attr *attr,
1918 					union bpf_attr __user *uattr)
1919 {
1920 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, true,
1921 						  false, true);
1922 }
1923 
1924 static int
htab_map_lookup_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)1925 htab_map_lookup_batch(struct bpf_map *map, const union bpf_attr *attr,
1926 		      union bpf_attr __user *uattr)
1927 {
1928 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, false,
1929 						  false, false);
1930 }
1931 
1932 static int
htab_map_lookup_and_delete_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)1933 htab_map_lookup_and_delete_batch(struct bpf_map *map,
1934 				 const union bpf_attr *attr,
1935 				 union bpf_attr __user *uattr)
1936 {
1937 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, true,
1938 						  false, false);
1939 }
1940 
1941 static int
htab_lru_percpu_map_lookup_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)1942 htab_lru_percpu_map_lookup_batch(struct bpf_map *map,
1943 				 const union bpf_attr *attr,
1944 				 union bpf_attr __user *uattr)
1945 {
1946 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, false,
1947 						  true, true);
1948 }
1949 
1950 static int
htab_lru_percpu_map_lookup_and_delete_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)1951 htab_lru_percpu_map_lookup_and_delete_batch(struct bpf_map *map,
1952 					    const union bpf_attr *attr,
1953 					    union bpf_attr __user *uattr)
1954 {
1955 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, true,
1956 						  true, true);
1957 }
1958 
1959 static int
htab_lru_map_lookup_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)1960 htab_lru_map_lookup_batch(struct bpf_map *map, const union bpf_attr *attr,
1961 			  union bpf_attr __user *uattr)
1962 {
1963 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, false,
1964 						  true, false);
1965 }
1966 
1967 static int
htab_lru_map_lookup_and_delete_batch(struct bpf_map * map,const union bpf_attr * attr,union bpf_attr __user * uattr)1968 htab_lru_map_lookup_and_delete_batch(struct bpf_map *map,
1969 				     const union bpf_attr *attr,
1970 				     union bpf_attr __user *uattr)
1971 {
1972 	return __htab_map_lookup_and_delete_batch(map, attr, uattr, true,
1973 						  true, false);
1974 }
1975 
1976 struct bpf_iter_seq_hash_map_info {
1977 	struct bpf_map *map;
1978 	struct bpf_htab *htab;
1979 	void *percpu_value_buf; // non-zero means percpu hash
1980 	u32 bucket_id;
1981 	u32 skip_elems;
1982 };
1983 
1984 static struct htab_elem *
bpf_hash_map_seq_find_next(struct bpf_iter_seq_hash_map_info * info,struct htab_elem * prev_elem)1985 bpf_hash_map_seq_find_next(struct bpf_iter_seq_hash_map_info *info,
1986 			   struct htab_elem *prev_elem)
1987 {
1988 	const struct bpf_htab *htab = info->htab;
1989 	u32 skip_elems = info->skip_elems;
1990 	u32 bucket_id = info->bucket_id;
1991 	struct hlist_nulls_head *head;
1992 	struct hlist_nulls_node *n;
1993 	struct htab_elem *elem;
1994 	struct bucket *b;
1995 	u32 i, count;
1996 
1997 	if (bucket_id >= htab->n_buckets)
1998 		return NULL;
1999 
2000 	/* try to find next elem in the same bucket */
2001 	if (prev_elem) {
2002 		/* no update/deletion on this bucket, prev_elem should be still valid
2003 		 * and we won't skip elements.
2004 		 */
2005 		n = rcu_dereference_raw(hlist_nulls_next_rcu(&prev_elem->hash_node));
2006 		elem = hlist_nulls_entry_safe(n, struct htab_elem, hash_node);
2007 		if (elem)
2008 			return elem;
2009 
2010 		/* not found, unlock and go to the next bucket */
2011 		b = &htab->buckets[bucket_id++];
2012 		rcu_read_unlock();
2013 		skip_elems = 0;
2014 	}
2015 
2016 	for (i = bucket_id; i < htab->n_buckets; i++) {
2017 		b = &htab->buckets[i];
2018 		rcu_read_lock();
2019 
2020 		count = 0;
2021 		head = &b->head;
2022 		hlist_nulls_for_each_entry_rcu(elem, n, head, hash_node) {
2023 			if (count >= skip_elems) {
2024 				info->bucket_id = i;
2025 				info->skip_elems = count;
2026 				return elem;
2027 			}
2028 			count++;
2029 		}
2030 
2031 		rcu_read_unlock();
2032 		skip_elems = 0;
2033 	}
2034 
2035 	info->bucket_id = i;
2036 	info->skip_elems = 0;
2037 	return NULL;
2038 }
2039 
bpf_hash_map_seq_start(struct seq_file * seq,loff_t * pos)2040 static void *bpf_hash_map_seq_start(struct seq_file *seq, loff_t *pos)
2041 {
2042 	struct bpf_iter_seq_hash_map_info *info = seq->private;
2043 	struct htab_elem *elem;
2044 
2045 	elem = bpf_hash_map_seq_find_next(info, NULL);
2046 	if (!elem)
2047 		return NULL;
2048 
2049 	if (*pos == 0)
2050 		++*pos;
2051 	return elem;
2052 }
2053 
bpf_hash_map_seq_next(struct seq_file * seq,void * v,loff_t * pos)2054 static void *bpf_hash_map_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2055 {
2056 	struct bpf_iter_seq_hash_map_info *info = seq->private;
2057 
2058 	++*pos;
2059 	++info->skip_elems;
2060 	return bpf_hash_map_seq_find_next(info, v);
2061 }
2062 
__bpf_hash_map_seq_show(struct seq_file * seq,struct htab_elem * elem)2063 static int __bpf_hash_map_seq_show(struct seq_file *seq, struct htab_elem *elem)
2064 {
2065 	struct bpf_iter_seq_hash_map_info *info = seq->private;
2066 	struct bpf_iter__bpf_map_elem ctx = {};
2067 	struct bpf_map *map = info->map;
2068 	struct bpf_iter_meta meta;
2069 	int ret = 0, off = 0, cpu;
2070 	u32 roundup_value_size;
2071 	struct bpf_prog *prog;
2072 	void __percpu *pptr;
2073 
2074 	meta.seq = seq;
2075 	prog = bpf_iter_get_info(&meta, elem == NULL);
2076 	if (prog) {
2077 		ctx.meta = &meta;
2078 		ctx.map = info->map;
2079 		if (elem) {
2080 			ctx.key = elem->key;
2081 			if (!info->percpu_value_buf) {
2082 				ctx.value = htab_elem_value(elem, map->key_size);
2083 			} else {
2084 				roundup_value_size = round_up(map->value_size, 8);
2085 				pptr = htab_elem_get_ptr(elem, map->key_size);
2086 				for_each_possible_cpu(cpu) {
2087 					copy_map_value_long(map, info->percpu_value_buf + off,
2088 							    per_cpu_ptr(pptr, cpu));
2089 					check_and_init_map_value(map, info->percpu_value_buf + off);
2090 					off += roundup_value_size;
2091 				}
2092 				ctx.value = info->percpu_value_buf;
2093 			}
2094 		}
2095 		ret = bpf_iter_run_prog(prog, &ctx);
2096 	}
2097 
2098 	return ret;
2099 }
2100 
bpf_hash_map_seq_show(struct seq_file * seq,void * v)2101 static int bpf_hash_map_seq_show(struct seq_file *seq, void *v)
2102 {
2103 	return __bpf_hash_map_seq_show(seq, v);
2104 }
2105 
bpf_hash_map_seq_stop(struct seq_file * seq,void * v)2106 static void bpf_hash_map_seq_stop(struct seq_file *seq, void *v)
2107 {
2108 	if (!v)
2109 		(void)__bpf_hash_map_seq_show(seq, NULL);
2110 	else
2111 		rcu_read_unlock();
2112 }
2113 
bpf_iter_init_hash_map(void * priv_data,struct bpf_iter_aux_info * aux)2114 static int bpf_iter_init_hash_map(void *priv_data,
2115 				  struct bpf_iter_aux_info *aux)
2116 {
2117 	struct bpf_iter_seq_hash_map_info *seq_info = priv_data;
2118 	struct bpf_map *map = aux->map;
2119 	void *value_buf;
2120 	u32 buf_size;
2121 
2122 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
2123 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
2124 		buf_size = round_up(map->value_size, 8) * num_possible_cpus();
2125 		value_buf = kmalloc(buf_size, GFP_USER | __GFP_NOWARN);
2126 		if (!value_buf)
2127 			return -ENOMEM;
2128 
2129 		seq_info->percpu_value_buf = value_buf;
2130 	}
2131 
2132 	bpf_map_inc_with_uref(map);
2133 	seq_info->map = map;
2134 	seq_info->htab = container_of(map, struct bpf_htab, map);
2135 	return 0;
2136 }
2137 
bpf_iter_fini_hash_map(void * priv_data)2138 static void bpf_iter_fini_hash_map(void *priv_data)
2139 {
2140 	struct bpf_iter_seq_hash_map_info *seq_info = priv_data;
2141 
2142 	bpf_map_put_with_uref(seq_info->map);
2143 	kfree(seq_info->percpu_value_buf);
2144 }
2145 
2146 static const struct seq_operations bpf_hash_map_seq_ops = {
2147 	.start	= bpf_hash_map_seq_start,
2148 	.next	= bpf_hash_map_seq_next,
2149 	.stop	= bpf_hash_map_seq_stop,
2150 	.show	= bpf_hash_map_seq_show,
2151 };
2152 
2153 static const struct bpf_iter_seq_info iter_seq_info = {
2154 	.seq_ops		= &bpf_hash_map_seq_ops,
2155 	.init_seq_private	= bpf_iter_init_hash_map,
2156 	.fini_seq_private	= bpf_iter_fini_hash_map,
2157 	.seq_priv_size		= sizeof(struct bpf_iter_seq_hash_map_info),
2158 };
2159 
bpf_for_each_hash_elem(struct bpf_map * map,bpf_callback_t callback_fn,void * callback_ctx,u64 flags)2160 static long bpf_for_each_hash_elem(struct bpf_map *map, bpf_callback_t callback_fn,
2161 				   void *callback_ctx, u64 flags)
2162 {
2163 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
2164 	struct hlist_nulls_head *head;
2165 	struct hlist_nulls_node *n;
2166 	struct htab_elem *elem;
2167 	int i, num_elems = 0;
2168 	void __percpu *pptr;
2169 	struct bucket *b;
2170 	void *key, *val;
2171 	bool is_percpu;
2172 	u64 ret = 0;
2173 
2174 	cant_migrate();
2175 
2176 	if (flags != 0)
2177 		return -EINVAL;
2178 
2179 	is_percpu = htab_is_percpu(htab);
2180 
2181 	/* migration has been disabled, so percpu value prepared here will be
2182 	 * the same as the one seen by the bpf program with
2183 	 * bpf_map_lookup_elem().
2184 	 */
2185 	for (i = 0; i < htab->n_buckets; i++) {
2186 		b = &htab->buckets[i];
2187 		rcu_read_lock();
2188 		head = &b->head;
2189 		hlist_nulls_for_each_entry_safe(elem, n, head, hash_node) {
2190 			key = elem->key;
2191 			if (is_percpu) {
2192 				/* current cpu value for percpu map */
2193 				pptr = htab_elem_get_ptr(elem, map->key_size);
2194 				val = this_cpu_ptr(pptr);
2195 			} else {
2196 				val = htab_elem_value(elem, map->key_size);
2197 			}
2198 			num_elems++;
2199 			ret = callback_fn((u64)(long)map, (u64)(long)key,
2200 					  (u64)(long)val, (u64)(long)callback_ctx, 0);
2201 			/* return value: 0 - continue, 1 - stop and return */
2202 			if (ret) {
2203 				rcu_read_unlock();
2204 				goto out;
2205 			}
2206 		}
2207 		rcu_read_unlock();
2208 	}
2209 out:
2210 	return num_elems;
2211 }
2212 
htab_map_mem_usage(const struct bpf_map * map)2213 static u64 htab_map_mem_usage(const struct bpf_map *map)
2214 {
2215 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
2216 	u32 value_size = round_up(htab->map.value_size, 8);
2217 	bool prealloc = htab_is_prealloc(htab);
2218 	bool percpu = htab_is_percpu(htab);
2219 	bool lru = htab_is_lru(htab);
2220 	u64 num_entries;
2221 	u64 usage = sizeof(struct bpf_htab);
2222 
2223 	usage += sizeof(struct bucket) * htab->n_buckets;
2224 	usage += sizeof(int) * num_possible_cpus() * HASHTAB_MAP_LOCK_COUNT;
2225 	if (prealloc) {
2226 		num_entries = map->max_entries;
2227 		if (htab_has_extra_elems(htab))
2228 			num_entries += num_possible_cpus();
2229 
2230 		usage += htab->elem_size * num_entries;
2231 
2232 		if (percpu)
2233 			usage += value_size * num_possible_cpus() * num_entries;
2234 		else if (!lru)
2235 			usage += sizeof(struct htab_elem *) * num_possible_cpus();
2236 	} else {
2237 #define LLIST_NODE_SZ sizeof(struct llist_node)
2238 
2239 		num_entries = htab->use_percpu_counter ?
2240 					  percpu_counter_sum(&htab->pcount) :
2241 					  atomic_read(&htab->count);
2242 		usage += (htab->elem_size + LLIST_NODE_SZ) * num_entries;
2243 		if (percpu) {
2244 			usage += (LLIST_NODE_SZ + sizeof(void *)) * num_entries;
2245 			usage += value_size * num_possible_cpus() * num_entries;
2246 		}
2247 	}
2248 	return usage;
2249 }
2250 
2251 BTF_ID_LIST_SINGLE(htab_map_btf_ids, struct, bpf_htab)
2252 const struct bpf_map_ops htab_map_ops = {
2253 	.map_meta_equal = bpf_map_meta_equal,
2254 	.map_alloc_check = htab_map_alloc_check,
2255 	.map_alloc = htab_map_alloc,
2256 	.map_free = htab_map_free,
2257 	.map_get_next_key = htab_map_get_next_key,
2258 	.map_release_uref = htab_map_free_timers_and_wq,
2259 	.map_lookup_elem = htab_map_lookup_elem,
2260 	.map_lookup_and_delete_elem = htab_map_lookup_and_delete_elem,
2261 	.map_update_elem = htab_map_update_elem,
2262 	.map_delete_elem = htab_map_delete_elem,
2263 	.map_gen_lookup = htab_map_gen_lookup,
2264 	.map_seq_show_elem = htab_map_seq_show_elem,
2265 	.map_set_for_each_callback_args = map_set_for_each_callback_args,
2266 	.map_for_each_callback = bpf_for_each_hash_elem,
2267 	.map_mem_usage = htab_map_mem_usage,
2268 	BATCH_OPS(htab),
2269 	.map_btf_id = &htab_map_btf_ids[0],
2270 	.iter_seq_info = &iter_seq_info,
2271 };
2272 
2273 const struct bpf_map_ops htab_lru_map_ops = {
2274 	.map_meta_equal = bpf_map_meta_equal,
2275 	.map_alloc_check = htab_map_alloc_check,
2276 	.map_alloc = htab_map_alloc,
2277 	.map_free = htab_map_free,
2278 	.map_get_next_key = htab_map_get_next_key,
2279 	.map_release_uref = htab_map_free_timers_and_wq,
2280 	.map_lookup_elem = htab_lru_map_lookup_elem,
2281 	.map_lookup_and_delete_elem = htab_lru_map_lookup_and_delete_elem,
2282 	.map_lookup_elem_sys_only = htab_lru_map_lookup_elem_sys,
2283 	.map_update_elem = htab_lru_map_update_elem,
2284 	.map_delete_elem = htab_lru_map_delete_elem,
2285 	.map_gen_lookup = htab_lru_map_gen_lookup,
2286 	.map_seq_show_elem = htab_map_seq_show_elem,
2287 	.map_set_for_each_callback_args = map_set_for_each_callback_args,
2288 	.map_for_each_callback = bpf_for_each_hash_elem,
2289 	.map_mem_usage = htab_map_mem_usage,
2290 	BATCH_OPS(htab_lru),
2291 	.map_btf_id = &htab_map_btf_ids[0],
2292 	.iter_seq_info = &iter_seq_info,
2293 };
2294 
2295 /* Called from eBPF program */
htab_percpu_map_lookup_elem(struct bpf_map * map,void * key)2296 static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key)
2297 {
2298 	struct htab_elem *l = __htab_map_lookup_elem(map, key);
2299 
2300 	if (l)
2301 		return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
2302 	else
2303 		return NULL;
2304 }
2305 
2306 /* inline bpf_map_lookup_elem() call for per-CPU hashmap */
htab_percpu_map_gen_lookup(struct bpf_map * map,struct bpf_insn * insn_buf)2307 static int htab_percpu_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf)
2308 {
2309 	struct bpf_insn *insn = insn_buf;
2310 
2311 	if (!bpf_jit_supports_percpu_insn())
2312 		return -EOPNOTSUPP;
2313 
2314 	BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
2315 		     (void *(*)(struct bpf_map *map, void *key))NULL));
2316 	*insn++ = BPF_EMIT_CALL(__htab_map_lookup_elem);
2317 	*insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3);
2318 	*insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_0,
2319 				offsetof(struct htab_elem, key) + roundup(map->key_size, 8));
2320 	*insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0);
2321 	*insn++ = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);
2322 
2323 	return insn - insn_buf;
2324 }
2325 
htab_percpu_map_lookup_percpu_elem(struct bpf_map * map,void * key,u32 cpu)2326 static void *htab_percpu_map_lookup_percpu_elem(struct bpf_map *map, void *key, u32 cpu)
2327 {
2328 	struct htab_elem *l;
2329 
2330 	if (cpu >= nr_cpu_ids)
2331 		return NULL;
2332 
2333 	l = __htab_map_lookup_elem(map, key);
2334 	if (l)
2335 		return per_cpu_ptr(htab_elem_get_ptr(l, map->key_size), cpu);
2336 	else
2337 		return NULL;
2338 }
2339 
htab_lru_percpu_map_lookup_elem(struct bpf_map * map,void * key)2340 static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key)
2341 {
2342 	struct htab_elem *l = __htab_map_lookup_elem(map, key);
2343 
2344 	if (l) {
2345 		bpf_lru_node_set_ref(&l->lru_node);
2346 		return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size));
2347 	}
2348 
2349 	return NULL;
2350 }
2351 
htab_lru_percpu_map_lookup_percpu_elem(struct bpf_map * map,void * key,u32 cpu)2352 static void *htab_lru_percpu_map_lookup_percpu_elem(struct bpf_map *map, void *key, u32 cpu)
2353 {
2354 	struct htab_elem *l;
2355 
2356 	if (cpu >= nr_cpu_ids)
2357 		return NULL;
2358 
2359 	l = __htab_map_lookup_elem(map, key);
2360 	if (l) {
2361 		bpf_lru_node_set_ref(&l->lru_node);
2362 		return per_cpu_ptr(htab_elem_get_ptr(l, map->key_size), cpu);
2363 	}
2364 
2365 	return NULL;
2366 }
2367 
bpf_percpu_hash_copy(struct bpf_map * map,void * key,void * value)2368 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value)
2369 {
2370 	struct htab_elem *l;
2371 	void __percpu *pptr;
2372 	int ret = -ENOENT;
2373 	int cpu, off = 0;
2374 	u32 size;
2375 
2376 	/* per_cpu areas are zero-filled and bpf programs can only
2377 	 * access 'value_size' of them, so copying rounded areas
2378 	 * will not leak any kernel data
2379 	 */
2380 	size = round_up(map->value_size, 8);
2381 	rcu_read_lock();
2382 	l = __htab_map_lookup_elem(map, key);
2383 	if (!l)
2384 		goto out;
2385 	/* We do not mark LRU map element here in order to not mess up
2386 	 * eviction heuristics when user space does a map walk.
2387 	 */
2388 	pptr = htab_elem_get_ptr(l, map->key_size);
2389 	for_each_possible_cpu(cpu) {
2390 		copy_map_value_long(map, value + off, per_cpu_ptr(pptr, cpu));
2391 		check_and_init_map_value(map, value + off);
2392 		off += size;
2393 	}
2394 	ret = 0;
2395 out:
2396 	rcu_read_unlock();
2397 	return ret;
2398 }
2399 
bpf_percpu_hash_update(struct bpf_map * map,void * key,void * value,u64 map_flags)2400 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
2401 			   u64 map_flags)
2402 {
2403 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
2404 	int ret;
2405 
2406 	rcu_read_lock();
2407 	if (htab_is_lru(htab))
2408 		ret = __htab_lru_percpu_map_update_elem(map, key, value,
2409 							map_flags, true);
2410 	else
2411 		ret = htab_map_update_elem_in_place(map, key, value, map_flags,
2412 						    true, true);
2413 	rcu_read_unlock();
2414 
2415 	return ret;
2416 }
2417 
htab_percpu_map_seq_show_elem(struct bpf_map * map,void * key,struct seq_file * m)2418 static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
2419 					  struct seq_file *m)
2420 {
2421 	struct htab_elem *l;
2422 	void __percpu *pptr;
2423 	int cpu;
2424 
2425 	rcu_read_lock();
2426 
2427 	l = __htab_map_lookup_elem(map, key);
2428 	if (!l) {
2429 		rcu_read_unlock();
2430 		return;
2431 	}
2432 
2433 	btf_type_seq_show(map->btf, map->btf_key_type_id, key, m);
2434 	seq_puts(m, ": {\n");
2435 	pptr = htab_elem_get_ptr(l, map->key_size);
2436 	for_each_possible_cpu(cpu) {
2437 		seq_printf(m, "\tcpu%d: ", cpu);
2438 		btf_type_seq_show(map->btf, map->btf_value_type_id,
2439 				  per_cpu_ptr(pptr, cpu), m);
2440 		seq_putc(m, '\n');
2441 	}
2442 	seq_puts(m, "}\n");
2443 
2444 	rcu_read_unlock();
2445 }
2446 
2447 const struct bpf_map_ops htab_percpu_map_ops = {
2448 	.map_meta_equal = bpf_map_meta_equal,
2449 	.map_alloc_check = htab_map_alloc_check,
2450 	.map_alloc = htab_map_alloc,
2451 	.map_free = htab_map_free,
2452 	.map_get_next_key = htab_map_get_next_key,
2453 	.map_lookup_elem = htab_percpu_map_lookup_elem,
2454 	.map_gen_lookup = htab_percpu_map_gen_lookup,
2455 	.map_lookup_and_delete_elem = htab_percpu_map_lookup_and_delete_elem,
2456 	.map_update_elem = htab_percpu_map_update_elem,
2457 	.map_delete_elem = htab_map_delete_elem,
2458 	.map_lookup_percpu_elem = htab_percpu_map_lookup_percpu_elem,
2459 	.map_seq_show_elem = htab_percpu_map_seq_show_elem,
2460 	.map_set_for_each_callback_args = map_set_for_each_callback_args,
2461 	.map_for_each_callback = bpf_for_each_hash_elem,
2462 	.map_mem_usage = htab_map_mem_usage,
2463 	BATCH_OPS(htab_percpu),
2464 	.map_btf_id = &htab_map_btf_ids[0],
2465 	.iter_seq_info = &iter_seq_info,
2466 };
2467 
2468 const struct bpf_map_ops htab_lru_percpu_map_ops = {
2469 	.map_meta_equal = bpf_map_meta_equal,
2470 	.map_alloc_check = htab_map_alloc_check,
2471 	.map_alloc = htab_map_alloc,
2472 	.map_free = htab_map_free,
2473 	.map_get_next_key = htab_map_get_next_key,
2474 	.map_lookup_elem = htab_lru_percpu_map_lookup_elem,
2475 	.map_lookup_and_delete_elem = htab_lru_percpu_map_lookup_and_delete_elem,
2476 	.map_update_elem = htab_lru_percpu_map_update_elem,
2477 	.map_delete_elem = htab_lru_map_delete_elem,
2478 	.map_lookup_percpu_elem = htab_lru_percpu_map_lookup_percpu_elem,
2479 	.map_seq_show_elem = htab_percpu_map_seq_show_elem,
2480 	.map_set_for_each_callback_args = map_set_for_each_callback_args,
2481 	.map_for_each_callback = bpf_for_each_hash_elem,
2482 	.map_mem_usage = htab_map_mem_usage,
2483 	BATCH_OPS(htab_lru_percpu),
2484 	.map_btf_id = &htab_map_btf_ids[0],
2485 	.iter_seq_info = &iter_seq_info,
2486 };
2487 
fd_htab_map_alloc_check(union bpf_attr * attr)2488 static int fd_htab_map_alloc_check(union bpf_attr *attr)
2489 {
2490 	if (attr->value_size != sizeof(u32))
2491 		return -EINVAL;
2492 	return htab_map_alloc_check(attr);
2493 }
2494 
fd_htab_map_free(struct bpf_map * map)2495 static void fd_htab_map_free(struct bpf_map *map)
2496 {
2497 	struct bpf_htab *htab = container_of(map, struct bpf_htab, map);
2498 	struct hlist_nulls_node *n;
2499 	struct hlist_nulls_head *head;
2500 	struct htab_elem *l;
2501 	int i;
2502 
2503 	for (i = 0; i < htab->n_buckets; i++) {
2504 		head = select_bucket(htab, i);
2505 
2506 		hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
2507 			void *ptr = fd_htab_map_get_ptr(map, l);
2508 
2509 			map->ops->map_fd_put_ptr(map, ptr, false);
2510 		}
2511 	}
2512 
2513 	htab_map_free(map);
2514 }
2515 
2516 /* only called from syscall */
bpf_fd_htab_map_lookup_elem(struct bpf_map * map,void * key,u32 * value)2517 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
2518 {
2519 	void **ptr;
2520 	int ret = 0;
2521 
2522 	if (!map->ops->map_fd_sys_lookup_elem)
2523 		return -ENOTSUPP;
2524 
2525 	rcu_read_lock();
2526 	ptr = htab_map_lookup_elem(map, key);
2527 	if (ptr)
2528 		*value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
2529 	else
2530 		ret = -ENOENT;
2531 	rcu_read_unlock();
2532 
2533 	return ret;
2534 }
2535 
2536 /* Only called from syscall */
bpf_fd_htab_map_update_elem(struct bpf_map * map,struct file * map_file,void * key,void * value,u64 map_flags)2537 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
2538 				void *key, void *value, u64 map_flags)
2539 {
2540 	void *ptr;
2541 	int ret;
2542 
2543 	ptr = map->ops->map_fd_get_ptr(map, map_file, *(int *)value);
2544 	if (IS_ERR(ptr))
2545 		return PTR_ERR(ptr);
2546 
2547 	/* The htab bucket lock is always held during update operations in fd
2548 	 * htab map, and the following rcu_read_lock() is only used to avoid
2549 	 * the WARN_ON_ONCE in htab_map_update_elem_in_place().
2550 	 */
2551 	rcu_read_lock();
2552 	ret = htab_map_update_elem_in_place(map, key, &ptr, map_flags, false, false);
2553 	rcu_read_unlock();
2554 	if (ret)
2555 		map->ops->map_fd_put_ptr(map, ptr, false);
2556 
2557 	return ret;
2558 }
2559 
htab_of_map_alloc(union bpf_attr * attr)2560 static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr)
2561 {
2562 	struct bpf_map *map, *inner_map_meta;
2563 
2564 	inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd);
2565 	if (IS_ERR(inner_map_meta))
2566 		return inner_map_meta;
2567 
2568 	map = htab_map_alloc(attr);
2569 	if (IS_ERR(map)) {
2570 		bpf_map_meta_free(inner_map_meta);
2571 		return map;
2572 	}
2573 
2574 	map->inner_map_meta = inner_map_meta;
2575 
2576 	return map;
2577 }
2578 
htab_of_map_lookup_elem(struct bpf_map * map,void * key)2579 static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key)
2580 {
2581 	struct bpf_map **inner_map  = htab_map_lookup_elem(map, key);
2582 
2583 	if (!inner_map)
2584 		return NULL;
2585 
2586 	return READ_ONCE(*inner_map);
2587 }
2588 
htab_of_map_gen_lookup(struct bpf_map * map,struct bpf_insn * insn_buf)2589 static int htab_of_map_gen_lookup(struct bpf_map *map,
2590 				  struct bpf_insn *insn_buf)
2591 {
2592 	struct bpf_insn *insn = insn_buf;
2593 	const int ret = BPF_REG_0;
2594 
2595 	BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem,
2596 		     (void *(*)(struct bpf_map *map, void *key))NULL));
2597 	*insn++ = BPF_EMIT_CALL(__htab_map_lookup_elem);
2598 	*insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2);
2599 	*insn++ = BPF_ALU64_IMM(BPF_ADD, ret,
2600 				offsetof(struct htab_elem, key) +
2601 				round_up(map->key_size, 8));
2602 	*insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0);
2603 
2604 	return insn - insn_buf;
2605 }
2606 
htab_of_map_free(struct bpf_map * map)2607 static void htab_of_map_free(struct bpf_map *map)
2608 {
2609 	bpf_map_meta_free(map->inner_map_meta);
2610 	fd_htab_map_free(map);
2611 }
2612 
2613 const struct bpf_map_ops htab_of_maps_map_ops = {
2614 	.map_alloc_check = fd_htab_map_alloc_check,
2615 	.map_alloc = htab_of_map_alloc,
2616 	.map_free = htab_of_map_free,
2617 	.map_get_next_key = htab_map_get_next_key,
2618 	.map_lookup_elem = htab_of_map_lookup_elem,
2619 	.map_delete_elem = htab_map_delete_elem,
2620 	.map_fd_get_ptr = bpf_map_fd_get_ptr,
2621 	.map_fd_put_ptr = bpf_map_fd_put_ptr,
2622 	.map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
2623 	.map_gen_lookup = htab_of_map_gen_lookup,
2624 	.map_check_btf = map_check_no_btf,
2625 	.map_mem_usage = htab_map_mem_usage,
2626 	BATCH_OPS(htab),
2627 	.map_btf_id = &htab_map_btf_ids[0],
2628 };
2629