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+1Swhagd2356o61y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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/k9ybt6KsFaNqmYMP53A5ZIwwPQPPhyoUwJjORWVYu4sEtAXH4y70hs3ZFFTQ/tKO9E096R+X0YBHj/Oim8080323J+F7UV80Q7PSnv1AhYChWFuAZ+I1M2boiaaB0+RSoEwvcOGIzqNi1FMapIPxuJb7XpKGwZU/6jhTJ96F8jAEyPN+W5czD8AUGq2tCUGI1YcIWaIWugyLAgRHe0qdil5ns7rQdIhKYKA07RUGV1n2pEr1cdQL7xy+JAcYJ0Ssh0zGD+CDoc4FcXzVqc3jIeSqKxF7RRMXPRDw7DwuFEAIOFaGTDitJacFpR6CTT18PYn7poCb74eoyVkw1CgxgzpkOip5FxWbWlLm0+3bGNNhhK4uA2RXjBkG+ZzlgqCjlMq/xrebhSr4ZNuyQgimrEfnlm6CT4jeU6lxepQV5vsv7e/A6x3XhkHyOIrUtbFwT29JlAiuasWms/qTmrhFR7Q6yxpjFwaP2sGwjDL62oyIByUeaSrypf3JRpZhi9bm2oB/ACeuHf1XKREJwjwc5ydKM3tPEvXQTmoaLLlr6A4X1NqoWKBUkDBnedYObHbgN1zRuteOzpVp//tT+WRd5vArV0jWBznnbhk/vf7KvCpCccBQZh+p5+cYlvGo+fyYudsTrkFN46810UJ9UeBkjDKLPaOw6lmDB6Tsa6Tw+bpqvyhQiReZIgYVAttcqoh6OKUJO4zGFYuM0UUOmdRl6brXuGIEwQfBq7zcf4hserP/LBiUZy+kvUX8BAIJGRRYPd/x/VaFB0EOw7arRioYLzxOesimEH2F38z1arYl4+ldourF5gekoaQPkloRdUh1PYyRZO0kOMqBj34vWrOJWbZdubwunsnyLSjntX1cc7xQGMOXk5rd28z59sDD69/JW42SHy4MK8ltuzumyD4+wMgXW3SGir9dBpgld33qs3mIOmMDYt6UCfqgEMbQQBLKzQKaul/0ViwInG301TyPVChJ+FqJgvY4WVOT8KlrX4OpkGHeC2kaJ+liCCtfGyxJOpnGMt2trK/7eRat4ilfKzMgX7PTSueBV8FqniRzchfgbHp8Z1fmk/4ZzsG9STJ2PjimBgHTWJARVCIYKxffTERq+CiIkx2ryQbqJXlxPTP/+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);