From patchwork Tue Aug 13 09:05:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 13761596 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 D800DC52D7D for ; Tue, 13 Aug 2024 09:05:56 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 53F6E10E07F; Tue, 13 Aug 2024 09:05:56 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=vivo.com header.i=@vivo.com header.b="Z7xK6XoT"; dkim-atps=neutral Received: from APC01-SG2-obe.outbound.protection.outlook.com (mail-sgaapc01on2055.outbound.protection.outlook.com [40.107.215.55]) by gabe.freedesktop.org (Postfix) with ESMTPS id 6CAED10E2D5 for ; Tue, 13 Aug 2024 09:05:50 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=WDhbWnYY2/Udac1YcnRLS+Kycb7DS0gcVEkQebsMsH1QK3yRt6SFkWfrQZxQ4QmpltAnwZ2cPTzhXjQ2zI5hBL12CnR/LBIwuQQxhq5nDarB+x5HavODUUnv1m/7pIfuyfD6utt7tHGeyQgltCRe4cl2zpk/nnlXQVT0k84eY6bTBJsnvA7KzP4fgDygV9jUUXX3D47tpBuhkDF7a733msTh+gAG2ZtHDvQnizgqIGy7iZkDnbViqpJ59KaOOyvNGvxoq8Apyg+hkWHbpZEsEDX970MuGdAJm5JTaKkFnInuybtMBkv30TtvL/DrhsXISnnjSZkzA9XsjdtoedXXMw== 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=VTn0BzRKTwiLZKTuKqzQTczqkn80pNa+NRgRfpbQSwg=; b=OS8WGebSfxYc+22B/yZV8PqQlPqD7rJVlgSFPVqTLeuN4m3xKGwlDnHgh6ovMiYtGRXQR+vjSM1dV7ArW64aARjxiFzwKyUlxh1avqcURp+8yo+BE0dKoYST0cD8yRzvuhHOKEcjfy7QFe5baa7RdtTHBCh+RmMezY2G+NTrRr57i/YABNf3WbfY5DEICn98rxfnWD+Bt1tT+MhSosa/uejE2Fd7M7wUeBkVrLdtP47gBJ3AGcvHVQTdyPzQlssKoh6U1A4pJ+4a2TfOgyRskaPMXrhh5t0Y3bPVnEJmUgn4MZmoYU9uqsxlAufnVpfhY1x56vLmp/mM0DiPqtOPQw== 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=VTn0BzRKTwiLZKTuKqzQTczqkn80pNa+NRgRfpbQSwg=; b=Z7xK6XoTY4FCifL4ngbUVsor9BAZfKA1fXWfKLcMOxWu3PoXB76kspBFjAUEejLLiXwzFOsGN9RQALgOR6/BaY/YyY9Xs5wXh6nkseDrBWCeGNBUnBOOYpcfLYqZnpYUwqVMlx7e0wXYUwQY4idFLs7HW0piMY/F80EgkTqIOuR7C5CxYxgTvDpnBq1/TbYRVwHyBombwbBnG4d3fToa9Ko3Tgvziv8XVmwyb3BnHdeqaP+EO/pZr8uVM6ljowWaWWcG1yUxUuZ5PAhafYvPHLYP5czgbeSDSrmmVY61LgxHABMn5qPSusidVHUnvrO5HwR/mtlpo9TE8aCXr+Ms7Q== 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 JH0PR06MB7198.apcprd06.prod.outlook.com (2603:1096:990:98::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7849.20; Tue, 13 Aug 2024 09:05:39 +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.7849.019; Tue, 13 Aug 2024 09:05:39 +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 , Vivek Kasireddy Subject: [PATCH v3 2/5] udmabuf: change folios array from kmalloc to kvmalloc Date: Tue, 13 Aug 2024 17:05:08 +0800 Message-ID: <20240813090518.3252469-3-link@vivo.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240813090518.3252469-1-link@vivo.com> References: <20240813090518.3252469-1-link@vivo.com> X-ClientProxiedBy: SI2PR01CA0041.apcprd01.prod.exchangelabs.com (2603:1096:4:193::15) To PUZPR06MB5676.apcprd06.prod.outlook.com (2603:1096:301:f8::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PUZPR06MB5676:EE_|JH0PR06MB7198:EE_ X-MS-Office365-Filtering-Correlation-Id: 8ab71fcc-9f61-444d-a0e7-08dcbb771a37 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|366016|376014|52116014|1800799024|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?baoFqYPCMQeOQmTEo1Y3itJ4A0jmzds?= =?utf-8?q?6qFfhPEz9w4fBiaHcIHeT4CQzy/swydrLpegDdKlg7wNBTkXubPZnDPchktZM8XLw?= =?utf-8?q?Y+vRCH7HrAOgONlma8sg3iy8iZ/K0/D5f6um74kYi2IOpRzt6WoKVSllCyHCjlbbX?= =?utf-8?q?Agz57i4Ph62S++qLPWiAh6LjW43QWUHKfihzJ3KUEaL0awjYLrG7+YakZfvs6b0FI?= =?utf-8?q?WxKdpGlsqTigAdWgunK6cSBmrCWjmOHhTZmfxiNjvD22DzwYOhobpgoS4wlBcKI1G?= =?utf-8?q?HuCqorGXZPi+dztyqTnpi5QkvnLgyo+98QV0cVo4+m6Lwh5H1p9DV7MWYd232/nXQ?= =?utf-8?q?g1KaPHajPXWm8qIxethG7Br5epps9Qte+UKq1zMeQw82KUZIHPqavVmNJwJsx1OyM?= =?utf-8?q?jrmH/0nIgOEmnU+z5nPWH8OqqYY7Wqh2yuoxwoJ7yGb9Sy4xv8bKWcIgsNaCcZI18?= =?utf-8?q?KfHIwEP777SPqHKAJW4wo8l9LYKGfhS3BfQMlSIWnfNhbTcDpfJ8bJyPm0xC4Z5JY?= =?utf-8?q?kBj+Zlns0g87qX2FOvwTgGn0yefRhnopQt0iyLyLB9fWy+dxd2omq0VOBOuUF/S4f?= =?utf-8?q?Gd1y013f+wHOTljzNEBZs8J+e0qmSsrcMh0sht2FuCfthoaIjuyUbNecaFaeOtOIG?= =?utf-8?q?NTcCRP+yViqWiMmThaU7T5hXFhsZQ24ykpqb7UoArz52p/E7O5ezRQp5TEC2g54HV?= =?utf-8?q?1q3CN3yAiGyb08P7fp+YP8xp2MsIKEbqJJChQezRxMobpH20LxbIa41SUBmr1TU5n?= =?utf-8?q?Y/h4D9cGQf6/rs80WwYIKka0QfRnYu4FZwVn2LHy+NZd69AZPw65TTxPz2g+B0paQ?= =?utf-8?q?S52kMeHca0UMhlMizgMpcDbTclvgQOGOCIlLpce07fg2WuLzoJZk6PwRgse0eod97?= =?utf-8?q?eEkLZ8RU7X7o06ZTPHwROtx2gTfBpvobiEz43GeCWOCFYMgAyDL3jDqJ7Ak18DwP7?= =?utf-8?q?6vL8W8hVB8ezHUrx5LJepaGa0vQs6Gop9WIL+InhuaZvLVLOiFkKDM/F4yYx61pZm?= =?utf-8?q?gQtWqCtg2nf0yQ7l6ZBXeBgVqScrQYB3XM9OX0g9kVkpI65naNS3JkJPTK93CPhEU?= =?utf-8?q?ZNeZroeeVi2+yYCFYbzF441IHAahDCxDUg/9S8hSls55lFvidnoUSDkgExprqZUpU?= =?utf-8?q?DJjOdBRAVhDvHaO61G9L4B5blebEqWP3J/PQ2H7+RwFsNbheTXDIdYILaIFX5V1jQ?= =?utf-8?q?FcHSEfP6GaZMeRceRiRg4Zr/7m/zIS+XOgmLuDWSLak51VOwqsAW3HSv18ru9TVHD?= =?utf-8?q?JGHaV1j8PL9jd4PBxMk/QxhlRDmEZ/zR3BbZIrhXHAFlUIJRkz52yweqyCpeZcRC9?= =?utf-8?q?TX/O9cFOMpKy0mWdqhaLjfCjLYolJnkLdA=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)(366016)(376014)(52116014)(1800799024)(38350700014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?+12xlRfZE0eVOIeCu/Pu1Movokos?= =?utf-8?q?V3FH/mwDpk+R+ufP1LuHCvMK4PU9/2NeG+YWsDMjAdRKkiF29OHj0TeCE8DkF+D04?= =?utf-8?q?4OooudW2dGsnYWUJytA2wB9D1OH9a2ghCRrU59KlXPcqZVkFpkw3hOTgy48S543ki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?utf-8?q?rljWs4e0yhJ9A/7xZ60MVYZMalvR8KM+QCA3+upXSE0x+kQwhtAEDnQPvP8bZm8uc?= =?utf-8?q?Fdwky0NH0qFoP9UMXh/Z47iU6CBkub0EFZDBN7nNU0eZUEmQzju1dR38OdAOIfDk4?= =?utf-8?q?IHfMLgOAjs4rfp88iKMi1bVtpGIYD0ZpXJMz6wQ7cHzTrpoWpx0Dz56F5pF2V/UtY?= =?utf-8?q?5NWWBOD0rpANC4cl7vqf8tT98wiYcJYe65ymONa6HF3a6Tun+Fb0ea8977Py3Nteu?= =?utf-8?q?MHsSzTQiwCthS7NrP5cYLFNhoZ8wJ/74EIA+jZHMV6V9a53t1ox0fxfAy4WlxP2OS?= =?utf-8?q?TBqVAI+ZihTAkAAPgUYqBO1gplNFtU+WhAuGZtTnKYs9XueKJustYJbB3NvvTFZmZ?= =?utf-8?q?7Dl34Xp6LHJbuchigV7nYUUduT64EHcbTYXexuMSGGIpwjokuHcEVEfGzvpg8kIK9?= =?utf-8?q?lk242n9Prgzkb0zgWicAYhrFnRmOk0jQQmqho2U/jbt+s2NRXCkOBFqO/7VrbIIym?= =?utf-8?q?Ec+eV+ufCUtL?= X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8ab71fcc-9f61-444d-a0e7-08dcbb771a37 X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Aug 2024 09:05:39.6696 (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: OHHIkHj1HeznqKEYjG+LFxBm4vZsxbtWZqqo6fCY4gbT+c7OXM8c16Iuj+hDd8cMhMG+6PTALIaOZhiJbhlYEg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: JH0PR06MB7198 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 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 d39f9e1cd532..3ec72d47bb1c 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; }