Skip to content

Commit fd103ff

Browse files
ameryhungMartin KaFai Lau
authored andcommitted
bpf: Convert bpf_selem_link_map to failable
To prepare for changing bpf_local_storage_map_bucket::lock to rqspinlock, convert bpf_selem_link_map() to failable. It still always succeeds and returns 0 until the change happens. No functional change. 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-4-ameryhung@gmail.com
1 parent 1b7e0ca commit fd103ff

3 files changed

Lines changed: 16 additions & 7 deletions

File tree

include/linux/bpf_local_storage.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
178178

179179
void bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool reuse_now);
180180

181-
void bpf_selem_link_map(struct bpf_local_storage_map *smap,
182-
struct bpf_local_storage *local_storage,
183-
struct bpf_local_storage_elem *selem);
181+
int bpf_selem_link_map(struct bpf_local_storage_map *smap,
182+
struct bpf_local_storage *local_storage,
183+
struct bpf_local_storage_elem *selem);
184184

185185
struct bpf_local_storage_elem *
186186
bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value,

kernel/bpf/bpf_local_storage.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,9 @@ static void bpf_selem_unlink_map_nolock(struct bpf_local_storage_elem *selem)
365365
hlist_del_init_rcu(&selem->map_node);
366366
}
367367

368-
void bpf_selem_link_map(struct bpf_local_storage_map *smap,
369-
struct bpf_local_storage *local_storage,
370-
struct bpf_local_storage_elem *selem)
368+
int bpf_selem_link_map(struct bpf_local_storage_map *smap,
369+
struct bpf_local_storage *local_storage,
370+
struct bpf_local_storage_elem *selem)
371371
{
372372
struct bpf_local_storage_map_bucket *b;
373373
unsigned long flags;
@@ -376,6 +376,8 @@ void bpf_selem_link_map(struct bpf_local_storage_map *smap,
376376
raw_spin_lock_irqsave(&b->lock, flags);
377377
hlist_add_head_rcu(&selem->map_node, &b->list);
378378
raw_spin_unlock_irqrestore(&b->lock, flags);
379+
380+
return 0;
379381
}
380382

381383
static void bpf_selem_link_map_nolock(struct bpf_local_storage_map_bucket *b,

net/core/bpf_sk_storage.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,14 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)
191191
}
192192

193193
if (new_sk_storage) {
194-
bpf_selem_link_map(smap, new_sk_storage, copy_selem);
194+
ret = bpf_selem_link_map(smap, new_sk_storage, copy_selem);
195+
if (ret) {
196+
bpf_selem_free(copy_selem, true);
197+
atomic_sub(smap->elem_size,
198+
&newsk->sk_omem_alloc);
199+
bpf_map_put(map);
200+
goto out;
201+
}
195202
bpf_selem_link_storage_nolock(new_sk_storage, copy_selem);
196203
} else {
197204
ret = bpf_local_storage_alloc(newsk, smap, copy_selem, GFP_ATOMIC);

0 commit comments

Comments
 (0)