1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2019 Facebook */ 3 #include <linux/rculist.h> 4 #include <linux/list.h> 5 #include <linux/hash.h> 6 #include <linux/types.h> 7 #include <linux/spinlock.h> 8 #include <linux/bpf.h> 9 #include <linux/btf_ids.h> 10 #include <linux/bpf_local_storage.h> 11 #include <net/sock.h> 12 #include <uapi/linux/sock_diag.h> 13 #include <uapi/linux/btf.h> 14 #include <linux/rcupdate.h> 15 #include <linux/rcupdate_trace.h> 16 #include <linux/rcupdate_wait.h> 17 18 #define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_CLONE) 19 20 static struct bpf_local_storage_map_bucket * 21 select_bucket(struct bpf_local_storage_map *smap, 22 struct bpf_local_storage *local_storage) 23 { 24 return &smap->buckets[hash_ptr(local_storage, smap->bucket_log)]; 25 } 26 27 static int mem_charge(struct bpf_local_storage_map *smap, void *owner, u32 size) 28 { 29 struct bpf_map *map = &smap->map; 30 31 if (!map->ops->map_local_storage_charge) 32 return 0; 33 34 return map->ops->map_local_storage_charge(smap, owner, size); 35 } 36 37 static void mem_uncharge(struct bpf_local_storage_map *smap, void *owner, 38 u32 size) 39 { 40 struct bpf_map *map = &smap->map; 41 42 if (map->ops->map_local_storage_uncharge) 43 map->ops->map_local_storage_uncharge(smap, owner, size); 44 } 45 46 static struct bpf_local_storage __rcu ** 47 owner_storage(struct bpf_local_storage_map *smap, void *owner) 48 { 49 struct bpf_map *map = &smap->map; 50 51 return map->ops->map_owner_storage_ptr(owner); 52 } 53 54 static bool selem_linked_to_storage_lockless(const struct bpf_local_storage_elem *selem) 55 { 56 return !hlist_unhashed_lockless(&selem->snode); 57 } 58 59 static bool selem_linked_to_storage(const struct bpf_local_storage_elem *selem) 60 { 61 return !hlist_unhashed(&selem->snode); 62 } 63 64 static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem) 65 { 66 return !hlist_unhashed(&selem->map_node); 67 } 68 69 struct bpf_local_storage_elem * 70 bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, 71 void *value, bool swap_uptrs) 72 { 73 struct bpf_local_storage_elem *selem; 74 75 if (mem_charge(smap, owner, smap->elem_size)) 76 return NULL; 77 78 selem = bpf_map_kmalloc_nolock(&smap->map, smap->elem_size, 79 __GFP_ZERO, NUMA_NO_NODE); 80 81 if (selem) { 82 RCU_INIT_POINTER(SDATA(selem)->smap, smap); 83 atomic_set(&selem->state, 0); 84 85 if (value) { 86 /* No need to call check_and_init_map_value as memory is zero init */ 87 copy_map_value(&smap->map, SDATA(selem)->data, value); 88 if (swap_uptrs) 89 bpf_obj_swap_uptrs(smap->map.record, SDATA(selem)->data, value); 90 } 91 return selem; 92 } 93 94 mem_uncharge(smap, owner, smap->elem_size); 95 96 return NULL; 97 } 98 99 static void bpf_local_storage_free_trace_rcu(struct rcu_head *rcu) 100 { 101 struct bpf_local_storage *local_storage; 102 103 /* 104 * RCU Tasks Trace grace period implies RCU grace period, do 105 * kfree() directly. 106 */ 107 local_storage = container_of(rcu, struct bpf_local_storage, rcu); 108 kfree(local_storage); 109 } 110 111 static void bpf_local_storage_free(struct bpf_local_storage *local_storage, 112 bool reuse_now) 113 { 114 if (!local_storage) 115 return; 116 117 if (reuse_now) { 118 kfree_rcu(local_storage, rcu); 119 return; 120 } 121 122 call_rcu_tasks_trace(&local_storage->rcu, 123 bpf_local_storage_free_trace_rcu); 124 } 125 126 static void bpf_selem_free_trace_rcu(struct rcu_head *rcu) 127 { 128 struct bpf_local_storage_elem *selem; 129 struct bpf_local_storage_map *smap; 130 131 selem = container_of(rcu, struct bpf_local_storage_elem, rcu); 132 /* The bpf_local_storage_map_free will wait for rcu_barrier */ 133 smap = rcu_dereference_check(SDATA(selem)->smap, 1); 134 135 if (smap) 136 bpf_obj_free_fields(smap->map.record, SDATA(selem)->data); 137 /* 138 * RCU Tasks Trace grace period implies RCU grace period, do 139 * kfree() directly. 140 */ 141 kfree(selem); 142 } 143 144 void bpf_selem_free(struct bpf_local_storage_elem *selem, 145 bool reuse_now) 146 { 147 struct bpf_local_storage_map *smap; 148 149 smap = rcu_dereference_check(SDATA(selem)->smap, 1); 150 151 if (reuse_now) { 152 if (smap) 153 bpf_obj_free_fields(smap->map.record, SDATA(selem)->data); 154 kfree_rcu(selem, rcu); 155 return; 156 } 157 158 call_rcu_tasks_trace(&selem->rcu, bpf_selem_free_trace_rcu); 159 } 160 161 static void bpf_selem_free_list(struct hlist_head *list, bool reuse_now) 162 { 163 struct bpf_local_storage_elem *selem; 164 struct hlist_node *n; 165 166 /* The "_safe" iteration is needed. 167 * The loop is not removing the selem from the list 168 * but bpf_selem_free will use the selem->rcu_head 169 * which is union-ized with the selem->free_node. 170 */ 171 hlist_for_each_entry_safe(selem, n, list, free_node) 172 bpf_selem_free(selem, reuse_now); 173 } 174 175 static void bpf_selem_unlink_storage_nolock_misc(struct bpf_local_storage_elem *selem, 176 struct bpf_local_storage_map *smap, 177 struct bpf_local_storage *local_storage, 178 bool free_local_storage, bool pin_owner) 179 { 180 void *owner = local_storage->owner; 181 u32 uncharge = smap->elem_size; 182 183 if (rcu_access_pointer(local_storage->cache[smap->cache_idx]) == 184 SDATA(selem)) 185 RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL); 186 187 if (pin_owner && !refcount_inc_not_zero(&local_storage->owner_refcnt)) 188 return; 189 190 uncharge += free_local_storage ? sizeof(*local_storage) : 0; 191 mem_uncharge(smap, local_storage->owner, uncharge); 192 local_storage->mem_charge -= uncharge; 193 194 if (free_local_storage) { 195 local_storage->owner = NULL; 196 197 /* After this RCU_INIT, owner may be freed and cannot be used */ 198 RCU_INIT_POINTER(*owner_storage(smap, owner), NULL); 199 } 200 201 if (pin_owner) 202 bpf_local_storage_unpin_owner(local_storage); 203 } 204 205 /* local_storage->lock must be held and selem->local_storage == local_storage. 206 * The caller must ensure selem->smap is still valid to be 207 * dereferenced for its smap->elem_size and smap->cache_idx. 208 */ 209 static bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage, 210 struct bpf_local_storage_elem *selem, 211 struct hlist_head *free_selem_list) 212 { 213 struct bpf_local_storage_map *smap; 214 bool free_local_storage; 215 216 smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held()); 217 218 free_local_storage = hlist_is_singular_node(&selem->snode, 219 &local_storage->list); 220 221 bpf_selem_unlink_storage_nolock_misc(selem, smap, local_storage, 222 free_local_storage, false); 223 224 hlist_del_init_rcu(&selem->snode); 225 226 hlist_add_head(&selem->free_node, free_selem_list); 227 228 return free_local_storage; 229 } 230 231 void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage, 232 struct bpf_local_storage_elem *selem) 233 { 234 struct bpf_local_storage_map *smap; 235 236 smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held()); 237 local_storage->mem_charge += smap->elem_size; 238 239 RCU_INIT_POINTER(selem->local_storage, local_storage); 240 hlist_add_head_rcu(&selem->snode, &local_storage->list); 241 } 242 243 /* Must be called with the owning local_storage->lock held. */ 244 static int bpf_selem_unlink_map(struct bpf_local_storage_elem *selem) 245 { 246 struct bpf_local_storage *local_storage; 247 struct bpf_local_storage_map *smap; 248 struct bpf_local_storage_map_bucket *b; 249 int err; 250 251 lockdep_assert_irqs_disabled(); 252 253 local_storage = rcu_dereference_check(selem->local_storage, 254 bpf_rcu_lock_held()); 255 smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held()); 256 b = select_bucket(smap, local_storage); 257 err = raw_res_spin_lock(&b->lock); 258 if (err) 259 return err; 260 261 hlist_del_init_rcu(&selem->map_node); 262 raw_res_spin_unlock(&b->lock); 263 264 return 0; 265 } 266 267 static void bpf_selem_unlink_map_nolock(struct bpf_local_storage_elem *selem) 268 { 269 hlist_del_init_rcu(&selem->map_node); 270 } 271 272 int bpf_selem_link_map(struct bpf_local_storage_map *smap, 273 struct bpf_local_storage *local_storage, 274 struct bpf_local_storage_elem *selem) 275 { 276 struct bpf_local_storage_map_bucket *b; 277 unsigned long flags; 278 int err; 279 280 b = select_bucket(smap, local_storage); 281 282 err = raw_res_spin_lock_irqsave(&b->lock, flags); 283 if (err) 284 return err; 285 286 hlist_add_head_rcu(&selem->map_node, &b->list); 287 raw_res_spin_unlock_irqrestore(&b->lock, flags); 288 289 return 0; 290 } 291 292 static void bpf_selem_link_map_nolock(struct bpf_local_storage_map_bucket *b, 293 struct bpf_local_storage_elem *selem) 294 { 295 hlist_add_head_rcu(&selem->map_node, &b->list); 296 } 297 298 /* 299 * Unlink an selem from map and local storage with lock held. 300 * This is the common path used by local storages to delete an selem. 301 */ 302 int bpf_selem_unlink(struct bpf_local_storage_elem *selem) 303 { 304 struct bpf_local_storage *local_storage; 305 bool free_local_storage = false; 306 HLIST_HEAD(selem_free_list); 307 unsigned long flags; 308 int err; 309 310 if (in_nmi()) 311 return -EOPNOTSUPP; 312 313 if (unlikely(!selem_linked_to_storage_lockless(selem))) 314 /* selem has already been unlinked from sk */ 315 return 0; 316 317 local_storage = rcu_dereference_check(selem->local_storage, 318 bpf_rcu_lock_held()); 319 320 err = raw_res_spin_lock_irqsave(&local_storage->lock, flags); 321 if (err) 322 return err; 323 324 if (likely(selem_linked_to_storage(selem))) { 325 /* Always unlink from map before unlinking from local_storage 326 * because selem will be freed after successfully unlinked from 327 * the local_storage. 328 */ 329 err = bpf_selem_unlink_map(selem); 330 if (err) 331 goto out; 332 333 free_local_storage = bpf_selem_unlink_storage_nolock( 334 local_storage, selem, &selem_free_list); 335 } 336 out: 337 raw_res_spin_unlock_irqrestore(&local_storage->lock, flags); 338 339 bpf_selem_free_list(&selem_free_list, false); 340 341 if (free_local_storage) 342 bpf_local_storage_free(local_storage, false); 343 344 return err; 345 } 346 347 /* 348 * Unlink an selem from map and local storage with lockless fallback if callers 349 * are racing or rqspinlock returns error. It should only be called by 350 * bpf_local_storage_destroy() or bpf_local_storage_map_free(). 351 */ 352 static void bpf_selem_unlink_nofail(struct bpf_local_storage_elem *selem, 353 struct bpf_local_storage_map_bucket *b) 354 { 355 bool in_map_free = !!b, free_storage = false, sleepable_users = false; 356 bool keep_fields = false; 357 struct bpf_local_storage *local_storage; 358 struct bpf_local_storage_map *smap; 359 unsigned long flags; 360 int err, unlink = 0; 361 362 local_storage = rcu_dereference_check(selem->local_storage, bpf_rcu_lock_held()); 363 smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held()); 364 365 if (smap) { 366 /* A map that is freed, or NULL here, has no program left. */ 367 sleepable_users = !in_map_free && 368 (atomic64_read(&smap->map.sleepable_refcnt) || 369 READ_ONCE(smap->map.was_inner_map)); 370 b = b ? : select_bucket(smap, local_storage); 371 err = raw_res_spin_lock_irqsave(&b->lock, flags); 372 if (!err) { 373 /* 374 * Call bpf_obj_free_fields() under b->lock to make sure it is done 375 * exactly once for an selem. Safe to free special fields immediately 376 * as no BPF program should be referencing the selem, unless it 377 * is sleepable: see below. 378 */ 379 if (likely(selem_linked_to_map(selem))) { 380 hlist_del_init_rcu(&selem->map_node); 381 if (sleepable_users) 382 keep_fields = true; 383 else 384 bpf_obj_free_fields(smap->map.record, 385 SDATA(selem)->data); 386 unlink++; 387 } 388 raw_res_spin_unlock_irqrestore(&b->lock, flags); 389 } 390 /* 391 * Highly unlikely scenario: resource leak 392 * 393 * When map_free(selem1), destroy(selem1) and destroy(selem2) are racing 394 * and both selem belong to the same bucket, if destroy(selem2) acquired 395 * b->lock and block for too long, neither map_free(selem1) and 396 * destroy(selem1) will be able to free the special field associated 397 * with selem1 as raw_res_spin_lock_irqsave() returns -ETIMEDOUT. 398 */ 399 WARN_ON_ONCE(err && in_map_free); 400 /* 401 * A sleepable program can still use, and fill, the fields of a value 402 * that it got before it let go of the owner. As for bpf_selem_unlink(), 403 * bpf_selem_free_trace_rcu() frees them, and takes smap for that: 404 * map_free() waits for this RCU read side critical section and then 405 * for the callback. It must not take smap away from an selem that 406 * destroy() has unlinked from the map. 407 */ 408 if (in_map_free ? err || unlink : !err && !keep_fields) 409 RCU_INIT_POINTER(SDATA(selem)->smap, NULL); 410 } 411 412 if (local_storage) { 413 err = raw_res_spin_lock_irqsave(&local_storage->lock, flags); 414 if (!err) { 415 if (likely(selem_linked_to_storage(selem))) { 416 free_storage = hlist_is_singular_node(&selem->snode, 417 &local_storage->list); 418 /* 419 * Okay to skip clearing owner_storage and storage->owner in 420 * destroy() since the owner is going away. No user or bpf 421 * programs should be able to reference it. 422 */ 423 if (smap && in_map_free) 424 bpf_selem_unlink_storage_nolock_misc( 425 selem, smap, local_storage, 426 free_storage, true); 427 hlist_del_init_rcu(&selem->snode); 428 unlink++; 429 } 430 raw_res_spin_unlock_irqrestore(&local_storage->lock, flags); 431 } 432 /* 433 * Highly unlikely scenario: memory leak 434 * 435 * When destroy() fails to acqurire local_storage->lock and initializes 436 * selem->local_storage to NULL before any racing map_free() sees the same 437 * selem, no one will free the local storage. 438 */ 439 WARN_ON_ONCE(err && !in_map_free); 440 if (!err || !in_map_free) 441 RCU_INIT_POINTER(selem->local_storage, NULL); 442 } 443 444 if (unlink != 2) 445 atomic_or(in_map_free ? SELEM_MAP_UNLINKED : SELEM_STORAGE_UNLINKED, &selem->state); 446 447 /* 448 * Normally, an selem can be unlinked under local_storage->lock and b->lock, and 449 * then freed after an RCU grace period. However, if destroy() and map_free() are 450 * racing or rqspinlock returns errors in unlikely situations (unlink != 2), free 451 * the selem only after both map_free() and destroy() see the selem. 452 * 453 * The owner that map_free() takes an selem from is alive: sleepable code, which 454 * is in no RCU read side critical section, may be walking its list for another 455 * map. And it can still use a value that it got before it let go of an owner 456 * that destroy() is called for. 457 */ 458 if (unlink == 2 || 459 atomic_cmpxchg(&selem->state, SELEM_UNLINKED, SELEM_TOFREE) == SELEM_UNLINKED) 460 bpf_selem_free(selem, !in_map_free && !sleepable_users); 461 462 if (free_storage) 463 bpf_local_storage_free(local_storage, !in_map_free); 464 } 465 466 void __bpf_local_storage_insert_cache(struct bpf_local_storage *local_storage, 467 struct bpf_local_storage_map *smap, 468 struct bpf_local_storage_elem *selem) 469 { 470 unsigned long flags; 471 int err; 472 473 /* spinlock is needed to avoid racing with the 474 * parallel delete. Otherwise, publishing an already 475 * deleted sdata to the cache will become a use-after-free 476 * problem in the next bpf_local_storage_lookup(). 477 */ 478 err = raw_res_spin_lock_irqsave(&local_storage->lock, flags); 479 if (err) 480 return; 481 482 if (selem_linked_to_storage(selem)) 483 rcu_assign_pointer(local_storage->cache[smap->cache_idx], SDATA(selem)); 484 raw_res_spin_unlock_irqrestore(&local_storage->lock, flags); 485 } 486 487 static int check_flags(const struct bpf_local_storage_data *old_sdata, 488 u64 map_flags) 489 { 490 if (old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST) 491 /* elem already exists */ 492 return -EEXIST; 493 494 if (!old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_EXIST) 495 /* elem doesn't exist, cannot update it */ 496 return -ENOENT; 497 498 return 0; 499 } 500 501 int bpf_local_storage_alloc(void *owner, 502 struct bpf_local_storage_map *smap, 503 struct bpf_local_storage_elem *first_selem) 504 { 505 struct bpf_local_storage *prev_storage, *storage; 506 struct bpf_local_storage **owner_storage_ptr; 507 struct bpf_local_storage_map_bucket *b; 508 unsigned long flags; 509 int err; 510 511 err = mem_charge(smap, owner, sizeof(*storage)); 512 if (err) 513 return err; 514 515 storage = bpf_map_kmalloc_nolock(&smap->map, sizeof(*storage), 516 __GFP_ZERO, NUMA_NO_NODE); 517 if (!storage) { 518 err = -ENOMEM; 519 goto uncharge; 520 } 521 522 INIT_HLIST_HEAD(&storage->list); 523 raw_res_spin_lock_init(&storage->lock); 524 storage->owner = owner; 525 storage->mem_charge = sizeof(*storage); 526 refcount_set(&storage->owner_refcnt, 1); 527 528 bpf_selem_link_storage_nolock(storage, first_selem); 529 530 b = select_bucket(smap, storage); 531 err = raw_res_spin_lock_irqsave(&b->lock, flags); 532 if (err) 533 goto uncharge; 534 535 owner_storage_ptr = 536 (struct bpf_local_storage **)owner_storage(smap, owner); 537 /* Publish storage to the owner. 538 * Instead of using any lock of the kernel object (i.e. owner), 539 * cmpxchg will work with any kernel object regardless what 540 * the running context is, bh, irq...etc. 541 * 542 * From now on, the owner->storage pointer (e.g. sk->sk_bpf_storage) 543 * is protected by the storage->lock. Hence, when freeing 544 * the owner->storage, the storage->lock must be held before 545 * setting owner->storage ptr to NULL. 546 */ 547 prev_storage = cmpxchg(owner_storage_ptr, NULL, storage); 548 if (unlikely(prev_storage)) { 549 raw_res_spin_unlock_irqrestore(&b->lock, flags); 550 err = -EAGAIN; 551 goto uncharge; 552 } 553 /* 554 * Not before the storage is published: the owner does not wait for 555 * users of a storage that it knows nothing of, so that nobody may find 556 * such a storage through the map. b->lock holds off those who find 557 * first_selem through the owner and want to unlink it. 558 */ 559 bpf_selem_link_map_nolock(b, first_selem); 560 raw_res_spin_unlock_irqrestore(&b->lock, flags); 561 562 return 0; 563 564 uncharge: 565 bpf_local_storage_free(storage, true); 566 mem_uncharge(smap, owner, sizeof(*storage)); 567 return err; 568 } 569 570 /* sk cannot be going away because it is linking new elem 571 * to sk->sk_bpf_storage. (i.e. sk->sk_refcnt cannot be 0). 572 * Otherwise, it will become a leak (and other memory issues 573 * during map destruction). 574 */ 575 struct bpf_local_storage_data * 576 bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, 577 void *value, u64 map_flags, bool swap_uptrs) 578 { 579 struct bpf_local_storage_data *old_sdata = NULL; 580 struct bpf_local_storage_elem *alloc_selem, *selem = NULL; 581 struct bpf_local_storage *local_storage; 582 struct bpf_local_storage_map_bucket *b; 583 HLIST_HEAD(old_selem_free_list); 584 unsigned long flags; 585 int err; 586 587 /* BPF_EXIST and BPF_NOEXIST cannot be both set */ 588 if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST) || 589 /* BPF_F_LOCK can only be used in a value with spin_lock */ 590 unlikely((map_flags & BPF_F_LOCK) && 591 !btf_record_has_field(smap->map.record, BPF_SPIN_LOCK))) 592 return ERR_PTR(-EINVAL); 593 594 local_storage = rcu_dereference_check(*owner_storage(smap, owner), 595 bpf_rcu_lock_held()); 596 if (!local_storage || hlist_empty(&local_storage->list)) { 597 /* Very first elem for the owner */ 598 err = check_flags(NULL, map_flags); 599 if (err) 600 return ERR_PTR(err); 601 602 selem = bpf_selem_alloc(smap, owner, value, swap_uptrs); 603 if (!selem) 604 return ERR_PTR(-ENOMEM); 605 606 err = bpf_local_storage_alloc(owner, smap, selem); 607 if (err) { 608 bpf_selem_free(selem, true); 609 mem_uncharge(smap, owner, smap->elem_size); 610 return ERR_PTR(err); 611 } 612 613 return SDATA(selem); 614 } 615 616 if ((map_flags & BPF_F_LOCK) && !(map_flags & BPF_NOEXIST)) { 617 /* Hoping to find an old_sdata to do inline update 618 * such that it can avoid taking the local_storage->lock 619 * and changing the lists. 620 */ 621 old_sdata = 622 bpf_local_storage_lookup(local_storage, smap, false); 623 err = check_flags(old_sdata, map_flags); 624 if (err) 625 return ERR_PTR(err); 626 if (old_sdata && selem_linked_to_storage_lockless(SELEM(old_sdata))) { 627 copy_map_value_locked(&smap->map, old_sdata->data, 628 value, false); 629 return old_sdata; 630 } 631 } 632 633 /* A lookup has just been done before and concluded a new selem is 634 * needed. The chance of an unnecessary alloc is unlikely. 635 */ 636 alloc_selem = selem = bpf_selem_alloc(smap, owner, value, swap_uptrs); 637 if (!alloc_selem) 638 return ERR_PTR(-ENOMEM); 639 640 err = raw_res_spin_lock_irqsave(&local_storage->lock, flags); 641 if (err) 642 goto free_selem; 643 644 /* Recheck local_storage->list under local_storage->lock */ 645 if (unlikely(hlist_empty(&local_storage->list))) { 646 /* A parallel del is happening and local_storage is going 647 * away. It has just been checked before, so very 648 * unlikely. Return instead of retry to keep things 649 * simple. 650 */ 651 err = -EAGAIN; 652 goto unlock; 653 } 654 655 old_sdata = bpf_local_storage_lookup(local_storage, smap, false); 656 err = check_flags(old_sdata, map_flags); 657 if (err) 658 goto unlock; 659 660 if (old_sdata && (map_flags & BPF_F_LOCK)) { 661 copy_map_value_locked(&smap->map, old_sdata->data, value, 662 false); 663 selem = SELEM(old_sdata); 664 goto unlock; 665 } 666 667 b = select_bucket(smap, local_storage); 668 669 /* local_storage->lock is held, so IRQs are already disabled. */ 670 err = raw_res_spin_lock(&b->lock); 671 if (err) 672 goto unlock; 673 674 alloc_selem = NULL; 675 /* First, link the new selem to the map */ 676 bpf_selem_link_map_nolock(b, selem); 677 678 /* Second, link (and publish) the new selem to local_storage */ 679 bpf_selem_link_storage_nolock(local_storage, selem); 680 681 /* Third, remove old selem, SELEM(old_sdata) */ 682 if (old_sdata) { 683 bpf_selem_unlink_map_nolock(SELEM(old_sdata)); 684 bpf_selem_unlink_storage_nolock(local_storage, SELEM(old_sdata), 685 &old_selem_free_list); 686 } 687 688 raw_res_spin_unlock(&b->lock); 689 unlock: 690 raw_res_spin_unlock_irqrestore(&local_storage->lock, flags); 691 free_selem: 692 bpf_selem_free_list(&old_selem_free_list, false); 693 if (alloc_selem) { 694 mem_uncharge(smap, owner, smap->elem_size); 695 bpf_selem_free(alloc_selem, true); 696 } 697 return err ? ERR_PTR(err) : SDATA(selem); 698 } 699 700 static u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache) 701 { 702 u64 min_usage = U64_MAX; 703 u16 i, res = 0; 704 705 spin_lock(&cache->idx_lock); 706 707 for (i = 0; i < BPF_LOCAL_STORAGE_CACHE_SIZE; i++) { 708 if (cache->idx_usage_counts[i] < min_usage) { 709 min_usage = cache->idx_usage_counts[i]; 710 res = i; 711 712 /* Found a free cache_idx */ 713 if (!min_usage) 714 break; 715 } 716 } 717 cache->idx_usage_counts[res]++; 718 719 spin_unlock(&cache->idx_lock); 720 721 return res; 722 } 723 724 static void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache, 725 u16 idx) 726 { 727 spin_lock(&cache->idx_lock); 728 cache->idx_usage_counts[idx]--; 729 spin_unlock(&cache->idx_lock); 730 } 731 732 int bpf_local_storage_map_alloc_check(union bpf_attr *attr) 733 { 734 if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK || 735 !(attr->map_flags & BPF_F_NO_PREALLOC) || 736 attr->max_entries || 737 attr->key_size != sizeof(int) || !attr->value_size || 738 /* Enforce BTF for userspace sk dumping */ 739 !attr->btf_key_type_id || !attr->btf_value_type_id) 740 return -EINVAL; 741 742 if (attr->value_size > BPF_LOCAL_STORAGE_MAX_VALUE_SIZE) 743 return -E2BIG; 744 745 return 0; 746 } 747 748 int bpf_local_storage_map_check_btf(struct bpf_map *map, 749 const struct btf *btf, 750 const struct btf_type *key_type, 751 const struct btf_type *value_type) 752 { 753 if (!btf_type_is_i32(key_type)) 754 return -EINVAL; 755 756 return 0; 757 } 758 759 /* 760 * Destroy local storage when the owner is going away. Caller must uncharge memory 761 * if memory charging is used. 762 */ 763 u32 bpf_local_storage_destroy(struct bpf_local_storage *local_storage) 764 { 765 struct bpf_local_storage_elem *selem; 766 767 /* Neither the bpf_prog nor the bpf_map's syscall 768 * could be modifying the local_storage->list now. 769 * Thus, no elem can be added to or deleted from the 770 * local_storage->list by the bpf_prog or by the bpf_map's syscall. 771 * 772 * It is racing with bpf_local_storage_map_free() alone 773 * when unlinking elem from the local_storage->list and 774 * the map's bucket->list. 775 */ 776 hlist_for_each_entry_rcu(selem, &local_storage->list, snode) 777 bpf_selem_unlink_nofail(selem, NULL); 778 779 if (!refcount_dec_and_test(&local_storage->owner_refcnt)) { 780 while (refcount_read(&local_storage->owner_refcnt)) 781 cpu_relax(); 782 /* 783 * Paired with bpf_local_storage_unpin_owner() to make sure 784 * destroy() sees the correct local_storage->mem_charge. 785 */ 786 smp_mb(); 787 } 788 789 return local_storage->mem_charge; 790 } 791 792 u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map) 793 { 794 struct bpf_local_storage_map *smap = (struct bpf_local_storage_map *)map; 795 u64 usage = sizeof(*smap); 796 797 /* The dynamically callocated selems are not counted currently. */ 798 usage += sizeof(*smap->buckets) * (1ULL << smap->bucket_log); 799 return usage; 800 } 801 802 struct bpf_map * 803 bpf_local_storage_map_alloc(union bpf_attr *attr, 804 struct bpf_local_storage_cache *cache) 805 { 806 struct bpf_local_storage_map *smap; 807 unsigned int i; 808 u32 nbuckets; 809 int err; 810 811 smap = bpf_map_area_alloc(sizeof(*smap), NUMA_NO_NODE); 812 if (!smap) 813 return ERR_PTR(-ENOMEM); 814 bpf_map_init_from_attr(&smap->map, attr); 815 816 nbuckets = roundup_pow_of_two(num_possible_cpus()); 817 /* Use at least 2 buckets, select_bucket() is undefined behavior with 1 bucket */ 818 nbuckets = max_t(u32, 2, nbuckets); 819 smap->bucket_log = ilog2(nbuckets); 820 821 smap->buckets = bpf_map_kvcalloc(&smap->map, nbuckets, 822 sizeof(*smap->buckets), GFP_USER | __GFP_NOWARN); 823 if (!smap->buckets) { 824 err = -ENOMEM; 825 goto free_smap; 826 } 827 828 for (i = 0; i < nbuckets; i++) { 829 INIT_HLIST_HEAD(&smap->buckets[i].list); 830 raw_res_spin_lock_init(&smap->buckets[i].lock); 831 } 832 833 smap->elem_size = offsetof(struct bpf_local_storage_elem, 834 sdata.data[attr->value_size]); 835 836 smap->cache_idx = bpf_local_storage_cache_idx_get(cache); 837 return &smap->map; 838 839 free_smap: 840 kvfree(smap->buckets); 841 bpf_map_area_free(smap); 842 return ERR_PTR(err); 843 } 844 845 void bpf_local_storage_map_free(struct bpf_map *map, 846 struct bpf_local_storage_cache *cache) 847 { 848 struct bpf_local_storage_map_bucket *b; 849 struct bpf_local_storage_elem *selem; 850 struct bpf_local_storage_map *smap; 851 unsigned int i; 852 853 smap = (struct bpf_local_storage_map *)map; 854 bpf_local_storage_cache_idx_free(cache, smap->cache_idx); 855 856 /* Note that this map might be concurrently cloned from 857 * bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone 858 * RCU read section to finish before proceeding. New RCU 859 * read sections should be prevented via bpf_map_inc_not_zero. 860 */ 861 synchronize_rcu(); 862 863 /* bpf prog and the userspace can no longer access this map 864 * now. No new selem (of this map) can be added 865 * to the owner->storage or to the map bucket's list. 866 * 867 * The elem of this map can be cleaned up here 868 * or when the storage is freed e.g. 869 * by bpf_sk_storage_free() during __sk_destruct(). 870 */ 871 for (i = 0; i < (1U << smap->bucket_log); i++) { 872 b = &smap->buckets[i]; 873 874 rcu_read_lock(); 875 /* No one is adding to b->list now */ 876 restart: 877 hlist_for_each_entry_rcu(selem, &b->list, map_node) { 878 bpf_selem_unlink_nofail(selem, b); 879 880 if (need_resched()) { 881 cond_resched_rcu(); 882 goto restart; 883 } 884 } 885 rcu_read_unlock(); 886 } 887 888 /* While freeing the storage we may still need to access the map. 889 * 890 * e.g. when bpf_sk_storage_free() has unlinked selem from the map 891 * which then made the above while((selem = ...)) loop 892 * exit immediately. 893 * 894 * However, while freeing the storage one still needs to access the 895 * smap->elem_size to do the uncharging in 896 * bpf_selem_unlink_storage_nolock(). 897 * 898 * Hence, wait another rcu grace period for the storage to be freed. 899 */ 900 synchronize_rcu(); 901 902 /* smap remains in use regardless of kmalloc_nolock, so wait unconditionally. */ 903 rcu_barrier_tasks_trace(); 904 rcu_barrier(); 905 kvfree(smap->buckets); 906 bpf_map_area_free(smap); 907 } 908