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?q?cs0YfXJLpahWtdoRXaQ+txbrklx/is13B6D55/rb2DxTvNTBWHliUpzzJCJv8KpYP?= =?utf-8?q?RhqOigsJ+QZGoz2hh1BmVvSzACEY7Ci1SfgV6yXTpi661Qmaih0WRYX8ML1SiO8V/?= =?utf-8?q?mje4JIMTbzALdfT4KlPB3RE1E4mc0PwfIc3M6kmodFojgeD06Dn1JsY1JsvX2cLdf?= =?utf-8?q?D3K+mydJ3GrypHHfanuKqQNmEqIyqKOOufCxBy3NsQ9m2Z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J/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/qsHWf4sMjFrcSLQtWWiHOQTi6chmaBJ6m4zuX?= =?utf-8?q?rO7v06yETP7/Vwo5uHyZ4VVAJxn9PX91Vz0xrbPDe9fBg2zJF2s0ZR2R5YQZDw/S5?= =?utf-8?q?s+davGLGSuUfBOt1D0/aqlP8MUT9U6il7sGF076RhNeIse4tPFHGNtbQ8tcEqFC+0?= =?utf-8?q?X/Vg2IG8oh9KG1+XrVMTKJErwz1JZ2DJxwPza9eUNnXND9Hu8onkzhuQZttvI3uGC?= =?utf-8?q?HIkbx6ty5RV93W22gRCA5Q3xiKuJr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STrVCfUSdPRp+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; }