From patchwork Thu Apr 20 18:51:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219108 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 ABAFCC77B73 for ; Thu, 20 Apr 2023 18:53:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231491AbjDTSw5 (ORCPT ); Thu, 20 Apr 2023 14:52:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232036AbjDTSwm (ORCPT ); Thu, 20 Apr 2023 14:52:42 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74D3244BA; Thu, 20 Apr 2023 11:52:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DQa9c+OY1jJHMf3qRpoUkegQAJuUhkHNqrBI0pFyudkcQUcli12DyW9Yr717riwsKnmK46fAhJQuwokZfRUW28qxWah3DlbePPkaBZ7bwDBXe9w+clF5aZT/Fo+AbcVuemWfStyE4iLbQZf+d8Noacy4MPCJlMDcMvY5mRKRR9Pht49IN8CfMkuduaY6cUe/gR3Y1kZOmo6X5JbVtS+/cVcFIafnv6JickDvH3rAE+4kGdTcvdYYxtqrNar/eFMvdCHMKDLiGwlYx9v9iJ3wUgFTaLuWuU5cphGTIkKy6KzKxtuNUfjNk2F+qjDhlkKIyQmR5iJPo9DpyYmgh5HngQ== 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=3blGAHefJ4aP70tNrJtt6rGHVG5EmtnNNBaQcgWD0rI=; b=HftrH7S+VAVOKLIlNQUSU3CyFP8OTnydiJHBxPGzP3SqMYNcupC7yeabLQWa8P4nJNox+2xPjOrCktItRwuqrdMZY6nTiAR4KxPaf/R3n1rSeQWmdzjGZ18K8IMJ81w0XYg56vTeGhM0BDIfKgzp7ijPIhtLYZtr6jbHmKppAeOIgMAwLOoKPkGtuBM+gtaxcfV9gtMWeq/Zw6+claJIEuPqw5zBNHt7hrC3t1R5DRci1TUEjo1ysGowdO7poA32uqRXVnYONJ2J7X9wxvyMSoQMDiO4XhmfSrJMwRehSjfPmzDNMRtCtvTKwokxW7nH+OLGn+YR2sozVXM4yoZNbw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=3blGAHefJ4aP70tNrJtt6rGHVG5EmtnNNBaQcgWD0rI=; b=BAY3YUFVT33oOZ47Bn49fiHEdhUWf5BNG/P1k0+ihKtPBEk5FxfF41LF6iBgyRsNtW/oos/Ex7daXxUfJmmqdggEa3jEEMbFjZZBQ1F1PS0F+cSzWK1F68by0Wt+alZJBmbi4FsSIxB5aGo7VRM1q8cLdCR0lvX3+BznJkNnvMyzzmQBc4b9rW1ENsbae/NNzPap96bhh13CxNX+KduV6dFLz1OXO9fpadY75BA7yCyEPb9KuFOYhWWiTrjM50/+iv065Aa3K5TIQUj2DplrynbsUO/mJardhXkYRNwD2RHXbFljVPvlyg9oIfaW6c1hDqChzZXuGBsNfN8ELwdkXQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:50 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:50 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 01/19] iommufd: Move isolated msi enforcement to iommufd_device_bind() Date: Thu, 20 Apr 2023 15:51:16 -0300 Message-Id: <1-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR03CA0013.namprd03.prod.outlook.com (2603:10b6:208:2d::26) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: 3be85793-1341-4cdb-90f0-08db41d04738 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8znuAU2rc5bnucutZqWqoEvHStBSWRmEb5KDLypr0rsjVwHSJoekGUqtv3SVbqEK1S/kv/Eoq2E3RLypdJ8cs3egi3fcJtfeofHN28OuLZ8dv8yqlG51HEqXWYWL33udbTCHrp1Tk683qYAjlDKWu/ktWo+nkt3rZ6OmboQ7sXEsX2b/iGNuJ74mWwTowqvulUJ/zQv8IXhfkrgRpOXMZjvCytcJET3vH968JVUvhqnjlHSSOBcs0859LAetEBuUzh9NhsLtXkstNw/6rdmFyOP21Wx7AkUhd+FlsOZ1FvfsgQKXcSM5rKjfzrHsLcki0i4RKiQtzikrEISKJkMwte+m5px8pl5sdDCQKmvXPJjayESpUW1a1+F2lTCvBeyLVAuogpNfMhg9OKDLQLwwCy9hPtk2a/FhYwC1RTp+RZDpVCgbUdQYNFnMejPWwLZY597yZ5tFcjJOpA9fYL1y8mEN7zZpHjm2m3s7KtXjvQAHywU/+XE7aRix1qvuHXInaNAVJv2pCoWWzQ9wuHgo+RO0DHRoVu/JPWn+keSKN527VvxI9l1BC5WYhIvnOh2xR2dYf4bmkkL2ULbE5qw3UH8FsiyJtKB4bYeRezFntVI= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: f6SyglhUzw/YpbAEJKQgALHl+Xls1XI3RuPCgAw1bXFqUQ5lnx6PkTidQXo9U4YYsbzBfFaL6vayNm4UIz0TLjRhsdeH8NnJOcf/ikLbfb/aU/apa/oNve+M2k2uy9S0SGIxtR9Gh0xF44p77j04Wp4JB7bZE4nYSuQS17qMXbqT/+IOUVlsdXQf23+IkIZ4ljJQEXuHsQ5wWzDR5+3794W+rbUx5AZeh0O1918n1TDiUjuh2+SDOPAQy3fTvPIhRzyC20muQ0S+6fLaygIHbdMXp6PCJ/yOBTXW76uXsSfLwVjlzd+vd1Uq2B42O3nwLD3Gu+t1WMuAOpjEEq3w89YBJcgiiOvGyRdDqv/qLCSxfoYITsVTMzEPcFPHTn+EEA5gEoy3NrptO8BY/RGbohYN04iHs2RH97Cjlk/y23ouygt54lBQDEfesCc4Y+HQTwtTR0hQGEauPGg/7aN4IfywMKANk4lv5aIK5j3r+z8woanNHHJzd57D2lGmCaZhN12ZgxavOyhu13nnqchitdLH+qtkvpeSYipBz3ZnAtMBHDSyLeDJwZlucEDtj2bmiiCKsgXVRI1BI1TJzk2KyCzybYmr/OUesCZubs0ET//nIBFVjkSzxcFQHIasYEpGmB/coudRDKcZpuI2z9uGj11FQh7YJSu7rNwiCVdiHfRYWht1RVwsfe+BlDVh0o75R6h4gY1vQeMeHJUEYFDkiBFLVBYxKqIRnWhMuPFQHFSgH/R0TeEe8H9EsYc/wtmqJMuIe/5x/XcEOGzfFliecHEs43J9T4O4ldlzkhAckBTb5fCLU0SRTK7keaNlhkA7kLcOPWg49oX7vHbgxqkv1E5q8RfRULqv/mPdNHnq5LMF2k0LSdIsjL1VwzqC4jK6OZSIbPuk9pEd8U5AeCQV6tWwAG6541gIGnVUILJomQFZdIYCYF3B+nxPoqB1n/tEwaHhcGVRmgNP+14KGYVlNB5fHxBpZ/3wJU9r76N4EF44WC+WYzo/ffPAlGjQBdSKSFNrYAZjhUvGO02AmRfa75C5hBQMPLVW6tGuq9MSac8ODtfpUhufqsZery4iqwrGPLRpHcxpTLMrJW3NCoyxqccOWYSis1iRdNLrQuAeAH/CIwMxdRhW/vRZNjpW6AvQoy2jsFVTg9mrEEsL0i8fLo4jVICE+4vevKhOPpNRCRVv37TP2xpfhEVL+76CBRdlofR+FXWiDd+5nUMk21qpDIwPafVcjUKztWqolX22cXHnU4urnnEqyObVAoBKDvvB81go1n/7Jrh2B0tzid+CPplPbw6Sc3bBQLiULYJWaZjmUVOH9Z6lVBLz3zJJFMhoQtl/2CEmgLYU3Mu3WLEkbgGMSDYITkUSU8xsq2pdfKkOj34tJC8WnHZsw/1ZrqaYG32GqsyE6Q27ZMkAwhvaF9404FVqLh1kDE8UOdi51ZTdQ59fgptzk9cV1yrvXWRnNGR14IVR+d4SBMzGMxaBBHFFLokj/vCRUOtM4iJwwu1Tho1ygAGzhTGX2diUnxzm6Pe7Xz4NXnTp4oN42yCGHckFt3CUITcz79/ZA6zJRSrlg0BRJ+UOyzoQso8/X35g X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3be85793-1341-4cdb-90f0-08db41d04738 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:41.4215 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: vivszOU8MAxWsM/17eN2qB+HOGDMujFh111hLLakT/gngLE+EcUmnISdHEyMH9Tq X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org With the recent rework this no longer needs to be done at domain attachment time, we know if the device is usable by iommufd when we bind it. The value of msi_device_has_isolated_msi() is not allowed to change while a driver is bound. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 38 ++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index c6f4852a8a0c08..e66303c17c894c 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -60,6 +60,26 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, if (!group) return ERR_PTR(-ENODEV); + /* + * For historical compat with VFIO the insecure interrupt path is + * allowed if the module parameter is set. Secure/Isolated means that a + * MemWr operation from the device (eg a simple DMA) cannot trigger an + * interrupt outside this iommufd context. + */ + if (!iommufd_selftest_is_mock_dev(dev) && + !iommu_group_has_isolated_msi(group)) { + if (!allow_unsafe_interrupts) { + rc = -EPERM; + goto out_group_put; + } + + dev_warn( + dev, + "MSI interrupts are not secure, they cannot be isolated by the platform. " + "Check that platform features like interrupt remapping are enabled. " + "Use the \"allow_unsafe_interrupts\" module parameter to override\n"); + } + rc = iommu_device_claim_dma_owner(dev, ictx); if (rc) goto out_group_put; @@ -146,24 +166,6 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, */ hwpt->msi_cookie = true; } - - /* - * For historical compat with VFIO the insecure interrupt path is - * allowed if the module parameter is set. Insecure means that a MemWr - * operation from the device (eg a simple DMA) cannot trigger an - * interrupt outside this iommufd context. - */ - if (!iommufd_selftest_is_mock_dev(idev->dev) && - !iommu_group_has_isolated_msi(idev->group)) { - if (!allow_unsafe_interrupts) - return -EPERM; - - dev_warn( - idev->dev, - "MSI interrupts are not secure, they cannot be isolated by the platform. " - "Check that platform features like interrupt remapping are enabled. " - "Use the \"allow_unsafe_interrupts\" module parameter to override\n"); - } return 0; } From patchwork Thu Apr 20 18:51:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219102 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 7E6D5C77B7E for ; Thu, 20 Apr 2023 18:52:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231708AbjDTSwZ (ORCPT ); Thu, 20 Apr 2023 14:52:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231927AbjDTSv4 (ORCPT ); Thu, 20 Apr 2023 14:51:56 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E874C35BF; Thu, 20 Apr 2023 11:51:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HP/0m6W0dDW5TZ/unYY0S1xZrt5a7+plSK3FgfDbs3t33A3bL1+M5OvwGDyXH1qOq7rtcIsYSAtAAKUHz0EwhxEFj2/dm7PZTXOm+n74oCGvVmOcXxIfBa+tCuRer4JufX7DMsPthj5XG0S1dUfUYQDyWCRKM+K0SoeDwNugABMHtjjCuVxE7Zq2MrIhPvkXevC3BtFCQEwah91JbxuR/8IJ7n8iIYxk2QY5wSABHR5tIQoG4B2hKewxpmhVt8la04JIB8rqNRLSjF0f4DiyF3vVUacJhSuYMmT+duSUSKCJOwwKo8GZCVri56HCUn7XBiUVGLDXoyLxR69Qq6cilw== 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=CZKnolzfT66UVjhCW5P9kzq/7vlXczbFBSK46RGNX3A=; b=YekFlJ70dNMWyqFQkl1YCeUllePoQ0jHQo6LnANWgO67Ue++iaEMuP4veJqW9b0Th1ibFIy00K7O5CZTkMmAbBWgpR7emH+gFq/SUi8S4oaGv7DA7p9RYKRWbasvjpZrGkXuiENmbvuWegmUWGXJ++JhBweUxtJQra03KsOg+ROZ0Cv5io0duKkwTgeAc/O5YCU0aQiu2QJO5rDV8zyKK1IVjV/2SoJypuhzZEqT03AVkm9XbS18d86u45e9A8MR9W4WVkt4CprtO+1XUqXqIbfQCWAACnjSu2jFiZOC0PgsXPHFBfZvb+jprqCi/33LbLP9gqA6vBmK3VHJ+WGJtg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=CZKnolzfT66UVjhCW5P9kzq/7vlXczbFBSK46RGNX3A=; b=BcUCbhKulMNkoYVoYf5IFEMAnYkOc0alpXF7duqWG2WUpHorVGqfB7EC0X7bZ525uIsSlxWB6c2DxRMU14Gk/KGMPwcgPIm26LWMtKV6LobYPvAVj0fI4ncIFWqvgtPO7c+TnWgvYJbNqRnMxGhDv4wXKq6gLaYCbEVU2lnc6abLvDERdZn/apVknqTosLtkBvf0hRGB0REHnOdqheCY1QtvyM3USeGUpG/9w8GtYj2WSvCYVRTI261Rl3ZR+EzUDcVRAmjSgZh1SwcP5Oyx27ZWYnp3QiwPN8rlF9+7Qchu5khe+zFvYNHAS9WZRhSXR//KqH7U7BA4hUa1QxXgjw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:46 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:46 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 02/19] iommufd: Add iommufd_group Date: Thu, 20 Apr 2023 15:51:17 -0300 Message-Id: <2-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR03CA0030.namprd03.prod.outlook.com (2603:10b6:208:2d::43) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: e848ac29-0fbb-4770-1941-08db41d045a8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: LBTuffLQC1bzVNdUh8pOx5R6u4qomhNMIhr0Ivny3NlfYIzeFO65NMNMK1QRMN68KPJ6lMtRSeo8oFc6G+vIowSmrLfsNDcVQtuN1z8UI8srQA2bbmZCft39deiVOgV1Ozer8GTdNc6n7o8RQtIIz7g7mjKXw+HThYitQvvTegUgPdxJaNeiBV17CvN9QN9bFxjduOHH4egluYmXMdX6wV4U3WqO70zj/YylreU6NBSKo5gvn3B/KIk1oWTZO8G1+uSx55MEy+Ls0LxyXemoPYCoO76LGAmNJ9QVI5gdSwlUwNQ5ipnRPpgE4DvdbHNPZ/mjMIjWYAXb+j6Mh8WaclQKyCevq/hDjFgjzyVrEb6hF7tSwsP1VlzxEdt4Gi1yn3V73mvohs3cw9KtZPAoeQucIdbV/V6Qbu/nZB8SIDPJZKd5aI+Gy2zEmKNX5+5uFlQlS/NdI1scV3PRhxlzsHTtoQ1kVHcq3WuYTQKXC3+j1PYpru6pjoM2ihDswo//NxBfVIm/7ra4mRL1tJDsoBk56wR6VE7vh5ZqZUZyd/IELjg6s1HVE+23xCTSaPZTi+O2OYKbF16AYXsQhZXew0OHOlU6gdhOPEzxPWQJIfk= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Q9I0Pnms+yD9cRvL1eEYE2dThNFwWvGjkZvQ6Pc2RsNXGjfZTrzOx6G3yFN5yrFMBSIOZcI7EQasc0qCiT8suKklSwv6BcECcuFeUYEx0ndXoTnwwdS51vdVXlwQYsTp83I0cV3ilZSOyI981YQ+u/r0uwwZzuRcl0SuzoQvKzrla59pXyANyXNOl/umDKV47ixg4LkytgVVkGjEKF67CJd/+n/z4IfPZGR93lhRKVYQ4B/VgBnv63AGsMn9ok5JgRwYcramBwgJKK5QP7o86BxaRZUTAHE8n0KH+eBrur9iyAl0dKA8MLZx3WM3wGsatYyi+/yiicH21FwgdLJR7EPLlYGGAX/eSFI916wyjt5NGsMLX5beZOSPXZOMWJud37MnqVTYxW4sqDVFgZFY1Rg7e0ukc49Oczi/9rWRYuxJ3skQwTiVLklNV1smatWxivrmxpLR8dwPfziK5JosNWkZCHKBDChNPvW05mhb90d8bfeLhCvaKyxWn6WOl5x2F0K2GPeeO/bd/ssN9lic+8BOGJtMgttSCGkNc2TeGzFrAY/SlcgCqPQTNv9tteYucmDrfw8A6wqxSQZsklkp/ZM2JFh9kGpA9E85153YWSzXO1rcP7cUWYz6XUtbSgmh+h20pkOpWp+HRjErgCoYpQHE1I+1XCNGtaDI1PfIvSLXLC7OyjEgpFdyBdu/1of2GiOqy7lPJKkfs4W8sKNqlGt1gonYosojRvl+tsLLmYJ8oR6PD0NondCKFn8EmzrL1VfQOhNoGT48LeQTII1IKoWiqL+LyNEupXAAYtpeUBpMArnkW2sV8GR69+dIjZ7WqqD62Kb57MHOZlgPkJrjud3h6XZ6REeFC3QR51Zf7mpOs/QpNUPZJqtB4CVHcImxB1EeujjODKahQ7kAIsgWWfucO/G1MF0J4DAXRyeCzYcTcqYxxZ/YIbhsqDAV3pVK/0vC1HGcZjz/lx85mB2wa7VKvy9td68jgFgcxfEmoOu2HbJoCfgMakzn3H/2uaa6idskaMxCnaYrkUDbx6sfDL2/idqrBuT7NAzV2Uo0eLYbufVjflxXQvDkSM5ArGk3Uux8OHu30jbfJ2kQ4cwaAb/vQnYA/tCMOoQvAjLyQK89uC/Bw1p+IOwq2mobLxh48nM7D8JHm1Ey3skTnkGrq3JBDRSallBVYXm4Sx6xvDdhlqJxoJEw629YIToxVxGvu3bkIXTiLx0kav9t+USBmQLDpsAwVgyhSqI5jf/MS5nv+wTOc9bQ0yegmter3iWqjkaPr+VEqq0k81QGqV71uiJwTKlTvWYJ4jALIwHHprc8+1hKmyAfsc+FfT8wp4WZkjHrlJZ2D1WmkiopKdn9p9iSRXJrvq4rHqhRAH13LUuNzOb/vp/p54bUAlLKBfKY64aMd0ugruQKBPi1HFOuP5wIwY6Xc+agG1Mb0HHMWnEIgPKJ66fGNrzI7/ERIbApFM3Irghaw7b8RRXQ48wFDSdxbWNCzmKBYWLzk5R6wrwxDAKZsQ0HUBFUfNGXXmvy0sPPCcnU4TSB5LLnbCf03fWhIPpyv1YRmpds+KWCeMESbZEWlXCFTgOBWz60aUEz X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: e848ac29-0fbb-4770-1941-08db41d045a8 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:38.2819 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Z51/O4k4rp198sKPNFmDSLQKzpDahevSKCQZ+9i8Me4eZh/LFmjNAtzEvl45tt41 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org When the hwpt to device attachment is fairly static we could get away with the simple approach of keeping track of the groups via a device list. But with replace this is infeasible. Add an automatically managed struct that is 1:1 with the iommu_group per-ictx so we can store the necessary tracking information there. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 141 +++++++++++++++++++++--- drivers/iommu/iommufd/iommufd_private.h | 9 +- drivers/iommu/iommufd/main.c | 2 + 3 files changed, 135 insertions(+), 17 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index e66303c17c894c..3fd623208c691f 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -15,13 +15,121 @@ MODULE_PARM_DESC( "Allow IOMMUFD to bind to devices even if the platform cannot isolate " "the MSI interrupt window. Enabling this is a security weakness."); +static void iommufd_group_release(struct kref *kref) +{ + struct iommufd_group *igroup = + container_of(kref, struct iommufd_group, ref); + + xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), igroup, + NULL, GFP_KERNEL); + iommu_group_put(igroup->group); + kfree(igroup); +} + +static void iommufd_put_group(struct iommufd_group *group) +{ + kref_put(&group->ref, iommufd_group_release); +} + +static bool iommufd_group_try_get(struct iommufd_group *igroup, + struct iommu_group *group) +{ + if (!igroup) + return false; + /* + * group ID's cannot be re-used until the group is put back which does + * not happen if we could get an igroup pointer under the xa_lock. + */ + if (WARN_ON(igroup->group != group)) + return false; + return kref_get_unless_zero(&igroup->ref); +} + +/* + * iommufd needs to store some more data for each iommu_group, we keep a + * parallel xarray indexed by iommu_group id to hold this instead of putting it + * in the core structure. To keep things simple the iommufd_group memory is + * unique within the iommufd_ctx. This makes it easy to check there are no + * memory leaks. + */ +static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, + struct device *dev) +{ + struct iommufd_group *new_igroup; + struct iommufd_group *cur_igroup; + struct iommufd_group *igroup; + struct iommu_group *group; + unsigned int id; + + group = iommu_group_get(dev); + if (!group) + return ERR_PTR(-ENODEV); + + id = iommu_group_id(group); + + xa_lock(&ictx->groups); + igroup = xa_load(&ictx->groups, id); + if (iommufd_group_try_get(igroup, group)) { + xa_unlock(&ictx->groups); + iommu_group_put(group); + return igroup; + } + xa_unlock(&ictx->groups); + + new_igroup = kzalloc(sizeof(*new_igroup), GFP_KERNEL); + if (!new_igroup) { + iommu_group_put(group); + return ERR_PTR(-ENOMEM); + } + + kref_init(&new_igroup->ref); + /* group reference moves into new_igroup */ + new_igroup->group = group; + + /* + * The ictx is not additionally refcounted here becase all objects using + * an igroup must put it before their destroy completes. + */ + new_igroup->ictx = ictx; + + /* + * We dropped the lock so igroup is invalid. NULL is a safe and likely + * value to assume for the xa_cmpxchg algorithm. + */ + cur_igroup = NULL; + xa_lock(&ictx->groups); + while (true) { + igroup = __xa_cmpxchg(&ictx->groups, id, cur_igroup, new_igroup, + GFP_KERNEL); + if (xa_is_err(igroup)) { + xa_unlock(&ictx->groups); + iommufd_put_group(new_igroup); + return ERR_PTR(xa_err(igroup)); + } + + /* new_group was successfully installed */ + if (cur_igroup == igroup) { + xa_unlock(&ictx->groups); + return new_igroup; + } + + /* Check again if the current group is any good */ + if (iommufd_group_try_get(igroup, group)) { + xa_unlock(&ictx->groups); + iommufd_put_group(new_igroup); + return igroup; + } + cur_igroup = igroup; + } +} + void iommufd_device_destroy(struct iommufd_object *obj) { struct iommufd_device *idev = container_of(obj, struct iommufd_device, obj); iommu_device_release_dma_owner(idev->dev); - iommu_group_put(idev->group); + iommufd_put_group(idev->igroup); if (!iommufd_selftest_is_mock_dev(idev->dev)) iommufd_ctx_put(idev->ictx); } @@ -46,7 +154,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, struct device *dev, u32 *id) { struct iommufd_device *idev; - struct iommu_group *group; + struct iommufd_group *igroup; int rc; /* @@ -56,9 +164,9 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, if (!device_iommu_capable(dev, IOMMU_CAP_CACHE_COHERENCY)) return ERR_PTR(-EINVAL); - group = iommu_group_get(dev); - if (!group) - return ERR_PTR(-ENODEV); + igroup = iommufd_get_group(ictx, dev); + if (IS_ERR(igroup)) + return ERR_CAST(igroup); /* * For historical compat with VFIO the insecure interrupt path is @@ -67,7 +175,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, * interrupt outside this iommufd context. */ if (!iommufd_selftest_is_mock_dev(dev) && - !iommu_group_has_isolated_msi(group)) { + !iommu_group_has_isolated_msi(igroup->group)) { if (!allow_unsafe_interrupts) { rc = -EPERM; goto out_group_put; @@ -97,8 +205,8 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY); /* The calling driver is a user until iommufd_device_unbind() */ refcount_inc(&idev->obj.users); - /* group refcount moves into iommufd_device */ - idev->group = group; + /* igroup refcount moves into iommufd_device */ + idev->igroup = igroup; /* * If the caller fails after this success it must call @@ -113,7 +221,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, out_release_owner: iommu_device_release_dma_owner(dev); out_group_put: - iommu_group_put(group); + iommufd_put_group(igroup); return ERR_PTR(rc); } EXPORT_SYMBOL_NS_GPL(iommufd_device_bind, IOMMUFD); @@ -170,14 +278,14 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, } static bool iommufd_hw_pagetable_has_group(struct iommufd_hw_pagetable *hwpt, - struct iommu_group *group) + struct iommufd_group *igroup) { struct iommufd_device *cur_dev; lockdep_assert_held(&hwpt->devices_lock); list_for_each_entry(cur_dev, &hwpt->devices, devices_item) - if (cur_dev->group == group) + if (cur_dev->igroup->group == igroup->group) return true; return false; } @@ -211,7 +319,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, } rc = iopt_table_enforce_group_resv_regions(&hwpt->ioas->iopt, idev->dev, - idev->group, &sw_msi_start); + idev->igroup->group, + &sw_msi_start); if (rc) return rc; @@ -223,8 +332,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, * FIXME: Hack around missing a device-centric iommu api, only attach to * the group once for the first device that is in the group. */ - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) { - rc = iommu_attach_group(hwpt->domain, idev->group); + if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) { + rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; } @@ -237,8 +346,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev) { - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) - iommu_detach_group(hwpt->domain, idev->group); + if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) + iommu_detach_group(hwpt->domain, idev->igroup->group); iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); } diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index d523ef12890e1e..2544f10dae9aef 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -17,6 +17,7 @@ struct iommufd_device; struct iommufd_ctx { struct file *file; struct xarray objects; + struct xarray groups; u8 account_mode; /* Compatibility with VFIO no iommu */ @@ -262,6 +263,12 @@ void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +struct iommufd_group { + struct kref ref; + struct iommufd_ctx *ictx; + struct iommu_group *group; +}; + /* * A iommufd_device object represents the binding relationship between a * consuming driver and the iommufd. These objects are created/destroyed by @@ -270,12 +277,12 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); struct iommufd_device { struct iommufd_object obj; struct iommufd_ctx *ictx; + struct iommufd_group *igroup; struct iommufd_hw_pagetable *hwpt; /* Head at iommufd_hw_pagetable::devices */ struct list_head devices_item; /* always the physical device */ struct device *dev; - struct iommu_group *group; bool enforce_cache_coherency; }; diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 3fbe636c3d8a69..e5ed5dfa91a0b5 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -183,6 +183,7 @@ static int iommufd_fops_open(struct inode *inode, struct file *filp) } xa_init_flags(&ictx->objects, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT); + xa_init(&ictx->groups); ictx->file = filp; filp->private_data = ictx; return 0; @@ -218,6 +219,7 @@ static int iommufd_fops_release(struct inode *inode, struct file *filp) if (WARN_ON(!destroyed)) break; } + WARN_ON(!xa_empty(&ictx->groups)); kfree(ictx); return 0; } From patchwork Thu Apr 20 18:51:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219091 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 F0BD4C77B7C for ; Thu, 20 Apr 2023 18:51:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231572AbjDTSvp (ORCPT ); Thu, 20 Apr 2023 14:51:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231388AbjDTSvo (ORCPT ); Thu, 20 Apr 2023 14:51:44 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62F7A2722; Thu, 20 Apr 2023 11:51:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=H5idkkagxxcgPUphndLm3hKoYMQ8ybpBR5bcN4IzLxJfKc6BWetx1+YdZCqrCl36ZRoKi0GqPlmaAKJfQojR/2XfQoQuXK7CsOScO90FVupX3CZZT5Zk59uULiJUiMRg6xNgiyXUducPqShQsYqX0z2a9ah2VXkxwqnIJzuHpzhQmCe3m7rFVrVNJIv448z2hYBPehD3+3Prk4lcW3zGuLGEpNoGXl1TJ6LrBqhFYUeOVCRZh0zdZGDaiDPW7nXMO06XX3TbaHe/fzlQbfrNpmFXWBxntRx9qFqiHvWnKD+dAm6t2LZCMtq+Wr8TPw4NhGokcxcmd8BlxEdCtA/Opg== 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=iF9CYXMf/7HRZlzzbzfWpeCrXmJIPYP1jSbYNy/mgOI=; b=lKhQjeUepaDIyGQmFSNJv5nGDArtYt3zZO0YW1oABclf62dhBKRWL3Xg064CQOyjamkdDE2/UpnVpCbDbjfIkiC17tCcdhXQyY+BY+Q8z9IyW+BL1fMIC9KsuOV8K2T1z+JubzqV6EXQe0ksf7LYhS1DsZ3g3ZAReh1diosgk/FQT1jSKt+1zeb1FOQqZ/pOZt9ZrDh267TAgT0nRVeF9mEIECU/0v3wbt6bfmyl1y84EsQf5JAlxXoQXp8Yt+RrdcTrzTPxPNb1xXNDcppdduNPDPJ05TgEtq7XUOWWjlOW6V/XTUkBhWzLZl5kvE+9DqDif2O+v8qq2Pp2ZL6a+Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=iF9CYXMf/7HRZlzzbzfWpeCrXmJIPYP1jSbYNy/mgOI=; b=uWMFjvXEMB4iTGN3Xnm3wSU2G2vwSWnfu4/Sx6/sQ3CS1eJWBtw4YsHBRZRgbcq0u5WljeKNLbNG9VyAOvqHaxaWJBb7QZIHa9z7I4qr02NEFNmQbzAZOR2Z65un5SwGbM78tzjRWPymOGYrMs+Hf0AQHbdr44bGwaOAmWwAtGIw0/T7bmFBiKP+NNZBIw6P/GGyLe3GT6EzyavV9CmO4DVFKFVKdE/ZPSZwSAVlOzD980M2xlVgfzU3IZPOmCY1FGK8sMfWLdh4XvwFqITePxCMjIZGuvvrz2Dyxee+IM6Gzw3ef6d7tNBL9BuvAu3UjRxEujy+WXq/8kz/FHAV+g== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:37 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:37 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 03/19] iommufd: Replace the hwpt->devices list with iommufd_group Date: Thu, 20 Apr 2023 15:51:18 -0300 Message-Id: <3-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR03CA0029.namprd03.prod.outlook.com (2603:10b6:208:2d::42) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: 3c7e3458-de31-48ad-e381-08db41d044a1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Kf+G9U2THOf61zpQKZdNaE1n9+AR/LxPbG9PQjXxneOdUiEtwygWTYHSdWJfv+jBB58JJVFhaNFO5jLRFccJIUT9EDJgfxY0zCUHeyPg98UXylJrVbszNz5D2o+WvLsAJ5rYz8nkPPHhNA1rBBHWYO5pK+/s/VGcCkrQ5BdK+ucBGHzDnQX0JZ/2bojtnaQgMzoHC3P8hSlFuQNABETJnhGwIFhsjiQ2lGemltEcCRASaFV658+pEk3sUheEnf2CrXGVEdJnKEvmwEN4rL3RSMOsVgQ3E1v3MqL/FqIaxGCV5M0QBP1QHgP4aB2Ez1Dq8+wyvIRQ0zjcq+mo6+eqj2HkW+/D26ch7bsobRW9LT2ZSREGuUzFetHdjPUxLd0Lk5O4aFnffSeXxdZnu7UXEfV1c/eksXm2T3JZe3SCI6R7zsASh7lpxUYBFH3pO8saG7Z6W/WQrTBJRyNAHrS1a0RonbT5ZIUysFRwxgPIyScIy0faZqPN2H91DC2wTsGJfx4u3ZJddCVajIHTQUUuktXLUoGmVFzh84fDvPi20Isj24Lm+eCprf9mmGdA+Y2n23RPpxnwiOnLKYaB31mY51qclFX/HcrYgL40AzCqbss= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(30864003)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: qq8w9bFSWvVPCb4Cv7FDgVfLgkwOKAgd+1mSjlzOA7PNo5XSzsCbxideoQajp/LMQy74mmAx/mVSqnRxa8oqPkg+gsfZdcEQZe4fFi6aZD78NKv8rgyzAGworzvM86561HEAZe/xasuvEESqzGGzWODFS6nvDZgdK2f8A3QSK7BbKpDk2Pm2Y1ulySDVmpvGEBXdVTYqIhP5EfcY7DDb7a2VzScmY6vsZkx2YtYxaalhETuLE9WmKN90r332Ce4EyyzhKzl9lgj8TOA9tWJXgkDy6Lg/dTwUTAjQ/ssvo5csbRXHnWm9yrjXkuR0T3G55gs8uFkG9YeN2wY0GQsNe/qmfEnJMTk/ATZFvDEE3USwPDscpuqbfI5o5q8DQQ30/7oBg22Iw+zpcH+jaPc6X6k8UDI5ODN5f9sSkNc0F53NVwEroS/oMdEY0+8zBwETpNX2Wy72NwCth1Yt39ofDGCqzBWnr+SqMJaaIzD/xWQkTPpI900jBAuEr+qbbQkWmarI8mmCivAEDw0a3/wG3d9vbypL17grWV3Fc2bcriiMo8rEhxlzD5xWM/6OlIUPhfuvy2keUAr+c4Z69TayPIhXOo8isjWRUapSaBYFwTPtKtnddTaXiR2RG+em+en9VsQixYnPOyDTPcExs7vWYRwCFDxkxrVamblxEA2o5aPOGCQDg6nHGyzKO6p214YFV7ktgC89rXVknX28UUllBURWuNIAvw7cho3GCy7eZ5UjIt9yZrIQnudrhwRxsDQsVllYCDwdl/I7+A/J9keHp6hTWsyVCwBircqSer1RZO0bZd2hn8ewbEFsR0ZIkyoVu+pmPsuGucB8CEA00s5SZFj3gTOgLKL2fq0Z+R0XDYIvU11yWlxU0MBWk4l2Os1cGMnDo352x41V+Gjl3PDzp0S4HngU1oaQs5GHNtgbqQi4V/QBMKsK8o0y+eoFTHDf6xpjg6L+nw9yBejKymjkyoTsnk6IpAKGKjEvT7PRlrAFLTzF3PEMMDNjp/l2lcKd1mXiu7OnYWpktU0n1xb6DfZ/MTJjOCavt6YSUQFZyX5LCUlwwwWFfXpaQm23987OZE+Kmwud21KPeXFj38bwVPauDO2IOfFEdpW84xbDJ7D6oFbDBBtbZYrUAYYr7lYJe+BYFiwSlQSBanX6J7C1rnBMHiVLYT3txrmabeKjCly5t0RSsXHN6GpyiBkRwOyM0VrPPbYJRyrqm2FDuLj3ctqyB7Os98QXaQLCq8N++9ufe/c/7WY7CmnrAFnv/DcQlM9tnN+GOvwUUMK4mYZqrUpySALyeDGQA020MBT0p6UfKWd1ZyyxIF3YkFYJzjqeILoC3GQdDcIQ7frww4lheaZbFfvq5ETLlHUCjWV6b5fSvF1KtYCHVcjo8+N7miPjAZs3Q8zMcpH4qz8xuFV3nccjD1s+2nXdg00dkc3tscS6Xzfp42wVRKP3RRXseIz2oVk0RuSG+SYxSyj9446OtJvyYqTBJGS+o7CuobUmSqhxjedCvkwW0lyu0k7h5vSy/TEIohfwi1Ub/570Jr4S3ovw+pVsQDYhOjbV/s6+DvI1XyTREcxBQQojiFGh072s X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3c7e3458-de31-48ad-e381-08db41d044a1 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:36.6776 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: O87KQxPzm/PAIrmBKS7gUCeQr+flLyZDslkp4bOiWReO4s4MdoVgzH3d7sKZ2yql X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The devices list was used as a simple way to avoid having per-group information. Now that this seems to be unavoidable, just commit to per-group information fully and remove the devices list from the HWPT. The iommufd_group stores the currently assigned HWPT for the entire group and we can manage the per-device attach/detach with a list in the iommufd_group. For destruction the flow is organized to make the following patches easier, the actual call to iommufd_object_destroy_user() is done at the top of the call chain without holding any locks. The HWPT to be destroyed is returned out from the locked region to make this possible. Later patches create locking that requires this. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 100 +++++++++++------------- drivers/iommu/iommufd/hw_pagetable.c | 22 +----- drivers/iommu/iommufd/iommufd_private.h | 13 ++- 3 files changed, 54 insertions(+), 81 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 3fd623208c691f..33f9ab2dc5f4ae 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -20,9 +20,12 @@ static void iommufd_group_release(struct kref *kref) struct iommufd_group *igroup = container_of(kref, struct iommufd_group, ref); + WARN_ON(igroup->hwpt || !list_empty(&igroup->device_list)); + xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), igroup, NULL, GFP_KERNEL); iommu_group_put(igroup->group); + mutex_destroy(&igroup->lock); kfree(igroup); } @@ -83,6 +86,8 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, } kref_init(&new_igroup->ref); + mutex_init(&new_igroup->lock); + INIT_LIST_HEAD(&new_igroup->device_list); /* group reference moves into new_igroup */ new_igroup->group = group; @@ -277,29 +282,18 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, return 0; } -static bool iommufd_hw_pagetable_has_group(struct iommufd_hw_pagetable *hwpt, - struct iommufd_group *igroup) -{ - struct iommufd_device *cur_dev; - - lockdep_assert_held(&hwpt->devices_lock); - - list_for_each_entry(cur_dev, &hwpt->devices, devices_item) - if (cur_dev->igroup->group == igroup->group) - return true; - return false; -} - int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev) { phys_addr_t sw_msi_start = PHYS_ADDR_MAX; int rc; - lockdep_assert_held(&hwpt->devices_lock); + mutex_lock(&idev->igroup->lock); - if (WARN_ON(idev->hwpt)) - return -EINVAL; + if (idev->igroup->hwpt != NULL && idev->igroup->hwpt != hwpt) { + rc = -EINVAL; + goto err_unlock; + } /* * Try to upgrade the domain we have, it is an iommu driver bug to @@ -313,8 +307,9 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, hwpt->domain->ops->enforce_cache_coherency( hwpt->domain); if (!hwpt->enforce_cache_coherency) { - WARN_ON(list_empty(&hwpt->devices)); - return -EINVAL; + WARN_ON(list_empty(&idev->igroup->device_list)); + rc = -EINVAL; + goto err_unlock; } } @@ -322,51 +317,52 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, idev->igroup->group, &sw_msi_start); if (rc) - return rc; + goto err_unlock; rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); if (rc) goto err_unresv; /* - * FIXME: Hack around missing a device-centric iommu api, only attach to - * the group once for the first device that is in the group. + * Only attach to the group once for the first device that is in the + * group. All the other devices will follow this attachment. The user + * should attach every device individually to the hwpt as the per-device + * reserved regions are only updated during individual device + * attachment. */ - if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) { + if (list_empty(&idev->igroup->device_list)) { rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; + idev->igroup->hwpt = hwpt; } + refcount_inc(&hwpt->obj.users); + list_add_tail(&idev->group_item, &idev->igroup->device_list); + mutex_unlock(&idev->igroup->lock); return 0; err_unresv: iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); +err_unlock: + mutex_unlock(&idev->igroup->lock); return rc; } -void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, - struct iommufd_device *idev) +struct iommufd_hw_pagetable * +iommufd_hw_pagetable_detach(struct iommufd_device *idev) { - if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) + struct iommufd_hw_pagetable *hwpt = idev->igroup->hwpt; + + mutex_lock(&idev->igroup->lock); + list_del(&idev->group_item); + if (list_empty(&idev->igroup->device_list)) { iommu_detach_group(hwpt->domain, idev->igroup->group); + idev->igroup->hwpt = NULL; + } iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); -} - -static int iommufd_device_do_attach(struct iommufd_device *idev, - struct iommufd_hw_pagetable *hwpt) -{ - int rc; - - mutex_lock(&hwpt->devices_lock); - rc = iommufd_hw_pagetable_attach(hwpt, idev); - if (rc) - goto out_unlock; + mutex_unlock(&idev->igroup->lock); - idev->hwpt = hwpt; - refcount_inc(&hwpt->obj.users); - list_add(&idev->devices_item, &hwpt->devices); -out_unlock: - mutex_unlock(&hwpt->devices_lock); - return rc; + /* Caller must destroy hwpt */ + return hwpt; } /* @@ -375,7 +371,7 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, * Automatic domain selection will never pick a manually created domain. */ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, - struct iommufd_ioas *ioas) + struct iommufd_ioas *ioas, u32 *pt_id) { struct iommufd_hw_pagetable *hwpt; int rc; @@ -392,7 +388,7 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, if (!iommufd_lock_obj(&hwpt->obj)) continue; - rc = iommufd_device_do_attach(idev, hwpt); + rc = iommufd_hw_pagetable_attach(hwpt, idev); iommufd_put_object(&hwpt->obj); /* @@ -402,6 +398,7 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, */ if (rc == -EINVAL) continue; + *pt_id = hwpt->obj.id; goto out_unlock; } @@ -411,6 +408,7 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, goto out_unlock; } hwpt->auto_domain = true; + *pt_id = hwpt->obj.id; mutex_unlock(&ioas->mutex); iommufd_object_finalize(idev->ictx, &hwpt->obj); @@ -446,7 +444,7 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) struct iommufd_hw_pagetable *hwpt = container_of(pt_obj, struct iommufd_hw_pagetable, obj); - rc = iommufd_device_do_attach(idev, hwpt); + rc = iommufd_hw_pagetable_attach(hwpt, idev); if (rc) goto out_put_pt_obj; break; @@ -455,7 +453,7 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) struct iommufd_ioas *ioas = container_of(pt_obj, struct iommufd_ioas, obj); - rc = iommufd_device_auto_get_domain(idev, ioas); + rc = iommufd_device_auto_get_domain(idev, ioas, pt_id); if (rc) goto out_put_pt_obj; break; @@ -466,7 +464,6 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) } refcount_inc(&idev->obj.users); - *pt_id = idev->hwpt->obj.id; rc = 0; out_put_pt_obj: @@ -484,14 +481,9 @@ EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, IOMMUFD); */ void iommufd_device_detach(struct iommufd_device *idev) { - struct iommufd_hw_pagetable *hwpt = idev->hwpt; - - mutex_lock(&hwpt->devices_lock); - list_del(&idev->devices_item); - idev->hwpt = NULL; - iommufd_hw_pagetable_detach(hwpt, idev); - mutex_unlock(&hwpt->devices_lock); + struct iommufd_hw_pagetable *hwpt; + hwpt = iommufd_hw_pagetable_detach(idev); if (hwpt->auto_domain) iommufd_object_destroy_user(idev->ictx, &hwpt->obj); else diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 6cdb6749d359f3..bdb76cdb1dc347 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -11,8 +11,6 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) struct iommufd_hw_pagetable *hwpt = container_of(obj, struct iommufd_hw_pagetable, obj); - WARN_ON(!list_empty(&hwpt->devices)); - if (!list_empty(&hwpt->hwpt_item)) { mutex_lock(&hwpt->ioas->mutex); list_del(&hwpt->hwpt_item); @@ -25,7 +23,6 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) iommu_domain_free(hwpt->domain); refcount_dec(&hwpt->ioas->obj.users); - mutex_destroy(&hwpt->devices_lock); } /** @@ -52,9 +49,7 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, if (IS_ERR(hwpt)) return hwpt; - INIT_LIST_HEAD(&hwpt->devices); INIT_LIST_HEAD(&hwpt->hwpt_item); - mutex_init(&hwpt->devices_lock); /* Pairs with iommufd_hw_pagetable_destroy() */ refcount_inc(&ioas->obj.users); hwpt->ioas = ioas; @@ -65,8 +60,6 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, goto out_abort; } - mutex_lock(&hwpt->devices_lock); - /* * immediate_attach exists only to accommodate iommu drivers that cannot * directly allocate a domain. These drivers do not finish creating the @@ -76,29 +69,18 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, if (immediate_attach) { rc = iommufd_hw_pagetable_attach(hwpt, idev); if (rc) - goto out_unlock; + goto out_abort; } rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain); if (rc) goto out_detach; list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); - - if (immediate_attach) { - /* See iommufd_device_do_attach() */ - refcount_inc(&hwpt->obj.users); - idev->hwpt = hwpt; - list_add(&idev->devices_item, &hwpt->devices); - } - - mutex_unlock(&hwpt->devices_lock); return hwpt; out_detach: if (immediate_attach) - iommufd_hw_pagetable_detach(hwpt, idev); -out_unlock: - mutex_unlock(&hwpt->devices_lock); + iommufd_hw_pagetable_detach(idev); out_abort: iommufd_object_abort_and_destroy(ictx, &hwpt->obj); return ERR_PTR(rc); diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 2544f10dae9aef..2ff192777f27d3 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -250,8 +250,6 @@ struct iommufd_hw_pagetable { bool msi_cookie : 1; /* Head at iommufd_ioas::hwpt_list */ struct list_head hwpt_item; - struct mutex devices_lock; - struct list_head devices; }; struct iommufd_hw_pagetable * @@ -259,14 +257,17 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, struct iommufd_device *idev, bool immediate_attach); int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); -void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, - struct iommufd_device *idev); +struct iommufd_hw_pagetable * +iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); struct iommufd_group { struct kref ref; + struct mutex lock; struct iommufd_ctx *ictx; struct iommu_group *group; + struct iommufd_hw_pagetable *hwpt; + struct list_head device_list; }; /* @@ -278,9 +279,7 @@ struct iommufd_device { struct iommufd_object obj; struct iommufd_ctx *ictx; struct iommufd_group *igroup; - struct iommufd_hw_pagetable *hwpt; - /* Head at iommufd_hw_pagetable::devices */ - struct list_head devices_item; + struct list_head group_item; /* always the physical device */ struct device *dev; bool enforce_cache_coherency; From patchwork Thu Apr 20 18:51:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219096 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 9285FC77B73 for ; Thu, 20 Apr 2023 18:51:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231863AbjDTSvv (ORCPT ); Thu, 20 Apr 2023 14:51:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231839AbjDTSvu (ORCPT ); Thu, 20 Apr 2023 14:51:50 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2069.outbound.protection.outlook.com [40.107.244.69]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F27B02D58; Thu, 20 Apr 2023 11:51:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iuzOW0F2V1RhtEdgnTqg9aMaPfXhm3GVlwaSdsKM36UwD8LXH1C/dRupYy7uu8b8sdgbH9Vq4PN7zQ/Y5lP5ppjiVJfUX3TjX7LEp6KkBFxRu4lcHsZ3n+mUG3yw3y84kGv+Y6HN0IHYdxmInsAivS7nkLs4I1kuRdPGKkWuhU9Af9zVK54CaxOAhXVp68s14WJb4InMUJCdGhZN73iT73L9SejrdpxzI5H6K+TmymG1p0UkZ2rseNli+6MDMz9BiK2xPqOF88RK3j3LRy4lxQ2qVN2f+D7LozpZ+cN53WYsV5iNMnytouyhAHvMMSwvJcKwMA4za1aGySioEfyeog== 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=MhnQUk2S8XQINgCIGa8jmbCAbr0NSnVgaNKgYsPyH9U=; b=nLPwqAteYt3CJnbiWUX6yFV7PYi1x1TBJs3qWfUVigx3fSqlApkkug0RQkPsryxGARbJqii9EXyohKhq/0KiU4qx0EOhNKC8btDVW/R26yN59+vXigvajXWas48jPZ7/U99t06B88mCGAh1VUK8m6EA7jOKAhYAFnd37SpVZaE/6kgOgt659Tvpsc/5w5n1iDr1ildzleLcUGpY3r344SAOFbVYaNSXoat99IteB8h9Yf//Ji4RI/2rWvFmiJB7rKXadmHLp2N2c0s955lBPbMZ/XH4KJdSqXS7ZKpCyxxf3JXwE9IbazYk59pCn4BfStqpDjQYGTYMyva2zTpqb3g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=MhnQUk2S8XQINgCIGa8jmbCAbr0NSnVgaNKgYsPyH9U=; b=Oy8Er7GmHmob1OhYMQj5wSu3pKr6dhAGLqAUxtJ3fepljNzsYE+gFDnecCM85APbkDpctq4km3ToluWb2aUDYzVl6+Gt+BlH+D9ru25th1UCpM0Z018VSqj+unrbTI1Hiv/kKHghq6S0m2Ma7vvqJf5rAFI0fLiT6kKVIE7TphzsqzXUP2f2+W/DT5G6JnaMFEPZRU5mxlnXVvjq48Gc4+vrG/loP0G+k5KVclLDcLmGcP3caRqpCyU9e6R+u2e50eIZM7EQoew5FNcFwCuvSVpQYc11G8kyYyFrqaGjo1hFPOtQk+7ooCyLzzWJbm8x7n4nZ29tGrz8C3VtR2ynkw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:42 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:42 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 04/19] iommu: Export iommu_get_resv_regions() Date: Thu, 20 Apr 2023 15:51:19 -0300 Message-Id: <4-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR03CA0029.namprd03.prod.outlook.com (2603:10b6:208:2d::42) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: 1eddc18d-0226-4276-9931-08db41d04541 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 50DOHuJKn7u0daSzRo5LAn7uPJV/pUUoUxRawZzjyQ9nW1DjgEg64f1l/Ok2+fdvS9iJ4SVK6kXJsU3Gb1vEMA81aK/fEVsYcEa+DuaDjejcCv+NmiSDGohhVNlUtep2fUm8xzjdLZLiGtGGYQJ0wM6JxvuE9D8Xf6V7mg9xw43oGPJfzHeD3tDDKFoxupluJ2xSooKWdkhPw8LejIP7gL8lykbk+rE0q2E2njbWySczm8Si+QKS8dLw733dbcjMqFXzqT7+IJpAoZpBbg/ABZsJU7XKPtrEFfoiKMSmzeCUvN95e10c0kiOxuRCOvnxCcmtIe9r+5a/NEv10NaPaF1cbI8oAMJjn8I7QYBDi/GMb0r4gtiNLJ4OmvqHv9dcqLUrQtL6jqzD9nH075e71bGld6YPyrv4N4kMlh6ujwMXufKmChVcPIUfzr26dtJtdH+pYY03F2WWRi/j+zv8/Rt0zw7qFi6m8/Xxn5L0OmYmw/s6L+3TDEcEG7VCi3JSq7Be14VQGclPl/yZhRch+FW8uTvdb5NWZAlTmBDvSV//Nz57NsMYSIRK6VKCOutLw3c3ACuV2o/F2vDzhjapRL/3muuooraLVOMta/HXF6Y= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: HGMVgb2cITV6bsvlavDZlpQJdEM0ryiTk7zlz+VgXJ17AcMK/M+dGESnkLE9hTa/idXVHfs/GFyvLHjSimGjjja8ZKdeyIQd8+NtCS1kyg8h4kkr5ZDlciiosUo3Xe1TMMVTaRCRR/T2hqxLK+I9fVtvnyZkw8p7EfVJMCPljREJe12PBbd3Y8evaczvQXI1Xn6R8lakQTDppEYVzaPzQu592yheYa33xnbeJWdY+OQAZR7mkG96M54qVU11oQEC9Lpdfqjy7FbQsMbJa8FvQ/Ctv0wqCRF5kgFmRYnWMWBwCiIVqxiVh90KQXvHoHkfzm+vqNfdN2igBo0XdymbWOpwP1TsIIXbOeV2aBRd4ZiwDiSfR2njJqSGrh6eyN+wqSR1Bpnjx2/V0Mzc/5BPCNp5xRzlk5hEJi5I8up9hF5EKs2pNSjDC0AUXFhHYYHg+eta4PnIhGYQbBAi4hvFKPxxsGaxvPYRHoAQz3srblAbdnWYpKCTLml+ZM1TPRQjJiIjz0YUKeBf9+3YeS2u/I3wTTXJHa8xM69QGYcpX3h/ZxrxZv7kKYFiFoRV882wqhI7731R5t6jCnyXWfiaEJEpAI2dF5w3KhXRqTBQq1VgdixfAPa21+mx6n7z8P7323nTZakLQWO/vFg990TxrOt1YGV4HP/ntY2ORPrIqC9ZkOQb5+pOCh8L1nakFjZaPkLCqlp7Mj5BOhO3vp9xDLEwUT3cbQAms4lAKjkKAw/WHyXNs7sI0TxT831PiROFtZQ7Xi/YPzWehFhZ7hlviB5XRPsbJJ+8LI5geciem4bZ1hkl/ePWXaP59HESKsnY+6JtouUY3xCMjlmZjAk8pWOeeiN/Ip5tFA8CUfmLyydB8d9+GuOyl+AlxcWC5J/joYMq1NlnhtsaL+zaiY5hxqGHHfkfIkeF47uOpUhIGE4u9bCWA+71SA9V10OPvrRqmfKZ/QhwusFMqoEuyce0KNSxdhHstJHQZ1/UbbcwTs0aN9yfojRw2YIX+6EMwlL39vZxN3mXR1a5S7I+kPL/uEH38s7bdWRNlztEJ+PIBeozizZriZ6Zjod/U6gt+YArMWAcw/buQXYTsiLrrCLq+a16wFzlcmo80701VGhxXFX4V1Ns8jk7XDgNigFk9NeAlYYhqQnoqlc54boZsyrQhnt3XaldgLPc+BCQuv2PBx5MxnU32cDVsETuuKBhCnW86LV7bq7yA97pal7vSUbeNlxN0w/KKOcX9DCPUfRGdXHLHVAhxb5L2aLJiNSbHppiIRnbLs/j23T11cGkm9odP8zL/pTovOhrZRtWY4J7Z/lccG8SoWptD8IrnUSjca5mBifueEcNK7XG+WX1e/IxFwRQj2ef3+btqWuQxciJeionsVcF2MV8ESrN3lk5sCSPPQtE9snCZuXtaqt36vHgHZY6ffGvT151oujVtECWFPDoUSysPFwSTp+/NC0+/aZSqhBBXHwGioFp4RVWT0Nlch5VciMnIAmjPcC0iLljV1gFi8v/FWXA3ibunfFEZAtKJDTx9GtSRisBCxeXGATFlgoGNgwwkcpSnSTi5EVRg2H1vciL/Ohp+2W4fpxHunli X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1eddc18d-0226-4276-9931-08db41d04541 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:37.5648 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: I7PhzMSYPLNDnadCadkYrIU+3+K8DblAFOBc1Cg9V+3GaPjtHI6g1Iqvc0dRvwBQ X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org iommufd wants to use this in the next patch. For some reason the iommu_put_resv_regions() was already exported. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommu.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 10db680acaed5a..76969904b93af4 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2639,6 +2639,14 @@ int iommu_set_pgtable_quirks(struct iommu_domain *domain, } EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks); +/** + * iommu_get_resv_regions - get reserved regions + * @dev: device for which to get reserved regions + * @list: reserved region list for device + * + * This returns a list of reserved IOVA regions specific to this device. + * A domain user should not map IOVA in these ranges. + */ void iommu_get_resv_regions(struct device *dev, struct list_head *list) { const struct iommu_ops *ops = dev_iommu_ops(dev); @@ -2646,9 +2654,10 @@ void iommu_get_resv_regions(struct device *dev, struct list_head *list) if (ops->get_resv_regions) ops->get_resv_regions(dev, list); } +EXPORT_SYMBOL_GPL(iommu_get_resv_regions); /** - * iommu_put_resv_regions - release resered regions + * iommu_put_resv_regions - release reserved regions * @dev: device for which to free reserved regions * @list: reserved region list for device * From patchwork Thu Apr 20 18:51:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219098 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 D1772C77B73 for ; Thu, 20 Apr 2023 18:52:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229780AbjDTSwW (ORCPT ); Thu, 20 Apr 2023 14:52:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231803AbjDTSvx (ORCPT ); Thu, 20 Apr 2023 14:51:53 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97B5D30E9; Thu, 20 Apr 2023 11:51:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kMTLhMsMndJ5UBeysqYYSlE0PsXWW5gTT+EyiehzcdIAffMx2WgHezQ3RDmNnAzFjw3yfRQDGjk5YxY818Bf/cmSFJ3u1agi/t24H/4exUl3CQwi/OQOZurEFILtAxX1dP5vgK6xfoLLfv6yuktAQ/PYJpApn3gWCkn0kNjUyaXSVgkb6pP1V8VIijqG8p6iqz1Phb9dZumPeF+BGwe9/XvmMJwu+yNhVwa2leQ81fF++DLqbPahfAvw46prw7wdPtjoJHJtB/fTqm1hbxT6BlKWiVh7aYzE55K5TRl9Q/hT9zlrdk3pT2/ZM0jFSZT9/KBtYWAz6Apx99Y+y8Ftug== 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=PotPaQfPH4+45IYRwWk4PvOt5jfLt720n3UICRNI6Uc=; b=NCGApNXxwqS2INjhOl7Ct7tLab/aTXCiKWZe8TWoLjq8Chxsyb2eEYU9pywjlJ7I+f3NFjgJYP+BuOrd5vzOGgVo1nIdtlRo1FKzdrvq9YAtbW9FzcgcRxmvTznuG5rhdbc7LfySShv/HGIoUjTrQJ1k+U2jfBTP60xioUTI81TcvdDp/EzZU4lWauuunY3rKaNyETD83W5SnYTV9PBsc8w4rTBdyEAjjgXOeA7HaoDQlaFgyySyKMXjcaTR5y2L+vxESRNgibnpgR1EnVu+JRmylVFszQnWv8eQu93gDY1cTIdy8vrxAFo/p2ccWMPkIKBNwESKILLzDZyaGd4WXw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=PotPaQfPH4+45IYRwWk4PvOt5jfLt720n3UICRNI6Uc=; b=JrZhBSlojtgSxfaHCaO54tvC+XTcNtaq91BjXziqflzw9GA5gMkNejeUwKaDRUMNFruxKkLXAlwzGfhJ2LFTQNv7ZfIGEobPoGlXXy0SQBLOfG8DR7OWYp6BUKRwXRRzmQRsch2Hnyd1tOBIohymbxQRLNLaJ8BlUYfGijWKlNokRJRpplUDD9q9tJu8PiOperlbnu7rA+End9kRIz6jtPMoiDaq04q6nzjQQ19wTV8wbreiq3ZNcN7s1bh9tFjMAw5pSoI5gRk09ot8GYsACBZpdLiKyTPZ3IKVY95doEyMSJiWHzz13ayVWTNfNilGaPt3YXdQreCXO46P7LqXUA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:44 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:44 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 05/19] iommufd: Keep track of each device's reserved regions instead of groups Date: Thu, 20 Apr 2023 15:51:20 -0300 Message-Id: <5-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR03CA0009.namprd03.prod.outlook.com (2603:10b6:208:2d::22) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: a0d2774a-1bad-4600-3128-08db41d0457d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ApZKffhTx0UoWfnCM4T/OQXMwLs1Lvc8UqREuXSvEnpD/ZOivlJruAc5fBpDi5totLUfQNr4eI6f2TYVSvewP0TFL4TsM6MNeIPyzBZq/J6B8Li10zR/JXTxouJnGlcRq06HprUxHrEdl22cKT79/rbLjbefFtgiqGJQsXXW6mmV6592vsfR7l7V7HriR3DYd8jgizXQ2DZ6yt1gUp6L9Fj2X+TdzE9dagAR17n3jj2ffIhIBhSYYBh61t6fkXqV6hB5wy4mgIhDd5MpNGXOXvC7LBlm+yDTTN3LJd75J8eARfhu3pj4AuXW1AqY7CmD804PQj1BfMxZyL0Bk56H4trnZpTQ676f/bxXsiDuzr5dNuOTy+wiWpO80QVte0GNd1IRsm65kihGQvycx3cEYPCCaCWHObbbeSRezG/SXyTFNecYiso3YCTkF9jdJZ/d89KpTf67/TmPiOiJHaa2vMiCYwOqXTbcSeOyLHnYWGr3zeL1Ue2mRBZrZDJCVndicjezXntvlrbI5iQeDev+rL9IjMoUrg+3ufllBLIyI/o5HmarLPragknhTSeKeJ3EiDoy+ZNyk2jnVtTSJ0jZTATz/W5Y1Dl+iRGdoLUEUDg= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001)(66899021)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Cr6PPZVV7aRwlk7LZW63TWLq0QycZFsmqQTxM63rfHdasaJh2TPCKpZnt09geG+qxy3tvZJURwguLl99H55evP2iL9+vD/GqXrfJmOg3zf4RdK0NNTDNDa3t9isR0RBAhlC0r00dgfs2vPmLkx9bRymqSWa32gvgovOxavrFBCXlTyS2o/cW+goOM+bg99jtqrSGAWh858t8VO+ppDKd6o4IFTN8Q+bMIcHZPDskdenck5cVpUPhZiex0WuMtP5C4xT3Ma1AiodEbGw875trYbpLvJv3h/WEPASKnj3rd05r+qlLDalvSYxynDXlew/fJhqWNRcL2f1rfuVI95vmzSWPTCElZtop9O8gMvCEBn8qZOwhPmLciWuS3fdn1B+mV5x7sEr4ILTl5QK8psm2wLtSqH20LiHmK2MRRFjx1dDOJfEMbVgw373qMA4DmJ6ttaXwzJp1cAwHcYOkrnrDK964foHpa4CnFQHMLF25TNDbcRl7OibGFdrLzGPd2QfXQF0SK4s/wq7pCf4z5eq4dtvSaM5UjXzq/4t6ar6KTHbQ4aL480GtSbJKBY48yxAEBmR/toOgCc8BIPE8HNmEpLvLb2s0zLAi2gHnFB/Qbc/ZDgx89ETxHQep1BhdkRA8UXc2SsBMimfhnV8EV4W0vbjwn9u3AKCJmhptBkE/BLIa9nGxlflPeOxRAXBnGi9gz5UnZ+IOOKPeK4YgM6/F5M3AJ1DkTVeE3ngdTwLPGvBt0dNZPZkXTZ/f6MGBp9uhDQyIa92KhlhnsPSBSfwVQn8OAX5yo+a4KiP6i8XJSsIKqToCEtxIy88ILHBKdTh4BW9Z+bb9Z+udoZEn3D7+pcWR1+j+qsubGM33kKlcXWwuWPmA450CZVLa9GKj8htlwPvY+TMfkY74cBHYzI4UtdbqFuKkSoVO+nEs1WT7NJ0h+9OgqXIhl6dZucJJ7UozsH6XBGiabTR5Xy22j6IBLEs9O2RNXogc4Mr57eeHGvY/UlvqJcmlVhR3bkzejRHSeqnKvGEu4rdnGPiXteSJW4T/uGcOKQaq1jsSPj2H3wCIOXvJZxN7yDuCPg6bNZCGU9enoCObNUxDxiOgVXL+iZchjsJz5DXo0kJU82+yO83vYmqz+oujhW2ViVOrs0C15scFheKXUXUJWwuxkdMFZML6k2/slTcoOiFeiA7ZYEGmc+IMto5eCj2n0OpHbUXiJSd0CkaHnPiwykoAkLJgBs5d3DtcCGxQG1AvDfcHWEPu6w2XbTZI1dWPC9X/AzgxZkiEQulS04LF/k/KzMNfWOpQzNwI36iCmi8f46+RVhi0t517arn8A8IHxlu/+I/V01BZV9XDBCI7nmcR8tJs9ltSUC8t2MXP7tfQpHiQwenCE2Fl27K6sAtg5PbXFjhrve41QiJCX5RR14yr1Z+aBc8eb1HzPJYQ3o1TRnd2oV5WiFpjlSetYglsYEvAKtUfMMQj8niAlkwZg9DFRed0ubWf+1sTRbgG0BKl4paUaG7+tvwROwUPidEtxeKCEgx5bpqn5lc/NooFKqFJuSP02PhvZ0vJV205yJSHEzIWUIp3AW4OdsZgqyy36bUF76oC X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a0d2774a-1bad-4600-3128-08db41d0457d X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:37.9184 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Bq6MfbWUEf2zaRk6wpDidk/nIiWv2rP7TpIaWGEFCX+CyqGtx4FMrT+4M4WFHPGV X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The driver facing API in the iommu core makes the reserved regions per-device. An algorithm in the core code consolidates the regions of all the devices in a group to return the group view. To allow for devices to be hotplugged into the group iommufd would re-load the entire group's reserved regions for each device, just in case they changed. Further iommufd already has to deal with duplicated/overlapping reserved regions as it must union all the groups together. Thus simplify all of this to just use the device reserved regions interface directly from the iommu driver. Suggested-by: Kevin Tian Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 5 ++--- drivers/iommu/iommufd/io_pagetable.c | 27 ++++++++++--------------- drivers/iommu/iommufd/iommufd_private.h | 7 +++---- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 33f9ab2dc5f4ae..6836089e62eea4 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -313,9 +313,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, } } - rc = iopt_table_enforce_group_resv_regions(&hwpt->ioas->iopt, idev->dev, - idev->igroup->group, - &sw_msi_start); + rc = iopt_table_enforce_dev_resv_regions( + &hwpt->ioas->iopt, idev->dev, &sw_msi_start); if (rc) goto err_unlock; diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c index e0ae72b9e67f86..f842768b2e250b 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -1161,25 +1161,22 @@ void iopt_remove_access(struct io_pagetable *iopt, up_write(&iopt->domains_rwsem); } -/* Narrow the valid_iova_itree to include reserved ranges from a group. */ -int iopt_table_enforce_group_resv_regions(struct io_pagetable *iopt, - struct device *device, - struct iommu_group *group, - phys_addr_t *sw_msi_start) +/* Narrow the valid_iova_itree to include reserved ranges from a device. */ +int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt, + struct device *dev, + phys_addr_t *sw_msi_start) { struct iommu_resv_region *resv; - struct iommu_resv_region *tmp; - LIST_HEAD(group_resv_regions); + LIST_HEAD(resv_regions); unsigned int num_hw_msi = 0; unsigned int num_sw_msi = 0; int rc; down_write(&iopt->iova_rwsem); - rc = iommu_get_group_resv_regions(group, &group_resv_regions); - if (rc) - goto out_unlock; + /* FIXME: drivers allocate memory but there is no failure propogated */ + iommu_get_resv_regions(dev, &resv_regions); - list_for_each_entry(resv, &group_resv_regions, list) { + list_for_each_entry(resv, &resv_regions, list) { if (resv->type == IOMMU_RESV_DIRECT_RELAXABLE) continue; @@ -1191,7 +1188,7 @@ int iopt_table_enforce_group_resv_regions(struct io_pagetable *iopt, } rc = iopt_reserve_iova(iopt, resv->start, - resv->length - 1 + resv->start, device); + resv->length - 1 + resv->start, dev); if (rc) goto out_reserved; } @@ -1206,11 +1203,9 @@ int iopt_table_enforce_group_resv_regions(struct io_pagetable *iopt, goto out_free_resv; out_reserved: - __iopt_remove_reserved_iova(iopt, device); + __iopt_remove_reserved_iova(iopt, dev); out_free_resv: - list_for_each_entry_safe(resv, tmp, &group_resv_regions, list) - kfree(resv); -out_unlock: + iommu_put_resv_regions(dev, &resv_regions); up_write(&iopt->iova_rwsem); return rc; } diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 2ff192777f27d3..22863759c3bfb0 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -76,10 +76,9 @@ int iopt_table_add_domain(struct io_pagetable *iopt, struct iommu_domain *domain); void iopt_table_remove_domain(struct io_pagetable *iopt, struct iommu_domain *domain); -int iopt_table_enforce_group_resv_regions(struct io_pagetable *iopt, - struct device *device, - struct iommu_group *group, - phys_addr_t *sw_msi_start); +int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt, + struct device *dev, + phys_addr_t *sw_msi_start); int iopt_set_allow_iova(struct io_pagetable *iopt, struct rb_root_cached *allowed_iova); int iopt_reserve_iova(struct io_pagetable *iopt, unsigned long start, From patchwork Thu Apr 20 18:51:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219094 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 EE4DDC77B7E for ; Thu, 20 Apr 2023 18:51:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231852AbjDTSvv (ORCPT ); Thu, 20 Apr 2023 14:51:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229780AbjDTSvs (ORCPT ); Thu, 20 Apr 2023 14:51:48 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2069.outbound.protection.outlook.com [40.107.244.69]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC75346B0; Thu, 20 Apr 2023 11:51:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WqTP4r/1y6IhfH1KF1H5b7GGtjHREVZXhQMxLOPStHZtiAI9TEDqAX8PFN+Ad2FtPI0vnS1rkaSFZyE8UOk0Eipfy6ITz2Z3c+Xs5u7oQWRd81YqqpHg4N3SYphyCL4rdBuxuv3Azopx9GQ5skYUT6olmL62ESHmuUJElxOmKKeGp1IqSsASgw09nJHp9jxAklsq/cfmjp7RohDHoWm0p5FGM9Z1sQoppjkILK4LR6vGFCBe7EaQAcVuOkHlexU2W624L4aMPyNv+FetnJw8O7qsaOGyhct6FsZavKqAM6xFcz37geAx3acRdpZR5k57kEDu3E3sN66ETsPREkaKSw== 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=4nZEjWqCAUjxSEZpEfp2tV/0q2Ftz/hRVhWNkh9mIr0=; b=dtL6WR8yx+9GhDgx9BF27PwBmVkDUy6RH6B8c+DCaAgu/Tg22d9mQEZClgR8U5U/7QKSuzUhPfMK4vODJr6pc/SU01/RSOLwVtoEg7O7jbNCv12phFkUeCHfp50qpchF2VNrM5Kj4sAIIGEZaNmqd6ZXtC2YvgSmq8qV79uZQ5W1T9nOeH0aoTzIklauEJJguQLQlEV8WdiI/X7pFUXjawMHQBUNpSgWZOpKnQGifulRWYiWCoKvLl8AmQNDWW3DzrS0spV2EusvuQ9ExxZTxODpvyf9NumZvIP0DSjHO7UMTYdiRBOxZEXf/1wh5JmjSyX45+MRgDGFS+J5Z/6iCg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=4nZEjWqCAUjxSEZpEfp2tV/0q2Ftz/hRVhWNkh9mIr0=; b=EtnJrkyO1c0hZTz+SiSiwT0YEY7ENU9hSa8RIUI5OUnccgDEKvT3A5Qdlqgr2l6IqlYmZyXHp71WlTdErB/qdAYrfJPAImmQ1uOPggrOzJTmzn4/yumfXWbzhO02MZ3D0ysFEYZ2pRta8bgsvtb1stfdan38MOHr7/RoIkNVnhGpO3Mxs5jcV5XfUpg0y/VnybbDmRdGA0cc7tJU+9ZvrRLscPmBaqJy7pky8tFpUHA/3EdIjEMdCS5lWl3Iefl9JwQE8DyDA9Rzib25A36EoRACfQoyWZcgewkopxS254XQ6msv1pice4syt0EmFqcF63uqVAQAq5/fIZ87PN2bFg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:42 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:42 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 06/19] iommufd: Use the iommufd_group to avoid duplicate MSI setup Date: Thu, 20 Apr 2023 15:51:21 -0300 Message-Id: <6-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR14CA0003.namprd14.prod.outlook.com (2603:10b6:208:23e::8) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: d03c29ce-0e77-4ad6-da8d-08db41d044fe X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: d6uyil+3EFbmp4Hy0NY/QeIt/wn7mBuL9b+65eOeoXW44UUrGnXfeu2T0B5wnxhRKjhkJGXeSY1IHddemFdFoSdiWHACTbQDTdOro7AEFqy8qvDySO+SAe/l0hLss6dJ65kO7lyN7L3vfNA9AgpDMTXOJkcjhn8sXUVpn8alUVrcQ0T4ilKucFaEqFY8LM+DolC/I/xvtVX1d7F1iep/iShZIlP8H7uqeZwixwo66yrHzUNSm2IftdtdX7SIn2nXbogc5C8dKm+EAznrSA6uouseQh+EZ5FHxB67IxXC07Hw0ZvknzP7YJL7XKhedarLteJ5pPmJ8WzSp/KUgUoVYLkAMIuckhWHWKYRxniDISYsBz/UaXC1Nw4he5C09czCbdtGHf0ZhHoF1n2WpRYd8t54WetZswmUW3kW+k3HD/Frk3wsILlF97rysZEdWDj+b0m9Jrnp63jyYAmiTM/tupEt5JMUsqfCzumullZUVhy6QDOOfY4I3gvX3aYqSQKUDyA2YaPipPP/OQwQFbUDq8X6z+/yCtkScHSUXVcrWa6a1wy9L8Id7IaZkO9C+bXtQjRPVysdfpXVaQl/wK4zLLRlh9RDrCFjZVD+vpe/rN4= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: iYgC9yJq9N/dxrsxg5EzepuDYdnxHA1wyznjvB8bPGt171b4TMVtdIyDyEwByuJfjYmPqnREQTJLcF/NbT2tJB/OpljSG6F350nEYm4+IiL9t7sbbApW9dTAtUKydE4k0j/g8DAscP6NSNUf9e1fP4TiOk5w5FGoGbBx2AZMF50+welOssWVHGFsZVbljD8OxGHPabIZZH6bgHxTCgOLVY+ZnP6Z99IYvDXJA/HQD3cSpBCAcuWIcOc8b1PtqEh/9NNgJ+p/N+NfvQFvECAmPhXotwtTIvijXMxhwWnvXwkTYNGu1F6SB61Yr4K8GjCiZS40UmBFeIGunitudpxcPD+t/WMNhYzejjSazVH4jPyXnlrh+kKs2CNPjmLYqTwKHlFWQSoV25JYMIWwrTVChp8FfrvYa8UTHZ+kaCoUzIVSFf2oRRtc6Y15QAvt2qygxmw0/m0j9m8LzTEokUp9qMZbVjYUEuP5x2km3ZRONrHaoIleRveLvPDwjJQMaSaOSzIP3oeL5+3ND/GUUoET4dI1kTdSYwtfS7NofTvPZfkn7nKSxnordYbbJ7lkCY5gTbQWvVjKXw1GRTzXqnm0bQHdKgzuEHEsYt4UFQk8Abix7zBHjjU8In9XX/XDqEnejupuXscpZzFGZPKKTB2AlF8QotU7Bho2A7bAJEtEFu0PocvFKMC0l06Idd5FIMejmY2tslJzK6xRkH24pe03U5yZVEB/JLJTmV0A8QRE/dG22u5XUn9y6f7ep25n96IqT6EZFZZLx91xyrVFYCDId6PZvV+vkjuKUQ5e05xM55hMS4ie8cq31NDWnnJWJ2PKzUogOAthC6JYlC9PJ2X5k4j8KCIwEw3ssieRNQkmYeVbYAEk7Q2UIEUcr+zMUbL2JWo1Z+rKLiuGCVudPGtqudGI2MIaBJqnd52j4fozqVhKatqj6NQUvpVXmHACNWVivL7NAxZHdoSyVdB0w23OLWGvj9WNtBlGe5fsWUOWi7+PAOcsGK6GXrG1bTq+oAO2QfUflKWxPAw56Vce3Htvf4x8DAzQfUabN4R0hc++rPnhbIwlCDlbF1H/VyOSFa9SAO9of4iOcqDPTP6CTj4TBfZj6VSTFw7xsDZgnmiAEJMFe8qDE+0fUnWXY/IATVldNsCf7uqjuhwk6R2yveq7qwH2SVEgJVBxJJpIUouPs2wG1rIAFHjw85rRLkLQu3acxFzo1G7ypSF7ZGSmJy/enmSXFc6xbpJ/i9WBi3N3Eafgkwo+/jXXQASX5YY96GhKY5t/FfH5kd+E7qb+3nSzOcMXXnDkAY9Z23CeLkGPEOsMum8tkLbFKwJPBTHVUWwwCwxJRVSWtpdZl51RYP8HuM3Xw5j3Qdr1jQGpH4O2W6wIcdyJ0sL264AeOGvKXTUgKU8HBWoqeSKRvSFYKzEI+2e+KnSUMyJf2emYWqPs/hbCGSV2rcV4SD0IOseuLPsHoC/pXUcqz/7QHzLMB1p3ZHqwP3UT52ZB7P7ss0gBv9izOz1w5jI3Z9tsfZyAXY92LDC6L6lBw8KDVDHdpZosfQ4sKcnaGp/kKwXnHUpZKPsGpg4MxNkZCYm1+JJnySPI X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d03c29ce-0e77-4ad6-da8d-08db41d044fe X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:37.1127 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ZZEjQICn8uuzfhBYgBQmym7BXs2qvP+u/u5ew4roamxA4rF258Q/LMDaMT/Bfxvu X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This only needs to be done once per group, not once per device. The once per device was a way to make the device list work. Since we are abandoning this we can optimize things a bit. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 6836089e62eea4..c0bea7c1760e67 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -318,10 +318,6 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, if (rc) goto err_unlock; - rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); - if (rc) - goto err_unresv; - /* * Only attach to the group once for the first device that is in the * group. All the other devices will follow this attachment. The user @@ -330,6 +326,10 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, * attachment. */ if (list_empty(&idev->igroup->device_list)) { + rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); + if (rc) + goto err_unresv; + rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; From patchwork Thu Apr 20 18:51:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219101 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 A2F2EC7EE20 for ; Thu, 20 Apr 2023 18:52:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231803AbjDTSw0 (ORCPT ); Thu, 20 Apr 2023 14:52:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231971AbjDTSv6 (ORCPT ); Thu, 20 Apr 2023 14:51:58 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 16CED4C3B; Thu, 20 Apr 2023 11:51:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=i8Re/Ee9eraFXJ+SGyb7TUevHWGyINvr2ylvKOgRs8jMnUzvPKqC+2bkuPUCiWwAhHGTMCctSxsM/t1KNbQRX0D3mpO73QcM+JgYLk/O034GW5wZhn6AQrSmUsxYfy53K/9DAjKMPOv0mGQ2M9+czaDBXKMXMeqyT6vQyFepp53qYRv+1WNd4LDdUXg6FKzmyMfZkpU/ZMNphfCVOZXj6GiYnlkKPJWu2auBvWCjZftnVYurmuys1ECEya64A/YARzuVOuF2VTPpO5BCrlmt8eNNrG0daw83uR/a2jn8OiDKNheHZcMkrQjsaTkz01+wsqEk8KkpcWG2i5xOBzm3mQ== 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=bRiHOtK8PoL6hmAlORhWrDp+upeVUzBH3TwjysZamx4=; b=McSCcqeT5xPajxc5S9s49rllgHGQKXwSYLdznCVutHnoZLzrGK3x1lmR1WdToZjIfBIA1X2kFfMLoswn7GH0zASYDD8tz3CgHn8YxfJo5FEfsFy9hIEAgwkIOt5Z6jslRpoUVUjhQj8GwUIDG4YGl4UdWSTmLF80jqxJNVW8llN+Ejs33rQYdxVKODnWJIxLAJwcEaeVyK0Dj4VhTTFq1H4edplugoKFgXhak85FJ34Di05PKl2eL27kl+vTF+NJK+eA86wg5k0nYi8eUNMmpeW6gsL87MeS+rRvpvK6UUI7HsFjc+5kxqnGy3uF/9AoLoI9S46S4F+RkIDcGFy6nQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=bRiHOtK8PoL6hmAlORhWrDp+upeVUzBH3TwjysZamx4=; b=GDDaHtFOgKJhUKlYCZeyXkiLDj2V6SKT5Jp6au2tMcHsYAXrKp3Yo1d8Nnr9qC2xAyejjbePK4rG44sy+KU846VEJb5GyxTqViaZGkrabT7fTC2JW1EVaC0RRSH2JB/Gz58n69HeTgm3JN5G8omBNO60SesK0aBWDF2mDHMovBLC7fabuKh1NHjX+dPu29llp7f7P/rSlfzO09eDuuj5fGqxsF5sQjO6x2OY2BFkYSQmT547mmoznEdWxkpY1DCczyRYcXxgTIcdbPkKCbqiXWiZDgC2Fd7RJvOo7cKhZ0Dhi/BPCEjoBVK618xEEO0fyF6spBOEW7hRPy1WoqgwtA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:47 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:47 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 07/19] iommufd: Make sw_msi_start a group global Date: Thu, 20 Apr 2023 15:51:22 -0300 Message-Id: <7-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR14CA0024.namprd14.prod.outlook.com (2603:10b6:208:23e::29) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: b58111b6-52d8-43f7-3d14-08db41d045ce X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: n8l9hBqeb11ycpYSSxYm/Y+42WVrw3N1/bhkr9TaFR4t1bm7Qf6XugSY/caBg1cKIX/PIQO2emUo926PI7FJed0jxymjV4NPHaykBYXMTOMcSVvlv7RB6W4aqhmsIxXFLiz+2eoDa44sXVIluF/RR9KM708aYgw+nEYESS76YHueilFVYV9KlR1tFsRrDXsugmG5jyb7fwomHblVzw+t2gvDKWk8L2ac+3LSwJ6nWh5UvDL9BVqJs++RjkPNkdS6XrrEriTWyFO0vMaGLVr14RIGU5AmiTvdwxod8djvkIrgOt/YepiPET9WXL8MemKtulAb+vYB/ziVyQBE4pceluvXZz4J3YstQAXfljo6UGSLf/it7dluM4d1yV/z6yathgQvw5OHnHgWckLi9jYkLhkpYdrNDuZpf5wSAPHkOCTuyJ92rwlo01yNeNGwhCgCJHtaM/PqhPAaRmiya1XGbb1rzhjthwF5dIgui/SID2N8G2bnopBs+7+QZDcHrjPDS+BEaxB5BNKhUbafpYfPwrgO/pbm7pkA2aQ3MZBHIlw7U2neLBPb1/Z1YESy3v8ZN06USiBSmNaNWKdA8ZJzJ40/AwG4NIv2rS+aiCymQHk= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: m3dOOztnYagI08GAp9FieiHUzAmxxdWzm5Uv0U/C890JXWKqnbBEJ/6w8Z/bhtycLdcAvNw9taTddqMpIVnCuEH0JSOjTFSiWH5/xIkKO7Wxa3l090g/BorLbhtHty4edrcwIqlCafRIHw7hZxOgZtVc6QWI0AcyVSqnJabi0dcjgYuz6hQfb649//LYYJ3XkTaAY8Ng1is65y4jhTDBIaNeCJAehtRLSnOm2x8jhSigVCK1jKOaf8y0yMS4zPX3UWb4UkFRsm/MLpz1h7kKJsxtYYXCaTMjjPsYSlV5YrXTJcxigjgnPPDGLQQ3MclITm0sRk/PQlyNm6c4p5AELlnaz4STAS5Qo8SqbZ6lPkxL+Ixhx/0hRnXCWc1rzfVqWToBLRk0HCMduU7Lddfm+QhwkTPK4oohKGDoWNr6mIDlHmOVOWqBiTsiOlK7uodQGshs1iib+h8GhZJSuTA/YcbupHQqY4evGZBQLjmmpwrOVC84WKdP8zCa3xoy9ISE9Mt3KXzqUbDE/z0uFtCufySp+XhfVLxs1oTyoRqH0vwWpjOywdTYu7uX7cmeXn9kQrNVVLIKQIMMXTiQ9vhe8wJCyy476PC+b3TYWGiDKQr9ylX8SxiWVeeVgxApPlFqnW45vpzrzbFWZUWETIhD3tDAdnlYV22iBKAb+1FKlB0DBtDlr2tIYPRklbPDJqugPedsLtzat7Qr53SCwiWdL1/8ABOfoOGKQ5ZE1ka8/uzYetQLOoCvd6R/0DHul+OqmcTJPG8XmBLXrBNUvgtiF3eFQQ40gxmr+OCYrlsfLNio0/Ym23DAC3xKlaFbRG3wN3RY5Za6QIMzNWST2F/a1fyXwkUm5ZQcpvfk8cJ/xxqPm3tz4gLQ36sPuU64S2OWD7gfP01szfSf7DO8CIjMX9sXnn5yQvWJ5htPEZQdTeMtgx6j2dYrGQPHIpuSJ+CaYZx0FEkpTipyQazlWFvIGJm45b3FYxgtJpwrnabRwKXYX6K/MCwahKUuzmWxhHfu50kV+9lG8mwW0piAwph5Rt9SoDEJMcmhAJqVcsM0pT2mBfEc3NTyfFVVNuMx2er5etUHF/nepdae1S57AVZjUyACWCgQLs+a6iuHAYoZISu+dUXCL/irv4iwv5gqCMcrJHM6sH7VbYleq26qP3UhtQJBfwwru8JQK0q9OSkXRTe42iaSpiYZf8ovXAjaOLjoLsDzSZZHReBkU/2FAnR928Y6b1K4Ce9PdWBNKv6H1wvlJcDoLPfsc9Z16sMxbOfu7N82rCBgQTawMTTpuzFNoWR4+ORHvbmee2R5xfHPxlDJHkiQfjvOpwc7aNBhwyfkigkGPBFWVulNjRDvb+/0iW8mFMrE1u9ECI6HQBvprqa4gHGmY9KZdiCBbc6Xy2jqSouudyKz9pMdFSsn76NM6CiLWispmZv2I9EqreWVrPaOpabXsFYs/60NGZOOfFYio4q9EaXHC7ZuZQh2Bi8TivmQYyqnl8hYnzoMfdh0kLqTvIKdfXkcTZgtz15qdBaK6CrPOoe+oOQwEH0wzhLgkF6e0bhTe1+T0sjEe3/PlC8R6TC0Tqwf+1ivncw91WVP X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: b58111b6-52d8-43f7-3d14-08db41d045ce X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:38.4350 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: IjvybQCEGZaMAhjIoIW/W2iSzJm4ImvCWrzMPI1k2Ify+PzOsX55X7dcroB7i2n8 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The sw_msi_start is only set by the ARM drivers and it is always constant. Due to the way vfio/iommufd allow domains to be re-used between devices we have a built in assumption that there is only one value for sw_msi_start and it is global to the system. To make replace simpler where we may not reparse the iommu_get_resv_regions() move the sw_msi_start to the iommufd_group so it is always available once any HWPT has been attached. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 14 +++++++------- drivers/iommu/iommufd/iommufd_private.h | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index c0bea7c1760e67..59fca93bbd0c47 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -88,6 +88,7 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, kref_init(&new_igroup->ref); mutex_init(&new_igroup->lock); INIT_LIST_HEAD(&new_igroup->device_list); + new_igroup->sw_msi_start = PHYS_ADDR_MAX; /* group reference moves into new_igroup */ new_igroup->group = group; @@ -249,10 +250,10 @@ void iommufd_device_unbind(struct iommufd_device *idev) } EXPORT_SYMBOL_NS_GPL(iommufd_device_unbind, IOMMUFD); -static int iommufd_device_setup_msi(struct iommufd_device *idev, - struct iommufd_hw_pagetable *hwpt, - phys_addr_t sw_msi_start) +static int iommufd_group_setup_msi(struct iommufd_group *igroup, + struct iommufd_hw_pagetable *hwpt) { + phys_addr_t sw_msi_start = igroup->sw_msi_start; int rc; /* @@ -285,7 +286,6 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev) { - phys_addr_t sw_msi_start = PHYS_ADDR_MAX; int rc; mutex_lock(&idev->igroup->lock); @@ -313,8 +313,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, } } - rc = iopt_table_enforce_dev_resv_regions( - &hwpt->ioas->iopt, idev->dev, &sw_msi_start); + rc = iopt_table_enforce_dev_resv_regions(&hwpt->ioas->iopt, idev->dev, + &idev->igroup->sw_msi_start); if (rc) goto err_unlock; @@ -326,7 +326,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, * attachment. */ if (list_empty(&idev->igroup->device_list)) { - rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); + rc = iommufd_group_setup_msi(idev->igroup, hwpt); if (rc) goto err_unresv; diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 22863759c3bfb0..7f4936cf537be4 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -267,6 +267,7 @@ struct iommufd_group { struct iommu_group *group; struct iommufd_hw_pagetable *hwpt; struct list_head device_list; + phys_addr_t sw_msi_start; }; /* From patchwork Thu Apr 20 18:51:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219099 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 93304C77B72 for ; Thu, 20 Apr 2023 18:52:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231199AbjDTSwY (ORCPT ); Thu, 20 Apr 2023 14:52:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231897AbjDTSvy (ORCPT ); Thu, 20 Apr 2023 14:51:54 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89AB94221; Thu, 20 Apr 2023 11:51:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JSRmmjDvZjN4/WQuFtFBDDSUz7YqPADRW1M1dI/hgwm7pKDkSL0ErPkRNqZLbvUoNRQiDY1DxCcy3nmFG9NC66dl36eKurrxvTxS14/G8ngWmweEayLpeocOwnUZaWqtwewaEuWVFuz2AU2frOxVHs1LrNN7v/EskrP1ombyyJQKsjnQHsyqVyGkiTOAhXgKuihGtNRX5Po4SlVqa7ee/+WeHCMIwoxYklDlUBBBIA4B+GMAJgdKVBXW92yPQlxyy8BDD7fPqzMWnpVCl7MQFti1r0avtpa5b/iAkCo2sHxmXmvomOC6h17rDiHbtwESrvBlLEeFQVbP31apYx5NfQ== 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=jxa71Jdmk1BalfkQ1UgLAXDQ+nsULNuZsStUUfI6AN0=; b=akNENlJ48ZIvg8EXdxcgFhliye6i9Cjxg8CUpK/muXAJ4Fd7Jtov09ZiWjn9PXe/TXyPCoppkshNEOmEcDjQ/p29nnZPYgadZRSI0dA9NsT9xaizZ+lwcbyQlLF+otDkRZE5FPgYrGeGR1zuNMfhz9TFhXk+3W8+xS/u2x1abIbpucmkCEr9MqQD8pSLFy+dPFyBfE5yzIHOJpOSlOLMTYIrKtu/K/TiRBTL5c059rbotBaUtaDDXb2kJ86ugJq3q8xH4rBulFI5t8pQt+q3iqR6Si+XA0ZHiTC8GsuA+Jxs5tMEWvQEZC0yi0Eb+hbQQIcPLKjJ+Tj6VTh9I/GlFA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=jxa71Jdmk1BalfkQ1UgLAXDQ+nsULNuZsStUUfI6AN0=; b=guqR3rjZHAuDX7Afxqydklvs7/+mXzMia6i+9/fd1RYt7Luc2A9LtlZ2xTny50V1dMAzPgSdPtifViABpFATb+XukIJ5SG+2vTBFhtX/wfvKXwDZxbyA+qdGzvXrQVGMELUd4ZG1JKusXWjZkM+9/jzBAD9//dunsmL65czbZw2tKkL+pNRtFBln6R3Sg7kMSRGZGbOVvs2yvyh5hQunh30+MTnLmsEb31zBuXEPrOvk3ZqDF2GFai1ClNA9QArNI+YQCu31huNokRxodW+/vNsKPQb8Zb7a1bKEzDz0ZSFqjjt6CxhnWU3KvjXj6VSzD0ivUyfOV2RszzGflxPrdQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:46 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:46 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 08/19] iommufd: Move putting a hwpt to a helper function Date: Thu, 20 Apr 2023 15:51:23 -0300 Message-Id: <8-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR14CA0024.namprd14.prod.outlook.com (2603:10b6:208:23e::29) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: 1b44d19a-3ccf-4434-1941-08db41d045a8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: k686SuqL2jAeIjxJBNifWI043FScc1OBgFvqBZKeUmczl+AEOZNJ+tEIo/iJyasGMDG4Lg2oRYbZSUZRhm9M0Ucf882kY99NeF+8tNYcQ7TDpkqYJ5rZYKXy23WVOls4K/6OaYIfvllxgd6HhA8S5+Y4JxYcI3XoW8qolZ8HU4YGg08e4QNEjFgnlaUpxf8CzG4GsRjwQzjCBDYmF1Rd/dclelxri3stJaillBHmF+L+2TJgVZpHxGtSIk9R7izai3f4bojhO2d5Aq5+KpXvZ4gtReN1PT11DHCuZH8DGNLDKqJX48oy27l0aQMl/d+/3dTMCup7ZQcLPUW5Lhw7KICVyDycxdmxa9H54NKsxVLuvgslN62XEowmvyy5Hja6jcUBgWLKVPwOu32Z9t1kkMu9ZAimzQSTzyUU/1JQ2FbyFV6MSZg1HwJS0c4hC3NEcvFHbrmJ+3qYoXFED9h9KHPK6g08SZAYqY/ReUSrYCuFVTIBppOfHkQdn33Bod/B8OaZ1wrcZSKCUwCs5N032cZbfRC+R+D/qqIl2KkNPSVvkvZDbMOZMFd+PHdaGPh3RfGJdHTe+VMeDyd0Ghay439NEepP0rK10J9yDPPXhsE= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: f9/dmvQlJuv0qKh5AlpId23BF1FDGM4aHe2p64m+ggbYcv8rbkHgq+Cd+kpjE4M+ZAo4nePNRaGslqGGlH34yUzx2xXPcVYieP09rPYSmp8sZ73Q8waPTRJ6rPGJVjVn1+npuNoQ1D5cLlXEtmi7mve9kYyf16Foqvp0Nlgf9woyRo4BHYdWgEkEOUNIoVagUEAETGraFE54IAQDTjSCaxuDpV+68nexz9J4EIhOxD+JYWMsqmdWhhk1/AvSM1EnZW3w/CrOwmeXBga1OnX6ptjLo6vmT0Lp9VT2JVEdQbff7Wmyh7JlFFeHpZpezfp3/dFa9DqqAKfYlDIOt/R9ZUpZU+X0YARTXlEBmd4DQVrNe8MXX2yHtCJjxijO2IjYdi0fePIsr6pwEMT55+BbHZGI2tVYO2/0UtQYdkB9k5fV5opwm1xmf8flpOB/T9gfEBw3MvZU0vSMsg1PLkDdT0v3shWXT13yUOJMlWzOqRtdHF5FAjFuqqHthrlY4aam3jMGDlbkDIKuhuZFuUuZl4dr/VWDdjhKnAuOu22L1O11TRCZxQHNfjdhyOhSsEMFfAVIdie7QTDl5SJ8cQVPIKhnzc+3BbJx/Iq6Oek1rSaBtm5d6L//TIYX4UMgZy16ciE5wqhOv6tGPg1pBmIr814RnHGq+JgLTphsttzmgijQiKiCcOelQ+fwxjGjdRDuYuNYuQLARCJm06u5HibSpqIw56tB+NWvQL4YhCy56vcfiZTP6DU0jZSBIwHHaqaGb6Q+yPJl/IEZvBDL02sOuhDtlAo8KDxBcsjWeZW1ym7eRHul5Pt7g/ogLKL924o4+3ft4kzx6IS84bH4+kvQB3hPSFqu46hynUKxKZLoy78+ifFRRHEzhHf5uo5HT6k9uNVnHrMhuubX9BUI2YDJmZkjxxfk9zvbIpQXZ7G7vwTY7s/5d8AAdt3ogyL4G60J0GDHWEWzIoJu4Xui+xrIjn+GJuQS/gWrTRoBTEGLx0XFRKpVTn1omjyQKfPE4OVLKCnsTsBvNWuRQrBSuqhDpsZRRSIx8v1njmLBIbymZnGxjuv4rMknT9yo8MQiXw4FbdBXzt+s7aUvRK0qi8ykCvvSqTz+/x1yRMAMLFItE129ZtlxJX/ykfVi5zO8zrHhzMyB1PzyBTFpJVVs36qI0/DaKwYPmWkQ0Q5IDVpyp8ICRSJ+Vi7nntkOU7AGxEy2Ap/CAaZZwon24DcrFqtqc1FSglvhbg1CxevmlrBZ9xlWEJhWZqj5QwomyQI1qN/qummn2maboZJTeW8UQzVXa3y99I3OXqWxhpi01hOcGuYmSNR5HZsw4KXy/zXKHh6GJPajnckqTng6SvfVA/swzb44aYZagRWMWBkh3LPAoVZI+viuTn5Gasa8JoHfMz6tiTUFcmdDgtjMoPp5habhxpwgbhTjil0+NtQASmSk9Ou+7ID9lH85doha28hFbZaCpu40M/Ex0E2NLolWVYiR+o4PbMwNNMvGHI7DGf4p+c4bNw1Y5f5k//sz/QML7Z+BH1okrGJ6y2QsLX3xilRCYcdADE1BaRu0SMawJKEb9C1LB6kHEJGbWNgWLaZcbiRa X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1b44d19a-3ccf-4434-1941-08db41d045a8 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:38.2144 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: N6lDaIWOjuayxLzzB3s0P9bywXki+4CSTJWDxmV6MRGp8IBGo7lt4LhVkrMpSp6J X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Next patch will need to call this from two places. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 6 +----- drivers/iommu/iommufd/iommufd_private.h | 10 ++++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 59fca93bbd0c47..ca9776432c56e8 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -483,11 +483,7 @@ void iommufd_device_detach(struct iommufd_device *idev) struct iommufd_hw_pagetable *hwpt; hwpt = iommufd_hw_pagetable_detach(idev); - if (hwpt->auto_domain) - iommufd_object_destroy_user(idev->ictx, &hwpt->obj); - else - refcount_dec(&hwpt->obj.users); - + iommufd_hw_pagetable_put(idev->ictx, hwpt); refcount_dec(&idev->obj.users); } EXPORT_SYMBOL_NS_GPL(iommufd_device_detach, IOMMUFD); diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 7f4936cf537be4..8bf053f4d4a9ce 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -260,6 +260,16 @@ struct iommufd_hw_pagetable * iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, + struct iommufd_hw_pagetable *hwpt) +{ + lockdep_assert_not_held(&hwpt->ioas->mutex); + if (hwpt->auto_domain) + iommufd_object_destroy_user(ictx, &hwpt->obj); + else + refcount_dec(&hwpt->obj.users); +} + struct iommufd_group { struct kref ref; struct mutex lock; From patchwork Thu Apr 20 18:51:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219100 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 5B52FC77B7A for ; Thu, 20 Apr 2023 18:52:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230376AbjDTSwX (ORCPT ); Thu, 20 Apr 2023 14:52:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231889AbjDTSvx (ORCPT ); Thu, 20 Apr 2023 14:51:53 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3602F35B5; Thu, 20 Apr 2023 11:51:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aFgP3N9UBgvdKGAdfWvK+igH/3sv+EMSvWneJaiyzQCRTetgp73IPNSSuNLHqB6cokQypofpMCeAopPyaYYUoVtbjSqmw/4lTi5xKLSTy8UIEohDnOhg5d91Idk2AEmPjSYrVD0M9CBlzCY5Kzaj6siTXY15dQdKNfTosjc2lw/OyWLJy/QGDQGFuF2vmqgOpdkV4KTNx5GEW0Zu95Gsj+16svY1VkgXNpAWBY4ilv1Xrag3RugGqKlxzmitD/QKixngulg39+qcs2SwRJf5yjymnYTA06nHC8mCPvEH1aP3bVi/TyZE3BjkuPpr9VmCWd/1f7mufBndZKIOVHcLlA== 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=7LrdpKynyvC2fkflYtlVdZXliUnXP3yg4qYyvtVteIY=; b=TGrBN40+fIZ97unBvQ+c3ZtvwHZwXQ+7GTeSnAA5uEVq3AZYnGwhJI+Yn/Y7D0sKSa9XiqbNjFbHSZ5lOSjne0F9k5FUxAkMvocXLI2nHJBoQjlxiXcSduRQyiPuJ6B9e3P1DQ8oXef8fhewNHKHjPP58JfPgnJeFyTIrC9Ao2uXX68N8GoynZruqUeyFM7HJ8+BL4BRhxMb9CPPpWt88W27l7sp2khaqkFBD2IzY2Q0RuGfTIYTu5Xk9xnVPF/quCazWc+VrpyZglve4wvScxh/XY4p5Krs6nJTNaAduVFHmYSbzgc86Sq7k/PKuPOqeM5kJWQVWnCt8kPcHa42KQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=7LrdpKynyvC2fkflYtlVdZXliUnXP3yg4qYyvtVteIY=; b=foLRGjHUfEG7DLAUSt+7piEigep7qnRiSu7Xfgr9xwKfZ0S4URErCpi83mYZPL6lirl8bR8c5WgEfQyOyMhc8q2HURwp3x9wW1rTiqIBKLoVmTy1CmVPiaF8Ogl7FVE+mZ2gSF+9FJSV/ruOgqdhoL9QCB0QWmkUTCQKT6fTk5QyhWN6ODtedHLWK383ZavxmEY+pR7r+OhZT5vEGoN9rSLR7uv5xkFgOdnv7BCvFILZcWfjiV2pPOh/K9jwvMGrNNb3nPX65/xjqFf3YHocdqArB2IduthZXYYyCbAN6tDdVFInvm06ypMc+lOYpRH6OwhYTyj0QWVez7onf6ntBA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:45 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:45 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 09/19] iommufd: Add enforced_cache_coherency to iommufd_hw_pagetable_alloc() Date: Thu, 20 Apr 2023 15:51:24 -0300 Message-Id: <9-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR03CA0104.namprd03.prod.outlook.com (2603:10b6:208:32a::19) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: 15ac0831-d738-49eb-906b-08db41d0459e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Xr1eA1sZpgMQJjt50uF+FRlMapAO7G/cXdL6GeFz+eQH8w8r6TwiaRhPrmHQtel8x6nc58LfmhfVVheWjWazOtQYu0Y7oATxG1poYTxaxOm07AFY5q9aa7EQ8napm5p6YL7hPepvpIJto1eJgsXDUNL8uzb7Ni6Sm1l2ma0pUFLNIyAQvTxlSYqio4wdBZB9GuaRO9r23z2Xw36KbidEhcZjrdh0uW/nUC24kqwKVh04nurTV//rkCg+ff3VREj1X2KBM1bg5U9UqGDM+eCwxUyKM67g5wde8uUmRJd24AgZQUdKWtnVemZ/H1uicFj4e/uSpS6oqTJvLbuvxOHEvTaT7TVGSdhO4BBON5U3JU89mHiaRsyzrb+QCIQCJ9HHpBOlkfweLR+HV6OfQDyowkGVIh5zPmTmj99Bnzvcfz+e0e3cCOQvziq6vxlBKdyPcWbv1YXmbVK/FJbCygQmFs/aTg8HplvBbGyExjq7XFSHU0I375s7cXxpE9LXt9zUJhI9/e7FxuAdAY1+FqnMsLetBUsW5AV7UJGTw4Zr5Q4ZPKPUJfbFpS2mD3MgMJUnWiZ8q3o2cotATxKUhS5SXhL1Bk7l4PoguU7US/D5hFo= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: +ulUdkm1QcbOq0Bz2yNnqoE3YZ/K04uodB86Wg1GlNQmXEQ/lUBd+DGUmEllG1IvqO/1z9UI9rp8K5h+HzVxzy/4S+EOlzMDRRftmnLp5IggWTVKKMvIbkLi9KBh7IyzFO755h31uiM7yCacslWTPlMuCgDTCOT51f1MSkRbbV6fH72yukUA0ZUbWlMp5SCkU4V6G9+2TerTSr4sjYVs8NEE3iD4iP8y4oHeZtB4J/ukrmIhWKBQ1JvF5hKPnCP1X6/IPvrUJBKN3uwa7hr5r2F3gZExIXOTyIsyUDhrkbhwYgQyqxvCGvz2o3dXu5CW+N4iLlIhkH5xX7vtML6oMk8qB9mpAi0mm6aPh/adDSZNORTt7ej/aLTRTHeJWHNo/YsxYLZYPUSJxrBY96B5YMLsiU12Cu7b0XuPttVf3aDabzrzX1gQ0WjBP2Fe1n+Azo8tSirUtrdoiP1Wdg30C0XDvjN1qnxD/pD0DkwxRcjq1RnDC2e5PebwYeXCcniR2FqHlRW6B6elPcHVFV6NUPgqlbhwFmbZzNQzlcxyo/fhp/j8J3ZtbqkGqt5EeLfswvXg6Bt+ygofkwDPnyhkYEIv6UUIoekvlF8PADUb1Zytb59oomtn9aTvVaRX7+FtY4lbwNu0HOjP3iQNKdF7hSfh5tRGQhoyHcjFCoJufMtjUqWUGfLmKZ303PxYOeHgRkC91bB5GICMcJ8iDjArieeOuleogSfnXkIEoEZL8O0t7gemr+YRrFKg9DRezmI5MbCP9SlowS0luyBggEs7T0jw1yDKri7tQtY6S7qRnoLhd7GPOKRPbKAKITD7Ou516KTnU+grKF4mqO8zVeasYU5GGbOwxCPtGnrp59Ookn17BYI1KYgX2GjWt4iezLyszZlec/CDpwzzi8yyXQaLDNSvC2KvipYRYfK2urXpEkxVzkj6u47KSmjN3CwFPh/0jFZhOTpkLRfW5s51QSuLZFhUkGA0fMLbBxdINaQkLivCMFOTcyvCm/FPSnZ9byINkIrqAOYbaBM/XwxCHW9aYQjOhp0P6mMu4z50xbNA0lpgKPQfPeI0wif3bNVt0D8WkVy1RGDN9Xhhbw1NtI/RFuuVTPyew4YLkvxtISwHlgR4tFwE5ipcrlAZTQXF9b2Fl9RJpqFkWDCcwIiDMY1dPgXxHwPLlT3TUJn+Lxf63gx3Ui9OnwbtSZgOAzWrfSI1VIt54TE7B7Zn0s9+dOqTaPplfpuMdeCHNOEi7MM1rhzZA9VTugHZhrfA4vxHo5x9PeXugGehUa1UCZgEDIApC/AO1AfuT+Umx7eciz2EHiXkf2aVCe8BQnIv7EjRAHfd24nNYra8QjygWMkoZvnKM5v26X7QYbbKGTRYJPx9TL8fncS86XX44T02MRIfVbYMFpNz+upSEOGZyqFKccmG8d27js4yCuvDRftRMejlSNQD0ZqFtL3z8HX8VxbeaX7zr1bQBqPFAHlSRmhgibhWHGpNV3ZcNw6kIgjG1Jn+mGFNAdKrw0cputFsYGrefBIFx5ajJc2Z/ZRqVcmIj2X7CPrhS5o5QKK9sUMzM960qi9Fe5Xr48rqjJefA5i7HrSH X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 15ac0831-d738-49eb-906b-08db41d0459e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:38.1663 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: UQb241LLnLDUc1IG7oDO3V0GA8tYLRHzDJGiG609XbxpACVaEmEXLaOvPdlQNd0S X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Logically the HWPT should have the coherency set properly for the device that it is being created for when it is created. This was happening implicitly if the immediate_attach was set because iommufd_hw_pagetable_attach() does it as the first thing. Do it unconditionally so !immediate_attach works properly. Reviewed-by: Kevin Tian Reviewed-by: Lu Baolu Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 19 ++++------------- drivers/iommu/iommufd/hw_pagetable.c | 27 +++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 1 + 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index ca9776432c56e8..782b4fd76f31eb 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -295,22 +295,11 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, goto err_unlock; } - /* - * Try to upgrade the domain we have, it is an iommu driver bug to - * report IOMMU_CAP_ENFORCE_CACHE_COHERENCY but fail - * enforce_cache_coherency when there are no devices attached to the - * domain. - */ - if (idev->enforce_cache_coherency && !hwpt->enforce_cache_coherency) { - if (hwpt->domain->ops->enforce_cache_coherency) - hwpt->enforce_cache_coherency = - hwpt->domain->ops->enforce_cache_coherency( - hwpt->domain); - if (!hwpt->enforce_cache_coherency) { - WARN_ON(list_empty(&idev->igroup->device_list)); - rc = -EINVAL; + /* Try to upgrade the domain we have */ + if (idev->enforce_cache_coherency) { + rc = iommufd_hw_pagetable_enforce_cc(hwpt); + if (rc) goto err_unlock; - } } rc = iopt_table_enforce_dev_resv_regions(&hwpt->ioas->iopt, idev->dev, diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index bdb76cdb1dc347..e0699d7f4c64af 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -25,6 +25,20 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) refcount_dec(&hwpt->ioas->obj.users); } +int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt) +{ + if (hwpt->enforce_cache_coherency) + return 0; + + if (hwpt->domain->ops->enforce_cache_coherency) + hwpt->enforce_cache_coherency = + hwpt->domain->ops->enforce_cache_coherency( + hwpt->domain); + if (!hwpt->enforce_cache_coherency) + return -EINVAL; + return 0; +} + /** * iommufd_hw_pagetable_alloc() - Get an iommu_domain for a device * @ictx: iommufd context @@ -60,6 +74,19 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, goto out_abort; } + /* + * Set the coherency mode before we do iopt_table_add_domain() as some + * iommus have a per-PTE bit that controls it and need to decide before + * doing any maps. It is an iommu driver bug to report + * IOMMU_CAP_ENFORCE_CACHE_COHERENCY but fail enforce_cache_coherency on + * a new domain. + */ + if (idev->enforce_cache_coherency) { + rc = iommufd_hw_pagetable_enforce_cc(hwpt); + if (WARN_ON(rc)) + goto out_abort; + } + /* * immediate_attach exists only to accommodate iommu drivers that cannot * directly allocate a domain. These drivers do not finish creating the diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 8bf053f4d4a9ce..471a3fdff1e0b6 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -254,6 +254,7 @@ struct iommufd_hw_pagetable { struct iommufd_hw_pagetable * iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, struct iommufd_device *idev, bool immediate_attach); +int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt); int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); struct iommufd_hw_pagetable * From patchwork Thu Apr 20 18:51:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219090 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 4A476C77B7E for ; Thu, 20 Apr 2023 18:51:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231388AbjDTSvq (ORCPT ); Thu, 20 Apr 2023 14:51:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231704AbjDTSvp (ORCPT ); Thu, 20 Apr 2023 14:51:45 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A3AD1FDF; Thu, 20 Apr 2023 11:51:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=F9H+gCY/HU1PhUq7e4+aBdXEfOIDzEPSkpktPuYLNbIhlZCZBU6LteUUfauopBCoB4y2v6d3l3OAfqB0acncCUylbCVKUSDovMtKbmAVP6WoDSSLUyPd4+lQxfgMVxzx96kNvgLLxL637G8l2qeasfZ9vRUCLzZrwYxFMTLanmbbCdNa5A3ArSvwtfF6LwhdgPZSlBuEPTu+W8c+Dm2/MGJ07O/Sj2gFzCRZdr5Z6tcjuSVMoXz1tq/XlopuXL54bKKN0MWeV77o5YU8vFTKThc4pQGSiugFWk339apJdGQQ6B6E3nYsY7CHsOYlz6BsDvgDFFiCZMc/FmhNg1c4Og== 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=oVb9jVnApTzhMvdMF6c8aV0Rw+U9o6GiqOq2QCvA+w4=; b=jXc+XeXkC4//s/eiZ9Rl4BMMTa62skSOoeaVI9M+uzOF/qkh/n5OK4rzTrwuloF7ta0/oVLib1NxYOrX7WcS30f6cO4gzOhF4Vij0+ECIX/qMutwEYHwWpVIlKPaDIYxmJ9mUNxb5af8STeG3d1bf2HUdy0/NOMtZXlAVVh12sr/776JHkzKC8ma7kGnc6H5KotyBCzPkkCF0koea3jIVY2exmzhxrTn/CoE0AzmDA9EAqB32CxQs39AReCMYGxyNrRBY6CdGgjr9slP5MqbGJkqhJpqwH5YJ/gsF9W/m8y9SY3ipJ+SRjefhmRRxq69Z0kulBZvsOln2TpEL5vdTg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=oVb9jVnApTzhMvdMF6c8aV0Rw+U9o6GiqOq2QCvA+w4=; b=N9MurN+xrfTJCspkz52hSKhBcYXbUtSukUafmHX9gPPZ7ceGkS1ureSq/AmtGzCOfwdHwyAEzA6Lr6ogDUWIXgIqmTm1XnlBmhBxCivjMsuqi6BECxSedFB65wlVu73WEU7WgiN8+bgvTm/XKMuAR8izAtkPa4CJWWSQ9mgK3VrUfbRdgVjxYP301lpLuyMSUxlj8KtPR0txZW+ztxLU9Dkwu94g/eNfU3FWe0bKnHbUK9tpEEo/AvrD97jJ5ERByKXHbXRGyLl30541gUhRGtwzW1AmNy3V93fxIJMrPSI2TFel+46DoqpsLsGQ6ONnNMxyDnr6cLI9BV04qH15tA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:38 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:38 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 10/19] iommufd: Allow a hwpt to be aborted after allocation Date: Thu, 20 Apr 2023 15:51:25 -0300 Message-Id: <10-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR03CA0016.namprd03.prod.outlook.com (2603:10b6:208:2d::29) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: 639f4dbe-2d17-4612-b9c0-08db41d044b9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4s06YzF63trhJ/hFNVa1t7Ra8RzHtLW7RGWsMNCwtR7TFiF/+WhqR1HpKSFdyIYlqQuLJewCN/jgR8xxgi+5csy2ohY/s1LsiUgVbsQ/8QO2tb/YQ+DVQfZTieU/XDwfLCeMEVyQQ9UpHGNuxNxnJ6QA9gvlQef4CgeXVGYqVoScxxb2W75d7DizzONLbYZ1goWx8Vl4SYSykg0Igk8/LCZVFJdmW+G+cqBaRTBmAl0A6oNXTC4zwFxb9RcJNxsExdw70ElaoZLMEDjk+RMukCVQlJHdmIgrvjvvgZnvnPLubtmiHBuaY/YjC9J9zYawk/eTESWoMEKnHVDg5VPAskS6yX6CEvrwE528+CZjp4vK4E8iu0NJKLFmWoD9SISviLhSKHwtYz+PwuMoJ5uSsdftqSERSXX564ShbJO7AISjmZa1v7tFZMgOc3nVYzvbrlTFCcG/qYMa1zeS2MSaCHgL3KN1+orobEH/GEMNW+n6BJ53qrWIX0cR7rQnqVKQfU7CrmbsG6nzNqwarUTYP7GFxi7RHCynAaRhqWvLmUsNNvznUeHACz6QAxZuxVrr X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: HdvuEaq6Qj9nRVF1JnJilFxQqlOemEhLAxJPSenv82U1AWhQImXEFi6NIIh1z7UQUr2siJh1YUJbnwBRXbHfeQBjdrSO9xql1u/lZYtK5Ln/mxa8VY5Zz3nRJdTlx3KRu7yRtTh6waMSkVG6ddXgz+Cls3iLpsWWW+vC55Gx1XKCbTk0ar+vgI+shAgSc3cIpK3Ef/mTiJD0NckguvkCvdhZI6Eu9EXfAFA1C++7E4R/o0nbyRsChqBinzEQS8C+tePbEch3NPkHFT8NLvTE4+69kQZ6Z0VDqOP42o3Br8ggK4GiiFCmJFSM6yY/aLoMHryDraWzfNX2KjY5+PJJ9m99rAXn4ui3NKqvFAFS/e4CIf2KEV8Jxnwnv1YZzZBAchL+WjVQzCb6zjOPARDMizHU7tXPNT2XtIZmDarh3ET39D6MGeOspr02BhXDlpBQrujTZ2B+pJt46mleb8QTP/McmgUY9RaLYJexFxHWL34T9WtTqtT33M08lI7d8QukCRgrtWW0GqlgG9Bo0C9jvDPMscW8nd9vjDZZ7WfNb24xngrtioHELRTtHM2Aeh/8g/nQuZR5Qa4JR/GJhrp6kLIrp3TnYijUtyoQ/bhou+8rOy/Ep3fDY+BLXfkYCZ1POvsZh6sJhXOIxy3MwfadXxtz44kKGfAofq0dsQmgctlEnU4HMW9CjWh/fj9ATx9y5bXW1zu11IDB4j3I3SSrBgGo3l2A2yMZoavrww1ykMiPsgVIiNkgVsV2WQfDif5rDVcah4EXEGuZ3hPHVIGWuBozsWvBOWA9a4H26nZCsAM/eBjmlqvVX4cMF4D+wliywZE9dWWF+wWWbawuR1IXPQWndbC4hh/hU91J/ZS5aS7U5EU1/DaeeqBRGMD3PJMO6rDx4C0f80NW5skgL3dldBOq7Kr6ENOaGzAKKQ8qNJPWj7tpgdeM+SaRIAlcF+GRgT5Wswjy1UJ6R3yB6LObvA3px4oN5zoadTM/ulhGzxfHV0Hl8znGjs/JT8pUtQW5HRxsayPI+EnQvh2GnNMKSPIth4pHSw+Vr0eCndm1JyBibQyh/moCeMdEzx6XeLPydhqeN73oNVU6PIHa1TDbdYDs9gddzZQ3hK2UikQL+DEjt6kxwxfLE6lbJmt00j+jGWLW8Pc9TjqHlbAe/Wkl1Sx/fNvqkY1TCCOcSlnXwUMBxmNcs20Smt/KpalxZBYtbmiIIagduBYKN9cDvsUWTyq0VB1kvweVWDDkfqkKLQIB7Fn0m73kAMpXZ3xkWHqqn00nRcAro56Jqu08bvrW/ZprDSAnfq2sgdzmAYpBb9V0D4tGHvrGBI4Q/DyDT7xhPCiVcvxseJczB1ZKcQWnFb3TN6aXiN1uodTv/Cz8A6hmISUsNe8yIr3Om6XqwRs33IypXyl54WTb3/6Ss4sztG5hTuJANLn0fIojwJ1r4uQzROwLe8I01CYCBD0rVQmbA0Os4UglPVq9nkNC4ekhPRD7pW/Jk9ps8Hkc3hRcmXfFqIcsPbspq1Qcu13kwEQMVz1zOIIDdh/wPNRbQjC1yVd7/zT7UaCyYLSuSmKleebXePoDR+ts3qTpPDc8OJHq X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 639f4dbe-2d17-4612-b9c0-08db41d044b9 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:36.7080 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: D1Y1d6P5hOjlCiRoBbOxoAlGN/9prG3SG2T/NhCrSE8HsBuV3TYEKufk59sueJJY X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org During creation the hwpt must have the ioas->mutex held until the object is finalized. This means we need to be able to call iommufd_object_abort_and_destroy() while holding the mutex. Since iommufd_hw_pagetable_destroy() also needs the mutex this is problematic. Fix it by creating a special abort op for the object that can assume the caller is holding the lock, as required by the contract. The next patch will add another iommufd_object_abort_and_destroy() for a hwpt. Fixes: e8d57210035b ("iommufd: Add kAPI toward external drivers for physical devices") Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/hw_pagetable.c | 19 +++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/main.c | 7 ++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index e0699d7f4c64af..2087b51d9807ad 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -25,6 +25,21 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) refcount_dec(&hwpt->ioas->obj.users); } +void iommufd_hw_pagetable_abort(struct iommufd_object *obj) +{ + struct iommufd_hw_pagetable *hwpt = + container_of(obj, struct iommufd_hw_pagetable, obj); + + /* The ioas->mutex must be held until finalize is called. */ + lockdep_assert_held(&hwpt->ioas->mutex); + + if (!list_empty(&hwpt->hwpt_item)) { + list_del_init(&hwpt->hwpt_item); + iopt_table_remove_domain(&hwpt->ioas->iopt, hwpt->domain); + } + iommufd_hw_pagetable_destroy(obj); +} + int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt) { if (hwpt->enforce_cache_coherency) @@ -49,6 +64,10 @@ int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt) * Allocate a new iommu_domain and return it as a hw_pagetable. The HWPT * will be linked to the given ioas and upon return the underlying iommu_domain * is fully popoulated. + * + * The caller must hold the ioas->mutex until after + * iommufd_object_abort_and_destroy() or iommufd_object_finalize() is called on + * the returned hwpt. */ struct iommufd_hw_pagetable * iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 471a3fdff1e0b6..a5d5a47641346f 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -260,6 +260,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_hw_pagetable * iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +void iommufd_hw_pagetable_abort(struct iommufd_object *obj); static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, struct iommufd_hw_pagetable *hwpt) diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index e5ed5dfa91a0b5..24f30f384df6f9 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -24,6 +24,7 @@ struct iommufd_object_ops { void (*destroy)(struct iommufd_object *obj); + void (*abort)(struct iommufd_object *obj); }; static const struct iommufd_object_ops iommufd_object_ops[]; static struct miscdevice vfio_misc_dev; @@ -95,7 +96,10 @@ void iommufd_object_abort(struct iommufd_ctx *ictx, struct iommufd_object *obj) void iommufd_object_abort_and_destroy(struct iommufd_ctx *ictx, struct iommufd_object *obj) { - iommufd_object_ops[obj->type].destroy(obj); + if (iommufd_object_ops[obj->type].abort) + iommufd_object_ops[obj->type].abort(obj); + else + iommufd_object_ops[obj->type].destroy(obj); iommufd_object_abort(ictx, obj); } @@ -401,6 +405,7 @@ static const struct iommufd_object_ops iommufd_object_ops[] = { }, [IOMMUFD_OBJ_HW_PAGETABLE] = { .destroy = iommufd_hw_pagetable_destroy, + .abort = iommufd_hw_pagetable_abort, }, #ifdef CONFIG_IOMMUFD_TEST [IOMMUFD_OBJ_SELFTEST] = { From patchwork Thu Apr 20 18:51:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219107 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 35063C77B72 for ; Thu, 20 Apr 2023 18:53:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231411AbjDTSw4 (ORCPT ); Thu, 20 Apr 2023 14:52:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57506 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231993AbjDTSwi (ORCPT ); Thu, 20 Apr 2023 14:52:38 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA5FF4487; Thu, 20 Apr 2023 11:52:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aJAfP6ySVb71QmvMvknnXHQWkM2ygqRiEbm9FaUE/n8if9RTxuxChydr2LmIeX8nLjChKmlTG6tqikDZKlFQJ2LDjKt0n3DgvJ/4YjpcotuYWNiWwX9U5eZ0/xBo9xt8TzXI/E9oLaYrPpFuTm8SBo/4nnzRP0q/9/Dw4pn/tSVzrT1y6aDxvay5JcS/0to8wVSewQLQCiSLA/9vBD47oMB4RishySWfJVCmoZn2aVGBDQ7jqgV1EtAnm73A6ySo1V5Dbrc/ns32vdwDQ8FlRF7Y1UnUZfPSPTcDxOXOpCBAPoeodrcPMi7F7KLU3zAnNRRCOa8SgAjBK48+82wXZw== 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=vYSLPWKndciQVJbztWQI4cCGJufjw8djgHM44HQUGmo=; b=DUmOLtyuY96E9fy49/lBI3O1J+O+Rm5131olYZZAb2W8rayhl+rwtjrP/zKSwM72OtKznHgeIjnea7PcwZm9QFjPJgsr3pOSRK6sR0DihH8wMb+h+/Gb7uDdBeIe4vZv8zOFD0CHgfeiEMMaQ64ctkDTaJ0/IJrHSVSibCM4i58qLaQnvdAlyUtHJxEd3CkFfmXXPcfBo91yHsxyOkwf5L4jf24Bvs6wS4uWIiLy4JhYG/e9AIrq1nlyoQEblc8skOml0JLBSSehHLlJImpPCnBDXJUMQOziDYGv2EHJymKhgiDNPUVPXjy0B3Nb1pxIMkxJYBOrnMkKO+P6Jh0Ngw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=vYSLPWKndciQVJbztWQI4cCGJufjw8djgHM44HQUGmo=; b=ozLQO2fC+eg+pMzBUJe4pq+QZ072u3X9FB9GzTSG4EP+rNbmUmiPmzoSG6G/VH+Ssb7mj09nkkYzl1iqYA4AR5jWicCPCsoncy1OINNvFVRDapoqlG5ZGGX7Vz3dXAUXd9urrlYom13C/fkr/N9RN3hqawAMVBcWmucCWD+o5Sm/B54pZoRlfbYgfNJRl3WHegLehm64ouY2OVJxHi55HV+RXajW1LMTMgToqbX48+HcQ3OzJrAhUD8yz37ndXG4xag0mBovy/BMXk5Z/vGlozIiWGbXBbQN45ZlXir5gHaH7MFko5LiMWNha0gc7mausba3gzdULujpFfqNBMswOA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:49 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:49 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 11/19] iommufd: Fix locking around hwpt allocation Date: Thu, 20 Apr 2023 15:51:26 -0300 Message-Id: <11-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR03CA0011.namprd03.prod.outlook.com (2603:10b6:208:2d::24) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: 4bd85004-aec0-4196-4046-08db41d0467c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ItZhLMS6V2y/BF88VFq9JVDkibw6JnbSnRSx22qLe3sWoysF0leeUmZlez/rubwB7eFeJuBplZckws8FOzgQ6/66LpT2MyeNcbFVEophIlnHq64SyY4cdggC3UxGRvCPq5v5RSyhNZQOJpgcAp8A2xmNAkw9pA+98x8gkk6MC+bEiB3WnUcsUnYNmmhjpxNnC3IqI9u0FowwdQpIpRlYcekswY7N0wVJyPsQ3Kf04JfIA4ppyMYGh6+6ITgcVQDIKSTKG5tgTsT+ylorwsl4OCtMR0/OwKvTvFXSWh0NM+NKVn8PlXqY5jtIDV4f0idt1/EbK9WNWW1ZrHyALFNPzpIKHSkTLeOQwQsy3ejdhKqcVqqOnh5dbEzjexJ3oFPnXInIgJDnI+WSoJ66eh6xnxLG3gQ5Q6GEbKpUFxLwCDDbhTO6K6ZB5usVUIlDzLqgBgtnpjXkfTtW83G4xT1RMrR18SWGVPw6owtha+AOfL/UDCQR1AKCCWkLK2fyAYqg/gypi432D/2VxAl/ZMHZNPiDnAS4oU+Pmr/M6ep+Qy/6hYUKpaBkYHFX+KJw3q4A X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: VzEjn4zkxlSFABR4LiYoWUy1fsL7JWlYXBsT+qVL0yhFFqhi7gFvnw09xC26PA+jy/0jJ3ShVQNP+vdSz2Ba/iFd52hFvQ4lrS+/jJFz2SY86o7dLVsVgonK5/I3fE5StcNQIujZNcduI1Zsg0HI8dV0HZoT1uevBPGY28KTpWpfjzNj3YmS8AarMDIFPmLRgpBj9qO0cetRQuWJlpyH/Wt5rhip89Vy7W8BN9aLy3f9GT37/6RD7w+ahC9CclWPfjBtSIaXx55Afx2/dradNk+y2j9vdxkXkao9UGC9M4UC8JjAKfXuyv2aXbHdvMuW+sQDrf/2qsCTTnQ5bp0qI8CLcn2xRvcETBdE2sZqKEi18IRlPUdeqhZix0Rm524v6XKgBHla5wwRXzL/5qsd7WpwerO5sUKweMv5tv2Nm7Yn6Rff6zebiSIu6/vms5nYt1qZ1Bcvje5tKIyN7aw0fpQFVt4TRKWUAQP6rumBMqBbFLNgMhYS1QDUc6nw6JjN4gMxVA2lpy9g1cAl1MU+UaFQ3umzbxPwd/sStnDi8y4lMUtP1Lpgu9eI/sTe9EEJMg2LeKy/LldOPnfq/YwJn3un4A6FVpIyPAvnNFwatrpzqX7VmKQCDC+RrTPE7aEOVxBIJeJdyhu4Rse9XRTEZo5LkahHl9KPVz50/fRqfOW3Wbo6V5QZmgAj0YY4s1oExoBWK200i6+JOLrGRRUjzVh/N4c2twz+etNP7NBPC5CYK8SMvZN3sSCBAMKpyqjNlGZXtMJEmxGXYGY18MvYJ/PVYCNvxzhGyWDsf2mD755BYFemnsjZmCa2hKu1Rp9hd6qtPFafX/shhXLp3Z+klgpG4kIL3GvKCoIzY5Fhx/4etiNWuMI0lmI+gcHwgWlPi6bkUHtpd1a0YP/wEzqzu0AriSpjcDZXvrF1dPAmXN4YeTboYohly4fob6OrN62BKQM9R0TzD8WWkKZnXv+5tZXU67o0/kliANPxLT7oXoGEfBMbqtuDverUUWfNsDVDm2wEV+GTvxrCuwvOs8KHnTMjmYMQqB6HbKL4PFtdHIgdqNubKGeIZpumlFM/l0Uy0YRdTgPNKuB7dac6YRRoAAkQzdy4BiBuhySyF/HvpG57ivR0y2UEuJaDH1hN8i2drBvx+8o9Wx4zeNF+N7h3TGM2nW23Mm6A32RmH1CqtpQsp/CKaVFAJUOmTqzOzO7PJPHqIXTeH4iYHblY5n6ELQssdrbN4HPWye7jsrXQz/uXnNmLgMbmWsMA6+dmZmDWfbjbLjmBAVxvOx9yd+vLprXlYpIC8hKfFuJrNsfsLG6yo2+RkusozWliut24K4qVNTRzaG3NTbN1hsRCAh9HoUB5fjgW8LP2QGWaRU5JzLoQHGncdURDfDOtuRSTt9ZT+wvqjCIdV/mSwn1j3fU6kWgjBYFZP6Sb3p/U51052nPeibhX8SM+WH84of3t7LqUVp8iVyY+pKfixTDneUJjk5Ibu0N9D65wOzqRhRAYP4/VJgS4KjLIs/dHdO7UaB9isARWQpSGY0yiioPof9gE6XtQXH+yKCXRqboex/kz52Ntz/2Hl8xsJY4AfsQENKPS X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4bd85004-aec0-4196-4046-08db41d0467c X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:39.5951 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: s61Up/wx+fzEgEDQY6Zp1T/TOt6q4Dlnsax42qn54dUS7mZWo0vTV2FkbVfmBScW X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Due to the auto_domains mechanism the ioas->mutex must be held until the hwpt is completely setup by iommufd_object_abort_and_destroy() or iommufd_object_finalize(). This prevents a concurrent iommufd_device_auto_get_domain() from seeing an incompletely initialized object through the ioas->hwpt_list. To make this more consistent move the unlock until after finalize. Fixes: e8d57210035b ("iommufd: Add kAPI toward external drivers for physical devices") Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 782b4fd76f31eb..2155fafda27a5f 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -398,8 +398,8 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, hwpt->auto_domain = true; *pt_id = hwpt->obj.id; - mutex_unlock(&ioas->mutex); iommufd_object_finalize(idev->ictx, &hwpt->obj); + mutex_unlock(&ioas->mutex); return 0; out_unlock: mutex_unlock(&ioas->mutex); From patchwork Thu Apr 20 18:51:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219095 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 4282EC77B7A for ; Thu, 20 Apr 2023 18:51:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231846AbjDTSvu (ORCPT ); Thu, 20 Apr 2023 14:51:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231800AbjDTSvs (ORCPT ); Thu, 20 Apr 2023 14:51:48 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E4FD273B; Thu, 20 Apr 2023 11:51:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Xtl3K8AB3kBV3Ui5T6+7tx8V8zlVSz/Vu/i040p7E3HPmzUH1itM5H/JfFr21vvnyOATgnWKAEeRRcfrek5pLVTmosAhi0tsWIuYdqEeMaTB+Lwh/SnxXuvwG6GFHMgl2QbaE3xOD/+8t2/jPbQfD3V+cMUEM42RwSCAQ4vN3DFD2d5K5LMfHcF21Peh6VbMsOAzFmbPlZr7LeJUL1N1wQSpUl1uz2sATRJrRhOL4894H6v315LUYv3M290oymPCpEf2kDVovov2LkVZdFeaWBkhd8p5ltTlO2zfFR9HvgH0xMt4w42YQiPW94uIxJDkE9E43CXWjQzXjltNXk7ffw== 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=I5Rzwud0AEnVkcU9Eg1RSZBsARPnBNOQKuq/4x50USw=; b=aSemGD+SlBL6xlR1hE5KTE9Iwpq1dtv25G9duRTnX/v4gKvOee9uFbQ++VjKYafyDY75TMyyTla5HDNHuc3IWYRcCepF+yHSQ8RBl7FM/dSXj5JY2SKMJddHA22Smyk8pUg/fEWoccpI3sGh+jm3ZbxdqfR26xza6XESHThyCgUCAv42itAUQvJOyzQc0IYNerb4/h55JjDP/mjrLAdPafYgGTAwL1NXv49imcW92aN45diZ0bAnsW2wD3Y7VYx8bPpYl3kJBg4mqyzFrn//W4Z/K1vwGYI0dIBGYwQzndS1Ciij3FAPor8k7s5N74SN1KO/QyiiznDWIFJsFWMLew== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=I5Rzwud0AEnVkcU9Eg1RSZBsARPnBNOQKuq/4x50USw=; b=YJwP5vIBwRQEIGHZWgTKYeUgZ4UdpcKuH7jur9PrlVhqCNWE7HGnK/B1ipkRdRILI8IHm/GkIq2OE8MqK46cPv0SUccyKtIzpfVfsILUJqfwdz14oV5Xgs57lw0h7AiF4j/C7Vf47n+RvBlN0BE4vNDswrcPfS3jrLkm5D3H2RMZl96MARKZ79XxL6A+ZVJZGa7xo3j6Rx+I9ngvKVs+wIvtZEZthSmaL+PSykCzPl8kLcDRWQ6ekvST4TzKHoAFkPkdBTIlgM8DCaXXXNLl9cWBwFh0doJFWCRKcgo1xOOVZzPI4nWv+8bu48/Yp6Hl6qXFoTchRvNyJHFay+bNUw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:41 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:40 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 12/19] iommufd: Reorganize iommufd_device_attach into iommufd_device_change_pt Date: Thu, 20 Apr 2023 15:51:27 -0300 Message-Id: <12-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR14CA0022.namprd14.prod.outlook.com (2603:10b6:208:23e::27) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: 1064c4e8-63cb-46c8-2aed-08db41d044ee X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PdoUspn0FcSlwYxQRx9JqgR0gLHNVX79XKlZc48Qcq1szDRfhgZ00Sd5V1pLxUb1aFhjW5F1UnSJGrDussdJht6evtDYQstrwm5vCCZItbpj0S9Grpxm9TGbmQJJbWcf6AJtutEFWX+sP9SEKD/ofRZlWJaHJflC6dLUtw53NaaiHGc8ixMOV1IqEu5t3txK4sXMm1U/Q2nDZq/pIoMC3JCaV+uLDUoW05Tdadt67QvTO1NWR45r1kxFKAUwpY2qV8HUEbVjw9ymHFwJvfPBGVahr7heBMecfyUCjD4iu+D5zWmwEIg/IkoTfkWp5EF5BLaafdkYkN1QD+qIMXlQZd7n0Wo+IXldb+8HgSandsmuo+13kpcAgVISqI/6yJXRJmPsHcHJvbk59c4pwHkviy48czP/iy3lporL/F1Tx3qXyJxqWxsDytbe9hE3sdZMgCXbsgf9JUv6FSDGpKqGxoKQe6nUa9QWvDlIizFsAY1piewRxs5MoLPbJErwtY6zR+nzTKM4CId2V+jqwuVZbcBltqump3T9SFOEGwW+T33mGL17YGmLd8LCX03McbW6 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 316vup4AvM+hRaPiPjU/plhjbmddx4DqVAYvhp/r0cwjXNRY4aD5YOxV4dUs0LSh7fytuKolMImSNF3jhe9jrfxqnEEvEI2T5YZK5rSLMt0TIJ06XlS1ePthJvo7AAFH3A9DO7t030toLI5h90jnUjFj+BulYxjWSduF/4mIds9PmQeeywHl8sSJQl96ciuNrzQ5iD876rjnTS7li49562wuSKUNaoYNagyAnlqE/VZB9IuL8IjzTB5gL+AHx+ychoTTSEeLY2kyqOnGy/9XM91SIzceHCPrzv1sreAp0l553d5qobP3mu91HDGML1Z83Zq5YmlHFPGwrrmPyM0WiXWwjIeoTBcL9gJfYI0E5oce7LYOcGMCF9pPU152JJlN2VuNFGA1tNu1Tpnoz6nm1gMCPnaG0O46mNPvVtoed25TrEbeUAqPccNdN2azeGMP9jx8b516jXdOo+wL/LkembMSOed9WmNvMf9aW94y4ejCnUcOreg2eQPShNhFPXURv7ojoeqc7vKA4m4sTViHJAyBRgb5PtaWzT8+/J/PqrgnOK9Rc/g1QC65LRm5hqCjybP1nUEI5L82fygSFv4c/Z248FVQmITl9RcDpEGmnSlrzqa/XAIVq82zoHx2/3rXPWHWfGu2e8+nSulQuMLHz37l1RJyIIcZMNRbylDQjuOCFJ5CaYje2gEukjTG/JNCGzoH6P29lk2QJVCRevgp7AIwzX4MS7fbHn9tI3Vz+uj2n2n2Kysei1S6dWOp5kL1fzZzBd4n2q/xccAmqRZ+258IKZZSo7wvK8R2OStzoWd5WDVc2iy4s/1OHVihcOqE9K3B9G4Iaa06zcC6+Hb4CSp0lyENdF7Mr3/2jzm3rprw24J5eaDQsJHVSIT1/WBE8Tt5Dd8ZgK3EcoyCos+yWM6xWyyfARW2KxRCWB36aYMNg1+rzUc05nJcUc9yyZA4SRykI4RM7NhBpj3R261ZWF3dcwp8nFYA4RjlrUzGxSiQWS6hHToh4Gw2yDrK4soeTrZ7WbBEMA/XO2EV2rkcwa/F+hD5uZ8Rn80cdBGoy3m/ffhLMU5S5KPtUrZjKcSID5wPx+/NtFKDO5ErwupufXXV+DbleyTrWGgtwywiocnNIWw/mTCxLoWetxWFju+FvO6x2WrvpWe1Cp5l5ZI8ZdFxJFTn4Q6P5+WGm7BUR7MaYTzSQpxudXIlV2kGNlYAPkyjk8q2D7569ByRP+fr2mY7rEV6y1WhQEvSFQb77oVmCrvio9cCNKmMZblFSp8IJljOE+JUDevqgG7YJcihMNEpX+KNT74uuw7Ubb8wpJh4DvyaZ4O6ELb98vRJNdu4cvua+Fb6k5AUEXkygSl9Ey0JqU7XL0nLR9tgPb16uGIonlilahJYKKTu/uIr/dxckr2ro3lZn17mO5dC1i+RbNCF23wzTMILjewZSOeIH+TZQbHttd4P0qAERcSPyh+YzJ0QNGLBENwSHrxl2O3nTt0duMxDaAsF55Vt2GiTNJgIvo+lBvtdcf+4LmRuTcRNKDISB35zXy2UC0R7twTsi3rcs9NTbryqDwmHozEt2XSun6871gh7nLueVtFI/g6k X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1064c4e8-63cb-46c8-2aed-08db41d044ee X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:37.0570 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: pRbpZPaZ2b05tnBabdT90w36Yt6CDUzUsdYtAb2sebFqRVWqerXqB1uhpxNwbSAu X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The code flow for first time attaching a PT and replacing a PT is very similar except for the lowest do_attach step. Reorganize this so that the do_attach step is a function pointer. Replace requires destroying the old HWPT once it is replaced. This destruction cannot be done under all the locks that are held in the function pointer, so the signature allows returning a HWPT which will be destroyed by the caller after everything is unlocked. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 141 ++++++++++++++++++++++++--------- 1 file changed, 102 insertions(+), 39 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 2155fafda27a5f..9d61a62c4be067 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -353,16 +353,41 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev) return hwpt; } +static struct iommufd_hw_pagetable * +iommufd_device_do_attach(struct iommufd_device *idev, + struct iommufd_hw_pagetable *hwpt) +{ + int rc; + + rc = iommufd_hw_pagetable_attach(hwpt, idev); + if (rc) + return ERR_PTR(rc); + return NULL; +} + +typedef struct iommufd_hw_pagetable *(*attach_fn)( + struct iommufd_device *idev, struct iommufd_hw_pagetable *hwpt); + /* * When automatically managing the domains we search for a compatible domain in * the iopt and if one is found use it, otherwise create a new domain. * Automatic domain selection will never pick a manually created domain. */ -static int iommufd_device_auto_get_domain(struct iommufd_device *idev, - struct iommufd_ioas *ioas, u32 *pt_id) +static struct iommufd_hw_pagetable * +iommufd_device_auto_get_domain(struct iommufd_device *idev, + struct iommufd_ioas *ioas, u32 *pt_id, + attach_fn do_attach) { + /* + * iommufd_hw_pagetable_attach() is called by + * iommufd_hw_pagetable_alloc() in immediate attachment mode, same as + * iommufd_device_do_attach(). So if we are in this mode then we prefer + * to use the immediate_attach path as it supports drivers that can't + * directly allocate a domain. + */ + bool immediate_attach = do_attach == iommufd_device_do_attach; + struct iommufd_hw_pagetable *destroy_hwpt; struct iommufd_hw_pagetable *hwpt; - int rc; /* * There is no differentiation when domains are allocated, so any domain @@ -376,52 +401,58 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, if (!iommufd_lock_obj(&hwpt->obj)) continue; - rc = iommufd_hw_pagetable_attach(hwpt, idev); - iommufd_put_object(&hwpt->obj); - - /* - * -EINVAL means the domain is incompatible with the device. - * Other error codes should propagate to userspace as failure. - * Success means the domain is attached. - */ - if (rc == -EINVAL) - continue; + destroy_hwpt = (*do_attach)(idev, hwpt); + if (IS_ERR(destroy_hwpt)) { + iommufd_put_object(&hwpt->obj); + /* + * -EINVAL means the domain is incompatible with the + * device. Other error codes should propagate to + * userspace as failure. Success means the domain is + * attached. + */ + if (PTR_ERR(destroy_hwpt) == -EINVAL) + continue; + goto out_unlock; + } *pt_id = hwpt->obj.id; + iommufd_put_object(&hwpt->obj); goto out_unlock; } - hwpt = iommufd_hw_pagetable_alloc(idev->ictx, ioas, idev, true); + hwpt = iommufd_hw_pagetable_alloc(idev->ictx, ioas, idev, + immediate_attach); if (IS_ERR(hwpt)) { - rc = PTR_ERR(hwpt); + destroy_hwpt = ERR_CAST(hwpt); goto out_unlock; } + + if (!immediate_attach) { + destroy_hwpt = (*do_attach)(idev, hwpt); + if (IS_ERR(destroy_hwpt)) + goto out_abort; + } else { + destroy_hwpt = NULL; + } + hwpt->auto_domain = true; *pt_id = hwpt->obj.id; iommufd_object_finalize(idev->ictx, &hwpt->obj); mutex_unlock(&ioas->mutex); - return 0; + return destroy_hwpt; + +out_abort: + iommufd_object_abort_and_destroy(idev->ictx, &hwpt->obj); out_unlock: mutex_unlock(&ioas->mutex); - return rc; + return destroy_hwpt; } -/** - * iommufd_device_attach - Connect a device from an iommu_domain - * @idev: device to attach - * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HW_PAGETABLE - * Output the IOMMUFD_OBJ_HW_PAGETABLE ID - * - * This connects the device to an iommu_domain, either automatically or manually - * selected. Once this completes the device could do DMA. - * - * The caller should return the resulting pt_id back to userspace. - * This function is undone by calling iommufd_device_detach(). - */ -int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) +static int iommufd_device_change_pt(struct iommufd_device *idev, u32 *pt_id, + attach_fn do_attach) { + struct iommufd_hw_pagetable *destroy_hwpt; struct iommufd_object *pt_obj; - int rc; pt_obj = iommufd_get_object(idev->ictx, *pt_id, IOMMUFD_OBJ_ANY); if (IS_ERR(pt_obj)) @@ -432,8 +463,8 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) struct iommufd_hw_pagetable *hwpt = container_of(pt_obj, struct iommufd_hw_pagetable, obj); - rc = iommufd_hw_pagetable_attach(hwpt, idev); - if (rc) + destroy_hwpt = (*do_attach)(idev, hwpt); + if (IS_ERR(destroy_hwpt)) goto out_put_pt_obj; break; } @@ -441,22 +472,54 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) struct iommufd_ioas *ioas = container_of(pt_obj, struct iommufd_ioas, obj); - rc = iommufd_device_auto_get_domain(idev, ioas, pt_id); - if (rc) + destroy_hwpt = iommufd_device_auto_get_domain(idev, ioas, pt_id, + do_attach); + if (IS_ERR(destroy_hwpt)) goto out_put_pt_obj; break; } default: - rc = -EINVAL; + destroy_hwpt = ERR_PTR(-EINVAL); goto out_put_pt_obj; } + iommufd_put_object(pt_obj); - refcount_inc(&idev->obj.users); - rc = 0; + /* This destruction has to be after we unlock everything */ + if (destroy_hwpt) + iommufd_hw_pagetable_put(idev->ictx, destroy_hwpt); + return 0; out_put_pt_obj: iommufd_put_object(pt_obj); - return rc; + return PTR_ERR(destroy_hwpt); +} + +/** + * iommufd_device_attach - Connect a device to an iommu_domain + * @idev: device to attach + * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HW_PAGETABLE + * Output the IOMMUFD_OBJ_HW_PAGETABLE ID + * + * This connects the device to an iommu_domain, either automatically or manually + * selected. Once this completes the device could do DMA. + * + * The caller should return the resulting pt_id back to userspace. + * This function is undone by calling iommufd_device_detach(). + */ +int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) +{ + int rc; + + rc = iommufd_device_change_pt(idev, pt_id, &iommufd_device_do_attach); + if (rc) + return rc; + + /* + * Pairs with iommufd_device_detach() - catches caller bugs attempting + * to destroy a device with an attachment. + */ + refcount_inc(&idev->obj.users); + return 0; } EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, IOMMUFD); From patchwork Thu Apr 20 18:51:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219105 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 F00A8C7EE23 for ; Thu, 20 Apr 2023 18:52:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231897AbjDTSw2 (ORCPT ); Thu, 20 Apr 2023 14:52:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232034AbjDTSwJ (ORCPT ); Thu, 20 Apr 2023 14:52:09 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F58D4221; Thu, 20 Apr 2023 11:51:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IiTMS8X7p/8jNlFAmSJAaOK68lgN67gTwxU0zL59m81MDcDeHSDE92+WGXe3kl3frtPl21qBSiFwzQnNeVwJuudwGGqHTIO8+FUXVa0MFy0YNvpCaUe+wTg5qh2SSZRsqk4k7Dm9piIzlHnmVr84UtAH1/pIgh5erI9jA4eXduQQRLrzxE9hRni2aOpKV13XkTdP0UWx4M+5pZo4whDJKVMBciV5AUx+RoTp7otAQkHua19x8Cpi11PvBvi5txd9OyWU0kR//YLLgw46xes5ls/5OS/Al3mxtTsG9h7BnXgDwLxPHD8Mkcd5ZhwRiAQgeiicdgnmnwyNVpOwxulH0A== 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=RFhH+VMaXLDWwRmCU+TbUC9YfLiad4RolEiHONxioxs=; b=XQb6toU69FOnpUFa6bnTYeI0sQcP5aMrt3XlnKL5dY6A3w5J9OhQbX7wO0a7syqtJUuAgZZJ9XChaYHdwnvQ0Bve/zyIAmAtLd68kCfVtkbGTpOzCWvhoZFe425y/mZt1hcqMhgteiS2IgQyasRd4bcerERQLf1Vg8GGMVhuKvoaJtMkXgY/a4Y7BvqD7amAXuNrpR3g1ttl7MkR5KfdHjoHiURdj5g0O52VW+B/ubZs6pd67yvWsabddkD8lTbPjtvYQj5qzTa28fquUI+JoMC1jJc6wPzC+sWp8ss5yK+kfkikUfH2EusmuMk8vAcMGWyDjtrq94pHApaQLq/g4g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=RFhH+VMaXLDWwRmCU+TbUC9YfLiad4RolEiHONxioxs=; b=ggzITyXO+BLupjCFHB9ZUbpdzqS9+ahdyvHbCh4njNNF6Zp+s9tzrPjIMBOZRvq8OBK1h65c6BsjjTSfJp7ZRy3gxSeoJ6JGeQrN1GgpkjUbn3zl0CQ74//r0y/gtyAyMlyFSDICcxb2lxF0/gp2FqaLsEzBqR+SwXk7rdiwLp1kcZW7C3LeVUm6E/sMwRGRI2UxEBm5hDkD6FjcDBEx5/hjT068hVemn9wf1cYYu7lcwN8mfT0f9TOlyZ0gA/iHo1BWe73kqC2sVP4Ni2eq4AIDtsmjhIHNcDu6tM49UjvIMztlFb8ysqcbvZsQwxC73HhMiYJsa5oEPpyxVi1rEA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:44 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:44 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 13/19] iommu: Introduce a new iommu_group_replace_domain() API Date: Thu, 20 Apr 2023 15:51:28 -0300 Message-Id: <13-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR03CA0106.namprd03.prod.outlook.com (2603:10b6:208:32a::21) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: bc40397b-192a-4f61-5ac1-08db41d04565 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: iNsXK6SrHYqgeugEoX9dvxXM3cOacwcv62RO4X9P3TBq4WL/2tIp37bFiGQVpDalJlaU/pH45v6CKi5jmRa+M9LutsDtYcuFCZ3ekphOfhZpQ2DwscZ49DrawEgTPVpENHdxod/MutPn6zUP/oYa9MkWwbYT6Nwi0jpi2CP2ikWTY2ImESOVeE1wIP6fviCJPmD6bXngYVc4sgCEpSfLtRKkJNSoj6I91l+0DqsV1JyPRVQY5CRS91YnySmYBe99MuOv0A7pmKbHe4bZc7fraIyVvLIqJ2iTUeJ90dzh2mTu38i+YYYzVB+WbevxHDVZA9Geh6s32L2N8EHjhXh+jAgwm21cp+XQW9qXuC89pXAxUfhnZ6eQKbeH0ULUTRGVxbXRFSkzATDdifl0aXhKvxedxXbAiP7nmZ5CdKv8c24Hz/fLKYnCvFNJXfjoqL12GW9npbzJXsTwF2yAYdUyelH6w1HAsnBT3DWnkBNNDAhW5lh6oyLZnQSSnMyi/c9T1I8Nkk7rwFEyB6dDRE6rfI//gI9mhjs6gh/DJ86Jue1HQMY6oVTkHjDoTWIdd1Xp X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: lSbjo3k7QrPwgWTyzWWxBEIXhtf5zvXajdyYMwSc5hs1JpaqnS2+Jdy1tJgYUdF6h5riKy+e+pQf141LGkff7xCgI252suOIBjc7HNNqQW4rsMHCF7Hn0tP/cmgpzyCI3KJmgQMYyI06BaKwzWkzdlBIv39w02VWXB2kzd6BdhXCpzamCsijljXUOhJqWFBZpAaJzvqVFLSULV22lN1X/BC9kqbU2dLszYNpWljHwzonij6mQomTKEjOKzavHH6UDB46Ne1sY8Qtk3SFfT5mADpA7Itji7m9ZIdaiq6t+46xDkbMsXdMvL7m7TKJM2az4tKUtXPW+Ur/XD3l8zLavJ4kQb37RuHQelRsOqc1wZJuV1y8bbkhJ6tnf1FstMJhT/AxSiaakYUZCOT9vOqD3r61NbgJclQI1Dl81a8BIx8KNYQxbsh7eXkusz7TE53Hnzo3+ZCaYOYuFleDOrNbLt1dZUooFXpuElYzE5twGCKOMKP/Jh/gdEEzGiH7j5zikayBqFITGTOOpIGgbcA27mHx3lF67YfaxoKZS/ZbkQXM04HMVIga+VzfiCW47wxLnxLKTg8EbTezOHAZkpMTikUpC2RpEVr/8ahkvjkhiSQse01stopfIhVom+sdSx22YsQ6wBuTOgh1v7XOc8UEuw3javasC78Hc3CMxZVErlGX+reKFVRdv1JAvPT3BZoWkQN98ckQtNbwdjDq7rSLA4atTT4RxtENdrQyHb5YMMaG10zTFlIGssSqLylBr1PNMAksk6bR/Sfsnh1FvcqoH6/ZQzCSnsZl+CMe0LdkrzppVEZlSpBw3+XE5y1NLl0pXX2YYcsHfSSD7qAJve67TW7gz5UP1EjelRGDQUlLCYVVvNbq7/CLMlnwsDCxPWVg5uErRCtbByrYTYngls3Ey16cLdN32e4rp+ypoXDt4veHBTDcLaiTcodLZJ14uZcHV9pVVsMlN0K6Ye3UzUkZl3KNJb2zuN5kYc4nAZm+t5VVyIVh5wg9rxaiwA/ikVgjOlf/WhZtKrEOTxqxohE31kLA6/tWwJKSaQ6zoFRc6k3zly+uzgXxm8tC7BRwyOzldbx82gtKdtcmX+2OxTRZ6u06o4nDPdIJRaWso8rNuqPPqoIeMfemnNL+IVe2IcnNl/I3lhupwkZLucuuYhZsISckpyYLvCe/il822R1zmS2Sv+4xOegv+Iv7mc7hL44ALHl/JM9eHh0XPmdBDd08hMZZh6uDmIxLnIGOaD8zQMR6NzI5wJwNZvYQkMfF91jNO41wCBkwM8M4YCR4acUEx6v15GoG7YhdWqs6FKDX8WfSbS/c0SnhG/KiKMQ+ovYXPc46bNnrnUPqGAVtwNnsA4eqloSIgZ/y+dN+HOmPVrByisRPM0jfLj11CbdDBhae+QitNScheKsiJle5YQtQU1vV2Ih8U7y0v9xtFQT6tNyRcZvW2ESrYsCn+FkzkKCCbtQnsm8u0KmQCWOipkZga1WOBkhTKUO8P400aXN2dXvJFU3z2Z7V/j7zK+Gcc/UPULebx5GwjFwie5JEPXco51n0eee1iRXAM0mPRvMR93dPh/VEmf0HGtELiieSQlMo X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: bc40397b-192a-4f61-5ac1-08db41d04565 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:37.8398 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: jntZfU23zc8DbbLi5Fry0yO7hvgjW29EQHNFwAwecqKBTKMVpsX4vIsxXILfMsqn X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Nicolin Chen qemu has a need to replace the translations associated with a domain when the guest does large-scale operations like switching between an IDENTITY domain and, say, dma-iommu.c. Currently, it does this by replacing all the mappings in a single domain, but this is very inefficient and means that domains have to be per-device rather than per-translation. Provide a high-level API to allow replacements of one domain with another. This is similar to a detach/attach cycle except it doesn't force the group to go to the blocking domain in-between. By removing this forced blocking domain the iommu driver has the opportunity to implement a non-disruptive replacement of the domain to the greatest extent its hardware allows. This allows the qemu emulation of the vIOMMU to be more complete, as real hardware often has a non-distruptive replacement capability. It could be possible to address this by simply removing the protection from the iommu_attach_group(), but it is not so clear if that is safe for the few users. Thus, add a new API to serve this new purpose. All drivers are already required to support changing between active UNMANAGED domains when using their attach_dev ops. This API is expected to be used only by IOMMUFD, so add to the iommu-priv header and mark it as IOMMUFD_INTERNAL. Suggested-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Lu Baolu Signed-off-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommu-priv.h | 10 ++++++++++ drivers/iommu/iommu.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 drivers/iommu/iommu-priv.h diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h new file mode 100644 index 00000000000000..7c8011bfd15374 --- /dev/null +++ b/drivers/iommu/iommu-priv.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __LINUX_IOMMU_PRIV_H +#define __LINUX_IOMMU_PRIV_H + +#include + +int iommu_group_replace_domain(struct iommu_group *group, + struct iommu_domain *new_domain); + +#endif /* __LINUX_IOMMU_PRIV_H */ diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 76969904b93af4..91a948a76db5ee 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -33,6 +33,7 @@ #include #include "dma-iommu.h" +#include "iommu-priv.h" #include "iommu-sva.h" @@ -2191,6 +2192,35 @@ int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) } EXPORT_SYMBOL_GPL(iommu_attach_group); +/** + * iommu_group_replace_domain - replace the domain that a group is attached to + * @new_domain: new IOMMU domain to replace with + * @group: IOMMU group that will be attached to the new domain + * + * This API allows the group to switch domains without being forced to go to + * the blocking domain in-between. + * + * If the currently attached domain is a core domain (e.g. a default_domain), + * it will act just like the iommu_attach_group(). + */ +int iommu_group_replace_domain(struct iommu_group *group, + struct iommu_domain *new_domain) +{ + int ret; + + if (!new_domain) + return -EINVAL; + + mutex_lock(&group->mutex); + ret = __iommu_group_set_domain(group, new_domain); + if (ret) + __iommu_group_for_each_dev(group, group->domain, + iommu_group_do_attach_device); + mutex_unlock(&group->mutex); + return ret; +} +EXPORT_SYMBOL_NS_GPL(iommu_group_replace_domain, IOMMUFD_INTERNAL); + static int iommu_group_do_set_platform_dma(struct device *dev, void *data) { const struct iommu_ops *ops = dev_iommu_ops(dev); From patchwork Thu Apr 20 18:51:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219097 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 56F2DC77B72 for ; Thu, 20 Apr 2023 18:52:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231869AbjDTSvw (ORCPT ); Thu, 20 Apr 2023 14:51:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231861AbjDTSvu (ORCPT ); Thu, 20 Apr 2023 14:51:50 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E5204217; Thu, 20 Apr 2023 11:51:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G6q80xCFGkLfSihQcV2+//UwBktl5rTHNj/K0XN2DU413oCxwfwgyuZlF5rsjvqEM5WVryBcdgb/bWi+RfQtTYdmhwA6bSkPH/pEnh4vmlKfARfoiWshG7zMLwHwzm+flVad7sRC0+UH+++Rbv1xmaPLflZdKX1nHdc8kfwkD0hbE1V8RpHKrfUhG2lI2+XjOcOhRRILkK4KQ6o+/1UEeGRwWwxRi6XiC3qeTAkGtzCMJU3WAN3NLhCmYF2KRmSStq0gv+J/CcbyOr7qXdYI3iiHQUDMUEVJVNLOmFRDlR8UZUaqNwX5X87j7/AwZRCto/M1CjL3KW1BvXWf7T65Dg== 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=SF5KYnt7bCj6mKudlspF9E+rYoi+Iwzl/TPDpr4j24o=; b=h+mAl5zQRrisul5+ZdQ0NrjHMg73F2X24m+bpxq+BLPqbR7ZnMvjsfkR4w81x2F3RBHv6VgN7fk884ULxUaBLQpDbYw/EfAQWUbLaOmmJHrwHP7VAgAXG755wKFyr4dfZ4E6XhuqmKBsuzN7I1LMPvS5QagvdH/vMp8s0gHNnqPdSqA4f+oFHFIpH15E7iLXr+i24bEu7N8pEsdDpuzRPA5/QxAv3CUUWpi/0usncazlg9sNGs0VXtH+dS7Ztj+LMdbCLKzI50NqJrhxkqAomNiFFOIp8UslB+/0UwDJqihVA8pYJPj55M8XPP1pAA0e6kxHqggUJUHpFHuTK9dhJw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=SF5KYnt7bCj6mKudlspF9E+rYoi+Iwzl/TPDpr4j24o=; b=DgX+JZFLV17PNO8KfVp/OPcvsgx9e0yT0M7Z2cGVq4ReCGdY5GbNIMQfqbNqXSCmq4FIWNAdTq/2k1IBPYvLiJBWUEWvi0jF6Wd8gPiK+LWTUh+aEDaCR03D2Bhxtjw0zuZDagThdQyYYvyMmMiqAmnHF9L5HxRRs1sEgE7hr6QNTvtGYdsFQO0bQHy/IdUWsNUoSyKkfhv2IOBQMcKXy1hm1Mv9ovyl+KbCUoTVLV4yzka20LcJG5rQCtpqB0IHtj1nIC3/nDMGNhGWszves/Jx/tVfNGmwvAVRUSnM5wik+GeZdcZl7cTyzjDgigInw9gkoqggnTvAnBJT97dwvA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:43 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:43 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 14/19] iommufd: Add iommufd_device_replace() Date: Thu, 20 Apr 2023 15:51:29 -0300 Message-Id: <14-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0349.namprd13.prod.outlook.com (2603:10b6:208:2c6::24) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: df914123-8db0-446b-9636-08db41d04560 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FY2AMtl8gtqQgkxlLYvEr6udxD8869+tZZeC3nAH2MjvKfB8jEAJ6/Y4m00HVVYcwyXCbgHLzq+PPJAaVTjZAfcKNi+HVVmAORxVoO5e+ndN8cjFYWde04EnrokPmnlFo8LiaEpRdKVpWH5QANSmuG3HiVHEvdVasABfP0n6QgMY4g/eTgsmMj71i/6eb1aBj64adiyP+QyrUMOV55drxSxxyctfcbXsaULEALa7LC4PCeM+C/n/Ifmu5FIUpAup+26EhX2M1NxwvSdsgkKAleRguTEADpvZnSPmp1o79rs+VNRaCKSaVIt87h4BPNHpushlLz46ECpAyl4lFXt/7pijnM6uUzoenNcpVRknXnWi32VNPg6mk9n7gJ4h8Db93cZpZ38A15EaFE46fhRlWrI+8I4MfgdHByP21JnsecYGB9vxYaxpVFP39xm1mqpQzBzErfK2MtjME/HilA2/JZTIek5hHhPav39A7sR48be3obTcWYdeLDFtxStdkrUC1gHem0y6xBbPjdNX9h38J5FRmiwHBKM9QBjcBJQP66QpdaKkvb0Is3fsvc8vVwpX X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: +88e0oCOEgvBW5tcjJvDRGPoVZXOI8RZDqlXdcfxSbKSnjkJdnQqbjfCA4EijFmYPIFHUbV2+vIfXZ0C4OvbwZTOv06qMOyh+ykTapwWlPZKbe1aDJG8+Hu+J/ELPXLzvi5X++l7NmVAdTzR3cRzuXhCMLM7e0vamjHDQzcGWXvixbUBagZB8Lm03S+cdKS1wDwAnBQaEDGJp9mWIm4Hzdl+TlqvxOeEYMV906oqw1GXSUBxsJ4J9AytIO9/BLv3aIdXszkIN4fadtIFr2+t1kxt0LHxvwBZg2dGr/h73+zWTGz0DxD0HvjSYO3Z7k6i9jytL3MyoRkydfwcmn1IZJo+FpHtIGtGH6+DhdJCoIRjQwyHI3qbg3O9k1KVNpk20DqBKI+2/S0+v+6YZLJPecvb8fUNkbxWC3N6gDZRYpA1BnSXESUm5FjYjcbm3nU1rXEo1TlfWVHtlrHvBdyZN1VeODlNzhV/7/jzJRPJfJdH/Jky9krKb3wrCNN3kPICY462CpAtqiCL10561z30lvBQsvYVYRMEhOo93PKXnc7FVkT7LWgHMCCnOgqrYAK4N/qHRjia7AxOyDpUaJgdQrivEMz676q1UzY/x3KgfLrQY/JOkV0n7ipbrUQJqI/SFa2Y37/pkY3BN6tAJ5j2CDHcwA0govIJLqM94U45+QL/txtiFzMxlz/KL1RkeYafO5jqPu1RyP9KP3ZdpklMA/UOQXYcbUHJRuMrJknNQHNvE4K1EDZQao2hVmoY1KWdFB+HlKIbagPfckj+KuVrlnO1AdDt83R8aT6lp7AILUG4CIkH5OqiLMdlfThrcXy6mMw9YumzaCj6z4FKzguLRjvclS70aSrZ1P+utc12A4Zhr7wqzS9cWj9EE2Qlcc78GuJL4rabm67wpiqdFt9YnxebEkK5Evr9pfh326XE/Fc1fxl5GB1coowl4lpGz/t5dIGCEIJFZ8SK4o2Hv0h00mXNizXXfxfDhZuHWW3/RBfJbez4E+b2jYmapn4LboN6q2xsYut3HkPdQh7siOp3mvk6X6l/EpGNYNxKQhThiSKiu4fSg4vklaG9fomCBXiTznOtkZsAJSufou2JC8TkH0R+CgeAA5DWtcpla1b9vet9Y05pktXBBLy9F3nzYfArSSBOwg5tH4/9H9HCVoknoqjg9pzrAPq1tGps8vrhQhvRfrrbmvQFvnMtvl9zEx/uno6ghX/s+0NSD5ECWrSGM7j843vNZb9MttvTUslMS9P0kW7BpF2GUdublXHThAiAYvIbqOO1V2F5wETMT9BQuXQ982w8Q1HCRoH/G99PIl3t83UNRHzNRdweGZCAV6gZQFBm3tWP4AQBfbCMxK5wJmiitZLCsQjQBKN21iDXM5aCAQe+5N4zicAwHSTR9XRFn/yUjLTWwPeqXvFFnzJXkpMzKAWRxHFwLArEvE3JwGZvEF9/qev2QBnUE6x0XuFP0gLqGlSE22nOsU9G2BmSKvA9UysnmbDZuCsY8dlhX+n3KJ/Ys0/nCILCH4j5eJM5kFFK4XRnw82/wV9NAI5eIn8F+Txgvwc/yYwrzbbVVMqdoRipC/1sSBMsPHkiEdtz X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: df914123-8db0-446b-9636-08db41d04560 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:37.8042 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Vznd5e4K/ahOa4fJ7DZ1Epd4pkmMaILcSSH/ojdSrSGyT4HYYzUVozd3sGtOrRPJ X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Replace allows all the devices in a group to move in one step to a new HWPT. Further, the HWPT move is done without going through a blocking domain so that the IOMMU driver can implement some level of non-distruption to ongoing DMA if that has meaning for it (eg for future special driver domains) Replace uses a lot of the same logic as normal attach, except the actual domain change over has different restrictions, and we are careful to sequence things so that failure is going to leave everything the way it was, and not get trapped in a blocking domain or something if there is ENOMEM. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 99 ++++++++++++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 1 + 2 files changed, 100 insertions(+) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 9d61a62c4be067..2770087059ba73 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -4,6 +4,7 @@ #include #include #include +#include "../iommu-priv.h" #include "io_pagetable.h" #include "iommufd_private.h" @@ -365,6 +366,84 @@ iommufd_device_do_attach(struct iommufd_device *idev, return NULL; } +static struct iommufd_hw_pagetable * +iommufd_device_do_replace(struct iommufd_device *idev, + struct iommufd_hw_pagetable *hwpt) +{ + struct iommufd_group *igroup = idev->igroup; + struct iommufd_hw_pagetable *old_hwpt; + unsigned int num_devices = 0; + struct iommufd_device *cur; + int rc; + + mutex_lock(&idev->igroup->lock); + + if (igroup->hwpt == NULL) { + rc = -EINVAL; + goto err_unlock; + } + + if (hwpt == igroup->hwpt) { + mutex_unlock(&idev->igroup->lock); + return NULL; + } + + /* Try to upgrade the domain we have */ + list_for_each_entry(cur, &igroup->device_list, group_item) { + num_devices++; + if (cur->enforce_cache_coherency) { + rc = iommufd_hw_pagetable_enforce_cc(hwpt); + if (rc) + goto err_unlock; + } + } + + old_hwpt = igroup->hwpt; + if (hwpt->ioas != old_hwpt->ioas) { + list_for_each_entry(cur, &igroup->device_list, group_item) { + rc = iopt_table_enforce_dev_resv_regions( + &hwpt->ioas->iopt, cur->dev, NULL); + if (rc) + goto err_unresv; + } + } + + rc = iommufd_group_setup_msi(idev->igroup, hwpt); + if (rc) + goto err_unresv; + + rc = iommu_group_replace_domain(igroup->group, hwpt->domain); + if (rc) + goto err_unresv; + + if (hwpt->ioas != old_hwpt->ioas) { + list_for_each_entry(cur, &igroup->device_list, group_item) + iopt_remove_reserved_iova(&old_hwpt->ioas->iopt, + cur->dev); + } + + igroup->hwpt = hwpt; + + /* + * Move the refcounts held by the device_list to the new hwpt. Retain a + * refcount for this thread as the caller will free it. + */ + refcount_add(num_devices, &hwpt->obj.users); + if (num_devices > 1) + WARN_ON(refcount_sub_and_test(num_devices - 1, + &old_hwpt->obj.users)); + mutex_unlock(&idev->igroup->lock); + + /* Caller must destroy old_hwpt */ + return old_hwpt; +err_unresv: + list_for_each_entry(cur, &igroup->device_list, group_item) + iopt_remove_reserved_iova(&hwpt->ioas->iopt, cur->dev); +err_unlock: + mutex_unlock(&idev->igroup->lock); + return ERR_PTR(rc); +} + typedef struct iommufd_hw_pagetable *(*attach_fn)( struct iommufd_device *idev, struct iommufd_hw_pagetable *hwpt); @@ -523,6 +602,26 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) } EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, IOMMUFD); +/** + * iommufd_device_replace - Change the device's iommu_domain + * @idev: device to change + * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HW_PAGETABLE + * Output the IOMMUFD_OBJ_HW_PAGETABLE ID + * + * This is the same as + * iommufd_device_detach(); + * iommufd_device_attach(); + * If it fails then no change is made to the attachment. The iommu driver may + * implement this so there is no disruption in translation. This can only be + * called if iommufd_device_attach() has already succeeded. + */ +int iommufd_device_replace(struct iommufd_device *idev, u32 *pt_id) +{ + return iommufd_device_change_pt(idev, pt_id, + &iommufd_device_do_replace); +} +EXPORT_SYMBOL_NS_GPL(iommufd_device_replace, IOMMUFD); + /** * iommufd_device_detach - Disconnect a device to an iommu_domain * @idev: device to detach diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 24f30f384df6f9..5b7f70543adb24 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -466,5 +466,6 @@ module_exit(iommufd_exit); MODULE_ALIAS_MISCDEV(VFIO_MINOR); MODULE_ALIAS("devname:vfio/vfio"); #endif +MODULE_IMPORT_NS(IOMMUFD_INTERNAL); MODULE_DESCRIPTION("I/O Address Space Management for passthrough devices"); MODULE_LICENSE("GPL"); From patchwork Thu Apr 20 18:51:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219092 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 EA7C0C77B73 for ; Thu, 20 Apr 2023 18:51:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231811AbjDTSvs (ORCPT ); Thu, 20 Apr 2023 14:51:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231704AbjDTSvr (ORCPT ); Thu, 20 Apr 2023 14:51:47 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9D5B3C2D; Thu, 20 Apr 2023 11:51:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GJMuDBfnWCpPtDCq8YvuOouw2vhzCU2LU3YM4yH48N02W2H3E2OsdmU7GVmhGAjZf0gACBVlvDXuwpSc5mZjsyEBQlYpzxhMz2qMab8IaIj5GNXLsXiV4n4IonvVWZBFOSG1OqmhZllddYlKU0pHtmOZ2/4lHsjHMZ2+HMUQdBShh7FGi/6GIVxMntcNH69Y+xkNf0S+txtZO3S6pXDeBjk9At43bAtmVh+eT7cJmwZfQa+Gt8RdS7U1TcdYB8yAftwrduFxS4+SiI9n5HaVXRy/QFS7PVWj6XEIa0YQ/epyoGWgZUTO+uJ8jfepFR7IepFd3KC1jRlnma88Qi5lYg== 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=VoCOaFExvw3O5LLUrcIrAH+TF+1XtgS69/Ev4knJS64=; b=HJqG/1uukBH2I6qpHIrE25R6kr2QUAil9g7gOtlrxaS4M+nNs3gdH6o88aE/0N5P9oPB8WD65ac3RmVzOULc9QHrYwY00CC4H/pEWP5Wkj04pqo45c8JYmUwKhhI5Gh4Sq/MjynFl+mQhaybZk+t1/PRT0CBjaKNO1/SungJBQ4Gi4PkZ07ncCEuMFu/9tQLFnYlkhqTWTcsb/OzPzrJWs7q8Se5OpUwSdTFlTrwvQ7rDb/fix5Icv7DGH/s//affkODu8GpdLdmCzWtqMEYY1JrWprM3Pfw0GHB+4bgIQhzLp/nvbF95vSvCR7s417kVyQKWVjlPJi6Isor6M1IYw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=VoCOaFExvw3O5LLUrcIrAH+TF+1XtgS69/Ev4knJS64=; b=p5Ii+cVpF7FX2HEI1k/btGy43Aru6Jr0FPgAh6hKOqbDzND5rcadCefuTOUflpgY9Lju34mVK/DH2BkXd8ORom+m39l2KHI62phVdNsaWuaUGy9r5sbMy085UDWLQbG3Bvtx0fyJjrN/3gMqV8CXI9hrLklGaSuv65aGfybz5zTHoQqmSJ1+GndrQSYI/z8cWwOPeEu8qx3SxRL6TPn97XdKIavJCsDSEd4ccRhpqN1oX+F3tAcoZXTwS/KnzZ1kDg0bEnLx0yRYrfEVyeLsmENx2+Y4Oa7ugsOvAdGnOOW7vX3svftfk/HNADNvBHNQIrrLk9xIBZpUdaTZM7KXQQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:40 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:40 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 15/19] iommufd: Make destroy_rwsem use a lock class per object type Date: Thu, 20 Apr 2023 15:51:30 -0300 Message-Id: <15-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR03CA0091.namprd03.prod.outlook.com (2603:10b6:208:32a::6) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: 3d1b3541-8c06-4c5f-ef76-08db41d044f2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yEjknz1IH5LxE1CDlv2nXfKntLlWwO7Dt1ix0qDQegxN/EFCn26qUFsUW+jfNcDx4lAUgJ0zZPF4yzdTW57Qk8pcprIvCan3oq0n8Yq3vKlXWMRLvf1h3nufqtWGvWXlq+eoZLbim/o43HUHoZo/6SSo4DcNRv5JpdtcYvmRPe99W1Cenu3vJ7QQ5/JBshcb5Il2mBRICYhuQY0nSg+AuBY9TkOFyE9pzJRdPMeZ3xZhGcKH1hgOBfwrWJp4hAuFWkPmzYl/HEazNBe4S4tQIpoddk3ODKS2iO3+NmUWn4EG22dRpQAqLTr6PtiVQqgGt3iEk/s12kpKLIEtDnuYHrfo9D+eSrE2EOqAieqcMhzhGEMc4w45xwp3UnRLbaGFkcQPM0xTYYyH5BE1gFfKHByCPibhuJ8jMA/LyOIWWqvDMa+zo9ZAlogqZXSdqqEY3ssJshYJ+iVltWOUcqDaUNDKnfS6r9NDFfMBYpUnBroh5p1Lp5IZozxp4gH6m53Uey5aEg2n52aSTf5YApmrDsfFslz1D00zyad+rCg5HHatjnz6LwPQEoQ1OJwwPdHH X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: YA7NqeoAe1KNAG1EqLkdfTdvp6+yteDQG0+mfENZQw9YOzxRdkNQJmeLUDD4NCItfXGLq2x1dnflM7qHeqlOh/zmozj6slStKppECXK928h171PsS+GiLv3d1j2IvzlciJxuIuslq5riEi9HVgzWFP7qNEdNvAavuwLWC9pZtkUsp7guxL7BpNUiJLG4XPYrGKLTSpiNG9Km9OfZnUU8lSD2blBPtcpsmZp6H9Y3RNLBEtybIrME8onMQyjKJlC2gYLXilvd4djPQG88hWZr/uzCUKWZv6wYMSEyfcUXkm7//ygX2XrdVmW83TbF2lrbtHkfCB0fqptgE+HynwjEz/Zt8Uy00CKO9KHGzp80/pCNUh0WyorDnKQFSl8hu96Jrdp9SaA4SFcWcrwwMLWXb2gpgN7DsHZDiezllXB3S3/NjVJNZYM8w3b8rexuGFySA51oBCNfGgOnFLXKcXy014+XMyg/80UI3GMwjxGy0K8oPiz1h2HDmwgY2Ww0gfzAewEAO9f3HK38kblknuzU9nd3I2+UXGdYM8msTY6IqV3GIXAEkj4vAMNx+hr67synvUQ9cyHblVIU1FxYwjZqzmdYNEE+GRA4mny99R8ATiUpWPBlK889qmgMqAK7MG43s7GrJmeFJqbnz1gkLTRmW175gixesCcUEh0iXbxo9qzqyTyODOsWONAiBOF3Aak8czufRwxjW3FY2UD+4O6FaC/mAfjg8ZU3Jn6PMMPxqw0UBLHonN+t58EMds/u4ZcfKSboYWVE/3GnmbOGlhQJnqHlNLSTN/GHXH5rvLdj9K07AIXlvX669xyLEEDPcDkRLyHOyn80+jHIE8vUDR/43tNLhdrIEWnp0gxHgaEHHgJXb+PLaeUW3TwdkF/PsJMLQ17RSp6YiwYZ624k8OPEOYIoSw0iDMKoI9BB05ezjThUWuyYBS022qjtHljDWWp4u9zRRljPj89QzYRF/LWSHHHE29c2oHRiqG6v8yTCtOkoBptBTsG0BYjbT1HBmzhOtVZwWPLim5wVbXlCRLw1R84Z1zqLWJe06zwXG14Di12eJKm8UGUN9oGPOjIStcOC0yyGRhH0o/9ZfvVTaWWcw9axYzyY6UcAqSZXMFsSkhWL+MDhAMVUvDbBj95+g7SzBkhe38TSrOB7VZ5b1bmCM5Y7iwrQoqL+KSF1V1CeCi2GvTPFYBz4L06L97pPD80TXuPj+dIeDPGB79DkjWbpMAi6mnk+/6oHKttAhJt+HnCcyH2Lie0myeAtt1PcYaWdKVHmTnUAvukkjeoGug4w1iAt4IXpcSBPYKfQ1+tuOfdS9TvK3eOhd5h+1m3+sNtADtbct2S8Yxy7fKFWeklCj2VwHL5JJdT5iLW4q8R/qK2VqUDJM6b/CPsN9mor6wEqrRLStapQYf+0kQEvBuNEyBkMYkWuW6hDiDrTj5AfWjsMio3gU1Rja+t/k5iYqie4Dyr0jBd0qQrPVHI3etJ7cNERYVUoIgA9kTqOH9m8zDe+iYORfPBaXfLE/ZreXtKqYUuL1LxW5IoXc8b+tL75iWDyAvTjZ39ccCOWF4e9Z+InJWe5y0O5ow3z9F6suiQc X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3d1b3541-8c06-4c5f-ef76-08db41d044f2 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:37.0414 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 9tokXZN0SvTw0UJgTpr3RSGFoKr+6AVTVpZVA7lZrdBZyoA6JXBBDcVctE1i2JOc X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The selftest invokes things like replace under the object lock of its idev which protects the idev in a similar way to a real user. Unfortunately this triggers lockdep. A lock class per type will solve the problem. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/main.c | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index a5d5a47641346f..d97bac38b7a41d 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -119,6 +119,7 @@ enum iommufd_object_type { #ifdef CONFIG_IOMMUFD_TEST IOMMUFD_OBJ_SELFTEST, #endif + IOMMUFD_OBJ_MAX, }; /* Base struct for all objects with a userspace ID handle. */ diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 5b7f70543adb24..f219e47d882525 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -33,6 +33,7 @@ struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, size_t size, enum iommufd_object_type type) { + static struct lock_class_key obj_keys[IOMMUFD_OBJ_MAX]; struct iommufd_object *obj; int rc; @@ -40,7 +41,15 @@ struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, if (!obj) return ERR_PTR(-ENOMEM); obj->type = type; - init_rwsem(&obj->destroy_rwsem); + /* + * In most cases the destroy_rwsem is obtained with try so it doesn't + * interact with lockdep, however on destroy we have to sleep. This + * means if we have to destroy an object while holding a get on another + * object it triggers lockdep. Using one locking class per object type + * is a simple and reasonable way to avoid this. + */ + __init_rwsem(&obj->destroy_rwsem, "iommufd_object::destroy_rwsem", + &obj_keys[type]); refcount_set(&obj->users, 1); /* From patchwork Thu Apr 20 18:51:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219104 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 D1340C7EE21 for ; Thu, 20 Apr 2023 18:52:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231819AbjDTSw1 (ORCPT ); Thu, 20 Apr 2023 14:52:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231975AbjDTSv7 (ORCPT ); Thu, 20 Apr 2023 14:51:59 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 321AA3C2D; Thu, 20 Apr 2023 11:51:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KuJWuzcvHe8/E9DtBthWWptQWuCqy6v5aQ6IBchQZ+buOPH4czVyvUIzyT4BOhGGJhReXrgxq+27oi4AlJlIn1ouT7dB+D4A1tzAgSOYppHQW33kgzvKCnJdB9E0PTn3/7/qzxJMvoW3ymc2/BMDjmxjnHLgCB0YUJngGi6zFfLYPuAoOtDpmW7FcleSXMfD8t+BB14vmYGVnPtw7eSvlXnvA7eksDS+V1X8rBnFsszeu6OfY4YQQoiY6Du/1bRk19NkX2N247/vF119uYEfPMZndrmovtgE154M3X2dyfFxI3TDBvQsB77pbU7rhWtFwLBu8UhuS+UD2C5+TRHKBg== 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=1ghKHbRE07T4XrpixzlOMsV4FCPz3qkBGoHREdGoGiI=; b=G5korABzuLiIshOplo2j/8M40/qDR6uNE/nsgd3ZnSucITNE3vAW4oVlcaCGTTg47DAnu6m11B80wwaLyLIU9kRFznbOLBZxM8SXCi2wkgjeSNLlcxt7dKd8s5gar7DaetR1ozh4Sp0ixAqM1NsEaBHs/cIZBRYF9eAu5ef8sFvI4sKor4kh4Uj2z07N3QR5gOF7/i2BcPyBIKgQcqDj8A/grdxcOyxTzz4bCtyrfJUszGqRFQV6/c9jUsD+ZchemzJTCsmtVCkDcFqzQzCgNhqiu5KYYBhiXrja4j1cyM8KhmY3lrlqaMq4LHdMA9a8Acd/UoYlhJWOp8PjH466Lg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=1ghKHbRE07T4XrpixzlOMsV4FCPz3qkBGoHREdGoGiI=; b=k6VJeqH2f1g5YNCfZOiQGdlnCI9BvZZkgWRrvEYHD+iaAwFvzBzegvWoVz7Nprsp5jtjDUCbmSaxGvKtXljRl/47ywvwdKuTWFd3KCdUHLS30fb9grySPG8Oc0U+Vh+yk1WSljKb5LUnp+tG9HlViPlLCTGW+lcfn1mpcrLvMAC+diCRBZb7AOjQrrccyJwLZOSHJj2Jovy88EYTgley38TuMC3/fbnO7PAfB5aZ8MCXqP4OfC/uLAJ76bwd8KF2axx1obIY1SLgEkvNf3unS4BMYIKEpBMrVy73Qt31tJN/JD9sqQRUQ4Qk+3EksV41pT5doNGkyMjaMCngucgAMA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:48 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:47 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 16/19] iommufd/selftest: Test iommufd_device_replace() Date: Thu, 20 Apr 2023 15:51:31 -0300 Message-Id: <16-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR03CA0006.namprd03.prod.outlook.com (2603:10b6:208:2d::19) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: eaf3cbda-16d7-4f90-3447-08db41d0462b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yFFijJLxIPetan91cBl7au8tj8dyfFjirjuL+/XkhOyrZqKlmVyWHKdBXauw0rgEinPE8/gmtrQQEKdvXSSLzusmVca6FYugGmgFA/Eg5cp38hUeRLAOOL54j70HcuYssPeO7nUw5e3i1UNaci2wqEL816potbvdDlMGA8H9whV0GXNDvnbx3LGLcOCUWOkkO+bKOFI0zkk0c2t5plz0tdrQ+xF5s9Ydt96qWnQRVyo0ViU4ukrjtpCfbRFTcjrX7U5Dz6dmo+1wZP/1UBjVxlZ/oKEZdxfNmFPMgkN2lPF37WnZIA3GIB/m2qo55BtIuLx9F65xIpupwg5NdY7Ej+1IbqRSOa/hVXEUJneZV4a45NC8t5V7mBl7SqmDYxbpedVC06DcIlRMHlB7FY9khznWLdRN+VehUl+0nbfdknq1zMeKEto3wi7zfj77yVcrUQFx7947CxKwnbxBG9DI38GPu1WA3c2SzdVqbXgCF84Mv2IDcWnJ6faWS+Z5vSk6MUh6bjMeWxAEx6WYR53OHIuT1PKBBLDvhfrMT0t2QE7Fa1FykVp6wvS/MuaERVaM X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001)(66899021);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: MuCJhKK8EhNjsQvBGXlwHsNZi/chZaeipFQ7XiqeII4K4NjTX+c0HOcfDjTvjN3tZh0zB+eEINyv9GB975Zc/UDzrtY6kkHemaPAIQLO33jBqC1/Z/fTAfJwummZjhnPT5dwqaeijQQto5kU9734AlCjAdC2He8PthacdtAFUyysJVV+0KWWtylD7DOHU47nbSfOsCJiM4dK8TvS9IBqeo2w3Zl5JIeRDctWxfPSJGAUr7sYNnGhpjOYh9FtYmCotz5GgTmnGn7qJxA5NBZYovEwIxeM4cXl6Eilv5Tg+sw/f2p+P/SHG7TeMGXyWgVNsOV467qOnFSbjlb+FxLmcR+7BB5JK+s8ftlJu6m19q2Q/VkP18pnh72hsTJJ4wSY1nuhTkLEkgLZMyh9l8sLwpyM0e9k5bpw9uVnyjvvldzmpSselAXVlATIZ8UzcbK3sPz/U7K3WYOKP+6eetyKH7V0rq8buPW2TnSr4VbTsfoFUVQWAxjjnCQHceVfe7P9E3qCEkioMf0RdfT+4jYfM1pyNDgWN+e8Z4ptOut2WKErM80O1Nkj/1iE2Vm0DMMfmNke/T3f4QlLLLOp4SnUN5GkvaS/F7B3pK7V6jxi1kr2J6XMsqTtpyQAtmkD7m5UrFTqvHjjURjEQ4PEwnd4jKfjq+bQ7M1TELEnbXPvR5HRBG/lJN/hncGslY2PFjEBMd2CJlC2+lAXIapFUIGZfKVR99Iv/GYYX4+clVioHyDe612qlUvhi4bPMuc2+DhlbSu88YlW6FDraD5P4N/Bor6MKTs8V+OxjKKr3vXuK7Mz8MFXLtWbDsEr2kgI3qSxF7g1c0YHBsSzhUPksQyImE20jNlem1RFpjzyBbTRk1F9qCsca0Y1XlDcl9vOawFF1K77ur4mQAcjXDOxOyG1yXjG5b30LBMDzMsDUvmcaWW1BJh33+fHNMU7s15v6YdGw9HtUgyoqrfY1JuVE60nAwkUP5aFGZq2VMwSKSQ8BoxAMCd1jA1LgOZSWo06G9vOg9+HgQqyXym3wSWNkC7n4fEHt/Po6bLbJhtNXzayOvKd9qNPDIxAQ3tYEZhyzWgHIz20XHGk5VrC55s2IzHAebOKGcZrNtab6jd81SJArfZzoqtbTiU5iiivRtGe+Q+rbjQRS9Sz4zW+hTq3pmuITumMPIO41T6rQWbAsZaLayBbVmtFPylvhCn2wGqOgxh8XqArwuRZJYdGrJpO2h0Qy5t98NI2Doj0cl9pLPOcId5nXZjRpGSM0gcRvT3bSVoUCmqGfaNo1+lKToK3c66AMoSGAASYHeCZoXzcIdmrmbHeiHalgNg7EnC5I1y2RB+EUfqKeQ71/RitiUcRF1HQssTNhJJV3/TJRhwwZqTZyWQO6uI7qoYw3cJnmACSldEfnrhzdvnyXwt7Wnr6JNcjOTkSdCpRjCDtv3UAciUPbBgM/wNdpY9+EAarPjba+y6Wq9um8O+jJClFso18gxroNs5teQa6PTaI2s1uSkBw92m4DKoev/ig+mWE4W/Q+4Fhz6dno+pT5vDocAvgXNXk5Iw9MCBHjaTA35tsB86HkjFa+2ljUhtR2f9KLVQqWxj9 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: eaf3cbda-16d7-4f90-3447-08db41d0462b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:39.1223 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: G78Q0wxEg6Ya72+Zd+Qisx9DOuJjQo03JVR/C7B4RYQ/HZBxCHjqYulRoccMkfQf X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Nicolin Chen Allow the selftest to call the function on the mock idev, add some tests to exercise it. Reviewed-by: Kevin Tian Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/iommufd_test.h | 4 ++ drivers/iommu/iommufd/selftest.c | 39 +++++++++++++++++ include/linux/iommufd.h | 1 + tools/testing/selftests/iommu/iommufd.c | 37 ++++++++++++++-- .../selftests/iommu/iommufd_fail_nth.c | 42 +++++++++++++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 30 +++++++++++++ 6 files changed, 149 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index b3d69cca77295c..e3f1035cbd6464 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -17,6 +17,7 @@ enum { IOMMU_TEST_OP_ACCESS_PAGES, IOMMU_TEST_OP_ACCESS_RW, IOMMU_TEST_OP_SET_TEMP_MEMORY_LIMIT, + IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE, }; enum { @@ -52,6 +53,9 @@ struct iommu_test_cmd { __u32 out_stdev_id; __u32 out_hwpt_id; } mock_domain; + struct { + __u32 pt_id; + } mock_domain_replace; struct { __aligned_u64 iova; __aligned_u64 length; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 58471f9452be55..dd792294298ffd 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -455,6 +455,42 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd, return rc; } +/* Replace the mock domain with a manually allocated hw_pagetable */ +static int iommufd_test_mock_domain_replace(struct iommufd_ucmd *ucmd, + unsigned int device_id, u32 pt_id, + struct iommu_test_cmd *cmd) +{ + struct iommufd_object *dev_obj; + struct selftest_obj *sobj; + int rc; + + /* + * Prefer to use the OBJ_SELFTEST because the destroy_rwsem will ensure + * it doesn't race with detach, which is not allowed. + */ + dev_obj = + iommufd_get_object(ucmd->ictx, device_id, IOMMUFD_OBJ_SELFTEST); + if (IS_ERR(dev_obj)) + return PTR_ERR(dev_obj); + + sobj = container_of(dev_obj, struct selftest_obj, obj); + if (sobj->type != TYPE_IDEV) { + rc = -EINVAL; + goto out_dev_obj; + } + + rc = iommufd_device_replace(sobj->idev.idev, &pt_id); + if (rc) + goto out_dev_obj; + + cmd->mock_domain_replace.pt_id = pt_id; + rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); + +out_dev_obj: + iommufd_put_object(dev_obj); + return rc; +} + /* Add an additional reserved IOVA to the IOAS */ static int iommufd_test_add_reserved(struct iommufd_ucmd *ucmd, unsigned int mockpt_id, @@ -944,6 +980,9 @@ int iommufd_test(struct iommufd_ucmd *ucmd) cmd->add_reserved.length); case IOMMU_TEST_OP_MOCK_DOMAIN: return iommufd_test_mock_domain(ucmd, cmd); + case IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE: + return iommufd_test_mock_domain_replace( + ucmd, cmd->id, cmd->mock_domain_replace.pt_id, cmd); case IOMMU_TEST_OP_MD_CHECK_MAP: return iommufd_test_md_check_pa( ucmd, cmd->id, cmd->check_map.iova, diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index c0b5b3ac34f1e0..3044a432a83e22 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -22,6 +22,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, void iommufd_device_unbind(struct iommufd_device *idev); int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id); +int iommufd_device_replace(struct iommufd_device *idev, u32 *pt_id); void iommufd_device_detach(struct iommufd_device *idev); struct iommufd_access_ops { diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index da0443ba16830f..57559f60ca6486 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -9,9 +9,6 @@ #include "iommufd_utils.h" -static void *buffer; - -static unsigned long PAGE_SIZE; static unsigned long HUGEPAGE_SIZE; #define MOCK_PAGE_SIZE (PAGE_SIZE / 2) @@ -1001,6 +998,7 @@ FIXTURE(iommufd_mock_domain) uint32_t ioas_id; uint32_t hwpt_id; uint32_t hwpt_ids[2]; + uint32_t stdev_ids[2]; int mmap_flags; size_t mmap_buf_size; }; @@ -1022,7 +1020,8 @@ FIXTURE_SETUP(iommufd_mock_domain) ASSERT_GE(ARRAY_SIZE(self->hwpt_ids), variant->mock_domains); for (i = 0; i != variant->mock_domains; i++) - test_cmd_mock_domain(self->ioas_id, NULL, &self->hwpt_ids[i]); + test_cmd_mock_domain(self->ioas_id, &self->stdev_ids[i], + &self->hwpt_ids[i]); self->hwpt_id = self->hwpt_ids[0]; self->mmap_flags = MAP_SHARED | MAP_ANONYMOUS; @@ -1274,6 +1273,36 @@ TEST_F(iommufd_mock_domain, user_copy) test_ioctl_destroy(ioas_id); } +TEST_F(iommufd_mock_domain, replace) +{ + uint32_t ioas_id; + + test_ioctl_ioas_alloc(&ioas_id); + + test_cmd_mock_domain_replace(self->stdev_ids[0], ioas_id); + + /* + * Replacing the IOAS causes the prior HWPT to be deallocated, thus we + * should get enoent when we try to use it. + */ + if (variant->mock_domains == 1) + test_err_mock_domain_replace(ENOENT, self->stdev_ids[0], + self->hwpt_ids[0]); + + test_cmd_mock_domain_replace(self->stdev_ids[0], ioas_id); + if (variant->mock_domains >= 2) { + test_cmd_mock_domain_replace(self->stdev_ids[0], + self->hwpt_ids[1]); + test_cmd_mock_domain_replace(self->stdev_ids[0], + self->hwpt_ids[1]); + test_cmd_mock_domain_replace(self->stdev_ids[0], + self->hwpt_ids[0]); + } + + test_cmd_mock_domain_replace(self->stdev_ids[0], self->ioas_id); + test_ioctl_destroy(ioas_id); +} + /* VFIO compatibility IOCTLs */ TEST_F(iommufd, simple_ioctls) diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c index d9afcb23810e1a..96fb2f04623f23 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -41,6 +41,8 @@ static int writeat(int dfd, const char *fn, const char *val) static __attribute__((constructor)) void setup_buffer(void) { + PAGE_SIZE = sysconf(_SC_PAGE_SIZE); + BUFFER_SIZE = 2*1024*1024; buffer = mmap(0, BUFFER_SIZE, PROT_READ | PROT_WRITE, @@ -569,4 +571,44 @@ TEST_FAIL_NTH(basic_fail_nth, access_pin_domain) return 0; } +/* device.c */ +TEST_FAIL_NTH(basic_fail_nth, device) +{ + uint32_t ioas_id; + uint32_t ioas_id2; + uint32_t stdev_id; + __u64 iova; + + self->fd = open("/dev/iommu", O_RDWR); + if (self->fd == -1) + return -1; + + if (_test_ioctl_ioas_alloc(self->fd, &ioas_id)) + return -1; + + if (_test_ioctl_ioas_alloc(self->fd, &ioas_id2)) + return -1; + + iova = MOCK_APERTURE_START; + if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, PAGE_SIZE, &iova, + IOMMU_IOAS_MAP_FIXED_IOVA | + IOMMU_IOAS_MAP_WRITEABLE | + IOMMU_IOAS_MAP_READABLE)) + return -1; + if (_test_ioctl_ioas_map(self->fd, ioas_id2, buffer, PAGE_SIZE, &iova, + IOMMU_IOAS_MAP_FIXED_IOVA | + IOMMU_IOAS_MAP_WRITEABLE | + IOMMU_IOAS_MAP_READABLE)) + return -1; + + fail_nth_enable(); + + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL)) + return -1; + + if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL)) + return -1; + return 0; +} + TEST_HARNESS_MAIN diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 85d6662ef8e867..8b11bb70095269 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -19,6 +19,8 @@ static void *buffer; static unsigned long BUFFER_SIZE; +static unsigned long PAGE_SIZE; + /* * Have the kernel check the refcount on pages. I don't know why a freshly * mmap'd anon non-compound page starts out with a ref of 3 @@ -66,6 +68,34 @@ static int _test_cmd_mock_domain(int fd, unsigned int ioas_id, __u32 *stdev_id, EXPECT_ERRNO(_errno, _test_cmd_mock_domain(self->fd, ioas_id, \ stdev_id, hwpt_id)) +static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id, + __u32 *hwpt_id) +{ + struct iommu_test_cmd cmd = { + .size = sizeof(cmd), + .op = IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE, + .id = stdev_id, + .mock_domain_replace = { + .pt_id = pt_id, + }, + }; + int ret; + + ret = ioctl(fd, IOMMU_TEST_CMD, &cmd); + if (ret) + return ret; + if (hwpt_id) + *hwpt_id = cmd.mock_domain_replace.pt_id; + return 0; +} + +#define test_cmd_mock_domain_replace(stdev_id, pt_id) \ + ASSERT_EQ(0, _test_cmd_mock_domain_replace(self->fd, stdev_id, pt_id, \ + NULL)) +#define test_err_mock_domain_replace(_errno, stdev_id, pt_id) \ + EXPECT_ERRNO(_errno, _test_cmd_mock_domain_replace(self->fd, stdev_id, \ + pt_id, NULL)) + static int _test_cmd_create_access(int fd, unsigned int ioas_id, __u32 *access_id, unsigned int flags) { From patchwork Thu Apr 20 18:51:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219106 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 F00F8C7EE2A for ; Thu, 20 Apr 2023 18:52:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231269AbjDTSwz (ORCPT ); Thu, 20 Apr 2023 14:52:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231638AbjDTSwZ (ORCPT ); Thu, 20 Apr 2023 14:52:25 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56F0E30E9; Thu, 20 Apr 2023 11:52:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CLMyrM+tZ7ihN2tYloe+LGe1P8dA6HeaMUFJgcufuevhT2ZebTPaGJNWrSO2Fds8c9nHXdRB+PQ49x/EZybv5Lp/GgJRACg+X5bFvR0Vq5PTIMgPWOyKNsXnt3KqJ03pb3r2CmkC5VsSQNsIBAV3hF8x2ApgwZKSOzWB2VL+ymJTf8ufu4KZuJThtWeWwbk4xDBeGWo3FwPw34vkCBnrWJ5I8wMZoaEAemYsUcNyCqIEf1oVM7UkIvV6ztAPZmoRB2zO78sJL1Cuw7gwXReN9BFeJDJYIhg8UYqrkk3QTTSuKNCP5JMmMeMuXmoBH5EZwsAbIy2wGkmNEr4Xb+HRGw== 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=qT6Ol1cSHqGxkMV0k4Tx2HBgEXoB/o2/tqIjtPeU2W4=; b=jxldoCBMKX3iqiPG7XsKgbws4WLQ93+IbsVHHyJ5lOaT9SiG8LsKUkwnWY4pp8Nc+NUkbPs8gH5VzPA0Ybz1BEtrGX5Dlc2QHNMnaM5qtiNt6WLwZ6w+3UpEu1W8rCbd9d8O7gxnG1xwgioLUAu0vz/T9xlLdI0FixctRkEYzonbsLv8Grri/xstIQGKu5bwXZb2ota15GPqBUEEzN42lgHYsLRGUARc7jvtgwmpEz3NqQszhxjfm969YBUDz63aEgA6u/mtwQt/I3WB33pnmh8vHomOETMWxko3xqiWIjbALMkSNAwJ6m7MFRLyLdzWfAITfdDj0OREKdzC8wEWwQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=qT6Ol1cSHqGxkMV0k4Tx2HBgEXoB/o2/tqIjtPeU2W4=; b=unOE5394iMAV/M0DVv2Eta2lSbAZM3qtBeS2NVunp1/UGORWYMsqbCx1Q482gUqONxaq81g+104U2hgCW0wTn9NXO1q28GtuzsjrqRMLs3KCiLkto4K5CgomlEmlLXcb0iXXhqKkGEhRqH2RNvoDLyuEZ7uDXegBOf3xMTx7k4kSONOp7ZtpPRPxQfcAu+woPYiEpQ1FpbWvPxA6z2lqUtwY9ihFJGWBWZ7sjx/KA6jE0j1l0Qsv2U/6Egnz7z9S165MuNKS/qkRvIQcHZOfU+3/n8Yv5ePrqLmblGW4BIA6jYSnLaRe8Cck1vq5LAxTF6JVx0tWAhLfUMej8u/vEQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:49 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:49 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 17/19] iommufd: Add IOMMU_HWPT_ALLOC Date: Thu, 20 Apr 2023 15:51:32 -0300 Message-Id: <17-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR03CA0005.namprd03.prod.outlook.com (2603:10b6:208:2d::18) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: 64ed84e0-88c9-40f3-6cd5-08db41d04645 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MIvHEZMzSyC1NYsyaMr/n8h0nMBXeVczMLftDiNUF/cRRGy1/47In75x2xhrBVKHlXN7jVsv49NYqXwK8Vlb7ceiaF8ohLqWYXEI8ckUUf8naQalQqqtHr2MleoU60lu6qtqBKJGJE9mRLGfeZ416kJN0W3HZFWo8Ah6kqbC2g7T0xTxPRyz86QZelwDC5Mvco4dLMPCaenaYHLObvQ/6dpkzMRexpfC57KL1DzfYWx2CDo76FqoX8At6ldmqXHX6MKvXgmpZtwttDsljk/hVN5uGRSFdUb0+2M6eWQqW7VN2UYdPLZw2/ifIM5r9Xj+lGgl1nLfwRs0WR707YnA65lISzRnPBnUg+pd8V3lJbQshssBmX4BCM8kZHvp6mNWt8Qac3k85WpsOhg9ao2QOyJKGgjqKSdpfnJD7I+VnZNrhY1mAU7hVsQ18V7G1YHzL7ksy6Qs5M0pdz4wZqSrO4rTN37cVON0SlNnXR4TGmEi5hQpmrysexaAPTTV+6b1Xl/kILYfYvP09UNSD5ItoVC+fXYXjDrOSjnVKqcQzvUO4X1kQAVTh7iY+ugtMspw X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 3oncfhnFj/yAPca7NiAMsHuW8xqNL0kRBLcdYV0xDw/pIxuLj7JKQ031xGCz8S6RfnTq9quq1cvQZzFq5A/1MTC1IBq+UFI+FBI4fgEdxwEYUp+sG19vptgxM/VHp0nUpSlws44WHSzVkHArVnVQhXPQz/sqw71Ui9ifOrQMUtdRRlvFGZf48CDbaBKCKGg5VThODrIUqBgFK1hYGzeDyqaJ0GNGGQtW7oYxRp5nx/aDzCciH0DZEoB3iadai83LoXcWM/nhGARiOn5sHuiFqHDWhS7MFeaUY4xDYnThOsHlKWe3b62wMsaYloRaeyAhSS0b+ylkqynNPx9FyUDyYh4o++70HU/UxYIdMbczf37jEKDMbtAlOmHrwyfGOVd4vwla4IEz081CRtt3h10lhKGbSo1oR94jSt2xoQQCQVWW2nut4LmdFM1m9filt6BQCR3EYo6mF0jDA/2JQnlb25pqJU3/lXUOMkuln6VFCqHPykIHpvJ2VMBQ3hYrgBIlOWRtmg+8xuZVULjmKh59WW7B39WUgaNvOlFhjUQ/smsMUPIebkPCrgCPIJ80lREsiylCGcyxIUGWA4bWByAjpSbpJrBpNQHOU7O9SRziEKVxQgbL471WNhtdo1Utk2eXEEME+Re+2G6BGii3/QbLKiCukCACT3pt7oPJA/UbZ2sPGswDPqNRHywLZe7LchVua8Y5inchVVGdtsdrgQR3gYZU3Elpi4FjlAsFJrWtfLG7HNsLm+kNg/lkXXcyxDTJaK5YdEn5GzZjjDZ1uFjJrZilEX3os88DLhdoZ+v5Cg16YdHOHbnL6CDWvXP6wRYSXjqcPf/ANuDSz42ffxJ0zadIgd0qJBZsc/GR6cMfsnzh09xQmFQESnwIHHcW7IOQYxbIFyGdUyK+DrYtJK/DUWgIBVSjrAJVD/gnW2wmfoQrZhv6AHppJRSYc7/G0kB7dFVBbOmLVHtEh3Zj5COodNh+5VyidV3EJcyP6i7+bdQKOu4oW6EqdTpVe7AgY9nO4v9fCdDm+J8N3vWF232366ocTGQKKkwGHM7Tp/XTC5Q3pCWIEDVjSsegSQso3PQy63tPEFxM+myuk8Gxzcm7a/8zeIZI62++O9XWgy2xhKEn3ipWfy94WiyfAnPUBcYMsASYCwFBBs6p6j9QMWaBz+ae3yUBBCGQbXNEmyZSwg4KzRrB0CWz7O15/u6jI3BS9jCIoEmm4jHa5JrgohGKYa4zNpwUw4pCbxY/mMLNmAAUF/okqLWkqiUi9PDwVIV0yA2srj9JP3Cv3g2wk9qdTf5P5sp32BBG/yr8iwSqBz/q2GVczORBy27YdJyfXaCkK8ctD3wo/OmtLSxvAfYMJTa4YymBEj4OHRssH+dnAU4qH+p6v5ts6xpZWcQGIARCJuWODA0bXknuuIiDU0X9oYkS6bLO8y/0oFZLnsEOD+isE/l6kxY4S8aghELGmo/hYtXKZF43Xj2o/tffjWnggrFd2LVOhVBJYfYnOC5u2kSq7XzL28wgIajAgBRzlXP7Dj2s1qflO2hhs4FjFBoFB78AyNe/SUslztUMa+ghKfYh9wO3A/nY5eEpX75tj09E X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 64ed84e0-88c9-40f3-6cd5-08db41d04645 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:39.2903 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 8c2EfnGRbc5ZnlQZsZADijPiMUT6ji0qCkCQSM8d3I0XxKNGuH9xYOsFCaU14XMg X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This allows userspace to manually create HWPTs on IOAS's and then use those HWPTs as inputs to iommufd_device_attach/replace(). Following series will extend this to allow creating iommu_domains with driver specific parameters. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/hw_pagetable.c | 46 +++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 9 +++++ drivers/iommu/iommufd/main.c | 3 ++ include/uapi/linux/iommufd.h | 26 ++++++++++++++ 4 files changed, 84 insertions(+) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 2087b51d9807ad..655ed32144f62e 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -3,6 +3,7 @@ * Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES */ #include +#include #include "iommufd_private.h" @@ -131,3 +132,48 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, iommufd_object_abort_and_destroy(ictx, &hwpt->obj); return ERR_PTR(rc); } + +int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) +{ + struct iommu_hwpt_alloc *cmd = ucmd->cmd; + struct iommufd_hw_pagetable *hwpt; + struct iommufd_device *idev; + struct iommufd_ioas *ioas; + int rc; + + if (cmd->flags || cmd->__reserved) + return -EOPNOTSUPP; + + idev = iommufd_get_device(ucmd, cmd->dev_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + + ioas = iommufd_get_ioas(ucmd, cmd->pt_id); + if (IS_ERR(ioas)) { + rc = PTR_ERR(ioas); + goto out_put_idev; + } + + mutex_lock(&ioas->mutex); + hwpt = iommufd_hw_pagetable_alloc(ucmd->ictx, ioas, idev, false); + if (IS_ERR(hwpt)) { + rc = PTR_ERR(hwpt); + goto out_unlock; + } + + cmd->out_hwpt_id = hwpt->obj.id; + rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); + if (rc) + goto out_hwpt; + iommufd_object_finalize(ucmd->ictx, &hwpt->obj); + goto out_unlock; + +out_hwpt: + iommufd_object_abort_and_destroy(ucmd->ictx, &hwpt->obj); +out_unlock: + mutex_unlock(&ioas->mutex); + iommufd_put_object(&ioas->obj); +out_put_idev: + iommufd_put_object(&idev->obj); + return rc; +} diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index d97bac38b7a41d..ba50eb4661e217 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -262,6 +262,7 @@ struct iommufd_hw_pagetable * iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); void iommufd_hw_pagetable_abort(struct iommufd_object *obj); +int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd); static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, struct iommufd_hw_pagetable *hwpt) @@ -298,6 +299,14 @@ struct iommufd_device { bool enforce_cache_coherency; }; +static inline struct iommufd_device * +iommufd_get_device(struct iommufd_ucmd *ucmd, u32 id) +{ + return container_of(iommufd_get_object(ucmd->ictx, id, + IOMMUFD_OBJ_DEVICE), + struct iommufd_device, obj); +} + void iommufd_device_destroy(struct iommufd_object *obj); struct iommufd_access { diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index f219e47d882525..73a91e96896252 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -265,6 +265,7 @@ static int iommufd_option(struct iommufd_ucmd *ucmd) union ucmd_buffer { struct iommu_destroy destroy; + struct iommu_hwpt_alloc hwpt; struct iommu_ioas_alloc alloc; struct iommu_ioas_allow_iovas allow_iovas; struct iommu_ioas_copy ioas_copy; @@ -296,6 +297,8 @@ struct iommufd_ioctl_op { } static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { IOCTL_OP(IOMMU_DESTROY, iommufd_destroy, struct iommu_destroy, id), + IOCTL_OP(IOMMU_HWPT_ALLOC, iommufd_hwpt_alloc, struct iommu_hwpt_alloc, + __reserved), IOCTL_OP(IOMMU_IOAS_ALLOC, iommufd_ioas_alloc_ioctl, struct iommu_ioas_alloc, out_ioas_id), IOCTL_OP(IOMMU_IOAS_ALLOW_IOVAS, iommufd_ioas_allow_iovas, diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 98ebba80cfa1fc..8245c01adca673 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -45,6 +45,7 @@ enum { IOMMUFD_CMD_IOAS_UNMAP, IOMMUFD_CMD_OPTION, IOMMUFD_CMD_VFIO_IOAS, + IOMMUFD_CMD_HWPT_ALLOC, }; /** @@ -344,4 +345,29 @@ struct iommu_vfio_ioas { __u16 __reserved; }; #define IOMMU_VFIO_IOAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VFIO_IOAS) + +/** + * struct iommu_hwpt_alloc - ioctl(IOMMU_HWPT_ALLOC) + * @size: sizeof(struct iommu_hwpt_alloc) + * @flags: Must be 0 + * @dev_id: The device to allocate this HWPT for + * @pt_id: The IOAS to connect this HWPT to + * @out_hwpt_id: The ID of the new HWPT + * @__reserved: Must be 0 + * + * Explicitly allocate a hardware page table object. This is the same object + * type that is returned by iommufd_device_attach() and represents the + * underlying iommu driver's iommu_domain kernel object. + * + * A HWPT will be created with the IOVA mappings from the given IOAS. + */ +struct iommu_hwpt_alloc { + __u32 size; + __u32 flags; + __u32 dev_id; + __u32 pt_id; + __u32 out_hwpt_id; + __u32 __reserved; +}; +#define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC) #endif From patchwork Thu Apr 20 18:51:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219093 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 46A54C77B72 for ; Thu, 20 Apr 2023 18:51:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231840AbjDTSvt (ORCPT ); Thu, 20 Apr 2023 14:51:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231803AbjDTSvr (ORCPT ); Thu, 20 Apr 2023 14:51:47 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2069.outbound.protection.outlook.com [40.107.244.69]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DBAC33C31; Thu, 20 Apr 2023 11:51:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ay6qb2YA/ESu5dYk+Ulr47A5vCiVtzhV9OyrMWs8cm4+47N6HUtXnFs4lqa9Rt+HsMHR3QPRXKTwxjJB29B+m8i7TL5XGD4ZyS9FLvi5FqrKPVW2iP5sViFtrDKGMsqLoV20kxWOF/v+Ai0y7hLY4wBc7ybdaP8VFwT8U7YQnHPXZDxiCw7cwY7iw2lT4VOG0JVx76M0Yc+AIgW3/CYMZflIYiXLQE/Zm97E6DKInoiYSLkaQgz5xBvJN9DjBLMDurO/sF1lN/jGjJlmAhTNH0ClZo99dQta3iIMUsD0alX/9fgoiU56n3EJPkJPTbdiwXZjbDtjLAMFbwTqGGdDqA== 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=+913O9G4ulLwvk+m50ADYtqHPLOTp0dqAaNi68zgDug=; b=MaisVIL61eo13RlpncUwvzCgI7TQpfOZj+iE6L/AinI4qDAo1BUqafv8fFPsK5uSv6u6dcQVZanvNOyOJ6PgQ0cAoEFkNybT/fayhYhBHROKZxnWd8jzPg09oBuu0Go9lL9TwTBc/Br7da2ZaMBjEbep1C65wLj9BSnQx1pgFwLdkq0/mrlliOTjBGfEfmu4XJSj4zHxzr4ve7N85SsDor5CqWNyiMlOLFpP6Kw1u3r8tNlEDiSeQ1ZKC0WMpcSN5Uy7P31SbwSc7AGES/mto9Oyp62yqFKH+fxdWvQ8wn23q1b/RIzN1f1AZvsFo5CrUvdpgmY4BFBk43mwrqYRZQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=+913O9G4ulLwvk+m50ADYtqHPLOTp0dqAaNi68zgDug=; b=UXp18yPQ0bRz5v+9/4J/HK5p3B85LcGF+A0e63oBtV8/GT19pJK9ObyPQQxHaWUIQRRw6yXH1PbPThlWt/g0Ba98T0q6HPouGU1QSc1aaqDJJxUWv/xUOMIWhxECrngxDlMgTog7Z/fMwUDcX9ZYuVl6R1axl+b3f0blCh2P1VuUNDieDupiX9Bd2VEoWH1i3kJHUmSVMv35Jng/3gAyndbT3TVrGpHh/C04lMlbYai7vkKN67s1YVnP+Gzsxorz0bWuZs1H8cGiG2AvR3WOCNuONgW+DkoTyLQQ+hH1bj5Z5wEZc9YWEA/RlKPwK/pDPZ7FdU2hSus4PdauSgZF8A== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:39 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:39 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 18/19] iommufd/selftest: Return the real idev id from selftest mock_domain Date: Thu, 20 Apr 2023 15:51:33 -0300 Message-Id: <18-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0338.namprd13.prod.outlook.com (2603:10b6:208:2c6::13) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: 14968afa-e3af-4620-b6c1-08db41d044d8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3uErnx77akByO8HnNE/DUqIu3LQdA1PltYvVxahXi+BZjyMwHLzCszsUOvCMZ/+Ga+AJWry3aGAWHPCb7eDzKhbuUjX4QezpGWdmKAUc1yZFZ1wpr0XKgaXh064zuJtQjl7pL5Tdwh32J/9anM6gaD3WijSUFZ/gIMxaJN+hGizA2SLvLbxoyLy8vGjqJPPREQa99fsL+ScrvXCM5G+ZSHnGSXov43ETiDaPxTB02FvY0ZYSaJf69UfL9xyYt7btMz/RavfRcwSKw3xxQc1DWR8qQa2bxrx+n/P6nH9u2wGcpnoqbOMMtRRVgh+qtoBEJVqKt7/xNfnow+wMy4xcd60pOteVdgf/0DB5TJPtB2JgqzYrLxHpnqbyhXpKAix6FQJu2Yv0nEPOM80K69iUf3gQ+lvQu5n8LDF3AlNZykmcqImBAYY7sySWjooT+eagLCCKhT+spwUfefZu2fJvJc+OphmwA5Ju7J0tz+g2Z0uftc1Pfz9cAuSsRG1KwIX1Dsc8P70Nvz8Uyiv7LeNV91cg9ZE0nVzg1qsLlhdXPrQ1ckxrkYlBpdj9kBYzUnQ/ X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: z9rylqqRrRZ9IxA2PoVzAvgDXPdaxhanlNnGK3gPQJi3XD6in7OwgB0MG/nh3qpq+J/M8ljtv1Nyxfra9PiXP8m0DzHSja0vYEJ4tESZXWnimHNDcFCpRVty6BRSrfvRFar1wl9uREVMVctf5SD0phZaeuzx5WlQkgwL9rXu1EC95CoomXlF1h5Ju9NKg2ZpLVM0Wb5Muejr/2I0LnmFSW7219GyrmgH64yqTCPuou8ht2/8HCAHLTAvCrO4Bh8kHQAnd11W6/aQSJmhEiJ+PAMnkbdax8R9oqaUHJN8v7+3QEXLER+zkMwH/sHYcvtFSgSb1cVNmj/m38jv7aVF0p6kQyYjkYYXXEtk4PQkc6/9AtyLku1xo40DbRfnyogsLWyYTxuhdahqeUTWFseDygfFQK/hu8AlhMXxXzSAQtIgBzCogUHln4ZWb5M6uHeRDwrIqx8mp2cieEgkrdRWUUpi++I1Ykd9kYj0uIxh2BCx88tmjRAUn+fX7LqyNHpbGe1oJi4O5YkaZ64LLU4ni3o96Ee6DXE6mNBn0LX3tdYPo4LV2lPOpBu3jACUD37mRYTEIniljH6CHJZEnqREDvrjaxepQBLQ3FaQ7SAU4evJoI9Aj1JpWh/PbopzMl2iEYyEGkTUmlqr41kKGYbgYDdaiPdfy7Lz0Gv5YwBiA+TPgy/znzCJGfdPLwaB2K3CCGIX8xxGgwY7dc/E2bEeAKFy5Hb/sDYFIeRZvwKV6SnljizvZqf0Na/az08OSFS92CBZ4XaWuXTb8r6soxFeHKjjX6KhYL4YH/vobCpXli3lWWtTOxxr8tXN3LpKZkLYPxIorX1rRhEIRY+2eFevHJkJjIdJbQjuw17ZTgEPFS3z11C/O64ZAy8K+oMfoXP162BFddLWW9aoqcHqdXCOaHoIQhfYHg9DASuKU8zmbmWSVsQMpE9p7BJ1gdvQ36dCdF7NNPdgYCOgRvhjQFFBbQgmUPTVm9z25EEVTa3q22uaLfh2685l2FzeYBOYUWHgJhhQ3x2nAVMdXVXcgOoNJpx9mhcWPWyfJnBhIqvJPYbw54U8SupuPJbvVEtcDRHrUjDXsSJ89ctqRpO49SDqODWmfREOs92qIZH4ZRZEWM+6jfrLXqhdJbVzYyP2uGymjoIlNKblBxSt8kgoZdVJiucZZYLVTmKSQgRkAXhk73JVwOXthyBrTTNHCulPFQctAn+xRyyryNu42ahUjlLGnH2qixycJkCS1bbyYWVAVPwREppqCq0+atZmBcScbeB2Ku6OcHPOT4/kR8y6OASWAYcQhJxkD9lVAQoD5YDz9jmUjwG//Ad5r7qmoZG8Ry7jG94xQaad/pAeB1STViBuvkbevKmLmwpbXKd8ucA6kveIvqZBX/cFCF0j4jYkDb9bCZLB3bIc/DmKGFM1vX6N3Dtq9EoqDdvaKSD6j3IFwcTxyhPGYn454EyB7qOBQt2N9YVELwrIftTR9QcKACowHyrtlCVZyLoeAixx5X+c+gdGEZblmSzQGjbTCo+Mp2lKt85Xv3go9Sl5CEVT3Jr/kwXwWLhAzXXP+Seg4pK1VUxFOzvYu5RDBJALk3XKVQ1S X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 14968afa-e3af-4620-b6c1-08db41d044d8 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:36.8820 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: icZtNpZymvWDSIfG3b/+DLpT0TlwT1hTLfpkgmLtvOLHdh74wNa1HvCnmVhQcmpE X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Now that we actually call iommufd_device_bind() we can return the idev_id from that function to userspace for use in other APIs. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/iommufd_test.h | 2 ++ drivers/iommu/iommufd/selftest.c | 1 + tools/testing/selftests/iommu/iommufd.c | 17 +++++++++-------- .../testing/selftests/iommu/iommufd_fail_nth.c | 18 ++++++++++-------- tools/testing/selftests/iommu/iommufd_utils.h | 12 +++++++----- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index e3f1035cbd6464..dd9168a20ddf4c 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -52,6 +52,8 @@ struct iommu_test_cmd { struct { __u32 out_stdev_id; __u32 out_hwpt_id; + /* out_idev_id is the standard iommufd_bind object */ + __u32 out_idev_id; } mock_domain; struct { __u32 pt_id; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index dd792294298ffd..a9c22560f51aa9 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -443,6 +443,7 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd, /* Userspace must destroy the device_id to destroy the object */ cmd->mock_domain.out_hwpt_id = pt_id; cmd->mock_domain.out_stdev_id = sobj->obj.id; + cmd->mock_domain.out_idev_id = idev_id; iommufd_object_finalize(ucmd->ictx, &sobj->obj); return iommufd_ucmd_respond(ucmd, sizeof(*cmd)); diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 57559f60ca6486..a6bc090d4b38a9 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -211,7 +211,7 @@ FIXTURE_SETUP(iommufd_ioas) for (i = 0; i != variant->mock_domains; i++) { test_cmd_mock_domain(self->ioas_id, &self->stdev_id, - &self->hwpt_id); + &self->hwpt_id, NULL); self->base_iova = MOCK_APERTURE_START; } } @@ -262,7 +262,7 @@ TEST_F(iommufd_ioas, hwpt_attach) { /* Create a device attached directly to a hwpt */ if (self->stdev_id) { - test_cmd_mock_domain(self->hwpt_id, NULL, NULL); + test_cmd_mock_domain(self->hwpt_id, NULL, NULL, NULL); } else { test_err_mock_domain(ENOENT, self->hwpt_id, NULL, NULL); } @@ -647,7 +647,7 @@ TEST_F(iommufd_ioas, access_pin) _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_PAGES), &access_cmd)); test_cmd_mock_domain(self->ioas_id, &mock_stdev_id, - &mock_hwpt_id); + &mock_hwpt_id, NULL); check_map_cmd.id = mock_hwpt_id; ASSERT_EQ(0, ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_MD_CHECK_MAP), @@ -802,7 +802,7 @@ TEST_F(iommufd_ioas, fork_gone) * If a domain already existed then everything was pinned within * the fork, so this copies from one domain to another. */ - test_cmd_mock_domain(self->ioas_id, NULL, NULL); + test_cmd_mock_domain(self->ioas_id, NULL, NULL, NULL); check_access_rw(_metadata, self->fd, access_id, MOCK_APERTURE_START, 0); @@ -851,7 +851,7 @@ TEST_F(iommufd_ioas, fork_present) ASSERT_EQ(8, read(efd, &tmp, sizeof(tmp))); /* Read pages from the remote process */ - test_cmd_mock_domain(self->ioas_id, NULL, NULL); + test_cmd_mock_domain(self->ioas_id, NULL, NULL, NULL); check_access_rw(_metadata, self->fd, access_id, MOCK_APERTURE_START, 0); ASSERT_EQ(0, close(pipefds[1])); @@ -999,6 +999,7 @@ FIXTURE(iommufd_mock_domain) uint32_t hwpt_id; uint32_t hwpt_ids[2]; uint32_t stdev_ids[2]; + uint32_t idev_ids[2]; int mmap_flags; size_t mmap_buf_size; }; @@ -1021,7 +1022,7 @@ FIXTURE_SETUP(iommufd_mock_domain) for (i = 0; i != variant->mock_domains; i++) test_cmd_mock_domain(self->ioas_id, &self->stdev_ids[i], - &self->hwpt_ids[i]); + &self->hwpt_ids[i], &self->idev_ids[i]); self->hwpt_id = self->hwpt_ids[0]; self->mmap_flags = MAP_SHARED | MAP_ANONYMOUS; @@ -1215,7 +1216,7 @@ TEST_F(iommufd_mock_domain, all_aligns_copy) /* Add and destroy a domain while the area exists */ old_id = self->hwpt_ids[1]; test_cmd_mock_domain(self->ioas_id, &mock_stdev_id, - &self->hwpt_ids[1]); + &self->hwpt_ids[1], NULL); check_mock_iova(buf + start, iova, length); check_refs(buf + start / PAGE_SIZE * PAGE_SIZE, @@ -1424,7 +1425,7 @@ FIXTURE_SETUP(vfio_compat_mock_domain) /* Create what VFIO would consider a group */ test_ioctl_ioas_alloc(&self->ioas_id); - test_cmd_mock_domain(self->ioas_id, NULL, NULL); + test_cmd_mock_domain(self->ioas_id, NULL, NULL, NULL); /* Attach it to the vfio compat */ vfio_ioas_cmd.ioas_id = self->ioas_id; diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c index 96fb2f04623f23..8ab20df4edc8c7 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -315,7 +315,7 @@ TEST_FAIL_NTH(basic_fail_nth, map_domain) fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, 262144, &iova, @@ -326,7 +326,7 @@ TEST_FAIL_NTH(basic_fail_nth, map_domain) if (_test_ioctl_destroy(self->fd, stdev_id)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; return 0; } @@ -350,12 +350,13 @@ TEST_FAIL_NTH(basic_fail_nth, map_two_domains) if (_test_ioctl_set_temp_memory_limit(self->fd, 32)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2, + NULL)) return -1; if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, 262144, &iova, @@ -369,9 +370,10 @@ TEST_FAIL_NTH(basic_fail_nth, map_two_domains) if (_test_ioctl_destroy(self->fd, stdev_id2)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2, + NULL)) return -1; return 0; } @@ -528,7 +530,7 @@ TEST_FAIL_NTH(basic_fail_nth, access_pin_domain) if (_test_ioctl_set_temp_memory_limit(self->fd, 32)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, BUFFER_SIZE, &iova, @@ -603,7 +605,7 @@ TEST_FAIL_NTH(basic_fail_nth, device) fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL, NULL)) return -1; if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL)) diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 8b11bb70095269..62e01412a7679e 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -41,7 +41,7 @@ static unsigned long PAGE_SIZE; }) static int _test_cmd_mock_domain(int fd, unsigned int ioas_id, __u32 *stdev_id, - __u32 *hwpt_id) + __u32 *hwpt_id, __u32 *idev_id) { struct iommu_test_cmd cmd = { .size = sizeof(cmd), @@ -59,14 +59,16 @@ static int _test_cmd_mock_domain(int fd, unsigned int ioas_id, __u32 *stdev_id, assert(cmd.id != 0); if (hwpt_id) *hwpt_id = cmd.mock_domain.out_hwpt_id; + if (idev_id) + *idev_id = cmd.mock_domain.out_idev_id; return 0; } -#define test_cmd_mock_domain(ioas_id, stdev_id, hwpt_id) \ - ASSERT_EQ(0, \ - _test_cmd_mock_domain(self->fd, ioas_id, stdev_id, hwpt_id)) +#define test_cmd_mock_domain(ioas_id, stdev_id, hwpt_id, idev_id) \ + ASSERT_EQ(0, _test_cmd_mock_domain(self->fd, ioas_id, stdev_id, \ + hwpt_id, idev_id)) #define test_err_mock_domain(_errno, ioas_id, stdev_id, hwpt_id) \ EXPECT_ERRNO(_errno, _test_cmd_mock_domain(self->fd, ioas_id, \ - stdev_id, hwpt_id)) + stdev_id, hwpt_id, NULL)) static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id, __u32 *hwpt_id) From patchwork Thu Apr 20 18:51:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219103 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 B3ADBC77B7F for ; Thu, 20 Apr 2023 18:52:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231839AbjDTSw2 (ORCPT ); Thu, 20 Apr 2023 14:52:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231997AbjDTSwF (ORCPT ); Thu, 20 Apr 2023 14:52:05 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 600B346AF; Thu, 20 Apr 2023 11:51:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fcRFhMyOu4OzuMocNbpBRJ/LCy7vWRYAXp6U8A6qqADZuvyRBnKAhLibYr2UJCuM/AiCEEHxlBdVTJtJJkPr2EU5Y2o6oyhYa9P/962EqNrB241MmTe6PhPMkxBaDGl8lghGUta9rh9NQTsdCdN/m+e8dqYjG2wxnkuIYd4uvlSi78dpBViTrL/qYutVEQBN9mWeBp+tgBZr9CZT59p3E38owXHU3IL2hgJZ60JlguuEuYU0gOvuHPd2zS2I8RNu1Llhnpz8qNMIi95aW5BXbgkb443mfTI+c9cIfvC4z9NLyq7TeJ5Mhfxiv9O8iK7QkdMBkN9cvZBGMmKIsZiqjQ== 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=YolAE6BNvfYz0E6dQwvzLuefk2hk+Mt9iPrHht8Q+D8=; b=nmHa8SM8Cbtzg4M8oJELzlJoHXkhRr81KUz9sQR1/0Y+1xdtC5v93x7U7EPstFI8w/t/T/HKmB32CZbsIKTiRh7tvnE1MUHju/LptUCJjdACugvKxEqP3DG9/BgczzVXBLG1+JAAkQUt/0pC63q+ngoeqyu8DtFUGIy2dp3SslYwJXZfi+9MPJ8B5+hkpMoeUuXEufYo/EyP+wQsPEwb7enxg9c0CAje7q3wBx5JMmJgqkQb8ofYJmYshPEQp+iaeABX2NBf6iBT3cTPa1EvIRjPut9dnApIIENdsYS0eR1w7d5PcYQhLKNlWFUtTqL7h67zV5cAumSazQIwPQuIeA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=YolAE6BNvfYz0E6dQwvzLuefk2hk+Mt9iPrHht8Q+D8=; b=Ch5lRF6M8dyyjsQgyo1kSjQj+dc+idHEfIa2R6AwopeP58gf1haDfDe7K41H9g/91UoRwtLfgurbtgrvJhkhGGfuB7j/pd7F0LRYWENdJ4YThOOQgtYi8GkvK6GbNBMENuPbv1UAjxJOcBDVrtTVt/ahhoVDPPFRxs2fJH0Ua6i/iEmEPN9JXoi/F7GmwldCJNqRYr+/uifEyCBZRKxjm2uq3fJYGnLpYkmQfsH0M4JbWouQB99GImLTfqAQ0B7HLVzSMpHsjoVgh+YZGhj22ZzrdyCTyKZvGqvqE2Iy6vs+AvXiECpqLGTUYwLju54y3oxascxSuA8ayZA7F6e6kA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:48 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:48 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 19/19] iommufd/selftest: Add a selftest for IOMMU_HWPT_ALLOC Date: Thu, 20 Apr 2023 15:51:34 -0300 Message-Id: <19-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0343.namprd13.prod.outlook.com (2603:10b6:208:2c6::18) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: 9c749862-d9bc-4937-81a8-08db41d04639 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 7BOpOzR22aXWCgCkR0Qxog4gRau4uJfJM1USQJPPSxYLVTq0W31wBttsyfKWr2ari4T9YfmfLD1wZxf1nb0zSEx8CsvIt4c5qgdPzBPojIJkaCmW+dT7hzJMZwwe5+IIR7UO0UcxI1Btj0P7xyEdvPEH2/w2+BmC++SnKf1BChDx9QYASmQToa2jErL82xKYFNNgRw4Vy6vuISQEtfdV0RNSXWAgVw+CBeznN1ly+RHtnGDp76TDEdjLoDwk16p8sbCHF8bq6gKT2Qm2O/qvuVG7krtjuthORwI6ajoHHMjnG9XxNSx0Z9FJpfheOjadphY3nTsVF+wT8HJqBwMTxtEWqQboIQQrAvpfOjXo7lZbPSPKpku69EeFIne0a9FVdi6ITgq3mFhwC2Vz6PUoaChlb8PUmhMKShAFKiveHKzy0EqKXN/ekqB2r/UQG2sCNAD23oBTHu0zVqNDUQgqJ100SYxtuQRTCQ48Y0kChMTgJbjrnllDbzi3XXB46Kwf41wdC08T5G0c50qgMzb5zeAPLfYInYsOAN5ePEOzQGKXnk97IO6ZaTG2zBiem6Oz X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 8vw51BuNjZarjrAfNREMC0/4i0h6nDn+r0yxD3dSOOl2hXayYLUnYJWc7E5lPp/Alf84NCJC1MBtN0nHqds5bkKNL01YNTaTMhH+TCFnKDxrpWONRMiYRR6pksVKlO6C1hPs3u2e/Csxfu1ghjRHO/L32itlGw/FC1d2rQsl39syQGgBwrPcBGjC0z05eQJNMmTvx4reW/mv71yoCUUmUrowuWD/vAlIBE8vTRMPtdrMiS9RhWyI5UYqa/eFtUOTloG9+zQ4L6UF48SoqZKKGUKJD5Zrnhz6W66imMx3Vddee8HvZT3H1HHonpe+A6md/9jzecui5QP+8nfDAmlRSxIYNilGTqOlXN8O6JifGkm8iofnd7HZCnNDnFa/LjZeFyWdWkCSQwSfPPuwEtutahBzTm02h9DFSx8HnOeydAudrqnSn18K9IWpTyb+MeSCUXh931ntUtSiQUMgyzzffceWH9TKH+0yaiDtGyJz7ML0kUTu5E+QycsyHJ7kfo8Zp1OInd+8JrXGkXnMKZZIzzcTlEibzzqyylLxUHi6E+r2ZUhg3x4UoEhw4D49VY7uO5mouzXGxrYifgevXYyJRnfdwgMmpfJ82C4BO/DPFCFuw7ir9II7Lst3ION3cSUSY87BZ/R1snRpHaiGs919WdYtMJa9Qkvb/64A1JoWFsv9TKEpAHP0oE/gsO+2F2E1pVfJwRFELtvHuC35cxAhnhyj35B15+vA26E8ma0PTjNp/iEyIaQ2kyLd3J6POK4cUpr3TK6uhWp1Kh/QWFJQ6p2TPJeCAro8UeuH0jGXtgPcKDYav8Ky1uN8jDYWC9gwquR8Kedf93wDdYfSs1wij6CdNKV0A2K8dbZtfHJFYXUaTmzB/yNPUtHGlrcm+FvG00dayflHKzbRM4Gnwwn4aYoei4cew3F7oA3WpHtzvzHiXq+DuSt3m5M2yGhHUpDmDwDXJMRfVJ+R8HeVhOD+foFZFaa7/dizaMAWWtpQ86nBLQvpKZTzRObn2hawBKWKmtVcldRx7Sf2Yc+N0nIUgGWaPiDK/iJvcbuKx6Uaz3C7vXbdGa2f9sf72nhtKbyNpn8eloRXsXxTBmgo0EdGiCv4nMObLK7UvFOnkDMu/03RbPeNWewnGzI9KqW2pmjZG1i4IY68CHdcWL8fHD7AxcW4j33Z3GRtVlBKpzml+gYga3OhPj+uldai4zpTr+pzVobLyFddvqxpGMlwh7uKeXqhY/zxA7yalvxHo3KuBSGAVj57aLw6oZRvPhOcNCQ9tlBrNe9KifYIuRft5I5SUchyNt5VFfVn4eP4kDhv/VczqqEtfPIqPeVFhaTvL+jAv5ofqgOJ1ZHgFN6ey9Xqx6HROSv/2DiRaWVh/rRSVsrQmVYn3GMzrLILTGgigBtm6uDmcxbO9cwLFm+eRCNFGItPsfgS4hXNYRaDuV3ZOv8jyqxi4jnpeRgSg2wQs5tK39X9Z/2K2Ofkdo4MDufAEpOD9rBSyJM1rFIjZeiOyxQ0+0QjFUyauex/UE1YlHrwr2j1ljVmERMQqgjHmBZXeeYTCCrRRz4bPhe24dgiq+79m+0pp0E/+U0tzxB3wvw5 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9c749862-d9bc-4937-81a8-08db41d04639 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:39.1779 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: mjJLm7/S5+VhHI9KjWxPqie8wPiTjI0iocv1rA5aIV3gSEcZMeQ5bpID4ErK3M+/ X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Test the basic flow. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/io_pagetable.c | 3 +++ tools/testing/selftests/iommu/iommufd.c | 15 +++++++++++++ .../selftests/iommu/iommufd_fail_nth.c | 11 +++++++++- tools/testing/selftests/iommu/iommufd_utils.h | 21 +++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c index f842768b2e250b..21052f64f95649 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -1172,6 +1172,9 @@ int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt, unsigned int num_sw_msi = 0; int rc; + if (iommufd_should_fail()) + return -EINVAL; + down_write(&iopt->iova_rwsem); /* FIXME: drivers allocate memory but there is no failure propogated */ iommu_get_resv_regions(dev, &resv_regions); diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index a6bc090d4b38a9..8b2c18ac6a2864 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -1304,6 +1304,21 @@ TEST_F(iommufd_mock_domain, replace) test_ioctl_destroy(ioas_id); } +TEST_F(iommufd_mock_domain, alloc_hwpt) +{ + int i; + + for (i = 0; i != variant->mock_domains; i++) { + uint32_t stddev_id; + uint32_t hwpt_id; + + test_cmd_hwpt_alloc(self->idev_ids[0], self->ioas_id, &hwpt_id); + test_cmd_mock_domain(hwpt_id, &stddev_id, NULL, NULL); + test_ioctl_destroy(stddev_id); + test_ioctl_destroy(hwpt_id); + } +} + /* VFIO compatibility IOCTLs */ TEST_F(iommufd, simple_ioctls) diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c index 8ab20df4edc8c7..d4c552e5694812 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -579,6 +579,8 @@ TEST_FAIL_NTH(basic_fail_nth, device) uint32_t ioas_id; uint32_t ioas_id2; uint32_t stdev_id; + uint32_t idev_id; + uint32_t hwpt_id; __u64 iova; self->fd = open("/dev/iommu", O_RDWR); @@ -605,11 +607,18 @@ TEST_FAIL_NTH(basic_fail_nth, device) fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL, NULL)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL, + &idev_id)) + return -1; + + if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, &hwpt_id)) return -1; if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL)) return -1; + + if (_test_cmd_mock_domain_replace(self->fd, stdev_id, hwpt_id, NULL)) + return -1; return 0; } diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 62e01412a7679e..53b4d3f2d9fc6c 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -98,6 +98,27 @@ static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id, EXPECT_ERRNO(_errno, _test_cmd_mock_domain_replace(self->fd, stdev_id, \ pt_id, NULL)) +static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, + __u32 *hwpt_id) +{ + struct iommu_hwpt_alloc cmd = { + .size = sizeof(cmd), + .dev_id = device_id, + .pt_id = pt_id, + }; + int ret; + + ret = ioctl(fd, IOMMU_HWPT_ALLOC, &cmd); + if (ret) + return ret; + if (hwpt_id) + *hwpt_id = cmd.out_hwpt_id; + return 0; +} + +#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_create_access(int fd, unsigned int ioas_id, __u32 *access_id, unsigned int flags) {