From patchwork Thu Jan 12 15:53:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13098271 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 7C358C61DB3 for ; Thu, 12 Jan 2023 15:53:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C04B28E0009; Thu, 12 Jan 2023 10:53:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B8C9D8E0001; Thu, 12 Jan 2023 10:53:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B9088E0009; Thu, 12 Jan 2023 10:53:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 853B98E0001 for ; Thu, 12 Jan 2023 10:53:45 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 50134C0169 for ; Thu, 12 Jan 2023 15:53:45 +0000 (UTC) X-FDA: 80346592410.15.4EAD2DF Received: from mail-qt1-f173.google.com (mail-qt1-f173.google.com [209.85.160.173]) by imf27.hostedemail.com (Postfix) with ESMTP id 95C9540017 for ; Thu, 12 Jan 2023 15:53:43 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=V5AonCl6; spf=pass (imf27.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.160.173 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=1673538823; 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=TBH19B7tt8c2ZqgTL5MGs4kQ+EgYdVrIXT4FgqCf58E=; b=qOybszHICS/ACsljKiZr4jBCp4YQPd41aIsGXIXiL6nIzR4wcFjc4KhzdTjrF3M9/W2qjZ jY3rider7CM76EHnGAckMMiNAlStwiyOuK0fmdIfvE71HGnfWZYbbLKum2blA7zmZGCu2f 5AJVEtdcSAeVDP5crtf3ZOIJJcfAvfQ= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=V5AonCl6; spf=pass (imf27.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.160.173 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=1673538823; a=rsa-sha256; cv=none; b=283YeEHjbQWBA4TKL0taCNDTQWxwJCHeOX35pKjOIuWlbZDsqSi2Vf1i0T5XPlvYdhKXlQ WAibHmDKGLnwUdFJyfkDhUA7dnYC7qRYJkc8wZE5E+SLbMDIe+WyCtNUqoVpEA++2v9kTi PzsDJ6A2CIMV7M1qxksLOG4L5N3N2o4= Received: by mail-qt1-f173.google.com with SMTP id bp44so16890214qtb.0 for ; Thu, 12 Jan 2023 07:53:43 -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=TBH19B7tt8c2ZqgTL5MGs4kQ+EgYdVrIXT4FgqCf58E=; b=V5AonCl6GtoPHEQDYaUYetOY7qBor5eB4wJnYjHi6v81IJ6LFl63Vb7qTQ6fZ62GmJ FZ3VHrJc1gdN1TOHjP2hoB35KssqBdVaaZbEQZ/E5zivkI0c5eo7XBU1gZCICr7Fpmmx tK8mbPvFPAdW5NE9w07N1Eta2988UZDI9ppynyPtdFQ5OYtkpv0D4+CkEJpBYNYN+qyQ uuqqCAyOCmPlXT/hg59VPHxvJMezaeIia6wBeOewTU0JIGj73v/uGM99APwnJyPb1MAa 6tlQS2nILNzaj0VNoUnSKxnsveyfjiyJpOyMNF4QnV3ia2uczbf/7pWYoLPwNbm+KQcS i0Fg== 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=TBH19B7tt8c2ZqgTL5MGs4kQ+EgYdVrIXT4FgqCf58E=; b=YHPapSTWUsRjces3+WTY67Aw1MovJC2TssyrBQUH21XqyY4mBVjLO+z2JKxuEpnnmP Df1wbtogLEEZ4x2hTrJwmeQ22Tg/z64lwHNRfN2zLMET5Ifl/q+NHwkWF8aGB557Xbmz ubMzjrvOG1Yv13nR/PRFshQLnFAMJ9uiLtR7G+xzNhH0KwKk+EqVcJmPQD23PIDz/+Lo pt/7tckU2FQ1C93hGp4SdNKR/cn6uo3RfmavEueFug8MmI5KhbnQqD0wxPx52D/bFb7B vU1RIumC5ebB/5NxYdsRVyrke3vVpVDkKf0iFcCq+1LNa14h9xtt9L4KZdlra1A8dx+O tXDw== X-Gm-Message-State: AFqh2kovNyaenagDLR89+mrpqZ5hSk0OC+hDzSu/1k90rNgncGFQ/lA9 xNsIjz9/dwems3DdnDDlM/w= X-Google-Smtp-Source: AMrXdXunMQbXCe3PjTlqR622vY/D62Blbxfq9VYi3TCPRza8jAekLqfz3F3c2jSj1Xlfyg52SWyWbw== X-Received: by 2002:ac8:7957:0:b0:3ad:903d:3ed4 with SMTP id r23-20020ac87957000000b003ad903d3ed4mr19548488qtt.59.1673538823178; Thu, 12 Jan 2023 07:53:43 -0800 (PST) Received: from vultr.guest ([173.199.122.241]) by smtp.gmail.com with ESMTPSA id l17-20020ac848d1000000b003ab43dabfb1sm9280836qtr.55.2023.01.12.07.53.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jan 2023 07:53:42 -0800 (PST) From: Yafang Shao To: 42.hyeyoo@gmail.com, vbabka@suse.cz, 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, roman.gushchin@linux.dev Cc: linux-mm@kvack.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next v2 07/11] bpf: introduce new helpers bpf_ringbuf_pages_{alloc,free} Date: Thu, 12 Jan 2023 15:53:22 +0000 Message-Id: <20230112155326.26902-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230112155326.26902-1-laoar.shao@gmail.com> References: <20230112155326.26902-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: pukwiuxcz884ye5ycyz59nsx75eypf4u X-Rspamd-Queue-Id: 95C9540017 X-HE-Tag: 1673538823-95030 X-HE-Meta: U2FsdGVkX19e1tcqEQhrfIDrMGay7gYkY3tKtRIWZgA8rh2bWNn+MrEZxbZtkcRi+3Y0zI6vAlLr8MuLmrMd2GBjYC94r3E0SmG7rGFx2lvWddQ74MmOm+PEP9oL7HSUu7KtOhOuZYz1eK0TdhIC1qBGsUuY+bGt/4QAjCh7EhQYjX5r29EwvMCGX/GH5GgTeuUDzr3d4oJ4SGn4uKkzYMEDGFKGIWMooGqMZN/eZEnC/T8mkCldXhQUaE8tzTPx1KGJ90zbNmTynTvbgkQkVDLEobyxoDmWIVHx80R2hyFPPhI+qqKZX/4Jw+B5uzHpn4o2oMwsYYvILzu0cC+0iK02/I/pW4I6eCE1/tEKNgoWPtV+/BbB0Rknnpy2HbOSAPX24mKz/2EENH60IQtYrwyHSg6NosTh7lWIlvQVsCvc6prFitCdlzHC4peXSfqhCjeb19xRGasl+NUtMm50I9y6mvTFA23dKGgQ9PXvCO6n6VckQHTABXUWq1zl79uVr24/DlYcPEBDIHfJUKdaJwwOMDzSwLQleB8Q3y39/gCF7bQa0/qu+E/3aOLYCPb2Vc6ux0zc3RJ5uNxjqgNnjwThdRZ9N0Ge+OFLGHUgw/O+2JKwtk2YClXH0fEJpZnMJVrGnSWUX24NEHLVYFP8zbmSRltoTmhNkfiNDPbhDVRB54c7mfTFBLaLqYHTW00GQVL5imjTmwvXjGftC/F79ibyxOtY7s6WGgDKXkELL059N8mhIRaN82y07lSmYAEUN3hlBtKlU1gNZCR7uGy17/ZviqHiaxETKKn753SNUU6f5r7MzgPW3R8uJL8YWTzpL8ggtqjfM+keT4/ImgW1biR4iikhetcjz2g5XQtWr29AGq7Tkz+o6guD5wXxuplFR1yISgUA2sQogETRcbFXONm2KIoVFVNlmutGP4lAqYTR5I+5GDuSU1VQ0wi9SDU9i1UArZqbzKIStqW5VUe T035wk6h m6ClkIRJxwxcUdqu+wzGZkMn7DJslGA2PJQ/HSJvYECm6VDlFuf3ExwrFV/JzpejCKAC7CLRfTt063TsEab8oF91AFhYMdVhmtEWd0KqbcXwT/LAZMA76OquqdWsi1PeY5SDe2d6yb2lnW8jL5qn5wGv51FtMIpHnnIaaKlNAoVLl/BK9VByfuxujuqA2dEf68L0RcUbpRPSTMQRxDqA8pHhBd68UFFFZwUYNnd+tHaINlNGuwdVIOE+f/Fxww9vAiHrzgc5bgY+VyAJULwsDUFbcuth0dmM/UoJr 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 80f4b4d..3264bf5 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)