From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mta1.migadu.com (out-157.mta1.migadu.com [95.215.58.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33E622F8E91 for ; Fri, 21 Aug 2026 05:06:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.157 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787288813; cv=none; b=QClQY5nlX3D5BU3k6vEZ3i8vEzqI4EivRuxjyMkI5BvyKOpC/SMcW4RSMLqH+VELhieWkGrN3e2suJ85x2JrN0BsRkXotm8k9I5UEJaP5fnD19ds6qnvSsy9VUZvKTnuQlky4TKeoN3xpXSwrUZ082SrpN8RIJApByUIwbLKYjc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787288813; c=relaxed/simple; bh=tm6u+ZlBoJQgiuGsXMgzX4HKl9jwnrAdYGPMvgSLM/Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VtPhDqJJ1v5eFQiHwGu0rORLbwgg3DBvvQySXnS6dFBagAYR6RJg5t+oNC/nkXMV7z93qnMA9dJUaeiZaLZghmE5t/RYQ7f/zv/f4E6BHOSWVXLRiLLYukNmxm9IO0oAJ5IwjB3QBr0apoWjg1NutZs1/T0q6xsJuo+4VNBsNxg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=tbNKZGEv; arc=none smtp.client-ip=95.215.58.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="tbNKZGEv" X-Envelope-To: bpf@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=tm6u+ZlBoJQgiuGsXMgzX4HKl9jwnrAdYGPMvgSLM/Y=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1787288810; v=1; x=1787893610; b=tbNKZGEv09mIVIewntzSnb/vzyVDpIwL2aad42WG/b0PWYOzfoFKqNeGMzgr6nxXxHLpE6l7 99hvJftqSnj6/ywIzMn9EnhARF0PvyqSbFujkAqTnHb/HttRS7k+4ZHAEq+qmZ4c0rJmOgetK9t /our3uDVYz9u4tI/DrIHJBWk= X-Envelope-To: bpf@vger.kernel.org Received: from localhost.localdomain (147.136.157.2) by smtp.migadu.com with ESMTPS id dffa8a668dcc0a41; Fri, 21 Aug 2026 05:06:50 +0000 X-Mizu-Trace-ID: dffa8a668dcc0a41 X-Migadu-Flow: FLOW_OUT From: Jiayuan Chen To: bpf@vger.kernel.org Cc: Jiayuan Chen , Emil Tsalapatis , Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Ihor Solodrai , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , linux-kernel@vger.kernel.org, linux-rt-devel@lists.linux.dev Subject: [PATCH bpf-next v4 1/4] bpf: Add a sleepable page allocator for map memory Date: Fri, 21 Aug 2026 13:06:12 +0800 Message-ID: <20260821050631.39784-1-jiayuan.chen@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260821050250.35112-1-jiayuan.chen@linux.dev> References: <20260821050250.35112-1-jiayuan.chen@linux.dev> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit bpf_map_alloc_pages() picks the allocator via can_alloc_pages(), a conservative guess for BPF program context that is always false under PREEMPT_RT. So even a caller that really is sleepable gets the non-blocking allocator, which never reclaims and never engages the OOM machinery. Add bpf_map_alloc_page_sleepable() for callers that know they are sleepable. Like the other bpf map allocators it places the page on the map's numa_node and does not follow the faulting task's NUMA mempolicy; arena memory is shared, so the map's node is the right placement policy. The next patch uses it from the arena page fault handler. Signed-off-by: Jiayuan Chen Reviewed-by: Emil Tsalapatis --- To sashiko: - The alloc_pages_nolock() fallback still zeroes: it forces __GFP_ZERO internally and only accepts __GFP_ACCOUNT. - __GFP_ZERO is unchanged from the existing __bpf_alloc_page(), and no arena-capable arch has D-cache aliasing, so there is no dcache concern. --- include/linux/bpf.h | 1 + kernel/bpf/syscall.c | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index ffa5626411ac..18e84ec4692d 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2784,6 +2784,7 @@ struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id); int bpf_map_alloc_pages(const struct bpf_map *map, int nid, unsigned long nr_pages, struct page **page_array); +struct page *bpf_map_alloc_page_sleepable(const struct bpf_map *map); #ifdef CONFIG_MEMCG void bpf_map_memcg_enter(const struct bpf_map *map, struct mem_cgroup **old_memcg, struct mem_cgroup **new_memcg); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 6874ba1424af..f9b81638e537 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -602,15 +602,14 @@ static bool can_alloc_pages(void) !IS_ENABLED(CONFIG_PREEMPT_RT); } +#define BPF_PAGE_GFP (GFP_KERNEL | __GFP_ZERO | __GFP_ACCOUNT | __GFP_NOWARN) + static struct page *__bpf_alloc_page(int nid) { if (!can_alloc_pages()) return alloc_pages_nolock(__GFP_ACCOUNT, nid, 0); - return alloc_pages_node(nid, - GFP_KERNEL | __GFP_ZERO | __GFP_ACCOUNT - | __GFP_NOWARN, - 0); + return alloc_pages_node(nid, BPF_PAGE_GFP, 0); } int bpf_map_alloc_pages(const struct bpf_map *map, int nid, @@ -636,6 +635,20 @@ int bpf_map_alloc_pages(const struct bpf_map *map, int nid, return ret; } +/* + * For callers that know they run in a sleepable context, e.g. a user page + * fault handler. can_alloc_pages() is a conservative guess made for BPF + * program context - notably it is always false on PREEMPT_RT - so going + * through bpf_map_alloc_pages() there would needlessly pick the + * non-blocking allocator, which never reclaims and never engages the OOM + * machinery. + */ +struct page *bpf_map_alloc_page_sleepable(const struct bpf_map *map) +{ + might_sleep(); + return alloc_pages_node(map->numa_node, BPF_PAGE_GFP, 0); +} + static int btf_field_cmp(const void *a, const void *b) { const struct btf_field *f1 = a, *f2 = b; -- 2.43.0