From patchwork Tue Oct 22 00:20:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13844938 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 1AE62D1714E for ; Tue, 22 Oct 2024 00:51:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type: Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date :Subject:CC:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=1FVq9UssPtzeq5kXsifl3eaATnk1YHonLysyDpGctPU=; b=k1D8PnL/fbmxdfGORXAVnwRcV6 UYAVGqj4TyKvnNeOnc+SQrsa9pSioPjDBtUGEOkz6VL0nORQwAW0mrfiZzD9v/X/irf85Q+wHtIdd haid1RtD2HpGcq1io2xiJETsGAohs4/ot9uaj3NFRoDaWpnCK5IR2yW077oy/M7LYClFfrFwPOhmP ChbZ5v54lPfEZ5/KUG/iNQbnEIZWAr4VE+n8vhi0MhmYZTNRAOADFYk7kYeSdgoPQaQy8ayFlpzeR ozHCI4fWvhMg9NBAcMtGdyb4A0KySvH5I112wMyOAcMNIaUKPCZ3ptAvl8eeKB/uxGCGaSRh4KLCe rrHU0GMg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t336y-000000097fT-1VCj; Tue, 22 Oct 2024 00:51:00 +0000 Received: from mail-bn8nam12on20600.outbound.protection.outlook.com ([2a01:111:f403:2418::600] helo=NAM12-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t32du-000000092V0-3hTC for linux-arm-kernel@lists.infradead.org; Tue, 22 Oct 2024 00:21:02 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=At0vMY0cLISXT0k/Kh/y/si4p7S7U1jlNB2T9vM6IAADfQRFtwRyJRm6WldRaqm/v6Ka7MCp+M5a/gXYRdrSd0UR4tdAC/XN+zWMimOb/1neh3DewV90OZyuc0vMW7tL+3rf+gQmGsPWj5dWGFv3KHbakPbOZpzFso/CClPFLP1LbFh9ALkhskQt7uF2+dwnp0gKIcsW+iq2aY6+j040IxK6osrwpLiuU5TX7R6d+zTEyFVQz+0MvQfC9ZH6WSQMBCuhKy1WBFAEROVC2TOt4Eho7hUGx1yFUW9B0CClX99QcOPLmmTNT/8gZW2fBQTb5yQLn1Gj5qXTEs25NwZxNw== 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=1FVq9UssPtzeq5kXsifl3eaATnk1YHonLysyDpGctPU=; b=tK0VPyILWuo9wjWUN+CTwaSCHvogaSWU6RM2auuU/Gx0+BnbIIIjuWNHmnnsKRbm7fkUqSPw8qt+C+dSdRRGrB2tq/YE/bALIxPzKljWhGwOVWqUhaNm2FlxkXCx0g/3p+2LZqAmWIZgOacrvBizBMxanN+mYdD/m+/mM/RveRST7cX0iZwkTO0rE5u6UzDpdq1BXaio8DERy5Z2oqYabis2UNI1Apz2xbPDqH0H/18qfryLLNaW4P0ii9xdCKsjthHyUHJ3ED9lLFJQeNKwo2TGTgnBsFy5hD9J0hk9ibi1q/PaXdej9ecbRhk4JKppklbiNbiI+foMXWH/+Q4dDg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=amd.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1FVq9UssPtzeq5kXsifl3eaATnk1YHonLysyDpGctPU=; b=axaCwfjhbwVKgdPv76x9BmftIjS0j6NiLz5hKCT1WWNQJ+zDtz0w/azXdiysAdvTtXE2W+vK4fUTKs8VoZlgtorf3lm6ZBomjtlhHCnUoHSH8hqPAE9pZljT9dmi3eQ8COeSyv3DaGx6yrbitIWTW+4+qTxYSyhF7V75R6NVbkYdUI4Dbd/ziV+4lW7qt49eZEk4BiaEYnDKYtJ/CKj4f0xZB4ed4AvxKQOc9+uoEE7OrpLKfX59Vi+hGDZ81pqZAMCF7F/EentTixWcf5VmbtLmT2svYnEkRbVfAmJq+C+qJB4nHoxJgO2yjLvBq2xbJtGK4O12AEZHI0Cn2lbX/g== Received: from BN0PR08CA0020.namprd08.prod.outlook.com (2603:10b6:408:142::8) by DS0PR12MB7677.namprd12.prod.outlook.com (2603:10b6:8:136::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.28; Tue, 22 Oct 2024 00:20:53 +0000 Received: from BL02EPF0001A108.namprd05.prod.outlook.com (2603:10b6:408:142:cafe::bb) by BN0PR08CA0020.outlook.office365.com (2603:10b6:408:142::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.29 via Frontend Transport; Tue, 22 Oct 2024 00:20:53 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by BL02EPF0001A108.mail.protection.outlook.com (10.167.241.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.14 via Frontend Transport; Tue, 22 Oct 2024 00:20:52 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Mon, 21 Oct 2024 17:20:38 -0700 Received: from rnnvmail201.nvidia.com (10.129.68.8) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Mon, 21 Oct 2024 17:20:37 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.8) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Mon, 21 Oct 2024 17:20:36 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 07/14] iommu: Add iommu_copy_struct_from_full_user_array helper Date: Mon, 21 Oct 2024 17:20:16 -0700 Message-ID: <63579e493cd66965cc830906fd1f152d9467bace.1729555967.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF0001A108:EE_|DS0PR12MB7677:EE_ X-MS-Office365-Filtering-Correlation-Id: 9a1554a0-64c6-4fdf-1a62-08dcf22f63b9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|7416014|376014|82310400026|1800799024; X-Microsoft-Antispam-Message-Info: OZZYTlvh20Osohn8NRMQgWP5aExEJcZDrbogL3LOEmdGqO0zVQQUQOylm0JdkQyaXY4e6kv/jDMD1MoKCohSjlfZj/ZCItnFMOJ8kuWzZ80vCgSLWCMpsnE/9cftAhOQlPvMolukwcD4qSOhqp91ZU1BqNEChI9H+5ncQc1M9iCV3NIaMl7JKhmJ+56QMsEdFdlG0luKPBB2AneReOWWukkUUqbjNdp+sqzRmDNxdc/AeO1iHqPhE5H4+I03JGVwh/UyVfsDAO/O5OvwnL41oJqAvKAxADKHUTIs+XvLFZlEGy21g4Xz1/ne/gp1/ZFzbwfTvDvujH/sde8mqTuC5BJ03eqRzhfPKZbfE/2ppgGJdnauhhS7Ce3NDjPBZryw+bdZ6sDya1K5+OTPxwIbZw1p4aCLkuwOnFV2C9K8As4PcqtC+iOk2IoOqv/7l84IcsoJ0cqszfsTtiubx15RM0McX6UsFtat54Ue7AAct8zbQTBskx6h5blHQoLx6yBbDF7pMAV6Y8rllqhBWQ79qb7nkjL99wFQ9h8KrNL2Q14FlWqbwPNff7xesR9O3zUfQPBCwpA9OiyN3AETiv96xQaw/fJM9d2lpOlBrsrU8Knh+yHUqwuOXniMKtRMGc5D0xEk1wjUiFXu3dIwTBltI3PfXNzWxPy37ASkQYRRXdxK/dHJbMiTZbTqBUP+e9MzhpstSv9dV6aBPNKn9tXMmb3pUZzE5JRaEn2O6/iirKAXkNTFjU5MxrJWNJ6uuX+O8ATqTD+CiWWYqR6FxdZDLnSwctYiGK1GE04O+auJxoSe3en69UujDN0QYnlwgdZB/CBRVHhdF6SdtMwT7fgX+c0aqSdEV+169ySCwdEVOumk/moVfq6C4LvMbHtY+qAP+mdkd8jbjgmpAfrWrfA1LyZXIp5Bj1aWhhLZ8QKvUVJouody9xerdrgMdYtHM3u8vD0T/Iv19NQC7WBCE+HY29AfQTa2BGAaIZiqya6nemVex4XE7IeCDrTWPbU5dmb0QRj+8GPY1OnoS0FA6bMbq/KbMlc38kDya+Y/ANVG0hz7FeCKCtnDCrbYB1YG6xZfi18b1X+Lt+y8fiY9zvK6pNky53xWo/+RpWKJQvQ4ZFryXFh+38kAXwLx3aDj/l6C8sh9hQSs0CNRDLIIZ712UE/0EXGwWP2W4tJ64gDCfcDQgOKEtXTHUbx+YB6Fz6hTppiF0qwITful1JRzKD4CuOriCFahrMm3Kwn3PVYxxcbn/Lm95adD94z2lykezL8XFPcMiJkJZYs+y3rquY/1Dm4Tqnrw55efG/Wd2V7Crdch6Q4+VF8C6k21xoTeP3ay+31vc7IJArHp00ABnAFGQHGEF0CtwSizC+ylXEL4mghRAaIpeDXgoHmVFMc0b0szt0oEuoLCSZyH/0SfsVMBmw== X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230040)(36860700013)(7416014)(376014)(82310400026)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 00:20:52.8196 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9a1554a0-64c6-4fdf-1a62-08dcf22f63b9 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0001A108.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB7677 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241021_172059_081103_14C02191 X-CRM114-Status: GOOD ( 17.20 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Jason Gunthorpe The iommu_copy_struct_from_user_array helper can be used to copy a single entry from a user array which might not be efficient if the array is big. Add a new iommu_copy_struct_from_full_user_array to copy the entire user array at once. Update the existing iommu_copy_struct_from_user_array kdoc accordingly. Signed-off-by: Jason Gunthorpe Signed-off-by: Nicolin Chen --- include/linux/iommu.h | 49 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 14f24b5cd16f..8105079ba5d9 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -493,7 +493,9 @@ static inline int __iommu_copy_struct_from_user_array( * @index: Index to the location in the array to copy user data from * @min_last: The last member of the data structure @kdst points in the * initial version. - * Return 0 for success, otherwise -error. + * + * Copy a single entry from a user array. Return 0 for success, otherwise + * -error. */ #define iommu_copy_struct_from_user_array(kdst, user_array, data_type, index, \ min_last) \ @@ -501,6 +503,51 @@ static inline int __iommu_copy_struct_from_user_array( kdst, user_array, data_type, index, sizeof(*(kdst)), \ offsetofend(typeof(*(kdst)), min_last)) + +/** + * iommu_copy_struct_from_full_user_array - Copy iommu driver specific user + * space data from an iommu_user_data_array + * @kdst: Pointer to an iommu driver specific user data that is defined in + * include/uapi/linux/iommufd.h + * @kdst_entry_size: sizeof(*kdst) + * @user_array: Pointer to a struct iommu_user_data_array for a user space + * array + * @data_type: The data type of the @kdst. Must match with @user_array->type + * + * Copy the entire user array. kdst must have room for kdst_entry_size * + * user_array->entry_num bytes. Return 0 for success, otherwise -error. + */ +static inline int +iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size, + struct iommu_user_data_array *user_array, + unsigned int data_type) +{ + unsigned int i; + int ret; + + if (user_array->type != data_type) + return -EINVAL; + if (!user_array->entry_num) + return -EINVAL; + if (likely(user_array->entry_len == kdst_entry_size)) { + if (copy_from_user(kdst, user_array->uptr, + user_array->entry_num * + user_array->entry_len)) + return -EFAULT; + } + + /* Copy item by item */ + for (i = 0; i != user_array->entry_num; i++) { + ret = copy_struct_from_user( + kdst + kdst_entry_size * i, kdst_entry_size, + user_array->uptr + user_array->entry_len * i, + user_array->entry_len); + if (ret) + return ret; + } + return 0; +} + /** * struct iommu_ops - iommu ops and capabilities * @capable: check capability