From nobody Fri Nov 29 15:54:28 2024
Received: from APC01-SG2-obe.outbound.protection.outlook.com
(mail-sgaapc01on2078.outbound.protection.outlook.com [40.107.215.78])
(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1C63C282E5;
Wed, 18 Sep 2024 02:52:59 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
arc=fail smtp.client-ip=40.107.215.78
ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1726627981; cv=fail;
b=cswqS2KtRimllAY3sFnIhDyvJ/0TcV5o3Qi6UvSGvvmpmF+BpxoQ2ow8DBCDkeKGW8O/bzjMSkBiiUkZLXUtqbBeGB894bSJ0h3pn5g6fd+8yTaXag2CZVBzdtqX7bchgsP6LYIe2X52aToiOcCzIi6rFrRPOL1VsoxbD7sy2ZE=
ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1726627981; c=relaxed/simple;
bh=qxD06Ft1qUU3poiLBG7Ax/3h3dOz1OZtadfeRGHv4cI=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
Content-Type:MIME-Version;
b=tRIsqZXEC5wCgodM6/l5cDX6Ej0Dbo0wNoVRlAuA4zWF9tU+aduEDOZNjzMFJWHqZl397hcNI76ZWlmNci1PzT4npMGFI68BnGoDXh5Ev6VA8AUWiDhuKJI8iAGPpDdn//al57I6T5zTigKM/gEdkyui1/u+YNi/lIcsF4n4SVw=
ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dmarc=pass (p=quarantine dis=none) header.from=vivo.com;
spf=pass smtp.mailfrom=vivo.com;
dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com
header.b=JKydu68v; arc=fail smtp.client-ip=40.107.215.78
Authentication-Results: smtp.subspace.kernel.org;
dmarc=pass (p=quarantine dis=none) header.from=vivo.com
Authentication-Results: smtp.subspace.kernel.org;
spf=pass smtp.mailfrom=vivo.com
Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com
header.b="JKydu68v"
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=20K=C3=B6nig?= ,
Gerd Hoffmann ,
Dave Airlie ,
Andrew Morton ,
linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org,
linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v7 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>
Content-Transfer-Encoding: quoted-printable
X-ClientProxiedBy: SI2P153CA0014.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::6)
To SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11)
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id:
List-Subscribe:
List-Unsubscribe:
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: SEYPR06MB5672:EE_|KL1PR06MB6236:EE_
X-MS-Office365-Filtering-Correlation-Id: 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:
=?us-ascii?Q?pq8E/VnWpdN/DcD+4hGexP2XoLgjF6CWYYWxe2LSWAkLvzLydxl53zlBW+Kb?=
=?us-ascii?Q?xQBV8BybCCfE2ctSs/58kg+zQgH5vPeTyc41dW0X29YCRNi7dst0Bi08rDsc?=
=?us-ascii?Q?Jom1xwDLJEzQiko3N5hD3cGRr1d8asumPwBQFEQz8oiTxk7gwD0sh7EqM5p4?=
=?us-ascii?Q?DfOo3YT4Wuje5YOaPu7P+ycD+lFrnRPJU7uPDvzb44ho2oWntPczSTZIX3n8?=
=?us-ascii?Q?RsFeKV6XTwHbrBrWHEYLFmU3WG5Sd/dX2bJ7SQ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?URuC1fGDLGdSuEJjfjM1rnUveSGFgr8nKDuw6/TnzL9NjCiTmShB3rVfXwK+?=
=?us-ascii?Q?kSqaHvgxbuicl2DcUzA2YZVDhs3zQ3twd9H0boiFLuCbZAX+1E96o+CfftVR?=
=?us-ascii?Q?/kr4QXCwd9zpzpxTplQJhy5ahXHFumTc6vTJEgAPSvlOhN1+0MvjoSl8mfO8?=
=?us-ascii?Q?kPem4cGK7u/D8ESA1Ub+rMLIbCHGWZ/BFYMKBb7zeUW0rFJGnrzESqmsp37U?=
=?us-ascii?Q?qkRbsGJGeUQM/u2omJgYFdTP8BQZWPnx1Hb67JSKN0MAojQjrEpRj8x7NL+h?=
=?us-ascii?Q?0NmrW4mg84U=3D?=
X-Forefront-Antispam-Report:
CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SEYPR06MB5672.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(52116014)(366016)(376014)(7416014)(38350700014)(921020);DIR:OUT;SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
=?us-ascii?Q?XmldueXiy6GACmwEMo2M/9KHeHrVzMA+6ngOxi3BazRGuLtOGLjlkUKwlQr/?=
=?us-ascii?Q?ItXhyrwWtIkWhEnSi7nsgihct9MaUTTinhN6I2MXKz+JvaMoNdorQQU2obMT?=
=?us-ascii?Q?iqaFeQp/taT+tGwMOe0sXAC22gjsNg8eutgs+l9vPMHHTx2ur7/OrqlBw763?=
=?us-ascii?Q?oyMfqRwGNiym/3lfGzuW7q60VbiWgU/aJbJgKbHDJt74ycVVjGq+rGUxrGXT?=
=?us-ascii?Q?Og7b0ZsiENCbIBTV2rPU2DlcPbOntIJ0FKlqR6KuzhIBQ+6GTkB6muo8Uv4h?=
=?us-ascii?Q?04Xi0oBSXVXXOkJpzOlj6daLdAtRht7zSgKElvs46kblGq4fmI5fY0t8nQzi?=
=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zRAJfzc40BK9Nzw+zI9dw7A4+Zsn+uf7uIOXjH4M9G4ULwc?=
=?us-ascii?Q?3xRf5BVQ2xUa4cOXf3xl+8JUSuMNJGUQgXmDgPaVFROuWAjFIEOVmo4BxTgw?=
=?us-ascii?Q?tldbQuVWkSBWwEwB1/xWVyDkmpNY7QJAedJrwQ2CboZNkbxefXXf0bHfZG6U?=
=?us-ascii?Q?JUa/NK8WKkxotX4hwuMY+2laejbJ3YVuyzXYOKJoocfzmBDOUxyXidHkEezq?=
=?us-ascii?Q?uTByzwGmfQH3O8vDtadXAvNB79O639Pc9opMQxirioylxV7/f8mYDJIh5Rl1?=
=?us-ascii?Q?tzImDtyD93GOratT0FqmN/TDVkys+eRn+9zYkLYtol7qzV408m8Ox7rO6NJo?=
=?us-ascii?Q?jsnP64lgfCOuNEFowAUijGVi8enmJXJxkJOHCdEv?=
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
Content-Type: text/plain; charset="utf-8"
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 =3D vmf->vma;
struct udmabuf *ubuf =3D vma->vm_private_data;
pgoff_t pgoff =3D vmf->pgoff;
- unsigned long pfn;
+ unsigned long addr, pfn;
+ vm_fault_t ret;
=20
if (pgoff >=3D ubuf->pagecount)
return VM_FAULT_SIGBUS;
@@ -51,7 +52,35 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
pfn =3D folio_pfn(ubuf->folios[pgoff]);
pfn +=3D ubuf->offsets[pgoff] >> PAGE_SHIFT;
=20
- return vmf_insert_pfn(vma, vmf->address, pfn);
+ ret =3D vmf_insert_pfn(vma, vmf->address, pfn);
+ if (ret & VM_FAULT_ERROR)
+ return ret;
+
+ /* pre fault */
+ pgoff =3D vma->vm_pgoff;
+ addr =3D vma->vm_start;
+
+ for (; addr < vma->vm_end; pgoff++, addr +=3D PAGE_SIZE) {
+ if (addr =3D=3D vmf->address)
+ continue;
+
+ if (WARN_ON(pgoff >=3D ubuf->pagecount))
+ break;
+
+ pfn =3D folio_pfn(ubuf->folios[pgoff]);
+ pfn +=3D 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;
}
=20
static const struct vm_operations_struct udmabuf_vm_ops =3D {
--=20
2.45.2
From nobody Fri Nov 29 15:54:28 2024
Received: from APC01-SG2-obe.outbound.protection.outlook.com
(mail-sgaapc01on2078.outbound.protection.outlook.com [40.107.215.78])
(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A205139D09;
Wed, 18 Sep 2024 02:53:01 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
arc=fail smtp.client-ip=40.107.215.78
ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1726627983; cv=fail;
b=HcsVtNXNM8i8RBIprUWGtdLwY3YUzKaT1/O813p391940XiTl+j5viXxpXFIOzw2jz99HVLMqrt3oy6hCZGP6bsO1rlR3zGSqEcraEo7XHXIiBi5pB7SQd0PONkQJd7RiMxbAjgCni+IiYz8mnuQMflAfUaKsWWZLqo7Y13xIxY=
ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1726627983; c=relaxed/simple;
bh=/gKolQTMs38UCvE511ydGXd706sDH+pHUsomxv5mFh4=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
Content-Type:MIME-Version;
b=h3lCPkxDYFosdNvfZu1Yyswf1CjXo3GauM0BZKTtPHuKfsMiJKQPt+Nt/8pCnEv7Ma4jmWL6cL43i5bRjkGuCkLMVR2lp04SE2BmaB+XLFgvHKfXb8jKvCDJ65m7ublMamawnJFFRQC+3+Kcsw5shtr3WoZYydS5GZjFziZEHjQ=
ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dmarc=pass (p=quarantine dis=none) header.from=vivo.com;
spf=pass smtp.mailfrom=vivo.com;
dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com
header.b=qu3JB8+I; arc=fail smtp.client-ip=40.107.215.78
Authentication-Results: smtp.subspace.kernel.org;
dmarc=pass (p=quarantine dis=none) header.from=vivo.com
Authentication-Results: smtp.subspace.kernel.org;
spf=pass smtp.mailfrom=vivo.com
Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com
header.b="qu3JB8+I"
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=20K=C3=B6nig?= ,
Gerd Hoffmann ,
Dave Airlie ,
Andrew Morton ,
linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org,
linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v7 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>
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
X-ClientProxiedBy: SI2P153CA0014.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::6)
To SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11)
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id:
List-Subscribe:
List-Unsubscribe:
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: SEYPR06MB5672:EE_|KL1PR06MB6236:EE_
X-MS-Office365-Filtering-Correlation-Id: 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?B?OWxLdEptdW9Qb3krek1QeGVydzBXSXM4a2ExMWljcUVqaGx2S283WFJMVWVD?=
=?utf-8?B?MllMV2swelAxWGd0RlZQVTBGK3RueVNtOS96YXpWbG1vUEFiQ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tVK1NsRnNHWXc4bXBSNExsVWJ0M0IxcE5s?=
=?utf-8?B?S3FNRUlsU3p1TmE5LzdGUmZsbUxabzBDeE5jbDFvWUM4WkY4Q1VwSWJuMUs1?=
=?utf-8?Q?KfkGGQcUPQ0=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?B?eHJDRWZGdHY5WlFxTGtyUkQ0dllGU0Vvd1luOG5wN2Q4ZDBzU1E0MkgrU3VZ?=
=?utf-8?B?VGIyTUt0ZzA4ZlFzTGtCbHBuTkZJN3BqSEVhTkpDeGd2WU9sWjdPcW9VUGZ1?=
=?utf-8?B?NnZLSnhESHRzVWJldnBvR2ZHbzZVdE1JbHFsdmR6SkRJWXg4NTA0Q1N4blor?=
=?utf-8?B?MDJXbVJ2d3J5MVRsTU9ZM3VpdUJXZ0pHLzBuSUVXazdpT0tIcVk4MkFDU0g0?=
=?utf-8?B?VUJwUE1WcFF4dXZsemt4Mzg4Y3BETlp5ZXRQbElmZ292MmFLZTZrQ2VGNkVy?=
=?utf-8?B?RHBHaEowNkNCQkIrSkpBZTM4VS9WdDZQSXpWWnAxbnNPTlA5bDlJcE5vYnYv?=
=?utf-8?B?VDNnUnN1cVc5cFdsWDNsMEZJOGZSWnpFNU05cCtWemU0eEg1bU5FcWsweVF4?=
=?utf-8?B?MTcxRkdRQTV4dC9NMHYrdDlyeVcrazduMUpmVUlXdUFvTXdJbnkzd3BTNmEw?=
=?utf-8?B?YU9BTkVDZmFHTy9UcUJndnlTMVUvOHB5TVpUVlNDc0dLMnBaQ3ExdEtkNkwy?=
=?utf-8?B?QkN2M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UN3BmUFJ4RnorZ2dVV1R0THZj?=
=?utf-8?B?bWJrRHNvbVU3YTZTSUZVaXRoeXRpbVJYVGNVRXR6SjJjSjc4aTAwcEJjakRj?=
=?utf-8?B?cDJPMU9LVlBFRERQUW5GbFp5SitUc1hxbXdMNXJxcUpRTTBqdkVyWWNHYW5U?=
=?utf-8?B?SjRqZS9IWDluNktJZlE5RElORVVSbUMweVE1RUEyUm13ZlNVQXhkcldLNDFP?=
=?utf-8?B?eVFCWWRRNHgzdkMzemtSdERZUTdUSmtraHZ2TmJrK0RidDRFT0lCdHBJTEVQ?=
=?utf-8?B?NkVzVjF0OTNlZW5UejZSdFl1amsya2MrRHRVaXl1dFdRQlNBMVpUek9jQytl?=
=?utf-8?B?dDJleG45dHNRZzdkSk95Y2xOUFV3SUhjMlZCenhjcE1DdmNPRm9RY0xWaUJy?=
=?utf-8?B?NnJiczhGTGFzY3FmejNsS2NyM0VZNlJaY3JLaGNwbDltU21sbDdrTnRPcHh0?=
=?utf-8?B?M0djVUNDY0tiV00yQlhLY2VCMmJtNGlIaVR0QVMrQnFNS3p6OTM4cFU5WGpn?=
=?utf-8?B?WXM5clo5WHBENzB6aGN4dCs0aUZEQU1yTDFod0JXUE0wS1k4dWRkRFBleDIy?=
=?utf-8?B?VDMyWC9wS3FMMGovWE5UU2x4dGFRMlNzR1EzSjNSOU43MDV0ZEVaYSs0VFdR?=
=?utf-8?B?eGhTOFg5bXRrTGFRenUzL21sZzk0amRQdmJ5eGt3cFVTMk4zemljZFdNaWpi?=
=?utf-8?Q?gGOgr/K+qduMqrzSn/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
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_pa=
ges+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=C3=B6nig
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 ios=
ys_map *map)
=20
dma_resv_assert_held(buf->resv);
=20
- pages =3D kmalloc_array(ubuf->pagecount, sizeof(*pages), GFP_KERNEL);
+ pages =3D kvmalloc_array(ubuf->pagecount, sizeof(*pages), GFP_KERNEL);
if (!pages)
return -ENOMEM;
=20
@@ -117,7 +117,7 @@ static int vmap_udmabuf(struct dma_buf *buf, struct ios=
ys_map *map)
pages[pg] =3D &ubuf->folios[pg]->page;
=20
vaddr =3D vm_map_ram(pages, ubuf->pagecount, -1);
- kfree(pages);
+ kvfree(pages);
if (!vaddr)
return -EINVAL;
=20
@@ -225,8 +225,8 @@ static void release_udmabuf(struct dma_buf *buf)
put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL);
=20
unpin_all_folios(&ubuf->unpin_list);
- kfree(ubuf->offsets);
- kfree(ubuf->folios);
+ kvfree(ubuf->offsets);
+ kvfree(ubuf->folios);
kfree(ubuf);
}
=20
@@ -351,14 +351,14 @@ static long udmabuf_create(struct miscdevice *device,
if (!ubuf->pagecount)
goto err;
=20
- ubuf->folios =3D kmalloc_array(ubuf->pagecount, sizeof(*ubuf->folios),
- GFP_KERNEL);
+ ubuf->folios =3D kvmalloc_array(ubuf->pagecount, sizeof(*ubuf->folios),
+ GFP_KERNEL);
if (!ubuf->folios) {
ret =3D -ENOMEM;
goto err;
}
- ubuf->offsets =3D kcalloc(ubuf->pagecount, sizeof(*ubuf->offsets),
- GFP_KERNEL);
+ ubuf->offsets =3D kvcalloc(ubuf->pagecount, sizeof(*ubuf->offsets),
+ GFP_KERNEL);
if (!ubuf->offsets) {
ret =3D -ENOMEM;
goto err;
@@ -372,7 +372,7 @@ static long udmabuf_create(struct miscdevice *device,
goto err;
=20
pgcnt =3D list[i].size >> PAGE_SHIFT;
- folios =3D kmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
+ folios =3D kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
if (!folios) {
ret =3D -ENOMEM;
goto err;
@@ -382,7 +382,7 @@ static long udmabuf_create(struct miscdevice *device,
ret =3D memfd_pin_folios(memfd, list[i].offset, end,
folios, pgcnt, &pgoff);
if (ret <=3D 0) {
- kfree(folios);
+ kvfree(folios);
if (!ret)
ret =3D -EINVAL;
goto err;
@@ -411,7 +411,7 @@ static long udmabuf_create(struct miscdevice *device,
}
}
=20
- kfree(folios);
+ kvfree(folios);
fput(memfd);
memfd =3D 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;
}
--=20
2.45.2
From nobody Fri Nov 29 15:54:28 2024
Received: from APC01-SG2-obe.outbound.protection.outlook.com
(mail-sgaapc01on2078.outbound.protection.outlook.com [40.107.215.78])
(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B09613D53E;
Wed, 18 Sep 2024 02:53:03 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
arc=fail smtp.client-ip=40.107.215.78
ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1726627985; cv=fail;
b=lmwe5Jvn5F1Rip5tVNqeUq2XuBXpack/Gg0q2l+rvdVYDus30g+sxmF4y0r0kPQrfDI12mlHbK6ExLPaA31DdSz1uOyBi9q1q2ckvy3KZGi+mxPj80kmuRD9cUDU1jZ6RMXodQaN2IUe2xuFxwrTNA7+/LfKataRObLl09v7+wY=
ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1726627985; c=relaxed/simple;
bh=a+prow2G8AOmB1mazi2wSP+bUuPXY5IMcpLNRCoxAlM=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
Content-Type:MIME-Version;
b=u3VUpu99GVShja9quKq4HXjkMtKk/acnaKMDBTAkauFGNl+qZe3zidy14r4prn1EyBAONaXYWLevfjKU8w9KkRh2SYNXB0mTerFtGAaOn6GuoPdPhMA/NTdwveA4NORQBtQwWL8Zco4drvu0HNp776bdfJFOHYXWAVGAcR/V2VQ=
ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dmarc=pass (p=quarantine dis=none) header.from=vivo.com;
spf=pass smtp.mailfrom=vivo.com;
dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com
header.b=Yv9zqMr7; arc=fail smtp.client-ip=40.107.215.78
Authentication-Results: smtp.subspace.kernel.org;
dmarc=pass (p=quarantine dis=none) header.from=vivo.com
Authentication-Results: smtp.subspace.kernel.org;
spf=pass smtp.mailfrom=vivo.com
Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com
header.b="Yv9zqMr7"
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=20K=C3=B6nig?= ,
Gerd Hoffmann ,
Dave Airlie ,
Andrew Morton ,
linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org,
linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v7 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>
Content-Transfer-Encoding: quoted-printable
X-ClientProxiedBy: SI2P153CA0014.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::6)
To SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11)
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id:
List-Subscribe:
List-Unsubscribe:
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: SEYPR06MB5672:EE_|KL1PR06MB6236:EE_
X-MS-Office365-Filtering-Correlation-Id: 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:
=?us-ascii?Q?NhV9IdxDzVXyNzetCmjbDyqcoWjLC/yWk53OrUUgkXVLu/T9su3r3PS5bX79?=
=?us-ascii?Q?8pmYnPaG40jsxiWc73GclwdaChEXLRA8hN6GeeSjTQn5KVpUccAqLsUYj+iP?=
=?us-ascii?Q?dcZ6D+xi5AIxuYmIRBI763l3cSJhxxinbReVX1YLoE+YuhbG9OPu3k3kk1uT?=
=?us-ascii?Q?CQET2WZ6ON8Y4DxC6qdgxg5iIB7L0K0A1v+wh8itBAcy1cyLG/7k3nGBmKpN?=
=?us-ascii?Q?dcEwL46cQghVC52EjCeYBqxzzylZs1fyZviYcFeJ4OdIjw9udXx2Q9FWvUZe?=
=?us-ascii?Q?5CJ0z5+Vdo7a4+EpWNlN07omDZTTanOaIrLBMEkep9PE61YdEyE9ysI+X26i?=
=?us-ascii?Q?edIOeD/HucEudtc9UAtMmKhkZVQ6zWldiS4gkzZgW887uhNZjaXZO+0+Q0zD?=
=?us-ascii?Q?KpSgmcUAALbNkvknHGxuQBG6j37DcwBFHPYW+eaXLWwEI64SUsXfDNbhXqtP?=
=?us-ascii?Q?zjd8uAXPUANT/diETMld6Wkmqi8Rq3kFVSZ4k5u8UuGyYF8MY7nBaBbl8k72?=
=?us-ascii?Q?0DMq6J83wfXeKETaVrRlOeClH2vT0VRsyQIupbOs7bX318nvayImFqQ2jyr0?=
=?us-ascii?Q?GUxuLUopOjZhGVXx/WIl7jypMPIvDk4LZK9YKQioyeGFZa1Dt7hYlQTOiQIA?=
=?us-ascii?Q?aDgi3t8+P8obXP+jyH0MBkGpRiihOIW4Z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:(13230040)(1800799024)(52116014)(366016)(376014)(7416014)(38350700014)(921020);DIR:OUT;SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
=?us-ascii?Q?SMZeFA+EuoR1qEUWUAoacDIL8Fy4ndBotw+csp1fwjyTAvyy2uIS5dpbqq6T?=
=?us-ascii?Q?tDX6CKOGSUrX/Jc25eJ9bHY69gHkdTRlOAVaW+8GrGHODUsp6Cv34vccHc79?=
=?us-ascii?Q?+DmFl0GhlzCDx804XWNNSqZZqH5IW8HnX31WQDl/mJHZEdjEFj9mzIOCz0cj?=
=?us-ascii?Q?mxYyoCQGYVsyKaclbn8yr4hUN6SOFYq9zYQXmlPoHXt+ezJhZ8E+hPIlvF3B?=
=?us-ascii?Q?fMIBPHtldP3zj7QEHR5zeQCmmxC/4soTe7uQJ6aX9IzOZkSoUxBf0rnZWKhp?=
=?us-ascii?Q?g7YHf0E6GvHyqlqHfhNFMfDOiTzuc1WCiN1FDNrBWd1uRHAYNh8c+9Y1SKzp?=
=?us-ascii?Q?ot7y7sCfx/QLMnWUTthyAqKL1S9ByqBjxzBIb599nM5Rms+nS4ZbrK6plTgK?=
=?us-ascii?Q?r+XC88QFnCtoaTAtOPRhaip7GDHahyH8DfVkyhc/rbtjlEqdBkai7F77BJxa?=
=?us-ascii?Q?IdAiiOZBfTD5KOEkSHGVgEibxuhXti9uxqJ76GnrLe/fU3swBInnx44wP6Xk?=
=?us-ascii?Q?ciXpBoRzb08NtwLG84DjMxL8gs1DFWZlzeyplcmbaVGjKPXX7l4DeepfOOnw?=
=?us-ascii?Q?JrUqhxt0mNiB0FDHnml90EcjqzXh0/apwN7fLQSRZ0/JP44JsByAF9X/Wit6?=
=?us-ascii?Q?JpFmfKiAK045ysM/7NAyF/4S3R6KjsQTEYzkNU+iyTt2usql/NJQzlrXUzdx?=
=?us-ascii?Q?b6bMB+gNdcDmKx6MBt1PqYMQNNjmpSStF1O13rWrnrhm5D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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
Content-Type: text/plain; charset="utf-8"
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 v=
m_area_struct *vma)
static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map)
{
struct udmabuf *ubuf =3D buf->priv;
- struct page **pages;
+ unsigned long *pfns;
void *vaddr;
pgoff_t pg;
=20
dma_resv_assert_held(buf->resv);
=20
- pages =3D 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 =3D kvmalloc_array(ubuf->pagecount, sizeof(*pfns), GFP_KERNEL);
+ if (!pfns)
return -ENOMEM;
=20
- for (pg =3D 0; pg < ubuf->pagecount; pg++)
- pages[pg] =3D &ubuf->folios[pg]->page;
+ for (pg =3D 0; pg < ubuf->pagecount; pg++) {
+ unsigned long pfn =3D folio_pfn(ubuf->folios[pg]);
=20
- vaddr =3D vm_map_ram(pages, ubuf->pagecount, -1);
- kvfree(pages);
+ pfn +=3D ubuf->offsets[pg] >> PAGE_SHIFT;
+ pfns[pg] =3D pfn;
+ }
+
+ vaddr =3D vmap_pfn(pfns, ubuf->pagecount, PAGE_KERNEL);
+ kvfree(pfns);
if (!vaddr)
return -EINVAL;
=20
--=20
2.45.2
From nobody Fri Nov 29 15:54:28 2024
Received: from APC01-SG2-obe.outbound.protection.outlook.com
(mail-sgaapc01on2078.outbound.protection.outlook.com [40.107.215.78])
(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id 648C715099E;
Wed, 18 Sep 2024 02:53:05 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
arc=fail smtp.client-ip=40.107.215.78
ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1726627987; cv=fail;
b=iLN46a2K2yYjaWzRon15chB+oy5/c7N7SqWnVKMIi6o+aUTo39ONOzSoZIjB2nYVUUpfgmA7+Ev0y7rR6He+oywEy1BQzbrHm+v3RtE290XZyOEE0P6nOtUwjU1A5yOfB9Dms1ANc+x24+Z+FAu3uxDononiBcURbnsLh4g3ODo=
ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1726627987; c=relaxed/simple;
bh=jk4/ZJxhCjruhYu630Y4VXiSl37DBQ/fcc7XaAc50EM=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
Content-Type:MIME-Version;
b=jeluyu3+bsN4owWTL92My0XvliL7sRI8LbLKpk2ZTYTQ32155O6cPUGKnvQiDSb099m/gEJZWnPYMsr3dOyYTh8KnajjHf+YSBkN9TFE2hwyAFGg94Us38hvXboyBEU85IViM4OQBoWTOCQYm7s8yJl7KGEuo26QQfj9s8Idq8E=
ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dmarc=pass (p=quarantine dis=none) header.from=vivo.com;
spf=pass smtp.mailfrom=vivo.com;
dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com
header.b=lBSAn+iM; arc=fail smtp.client-ip=40.107.215.78
Authentication-Results: smtp.subspace.kernel.org;
dmarc=pass (p=quarantine dis=none) header.from=vivo.com
Authentication-Results: smtp.subspace.kernel.org;
spf=pass smtp.mailfrom=vivo.com
Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com
header.b="lBSAn+iM"
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
b=alXeCrE0lsqZVSGIAAp+gCRhgITvaUZmTxoEejvR68UEou3A8Sbq6hLaiF0vzOpw0jQLimtv6eAoM5CSykomGIT3nHFcsdpvLrZ6YKtwV5Ow0Mp5nO/c0O5W6CNU9HS3CAK3HvOnUUNze9bRslH+NUA6Ylx2Xk51CtoVX3J+x6Ll/6P5bL+rCDHGv3q6DodyiI02INb1iHoSB3kNoROfhWi3gveFjA39WbHG0hHFsx/vixlJSUN3ldcdiAnKHTF3qwdjPvOUUd1gDFiN3bCfHZAwbda+1ge8IbfAf8n4cc4Jp8+NCzmfjR/1Bd7fsH0oyQ+WA3fNN3Trf73RK6MXyA==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
s=arcselector10001;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1;
bh=eiAQP2XOZ/W2FfmhMmIpXM39JfU6KKhxAyOVGhBDjU8=;
b=EUVqMsVtDMQ1729BVz6tgTsoOLTszpaJPZ5YiqHCMjhP1Zv5AboBqY8NzQldJ2dgVwc7Me/ANCP8Eeo7Y8q1u+bWQ8W7lXolB/4u2sI9ax1zTihfptJCTDeTIz5gi7PqwMv6A3qmbRoOXO9TCUqnl3ZDutYdOmscI1m8CMVgRDJKvXhuLW6SjCAg4yPLRHXIRAktVUuj8Y3lGGJUthWhvp767B2FroW7Dfiku7wA5Cx/UUxKAYRu+HcxSaRo4K7HRWQ2efvlC5xowom3Hz3pPAVjjRE7fm0B2/clV8Qzp8FszUmeQAkwM9qXkjqUQlHRbXv0f0KyoJYq5lztossUlg==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass
smtp.mailfrom=vivo.com; dmarc=pass action=none header.from=vivo.com;
dkim=pass header.d=vivo.com; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vivo.com; s=selector2;
h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
bh=eiAQP2XOZ/W2FfmhMmIpXM39JfU6KKhxAyOVGhBDjU8=;
b=lBSAn+iMTG3BtMDV8l7M79ReljBM84ez0CT7gD7hSkAetkbXaNlYt//IEUEeOS9ed5KrOXRwFHDmSLJsVwZKwM+TARtNJWJejNfiUIr8LBq4WVnVV53bvFZDRI/snL8Hunfr/iljwek3Ev8MuPkIzJ6DriBgM2IK1J5e4kdXVVxnik17mgZPJ8pNEO75yW2lTSms+E7GryE/nyM2qx1ESw2zTei8nDwju3OUE11isBZkFX3BCfiDXlzhq0nmjrrOs+89ACh7ZmBcpZR0OTVh3v4ibI4aZepUtCtDEkUORkr8Sf+hI3gRv2AxM9nCMolxRyGAIEhwrZcJj3rxE+IsdQ==
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=vivo.com;
Received: from SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11)
by KL1PR06MB6236.apcprd06.prod.outlook.com (2603:1096:820:d7::11) with
Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.23; Wed, 18 Sep
2024 02:53:01 +0000
Received: from SEYPR06MB5672.apcprd06.prod.outlook.com
([fe80::2f0c:5cf2:abb0:d852]) by SEYPR06MB5672.apcprd06.prod.outlook.com
([fe80::2f0c:5cf2:abb0:d852%5]) with mapi id 15.20.7962.022; Wed, 18 Sep 2024
02:53:01 +0000
From: Huan Yang
To: vivek.kasireddy@intel.com,
Sumit Semwal ,
=?UTF-8?q?Christian=20K=C3=B6nig?= ,
Gerd Hoffmann ,
Dave Airlie ,
Andrew Morton ,
linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org,
linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v7 4/7] udmabuf: udmabuf_create pin folio codestyle cleanup
Date: Wed, 18 Sep 2024 10:52:27 +0800
Message-ID: <20240918025238.2957823-5-link@vivo.com>
X-Mailer: git-send-email 2.45.2
In-Reply-To: <20240918025238.2957823-1-link@vivo.com>
References: <20240918025238.2957823-1-link@vivo.com>
Content-Transfer-Encoding: quoted-printable
X-ClientProxiedBy: SI2P153CA0014.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::6)
To SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11)
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id:
List-Subscribe:
List-Unsubscribe:
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: SEYPR06MB5672:EE_|KL1PR06MB6236:EE_
X-MS-Office365-Filtering-Correlation-Id: db7238e7-bb33-49c6-529e-08dcd78d0283
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam:
BCL:0;ARA:13230040|1800799024|52116014|366016|376014|7416014|38350700014|921020;
X-Microsoft-Antispam-Message-Info:
=?us-ascii?Q?v1kC9CsQTxJ0mHIPVzG/Wsv4vudd5MvZ8Bt57INR/J1gZsQfHs47rgwJAsQ4?=
=?us-ascii?Q?UANat8vMS6O2L9l51CPa9BItHYs1Ua7BosB+/mfvQ7PkxzXr66UO6w4fCowK?=
=?us-ascii?Q?40zB1K8mR0ky2ZbcqnAew+jcm8qFpYORih95YVaMtbY5kK9lC5F5QeP9vLvE?=
=?us-ascii?Q?2IPi5yzDm7WBDYCcscPOw0tcpzgK0zxEEnMI5fun2yI4PePFZ6HztkKYFmIn?=
=?us-ascii?Q?u3ET15KITrTJ4C/mo3Qq0RAhHc4v+VQ+ffotFJU2JayfJ/CVtwlgsMYW4iQv?=
=?us-ascii?Q?kCSfJ2tbftbBd9c5wdttCo4laIhV2Ss2KubWb5gGEw2Io44NeZZ9dBDzvkHd?=
=?us-ascii?Q?WHeRPawCBdYCEno5Ko5fHwwsy0cFnXkpHYJjK88HG7PTiXWdt3UDSC+J9J2x?=
=?us-ascii?Q?XjNZTd4sdFsTr7CtFEJh0NENWaJKyOQmJ95V0Nfho4bn2DGoINkWh+iFYXHx?=
=?us-ascii?Q?o/Co4by/duXqcGnTezPA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K2ftJsL6l5eZw?=
=?us-ascii?Q?PEfmu+ypt+rtYKW08usCcZ83HQz6oBpVIpxtlT7JUl/TinowYp4kvp5radc3?=
=?us-ascii?Q?qcOtoZJ1waU=3D?=
X-Forefront-Antispam-Report:
CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SEYPR06MB5672.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(52116014)(366016)(376014)(7416014)(38350700014)(921020);DIR:OUT;SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
=?us-ascii?Q?BlQY9Xq1zxH31eAA4W9V+v75iJiLImO/l29XouU1noceRvs4In+XrXVrFF4p?=
=?us-ascii?Q?HBB5lPJGP4a4Vciu1bkG6/ogTQryKUNeeqxUbwysxs99SYtkcXj1ixhKWq0X?=
=?us-ascii?Q?4LTxU63JO5ts/0G0QdBslk85m8YfyJ3shYbAjf6Jr3YoysMk2R3dP8JFfran?=
=?us-ascii?Q?xFFYj8C61eY6EWDffHWv7CSqR2EspUpdt8JUh3shE3BXyrE52IeI994E7FPp?=
=?us-ascii?Q?/cwNlFSr0TcguqjIxrQkFBUVrumGTcb8U+XBytBdFcUR+Ij5FCM/leHwfWSj?=
=?us-ascii?Q?5JELOXkKQbSbTjh4hVLZjYRpjtoHfMOTHySQ+etfMqLL8WyvMTUJuNVb9Bam?=
=?us-ascii?Q?bSCriWMwJjyMaLArTP75m2GPJAVUUbbKTkFcGA/IDB7lET9+AuTKxiXt/peK?=
=?us-ascii?Q?8kO38RtKYT7xIhPL48O0vTuzxfj5MIKEuQ9gNtsCXAsl80YXS0WB4kSzCTXL?=
=?us-ascii?Q?VAu54xXkep8Fk5fRHO/hE8Iaq0C1H6Jhq76ooEisj50QVJb5mi+jH/1n+C/1?=
=?us-ascii?Q?lDIw5VUaZvNnjTpEmzbalL9J83Y71kdC+aGaM4pCmPl5iFJv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q?5+HI2z63nMGU4dh5wINfm0Q7LuEwsuk4rQE2L40MSy7ZaR7dMH9lroR4lCz/?=
=?us-ascii?Q?UFlVImP/nQ99hzyDgwnu7zwn87MktfoxIisZDZKtUVxnEA0EDhUU/3kTrW3O?=
=?us-ascii?Q?hhAjKoiOjCuCIc8wmNDjCsu96mELCwo5MBdt1asG?=
X-OriginatorOrg: vivo.com
X-MS-Exchange-CrossTenant-Network-Message-Id:
db7238e7-bb33-49c6-529e-08dcd78d0283
X-MS-Exchange-CrossTenant-AuthSource: SEYPR06MB5672.apcprd06.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Sep 2024 02:53:01.4094
(UTC)
X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted
X-MS-Exchange-CrossTenant-Id: 923e42dc-48d5-4cbe-b582-1a797a6412ed
X-MS-Exchange-CrossTenant-MailboxType: HOSTED
X-MS-Exchange-CrossTenant-UserPrincipalName:
5EbJbCiHdfJsz0W1YlLe2ByVH4aXufYQjMYQxKSp+IWZeQKf0WOqq7yDvVNXxsaE22j5QoubdRcHHLpeedY9tw==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: KL1PR06MB6236
Content-Type: text/plain; charset="utf-8"
This patch aim to simplify the memfd folio pin during the udmabuf
create. No functional changes.
This patch create a udmabuf_pin_folios function, in this, do the memfd
pin folio and then record each pinned folio, offset.
This patch simplify the pinned folio record, iter by each pinned folio,
and then record each offset in it.
Compare to iter by pgcnt, more readable.
Suggested-by: Vivek Kasireddy
Signed-off-by: Huan Yang
Acked-by: Vivek Kasireddy
---
drivers/dma-buf/udmabuf.c | 137 +++++++++++++++++++++-----------------
1 file changed, 76 insertions(+), 61 deletions(-)
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index aa182a9dcdfa..9d58c045b8eb 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -291,9 +291,6 @@ static int check_memfd_seals(struct file *memfd)
{
int seals;
=20
- if (!memfd)
- return -EBADFD;
-
if (!shmem_file(memfd) && !is_file_hugepages(memfd))
return -EBADFD;
=20
@@ -328,17 +325,68 @@ static int export_udmabuf(struct udmabuf *ubuf,
return dma_buf_fd(buf, flags);
}
=20
+static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd,
+ loff_t start, loff_t size)
+{
+ pgoff_t pgoff, pgcnt, upgcnt =3D ubuf->pagecount;
+ struct folio **folios =3D NULL;
+ u32 cur_folio, cur_pgcnt;
+ long nr_folios;
+ long ret =3D 0;
+ loff_t end;
+
+ pgcnt =3D size >> PAGE_SHIFT;
+ folios =3D kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
+ if (!folios)
+ return -ENOMEM;
+
+ end =3D start + (pgcnt << PAGE_SHIFT) - 1;
+ nr_folios =3D memfd_pin_folios(memfd, start, end, folios, pgcnt, &pgoff);
+ if (nr_folios <=3D 0) {
+ ret =3D nr_folios ? nr_folios : -EINVAL;
+ goto end;
+ }
+
+ cur_pgcnt =3D 0;
+ for (cur_folio =3D 0; cur_folio < nr_folios; ++cur_folio) {
+ pgoff_t subpgoff =3D pgoff;
+ size_t fsize =3D folio_size(folios[cur_folio]);
+
+ ret =3D add_to_unpin_list(&ubuf->unpin_list, folios[cur_folio]);
+ if (ret < 0)
+ goto end;
+
+ for (; subpgoff < fsize; subpgoff +=3D PAGE_SIZE) {
+ ubuf->folios[upgcnt] =3D folios[cur_folio];
+ ubuf->offsets[upgcnt] =3D subpgoff;
+ ++upgcnt;
+
+ if (++cur_pgcnt >=3D pgcnt)
+ goto end;
+ }
+
+ /**
+ * In a given range, only the first subpage of the first folio
+ * has an offset, that is returned by memfd_pin_folios().
+ * The first subpages of other folios (in the range) have an
+ * offset of 0.
+ */
+ pgoff =3D 0;
+ }
+end:
+ ubuf->pagecount =3D upgcnt;
+ kvfree(folios);
+ return ret;
+}
+
static long udmabuf_create(struct miscdevice *device,
struct udmabuf_create_list *head,
struct udmabuf_create_item *list)
{
- pgoff_t pgoff, pgcnt, pglimit, pgbuf =3D 0;
- long nr_folios, ret =3D -EINVAL;
- struct file *memfd =3D NULL;
- struct folio **folios;
+ pgoff_t pgcnt =3D 0, pglimit;
struct udmabuf *ubuf;
- u32 i, j, k, flags;
- loff_t end;
+ long ret =3D -EINVAL;
+ u32 i, flags;
=20
ubuf =3D kzalloc(sizeof(*ubuf), GFP_KERNEL);
if (!ubuf)
@@ -347,81 +395,50 @@ static long udmabuf_create(struct miscdevice *device,
INIT_LIST_HEAD(&ubuf->unpin_list);
pglimit =3D (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
for (i =3D 0; i < head->count; i++) {
- if (!IS_ALIGNED(list[i].offset, PAGE_SIZE))
+ if (!PAGE_ALIGNED(list[i].offset))
goto err;
- if (!IS_ALIGNED(list[i].size, PAGE_SIZE))
+ if (!PAGE_ALIGNED(list[i].size))
goto err;
- ubuf->pagecount +=3D list[i].size >> PAGE_SHIFT;
- if (ubuf->pagecount > pglimit)
+
+ pgcnt +=3D list[i].size >> PAGE_SHIFT;
+ if (pgcnt > pglimit)
goto err;
}
=20
- if (!ubuf->pagecount)
+ if (!pgcnt)
goto err;
=20
- ubuf->folios =3D kvmalloc_array(ubuf->pagecount, sizeof(*ubuf->folios),
- GFP_KERNEL);
+ ubuf->folios =3D kvmalloc_array(pgcnt, sizeof(*ubuf->folios), GFP_KERNEL);
if (!ubuf->folios) {
ret =3D -ENOMEM;
goto err;
}
- ubuf->offsets =3D kvcalloc(ubuf->pagecount, sizeof(*ubuf->offsets),
- GFP_KERNEL);
+
+ ubuf->offsets =3D kvcalloc(pgcnt, sizeof(*ubuf->offsets), GFP_KERNEL);
if (!ubuf->offsets) {
ret =3D -ENOMEM;
goto err;
}
=20
- pgbuf =3D 0;
for (i =3D 0; i < head->count; i++) {
- memfd =3D fget(list[i].memfd);
- ret =3D check_memfd_seals(memfd);
- if (ret < 0)
- goto err;
+ struct file *memfd =3D fget(list[i].memfd);
=20
- pgcnt =3D list[i].size >> PAGE_SHIFT;
- folios =3D kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
- if (!folios) {
- ret =3D -ENOMEM;
+ if (!memfd) {
+ ret =3D -EBADFD;
goto err;
}
=20
- end =3D list[i].offset + (pgcnt << PAGE_SHIFT) - 1;
- ret =3D memfd_pin_folios(memfd, list[i].offset, end,
- folios, pgcnt, &pgoff);
- if (ret <=3D 0) {
- kvfree(folios);
- if (!ret)
- ret =3D -EINVAL;
+ ret =3D check_memfd_seals(memfd);
+ if (ret < 0) {
+ fput(memfd);
goto err;
}
=20
- nr_folios =3D ret;
- pgoff >>=3D PAGE_SHIFT;
- for (j =3D 0, k =3D 0; j < pgcnt; j++) {
- ubuf->folios[pgbuf] =3D folios[k];
- ubuf->offsets[pgbuf] =3D pgoff << PAGE_SHIFT;
-
- if (j =3D=3D 0 || ubuf->folios[pgbuf-1] !=3D folios[k]) {
- ret =3D add_to_unpin_list(&ubuf->unpin_list,
- folios[k]);
- if (ret < 0) {
- kfree(folios);
- goto err;
- }
- }
-
- pgbuf++;
- if (++pgoff =3D=3D folio_nr_pages(folios[k])) {
- pgoff =3D 0;
- if (++k =3D=3D nr_folios)
- break;
- }
- }
-
- kvfree(folios);
+ ret =3D udmabuf_pin_folios(ubuf, memfd, list[i].offset,
+ list[i].size);
fput(memfd);
- memfd =3D NULL;
+ if (ret)
+ goto err;
}
=20
flags =3D head->flags & UDMABUF_FLAGS_CLOEXEC ? O_CLOEXEC : 0;
@@ -432,8 +449,6 @@ static long udmabuf_create(struct miscdevice *device,
return ret;
=20
err:
- if (memfd)
- fput(memfd);
unpin_all_folios(&ubuf->unpin_list);
kvfree(ubuf->offsets);
kvfree(ubuf->folios);
--=20
2.45.2
From nobody Fri Nov 29 15:54:28 2024
Received: from APC01-SG2-obe.outbound.protection.outlook.com
(mail-sgaapc01on2078.outbound.protection.outlook.com [40.107.215.78])
(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6BEBD15C139;
Wed, 18 Sep 2024 02:53:07 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
arc=fail smtp.client-ip=40.107.215.78
ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1726627989; cv=fail;
b=bFJPIIDRGoNBF+hR8ygmh37gfuqzlzqzEG4twBKibz2jEf4+geH+Nfb7htdFfkfV8+ZB+dWyod2QsYedwHoGY48vwvGxYpmoc+HTKGwVGuAZH7y5w0JFb11j7hCkPQPlHfo64kVKSWQklBppCXeX2B1k0k1t6jnRsqTlncF9+ug=
ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1726627989; c=relaxed/simple;
bh=DX5+CF8hi7M0+XwIdf34umZzR0svoNwA8zFM256CrR0=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
Content-Type:MIME-Version;
b=kcPZy8rPJzWeBexgreSd1IOdtKsbvzG4djxlBqwZTd8cDdtvT9Ct03Gmp99bti43vMEG8hO6NaW4uwq8swONQdPQS8Rxpunsde5cNw9qjnx09lQe6Y3KTa894/KnlOI21QcwlASldzk2U22LLzPh9PZRs701IsvS9goux31Mll0=
ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dmarc=pass (p=quarantine dis=none) header.from=vivo.com;
spf=pass smtp.mailfrom=vivo.com;
dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com
header.b=gI3k0aEW; arc=fail smtp.client-ip=40.107.215.78
Authentication-Results: smtp.subspace.kernel.org;
dmarc=pass (p=quarantine dis=none) header.from=vivo.com
Authentication-Results: smtp.subspace.kernel.org;
spf=pass smtp.mailfrom=vivo.com
Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com
header.b="gI3k0aEW"
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=20K=C3=B6nig?= ,
Gerd Hoffmann ,
Dave Airlie ,
Andrew Morton ,
linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org,
linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v7 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>
Content-Transfer-Encoding: quoted-printable
X-ClientProxiedBy: SI2P153CA0014.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::6)
To SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11)
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id:
List-Subscribe:
List-Unsubscribe:
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: SEYPR06MB5672:EE_|KL1PR06MB6236:EE_
X-MS-Office365-Filtering-Correlation-Id: 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:
=?us-ascii?Q?X+8aT7cWW7GxHcgK2k84niAdw/UJ9Sa0uRqRlWS1kaOStHaP81y90aGyfxrs?=
=?us-ascii?Q?2tMjQebSX0mhFTnZy55qegrlLIVebf6FKBLYHLKqhnI97kNb2IaMnOSBBVFQ?=
=?us-ascii?Q?6ufAbC+hcmSRYtrYMV1QcCodq42HE337hsn8a3QMUXQWcM2KU1dw0zAmw0fB?=
=?us-ascii?Q?LbH6zOqC2LqiU2229kWMuSmWKu5LlXszAEeN4h1b2iqEdm3SUSo+nAmSdA1r?=
=?us-ascii?Q?94G9qKhYDtkfs4U7ZjZO8uIiDvMsEIl8G8drysu5fsq9spKnGJdAgPQEJZPF?=
=?us-ascii?Q?aDgQoRC4wytAWB3n509R84v9Vp/tzTV0ZZVH8LUMxobqccB+PBtv3IZmUwMX?=
=?us-ascii?Q?j/RO044Sx9mj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iMwlTSUt+ONo2oqjk5FPYIKfR?=
=?us-ascii?Q?CdQO/j+f38TsqZIEZNO0aU8Xzk8GyU7usbWenTNsid6DuKiSSsGC4kLmeFtB?=
=?us-ascii?Q?+U6LB/UDToTvyH/+2GUZWnWff+Bw+6+hcMwPXxurvriMUQ9AqrFkYBSfAtpT?=
=?us-ascii?Q?b2RcyqRuowbKT9pW1OhMIFaZehSXd0WkhTY6rDKKeCAYmpeJFIvysuJIrMaU?=
=?us-ascii?Q?8r8R3eMLnKs=3D?=
X-Forefront-Antispam-Report:
CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SEYPR06MB5672.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(52116014)(366016)(376014)(7416014)(38350700014)(921020);DIR:OUT;SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
=?us-ascii?Q?5StMi5Zvrykufibcz8X7cp7yyhy7riryR+888ZigI5El0vW4GDG0itK0UvLh?=
=?us-ascii?Q?N6iOeT9ULN1JLzqhL04svOa1ZFHnkXZ1Ax44XKOYaalFx/hGKkZThkhCRiNg?=
=?us-ascii?Q?LfSUYdDSyqSrFQxgVsOBTBrRERYXcNm0vTsLaG6bOaq4+L6IjqoIFYBqbZIe?=
=?us-ascii?Q?P5UWk4NkfcZvK9XS6AhRkNRoVW3swwI+g/yM0UWBGhwWhtsEdcNxSEDU7nKb?=
=?us-ascii?Q?fO0zxTTSwMK9YdDcLssDS/zMpugarWYlT1Yb8w2rwWEaZnXhxMjSBWl/lt9G?=
=?us-ascii?Q?UpAey5dgw6hg2V3iptOeAjqzCBs84iB6ieKLgjc32Os9okGSlc1xl4KPKy2n?=
=?us-ascii?Q?4ohLKUHxwN/Lk5WsA4TTCS5tBjObf5sxJ/YeXLkLxGNqlhNpmQHhmMlAinFi?=
=?us-ascii?Q?Tozd4+ncK+GjG9L5ckq3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1JwLGTlV?=
=?us-ascii?Q?pduLezVyOcYem9k34RrmsD1Yyj65F30Qs5apZBklLN5pkvUhnY82wVkRy0yY?=
=?us-ascii?Q?XUZhLXNeylXvXAvrtRpN5/y1aZ1Gm19yK/oqINnbeE31yfeXkIWMURCDvBnc?=
=?us-ascii?Q?imZ3a19kQ96xNTtPUaWJzmUk8aKJ1gOP8LmlCVkv7fXeDhy1Pm/4Ufrhw/gR?=
=?us-ascii?Q?X3Mp6X+I8JptuT9R4uSGpLtEAZRq3L3a8Pm3aCimIlHIytf0VcXm+cuvnHcy?=
=?us-ascii?Q?fQaxm2KLcSte/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
Content-Type: text/plain; charset="utf-8"
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_l=
ist,
return 0;
}
=20
+static __always_inline int init_udmabuf(struct udmabuf *ubuf, pgoff_t pgcn=
t)
+{
+ INIT_LIST_HEAD(&ubuf->unpin_list);
+
+ ubuf->folios =3D kvmalloc_array(pgcnt, sizeof(*ubuf->folios), GFP_KERNEL);
+ if (!ubuf->folios)
+ return -ENOMEM;
+
+ ubuf->offsets =3D 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 =3D 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);
=20
- unpin_all_folios(&ubuf->unpin_list);
- kvfree(ubuf->offsets);
- kvfree(ubuf->folios);
+ deinit_udmabuf(ubuf);
kfree(ubuf);
}
=20
@@ -392,33 +412,24 @@ static long udmabuf_create(struct miscdevice *device,
if (!ubuf)
return -ENOMEM;
=20
- INIT_LIST_HEAD(&ubuf->unpin_list);
pglimit =3D (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
for (i =3D 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;
=20
pgcnt +=3D list[i].size >> PAGE_SHIFT;
if (pgcnt > pglimit)
- goto err;
+ goto err_noinit;
}
=20
if (!pgcnt)
- goto err;
+ goto err_noinit;
=20
- ubuf->folios =3D kvmalloc_array(pgcnt, sizeof(*ubuf->folios), GFP_KERNEL);
- if (!ubuf->folios) {
- ret =3D -ENOMEM;
+ ret =3D init_udmabuf(ubuf, pgcnt);
+ if (ret)
goto err;
- }
-
- ubuf->offsets =3D kvcalloc(pgcnt, sizeof(*ubuf->offsets), GFP_KERNEL);
- if (!ubuf->offsets) {
- ret =3D -ENOMEM;
- goto err;
- }
=20
for (i =3D 0; i < head->count; i++) {
struct file *memfd =3D fget(list[i].memfd);
@@ -449,9 +460,8 @@ static long udmabuf_create(struct miscdevice *device,
return ret;
=20
err:
- unpin_all_folios(&ubuf->unpin_list);
- kvfree(ubuf->offsets);
- kvfree(ubuf->folios);
+ deinit_udmabuf(ubuf);
+err_noinit:
kfree(ubuf);
return ret;
}
--=20
2.45.2
From nobody Fri Nov 29 15:54:28 2024
Received: from APC01-SG2-obe.outbound.protection.outlook.com
(mail-sgaapc01on2069.outbound.protection.outlook.com [40.107.215.69])
(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1E36716BE0B;
Wed, 18 Sep 2024 02:53:10 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
arc=fail smtp.client-ip=40.107.215.69
ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1726627992; cv=fail;
b=R00KJTik5SVONjfDbv7D1EN8RvEumNKSz4xjOlud8H1SmEllaaKHetcz/TEmzCEpck4QOJ2rRxMcBK/XpsR79aekYnXR0BG4cDDBLm7NoThjaBnGSvFIxgR06eJRB26uazyBv7CRa22Y8+cNaaAuhd5lEy99QfX9UhLu48DALgk=
ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1726627992; c=relaxed/simple;
bh=oqize5TZts19ihGODp/xNNvAzmxV0LQdpFznDx0X7Ng=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
Content-Type:MIME-Version;
b=gteeKBJPK1YzjEPLuNfDXb5zGExx4sXQooMgP9yji+yZQNCU1bGGPXcMd+PWyTNszeFXnxNuHD/avTbfRFpkCgLQzKqUDB+Wr5gzF0v+x8jGFBSR9c74IycFaTOVCOAQco8hw83tMKFWifumA/DKiqazhYSd4OkRVG20Z9Z6aWc=
ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dmarc=pass (p=quarantine dis=none) header.from=vivo.com;
spf=pass smtp.mailfrom=vivo.com;
dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com
header.b=Qd24zVKB; arc=fail smtp.client-ip=40.107.215.69
Authentication-Results: smtp.subspace.kernel.org;
dmarc=pass (p=quarantine dis=none) header.from=vivo.com
Authentication-Results: smtp.subspace.kernel.org;
spf=pass smtp.mailfrom=vivo.com
Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com
header.b="Qd24zVKB"
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=20K=C3=B6nig?= ,
Gerd Hoffmann ,
Dave Airlie ,
Andrew Morton ,
linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org,
linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v7 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>
Content-Transfer-Encoding: quoted-printable
X-ClientProxiedBy: SI2P153CA0014.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::6)
To SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11)
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id:
List-Subscribe:
List-Unsubscribe:
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: SEYPR06MB5672:EE_|KL1PR06MB6236:EE_
X-MS-Office365-Filtering-Correlation-Id: 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:
=?us-ascii?Q?WYv4eiTzpz2iCHoUYjAaXySZS26cDgW9ugvRxu326R3Ewo9pNXcNE1sNY+VJ?=
=?us-ascii?Q?l8E4a9ShGg8bdJhaIQzGt0x8ZRwWz523dwwmqMQ+IefYFLyr2yHIzOJg8LXG?=
=?us-ascii?Q?VpgKk89koaCBVGkptIl5D9pHQsYDe0pzPhxO3aQ+B00pYTPCRgjHK+aMbL+4?=
=?us-ascii?Q?y1Kn8h01fvUVwVNNrQ94iXdeLGcFSBl8DPgIkWpweud2o6fca9xnSuMWFskH?=
=?us-ascii?Q?dGcFJ3LvtaQ7QIw3IGl724Jlg/vWrLpkXGRpXjOv3KLIc3l7ikVXiqjw5QXZ?=
=?us-ascii?Q?UCYQ64BUReiUJgQXby3a6mv4aiEGdQ7otAPcZPg6/G33mf1g/FhgKJG/e1Cv?=
=?us-ascii?Q?1morm+JdhYV8IaqRadIlqmXI4PezX3gnaBsLfXpO02IjNfQKFRP1hd6EYJO8?=
=?us-ascii?Q?pJiLb4UAczJ+IFj/M89LZHN+pNiYhmBJMCx1tzmV8TmLtviLu1v8ain0MP8t?=
=?us-ascii?Q?uo1C0l3ffsSkfQNL57mOuyw/A/k7XX1t2QzXVh2ds+zqzxVXVPXWFay5hm3b?=
=?us-ascii?Q?ggUIrmaNGAKDlSrCO2gXOTD0mhhQeIFgZN4d37YA47atPftw4h5hSdd2jqTW?=
=?us-ascii?Q?5qnBlKA3hil7A/t4vceway+/l/5pClwVUXMnOsYRon7foJbBt06ztyU8TPSH?=
=?us-ascii?Q?mNDaAoVcIFDnTkmXkgOL23J0NJ/XIIpAaIFwHEvU15MPL2VGTStlSPK59oDi?=
=?us-ascii?Q?dPZL80W0EU96eJWh1vskr+v2fTeETcMACrsrkl4l67s0eBvjoAoYxGEPl4GA?=
=?us-ascii?Q?sP1W0MA3sfYeCIzPOwHP2NPAy9jXnxqHbt1VcQE987b+oshCadqKWe9bFlFu?=
=?us-ascii?Q?HwgVi5T7NSPncoxFGtmTJifQp3W77pTMNYo8qNQ7l7zJ9QHtSpqZWRdVowVP?=
=?us-ascii?Q?2STlGgKfs/T2MgxMSinO1cKt1+MWYeIOepr7uDCyR21BU8jEuBvo6+ra7QlI?=
=?us-ascii?Q?gjUU5cEG2AhSDcEJoEkM5qud2BIAHJj+TEawhD//9fzMW6MYHEiJLzwnMXO7?=
=?us-ascii?Q?I920hHMEv4wlwd4w8ArmuTSxLIor3JgnfOFiRWiqSDgX26Z5Gk9Fkp2MECjS?=
=?us-ascii?Q?B9yMcVFCu+ARMsbIG6CheeTy+3mhtjlf7gATPMhjQ8oW3+jVkCxUH1HCI+T8?=
=?us-ascii?Q?7IRmUbg/Q9rzldjcY92TJgQCC8QKQ6Oo/rQ5j3+Qrl+EDOzYp6FiN3+SwWI8?=
=?us-ascii?Q?viLZwx/W5SRuk8X5yguGqoi7ipQreOXmW6+iPeViMPR7aXurbVNiZYVYZHSb?=
=?us-ascii?Q?coK3QbIXC8Bn67/l20DK3s2swZYoKrxmJNVGtPpvza3MtV7j2qtvQAwC+Am6?=
=?us-ascii?Q?wfrVDGAo90syD2+br0rWzJdSPBFfKncrg+BTbtzbJUy45t7j3TZ46DTmzNVx?=
=?us-ascii?Q?NYZJg1nCf7KNQfZksOH7Ah08tV2cReFq7VdugolijQlKqtHxVa2QSRFLiaya?=
=?us-ascii?Q?xi/zdA3cWkU=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-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?us-ascii?Q?zFCZYbumly3WHlmslvn7CYaIQ89U9YJ94nB+CAivRDhkpoR+MgWwOqXW5Zu4?=
=?us-ascii?Q?MrpqFkKdOLJTtDHL5DQY2zt7nYF9H0RVcrZxnyAj7D2AuA9vM/r9QObSTQiZ?=
=?us-ascii?Q?cnMdIgW+qMsFLsvvdGXK1KmUTc2CCpC6ZB51+4kxlrpqVQbSndrVkL80mrof?=
=?us-ascii?Q?m4QOxhRU3i8oAwVfTcpWKI67LnefeD7qGtlMFwoWtFWEt9W+5deDIaJUDeZ2?=
=?us-ascii?Q?QeHQMt2cwzVv926Hxmij6yFyOdPi8ZHBhFlwEX2Juam3VUkumwV8e2vH61Vc?=
=?us-ascii?Q?gDz+dm7nB6g04W48YQx00Jlpieb1Tdg9Eh1lMxiet0YLEZm+Mwyl3NVhVgDs?=
=?us-ascii?Q?1K/UI3eqD0z8fGJLKGfe186hoFwl4qf4Rfbx/YoucWkZ7iD8aXwKB41CToes?=
=?us-ascii?Q?I9W9OJMy0TG2l0O001EH8BLAnWBXHIx79aVW4iZd4MklLHXRYcc4J0big4eW?=
=?us-ascii?Q?9BbFUoQNi7y2fZ27BlO5hPXOn7ysBi11WlbLZst45amlixmcz5jVQNajzrcV?=
=?us-ascii?Q?Hl8AUCy0M24K+7tIgBeIf1EpEvCCsEYDzRFIqTwOC9Dy9KFrINeuAU/U5G01?=
=?us-ascii?Q?QLaQkYd7hascqOsyJ6rZa0B8Hr4UpPS9lZVQkCJTDice6ST5Be3X9jPlWD99?=
=?us-ascii?Q?jSOVrFOAdHaNtRZLmk+JBhA0Ni21eLiU+t5eJrJdfYM6UG50jf6BqlXEFknf?=
=?us-ascii?Q?q3WSngzQo7zm75yui8l9+I3hIrfqXa/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
Content-Type: text/plain; charset="utf-8"
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;
};
=20
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);
}
=20
-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;
=20
- while (!list_empty(unpin_list)) {
- ubuf_folio =3D list_first_entry(unpin_list,
- struct udmabuf_folio, list);
- unpin_folio(ubuf_folio->folio);
+ for (i =3D 0; i < ubuf->nr_pinned; ++i)
+ unpin_folio(ubuf->pinned_folios[i]);
=20
- 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 =3D kzalloc(sizeof(*ubuf_folio), GFP_KERNEL);
- if (!ubuf_folio)
- return -ENOMEM;
-
- ubuf_folio->folio =3D folio;
- list_add_tail(&ubuf_folio->list, unpin_list);
- return 0;
+ kvfree(ubuf->pinned_folios);
}
=20
static __always_inline int init_udmabuf(struct udmabuf *ubuf, pgoff_t pgcn=
t)
{
- INIT_LIST_HEAD(&ubuf->unpin_list);
-
ubuf->folios =3D kvmalloc_array(pgcnt, sizeof(*ubuf->folios), GFP_KERNEL);
if (!ubuf->folios)
return -ENOMEM;
@@ -236,12 +222,18 @@ static __always_inline int init_udmabuf(struct udmabu=
f *ubuf, pgoff_t pgcnt)
if (!ubuf->offsets)
return -ENOMEM;
=20
+ ubuf->pinned_folios =3D kvmalloc_array(pgcnt,
+ sizeof(*ubuf->pinned_folios),
+ GFP_KERNEL);
+ if (!ubuf->pinned_folios)
+ return -ENOMEM;
+
return 0;
}
=20
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 =3D ubuf->pagecount;
+ pgoff_t nr_pinned =3D ubuf->nr_pinned;
+ pgoff_t upgcnt =3D ubuf->pagecount;
struct folio **folios =3D NULL;
u32 cur_folio, cur_pgcnt;
+ pgoff_t pgoff, pgcnt;
long nr_folios;
long ret =3D 0;
loff_t end;
@@ -372,9 +366,7 @@ static long udmabuf_pin_folios(struct udmabuf *ubuf, st=
ruct file *memfd,
pgoff_t subpgoff =3D pgoff;
size_t fsize =3D folio_size(folios[cur_folio]);
=20
- ret =3D add_to_unpin_list(&ubuf->unpin_list, folios[cur_folio]);
- if (ret < 0)
- goto end;
+ ubuf->pinned_folios[nr_pinned++] =3D folios[cur_folio];
=20
for (; subpgoff < fsize; subpgoff +=3D PAGE_SIZE) {
ubuf->folios[upgcnt] =3D folios[cur_folio];
@@ -395,6 +387,7 @@ static long udmabuf_pin_folios(struct udmabuf *ubuf, st=
ruct file *memfd,
}
end:
ubuf->pagecount =3D upgcnt;
+ ubuf->nr_pinned =3D nr_pinned;
kvfree(folios);
return ret;
}
--=20
2.45.2
From nobody Fri Nov 29 15:54:28 2024
Received: from APC01-SG2-obe.outbound.protection.outlook.com
(mail-sgaapc01on2069.outbound.protection.outlook.com [40.107.215.69])
(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C3AE16DC15;
Wed, 18 Sep 2024 02:53:13 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
arc=fail smtp.client-ip=40.107.215.69
ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1726627995; cv=fail;
b=dkkeLMDqs7DOSxsM1+z7Hk48crmY70wO6gk7VUbHrlpvYw/Kk1KysPVxi3DK5Ix3MBndn1Ne+P/HFz1Nk7TH9v36YNSgLJdUw0jL3RdEM+LHqBg12MLhwhBPOVkDQu1TJ1DZlMWP7A1ZMCMIi+0wvyoFUP3ufZa3G246K/NJ2U8=
ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1726627995; c=relaxed/simple;
bh=m0LeUetQIbNPpjrR8LnFq4C3q9qH862sWgTvzCGCpKQ=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
Content-Type:MIME-Version;
b=RPORr6kzaUy17TwT+Zo1jPBUHrz7jgtBdonmO8iD8wI1E9i8JFrriKnKBv/y+aYK26d/3OYYRbfIU2xC+NMUcyBpuyqblKzrp7oMlV7yj5tiHbY8fx6jZjaTjr7gsQuKLhzbIQ/ofTIiYwpldULoDg2SgxPP1MDlpgA3XOnlgDI=
ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dmarc=pass (p=quarantine dis=none) header.from=vivo.com;
spf=pass smtp.mailfrom=vivo.com;
dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com
header.b=WJQce8gV; arc=fail smtp.client-ip=40.107.215.69
Authentication-Results: smtp.subspace.kernel.org;
dmarc=pass (p=quarantine dis=none) header.from=vivo.com
Authentication-Results: smtp.subspace.kernel.org;
spf=pass smtp.mailfrom=vivo.com
Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com
header.b="WJQce8gV"
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=20K=C3=B6nig?= ,
Gerd Hoffmann ,
Dave Airlie ,
Andrew Morton ,
linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org,
linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v7 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>
Content-Transfer-Encoding: quoted-printable
X-ClientProxiedBy: SI2P153CA0014.APCP153.PROD.OUTLOOK.COM (2603:1096:4:140::6)
To SEYPR06MB5672.apcprd06.prod.outlook.com (2603:1096:101:bc::11)
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id:
List-Subscribe:
List-Unsubscribe:
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: SEYPR06MB5672:EE_|KL1PR06MB6236:EE_
X-MS-Office365-Filtering-Correlation-Id: 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:
=?us-ascii?Q?Jfq/NXpdQKKeeEafXyjGeIkz361NqCtE0SvGoiXz+N84JHXsfbrLVbMN/GHw?=
=?us-ascii?Q?iSx2aooBLyrG/YbmXJcc1i+8hzfVloiOiCXrWwKlqM/bVbinqwgnH6f2Sdyd?=
=?us-ascii?Q?vf6RGdq5d1jq2V43JZ20Cr/ikQ0TMIEKtG35P7H2CxLjKNcaU5FqxatlZLRy?=
=?us-ascii?Q?nDJG7Z27WhhiMwXd1fpF6XuGlA4xCDF07lpKqQQec4B/Cno7gH/NHw6m3qzB?=
=?us-ascii?Q?6+YelNoG50z7oZ1f+vSmh8crOmWzdRVnC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?=
=?us-ascii?Q?rv01JbVsaWPQWnPPa3J0MVgiNr18nmw3eBu2armcs7r65ik+mwM1RlZy2Khl?=
=?us-ascii?Q?5YUSwbPd10NqZ2gVCtUsQ3VMi1GeIiaY0wM38A5RcY2kkZGEsMZSys0nOwkr?=
=?us-ascii?Q?Wc2XZE8aBbz5u5Tj/xWSIdN+OLWT+JmvzFdrJrsEOhdV6rVT2j+qXwKcdmDp?=
=?us-ascii?Q?ev8AHqg7GA5dwvtrJsE3jwhIdgBnXkkf6rrcbNzCOSDcxvTQE2ixAQr1a684?=
=?us-ascii?Q?h04j4a80fK8B7RzU2RBtbAoPXnnb9frl8j5x99Dx6pmnqg6Qw6MD6T2ZlWWA?=
=?us-ascii?Q?+lRPvEq07MY=3D?=
X-Forefront-Antispam-Report:
CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SEYPR06MB5672.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(52116014)(366016)(376014)(7416014)(38350700014)(921020);DIR:OUT;SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
=?us-ascii?Q?4YTeLeDF0OyIJtSdcoi/xntm5P7SgMCOgv5+nGNzQb+CLLAXCmNgAA14L1Gf?=
=?us-ascii?Q?Hd98azHc6a3/9KceXUXof78IYZ8sU1icvjPg4EbSnCIWwde3uf5WnI+rxDiR?=
=?us-ascii?Q?YS/VoTgYPuudnVdJBWJ32CyzH5p57QNPPzduYi/LbVGC54rg6vGRhfUGGppU?=
=?us-ascii?Q?Wyd6QML9y3z3i7ge0qw+y2p0X2I0QvZmaF2wnwBZei5HfZ/aoaMPMoxQcn3B?=
=?us-ascii?Q?iTjdItTNYYIoEp8VoajZ1+NujpKK6mnDxA2a3rjDiY+4ZHxrjLJk5YK8ZFgV?=
=?us-ascii?Q?efUMQ9+B4QYOn2KKIcXiR+96PlBZwn1mP1Cb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?us-ascii?Q?o96HjzoZbJTnNLEx7YRcbAR80Ngf50AwbM89WjFhIfPoDJ5fRACVhTBOZAyH?=
=?us-ascii?Q?ju4VNdJlPnBAlYyRn5JU1rcleawh8K1r08w4rRgwNNS+Z6vqHeOf8Yfbu8pl?=
=?us-ascii?Q?5gxShARzCap4MA0JSLQ0EUqbQjCKa2Z4yoMjj8gSNe5H9aKLPnQL3svuiGFL?=
=?us-ascii?Q?IbuJ4BVmwcnbKz8lOOb1h9yLOWD8JmD7MI6/I2Rqp2vv8YsFPKoYxOyb4AAL?=
=?us-ascii?Q?w89IrUGk3lpevZ4sN8Vb8LnXh+8zqCDAZcKlXWSEz1LIqqPBnatuXRamcsSt?=
=?us-ascii?Q?GMgZJvibvhTq0DRMJQYb60JswKJNUsf4x6eanS5ZVZ+FFiXfX46JfpirbAIZ?=
=?us-ascii?Q?Ki8QXYwDMcp98XA1y91TECajxiWE2WkSdmlyhbuf?=
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
Content-Type: text/plain; charset="utf-8"
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,
}
=20
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 =3D ubuf->nr_pinned;
pgoff_t upgcnt =3D ubuf->pagecount;
- struct folio **folios =3D NULL;
u32 cur_folio, cur_pgcnt;
pgoff_t pgoff, pgcnt;
long nr_folios;
- long ret =3D 0;
loff_t end;
=20
pgcnt =3D size >> PAGE_SHIFT;
- folios =3D kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
- if (!folios)
- return -ENOMEM;
-
end =3D start + (pgcnt << PAGE_SHIFT) - 1;
nr_folios =3D memfd_pin_folios(memfd, start, end, folios, pgcnt, &pgoff);
- if (nr_folios <=3D 0) {
- ret =3D nr_folios ? nr_folios : -EINVAL;
- goto end;
- }
+ if (nr_folios <=3D 0)
+ return nr_folios ? nr_folios : -EINVAL;
=20
cur_pgcnt =3D 0;
for (cur_folio =3D 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 =3D upgcnt;
ubuf->nr_pinned =3D nr_pinned;
- kvfree(folios);
- return ret;
+ return 0;
}
=20
static long udmabuf_create(struct miscdevice *device,
struct udmabuf_create_list *head,
struct udmabuf_create_item *list)
{
+ unsigned long max_nr_folios =3D 0;
+ struct folio **folios =3D NULL;
pgoff_t pgcnt =3D 0, pglimit;
struct udmabuf *ubuf;
long ret =3D -EINVAL;
@@ -407,14 +400,19 @@ static long udmabuf_create(struct miscdevice *device,
=20
pglimit =3D (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
for (i =3D 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;
=20
- pgcnt +=3D list[i].size >> PAGE_SHIFT;
+ subpgcnt =3D list[i].size >> PAGE_SHIFT;
+ pgcnt +=3D subpgcnt;
if (pgcnt > pglimit)
goto err_noinit;
+
+ max_nr_folios =3D max_t(unsigned long, subpgcnt, max_nr_folios);
}
=20
if (!pgcnt)
@@ -424,6 +422,12 @@ static long udmabuf_create(struct miscdevice *device,
if (ret)
goto err;
=20
+ folios =3D kvmalloc_array(max_nr_folios, sizeof(*folios), GFP_KERNEL);
+ if (!folios) {
+ ret =3D -ENOMEM;
+ goto err;
+ }
+
for (i =3D 0; i < head->count; i++) {
struct file *memfd =3D fget(list[i].memfd);
=20
@@ -439,7 +443,7 @@ static long udmabuf_create(struct miscdevice *device,
}
=20
ret =3D 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;
=20
+ kvfree(folios);
return ret;
=20
err:
deinit_udmabuf(ubuf);
err_noinit:
kfree(ubuf);
+ kvfree(folios);
return ret;
}
=20
--=20
2.45.2