From patchwork Wed Aug 10 15:18:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12940671 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 B1B83C19F2A for ; Wed, 10 Aug 2022 15:19:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 463636B0072; Wed, 10 Aug 2022 11:19:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 325A56B0080; Wed, 10 Aug 2022 11:19:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1031C8E0001; Wed, 10 Aug 2022 11:19:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E931D6B0075 for ; Wed, 10 Aug 2022 11:19:06 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C6A6A1C6D01 for ; Wed, 10 Aug 2022 15:19:06 +0000 (UTC) X-FDA: 79784041092.08.F6B7A7B Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by imf10.hostedemail.com (Postfix) with ESMTP id 50C8CC0177 for ; Wed, 10 Aug 2022 15:19:06 +0000 (UTC) Received: by mail-pj1-f43.google.com with SMTP id 15-20020a17090a098f00b001f305b453feso2515792pjo.1 for ; Wed, 10 Aug 2022 08:19:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=wfVdiCm3ki6hY+6umh5GbzZ8nNKi0kDY1+Bd1ej/jM8=; b=UdXfowzBOTp4IdbmLV0WXeQAcY/vsJ/7uo+iK0cbB9dRpy21/lcf/YanTi3shCIAU0 Q8rfIZTnd0sFO2eOAJg8bQRhBlA5UyajIW7JKq6kChqbxIqBjxer1gwpkQzfLIAI5B/5 VUeN+qVeFxShxqCMoW4Wr+biXvlCI+ndPPS5CnKurUdGwbSf4MtK3rdRBQ45hNXX9V5z JAQTTSrfQv7hQ3PFf8Jj36ClWIoJ5YqlWfCZdHmguctdIvHlIOMV3/PBphph5bd/SDbY GC2bpylDRY7EkNbki9of3Gdh3iWuOlFezWsDa+jJ5+bZZ4AXwuCNcMB0hstIpQyEK8cV +YZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=wfVdiCm3ki6hY+6umh5GbzZ8nNKi0kDY1+Bd1ej/jM8=; b=SFUmyBKxZeRA8nbxY9Ln1bcTtIKrr2PknGa9l01tdfmHMk01MZGbUXwmceJ0KCrFzT lLKfSES28COD72qiw0jQcNp8v2tGvD/Vyo6k7AvugX9eMUkJOHwAOHsafwqQIraiDOtO oRPtYBxWYPDn9fiTN5yQm1+mk9P/rO74nBkYeN/5NZjImSjYUfNO2AjaVM1nZ0EzxoQN mMa7nNJ3r96yWQwAkIH+Gnr4s5N1tmV8w+ymmiao2ioMVN1Sg4W8+t68bA8F77oxhP3d YnTMM3rmhhSqinJT7WZByRdyhcerk4hbXYkRbOnUt5a6t1/jWtirXGM7kY7xOfGQEjOy kuvA== X-Gm-Message-State: ACgBeo0iHXMD5gUui+Q9TLl2iwNMt7b0ACgskNGv7RHLx4yi/Ev0wTWZ gcJM0Gx8+I6eTwsp+v5eT1M= X-Google-Smtp-Source: AA6agR6Zk/PLReQ+MJ1KpoaibPWZe0SiORmjhQNLojFx1LN3y6d44aIAK00GwGL2TEAU0VSXMfvqcQ== X-Received: by 2002:a17:902:e80c:b0:16f:8663:6c57 with SMTP id u12-20020a170902e80c00b0016f86636c57mr28145450plg.102.1660144743943; Wed, 10 Aug 2022 08:19:03 -0700 (PDT) Received: from vultr.guest ([2001:19f0:6001:5c3e:5400:4ff:fe19:c3bc]) by smtp.gmail.com with ESMTPSA id 85-20020a621558000000b0052b6ed5ca40sm2071935pfv.192.2022.08.10.08.19.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Aug 2022 08:19:03 -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 , Andrii Nakryiko Subject: [PATCH bpf-next 10/15] bpf: Introduce new helpers bpf_ringbuf_pages_{alloc,free} Date: Wed, 10 Aug 2022 15:18:35 +0000 Message-Id: <20220810151840.16394-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220810151840.16394-1-laoar.shao@gmail.com> References: <20220810151840.16394-1-laoar.shao@gmail.com> MIME-Version: 1.0 ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=UdXfowzB; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660144746; a=rsa-sha256; cv=none; b=1v5IgQGm1/jmcmV8oKJtmzseXLqYziZiXIIyKxTLXG7hkuN19XTjFH+mZbcsKXzMUiPb6Z PPiEJ/60KCz5urpw3z6nNWNzleETkiVgY9dMxmnbPYw1C8UrgiuR2RLBFnTyvNcXScBHUo +WzMmDAPVKm7QpiIxG1gEDfRVU4bHOA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660144746; 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=wfVdiCm3ki6hY+6umh5GbzZ8nNKi0kDY1+Bd1ej/jM8=; b=kc3jZMLLBGUjqbzqY941cf5iZH4M95GWjGhDM5H/5A/KHNwOvoUn4VL44wtCZqpvERDqFu ifL36B0TDk5I1XU35lyVbqfZRwEUfA88jBCNG0wefFnoP8pNO2vQICte8g/2zOpFAG4Tjz jFYMFvxYOps1Du7J+6o4e3W1cVYpLJw= X-Rspamd-Server: rspam10 X-Rspam-User: Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=UdXfowzB; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com X-Rspamd-Queue-Id: 50C8CC0177 X-Stat-Signature: 8ighy4fsspxifmjaouwt1qnz5857i611 X-HE-Tag: 1660144746-521428 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: Allocate pages related memory into the new helper bpf_ringbuf_pages_alloc(), then it can be handled as a single unit. Suggested-by: Andrii Nakryiko Signed-off-by: Yafang Shao --- kernel/bpf/ringbuf.c | 80 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 24 deletions(-) diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c index 5eb7820..1e7284c 100644 --- a/kernel/bpf/ringbuf.c +++ b/kernel/bpf/ringbuf.c @@ -59,6 +59,57 @@ struct bpf_ringbuf_hdr { u32 pg_off; }; +static void bpf_ringbuf_pages_free(struct page **pages, int nr_pages) +{ + int i; + + for (i = 0; i < nr_pages; i++) + __free_page(pages[i]); + bpf_map_area_free(pages, NULL); +} + +static struct page **bpf_ringbuf_pages_alloc(struct bpf_map *map, + int nr_meta_pages, + int nr_data_pages, + int numa_node, + const gfp_t flags) +{ + int nr_pages = nr_meta_pages + nr_data_pages; + struct mem_cgroup *memcg, *old_memcg; + struct page **pages, *page; + int array_size; + int i; + + memcg = bpf_map_get_memcg(map); + old_memcg = set_active_memcg(memcg); + array_size = (nr_meta_pages + 2 * nr_data_pages) * sizeof(*pages); + pages = bpf_map_area_alloc(array_size, numa_node, NULL); + if (!pages) + goto err; + + 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; + } + set_active_memcg(old_memcg); + bpf_map_put_memcg(memcg); + + return pages; + +err_free_pages: + bpf_ringbuf_pages_free(pages, nr_pages); +err: + set_active_memcg(old_memcg); + bpf_map_put_memcg(memcg); + return NULL; +} + static struct bpf_ringbuf *bpf_ringbuf_area_alloc(size_t data_sz, int numa_node, struct bpf_map *map) { @@ -67,10 +118,8 @@ static struct bpf_ringbuf *bpf_ringbuf_area_alloc(size_t data_sz, int numa_node, 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; - size_t array_size; - int i; + struct page **pages; /* Each data page is mapped twice to allow "virtual" * continuous read of samples wrapping around the end of ring @@ -89,22 +138,11 @@ static struct bpf_ringbuf *bpf_ringbuf_area_alloc(size_t data_sz, int numa_node, * when mmap()'ed in user-space, simplifying both kernel and * user-space implementations significantly. */ - array_size = (nr_meta_pages + 2 * nr_data_pages) * sizeof(*pages); - pages = bpf_map_area_alloc(array_size, numa_node, map); + pages = bpf_ringbuf_pages_alloc(map, nr_meta_pages, nr_data_pages, + numa_node, flags); 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; - } - rb = vmap(pages, nr_meta_pages + 2 * nr_data_pages, VM_MAP | VM_USERMAP, PAGE_KERNEL); if (rb) { @@ -114,10 +152,6 @@ static struct bpf_ringbuf *bpf_ringbuf_area_alloc(size_t data_sz, int numa_node, return rb; } -err_free_pages: - for (i = 0; i < nr_pages; i++) - __free_page(pages[i]); - bpf_map_area_free(pages, NULL); return NULL; } @@ -188,12 +222,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_area_free(pages, NULL); + bpf_ringbuf_pages_free(pages, nr_pages); } static void ringbuf_map_free(struct bpf_map *map)