From patchwork Tue Sep 3 08:38:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huan Yang X-Patchwork-Id: 13788302 Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2060.outbound.protection.outlook.com [40.107.117.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 7B9241D86CC; Tue, 3 Sep 2024 08:41:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.117.60 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725352914; cv=fail; b=o1pyyVYuYr0xirYxuQXfdjb7YfOZL9ZpKjKkDsJp31mYtoT0mztzn346zU6WKkS2Ar0PhRmH6AQvlNGCGpkNJgDmiSZavvOMkT8OtATerhcDT4nge1SyPc3+XQI2TivP6A4orn60iOriNSuk2U9RGaDrLCpSPG11B0VgxhTZl94= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725352914; c=relaxed/simple; bh=+wtAyTZqB5EKV7cKoVKbNMxFnrdXRF7b6LorZ1FcXtM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=s0lvUKxVe6cSEiUpz1nA2bLyd0pLAdzaFJt44HygNDMrBBgUIzRRH636UkeR1Au58EkJBx0GjjcdZd6IdA3mmMmBHeaOqtkU+ZVNLUDZ0dPloRiv9+VgnXaubZqZW0o0zovyX5eb6X0kg0Yp64QjOLGuXC/Zu7UFIj6DC6wbnWY= 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=VeTTAqhJ; arc=fail smtp.client-ip=40.107.117.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="VeTTAqhJ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=kcEpYELBzUJvNFowwWcRh8FmTJ/PcVgJ4PASxMzATh/jdfHWq3U3xjKWcG3YbzGfm+MsN365DAdcpKBnmG+2bBY09Kaiycm8dkjC3zkpLkfglczIop5zEUZafg+zKX70zUlEEm+K0wAPunqu4sRdqgrEMRGo14x3shreeXXM/xeV+uL9vBf1msOevvLW8B6AyI+EN2GShDeyVs2nKjeraA+ol5wbD7PhyS+OGQ68h6wkf9Blj4YsE0l/uiXYnqYZ8qJMtNkNOPZwvrqjHCTfivArLKqip7kac3AESaTvM99hIReuupLT/eA2Hc91EyF2wYcyd3VkSk44+/4TeLEfzA== 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=fSFf2g3ny3ZSHAkRcTr13Ksd+qIbg9+zhN/spkWHmIM=; b=TwfsBqukAn+oUuHsy+Ol6eN8325/hFvoq41F7Zd2FqcxewzhnoCIhCkveAVs3lEtb2+jtkmgVe5Xw7hsMbd3J6CjVezDN58WgZU9VlxwisLjLxJsSVIuC1BJ9Rf5OnEq092B8Ik4cPS4tyQB0w/oz7Vike7UpwzUECwFJb9L8eC3aiBQPkLZ28rplOp9pwQYodrrAF2LsaarA6M+KtlqPGIjWY4NQ8cnhyuaBUkffDhCddwQgAqe5ZfaARHXV0Ji5h0ApcNlfOJKwM+Mo+ZLRVxL7cme8Y6u/0Y+VGbcPnJj841x41DHISvA4RSe8ktjp5pk7byR2DApgx6Ap4fKYQ== 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=fSFf2g3ny3ZSHAkRcTr13Ksd+qIbg9+zhN/spkWHmIM=; b=VeTTAqhJqSUmJ6aPS66q8dYOPdEqzXNkAdW8f9NNsrikGhIZtr+fkevG14c4crU4IugD2qkifvSVaGvGgNLhbZV/Wyndmfco+pFf8xOkxro+9IKv2CALgezh7x7TLg8m0iMB0j8VaAcUga/F9fAcqw4iXaGPgIr54ENRIxEbzKaGzY1NnLYxyYKZwObSAFQI5TbvwUNN6mI2MJ9ZBCXzCeAxUiasUeqYnbpLSR6Zxut3HXaA17Excnb4wk1GrOvKhmt7juWNVzZNK29LWUhYYZhAw2mkT+9s579GqihwlWSShBWQrBC3bonzTJv6VHMmSmqjV2lIoThANJHcjmnyQA== 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 SG2PR06MB5359.apcprd06.prod.outlook.com (2603:1096:4:1d7::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.24; Tue, 3 Sep 2024 08:41:49 +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.7918.024; Tue, 3 Sep 2024 08:41:49 +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 v5 5/7] udmabuf: introduce udmabuf init and deinit helper Date: Tue, 3 Sep 2024 16:38:10 +0800 Message-ID: <20240903083818.3071759-6-link@vivo.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240903083818.3071759-1-link@vivo.com> References: <20240903083818.3071759-1-link@vivo.com> X-ClientProxiedBy: SI1PR02CA0026.apcprd02.prod.outlook.com (2603:1096:4:1f4::11) 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_|SG2PR06MB5359:EE_ X-MS-Office365-Filtering-Correlation-Id: bf3a0b50-ab8e-4cda-880a-08dccbf4408e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|52116014|376014|38350700014; X-Microsoft-Antispam-Message-Info: T0g8gcG8WM7pFoPX529vuNzzmf8DOsgv6MiB0Js77okuMlQlwuWybJBverDvwJAYqbrY4lJ3Nx/AkWEWHjjhoq7WGO4RtKUlbRkVmXBhrUipF5kkbn4Zf3//72KQqJOW1waZcZU+C5QSe3N8pRzCXjg8w3d1r4N5FjpHFJgdtS28tYuvadf20v6BFfSyjyEr2NWi5T5a0y5B2uGdiLbK3e2TBBzpdD4/ADEqNixC4KjfaOWjSIO/vVffcrxr4KtduWjw9SUGugYSq/Go6XIZ1lW59pX7thvkY0RtBuSgvdPFUDNpDy7jra/SlAEvVbmfFbJ3AEXsq2lSq9Zpe5ChCpPeWJw7tKFecHXNek+uhTwAanmy0BpEa97fT4+oy0aKzlGwkg778n2DULdwR86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L:1;SRV:;IPV:NLI;SFV:NSPM;H:PUZPR06MB5676.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(52116014)(376014)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Yz0Kt6suNci93Mhoq53zzx2XRFhb6e0GmQp+5Fsyy0/YQzh7nHObUHIwDQ4y+iAvlhS0AgfLsUrf4FjOUN++XOsIgiIpLvC4s345ws9iVnTiT1YNguVdAFwv+IJSYxMyp6mpmKG6ra82+Jie0NogLomomQYuxHGUfVANLjrXg7mI2stapM0wA7tvOsiEgbIL1G+UPON89jvGgQrCDMU5u9+VNjGF6gWVLXCGJT2uQKp1sOmvtQ3M57daG3ZGTI3gekkT7A+OydRRluSI6jGRBG7WbQfgQpKIFAN0doWMj+4QGqUiK83Ho3MsVzE2VxMtZuFpqhSRe6l27czteYeoI4JwULaAUBEHjgc+qxSApzYf8cj4Pf0ZyDqMUxVDvRtdAcxmdnl4JJaWRlGDHK0JyrSFS6jqw08VM305u8u+DplKPKq5V5a2YxCz5shaHngf3zDHk7Cr067lfxZ8EpTyrl0k2vPDB47s3EXD8MuakOW7qWts7SvHgmGuc8pCbN7IiYII4iNlkD4EQ6dx/5MiobGwP+9ltzXbVTVY+aB2D2H+UaYBWzmb5umnBTjgvz3jj2uMyiNioqcimOzsGSjCiCICAxTEUwY7Wqi/kV1rEkmDj0EEBCVY3ndK5OwMo0o6NkVaTi0vumyO5KzBmMoO3E/imSC+I9OYaAWzDg7K3NKSNTfVhRbiD6qBAHo8HRxI3t/FUxYbCUJq49YTJZwiufATKWPrRx1ptsy02OTZkRX78JSTCNlYpr9JK7ALZQZRJtcjGiwsNfhm8OC1bcaucP3JjcAhxiO/G6XVebhEOUUIqW9TZ23zVxpxFjeD3UArNjMVEuXenQSw5vHp61efYe7J/ODpyV27df/F3o9vFql7Dl5fW7eWdaGGmOMYKxlCF/PDIdYZUpKN+/+AVRM8I93903eMBzL8gDTV2eFCy6wrSlnCiXAe07tDXfjS7drGoNDt85qGeF4eB5XVqvZ/9ZZh4dAwjcqK20dvMQZqgJrwKCFSho/5/f146F3e/NyXfAWTRjbXzoHJnuKwyjHPE/5BMSUp0u+oUcfc2G6ZosZLFVGpQKapnyB5A0z9I+rkg39VdVB/NF/ZMSHU7EBMi4cCPdGKSo+9Rqslmz/M4w+bsZQcl5ISk+sP0KOYr9Dd9J6S4KQMNe0v4rDZFqMtEF+FvZ0KkM9u1w7x2nGzcU27Lm+OUUxIqQy0e1eOHU1JykPMR3N6wAT+FRPwD7K3auu+ieIpH6fRDdJodmDiXkBvPy2xYTt3FgcZefTiHKvoZ5z0glVExO7TMspLfivwrsAYxFE1b5qfEVl4ClB/5S+AvZlH6/SiO1v1CTJc7LBqBfmGmesqcYXYxb7F52AYsSvwpjFDQfdEtF2H9q+DCSqICSoXTKmkEMQx75Pq1g62UP4B4HtRK/GmC4A2My8/YRrXF2FQt5/c2d0nxi3Kdoqtevp+KpkYaIdYm14Oa14QC6bgtDsDkMmBG83wS+znU+8aSqhGSy91gl5MOODw/xr0fGxzz+RqJ4S8Knq5kR0cXiqidwZyh6uWigbENUJxH533NWxcOL4y1cS28kDzmtpkxuqGYtk7Cgug5e1Laxw2 X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: bf3a0b50-ab8e-4cda-880a-08dccbf4408e X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB5676.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Sep 2024 08:41:49.6819 (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: e4+UH7M0V/Vecu+xI5/neW35wzbcKE0wlg8ZxjFVFmgtdKna/ZDtZ8KCxR6JjP8EQvSrOKm7rFLbc3n+bbnqXg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SG2PR06MB5359 After udmabuf is allocated, its resources need to be initialized, including various array structures. The current array structure has already been greatly expanded. Also, before udmabuf needs to be kfree, the occupied resources need to be released. This part is repetitive and maybe overlooked. This patch give a helper function when init and deinit, by this, deduce duplicate code. Signed-off-by: Huan Yang Acked-by: Vivek Kasireddy --- drivers/dma-buf/udmabuf.c | 52 +++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index ca2b21c5c57f..254d9ec3d9f3 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -226,6 +226,28 @@ static int add_to_unpin_list(struct list_head *unpin_list, return 0; } +static __always_inline int init_udmabuf(struct udmabuf *ubuf, pgoff_t pgcnt) +{ + INIT_LIST_HEAD(&ubuf->unpin_list); + + ubuf->folios = kvmalloc_array(pgcnt, sizeof(*ubuf->folios), GFP_KERNEL); + if (!ubuf->folios) + return -ENOMEM; + + ubuf->offsets = kvcalloc(pgcnt, sizeof(*ubuf->offsets), GFP_KERNEL); + if (!ubuf->offsets) + return -ENOMEM; + + return 0; +} + +static __always_inline void deinit_udmabuf(struct udmabuf *ubuf) +{ + unpin_all_folios(&ubuf->unpin_list); + kvfree(ubuf->offsets); + kvfree(ubuf->folios); +} + static void release_udmabuf(struct dma_buf *buf) { struct udmabuf *ubuf = buf->priv; @@ -234,9 +256,7 @@ static void release_udmabuf(struct dma_buf *buf) if (ubuf->sg) put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL); - unpin_all_folios(&ubuf->unpin_list); - kvfree(ubuf->offsets); - kvfree(ubuf->folios); + deinit_udmabuf(ubuf); kfree(ubuf); } @@ -396,33 +416,24 @@ static long udmabuf_create(struct miscdevice *device, if (!ubuf) return -ENOMEM; - INIT_LIST_HEAD(&ubuf->unpin_list); pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT; for (i = 0; i < head->count; i++) { if (!PAGE_ALIGNED(list[i].offset)) - goto err; + goto err_noinit; if (!PAGE_ALIGNED(list[i].size)) - goto err; + goto err_noinit; pgcnt += list[i].size >> PAGE_SHIFT; if (pgcnt > pglimit) - goto err; + goto err_noinit; } if (!pgcnt) - goto err; + goto err_noinit; - ubuf->folios = kvmalloc_array(pgcnt, sizeof(*ubuf->folios), GFP_KERNEL); - if (!ubuf->folios) { - ret = -ENOMEM; + ret = init_udmabuf(ubuf, pgcnt); + if (ret) goto err; - } - - ubuf->offsets = kvcalloc(pgcnt, sizeof(*ubuf->offsets), GFP_KERNEL); - if (!ubuf->offsets) { - ret = -ENOMEM; - goto err; - } for (i = 0; i < head->count; i++) { struct file *memfd = fget(list[i].memfd); @@ -446,9 +457,8 @@ static long udmabuf_create(struct miscdevice *device, return ret; err: - unpin_all_folios(&ubuf->unpin_list); - kvfree(ubuf->offsets); - kvfree(ubuf->folios); + deinit_udmabuf(ubuf); +err_noinit: kfree(ubuf); return ret; }