From patchwork Wed Oct 9 16:38:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13828853 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 E205FCEE332 for ; Wed, 9 Oct 2024 17:15:26 +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=n5W6GPvTFiR9C5kTo46PFn3r++s62U7OGDx4eFcBIAQ=; b=f9bIGXTPT1mdOjsHFc7vXeYlHa AhdzNIuQYkZCFOdl8FyQFdADQdSHDGcs4uuHBw0Bdw3KxWzacWsNSuZEjIK7oyK/GUrS5YWIw5mqv fbGRXp1nffYoWfoxvAuijoVNptfzDNaoosaXRXVH5qd2fVYFXxV+TGdwCeNyO6ETG8WWIp0r1xd1i PsNmveyx6LCjXPteMoAF38pp73pqloUeMWw7TjQSMwjPFaj2TZ6JiWe0bPbEAv7X1i0NPHkpHSiZw JxolRK++69q81Icb1GUA1bV722Pv3HwDPLsAaB5XdWOUGp3Hm3s3zmsSOe/7djTzefeoadSfZiAUf vgW4BMww==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1syaHN-0000000A8f0-3z0K; Wed, 09 Oct 2024 17:15:17 +0000 Received: from mail-dm6nam10on20601.outbound.protection.outlook.com ([2a01:111:f403:2413::601] helo=NAM10-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1syZib-0000000A0bE-46wy for linux-arm-kernel@lists.infradead.org; Wed, 09 Oct 2024 16:39:23 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=JvrS+iG2v82VUkTmprdialp9EXIbCS8w4/ez4HxBvKp9MQx0eyD9oyfJwP0Q8Z6czhyijsYq3HjmuAoq8Cf9JiD+NS5CfY+IR7yIzynbyk46RvwDbnspE6+GIG8gcFMkZw2xNyEAfrvVhQAgYikM9jtNWnpYFWbcK0wseCqfyY0r/JTpSrq+kSg6IqbOmHihEHvf5ZAE1ESwKULwNl9t72PxGJGBVO4cXumPXPIDQqV6YCrTnCtZBddtkl5GNfOjg40R6ThkGaHzOZsT/3Fb5BDhQwBlzEZCfEGzgjRKVTJERT8po2sBr5CSHr6qFVwalnQkVkI/GwoppwdnrK2/Xw== 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=n5W6GPvTFiR9C5kTo46PFn3r++s62U7OGDx4eFcBIAQ=; b=aMa9wFU9I6o42uSt32TUyQpTDZIw37RLKka5EVlA+FP7G2B0YBQNHJ2WJVvLLqTbqJ8ljitbAFwUAlWTKvBzUj4Jt/h1b5aDjn5H/QBsX4H/lMaBuZPwinfnx2irzJ//Oan0P79Ve3inZw0CGoDhWQGC8varUQDfccq+zI6qR2zVNVHXbahLan3HBOsTabUKQppUo8seVzdGGThUUEh3pG9sIfXaqx53Jyq4hnVsyM562jdptuSIBCYtcUjVRQNZ8U6PMrMP+VFezHC7rBKWYdOENKtvgwILxUeLEKTBgUpeqGzh5CToQtr9wGMYbRiMmfg3MJNNUPX2rrOnCbHlGg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=intel.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=n5W6GPvTFiR9C5kTo46PFn3r++s62U7OGDx4eFcBIAQ=; b=UK4+gB5IDRK8dp77bjjuyfwrja2NvkDRyZxV4T9i8WX3cMqyvinQd7vt3S8uOT2ClhuoaYhXOdh2wln1RjZ2ge+aOi0+Hlr++m9uRNHELElGjk8xs/R/LWgoo9h7v74GDD5MJk1mYboDHPJtEwinVHCvON0ursssYdFGB/SLzarOAE0ONwAVPJUcrkJMS0s4tOMTglOcGOpuNdkJ3QqEc6oN92Zkw5bpBj4I2ymrwIqWAb3ak5b+dZaFazKELzmbcjZcy7TcN8eDljjqA3DH3GNrqBQdyKI5memull8cquJ3Wg7jlx3tS/Pm/yxa6UZANjxRQSd9Ie92ay8VAz5Q6A== Received: from DS7P220CA0007.NAMP220.PROD.OUTLOOK.COM (2603:10b6:8:1ca::14) by MN2PR12MB4048.namprd12.prod.outlook.com (2603:10b6:208:1d5::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.16; Wed, 9 Oct 2024 16:39:13 +0000 Received: from DS3PEPF000099E2.namprd04.prod.outlook.com (2603:10b6:8:1ca:cafe::8c) by DS7P220CA0007.outlook.office365.com (2603:10b6:8:1ca::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.18 via Frontend Transport; Wed, 9 Oct 2024 16:39:12 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) 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.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by DS3PEPF000099E2.mail.protection.outlook.com (10.167.17.201) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.13 via Frontend Transport; Wed, 9 Oct 2024 16:39:12 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Wed, 9 Oct 2024 09:39:06 -0700 Received: from drhqmail202.nvidia.com (10.126.190.181) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Wed, 9 Oct 2024 09:39:06 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.12) by mail.nvidia.com (10.126.190.181) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Wed, 9 Oct 2024 09:39:05 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , Subject: [PATCH v3 08/16] iommu: Add iommu_copy_struct_from_full_user_array helper Date: Wed, 9 Oct 2024 09:38:20 -0700 Message-ID: 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: DS3PEPF000099E2:EE_|MN2PR12MB4048:EE_ X-MS-Office365-Filtering-Correlation-Id: c6188e38-f4cd-4ca8-73b0-08dce880e81f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|36860700013|376014|7416014|82310400026; X-Microsoft-Antispam-Message-Info: HGAFqYDaK7ZO6Z+fJKC/4CnKViWSzIsKx/opY87fclkkwyf9fP48FL0W8ENBff/67UNb6x0vWMPijJ3ggGxPWQQtRAuPEjJlBKmkeyoRuoJ6Ygj+yhceWOcA9ZVN2CM3i5yh7rgkAsrolnqgwnN90sdIg/7pu8yW7b32WK1r0WtptWXTwvr8AwocrWibaqVMk5Pptw5IiBfhGuUZt1SXb5eWXYOvydq96FpWlb7eg1Z+Ls8uL0ybQ1yrDHzvnGQONRuUxWu6mXNgVn8CFx/mbk1azdy1Dop1VigKbGh4lLv6spZKa+wWEmOGBe3KabqEhqZof+IpSVnGImE/O7s94oIVpqgafnoTPSMEFoFGVApHxjfKnkY5U2SETuGgUZUgGHLwIcDqeD53Lyn7RDuTQAXT4XuNhFpQNMCUo5eoPx8b4Hwj5hhnuj+MLZomtvHxdgoJ83bfS5Sds3fT5H6oll7saHNI2zMABEUvV8wg6bLM5Hwe2+fM+fZVy5IeXFbni4atmdS4D05uIImc7T0Drl5YZ+S6QAZ1/rpqF6YiX9ylt0oTWnrlMbNON+QsXxStWHSv9jLEOTeqqlpHM9E29NXMpAb/bPquyY3uNfIpjpNnQow3ro4NJit1nB3zFLsLiqnj6HNSqA1If8rc8ImNJ2mqSq1znQVt85bG1Uhurb2ilba7/NIhVo2rZIzcNCIrkwGiu3S7A/dXWxtRstW4QsjlHPtFAtVSSC85XBbomOog+Fc8MxYj/92RGfMNYIPVmAqqp+jdFxWgXaxreMQ+gzm6RMfisNGo5ZV4iik+ZFXxENbqQ4VXn0vrmWxYRs3FqcHUESfaRGF+tjYTvGkNJ8at4sa186medla+ZfxdVFeGZwX4zwm7BSsyrq5aeVWXS+Nf5RSvn4YM5Gh2I24sts+7jxinLa6R+AoPv9AIqYpw0XktK3s1dWnJoacAGJvuj2zbU7KjKLK26H9t/IGChf6qscu5o8vYQ0WoFluOoACkA1bKjkmmDxgU16wTSsXDimmj8T85BANlUAQBHdmiOtoBrx2Txezx9YvVONwNCuB5LUpIUmdD+ApHTmWHSfxBGhS3WNGrXJh+XvjCqoRKfSqOVd6PjiGlHeV6yUm3WNsXZc3gJrkIl0La7JeGJS+pt6D57abJtTj5fw3eFjVlrsjs8g/DTpoarjfpqL7Y8OYL7G6Kr8xsgGTTXnblGfET22KZ23C7CBqH1OHhERXHUZKRKijvPtIt8K8tF4iywdvq2NCzAOemkJFnPUuTLQqd1Mo+N856ay+cFDtKK58jkC25OxrnIILyI/6H86tZoqsNuANsu1VLr+TNig0ipUmZwINcDbfAyTa7xd9s3WqSRj0PMNCXgUpgmNs1z1fOtdm+tZnnSzb3PI4OaZkR8prs X-Forefront-Antispam-Report: CIP:216.228.118.233;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge2.nvidia.com;CAT:NONE;SFS:(13230040)(1800799024)(36860700013)(376014)(7416014)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Oct 2024 16:39:12.6194 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c6188e38-f4cd-4ca8-73b0-08dce880e81f X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.233];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF000099E2.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4048 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241009_093922_105439_3A93EA77 X-CRM114-Status: GOOD ( 18.24 ) 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 1de2aebc4d92..c980fd0e2174 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -494,7 +494,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) \ @@ -502,6 +504,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