From patchwork Thu Aug 1 10:45:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 13750172 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 375E0C3DA4A for ; Thu, 1 Aug 2024 10:45:35 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9A09F10E8F6; Thu, 1 Aug 2024 10:45:34 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=vivo.com header.i=@vivo.com header.b="dCcRqFoC"; dkim-atps=neutral Received: from APC01-PSA-obe.outbound.protection.outlook.com (mail-psaapc01on2043.outbound.protection.outlook.com [40.107.255.43]) by gabe.freedesktop.org (Postfix) with ESMTPS id 663B810E8F6 for ; Thu, 1 Aug 2024 10:45:33 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=B9/UHc2GtBuBgfslbsAm2GtE1JJZgfZPUc0JZ5CmHr/4LGke+J6+/NONFjGkXwZwqzSUHJuaWxl5sBApm9/iSr+/RUo7G6FsI2qCcb1NkoGgiC0if2dwWIch1XVPhH7WwBCCxEr5LDvSgEfV6eKn/2K7X7wOHmacU0ONHLLYagrND8CWJeRzHBgEdFN/6BWu4ux69mxVHwNQOFKDrEczR1NK7JKS4LTvonu/SQlGzUwGFzw5DE8eP1owbPTNfW+OZ7SNkJOQXRZl0UBYD5hNUEKZ81XFhik5g6difC2uUhPU0dIPVr1rxyle6VL7QdrwfKp+AGoQ+FNDf+8A19sMlA== 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=Vi78hp5Aou3qDPxtxj+R4tHNMNYIlPV0XE+Goi9lKLk=; b=GYJWAcGZGSgBvOvguKy+3uig8XqWmSFCwgJjZ9BhygHpY0KPULxz2UPPOUqo/+K264nPCOIN+2sh3hW/kEhziUQEqOlHuzSESly+1Vspe2wX8tlIT9MoujFWTYt/nFFBF6fACcDmyZKgsjTIVAW7YZ3uMHjEQCKc14rEPSRwjf7lAJi5OhMZrOGP+GLk42H84cQ6OY1gT6rGdopdDJB0CdM/hTiGEB3SQyVV9MtyvE5sKg/5fmb+Be7Cnm9bg489xxVzpaTtrpyzLxr9yL+R00uYgqkkwtgGpK2++nSRZxkIwzZp1qUOkahSiCvDk87Z7KguQXpwCJyWUHXT+Nb+OA== 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=Vi78hp5Aou3qDPxtxj+R4tHNMNYIlPV0XE+Goi9lKLk=; b=dCcRqFoCSjhkVB92MH+5KQSEzwGeHyjCAV17dDdSxjEDFkT3P0vziKEMgUr32LuN8xh4mRxz4ezlGx5Uivqpdw9zPDorVCyxZK6dLwwdaXMG8hetzeR7+ZbIgddukkNQBxKMdiThocQVlCLAKZ81pw8qARncmB/1PGzsPgVU+94QNQ15q9Z99wX4uK5h5pA9CLHS6L7EejfMvzEsLnDQHVutQP4NSKgFw2lC5YFC5mky0P0sEniikHFFXzYfoZEaN+8cohW0p71/toQDJl9WK1ke8i21iop1vposYM6L457+l03UyL2XdYIvYOJloM4gPf8U5oKWn4/OSKvHEpkpdA== 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 SEZPR06MB6304.apcprd06.prod.outlook.com (2603:1096:101:10d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7807.28; Thu, 1 Aug 2024 10:45:27 +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.021; Thu, 1 Aug 2024 10:45:27 +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 2/5] udmabuf: change folios array from kmalloc to kvmalloc Date: Thu, 1 Aug 2024 18:45:06 +0800 Message-ID: <20240801104512.4056860-3-link@vivo.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240801104512.4056860-1-link@vivo.com> References: <20240801104512.4056860-1-link@vivo.com> X-ClientProxiedBy: SG2PR06CA0209.apcprd06.prod.outlook.com (2603:1096:4:68::17) To PUZPR06MB5676.apcprd06.prod.outlook.com (2603:1096:301:f8::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PUZPR06MB5676:EE_|SEZPR06MB6304:EE_ X-MS-Office365-Filtering-Correlation-Id: b51f6e2d-040b-4fb2-f96b-08dcb2170e6f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|1800799024|52116014|366016|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4ndgccI8Qoy8By98K2CkjzAGQwiI/CLtl8wr9jDdWe5FGxd67B+WpukaU7+aPyk5b2XQSkOsJQIjh59omRuNkOvyrxL8YgBZ56r1gtX2px8+nqG9yD5aJTlZlRyd+XuStHBhXDhqU91NHumtb2UDiavnf7SBdSmO6yRNNEKdfvenr/nHop5gBwbThd/Ca8ofCb6mfyyyqXXQKTiv9BXyIv3plWvZWzRfpXl+gOt3PjsPfnBliAIaA9hg43xc//NR9uIj0PGUd/MUhqaMOJFLtKb2mtTaqEzBILrMf3mlki0vraKimR011IxfZT4oIMlB5oo79YB47HXRpEV+vnqQzL4nJciK+gdlCzZ5IVnsH8Ts9xPgEbH4WmPnpE5A== 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)(52116014)(366016)(376014)(38350700014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 6YyoEtaEmLvqUFiLgKtkV80TGsHKYs2BbVsbTmJUGba20cxJXHJ66KnCjr38uEyLRsoj3B3t9uBlFoPJjKsfd5KHyUAuyMemqEY/t7wi9quVMyfcpJ+f+p2ow9FLNxj56XXxaBh59UrWz+BaumjB6OAHPlh05Plmgm6p/RI1hznIuPcfVH/FqM1ZLn3O0lJ3nKaM0wJF5ArfakBpb7430OTmcBd+Ok7IH5iecQraRaeWthiCcsiiS0A3Ohm7e1QI3uEZKU4dg+XeYk2eQ2b+0uryc/6I2kmMJhMPuwkUzXYe03UrBC10In5PVkfjTOUAr+bhfFqosjnGzf9KDi1bq0VUxc90adq9VNKOwmRGjFk+FiaqO/3ohLyEKtds35TwILU+nqUeoi2XILTNh6Ha04co35Ld+K02zfkxyRu2oy86fs+xvzd8qf0uKe+d1uifpDbKFbEaLzgo/95YoljgANpG1G5/PYn8XR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17s2uDBW4bqBlJo0rkL/IgOW2JKR/U34+otA9x X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: b51f6e2d-040b-4fb2-f96b-08dcb2170e6f X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Aug 2024 10:45:27.7704 (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: hMDEtKBb9woECbNBtFVqPjxuPg04GXV5e4jeOB6IpZ4mDoZDT3v2Vvb3dH2tLfpVuaqGKr6wn4Mkdpw+etYIsA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SEZPR06MB6304 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 --- 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 d69aeada7367..a915714c5dce 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -83,7 +83,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; @@ -91,7 +91,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; @@ -203,8 +203,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); } @@ -330,14 +330,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; @@ -351,7 +351,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; @@ -361,7 +361,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; @@ -390,7 +390,7 @@ static long udmabuf_create(struct miscdevice *device, } } - kfree(folios); + kvfree(folios); fput(memfd); memfd = NULL; } @@ -406,8 +406,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; }