From patchwork Mon Mar 27 18:05: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: 13189807 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 16261C77B6C for ; Mon, 27 Mar 2023 18:05:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232479AbjC0SFg (ORCPT ); Mon, 27 Mar 2023 14:05:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231990AbjC0SFa (ORCPT ); Mon, 27 Mar 2023 14:05:30 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2078.outbound.protection.outlook.com [40.107.94.78]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 691CC30FC; Mon, 27 Mar 2023 11:05:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lbC1uiNj2nE8EEk4Adt51n2/pqwzZqZ7Aj+h38w3VKJOJzvHOPRzqcEiTycVwiUxt7Gz3AytNtRbHfN31M45H01GFRD0Xiy3xjdAtpNJj93kjS6MH7xSd3H4DsPtmjGCllaeNogktiWZQ9sWFRe+rkNTvMOT4xlNKe2R3gkbV5ZlokHNa/5jbZJLHyg+QbABaZnYmHl+hTU5QjNF41eVa0rJxQ8bz2iPsUkWVKIBBqPlPbrzG73R+0ihsDsmJvQ8NWJyYzMlCCuFlnHubzKZ/imwHxWDjhJYgSn1SFGq8onyh91QWICKlAH1eqK18azGnT5JIOoA9PQ76Ai01bsYtQ== 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=UhsXn0FewKVnreJMeACOaqjGG5sfQjsE7ypOeLMVg9g=; b=V/zYO3cCf6drME3gEnQr6dHNp3TZWRuEnasy7BHVHrXO15zkwX4ipcIj+XWVFnfmnHGsliK0kPLsY4qmKaYzhQTTiMzhChbGC4cA/rkwG1ypUmUVbCL5AoVmOMLsCjLE3tFtroREiUNEoB5W3kVdkr9WaXsbFcrkEY3029scwdJsGfZheBz/5VQw2NfBEGzMWD9tBDIvbLd8JqG8RnoHiPXCm2td68HBDAAZt0By/fu7SZUt2skNipai22+kY/xRwnFpdlc//tAU3wZMEiyc8cCAcSf+GkG7JtnEGu//ASdHgL5zwPc7B8/YmmDKaVG4YUFaj+ViL6ij+1lbsNUMQg== 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=UhsXn0FewKVnreJMeACOaqjGG5sfQjsE7ypOeLMVg9g=; b=Ib/ns/hkdlO+7R4TiVQA57DPiQg/cs+hJxtOGLdJK9uEFWcHMkNLMVjgKEzidHherop+VBZd9byZrHJ6uy9ADms/GbTWlND09paanZqkYhYNiZQlpLHaTo0ZfpjaoWSJDIU0Q3t+0qL8XVl17FLh0xA0F9NT6Ngq+jUOdPkmLYHkbz9eyoXgxFZ0RLa8hnfL3lAzvrZVLvD7ZllnMi69o1MMe4aPtQD5CfRxnqHzxKvnhpcuOC7jt7wqS3ISK+DdPvDmORWW2pN9GkzbzOMTMAKMrINr1IrR//nb0ky5uJ60pARYyGtsktw5vQBsDThDuGxCmP0JzRC1iyoOzvjlow== Received: from DM6PR02CA0127.namprd02.prod.outlook.com (2603:10b6:5:1b4::29) by CH3PR12MB8934.namprd12.prod.outlook.com (2603:10b6:610:17a::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.32; Mon, 27 Mar 2023 18:05:27 +0000 Received: from DM6NAM11FT078.eop-nam11.prod.protection.outlook.com (2603:10b6:5:1b4:cafe::b7) by DM6PR02CA0127.outlook.office365.com (2603:10b6:5:1b4::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.43 via Frontend Transport; Mon, 27 Mar 2023 18:05:27 +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 DM6NAM11FT078.mail.protection.outlook.com (10.13.173.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.9 via Frontend Transport; Mon, 27 Mar 2023 18:05:27 +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, 27 Mar 2023 11:05:14 -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, 27 Mar 2023 11:05:14 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Mon, 27 Mar 2023 11:05:13 -0700 From: Nicolin Chen To: , , , , , , CC: , , , , , , Subject: [PATCH v6 1/4] vfio: Do not allow !ops->dma_unmap in vfio_pin/unpin_pages() Date: Mon, 27 Mar 2023 11:05:02 -0700 Message-ID: <648acac966bac4c4760faf8be5b05d054b8f3f6a.1679939952.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: DM6NAM11FT078:EE_|CH3PR12MB8934:EE_ X-MS-Office365-Filtering-Correlation-Id: bcc5a4a8-7c77-4c57-8721-08db2eedd83b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: TJWrk6XR6dFMWNZvhSo/Rda4bTXXzSqgvis6qKF8cWtKA9p6eX29oCllDPPz8LSOlk4rKRkj7SD9HAYgQfzDX1phYxEZQRuweX2RdDEgr7m2mXgVuladIjbDPQZy1Vy7WPi+mGms/VLUbb7OZ4MQBv5850DykdnpJs678NYEhWdVHUnW6VgXvbu5w8lrd0PvW2/fwqRXiFlOsR2c10Ffwz3PFncIu6ahwRS5bgsiJbHSlkJZ/3Y+hjO+HcGqAXqqMsoaMV5SM4VRC9C9SotdMoGMw9OZRqQEf8WPn6MICTKOIm5B82qQqHuxkdDRNPAq2zM/c+F5QwfEGNwD4cRBZClAfFr1MjHcor/xXpWRJCpZx7LMtdY8RDomfjZJ35AFdSs3jVDdCMFfPdDZ9TqcTdPii7mJWb8r9qv1xftXvhyPsjPludl9F1FEt1U3+P3sSbKh/63rt0r2+hKjOy1gWytRPYwPu48x0Rb1MFWCVpipHAufVstjqqFgeqRu89xtR+ybP73zowcJgouEZ0YvjL2znXb9AadP5ilkV7jgnBwJHC3oT5zQB5gKEnZMcsc9zV4Ncz/N5EMf6l9+qJcJqE8wLHKYiGt7hfpimcSk+8Rx/usvmxS2YbfBvju1ZtTNWf8Ais5OaggpFlVmo1QBJwlMEqa66WdU368nw7Adm/vIsZoKz/Cq51t5nAsupudEFwnwJxytRwhIxSnRxtbU65BBek1rH75nBtuVF0padBB6vx1ag8/51o7dROCbNbC9noxPGvjMobYNQjrtIpPGpfWs5t+upnP1gbfQXVAn8uI= 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)(396003)(39860400002)(346002)(376002)(451199021)(40470700004)(46966006)(36840700001)(7696005)(7636003)(356005)(7416002)(426003)(82310400005)(8936002)(5660300002)(36756003)(186003)(26005)(40480700001)(2906002)(6666004)(47076005)(86362001)(2616005)(36860700001)(336012)(83380400001)(40460700003)(82740400003)(34020700004)(41300700001)(70206006)(110136005)(70586007)(478600001)(4326008)(54906003)(8676002)(316002);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:05:27.1557 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bcc5a4a8-7c77-4c57-8721-08db2eedd83b 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: DM6NAM11FT078.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8934 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 896d8bb49585..48807d587946 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -1485,6 +1485,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); @@ -1522,6 +1524,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 Mon Mar 27 18:05: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: 13189809 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 7808AC761A6 for ; Mon, 27 Mar 2023 18:05:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232631AbjC0SFk (ORCPT ); Mon, 27 Mar 2023 14:05:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232554AbjC0SFf (ORCPT ); Mon, 27 Mar 2023 14:05:35 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2083.outbound.protection.outlook.com [40.107.102.83]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64B393A99; Mon, 27 Mar 2023 11:05:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dT6+m7kbGuixaJu2WVOjs1FbleSTl8q66jwBeY+MosHxcD5pN+wJsWaBRUV/605VVqAoiY2AWjYcI7r9gCytDdDzZHrGeZghsYx8r3ZYZhF8EbtVBdSm/X+dPAhevcaDAzwB7yffugOh5MR0mVs2TNL+frTiV8YMkSKx1SdI2EvQwQWTxcfJBh02/w3Lo48TPN1Tmv5/BR/N5R40ppgxuC2KZSwcOoyzOJLDfRBnVPjzO1JUvHUSC/G/3sBxcVyi5f3HKR5NJHPjv0TZh/ILlgcsPgqXWyTIcnVa1gcy4AbeMcCG2FE4mX7wX87mjnkAjCQI+FCUeI1lp0o66I5RIw== 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=/NowpQxnwrx6PH8/RJtJ9gNozlYcnvudGcfIdWOhv+w=; b=gYfHItmga+NLSCVBtA3YKPaVl2v5R9XckqKPbyFjqjEqlwipElSeFe2z3rgFNUXcqHXKdKl1xHkPkPsUBv+zBYNcdAr6313XfQJ8WzHkXN69N6/bzbyqs/+/E91EvywytTa79A9HnhPWkGFScCULhKofioqCvj5RRw4wtb/DIXgyN6wv8dTLPhmNU+Rij7a/cvdz/Di0pDKc1o4nKvN8JhKsih33qzAvm/iEwFq0VrXZ8aBD0iT6kBNbkNcu4J296IUt8ns2hovLLpjLu3Hc+jnIKeu9LsQ5bsgJ2Y0+U2qp7n68UTQBQM6S5Uiq8m6kDeaWq7gJieBWAFYeCkAdJg== 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=/NowpQxnwrx6PH8/RJtJ9gNozlYcnvudGcfIdWOhv+w=; b=Z0FDfmuZD9+mLUrlmggwvitBAcv5iOxI1Zudqx+RmYGRdzTEB3Rn98GRzpZQcupyohtHsV8eZgORM2JDFPGlV2Hue8DP2sbbsWtE8k7sTOcwNL+St3vlN7X1lsRhdaev/S1d07vnpzUOW63TBZ/tm2r0SklgIFVCHpUoU0QynnaaRVZkOAK6o5DbIMRTsK6zu6+SGkghlrTl+VmnANgsexGM4NOIjLp2ffDQJs+VbAEAq0B6PT9JKmaff4gVnU5wHegUmqusoxKGyf32RvNoGyLpKRgCTh3a8oDxoKzQ6wbMiGkesmNDnI3N6L7DjCun8dkfrdqNVr+3CXu0hCZ1Pw== Received: from DM6PR18CA0029.namprd18.prod.outlook.com (2603:10b6:5:15b::42) by SJ0PR12MB5673.namprd12.prod.outlook.com (2603:10b6:a03:42b::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.38; Mon, 27 Mar 2023 18:05:28 +0000 Received: from DM6NAM11FT052.eop-nam11.prod.protection.outlook.com (2603:10b6:5:15b:cafe::50) by DM6PR18CA0029.outlook.office365.com (2603:10b6:5:15b::42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.41 via Frontend Transport; Mon, 27 Mar 2023 18:05:28 +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 DM6NAM11FT052.mail.protection.outlook.com (10.13.172.111) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.22 via Frontend Transport; Mon, 27 Mar 2023 18:05:27 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) 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, 27 Mar 2023 11:05:15 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Mon, 27 Mar 2023 11:05:15 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Mon, 27 Mar 2023 11:05:14 -0700 From: Nicolin Chen To: , , , , , , CC: , , , , , , Subject: [PATCH v6 2/4] iommufd: Add iommufd_access_replace() API Date: Mon, 27 Mar 2023 11:05:03 -0700 Message-ID: <955f7464fafc72984f3ec441671f37948f01d714.1679939952.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: DM6NAM11FT052:EE_|SJ0PR12MB5673:EE_ X-MS-Office365-Filtering-Correlation-Id: de52682c-92be-4f14-87dc-08db2eedd8a8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ukVYYLH7OsB24f3MJyVVeuTBxJskD/Dy+Hxcz3JJ8qcwD0Wi5iNpBQ7euwwSV73AssXxGd9vwD0lT9h18eebHI+qqafM3CeawYq5TVJGxaV+Sh41Fqz3t2xK3LVbq81eHseDDMOOXuNbCtv0Mjn/Xf1besdKpMDai75TT1Cyrso2YNjA28Hx50W0flL25v1IfcldTpJ5u/6v5jcDDCTvsxN4lSjhR0rFZUNUpH/Ica1KpbsgsGy/t4PKHIaBeGgbiYNnMXWTKRuHbvpc7Fpu57RUh2lLA/qhivR4MiVrOvWhl9oG9iqKVXd97nbQfgjxm8ZpsnZn2msybzDog0DvHyogsKlN4o/5S61tppDI50x7Dh/60+CsuZ5fdQIjhZsw/h+ldP9DMrduev2h85rpEqYzTQRm+xNn/ZNXe01QEZaERgsju+3P+mGph3un/wb9JmpTmHBLx3g0PviKcflGElXICLQ3uKLe5sb/vZ19K7ZblzN/37y5PYs7r6koO6q+o4B2lPkHTacHSpSehrWKludLfo2tfadCDLQ9F0ivfFC87rRRJivz+cPyB/VuIvGX7HUc2ca/KcoP8MOo+q636bVsXC8FqFifHj4JTdixgHSSRLYPUUZdseY46gWVHSZceXHsXbPakCavOY3biTGpdHHzj+kTqLWQuFqiJQFLaMHXL4hfqJiRG//WzSTqlejcTnPu9KQIP2m7VCoFofMwX5qtwJ9aqsh+pvsY/iqCaJqfrXm08ZZetA1PGV4+tgXzIgKOxtFiJK6okuJdMBuwEL+sOTpQ4vOHTxPXlOoWx4Q= 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)(346002)(376002)(136003)(396003)(451199021)(36840700001)(40470700004)(46966006)(356005)(83380400001)(54906003)(7696005)(34020700004)(186003)(110136005)(478600001)(40460700003)(40480700001)(6666004)(26005)(336012)(70586007)(36860700001)(8676002)(2616005)(70206006)(86362001)(82310400005)(82740400003)(2906002)(316002)(7416002)(41300700001)(5660300002)(36756003)(7636003)(426003)(8936002)(4326008)(47076005);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:05:27.8701 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: de52682c-92be-4f14-87dc-08db2eedd8a8 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: DM6NAM11FT052.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB5673 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 Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 53 +++++++++++++++++++------ include/linux/iommufd.h | 1 + tools/testing/selftests/iommu/iommfd*.c | 0 3 files changed, 42 insertions(+), 12 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 6f7b349ba8f5..b6d61e886fbb 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -752,38 +752,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 != NULL && access->ioas->obj.id != ioas_id) { - 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 != NULL && access->ioas->obj.id != ioas_id) { + 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 135dc2a28156..6752c58226d1 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); 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 Mon Mar 27 18:05: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: 13189808 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 A441BC77B60 for ; Mon, 27 Mar 2023 18:05:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229746AbjC0SFh (ORCPT ); Mon, 27 Mar 2023 14:05:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232523AbjC0SFd (ORCPT ); Mon, 27 Mar 2023 14:05:33 -0400 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2081.outbound.protection.outlook.com [40.107.220.81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24F8635A1; Mon, 27 Mar 2023 11:05:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XbE8POiWwF7a/l9myPHtLZvSktOya1ZIMnkiEUycM5mjOGuG3mRXx90qkQtwohiEbc2DuRDEhwLpLtU+MeUmaUqqcvIWpbMBNPEFla1hp+drR2UrTxjxQDAMtvB4QlrZQhyInIKDEWzQGaGhTa7K+os2uOXoiNdbZpriPCl6e0JiWi24ap8lV+NRxh86X+JwcQ5fBUsVmcNSTdOB1lHWAO4xs33xNbu5C5rWZCGE13TqsREi6v5D7Su7nTzrJ5W6XsffMJyL2+sRwOiCcmPUqKcjs5MEA3rSxbTqDOIITOwzZmUwm1H/DQBlG/N7ygHRhJUjukabrw0hg83U1cg9fQ== 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=JSPe+tjLHzxQWVI7DcLOpIVRWSdFYoe1dYjXnksZ574=; b=HcWPN48UsYj/Ai06OsO7jbTzBNVzZq6EO480b3olTbtt7OMme7WtqmBdEKxY6CcRw+So7d/nXshisu2pEUpnQd1IHF6Cvil7C+lIPOWC+nwXLc1QXGPeKultVHdqe7miVkZA71F0ruRixagmFh9qZvBSuC2v87wAJpFeAB8JPoqPBE3Es/DiHA9KSEXPInkV5GVXgcsCXpbb/4FbsIUBIs249CXWH6OuR/7JRKrGkHZ87Q2XSVWekO+34TkVD4aXIybbQY7HUEnfK9ndbaHvd2QrdtBVWu5AIaOl1HCBtNM6CeVv5XEdKhVaQ0qVF4shZNPVoSj9q3qTFbjOZuk5/A== 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=JSPe+tjLHzxQWVI7DcLOpIVRWSdFYoe1dYjXnksZ574=; b=nFZsTSfZJ3Syb5AUsjZU9WMU106oQsC/tU3e89rcfci7vqMIPWJ/Xd8uL+TleTNjaO066jp+/6sl2oLDEf54tT5GF/gq9nWk6WYktillVMVlxOa+C07lwV480QB3pKOMxEoWA+/Wk8qtIpQixn3C3OxNjJGaW/VkmC7wZra0H4JPK3TaiQwyrPsmGCexTL8nudZcWv+Qxcyw16xcCIkzzgDdnNmNMyuZMDtd3bYZmBd+ZqviVyqlWLiSXHcfNaaV5lHK66km4JkfOm7f5t74mrI/dQSu5/yZ64xq+bJMuf5ukc1VteMENWziddx6CaHCyqrc4a/AVQZgJGai7rV84Q== Received: from DM6PR02CA0119.namprd02.prod.outlook.com (2603:10b6:5:1b4::21) by SJ0PR12MB7036.namprd12.prod.outlook.com (2603:10b6:a03:483::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.41; Mon, 27 Mar 2023 18:05:29 +0000 Received: from DM6NAM11FT078.eop-nam11.prod.protection.outlook.com (2603:10b6:5:1b4:cafe::59) by DM6PR02CA0119.outlook.office365.com (2603:10b6:5:1b4::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.41 via Frontend Transport; Mon, 27 Mar 2023 18:05:28 +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 DM6NAM11FT078.mail.protection.outlook.com (10.13.173.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.9 via Frontend Transport; Mon, 27 Mar 2023 18:05:28 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) 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, 27 Mar 2023 11:05:16 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Mon, 27 Mar 2023 11:05:16 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Mon, 27 Mar 2023 11:05:15 -0700 From: Nicolin Chen To: , , , , , , CC: , , , , , , Subject: [PATCH v6 3/4] iommufd/selftest: Add IOMMU_TEST_OP_ACCESS_REPLACE_IOAS coverage Date: Mon, 27 Mar 2023 11:05:04 -0700 Message-ID: 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: DM6NAM11FT078:EE_|SJ0PR12MB7036:EE_ X-MS-Office365-Filtering-Correlation-Id: 9e36edc7-7911-48ab-3c47-08db2eedd938 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: aADICFbbBGiZzndSsHIWlnsJh4hRrOsyXdyGksCczgxmkGHbICHg1t3Gd8yqgFmBZ0lbwnYrC6Srsl2ucDt7tuJkLeVGZ+x9+8TvGx43FFcZ0JijtxjQgRVcF0QiYIGxgToGRtQwUHLtssCVbqpZwVJaEOYJL5g0BjFleK5zq7ZGt1qXPjFOSaf1zUIdi3w+M3XDwSSAhQ8HDQjAxWLJ253R12PACfQEw9ME3geppzczzYqiCulnUvc1T0MoFUZZgmGrkZ3pCJIMd4sUxU3kVg5AXs2jBfO27+deH2lBXbdhoJu0IKBsTgBlyyA0rD5lHUAoQX848Fs8kEx95g/ZfKG3BVQiS2dXJZ866iNLNJI2hqyezK9odw+rhn7J/pX8LXEiBSe00AbaNyCpimiaro5snWg1QDBaOjT3U/Mc94uXXiwbX82ny9k87YnnblkJFvzTUyXxNvtjmE47dOf4NQpCBtQHNn2LeI9+8OFuSRscmAi7Rsf01Z3l2MJn8k3ZdNr5J6g7EQD5Dq1b/JPf5tJP/EGnDCdh+keCF9dqDVBrt5CNb+DPcsVQH0OfUFDc++ZUW6DQNXaW50KqhsKtQru25gkoO4mVDf7ZBe66+mejFGcKil8o671jsyKAK/rCaJ0bXrlsEOY55nQxyX/PSD+Pf/Wm/AQclDlCU1C6c7gsycI6LpjfnrVH9SMRr/p/B3w06VjSvD7TEMgpNSNJsugMg+Vy5fa31nuLR9eadyKa6i5P9vWcUqneqISKB8315KJitLG6pqX6sHXNMmbOMg== 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)(376002)(136003)(396003)(346002)(39860400002)(451199021)(36840700001)(46966006)(40470700004)(40460700003)(36860700001)(34020700004)(478600001)(316002)(54906003)(110136005)(7636003)(82310400005)(8936002)(5660300002)(86362001)(36756003)(2906002)(7416002)(356005)(70206006)(70586007)(8676002)(40480700001)(41300700001)(4326008)(336012)(186003)(2616005)(426003)(26005)(83380400001)(82740400003)(6666004)(7696005)(47076005);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:05:28.8119 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9e36edc7-7911-48ab-3c47-08db2eedd938 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: DM6NAM11FT078.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB7036 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 2deda489a934..7714d8f53603 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 Mon Mar 27 18:05: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: 13189810 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 D3EEFC6FD1D for ; Mon, 27 Mar 2023 18:05:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232707AbjC0SF5 (ORCPT ); Mon, 27 Mar 2023 14:05:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232556AbjC0SFj (ORCPT ); Mon, 27 Mar 2023 14:05:39 -0400 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2085.outbound.protection.outlook.com [40.107.101.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5887B3A90; Mon, 27 Mar 2023 11:05:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ClmIj8Y7i1MasEzu0Zf56GXQPPic8+WWoVqDTggMF0I/iUf0uPoPmq32J0c9avtaofuHKekFrk6CpNaDbaFPOQm0Q92v4+Iexiyb9gl0Xbe/I+jKQ5zI8piAQz8ucO29L5FkDyPzn+09EslDQaUYcKx3XuSgiB7gdEGDvVruL2eji3d6hFp3KGDmH7agIoLxG7ATG4rwvZrZ0CdGslwij6cLDNMUb7bx3tFWLTv2Q2FLvRWT4fnNC1sYGotgPE0wUrrHEpl0+baMhGe2B7d2oSGcKTXSHIAsQuRgLqZsITyILirp1stNEMpsCtAwjP72o8gQDlUTiecU/8F6NjcwqQ== 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=N26e0KwqjdFmKEVlqzoOUIgCYtGoSWRGIBJKDYKI73k=; b=H3RVym2mP98Stbp/TvLOughxBpf0ltZMI1lpegSvkJUpoWnaqjPzluFfIe8hK4aBwLY8kCOY0Ufhq3iLpWJKbVNuMT7URmEII6Z1E9dZJdpQy2DXEJOe8kCiXbY+lms/AsGyTGsUHtAhBX8T23VjWcNkpsnto/cUcnPzS7pmna+BarRCkmlOgxpt+6Ad4QssO4GJlvKmG8Ly/jvGhI8j0Qg/qYZrUdDdcOvW5r4yRq3zKwHlB2jy2dk8exG/KIF6iqqztqreWAv+sFsSrG0GvJiN7QIHqYTX63xawgTyFUWnYSKoE6cOIauGqdZHLuLKKzsuk0mP6KkJl/nvgq3ZXw== 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=N26e0KwqjdFmKEVlqzoOUIgCYtGoSWRGIBJKDYKI73k=; b=lM3RE6YG7izXWMswAZYT26k5t4A+3l8ScHMKsZZiODlgkaRnfRuQ7RDOkgYvaIFT/ItNVb8cu2a8J+u7yGza3nQ9eW0DSu4lJweCtSRHVjtQZ6YbFSkML6Qgy1dtx6pVJR5e/dvalnfT66ToZGNCatPeOQwxsJQgKCrZFOXt7nBZRHv4CtTrRU+6W74iTKobrze6kWgNsZHL2kKVK1xynHsd8OgbTF3X2GaPTyBPEyTAdbpeEo3xjdK5N0oH79c/Z1joUT0n3D5gEM3P3P6YfZNlN5Ncy50saTOQVIJGKBQUkM6+N1xfsf/XcByeq7ZCDvPhJFWLtGXYqNgTIAmd0g== Received: from DM6PR02CA0121.namprd02.prod.outlook.com (2603:10b6:5:1b4::23) by DS7PR12MB6213.namprd12.prod.outlook.com (2603:10b6:8:97::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.41; Mon, 27 Mar 2023 18:05:30 +0000 Received: from DM6NAM11FT078.eop-nam11.prod.protection.outlook.com (2603:10b6:5:1b4:cafe::52) by DM6PR02CA0121.outlook.office365.com (2603:10b6:5:1b4::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.42 via Frontend Transport; Mon, 27 Mar 2023 18:05:30 +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 DM6NAM11FT078.mail.protection.outlook.com (10.13.173.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.9 via Frontend Transport; Mon, 27 Mar 2023 18:05:29 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) 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, 27 Mar 2023 11:05:17 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Mon, 27 Mar 2023 11:05:16 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Mon, 27 Mar 2023 11:05:16 -0700 From: Nicolin Chen To: , , , , , , CC: , , , , , , Subject: [PATCH v6 4/4] vfio: Support IO page table replacement Date: Mon, 27 Mar 2023 11:05:05 -0700 Message-ID: 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: DM6NAM11FT078:EE_|DS7PR12MB6213:EE_ X-MS-Office365-Filtering-Correlation-Id: a1e8628b-7769-4509-6c0d-08db2eedd9ed X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xqlVwcrf8yBk2XVgXLIQIPeWZtFQoSsqb1Uermq1FECQarP6QOHpOeVY4D6NOUtQ8wr4e8fq96F6YnlFOyjfcxP5/NQLlnhvPelnSVHRJFNVzwjdJHSY7866XHzVK8SaO0OQpei/OmB3L4Heq1/wValXyJLKWmlyo0xGBDGsRCjriOMAWZIJSdAq1/m16mNptY71iiPEH71k057PjnC010jE66pfGT1Gp2YyvJKeDqnX/ETpawqize+CqflpD+L7kbLMuPSwMrmlO7MtB6+kHwglZjYUPlvO6nP5MCd2nhfLA/Vto1pqKZpcFe/ptL+RqO6FNC4rCrzcMsW/TzprMBDYUTGB667x81ZEp9AkvRnWJwgNw8xOdB1NbiDqOaDD+qhczEbGE3uq+0N+BeuqZjAHh/+aKruubEMYtart/R8P0LhrrzaIY2WHQ3JYrXMEcOn0Acz8ytMB8GbXIqM8bKXpL/sX0+WXMKxj2iwIt/IIK8rBgwybeIPPRQDqmPXflgBE3YBwXIdTCFr6u6O6/3yYisU6CG9VyxUKOvDunhGLSFDfJG7Ny1RHBopjWMNKUAcqu32sXd0MvkphyrX0gDSiYZvKUs+/W0MEKxXEHpIGWuh1VU4oTQykWWIEtI1ui6XN4iwtiXh7hEBQ+uEGbo6l3yOvsk+mLUJFOb3sGFzLp+Cmx3Ua7HiWSwkZt40LPc4A1aXCmzBepmwRt9Ry3eEn2355aavPey1zXAtY+fTuDcE6m92XNDWmEvQm1I+gWEsETKJmN306/co4R3SEJ+c7O/qozopyLYc2IW5kg2A= 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)(39860400002)(346002)(136003)(376002)(451199021)(40470700004)(36840700001)(46966006)(26005)(40460700003)(8676002)(70206006)(110136005)(316002)(4326008)(70586007)(34020700004)(36860700001)(8936002)(7416002)(82740400003)(5660300002)(54906003)(186003)(7636003)(41300700001)(47076005)(7696005)(2616005)(426003)(336012)(83380400001)(6666004)(478600001)(40480700001)(86362001)(356005)(2906002)(82310400005)(36756003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:05:29.9837 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a1e8628b-7769-4509-6c0d-08db2eedd9ed 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: DM6NAM11FT078.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6213 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 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 df4d6e0739b5..2d97424be582 100644 --- a/drivers/vfio/iommufd.c +++ b/drivers/vfio/iommufd.c @@ -121,9 +121,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; @@ -198,8 +198,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 bf6c97e759c7..e067cc5ab0e5 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