From patchwork Wed Sep 18 02:52:24 2024
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
X-Patchwork-Submitter: Huan Yang
X-Patchwork-Id: 13806533
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 1F0DCCAC5B2
for ; Wed, 18 Sep 2024 02:52:58 +0000 (UTC)
Received: from gabe.freedesktop.org (localhost [127.0.0.1])
by gabe.freedesktop.org (Postfix) with ESMTP id 4B20A10E514;
Wed, 18 Sep 2024 02:52:57 +0000 (UTC)
Authentication-Results: gabe.freedesktop.org;
dkim=pass (2048-bit key;
unprotected) header.d=vivo.com header.i=@vivo.com header.b="JKydu68v";
dkim-atps=neutral
Received: from APC01-TYZ-obe.outbound.protection.outlook.com
(mail-tyzapc01on2066.outbound.protection.outlook.com [40.107.117.66])
by gabe.freedesktop.org (Postfix) with ESMTPS id 7AECE10E514
for ; Wed, 18 Sep 2024 02:52:56 +0000 (UTC)
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
b=lYRbzls+yOOUG2ViIrZUg70ZUE9GFfueluHI9uwmh2V66NqmOuwk62Q3vR7PuHylAjtaBRHD2kW2W4byEVQWoJitiwqskQQTXheNd87SanyN6cQurZE4XAmUFXOBFw5l+fu6G1yJ0yhp2fuHRXX8B4fIyMS/40RE0uTF94rqkKnUcLbp+ZqFOzYG7csZayE8+fdiZsjqMrLBArAkEYi9vRgPVeKL+c2Xbrjiay4jHyDJIK4bLI/ELmckMr4u94OSTQKKbFGaDXc8nj7VSRtKNvOFZC0EBdDdFFZEH1IS0L7vXvMtjuqxFLEE/NPDKNxzkWA9zsz3zWnMp4t+oYcs5g==
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=UQ8e0wYHxucCIsMRm9Goa9hgeMB+ZQXpe1q+cyK+35n8EeI+S5RTsELtbS4ELtF1SYkY7hTsMzvDSCUaDFqfUAYoOTvY06iI73H409L1w9ZXjQCaHeyRIHbfiFQ/+YAah0Em0IQVfGGMzc/Pv559xdl/rZvBR8c5agtaL40QeNcyDE2JhfJvMtm0vSlvkFGH5wnNoGLZ/8WLzebsZrHHydKg2Z0FwpeIqZRfWMUVXO8HbSIKHjFowi4+7aGIcyal0VzpY65zW4zUdX4zrTR2WV8t0t0+gtwP0kmnNjv0xipuGUq4YskRp5uu3cjq+F7fX189e0gwRo8RJeZRhhddoA==
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=JKydu68vwRA+Mkz93I3+LG17SlBUWoi690Pl+qTcqhqGwGZU9s9gT1yTYbrjDXzhFnjbEIFNAFBw0EYJh1y6q/9bo18RvsAVa+amofK8ztoe3c2qimiBrHJAVoWIzdLeYmfgodHr3tKCpFINaukff/Qjcvixu80JP6tIk5pC85U9j9gMb4wUuWTyW5nPRLuQ8p7mlSXpwDh9Ba7T9IRsoIJ4d0OR1YKU0MxTLsUXFDTpghTCmwikre1TUCNg47Ou+Q1v1P8WFAeGHG4IcBVKC0OpYJmU6SCeCVuk3C8fnPgo3kH79uKeu6FTvwAkwKKpLteQiSFbNVMfNSK+N9e20g==
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=vivo.com;
Received: from SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11)
by KL1PR06MB6236.apcprd06.prod.outlook.com (2603:1096:820:d7::11)
with Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.23; Wed, 18 Sep
2024 02:52:53 +0000
Received: from SEYPR06MB5672.apcprd06.prod.outlook.com
([fe80::2f0c:5cf2:abb0:d852]) by SEYPR06MB5672.apcprd06.prod.outlook.com
([fe80::2f0c:5cf2:abb0:d852%5]) with mapi id 15.20.7962.022; Wed, 18 Sep 2024
02:52:52 +0000
From: Huan Yang
To: vivek.kasireddy@intel.com, Sumit Semwal ,
=?utf-8?q?Christian_K=C3=B6nig?= ,
Gerd Hoffmann , Dave Airlie ,
Andrew Morton , linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v7 1/7] udmabuf: pre-fault when first page fault
Date: Wed, 18 Sep 2024 10:52:24 +0800
Message-ID: <20240918025238.2957823-2-link@vivo.com>
X-Mailer: git-send-email 2.45.2
In-Reply-To: <20240918025238.2957823-1-link@vivo.com>
References: <20240918025238.2957823-1-link@vivo.com>
X-ClientProxiedBy: SI2P153CA0014.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::6)
To SEYPR06MB5672.apcprd06.prod.outlook.com
(2603:1096:101:bc::11)
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: SEYPR06MB5672:EE_|KL1PR06MB6236:EE_
X-MS-Office365-Filtering-Correlation-Id: 7bbf6e02-177c-406a-0513-08dcd78cfd44
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam: BCL:0;
ARA:13230040|1800799024|52116014|366016|376014|7416014|38350700014|921020;
X-Microsoft-Antispam-Message-Info:
pq8E/VnWpdN/DcD+4hGexP2XoLgjF6CWYYWxe2LSWAkLvzLydxl53zlBW+KbxQBV8BybCCfE2ctSs/58kg+zQgH5vPeTyc41dW0X29YCRNi7dst0Bi08rDscJom1xwDLJEzQiko3N5hD3cGRr1d8asumPwBQFEQz8oiTxk7gwD0sh7EqM5p4DfOo3YT4Wuje5YOaPu7P+ycD+lFrnRPJU7uPDvzb44ho2oWntPczSTZIX3n8RsFeKV6XTwHbrBrWHEYLFmU3WG5Sd/dX2bJ7SQX+DyZEdEdaGy2wu2P73tbzoNYFERFo/svFyNGsdS0lvoq2sKZkRUe5Hp4NKORmk00okrD5Pbly2Eeif+t2qtjdUg/OLyHb+Yw59whh9biMQP1Er1oO9sdzMSwHcFH/q7nJOcJgO341f62cyqNl7vZ6A6rHo1WwlB2FBptQv19ucsv9ZBWKPglDozAXpPRr5rObOf0XQbOBl0MUAHa18ZSPUg+jXAMVEOBEIYbqlk78TNrLjUfNY/2+Y1xPRcjjR5rw3Fmf60OqwoZdFmCi/9a7Em9HjdJO31senDxPV55HRc4cpdM02K0rdVraV7lRkwKiddnWNhkzmPoTTOgyuT3Q/SfsTs0ImgoCRbxQkFDJ2BadWLBswUOH/7hvS2MVQA+q6WQDcV2LBpxvLucxlRs3ZdTAwQFEoubirRItBuAcojQ8aRUdl36/ju0Gnta1JwqbvnAI2FU/oyPnRzpb0F5shVRg4it7QP1GAlBlazskodmngW+c/MdMfHvNgU1AQqFCLEQltxxHA2fCCpA+Ttph5eGwxB7WYwrSCMg97+uw2wsgPCttQHdbi2R3LFBT63hTiEfvLrd+ZIPJMXjak3pmBSBrrOD8sHFA3q0HAkwc12Qq0HZPYLelWjlbfinoNONj/uBh534wznJv7lDSHR3LKiuah1yhC/ICwYnczm5HA4EeTnQt/buIDETQfqMyHyXKcGs9uwa9d6sVYj/Y2h3wamFdrQdY3FnlQ/JokdmzfGz8Rq9ETtfD9nKCktlpkJlHBgyb5xKencGNcVa3E2WqdP+KSF9RE2RVkgHYRfBirjzroH45gnf6esKRnu55e/WEjJDvs8+tyZvkhcgnNeEWqOjVl5lQURuC1fGDLGdSuEJjfjM1rnUveSGFgr8nKDuw6/TnzL9NjCiTmShB3rVfXwK+kSqaHvgxbuicl2DcUzA2YZVDhs3zQ3twd9H0boiFLuCbZAX+1E96o+CfftVR/kr4QXCwd9zpzpxTplQJhy5ahXHFumTc6vTJEgAPSvlOhN1+0MvjoSl8mfO8kPem4cGK7u/D8ESA1Ub+rMLIbCHGWZ/BFYMKBb7zeUW0rFJGnrzESqmsp37UqkRbsGJGeUQM/u2omJgYFdTP8BQZWPnx1Hb67JSKN0MAojQjrEpRj8x7NL+h0NmrW4mg84U=
X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:;
IPV:NLI; SFV:NSPM; H:SEYPR06MB5672.apcprd06.prod.outlook.com; PTR:; CAT:NONE;
SFS:(13230040)(1800799024)(52116014)(366016)(376014)(7416014)(38350700014)(921020);
DIR:OUT; SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
XmldueXiy6GACmwEMo2M/9KHeHrVzMA+6ngOxi3BazRGuLtOGLjlkUKwlQr/ItXhyrwWtIkWhEnSi7nsgihct9MaUTTinhN6I2MXKz+JvaMoNdorQQU2obMTiqaFeQp/taT+tGwMOe0sXAC22gjsNg8eutgs+l9vPMHHTx2ur7/OrqlBw763oyMfqRwGNiym/3lfGzuW7q60VbiWgU/aJbJgKbHDJt74ycVVjGq+rGUxrGXTOg7b0ZsiENCbIBTV2rPU2DlcPbOntIJ0FKlqR6KuzhIBQ+6GTkB6muo8Uv4h04Xi0oBSXVXXOkJpzOlj6daLdAtRht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wuMY+2laejbJ3YVuyzXYOKJoocfzmBDOUxyXidHkEezquTByzwGmfQH3O8vDtadXAvNB79O639Pc9opMQxirioylxV7/f8mYDJIh5Rl1tzImDtyD93GOratT0FqmN/TDVkys+eRn+9zYkLYtol7qzV408m8Ox7rO6NJojsnP64lgfCOuNEFowAUijGVi8enmJXJxkJOHCdEv
X-OriginatorOrg: vivo.com
X-MS-Exchange-CrossTenant-Network-Message-Id:
7bbf6e02-177c-406a-0513-08dcd78cfd44
X-MS-Exchange-CrossTenant-AuthSource: SEYPR06MB5672.apcprd06.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Sep 2024 02:52:52.8498 (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:
2We4XLip6P05NPhjUcmG4NMgv4O6v4NWDKqVIbkt1wosygGCwIEm9ylhJwOxAE/w3jJVhtnq1HfZMQ+gLSc8Tg==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: KL1PR06MB6236
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 Wed Sep 18 02:52:25 2024
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-Patchwork-Submitter: Huan Yang
X-Patchwork-Id: 13806535
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 DDA4ECAC5B6
for ; Wed, 18 Sep 2024 02:53:03 +0000 (UTC)
Received: from gabe.freedesktop.org (localhost [127.0.0.1])
by gabe.freedesktop.org (Postfix) with ESMTP id 5F00310E519;
Wed, 18 Sep 2024 02:53:03 +0000 (UTC)
Authentication-Results: gabe.freedesktop.org;
dkim=pass (2048-bit key;
unprotected) header.d=vivo.com header.i=@vivo.com header.b="qu3JB8+I";
dkim-atps=neutral
Received: from APC01-SG2-obe.outbound.protection.outlook.com
(mail-sgaapc01on2064.outbound.protection.outlook.com [40.107.215.64])
by gabe.freedesktop.org (Postfix) with ESMTPS id B426210E515
for ; Wed, 18 Sep 2024 02:52:59 +0000 (UTC)
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
b=DGEM/L0/RWhtqKuRZsLPCn8v+bQCSAcpr6dsNLTmGn38nQW7dekz2guprbG3f+qepL99lUsZO29L9nioPwPBpzyD5AbM/W4+3E4yCY8MXoxPZtjHm/j0/RUn+FyXp6+YByk5L10GQEdgF3q4zzyiVYzf9Xu1jlAqHuXG/p2B2RhxrQtT9734/fC+RjfdmWIo/SozC2izsNaeNsPdtIDVUGSe/vA914Gop2Ti/qppy9f+XCW0APHDg4w1AZXSva92MEWG42WI5QtUKKsDFQfv1uMtVpWtxDoz/boUiM8F28RHceZxGfCeqrrIig9C3sEURfwDxRBy9AvuQz6kirWdxQ==
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=wt4yr2YWaZ4LTh56Mk1W3wInpTHU9tH/JI4wnVIDZMJlLWwfGm30RQFcx7tikdo9JOmQaGFEHE1NhDvxKoWIjJMMt5yEFulYO3cWeW+Ws7ule8YDs3nc01/sDS/7k7A9VB5qbLSAVWdAYDuMoceqV90TGaE0I2HCluG8g83faPa8GomibRnlrueQIN61Un9YwRLZZvTbqAjDm0Bzg4gi+6jOK42+UhaZUGEkDq5JsPfcAnCnE3Erl+ZIMD5j5Kw735dzkRpudmA9xQCu0iOtFTXFBoNR+aVQpaB1tUr9tzvoswsDihwg7yEY9bfdr8at2LFnflZp2WCrxF4zlemJ6Q==
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=qu3JB8+IOB8nsKkO4hrL7UIh2beXxefLGOu7tGm7jhCpA9Vu4eVb+5B5zUrr4ZSby8CnSsDFpedgu0DdW5i2e+D8pTGuZYHb9FTtHfuSZS6OcOnuulYxfchDmsEU3mkoRdYO+HbgQ3kmIVkFVVJLEmA9U88/cJb7L0INaorK5CIIvzrq7LwQHnwstrdaHS5kr3IJAYy5ROej0tG6UtpVZgR9yoJDv4QoeZ7U/omRyooB23vT7/LcVfUJqnIllPBApvuSXs5sxRXEVvbJPVBC/5fuIW5zi9f/zXHu8RQD/XP8mgPoL1t+NYcgXr9uFyFmOLabdN6EHPiQSFGnO8gzeA==
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=vivo.com;
Received: from SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11)
by KL1PR06MB6236.apcprd06.prod.outlook.com (2603:1096:820:d7::11)
with Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.23; Wed, 18 Sep
2024 02:52:55 +0000
Received: from SEYPR06MB5672.apcprd06.prod.outlook.com
([fe80::2f0c:5cf2:abb0:d852]) by SEYPR06MB5672.apcprd06.prod.outlook.com
([fe80::2f0c:5cf2:abb0:d852%5]) with mapi id 15.20.7962.022; Wed, 18 Sep 2024
02:52:55 +0000
From: Huan Yang
To: vivek.kasireddy@intel.com, Sumit Semwal ,
=?utf-8?q?Christian_K=C3=B6nig?= ,
Gerd Hoffmann , Dave Airlie ,
Andrew Morton , linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v7 2/7] udmabuf: change folios array from kmalloc to kvmalloc
Date: Wed, 18 Sep 2024 10:52:25 +0800
Message-ID: <20240918025238.2957823-3-link@vivo.com>
X-Mailer: git-send-email 2.45.2
In-Reply-To: <20240918025238.2957823-1-link@vivo.com>
References: <20240918025238.2957823-1-link@vivo.com>
X-ClientProxiedBy: SI2P153CA0014.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::6)
To SEYPR06MB5672.apcprd06.prod.outlook.com
(2603:1096:101:bc::11)
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: SEYPR06MB5672:EE_|KL1PR06MB6236:EE_
X-MS-Office365-Filtering-Correlation-Id: 5e083001-4293-478e-aff8-08dcd78cff1b
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam: BCL:0;
ARA:13230040|1800799024|52116014|366016|376014|7416014|38350700014|921020;
X-Microsoft-Antispam-Message-Info: =?utf-8?q?9lKtJmuoPoy+zMPxerw0WIs8ka11icq?=
=?utf-8?q?EjhlvKo7XRLUeC2YLWk0zP1XgtFVPU0F+tnySm9/zazVlmoPAbCOYgyobigZC6pks?=
=?utf-8?q?LJEuIWUafG1Zqyj9S9RvvDxEgpsRJXXnYglf0LHbeT7Kn/iM/zgbS6QoWb/WBk6Sd?=
=?utf-8?q?04/oDSM4PEGA/Bzbc5W+tRdZCXAQNVxj3Fq2Lur/ruk9p/kN/cMmPP4bj8nDJuSOm?=
=?utf-8?q?WEXVb63sCryDVwZvmOoe+XASC+nslfE4vMiDdIIo8DaT9eoMsrDhoBxwuRKimQAzf?=
=?utf-8?q?kJqOhA+0N2Hphj5BNdF8nAEen03dfi9DFnp9GhgyiU/ZIkY51h4caN9lYv7MhsCFT?=
=?utf-8?q?tV4h5+eMUuf4W/jrqJX9eeLXavDcHZP+Q+Rrip55AWs9FejrvAuuvy2ewyypnHNWa?=
=?utf-8?q?Hlznmg4p8+8aMXLNodkBKF2kPhWFsV2SJDKApYq8G8K/KwPKdGHGLG2/32WyWZLPA?=
=?utf-8?q?cOkHOVsDgQxnrrFrce6C92NA3W3/zWuSA8VZOo0nG6TbZqmVx1NiiSslf8nKoYbf0?=
=?utf-8?q?h2td9vsggT5pi0uwJ8QzhAg0NmlQokuvJsn+BhA+SfBv3PAxLCfFfAF5RGGxFpe6t?=
=?utf-8?q?F5mXlFqbrDnwOvOEeoMOoud6eT4k4mZctN+0nvbs82mdU91A0ZDwwYwaCwJktRsja?=
=?utf-8?q?dgQeqomouUWE2bLnBIagO/KtRPMAFU8Jzf0PfhgAJgkCgj/TZcfJaJNDXo5foD5gh?=
=?utf-8?q?/PWr0paJjSeX2nniiM67JSmxKhDIUuENTqZS5FTJTT9FF/BV4CDZQLeVumUSxm3AA?=
=?utf-8?q?Fms5rbwMFIwVxPfFYLdJkhPSa1C4rlU02nvqkfxhsVGYkLZdLmsHWTWON24mVpHGm?=
=?utf-8?q?TWjWrQKvIQIaSB1e2Afp5+2o/IS1E+TGt1YtWvL0kYomWATfgvoOzHiiwESUvf3n3?=
=?utf-8?q?mLMJwpLTmlhXeTRqFwEcFEcpn3i2WPhRoAdqXLVL3bs6/q8YZez2qQdWC55smjDfO?=
=?utf-8?q?IabX6Js/9vTBh8iHzkvTupTESSjkm8ATNj/cQFQTXmB2YF74Nn65qQYyJF0d8hoDT?=
=?utf-8?q?/KyG0VHtNrUmNTa5P1tJ5UUvx5RjvVuCRKyxuXba+/Hqcdw6Ht+EQPqePg44kA4JO?=
=?utf-8?q?6I8jMIYqhUpiucS3dJC2ShSDmuY3Bql1Co2ZgTkicACdzVCsRWrL0kYyQ8QmEQpr0?=
=?utf-8?q?cILyR/Q6wgOZdtItQ5HYrDt9q4oRUfdP0PBhKcWLIZyky7qEiJTqr/oScIVzHYAXK?=
=?utf-8?q?RBw/1ChK1bFLtKwBPBXukj7qV0fCKx/YxwNV/TvSllChWn5x0chNkO2SttmiZhqkj?=
=?utf-8?q?jgIi0PNM+ye4hawEfVcDdtHdoaKI9obBy+z8D/kU+SlFsGYw8mpR4LlUbt3B1pNlK?=
=?utf-8?q?qMEIlSzuNa9/7FRflmLZo0CxNcl1oYC8ZF8CUpIbn1K5KfkGGQcUPQ0=3D?=
X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:;
IPV:NLI; SFV:NSPM; H:SEYPR06MB5672.apcprd06.prod.outlook.com; PTR:; CAT:NONE;
SFS:(13230040)(1800799024)(52116014)(366016)(376014)(7416014)(38350700014)(921020);
DIR:OUT; SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?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M/0jbeKgMkzk8hEiYRPpljDYPpm8W0vOiuaV5Kbntf?=
=?utf-8?q?B8opcOZ/Q/JEt/5l8mh6p2UMvTN2gAHb4fxKW4wgcG0gvGA/gz081/adHaQFKsOMr?=
=?utf-8?q?9De+Eop7BNu3keQFG3RsblwcWFaDOkBiMHduacsi8BBlzvWqaXFoUkSJN9T7pfPRx?=
=?utf-8?q?Fz+ggUWTtLvcmbkDsomU7a6SIFUithytimRXTcUEtzJ2cJ78i00pBcjDcp2O1OKVP?=
=?utf-8?q?EDDPQnFlZyJ+TsXqmwL5rqqJQM0jvErYcGanTJ4je/HX9n6KIfQ9DINEURmC0yQ5E?=
=?utf-8?q?A2RmwfSUAxdrWK41OyQBYdQ4x3vC3zkRtDYQ7TJkkhvvNbk+Dbt4EOIBtpILEP6Es?=
=?utf-8?q?V1t93eenTz6RtYujk2kc+DtUiyutWQBSA1ZTzOcC+et2exn9tsQg7dJOyclNPUwIH?=
=?utf-8?q?c2VBzxcpMCvcOFoQcLViBr6rbs8FLascqfz3lKcr3EY6RZcrKhcpl9mSmll7kNtOp?=
=?utf-8?q?xt3GcUCCcKbWM2BXKceB2bm4iHiTtAS+BqMKzz938pU9XjgYs9rZ9XpD70zhcxt+4?=
=?utf-8?q?iFDAMrL1hwBWPM0KY8uddDPex22T32X/pKqL0j/XNTSlxtaQ2SsGQ3J3R9N705tdE?=
=?utf-8?q?Za+4TWQxhS8X9mtkLaQzu3/mlg94jdPvbyxkwpUS2N3zicdWMijbgGOgr/K+qduMq?=
=?utf-8?q?rzSn/rQ0NQIQ?=
X-OriginatorOrg: vivo.com
X-MS-Exchange-CrossTenant-Network-Message-Id:
5e083001-4293-478e-aff8-08dcd78cff1b
X-MS-Exchange-CrossTenant-AuthSource: SEYPR06MB5672.apcprd06.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Sep 2024 02:52:55.6766 (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:
P2ifdxKyrXGDYjXMLPVSQ5QQeXMUOnuEayRGmkn6pZlTIVZG2UD4ol9wpfPkwSOatOz+gfD3tjHjToZPZcoCiw==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: KL1PR06MB6236
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 Wed Sep 18 02:52:26 2024
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
X-Patchwork-Submitter: Huan Yang
X-Patchwork-Id: 13806537
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 EDF89CAC5B5
for ; Wed, 18 Sep 2024 02:53:08 +0000 (UTC)
Received: from gabe.freedesktop.org (localhost [127.0.0.1])
by gabe.freedesktop.org (Postfix) with ESMTP id 78CBC10E51D;
Wed, 18 Sep 2024 02:53:08 +0000 (UTC)
Authentication-Results: gabe.freedesktop.org;
dkim=pass (2048-bit key;
unprotected) header.d=vivo.com header.i=@vivo.com header.b="Yv9zqMr7";
dkim-atps=neutral
Received: from APC01-PSA-obe.outbound.protection.outlook.com
(mail-psaapc01on2079.outbound.protection.outlook.com [40.107.255.79])
by gabe.freedesktop.org (Postfix) with ESMTPS id EFD1210E51A
for ; Wed, 18 Sep 2024 02:53:04 +0000 (UTC)
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
b=F7G38yLVQsQr7V5GO+n4phmKCcM+w1JTWwAT4yBqyEMnAFpqVj4clPHJ3ptBOopxBq8OINpDv5Ey7/peJrKGPHJaT8B1zUjHovdVyN+rIgoGmuBQe5Fh2akGpPWfZo/gK29++aR/6e3t+0/dz9AWzVJueuuQ93XXFprj7t13XOSNM3VsUHEAtcAT//yXs3Wo7JoP8VF/qyO20ZvgjU6OK/zr4HeZdBFWr/2Xp2ttgzm67f2mf1kWqOxHRq0MOzdWPnavVcfzhWw/L08oCEPoUuXgeFq8I6ytuFSYJdn6hJQuhgImnP834xGycFUAXKtn3aIcpq5yPR2/gVrdxm+3WA==
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=pR9wSPVOI7eXTkp0FrF0jRKskMdnCcm+aAsS3Z2Iew0=;
b=GZExe/Jv7CsvVxXZ63L9FMwxwP6BcUvn4WFjXFywzB0fm3eilcsc12wxvHVh9k+Qh0sDTi/BrcY5ypk7zMWMrrKge4elFAjIVu13hzTCIxc27QlMndoNm55IXbmgKeA/o1metLWPmBQoNlK4sVM8nu6495N+bMoyf/37fTmGDZz1j+vMFAbtCyKoojzvlSNvXYr+Qh3gOLdkR7CLJ/dg6xYP5NFnWsCYw3Es8iT3/lS+gfViHXn1hjjTi1+93chOdgXhr7o9xRyXoYpuOSl6hEYoRsFVINZPdnXiN+cE0/8zAE4GdVe3aTaxjAGdfuOJUYgTI1YocPVF/LHLQz1ZsA==
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=pR9wSPVOI7eXTkp0FrF0jRKskMdnCcm+aAsS3Z2Iew0=;
b=Yv9zqMr7E16+F7hFxE+U8LiQg0aoqXF7aCyMnp8qjxNbcE5PEiQEZb+RdFBtnGE6fdq9vQEhgCEAdci+rY45qgAb0zQKRIjemoDr/jzgGyA99R0j2JbXL4nVc5ZMONk7knMiMp4nzZL2+zXuQas5LIGotnOwupIXA8JuAnBU+oCR6ej/m/xEMecjlCz0E7+iLZV2vkZslvo8JXaDGYgpZRVW5bCoFwMtnZweBLLd6mFVMKJbcWOgMsf4IOCZogH+4pNo0Ib6kAzMI7tTCk8HDx6//3ltiPpbd8YiVBarP0HNPXfpQKoofpFx9tHP1qwjT7+f5/iGeuaoibr/9Hs94w==
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=vivo.com;
Received: from SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11)
by KL1PR06MB6236.apcprd06.prod.outlook.com (2603:1096:820:d7::11)
with Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.23; Wed, 18 Sep
2024 02:52:58 +0000
Received: from SEYPR06MB5672.apcprd06.prod.outlook.com
([fe80::2f0c:5cf2:abb0:d852]) by SEYPR06MB5672.apcprd06.prod.outlook.com
([fe80::2f0c:5cf2:abb0:d852%5]) with mapi id 15.20.7962.022; Wed, 18 Sep 2024
02:52:58 +0000
From: Huan Yang
To: vivek.kasireddy@intel.com, Sumit Semwal ,
=?utf-8?q?Christian_K=C3=B6nig?= ,
Gerd Hoffmann , Dave Airlie ,
Andrew Morton , linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v7 3/7] udmabuf: fix vmap_udmabuf error page set
Date: Wed, 18 Sep 2024 10:52:26 +0800
Message-ID: <20240918025238.2957823-4-link@vivo.com>
X-Mailer: git-send-email 2.45.2
In-Reply-To: <20240918025238.2957823-1-link@vivo.com>
References: <20240918025238.2957823-1-link@vivo.com>
X-ClientProxiedBy: SI2P153CA0014.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::6)
To SEYPR06MB5672.apcprd06.prod.outlook.com
(2603:1096:101:bc::11)
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: SEYPR06MB5672:EE_|KL1PR06MB6236:EE_
X-MS-Office365-Filtering-Correlation-Id: 5030fe5e-96bd-4e00-94bb-08dcd78d00ca
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam: BCL:0;
ARA:13230040|1800799024|52116014|366016|376014|7416014|38350700014|921020;
X-Microsoft-Antispam-Message-Info:
NhV9IdxDzVXyNzetCmjbDyqcoWjLC/yWk53OrUUgkXVLu/T9su3r3PS5bX798pmYnPaG40jsxiWc73GclwdaChEXLRA8hN6GeeSjTQn5KVpUccAqLsUYj+iPdcZ6D+xi5AIxuYmIRBI763l3cSJhxxinbReVX1YLoE+YuhbG9OPu3k3kk1uTCQET2WZ6ON8Y4DxC6qdgxg5iIB7L0K0A1v+wh8itBAcy1cyLG/7k3nGBmKpNdcEwL46cQghVC52EjCeYBqxzzylZs1fyZviYcFeJ4OdIjw9udXx2Q9FWvUZe5CJ0z5+Vdo7a4+E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BgLMPo1rHGYVoaUX3IgRDtUTFB7/K+jH6RA08paGKzRd7P4qggNY=
X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:;
IPV:NLI; SFV:NSPM; H:SEYPR06MB5672.apcprd06.prod.outlook.com; PTR:; CAT:NONE;
SFS:(13230040)(1800799024)(52116014)(366016)(376014)(7416014)(38350700014)(921020);
DIR:OUT; SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
SMZeFA+EuoR1qEUWUAoacDIL8Fy4ndBotw+csp1fwjyTAvyy2uIS5dpbqq6TtDX6CKOGSUrX/Jc25eJ9bHY69gHkdTRlOAVaW+8GrGHODUsp6Cv34vccHc79+DmFl0GhlzCDx804XWNNSqZZqH5IW8HnX31WQDl/mJHZEdjEFj9mzIOCz0cjmxYyoCQGYVsyKaclbn8yr4hUN6SOFYq9zYQXmlPoHXt+ezJhZ8E+hPIlvF3BfMIBPHtldP3zj7QEHR5zeQCmmxC/4soTe7uQJ6aX9IzOZkSoUxBf0rnZWKhpg7YHf0E6GvHyqlqHfhNFMfDOiTzuc1WCiN1FDNrBWd1uRHAYNh8c+9Y1SKzpot7y7sCfx/QLMnWUTthyAqKL1S9ByqBjxzBIb599nM5Rms+nS4ZbrK6plTgKr+XC88QFnCtoaTAtOPRhaip7GDHahyH8DfVkyhc/rbtjlEqdBkai7F77BJxaIdAiiOZBfTD5KOEkSHGVgEibxuhXti9uxqJ76GnrLe/fU3swBInnx44wP6XkciXpBoRzb08NtwLG84DjMxL8gs1DFWZlzeyplcmbaVGjKPXX7l4DeepfOOnwJrUqhxt0mNiB0FDHnml90EcjqzXh0/apwN7fLQSRZ0/JP44JsByAF9X/Wit6JpFmfKiAK045ysM/7NAyF/4S3R6KjsQTEYzkNU+iyTt2usql/NJQzlrXUzdxb6bMB+gNdcDmKx6MBt1PqYMQNNjmpSStF1O13rWrnrhm5DoA7NjjG+nyC8MMpI2aN9ufQDyi7h2puUvyqj7GuIKi/b7ICQuwr4FVsrCITxr1M46vxv35WcsXO6kjJTFJz6eV2yXBdFf447+S/QEeljb7EIe+pBhWGe3MLZQ/YyQ6jqta6x8KJfKmgTbvPHp5wItTRnXIkClTRbbqNPJ3JYUmxYwDGmHvUJmIE1PaxI7KPW9Dz7Tv3bHMpJouipNwN4manYyqU45Y83BVG447ixcPhvfS/cHsMWEhMZeICcSBP8382s1YJrDgp6Y3WQTwtAcLyoRzGfm0KNb3A/MxdD/Ea6591Nun+swannAsRCvVLJPUxqF14j3u8UYtiNjdcxUpKDNn6/eJKqc9vM/jubVXPUKllV9J+ImqLaI4pzJKaEDK9M4NEIIK2qgj0NgVvmPR2mS6SE2xBmKZhfFhs0iscuRlJTa9lehXtKGP0eVgaKHyUvkQsH8pOP2B2L00QLYcqppTmWPd57SCtgTJlcobzBfpXReEvRZ3WnVaAD9UW3xl7fbRVT04WwknAmFhsExT297ZWiwLnR1eSxnMChcyYNVp1XdGVcf4AllyfXxGDx/5HAiEmpIBMMlhM/5yQpwq59V4VF3f5zM5KipAwBSgTpS9t50MH6R+LMOhDUmMcDv8CkxXkJuIxx/LSojpBwVH6A3eM4wAyZUjnpu9EqQpDGyQMSZ1aLBsXgo0kmUBdyRX6fAI2nIZs02CwZYgNkr/EDF2CX3Rf7jUKEzkzCMjtlA7rV6CsUkEigy09ZXkKFs5PedXTi0qUpkswFptFrH8QphbVj3O2nJpAt+LGASYu5gVgkyqe5r21q3KzFzJGo8c
X-OriginatorOrg: vivo.com
X-MS-Exchange-CrossTenant-Network-Message-Id:
5030fe5e-96bd-4e00-94bb-08dcd78d00ca
X-MS-Exchange-CrossTenant-AuthSource: SEYPR06MB5672.apcprd06.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Sep 2024 02:52:58.5178 (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:
7T9db4i6KYS9Nb6SMjitG9gy/3H3w6f7wTYtWt3WGX2KrXrnHzXrs1lY1/Fo/vuKxDTnLo2pRVzEKjvE4fh5MQ==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: KL1PR06MB6236
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.
Fixes: 5e72b2b41a21 ("udmabuf: convert udmabuf driver to use folios")
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 Wed Sep 18 02:52:27 2024
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
X-Patchwork-Submitter: Huan Yang
X-Patchwork-Id: 13806536
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 DE1D4CAC5B2
for ; Wed, 18 Sep 2024 02:53:06 +0000 (UTC)
Received: from gabe.freedesktop.org (localhost [127.0.0.1])
by gabe.freedesktop.org (Postfix) with ESMTP id 69F0310E51A;
Wed, 18 Sep 2024 02:53:06 +0000 (UTC)
Authentication-Results: gabe.freedesktop.org;
dkim=pass (2048-bit key;
unprotected) header.d=vivo.com header.i=@vivo.com header.b="lBSAn+iM";
dkim-atps=neutral
Received: from APC01-PSA-obe.outbound.protection.outlook.com
(mail-psaapc01on2079.outbound.protection.outlook.com [40.107.255.79])
by gabe.freedesktop.org (Postfix) with ESMTPS id 5AB1310E51A
for ; Wed, 18 Sep 2024 02:53:05 +0000 (UTC)
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
b=alXeCrE0lsqZVSGIAAp+gCRhgITvaUZmTxoEejvR68UEou3A8Sbq6hLaiF0vzOpw0jQLimtv6eAoM5CSykomGIT3nHFcsdpvLrZ6YKtwV5Ow0Mp5nO/c0O5W6CNU9HS3CAK3HvOnUUNze9bRslH+NUA6Ylx2Xk51CtoVX3J+x6Ll/6P5bL+rCDHGv3q6DodyiI02INb1iHoSB3kNoROfhWi3gveFjA39WbHG0hHFsx/vixlJSUN3ldcdiAnKHTF3qwdjPvOUUd1gDFiN3bCfHZAwbda+1ge8IbfAf8n4cc4Jp8+NCzmfjR/1Bd7fsH0oyQ+WA3fNN3Trf73RK6MXyA==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
s=arcselector10001;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1;
bh=eiAQP2XOZ/W2FfmhMmIpXM39JfU6KKhxAyOVGhBDjU8=;
b=EUVqMsVtDMQ1729BVz6tgTsoOLTszpaJPZ5YiqHCMjhP1Zv5AboBqY8NzQldJ2dgVwc7Me/ANCP8Eeo7Y8q1u+bWQ8W7lXolB/4u2sI9ax1zTihfptJCTDeTIz5gi7PqwMv6A3qmbRoOXO9TCUqnl3ZDutYdOmscI1m8CMVgRDJKvXhuLW6SjCAg4yPLRHXIRAktVUuj8Y3lGGJUthWhvp767B2FroW7Dfiku7wA5Cx/UUxKAYRu+HcxSaRo4K7HRWQ2efvlC5xowom3Hz3pPAVjjRE7fm0B2/clV8Qzp8FszUmeQAkwM9qXkjqUQlHRbXv0f0KyoJYq5lztossUlg==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass
smtp.mailfrom=vivo.com; dmarc=pass action=none header.from=vivo.com;
dkim=pass header.d=vivo.com; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vivo.com; s=selector2;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
bh=eiAQP2XOZ/W2FfmhMmIpXM39JfU6KKhxAyOVGhBDjU8=;
b=lBSAn+iMTG3BtMDV8l7M79ReljBM84ez0CT7gD7hSkAetkbXaNlYt//IEUEeOS9ed5KrOXRwFHDmSLJsVwZKwM+TARtNJWJejNfiUIr8LBq4WVnVV53bvFZDRI/snL8Hunfr/iljwek3Ev8MuPkIzJ6DriBgM2IK1J5e4kdXVVxnik17mgZPJ8pNEO75yW2lTSms+E7GryE/nyM2qx1ESw2zTei8nDwju3OUE11isBZkFX3BCfiDXlzhq0nmjrrOs+89ACh7ZmBcpZR0OTVh3v4ibI4aZepUtCtDEkUORkr8Sf+hI3gRv2AxM9nCMolxRyGAIEhwrZcJj3rxE+IsdQ==
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=vivo.com;
Received: from SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11)
by KL1PR06MB6236.apcprd06.prod.outlook.com (2603:1096:820:d7::11)
with Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.23; Wed, 18 Sep
2024 02:53:01 +0000
Received: from SEYPR06MB5672.apcprd06.prod.outlook.com
([fe80::2f0c:5cf2:abb0:d852]) by SEYPR06MB5672.apcprd06.prod.outlook.com
([fe80::2f0c:5cf2:abb0:d852%5]) with mapi id 15.20.7962.022; Wed, 18 Sep 2024
02:53:01 +0000
From: Huan Yang
To: vivek.kasireddy@intel.com, Sumit Semwal ,
=?utf-8?q?Christian_K=C3=B6nig?= ,
Gerd Hoffmann , Dave Airlie ,
Andrew Morton , linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v7 4/7] udmabuf: udmabuf_create pin folio codestyle cleanup
Date: Wed, 18 Sep 2024 10:52:27 +0800
Message-ID: <20240918025238.2957823-5-link@vivo.com>
X-Mailer: git-send-email 2.45.2
In-Reply-To: <20240918025238.2957823-1-link@vivo.com>
References: <20240918025238.2957823-1-link@vivo.com>
X-ClientProxiedBy: SI2P153CA0014.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::6)
To SEYPR06MB5672.apcprd06.prod.outlook.com
(2603:1096:101:bc::11)
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: SEYPR06MB5672:EE_|KL1PR06MB6236:EE_
X-MS-Office365-Filtering-Correlation-Id: db7238e7-bb33-49c6-529e-08dcd78d0283
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam: BCL:0;
ARA:13230040|1800799024|52116014|366016|376014|7416014|38350700014|921020;
X-Microsoft-Antispam-Message-Info:
v1kC9CsQTxJ0mHIPVzG/Wsv4vudd5MvZ8Bt57INR/J1gZsQfHs47rgwJAsQ4UANat8vMS6O2L9l51CPa9BItHYs1Ua7BosB+/mfvQ7PkxzXr66UO6w4fCowK40zB1K8mR0ky2ZbcqnAew+jcm8qFpYORih95YVaMtbY5kK9lC5F5QeP9vLvE2IPi5yzDm7WBDYCcscPOw0tcpzgK0zxEEnMI5fun2yI4PePFZ6HztkKYFmInu3ET15KITrTJ4C/mo3Qq0RAhH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/cyPmcrM8U+LY4Bo9PNgKJNLqx9K2ftJsL6l5eZwPEfmu+ypt+rtYKW08usCcZ83HQz6oBpVIpxtlT7JUl/TinowYp4kvp5radc3qcOtoZJ1waU=
X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:;
IPV:NLI; SFV:NSPM; H:SEYPR06MB5672.apcprd06.prod.outlook.com; PTR:; CAT:NONE;
SFS:(13230040)(1800799024)(52116014)(366016)(376014)(7416014)(38350700014)(921020);
DIR:OUT; SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
BlQY9Xq1zxH31eAA4W9V+v75iJiLImO/l29XouU1noceRvs4In+XrXVrFF4pHBB5lPJGP4a4Vciu1bkG6/ogTQryKUNeeqxUbwysxs99SYtkcXj1ixhKWq0X4LTxU63JO5ts/0G0QdBslk85m8YfyJ3shYbAjf6Jr3YoysMk2R3dP8JFfranxFFYj8C61eY6EWDffHWv7CSqR2EspUpdt8JUh3shE3BXyrE52IeI994E7FPp/cwNlFSr0TcguqjIxrQkFBUVrumGTcb8U+XBytBdFcUR+Ij5FCM/leHwfWSj5JELOXkKQbSbTjh4hVLZjYRpjtoHfMOTHySQ+etfMqLL8WyvMTUJuNVb9BambSCriWMwJjyMaLArTP75m2GPJAVUUbbKTkFcGA/IDB7lET9+AuTKxiXt/peK8kO38RtKYT7xIhPL48O0vTuzxfj5MIKEuQ9gNtsCXAsl80YXS0WB4kSzCTXLVAu54xXkep8Fk5fRHO/hE8Iaq0C1H6Jhq76ooEisj50QVJb5mi+jH/1n+C/1lDIw5VUaZvNnjTpEmzbalL9J83Y71kdC+aGaM4pCmPl5iFJvLlaEHEdfiaIbl8qCfbB0S8aEJh6MXUoiDfzzPDTXYtIfVJBuaBfG6AiqhyiV1GuzVILNCQZMHJG+zTMIxBlg2MepDTBoVQVG5vXBH9Ra/NAibY0VUZ9LaHNVtzmOrz5AV+gLHJQfZQSrAG3zCnCDkBQ9LzSukudxk0+oAuiZ/yg2isH/aWJVzFnthReWypb+8WanmhctRRS2hQtqQjL20vQmfA4eDrhWeVgojGkMYFDQv5aKIkQ2As8TnFWo+65XRGVciO5xz62w5TYzvQqpDj47VmLBT16eDfsuPr+nhYawPK/9bh9IPZCkoUoWOvKRsDGcJzCZAAnd72/beS7O9lf4rjNZF+rjnXGQSaBh4xm0g7s779Uoxr0VPtUbeARVv3xvPZUUsgU++ReaMhF+ug6D5EUO7cEpXy5tzJ6pzGtNcbqAVyF23+ArlTVGc7CkGCbEu6mGqrRTmMJmDjBOC6e1pFI23wvxLNVxJLUxA339EeujXWKJFRzA4kH7qWiqz1gCf/D9s+Cbxtp5t7mcDjWueoSi3EH3ROEU67EJkYGgbK9VzWGgtdG5v1Rtq8LFBGtJDx8PO36Eucr1d2ue1Y9hzfYnXq7pdxWv1r5Gad9dUEehtGjtMPy7yOhTmRsacoJSLbJf7WaDPL3AGVaiOiwsaRtSA8g26b7eDB0u6xrOE9kWPsz7ryanrZdUddiUXSdPIKCI+06r3urZmLBaJ6qjyRM5cZfvWGg0y/FGK7WaYysdNz6X30ULz/T//Dn/h6V65MeBMFd0iXzZhD0QWj9aoySit7n2p3tIiQIFEMO6wwthS32TsBRQ4unwxT8p7GsTJxYId2vAnfTpVDb85+HI2z63nMGU4dh5wINfm0Q7LuEwsuk4rQE2L40MSy7ZaR7dMH9lroR4lCz/UFlVImP/nQ99hzyDgwnu7zwn87MktfoxIisZDZKtUVxnEA0EDhUU/3kTrW3OhhAjKoiOjCuCIc8wmNDjCsu96mELCwo5MBdt1asG
X-OriginatorOrg: vivo.com
X-MS-Exchange-CrossTenant-Network-Message-Id:
db7238e7-bb33-49c6-529e-08dcd78d0283
X-MS-Exchange-CrossTenant-AuthSource: SEYPR06MB5672.apcprd06.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Sep 2024 02:53:01.4094 (UTC)
X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted
X-MS-Exchange-CrossTenant-Id: 923e42dc-48d5-4cbe-b582-1a797a6412ed
X-MS-Exchange-CrossTenant-MailboxType: HOSTED
X-MS-Exchange-CrossTenant-UserPrincipalName:
5EbJbCiHdfJsz0W1YlLe2ByVH4aXufYQjMYQxKSp+IWZeQKf0WOqq7yDvVNXxsaE22j5QoubdRcHHLpeedY9tw==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: KL1PR06MB6236
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 aim to simplify the memfd folio pin during the udmabuf
create. No functional changes.
This patch create a udmabuf_pin_folios function, in this, do the memfd
pin folio and then record each pinned folio, offset.
This patch simplify the pinned folio record, iter by each pinned folio,
and then record each offset in it.
Compare to iter by pgcnt, more readable.
Suggested-by: Vivek Kasireddy
Signed-off-by: Huan Yang
Acked-by: Vivek Kasireddy
---
drivers/dma-buf/udmabuf.c | 137 +++++++++++++++++++++-----------------
1 file changed, 76 insertions(+), 61 deletions(-)
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index aa182a9dcdfa..9d58c045b8eb 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -291,9 +291,6 @@ static int check_memfd_seals(struct file *memfd)
{
int seals;
- if (!memfd)
- return -EBADFD;
-
if (!shmem_file(memfd) && !is_file_hugepages(memfd))
return -EBADFD;
@@ -328,17 +325,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 +395,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 +449,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 Wed Sep 18 02:52:28 2024
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
X-Patchwork-Submitter: Huan Yang
X-Patchwork-Id: 13806538
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 55F34CAC5B5
for ; Wed, 18 Sep 2024 02:53:12 +0000 (UTC)
Received: from gabe.freedesktop.org (localhost [127.0.0.1])
by gabe.freedesktop.org (Postfix) with ESMTP id CD36A10E51B;
Wed, 18 Sep 2024 02:53:11 +0000 (UTC)
Authentication-Results: gabe.freedesktop.org;
dkim=pass (2048-bit key;
unprotected) header.d=vivo.com header.i=@vivo.com header.b="gI3k0aEW";
dkim-atps=neutral
Received: from APC01-SG2-obe.outbound.protection.outlook.com
(mail-sgaapc01on2079.outbound.protection.outlook.com [40.107.215.79])
by gabe.freedesktop.org (Postfix) with ESMTPS id C1B6610E51B
for ; Wed, 18 Sep 2024 02:53:07 +0000 (UTC)
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
b=XgQ2rx2cUP0TG2IHW2DbCU6oUAj0knOYh14dY48u7EZ/qhWzW4xuhaOU6lPOHBQv+cKr07yJo7n4J/DECYUuxWOi4vEUmXQZSKvtzMDOayMEDQf5lAhCJcxOkq4V2FQDvEzrHEyiVhWDfJivoZFjxTL4DRmcCOdC/AGykG7KRtAch3Es3+xIuWksrod4XcHSrQYT1FR8hvYEZsRWIMpgBQofUbnr/eZGHK2bWHl4o7oBiJuenMYiNki6G9taUSzRdfYsVKDHMMkjBh5wfx24aIw85Cz9/Vf8cvGPfzkSu+f6260OvyHKoCQYJ7kvLMCrUVVjzAApSX5gTs+e9PmyZA==
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=qP85JJ7XhHxUZ0bL0II6CGxMXuUorY5CUTQpLmn9RwI=;
b=lMZ4IPyuuELt3vSYe3OdOjaLO/zQh/gUxnIfDj9LFrOcZH/ycVnzFKGslZ0UfLUnlxl3ELpi+UqCSYWz/DShlNhMLn4CDbJz9kCM92LD4HlGLKVsjnv5QmyY2yseuI+5T1KlEwkZ3si2jfovzKotQEsjmqZwngBtuRUE/oNqelowY/hBchWNxcRZOy4VghrOmxj1lfq6XFF1N/bEY81/HptlpJHYq15D/qWvxIpw80Ws51gP9o5E+52+UmsqsEkUgV2IVEgXoQRlwMxTFnUEMBMMaVtbDGeks2SuCnq5GcnQ0n7koJU5EdJNwS8wbO2HnVtjc5Go3MDM44ducjHh3g==
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=qP85JJ7XhHxUZ0bL0II6CGxMXuUorY5CUTQpLmn9RwI=;
b=gI3k0aEWi0rwSlwwgnoP5tggGMEITs03lOLVmL3YyOHXpxhmUCZP0w5r3NBPktIiFxCCIR39p+rdWUA0XY8NxwY2nXIFMPXlHb3Do6SMcbUlVOlGXihmWBWnr7McvGpv6vKYyJT3+9vgrC11gXl22I9CBpNC7v5qWA+38BnYlSuDwYdyVjaDuoYUMx/4vccXaP9ysSEwdSXtsXO5FTDBWtxoQ5tYdtcxKmCDEBOAYhfoBaavhwN3iqAD+Zbj39eT+ReuTRYNRuegXDXlzG/GIQzK24vRtvHm76MReEvsNSfXP/RgI8B5TZp2KJX804J8Gd8oJDlHulIIFfInP4AJXg==
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=vivo.com;
Received: from SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11)
by KL1PR06MB6236.apcprd06.prod.outlook.com (2603:1096:820:d7::11)
with Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.23; Wed, 18 Sep
2024 02:53:04 +0000
Received: from SEYPR06MB5672.apcprd06.prod.outlook.com
([fe80::2f0c:5cf2:abb0:d852]) by SEYPR06MB5672.apcprd06.prod.outlook.com
([fe80::2f0c:5cf2:abb0:d852%5]) with mapi id 15.20.7962.022; Wed, 18 Sep 2024
02:53:04 +0000
From: Huan Yang
To: vivek.kasireddy@intel.com, Sumit Semwal ,
=?utf-8?q?Christian_K=C3=B6nig?= ,
Gerd Hoffmann , Dave Airlie ,
Andrew Morton , linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v7 5/7] udmabuf: introduce udmabuf init and deinit helper
Date: Wed, 18 Sep 2024 10:52:28 +0800
Message-ID: <20240918025238.2957823-6-link@vivo.com>
X-Mailer: git-send-email 2.45.2
In-Reply-To: <20240918025238.2957823-1-link@vivo.com>
References: <20240918025238.2957823-1-link@vivo.com>
X-ClientProxiedBy: SI2P153CA0014.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::6)
To SEYPR06MB5672.apcprd06.prod.outlook.com
(2603:1096:101:bc::11)
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: SEYPR06MB5672:EE_|KL1PR06MB6236:EE_
X-MS-Office365-Filtering-Correlation-Id: 9069a733-c9a9-406b-11ef-08dcd78d0482
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam: BCL:0;
ARA:13230040|1800799024|52116014|366016|376014|7416014|38350700014|921020;
X-Microsoft-Antispam-Message-Info:
X+8aT7cWW7GxHcgK2k84niAdw/UJ9Sa0uRqRlWS1kaOStHaP81y90aGyfxrs2tMjQebSX0mhFTnZy55qegrlLIVebf6FKBLYHLKqhnI97kNb2IaMnOSBBVFQ6ufAbC+hcmSRYtrYMV1QcCodq42HE337hsn8a3QMUXQWcM2KU1dw0zAmw0fBLbH6zOqC2LqiU2229kWMuSmWKu5LlXszAEeN4h1b2iqEdm3SUSo+nAmSdA1r94G9qKhYDtkfs4U7ZjZO8uIiDvMsEIl8G8drysu5fsq9spKnGJdAgPQEJZPFaDgQoRC4wytAWB3n509R84v9Vp/tzTV0ZZVH8LUMxobqccB+PBtv3IZmUwMXj/RO044Sx9mjc6IBy9iYWqTBi5O5tMi+epKQ36CzU4Ht2wK574TwC6ZueKCEgjOFhP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1; SRV:;
IPV:NLI; SFV:NSPM; H:SEYPR06MB5672.apcprd06.prod.outlook.com; PTR:; CAT:NONE;
SFS:(13230040)(1800799024)(52116014)(366016)(376014)(7416014)(38350700014)(921020);
DIR:OUT; SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
5StMi5Zvrykufibcz8X7cp7yyhy7riryR+888ZigI5El0vW4GDG0itK0UvLhN6iOeT9ULN1JLzqhL04svOa1ZFHnkXZ1Ax44XKOYaalFx/hGKkZThkhCRiNgLfSUYdDSyqSrFQxgVsOBTBrRERYXcNm0vTsLaG6bOaq4+L6IjqoIFYBqbZIeP5UWk4NkfcZvK9XS6AhRkNRoVW3swwI+g/yM0UWBGhwWhtsEdcNxSEDU7nKbfO0zxTTSwMK9YdDcLssDS/zMpugarWYlT1Yb8w2rwWEaZnXhxMjSBWl/lt9GUpAey5dgw6hg2V3iptOeAjqzCBs84iB6ieKLgjc32Os9okGSlc1xl4KPKy2n4ohLKUHxwN/Lk5WsA4TTCS5tBjObf5sxJ/YeXLkLxGNqlhNpmQHhmMlAinFiTozd4+ncK+GjG9L5ckq3TvT/GhwpLZ0bc6zQIb7lJWT4lMQCtZCsZUOil9WDl7rIsCutqE6qUTv++OrmOjFQjpvyw6bHsaGlsruFYI/rJoKvJm293I1NyslYI+1XzV18LaEeqhcug98outuGMd0UHjRwJOo9p3gyF1HnqrFt2FiExYMTJz1UhfY8mwZh11pef2PAS38B3AvwobhGNBhCFO8mHR+AEml6dCEf/vVsu6oaYPVh96g2bUslvSeHPQoX5kQGbwNEVhvwPXYuEq6BonC29DWVomaeyF9b4AEtmznfD1cvIySyF/WuFV6katkFLrzb+rmJdlUvMniRe4hP1/Ku557AiPJ4UM9ef2DfAqsM51Gmh8OFD2Jhq3AsgTW4t4OfqQh5D/ZVwXGa2LXQzHX3DoKQNlV6qtcMAUGs96Zhd5eXep7+PfWG/Dm2iYNOJJRvz4sIOF8Zbz47jJdHeGKwavyn9BuqJhMK7cnGvStSqIdtdnlBdArcVzvyKwrv+qt6DV3ojY2gGSOtO6oU212jYUMM8SibAGXYpWlr2o8e4iNSEQuol0NmKFNTJHsiSvkRW+M+sHcyGp7bKqohk0Gk4jmrpanhB3YrwQclcAF3+W0rGpqVpbQO6CMaYFDMpz1/kEb/2lbZFcbUyWAIoHkhUvNfKxNLY2W3Ya9DVcMGTyoxSCdAgEk9pvtI98Us4loVz18EZ/5mpgKzN9h9p909mMb4/Ky+UtHa3Q6XL+K5iOIWrZ4gDB1DOl9aANrxhmuR0C4nLCchU6NUGLCdl7/lFca4NxHGedEMRdu5tQOpjXpeuzkshcWGayksu73n6Fnq3hue4tO2zRM4f7gj4o5jTcVW9bFAqOouh/fAKBMFwfMCzdizgGL1NLzY1JwLGTlVpduLezVyOcYem9k34RrmsD1Yyj65F30Qs5apZBklLN5pkvUhnY82wVkRy0yYXUZhLXNeylXvXAvrtRpN5/y1aZ1Gm19yK/oqINnbeE31yfeXkIWMURCDvBncimZ3a19kQ96xNTtPUaWJzmUk8aKJ1gOP8LmlCVkv7fXeDhy1Pm/4Ufrhw/gRX3Mp6X+I8JptuT9R4uSGpLtEAZRq3L3a8Pm3aCimIlHIytf0VcXm+cuvnHcyfQaxm2KLcSte/84rJxQU16vRE2MxzEn3qo8wVESU
X-OriginatorOrg: vivo.com
X-MS-Exchange-CrossTenant-Network-Message-Id:
9069a733-c9a9-406b-11ef-08dcd78d0482
X-MS-Exchange-CrossTenant-AuthSource: SEYPR06MB5672.apcprd06.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Sep 2024 02:53:04.7254 (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:
axWcyLdZBYdW7NnfGHm31Ke9qrx8cezqvoSAZVYyIDxAa5uWkUkh61dfSyTLODq86eufN3qS8rsFKAvMTvcuSQ==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: KL1PR06MB6236
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 9d58c045b8eb..40ebff2c77f4 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);
}
@@ -392,33 +412,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);
@@ -449,9 +460,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 Wed Sep 18 02:52:29 2024
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
X-Patchwork-Submitter: Huan Yang
X-Patchwork-Id: 13806539
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 602F0CAC5B2
for ; Wed, 18 Sep 2024 02:53:13 +0000 (UTC)
Received: from gabe.freedesktop.org (localhost [127.0.0.1])
by gabe.freedesktop.org (Postfix) with ESMTP id C777A10E51E;
Wed, 18 Sep 2024 02:53:12 +0000 (UTC)
Authentication-Results: gabe.freedesktop.org;
dkim=pass (2048-bit key;
unprotected) header.d=vivo.com header.i=@vivo.com header.b="Qd24zVKB";
dkim-atps=neutral
Received: from APC01-TYZ-obe.outbound.protection.outlook.com
(mail-tyzapc01on2081.outbound.protection.outlook.com [40.107.117.81])
by gabe.freedesktop.org (Postfix) with ESMTPS id B67E110E51B
for ; Wed, 18 Sep 2024 02:53:10 +0000 (UTC)
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
b=waNQLEole6aiZtTYWKuwj+nCsNRLynhmEd0yj8AAeWFlfsvADAYz9HFgfHemZ9xUiU65yW5sYdvzYDxvN2lMiIA/W/sJ+cDKzvHVHrykuQe+2h7i2rSBt4cnMOBZwGJd88dIGlgyert4dxpwaNQrRCvrXO/al6WO7BSEfBDEo+oVCaG24IpHx5fCpMOeQA1PuCyYWPWBWr9DzlU17hxLSd0/ypVaqzuxzCKZGOGwH2O6np/3MZvqkFR5JzM7CbGJKK25ycYsFMa5pP2IVYk3PXl28gSZDkU5jW5xv4IBleF4fYKLbjx5ZQL1GRbzUlmBYyGGX4RxFI1IhTKhPEq6kQ==
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=dpZaqFy3+zW1czl3YqV+mpwLQ0s0TQhAq/biiZ5XOno=;
b=hAF9fZK3ba08U9EHgdqz2zYa3TugaNXhLWniFJibrwXpF17gIREiXLZ/KVkRtkATLpJZbFfvrbL5cAJ13I/ZT3TMNezaBMClBXsTCOYfNPjJ1YQJwm+3Zvjwif9C6glsf4WF0MRIhep14nkKUGH3BYsO1M25J5ld43MJLycnXXKdBmFLwqT/kIDF6iDnadhNJyUaW94XgkHA5tsqnMUofeyV/9LvWKFQGEpauGYad8MWLU/vZOD8hAXDyG5tLaP51mA7Sbo3RsLp6d4yFFVnzTfZ4ecOeXZ2z8p1zSjXumsJ5QTKyVRyYaw5ZtJjfRUCs+N2eXdTJMV/il0l/GUXBw==
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=dpZaqFy3+zW1czl3YqV+mpwLQ0s0TQhAq/biiZ5XOno=;
b=Qd24zVKBpjBoG+WoM9qAvpKsvQ4BbYk6W+hdYJWsRKVN1JvFSBYbyM1Wu+/+4bhYxo355AKtjuk1ZvojXlmDc5Pgf+XxO1ZydMcSRd4yMSghrdr0Sj7Xq8ZGIBRvAg7I5YvOj5na7ddnXWZeqkZHDLM9eQgMiG7FUrceM7tNNsbSmWqVlNxnSHcE8qQjBZVIL8hjr35XL2saEJzNZltLN0pgtGOfH7yd8k46EbkbGjDF4U0qD6YB9mPOiDhxaaVoN3dARBNGWhEdcV0o+CcjyOc91lcPEyc6OaGfYpzII5w7bTPom4RBi9XT58hceqoq3CnsGc8xzEEBYeDmr59kiQ==
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=vivo.com;
Received: from SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11)
by KL1PR06MB6236.apcprd06.prod.outlook.com (2603:1096:820:d7::11)
with Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.23; Wed, 18 Sep
2024 02:53:07 +0000
Received: from SEYPR06MB5672.apcprd06.prod.outlook.com
([fe80::2f0c:5cf2:abb0:d852]) by SEYPR06MB5672.apcprd06.prod.outlook.com
([fe80::2f0c:5cf2:abb0:d852%5]) with mapi id 15.20.7962.022; Wed, 18 Sep 2024
02:53:07 +0000
From: Huan Yang
To: vivek.kasireddy@intel.com, Sumit Semwal ,
=?utf-8?q?Christian_K=C3=B6nig?= ,
Gerd Hoffmann , Dave Airlie ,
Andrew Morton , linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v7 6/7] udmabuf: remove udmabuf_folio
Date: Wed, 18 Sep 2024 10:52:29 +0800
Message-ID: <20240918025238.2957823-7-link@vivo.com>
X-Mailer: git-send-email 2.45.2
In-Reply-To: <20240918025238.2957823-1-link@vivo.com>
References: <20240918025238.2957823-1-link@vivo.com>
X-ClientProxiedBy: SI2P153CA0014.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::6)
To SEYPR06MB5672.apcprd06.prod.outlook.com
(2603:1096:101:bc::11)
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: SEYPR06MB5672:EE_|KL1PR06MB6236:EE_
X-MS-Office365-Filtering-Correlation-Id: 0ac98169-343c-47c1-d939-08dcd78d062e
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam: BCL:0;
ARA:13230040|1800799024|52116014|366016|376014|7416014|38350700014|921020;
X-Microsoft-Antispam-Message-Info:
WYv4eiTzpz2iCHoUYjAaX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mhtjlf7gATPMhjQ8oW3+jVkCxUH1HCI+T87IRmUbg/Q9rzldjcY92TJgQCC8QKQ6Oo/rQ5j3+Qrl+EDOzYp6FiN3+SwWI8viLZwx/W5SRuk8X5yguGqoi7ipQreOXmW6+iPeViMPR7aXurbVNiZYVYZHSbcoK3QbIXC8Bn67/l20DK3s2swZYoKrxmJNVGtPpvza3MtV7j2qtvQAwC+Am6wfrVDGAo90syD2+br0rWzJdSPBFfKncrg+BTbtzbJUy45t7j3TZ46DTmzNVxNYZJg1nCf7KNQfZksOH7Ah08tV2cReFq7VdugolijQlKqtHxVa2QSRFLiayaxi/zdA3cWkU=
X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:;
IPV:NLI; SFV:NSPM; H:SEYPR06MB5672.apcprd06.prod.outlook.com; PTR:; CAT:NONE;
SFS:(13230040)(1800799024)(52116014)(366016)(376014)(7416014)(38350700014)(921020);
DIR:OUT; SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
AVY76DOZvdeHT7q5FRsq7j/YMfBWIUNhwavmmlEKIpae4MVNrygdgRFfpwMfS2ovu786I5Teozib88XG2wM+bUWvAa4pjhMyzHH7UfvuVlNsSehwvDbHcmkYmB6JKqk7TXeaJuQ7ARGb3BEK4LSPIcyE/6l9QT0ay0EAXxe3S7H3m5hgApfQI21wLO1z+vbs5WCgDW3/B+8PArn68SuX/jtT6ZziWLUuk9WB2eFeD/pE9+NA7nagA7deEA30Jo2dTMZCg0yjrif7TrnUF+okg5fWHV2AJVe2fokqfjoXRnr8ulI46+AkwEV7nFaxZztyrApWoOcW++T2RQt7IHWuCtI11ymCQBd+zWkp8PVZpyAxHRwTY7WaFTzLeRisnddGEuob9h2p4fww5xAQTnHcpi4VhlghjvxF/6gGR1T5zJTQwf3kULRVf9xyHeM/l7dCn6Z6gX/BYd4tvPb/4Ys5Z+aGoJfn3LBA1DW7VQif9ZYsv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3hIrfqXa/pNdVN4Dpp
X-OriginatorOrg: vivo.com
X-MS-Exchange-CrossTenant-Network-Message-Id:
0ac98169-343c-47c1-d939-08dcd78d062e
X-MS-Exchange-CrossTenant-AuthSource: SEYPR06MB5672.apcprd06.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Sep 2024 02:53:07.5322 (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:
G63Hd6VY0fcgzqz5Uaj/a7OoUi7TlhDFAhbf8BkMo1Bn4TuGuSlY3oP7B5FAnHeDBu8lbsECdJnmLlfUU8+lIQ==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: KL1PR06MB6236
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 create an unpin list to record
each folio obtained from the list and unpinning them when released. To
maintain this, many struct have been established.
However, maintain this requires a significant amount of memory and
iter the list is a substantial overhead, which is not friendly to the
CPU cache.
When create, we arranged the folio array in the order of pin and set
the offset according to pgcnt. So, if record each pinned folio when
create, then can easy unpin it. Compare to use list to record it,
an array also can do this.
Hence, this patch setup a pinned_folios array(size is the pgcnt) to
instead of udmabuf_folio struct, it record each folio which pinned when
invoke memfd_pin_folios, then unpin folio by iter pinned_folios.
Note that, since a folio may be pinned multiple times, each folio can be
added to pinned_folios multiple times, depend on how many times the
folio has been pinned when create.
Compare to udmabuf_folio(24 byte size), a folio pointer is 8 byte, if no
large folio - each folio is PAGE_SIZE - and need to unpin when release.
So need to record each folio, by this patch, each folio can save 16 byte.
But if large folio used, depend on the large folio's number, the
pinned_folios array may take more memory, but it still can makes unpin
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 40ebff2c77f4..24badfefa6b4 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);
}
@@ -348,9 +340,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;
@@ -372,9 +366,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];
@@ -395,6 +387,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 Wed Sep 18 02:52:30 2024
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
X-Patchwork-Submitter: Huan Yang
X-Patchwork-Id: 13806540
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 0F91CCAC5B5
for ; Wed, 18 Sep 2024 02:53:17 +0000 (UTC)
Received: from gabe.freedesktop.org (localhost [127.0.0.1])
by gabe.freedesktop.org (Postfix) with ESMTP id 888C210E520;
Wed, 18 Sep 2024 02:53:16 +0000 (UTC)
Authentication-Results: gabe.freedesktop.org;
dkim=pass (2048-bit key;
unprotected) header.d=vivo.com header.i=@vivo.com header.b="WJQce8gV";
dkim-atps=neutral
Received: from APC01-TYZ-obe.outbound.protection.outlook.com
(mail-tyzapc01on2061.outbound.protection.outlook.com [40.107.117.61])
by gabe.freedesktop.org (Postfix) with ESMTPS id 218CA10E520
for ; Wed, 18 Sep 2024 02:53:13 +0000 (UTC)
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
b=ewoUO17ZWfKLx/2cMN5TN5zGG0dGQ3QTMG65oN8rxtvX1VovWpTjM5pfMYaS09xJLfG3bgz+Nzg8j2yg/neCLbpevSO4eILEwv7kab4rSa0YXVPPb6JgfRreoOrr6XtDeyKxcmWVD9Kd2zq9ncA5CtrnNjgbqg0Q0bBtlKnfGJSOMsPNDfEeh9Uh28T+9TIQ8tYhnrhrP4GrfUy/S891mq3WAx1Hy4eK/iAkQRyJHg8DxSphefAC99lU5mk0N7f8XwECFfBwbpQKerUnC0OcLOBqzaA41bkeXhaDM44z7QD3L74Ign6dvpuEBxl1Vb1aySXE62h1rqfYXODaqMezwg==
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=pBH4ndlxGVYUUbFKupkt4SbcLGPfRrO5yEdC89OSr6o=;
b=KDkZ/nFjmnso6Ar8FVQ7nQN3Up33hreOk4zO4K39GgFM+usJxdtCCnCi/LnnGjI9Zk9sJRgV+UGqY9RtA7n/0DjUXvYuNnruCxc4BfdYZTY0/PcvddMlU4W8eJIDyn1V7RWHMvtwU0fX6PQI3OUAoV762/e4wPXV8JQsh0GPo7TcfAVKStIU0CP0qv+ouh9ZDbYgeqCwOeL09KrA71ClAOV4Kf7gjBiscrlOOpGzM7lfsCxLa8GeOV4pi9Bb8kscnqJKTQr4EryRSLUWgyi+AzruO5cYZXXM/1LtC/HVjXvKDt/0YfHfJK41p+w5J1+bnHfWgpEtPatt7fZoTetFng==
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=pBH4ndlxGVYUUbFKupkt4SbcLGPfRrO5yEdC89OSr6o=;
b=WJQce8gVHKoBKGEkfHz8e2iSi6L4FMaDFIAGNktxuqWAOl3/H2X/UkO4roBIzR32vcUudJWXj/cF5usJx6N8Jzwpp3wg29kjLNWfh2nE3DBDgtK8S+/fIsiy+rUFXC0vyM7T8pRp87JMwvxZCYP+x/R4dyIdmpeEHV1zJ+EDdGFZPwt1ntOgAH0VzQlYh8WxvSD7GEQlE7fMmYpedSXNLiGF9M3jNhlYSAOiaP5LXXUErOe1xj9uA6rcpvcGIEKAqiOrwiM8pjftFEaUJ7pVD5Ls7hqpkg2qJ0aH5mWuG2YemiukGFQLihFrih/4CA173AJHFOzDgEBq3jOMXPzihg==
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=vivo.com;
Received: from SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11)
by KL1PR06MB6236.apcprd06.prod.outlook.com (2603:1096:820:d7::11)
with Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.23; Wed, 18 Sep
2024 02:53:10 +0000
Received: from SEYPR06MB5672.apcprd06.prod.outlook.com
([fe80::2f0c:5cf2:abb0:d852]) by SEYPR06MB5672.apcprd06.prod.outlook.com
([fe80::2f0c:5cf2:abb0:d852%5]) with mapi id 15.20.7962.022; Wed, 18 Sep 2024
02:53:10 +0000
From: Huan Yang
To: vivek.kasireddy@intel.com, Sumit Semwal ,
=?utf-8?q?Christian_K=C3=B6nig?= ,
Gerd Hoffmann , Dave Airlie ,
Andrew Morton , linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v7 7/7] udmabuf: reuse folio array when pin folios
Date: Wed, 18 Sep 2024 10:52:30 +0800
Message-ID: <20240918025238.2957823-8-link@vivo.com>
X-Mailer: git-send-email 2.45.2
In-Reply-To: <20240918025238.2957823-1-link@vivo.com>
References: <20240918025238.2957823-1-link@vivo.com>
X-ClientProxiedBy: SI2P153CA0014.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::6)
To SEYPR06MB5672.apcprd06.prod.outlook.com
(2603:1096:101:bc::11)
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: SEYPR06MB5672:EE_|KL1PR06MB6236:EE_
X-MS-Office365-Filtering-Correlation-Id: 73c6b6c9-1e62-4835-26a3-08dcd78d07dc
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam: BCL:0;
ARA:13230040|1800799024|52116014|366016|376014|7416014|38350700014|921020;
X-Microsoft-Antispam-Message-Info:
Jfq/NXpdQKKeeEafXyjGeIkz361NqCtE0SvGoiXz+N84JHXsfbrLVbMN/GHwiSx2aooBLyrG/YbmXJcc1i+8hzfVloiOiCXrWwKlqM/bVbinqwgnH6f2Sdydvf6RGdq5d1jq2V43JZ20Cr/ikQ0TMIEKtG35P7H2CxLjKNcaU5FqxatlZLRynDJG7Z27WhhiMwXd1fpF6XuGlA4xCDF07lpKqQQec4B/Cno7gH/NHw6m3qzB6+YelNoG50z7oZ1f+vSmh8crOmWzdRVnCAJsQUrQGHFQDSq1GynjeiMJX3TEIORTvPphtejlNPV9RlztkbPrR+qhHjrTxEHxu59nEauJxis8wZnpxP+CEVd+i9prpckDI670vsyQ+HkyhG5xzsheXPk0bqdFbv/F1H65lKzyiT09shYI3T/pyEOKgEgVUIs5hSsTNCp+1XSWJVC0s0u/k+KKrUOEvd3ruErIrjRxYGbHx6MRclfGotOFhEHQ4QdRDqxBl3z/iOtOfP0BKd8SIZXs8LkLaxTR5rh0GcPDpmZTq4FfLPZUAtcCiGXo0Bp0H38TGvgcKEKWDDV3o0TPnAe8vAEp+5qX1hcjO51xoYEQiy8lBkTO3XoJd2gQgsAZJlgiRk9Zz6KHicg4Rfz71me7RnVxMVfoFvqqh0Pf4mXVXDr3Gt/Z5LLuUpbMw1FMP2qKEXOsBsc1+gM/yrZt7DbEPlTD1wKwb4FaXVvvzdNHQJ3mnZVt0leqDPwA9FkeLRHzUhLkXWpAOXPJTnc2gAN15d2pTylBrI32ok4t/k492tcKPrSrwRucT9OC45ztG9i1IV8ze+cyTrKMz0spwIoSspQBSjwTEkBVf1/tdF1RK6sjoO7DKCnKQTCJegxf0L8nli/wcGb+xvJCZiQfvUUPILQnaFkNILkZ947VY7cvt4lJKQtzGH/BAwBMHKsFip4SmXg9S6XRQkJG1mE9M9Dx1/MtBSnAa9lNZcZabizdNSg767DfdOSKr/MYwsfoS17b95VfnB6hErhffICNTrVG7f/Lgm2jGvtn9zDdvZDUEbY5Jq9VB9Wz92zq7G1zxbdFbekop9HgbMc2J6wXW/DqUtDlYpvImZwOGChsLaRvaMttQ6/RfEY1Sto2DlpX/0w5rv01JbVsaWPQWnPPa3J0MVgiNr18nmw3eBu2armcs7r65ik+mwM1RlZy2Khl5YUSwbPd10NqZ2gVCtUsQ3VMi1GeIiaY0wM38A5RcY2kkZGEsMZSys0nOwkrWc2XZE8aBbz5u5Tj/xWSIdN+OLWT+JmvzFdrJrsEOhdV6rVT2j+qXwKcdmDpev8AHqg7GA5dwvtrJsE3jwhIdgBnXkkf6rrcbNzCOSDcxvTQE2ixAQr1a684h04j4a80fK8B7RzU2RBtbAoPXnnb9frl8j5x99Dx6pmnqg6Qw6MD6T2ZlWWA+lRPvEq07MY=
X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:;
IPV:NLI; SFV:NSPM; H:SEYPR06MB5672.apcprd06.prod.outlook.com; PTR:; CAT:NONE;
SFS:(13230040)(1800799024)(52116014)(366016)(376014)(7416014)(38350700014)(921020);
DIR:OUT; SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
4YTeLeDF0OyIJtSdcoi/xntm5P7SgMCOgv5+nGNzQb+CLLAXCmNgAA14L1GfHd98azHc6a3/9KceXUXof78IYZ8sU1icvjPg4EbSnCIWwde3uf5WnI+rxDiRYS/VoTgYPuudnV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JTnNLEx7YRcbAR80Ngf50AwbM89WjFhIfPoDJ5fRACVhTBOZAyHju4VNdJlPnBAlYyRn5JU1rcleawh8K1r08w4rRgwNNS+Z6vqHeOf8Yfbu8pl5gxShARzCap4MA0JSLQ0EUqbQjCKa2Z4yoMjj8gSNe5H9aKLPnQL3svuiGFLIbuJ4BVmwcnbKz8lOOb1h9yLOWD8JmD7MI6/I2Rqp2vv8YsFPKoYxOyb4AALw89IrUGk3lpevZ4sN8Vb8LnXh+8zqCDAZcKlXWSEz1LIqqPBnatuXRamcsStGMgZJvibvhTq0DRMJQYb60JswKJNUsf4x6eanS5ZVZ+FFiXfX46JfpirbAIZKi8QXYwDMcp98XA1y91TECajxiWE2WkSdmlyhbuf
X-OriginatorOrg: vivo.com
X-MS-Exchange-CrossTenant-Network-Message-Id:
73c6b6c9-1e62-4835-26a3-08dcd78d07dc
X-MS-Exchange-CrossTenant-AuthSource: SEYPR06MB5672.apcprd06.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Sep 2024 02:53:10.3853 (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:
mCTEZQOUBLPtUe8qO1/NkCtot61/zNGcoB3LUXcX28ZQuC+1p+CDoxuy0SYX5l2GG18xzMXV5TSk3pwyWWeGiw==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: KL1PR06MB6236
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 current way is dynamic alloc an array(use kvmalloc), get folios,
save into udmabuf and then free.
Depend on the size, kvmalloc can do something different:
Below PAGE_SIZE, slab allocator will be used, which have good alloc
performance, due to it cached page.
PAGE_SIZE - PCP Order, PCP(per-cpu-pageset) also given buddy page a
cache in each CPU, so different CPU no need to hold some lock(zone or
some) to get the locally page. If PCP cached page, the access also fast.
PAGE_SIZE - BUDDY_MAX, try to get page from buddy, due to kvmalloc adjusted
the gfp flags, if zone freelist can't alloc page(fast path), we will not
enter slowpath to reclaim memory. Due to need hold lock and check, may
slow, but still fast than vmalloc.
Anything wrong will fallback into vmalloc to alloc memory, it obtains
contiguous virtual addresses by loop alloc order 0 page(PAGE_SIZE), and
then map it into vmalloc area. If necessary, page alloc may enter
slowpath to reclaim memory. Hence, if fallback into vmalloc, it's slow.
When create, we need to iter each udmabuf item, then pin it's range
folios, if each item's range folio's count is large, we may fallback each
into vmalloc.
This patch find the largest range folio in items, then alloc this size's
folio array. When pin range folios, reuse this 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 24badfefa6b4..8ce1f074c2d3 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -338,28 +338,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) {
@@ -388,14 +380,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;
@@ -407,14 +400,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)
@@ -424,6 +422,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);
@@ -439,7 +443,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;
@@ -450,12 +454,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;
}