Skip to content

Commit bbc6be0

Browse files
committed
bpf: Use kmalloc_nolock() in local storage unconditionally
Simplify local storage and selem memory alloc/free paths by switching to kmalloc_nolock unconditionally. While it is okay to call bpf_obj_free_fields() immediately in bpf_selem_free() when reuse_now == true, it is kept only in bpf_selem_free_rcu() to simplify the code. This requires rcu_barrier() to be called unconditionally in bpf_local_storage_map_free() since bpf_obj_free_fields() may also be called from rcu callbacks. In addition, remove the argument, smap, from bpf_selem_free() and rely on SDATA(selem)->smap for bpf_obj_free_fields(). This requires initializing SDATA(selem)->smap earlier during bpf_selem_alloc() as bpf_local_storage_update() can allocate an selem and free it without ever linking it to a map. Finally, clean up and update comments. Note that, we already free selem after an RCU grace period in bpf_local_storage_update() when bpf_local_storage_alloc() failed the cmpxchg since commit c0d63f3 ("bpf: Add bpf_selem_free()"). Signed-off-by: Amery Hung <[email protected]>
1 parent 009600b commit bbc6be0

File tree

6 files changed

+41
-240
lines changed

6 files changed

+41
-240
lines changed

include/linux/bpf_local_storage.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ struct bpf_local_storage_map {
5353
u32 bucket_log;
5454
u16 elem_size;
5555
u16 cache_idx;
56-
struct bpf_mem_alloc selem_ma;
57-
struct bpf_mem_alloc storage_ma;
58-
bool bpf_ma;
5956
};
6057

6158
struct bpf_local_storage_data {
@@ -129,8 +126,7 @@ int bpf_local_storage_map_alloc_check(union bpf_attr *attr);
129126

130127
struct bpf_map *
131128
bpf_local_storage_map_alloc(union bpf_attr *attr,
132-
struct bpf_local_storage_cache *cache,
133-
bool bpf_ma);
129+
struct bpf_local_storage_cache *cache);
134130

135131
void __bpf_local_storage_insert_cache(struct bpf_local_storage *local_storage,
136132
struct bpf_local_storage_map *smap,
@@ -186,9 +182,7 @@ struct bpf_local_storage_elem *
186182
bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value,
187183
bool swap_uptrs, gfp_t gfp_flags);
188184

189-
void bpf_selem_free(struct bpf_local_storage_elem *selem,
190-
struct bpf_local_storage_map *smap,
191-
bool reuse_now);
185+
void bpf_selem_free(struct bpf_local_storage_elem *selem, bool reuse_now);
192186

193187
int
194188
bpf_local_storage_alloc(void *owner,

kernel/bpf/bpf_cgrp_storage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static int notsupp_get_next_key(struct bpf_map *map, void *key, void *next_key)
146146

147147
static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
148148
{
149-
return bpf_local_storage_map_alloc(attr, &cgroup_cache, true);
149+
return bpf_local_storage_map_alloc(attr, &cgroup_cache);
150150
}
151151

152152
static void cgroup_storage_map_free(struct bpf_map *map)

kernel/bpf/bpf_inode_storage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static int notsupp_get_next_key(struct bpf_map *map, void *key,
181181

182182
static struct bpf_map *inode_storage_map_alloc(union bpf_attr *attr)
183183
{
184-
return bpf_local_storage_map_alloc(attr, &inode_cache, false);
184+
return bpf_local_storage_map_alloc(attr, &inode_cache);
185185
}
186186

187187
static void inode_storage_map_free(struct bpf_map *map)

0 commit comments

Comments
 (0)