All of lore.kernel.org
 help / color / mirror / Atom feed
From: Emil Tsalapatis <emil@etsalapatis.com>
To: bpf@vger.kernel.org
Cc: ast@kernel.org, andrii@kernel.org, eddyz87@gmail.com,
	memxor@gmail.com, daniel@iogearbox.net,
	Emil Tsalapatis <emil@etsalapatis.com>
Subject: [PATCH bpf-next v1 0/4] bpf: Use kmalloc for local storage if possible
Date: Tue, 24 Nov 2026 06:03:53 +0000	[thread overview]
Message-ID: <20261124060357.11700-1-emil@etsalapatis.com> (raw)

Local storage currently only goes through kmalloc_nolock() to allocate
new memory. This is needlessly restrictive, because the calling context
may allow for spinning on the zone lock, or even sleeping. Exclusively
using kmalloc_nolock() has the downside that the call may fail where a
different variant would succeed, e.g., under heavy contention on the
ZONE_NORMAL zone lock combined with high hugepage usage in the system
draining ZONE_DMA32 below the watermark where a ZONE_NORMAL allocation
can allocate from it. This scenario has been observed in production
environments.

Add more granular context checking during local storage allocation, and
do either a GFP_KERNEL kmalloc sleepable allocation, a GFP_NOWAIT
nonsleepable kmalloc allocation, or a kmalloc_nolock() allocation. This
requires plumbing the aux metadata of the program through the local
data allocation helpers, and inspecting at runtime the context's
preemption/nmi state along with the sleepability of the call.

The logic is effectively:

  if (aux && bpf_prog_can_sleep(aux->prog)) {
          ptr = kzalloc(size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  } else if (IS_ENABLED(CONFIG_PREEMPT_COUNT) && can_spin_in_alloc() &&
             size <= KMALLOC_MAX_CACHE_SIZE) {
          ptr = kmalloc(size, GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_ZERO);
          if (!ptr)
                  ptr = kmalloc_nolock(size, __GFP_ZERO, NUMA_NO_NODE);
  } else {
          ptr = kmalloc_nolock(size, __GFP_ZERO, NUMA_NO_NODE);
  }

Signed-off-by: Emil Tsalapatis <emil@etsalapatis.com>

Emil Tsalapatis (4):
  bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
  bpf: Plumb local storage context
  bpf: Add sleepable local storage
  selftests/bpf: Test kmalloc variant selection for task storage

 include/linux/bpf.h                           |   8 ++
 include/linux/bpf_local_storage.h             |   3 +-
 kernel/bpf/bpf_cgrp_storage.c                 |   9 +-
 kernel/bpf/bpf_inode_storage.c                |   9 +-
 kernel/bpf/bpf_local_storage.c                |  53 ++++++--
 kernel/bpf/bpf_task_storage.c                 |   9 +-
 kernel/bpf/syscall.c                          |  27 +++-
 net/core/bpf_sk_storage.c                     |  17 ++-
 .../bpf/prog_tests/task_storage_sleepable.c   |  94 +++++++++++++
 .../bpf/progs/task_storage_sleepable.c        | 126 ++++++++++++++++++
 10 files changed, 319 insertions(+), 36 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/task_storage_sleepable.c
 create mode 100644 tools/testing/selftests/bpf/progs/task_storage_sleepable.c

-- 
2.52.1


             reply	other threads:[~2026-11-24  6:04 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  6:03 Emil Tsalapatis [this message]
2026-11-24  6:03 ` [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time Emil Tsalapatis
2026-11-24  6:21   ` sashiko-bot
2026-11-24  6:40   ` Alexei Starovoitov
2026-11-24 13:59     ` Harry Yoo
2026-11-24 18:07       ` Emil Tsalapatis
2026-11-25  4:03       ` Alexei Starovoitov
2026-11-25 14:09       ` Peter Zijlstra
2026-11-25 14:30         ` Harry Yoo
2026-11-25 16:04           ` Peter Zijlstra
2026-11-26  1:07             ` Alexei Starovoitov
2026-11-26  1:04         ` Alexei Starovoitov
2026-11-26  9:44           ` Peter Zijlstra
2026-11-26  9:46             ` David Hildenbrand (Arm)
2026-11-26 20:40               ` Alexei Starovoitov
2026-11-26 20:45                 ` David Hildenbrand (Arm)
2026-11-26 21:04                   ` Alexei Starovoitov
2026-11-26 20:39             ` Alexei Starovoitov
2026-11-24  6:40   ` bot+bpf-ci
2026-11-24  6:03 ` [PATCH bpf-next v1 2/4] bpf: Plumb local storage context Emil Tsalapatis
2026-11-24  6:17   ` sashiko-bot
2026-11-24  6:03 ` [PATCH bpf-next v1 3/4] bpf: Add sleepable local storage Emil Tsalapatis
2026-11-24  6:40   ` bot+bpf-ci
2026-11-24  6:41   ` sashiko-bot
2026-11-24  6:03 ` [PATCH bpf-next v1 4/4] selftests/bpf: Test kmalloc variant selection for task storage Emil Tsalapatis
2026-11-24  6:40   ` bot+bpf-ci

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=20261124060357.11700-1-emil@etsalapatis.com \
    --to=emil@etsalapatis.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=memxor@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.