From patchwork Wed Jul 31 09:02:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 815842 Received: from APC01-SG2-obe.outbound.protection.outlook.com (mail-sgaapc01on2086.outbound.protection.outlook.com [40.107.215.86]) (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 CFD791CF93; Wed, 31 Jul 2024 09:02:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.215.86 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722416574; cv=fail; b=BJudVpank4mnTUK68TL4SF0vXBcapqyVTbREqLqPuq492QTcAIulwHDplUdK/psiZvltjING7ALUMp7KjAsPHaDIWwaE4oaZH5muYBbrZeS7hedfr5hRbRzrtKvMHcO+rMXHgNy5lib1ZuxGSpdpdslE3EeXywg4f8/2qzN0kQs= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722416574; c=relaxed/simple; bh=BjdyK/sz5gEkEg7fImNeX7lKO3p1NfDSawwj+nib05A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=VzEFo6FVhVhcTa58N5XBJE/MlZySvxipZsiuGodSFrQQUanlvNBkOJpiIh2fxPspJrrfwIwNcLAULgnk0Lsgx2qhFNlGjqoAs5T6mgkJSr5emPctiqfcAp7B2sqPr/um3HApdQLp22/HRTIPUQYvtUAwqockeToMdozuoOW+dO4= 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=i4SncwO2; arc=fail smtp.client-ip=40.107.215.86 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="i4SncwO2" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=RD+I1+ebJ93/Ea0CceJgc3Eyc2Y4D8E6ZfnmxQtJqpiotAwYBcvzwweOo8tyr3lE1tIx24/tqnXE7RIY1amW1MSGEy3ThJ94SkYR61UEu8bYN1yA7/h0eWjmu9NM2EfEviXYCEURb9k5thuy9iohCz3sRbnjEpKMn/xRWfafLUsajKWRWahvOq/B2QuXpoMHjXEqN3CVv+Lm6veuA1k+4GzrrkWybiaAmKaXPnu+wHJ59MAKR/uLz0jEUkSS8Yeyf1d1xeOtdQMj1ceQ1KbhMqf7gClgLKxH3wxfyUIEWzCAsGWz7Ija41eBhDNftdn3ywjFw7DY5E8lvtkrZh69hA== 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=rh6mMFBK64P5wZb+ppyB0dcKV5TjJKuBEtiJnIYiZwg=; b=F2e8sbXoam2w4QSAxEqQw+7WPAibiONsYdsjF2RFlibiuQ/5juoI6l5rO1PHBQSb9ijQ2Gs5fxQlVAXhOFtcs39R0P3HeNZy9859ui3UmqRyRLBclXwlqxkO6cdb0rITU4/1szGV8tSe5SjImVCp09zo93cRn3WxXSCE60xO9HErKlDYFee/AQ2VG4hlaFCOiLv8qPB+TEeHb2Bl+0KpxGEL7XS/yjrnz+aAc+vIVj8dT0XWU7XbVUtvbAIr+zgTeCmxH5shqD1+jEr1ZDJTuIwehouDq+WA1YQyU8zPbgaZ9YjM4nwdY4StslTJgYfnk1jJ5gWjlT3Fwx8tdUVfbA== 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=rh6mMFBK64P5wZb+ppyB0dcKV5TjJKuBEtiJnIYiZwg=; b=i4SncwO2w6IeBvbOznB5aociyshFbEhiWlnoLrcNJxTF2pgIL9Kj84z++Fjhq30Eyv62Awu2YfNHlMAxnLalS55AmxPAoXd5rNy0NGAlns5rRWMEHmD0YDYzayXet7+MEix0llOtFBdAsCQsbm0tUc1Yt1on6arBJxLmPvoV4WnYZAuLHjbugiBHQ7IDHFxc3gZw4K5LFytN17c81TKRwmumljOSH+0pQqzczpGHs2hgPUTlXSZmM4ZUjotAwcdwDMYYcTEkmTuGVCvPsVVbRoejgLxTm0Oxkagx8v0DviXvwcKX3ib2SGUI8qM63Tx1O+2g0UvvsYiSuhEljxYs/w== 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 SEYPR06MB7027.apcprd06.prod.outlook.com (2603:1096:101:1e3::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7762.33; Wed, 31 Jul 2024 09:02: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.7807.026; Wed, 31 Jul 2024 09:02:47 +0000 From: Huan Yang To: Gerd Hoffmann , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org Cc: opensource.kernel@vivo.com, Huan Yang Subject: [PATCH 2/2] udmabuf: make udmabuf folios and offsets set more readable Date: Wed, 31 Jul 2024 17:02:33 +0800 Message-ID: <20240731090233.1343559-2-link@vivo.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240731090233.1343559-1-link@vivo.com> References: <20240731090233.1343559-1-link@vivo.com> X-ClientProxiedBy: SI2PR01CA0035.apcprd01.prod.exchangelabs.com (2603:1096:4:192::13) 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_|SEYPR06MB7027:EE_ X-MS-Office365-Filtering-Correlation-Id: 6c10943a-cf4a-4452-c13e-08dcb13f8c1a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|366016|376014|1800799024|52116014|38350700014; X-Microsoft-Antispam-Message-Info: lHQCp9t2YyrcqD4kPKk49qgMEHLFvgAlxq120pTPBAaczC+NRMnwum/VBJcpM3qXVaJZMn/iMkSpegyF3DAAT/xj7a0Qt3tj4zJN18BQ7wBMY/hBPePedvelLOphFH8xgoDkXHr4iLMK2yz0gRgzTB3qXe2BSdzQXzMBUgcl0ROjp13i+Qz7mKhrYRZULc4qVJBryT6l9HZiqDIKKxVHzsK87TF8AF+Kxzj7/AgmmaDgkRrlj6Lht6p3pYVWxnLZgRLixT+/cR/6l3Dx409lTcfavuSnKsxKPSLNyuqImo/3M5zZPD0tIomXeaN9vshuItiN0o2DH+WRlOoXMuLhRlfbAYTdtqFSX4+etPwuUXMjdQ9BjpH7idWD32z4TLs92gmjkVL6KfuChKQet+Rsaxskb1+Nnw5BTZhgcMrVdv1UQKnz7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PR06MB5676.apcprd06.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(376014)(1800799024)(52116014)(38350700014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: lQ02AiDFhOE6uoaxFqHPB795rw8PpaESQzi/1bHUxYFkr5dnUBVEkUE7v4eDTIZYMQuNNm9VNhBEzDLOY7u8fFnYyyRg9DjPPvTS8+C6Q+6onF1t5Dy999n9znIri4oKAdo9jTcWzpdEH91updM8BLfcPYeZGXz56UGTunSQj9qit8msAkj6OJMnI8kg3yZN50lUZxQmPHz314v/rBJ+u/bkjmeP0o0+NgyF8zS+PtTSsH4p0rDLFjg2C72zBqdfZGr1WKXLR+CAOxmGZRClMD9hqWb87eLI0c+znHi9hakzlfAGIgxkZ/iCbOXIO5A57iLuqAd/mA1D35T3zgho5jYQ0jNSFm/la2eaVCWwhcqBshz33j+rqqdyeGkHj14oSn0ErIVPvM5qDfLK4/hl8sXra+wxbyCNFzpMzq8FwQcRPoR+3nJAeI/iHxqFvObkU0fVdeRwAHhp6BvDNPMbpYmdiT0k0yH3BUX9aLE9D73Qs7EKm7H/LqoxV9N6fCAoxr6bcoHDmtcwYodX1XwfKZqMmj6stpMpcRmgCmi7nonUpG4EDRq7Hdfrt8CcJx2E1ttO97yyccyjqbDt1CWUbiAQPe6p3AaJgd+iyF3ufOw41vK5nssTWWj3/uoZa4ZXUwK9HUcOmFlp4R14vKvG8by6guogqZQphgGvIOm+Twv0FwwBK2JqjY45RFbbpPPwgZ0n6g4E1Wa4CzF/VWlPE3Y1C44AXPLc5IQ+3d0Ro0qws5DWUe+qDavxS70crQiN0Tbyub/lz8dxtMxOd4MK8d55V9TkOU8L4sx69Bq85DLXOzSWQxzsggF8/NdARiO80qqRmwBwPmhQGgHMzYEC8Dl6yLMxHAnFdDCzCb3hnYfFMQBR3NJesp3J5paN1I1M0TrBU52863CIKcIUaewfoOQrHAiWZTx8e/77isIi2KYQ7XLg7gIxxeATkasvbPJZi7CQDtI0TkFgiUEx2xvm6auZfeh23X8s9KWj1hxwDxJxRgak+GQcU0XXlpZgFmudWjPSiEhVhPwdGL2LXQZD/LzPInjZxrFarVhJmqcPZAE2njlOCDORN/V/Uk44GLeJT00MJl7Tb/plSewacL6aZJv8l/3NLHYy4jQiTdsy19ZNW7LSEwe5dFMUDDZKyIu7TEIJSTcF4DLCYkk7Zdmr+tjmRChhol/EjSQ7Euhj/ZFKukwG1CyaD+2+wWNAqpYVKqmK4Smk3ntvpmSetzUY19cdCkn+p7NrtXGggk7XoyEHZql0bcyIvur5qfRL32Q7FQgmQvn7xM/FDEc0/jxioqf7psVoW1xUH1Tr6r7U9f9ihS3JD/Q4zWqIywn2k6ruD7UH0AeoNRXJqeT0dDmoxWL+9cm+P0iPrLm1txS6vNjlkRyqtHGIymXXJY6fz44V2/+mhNncR/xfwH3I1GwqSodpZWz+RE6MJ2IBIl98Z/6+/cMf9VhT3O+twnFbBr4qeK2AodjqCU7/9Ha4vxmy7g2ZunQjKKp2bLQVn1zi+vJUG0XGSYlrljHOeVzj03HpIDOevHw6NSqPUhplsvqJ5Ma6zbxFzZwJpjjBc8cJG19eQINbEq/A5eeh90Bt4FcO X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6c10943a-cf4a-4452-c13e-08dcb13f8c1a X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Jul 2024 09:02:47.2708 (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: zqZ8EUSKMSj5uuje2ME9i8oo+xM+JpttON2kqXir4SDtqmRIllSvVNFLpy95XOj9b7WcWy1eQ+MZJTVHQ6kh3g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SEYPR06MB7027 The current code for setting folios and offsets is not very readable. In fact, udmabuf->folios represents the head page of each folio, while the offset indicates the PAGE_SIZE offset within the folio for the corresponding pgcnt. And folios only add head page(the folio) into unpin list. This patch changes the loop condition to use folios as the outer loop and sets the subpages of the folio in the inner loop, making it easier to understand the relationship between unpin and folios/offset. The loop conditions for j and k are actually only used in the head loop, so they can be turned into loop variables. Signed-off-by: Huan Yang --- drivers/dma-buf/udmabuf.c | 40 ++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 6604d91e7072..0285194e6b51 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -301,7 +301,7 @@ static long udmabuf_create(struct miscdevice *device, struct file *memfd = NULL; struct folio **folios; struct udmabuf *ubuf; - u32 i, j, k, flags; + u32 i, flags; loff_t end; ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL); @@ -338,6 +338,7 @@ static long udmabuf_create(struct miscdevice *device, pgbuf = 0; for (i = 0; i < head->count; i++) { + u32 j, k; memfd = fget(list[i].memfd); ret = check_memfd_seals(memfd); if (ret < 0) @@ -360,26 +361,27 @@ static long udmabuf_create(struct miscdevice *device, 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; - } + /** + * Iter each folios we got from pin, add each folio into unpin + * list and setup each folio and page offset into folio into + * corrent page count position. + * So that we can iter any offset in size and get correctly + * page. + */ + for (j = 0, pgoff >>= PAGE_SHIFT, nr_folios = ret; + j < nr_folios; ++j, pgoff = 0) { + long nr_pages; + + ret = add_to_unpin_list(&ubuf->unpin_list, folios[j]); + if (ret < 0) { + kfree(folios); + goto err; } - pgbuf++; - if (++pgoff == folio_nr_pages(folios[k])) { - pgoff = 0; - if (++k == nr_folios) - break; + for (k = 0, nr_pages = folio_nr_pages(folios[j]); + k < nr_pages; ++k, ++pgoff, ++pgbuf) { + ubuf->folios[pgbuf] = folios[j]; + ubuf->offsets[pgbuf] = pgoff << PAGE_SHIFT; } }