BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v6 0/4] bpf: arena: handle memory.max on fault-in with reclaim/OOM
@ 2026-08-25  9:49 Khawar Ahemad
  2026-08-25  9:49 ` [PATCH bpf-next v6 1/4] bpf: Add a sleepable page allocator for map memory Khawar Ahemad
                   ` (4 more replies)
  0 siblings, 5 replies; 17+ messages in thread
From: Khawar Ahemad @ 2026-08-25  9:49 UTC (permalink / raw)
  To: bpf
  Cc: linux-kernel, ast, daniel, andrii, eddyz87, jiayuan.chen, emil,
	ahemadkhawar123

This series fixes an issue where accessing a valid BPF arena page under
memory pressure (hitting a cgroup's memory.max limit) incorrectly results
in a SIGSEGV crash instead of invoking memory reclaim or the memcg OOM
killer.

Problem:
========
arena_vm_fault() performed page allocation while holding arena->spinlock
(with interrupts disabled), restricting the allocation to the non-blocking
alloc_pages_nolock() path, which cannot sleep, reclaim memory, or invoke
the memcg OOM killer. When the cgroup's memory.max limit was reached, the
allocation returned -ENOMEM, which arena_vm_fault() converted to
VM_FAULT_SIGSEGV, killing the process on a valid virtual address. Hitting
memory.max is routine (e.g., page cache growth from reading a large file),
so this killed innocent processes.

Solution:
=========
1. Add bpf_map_alloc_page_sleepable() for callers that know they are in a
   sleepable context. can_alloc_pages() is a conservative guess for BPF
   program context -- notably it is always false on PREEMPT_RT -- so going
   through bpf_map_alloc_pages() there needlessly picks the non-blocking
   allocator. The new helper uses BPF_PAGE_GFP (GFP_KERNEL | __GFP_ZERO |
   __GFP_ACCOUNT | __GFP_NOWARN) directly.

2. Rework arena_vm_fault() to preallocate the page outside arena->spinlock
   with bpf_map_alloc_page_sleepable(), like do_anonymous_page() does, so
   it can sleep, reclaim, and invoke the memcg OOM killer instead of
   turning a routine memory.max event into a spurious SIGSEGV.

3. A lockless probe (vmalloc_to_page()) before allocation skips
   preallocation when a page is already mapped (e.g., allocated by the bpf
   program), so the common case wastes no allocation. The rare race where
   such a page is freed before the lock is taken falls back to the
   non-blocking allocator under the lock.

4. Return VM_FAULT_SIGBUS for non-recoverable errors (lock failure,
   range-tree and page-table failures) instead of VM_FAULT_SIGSEGV;
   only BPF_F_SEGV_ON_FAULT, and a scratch-page hole under that flag,
   is a real user addressing error and keeps VM_FAULT_SIGSEGV.

5. Add a BPF selftest (arena_memcg) that joins a child process into a
   memcg capped 64 MiB above its post-load usage, faults arena pages
   until the budget is exhausted, and verifies the child is OOM-killed
   (memcg oom_kill event) rather than receiving SIGSEGV.

v6 vs v5:
  - Fixed redundant #include <unistd.h> inside #ifndef PAGE_SIZE guard
    in the selftest (the header was already unconditionally included).
  - All patches carry both original author and reviewer Signed-off-by.

Signed-off-by: Khawar Ahemad <ahemadkhawar123@gmail.com>

Jiayuan Chen (4):
  bpf: Add a sleepable page allocator for map memory
  bpf: arena: allocate the fault-in page outside the lock
  selftests/bpf: Add read_cgroup_file() to cgroup_helpers
  selftests/bpf: Add a test for arena fault-in under memory.max

 include/linux/bpf.h                           |   1 +
 kernel/bpf/arena.c                            |  90 +++++++---
 kernel/bpf/syscall.c                          |  21 ++-
 tools/testing/selftests/bpf/cgroup_helpers.c  |  67 ++++++++
 tools/testing/selftests/bpf/cgroup_helpers.h  |   4 +
 .../selftests/bpf/prog_tests/arena_memcg.c    | 157 ++++++++++++++++++
 .../testing/selftests/bpf/progs/arena_memcg.c |  24 +++
 7 files changed, 339 insertions(+), 25 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_memcg.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_memcg.c

-- 
2.54.0 (Apple Git-157)


^ permalink raw reply	[flat|nested] 17+ messages in thread
* [PATCH bpf-next v6 0/4] bpf: arena: handle memory.max on fault-in with reclaim/OOM
@ 2026-08-25  9:19 Khawar Ahemad
  0 siblings, 0 replies; 17+ messages in thread
From: Khawar Ahemad @ 2026-08-25  9:19 UTC (permalink / raw)
  To: bpf, linux-kernel; +Cc: ast, daniel, andrii, eddyz87, jiayuan.chen, emil

This series fixes an issue where accessing a valid BPF arena page under
memory pressure (e.g. hitting a cgroup's memory.max limit) incorrectly
results in a SIGSEGV crash rather than invoking memory reclaim or the
memcg OOM killer.

Problem:
========
arena_vm_fault() previously performed page allocation while holding
arena->spinlock (with interrupts disabled). This restricted the allocation
path to the non-blocking alloc_pages_nolock() path, which cannot sleep,
reclaim memory, or invoke the memcg OOM killer. When the cgroup's
memory.max limit was reached, the allocation returned -ENOMEM, which
arena_vm_fault() converted to VM_FAULT_SIGSEGV, killing the process on a
valid virtual address.

Solution:
=========
1. Introduce bpf_map_alloc_page_sleepable() using BPF_PAGE_GFP
   (GFP_KERNEL | __GFP_ZERO | __GFP_ACCOUNT | __GFP_NOWARN) for sleepable
   contexts.
2. Rework arena_vm_fault() to preallocate the page outside arena->spinlock
   with bpf_map_alloc_page_sleepable(), allowing reclaim and OOM handling.
3. Use a lockless probe (vmalloc_to_page) before allocation to avoid
   redundant allocations in the hot path.
4. Replace misleading VM_FAULT_SIGSEGV returns with VM_FAULT_SIGBUS for
   non-recoverable internal errors, reserving SIGSEGV strictly for true
   addressing violations under BPF_F_SEGV_ON_FAULT.
5. Add a BPF selftest (arena_memcg) to validate fault-in behavior and
   proper OOM handling under cgroup memory.max constraints.

Signed-off-by: Khawar Ahemad <ahemadkhawar123@gmail.com>

Jiayuan Chen (4):
  bpf: Add a sleepable page allocator for map memory
  bpf: arena: allocate the fault-in page outside the lock
  selftests/bpf: Add read_cgroup_file() to cgroup_helpers
  selftests/bpf: Add a test for arena fault-in under memory.max

 include/linux/bpf.h                           |   1 +
 kernel/bpf/arena.c                            |  90 +++++++---
 kernel/bpf/syscall.c                          |  21 ++-
 tools/testing/selftests/bpf/cgroup_helpers.c  |  67 ++++++++
 tools/testing/selftests/bpf/cgroup_helpers.h  |   4 +
 .../selftests/bpf/prog_tests/arena_memcg.c    | 158 ++++++++++++++++++
 .../testing/selftests/bpf/progs/arena_memcg.c |  24 +++
 7 files changed, 340 insertions(+), 25 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_memcg.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_memcg.c

-- 
2.54.0 (Apple Git-157)


^ permalink raw reply	[flat|nested] 17+ messages in thread
* [PATCH bpf-next v6 0/4] bpf: arena: handle memory.max on fault-in with reclaim/OOM
@ 2026-08-25  9:16 Khawar Ahemad
  0 siblings, 0 replies; 17+ messages in thread
From: Khawar Ahemad @ 2026-08-25  9:16 UTC (permalink / raw)
  To: bpf, linux-kernel; +Cc: ast, daniel, andrii, eddyz87, jiayuan.chen, emil

This series fixes an issue where accessing a valid BPF arena page under
memory pressure (e.g. hitting a cgroup's memory.max limit) incorrectly
results in a SIGSEGV crash rather than invoking memory reclaim or the
memcg OOM killer.

Problem:
========
arena_vm_fault() previously performed page allocation while holding
arena->spinlock (with interrupts disabled). This restricted the allocation
path to the non-blocking alloc_pages_nolock() path, which cannot sleep,
reclaim memory, or invoke the memcg OOM killer. When the cgroup's
memory.max limit was reached, the allocation returned -ENOMEM, which
arena_vm_fault() converted to VM_FAULT_SIGSEGV, killing the process on a
valid virtual address.

Solution:
=========
1. Introduce bpf_map_alloc_page_sleepable() using BPF_PAGE_GFP
   (GFP_KERNEL | __GFP_ZERO | __GFP_ACCOUNT | __GFP_NOWARN) for sleepable
   contexts.
2. Rework arena_vm_fault() to preallocate the page outside arena->spinlock
   with bpf_map_alloc_page_sleepable(), allowing reclaim and OOM handling.
3. Use a lockless probe (vmalloc_to_page) before allocation to avoid
   redundant allocations in the hot path.
4. Replace misleading VM_FAULT_SIGSEGV returns with VM_FAULT_SIGBUS for
   non-recoverable internal errors, reserving SIGSEGV strictly for true
   addressing violations under BPF_F_SEGV_ON_FAULT.
5. Add a BPF selftest (arena_memcg) to validate fault-in behavior and
   proper OOM handling under cgroup memory.max constraints.

Jiayuan Chen (4):
  bpf: Add a sleepable page allocator for map memory
  bpf: arena: allocate the fault-in page outside the lock
  selftests/bpf: Add read_cgroup_file() to cgroup_helpers
  selftests/bpf: Add a test for arena fault-in under memory.max

 include/linux/bpf.h                           |   1 +
 kernel/bpf/arena.c                            |  90 +++++++---
 kernel/bpf/syscall.c                          |  21 ++-
 tools/testing/selftests/bpf/cgroup_helpers.c  |  67 ++++++++
 tools/testing/selftests/bpf/cgroup_helpers.h  |   4 +
 .../selftests/bpf/prog_tests/arena_memcg.c    | 158 ++++++++++++++++++
 .../testing/selftests/bpf/progs/arena_memcg.c |  24 +++
 7 files changed, 340 insertions(+), 25 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_memcg.c
 create mode 100644 tools/testing/selftests/bpf/progs/arena_memcg.c

-- 
2.54.0 (Apple Git-157)


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

end of thread, other threads:[~2026-08-25 12:23 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-25  9:49 [PATCH bpf-next v6 0/4] bpf: arena: handle memory.max on fault-in with reclaim/OOM Khawar Ahemad
2026-08-25  9:49 ` [PATCH bpf-next v6 1/4] bpf: Add a sleepable page allocator for map memory Khawar Ahemad
2026-08-25 10:11   ` sashiko-bot
2026-08-25 10:22   ` Khawar Ahemad
2026-08-25 10:32   ` bot+bpf-ci
2026-08-25  9:49 ` [PATCH bpf-next v6 2/4] bpf: arena: allocate the fault-in page outside the lock Khawar Ahemad
2026-08-25 10:32   ` bot+bpf-ci
2026-08-25  9:49 ` [PATCH bpf-next v6 3/4] selftests/bpf: Add read_cgroup_file() to cgroup_helpers Khawar Ahemad
2026-08-25  9:49 ` [PATCH bpf-next v6 4/4] selftests/bpf: Add a test for arena fault-in under memory.max Khawar Ahemad
2026-08-25 10:32   ` bot+bpf-ci
2026-08-25 10:43   ` Khawar Ahemad
2026-08-25 11:19     ` Jiayuan Chen
2026-08-25 12:23       ` Kumar Kartikeya Dwivedi
2026-08-25 11:28   ` Khawar Ahemad
2026-08-25  9:55 ` [PATCH bpf-next v6 0/4] bpf: arena: handle memory.max on fault-in with reclaim/OOM Jiayuan Chen
  -- strict thread matches above, loose matches on Subject: below --
2026-08-25  9:19 Khawar Ahemad
2026-08-25  9:16 Khawar Ahemad

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