From patchwork Fri Jan 24 03:56:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13948884 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA47BC02181 for ; Fri, 24 Jan 2025 03:57:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6861D280035; Thu, 23 Jan 2025 22:57:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 60DC428002E; Thu, 23 Jan 2025 22:57:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 45F6D280035; Thu, 23 Jan 2025 22:57:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 2542B28002E for ; Thu, 23 Jan 2025 22:57:21 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C9ADA47AEA for ; Fri, 24 Jan 2025 03:57:20 +0000 (UTC) X-FDA: 83040985440.11.FC48966 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf18.hostedemail.com (Postfix) with ESMTP id C60B11C000B for ; Fri, 24 Jan 2025 03:57:18 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=DbN7e2wN; spf=pass (imf18.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737691038; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bDuPZRtqsVh7bFH3yxNIFanE739bKkzWZ/UfDyq/wdw=; b=EAAN76tYqLA9yY0/d85YnqQdIEFV3CMsvXaVF+//8J86NFBGwVpmxNIr//QFo8d0kKVyfd 4oJ/yikQKtARl5AzJsYLzCF0HC2LVun+N5gRZU6BSnjN2tRr+wRuHZif7SMe5C1I3Ty3rG aJUXogRVIyqX66Lp7N3Q33BfOQqpPes= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=DbN7e2wN; spf=pass (imf18.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737691038; a=rsa-sha256; cv=none; b=RRFUghWDa8WiYFH1oOU3dDQJyuqfMS1sAFCUwoVrUD89nUHgBUp6sd+TuBzc+aSmB0ukIz WzV9WyHcTOxkZ3SvDPs6sG1cVjA+aOMJg+c2yjx/nfM1ziWp4suk8e0EO7IXCtQ9HS4eTm oulReTlebcpBSG9PneKvSxpS5oev/0g= Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2efded08c79so2532797a91.0 for ; Thu, 23 Jan 2025 19:57:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737691037; x=1738295837; darn=kvack.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; bh=bDuPZRtqsVh7bFH3yxNIFanE739bKkzWZ/UfDyq/wdw=; b=DbN7e2wN0fxR6BRxFk3B4glCyMoQ1O45EaDkPftlfway4qQMhvDR7k4DRVsQs4tcoS 15SDIGajKUjvXt+BAW5JVorEDBfoyuGaz3e+zFUojI+ubgozITfGyONfmYJYk7RGWsOM oauncUC0ihUo/Nxx1Nv+uB4E4AW6aIboNUtbdN1LBnQh+VhlEr4KSK/ZcaDlgn9lqUsD qLmKZ4VyvXdHxD64WGb7rqQwSjS5jXgx1s2OJ/YGFE2FjaIYsu+EOHu6sG9MAvT0IrVH oLA4k4LLNuhhSdWGR2oohWdfiwgkIcnZUW5mK3d8zZyuS5oYY3v1NgcOkYeMANny0z/2 3Z5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737691037; x=1738295837; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bDuPZRtqsVh7bFH3yxNIFanE739bKkzWZ/UfDyq/wdw=; b=ghrYo+Foalvib+UKXROx9m0TYo11O7OdHskjR3SD0W5OBk34nDVWZx3MM4JGi7R1/6 fZ/uSoCRaziJFS0R+jt4mXi3wi4QIWrcIv/wI9l+GNTwJZnrP0Rm2mcy0WQt7UP8VvYD QVErPBTuK7zP+9ECLo+9/cUlkKhkmBw+ikBO5EuW7qV41DIm81DVmJHp5MOGfTF9cYwY G81ZOJtRJN5mF9E29RIi7D3hRS4AYLd0oAgJYFeGisaadbBM/jQrPdoqTOL5SzlJpqWn ON6XlgA76iWe2xnI7LibYTbhwjeZ+xdQNQCyKhWGwpRiaKWNbrki/F8giYmi3pzUOsAx HdUw== X-Forwarded-Encrypted: i=1; AJvYcCW28JZ5ygujoMaZkljNh8bWf0HdOwIVT/RgX4xW4CK2O65xdy1U5P68Yp7aDBvhGyjeiMhaEgCdbQ==@kvack.org X-Gm-Message-State: AOJu0Yz47ut/0stcZaIlZzGLBpOnoJVXrrRASPKCPCBp+6LuoK2hlNMP wlzOBfnvZK2uig69+WwFn/8TwuXthWO+s+aZw49VBf8CTrjTaft6 X-Gm-Gg: ASbGncuGaUMtdsxUHr9ZQBra5RX3IoQHJ8iPlZKC9qwYXx7aepZMVO4CywPoi/s1qDZ raSsBkHprGYq+gDHXZSQpwQLfqA/zzppdLkYHPaRZfjVI+Qg/U3WrkBCtNnCPsOP2LRFyxSfaz/ VFJot42s43y/le19g2IJEcfvqcsKK5Kb/Pux6QS3r/RvV5+hNklaP39QzIZmNvkQtXdtJ9YvLSF GVz1OpkakYQK17DWVooHfh4N87LnFwH08mZVE9OAWuyiY1dicrW0CD14J7nbNIUd0PtD4ywP4Gk kCH3uvQqf9u0SEts6P2+evYaB9gc7IHo8VBXeV4= X-Google-Smtp-Source: AGHT+IE+LCbRskWbsR6jz9xA0dh2MfdRvUJzJjh376jPaUjhK25k9uihZ7etnl2RsWLAETsaV50r6A== X-Received: by 2002:a17:90b:3547:b0:2f7:ef57:c7df with SMTP id 98e67ed59e1d1-2f7ef57c8fbmr11319328a91.7.1737691037538; Thu, 23 Jan 2025 19:57:17 -0800 (PST) Received: from macbookpro.lan ([2603:3023:16e:5000:8af:ecd2:44cd:8027]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f7ffaf896csm542025a91.34.2025.01.23.19.57.16 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 23 Jan 2025 19:57:17 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: andrii@kernel.org, memxor@gmail.com, akpm@linux-foundation.org, peterz@infradead.org, vbabka@suse.cz, bigeasy@linutronix.de, rostedt@goodmis.org, houtao1@huawei.com, hannes@cmpxchg.org, shakeel.butt@linux.dev, mhocko@suse.com, willy@infradead.org, tglx@linutronix.de, jannh@google.com, tj@kernel.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH bpf-next v6 6/6] bpf: Use try_alloc_pages() to allocate pages for bpf needs. Date: Thu, 23 Jan 2025 19:56:55 -0800 Message-Id: <20250124035655.78899-7-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20250124035655.78899-1-alexei.starovoitov@gmail.com> References: <20250124035655.78899-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: C60B11C000B X-Stat-Signature: w1stk1kypphe7bnz3hsbwkdaj99fkqrm X-Rspam-User: X-HE-Tag: 1737691038-540050 X-HE-Meta: U2FsdGVkX1+xQqQ/86hAIes8iJ57jLcLlFQ4i24dHiVVc1kpUQtiDNQpDEQmX6STo2NTLAWQJHKkJQp8L0b4QG4HMuMvf98GnT3uhDg0CLQ41SOWcfYeGFhpS4VZVq2Xom+01YWl3y+GnZozYsTl4F/f1N2v3Do1BogK9rJ+Bfz36SQeBvPXR9Vr5ZuC2r6RVIBD5qaDGkMeUer5+Js1vRiuITpVUDZA5cR8Yrds7zGnX1mCwfoH69O3H9hOE7khTIo2n+TWDm4xIpiib4/0yUgxc/Mo1hwPrLr8AFKYP2gs6UvjY6qQHxGEDqqAyG6K/GeqXzf68HrPp+cwYMsTSOuHbIY1BKkTT/F6tpW6GNEipYaI2U3ZmtL99xQfUWsOxIPrbyqpdZMJas8z53xrO0nsoxN+6SEbp9mhkJsLhUpP3tS+iU8ksZMgN/UrhK2ZuCQvebGRrE/FxLREbACQxfVxWUZJ0LZeh9A65fb/Z05zMNUaEE/lA7ln+O0xg3vzdLiGLvPQgCxF6ulRFWxWH3niA0l/+6LJwls12gqyQuM0Z4DaKYOn88XPurTWs+BTLdFB2eGHBJoqeii+5l8dtyhFvohy1EOqAirqZIBv61hri1OXriEmpiv3njBpS6ysr0Asj3QgA7ugpe9nt80h/0m7BGcToM3AXTUwOtW+zJ6QpO2I7gg5UVr5jX/YE2VehyZXPT0EkZ1wOQhDnBUk8/0uAqU6QV329ha4FvkAwpFklEDU8ehaD9fy84qd2RTLEkKGF9WzDxkU/cyWC2Z7Eq7Npo7P/e+ST925TJhG0OuGT/J9DqpBWF7e8LV7PSYRJg7p6uIXtmvhGHiXWN+R8yEzo1u+FsSWrLdqtci3rR37Dr5oT+oGR2fa/tjQqS3ULSDtTVyzBKwtNzaEr+i4B2t2ZfhpSHMvdwC2cGsiYDj4dEiOyOwmjn6bNL6Ko0i818yyhAjKEwS1maSK3B3 AABeNyRR XZm0NiQqdwmK9HLwk61WRvZCNRuT0604R+nNiwkKCOSu7ozzIPsPEzjBmaZTvUAR63Sc7TJs4kVgBi/hTbP5tSt/+vvZFXr0sLX/7w9aKSUcyB2TqfP/c66VobA4wwjiu5rBTKq+ZzCggcOXzhVKHRMBgctBG3LgVupegOOZw5A9v6R0QjMZ5FbhTVyU1N1izVOG5usQ6MUNW9db1n0PyiiI2x4bEEVE+MHQqqYc/pgk9XaPgC4ONv09E0GxwY5zdv+pS28fwArp797bfs0Q2if2lcT8Uef14eAdNEBWiLBWCYDXbpyfLjAknbW6W4RYLUMFDt97V+Gza+tdtTNKL0aLwV2O4qrAQ6zJ8njHDOn2VmYC1s+8khoOgWt2KtJixVcYX0yIEyyfdxdhQ1rqFs/iwTbTI0OZVKadi8qm1fDTFXadyLW0NWsu+jmXZ6Y+7Er1NnxUHXm1rT3B7SVf3XZ5Wfe9XUBiPwL8RG+8D/DwMcRk05V/C39xRGO41t3KdsdHNBXPIe0106FnmQJocB4bnf5CVVTn5Cmrwx20+iVdDJNA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Alexei Starovoitov Use try_alloc_pages() and free_pages_nolock() for BPF needs when context doesn't allow using normal alloc_pages. This is a prerequisite for further work. Signed-off-by: Alexei Starovoitov --- include/linux/bpf.h | 2 +- kernel/bpf/arena.c | 5 ++--- kernel/bpf/syscall.c | 23 ++++++++++++++++++++--- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index f3f50e29d639..e1838a341817 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2348,7 +2348,7 @@ int generic_map_delete_batch(struct bpf_map *map, struct bpf_map *bpf_map_get_curr_or_next(u32 *id); struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id); -int bpf_map_alloc_pages(const struct bpf_map *map, gfp_t gfp, int nid, +int bpf_map_alloc_pages(const struct bpf_map *map, int nid, unsigned long nr_pages, struct page **page_array); #ifdef CONFIG_MEMCG void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags, diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c index 4b22a651b5d5..642399a5fd9f 100644 --- a/kernel/bpf/arena.c +++ b/kernel/bpf/arena.c @@ -287,7 +287,7 @@ static vm_fault_t arena_vm_fault(struct vm_fault *vmf) return VM_FAULT_SIGSEGV; /* Account into memcg of the process that created bpf_arena */ - ret = bpf_map_alloc_pages(map, GFP_KERNEL | __GFP_ZERO, NUMA_NO_NODE, 1, &page); + ret = bpf_map_alloc_pages(map, NUMA_NO_NODE, 1, &page); if (ret) { range_tree_set(&arena->rt, vmf->pgoff, 1); return VM_FAULT_SIGSEGV; @@ -465,8 +465,7 @@ static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt if (ret) goto out_free_pages; - ret = bpf_map_alloc_pages(&arena->map, GFP_KERNEL | __GFP_ZERO, - node_id, page_cnt, pages); + ret = bpf_map_alloc_pages(&arena->map, node_id, page_cnt, pages); if (ret) goto out; diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 0daf098e3207..55588dbd2fce 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -569,7 +569,24 @@ static void bpf_map_release_memcg(struct bpf_map *map) } #endif -int bpf_map_alloc_pages(const struct bpf_map *map, gfp_t gfp, int nid, +static bool can_alloc_pages(void) +{ + return preempt_count() == 0 && !irqs_disabled() && + !IS_ENABLED(CONFIG_PREEMPT_RT); +} + +static struct page *__bpf_alloc_page(int nid) +{ + if (!can_alloc_pages()) + return try_alloc_pages(nid, 0); + + return alloc_pages_node(nid, + GFP_KERNEL | __GFP_ZERO | __GFP_ACCOUNT + | __GFP_NOWARN, + 0); +} + +int bpf_map_alloc_pages(const struct bpf_map *map, int nid, unsigned long nr_pages, struct page **pages) { unsigned long i, j; @@ -582,14 +599,14 @@ int bpf_map_alloc_pages(const struct bpf_map *map, gfp_t gfp, int nid, old_memcg = set_active_memcg(memcg); #endif for (i = 0; i < nr_pages; i++) { - pg = alloc_pages_node(nid, gfp | __GFP_ACCOUNT, 0); + pg = __bpf_alloc_page(nid); if (pg) { pages[i] = pg; continue; } for (j = 0; j < i; j++) - __free_page(pages[j]); + free_pages_nolock(pages[j], 0); ret = -ENOMEM; break; }