From patchwork Wed Jul 31 07:37:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 13748246 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 CA84FC52D1D for ; Wed, 31 Jul 2024 07:38:18 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 3125F10E483; Wed, 31 Jul 2024 07:38:18 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=vivo.com header.i=@vivo.com header.b="i69gFSVL"; dkim-atps=neutral Received: from HK3PR03CU002.outbound.protection.outlook.com (mail-eastasiaazon11011052.outbound.protection.outlook.com [52.101.129.52]) by gabe.freedesktop.org (Postfix) with ESMTPS id C6A7A10E483 for ; Wed, 31 Jul 2024 07:38:16 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=nWMPPuyKSbAjQO6HDoPPrEd7iYI+uUFeV35XCTChUSYmtyHRSblalWgpOOUv5bmbp4CYCgbGWH5U8A0hdeqPHhWsoXLd5IL2NmGT2oCMqWLzfmuKrNO224tkGOt85gdiFpuFybppwdnR+rdSNIHbjLJ7CufbLtY4fP9QiVQD9PH+dI5KVpqzhe2doqtPjf4uzt7umljMNQ8Hss5pUZrap+6PvoA0W6vww16ujdROMBg5ealsolpO1hxn7I6M6GtXoSKe1/9b1eidviBHWvVgLMAv3h6wvHiJdcfEHMKH8ZGb3QPAWHF7+rl1Ff25Ogmt99G5aA7hBXyewhES4ohenw== 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=ckG3WKdFIajY4ft6VdIEmXtiq/0qclqbB/i5aevI1+c=; b=y7G5lWcwtsVPiQQny1LMGBHgwqWTNSg1Euq/jwpvVVftjPfNvV38sJ+Gb5i1huuWwKq3kUwVqUyyBNx4uZIGIvJV7wOb3VIRg3mylQ86NpuDwEugLyYXsPmCAUjfPbmcopv+9l9wYKEca7Ry56q1ZI8xDUL8kiL8bmXcyURpldULRgBvOl8EZmP+v8X/FTEDjYpE88Uejs0kR4e0sf9WQSNTsUgz0ZdbknGmv9U3qA3rVXxndz0qMc6DZzF9VZrCS1Us+PHQOAMDXed50vMxKzOQb4vVSTnOw8u1kfLvSfnLAbhtTcz6axj1HIs7GFkKjVS691AaLnca7F8n939Keg== 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=ckG3WKdFIajY4ft6VdIEmXtiq/0qclqbB/i5aevI1+c=; b=i69gFSVLMLDEiwzCJxlo+0MXjHfuX5aJ9+brWtWLNfSZyNwUib2LBGdr3YrocDQZBrB8yKueV6vmJUte1rlxZPCoQTLsCUMsge1a0uVgsA4PaIcMZ8B3kqOkxTILe9Y7GYYCW6fXuVZtX8HBrZ4ZCP4cSFwezdYdI87L8cHmxr0R2/No0qp7IrBjsVRfA3ja1khF6vMf8yNXf87jmMkjEcgXyFbaxDNcVujqjralbR4a3OyTk8bwHQsyVzqa93FDFmsK6/nJsUMIYBJN0X57J2/QHjWjPW5/ZafwoCZxslstms609YBIN6fr4SXx36oqGvENuRsWKtDZ3X+oKM8EVA== 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 KL1PR06MB6069.apcprd06.prod.outlook.com (2603:1096:820:de::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7807.28; Wed, 31 Jul 2024 07:38:09 +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.7807.026; Wed, 31 Jul 2024 07:38:09 +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 v3] udmabuf: use kmem_cache to alloc udmabuf folio Date: Wed, 31 Jul 2024 15:37:51 +0800 Message-ID: <20240731073752.1225177-1-link@vivo.com> X-Mailer: git-send-email 2.45.2 X-ClientProxiedBy: SI1PR02CA0058.apcprd02.prod.outlook.com (2603:1096:4:1f5::9) To PUZPR06MB5676.apcprd06.prod.outlook.com (2603:1096:301:f8::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PUZPR06MB5676:EE_|KL1PR06MB6069:EE_ X-MS-Office365-Filtering-Correlation-Id: 032edd1f-0333-4c13-42aa-08dcb133b969 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: +kb6xIdKw5XIjt997iQVE/9nGtpkIiWzoQhrap/1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z4NjljeUrKoxtGpxq60BrqKNRNsZdaTd9sUMqIFOmPUrrDLNZ1X83UP1k7O8qGnxWopi89KCyti7wuahKJyuZYBv69BreLATYuyTUsIdkk/JYQM8+Y4BV+mavUFy0C3h2fm0chkd4/+f2abA4xYOEN3e+3Usdun60c9c7Bj65iafbHVuKJKj2rV80lAkgggn/5Yl0XC1oOghZmnXwPFjd/n8TNgLl0kvK0iYhDyZEf9r5svxQJKV3+Su8Cdt4YH/XvnMAIRV1ow0dG2shg1i4sxEYGt6nBuflvbxof0qB4NBExFyzw== 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: qzrGJIuVfywqRC7ygNk/UyT37m11yO2WHPk5hAfX/9Uud/zZjvyOMwiTaaAYBwuWgkK/c62QNj8Y889i1GTEunhg1YCJ/LTpP4qiR1GXmmoBhCicb6Cg0nRQcr3Mx7aMEttTuUWyGy9+fnyDlp/7lGIaDhnm2G89J2lEp6pyJqzL1t3dpf8rcOr4T+Ftra9Tl2v1KrYszJuYL359UFIUcM/nfS6CcGCvxKB8lRNZl7WB4NygU6AZau/m50umpm9n6bbrIpDqNhEHayx9wy6Q22eVRq+U2VoMPchxBS8QSYObbNAsBzKIv4jtZNAcYDK+Zdi1F7qUsWQAkeeZn8lNHWTjwnB5GMcvOWlPP7WgOFcZje8qx9JhTnRdHm+EApv5j7ZI4IqjMPQvws0mi+9JvHGJ56KUbpYPJEBPh76LBxj0Yu5NAJQtQYLfxDAiPnnNmvOnkaS2NO/6gZq/YPpX0I/VV6IRberiV4BZIeUEpC5ll9SVSaFKkm987SXHdqIrtAoRIo7tZRHIchp+Tez+q7vjbtogTSL02Ua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-Exchange-CrossTenant-Network-Message-Id: 032edd1f-0333-4c13-42aa-08dcb133b969 X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Jul 2024 07:38:09.3828 (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: P5OwT8Pt7JxihjFAh6Zhh6Udp3OwRvxqHbm3Yp7j1/YyER5p+0ZTORPFkNi/gNPxtAupCnB9W8ox8G6vbnVzEA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: KL1PR06MB6069 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" The current udmabuf_folio contains a list_head and the corresponding folio pointer, with a size of 24 bytes. udmabuf_folio uses kmalloc to allocate memory. However, kmalloc is a public pool, starting from 8,16,32 bytes. Additionally, if the size is not aligned with the kmalloc size, it will be rounded up to the corresponding size. This means that each udmabuf_folio allocation will get 32 bytes, and waste 8 bytes. Considering that each udmabuf creates a folio corresponding to a udmabuf_folio, the wasted memory can be significant in the case of memory fragmentation. Furthermore, if udmabuf is frequently used, the allocation and deallocation of udmabuf_folio will also be frequent. Therefore, this patch adds a kmem_cache dedicated to the allocation and deallocation of udmabuf_folio.This is expected to improve the performance of allocation and deallocation within the expected range, while also avoiding memory waste. Signed-off-by: Huan Yang --- v3 -> v2: fix error description. v2 -> v1: fix double unregister, remove unlikely. drivers/dma-buf/udmabuf.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) base-commit: cd19ac2f903276b820f5d0d89de0c896c27036ed diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index 047c3cd2ceff..c112c58ef09a 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -24,6 +24,8 @@ static int size_limit_mb = 64; module_param(size_limit_mb, int, 0644); MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in megabytes. Default is 64."); +static struct kmem_cache *udmabuf_folio_cachep; + struct udmabuf { pgoff_t pagecount; struct folio **folios; @@ -169,7 +171,7 @@ static void unpin_all_folios(struct list_head *unpin_list) unpin_folio(ubuf_folio->folio); list_del(&ubuf_folio->list); - kfree(ubuf_folio); + kmem_cache_free(udmabuf_folio_cachep, ubuf_folio); } } @@ -178,7 +180,7 @@ static int add_to_unpin_list(struct list_head *unpin_list, { struct udmabuf_folio *ubuf_folio; - ubuf_folio = kzalloc(sizeof(*ubuf_folio), GFP_KERNEL); + ubuf_folio = kmem_cache_alloc(udmabuf_folio_cachep, GFP_KERNEL); if (!ubuf_folio) return -ENOMEM; @@ -491,11 +493,20 @@ static int __init udmabuf_dev_init(void) DMA_BIT_MASK(64)); if (ret < 0) { pr_err("Could not setup DMA mask for udmabuf device\n"); - misc_deregister(&udmabuf_misc); - return ret; + goto err; + } + + udmabuf_folio_cachep = KMEM_CACHE(udmabuf_folio, 0); + if (!udmabuf_folio_cachep) { + ret = -ENOMEM; + goto err; } return 0; + +err: + misc_deregister(&udmabuf_misc); + return ret; } static void __exit udmabuf_dev_exit(void)