From patchwork Tue May 9 04:44:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 680782 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 3950FC77B7C for ; Tue, 9 May 2023 04:45:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234708AbjEIEo7 (ORCPT ); Tue, 9 May 2023 00:44:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233960AbjEIEo6 (ORCPT ); Tue, 9 May 2023 00:44:58 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2070.outbound.protection.outlook.com [40.107.92.70]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B02EC76BF; Mon, 8 May 2023 21:44:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eLf4W7lW5kgI7CiJGsu7fTduVSSICIkadV6vIEErJKANbvxh1hInyFpFT9p/HCZgcY1GQzn7WZya4VJFVFfJ/xT37fNRBAA7zg4eITPnXMglDUC37BA4BdmMPoxfCUE6DMlJp2REwxYvh7tZZ+OyzqyCPHrTQHZLTcSMGODEp7qMQtE6zAp+4gyawyDrmOjfuUUVp3M8n/CWxfuljJviWJb6I5qA/EBe5lLeTUFkfeNsqgdfdhSeQMBiyMgrjhTVfuNIUt7Bs7NgNQXFmXsF9B9H3l0ije5HhrQymq6mR7C3oRPGn0+lyD0Xb/eUtXYp/Kms1mrx7bQ86mhRp4oljA== 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=f0pfVkk+cS/b2HlV4J+2taPjCRGkf8aumGCPtHh3Zjc=; b=j2C/32TlgPwvQgcrEXCLpvBIABQ3c+WI+w/e3v7TO29kCEegDheO7IJYj2uk2e8cTx/3XQ0QKJOyOv/rbTVxgFaQs2oGWsqfl9V7ZTm5Ru56AzItsBWS527dMIwjVVCBGlUBJT1NTBgYxLBg5SwpkP1FI0v/G4R/MFENZsCGykE5hkxx0tuD1TwSd5aszoDfk9fwmftLnOYZRAPC5DGtBg6F4zXSLCrqPM5B3KYZgeyek1KY2ndWNWhSHKrKONN1jwtZzRR5BWXj2ekMZMasOwqj6Br1zC0F/dDhGPZFNroTSiIgnyTIO6bMwDEKbLPps+iBOcHqRuKLMFoamD+13g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) 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=f0pfVkk+cS/b2HlV4J+2taPjCRGkf8aumGCPtHh3Zjc=; b=QTbJ4nylDxmKud6PcoDFyFGfaDsT8aPcEuEPMzELzgFMK/5BoX0TOkk40bkyWkSTMKTCaDfDgJitD1jsoy2CJqB8Ld1tJu8mWvE49K+yHGmeT8plZ12yNQybO0G2czSD0lQW1QB5GgxzxP0S4zfj4V5pDGeYjQQNgalTwlmqQXV70POZrRutWKTijcYaH19mxDLf6d2HEpVnDdtRh5jDlKXif95JRP236HYYLacqI72Nk2MmOcVxzFtCvCHT7gTFKWee5Tfi5dumKkxwZ61A1f53eEyEAcCijeDGUsxkiary42CCx034UtjaCysBVQQ7N1UBU6ZVr5FRtfCYlAaGzA== Received: from BN9PR03CA0065.namprd03.prod.outlook.com (2603:10b6:408:fc::10) by CH3PR12MB9026.namprd12.prod.outlook.com (2603:10b6:610:125::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.33; Tue, 9 May 2023 04:44:54 +0000 Received: from BN8NAM11FT083.eop-nam11.prod.protection.outlook.com (2603:10b6:408:fc:cafe::45) by BN9PR03CA0065.outlook.office365.com (2603:10b6:408:fc::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.33 via Frontend Transport; Tue, 9 May 2023 04:44:54 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by BN8NAM11FT083.mail.protection.outlook.com (10.13.177.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.33 via Frontend Transport; Tue, 9 May 2023 04:44:54 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.5; Mon, 8 May 2023 21:44:42 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Mon, 8 May 2023 21:44:42 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Mon, 8 May 2023 21:44:41 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , Subject: [PATCH v7 1/4] vfio: Do not allow !ops->dma_unmap in vfio_pin/unpin_pages() Date: Mon, 8 May 2023 21:44:11 -0700 Message-ID: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN8NAM11FT083:EE_|CH3PR12MB9026:EE_ X-MS-Office365-Filtering-Correlation-Id: 2bb5eba7-b36a-4322-e68f-08db50482233 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: KLbkGcBYN3iif/AoHNG+ZR4cCrwnwmR5Pd/3cSpDBaIKvWKQ09isOI1WcvYwhkcPjBl2pvZ6B6aTsyCQ7uc1D0f8lcN3JS1qzkoPG/rhPuJeEWj3vUaaa8M//QuzW10BQHSyBrAyfVDjaifLwU5dOrmCaN95FjrnFjh9yAPErNJGqQ9HrewPIS/IHL+49jaFipjxSq+BWlKSP8W2M6R/tRlW+QMze7fbpb7878twNqnBk1JZJlXjd4ObLdvgu095QXKpb/OCK/X/dBXk+q4Quh5A+XGRxm0PkIfA3sR4RK6HM9s8ZZALMI1XDNZ30qOUTzs5RtvmIFtg73wIQ5jOht9Xagqs7vaZm6RU2PgmM0Virtt4dVbfUkn+pyVVEZc2HzC8j/ET/jD1Gc3FZpL7Z/0kxp8uEufG8iAz1VcohFFrRGu1OlR+T2bE9SGHz9DpooGpiGBdfe6D3dR9um3wz0UqjYwHmy9KPnGGf6+2AXHgm+CzR/bMVsM9ScibCrA/cTyUlWObgGXD0Xl3Ec/IUnUliFavNEtdkrv4qg3KHslmpQdN6zV2++3UXFHQaEtW1IWQWQ7ZHNSP3/dCvfiXviXgHIpoj3qTXtX03h4Q/pM4VqFQGNEHSdFi/z34Ab8oOQATXmfPLEqB7WxMq25nayrATzbMlBkkRriro82qe5UKqApu51X3XJiIIqENENAJ8EfNnjv3PNoOsypgrBoJlw== X-Forefront-Antispam-Report: CIP:216.228.118.232; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge1.nvidia.com; CAT:NONE; SFS:(13230028)(4636009)(396003)(136003)(346002)(376002)(39860400002)(451199021)(36840700001)(40470700004)(46966006)(186003)(26005)(40460700003)(2906002)(86362001)(82310400005)(2616005)(40480700001)(83380400001)(47076005)(336012)(426003)(7636003)(82740400003)(356005)(36756003)(36860700001)(316002)(6666004)(110136005)(41300700001)(7696005)(54906003)(8936002)(8676002)(478600001)(70206006)(70586007)(4326008)(5660300002)(7416002); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 May 2023 04:44:54.2720 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2bb5eba7-b36a-4322-e68f-08db50482233 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.232]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT083.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB9026 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@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 3b046023ac15..b1e69b389e31 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -1454,6 +1454,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); @@ -1491,6 +1493,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 Tue May 9 04:44:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 680781 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 A1C4BC7EE22 for ; Tue, 9 May 2023 04:45:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234600AbjEIEpC (ORCPT ); Tue, 9 May 2023 00:45:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234803AbjEIEpB (ORCPT ); Tue, 9 May 2023 00:45:01 -0400 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2065.outbound.protection.outlook.com [40.107.243.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BDD776B9; Mon, 8 May 2023 21:44:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CuBIOPaxwcXWLY9mnJKn6jd90EbWO1FFWcMTdxHt/jNFeP9tOgLaZdGYpmPFU37pYgFZXSJ4siVQCS/ihJnhvtHx3ZlgwonAE1NfhlsThMbeBWhLDixd6HFebsc3ZlKmhAeLHmfx0DHybN7WAVWEfRaMaaXQr+q15b2YseZSdan18Qom/Uv/RoscgDkkNXJrW/JMhGA2L4Ng5qzfFNjSqCLiNfW+ttOECLfCUBnXitwAyS2hoQJQjBUEtvqYdn6f5hCyuvvFlwPE+F+QPJdX1KPCKV2yZ/TtBD/fLhOck6fKc5OcE0x0sF9mj+Pg5ZNEv8S6/l3jJH2BRGS2zTK9uQ== 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=VylF40GMOWMB9o+Fkn70R7R+ggYRkXjNRln4dD5h9t4=; b=ah8XQR74JbF6SO1FPrlDbe6NQUN2NCZ3+N6/ZRnIoAgjc/Rxhz4vHnSyUGoeBLhrjgkPJlGf66pjTa6g988I5LajkWVJFIK/H3XP4xyVtEfcpTvRhhKd/xLyNoqNQsVQRptvnpBV/DwtCXkuEC8fJEHD3envGSJ79B0IRPb9IMwCnUY7LXwiH2LTEQLkJsWL3o7YRoVK0VCPqkPILGGmmr1N8xjV6RbJSkfiOadVoBmE9TNLveXiv4CSlbOzoaDoeJAAze1kM2DyRK7ENO4GHCYXE7mErC7/vgzsEQJeNFckoDtu9SQjb3nnPC4zNN+0+EVNuzavGlJxxpVjkeBkrw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) 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=VylF40GMOWMB9o+Fkn70R7R+ggYRkXjNRln4dD5h9t4=; b=KXmGB59ABNZwJhS+Ujj2LajE7kdX1BY3zbOuTJAxWKE+4uEk+6rH3mHfNPg3WtFgz7+SzX6IKx9FyLt46mbLwXToq6jEwK96E5H8Z0/afNXyaSmoIF+DeJpnIJUpI5BDer+V25mMLZVRghNhK80/1u7j8foocySaVYjQjVR741yPa+2I6wTNcZuXiySPLx+KY6AqBeH4/DVQeJcXvg2fusiPV2xh18snH7J6vtBjTxz/S2BBq23jzr2sYp3Uf4L/8GFFFKJ7Yhu1f7K0RC14z3l6oyQNXis9D/DpYkGN9LnfnHnPhTeRREvgWZh/8lGinHd2ZXyareExsi5IvqignA== Received: from BN0PR03CA0036.namprd03.prod.outlook.com (2603:10b6:408:e7::11) by PH7PR12MB7455.namprd12.prod.outlook.com (2603:10b6:510:20e::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.32; Tue, 9 May 2023 04:44:57 +0000 Received: from BN8NAM11FT076.eop-nam11.prod.protection.outlook.com (2603:10b6:408:e7::4) by BN0PR03CA0036.outlook.office365.com (2603:10b6:408:e7::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.33 via Frontend Transport; Tue, 9 May 2023 04:44:56 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by BN8NAM11FT076.mail.protection.outlook.com (10.13.176.174) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.33 via Frontend Transport; Tue, 9 May 2023 04:44:55 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.5; Mon, 8 May 2023 21:44:43 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Mon, 8 May 2023 21:44:43 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Mon, 8 May 2023 21:44:42 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , Subject: [PATCH v7 2/4] iommufd: Add iommufd_access_replace() API Date: Mon, 8 May 2023 21:44:12 -0700 Message-ID: <86a2a1476dc02a0179a3f6d700910fdbf6a9c078.1683593831.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN8NAM11FT076:EE_|PH7PR12MB7455:EE_ X-MS-Office365-Filtering-Correlation-Id: e077b1d1-f866-4711-d003-08db50482321 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: BRgU8CF3KUqJFr7gYPYNF+Le8/nEiMKFAbMIsghKalzPZh3g1TEPR+Y2hSkllj4IVp8xpUGQrjICP0kORNS0Z9fHSuUcE1F+JG4eTTVH1IwZOPDrj68ZGVjeFQq/1nvqLMsDco/z13Qh3PTCDv6RoxxJIZgCfr7UhuL7GDyecf7LEGof/rjv4z0ekp+dhl4/DRyUlwODcBO+rzHRrzotPQrquMX6JTfQWcT9zBnu2GzGCGfuz35wxMjtAinSAakMyinTH9NWfJ+qxaVRnl/4wMOgwGl6A6Fccz4WxwamWS2jQa7KypGsJmVWJ7OKZcvzfxImRpH3zuLA7F6AjfoLhmIQR5K9H7rr1o/NMEN0P1BJZueEQPaNax0aMXntXdDthRpcKMj/KkqBr02GxS+5qHLL3kT4Fa0Ca7sw2SQoD9jYIgbE3Ph3b6c7ysoIMQwWm0K+akFc6c3cJB9ZTE45j4FCyzTE5VSC5cT4ZJ30KPRet1VbkCSpTXrUo7AG3wmA/THABtC4p3D5PP5YnXbd5C1Xv9EbVtztmwQHWXT28CDwA0A6MSj8k1vfaoQZWuexUE5JQcsYyvJk5j3m1FJuuiTtFqMS4PHNZwyrvsE3OtonC5KRXmCnAX9zcP1yd9ua32Enn8lHk2SITqAfeDvsOpDfmsOyeOpcF+Be7fz6ZeuLIcXXS2bkRV0vxKss4z2MhUH7qcUTNus5YaYN9ygeMQ== X-Forefront-Antispam-Report: CIP:216.228.118.232; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge1.nvidia.com; CAT:NONE; SFS:(13230028)(4636009)(39860400002)(136003)(346002)(376002)(396003)(451199021)(40470700004)(46966006)(36840700001)(36756003)(86362001)(47076005)(54906003)(110136005)(316002)(4326008)(70586007)(478600001)(70206006)(7696005)(40480700001)(82310400005)(8676002)(41300700001)(5660300002)(8936002)(2906002)(7416002)(186003)(82740400003)(7636003)(356005)(336012)(2616005)(36860700001)(26005)(426003)(83380400001)(6666004)(40460700003); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 May 2023 04:44:55.8155 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e077b1d1-f866-4711-d003-08db50482321 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.232]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT076.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7455 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@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 Reviewed-by: Kevin Tian Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/device.c | 53 ++++++++++++++++++++++++++-------- include/linux/iommufd.h | 1 + 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 989bd485f92f..051bd8e99858 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -769,38 +769,67 @@ 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 int iommufd_access_change_pt(struct iommufd_access *access, u32 ioas_id) { struct iommufd_ioas *new_ioas; - int rc = 0; + int rc; - mutex_lock(&access->ioas_lock); - if (access->ioas) { - mutex_unlock(&access->ioas_lock); - return -EINVAL; - } + lockdep_assert_held(&access->ioas_lock); new_ioas = iommufd_get_ioas(access->ictx, ioas_id); - if (IS_ERR(new_ioas)) { - mutex_unlock(&access->ioas_lock); + if (IS_ERR(new_ioas)) 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); + if (access->ioas) + __iommufd_access_detach(access); access->ioas = new_ioas; access->ioas_unpin = new_ioas; - mutex_unlock(&access->ioas_lock); return 0; } + +int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id) +{ + int rc; + + mutex_lock(&access->ioas_lock); + if (access->ioas) { + mutex_unlock(&access->ioas_lock); + return -EINVAL; + } + + rc = iommufd_access_change_pt(access, ioas_id); + mutex_unlock(&access->ioas_lock); + return rc; +} 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; + } + if (access->ioas->obj.id == ioas_id) { + mutex_unlock(&access->ioas_lock); + return 0; + } + + rc = iommufd_access_change_pt(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 d25fbd3a80b1..ea3e704dde8d 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -48,6 +48,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); struct iommufd_ctx *iommufd_access_to_ictx(struct iommufd_access *access); From patchwork Tue May 9 04:44:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 680412 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 7730BC77B7C for ; Tue, 9 May 2023 04:45:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234507AbjEIEpD (ORCPT ); Tue, 9 May 2023 00:45:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229557AbjEIEpB (ORCPT ); Tue, 9 May 2023 00:45:01 -0400 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2085.outbound.protection.outlook.com [40.107.100.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47A9F7AAE; Mon, 8 May 2023 21:45:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HfLxso3lpoVgEJXIvP8RrlqF4M2yyXHc3sFpHdAMU0SKdHtZVlbpR+utopEY451ClBiFK7N/VmOc8zpHnuxZhgp/bisA3fkaJR2uuLR8asihzElYQQ51rCqplQvkViS8V47dRgnk5KXUk5v7D4RbxngjA2FmKNe5AqUSXwcf8Ykp7GENBikuIwYZlXxHLcUscLSrQc61xZBM+FmCcTqUAwoYzlvANGs/zza8ekHnwLYuurSeVOR1uLfZlhXLepiY5HT57YgcuztyvuifJHvcN4Ff/VHYfrpXj9IixZVFYBZnifGtqrTk75rqT16UhqsP8+z9o8wvuLj3pJk7pWS1pA== 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=2NYdUJEXpywf+zdQPOocEyC5wWJhiEXVpeGoZ+OlqWA=; b=diWO1Jn09sNJx0Z8FgFjk6TZGACE6+CnZWiuvES7AffZSq7VqACReY/QYEIGtHYiffFNwJxpQ1NJkddiKMWr/iSL3Y1WR36Oc7GxWoTNLUswGa3dqViHDcNFERMtMVQrZ98L37ttf1iSCndtDLfYWnnsxR71825UK4taZXyOgrZxBfO0TCSYnQ+lHqyQF65q4HOH0z7JUB0oZpUnWAD7CocoK9fI2Fs6+lBVSla/7O9wlwJdEsyoeAEc2XTAEoA5mZEbf0j4iJ9PzJGfiPRb495xiBCq4BIZMrlC55wDG9a9oQylaTg0SByQ6oxnMLR8P6qAHE+FNYCV5jZcex2z7g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) 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=2NYdUJEXpywf+zdQPOocEyC5wWJhiEXVpeGoZ+OlqWA=; b=NKoId0Ckv4d8GOp93CDV0DzXVs0SkImyFTz96dsovFS5fRy7b0w1THFXcvtjIriIjDsY/GOFEFLMP6pA4pURDGzY8HhusPix7tH0YzSZE+GoRXKS4kDuNyLCeGtg1W2dk+/7XpU2gR3EiyJj9SqVaK3/GjIM3NIzfcEHFrQVmQ+I0/HmQ+1umYO/+rCkvOSayhSmqDObmTPJna7rc0mO9g2PNGh0XCy98TNsDdhsIaw210NbuvjeLzGkdxbj31yyAcEoSsCl3MchlUjnueRTKPDSgw4eSdXhAXQ0M8NC1+UYSC4rIsUMawlOGkHQ0Ej516qXMEWdhWuDxJr8FyUmoA== Received: from BN0PR03CA0027.namprd03.prod.outlook.com (2603:10b6:408:e6::32) by CH3PR12MB8329.namprd12.prod.outlook.com (2603:10b6:610:12e::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.31; Tue, 9 May 2023 04:44:58 +0000 Received: from BN8NAM11FT081.eop-nam11.prod.protection.outlook.com (2603:10b6:408:e6:cafe::57) by BN0PR03CA0027.outlook.office365.com (2603:10b6:408:e6::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.33 via Frontend Transport; Tue, 9 May 2023 04:44:58 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by BN8NAM11FT081.mail.protection.outlook.com (10.13.177.233) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.33 via Frontend Transport; Tue, 9 May 2023 04:44:57 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.5; Mon, 8 May 2023 21:44:44 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Mon, 8 May 2023 21:44:43 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Mon, 8 May 2023 21:44:43 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , Subject: [PATCH v7 3/4] iommufd/selftest: Add IOMMU_TEST_OP_ACCESS_REPLACE_IOAS coverage Date: Mon, 8 May 2023 21:44:13 -0700 Message-ID: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN8NAM11FT081:EE_|CH3PR12MB8329:EE_ X-MS-Office365-Filtering-Correlation-Id: 939c83e7-dfb1-420c-c823-08db5048243d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 211XZKw81Ygd1oe2f3wa+MXTn1RG/wHrKZmZmvSI7EiPj8kEmGct+qcOQM5ka8Uo00NTQFbVi7gWo3MOSsNxOZ6/ZnYmvSq9cA66OeG8GOAt8DDRj3pj59DvonDNv9yvm80LROQeLAcu7qElvAzBeVNDD5omXoIf0XF26YzIwzSUleJKIslMoeWRx6nc9TciGMTQwnQqWr/9LHvp+Ys7P1liHfMhY7HL66CSb1D6ajrnjQOIiJYgnr1KyHIOf7h4z1R/mTaRc7pxSLOdquC+8mHL8gblc76CKcKq1DM6JFqjcBLyedDNs2GoHRU3ah/KIdjUfb/onybigROkcK/hmgQcSQSbJG8ZUc/ui9MDJODLtJXIzWnuSvAx5ipX6e6Nb0IEc1Av3+AAM7ohhyk+ZVpM90wEKeauNU59ebTSsqXBb1hzN14/dAH2g/8E3A+26yuN1yhrmj94MqiTGOl3S0zTkqyIwf7NWfnspYPfYzK7yLjpzteQG2/Dd+Q2nR05KRh3lelqsdd22Zw6zBP0IkmzjCnLeaTfaGJqa8/KIUy8rML52ZOijyt6Xd1vqlvN2UqYV12ZcwLzu5R9V+By+BiStcGgRc0Iv5NQcNM6RBQbPrnPwvY+Ijbudv7k5ZLmRe3gFC9Z6FRMsdLhfEqAuF3H1+I02zJjmDjlLiR0IneY+R5Sj3Au7dfVU5rUpLkYL/ORN1PPBtkQ07i3WR3Qow== X-Forefront-Antispam-Report: CIP:216.228.118.232; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge1.nvidia.com; CAT:NONE; SFS:(13230028)(4636009)(136003)(376002)(396003)(39860400002)(346002)(451199021)(46966006)(40470700004)(36840700001)(2906002)(54906003)(8936002)(316002)(478600001)(8676002)(5660300002)(4326008)(110136005)(41300700001)(6666004)(70586007)(7416002)(70206006)(7696005)(26005)(186003)(356005)(82740400003)(40460700003)(2616005)(36860700001)(36756003)(336012)(83380400001)(426003)(47076005)(82310400005)(40480700001)(86362001)(7636003); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 May 2023 04:44:57.7078 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 939c83e7-dfb1-420c-c823-08db5048243d X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.232]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT081.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8329 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@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 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 Tue May 9 04:44:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 680780 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 BB305C77B7C for ; Tue, 9 May 2023 04:45:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234792AbjEIEpN (ORCPT ); Tue, 9 May 2023 00:45:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234806AbjEIEpI (ORCPT ); Tue, 9 May 2023 00:45:08 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2064.outbound.protection.outlook.com [40.107.93.64]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 91CB593ED; Mon, 8 May 2023 21:45:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eFyFguCDz3k60qrx3/3cJghJHkIFJq+glbTi+SKG1PfQwVWzVBm8P/z3fmeidysVbeQzwDA584ceNK098WRYDHbGZabzf4ra9elwDuUtLwF4M9z1EDzOyXOQ6mB4S4oybvGTgm4gcflXqMAcsMW0za4TuR9JXQyrpGGrCEiKQak2DT+dCy31Tl9Bh2SADaOge4WiGr8bVDFjMOOlejP/THa5z6/Lb1TlFYaHapQbMvG7vPNAL4vFuQNDkrl/16eSJSbXhBxmowUEOQivik/fHqQptvN3PkY8qARO6sry23cN6BL5FFS2v5/x9MVhk4N3NP523BiJLfKM29hus25USg== 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=Qc2aKx/azFpnC2c7hwbY0ynA6kIvS2Xy+Kbhg1tZrNA=; b=G88MhM7nazX8LSDXqPpJdRHyegQmudEb/+Os29lVSANcFJgjgzuikABtgksMNuZsSyLC4T0dM3HBJoO0CdY1MUyEkMXvBeFtHPU2r1L4BARhb9Zh3oM9iHml2X1xhXQcZrm9OVgN3wjCJ7oX8r6Earknmt3TQqqwDm+eiLw60WPTCCfnBP//xeROsrisGSH0XN5rXozi4p4+i+Orx6MdZMoTAlmWulERfUmbLxUQlt6OlrRQUBPA26WitDxYAJNS/iLoWkn+EUpIWpvRHHrAWCeN6ZRW5xGSM7cknGXWaA9BSENXh6svTsJOQ1RaiygyeWahH01GSpYoXkOaO2YXIQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none 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=Qc2aKx/azFpnC2c7hwbY0ynA6kIvS2Xy+Kbhg1tZrNA=; b=PBFp1CloF38udQJRWXqyEZIYyTKqiHTEFJ7L/CnHe+iltwvOmwNFtf1sYZbov9TPUmYoay9Il1pWTWt5JAGYZTygxjahO3PkY2a6rqla6uAWgHbFEnEoHuwx5A2H9FU+jsl1Li+tC8fwtzS+Sv/SnqlzxBGQ6ThA6ycdYLGjtWjdG+uz4QBeAK9IPuOM31p82SfUjAMsMoatYb4ORnJvazFyMFpUoakCQLlFZFJ26D0MHljqWpgZ3cUSaG8I3desyjjIB3bumkQzXSp+d5vv/xoXcXB9NP8blIhriBKn3nFGnKdr76u47aKgZnuQ8KA2084phM+/k/kSzTI6cNZlQA== Received: from BY5PR17CA0013.namprd17.prod.outlook.com (2603:10b6:a03:1b8::26) by DS7PR12MB8203.namprd12.prod.outlook.com (2603:10b6:8:e1::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.32; Tue, 9 May 2023 04:45:04 +0000 Received: from DM6NAM11FT068.eop-nam11.prod.protection.outlook.com (2603:10b6:a03:1b8:cafe::63) by BY5PR17CA0013.outlook.office365.com (2603:10b6:a03:1b8::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.33 via Frontend Transport; Tue, 9 May 2023 04:45:04 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by DM6NAM11FT068.mail.protection.outlook.com (10.13.173.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.33 via Frontend Transport; Tue, 9 May 2023 04:45:03 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.5; Mon, 8 May 2023 21:44:45 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Mon, 8 May 2023 21:44:44 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Mon, 8 May 2023 21:44:44 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , Subject: [PATCH v7 4/4] vfio: Support IO page table replacement Date: Mon, 8 May 2023 21:44:14 -0700 Message-ID: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT068:EE_|DS7PR12MB8203:EE_ X-MS-Office365-Filtering-Correlation-Id: 00908405-c28d-45f2-7f57-08db504827a2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: c4PoPlXHwGd/Ls4LXlVya0oD/eMiPBlFGses1T273esUJS9d3xlSt9EA5kVigW8Uh3zvdVpPWKbGRespZ3zsri3IdHTFlUbaxpWSh1NVK8y9lVnuR7eM3pciTIANCA/dK132FeCP3v2/qRPiaSF2RNvyFQDaz+Zfg0by9Vy3ywb6WE1j36NVl0kNDpiGoUSonzTAmpwjH6Lr2T1xM5q8SWxwO6dw4qx5rQ9p0rYvm7Q1Wezo8gU1VXXT/0z1niBYONZoGNU58ftDpUg0fC0hzz+AljAeVWy8zzFBc7sfFjX/PVIc58vYUY3P3bCOgLtYcXWNcAn4IUZrtDd5tWxYLKJuBuTeme3wP3QJbnNbUsuDeY9ZsfvqHQViHtNB4b10NWGScSLO17nLVnv95ei/4npnvyycaA+Ogz3FVOUEa14eU60vHoQR8xxdyz+E1Xflji0mdy1SHASW6VgGdCZAynDEe4lxJRyY3IGGf8SvaI88VhXhvpLtdGRZNSjiqloreJw2LBnuV8NZ4nOQaNJR/XMM9kKOBfy53LvIysmwSsgd3E+RiXm2e6h6FSV8Z6lhRD/rgXkoCQg6l8pkhfuRTEVgF7Qt7zOl0LZSJgZYW4+uanzgBZKFfu6aSHrvFqpd042G+H6Hn+74BgWLw+Bc1K+IeWcGZmK4+Wu8nB3T7OpZG32TxplyXq1lpDIutbGZOcTkm/RGR9xSBLAswbauZg== X-Forefront-Antispam-Report: CIP:216.228.118.233; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge2.nvidia.com; CAT:NONE; SFS:(13230028)(4636009)(39860400002)(396003)(136003)(346002)(376002)(451199021)(46966006)(40470700004)(36840700001)(86362001)(36756003)(47076005)(54906003)(110136005)(316002)(4326008)(70586007)(478600001)(70206006)(7696005)(40480700001)(82310400005)(8676002)(41300700001)(5660300002)(8936002)(2906002)(7416002)(186003)(82740400003)(7636003)(356005)(336012)(2616005)(36860700001)(26005)(426003)(83380400001)(6666004)(40460700003); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 May 2023 04:45:03.4479 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 00908405-c28d-45f2-7f57-08db504827a2 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.233]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT068.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB8203 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@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 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 00c14821bfa5..99eaf03e337b 100644 --- a/drivers/vfio/iommufd.c +++ b/drivers/vfio/iommufd.c @@ -161,9 +161,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; @@ -243,8 +243,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 62bd41c214fd..e57112f63760 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -234,6 +234,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