From patchwork Thu Jul 27 20:24:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13330702 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 C14C3C00528 for ; Thu, 27 Jul 2023 20:25:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232834AbjG0UZK (ORCPT ); Thu, 27 Jul 2023 16:25:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229866AbjG0UZJ (ORCPT ); Thu, 27 Jul 2023 16:25:09 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2073.outbound.protection.outlook.com [40.107.92.73]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14DD02688; Thu, 27 Jul 2023 13:25:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PBqTn31NYP5zyDmVy7sHJxxbxCBcfCiylutNyVGdwHl0ZQTzLNs755P36n+vHF/DsXgEm0QjdPIBHI4qrukC77yAGTd+X6+eQb4u59ip3xUXWYepzyIsnrEkVadpEGsD5o3C+xKDGSNFTTJbGY/xhU0pZEc0JEIp3HyL4/nK6gsZd2TvlNmBWXDC0qkUBJ/tFc5JduWtvKeGU1qdJYM6qihNH//bsRVUZ3I0Fu28JI5HNbzdqlNmkEq/nisJ4ORUL6ShxgoybVGL1G91iZA8M4Q6WIiE371JqgV4Xl3r71gcs4tIMUa00Sgxio4eU0ywwgONV1vB/g+dtB3PI5Q3TQ== 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=mUc06e5xx4kTwBKEnoGTOSehjX6v5imRbufbXUgPcoU=; b=j+9xxVsWZQ9Og6dfpE1LffT4SNNK2fz+kZV2zqwHUHlD8f3pJw9eSwez5/28h6SfCBsVInxyHmD+j8I1C50AIZR3l6wsbJ699biy0UCzTxlDmf/jAIKvuNIPurFvzMhSEwvuX87yoVM7YiXXEVJqmEBneFGAHtqVVh3+LCx2SWV+2g5Sv6bhGEBbl5r+pqt+sH9MUjMSHqVyXzvlvdaU4yo0KlobVExZeYDAfmUA4hHyLHQRDO4rkeCTjPg1A1g1bI6ZDb7AH+qH9acp1Uxcy9dvTWs3s3DGNSkID7G5K6f+Z9kOVTizTFozD0EtdTzyH1XQjpoHjEyEfxLat2Hwug== 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=mUc06e5xx4kTwBKEnoGTOSehjX6v5imRbufbXUgPcoU=; b=HMAvO2WimyySL6ACQ8QeYZ6YCg9rOXVOyjZcG81hGIuMtxNGraGLwnvlB4YYrJsyerL7JsqVvQH3tONg8ag567oIKMzzNIBG9dU6s+at9kUIMf3Eko9qoIj3MmWeaOxUZP6mcVBAxsMDdOK5XQpzOWmzSLgzI849cBfynfGi00rtXlYSixqnIkDq+3yg/pbpPRg4p5CHgHdW3cTX/IaPJ2xqkiWJWvain1CbtrQJcHjUXV0F0TalUnu2NDhtoNVfhKPciz7Qz4ist6KDogF5wLzSism/pGsqjBNI7j+vmIcysX5qICq90/DQGLQiyjbjRmm+TIvNWdY+99MjGY6Ygg== Received: from DM6PR02CA0045.namprd02.prod.outlook.com (2603:10b6:5:177::22) by LV2PR12MB5821.namprd12.prod.outlook.com (2603:10b6:408:17a::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Thu, 27 Jul 2023 20:25:06 +0000 Received: from DM6NAM11FT099.eop-nam11.prod.protection.outlook.com (2603:10b6:5:177:cafe::74) by DM6PR02CA0045.outlook.office365.com (2603:10b6:5:177::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 20:25:06 +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 DM6NAM11FT099.mail.protection.outlook.com (10.13.172.241) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 20:25:05 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) 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, 27 Jul 2023 13:24:49 -0700 Received: from rnnvmail202.nvidia.com (10.129.68.7) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Thu, 27 Jul 2023 13:24:48 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.129.68.7) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 13:24:47 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v10 1/6] vfio: Do not allow !ops->dma_unmap in vfio_pin/unpin_pages() Date: Thu, 27 Jul 2023 13:24:32 -0700 Message-ID: <85d622729d8f2334b35d42f1c568df1ededb9171.1690488745.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT099:EE_|LV2PR12MB5821:EE_ X-MS-Office365-Filtering-Correlation-Id: 75a4fe6d-af71-4ac9-1d1a-08db8edf90a2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +3TVDUsm9Cvvb5fY03/qivz5KhcM02XQgTaolmfv1LvaOnNtWaIwO5QyeM+ulohizXXlnNwx0kc1ZUrMOE0JDP+Q7yF8kfoTbUYohlq9cFeO/Ejtq46HVq2gShEUEpTDSihdbybbZkoWblXZVCx3G1m1TJpD9G2p+FzMkBA7s+NN+9aMNhgnoKPktudhMPgVjkdOVcNCrZY2EOIlI8L9lF9tonYpYjGA88gwgg/brsof/gr45uB3wtWpxM/uIJh8AHAw3l5w3rXJLoy+qkcLu7Ets30BKK7u9iQWJkoVc9PaJDfecwO5RIw6UREY9dUlVuRAHkNvigTGdn4jiOqSW1Jv0aZydedzkQIvOfbA/E5l7NL2OUAkoKWG5mkLaC1+gWOU4wPKgqoprWhf8a8pc87mebX8CAmpAHpPmBhICAmGxMEBYvtKlkCOjb3uTgu3MB36tZz8q9xEZG1+W/HNDUtj2YdCq4Z9xuCCj0z6se9Q+g9Y314qpkY2D0cXDS0Z11Sx+pLMUl7otO3/elnQTwm2kdBK+o+sFZn2DHUYGuYu4Y3uUYcVqXUdPrVxx0Z6ooHhqcHv+SBgx8j4jTJutGYQ4xYSp4P+35t0QDwD1Ke+dQqwW7hbmprKdpxQdN5zZFDr9y7i9y/dM143UUOmjb2bcK6vCcdwyYEO9CBYlFcDw0ahwDOTnLvjNMz5U7e+mX7F1bzWm6JHys9nGN5bBAAJvMMvFY+/6ijz83n/9GP0slFgP1AZsweCFIrXncy1 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)(376002)(39860400002)(136003)(396003)(346002)(82310400008)(451199021)(46966006)(36840700001)(40470700004)(2906002)(316002)(8936002)(8676002)(7416002)(40460700003)(5660300002)(41300700001)(36756003)(86362001)(40480700001)(426003)(7696005)(6666004)(54906003)(478600001)(82740400003)(7636003)(356005)(26005)(186003)(336012)(110136005)(2616005)(83380400001)(70586007)(70206006)(36860700001)(47076005)(4326008);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2023 20:25:05.7045 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 75a4fe6d-af71-4ac9-1d1a-08db8edf90a2 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: DM6NAM11FT099.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV2PR12MB5821 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org A driver that doesn't implement ops->dma_unmap shouldn't be allowed to do vfio_pin/unpin_pages(), though it can use vfio_dma_rw() to access an iova range. Deny !ops->dma_unmap cases in vfio_pin/unpin_pages(). Suggested-by: Kevin Tian Reviewed-by: Kevin Tian Reviewed-by: Yi Liu Reviewed-by: Jason Gunthorpe Reviewed-by: Alex Williamson Signed-off-by: Nicolin Chen --- drivers/vfio/vfio_main.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 902f06e52c48..0da8ed81a97d 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -1483,6 +1483,8 @@ int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, /* group->container cannot change while a vfio device is open */ if (!pages || !npage || WARN_ON(!vfio_assert_device_open(device))) return -EINVAL; + if (!device->ops->dma_unmap) + return -EINVAL; if (vfio_device_has_container(device)) return vfio_device_container_pin_pages(device, iova, npage, prot, pages); @@ -1520,6 +1522,8 @@ void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage) { if (WARN_ON(!vfio_assert_device_open(device))) return; + if (WARN_ON(!device->ops->dma_unmap)) + return; if (vfio_device_has_container(device)) { vfio_device_container_unpin_pages(device, iova, npage); From patchwork Thu Jul 27 20:24:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13330703 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 CA9BCC00528 for ; Thu, 27 Jul 2023 20:25:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232874AbjG0UZO (ORCPT ); Thu, 27 Jul 2023 16:25:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230109AbjG0UZM (ORCPT ); Thu, 27 Jul 2023 16:25:12 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2063.outbound.protection.outlook.com [40.107.93.63]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AADD272B; Thu, 27 Jul 2023 13:25:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EXCN+7A9M3DuYtq4ER3FffyHRKWHE6NqhV5t+kg6k28uhnr4umXcfFnQkRAPj5wBwvqmYf34sCgChcpaJevR8bgZJ5ygTN+omU8p8XWVye9VCmXA8DrQHHKcUFpZZzw7Tvfe2eYIjqav2DbjXcCAZ+OmLeIHNg4dn4hhQYDd6VrX6LDG3mUAbp34/EyzG821IzrVd091OBE5b/55Dhb4E0L/rrT+GprZM25iZr0rpBuMOaPtIYbA95Gm61chtxQWHZkcb20qnPAHB/RKxaigdDuPKyFiOv7q4cWvJ09/NK2aNkX9wrDSXBw+I4IRdZ5c33ad97xXQ5xg02Q3lP8LJg== 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=qZYDw7d4hrp0hCVXIirdg00pXN2Sgkz4tDdwzRwGfdU=; b=Nk2BywFSb4PWoVZylKoiyVDLyK2aBDHEGptf7hzAi95QRi8Xn1JfBAi7RQe5/Gm28SeodzINOe4zuGWU9rg8L+cASP3hEskWi8mMaYalBnWx1E9xOcxzAwxVKCN0u+ZwD8toBijkzjVqBpPd4iJXqHFqrcViYVGKPq1VbFuenZYKoeTcyqBqLGNJu7IvlkHd/p/w959TQePlBSXLC1XhtqZ1JGbOydVILnb0hBXBPciNEJqi8g2zw5X421I17SvVyxEkhMRa2hjm/P14mzceaGDWYMIApHznJ+418PRgSxzTlAw1wC+gjgktlaJQgr+2y5lQja2dlAFj8SnU6MOLcw== 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=qZYDw7d4hrp0hCVXIirdg00pXN2Sgkz4tDdwzRwGfdU=; b=oEgASsxtIUERFKoFT4dsAvtm60VhhSzDc5O2Q493G4mRZ2RRAWFK9464dgsOvmgBVLD1vFVSOg0gjRVLzCh+j2gGQ1aMxguuSEXb33p7TZv1MQFuioVWWxIgB3ABp7j2SjjY0VdbpX8CNRqtMO2yGpkCO7YPHlwrJ7hxXcShRE72suJwwsybF/Pbc4v5OIOEcPpPuG09EfL0n0xdtSOEXB17lFlTE3E06UW3m0QwscJYwfhYn0/QSXGnSZrD7GQ50bYnxDg5Zih4/V1ECJHHJIcobjNj+hZ2O+m1OgpUKEYHSx6VBC5GyzQtnIwvKPbB4oef3eiyYwkS20dGkevU4w== Received: from BYAPR01CA0043.prod.exchangelabs.com (2603:10b6:a03:94::20) by CH3PR12MB8354.namprd12.prod.outlook.com (2603:10b6:610:12f::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Thu, 27 Jul 2023 20:25:07 +0000 Received: from DM6NAM11FT037.eop-nam11.prod.protection.outlook.com (2603:10b6:a03:94:cafe::11) by BYAPR01CA0043.outlook.office365.com (2603:10b6:a03:94::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 20:25:07 +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 DM6NAM11FT037.mail.protection.outlook.com (10.13.172.122) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 20:25:06 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) 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, 27 Jul 2023 13:24:50 -0700 Received: from rnnvmail202.nvidia.com (10.129.68.7) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Thu, 27 Jul 2023 13:24:50 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.129.68.7) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 13:24:49 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v10 2/6] iommufd: Allow passing in iopt_access_list_id to iopt_remove_access() Date: Thu, 27 Jul 2023 13:24:33 -0700 Message-ID: <9cf230e73d40b1458dcbb116d1fcfe8c62058bfe.1690488745.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT037:EE_|CH3PR12MB8354:EE_ X-MS-Office365-Filtering-Correlation-Id: fb4fe828-79e2-4e2b-f76d-08db8edf914f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: jV/i7qUFFTldIVpx5MJuYUmgs4UGxtCfmA16e8Z1dO7SissVEk2jSAbBpEuyGNMQlJ4fMb/9U3VujAdKD9g3oBnjh2gklQbO7LfKi9zS5UGaYGZZoH+8DpU5PBAiEfU3qJRQr1wJ42Ii3N7qmhjxmatpUfZaz8I1G+dh1hUauatDyBRFSua/68w029M+o37nZjFbfEi8mW/NO4Kuk98jtIiy3EKz+9917hYLCsUL8f9eOB1JWND3aShC2echJesNklRdJeHwrCcuwtG5yFlQjg9OVwwS9g98IU1epbNtCpGJ4fEXb7TlunG5ju/Y+eCd6ITVVcNJJrNKZk8RtQz14JEEo3dH25mjTc9anFdNKtu2vupYf+cTvylTKeIqCE2/VEnL+AiP2nnelnvuYEJA7Es4tyYkdrUZklvVuNip3oznxWdiXv/Iu/jfBmpDUPohBCrtSqzRRpjkutYmzUyQ7Mfoz4Oa/NrP5LBq5My1GiZ87ikzkI83FBBeVhTD52fPqgJjPE+WhmKwJK9aJZnEaDxjzn9nRl1F+3Se8+lqLSPXJ75JMBKU2u5OA13QgXR/s3AqUlbJl3QNsDZOe0tPeyieuOamKh3JpkAg1JAYpXhX95ertmmSbb+jDdHV8gRPg6HWW07Q6ObbiU/vn7g5L/R3csQEOJIq2cUPPn5MhJeorXDQFCzQuYCtxcfUpu3gAQcj0Z+PfXnpR85tOu62XnVZ1h13qPcDZJh9GL/5yh5tIwNgk8lWqwW2yhfx0xhO 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)(39860400002)(346002)(82310400008)(451199021)(40470700004)(46966006)(36840700001)(5660300002)(7416002)(70206006)(4326008)(70586007)(316002)(2906002)(478600001)(110136005)(54906003)(8676002)(41300700001)(8936002)(7696005)(6666004)(40460700003)(86362001)(82740400003)(26005)(40480700001)(186003)(36756003)(336012)(83380400001)(47076005)(426003)(36860700001)(2616005)(356005)(7636003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2023 20:25:06.8439 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fb4fe828-79e2-4e2b-f76d-08db8edf914f 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: DM6NAM11FT037.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8354 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This is a preparatory change for ioas replacement support for access. The replacement routine does an iopt_add_access() at a new IOAS first and then iopt_remove_access() at the old IOAS upon the success of the first call. However, the first call overrides the iopt_access_list_id in the access struct, resulting in that id un-erased in the xarray. Add an iopt_access_list_id in iopt_remove_access, so the replacement routine can save the id before it gets overwritten, and pass the id in iopt_remove_access() for a proper cleanup. The existing callers should just pass in access->iopt_access_list_id. Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe Signed-off-by: Nicolin Chen Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 6 ++++-- drivers/iommu/iommufd/io_pagetable.c | 6 +++--- drivers/iommu/iommufd/iommufd_private.h | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 57c0e81f5073..7a3e8660b902 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -690,7 +690,8 @@ void iommufd_access_destroy_object(struct iommufd_object *obj) container_of(obj, struct iommufd_access, obj); if (access->ioas) { - iopt_remove_access(&access->ioas->iopt, access); + iopt_remove_access(&access->ioas->iopt, access, + access->iopt_access_list_id); refcount_dec(&access->ioas->obj.users); access->ioas = NULL; } @@ -776,7 +777,8 @@ void iommufd_access_detach(struct iommufd_access *access) access->ops->unmap(access->data, 0, ULONG_MAX); mutex_lock(&access->ioas_lock); } - iopt_remove_access(&cur_ioas->iopt, access); + iopt_remove_access(&cur_ioas->iopt, access, + access->iopt_access_list_id); refcount_dec(&cur_ioas->obj.users); out: access->ioas_unpin = NULL; diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c index 4d095115c2d0..3a598182b761 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -1158,12 +1158,12 @@ int iopt_add_access(struct io_pagetable *iopt, struct iommufd_access *access) } void iopt_remove_access(struct io_pagetable *iopt, - struct iommufd_access *access) + struct iommufd_access *access, + u32 iopt_access_list_id) { down_write(&iopt->domains_rwsem); down_write(&iopt->iova_rwsem); - WARN_ON(xa_erase(&iopt->access_list, access->iopt_access_list_id) != - access); + WARN_ON(xa_erase(&iopt->access_list, iopt_access_list_id) != access); WARN_ON(iopt_calculate_iova_alignment(iopt)); up_write(&iopt->iova_rwsem); up_write(&iopt->domains_rwsem); diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index dba730129b8c..8ba786bc95ff 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -323,7 +323,8 @@ struct iommufd_access { int iopt_add_access(struct io_pagetable *iopt, struct iommufd_access *access); void iopt_remove_access(struct io_pagetable *iopt, - struct iommufd_access *access); + struct iommufd_access *access, + u32 iopt_access_list_id); void iommufd_access_destroy_object(struct iommufd_object *obj); #ifdef CONFIG_IOMMUFD_TEST From patchwork Thu Jul 27 20:24:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13330705 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 293D8C04E69 for ; Thu, 27 Jul 2023 20:25:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229661AbjG0UZR (ORCPT ); Thu, 27 Jul 2023 16:25:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231601AbjG0UZM (ORCPT ); Thu, 27 Jul 2023 16:25:12 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2063.outbound.protection.outlook.com [40.107.244.63]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1CB426BB; Thu, 27 Jul 2023 13:25:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Q1v8TmRkgu0PWYbirConEIUWTnsL3dEW6dqfZGH3TGN/31n7xhim3YhOe08Ez+/VnH1oI2wx0qHmwG1DttYeJ7/8lu77QPaSSwtvOH1/BUu/Et4MT1CDg2+++abL+fq8f4swRBjfceLgi04QVxd3ZZxqsMnoffCHUGlhuUrjxf6nO9oY4g0NVNIwsd8zR36G1mH94L0o8VpRqYcIUbhWLc9WWu2bJEUDUUc7GsRh0K/XXf8Kv7sitmPz+89tdDsk4X6NShar4HLeMe6gYFbYefDgHP6DJyPn+yA+NnzwG72TnXXDCWvE0EJ8oXwevRxUZtY7YxoYuqGq/QlsU/MUAg== 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=U990fYf0Hw3SvVRrGVhQXCO0f1RQgDu6SqzDq2bdnoU=; b=gSynDAfpS6PyVEvXk9IHI2V3Xw3vo20T1eBDk+GH16bhOH3OOb1VnGIxhAQNRPPVslXmQiv/jM+sTEKTsix7PUWmTN66MiLI2T7BBybmLZP69V7mO9GcDbXVVRjKiaUqEb1U5Y8S8a+Hmqt1YX5656yPjUm8TQfyo97eKjtvqF44ffowwVZO/debXNwGkwp3EvIVUwWWs8voSCiydLKTHu6aqsaLFtcSJLd4/AlZ3vSyEV1eDSsGOi8Fb5X5Np+8uFm23yq24gNdwdJG7JqGbe1Dfg86rLUIgAE+Tle3jw+wiO3KWcNjYk+4ziR1b+0VsU9iKwe/2l2f917WkpicIQ== 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=U990fYf0Hw3SvVRrGVhQXCO0f1RQgDu6SqzDq2bdnoU=; b=m2RiFApaRz3PYAgOvzgY0AhB+FIbg/S2LLmosd3Md3jM3vjXgMH9vZb2gIXPsIe1ZdsSEv/pfn4C9PKEIOnVyG3c/f2zqjZr9efHz2tV42ECGTsUQB3rV8lk5hL6opfXdgfyCiCtGLzWczIsWyP7BWtvx/xojCvHtrbE/T1uX/o5jR9ooSYc18X2Xu6mfGypGYfX0MV5hOFgJheptz5EFiVy4RenNVrbNSI14uwYZNGAHTEhoIfKwJTLq1gIWHwQQKYCM13yBcapSFXqP/AAE81pPKBiHC9GUkcRAcTd+Tpx5RUHLijbNZyt9Hr61CmKfeEjvzB6ebfsiMZV7PU3tQ== Received: from MW4P221CA0012.NAMP221.PROD.OUTLOOK.COM (2603:10b6:303:8b::17) by DS7PR12MB6023.namprd12.prod.outlook.com (2603:10b6:8:85::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Thu, 27 Jul 2023 20:25:07 +0000 Received: from CO1NAM11FT019.eop-nam11.prod.protection.outlook.com (2603:10b6:303:8b:cafe::2a) by MW4P221CA0012.outlook.office365.com (2603:10b6:303:8b::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 20:25:07 +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 CO1NAM11FT019.mail.protection.outlook.com (10.13.175.57) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 20:25:07 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) 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, 27 Jul 2023 13:24:51 -0700 Received: from rnnvmail202.nvidia.com (10.129.68.7) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Thu, 27 Jul 2023 13:24:51 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.129.68.7) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 13:24:50 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v10 3/6] iommufd: Add iommufd_access_change_ioas(_id) helpers Date: Thu, 27 Jul 2023 13:24:34 -0700 Message-ID: X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT019:EE_|DS7PR12MB6023:EE_ X-MS-Office365-Filtering-Correlation-Id: b804c77b-0b56-4574-e2e7-08db8edf91b2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: aA5GDSldGyUkisf1TDtph7eazgFOfdBH10L9MZLF93BXvtePYs8vPLsFAvfLk0XDZQ7lv67BaY64iMaQVpndgbEdjTRShLptL6jf+wkcHI7zBdfYOErL9y9PJv5AupDfXM/pLhFlEPtMA/XkP/1ur/BZw6YXXjJovODp9PP8CwQbZDBFDoLF0BprJ8XUdBc2P3UBsmdsGGKJKx8emHErQfBVQUrspBOuJrCk0QhacrtCaRrnnknJ5CzvRLCFqVvcL54cSu+1ucClKmFT+xuWJVTx9VAqVKkjRWfWiFWBeq0MZl0L8+RFkoHLUX+o4b3N8/7BADC59S4igctS/Utb+5WQaFi60Q/qAvizrYZugCibhPHnTv2HjkshH234RpaCUL3lIiSaycX7rp7efIfqc4va/XA0i/fL74Bq0FZDlBpeQMaMZw+x5SEpMP0vSjegcVh5VZuO/j8ovnkEum07ifbJGBgx++qixunin1jYSAVZ5Ut1wy7PI+1DqKy4DT7Qt9wBWjPr6JqWP4ucG27tv/u1soFXBe8GLPL/iugcqdnZ58xbC19kzJSJ+hYm5lJRugJMZYep4MsUT0UYXyoPfa06nvRVTgt79dNytxwKC6dJy7ZgQL3IQN5u4tck1bnX6DRU+0g4PbDZp6yq7g7H3vRSfWGE1d59mLRsaArpvEGE7oH4eVk//knMS1lss3sm8idchMo4b4ublRA8dhwVkgZngdn/G3kmHL2xSPJisXRXznpBiIuG15wMmKYoMSKj 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)(136003)(376002)(346002)(396003)(39860400002)(82310400008)(451199021)(36840700001)(46966006)(40470700004)(86362001)(316002)(41300700001)(2906002)(7416002)(40460700003)(5660300002)(8676002)(8936002)(36756003)(40480700001)(26005)(478600001)(356005)(82740400003)(6666004)(7696005)(83380400001)(47076005)(36860700001)(186003)(336012)(2616005)(426003)(4326008)(70206006)(70586007)(110136005)(7636003)(54906003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2023 20:25:07.4765 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b804c77b-0b56-4574-e2e7-08db8edf91b2 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: CO1NAM11FT019.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6023 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The complication of the mutex and refcount will be amplified after we introduce the replace support for access. So, add a preparatory change of a constitutive helper iommufd_access_change_ioas() and its wrapper iommufd_access_change_ioas_id(). They can simply take care of existing iommufd_access_attach() and iommufd_access_detach(), with a less risk of race condition. Also, update the unprotect routine in iommufd_access_destroy_object() to calling the new iommufd_access_change_ioas() helper. Suggested-by: Jason Gunthorpe Signed-off-by: Nicolin Chen Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 123 +++++++++++++++++++++------------ 1 file changed, 80 insertions(+), 43 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 7a3e8660b902..e79cbedd8626 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -684,17 +684,82 @@ void iommufd_device_detach(struct iommufd_device *idev) } EXPORT_SYMBOL_NS_GPL(iommufd_device_detach, IOMMUFD); +/* + * On success, it will refcount_inc() at a valid new_ioas and refcount_dec() at + * a valid cur_ioas (access->ioas). A caller passing in a valid new_ioas should + * call iommufd_put_object() if it does an iommufd_get_object() for a new_ioas. + */ +static int iommufd_access_change_ioas(struct iommufd_access *access, + struct iommufd_ioas *new_ioas) +{ + u32 iopt_access_list_id = access->iopt_access_list_id; + struct iommufd_ioas *cur_ioas = access->ioas; + int rc; + + lockdep_assert_held(&access->ioas_lock); + + /* We are racing with a concurrent detach, bail */ + if (cur_ioas != access->ioas_unpin) + return -EBUSY; + + if (IS_ERR(new_ioas)) + return PTR_ERR(new_ioas); + + if (cur_ioas == new_ioas) + return 0; + + /* + * Set ioas to NULL to block any further iommufd_access_pin_pages(). + * iommufd_access_unpin_pages() can continue using access->ioas_unpin. + */ + access->ioas = NULL; + + if (new_ioas) { + rc = iopt_add_access(&new_ioas->iopt, access); + if (rc) { + access->ioas = cur_ioas; + return rc; + } + refcount_inc(&new_ioas->obj.users); + } + + if (cur_ioas) { + if (access->ops->unmap) { + mutex_unlock(&access->ioas_lock); + access->ops->unmap(access->data, 0, ULONG_MAX); + mutex_lock(&access->ioas_lock); + } + iopt_remove_access(&cur_ioas->iopt, access, iopt_access_list_id); + refcount_dec(&cur_ioas->obj.users); + } + + access->ioas = new_ioas; + access->ioas_unpin = new_ioas; + + return 0; +} + +static int iommufd_access_change_ioas_id(struct iommufd_access *access, u32 id) +{ + struct iommufd_ioas *ioas = iommufd_get_ioas(access->ictx, id); + int rc; + + if (IS_ERR(ioas)) + return PTR_ERR(ioas); + rc = iommufd_access_change_ioas(access, ioas); + iommufd_put_object(&ioas->obj); + return rc; +} + void iommufd_access_destroy_object(struct iommufd_object *obj) { struct iommufd_access *access = container_of(obj, struct iommufd_access, obj); - if (access->ioas) { - iopt_remove_access(&access->ioas->iopt, access, - access->iopt_access_list_id); - refcount_dec(&access->ioas->obj.users); - access->ioas = NULL; - } + mutex_lock(&access->ioas_lock); + if (access->ioas) + WARN_ON(iommufd_access_change_ioas(access, NULL)); + mutex_unlock(&access->ioas_lock); iommufd_ctx_put(access->ictx); } @@ -761,60 +826,32 @@ EXPORT_SYMBOL_NS_GPL(iommufd_access_destroy, IOMMUFD); void iommufd_access_detach(struct iommufd_access *access) { - struct iommufd_ioas *cur_ioas = access->ioas; + int rc; mutex_lock(&access->ioas_lock); - if (WARN_ON(!access->ioas)) - goto out; - /* - * Set ioas to NULL to block any further iommufd_access_pin_pages(). - * iommufd_access_unpin_pages() can continue using access->ioas_unpin. - */ - access->ioas = NULL; - - if (access->ops->unmap) { + if (WARN_ON(!access->ioas)) { mutex_unlock(&access->ioas_lock); - access->ops->unmap(access->data, 0, ULONG_MAX); - mutex_lock(&access->ioas_lock); + return; } - iopt_remove_access(&cur_ioas->iopt, access, - access->iopt_access_list_id); - refcount_dec(&cur_ioas->obj.users); -out: - access->ioas_unpin = NULL; + rc = iommufd_access_change_ioas(access, NULL); + WARN_ON(rc); mutex_unlock(&access->ioas_lock); } EXPORT_SYMBOL_NS_GPL(iommufd_access_detach, IOMMUFD); int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id) { - struct iommufd_ioas *new_ioas; - int rc = 0; + int rc; mutex_lock(&access->ioas_lock); - if (WARN_ON(access->ioas || access->ioas_unpin)) { + if (WARN_ON(access->ioas)) { mutex_unlock(&access->ioas_lock); return -EINVAL; } - new_ioas = iommufd_get_ioas(access->ictx, ioas_id); - if (IS_ERR(new_ioas)) { - mutex_unlock(&access->ioas_lock); - return PTR_ERR(new_ioas); - } - - rc = iopt_add_access(&new_ioas->iopt, access); - if (rc) { - mutex_unlock(&access->ioas_lock); - iommufd_put_object(&new_ioas->obj); - return rc; - } - iommufd_ref_to_users(&new_ioas->obj); - - access->ioas = new_ioas; - access->ioas_unpin = new_ioas; + rc = iommufd_access_change_ioas_id(access, ioas_id); mutex_unlock(&access->ioas_lock); - return 0; + return rc; } EXPORT_SYMBOL_NS_GPL(iommufd_access_attach, IOMMUFD); From patchwork Thu Jul 27 20:24:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13330706 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 3B403EB64DD for ; Thu, 27 Jul 2023 20:25:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232927AbjG0UZT (ORCPT ); Thu, 27 Jul 2023 16:25:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232865AbjG0UZN (ORCPT ); Thu, 27 Jul 2023 16:25:13 -0400 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2048.outbound.protection.outlook.com [40.107.101.48]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80BB42D71; Thu, 27 Jul 2023 13:25:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eIDKK53D293JBK/9f152A4St9+zE1H2s9EJsANrXiOcRF9oXxDAUVaTOPTwfM9b5WNeFgotacrLBhkSm6AKZ6jsx3cIXhNNu9NKOlCY8q4sXQKhCwcigdJbhbYJFGltINXNMlJ2HS1BbfUJBv6ZfHz0/oXlX8ppSo76P9Cf/r9spW0imp3BQq018QDUDtIGwH4cQ0RrjlMudiIG7sRamhPjMy+2dAe8A3zH48c9d2cNoFoT4EOsren+XY/pv3ab3qaWeH2FS/rnpl0++07sXuTllfZDBDh9r1YBcTOJb3fhjGkld34vfkDwMy8PMfJDJPhFbNcWPOMYoe5COZ5s8vg== 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=4159wIImCNgUuFmUXgR4VRJCiyMerOaaf0nbuuC/iBM=; b=Rgsf+MV406ijO8FtZwPi0d0AOipw0HSF/q5VT6+LIusRQa5tQyxa1Do2GVNaGSYkhdAAx+DPEu1ZuttdxynEVkkiXnOX4dbGGM27v2qWc/3WPWI9q8lJE7PIX103v+fg1cJl1EVNkHfS9ucFpoqLvLDm0+a/tJ0ayxMyaMW4WRAXuXDsijJYjFbCh+afGREr0ZAT4hIo5XFN+PCH/c4XYiWfabgmVnYyBWY09yiIBsiu230T+ZvOq4IKRDs7Q014ncV4GrsWrRM3mFHJCXdkBgaeLzI3qaSzf1tzyf/2l03x0ApT7pP3xTJRkemjaTZqg3o7oM1KPwBfOb0u9MHq7g== 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=4159wIImCNgUuFmUXgR4VRJCiyMerOaaf0nbuuC/iBM=; b=fi3FJ/YKITJq4YX0hNPajjzWNKWoceHi4JDapED8R3oriA2LQu9s3RhQ/KIzeiv2NOEi5MRTEfEuvAR6Qo/ICklMjtH4YJfOP+I9YOcyXXITBjKlgpCdYXruKf2YXfOw1gzUyLxJDaBl71YuTpa2IDiXytcG7fYBq0fnHw7fgUmLUE6jAnGHNBtKFjVR9yB4SZqrSBmOzG/z6Io/8KNTBpZkjOcnyZGEsMbqP1X/0uf9yqXNKRnEy7ge3T04P4G0qHEcYBK0As4CaUjZSszxLOcSWim/+Tvywpnwdp/SwP+Cprx5dW3b3rV1NuuBHBT8Okt8GlIPecmYR/rZgwhj2g== Received: from DM6PR02CA0051.namprd02.prod.outlook.com (2603:10b6:5:177::28) by PH7PR12MB9103.namprd12.prod.outlook.com (2603:10b6:510:2f5::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Thu, 27 Jul 2023 20:25:09 +0000 Received: from DM6NAM11FT099.eop-nam11.prod.protection.outlook.com (2603:10b6:5:177:cafe::e2) by DM6PR02CA0051.outlook.office365.com (2603:10b6:5:177::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 20:25:09 +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 DM6NAM11FT099.mail.protection.outlook.com (10.13.172.241) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 20:25:09 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) 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, 27 Jul 2023 13:24:52 -0700 Received: from rnnvmail202.nvidia.com (10.129.68.7) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Thu, 27 Jul 2023 13:24:52 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.129.68.7) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 13:24:51 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v10 4/6] iommufd: Add iommufd_access_replace() API Date: Thu, 27 Jul 2023 13:24:35 -0700 Message-ID: <50da1150e2a4b7bfb51db91f72db9a4dacd4f918.1690488745.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT099:EE_|PH7PR12MB9103:EE_ X-MS-Office365-Filtering-Correlation-Id: 711075ff-3563-43ad-3481-08db8edf92cb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Q7owPDcO5pDoxI/dRPac/21wmIxDg4zmgVmtuMDxTtT0CjPPjLPrufyiz8sstxU7xivbOYH9Z1FASHos3dQ8Za1D4rRiwh2wnpotr09dyrummukXxFRBZUrRUoSSN7lDbVY4I15liP+IfezBo1XT/Lh5P5ww6pkum4pAyp4wm56tzj1hHzRPWLAB/MEhBjcEelJ2ui7sZREojp9RKK3NNr0LJdiybDKSLwA9HJ1AIIX0nW4SECGw2v85hRK+QOLrzqfR3hgohZbb8/4IoqHSotI6eK5XLXvwbnGc4ud77CZlg2FZxq2aFeRjt2keLURm/h53Pzp+x5z20i/2w+yE/MWdZmdWEpHuNzr8ehcFxNKZ6BrPDrwY1eLtj2JaLKyC9wXzruZ+kxDsRl5Htw8m5gK0zwBBxZ7z+QRq7J8ReKio1C9nrS/I8NWdhbKeYYVI00yooCelFUWkZZFHMQqKBffoL+XQKCAO9+J24skiiBJtPCwB9fGQGyCc9gYKAFqz81LhLXERNUDelqhoXKAnO4WprCc+XVtnibh9enqdxzBSkVKhaG0dRCVmb2a7WIiKM8kirDJkjSvg9MH1ICj7zm5KPjA9iYoE6qGi6XatoPnThD51sL4LDfWEwCjF/SqfURTU6YwxnzmiaYXHvWtnDD17hzJN2aeovwKyelctR1jrX6Dz/Xr9Em4HCCOfq7wIJ1+XcJwqBaLUWy2zHs0MKpcqygzps79bkuAfKg9aWFpev4+Tf+2ywL9pfHGFpIu+ 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)(39860400002)(376002)(136003)(346002)(82310400008)(451199021)(40470700004)(36840700001)(46966006)(86362001)(356005)(7636003)(36756003)(40460700003)(2906002)(40480700001)(110136005)(54906003)(478600001)(82740400003)(36860700001)(336012)(26005)(2616005)(426003)(186003)(47076005)(41300700001)(7416002)(8676002)(6666004)(7696005)(316002)(83380400001)(4326008)(70206006)(70586007)(5660300002)(8936002);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2023 20:25:09.3292 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 711075ff-3563-43ad-3481-08db8edf92cb 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: DM6NAM11FT099.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB9103 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Taking advantage of the new iommufd_access_change_ioas_id helper, add an iommufd_access_replace() API for VFIO emulated pathway to use. Suggested-by: Jason Gunthorpe Signed-off-by: Nicolin Chen Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 15 +++++++++++++++ include/linux/iommufd.h | 1 + 2 files changed, 16 insertions(+) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index e79cbedd8626..7a35503b0123 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -855,6 +855,21 @@ int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id) } EXPORT_SYMBOL_NS_GPL(iommufd_access_attach, IOMMUFD); +int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id) +{ + int rc; + + mutex_lock(&access->ioas_lock); + if (!access->ioas) { + mutex_unlock(&access->ioas_lock); + return -ENOENT; + } + rc = iommufd_access_change_ioas_id(access, ioas_id); + mutex_unlock(&access->ioas_lock); + return rc; +} +EXPORT_SYMBOL_NS_GPL(iommufd_access_replace, IOMMUFD); + /** * iommufd_access_notify_unmap - Notify users of an iopt to stop using it * @iopt: iopt to work on diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 0ac60256b659..ffc3a949f837 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -49,6 +49,7 @@ iommufd_access_create(struct iommufd_ctx *ictx, const struct iommufd_access_ops *ops, void *data, u32 *id); void iommufd_access_destroy(struct iommufd_access *access); int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id); +int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id); void iommufd_access_detach(struct iommufd_access *access); void iommufd_ctx_get(struct iommufd_ctx *ictx); From patchwork Thu Jul 27 20:24:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13330708 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 3EA1BEB64DD for ; Thu, 27 Jul 2023 20:25:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232954AbjG0UZY (ORCPT ); Thu, 27 Jul 2023 16:25:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232876AbjG0UZP (ORCPT ); Thu, 27 Jul 2023 16:25:15 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2058.outbound.protection.outlook.com [40.107.223.58]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A03CA272B; Thu, 27 Jul 2023 13:25:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DPF5sqpb22dKdpxENRpC02m+Igw8vufEhQ/XFw1myT6n0eVoBiVyZiVFG+Q4XYCklGX+gIiUHf/qARbCRJvw8Ix2Nt8dBKBF/wgMUKpwfDlTmfEi/neOIq9wWb6WAznm0QgTwhVAbIEuHDO58F8cMxFNwbrNv+mo0uiTMjm+miLaIrGVJMLg26vu1N+QPU91CAAoXj9BHBXNAdKtKNfOaj9dfHM5mfYEIJFCh9+hfUCz2Xbwsek4PADLCZfBfZMAVBdv8d5cW3A3qT9oumCi09BKvxjuF3NBU+9KcdrfYX95IiFMgv5nU8lmBHYGGTCvcGdTZtw20rdZ2t0Rq135hw== 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=iirMKJG1l4d932PYYOqkXDT5WydmhkNOrAq8wo1jKiw=; b=iylr+L2BQ6OPiFQet+S0TG/jIct4vRZpolc7VnXVhRWrm4Nv+GFZ2p1I0x6r2AHDB/RE18KRLNCZNYn0gBU3bKV8Vp0qwNZ7uF8ba0Sa5U0UtyaV3HmSeH/M7FLUvbnjImScilbrAqFrQ0ZY43egUQAIg/jWcPuxsGq8RVaZFgOm0AAnGzvgediY5VsdBqsSD+Ll+m7DIRzTxfaqgKd/BwLNjzxTW9PdVG5oaXUkxXX1OzxbjUapigVItWqWSwVeAteGz2d95Fe02ghhTa4IbQZUDFr8PJiZLyHAtW+OfxuYZLaC0c0wLFmwoSXXZcFGpae/GEs1FwF0mi66gX9XTQ== 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=iirMKJG1l4d932PYYOqkXDT5WydmhkNOrAq8wo1jKiw=; b=Z1cR3amkolnLfSct5VMX4zqZkBMu+1RRqQ3stU8Fs88NBYUpAzAxtGoTj/WtRHm9SvzscmpzxW0udPonkl6oSn/cRKtYVPEbTvcqQJn2Xj0BUxeuAE4/R7eNCGx4mcyTymrSIpa27QbIG8LloWuFB7mMGx4O/hjo6fVM4M0PBZHhQ5zWhx7KXiY5mvvQbytbDUaJYWWR2o5khTUkM68iCAj5myXVL4hDqRX1j6KW2G+5l7oIeD7726FeN+WIIBVlceLSu6IguhLIfT2ciqjJWlJr30VWihCDSkm0HFlWMEmYJInREq1r7XmhWpDEYzcKSOQWEoB9pgF37wxfmmJiLw== Received: from DM6PR02CA0072.namprd02.prod.outlook.com (2603:10b6:5:177::49) by LV8PR12MB9136.namprd12.prod.outlook.com (2603:10b6:408:18e::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Thu, 27 Jul 2023 20:25:10 +0000 Received: from DM6NAM11FT099.eop-nam11.prod.protection.outlook.com (2603:10b6:5:177:cafe::1) by DM6PR02CA0072.outlook.office365.com (2603:10b6:5:177::49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 20:25:10 +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 DM6NAM11FT099.mail.protection.outlook.com (10.13.172.241) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 20:25:10 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) 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, 27 Jul 2023 13:24:53 -0700 Received: from rnnvmail202.nvidia.com (10.129.68.7) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Thu, 27 Jul 2023 13:24:53 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.129.68.7) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 13:24:52 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v10 5/6] iommufd/selftest: Add IOMMU_TEST_OP_ACCESS_REPLACE_IOAS coverage Date: Thu, 27 Jul 2023 13:24:36 -0700 Message-ID: <0a70ac96e5d10d090b16f4aad5da40dfd83af6ad.1690488745.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT099:EE_|LV8PR12MB9136:EE_ X-MS-Office365-Filtering-Correlation-Id: a0820bc2-e5f9-482b-a13f-08db8edf935c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nPf2jkBleH6kA0z0KzkXwL0fIhFQj6xJI+TRSkrKluYHNclGXKH1DczgwiwEtUeECp6tEJGypg9Qp5ia+q6R5+m7bsR/xb81TNmjcazs1M615GHUlMAgbkSgVXWvzqYRTWwcalKGuvSsu/NMpUjfb81DWmkbRrAJqzNeKyG7sl9rSEiO98C7dVNX1HjexYHWBh+uWF8QqkNGnHTBBhduB7yqX2dgTMHQ/QVb22OhpiKGA3RUEPQefb86DVHmP7MSsKIugH6W/ZzlmPV64biA0j3w13B26TLLEOf5tgIIubGBDSs+l+2klLMEeRpEWDJkGILH4+sEInsukCU+jE4lL9Mxrp5RNiFM6+X4WwqVO2jZJsQJ1sq481Xgk7VCjn3OM8kFIITMQzX2tkbGCu5HC6Axq/kmghisAtdPkI4Af9BAevaXYnqqEEK96wkWSq6WxtrKlGgkbp+3EEB5pQYJQ8VMjHpSY6ZueovEucq52oA2ET8STVLPCSM8r7pJjcnn+eWzbbPG+Ijz5Pbdr3ZZRFdl3Z4GMDAhYGT8qahl8fRilxsPBcA+urFuOVg7v4rCm6vFUs/tMkVfXFWvi/UpUjbcqET90Ee06OuaiAbeZ6EUizluNfX/SWdlVC9eSzJiZ8gqZ/yK9pU9QSzz7OU1ZKr15N8Ok2O8sXbZa95XdjBt/Cjs+wKmWxm/UEjYBqzJ1baXDl4DCyEFPyJ5ja8swiKUc65oDxTfkGUiC0zZ+yU= 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)(376002)(136003)(346002)(39860400002)(396003)(82310400008)(451199021)(46966006)(36840700001)(40470700004)(2906002)(336012)(26005)(186003)(316002)(7636003)(356005)(36860700001)(70586007)(2616005)(426003)(40480700001)(83380400001)(5660300002)(41300700001)(47076005)(70206006)(40460700003)(8676002)(7416002)(110136005)(36756003)(8936002)(4326008)(82740400003)(54906003)(6666004)(478600001)(7696005)(86362001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2023 20:25:10.2823 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a0820bc2-e5f9-482b-a13f-08db8edf935c 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: DM6NAM11FT099.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV8PR12MB9136 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add a new IOMMU_TEST_OP_ACCESS_REPLACE_IOAS to allow replacing the access->ioas, corresponding to the iommufd_access_replace() helper. Then add a replace coverage as a part of user_copy test case, which basically repeats the copy test after replacing the old ioas with a new one. Reviewed-by: Kevin Tian Reviewed-by: Jason Gunthorpe Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/iommufd_test.h | 4 +++ drivers/iommu/iommufd/selftest.c | 19 ++++++++++++ tools/testing/selftests/iommu/iommufd.c | 29 +++++++++++++++++-- tools/testing/selftests/iommu/iommufd_utils.h | 19 ++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index dd9168a20ddf..258de2253b61 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -18,6 +18,7 @@ enum { IOMMU_TEST_OP_ACCESS_RW, IOMMU_TEST_OP_SET_TEMP_MEMORY_LIMIT, IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE, + IOMMU_TEST_OP_ACCESS_REPLACE_IOAS, }; enum { @@ -91,6 +92,9 @@ struct iommu_test_cmd { struct { __u32 limit; } memory_limit; + struct { + __u32 ioas_id; + } access_replace_ioas; }; __u32 last; }; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 9d43334e4faf..bb2cd54ca7b6 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -785,6 +785,22 @@ static int iommufd_test_create_access(struct iommufd_ucmd *ucmd, return rc; } +static int iommufd_test_access_replace_ioas(struct iommufd_ucmd *ucmd, + unsigned int access_id, + unsigned int ioas_id) +{ + struct selftest_access *staccess; + int rc; + + staccess = iommufd_access_get(access_id); + if (IS_ERR(staccess)) + return PTR_ERR(staccess); + + rc = iommufd_access_replace(staccess->access, ioas_id); + fput(staccess->file); + return rc; +} + /* Check that the pages in a page array match the pages in the user VA */ static int iommufd_test_check_pages(void __user *uptr, struct page **pages, size_t npages) @@ -1000,6 +1016,9 @@ int iommufd_test(struct iommufd_ucmd *ucmd) case IOMMU_TEST_OP_CREATE_ACCESS: return iommufd_test_create_access(ucmd, cmd->id, cmd->create_access.flags); + case IOMMU_TEST_OP_ACCESS_REPLACE_IOAS: + return iommufd_test_access_replace_ioas( + ucmd, cmd->id, cmd->access_replace_ioas.ioas_id); case IOMMU_TEST_OP_ACCESS_PAGES: return iommufd_test_access_pages( ucmd, cmd->id, cmd->access_pages.iova, diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index dc09c1de319f..8acd0af37aa5 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -1283,7 +1283,13 @@ TEST_F(iommufd_mock_domain, user_copy) .dst_iova = MOCK_APERTURE_START, .length = BUFFER_SIZE, }; - unsigned int ioas_id; + struct iommu_ioas_unmap unmap_cmd = { + .size = sizeof(unmap_cmd), + .ioas_id = self->ioas_id, + .iova = MOCK_APERTURE_START, + .length = BUFFER_SIZE, + }; + unsigned int new_ioas_id, ioas_id; /* Pin the pages in an IOAS with no domains then copy to an IOAS with domains */ test_ioctl_ioas_alloc(&ioas_id); @@ -1301,11 +1307,30 @@ TEST_F(iommufd_mock_domain, user_copy) ASSERT_EQ(0, ioctl(self->fd, IOMMU_IOAS_COPY, ©_cmd)); check_mock_iova(buffer, MOCK_APERTURE_START, BUFFER_SIZE); + /* Now replace the ioas with a new one */ + test_ioctl_ioas_alloc(&new_ioas_id); + test_ioctl_ioas_map_id(new_ioas_id, buffer, BUFFER_SIZE, + ©_cmd.src_iova); + test_cmd_access_replace_ioas(access_cmd.id, new_ioas_id); + + /* Destroy the old ioas and cleanup copied mapping */ + ASSERT_EQ(0, ioctl(self->fd, IOMMU_IOAS_UNMAP, &unmap_cmd)); + test_ioctl_destroy(ioas_id); + + /* Then run the same test again with the new ioas */ + access_cmd.access_pages.iova = copy_cmd.src_iova; + ASSERT_EQ(0, + ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_PAGES), + &access_cmd)); + copy_cmd.src_ioas_id = new_ioas_id; + ASSERT_EQ(0, ioctl(self->fd, IOMMU_IOAS_COPY, ©_cmd)); + check_mock_iova(buffer, MOCK_APERTURE_START, BUFFER_SIZE); + test_cmd_destroy_access_pages( access_cmd.id, access_cmd.access_pages.out_access_pages_id); test_cmd_destroy_access(access_cmd.id); - test_ioctl_destroy(ioas_id); + test_ioctl_destroy(new_ioas_id); } TEST_F(iommufd_mock_domain, replace) diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 53b4d3f2d9fc..70353e68e599 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -119,6 +119,25 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, #define test_cmd_hwpt_alloc(device_id, pt_id, hwpt_id) \ ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, hwpt_id)) +static int _test_cmd_access_replace_ioas(int fd, __u32 access_id, + unsigned int ioas_id) +{ + struct iommu_test_cmd cmd = { + .size = sizeof(cmd), + .op = IOMMU_TEST_OP_ACCESS_REPLACE_IOAS, + .id = access_id, + .access_replace_ioas = { .ioas_id = ioas_id }, + }; + int ret; + + ret = ioctl(fd, IOMMU_TEST_CMD, &cmd); + if (ret) + return ret; + return 0; +} +#define test_cmd_access_replace_ioas(access_id, ioas_id) \ + ASSERT_EQ(0, _test_cmd_access_replace_ioas(self->fd, access_id, ioas_id)) + static int _test_cmd_create_access(int fd, unsigned int ioas_id, __u32 *access_id, unsigned int flags) { From patchwork Thu Jul 27 20:24:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13330707 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 16156C04E69 for ; Thu, 27 Jul 2023 20:25:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230491AbjG0UZW (ORCPT ); Thu, 27 Jul 2023 16:25:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232636AbjG0UZO (ORCPT ); Thu, 27 Jul 2023 16:25:14 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99F7C26BB; Thu, 27 Jul 2023 13:25:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UhzIbkZsGZoIW/HokhIuJPfxwZbCDrGdQhbb1f8I+WSvNNdEhiVss5JIyfpD/7yM7nPC0oBfyHvKewbf6FF3bNpoGLTXsFXiA0G5ntU14O/B78s1d/K2M9uj5RBwh6knNfDCI94xa/ghZgopqkzsaVMU4QRtkI2msURZe9mMGSZ3qYLRrUvYbUyDCRi8T+XPnmI0EcDuVOcCyEAyVtn7KJg/zyAYW+KgB6L410XCH05ns52G3YJFitAomVwIRsv0kR/ttAOaE2pfItXwrUVD6kNX8qvE1fxUoZLzYz9qaW4tulDGaOwe7Hae3YNvD6P3rjkdxhVsQaCK/mPpBQzxRg== 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=R6aD3dt1PckeOKkxIclyv3iBH0qTiumx46+55bGQRk0=; b=h/E3itdYFcD3vUqpGw84irZScQeZjOBEwa+l0JAaCtZsKfQrMtpBRrR6VMGhUkAPC7bKavGP9YoQK+EqSW1PO0wPxutenSOzo2GHUCJEEeBbIYuNwqHkz1TTpkukXoTJUQe6d1d3futMHBO/pOnQ+qNHZLaE6MsRzEiF/l+Xk7moyAEN03xwJ5/KVy1An2pu/JP8RJ2G0HEGRHcVs4yWDLTogwaemVm3cIMqq8UQe6HY3hb5L+xR3ys5m9rc1c5MqpfOuLwGNFF3908rULmb0XF1WgCnlkTXYFSFyVgMCeFnz4nQ5q11bbrwdWGpWIQHRajLRTCYeBanUFZ4UPnDzA== 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=R6aD3dt1PckeOKkxIclyv3iBH0qTiumx46+55bGQRk0=; b=YrCdY1Dz6Y45knIkuambopEJMgwHZ6ThTzEZxfMbBmht5pT5AnoeM/7Gv+w6M4yf73gbEUkl/s/OH/QVr4uCh6XbRHMhnKFFVSGKcHQWxmnklV4mMuspcWMuVqVTepSpIfqil26z2IttXaSf7GZUAxDT5E3i/v5AlbUwWh11tANJOOeoWD12AxzWLBWKG2vQknwN9yfhV+So3bKubVY4l3d9aO4S72YD4+LyLxQyf/Zg1T5Hcz1dHuXWVt3+CQd06UKonyf1okzGnbdmnk131QjApd9UDF1pi8Bxh+HsE+D+darW/cbdaGmPICCW67sFZj9eA52AUmBxwN68Wvya2w== Received: from DM6PR02CA0057.namprd02.prod.outlook.com (2603:10b6:5:177::34) by BY5PR12MB4950.namprd12.prod.outlook.com (2603:10b6:a03:1d9::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Thu, 27 Jul 2023 20:25:11 +0000 Received: from DM6NAM11FT099.eop-nam11.prod.protection.outlook.com (2603:10b6:5:177:cafe::e3) by DM6PR02CA0057.outlook.office365.com (2603:10b6:5:177::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 20:25:10 +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 DM6NAM11FT099.mail.protection.outlook.com (10.13.172.241) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 20:25:10 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) 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, 27 Jul 2023 13:24:54 -0700 Received: from rnnvmail202.nvidia.com (10.129.68.7) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Thu, 27 Jul 2023 13:24:54 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.129.68.7) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 13:24:53 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v10 6/6] vfio: Support IO page table replacement Date: Thu, 27 Jul 2023 13:24:37 -0700 Message-ID: <12b9718d0225701b37aad75395187ef667464bc0.1690488745.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT099:EE_|BY5PR12MB4950:EE_ X-MS-Office365-Filtering-Correlation-Id: 40c6c4ce-7dc4-4122-afb9-08db8edf93ad X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FCoW6MUelZAu67e2fzAxOHM5s5vHs/cIpQy8E3iNMlWvqRB+zHO8bZy3mK/6B//lU1GQg/f5ha0uuZhgDDriVqglT6lHARSCSWKAIoF/bS+YVGp0EprrGDCF/qTl18t51opcXglJ7gKHEOpz4dWU6u6gOSa8ubWjPPTWpfO1pkVUZmuDB3vrJrzumx1EjbKKpsPdyrfwg9IAfoAgCSnjz55J0wSJZHKwCMQa591In3rkzihyIwVOXwKtkRpidXNrZliYo4CZZuk93itopU+DPNMAsUWWhLh6F3qdTrLlgSxnK5uAfWad5SJj8NnxHTcKDLjUakc+ybG21hx3jQmQVmRIMv7J/x8OyroHeVbC5XOUc2nhTpbykYvGvRK6a5ffUi91MseCH9hoi+q9QudoJOZAJswU91Aq+uC7oXGPYHx5g1o4rEuhif4gco910HEeB+3VAo7Szwkz+ZKiiCEIGaPCV9BtgQn2z+El0JNheQ1JmQmo5MzbqoAljVOYn3yMRvBzCvGlQWp1bth+MYBNnx5S7AubLdi9uPRyZAwIdOpgj6xVxhyJrU8Lr3vh/A7MW5UPrI5Yy/b4FdglV1MMRYEBIjsS+gVWRC6qbaz7u7jRGzipkIh66qseCJFgfwXoalnHSZrn/7nDZqOm2T0p/nEURcmBwoBdSiP/HdIw4o3Wc8tgu1yJDqOhUgSlI5QSAh08hyAPkZEg3yimrf3qjoKS5Wbhl8PSezLNmP13QA0yUoDeIZA5EKFZotOSWUKd 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)(376002)(39860400002)(136003)(396003)(346002)(451199021)(82310400008)(40470700004)(36840700001)(46966006)(47076005)(478600001)(110136005)(54906003)(86362001)(70586007)(4326008)(316002)(70206006)(356005)(7636003)(40480700001)(186003)(6666004)(82740400003)(8676002)(26005)(336012)(40460700003)(7696005)(8936002)(7416002)(2906002)(426003)(41300700001)(5660300002)(2616005)(36756003)(83380400001)(36860700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2023 20:25:10.8135 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 40c6c4ce-7dc4-4122-afb9-08db8edf93ad 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: DM6NAM11FT099.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4950 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Now both the physical path and the emulated path should support an IO page table replacement. Call iommufd_device_replace/iommufd_access_replace(), when vdev->iommufd_attached is true. Also update the VFIO_DEVICE_ATTACH_IOMMUFD_PT kdoc in the uAPI header. Reviewed-by: Kevin Tian Reviewed-by: Jason Gunthorpe Reviewed-by: Alex Williamson Signed-off-by: Nicolin Chen --- drivers/vfio/iommufd.c | 11 ++++++----- include/uapi/linux/vfio.h | 6 ++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c index 4d84904fd927..82eba6966fa5 100644 --- a/drivers/vfio/iommufd.c +++ b/drivers/vfio/iommufd.c @@ -146,9 +146,9 @@ int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id) return -EINVAL; if (vdev->iommufd_attached) - return -EBUSY; - - rc = iommufd_device_attach(vdev->iommufd_device, pt_id); + rc = iommufd_device_replace(vdev->iommufd_device, pt_id); + else + rc = iommufd_device_attach(vdev->iommufd_device, pt_id); if (rc) return rc; vdev->iommufd_attached = true; @@ -223,8 +223,9 @@ int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id) lockdep_assert_held(&vdev->dev_set->lock); if (vdev->iommufd_attached) - return -EBUSY; - rc = iommufd_access_attach(vdev->iommufd_access, *pt_id); + rc = iommufd_access_replace(vdev->iommufd_access, *pt_id); + else + rc = iommufd_access_attach(vdev->iommufd_access, *pt_id); if (rc) return rc; vdev->iommufd_attached = true; diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index fa06e3eb4955..537157ff8670 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -939,6 +939,12 @@ struct vfio_device_bind_iommufd { * Undo by VFIO_DEVICE_DETACH_IOMMUFD_PT or device fd close. This is only * allowed on cdev fds. * + * If a vfio device is currently attached to a valid hw_pagetable, without doing + * a VFIO_DEVICE_DETACH_IOMMUFD_PT, a second VFIO_DEVICE_ATTACH_IOMMUFD_PT ioctl + * passing in another hw_pagetable (hwpt) id is allowed. This action, also known + * as a hw_pagetable replacement, will replace the device's currently attached + * hw_pagetable with a new hw_pagetable corresponding to the given pt_id. + * * Return: 0 on success, -errno on failure. */ struct vfio_device_attach_iommufd_pt {