From patchwork Thu Aug 22 08:43:33 2024
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
X-Patchwork-Submitter: Huan Yang
X-Patchwork-Id: 13773016
Received: from APC01-PSA-obe.outbound.protection.outlook.com
(mail-psaapc01on2065.outbound.protection.outlook.com [40.107.255.65])
(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 5B43619470;
Thu, 22 Aug 2024 08:44:46 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
arc=fail smtp.client-ip=40.107.255.65
ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1724316289; cv=fail;
b=nGrleQYjTKgDO1DpQR7jstJ6wEg18aN+5D9AG8SmPWkurSuKCANZQVGqEID6h2hRpxX/Xdxs5vpY1ZMhk2H8UY69d0w2ovKZwTRHBGxDhXpwqedZI017SYe0GogY9xYZwTWxOn//9wr4iAzoXp0f9fb7D1K76tFK+qXRzaxWPmw=
ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1724316289; c=relaxed/simple;
bh=oiu5UtSSW8XH/h/B60o95/K2pqT/jsE+Y3eyQRJYuag=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
Content-Type:MIME-Version;
b=DnErvWFeWmcbO0BO73JWNTGnqbWnqxOAkl04aXv7+fyfPE0oLBTTK80p4L/WYC0cy9lR/weDDCEigHJ5GTHqH8A97gmPguGgS9dU+VKQm+m1pqJ8wztytaWXV0K5CoMYpTk6MKDQN07jCOAHB1jl6NXJn0kfCH3iDRgjq8rhhBA=
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=hw62FJPG; arc=fail smtp.client-ip=40.107.255.65
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="hw62FJPG"
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
b=U7ve0okPVbm2Kil/p7fq3tjecuZvkKVIsOjigvufAkRGadarjio5lGbllw58cU+NMml+Wm01mdrNBpbxzlzVq9bPZBq0h+LtmS66PD+IwEZ4xvSv0Pvm+tsah4Y/LAtBDJWm2+CGB4XZ0PEniS1+f/ihD6guLCLWG/QivBQoVBU1fjiFy3t2/xnbWDoAFuH6OCnbCdAhvOlk1FNK0zp9wQaGioE96Ve8WyqpkCKJU3eshFgfbisaxyljrBVACjv1u8NzWmUR2nEcER27DIIKSXSThOoROKI3V7NtM+otHqqNUDL34QUay8AAWE6Hy0W8MdAm1JO8gq8A8DSpPq5b2w==
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=nzuJ8J9ZQT1nrd+zYDpOnn3+Vmeyyfpx3zVmuuT8KuI=;
b=AOeeOReXD9TqoP4U7+lEsth+DDcawcT3J8RsaSJnAD7JK62sobnIqKTveXLQfHc9JKmdSlJQFjEv2302TAVcOX0Q267xXdfYrTqB7jIdJ3W8ekVzZYTFZB7VKzrWQRljydmOF3kfRmFXaxQC83nN7dMW8mHVtE57Qqnk0BdN+uiyHZZJmBExD5MRP3JrynyMNGnAqF3vjQKoUYhuyJxMCr0go07Drw0GIL66osvpXR4cN9UieJaLJ42iXGzhtQfske4/dacqznxZAq051ZKejvWdSKtJ8Q0Yq+HbTUmO+w0G4rp9kdVNS04LFAYQSiFXH03X0BvFgnrXsI/uYXLLug==
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=nzuJ8J9ZQT1nrd+zYDpOnn3+Vmeyyfpx3zVmuuT8KuI=;
b=hw62FJPGy7/wwxQnqDuZqiKBJ0/FarpCd/B6mBiWWMtD/nSl9PszJ0WiFguLASK1ipLKs8q4SoBy1iju9wqyN4r63uc+CvL+1eyxwUZZWrmTBHi4vndJ19iQXl57Zu482X676bkBJBY0b+FptA+DvSF6+MpusfgmHk4nF1QJO2EXdJoqZU9FRdkK4ZAACiPIP7f/IpBjhwvTlFK3u9kxr/ILs7qCtO0kCJIEzyfTS3qkgK1M4eWXJGTGOVsmdJOUpVcX/cqJpmCepXunHUzbWR2svq8dRj/rj+gsEzV1p/lgpnxBnAsDpmyXcpofArBJkPAPjrVLIp3u0uxbV7B7CA==
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=vivo.com;
Received: from PUZPR06MB5676.apcprd06.prod.outlook.com (2603:1096:301:f8::10)
by TYZPR06MB6402.apcprd06.prod.outlook.com (2603:1096:400:425::6) with
Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.27; Thu, 22 Aug
2024 08:44:42 +0000
Received: from PUZPR06MB5676.apcprd06.prod.outlook.com
([fe80::a00b:f422:ac44:636f]) by PUZPR06MB5676.apcprd06.prod.outlook.com
([fe80::a00b:f422:ac44:636f%5]) with mapi id 15.20.7875.019; Thu, 22 Aug 2024
08:44:41 +0000
From: Huan Yang
To: Sumit Semwal ,
=?utf-8?q?Christian_K=C3=B6nig?= ,
Gerd Hoffmann , linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang ,
Vivek Kasireddy
Subject: [PATCH v4 1/5] udmabuf: direct map pfn when first page fault
Date: Thu, 22 Aug 2024 16:43:33 +0800
Message-ID: <20240822084342.1574914-2-link@vivo.com>
X-Mailer: git-send-email 2.45.2
In-Reply-To: <20240822084342.1574914-1-link@vivo.com>
References: <20240822084342.1574914-1-link@vivo.com>
X-ClientProxiedBy: SI1PR02CA0059.apcprd02.prod.outlook.com
(2603:1096:4:1f5::19) To PUZPR06MB5676.apcprd06.prod.outlook.com
(2603:1096:301:f8::10)
Precedence: bulk
X-Mailing-List: linux-media@vger.kernel.org
List-Id:
List-Subscribe:
List-Unsubscribe:
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: PUZPR06MB5676:EE_|TYZPR06MB6402:EE_
X-MS-Office365-Filtering-Correlation-Id: 57638795-8c5f-41a3-c021-08dcc286aa2d
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam:
BCL:0;ARA:13230040|1800799024|366016|376014|52116014|38350700014;
X-Microsoft-Antispam-Message-Info:
8TcsI1Nq2DhUDZyN96sOmRcmZLOhJUqto3ybeZRrOEABDjFNnUF7thuNEzKjCP80ic6qK+7nMxRRDE/VOvUWBqih2RSFYok10gnn31Lh63QnjTI/UCSWTqTLPlryxf/EScnKVhpoZQo7AiW3PTUC+K8WJQlDMOEaWBp2Lq6zmYcd1eAwmyhkwYj+8qeXyjGaVFL3AoGQ6CQUhku/zMHRweP54E2XihjKi9pLisK10piK3qNMJigF9nFb2r2VLpgT3bQXfqlkbIFGUdVO/3ibarV5SNFwQjwwtZfcvQf50RYlNkeKjK7VQfVcmMLYf8aIxOCw709f8ZOEVO3AlR1O25KEK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WygPJIt3A==
X-Forefront-Antispam-Report:
CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PUZPR06MB5676.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(52116014)(38350700014);DIR:OUT;SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
/jn4Hz8YfUYysaj8tuVOMVJ2WNEbPunMnmT7Oux4UktcICpClfQfFVaGxfVYKDrVp3PbbW5ROFa7mmua1quBtAsrAHEoyaERgXsadf/UxjeqqurXzKpBIw2E0HKTNQ1Z6DqAjMr1NtVOC+fA5scdL0TaZxV2zZbvT3oTj3x6S9fvQ0sDgQOnPxu8MeKGdRoTK+l8GyhXrcYvTHAfrmtLas5xkCP9WdqTr4SOTCrxFRJMTw3IY73HrM4SZYwunJ5ZUHDMucTCBvVXhsh1d/GRv2b/JqZhxkoI8HCSqAEno/kNlLpak6xdJefr3MMcCB2ugZOW892PrD3yaS4Rxwv6PbQNdROHSAGcGdIS4EPCW6IML1ZbbxvDzzybg/gwUvZwwwkfc2ls+qK5MtIUEFWahYKxkZkMMRs9AuIRLZeorugqPvigdkFPI3KANuvu2vRnCq7+17P4aZG8YOSNaMZqpEcsLtsrb4+HfYb7xDuR+ZxNhZnEqbpvnoaM9r0LAH2rAd4/dI89/AFKaQ08Do+eabnbzDL/UI5NWv280lpKQQR9X3FRQifLM9lLsvlevuvjxnQZlcnRKgs5M9pmQCMPifc7jcugXzKy0mdh3cLGPIPvskgxKUy0Pd0h0fsnOcO/ubswBgSMhOZ166O6SJhAWOETXJD8gsnT8a61EQBNHlcOGNdlHiPTX/oYkX3DPeI4LYpU+bzcEhbVijbs5wtApJBr2cffeDPGD4d3sCcjZKMfYArUhIXSLSktTP6MSAuLiJoWzZnFDGGXPqHm8NFlefeA/2odwmhRvRupXo3tFReH4D4esCPZ05XNfiIm7yOfrAx+y2x8ReLDBZj93STK8qDtSpUKEgQwO5zkL+UE3g9WwI0sAvjmMgGiGAFgD43L7nkYYL4ecUhr5igEXParyOplL6fMjnBnGVChfpFzHT4we80wsaKSY+Z0inRrd50gY3X4ItsDNE6txMiItuaZNn4UkWfqrLh9KWVVybrEFjgsmcNPoGp4JsjXSNcDaxfx1wjlMuHQf8XQ6IXKrjJ3vXne3mACINzit14LhotvpK1bzbrPW+jMO0go803SxhMk14649NZtdF005w9GGf09GkV6GJC0cuPgLCIH7HI5JO40vgYqJ2JyH2A0Dz99h4BUsHmz8JxyJSCa7tPQqZvtLNLhdJj4TFLs3fn9LkXlK4P4rOHtYUDoWTRs59F8i/jg3DduvXzM5X+GWXBO2soQG3oK2/C2gh8w92bJeB6vjcHQ2tPWCHZnmYLlgYiU3afWRH6Vb5Rgk+G1hSGlaB6Hxbd5olVnxMIb57XBAnP0Zgin7enx3AeEJF7M7SdM6zOVRutqrXXardKLyoUJSuKBtIn1hyO/+QNtbVyBoccC2iBaXba3TE9nH4rWmIU0rXfLzlzdFywNPwTr94w85F89i8uSAZx21Eczw8+naljtvyK5Sv8udZxbJnCvVZSVgVKkKQ3Ybop0aSlf90bPN6ull64YZtVq6dGAmaDL4yDIktNjUyKigqhvshkPt8DziAgp1GeAtTnkpoxZjVIKBqHgDpXXFGRdip5B0zkrgA8KD0VODcL6GusDvoehrUth8fSw
X-OriginatorOrg: vivo.com
X-MS-Exchange-CrossTenant-Network-Message-Id:
57638795-8c5f-41a3-c021-08dcc286aa2d
X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Aug 2024 08:44:41.8227
(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:
a0l9MHEH+zOwyOm3wAr5cDeLXM6Gx+svB5rGG2TwcNp219D8CeNUCpKThJdGcOu5BMJNuCHQJNLTRymBryLIxA==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYZPR06MB6402
The current udmabuf mmap uses a page fault to populate the vma.
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 vma area with pfn when the first page fault trigger, so,
any other access will not enter page fault.
Suggested-by: Vivek Kasireddy
Signed-off-by: Huan Yang
---
drivers/dma-buf/udmabuf.c | 26 ++++++++++++++++++++++++--
1 file changed, 24 insertions(+), 2 deletions(-)
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index 047c3cd2ceff..0e33d25310ec 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, end, pfn;
+ vm_fault_t ret;
if (pgoff >= ubuf->pagecount)
return VM_FAULT_SIGBUS;
@@ -51,7 +52,28 @@ 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;
+ end = vma->vm_end;
+ addr = vma->vm_start;
+
+ for (; addr < end; pgoff++, addr += PAGE_SIZE) {
+ if (addr == vmf->address)
+ continue;
+
+ pfn = folio_pfn(ubuf->folios[pgoff]);
+
+ pfn += ubuf->offsets[pgoff] >> PAGE_SHIFT;
+
+ if (vmf_insert_pfn(vma, addr, pfn) & VM_FAULT_ERROR)
+ break;
+ }
+
+ return ret;
}
static const struct vm_operations_struct udmabuf_vm_ops = {
From patchwork Thu Aug 22 08:43:34 2024
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-Patchwork-Submitter: Huan Yang
X-Patchwork-Id: 13773018
Received: from APC01-PSA-obe.outbound.protection.outlook.com
(mail-psaapc01on2065.outbound.protection.outlook.com [40.107.255.65])
(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 6CDD516EB76;
Thu, 22 Aug 2024 08:44:49 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
arc=fail smtp.client-ip=40.107.255.65
ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1724316291; cv=fail;
b=siNVwnczS545BMd6SkaDRAM3kNOfMZvW76SwSf3AzB6GBlnbhS/zyR9DPct3Cr2lRKafpo63dgmVReXC0GrURY1Uy8rmWkuT8w7/R75toeTDaSDP2NfyE65ixVz5+T7umXwQnnoUMn4wSrThjCpGJUpZqpOBXU1Ib3oRWuhZPto=
ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1724316291; c=relaxed/simple;
bh=uU+Jqdf/3/DHtfwUJSCwMfbQ2edyjuxtIqmJxPx23aM=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
Content-Type:MIME-Version;
b=jASMlpdJQ775tsSGRt7HtXB2hr4fVRdxpIDUyN0iTyr4UG/SAo+GfClQ6DPUqiq6fv3kVGDJKAbThQNefxjc52snQS2mWPPQCDGkpQqaSAyOiKYu58UDabraVTOuqb0d/hP9dcB49sGp6q2xUQORowUm53rubDEuhsKnCN8t/R4=
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=AGJgfKxk; arc=fail smtp.client-ip=40.107.255.65
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="AGJgfKxk"
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
b=lzmkWLz5b5jVZ81HPPvS9B94sXamN4WVaBEoOi5LrsSpmdUSeY8ZDg35+wRDtWUDpTymJWt+CYAX8kjrbbhf5zcfdmnMOsfeP7mHXMtCQsikC3KRUqZomP9u5C27R5baRyZ97Y87xsKzKXdtjWj978gDNzQnyjRTshWNJh0hXUlwk0jzMT37bJydDhFQORA/0eYPA/DSifKIEYQF7qzkYUEaoDzUp4UvFiZ4+v59VLGl2yWAX+Mr2c/PNY3aa3fKVlzZOMPnFJ6gHIPJpgKQL1ZBH1hGYVBhDoVfZWbvP5aPAIEdGJXu2grDLyy9tT7hyH583AQCwO/8qYks1w62Aw==
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=ZBoWxiC0767j5ureUaV1O4bR94sWbe0OopEd4xp5fjk=;
b=x3aBYm5/xAZj/sDkuN3olgycFaEHNrRlXKFsXKTIc85tJsbGOk7pSYL4uYPbVO0aaCI6s4wn7xxAPOiH5aIqoVoZYsy/vJuEIrqy5A7rZXGcFeIw2GSqYZ2Z1aTLAcQOsnI+JoHfeU2Q+ZwAy2AaFgSz8CR9/pR6PftWUeS90xtBXEweyaHiyktNowOzRRVBmuqZHbxdNDTROTCvf1/Q9FKkBBwPaHF8GGkO8/vBy6xE2ou2tssvkD5xOYRnNcpX+EZqMfy4y1NOu613/RZjgY8O8IbrkPTTFE6e0hYZ683p0vi+30YbhFWAK+itpq8dWrt5dL2xdaE/6ZwPgdRPCQ==
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=ZBoWxiC0767j5ureUaV1O4bR94sWbe0OopEd4xp5fjk=;
b=AGJgfKxk/vqN8lxyzoRBgZCAV5143FiDdgz7xAKqzRrF8ooRy6PDslRsn57EDn5+VtRycWWRz/t/i1xmyhjMt9glmqzYzTBMeblJu2/R1xOHx6NKHu/SuROs/wkeiuwDKwxYWnYTebGR/0UeXAm+KTbk3p++N1s0HFWhcwlOiIL0DBZ+q8TnttBzKubZgs10CHbUVWA/TFR2dov4ROvKOZD4flwz7CifvzxgZxqzoBWmdl0rYp5IgHJo8cjgZ0dH93u11gGxvwG9p2bnRgBud4FkoK5ibMhsMLaBEJXnSLENETvwgtAJedL7YK+LFk3Ilg/bU6t6p4DaUa4yW0dPZQ==
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=vivo.com;
Received: from PUZPR06MB5676.apcprd06.prod.outlook.com (2603:1096:301:f8::10)
by TYZPR06MB6402.apcprd06.prod.outlook.com (2603:1096:400:425::6) with
Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.27; Thu, 22 Aug
2024 08:44:44 +0000
Received: from PUZPR06MB5676.apcprd06.prod.outlook.com
([fe80::a00b:f422:ac44:636f]) by PUZPR06MB5676.apcprd06.prod.outlook.com
([fe80::a00b:f422:ac44:636f%5]) with mapi id 15.20.7875.019; Thu, 22 Aug 2024
08:44:44 +0000
From: Huan Yang
To: Sumit Semwal ,
=?utf-8?q?Christian_K=C3=B6nig?= ,
Gerd Hoffmann , linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang ,
Vivek Kasireddy
Subject: [PATCH v4 2/5] udmabuf: change folios array from kmalloc to kvmalloc
Date: Thu, 22 Aug 2024 16:43:34 +0800
Message-ID: <20240822084342.1574914-3-link@vivo.com>
X-Mailer: git-send-email 2.45.2
In-Reply-To: <20240822084342.1574914-1-link@vivo.com>
References: <20240822084342.1574914-1-link@vivo.com>
X-ClientProxiedBy: SI1PR02CA0059.apcprd02.prod.outlook.com
(2603:1096:4:1f5::19) To PUZPR06MB5676.apcprd06.prod.outlook.com
(2603:1096:301:f8::10)
Precedence: bulk
X-Mailing-List: linux-media@vger.kernel.org
List-Id:
List-Subscribe:
List-Unsubscribe:
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: PUZPR06MB5676:EE_|TYZPR06MB6402:EE_
X-MS-Office365-Filtering-Correlation-Id: 5d36d840-a83e-4f37-332a-08dcc286abb0
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam:
BCL:0;ARA:13230040|1800799024|366016|376014|52116014|38350700014;
X-Microsoft-Antispam-Message-Info: =?utf-8?q?LZwjfVBi2o/hRh7mZK4IbD3Omuzj2bc?=
=?utf-8?q?r4FE2Dn/P/B9FI4rcAoSg048Thw1gsfd9IsDbp9aq6qy8RchTas3eVJ7kZxL1D6Cf?=
=?utf-8?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x65?=
=?utf-8?q?MlnUkLWRFvZLKAwSaXX8wh/FKYKqKjYodWCz/QOhisXE0TGUkGuFqMqkvzeEqFvrB?=
=?utf-8?q?Hw7VUIn5DZJmGKmOEl78jAeRYdHx9xtIJWR8wp7puHtBm6G27V3fJ+zdkegrfsj9Q?=
=?utf-8?q?roy2vjKA/PIlACf+KyuTJ/kDJNxMT3rmwKs1HD70xSbusePH2tEf+ACsKRuW7mEg3?=
=?utf-8?q?EVRIm+dEOZ7OApfgIL+f0NdR8EBkhz4NPWR0TPV+LNJ/WVGVW8Z99DdX3hyjr2xKY?=
=?utf-8?q?42o9fBuTzcFHtytdwuVMWczpZSF4r9rkS4AWTe5HJmHokKS+nI9i9FksQSG7zqGwp?=
=?utf-8?q?BTIeuXKYuklqMA8U7A8SR0CPUiJ2zP7aqAwqCwerB7PG51aiCuzKsm7AvrJhQLPWX?=
=?utf-8?q?J/mxnf6QvjTqAf/LHYhGaiujllhxcCPaLA=3D=3D?=
X-Forefront-Antispam-Report:
CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PUZPR06MB5676.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(52116014)(38350700014);DIR:OUT;SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?3DxcYBgDUnNNKv1yUm/4tXlhnV4J?=
=?utf-8?q?OGla/wXJ01otzV03hLJNzwaJBEWKTV+pIyRQDaaugy6WkLLu4l99Ls1VM2VEeItEp?=
=?utf-8?q?rdIArkq/J4OsSa/Z7ddG6Si7lCUPzNqVr9hMYx42XoawvLm3H4NLI+Pg79f/hFTLJ?=
=?utf-8?q?avlp8e9ET+mn62NB7VBuDJmkb+OR0gKQiKVMkhDiyGHcOvMEdgZjGNGEomnW4lRPD?=
=?utf-8?q?jCaXK5xtaemTDpuiVF0enEaY3Go/qsHWf4sM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iEhV2l/kK7AKr7DUsOb0t+xllfHYiCckr6SurZtfnM1xvJsGlmn?=
=?utf-8?q?HOiObc9+APTA9XpACO7TbKt1koYojmKWtD7nMx0M5k+b/pjZ+UN9rM9PSY46HGxqY?=
=?utf-8?q?5zMXq7pnYIt8Swlpzc+pIRSLhuA+wqW7o+NkilaNDXIbYdwZz8lZ31QlksyREmeWb?=
=?utf-8?q?SwCiKRyl719T5JUmZYmYMf+GhBciT8IhenNLUfILvM/sNRgMQUuRWkYKxNHz5ySYh?=
=?utf-8?q?r9eebJMTEU5Wqnp9w3pgc5inEP7l4LeSTrVCfUSdPRp+FOTkKQn82cc7cWFqBCf3B?=
=?utf-8?q?dZxQp6SEW9kMi//wgiNYFYacQfPj9urV4Slz+Tm/Cx2qPlWdQfNImXy5U+Th7drfT?=
=?utf-8?q?yMKxjXIpURH+Qy8uLVj7h6TA9EWG0ODzNF1fH4rYaLl9BEiFEFhGa3BigWzEhuyUK?=
=?utf-8?q?gWyn908t4Cfh?=
X-OriginatorOrg: vivo.com
X-MS-Exchange-CrossTenant-Network-Message-Id:
5d36d840-a83e-4f37-332a-08dcc286abb0
X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Aug 2024 08:44:44.3238
(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:
lCFDaTiiydVsCrklW2IykbodBFt4GLWfU90igbjW5HfU5GE+IQmeJtg9rQMf5O8af4Q2lfrcdrBVT2/pLNWM2g==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYZPR06MB6402
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 0e33d25310ec..1bad4576d492 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -102,7 +102,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;
@@ -110,7 +110,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;
@@ -218,8 +218,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);
}
@@ -344,14 +344,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;
@@ -365,7 +365,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;
@@ -375,7 +375,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;
@@ -404,7 +404,7 @@ static long udmabuf_create(struct miscdevice *device,
}
}
- kfree(folios);
+ kvfree(folios);
fput(memfd);
memfd = NULL;
}
@@ -420,8 +420,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 Thu Aug 22 08:43:35 2024
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
X-Patchwork-Submitter: Huan Yang
X-Patchwork-Id: 13773019
Received: from APC01-PSA-obe.outbound.protection.outlook.com
(mail-psaapc01on2065.outbound.protection.outlook.com [40.107.255.65])
(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 4E35517C217;
Thu, 22 Aug 2024 08:44:51 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
arc=fail smtp.client-ip=40.107.255.65
ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1724316292; cv=fail;
b=d/iYKrAhr99ztFoG9owYHsaPPy6Y/1YN6ZRvxWXyuN9Lvp7A+XW8AEfgyW/qJS/3Rf2w2XEQooRnj79SYY91v0Ni/uArLSNFI+QEpzhAxeqz3QFlkS5k9GJ11/lFjgUX6eH7sO3XBN4TRy7Ow/aae5/sB9DoaaX9FBEjqyzUw+8=
ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1724316292; c=relaxed/simple;
bh=3FEvvLx1dutsyAvFnDiBHlJREhhEOwmFEGQtnei+X1Y=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
Content-Type:MIME-Version;
b=uSubq7ChEiLhpV1MZGuGW8InkgxeOUcVjUwpVGry16gtJWR0YsCjz9t6YLv+koKGxpayDwiGsWnKntAlQsVCqvTXwdz/P2yV0ykUIIFOnDreSimW/quwNLSP0bT1Ti2jSFv1CIat1+HRb1DMC9MshMfxeZTyY43khD2QapfGIoU=
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=hGv0/goh; arc=fail smtp.client-ip=40.107.255.65
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="hGv0/goh"
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
b=IDePbvr0GUJakKWB5bUfD7/9ff/Tm0801BAoJsP2uJuNvYbfcv61mcudevBfb9TPkBmnOy/p4i0Ekmeyqn6yR1gfK4MEqYC6d/YsVVkNJIaJK6AXakvGBGrC1EsaqJ/3pB4Nndd5oLlc/GBorfVhAVuenRU2XHR7VSS7kk6O79wDEsFdhEOn7gP2PJiG1NW+7PSF7ZKxcd0Qlc9mI+TQRl8GnWtO1j68pJbeLO5dw9sGSk5Vj6HVdYsaVkbad89x2/JDuuD8Be6KuB2YlHKx6uoYnINTQskPcxFNOREeLgHol9Rhli39o895lFYHnEmJj50TpQcBKcABJwWULHYjPg==
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=Ko/aKqqMO/BFJfW8BMVb0nA4mKprdnKSjuolrgQoDFs=;
b=k0o9pPiasQFm76R1vJ1VmrgoBTBjjZoI+mWFb27ER7EiPDnby/2MFXdp5XC5kYxudIq/DMa5T8CGstIAbNTVItleUGvKkuYZ5wOQJQijLcwdpQBjKbI7gqjcR0b+PaCwbRqSzJY1JVviQj627BkOZM9Xjpx+SBjBfTfhrgH5nLDvQ8sjvpg9Z0nOBezQRzPMoJEVJFulO1qmeMV5SiI4Sl1lweoJg/2hHAPzLiXLOicVThSkCgV4B/sD4Yu2b4IAWSQFApgIHE4J62TQKbITc1klkoSlwsnkiNtp6R1rnA/2TNwI8sXW4svfP+yDYuOUqlHYa364XQAqm2Wqx9KR0A==
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=Ko/aKqqMO/BFJfW8BMVb0nA4mKprdnKSjuolrgQoDFs=;
b=hGv0/gohsJb70vLIBq0RfXFp8uuH9AYpJhKrtxe0HR/+KTGj8OYFjwO1KsN5V0cpqsagfuDJv/+QXqlWU1AQlgjtdLV2gMmfB1UtvRWXsY9jk4NS89fhDQzkrJSL8gYIC6M2p8daT6pPNcnxTZ80BMw7E3FkmgCGJsrsKl+ZPODhwVhOInRZH80eUIXCVZdB7VaEC6TwIqFVXxWerwxV3dG6Phk1ZlABIoivUPjtqOGAs61PiqR9oiqXe9gvVa//WrN5P6l7Gx1QLDVJWjazx8e8L0/gGjKRA0NjzpXS4wamUooTi1AsQpgSRAyg6AWlBrjQ6/6meXLzq/JKeIf2JA==
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=vivo.com;
Received: from PUZPR06MB5676.apcprd06.prod.outlook.com (2603:1096:301:f8::10)
by TYZPR06MB6402.apcprd06.prod.outlook.com (2603:1096:400:425::6) with
Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.27; Thu, 22 Aug
2024 08:44:46 +0000
Received: from PUZPR06MB5676.apcprd06.prod.outlook.com
([fe80::a00b:f422:ac44:636f]) by PUZPR06MB5676.apcprd06.prod.outlook.com
([fe80::a00b:f422:ac44:636f%5]) with mapi id 15.20.7875.019; Thu, 22 Aug 2024
08:44:46 +0000
From: Huan Yang
To: Sumit Semwal ,
=?utf-8?q?Christian_K=C3=B6nig?= ,
Gerd Hoffmann , linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang ,
Vivek Kasireddy
Subject: [PATCH v4 3/5] udmabuf: fix vmap_udmabuf error page set
Date: Thu, 22 Aug 2024 16:43:35 +0800
Message-ID: <20240822084342.1574914-4-link@vivo.com>
X-Mailer: git-send-email 2.45.2
In-Reply-To: <20240822084342.1574914-1-link@vivo.com>
References: <20240822084342.1574914-1-link@vivo.com>
X-ClientProxiedBy: SI1PR02CA0059.apcprd02.prod.outlook.com
(2603:1096:4:1f5::19) To PUZPR06MB5676.apcprd06.prod.outlook.com
(2603:1096:301:f8::10)
Precedence: bulk
X-Mailing-List: linux-media@vger.kernel.org
List-Id:
List-Subscribe:
List-Unsubscribe:
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: PUZPR06MB5676:EE_|TYZPR06MB6402:EE_
X-MS-Office365-Filtering-Correlation-Id: 9980c4ea-b4af-4fbb-bb0b-08dcc286ad2e
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam:
BCL:0;ARA:13230040|1800799024|366016|376014|52116014|38350700014;
X-Microsoft-Antispam-Message-Info:
SbSsRkQLYbnJHIeEg2V2L0j3EUTFtGsUhjyP8o83ykD5O9qhUyo4pw+wFYOde34J4RjG7BCc6A0pWQ1QeQNhkfF8xB3KyB6ZCKmLqtwbYKsYkH1pN6z47kZi8VlAVF0wKj3U/xrzdSOoGnOKwfUnDfzjknnzA3EZNxSNQLGdGMrmJM2U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iL65IvakHK8wjX20XvRwDnEd6HbUVU8rqji1h9i+TI2a2ZWY+o70WFrcSeImRrDE4uH17bVJDK6slehJNQMUkB4HH/S2guoGsgY6H0r+OPDDiwGyFrpqCtQWbtuJdtFoXacuN7rPSOwjnZPX5AsuHaVt2/y6l4+0EiCCZ5s+USVsSRg==
X-Forefront-Antispam-Report:
CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PUZPR06MB5676.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(52116014)(38350700014);DIR:OUT;SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
hF7lIBJJ4LK9ClnsEk8e/vf+Ygrfr5myRc8qGy1whSDZZT9apaRb9OGHhYRP90xcYehX8cg3PVrB11mlb+VtW3l59fEo8t5uGwoPCosapnEURvoWbVwAsItWDoSocpCx2e6ryTRvDW6M7gejGG5X1rGc8kEKsY+O3f3MbmEnJqts5EVmlvZMPhPHRID+LIQ+CfC6KIX8FPBurifhXbptPK2RtDMU8597eCWfP/m5aFCePXZqwZN3NXSf//0cN4BU387q0mqR2+lC5X157anN6fWDY1Dm7mMEcXCl1MbmDEUvRgF4wsC9vd25Qz7yZkUp9N6+sBBdZ69WoQwBY7a9ELJNOqYw+Y0tXJwpSrHO8S3vRBZFKBUYKxrxwR/J0HxfhA9iEmhT0ths0KZcx2GzkuPldhLixip27J2nk5tdbcRei3Ak6GIW2qPBajoEXy1NiRcnahQBaxwRiQUOwfoiwlFf7sB53dGvv7c3sLNsLB61WlhXj8TIscz4M4YNidM0SdxIrauQJbuu6GwON/qEDUXx2RUKp/XcrQ8d4mMYDAPDw7hfAjx1EaIKmBhEbUssFlBr7MtEdhk1l56//VDsOUaoyESLpgLQ7D6yvcp8N94bi8618WH+30NPv8BsX/pUJnnNcBEFkM46NChJJtyPnOkXxZSm/6AXIit8nqeH47R6UWyQMfdCzQqyV+z6S1VhKwzIYgkDFBgSbf+1jPW/bQ/UELKT0iZnxr1KbiapkEyO1xDobIWXAr2MNzjTi89BoQAIwTUxSF9Pq9XWEZmSLG6T9+x/Uz1e9RtC37sdVNe0ofvd4DYTDFaoIOdti3U2dO8d4tZh7j0Ywxt4sclpHpQcrVnJ6OWpQKU0nS2REbaQxxMCnbPTUFwZZma93Vz9MMTB0lA02b+QlUxnLDSHebplshWklKkGKdOQG0dGL6Zxv/tbuaVZdP5Qs5f6kw7Pu46cM15nRVyV9B3+cetG/IKs81EeNdL/1Y/93eXxytd109F38y+RpnoU3OvybRBcW3le7R1W1Tu0/dAMX7/Onozz3nuf+ebPJJZVbhHAS7SEg77DFaIRK5luMrpPq7kzQXAWh7Ad9TA8mqjFwePnG0HBHY7w1yeTGIFGmdH12FIMkhTv2bU2L3e4RUxtxB9H1YuRgLcKUwqvEGjMaf5G/6VdJGvboCCiMkHmTQspUpsHUjWj9UIlocSZ96X9cIPHZMBGcc/AoLHpNFiuPBc5V5RYQxDBAVkG5guc5ePGRv0OyWSS0N+GfgyDG9JqPUdtIjRpcmnKuwBDj3ufeSXTmvapZPHgBTcuMlM2/Wt8sOW8trt7gK7PFxjm8UAmh9AJT4YO/o0jLRw17RyI5IXb8M5hKO/lMkZwxdmzLhOyh0u2I0vn0Jno5xhiQZFWcBiTkWrBYtzt6mOaoIRq6OwQUB3Hi34SsXdDLsckkeS7MjMaAr7vUI2O7VZOFGnRUbFlbwnj5tA9uPB0fnC8Nq4/ITqUikADx2SISO/69b3yHzJGofk4NJRMwIckNkFRhj+fuuIMmexQAv20jEa1aqJ2ff4CfszmIig1z9Wp6M6+4tI84eURNe+vliumuCByiDzy
X-OriginatorOrg: vivo.com
X-MS-Exchange-CrossTenant-Network-Message-Id:
9980c4ea-b4af-4fbb-bb0b-08dcc286ad2e
X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Aug 2024 08:44:46.8043
(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:
4qChdGE0dQrW3WKWv0SByqj3aHIYlXoT/hkR4+7FnlNFcipUeevkh5JB4RZ9/wC7C9HMljNl/bPBRRVgad0T/w==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYZPR06MB6402
Currently vmap_udmabuf set page's array by each folio.
But, ubuf->folios is only contain's the folio's head page.
That mean we repeatedly mapped the folio head page to the vmalloc area.
Due to udmabuf can use hugetlb, if HVO enabled, tail page may not exist,
so, we can't use page array to map, instead, use pfn array.
By this, we removed page usage in udmabuf totally.
Suggested-by: Vivek Kasireddy
Signed-off-by: Huan Yang
Acked-by: Vivek Kasireddy
---
drivers/dma-buf/Kconfig | 1 +
drivers/dma-buf/udmabuf.c | 22 +++++++++++++++-------
2 files changed, 16 insertions(+), 7 deletions(-)
diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
index b46eb8a552d7..fee04fdb0822 100644
--- a/drivers/dma-buf/Kconfig
+++ b/drivers/dma-buf/Kconfig
@@ -36,6 +36,7 @@ config UDMABUF
depends on DMA_SHARED_BUFFER
depends on MEMFD_CREATE || COMPILE_TEST
depends on MMU
+ select VMAP_PFN
help
A driver to let userspace turn memfd regions into dma-bufs.
Qemu can use this to create host dmabufs for guest framebuffers.
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index 1bad4576d492..0bbc9df36c0a 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -96,21 +96,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 Thu Aug 22 08:43:36 2024
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
X-Patchwork-Submitter: Huan Yang
X-Patchwork-Id: 13773020
Received: from APC01-PSA-obe.outbound.protection.outlook.com
(mail-psaapc01on2065.outbound.protection.outlook.com [40.107.255.65])
(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 1EED617DFE8;
Thu, 22 Aug 2024 08:44:53 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
arc=fail smtp.client-ip=40.107.255.65
ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1724316294; cv=fail;
b=iHWQML9u2C57H6Qf6VkGd/HgIvgG/Myh6cFTHFS2j2aILDraWzA0vqcGe/85bkyTfbxmC6gvm5ECdWZye0MDkOEnrE2W2uSdxvHPwFNhkBM/YUs2tvz4kP6GShscegUyo6SqEuZJ5T/e8t7Oo4RCZUSkL9zK56STwmSiRARSiks=
ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1724316294; c=relaxed/simple;
bh=XHlvgGIcH5btiasSFtYweDpnmB8Fu67I3ALKUEH981c=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
Content-Type:MIME-Version;
b=Fmz1ku4HhW0JEOQImAod+NoeaWK/NewxoLKUwdzVw4ZY0MWILXY5ibps8cBavYITeWtBJNiKsj16HtzFJIY5NeSFAX7esRce4pVCI6jRkeM5qQzAl2+i+xHgRgR5qS/bGcP/7HzDLlg2EnZ+Ix9jXkRxPFg+nnZoG35JXHoacpo=
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=axLBeTRH; arc=fail smtp.client-ip=40.107.255.65
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="axLBeTRH"
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
b=GxrBc7cFz6PkPDITB9ZNCmg9jRT/ITE9ZSCjYNMFG3zUqZEgEmDdQk0e5HajHBjszKM+yZAJE9VdohlRr1SaCuIU+tJ0PPZ2BsfGGdI4NH6WS1umdMMXMXCvx5e3pJFZd4SS+ly9H0WKMIxA7SiqK/gHegNlaGn0DiJff6EAVbx76TIHDrgn0oIZmmPUKSIzulWAhA3W7Hr8IBDiMG40sunLMTCzDACec77ITkJcmsZj8BCflwezxFauSt//3pg8oUJnbV0sny3s4xTPfznBhytMPP6TxQSzWDQihNBSBr5PokjuVlR6aU5jhteTka16+hrmJ/e5qAGyhi9QRPvBig==
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=SLFgEfNid5pqAAh2hdZjcwk6nug8D9sWkT5ooDyspuk=;
b=DF/asdkz6bZ6eXbSlfgNFZTLiuM0y8grATCV28vDcPQ6Mc4rBGqUe9BsMaYFyjYZZnvEzxz5laAePj0sP9jHlL92x7tYS0VPfmaj9kuCC1TiXLpq0ZUVgwlde+r6krkg0K9g3JAIsV50m0z1XF9F+pAXo6Jcy6srbcb6mVOGUsNkYjaYlrrGutwwoM4WXoTaKqGR3ru19UGOg+6cl1LVP2kBWUYcnUBv2vp7aY7vBSAQTMEBJY4OJ5Bp4QZ9Qq1RuXYzeHGavcLg5jGN4f196J9daFjrKTs3m4hOg9AfeLVN5Dv443fJWg4MrlI78L6FgLJrJsNUpWzKP7YFMIZ6Ow==
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=SLFgEfNid5pqAAh2hdZjcwk6nug8D9sWkT5ooDyspuk=;
b=axLBeTRH21ZEYj+v/ZcLSI69/wtaxt2fmcOTOfqUg0SD3638/izS99/7pSrkGBEVzcHYYxGdQtjxaEe3FfAA0I9OwHAvp1MEXk94cFA9lhySL45/YNq+jkqq8FMe7WK3rivwy96IwaRgT75p9rXcmXRybmx+n5TK8ALG4X0yNM2Xo7v9dSaPFkTM4ys2JTI3nD8iGxiF4Xx6phBerwgZdOwDYFxN4tuU4xBOjf3+PXzZnIjcYOy3KUxq9Bh7DE3+EpSv+e+XTNgT9UR/BDqNJ/mih2toOfYoZpSMIBkGZG5XDtaJcglJJ+F1oIFtGZemGP7uzaycEZORZinxz3z1iQ==
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=vivo.com;
Received: from PUZPR06MB5676.apcprd06.prod.outlook.com (2603:1096:301:f8::10)
by TYZPR06MB6402.apcprd06.prod.outlook.com (2603:1096:400:425::6) with
Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.27; Thu, 22 Aug
2024 08:44:49 +0000
Received: from PUZPR06MB5676.apcprd06.prod.outlook.com
([fe80::a00b:f422:ac44:636f]) by PUZPR06MB5676.apcprd06.prod.outlook.com
([fe80::a00b:f422:ac44:636f%5]) with mapi id 15.20.7875.019; Thu, 22 Aug 2024
08:44:49 +0000
From: Huan Yang
To: Sumit Semwal ,
=?utf-8?q?Christian_K=C3=B6nig?= ,
Gerd Hoffmann , linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v4 4/5] udmabuf: udmabuf_create codestyle cleanup
Date: Thu, 22 Aug 2024 16:43:36 +0800
Message-ID: <20240822084342.1574914-5-link@vivo.com>
X-Mailer: git-send-email 2.45.2
In-Reply-To: <20240822084342.1574914-1-link@vivo.com>
References: <20240822084342.1574914-1-link@vivo.com>
X-ClientProxiedBy: SI1PR02CA0059.apcprd02.prod.outlook.com
(2603:1096:4:1f5::19) To PUZPR06MB5676.apcprd06.prod.outlook.com
(2603:1096:301:f8::10)
Precedence: bulk
X-Mailing-List: linux-media@vger.kernel.org
List-Id:
List-Subscribe:
List-Unsubscribe:
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: PUZPR06MB5676:EE_|TYZPR06MB6402:EE_
X-MS-Office365-Filtering-Correlation-Id: 978bb938-e095-4fe6-7b85-08dcc286ae9c
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam:
BCL:0;ARA:13230040|1800799024|366016|376014|52116014|38350700014;
X-Microsoft-Antispam-Message-Info:
xh9eE7ZgaU0YOTypFPOA5RO5s0hcRZC6p1MZkbfU2Jar0/qEnR6TZjzWnNDlatHCSuJFYtEeGP/VvX4m3ikiplBQ1mR1ZUqTEDf3fYjr/1cWYXBbkGZpUS4P5jjMkEFdFY5ET1kvlHQ1Zi76h4a9QWqYlLeYRJO/IqyslAHEqZvTpWln7jQ4Q4ed+BOJ4BS9GdtgbZ5bj92mApepwioRSU6CdFUKsSbVZgWntzEtRjcJ4QjAAc2q1PbgEzud79vCSM7V6OorS5vEPyIgK9i3ThQLf/4bBwh9402vE9VEKLLce/WpX3eI9GLcA+xjxfgdWZmvvcHwia3jabBB+8xX33sUwVqAzqSnTw4K8Mx3PYuqiKLyPvP7uvVSb5DmuDyvmLsXq3uP6KUyBPQFcZAOHBdqOPQT/BMiIklIaylY6wRBk5E1jKbApeAvMBpFYezmvzICYxWLqIOs3eqXExW33zgPBSmYzclImS3AbOvl0SZHmjU9UtyjU4rOB8CzB+Zsau+LsdZeV4wk7+Qy+jT2DkA1cVa7cynCXJ2ZlPJCC0+D53TtDJvRCBXChNzVjlalwcIu50CxbmTbKog9F0UzngBBhdYDjaq8G+UJdatW2UsAmZRbYrks/+RZ+PjStthY6lW7tFViEMbHSkXcJ4YstzTCMHPP/m2xaBSWuFC8DgfNqTj7ngXAG7OlyWrq9c+MBwy8/xSP5UFBu/1FjahUJ+4202ISnMfiC8diO+qNFZe6ZyGSQUdipqvt00FvhYULHoI59iy+Cb+8autOww0cJXaVI0+6j0nUkKlmkpbMzlDGpWv85serzqLuci4LglAUpE3ukE1w5lX3XZh4hNueGXyfepIbU9fXVM68eY7YVsx3EJD+rTGmc/jIiRAIAagptki5fN8T3JLvJMKc2ziHLH0OUXr5BfPJBF7qn89OzW+Hp2p5T/TDvWAf8odhAa5bIv4QccfLdHFvZOta2tIJfTlzetLk3T6rlowIFmx7TrDCbCWeeMw3wnHfkBB5zxuUK5POuzGLSc8ISIh/bNlea7j3YEMKPfyKzTrlxI2x9DKKj3OlsM2E3A2v0orSkkeEFSB2jO+C4SVc+RF4IMS4saYTL/MKYhXA34D3iflot7XMLUA4C0M2muheekALFiXQ0AjfmgMCsQ6fuDNEl7BKk5jvTU8t7CwYZq0PYHPAXndl06W50So48CfnLP/LFo28j0KhdJqrhqScxYD/AmmDfoQNhH30noyS0ddH0U3FeerK6C21zurJL85vv7oF89htpxtHPw9vDuuQ297g0kI/RBy2FGJsk23EoEcHehSAvnCDeDwjewlwJAd9al3icDlPH4udErV6rTJCBnRgdV6F4UaVjaUfbQ7vqDHCFBcnoT40zbxMg0ijKcrtVWDA5jQmiz4eU6UtnN9RQ3j3ALHRpw==
X-Forefront-Antispam-Report:
CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PUZPR06MB5676.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(52116014)(38350700014);DIR:OUT;SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
eXLg/F8rKdFDQ+nk84OczwlHJhQKeTUsIi049Gw1E3gM541QAEcBXjxeRQ3CHyKW6/LznaaKJFWmO/ctgAq7rWzRZ50suqwWAMFIg0zswxXOPFPpPZqCHmyDMzcuvfAj/m4Q2OuEaQOUvd43NBexEIrlX9z9z+t88jDHPewh+VltTw9+vU93gGzqyncsn1Jv0a3TlLE0gMCdqgBV8FXoJApcsNNFTusESwQ5jgta3uuWU6qmuvh53/si94AevI2mWlvadxyxlYTk1IhaFi4/6BdvgHee7nxfmWYDav3tceRmOsazrfXn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qXbs/RtkK+SsEYGtOhmsJzExrSDE8PSJG6My+fkBZuc3T+WQ1izteCwKHCrxbzferMOmyHCcQM0yIqQ7fNzBnfqVBO+yl9CDPBI2G39bVDfuRm9h9Cfp0TywDogB2dezwLB4/54RIW32vvqlEzTQZx1KeBivCWDvAnrS+ctoN4favX3HJwYJIMKxMVVigoIZAKUyrtRg2Wx42mi0mi77Lp38yOOcQ
X-OriginatorOrg: vivo.com
X-MS-Exchange-CrossTenant-Network-Message-Id:
978bb938-e095-4fe6-7b85-08dcc286ae9c
X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Aug 2024 08:44:49.2046
(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:
oarDdgCnLLu1bD0YNdAgpPGtiN5V7zz7B0iUSxDUTG5R+/2H6xOuZgBM2/gEIBX9BNqLVEb6aS7vL/9feVYqYg==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYZPR06MB6402
There are some variables in udmabuf_create that are only used inside the
loop. Therefore, there is no need to declare them outside the scope.
This patch moved it into loop.
It is difficult to understand the loop condition of the code that adds
folio to the unpin_list.
This patch move item folio pin and record into a single function, when
pinned success, the outer loop of this patch iterates through folios,
while the inner loop correctly sets the folio and corresponding offset
into the udmabuf starting from the offset. if reach to pgcnt or nr_folios,
end of loop.
If item size is huge, folios may use vmalloc to get memory, which can't
cache but return into pcp(or buddy) when vfree. So, each pin may waste
some time in folios array alloc.
This patch also reuse of folios when iter create head, just use max size
of item.
Signed-off-by: Huan Yang
---
drivers/dma-buf/udmabuf.c | 165 +++++++++++++++++++++++---------------
1 file changed, 101 insertions(+), 64 deletions(-)
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index 0bbc9df36c0a..eb55bb4a5fcc 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -321,17 +321,87 @@ static int export_udmabuf(struct udmabuf *ubuf,
return dma_buf_fd(buf, flags);
}
+static int __udmabuf_pin_list_folios(struct udmabuf_create_item *item,
+ struct udmabuf *ubuf,
+ struct folio **folios)
+{
+ struct file *memfd = NULL;
+ pgoff_t pgoff, ipgcnt, upgcnt = ubuf->pagecount;
+ u32 cur_folio, cur_pgcnt;
+ struct folio **ubuf_folios;
+ pgoff_t *ubuf_offsets;
+ long nr_folios;
+ loff_t end, start;
+ int ret;
+
+ memfd = fget(item->memfd);
+ ret = check_memfd_seals(memfd);
+ if (ret < 0)
+ goto err;
+
+ start = item->offset;
+ ipgcnt = item->size >> PAGE_SHIFT;
+ end = start + (ipgcnt << PAGE_SHIFT) - 1;
+
+ nr_folios = memfd_pin_folios(memfd, start, end, folios, ipgcnt, &pgoff);
+ if (nr_folios <= 0) {
+ kvfree(folios);
+ ret = nr_folios ? nr_folios : -EINVAL;
+ goto err;
+ }
+
+ cur_pgcnt = 0;
+ ubuf_folios = ubuf->folios;
+ ubuf_offsets = ubuf->offsets;
+
+ for (cur_folio = 0; cur_folio < nr_folios; ++cur_folio) {
+ pgoff_t subpgoff = pgoff;
+ long fsize = folio_size(folios[cur_folio]);
+
+ ret = add_to_unpin_list(&ubuf->unpin_list, folios[cur_folio]);
+ if (ret < 0) {
+ kfree(folios);
+ goto err;
+ }
+
+ for (; subpgoff < fsize; subpgoff += PAGE_SIZE) {
+ ubuf->folios[upgcnt] = folios[cur_folio];
+ ubuf->offsets[upgcnt] = subpgoff;
+ ++upgcnt;
+
+ if (++cur_pgcnt >= ipgcnt)
+ goto end;
+ }
+
+ /**
+ * Only first folio in item may start from offset,
+ * so remain folio start from 0.
+ */
+ pgoff = 0;
+ }
+end:
+ ubuf->pagecount = upgcnt;
+ fput(memfd);
+
+ return 0;
+
+err:
+ ubuf->pagecount = upgcnt;
+ if (memfd)
+ fput(memfd);
+
+ 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, max_ipgcnt = 0;
+ long ret = -EINVAL;
struct udmabuf *ubuf;
- u32 i, j, k, flags;
- loff_t end;
+ struct folio **folios = NULL;
+ u32 i, flags;
ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL);
if (!ubuf)
@@ -340,82 +410,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))
+ pgoff_t itempgcnt;
+
+ 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)
+
+ itempgcnt = list[i].size >> PAGE_SHIFT;
+ pgcnt += itempgcnt;
+
+ if (pgcnt > pglimit)
goto err;
+
+ max_ipgcnt = max_t(unsigned long, itempgcnt, max_ipgcnt);
}
- if (!ubuf->pagecount)
+ if (!pgcnt)
goto err;
- ubuf->folios = kvmalloc_array(ubuf->pagecount, sizeof(*ubuf->folios),
+ 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;
-
- pgcnt = list[i].size >> PAGE_SHIFT;
- folios = kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
- if (!folios) {
- ret = -ENOMEM;
- goto err;
- }
+ folios = kvmalloc_array(max_ipgcnt, sizeof(*folios), GFP_KERNEL);
+ if (!folios) {
+ ret = -ENOMEM;
+ goto err;
+ }
- end = list[i].offset + (pgcnt << PAGE_SHIFT) - 1;
- ret = memfd_pin_folios(memfd, list[i].offset, end,
- folios, pgcnt, &pgoff);
- if (ret <= 0) {
- kvfree(folios);
- if (!ret)
- ret = -EINVAL;
+ for (i = 0; i < head->count; i++) {
+ ret = __udmabuf_pin_list_folios(&list[i], ubuf, folios);
+ if (ret)
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);
- fput(memfd);
- memfd = NULL;
}
+ kvfree(folios);
flags = head->flags & UDMABUF_FLAGS_CLOEXEC ? O_CLOEXEC : 0;
ret = export_udmabuf(ubuf, device, flags);
@@ -425,9 +463,8 @@ static long udmabuf_create(struct miscdevice *device,
return ret;
err:
- if (memfd)
- fput(memfd);
unpin_all_folios(&ubuf->unpin_list);
+ kvfree(folios);
kvfree(ubuf->offsets);
kvfree(ubuf->folios);
kfree(ubuf);
From patchwork Thu Aug 22 08:43:37 2024
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
X-Patchwork-Submitter: Huan Yang
X-Patchwork-Id: 13773021
Received: from APC01-PSA-obe.outbound.protection.outlook.com
(mail-psaapc01on2065.outbound.protection.outlook.com [40.107.255.65])
(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 25D0017E918;
Thu, 22 Aug 2024 08:44:55 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
arc=fail smtp.client-ip=40.107.255.65
ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1724316296; cv=fail;
b=Zpkvol9iweYWDd5Czp/dPmiAm3lFFJFAxT+xc9aHR31K+S6K8L3IDQ72weEbSd3V04b0Ugbz04BGsqSZQKGorBXEx06a/p246R8emVkrE5bG5mHe1rUiJqXiqUZmCF1f1mm/KGscZwypQar+gWGvMLWnxScoWJZhUi36NT04oQg=
ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1724316296; c=relaxed/simple;
bh=yyxoXjgryW8g8ejlcqwBXZKK+tBkqunHrEtLN2Kknd4=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
Content-Type:MIME-Version;
b=URf68wSXHTZRklKnv1s4LPP38qqn33igRyM25bPrZPri6af77leTD/vJgmgYax2oz6c6beq4Qc4os1dcsHczYHC6z/3uJ+XgRXOepScvANOEKFGcoDX8wp/pI3dVmlgwrJFMAdT8EUHTsw4ZFOMO1PvZVVAUWaaBLE96frpyu74=
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=q18FGH1H; arc=fail smtp.client-ip=40.107.255.65
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="q18FGH1H"
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
b=Rm58CO9/tApObrNcpf2VstrtudbfJ2AMybkOeoCzcJh5oc802S9ZwELSoSSocYI1KKxKNaxGVKcM6T7j7M+Q8gkOB45I63Cbohn0LMJbkKDb/BSQYGbwXDKr6mviYh2H1XtROuxRybw6Gh+vCCcN7h4tpqiZqhbR9WX4QTNesd0CHTEITgjbNn2D7KdYUlsfCO4mj53wMUA08HUTrDIe+2qiD7aL0FUGO6aFNRQq2IFiGmBoNyjHxHlwmuX5DPUQ+M4vZ0U+fgFPUYvCzw95emkaS0l0DYXs7dgWkEEeiCpdgbJ0KR3+73dXis9L+ahPJSVMeF6G1sTDA916AkdEgA==
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=p/GUYS9uhhT8EiUqZYlaKs2ODEKxLXBvtKncVnYF1Es=;
b=PLdmcK3ugsie8ewYKl7JHvdRbogDSpAyVZSRHbqcG4yK2Fybi9vVds+ufQRlF/kDUEZ87dVP2bZhf/R5xvjs91Jx7MbGCcDdBtJV+ewoW+hPzctmWYtW9+1Gh+erGohHgMFaWp2e0ICdxxUYTz8crvTaobKKYV7HvstSKbbcukI1ezNKzgXMBlPISzjmKB6q4cBgSr+rpvqC6q2q7fmRVYEbc88C2Sx8iTn5L5NcQaASsvpqROUnMh+2rCoHLCMhNF6hRKeC1YlZN7DElFRCpDIuHN2xF6WuyO9+yZ8R3UN9Rd5FBrQOhw8mf6gQMT4tW41Nn43pbW2w9aASG8R5Mw==
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=p/GUYS9uhhT8EiUqZYlaKs2ODEKxLXBvtKncVnYF1Es=;
b=q18FGH1H2+1EE6Vg6XDHMNN4C4gCqDjPObdfshkQZBPKUALQTeBKgBi62KnR8dyDL4885DsbbVmPj7syX8qz011MyD8B9zhzh1PtYw1gJTSm6MBp9jhuuVip5Dkcy2Pkcg4AUBnaPGgZoWb3SZj8vdHPbS9ubpPTLJXlbkLWiha0pxuBWdzeZ2887M0lfjEFe1jAzDhYqPdLFFVPWGjxuf6QUBXm1bgh3SI4MBwGt3SLTn5iW5I3IYfS7gaxUst8AjA4wSYpYaSyH663ynAuGzTi5DLWKltWV00XiAFWzbCBdhVuFJkdKA1fQlxc1nDYCPfJTty1Tk0BngFtc5ANMw==
Authentication-Results: dkim=none (message not signed)
header.d=none;dmarc=none action=none header.from=vivo.com;
Received: from PUZPR06MB5676.apcprd06.prod.outlook.com (2603:1096:301:f8::10)
by TYZPR06MB6402.apcprd06.prod.outlook.com (2603:1096:400:425::6) with
Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.27; Thu, 22 Aug
2024 08:44:51 +0000
Received: from PUZPR06MB5676.apcprd06.prod.outlook.com
([fe80::a00b:f422:ac44:636f]) by PUZPR06MB5676.apcprd06.prod.outlook.com
([fe80::a00b:f422:ac44:636f%5]) with mapi id 15.20.7875.019; Thu, 22 Aug 2024
08:44:51 +0000
From: Huan Yang
To: Sumit Semwal ,
=?utf-8?q?Christian_K=C3=B6nig?= ,
Gerd Hoffmann , linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v4 5/5] udmabuf: remove udmabuf_folio
Date: Thu, 22 Aug 2024 16:43:37 +0800
Message-ID: <20240822084342.1574914-6-link@vivo.com>
X-Mailer: git-send-email 2.45.2
In-Reply-To: <20240822084342.1574914-1-link@vivo.com>
References: <20240822084342.1574914-1-link@vivo.com>
X-ClientProxiedBy: SI1PR02CA0059.apcprd02.prod.outlook.com
(2603:1096:4:1f5::19) To PUZPR06MB5676.apcprd06.prod.outlook.com
(2603:1096:301:f8::10)
Precedence: bulk
X-Mailing-List: linux-media@vger.kernel.org
List-Id:
List-Subscribe:
List-Unsubscribe:
MIME-Version: 1.0
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic: PUZPR06MB5676:EE_|TYZPR06MB6402:EE_
X-MS-Office365-Filtering-Correlation-Id: 489ea39e-c09d-44a1-c097-08dcc286b009
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam:
BCL:0;ARA:13230040|1800799024|366016|376014|52116014|38350700014;
X-Microsoft-Antispam-Message-Info:
6u/I1z7xb5OhKvsDto3tPFjd1pOXM/B0POSWuflf9vCgqw2lQHP36nRUBFhFic0ABu9Ugur8mMA461t3QiYc/w5jnJ+wNjuvrR7AJrNbpr+/oaOUCyRcOYsZcDY/3JMQo2VbSMTacCvw8C8JeJ9AIExPHR4fgxb/0su21hn83EjkVdG/1fCw3HnWOPfg4wUIU0bGJSqAanXhqitLaSQVHv5EUOV/PhG3ETXv4ovNXxXjaW8zyNf+vxg4GIYsQ+/n0QSM9tKvpDrzxn3+BF9Bu34VjOv43Tsd2tQNIHAeiIrTsrbu1W3STV+10IFzO6cKFVqwFEXKyQx5t62Qte0v0ZRG7+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wXEg==
X-Forefront-Antispam-Report:
CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PUZPR06MB5676.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(52116014)(38350700014);DIR:OUT;SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
+3iycOLFw0lrmgduuN/HOFaFXTLUjXMDyCMjR5e/MdLbN7ZHYjoxlIDIUREzUNvy8U2Fu+XJZT3UwWTT67UTGXxbkS/Je4cSE/2aqYaqaDOst9zmISB2trSswjMEoVzJPNirx3UIM7m65fH5N9G62bD75O2iUl21Q7Ze6I1U9tgpkA7Z/mq4R9tFhoco3Dfel6zZWokw5rYnxs6bInOsmCXbBNFyoSFk3gHx+nA0z2kdc3CCcB5ZX2vmY3UQDimWo9DEGa1mAmx5c3Xu75LS2fo98dQsnVe+Ya+ADo32eX7ve47LkaGCcHZ0Aj8xTAFa3egBOemhNnlklnLoHGCqYDCcKPIHhAOtq6P6DtLBZGesev4HDz5ksNBOASUBD0YxbxHuI0Fp7J+xL/2LBDO9XXBYleEkkIat0UAnY1cIZchgTgPeFC9SNdBHJhBtxYXgAO5ARFRdmJcNH6Gn3XD7MFyRJ7iglVLHMXqrtpbnHoRJEj6vBD18A69cA85At4XjBp6tru9V39c5UYvqNl+5wFQgv1wZ8opDsoTKw5gTNDT2Z6TdkycKFMaJTa9p8xSR1u2ZVrXshFERvPd2yL5Ms6OPjWydbppavqumxq9VnwAjJWAKsMwNkeVm3Oe76b8RLGeZbG0ZdtmWXaW/NxOFwfzHOjhXxvYVHr35ndy/xgLECo7V2G7Jk5kHYffn3oVfll8+pTLwzmULk5MKcVg4ZFQWB6l9acj7rhlCeLaZiQuFNty1m9yp+HLys4+HYT50ECq9BjpumVmGcnU35L2WUMkW3V3VZfTj1PHXqT8vNEqfBhleGGDfQCHAuj+Wo0/s35CbSIa9Cwz6rjmprT2M9Ic/LK1mXTUWaFSRF8wRJDJZfURiyBANGzxeS4VPRQv1B5km/Sa9ZihHwoFibZRZvnj6QpkUQrSDNqP5NtlJ2euJSRT+jsBpmf2NnDpJh8C1iC1txSiZPRypUbZNzCaL6ezRFz05zCkNWlP/dvR69EKvll4MRpYon9VbV3qaZ7eScmBxFz1S/mL1FXPsUcFylQwfzYN5KQ7OtRKfFM3p/KgIXWtAEl5p8gm789Hvn4Hd+0GgKJOmrMR93XeY772O8/PpxiBQsPiXmH1vPOJlnctBsOyUTyRijGbTv2Sp+eOh8oIkKX0O6wZOWQeSEFuSh7dJhVIFPiwvbt12Pee9T3LDH/rLbv/8wAV7uCVPtKCp5jvdR5FFN5aZBtP+LG5AJiSF8XGDKJFIxLmjuAkBD/FekYB81FTFqKeT+KApb7W/rWzzumMdhJyB7q50M3zVaVIP9heTkLvEhbZZ2Yc1DZv6Cnh+kER/iIy7WTDQ3kj8iQEaSMmqC1+GMwK/fb4DUy33QVat1xjf9rqlQFL6CU3WREmL3wk3OK6qbdd3S8G56ZfPnoaNd1qnwDxu9qrW+0X6xMjbJIhopB7K+1UR6YjWw8miiCW+/c2zz/Cu8rjEeioaioslLoo1hnuA7IuMMiLLGCYDXIFpNm4fnrA4PAXif8jSbYV6EMy1A3educg8ZPsaP0QEAoqZlUCJhXSNE95BpoVkB0tmmxz7oilFud2RQXDWL5BhBMhTtFpwR+5+
X-OriginatorOrg: vivo.com
X-MS-Exchange-CrossTenant-Network-Message-Id:
489ea39e-c09d-44a1-c097-08dcc286b009
X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Aug 2024 08:44:51.6037
(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:
jINC/2t7b2tobQEnX2S4z01JaP5Myv/WirH+BIVhTRGRjGIgjTEjam5b7uhZYkjhXd8V7qJ/HQ0nv1TKttnksg==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYZPR06MB6402
Currently, udmabuf handles folio by creating an unpin list to record
each folio obtained from the list and unpinning them when released. To
maintain this approach, many data structures have been established.
However, maintaining this type of data structure requires a significant
amount of memory and traversing the list is a substantial overhead,
which is not friendly to the CPU cache.
We actually don't need to use unpin_list to unpin during release.
Instead, just use a folios array record each folio is ok.
Compare udmabuf_folio 24 byte, folio array is 8 byte. Even if array need
to be pgcnt*8, may waste some memory when use large folio.
The access of array is faster than list, also, if 4K, array can also
save memory than list.
Signed-off-by: Huan Yang
---
drivers/dma-buf/udmabuf.c | 80 ++++++++++++++++++---------------------
1 file changed, 37 insertions(+), 43 deletions(-)
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index eb55bb4a5fcc..a45cec1f82b3 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)
@@ -189,32 +195,12 @@ 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)
-{
- struct udmabuf_folio *ubuf_folio;
-
- while (!list_empty(unpin_list)) {
- ubuf_folio = list_first_entry(unpin_list,
- struct udmabuf_folio, list);
- unpin_folio(ubuf_folio->folio);
-
- list_del(&ubuf_folio->list);
- kfree(ubuf_folio);
- }
-}
-
-static int add_to_unpin_list(struct list_head *unpin_list,
- struct folio *folio)
+static __always_inline void unpin_all_folios(struct udmabuf *ubuf)
{
- struct udmabuf_folio *ubuf_folio;
-
- ubuf_folio = kzalloc(sizeof(*ubuf_folio), GFP_KERNEL);
- if (!ubuf_folio)
- return -ENOMEM;
+ pgoff_t i;
- ubuf_folio->folio = folio;
- list_add_tail(&ubuf_folio->list, unpin_list);
- return 0;
+ for (i = 0; i < ubuf->nr_pinned; ++i)
+ unpin_folio(ubuf->pinned_folios[i]);
}
static void release_udmabuf(struct dma_buf *buf)
@@ -225,7 +211,8 @@ 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);
+ unpin_all_folios(ubuf);
+ kvfree(ubuf->pinned_folios);
kvfree(ubuf->offsets);
kvfree(ubuf->folios);
kfree(ubuf);
@@ -326,9 +313,9 @@ static int __udmabuf_pin_list_folios(struct udmabuf_create_item *item,
struct folio **folios)
{
struct file *memfd = NULL;
- pgoff_t pgoff, ipgcnt, upgcnt = ubuf->pagecount;
+ pgoff_t pgoff, ipgcnt, upgcnt, nr_pinned;
u32 cur_folio, cur_pgcnt;
- struct folio **ubuf_folios;
+ struct folio **ubuf_folios, **pinned_folios;
pgoff_t *ubuf_offsets;
long nr_folios;
loff_t end, start;
@@ -351,22 +338,21 @@ static int __udmabuf_pin_list_folios(struct udmabuf_create_item *item,
}
cur_pgcnt = 0;
+ nr_pinned = ubuf->nr_pinned;
+ upgcnt = ubuf->pagecount;
ubuf_folios = ubuf->folios;
ubuf_offsets = ubuf->offsets;
+ pinned_folios = ubuf->pinned_folios;
for (cur_folio = 0; cur_folio < nr_folios; ++cur_folio) {
pgoff_t subpgoff = pgoff;
long fsize = folio_size(folios[cur_folio]);
- ret = add_to_unpin_list(&ubuf->unpin_list, folios[cur_folio]);
- if (ret < 0) {
- kfree(folios);
- goto err;
- }
+ pinned_folios[nr_pinned++] = folios[cur_folio];
for (; subpgoff < fsize; subpgoff += PAGE_SIZE) {
- ubuf->folios[upgcnt] = folios[cur_folio];
- ubuf->offsets[upgcnt] = subpgoff;
+ ubuf_folios[upgcnt] = folios[cur_folio];
+ ubuf_offsets[upgcnt] = subpgoff;
++upgcnt;
if (++cur_pgcnt >= ipgcnt)
@@ -381,12 +367,12 @@ static int __udmabuf_pin_list_folios(struct udmabuf_create_item *item,
}
end:
ubuf->pagecount = upgcnt;
+ ubuf->nr_pinned = nr_pinned;
fput(memfd);
return 0;
err:
- ubuf->pagecount = upgcnt;
if (memfd)
fput(memfd);
@@ -407,7 +393,6 @@ 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++) {
pgoff_t itempgcnt;
@@ -442,6 +427,14 @@ static long udmabuf_create(struct miscdevice *device,
goto err;
}
+ ubuf->pinned_folios = kvmalloc_array(pgcnt,
+ sizeof(*ubuf->pinned_folios),
+ GFP_KERNEL);
+ if (!ubuf->pinned_folios) {
+ ret = -ENOMEM;
+ goto err;
+ }
+
folios = kvmalloc_array(max_ipgcnt, sizeof(*folios), GFP_KERNEL);
if (!folios) {
ret = -ENOMEM;
@@ -463,8 +456,9 @@ static long udmabuf_create(struct miscdevice *device,
return ret;
err:
- unpin_all_folios(&ubuf->unpin_list);
+ unpin_all_folios(ubuf);
kvfree(folios);
+ kvfree(ubuf->pinned_folios);
kvfree(ubuf->offsets);
kvfree(ubuf->folios);
kfree(ubuf);