summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorEmil Tsalapatis <emil@etsalapatis.com>2026-10-02 10:52:13 +0000
committerAlexei Starovoitov <ast@kernel.org>2026-10-02 13:00:59 +0000
commitcbe7d2bf7355416815c878e839d03028f4724dfb (patch)
tree86582d61c9752b78de1ac8e45f7e3e986e9abd09 /kernel
parent11de29f38a3455e5785c5fd5f38d93c413c5e822 (diff)
bpf: Add sleepable argument to bpf_alloc_pages()
bpf_alloc_pages() currently decides whether it may use the blocking page allocator from the current execution context alone. Let callers further restrict that choice by passing whether their context is sleepable. Use the blocking allocator only when both the caller and runtime context allow sleeping. Add __GFP_RETRY_MAYFAIL so this path can reclaim without invoking the OOM killer when the allocation is charged to another memcg. Existing non-sleepable callers retain the no-lock allocation behavior. Signed-off-by: Emil Tsalapatis <emil@etsalapatis.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://patch.msgid.link/20261002105218.6171-3-emil@etsalapatis.com
Diffstat (limited to 'kernel')
-rw-r--r--kernel/bpf/arena.c6
-rw-r--r--kernel/bpf/syscall.c10
2 files changed, 8 insertions, 8 deletions
diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c
index de4f7c7f68f5..c556df7730c4 100644
--- a/kernel/bpf/arena.c
+++ b/kernel/bpf/arena.c
@@ -317,7 +317,7 @@ static struct bpf_map *arena_map_alloc(union bpf_attr *attr)
INIT_WORK(&arena->free_work, arena_free_worker);
bpf_map_init_from_attr(&arena->map, attr);
- arena->scratch_page = bpf_alloc_page(NUMA_NO_NODE);
+ arena->scratch_page = bpf_alloc_page(NUMA_NO_NODE, true);
if (!arena->scratch_page)
goto err_free_arena;
@@ -550,7 +550,7 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf)
* The probed page was freed meanwhile or preallocation failed;
* try the non-blocking allocator, we cannot sleep here.
*/
- new_page = bpf_alloc_page(map->numa_node);
+ new_page = bpf_alloc_page(map->numa_node, false);
if (!new_page) {
fault_ret = VM_FAULT_SIGBUS;
goto out_err_locked_memcg;
@@ -771,7 +771,7 @@ static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt
uaddr32 = (u32)(arena->user_vm_start + pgoff * PAGE_SIZE);
- ret = bpf_alloc_pages(node_id, page_cnt, &pages);
+ ret = bpf_alloc_pages(node_id, page_cnt, &pages, false);
if (ret)
goto out;
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index a5df15a6cd51..ef8fb2f6e6e3 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -602,14 +602,14 @@ static bool can_alloc_pages(void)
!IS_ENABLED(CONFIG_PREEMPT_RT);
}
-struct page *bpf_alloc_page(int nid)
+struct page *bpf_alloc_page(int nid, bool sleepable)
{
- if (!can_alloc_pages())
+ if (!sleepable || !can_alloc_pages())
return alloc_pages_nolock(__GFP_ACCOUNT, nid, 0);
return alloc_pages_node(nid,
GFP_KERNEL | __GFP_ZERO | __GFP_ACCOUNT
- | __GFP_NOWARN,
+ | __GFP_NOWARN | __GFP_RETRY_MAYFAIL,
0);
}
@@ -624,13 +624,13 @@ void bpf_free_pages(struct llist_head *pages)
}
int bpf_alloc_pages(int nid, unsigned long nr_pages,
- struct llist_head *pages)
+ struct llist_head *pages, bool sleepable)
{
unsigned long i;
struct page *pg;
for (i = 0; i < nr_pages; i++) {
- pg = bpf_alloc_page(nid);
+ pg = bpf_alloc_page(nid, sleepable);
if (!pg)
goto free_pages;
llist_add(&pg->pcp_llist, pages);