From patchwork Mon Sep 9 09:18:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 13796502 Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2061.outbound.protection.outlook.com [40.107.117.61]) (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 93C8C42AAB; Mon, 9 Sep 2024 09:19:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.117.61 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725873562; cv=fail; b=RQL+9IFLgYduCFTVLntiN+s78ZVNHBVN+ab5gUzxXkuW/TEkYBg7oTYuV3mRhkOuQuQWzOGjllzikTfEYTBhX9rFf8h5VlCptK9rdKW6/r/fJ5NmwoLNRsNVUK29caxf5qKvpYvCs8nWDhkf+tCFkZQlxF0E4KFeTodKQWzPawk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725873562; c=relaxed/simple; bh=/gKolQTMs38UCvE511ydGXd706sDH+pHUsomxv5mFh4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=raGHRzNqO9fOH7ymddzQYI1/khduvRj2ZmmgjXNVcINjJtsubvUQbXpCN//OPKUDVw15yQfdlycvb0YIceYjSS8i4oaU6srZx529OdIsSLmna0V/M3q8WX3io+nDMy3xoMUhJ17gd+OGJftwuUbD0lLv1Ulk1AZaXO0PcI9e1MU= 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=glFJwnmb; arc=fail smtp.client-ip=40.107.117.61 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="glFJwnmb" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=qAs0Gjc8bpYeIdKExTRErgm/RVgPLp2Cft8QOxYsVcE3TWIQX3//l/v70y/7+41MJvpgbgad8oi+3qgskZLYk18CeyERyOIcHZc+IYPeRf6gWiRHU6Z1ZbiEBSlbE8PTlUetrW5MDAyb8SCkx362YPKTzZC/pDcuqLcCxgfzsxDnhu7ejNaqJhZQnLXSRop0pt9tWM4YUuLgkJ1NZH+sTNqV/2qu7NwjqBAbrs+scThVhnCmez42x59cQ1v9jB9bVSyDUC6WKv0YfCjpdbDiiczZLumESgsdp8gDBNpRAJmhf19dEAa13I4dGOILeOSF9cFqykL3HIZfevMx/IMduQ== 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=gMzgGgj3NgwLdKXnhpYPZxgFQlER0fazfsN7tOlr61Q=; b=ZxCeur5Lv0mAJDS08fJtS2tpDD0e5tWw84eHq4VYxuTyWedErCvrJCGIX+NzDqQOsHn0KG8iTIz5Gmw3QUyoiS1cTiGd45nMm25sfgjxR2tqAFDGke5G+PKJwo/Qu3zUEtztUC2fiHR20m3q8tKdSJpnsBM8OW+Z1BSmGYuKTLkrNoQ9MmujxbhxIk8OH9thyChNWqGGx2wSsmxeqpYdPR7wzd3PUVgoXY8aKTsRpaUXBtssOTZtjdQH7eSLv5TkJhyTfKVLpCElpdpTr0sYPVMyvBqy2Pl7dqD7XLuGwFdpsAWlAdG50w9bjhsu93DB3TIBpSty2+l1OFfNLoMqPg== 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=gMzgGgj3NgwLdKXnhpYPZxgFQlER0fazfsN7tOlr61Q=; b=glFJwnmbiU8AMrsh6sRfqekCVoYy5pVbaxISryxonDS+bEmV4nIRMRG/lJ4ClzCbvHhFrw7xyw4AoxO5pt+QQAL8IJdmRxD/JVlQTj9ARJUyY2lReraweylZimbZm+GZUmmIQ402KblO9MPseLzkwhQRWrd1Yn3/xvO4X0SCvI2pGLpc+PPhSy0h2F4OqcSrmVoqIAfxV9fPP6ER2AmzaoyasCfu85QDHtZ9ZOmh+/ZAa+n0WR5pOrugLfqJKvyQyY92/SCbj00vi8DwDOUNyzpgxEiLX+9R31dOzZT6QRPVFqeNZqv1NnpsYcx1YDyKUx/FVWIH35+fH4xr4xcj3A== 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 TYSPR06MB6292.apcprd06.prod.outlook.com (2603:1096:400:42d::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.23; Mon, 9 Sep 2024 09:19:14 +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.7939.017; Mon, 9 Sep 2024 09:19:11 +0000 From: Huan Yang To: vivek.kasireddy@intel.com, 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 Subject: [PATCH v6 2/7] udmabuf: change folios array from kmalloc to kvmalloc Date: Mon, 9 Sep 2024 17:18:40 +0800 Message-ID: <20240909091851.1165742-3-link@vivo.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240909091851.1165742-1-link@vivo.com> References: <20240909091851.1165742-1-link@vivo.com> X-ClientProxiedBy: SI1PR02CA0011.apcprd02.prod.outlook.com (2603:1096:4:1f7::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_|TYSPR06MB6292:EE_ X-MS-Office365-Filtering-Correlation-Id: 956f5a06-60d8-4fbb-0adc-08dcd0b07709 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?mg0KX9lcSH+gSCq2ACcS7y6FPlneYz2?= =?utf-8?q?eUOSl5/cpEdtgm/g+jpM1KPu4C8aKWjAnhL3ZqQamxZSczZNvg7jkxKA3HoWNN85R?= =?utf-8?q?TwIjgNzOfT+xRQHz33S1LNEPoSBkNkG+hWtqKzriG3NC/kJ1zqXB2Go6BORt9iv9I?= =?utf-8?q?Nl6N0uywuIizbbEHhVxNrDmQVHCQm0jK43r2s2c1Qv9B/M7jeeEaqHCiAI3HyqTlr?= =?utf-8?q?izstnVtWY3mZqB37agqji0ghvgE+TjF57kuyu3zk91dz0M9n+ymB7CyUebPIIYweZ?= =?utf-8?q?zpUR7ya7cZ9cj7R1keStKLXrsYdhAZTh46TNI8k0mXmqf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RqLv7bkXIh6q/TMe?= =?utf-8?q?J4ehjtQRhU4c6tg20ajNl2WRcqai9pUT/lVTfk3WpJPFbuOasRDv0t5DOJcnQ4+st?= =?utf-8?q?JQbtJDaRY/UzF9mya5F9aE8QD2dWcpzoGevEogmpDImVljX7pON8XgOPm2jUJ1BQ2?= =?utf-8?q?1tEkr0vAxJ8V6W2DWGW5jactwbAWbkw54w=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?zxTJXl52ps6RUbzsHEh8C/g+iZ4O?= =?utf-8?q?YCBjut53vJj4OEgarz4R9aw+VqEG83O44lzU9PO1shh19L5pe9QagdIRGk2gDmDTT?= =?utf-8?q?E94Rl3v4twmEmK6ecHJk3nRT61MUdp/Eld/0s364hA7ebS93ESNEhP56rXDjky7jx?= =?utf-8?q?mFUfpsN1GDHSPdnaFvjM7NA+1RQNyeO6s2PWqGxEHMu9OHPsJbUr6DfLzeaBj2nMG?= =?utf-8?q?5NM/jNrj3FmjQcdrDwHJfr8ESi8q1Ip/4YSNFVSpF855WLknyZu8FnxARO5whG/jy?= =?utf-8?q?BThj5rDsY79GLzgxXZuxnlDDbU5K41e3fY0MvkPdzipsA+jQtAc9lHeYR4YBDJ9yl?= =?utf-8?q?dCCWG6XUa14L5U46L+JLKbBZf7KJqalwu+zEEDW8AjMHbTTLl+z/YFEffqcjLdKfm?= =?utf-8?q?HZhOCVAb8Tn+BzRLCUme8hMZb3pja8iJ/D/4U4o9K+T4fdI6Od5TMl7kQw0zCMQ0C?= =?utf-8?q?RIdMwgz7bhKtr7MF8jP9+KrMLfdypqIilbYsMk+iOBG4Kd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tv6m9+49fZnD0ATOd+nf+rF+x/lnp6?= =?utf-8?q?3CspV3RHLHzExEpbFAvNLy+7cVXHmfgxIXL8AnAiyYkXKx+anhRLJeWqHkt25LJGe?= =?utf-8?q?E8nWeT9FCUCDNrsu8IKRuKkJMeqdg5NdGnSnBQr6SVJ3OP0IgqsJBqF1AjKRV66FT?= =?utf-8?q?SQH7BEzzMK5W?= X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 956f5a06-60d8-4fbb-0adc-08dcd0b07709 X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Sep 2024 09:19:11.1327 (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: YBXjc+mpZYaNvvYAJfUyDmJIQVpGWWmJuC7gai4PM6CxBF5/jnjvXQIMAOyBZbmMK70Tk7fntRMyRcjfm2PhAQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYSPR06MB6292 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 2170d975cc76..ba9dbc7caf71 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -109,7 +109,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; @@ -117,7 +117,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; @@ -225,8 +225,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); } @@ -351,14 +351,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; @@ -372,7 +372,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; @@ -382,7 +382,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; @@ -411,7 +411,7 @@ static long udmabuf_create(struct miscdevice *device, } } - kfree(folios); + kvfree(folios); fput(memfd); memfd = NULL; } @@ -427,8 +427,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; }