From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-173.mta1.migadu.com (mta1.migadu.com [37.59.57.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8DFC53F44FC for ; Wed, 5 Aug 2026 09:20:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=37.59.57.117 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785921617; cv=none; b=Sqba85Okk2aigrlZReCvQWL+mxaQOcu+uvMNrsuzADxJSxLJAhfSjHpSxPMdzTmS9jo6DVFHAmjJjEZ6cD7a0vLT4eXShbpts+ImgfmhnsMIR5PMl7cV8y/6PT6Q9kwvQMAdx+3K2r5I1559aCk1jZh69zFEnOC8E7cGKlfFQtI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785921617; c=relaxed/simple; bh=ijsnVrTrJAr9iIFufGidF9T2LTUTcg75qzLv7bwVVP8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MDvaaKlq5Dmcuq09JZosF+sb25ps7CM14nyCRXrZgXmRTAePlfNEwdjMS+k8uKQ1KaRikEvin6xi9VK5R2vu25umZMWw1tLAgPtEiHuHT+fRwEecEVPQjsibMAG5YcQ8374vjCppVBEu4HGmVLG9zf1iSie/cBhCbhNTsSx5/yI= 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=ArIN6sN/; arc=none smtp.client-ip=37.59.57.117 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="ArIN6sN/" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1785921611; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nI2x4Jw4QeDH+z0w8stHMu3B2x9kMF1+3hiOMr55Tzo=; b=ArIN6sN/8ojY0V9jMtDPvYjzaY+pzmBJlFZRF64c4OxST+5INcT9DOa5Qsq/KaRA8YsDmT XbD3U1Dmh4eIWbRvatGb0WWwB21vi4Hi1tLDWsJ5vYLqn3lhxdYlcY6CHZOHe1qM7EcBBR 0NE+eI64Ivk/Dsif8WrSBtomjYu0b6E= 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 , Shuah Khan , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-rt-devel@lists.linux.dev Subject: [PATCH bpf-next v2 1/4] bpf: Add a sleepable page allocator for map memory Date: Wed, 5 Aug 2026 17:15:54 +0800 Message-ID: <20260805091720.139924-2-jiayuan.chen@linux.dev> In-Reply-To: <20260805091720.139924-1-jiayuan.chen@linux.dev> References: <20260805091720.139924-1-jiayuan.chen@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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. It allocates from the map's numa_node, like the other map allocators. The next patch uses it from the arena page fault handler. Signed-off-by: Jiayuan Chen Reviewed-by: Emil Tsalapatis --- 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 73bacfc6444d..0dc51c37c25c 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 8d111da88655..67d8157c6623 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) { -- 2.43.0