From patchwork Fri Jul 29 15:23:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12932540 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 500C3C19F2A for ; Fri, 29 Jul 2022 15:23:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E51A06B0081; Fri, 29 Jul 2022 11:23:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E02486B0082; Fri, 29 Jul 2022 11:23:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C559C8E0001; Fri, 29 Jul 2022 11:23:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B3CDB6B0081 for ; Fri, 29 Jul 2022 11:23:43 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 88CF98118F for ; Fri, 29 Jul 2022 15:23:43 +0000 (UTC) X-FDA: 79740507126.11.5118207 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) by imf14.hostedemail.com (Postfix) with ESMTP id 1EFC81000CA for ; Fri, 29 Jul 2022 15:23:42 +0000 (UTC) Received: by mail-pj1-f54.google.com with SMTP id q7-20020a17090a7a8700b001f300db8677so5565753pjf.5 for ; Fri, 29 Jul 2022 08:23:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jAUWseH3fRr8riszGReq2qXDLxnVVqoEg86tgbmI8NE=; b=fmrI2QyU7IHbIWzCz94cxxmolN8UgJDWb3QaJ65Tp00KLnhEL9aZ2FVu4/xeQFvkxm Vf6GZuOm6d/T0FvqGvBzpq/iwTAPES9t1kDoGxhPm8QpjxLmP/5UJGNNb0u+Sw5CvaJU w3rQTlHgYH+YBmFCql1y8JfODcl23WsL8RFZh1+IX1gF88xfPVACbPoI7yeFykfu30rd ds2nhZtOma9CpW8KYv3890dSigOkV30IpMYMN9A/3owa6ayTK+XMB4ZF0bjQO7p0A0kv 6ONIyiiNrA6VEQXauRHRTLAUlWQCeR3w+k0iKKKdb6wLCGF+rBN/rkb2Yv9HkOoqjBOs 25yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jAUWseH3fRr8riszGReq2qXDLxnVVqoEg86tgbmI8NE=; b=UeBorp+CysLyIFoZt5mBQ6a8gqCFDuhz7WfHvB6QU/pxu8FgWap49t0Ma3JRF371bk OUjug/ISrYok+eXutvaPPM9+F3OLXPFu2xN60wJLxF9YIuZtAq60aXq2oORaQbeuItGt MdXwCUrm4CBPsHihjEwtXMbGvRRiIGi4QLt8BHEL3Dt62opHyV2K4xlq76myrVRg+gmn sILlbM1BpENuPcmBN9GWGfFsofOXmhPE0TeEqqLfnZQ8+xrsufGx2PAQN6VbGIHAYJno vdSuSbi6JCU4Xd40AYso65opczLpGtz6ZSzhZNmMMWL1DPTmYQyotzSasdwh5opWIo/f f+gQ== X-Gm-Message-State: ACgBeo0hOKtSgw6QZb2ujwyqhwl2q7Sa/4iGW3kuouBze8hU+EVrnCdy ik44OG6Y53rpOtHxbwq7nCs= X-Google-Smtp-Source: AA6agR73Gj5Asvp4W0xUgGklQRFVIg49NlQKP3kuhqIKi/rewhM/3O8FzmBipmh7MLHSJUTpYlMo1A== X-Received: by 2002:a17:903:11c9:b0:16b:8293:c5a1 with SMTP id q9-20020a17090311c900b0016b8293c5a1mr4435414plh.72.1659108222266; Fri, 29 Jul 2022 08:23:42 -0700 (PDT) Received: from vultr.guest ([2001:19f0:6001:2912:5400:4ff:fe16:4344]) by smtp.gmail.com with ESMTPSA id b12-20020a1709027e0c00b0016d3a354cffsm3714219plm.89.2022.07.29.08.23.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Jul 2022 08:23:41 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, songmuchun@bytedance.com, akpm@linux-foundation.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-mm@kvack.org, Yafang Shao Subject: [RFC PATCH bpf-next 10/15] bpf: Use bpf_map_pages_alloc in ringbuf Date: Fri, 29 Jul 2022 15:23:11 +0000 Message-Id: <20220729152316.58205-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220729152316.58205-1-laoar.shao@gmail.com> References: <20220729152316.58205-1-laoar.shao@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1659108223; a=rsa-sha256; cv=none; b=7WC47il8Qeu6oYqrCw9wrr0796kFjQ/NL4CFtTH7KlFqY1Ct4Jg+ciSP9Or+cwKxlqY9H/ XnhacLoDeNkcj02lqBwjtw/y/0TWcCb4/+B1veT2d9FVEx+xPS21PO+fQuC8e9mSy+t9A3 yDrDjdcBnBYWmiCOLaB+QRoz4LcYQSg= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=fmrI2QyU; spf=pass (imf14.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.54 as permitted sender) smtp.mailfrom=laoar.shao@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=1659108223; 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=jAUWseH3fRr8riszGReq2qXDLxnVVqoEg86tgbmI8NE=; b=a/njWthfbbQGBOsMTadIMlZ9ZYwa3fQN34JWal7RTpyO1WeBJThr+IAZ1x0syVbc75bf2z 9tWmXHOx2OakXeivaPEv2D1dHfF4sC/ee0gUnyoQe72tQColoIooQbafEhx5BBrt+OxWkl UeNQwIQN6ktE3iBzBmwUl6TTv6Gu7ao= Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=fmrI2QyU; spf=pass (imf14.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.54 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspam-User: X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 1EFC81000CA X-Stat-Signature: brtb1ttdjcmomg8oj3b4jiwsie4iy1qs X-HE-Tag: 1659108222-213320 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: Introduce new helper bpf_map_pages_alloc() for this memory allocation. Signed-off-by: Yafang Shao --- include/linux/bpf.h | 4 ++++ kernel/bpf/ringbuf.c | 27 +++++++++------------------ kernel/bpf/syscall.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 711d9b1829d4..4af72d2b6d73 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1638,8 +1638,12 @@ void *bpf_map_container_alloc(u64 size, int numa_node); void *bpf_map_container_mmapable_alloc(u64 size, int numa_node, u32 align, u32 offset); void *bpf_map_area_alloc(struct bpf_map *map, u64 size, int numa_node); +void *bpf_map_pages_alloc(struct bpf_map *map, struct page **pages, + int nr_meta_pages, int nr_data_pages, int nid, + gfp_t flags, unsigned int order); void bpf_map_area_free(void *base); void bpf_map_container_free(void *base); +void bpf_map_pages_free(struct page **pages, int nr_pages); bool bpf_map_write_active(const struct bpf_map *map); void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr); int generic_map_lookup_batch(struct bpf_map *map, diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c index 7c875d4d5b2f..25973cab251d 100644 --- a/kernel/bpf/ringbuf.c +++ b/kernel/bpf/ringbuf.c @@ -63,15 +63,15 @@ static struct bpf_ringbuf *bpf_ringbuf_area_alloc(struct bpf_map *map, size_t data_sz, int numa_node) { - const gfp_t flags = GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL | + const gfp_t flags = GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN | __GFP_ZERO; int nr_meta_pages = RINGBUF_PGOFF + RINGBUF_POS_PAGES; int nr_data_pages = data_sz >> PAGE_SHIFT; int nr_pages = nr_meta_pages + nr_data_pages; - struct page **pages, *page; struct bpf_ringbuf *rb; + struct page **pages; size_t array_size; - int i; + void *ptr; /* Each data page is mapped twice to allow "virtual" * continuous read of samples wrapping around the end of ring @@ -95,16 +95,10 @@ static struct bpf_ringbuf *bpf_ringbuf_area_alloc(struct bpf_map *map, if (!pages) return NULL; - for (i = 0; i < nr_pages; i++) { - page = alloc_pages_node(numa_node, flags, 0); - if (!page) { - nr_pages = i; - goto err_free_pages; - } - pages[i] = page; - if (i >= nr_meta_pages) - pages[nr_data_pages + i] = page; - } + ptr = bpf_map_pages_alloc(map, pages, nr_meta_pages, nr_data_pages, + numa_node, flags, 0); + if (!ptr) + goto err_free_pages; rb = vmap(pages, nr_meta_pages + 2 * nr_data_pages, VM_MAP | VM_USERMAP, PAGE_KERNEL); @@ -116,8 +110,6 @@ static struct bpf_ringbuf *bpf_ringbuf_area_alloc(struct bpf_map *map, } err_free_pages: - for (i = 0; i < nr_pages; i++) - __free_page(pages[i]); bpf_map_area_free(pages); return NULL; } @@ -189,11 +181,10 @@ static void bpf_ringbuf_free(struct bpf_ringbuf *rb) * to unmap rb itself with vunmap() below */ struct page **pages = rb->pages; - int i, nr_pages = rb->nr_pages; + int nr_pages = rb->nr_pages; vunmap(rb); - for (i = 0; i < nr_pages; i++) - __free_page(pages[i]); + bpf_map_pages_free(pages, nr_pages); bpf_map_area_free(pages); } diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 4f893d2ac4fd..5c13782839f3 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -559,6 +559,47 @@ void bpf_map_container_free(void *container) kvfree(container); } +void *bpf_map_pages_alloc(struct bpf_map *map, struct page **pages, + int nr_meta_pages, int nr_data_pages, int nid, + gfp_t flags, unsigned int order) +{ + int nr_pages = nr_meta_pages + nr_data_pages; + struct mem_cgroup *memcg, *old_memcg; + struct page *page; + int i; + + memcg = bpf_map_get_memcg(map); + old_memcg = set_active_memcg(memcg); + for (i = 0; i < nr_pages; i++) { + page = alloc_pages_node(nid, flags | __GFP_ACCOUNT, order); + if (!page) { + nr_pages = i; + set_active_memcg(old_memcg); + goto err_free_pages; + } + pages[i] = page; + if (i >= nr_meta_pages) + pages[nr_data_pages + i] = page; + } + set_active_memcg(old_memcg); + + return pages; + +err_free_pages: + for (i = 0; i < nr_pages; i++) + __free_page(pages[i]); + + return NULL; +} + +void bpf_map_pages_free(struct page **pages, int nr_pages) +{ + int i; + + for (i = 0; i < nr_pages; i++) + __free_page(pages[i]); +} + static int bpf_map_kptr_off_cmp(const void *a, const void *b) { const struct bpf_map_value_off_desc *off_desc1 = a, *off_desc2 = b;