From patchwork Mon Sep 9 09:18:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 13796506 Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2061.outbound.protection.outlook.com [40.107.117.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F243F1B4C3F; Mon, 9 Sep 2024 09:19:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.117.61 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725873569; cv=fail; b=aK2/8frKgVKg/IrwnIAAM0rNh04IjFAQ6gjTD4VAi0zcMNty89m/H9QFEQnXiA32nlFRTxO48hd6dAANGJHHoU0XURXcNnY04MkTZ0TQiiWJzDSKq/xbIo51V4MzZpvD6aQedvVd5bY6xiK/oT4HCsm7NjyJgirjwK/6G9jQb9o= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725873569; c=relaxed/simple; bh=ZekLqoJ+UcuDoq+wzukX3SfEfTiDTTO+MbeMHjwhMBM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=EEOLvxCT/MmWkY0u3WWpkEokntKzuYsZqyGihjTs63oO8khaQfacSjmZmwcGzJVIAmT59ERwIP0XHGiEppqEZzw7Tf2T2XC9wHcQRCPsQ1ye6TKDNhhgSV/uh9Yt9FiiU4K3UQcpZr+gVUyKh3E3XYVbkWexlWxZuQ9r5WR+JA4= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=vivo.com; spf=pass smtp.mailfrom=vivo.com; dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com header.b=qXsd1NvQ; arc=fail smtp.client-ip=40.107.117.61 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=vivo.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=vivo.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com header.b="qXsd1NvQ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=WITgtunADyOFV9KE4T4OaSomGnIU3UNJu+0gPrrtF45p769tjOU5VOzLe4nE4gJGIGqHRQk0bC1k4/pFFtrLjAkLhxz13/JGxhwNKielcYD6/Uaxt1CWiVJPod9aFSHtsznHsqmHJdnj46iJY/TeSIVPLRZpxuAyr9iorjf11+kMk1VzwUtQVy3eeepqEJH5ZkiMYGIT+sq6ow11wsAg7G6fiOrIS2I+3uefTnJcd/XyGOeGbP7uzMjPDfFKZZyS56mpJmUSzSiP9woX1RYFiCXq9GCy2DBK9rFtDoBoA2hPfRbcINCFm2S2/ZtYlW5VnffkGuqi+Y2HC8sH09RdHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=d/F4MlgK7pTrUhUQUbT6eSuBN6AE8jElWIdcMynY65M=; b=jqTA3PekZwXZN/OYVKJFizRcBn9zGbdCWlBX/zJeqicS13dLe+PSZCHvU7JntRKcgRVJjBM1Ye1oTnayz9vWKnElNzxC895OUDHEqpRnge83NzY72k3VJr8sxnYcD2qHiGdMS4XR9LxFLVsYMRImyTha6t+a20PlX5vZ6ozgm5nWAghPpc1WQ3HgL4LsZa3pJkIpubrHxVvhc+husHK5XMCx5CrV9rN4LE421/d8AvH1xH8722ksa/D/o6iSz5+4Y37iC9vwFFYMovoIsx/zR3ksTjFmmiNoqi5TI5qO8It41yjmJyUzzzJd2eXBD2SIZW7GiJ06T0ciYbj3VWKN6g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=vivo.com; dmarc=pass action=none header.from=vivo.com; dkim=pass header.d=vivo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vivo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=d/F4MlgK7pTrUhUQUbT6eSuBN6AE8jElWIdcMynY65M=; b=qXsd1NvQCBdGpu4XpuK0Ua8MKe8EeZ89PfA5E1fcu3jtOaYO4HPjHCf1+SU8UIBK/KeMDeOj8SSuXrJ3mj8K6H0AzvD57y+yYUysMc6l1WBdJ3YG6DxY1+AEjoSNvmBTJa8na47vskDHQPcZbZd2qpZSoS0asvbQ0X2TmfQ3Hf/0JeonXM+MXk7pbfXR78aXU2WbxJQXSgn6q5AfHh/acgbXy3nf6776pkVRXhXgbJqvmSaB39spRtbak8upHIFUotknjjdwXqjLA4szwFD77gxVtISqqGKlCJx9I8QwVcknewhE4r4Eq0uOqGpoSn3KKOSA0+zqZo9pg4/aFAqSLQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=vivo.com; Received: from PUZPR06MB5676.apcprd06.prod.outlook.com (2603:1096:301:f8::10) by TYSPR06MB6292.apcprd06.prod.outlook.com (2603:1096:400:42d::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.23; Mon, 9 Sep 2024 09:19:23 +0000 Received: from PUZPR06MB5676.apcprd06.prod.outlook.com ([fe80::a00b:f422:ac44:636f]) by PUZPR06MB5676.apcprd06.prod.outlook.com ([fe80::a00b:f422:ac44:636f%6]) with mapi id 15.20.7939.017; Mon, 9 Sep 2024 09:19:23 +0000 From: Huan Yang To: vivek.kasireddy@intel.com, Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , Gerd Hoffmann , linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org Cc: opensource.kernel@vivo.com, Huan Yang Subject: [PATCH v6 7/7] udmabuf: reuse folio array when pin folios Date: Mon, 9 Sep 2024 17:18:45 +0800 Message-ID: <20240909091851.1165742-8-link@vivo.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240909091851.1165742-1-link@vivo.com> References: <20240909091851.1165742-1-link@vivo.com> X-ClientProxiedBy: SI1PR02CA0011.apcprd02.prod.outlook.com (2603:1096:4:1f7::19) To PUZPR06MB5676.apcprd06.prod.outlook.com (2603:1096:301:f8::10) Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PUZPR06MB5676:EE_|TYSPR06MB6292:EE_ X-MS-Office365-Filtering-Correlation-Id: 485a1bac-d4f2-4d16-369d-08dcd0b07e71 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014|52116014|38350700014; X-Microsoft-Antispam-Message-Info: OAGKN3bF+0OLEYdyh3UQAoEipSPKy8NsLdoOWYo8hj2ZGt3tLrpdMdvzooYnhMie+Dq6qgIrPq+AKW+hO7Ew36sS/vjyNqzUjfl/v3abW+Qe9A+ROxwFK398QK4o1Xi1E5ooYhnSxvmcVE5gO9TpOpy5uCAZoA/BXq3DyOZSSQGKP+nG8foH6dGCOm9/Kb7QB+8U6Vy1rsA43ktNsz8IM6bbEmKTU7P6qgw2LewjDAfacAT5SFyBtTvjJwllZ3i02lIMmBQTVvKHz02Fuxp2s2okendgO0WqbChZrWZaeL++YUvmCGNOTNWzYqQZdoVXjO6yKIL4AJyAuUeURm6m+Fk/V8n3Rctbh7Vbgcq79lNZ80IqcwnqI/ngQO3B6NwvutNXyB3G33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;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PUZPR06MB5676.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(52116014)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: QYVNRXpqF8i8tVb/OVtlRNfvb1pQ8uHc0l1rB84T2YV0FsSfqPgTu2ZB+bdbrKz4S0+rwoA/4MIO7HzT/tE5R+CVKtcgQdmTjGpX1hxcqO1+i+vxQVunYe8pfepELI8FMYGZpcMZPyJTPXiVM5BJLRxNWpjKX3qAhaiDZ3j8W649h5mQ8lGGJfKcVeBztzdIkR/OBXiplJ+TSGdyHa/WYeqVSG7hyR8oeRJFSmhiIsyyCzH2yFpuwmIHrIQFm2LvJzNmY4O+RbtMO7uxqE2NIQV06+axTacGXsFHXHWHxg6T54kMl/k3MYkbYrfo8hbYTlm8DiryaEHuk9177PTt/rOIjxcXpKv/auqoL0hZxrZoelQjuGUnorH2BB+qgU94Xqd4cjCVQBqw0PNmOYlbNVjO5GLLX39pZRjJed3ZUM9CrghoGnFdS75+h1bOYysGxajoHUmNKdlUszQ9uzEJNq9AoZyFe9f5QBeDont8dHdzy26F7TZEvs7hB5Lf8TMY/EHbqVrAy3HHSJAgCrYGhqeWmaFWWf/gD3r9py8hzYrXAGc/1295BLXNNLN+UEq0W739ivFRle1U5APgQU7XSBV5tpKPFqpss87PuAcz4LlER+v7yDRp85Sl+Md6ByaKtM8n7kGd1ilYzDfNwXLyvdiGzBT2VlTNuaz/8A7HgVSF1m/O9BN+STPVBML1WKl70H9yKIHESmCdQumIlTJVfBoVO7/NnGbQVWnjQXsuieFrAB9TfKsZqipGnSTijWpnC4ldxLDozOArcZ+wxy49KYN2KwXfL2d4ybAKPbzBTrJvP6DPcQNGWzF15i3bZps7aCUY68FSO6D0CrmYtkj3gY5TKTpagxcvZ3GjdM6dS+rATvcibrp3VsXfxbWh59mNaE1mvnSrJGRGJmqn84hSzIzgDuIEmUY9CaXxSuR6TlQNQ2i1jmESEa5+g0M46oXSZv1A9tjKQm0+uQHDLztJmAZz5VLrraGSPptuJJqyRkHQ89QBM5DM4PaANHHCo2BgFhHlQDaq2pz1Hi6NA8zIuDc9Ke4ykTZmID6NwAmURZcaeexP5ORWjzjvLZSBjUtNFFMZUBIYaEoCRdukj4noNWDt5uixnJWFDzGfXr94nkYsd9Cq3SxM4Qy9NKrOXgbVxhYpTHg4C1vvvsNeeqU5YGMau+VOfJn02r/PHRhGAbYs4A+SErfMbAd/hhM0YRenXXkQDFO4P+EwjiBr3y/6jyBXFSjxJem+TONbirXd+1ysa5C2OPNQugqu2wxDVLa8DyxUZyQWZHADU6467srkb8eE5IOClA3dSvflB3UoJWt6a4SaKd0IlPDhU2e22+0xQM1tGl1RUzOzwlx4U6Z7bA2X1gz35Jnn5bSA8DK+EMtL255Wga3WsWOY9h5T76/TQG0+ZfN7CqvuVnJRujNGxx+z2WiwkNEj5L0j7aK23uLDeUo/FM9jp+rvMymWXjfkBmQKi2C8cONoOI+o4uNykKw4IQV2NrGN1glocBsiydVN/Q+TV7O7UpF7bDjKsp/Z8zuYzJ1D+oyfB9SW70dvHd5LxkGGp3UmikgiIKeUQtGgNpdLCWcU7Cw5IKh8gXkz X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 485a1bac-d4f2-4d16-369d-08dcd0b07e71 X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Sep 2024 09:19:23.5655 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 923e42dc-48d5-4cbe-b582-1a797a6412ed X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: +M07UJOYdAd0KZjTr0u3U3LZY7IKix+I2WtF0434Myaslwk93HlXpn9NOcTFMTV9sqg6Uqe2QDiGPfjnswKC5A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYSPR06MB6292 When invoke memfd_pin_folios, we need offer an array to save each folio which we pinned. The currently way is dynamic alloc an array, get folios, save into udmabuf and then free. If the size is tiny(alloc from slab) is ok due to slab can cache it. Or, just PCP order can cover, also ok. But if size is huge, need fallback into vmalloc, then, not well, due to each page need alloc, and map into vmalloc area. Too heavy. Now that we need to iter each udmabuf item, then pin it's range folios, we can reuse the maximum size range's folios array. Signed-off-by: Huan Yang Acked-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 0e405a589ca2..9fc5d22d54ae 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -341,28 +341,20 @@ static int export_udmabuf(struct udmabuf *ubuf, } static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd, - loff_t start, loff_t size) + loff_t start, loff_t size, struct folio **folios) { pgoff_t nr_pinned = ubuf->nr_pinned; pgoff_t upgcnt = ubuf->pagecount; - struct folio **folios = NULL; u32 cur_folio, cur_pgcnt; pgoff_t pgoff, pgcnt; long nr_folios; - long ret = 0; loff_t end; pgcnt = size >> PAGE_SHIFT; - folios = kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL); - if (!folios) - return -ENOMEM; - end = start + (pgcnt << PAGE_SHIFT) - 1; nr_folios = memfd_pin_folios(memfd, start, end, folios, pgcnt, &pgoff); - if (nr_folios <= 0) { - ret = nr_folios ? nr_folios : -EINVAL; - goto end; - } + if (nr_folios <= 0) + return nr_folios ? nr_folios : -EINVAL; cur_pgcnt = 0; for (cur_folio = 0; cur_folio < nr_folios; ++cur_folio) { @@ -391,14 +383,15 @@ static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd, end: ubuf->pagecount = upgcnt; ubuf->nr_pinned = nr_pinned; - kvfree(folios); - return ret; + return 0; } static long udmabuf_create(struct miscdevice *device, struct udmabuf_create_list *head, struct udmabuf_create_item *list) { + unsigned long max_nr_folios = 0; + struct folio **folios = NULL; pgoff_t pgcnt = 0, pglimit; struct udmabuf *ubuf; long ret = -EINVAL; @@ -410,14 +403,19 @@ static long udmabuf_create(struct miscdevice *device, pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT; for (i = 0; i < head->count; i++) { + pgoff_t subpgcnt; + if (!PAGE_ALIGNED(list[i].offset)) goto err_noinit; if (!PAGE_ALIGNED(list[i].size)) goto err_noinit; - pgcnt += list[i].size >> PAGE_SHIFT; + subpgcnt = list[i].size >> PAGE_SHIFT; + pgcnt += subpgcnt; if (pgcnt > pglimit) goto err_noinit; + + max_nr_folios = max_t(unsigned long, subpgcnt, max_nr_folios); } if (!pgcnt) @@ -427,6 +425,12 @@ static long udmabuf_create(struct miscdevice *device, if (ret) goto err; + folios = kvmalloc_array(max_nr_folios, sizeof(*folios), GFP_KERNEL); + if (!folios) { + ret = -ENOMEM; + goto err; + } + for (i = 0; i < head->count; i++) { struct file *memfd = fget(list[i].memfd); @@ -442,7 +446,7 @@ static long udmabuf_create(struct miscdevice *device, } ret = udmabuf_pin_folios(ubuf, memfd, list[i].offset, - list[i].size); + list[i].size, folios); fput(memfd); if (ret) goto err; @@ -453,12 +457,14 @@ static long udmabuf_create(struct miscdevice *device, if (ret < 0) goto err; + kvfree(folios); return ret; err: deinit_udmabuf(ubuf); err_noinit: kfree(ubuf); + kvfree(folios); return ret; }