BPF List
 help / color / mirror / Atom feed
* [PATCH RFC bpf-next 0/2] Switch to kmalloc_nolock() in BPF local storage
@ 2025-11-12 17:59 Amery Hung
  2025-11-12 17:59 ` [PATCH RFC bpf-next 1/2] bpf: Always charge/uncharge memory when allocating/unlinking storage elements Amery Hung
  2025-11-12 17:59 ` [PATCH RFC bpf-next 2/2] bpf: Use kmalloc_nolock() in local storage unconditionally Amery Hung
  0 siblings, 2 replies; 9+ messages in thread
From: Amery Hung @ 2025-11-12 17:59 UTC (permalink / raw)
  To: bpf
  Cc: netdev, alexei.starovoitov, andrii, daniel, martin.lau, memxor,
	kpsingh, yonghong.song, song, ameryhung, kernel-team

Hi,

This patchset tries to simplify bpf_local_storage.c by switching to
kmalloc_nolock() unconditionally. Currently, local storage adopted
BPF memory allocator in task and cgroup local storage or when PREEMPT_RT
is enabled to allow getting memory in different context without deadlock.
However, due to performance reasons socket local storage did not switch.
Using different memory allocators added a decent amount of complexity.
Therefore, to make [1] and other future work in local storage simpler,
this patchset consolidates the memory allocation/deallocation paths by
switching to kmalloc_nolock() unconditionally.
 
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 losses some when switching from kzalloc() to
kmalloc_nolock().


Socket local storage
memory alloc     batch  creation speed              creation speed diff
---------------  ----   ------------------                         ----
kzalloc           16    104.217 ± 0.974k/s  4.15 kmallocs/create
(before)          32    104.355 ± 0.606k/s  4.13 kmallocs/create
                  64    103.611 ± 0.707k/s  4.15 kmallocs/create
                  
kmalloc_nolock    16    100.566 ± 0.560k/s  1.13 kmallocs/create  -3.5%
(after)           32     99.708 ± 0.684k/s  1.15 kmallocs/create  -4.5%
                  64     98.375 ± 1.757k/s  1.13 kmallocs/create  -5.1%
                   
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.399 ± 0.142k/s  2.51 kmallocs/create  +3.0%
(after)           32     23.495 ± 1.285k/s  2.66 kmallocs/create  +2.6%
                  64     23.701 ± 0.207k/s  2.63 kmallocs/create  +5.1%

[1] https://lore.kernel.org/bpf/20251002225356.1505480-1-ameryhung@gmail.com/

---

Amery Hung (2):
  bpf: Always charge/uncharge memory when allocating/unlinking storage
    elements
  bpf: Use kmalloc_nolock() in local storage unconditionally

 include/linux/bpf_local_storage.h |  12 +-
 kernel/bpf/bpf_cgrp_storage.c     |   2 +-
 kernel/bpf/bpf_inode_storage.c    |   2 +-
 kernel/bpf/bpf_local_storage.c    | 283 +++++-------------------------
 kernel/bpf/bpf_task_storage.c     |   2 +-
 net/core/bpf_sk_storage.c         |   6 +-
 6 files changed, 53 insertions(+), 254 deletions(-)

-- 
2.47.3


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2025-11-13 18:59 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-12 17:59 [PATCH RFC bpf-next 0/2] Switch to kmalloc_nolock() in BPF local storage Amery Hung
2025-11-12 17:59 ` [PATCH RFC bpf-next 1/2] bpf: Always charge/uncharge memory when allocating/unlinking storage elements Amery Hung
2025-11-12 17:59 ` [PATCH RFC bpf-next 2/2] bpf: Use kmalloc_nolock() in local storage unconditionally Amery Hung
2025-11-12 19:35   ` Alexei Starovoitov
2025-11-12 19:51     ` Amery Hung
2025-11-12 20:04       ` Alexei Starovoitov
2025-11-12 21:14         ` Amery Hung
2025-11-13  1:08           ` Alexei Starovoitov
2025-11-13 18:58             ` Amery Hung

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox