From patchwork Thu Mar 23 08:33:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13185231 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 87CFEC77B62 for ; Thu, 23 Mar 2023 08:34:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231539AbjCWIeN (ORCPT ); Thu, 23 Mar 2023 04:34:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231492AbjCWIdw (ORCPT ); Thu, 23 Mar 2023 04:33:52 -0400 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2058.outbound.protection.outlook.com [40.107.212.58]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D894BDBCA; Thu, 23 Mar 2023 01:33:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=njNIiG/WVP/JIRyJyvMtBU4Eq/GWUYHoaXPuMlLhSiI1DSp9K4HSkq9HLeA1BmWBdjIDUwxnQ0auUmvfdUrsXOqIjz0KRZVdaMWXf61pES1sRdI6K39+xL9DeGIGKqwSvJA+0eIOLjUZbzNsSwL6v3GnbWy1nk1lRnA7LFuK1zzUyVxSAzNswmogtrYiIx4q0sx7xVnkFJdJSEW8OpvP4nyFjYlJHoZvCv8ZWu70gEt6jrL4dZlhjUd+Bul46NjTLG4E1C1TBRXpCSzeIA5gAc/3A2iVT9DMa6K0z2Lef7b4+DyksDAFYkjNt4LBNEXHl3745UZ/1Md7In0dLJ72jQ== 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=yTtPnU9JqaC3rKvfFPnLIqJbP/9m+qfwOArqTsKitCg=; b=ICw45cYQu+AaeGLtqgTSU1V5m04+oiKovvIB8J/Z3wpj3T67AbsyUXgYlOgOrVrCrL20ZhGF2x2gdN9kyyv3Tbqol4Ma7yPWLVnHx03O7apa+t9x7gDZF28ebVADZjRIDSV5EhQsojRTxes0dnEcDIfOzZA8UF7qHY6iH61shrcqI4ZCbrw0iYXA72JzgQjvDyStli3TMnP3p+BC1x1XI9/9xlnHcDM4AowecMgKPnxezLrYInsE5BqMvgP/OgBTa7RumgcsaGMdePZN6hy0AlfA4QoEthg0wAKNSqRmcnKE7CHP9a7gE060M8MVJcEsXYjDniyII8uJD841KehCHQ== 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=yTtPnU9JqaC3rKvfFPnLIqJbP/9m+qfwOArqTsKitCg=; b=eYJS5XGpLdzS2JQF3jfmcWKJKJHDgFCzCwrapVzKLoSSbclTqOb9gSlwI1dVKosz3Y5KvlEvRaAjAhorSbPTl3Afw01gUbJFVRxfuOcsbNxgm3ncN0WnJxW5/pyIvcyliJNSlIocX5VyTeVS7bv7fB69gIy0ZeEmxEOFuSwPJeQsFC/yKY7wzCoYoLjzsYbA5xi98+Fqs/ZP0NYU6g5oBf4YIoySAA6rSx8F8892tp/pKeOPybf3CNfzcTGyGvEZE9sIzzfTXiPT/FlBKAyKNeHzihvJqY8YE50DHzxIXTbL8ZZviPM34Q8AxiSPM+OfPjdCKU0nZ20uUFA102CXMQ== Received: from BL1PR13CA0075.namprd13.prod.outlook.com (2603:10b6:208:2b8::20) by MN2PR12MB4286.namprd12.prod.outlook.com (2603:10b6:208:199::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.37; Thu, 23 Mar 2023 08:33:39 +0000 Received: from BL02EPF0000C404.namprd05.prod.outlook.com (2603:10b6:208:2b8:cafe::46) by BL1PR13CA0075.outlook.office365.com (2603:10b6:208:2b8::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.4 via Frontend Transport; Thu, 23 Mar 2023 08:33:39 +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 BL02EPF0000C404.mail.protection.outlook.com (10.167.241.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.30 via Frontend Transport; Thu, 23 Mar 2023 08:33:39 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) 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, 23 Mar 2023 01:33:24 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Thu, 23 Mar 2023 01:33:24 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.5 via Frontend Transport; Thu, 23 Mar 2023 01:33:23 -0700 From: Nicolin Chen To: , , , , , , CC: , , , , , , Subject: [PATCH v5 1/4] vfio: Do not allow !ops->dma_unmap in vfio_pin/unpin_pages() Date: Thu, 23 Mar 2023 01:33:02 -0700 Message-ID: <3fbeb9bda2b297806e991017dd975aa512670033.1679559476.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: BL02EPF0000C404:EE_|MN2PR12MB4286:EE_ X-MS-Office365-Filtering-Correlation-Id: 2fe3a221-149c-4fe2-d379-08db2b794d69 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: a/K98s2OiWlirkkzOnOOQugQXnqqc1dEvP1q6hM7Cb7qe+FlMNSBqojqxW/IDwbDB/IjZOloMrMmta9EyDFUBTJlHlc3h+ABGc4QXhUU3rnUR8uaBADIKjDwQNGuyag0TpgBQcHqzyisGJ+SLvgtyHwq54wZlmLC54djwwC7cybJjmw+mYbGen3BYSZkmfQEZVvGkV9JYNEp9jYnD2SXf5MY4mup+cdd2VRP6FNCB+H60A8fVtbEzDDCloJiMFjzkozuku84Mwg+NHJaBvBK/3AWgA9aWk8laG3W+MBfxa8zFBCsPHPfxHmq3txPptIEIS+1/mYt/BXKk5s2InbxgUzqvP3i8exsKI1UMjDJL//z1ZPorRVICVkysZzdTF5OZTvSWDbseHb4bQBsoeB+x7z8TcWY9TCqvHmE4JQTms/ntU85euVkgNLgqZSxCm6R44BC3ISRPqXJhe0XPQUwEp4vGh0xVd121AsNpL/vOdkbWThxkbQlXYTGYHgZfjtk9k9jHrzvV1R+VVdje7T6GMySNtJ1dLaXwqaq8PQGFg/x3xGndHkkGyeidCt/fP5NTk0UaCQ6gEqqa1959EaJLvqbcAc4nJg/QPnhlz6qEu7xKPq07B7KWoR9UIuqLu1yhy7pEhberqBY9+dwXs9RiUNkCyJ7CcmT8zTvsQOIYJeELSIGShcUzwinp458gnsGSc50wO59+Yn2VO1BSQhv72rEmpLhxThmkxstV6FAkqeBslvNpbhmE5jse06st9IK 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:(13230025)(4636009)(376002)(346002)(136003)(39860400002)(396003)(451199018)(46966006)(40470700004)(36840700001)(86362001)(40460700003)(82310400005)(36756003)(40480700001)(4326008)(54906003)(70206006)(83380400001)(478600001)(8676002)(186003)(26005)(336012)(2616005)(426003)(110136005)(6666004)(47076005)(70586007)(7696005)(356005)(316002)(5660300002)(7416002)(82740400003)(8936002)(41300700001)(7636003)(36860700001)(2906002);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Mar 2023 08:33:39.0844 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2fe3a221-149c-4fe2-d379-08db2b794d69 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: BL02EPF0000C404.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4286 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 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 426ebfc0ee1c..546e1fc8d8e1 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -1482,6 +1482,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); @@ -1519,6 +1521,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 Mar 23 08:33:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13185230 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 0AAEEC74A5B for ; Thu, 23 Mar 2023 08:34:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231523AbjCWIeL (ORCPT ); Thu, 23 Mar 2023 04:34:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38374 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231491AbjCWIdw (ORCPT ); Thu, 23 Mar 2023 04:33:52 -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 59F26E39C; Thu, 23 Mar 2023 01:33:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kxjDQtJFi3CVtA4ELS9Th0p7Zs8Savf9VrrfS4fxOMp1QMm/cgwseH0PWO8QW+rZwmQNuGpUoZ+cBV+Fio27818Yu3AL2ka5ncSSM9r/ZIeD5rZHY4TUtM4i1uYUTi1GfYqXM1jhHjfL14S7f8vxs+CEOOlkIkwDEF3YbKqyoEI3/kvwQoqtXbAB6LViO/aKegNqmI5Un8w6ZC/rMV8YtgpKnQGSEhfDX4ozqtLnC59UwO91cV72XHOMvR6lrTtC74XiUD37GdJ90x4vRfc1fUZ38RU8V/koKx1tX+RwC+28qTwveqdHBk+bKXlp3mz/sn0sSSiEYfa906MA16/Hfg== 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=jPC1QRJtNYeYfwMILSbEoejTgiHZH9D/NLYMZO1NuWo=; b=C4xMivCWDR+T8DP7qMzRNkCk83QsasgMwv0+m2MNV2/WwLfT7niQ3brxbFkObjDXUBkniRCgMHOg4yNcnjwtXjZKbU0feVa0YWN4D9MMOjKOpznKK2cGKKLMz00ozWsytWTSqk+O8fiRLE2udDnzZjEDlnC0v/SgcaBS9KdAzsdmT49odpZgvqkyTj6zKsuRgSZInMo7LGgxszVInzLI3WAwEFoKymo4pjFhow8uMk5/BIUmvAZkuMyToiiHMZo8SbPD/6WdUc3gs6V3ZjAsxnBK2VT7VQd+Ijd/C5MdKbloEMmEx4PgA9pu5GYrTd7NTTbvV/oUT/wQD1xotT0zHw== 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=jPC1QRJtNYeYfwMILSbEoejTgiHZH9D/NLYMZO1NuWo=; b=gBx8oR1nXGdpT38b4X2hZi8YoQFdgt00miSPRCX9ZpTg4Rj0SzfB4/nNBdlPpBp3WwR5QsinTMXyGQ6IaJ6RSIIPbcfuYbE0SWhvm8qaWTm7ihKoPfovEX7IeHRaKI506tSoSfC5aFF34uTss1yfH2CttDijYrs2RpvMkYBWhOOlS4Ip2rHeA9E4WEk4Kkmzy0DEg+N2LD8Wcj97JYqVdh1/U54gtbzQIEvo8FAHsEZuFaHRNK9p3+5W4MOoZw9Df7D74hO/JB3+0MTojvMbWZIdHeFIjVeOU33JOcwEyHvKMX+nuQKnOiYahUFmOZti+1CxvJff3O2Mk4PPJwvFBA== Received: from BN0PR04CA0029.namprd04.prod.outlook.com (2603:10b6:408:ee::34) by PH0PR12MB7908.namprd12.prod.outlook.com (2603:10b6:510:28e::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.37; Thu, 23 Mar 2023 08:33:40 +0000 Received: from BL02EPF000108E9.namprd05.prod.outlook.com (2603:10b6:408:ee:cafe::82) by BN0PR04CA0029.outlook.office365.com (2603:10b6:408:ee::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.38 via Frontend Transport; Thu, 23 Mar 2023 08:33:40 +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 BL02EPF000108E9.mail.protection.outlook.com (10.167.241.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.30 via Frontend Transport; Thu, 23 Mar 2023 08:33:39 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) 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, 23 Mar 2023 01:33:25 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Thu, 23 Mar 2023 01:33:25 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.5 via Frontend Transport; Thu, 23 Mar 2023 01:33:24 -0700 From: Nicolin Chen To: , , , , , , CC: , , , , , , Subject: [PATCH v5 2/4] iommufd: Add iommufd_access_replace() API Date: Thu, 23 Mar 2023 01:33:03 -0700 Message-ID: <2c3fa7a21373ee10af7cc9f8c370945a08341930.1679559476.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: BL02EPF000108E9:EE_|PH0PR12MB7908:EE_ X-MS-Office365-Filtering-Correlation-Id: 6ca4b91d-4cc0-4782-7157-08db2b794ddd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: lMkGEi7ut4fo4tUukxjqjxWH4q1oWnCKVixJ1kxDT8yvlXKeaCyAXVxjtPl2lBYn6BkMzt6wjdRTZ4TdevD5xkQKsUf301SsO3BPK06yP3Eoi/TndWjAYujLSe0IAEFKOIN2VrMdjEla1wjoaHlPBcHiTDiwTELQKVuK5Wtq16Dx10JnkpQx5BRM2aJEFfCuGKt3MP1jwS00UmKf1fKDt0VdgvlkVf39HvbWuYELleKQP7PADG+63jA5RIdEkkk3DR1a1NBUtgsdyrECPPxu+o4ksqyM6sYpbbejm+7i++hqXxB1FpR0edDXEhzbw39PEDXqnDvWAxNlAGaHeRGK4nQjm5ORIj4ulSmxjoAZJmpHz4UY15HDpHKcfE/111vM/6XAP+/fijHxpDw93otqNbA9T7rDCkxadN6idVBi5N7acVjTDGKXkk4SCAfnl9eutqXkTmdJNNJbrCTE9BnjR7LJdc7LYhx4w66zNw1RFSJFuw87vcmR+6w4FbbGG/7qDy23rOw2hGf2ruLmSTgPUEKGM36XdnLdfeY9kys6yjmx1sW/fpbXFWpGu+ckAr/PyWKWCsxDUKxvCVZ/clIAw4EZymy8ueNcTAtgd8ojI1ci82lfWdG5CxlVGhBguFPgMd5RnBrl5Bh5f8d/N1i+e+9zRPwflXioFHpTZhomcDFwkhCTuwm04s+VV+9iOIefNNp0SGHjvcSpChjLDpe3JORzsuk8QJOSt7OoOU3C3XilBoHce7fhwAWPs6ODmeRe 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:(13230025)(4636009)(136003)(396003)(39860400002)(346002)(376002)(451199018)(46966006)(36840700001)(40470700004)(47076005)(426003)(186003)(6666004)(7696005)(336012)(478600001)(26005)(83380400001)(8676002)(2616005)(110136005)(316002)(70586007)(70206006)(54906003)(7636003)(8936002)(5660300002)(40480700001)(41300700001)(7416002)(82740400003)(36860700001)(4326008)(2906002)(40460700003)(356005)(86362001)(36756003)(82310400005);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Mar 2023 08:33:39.7640 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6ca4b91d-4cc0-4782-7157-08db2b794ddd 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: BL02EPF000108E9.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB7908 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Extract all common procedure, between the iommufd_access_attach() API and the new iommufd_access_replace() API, to an iommufd_access_change_pt() helper. Add iommufd_access_replace() function for VFIO emulated pathway to use. Suggested-by: Jason Gunthorpe Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/device.c | 60 ++++++++++++++++++++----- include/linux/iommufd.h | 1 + tools/testing/selftests/iommu/iommfd*.c | 0 3 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 tools/testing/selftests/iommu/iommfd*.c diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 012e85fc6d77..3b39b76fbc3b 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -746,39 +746,77 @@ void iommufd_access_detach(struct iommufd_access *access) } EXPORT_SYMBOL_NS_GPL(iommufd_access_detach, IOMMUFD); -int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id) +static struct iommufd_ioas * +iommufd_access_change_pt(struct iommufd_access *access, u32 ioas_id) { struct iommufd_ioas *new_ioas; struct iommufd_object *obj; int rc = 0; + lockdep_assert_held(&access->ioas_lock); + + obj = iommufd_get_object(access->ictx, ioas_id, IOMMUFD_OBJ_IOAS); + if (IS_ERR(obj)) + return (struct iommufd_ioas *)obj; + new_ioas = container_of(obj, struct iommufd_ioas, obj); + + rc = iopt_add_access(&new_ioas->iopt, access); + if (rc) { + iommufd_put_object(obj); + return ERR_PTR(rc); + } + iommufd_ref_to_users(obj); + return new_ioas; +} + +int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id) +{ + struct iommufd_ioas *new_ioas; + mutex_lock(&access->ioas_lock); if (access->ioas != NULL && access->ioas->obj.id != ioas_id) { mutex_unlock(&access->ioas_lock); return -EINVAL; } - obj = iommufd_get_object(access->ictx, ioas_id, IOMMUFD_OBJ_IOAS); - if (IS_ERR(obj)) { + new_ioas = iommufd_access_change_pt(access, ioas_id); + if (IS_ERR(new_ioas)) { mutex_unlock(&access->ioas_lock); - return PTR_ERR(obj); + return PTR_ERR(new_ioas); } - new_ioas = container_of(obj, struct iommufd_ioas, obj); + access->ioas = new_ioas; + access->ioas_unpin = new_ioas; + mutex_unlock(&access->ioas_lock); + return 0; +} +EXPORT_SYMBOL_NS_GPL(iommufd_access_attach, IOMMUFD); - rc = iopt_add_access(&new_ioas->iopt, access); - if (rc) { +int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id) +{ + struct iommufd_ioas *new_ioas; + + mutex_lock(&access->ioas_lock); + if (!access->ioas) { mutex_unlock(&access->ioas_lock); - iommufd_put_object(obj); - return rc; + return -ENOENT; + } + if (access->ioas->obj.id == ioas_id) { + mutex_unlock(&access->ioas_lock); + return 0; } - iommufd_ref_to_users(obj); + new_ioas = iommufd_access_change_pt(access, ioas_id); + if (IS_ERR(new_ioas)) { + mutex_unlock(&access->ioas_lock); + return PTR_ERR(new_ioas); + } + __iommufd_access_detach(access); access->ioas = new_ioas; access->ioas_unpin = new_ioas; mutex_unlock(&access->ioas_lock); return 0; } -EXPORT_SYMBOL_NS_GPL(iommufd_access_attach, IOMMUFD); +EXPORT_SYMBOL_NS_GPL(iommufd_access_replace, IOMMUFD); /** * iommufd_access_notify_unmap - Notify users of an iopt to stop using it diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 7c79f9b33246..cd7ae5a625fc 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -46,6 +46,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); diff --git a/tools/testing/selftests/iommu/iommfd*.c b/tools/testing/selftests/iommu/iommfd*.c new file mode 100644 index 000000000000..e69de29bb2d1 From patchwork Thu Mar 23 08:33:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13185232 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 171D7C7619A for ; Thu, 23 Mar 2023 08:34:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231553AbjCWIeP (ORCPT ); Thu, 23 Mar 2023 04:34:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231494AbjCWIdw (ORCPT ); Thu, 23 Mar 2023 04:33:52 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2060.outbound.protection.outlook.com [40.107.92.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D8944227; Thu, 23 Mar 2023 01:33:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TrajgAIFgzIgu46aq/FoU9S3newxYoc++C8ANvhtzXf5XIgruT80vgmOKkO29+eODx/eLxnBU17rFbLiUcMqrmIdfnAKTuOaDXTg1IcCrel+f7p86lthouAcNWCSBZiushlUmRafbyBWKSwS2j4sjsa7TpUN9jgEAxzHb4Zz3fF4ZBhhFCWRbFFERFcrsbcNgdJSdV0xVDv5fFO0g3PD10KBWG+raqmSU3UNcWfxWLlFRwnEphSeMm6GNVpkbSa/laM6EkMzDT1COtm/oksfwuw8UxQPdHp86y/k5iRxLTn1ucAO+jkBQPW7jWgBu54E2KAcUFwex6JBeAfPtcifhg== 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=JRlPS1nvMhiSDecvNOXKaOh/AfENj67Efp6e+8Plco0=; b=ODrUu8exaklSoHmKQS2BSqg586ezyo1eQkj+45s/HN6/p5sUv5nQnAlMj5lDc1OYZYcYFF54ZFmbMdoNllWH3OXrOSbFO9Gx/nyaLHuPJ5OxSBxa6xpqqoc0aF8o0wxGsB86DXX0c6AqcwERP4cVbLqSPSh3BJ0Z2x1BIhRpuWqcktUtJPd4qnJtrC6Iu8mvb1dvQ8Bc0del8YqgB+qKdPgPeqj9pP0n6iDUcNtoRd7uMXIMh9GZBtlNtYeqirhnHpymb4oUoP2egQ/Jtf/cb/BfNkab0q/ZjLjvlC9ieM2uB49KJRcMnmyeP6OLHBjyc6NhGkHwV106BpVJ9V21KQ== 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=JRlPS1nvMhiSDecvNOXKaOh/AfENj67Efp6e+8Plco0=; b=G5cIXxqY5yIxGt9hexiT9bPe5cfz29f/8BhQAuO82ylvuQdu75f8NEFmgrQtZcNY9w8h17MQCkBG8DYyWoq9dYgTubfhGhk8B7I9oy9nRiil6eS/YSe+9TSJajC5t9z5kIQBMow9h93jfl8B8HVDTbLPVLuwsgwUXRncbtcM3qcS1uX6eXBMl7NBkPTL2TFMtnKAeJBvPeSsuNv5MwCqljBKMdwvC4amvwLWuIUhYU6UNeV/VSWgt1s0h9FTRyibOmYg36ps1goRYLYG92I3c2sZ3HpSfQcxQ/KtRHGFm8KCKAJawKAtMbhdSbVQODAiD5RjROXzP2tSrp78QG0O1Q== Received: from MN2PR20CA0006.namprd20.prod.outlook.com (2603:10b6:208:e8::19) by BL3PR12MB6618.namprd12.prod.outlook.com (2603:10b6:208:38d::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.38; Thu, 23 Mar 2023 08:33:43 +0000 Received: from BL02EPF0000C402.namprd05.prod.outlook.com (2603:10b6:208:e8:cafe::54) by MN2PR20CA0006.outlook.office365.com (2603:10b6:208:e8::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.38 via Frontend Transport; Thu, 23 Mar 2023 08:33:43 +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 BL02EPF0000C402.mail.protection.outlook.com (10.167.241.4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.30 via Frontend Transport; Thu, 23 Mar 2023 08:33:42 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) 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, 23 Mar 2023 01:33:26 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Thu, 23 Mar 2023 01:33:26 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.5 via Frontend Transport; Thu, 23 Mar 2023 01:33:25 -0700 From: Nicolin Chen To: , , , , , , CC: , , , , , , Subject: [PATCH v5 3/4] iommufd/selftest: Add IOMMU_TEST_OP_ACCESS_REPLACE_IOAS coverage Date: Thu, 23 Mar 2023 01:33:04 -0700 Message-ID: <370c59332690cecc62b95f69313ffe772c8d91c7.1679559476.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: BL02EPF0000C402:EE_|BL3PR12MB6618:EE_ X-MS-Office365-Filtering-Correlation-Id: d18b1a61-4d52-446c-743e-08db2b794f80 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gu6QBZDfvjMtFr2XHD3wrHCPkzv1P+K0o6nQNp7cPzgv4LfLZFE4TQmFTD3uLadMuG4/SlnBLYHxj+b01L8DZ+/nUplG4WPwNmir17QPlpafGeVgR0ZKsGa4fko4fv/Iu/5WROjZScviCq0s8JrRJGUYGadhsk93cEMxr/x6SHtDU7+fg19zb5injIV69pk9j5xstb97/BaZftyIAro2BtD8UNKwEBdeft3fCif7DGykliiB4nAhqM/Y49Rnh9OEaOZsy+z+QiCBkHZ4iZryRR9xRlneNhQ4MT+K92YnVuaTQXTNssuzL4Jex4C6mA4llyqJ0M8UszGOiIEcmc9FWs4irtneL3SYUr55KTsUqcRwsJgvSm9CU8COF2i0nn96EyUQdMdxxUF6Xms5kiy1vcGOy5sq/SyWSsILiXTbRNpkB3wcZPUs88Qm+ghl2sTPteqsazzEKGJ0P5kAZaqqsVzFTIfXPy9K9r/O7mSjnpz79WJY8qls8KxX+B90jFFNXUu8WbGfmEYlr8peT+b4j4AfocJEb1vsYgfjuGEnhX9SqMgQlewe6VOjJYIk17P+9rcjXeRWbZ+Ie1qes5Yy6K4skgvZoMPeaJ2nzzn6yHk2xX2m9upTTh6vaWYduO/tXG+kUWa7q9znLj3U+0N5gtVgYUah4zUyD/hPAmISDv1tLVKMxxQK6uKbbWvsfxD4kIYas9u28VsJfz8Hq1JI4XKOjzv3jZif2ZZup5nnSw3mRyt/ClwA9oGBW2SNsMv9 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:(13230025)(4636009)(396003)(136003)(346002)(376002)(39860400002)(451199018)(46966006)(40470700004)(36840700001)(41300700001)(8676002)(7416002)(5660300002)(4326008)(2906002)(40460700003)(36860700001)(356005)(7636003)(82740400003)(86362001)(36756003)(7696005)(26005)(6666004)(110136005)(478600001)(70586007)(316002)(70206006)(8936002)(82310400005)(40480700001)(83380400001)(47076005)(54906003)(426003)(336012)(2616005)(186003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Mar 2023 08:33:42.6037 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d18b1a61-4d52-446c-743e-08db2b794f80 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: BL02EPF0000C402.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6618 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 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 ff4fc88640a0..9f6b2d71bc87 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 c07252dbf62d..3c8cb9d6c22b 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -1252,7 +1252,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); @@ -1270,11 +1276,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 9b6dcb921750..c31e75607355 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -117,6 +117,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 Mar 23 08:33:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13185233 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 B6BDBC6FD1D for ; Thu, 23 Mar 2023 08:34:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231567AbjCWIeS (ORCPT ); Thu, 23 Mar 2023 04:34:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231499AbjCWIdx (ORCPT ); Thu, 23 Mar 2023 04:33:53 -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 351733A8D; Thu, 23 Mar 2023 01:33:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XPwjOaLVv8BNnkkF0Azvi7WjGDoqyWGxZv3CCa68SRh1uWhl4Yd9hAestbfdMK9+Kd+of8DGEe3oUCPi1lf8toWzo9LxXfaNS8cSFu1Twu+iylTOdVxVwwgHN1XIp/ekwnQqtnfqcRnA7PQzCNUWxZsjkCB2hHPeTJgu6swS6mxFex2XV76Q74ELZb1QK1SSWLEwstPku619vfaf0YJaNzlxU/otc9gsq1DMSz1Y4cwMxWPUy3xix1PoVTdEB2IjZWkJ5VnRSdlYqQEjO/WUiG+ceeSbsbdXlMQreXw28R84mVBQS6Sju6z9TAz5qfZY1YLNRdZixsVKlp8LmjxMzg== 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=ZZrI4Zh2ZNVvzhQaED0G2DfuNqC97VHB92s1qyb1cmE=; b=Kgn2+vaCLnHWtKfWDB7JWbOIKGcSjb6SDHbopE3xZjsLm4zND75BQeDtMkD6ZKUTAN9qU3BUHPEpLrDvgPji5JG3y4+4YeE86cqZpnABMhp7IxAZ2reqc5tuGYhvIwpsJbU42c985VGYkG52ff7oIPSZmU8tUBa1QZ6fc18N8AsB+DkhNkZmALEjnR+KBARtGDK/w2/N1jgA1XvqxXYFcmUnIHjl0KdRY8wLxvHOIKDzlUbnYxK7EuwtdFQ5clmyGEfaVoZnRETrWd4YJ5UUKf/lrPMmV1+xipZ6MKDNrEIsiSZ4dW0ILuVXeNXuH4LfVLLFs8yovLM79MTfw+PqBw== 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=ZZrI4Zh2ZNVvzhQaED0G2DfuNqC97VHB92s1qyb1cmE=; b=ASHXemwr65/kXE9hWOjA3Sz6U+d+GdOdJmkNtwembnOnquZTpn4lXJUxoYieflqKnc0yi4XJlh/E6zLL51i2Rxt1wNye4DQIV5MS4o7NYuS6ma2NLDkJX9PXKlgBD6ed0KU+WHw1v/GH3BdlZM3GLZLNaQx5D40LnZ0NyBvwRvpimtsLS72QORy1bpOsjqFRJZvh9JwgtoWk1j/yfI+BbE/z0x7hxdMBxdZ/06aGrpR+d6Euai/3R9qpYs/8qO30Argo+Jf2v+9y/3UCL336RixxW8guNXYWLXN0BfTujjPnXmy+E7CINGhDXIVu01pzdHN85xzxJno22ajdE+YbVw== Received: from BN0PR04CA0028.namprd04.prod.outlook.com (2603:10b6:408:ee::33) by IA0PR12MB7776.namprd12.prod.outlook.com (2603:10b6:208:430::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.37; Thu, 23 Mar 2023 08:33:41 +0000 Received: from BL02EPF000108E9.namprd05.prod.outlook.com (2603:10b6:408:ee:cafe::1d) by BN0PR04CA0028.outlook.office365.com (2603:10b6:408:ee::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.38 via Frontend Transport; Thu, 23 Mar 2023 08:33:41 +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 BL02EPF000108E9.mail.protection.outlook.com (10.167.241.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.30 via Frontend Transport; Thu, 23 Mar 2023 08:33:41 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) 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, 23 Mar 2023 01:33:27 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Thu, 23 Mar 2023 01:33:27 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.5 via Frontend Transport; Thu, 23 Mar 2023 01:33:26 -0700 From: Nicolin Chen To: , , , , , , CC: , , , , , , Subject: [PATCH v5 4/4] vfio: Support IO page table replacement Date: Thu, 23 Mar 2023 01:33:05 -0700 Message-ID: <29023644817002345ab94d8916a8447debb45a95.1679559476.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: BL02EPF000108E9:EE_|IA0PR12MB7776:EE_ X-MS-Office365-Filtering-Correlation-Id: 4fa4bf11-7ea6-4dcb-799f-08db2b794ee8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YkBx7RCN6HgYF0AdmMF3/tTF9ZS07KI75VNH8i1+OajQbBmH2YabTYrLZX4zh9EdRuZnOK/9t4bZF3ou7C5SYAR/ior03jldGQcjBwRMMdsUMnVLzIjfUv1IabUiDRd8Jr/RW6zOeGKvYNAZlMU8lB/3OEM1TxfvaFd1yvkI0uh6775c37OceOLoEeDD7FEtZ4CesWexrw/Pa37al5wlcdVpSB/pxHX6a/fKt3Och1ezwbWVJ66oxUYcBYZWpXNDlOb2fCkL+h6CGZcNWh97duqAT+mQ57DW6p0Yi++kuGJF+TTapjnwzRROnfWNXTmwzPviryw4DD2MtPqy+E0N0Y8+yMv9HuPyZOQ38sN3R/OCPPihtTaDrnFe/1Hgx7Nl/jzr02i3kRxBAYcvawrZsuuc1fu/HxHBbGj71dWIeXGAjCDJaJaNha4ArSNzHvH6ZOLuBsEKsG4fECTBgGYgujN/oHANArmelE64Jb8Gljiv5vQiwRsZlMqKBrvm8mjuLIrPdVqqATjDdKZUHfHl+gSjEQjotIgADO+57j1ipJ2im6HVpoT7+aKPS582hvGjOjzzwrMmyvhspQvA6e+TpIv2Q0m16xkgQFanZU3DSW44L1cVV6egO8ercnT3IA2B0iSwUT4jFstsXVhfYUZuut+M74quTcD8v6NZFeQfCtLbo9GAWKZxwngTz9RNXavv+z8I4vBorvU2hWnXtX4QOQ+sqd7iIJkxaL7YqqP+DAlG3E0FYxyTweDIYtxIGbme 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:(13230025)(4636009)(346002)(376002)(136003)(396003)(39860400002)(451199018)(46966006)(36840700001)(40470700004)(8676002)(7696005)(70586007)(86362001)(2906002)(5660300002)(70206006)(8936002)(36756003)(7416002)(36860700001)(82740400003)(7636003)(4326008)(356005)(478600001)(316002)(6666004)(110136005)(54906003)(40480700001)(336012)(83380400001)(26005)(41300700001)(40460700003)(2616005)(47076005)(82310400005)(426003)(186003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Mar 2023 08:33:41.6077 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4fa4bf11-7ea6-4dcb-799f-08db2b794ee8 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: BL02EPF000108E9.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7776 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. Signed-off-by: Nicolin Chen Reviewed-by: Kevin Tian --- 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 f3fa03495a41..861e10a6b198 100644 --- a/drivers/vfio/iommufd.c +++ b/drivers/vfio/iommufd.c @@ -113,9 +113,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; @@ -192,8 +192,9 @@ int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id) if (WARN_ON(!vdev->iommufd_access)) return -ENOENT; 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 53c72e26ecd3..3f2548a5e847 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -243,6 +243,12 @@ struct vfio_device_bind_iommufd { * * Undo by VFIO_DEVICE_DETACH_IOMMUFD_PT or device fd close. * + * 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. + * * @argsz: user filled size of this data. * @flags: must be 0. * @pt_id: Input the target id which can represent an ioas or a hwpt