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_elem *selem) 23 { 24 return &smap->buckets[hash_ptr(selem, 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(const struct bpf_local_storage_elem *selem) 55 { 56 return !hlist_unhashed(&selem->snode); 57 } 58 59 static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem) 60 { 61 return !hlist_unhashed(&selem->map_node); 62 } 63 64 struct bpf_local_storage_elem * 65 bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, 66 void *value, bool charge_mem, gfp_t gfp_flags) 67 { 68 struct bpf_local_storage_elem *selem; 69 70 if (charge_mem && mem_charge(smap, owner, smap->elem_size)) 71 return NULL; 72 73 selem = bpf_map_kzalloc(&smap->map, smap->elem_size, 74 gfp_flags | __GFP_NOWARN); 75 if (selem) { 76 if (value) 77 memcpy(SDATA(selem)->data, value, smap->map.value_size); 78 return selem; 79 } 80 81 if (charge_mem) 82 mem_uncharge(smap, owner, smap->elem_size); 83 84 return NULL; 85 } 86 87 void bpf_local_storage_free_rcu(struct rcu_head *rcu) 88 { 89 struct bpf_local_storage *local_storage; 90 91 /* If RCU Tasks Trace grace period implies RCU grace period, do 92 * kfree(), else do kfree_rcu(). 93 */ 94 local_storage = container_of(rcu, struct bpf_local_storage, rcu); 95 if (rcu_trace_implies_rcu_gp()) 96 kfree(local_storage); 97 else 98 kfree_rcu(local_storage, rcu); 99 } 100 101 static void bpf_selem_free_rcu(struct rcu_head *rcu) 102 { 103 struct bpf_local_storage_elem *selem; 104 105 selem = container_of(rcu, struct bpf_local_storage_elem, rcu); 106 if (rcu_trace_implies_rcu_gp()) 107 kfree(selem); 108 else 109 kfree_rcu(selem, rcu); 110 } 111 112 /* local_storage->lock must be held and selem->local_storage == local_storage. 113 * The caller must ensure selem->smap is still valid to be 114 * dereferenced for its smap->elem_size and smap->cache_idx. 115 */ 116 bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage, 117 struct bpf_local_storage_elem *selem, 118 bool uncharge_mem, bool use_trace_rcu) 119 { 120 struct bpf_local_storage_map *smap; 121 bool free_local_storage; 122 void *owner; 123 124 smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held()); 125 owner = local_storage->owner; 126 127 /* All uncharging on the owner must be done first. 128 * The owner may be freed once the last selem is unlinked 129 * from local_storage. 130 */ 131 if (uncharge_mem) 132 mem_uncharge(smap, owner, smap->elem_size); 133 134 free_local_storage = hlist_is_singular_node(&selem->snode, 135 &local_storage->list); 136 if (free_local_storage) { 137 mem_uncharge(smap, owner, sizeof(struct bpf_local_storage)); 138 local_storage->owner = NULL; 139 140 /* After this RCU_INIT, owner may be freed and cannot be used */ 141 RCU_INIT_POINTER(*owner_storage(smap, owner), NULL); 142 143 /* local_storage is not freed now. local_storage->lock is 144 * still held and raw_spin_unlock_bh(&local_storage->lock) 145 * will be done by the caller. 146 * 147 * Although the unlock will be done under 148 * rcu_read_lock(), it is more intuitive to 149 * read if the freeing of the storage is done 150 * after the raw_spin_unlock_bh(&local_storage->lock). 151 * 152 * Hence, a "bool free_local_storage" is returned 153 * to the caller which then calls then frees the storage after 154 * all the RCU grace periods have expired. 155 */ 156 } 157 hlist_del_init_rcu(&selem->snode); 158 if (rcu_access_pointer(local_storage->cache[smap->cache_idx]) == 159 SDATA(selem)) 160 RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL); 161 162 if (use_trace_rcu) 163 call_rcu_tasks_trace(&selem->rcu, bpf_selem_free_rcu); 164 else 165 kfree_rcu(selem, rcu); 166 167 return free_local_storage; 168 } 169 170 static void __bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem, 171 bool use_trace_rcu) 172 { 173 struct bpf_local_storage *local_storage; 174 bool free_local_storage = false; 175 unsigned long flags; 176 177 if (unlikely(!selem_linked_to_storage(selem))) 178 /* selem has already been unlinked from sk */ 179 return; 180 181 local_storage = rcu_dereference_check(selem->local_storage, 182 bpf_rcu_lock_held()); 183 raw_spin_lock_irqsave(&local_storage->lock, flags); 184 if (likely(selem_linked_to_storage(selem))) 185 free_local_storage = bpf_selem_unlink_storage_nolock( 186 local_storage, selem, true, use_trace_rcu); 187 raw_spin_unlock_irqrestore(&local_storage->lock, flags); 188 189 if (free_local_storage) { 190 if (use_trace_rcu) 191 call_rcu_tasks_trace(&local_storage->rcu, 192 bpf_local_storage_free_rcu); 193 else 194 kfree_rcu(local_storage, rcu); 195 } 196 } 197 198 void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage, 199 struct bpf_local_storage_elem *selem) 200 { 201 RCU_INIT_POINTER(selem->local_storage, local_storage); 202 hlist_add_head_rcu(&selem->snode, &local_storage->list); 203 } 204 205 void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem) 206 { 207 struct bpf_local_storage_map *smap; 208 struct bpf_local_storage_map_bucket *b; 209 unsigned long flags; 210 211 if (unlikely(!selem_linked_to_map(selem))) 212 /* selem has already be unlinked from smap */ 213 return; 214 215 smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held()); 216 b = select_bucket(smap, selem); 217 raw_spin_lock_irqsave(&b->lock, flags); 218 if (likely(selem_linked_to_map(selem))) 219 hlist_del_init_rcu(&selem->map_node); 220 raw_spin_unlock_irqrestore(&b->lock, flags); 221 } 222 223 void bpf_selem_link_map(struct bpf_local_storage_map *smap, 224 struct bpf_local_storage_elem *selem) 225 { 226 struct bpf_local_storage_map_bucket *b = select_bucket(smap, selem); 227 unsigned long flags; 228 229 raw_spin_lock_irqsave(&b->lock, flags); 230 RCU_INIT_POINTER(SDATA(selem)->smap, smap); 231 hlist_add_head_rcu(&selem->map_node, &b->list); 232 raw_spin_unlock_irqrestore(&b->lock, flags); 233 } 234 235 void bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool use_trace_rcu) 236 { 237 /* Always unlink from map before unlinking from local_storage 238 * because selem will be freed after successfully unlinked from 239 * the local_storage. 240 */ 241 bpf_selem_unlink_map(selem); 242 __bpf_selem_unlink_storage(selem, use_trace_rcu); 243 } 244 245 struct bpf_local_storage_data * 246 bpf_local_storage_lookup(struct bpf_local_storage *local_storage, 247 struct bpf_local_storage_map *smap, 248 bool cacheit_lockit) 249 { 250 struct bpf_local_storage_data *sdata; 251 struct bpf_local_storage_elem *selem; 252 253 /* Fast path (cache hit) */ 254 sdata = rcu_dereference_check(local_storage->cache[smap->cache_idx], 255 bpf_rcu_lock_held()); 256 if (sdata && rcu_access_pointer(sdata->smap) == smap) 257 return sdata; 258 259 /* Slow path (cache miss) */ 260 hlist_for_each_entry_rcu(selem, &local_storage->list, snode, 261 rcu_read_lock_trace_held()) 262 if (rcu_access_pointer(SDATA(selem)->smap) == smap) 263 break; 264 265 if (!selem) 266 return NULL; 267 268 sdata = SDATA(selem); 269 if (cacheit_lockit) { 270 unsigned long flags; 271 272 /* spinlock is needed to avoid racing with the 273 * parallel delete. Otherwise, publishing an already 274 * deleted sdata to the cache will become a use-after-free 275 * problem in the next bpf_local_storage_lookup(). 276 */ 277 raw_spin_lock_irqsave(&local_storage->lock, flags); 278 if (selem_linked_to_storage(selem)) 279 rcu_assign_pointer(local_storage->cache[smap->cache_idx], 280 sdata); 281 raw_spin_unlock_irqrestore(&local_storage->lock, flags); 282 } 283 284 return sdata; 285 } 286 287 static int check_flags(const struct bpf_local_storage_data *old_sdata, 288 u64 map_flags) 289 { 290 if (old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_NOEXIST) 291 /* elem already exists */ 292 return -EEXIST; 293 294 if (!old_sdata && (map_flags & ~BPF_F_LOCK) == BPF_EXIST) 295 /* elem doesn't exist, cannot update it */ 296 return -ENOENT; 297 298 return 0; 299 } 300 301 int bpf_local_storage_alloc(void *owner, 302 struct bpf_local_storage_map *smap, 303 struct bpf_local_storage_elem *first_selem, 304 gfp_t gfp_flags) 305 { 306 struct bpf_local_storage *prev_storage, *storage; 307 struct bpf_local_storage **owner_storage_ptr; 308 int err; 309 310 err = mem_charge(smap, owner, sizeof(*storage)); 311 if (err) 312 return err; 313 314 storage = bpf_map_kzalloc(&smap->map, sizeof(*storage), 315 gfp_flags | __GFP_NOWARN); 316 if (!storage) { 317 err = -ENOMEM; 318 goto uncharge; 319 } 320 321 INIT_HLIST_HEAD(&storage->list); 322 raw_spin_lock_init(&storage->lock); 323 storage->owner = owner; 324 325 bpf_selem_link_storage_nolock(storage, first_selem); 326 bpf_selem_link_map(smap, first_selem); 327 328 owner_storage_ptr = 329 (struct bpf_local_storage **)owner_storage(smap, owner); 330 /* Publish storage to the owner. 331 * Instead of using any lock of the kernel object (i.e. owner), 332 * cmpxchg will work with any kernel object regardless what 333 * the running context is, bh, irq...etc. 334 * 335 * From now on, the owner->storage pointer (e.g. sk->sk_bpf_storage) 336 * is protected by the storage->lock. Hence, when freeing 337 * the owner->storage, the storage->lock must be held before 338 * setting owner->storage ptr to NULL. 339 */ 340 prev_storage = cmpxchg(owner_storage_ptr, NULL, storage); 341 if (unlikely(prev_storage)) { 342 bpf_selem_unlink_map(first_selem); 343 err = -EAGAIN; 344 goto uncharge; 345 346 /* Note that even first_selem was linked to smap's 347 * bucket->list, first_selem can be freed immediately 348 * (instead of kfree_rcu) because 349 * bpf_local_storage_map_free() does a 350 * synchronize_rcu_mult (waiting for both sleepable and 351 * normal programs) before walking the bucket->list. 352 * Hence, no one is accessing selem from the 353 * bucket->list under rcu_read_lock(). 354 */ 355 } 356 357 return 0; 358 359 uncharge: 360 kfree(storage); 361 mem_uncharge(smap, owner, sizeof(*storage)); 362 return err; 363 } 364 365 /* sk cannot be going away because it is linking new elem 366 * to sk->sk_bpf_storage. (i.e. sk->sk_refcnt cannot be 0). 367 * Otherwise, it will become a leak (and other memory issues 368 * during map destruction). 369 */ 370 struct bpf_local_storage_data * 371 bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, 372 void *value, u64 map_flags, gfp_t gfp_flags) 373 { 374 struct bpf_local_storage_data *old_sdata = NULL; 375 struct bpf_local_storage_elem *selem = NULL; 376 struct bpf_local_storage *local_storage; 377 unsigned long flags; 378 int err; 379 380 /* BPF_EXIST and BPF_NOEXIST cannot be both set */ 381 if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST) || 382 /* BPF_F_LOCK can only be used in a value with spin_lock */ 383 unlikely((map_flags & BPF_F_LOCK) && 384 !map_value_has_spin_lock(&smap->map))) 385 return ERR_PTR(-EINVAL); 386 387 if (gfp_flags == GFP_KERNEL && (map_flags & ~BPF_F_LOCK) != BPF_NOEXIST) 388 return ERR_PTR(-EINVAL); 389 390 local_storage = rcu_dereference_check(*owner_storage(smap, owner), 391 bpf_rcu_lock_held()); 392 if (!local_storage || hlist_empty(&local_storage->list)) { 393 /* Very first elem for the owner */ 394 err = check_flags(NULL, map_flags); 395 if (err) 396 return ERR_PTR(err); 397 398 selem = bpf_selem_alloc(smap, owner, value, true, gfp_flags); 399 if (!selem) 400 return ERR_PTR(-ENOMEM); 401 402 err = bpf_local_storage_alloc(owner, smap, selem, gfp_flags); 403 if (err) { 404 kfree(selem); 405 mem_uncharge(smap, owner, smap->elem_size); 406 return ERR_PTR(err); 407 } 408 409 return SDATA(selem); 410 } 411 412 if ((map_flags & BPF_F_LOCK) && !(map_flags & BPF_NOEXIST)) { 413 /* Hoping to find an old_sdata to do inline update 414 * such that it can avoid taking the local_storage->lock 415 * and changing the lists. 416 */ 417 old_sdata = 418 bpf_local_storage_lookup(local_storage, smap, false); 419 err = check_flags(old_sdata, map_flags); 420 if (err) 421 return ERR_PTR(err); 422 if (old_sdata && selem_linked_to_storage(SELEM(old_sdata))) { 423 copy_map_value_locked(&smap->map, old_sdata->data, 424 value, false); 425 return old_sdata; 426 } 427 } 428 429 if (gfp_flags == GFP_KERNEL) { 430 selem = bpf_selem_alloc(smap, owner, value, true, gfp_flags); 431 if (!selem) 432 return ERR_PTR(-ENOMEM); 433 } 434 435 raw_spin_lock_irqsave(&local_storage->lock, flags); 436 437 /* Recheck local_storage->list under local_storage->lock */ 438 if (unlikely(hlist_empty(&local_storage->list))) { 439 /* A parallel del is happening and local_storage is going 440 * away. It has just been checked before, so very 441 * unlikely. Return instead of retry to keep things 442 * simple. 443 */ 444 err = -EAGAIN; 445 goto unlock_err; 446 } 447 448 old_sdata = bpf_local_storage_lookup(local_storage, smap, false); 449 err = check_flags(old_sdata, map_flags); 450 if (err) 451 goto unlock_err; 452 453 if (old_sdata && (map_flags & BPF_F_LOCK)) { 454 copy_map_value_locked(&smap->map, old_sdata->data, value, 455 false); 456 selem = SELEM(old_sdata); 457 goto unlock; 458 } 459 460 if (gfp_flags != GFP_KERNEL) { 461 /* local_storage->lock is held. Hence, we are sure 462 * we can unlink and uncharge the old_sdata successfully 463 * later. Hence, instead of charging the new selem now 464 * and then uncharge the old selem later (which may cause 465 * a potential but unnecessary charge failure), avoid taking 466 * a charge at all here (the "!old_sdata" check) and the 467 * old_sdata will not be uncharged later during 468 * bpf_selem_unlink_storage_nolock(). 469 */ 470 selem = bpf_selem_alloc(smap, owner, value, !old_sdata, gfp_flags); 471 if (!selem) { 472 err = -ENOMEM; 473 goto unlock_err; 474 } 475 } 476 477 /* First, link the new selem to the map */ 478 bpf_selem_link_map(smap, selem); 479 480 /* Second, link (and publish) the new selem to local_storage */ 481 bpf_selem_link_storage_nolock(local_storage, selem); 482 483 /* Third, remove old selem, SELEM(old_sdata) */ 484 if (old_sdata) { 485 bpf_selem_unlink_map(SELEM(old_sdata)); 486 bpf_selem_unlink_storage_nolock(local_storage, SELEM(old_sdata), 487 false, true); 488 } 489 490 unlock: 491 raw_spin_unlock_irqrestore(&local_storage->lock, flags); 492 return SDATA(selem); 493 494 unlock_err: 495 raw_spin_unlock_irqrestore(&local_storage->lock, flags); 496 if (selem) { 497 mem_uncharge(smap, owner, smap->elem_size); 498 kfree(selem); 499 } 500 return ERR_PTR(err); 501 } 502 503 u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache) 504 { 505 u64 min_usage = U64_MAX; 506 u16 i, res = 0; 507 508 spin_lock(&cache->idx_lock); 509 510 for (i = 0; i < BPF_LOCAL_STORAGE_CACHE_SIZE; i++) { 511 if (cache->idx_usage_counts[i] < min_usage) { 512 min_usage = cache->idx_usage_counts[i]; 513 res = i; 514 515 /* Found a free cache_idx */ 516 if (!min_usage) 517 break; 518 } 519 } 520 cache->idx_usage_counts[res]++; 521 522 spin_unlock(&cache->idx_lock); 523 524 return res; 525 } 526 527 void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache, 528 u16 idx) 529 { 530 spin_lock(&cache->idx_lock); 531 cache->idx_usage_counts[idx]--; 532 spin_unlock(&cache->idx_lock); 533 } 534 535 void bpf_local_storage_map_free(struct bpf_local_storage_map *smap, 536 int __percpu *busy_counter) 537 { 538 struct bpf_local_storage_elem *selem; 539 struct bpf_local_storage_map_bucket *b; 540 unsigned int i; 541 542 /* Note that this map might be concurrently cloned from 543 * bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone 544 * RCU read section to finish before proceeding. New RCU 545 * read sections should be prevented via bpf_map_inc_not_zero. 546 */ 547 synchronize_rcu(); 548 549 /* bpf prog and the userspace can no longer access this map 550 * now. No new selem (of this map) can be added 551 * to the owner->storage or to the map bucket's list. 552 * 553 * The elem of this map can be cleaned up here 554 * or when the storage is freed e.g. 555 * by bpf_sk_storage_free() during __sk_destruct(). 556 */ 557 for (i = 0; i < (1U << smap->bucket_log); i++) { 558 b = &smap->buckets[i]; 559 560 rcu_read_lock(); 561 /* No one is adding to b->list now */ 562 while ((selem = hlist_entry_safe( 563 rcu_dereference_raw(hlist_first_rcu(&b->list)), 564 struct bpf_local_storage_elem, map_node))) { 565 if (busy_counter) { 566 migrate_disable(); 567 this_cpu_inc(*busy_counter); 568 } 569 bpf_selem_unlink(selem, false); 570 if (busy_counter) { 571 this_cpu_dec(*busy_counter); 572 migrate_enable(); 573 } 574 cond_resched_rcu(); 575 } 576 rcu_read_unlock(); 577 } 578 579 /* While freeing the storage we may still need to access the map. 580 * 581 * e.g. when bpf_sk_storage_free() has unlinked selem from the map 582 * which then made the above while((selem = ...)) loop 583 * exit immediately. 584 * 585 * However, while freeing the storage one still needs to access the 586 * smap->elem_size to do the uncharging in 587 * bpf_selem_unlink_storage_nolock(). 588 * 589 * Hence, wait another rcu grace period for the storage to be freed. 590 */ 591 synchronize_rcu(); 592 593 kvfree(smap->buckets); 594 bpf_map_area_free(smap); 595 } 596 597 int bpf_local_storage_map_alloc_check(union bpf_attr *attr) 598 { 599 if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK || 600 !(attr->map_flags & BPF_F_NO_PREALLOC) || 601 attr->max_entries || 602 attr->key_size != sizeof(int) || !attr->value_size || 603 /* Enforce BTF for userspace sk dumping */ 604 !attr->btf_key_type_id || !attr->btf_value_type_id) 605 return -EINVAL; 606 607 if (!bpf_capable()) 608 return -EPERM; 609 610 if (attr->value_size > BPF_LOCAL_STORAGE_MAX_VALUE_SIZE) 611 return -E2BIG; 612 613 return 0; 614 } 615 616 struct bpf_local_storage_map *bpf_local_storage_map_alloc(union bpf_attr *attr) 617 { 618 struct bpf_local_storage_map *smap; 619 unsigned int i; 620 u32 nbuckets; 621 622 smap = bpf_map_area_alloc(sizeof(*smap), NUMA_NO_NODE); 623 if (!smap) 624 return ERR_PTR(-ENOMEM); 625 bpf_map_init_from_attr(&smap->map, attr); 626 627 nbuckets = roundup_pow_of_two(num_possible_cpus()); 628 /* Use at least 2 buckets, select_bucket() is undefined behavior with 1 bucket */ 629 nbuckets = max_t(u32, 2, nbuckets); 630 smap->bucket_log = ilog2(nbuckets); 631 632 smap->buckets = kvcalloc(sizeof(*smap->buckets), nbuckets, 633 GFP_USER | __GFP_NOWARN | __GFP_ACCOUNT); 634 if (!smap->buckets) { 635 bpf_map_area_free(smap); 636 return ERR_PTR(-ENOMEM); 637 } 638 639 for (i = 0; i < nbuckets; i++) { 640 INIT_HLIST_HEAD(&smap->buckets[i].list); 641 raw_spin_lock_init(&smap->buckets[i].lock); 642 } 643 644 smap->elem_size = 645 sizeof(struct bpf_local_storage_elem) + attr->value_size; 646 647 return smap; 648 } 649 650 int bpf_local_storage_map_check_btf(const struct bpf_map *map, 651 const struct btf *btf, 652 const struct btf_type *key_type, 653 const struct btf_type *value_type) 654 { 655 u32 int_data; 656 657 if (BTF_INFO_KIND(key_type->info) != BTF_KIND_INT) 658 return -EINVAL; 659 660 int_data = *(u32 *)(key_type + 1); 661 if (BTF_INT_BITS(int_data) != 32 || BTF_INT_OFFSET(int_data)) 662 return -EINVAL; 663 664 return 0; 665 } 666