From patchwork Mon Sep 9 09:18:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 13796488 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id ACFE0ECE57B for ; Mon, 9 Sep 2024 09:19:17 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C323F10E347; Mon, 9 Sep 2024 09:19:14 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=vivo.com header.i=@vivo.com header.b="fSpub4LG"; dkim-atps=neutral Received: from APC01-PSA-obe.outbound.protection.outlook.com (mail-psaapc01on2043.outbound.protection.outlook.com [40.107.255.43]) by gabe.freedesktop.org (Postfix) with ESMTPS id 5E92810E341 for ; Mon, 9 Sep 2024 09:19:13 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=VK+Sypy38ttK0ZDJ6rNgFj10cJdcw+TKFH9Eeq2Ifotev3eqbpd0sZjrgZ5DlolQ7V8fLdMAelnhG0F3ND87a/QxUwOBIpORXns149VGPo2uaY5R6zgwDQ8r9V+4VDCx6IgCnBgA5y0eDI5T1yzLTY1XgYhmGYIVr36GSArlRHoaJAporaGwgIEX6/6ZNraJ0NxLCOfJOhdXMN5btKdoGPmt7KBvA4K2WBvwBzy4GWZVi607fKP5KHEFrchWzawtaQ2SUp5WHvcya957V3BwDf5DZh56q0gHEQOhim0A3BqSvBSLiJ4o2lKyAYJgZW9fkKbvcRVm9gDwcu+KxiO9ag== 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=93hIlXR6rTCFIQW+5KF6L9/Zv17kVZyN1YP+CsH762Y=; b=IBJq46i+MDJIrHpwc8Xbk+MqNZThsVd6uFadfEfiiHTw2K32sdOA+fYu/VoSdxZWemRrsO78erN82WKy5+a8DmgYWigjKFWVolCQrb5psdqbEt1w1frldnAazxXvJ8AOXkzKas20yd9fxw0h7cVbEkc0IOKpbzW4nTLsxdzHTDBuLDJhIoCnBDh3gNYbkKO61/yU29gSskLWZdPLDtjSzsmMXACCT055jfQxXvr7EwuUJa4P9Fh4x0YtUNCYDUGQqUPcuLeDlN/4pdfL8q67Mr8wHIZrRizDebNcQse05ICLol9UibWsHpBYK2SxC90NmKWsTS4uV8hrrnBWLxDkOw== 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=93hIlXR6rTCFIQW+5KF6L9/Zv17kVZyN1YP+CsH762Y=; b=fSpub4LGlr02q2ajoJGRIOQOcZ4PN18Ph2nmDzUv8xbLsAUl09kjLmNUhcWUIFQykTiQQuU436Dj67t7UNXdfR2PcxtNCo8I59zgUgO8g3cHKKt+9z05OlVDIjSNXDd2Lmbf25Zk3ziLBOx6zIk9ErLXVkH78jULcMecoeNftyA9xeQCmLYMjjbaYSsbVKRo73wa9vY8cPhOK3H1JBVgesuNzIPHJhbfIyrUnF+9bOfxjaGIxL+xYoTM7bbN2AKE1xjYp6R0k/F8pKJUolHM5c2NT0fasamHdP1DkeYPLscoAQ8Nxu5LcqU9xlXIca9xKhRnLodEtHAV6sJdSX+CNw== 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 JH0PR06MB7180.apcprd06.prod.outlook.com (2603:1096:990:98::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.27; Mon, 9 Sep 2024 09:19:08 +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:08 +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 1/7] udmabuf: pre-fault when first page fault Date: Mon, 9 Sep 2024 17:18:39 +0800 Message-ID: <20240909091851.1165742-2-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) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PUZPR06MB5676:EE_|JH0PR06MB7180:EE_ X-MS-Office365-Filtering-Correlation-Id: 547b8ed0-d8bd-4b8d-bcae-08dcd0b07595 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|366016|1800799024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7qvkfITjPVB6UbuW2raZ85rwFu/fl4KA91iBlxixlteJGIviqCc+at2sGzXJ1HjYBnBwJ5FTMZicpXkiHboxo7AqHUfPOAhjDneufTmdxMJWD1mKe5xlkSEr2EGsbccdBW99cpYAcaF0A7Kap45CAPR5ZNwDOo1itay1CJSiyp3sc2sHgFY7qM22AcAeON1pDp4Xx1RDCV+10azMkOIeFbaZLZIwgyn4mGfpiKoYrDAOKVhutcJFk5q/6KB+jPxlTsZbuq/aur+I+XJpPGqLCvRWMplRTNwEPn7IEcbEO5zaU11kCjrHJELPTEx7oE2RROawa01E5DoKb8zYGO5D3OP4Fj4ym99gedWgHkoWIqjnvlBWtaNWBlxK1+QV9/t8xrw== 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: fpI95VbIck56EVrOyABw7n5ijNyOBQ+aBg6AsRrb0hcQ3qQr73G4Biw5YHHnRPtbhG/pNT/CPClHOz2kazusjVLFm1OLqWck8M4O/qSeRF+gHlAFWT7GRE5yU3IrFovVl7YLFWZwjiwvKtCHtZjN79w0/6QiNAwNzt2tHfDWAhugntuoqhh1fqaH0a9PvE00zzaMNyzcbia4G1JiAYq+tnTFU7Ll/9fxqxq++ENeP8gNZKZhrcL00gESDRSoPwRFutuKICQEVcjWH85D0o4Pce5zCloLav7VcWj0v6eBVhOiP1kfgOb4VfcGwxSNWVrD+t0UvDptNvkYVYBrYRfAJgXEY7LTRoNcv+KAr+i1weLnvXyv3PLwiNXfJC2IVRF65loJlYD616NYhlBLO2DWgeyYItqlCPQTH5drhfyr8VcrHd5h5wH5VRpzvHI/pGbSQlCTjM8xrXe0Y/rDHXrLjVusyX1ud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E6wybNyJqAeWikiA3XCqp4EGABBuQEvdOtJOR/gJ+WoDBQE5DHzS X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 547b8ed0-d8bd-4b8d-bcae-08dcd0b07595 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:08.6705 (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: 5DwNIy56TfWgpzjZsGXBrx6oKRF2yIxXPX+heaJy2srxFJF0R7J/hYghXJ+g4OgdVCdpn87rCzbwCcpodibVqQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: JH0PR06MB7180 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" The current udmabuf mmap only fills the physical memory to the corresponding virtual address when the user actually accesses the virtual address. However, the current udmabuf has already obtained and pinned the folio upon completion of the creation.This means that the physical memory has already been acquired, rather than being accessed dynamically. As a result, the page fault has lost its purpose as a demanding page. Due to the fact that page fault requires trapping into kernel mode and filling in when accessing the corresponding virtual address in mmap, when creating a large size udmabuf, this represents a considerable overhead. This patch fill the pfn into page table, and then pre-fault each pfn into vma, when first access. Notice, if anything wrong , we do not return an error during this pre-fault step. However, an error will be returned if the failure occurs when the addr is truly accessed Suggested-by: Vivek Kasireddy Signed-off-by: Huan Yang Acked-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 047c3cd2ceff..2170d975cc76 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -43,7 +43,8 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) struct vm_area_struct *vma = vmf->vma; struct udmabuf *ubuf = vma->vm_private_data; pgoff_t pgoff = vmf->pgoff; - unsigned long pfn; + unsigned long addr, pfn; + vm_fault_t ret; if (pgoff >= ubuf->pagecount) return VM_FAULT_SIGBUS; @@ -51,7 +52,35 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) pfn = folio_pfn(ubuf->folios[pgoff]); pfn += ubuf->offsets[pgoff] >> PAGE_SHIFT; - return vmf_insert_pfn(vma, vmf->address, pfn); + ret = vmf_insert_pfn(vma, vmf->address, pfn); + if (ret & VM_FAULT_ERROR) + return ret; + + /* pre fault */ + pgoff = vma->vm_pgoff; + addr = vma->vm_start; + + for (; addr < vma->vm_end; pgoff++, addr += PAGE_SIZE) { + if (addr == vmf->address) + continue; + + if (WARN_ON(pgoff >= ubuf->pagecount)) + break; + + pfn = folio_pfn(ubuf->folios[pgoff]); + pfn += ubuf->offsets[pgoff] >> PAGE_SHIFT; + + /** + * If the below vmf_insert_pfn() fails, we do not return an + * error here during this pre-fault step. However, an error + * will be returned if the failure occurs when the addr is + * truly accessed. + */ + if (vmf_insert_pfn(vma, addr, pfn) & VM_FAULT_ERROR) + break; + } + + return ret; } static const struct vm_operations_struct udmabuf_vm_ops = { From patchwork Mon Sep 9 09:18:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 13796490 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C2BF0ECE57B for ; Mon, 9 Sep 2024 09:19:24 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 3F94310E34E; Mon, 9 Sep 2024 09:19:24 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=vivo.com header.i=@vivo.com header.b="glFJwnmb"; dkim-atps=neutral Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2068.outbound.protection.outlook.com [40.107.117.68]) by gabe.freedesktop.org (Postfix) with ESMTPS id 2F38310E34D for ; Mon, 9 Sep 2024 09:19:21 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=qAs0Gjc8bpYeIdKExTRErgm/RVgPLp2Cft8QOxYsVcE3TWIQX3//l/v70y/7+41MJvpgbgad8oi+3qgskZLYk18CeyERyOIcHZc+IYPeRf6gWiRHU6Z1ZbiEBSlbE8PTlUetrW5MDAyb8SCkx362YPKTzZC/pDcuqLcCxgfzsxDnhu7ejNaqJhZQnLXSRop0pt9tWM4YUuLgkJ1NZH+sTNqV/2qu7NwjqBAbrs+scThVhnCmez42x59cQ1v9jB9bVSyDUC6WKv0YfCjpdbDiiczZLumESgsdp8gDBNpRAJmhf19dEAa13I4dGOILeOSF9cFqykL3HIZfevMx/IMduQ== 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=gMzgGgj3NgwLdKXnhpYPZxgFQlER0fazfsN7tOlr61Q=; b=ZxCeur5Lv0mAJDS08fJtS2tpDD0e5tWw84eHq4VYxuTyWedErCvrJCGIX+NzDqQOsHn0KG8iTIz5Gmw3QUyoiS1cTiGd45nMm25sfgjxR2tqAFDGke5G+PKJwo/Qu3zUEtztUC2fiHR20m3q8tKdSJpnsBM8OW+Z1BSmGYuKTLkrNoQ9MmujxbhxIk8OH9thyChNWqGGx2wSsmxeqpYdPR7wzd3PUVgoXY8aKTsRpaUXBtssOTZtjdQH7eSLv5TkJhyTfKVLpCElpdpTr0sYPVMyvBqy2Pl7dqD7XLuGwFdpsAWlAdG50w9bjhsu93DB3TIBpSty2+l1OFfNLoMqPg== 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=gMzgGgj3NgwLdKXnhpYPZxgFQlER0fazfsN7tOlr61Q=; b=glFJwnmbiU8AMrsh6sRfqekCVoYy5pVbaxISryxonDS+bEmV4nIRMRG/lJ4ClzCbvHhFrw7xyw4AoxO5pt+QQAL8IJdmRxD/JVlQTj9ARJUyY2lReraweylZimbZm+GZUmmIQ402KblO9MPseLzkwhQRWrd1Yn3/xvO4X0SCvI2pGLpc+PPhSy0h2F4OqcSrmVoqIAfxV9fPP6ER2AmzaoyasCfu85QDHtZ9ZOmh+/ZAa+n0WR5pOrugLfqJKvyQyY92/SCbj00vi8DwDOUNyzpgxEiLX+9R31dOzZT6QRPVFqeNZqv1NnpsYcx1YDyKUx/FVWIH35+fH4xr4xcj3A== 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:14 +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:11 +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 2/7] udmabuf: change folios array from kmalloc to kvmalloc Date: Mon, 9 Sep 2024 17:18:40 +0800 Message-ID: <20240909091851.1165742-3-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) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PUZPR06MB5676:EE_|TYSPR06MB6292:EE_ X-MS-Office365-Filtering-Correlation-Id: 956f5a06-60d8-4fbb-0adc-08dcd0b07709 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: =?utf-8?q?mg0KX9lcSH+gSCq2ACcS7y6FPlneYz2?= =?utf-8?q?eUOSl5/cpEdtgm/g+jpM1KPu4C8aKWjAnhL3ZqQamxZSczZNvg7jkxKA3HoWNN85R?= =?utf-8?q?TwIjgNzOfT+xRQHz33S1LNEPoSBkNkG+hWtqKzriG3NC/kJ1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bAP8/8WbFess3a?= =?utf-8?q?gykB4KXwk4feeB1ahg1feFKJvhmjzVDg7GWg3qSgiVJ1VDGuyTji+54jikr1CCAsX?= =?utf-8?q?Hgp2gYnAcK0K76M43nAPuzd7s656Nddvr8VYC6tWlQWnD1NpssSZzbKSwrQ7gkvFJ?= =?utf-8?q?23za1l2g/xBtf3k0jFff7icWwaqiGbq4u3pcXUDYwUzyG2o6TRqLv7bkXIh6q/TMe?= =?utf-8?q?J4ehjtQRhU4c6tg20ajNl2WRcqai9pUT/lVTfk3WpJPFbuOasRDv0t5DOJcnQ4+st?= =?utf-8?q?JQbtJDaRY/UzF9mya5F9aE8QD2dWcpzoGevEogmpDImVljX7pON8XgOPm2jUJ1BQ2?= =?utf-8?q?1tEkr0vAxJ8V6W2DWGW5jactwbAWbkw54w=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)(1800799024)(366016)(376014)(52116014)(38350700014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?zxTJXl52ps6RUbzsHEh8C/g+iZ4O?= =?utf-8?q?YCBjut53vJj4OEgarz4R9aw+VqEG83O44lzU9PO1shh19L5pe9QagdIRGk2gDmDTT?= =?utf-8?q?E94Rl3v4twmEmK6ecHJk3nRT61MUdp/Eld/0s364hA7ebS93ESNEhP56rXDjky7jx?= =?utf-8?q?mFUfpsN1GDHSPdnaFvjM7NA+1RQNyeO6s2PWqGxEHMu9OHPsJbUr6DfLzeaBj2nMG?= =?utf-8?q?5NM/jNrj3FmjQcdrDwHJfr8ESi8q1Ip/4YSNFVSpF855WLknyZu8FnxARO5whG/jy?= =?utf-8?q?BThj5rDsY79GLzgxXZuxnlDDbU5K41e3fY0MvkPdzipsA+jQtAc9lHeYR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lsNmLR2LenNWmjJBgLrMJ7Ys?= =?utf-8?q?bdidqYo30o9HiJOWysd0oOytiiCZbkzPhqwJc0pnLd9fkHBRC7jOSA//ZqqeqYLh+?= =?utf-8?q?3gR8290i3NBYPZX3HnNqc8RcEiIqUUfTOVEywNCwkDrxyU+ODfeFbLOxKrrPSIHoB?= =?utf-8?q?d6+M5egciYUygUhC0R2fuhHCMnxx6dCfZJdtv6m9+49fZnD0ATOd+nf+rF+x/lnp6?= =?utf-8?q?3CspV3RHLHzExEpbFAvNLy+7cVXHmfgxIXL8AnAiyYkXKx+anhRLJeWqHkt25LJGe?= =?utf-8?q?E8nWeT9FCUCDNrsu8IKRuKkJMeqdg5NdGnSnBQr6SVJ3OP0IgqsJBqF1AjKRV66FT?= =?utf-8?q?SQH7BEzzMK5W?= X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 956f5a06-60d8-4fbb-0adc-08dcd0b07709 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:11.1327 (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: YBXjc+mpZYaNvvYAJfUyDmJIQVpGWWmJuC7gai4PM6CxBF5/jnjvXQIMAOyBZbmMK70Tk7fntRMyRcjfm2PhAQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYSPR06MB6292 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 2170d975cc76..ba9dbc7caf71 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -109,7 +109,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; @@ -117,7 +117,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; @@ -225,8 +225,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); } @@ -351,14 +351,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; @@ -372,7 +372,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; @@ -382,7 +382,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; @@ -411,7 +411,7 @@ static long udmabuf_create(struct miscdevice *device, } } - kfree(folios); + kvfree(folios); fput(memfd); memfd = NULL; } @@ -427,8 +427,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 Mon Sep 9 09:18:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 13796491 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4506AECE57E for ; Mon, 9 Sep 2024 09:19:26 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id AB79B10E34D; Mon, 9 Sep 2024 09:19:25 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=vivo.com header.i=@vivo.com header.b="bN0sBakD"; dkim-atps=neutral Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2068.outbound.protection.outlook.com [40.107.117.68]) by gabe.freedesktop.org (Postfix) with ESMTPS id EE9A510E34B for ; Mon, 9 Sep 2024 09:19:20 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=T4WKhs7tbh27ZdL6TA8+CcO3DSRPJ/jGFBI2EpIUlkfI1AGSJMxsiEuq1NgXOxCLVtiw1VrgZgaa4oPZyUpkpmwAFNuTUB8/ofAOtReFoCjXIsrela2SW/7qwCbw0vk3CQkjg1HK4iTN+R48kkv905xY4xNfQ15K+3xrKtlnP86m0dglkHs6RFrQOFCcLE1nGff9ihZYmM3IemcmUHDusoyBhnfxt/erfwnYwY0GMW1j7z9MUfhCjQX7T4nuknKwwuvylZFxe4b8Y0aRWzrVH6dcrLIL3nVGWDV7H7rw3PcTUh3TpUodBKU8kvV1DYdJeOZMbsYs6ctmmphugeGzAg== 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=jP3zJl5GHA6Cli3QdnQ25Nfb1coEB05mLki2geoKI9I=; b=dH6ahMdZZUiDXPP4TAkjhWctAy4e5JiQ668MJ5PdJuoSLLMdq7cE/O5wUD8kLmD6ozXh1j/Sic2+1tneDv0uaJtBUvF/aK24ZDOq2xsu5Jf7RdBGVMHKpK15zqw3rVdoYK3lgPvoaKESSgcdPkF/svI/0wIR/NlKAPvC+7ogMTlOrib4hazphxcFWT1z8zjUpo7PgULpHM8/L5vJEoQCTNkrUwaYPcOv+ng1IarquWFDExLUGIULGPvzsuOngiD7B+4xiyU2zhVBnEcI6V72L2DgvCW4Pv0WVrLbG66lve6KXvjTq9tWNCJZQ32Gja2nQjrEihS89rhumEAlGFZw4g== 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=jP3zJl5GHA6Cli3QdnQ25Nfb1coEB05mLki2geoKI9I=; b=bN0sBakD72MFvLD7r6soltjAy5Ftzx29x/XjdMbex0lMGCvRl58GW1OEB7vM4eSQgfcV+v7aeTMvxQhPD3RjOTjMVl4Slde1+s4lQoAONP5JyrnhbIY/PnI3LKpLD2JS0WS+aO/i6p8XXge1VKVx0OnlZLidFceuGSA9ETzgi64a21UwqPnLP7+D4dZwI0wb5pTYUCwx5mk1KlJuZkeMDL1b/MLOarWN7yh9HSIinYd/6kamDiIKRH4hL9w2nY0BmjDV7TRIL1rUiUoNsGgkWXddvyshMN7KC9OXaDlFu2JrHXVmCTCz06n3unIW3eBUh7zCpIQ378O2cZaiokZedA== 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:15 +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:15 +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 3/7] udmabuf: fix vmap_udmabuf error page set Date: Mon, 9 Sep 2024 17:18:41 +0800 Message-ID: <20240909091851.1165742-4-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) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PUZPR06MB5676:EE_|TYSPR06MB6292:EE_ X-MS-Office365-Filtering-Correlation-Id: f636a370-eadf-4e9f-d312-08dcd0b07881 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: ncbFvZsOtIhqiaE4UJ2/MIJI2LFlEqeg6PRnjohZzySq+9K5eImOEBfdt/vr1y7WOqXbWlKN/vCeYf6ZBha7rIlJAXThtBYFMYHPm/s9MkI+Su330K8zm4xh9gbHd6hgephZmKAmD+hIJTazZO4ujWxs93Jz1XXniVZqgO6djThY2xY3E8FP1lex+32MOwMH+RWhUTXgrgf5IJ/KHhSYq54m6EEXEi7F0b3L/bcOP8AU/OcrbIJSqpIKWTL57NSWYpXoDOgcUNXWtClbCGRMsfcfzIkKysbd6hoXK9zUcrkECKDbz1+1aIbcZB7w9pQor67slm5CEh4vYBUATruJ2hHXRYKF2LhzhpKcizTx+yC/mbQMtMJ0eWBiex6eJSQNeGiUw9qKrv40JiUbzMIhavevEt2oQKj95T0xIWjN9kF6brh3n+WUq/u9SF281b+50xBwYsoTyyY9VlJVITCSsJu/BjQ738to/iD0sOzliz77erJJuKJyQ1zUE4yzm5d/CyePPTt/VcNolD1CLPjTmPM1pkELqshczHjiwX3SYDcGt7wEfffTYleticfWFaF6DuGpz2mFNtQuOgEFJMVCTSSRb1LNfGYeOmPmzKKyALm+QJDQ67+tfJhZkQoE+RMQEgd9/1+QsyBN49BegGM36JxT4x7UbRrR1jTqSffMKMLjnesfLtg6YE/YVFnweBjy2OIZsB6smrdJlQx2TFAoQG9oF/UL5ZGRuMdhl/otUnETBHGOLuijDXcLNAMxZO6G7f1WkHctstjiIdHXMIoHV2qGb/goh7qpvy+c79uDkb4YK3GfmP1V6m22kkyS+Lgp3ABc+wVTBRgHN8Va1L17EFbpbUpnR+k1+pwxqiHA5L4V52PwOjIwUcmG42Mu6BKGFF5P1Behz26b0BHoJdTqKtyQ9U3xXyeIKBcJajB2swA1p3tpkWEZEi+H5dX/cRm+Ps1K5QMXtbl/YmE4WUOQelEJ18pD20Il8LQND0sTMJV8jhgbrVjLiGNHIJpvYeJNH8AdIq932hqDotyqz/pXjRjkRlW05ERFPBMCYDUFwq48mQN7WOyJkjRHXQhRE/f8LsrR7hmgGDVOE5CsaNiPZMymjwkSECDM0xjkZYefLVBXsr7FJs2X5awd2QqeCYTlSB5oeDbngc1OS8wtndaeWvdu2+NVmlkGwKKVzDtR9t6sywPpeEJoVI2snCB+E+fJoYcdWmTVM4UfCnOhrrPv8IVV2xCgJESxQmIKxQfpTZA4zST7a3QtEdfFUfAoVGSJrTaypK+wYXNayNc/8BWPCWXb+nKPV2IO+hwN79kekj9HHI+NkT5/loz08Q3f6rzXZcXgmGFBiSntAMB/nkD2ErRjwxui8AgSAZu84tyRxecOoc+De0bG5raPXQKZUfl7Pdz9unxKs0FjB+M6XGWKJA== 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)(1800799024)(366016)(376014)(52116014)(38350700014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: n9l56f+gIFrCu+fQHxbv2Sy+F7exUClviXpkSFQlqGQaQn0IpqcW9SkAJ8DgB5XfekTUMH3thJ31/i4Y3NotwUCkgLbqtA/gdCBCICdcZZRuAGaeNusxC39RTDVk4KHiBXv9F9fC5JGi1aMBO32HxxkIjY/pSEI4v7rkoq8i1BG5fcx+3AYvJXkuGu7mXJfydDBFEU0TulSjj3xQbok0xD9E5B8u39eIxWIaohxxE6TchpxkSDGKN5rZ4/e9lS/IWOtQkd7wkoLRRVyGXoBZHsp14S0b/YqEC6ev1mnXDf2gV0YcaHDwJmE6jbSoARG6GeStwW9ct0XC2mzin/t3EwaQ8tetCC6kqD5pTvVfsJNdVvxhyvWlQDfII34AzcpPwEgdRzmeaJppiz4QYASbcoVi4GpwWFQWo6oyypktt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0uqD1KdJWKK0l7T4an6k4DP15WEb1xWL10wRmIjltUzXRQac18BDBNnmH+/yKZWH4NaXkFD59OUi1w7+XqZ3gg7x1bs9BZVai X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: f636a370-eadf-4e9f-d312-08dcd0b07881 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:13.5860 (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: Rwwh51eqsbp2WVyN8VC/IUoaB4t+uHSf7+wOiIpWT+wGVpPhrE6v8cowix6ITN2y68WaupWLzQ6fc7gOdLTJcA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYSPR06MB6292 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Currently vmap_udmabuf set page's array by each folio. But, ubuf->folios is only contain's the folio's head page. That mean we repeatedly mapped the folio head page to the vmalloc area. Due to udmabuf can use hugetlb, if HVO enabled, tail page may not exist, so, we can't use page array to map, instead, use pfn array. By this, we removed page usage in udmabuf totally. Suggested-by: Vivek Kasireddy Signed-off-by: Huan Yang Acked-by: Vivek Kasireddy --- drivers/dma-buf/Kconfig | 1 + drivers/dma-buf/udmabuf.c | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig index b46eb8a552d7..fee04fdb0822 100644 --- a/drivers/dma-buf/Kconfig +++ b/drivers/dma-buf/Kconfig @@ -36,6 +36,7 @@ config UDMABUF depends on DMA_SHARED_BUFFER depends on MEMFD_CREATE || COMPILE_TEST depends on MMU + select VMAP_PFN help A driver to let userspace turn memfd regions into dma-bufs. Qemu can use this to create host dmabufs for guest framebuffers. diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index ba9dbc7caf71..aa182a9dcdfa 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -103,21 +103,29 @@ static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma) static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map) { struct udmabuf *ubuf = buf->priv; - struct page **pages; + unsigned long *pfns; void *vaddr; pgoff_t pg; dma_resv_assert_held(buf->resv); - pages = kvmalloc_array(ubuf->pagecount, sizeof(*pages), GFP_KERNEL); - if (!pages) + /** + * HVO may free tail pages, so just use pfn to map each folio + * into vmalloc area. + */ + pfns = kvmalloc_array(ubuf->pagecount, sizeof(*pfns), GFP_KERNEL); + if (!pfns) return -ENOMEM; - for (pg = 0; pg < ubuf->pagecount; pg++) - pages[pg] = &ubuf->folios[pg]->page; + for (pg = 0; pg < ubuf->pagecount; pg++) { + unsigned long pfn = folio_pfn(ubuf->folios[pg]); - vaddr = vm_map_ram(pages, ubuf->pagecount, -1); - kvfree(pages); + pfn += ubuf->offsets[pg] >> PAGE_SHIFT; + pfns[pg] = pfn; + } + + vaddr = vmap_pfn(pfns, ubuf->pagecount, PAGE_KERNEL); + kvfree(pfns); if (!vaddr) return -EINVAL; From patchwork Mon Sep 9 09:18:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 13796493 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8BB98ECE579 for ; Mon, 9 Sep 2024 09:19:29 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 1077910E34F; Mon, 9 Sep 2024 09:19:29 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=vivo.com header.i=@vivo.com header.b="NJgmB0a0"; dkim-atps=neutral Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2068.outbound.protection.outlook.com [40.107.117.68]) by gabe.freedesktop.org (Postfix) with ESMTPS id 641BC10E34B for ; Mon, 9 Sep 2024 09:19:21 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=BETXIZQmNXMVyHb9d9Huf7+fxN1gz78jldN1r1afP+f373Bi9jm+RgY5seK/90pSmFyOsJ9SqOJMgzxMwEYCRCNlQ9mVBFcut6zQGZts1nEMEG5Ypesb5b+7f2FisSIyjV0rikUZHkB0U/3HTlAT5LXu2aqa2YViVp5r3k7NvIBb/wYBG8r9TOGGIZGVG586nR/ni/u4N1GSHHMnU3bsxGkBl/Ff2vjpLNoLHxdR5ZOdvUil/MRF3LBW7Ie0zlJctYm6hmiDti1TiTBemzIJd2t8y68hBdX5Q9eJeQaFqQm1JJyWosnJ2YhO8zyGWxKnRo54NZGI/p/AKmcHcxmlcA== 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=bQkwDcoxqOcmBd3NkTTXFHVpXFaDfQknUSNfxlJgkhM=; b=YCoCgvzsigTB21pvrZEXbtftBPyZjTQB07deTPOxO5T4fr4QvQeq3TFGi0v/vgEKp+ncH/sMAw0BKrOM3uoAg9BD33DCLRNXgnIJz9FYFuxKL/CBJyhGWeawxIR1AbxPcZD0lK+SAT2RzpBNika9MVWheKRTu7sgguJETbhRKxGG4DObHoGFGsRZ8r+2fYdGiDVYqlFEpkhKAsM69L7l4AS8tDRZucaGJbekf39b/TcaKhZW/z8tITm4LsXoGd2I4efbR84RePJNgRkr+dmHvYeL+dmXvI7vFzFyyuj+RIqRWnqRpE0R1AuIZ4H3xK92LCS0nd9k7QYfzad1EAKoWw== 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=bQkwDcoxqOcmBd3NkTTXFHVpXFaDfQknUSNfxlJgkhM=; b=NJgmB0a0yRs/5tEQmau+5z8jK5bxLtNsvF7sJcYj5Tc0XhGZjwhm4z45ak/zdbgzv7IADzzU1shvIMcHjkXJHhN8hNdWd47Dcs4AYb2xEypLJmSjX0yjNkXsv7ithNffvalZ5g1EWS5nD2rCAz2p51nHrFNC2SQoz41Dx/4vi8A5aYUKF7I6eHMWtk7Mrc3acpHiiaRV5yebjmOcSpfIVFbtq9vl1IBjEbf+VMhr4D8Wwujxe8jGpbx8xyYbdfDSjv992xGcY1aPmj1FLZGHZp2Wy59OApFkTVjLFXIAoKQHd7JcXLOXaQrZ3Fpri5xgTeJsI0OZD7Onk/9JTbO1Zg== 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:16 +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:16 +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 4/7] udmabuf: udmabuf_create pin folio codestyle cleanup Date: Mon, 9 Sep 2024 17:18:42 +0800 Message-ID: <20240909091851.1165742-5-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) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PUZPR06MB5676:EE_|TYSPR06MB6292:EE_ X-MS-Office365-Filtering-Correlation-Id: 9e239420-900e-4a5e-7569-08dcd0b079fd 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: nz3B+NMxcAaFo6CUfRuH4lGK5EcB5bnDlfFsXICdlCpy1v9XbTc899x9ORzhWRk6gJXldsqiolb4EYXcFDgWcocbJPRDvPAxJFJ6L1byrHCHsbbTCXZ515wtBOnDRrX4ku0CZqt/3ijCd5pWE0CosFDwtUe4ftv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BXMlNp2Q85NNa40B0suLBysveKutB5K8P8SdDzoUKHhFWS9SlMr+DnmxI7kg+O0st0osQTH9IbdOUtZWwQXUh52slA4PRzfBQDYmWD9H/doMdPwO8WJB6lXaUKRBVc+e3vyHKc0PFfQ9HVnyPhehnnSjtlm18da6w9/R/aTpxU56/YJjDVCM+WsUqIfS0PKT0n/wH52w5zlB6gnUefprw== 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)(1800799024)(366016)(376014)(52116014)(38350700014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: dgp3SWatAMzytnxsBlnwd3tNHQ8yS7oEcb9YceTnXqLRKFZ8TuuQ8l2Z4+Xp/wklOX+b7QGpz4JwpZeoUNSgDysWDAyofMWZSIoITb688wVlFDSaafoywnUiIfFwlxYfpNz9NnjVdVrtLEKysCXpA7lBnTVrnEjnX55Sf6nbGL9ssNKEsDdbqNFdeMzp3oBtT/w2ylzF3f+F4/wF0TWW5M14Jsjya5OjTIQMu1+MT80Kjrp1eJgi5PfWLCkytjcN5yqEqH6PtSdzABeG9ZpeI0v4Oou6YKglNJ9KNNPCRzpcSsFIr4zXZOGmdsrauT4fizCVR0TbifwSXlVimBVoMV2g2EaNRsLRTAZaaP9UQZtJCaQ5xY4rEUZi3Ewf+CanB/PRzBpVQJUkbzFkdi4qsBR+Dx6JmsbmQjANxFG9YozzW8oOldZNwLH8P8DznaLrBJ/LRt+RDle6M0o98BcJ5KUA7c5A+xJj67GEIos5bYDLEK46ZE/PaOCqu7/9PnsCE994FRXragO5vuO/qPw2ZUecnOQJ0nXzCEkIrDl11lO3Xb2MZX6bAcKXObnO26mlTCtJafd5C0YThCKod/NOd4fnMUkVZWo8j1oDIrAsgadFmBWUOKDcTnHxyz9SEo3a3BqC+5MXauDolo3M88amPSgYO2HMvN1R+a2q05jDQc4G8+v0Ce1wIlmCCcXdtEP09/M2A4V45AjvqEr6IRdsNY37Tglr7HI3IgJChNERhY0jlru4DgJZfRZHurjGZn+v3IbMll/SOcaYRM3rXFTBvGcqp6FKbNGOQszoVSIwFJb0S00/bh1/vLyKV31KL0s1eHKSVuw4WJV9XF7RBxavNhIoO77MN3r74LIMrHq4dZ3PhCdDN5WpAcT2/lmrpSPDj62DCr+X3iYUMdGK3BPYZ/kLSnfusQ8tWZcDOhB+PQU3/k18oKdkQW7DwnZIfIAkLCFzWOhF31SVNhT025W88VqA4Y6sQziADpM2lsy+2I3gtl1s0YTFn4MRb9ZapiuW2OiyFXVJlDX+kpnyNNB3U/+r1CQu+/fNsVK40+AGtSEn31ArrI+Y1Lq8iQsA7MbPE8WHlYn27jc+YvED5kdUVgLuyCJWSNASDE8o7Zc+VaY+rm8LxTyp+Km0iMinPN513xiXdutXBvGaC52zNFPyhOG0//rrRGccpkSR0LWvP2yHjITuvzgWX/l7gyp5g1Z8aQXX/hkrivHw+IjJKIZdG5fLYRtmLdcwqYtfiz/NqDoeJrZP6YauEZU4Kl8CcqBdP13uOAcjvyxlT60DAa+DzE2UC2L4LGW/RBk191hXv9jvwXAZlzBrva87pJEJg+wG+JAUAaoEpdi49D1kOK5Ul3boyhb6939Q2M6w4Q2YM59XNLlt1dab4SAlUla6IMcqekL/lU5MGFrNENhgpfd1Tk6MkT3yPuQ+jePkj4zGLTMN0cyjwQZ0LXgrNqgAlIXLzjb9vdPfmlT6B4DiIPYhPgdiiSw4KyWiUbqRnDeRe+fPfVvU9OwI69frpx/39mV0zk4u5r4yG6N8qRNDUJewjv7yhXvC3e/GWIIa0gLPVZ9NqJW98q7oyZS2wiYC4uAy X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9e239420-900e-4a5e-7569-08dcd0b079fd 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:16.1106 (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: /7DNR1MfQswOBSfO5c8tAvMdFOvG2RxttpS2J3Foki56fv5ITqgngmBJEdkQT7fj2yJfcfa/nSO7gnZNxYlehQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYSPR06MB6292 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" This patch aims to simplify the pinning of folio during the udmabuf creation. No functional changes. This patch moves the memfd pin folio to udmabuf_pin_folios and modifies the original loop condition, using the pinned folio as the external loop condition, and sets the offset and folio during the traversal process. By this, more readable. Suggested-by: Vivek Kasireddy Signed-off-by: Huan Yang Acked-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 134 +++++++++++++++++++++----------------- 1 file changed, 76 insertions(+), 58 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index aa182a9dcdfa..fe1466f7d55a 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -328,17 +328,68 @@ static int export_udmabuf(struct udmabuf *ubuf, return dma_buf_fd(buf, flags); } +static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd, + loff_t start, loff_t size) +{ + pgoff_t pgoff, pgcnt, upgcnt = ubuf->pagecount; + struct folio **folios = NULL; + u32 cur_folio, cur_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; + } + + 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 end; + + for (; subpgoff < fsize; subpgoff += PAGE_SIZE) { + ubuf->folios[upgcnt] = folios[cur_folio]; + ubuf->offsets[upgcnt] = subpgoff; + ++upgcnt; + + if (++cur_pgcnt >= 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 = 0; + } +end: + 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; struct udmabuf *ubuf; - u32 i, j, k, flags; - loff_t end; + long ret = -EINVAL; + u32 i, flags; ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL); if (!ubuf) @@ -347,81 +398,50 @@ 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); - ret = check_memfd_seals(memfd); - if (ret < 0) - goto err; + struct file *memfd = fget(list[i].memfd); - pgcnt = list[i].size >> PAGE_SHIFT; - folios = kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL); - if (!folios) { - ret = -ENOMEM; + if (!memfd) { + ret = -EBADFD; 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; + ret = check_memfd_seals(memfd); + if (ret < 0) { + fput(memfd); 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; @@ -432,8 +452,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 Mon Sep 9 09:18:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 13796489 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7553AECE579 for ; Mon, 9 Sep 2024 09:19:22 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 01B6410E34B; Mon, 9 Sep 2024 09:19:22 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=vivo.com header.i=@vivo.com header.b="D3OxzCue"; dkim-atps=neutral Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2068.outbound.protection.outlook.com [40.107.117.68]) by gabe.freedesktop.org (Postfix) with ESMTPS id 98FC710E34B for ; Mon, 9 Sep 2024 09:19:21 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=jB6H+otLvm+di6YfyUQslFNCL6fPolzH3AqFQx80CVc02oYQw+tBl0btBD3uhnmx1+S+oXWVIEcUGIgOOrSPmm6Sw/q2onUkWCeF+q5h/yubIl7CUr+k5deVe0F2SGclASIUVwYpJ+3bn/vX7zlZWuDK5sa+E5WA+DpBA0Ax6ygrvjwm3zAMl/DY7S42OR5m+mzBNO9YP7I77aSCCTQH+AwXSwOKQ11sstfXr4/wZhO80DeEgSBQ7xY9n/pil9QMIrvcEGn6F47xjMQcBZzlEa2ezzNlcNmWVOK8SCfZrUPulHCb8xgvjFeRBbSpUaX/BHGmz2gckmWzEl09yufyGg== 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=htKih652snK6YMvQ1BKfnFkyzLJzkrbeAa0pGt3freI=; b=bvEuE8q0ndjsoZqLZGRwXHaqhcS6XLhrUCxg4zMZ5PH+SjQzWoIUXlGC3+gS5U67rQdCWC9engjHt9ekcOtGgZNKp2gzrXYBpfSGkpFWJ6v59v0HP9W3eY5VbgEkQ14+dZpG1TM4/IVWHOMEoMsjq165keSk39G0Vsn2CTLMo0bPFr6NUt2zv21cJaMpCf1ehT83L47J4FsubAt6T2t6SWnYPCSpGiocxoavUcGXDowFmezk2nuRqYcyYt/Z+LPXeAvykVtxfT2jYZiX2OFZ8etY1dOHkSMUB3TmuQbpUnBSBKbaJU/2v2xfOOqRy5EWw0nh90imRciNUL+usPsBDA== 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=htKih652snK6YMvQ1BKfnFkyzLJzkrbeAa0pGt3freI=; b=D3OxzCueFA3gRUmvkxaihXLLkAZxnkcATKpy3MgV+KIRRwbz803BC24OziIUmEDFJDIHa6+GPkgcHSXH60BCpqiXiGrcE2pqBGPMLunEGp2bZfGUOkU1EV1OrC2deGmGoxsuNknxNzXyMXF67FJv05Ks7zIxp0xruYdVfgUsiE+tY8uU220uvQf6+uEyxzXy0xR0Fow1wsZwlGUtWvEklouVij0OLzD3qI/qi360l7YzU10XRKnQPI9okHUXNjfIFfbBhg8BOoLh21m8sqJ7aVeAFt0ZoS8D8Wc61sKcEmdzYzG+I+sc0Js2q8G8BGUeRqCoZAwU517fP+mzUdma9w== 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:18 +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:18 +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 5/7] udmabuf: introduce udmabuf init and deinit helper Date: Mon, 9 Sep 2024 17:18:43 +0800 Message-ID: <20240909091851.1165742-6-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) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PUZPR06MB5676:EE_|TYSPR06MB6292:EE_ X-MS-Office365-Filtering-Correlation-Id: 148ba25e-c4bc-49ac-7d3d-08dcd0b07b7c 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: u4yt+ye+F16q88bOvViqzdwUu3MOhGw5uY/CVC3Lou9ffygECzAaiTD5eH1B4/GjeXErDac+2n9SfAvAaoqYQekXivnJn8HxkS5roQKLIbYI9Ha1u79+s1v9J/CVngtDPzcvHqHXT3qOo9Oo9ppOrVToLabwSRDmNJCWBx+nsdDePrY0gptTqIeYBa7KRcmpmRsJU4OZ08bgTOhUqs7QpOuzPBgGufTrEm4hreqfmq3wY5TRGzM+mHyW/843kvoTNbRqXnocA29yppj/mKRmogxzRvY5+fpJJfAzGDPiNDD+jp8yfqov7rBc0/2yyM+vsNbS9KuIiYLSvXHms6XbHIS4fZkB81wAsqj2j45Guf1N9JppEjJ3vDjnr7mMR/hFCeH2QTAycysqNVp0/9hoK+LYBBJlWebHUrc2xACamiLtBS8jws/JuV/zQaEDz9nR50nXHb/paTLydH6hFr3EJ3McNY1eGuoB/IcHxmgXs6x5eHWTnFm5XJ7rN4DXI6tDEXj7WXf4kxABwFDdJPSiuvHm999ygU159G8O/S/3k0VC03xddce2BU26+r+AZ7ki7zF96xohZcQqfOVVUBIvDj/nD0gQsh+zNRJNuWafRU6tWNzNSj9NzAalor2jvKaLlGnmNgdh2kvCfQcv1JEE0IMsR099S0YcY3QVI9ilTbh3763YGngN3fgh1BQAtmcemgMZKHN6+PytJMPeIVDf6v1K/m6AKIUWcHt/PHOT3yL7rHKI7XSYjvDepRGF01B6TzM866HG/dMNnKXqRMHmPqxyfd58P1+Yd1geoZ9u0O+5XUnihGq09v3crIRUnVbafVajjdDtILbhHw+9uGeLXKYCrkw78vis4iIbh8cCaRjcFQMplgZN8soQbZkB/jYrETARrMzUJPjiReLoePGxGwtDvrj8Oa//xSZTS9SSY0yFwTDELZMA1wsDVYmFaR0oxglyy5q3dFAyxg+9QlcmPCAcDSUu/Ws8Wh1+p3jk/qftD+w0kKM5urZMOFzDU65w4awJQsM6AKNoBERQ3cSOiHPUtzS85NCHHhgEs06pvX9m76nPVCRSIhGoWysePGnqKKxaUdWl1Cm2JEdNKkfgCa/nXom59W89OdwpNyUkompHf422XB7qpPRz2+FvzjeiDHXc1Ud+Dk4oMW++d6/e7xD7x27uYdAhlB8+639JoxF/SJWT6XAQN8uOrfFpnk9QNAnrAKSZjTYtX+u4+mzCDd4dvcMXuPOEEKxdktxyFvPE6plxoFZStnlCkq09l72tIO3rhfPihgsTSPrlDd+T+3lNaP7N+qI6GKvRr+FuNY/HxLMBVhnaSah/YGsltSHUwbb63wZ4GMJDd5s+wW96o861/+QYVyZ5wjb6lo+ep6Q2WXA1gDLIXV0naODNUV4ACthNxfwPXEXkEIKmJqSI+A== 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)(1800799024)(366016)(376014)(52116014)(38350700014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2xf40Xm8KVaU+FcL8KNc41r3/iaAHJD8y3niMr+nbwAH1pU+mao9Aq7wSwERIGiwkyRF94gj22j7FGatg5hEIFqYOKI0RQV8Sk9z0P7GGhN4jrTVsJorzJG9QVoYL42Eg6YjnqHYYd7NK9vKuIDAX6SoqqmlwCRd/ZiBSFZXPh8FCELwbvyaj5kJ50/crTbSDkv8ZVjOI8dbBefCKe6jMg1tSi8XEhAsjbCeiNgh6pQyvSURcwbSm87TQeCJx5KCuLrG4CHXownMiqUUQQ0MXP3J6QFq5DVYtW7Zr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GdBlAXabSiGPWOSFAqT7c5WFD+lMYtXcHgyI4iwc91s59P9vAKIK5keiMBiekkNi1bTDWvO3FZXWYZcucz83F7nNVYNvHlePf3FVCtSOnXm4RDiW+zFvtzHV3/iu/677xC2t9ZZFWCJqpUHu3EHQpvvmdCfeDjbBVgTZgUl+Ukr44Wqn6zp3UZz0ZRLf9lO9XbNlf91061YDbFNUSahj3FTIqUioXh3gfdq/7CI X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 148ba25e-c4bc-49ac-7d3d-08dcd0b07b7c 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:18.6017 (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: gwurMZeiQI3gV8FRJP0mHWjmniuKuwyiFfKeUt4hwbkgF3gFgGLxTHBw0RQgk08Ais9Q8DhC9HbVGT6WZ/krHg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYSPR06MB6292 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" After udmabuf is allocated, its resources need to be initialized, including various array structures. The current array structure has already been greatly expanded. Also, before udmabuf needs to be kfree, the occupied resources need to be released. This part is repetitive and maybe overlooked. This patch give a helper function when init and deinit, by this, reduce duplicate code. Signed-off-by: Huan Yang Acked-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 52 +++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index fe1466f7d55a..c2eda960aaaa 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -224,6 +224,28 @@ static int add_to_unpin_list(struct list_head *unpin_list, return 0; } +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; + + ubuf->offsets = kvcalloc(pgcnt, sizeof(*ubuf->offsets), GFP_KERNEL); + if (!ubuf->offsets) + return -ENOMEM; + + return 0; +} + +static __always_inline void deinit_udmabuf(struct udmabuf *ubuf) +{ + unpin_all_folios(&ubuf->unpin_list); + kvfree(ubuf->offsets); + kvfree(ubuf->folios); +} + static void release_udmabuf(struct dma_buf *buf) { struct udmabuf *ubuf = buf->priv; @@ -232,9 +254,7 @@ static void release_udmabuf(struct dma_buf *buf) if (ubuf->sg) put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL); - unpin_all_folios(&ubuf->unpin_list); - kvfree(ubuf->offsets); - kvfree(ubuf->folios); + deinit_udmabuf(ubuf); kfree(ubuf); } @@ -395,33 +415,24 @@ static long udmabuf_create(struct miscdevice *device, if (!ubuf) return -ENOMEM; - INIT_LIST_HEAD(&ubuf->unpin_list); pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT; for (i = 0; i < head->count; i++) { if (!PAGE_ALIGNED(list[i].offset)) - goto err; + goto err_noinit; if (!PAGE_ALIGNED(list[i].size)) - goto err; + goto err_noinit; pgcnt += list[i].size >> PAGE_SHIFT; if (pgcnt > pglimit) - goto err; + goto err_noinit; } if (!pgcnt) - goto err; + goto err_noinit; - ubuf->folios = kvmalloc_array(pgcnt, sizeof(*ubuf->folios), GFP_KERNEL); - if (!ubuf->folios) { - ret = -ENOMEM; + ret = init_udmabuf(ubuf, pgcnt); + if (ret) goto err; - } - - ubuf->offsets = kvcalloc(pgcnt, sizeof(*ubuf->offsets), GFP_KERNEL); - if (!ubuf->offsets) { - ret = -ENOMEM; - goto err; - } for (i = 0; i < head->count; i++) { struct file *memfd = fget(list[i].memfd); @@ -452,9 +463,8 @@ static long udmabuf_create(struct miscdevice *device, return ret; err: - unpin_all_folios(&ubuf->unpin_list); - kvfree(ubuf->offsets); - kvfree(ubuf->folios); + deinit_udmabuf(ubuf); +err_noinit: kfree(ubuf); return ret; } From patchwork Mon Sep 9 09:18:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 13796492 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 70A36ECE57B for ; Mon, 9 Sep 2024 09:19:27 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id AC35510E349; Mon, 9 Sep 2024 09:19:26 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=vivo.com header.i=@vivo.com header.b="NiWByeHW"; dkim-atps=neutral Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2047.outbound.protection.outlook.com [40.107.117.47]) by gabe.freedesktop.org (Postfix) with ESMTPS id 2F37810E34D for ; Mon, 9 Sep 2024 09:19:23 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=lHPW++6oKlcI7COgXYAYa//6Zvr3YpM3dzsjiTmvWAm4Sth2dgolpkkmmr+7QcP/Fi5xFwUHfprArPPKxExRIGSqzec87QVlye7s1B4eG5Q5uHxO7xkE6veKwuoG7sLLjlUHABkC/aSqkEhgb7PZG8MsIf4wTbOtNL3mvOsBopsv1//iGqxOS3cU/8gOzdjyMao4KZl3cy5lpULbzT8fK4+QtTd7Zdja9qeXZTnbvtL3pAauLhNzaGESYXbBuLQirQvkxmkEliomS6LqosRUgIB7kmRJ/uFLGG0xa/iw8rndaXzU/OqqwZrkfge7zE5w6aNXGOg/PS9xtVYI0iZFHA== 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=Lqv0NS7+29FLPoKDiiUKJxTlVu9rHwr9umC/2I4bD1k=; b=s4zQ9f091/NgRtiFdYSIFToqh3R3wdIklz+/3y+UI9PWg21HbBZZIEXoEu4NX/cNYdLcRN2Fqi+ludJAAdoHWuoTm5xm+jsboy6/EVWwE65g0musiEwQPeG74LoaLsaLc7F961sXOL4Jz9AispRbIVThPxD9Lo4QxWHoHEjvXf+TSXwy2UejXlq8G9UKOb4iBeOtoS7FVNSnzK9yj9qo/onfj1TzVGTU7iaQFBIL/nROFUtJM7aa1EKGEeW6ETr0EAz7wkd6qZA0p8lgW98ZKIwv5kjCmiCYfBCu+JmKmkVNTlcxPIPciZ+w3jBjha9KJlJN6dVMLiPQkbwsW425bg== 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=Lqv0NS7+29FLPoKDiiUKJxTlVu9rHwr9umC/2I4bD1k=; b=NiWByeHWdM//gNTqAeZKC4QenAZnl45XKyDVpFT+42HWvVJMtSkfRhlM5Eg853Alpn+XWAA4M85Dv6f96F3ZOcqBKEQGe4zkRIakYaCycAnd9HF+xOompkv9yZK4ZsDKRMVmqqXlb9uXHRjgbKWwjlYmDl2B9w/c/5LwvO3CtCnSBs1suMk+ShCyJiPPBCRnKZaX682VWudlpJJPmsmEvA4zTFFJiArvznDYgFC584C2slXoFVSWKFmGrPG7TB5r6ZPix9WqMHTT5/cBU3ewMordyf5uPY/umAQQ/db5c9ZMel8El5LX4EsTuMWCj1zrRoSSxfbIw4ECpD1yg5Hevg== 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:21 +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:21 +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 6/7] udmabuf: remove udmabuf_folio Date: Mon, 9 Sep 2024 17:18:44 +0800 Message-ID: <20240909091851.1165742-7-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) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PUZPR06MB5676:EE_|TYSPR06MB6292:EE_ X-MS-Office365-Filtering-Correlation-Id: 90ecb63a-84d9-4b05-5786-08dcd0b07cf8 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: DRS8lprmR7zShZS9JCA6ImQrzrQBQ0Mivmk1sKsagP8sUESDye9EILczyaoznrRO69LSpZkVT+NfKhHnsjCwltlT1gN4ZoYCCAg9813cR7xwetrhL4nz1p+BkIV6uyKoXbaTQNIpcjJcyfHaIaTsSOa9VA28gNVDdEKVe429M52Qvip33TsgEpBbF+Dq+ZqOJoH7JShIYM8ZCdOSfbQKN/fVswI2mgpjWbOU6Swmn6Q6Ek/uQmjQ6hs8ECDHjemVCCJQLU6wwEHkV1LbZ6qIk1PAiVIm3YmsLF9oxb8gl9tIsz+jDoo7c5RRtAYZ8q0KdDP8T9gMfVEymCluCKSpSC9p0uA65i6opJNnCLBeBBi0y2Bq3RK/eQn/Xw3aO6vRH5gbre4nzte0OU+MN0q//AmEAEhfFEFbWIfy5a752XkKjNB6QeU8TGKNgQmnJd3nyFCLC6MYE/IWUxORo+Mf5IQTNi6/67ddRLn00JAVzvWpAFa23JrA1+WzxRTuL0Ocez3fcaC7zH6ClA187gPMUmvQGfjM32+9/X80OdomMzAh+0SNQ104NyIUiwftUeEZl765mQ/un9d7dRi5MUsp73h90urJuNwmihPi+IShwUiVCY9SiLPuSiTmxUUuh94qRrw4er3zQTBlPgy7Mhmazl0+D3+lCc2wKEYT9QN5qag5EWUlFg86qeZPY04jPm5P7y9eLCTCNwngYhSEGVy66LB9eYhHjy9N3Vybfwz57SxdRK6JCzTMY8NeDz8ISrX26OGtRzWSscN6zUEECjVWWI6m4mhkTQz+ze488B4QZsQw6yTwc2ep0YpewoTZiYiR9PJRck0zUWB4KJKKPPLAuyYMY2gC6Vk1JpswmaCH2F3yxD0CWe861hIF++Ql+JzQFwEVv0uUjbF90vczEyTthGLKrNxTQsq09sANCzurmzu1lO9YsScYvAq6fWPU8GsLvjRQ4ve3qlDKM7XfrGeyeejlTepmmWAZp608KqYhvQuyyfVTsAfaxaqCSXtuKsER3RlUZuds6uc1Uv9H/+Ys9YXRadQB0Jxhpgc7VVyJdFmStmb+T9RKbc0sG+xK7cro8kwrxhDJQzNPSKdiKrkciOQSXuy+AFRZdCXSf/m0tbx5SuqYTWSoEQ26hMM27ZasCc7YWWBgL2v7LfVktJif4ZehLamtIWTjFIq5eXsUuTfs4CPTlh96LpUXyxZOBYUPqCzhp6XjyWJKheWjVDqQ/0cuVCEYL50pBaMhHU/Ep7AD77nMlCvfl83xXRD9VcI6auZSGo5djFWLVoFVFO2oXQxzNTbUVIjB17JFKvK9ItwUeqxBNUE57e14U4vTHra1LPH1si9TFdIucO2qFKUH8LCdUOLnewN2TtS0bME1Nbtav/UhNXVU05gx2K2uNTWjNpdgYQfDcUhLoJp+ck8fDg== 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)(1800799024)(366016)(376014)(52116014)(38350700014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: GPCiCwBU4dj/M/jdv0rwSxDmtF6q45rNxtJhbqmh06Kf/nvVGAJWuMPy9kcgXkxvrdteDozrd81qJzPT5vw59ZDQpSqhzfheZI0zcWLFQ2GK9vsPZlUcOvNH6wIszCMtjHCjD8B7Svuv03QZxJLjzQw6Ao22jd2EuZ0YS6vJca2ekXmmaRgbuCG8NXKcO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+Ie5gTZGIRhoC9ra6wk9k6Nv+5dgWHr4Zdzh/wbI9oWC3AMoTkqRzgvxqhHTlwbrC96ubVm18yI4F3kHzOkThD79PsuBp5Yu1zUJBgseemP3Dcj518snhfVa0jnOqisFZhivX/YKeFARbQVwdXv2twQdpcSmwE0yNhL5KyGnLovdE8XtiWlDj76vLw+92je7zczuoqmHJDnX5AYA2sNa0DDfCtHB14GGvlxxhzvkc81G7odkBIOrfaQhcJU8iCn56wNi3fruTdqY0wvtdrQJNVN+PBbOHXVVmpvy+rChK1zewgKeLkPcl2U4dBvy+eKSU59c3E/hyTb X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 90ecb63a-84d9-4b05-5786-08dcd0b07cf8 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:21.0890 (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: DvMxfGvHMJ8MxZuN6LG6Am9ELAuF23BZEeVJY0tZcgFvFOgUMcTl5QIH14mPtl/iIF4qAhkcquuLnjAMI3Jv8A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYSPR06MB6292 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 c2eda960aaaa..0e405a589ca2 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) @@ -196,38 +202,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; @@ -236,12 +222,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); } @@ -351,9 +343,11 @@ static int export_udmabuf(struct udmabuf *ubuf, static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd, loff_t start, loff_t size) { - pgoff_t pgoff, pgcnt, upgcnt = ubuf->pagecount; + 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; @@ -375,9 +369,7 @@ static long 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 end; + ubuf->pinned_folios[nr_pinned++] = folios[cur_folio]; for (; subpgoff < fsize; subpgoff += PAGE_SIZE) { ubuf->folios[upgcnt] = folios[cur_folio]; @@ -398,6 +390,7 @@ static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd, } end: ubuf->pagecount = upgcnt; + ubuf->nr_pinned = nr_pinned; kvfree(folios); return ret; } 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: 13796494 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A6896ECE57B for ; Mon, 9 Sep 2024 09:19:31 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 1CC3610E352; Mon, 9 Sep 2024 09:19:31 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=vivo.com header.i=@vivo.com header.b="qXsd1NvQ"; dkim-atps=neutral Received: from APC01-SG2-obe.outbound.protection.outlook.com (mail-sgaapc01on2059.outbound.protection.outlook.com [40.107.215.59]) by gabe.freedesktop.org (Postfix) with ESMTPS id 6312B10E350 for ; Mon, 9 Sep 2024 09:19:29 +0000 (UTC) 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) 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/ngQO3B6NwvutNXyB3G33Cq+/ulDyivq8Out/oY0Kh0WegHodD/2A4vUCNfjNJEntmMivQ6LjqNlpJONOkbhHUheUbyoG8FJ3YnjnB+4WFmgFAQJqjo8qe9mOEiUdFNBaV80/M+2ffWq8mMsaWdOIKO/+XQOzAv6cqZvHHAy0w0xdYeTSIhYfGn317tpaF/nwOad4xdlHhQKN/q1Mptf4W8EXCRrTAOgwSAuDJT4yjdW8Uk/zrDXQ+z/Xms17nglxUDadqsKo/yEFsm2MyKeR5kRRC431s5A03vUpGaQ+54dMz6sX6TaJlWJbw0drjZ4X4xk4tYaQsY8rrHuVujeZVwng4RWeEhYD3dU7BNq9Mf0guEO29FY7GB2BeoRrio1LXgXJScRk6RRj8RHBIgxcmejsKVBcRsigTRTBrDBcFtcIxk3eBHVc6v+ZQ2a3OCX3JRgxSg1vF+xwCaWbj63U0VfjIvNbXHB2DGOfyY8yv/L/CMPBkVAR7XpmOgF8zkGx+A1J1jijf940ExN7vCfC9oKEeAW8lXnruPa76UFkmSKLNCCQblzRLoIeVgo+c2QWNiBtUHAol+9oz5g7lUq8o3gvYMh22BQm19sDZcfrn5CGL+M3Xez9iBqNp+4BOObQY5hYgiVUK4oi2g98oJEOqQQbOS/sip/hAv+/3mRFFBGExvNG87PqCk3OVHac9R4uYB58XaSkID9+YiVw6nkhbTnOB5AElVR4V3dsVJifA3bVSYsIDZaKDQ6Ed9SdpY9ct05VD6Fyh/OGg4zTvhGA4zfGVW/gObPeVztw6GdR0MgH8ugq23B3ay6PftWRhKgDXXN8h/sPXV5WwpxFENC85WuuZaSU+MwVjm6OAIqsRcknTg/SkuxHB6A5iGFldWMEhuzpgVwzL9nESZyqAagEo99x1lFDS9qrr1KrnpsGV5BxC6Vb5wWrg/KOI5Qo8Em9tq78ylmw527fTfWPethYd7djA9zUjgXtXw1Z16Zn02x2dqk/1pw7z419RDxi+B/ucUvSwRWpjCmGc33IX3q1Kn/wHv/3w/nQ== 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)(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/auqoL0hZxrZoelQj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Uo/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 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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; }