From patchwork Tue Sep 3 08:38:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 825148 Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2066.outbound.protection.outlook.com [40.107.117.66]) (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 E36A33DABE6; Tue, 3 Sep 2024 08:41:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.117.66 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725352906; cv=fail; b=NWvd+xHSz2rQfptdbHESipfyX9/ZEIG71kzrg0rUcrU04FA1+vHLFAU+Qz7CYeB7mQtLaXSlMkOYD8XlCAi6e/p2orcg9iS/ney6to9ss12ep8JoPRrG+ObAxIF3DlIcBO36yj287L2RPxKcmNqWDkxTRABYEzNUELW8tgiCfDg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725352906; c=relaxed/simple; bh=F6hE2Hob1C9OH63nyEve8jtrlhyVSXG0qswWbVYVbZw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=okyxkFbD1NSnqwZhM9f0M+xk9PGVs96dyu6sHF8v3k/VpSDLr0Fwm7hjDm4AYvAcnZw5kjJaNtW5qLJzdZKwJI46QpljWdoEMcqAI2u/CoNB5N94d22Q1V/nvQ7tonPQmUHbQvwLA4jntQlZPBjQgvhnrk1x70C94kmCVi4bZVY= 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=DKo6Zlde; arc=fail smtp.client-ip=40.107.117.66 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="DKo6Zlde" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=xxUwp1yEyUK4EvgQBLmQydU5conZpMlQMisXkRqV0oKQ6s06Do3A1dBF8hzl23z/WmBnx0jJC7pqirQ6WDl+1poZOtlZYPvjunTGBwYQIsoYvG8Kfbbozg0VKAFT9uN9D7XBsaP3Lgbl65yQ1AFoKkiQiBOeRepfed2zKnHpfPhxRtStKA/RUgqQTARr7g3awiDJjVUP0BgHEGXKt+0I+H5cCT5RK+McwHQX+XZGAXew1dhLtjkrLq2KTEOr8CORs5Loqzxlexg6uBz2P3f85fTuXSTY+OJ9DwAxnzwdAabAhtVSfUWlfTrRO/SjSQ5nIsqlM0OKMdQv/8DxT5OlmQ== 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=XIEQ3rlqzNJaqDoCL5uRxsy7PyfbHUkPJ0IE5caIGbk=; b=v97dTY+2fAGsKsxuqH32jPbAz05RyaQbSpTys4aqKdxoq2wFoCpY9WvhQT62EIL9AHwzt1qQHjJYeamLYSmxltUjhwYqbCyZhLnqzBhljim6nT21XCVtQLwV3Tv+G66J74mzOyrzMkQYTSeQrZU/gzPQqHscHmjVoSP357n1LQRNTSe3BCIwWXa2kuHxJO0AlWu4mwHC4oiiEQSfPynJl89zgukqZzfSvmVtX5A4RczEMsorZDr/nl+y/OoB5UJoBucxOj1DxzbQQ6E09J5Hj5t75Uj9hqMDlrGdayyOCYFtzd1RUOvHJX8xFdZJefH4UZ9Ok91mBGZIjXhM1CdXOQ== 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=XIEQ3rlqzNJaqDoCL5uRxsy7PyfbHUkPJ0IE5caIGbk=; b=DKo6Zldeca6M9Gr6TObpIlaYiD92BMBwP8YEuS1CQpuIH14O33znoJAxmEYd23BBs/zUUdoNyfFwWc61EoGHnznr5kjlfq51Y6azMnM1Wjei/RV+4ytt+jNzySzknMM0QpvCoJi1neKQ8KpWk7Fn9Ut7EO2bfhTlFnLhIcDEU0t2UUBfLBhsDywkJtlCsi1hcQFLWVU7aOaA1TppFJ5bXTBTe6f73BYz+MnLBxXlsXllLJrrrQGZdYMauIQFZpT62EXDphVA6gTyxj34qP7ahb/a9hKL5QSXl7zkiKeGInu3rovytFC3KANM/x7IljL0UPdqt72tfo3US8Ecr8xORQ== 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 SG2PR06MB5359.apcprd06.prod.outlook.com (2603:1096:4:1d7::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.24; Tue, 3 Sep 2024 08:41:42 +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.7918.024; Tue, 3 Sep 2024 08:41:42 +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 v5 2/7] udmabuf: change folios array from kmalloc to kvmalloc Date: Tue, 3 Sep 2024 16:38:07 +0800 Message-ID: <20240903083818.3071759-3-link@vivo.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240903083818.3071759-1-link@vivo.com> References: <20240903083818.3071759-1-link@vivo.com> X-ClientProxiedBy: SI1PR02CA0026.apcprd02.prod.outlook.com (2603:1096:4:1f4::11) 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_|SG2PR06MB5359:EE_ X-MS-Office365-Filtering-Correlation-Id: 24b1f3b8-3626-4974-a4ef-08dccbf43bf6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|366016|1800799024|52116014|376014|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?SkS8J/paByaM+cBmIQRuXGjd560QPc4?= =?utf-8?q?UqoAHq6qcHwVabl6X1XUgiJsjfeF41QP0bihOtZ2A3B81/SxKiAoSvx3BoimFtQlV?= =?utf-8?q?p1xqfGayx/tUQGWyny+p0qTM35HXBP8m2OaCm0s9NGfiCtq/hQhJWYcCP1ukDwPPW?= =?utf-8?q?RSmt992bDmz6zGLKDa/8B5JS8tHqpgZ0QX1fcvHvQHE0CCPMNb+rGkOjNzGxtCJ3t?= =?utf-8?q?aKzHsxfE6WVaGx9GY9gU1x0KZFjRv1A5mYXOlw4HA9VmieZlMfXvdDtHliugXpAL3?= =?utf-8?q?zYSRP5FDGkAJhKVhGC+SwL0umFb5mm6m/HvJD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rR5U9BPXdDtY5sSk7F4b/USYngk?= =?utf-8?q?voy8yOVYG7WORxepa2+PzfgkfYqBZ2Mc9zKvyiLQOPLyPCjfFPaHQCN9dSrw7L3Z/?= =?utf-8?q?1JpR6MqBEZENp3COuq8iMB53Brqf/aFN+PeCPSH3La0lEa+82FUp0ZcPle7oEqhKA?= =?utf-8?q?q++o2N7PA1TWBv5qygHtGUf6UQcBhLUiSw=3D=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PUZPR06MB5676.apcprd06.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(1800799024)(52116014)(376014)(38350700014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?7UmduumG5r87pyB62W5gu9bZ3zO8?= =?utf-8?q?ss2Rkdjw9rgHmQMNkK1ZmkrQz993x2itt+ohX5xnLPB8WjjP3glpuzb6+ZIcxvSes?= =?utf-8?q?JgFh8iRYXb1J5rWkEtxuIUk+WDr1GVQhepQaAY72ZCq03N+vDLqwP8MelBlgqS1Yt?= =?utf-8?q?HzYSisieUcGpaacxQFpBSKqM/tZDwW+MPlst4jdEC5TAywFk5ocljGz09mGZPTNou?= =?utf-8?q?uhpQUui9LTIGoqu/BJdfEiVbbr7n9QiHP4zEqM/80Q5ICFwMS0NNo6ks/Ys4Ak4vH?= =?utf-8?q?Gu90R0ad1JlK8NZ9iv73pPcAVrlEn7nTj1svGYYlENnTmkDe/zaosxMtEyxj5amx8?= =?utf-8?q?S8Uo2xY0+0GdocbPu0BVSo+kliS1px/YALXmIEmlRx1zKNZoZoIwFvU/jOZ4ZcBLZ?= =?utf-8?q?hdzMrf6eFkGby4U6QmtGQqlXXpIFCp3XleRMBIlVZ+IfnLIkKo4nD6hplOQogAAK3?= =?utf-8?q?IEupDrgHHJYiyz52dSDzxclrVoakV3OA58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Rs6Fn2SQ/Z50VHQ1clO/IqmO?= =?utf-8?q?6ljyzdj6W37/nq+bhh4df2ZtDcXuU+4yVlsRJwl5GnodJTqMcptDyT95RmONXPw4g?= =?utf-8?q?LNOTMVrzq+kI97hdlSVBZo4YGOI+DRUsCr9kuKTEIoPWcwur4pArDIidlWnMD8mlj?= =?utf-8?q?pL8Dz3sKH7j5?= X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 24b1f3b8-3626-4974-a4ef-08dccbf43bf6 X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Sep 2024 08:41:41.9549 (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: Dso8+b8AUVvpim3qBIORCPxeiASKu+ZoOqQQ3vlNwrqx2unnpJtbbe5i7h9dBRHnte/0RwRyIwyCF5IT9IL1ew== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SG2PR06MB5359 When PAGE_SIZE 4096, MAX_PAGE_ORDER 10, 64bit machine, page_alloc only support 4MB. If above this, trigger this warn and return NULL. udmabuf can change size limit, if change it to 3072(3GB), and then alloc 3GB udmabuf, will fail create. [ 4080.876581] ------------[ cut here ]------------ [ 4080.876843] WARNING: CPU: 3 PID: 2015 at mm/page_alloc.c:4556 __alloc_pages+0x2c8/0x350 [ 4080.878839] RIP: 0010:__alloc_pages+0x2c8/0x350 [ 4080.879470] Call Trace: [ 4080.879473] [ 4080.879473] ? __alloc_pages+0x2c8/0x350 [ 4080.879475] ? __warn.cold+0x8e/0xe8 [ 4080.880647] ? __alloc_pages+0x2c8/0x350 [ 4080.880909] ? report_bug+0xff/0x140 [ 4080.881175] ? handle_bug+0x3c/0x80 [ 4080.881556] ? exc_invalid_op+0x17/0x70 [ 4080.881559] ? asm_exc_invalid_op+0x1a/0x20 [ 4080.882077] ? udmabuf_create+0x131/0x400 Because MAX_PAGE_ORDER, kmalloc can max alloc 4096 * (1 << 10), 4MB memory, each array entry is pointer(8byte), so can save 524288 pages(2GB). Further more, costly order(order 3) may not be guaranteed that it can be applied for, due to fragmentation. This patch change udmabuf array use kvmalloc_array, this can fallback alloc into vmalloc, which can guarantee allocation for any size and does not affect the performance of kmalloc allocations. Signed-off-by: Huan Yang Acked-by: Christian König Acked-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 0a8c231a36e1..789fba312a68 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -111,7 +111,7 @@ static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map) dma_resv_assert_held(buf->resv); - pages = kmalloc_array(ubuf->pagecount, sizeof(*pages), GFP_KERNEL); + pages = kvmalloc_array(ubuf->pagecount, sizeof(*pages), GFP_KERNEL); if (!pages) return -ENOMEM; @@ -119,7 +119,7 @@ static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map) pages[pg] = &ubuf->folios[pg]->page; vaddr = vm_map_ram(pages, ubuf->pagecount, -1); - kfree(pages); + kvfree(pages); if (!vaddr) return -EINVAL; @@ -227,8 +227,8 @@ static void release_udmabuf(struct dma_buf *buf) put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL); unpin_all_folios(&ubuf->unpin_list); - kfree(ubuf->offsets); - kfree(ubuf->folios); + kvfree(ubuf->offsets); + kvfree(ubuf->folios); kfree(ubuf); } @@ -353,14 +353,14 @@ static long udmabuf_create(struct miscdevice *device, if (!ubuf->pagecount) goto err; - ubuf->folios = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->folios), - GFP_KERNEL); + ubuf->folios = kvmalloc_array(ubuf->pagecount, sizeof(*ubuf->folios), + GFP_KERNEL); if (!ubuf->folios) { ret = -ENOMEM; goto err; } - ubuf->offsets = kcalloc(ubuf->pagecount, sizeof(*ubuf->offsets), - GFP_KERNEL); + ubuf->offsets = kvcalloc(ubuf->pagecount, sizeof(*ubuf->offsets), + GFP_KERNEL); if (!ubuf->offsets) { ret = -ENOMEM; goto err; @@ -374,7 +374,7 @@ static long udmabuf_create(struct miscdevice *device, goto err; pgcnt = list[i].size >> PAGE_SHIFT; - folios = kmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL); + folios = kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL); if (!folios) { ret = -ENOMEM; goto err; @@ -384,7 +384,7 @@ static long udmabuf_create(struct miscdevice *device, ret = memfd_pin_folios(memfd, list[i].offset, end, folios, pgcnt, &pgoff); if (ret <= 0) { - kfree(folios); + kvfree(folios); if (!ret) ret = -EINVAL; goto err; @@ -413,7 +413,7 @@ static long udmabuf_create(struct miscdevice *device, } } - kfree(folios); + kvfree(folios); fput(memfd); memfd = NULL; } @@ -429,8 +429,8 @@ static long udmabuf_create(struct miscdevice *device, if (memfd) fput(memfd); unpin_all_folios(&ubuf->unpin_list); - kfree(ubuf->offsets); - kfree(ubuf->folios); + kvfree(ubuf->offsets); + kvfree(ubuf->folios); kfree(ubuf); return ret; } From patchwork Tue Sep 3 08:38:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 825147 Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2066.outbound.protection.outlook.com [40.107.117.66]) (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 0C6791D095F; Tue, 3 Sep 2024 08:41:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.117.66 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725352910; cv=fail; b=rejZC24xNP6hBD51z7Iuf+IVWMol63TGiKhzdKZLxJoCHrHI2362k2JvK9xHflsMSdv5J5Wadavnt/QP5Id6ocvNvhOPdtjHxZUaQJk8Pqgks2O8P2rR1nZRxFmHMcnX5oOTd4ZNR5A7pp8Yl6ahW1EFgnuFh/ZaKbfBN0nmiAU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725352910; c=relaxed/simple; bh=/bJ3kPIC3q5soaFW+DrWHX5yxOyxEMGu1gxW1cSLMs4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=A4///CydV742/DTaO0/U6FMlYqyqYQ+pWCLBOUvrY2xfByqEgjXAehGu64GDM9RwVyFW0oAcsCAqRc7HYJxnOLLeyGIutq0W2LcQO7eELQiqyrb29n4mxQTPXgWh49RJvnLItfln+tPSwGc5Nndf4Sqe/fI3pPSRfMinJokqP9o= 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=L+6DvRb0; arc=fail smtp.client-ip=40.107.117.66 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="L+6DvRb0" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=hs9X7PKuvHf9ZZL82gR8/motsl/pEQZqjgmlVSuBYDUgiartOX/AK68st9zMB5YTWgsJ18GJeIXDxmAhqaZzNuyRlDTpGaNCgJCPMiCcDx7Jxua+/8GYQDATne6mGHUG1E/WzoBryuieUhp2cLvTDSBPUTAMNvctj4NJwa/eGvNHOcdvO4E0H54N3jWxIjMRyaxXKO44UiAQA+7aaQJzrOhjTlK0VwfFqDBW2sPEh9tsg30iRx4PvhAjSdxr+2obxM4mbXZSvgG+bh+3we9n/lvw/GLrKZS8hezhBoY7qAlcd8TK8ZTBlCmdejlKqG79JL1xI4cwAdhwYOsXIpLpQw== 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+oBILXlrKLI4yMdKcQ0YyodD+3vURTgtAhI0Rs4j0o=; b=ev5wzGGusGRvotVWNDaiVs6HhoqJo+dl2et1tEn7vLPw7gzuxlXRE9esM+h380xxygfVfkFxRpp/ZleldyL8A1fmR94jyuCpUcRHgjqSXmsXEjYssm9gTJWG+KfSIPD0I23DoKCOdUR/9tLjEo3jkZ6rMXNCT/HVLq3SxspaQyAYWpOsC5tLSy0dJWlB7yNdVKm6rGSX1CCnHnEu8tzOXwRsoLNzgcrZgIEK0AGM58F4DdNW/tQKODPVyjlOy9o4i1zprtLkhRTzJUFUx/0P59Ypg92W88bRw8OLuyMrMwvkB/3xx+DOOpKSOln3YCAdIELFNYwpAVqF8Kv7tOtp3w== 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+oBILXlrKLI4yMdKcQ0YyodD+3vURTgtAhI0Rs4j0o=; b=L+6DvRb06GusiyIcGc5cHYfeor1DraKrFKeoZuF1kb8RTLiInupZyzyQhhMTy8AWBK20w5oHwdx4RMN6rlwtqz5k7PLRAGYXmVWfLdban4422ZH0G/VC7Gfcr2cmMe1nPQNMzq4ChbhiZSt51htSZmYCt5+skAGTe+g+OEtIR1WbHGn3sJ7g9KF8bci6Z142b/EYJXPw1CYjlIB4O7xk4bUDJvdTmWPbzWFCadGBze09tWSmAnA/oBj0ZLnZsASIqMfGhQe6g5EDtDtf6idjzVP7rUULxbHVu4TgLaq6Ar/kx6E8NjJbd5vkl/f/oKjP4G7U6jgN/kz67GfZljIIzQ== 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 SG2PR06MB5359.apcprd06.prod.outlook.com (2603:1096:4:1d7::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.24; Tue, 3 Sep 2024 08:41:47 +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.7918.024; Tue, 3 Sep 2024 08:41:47 +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 v5 4/7] udmabuf: udmabuf_create pin folio codestyle cleanup Date: Tue, 3 Sep 2024 16:38:09 +0800 Message-ID: <20240903083818.3071759-5-link@vivo.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240903083818.3071759-1-link@vivo.com> References: <20240903083818.3071759-1-link@vivo.com> X-ClientProxiedBy: SI1PR02CA0026.apcprd02.prod.outlook.com (2603:1096:4:1f4::11) 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_|SG2PR06MB5359:EE_ X-MS-Office365-Filtering-Correlation-Id: d374601e-6c5a-4a0b-d6bb-08dccbf43f07 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|366016|1800799024|52116014|376014|38350700014; X-Microsoft-Antispam-Message-Info: cn58EvbnXD7D23BVX5TVfF70tWvvXlkh/DWCPJ/vhwosnG3WHZSxvFi+qsxXvMvg9J+UGDE1uP4XLA6cawFa85A4eqzHORHbR/heqolYh9EtcG5rXEKkq9OKolFgXqgQQpfxHCXHB53yDxfPX/ws21y6n2gXbHV0o4cxlPniWdqiXHxZDCAutkIiWC2WBvt3YeDZVA8jgRNfR2LsFuk5lmumuSHemMnGoARVLDuflbjXWCakQmszS21JdjoFk9CNjKGQ8sM/zwYGTTo6aY9cuF3BQrjpz0fAaFJrAo7gh7Uj3rFjFEKEAxb5F/fjUIxuC/Ru5gaTKyWlOD9YZq0vBPFmSsOyt72WNLroq6jpLvBZK5OUZYx4iS+yQc1Wre+1Swhagd2356o61yAytevSaMUYtIiQP9/iOm0GUTZD+NIX6y4ltNNx2rgMsb06HKiyDcPAvRTjxyBWOVRHBwMRRBYBKCgOef/8rPKq/FsBPkvRqdc6Agk/+GhmoDlTT0KJ25T+SHULMfzAz/GVVSIPl7F3vejHiPLqSJco+8CA+6wR++b1q2WCcoW4JM+kVW1l9cNwcYnIF98VzsJvnxZ47HFLXXqz2zdQK+stT6Y29Cw0ZijXv6x5DPDFX4AE6gff/ksL/JXb8/q+CzHvCLc3+um84IslAcma17SNNNBQtgm3x3evVNbXzWNX1mH+sD+b+vWcyU4bC5+VFsy3flyOwAaLDH2H+97qjrxN6PNCjp7awJtnTOGUzDC1zxkpRSbAetaUvsNJbrA6jfXqcOqMX1pJScedj5emcjsESDeb98wGLVuyvqBuxmQ1oJS/LmJ/MGFiAI0ejhr85wcNhByxSAsM1aIM4OJCvDVPhwH+Gxxy7+qVZsifqDrr8IqhfMBSPdnevIk1huzj9MYiAbWFTw5HI4Fv3nVdqzdV36Bkyti59lEtX901okK6QcIfmdaRqi3z1OBlg5crZYD1G++IN+Ei+bVhkb4juNZZjermtJzwbXJeD4HNn36PxPlcqxkRwsU6DUimJtzbn45zEEcUrr/aXla1CUHm880k9DXDNCwyedbff2eP/Vgo0DkDp7zPZqO9yCY3eSK2Z56Uwa+dQmzVO6ru+iVnlv0BQtRh0cpTzfHwvRGS/7BRqh/qdQTtOIg8yYibpQG3QBvmx0F/m9PGOlzXY67Hzn51qYp11fpf30O2/KZGoNdQKUHM6GIN054epGHtaWBndtSw39fySwhl4C/A1CiBL9OfJAd3thd/srOwhwuTUGD85jGqpORp4cyJd2hnT7f1m0DdLtyWVIfe0s2aEmq5e+93TxK3ZNu871SouH/bOB+zU22znO7CPXlhKkv2iIUKjm8THghWqaPnMLxbYP0qMBdfu4vrx1oCWDCTKtV2pjJxNu4JLLhLGEUwKpT/7g54uSwQLVbR7g== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PUZPR06MB5676.apcprd06.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(1800799024)(52116014)(376014)(38350700014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: t6YMiXkMOmMBZmpuu/NJYN+C/GzDLZi7+8brNDT4xOh3peDg57RnWhUOCfmzLxtMsK6UH+3MNeVwgBiUSOpLPmN4A7YhYUGAg0+tZfraDaxu/k9ybt6KsFaNqmYMP53A5ZIwwPQPPhyoUwJjORWVYu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GQs4n9+FNK/dvZ0Z3oYw6fO8R4ccUpjG6kDpDYlm3LJSeqco3jsekdiV7FKHGpls27NAEpHJnn2y14mi/RvynCQWg4vpXDR9QlUv8Nselk1YnxsUGIURrzmRXNBI2bJepR21qTztMdMxATe8b55d+0HkqojDTUOn41WgdtobBxGSG2ztNTqiAHVx7EFqiv61fmHU7GZHXCPajVUcGvsqEvZz3BwHY/NNXo7ho1I0OYAKjB70H1nQmsrFQexGvQYm+Qrai/pLun73nGsmkHoQb239WxTHEQ8Z0uGLM6WrFGOxjR0V8G9mzQfYYeRzTDIJudMNGsNBJuHiALttc0xBxUtZkgaE32hEThOYuj X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: d374601e-6c5a-4a0b-d6bb-08dccbf43f07 X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Sep 2024 08:41:47.0985 (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: kiPQwDpkf/K2qTB9u+CyNdTpjQ/9zNRUfcMFzqHjHaxfgL3UjhyuCscft1ncgzu6lb0jTS6+vsS4rgRC6IqxLg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SG2PR06MB5359 This patch split pin folios into single function: udmabuf_pin_folios. When record folio and offset into udmabuf_folio and offsets, the outer loop of this patch iterates through folios, while the inner loop correctly sets the folio and corresponding offset into the udmabuf starting from the offset. if reach to pgcnt or nr_folios, end of loop. By this, more readable. Signed-off-by: Huan Yang --- drivers/dma-buf/udmabuf.c | 132 ++++++++++++++++++++------------------ 1 file changed, 71 insertions(+), 61 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 456db58446e1..ca2b21c5c57f 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -330,17 +330,67 @@ static int export_udmabuf(struct udmabuf *ubuf, return dma_buf_fd(buf, flags); } +static int udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd, + loff_t start, loff_t size) +{ + pgoff_t pgoff, pgcnt, upgcnt = ubuf->pagecount; + u32 cur_folio, cur_pgcnt; + struct folio **folios = NULL; + long nr_folios; + loff_t end; + int ret = 0; + + 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 err; + } + + cur_pgcnt = 0; + for (cur_folio = 0; cur_folio < nr_folios; ++cur_folio) { + pgoff_t subpgoff = pgoff; + size_t fsize = folio_size(folios[cur_folio]); + + ret = add_to_unpin_list(&ubuf->unpin_list, folios[cur_folio]); + if (ret < 0) + goto err; + + for (; subpgoff < fsize; subpgoff += PAGE_SIZE) { + ubuf->folios[upgcnt] = folios[cur_folio]; + ubuf->offsets[upgcnt] = subpgoff; + ++upgcnt; + + if (++cur_pgcnt >= pgcnt) + goto end; + } + + /** + * The term range may start with offset, so the first folio + * need take care of it. And the remain folio start from 0. + */ + pgoff = 0; + } +end: +err: + ubuf->pagecount = upgcnt; + kvfree(folios); + return ret; +} + static long udmabuf_create(struct miscdevice *device, struct udmabuf_create_list *head, struct udmabuf_create_item *list) { - pgoff_t pgoff, pgcnt, pglimit, pgbuf = 0; - long nr_folios, ret = -EINVAL; - struct file *memfd = NULL; - struct folio **folios; + pgoff_t pgcnt = 0, pglimit; + long ret = -EINVAL; struct udmabuf *ubuf; - u32 i, j, k, flags; - loff_t end; + u32 i, flags; ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL); if (!ubuf) @@ -349,81 +399,43 @@ static long udmabuf_create(struct miscdevice *device, INIT_LIST_HEAD(&ubuf->unpin_list); pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT; for (i = 0; i < head->count; i++) { - if (!IS_ALIGNED(list[i].offset, PAGE_SIZE)) + if (!PAGE_ALIGNED(list[i].offset)) goto err; - if (!IS_ALIGNED(list[i].size, PAGE_SIZE)) + if (!PAGE_ALIGNED(list[i].size)) goto err; - ubuf->pagecount += list[i].size >> PAGE_SHIFT; - if (ubuf->pagecount > pglimit) + + pgcnt += list[i].size >> PAGE_SHIFT; + if (pgcnt > pglimit) goto err; } - if (!ubuf->pagecount) + if (!pgcnt) goto err; - ubuf->folios = kvmalloc_array(ubuf->pagecount, sizeof(*ubuf->folios), - GFP_KERNEL); + ubuf->folios = kvmalloc_array(pgcnt, sizeof(*ubuf->folios), GFP_KERNEL); if (!ubuf->folios) { ret = -ENOMEM; goto err; } - ubuf->offsets = kvcalloc(ubuf->pagecount, sizeof(*ubuf->offsets), - GFP_KERNEL); + + ubuf->offsets = kvcalloc(pgcnt, sizeof(*ubuf->offsets), GFP_KERNEL); if (!ubuf->offsets) { ret = -ENOMEM; goto err; } - pgbuf = 0; for (i = 0; i < head->count; i++) { - memfd = fget(list[i].memfd); + struct file *memfd = fget(list[i].memfd); + ret = check_memfd_seals(memfd); if (ret < 0) goto err; - pgcnt = list[i].size >> PAGE_SHIFT; - folios = kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL); - if (!folios) { - ret = -ENOMEM; - goto err; - } - - end = list[i].offset + (pgcnt << PAGE_SHIFT) - 1; - ret = memfd_pin_folios(memfd, list[i].offset, end, - folios, pgcnt, &pgoff); - if (ret <= 0) { - kvfree(folios); - if (!ret) - ret = -EINVAL; - goto err; - } - - nr_folios = ret; - pgoff >>= PAGE_SHIFT; - for (j = 0, k = 0; j < pgcnt; j++) { - ubuf->folios[pgbuf] = folios[k]; - ubuf->offsets[pgbuf] = pgoff << PAGE_SHIFT; - - if (j == 0 || ubuf->folios[pgbuf-1] != folios[k]) { - ret = add_to_unpin_list(&ubuf->unpin_list, - folios[k]); - if (ret < 0) { - kfree(folios); - goto err; - } - } - - pgbuf++; - if (++pgoff == folio_nr_pages(folios[k])) { - pgoff = 0; - if (++k == nr_folios) - break; - } - } - - kvfree(folios); + ret = udmabuf_pin_folios(ubuf, memfd, list[i].offset, + list[i].size); fput(memfd); - memfd = NULL; + if (ret) + goto err; } flags = head->flags & UDMABUF_FLAGS_CLOEXEC ? O_CLOEXEC : 0; @@ -434,8 +446,6 @@ static long udmabuf_create(struct miscdevice *device, return ret; err: - if (memfd) - fput(memfd); unpin_all_folios(&ubuf->unpin_list); kvfree(ubuf->offsets); kvfree(ubuf->folios); From patchwork Tue Sep 3 08:38:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 825146 Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2060.outbound.protection.outlook.com [40.107.117.60]) (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 6AADA1D86E4; Tue, 3 Sep 2024 08:41:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.117.60 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725352916; cv=fail; b=IFSIDFkgajUlkDdOZWITS0ozRaXQOGFJ6/0z1vIMutm/q473EAoQladuWJvWcL3IVWRjgRy6aY7TLAGB32fboTu45IU8m5AMpU07JN+nl0A42begBJbixk8naTRXKKcuTzbZch5derodRNMDsZmK/QzcwYDUoYAQA6xpYUdzodY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725352916; c=relaxed/simple; bh=R8I81NvpBR0lVlTEVYqm7gYrhKrTWrG+8HtvdWKwmNo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=UdrKC1CgY5KWl60YsK9iIz62Ix9X/crS8AiMv5exLD0+JSwWOLWn7j45nmrQcCJd4D4ddnK4YXFAGr55GpDg+fclpg/OVqtiTSUnhThrhWXR1QT0QQHEmZoYb7auRCTyuTHkWo7Bk56Kgcw4jm+Hf5W3CterzkxZcQ0k85GlyFw= 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=EHIPtM9x; arc=fail smtp.client-ip=40.107.117.60 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="EHIPtM9x" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Nle11Vm9Bod4tsd5/kHfEUXC6GsdqVIiwV/12SjavF1OizJMIw1UsXmOQJs77P7QjD2V6HTVTBtv/XWW18suphh5lkPci88FjYqZStt5SSou/cnMqPqHhsysIKlPPulB6vtY1qT53iUIgtlFTyn0Sk/25nIN21W1VYe06HuZ9D5wHsph26BFolATTlWtFMeIQfeT9TfzqX0oGKlzGX0nfCsEaauV6CUrCYwR7YjLWAlpexBKLszg6/ZWYbJBuix8x1vHR9U+KD9hdPHElgSW9NK6JxU/RqlCM1V6XNqaHU9JJn1y2MX9gGUxtgNJHg+GZ8FY3MET0YbgeKizhFwpyw== 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=2w9ISoNQ1TCRixOf5sLn0DMvJi7b12W4RPHtDUCStiw=; b=FS6I4mVkCTIv/nMkq4nckItnc0IWwJA266V8F51FNCNxhitHpib5wm0Erg9G7O5cHEb+SL9uHjRkdSkulmTTWq/JURbMaok8TWQNx5YQExmjtoRMg8JtHNRV2VWUV5lQJpAO2uVQb4K3XMazzoYzguJ0qqZH/qyIxsy8ZBSja+7ZQ7TBBEhV7CFDJWtN+YB7anHgOXcHIvWmBIHh/R++il9MZIGEoiBjOmsg1CBbkph4Bi0QUDBBrviZBpgGFEIKZeSbVFbVazoLvK0mEc8d2xBl2fIYlZpL36A+B8SljvyyCOJmT4RdrSmqqKDAQxPseKEts3qb6rRmQfI3k8OxyQ== 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=2w9ISoNQ1TCRixOf5sLn0DMvJi7b12W4RPHtDUCStiw=; b=EHIPtM9xQiQPONc11ouLV0nXGwx2PTMr5f+VCu68inahk2iWOisvKvo+fvOUa/+2/oTTcHOtz0DZTW1Q6ixAKbu6gBWikGVCc8NJG7/wv6vJAMTv24QYuaTiPo/HKVyX/I+SJyB38LW824XE8wg+Xh43Z9QJGophbW+ndNGZut6cxsW7or8wxZvz746smRxxprVvqFH/tkMrO+PRB2/QftICv9UFH2QKeoFTgMGHBq7tFg/Krmogat1Ypn1nUgD7JoGd47jBkmriA6VaK5xVi1PC99G9qC2x+1OG2SupEh2H6DmwoizVKPQ09IdWY/dy8iyNhlw6LQs3H5BS3SiyCw== 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 SG2PR06MB5359.apcprd06.prod.outlook.com (2603:1096:4:1d7::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.24; Tue, 3 Sep 2024 08:41:52 +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.7918.024; Tue, 3 Sep 2024 08:41:52 +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 v5 6/7] udmabuf: remove udmabuf_folio Date: Tue, 3 Sep 2024 16:38:11 +0800 Message-ID: <20240903083818.3071759-7-link@vivo.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240903083818.3071759-1-link@vivo.com> References: <20240903083818.3071759-1-link@vivo.com> X-ClientProxiedBy: SI1PR02CA0026.apcprd02.prod.outlook.com (2603:1096:4:1f4::11) 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_|SG2PR06MB5359:EE_ X-MS-Office365-Filtering-Correlation-Id: f25467ed-1eca-48a1-e229-08dccbf44219 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|366016|1800799024|52116014|376014|38350700014; X-Microsoft-Antispam-Message-Info: D57RObyxoOlS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qnvrI5l+y1UjUydZxXMudUzsMaLUKdnPeClUYdCj680bev3mqVLPbbGB1FYrXVzpoQSlHBwvta7A3s1XOSdHP1GAOaYBPRa3u+Nm1mkAVlY3Afkz1gKi9PRk25f4nl/NbfUFatEt6PsctLLuk8NumArl00pmkNP5eHD8jzBaWnKoGa2kXXDe7Q1pq8O0TQ1bGs1XB5+Bg8tjgVz90Dr6rsjxuIbFUWchMGS2FpetkOpgjDM3K/WbsdpXSIyhAZOmntPUhTqQrDLJ8ZcizPaoLRKoaUfOTFylDdS3R85hE3V0qhWHbdNjSJfmwgPVkl5qbf73zqyyx9QW5BGQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PUZPR06MB5676.apcprd06.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(1800799024)(52116014)(376014)(38350700014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: USTIxZZq6cMXN4G8MbZtah8/MEF8JEXQ7oQ5JR2L0h7CGDPTEGhdWgPtSn5vrT0W3AavUXencKlmV3krMfmjlh3xB7NdjDb3+DcVe5gGmtjr0WGwEK+xhTshZFnowzthTuvINnbXs8jxU6geFk8IErADWKkOIoCK2j8m3isoqrHXyM7UsMH2jD4n8aORO53qUTLEpxdt6GwUWNaveom/v2RBIERDk44eRrTnNmgRvpN6JO6WoKHrVnCGCE+7DBOls09pmUimH5gbfJBDcvm/NLKZ1QwU/I+r3XU9vbfLnT4Nep8hWluw+5GoEuLWa5D1BrZX8n7KtczW6hqfKUOOiYCxnhyQTXq0x/xgAJuEVN+aoyXqN0Rn50P+0aUnzrQuV83ezTR+VvrBtaGEy9mo0yx1c6YuJD2ipkT/P0UekLHiqS652TQFVwNAIi4FnR41L23ATelgIqq5FXa4rBHM3yWU7hxMCsS0KuOte5d/wtHb7mge91jD/LiSDm3D8ETp2VYs+YV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qUxvAXxn6 X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: f25467ed-1eca-48a1-e229-08dccbf44219 X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Sep 2024 08:41:52.2579 (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: +fZMt5zDK8qC+CCNcWzpx5ydeJohsc67KhGiYXZ2zH5+ISHTljGqpEum4bzih0G8TJci8uE3/83oGmoMc9wR3g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SG2PR06MB5359 Currently, udmabuf handles folio by creating an unpin list to record each folio obtained from the list and unpinning them when released. To maintain this approach, many data structures have been established. However, maintaining this type of data structure requires a significant amount of memory and traversing the list is a substantial overhead, which is not friendly to the CPU cache. Considering that during creation, we arranged the folio array in the order of pin and set the offset according to pgcnt. We actually don't need to use unpin_list to unpin during release. Instead, we can iterate through the folios array during release and unpin any folio that is different from the ones previously accessed. By this, not only saves the overhead of the udmabuf_folio data structure but also makes array access more cache-friendly. Signed-off-by: Huan Yang Acked-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 65 +++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 254d9ec3d9f3..d449c1fd67a5 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -27,15 +27,21 @@ MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in megabytes. Default is struct udmabuf { pgoff_t pagecount; struct folio **folios; + + /** + * Unlike folios, pinned_folios is only used for unpin. + * So, nr_pinned is not the same to pagecount, the pinned_folios + * only set each folio which already pinned when udmabuf_create. + * Note that, since a folio may be pinned multiple times, each folio + * can be added to pinned_folios multiple times, depending on how many + * times the folio has been pinned when create. + */ + pgoff_t nr_pinned; + struct folio **pinned_folios; + struct sg_table *sg; struct miscdevice *device; pgoff_t *offsets; - struct list_head unpin_list; -}; - -struct udmabuf_folio { - struct folio *folio; - struct list_head list; }; static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) @@ -198,38 +204,18 @@ static void unmap_udmabuf(struct dma_buf_attachment *at, return put_sg_table(at->dev, sg, direction); } -static void unpin_all_folios(struct list_head *unpin_list) +static void unpin_all_folios(struct udmabuf *ubuf) { - struct udmabuf_folio *ubuf_folio; + pgoff_t i; - while (!list_empty(unpin_list)) { - ubuf_folio = list_first_entry(unpin_list, - struct udmabuf_folio, list); - unpin_folio(ubuf_folio->folio); + for (i = 0; i < ubuf->nr_pinned; ++i) + unpin_folio(ubuf->pinned_folios[i]); - list_del(&ubuf_folio->list); - kfree(ubuf_folio); - } -} - -static int add_to_unpin_list(struct list_head *unpin_list, - struct folio *folio) -{ - struct udmabuf_folio *ubuf_folio; - - ubuf_folio = kzalloc(sizeof(*ubuf_folio), GFP_KERNEL); - if (!ubuf_folio) - return -ENOMEM; - - ubuf_folio->folio = folio; - list_add_tail(&ubuf_folio->list, unpin_list); - return 0; + kvfree(ubuf->pinned_folios); } static __always_inline int init_udmabuf(struct udmabuf *ubuf, pgoff_t pgcnt) { - INIT_LIST_HEAD(&ubuf->unpin_list); - ubuf->folios = kvmalloc_array(pgcnt, sizeof(*ubuf->folios), GFP_KERNEL); if (!ubuf->folios) return -ENOMEM; @@ -238,12 +224,18 @@ static __always_inline int init_udmabuf(struct udmabuf *ubuf, pgoff_t pgcnt) if (!ubuf->offsets) return -ENOMEM; + ubuf->pinned_folios = kvmalloc_array(pgcnt, + sizeof(*ubuf->pinned_folios), + GFP_KERNEL); + if (!ubuf->pinned_folios) + return -ENOMEM; + return 0; } static __always_inline void deinit_udmabuf(struct udmabuf *ubuf) { - unpin_all_folios(&ubuf->unpin_list); + unpin_all_folios(ubuf); kvfree(ubuf->offsets); kvfree(ubuf->folios); } @@ -353,7 +345,9 @@ static int export_udmabuf(struct udmabuf *ubuf, static int udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd, loff_t start, loff_t size) { - pgoff_t pgoff, pgcnt, upgcnt = ubuf->pagecount; + pgoff_t pgoff, pgcnt; + pgoff_t upgcnt = ubuf->pagecount; + pgoff_t nr_pinned = ubuf->nr_pinned; u32 cur_folio, cur_pgcnt; struct folio **folios = NULL; long nr_folios; @@ -377,9 +371,7 @@ static int udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd, pgoff_t subpgoff = pgoff; size_t fsize = folio_size(folios[cur_folio]); - ret = add_to_unpin_list(&ubuf->unpin_list, folios[cur_folio]); - if (ret < 0) - goto err; + ubuf->pinned_folios[nr_pinned++] = folios[cur_folio]; for (; subpgoff < fsize; subpgoff += PAGE_SIZE) { ubuf->folios[upgcnt] = folios[cur_folio]; @@ -399,6 +391,7 @@ static int udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd, end: err: ubuf->pagecount = upgcnt; + ubuf->nr_pinned = nr_pinned; kvfree(folios); return ret; }