Skip to content

Commit 0be0838

Browse files
ameryhungMartin KaFai Lau
authored andcommitted
bpf: Switch to bpf_selem_unlink_nofail in bpf_local_storage_{map_free, destroy}
Take care of rqspinlock error in bpf_local_storage_{map_free, destroy}() properly by switching to bpf_selem_unlink_nofail(). Both functions iterate their own RCU-protected list of selems and call bpf_selem_unlink_nofail(). In map_free(), to prevent infinite loop when both map_free() and destroy() fail to remove a selem from b->list (extremely unlikely), switch to hlist_for_each_entry_rcu(). In destroy(), also switch to hlist_for_each_entry_rcu() since we no longer iterate local_storage->list under local_storage->lock. bpf_selem_unlink() now becomes dedicated to helpers and syscalls paths so reuse_now should always be false. Remove it from the argument and hardcode it. Acked-by: Alexei Starovoitov <ast@kernel.org> Co-developed-by: Martin KaFai Lau <martin.lau@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-12-ameryhung@gmail.com
1 parent 5d800f8 commit 0be0838

6 files changed

Lines changed: 39 additions & 41 deletions

File tree

include/linux/bpf_local_storage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
171171
return SDATA(selem);
172172
}
173173

174-
void bpf_local_storage_destroy(struct bpf_local_storage *local_storage);
174+
u32 bpf_local_storage_destroy(struct bpf_local_storage *local_storage);
175175

176176
void bpf_local_storage_map_free(struct bpf_map *map,
177177
struct bpf_local_storage_cache *cache);
@@ -184,7 +184,7 @@ int bpf_local_storage_map_check_btf(const struct bpf_map *map,
184184
void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
185185
struct bpf_local_storage_elem *selem);
186186

187-
int bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool reuse_now);
187+
int bpf_selem_unlink(struct bpf_local_storage_elem *selem);
188188

189189
int bpf_selem_link_map(struct bpf_local_storage_map *smap,
190190
struct bpf_local_storage *local_storage,

kernel/bpf/bpf_cgrp_storage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static int cgroup_storage_delete(struct cgroup *cgroup, struct bpf_map *map)
8989
if (!sdata)
9090
return -ENOENT;
9191

92-
return bpf_selem_unlink(SELEM(sdata), false);
92+
return bpf_selem_unlink(SELEM(sdata));
9393
}
9494

9595
static long bpf_cgrp_storage_delete_elem(struct bpf_map *map, void *key)

kernel/bpf/bpf_inode_storage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ static int inode_storage_delete(struct inode *inode, struct bpf_map *map)
110110
if (!sdata)
111111
return -ENOENT;
112112

113-
return bpf_selem_unlink(SELEM(sdata), false);
113+
return bpf_selem_unlink(SELEM(sdata));
114114
}
115115

116116
static long bpf_fd_inode_storage_delete_elem(struct bpf_map *map, void *key)

kernel/bpf/bpf_local_storage.c

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,11 @@ static void bpf_selem_link_map_nolock(struct bpf_local_storage_map_bucket *b,
377377
hlist_add_head_rcu(&selem->map_node, &b->list);
378378
}
379379

380-
int bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool reuse_now)
380+
/*
381+
* Unlink an selem from map and local storage with lock held.
382+
* This is the common path used by local storages to delete an selem.
383+
*/
384+
int bpf_selem_unlink(struct bpf_local_storage_elem *selem)
381385
{
382386
struct bpf_local_storage *local_storage;
383387
bool free_local_storage = false;
@@ -411,10 +415,10 @@ int bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool reuse_now)
411415
out:
412416
raw_res_spin_unlock_irqrestore(&local_storage->lock, flags);
413417

414-
bpf_selem_free_list(&selem_free_list, reuse_now);
418+
bpf_selem_free_list(&selem_free_list, false);
415419

416420
if (free_local_storage)
417-
bpf_local_storage_free(local_storage, reuse_now);
421+
bpf_local_storage_free(local_storage, false);
418422

419423
return err;
420424
}
@@ -804,13 +808,13 @@ int bpf_local_storage_map_check_btf(const struct bpf_map *map,
804808
return 0;
805809
}
806810

807-
void bpf_local_storage_destroy(struct bpf_local_storage *local_storage)
811+
/*
812+
* Destroy local storage when the owner is going away. Caller must uncharge memory
813+
* if memory charging is used.
814+
*/
815+
u32 bpf_local_storage_destroy(struct bpf_local_storage *local_storage)
808816
{
809817
struct bpf_local_storage_elem *selem;
810-
bool free_storage = false;
811-
HLIST_HEAD(free_selem_list);
812-
struct hlist_node *n;
813-
unsigned long flags;
814818

815819
/* Neither the bpf_prog nor the bpf_map's syscall
816820
* could be modifying the local_storage->list now.
@@ -821,32 +825,20 @@ void bpf_local_storage_destroy(struct bpf_local_storage *local_storage)
821825
* when unlinking elem from the local_storage->list and
822826
* the map's bucket->list.
823827
*/
824-
raw_res_spin_lock_irqsave(&local_storage->lock, flags);
825-
hlist_for_each_entry_safe(selem, n, &local_storage->list, snode) {
826-
/* Always unlink from map before unlinking from
827-
* local_storage.
828-
*/
829-
bpf_selem_unlink_map(selem);
830-
/* If local_storage list has only one element, the
831-
* bpf_selem_unlink_storage_nolock() will return true.
832-
* Otherwise, it will return false. The current loop iteration
833-
* intends to remove all local storage. So the last iteration
834-
* of the loop will set the free_cgroup_storage to true.
835-
*/
836-
free_storage = bpf_selem_unlink_storage_nolock(
837-
local_storage, selem, &free_selem_list);
838-
}
839-
raw_res_spin_unlock_irqrestore(&local_storage->lock, flags);
840-
841-
bpf_selem_free_list(&free_selem_list, true);
842-
843-
if (free_storage)
844-
bpf_local_storage_free(local_storage, true);
828+
hlist_for_each_entry_rcu(selem, &local_storage->list, snode)
829+
bpf_selem_unlink_nofail(selem, NULL);
845830

846831
if (!refcount_dec_and_test(&local_storage->owner_refcnt)) {
847832
while (refcount_read(&local_storage->owner_refcnt))
848833
cpu_relax();
834+
/*
835+
* Paired with refcount_dec() in bpf_selem_unlink_nofail()
836+
* to make sure destroy() sees the correct local_storage->mem_charge.
837+
*/
838+
smp_mb();
849839
}
840+
841+
return local_storage->mem_charge;
850842
}
851843

852844
u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map)
@@ -940,11 +932,14 @@ void bpf_local_storage_map_free(struct bpf_map *map,
940932

941933
rcu_read_lock();
942934
/* No one is adding to b->list now */
943-
while ((selem = hlist_entry_safe(
944-
rcu_dereference_raw(hlist_first_rcu(&b->list)),
945-
struct bpf_local_storage_elem, map_node))) {
946-
bpf_selem_unlink(selem, true);
947-
cond_resched_rcu();
935+
restart:
936+
hlist_for_each_entry_rcu(selem, &b->list, map_node) {
937+
bpf_selem_unlink_nofail(selem, b);
938+
939+
if (need_resched()) {
940+
cond_resched_rcu();
941+
goto restart;
942+
}
948943
}
949944
rcu_read_unlock();
950945
}

kernel/bpf/bpf_task_storage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static int task_storage_delete(struct task_struct *task, struct bpf_map *map)
134134
if (!sdata)
135135
return -ENOENT;
136136

137-
return bpf_selem_unlink(SELEM(sdata), false);
137+
return bpf_selem_unlink(SELEM(sdata));
138138
}
139139

140140
static long bpf_pid_task_storage_delete_elem(struct bpf_map *map, void *key)

net/core/bpf_sk_storage.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,23 @@ static int bpf_sk_storage_del(struct sock *sk, struct bpf_map *map)
4040
if (!sdata)
4141
return -ENOENT;
4242

43-
return bpf_selem_unlink(SELEM(sdata), false);
43+
return bpf_selem_unlink(SELEM(sdata));
4444
}
4545

4646
/* Called by __sk_destruct() & bpf_sk_storage_clone() */
4747
void bpf_sk_storage_free(struct sock *sk)
4848
{
4949
struct bpf_local_storage *sk_storage;
50+
u32 uncharge;
5051

5152
rcu_read_lock_dont_migrate();
5253
sk_storage = rcu_dereference(sk->sk_bpf_storage);
5354
if (!sk_storage)
5455
goto out;
5556

56-
bpf_local_storage_destroy(sk_storage);
57+
uncharge = bpf_local_storage_destroy(sk_storage);
58+
if (uncharge)
59+
atomic_sub(uncharge, &sk->sk_omem_alloc);
5760
out:
5861
rcu_read_unlock_migrate();
5962
}

0 commit comments

Comments
 (0)