From patchwork Fri Sep 2 02:29:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12963567 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 8F104C38145 for ; Fri, 2 Sep 2022 02:30:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1E2DF8009E; Thu, 1 Sep 2022 22:30:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 190FF8008D; Thu, 1 Sep 2022 22:30:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 00AE48009E; Thu, 1 Sep 2022 22:30:26 -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 D8FDF8008D for ; Thu, 1 Sep 2022 22:30:26 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B60C61405D3 for ; Fri, 2 Sep 2022 02:30:26 +0000 (UTC) X-FDA: 79865566452.24.60497A1 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by imf08.hostedemail.com (Postfix) with ESMTP id 701F1160061 for ; Fri, 2 Sep 2022 02:30:26 +0000 (UTC) Received: by mail-pg1-f181.google.com with SMTP id x80so816543pgx.0 for ; Thu, 01 Sep 2022 19:30:26 -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:subject:date; bh=E8MAYkdVIQP0FJEM6EBF4l8Xg/qgqUjpGYemv3sFZKw=; b=WS87BarBUJUmdJvwHVOUJY8VhLnc3qqplexcN9euuSS2P7/7zhZnOc+Mdfx1nLfPp4 9WwWX1Zu+Wrt6tRcQXJGP+9X5wVNcqCKy7Ytx+0yvkRORBzJMZPRSfGfU5GKm/YFHg22 CbIxqWhkwiFvp6k3naiQouLi/puXIuV6j5aVe07KNixUP6a85g9RtfkPff9rzfmEO4Td iGUdUahKFP8/Nv+/ndoJhg+QwuPa20S42nMTGRzV3onP3zPd8NxcsLe87WY1UXyZtYHB 03hF7uKZlzxujTSM4oSCEJFzU7k8OW1wQlzMsrpPnHhpeL0IifVBg2WuiV3EgidomGr0 D6bQ== 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 :subject:date; bh=E8MAYkdVIQP0FJEM6EBF4l8Xg/qgqUjpGYemv3sFZKw=; b=eDObbLWKsn1SOWKw/ojky2uWn8/Jmh6RKo+M9/+6tfiC0Q2tTHHvHH3LwEEIK1IYj2 QvzfZw3t7nBis5lmkLU43ggS/DSR78SJBtlG8lHOKMiTnbmsH2ov0ap/JOwVRn4dM/rZ 3481LzaK6vdSET80TgyIyHfCGTI0tBLsaFDJJISU8PZbm8HKOi1fpwuLq4BIhvncBGHZ iK3A7OC1tBz8pRrVQ9RmQuhCtrBdWQ9Jfy0gxcOkyRHB/ENnYqjqk5etoiu/q/iNYnOI Wvpb/if0EXwDAFRDil4Mu6r8CtRZrbS2rfXI3knB8/YWvGD/bLOUxHGkbhnzyxQ/c6Q1 9aUQ== X-Gm-Message-State: ACgBeo08JERtnuiD0NngXXQO3XKB/vTIlGlbq5/zRehsqm2hWZiRiRWF +rVh85pbTgJt4zpc9PQ2Dew= X-Google-Smtp-Source: AA6agR7LqQuyAM3qmmIWpEDGymm/gkFBBR7fNB6mvH0YDlAlzs+wsP1hmzMC4vbU/wwv1JDDBSFZJA== X-Received: by 2002:a63:e25:0:b0:41c:30f7:c39c with SMTP id d37-20020a630e25000000b0041c30f7c39cmr28488997pgl.147.1662085825466; Thu, 01 Sep 2022 19:30:25 -0700 (PDT) Received: from vultr.guest ([2001:19f0:6001:50ea:5400:4ff:fe1f:fbe2]) by smtp.gmail.com with ESMTPSA id j4-20020a170902da8400b0017297a6b39dsm269719plx.265.2022.09.01.19.30.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Sep 2022 19:30:24 -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, tj@kernel.org, lizefan.x@bytedance.com Cc: cgroups@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-mm@kvack.org, Yafang Shao Subject: [PATCH bpf-next v3 07/13] bpf: Introduce new helpers bpf_ringbuf_pages_{alloc,free} Date: Fri, 2 Sep 2022 02:29:57 +0000 Message-Id: <20220902023003.47124-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220902023003.47124-1-laoar.shao@gmail.com> References: <20220902023003.47124-1-laoar.shao@gmail.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662085826; 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=E8MAYkdVIQP0FJEM6EBF4l8Xg/qgqUjpGYemv3sFZKw=; b=WC4dGc6tZiPeNGvOITroutsiHtv0CIeUjfCUw5jFdjhm0vueB6xYcy+EKBfnPxfGZlwZZK jA+dPVKSjKzMHD56OCI1hWTppToVEx0fWZUlTKdG2PWMb6Kvpq+lhdhcd+WYOc0adFeCIE 42eLfcpQFTCZZsw2TcgFgnLVcSyMtA4= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=WS87BarB; spf=pass (imf08.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.181 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662085826; a=rsa-sha256; cv=none; b=hDYvJt1x5yS/Es5JQwdN81CRI5Y0vHfE8NNlq9WqjAethHlDjf+Vs/L2lDarCdcMI1W+LJ ed0iaRj//7L5zphldJl1ueAB/KR08bS3Ow99T565n8nUwGOuPv6w+LDlBu9u1rBAzgxxWV gW3k2VUL+6DudUpYNAEU2QMqs/vaA8U= X-Stat-Signature: dhyma5um56y6joo9556ukgn6bni34wfm X-Rspamd-Queue-Id: 701F1160061 Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=WS87BarB; spf=pass (imf08.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.181 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam05 X-Rspam-User: X-HE-Tag: 1662085826-648081 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 Acked-by: Andrii Nakryiko --- 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)