From nobody Fri Nov 29 18:40:06 2024 Received: from APC01-SG2-obe.outbound.protection.outlook.com (mail-sgaapc01on2078.outbound.protection.outlook.com [40.107.215.78]) (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 648C715099E; Wed, 18 Sep 2024 02:53:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.215.78 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726627987; cv=fail; b=iLN46a2K2yYjaWzRon15chB+oy5/c7N7SqWnVKMIi6o+aUTo39ONOzSoZIjB2nYVUUpfgmA7+Ev0y7rR6He+oywEy1BQzbrHm+v3RtE290XZyOEE0P6nOtUwjU1A5yOfB9Dms1ANc+x24+Z+FAu3uxDononiBcURbnsLh4g3ODo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726627987; c=relaxed/simple; bh=jk4/ZJxhCjruhYu630Y4VXiSl37DBQ/fcc7XaAc50EM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=jeluyu3+bsN4owWTL92My0XvliL7sRI8LbLKpk2ZTYTQ32155O6cPUGKnvQiDSb099m/gEJZWnPYMsr3dOyYTh8KnajjHf+YSBkN9TFE2hwyAFGg94Us38hvXboyBEU85IViM4OQBoWTOCQYm7s8yJl7KGEuo26QQfj9s8Idq8E= 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=lBSAn+iM; arc=fail smtp.client-ip=40.107.215.78 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="lBSAn+iM" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=alXeCrE0lsqZVSGIAAp+gCRhgITvaUZmTxoEejvR68UEou3A8Sbq6hLaiF0vzOpw0jQLimtv6eAoM5CSykomGIT3nHFcsdpvLrZ6YKtwV5Ow0Mp5nO/c0O5W6CNU9HS3CAK3HvOnUUNze9bRslH+NUA6Ylx2Xk51CtoVX3J+x6Ll/6P5bL+rCDHGv3q6DodyiI02INb1iHoSB3kNoROfhWi3gveFjA39WbHG0hHFsx/vixlJSUN3ldcdiAnKHTF3qwdjPvOUUd1gDFiN3bCfHZAwbda+1ge8IbfAf8n4cc4Jp8+NCzmfjR/1Bd7fsH0oyQ+WA3fNN3Trf73RK6MXyA== 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=eiAQP2XOZ/W2FfmhMmIpXM39JfU6KKhxAyOVGhBDjU8=; b=EUVqMsVtDMQ1729BVz6tgTsoOLTszpaJPZ5YiqHCMjhP1Zv5AboBqY8NzQldJ2dgVwc7Me/ANCP8Eeo7Y8q1u+bWQ8W7lXolB/4u2sI9ax1zTihfptJCTDeTIz5gi7PqwMv6A3qmbRoOXO9TCUqnl3ZDutYdOmscI1m8CMVgRDJKvXhuLW6SjCAg4yPLRHXIRAktVUuj8Y3lGGJUthWhvp767B2FroW7Dfiku7wA5Cx/UUxKAYRu+HcxSaRo4K7HRWQ2efvlC5xowom3Hz3pPAVjjRE7fm0B2/clV8Qzp8FszUmeQAkwM9qXkjqUQlHRbXv0f0KyoJYq5lztossUlg== 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=eiAQP2XOZ/W2FfmhMmIpXM39JfU6KKhxAyOVGhBDjU8=; b=lBSAn+iMTG3BtMDV8l7M79ReljBM84ez0CT7gD7hSkAetkbXaNlYt//IEUEeOS9ed5KrOXRwFHDmSLJsVwZKwM+TARtNJWJejNfiUIr8LBq4WVnVV53bvFZDRI/snL8Hunfr/iljwek3Ev8MuPkIzJ6DriBgM2IK1J5e4kdXVVxnik17mgZPJ8pNEO75yW2lTSms+E7GryE/nyM2qx1ESw2zTei8nDwju3OUE11isBZkFX3BCfiDXlzhq0nmjrrOs+89ACh7ZmBcpZR0OTVh3v4ibI4aZepUtCtDEkUORkr8Sf+hI3gRv2AxM9nCMolxRyGAIEhwrZcJj3rxE+IsdQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=vivo.com; Received: from SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11) by KL1PR06MB6236.apcprd06.prod.outlook.com (2603:1096:820:d7::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.23; Wed, 18 Sep 2024 02:53:01 +0000 Received: from SEYPR06MB5672.apcprd06.prod.outlook.com ([fe80::2f0c:5cf2:abb0:d852]) by SEYPR06MB5672.apcprd06.prod.outlook.com ([fe80::2f0c:5cf2:abb0:d852%5]) with mapi id 15.20.7962.022; Wed, 18 Sep 2024 02:53:01 +0000 From: Huan Yang To: vivek.kasireddy@intel.com, Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Gerd Hoffmann , Dave Airlie , Andrew Morton , 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 v7 4/7] udmabuf: udmabuf_create pin folio codestyle cleanup Date: Wed, 18 Sep 2024 10:52:27 +0800 Message-ID: <20240918025238.2957823-5-link@vivo.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240918025238.2957823-1-link@vivo.com> References: <20240918025238.2957823-1-link@vivo.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SI2P153CA0014.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::6) To SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SEYPR06MB5672:EE_|KL1PR06MB6236:EE_ X-MS-Office365-Filtering-Correlation-Id: db7238e7-bb33-49c6-529e-08dcd78d0283 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|52116014|366016|376014|7416014|38350700014|921020; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?v1kC9CsQTxJ0mHIPVzG/Wsv4vudd5MvZ8Bt57INR/J1gZsQfHs47rgwJAsQ4?= =?us-ascii?Q?UANat8vMS6O2L9l51CPa9BItHYs1Ua7BosB+/mfvQ7PkxzXr66UO6w4fCowK?= =?us-ascii?Q?40zB1K8mR0ky2ZbcqnAew+jcm8qFpYORih95YVaMtbY5kK9lC5F5QeP9vLvE?= =?us-ascii?Q?2IPi5yzDm7WBDYCcscPOw0tcpzgK0zxEEnMI5fun2yI4PePFZ6HztkKYFmIn?= =?us-ascii?Q?u3ET15KITrTJ4C/mo3Qq0RAhHc4v+VQ+ffotFJU2JayfJ/CVtwlgsMYW4iQv?= =?us-ascii?Q?kCSfJ2tbftbBd9c5wdttCo4laIhV2Ss2KubWb5gGEw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6LATmNGFG4OhTtBEsr5HkgzsCH7cAc7TBZwjn0hjAItShbJis6v?= =?us-ascii?Q?JosItVWHTptJWMHgs6vsTrsZ9EMInrhxyLlPZllGiXI/pjRC9zM57LcGueWZ?= =?us-ascii?Q?CXMKMSrkEwcUSrbrz6J9/cyPmcrM8U+LY4Bo9PNgKJNLqx9K2ftJsL6l5eZw?= =?us-ascii?Q?PEfmu+ypt+rtYKW08usCcZ83HQz6oBpVIpxtlT7JUl/TinowYp4kvp5radc3?= =?us-ascii?Q?qcOtoZJ1waU=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SEYPR06MB5672.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(52116014)(366016)(376014)(7416014)(38350700014)(921020);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?BlQY9Xq1zxH31eAA4W9V+v75iJiLImO/l29XouU1noceRvs4In+XrXVrFF4p?= =?us-ascii?Q?HBB5lPJGP4a4Vciu1bkG6/ogTQryKUNeeqxUbwysxs99SYtkcXj1ixhKWq0X?= =?us-ascii?Q?4LTxU63JO5ts/0G0QdBslk85m8YfyJ3shYbAjf6Jr3YoysMk2R3dP8JFfran?= =?us-ascii?Q?xFFYj8C61eY6EWDffHWv7CSqR2EspUpdt8JUh3shE3BXyrE52IeI994E7FPp?= =?us-ascii?Q?/cwNlFSr0TcguqjIxrQkFBUVrumGTcb8U+XBytBdFcUR+Ij5FCM/leHwfWSj?= =?us-ascii?Q?5JELOXkKQbSbTjh4hVLZjYRpjtoHfMOTHySQ+etfMqLL8WyvMTUJuNVb9Bam?= =?us-ascii?Q?bSCriWMwJjyMaLArTP75m2GPJAVUUbbKTkFcGA/IDB7lET9+AuTKxiXt/peK?= =?us-ascii?Q?8kO38RtKYT7xIhPL48O0vTuzxfj5M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?us-ascii?Q?cZfvWGg0y/FGK7WaYysdNz6X30ULz/T//Dn/h6V65MeBMFd0iXzZhD0QWj9a?= =?us-ascii?Q?oySit7n2p3tIiQIFEMO6wwthS32TsBRQ4unwxT8p7GsTJxYId2vAnfTpVDb8?= =?us-ascii?Q?5+HI2z63nMGU4dh5wINfm0Q7LuEwsuk4rQE2L40MSy7ZaR7dMH9lroR4lCz/?= =?us-ascii?Q?UFlVImP/nQ99hzyDgwnu7zwn87MktfoxIisZDZKtUVxnEA0EDhUU/3kTrW3O?= =?us-ascii?Q?hhAjKoiOjCuCIc8wmNDjCsu96mELCwo5MBdt1asG?= X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: db7238e7-bb33-49c6-529e-08dcd78d0283 X-MS-Exchange-CrossTenant-AuthSource: SEYPR06MB5672.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Sep 2024 02:53:01.4094 (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: 5EbJbCiHdfJsz0W1YlLe2ByVH4aXufYQjMYQxKSp+IWZeQKf0WOqq7yDvVNXxsaE22j5QoubdRcHHLpeedY9tw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: KL1PR06MB6236 Content-Type: text/plain; charset="utf-8" This patch aim to simplify the memfd folio pin during the udmabuf create. No functional changes. This patch create a udmabuf_pin_folios function, in this, do the memfd pin folio and then record each pinned folio, offset. This patch simplify the pinned folio record, iter by each pinned folio, and then record each offset in it. Compare to iter by pgcnt, more readable. Suggested-by: Vivek Kasireddy Signed-off-by: Huan Yang Acked-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 137 +++++++++++++++++++++----------------- 1 file changed, 76 insertions(+), 61 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index aa182a9dcdfa..9d58c045b8eb 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -291,9 +291,6 @@ static int check_memfd_seals(struct file *memfd) { int seals; =20 - if (!memfd) - return -EBADFD; - if (!shmem_file(memfd) && !is_file_hugepages(memfd)) return -EBADFD; =20 @@ -328,17 +325,68 @@ static int export_udmabuf(struct udmabuf *ubuf, return dma_buf_fd(buf, flags); } =20 +static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd, + loff_t start, loff_t size) +{ + pgoff_t pgoff, pgcnt, upgcnt =3D ubuf->pagecount; + struct folio **folios =3D NULL; + u32 cur_folio, cur_pgcnt; + long nr_folios; + long ret =3D 0; + loff_t end; + + pgcnt =3D size >> PAGE_SHIFT; + folios =3D kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL); + if (!folios) + return -ENOMEM; + + end =3D start + (pgcnt << PAGE_SHIFT) - 1; + nr_folios =3D memfd_pin_folios(memfd, start, end, folios, pgcnt, &pgoff); + if (nr_folios <=3D 0) { + ret =3D nr_folios ? nr_folios : -EINVAL; + goto end; + } + + cur_pgcnt =3D 0; + for (cur_folio =3D 0; cur_folio < nr_folios; ++cur_folio) { + pgoff_t subpgoff =3D pgoff; + size_t fsize =3D folio_size(folios[cur_folio]); + + ret =3D add_to_unpin_list(&ubuf->unpin_list, folios[cur_folio]); + if (ret < 0) + goto end; + + for (; subpgoff < fsize; subpgoff +=3D PAGE_SIZE) { + ubuf->folios[upgcnt] =3D folios[cur_folio]; + ubuf->offsets[upgcnt] =3D subpgoff; + ++upgcnt; + + if (++cur_pgcnt >=3D pgcnt) + goto end; + } + + /** + * In a given range, only the first subpage of the first folio + * has an offset, that is returned by memfd_pin_folios(). + * The first subpages of other folios (in the range) have an + * offset of 0. + */ + pgoff =3D 0; + } +end: + ubuf->pagecount =3D 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 =3D 0; - long nr_folios, ret =3D -EINVAL; - struct file *memfd =3D NULL; - struct folio **folios; + pgoff_t pgcnt =3D 0, pglimit; struct udmabuf *ubuf; - u32 i, j, k, flags; - loff_t end; + long ret =3D -EINVAL; + u32 i, flags; =20 ubuf =3D kzalloc(sizeof(*ubuf), GFP_KERNEL); if (!ubuf) @@ -347,81 +395,50 @@ static long udmabuf_create(struct miscdevice *device, INIT_LIST_HEAD(&ubuf->unpin_list); pglimit =3D (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT; for (i =3D 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 +=3D list[i].size >> PAGE_SHIFT; - if (ubuf->pagecount > pglimit) + + pgcnt +=3D list[i].size >> PAGE_SHIFT; + if (pgcnt > pglimit) goto err; } =20 - if (!ubuf->pagecount) + if (!pgcnt) goto err; =20 - ubuf->folios =3D kvmalloc_array(ubuf->pagecount, sizeof(*ubuf->folios), - GFP_KERNEL); + ubuf->folios =3D kvmalloc_array(pgcnt, sizeof(*ubuf->folios), GFP_KERNEL); if (!ubuf->folios) { ret =3D -ENOMEM; goto err; } - ubuf->offsets =3D kvcalloc(ubuf->pagecount, sizeof(*ubuf->offsets), - GFP_KERNEL); + + ubuf->offsets =3D kvcalloc(pgcnt, sizeof(*ubuf->offsets), GFP_KERNEL); if (!ubuf->offsets) { ret =3D -ENOMEM; goto err; } =20 - pgbuf =3D 0; for (i =3D 0; i < head->count; i++) { - memfd =3D fget(list[i].memfd); - ret =3D check_memfd_seals(memfd); - if (ret < 0) - goto err; + struct file *memfd =3D fget(list[i].memfd); =20 - pgcnt =3D list[i].size >> PAGE_SHIFT; - folios =3D kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL); - if (!folios) { - ret =3D -ENOMEM; + if (!memfd) { + ret =3D -EBADFD; goto err; } =20 - end =3D list[i].offset + (pgcnt << PAGE_SHIFT) - 1; - ret =3D memfd_pin_folios(memfd, list[i].offset, end, - folios, pgcnt, &pgoff); - if (ret <=3D 0) { - kvfree(folios); - if (!ret) - ret =3D -EINVAL; + ret =3D check_memfd_seals(memfd); + if (ret < 0) { + fput(memfd); goto err; } =20 - nr_folios =3D ret; - pgoff >>=3D PAGE_SHIFT; - for (j =3D 0, k =3D 0; j < pgcnt; j++) { - ubuf->folios[pgbuf] =3D folios[k]; - ubuf->offsets[pgbuf] =3D pgoff << PAGE_SHIFT; - - if (j =3D=3D 0 || ubuf->folios[pgbuf-1] !=3D folios[k]) { - ret =3D add_to_unpin_list(&ubuf->unpin_list, - folios[k]); - if (ret < 0) { - kfree(folios); - goto err; - } - } - - pgbuf++; - if (++pgoff =3D=3D folio_nr_pages(folios[k])) { - pgoff =3D 0; - if (++k =3D=3D nr_folios) - break; - } - } - - kvfree(folios); + ret =3D udmabuf_pin_folios(ubuf, memfd, list[i].offset, + list[i].size); fput(memfd); - memfd =3D NULL; + if (ret) + goto err; } =20 flags =3D head->flags & UDMABUF_FLAGS_CLOEXEC ? O_CLOEXEC : 0; @@ -432,8 +449,6 @@ static long udmabuf_create(struct miscdevice *device, return ret; =20 err: - if (memfd) - fput(memfd); unpin_all_folios(&ubuf->unpin_list); kvfree(ubuf->offsets); kvfree(ubuf->folios); --=20 2.45.2