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