BPF List
 help / color / mirror / Atom feed
From: Amery Hung <ameryhung@gmail.com>
To: bpf@vger.kernel.org
Cc: netdev@vger.kernel.org, alexei.starovoitov@gmail.com,
	andrii@kernel.org, daniel@iogearbox.net, martin.lau@kernel.org,
	memxor@gmail.com, kpsingh@kernel.org, yonghong.song@linux.dev,
	song@kernel.org, ameryhung@gmail.com, kernel-team@meta.com
Subject: [PATCH RFC bpf-next 0/2] Switch to kmalloc_nolock() in BPF local storage
Date: Wed, 12 Nov 2025 09:59:34 -0800	[thread overview]
Message-ID: <20251112175939.2365295-1-ameryhung@gmail.com> (raw)

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


             reply	other threads:[~2025-11-12 17:59 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-12 17:59 Amery Hung [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251112175939.2365295-1-ameryhung@gmail.com \
    --to=ameryhung@gmail.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@meta.com \
    --cc=kpsingh@kernel.org \
    --cc=martin.lau@kernel.org \
    --cc=memxor@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox