From patchwork Mon Aug 5 03:25:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 13753099 Received: from APC01-SG2-obe.outbound.protection.outlook.com (mail-sgaapc01on2060.outbound.protection.outlook.com [40.107.215.60]) (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 D98A01422BD; Mon, 5 Aug 2024 03:26:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.215.60 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722828382; cv=fail; b=lCH4d1wSzCg4tAxKj+wrtOMRNu+neZyVc7o3IVJts7M7h+Wn3p7giA2AP7mRA7ApxwZ6MNVJ0cRygr8oGSFAzhREYIXkRJ2a3UrZ6aQnvJfqbnI2PLSIJ52j2Rd6AQ6/uk+0ZvV82IMj02vRnxQ5Dq29CfS25ksrur3sEny01zE= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722828382; c=relaxed/simple; bh=1JV5KSiwfEaIFUEDfFjf2A1mxlEviQDERg3Ki6gKE2M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=Q9YkUz5GlWO9wHTgBeZ3PK9GLJ8hY0ufWZv6H/QZEykysw80I+ACmh2Dpy9NTzlOooFcxHqVs2fey8EDlQ+CymS1jvv/SVJdDmmnz6lbIkvReTZ38tVbg6yZqB+o+JAPYnUGrTacjAJpbGUQ+9xLUO7s9PxbQwI6qSyvu0m+NSw= 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=nLQtw5nE; arc=fail smtp.client-ip=40.107.215.60 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="nLQtw5nE" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=xAHzvs0zhAAxkvwni4WuadJsl43EkEXt4294iLXKxTm3Y0rAL8OzYuxYlVU6LNpxErHBz5mfv/NXlSRkzDbdGn1VAw1Trz+KCdqanwxdPSmvoJ7PyYAzeXWe7JSLeTYdsMfT0XIGLlHAZScFuF99B7UquPRnEZEVcbwuj7GK3mY8DbNPjzcedcdi8vd/134vEpzz8wmnWeAnOtjcGwkdFuVos2wsdIxscnifHQs8Vu8SaGiadgnZBdTEvjr6tVr5gSsgq1OHdgFqQAHZLwQonG01rPaUIDtaiJBWKZ3FaTBg3SQTYgj/z2KTUKurZkUCl+AWvsITsgsOpFZ7dvutXQ== 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=tsHH4X5CdxMLGJRzF8Qxk8XqsOUiC6aUr+MRuw1SGcU=; b=CSDEKNsZgKiAUD5dKC9TRFW1P/YoNMcUoqLh81EGp+Bqo1gyKQuxIztnwNBve0oZCxnrmjLHhWwtU/YP/eQ4V0265kpAGhC3QQbL86VVY6qnJ/U5XKayOISqAT7VRRAxIWfg0N7T1wU73zeiniS7zMyWHJKgSmOAOxcwnHmRL69GDUaeCdDhPHhnoy3xkiEymoiuXzr/Kxo0h/jd8tdLIVTUs2i7qvwSII1p6wePhXMiMEuarlQ0kQu1ubL5mHLqWxi6QEVC8LRAzfxgJAJLG/J7e598McEeLHrmSNIpH1LCoy+SyC8vNgXVvHHb4VZwDYXsInPCQ0o7iUmkuLjSRQ== 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=tsHH4X5CdxMLGJRzF8Qxk8XqsOUiC6aUr+MRuw1SGcU=; b=nLQtw5nEjr6NORcz+HemiqGEyLFIkoPO2DkSdXG8NeGAGyPUq+Tr8+8YBaTVsOj04p+Di68uj+i6D43XSatd139HMjYZHeaCdLepLCRcjLdLE++xShGyj9RnFXCZJsgoyPJLrChs7vrmQRyRqS8FixsoYZuNEn3uE2ZLXl0JN7vEDdugHvtOP1PzvTjwtr01wLG+P6AmgP9OLPOGXcfwJItXPscsC0YOv6uvjSaUEFmo4eiN28xHQ8NqiCj8XLFSYTgzlUfGT3K64WWfsOozAXDYveytnolQAyxuO7xbU+x1VF0zUFm+y/6Q+XGuCekQi8Va/AclkpgQn6qrluICKA== 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 KL1PR06MB6988.apcprd06.prod.outlook.com (2603:1096:820:11b::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7828.26; Mon, 5 Aug 2024 03:26:10 +0000 Received: from PUZPR06MB5676.apcprd06.prod.outlook.com ([fe80::a00b:f422:ac44:636f]) by PUZPR06MB5676.apcprd06.prod.outlook.com ([fe80::a00b:f422:ac44:636f%4]) with mapi id 15.20.7828.023; Mon, 5 Aug 2024 03:26:10 +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 2/4] udmabuf: change folios array from kmalloc to kvmalloc Date: Mon, 5 Aug 2024 11:25:44 +0800 Message-ID: <20240805032550.3912454-3-link@vivo.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240805032550.3912454-1-link@vivo.com> References: <20240805032550.3912454-1-link@vivo.com> X-ClientProxiedBy: SI2P153CA0025.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::12) 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_|KL1PR06MB6988:EE_ X-MS-Office365-Filtering-Correlation-Id: 4e9b9454-d87f-48c1-5537-08dcb4fe59e8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|52116014|376014|1800799024|366016|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?MU1pYJMgdOP2yKYOLatHFFXaAhWAc5k?= =?utf-8?q?clvHdvbKn62Gg40p3HUoV9BnKfo7PzSKQNM+t00CgunPs+KnzVGxxXiXMeV8AO7Ch?= =?utf-8?q?jNRBN9oX18CS/hRiuQbqGTnA4YD9HccHVz/6PDA0AMPb5NXPOKh424QNzhCeEaiyS?= =?utf-8?q?wZgRIHrLoYGquob2iIDscc3ImpR+lREBhEsMYtNN03Q3kcyne9gF+Ms++lyQaprLh?= =?utf-8?q?XoIC665FY8DrinfiL9wzYFwUjSTNnkltGWSbAojGj74p6L0JPMzRG1gUnR0VowuoI?= =?utf-8?q?CFbUvWyfTjqfkch2G9ap5IiG6aNpjU+QivOyEEn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0lkydFgYpY?= =?utf-8?q?PeMINHJhRrd0+KPoW19ZNV3LEBBr81ES9jE6SKyhc0IypPvtNYdJVGIeAhso9DBdf?= =?utf-8?q?gFvMAFJ3sdwDsYkTd60dLrWNOkdYzpbymLPVhcuSs1WnbB2KW+Ul6/Kfj1C/lq7am?= =?utf-8?q?njL4/1loXtTsR3rK5hOFy3Xj9mQzJWcCyw=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)(52116014)(376014)(1800799024)(366016)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?v8qKAB2LgjkdjKZO2V+Su5c+gBlO?= =?utf-8?q?saonjejxY/YP98BYwxX9pA4UD1xm5RdLXwPlHCCAF/i61FQCODLh58xcHD0z+9uEs?= =?utf-8?q?ZHqypeeuIvLlM3DBDbmLZXRa3j+TEoW8ezisILdd7NS+2SYoz78FyWLBA9D9sh0yD?= =?utf-8?q?ilZ3LXedrROXXuLrqDeAI0gWSLhbXuJ/cQKmctYj3mBSDm/uJKwxINUNWZNSM4A7e?= =?utf-8?q?X4cYYnMtWolCUidUZ/D2NOQczHgfZTI1VTSmMtAD0RQZ/mnln+1UuMWjiBthW3vf9?= =?utf-8?q?tVk6IHLfqJplmqKxpoesajRL8fsnGoEwF2IfFk0c3ijB4kEKA18jqNEApV1pIo4Fg?= =?utf-8?q?Eb3ABbDUB+qKxN59215GfAAZbypr75anJQcdWH2hk2HOXnNUhTjwuhJpUpS1NsSwb?= =?utf-8?q?l/ZLpAGdcX0BRTLT5iJveMNRUfm/ZobPHfDGgF9qnTBXhHe9hU82LENH54HxgZ+Rh?= =?utf-8?q?idlmgKuNjwAyykCZILUq/jJMS4eqMCH4rhjlPAR/3lGrQIRtsX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PlkIIdY/OAiFmj8T?= =?utf-8?q?ZRZG/LJzavBNhPvKGPPc+Rc0Vb+C1wkoDX/3s402k0MOjivkYxvNkcw55ZSm0+4S3?= =?utf-8?q?llNd90A1BAnQy/b1VdlSqAuTfzXxTn1w9CvCk830l7YvSpZmIQbjbqy/Dr1EbkJ8N?= =?utf-8?q?yYDpR7E6vZW2?= X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4e9b9454-d87f-48c1-5537-08dcb4fe59e8 X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Aug 2024 03:26:10.5236 (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: ddhW1yfnWIcNHiLjML+fhV/EqjdlpvMj3Qf0A8A68SOskCSBnUpOstVsFMF0+nzbE/A8k+Q4ZHdFzM2cwAhzNw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: KL1PR06MB6988 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 475268d4ebb1..af2391cea0bf 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -73,7 +73,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; @@ -81,7 +81,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; @@ -189,8 +189,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); } @@ -315,14 +315,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; @@ -336,7 +336,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; @@ -346,7 +346,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; @@ -375,7 +375,7 @@ static long udmabuf_create(struct miscdevice *device, } } - kfree(folios); + kvfree(folios); fput(memfd); memfd = NULL; } @@ -391,8 +391,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; }