From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 74EC73F4835 for ; Tue, 25 Aug 2026 09:50:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787651410; cv=none; b=CtXS2T8y1OaiQNlJT5ouPE1RM4FybdML7TADojr55dKYftPC894c+wYYlwWfN6ERCzPqKP0xzeUGPIWsHwgHF2y98mvgN9M2X3CW/dtKuwwe/DAxKLf1swjoEhmbjoZA/zy+PUUt8vII48GEjOCdq5In2gpweBsrs2DUQRvvB7c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787651410; c=relaxed/simple; bh=PyE160OajPSsqQd9ddyOfvbdMpeClWf6VPakHByI8uA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pcJS6UuwGEA2IHUm7viEKMx+aibben1Xzh8Awm1g/afoysovma6AyaT+iOvtu6BP2wFS04/eiIg+1NyE7g9qj/xBKjWb7Vh1zYDRAjdSoPgnnQ9VrX2YL6dwQw7XGhfz5xlGBZL3c85QTVUGoimwq3LmUm7ibzYzkFOW6epB0II= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mylXEhIO; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mylXEhIO" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2d5655cc850so49983245ad.3 for ; Tue, 25 Aug 2026 02:50:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787651408; x=1788256208; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=W7cSIyxl/i1oxSUvdnacr9V24YcqM8fZNDWiyKdu00s=; b=mylXEhIOn2K1qqw1nMVq4eo5yJ2TnzTcl+0GdsooM5a0b8Y0cYu8qS2oq6AHCzjWBW 6q5DReILtgQinihErYEGpenSQItpt4AnYHzllNvMB7w10xWKvZKfogS/cFP3IEb0Ho6T KpMzA/czWthiNcW0ux5NhwdFWws3vxbvevnUG0c9Ieb5j7rpj7dvMx+lufLU2EqcXycG LcQliSZvQyKnH1FS82BrgFQigDIHP1ydA8KSeHPPL3oXlsXp1Zz4NZuQTVxUmricyU// WdrD6v02+/fIAoep7PhyRI74kjccojGm0Mh/fDC2nyoIUyd6MkJwl6pPvqq0J+JS772r qXWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787651408; x=1788256208; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=W7cSIyxl/i1oxSUvdnacr9V24YcqM8fZNDWiyKdu00s=; b=h0hxo13f/ik9XsEQjy7BFhWGbCdMLORKVk+96YV4MtyiPGPmO2zuMAe95FaM5lxRfk YS9s8s2HbrztZMRuxEKXdA+DCMxcSue3QEhndHtzPXZmrGKwH2ddA78YgGauDzOoDRjZ gnk5KHRrVcaT82xvIz6nm3hfr5Xf9u4ugGGTONclr9STLxRGXjUYY0bkcDq+WQL2hgkN Jup/3+sn/7ke8/VXtCZ2i5Zmnbhy3vJDOvS8n/6JRNKcVL/Q154mxuj3cFhfSBBiRyq7 eCEuHzWXGaQnid11PVn+HqHo77pM6BaEAADaZTDtb/GtPfk/4TCMKGqrZDD9fJhaXzd7 74iA== X-Gm-Message-State: AFuF++lpBkp1ZrdHbfhxq4q6/+tmAm9rKbECtp4xOjGEYFTqUhs77BU0 q+mLFsX89m4PYewEKJROq7vSr/cwIkSifuUr6r5+Hfeft4fU8tlrG8Rh X-Gm-Gg: AR+sD12/+2Ho82nN5YB6dyKa2qHBcFuHSkew3M15GSCNMe3X7Pty1I5xtcZ9kMY3i/I onzfqt8B85UivXcrH99gm2kGc7opmPQhbmCxSrz1PpQ4jj6Kz6BSg8So8lTVBvEktCGoNlSgBtb UezMlTRuN2guWGlGmr+jNng95JCqegK5VB+1zbm6DxzxBH3vUynNQeDwYL+uWUU3Ybtpcz1yAq5 LSvUTd/AScvKzjibKHLLP6RkI2JQNb85+R78DbATou/nXK7s/cnhtZQ73776iE39ZiXXSv5Vqj3 A018ly9k6rDJBrUWrTKq4/it9kyO0P56xlqk180+vEhcF+kcllKpS7LQO+HoBfTsKIOh0kAwwEJ xAw6l9r0yngXbRYUi7AMVZCTth5KyUqz3l+7T1/xTrQbOgHk3wzyMD2m1OnmTArcH52OC9ZhXub QMimua+gDjmf6GpuB+q3TXOni7k7g302+p1aX1D52DGdf0G5q4acvVYB8m/suSS2pXJOQkPTRxM PQHCufpxh5IUKrZ0zDX2OSazO/+F0hreMg= X-Received: by 2002:a17:903:390d:b0:2c9:fb11:1bf4 with SMTP id d9443c01a7336-2d64af4eea9mr635232355ad.7.1787651407659; Tue, 25 Aug 2026 02:50:07 -0700 (PDT) Received: from localhost.localdomain ([103.120.31.178]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-32827662702sm5923062eec.10.2026.08.25.02.50.03 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 25 Aug 2026 02:50:07 -0700 (PDT) From: Khawar Ahemad To: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, jiayuan.chen@linux.dev, emil@etsalapatis.com, ahemadkhawar123@gmail.com Subject: [PATCH bpf-next v6 1/4] bpf: Add a sleepable page allocator for map memory Date: Tue, 25 Aug 2026 15:19:52 +0530 Message-ID: <20260825094955.83240-2-ahemadkhawar123@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260825094955.83240-1-ahemadkhawar123@gmail.com> References: <20260825094955.83240-1-ahemadkhawar123@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Jiayuan Chen 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 Signed-off-by: Khawar Ahemad --- 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 b3cd28d9e3..c817c99d29 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 6874ba1424..f9b81638e5 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.54.0 (Apple Git-157)