Skip to content

Commit d6ec090

Browse files
author
Alexei Starovoitov
committed
Merge branch 'replace-bpf-memory-allocator-with-kmalloc_nolock-in-local-storage'
Amery Hung says: ==================== Replace BPF memory allocator with kmalloc_nolock() in local storage This patchset tries to simplify bpf_local_storage.c by adopting kmalloc_nolock(). This removes memory preallocation and reduces the dependency of smap in bpf_selem_free() and bpf_local_storage_free(). The later will simplify a future refactor that replaces local_storage->lock and b->lock [1]. RFC v1 tried to switch to kmalloc_nolock() unconditionally. However, as there is substantial performance loss in socket local storage due to 1) defer_free() in kfree_nolock() and 2) no kfree_rcu() batching, replacing kzalloc() is postponed until necessary improvements in mm land. Benchmark ./bench -p 1 local-storage-create --storage-type <socket,task> \ --batch-size <16,32,64> The benchmark is a microbenchmark stress-testing how fast local storage can be created. For task local storage, switching from BPF memory allocator to kmalloc_nolock() yields a small amount of improvement. For socket local storage, it remains roughly the same as nothing has changed. Socket local storage memory alloc batch creation speed creation speed diff --------------- ---- ------------------ ---- kzalloc 16 144.149 ± 0.642k/s 3.10 kmallocs/create (before) 32 144.379 ± 1.070k/s 3.08 kmallocs/create 64 144.491 ± 0.818k/s 3.13 kmallocs/create kzalloc 16 146.180 ± 1.403k/s 3.10 kmallocs/create +1.4% (not changed) 32 146.245 ± 1.272k/s 3.10 kmallocs/create +1.3% 64 145.012 ± 1.545k/s 3.10 kmallocs/create +0.4% Task local storage memory alloc batch creation speed creation speed diff --------------- ---- ------------------ ---- BPF memory 16 24.668 ± 0.121k/s 2.54 kmallocs/create allocator 32 22.899 ± 0.097k/s 2.67 kmallocs/create (before) 64 22.559 ± 0.076k/s 2.56 kmallocs/create kmalloc_nolock 16 25.796 ± 0.059k/s 2.52 kmallocs/create +4.6% (after) 32 23.412 ± 0.069k/s 2.50 kmallocs/create +2.2% 64 23.717 ± 0.108k/s 2.60 kmallocs/create +5.1% [1] https://lore.kernel.org/bpf/[email protected]/ v1 -> v2 - Only replace BPF memory allocator with kmalloc_nolock() Link: https://lore.kernel.org/bpf/[email protected]/ ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents ec12ab2 + f484f4a commit d6ec090

File tree

3 files changed

+74
-175
lines changed

3 files changed

+74
-175
lines changed

include/linux/bpf_local_storage.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ 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;
56+
bool use_kmalloc_nolock;
5957
};
6058

6159
struct bpf_local_storage_data {
@@ -97,6 +95,7 @@ struct bpf_local_storage {
9795
*/
9896
struct rcu_head rcu;
9997
raw_spinlock_t lock; /* Protect adding/removing from the "list" */
98+
bool use_kmalloc_nolock;
10099
};
101100

102101
/* U16_MAX is much more than enough for sk local storage
@@ -130,7 +129,7 @@ int bpf_local_storage_map_alloc_check(union bpf_attr *attr);
130129
struct bpf_map *
131130
bpf_local_storage_map_alloc(union bpf_attr *attr,
132131
struct bpf_local_storage_cache *cache,
133-
bool bpf_ma);
132+
bool use_kmalloc_nolock);
134133

135134
void __bpf_local_storage_insert_cache(struct bpf_local_storage *local_storage,
136135
struct bpf_local_storage_map *smap,
@@ -184,10 +183,9 @@ void bpf_selem_link_map(struct bpf_local_storage_map *smap,
184183

185184
struct bpf_local_storage_elem *
186185
bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value,
187-
bool charge_mem, bool swap_uptrs, gfp_t gfp_flags);
186+
bool swap_uptrs, gfp_t gfp_flags);
188187

189188
void bpf_selem_free(struct bpf_local_storage_elem *selem,
190-
struct bpf_local_storage_map *smap,
191189
bool reuse_now);
192190

193191
int

0 commit comments

Comments
 (0)