BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v1 0/4] bpf: Use kmalloc for local storage if possible
@ 2026-11-24  6:03 Emil Tsalapatis
  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
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Emil Tsalapatis @ 2026-11-24  6:03 UTC (permalink / raw)
  To: bpf; +Cc: ast, andrii, eddyz87, memxor, daniel, Emil Tsalapatis

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


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

end of thread, other threads:[~2026-11-24  6:41 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24  6:03 [PATCH bpf-next v1 0/4] bpf: Use kmalloc for local storage if possible Emil Tsalapatis
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  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

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