Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

bpf: Convert bpf_selem_unlink_map to failable

To prepare for changing bpf_local_storage_map_bucket::lock to rqspinlock,
convert bpf_selem_unlink_map() to failable. It still always succeeds and
returns 0 for now.

Since some operations updating local storage cannot fail in the middle,
open-code bpf_selem_unlink_map() to take the b->lock before the
operation. There are two such locations:

- bpf_local_storage_alloc()

The first selem will be unlinked from smap if cmpxchg owner_storage_ptr
fails, which should not fail. Therefore, hold b->lock when linking
until allocation complete. Helpers that assume b->lock is held by
callers are introduced: bpf_selem_link_map_nolock() and
bpf_selem_unlink_map_nolock().

- bpf_local_storage_update()

The three step update process: link_map(new_selem),
link_storage(new_selem), and unlink_map(old_selem) should not fail in
the middle.

In bpf_selem_unlink(), bpf_selem_unlink_map() and
bpf_selem_unlink_storage() should either all succeed or fail as a whole
instead of failing in the middle. So, return if unlink_map() failed.
Remove the selem_linked_to_map_lockless() check as an selem in the
common paths (not bpf_local_storage_map_free() or
bpf_local_storage_destroy()), will be unlinked under b->lock and
local_storage->lock and therefore no other threads can unlink the selem
from map at the same time.

In bpf_local_storage_destroy(), ignore the return of
bpf_selem_unlink_map() for now. A later patch will allow
bpf_local_storage_destroy() to unlink selems even when failing to
acquire locks.

Note that while this patch removes all callers of selem_linked_to_map(),
a later patch that introduces bpf_selem_unlink_nofail() will use it
again.

Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Amery Hung <ameryhung@gmail.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://patch.msgid.link/20260205222916.1788211-3-ameryhung@gmail.com

authored by

Amery Hung and committed by
Martin KaFai Lau
1b7e0cae 0ccef707

+39 -18
+39 -18
kernel/bpf/bpf_local_storage.c
··· 61 61 return !hlist_unhashed(&selem->snode); 62 62 } 63 63 64 - static bool selem_linked_to_map_lockless(const struct bpf_local_storage_elem *selem) 65 - { 66 - return !hlist_unhashed_lockless(&selem->map_node); 67 - } 68 - 69 64 static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem) 70 65 { 71 66 return !hlist_unhashed(&selem->map_node); ··· 342 347 hlist_add_head_rcu(&selem->snode, &local_storage->list); 343 348 } 344 349 345 - static void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem) 350 + static int bpf_selem_unlink_map(struct bpf_local_storage_elem *selem) 346 351 { 347 352 struct bpf_local_storage *local_storage; 348 353 struct bpf_local_storage_map *smap; 349 354 struct bpf_local_storage_map_bucket *b; 350 355 unsigned long flags; 351 356 352 - if (unlikely(!selem_linked_to_map_lockless(selem))) 353 - /* selem has already be unlinked from smap */ 354 - return; 355 - 356 357 local_storage = rcu_dereference_check(selem->local_storage, 357 358 bpf_rcu_lock_held()); 358 359 smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held()); 359 360 b = select_bucket(smap, local_storage); 360 361 raw_spin_lock_irqsave(&b->lock, flags); 361 - if (likely(selem_linked_to_map(selem))) 362 - hlist_del_init_rcu(&selem->map_node); 362 + hlist_del_init_rcu(&selem->map_node); 363 363 raw_spin_unlock_irqrestore(&b->lock, flags); 364 + 365 + return 0; 366 + } 367 + 368 + static void bpf_selem_unlink_map_nolock(struct bpf_local_storage_elem *selem) 369 + { 370 + hlist_del_init_rcu(&selem->map_node); 364 371 } 365 372 366 373 void bpf_selem_link_map(struct bpf_local_storage_map *smap, ··· 378 381 raw_spin_unlock_irqrestore(&b->lock, flags); 379 382 } 380 383 384 + static void bpf_selem_link_map_nolock(struct bpf_local_storage_map_bucket *b, 385 + struct bpf_local_storage_elem *selem) 386 + { 387 + hlist_add_head_rcu(&selem->map_node, &b->list); 388 + } 389 + 381 390 void bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool reuse_now) 382 391 { 392 + int err; 393 + 383 394 /* Always unlink from map before unlinking from local_storage 384 395 * because selem will be freed after successfully unlinked from 385 396 * the local_storage. 386 397 */ 387 - bpf_selem_unlink_map(selem); 398 + err = bpf_selem_unlink_map(selem); 399 + if (err) 400 + return; 401 + 388 402 bpf_selem_unlink_storage(selem, reuse_now); 389 403 } 390 404 ··· 437 429 { 438 430 struct bpf_local_storage *prev_storage, *storage; 439 431 struct bpf_local_storage **owner_storage_ptr; 432 + struct bpf_local_storage_map_bucket *b; 433 + unsigned long flags; 440 434 int err; 441 435 442 436 err = mem_charge(smap, owner, sizeof(*storage)); ··· 463 453 storage->use_kmalloc_nolock = smap->use_kmalloc_nolock; 464 454 465 455 bpf_selem_link_storage_nolock(storage, first_selem); 466 - bpf_selem_link_map(smap, storage, first_selem); 456 + 457 + b = select_bucket(smap, storage); 458 + raw_spin_lock_irqsave(&b->lock, flags); 459 + bpf_selem_link_map_nolock(b, first_selem); 467 460 468 461 owner_storage_ptr = 469 462 (struct bpf_local_storage **)owner_storage(smap, owner); ··· 482 469 */ 483 470 prev_storage = cmpxchg(owner_storage_ptr, NULL, storage); 484 471 if (unlikely(prev_storage)) { 485 - bpf_selem_unlink_map(first_selem); 472 + bpf_selem_unlink_map_nolock(first_selem); 473 + raw_spin_unlock_irqrestore(&b->lock, flags); 486 474 err = -EAGAIN; 487 475 goto uncharge; 488 476 } 477 + raw_spin_unlock_irqrestore(&b->lock, flags); 489 478 490 479 return 0; 491 480 ··· 509 494 struct bpf_local_storage_data *old_sdata = NULL; 510 495 struct bpf_local_storage_elem *alloc_selem, *selem = NULL; 511 496 struct bpf_local_storage *local_storage; 497 + struct bpf_local_storage_map_bucket *b; 512 498 HLIST_HEAD(old_selem_free_list); 513 - unsigned long flags; 499 + unsigned long flags, b_flags; 514 500 int err; 515 501 516 502 /* BPF_EXIST and BPF_NOEXIST cannot be both set */ ··· 595 579 goto unlock; 596 580 } 597 581 582 + b = select_bucket(smap, local_storage); 583 + 584 + raw_spin_lock_irqsave(&b->lock, b_flags); 585 + 598 586 alloc_selem = NULL; 599 587 /* First, link the new selem to the map */ 600 - bpf_selem_link_map(smap, local_storage, selem); 588 + bpf_selem_link_map_nolock(b, selem); 601 589 602 590 /* Second, link (and publish) the new selem to local_storage */ 603 591 bpf_selem_link_storage_nolock(local_storage, selem); 604 592 605 593 /* Third, remove old selem, SELEM(old_sdata) */ 606 594 if (old_sdata) { 607 - bpf_selem_unlink_map(SELEM(old_sdata)); 595 + bpf_selem_unlink_map_nolock(SELEM(old_sdata)); 608 596 bpf_selem_unlink_storage_nolock(local_storage, SELEM(old_sdata), 609 597 &old_selem_free_list); 610 598 } 611 599 600 + raw_spin_unlock_irqrestore(&b->lock, b_flags); 612 601 unlock: 613 602 raw_spin_unlock_irqrestore(&local_storage->lock, flags); 614 603 bpf_selem_free_list(&old_selem_free_list, false);