From patchwork Thu Apr 20 07:47:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13218043 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 947ACC77B72 for ; Thu, 20 Apr 2023 07:48:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234162AbjDTHso (ORCPT ); Thu, 20 Apr 2023 03:48:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234149AbjDTHse (ORCPT ); Thu, 20 Apr 2023 03:48:34 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2049.outbound.protection.outlook.com [40.107.102.49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6FE7344B9; Thu, 20 Apr 2023 00:48:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Y7rIhnEzKaYvtHfOWezEpnAIFjKFZG7OmAbpusbQnfwxZpqtDawgrVDFmuhQ3fBUd68ylDkC2vyjuMeHeAgZETLS3vGPnoZJAtIq94hbtJSWUE3cDeAzLeYgYcZeCYzc6ZLh/bXsZZOTwePUi44wyDyuq7a0TahhjdGsctBZ9dw/W8Kkqc/HhLURJuDTFx9cIon/EqtCs5SOXhXIlHjZHHh/kutHcIJlq58SgMHhOK1Gt0R4sJNFYBbZixCOwm85t1+QxulGhajHteP5G3KbD13FKlDUuhgzkEc5KLpe+OOALZrW0l/6g9ePWiosJeW0KxQ+7YftmnL3mFcrd5LYGw== 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=Hf53zDrbQHzWCN6M+4n/hVt2zAAGtcY0Lvjjgb119bI=; b=GNquYl8BjupPEROx2Cl+SquGfK8XV/db7HwJUnqzFADyXUS4+qaslGe6CVls5VvM56pi//EAUln9s4yfjsVL5GGHn5U/11GhoMFdLUzJah6cnhPp1NfHwmpAf4tRLoILg9jCFqKniMMkh4sncPIZtFfm7PJem3AB8Nk7atUOWePInsymSOFd8XMGo8F+12YjtYnyMm2pFxwFrW2kehYE9rF/6jnj1kKWVnh1kGyXlKRIhBBC96qvFiY7SzrK1VjTrfANZ7L/k43S/jtFZYfW1V1ATARsgFm67zHwJhpLMTmUd2Cre8mcJT52yskvLj3zEyVeU3u+II3+Grj5IkUaDA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) 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=Hf53zDrbQHzWCN6M+4n/hVt2zAAGtcY0Lvjjgb119bI=; b=GRqKCDddx/iX1Yxpi5IiXixwNksDRLKcQQkOEOJGz0ANM362Nbaf1as6dYiEWBi2vnvuXP/PC+rrwd6ZYt4olcd5lEu5DM7CldxxwwTDi5pU+Q/eJx9ArXRlg3AUDU9kJH1bO2GkYX4F7T5rD3J4DMi3qy70ZuFMxNjAW+w+k7QzWZZg6qSRrKiRWVpTEgZ1MekCjvhKvohxC32/YJRKUU7NCWsumRSLTGLNu1/6H0lucR+l0IMNsgD9xghJk4XGnv+VIsmZCFf09rQ1b6iFWcHM4kYImRFDYKwNLF6GBFCobF58pyAF7v+WGrASNX5vrZeWnbAtpKNovR19n0Tlfg== Received: from DM6PR01CA0023.prod.exchangelabs.com (2603:10b6:5:296::28) by IA1PR12MB6043.namprd12.prod.outlook.com (2603:10b6:208:3d5::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.20; Thu, 20 Apr 2023 07:48:29 +0000 Received: from DM6NAM11FT017.eop-nam11.prod.protection.outlook.com (2603:10b6:5:296:cafe::b8) by DM6PR01CA0023.outlook.office365.com (2603:10b6:5:296::28) 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:28 +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 DM6NAM11FT017.mail.protection.outlook.com (10.13.172.145) 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:28 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) 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.986.5; Thu, 20 Apr 2023 00:48:16 -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:16 -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:15 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , Subject: [PATCH RFC v2 1/3] iommu: Add set/unset_dev_data_user ops Date: Thu, 20 Apr 2023 00:47:45 -0700 Message-ID: <9361299fb200c1153868d8197c69cc67eef13149.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: DM6NAM11FT017:EE_|IA1PR12MB6043:EE_ X-MS-Office365-Filtering-Correlation-Id: eed695c6-8839-4875-1de3-08db4173a17e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EbHPfdHa2fYFgxhCv7AKFiQLgV3wMupwc0L/iWxH+kYGhTcdgdP+dxKDhMNEOVuv2vdVbVG/RAlkqQCKIZO0HHVR5p7jljXE9gkR2e5YIcSX2REY+j0+/sKABKpDA41l64+Sofutiialhpjnteg+bTNMxu7Z33XB0KkLVKe6g+VNyItRNNOvAjYWmtFFU5fCBNIcBHGVj1XWHJbK+fTVGQZEEWEIj5/MOqSTH+1heuL1dw7QMsbUFRfNofJcCeEmBCkkU5PAVnUd2a6wAxIkFAox44p+GoJqL7GhGgATxSPYw2EBsF7YRVWyClQIxfN4Nnkfv92JROBaZtd3ICnVba5zXJOSNF1YOZQMu+GBGhxTrWXWECccXGAmojKzibP/zXXr1LeoAWf5AQFCgR+eGrfn+grENwxGVVdtqWbXRZLaPn2hSuXKs/8mL5Az25b+9o43MRvbmy6pBPs12DSSWq2vHtptxUalF0jXtCSKIj0v6DJzSMx4wAZ8+CNXxVDa5fOfSDrm7NfXfyFVCMzBvDDEBNckTegHAv4SolyCXoZ+qpN57jE9BbYxYgQ2ih44Lv509+bdr20AcmJLB4e8uA0WRBQPvIij4FPaj1OR99rrThZ765M3HExbcbR6a2+Vx/t6SWyUOZCyDfMffnnslqrqOII9u5HWsP48A9xhyBpo3YZOBGMmjDYV/0dTdXHuxPt8LYA/UXtGCNY2IEffMy0hni1clT608ku8jcWgwythCFTIuWlV81L536v4PvhD6LZojp/tgxetozqVhKtdsHrD+xOtOJMStA1VMBQhaaI= 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:(13230028)(4636009)(396003)(136003)(376002)(346002)(39860400002)(451199021)(46966006)(36840700001)(40470700004)(2906002)(34020700004)(2616005)(36860700001)(40460700003)(478600001)(6666004)(82310400005)(86362001)(26005)(7696005)(186003)(36756003)(40480700001)(336012)(426003)(41300700001)(82740400003)(7636003)(54906003)(356005)(8676002)(8936002)(47076005)(4326008)(83380400001)(70586007)(70206006)(110136005)(316002)(7416002)(5660300002);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 07:48:28.8092 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: eed695c6-8839-4875-1de3-08db4173a17e 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: DM6NAM11FT017.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6043 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The device behind an IOMMU might be used in the user space by a VM. So, it might have some user space data. For example, a device behind an SMMU has a static stream ID. In a virtualization use case, both a host environment and a guest environment have their own Stream IDs. A link (a lookup table) between the physical Stream ID and the virtual (user) Stream ID is needed when the host handles the user cache invalidation commands. Add a pair of new ops to allow user space to forward user_data of a device via iommufd. Signed-off-by: Nicolin Chen --- include/linux/iommu.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 5c99aeaccd1d..c87f0f1527dd 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -257,6 +257,9 @@ struct iommu_iotlb_gather { * @remove_dev_pasid: Remove any translation configurations of a specific * pasid, so that any DMA transactions with this pasid * will be blocked by the hardware. + * @set/unset_dev_data_user: set/unset an iommu specific device data from user + * space. The user device data info will be used by + * the driver to take care of user space requests. * @hw_info_type: One of enum iommu_hw_info_type defined in * include/uapi/linux/iommufd.h. It is used to tag the type * of data returned by .hw_info callback. The drivers that @@ -303,6 +306,9 @@ struct iommu_ops { int (*def_domain_type)(struct device *dev); void (*remove_dev_pasid)(struct device *dev, ioasid_t pasid); + int (*set_dev_data_user)(struct device *dev, const void *user_data); + void (*unset_dev_data_user)(struct device *dev); + const struct iommu_domain_ops *default_domain_ops; enum iommu_hw_info_type hw_info_type; unsigned long long hwpt_type_bitmap; 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: 13218045 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 09B6AC77B72 for ; Thu, 20 Apr 2023 07:49:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234177AbjDTHs7 (ORCPT ); Thu, 20 Apr 2023 03:48:59 -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 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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); From patchwork Thu Apr 20 07:47:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13218044 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA393C77B73 for ; Thu, 20 Apr 2023 07:48:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234171AbjDTHsz (ORCPT ); Thu, 20 Apr 2023 03:48:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232726AbjDTHsf (ORCPT ); Thu, 20 Apr 2023 03:48:35 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2079.outbound.protection.outlook.com [40.107.92.79]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A57246A4; Thu, 20 Apr 2023 00:48:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YrgLksOJBA+x4rIK5bVZoZrPW9V8HHBerPKKPgJ4xFvmDfz+tjC9a+XPBSMgLpR1ZnKzOWB5fxPrc8bh3lcG2rCiN2DPw0I7kPrDMk11vzzdkBntD69JKaBJ8od6a34P8tXKrqb4aTEHPPnjAuAWDFy1hpvFSWcJPavZvCW/Wnc+qmYNb4ExrJ+4hfeXoPNis0YIkI+eyqgYoi1yfi3YgPfW/Op3O0RGB60BUIx4ret6tDoQzYmkTO3NXjfzyXpWA6mZuTRupG0VumprO7ZTVHvk5qnVll6Mwz0+Qc9m+lfgG8Jg1szCZISWlspIxpnj81ceJ5Y1c+jXg4nsAcXlDw== 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=SB5IaaRV6DtyBHNYRvQgKfnE6ElEe6c/CS9kqoQICfg=; b=bkwCjBd4Tbmoxv9Vqhfc23g7gVgSazTVCfbYJvolixVjCrrtwu5Bo/iD8LhW2y/1OGkVV3Eqq8xz21deRsymNryIf4XpR/L+1edUHcTE6aSX4XaMHkz+3UJ0e9+07V2wMoeb2nkPW3pLcl1W9e0OM5nnPDkQjttA22sld72tbKEQtN6LbI/7sxpx7DkxDOuSgex9zb7bRPW52Dc66d7pwNl6OkovKE9OE9s7h1g/YQFMizmG+SkJe+nQVr95NseIGBmQwcegJehenxrvur3fL1pbzTGWEbH0JhZ23p7Ra1eYas3o1cmGAHSCigyzsdjI1zEeHo/1IaY0Zmztf1Pa+w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) 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=SB5IaaRV6DtyBHNYRvQgKfnE6ElEe6c/CS9kqoQICfg=; b=Y+K0fNSbGmKNOTJ2LSfunrGdJkpVcyFlyY+VDs1qOjOAPQpHmZzC51jFLzYd+QrSy3lQ88suJN1yMvNzfUgOtQnfPUGa0K9dREDRt4AtnLQ30f25RGWeUhF2ROTAGGM78uA0y3C/QYlEFBsVzmzDC2mK8AdV7du21d2Ht4W3dEq1FhGdNPkSbeGzCDomA9gmM2ZGwZiEI5Tl6Q8FUjS0c5OAnIRdJjfDHs9qXxnE8Uw6sJZZjdf42YhfQnWHMoAv8k/PEegqrnKz+pF76pY5aBBpLd/ApX/0KE/R+uF8chY2ctTehP0nFjAOcmJqiZTgUJjIq745Q1Wr1/Ednc1nLg== Received: from DM5PR07CA0063.namprd07.prod.outlook.com (2603:10b6:4:ad::28) by MW4PR12MB7166.namprd12.prod.outlook.com (2603:10b6:303:224::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.31; Thu, 20 Apr 2023 07:48:30 +0000 Received: from DM6NAM11FT022.eop-nam11.prod.protection.outlook.com (2603:10b6:4:ad:cafe::43) by DM5PR07CA0063.outlook.office365.com (2603:10b6:4:ad::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.24 via Frontend Transport; Thu, 20 Apr 2023 07:48:30 +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 DM6NAM11FT022.mail.protection.outlook.com (10.13.172.210) 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:30 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) 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.986.5; Thu, 20 Apr 2023 00:48:19 -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:19 -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:18 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , Subject: [PATCH RFC v2 3/3] vfio: Add dev_data_len/uptr in struct vfio_device_bind_iommufd Date: Thu, 20 Apr 2023 00:47:47 -0700 Message-ID: <8232c5ea073d793d277f45036a6e61e0849b4202.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: DM6NAM11FT022:EE_|MW4PR12MB7166:EE_ X-MS-Office365-Filtering-Correlation-Id: 3bdcb85b-4a6b-438e-3f31-08db4173a25e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: vdJFSWdmjtZWRXhvKqQpXGrqN3rXQ4di2zuNB+hsd+Ej4WHJuKymMckpBC8j5NeYKBFxJeKFAfHieLBFxrEpPSL7PmddvFkWi/GqlAnnAtCTBEIg6ruYhftZhPijgDjHHMNYTur1vqPeiwlsqULC1/ExAISPDFrqF0lwHY1NnDio1HRvWT5zmaU+mQ5beJP62GL4Xc67u13dl0scY5/JTAkGnfjFuuETVugBlKKp4yfyA2ak1vJ11SSkdTMt84B7751tEeiC+FQpv+LOjr1siIC4oYyHwl0xWzXZCtrh2j/EbU900uBBkAPh/6Ob5PmHmnvBTp+FTyu7a6g5lhomdojheBcg1mzWyUUKpQJruSMpkwhyrEIoAoHDx/sdC7TOkFJdGHKVIUE9xHDOZy0acMKEyIx+TExyAEFkdAf87bZdMt3FAPlXWJ8N4GEUTg01Hw0p4c2unYm5861TwDND7TfRtsOPNAM8dG8KN8H1g3LNZYSU/RR9Q8pstfz3Ya5JmoxJyrni35fZlwjXQD8tK6kIOcEo6D6cjgIryJnmqN0i7D7iWFr23pj1zGsCMnX6dc5heB9+EFkgF/aLyTA/GaQEtK3DEVgjCXXIc4ysO4drfZYslB4WEepoGlPV8TMCusLf9nGb0mVQH973Ka8OUyVbdYM/OkpklPFtOZWSlOwfw029S3WPl2bngKKGX3y08FBujzTK+I+50NmhpDfIsxgYjxdhlautexde8nST+tEGt1qY/8jPuHM/h8pzIvaDiXyXnlDjdz0bJID3+zbYWNuBlfq8oHg0ZfjdO09XuPI= 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:(13230028)(4636009)(136003)(39860400002)(396003)(376002)(346002)(451199021)(46966006)(36840700001)(40470700004)(36756003)(186003)(26005)(34020700004)(36860700001)(40460700003)(478600001)(82310400005)(6666004)(40480700001)(110136005)(4326008)(54906003)(8676002)(86362001)(316002)(7696005)(7636003)(70586007)(8936002)(70206006)(356005)(82740400003)(7416002)(41300700001)(2616005)(47076005)(2906002)(83380400001)(426003)(336012)(5660300002);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 07:48:30.2781 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3bdcb85b-4a6b-438e-3f31-08db4173a25e 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: DM6NAM11FT022.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB7166 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This allows user space to pass in an iommu specific device data with the VFIO_DEVICE_BIND_IOMMUFD ioctl. The data is not mandatory. But it must be provided if a non-zero data_len is passed along. Signed-off-by: Nicolin Chen --- drivers/vfio/device_cdev.c | 19 +++++++++++++++++-- drivers/vfio/iommufd.c | 13 +++++++++++++ include/linux/vfio.h | 2 ++ include/uapi/linux/vfio.h | 13 +++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c index b5824179cd48..70241d9c0fa9 100644 --- a/drivers/vfio/device_cdev.c +++ b/drivers/vfio/device_cdev.c @@ -104,9 +104,10 @@ static struct iommufd_ctx *vfio_get_iommufd_from_fd(int fd) long vfio_device_ioctl_bind_iommufd(struct vfio_device_file *df, struct vfio_device_bind_iommufd __user *arg) { + uint32_t mask = VFIO_DEVICE_BIND_IOMMUFD_FLAG_DATA; struct vfio_device *device = df->device; struct vfio_device_bind_iommufd bind; - unsigned long minsz; + unsigned long minsz, datasz; bool is_noiommu; int ret; @@ -117,9 +118,23 @@ long vfio_device_ioctl_bind_iommufd(struct vfio_device_file *df, if (copy_from_user(&bind, arg, minsz)) return -EFAULT; - if (bind.argsz < minsz || bind.flags) + if (bind.argsz < minsz || bind.flags & ~mask) return -EINVAL; + if (bind.flags & VFIO_DEVICE_BIND_IOMMUFD_FLAG_DATA) { + datasz = offsetofend(struct vfio_device_bind_iommufd, + dev_data_len); + if (bind.argsz < datasz) + return -EINVAL; + if (copy_from_user((void *)&bind + minsz, + (void __user *)arg + minsz, datasz - minsz)) + return -EFAULT; + if (!bind.dev_data_uptr ^ !bind.dev_data_len) + return -EINVAL; + device->user_data = u64_to_user_ptr(bind.dev_data_uptr); + device->user_data_len = bind.dev_data_len; + } + /* BIND_IOMMUFD only allowed for cdev fds */ if (df->group) return -EINVAL; diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c index 16d6aac06180..0b985ccffcbe 100644 --- a/drivers/vfio/iommufd.c +++ b/drivers/vfio/iommufd.c @@ -91,6 +91,16 @@ int vfio_iommufd_physical_bind(struct vfio_device *vdev, idev = iommufd_device_bind(ictx, vdev->dev, out_device_id); if (IS_ERR(idev)) return PTR_ERR(idev); + + if (vdev->user_data) { + int rc = iommufd_device_set_data(idev, vdev->user_data, + vdev->user_data_len); + if (rc) { + iommufd_device_unbind(idev); + return rc; + } + } + vdev->iommufd_device = idev; return 0; } @@ -104,8 +114,11 @@ void vfio_iommufd_physical_unbind(struct vfio_device *vdev) iommufd_device_detach(vdev->iommufd_device); vdev->iommufd_attached = false; } + iommufd_device_unset_data(vdev->iommufd_device); iommufd_device_unbind(vdev->iommufd_device); vdev->iommufd_device = NULL; + vdev->user_data = NULL; + vdev->user_data_len = 0; } EXPORT_SYMBOL_GPL(vfio_iommufd_physical_unbind); diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 46b313f8bfaf..e4bb63801472 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -68,6 +68,8 @@ struct vfio_device { #if IS_ENABLED(CONFIG_IOMMUFD) struct iommufd_device *iommufd_device; bool iommufd_attached; + void *user_data; + u32 user_data_len; #endif bool noiommu; }; diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 17c5a1dadd08..9cec823c2829 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -212,15 +212,28 @@ struct vfio_group_status { * as long as the input @iommufd is valid. Otherwise, it is * meaningless. devid is a handle for this device and can be * used in IOMMUFD commands. + * @dev_data_uptr: User pointer of the device user data. + * @dev_data_len: Length of the device user data. + * + * A device user data is an iommu specific structure that must be defined in + * the include/uapi/linux/iommufd.h file. On some platform enabling the iommu + * nested translation configuration, a device behind the iommu, while working + * in a guest VM, needs to provide the host kernel a certain virtual ID in the + * guest VM. For example, ARM SMMUv3 requires a virtual Stream ID to sanity a + * cache invalidation command from the user space. User space wanting to pass a + * user data must set VFIO_DEVICE_BIND_IOMMUFD_FLAG_DATA flag. * * Return: 0 on success, -errno on failure. */ struct vfio_device_bind_iommufd { __u32 argsz; __u32 flags; +#define VFIO_DEVICE_BIND_IOMMUFD_FLAG_DATA (1 << 0) __s32 iommufd; #define VFIO_NOIOMMU_FD (-1) __u32 out_devid; + __aligned_u64 dev_data_uptr; + __u32 dev_data_len; }; #define VFIO_DEVICE_BIND_IOMMUFD _IO(VFIO_TYPE, VFIO_BASE + 19)