From patchwork Thu Apr 20 07:47:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 85770 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp159550vqo; Thu, 20 Apr 2023 01:09:56 -0700 (PDT) X-Google-Smtp-Source: AKy350aEP59hsG5KPCXV4Z2oyu8vrQN3/AO/glp4SUgKBvEMhSUIZ2i8LxeA7zSsF8UasAcZZx4K X-Received: by 2002:a05:6a20:3945:b0:f0:471f:4aee with SMTP id r5-20020a056a20394500b000f0471f4aeemr1797808pzg.5.1681978196296; Thu, 20 Apr 2023 01:09:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1681978196; cv=pass; d=google.com; s=arc-20160816; b=VlzoSmeyjVx0Uy1B5uTffhsnj/WTxRu+8ghAgw3qw3+t0hhXIKJOZlLXJ9GfyiErLR 2Sqv4ozOovq24PWGb5W+pOjyaSyyUYJcb4BozVlN3+FLQeKtynuyol3JfniCDTDOfJrc Bhy3rjdrt2dsqcUSZUI1UArH7WkCv7BaSRM3mtK2tH7l3MYfxNhWAi81SplboqN4X8UB yPrDy/VeqIMx+y7llepE3CTv94TKjVmxkdwzkCJwX4QEp648ksgaQr0o4zqrCrXjg3k3 98/o+b/9NTpPhpnEJHSjI+D6lqMNSGRgz3yyksWoxbHH8kpeQbsrvLXLmdS2uEhTmD8L i2eQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=BUU49S5Qxv2DN+8BkUjn6g2JPJVJ8Eq13zKQh4ZygSc=; b=O2XDRwHvYvPVX+Ag/c3LBsOhe00RlFfNaI8p9uw1c6zVrRSDvCWfNO927O28/9z2YL BlRIRnvEJnAkiyqi4N6So3ZUSbkTiqlbwxsODk3G8dIkChSyCAX/cCLgTaENtHE7KJx0 091RUKTfCVjzIzhTkJx8kigb2EZqORkSPVWsVQS/zzgL/+jCrcz+Wwoim05VDVbXetz7 /1VBx1d8raDWrw9JM4PSUM9op10k9MyYuqSubUATiuozhmJDuzBV5KIMxAZ6cXE0ymif CxKir2IJUHOBp1NMeEv52z6LKJrPBEPLIgT0ZnVct7Xh4/UcmLp2IoFcFnYM8kKKnWKc fz4Q== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=fOT1UxmE; arc=pass (i=1 spf=pass spfdomain=nvidia.com dmarc=pass fromdomain=nvidia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z11-20020a6552cb000000b0051942a23eefsi991287pgp.533.2023.04.20.01.09.43; Thu, 20 Apr 2023 01:09:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=fOT1UxmE; arc=pass (i=1 spf=pass spfdomain=nvidia.com dmarc=pass fromdomain=nvidia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234195AbjDTHtJ (ORCPT + 99 others); Thu, 20 Apr 2023 03:49:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234154AbjDTHsk (ORCPT ); Thu, 20 Apr 2023 03:48:40 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2055.outbound.protection.outlook.com [40.107.244.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8716446BB; Thu, 20 Apr 2023 00:48:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ee97OuntGs10m+Pl8S81TZZ1HjJua3lwDsW2pvIyXQ2S2Mh51N3uMqN3zQkFalqU8Rc/cr1rYejf4Ysy3wcepbp+6qlj+IZDRpcUOiapZBapxgIFGxfB3oTItSJg3Xo+J02r134pHpFuiREeGtBLnFawb8Ph0gu84foiGxi6TMEYbYnP5jP/rTMr8nQpt3smVg2V+Unb6xWQh0gpuIGC7uVzgqOJGNgOt/yNShzk0tyKaRb4LMzJysyWr+eci2oPNe+mIKw4FSJJtXJfps0TG/tMSmQhWKpWzfNmTbVncUCV8l1cj0I+AHwXMiA2Gf9aaOX7KjzdKHw7/Fbf56IAmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=BUU49S5Qxv2DN+8BkUjn6g2JPJVJ8Eq13zKQh4ZygSc=; b=eYxUyldo3RImXQ4tLrk2fWKoFgOZRQlL+58oACg9G4tpMQL5rCCdkTB950I2h2YxtPMtOmARMtB/TIUZkGucMviBmPY06d4P4zq2w8s0cBFiSf4taZAUWhoG3J/EB9i8lg3LgycLjKzTWas+1ilWlPetLCJCmv+R7H2AwZbk0V5W3HddrNB37svLOfUw+o2qF+2Zh5rNRfSUNFDl7o9s819Zq/6WszGMttckIqqFR315xW4eFelyhTKdBZj0BI2ZxhS9aMIKtSwFchDKyH7xIMDwnT7QsdSalaE5w+S9bYU37SYmENpR5TAG1hT8YDXcLFFvMpJzJAoWlU4ucZL1Gg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) 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 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=BUU49S5Qxv2DN+8BkUjn6g2JPJVJ8Eq13zKQh4ZygSc=; b=fOT1UxmE/9f2AUTw0VVWMv5rnOb0MYfCUbufBM5K7LKP+fOQMmERP4fh7S7hhyUo0nSkB+/DuylEqfL57SnWYQ8T6j32L+aP9emCSr9g9xQj2o21CQ/s3uD37n8CATl5+tRQbbPHD/eoVOn8XyQxqebvONVUfYVBxVlG3OCHRSe9vWtX7AyEZoB0zCZWLM5r1ix7GcF7bQpsLhPqSeSMHQ3d9ApqX7Yagm3FDH7eU7x4xFG9pp4d1ICzpaL7xo/QyBQw/A7eiWt9IJBOaeWcjrUteYum9o8kvOaZ2GQ73bXWKGGuIzy0VBxVPIQlUuvolasaHIEUCAMhTqMYKI/+7A== Received: from DS7PR07CA0012.namprd07.prod.outlook.com (2603:10b6:5:3af::20) by SN7PR12MB7323.namprd12.prod.outlook.com (2603:10b6:806:29a::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 07:48:37 +0000 Received: from DS1PEPF0000E638.namprd02.prod.outlook.com (2603:10b6:5:3af:cafe::72) by DS7PR07CA0012.outlook.office365.com (2603:10b6:5:3af::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.25 via Frontend Transport; Thu, 20 Apr 2023 07:48:36 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) 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.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by DS1PEPF0000E638.mail.protection.outlook.com (10.167.17.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.17 via Frontend Transport; Thu, 20 Apr 2023 07:48:36 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.5; Thu, 20 Apr 2023 00:48:18 -0700 Received: from rnnvmail201.nvidia.com (10.129.68.8) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Thu, 20 Apr 2023 00:48:17 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.14) by mail.nvidia.com (10.129.68.8) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 20 Apr 2023 00:48:16 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , Subject: [PATCH RFC v2 2/3] iommufd: Add iommufd_device_set_data and iommufd_device_unset_data APIs Date: Thu, 20 Apr 2023 00:47:46 -0700 Message-ID: <93edbb5af7d11f95921eb10ba929bccde0917bea.1681976394.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: References: MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS1PEPF0000E638:EE_|SN7PR12MB7323:EE_ X-MS-Office365-Filtering-Correlation-Id: a0bde68b-f090-4e6c-1660-08db4173a60c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gwzHfYR5Ma92DiYNAWS3Ccmx0How0JuafH8h9fZjUT4zwrEMJ3qocqsgRyerjB7b7/mpAP38xPckvwmKPf0nXe6zUhud9BL5rzCfQ8+dt650FiiB8uybYpECpoEqQCk2J7F+Dn3IFgMd1uQm/oOWs9MijtL8JBH5VLR2C94qZalFqxjkTE6Z+MN26hKSvxo+DLJ6VR1NOzRkQGxGvlV9ivQFYXQS7JPGjAZPusYmIZYV9favlfau681j2tvGHOOyAKai6kvgIBlalAIgHrIT2xTA1xtDKPmBjmq031oa8SJK98S5NJ6et87F0y2Btw48uX+/roOo0crbD+tERb6VNnpG3HAyzvpqCKXgwZarhyjUdmGVZzJ9WRPpRgKrJXsh5QzANdVoRRmtwzSuFNvlaXV6/CRw2KB3u+rE7SDJo0j0vFy+wS0tMO6HTksdaBm1WYptvfSkRnAhP3eO/Ft1vwkWw63Sub/M89hXzpdZm/5sKejP1h27aTQlD2uIXAC5paOgiT2XLcvS2YMK6A7k8Ndxkrr8zhafq7EnDruSfkPr0sBkt2ZWxKJiLby+XDSA6iXsFUL327r9Y4x5G9yNljWichex9BL8ZOe7jOw24Tf4E47lUKmZby5VJKmLnyTNJ7+Elid+GHqwrZ20nWk4QBOesxiPfPAK7idgpWv66aiIPYcgySXFn4Msq/GhCEMIrWFLPTzwGe1FFavUdv49YAfDmKaNE4eP7OpzEKbLlO0AxbA2GF5Qb50dJra5r28s X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(346002)(376002)(396003)(136003)(451199021)(46966006)(36840700001)(40470700004)(40460700003)(6666004)(478600001)(7636003)(356005)(82740400003)(82310400005)(2616005)(186003)(26005)(47076005)(36860700001)(426003)(7696005)(336012)(86362001)(4326008)(54906003)(110136005)(70586007)(36756003)(40480700001)(41300700001)(8676002)(8936002)(316002)(70206006)(7416002)(2906002)(5660300002);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 07:48:36.4505 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a0bde68b-f090-4e6c-1660-08db4173a60c 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.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS1PEPF0000E638.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7323 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763681969114266474?= X-GMAIL-MSGID: =?utf-8?q?1763681969114266474?= The uAPI handlers can call these two functions to set or unset an iommu specific device data. For example, the SMMUv3 driver would get a virtual Stream ID using this interface to tie it to the device's physical Stream ID, for sanity at the device-specific invalidation commands. Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/device.c | 65 ++++++++++++++++++++++++++++++++++ include/linux/iommufd.h | 4 +++ 2 files changed, 69 insertions(+) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 16fdf4a16643..c4e895239486 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -610,6 +610,71 @@ iommufd_device_auto_get_domain(struct iommufd_device *idev, return destroy_hwpt; } +/* size of iommu specific device data, indexed by enum hw_info_type. */ +static const size_t iommufd_device_data_size[] = { + [IOMMU_HW_INFO_TYPE_NONE] = 0, + [IOMMU_HW_INFO_TYPE_INTEL_VTD] = 0, +}; + +/** + * iommufd_device_set_data - Set an iommu specific device data + * @idev: device to set data + * @data_uptr: User pointer to an iommu specific device data + * @data_len: Length of the iommu specific device user data + * + * This sets an iommu specific device data from the user space. + */ +int iommufd_device_set_data(struct iommufd_device *idev, + void __user *data_uptr, size_t data_len) +{ + const struct iommu_ops *ops = dev_iommu_ops(idev->dev); + void *data = NULL; + u32 klen = 0; + int rc; + + if (!data_uptr || !data_len) + return -EINVAL; + if (!ops->set_dev_data_user || !ops->unset_dev_data_user) + return -EOPNOTSUPP; + if (ops->hw_info_type >= ARRAY_SIZE(iommufd_device_data_size)) + return -EOPNOTSUPP; + + klen = iommufd_device_data_size[ops->hw_info_type]; + if (!klen) + return -EOPNOTSUPP; + + data = kzalloc(klen, GFP_KERNEL); + if (!data) + return -ENOMEM; + + if (copy_struct_from_user(data, klen, data_uptr, data_len)) { + rc = -EFAULT; + goto out_free_data; + } + + rc = ops->set_dev_data_user(idev->dev, data); +out_free_data: + kfree(data); + return rc; +} +EXPORT_SYMBOL_NS_GPL(iommufd_device_set_data, IOMMUFD); + +/** + * iommufd_device_unset_data - Unset a device's iommu specific data + * @idev: device to unset data + * + * This unsets an iommu specific device data that is previously set from user + * space, calling ops->unset_dev_data_user. + */ +void iommufd_device_unset_data(struct iommufd_device *idev) +{ + const struct iommu_ops *ops = dev_iommu_ops(idev->dev); + + if (ops->unset_dev_data_user) + ops->unset_dev_data_user(idev->dev); +} +EXPORT_SYMBOL_NS_GPL(iommufd_device_unset_data, IOMMUFD); + static int iommufd_device_change_pt(struct iommufd_device *idev, u32 *pt_id, attach_fn do_attach) { diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 6752c58226d1..4b3a70faa409 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -25,6 +25,10 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id); int iommufd_device_replace(struct iommufd_device *idev, u32 *pt_id); void iommufd_device_detach(struct iommufd_device *idev); +int iommufd_device_set_data(struct iommufd_device *idev, + void __user *data_uptr, size_t data_len); +void iommufd_device_unset_data(struct iommufd_device *idev); + struct iommufd_ctx *iommufd_device_to_ictx(struct iommufd_device *idev); u32 iommufd_device_to_id(struct iommufd_device *idev);