From patchwork Thu Jul 25 02:13:49 2024
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
X-Patchwork-Submitter: Huan Yang
X-Patchwork-Id: 13741519
Received: from APC01-PSA-obe.outbound.protection.outlook.com
(mail-psaapc01on2048.outbound.protection.outlook.com [40.107.255.48])
(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 E1E361876;
Thu, 25 Jul 2024 02:14:22 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
arc=fail smtp.client-ip=40.107.255.48
ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1721873665; cv=fail;
b=s5AGg1EM9uGAfAuuSw071HhOsjEBhJHvqaGKG6D7WLjixukxgPdswOf5Ee1WEscPfPTEQja6b+KbMEkKUlVCpEK4xp+Dnd0Xw7IKoUCTd6T0LZX2r5DrHu7Pq2uI3V/DovCufquZKBcCEUZHtZM3miKGIm2BuN17XzILewhhY+g=
ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1721873665; c=relaxed/simple;
bh=U7sPaSKxBl9AsrVVSHwUjLHpO0kZSstO66xhdqbUd7E=;
h=From:To:Cc:Subject:Date:Message-ID:Content-Type:MIME-Version;
b=Rp9VQn3MpBSY5bwHJsrJAw83+Ohob2HOaFv/Dut36YYviiUamzaA7+mG6cHl+3zxbnLHspYKn/8JHOjV4esQzpOmGGVhBKgkgwilvp9vFPnTdKrC/G/sQkIp9Tf8jkXMslqfYs8mIrcAwf14/xqSBEVGLnEqLviO8tJCOuoWXZg=
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=Te5BZbJf; arc=fail smtp.client-ip=40.107.255.48
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="Te5BZbJf"
ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none;
b=r/JwbUDGPG7WJuD2GiQ/VroT5tjLanw2d+4JdUeme0bG1ljyf7eOTpcm+iPFswwG4Q5Xd5FoVTRRQLsrJ+LgEKBrnHbCkl3bjQZvQ8wkDiOIBrf5nFp2hhg6mhhqNS4XhkRaf4davaapVkeFWIJ2JSw9kBPx2YhiY10yUrX7oF7TsUnGo9+jwYYrK1hwU9sJvNDZnnlq/6GNuVJn2r9RiNK49G4s7lCbTF2puAhl0CTx5tbM+HiJizjDOY43M/iPi+1Nf8kOavp5EBS0jjk0cTufKXb6O/kPUL62+f9TIIKM+ZnC1x4EhQiYq4Cyvb+JullXmLAZYY8+AV0IHK2SMg==
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=WU8I3XPlulrftvQutKfwAcNZnYGqc/K0uhAMsDyn3cM=;
b=B10V5C8MIcCHrmQT/W2WwlEwmCJVdAemZOOR//Ifubvvz5DZ+Z8s+52Agbwux9o3wwUqxl3DB/hhXZT4hxjSpuK5Z/U5ddbbqMZKcUuMbKv0EQm4Xza76UbsgXWN7Ie+eSqA+lL+d8azh4dRbGEb8O8bjQFc0vIPS3Fqj3TMUUptbG83fX9oCALnKoCz/OTOFRkzIkg97CgHfxS9d6NXGJZNT1o7+oMOWGJmHKL9a6OfM7/1NWdk7PgdqwZ7K+D1Zbphj+0DmzXahn4Ev64LmISSDkbaraDACgjqbzRvXp+YeZ4FcCFrJCa5106FpBllpmL5l4F/s070ptJ269UiUA==
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=WU8I3XPlulrftvQutKfwAcNZnYGqc/K0uhAMsDyn3cM=;
b=Te5BZbJfAWDS4/3VnZoodMDhaVkjbWYuNgbA5UAOZdXWH6lZ2qqFSUAN1i3wYQvvPERXRGwOyB2xANULkQZXlAzarto2R41teJILwb4+5c1Zo87847WYl1rxj0Zygd8yYquUA7OTxpqfe3icakDd2ZouYWWXi6VlfTfyurP6V4/6jysNX8FOiII3UC8JJQ00eTdAjEFhk8I/a/fleW8gXIeBv40pC+rZ3hzjbv1XXGpucwOplJdvh2DK7GO8SdEXlDd88UkH9AJJsM0ra5FzgsGvkwgCIIxtHq0aguOyz9bq7UArmaQt62ZoVjIzNuCt0mFes9SdLbN3KP01hv9xCQ==
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 PUZPR06MB6104.apcprd06.prod.outlook.com (2603:1096:301:118::12) with
Microsoft SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7784.18; Thu, 25 Jul
2024 02:14:16 +0000
Received: from PUZPR06MB5676.apcprd06.prod.outlook.com
([fe80::a00b:f422:ac44:636f]) by PUZPR06MB5676.apcprd06.prod.outlook.com
([fe80::a00b:f422:ac44:636f%6]) with mapi id 15.20.7762.027; Thu, 25 Jul 2024
02:14:15 +0000
From: Huan Yang
To: Gerd Hoffmann , Sumit Semwal ,
=?utf-8?q?Christian_K=C3=B6nig?= ,
dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org,
linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com,
Huan Yang
Subject: [PATCH v2] udmabuf: array alloc use kvmalloc
Date: Thu, 25 Jul 2024 10:13:49 +0800
Message-ID: <20240725021349.580574-1-link@vivo.com>
X-Mailer: git-send-email 2.45.2
X-ClientProxiedBy: SG2P153CA0010.APCP153.PROD.OUTLOOK.COM (2603:1096::20) 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_|PUZPR06MB6104:EE_
X-MS-Office365-Filtering-Correlation-Id: 1eefeb49-4e5a-49b5-13cf-08dcac4f7b75
X-MS-Exchange-SenderADCheck: 1
X-MS-Exchange-AntiSpam-Relay: 0
X-Microsoft-Antispam:
BCL:0;ARA:13230040|52116014|376014|366016|1800799024|38350700014;
X-Microsoft-Antispam-Message-Info:
lz+4VP+vjfLhFT7QSkT1UByM8c2ZUi0uaiRUFt+7WYdavBNoaiqeQ9wt25cIG91DLg0BF4/PkWEfwi+/DEb/YdbU5OCK9NQZi4xO0ZXcRlCproTFHWrG7fzpZwUdQPM9s8uXKbA02K52ny2BMAVuGiuL4sw/E/6I39PfWq5xAXVwHm74IkV/8nodUSh58yN0ZBjLufrEbLZLwmum9uL2QLxf+sAMVvEUGa2qKmInelCoPtpB1zMzlD9WlEXoWwDgrGwVvQ8pOdXtW805mp6byYgCAZktX0wWQ46Z1jT4u8s/BIjMlo3uFAwp342anM9k5x0ObehwOMcjtgSOlZGeQ2LNT7dUMBXFdWLKSz484ITMBagzkJJw1obL/Wz3Dq/xO6vDI5an6zRHXT/XEccTjudru7XeIdBfx//DUkE9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PUZPR06MB5676.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(52116014)(376014)(366016)(1800799024)(38350700014);DIR:OUT;SFP:1101;
X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1
X-MS-Exchange-AntiSpam-MessageData-0:
QiwM6FdxAe6bmaBarMRQp+gIepU9oiC8EMz4O3ILQ0e0cO2UF1smCTLZMuWJoSHNxzIcG1pcJONnKdJD+doJK5t4FKYm6Vh2L6+nA0rVXBAU1XXsVCGH2Yqh5z/DkcnaQfUBHfY7t7lobflKbNyhBiOI3nMnsljz5dZazduSgtBs55yRFcf+b0G3D+tDJNO6ziSGmlmJR3MWm89xM2f8UkQ4xcT/1FZzeEp2OYi1SqMuLQtlN9DVIm0A9a7UhoKt+P2uR1JZWQUcKrFocX05OKTSe3wHOGCchbAhJsIeomhFJl1VmM5E0Pk35NSGcWiZmT3OD4O7GY0GnBELed8qcDzE7tlyb5Ji+PsDmKeZkDaJo8eRlqv1H5k89pdme2kJS7NuxKnkNtHcOOEKOg6o8MgMf7OWNufaOUV1tcFltK+jpcUJFTceJM8kxo2CjBwH3b3+W1a67Ru+Ia/WOjv7XcWi1ZtmoFX5zwScCsGaSwe8vae/197EKwPYE+a/jnto3iuXs2T2sPBWiftw8PrY9NexNx58LWnoZVNH6qDw4LgIJAUUPhSM4DEHFK6iYAZd2gZgeAwOtpo/hWiqfKZQFffKlUHxXV7w0j4FN6Ze5sWJpNngLrh59NwHZV7SDn5FdUH2+fPO2bOXP56f4Yoi+AnwcJvxXPt9wqUkYpx2NvH/ICPlQUw9yGTEdeNNpaIsuvmCWqf4q6rDh0LzyS/AVp5p0NySusLDoAb1cn583krJk1itvpJbvsjoLwlsfkwVDsVuuWorRqYVPeTENb9Zsk3G40IuioQl1TTPk2CreVi4l1v6VcKsGG6d+CYDEpgkZxkiRQnZq2pmAtv/2kWNPnkzT+fRj5+/Ev/rFivbR5a54KK1iENZE4XmiegIoh8RGa04SlyqKf7UpIa8nByiHRhG4pz5ocQ/eYN1+hAcwf2KBvwXQgl2anAEmF89LYIW0Iyo5DvAtKLRJIUErB/ReXnsPhBC2jrjMOzY5uzzvkAY9/5PZvXyfBVp0YlmjHWfUE0OsaItF24Xax5SLdmBBbwN4Q6C336eYo9NZmfAt4Ydvpp8OI09aQccFnwScVoVFIw9dh4jWSI0LjaoVTGGMiUq2cxXmOH0Y9eDer+F0ZwauErI80LTr/RuF/3jAEPW+lctEBd917SiMD1Z7E9qakeePiouFqjFSLGxLeKjlIC2H6ON4888FW4/njQv6/D6NRaEExWYneiujrwyngU0BdgmVNsLm9yoLmHGyBVuuBIpjJa486iUKVjZXq1K4LRaGXk28LhtXMD5DCVyQCHLWEOtvwXeQybzw6b0xhzWLh+Wmrdo425wuDgHrhl5wHyPVRXF4jnMk+Lv/94lpUZN2JHVFdzZYWjaooTQ5d0eFDNjGodKlaHKJ56UCdz3dhI/LWG8OcCHn9rPbS3BmKGeV/2l23IXwg1ZBIfUk8BW+Axtb1UB/jAZW8B5I23N4/D7oE1+W70SD9Qk68VnCnPLpynM4/Qn0KOOpdcDffxz50T13bppPjVaPKgBvnSoCEvblHCsTG4gaaUZl84iu+80UBK/n2PPnMsTVGu6seEnLzsRZrD9lH2ZvV27SPIddpTE
X-OriginatorOrg: vivo.com
X-MS-Exchange-CrossTenant-Network-Message-Id:
1eefeb49-4e5a-49b5-13cf-08dcac4f7b75
X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Internal
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Jul 2024 02:14:15.5424
(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:
cvB0z9hZdqyt68OG2JgPuiYYr/gvUN999fja1rjQyReQk0pJgeMCI0T5FxIrJHuftlbUSxIW7TfoenmlqCvOoA==
X-MS-Exchange-Transport-CrossTenantHeadersStamped: PUZPR06MB6104
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
---
Changelog:
v2 -> v1: rebase, change offset and mempin folio array use kvmalloc,
change description.
drivers/dma-buf/udmabuf.c | 24 ++++++++++++------------
1 file changed, 12 insertions(+), 12 deletions(-)
base-commit: 2347b4c79f5e6cd3f4996e80c2d3c15f53006bf5
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index 047c3cd2ceff..7addb1720bc3 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -80,7 +80,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;
@@ -88,7 +88,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;
@@ -196,8 +196,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);
}
@@ -322,13 +322,13 @@ static long udmabuf_create(struct miscdevice *device,
if (!ubuf->pagecount)
goto err;
- ubuf->folios = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->folios),
+ 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),
+ ubuf->offsets = kvcalloc(ubuf->pagecount, sizeof(*ubuf->offsets),
GFP_KERNEL);
if (!ubuf->offsets) {
ret = -ENOMEM;
@@ -343,7 +343,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;
@@ -353,7 +353,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;
@@ -369,7 +369,7 @@ static long udmabuf_create(struct miscdevice *device,
ret = add_to_unpin_list(&ubuf->unpin_list,
folios[k]);
if (ret < 0) {
- kfree(folios);
+ kvfree(folios);
goto err;
}
}
@@ -382,7 +382,7 @@ static long udmabuf_create(struct miscdevice *device,
}
}
- kfree(folios);
+ kvfree(folios);
fput(memfd);
memfd = NULL;
}
@@ -398,8 +398,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;
}