Skip to content

Commit 1b7e0ca

Browse files
ameryhungMartin KaFai Lau
authored andcommitted
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
1 parent 0ccef70 commit 1b7e0ca

1 file changed

Lines changed: 39 additions & 18 deletions

File tree

kernel/bpf/bpf_local_storage.c

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ static bool selem_linked_to_storage(const struct bpf_local_storage_elem *selem)
6161
return !hlist_unhashed(&selem->snode);
6262
}
6363

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-
6964
static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem)
7065
{
7166
return !hlist_unhashed(&selem->map_node);
@@ -347,25 +342,27 @@ void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
347342
hlist_add_head_rcu(&selem->snode, &local_storage->list);
348343
}
349344

350-
static void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)
345+
static int bpf_selem_unlink_map(struct bpf_local_storage_elem *selem)
351346
{
352347
struct bpf_local_storage *local_storage;
353348
struct bpf_local_storage_map *smap;
354349
struct bpf_local_storage_map_bucket *b;
355350
unsigned long flags;
356351

357-
if (unlikely(!selem_linked_to_map_lockless(selem)))
358-
/* selem has already be unlinked from smap */
359-
return;
360-
361352
local_storage = rcu_dereference_check(selem->local_storage,
362353
bpf_rcu_lock_held());
363354
smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
364355
b = select_bucket(smap, local_storage);
365356
raw_spin_lock_irqsave(&b->lock, flags);
366-
if (likely(selem_linked_to_map(selem)))
367-
hlist_del_init_rcu(&selem->map_node);
357+
hlist_del_init_rcu(&selem->map_node);
368358
raw_spin_unlock_irqrestore(&b->lock, flags);
359+
360+
return 0;
361+
}
362+
363+
static void bpf_selem_unlink_map_nolock(struct bpf_local_storage_elem *selem)
364+
{
365+
hlist_del_init_rcu(&selem->map_node);
369366
}
370367

371368
void bpf_selem_link_map(struct bpf_local_storage_map *smap,
@@ -381,13 +378,24 @@ void bpf_selem_link_map(struct bpf_local_storage_map *smap,
381378
raw_spin_unlock_irqrestore(&b->lock, flags);
382379
}
383380

381+
static void bpf_selem_link_map_nolock(struct bpf_local_storage_map_bucket *b,
382+
struct bpf_local_storage_elem *selem)
383+
{
384+
hlist_add_head_rcu(&selem->map_node, &b->list);
385+
}
386+
384387
void bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool reuse_now)
385388
{
389+
int err;
390+
386391
/* Always unlink from map before unlinking from local_storage
387392
* because selem will be freed after successfully unlinked from
388393
* the local_storage.
389394
*/
390-
bpf_selem_unlink_map(selem);
395+
err = bpf_selem_unlink_map(selem);
396+
if (err)
397+
return;
398+
391399
bpf_selem_unlink_storage(selem, reuse_now);
392400
}
393401

@@ -429,6 +437,8 @@ int bpf_local_storage_alloc(void *owner,
429437
{
430438
struct bpf_local_storage *prev_storage, *storage;
431439
struct bpf_local_storage **owner_storage_ptr;
440+
struct bpf_local_storage_map_bucket *b;
441+
unsigned long flags;
432442
int err;
433443

434444
err = mem_charge(smap, owner, sizeof(*storage));
@@ -453,7 +463,10 @@ int bpf_local_storage_alloc(void *owner,
453463
storage->use_kmalloc_nolock = smap->use_kmalloc_nolock;
454464

455465
bpf_selem_link_storage_nolock(storage, first_selem);
456-
bpf_selem_link_map(smap, storage, first_selem);
466+
467+
b = select_bucket(smap, storage);
468+
raw_spin_lock_irqsave(&b->lock, flags);
469+
bpf_selem_link_map_nolock(b, first_selem);
457470

458471
owner_storage_ptr =
459472
(struct bpf_local_storage **)owner_storage(smap, owner);
@@ -469,10 +482,12 @@ int bpf_local_storage_alloc(void *owner,
469482
*/
470483
prev_storage = cmpxchg(owner_storage_ptr, NULL, storage);
471484
if (unlikely(prev_storage)) {
472-
bpf_selem_unlink_map(first_selem);
485+
bpf_selem_unlink_map_nolock(first_selem);
486+
raw_spin_unlock_irqrestore(&b->lock, flags);
473487
err = -EAGAIN;
474488
goto uncharge;
475489
}
490+
raw_spin_unlock_irqrestore(&b->lock, flags);
476491

477492
return 0;
478493

@@ -494,8 +509,9 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
494509
struct bpf_local_storage_data *old_sdata = NULL;
495510
struct bpf_local_storage_elem *alloc_selem, *selem = NULL;
496511
struct bpf_local_storage *local_storage;
512+
struct bpf_local_storage_map_bucket *b;
497513
HLIST_HEAD(old_selem_free_list);
498-
unsigned long flags;
514+
unsigned long flags, b_flags;
499515
int err;
500516

501517
/* BPF_EXIST and BPF_NOEXIST cannot be both set */
@@ -579,20 +595,25 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
579595
goto unlock;
580596
}
581597

598+
b = select_bucket(smap, local_storage);
599+
600+
raw_spin_lock_irqsave(&b->lock, b_flags);
601+
582602
alloc_selem = NULL;
583603
/* First, link the new selem to the map */
584-
bpf_selem_link_map(smap, local_storage, selem);
604+
bpf_selem_link_map_nolock(b, selem);
585605

586606
/* Second, link (and publish) the new selem to local_storage */
587607
bpf_selem_link_storage_nolock(local_storage, selem);
588608

589609
/* Third, remove old selem, SELEM(old_sdata) */
590610
if (old_sdata) {
591-
bpf_selem_unlink_map(SELEM(old_sdata));
611+
bpf_selem_unlink_map_nolock(SELEM(old_sdata));
592612
bpf_selem_unlink_storage_nolock(local_storage, SELEM(old_sdata),
593613
&old_selem_free_list);
594614
}
595615

616+
raw_spin_unlock_irqrestore(&b->lock, b_flags);
596617
unlock:
597618
raw_spin_unlock_irqrestore(&local_storage->lock, flags);
598619
bpf_selem_free_list(&old_selem_free_list, false);

0 commit comments

Comments
 (0)