Commit c0d63f30 authored by Martin KaFai Lau's avatar Martin KaFai Lau Committed by Alexei Starovoitov

bpf: Add bpf_selem_free()

This patch refactors the selem freeing logic into bpf_selem_free().
It is a preparation work for a later patch using
bpf_mem_cache_alloc/free. The other kfree(selem) cases
are also changed to bpf_selem_free(..., reuse_now = true).
Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
Link: https://lore.kernel.org/r/20230308065936.1550103-10-martin.lau@linux.devSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent f8ccf30c
...@@ -152,6 +152,10 @@ struct bpf_local_storage_elem * ...@@ -152,6 +152,10 @@ struct bpf_local_storage_elem *
bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value, bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value,
bool charge_mem, gfp_t gfp_flags); bool charge_mem, gfp_t gfp_flags);
void bpf_selem_free(struct bpf_local_storage_elem *selem,
struct bpf_local_storage_map *smap,
bool reuse_now);
int int
bpf_local_storage_alloc(void *owner, bpf_local_storage_alloc(void *owner,
struct bpf_local_storage_map *smap, struct bpf_local_storage_map *smap,
......
...@@ -125,6 +125,17 @@ static void bpf_selem_free_trace_rcu(struct rcu_head *rcu) ...@@ -125,6 +125,17 @@ static void bpf_selem_free_trace_rcu(struct rcu_head *rcu)
call_rcu(rcu, bpf_selem_free_rcu); call_rcu(rcu, bpf_selem_free_rcu);
} }
void bpf_selem_free(struct bpf_local_storage_elem *selem,
struct bpf_local_storage_map *smap,
bool reuse_now)
{
bpf_obj_free_fields(smap->map.record, SDATA(selem)->data);
if (!reuse_now)
call_rcu_tasks_trace(&selem->rcu, bpf_selem_free_trace_rcu);
else
call_rcu(&selem->rcu, bpf_selem_free_rcu);
}
/* local_storage->lock must be held and selem->local_storage == local_storage. /* local_storage->lock must be held and selem->local_storage == local_storage.
* The caller must ensure selem->smap is still valid to be * The caller must ensure selem->smap is still valid to be
* dereferenced for its smap->elem_size and smap->cache_idx. * dereferenced for its smap->elem_size and smap->cache_idx.
...@@ -175,11 +186,7 @@ static bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_stor ...@@ -175,11 +186,7 @@ static bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_stor
SDATA(selem)) SDATA(selem))
RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL); RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL);
bpf_obj_free_fields(smap->map.record, SDATA(selem)->data); bpf_selem_free(selem, smap, reuse_now);
if (!reuse_now)
call_rcu_tasks_trace(&selem->rcu, bpf_selem_free_trace_rcu);
else
call_rcu(&selem->rcu, bpf_selem_free_rcu);
if (rcu_access_pointer(local_storage->smap) == smap) if (rcu_access_pointer(local_storage->smap) == smap)
RCU_INIT_POINTER(local_storage->smap, NULL); RCU_INIT_POINTER(local_storage->smap, NULL);
...@@ -423,7 +430,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, ...@@ -423,7 +430,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
err = bpf_local_storage_alloc(owner, smap, selem, gfp_flags); err = bpf_local_storage_alloc(owner, smap, selem, gfp_flags);
if (err) { if (err) {
kfree(selem); bpf_selem_free(selem, smap, true);
mem_uncharge(smap, owner, smap->elem_size); mem_uncharge(smap, owner, smap->elem_size);
return ERR_PTR(err); return ERR_PTR(err);
} }
...@@ -517,7 +524,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, ...@@ -517,7 +524,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
raw_spin_unlock_irqrestore(&local_storage->lock, flags); raw_spin_unlock_irqrestore(&local_storage->lock, flags);
if (selem) { if (selem) {
mem_uncharge(smap, owner, smap->elem_size); mem_uncharge(smap, owner, smap->elem_size);
kfree(selem); bpf_selem_free(selem, smap, true);
} }
return ERR_PTR(err); return ERR_PTR(err);
} }
......
...@@ -197,7 +197,7 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk) ...@@ -197,7 +197,7 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)
} else { } else {
ret = bpf_local_storage_alloc(newsk, smap, copy_selem, GFP_ATOMIC); ret = bpf_local_storage_alloc(newsk, smap, copy_selem, GFP_ATOMIC);
if (ret) { if (ret) {
kfree(copy_selem); bpf_selem_free(copy_selem, smap, true);
atomic_sub(smap->elem_size, atomic_sub(smap->elem_size,
&newsk->sk_omem_alloc); &newsk->sk_omem_alloc);
bpf_map_put(map); bpf_map_put(map);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment