From patchwork Mon Dec 12 00:37:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13070699 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 49D0AC4332F for ; Mon, 12 Dec 2022 00:38:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E196F8E0009; Sun, 11 Dec 2022 19:38:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DC9508E0002; Sun, 11 Dec 2022 19:38:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C4CC58E0009; Sun, 11 Dec 2022 19:38:09 -0500 (EST) 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 B5CD88E0002 for ; Sun, 11 Dec 2022 19:38:09 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 92AE01A04E2 for ; Mon, 12 Dec 2022 00:38:09 +0000 (UTC) X-FDA: 80231792298.01.A4666F5 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf23.hostedemail.com (Postfix) with ESMTP id E839D140004 for ; Mon, 12 Dec 2022 00:38:07 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=QwHnrYU5; spf=pass (imf23.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.52 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=1670805488; 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=b7mLIzH9QGa/Vp7L1frOgw2MpIN2USRO4iU649EtnEc=; b=5a0cNpFpVLhLzPEo80939//xwFn4A5r9Y9XyuOFis7tsOdItXwhYm95VdlLlJ2FaD4iCBT bcR4M3t+BfKFrqiwC7b0Il3aW98UPnHaPFjfvWld/jp80jjUDbaxtok6V1HkeO/D9c0Qa9 oLklsBofqrMEYe9Xh7zcbrz7ejgN/2o= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=QwHnrYU5; spf=pass (imf23.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.52 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=1670805488; a=rsa-sha256; cv=none; b=znxpo2nAfDisnzsgjyLPb9hAdi3OejlYs+TRM37sKDb3AioFY6KhyUYfINAIsxov4/FPdr JRQfADo86fsn7PAE0hpNmMSeeC5WkOv4EeGqAuKPKWKG09y0eiE7nVpekXfpK+asxEDt6a Y7q+1XP1CjxOzBPv8P/FYuWe2AnZ1rg= Received: by mail-pj1-f52.google.com with SMTP id u15-20020a17090a3fcf00b002191825cf02so10476019pjm.2 for ; Sun, 11 Dec 2022 16:38:07 -0800 (PST) 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 :message-id:reply-to; bh=b7mLIzH9QGa/Vp7L1frOgw2MpIN2USRO4iU649EtnEc=; b=QwHnrYU5dlXPi4mKazfawl3O2vLOVNWdtMU7R0MIhOaiv6hPJkTBBHO24XgIPdO/Gf 9na56WI++Cb+a/hnv0W5d43A4c9pZsDP0T6C2ZZl5O2bTxtqze9EQPUi7+XEuVneB6jT kticj3IJQ4XUgA+Z1YDssULNPsBFCtJzYxLaHCITVnk6zPI61pwxHeuvswRzdZilRwc0 G8WrVsH3guWmfGM4IyiM+dMvuNvxlqlKIxYxIRRqC/yJpCDsrbeCXRRYbki05brj76EA queFVstoRXm+39NEjEKp+U/sBWNt4j/jCl8AAQzPQO6p0E07EggCIzSsaOAdHCHnNxpH bSiQ== 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:message-id:reply-to; bh=b7mLIzH9QGa/Vp7L1frOgw2MpIN2USRO4iU649EtnEc=; b=Qtr9L68D2WZ9A8fMhVqesLJyZwzM4hxtpHYPG9Ylni1z/wHH5YCZBNkLE4ir8xWM9C dqZLLIeC455P/nyR+uV+YN4w/Cyp/aYwhPt8cl15FCzT7oHMZ84Wjvm37sJmlxp5CaoU HwrR/e47z0Z/fTODpTKO2AV+vDiqGFkNoboa1vMX1YxKYD58MsMFuhY4cJVFjjzUOqeF x7z3iBVn1jne4UO9rvZ9fMKQT8Gc8WxCGxS4KxMZaUvMRC1xm2jnylnFN7tGsz/7+sTQ AqLlIxt1OrucJ1R5vE1pT6cnWCIDBuOICCDV/T/AzC8CU80hLayCujQw29UQlYJofLVR LVrg== X-Gm-Message-State: ANoB5pmf7UpIj0oJsratbjKnUGfgqhQHShLPh2YN+Evw3OV4EGgKgXvH BOX93ohi7oP15nIkzw1AJ0I= X-Google-Smtp-Source: AA0mqf5z6xRPI9ZAMyGtUx6mm4rODaDI03F7bbR1btk/8oT+AjpT7zlHoWOWr51yN5JyyqN1xQqw+g== X-Received: by 2002:a17:902:ab54:b0:189:6bda:e98f with SMTP id ij20-20020a170902ab5400b001896bdae98fmr13586506plb.58.1670805486992; Sun, 11 Dec 2022 16:38:06 -0800 (PST) Received: from vultr.guest ([2001:19f0:7002:8c7:5400:4ff:fe3d:656a]) by smtp.gmail.com with ESMTPSA id w9-20020a170902e88900b00177fb862a87sm4895960plg.20.2022.12.11.16.38.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Dec 2022 16:38:06 -0800 (PST) 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, tj@kernel.org, dennis@kernel.org, cl@linux.com, akpm@linux-foundation.org, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, vbabka@suse.cz, roman.gushchin@linux.dev, 42.hyeyoo@gmail.com Cc: linux-mm@kvack.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 6/9] bpf: Introduce new helpers bpf_ringbuf_pages_{alloc,free} Date: Mon, 12 Dec 2022 00:37:08 +0000 Message-Id: <20221212003711.24977-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221212003711.24977-1-laoar.shao@gmail.com> References: <20221212003711.24977-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: cuqszgee3bojqjqbmtnqxhpsx4m5sfbx X-Rspam-User: X-Rspamd-Queue-Id: E839D140004 X-Rspamd-Server: rspam06 X-HE-Tag: 1670805487-879977 X-HE-Meta: U2FsdGVkX198Oh/8GzXUByH746boG87Ok5IiAon6cN7nJY+oDDCVj22xfsA629cJssKUE3BVyelJUN/7DdGMVpl1OQ0UZxGrgZN4dMw9xlEzEwsXWZ76u4SOvcbymrizVOvRjI9dZn3tsZcb+hKcajwS6rU9MRhW1aV1zQwmzIb7aXvHfRwYM80LPp+mEQVek6RdeER7AneQuKojEw/QQS3ueF0ehsaUkKbcMINc7UUq78bgP03eDBn5I7Gffll88KxoLvY7clqxljiWoS1e1cFu8UnuSRogxxde6EgQsjqMeCeXQ6yomgS8REwMbdlx3ZWFwRpDNyH5k/7BsWLdSbeoQdUrRSf8KGC110baf+MH/JHBEiTeR1sfP9c9xr0jAwSnyy6s0Uk79aHKgi/JNTmU3tp6zb4ThQ8Gu2YVgY3H4VSM2+D43RpR2CS+QiVC//2o7lHehXRh+S0mOSacRrdbG6kKa+rVzFUJQuKLUQkmByV8bHae7BCfJN94xRBMJufw/w6z57UZzqLJBqZ2vxLyxei5yZb2DPWFeFqWZkfmxNKBPgvLqnoJDvTzEM0cE69mT3YSuisYInd0cKHZwt3LgvxVbVGlrb8oqy0Xi378LNs1EEztGIqFPomcp19xag+5z6Iarw6Ax6cy9N823mJpHIaeuwm/M2rAmNgcp6vCtlqrNpgdcyZGzdHhPGJRjNlHiFTjNncaOleUtXv1zV9AeEzsykNL5u65pCpz+yslh8zZOWP0TbD97U5LyDoiw5IQOaLTuAs1oCophxPmloiG+ObWNpU1j9PKplOhujHxGx2tAou5LrLwHKdsLgei0uCJR5I2HN0w4aD0HfuD2mxLs0R06ruaem6T6rRTj78b5rNkRJ+IXEe6DnlmtMa1F23AWaM3qAEfCfHpbzAOF8uY6PQtcQoudvDOl+0eubVLIIxi3A5rWV99FyXDwsosNGpoxAu4yUoTfZBxYJp WwsTJ7k3 f5aHBvjSj/GAtzzm2drwztgZxNcWlI9urVHF5vFNPoqF8EKV+FOTAjRxbNJGL7eiDsY73TxnqKMnYtwZrm9adPoGIUD0MQ3tTGabfCMIVURgAiNROACPjC2+HIde0y8/tb0fOA3Q7P3FXwdIol2FlJRbu1vp021x0AA+gGvpo30tl1/5JepkaFy2L5+zAuATAMIpbpUgxOMx7T8y3OfVMIn5YtUbyEfOTGq7XW1b9v52hFXo0sf9YtV3gzqkOPbx7dg9JhCTxZOO/LT8ZBnrCxkRh7w== 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 | 71 +++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c index 80f4b4d88aaf..3264bf509c68 100644 --- a/kernel/bpf/ringbuf.c +++ b/kernel/bpf/ringbuf.c @@ -92,6 +92,48 @@ 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); +} + +static struct page **bpf_ringbuf_pages_alloc(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 page **pages, *page; + int array_size; + int i; + + array_size = (nr_meta_pages + 2 * nr_data_pages) * sizeof(*pages); + pages = bpf_map_area_alloc(array_size, numa_node); + 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; + } + + return pages; + +err_free_pages: + bpf_ringbuf_pages_free(pages, nr_pages); +err: + return NULL; +} + static struct bpf_ringbuf *bpf_ringbuf_area_alloc(size_t data_sz, int numa_node) { const gfp_t flags = GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL | @@ -99,10 +141,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 @@ -121,22 +161,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); + pages = bpf_ringbuf_pages_alloc(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) { @@ -146,10 +175,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); return NULL; } @@ -219,12 +244,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); + bpf_ringbuf_pages_free(pages, nr_pages); } static void ringbuf_map_free(struct bpf_map *map)