Skip to content

bpf: Use kmalloc for local storage if possible

Emil Tsalapatis bpf 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest4 patches 1 replyIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4bpf: Add call for selecting kmalloc or kmalloc_nolock at call timeinclude/linux/bpf.h kernel/bpf/bpf_local_storage.c kernel/bpf/syscall.c+30 -61 reply
2/4bpf: Plumb local storage contextinclude/linux/bpf_local_storage.h kernel/bpf/bpf_cgrp_storage.c kernel/bpf/bpf_inode_storage.c +3 more+62 -320 replies
3/4bpf: Add sleepable local storageinclude/linux/bpf.h kernel/bpf/bpf_local_storage.c+10 -10 replies
4/4selftests/bpf: Test kmalloc variant selection for task storagetools/testing/selftests/bpf/prog_tests/task_storage_sleepable.c tools/testing/selftests/bpf/progs/task_storage_sleepable.c+220 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH bpf-next v1 0/4] bpf: Use kmalloc for local storage if possible

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);
  }

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
Signature · 1 line
2.52.1