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 <linux/rhashtable.h> 13 #include <uapi/linux/btf.h> 14 #include <linux/rcupdate_trace.h> 15 #include <linux/btf_ids.h> 16 #include "percpu_freelist.h" 17 #include "bpf_lru_list.h" 18 #include "map_in_map.h" 19 #include <linux/bpf_mem_alloc.h> 20 #include <asm/rqspinlock.h> 21 22 #define HTAB_CREATE_FLAG_MASK \ 23 (BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE | \ 24 BPF_F_ACCESS_MASK | BPF_F_ZERO_SEED) 25 26 #define BATCH_OPS(_name) \ 27 .map_lookup_batch = \ 28 _name##_map_lookup_batch, \ 29 .map_lookup_and_delete_batch = \ 30 _name##_map_lookup_and_delete_batch, \ 31 .map_update_batch = \ 32 generic_map_update_batch, \ 33 .map_delete_batch = \ 34 generic_map_delete_batch 35 36 /* 37 * The bucket lock has two protection scopes: 38 * 39 * 1) Serializing concurrent operations from BPF programs on different 40 * CPUs 41 * 42 * 2) Serializing concurrent operations from BPF programs and sys_bpf() 43 * 44 * BPF programs can execute in any context including perf, kprobes and 45 * tracing. As there are almost no limits where perf, kprobes and tracing 46 * can be invoked from the lock operations need to be protected against 47 * deadlocks. Deadlocks can be caused by recursion and by an invocation in 48 * the lock held section when functions which acquire this lock are invoked 49 * from sys_bpf(). BPF recursion is prevented by incrementing the per CPU 50 * variable bpf_prog_active, which prevents BPF programs attached to perf 51 * events, kprobes and tracing to be invoked before the prior invocation 52 * from one of these contexts completed. sys_bpf() uses the same mechanism 53 * by pinning the task to the current CPU and incrementing the recursion 54 * protection across the map operation. 55 * 56 * This has subtle implications on PREEMPT_RT. PREEMPT_RT forbids certain 57 * operations like memory allocations (even with GFP_ATOMIC) from atomic 58 * contexts. This is required because even with GFP_ATOMIC the memory 59 * allocator calls into code paths which acquire locks with long held lock 60 * sections. To ensure the deterministic behaviour these locks are regular 61 * spinlocks, which are converted to 'sleepable' spinlocks on RT. The only 62 * true atomic contexts on an RT kernel are the low level hardware 63 * handling, scheduling, low level interrupt handling, NMIs etc. None of 64 * these contexts should ever do memory allocations. 65 * 66 * As regular device interrupt handlers and soft interrupts are forced into 67 * thread context, the existing code which does 68 * spin_lock*(); alloc(GFP_ATOMIC); spin_unlock*(); 69 * just works. 70 * 71 * In theory the BPF locks could be converted to regular spinlocks as well, 72 * but the bucket locks and percpu_freelist locks can be taken from 73 * arbitrary contexts (perf, kprobes, tracepoints) which are required to be 74 * atomic contexts even on RT. Before the introduction of bpf_mem_alloc, 75 * it is only safe to use raw spinlock for preallocated hash map on a RT kernel, 76 * because there is no memory allocation within the lock held sections. However 77 * after hash map was fully converted to use bpf_mem_alloc, there will be 78 * non-synchronous memory allocation for non-preallocated hash map, so it is 79 * safe to always use raw spinlock for bucket lock. 80 */ 81 struct bucket { 82 struct hlist_nulls_head head; 83 rqspinlock_t raw_lock; 84 }; 85 86 struct bpf_htab { 87 struct bpf_map map; 88 struct bpf_mem_alloc ma; 89 struct bpf_mem_alloc pcpu_ma; 90 struct bucket *buckets; 91 void *elems; 92 union { 93 struct pcpu_freelist freelist; 94 struct bpf_lru lru; 95 }; 96 struct htab_elem *__percpu *extra_elems; 97 /* number of elements in non-preallocated hashtable are kept 98 * in either pcount or count 99 */ 100 struct percpu_counter pcount; 101 atomic_t count; 102 bool use_percpu_counter; 103 u32 n_buckets; /* number of hash buckets */ 104 u32 elem_size; /* size of each element in bytes */ 105 u32 hashrnd; 106 }; 107 108 /* each htab element is struct htab_elem + key + value */ 109 struct htab_elem { 110 union { 111 struct hlist_nulls_node hash_node; 112 struct { 113 void *padding; 114 union { 115 struct pcpu_freelist_node fnode; 116 struct htab_elem *batch_flink; 117 }; 118 }; 119 }; 120 union { 121 /* pointer to per-cpu pointer */ 122 void *ptr_to_pptr; 123 struct bpf_lru_node lru_node; 124 }; 125 u32 hash; 126 char key[] __aligned(8); 127 }; 128 129 struct htab_btf_record { 130 struct btf_record *record; 131 u32 key_size; 132 }; 133 134 static inline bool htab_is_prealloc(const struct bpf_htab *htab) 135 { 136 return !(htab->map.map_flags & BPF_F_NO_PREALLOC); 137 } 138 139 static void htab_init_buckets(struct bpf_htab *htab) 140 { 141 unsigned int i; 142 143 for (i = 0; i < htab->n_buckets; i++) { 144 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i); 145 raw_res_spin_lock_init(&htab->buckets[i].raw_lock); 146 cond_resched(); 147 } 148 } 149 150 static inline int htab_lock_bucket(struct bucket *b, unsigned long *pflags) 151 { 152 unsigned long flags; 153 int ret; 154 155 ret = raw_res_spin_lock_irqsave(&b->raw_lock, flags); 156 if (ret) 157 return ret; 158 *pflags = flags; 159 return 0; 160 } 161 162 static inline void htab_unlock_bucket(struct bucket *b, unsigned long flags) 163 { 164 raw_res_spin_unlock_irqrestore(&b->raw_lock, flags); 165 } 166 167 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node); 168 169 static bool htab_is_lru(const struct bpf_htab *htab) 170 { 171 return htab->map.map_type == BPF_MAP_TYPE_LRU_HASH || 172 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH; 173 } 174 175 static bool htab_is_percpu(const struct bpf_htab *htab) 176 { 177 return htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH || 178 htab->map.map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH; 179 } 180 181 static inline bool is_fd_htab(const struct bpf_htab *htab) 182 { 183 return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS; 184 } 185 186 static inline void *htab_elem_value(struct htab_elem *l, u32 key_size) 187 { 188 return l->key + round_up(key_size, 8); 189 } 190 191 static inline void htab_elem_set_ptr(struct htab_elem *l, u32 key_size, 192 void __percpu *pptr) 193 { 194 *(void __percpu **)htab_elem_value(l, key_size) = pptr; 195 } 196 197 static inline void __percpu *htab_elem_get_ptr(struct htab_elem *l, u32 key_size) 198 { 199 return *(void __percpu **)htab_elem_value(l, key_size); 200 } 201 202 static void *fd_htab_map_get_ptr(const struct bpf_map *map, struct htab_elem *l) 203 { 204 return *(void **)htab_elem_value(l, map->key_size); 205 } 206 207 static struct htab_elem *get_htab_elem(struct bpf_htab *htab, int i) 208 { 209 return (struct htab_elem *) (htab->elems + i * (u64)htab->elem_size); 210 } 211 212 /* Both percpu and fd htab support in-place update, so no need for 213 * extra elem. LRU itself can remove the least used element, so 214 * there is no need for an extra elem during map_update. 215 */ 216 static bool htab_has_extra_elems(struct bpf_htab *htab) 217 { 218 return !htab_is_percpu(htab) && !htab_is_lru(htab) && !is_fd_htab(htab); 219 } 220 221 static void htab_free_prealloced_internal_structs(struct bpf_htab *htab) 222 { 223 u32 num_entries = htab->map.max_entries; 224 int i; 225 226 if (htab_has_extra_elems(htab)) 227 num_entries += num_possible_cpus(); 228 229 for (i = 0; i < num_entries; i++) { 230 struct htab_elem *elem; 231 232 elem = get_htab_elem(htab, i); 233 bpf_map_free_internal_structs(&htab->map, 234 htab_elem_value(elem, htab->map.key_size)); 235 cond_resched(); 236 } 237 } 238 239 static void htab_free_prealloced_fields(struct bpf_htab *htab) 240 { 241 u32 num_entries = htab->map.max_entries; 242 int i; 243 244 if (IS_ERR_OR_NULL(htab->map.record)) 245 return; 246 /* 247 * Preallocated maps do not have a bpf_mem_alloc destructor, so fully 248 * destroy every element, including the extra elements. 249 */ 250 if (htab_has_extra_elems(htab)) 251 num_entries += num_possible_cpus(); 252 for (i = 0; i < num_entries; i++) { 253 struct htab_elem *elem; 254 255 elem = get_htab_elem(htab, i); 256 if (htab_is_percpu(htab)) { 257 void __percpu *pptr = htab_elem_get_ptr(elem, htab->map.key_size); 258 int cpu; 259 260 for_each_possible_cpu(cpu) { 261 bpf_obj_free_fields(htab->map.record, per_cpu_ptr(pptr, cpu)); 262 cond_resched(); 263 } 264 } else { 265 bpf_obj_free_fields(htab->map.record, 266 htab_elem_value(elem, htab->map.key_size)); 267 cond_resched(); 268 } 269 cond_resched(); 270 } 271 } 272 273 static void htab_free_elems(struct bpf_htab *htab) 274 { 275 int i; 276 277 if (!htab_is_percpu(htab)) 278 goto free_elems; 279 280 for (i = 0; i < htab->map.max_entries; i++) { 281 void __percpu *pptr; 282 283 pptr = htab_elem_get_ptr(get_htab_elem(htab, i), 284 htab->map.key_size); 285 free_percpu(pptr); 286 cond_resched(); 287 } 288 free_elems: 289 bpf_map_area_free(htab->elems); 290 } 291 292 /* The LRU list has a lock (lru_lock). Each htab bucket has a lock 293 * (bucket_lock). If both locks need to be acquired together, the lock 294 * order is always lru_lock -> bucket_lock and this only happens in 295 * bpf_lru_list.c logic. For example, certain code path of 296 * bpf_lru_pop_free(), which is called by function prealloc_lru_pop(), 297 * will acquire lru_lock first followed by acquiring bucket_lock. 298 * 299 * In hashtab.c, to avoid deadlock, lock acquisition of 300 * bucket_lock followed by lru_lock is not allowed. In such cases, 301 * bucket_lock needs to be released first before acquiring lru_lock. 302 */ 303 static struct htab_elem *prealloc_lru_pop(struct bpf_htab *htab, void *key, 304 u32 hash) 305 { 306 struct bpf_lru_node *node = bpf_lru_pop_free(&htab->lru, hash); 307 struct htab_elem *l; 308 309 if (node) { 310 bpf_map_inc_elem_count(&htab->map); 311 l = container_of(node, struct htab_elem, lru_node); 312 memcpy(l->key, key, htab->map.key_size); 313 return l; 314 } 315 316 return NULL; 317 } 318 319 static int prealloc_init(struct bpf_htab *htab) 320 { 321 u32 num_entries = htab->map.max_entries; 322 int err = -ENOMEM, i; 323 324 if (htab_has_extra_elems(htab)) 325 num_entries += num_possible_cpus(); 326 327 htab->elems = bpf_map_area_alloc((u64)htab->elem_size * num_entries, 328 htab->map.numa_node); 329 if (!htab->elems) 330 return -ENOMEM; 331 332 if (!htab_is_percpu(htab)) 333 goto skip_percpu_elems; 334 335 for (i = 0; i < num_entries; i++) { 336 u32 size = round_up(htab->map.value_size, 8); 337 void __percpu *pptr; 338 339 pptr = bpf_map_alloc_percpu(&htab->map, size, 8, 340 GFP_USER | __GFP_NOWARN); 341 if (!pptr) 342 goto free_elems; 343 htab_elem_set_ptr(get_htab_elem(htab, i), htab->map.key_size, 344 pptr); 345 cond_resched(); 346 } 347 348 skip_percpu_elems: 349 if (htab_is_lru(htab)) 350 err = bpf_lru_init(&htab->lru, 351 htab->map.map_flags & BPF_F_NO_COMMON_LRU, 352 offsetof(struct htab_elem, hash) - 353 offsetof(struct htab_elem, lru_node), 354 htab_lru_map_delete_node, 355 htab); 356 else 357 err = pcpu_freelist_init(&htab->freelist); 358 359 if (err) 360 goto free_elems; 361 362 if (htab_is_lru(htab)) 363 bpf_lru_populate(&htab->lru, htab->elems, 364 offsetof(struct htab_elem, lru_node), 365 htab->elem_size, num_entries); 366 else 367 pcpu_freelist_populate(&htab->freelist, 368 htab->elems + offsetof(struct htab_elem, fnode), 369 htab->elem_size, num_entries); 370 371 return 0; 372 373 free_elems: 374 htab_free_elems(htab); 375 return err; 376 } 377 378 static void prealloc_destroy(struct bpf_htab *htab) 379 { 380 htab_free_elems(htab); 381 382 if (htab_is_lru(htab)) 383 bpf_lru_destroy(&htab->lru); 384 else 385 pcpu_freelist_destroy(&htab->freelist); 386 } 387 388 static int alloc_extra_elems(struct bpf_htab *htab) 389 { 390 struct htab_elem *__percpu *pptr, *l_new; 391 struct pcpu_freelist_node *l; 392 int cpu; 393 394 pptr = bpf_map_alloc_percpu(&htab->map, sizeof(struct htab_elem *), 8, 395 GFP_USER | __GFP_NOWARN); 396 if (!pptr) 397 return -ENOMEM; 398 399 for_each_possible_cpu(cpu) { 400 l = pcpu_freelist_pop(&htab->freelist); 401 /* pop will succeed, since prealloc_init() 402 * preallocated extra num_possible_cpus elements 403 */ 404 l_new = container_of(l, struct htab_elem, fnode); 405 *per_cpu_ptr(pptr, cpu) = l_new; 406 } 407 htab->extra_elems = pptr; 408 return 0; 409 } 410 411 /* Called from syscall */ 412 static int htab_map_alloc_check(union bpf_attr *attr) 413 { 414 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH || 415 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH); 416 bool lru = (attr->map_type == BPF_MAP_TYPE_LRU_HASH || 417 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH); 418 /* percpu_lru means each cpu has its own LRU list. 419 * it is different from BPF_MAP_TYPE_PERCPU_HASH where 420 * the map's value itself is percpu. percpu_lru has 421 * nothing to do with the map's value. 422 */ 423 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU); 424 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC); 425 bool zero_seed = (attr->map_flags & BPF_F_ZERO_SEED); 426 int numa_node = bpf_map_attr_numa_node(attr); 427 428 BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) != 429 offsetof(struct htab_elem, hash_node.pprev)); 430 431 if (zero_seed && !capable(CAP_SYS_ADMIN)) 432 /* Guard against local DoS, and discourage production use. */ 433 return -EPERM; 434 435 if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK || 436 !bpf_map_flags_access_ok(attr->map_flags)) 437 return -EINVAL; 438 439 if (!lru && percpu_lru) 440 return -EINVAL; 441 442 if (lru && !prealloc) 443 return -ENOTSUPP; 444 445 if (numa_node != NUMA_NO_NODE && (percpu || percpu_lru)) 446 return -EINVAL; 447 448 /* check sanity of attributes. 449 * value_size == 0 may be allowed in the future to use map as a set 450 */ 451 if (attr->max_entries == 0 || attr->key_size == 0 || 452 attr->value_size == 0) 453 return -EINVAL; 454 455 if ((u64)attr->key_size + attr->value_size >= KMALLOC_MAX_SIZE - 456 sizeof(struct htab_elem)) 457 /* if key_size + value_size is bigger, the user space won't be 458 * able to access the elements via bpf syscall. This check 459 * also makes sure that the elem_size doesn't overflow and it's 460 * kmalloc-able later in htab_map_update_elem() 461 */ 462 return -E2BIG; 463 /* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */ 464 if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE) 465 return -E2BIG; 466 467 return 0; 468 } 469 470 static void htab_mem_dtor(void *obj, void *ctx) 471 { 472 struct htab_btf_record *hrec = ctx; 473 struct htab_elem *elem = obj; 474 void *map_value; 475 476 if (IS_ERR_OR_NULL(hrec->record)) 477 return; 478 479 map_value = htab_elem_value(elem, hrec->key_size); 480 bpf_obj_free_fields(hrec->record, map_value); 481 } 482 483 static void htab_pcpu_mem_dtor(void *obj, void *ctx) 484 { 485 void __percpu *pptr = *(void __percpu **)obj; 486 struct htab_btf_record *hrec = ctx; 487 int cpu; 488 489 if (IS_ERR_OR_NULL(hrec->record)) 490 return; 491 492 for_each_possible_cpu(cpu) 493 bpf_obj_free_fields(hrec->record, per_cpu_ptr(pptr, cpu)); 494 } 495 496 static void htab_dtor_ctx_free(void *ctx) 497 { 498 struct htab_btf_record *hrec = ctx; 499 500 btf_record_free(hrec->record); 501 kfree(ctx); 502 } 503 504 static int bpf_ma_set_dtor(struct bpf_map *map, struct bpf_mem_alloc *ma, 505 void (*dtor)(void *, void *)) 506 { 507 struct htab_btf_record *hrec; 508 int err; 509 510 /* No need for dtors. */ 511 if (IS_ERR_OR_NULL(map->record)) 512 return 0; 513 514 hrec = kzalloc(sizeof(*hrec), GFP_KERNEL); 515 if (!hrec) 516 return -ENOMEM; 517 hrec->key_size = map->key_size; 518 hrec->record = btf_record_dup(map->record); 519 if (IS_ERR(hrec->record)) { 520 err = PTR_ERR(hrec->record); 521 kfree(hrec); 522 return err; 523 } 524 bpf_mem_alloc_set_dtor(ma, dtor, htab_dtor_ctx_free, hrec); 525 return 0; 526 } 527 528 static int htab_map_check_btf(struct bpf_map *map, const struct btf *btf, 529 const struct btf_type *key_type, const struct btf_type *value_type) 530 { 531 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 532 533 if (htab_is_prealloc(htab)) 534 return 0; 535 /* 536 * We must set the dtor using this callback, as map's BTF record is not 537 * populated in htab_map_alloc(), so it will always appear as NULL. 538 */ 539 if (htab_is_percpu(htab)) 540 return bpf_ma_set_dtor(map, &htab->pcpu_ma, htab_pcpu_mem_dtor); 541 else 542 return bpf_ma_set_dtor(map, &htab->ma, htab_mem_dtor); 543 } 544 545 static struct bpf_map *htab_map_alloc(union bpf_attr *attr) 546 { 547 bool percpu = (attr->map_type == BPF_MAP_TYPE_PERCPU_HASH || 548 attr->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH); 549 /* percpu_lru means each cpu has its own LRU list. 550 * it is different from BPF_MAP_TYPE_PERCPU_HASH where 551 * the map's value itself is percpu. percpu_lru has 552 * nothing to do with the map's value. 553 */ 554 bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU); 555 bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC); 556 struct bpf_htab *htab; 557 int err; 558 559 htab = bpf_map_area_alloc(sizeof(*htab), NUMA_NO_NODE); 560 if (!htab) 561 return ERR_PTR(-ENOMEM); 562 563 bpf_map_init_from_attr(&htab->map, attr); 564 565 if (percpu_lru) { 566 /* ensure each CPU's lru list has >=1 elements. 567 * since we are at it, make each lru list has the same 568 * number of elements. 569 */ 570 htab->map.max_entries = roundup(attr->max_entries, 571 num_possible_cpus()); 572 if (htab->map.max_entries < attr->max_entries) 573 htab->map.max_entries = rounddown(attr->max_entries, 574 num_possible_cpus()); 575 } 576 577 /* hash table size must be power of 2; roundup_pow_of_two() can overflow 578 * into UB on 32-bit arches, so check that first 579 */ 580 err = -E2BIG; 581 if (htab->map.max_entries > 1UL << 31) 582 goto free_htab; 583 584 htab->n_buckets = roundup_pow_of_two(htab->map.max_entries); 585 586 htab->elem_size = sizeof(struct htab_elem) + 587 round_up(htab->map.key_size, 8); 588 if (percpu) 589 htab->elem_size += sizeof(void *); 590 else 591 htab->elem_size += round_up(htab->map.value_size, 8); 592 593 /* check for u32 overflow */ 594 if (htab->n_buckets > U32_MAX / sizeof(struct bucket)) 595 goto free_htab; 596 597 err = bpf_map_init_elem_count(&htab->map); 598 if (err) 599 goto free_htab; 600 601 err = -ENOMEM; 602 htab->buckets = bpf_map_area_alloc(htab->n_buckets * 603 sizeof(struct bucket), 604 htab->map.numa_node); 605 if (!htab->buckets) 606 goto free_elem_count; 607 608 if (htab->map.map_flags & BPF_F_ZERO_SEED) 609 htab->hashrnd = 0; 610 else 611 htab->hashrnd = get_random_u32(); 612 613 htab_init_buckets(htab); 614 615 /* compute_batch_value() computes batch value as num_online_cpus() * 2 616 * and __percpu_counter_compare() needs 617 * htab->max_entries - cur_number_of_elems to be more than batch * num_online_cpus() 618 * for percpu_counter to be faster than atomic_t. In practice the average bpf 619 * hash map size is 10k, which means that a system with 64 cpus will fill 620 * hashmap to 20% of 10k before percpu_counter becomes ineffective. Therefore 621 * define our own batch count as 32 then 10k hash map can be filled up to 80%: 622 * 10k - 8k > 32 _batch_ * 64 _cpus_ 623 * and __percpu_counter_compare() will still be fast. At that point hash map 624 * collisions will dominate its performance anyway. Assume that hash map filled 625 * to 50+% isn't going to be O(1) and use the following formula to choose 626 * between percpu_counter and atomic_t. 627 */ 628 #define PERCPU_COUNTER_BATCH 32 629 if (attr->max_entries / 2 > num_online_cpus() * PERCPU_COUNTER_BATCH) 630 htab->use_percpu_counter = true; 631 632 if (htab->use_percpu_counter) { 633 err = percpu_counter_init(&htab->pcount, 0, GFP_KERNEL); 634 if (err) 635 goto free_map_locked; 636 } 637 638 if (prealloc) { 639 err = prealloc_init(htab); 640 if (err) 641 goto free_map_locked; 642 643 if (htab_has_extra_elems(htab)) { 644 err = alloc_extra_elems(htab); 645 if (err) 646 goto free_prealloc; 647 } 648 } else { 649 err = bpf_mem_alloc_init(&htab->ma, htab->elem_size, false); 650 if (err) 651 goto free_map_locked; 652 if (percpu) { 653 err = bpf_mem_alloc_init(&htab->pcpu_ma, 654 round_up(htab->map.value_size, 8), true); 655 if (err) 656 goto free_map_locked; 657 } 658 } 659 660 return &htab->map; 661 662 free_prealloc: 663 prealloc_destroy(htab); 664 free_map_locked: 665 if (htab->use_percpu_counter) 666 percpu_counter_destroy(&htab->pcount); 667 bpf_map_area_free(htab->buckets); 668 bpf_mem_alloc_destroy(&htab->pcpu_ma); 669 bpf_mem_alloc_destroy(&htab->ma); 670 free_elem_count: 671 bpf_map_free_elem_count(&htab->map); 672 free_htab: 673 bpf_map_area_free(htab); 674 return ERR_PTR(err); 675 } 676 677 static inline u32 htab_map_hash(const void *key, u32 key_len, u32 hashrnd) 678 { 679 if (likely(key_len % 4 == 0)) 680 return jhash2(key, key_len / 4, hashrnd); 681 return jhash(key, key_len, hashrnd); 682 } 683 684 static inline struct bucket *__select_bucket(struct bpf_htab *htab, u32 hash) 685 { 686 return &htab->buckets[hash & (htab->n_buckets - 1)]; 687 } 688 689 static inline struct hlist_nulls_head *select_bucket(struct bpf_htab *htab, u32 hash) 690 { 691 return &__select_bucket(htab, hash)->head; 692 } 693 694 /* this lookup function can only be called with bucket lock taken */ 695 static struct htab_elem *lookup_elem_raw(struct hlist_nulls_head *head, u32 hash, 696 void *key, u32 key_size) 697 { 698 struct hlist_nulls_node *n; 699 struct htab_elem *l; 700 701 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node) 702 if (l->hash == hash && !memcmp(&l->key, key, key_size)) 703 return l; 704 705 return NULL; 706 } 707 708 /* can be called without bucket lock. it will repeat the loop in 709 * the unlikely event when elements moved from one bucket into another 710 * while link list is being walked 711 */ 712 static struct htab_elem *lookup_nulls_elem_raw(struct hlist_nulls_head *head, 713 u32 hash, void *key, 714 u32 key_size, u32 n_buckets) 715 { 716 struct hlist_nulls_node *n; 717 struct htab_elem *l; 718 719 again: 720 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node) 721 if (l->hash == hash && !memcmp(&l->key, key, key_size)) 722 return l; 723 724 if (unlikely(get_nulls_value(n) != (hash & (n_buckets - 1)))) 725 goto again; 726 727 return NULL; 728 } 729 730 /* Called from syscall or from eBPF program directly, so 731 * arguments have to match bpf_map_lookup_elem() exactly. 732 * The return value is adjusted by BPF instructions 733 * in htab_map_gen_lookup(). 734 */ 735 static void *__htab_map_lookup_elem(struct bpf_map *map, void *key) 736 { 737 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 738 struct hlist_nulls_head *head; 739 struct htab_elem *l; 740 u32 hash, key_size; 741 742 WARN_ON_ONCE(!bpf_rcu_lock_held()); 743 744 key_size = map->key_size; 745 746 hash = htab_map_hash(key, key_size, htab->hashrnd); 747 748 head = select_bucket(htab, hash); 749 750 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets); 751 752 return l; 753 } 754 755 static void *htab_map_lookup_elem(struct bpf_map *map, void *key) 756 { 757 struct htab_elem *l = __htab_map_lookup_elem(map, key); 758 759 if (l) 760 return htab_elem_value(l, map->key_size); 761 762 return NULL; 763 } 764 765 /* inline bpf_map_lookup_elem() call. 766 * Instead of: 767 * bpf_prog 768 * bpf_map_lookup_elem 769 * map->ops->map_lookup_elem 770 * htab_map_lookup_elem 771 * __htab_map_lookup_elem 772 * do: 773 * bpf_prog 774 * __htab_map_lookup_elem 775 */ 776 static int htab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf) 777 { 778 struct bpf_insn *insn = insn_buf; 779 const int ret = BPF_REG_0; 780 781 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem, 782 (void *(*)(struct bpf_map *map, void *key))NULL)); 783 *insn++ = BPF_EMIT_CALL(__htab_map_lookup_elem); 784 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1); 785 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret, 786 offsetof(struct htab_elem, key) + 787 round_up(map->key_size, 8)); 788 return insn - insn_buf; 789 } 790 791 static __always_inline void *__htab_lru_map_lookup_elem(struct bpf_map *map, 792 void *key, const bool mark) 793 { 794 struct htab_elem *l = __htab_map_lookup_elem(map, key); 795 796 if (l) { 797 if (mark) 798 bpf_lru_node_set_ref(&l->lru_node); 799 return htab_elem_value(l, map->key_size); 800 } 801 802 return NULL; 803 } 804 805 static void *htab_lru_map_lookup_elem(struct bpf_map *map, void *key) 806 { 807 return __htab_lru_map_lookup_elem(map, key, true); 808 } 809 810 static void *htab_lru_map_lookup_elem_sys(struct bpf_map *map, void *key) 811 { 812 return __htab_lru_map_lookup_elem(map, key, false); 813 } 814 815 static int htab_lru_map_gen_lookup(struct bpf_map *map, 816 struct bpf_insn *insn_buf) 817 { 818 struct bpf_insn *insn = insn_buf; 819 const int ret = BPF_REG_0; 820 const int ref_reg = BPF_REG_1; 821 822 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem, 823 (void *(*)(struct bpf_map *map, void *key))NULL)); 824 *insn++ = BPF_EMIT_CALL(__htab_map_lookup_elem); 825 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 4); 826 *insn++ = BPF_LDX_MEM(BPF_B, ref_reg, ret, 827 offsetof(struct htab_elem, lru_node) + 828 offsetof(struct bpf_lru_node, ref)); 829 *insn++ = BPF_JMP_IMM(BPF_JNE, ref_reg, 0, 1); 830 *insn++ = BPF_ST_MEM(BPF_B, ret, 831 offsetof(struct htab_elem, lru_node) + 832 offsetof(struct bpf_lru_node, ref), 833 1); 834 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret, 835 offsetof(struct htab_elem, key) + 836 round_up(map->key_size, 8)); 837 return insn - insn_buf; 838 } 839 840 static void check_and_cancel_fields(struct bpf_htab *htab, 841 struct htab_elem *elem) 842 { 843 if (IS_ERR_OR_NULL(htab->map.record)) 844 return; 845 846 if (htab_is_percpu(htab)) { 847 void __percpu *pptr = htab_elem_get_ptr(elem, htab->map.key_size); 848 int cpu; 849 850 for_each_possible_cpu(cpu) 851 bpf_obj_cancel_fields(&htab->map, per_cpu_ptr(pptr, cpu)); 852 } else { 853 void *map_value = htab_elem_value(elem, htab->map.key_size); 854 855 bpf_obj_cancel_fields(&htab->map, map_value); 856 } 857 } 858 859 /* It is called from the bpf_lru_list when the LRU needs to delete 860 * older elements from the htab. 861 */ 862 static bool htab_lru_map_delete_node(void *arg, struct bpf_lru_node *node) 863 { 864 struct bpf_htab *htab = arg; 865 struct htab_elem *l = NULL, *tgt_l; 866 struct hlist_nulls_head *head; 867 struct hlist_nulls_node *n; 868 unsigned long flags; 869 struct bucket *b; 870 int ret; 871 872 tgt_l = container_of(node, struct htab_elem, lru_node); 873 b = __select_bucket(htab, tgt_l->hash); 874 head = &b->head; 875 876 ret = htab_lock_bucket(b, &flags); 877 if (ret) 878 return false; 879 880 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node) 881 if (l == tgt_l) { 882 hlist_nulls_del_rcu(&l->hash_node); 883 bpf_map_dec_elem_count(&htab->map); 884 break; 885 } 886 887 htab_unlock_bucket(b, flags); 888 889 if (l == tgt_l) 890 check_and_cancel_fields(htab, l); 891 return l == tgt_l; 892 } 893 894 /* Called from syscall */ 895 static int htab_map_get_next_key(struct bpf_map *map, void *key, void *next_key) 896 { 897 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 898 struct hlist_nulls_head *head; 899 struct htab_elem *l, *next_l; 900 u32 hash, key_size; 901 int i = 0; 902 903 WARN_ON_ONCE(!rcu_read_lock_held()); 904 905 key_size = map->key_size; 906 907 if (!key) 908 goto find_first_elem; 909 910 hash = htab_map_hash(key, key_size, htab->hashrnd); 911 912 head = select_bucket(htab, hash); 913 914 /* lookup the key */ 915 l = lookup_nulls_elem_raw(head, hash, key, key_size, htab->n_buckets); 916 917 if (!l) 918 goto find_first_elem; 919 920 /* key was found, get next key in the same bucket */ 921 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_next_rcu(&l->hash_node)), 922 struct htab_elem, hash_node); 923 924 if (next_l) { 925 /* if next elem in this hash list is non-zero, just return it */ 926 memcpy(next_key, next_l->key, key_size); 927 return 0; 928 } 929 930 /* no more elements in this hash list, go to the next bucket */ 931 i = hash & (htab->n_buckets - 1); 932 i++; 933 934 find_first_elem: 935 /* iterate over buckets */ 936 for (; i < htab->n_buckets; i++) { 937 head = select_bucket(htab, i); 938 939 /* pick first element in the bucket */ 940 next_l = hlist_nulls_entry_safe(rcu_dereference_raw(hlist_nulls_first_rcu(head)), 941 struct htab_elem, hash_node); 942 if (next_l) { 943 /* if it's not empty, just return it */ 944 memcpy(next_key, next_l->key, key_size); 945 return 0; 946 } 947 } 948 949 /* iterated over all buckets and all elements */ 950 return -ENOENT; 951 } 952 953 static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l) 954 { 955 check_and_cancel_fields(htab, l); 956 957 if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH) 958 bpf_mem_cache_free(&htab->pcpu_ma, l->ptr_to_pptr); 959 bpf_mem_cache_free(&htab->ma, l); 960 } 961 962 static void htab_put_fd_value(struct bpf_htab *htab, struct htab_elem *l) 963 { 964 struct bpf_map *map = &htab->map; 965 void *ptr; 966 967 if (map->ops->map_fd_put_ptr) { 968 ptr = fd_htab_map_get_ptr(map, l); 969 map->ops->map_fd_put_ptr(map, ptr, true); 970 } 971 } 972 973 static bool is_map_full(struct bpf_htab *htab) 974 { 975 if (htab->use_percpu_counter) 976 return __percpu_counter_compare(&htab->pcount, htab->map.max_entries, 977 PERCPU_COUNTER_BATCH) >= 0; 978 return atomic_read(&htab->count) >= htab->map.max_entries; 979 } 980 981 static void inc_elem_count(struct bpf_htab *htab) 982 { 983 bpf_map_inc_elem_count(&htab->map); 984 985 if (htab->use_percpu_counter) 986 percpu_counter_add_batch(&htab->pcount, 1, PERCPU_COUNTER_BATCH); 987 else 988 atomic_inc(&htab->count); 989 } 990 991 static void dec_elem_count(struct bpf_htab *htab) 992 { 993 bpf_map_dec_elem_count(&htab->map); 994 995 if (htab->use_percpu_counter) 996 percpu_counter_add_batch(&htab->pcount, -1, PERCPU_COUNTER_BATCH); 997 else 998 atomic_dec(&htab->count); 999 } 1000 1001 static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l) 1002 { 1003 htab_put_fd_value(htab, l); 1004 1005 if (htab_is_prealloc(htab)) { 1006 bpf_map_dec_elem_count(&htab->map); 1007 check_and_cancel_fields(htab, l); 1008 pcpu_freelist_push(&htab->freelist, &l->fnode); 1009 } else { 1010 dec_elem_count(htab); 1011 htab_elem_free(htab, l); 1012 } 1013 } 1014 1015 static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr, 1016 void *value, bool onallcpus, u64 map_flags) 1017 { 1018 void *ptr; 1019 1020 if (!onallcpus) { 1021 /* copy true value_size bytes */ 1022 ptr = this_cpu_ptr(pptr); 1023 copy_map_value(&htab->map, ptr, value); 1024 bpf_obj_cancel_fields(&htab->map, ptr); 1025 } else { 1026 u32 size = round_up(htab->map.value_size, 8); 1027 void *val; 1028 int cpu; 1029 1030 if (map_flags & BPF_F_CPU) { 1031 cpu = map_flags >> 32; 1032 ptr = per_cpu_ptr(pptr, cpu); 1033 copy_map_value(&htab->map, ptr, value); 1034 bpf_obj_cancel_fields(&htab->map, ptr); 1035 return; 1036 } 1037 1038 for_each_possible_cpu(cpu) { 1039 ptr = per_cpu_ptr(pptr, cpu); 1040 val = (map_flags & BPF_F_ALL_CPUS) ? value : value + size * cpu; 1041 copy_map_value(&htab->map, ptr, val); 1042 bpf_obj_cancel_fields(&htab->map, ptr); 1043 } 1044 } 1045 } 1046 1047 static void pcpu_init_value(struct bpf_htab *htab, void __percpu *pptr, 1048 void *value, bool onallcpus, u64 map_flags) 1049 { 1050 /* When not setting the initial value on all cpus, zero-fill element 1051 * values for other cpus. Otherwise, bpf program has no way to ensure 1052 * known initial values for cpus other than current one 1053 * (onallcpus=false always when coming from bpf prog). 1054 */ 1055 if (!onallcpus) { 1056 int current_cpu = raw_smp_processor_id(); 1057 int cpu; 1058 1059 for_each_possible_cpu(cpu) { 1060 if (cpu == current_cpu) 1061 copy_map_value(&htab->map, per_cpu_ptr(pptr, cpu), value); 1062 else /* Since elem is preallocated, we cannot touch special fields */ 1063 zero_map_value(&htab->map, per_cpu_ptr(pptr, cpu)); 1064 } 1065 } else { 1066 pcpu_copy_value(htab, pptr, value, onallcpus, map_flags); 1067 } 1068 } 1069 1070 static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab) 1071 { 1072 return is_fd_htab(htab) && BITS_PER_LONG == 64; 1073 } 1074 1075 static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key, 1076 void *value, u32 key_size, u32 hash, 1077 bool percpu, bool onallcpus, 1078 struct htab_elem *old_elem, u64 map_flags) 1079 { 1080 u32 size = htab->map.value_size; 1081 bool prealloc = htab_is_prealloc(htab); 1082 struct htab_elem *l_new, **pl_new; 1083 void __percpu *pptr; 1084 1085 if (prealloc) { 1086 if (old_elem) { 1087 /* if we're updating the existing element, 1088 * use per-cpu extra elems to avoid freelist_pop/push 1089 */ 1090 pl_new = this_cpu_ptr(htab->extra_elems); 1091 l_new = *pl_new; 1092 *pl_new = old_elem; 1093 } else { 1094 struct pcpu_freelist_node *l; 1095 1096 l = __pcpu_freelist_pop(&htab->freelist); 1097 if (!l) 1098 return ERR_PTR(-E2BIG); 1099 l_new = container_of(l, struct htab_elem, fnode); 1100 bpf_map_inc_elem_count(&htab->map); 1101 } 1102 } else { 1103 if (is_map_full(htab)) 1104 if (!old_elem) 1105 /* when map is full and update() is replacing 1106 * old element, it's ok to allocate, since 1107 * old element will be freed immediately. 1108 * Otherwise return an error 1109 */ 1110 return ERR_PTR(-E2BIG); 1111 inc_elem_count(htab); 1112 l_new = bpf_mem_cache_alloc(&htab->ma); 1113 if (!l_new) { 1114 l_new = ERR_PTR(-ENOMEM); 1115 goto dec_count; 1116 } 1117 } 1118 1119 memcpy(l_new->key, key, key_size); 1120 if (percpu) { 1121 if (prealloc) { 1122 pptr = htab_elem_get_ptr(l_new, key_size); 1123 } else { 1124 /* alloc_percpu zero-fills */ 1125 void *ptr = bpf_mem_cache_alloc(&htab->pcpu_ma); 1126 1127 if (!ptr) { 1128 bpf_mem_cache_free(&htab->ma, l_new); 1129 l_new = ERR_PTR(-ENOMEM); 1130 goto dec_count; 1131 } 1132 l_new->ptr_to_pptr = ptr; 1133 pptr = *(void __percpu **)ptr; 1134 } 1135 1136 pcpu_init_value(htab, pptr, value, onallcpus, map_flags); 1137 1138 if (!prealloc) 1139 htab_elem_set_ptr(l_new, key_size, pptr); 1140 } else if (fd_htab_map_needs_adjust(htab)) { 1141 size = round_up(size, 8); 1142 memcpy(htab_elem_value(l_new, key_size), value, size); 1143 } else if (map_flags & BPF_F_LOCK) { 1144 copy_map_value_locked(&htab->map, 1145 htab_elem_value(l_new, key_size), 1146 value, false); 1147 } else { 1148 copy_map_value(&htab->map, htab_elem_value(l_new, key_size), value); 1149 } 1150 1151 l_new->hash = hash; 1152 return l_new; 1153 dec_count: 1154 dec_elem_count(htab); 1155 return l_new; 1156 } 1157 1158 static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old, 1159 u64 map_flags) 1160 { 1161 if (l_old && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST) 1162 /* elem already exists */ 1163 return -EEXIST; 1164 1165 if (!l_old && (map_flags & ~BPF_F_LOCK) == BPF_EXIST) 1166 /* elem doesn't exist, cannot update it */ 1167 return -ENOENT; 1168 1169 return 0; 1170 } 1171 1172 /* Called from syscall or from eBPF program */ 1173 static long htab_map_update_elem(struct bpf_map *map, void *key, void *value, 1174 u64 map_flags) 1175 { 1176 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 1177 struct htab_elem *l_new, *l_old; 1178 struct hlist_nulls_head *head; 1179 unsigned long flags; 1180 struct bucket *b; 1181 u32 key_size, hash; 1182 int ret; 1183 1184 if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST)) 1185 /* unknown flags */ 1186 return -EINVAL; 1187 1188 WARN_ON_ONCE(!bpf_rcu_lock_held()); 1189 1190 key_size = map->key_size; 1191 1192 hash = htab_map_hash(key, key_size, htab->hashrnd); 1193 1194 b = __select_bucket(htab, hash); 1195 head = &b->head; 1196 1197 if (unlikely(map_flags & BPF_F_LOCK)) { 1198 if (unlikely(!btf_record_has_field(map->record, BPF_SPIN_LOCK))) 1199 return -EINVAL; 1200 /* find an element without taking the bucket lock */ 1201 l_old = lookup_nulls_elem_raw(head, hash, key, key_size, 1202 htab->n_buckets); 1203 ret = check_flags(htab, l_old, map_flags); 1204 if (ret) 1205 return ret; 1206 if (l_old) { 1207 /* grab the element lock and update value in place */ 1208 copy_map_value_locked(map, 1209 htab_elem_value(l_old, key_size), 1210 value, false); 1211 return 0; 1212 } 1213 /* fall through, grab the bucket lock and lookup again. 1214 * 99.9% chance that the element won't be found, 1215 * but second lookup under lock has to be done. 1216 */ 1217 } 1218 1219 ret = htab_lock_bucket(b, &flags); 1220 if (ret) 1221 return ret; 1222 1223 l_old = lookup_elem_raw(head, hash, key, key_size); 1224 1225 ret = check_flags(htab, l_old, map_flags); 1226 if (ret) 1227 goto err; 1228 1229 if (unlikely(l_old && (map_flags & BPF_F_LOCK))) { 1230 /* first lookup without the bucket lock didn't find the element, 1231 * but second lookup with the bucket lock found it. 1232 * This case is highly unlikely, but has to be dealt with: 1233 * grab the element lock in addition to the bucket lock 1234 * and update element in place 1235 */ 1236 copy_map_value_locked(map, 1237 htab_elem_value(l_old, key_size), 1238 value, false); 1239 ret = 0; 1240 goto err; 1241 } 1242 1243 l_new = alloc_htab_elem(htab, key, value, key_size, hash, false, false, 1244 l_old, map_flags); 1245 if (IS_ERR(l_new)) { 1246 /* all pre-allocated elements are in use or memory exhausted */ 1247 ret = PTR_ERR(l_new); 1248 goto err; 1249 } 1250 1251 /* add new element to the head of the list, so that 1252 * concurrent search will find it before old elem 1253 */ 1254 hlist_nulls_add_head_rcu(&l_new->hash_node, head); 1255 if (l_old) { 1256 hlist_nulls_del_rcu(&l_old->hash_node); 1257 1258 /* l_old has already been stashed in htab->extra_elems, cancel 1259 * its reusable special fields before it is available for reuse. 1260 */ 1261 if (htab_is_prealloc(htab)) 1262 check_and_cancel_fields(htab, l_old); 1263 } 1264 htab_unlock_bucket(b, flags); 1265 if (l_old && !htab_is_prealloc(htab)) 1266 free_htab_elem(htab, l_old); 1267 return 0; 1268 err: 1269 htab_unlock_bucket(b, flags); 1270 return ret; 1271 } 1272 1273 static void htab_lru_push_free(struct bpf_htab *htab, struct htab_elem *elem) 1274 { 1275 check_and_cancel_fields(htab, elem); 1276 bpf_map_dec_elem_count(&htab->map); 1277 bpf_lru_push_free(&htab->lru, &elem->lru_node); 1278 } 1279 1280 static long htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value, 1281 u64 map_flags) 1282 { 1283 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 1284 struct htab_elem *l_new, *l_old = NULL; 1285 struct hlist_nulls_head *head; 1286 unsigned long flags; 1287 struct bucket *b; 1288 u32 key_size, hash; 1289 int ret; 1290 1291 if (unlikely(map_flags > BPF_EXIST)) 1292 /* unknown flags */ 1293 return -EINVAL; 1294 1295 WARN_ON_ONCE(!bpf_rcu_lock_held()); 1296 1297 key_size = map->key_size; 1298 1299 hash = htab_map_hash(key, key_size, htab->hashrnd); 1300 1301 b = __select_bucket(htab, hash); 1302 head = &b->head; 1303 1304 /* For LRU, we need to alloc before taking bucket's 1305 * spinlock because getting free nodes from LRU may need 1306 * to remove older elements from htab and this removal 1307 * operation will need a bucket lock. 1308 */ 1309 l_new = prealloc_lru_pop(htab, key, hash); 1310 if (!l_new) 1311 return -ENOMEM; 1312 copy_map_value(&htab->map, htab_elem_value(l_new, map->key_size), value); 1313 1314 ret = htab_lock_bucket(b, &flags); 1315 if (ret) 1316 goto err_lock_bucket; 1317 1318 l_old = lookup_elem_raw(head, hash, key, key_size); 1319 1320 ret = check_flags(htab, l_old, map_flags); 1321 if (ret) 1322 goto err; 1323 1324 /* add new element to the head of the list, so that 1325 * concurrent search will find it before old elem 1326 */ 1327 hlist_nulls_add_head_rcu(&l_new->hash_node, head); 1328 if (l_old) { 1329 bpf_lru_node_set_ref(&l_new->lru_node); 1330 hlist_nulls_del_rcu(&l_old->hash_node); 1331 } 1332 ret = 0; 1333 1334 err: 1335 htab_unlock_bucket(b, flags); 1336 1337 err_lock_bucket: 1338 if (ret) 1339 htab_lru_push_free(htab, l_new); 1340 else if (l_old) 1341 htab_lru_push_free(htab, l_old); 1342 1343 return ret; 1344 } 1345 1346 static int htab_map_check_update_flags(bool onallcpus, u64 map_flags) 1347 { 1348 if (unlikely(!onallcpus && map_flags > BPF_EXIST)) 1349 return -EINVAL; 1350 if (unlikely(onallcpus && ((map_flags & BPF_F_LOCK) || (u32)map_flags > BPF_F_ALL_CPUS))) 1351 return -EINVAL; 1352 return 0; 1353 } 1354 1355 static long htab_map_update_elem_in_place(struct bpf_map *map, void *key, 1356 void *value, u64 map_flags, 1357 bool percpu, bool onallcpus) 1358 { 1359 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 1360 struct htab_elem *l_new, *l_old; 1361 struct hlist_nulls_head *head; 1362 void *old_map_ptr = NULL; 1363 unsigned long flags; 1364 struct bucket *b; 1365 u32 key_size, hash; 1366 int ret; 1367 1368 ret = htab_map_check_update_flags(onallcpus, map_flags); 1369 if (unlikely(ret)) 1370 return ret; 1371 1372 WARN_ON_ONCE(!bpf_rcu_lock_held()); 1373 1374 key_size = map->key_size; 1375 1376 hash = htab_map_hash(key, key_size, htab->hashrnd); 1377 1378 b = __select_bucket(htab, hash); 1379 head = &b->head; 1380 1381 ret = htab_lock_bucket(b, &flags); 1382 if (ret) 1383 return ret; 1384 1385 l_old = lookup_elem_raw(head, hash, key, key_size); 1386 1387 ret = check_flags(htab, l_old, map_flags); 1388 if (ret) 1389 goto err; 1390 1391 if (l_old) { 1392 /* Update value in-place */ 1393 if (percpu) { 1394 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size), 1395 value, onallcpus, map_flags); 1396 } else { 1397 void **inner_map_pptr = htab_elem_value(l_old, key_size); 1398 1399 old_map_ptr = *inner_map_pptr; 1400 WRITE_ONCE(*inner_map_pptr, *(void **)value); 1401 } 1402 } else { 1403 l_new = alloc_htab_elem(htab, key, value, key_size, 1404 hash, percpu, onallcpus, NULL, map_flags); 1405 if (IS_ERR(l_new)) { 1406 ret = PTR_ERR(l_new); 1407 goto err; 1408 } 1409 hlist_nulls_add_head_rcu(&l_new->hash_node, head); 1410 } 1411 err: 1412 htab_unlock_bucket(b, flags); 1413 if (old_map_ptr) 1414 map->ops->map_fd_put_ptr(map, old_map_ptr, true); 1415 return ret; 1416 } 1417 1418 static long __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key, 1419 void *value, u64 map_flags, 1420 bool onallcpus) 1421 { 1422 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 1423 struct htab_elem *l_new = NULL, *l_old; 1424 struct hlist_nulls_head *head; 1425 unsigned long flags; 1426 struct bucket *b; 1427 u32 key_size, hash; 1428 int ret; 1429 1430 ret = htab_map_check_update_flags(onallcpus, map_flags); 1431 if (unlikely(ret)) 1432 return ret; 1433 1434 WARN_ON_ONCE(!bpf_rcu_lock_held()); 1435 1436 key_size = map->key_size; 1437 1438 hash = htab_map_hash(key, key_size, htab->hashrnd); 1439 1440 b = __select_bucket(htab, hash); 1441 head = &b->head; 1442 1443 /* For LRU, we need to alloc before taking bucket's 1444 * spinlock because LRU's elem alloc may need 1445 * to remove older elem from htab and this removal 1446 * operation will need a bucket lock. 1447 */ 1448 if (map_flags != BPF_EXIST) { 1449 l_new = prealloc_lru_pop(htab, key, hash); 1450 if (!l_new) 1451 return -ENOMEM; 1452 } 1453 1454 ret = htab_lock_bucket(b, &flags); 1455 if (ret) 1456 goto err_lock_bucket; 1457 1458 l_old = lookup_elem_raw(head, hash, key, key_size); 1459 1460 ret = check_flags(htab, l_old, map_flags); 1461 if (ret) 1462 goto err; 1463 1464 if (l_old) { 1465 bpf_lru_node_set_ref(&l_old->lru_node); 1466 1467 /* per-cpu hash map can update value in-place */ 1468 pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size), 1469 value, onallcpus, map_flags); 1470 } else { 1471 pcpu_init_value(htab, htab_elem_get_ptr(l_new, key_size), 1472 value, onallcpus, map_flags); 1473 hlist_nulls_add_head_rcu(&l_new->hash_node, head); 1474 l_new = NULL; 1475 } 1476 ret = 0; 1477 err: 1478 htab_unlock_bucket(b, flags); 1479 err_lock_bucket: 1480 if (l_new) { 1481 bpf_map_dec_elem_count(&htab->map); 1482 bpf_lru_push_free(&htab->lru, &l_new->lru_node); 1483 } 1484 return ret; 1485 } 1486 1487 static long htab_percpu_map_update_elem(struct bpf_map *map, void *key, 1488 void *value, u64 map_flags) 1489 { 1490 return htab_map_update_elem_in_place(map, key, value, map_flags, true, false); 1491 } 1492 1493 static long htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key, 1494 void *value, u64 map_flags) 1495 { 1496 return __htab_lru_percpu_map_update_elem(map, key, value, map_flags, 1497 false); 1498 } 1499 1500 /* Called from syscall or from eBPF program */ 1501 static long htab_map_delete_elem(struct bpf_map *map, void *key) 1502 { 1503 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 1504 struct hlist_nulls_head *head; 1505 struct bucket *b; 1506 struct htab_elem *l; 1507 unsigned long flags; 1508 u32 hash, key_size; 1509 int ret; 1510 1511 WARN_ON_ONCE(!bpf_rcu_lock_held()); 1512 1513 key_size = map->key_size; 1514 1515 hash = htab_map_hash(key, key_size, htab->hashrnd); 1516 b = __select_bucket(htab, hash); 1517 head = &b->head; 1518 1519 ret = htab_lock_bucket(b, &flags); 1520 if (ret) 1521 return ret; 1522 1523 l = lookup_elem_raw(head, hash, key, key_size); 1524 if (l) 1525 hlist_nulls_del_rcu(&l->hash_node); 1526 else 1527 ret = -ENOENT; 1528 1529 htab_unlock_bucket(b, flags); 1530 1531 if (l) 1532 free_htab_elem(htab, l); 1533 return ret; 1534 } 1535 1536 static long htab_lru_map_delete_elem(struct bpf_map *map, void *key) 1537 { 1538 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 1539 struct hlist_nulls_head *head; 1540 struct bucket *b; 1541 struct htab_elem *l; 1542 unsigned long flags; 1543 u32 hash, key_size; 1544 int ret; 1545 1546 WARN_ON_ONCE(!bpf_rcu_lock_held()); 1547 1548 key_size = map->key_size; 1549 1550 hash = htab_map_hash(key, key_size, htab->hashrnd); 1551 b = __select_bucket(htab, hash); 1552 head = &b->head; 1553 1554 ret = htab_lock_bucket(b, &flags); 1555 if (ret) 1556 return ret; 1557 1558 l = lookup_elem_raw(head, hash, key, key_size); 1559 1560 if (l) 1561 hlist_nulls_del_rcu(&l->hash_node); 1562 else 1563 ret = -ENOENT; 1564 1565 htab_unlock_bucket(b, flags); 1566 if (l) 1567 htab_lru_push_free(htab, l); 1568 return ret; 1569 } 1570 1571 static void delete_all_elements(struct bpf_htab *htab) 1572 { 1573 int i; 1574 1575 /* It's called from a worker thread and migration has been disabled, 1576 * therefore, it is OK to invoke bpf_mem_cache_free() directly. 1577 */ 1578 for (i = 0; i < htab->n_buckets; i++) { 1579 struct hlist_nulls_head *head = select_bucket(htab, i); 1580 struct hlist_nulls_node *n; 1581 struct htab_elem *l; 1582 1583 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) { 1584 hlist_nulls_del_rcu(&l->hash_node); 1585 htab_elem_free(htab, l); 1586 } 1587 cond_resched(); 1588 } 1589 } 1590 1591 static void htab_free_malloced_internal_structs(struct bpf_htab *htab) 1592 { 1593 int i; 1594 1595 rcu_read_lock(); 1596 for (i = 0; i < htab->n_buckets; i++) { 1597 struct hlist_nulls_head *head = select_bucket(htab, i); 1598 struct hlist_nulls_node *n; 1599 struct htab_elem *l; 1600 1601 hlist_nulls_for_each_entry(l, n, head, hash_node) { 1602 /* We only free internal structs on uref dropping to zero */ 1603 bpf_map_free_internal_structs(&htab->map, 1604 htab_elem_value(l, htab->map.key_size)); 1605 } 1606 cond_resched_rcu(); 1607 } 1608 rcu_read_unlock(); 1609 } 1610 1611 static void htab_map_free_internal_structs(struct bpf_map *map) 1612 { 1613 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 1614 1615 /* We only free internal structs on uref dropping to zero */ 1616 if (!bpf_map_has_internal_structs(map)) 1617 return; 1618 1619 if (htab_is_prealloc(htab)) 1620 htab_free_prealloced_internal_structs(htab); 1621 else 1622 htab_free_malloced_internal_structs(htab); 1623 } 1624 1625 /* Called when map->refcnt goes to zero, either from workqueue or from syscall */ 1626 static void htab_map_free(struct bpf_map *map) 1627 { 1628 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 1629 1630 /* bpf_free_used_maps() or close(map_fd) will trigger this map_free callback. 1631 * bpf_free_used_maps() is called after bpf prog is no longer executing. 1632 * There is no need to synchronize_rcu() here to protect map elements. 1633 */ 1634 1635 /* htab no longer uses call_rcu() directly. bpf_mem_alloc does it 1636 * underneath and is responsible for waiting for callbacks to finish 1637 * during bpf_mem_alloc_destroy(). 1638 */ 1639 if (!htab_is_prealloc(htab)) { 1640 delete_all_elements(htab); 1641 } else { 1642 htab_free_prealloced_fields(htab); 1643 prealloc_destroy(htab); 1644 } 1645 1646 bpf_map_free_elem_count(map); 1647 free_percpu(htab->extra_elems); 1648 bpf_map_area_free(htab->buckets); 1649 bpf_mem_alloc_destroy(&htab->pcpu_ma); 1650 bpf_mem_alloc_destroy(&htab->ma); 1651 if (htab->use_percpu_counter) 1652 percpu_counter_destroy(&htab->pcount); 1653 bpf_map_area_free(htab); 1654 } 1655 1656 static void htab_map_seq_show_elem(struct bpf_map *map, void *key, 1657 struct seq_file *m) 1658 { 1659 void *value; 1660 1661 rcu_read_lock(); 1662 1663 value = htab_map_lookup_elem(map, key); 1664 if (!value) { 1665 rcu_read_unlock(); 1666 return; 1667 } 1668 1669 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m); 1670 seq_puts(m, ": "); 1671 btf_type_seq_show(map->btf, map->btf_value_type_id, value, m); 1672 seq_putc(m, '\n'); 1673 1674 rcu_read_unlock(); 1675 } 1676 1677 static int __htab_map_lookup_and_delete_elem(struct bpf_map *map, void *key, 1678 void *value, bool is_lru_map, 1679 bool is_percpu, u64 flags) 1680 { 1681 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 1682 struct hlist_nulls_head *head; 1683 unsigned long bflags; 1684 struct htab_elem *l; 1685 u32 hash, key_size; 1686 struct bucket *b; 1687 int ret; 1688 1689 key_size = map->key_size; 1690 1691 hash = htab_map_hash(key, key_size, htab->hashrnd); 1692 b = __select_bucket(htab, hash); 1693 head = &b->head; 1694 1695 ret = htab_lock_bucket(b, &bflags); 1696 if (ret) 1697 return ret; 1698 1699 l = lookup_elem_raw(head, hash, key, key_size); 1700 if (!l) { 1701 ret = -ENOENT; 1702 goto out_unlock; 1703 } 1704 1705 if (is_percpu) { 1706 u32 roundup_value_size = round_up(map->value_size, 8); 1707 void __percpu *pptr; 1708 int off = 0, cpu; 1709 1710 pptr = htab_elem_get_ptr(l, key_size); 1711 for_each_possible_cpu(cpu) { 1712 copy_map_value_long(&htab->map, value + off, per_cpu_ptr(pptr, cpu)); 1713 check_and_init_map_value(&htab->map, value + off); 1714 off += roundup_value_size; 1715 } 1716 } else { 1717 void *src = htab_elem_value(l, map->key_size); 1718 1719 if (flags & BPF_F_LOCK) 1720 copy_map_value_locked(map, value, src, true); 1721 else 1722 copy_map_value(map, value, src); 1723 /* Zeroing special fields in the temp buffer */ 1724 check_and_init_map_value(map, value); 1725 } 1726 hlist_nulls_del_rcu(&l->hash_node); 1727 1728 out_unlock: 1729 htab_unlock_bucket(b, bflags); 1730 1731 if (l) { 1732 if (is_lru_map) 1733 htab_lru_push_free(htab, l); 1734 else 1735 free_htab_elem(htab, l); 1736 } 1737 1738 return ret; 1739 } 1740 1741 static int htab_map_lookup_and_delete_elem(struct bpf_map *map, void *key, 1742 void *value, u64 flags) 1743 { 1744 return __htab_map_lookup_and_delete_elem(map, key, value, false, false, 1745 flags); 1746 } 1747 1748 static int htab_percpu_map_lookup_and_delete_elem(struct bpf_map *map, 1749 void *key, void *value, 1750 u64 flags) 1751 { 1752 return __htab_map_lookup_and_delete_elem(map, key, value, false, true, 1753 flags); 1754 } 1755 1756 static int htab_lru_map_lookup_and_delete_elem(struct bpf_map *map, void *key, 1757 void *value, u64 flags) 1758 { 1759 return __htab_map_lookup_and_delete_elem(map, key, value, true, false, 1760 flags); 1761 } 1762 1763 static int htab_lru_percpu_map_lookup_and_delete_elem(struct bpf_map *map, 1764 void *key, void *value, 1765 u64 flags) 1766 { 1767 return __htab_map_lookup_and_delete_elem(map, key, value, true, true, 1768 flags); 1769 } 1770 1771 static int 1772 __htab_map_lookup_and_delete_batch(struct bpf_map *map, 1773 const union bpf_attr *attr, 1774 union bpf_attr __user *uattr, 1775 bool do_delete, bool is_lru_map, 1776 bool is_percpu) 1777 { 1778 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 1779 void *keys = NULL, *values = NULL, *value, *dst_key, *dst_val; 1780 void __user *uvalues = u64_to_user_ptr(attr->batch.values); 1781 void __user *ukeys = u64_to_user_ptr(attr->batch.keys); 1782 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch); 1783 u32 batch, max_count, size, bucket_size, map_id; 1784 u64 elem_map_flags, map_flags, allowed_flags; 1785 u32 bucket_cnt, total, key_size, value_size; 1786 struct htab_elem *node_to_free = NULL; 1787 struct hlist_nulls_head *head; 1788 struct hlist_nulls_node *n; 1789 unsigned long flags = 0; 1790 bool locked = false; 1791 struct htab_elem *l; 1792 struct bucket *b; 1793 int ret = 0; 1794 1795 elem_map_flags = attr->batch.elem_flags; 1796 allowed_flags = BPF_F_LOCK; 1797 if (!do_delete && is_percpu) 1798 allowed_flags |= BPF_F_CPU; 1799 ret = bpf_map_check_op_flags(map, elem_map_flags, allowed_flags); 1800 if (ret) 1801 return ret; 1802 1803 map_flags = attr->batch.flags; 1804 if (map_flags) 1805 return -EINVAL; 1806 1807 max_count = attr->batch.count; 1808 if (!max_count) 1809 return 0; 1810 1811 if (put_user(0, &uattr->batch.count)) 1812 return -EFAULT; 1813 1814 batch = 0; 1815 if (ubatch && copy_from_user(&batch, ubatch, sizeof(batch))) 1816 return -EFAULT; 1817 1818 if (batch >= htab->n_buckets) 1819 return -ENOENT; 1820 1821 key_size = htab->map.key_size; 1822 value_size = htab->map.value_size; 1823 size = round_up(value_size, 8); 1824 if (is_percpu && !(elem_map_flags & BPF_F_CPU)) 1825 value_size = size * num_possible_cpus(); 1826 total = 0; 1827 /* while experimenting with hash tables with sizes ranging from 10 to 1828 * 1000, it was observed that a bucket can have up to 5 entries. 1829 */ 1830 bucket_size = 5; 1831 1832 alloc: 1833 /* We cannot do copy_from_user or copy_to_user inside 1834 * the rcu_read_lock. Allocate enough space here. 1835 */ 1836 keys = kvmalloc_array(key_size, bucket_size, GFP_USER | __GFP_NOWARN); 1837 values = kvmalloc_array(value_size, bucket_size, GFP_USER | __GFP_NOWARN); 1838 if (!keys || !values) { 1839 ret = -ENOMEM; 1840 goto after_loop; 1841 } 1842 1843 again: 1844 bpf_disable_instrumentation(); 1845 rcu_read_lock(); 1846 again_nocopy: 1847 dst_key = keys; 1848 dst_val = values; 1849 b = &htab->buckets[batch]; 1850 head = &b->head; 1851 /* do not grab the lock unless need it (bucket_cnt > 0). */ 1852 if (locked) { 1853 ret = htab_lock_bucket(b, &flags); 1854 if (ret) { 1855 rcu_read_unlock(); 1856 bpf_enable_instrumentation(); 1857 goto after_loop; 1858 } 1859 } 1860 1861 bucket_cnt = 0; 1862 hlist_nulls_for_each_entry_rcu(l, n, head, hash_node) 1863 bucket_cnt++; 1864 1865 if (bucket_cnt && !locked) { 1866 locked = true; 1867 goto again_nocopy; 1868 } 1869 1870 if (bucket_cnt > (max_count - total)) { 1871 if (total == 0) 1872 ret = -ENOSPC; 1873 /* Note that since bucket_cnt > 0 here, it is implicit 1874 * that the locked was grabbed, so release it. 1875 */ 1876 htab_unlock_bucket(b, flags); 1877 rcu_read_unlock(); 1878 bpf_enable_instrumentation(); 1879 goto after_loop; 1880 } 1881 1882 if (bucket_cnt > bucket_size) { 1883 bucket_size = bucket_cnt; 1884 /* Note that since bucket_cnt > 0 here, it is implicit 1885 * that the locked was grabbed, so release it. 1886 */ 1887 htab_unlock_bucket(b, flags); 1888 rcu_read_unlock(); 1889 bpf_enable_instrumentation(); 1890 kvfree(keys); 1891 kvfree(values); 1892 goto alloc; 1893 } 1894 1895 /* Next block is only safe to run if you have grabbed the lock */ 1896 if (!locked) 1897 goto next_batch; 1898 1899 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) { 1900 memcpy(dst_key, l->key, key_size); 1901 1902 if (is_percpu) { 1903 int off = 0, cpu; 1904 void __percpu *pptr; 1905 1906 pptr = htab_elem_get_ptr(l, map->key_size); 1907 if (elem_map_flags & BPF_F_CPU) { 1908 cpu = elem_map_flags >> 32; 1909 copy_map_value(&htab->map, dst_val, per_cpu_ptr(pptr, cpu)); 1910 check_and_init_map_value(&htab->map, dst_val); 1911 } else { 1912 for_each_possible_cpu(cpu) { 1913 copy_map_value_long(&htab->map, dst_val + off, 1914 per_cpu_ptr(pptr, cpu)); 1915 check_and_init_map_value(&htab->map, dst_val + off); 1916 off += size; 1917 } 1918 } 1919 } else { 1920 value = htab_elem_value(l, key_size); 1921 if (is_fd_htab(htab)) { 1922 struct bpf_map **inner_map = value; 1923 1924 /* Actual value is the id of the inner map */ 1925 map_id = map->ops->map_fd_sys_lookup_elem(*inner_map); 1926 value = &map_id; 1927 } 1928 1929 if (elem_map_flags & BPF_F_LOCK) 1930 copy_map_value_locked(map, dst_val, value, 1931 true); 1932 else 1933 copy_map_value(map, dst_val, value); 1934 /* Zeroing special fields in the temp buffer */ 1935 check_and_init_map_value(map, dst_val); 1936 } 1937 if (do_delete) { 1938 hlist_nulls_del_rcu(&l->hash_node); 1939 1940 /* bpf_lru_push_free() will acquire lru_lock, which 1941 * may cause deadlock. See comments in function 1942 * prealloc_lru_pop(). Let us do bpf_lru_push_free() 1943 * after releasing the bucket lock. 1944 * 1945 * For htab of maps, htab_put_fd_value() in 1946 * free_htab_elem() may acquire a spinlock with bucket 1947 * lock being held and it violates the lock rule, so 1948 * invoke free_htab_elem() after unlock as well. 1949 */ 1950 l->batch_flink = node_to_free; 1951 node_to_free = l; 1952 } 1953 dst_key += key_size; 1954 dst_val += value_size; 1955 } 1956 1957 htab_unlock_bucket(b, flags); 1958 locked = false; 1959 1960 while (node_to_free) { 1961 l = node_to_free; 1962 node_to_free = node_to_free->batch_flink; 1963 if (is_lru_map) 1964 htab_lru_push_free(htab, l); 1965 else 1966 free_htab_elem(htab, l); 1967 } 1968 1969 next_batch: 1970 /* If we are not copying data, we can go to next bucket and avoid 1971 * unlocking the rcu. 1972 */ 1973 if (!bucket_cnt && (batch + 1 < htab->n_buckets)) { 1974 batch++; 1975 goto again_nocopy; 1976 } 1977 1978 rcu_read_unlock(); 1979 bpf_enable_instrumentation(); 1980 if (bucket_cnt && (copy_to_user(ukeys + total * key_size, keys, 1981 key_size * bucket_cnt) || 1982 copy_to_user(uvalues + total * value_size, values, 1983 value_size * bucket_cnt))) { 1984 ret = -EFAULT; 1985 goto after_loop; 1986 } 1987 1988 total += bucket_cnt; 1989 batch++; 1990 if (batch >= htab->n_buckets) { 1991 ret = -ENOENT; 1992 goto after_loop; 1993 } 1994 goto again; 1995 1996 after_loop: 1997 if (ret == -EFAULT) 1998 goto out; 1999 2000 /* copy # of entries and next batch */ 2001 ubatch = u64_to_user_ptr(attr->batch.out_batch); 2002 if (copy_to_user(ubatch, &batch, sizeof(batch)) || 2003 put_user(total, &uattr->batch.count)) 2004 ret = -EFAULT; 2005 2006 out: 2007 kvfree(keys); 2008 kvfree(values); 2009 return ret; 2010 } 2011 2012 static int 2013 htab_percpu_map_lookup_batch(struct bpf_map *map, const union bpf_attr *attr, 2014 union bpf_attr __user *uattr) 2015 { 2016 return __htab_map_lookup_and_delete_batch(map, attr, uattr, false, 2017 false, true); 2018 } 2019 2020 static int 2021 htab_percpu_map_lookup_and_delete_batch(struct bpf_map *map, 2022 const union bpf_attr *attr, 2023 union bpf_attr __user *uattr) 2024 { 2025 return __htab_map_lookup_and_delete_batch(map, attr, uattr, true, 2026 false, true); 2027 } 2028 2029 static int 2030 htab_map_lookup_batch(struct bpf_map *map, const union bpf_attr *attr, 2031 union bpf_attr __user *uattr) 2032 { 2033 return __htab_map_lookup_and_delete_batch(map, attr, uattr, false, 2034 false, false); 2035 } 2036 2037 static int 2038 htab_map_lookup_and_delete_batch(struct bpf_map *map, 2039 const union bpf_attr *attr, 2040 union bpf_attr __user *uattr) 2041 { 2042 return __htab_map_lookup_and_delete_batch(map, attr, uattr, true, 2043 false, false); 2044 } 2045 2046 static int 2047 htab_lru_percpu_map_lookup_batch(struct bpf_map *map, 2048 const union bpf_attr *attr, 2049 union bpf_attr __user *uattr) 2050 { 2051 return __htab_map_lookup_and_delete_batch(map, attr, uattr, false, 2052 true, true); 2053 } 2054 2055 static int 2056 htab_lru_percpu_map_lookup_and_delete_batch(struct bpf_map *map, 2057 const union bpf_attr *attr, 2058 union bpf_attr __user *uattr) 2059 { 2060 return __htab_map_lookup_and_delete_batch(map, attr, uattr, true, 2061 true, true); 2062 } 2063 2064 static int 2065 htab_lru_map_lookup_batch(struct bpf_map *map, const union bpf_attr *attr, 2066 union bpf_attr __user *uattr) 2067 { 2068 return __htab_map_lookup_and_delete_batch(map, attr, uattr, false, 2069 true, false); 2070 } 2071 2072 static int 2073 htab_lru_map_lookup_and_delete_batch(struct bpf_map *map, 2074 const union bpf_attr *attr, 2075 union bpf_attr __user *uattr) 2076 { 2077 return __htab_map_lookup_and_delete_batch(map, attr, uattr, true, 2078 true, false); 2079 } 2080 2081 struct bpf_iter_seq_hash_map_info { 2082 struct bpf_map *map; 2083 struct bpf_htab *htab; 2084 void *percpu_value_buf; // non-zero means percpu hash 2085 u32 bucket_id; 2086 u32 skip_elems; 2087 }; 2088 2089 static struct htab_elem * 2090 bpf_hash_map_seq_find_next(struct bpf_iter_seq_hash_map_info *info, 2091 struct htab_elem *prev_elem) 2092 { 2093 const struct bpf_htab *htab = info->htab; 2094 u32 skip_elems = info->skip_elems; 2095 u32 bucket_id = info->bucket_id; 2096 struct hlist_nulls_head *head; 2097 struct hlist_nulls_node *n; 2098 struct htab_elem *elem; 2099 struct bucket *b; 2100 u32 i, count; 2101 2102 if (bucket_id >= htab->n_buckets) 2103 return NULL; 2104 2105 /* try to find next elem in the same bucket */ 2106 if (prev_elem) { 2107 /* no update/deletion on this bucket, prev_elem should be still valid 2108 * and we won't skip elements. 2109 */ 2110 n = rcu_dereference_raw(hlist_nulls_next_rcu(&prev_elem->hash_node)); 2111 elem = hlist_nulls_entry_safe(n, struct htab_elem, hash_node); 2112 if (elem) 2113 return elem; 2114 2115 /* not found, unlock and go to the next bucket */ 2116 b = &htab->buckets[bucket_id++]; 2117 rcu_read_unlock(); 2118 skip_elems = 0; 2119 } 2120 2121 for (i = bucket_id; i < htab->n_buckets; i++) { 2122 b = &htab->buckets[i]; 2123 rcu_read_lock(); 2124 2125 count = 0; 2126 head = &b->head; 2127 hlist_nulls_for_each_entry_rcu(elem, n, head, hash_node) { 2128 if (count >= skip_elems) { 2129 info->bucket_id = i; 2130 info->skip_elems = count; 2131 return elem; 2132 } 2133 count++; 2134 } 2135 2136 rcu_read_unlock(); 2137 skip_elems = 0; 2138 } 2139 2140 info->bucket_id = i; 2141 info->skip_elems = 0; 2142 return NULL; 2143 } 2144 2145 static void *bpf_hash_map_seq_start(struct seq_file *seq, loff_t *pos) 2146 { 2147 struct bpf_iter_seq_hash_map_info *info = seq->private; 2148 struct htab_elem *elem; 2149 2150 elem = bpf_hash_map_seq_find_next(info, NULL); 2151 if (!elem) 2152 return NULL; 2153 2154 if (*pos == 0) 2155 ++*pos; 2156 return elem; 2157 } 2158 2159 static void *bpf_hash_map_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2160 { 2161 struct bpf_iter_seq_hash_map_info *info = seq->private; 2162 2163 ++*pos; 2164 ++info->skip_elems; 2165 return bpf_hash_map_seq_find_next(info, v); 2166 } 2167 2168 static int __bpf_hash_map_seq_show(struct seq_file *seq, struct htab_elem *elem) 2169 { 2170 struct bpf_iter_seq_hash_map_info *info = seq->private; 2171 struct bpf_iter__bpf_map_elem ctx = {}; 2172 struct bpf_map *map = info->map; 2173 struct bpf_iter_meta meta; 2174 int ret = 0, off = 0, cpu; 2175 u32 roundup_value_size; 2176 struct bpf_prog *prog; 2177 void __percpu *pptr; 2178 2179 meta.seq = seq; 2180 prog = bpf_iter_get_info(&meta, elem == NULL); 2181 if (prog) { 2182 ctx.meta = &meta; 2183 ctx.map = info->map; 2184 if (elem) { 2185 ctx.key = elem->key; 2186 if (!info->percpu_value_buf) { 2187 ctx.value = htab_elem_value(elem, map->key_size); 2188 } else { 2189 roundup_value_size = round_up(map->value_size, 8); 2190 pptr = htab_elem_get_ptr(elem, map->key_size); 2191 for_each_possible_cpu(cpu) { 2192 copy_map_value_long(map, info->percpu_value_buf + off, 2193 per_cpu_ptr(pptr, cpu)); 2194 check_and_init_map_value(map, info->percpu_value_buf + off); 2195 off += roundup_value_size; 2196 } 2197 ctx.value = info->percpu_value_buf; 2198 } 2199 } 2200 ret = bpf_iter_run_prog(prog, &ctx); 2201 } 2202 2203 return ret; 2204 } 2205 2206 static int bpf_hash_map_seq_show(struct seq_file *seq, void *v) 2207 { 2208 return __bpf_hash_map_seq_show(seq, v); 2209 } 2210 2211 static void bpf_hash_map_seq_stop(struct seq_file *seq, void *v) 2212 { 2213 if (!v) 2214 (void)__bpf_hash_map_seq_show(seq, NULL); 2215 else 2216 rcu_read_unlock(); 2217 } 2218 2219 static int bpf_iter_init_hash_map(void *priv_data, 2220 struct bpf_iter_aux_info *aux) 2221 { 2222 struct bpf_iter_seq_hash_map_info *seq_info = priv_data; 2223 struct bpf_map *map = aux->map; 2224 void *value_buf; 2225 u32 buf_size; 2226 2227 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || 2228 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { 2229 buf_size = round_up(map->value_size, 8) * num_possible_cpus(); 2230 value_buf = kmalloc(buf_size, GFP_USER | __GFP_NOWARN); 2231 if (!value_buf) 2232 return -ENOMEM; 2233 2234 seq_info->percpu_value_buf = value_buf; 2235 } 2236 2237 bpf_map_inc_with_uref(map); 2238 seq_info->map = map; 2239 seq_info->htab = container_of(map, struct bpf_htab, map); 2240 return 0; 2241 } 2242 2243 static void bpf_iter_fini_hash_map(void *priv_data) 2244 { 2245 struct bpf_iter_seq_hash_map_info *seq_info = priv_data; 2246 2247 bpf_map_put_with_uref(seq_info->map); 2248 kfree(seq_info->percpu_value_buf); 2249 } 2250 2251 static const struct seq_operations bpf_hash_map_seq_ops = { 2252 .start = bpf_hash_map_seq_start, 2253 .next = bpf_hash_map_seq_next, 2254 .stop = bpf_hash_map_seq_stop, 2255 .show = bpf_hash_map_seq_show, 2256 }; 2257 2258 static const struct bpf_iter_seq_info iter_seq_info = { 2259 .seq_ops = &bpf_hash_map_seq_ops, 2260 .init_seq_private = bpf_iter_init_hash_map, 2261 .fini_seq_private = bpf_iter_fini_hash_map, 2262 .seq_priv_size = sizeof(struct bpf_iter_seq_hash_map_info), 2263 }; 2264 2265 static long bpf_for_each_hash_elem(struct bpf_map *map, bpf_callback_t callback_fn, 2266 void *callback_ctx, u64 flags) 2267 { 2268 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 2269 struct hlist_nulls_head *head; 2270 struct hlist_nulls_node *n; 2271 struct htab_elem *elem; 2272 int i, num_elems = 0; 2273 void __percpu *pptr; 2274 struct bucket *b; 2275 void *key, *val; 2276 bool is_percpu; 2277 u64 ret = 0; 2278 2279 cant_migrate(); 2280 2281 if (flags != 0) 2282 return -EINVAL; 2283 2284 is_percpu = htab_is_percpu(htab); 2285 2286 /* migration has been disabled, so percpu value prepared here will be 2287 * the same as the one seen by the bpf program with 2288 * bpf_map_lookup_elem(). 2289 */ 2290 for (i = 0; i < htab->n_buckets; i++) { 2291 b = &htab->buckets[i]; 2292 rcu_read_lock(); 2293 head = &b->head; 2294 hlist_nulls_for_each_entry_safe(elem, n, head, hash_node) { 2295 key = elem->key; 2296 if (is_percpu) { 2297 /* current cpu value for percpu map */ 2298 pptr = htab_elem_get_ptr(elem, map->key_size); 2299 val = this_cpu_ptr(pptr); 2300 } else { 2301 val = htab_elem_value(elem, map->key_size); 2302 } 2303 num_elems++; 2304 ret = callback_fn((u64)(long)map, (u64)(long)key, 2305 (u64)(long)val, (u64)(long)callback_ctx, 0); 2306 /* return value: 0 - continue, 1 - stop and return */ 2307 if (ret) { 2308 rcu_read_unlock(); 2309 goto out; 2310 } 2311 } 2312 rcu_read_unlock(); 2313 } 2314 out: 2315 return num_elems; 2316 } 2317 2318 static u64 htab_map_mem_usage(const struct bpf_map *map) 2319 { 2320 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 2321 u32 value_size = round_up(htab->map.value_size, 8); 2322 bool prealloc = htab_is_prealloc(htab); 2323 bool percpu = htab_is_percpu(htab); 2324 bool lru = htab_is_lru(htab); 2325 u64 num_entries, usage; 2326 2327 usage = sizeof(struct bpf_htab) + 2328 sizeof(struct bucket) * htab->n_buckets; 2329 2330 if (prealloc) { 2331 num_entries = map->max_entries; 2332 if (htab_has_extra_elems(htab)) 2333 num_entries += num_possible_cpus(); 2334 2335 usage += htab->elem_size * num_entries; 2336 2337 if (percpu) 2338 usage += value_size * num_possible_cpus() * num_entries; 2339 else if (!lru) 2340 usage += sizeof(struct htab_elem *) * num_possible_cpus(); 2341 } else { 2342 #define LLIST_NODE_SZ sizeof(struct llist_node) 2343 2344 num_entries = htab->use_percpu_counter ? 2345 percpu_counter_sum(&htab->pcount) : 2346 atomic_read(&htab->count); 2347 usage += (htab->elem_size + LLIST_NODE_SZ) * num_entries; 2348 if (percpu) { 2349 usage += (LLIST_NODE_SZ + sizeof(void *)) * num_entries; 2350 usage += value_size * num_possible_cpus() * num_entries; 2351 } 2352 } 2353 return usage; 2354 } 2355 2356 BTF_ID_LIST_SINGLE(htab_map_btf_ids, struct, bpf_htab) 2357 const struct bpf_map_ops htab_map_ops = { 2358 .map_meta_equal = bpf_map_meta_equal, 2359 .map_alloc_check = htab_map_alloc_check, 2360 .map_alloc = htab_map_alloc, 2361 .map_free = htab_map_free, 2362 .map_get_next_key = htab_map_get_next_key, 2363 .map_release_uref = htab_map_free_internal_structs, 2364 .map_lookup_elem = htab_map_lookup_elem, 2365 .map_lookup_and_delete_elem = htab_map_lookup_and_delete_elem, 2366 .map_update_elem = htab_map_update_elem, 2367 .map_delete_elem = htab_map_delete_elem, 2368 .map_gen_lookup = htab_map_gen_lookup, 2369 .map_seq_show_elem = htab_map_seq_show_elem, 2370 .map_set_for_each_callback_args = map_set_for_each_callback_args, 2371 .map_for_each_callback = bpf_for_each_hash_elem, 2372 .map_check_btf = htab_map_check_btf, 2373 .map_mem_usage = htab_map_mem_usage, 2374 BATCH_OPS(htab), 2375 .map_btf_id = &htab_map_btf_ids[0], 2376 .iter_seq_info = &iter_seq_info, 2377 }; 2378 2379 const struct bpf_map_ops htab_lru_map_ops = { 2380 .map_meta_equal = bpf_map_meta_equal, 2381 .map_alloc_check = htab_map_alloc_check, 2382 .map_alloc = htab_map_alloc, 2383 .map_free = htab_map_free, 2384 .map_get_next_key = htab_map_get_next_key, 2385 .map_release_uref = htab_map_free_internal_structs, 2386 .map_lookup_elem = htab_lru_map_lookup_elem, 2387 .map_lookup_and_delete_elem = htab_lru_map_lookup_and_delete_elem, 2388 .map_lookup_elem_sys_only = htab_lru_map_lookup_elem_sys, 2389 .map_update_elem = htab_lru_map_update_elem, 2390 .map_delete_elem = htab_lru_map_delete_elem, 2391 .map_gen_lookup = htab_lru_map_gen_lookup, 2392 .map_seq_show_elem = htab_map_seq_show_elem, 2393 .map_set_for_each_callback_args = map_set_for_each_callback_args, 2394 .map_for_each_callback = bpf_for_each_hash_elem, 2395 .map_check_btf = htab_map_check_btf, 2396 .map_mem_usage = htab_map_mem_usage, 2397 BATCH_OPS(htab_lru), 2398 .map_btf_id = &htab_map_btf_ids[0], 2399 .iter_seq_info = &iter_seq_info, 2400 }; 2401 2402 /* Called from eBPF program */ 2403 static void *htab_percpu_map_lookup_elem(struct bpf_map *map, void *key) 2404 { 2405 struct htab_elem *l = __htab_map_lookup_elem(map, key); 2406 2407 if (l) 2408 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size)); 2409 else 2410 return NULL; 2411 } 2412 2413 /* inline bpf_map_lookup_elem() call for per-CPU hashmap */ 2414 static int htab_percpu_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf) 2415 { 2416 struct bpf_insn *insn = insn_buf; 2417 2418 if (!bpf_jit_supports_percpu_insn()) 2419 return -EOPNOTSUPP; 2420 2421 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem, 2422 (void *(*)(struct bpf_map *map, void *key))NULL)); 2423 *insn++ = BPF_EMIT_CALL(__htab_map_lookup_elem); 2424 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3); 2425 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 2426 offsetof(struct htab_elem, key) + roundup(map->key_size, 8)); 2427 *insn++ = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0); 2428 *insn++ = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0); 2429 2430 return insn - insn_buf; 2431 } 2432 2433 static void *htab_percpu_map_lookup_percpu_elem(struct bpf_map *map, void *key, u32 cpu) 2434 { 2435 struct htab_elem *l; 2436 2437 if (cpu >= nr_cpu_ids) 2438 return NULL; 2439 2440 l = __htab_map_lookup_elem(map, key); 2441 if (l) 2442 return per_cpu_ptr(htab_elem_get_ptr(l, map->key_size), cpu); 2443 else 2444 return NULL; 2445 } 2446 2447 static void *htab_lru_percpu_map_lookup_elem(struct bpf_map *map, void *key) 2448 { 2449 struct htab_elem *l = __htab_map_lookup_elem(map, key); 2450 2451 if (l) { 2452 bpf_lru_node_set_ref(&l->lru_node); 2453 return this_cpu_ptr(htab_elem_get_ptr(l, map->key_size)); 2454 } 2455 2456 return NULL; 2457 } 2458 2459 static void *htab_lru_percpu_map_lookup_percpu_elem(struct bpf_map *map, void *key, u32 cpu) 2460 { 2461 struct htab_elem *l; 2462 2463 if (cpu >= nr_cpu_ids) 2464 return NULL; 2465 2466 l = __htab_map_lookup_elem(map, key); 2467 if (l) { 2468 bpf_lru_node_set_ref(&l->lru_node); 2469 return per_cpu_ptr(htab_elem_get_ptr(l, map->key_size), cpu); 2470 } 2471 2472 return NULL; 2473 } 2474 2475 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value, u64 map_flags) 2476 { 2477 struct htab_elem *l; 2478 void __percpu *pptr; 2479 int ret = -ENOENT; 2480 int cpu, off = 0; 2481 u32 size; 2482 2483 /* per_cpu areas are zero-filled and bpf programs can only 2484 * access 'value_size' of them, so copying rounded areas 2485 * will not leak any kernel data 2486 */ 2487 size = round_up(map->value_size, 8); 2488 rcu_read_lock(); 2489 l = __htab_map_lookup_elem(map, key); 2490 if (!l) 2491 goto out; 2492 ret = 0; 2493 /* We do not mark LRU map element here in order to not mess up 2494 * eviction heuristics when user space does a map walk. 2495 */ 2496 pptr = htab_elem_get_ptr(l, map->key_size); 2497 if (map_flags & BPF_F_CPU) { 2498 cpu = map_flags >> 32; 2499 copy_map_value(map, value, per_cpu_ptr(pptr, cpu)); 2500 check_and_init_map_value(map, value); 2501 goto out; 2502 } 2503 for_each_possible_cpu(cpu) { 2504 copy_map_value_long(map, value + off, per_cpu_ptr(pptr, cpu)); 2505 check_and_init_map_value(map, value + off); 2506 off += size; 2507 } 2508 out: 2509 rcu_read_unlock(); 2510 return ret; 2511 } 2512 2513 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value, 2514 u64 map_flags) 2515 { 2516 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 2517 int ret; 2518 2519 rcu_read_lock(); 2520 if (htab_is_lru(htab)) 2521 ret = __htab_lru_percpu_map_update_elem(map, key, value, 2522 map_flags, true); 2523 else 2524 ret = htab_map_update_elem_in_place(map, key, value, map_flags, 2525 true, true); 2526 rcu_read_unlock(); 2527 2528 return ret; 2529 } 2530 2531 static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key, 2532 struct seq_file *m) 2533 { 2534 struct htab_elem *l; 2535 void __percpu *pptr; 2536 int cpu; 2537 2538 rcu_read_lock(); 2539 2540 l = __htab_map_lookup_elem(map, key); 2541 if (!l) { 2542 rcu_read_unlock(); 2543 return; 2544 } 2545 2546 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m); 2547 seq_puts(m, ": {\n"); 2548 pptr = htab_elem_get_ptr(l, map->key_size); 2549 for_each_possible_cpu(cpu) { 2550 seq_printf(m, "\tcpu%d: ", cpu); 2551 btf_type_seq_show(map->btf, map->btf_value_type_id, 2552 per_cpu_ptr(pptr, cpu), m); 2553 seq_putc(m, '\n'); 2554 } 2555 seq_puts(m, "}\n"); 2556 2557 rcu_read_unlock(); 2558 } 2559 2560 const struct bpf_map_ops htab_percpu_map_ops = { 2561 .map_meta_equal = bpf_map_meta_equal, 2562 .map_alloc_check = htab_map_alloc_check, 2563 .map_alloc = htab_map_alloc, 2564 .map_free = htab_map_free, 2565 .map_get_next_key = htab_map_get_next_key, 2566 .map_lookup_elem = htab_percpu_map_lookup_elem, 2567 .map_gen_lookup = htab_percpu_map_gen_lookup, 2568 .map_lookup_and_delete_elem = htab_percpu_map_lookup_and_delete_elem, 2569 .map_update_elem = htab_percpu_map_update_elem, 2570 .map_delete_elem = htab_map_delete_elem, 2571 .map_lookup_percpu_elem = htab_percpu_map_lookup_percpu_elem, 2572 .map_seq_show_elem = htab_percpu_map_seq_show_elem, 2573 .map_set_for_each_callback_args = map_set_for_each_callback_args, 2574 .map_for_each_callback = bpf_for_each_hash_elem, 2575 .map_check_btf = htab_map_check_btf, 2576 .map_mem_usage = htab_map_mem_usage, 2577 BATCH_OPS(htab_percpu), 2578 .map_btf_id = &htab_map_btf_ids[0], 2579 .iter_seq_info = &iter_seq_info, 2580 }; 2581 2582 const struct bpf_map_ops htab_lru_percpu_map_ops = { 2583 .map_meta_equal = bpf_map_meta_equal, 2584 .map_alloc_check = htab_map_alloc_check, 2585 .map_alloc = htab_map_alloc, 2586 .map_free = htab_map_free, 2587 .map_get_next_key = htab_map_get_next_key, 2588 .map_lookup_elem = htab_lru_percpu_map_lookup_elem, 2589 .map_lookup_and_delete_elem = htab_lru_percpu_map_lookup_and_delete_elem, 2590 .map_update_elem = htab_lru_percpu_map_update_elem, 2591 .map_delete_elem = htab_lru_map_delete_elem, 2592 .map_lookup_percpu_elem = htab_lru_percpu_map_lookup_percpu_elem, 2593 .map_seq_show_elem = htab_percpu_map_seq_show_elem, 2594 .map_set_for_each_callback_args = map_set_for_each_callback_args, 2595 .map_for_each_callback = bpf_for_each_hash_elem, 2596 .map_check_btf = htab_map_check_btf, 2597 .map_mem_usage = htab_map_mem_usage, 2598 BATCH_OPS(htab_lru_percpu), 2599 .map_btf_id = &htab_map_btf_ids[0], 2600 .iter_seq_info = &iter_seq_info, 2601 }; 2602 2603 static int fd_htab_map_alloc_check(union bpf_attr *attr) 2604 { 2605 if (attr->value_size != sizeof(u32)) 2606 return -EINVAL; 2607 return htab_map_alloc_check(attr); 2608 } 2609 2610 static void fd_htab_map_free(struct bpf_map *map) 2611 { 2612 struct bpf_htab *htab = container_of(map, struct bpf_htab, map); 2613 struct hlist_nulls_node *n; 2614 struct hlist_nulls_head *head; 2615 struct htab_elem *l; 2616 int i; 2617 2618 for (i = 0; i < htab->n_buckets; i++) { 2619 head = select_bucket(htab, i); 2620 2621 hlist_nulls_for_each_entry_safe(l, n, head, hash_node) { 2622 void *ptr = fd_htab_map_get_ptr(map, l); 2623 2624 map->ops->map_fd_put_ptr(map, ptr, false); 2625 } 2626 } 2627 2628 htab_map_free(map); 2629 } 2630 2631 /* only called from syscall */ 2632 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value) 2633 { 2634 void **ptr; 2635 int ret = 0; 2636 2637 if (!map->ops->map_fd_sys_lookup_elem) 2638 return -ENOTSUPP; 2639 2640 rcu_read_lock(); 2641 ptr = htab_map_lookup_elem(map, key); 2642 if (ptr) 2643 *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr)); 2644 else 2645 ret = -ENOENT; 2646 rcu_read_unlock(); 2647 2648 return ret; 2649 } 2650 2651 /* Only called from syscall */ 2652 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file, 2653 void *key, void *value, u64 map_flags) 2654 { 2655 void *ptr; 2656 int ret; 2657 2658 ptr = map->ops->map_fd_get_ptr(map, map_file, *(int *)value); 2659 if (IS_ERR(ptr)) 2660 return PTR_ERR(ptr); 2661 2662 /* The htab bucket lock is always held during update operations in fd 2663 * htab map, and the following rcu_read_lock() is only used to avoid 2664 * the WARN_ON_ONCE in htab_map_update_elem_in_place(). 2665 */ 2666 rcu_read_lock(); 2667 ret = htab_map_update_elem_in_place(map, key, &ptr, map_flags, false, false); 2668 rcu_read_unlock(); 2669 if (ret) 2670 map->ops->map_fd_put_ptr(map, ptr, false); 2671 2672 return ret; 2673 } 2674 2675 static struct bpf_map *htab_of_map_alloc(union bpf_attr *attr) 2676 { 2677 struct bpf_map *map, *inner_map_meta; 2678 2679 inner_map_meta = bpf_map_meta_alloc(attr->inner_map_fd); 2680 if (IS_ERR(inner_map_meta)) 2681 return inner_map_meta; 2682 2683 map = htab_map_alloc(attr); 2684 if (IS_ERR(map)) { 2685 bpf_map_meta_free(inner_map_meta); 2686 return map; 2687 } 2688 2689 map->inner_map_meta = inner_map_meta; 2690 2691 return map; 2692 } 2693 2694 static void *htab_of_map_lookup_elem(struct bpf_map *map, void *key) 2695 { 2696 struct bpf_map **inner_map = htab_map_lookup_elem(map, key); 2697 2698 if (!inner_map) 2699 return NULL; 2700 2701 return READ_ONCE(*inner_map); 2702 } 2703 2704 static int htab_of_map_gen_lookup(struct bpf_map *map, 2705 struct bpf_insn *insn_buf) 2706 { 2707 struct bpf_insn *insn = insn_buf; 2708 const int ret = BPF_REG_0; 2709 2710 BUILD_BUG_ON(!__same_type(&__htab_map_lookup_elem, 2711 (void *(*)(struct bpf_map *map, void *key))NULL)); 2712 *insn++ = BPF_EMIT_CALL(__htab_map_lookup_elem); 2713 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 2); 2714 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret, 2715 offsetof(struct htab_elem, key) + 2716 round_up(map->key_size, 8)); 2717 *insn++ = BPF_LDX_MEM(BPF_DW, ret, ret, 0); 2718 2719 return insn - insn_buf; 2720 } 2721 2722 static void htab_of_map_free(struct bpf_map *map) 2723 { 2724 bpf_map_meta_free(map->inner_map_meta); 2725 fd_htab_map_free(map); 2726 } 2727 2728 const struct bpf_map_ops htab_of_maps_map_ops = { 2729 .map_alloc_check = fd_htab_map_alloc_check, 2730 .map_alloc = htab_of_map_alloc, 2731 .map_free = htab_of_map_free, 2732 .map_get_next_key = htab_map_get_next_key, 2733 .map_lookup_elem = htab_of_map_lookup_elem, 2734 .map_delete_elem = htab_map_delete_elem, 2735 .map_fd_get_ptr = bpf_map_fd_get_ptr, 2736 .map_fd_put_ptr = bpf_map_fd_put_ptr, 2737 .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem, 2738 .map_gen_lookup = htab_of_map_gen_lookup, 2739 .map_check_btf = map_check_no_btf, 2740 .map_mem_usage = htab_map_mem_usage, 2741 BATCH_OPS(htab), 2742 .map_btf_id = &htab_map_btf_ids[0], 2743 }; 2744 2745 struct rhtab_elem { 2746 struct rhash_head node; 2747 /* key bytes, then value bytes follow */ 2748 u8 data[] __aligned(8); 2749 }; 2750 2751 struct bpf_rhtab { 2752 struct bpf_map map; 2753 struct rhashtable ht; 2754 struct bpf_mem_alloc ma; 2755 u32 elem_size; 2756 bool freeing_internal; 2757 }; 2758 2759 static const struct rhashtable_params rhtab_params = { 2760 .head_offset = offsetof(struct rhtab_elem, node), 2761 .key_offset = offsetof(struct rhtab_elem, data), 2762 }; 2763 2764 static inline void *rhtab_elem_value(struct rhtab_elem *l, u32 key_size) 2765 { 2766 return l->data + round_up(key_size, 8); 2767 } 2768 2769 /* Specialize hash function and objcmp for long sized key */ 2770 static __always_inline int rhtab_key_cmp_long(struct rhashtable_compare_arg *arg, 2771 const void *ptr) 2772 { 2773 const unsigned long key1 = *(const unsigned long *)arg->key; 2774 const struct rhtab_elem *key2 = ptr; 2775 2776 return key1 != *(const unsigned long *)key2->data; 2777 } 2778 2779 static __always_inline u32 rhtab_hashfn_long(const void *data, u32 len, u32 seed) 2780 { 2781 u64 k = *(const unsigned long *)data; 2782 2783 return (u32)(k ^ (k >> 32)) ^ seed; 2784 } 2785 2786 static const struct rhashtable_params rhtab_params_long = { 2787 .head_offset = offsetof(struct rhtab_elem, node), 2788 .key_offset = offsetof(struct rhtab_elem, data), 2789 .key_len = sizeof(long), 2790 .hashfn = rhtab_hashfn_long, 2791 .obj_cmpfn = rhtab_key_cmp_long, 2792 }; 2793 2794 static struct bpf_map *rhtab_map_alloc(union bpf_attr *attr) 2795 { 2796 struct rhashtable_params params; 2797 struct bpf_rhtab *rhtab; 2798 int err = 0; 2799 2800 rhtab = bpf_map_area_alloc(sizeof(*rhtab), NUMA_NO_NODE); 2801 if (!rhtab) 2802 return ERR_PTR(-ENOMEM); 2803 2804 bpf_map_init_from_attr(&rhtab->map, attr); 2805 2806 if (rhtab->map.max_entries > 1UL << 31) { 2807 err = -E2BIG; 2808 goto free_rhtab; 2809 } 2810 2811 rhtab->elem_size = sizeof(struct rhtab_elem) + round_up(rhtab->map.key_size, 8) + 2812 round_up(rhtab->map.value_size, 8); 2813 2814 params = rhtab_params; 2815 params.key_len = rhtab->map.key_size; 2816 params.nelem_hint = (u32)attr->map_extra; 2817 params.automatic_shrinking = true; 2818 2819 if (rhtab->map.key_size == sizeof(long)) { 2820 params.hashfn = rhtab_hashfn_long; 2821 params.obj_cmpfn = rhtab_key_cmp_long; 2822 } 2823 2824 err = rhashtable_init(&rhtab->ht, ¶ms); 2825 if (err) 2826 goto free_rhtab; 2827 2828 /* Set max_elems after rhashtable_init() since init zeroes the struct */ 2829 rhtab->ht.max_elems = rhtab->map.max_entries; 2830 2831 err = bpf_mem_alloc_init(&rhtab->ma, rhtab->elem_size, false); 2832 if (err) 2833 goto destroy_rhtab; 2834 2835 return &rhtab->map; 2836 2837 destroy_rhtab: 2838 rhashtable_destroy(&rhtab->ht); 2839 free_rhtab: 2840 bpf_map_area_free(rhtab); 2841 return ERR_PTR(err); 2842 } 2843 2844 static int rhtab_map_alloc_check(union bpf_attr *attr) 2845 { 2846 if (!(attr->map_flags & BPF_F_NO_PREALLOC)) 2847 return -EINVAL; 2848 2849 if (attr->map_flags & BPF_F_ZERO_SEED) 2850 return -EINVAL; 2851 2852 if (attr->key_size > U16_MAX) 2853 return -E2BIG; 2854 2855 if (attr->map_extra >> 32) 2856 return -EINVAL; 2857 2858 if ((u32)attr->map_extra > U16_MAX) 2859 return -E2BIG; 2860 2861 if ((u32)attr->map_extra > attr->max_entries) 2862 return -EINVAL; 2863 2864 return htab_map_alloc_check(attr); 2865 } 2866 2867 static void rhtab_check_and_free_fields(struct bpf_rhtab *rhtab, 2868 struct rhtab_elem *elem) 2869 { 2870 if (IS_ERR_OR_NULL(rhtab->map.record)) 2871 return; 2872 2873 bpf_obj_free_fields(rhtab->map.record, 2874 rhtab_elem_value(elem, rhtab->map.key_size)); 2875 } 2876 2877 static void rhtab_mem_dtor(void *obj, void *ctx) 2878 { 2879 struct htab_btf_record *hrec = ctx; 2880 struct rhtab_elem *elem = obj; 2881 2882 if (IS_ERR_OR_NULL(hrec->record)) 2883 return; 2884 2885 bpf_obj_free_fields(hrec->record, 2886 rhtab_elem_value(elem, hrec->key_size)); 2887 } 2888 2889 static void rhtab_free_elem(void *ptr, void *arg) 2890 { 2891 struct bpf_rhtab *rhtab = arg; 2892 struct rhtab_elem *elem = ptr; 2893 2894 bpf_map_free_internal_structs(&rhtab->map, rhtab_elem_value(elem, rhtab->map.key_size)); 2895 bpf_mem_cache_free_rcu(&rhtab->ma, elem); 2896 } 2897 2898 static void rhtab_map_free(struct bpf_map *map) 2899 { 2900 struct bpf_rhtab *rhtab = container_of(map, struct bpf_rhtab, map); 2901 2902 rhashtable_free_and_destroy(&rhtab->ht, rhtab_free_elem, rhtab); 2903 bpf_mem_alloc_destroy(&rhtab->ma); 2904 bpf_map_area_free(rhtab); 2905 } 2906 2907 static void *rhtab_lookup_elem(struct bpf_map *map, void *key) 2908 { 2909 struct bpf_rhtab *rhtab = container_of(map, struct bpf_rhtab, map); 2910 2911 /* Hold RCU lock in case sleepable program calls via gen_lookup */ 2912 guard(rcu)(); 2913 2914 if (map->key_size == sizeof(long)) 2915 return rhashtable_lookup_likely(&rhtab->ht, key, rhtab_params_long); 2916 2917 return rhashtable_lookup_likely(&rhtab->ht, key, rhtab_params); 2918 } 2919 2920 static void *rhtab_map_lookup_elem(struct bpf_map *map, void *key) __must_hold(RCU) 2921 { 2922 struct rhtab_elem *l; 2923 2924 l = rhtab_lookup_elem(map, key); 2925 return l ? rhtab_elem_value(l, map->key_size) : NULL; 2926 } 2927 2928 static void rhtab_read_elem_value(struct bpf_map *map, void *dst, struct rhtab_elem *elem, 2929 u64 flags) 2930 { 2931 void *src = rhtab_elem_value(elem, map->key_size); 2932 2933 if (flags & BPF_F_LOCK) 2934 copy_map_value_locked(map, dst, src, true); 2935 else 2936 copy_map_value(map, dst, src); 2937 } 2938 2939 static int rhtab_delete_elem(struct bpf_rhtab *rhtab, struct rhtab_elem *elem, void *copy, 2940 u64 flags) 2941 { 2942 int err; 2943 2944 /* 2945 * disable_instrumentation() mitigates the deadlock for programs running in NMI context. 2946 * rhashtable locks bucket with local_irq_save(). Only NMI programs may reenter 2947 * rhashtable code, bpf_disable_instrumentation() disables programs running in NMI, except 2948 * raw tracepoints, which we don't have in rhashtable. 2949 */ 2950 bpf_disable_instrumentation(); 2951 2952 if (rhtab->map.key_size == sizeof(long)) 2953 err = rhashtable_remove_fast(&rhtab->ht, &elem->node, rhtab_params_long); 2954 else 2955 err = rhashtable_remove_fast(&rhtab->ht, &elem->node, rhtab_params); 2956 2957 bpf_enable_instrumentation(); 2958 2959 if (err) 2960 return err; 2961 2962 if (copy) { 2963 rhtab_read_elem_value(&rhtab->map, copy, elem, flags); 2964 check_and_init_map_value(&rhtab->map, copy); 2965 } 2966 /* Release internal structs: kptr, bpf_timer, task_work, wq */ 2967 rhtab_check_and_free_fields(rhtab, elem); 2968 bpf_mem_cache_free_rcu(&rhtab->ma, elem); 2969 return 0; 2970 } 2971 2972 static long rhtab_map_delete_elem(struct bpf_map *map, void *key) 2973 { 2974 struct bpf_rhtab *rhtab = container_of(map, struct bpf_rhtab, map); 2975 struct rhtab_elem *elem; 2976 2977 guard(rcu)(); 2978 2979 elem = rhtab_lookup_elem(map, key); 2980 if (!elem) 2981 return -ENOENT; 2982 2983 return rhtab_delete_elem(rhtab, elem, NULL, 0); 2984 } 2985 2986 static int rhtab_map_lookup_and_delete_elem(struct bpf_map *map, void *key, void *value, u64 flags) 2987 { 2988 struct bpf_rhtab *rhtab = container_of(map, struct bpf_rhtab, map); 2989 struct rhtab_elem *elem; 2990 int err; 2991 2992 err = bpf_map_check_op_flags(map, flags, BPF_F_LOCK); 2993 if (err) 2994 return err; 2995 2996 guard(rcu)(); 2997 2998 elem = rhtab_lookup_elem(map, key); 2999 if (!elem) 3000 return -ENOENT; 3001 3002 return rhtab_delete_elem(rhtab, elem, value, flags); 3003 } 3004 3005 static long rhtab_map_update_existing(struct bpf_map *map, struct rhtab_elem *elem, void *value, 3006 u64 map_flags) 3007 { 3008 struct bpf_rhtab *rhtab = container_of(map, struct bpf_rhtab, map); 3009 void *old_val = rhtab_elem_value(elem, map->key_size); 3010 3011 if (map_flags & BPF_NOEXIST) 3012 return -EEXIST; 3013 3014 if (map_flags & BPF_F_LOCK) 3015 copy_map_value_locked(map, old_val, value, false); 3016 else 3017 copy_map_value(map, old_val, value); 3018 3019 /* 3020 * Torn reads: a concurrent reader without BPF_F_LOCK may observe 3021 * the value mid-copy. Callers requiring consistent reads must use 3022 * BPF_F_LOCK, matching arraymap semantics. 3023 * 3024 * copy_map_value() skips special-field offsets, so old timers/ 3025 * kptrs/etc. still sit in the slot. Cancel them after the copy 3026 * to match arraymap's update semantics. 3027 */ 3028 rhtab_check_and_free_fields(rhtab, elem); 3029 return 0; 3030 } 3031 3032 static long rhtab_map_update_elem(struct bpf_map *map, void *key, void *value, u64 map_flags) 3033 { 3034 struct bpf_rhtab *rhtab = container_of(map, struct bpf_rhtab, map); 3035 struct rhtab_elem *elem, *tmp; 3036 3037 if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST)) 3038 return -EINVAL; 3039 3040 if ((map_flags & BPF_F_LOCK) && !btf_record_has_field(map->record, BPF_SPIN_LOCK)) 3041 return -EINVAL; 3042 3043 guard(rcu)(); 3044 elem = rhtab_lookup_elem(map, key); 3045 if (elem) 3046 return rhtab_map_update_existing(map, elem, value, map_flags); 3047 3048 if (map_flags & BPF_EXIST) 3049 return -ENOENT; 3050 3051 /* 3052 * Reject new insertions while map_release_uref cleanup walks the 3053 * table. Without this, new elements could keep triggering rehash 3054 * and prevent the walk from terminating. 3055 */ 3056 if (READ_ONCE(rhtab->freeing_internal)) 3057 return -EBUSY; 3058 3059 /* Check max_entries limit before inserting new element */ 3060 if (atomic_read(&rhtab->ht.nelems) >= map->max_entries) 3061 return -E2BIG; 3062 3063 elem = bpf_mem_cache_alloc(&rhtab->ma); 3064 if (!elem) 3065 return -ENOMEM; 3066 3067 memcpy(elem->data, key, map->key_size); 3068 copy_map_value(map, rhtab_elem_value(elem, map->key_size), value); 3069 check_and_init_map_value(map, rhtab_elem_value(elem, map->key_size)); 3070 3071 /* Prevent deadlock for NMI programs attempting to take bucket lock */ 3072 bpf_disable_instrumentation(); 3073 3074 if (map->key_size == sizeof(long)) 3075 tmp = rhashtable_lookup_get_insert_fast(&rhtab->ht, &elem->node, rhtab_params_long); 3076 else 3077 tmp = rhashtable_lookup_get_insert_fast(&rhtab->ht, &elem->node, rhtab_params); 3078 3079 bpf_enable_instrumentation(); 3080 3081 if (tmp) { 3082 bpf_mem_cache_free(&rhtab->ma, elem); 3083 if (IS_ERR(tmp)) 3084 return PTR_ERR(tmp); 3085 3086 return rhtab_map_update_existing(map, tmp, value, map_flags); 3087 } 3088 3089 return 0; 3090 } 3091 3092 static int rhtab_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf) 3093 { 3094 struct bpf_insn *insn = insn_buf; 3095 const int ret = BPF_REG_0; 3096 3097 BUILD_BUG_ON(!__same_type(&rhtab_lookup_elem, 3098 (void *(*)(struct bpf_map *map, void *key)) NULL)); 3099 *insn++ = BPF_EMIT_CALL(rhtab_lookup_elem); 3100 *insn++ = BPF_JMP_IMM(BPF_JEQ, ret, 0, 1); 3101 *insn++ = BPF_ALU64_IMM(BPF_ADD, ret, 3102 offsetof(struct rhtab_elem, data) + round_up(map->key_size, 8)); 3103 3104 return insn - insn_buf; 3105 } 3106 3107 static int rhtab_map_check_btf(struct bpf_map *map, const struct btf *btf, 3108 const struct btf_type *key_type, 3109 const struct btf_type *value_type) 3110 { 3111 struct bpf_rhtab *rhtab = container_of(map, struct bpf_rhtab, map); 3112 3113 return bpf_ma_set_dtor(map, &rhtab->ma, rhtab_mem_dtor); 3114 } 3115 3116 static void rhtab_map_free_internal_structs(struct bpf_map *map) 3117 { 3118 struct bpf_rhtab *rhtab = container_of(map, struct bpf_rhtab, map); 3119 struct rhashtable_iter iter; 3120 struct rhtab_elem *elem; 3121 3122 if (!bpf_map_has_internal_structs(map)) 3123 return; 3124 3125 /* 3126 * Block new insertions. Once observed, no new growth is triggered, 3127 * so any in-flight rehash will drain and the walker is guaranteed 3128 * to stop returning -EAGAIN. Treat -EAGAIN as "rehash in progress, 3129 * retry"; do not wait for the worker. 3130 */ 3131 WRITE_ONCE(rhtab->freeing_internal, true); 3132 3133 rhashtable_walk_enter(&rhtab->ht, &iter); 3134 rhashtable_walk_start(&iter); 3135 3136 while ((elem = rhashtable_walk_next(&iter))) { 3137 if (IS_ERR(elem)) { 3138 if (PTR_ERR(elem) == -EAGAIN) 3139 continue; 3140 break; 3141 } 3142 3143 bpf_map_free_internal_structs(map, rhtab_elem_value(elem, map->key_size)); 3144 3145 if (need_resched()) { /* Avoid stalls on large maps */ 3146 rhashtable_walk_stop(&iter); 3147 cond_resched(); 3148 rhashtable_walk_start(&iter); 3149 } 3150 } 3151 3152 rhashtable_walk_stop(&iter); 3153 rhashtable_walk_exit(&iter); 3154 WRITE_ONCE(rhtab->freeing_internal, false); 3155 } 3156 3157 static int rhtab_map_get_next_key(struct bpf_map *map, void *key, void *next_key) 3158 __must_hold_shared(RCU) 3159 { 3160 struct bpf_rhtab *rhtab = container_of(map, struct bpf_rhtab, map); 3161 struct rhtab_elem *elem; 3162 3163 elem = rhashtable_next_key(&rhtab->ht, key); 3164 3165 /* if not found, return the first key */ 3166 if (PTR_ERR(elem) == -ENOENT) 3167 elem = rhashtable_next_key(&rhtab->ht, NULL); 3168 3169 if (IS_ERR(elem)) 3170 return PTR_ERR(elem); 3171 if (!elem) 3172 return -ENOENT; 3173 3174 memcpy(next_key, elem->data, map->key_size); 3175 return 0; 3176 } 3177 3178 static void rhtab_map_seq_show_elem(struct bpf_map *map, void *key, struct seq_file *m) 3179 { 3180 void *value; 3181 3182 /* Guarantee that hashtab value is not freed */ 3183 guard(rcu)(); 3184 3185 value = rhtab_map_lookup_elem(map, key); 3186 if (!value) 3187 return; 3188 3189 btf_type_seq_show(map->btf, map->btf_key_type_id, key, m); 3190 seq_puts(m, ": "); 3191 btf_type_seq_show(map->btf, map->btf_value_type_id, value, m); 3192 seq_putc(m, '\n'); 3193 } 3194 3195 static long bpf_each_rhash_elem(struct bpf_map *map, bpf_callback_t callback_fn, 3196 void *callback_ctx, u64 flags) 3197 { 3198 struct bpf_rhtab *rhtab = container_of(map, struct bpf_rhtab, map); 3199 void *prev_key = NULL; 3200 struct rhtab_elem *elem; 3201 int num_elems = 0; 3202 u64 ret = 0; 3203 3204 cant_migrate(); 3205 3206 if (flags != 0) 3207 return -EINVAL; 3208 3209 rcu_read_lock(); 3210 /* 3211 * Best-effort iteration: if rhashtable is concurrently resized or 3212 * elements are deleted/inserted, there may be missed or duplicate 3213 * elements visited. 3214 */ 3215 while ((elem = rhashtable_next_key(&rhtab->ht, prev_key))) { 3216 if (IS_ERR(elem)) 3217 break; 3218 num_elems++; 3219 ret = callback_fn((u64)(long)map, 3220 (u64)(long)elem->data, 3221 (u64)(long)rhtab_elem_value(elem, map->key_size), 3222 (u64)(long)callback_ctx, 0); 3223 if (ret) 3224 break; 3225 3226 prev_key = elem->data; /* valid while RCU held */ 3227 } 3228 rcu_read_unlock(); 3229 3230 return num_elems; 3231 } 3232 3233 static u64 rhtab_map_mem_usage(const struct bpf_map *map) 3234 { 3235 struct bpf_rhtab *rhtab = container_of(map, struct bpf_rhtab, map); 3236 u64 num_entries; 3237 3238 /* Excludes rhashtable bucket overhead (~ nelems * sizeof(void *) at 75% load). */ 3239 num_entries = atomic_read(&rhtab->ht.nelems); 3240 return sizeof(struct bpf_rhtab) + rhtab->elem_size * num_entries; 3241 } 3242 3243 static int __rhtab_map_lookup_and_delete_batch(struct bpf_map *map, 3244 const union bpf_attr *attr, 3245 union bpf_attr __user *uattr, 3246 bool do_delete) 3247 { 3248 struct bpf_rhtab *rhtab = container_of(map, struct bpf_rhtab, map); 3249 void __user *uvalues = u64_to_user_ptr(attr->batch.values); 3250 void __user *ukeys = u64_to_user_ptr(attr->batch.keys); 3251 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch); 3252 void *cursor = NULL, *keys = NULL, *values = NULL, *dst_key, *dst_val; 3253 struct rhtab_elem **del_elems = NULL; 3254 u32 max_count, total, key_size, value_size, i; 3255 bool has_next_cursor = false; 3256 struct rhtab_elem *elem; 3257 u64 elem_map_flags, map_flags; 3258 int ret = 0; 3259 3260 elem_map_flags = attr->batch.elem_flags; 3261 ret = bpf_map_check_op_flags(map, elem_map_flags, BPF_F_LOCK); 3262 if (ret) 3263 return ret; 3264 3265 map_flags = attr->batch.flags; 3266 if (map_flags) 3267 return -EINVAL; 3268 3269 max_count = attr->batch.count; 3270 if (!max_count) 3271 return 0; 3272 3273 if (put_user(0, &uattr->batch.count)) 3274 return -EFAULT; 3275 3276 key_size = map->key_size; 3277 value_size = map->value_size; 3278 3279 keys = kvmalloc_array(max_count, key_size, GFP_USER | __GFP_NOWARN); 3280 values = kvmalloc_array(max_count, value_size, GFP_USER | __GFP_NOWARN); 3281 if (do_delete) 3282 del_elems = kvmalloc_array(max_count, sizeof(void *), 3283 GFP_USER | __GFP_NOWARN); 3284 cursor = kmalloc(key_size, GFP_USER | __GFP_NOWARN); 3285 3286 if (!keys || !values || !cursor || (do_delete && !del_elems)) { 3287 ret = -ENOMEM; 3288 goto free; 3289 } 3290 3291 if (ubatch && copy_from_user(cursor, ubatch, key_size)) { 3292 ret = -EFAULT; 3293 goto free; 3294 } 3295 3296 dst_key = keys; 3297 dst_val = values; 3298 total = 0; 3299 3300 rcu_read_lock(); 3301 3302 /* 3303 * Cursor stores the key of the next-to-process element (stashed by 3304 * the previous batch). Look it up directly so the element is included 3305 * here rather than skipped by next_key(). If the cursor was deleted 3306 * concurrently (or by the previous do_delete batch), return -EAGAIN 3307 * so userspace can distinguish a lost cursor from end-of-iteration 3308 * (-ENOENT) and restart from a NULL cursor. 3309 */ 3310 if (ubatch) { 3311 elem = rhtab_lookup_elem(map, cursor); 3312 if (!elem) { 3313 rcu_read_unlock(); 3314 ret = -EAGAIN; 3315 goto free; 3316 } 3317 } else { 3318 elem = rhashtable_next_key(&rhtab->ht, NULL); 3319 } 3320 3321 while (elem && !IS_ERR(elem) && total < max_count) { 3322 memcpy(dst_key, elem->data, key_size); 3323 rhtab_read_elem_value(map, dst_val, elem, elem_map_flags); 3324 check_and_init_map_value(map, dst_val); 3325 3326 if (do_delete) 3327 del_elems[total] = elem; 3328 3329 elem = rhashtable_next_key(&rhtab->ht, dst_key); 3330 dst_key += key_size; 3331 dst_val += value_size; 3332 total++; 3333 3334 /* Bail to userspace to avoid stalls. */ 3335 if (need_resched()) 3336 break; 3337 } 3338 3339 if (elem && !IS_ERR(elem)) { 3340 /* Stash next-to-process key as cursor for the next batch. */ 3341 memcpy(cursor, elem->data, key_size); 3342 has_next_cursor = true; 3343 } 3344 3345 if (do_delete) { 3346 for (i = 0; i < total; i++) 3347 rhtab_delete_elem(rhtab, del_elems[i], NULL, 0); 3348 } 3349 3350 rcu_read_unlock(); 3351 3352 if (total == 0) { 3353 ret = -ENOENT; 3354 goto free; 3355 } 3356 3357 /* No more elements after this batch. */ 3358 if (!has_next_cursor) 3359 ret = -ENOENT; 3360 3361 if (copy_to_user(ukeys, keys, (size_t)total * key_size) || 3362 copy_to_user(uvalues, values, (size_t)total * value_size) || 3363 put_user(total, &uattr->batch.count) || 3364 (has_next_cursor && 3365 copy_to_user(u64_to_user_ptr(attr->batch.out_batch), 3366 cursor, key_size))) { 3367 ret = -EFAULT; 3368 goto free; 3369 } 3370 3371 free: 3372 kfree(cursor); 3373 kvfree(keys); 3374 kvfree(values); 3375 kvfree(del_elems); 3376 return ret; 3377 } 3378 3379 static int rhtab_map_lookup_batch(struct bpf_map *map, const union bpf_attr *attr, 3380 union bpf_attr __user *uattr) 3381 { 3382 return __rhtab_map_lookup_and_delete_batch(map, attr, uattr, false); 3383 } 3384 3385 static int rhtab_map_lookup_and_delete_batch(struct bpf_map *map, const union bpf_attr *attr, 3386 union bpf_attr __user *uattr) 3387 { 3388 return __rhtab_map_lookup_and_delete_batch(map, attr, uattr, true); 3389 } 3390 3391 struct bpf_iter_seq_rhash_map_info { 3392 struct bpf_map *map; 3393 struct bpf_rhtab *rhtab; 3394 struct rhashtable_iter iter; 3395 }; 3396 3397 static void *bpf_rhash_map_seq_start(struct seq_file *seq, loff_t *pos) 3398 __acquires(RCU) 3399 { 3400 struct bpf_iter_seq_rhash_map_info *info = seq->private; 3401 struct rhtab_elem *elem; 3402 3403 rhashtable_walk_start(&info->iter); 3404 /* 3405 * Re-deliver the element returned by walk_next() at the end of the 3406 * previous read() — bpf_seq_read may have stopped before show() 3407 * consumed it. Rehash rewinds the walker; retry on -EAGAIN. 3408 */ 3409 do { 3410 elem = rhashtable_walk_peek(&info->iter); 3411 } while (PTR_ERR(elem) == -EAGAIN); 3412 3413 if (IS_ERR(elem)) 3414 return NULL; 3415 3416 if (elem && *pos == 0) 3417 ++*pos; 3418 return elem; 3419 } 3420 3421 static void *bpf_rhash_map_seq_next(struct seq_file *seq, void *v, loff_t *pos) 3422 { 3423 struct bpf_iter_seq_rhash_map_info *info = seq->private; 3424 struct rhtab_elem *elem; 3425 3426 ++*pos; 3427 3428 /* Rehash rewinds the walker; retry until it stops returning -EAGAIN. */ 3429 do { 3430 elem = rhashtable_walk_next(&info->iter); 3431 } while (PTR_ERR(elem) == -EAGAIN); 3432 3433 if (IS_ERR(elem)) 3434 return NULL; 3435 return elem; 3436 } 3437 3438 static int __bpf_rhash_map_seq_show(struct seq_file *seq, 3439 struct rhtab_elem *elem) 3440 { 3441 struct bpf_iter_seq_rhash_map_info *info = seq->private; 3442 struct bpf_iter__bpf_map_elem ctx = {}; 3443 struct bpf_iter_meta meta; 3444 struct bpf_prog *prog; 3445 int ret = 0; 3446 3447 meta.seq = seq; 3448 prog = bpf_iter_get_info(&meta, elem == NULL); 3449 if (prog) { 3450 ctx.meta = &meta; 3451 ctx.map = info->map; 3452 if (elem) { 3453 ctx.key = elem->data; 3454 ctx.value = rhtab_elem_value(elem, info->map->key_size); 3455 } 3456 ret = bpf_iter_run_prog(prog, &ctx); 3457 } 3458 3459 return ret; 3460 } 3461 3462 static int bpf_rhash_map_seq_show(struct seq_file *seq, void *v) 3463 { 3464 return __bpf_rhash_map_seq_show(seq, v); 3465 } 3466 3467 static void bpf_rhash_map_seq_stop(struct seq_file *seq, void *v) 3468 __releases(RCU) 3469 { 3470 struct bpf_iter_seq_rhash_map_info *info = seq->private; 3471 3472 if (!v) 3473 (void)__bpf_rhash_map_seq_show(seq, NULL); 3474 3475 rhashtable_walk_stop(&info->iter); 3476 } 3477 3478 static int bpf_iter_init_rhash_map(void *priv_data, struct bpf_iter_aux_info *aux) 3479 { 3480 struct bpf_iter_seq_rhash_map_info *info = priv_data; 3481 struct bpf_map *map = aux->map; 3482 3483 bpf_map_inc_with_uref(map); 3484 info->map = map; 3485 info->rhtab = container_of(map, struct bpf_rhtab, map); 3486 rhashtable_walk_enter(&info->rhtab->ht, &info->iter); 3487 return 0; 3488 } 3489 3490 static void bpf_iter_fini_rhash_map(void *priv_data) 3491 { 3492 struct bpf_iter_seq_rhash_map_info *info = priv_data; 3493 3494 rhashtable_walk_exit(&info->iter); 3495 bpf_map_put_with_uref(info->map); 3496 } 3497 3498 static const struct seq_operations bpf_rhash_map_seq_ops = { 3499 .start = bpf_rhash_map_seq_start, 3500 .next = bpf_rhash_map_seq_next, 3501 .stop = bpf_rhash_map_seq_stop, 3502 .show = bpf_rhash_map_seq_show, 3503 }; 3504 3505 static const struct bpf_iter_seq_info rhash_iter_seq_info = { 3506 .seq_ops = &bpf_rhash_map_seq_ops, 3507 .init_seq_private = bpf_iter_init_rhash_map, 3508 .fini_seq_private = bpf_iter_fini_rhash_map, 3509 .seq_priv_size = sizeof(struct bpf_iter_seq_rhash_map_info), 3510 }; 3511 3512 BTF_ID_LIST_SINGLE(rhtab_map_btf_ids, struct, bpf_rhtab) 3513 const struct bpf_map_ops rhtab_map_ops = { 3514 .map_meta_equal = bpf_map_meta_equal, 3515 .map_alloc_check = rhtab_map_alloc_check, 3516 .map_alloc = rhtab_map_alloc, 3517 .map_free = rhtab_map_free, 3518 .map_get_next_key = rhtab_map_get_next_key, 3519 .map_release_uref = rhtab_map_free_internal_structs, 3520 .map_check_btf = rhtab_map_check_btf, 3521 .map_lookup_elem = rhtab_map_lookup_elem, 3522 .map_lookup_and_delete_elem = rhtab_map_lookup_and_delete_elem, 3523 .map_update_elem = rhtab_map_update_elem, 3524 .map_delete_elem = rhtab_map_delete_elem, 3525 .map_gen_lookup = rhtab_map_gen_lookup, 3526 .map_seq_show_elem = rhtab_map_seq_show_elem, 3527 .map_set_for_each_callback_args = map_set_for_each_callback_args, 3528 .map_for_each_callback = bpf_each_rhash_elem, 3529 .map_mem_usage = rhtab_map_mem_usage, 3530 BATCH_OPS(rhtab), 3531 .map_btf_id = &rhtab_map_btf_ids[0], 3532 .iter_seq_info = &rhash_iter_seq_info, 3533 }; 3534