From patchwork Mon Jul 17 18:11:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316181 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 DBFEDEB64DC for ; Mon, 17 Jul 2023 18:13:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231560AbjGQSNX (ORCPT ); Mon, 17 Jul 2023 14:13:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231985AbjGQSNQ (ORCPT ); Mon, 17 Jul 2023 14:13:16 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2066.outbound.protection.outlook.com [40.107.223.66]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC1F31BC7; Mon, 17 Jul 2023 11:12:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Uds2I/8nF+bSfG/22eptJV9HZEYYFxlhU+1jxXKx0KxuMUsh/dpOK0arsdqX/VFWObDMssD+gjgFPhiR+XfG3+MOdEazFHvZEeLZwFkyLch8AYVx57QOZ54tVWQRf2HNViwHdmKfPjDpzJIPl/Qktiaor5zy7zb+LN15NfcZPJLWBxmVOTkMGSeiU0sdUStNqA9ixCErCBd+vdgF4UJmc03aVeSvLl0XDMQvWKxr4GTWMYujd6r7hk7FsGpZLGXeSpf/qW1/5se2qLtD4hjO+JtZjXBFAtOePbA2m4QM84LD4NeLV8SsU2o056fjwqAqRHGQgDt9Lu0Wv4463URxSQ== 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=3Yerv63V1z3x8getRyfYKfQJVbdDuRELWrnhqE1QQyI=; b=gzA34cAN9MFZtIquqtvi+nWp0lwG4beKgeUDECiXq+TFhAQw2lz9C0xLMO+4QC1TTjfhcuo/eKdfb0vEW8T5NU74n9+n1ffHWC7duYunnpoPQW3pysG87rfxEKhNjV9oBmYIAbOwfw7WjFok/u0vzdSygen7O+yEUzgt6vP9qLH+qauyzDp9gdtYndUs+hw3KmWdzHOcK9S06aSEKjiFp5ymIxAylDl9zqP5A3JAPJlsXrPXTDRqMLYF0FhKcIe9nS7xe1Zv4ktbwbz0XLn3TAlrdqyFMPrl8lm/CtQVLLVC25/CADUHCDXxOMf23CupDNS5lTUbEVgZWulVrR8wcQ== 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=3Yerv63V1z3x8getRyfYKfQJVbdDuRELWrnhqE1QQyI=; b=Aaqyba5KZqw1kwYMoNoqQtXrJPdiq5z8e4dy1VteTsel0mTLP5GFx264HDu4kQ9VZ7+cJD0/ot483SetQwrnM8/SA3Ii/nfBy5QOh/M7VaWAj9EYeJd78Gz3FfnuKnfzPQPEs7wiEDY3T3SLjfL51JfDh5h+H03YqI3dRGPlXKciffcunE4U3pTnEC5FfTPgW5e8IOkskdpFwxZ+tRFBp9lYUADLcWX6h+eyw8mEdzXZ08DybID0cUi+mCkZTzCHzvs+b5eW2dpMVgv6Jw6HQiJqL6dIAT+gf+Byvm5U8RRkoSAOS4xror/RGhSSr0pC2rXd0620QkezhTTiF9r21w== 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 DM4PR12MB6205.namprd12.prod.outlook.com (2603:10b6:8:a8::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.31; Mon, 17 Jul 2023 18:12:20 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:20 +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 v8 01/19] iommufd: Move isolated msi enforcement to iommufd_device_bind() Date: Mon, 17 Jul 2023 15:11:57 -0300 Message-ID: <1-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR06CA0060.namprd06.prod.outlook.com (2603:10b6:a03:14b::37) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|DM4PR12MB6205:EE_ X-MS-Office365-Filtering-Correlation-Id: 887133ad-f737-4cd7-11ad-08db86f15b78 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EJ61xD5Rwygmc1TRiGP2VQ0naLXyLOx2i67/wCJ8RK/Sotq4Sld3tkgCp9nQ9JsyRmOQgarugq3gJ0W/8e3pDx+ajOSDmW1q3BsNkOHet0kpE4WI4cOPYAHnOIPMB9sIqNCgEX1byahKnNtEIT2XQomd9h/BJLwjeZIBOikfCd68OFRs7ewj2sTvsyt2noeiNRYnxhzbRiRr25tuhkx8pfA7icYr5JC9iVgb5twZ/SFanVlReitehaU5dAHOuZ6QQwdeXDMQ91/dSKbkltENu6OlXZWP4bV7hYtzrSzcj9/h4StYVKSGM+espNzprWpI9Ev3d9pWdaZ/1p9aP/OLfvQn2EoO8YLsR3UzTg51hEJD/o0spT9Zn3mcKf2XGW75kWOtw4v49uTgSa5BNG/EuiyRQu7RtMkLei/UC7wqLde+tzw0xDFHjwhZg89GSVfW0TbjC5NuDIg5GKtfyk06m2CnlK6vCQP04foYa+ESqyA6ww4QoUif2vjKT72IHDz/lKuRmZPPxpqjYSsTwYyh8feRDF9DUF7kTxo5c7PWYSsMSeQZs2IcjREN6Yv9lSEn0jymEMc37WDx3YHW/6RoE0AB25xjEWr3lAP2s4Bwc5U= 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)(346002)(39860400002)(366004)(396003)(376002)(136003)(451199021)(6666004)(478600001)(83380400001)(186003)(2616005)(26005)(6506007)(6512007)(8936002)(41300700001)(8676002)(4326008)(2906002)(5660300002)(316002)(86362001)(6486002)(66556008)(66946007)(36756003)(54906003)(38100700002)(66476007)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: jl7AJwnMlrCyIg5n610POZmd+tRXi9psVsn0+mf5NgM3WLW0rGEx/RKYBAMC/qH0dEzWPmk4T3RJ+ZSdYrpFHi9XoET2ufeE1gfQSLTUqndL6tksIC2imvICe4f0EIlj6pHZEf0EPSzuugXzXw9I/SL8LvoC8jifuxvLD6XLsnBNFRrrlp+pOZ/piwubqT6Rn+TFVpcwF2U7Jtn9tccnmsGVNuQ2pUHJoiySljWu3qnaeb5bRZLLCv/4Lx5rm9VofiIWKo0MV4oVCBZyzOfei2ojv1jLKE+xFLjewsgfR6hBnaP97Jvgw2Hf0TbmgmtCr0is4l3FG50/RKFvPTvQi42UFVxSBL7pVjlz8xHtjHxtLFWSHU4R4V3ZpoNfGDmi53/GKWkwNUNzB246TLGqH7/T4oEZX8aVDPOsD4KXsD2IoI+kEiq5+66OnLn/pq5RNLMf5Z5FuYrMulvK+L6IoQE7NDaCJn11ecsN2AdAIGfP4mykW38niP105iFA330sqxwUUrpOoyje7P79pxArdWnDWaGhsB/yMqUoOPuQ6+J0IwpByb6DcUFXYLJdhepeqJJimV1Iw+EZo9mA5Mgor96ZlcALC//DRoW3Hv2VTSUGohYvpYJ3/UXn++bZmO0/8kZSc8sHpMBCm5+B1vgqHaKpPu8gXgymSvuXm0pmcEFDH5mvr2oK8Jte3DIeUYlf9MdTZisIjoLPb3rC126hRFa4IRXPJgc6ITF3BuBUc5VYhRxoWR9J0gKQK2/Gn5YAuJajM+TgVYDUpKl3Gku/1yObklde4jV6qNXPO09gQ98VtU2OjQVGMKlyABBeL5cE1eYfS5t9bT/zmZuoSc3WDjV/OdNpiSj9heqL/6K5+E8i90f6mcAdjOXztFK/Ga/xE2VZd0jpekJHhUPBm6VwjDYlEWcV1BE3GfewbRJ9ahZif2v1Zlq0kBunIK5s80uSoKeH10smV5TYiSh8pjangN2doVGVyNR7BYB5fZI09V/4MGoJXWBGc09E6iBDbLubjJk53gUMIBy5zqkIrX3dmAezSN2cJFS7he+hQpYMCHl2grA8qN4Pu7kvKg9ib0IQ7aCVdJww1+149QnYgzbgBXRDCKJOr22/f1baCo6HUzJ11U0TkYK9LUuK8qOwKJDPwcDtF7ajVEWXIDkuWQp78qZbHxKGU5T7ZqZrcUxUgJ7s5YWnNmq13WcTGvsUKCpdGjT3cRC1R4Uvic4gWA6K1qa5oK8PAhlDmbgwNGuaqWkylVTGjWL+Ml2w6aq6Ss9IAmKCFlgl4NB8JNfAQMxROE+pkkmQz++mobi6SRpjW7naYrd6H1irGNAVewjc23H8OD98V5UPjfICm7rvAwfUJiEnZupP8xD3It2Rw71Gvja4R2FzIltN8zX5+2YquskkmTFKy84WgB2OSNnUx60OXU87RZ8nOcWJyy5GrqNYXpy0hM0ezTs8BrFcjORV/FJ/ZGPoKjCsgR+JMbxOFXho3pGX0Ua5rY3Hx3eq+MkT4POZn9GPia0bAE8Z1HL3xi8e2rknpjtVI7sj8YOa2lPh92UPUHTxojvUmukhI2CgetDdtZt+LxCnLOgcu+QWkms5 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 887133ad-f737-4cd7-11ad-08db86f15b78 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:18.3953 (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: CA1UmtCVkmvoTQH/AhVAQND82obxqlyOGZq0fpC3ly1vhYtxSn3486gJzC1flsDx X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6205 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 Reviewed-by: Lu Baolu Tested-by: Nicolin Chen 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 29d05663d4d17a..39dcbb5cda7e23 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 Mon Jul 17 18:11:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316196 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 50C58EB64DC for ; Mon, 17 Jul 2023 18:14:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231766AbjGQSOn (ORCPT ); Mon, 17 Jul 2023 14:14:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231776AbjGQSO0 (ORCPT ); Mon, 17 Jul 2023 14:14:26 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2082.outbound.protection.outlook.com [40.107.223.82]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 705221BC5; Mon, 17 Jul 2023 11:14:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YfL+zcJQvck3KFm3i9XPuS6RoYg+iYU3y+3UYipX637anneid6IJRbhUXGQk0MIn8HrXtr5ss6nOXRPtmHEr98pwUgB/oWN5oQ6fUF+K60o80VizgkMUN55IqGLchexNWe0z83T0Pdm9cCuMBbghleiKH/V9P2/M+eiieTtMAiTsWzghQwZv6vmS+mSq2RsIuXo8Cgqne4dgbjKmVZypMlCIjZBDccODwhdfgR2cu/EdpPqh2RMH9T2197g4VG0MqFEtYSarYalGhb+fbXmvh7I3iklvpQP0E9bNJbe7Uw4tRUVZ57Mtnp28txmVAlfqN1/kXNyy8f3sJbIj2DlEZw== 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=VKnB8ekxC+tZlDdOl69cWSqcA8pG1G9adkpwa6jo2Rk=; b=JCFkxmeYGajNiYilij9h40BUR0jd1EI1itX/r/xo114ZFexcM+9czJf8g8p4RaX7ZB5gTLVAHMbfEyrKlMS607Hu2ED9AXUPW1Yu++GwIBzDTqDm8Z9Cl5ABQM0kB2BFFESVBnvx7zZr8+foychW1qXmuMOAkuIpQ/OmPk14I0ZrVVuC0YfCOjNUZ3fHP6JdUhtBw1mOpnSPHqhtwN/mNSNHKOfdnXlDPKkYk0C0qT+BbVWY613Vry+TFJX9G/A/oXrgiwhpbjdkzWGemmxrbZfMW/ZAAdgkWiMUphi0JcPte1TaQ/MR8IQ7ZPORE9d2nYNWWSXWpF0cD9pdfDfmJw== 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=VKnB8ekxC+tZlDdOl69cWSqcA8pG1G9adkpwa6jo2Rk=; b=sEhmzKzOJXS+ElMx7wgdVkcHn5o3X3lYSDNRf2PMwmUj6bVpamdBIMmpxeJVMUUqdXm2UtVLPCiN7H5Wj/1b2moGWR6zzNsNqFwqimmcW5sYz3QUg1KdcbUjoL95N8Hch+NFr3yFNv3k+SfBBThC0dxTPdkap00bzGvxgSWKGztWPFTSxPc8o7L49ha7ucpNNROt2bA9fEGBP2VXm0kk4HKg3REfs9aLhIirK8hrBz4XzpQQo4EMrqqgzl18uUAOyTsX3O5O/WvRaH8FWJqyU161iy1jFvFrkG1OEEyGs/WV0HMvVztpuuyU/FHR5cs9HIC9M7fKQR1RPtLu/yPgow== 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 CY8PR12MB8313.namprd12.prod.outlook.com (2603:10b6:930:7d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.31; Mon, 17 Jul 2023 18:12:30 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:30 +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 v8 02/19] iommufd: Add iommufd_group Date: Mon, 17 Jul 2023 15:11:58 -0300 Message-ID: <2-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR13CA0011.namprd13.prod.outlook.com (2603:10b6:a03:2c0::16) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|CY8PR12MB8313:EE_ X-MS-Office365-Filtering-Correlation-Id: 57871668-c2fb-4452-74b7-08db86f15de1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: BBrzg1dRpzCUCciAHp+8eAMUVBFgw/ts7Og/gWR9rqXFEzJXfQ+hVP1mz/rbwzh3KGQbV+3FcuId9wNeH5GXhQt4sS3Eo4Ulog+Z11CdFsEr4RzI0iPTVLaNPP4P3ii71np9xIjLRUFSldQis481OXmT5f6+mr/sWzRlS/tWTO2XmBSXJsrAd7tmQQRnG2zU3jS87jS3y8rOkJIsy8JPraBxvYNG/YhHfLFptnxqv7+GBGFKsTx/d/GtWoo0YgssiE1fYAHicYtvq/n76tcjLZDFHRSsNtEBFE8MmU9QOrQuOR1LPQ5oOG59ExxDo7eRllD/LhF/3Q6OcLahxKkAryRF1eqWgw1NzzRfO6MjSLR38b9wIMgeIjYrE6etfblevg60bJzaxubNl05u0Uivyk+vuNFYQh9d4kQCB5fG1pwSZJ9hFURbTsqzq8k2JUs0v+lp4mTWeUpKUDyTzBg1J1XfjZ8TAd88Q2PH/tNMmTIQRsdHHXepr00GlLiVTHR8pHhEN8cUb5tRChc+XbJvChINdM1QvQugwtV3qA4GSGplLgXPBZSYfeKCPt8kc5/cSno2k5ibD6WYi4DiRmsQdBQ0qq10QrriS/sj5nXbwvg= 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)(396003)(136003)(39860400002)(376002)(366004)(346002)(451199021)(2906002)(54906003)(478600001)(6486002)(6666004)(8936002)(36756003)(8676002)(66556008)(41300700001)(66476007)(316002)(4326008)(66946007)(83380400001)(38100700002)(86362001)(6512007)(5660300002)(26005)(2616005)(186003)(6506007)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: huE3dAGu06H1olPXtuWPxLyTZ5eN1qQGnXhmP6/JMMaiS0XXPD7Hylt1zJ/mwcO07oTz36WlI4jNaNQoe7UMdG8XGNcwA2Du2pZoXVnvmCQ5xWjx/tLweFEkIlVWkuBs0NJuOB+x7s2ZhxC1OGOowjRKAed7xBu8nrpJE1ZBNljR3MR48iHwk/9qCRYfjT8Z7+zabTJjUphgfd5xyXqBJpkY6lhyU0zowVv1nFFc6b14InaCVa7GHVQmfFpO4rqXN2ZLFjsQqG/+bZLghCAi4wuAzLjQmcrZVBXp+KjKIV1OnK91nvj4GS8Qi5DTVYiRPDkZMWBqLmjOhJS+DH+qbWN9xHkFeOPgWje9xHVFH7m6Bn+96HsKNbptfE/sxG0ZeQN8kMVkxE9Sw1dLbCZi4U3GxrySaVQTiHm16TOhctYaHoURcgSaGycOtO/e1IRHv48+VwQXfL2rAUkPDoul1/ngJD8ZJDCijGvDAGH885ncj/yCxgiBv5YkRyHC3TbbWtEaT+UwgckBmkE8oixAbxtxBI4wn1OYDfaLAzjmWNnb6jDPtKHvpJFC74VRP3G7t+HxESZSE5RVrl5+iIuBdhcLDSguG9S+VaQIftXdAiC3QXUqf9EChEOSJGGzL90pUJE5NFQn1FdZzk63O0805RYad3xeHmPw7tMNMcOT5Z+eu2oOhY1paT/FgQco8JI1j44oP4fcrzkJJj517oPiPzabfg+WKtYVPQVHivvQ9+umpGkAzaVYJNsrdl6yXtbgY5omjACpYIduBRZJLWUy9NNGqS5NWDIM7YH1G7zMGaQZlPfRxaXnoJf4qHwQJ5h1uCZskW31bTMhtkrSOGxqRXN40jYSqubmjrNKKXmcYFqrnOtv3npp1EBKyMvKbpcbMAc3izcQgtnZjQIYz1FUZfb0u4g+xVN6VKjnfnmRSpbq3ghXt2bZcBm2zIUeSAI20r/kSDp6bQmCb0c9vZnFEtCvlQhmSVyHLvErqFCboewPGEt8BFr6lvf6UlOmhx9Ay3/W6YG+mukgCm3/VP67GoYM6M5FPinjcl1mx4OfuSDKOF3P9neaHKaudO5ufcQs6EWZypgmp2axhhlkbyOGUgg4XLgSO4LYjwiLnemET3YlxtwXBhDOH+47ndrKjkFmJozHo5mne05bjVxhMefB+G6NEyT0nq+LgJmySTpkBUSOsl16ORJu2umrXD1RRMZQseTPtrZHF2cBnc7Ai78sb2PwDvP0mdiTmh90EZ99JYzDp6RdLvJC5UzV7MPzTZ4SOUmfIpInEbn1KDN67NHMfX9ry7pVold4ixP75zYyTN3UAHKyjkJpH9fcxeXIup2cFd782PGfPLc4gIOB+J5qdAqUa+NTgrAuh39bdrk/TK3x4Q+cnb6297x7FPPUFM4gg67nSohTpQI/SVN+5nlW4hAYKsB669mLPvC06zvmrPoTeMZPDk+9R5kWAy+LLH9TW+UP+pf9ROJcx8bSglQoYiSoQu+KdlMivvZlchA6dQxOaLSJkK7OiaNyveOUYPfnhLAnJsWq+rJ9uxKLJeFgE3LXoHKe4EQnSvMK4x1btAs09uidIIXjmOIkZzzD2Ild X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 57871668-c2fb-4452-74b7-08db86f15de1 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:22.6113 (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: v96l/EPYPb9TQGTgCjj2Uk7fU3QmdOeQGfmeFs0FjsN6/fBCK+nvJDqkhYcVsMdf X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB8313 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 Tested-by: Nicolin Chen 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 39dcbb5cda7e23..327057c5461715 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 b38e67d1988bdb..f45615f19798e6 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 Mon Jul 17 18:11:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316194 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 0F56FEB64DC for ; Mon, 17 Jul 2023 18:14:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231654AbjGQSOS (ORCPT ); Mon, 17 Jul 2023 14:14:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231627AbjGQSOM (ORCPT ); Mon, 17 Jul 2023 14:14:12 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on20601.outbound.protection.outlook.com [IPv6:2a01:111:f400:7eae::601]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 976AB1989; Mon, 17 Jul 2023 11:13:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RzosH9wNzsZgZPPFFVT76eHR90xpy7I7GpUoA2GNXQJhI40+GZ1G9nMgS/g5tPzEvHK6/Mvq7/wfTAa4XN/dh7yJ7gp+onRDNJlRA/5XV0288oLFOgnxTitLi2jN5qh6uBYCu/UUgYmwnmOegilbZuwsR5AousOMnNzi+sIC450YGrDE+6z/yuhe/nLCI+XWz4b+0HrIpiIyvrE0JSyfhmNOg/e3wn4I0UGyajgvRqYqjv3CBVe4bJV4yYLTYtet5hzwywVGNaKbrGlXNNiST6pA1wtG3k5rpej+C4r7WHPf2PAw2xcQxo0NG42nx5qhAghFnoZeiZuOQz4ZUX37Hg== 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=SKavxZx0xtJ4+JXlz3Upn7Vkv7zIMSYBv8i+/jLoCZc=; b=cRCsGw8lpiA8tlIKjiFl2hJiv+nc5I9tsv3gQZRa6hA0lVRhdRcVrZ00cxPwJiyDFUMNHrB+lxpiDjflz81R6bV14caMaymUxA6bT6ARkCrKhW2C8AJSIcIeU1Mw3/rnkV5z5d8s03YlsJazsSKPeFoGPWZt9tQlZdJQg7dao9arbO/JB+Y9GLZqeEawQLrjvAQcbjxEfKxosv2hi+kvyuent/HuUcxwNIIzj1RmU9pXqxAdIdtZeVGMH/PPzRXYKWLBJx5NqXnhhpsXwn7w76xx3z+vYBNAEke68SDHpPLsSMhOwMFvbc29PoyXeq4LAFpUGZ47SctAuwsFlpWy6A== 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=SKavxZx0xtJ4+JXlz3Upn7Vkv7zIMSYBv8i+/jLoCZc=; b=o3TbJhlHuslOiHbsXTn+cYZfjM3nvJ53W6IUlpoIxkUeBaMchIGPqtzvmiKyZneDfpJ5D6wWs198PKZWGa0TsbO1b4G5bxjfXcNKpbfPPbgvqQme8qPdtFrttLcd2eMQvU4YM0+2IT9pvPz1OoJrOX6HEfCvuE4ykJy+rSPGi822RNCh5jX1PENtZfBOw1sEQMw8eh1OmStZEp789se/trv5KPYOH1JjTBO/uUbgU+cNRm6U7Mk/zMiS+3OX7vs/X+CKwPn18vHlh3eC+hJHiVb/wXrB9fy0Y8qvELYZO83k+LlONtDfojCKjccNeW7y8fG8olB+ZNCCv5SH3mZmmA== 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 SN7PR12MB6861.namprd12.prod.outlook.com (2603:10b6:806:266::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.32; Mon, 17 Jul 2023 18:12:28 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:28 +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 v8 03/19] iommufd: Replace the hwpt->devices list with iommufd_group Date: Mon, 17 Jul 2023 15:11:59 -0300 Message-ID: <3-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR13CA0009.namprd13.prod.outlook.com (2603:10b6:a03:2c0::14) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SN7PR12MB6861:EE_ X-MS-Office365-Filtering-Correlation-Id: c2ab128d-5be2-465a-aa1c-08db86f15d4d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cQhNKXG6igp9jvE6uW98cbXwQyo3OLMLNaUApd1qjx7ViO7gwQPu0ynn10RKB6tP8QwFPZJNkzCsehCHSFv3ymId2c5IKOzhLaE91X+lFON/ctSXHqovaaAIqSEL/OaswMuutc8diWb0PbrD4Yf+WGsSe5Upvep4akERheQLNYzUSNVuhj9eEr5cNuelWKkr4o7bOpSyHc90/MS97B4JLqkOuXS4/3E0PN+WjzCjaSCyuxIx2AyM7zU3nfekMAh3QkOLkPZB0YP1jCIrpdW5us53NXBH/l0jEwB/zO1vhjKUi5nlQjksmZlzqW61pkvi8BiwndD4s+HgLCddIXh8cWmX5B7Zi+o4oAKxe9XuJvf7yDsS6PH5CYz9nkVXM4a8t1TPpm/3uzHgE0cA35yJE6EAfMWwD5FXWlIDx6ioEqOEKrOa3D3qKNI741PIslfYnNHyT/veQZUKp5flzWJ3o8AYjnxNkjDDS0Uh4s3vuNR6vRvvBLB+1qMp91sq70M64jTX3IlXF8T0x1FlVpCAIrmSND2XMXUvP/f05iXbOeUiuPi+UBBOLiXUhy+JYwkIuRYQTTKSjEh3yAihrb5gvYKNEYWxclpmxlRdusrfblc= 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)(346002)(39860400002)(366004)(376002)(136003)(396003)(451199021)(316002)(41300700001)(83380400001)(6512007)(186003)(2616005)(26005)(6506007)(6486002)(478600001)(54906003)(6666004)(4326008)(66946007)(66476007)(66556008)(38100700002)(86362001)(5660300002)(8676002)(8936002)(30864003)(2906002)(36756003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: B2WjMBV+Wz+smfu1LOvpxbWMDgFK7EHrglVuKvOqKlLUrEzyL+1A31uNjPg1xNxvttX/dCsCj2Ctppxn0O/hMyn/fcAf12VueIwfbnWtsydTvU8uvonextdnIgtntORLEM+6JbLAjk2JHRfJwWdcirlvidNpaVMvVA49qZa3fpYt8nc55+aAXlpjwZnB7qLCn2XOwKYJx/ER6ANO13815VhpSGnjUrBPStD3xuom49ksvmjIA/yUrIPp7QQs5alMUeQPulcKwrUGu4dSS4GjH2dfKm9pDAOCXqiMZls5CH1flSyNH18qq4srcKV0IkdbxiZVYVGYvVWc7FrW43Y7PVsp69W0ebQHZYsQbNoq+Bk22L36WupK/InlpF4lA9hhRFReP4W1NcuroQXaS/B92ANNz69FgzyHkFmBdr4CKmlEhuHIlulGhQUTseuUHMzUM3STbUgtNQI9AJdm5KCrV0+PP0FCh55Y4Q88dUiDcxd6jgdBlbrd01y2nD8l4m6FzeqmCtN+DXdMIRo78jrXPNMNFkyrDiC6fXBB7e3RlEMUypQeU9vNz25Gs+29b1BGzET2u5fxEp176CfpcRP4cq3vvlG8mA1bq559RDMNDhWXwtbLeJRCJJaGuYCbhWSAHCgkHym0Tzt+kDdIX1OOSTyLoz40kLHHaN2qifHvBk38ls5pU0JxtWYP2eTWY8KaUKrENQ0Wriog+W+kidGoNYVh/Bh7yGaklRrkQ5n4xg7733HvMTyDZ0McmSrt3JFH4Tvhz8IKiLRTdRAzEomIEDF6Jk+3bXxwey96vl6o6QmYrfqCTyywHH0E9NALzFPsZy4EHqnOQbxVaO3upNp2SnLO9zQkP458Ld3DcPVbn8p894D54Q3EoaX40BkKVW7IIXYQYhI7KcB2Mik/vZh20HLaqCXdrzhCfzZ+sCmmrUQ+Y/ZAnINPLKcCjrdA+qpZneqGKJqskld1IgWSJbP1fxqcUEuP0xw7TBux6Zl+WIXutjg27wyi9jR4zTe9PH4dzdjHoJ0mEWiTVVNPWy6SCMRR26Swp1qjV3UZ/SqvYfQjZ1oJQFLG4D2DQOr40j0iBrkffb4BdoCjlzo/vgN+Tyjf4+EtjAt5GOOBANJ9JWbk8E+PIecd5dHKf/fr4p4JuRyIhVGYDrM2ZnhBCMcNlUbt8pGbcE9BCE+txFXsHgz87gYZe13lQe5WhlIJcAi6907QvsYPdtYMaNRe6xPAY3ghY/5yVx5ruhE68W9MaWItaNlH98GkxAu8AVIIpkZBtH68VFlupqsBtCbJJvcaKmknlV1e3JP+zFD35Gs+Jx6PtlGsyOjsJ5QmPXgrVIkgsIAXlRIDiyz30Zg3EnI1viiobvcJCgPMPrZckJHYiuMMKK+hpnLJ0aVgl55o5n5YlrjxmNX6htEO4nvSfEJtZpo2EQz4JVIOJ603SXlY4uocBhnejrh1Mb+wvYRnEZab+YdZY8fl3l7/j91xmy6e4lIt6S5HPMhIC5qj/Xs8TozixLRPMguUYK/7CQP9y0AdkoOBXhfSFdW+IxQ2HJsDehuqAd0ns8anukkUxfgjFMNJ7FB/9lcUsDxPvKtpjZNW X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c2ab128d-5be2-465a-aa1c-08db86f15d4d X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:21.6713 (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: hu4miS+w6pVzsCKvPYSdOWeLtGqZ0Rtq2UhNFfq6qfSpvyfD5pGKXDE6jlxavkN2 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB6861 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 Tested-by: Nicolin Chen 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 327057c5461715..0ab2fdf9c76be4 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); -} + mutex_unlock(&idev->igroup->lock); -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; - - 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 f45615f19798e6..e2eb1db5f8f8ce 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 Mon Jul 17 18:12:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316188 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 2BCA1EB64DC for ; Mon, 17 Jul 2023 18:14:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231446AbjGQSN7 (ORCPT ); Mon, 17 Jul 2023 14:13:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231409AbjGQSN6 (ORCPT ); Mon, 17 Jul 2023 14:13:58 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2055.outbound.protection.outlook.com [40.107.223.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9ED7E6C; Mon, 17 Jul 2023 11:13:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=REIjMCubaMqNFRTndz+Pq5B1b7ySw8TtE0BCpMg+75UdL1cXbEq6lM7PkXyQ/AH5fMmvICXRgp4Y6l6tUpBRSkf50Xy1Ox4c0MXC7F4YbSSXHcNhFJr1hsFqfYaQdqCiKRg1Z77w/1ydrCxUSUiXExw4nSC/ygyJpwKRsdZZJE8Rq63IVzjl2A0glVHN1kwvQ2xZFyOGQCodZIHavH7tNxTOHxDC11FmYg+kKPAjNL8yD6hPxvKy2g2w7sbdqSbbQZLYBPFHOYDH3X1ctpmPzjsuvJURjcu0Vo4WFvPckV2vWpJJ2ovDCwMmOsHQXKeDuo7hUwBil5BkqTPlk1PSJg== 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=WTJDjvDUYIGHXvH/QGyD271sAY0kcFpBGrR7tF4sLa4=; b=n5dDn2GMmW//pna4qAJstD0hqIKkb8muSj4Dfl5VWuMWONGq6mzjs+gd7ouL8nEZ7fdzBR1CY56LaFoT4stKA6Oc6JfLv/FKvaTAX/QcVwkmyKUjWDFJaRL2AV1Kh3vweGEB4PhFoI1iisXRsrYvZGl3glbfzeGZw5+t/lrZzOLfsNMaNcWz+x//2GdtW67jLGRaovbfYxNMQh0IbxxOLxFl56Z5e2tq1tR8OB7l/I+6MTgGFep3Bsa59mFsqTjTVdtaWol9wfD+NDHFxUqrLj0qL6uE4rf9MogRFSFYc7HW/fLiiG3WteJOyl5cZTAOEELORK2fiHGYaf5gWQqhNw== 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=WTJDjvDUYIGHXvH/QGyD271sAY0kcFpBGrR7tF4sLa4=; b=Qxea7km88+Q8kohqJgMDcsuhM9aoB9XBDsYdf20ILgEAbri0MmtKqKZcDbEoGG8injeMf7omuPvXs3D5I0y7N53mJ/ARWVndqWPbwzOm8owqcG/ezzmH87rGmeBr/gO0pc9lG/136zGEQO/RTBkogb0dmXsc/FqjCB6s7HLj+ZVRDNHk94mjRVt/RLdiFhdc7GXnzkzMo8G+fc+jzfzM0td5cGG8/ZbX6+/WHGwVcWYoJuw/6bxCB4b4mRdB0HT+aHqUhWGJVmSjvYAq+fyUU7uZnm4Eb9UA+jv276m68UN1v+wnesSfoHqgoa4FbxFoitKY5a/fX7HuEAHIaOd0MQ== 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 CY8PR12MB8313.namprd12.prod.outlook.com (2603:10b6:930:7d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.31; Mon, 17 Jul 2023 18:12:30 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:30 +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 v8 04/19] iommu: Export iommu_get_resv_regions() Date: Mon, 17 Jul 2023 15:12:00 -0300 Message-ID: <4-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR11CA0080.namprd11.prod.outlook.com (2603:10b6:a03:f4::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_|CY8PR12MB8313:EE_ X-MS-Office365-Filtering-Correlation-Id: ff816fbf-a888-4d4e-9bcc-08db86f15eb5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ywsukzYUBiPkYktxP7PFt6waBe/uWh3I4OT0O87p48zSlrGMYlxYOO2zn8GXuStYs0rEac0A+/R2CTi2yQpyIeyIsW6+kfjNrRHkgZeiSACspfaMYcR4QeTZj3aolLIWaEpFDEJ2JI97TEbN0CJqLYblPRSA8GAd0rOf/bGIoitd+PvY1aKe/FHZr+4QUlzozWu/u7TIs/Tprq52+ZFlBpmJHkUBHGTdNSWtZqqsw84DWDbVdOM6O3BD5IjTMBTj6fcSoDPmYfQhQebtfK7CW+SOxL3ozqheQyf9HyYcF0oh/DPbSn2ulkozqlST5vEIFvMl08WqZkCHU2i/UU7KtRQpOP6idI8R2l/mGb1kF5cfu0/cISaepQH5agE2aIXtZVBztsRo/2nOfm8rBlNjh65f3c612vjEoahDL0JrkFD8gEL1z5jqDOOua01J7tzoe+rDC2FhK+iQqXnqgsgTD/O2xeQ3lX3ODW5EMpbze5VqbfKr0bL/5zVg0pq5l1CPamBK14BcqmQ3ckMuuK8PmpaN/f1Kl8ZlzQmTFIqaEPnrn9Mzmtiq/ZsRIMWJkZrDCuq469DR2T3KjtIXfYPDuRQ/Q5/GdmDUyzYIFa55Jhk= 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)(396003)(136003)(39860400002)(376002)(366004)(346002)(451199021)(2906002)(54906003)(478600001)(6486002)(6666004)(8936002)(36756003)(8676002)(66556008)(41300700001)(66476007)(316002)(4326008)(66946007)(83380400001)(38100700002)(86362001)(6512007)(5660300002)(26005)(2616005)(186003)(6506007)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ktNw7UhzYo+zA/HI/CZfSvbBwAbIF4cAQ0uF42Ymwsi3p6M1z1uuFgLCydH0B94uJjJY5ckNv2KQjSq2qLl0AO1vth9PwKdZBVPJKeP1JKSQVoJSCvI95KNVLIpXgyp5wh/9qohmLtKtu668QXkX0X+a0hYEXCm3j9gPruWiyGsnj5LsJLt9fiEG/x6jzr1+KaEbtW/I+daeWTOH+KB+ce7yjqIO+QBSiFfm4Q8N2mmcu21Wx8eDQZJwYEQfhkn4DHULNJy970wpG25lXtMBH0RxzfGjc+/q8gIvwKingqjrusVxNc0vHXePk9/yhWR7wE4WPgkEPfj8tLKheOS3JELxB5P3tWz/js1EBxYVpUVotzS9E0ZkQTSZAmBaxaA/V0+o5njY59WiMwGK+zQkaz9vuUCs8w8K0IKQf269uU5kr4Sg+vNZHZn+PZzENiIOenA0mVYC4s9fsZdNaoIWZlHY0nrqtAr8f4HuQVYBsd5xmIfIc4IswZ4YBxNYsz3rqtcW/aDtbAATTfC1shSr/98uxbXn5xFLgE5beZB9cZJLu1tJ5iiBi8KYZU1Aysrtq77nBFZt4h34WOjKv4vb/2PxsSxWfeXmBCWWBI0YL0/Fu2KGreXVywAUnIGiAqr2UglSR9Z+sQ8y3O+DEg6ctOehNZaYMFlISa+HIhoAEgSq6Z0Qb9X63NDT3RFvM2mrwaNegTZN6hXpXXJnTB2YeQwpFt7XblNv64iX2mBfhFi2Jo089fJqfROAO99gsdYr3dMwG+oR2EP8PvTsdm/E8ftDlRHO+V++/kT70wE8aTB9RBMVNglWK/s2s3YQUPzzH4BN6Qx6aHRJReoVjwuC6v54RzK9G+ayzv48jrjCCoApGWvXf9L0dQ5UIVVfUKBTfVk6k9VJnFSEPinO27RHYtpl3e5b60gRMfG3g/y1sCt7O/Vh6lQWfYjRRK17vnwhOrqXbNcxLAjIIwAF17NYpXWvcL1fRGW1650Zt4wyui2h6ekk4lP46tMHd65Jpo1hNt1Fsk5MNr97dNDgUJM7wUgTJhkqZjhlb6iJxSnKHT1uwtV6KyO/rvZpuPxRdEn1tT6IFQ6GHXlaVmwaF0MEeVuJp/6dakDOERjoutr5K23A+4uQ2xgR0Frks4RtEgvS+CJs14uyr+iPjasDWFz/qJvXUM/36G/d0DP4X9cRpoChb8mWnaGaPe8WzNRNKzvLEZjofu8uNMToLFQ1Hz0aoOBKFhyF8ppee8unu+U7ECDuNlEFge/XNljX+RiwniYltiWckn2/yJVInEWVCoEHX5pMXtuZvT2gzmdj/Um45ze2aSQ1aKBRTGaxlmYpp4hin4irUVnbx2+7snHYUgtI2xod8G8ojYwpSPvJyuUTxPhmSe3K6+kWSQT5b+dJPRCJ1jpyffIWlYvGHigwRrGMReVF0w9W9SwXBSLW4zxnEKuveDSoqI7WiUn2GL1QMm0ZjJV930w/FGlzrRu5yJG2kECZpqjy9uARSE2FSsDfTEkxgSlgqtl+CGyOrZM2oFWijFDSbx421ySBGIvSb27pOi5W631T6BWdbBnIcnozdamhLqd3RHdiKHVKB2qO+uQQ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ff816fbf-a888-4d4e-9bcc-08db86f15eb5 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:23.8041 (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: mfyqc+we7Hxw3SIjHqzQPPjIE+X4l/EvvYt1sKW/FmOTEAKcn1Pkpb6Kk+wx+XNU X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB8313 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 Tested-by: Nicolin Chen 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 caaf563d38ae09..b0804fbad6f9da 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2642,6 +2642,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); @@ -2649,9 +2657,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 Mon Jul 17 18:12:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316190 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 53A40EB64DC for ; Mon, 17 Jul 2023 18:14:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231623AbjGQSOC (ORCPT ); Mon, 17 Jul 2023 14:14:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231607AbjGQSOC (ORCPT ); Mon, 17 Jul 2023 14:14:02 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2068.outbound.protection.outlook.com [40.107.94.68]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63C961708; Mon, 17 Jul 2023 11:13:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Qz++FgZv8sGpGBOX/rt9ZjekZQujOGfCGX1HOc6v83/CFA/t5cpjYeGmR0KdNfdsGgRiToT/w96p3RxynUEiX4qHnbEK5n5OJrXRe2qKa4tEftl3Z9KY/C9j4N8Y0PJkRg0sZ99UBIprqS0dmpn0o+LKXmbRUg3YI6qjJwPdCo0sAkdzJStnj/ejFCm2nTOnsJZ7cw8lZggAM3uuU42/d8BP9tZzfVJxu+6/X1G6VJRL9u1D7BxHfWzasU8nHV1+ONu9A0V2byff7AzHFG9pDWcYlYKihfBZfqPJIcaks5dEB00YKbvI65jOS6CFgx47ClMUPAw3FmO/lb3XMo54yA== 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=KNa1hrtutCPIWVF6Jnhw3LzBRke3a19yea8AGSh7wXQ=; b=LUSAZkLTdgjJgNxcMZmGFXqH87Jj4Kh1G0gTsjW2BjycHYQI0IEoy0g+NGetJzdcF6ziOPGSb7e96R/kgUFlHMFgDH2hu/qjsDMXoobsNucI8KMUTK5ar/pm87+9XWlLgKynSyUxVwExjxKO/5HtqPQyolJLcfS8SYIus9ZYfefLCYidnvZKmewg1AStFHa5YmugIopYtU6B/SWPtjkYJfcRMyI9a0RLjJPvfXkgU25d8r/mQccYX+BLOwm1eP3ZAVkfVPNf9ATzJazrm1ryEsW+XqclsUU4ejOFmz2zrKqdTC8k1ISiw+OaumFnSa9IlqxpZX1XrcXs/IsurCDnGw== 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=KNa1hrtutCPIWVF6Jnhw3LzBRke3a19yea8AGSh7wXQ=; b=dnhXOGaC35AEZRjPHGnjZDVefYOUJFQfPbfVGM68CRQVqyw2ROYlgk/+0sFzYElCCK8QnulBAHgqAa/nPgHF4rDyXktKLs9v6CW7PxmUnlJbPgT0jXKNQG0BebkT9Ea28qLGPQ7ieNV9BvLkF+3RY9XqQ9rjk3kMJX95Q+dPPRT+sAdGXfoTJhBYPGZ/Po+bLUeiT8K7Von7SapqFiN23p4U+URt5ljFSRsp8e4CG+6PtZYl97n0I1JEMPTFKUQifEi2efgQNnc6cO4mTPjv6L3FlmuZJwMr26V/MKePsb72M9Rooycy2PRbFX3oazZkvRrWDco2z8hF13EjYoxySA== 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 SJ2PR12MB9137.namprd12.prod.outlook.com (2603:10b6:a03:562::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.31; Mon, 17 Jul 2023 18:12:25 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:25 +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 v8 05/19] iommufd: Keep track of each device's reserved regions instead of groups Date: Mon, 17 Jul 2023 15:12:01 -0300 Message-ID: <5-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR13CA0024.namprd13.prod.outlook.com (2603:10b6:a03:2c0::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_|SJ2PR12MB9137:EE_ X-MS-Office365-Filtering-Correlation-Id: 57e54157-980d-4843-123c-08db86f15d12 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: iGQXcl4L0vyCbvAvjwX/PvsVU5kKdCrZ5xU33Wvy51LJzybWHIEKTlrAIfI1QU21gGXY+1LvpgcEDmyEvagV6vAFPYiq7Uu90i5QkLTF8qHL5KEqYxotwlYrZia7LtZ2fg9rgXk1NoMATvKgFBc96XeZ8ymNKjIfIEUW/x0QihOfgMS63wSK1J62YvzjgTgsGwyqx9XYNyWCxNrjoK1J4A466PvnrSvA8pXTLAVcWNqK/7njYwTUjoGu/vO/Jm8M1iSq4KsxoHAqswacTFZV3IOHcPQHoCFEhjIMg+Jil6205MCL+0yj2Q7dlickMMaD2oa4/DUrw2xWjtdeQj8SOraazXL+WHDFGaReanAltmPlyB9lYDJnEZ08+mnWDdU8T3Z68ub7hSGWFvNEXl7fQC3B4NQ0op+HLuFoHL/J+HNPB0SHxIHPYjq8ybi7UupA1tgstjX+rnpqD5WwNQs9ctNiJ90ou3NfSoO7ryOE93cqf0Aox4FdLlcV/xsC0ELybqjsuVtKYAiS0ljQV5fDDONUOphm2w1IATg575HIexCeasOAilzsApuEdsbSAhL7Z0k+UIVCsaW3AmUiBw6qjVXe1NuYSVk4ihWvnW6Pcus= 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)(376002)(136003)(39860400002)(396003)(346002)(451199021)(86362001)(2906002)(36756003)(66899021)(6512007)(186003)(83380400001)(6506007)(26005)(38100700002)(6666004)(6486002)(54906003)(316002)(5660300002)(66476007)(66946007)(66556008)(2616005)(4326008)(478600001)(41300700001)(8936002)(8676002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: eTfRWcSQdq1QQZCm4DNwLz+oIFU7RZ6y3lIWbAJ4cP1xok3M0F7Rbtzr4cibeROJcF2MTnBhWXcw3ZHWkDGR8a/JkiRz5F+V2sk8k2KNg0qwlXB+TIgGUSFSmx3PGzkeNz64JQ8hym5dO/wIOmwBH86hZQuJ79WGF1BfS+tRuCwT0Iwy2jDoPPvWeTsIp0ZCm28heDPaLHh7CysaX9ahekXkqCG3yXaaPqthqrP+qeWXVSnJOra7qZXAJmBJK9UaupO5pOKLt9/erYRIKxsEioozwVGHg94ayhMf7490L38hI4le3vzgkiHJAEsnryXyGd2DQFerlfkqbqQODcLOu55RbxFnzLN9zjHwuHZyaKc2npntjGZHKaXCpU8I3heXggytPFEjoIi1Jy+sG0JnbUevzZBeviqELCnLoqPJ4u9tYA9pXr3Whld9M4KAxr8YZxDCz0qiWBt4RhFMbeuB9tjjPB8BVOy8m8oZMfj2QfEtL3qAm95Jb03QzsvrhzcxYM1TtGndcY1TOQGebze731E2DaztSSr6EpLadKyHlNJYED6JESf+twJHLc/Fa8uLYKoY0+a3sCrcFij25ZrnfBxOa1/cPfFS1lzgdpKQ+JG/YV4ROV1jAq/ENeUMiEF22zfd6fXn5ePw86EdPJxkho6rVXrB2/uGsBeg0DkdC33O6F/bllTiQX5GDMgqFQrjZEXtaW442W8lOP8+mzD0yyM0RdynFexQaKI5hKjX7k7MrvEqpL95v8Bqr70aoqlOMA+CSCNPS1BO1Oz3hfJ9FzPIDUz4QkmZSsr3O6JpL9W2JCQ1AoB0UXK7bLkwVdIF/+MARUhPYVPONGxBcYIvh4REgWfq+vDx1CjZ1yqf6tMQpTg6cFvS68x93XNBtON+RUiNRuS/ksUbrqiOfQemfo9catUdYJEkd8Ztthka4xO/DTWAidmxPiNQawjcV9DBP8IKLjBTqj27FgzVXkrt6RNiHeyDwYUgiYD+LBZm4QiQUbbgWNIzpJJAjqIZvnrJNYq6yZ9AyEunTyMsRljRCeA5v0PyhVkj1tWSQ4sXqejUB9wM/RDUF1uKatxXF3QK5OjSgQLJidLt+U8PQel7LPZRDcNT+edgIvJGo5Yh3vmnLDLYAk5K5VGRfvoruDuuQxwNpoPkI0iuhpuPohSKAlx9D7IB1AoAJA8aOhVMJkCFtgITHo1Z4FmcJ+mhZ/ykRjX4+Q2H79hoEOCgS7wmRv2BO70c1jg9JO54gO4wASjm5J23Pgz1WB6/Kd4NtcY012jy19xwVhWZ4LVQ3/0Enz8f9Au8ZTHytdPCEf4QIwCkSl4VZ/hzqSbXg1/C6Uh+93uRiovXMrAQVvfCiDrCZtki0ONqZLFR9kdYsv/Piw8XPPaHg9m3USC/aYIWN3+dzi6Y5QMs+tKqYxP/QG9I6YzST/pbKtPY48feYjkHdUgMyKlX5XFaGxcV/R6/0lyWGUrh6Ye7lrYIuqtQUrwbAHEj8X1y/1+0yCR2YV+LAo161wtDhXss/2dWCYc+MOyEMuWDt6MbXb2fXnYE5QHf5FMtJpL2GvyEDwYVb3qC841GWPRVs/dHt2ti+GJ680qJ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 57e54157-980d-4843-123c-08db86f15d12 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:21.0873 (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: oNhm3niayELPRtyY0cz/VgESknm5jukcpqUOV3JRxmHDiuaJZqJB1uAQgcW9mLNs X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9137 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 Tested-by: Nicolin Chen 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 0ab2fdf9c76be4..83bd6b751a9799 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 724c4c5742417a..112bea5a84e43a 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -1169,25 +1169,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; @@ -1199,7 +1196,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; } @@ -1214,11 +1211,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 e2eb1db5f8f8ce..d5c3c627c0dff3 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 Mon Jul 17 18:12:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316195 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 85038C001DC for ; Mon, 17 Jul 2023 18:14:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231773AbjGQSOZ (ORCPT ); Mon, 17 Jul 2023 14:14:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231673AbjGQSOO (ORCPT ); Mon, 17 Jul 2023 14:14:14 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2055.outbound.protection.outlook.com [40.107.223.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 095F5B3; Mon, 17 Jul 2023 11:13:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CHN0niLqjbWJcVxNxS7myP6TSEeQnV/EBJie7N1Ie6KQahlTGdenMKGNeaHwzA6RdPg2tWn5n7/MERPz2Y2Pp0hJ3dOW0ib7XQzMvgxS/9mAOWRwHOJVj0VnYDZRc40R+siKstJg6EsA3f6DHSAd+PSGHuO7+qgS3vn2QQ8KVczH3etVf5oopMyq/274oJZVjYPuA9A+/xBOTHNnClfLpmtRqHPCkI+48iaT1qoZPRM5TBI37zCz5tgqxarpp/lF1OHETLfdgDDu4spJ7DSZKZgrmTTq2/TbO7U2GRXT67mliVvejvhEIi84R4BTk/cmyjkRUEPT6Cgo3LOcxCKXow== 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=PxI/vFXgUgAsRZh+P2ld87+dXHhDPADonliJcAR4MZU=; b=NxqnA7zlFO9A8IEPSVjJPQqWO2oT+TYIlFQ6i7WtB7jheCAIKp4cCs9/mSse+a8KEMINhBHATDxiABzKHMFYYbNvwqmDYcncbgWXj7maWVrzLY+0zzgSz4IfvNIdeMVf7ndEchHqfA7BwYNBvEOfk2EVrsioVPjuJw4TkIHcdPK7SKX31jKdo64zTFN6m2Dp0+tEJgu2cvKmmyGa/csxbDSrQqVGPLOOIs6yVrdfAvMuV/mnec7LDdKb9V3sDqgFrukCPryuGmGWjjhgMDKDUipnRdmBJ59puM5IP7OVF/KEfayJuEmTAofXrQB3+e0ekAiRYBMShCig7ypW3zqXrw== 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=PxI/vFXgUgAsRZh+P2ld87+dXHhDPADonliJcAR4MZU=; b=HnvDBjvXXxD1Ps2+ccd+S6KT9la/hZXVTuEnTRyYLv3yC3/cPsWbYpEcAcLhRobpw3BQ7vZVnpU5JjL9s0P/6KGVMIWVRhKmpT5f+96fF6qJsFXZI8MR2aFbk6z58z33IH7Yg62QhQVDXGhH+AfCwzekBAmM7+XxQOXxvsW/OJjhnF47CKVlKLNWJD0nHEnpD3DiOPm51QGIk7THC/9TsRK7t9m/X0wHymug8GprKY8AGMlt4gFQHeqdAB1vAiLwkRoHZCcFv5eESx7ZNH88brwocVp8IfSr+M/0/Jy3ADADSXPsoBuVyjs4zFiKSMLUSf4ZaQyyx9PnrZbhSq2hFw== 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 CY8PR12MB8313.namprd12.prod.outlook.com (2603:10b6:930:7d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.31; Mon, 17 Jul 2023 18:12:32 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:32 +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 v8 06/19] iommufd: Use the iommufd_group to avoid duplicate MSI setup Date: Mon, 17 Jul 2023 15:12:02 -0300 Message-ID: <6-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR11CA0086.namprd11.prod.outlook.com (2603:10b6:a03:f4::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_|CY8PR12MB8313:EE_ X-MS-Office365-Filtering-Correlation-Id: 82e531ba-7bc8-473f-92f1-08db86f15f12 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: P3YkOW32nqcWarE1QoH7C6k6aSoiT90fwk8Obk29bTTn5oUqDpTP2S1ETC3Wmnxw/BrhibfinW/b/7hXbQNeKMZiDpWPUYls8QvGkUkE/kz4D2Gf777bm5hz2kUFOZNdfuSUHgYi+fRkNh/fckJYaaS8NJzcfXkWeNtcDb5YTudJZ/j5j6DgBU0VOo1J0ljOx2c/F6EGRhX8fRsycWGSs8K7wz9rpgPniGI8Ju/st1Ja8Ee5deRZCzKAKLM416fsZ799OknZmn1YqSnFAflXqaaWQ21n93Lub+M7XQCzJ+BjGOf2KVrpVoG7wOWq494AopP2n4zPEmGcfF/ebgr8mCoa/As05Q09d4o6cHmBBphFxn63AfLqBkLIy+tnS8S9hSI8AsDvjMqXK5NQCpPBQRZyUrv6Mj+BwkwNwVSDYWZL/+Xw8LIux26a3N9MG/pnt7HLyzncjZ4g4HYdvVS7MZcMX/9FKd/GR58GngN3AJuPq9oCrTebOzaK1DAfiKdy+SJLvo1X35MJCgoi7rp23tBij8MZqKtB9ixDgDPJ5sgwE5wlU5ZN7sH+S5G5TtJixqUXzfRYBca7cvm2l2DsAdQdk/p2fgOjkhbf6a5Rjc4= 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)(396003)(136003)(39860400002)(376002)(366004)(346002)(451199021)(2906002)(54906003)(478600001)(6486002)(6666004)(8936002)(36756003)(8676002)(66556008)(41300700001)(66476007)(316002)(4326008)(66946007)(83380400001)(38100700002)(86362001)(6512007)(5660300002)(26005)(2616005)(186003)(6506007)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: UdIYwiE+0VDWdB4u9njx6BbiLYkQVnwd5tBKXK/+LnO5e9mPPXbUJOwLVratatUlxukNHz/7MG3uwHMvX6NeupPd+7rYPDC9KNOxwNJ2gbnrMtiwt3p1pf1TEqosEVtNDxUYdi/jyJPxGBmRuzErnKoFAi1Wgx/MLwzNe2aTqUM41w5Jej/R77hR0oRM8o7p8imHFGXbayITjlgZYDWSJ6O0lC5TUjzamVMQmMvjALzew7KPTngC9I5dFR6DzaoT33oA+2PPFo03xEEZWqoJp8Q3FOqFiZUrA22EWx3qQ3nQ6ZUnSA6kt6I+deYpg0Id9KwmRHGCzaFoV7c5o/CSBvGxf7f2kGMfkdTL4Mr9AZC4QeA4Pkg3UOPfYxeMIbMBZfTlhqsknpbn1VdpT468DHovfBBgKljOK15bjkoh9NeqTeRtXTQCRYpd2eFY1UlBtJ3XbqETgw3mJ/UDaa9fNJgHRHGKUF5aYE+W97TPgMadKleExPtLU4+vDpuwnmY+0u+71nY7i7eJiiGQWS/7znAUlzCwE8Fnb5667zvKRmkDg7PA2T/46D5loHBhowip+RNDaSF3H3NqR9nSi3k+xfpIQqo0DWKThhWDLWU3nSdtUAK8pIsNuc0c4jOtAENZ1k7rpk1017sJwY0MxOM1Ie6FEPtiv+0R+GNPEiheePqe2sziBJn646ElsgsxEwGDFzf8JtHuy4QgWHPkAX5G7Ifu1IQd51uIRLrpZ6AJfZmxymOBa9FG6dgeDSrGEsxDHsZFld74eGDgMt0ghVOUdxenok0FflwcajotRh7MkmXy8PAFMlbFva4egpwfMYJFO1TYrSld4YkRcExsvj7I6Vju8OJbF8b6BE3bEdzaeRQEJ6GQkqRCHaKBrz2LYx1ADB0SyU8ZKYK6lnnozdWcIzJlKXTgDuMWt/jNURLpL+DjHjMtnH4ZVIO+3SUVUpbosE50I2OfasbFo4YXQx21FUEpC68HJKiPmRystW+dUbAmXJpadGWQa7IFatGUbtySLS4AZS8rJKIahrMIQlvXMh4vg4770Fx3YI1dY8sFcL8HiyZ3HtW5CGWsrNK/PbWuK66PB+6dWHDKJ9pRmuRhq/R+52bX3QM6EIR+O0zahbfNEwJH72hYoZossuEggkNPE47dnVs6HJAekjSuARpR8OPn/4Te50rMDT9x+LkleLalIAmmILzfiJwPsI7Um6qaTnpjOaouB4gJACaf+tFOJ/exIFFesBpQVs6GZC8sZFWWm+bJoQipCdYfF9ZI/WLFV+d0zug4/LM7cE7pHesAItQwFyLPQKjKS1U9XUZlByrJSRHXRkbfubVazIOzUqD8h1HiXElnA6cA+2UfM4gEMCQ6iku07C5CestsG8iWBX8c8AAEqCBXsvILz3lmGnGUyMem5xrUBHf+q9qwUt8R9M0FShKjb2zAB6jhZ/9PS6GzGqWskCgzWysWErGaLy+uJm16bXzzHcDs6CcnivlcbQfZYd3tqnSfMSwKsCf6pkDO+cwx8VZ4vl7/lr1y5Y4FMu841ceZ2vxKLaRwhg0eh9ey2yilUnWFSOp+lNCVKb2LS1vcsFo8+S5QByHN6wKK X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 82e531ba-7bc8-473f-92f1-08db86f15f12 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:24.4386 (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: /kHJekwX5Gwlg8raARvjn1+gHvBA+LAggdVZM954/qCNSwWqJcRrAzGpRuB76Qy2 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB8313 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 Tested-by: Nicolin Chen 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 83bd6b751a9799..b2504c612a37b7 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 Mon Jul 17 18:12:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316179 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 561C7C001DE for ; Mon, 17 Jul 2023 18:13:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230218AbjGQSNS (ORCPT ); Mon, 17 Jul 2023 14:13:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231854AbjGQSNF (ORCPT ); Mon, 17 Jul 2023 14:13:05 -0400 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on20603.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e8d::603]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3D871BC3; Mon, 17 Jul 2023 11:12:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KT1gZAHrl6ygDmSdqpRWly2GklzkH3iVNl3R/1aCbS8k3SfEL81Qq2DHi607X+UhC2/MoohMpjJ/NA/I0Q+cec/sRolENlzYuwQuHKvjgTOGPs5sP3tgT/sO1ETTApyMnU/oveo9hjWtjLoxcg7IMrBXualbY7puUhs7AYv6bAa4NPlKJ+yXLT8+p+IdtO+SJ9wLx4ePJGVA6HwOuctFG68tETpX+XZ4stPKA9ggmHRgMyIiNRjC2MScoOUlK+CeT5HhcKKfuH08pAie331ULbw5X2m7BV/b6YFOSA9N2VE2hgWW3y8D++pHcrcd4VG6LRtMHfpmOwVOHKHSDBnmBg== 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=Sgtd24NY/W45v1jgk7GcLwdCWHckPN9QJng9ZjPGeIM=; b=dmYRIh6sRA02Va4VNSjb0YUSQ881oQx4ENVroy7Ph+AChlQW94n2iZAzc6mftnZ7C57eAcM/8kyaN6/+Xf0N1s6YBmljGooKxKMP43wLu8V3FvbRvjCW9zYXitGfbKtYFYvHO6HoP/E5TS0X0kiImfdxIixLK7PSg2oSF1BAK/hzto/iRhatDpLjF7mnPE/0+kcxZjWjBSKZV/iwoknUvfRYUs/LHp/kUfX5QCpAEWO0J/95/cECes7zmMiQCFJatyhoVIFinfsaXXIXs3GOkaN7c0kXsEUNiaZ8uSmiHrzjFOIzcmmnGZr9ubZTJrK2DvUHtGuGV9UdC/hNazJXvQ== 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=Sgtd24NY/W45v1jgk7GcLwdCWHckPN9QJng9ZjPGeIM=; b=gApSVR30Kp+Ax2FMBv4NGd94UfqzJjzQSyeuzmoYXWj+k8GoSEJ6JV+ukzGVPZYfrYEtyBoeb05xGHzFE0dJUs5N8Lu8f7q0CyiZlpX6J9W4jwK4R3x78e1O8ioMDMnKuPPTDv4gTAuOhMKm1+ZVukKzkFOm4W0saRZQxouK6EnBtDTF7cgb3jabSNvm/TKDAE8apohY/niowBHiUeYx5u2h0hIE0/+mfE2JEhh24JIWDgVjlZN231vJRglxJBAGO7KjOuD+9b6fnSFENh40R7n8XyE/hyhCu03/pRx+98WpzytgN6e12yAA9PSYx0/wp+Du391CKwUrIhtATu+ESg== 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 SJ2PR12MB9137.namprd12.prod.outlook.com (2603:10b6:a03:562::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.31; Mon, 17 Jul 2023 18:12:19 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:19 +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 v8 07/19] iommufd: Make sw_msi_start a group global Date: Mon, 17 Jul 2023 15:12:03 -0300 Message-ID: <7-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR11CA0074.namprd11.prod.outlook.com (2603:10b6:a03:f4::15) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9137:EE_ X-MS-Office365-Filtering-Correlation-Id: 5862ad6b-43de-4342-b253-08db86f15b56 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wEQEPFuxt7VxgkbPywtGbpGDbbiWAuu/0CfL4AA4dh1hXjyX4Xputb1yKSIgy6YTYjJx9VFxGt11XLYLwGkSrpOLfGWnCPBUIORJEP5I8d9OFHSvzaKgrsMieDilHFOoDscC0AWnHzVdaiVG0QNn9+pd3e5jrylSsI5urGTw5eVnxy/r054DQWVI9kpvazhBMZIuBC5m8/tw5IVC7/QRVctgSoaOpBCzw+MGpHuZGErQ9GrV/VzCOHxtkLUrbgek6CvKSujrJhqddshPYAyLBvUNnDdLLMmLRKmZ3VtLGmxaHci+Loj+P1JAr6IHyc2yYZY+QA4AugK+Vod2kjzBpyhoXf0ugSeUlFqT/qnZQCilW2TOfo5oV3aGq3iJL/HceVMf92WNE5/hglF2305UQ/6j4kkMbQpQ1wUg2M+SIKmixIctzMCs1CQT7Osk5/ztgL6//vl6BeuO2mWnzXyj9nPfaFxWir6shrw7wgLJN56walzqPfepaOtF8x1eC/neM7Em+ydDon9StDWsXt9+whpJm9LZGz44ZQXpXATAFRKjEemUS4gNSrunSt8PWsRGOz3ry3kUIuxc1rCWXWsTOw3g1eg2zeLZw68FSjeVNf4= 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)(376002)(136003)(39860400002)(396003)(346002)(451199021)(86362001)(2906002)(36756003)(6512007)(186003)(83380400001)(6506007)(26005)(38100700002)(6666004)(6486002)(54906003)(316002)(5660300002)(66476007)(66946007)(66556008)(2616005)(4326008)(478600001)(41300700001)(8936002)(8676002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: yI18imMNbljERv85OQkdvaIpFNPN6lLow/3GuBMPa9ceJJtU8Ja8+a+W+aqSll/an43a77390MNJJQlxcWYYNXhuGufxYZ8I2vDwG4tdzf3+yDH7cQtdMa7e11xXpALSyiaTDFVyfy1hYCvsMtnRcryHSq5m5TPw7TwzKToB9PV6h3/t+RHAauPhNImYHKJlxgBK5rLoMEjm6pY5Qkwtj/pbS0k4QL1wX3XTfx0GzwHgngni9Z2wzXLAJloQbRpbXub1K8uS39u0q/ScxMuuD4cJvlsvRvKI83GwBBn+9B0mXZFFD44GjjoOM2c2qvS4ErHCli1fFuO1YQ0008xXPbrxmIp7Wu9ZlRz7LG0H1gGcaYMhrPqAkYvYqGUUoU3ea8AcpJPE3AxpU5OhCCJj4SUfl4smPeYQOah+Aoo4DYEiZvtvYCZGuLKPigWDKVUtalmwnjHBSNZqk8/MFuy3NyIIhNADQDQulN1jPwTqMXcOR8MBEs2yni4OqOQLlI6U8ood/M3kNI9KjP2Icz9GwHec8qYlJg5M4MmH2HH6Da+nYTvNWUQq5as+bOG46nWUjTTKcM3jzgay4wYdhHkv+VA7FzCud8MyZ/R7QhZRC4I5D254zC/KVbgc1UTGDM5DBvN1OVgzAQQbSsBbJVHXC5SWeyXKEJzHKel73mXKLGV3IDO4su2ZslmE7xaiavr1qC4ucjznpmCLDYrbq/fO24OKQKAuEJ8Iqj2gOyoCXMNiJSp9SdT6v7XzUGZb4KZYvwFVOoN0jw+D/aPIsnuHj77Gk3Dbj0BK2c9lX2LVAbQoPKx5lRTPFe0nmBs/hMLk6CwtZd2CUMLFFVsI3pPsLdyWNIBVyIB0u9Z8C3B1N1yZ7cZMJHw5zARXuEnc+iFObbUTJgJZznahuLGd2llBzUH3NcplEbdS4z6+1KQFaNnNB70tUnm2de+mgualmuiqCZ6fQ66D0LNzWF1y3LXpOo419w81v9eZ3cy4AxQGnmoGVeRM2Za0Rz/IBE1n8lgUf0h5hoPLRb14Kd/lQC+dudvJxVvI/+r7E25COAaYQIiA2lhhB9ie7mVkjRlC1GJWjx/Vbn57SmGr2oSTGcy1fDha5GtHPYC9vgy0hJjcEzLf2MjUsh2qpQDgrfmmIP5nx/oMc3P6OAlQmTQHes2PvSjWjFtRutoFY4QecQ4nJKmAi+e6ht73DcGrusp1GZ0cnACHPZkSlxbRuyTgTciS4YR8hIku4GgVVnsJevGEm2zSEPZIectzXCO0O2DUjqv86NJ1mYtzFCh5Ru7zteMAlayN2Bn+PoD6ajAsAyJjUsE8M0nnb3aeYT2jBw0J//y9Mutpcgf7AoKtxplHMB/MBjzDJRwnhWfk5lrI5mN9M3wDVxVVqFXohRV7oPpE0YPZL3fNslXtz/xTrgz8Hpx6jfE6QY++PUisrjxg97PUBGtEM4x05okV5MY3+duaA0NoFJstbue66ol2ZFkvF9mtug6YBZzaE7Lw6f2SB8/Va/LgveHuZdjA5q1lQz4BUcpylHWSkNNvhQVLRUQu/5zaCqasFoSwAU5L0cDtcYhM8B9NViGvqmf+a7hpo8KD3q5+ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5862ad6b-43de-4342-b253-08db86f15b56 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:18.2218 (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: rqdRrkhPn1Pmyhtw3VQKxpTLloILru8DYXC0MtYAWqWMbASAY+PhHUHN41LNJZwZ X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9137 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 Tested-by: Nicolin Chen 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 b2504c612a37b7..0ca9fdab07dfdc 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 d5c3c627c0dff3..9ede238908e7b3 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 Mon Jul 17 18:12:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316184 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 BA8C5C001DC for ; Mon, 17 Jul 2023 18:13:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230377AbjGQSNg (ORCPT ); Mon, 17 Jul 2023 14:13:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231208AbjGQSNd (ORCPT ); Mon, 17 Jul 2023 14:13:33 -0400 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on20603.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e8d::603]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 007CE1725; Mon, 17 Jul 2023 11:13:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LLpfK5T6znm50Yk6cNUSz0rT0NGRHfshg4qUdJVmG3nAG5WYe6cN0lRq/MWkm+d/Arz8G4qB4SnMzAJ2RtCuGY90UMvMf/dxVlN1NfElYn+ZRWHKJ65/YiHeEarN5ca5aVTx7pYF1zPwVV79A6yTAbnKnUbw6DShE8tFgbX4WjtEJEyw6tiLkmjjOsCcBYpbpyMGotoU+Rir4f6fV/v4cqnvR34LFi/bIHvJToPJI9wQCkt2yjZ/ISOUZKXlIgzs5VE1I7hTtdI6E95TbEngnRHYy2itB1c2NUUaL6Won5P12qnJaznESZs6hUlvlGzctyTa3Cp0uut+APQS8okVlQ== 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=TUZiQxZj61NoJJo30CvAa+GoDn8iIdPqmROF3I7g6d4=; b=fae4Oe8LeFjiqAx4XGKj4Smkid1lF9gIhzDYibjqjfRj2RBAQZzp+DxtniF9xcoRmQ7TB+61gU97K+jYAJm8E5x0anFWOCteU8Utmh30aGI8pkLOcJHrPVOfK0WEgHkoEigCDMhJD+W2FNZtCU0dZduXnvaJYwoJ0DA+nuj+s3exe8cw4HTQU9NjrUdSHnVupklGXvrs0eu4I7pU/zKAbXuIm54Ljh474EpPfn9jVL0m6OjrK1Shgoc0umqvbaYjlUQ2jsu9hIJTE0IuRRQ7YULEkimfYwiWQJuUcdCvsC1Sgl6TPW1RYRdkzLizuAar89sHfXBMNec6vf4guwlUgw== 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=TUZiQxZj61NoJJo30CvAa+GoDn8iIdPqmROF3I7g6d4=; b=sB/NlVwc17qZ/AV9+P9dsCrargVuBoNx8pohiVJQHp/57lRDcTegoNHEVPQKjlTIPg5XI/2iCEHpc/0IcAnBRWJTVB3/4XKIhWrJrzDtTMC10N+3ndHNIRkwQ1sDeZ52MDXfOJJ62028obHzNQFATxeIa5Nm0rON6JB/WnH8eRgYoId5Rf7I0JMCqKrQCwIYPUWyB0tCWZJQXHO74XogWWdR4sTSj7TkjaArXZqRZ8KiuUvr56ESzUs9+JFobGYRu4bae/zaYMWpw46BBs9pADf22lPktdFcZko9KDgLJtFajxpC4yxDTFZg+F9r7d2I43L8BEDVAtWKamx8u1969g== 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 SJ2PR12MB9137.namprd12.prod.outlook.com (2603:10b6:a03:562::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.31; Mon, 17 Jul 2023 18:12:21 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:21 +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 v8 08/19] iommufd: Move putting a hwpt to a helper function Date: Mon, 17 Jul 2023 15:12:04 -0300 Message-ID: <8-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR03CA0032.namprd03.prod.outlook.com (2603:10b6:a03:33e::7) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9137:EE_ X-MS-Office365-Filtering-Correlation-Id: d10d9043-77c7-49a7-11ad-08db86f15b78 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: UP8QJK0DvbfcM8nh0c1yczfklJqwUQx/j8S1QByWsE4xxcl4b8hPm//2Bf620pRdWDxuTVoudUfAaZmSQ9+KMOx0NrU4H7H8b+TP7YMYdZW0cBstz6XqaMbLmFk+9jS7HyL+kYvJ4x7PuBuNXDtQl6YFDOgvwkqBHEZsKlLiVP+bB6xWgK425irklIvGgCLifLoZtIrnKhN4h0vul89qGUA40SCMKBN8zQPl4NcjLxFnrZDsN9RXwKkTPWUHWeL/PAuLGJuTKF9onIz+w73NUmiCMFDPO0uA7DyD8L2ZQ4HnS9IUMXS4QrTFdy6XVi3azg49C86BIelptr/8sO7C5noahUB5fFOxLk4cwwt6fDpP07P65EkYyiLuKXvC3tlqZGYHvYs8KBhS/dFzSLjcS59t81cL3AASYxJdSr8VXis1mKsXwDWX9gCacPisLBopBJ8pB3DB5ypHbPiFGdR6G6wAO8c6a2HiRtTiICt2EEnjqOVuQqYeMN2qPlZBo1/NJF5/HP8VA/nNQWxSEbNoDHIGFl5KIy8RJayEUJ6OvAhgU3FXHBQLjZPwFsom8thvKJ1qW/StDJLVz7DrYLE3vBa+Ld+2D+fatWq/jOAyGuA= 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)(376002)(136003)(39860400002)(396003)(346002)(451199021)(86362001)(2906002)(36756003)(6512007)(186003)(83380400001)(6506007)(26005)(38100700002)(6666004)(6486002)(54906003)(316002)(5660300002)(66476007)(66946007)(66556008)(2616005)(4326008)(478600001)(41300700001)(8936002)(8676002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: BrhOTk8do0e7Jck8W0uPovWTbFUOMnXBsmYgN8ZK69UIm5PUEg0nKbAraIIYlRoklCYodAiQUE2VHyfQl9AIBLP0JwE0J+EAo6s+YyQNVuxYaz78oBpI7JmWgVZYgNFqcChHkIswaAjoZ1PIuhjgwTPsW7XXm1OHx7giccLLxjvUuHE/ai8Uri7+U39MMBZqBLhpf7R7DFFvnZFRjIdJ6iqgAv6FEK/rheIRNjWQ9jZBBIylcLgiieUY+5O8lKIIEWfGujjTGWe01CykxVqjEY1MMEBVKaAeYCpSDiNG4nHFjM2S6o9EClNijG6YF4vTZN5YZKj1QJ5aa0XBxS78xsKW38pJINcHUEZPNkUvT9rxXth73vQMF2r5x5ZtY/ls8AXyj4R2+ZgIkGuDf1S4jbyDSbLf17knD/aIXqTtHou+5sLa0MnKLOAhWtnU3prYLJRgiZ8/Rp3W8Wpj7QqmA92PLWVeAQaie0Ac2p/VGiWh6m1vcoXilrrWtCSHk3gfip3nToJaFW/TJjgOcSaJ9MUiqBo92dzHVFJLbJAB6wtoPb2iozmldBLgudIcdlBBqJxNT8Mha57Kq+DBHO7hIh59l80ShnNyzJmV7YYVceMRnbhqZGcu4E45hcakub8Wqpta32e74NqxaPx7hOTkTIp1nk6mgewUzET95j9OPBqCrH+K9lpZqRaV9wOYURmXefy1DD8L/4eLFPTnl8Kh3DRBV9RfT2v66uV0nayNCf5SodU4xIQc/19ne2JPUIggqQc+YB9Gw7bUIO2NToXYzJeUWoI7pQLfcC7nlMlDf+bXxbR1yQ/enHKCBQcAlOWYgmye+SBFWvNNwPSpoatlGkA/EVVuWaCP3SiZhUzlJ7BXb3/GLf2cJK5eq8XCfZduIaT9IsaklZpfc8+Yib5A1Ex5WnecYn6GYD8XoEEafpVtYFnGlRFgd1ROFld8nKjGuz2KNPW97/jNGpODRX4fBpa2hTH1aqCfXFccV2mxGz5I7dxm9CqDc8oX6qvUOWRrP3AFcrmvvX98SctOa/gCGqbgWOwMNbiHa13Ejcl+GCD53pBKYurN40HE0IpsoVS6uh/hV4/1D6I3Ft3krku4/Pv50KcFV3UV0MDxi78JgQqjzNmaLJdqvPcKd2J9sqb8WJKdGCZCgAWNITG0CkMM9X6mgQTRBwvpexblujMeAbex+X27Oj70CpeVsTdAcn5+ZBMJ5TLxvAlUhzS8P+VH98yrb40TvNooSJ1TTen6+BBHVxKZC0yMVdHqs3Mg3pBwsF1WLASk9dLcZZHIkjE453/Y5rXqCXkeDbiD1vSqYVleyj6b1OIGAfYZ64KSCs9gZhtrzcVel1b5FEfd9NVJoQm+0dshi6VdTaY1/yYFP0aj6nBwzOzP+av7UdF7qvZDgBXFfCqyqLKpg9O7Nw7BMi0PDgFc8bxWUC5DT6Z/Q289meJxNNk/RZMTtIWW5ht58+NXEAWpcscK8tPMLxOH4kvZptoqmGA6dje3n1xLi2JaXkS3vEBVqTLEssaEs3P0Nn06ibc8k9PCY/NJ5AGzHRg3OCdNJa0xrP2tRtPHAEx4j3hSlxQmVyrYAM0SaS9f X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d10d9043-77c7-49a7-11ad-08db86f15b78 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:18.4221 (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: VSQAZfYDkM3mOUQVEoFvM5Ty66oG54aaFRt+3bcKhosDLyISL/wcqJTd8IP+Ph4c X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9137 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 Tested-by: Nicolin Chen 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 0ca9fdab07dfdc..7db5df11ad9be3 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 9ede238908e7b3..63b2587a37f23d 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 Mon Jul 17 18:12:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316178 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 2D8BFEB64DC for ; Mon, 17 Jul 2023 18:13:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231987AbjGQSNR (ORCPT ); Mon, 17 Jul 2023 14:13:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231797AbjGQSNA (ORCPT ); Mon, 17 Jul 2023 14:13:00 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on20601.outbound.protection.outlook.com [IPv6:2a01:111:f400:7eae::601]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9CBDC19AB; Mon, 17 Jul 2023 11:12:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BcewdVyatxhh0/MxUzOXvdkwQmaeyyDDCaEnQkHJHrca09s3QaVQxeS4xZSx9sPUaSK/XiMFM2phtqWG4PQJZTsS1JOiSeaPdPIj7hrRgeJJ9TPr9rNq7uw20cvas8ny6at40wnZMMF28sdDCvb/uyD9Ast9s9/Cq0+IUQbDSfZbuTKbbD6V1KO8qLUc6aXcSJa6MzsPptoecRphQgzlbTugZ4sTGL8QTBgbP3dJrxSjxDeUpFvCg5I9jhRuc554oz8ofYfTl3/Xo/PkGLvs6jfumc0wYAjVb3LCqlVHjCvZfnAF3Hb7d3sXCR/SZku51Oix3yuPAKzuIkleYYDy/w== 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=59+UqnDsP73Vw9a2e/KkthiRq/RcFwX1H/JPPbAUIwo=; b=kKlq+TNDqQ7AZ6oDkBH8zzXBv7zPE4pHW/wBj6+xEuw/IZWFgd4//1lFXsQ5aamZA4vg19N/r88oAQZA5J/Z5mtSUZspquryOI1Dx3n6LkUI1gIrM/rtXdomyUBzUrd/vLKi7KE0B8FFFRKrCFKLsLBOa40owAjV89Q0MMNA/5i8izKVEr35awKXLs+WX5rPjzXM0XGF4Gb+0nKH9jlulFhDQkRptYtj/eRt3vQu6StcIw28chMK4SzgmVYCL97z9V0neJFR2zF5g80EJ5q/H0TdUze7S0bppjoP5QtgfrUyxr/8hu0yEKLpl4CJd1lVqZm76eaagb9pkdIk9fqdlw== 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=59+UqnDsP73Vw9a2e/KkthiRq/RcFwX1H/JPPbAUIwo=; b=R2k19A7h58GKY0CSUC3VkdMG+jjllwLAZ+zPfh2fOu+hdgFIBLLBJ8MR+wW5DS8Ou1Fs6GuhEhUCJ0zCEWfPZNdRuw0wwhnin5VW2WkDsV4Lo1KrJ7Kud9no9811+pjOmpmbvBtjjzS7ZVXmYsHIaUdP6fHrmxFPyexDNX2u3UDGVKVrenduoKVKgGWUW5gqmPE/Tbi/iMXWadcjh2K81lIDx4hRKeHI9RNmuW5OuDC1jkdisr1qQQqO52+Ehzkxw3a+GHx/fX2z2ofO57X4ItWPAUudrMg1gMNe2hFFcMk/MhH7LHaOqdPM7hMFtWHkRIc405r3aCU66x98qh2AYA== 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 SN7PR12MB6861.namprd12.prod.outlook.com (2603:10b6:806:266::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.32; Mon, 17 Jul 2023 18:12:24 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:24 +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 v8 09/19] iommufd: Add enforced_cache_coherency to iommufd_hw_pagetable_alloc() Date: Mon, 17 Jul 2023 15:12:05 -0300 Message-ID: <9-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR13CA0016.namprd13.prod.outlook.com (2603:10b6:a03:2c0::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_|SN7PR12MB6861:EE_ X-MS-Office365-Filtering-Correlation-Id: a494935f-6218-4110-ee86-08db86f15ceb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: pcvE2C92i/rssYJQOf57k4ssKqcV2uxQK3rW1hh9zrmEge8rVrCnuEV77yLdS1RldNLsNEtTk27WQwJj7PCoUv/0W/fbLvkeOoYpRFJJTCV3Ms3B67azvIbYHPcT04po9Pr+KR7R/qZeI1z8t1bmjz2HqqxjfQI8h/vG1QlywYsGsgLhBNAhy5ApQZFdwr5MPgKP4Gg6ETvt2hS67SXEeQ4RVmdBMFTnnPhBJDCSfTeGuXciU+Vqq+aFIiOokwS8u2nl8tCyft+m7Ud+c3Gwhy7vqfBFW85LguAFCEbPNygHYRbuXvY6V7xm72KnXnyTDIRuWItVPEHs+WZzGWvquqPmCq8+3A2HTk4LuQCQQzxBSLDQJ5y9v+WjdsKLon4x5vza+BDsZ4GCL9vhvKFVaS3t4JXcht8YQSW6qs3KCTj6HC0xN9C8YSxVQc3PFMNLGvwIOHs3L9RsjVNnYmvbvboJEyv/WpK8M1ncoeZRdA1LSHLbam98pDsQdn6Y0erkkvq4oy75lkfB/u6/3A+nRhFmhRPhf0k0lj3SVZV1adbz6EQuX0NUFIxlQXFa43/H84bHgFB7dkb/6cPmeMteIaTndsXEyaDyks2d/SAjpsw= 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)(346002)(39860400002)(366004)(376002)(136003)(396003)(451199021)(316002)(41300700001)(83380400001)(6512007)(186003)(2616005)(26005)(6506007)(6486002)(478600001)(54906003)(6666004)(4326008)(66946007)(66476007)(66556008)(38100700002)(86362001)(5660300002)(8676002)(8936002)(2906002)(36756003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: TMANlwnrlFYu/ppDe5TrVPlxDFmiuPTQzHUq3yMEhIkmyqzXkPfeJCF6LB+Az5Z7pzJEl8H7sKYsRSdFUOSe43Tk6ET+vTf/vms6JLJNd6wfTRJOvYj1jRGMv2hvD9CsepwXRHFiEi516pbt99A4l905TOLe83IgXbFVtG5kOMpdktsO/iTiTau8b0qNbGes0fwxYx1VbO7Bcc/+PuK2wBnywtrvyeJA1rgFEdrhAsnIgeXEUVymjwNAPlUTdOpQ54nprALvlfMGxqc40atilgOOAaZiPMHaWawsdurCASrl+phT/pdGwJGqrgq7w9E5wm2n+plZ31j+RWukALca2n36gHJFKmx7yzkP4GvUvujv4wwvVwjQBpnckN8AD/p8YoaSxOf96j1BmIgBA6B+l3jZhf1/t+J6dfU4BDANAa041xbpZ2PYwzbMkDODdIYf3WWLO5/FE1nosPPA4XXZ1dF0/joOucgy6kO1j3Tv7eV6CzDT3CwFmXlXkOfdJuU5SITWh7+vZ4HQr3YzsQp2S0bW80+J8ejCb4RJmRcQEK0wEKVdUR7zcHbb+VHfnFrqBuehUG4ykYMSDEt/hOyl+213FYTQNR6DCs5qcKeFRL7SwEY59cCvchn2FVolDRqCupNf68NrcuDWZbfCLsyepB6s+DAD0bylg8OQdCTsQLs7G5iv1Fl3Br1wjMtosx0R3FXxR6drqkkD4ywMEQ9FD1eXx6K+FaVvzVS/GHn4Ezwkca5sGtLlMv68fAIfh8F2aXQaAcj6ZiwF9JeOGJNuQo3MsCSLBV4X+feZ8tDFBJL1s62h1Omn8THosNJ7L5kg0HVU+6NMphAZ/xGGdywtpKyJuu5uAcldngo6w4y1l1A4g2qAwJSGl6vecaISS0ohWa3UtuYGIuDN79EwiSqM7g78e1wanJ5ESWw5jQdfqjWjGc+BHbxiDDXJA5BbmAGpinXVP3iLFQ2zEvuglWsRqelO7qY1T9WVxRxbCD/imcMca3p4zP/XNQ9Z6h3NvVaGgaWcxrAT0BkpVMf3kkFP+96xo5kOOIbO4PLeuJeEl+R1JpNo5tHkV64oKUqS56+n0M6Tp4SbJS1VwK3XKn03cFxPCoBg5meneAOwCgtRI0oFCXki2wMY4YLNgjNfmV6uQDIdsIDenCuI8by+dRw5Je/s9ErMNYH90Uv3OxceSbIEmhgUeyx4HHZfou8IPC4cqFhQOhhdrS1joJ8vVi9y7QQrutfokRgZAyW9ORVqHPjryS0G9BG3+syxFrf6raxcIefxnozf349Jq8nSLeGAvsEMR/aOqZGUZDYU3OggR6BcIC9aHGhYU14tT91AuLMEAGFVeaee3Xwaz2Ls74hRCp2aT2v1FeV0ILK0rRx0yj0EPiTyUqqO5v4QXOzB9EdVzms4nLFj1xP4/A72qprJI2tYXoNiVDGj3vK7oNd2lGWHnVTleeDRv8jKXRdmdLSEBwewm8eRHTPwSP0zjeA1E0oQe3vQ5WMZKGR/b1xu7qGlAinTg9OJLt+yBoBAH7GSivAIIz16BCGvA3eU9OoU+FVGsdKtjzWB0nnOU7KjVI2Afc4dzEuEfY/R2HbdVCPd X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a494935f-6218-4110-ee86-08db86f15ceb X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:20.8165 (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: VqmKHdgi9OYjmcFk5qQSRo48s/9+K0haagRJi6W6ebzMpxaeAgDfuD7wlhuLA2Qo X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB6861 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 Tested-by: Nicolin Chen 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 7db5df11ad9be3..d41e34d1b778dd 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 63b2587a37f23d..5ce9eaf5fb88e3 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 Mon Jul 17 18:12:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316192 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 15083C001DF for ; Mon, 17 Jul 2023 18:14:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231630AbjGQSOR (ORCPT ); Mon, 17 Jul 2023 14:14:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231621AbjGQSOM (ORCPT ); Mon, 17 Jul 2023 14:14:12 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2065.outbound.protection.outlook.com [40.107.94.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 859301986; Mon, 17 Jul 2023 11:13:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=D4DoImQMgGxJoixeuajf1wEmKED5v1+nqeP6DNDbhLrYCQH6F8Er2pVn9NJhSC4Hex5cnSV5iYgYGySFmfg7zrucfDHecjG/mMFCMyTJWYLeDKt23LODGjDeYp6mL1rmqZ+ijbmyTHy1HZiymIMTXFcp+lSdFvrEylMJ45OZo2p/merSeSDQbeDiXaH2I+wIwkTkEjWawO7IRZziBgUJYdnII996cDe8xr1NcD+vToaJ0DlKRGujtiyWbwQCnkDDLr1gos1FzJyoxyS/Hl6lfunOdWHrprJ6wAPbLI3tzA4iOI8NO7tOg6U6KAPH2xqtD84BfUXAXQNunyihABuKwQ== 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=c1vpWodbIhaTwTUGrA968Gp8kCDO6ddJqInRs8uVdsQ=; b=DYTugerxyBa/frCNRDC3tedd6OtahfULe2sl0FwDr0mKnOhNCYq2/b4B1VRqlkuW9kQa72Itu5tLVsZvs1TUjgabtKIGGKxKMftu2SAHWZF2ILXAQvSKVqdSD76rPv/Tfig55rjivtpFRsEmMzvhfuJbc5+DHTxEBB0G+EoqDYbzBTkeE9tGJlmFTxk/ef9f3Ba1jyQNr0xgkhvE4NXlpfy5HJ7T9E6o8xbhz/dOMOQif2CI7UUdY3YXs6ZnNTpKNpBMuPKA1rBkupbUdzYZA8oDYkv3eAp1iENbbMLA+oQuZxj9J/bXpoJ+TFZAKhJAlwAKivxFtxGv22sGFUIl9g== 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=c1vpWodbIhaTwTUGrA968Gp8kCDO6ddJqInRs8uVdsQ=; b=KiLrp+y1TODSGqu99Okic+irnKWfo/Z8aA9/lahJbo+R3AQkog0Ga1zJfKMIMsGoXeOcS41vKsoBJRkBGBDU4DQg0kQmVoxIaQSDoydVnL6IkVe5ojr9BQ84w6T8IKYcCtyTcjdAgbq7/x30gJXabeNq3w0RN0JmyEmKaUUOLOMM9E3l5mqbdvjZttvqvOLmrepzFlAcIMzVNYlpBRk4i9u9T/a8dfAeN8W8X8VzahQJZcb9nCHC/iKeMVpYzz8p1CsLZmEXEXO2srUMlowscb4fkxPt2or+D7tkqwDc1GxharzHercq9YFdsqx7TJcklpJmWQww/OgnqC8JNqUrww== 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 SJ2PR12MB9137.namprd12.prod.outlook.com (2603:10b6:a03:562::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.31; Mon, 17 Jul 2023 18:12:20 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:19 +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 v8 10/19] iommufd: Allow a hwpt to be aborted after allocation Date: Mon, 17 Jul 2023 15:12:06 -0300 Message-ID: <10-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR06CA0061.namprd06.prod.outlook.com (2603:10b6:a03:14b::38) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9137:EE_ X-MS-Office365-Filtering-Correlation-Id: 1b7f639a-2076-4f29-76c8-08db86f15b5b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dOeWEdYssOHYz3wNRhppTRL1XPzwvq+QdWEV8f0KKzu2OF2i/Xu/+C50cjAvFD4qtN+IQAjVoy1uM291RALVAHMDjWO7Sgoi+1WabqSNXJ1AXUTmBdVw3AGSyL7ssM2F/kuZZnrFvkqECJY3A3rDVJi2rPoYmNDI3HYGh+dHrxkUY/t3kbeaXPAjtvWjng+QA0OxvmWe9GXpBO1nqMpW3R4t2dcFM2BPnXFT55GFHhipOCaTqeKCpeV+YGqQLMqhWidb8TwsJHZp1f/Pc+eNVAXqm6+fjONX9Krv6FTz87ted/T9VjxsxTuWvxetqFPkQQxXFu1hnI1+QqPpC5dyUQjLEiwUoacsEbpUUdPeS99MaYKnV0AKC1ui2BTREKKeO3hFk9PPQ6/H7IEeaFUIspFZlmdFv2MyPsLOHgEOnsQKxRg3qYDkk+PyvZjChwwoSoHDUWDZIe4Li3SHLpDwpnpcCHLbe8nXK7BB+/mjYBqlNb/iaBhz99pnsINAoxDdOo/VnYoy2DQy/+fVwLe8JkhvFXqB1BFh70KPpwJHJegEup5S/OXHsG3KVIlzzgxZ 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)(376002)(136003)(39860400002)(396003)(346002)(451199021)(86362001)(2906002)(36756003)(6512007)(186003)(83380400001)(6506007)(26005)(38100700002)(6666004)(6486002)(54906003)(316002)(5660300002)(66476007)(66946007)(66556008)(2616005)(4326008)(478600001)(41300700001)(8936002)(8676002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: hY3zPhEqrQF/zwcm2Gu8O7Gvf7SdYyi0xJWZkDoGe9WUEAqS76x5SLZ3/VPPYGBPVJmr/XhN2fpIzoky30CDv6jwyIs78Rv/DmstJZDoTPuwdreZql9BP5vMmt89NeaoV4Asan0pChItO5d24ElIrNeNLXh7mQNOwxDN3XS75CkU9d0zTSFC+fqGY6AO+xzPsqqIuZiyMuAkDv15oZRQzWY68v3y0sD+MV155unouws7I+I+U2GhUdA6fTcuDUSkaqvxAVOtQ+7klZyGGUrdN+ULrVZ9OUaSKmQWy25Octz3HyU2LfelMe4qY8YhE5FGaNXrAf2Yke2xKiY1HO0pVK5V9zNDMIYlW9d/5VzWkM+/zdu36B7TPaRJS98O18sqvXC6JNBD9rI4kACePFJW8Ci00iV83MRc6ahGQ5J1L1J3Zqxy4kUxq7weL8RzaGLV9MHX9nzgenZOaQN0uWvn7G62GUQ997LMZfXu4LGB+ABAAJQ4yN9kXL7DIvEXe2wvEAHJKYgoPjZyDTTqMLSR8bvjmVlx17q02Z/H0FLrBvzL96cjXi9MDUk0ezWqdXU/p7O58pBAU7ymoeYD15Wi87Zu295HGsGNs3VRGTdGU8pxvMunnl7CUbWWp5dSJwj6a6713+NeY6Bt7/yiAi23iKsMfIiPecfvlK8dsEW+YYVpDdpTCPO3gx3zEF9hse2SvbUtLCxt65ahRgsf7sGYOh74Xh4y3eRulbChH1HepTYh7ZhZ9tIpjIOOu9ORzsOz8rNggBueDfNUf0pHoW9xivQivmAMg+htKSgCELzyjbiJC6E8N8epIHHwu7uvVthK8TwcmyvsqCZ4/3pm3OGbwLeMz5Kzzmh2EnhCDvTt0VFUvQEmmN8lmOzKfRwrO+tBBS2B6K9SZjN3+dbgRx5uN5RwOx0Q/zCncFJ3sHYzepzY7Cd91YML0Uuge2L3ZNPlNC6viex3XtBl5p18hreXSisSmkb2gIOz5KdCHF8xvefQ+D9MOINgbV6/LUXWd6xdgyjyPc3auJRVHKKRQDPGALICbP6G/UIRpxiTxNfstGbbmjZWQ7Y34DAX/ruP5V+ntiK+mgm/nzG6qCN/QZYG3ie2H+nBEhU0NerVLP95JuoFwt6j/Wtd80df7EQxhFQTg3+27lNj83FP8mP41B2ocdAmNQa+kx+fR67LYkvyXkYps7uqWte921RIpU32EmfuxEOc0Nad/UbWEfAAMT7oTrHwTnudor60xOd85c12fU8BiC05hPWWz5TI8aLg5JnDa6FPqpKHawTbsB9GYNN2cKdq6siMOq9YTxFsN7cC7Q+mkt2NxVtaiTI+UvZkeMBXzS2mMIgSCIXV6HZlpTo/tTrhOUx74e9NloVYBrUk8UKfK6Kzt9kS/e0/+kSC976l3RRLeJUd31VpJfUWNNM4VEUwFXBMLXwPETjqeU+f5GxLVwHkv0ofXdsUffreKTVhepPxClWsf1R1SbjT65/i84s2g8HJvRa/BJrBk05Z1mz4b1INKMKUqTSqsWjVaoc5iAZSjjiKFHHaWQGnQYNwxTWvuS0M6lmtuDTwmUVKkAv17UAM88wDpw6OywvF0cRz X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1b7f639a-2076-4f29-76c8-08db86f15b5b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:18.2438 (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: 8XnE26yCAJwx3mifB+lLRIa4z29NEvMddF0Ql4NP4urJILC8dbT1n5oXqAX733Lh X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9137 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") Reviewed-by: Kevin Tian Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- 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 5ce9eaf5fb88e3..07d60d0640fee3 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 Mon Jul 17 18:12:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316197 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 8895AC001DE for ; Mon, 17 Jul 2023 18:14:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231818AbjGQSOo (ORCPT ); Mon, 17 Jul 2023 14:14:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231688AbjGQSO0 (ORCPT ); Mon, 17 Jul 2023 14:14:26 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on20601.outbound.protection.outlook.com [IPv6:2a01:111:f400:7eae::601]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0716599; Mon, 17 Jul 2023 11:14:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PtLB6tKQn111eB3ojTAnJrMiun3Dlfl9gWRyW1hpfK2KytKt4SCd+busgEJvwn0oUpaeUbwV8omI6C7ssr7nIjUWSnC5n7xSBUeZ6UKMRp8bKJo3GcbrUh1ZW5Bj3c2UjGU3COhZSnCUA0K0A+8Qw/fbSeuPxirdQrfEeChOqHRM7pI8O5V+lnPHLUfj63pYp2xj6QJ6/6udb+I0XZOSuyl1T0OxMliBGgXwzsulCx5RiHcb6CJ6Z/Hm918B2GMMimsMxj3LrR5WecbJHXaGhqyWmouMJES6efZn87TtNCxjxAU9lCfjWyDAJv79Vd0DQ3VtlsfwS8v1Ulk2f4CsAg== 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=uN0qA8U2d8lz/z4AuOr5qtTlu7B18EIn4DYb4x0ocmY=; b=EF4wOeJwp42q+3Vx2F5MDm9VN1/EiNJIEBbdSYEayu7SDNhcLPUt7hAFZY2uuJTyJcDpMq58hbxfonwdcTgWaeAJGUWWBWINNsoqhJwKJUspZMcUl65ucsvmdxhP6XyVfsAShGeloGR0ebi1vprmIEpg3dbIRPHm8BZUzISHcQcYo5XVgem4t40wbd2/ifR6z/7j5uLcXvVPe9KH0ZvzFQyXlUrvtfKbfZSBowWi6HQHSOCvV58JVrI6ZurZZGnRlxzkGJcksIsQ7tBOZgRPDlAwhzOAVg/c5NK25nnaiBG/PGar1E8M7cLNee4yw42Hadd7k022/PmKqS8DfglUGA== 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=uN0qA8U2d8lz/z4AuOr5qtTlu7B18EIn4DYb4x0ocmY=; b=kBBfdLp+4e/BK7wWu1G7XYCh6XWKtjEpMKJ4rTD9ifIEKK14aTl8oLg1dfPVGBtyOLRl0ufvx3YOdkkfUj66HHZ2Z4rLHsv9DG71T3pFKeROWcg/K8vGBIiVKc3RtSFHWA0MN4fAcaYzF2MG9we0s+gMihSurruCEo3kuWEI8W4c5jl1QQnrcznRCmqkOh7GXKIjDe0ZT/4V4mWHE4NGSy9gI76Lux/w468kQEsoy5RgC4h3IodwVmuTmy4zAxjan/kULEKLsM0rwRLcu1/9lHZC2QrTuzmN6d3SCfl8QbnAT9niP1AKw5FavZwp/LmYbFyCqVgwltqNeuI6mY32dA== 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 SN7PR12MB6861.namprd12.prod.outlook.com (2603:10b6:806:266::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.32; Mon, 17 Jul 2023 18:12:31 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:31 +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 v8 11/19] iommufd: Fix locking around hwpt allocation Date: Mon, 17 Jul 2023 15:12:07 -0300 Message-ID: <11-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR13CA0209.namprd13.prod.outlook.com (2603:10b6:a03:2c3::34) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SN7PR12MB6861:EE_ X-MS-Office365-Filtering-Correlation-Id: 58f1616b-c71a-47e4-fdfa-08db86f15eb7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sA36YYBjwict1iu8Fs02TOJTnuajLi+6SPfqNsn2yg6HHnpJRVSF5MYAv4r5cakQMYO56rT3r9rJncrEudPwC3cVtmlWXrzjMI618OdOEKuOkV1Rpx8ykUn5kO+ZaUlehf+BxGw+5+dwp14zyX52lZS+IpXDYnqlAeh7XzBx4vjtMcgfmFQiwIOpi2ria076Zn0lva8MzYcf1KQ3alirP+Hw+sUa1UmZzPy/jhpsZ2ng1TarOHjPpBGTHr4AyMcAlvGqPbPDowyqb24BqBXlPIRL/4FeEi2du3ytZmW//+SCl6rGOqehQx39zxnGu8Ja1vC13erfkDqV0X5yhTj5VE6iD4KHvta+ATzpTFzvyGVMWn1w450QFP42MLySDVzABKYEAivKmAd9Ijl8740UYOp4xtb1js1kfat/FkV1KRbNHGfYz1Oz2F24MwR68shqUSAOhxjYLysxd1Xgl6jXA1GlRxKpNsXJxqt/i0SmjwsSvfIjrGmVTD5nwaaj+uWaLLZPP2182oEvvYlZ8sojiDRX0n/1u6kC8fUZ7tuCP25IvxMZ6R2TU9c+J9W9vk/M 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)(346002)(39860400002)(366004)(376002)(136003)(396003)(451199021)(316002)(41300700001)(83380400001)(6512007)(186003)(2616005)(26005)(6506007)(6486002)(478600001)(54906003)(6666004)(4326008)(66946007)(66476007)(66556008)(38100700002)(86362001)(5660300002)(8676002)(8936002)(2906002)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: CNGxoH1OnbTmeWWKqAerWTAK5AoQLfokMzDSnOHBWrFo6OXMZ6yz/HrE2LRTunWXStIxFo8dJ7fvD/nYG+6395dKqiixLNkEnS0QUSlFEp5SFcVB6qxdDF7JABJzN30P7mahbuxUjonSZfWNiNX25b1ZAdUgoFiKkQ9JE+9rJk/RnXZ4XbiVrhNfV1wnZ6KQ5kW6kvzOZv6/vTgR82e7reEEJrvRWqB2tHJT7ewCZaO1GPFHXB0pV7vFtMN6KKHfi2F0UGnDXlrkShmsIFPIADeYh6/wfC9unQ1LbbiNm9UBCN9LvnDBbFDjIVcM4X/OwqOoRLGDRl0MNvrMqhSib+rr+9sb+kSGP9k4DoVhw+aG+A6skEFbbutoYRKBGsgffT2d2/aE3CyBkJfIDvkLdlGaOOYmtgvFgrdAjM9xTlJDQCJfJyegEQ197ZOxTA3qJHxSHc7Ek9HS/RDuHH/tJdb1Em4bZTuCLsCqozq8XSTeRxJgEWEMDlMUL/dzdBzb2zhY69UeavxEuu9ct/klNOfpXi1G5WAl4SgNpnaiQBifvPvzmBEf2ZfYyjdJjyT12MTklGIYct6wxXgjXvL/LjMema4yh7UT4tfLkqID+VBLNoxSecqMd8Z37yibLbKCphyLm/OAas8tbhh17zBsr5hazDNKlKKgLTX7RAZvTkm4LAlXF1JqDtRCyCUxeTFNy4RMPPORFNgfO/qA5pusEaxFkXDeJ/ifeW1f77YOqxWDa6zE4eUib/RXe3wEZZVYdnnZotYoLKiy8sPFAK5ZmQT3qLYxyAlAeJPwuGItM/F4Mf/N+5jOSTvyHFBM+sAcDDYI0CdTiHE8jDZNDs0xZbABIrNVefW0hvgjStUEMp1WJYvkCRHUYqEkdxaP229mBGYxopiyMVyxrqGDcjaOFqxink5YzSXazsvN5o4RPVA5RoX63SWZe/1I9C63AswWyU2R6qoeqZPw144Mbz/VUlBB4AflGCyFZMvjep5sS2AT/3AfFPOqJgfNiPaWFBsqwnuoqEqlvUe9ixM7wb1tWNJ2dkSKQG+Hmsc324b0wUd0DupY5FZTeErDJ0cgwHdajhhqG3mYRc1NQYcTrskvpUR/HblVDLBSw3qXmOyJWb4hS4KCeEfh7NdoTuGtOYlM9T35ITGSK1Fwm4ZndGlnCwmHsM17io64hW6wnPxYyYrBaLJmb0NzlfFLvZtE05Ify5s1lOpXc5llg+usMIy0fekcN33aGZ6GbDaSkSlcP16EUHnkozoGe+x03mhjxxdg4PNPjH4m3183/DsklhTSWfeNzTBvaAWCxfW8B3wh78CRuxQYKJu6dITaeHvs9lE5EjaEmc8j1nReqy0/wFP3fpInAXbSiqkqMX460FHxMelYywqAOmoCs1oVl5qgsdISzfyl52p2KbQal6iqMmH1nLdxmDxQaHgwD0PjqjEek6tsBRwwJBCEuowAeJ/YHGuVH28oKOsKxWl5QWNbQvREYGrWH6fepyO22Q8YeRD67R6BsuPcrekssVu0W7vJE/AYr1bsBFRvrwKwWaH7H2UosoyoArxNK6e8lDhancKICBHIE75BYoE6YuXmOxqKOfOR X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 58f1616b-c71a-47e4-fdfa-08db86f15eb7 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:23.8236 (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: +IpzMN4DjvfLfcK6csWTuB0jhBqfFQsV69wg319PEsviLe9zf9o2dag3GeJhiNxi X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB6861 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") Reviewed-by: Kevin Tian Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- 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 d41e34d1b778dd..f2b34c56d01216 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 Mon Jul 17 18:12:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316185 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 9C9F2C001DE for ; Mon, 17 Jul 2023 18:13:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231348AbjGQSNh (ORCPT ); Mon, 17 Jul 2023 14:13:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231283AbjGQSNg (ORCPT ); Mon, 17 Jul 2023 14:13:36 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2068.outbound.protection.outlook.com [40.107.94.68]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8BB1172B; Mon, 17 Jul 2023 11:13:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=epzFFrPXHoThPgfvSG/1Dkz6q1w440bCFY49z7P2ge15ONNv2MnaG0Srgp5g9ArklB9wi2cd/sVljPd/bX9rW0+0tTO/eXSq/2VOCcxpnsjprgbUqvY0j7PDroU6wT4JpXOWMgcnpopZn5jmcLeFHUQ/Yr/Y42XzkD3hK2t73ybvrN6LxlMiQeO5YG0ulmcmRriXE+quzHmgLpH+It/5C/XAjXWk7Eg44AXCFHRuoPK4tVVwlg6Vy/TwT0Dhgmj3jRWa6T30/vqurP5X8NQcM1YWWnDt7JQ3nX2UfhW3HGJ9zg5qrMAE8uMYGgV1NvNxcE1lYROOZktFnk/bsC/4UA== 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=NAaGDT7OpvBrZRHecD1g9zcOZQwFgtLl6th7riYqQsg=; b=AIvHidl/Wa+tp8ryakN3nde5dR0+5YdvTeO29Xngb/GjE+FLM5szuN9Si32EqHKyJqKl0vRUh8XGpLNayyoQZzA4GCCO0+J762yGvYpsAHsfpXbRxe5MGOOOQMQHD41FCM8XyAG/0EgiSNU6vJ8RVwp9HJ6qPDRhv1GI7gCHjNZCeMu5ruhfd7OLULV0HTKQvE/DONrmg4thQ6vkbQdEHmf2ZpQCMbpEUWlSn4S/kRigJ5Iiti6ybabBG6bCQMb+r2WmkgeGtM3rTIERjbQ+ckEA7Fi8KJut4OWK1XVUw+x00ApLA8cDdXvKZwNK2Dw7PEKkWAuZZmySL8waTeBNew== 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=NAaGDT7OpvBrZRHecD1g9zcOZQwFgtLl6th7riYqQsg=; b=WyrJACDjrP6VsNL7c2KZzTOJ8Qp3lKkFZ1lKwWuCJfEtUjHwVSxUsJXhYVz4GoHPbO4JpbAMOJqwtpPqxJEqcUlY7kDXSnvqZIA2PBNJNB/gUMzOyj1R11IFBfluV3fp/aHPKOx5JCVQUtyJREAI63wkAcmf9KiGp+YKxn/RvO9DQlo+vJ6JM2atp2Kn3cwrqO+X/xjjifyHdQLR3xo29O/J0SuU+LjW3HEvPT+NC9OMPWqovL+uhqcBnjB27Tr3C03x3VrXb6Yc9GNseCXpdmR7OUip9wti332XKwmjj2hoM4uq7MJ3EVpOztMIpQPUi4R6JAIJDrBgPY4Vu/zDVQ== 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 SJ2PR12MB9137.namprd12.prod.outlook.com (2603:10b6:a03:562::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.31; Mon, 17 Jul 2023 18:12:23 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:23 +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 v8 12/19] iommufd: Reorganize iommufd_device_attach into iommufd_device_change_pt Date: Mon, 17 Jul 2023 15:12:08 -0300 Message-ID: <12-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR03CA0104.namprd03.prod.outlook.com (2603:10b6:a03:333::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_|SJ2PR12MB9137:EE_ X-MS-Office365-Filtering-Correlation-Id: 1f898ef4-5746-4bc3-be60-08db86f15ba7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: i9fxp+hKjQIAcHw5SllBfqZ1zaBQ9LxkO8jYvHxfWcd2dD5WIbnXJ3ggSwJJwj8rMuOIPNEiJHVO4CeGxgbBAkr9fizQR8fFynVGIOrSWp7PRLGwOAofhK/0n06PCFAraM5sRcRDpCdtWi8dmy9SFzR10ImwXNUfTM2U7lhyFH8qeynhrZZADAs2dCnT4wgfNJT8Uik+7XBwRAFlwsKmwjJompd91UrvIb5UtJQt+aymsXHxexGuwLPeMMGzQQtnsfjQ97a0zkF1Gv8OrGRD12A3iFhps4jwDkyCURtagg3ZRkUDzut89K4ccDn8YfdQdPSm5aoUo5WIPajSNnBdCIXleT7mHpdsgQFDtvvWnmmybhZVS8340lWUg+Z1OyUt+sl8YppKOjGH1g962OPupaGUrOsQh6Tubn4zd2uZ5YqCkbA/ijwbaBOhE5XlJhImpeACnddXEb15S26icsc/atpDLECuT6ZWDCD9vNa+Rjx2GqQGu5ilsyGAn0zGgLP16SgHS5vkPi5di9LNjWWuGn7JoRELVrruNmW7lvKu3SbBSM9jSTVldHUdNpMG840j 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)(376002)(136003)(39860400002)(396003)(346002)(451199021)(86362001)(2906002)(36756003)(6512007)(186003)(83380400001)(6506007)(26005)(38100700002)(6666004)(6486002)(54906003)(316002)(5660300002)(66476007)(66946007)(66556008)(2616005)(4326008)(478600001)(41300700001)(8936002)(8676002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: lU2FJt1liiVbQ9YpTsxjXbQ2Jgvh3m2IJSH6beA02tsCxx8NfuYcBEYy6jqv0YRZPAGW4JddZtB3KDuiBN+aze8d1m9Z1MzkyxlrA57CN4NERSyIaUolcU0HYlDgtIhQ8j4njSOzywICPfOiMZ7qA8JtLMmI1Qr/sV+IwIftzjP7zf+zgZF7eqwKdXF8maARIUgsTX9Mc9y5qkr81KNJKmoIs4JOHld6DIIy9AyPSRTKtCi+NZ4IAPoU1+tC94yrVSaQtgC1zI9wO8zjnfdUQYM2x38g0GyxsJzU8s1bo4ifp4d6+BtmPFSREFXERb1ughJrWZhb2pLVQfu5SozLSRNGNFi+mrihFb68SmuPVe3CGbN/ykRBa2+3Q7ReCCJV2ZFLM7zazt7jBH57wQbcN8X3ib0UyyE7kGkBoFAk1ZUfzwN50c/N0xatStnAb5z5MHn2tNJNydI19oBZcVFxFhB61DHcXhd4RFbVDQd/qQnT/X0H+Psx63tclaQaZcy2sIEObxDW+cu+ZGwEsU0V2i1/KvFm5AVqjJefw59aWoVUoo8KoeDVSsVxn0av0RgP0dEUsoJc2fi7sGmzEANE8ABjhIVqkpH19gbGh1EuljV96wgRtUTBYTqRq03scslJapWQw8wpVS/ed7SV1EBYd4JuqF7tBd7bQPBpbpF5vSNEfD33RIBprNBIXYkyK9IMz/WJ+6+i+UCC11VBOB0seXOUhKMPVyOq/LmrKdN57CBJsnInKt48WxBl4jl737kT+U82xNvFxR30SX5K2VVY6Bhda+K7zjJCfeHpP7lcFx64flq09/Evc0kaMXiOLjvkT07rfDzhxDofa/6iOVPMp+fTCD5qEUNqpVB9ckCCnPqpOGWYOhY93woO4I9TcHjEuLWmLyUizNJLc8GIOrMS++Rtv5ZYO7MIFhn4iA5cY3aTU+gFp/f9352sG5f7ZSL6x0ymauv5sdeqeww2jH3Q8HtsqbyBipY2VD4gyZTwctDuHolCz6tOWUnppvKtwmZClfoKC+M6k8sZyMB6nM8tAP+JBWpiggYe/Dak/Igsi+5vSGKyOoyCJPJ6Po0+La/ewVsn01Vip5Hr/nf8MC/ioAWpWeglizA8DP3jO0qOS4L3KlFT8IKmt3y1lCFZapaikIQO/0c6WXpsMiCFb/8uoCSOGMna/wb/1EJjg+6YV3ms7G7MrHqsqnSRmUHkVfApl7zH0Vcr111K9n4S4pYh/hY9GVjrwgAOryVKC/JVT2sOueC6QSJy50D1neN7DEuw2JTrepcnqLogI7C50iXfWfs2CA9jvbN8pRjjHYAygpTD5wL0p0RAqeUVDw5IheUsml0hTcCZdsJz4VGjJwoVfcAMjEmaksslYi7HfmzUESwxBb7abChI0UbTt+c/n67mKAv2+mTutIgVF6oAn4Iz90DHiS1VhyvcKjBQpPVwdPOq7sQ4FDldlY5+8PglJQl9LegGGuIS7WO8kG5i15eS+trTwwpzlexz9a5hX/hgQIXKdM6nVwAP9tLTC+o12VmUV7Bf4bqPIqHXK17kF3E7aRx9ZtTwc+ZP/dZsCZykZ55JSHPPXegmEq6oVL9nV3kp X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1f898ef4-5746-4bc3-be60-08db86f15ba7 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:18.7543 (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: ZsG+zESG8C8g93h4+vNPW0geGHWj1Rj0YVTOWvVmHwT265Hyjn1O6t0RCDoR3G9X X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9137 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 Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 165 +++++++++++++++++++++++---------- 1 file changed, 114 insertions(+), 51 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index f2b34c56d01216..87962d19ccd363 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,38 +401,101 @@ 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; +} + +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; + + pt_obj = iommufd_get_object(idev->ictx, *pt_id, IOMMUFD_OBJ_ANY); + if (IS_ERR(pt_obj)) + return PTR_ERR(pt_obj); + + switch (pt_obj->type) { + case IOMMUFD_OBJ_HW_PAGETABLE: { + struct iommufd_hw_pagetable *hwpt = + container_of(pt_obj, struct iommufd_hw_pagetable, obj); + + destroy_hwpt = (*do_attach)(idev, hwpt); + if (IS_ERR(destroy_hwpt)) + goto out_put_pt_obj; + break; + } + case IOMMUFD_OBJ_IOAS: { + struct iommufd_ioas *ioas = + container_of(pt_obj, struct iommufd_ioas, obj); + + 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: + destroy_hwpt = ERR_PTR(-EINVAL); + goto out_put_pt_obj; + } + iommufd_put_object(pt_obj); + + /* 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 PTR_ERR(destroy_hwpt); } /** - * iommufd_device_attach - Connect a device from an iommu_domain + * 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 @@ -420,43 +508,18 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, */ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) { - struct iommufd_object *pt_obj; int rc; - pt_obj = iommufd_get_object(idev->ictx, *pt_id, IOMMUFD_OBJ_ANY); - if (IS_ERR(pt_obj)) - return PTR_ERR(pt_obj); - - switch (pt_obj->type) { - case IOMMUFD_OBJ_HW_PAGETABLE: { - struct iommufd_hw_pagetable *hwpt = - container_of(pt_obj, struct iommufd_hw_pagetable, obj); - - rc = iommufd_hw_pagetable_attach(hwpt, idev); - if (rc) - goto out_put_pt_obj; - break; - } - case IOMMUFD_OBJ_IOAS: { - struct iommufd_ioas *ioas = - container_of(pt_obj, struct iommufd_ioas, obj); - - rc = iommufd_device_auto_get_domain(idev, ioas, pt_id); - if (rc) - goto out_put_pt_obj; - break; - } - default: - rc = -EINVAL; - goto out_put_pt_obj; - } + 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); - rc = 0; - -out_put_pt_obj: - iommufd_put_object(pt_obj); - return rc; + return 0; } EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, IOMMUFD); From patchwork Mon Jul 17 18:12:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316182 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 8F57BEB64DC for ; Mon, 17 Jul 2023 18:13:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231546AbjGQSNb (ORCPT ); Mon, 17 Jul 2023 14:13:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231578AbjGQSNY (ORCPT ); Mon, 17 Jul 2023 14:13:24 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on20601.outbound.protection.outlook.com [IPv6:2a01:111:f400:7eae::601]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF27F10CC; Mon, 17 Jul 2023 11:12:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ghk0nItDQk/sAaUnyrL2SF13ic54mE+OXH71KAFDBi2oU4sS1EIkrX8SvjOfeuJ+E5kyciNeuu71LVUzC9nAG/bzHfWdrVNzsJ9TYLv2nZK0pxnbZTULiC1/vYffppcBQBT70pQJqqu43KcH9ubYvD+wLmEu23hCPlGr0GX3MhWkKTi4LQ1csvVbRYmbkS7hVuZqa5Ny9Rhz0itXGY5tnORwpp3G4MiHPM4e2NOAfKBTKW2ABJtYQDhgbjBYMgnCQCtjn/plaePRaEiOlWPo+yIAuvMkejhJ649+aOHU1S9/j2neR9RTFATvuzRyOMcFgX6Dio1Ou5x8ktHFgzPcqQ== 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=lwrr+K8C6LvTSUm7HLyEg5ozvuzlhuxJh8GjjAOBsdE=; b=IN2Dxfc+MLgi8IxFRiiarKIDfWig3Nr+iQxIju31mKTP+ZXO/4BoVy/XqY386vaYMJbl0B3FwZ2DTc1sknRMrKsDtruwWIrmQ6suok+lvMVo3d8UFnv0adyr4A0wofofQxWd0sHo8/HpsQE0X6yArY4rJ2MTCLLeKRSRKFRgIdaPbkOLquhJtyd0tefnUkl7i8aN1mTxiUF6/AlNRKTIN1VbuHRav3QStKuAF+tphwyAAH1J098tirvXjHFJu5KwmZot7QwWcel1fw7NL1zfWnPToOmBzGAcSky/2r7vmcvkm7cvBq5rIqwDS/WpQlcB6f1hEkub3DWAEsy+TFLIzA== 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=lwrr+K8C6LvTSUm7HLyEg5ozvuzlhuxJh8GjjAOBsdE=; b=E3r0GChu3ZCOFA6hXtXE6I/jwur2PRViptvhrT4ixBhLk4wIXRePnW4BzCxMtRjvqWwq9QnBx6yMYQTtWCGN7xR7FkUCHaT1EzDbm+XED/qGAqbpcrsY36F5uQUD0Eqw8FHUDHvG/VwJ9AlQUwykxiN9YJtlKQS6l8oRmSDHrZ/iYpZ5mss1HEtBjy1wGqCgKOt4av9XIOuCmAlQ5m2TeEA8Hq+b3nef1Q04tcB/HMk8FvrVVRoytxciE2ZutrSfGjmJ42uCQnwvg8vkN3NcTMfZhxj+VIpr38mg4GZ2aUNyQdbeEgwATWN8yguJndIh2qnH+2ESE7NDtcmbRpJ/xA== 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 SN7PR12MB6861.namprd12.prod.outlook.com (2603:10b6:806:266::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.32; Mon, 17 Jul 2023 18:12:26 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:26 +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 v8 13/19] iommu: Introduce a new iommu_group_replace_domain() API Date: Mon, 17 Jul 2023 15:12:09 -0300 Message-ID: <13-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR11CA0083.namprd11.prod.outlook.com (2603:10b6:a03:f4::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_|SN7PR12MB6861:EE_ X-MS-Office365-Filtering-Correlation-Id: 086fcd08-5f4f-4f3f-d6c0-08db86f15d16 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: HUea0MdkPuUiz06supNMfkBAhlareefSbnlXBnTe4HhduBAzPhhy5JNz/j4LZmuSV/Zusc+U3r0rvTiZ1RZJsPAS9+l7ve0/HwCnTYDXjnNblTsJPCBn+gq62hQTePpZv8boo0UD4SYnW3JS5eHJVRNn0NYpej2zPjwwoRqtexqVDYb3voLl1/77om5qYLSjh1vUMYwNlrfwXYtwrbMR2zJnlAp1eg2tXYt+HJ+1xIx6kaz3OBQhHwqrS3jCP5MdpD2qyyXuLpA99jbxT++5mUjiUnA6HmtqyTeQONBaTwLQ08Q0u5YAza/7Uwcl1gySI4+qBAnLvo3kDQeEudQF/dZhNfHoht8/225ONTH/x0+7/KvW8tq21SttSFLTLdj3TprrNdUPQ5tTY3lirtv56MqBk2c2WUnyk0H9ofvdpnFvnQumYjZO7pY0c+0bBzHPNtTjxALpBHbfC+axdxB4aD7D8mflaHQD1MKDa8ytXDKFADBnAdRxezi1qmcSgGcWbLH28KFl5yBVNu1BG0/Vf3cEXA5klbnK/xwfxhjy7S0u4hk+TnlKJ97cEkgpuTXK 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)(346002)(39860400002)(366004)(376002)(136003)(396003)(451199021)(316002)(41300700001)(83380400001)(6512007)(186003)(2616005)(26005)(6506007)(6486002)(478600001)(54906003)(6666004)(4326008)(66946007)(66476007)(66556008)(38100700002)(86362001)(5660300002)(8676002)(8936002)(2906002)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: De1WiAypSzVi9i65KeDVczn5KkWy6jMsu1BxVeSBs+HLTbOaQMER8AGlXD/rCjgak0dj8dxzS+yQ/mMRRJHTjW0V7IDeJ4r0yJOmNNCazl8cbSG9GpVY9stTVITcqmSEXh5LImq1v5REhgrWXX7uapKrbKl+YeObkgopNRtXT4UNtQ/cOI6KQj6N81ht3VyEUJx2s6dRxVLNRW28+Fxl1K+4Fkp7wtXT88nh5fJmgE1h4kxRAyFKSblbuhZl4UUdrW8tlqpU8/q42j2xAUX1OawPJFb3NPOIILFdoO6SM0kqcWm/XeSsepJupYGV0wekaC6f0qvRQ8T8i9vfELJ1iTJZFcyntPRiH19G5NJmtWhqaDsbTem0xcQ1714FOrOBkBSgF40XG+L55q5OZwURbzF9SoDhHW1QefmRovUqZqTUlEwag9rQqcSUQQ8Y3Dc+MdTZYS7pXXmksJkkYCqcgSSR17rItS7M2fY00Qg3ZPQkR8iCFKcLEzoFf3ZJ6NiWXe5/ahVH1UnWj0xLRWqkBc8b+s7gzmMlEwbFCJ0L8Nc1lsXdf27soLs3ZX9j5yc6GIFL4I2wohGNr1xCFC4kMfhRL80Hots7dvFnc2OTkM5jriVfnYYeaKoElVGfOtdCClJC9HC+U2a3AnYLl1Ve5jB/YKSogBzQuB8HuZzRmNEo+rlQEOrkcSGHA/kXJE65n1l/Jof232MKuk6Gyz5f2mk208POroBrdtOS1HGNI8JYeW1tSmqFol20AYayoZf/T4XUcKvYWnmqSJSIIZ5BKxrWABGyNJhxrrdUKbotJPHkFYzKbrP+Y0ON9p9Bxf2TO6OJ6jFrA491FBPlmSwWOap0UY1QlhAphNPzElGB5Ft5C9DE1TnzLdnNrwUZgdkHwbyKpWCtdkLG8+gOxGJ1oH0mNtXim6uK01gYcSzJB6fL3X56126Urk6DVha8xnEQ3ku1FgfAGQFe/5IaDN8GRfQvYA4ZMXJRClxnfUQScKORy9Li/Qw+BeaoCg7J47Q+eOfA5HUTylj509zymRupEjacc0ydEtO8RWWZRiVfrMiGcXTwi3sJ6rz79BqeSr8YWl6PZLE4HjH8Szp0/ED09TumSRX83NSB3oPBCj2ahDoYSjcJ0gUj9zXVDfR9XkY83QgkTzVVLQAcKOUQCEvaVA1i/uMBOwJiTO02Q6KKCOeSE9+MYgImJojgTh97GpLshBiis1FAB5xr8arLJviZMLJUS8gzU/nvHJae0wX3z483qtVYgWdt11eNgxwQqfYRvtqdROkx+drdRcHGxY6WRZW93aN8z0WrHjMH0Mh6smb6dBgFAsKMajP08ziIpXwDbvDcgRNV4Q5Z5vdWBJOZNKuae8ftjecOw+BJVDQCVdmb17NyhtQWne2s3YfaeyRVRZfXpmfBNc08gcLoX11AO8HrzIXdKV8GudEhsZl3YgHRhbKrGG/BK5ZZtB/6IcdObL+Bg8cscAzAapipy38xw2j7a2OmRPFKHi/+ot9C4/Eo9rPomKVGoMA8XHrQWi3buJdTMTTeAHelR251lEklBDLcJFzMFZwHrQ3t27gK6xY0Kx7WTStSvDV+TjsT7Mtg X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 086fcd08-5f4f-4f3f-d6c0-08db86f15d16 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:21.1409 (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: wepj8no3NBCb/uOn+4KsDY049Ijf/2tOHZWPzjVehsqvGk7HV4tbiELt5yhdKyGu X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB6861 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 Tested-by: Nicolin Chen Signed-off-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommu-priv.h | 10 ++++++++++ drivers/iommu/iommu.c | 27 +++++++++++++++++++++++++++ 2 files changed, 37 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 b0804fbad6f9da..58761967657371 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -34,6 +34,7 @@ #include #include "dma-iommu.h" +#include "iommu-priv.h" #include "iommu-sva.h" @@ -2114,6 +2115,32 @@ 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); + mutex_unlock(&group->mutex); + return ret; +} +EXPORT_SYMBOL_NS_GPL(iommu_group_replace_domain, IOMMUFD_INTERNAL); + static int __iommu_device_set_domain(struct iommu_group *group, struct device *dev, struct iommu_domain *new_domain, From patchwork Mon Jul 17 18:12:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316187 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 B3B2DC0015E for ; Mon, 17 Jul 2023 18:13:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231343AbjGQSNy (ORCPT ); Mon, 17 Jul 2023 14:13:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229987AbjGQSNx (ORCPT ); Mon, 17 Jul 2023 14:13:53 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on20601.outbound.protection.outlook.com [IPv6:2a01:111:f400:7eae::601]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C7C2B1; Mon, 17 Jul 2023 11:13:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AgnEyvBi7Og4yGBRM89rFMKDs5sFBQ5b6g6A2m4R4YIFItTsLRPGAO8qhzclZcvIoBKPeW0Dl6+QOkGM2S+uOs4DRwkL2hGoH/CdHs8z2c0bxYnToFLlptxknNcfj4bTOjvZlATkzJCkEoiqGSlwBFNAjh8BhpOndRO2XoHNw2wphmlNfEZ2PKtihBqqK8N6yh/9B70nvPEm2r/X/RTjTZnK/xtiDFV9vWXd+xf8/UHXYulM3rasniAiCgFFeJE4z/bpGprQhN6/ZTOjPGzxHji1s2CHG/PskTNot1WMPCnQOZkebN4VdxLkzGSbjnd3ZhF4JG9obi6K555vuCAQkA== 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=kn4K5paiLIjHz+WPBXBKqUFIWDP32uFlhOQmxv8Z2mY=; b=OMc90dxdQsoemVwcCqVQF0MTX/0ZNBzubhqDuF1tjM7koXl+3eP8lv4QEjt73o6j2g1RHaxKZvSL1WzKTikrBHMNEtm+dKTco7Jl6IcgDL0O3OqIrtgBlq+kxsfAaV7p+6cG4np64cBz7kW/O7xvxPt4HXTPDeypD8nnYzsP3KlB/S5Gdfpnu1iH+zIOVQsws4PRHPbtmNCd/dLhMZFpqj32fyex6xNM45kAkVjqwok9mJK7YPXZaKADODaIbiwDuW4o34yIFCQMc1USg5z71FYtmuoPjMjaUmRbyjRDCXuNTLCte19whzTOFppjd/iC+zn0S0c2kuvX4NvKbzLlVQ== 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=kn4K5paiLIjHz+WPBXBKqUFIWDP32uFlhOQmxv8Z2mY=; b=f1WwhRhK3G52LgdQIwTiQaccMV0RRM0ckya8wGSXa/o5iR6NTFaevenm8JJUMSdef/Q5TmQRThF4yqVbc721iGhXI6k2e7JBtSvr/CXqSyeiMnpoFCafF4qVDDIdh4Ib5UyIRwjYNKZm3G8zCKO6PmnBtJmXA1SwashP4pAZCq5AU4NrRvmZjPzou0FXAB0jtl1e0UbuqtjwfQ+ukUp/QayqU21GAx0evfHbOqHMDcoRL9lcdvqt9y+K46LsTynTEGlA2j4I7UayVwFbFL4aq69pgpraQ8gaBV6EKoZlhk620EzM63ig065+rT0HP/QTscDgoAlRNs6mUwjsngbzEQ== 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 SN7PR12MB6861.namprd12.prod.outlook.com (2603:10b6:806:266::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.32; Mon, 17 Jul 2023 18:12:28 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:28 +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 v8 14/19] iommufd: Add iommufd_device_replace() Date: Mon, 17 Jul 2023 15:12:10 -0300 Message-ID: <14-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR13CA0201.namprd13.prod.outlook.com (2603:10b6:a03:2c3::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_|SN7PR12MB6861:EE_ X-MS-Office365-Filtering-Correlation-Id: 5113d103-9fff-40a0-38e9-08db86f15d19 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yaHTmqtgIXodP4IYPD2EfmsUbosCxs9kjR91tikSUzCqosa0neiHFdeopl3OZ/LFSyogvPExbnYPp6CsYYUQUPOp0ii8ymoLfHMBg86olPDeV29YZPznE7byMqLNu9KNA+nJm/yOXkDO5vn7aW55vRckp4I1nHPPqbaeQDy/l5GpSFRF01Cpdcv+nHqc6sKlpOSl4IdRjup0syh4S1363QaIjmRJfjyWJJBZVNNzvuNYrzGEoy4XVoI38K3vnaCo+kAAn1Xhc15jmJ41nYYm3mf8IDRoM+TKoeBBfKqluiytjEZG+LrJyj3aDXCpCLGO4g5vw8qOjbp+KmOmGPxo4HlL4KW5YPqg+758LIvXwkAHmrxOmysmw08uHP5nGWJkACchlK/RkZyqvLFUVb4Fc92o+KEjjI0R0A4criJZ+I156F+uL3oUm7o2s7tAV+Bh4p20rU2tdcjgf+/2S7f80bmsbpI9bE5LEoUiu2k4Otb7O00s818SJOt6nOU7iSuAlH7FUao4M+Gwz1q4eeb+JIdvWn83yQetc/zJuWPURL7gXGU01r1fOfIZYsXE53pe 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)(346002)(39860400002)(366004)(376002)(136003)(396003)(451199021)(316002)(41300700001)(83380400001)(6512007)(186003)(2616005)(26005)(6506007)(6486002)(478600001)(54906003)(6666004)(4326008)(66946007)(66476007)(66556008)(38100700002)(86362001)(5660300002)(8676002)(8936002)(2906002)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7brhKSxrRoEeUmELzsiOo65NErlakmmTjwekVG3l0ftQaZl3tsuh76A5LxPezcX0E/bvsELnMSfdGwd5B26Gigm5kaVOiBsCs0DgnZGbUcbhaf2IF2wS+7qSd6ZeY/FBjIxgaI2MbPdcw3DlDnoMuPzudmyk44DFg4nbtMSCMKFjdD98uw/6rtkdxmxP5+P+GOO4OyEnUtez+XXYIVumD0iiwKlE/vtdyAmrqXLcEO1aCKLJeWCWD0q5+TIvrwhvKW5Z8O6F96PtEOrrOW2XC1ewc0g808JATcJb833lOmqsWEpxhe5kY5bzHaxXJivXuHQj58xUW7B92g/j0um7gu+w2gf3EirahHjzLOj8TfZk5ZORErcOpK8E8dRV6CtYAuRFAnT4AEj6rmpKnqzI3nN9+Cc8w25KncNpqaYCcblluqnJSVLaPWhsKt5lohMVWvvA7/dLy2/QZhpjLrlb5qnxqC10bKeAzG05kBYQSofWgYdd9sa27DB3c1uKVakT3vZwQKCtW7jvK7iIAvTfLptvb1Xrq+X5zsUqLIQ/S491Wi8ZPdECodDRIIfl3wMsOIXB82eqgVJ4xNPJVYSDXrjYTjqETsalLEwscxQTkE41tXdZnLkGYUaprWHuiYt6tkmnPY9RCoSY67YAuhSD93ei7o5ESoCbOp7xRcYoSh5Lp98QpQfqXdiXXFf3F3Xf6Y/L+LMJm97DVY8FfqEerAk4iFJWP+q5ASAuHo61WiwzwB6qcroIjWNONVLgEGjHvHNmnS5KSfnOy62ArixcqYlFHLTF+9zGRAH58cURTA+A31aflKJY3OSKxBzMrE+3Gd6H0bC7v7SnBFQRunCiH4CyuRn1/gslpDwG1gaimU/CrElIXQn4VLeHYsfgwE5xW/LDYtKu37comXNOpwItUny5SyEhDY7kwlIbENsXzAlB7v1Vp01QC+savMrzCynVSmyhAbrwvtTXTKbD6xAQ4dHqAhrsGc9j/BiYShZiV9X4RKSZfxiwrZXoxoH1pQQVMw+9Z/ISaThKHLajoyd/HLBOMM+AnayM1o+gAF5cPMlh2T4CuPa4Qt1drgd+J4aZq5JJg7U/0r6a/gB9nkFHACGUoJwOy1kT0AI16DoPhi7mT88mkfpiMSukib7KGMuXazIvk9czYpWf0vDNEC5bP+aIcMhi/6zl8jA0uOUhOA/cB7THpIxKIjIYPKWld2BdPD6vXNf55TTMl5Fk1foXTtMLvTK/G07msBJq/dXKztlUTD9xhI5e3cXINS6Tikf1OqrNU9NBHJgtHcqmIIEm4qEzXAMUYXyj4u0SdE6mRsu37x/LcoXvUFi4A52oV8VuSvpEoLNovgOaS6JkLyo9GBFOarO2LYhn8/+sx2lbkK1ZrtH1ORT31K9vlvjwiYDrHye8fv9FDjvv+RGp/szi+Wftj/RWg69iccNL3JQh+ehZ1K9bw/MYweebMorIww516slE1ou57bAYmxuTpZDYVimB8qWb0glN9r/EN9BSKUWAA0Mt7QCtzuW2gfb2sP65CKarP19x2Q/g73/gS/w0Hekab3F5t6DnqUrzbAaRLoXt0txQAqcjFqsTKjVZcMUx X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5113d103-9fff-40a0-38e9-08db86f15d19 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:21.1569 (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: yAtnapEkiGKsVLc3X2UsuU2ABq+l6S3Fdu9dtOBlzGJrDfchZxf+dY5kT+nWF121 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB6861 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 Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 101 +++++++++++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 1 + 2 files changed, 102 insertions(+) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 87962d19ccd363..0044c2df500ef9 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,28 @@ 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 Mon Jul 17 18:12:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316180 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 14E04EB64DC for ; Mon, 17 Jul 2023 18:13:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231416AbjGQSNT (ORCPT ); Mon, 17 Jul 2023 14:13:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231876AbjGQSNH (ORCPT ); Mon, 17 Jul 2023 14:13:07 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2068.outbound.protection.outlook.com [40.107.94.68]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25F691BCD; Mon, 17 Jul 2023 11:12:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Y6eW8wu5JKP47on6GxUgy6X3OlvZjnCrhtyUrH85zU7LVBQVknc8eS0Bz6gA3MuXMpOQelKqwdOZlSAMk9SKA0pIoflqFU4UP/LI989NyqFQ+D07AJciebiUCY9MelAprDcrTtJz1aNd/djKaI5QHetukeAc9joLNZYWlP6cb0TmOjsXA5jXS/2DsX6KgjwSYrsuijw8uOfOo0RYBA6Ugjv3iHECBEQVg4sihIvv9EC2EScoMAvqEe5+BPDYjb5D0UoFbsA09KLd/py67cB8VdmuE/NuAtSYxRe4NTjCaGSUF3TKfmCAT2EBcN8I3UiC6xKcot/6J5ZKQvamkAHYkw== 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=pU82gALeUAkqIlXVzIpG958Sk5xxwixtATcq9aO/03k=; b=jf8Q6rpmHNdTwFIeIWM3RyN0lMYmITtB2Y7d1xy5jFyQcmeJxuz9gJov9cmMChEKGnixD22fa4Ou0uAu/QjMAXmklOPyvosnTkqi4NWEYej83ErN15uwqL6aD4IJMILg8ExZ6G9s1zry0BdTLqmjC1wXOULa4arfV5oXUyUbE2p+uWYDfqNIrzP8QF1bE/9Jd4xMoxjDittN0sb+WmoBhtXy1ZqrRHCVJGgoHtk5V5GPMsfr9ga68nPWC+SKLUggdzv2fMiMBGbW3R0uJX/adLoBVDmPxcDBUgqdn/8+i3gO79OycQ+qcLdGGyipA+B3oQlJ0S/I/wndSe0C0+30FA== 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=pU82gALeUAkqIlXVzIpG958Sk5xxwixtATcq9aO/03k=; b=bt5A1WLFh4HkLul58T80IxLcp0teAw/0xhmomjY3D8UoHf0Nj7RAfV7HPlIu3mt/aZjBFQRMK0MOtH4iHopW7k4tiM10yjSx9jhc5DVKhb6bd8hR4AoFoipQ+kQXxZ+ZiBhwlVpHQoCovdnYsY/ygHhcdz1lS6NAfHRQRs/uvZao3PAfURSfv4oMn0k+L/JjRzqYDG+ShDDYE6XhcmueK2VkXDfqoJsT9bS+do7gpkibSAODu7Ggmp+6Ze0DAOCbEYLReW2iL+sPEqDovZmmxMenJNRM3tz9NB3rHB+ACOGcWh2GoN2DYtAk0iTnyETV4Lu1pFZxN8Dt4lLxkJMjpA== 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 SJ2PR12MB9137.namprd12.prod.outlook.com (2603:10b6:a03:562::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.31; Mon, 17 Jul 2023 18:12:18 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:18 +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 v8 15/19] iommufd: Make destroy_rwsem use a lock class per object type Date: Mon, 17 Jul 2023 15:12:11 -0300 Message-ID: <15-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR13CA0016.namprd13.prod.outlook.com (2603:10b6:a03:2c0::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_|SJ2PR12MB9137:EE_ X-MS-Office365-Filtering-Correlation-Id: 3119e1e7-5df0-45c9-b253-08db86f15b56 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: pBTCERWOAe1LTNOg4YXvYQ2SCICiX+21BCwZ/2FoZP49JhFCzfn+xf1sR6aA6e1y4bSZ8CYT7h3P4Lsog1CXJfRw6oZoCOndCERJR+riH+tBX0+do7mnR6wzlVEgGKpuJ3yDKHw+acY5sJa2mYpBOG1vOBeuIR+fyxQqsKumvEsoc0cv72YdufCNpE3zJLPyMZRsaFSYiKv4bT1gvMYg9Z/SCB5OiBRxhniPr5xsw/uDfsyWnFkhc6XI4yofsfjYQoo3dZVS43J6rcSrxUaPztmgQ4YkRxnscf+UZbuU26b0e2biOlFf57nLVT1LVrUyLRh/zBX2892kQGBmphYhdfN6pBIGu7Ieh96XRC5Gc5u+g7hR8fe2Gi4TvzflOKqBRy6weQnjkg6TgzLidYV2wE3B7EkHlTgDb6Oe+t8ZEDtc2tAs8DqHL91HfKWlHM/6tR7bDJhTCeyXI0Ndm/ppY/7Y2WPSE0pWYZn2pD7w30u/Y5I+QzKOOh1GtKXsWmvUnynPj5Go6depw+/BN3VwnrK37ndkSnoIYTQr1XCDqA6yni0KyUzVvdfRHqv6KJJ1 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)(376002)(136003)(39860400002)(396003)(346002)(451199021)(86362001)(2906002)(36756003)(6512007)(186003)(83380400001)(6506007)(26005)(38100700002)(6666004)(6486002)(54906003)(316002)(5660300002)(66476007)(66946007)(66556008)(2616005)(4326008)(478600001)(41300700001)(8936002)(8676002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: q3Prk92mOEH48mtFE/RnXID5ffdKagW16bEg/m6AZFwylCk/1Oqr2NSlou89ICSJvnqKdGU1EYlZJgK3IUPW+LSi8fKVI2ljjRhQuFhn7aszbf00re0bAt9h0NqeeQWaJK/GSYESClNSZwY/7ef2qy9Xznn1DhHdJXxC4iA0KKAMk9sQBxnE9KtzCLBKDau3nBuHz3mdM2aap8kWr8KPX0fuJXcDQapNiFRh+p7AEfWXDAgdzANDL/kljOd1pbRjBv56TTb3dZvssjMp69/Vsh7b1RYhbHvsBRE02ueEsdhGm0sA+Fq7fXSiY2XqvHj3XCpjHZawJPJI5PFYnWQkLpbbv1VNbDeIUvppNGFHEmY8ZU7qPtCr2YElmOTrhpnzHsGeghjuEOJ47Lce3vubxOe/NGP4VZOWCyBok2MMGBcuvqFI2KqwSgNfdJW76qruBbYANRHOB45z9YE0R+N+fv1eHJUNHVfe0cPetxykXKj3+G03+65vEsHssUzbgtHdVeu0lgDFacV8s/TDvfHBAtJNjaqsIjGQClspJIPuociK5Mc+Culi/DMNR1lVankial2hXDjXiUzlJxkStT2tafuUSPvhH+mYfvkP5utyUPUfBhJRJToRz26+gzYjrMaX2Nl+GnVvQzq7n1D6xa2LHNLkzHHzEORqEpuJpsc1L03mR2sn06v9fQ6j/WF+ia1USW8zHWx5Ij9DpGYv4XvSfDP13TXIIINdTmZD5Y0j59SLviQsaY4PsSsqiuzQE55gBz8CFfHC8omEeGCrW7huXZq5koCCd+t1Od5+j2XPlIC8UQu3ZnUKtg71EPvxQF5JTh8f9ggxQU6dEginLvYTf7Kfc4t9xtFJNhLwyj0yyli5d+9iUijVa9lF5TTthq0Sokyt3SqvEw7nTqY64vgLE9qmfFZ4ifcio2gfc6LnVMhDBtUhIfYOSDjqKh8aSt+2cTLmeHZvSh+LbnRudB16/oe3bpQeV5DClj9prlfxi8kd110iqTKVX37bDbDY5uisl33S1L7w3G4d6CWDNlwVlSc29mN/i+gYQkyWUMeKibM5rF5gFfRiHkzCSTqWSjCK5zqF9sybWVLL2qDgJEWiO6G1Ipw4BNjshICIYJdK/QcZ3ogxHVJvPAj8E9adIj8RWZBzFK+d//g4fSjzBFnDmlIW4eoiKxjcKT5o7mSU4zCpzsXjknBnS0hdBym3Xb1+dNC9/rKYJR6x4muhRpmTRuJ9NC4Jw6N8M+jxzpJZOK7jjbRWHYbN7EAq3WvHToTdWMD+kpG/8dEpQkBRmsDDldEme8EFXKA6NEHO4rLYA+Uat4pIGeUgpyHaIDfAdRyMMi17WIwixQL4dq6c8Jgh2deW3CaDiOOFfio5OtQWq9fNyr2530QM9j3nndt/oycyEnbm+BecyjraOPVNDSod+Nbg1OocF8tJHpllKo7aO7tZf9hQhnLzAUUoSigCHs33r7eNdhZOgqqaq/nPrr1jjZiWyy4N6aIgwhcAk+7glCX04x57sp/F6o0tHjLvb2rNJW9tmVH9o/CFNXwwSAlMv7Ad54+icSvs4SLR9bTVBxJxOU1ZZH+53KxSq2MaJT7k X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3119e1e7-5df0-45c9-b253-08db86f15b56 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:18.1909 (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: N4S1cVPKX49+vEmSjBbKcqAjevjRE8Iopp5Gl87AqApBVZ4+VGdjP3rdLeCEN9xe X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9137 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 Tested-by: Nicolin Chen 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 07d60d0640fee3..79942934a4d69a 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 Mon Jul 17 18:12:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316191 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 1637BEB64DC for ; Mon, 17 Jul 2023 18:14:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231669AbjGQSOO (ORCPT ); Mon, 17 Jul 2023 14:14:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231666AbjGQSOL (ORCPT ); Mon, 17 Jul 2023 14:14:11 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2066.outbound.protection.outlook.com [40.107.223.66]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFC5E10E4; Mon, 17 Jul 2023 11:13:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=X9+7h8fBjBrrYrngckXp2NVLRcf0z6zSdpO0ky4oPx6L4ME+5gu1ED4HXZLPkEULWUtn+tfoq1/9fwbMtiGBE+DBWKIg/s1VzHExKSdr6kQ6ifT63cp0WKuWAFhU3h62IQVpT022J8z9WR+iXtdlwIUtbBcOFH0ibsrpFKM/nXtAW66wLZlF4ZGzXherqoGXzNf4jQgfkG/pscjKFV5OvnuqvCywi4ErZNTTvyPs4y755QdI7+T329G6bPCGikBwlpcbPvbJWsW7Uf6nq5HOKY+cljlTnuiO4EIxs2I5QHwtm1gB3aakWhcCBom7Pt1sEfgd8qOUU7rTjebLqjqejQ== 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=YoYtnkB0/YwcnPwmpggUl/i84RgyHrIPUcu/dBnZwUc=; b=Lq5WZdaoVsv6SskCApIDf7QB6xNrmovcb1r54KLAy4Nax0W59RUW3uXee9AWuMnmTInYc0Fw+wunkagnKjPb1Rapxd+/Ra3VmMHdSUbfh53LKTZMsN34HMmVkZn4hepuu0JERITPyj5YitYkQwy5epYkIwZaVvMxpT+jOH8u3NO+k59xA9TRfEI5+wGPobr9SlHicwat0R9b/H2r5sCSpK1sEunuoe5+C1+HbcCjVSJwIOn7JvouZoS5NEgS0bGa7YPk9aOHhvnYO0ArNr79JitwMogN8WHTXVISISBDSMMQ7n76IGJqvAJ0FS5r/4EUBiEtqKlvbCkrYey4yB9/mQ== 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=YoYtnkB0/YwcnPwmpggUl/i84RgyHrIPUcu/dBnZwUc=; b=I3NqIN0uLOc1rkg0SRprFgllJi265bemBDwxNuligHwAZtglLDkJ4RiukrzqgrscfrnqEESyTXTVTPSv53Zqgt4pijd4BYrU2/vaIbQbH5m4JN3x+Ku+XTY5Q228MxWRKciEYSvnkUArIkuukmWMpzu8E1UYTOI/WkGtj5nd/Xwk2L88bAH6+tfCegh5zwbcNwoQYDpkujVLMRzpWXiEZLxwpPR9fJcU+wC33ai3ro9i9soJsGtG2Z+PvYf5eyvZ0NiL42D1cT4gebnONsXb1szX/+CraF/6jkHte4S0njz1VJHSt3hLBdY1TKXTUFMerKTENiZQs2BIfWI6fiuaVg== 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 DM4PR12MB6205.namprd12.prod.outlook.com (2603:10b6:8:a8::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.31; Mon, 17 Jul 2023 18:12:22 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:22 +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 v8 16/19] iommufd/selftest: Test iommufd_device_replace() Date: Mon, 17 Jul 2023 15:12:12 -0300 Message-ID: <16-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR06CA0046.namprd06.prod.outlook.com (2603:10b6:a03:14b::23) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|DM4PR12MB6205:EE_ X-MS-Office365-Filtering-Correlation-Id: eb4236b3-8a68-4d8c-9a92-08db86f15b81 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /JQo578oxYv3isr56h3YQIgSpoDeEu2OJbhcWtpF5hYMwlS8Oy8A9jJefNkdclPPpNLX8w/8CV4OEKgu9Dt/67U4ChvsNzFNbiYgauxq/KiHsR7daiz0e3FwM2p3AbXa0AyDTE183UGL8Ykf6DCswGQT6uIgC3CxrZ4P1+dV5Tr0sZ+Wf7CsjtywYYVdNntlUaexmfHw2HcPEJrm+1nNYqQ0bcdiiGY1V9tx4wCrH+yKRBewSL3cv91PSSOaVcq16E9KJzFtSBhTndsWG6rRSs7uBdKuca/H91XcIuN49wDVFhC4raVc5OXD3mjksOUEic4bYvFa68ONVDaT520Hejx+7K5LQ2oHQsJ/WlR9AKdSoGy2Xd5I1wjdIHgl9U85Ynd8081u4FxtUnDH+lkNQTHtXkVK5HqUciAEdooW7vHVrwsl4IdOJFaEUDu/lnmKt6r5YhZZ10k71+ZdVtxLNQADo0+Bwqqs+8dysh9e0YYghAevb+xCgJ2xflRiuWiofsHvu9Eyz/r9aV+j2wy8U2BhUhoAj9jJFgW9kZk5cauUNmoLzCVN/mHRW4B9NyNK 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)(346002)(39860400002)(366004)(396003)(376002)(136003)(451199021)(6666004)(478600001)(83380400001)(186003)(2616005)(26005)(6506007)(66899021)(6512007)(8936002)(41300700001)(8676002)(4326008)(2906002)(5660300002)(316002)(86362001)(6486002)(66556008)(66946007)(36756003)(54906003)(38100700002)(66476007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: l5oh1D3BQqXujDnh91kGfFNsexEHPUd0yxk6rgPFapjUtV4ts4a2AO/KIg8t1s3AO9FBMAAi43LTS7Wrlep+pdK/IubfZBLPM2CGg+bbf5941RlUMG5STl6iUlmhOvdt56SuSLVt1VSW0dTCiolmod/JB4JFdKeuSfAt7GjwZLdXcLNGMUHGxneyImAukGpl0sdS5EZda0VbRymnHsJ9eyGXFOy/p5PnlGLJMcZhJ61Hqys0S4p66AaNfpYnO7f+P/wEFfvikDEhIyu7rwPk718fS8PlwJsS0tsSvthRaNu4CGJRM2fgK0hBUROD5YX1+WRm+Wverb2PNXDxweSGK90yaMzjMS+d97MW4KZF2gU19biTgGeFZ1Lar8fLiPxUSbUJtAg8MvIAIt1nI7b9WH1i00D2F89AW6dPcWrBdtUUxMeTvKRgYDzIbmvnyloRa/AFh0xLhyEkT8YYl6JTjy8VedGOBrbaE/DpY7cm8K/uUiORcghGZuSCq/PhLtEKiJPJYcW+nDpziMGNwIHS6OXKz+Q2gVLCSSclpprZufTTG/CmG5C+BrBqpfuIBYk51QrkzhHKhxzg0jmnjfK2nlRW4vJ8GzTR2gN94Ckz5mnfPG/FfNZfx1AFMNShYrASytAAWEoqTncYc3qIVxr5kI+k/9thOOjGXZpZu5ya9yHRoS7eaEs4O53XqzbopqP4zvCRoTFbO+rbe1iW5BO33dNybuCQhPILwayXxreLmaOYqmh2T59am3tUWKk+qEGIF/I7jGx28a6x+hoBzDkwHHBko/nCshCGMUhiCz/vnS4KbQ/lUJr3wpqYNpO2AUlmYw73Uzq20wlL8zvSrlCDssqwjm2nnUUbOH/v3bq6ebjEENvYys8WEcqLVeSSFiZuYLPsvKZmstvVHwF62/LdM6VGmf1cxBVJHSLOyMxa+KPsse9nSpmw5LIZoeo8LSkEjV4+hmuq+HXGnzHABkleHPZSHCoJezEkjRNLdSGsPevcvW2svIfFsUMDhOWzZzIKiUDNAaQ+5Jfo+zmqk/bXvgZpD+AsSM7nDuu5RFyrPv+2OCaTSIG3NVIr+GVny5qTL69k+GJmvDke9AJNpBz90xNpLFdCDZfqpy9/4vmeFnCfXLSkm8HJjl32EYHZoZbnyg3mPJzku2zus91uXLqpKPCwdj/RcE3KqkQenwAZ3i/wxGDdmHQCSplWA1BrgmQPruMaLP3iS6lPEV69HZUHTQeTqR0C2ZH3hHFSdq2B301FMzJFUBXoHAV7si9w3huYfh+QmNoZt0q/qfjWS1WCNyOCo2m4yZSPFQ2ADqwdvtkL++O6Wt4sCz3uINM9WVxhDDxFLhmQydreacvCdWX1ty54G332f2Ztrf8wZyYTR3inpmspyXswrLr9/EKlgzeoa0RLWBBDoC+/ztSq9fuw61D47fB4sjWYKEygIJ0XoCLYp38K8Y1FmVWvedgkjbWSZTh6nzbu1IXo6NQp4WjYVDXrnzWkwl/VeGl7tjR99H2J06l2XodofO4vYUtcJbduBcajQr52h83WgPkyxbHBYFocz3T96F/SO2PqKpa/xQKGlvoqqnuKIP+xmLk/bPTW X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: eb4236b3-8a68-4d8c-9a92-08db86f15b81 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:18.7209 (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: OhXkPhBa35/g36G+6/l+Y1tTsgjWOwERgwRSbx+U8LeFcdMBaTn5sW1jL9UGw5Oq X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6205 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 Tested-by: Nicolin Chen 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 74c2076105d486..eb33dffb955dcf 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, @@ -948,6 +984,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 1129a36a74c442..5e417868f575b9 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 e4a6b33cfde48f..96881ecca5c757 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) @@ -1035,6 +1032,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; }; @@ -1056,7 +1054,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; @@ -1308,6 +1307,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 Mon Jul 17 18:12:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316189 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 A7E7CC0015E for ; Mon, 17 Jul 2023 18:14:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231613AbjGQSOA (ORCPT ); Mon, 17 Jul 2023 14:14:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231576AbjGQSN7 (ORCPT ); Mon, 17 Jul 2023 14:13:59 -0400 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on20603.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e8d::603]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F5B210F9; Mon, 17 Jul 2023 11:13:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QUyRLLZnR8qXYYu0O+HO0tl4IxBZ0wUgyj++6mjQGDp4hGOuA/qAiOB84ehE/hDUr3VbKA2cEMnrO8fz+5Jj1CmLS403+S3S3disXnLtMn9FuSN1DFgLuuil9Uu6OrZBT0q0C2bVPfC2NfdVhKxcZ4Fhzk42aTvAorzV4Ux6rViSrBcGE3E751Lg5ISVg7StyksFtAn5t3Qo4b6Jg9IfLOX7B9vMZdtC7zVBE5LP0cOuRvgk5tdueuOJL0o+yfpq9dBQ50b8eEfyn3oPAQWDsHLpQ17J3XYmK/HiEEZiiy9C2PlApaffU2teoksBBqSz7EBWzG1lAxwluQ6iUFY5sQ== 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=AGMWvL49rAP1m8yoMj0d6B9rpdSmPX8SPYhFnppCdmw=; b=l7DwiM7z8Dcl2/Dzc69bwFwR230mHnF23VuujXGtNVD17Up2ZfuVQiLlkQocBvK9a2I2hZgMsWJ/w06JAKRoZaDAscIyKcfoAhosA7yu7M+nNWjEu/Q3c5mCrrKDvfWy1p1v7htuYdMQvpG/gLQ+ua8KzwUiEOiY7bmX1LledrrVKhKmrNPGgWGAbqYgBo15tGfLtT0SK74rMejQuLoSwYQKaYfvC3OFz9bW07/r4jH9S0MyuBRwuJgRxAzZDqZvSZAKRBxGdf3gbIVswk6s5GGoSmeoIyHzGsqQ8UslVh5Jt8dONBaYrY+weySEZqDC+k71DG1cm7GTiXxreXOV9A== 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=AGMWvL49rAP1m8yoMj0d6B9rpdSmPX8SPYhFnppCdmw=; b=n8Y39oyll6vpyuQTIq4z6p9RqPGZEdMwrqnnXXLnSi0YD9UtF03zbJPiyQgTA0MjFQ6LFrdi8i6c+hHy8V5gk/uChUwrAPE2cEpz8CjDmJFg6syDvIY5/c5TU3MXppjsHwT726ZOjaLKK+JZz7VRFj9GRRR6de2AVtMyDh911OzUTxpjTvYjvFipby/EkknMH8ydEQLlZ6gPp7XehfxuMg/QyOGY0fb5odt3F1ZXfkctxTlreZKVoetaAvtMYJ1jeD/OJ1p2Zzu1cJSF/jTHbbvPIAIFj0FPuU3fIIXe1mz9JSP+7pBSUyWphJTY1aAdfCPwa9Dw6XmYyr7V92VO1g== 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 SJ2PR12MB9137.namprd12.prod.outlook.com (2603:10b6:a03:562::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.31; Mon, 17 Jul 2023 18:12:24 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:24 +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 v8 17/19] iommufd: Add IOMMU_HWPT_ALLOC Date: Mon, 17 Jul 2023 15:12:13 -0300 Message-ID: <17-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR08CA0061.namprd08.prod.outlook.com (2603:10b6:a03:117::38) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9137:EE_ X-MS-Office365-Filtering-Correlation-Id: 071ec32e-c68a-4659-3e98-08db86f15c0e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qKMLVLoZwS2KlOMQf1zELLKQZtWXL56/5hy8/A6HPFOIeKH0fDt6+yov6KsxVc4nKwtBF6SHtLmLjYaNU/5ZcRdMzR8CUewqKLtdY4/8bPmeJzrWqAfUHsRwoxdSFEdndWxuaXEbvDZ6rTgre/Nvm+zo0gbyhYZ0LXaANBvTfegCoC0dgjVRa0rf66+O49vn+lbo0F/DhHNYhsSR6hJEkeDtDC4HxYtLllB4tyBIDJgimwH4fmTwecFCwj+MLE4zkNtYJWvVbVSgVkZ/6dNYRgb61J7dPCbQruWURawbg2rJWF6uBs89a1h1k6HOdZ+6ppvRVVnJbGE0BP5GiNpb6xUdtC8HBsNhrY+PAt66kNByQRNK11QXxmVKIe4OPSMKB8+9m132+FwDyqmy8hpsNFHvt4xKRUs1HtlgwcQmhxmGgUVsVxcPMu4QwuJWvr+boFEn6RdobXXRsClZ+b0qFTqSSHn+PgWMGvUAhcWhOZX2Ls6yPd+OnjVxSGqauCOnjCifFokAig9z1UPObv6NYr/xrqf0sANMn0/LcwrSthmXFXPudB6qTsziAWwRGERa 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)(376002)(136003)(39860400002)(396003)(346002)(451199021)(86362001)(2906002)(36756003)(6512007)(186003)(83380400001)(6506007)(26005)(38100700002)(6666004)(6486002)(54906003)(316002)(5660300002)(66476007)(66946007)(66556008)(2616005)(4326008)(478600001)(41300700001)(8936002)(8676002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: INoIm2OX2ZM0Ad/1gWn+A+rmA5zAsl+FjYz8jGqQRbVfEZVSrgHh2Y9lTRZjgyxyxzR0hfFnosbrmBBsGVxaI+JTb4WrBYacECzY1Jh93RT1Sd0Yp/Q0rXyC/+btgP2IB0gLIr3A09IsgLmUOjWG4FALu9i56j/6+CDYZa7fVJ84aBEKCgD9ZE10BvTMjaV0BaVt8sRWTHdslFTGCrtjbJw4/CNA8jU6OBJXSEO4Nm+kMTbYjYSgNaCxyyMc2XLRCt/QaiOSyY6NAil7Vq+H6ogdTyBfaR5h3JF5UBQBTvhGokrcI1tT+oh/y00EDmvVX3LFR4wOeLVe8O/6TLrUbswn37WNy/ZrlBralWDkE86+NM+gBdaWrKJlODNO7iI0eGVR5gPS4s1XiQ5b2X+9GHQvcKP1w7p6jT3U6104x1obUE8oZwmTb5WbWYA/KjjPkAQ9vrgDyp3fMrRIsS1JzNLQkt+uoqf7f2iELuB3Ycq54NU/S7Z7QSSwVEnbFcFwqvsOhJBilIFPwOaDr8t+MJ9PwGt+qvcTEqSUoRrwSvcov4XqmE3wNa9dto0f+AeSPc24P8U3nRvfBm9YwsYkiyWA4rSU1eOdvt3vaRHyrqp51eUCUvna92erIbgw+yf5xO2e62nuZ5NDg2oNehh0sbRAWXBvCNkkGCDdI4VCHiTCVC7ZNr4CQWZuRvUXWG49XwBcGR5XdQ6hjZPojR01xGRsVUQxl/SNvKy4tUHebpbZ7rYB/+XldzvmQm0UMHIMYBD0Dq2S7ANKFtmPWbMBYos7zW+QnWPkjqraRSj36bpa/XVVK4OcCLssHQwIU9R+XwLXJpEMdkKjboBbhl0oaYEF/l5uXAdTKly0zIuXq3dkWixgTRdTdB49tX+bwqIEozG/6Au1e6Yq3GMXUwDkWG+UTsja9JmhfOhbIFZS/5/n7H6/56M58ixa7zOFocjxRbYpMnQkoX1ivl/UFTf/qXqTTr1afhWktPVF0LpXUcBS8sCeLzpR+99qV8r9ps78d+gqo5XKcOtU0Tlg6xznSP+o4vDhP8MyYNo/S3k6tbHYMdM43vfV+HVXzBLGPt/VfCi5Dcq8Jwe8WmEpOSB2nq/uxjN3S6eAhWyZ8smmbGgWvE9fuc6Ovi/WUWArc1nXuFsWXN5BjuuMmIfnvsX9xv7OdQjiaOjQA70VOb/dUIgj672EC4cXVMIA3tW9C3Ib/ygV3Sqvocz0Jc3J5Knqupy2qQh8irSfryFdsCdM9ifg13Mp0bhK0h84LlHVICFrkTR37xphcU4JjnbgFfhNTVUUcOJFG+7xENucFexb2DuBK2RJ3rZP0AIDqpCozyUmEq/4iPvtYXnLrhzO9+2UkgicB4cvIuxYjsYVhiKO0Nv4GLNQCpApdxU442Ue2BErxlrtFwzKRwIrEbcn7ojf4doYefqneuLS1+MCii16/fZfip9pzZrIGZ2iXqF+DQpShdbEKV2PNue4HS9haiWanDlcIYyW1ixh5a7uu7v/uzZjwNCfBvSfGaE0qbe51iWF5zAxWrnGE/9IF/RnzDdsBTJkRiGdREjsjBVqVded4uzOXLMqj5tJyR8bK4OuZqBk X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 071ec32e-c68a-4659-3e98-08db86f15c0e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:19.3833 (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: Ezgz4pGHUB/ZUbPhvssbFlaYHoJstbXetc8bzB4QBoPvtw6mklI6VTNKPlZ5Sw9x X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9137 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 Tested-by: Nicolin Chen 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..cf2c1504e20d84 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->ictx, 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 79942934a4d69a..54916fcf8d2a3c 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 Mon Jul 17 18:12:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316186 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 AFA05C0015E for ; Mon, 17 Jul 2023 18:13:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231547AbjGQSNp (ORCPT ); Mon, 17 Jul 2023 14:13:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229987AbjGQSNo (ORCPT ); Mon, 17 Jul 2023 14:13:44 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2066.outbound.protection.outlook.com [40.107.223.66]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7D5CBE; Mon, 17 Jul 2023 11:13:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bUHL4d1k3Sy6WjXIJanfwg6IHLFCAyRRj2wqf4pUeG+i8E2ao7JmEx5mUyjp/uZiWwKS6a4ffBQb9VyvfqyA5vGxdq9SyuM6CQ11JKxCLJ/LvfgUEL60kG3Iy9Dn4NPhoFy9z7wUpgRmgGN68PCNBnO0POWWfHIcq6W3mx5JcXCk6bV4/L7WAwqXAqh9HlGzuT83uEssfMamJz058Us3P6XUqlmb9GBHK435YM6cTTltLVMgyk0cf444YfslHsSB6zEuUVKtlEL5IVYdHWqKmhfHE0q2DhPA1dEhD6szzDTxu4cErKfuthlvPuDgD5PU6grzZ18fqPD0Z72n7k/AEg== 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=jIFJCw18jV6oAN5RmGtWFp3dTVEA00yQpg3MSokBEv0=; b=VT8CZp52zDVgrkbowVlq0H47KsJTBotmUb2GG/R3u9gOXlKLeR7/mdtxbUZj2Y5i6v+WLjJyJeKKCWyEriGeXQ11HKnYX4UyX3TfYZ8QIaKCNnr9pk4rX3Kd/wQ3OD8lzYUTXGvAtoufaJx6J3sqU3USvPrhAiaV0tSP1lA6hnSzg6032yIyYkHdqbikWdW70LYDrDpo9Y9KjAR2Sp0OZtVXhEbYCTLBcsM/GvgzPstR5t55Rdlq3jOyKxWPVE4gPUfUjYcNi55bf86huClwuX2rBf+QfK8BWSz4eIpScS2iNGl0yMXQEFgkOg42nVgWFfKq6l1wlgfLx2C12y9lFA== 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=jIFJCw18jV6oAN5RmGtWFp3dTVEA00yQpg3MSokBEv0=; b=bw6iN2TwkIjq/UhvErTGjMXscUchHL+IIM404OocsXQj3CyYn95yASjj0ztqt6XdPdj09Xgy/mux0xn1xmolV3LJ1zcvEFS4psSlECaTf6Ys2oTN0gqq/b94FE2D7kQKdTBTzwkDr241bljl2fVdfehPMUpTXgxw1tVngpHJqTR9+UevZB2i+oFdJWvzH2gPFV1b/rqHwgaE/cXzjuhATqliv3qCc1vsI4lqWZlllCzU+9tqP3TF6nZhQ2xIBrad+w5rkn4v1OM18pUWiRK0KCwuKP/iWNs/Dj3bUmVXI+eLN9DiVzLPcebDuqa6Uo+g9mDg8FBGR6j0w2spM/KK0g== 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 DM4PR12MB6205.namprd12.prod.outlook.com (2603:10b6:8:a8::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.31; Mon, 17 Jul 2023 18:12:22 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:22 +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 v8 18/19] iommufd/selftest: Return the real idev id from selftest mock_domain Date: Mon, 17 Jul 2023 15:12:14 -0300 Message-ID: <18-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR11CA0073.namprd11.prod.outlook.com (2603:10b6:a03:f4::14) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|DM4PR12MB6205:EE_ X-MS-Office365-Filtering-Correlation-Id: 490f4a91-06b5-43e1-73e0-08db86f15b7a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YAPn5AjB/5xIwrh8bSdnbm3enD+hdozi3EUTxepvFpyYm0ku54DI6idrVmpC7pN0+9Ou1uxWDMh6SCPuZ+apOqmwirKPPk5Vnlhm1kKvS+98bZkwuGXlfLSLojsU8e6ZzJqo+qNITQYPNE/DEteEROVGfAUtbC/npxQdCF3GbbLZQxPdRheKpv9wVlICSghBxIRjWC9bMi6POj3N7Q4zXxf3e3RrycE1ELL0Esr7vqkin5/3Wev9qHLELwc0yGpvqvYeNIfrHlKrg3zE8EcJeuIBOlX6SXrgGAYKtZnDs1i2IFTHOsDlDS/AiGxNZ+XTtubqMST/FvvB8pK9uWRaA3zPkUSfqBZFPsv18qoU69+CDuByj3gBqe8SPrZusIf6DWK8NVDS2FQPPMjoVuJyMiK7ZmLauWONZ+juIqH/kMZv5tpLK5ccA/WpsDFvSmkJRmU3M8vlxKVmDhMw8oOd9zRYmxf0SIOX7zLpYJANpg45qQic5LxtYBmYribyD3aHPmGlxShuRlkhopnG1qR4AqhdNLPEEcvhtXusDCDpAnvIftYalRyo74IMfU6oBbHy 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)(346002)(39860400002)(366004)(396003)(376002)(136003)(451199021)(6666004)(478600001)(83380400001)(186003)(2616005)(26005)(6506007)(6512007)(8936002)(41300700001)(8676002)(4326008)(2906002)(5660300002)(316002)(86362001)(6486002)(66556008)(66946007)(36756003)(54906003)(38100700002)(66476007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: AfIxuVh3RqsxjNFaIs6CKl5kwCmk2W3ZHYl7eFiclAm0Yj8lL/wv4+Veg3JWNhH+VsdG7kVAgQT+aRNYMjHDPV3OooRHIjzdK0IkjpDJfi9g++w9OBkuS4KwZejtztQsfhneqvZq8MYCOKJaWXMAX+aqMW6G78ipp+o1JOE307xj2UUA16BjJzKqgbuicTJOGwz+MOp6Y+O0i/yIrI5/GI6jdhsBF+lw1bSQFiwhMqNhX/lmryV9umjIm2M/+9g6H79HGyvNEvzANLVp/QkLzE4XoTxpFP6AcqhNLwkDAL+Civh/LzeH0a1sI+YOphTFDmWonea9UWXgC2eY/Z2pjHPIVPFkZjbCNA8O1NcE5H3NvX0iUi9nJ2CglMfJz7VWzUi5EGj0auHQIgLFhx0V9zMJmEWb96i/83CLpju+ojWYvO/FpmKT9v11YaY5buuvoPcnUmwp96pd8EXKVwv6Hjrz1qG4IfsMeqQQ62oMuiwvP7SLCIatT7uq7ydf3TgcSGkJDlJ/nKsWHDwJrb7u2BeQGK2FvOzNOLNyCQTmKIKcBdB8kTLBDO3sgURMXPWHavdaOkYamry0LQFHs1aW3iN0OYoNDepBGtNILNILmaHoRdpvPbqD2R3Ag5WoNw8jH9uEHQ9Ro97Ip4Oo4stlL4BH2xk05q5lB5rgD7GA07de/kesNrNpWvapNRr81pKpKrttE7kYmTjmZ2OtjIhL7HjGZKF3M47CW1Yf0zgL6zYPw1/O6ljz8DdYhMTvq4JD3A8/LAi3GkQlwi6mN2UD+v8RqoF3EVuqlT1IBVcJ5yJsF1TpJpiehPMmwfv2Tlx6h9F50q49B/MjofBZ6NS6B0CXBG9nIPH/S7CqUZG0ooFav9b7hyc+Tkon0Ea488uhp3K7o14GWRgBTslNmwvA0+YMWy+DbPUL9T6U0kno7OYBZzAsMSovx8mQgHy6TrED6GC0/NJgpM6fJvnMMRrrzH6AWeQDXfdmysIRard3bTKU715vtSd2xc9ePo0cZtIbJlFIXRVd9nUCzyRLXdZXDu6JTBqUFukK23WAVZEeptDKojpuCtXg3kCPNG7iwC9ztI/4NvBIxBQk5jsstWdRmG8mpO3zkwGYHEoq6e+WYYXs8nxKJ3o6Su9xmx7kB1ZVXsQ4Oy9fZi1tZ091lezb/O3xdLWZuOd7po5lFMMEQTLQwhZK+8esXITnSr5Td0RsfbiJmiGPXj53VpNsHKhY/tXkWt9BSj7DDT3uSgaphJn53EbTAD/kKRJ7pLVpTAzVEOTlLJEEz89kmXbfnSwtPIIq8Wqo/qirUxMsLxmvW9GW6iMh/4K2F5EDRaKftjEoko9fsX+mFm2NEsiC16ZlRtvHht3WzOxuG8Q12sIctiFU3dgWUzBa7rt57rgj62xIHgTdd9ATsCtbWgH3tEWEdzNHB0o/Y3xwUXMlYkkuPt8BxQLMpAIW124Y5gg1SqpkJSFxA+ZqSHxF6PsPHCt4Ao8RLP5Kf8hHTJaGEvB28eTjEVA/T4HKULU98Qf/8CvQ5nxQZ4oTtUWu9QiCgOHzUm5cOnMXg0d9QGJMK3JaJPy1Y8ULCLF3eBKhaAm4VTB/ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 490f4a91-06b5-43e1-73e0-08db86f15b7a X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:18.6402 (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: 9mT4//Ikm+tVTNweW1tgxiitbbjTkCAhar76YwxeaQGPRMdpCc/XjoU0iGOetoP1 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6205 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 Tested-by: Nicolin Chen 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 eb33dffb955dcf..9d43334e4faf16 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 96881ecca5c757..78841aba47d038 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); } @@ -681,7 +681,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), @@ -836,7 +836,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); @@ -885,7 +885,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])); @@ -1033,6 +1033,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; }; @@ -1055,7 +1056,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; @@ -1249,7 +1250,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, @@ -1458,7 +1459,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 Mon Jul 17 18:12:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316193 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 CA61EC001DE for ; Mon, 17 Jul 2023 18:14:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231637AbjGQSOP (ORCPT ); Mon, 17 Jul 2023 14:14:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231684AbjGQSOM (ORCPT ); Mon, 17 Jul 2023 14:14:12 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2046.outbound.protection.outlook.com [40.107.244.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F202C1731; Mon, 17 Jul 2023 11:13:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Bl1VapG0+Kju0rLBJZa9gYYQurBXTF6Lbn4KP3Q+OjnJFzw9FJN9j5wnXnnN52KQfgtC45EFGylTQmtrWp1qmMBPgadvonBGKuJdvgol4TkaKRA4s08/Wa3TfXmO+CvUVqHiuZif0K7564RrWOK8UghrdGDgyuGzOCEV8WCqvMf7j6cuzPPP8PcALfjdDrxXaKMjHV6KiGYg5jXuMi4GGX1LmFperoXPscvv0lQ9ytwvlXuVYaWBN3LdhC82lRHbG5qh3jIFEpg/mo43QZ7UIIBrsIYm6AerNsyjEjuQERxonB5RdQdjn5CTPcnZ98uTveIcxoB6d21L3H7+swYajA== 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=6dttoGv0tLDpUn1w7Pj0S44VfDSejSDJqLPkW9nuJyI=; b=XYo+P4jj80ECZsBEXOw+VHWSWLm6gs0NYke1dHigNu1C7PrPDQtkxXWXJJ5e6JYzVawosY3TzMNWoYQjKOKowsEaup9XTmVCX02jwldC/0+5RnLBImr4U3wvfJKg+rM+0EYJqyV8De6tBJiAqSGzje8ll92/sSMYtxwbyIWLa/MrxHCF1OnrHgYlZGnCzpIqCvEWDFKOZHehgzN1hJwqhkvQNWZH67+5pm1Zs9PxbDaupDyxN93foZAtrgzV6Pdtzgk3esvbxFmc00T23QJlpXqxs89P+2IBnZsfCnT64UdT1W9CowCz3bWR6ImhQrSrsHKI9RUhk0SV+s4A/YlXzQ== 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=6dttoGv0tLDpUn1w7Pj0S44VfDSejSDJqLPkW9nuJyI=; b=i9xbCzo7PQAl6/2mrphzlBM2thpi0goy3iziLZA6TLbYvF1eDop1yz7YU5mOr6Xnpm0ODesIhswRxugGp3/v2gYgnOStDIXfn29Iq73AETHVad7Xv0ANJoV5ZUknfAbN+Ba43R+kwOOD0g9wFL5ay8qa9iCdXnAt/Np/fkta+GmruGhxFfgpMaES88NEpzFJYIqXY3qEkVXUahyk982ncxNATDjW0P/ubyFTW51A8MG0bW6Z3+BIuT/21Ff+MfXm9yEspcpxNVHw/GS4iGzDNFL/jZEOM7TmINce8VzF2SpxJloXkH0Myx3NWfCnE73AJVeowIDYxmB2OF2Z0YQudg== 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 DM4PR12MB6205.namprd12.prod.outlook.com (2603:10b6:8:a8::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.31; Mon, 17 Jul 2023 18:12:22 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:21 +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 v8 19/19] iommufd/selftest: Add a selftest for IOMMU_HWPT_ALLOC Date: Mon, 17 Jul 2023 15:12:15 -0300 Message-ID: <19-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR13CA0202.namprd13.prod.outlook.com (2603:10b6:a03:2c3::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_|DM4PR12MB6205:EE_ X-MS-Office365-Filtering-Correlation-Id: 723008c6-58b2-4fdb-3859-08db86f15b7f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: UbhyC69E/nv0HKd0xf3bUwrvMVKKW3ur64crglMOwewqud3gXITipCRxLcIu3rw7rmaFKusipDuOIHs5CI6TUZ00Uk59tkpdC6Xaw19nqwIVwQ7veN9WuttFU18TlH4Xl7FX3X4wiqfKUnitbdOiGBriz5z6HS97A49ixSJeOhmCH9KfIq7mTC68VcxpECkp2++XIdSVG8E/kFpafpjx3UX5wsdshkwncMmgzKVodbU9EBLTDZA/xsAsCXVnmJD60jJw1rGoeXuKfaaAQmCcIxG9wZK0c4OJik9AmoUilciCK//eYTH4ypRBh7r68U8aaM7ZJClWDuwAPw9CjuQ1x2BhLLcIxYXfnNzjfwViWq6YzFeT0NT5+7b8C5afqWgGrgZLGDZIueahJ0knr/zZR9SCmwTjWn1rdK0j6395rMnkFfHfHXsIGIp2Ipt1GbMAk08+fkQyiOTJigUcb6Dk0VSlvoM10ru//02YfjqOxyQFsPpCtUHIKGyccSWhzm7tVitB27lQl+Dpl6h7+uQCCJe5UgP6hFPwZqqQTlGGEQxKS0AsGsM3qtnenxSYOIIw 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)(346002)(39860400002)(366004)(396003)(376002)(136003)(451199021)(6666004)(478600001)(83380400001)(186003)(2616005)(26005)(6506007)(6512007)(8936002)(41300700001)(8676002)(4326008)(2906002)(5660300002)(316002)(86362001)(6486002)(66556008)(66946007)(36756003)(54906003)(38100700002)(66476007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: YiekfgnrdvN+VXUN6pr+xEsdVQOS19IoTaHc33QeYvy7W0C/2pUmvJkQHN450XS5k44d6UWeWxxANWnsguDphMdwaz8h/WVqzYjuIm6Wy51eUzJdregkCezhcjtg8b4sKRm7UHovoSRHliFJhm5XjD9/k3q5LCoekhQCIjuDrrfcVFSET50bT7/5V+AIfJDgj12DdMr1uQXr6txf2REOSCE9Wl9VjE9r5GTilHaneppN95neb19BGUE6YqbXqaN75zFYGIloH+48+WCPSy/jZvIYGbnJrb2P4St5Sxq12htGN9aHyEC91nPCuT2S/kjcQ1+uSB3X0QfmTVkAACRwMlBnRh322q6S9uy0kny5JMFdiEFMZnJdL/GVygIJXbCHs2MGkrm4DgPybeqL/KZ66r/MEkmXr1GP1FeN7gnoxzdH2HljsIAB5E3iE/TWeB8+k3AJIvxTCVqxQyzjQ0pauPxghbsOz5jxLuUKlddEAkyMFDe1avqx6BGuJTDrjqPgH+aNQqli1qM0DMV99te12AeS2cPx4/4A7zVDqrTOzpNj/xiT5cGrXjXTHdxOyD1zyVODjsKOyUiePs9OPqx9tP6tYcJd2VlR94U9Chw3Og308W78yd8H4jjtoLYeX3LdlMxypQatJl4qXQ/GEhV/84TilWGt0ixHFxWt0KKko5davZQrTcgg58MElQkmkpY6V4eN8kA39MoZtQqH8+FD8cltiQdZgxhTjYYCx6bbXPeIDl04DrJcKWepOoE67Wrg77uGmcU8Dmj3FfiU234+L9yQtkUyXd2WWx9TIaQNiti93pIiv9Ezw6ThWy56LhaVExgJBD87pvB+KCymEflAI1j13iRddJHstHDMEFZRkY+4WQzsdxDIArTNf8KoSDq0yusQ/EoTyUYmfvDwv4XsJnwg/Vh5eEYuInXEi9DPjO1pRcbJptgvtkO/xrBGZDj4ekgTU9mxUtsqn5htie70UxWv0dY7uvAF+2SyZ/reEpyWOpZEycdM47Y12jSLePTLWSeUcM3asAlnllFEcGt1hyXlZT55hV/stZJ5Ge38iXz6tjiKQO7xjmX+hrzS63mc3v9CdCEjuP2ggRIIWlgh7aUCvStFKzzwWWHXW1nGejbWq7LvOULRonn7q4sYAae2VXQr3QPPSj52QTKf/xqlyF/HaXcmVDbPex6FibwVpkcy4j0ic8hXIEx7d2UNg55H/2FOMWz4KP5KnCbXIsZ8XgqvzhYQzT/O/9COVnXIpGsyeH+ZvSkfLqfqhDMegdSRcU0o0/FeSwfTOukikEAjnZ1u6J78zne+SF4YddmRMCNGzkoWp9lQD5ZiaLjpT+zIDVv/JaYKxDYCYT6xJyVwvGwVEx8+tF2fX/GKtJaNaIGg/3goIjIfcooHR6lUsuxISZOzbdSdTSQbSPfGZgt0DUOyeNqps9ZyyyOa0swckMsypwfT0cHgADOAyO89wv5Pw1xUdYV1UUftLYp3CYHnYRphRemNxs6Icu7C46jUEugJwov786ckUufPAUihs7ZcAEnS7Z232CglGp3eMjsfsOomCoQ72S3LqYgeZcI75PMVrHxUQTdgyNeVh+1UmXjR X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 723008c6-58b2-4fdb-3859-08db86f15b7f X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:18.5054 (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: MammSpFFpd0pHXl7YqProk5O3H9oODvNtCrkG2y1jEikBE8YGBDf0O9JcEdXZQxV X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6205 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Test the basic flow. Reviewed-by: Kevin Tian Tested-by: Nicolin Chen 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 112bea5a84e43a..4d095115c2d0b8 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -1180,6 +1180,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 78841aba47d038..dc09c1de319f05 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -1338,6 +1338,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) {