From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) (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 0F1AB3E5A29 for ; Tue, 25 Aug 2026 09:16:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787649420; cv=none; b=fiCTueKDaE97gPosE/Y3Y30ksMTIWp8c81/wYuZqhaqOl4xJ8BxjrRQAeogGe4c1HY7tlOIjXXrhlgrtl3AODasOgp0qzYwgGl3pFPW6dnLA1n2ubabQPKc94qVsAMv5w5GUMgNPXye76qd6z5CeMGcRuYdIExRbRw+E2Ne55lo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787649420; c=relaxed/simple; bh=g+/TtVkzmh631DxnNEFoUvESWZL7IYHefa8yB6Kzeww=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=b3zGU6d1VzFBBPAIWM6LtKDK9/VrMvFawAmaJ4kQSokB/Ws2ygOe3/COzJYv78c0eGhJcIslTWNFRZGjLD5o74cQrRzcGwTlYvN9uyF98D0gaVzbPzNnIxt21plq+ne5y7/4ME6n4feTeRi9GaQPksNsPK+mS2tA6VnxNVHnba0= 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=cA2Tr6Zg; arc=none smtp.client-ip=209.85.216.48 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="cA2Tr6Zg" Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-38dc4553f62so6255717a91.0 for ; Tue, 25 Aug 2026 02:16:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787649418; x=1788254218; 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=UnWWoI95YwE6br4ddsyhfhfO9WEVo89LWu8/RDZKuLM=; b=cA2Tr6ZgrLuGseRJLHHNpuDSDhARMc21mJR9exn+M3sdPia51nxlqO4l79KeoQ9hpl cXnbclWeyyEoDh7kFG7LPTE4M+XGP61FmQknhwdvhhT1lZJCKSwkwTVbFvtawUETcJAU NuPFgZ4p0U4Ta285kewcUhzFXoKoUgChsETDDO2dZrpI5W7+dUow5Zh1iaMYa8acI+NW /IqPxzd4w4DlCbzy96CGcD4oN+ZjWPNWSw2vPtZHq+v6FpVGQpAME7YbF0rJv+OGtUJ2 FPDk5fJvY7poMENLOlh80ew3yKJPY15eoLeR2sm7APHa3q/cg+mkczvJh38+6XMOJVrE wTkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787649418; x=1788254218; 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=UnWWoI95YwE6br4ddsyhfhfO9WEVo89LWu8/RDZKuLM=; b=d63/2f7xTkcaEhbxA+xBK3fI5+MUN+xD74mFXUR2KJbFq/LF+W8ljAmeXec/8ZCNqV FmNUnM/H2QBCPlU/9Dys3FVmoLViWDEkuL79Dy23x9qb/njMUqts5i4QkcLX8gxtUvJk lVuX88mmA7DcjL3pQGUDx1FPZy0TCSTutvML8vUAm48tHUKZ+zmS66ZljkC4qO4T1lS/ IWRCLMzyKE1LzjxmxG6RyBIchQOHlIqTiWohi7Z+YId3QA9+GcVWaVH+bTpDFYQOl5li gdvZx7aCdxdj+yUMWrCGv7Vqq7ql1f0RBxZTJH41MrQGWzyTMCadzKH+cwoBqQpuv6pZ TlVA== X-Forwarded-Encrypted: i=1; AHgh+RpYIe3wrIBa5HiMiUwtoKitE+NvzxqmbDjRA1bDOJa4IgG2oNzJZyuaenZFmxzmg1X6PylXzj5vuPAmwuo=@vger.kernel.org X-Gm-Message-State: AFuF++mKCP7haWpLIonPvyUHWVV7HRc5z8OAvMz9At6wbVpPVX70fxrM 7XlyCHgMtq1IDIRc57vJ5MFSuaDHScebWaQAKBzwn+Oz8urIT8Io8grc X-Gm-Gg: AR+sD12t9dnM/zBNgIu77OdL9SIeUqDJssR7phVrVugaSRr/ANgPvujuvjH1bhi+jWS +FflkHp4GRCSiOGgq/rsH0SiTPdVSF6iMCWViMgd6HjQ6aFI1xxYo8z8syWNV1pEfP+nnWadbev mCwKdI58v6ELJdzBteEmZzx8j0BS7z7T4wSb2jHYea0Zl+4RcmeA0VLtsyZCUrkFMv29g+EkYEX sk3lh0rvtONCPL9KiYrsUCsoKwruR2c3FKP2XXMDZigNuZZpI24ZL++lAs+F1IDmrJ9oCR9bTja VVGo3IQfDwA0810rHBWUA6FfsQ6IQIw88DOJOOwHflwM2tJiqC+GIFlmCFTFuCfxEqzKkqFKMEf 0vdL9oNPTDqFs6SI8++g5x0CRaVWOyOv5hrjtOCFlRoq43Nn2wX23OrRvLmHAOgm59Bos86C9Xs /LnmK1/l2eS52jhy+BZplYmN18KPT4QwzdUKyG1EwB57jsTqVQUBpYQ5TAZd7XQ8l30cLeVKEHL uzqH7i28yoDISg4e68= X-Received: by 2002:a17:90a:1697:b0:396:635a:9b10 with SMTP id 98e67ed59e1d1-396635aaf7emr125987a91.11.1787649418345; Tue, 25 Aug 2026 02:16:58 -0700 (PDT) Received: from localhost.localdomain ([103.120.31.178]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-141860fe08csm52122658c88.8.2026.08.25.02.16.54 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 25 Aug 2026 02:16:57 -0700 (PDT) From: Khawar Ahemad To: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, jiayuan.chen@linux.dev, emil@etsalapatis.com Subject: [PATCH v6 1/4] bpf: Add a sleepable page allocator for map memory Date: Tue, 25 Aug 2026 14:46:44 +0530 Message-ID: <20260825091647.81632-2-ahemadkhawar123@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260825091647.81632-1-ahemadkhawar123@gmail.com> References: <20260825091647.81632-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 --- 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)