From patchwork Sat Feb 25 00:27:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13151885 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 C243CC6FA8E for ; Sat, 25 Feb 2023 00:28:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229677AbjBYA2P (ORCPT ); Fri, 24 Feb 2023 19:28:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229667AbjBYA2N (ORCPT ); Fri, 24 Feb 2023 19:28:13 -0500 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2061.outbound.protection.outlook.com [40.107.92.61]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C18591A94D; Fri, 24 Feb 2023 16:28:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mCh/TjUh7EfmZSa83boPaIB3cLOE9vV8UV1fJYVQxGEl7jGu6kVQaayH1AfwY5mcKfQHWjXnjc91SERPvukOSXd8xummzZsRbAycV5nr49tagNhLKaDJkSkWQX+9mqUOebULUnqrjeyoMKEmOOFmlCYYJZzuBRpDVUTqn5rcPZs6jxU10N/o7esfntM/11SfnYyhPeFnqHZ69YyoGnLALpUqoKOny79bYawWR86aGEXSeQzsCKFIKNrBsWbzOrHYYszbVClta6cacs1BA5WAc7t9DdXtTveJTWVn83KvbhY+hZdjF7DZXlAa1oQaxXqUBbFM6JMb17SRyVxoHQYNXg== 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=WR4vk97ODvhdpLx7DKr3vsd678K1TcH8M0VYT4/Fyww=; b=O92ImgNPHChwlgyk9TLK5uISTXXjbkoVirHk3MszrMGr5iZJPcevavwKvZxW7/Whe4/43wc/Pr4srOeWkScoflaW17FHHy7npuIO5QsDNQsKscvrabOto9t5l/IlcR8Bv3ZeGC/J7IzoCZblF+9YX2an4j1EsIdc8Azrh5NJhJIsBUmoj8eJbKfmfEbRf07XOVf6vMmxeeZElfKxiFSjVZz48REfK6zxY8KiN0ClR3VwuX9+zacYyRhPYJb3pL43RwxFTxinULVoQ77QZufB1emW1bMreleX4OGYoxRVk5FSnKRxNVvUYY6kg2ygliLlT3X7vVT68I9SiesFILAPXA== 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=WR4vk97ODvhdpLx7DKr3vsd678K1TcH8M0VYT4/Fyww=; b=ba0O1O4oXrQQkpT/VNrn9iiMxFS0gANQOvcMdhUa6VdpUv2J7U01HSkA7Pr3vkrVMFgr3cywGQ+K05xhRsWL5pWVOwLG6D8IISJYsusM7FBwRI7m19iSjmnX/XU5JeI0sN6KUYLWFV2iOftFu4s0CsDThHYF8RYjESPwaPiNhrdCWhx30kIKeNe3vomv4A0VOPA+9+mfjaZ8pIi8sFe2QT9CJYwXyLbaSBoQ/BInwVe5ykzUzI8atu/foYO7a/QHHepZ0149OpvxobMBNF/228gyf3wVDbN4wTYzBivEzuxmiVrVyP/5o0wttIJRAwamEZD+SH3sAtktwo30I/kZHg== 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 DS0PR12MB8019.namprd12.prod.outlook.com (2603:10b6:8:14e::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.24; Sat, 25 Feb 2023 00:28:07 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6134.021; Sat, 25 Feb 2023 00:28:07 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH 01/14] iommufd: Move isolated msi enforcement to iommufd_device_bind() Date: Fri, 24 Feb 2023 20:27:46 -0400 Message-Id: <1-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR03CA0136.namprd03.prod.outlook.com (2603:10b6:208:32e::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_|DS0PR12MB8019:EE_ X-MS-Office365-Filtering-Correlation-Id: 2f88df7f-92ef-40e7-bf48-08db16c727f8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sQB0qUteDV+li0Zg9zOpnOQca27fNE8loyal8l5mxGSavghtcXpm/SV0LarHbBxEAxKuTIyFeAOx/3X3BOh5cJvophEgtoW6QVqEFUpdNRuCDDX/18KKjlbyNTHhyLPRMRjNNOpxaX0UG0OJS5+rgtCkkgjzYFJ87OJHiZWrnSSIPgik69YMxLAkMw7pd67G7OL+ubixI6cNNNp+4WjuKaEIQMayWlceN8HlckfHJjhSncxgqANsIxO2wucZWQuEDUUK99zRGiyDFiztdHXLFQLzBV5wuSJmBW9zLB34L+AQHWY1KkLbHthD7MA3nxoIb/8sViRBpPa8aABPPmdtalasMR0o3hTs5KspPSHaW4Mw4zRXiKi05N1XWglWOU1ozxKbML37tQrM+MqP3SCJI1BshjqCQ82jlIinjz84Fvj7MenISgnSJpaOUEPwDVvaxVHiG6TeeXNgtq2XOBMUWepNuQy/rPI+XYoKJbzA3OiebLhWDQ9qTOXxO8Fwc8wLooQh5ZRhFCi10or7/yQMxJZLZ10Xvy/BiaUBtHwbzDo0ZfruggcHZ1ZWA/2f0ADABSWbVoASdpy/maTF+c94I2JGVStTh4tWdasavhktbvTrw5Dys02WFd0S89tEAUBNbqkdrc0tfnKnAsOCH51QFr0huiFQzqvTqmhQyJBDVz0LCL8DJ6Xe2U2HzY0Yrrt1 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:(13230025)(4636009)(39860400002)(376002)(396003)(136003)(346002)(366004)(451199018)(36756003)(2616005)(316002)(54906003)(86362001)(6486002)(6666004)(26005)(186003)(66946007)(2906002)(8676002)(41300700001)(66476007)(6506007)(478600001)(6512007)(5660300002)(66556008)(4326008)(8936002)(83380400001)(38100700002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2cAe2tJddDrmTS813qmWI+6UG/5+WQ9bR+j5zjGu89mYAINNAFiYWbcFXl/eV4wjF4fksW2RvGoE593Et8Ns/of+m7o54B+jbP4REE0GW4iwNbYXhcfkihYPKVLe7G+Ts8WTcLaOxNj7W5HV2qIwF7agng1+aRcpJQLL44mnuilMkmFry59nLKu+hjycunMFcmwi6G7uCIFu+iA0664VsG/clhtKeX8FyelemjK0bsSQneSD+UftzC4ZInCWRetWCidL+HArQc26L0mETrDuRMcoomIY9U3cV1BTJCguGZs3V/h82BywPg9IoYkqIG2YAMfT9z+vTAMnQygh5dpbNF3scTJjMXI8pkvw4q0j3aFDPVBh8ucI490iKHuINuxkCC9oRrrB+yivCuZwL3Z2D1pqScEGl+8cpNN0qKRYHGZrstK7SC/aonYCeZp4RTxOUEKstfLHA5TT7KNk6oqTCNH3KrhUsaioNUvV7Rk1OonrfJSKjyl4SKiCiz0IAwLpCG/G1eD/UoLViYG6Tni2l+lJ5NFuTCFTdVUGoqOdifOla63560duggGIfbArk7/hqr+gx4egojjDdAmiFUNXiWEPQbn9cUyTcHxCVOBr6gSx272yorEsrEbKkqFYmf3/OJiX3iqjHplroBlm6QCeGXGxtA/Z/m6KQNms5XxBXPSk7M0MKY2FX/8dyIZjvM7xcesx41jDOq2NiOvaueiBqXELc4QIUHT1CxB2uE9KavIi/SLN2RtkuaXgMmq0E4w7u9y4v/a0ugGhb2SbTrVo4b9WS0/aC6InJap1QWEXp04bCp+tf4OUdSapX+Iydwcto2TUt7pyE7CztMQLzrX55y5tOTfYco19qnQ+jIP065zs+ccmPSTri1R8dmPtrg6HNAkhn6nykX3Sc59XOCjIBkVqLyldg/+uD5MhwWrfZtIWjQVGIIaN57ddTwvVtNd/4fwDnvcY8juzMi4oK8RfOdaGx52IS5d/sL0vos3yXQHwGWHZJnTgmkGh8136+4P6M1bB51Sm3qTD3Al4nAUEOaug/OsJh3r8GkoOBsUZOlGcc0NP396hma+1biaswttAOBqAPhtHbYLQ8AQq6t4RpP5V3nL6xxdsx5MxOliogCXsgL3Kq0XyYd7JMNHxxk7QCPuCEj53Uj0Y6PMWZNmWl/oTnidiUqdE4Ul8Qr1Gk2Tqzp+pb87p23W3imPfmtmIePSrAp6SjOdZMsz+6IdrziGB5Gkgfp4sYbdDqyLh8bj12O+c8BBiUrCq3xaaDrxN76ClNO4pUsCDFx/PRw9IYR4QZSxpqmq1QpxA6x3Q4PyMgV8ITuyRxoULCbyQtzA6SA9yO3kvaEkE6ZcW0M4kSzAPyyViWhkbvZpjS3UZUbyBTS+Glcyb41/eVfgueqJxGikiFsc+Ssley2po7bvlD1oG9MlnxOpdQlTBYCCCFGOSclGoQRQYvodC32JqIlbHZzZvTBU/pnFiXSd8gI0mClzeaqgZ28zPpsSJv5JTvXO1GSX+ChsMJJ1mOymPbIUE62So+OpcB23LhO7fUr27MXJmeHJLgp9BlrvdeGFQfKlD4QCq4/rV2UkJ/8bgHTuZ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2f88df7f-92ef-40e7-bf48-08db16c727f8 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 00:28:02.9672 (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: iEh+6UD2+meeda1kN9ESgeo51l1uvukQqDSaaqD5Ip75BFkPnHVHYuU2yr05jwkZ X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8019 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. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 38 ++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index c6f4852a8a0c08..63b65cdfe97f29 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. 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(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 Sat Feb 25 00:27:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13151876 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 97B68C7EE31 for ; Sat, 25 Feb 2023 00:28:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229576AbjBYA2J (ORCPT ); Fri, 24 Feb 2023 19:28:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229607AbjBYA2I (ORCPT ); Fri, 24 Feb 2023 19:28:08 -0500 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2073.outbound.protection.outlook.com [40.107.212.73]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 622375B96; Fri, 24 Feb 2023 16:28:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=g2CqrUUHy9FOdjURGHSuV9aNmtS7PzvTOhtVz9PkAgszsjLUc1N3T7xcumcGN7GJ6Q8D0e1hX8+YqN5uSEyVBwUzS6bMdGhAXHRqE8DtvaQbDRdC42Kilqih5jSYKiz9kecPgV5r0ptaPQ8Axr5rN5kRLKjRbdCqGi5aYdFnhHUD4mimBq2y/efY23qDM3604GytNThY8Tx7Ds1KS5lgceif/ahtCY+i5bnZy1aGwr/IHMMxTflox4JIELyuUNYHl92CnVO8Fdyfp0MdhuwxrzkEhihtr4Z01684K4HJmuxmNLpkcmolM33dCVv4kCgmuQXoJB1NHKdfA23tVKHqcg== 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=CGLJuCYYA1vnqsxOFyQDXK4MbFCoAqlxbAzGKOni9FM=; b=LwqJJCa8CW/exnEu7sKwKlQOULXvbZAOiHWd1kusyMxIYCgRSVqDrN50YogxZQMTO9g3bpWQxE6jjo1wHmG59pIWFGoHAVyzd8+hfRTECpYB7NJuAVkfu6Sk9eu6usU50Ii6lC0ORJRf/uE/iX6bX0W4qUSZgI8m/5JnnkNjdZmAw+pJ3PuDpgggW7Ide7FMZUiQ7+ht3ySbxSqaEZHwq/AZ6BjUNSFsllGaX+As3tmYaNHSp6NkjFUfXdsqma6jQVV3/zODBK2ljKDiWHy53xf2Ie15ec501NvJ+XGo5n7SR/C9ntQBLVuRfJ5h71/NkvguOA3wHAZRR6pk0qsZlg== 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=CGLJuCYYA1vnqsxOFyQDXK4MbFCoAqlxbAzGKOni9FM=; b=DVQsnOV1R8CK1Sjp2IPHvpCplcHWqzPhKPSepMcObvgN8zyxFhEU3TMBJiw6sDyrBicGeQucIgO933mRUymngeAzPV8opE663ocHM/RCxcaSCOLL2gU3vG+XdZ/KKf/x6OE+ZYT1GDgEqwyFfoXYNwifpU7PQl61vEzNAVxBGD5behGTL8ixPBu+eP2Cfe8wM29TqcKxml21Zski0vXbK8INP5LaobPKHoPE4pUJCeIJ8uV4vtUYn9LFhrFlWF6kNbpei673L550roUAmGHAAUvcWS85PXgSKDgArLwef6vlVfm1azzCr2N/8AmfM5d6qSzCUhJeTf7JG/adZ5eW0w== 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 DS7PR12MB6096.namprd12.prod.outlook.com (2603:10b6:8:9b::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.21; Sat, 25 Feb 2023 00:28:02 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6134.021; Sat, 25 Feb 2023 00:28:02 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH 02/14] iommufd: Add iommufd_group Date: Fri, 24 Feb 2023 20:27:47 -0400 Message-Id: <2-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR03CA0148.namprd03.prod.outlook.com (2603:10b6:208:32e::33) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|DS7PR12MB6096:EE_ X-MS-Office365-Filtering-Correlation-Id: fcf8a622-4845-4bb8-0749-08db16c726e4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: u2P4wI/fqPpvmh+ZylXdYixwiZ94/bXU7tiMhD3NGhzO9aPCi/DbElePKsZPzS6oJWGneqEB5dGQ++J6QzARqVh9TK3ds26JfyEMCjK3yC0O8eGPbobKighSIXwe/VkGZOSjJ27I91NPglpE6wrhUOTSP9JCfukObdBckbm5MO1RQtUXVKcUp6DBJFXETCB7j+U2cSilUWKktQTBuBtmrqmCzP9+JXlUfAv31fwdCrgd2kByExI5ggw28tCKu6SltSQ7DtoHOfoAXqW6c+uMkQ26gX7gvPwnlSAcvfx+UoisgWcgaA9aHCdyxDHIqdB5tOEjj4RoMXGXfDkw06JcgA2Q/FX8WVxR3zc6GZjkqEHfcZ1IuyqugXJwCyxf/4apoPseTP7GBVi9wb7bAIhfzJm8dAAIrjPEIVnsbeT0zeF49PRqTRnxI33xSWsfjesdgQohP6rGY3yhMkIH/LAWoaCuB1TKGe02JxR7oFRQCduDOiIzO6qvZjTVjfXxDUBzVavHMnTDTV64vn3Pzq68APkvLD2YsFgcc5D/LGd9nEkJsLXBfAQeH5EIHl2KDInaKa7eLJQEXsTJd/Vxu1G72Mm3yqneAs7w7B5L9Madc1ElGVDRWaFn/V9dX3adqU7S6mdKm4dTFAk+6yd5aYaQNeCDsig+x0PKfcWFMy4lA36oMQpFzMbmh5EC9B06Qn8C 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:(13230025)(4636009)(136003)(346002)(366004)(396003)(376002)(39860400002)(451199018)(2616005)(41300700001)(83380400001)(5660300002)(54906003)(478600001)(8936002)(4326008)(38100700002)(26005)(186003)(86362001)(8676002)(66946007)(6506007)(6512007)(66556008)(316002)(66476007)(36756003)(6486002)(2906002)(6666004)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 0nEgRVbTCqbRBdE9hztAMD4FjbibT/hkxuCxCZpiXhh4ReAlNXRhcumYeWSaARtUhEhHgyD7SZsku76G7zhCP8a2J5hVXcD4Njsyq42exdoFCTXjhhsmLvNL/dQM9ZoCR67XFQ/YkPHSEDrGIBxph42tSJe/MOcm7CqfAGF5ywPgqdzCOffXTBa+zSRzFE+0UPK5dWSKv+q2uR0jbdKizYt1o5kQYOI9lc+Qni0rQtYywD7Kh0sTZmR1Q0F4xjB+lxIFAtWEIEA1SpE1Nq0Pbb1ZPvdXyvJKTbOzvWckbPhBaZwi2U53EXJ9on8039nYwO8xNxWfOMUybnRKEQ1MMZa+HD6D/4wuexCszByFL1WbAadM3iUfd3hv/GEMQU8IJrqDDwGb0Yularhgeb+KqrVH2QcCfHREE7G3JvvGaSzD+eJb4/WZWTm2n4lycxqgGTJGiMADua9C3+D0Ga4Lb25QNPimJLvEnIJARacy6TsLIilU7X8rF2Ddc7xmLbw1q0pIkqVzhCFQZ7TeIm7q4JIbk4n3q8fnSxoHnex+Rvj5YnqRaTaWiOAHyHO5OWv6kiSHNGdm3BB65aRIyVPWmLAr47+mxw7oh/mB0ruNmihFPDoq1Yqe67lSPW1R2mdI48nFlDaSwIufZNtEQTKZ96hNHw3lGZayPeDWW4ZEdC0XnUFhd9qFURfBRC5GhazpW4rbJEX6tWQg1LNjlXzbKcBeTg8MwkQkGE2GmytS/jTpSoPrLYgCpIZVaZv8XiKOlXJ1DUs2ySN5UCO4EzlInA50mQxfsfQ2mR/w6a+skMajBTJGVCAyBl42H+OH7vUGlWt5LKtJB7oym7HNlvTmx/hi4Bg8tQF5cxh8jSh/K1JVuvUWXPs3rEOaaoQIY/ot7EZApFo8h3FfMk5sP49muM6a8gsZOlhvsxvVhu+pHcGG3I8QHX3RWE7jpz6UVQwDSGruqkr+bS52uLgytHyRXHjGQqFPyGk7qFwZzN1pssIl6o2O+u7cQ5k0YOyadhS7jg1O9oxakx9DsxHAQZeJ1/G7OMHyCSPdJcmBsvt0Zw5wd4v2HsB2DI5ZQTHHXZSA6nPNBIpRmPs6zAleQQcy6srMEYiAJ/cJMBapO+6tTkQBcDEjFXH0kT3sMwz/4jd1tZrN42pRUwUgvt9+NGvGPTAEO/5WovfJwPZqSUVTmz4Aayr2/1IIF1LlnFfjuFM5Rr8FJhYVZb18nrqc7xSPpR6IInhZH5tphmkMlc9NoaOjozuIutEDYJ9CtApHou3KGcziEfGklfJ/6xvekpy8+e0Nl6Y9Mo3Ip0t0UmfvY2EBB9qSplWFCYRThmEPXKqOYoosvk9yOV+t6C8zp3ff2dr5sWtlyoiiX9r8AA0FKdlAcbXU46O7FqL8OIgrNWl0f+rHilT1UfDkK6GnbTc0sgAT+hhnPpZiiQZigsg9GmW5qRQBRid/XE0K2i9dgLPwfY+GGhKTkC3cVA5i3SGA798GmD1cuUtHqifBy3nAap5/DhR3XFaZ58hGag9+b9B0taSr6wK+kiKRFytSNIuKE8n+LVosljr5oRegbmiWows2fs7TSIZsPxt9YqEGOWBN X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: fcf8a622-4845-4bb8-0749-08db16c726e4 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 00:28:01.2181 (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: GfMBWOJ/RWCTTNa1LsoQLjPdAMZ7IxjeVJboETLx9IGvcv3mzYlTJx5sgpFegSr8 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6096 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. Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 128 +++++++++++++++++++++--- drivers/iommu/iommufd/iommufd_private.h | 9 +- drivers/iommu/iommufd/main.c | 2 + 3 files changed, 123 insertions(+), 16 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 63b65cdfe97f29..d1e227f310e823 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -15,13 +15,110 @@ 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); +} + +/* + * 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 (igroup && igroup->group == group && + kref_get_unless_zero(&igroup->ref)) { + 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; + + /* + * All objects using a group reference must put it before their destroy + * completes + */ + new_igroup->ictx = ictx; + + /* + * We dropped the lock so igroup is invalid. Assume that the + * xa had NULL in it, if this guess is wrong then we will obtain + * the actual value under lock and try again once. + */ + 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 (igroup && igroup->group == group && + kref_get_unless_zero(&igroup->ref)) { + 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 +143,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 +153,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 +164,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; @@ -98,7 +195,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, /* The calling driver is a user until iommufd_device_unbind() */ refcount_inc(&idev->obj.users); /* group refcount moves into iommufd_device */ - idev->group = group; + idev->igroup = igroup; /* * If the caller fails after this success it must call @@ -113,7 +210,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 +267,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 +308,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 +321,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 +335,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev) { - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) - iommu_detach_group(hwpt->domain, idev->group); + if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) + iommu_detach_group(hwpt->domain, idev->igroup->group); iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); } diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index d523ef12890e1e..2544f10dae9aef 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -17,6 +17,7 @@ struct iommufd_device; struct iommufd_ctx { struct file *file; struct xarray objects; + struct xarray groups; u8 account_mode; /* Compatibility with VFIO no iommu */ @@ -262,6 +263,12 @@ void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +struct iommufd_group { + struct kref ref; + struct iommufd_ctx *ictx; + struct iommu_group *group; +}; + /* * A iommufd_device object represents the binding relationship between a * consuming driver and the iommufd. These objects are created/destroyed by @@ -270,12 +277,12 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); struct iommufd_device { struct iommufd_object obj; struct iommufd_ctx *ictx; + struct iommufd_group *igroup; struct iommufd_hw_pagetable *hwpt; /* Head at iommufd_hw_pagetable::devices */ struct list_head devices_item; /* always the physical device */ struct device *dev; - struct iommu_group *group; bool enforce_cache_coherency; }; diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 3fbe636c3d8a69..e5ed5dfa91a0b5 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -183,6 +183,7 @@ static int iommufd_fops_open(struct inode *inode, struct file *filp) } xa_init_flags(&ictx->objects, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT); + xa_init(&ictx->groups); ictx->file = filp; filp->private_data = ictx; return 0; @@ -218,6 +219,7 @@ static int iommufd_fops_release(struct inode *inode, struct file *filp) if (WARN_ON(!destroyed)) break; } + WARN_ON(!xa_empty(&ictx->groups)); kfree(ictx); return 0; } From patchwork Sat Feb 25 00:27:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13151881 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 91CDEC64ED8 for ; Sat, 25 Feb 2023 00:28:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229669AbjBYA2N (ORCPT ); Fri, 24 Feb 2023 19:28:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229500AbjBYA2L (ORCPT ); Fri, 24 Feb 2023 19:28:11 -0500 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2061.outbound.protection.outlook.com [40.107.92.61]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC0C518B1A; Fri, 24 Feb 2023 16:28:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QAXlZeMob0u78gSal8b+GrxwDzJ8AdOy23k23myzX/y6Y+ZkJTlzjOvJsIX7KKwR/pbk6vOPH5ubO4truYZttvxL7k5XNzYpAaLWr0MV1NGsppc9BH0IQNo/fA4LNuiw94TMZ9hGnAaGPc9cnldJwJC1OtIDRDylyWT2U9nBfkN+JR1Om1Aqa2s9h3xf5ryLLxO3tVXGRc4Gu4LkHEDc7VMKxJmJIolWgMRsIElQEm7MtOqlzT0XaGLen5cLb9XuPynXYwia7oaV0282rS9IttvjRe5IM/xOIb6I0OqVXR37q/yvaRL90OisDTAyZiwvq73S4UmvU9trxCtzkCRgpA== 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=eh0NycKltfduhZDQLFW4V562rmKPtPvJsMvEKchc71E=; b=aEUs0r3uv72AcDJhiI+R8H/9k3nq6AKik4dR2O8rQZURZ0EHqyKpqyUp5+CQgWI+SxZyGrv8mXhOzt/LpDLcJ1RegH64vQPKLHUCtNtQddNWP7qGH2eiRpUYD+Cv9vxVDFIeMnnVUmPBP3i5WowE81zU0/OxCwdYKsG0NGt6ylWctHuw47nghN4472HVu5dee9hlp8x+aY8zNL8nuKnRvgps00+fmpXwBdiJpGstmg2IbBCiE7zJsB4sdr1GyRjhz/f9+bsH22AEyqoiuYqEVYT7T3OFIPrCautcoz7rcL+eV2NOQcKzcX/I4RjzPEJoYKjESTx0j5WvIRdJZOQawg== 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=eh0NycKltfduhZDQLFW4V562rmKPtPvJsMvEKchc71E=; b=HsMXdsb0/ikFzcFn9NF/ZLM8aTURegGDOlYLzgt7Ew/0ApxqKS2FxXky3EpdR5rAaCG1c4mhxLHOv8XMErbEJpiUbdeB+o/pH+2s1YrlOLfE3533Wy/E9SLNe46KfA63aTXGHgegJLAK0RvPBDzlqT6Ryjq7W/HfQqFGFDAJ9RcWPU1d/lCB7Wzf4WhYab4QB6hAHEdAZPGWsUwiqpgCEv8aG9apWGt4VtPDQNTjW+yKkAQYRyZZuq6O89RMBZMgDW46Nw0oIVhrbgvmjALxGP2Fbwo0uOh4AgYoVJcbLrnEwNLO81RJ1wuUSOGPsPEW4aFHzdbz7+W7yajQAvXmsg== 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 DS0PR12MB8019.namprd12.prod.outlook.com (2603:10b6:8:14e::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.24; Sat, 25 Feb 2023 00:28:06 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6134.021; Sat, 25 Feb 2023 00:28:06 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH 03/14] iommufd: Replace the hwpt->devices list with iommufd_group Date: Fri, 24 Feb 2023 20:27:48 -0400 Message-Id: <3-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR07CA0024.namprd07.prod.outlook.com (2603:10b6:208:1a0::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_|DS0PR12MB8019:EE_ X-MS-Office365-Filtering-Correlation-Id: fe524aac-bb53-4097-222f-08db16c72767 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PTZKk9DUvYi7E4pota5M2CVVnABKrTiRVa0Wquu8FL/RIn1O+JlxozZT3Nudk7glxG5QeGnoYUXSBmjc+ryCIPeB1K+40889alvYvTRQ19OTzkHUsT2l56wguOWm/tGaNE+nfMWl6wm10eRLeGplKI7LX5Ah+r5ABVAI7zMeb/3q89wL/DuU8iUaqnT6ifg9AZe5q9c1zEFDon9F7Ra4QE6IZmOWTp2snJ6eo0bdhoAZk4PP8fhwtxeXE/MM5HSQTy6Dzysbp5nr418v+uQPon07FqJxUccxSWzbbp6Q3vxqMS4JJ1olb7nQDV6Z26uz2y40x+ax8xcKrYXWI8tEt3aFkkokt00poDohpJFlHfN3Mm9802hIKw9tlCu29eoJU65qGRXYqFoP7lU7aHrMUOoIrjqkZibz63rUnkq7T6melmb04haKg/MW5PHG0E5Iq2xD9U/nD+D81JCxe9+OSWSG1uYyFEck9MEyAxe6aVKPgD0XbH6BYIi/EITS/uNvWHVh4A/OOV0RmFEO1CFY6wuwCgHwMmPWC4bpmxuakyfJT2OSSTolkubcmtv+BYmUz4CgU3lZE58eQHTFOcG7NcSgJptmJt7co89PZ4m5CyP2vv89m08cmr4vGlNOtLysm1GvEWVP1M4Z+RyPHCgqQamJhnC0YXaFSrlgPYYAL8b8QR63i70kHtDeHcRmLsCe 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:(13230025)(4636009)(39860400002)(376002)(396003)(136003)(346002)(366004)(451199018)(36756003)(2616005)(316002)(54906003)(86362001)(6486002)(6666004)(26005)(186003)(66946007)(2906002)(30864003)(8676002)(41300700001)(66476007)(6506007)(478600001)(6512007)(5660300002)(66556008)(4326008)(8936002)(83380400001)(38100700002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 1Ui6fvV9G1cbsyIlYlJyolr/HPi57I5H+GcjiEzjaqqrVoxxGj7Tj77IOH4uTg5MJ9q0WtSMrHM4HChxG4NbLx2p5dxiEgOLvkkOSgHIslKFpS+iweqHRJdzMeUInRQPFCpVaD/qkhk5V7Cb0mavS9HuIebDTsgEsEj8rN2fUWKkrrvY97EcepRYkDYcHvyT3SKZy9jwceo3SDROZ0mF2o9YGeP5wk/eSc/iTQx4G9tVgpsdqosS8jYQkUrsiIKDUKLk/ES/BefZvsHD26s4rJKW55W/iQiP0HNI2CjN9lbj5Drwi56mta+deKaXoHNUPUMpGLbvji4KJkXYG0oHbEHNhsfw7F5kvqgW1OJ1nG5X23PvpiRMj3a8vLBdBa/VGxhiusAH9g5VTqxdM+JtUSlt01OlZarp3zDBmgRQXA01+eZ2f1aZah2etXOI6iW//kkAVQx9YoSqxiGa2yJhvTIMiwQl22TsNcONeTqf7tjF8EqWYdc64peQdWc5Jgx5WfiV27PH/tJP6/XNyEiDVcjz0qXW7jbzEVMFYwGaVo5jY2u3BmkGIYR/zyFfAHlbvE0yU324gnkNuVIC5WgpKdWC4GZd8Uz44oNgfRj6TGW6Ey5Dbl2eHB7Re0EOl3MGLG/Xni3wMUCAY+grpDukKKSIHCmSTSELnPDnfG7MqFpuRdmjI1P5B1GAC350MeFHGd/4U/di6jl8IEcv/yrcPTsqJh9ejn2PBEcBWapcoM91Raj5B9EtkcTLoIvLP8kgVP4MCDAjakSojOYhSwXlFxZL6GbIzcQ9jLc6NKppyr6ue+sJGCPmyf0TcAiTjLh7kItStnyR+jrzTQLg+MJaPMbsKNZmwOHdYHArUdKgjTqTHwnlMKPswEoaOzCHks6ecuQddP4kPOLH85ws6FADvGOU/3jkqkbOP/25MX2Jz/5j3KFm2NOuivDwd4pd3go9Z6iKH/IqZ3q92loYLnhWMfRYfz9XNQfvF7jt4fnPQqn7bpg5ybmYFoz34Mg4KfmsZP/aKgetYrTY8PE9PSPo3aRtMnTbleLKq7tgFv3PWuyBxs/y77ZZe3muVN3CirO+gPtJ0PLHeAMpiDeWuDhURLWhX2bS0kC4yHV6XIV8IqSUu/OfNvUmWjpt4wZskRTxa5+/goz0VYnkc9Km2qvFT9I8oyar53+VV6BLXAIg9ozWxzUdDDTmpeU8EgcFfE+48X2i7HPNE4WkE8yPTtOvKnuQ11HphBlzBAn+s3tUl32fl8WwV6Q6ATdVkRynTvHOeEFHdtUDderpsUGQdnr69MD8lredRTwnwLJY1L/ZbLeNQVgnZXSsw1mDAbAuChZn1smrJANLF1Z3mLUgZmau62D30KsKHX9IYbOyMe8Wlo1aGM8Igd+LeQlr7LpdAc82BdapeL1Y0yJ2O8orB7j/Y7AqaaNkuqgHNn9TSzK66j4O2KUq38BjG3HaY2wp+Ro9sd/63b/XYlzfkmnmfX1AbkqBUQQsCWpEKYUgDypyNlAJyA08FTglA2PEjbH6/BXqREQrJcNZtdiFS/JQPJPj1lcF7EEegj7UreRnK3kSLZ0jA83vahVqbu3V9DigQ7rA X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: fe524aac-bb53-4097-222f-08db16c72767 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 00:28:02.2661 (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: 6VwYkilcydYTuKh3RRs4YU0x7DDwCxlCjYDpiOo7GswqL86+pcllwjwmrybzKnSV X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8019 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. The iommufd_group stores the currently assigned hwpt for the entire group and we can manage the per-device attach/detach with a simple counter. Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 75 ++++++++++++------------- drivers/iommu/iommufd/hw_pagetable.c | 23 +++----- drivers/iommu/iommufd/iommufd_private.h | 12 ++-- 3 files changed, 47 insertions(+), 63 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index d1e227f310e823..264bfa2212481f 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 || igroup->devices); + 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); } @@ -70,6 +73,7 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, } kref_init(&new_igroup->ref); + mutex_init(&new_igroup->lock); /* group reference moves into new_igroup */ new_igroup->group = group; @@ -266,28 +270,15 @@ 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); + lockdep_assert_held(&idev->igroup->lock); - if (WARN_ON(idev->hwpt)) + if (idev->igroup->hwpt != NULL && idev->igroup->hwpt != hwpt) return -EINVAL; /* @@ -302,7 +293,7 @@ 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)); + WARN_ON(!idev->igroup->devices); return -EINVAL; } } @@ -318,26 +309,38 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, 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 can attach every device individually as well. */ - if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) { + if (!idev->igroup->devices) { rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; + idev->igroup->hwpt = hwpt; + refcount_inc(&hwpt->obj.users); } + idev->igroup->devices++; return 0; err_unresv: iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); 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; + + lockdep_assert_held(&idev->igroup->lock); + + idev->igroup->devices--; + if (!idev->igroup->devices) { iommu_detach_group(hwpt->domain, idev->igroup->group); + idev->igroup->hwpt = NULL; + } iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); + return hwpt; } static int iommufd_device_do_attach(struct iommufd_device *idev, @@ -345,16 +348,9 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, { int rc; - mutex_lock(&hwpt->devices_lock); + mutex_lock(&idev->igroup->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); + mutex_unlock(&idev->igroup->lock); return rc; } @@ -364,7 +360,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; @@ -391,6 +387,7 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, */ if (rc == -EINVAL) continue; + *pt_id = hwpt->obj.id; goto out_unlock; } @@ -400,6 +397,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); @@ -444,7 +442,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; @@ -455,7 +453,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: @@ -473,13 +470,11 @@ EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, IOMMUFD); */ void iommufd_device_detach(struct iommufd_device *idev) { - struct iommufd_hw_pagetable *hwpt = idev->hwpt; + struct iommufd_hw_pagetable *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); + mutex_lock(&idev->igroup->lock); + hwpt = iommufd_hw_pagetable_detach(idev); + mutex_unlock(&idev->igroup->lock); if (hwpt->auto_domain) iommufd_object_destroy_user(idev->ictx, &hwpt->obj); diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 6cdb6749d359f3..566eba0cd9b917 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,13 +60,16 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, goto out_abort; } - mutex_lock(&hwpt->devices_lock); + mutex_lock(&idev->igroup->lock); /* * immediate_attach exists only to accommodate iommu drivers that cannot * directly allocate a domain. These drivers do not finish creating the * domain until attach is completed. Thus we must have this call * sequence. Once those drivers are fixed this should be removed. + * + * Note we hold the igroup->lock here which prevents any other thread + * from observing igroup->hwpt until we finish setting it up. */ if (immediate_attach) { rc = iommufd_hw_pagetable_attach(hwpt, idev); @@ -84,21 +82,14 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, 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); + mutex_unlock(&idev->igroup->lock); return hwpt; out_detach: if (immediate_attach) - iommufd_hw_pagetable_detach(hwpt, idev); + iommufd_hw_pagetable_detach(idev); out_unlock: - mutex_unlock(&hwpt->devices_lock); + mutex_unlock(&idev->igroup->lock); out_abort: iommufd_object_abort_and_destroy(ictx, &hwpt->obj); return ERR_PTR(rc); diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 2544f10dae9aef..5f3ad16da819e7 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; + unsigned int devices; }; /* @@ -278,9 +279,6 @@ 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; bool enforce_cache_coherency; From patchwork Sat Feb 25 00:27:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13151874 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 7AD44C64ED8 for ; Sat, 25 Feb 2023 00:28:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229566AbjBYA2G (ORCPT ); Fri, 24 Feb 2023 19:28:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229477AbjBYA2F (ORCPT ); Fri, 24 Feb 2023 19:28:05 -0500 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2073.outbound.protection.outlook.com [40.107.212.73]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EE3218B0B; Fri, 24 Feb 2023 16:28:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hx2fUHfir+8Z3LMXvF2uL8C7U4a7/kkKvkcUT+UwuySal9c1tm98NHOsV25NfiVe9m7h662hD+t01FdoN1kNeG1jw6XXSRO3NPrsAo5keuAhU4gGqAgSgYrU15tukDWhUBL052S7BWMoisGw5+T37iog9LG1lvAEKYBg2ipe4srspBCufnOA4WQz8A7GjEHwx5D/X7uj+Ld28WAhNTzGvTIf2oGm++ftJ6G1lgC17OSN5zaMtRCIhJMEkTS9Lg7WL+5Fn/w+o0+4LXJrh01s59foR7dyhokTrZwMe6X5LVu0Y/DuFOF5iW/ZmTLxuLvc4RRvD06GiCVGytCcIeJzbA== 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=AnzJQh/4OPTkwchnBXPM9CMhIalhimwOA0CZalhAdmo=; b=mSSaIK2eMfuBVQ3HXnLNq6azYFojGWysJJCHNWPwFguaWcs39yANmKbIewgk0fQmYK3IV9fcGVP/IG/gmUHIqGQrwnifbb+ulJFl+68EhWaMIPc6bhWNWCq6pM7v9zjy2oUQhc653fiLi8LPbbTmNFMT7L6XTkIv5GnMvLiUp0WSxbhG5QICg86TG0uj6v0Tqa5q/B3ih2v8zEbWijK+L7YKtDOX/Pce0JU+QG/NE3EiKw9RljDC3z1YuO/7UQFYn0eSZoBflYmuvNJKOaRyFDQbGLMPev5azDXWoogtfPDHU87qjBz/Vmz5+5/5p2F5M9NsLbs2OX42erR8U5iZ0g== 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=AnzJQh/4OPTkwchnBXPM9CMhIalhimwOA0CZalhAdmo=; b=qF2eYTK/qQNZObzgSgksULJBVoXPM3Nyit7xmm+lEh2fU/aXVqaKnKzqcRme3tClau+zk8wd5liRsseysWmqbWOsJvvaq8oRgQiOjmvoqxbtJxkNIKaV5bX4HFob4jISJaq7ZeUjpv9rlQk2hliPrr4DlsWys5Jlkj35gRRSdIyL7g90eLS5mhKJmIO6JdN6adlQHbYE4vnsPohbH22W9TZLq9k+CD4bUP5jP98Tn+Bc531JGED8n26/Qmaql9BoSq95+dyL4FA50ybFMZyn37++vbsMm5Y8xZbE6G7lLiubfZ3MHotD4epY1I8d/31NX9IvFiCqCYQsz7lIlw+F6w== 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 DS7PR12MB6096.namprd12.prod.outlook.com (2603:10b6:8:9b::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.21; Sat, 25 Feb 2023 00:28:01 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6134.021; Sat, 25 Feb 2023 00:28:01 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH 04/14] iommufd: Use the iommufd_group to avoid duplicate reserved groups and msi setup Date: Fri, 24 Feb 2023 20:27:49 -0400 Message-Id: <4-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR07CA0024.namprd07.prod.outlook.com (2603:10b6:208:1a0::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_|DS7PR12MB6096:EE_ X-MS-Office365-Filtering-Correlation-Id: 7c4e75d0-c87c-437c-579f-08db16c726d3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: deCX8ufa9ebyFQGs2KM7AisEPaKfKH2qSnYfDQOMnKqs2+vhzjZG1exrjnMtljxpl5K69cP+TslKvm4VTBUhbOuA9bZpenRu0AHUEO8j+McQfgevbQetIqUDb99u1iHK9YxDxByoZwirJhwaMDWvWwgSYVPjYhDy4FblqXsiaD9YE2JRxvR0Wb3N6KjDTG13yXYSYZODjQkfYyGqW3us9Oe+1dcwER2k12KZml68znNC3neDiGKuPAu8mHfxh5CM0IedYjx5eOAybqPbWPB4WdXwdEPPOCNmolwyq6Ro37hf/F+e7x0JRLle762zw2TLYR3H90A2S2LRpyKM8GF3oXdta/wbhqg965Nz5tpn/br9vAovpRuZUJReoPh7XDsSmCCUvceetNQJmxYpA78O49R72s4mvlJL12qHXCmvp/Mt1nnV/0UkFBHGgKP/mfaLXzbY4i+RGXt+uRkQH+5DH4FQMwCY+/WUwyOFBy1mzu0ZAR9GwjUdmfPyYZ6CHQGi3FzErc1U34LdE5rxyjh8uz5EFdnbFprWvxJOM9eAV8te8KMHJI/0veQvgiMIK9gYZns6qaYUSuXjrQRZ87XCRm3J4MRt33t7C7GzTnaEWaHbWoLnFni1iVQyMHeXGun6YtjxAfVPMXcBIZhvEgi7hMRzHsHDTHZR5TrvkfKUa5q2nAO1CC5AUoZWWLs2euUo 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:(13230025)(4636009)(136003)(346002)(366004)(396003)(376002)(39860400002)(451199018)(2616005)(41300700001)(83380400001)(5660300002)(54906003)(478600001)(8936002)(4326008)(38100700002)(26005)(186003)(86362001)(8676002)(66946007)(6506007)(6512007)(66556008)(316002)(66476007)(36756003)(6486002)(2906002)(6666004)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: hVJuPrRvO71Y8Bpn1Jp85LGlvDTE3cQUaNBH63x35q2xI01LZBrmAtryjictho0b1Nv3/+MCr2CvyCMm34sclXRSle6s7Bki/qIXQ/aDARXzbG2oMhg3mGP+RCWc0W/FWn+pfY4VgLo9UMrylnuhjvyIOO3dQOYWapKabZxjl0vNVwRy6AwAbnsKe4t+YGMuNyw1/2jSFdxzMxuzYPIEyWyLTbS9NP26oXj5aE50EKh8VDeevUDTRQGFlw7g7SxanazbBqKfMnqflhP0CAXtGbLhZXxrm2MZJpA+F4iw7m5wHsyhzTl9UBxZxOBshSFe//Z3YbElm1IC38PMhafzaPz5b5g7mODnlPwR7WOipxiTK3QKANC4EcfmKHqR9Q1uuMw1Ha+wBQbmbGsX+T3rbctuJEmfOx3/ogeVUd7udCzVjtUA0+f95sLXx3QEgNS9d7iKmyH8j+Ez0B/9vH/D7HTRKaoc8VEgmcL5bPQoatlnnYBoOyv6DgQ6jm/lgXPbfxCCwRWDCTnWORlEg1MJpG8KK+sWT3k2XjBIGkjybPzdNXwG3n4ozfruEDX95bwHbQPw3K4fdG9VNbeRwZrBhhnzWnGCKsKjlJxUJN5EfURhZtFTGpKhT5CIAUcVSXWQyNADMNt92flvjZCg1zrX8H3A7GDqA3OkJZf1ffDL3qmiR7TGx91gU2xspBy3rR8APbcNyQUSW0bUKC+zLo6+8HrcW/WUftVNImDrTYKwN6pPHV6ypNrwnGDYt/NiBoS3mxCx9q/Zrt4D6LWFmJJP86r69WvwevtbJftL8gDbtjHIDhBoksPZA39jjq5w+yDrA/muffLzNIi+yddAEFIYsDx4gEXWpi0AFFEnrEimZyBPlf4U+Na3y1hAXv5BPWgeBmGlHiRq6XtacF8ohf7itg00IgxSaj4Nf4qvU3FylOhCog1axYOQygt1j2C8qQc//eRpVcRR9KEHcYsfkUgc/qWqgni5cJyRxDYHV8kKDXpm11d/fY/u/azTTtkLY9QXqtKcoFoFCMeF+vMhNhZAjImjNwaT3DDP1wUcGXhTjKpiRS+yM8FRYi7LPfdUsr/aLtrS/L1z/9GKhzwbWR2I2jIwc142RMtbfkd8KuCYS4ssCRSoYgF1tOM0h9aiSWX5aYu5s+lvMHvFDzHSYpUHrywTEdMd3gnvn+09sboMB+VnLCzzxsQAVq64PQGdHO01cun7963ZTSKdAGO0oNZAEBzZXmMUV+mzPXpQxdKXCCfywbGEcqBW1N9zVmIv55FWwT+2AYxUC1/WMeGkFDa0mrHStF98eY84+BjfVAX6IBA32oLP3eLvu8ZnzT4WPHWuXkSV8sCrZshyObJuRy0pNdJxCbXcirC9exJZlBc21MsKqawd42wSawr0/hYblPXmHvzBdyL/hgiyquTjozoMyfAQel0kB0cp3a/OTEccuFeaRVkpEO9dFKGcylVasir6kCzHoT4GTHFAkGLeUWqAhmlwIpDWH/CUNfoCanB/i22q5Xo2FTty7LZ1xQA/aH2L7BaQUiorgh99LoyutE4gGgpLBA8rdzmyJ9QkMrDIfrF1Q0W6L4LLeel9fr+w0K+0 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7c4e75d0-c87c-437c-579f-08db16c726d3 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 00:28:01.0619 (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: aUgZFyHoOYTy/9+802etF3mUBIHoCeZMw+kaQZ33DQ+LlJch4CVELAi0cNHp2fjp X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6096 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org These items 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. Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 38 ++++++++++++++----------- drivers/iommu/iommufd/io_pagetable.c | 5 ++-- drivers/iommu/iommufd/iommufd_private.h | 1 - 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 264bfa2212481f..75e8d79678736f 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -298,9 +298,20 @@ 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); + /* + * The first device in the group to be attached will do all the work + * to setup the hwpt and ioas. Every other device re-uses it through + * the shared group attachment. Users are allowed/expected to attach + * every device in the group to the same hwpt, that just turns into + * a NOP. + */ + if (idev->igroup->devices) { + idev->igroup->devices++; + return 0; + } + + rc = iopt_table_enforce_group_resv_regions( + &hwpt->ioas->iopt, idev->igroup->group, &sw_msi_start); if (rc) return rc; @@ -308,22 +319,15 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, 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 can attach every device individually as well. - */ - if (!idev->igroup->devices) { - rc = iommu_attach_group(hwpt->domain, idev->igroup->group); - if (rc) - goto err_unresv; - idev->igroup->hwpt = hwpt; - refcount_inc(&hwpt->obj.users); - } + rc = iommu_attach_group(hwpt->domain, idev->igroup->group); + if (rc) + goto err_unresv; + idev->igroup->hwpt = hwpt; + refcount_inc(&hwpt->obj.users); idev->igroup->devices++; return 0; err_unresv: - iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); + iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->igroup->group); return rc; } @@ -339,7 +343,7 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev) iommu_detach_group(hwpt->domain, idev->igroup->group); idev->igroup->hwpt = NULL; } - iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); + iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->igroup->group); return hwpt; } diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c index e0ae72b9e67f86..096491bbb5acf5 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -1163,7 +1163,6 @@ void iopt_remove_access(struct io_pagetable *iopt, /* 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) { @@ -1191,7 +1190,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, group); if (rc) goto out_reserved; } @@ -1206,7 +1205,7 @@ 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, group); out_free_resv: list_for_each_entry_safe(resv, tmp, &group_resv_regions, list) kfree(resv); diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 5f3ad16da819e7..dbecdff013d082 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -77,7 +77,6 @@ int iopt_table_add_domain(struct io_pagetable *iopt, 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_set_allow_iova(struct io_pagetable *iopt, From patchwork Sat Feb 25 00:27:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13151884 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 15E60C7EE32 for ; Sat, 25 Feb 2023 00:28:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229683AbjBYA2Q (ORCPT ); Fri, 24 Feb 2023 19:28:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229668AbjBYA2N (ORCPT ); Fri, 24 Feb 2023 19:28:13 -0500 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2073.outbound.protection.outlook.com [40.107.212.73]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9CE011ADE2; Fri, 24 Feb 2023 16:28:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=f3+gYNHHfCWWqW7WCtK9tFYKMrlHxADTdw6SSAhU2zBwVMfQixdR+1vj0WFCGD+iJi/xTGji9DZFqLqvPX5j+GY8wd1LEIRZTNj97xPxbdrADZDQzS9QJpZcy1DjphwlLQrc8lAUBypvtbUEb9CCG6Cg4YwuJ1md1cVXxN5eJSwJ6oB2SfoFi0d1d0rUGGlSf6wLAWkNFWMFkyKANhZhqr0X3FsVWYxhCi3amBxIW+qQOqooxGNMqllox7UfmNR7jEURubrnIHk5MlQrGDN/N/AGUn5Z0Do7JZvax7XgOVJ/C+13oBvNDDLouY0lXl/sinI4JaCPZrdFJ/zp06F8yA== 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=pn25eTqY5kWysyNu58e+mWz/bPoasSrMP2Pf+5Ovy8I=; b=BTpCM4iT7yMg1Vg3alAl0LsaCnAiLZdPCY7FjMUfwndMcga7sD/dI3LjrKR6/FkVaTOI2I1FvcVsmleLn+v93kQcuEejL6g4BTShJn7t9qCRsrtMV37NhOTWw3bQOstC8JAqxz/G87tNYyGHv6vHchN6QEi7ELc9WGmdXfcMA+0G0nD/mjrddMu5Frj5pvyuz4CndBTO904BH+VLb7z7ttzjd3bbT2SqAthYn4GfrLQTcxtGH05RIFP+eOgLUtW1pWZY3tyX+oVdghqbO3wDJUOXgZg3Ddum6fbbV5qetaNSuZGmftbct2krlw6uNwSisrxaNw1MYp6cWDBiYqwxBA== 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=pn25eTqY5kWysyNu58e+mWz/bPoasSrMP2Pf+5Ovy8I=; b=Y5b7Pb+eUG+l9pml/LKkCTqOvsMa7ovXgebe+sfRGoHnYt/7Q18P2PSjtUfUUJopl7QjHQK/t6uuXGFvzpi1ayMhFUbZAqHd1rcsS/lCTRbCzGGLvSmySzd5VGiuFtf7OTMX+w61IbwqJi5HdUFmXKMpkYWP66IJCEoroZLysl+RBKeYxKKFZfe4+3FCmpipk4LPCC5MZT+3kBW9awuVT/pNMflgAN25dJQREFJPzGyPeP0RLytKOdz50dWv3kb941Ljuh0j5RLJU8UottKdp79ODSSeDzJq1wMg8bQmJZLD7a9BUktec1YRBKmJROJBilwDcFIkBRQEJQaOw1yCAQ== 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 DS7PR12MB6096.namprd12.prod.outlook.com (2603:10b6:8:9b::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.21; Sat, 25 Feb 2023 00:28:05 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6134.021; Sat, 25 Feb 2023 00:28:04 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH 05/14] iommufd: Make sw_msi_start a group global Date: Fri, 24 Feb 2023 20:27:50 -0400 Message-Id: <5-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR15CA0023.namprd15.prod.outlook.com (2603:10b6:208:1b4::36) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|DS7PR12MB6096:EE_ X-MS-Office365-Filtering-Correlation-Id: 2404d883-e61a-4632-251c-08db16c72748 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: mB5JGp5mzTfIcuO3q/zKMZTFsZTY2MJlsTFJzqpZDOEdfVK7s6bVEllIxv5u0XlMR32oiE9plBKCfyXGOS6ZKrExyUmaCkKuLHLV9K+AiB5jRtFiW4GWF5/aoNc27Od21jRLTRDnMpPrz3z76Io3pvU3LWt+r64Krfwj07cLAf1s8dKDovJ4p4vnMVRrnv96AbYWE+s1XeGFyJVFK2lgQLWyFUs+3difSOxZHkziQR5tUeb9oMM66GuSBGDbRUAtnCsaYaZeVMBi5b+DWYfEGENEOAH+IE2Lc2xsHDI6F3w+0EI7NhHc1YDWHZBiJnLWwy5FYXtualUtFurxXluoFTGOVUV6R9tCLGu0ntnSrB1ZqoExbtmmssTyL6f3ejzLokTdA6tKDagy0zwRoU16DzPrrjNysK2yOEtVJ3ykztw/ir6rYTH+8SYSurClJunUY13rXzzsOaP4J3iGivfBYf1Tu5mP1pSmJX9Byi5WrL9INa8PHjySPyXyTh94EjFl8hQwTI9B/jodopY88DYvZ3t+KD/9/K+akduS8g0vtazgvgwE1JV53SdG/OcwKkRUYd5JdMhYx331hKgI1RK7FY4gPyW6qFWTILnU9+xUbIpIH3vfyXri4cNnXqDJ11wVsHUFOz+TYLVsghyvV8FBCrzmCBOw6IkrmlfY3DYkvI+7OyZv/1sx0ZzjKyn7s8Eh 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:(13230025)(4636009)(136003)(346002)(366004)(396003)(376002)(39860400002)(451199018)(2616005)(41300700001)(83380400001)(5660300002)(54906003)(478600001)(8936002)(4326008)(38100700002)(26005)(186003)(86362001)(8676002)(66946007)(6506007)(6512007)(66556008)(316002)(66476007)(36756003)(6486002)(2906002)(6666004)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: /3pnGq7m2ufNlRT6vVVMczYWHrKKuWEjFBvmG+RJQmg+c2koyDkIcoq4SOjhJXQXgstmzOEE9WZ99u6qovgvk4W5CLsdeqN23nQ1tuxZuShYZoGyIx9BrMUPUwwMAotONjAcOTXibxupnMoCw2PANPo+aMKn6CasS/fKPqgVaRtasMY4u8DbczLqBF53m2OSghjFsZgKExD/77uxYjmL9fZJtXsVnlieK93fkH6MKRZQL6IW1M0++GXpwjFjYcWtjE8Jwj0ZTxe3mrxvkb1o+fsEeh9Pl64N3pgaWbOnUPb59ZmdNARbDRofCw1gRjJ/igTAO/bX120KRwCo/GO+0Vj88cqF6DggkQysefyePXtRRRe6ktrE0wH5PE+dn9fU/yoCXSt4IU1qxLiYciXMuA9V7WzYhVrx5IzAg4jerNtSQuys6G5xVq1odXlCFjF3Avu5bRk2WzqLiZzinaLZvcvKuSvD+EGOGNscXHGWiyzXNl4i1lmz975S8Lvwenmc8mhR2HG3GDRNuoGZhoqrboZMpF47eaNJBPE7FG9ITlcSRZLbQm+RP1I8teELRF82SKSZEICzl0eOse7QoErt2Yem8bPhRBtQPiLPBpkwR9ktof+bVG7976jC8kZMtgV6Fyr/c2xWZQZkbBzf3brr/fmkCUDvAd8l7fXaPOUYgfYfbshxZdfJftKzI02qXsLFUrQP+DPlM4EWFK9JXR/++9LkBBq98uexBLHakglvuEkueqmcx0ByBF0eHEdQ61errLs6d1ZSdsEH+4KRTIP4zJBp+nONw3OVXaoCb3AaG0kBl80u9YwZBEGv/VTs/QrG4465kp+UDahBsedCM5HWkbY3RDUs4inCMGUbUjSLrQB8SjhulSRUkQ83lNfNSV1cai7Jg9xV4prsAb1ndWiTfGk9eByIes+5zV5iMpkxyNVvYF4Dj5FatnXIPMwLYDjZuZEA+ci4jlOFmtsTYhRxtDuXRNGLV5Zpgf03KtHOISr6XJTt0AT9yTS/pt58mQ1GTaW91b6yx+GA8d62ci42WOLE4F7dnde3IDuLo1tW1Dn3iRZ9PHtG1NeXGBeT3NJWXBgAheXzuJSF0lqtYhM2xf9xvLcQ+qCXJ0QgnA1ULAwSMmgP/usVL4gFm7OOmNpifV3vaHDZnh6n9XoTp3mFldvB34Fp8o1HKcQ9JPsaVr6cbIv4Vv7Xdhl7KwVFgnX6FoOOoNLJtmkQZ1uMBpNLiLhmViKL+17dvfAnYqscR6+mcLR1fYD2mGthqclht3GoqpkuMFp65uZhCINofkds7Bg1oBfMyW9H5fKPobuRc36kLReIxCHMpWiC0HhUxwVr/4JCZqqKak+z8hrtnSKlesvFw2pyuIVlUGDUfrtH8AziSwpbCdqbx3UJJivUGNMXo5Oj1fsDJDjSrTKzmbtPka7uRVzQEcYA/IAWZKWtzNigpPjBRt8MHO/FnQnSee41FPTVqOQ7JDppfzM3/AwHI2+pSlfKQSkfloQ73Z43p2QKHuS51ZY2WoKWPsLpgF0R7PA8TWq1Ez4ZwvfJXP3t/bKgwcf2HmOjcqxo39OilRLpIKFk4OTqp7tjTL0/AXiL X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2404d883-e61a-4632-251c-08db16c72748 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 00:28:01.7961 (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: FqVXKGrRvwRbtY0GoMMXkQ4ZGJGbTFIUevqTEJR9n5wLSh+zm6AAPXZFOTcP+7nm X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6096 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_group_resv_regions() move the sw_msi_start to the iommufd_group so it is always available once any HWPT has been attached. Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 13 +++++++------ drivers/iommu/iommufd/iommufd_private.h | 1 + 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 75e8d79678736f..ea34dc5b0fd1e4 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -74,6 +74,7 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, kref_init(&new_igroup->ref); mutex_init(&new_igroup->lock); + new_igroup->sw_msi_start = PHYS_ADDR_MAX; /* group reference moves into new_igroup */ new_igroup->group = group; @@ -238,9 +239,9 @@ 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) + struct iommufd_hw_pagetable *hwpt) { + phys_addr_t sw_msi_start = idev->igroup->sw_msi_start; int rc; /* @@ -273,7 +274,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; lockdep_assert_held(&idev->igroup->lock); @@ -310,12 +310,13 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, return 0; } - rc = iopt_table_enforce_group_resv_regions( - &hwpt->ioas->iopt, idev->igroup->group, &sw_msi_start); + rc = iopt_table_enforce_group_resv_regions(&hwpt->ioas->iopt, + idev->igroup->group, + &idev->igroup->sw_msi_start); if (rc) return rc; - rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); + rc = iommufd_device_setup_msi(idev, hwpt); if (rc) goto err_unresv; diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index dbecdff013d082..0497d6190fd1b3 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; unsigned int devices; + phys_addr_t sw_msi_start; }; /* From patchwork Sat Feb 25 00:27:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13151875 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 457ACC7EE2F for ; Sat, 25 Feb 2023 00:28:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229477AbjBYA2H (ORCPT ); Fri, 24 Feb 2023 19:28:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229489AbjBYA2G (ORCPT ); Fri, 24 Feb 2023 19:28:06 -0500 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2073.outbound.protection.outlook.com [40.107.212.73]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D2AC18B0E; Fri, 24 Feb 2023 16:28:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G1JbaJ7tE2h8M67dbfMylpeYOxlCQwwzbi5wGkvxjE7rM7UNiTXJQsGG7l+DH1fM7s5MowOXS910yTMwvfthlUyRwE1EHbDy1OVpbYSPc1pM/FoNrDqN6POp8lkISUP4lIHhIA1iirtiSKQbHbQmkaOZx3sF7V65LqgmaG2rm9RI/r8pGzjBBiaqcvCPO2i0FEODspmYZqOY88XrCjRX/QIVbb/UYNsX+HOXiRw1Dke7HDo4IHUDqE8AX25b/BUucGiWL4eyn2AKeeAZCfwQGyWH4+4SfLCyHZsL1PDA3nMywcLVcJ1dED7UicSDarWgrntoWH/gZ56aPw5GaWiV1w== 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=lILqUPGTW7ech+aXy21BlouSIKW68LENQUYSLI+pYnM=; b=UGhEZ/Ztnmij8w36RMbf3Jkkf4UCjzqTZYeTIhRqjOT4UiEGyqnKFl3FVPHFdqe5OhFf+bKmCuE4PuyC4PLRt1UorG/7/UseGpyUTnt076A66muuIbsLTock6nRgKkYNq/iN0RMts+a+nge5fuPFtRSq2GjpTL4FkBe+ZDaPvOIR70ejlg4ZA/DfqVVBfJJZ9YEnLlfKvQOqw4UDL8DVTIXHUAfRWp1xUczrUg4b4ABS5QMym83jy6e36dzdrxFS+ghv/eCJc0/4GHiQgdaKGLy9L9LeYKh3ESNTJko/UC2szGMUlvzA8B5UC5s4l4Bilbt7seVpElSnzYgiQlLCtQ== 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=lILqUPGTW7ech+aXy21BlouSIKW68LENQUYSLI+pYnM=; b=JWK+IjShJ+fmTfrfQIuAw/sHZoFfA7SSd7vFIWn8zT9gUWb59MmUmIwwB1SIKyMyvB/ZVepIL1kAWxgIR0NSPwD0FIMFZv3YhNn5WFTGsxvqLePqPAlmHNKyr/KlXwfZt/qkajdpjQPyHF9rkWbiaqJyQ9sMyfBqZ4+4c6q15uzzmw0Cr+m64GP+iW3vsfOcLZSgcZYQyVWXeb6OjAaKxo1ps3GkoXza5bqayt04NG4//XJRDjCKUMaYp6KE6uALxpk2yIYLadsFREmv7GAWCaquch8z9/hMs6V7WSlEQiGUbmepOUdDYK3wUX+/aEWHvBTY3G2ShCIa5mP+3TZwKA== 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 DS7PR12MB6096.namprd12.prod.outlook.com (2603:10b6:8:9b::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.21; Sat, 25 Feb 2023 00:28:02 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6134.021; Sat, 25 Feb 2023 00:28:02 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH 06/14] iommufd: Move putting a hwpt to a helper function Date: Fri, 24 Feb 2023 20:27:51 -0400 Message-Id: <6-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR15CA0017.namprd15.prod.outlook.com (2603:10b6:208:1b4::30) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|DS7PR12MB6096:EE_ X-MS-Office365-Filtering-Correlation-Id: 60b5ef9c-fa6f-4840-0749-08db16c726e4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4JHufGPr3FjLIZE/d/j9knwJUo0twcm59jI7Po/uo8x5lMp34a+JogbSwHoV21mGKMqONMY74D2lPmHwaKi0P2p/DwwDQ6v/lSGGSEkL840joitzLffyHBleWqB2NaEqJlpNleMeZNTlhzbewhHFQy25U0lscqw9CWbxuJtFD4eGxfF4zp8XfVeYMqCbUv38jEfEcEfcDRztcYdKgQKV0uS6TMvGlCSRn9YhC2vZRb/yGHPGRpfAOVUdWR5V1+YeV/J7oHYX5Db65vPp2/7nnlED2cY9tlKdD+4nFinwip+00S7UfB+0EqbyfH8QX53RUsqIv8l23wJVLKd4riAYuzRpMyfwDbakmsVFdIFLcHro5MRV8yUvg3fP2kTvzUQtGNxDPu1GQRBhBIzyEyL8l849JlQXbQhZZ0+kuy5V9xOPaGC69laPSqHPuyc6eYL/eSFm5828DMagl8OE6tEjekbW4hDdjyo9EpBOnzhwf5NQw7y/xC1GhzQqrujzB7rqbmV+9EUfYsunrUvmNT31dvkpyfwZZHtjscA3IP8nYnLPWBBNexiycmo7wQWOgk+W/YuAv6G1nd8+Xj27frDxbJXRoaf69fEaO0BTrNNvevV9w7I+r+A9iDsfFA/+5nG93+QQnCMPwGNPqOB+FQrX0ECsKjXoKIbRBexI6g5/Ec/CcA/b6UfKIW7Qr3xKf04s 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:(13230025)(4636009)(136003)(346002)(366004)(396003)(376002)(39860400002)(451199018)(2616005)(41300700001)(83380400001)(5660300002)(54906003)(478600001)(8936002)(4326008)(38100700002)(26005)(186003)(86362001)(8676002)(66946007)(6506007)(6512007)(66556008)(316002)(66476007)(36756003)(6486002)(2906002)(6666004)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9n0lw+Nwce/RueKlIif5UhszFkzEg2E+zTmtbjk9UGnEzxuvqxud9n8Moy6ibkgzaRHLzRD3ZXwJoSku3iz2NT/GJjJo6eqedp+arvqrQWzSC5Z4wn0kg8RvcKmR8VvFA99Znewu2JIwxyXGLMovdWXBqBJl4Z3kyop4Aens3Q1Jmy5bMSmeoytdyDbAqXgs8NN+XpXaw1R7SqFUUWeE+V7rDjsOi/I8Vn31/i5lAdvkD6ii1CXbKUjXwFZyorXqUwXgNK1Q9f4zz6ikkAOdWxQZVtwjMi9s7CUjVBacY+TS6ljx/YejKjBXwG3yL2ah34vKkalHM0wfJFNW+2tkTc3hySMu6NfVJd2Gw5f1vGrOBsg2oggr0h1pKwk5TETkkr1cgrs905skz+YcwkcLggb0q+9IwnKEtTeiJGAk3MReG2RTdV/SI2Urp8EZj6jSyENiaB4NHMpF7j/34ozniDvJ/5xHM/IIC/ntYcgcEu51aOpMAj1jd1obTWSnqWyywAxPxyZXDAcUDPs4J+7YxIow9PVMklO+6lQJ/Rt+G7HUSR4TtxAA1ZswxMcr5lQxkwqEnh2rwVUWaksKjBsKg0TID43mO4SMBXRgleD1rI5HTsXG6TBCrqPmTFrx8Ua4tBboeyx19YJ6f4ewOLFyKlDhjiGz874uFJ8uJHfTsd4Mxt/gy1622Z6Bzx7yZ7rs72cDeXDlyisGLYUZMpPgMsIofb80g+C2Lloql0/4Vly+bePS8wnipjYDjB2+CE3tuDKQhbXw+K5lrNsHGpXcrJwXoRhhTGqgWU49sovCIcbQJ5YdW87wcNRgCLAlTkluw5aBg6RkoVxXp5+Y7s0d2tFgZD6hGoHjrAGs/gFhQecdsh1EoAOfO/DN+OOrLKGfHyHH0jpmRuubT8VhTrq8p0QtThry5wDpRSshPRneu9Fn3mzpOG0SM2yl36F5k1zBzN27wlSvNi+5EP4G107yuh1UshKowGVo+1phMV2Vyv9u7kt701bwu4o0fD4S6oNZDe6oHaJ7wjGaBsx36mJyqWERAzcJNcZMs8YxvzyLTvTakpsc39aSwe8GHNc4hvgOPAnkFgB6mRiuAVKjpDGEpxD/57yGdSiR/nyDV1PU6bIQaGQKQX1WGWfQ+Frz8A3suFubwFH5Bl2hAvn9/kZs+UuYhU3VJGUzvI3gmtjRqT3g6Wj4tvDO0L3NoZ6u5PSxW2s3cdN/rhgB58sqnSZI7azTukqUFiGzreSTcSDCTY+BCxgRiSDXLo4ZRuVuJgJAcugYVIgWd8QL56h1yI9OX4WedFfMEeGJxHWXJW4YDKNum/1J84CXELtLAslYyP7HM32qIMleAF8wpSnuEiTLW2m+NMaS/FigqDzh/iTgJVLTIVlt0g0dEQUqSVs/JrRxtMD5SRHOVykL0oFLDkjtmTsZ7mwDQv4pDtpGYaUqlvIzIjtfPdzAmDWIv0/dp+LAd1ZBUEZFyrLTXVYzD9V87qP91GVuJ2nkBmRvRffwfKGLlnZJbmf4I3tymJlW7AWDHEXTw2Taxuy3LicQ2Tp+Miy39J15ZceRIEdYZCYX4yTYi8pkLzRgDymfJaZZCpWF X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 60b5ef9c-fa6f-4840-0749-08db16c726e4 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 00:28:01.1400 (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: ZfCr4coU5a3KO3E7AgOlh1AGHU+GXZo48l39eVC9ctQQ/IDg6wpPf5YAKhlYnFTv X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6096 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Next patch will need to call this from two places. 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 ea34dc5b0fd1e4..590f84afa06503 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -481,11 +481,7 @@ void iommufd_device_detach(struct iommufd_device *idev) hwpt = iommufd_hw_pagetable_detach(idev); mutex_unlock(&idev->igroup->lock); - 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 0497d6190fd1b3..c0c80181d05ed1 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 Sat Feb 25 00:27:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13151888 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 BA9B5C6FA8E for ; Sat, 25 Feb 2023 00:28:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229675AbjBYA2T (ORCPT ); Fri, 24 Feb 2023 19:28:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229500AbjBYA2R (ORCPT ); Fri, 24 Feb 2023 19:28:17 -0500 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2061.outbound.protection.outlook.com [40.107.92.61]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35828199E9; Fri, 24 Feb 2023 16:28:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=l/JuupbgTLOv2Pp4laCwDBNATPGNvC6Zu86FGr7fg0P49qf9bZYfR28gU/KrY+AOECsd13z641gn5smsp4fqoAP6CHBTsEJhLpIWa+iT2hQo5rYfr3nRM/hFgJf0i+9y3udpr2TgApU4ANTlUWBVoqOPAhAgHTejZJAQWFr4eHqUvIWFsGFn5a1w5sIxhQgTsasPVPKZ4ETvQAQtFcg4fzJsTrlrxqnfe28wyJEkViEN58942v+CMFQ1Iw1ezh4cZdzh8mDx0/zZir+HxHnisGgbUNSHqlMvIGcpFmLiw/ioZ9cULEIAmrLGhgzPBlsUEDStpHg57usa7IDca/suCg== 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=0ge2EP0pX5I1JE7JGXadDOSSuk5DSq7t2oayUgQEu8w=; b=hvuIsoqLzYGSVtodIxKp1Pyp34weDr45PAqgCu4j5VvV/0e160TFkTgO0p8IMeLwfhGiOT5kl4PWnQ2N15LFhNEOjEubNJmhO11F4ruIvNMUEPIIGlq/+8T+eH2OKKofTF8vG/bsjV7j8fLunIFW/Bgj0HSo8FLBL7g3WuIpnDy9ZDynVtPGb6Z0u2ZirxftuQ6G5bXs2eOAEp0pLFvpEh2PpjIkr/VTvn4XwDtQ1WOk1+JZMaanaRt6V8zENYpIGoXlfbDJ77R8fBzB5iGu3ZePkKq2fPN4AVLmWr29zD1eizfaO1nIch4UpTy3cUwcIblUB4U8VgZlOPNinxVKKQ== 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=0ge2EP0pX5I1JE7JGXadDOSSuk5DSq7t2oayUgQEu8w=; b=oXhsRKgbesrNi1cyc8ypfzH8qBzyrDiYU8P7L5brpe0wVfAYNPD0XKfUojyUE9e+SKARC0HWtprMCOhrw56XOSLfk6QAZYPX8eh2J19bG+VePe3IdE4qoIpdIZvlbQwkRPnZaKfwa289/kyzY8cWDGfrMqojpvaXL8JbIy57RL/ODWmTj3G90t18mmWW0UmZ4lSNjdTHjC/7xWcgJ2Cit9lfAxr9A4QAinknsqZaxHxkfvBGdfdL76miGmxxLgGkoqS1oOJzo4vsMKBcfWaTw82JJ2smmg6UnjzBGLqnn7u80RCo/KpCYQ2PoxDfRPrjSeIvaGDYH5Jua8ugo93gQg== 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 DS0PR12MB8019.namprd12.prod.outlook.com (2603:10b6:8:14e::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.24; Sat, 25 Feb 2023 00:28:08 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6134.021; Sat, 25 Feb 2023 00:28:08 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH 07/14] iommufd: Add enforced_cache_coherency to iommufd_hw_pagetable_alloc() Date: Fri, 24 Feb 2023 20:27:52 -0400 Message-Id: <7-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR03CA0141.namprd03.prod.outlook.com (2603:10b6:208:32e::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_|DS0PR12MB8019:EE_ X-MS-Office365-Filtering-Correlation-Id: ebff4dbd-5ce9-463d-66ad-08db16c72874 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Uy7g3a3O+vsPF2lnkBmzUqRNMNtMwu5yw1WkoK6R5UWo0WpcZwud4U6XHpiWFBSfJ/ZL/kXVsk88pp5kWydwT/7ykKo25TvpuWZhrYQIOW6BiNDzRFRR4Ek0CjxF5ut0AieqlLgXiz3WXxpCD3p+YAVUzp3UUvsjGbfgyae5RkcKZiwRZM3BNtAIDMWvmWWs0IVQsPN1cKxTTBbBrJedeHytAOHcBcJ6doSp7cIFDSKLeCXYJBHlrexkfEkaSzY5g4nmhrEKbAnfyQMEdsKDbTaD+NZMoqXN9Izzw6dZX2etVa5uNAZ+ZFTameKMm5HNGo4gNHsD2HrtC/d3AQjI88UgmsSJGpmGt9bO3AcZloPR/tsGNQ4WY/C9kFdYRfrO2w330n3ZirF5Rz1FQ9Sil1MEsN9wBVJN9DPclbWlYKehpCDsNpFkWBZ+B+8D4+w36Svho932x2oBOR6k/HmaWNbAQXX2cR8e3ikXyuGWSbDTYReXF7ZBuemudlSSRgNWeT3BCooyMpS3Dolk/bb72V0Pszvdz05rjR8wnBoTvSeDF00KAGfiy5vLZGZvgHdtEp9OEfAfSTpY/ZDAIqv6PnTUd/lT5mhw6Wl6cDUumvBVR54tcExXNuDf+LEqYHgVlONeyeTYabVKANphKed0pSb+tHxUBBs4y/mQmL9BiCf+ctW77xVFUvca9uxpfVOI 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:(13230025)(4636009)(39860400002)(376002)(396003)(136003)(346002)(366004)(451199018)(36756003)(2616005)(316002)(54906003)(86362001)(6486002)(6666004)(26005)(186003)(66946007)(2906002)(8676002)(41300700001)(66476007)(6506007)(478600001)(6512007)(5660300002)(66556008)(4326008)(8936002)(83380400001)(38100700002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: fudsjkJyutkn7hWm1ptykExqDHLFAfsxbrD9Q0W/CuCDhfWz3CZATPGKmfUOMC9ZXiYgabjTJqIafiuUz+DeEAlcZ96GIAOrkEELzEudxkTvQKcCAhKFTNUf6UwbvEVqAt5/FnidYjFeJetBGdm0AXcE0W86dU8m3cwKRNIlnwDjvbgzDp8foyRlm0dDQNe6D0gR0kD5CdPtfjMxJ26a9Hh5ae58+ak5VcFi7Vp6VlIP/EEIFtAC67f4TVkZtTSPX+/uOUZwnI7L/TDsdjU3R5AVGPuqZGDgiSsbpZ+paJrPRtqvkMmPJ30UW2UncXjKNkIVaiUrBmcPjXZDxSG0bwn2bEg6yCoU0RXNNty4bc1bA8RLcm2prUJ/YHRJVsDJPdmGvDfvdv0btsavjPwX84lSrB1tvw+kSxhSdBkHhwQ8SUeyLjn4kC198hDRLPJk57j/+YJONh/rHgV+F13vl2mgLDKpVhQZOHtV5RRAcsqQ8CPFVEhAG8u9hEEtwnqikqfG/Y/gpCItDgT3cAmtwtZKI1YcMtjiDTjQ1QTl8eB6hM4A3Y88TUUkSK4YDzXzUfc2qbm4WgwuUnRKlRXB3QyiXPKuRSbk/UCNRADPugJXs2QLDZh3UCmvjcqYtKgkjz6PoxeUwr7ZIrpamE/eSM3R4vdmHhI+f67yvKTDQDSXkNOfqKkxkuSDvPsR6nTNB4TQxY+anhhdH/tF/9sAp2wIcY5TJo9nEJbNqEVRR/aK/2/BKxPA82qVf0wEB7S/e0uf4E87ZSq8ql7qlbaQtE55ivnpg/RaFbD5ahxVhULMsNqnloSVh8Dvnd8eHpmJsKpqpEL9/EuHhKjbnkInl82BbL2uPAzLCVgN0kZEY6VrX7TeguBgN/fpd2QcNtsuGDdnqLj91G+PNjyxRR3jZ1K3n+G0+U8maOu7QneH7vHkZYw5QDp+XUVWymhd9YFNDV0uJ8YGz4oWceKw7BWP6Ck1LBS135VY1a9I2yCr6dReeAHG3TdJ4gPjDczaGxuSfa75n6A2mrKbMTRyXWo9ig5BwU0uybVMlOr9aWd8W0GpRZebqDMFn91TxxT517IaELbuX8c1Kq43JACbN012n1kfJWzfrBIRnwTLSS1kf57O0IkexFLIDDXDnTW6NBeZMavd9qok+onj6scJ9j55zOZHsFFXb4W8Ck7yzfTYGiR5Gdjyvqg2gOb3ia7uNpn2jyafXa89X5RFcP7wAF/+ooIYrXLKYXMlfpV8DFtdY94b1c++XtboPx/fjh0zp4R65g4gffU2GOUyn2Y0g1lb+t6T1SZ3dAeF3mZ/D0Wp68TKBS8i8rPR5MFU2O+YXwbFsQFd6lwvVLV7N41LVuqQ2dM1cKBXAsgkS8QvgDwSQBZsDHhdG2b8P6fbMdeHm5y7tHddlz7kjv7NP3lqZNL30rgFEKvFE4KsdZ8Ztgl2kWV4qkRKcqjNRbCBarhPQYqjc6vODpqn856teBZOg+AjwSatXUjKYO9pF8ALn+pNTA0r9d/jvi7Nbn8nr6e5QLGY7MTmTMv8A5Xnzz+GyU1CLtjGtwR12VQxmk3tSmONf1Qx2vyoZNYo9dyIShAoIgCm X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ebff4dbd-5ce9-463d-66ad-08db16c72874 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 00:28:03.7639 (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: ZwTek3/kg66TVCtcNOsBhbkmlW+GE77TflyKjWm+XxDKVsCPB09XFOcikkFj2mm8 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8019 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. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 20 +++++------------- drivers/iommu/iommufd/hw_pagetable.c | 27 +++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 1 + 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 590f84afa06503..7f95876d142d7f 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -281,21 +281,11 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, if (idev->igroup->hwpt != NULL && idev->igroup->hwpt != hwpt) return -EINVAL; - /* - * 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(!idev->igroup->devices); - return -EINVAL; - } + /* Try to upgrade the domain we have */ + if (idev->enforce_cache_coherency) { + rc = iommufd_hw_pagetable_enforce_cc(hwpt); + if (rc) + return rc; } /* diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 566eba0cd9b917..2584f9038b29a2 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; + } + mutex_lock(&idev->igroup->lock); /* diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index c0c80181d05ed1..8921761ff98980 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 Sat Feb 25 00:27:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13151880 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 06D64C6FA8E for ; Sat, 25 Feb 2023 00:28:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229479AbjBYA2M (ORCPT ); Fri, 24 Feb 2023 19:28:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229535AbjBYA2K (ORCPT ); Fri, 24 Feb 2023 19:28:10 -0500 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2082.outbound.protection.outlook.com [40.107.94.82]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 367CE19F3C; Fri, 24 Feb 2023 16:28:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RdNUfwOOjXZdLlo2ibIJ06aiF9YwL4xJ/+abNXwK5R7hvnS3KIMQKgKznUHO2HlXqrmNwWNisMf7NAchzI6C/uZrjvJxYxG3cPvEycdu5QRfZgJou/yjQlb7e+1QMcuiYfyzR4JiWGS04c8/WQhb22JMmn97XkLQt0OEc5tcQcv9g5IoPylILO+gB+SUK84PchFAZl3/PmJopRZafVPFgYyZARk/8LphN1qPBdyr3/ThSWGitrbZTiH46PwqzBPOyimEGB/a2cG9IojJYQIyV4I67WO81pmEgEzb1TBAnB81bjzeORf07T2mb/ximJ/OxjLtabNJXDmIRmxjwims0g== 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=Ss9a4Pd/0GLcdwASCAR6Ahbzror96iWMtEj2Mk5fVuQ=; b=KkogLPTJrha2ym6cy5xlBFvAGZW3+jjlec64+JzdkzKfmrDDvA4UNuLVPJu25jPcnu8pbLrS+Ih2KkUsUKFsMqAwWY/GPuUP5VkQUdODqcGQmmof+wZG8Dgz7d/zFui0dbiet4OFSklofLJT1uXe+7TMEdvqsE0ymvWEulY7Tr7u18gCg4aVN5AtbiWmnLkbVSxQwu/hrxAGz8nljiTqKFpJrtRvERWbkU/jZh5Hc3nd+BJh4b8DhjJKASKtyxvqbPfM9Rhl6glv5ZbTvM9TeODDhS4I9+DpX6lmqZgPZLwY2oMDOeGrn2frhAUu9Ik1ePIwvJMhk2DgpyS895s8Tw== 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=Ss9a4Pd/0GLcdwASCAR6Ahbzror96iWMtEj2Mk5fVuQ=; b=NZbGtEmkTjGXb9ZshtcogMJm5/twAp3TxiVP6Tuqb3DDddAacHbDfijqhRG5yil6tz5xyMzIIZ5m0Ef4p8vpctPXLffqONZpCPbtynkK3XJwUS9VRqlfn+PfI/WLw5zFtZ5/7bv1ZitqEQ09qwonOmiUbS3sLa+iN8XtQqC7eVukuYVQ5iwQkgwjOsU62QthLLABgFsqCbOtUrCPzEm9GflhETb9UyIQ3BilUc8VJI3jLrvcdKJuvwyTgvPo3q8fyWfZG5p2HBnWziFHwC82WzIWypcKWusa8GUgnZG6O1KKKr+HEBsD7Y8Iii4DdsN45mNxlIsKUYvmkkDXRf803g== 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 BL0PR12MB5507.namprd12.prod.outlook.com (2603:10b6:208:1c4::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.24; Sat, 25 Feb 2023 00:28:06 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6134.021; Sat, 25 Feb 2023 00:28:06 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH 08/14] iommu: Introduce a new iommu_group_replace_domain() API Date: Fri, 24 Feb 2023 20:27:53 -0400 Message-Id: <8-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR20CA0033.namprd20.prod.outlook.com (2603:10b6:208:e8::46) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL0PR12MB5507:EE_ X-MS-Office365-Filtering-Correlation-Id: 1cebfb65-44f8-4623-2a29-08db16c7279e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 7SbtLqGzWahXPdcLRUOi4Jonwbx+BVJ6iI80HGuenGBRrhwos7AKeIuaI/8ZBunMPf+HrHmyP20aht73G4iclZVHWpkDG+LWuCfucJvDUJpnq+/WTikzimd9b4xWXPH5N/8Mm9icXO9qyKhTkAfaftoNQIIwFcyWlC8ToPTJt43PXtuf9Yt/GM1Ka88uS5PrcfaQOYh0JozcKFCvNqOF5DHcwtNY6g/sDZr5hDdz+xQLn6CGW3hQ4JGIRXBHFSBlgI3Dc/M+ojSQvn7n77gjauMsj1trnhQd2/Jz65uvgj/CdUDP4kqy5scbN6URzJBiJhA+rj6g8QXc3lVSoEi9qUhSNaDZOeowgDeApsgMF5U3CAMClzdmCDiEiEprBxZA2kauOq9DQ0QP8rZBHds0wYSbxlW0h37DyAsHqVCSxWKMY6ISFADB5TwQOxy0esXX7TRvM+0tPbD4Fc/2blhVD8FZK73aRRfWRKfheC8sn+z3Oi1oYLG+kg/gt35Yx0D8DnVzUtk0Fb5J73bpDGAOuZC8zCllJak9Qz2HmUWAfw7Po/ZsSybK7zStCzfL+9tUADymWnCyF0dx8nVWnWmUgWz6RD7PUHtA4WPe/rJU4LBI9ITqTbpxAFVwlAx/Cevj+7Gc1uK4bdpFkN9pSxsi5rwsyWhZQPxoxk9L4Q8eC8zal1IUt8K5x0WRfnX5fQQF 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:(13230025)(4636009)(346002)(136003)(396003)(39860400002)(376002)(366004)(451199018)(26005)(6506007)(186003)(6512007)(6666004)(41300700001)(83380400001)(86362001)(36756003)(38100700002)(2616005)(66476007)(66946007)(66556008)(8676002)(4326008)(2906002)(8936002)(5660300002)(6486002)(54906003)(478600001)(316002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: aHBNu6Hy65jq/g8ZQPX2uj/54aJ3J2eW4wTi0ePfG2uoSRwK1xhLbPiw+kL6xqRuY5GDkhhUZUgis8T6E8VpFvNJ8pKRkba5Uy+D7mHwz5IjHLulTmFNKhxCwA6ABGnS9+n4tibdKZym8Cgya2omJ9DaAhJww5mUZD9ePNcKeWv8S+FUUQYJ/5XIQhCm/rv+nS84nAN8bA1Kx20H06sry/FtVWxz+rxQckujzZvDrBsMhOqvSTeWigDq4csss879bCqSwC+H8CHtJ33ad9sGml9sOLTFR0aRp6bPBL2W8htWqgOnNbaFac38b+Z6yxNeurAItgmLq98zAMdA7KFjje/EMlT0674C3QY1irl+dPwMg9c3Y5KRz9OKgjL7CHoHpa8C0ScPegBQv7EiWGx/p/t0aaJfbk91gf+o7ZV8X5CUZqX6Xf1LQzmP0xkivo17zqRV91/gDISL7jz8in3sp5vWrr4XVlRY/mZSV7DK6RCluWfh44X952yv2vKW3T+nUYeR5mRXm0d3GvUQ2Fii1yvJ+oK6NOaMgmHjw/kbFajin0rRm0S6HTWO3hntGKcV/vDN1rAsk8A/T0bWOiCBlQGWKxlrHX8EjCihnWQpz5u/kgil1ztr2V2vrBn4K31GWgN+tokFb4kArcE0IgGgM3sBhw/jfzETN7wVNo8VeK0XnebJ7ie5ib29B3usqqXvjw9Gkw6FZaV8MMn2+RKMR6VgclpHcXH8q5aJFk0GuEXQsXPFcVkN6WitscS8RlNxNF5Hef27WaT5E9dO834w9e5BShAKxQh4YoXIuYv19DLPES/UsrO3xCptR5SqVjfA+9DQ64sfJn/Ttmc1nJ7JIOfqQfpzNT3aDzXCKQOWQIOgrdTzZRh7kOpQHzzKrXBUgzUhyf1kdaASDIUDIRBmgI9Rp7eb0k5wAHSkKrP1Bw2DMHccTSVnIEpLeiMAG9UWMDF95brS+5wDetzi8NAIv92tR0Zq98QkVBErUOKsA0WpuUAs8N77F7oyKdGZOB7djN77VTLOAlG/xnKZcIVKdvHZYMZwlXz4q1vRTfpIEaSBV27tHiRFeyIeFIne/+qm7oa31a5v6O1Shb8eYwVxeiGYDKlGy9APG4zjlYzn9L1aCTArC+dqGctNTXKylCf6TEl4BzhN+RJrD9wnya1ByXexzCA2xM25QP3RyQL1IzwWlv9AOy4NR8TGpyrIAaCWnHelyl4qRU3t5rNrKPHUnWd3jRbipbbnEfISffK4KAeUnnIysNU516kOSx9+HnzVOx/dnTd3kGR522V4KvpF0gs/2oJPTsM0Fu21wAhPET13W1YIKBuqjxsDVzEdOYqx5wR5NVxdVjEfYU+0a/jC4w1f2u59sSonBgjxXt7cdEjd6cU6MLpnY0D3H4j7jiCuAuQmFAgfV0plQict5u/+snoqb5Pl087agwIVk/iJ6QH33R8BCCLHXQVdC8iNnQ3fblGCKqppRVUM/asZmqf+DYMQ6mxvrwJfLKQSaOENrQIG1N2HOq+5GVp3Vnp67Z02tZv6uAHqPN9/Lyyc6V7LXvL0q56hxPNYm3A17k+3pktIWg9dIWlc0+4WhThvnSiZ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1cebfb65-44f8-4623-2a29-08db16c7279e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 00:28:02.3911 (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: L8VkXBBLhZ4IuWPJ8svsF0QpnsyUwn/NMb76L/RKvWHuN2+bI5Z8ViUA/aoVwHh9 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL0PR12MB5507 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 adderss 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 Signed-off-by: Nicolin Chen Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/iommu/iommu-priv.h | 10 ++++++++++ drivers/iommu/iommu.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 drivers/iommu/iommu-priv.h diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h new file mode 100644 index 00000000000000..7c8011bfd15374 --- /dev/null +++ b/drivers/iommu/iommu-priv.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __LINUX_IOMMU_PRIV_H +#define __LINUX_IOMMU_PRIV_H + +#include + +int iommu_group_replace_domain(struct iommu_group *group, + struct iommu_domain *new_domain); + +#endif /* __LINUX_IOMMU_PRIV_H */ diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 10db680acaed5a..4c91d9bdd114ab 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -33,6 +33,7 @@ #include #include "dma-iommu.h" +#include "iommu-priv.h" #include "iommu-sva.h" @@ -2191,6 +2192,35 @@ int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) } EXPORT_SYMBOL_GPL(iommu_attach_group); +/** + * iommu_group_replace_domain - replace the domain that a group is attached to + * @new_domain: new IOMMU domain to replace with + * @group: IOMMU group that will be attached to the new domain + * + * This API allows the group to switch domains without being forced to go to + * the blocking domain in-between. + * + * If the currently attached domain is a core domain (e.g. a default_domain), + * it will act just like the iommu_attach_group(). + */ +int iommu_group_replace_domain(struct iommu_group *group, + struct iommu_domain *new_domain) +{ + int ret; + + if (!new_domain) + return -EINVAL; + + mutex_lock(&group->mutex); + ret = __iommu_group_set_domain(group, new_domain); + if (ret) + __iommu_group_for_each_dev(group, group->domain, + iommu_group_do_attach_device); + mutex_unlock(&group->mutex); + return ret; +} +EXPORT_SYMBOL_NS_GPL(iommu_group_replace_domain, IOMMUFD_INTERNAL); + static int iommu_group_do_set_platform_dma(struct device *dev, void *data) { const struct iommu_ops *ops = dev_iommu_ops(dev); From patchwork Sat Feb 25 00:27:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13151882 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 E01E2C7EE30 for ; Sat, 25 Feb 2023 00:28:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229662AbjBYA2O (ORCPT ); Fri, 24 Feb 2023 19:28:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229643AbjBYA2M (ORCPT ); Fri, 24 Feb 2023 19:28:12 -0500 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2073.outbound.protection.outlook.com [40.107.212.73]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7428F19F2E; Fri, 24 Feb 2023 16:28:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QtqbfHuh1IR2N5OHdG7Sm1mb166ok6skIyf2PuzjH2kooM4y5HxrgFAFB54/QwkCMbQA7yPnJz0RS2W7M+4GKuWWQ1EwOk4RuiIQB/ADll55ICldVCpbrhZaRX9KAjyPAKtCFtbK9TcZw3cQ8q058NrtuHol9zDxezD5MeH/H5MdVEGnh4FWb6418faAmNrP+IhMAqG3MKK2jCLfg/i9+EZr14GRglLHxsrDg5JAIncljSngWlX3Y4as8Ciobzlbs5Cz3rbvOmxSAl5Cur4WJM71Rl6j7o9wkK7b1WXoTObRChqhMuLBFp5VTeBf7lqZvM0hhk4Xf3ZNu6UEh5pCZg== 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=p4jsDnIPKUURm30JQFwPrF0/DaKdFDSyLOO5CGcC9r0=; b=OSPxACVPdMEWy7QPXoLwcVw3T5PB0zthxzhnwNR2lRWt1aSu2nvAm+uflfwPU08VOIvKL4ApH018kw5yc7JTR7jMTAW3awapMNxUyEZRtZPi9MaubOWmY5FAwYQT2maGGsBTQGQw3eQEKw0AvCzEe7k+qMBsykBUZUluotWoaTuA2NwlY+vmOdqX00qtiDvrhcWAZ/fjvjIAuCs/7XclXOX7UEL/GfjGuLOgVWofjJ0Ahu/fsuQa9/rpJKyGyiZ55c7msgDgwPb4AHuynKwkJzSoHRx2lk7nZt0O8hDaz/MujPFV41Twydf04jpGWRLpBLm76ppk3s9zpox6eErXeQ== 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=p4jsDnIPKUURm30JQFwPrF0/DaKdFDSyLOO5CGcC9r0=; b=kT/qbJPvwo32Sxi70O+AdBM57lFHcSz/j5EFc39aCIB4FjlsCx1SNsrXRMquRNA4okvbfvxFh0JFn/88iSm7BNlAifgywz+YPLoVU6nsYLKs4LQSFq3RA7RXpS+icrPKUFIiZs/swr+tk8sANkwBF3+Zv/428GQ3xoMjlsnx8qidpMQlKaDsif3x5vwaSACSzZvyABO2E7pcVUdt+6X6yZpSg5H6c7Bf/eawInp7eM0Xy1nMFuDIOgtugmGdTBal/BYtIkO+gc9R7B80UXVuTXSaeymF1lAOCjtbID9z6u82KSVm5EK8cOF1oNdpzMyooKJGXlTgTzWn/l2QQuJSsQ== 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 DS7PR12MB6096.namprd12.prod.outlook.com (2603:10b6:8:9b::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.21; Sat, 25 Feb 2023 00:28:04 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6134.021; Sat, 25 Feb 2023 00:28:04 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH 09/14] iommufd: Add iommufd_device_replace() Date: Fri, 24 Feb 2023 20:27:54 -0400 Message-Id: <9-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR07CA0027.namprd07.prod.outlook.com (2603:10b6:208:1a0::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_|DS7PR12MB6096:EE_ X-MS-Office365-Filtering-Correlation-Id: 3e8340a5-8d5b-4458-dacd-08db16c7271a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: L3CwmQnOFM6ZE82zg2eN3z81fdxGtXyIyQseRkfJRXzpBj2SVMtssk2E8w2mWInOYwh6DCiuPT67Irh92kWw/FXMdHm+MgVLRVg+XlWx8set5qxkN9uH7H2VhbdgANmy25xTHsIZAjJchROCuo00/Fi49PBqC2mdpHKmbY9RN3BVPH2OtJYsRrSlh88yY2Um+V2mur5/41pHGc4q4yQWDTcOOAM2o01P9cLqXd8CI1pqrIoaF3mpJZ7B54VfdscVN3aiJRsVBVqIo+hkejpClejM2bzo8RV/Q3eHYD9X/6OJ3PT5F+xBGzbAKp4/+1epp9P1CEiiALILHoQtAVIvnq9NMcvT4JHwsJHAc7Tupwa4/BHAGPnSTyimiiLhSD4Q7dTaYJIkOp/wZMeMYblDuJSEC032iMoUuPJQ7QfJ63wV+cMe5SZexYrpsa9RZ4ST5Se3SBA9g1VT+3j0UFG73kB5IjxfpkGMsLE6cRGTQxRYMSaBZyAboL7QDIVksatDu9A0SDZx8tPXQWLA2CHUVQRvGMN1upUp7NI06b7sBVlco7Ff9TsvNo8Q5vlHUfIVTxipCKlYKS0vyM/ys1q2I1ZQLSfDSDOEX3AdEbct25VpAFdLPBm9sHroyiqfZq5XRLP1IibX9TZWL+CTILe1tFWvNsWSwBpAyocPXF8pMstAxUo7NKcpwXTlVw8de+og 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:(13230025)(4636009)(136003)(346002)(366004)(396003)(376002)(39860400002)(451199018)(2616005)(41300700001)(83380400001)(5660300002)(54906003)(478600001)(8936002)(4326008)(38100700002)(26005)(186003)(86362001)(8676002)(66946007)(6506007)(6512007)(66556008)(316002)(66476007)(36756003)(6486002)(2906002)(6666004)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: geFc9BWn2scDXzHm4hwdWYPyZrl23o5cuRaF9kqZ/1DKzI6pWqrcRNHGe7dFlgiebSTO9psoB01cxWB05oRZ8yA0Hh2Wl74oW31fjmsMXD9Ykro1ytR1Br7Kybs3e6vh62xfKyFr65Aa+5AhG6xUUhb7HUkVktw+0RFKZJL44Dc3bh8GwFfWOvFywNF7iQxxThTDbGIk+LXBMxzadRDBmQN0b3AxC4L/hs5KRf3vniqzD8/n03D5ch/j0I69kRKszmAXMZB/JnNuPYK5meFozYBlWz1NEM7rN6iMBuspqQ4Mco5ogWm8v0xWBCJPl/jK04RO+7toP14OdjtSoMgGql9FjRw3AOKfnBj1XRaSX4Hki4R/EE9wyX/ZTBTUljQHCaEYS75gxBK3XYEUJzuxd0MEuuNUr/JJQ8mQuzf8sLt/NMPXOGAjgMIkP/5sHL9LXlxFJ6UXpiZNs1UxDvLilF0+kfSMxKh7tw9yNQZj+JfqMfzrUvC3/P22CZ2KE4fS10/g09FYyABNvwgybfP1O3eG4qYBaRRsHCFSUgCx5D6b8Vlw2c54uIC+XNfs5fKSLy+dQ+QqLMvfEDYrHrQQeV2TlGp6W8eN0Z8pMUNxxhWQirYFpii9Nai8j7WikPwIlhW66RDnatrJpqqdbFW7CpdfCPLxPYwnLSuqlfKzIeKv1w/aR9TzlBhxUzptlvSZtWqNxuGKsmeX979qNZ/MPXkFNPLer+eNSgZmjqjuF2BBg/OGMiX92WJJlW3WSdtl63HfP0RDGOsSniaJor+cOotJ4gzI3mNjQmnWpV3aARJxiR5C8cI31mR6QJZ36V/aypA43VzTNnnfzhpKNbss2PITLPcE7e4jXAgsQzz5IEDIA4S14I4nfJXX5QvojWHVEmQNt5Qjtxjsfq1fm27dJqN56QVmNTo06ncS43siqsYPaY3OyClok7R+7K3ebGQVOT8i7SABpepz7p/Z1xNhegJ26bYxwoB2Nkg01/VDGk6WGsvxx01ehDIj/j9RLcLS5S3yxEV06eVm8FDYAqZnHmePOFMPwfr9QZQLzfxAy1HPfM6y8mQkbwHTVoitbA82eRti8y5SKPdNkRVA46i98j0l+LsHjDa96qJCCPpZOrQtO6iPAVnD4hEeJTdgdIbJzEXqqjtWCbaXdIKUS8laxBoB10KbbA73cQOufOSxob0BKXE9IpHC9VuCFvqjXy1Al5X/jeRtmlUCDMUCRa8zsbHnmlrhqQwR11CSzm/i8Iyz8w/RaVhgO924C777eH6MVzWNmcCrPU24hSJOGszhfHzQLZrEvt9FVRhf8ePsdZy1VPJNPZjSTWq1q6l1cjY9gQw4oXHohlzZDWrKyxblIBD9RyrTJDcYhJ4eWzuqQTJjpGVz4JUSELchQHMM8gVGvDnlmFXtrq0fGMu+vC8TmAdS3Qr/oQCLpOzo1Ddz3a5ytUL+ortAsEXUxd2bLz1Hf8pyLQj+wGwQqdnIsCytuVE+Zp2IWu2bnvU1MiAVA1Auis40IJoBmyBrZyGu5LOtVP6X6i1uQ1WgCtOvGNnryNIaroyhci7ogBvpnCtzslltXN4GlylE7IBVwKpGW+Vb X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3e8340a5-8d5b-4458-dacd-08db16c7271a X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 00:28:01.5930 (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: WXP+DOXfXTWpc2kq5KWzoPBY8DOMkVXc9YyJKkPsJaB3uvSu4kLogOsRFYhJxyEf X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6096 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. Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 216 ++++++++++++++++++++++++++------- drivers/iommu/iommufd/main.c | 1 + 2 files changed, 175 insertions(+), 42 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 7f95876d142d7f..913de911361115 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" @@ -338,27 +339,101 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev) return hwpt; } -static int iommufd_device_do_attach(struct iommufd_device *idev, - struct iommufd_hw_pagetable *hwpt) +static struct iommufd_hw_pagetable * +iommufd_device_do_attach(struct iommufd_device *idev, + struct iommufd_hw_pagetable *hwpt) { int rc; mutex_lock(&idev->igroup->lock); rc = iommufd_hw_pagetable_attach(hwpt, idev); mutex_unlock(&idev->igroup->lock); - return rc; + if (rc) + return ERR_PTR(rc); + return NULL; +} + +static struct iommufd_hw_pagetable * +iommufd_device_do_replace_locked(struct iommufd_device *idev, + struct iommufd_hw_pagetable *hwpt) +{ + struct iommufd_hw_pagetable *old_hwpt; + int rc; + + lockdep_assert_held(&idev->igroup->lock); + + /* Try to upgrade the domain we have */ + if (idev->enforce_cache_coherency) { + rc = iommufd_hw_pagetable_enforce_cc(hwpt); + if (rc) + return ERR_PTR(-EINVAL); + } + + rc = iommufd_device_setup_msi(idev, hwpt); + if (rc) + return ERR_PTR(rc); + + old_hwpt = idev->igroup->hwpt; + if (hwpt->ioas != old_hwpt->ioas) { + rc = iopt_table_enforce_group_resv_regions( + &hwpt->ioas->iopt, idev->igroup->group, NULL); + if (rc) + return ERR_PTR(rc); + } + + rc = iommu_group_replace_domain(idev->igroup->group, hwpt->domain); + if (rc) + goto err_unresv; + + if (hwpt->ioas != old_hwpt->ioas) + iopt_remove_reserved_iova(&old_hwpt->ioas->iopt, + idev->igroup->group); + + idev->igroup->hwpt = hwpt; + refcount_inc(&hwpt->obj.users); + return old_hwpt; +err_unresv: + iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->igroup->group); + return ERR_PTR(rc); +} + +static struct iommufd_hw_pagetable * +iommufd_device_do_replace(struct iommufd_device *idev, + struct iommufd_hw_pagetable *hwpt) +{ + struct iommufd_hw_pagetable *destroy_hwpt = NULL; + int rc; + + mutex_lock(&idev->igroup->lock); + destroy_hwpt = iommufd_device_do_replace_locked(idev, hwpt); + if (IS_ERR(destroy_hwpt)) { + rc = PTR_ERR(destroy_hwpt); + goto out_unlock; + } + mutex_unlock(&idev->igroup->lock); + return destroy_hwpt; + +out_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); + /* * 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) { + 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 @@ -372,52 +447,57 @@ 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); - 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); *pt_id = hwpt->obj.id; + iommufd_put_object(&hwpt->obj); + if (IS_ERR(destroy_hwpt)) { + /* + * -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; + } 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; mutex_unlock(&ioas->mutex); iommufd_object_finalize(idev->ictx, &hwpt->obj); - return 0; + return destroy_hwpt; + +out_abort: + iommufd_object_abort_and_destroy(idev->ictx, &hwpt->obj); out_unlock: mutex_unlock(&ioas->mutex); - return rc; + return destroy_hwpt; } -/** - * iommufd_device_attach - Connect a device from an iommu_domain - * @idev: device to attach - * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HW_PAGETABLE - * Output the IOMMUFD_OBJ_HW_PAGETABLE ID - * - * This connects the device to an iommu_domain, either automatically or manually - * selected. Once this completes the device could do DMA. - * - * The caller should return the resulting pt_id back to userspace. - * This function is undone by calling iommufd_device_detach(). - */ -int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) +static int iommufd_device_change_pt(struct iommufd_device *idev, u32 *pt_id, + attach_fn do_attach) { + struct iommufd_hw_pagetable *destroy_hwpt; struct iommufd_object *pt_obj; - int rc; pt_obj = iommufd_get_object(idev->ictx, *pt_id, IOMMUFD_OBJ_ANY); if (IS_ERR(pt_obj)) @@ -428,8 +508,8 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) struct iommufd_hw_pagetable *hwpt = container_of(pt_obj, struct iommufd_hw_pagetable, obj); - rc = iommufd_device_do_attach(idev, hwpt); - if (rc) + destroy_hwpt = (*do_attach)(idev, hwpt); + if (IS_ERR(destroy_hwpt)) goto out_put_pt_obj; break; } @@ -437,25 +517,77 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) struct iommufd_ioas *ioas = container_of(pt_obj, struct iommufd_ioas, obj); - rc = iommufd_device_auto_get_domain(idev, ioas, pt_id); - if (rc) + destroy_hwpt = iommufd_device_auto_get_domain(idev, ioas, pt_id, + do_attach); + if (IS_ERR(destroy_hwpt)) goto out_put_pt_obj; break; } default: - rc = -EINVAL; + destroy_hwpt = ERR_PTR(-EINVAL); goto out_put_pt_obj; } + iommufd_put_object(pt_obj); - refcount_inc(&idev->obj.users); - rc = 0; + /* This destruction has to be after we unlock everything */ + if (destroy_hwpt) + iommufd_hw_pagetable_put(idev->ictx, destroy_hwpt); + return 0; out_put_pt_obj: iommufd_put_object(pt_obj); - return rc; + return PTR_ERR(destroy_hwpt); +} + +/** + * iommufd_device_attach - Connect a device to an iommu_domain + * @idev: device to attach + * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HW_PAGETABLE + * Output the IOMMUFD_OBJ_HW_PAGETABLE ID + * + * This connects the device to an iommu_domain, either automatically or manually + * selected. Once this completes the device could do DMA. + * + * The caller should return the resulting pt_id back to userspace. + * This function is undone by calling iommufd_device_detach(). + */ +int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) +{ + int rc; + + rc = iommufd_device_change_pt(idev, pt_id, &iommufd_device_do_attach); + if (rc) + return rc; + + /* + * Pairs with iommufd_device_detach() - catches caller bugs attempting + * to destroy a device with an attachment. + */ + refcount_inc(&idev->obj.users); + return 0; } EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, IOMMUFD); +/** + * 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 e5ed5dfa91a0b5..8597f2fb88da3a 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -461,5 +461,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 Sat Feb 25 00:27:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13151877 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 2E820C7EE30 for ; Sat, 25 Feb 2023 00:28:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229625AbjBYA2J (ORCPT ); Fri, 24 Feb 2023 19:28:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229500AbjBYA2I (ORCPT ); Fri, 24 Feb 2023 19:28:08 -0500 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2073.outbound.protection.outlook.com [40.107.212.73]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAA1918B17; Fri, 24 Feb 2023 16:28:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=S4tCRjr+aUJ1sOGEEX3wioa0l3nMDXSjxyhdByvxVdAZOqfHkmALna5WYiRjJfw3Ot9g7OehUXCyG7cDNaaH3PT6Do2hwGpKopGqb0ywPwcZl+t48rdpckY70e5XTBH46t4bYx28fZjkWNuIdFv+A/LHhgYzhW+9Bb2TFXV4NsZzPwr4l/yiz4FR0rSIGEvz+JsuLKL8puUslIq8oSeUntO8DuRu0qEmUmOdmJXF0/A9IMtHe1lUIsniG7uQq+JNMN/IAHzKsVUFeJACjKht8SOA+YjLyASXnaaU+cN0SlVvnneYD26dwP9SXQOCwVvezIzp0NnbZnd8bDfdhkltng== 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=UfkDS3OHo5KvGeEkDinURCWayrdEg6pubPX6m+SQ2yw=; b=ajbmv29AsEktEV9vpk8C0uUcbYRUbXue1Rhhnp5M/n+bS1ORDUj6O33W5aol1le8zFrXjPXl4SHRngPLtvJ7j3sErF0d+KabykdAOFlsnqNYe65oX42VTwCk8P4sswn6WpVSr+fSdJd7VcvpdmNxGuU/VwqvH1Fu1gwTD3YkFezRwwEh0JwPrkGMNZmjQwBymhWYE3hYSp5zPNGMEl9MHNfRgtVTtBNBo3x2SLsxQqu6XW9lUjnc8Y9f+L93z9l/YNYi5xGhI72fy1dMHRL3pVoRKMydTmHCIcsxN2aHWlgtluJw2KJf4tPQvazdubzyPJgEwNRZdr0qy/MH9ZVoig== 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=UfkDS3OHo5KvGeEkDinURCWayrdEg6pubPX6m+SQ2yw=; b=JsYpXj9HQa5EDtaSrlpW/jiV9t7WcxjLtJyhAIkpVpKGU0xh0+BpuhZNGlDumY294DdUKqavsLH6ZdiI1I+5xtAzr6boAdqFUcdr2s/YCaG8gbmzNkU41OejQrK3dWDwXAplYBkLqmcIprcokMf6Aac4anEIRklkCsXbAznynuEINyndrZsboEUCxki+eu0HYtSHDellNfcF0jyoakqHOQdBa10czggGoQwU/9++FZXQnaG4Qv/nblBN8PXq0bXLnCJzQFagUEaCjrSmBIqE6SDlkkWR+/AEx8Tgxoiobbh7JAv0ko5uZNztHQkKLQd/zNnU3gpJ35OxR9HGkcqRzA== 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 DS7PR12MB6096.namprd12.prod.outlook.com (2603:10b6:8:9b::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.21; Sat, 25 Feb 2023 00:28:03 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6134.021; Sat, 25 Feb 2023 00:28:03 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH 10/14] iommufd: Make destroy_rwsem use a lock class per object type Date: Fri, 24 Feb 2023 20:27:55 -0400 Message-Id: <10-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR15CA0033.namprd15.prod.outlook.com (2603:10b6:208:1b4::46) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|DS7PR12MB6096:EE_ X-MS-Office365-Filtering-Correlation-Id: 402d8fc5-03b2-48a8-c8d0-08db16c72707 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GygNCsJuvlDlWtTBwl9JWVcRAfeOP7dd1jgBbDG/Wm5xJxI7ejxUxi3gqL5HV/g9JK8gvY+iT3QKtUO7eoEbYemv6JLvtz/mFVUXHdxKWRSlEplf6ISlVQWGbNPs0oHNJCZus/uLkrsytGoh5h+tK4W9+urNXgfJN/kjOEsaeif3z2tdH51O+q/lUTJ+fnsNf3L96NdqBhIelz2vJZbfScXV0AheOVdtKQTpLevm7zblICV1AMJB+rGAHZF08Ayg92J3C1eccGKTdggVCXtEWs0qvGNlaTd43nD6STZ+5L0fbET1K0j5YzpPCGMJZ/le2iQRO36Via8DZRnSeG0XOuBlVo+0J8jk/4orsEbael36e4O14C8BRsBNLeQg5/e/FHpDurhMPcNHIaTGKvZ1hPOKXdDu4biGuwjgWppqujB6w8iEnWZ1lWL+PfCxm83eokXDCzAqE/LcIHUqFfeb/1FAuPVotRtbKl+V8aqSWwviW1Y0xiqfPd4LE/2KU3/+sxZ7A2axnyHFewqRWVcMJsMpgGAyfi3wPIELG+OltCXw9hSp9VHMCxerrXsQHrGGIV/pImuOdZjO7DVDtEn3Rf0UNHgrxg4R2/qyPKLmOD1QgvpcdidZitYlUGXSsAJ1ck0wXnG5I2JmIAuOmRhYEw== 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:(13230025)(4636009)(136003)(346002)(366004)(396003)(376002)(39860400002)(451199018)(2616005)(41300700001)(83380400001)(5660300002)(54906003)(478600001)(8936002)(4326008)(38100700002)(26005)(186003)(86362001)(8676002)(66946007)(6506007)(6512007)(66556008)(316002)(66476007)(36756003)(6486002)(2906002)(6666004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 0fTVXTXZAU9UorreBhSw89ZXwm6JLk8yHDKNna32EA4A0SKSarOf3/qRTLAjTH5z/ziCvopLPeKJRkctZwEDM76tV9Gm4SisDhkXzOff8Us9jh1Exz3rUHd8z8Y1kDjDc31JTLOW2k00w6Cg7hOuTOCGUPQkTA7jDu+6xVxHr8CaTqCmm6bNDv1d5GSCYVKbU5L5/IccfOkWHLd57woXq1VoEByAvjdxfC3nXfzftDpQSkYSjwQJ1JOW0qrlj3yrG099puyXrjuDrT6nuDixIpdOQZpBa1hfxoyZPw7cRm/6eNHU80aDKP3daygnydGkHQ116t4t5VyEspGtgw98t0ej1+gWIXO7dMroIAP0UB0t8NDJB66+bHZAMhjG2A+yEHu2/DEZ5urbo5yrvuY0uM1jRieOuvGeDfw4zmAKK0lj7C2r+z2skskccTP2uV2Q1VVLnNG4rU7EP553h4tYVlcFhVFA9dBmeipuylsHNH9esCMhOseDBlkkOGHodApiY/lVcXU+otuZyJYsP8gWFdoPRmE0S35VwdrlcAHy33qAxRGsZiUd4DBEtOyFlZOhH28Xm2kx5Di5HhmGt4mKtiFuxjV8UQtNTTkb3h0ohVu9R28YtjhFFPEeFngJZfKb9lubhemyayWkRe2pQIBkkWdUAxB+Jf6G4pjmdPI6RpJReiM1cd0qp1/PMJea6ol0lvwbA6KfS86uvsJtEpOcLJJ6uP7cCTmUAjwwRiSq+1JSikS5Zs+WytzwOtAILLSdBAm09NITm101Sie82YunEuaq2/tY715SWqGVxlmsNoGnb4Vj35YKQeqaUZqPYZoSdn/IumPSKD5EAZe/hNLJBxQ9Jq+nulgOsrXOU+NBX9mbCx+x6gQfH/FTtnhF2O9/Xd7cXaghmcsnhflWoH9mG13SFJGbcYXV83DzQtx8KGG0yU0p5tcBZmQrj3TA5hmzJzuS6CcoKNgtxB593l58GLEO1xnn7nBKBM28/d74nGT9K2O4RjtGGH64WdGO7fkQW47cSbKbH4SQj5nHFG2esHgzQ9GdJAzIwQ3CM2/qbRFOSSKp9x7LdsAcxKaS9jqkF91Nlwglm6LK8QcT35SDzvoUiCYTBbav4LlsVgp3lmBTNsWqdla1WJFQSFWjafhREkpqo3iSY8pR7fb/bbQ9iHeSbQgx1d7pRcYjusf5TCyVjFUNwrZnS7ju7wY3jQkXLuSTXExl0zK67btiGkt9qH/vy2PN0GCgNZh/KkVG5s8idd14dsaf3SGe0Ty17OBytknSobZmoG34TGJUOwfoIgWvUXvUB9QS0spWMtBxY7kiEs23KI1hFJiQfUDwrypYg+OrZ3Y4HaY21dsUn2Bx7H21pCcC3S/jsgoWJY8piMLPIN+jHJblLIqZOYmGJ/Zj9iYIupWs+isPFVyil74dJfE5dVIh2lPe8ZU4NHg9D6fQIf9khyhNYYB5c2YdsuT+Rfzf6UkRMGwITSLBfylPqc1nzcqLQqbKcp/kM/azm7g2UDPdBkj7+9+x0uEsIVA3xG1EiHAE3pnmP9D+AZv5JVhNqSoydDse4PI1b7nD8WDi6cdLfm/5yz6Ui+WH2xxV X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 402d8fc5-03b2-48a8-c8d0-08db16c72707 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 00:28:01.4368 (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: WouUf24WuEOlOpw+w7tXmcjqXv0+kK8LlTnA4gBg0xjBZgRwO7JlvjCxwf23Ut4o X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6096 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. 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 8921761ff98980..cfcda73942b533 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 8597f2fb88da3a..9cba592d0482e7 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -32,6 +32,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; @@ -39,7 +40,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 Sat Feb 25 00:27:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13151879 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 C03DDC7EE2F for ; Sat, 25 Feb 2023 00:28:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229636AbjBYA2L (ORCPT ); Fri, 24 Feb 2023 19:28:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229631AbjBYA2K (ORCPT ); Fri, 24 Feb 2023 19:28:10 -0500 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2073.outbound.protection.outlook.com [40.107.212.73]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B0EF5B96; Fri, 24 Feb 2023 16:28:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=B8DioV139lw5wAsgLnVNiUC1doSjmgIWdJurdvAtO4GitstaJcpAMS6aVDBVEaOcHtpMe3PfA3vDzbxO38YHBmHN015Nf5N4Liw/mB33dZuQuBD036iR1Zs7yJBGVvFenuCawPXkY9+1YFHBwoRW45ZkuEz31xdtlOXEBQ4Ni7jPqeddHyIhSog1ZX52dCldSYPYfs/u2BntOl4l0fO+CR2aqAQdLVT6I1FCQKhi/9kSSz4NPtguXa4lWlY3fT6v4RW29j0E1XJAurDU4S+8tB/KE8jD+AEJcrJ/ongq8hRlMFq5M0Da+sZMAjOPC8qXGQGDGjGMPjJwd1GeH6x0wg== 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=CzAwhhrKn3w0dk9EHUJLUTC4ccWseV42hXhYzMtlCvQ=; b=Uw7ILcN2ddsTVtjTasTKzT67smCR2Da3SITOlkmk08dxytlooOksvZYbFzqVPT3ln6onhSrH3XkiLNYouwdH1V1GB43jzKbUZrDve477/qZXHdBcVEt54gnBMTbcabkAX820W8GW62YTBYPjtps1jvVae9PQSJlIo0NnhfsKfstJYbUU2wjIIjPKH6Z7DXr2vJIA+kP4/GZglPu4rwJUjlcqSP3L/IHZWV3sNdk4UzbS7268BqdkmOhAyVmf9a5ZD+SRhERVKqM0QttfAeIkXztuhmmD/9lTU4ylra5p1febwZjHOJOnciVI8bRfQIc1vJR/ykX/dN2Bpq4o5HMo5Q== 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=CzAwhhrKn3w0dk9EHUJLUTC4ccWseV42hXhYzMtlCvQ=; b=sS7kjzWkFuX23RSxwrf1f/LqGHOQUJbT+Zk2trvzsEj3MDdwbg7FYjFeuH9Uu09rrCZIm77oBej20eZ2JYAvGABLD93b5fAmvM/vw0rkafgdVIv/pkJbbIqbETr5XgsnUdI2FpVmR3gwJJSMQU9v1BWo3oVu9JXU7OWrZ27KEbH1fJESvX6UOEBUfTBXxGaLncilBtuGFMxwaRUrmIKk1I1mAVT1IqTRhOKWEn/Cy6CllSmSraO5ApeGO6TumpVXB4uTUJMYXxYUG4oA1dS5p/6Niw1ADLSMjnoSUgJekCxVscOY78o6Dl+qHJ3P2zLJPhm8D628fqCbi1aRObngEg== 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 DS7PR12MB6096.namprd12.prod.outlook.com (2603:10b6:8:9b::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.21; Sat, 25 Feb 2023 00:28:03 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6134.021; Sat, 25 Feb 2023 00:28:03 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH 11/14] iommufd/selftest: Test iommufd_device_replace() Date: Fri, 24 Feb 2023 20:27:56 -0400 Message-Id: <11-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR03CA0150.namprd03.prod.outlook.com (2603:10b6:208:32e::35) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|DS7PR12MB6096:EE_ X-MS-Office365-Filtering-Correlation-Id: 7c2b6316-6a2c-4550-c8d0-08db16c72707 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2AaO6WDLmwdec4ZDnPEmv3e9gkhkb/umMangZR+UjmntverOESX0SiIutMBmwXfjPHVOoAoOU+3Wm2Ef6G08UVKL1MAN5mlV2CpjkQAkB31swQqqv6DqVjzHLgM27nIZTxzI61hUXsHGt+s6oR0/ySavYBoFaWrQ+kBBeCLa4BgbQJSyJYkTl2sII1QwEEr2IvGg3WsjYWdthQirCFzwgtporgsKaZorYXhoLWOPPnTH/2YNidOwNLMlJmfGSfO6Bt7JH3fhtlkyHGXtKxSdKrbxoqFV+wAhb/kA6OvEev4Qz+HMh4VUGfgNG5VhhA6fEpj/5KeIJSyTP5aGfUR+I+lPwZr8f9iIL8bDLrGHbDdLpiv6cXdyulM4N10m/pFYWlEELg9CMCDndvloyzzc751MVHjvxyt50Zu7NS+PFPDZH3CfDPd9KP8CJ748fNzknVHm+HcxTzvN4S9+hBwisr+yWS7ZmschKFmYK16KElO4q5w6bfC4m/r8ZnfYZpJCZRetxyilfkSD1qxrs7ncvk4a7oy4djGU7PgSFDf6ztYF2C++G9KKpbfPN1dqbsDlGrqi8yB8bbpPUQTtfSx3j7nUEuu8lbxMxMS55pgOC4/VmjFO8+zaqmKo6p47iLVv54RHlfXYDke2OIAwjWXZtQ== 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:(13230025)(4636009)(136003)(346002)(366004)(396003)(376002)(39860400002)(451199018)(2616005)(41300700001)(83380400001)(5660300002)(54906003)(478600001)(8936002)(4326008)(38100700002)(26005)(186003)(86362001)(8676002)(66946007)(6506007)(6512007)(66556008)(316002)(66476007)(36756003)(66899018)(6486002)(2906002)(6666004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: VHO0xjNeTsTrSPuGjVliCYAGmkq2otY/WK5OtfnuhPHZZa4/aFzMZewdNooS/dHVBMu4HhjhPee3YLXh6vvjkExGbC+si6wM8ogkjYQbCYACgMPjsgLzKwOmqnevfQOblSLSwjjOzY3xSg/YVVG83UoJCaL7sKnyrLTZSZu5aYle/E7ANM8dmMg15TKVxPjHvTIrhkhncJHFZdF8tX16uCBGu0azOtx5yxmqQqKMVJc0XJMgN+vXppApFDgIRB2QsN4XYnvnrSFdqdmgzwUGnMVaVuWFo4wstONyk3VGgiNXrnm7TLQcwH3SWtnYVxCK29kEDqvDcmsdolnS5xGT0W0cXAQXFfiBZcsg3jXgjPMJU0C5UYrr7vO8ptcdqS2XEj9aCS/Jsob8ag5ZVjnosqNSkeMfO+eHlGwYuT4dhfdQLpH7PK/nHzwyS8pstgD/Irw7w+foYhb60nhzOv0E5F7P4utTZa1thRawlqjEAa4zQTBEtYN01HguzO6UQDVKD70xSKYRj/CwGw+EGtCS/Lj+14nHTNrLRrOSAepem5ssSNjrhTNeoYRUA+wZh//zvcikw9uA3EPS8vxN3+NIpDlIlJ7BS6WyHk0ELuaet95uC0kkNV2El1tj2BwcRsKAML8xOX7aED5VRBmHi2MHHCHQpo9Wyh9XbfwnH8C7Hzl/lWTMkzEfmyPgohP1DBXmIPP7HHdcQM6VSSPfnMQz3YA6NeOo6mu07ogffDgKxo+UrKC6NR5QvT5CJrR2Hi/tj2n1g0UwobDdS5LRFdyzz7O08IWMF4w9adv3R0WfO5H/wgyzX+xz58s6fP6Qt40uomizkhBFhhaWRPytZytOzt4MT1+LknrmMqB9iCzNQsJWwRFDJSNzlt0VQfvyH+Q58EvIa5s0AKwgRxpvsYfCHgcb0uygZWAoh31zuG+8q6c2mQsY6shhb9IwMq8MxVOiAKXNT7gXONivfmmH5AiQ2J8b8JPWKewqusq9Zv2HcQKkkoWhTiKLnIrGzojm8YwF7gB86E57MXZ2bu0CxAPYYmZI0qn1gpsz47CjdHMlkFv7Pc1eA1cS2NfAKVNpWEEqhN+/8NEtDVao1d9cAxezLFKSZVbbio2ltqBJhkXsDTKBn9DTPVcLZG4NyEB6giCgYtISKbOAYxq9yVZw0DTg/AYV6Nrb2f4Yy027YLJ9JdsnHCnnKSlZQGcyDKUp5ndIa1gp+q5dFEBBQJ/PB4M+SsLlMzcrPeA5hxnC3pJEb0O/etLUseWuv8oFtg8BjPjdAVn4HJ/ccuzT9bHKX7i3oMtItNWXwlSMg9/fvbMtK2anOAZKujuXLCSUC0EUOxWJ92bdd97XM/uwJ3ZdoqjXfxTsMSsAxqATVPoNATvs+OQoJshE/fC6fkSQf1NI91EESGSN2U1o0/ou9u7tt5vzww8rrTo1BA54Mf88GZfXvkt7X8sxOOxt35aHGkNr3Y+E/uXKOsAMQM5SCYYYVu/S6ooeTECYbIV3j9UDDKtt/uH0NJol/a809rBKmAdjaueSHc1sz5Ol3zQgHUEwkupHSw4/7+IZp21D2Fh53B3QUk3EgQVHjyuhWghz0p+NAI0u X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7c2b6316-6a2c-4550-c8d0-08db16c72707 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 00:28:01.4837 (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: bEP1eUw0qKKT19T3fpKBfE5S2hKeuWk8ALYbgZJ4b6uMWoT2vK+ou+DN+CLp5Ujp X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6096 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. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/iommufd_test.h | 4 ++ drivers/iommu/iommufd/selftest.c | 39 +++++++++++++++++++ include/linux/iommufd.h | 1 + tools/testing/selftests/iommu/iommufd.c | 34 +++++++++++++++- .../selftests/iommu/iommufd_fail_nth.c | 27 +++++++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 28 +++++++++++++ 6 files changed, 132 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index 1d96a8f466fd29..c60c501775e9bb 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_device_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 8540adcb68f1f0..701d6d6be3f06d 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, @@ -939,6 +975,9 @@ int iommufd_test(struct iommufd_ucmd *ucmd) cmd->add_reserved.length); case IOMMU_TEST_OP_MOCK_DOMAIN: return iommufd_test_mock_domain(ucmd, cmd); + case IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE: + return iommufd_test_mock_domain_replace( + ucmd, cmd->id, cmd->mock_domain_replace.pt_id, cmd); case IOMMU_TEST_OP_MD_CHECK_MAP: return iommufd_test_md_check_pa( ucmd, cmd->id, cmd->check_map.iova, diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index c0b5b3ac34f1e0..3044a432a83e22 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -22,6 +22,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, void iommufd_device_unbind(struct iommufd_device *idev); int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id); +int iommufd_device_replace(struct iommufd_device *idev, u32 *pt_id); void iommufd_device_detach(struct iommufd_device *idev); struct iommufd_access_ops { diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 91f6bcd5b8f5e9..e1252a874f0b32 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -1001,6 +1001,7 @@ FIXTURE(iommufd_mock_domain) uint32_t ioas_id; uint32_t hwpt_id; uint32_t hwpt_ids[2]; + uint32_t device_ids[2]; int mmap_flags; size_t mmap_buf_size; }; @@ -1022,7 +1023,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->device_ids[i], + &self->hwpt_ids[i]); self->hwpt_id = self->hwpt_ids[0]; self->mmap_flags = MAP_SHARED | MAP_ANONYMOUS; @@ -1274,6 +1276,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->device_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->device_ids[0], + self->hwpt_ids[0]); + + test_cmd_mock_domain_replace(self->device_ids[0], ioas_id); + if (variant->mock_domains >= 2) { + test_cmd_mock_domain_replace(self->device_ids[0], + self->hwpt_ids[1]); + test_cmd_mock_domain_replace(self->device_ids[0], + self->hwpt_ids[1]); + test_cmd_mock_domain_replace(self->device_ids[0], + self->hwpt_ids[0]); + } + + test_cmd_mock_domain_replace(self->device_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 dda8f0187cd88b..352835cfe84de0 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -569,4 +569,31 @@ 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 device_id; + + 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; + + fail_nth_enable(); + + if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id, NULL)) + return -1; + + if (_test_cmd_mock_domain_replace(self->fd, device_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 0d1f46369c2a30..68dbb59f9a515a 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -66,6 +66,34 @@ static int _test_cmd_mock_domain(int fd, unsigned int ioas_id, __u32 *device_id, EXPECT_ERRNO(_errno, _test_cmd_mock_domain(self->fd, ioas_id, \ device_id, hwpt_id)) +static int _test_cmd_mock_domain_replace(int fd, __u32 device_id, __u32 pt_id, + __u32 *hwpt_id) +{ + struct iommu_test_cmd cmd = { + .size = sizeof(cmd), + .op = IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE, + .id = device_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(device_id, pt_id) \ + ASSERT_EQ(0, _test_cmd_mock_domain_replace(self->fd, device_id, pt_id, \ + NULL)) +#define test_err_mock_domain_replace(_errno, device_id, pt_id) \ + EXPECT_ERRNO(_errno, _test_cmd_mock_domain_replace( \ + self->fd, device_id, pt_id, NULL)) + static int _test_cmd_create_access(int fd, unsigned int ioas_id, __u32 *access_id, unsigned int flags) { From patchwork Sat Feb 25 00:27: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: 13151878 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 9FE79C7EE32 for ; Sat, 25 Feb 2023 00:28:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229626AbjBYA2L (ORCPT ); Fri, 24 Feb 2023 19:28:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229607AbjBYA2J (ORCPT ); Fri, 24 Feb 2023 19:28:09 -0500 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2061.outbound.protection.outlook.com [40.107.92.61]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D19B718B10; Fri, 24 Feb 2023 16:28:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TIpowpHxyCisy6xJcFsEjnHeptRXTVlh3HZ7dPdb827SRaAOlE7bfvTKm4eln2fIqBZPX2lxGtbjLmGWVqlFEHFXkTQqOjp35d1Db72hgOhrneI5S8bNv3KQAM5C2b79P0Gb9K7yB66kVZNv5SZzmByuiNcv41EFunWDJcd6sXJhXoCasoJ0qLcd4lQLTNIlZLrb9iN57H6vQq1r/XjpDe3IBeDkJp2EpdgaFohDj+G6DfQR9XXm2eC7/KVKeAWK1bUmjr3rRha8rAhWzey54GwLiEyNvvN1jEBLeefZwIngwdALbA9XrbnFf0S1YHiLm8l70S52Ol2JYPvJ48j3AQ== 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=ENaO+avjAO6EdUWzFxPaguNwx7WoZnk4YCjfrM60PIw=; b=bJ3l8gA3OyK1bec5ahmt/iJ/wH3wyR7quxp9jo+a4u//NHiRIhOXgex8FteR7UixsupesX8J3YHPlqP4xWkEFgJteJU5qv2nhVCUEMFX2kq4KKKpv/D47JXif1wJOYb7n9X9P5sUPtc79berjuogLD/VeLUK/RoZ+HN5jUVNBdOALcmpF37VpSRQc1iWkQD6yuPTINzg+mfuiisrQkNERTwc+d2t5kl2GRFTjwxq3MxdN1d3s2h41Y38mF+lfCcYVWXtBxPiTxV5lDRdYrouFOudTM296iK9X9T6ZGBahq89gWZfF3aDN9HkF2eVHV8bLuqrWTL+d8toPvxbUAoG8g== 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=ENaO+avjAO6EdUWzFxPaguNwx7WoZnk4YCjfrM60PIw=; b=TfajVytg9I8Z6vqbWr8BIsCAeeiZuvyusomrzNBpoq8SoqJxpL6K1LR4n9V56CToEJfsTL2lQTHSpFkmsiyWl1Y3VabgTjshq0+58IEMFa6V8gTTmGZamYowvBCrsKz5jQ6Uf0wKRyk6m1pYy/RKYrLfzmR0XKUwnEJvZ80mS5yus46qjwL7njzSEQZKF/Ad/GMKG5PMDSoLHMpp5om0xVx/HSTbMVitzMcyfN+No6qFA/XRCSrEq1091IcmBJBAu4/YZJ1m6K0/cH5vMnUKqKXHbUlsm4OjYHa8l93TNHj6Sw5SgV46VsO4OjyUs+EP+co8r5s5vl3UnKzilvfurA== 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 DS0PR12MB8019.namprd12.prod.outlook.com (2603:10b6:8:14e::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.24; Sat, 25 Feb 2023 00:28:05 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6134.021; Sat, 25 Feb 2023 00:28:05 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH 12/14] iommufd: Add IOMMU_HWPT_ALLOC Date: Fri, 24 Feb 2023 20:27:57 -0400 Message-Id: <12-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR07CA0005.namprd07.prod.outlook.com (2603:10b6:208:1a0::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_|DS0PR12MB8019:EE_ X-MS-Office365-Filtering-Correlation-Id: 1a723dec-b4a2-4659-729f-08db16c72756 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: NWuHvRHpRtd1wCpvn25ov7hpAXyd/vJBNhnirLQY4E/BzW1iYza7ch9n/CUe3WhEOd7tSnVNzWnRRuYkpu7Zm4g3T28NyHpVhBftX+ymU/1Q6IfA0vtIXZyXtHANT8ULiEXFjy1yYhd0k2j3+X43INzExM66ehLMRxcky+vaNq/Qdz7IwO/p0pA2HI1rDEoSyRxm/8gri0Y68/Ck2Z228l1UdiClSZ4NPu+7yFqs8vLvK/kV5WhxRwkycRnNeYwFB3dl4ZrhVNUcfqaf/eOKNAfYydQ8TCvRBPUGzYuIR7GopNbukmSbB57UtskXHlnDxXsxAWyewUvrB0jHkhNeHxIHlKfNsCEx5GZ/O9OL8Rgqme//r1fD/mT2ocNnac8FB6GDAB9CXwQL4uCOdQu/TlcRmAYdoIqczWKq4u/mT2JGJ9BFaa4MkF9cgZ8kvBxfajPtz1MeHNFj2brHO2lNbofSnh5yJTwykBlJJTWFngHwWW2qjHxdvG+HmPl8KDG6zgVcPU7lkqniQ4UNBFPNipaR14pd9i29+lx4pcHx+sRh/tTy5eBL1B+BRtpBILU4iiKr3lBV4eBoGMg/zayaS1QPu0FTW2CN/6bHC+IcZ8y94kT0gQ4XrHuVjxQ0DIsX0WGRPoSdXhPfChCO1CbVlg== 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:(13230025)(4636009)(39860400002)(376002)(396003)(136003)(346002)(366004)(451199018)(36756003)(2616005)(316002)(54906003)(86362001)(6486002)(6666004)(26005)(186003)(66946007)(2906002)(8676002)(41300700001)(66476007)(6506007)(478600001)(6512007)(5660300002)(66556008)(4326008)(8936002)(83380400001)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: o0FDY+5TrJ0I9LUwtm9wbXP7f82HorKMv66F6za6dN9gf/WD0J+QnQO1PA4z1PW5+xzmPH9TThMGFBkSMuVUw8jxuJLm3UpFM4Ft5/S0+I6XflFcljbIKHcIRfV9EQpCwSQyTOuFR1DvpWeNXRNmk+dM8UNKuwkPs4Q4vsIEy76FrIGq2TGPJe6jpuMH0VDWvF8MxZKuQQZlNfwktlmt+BOqpz+N7iKhz50MJhhgWs9MraVRF4f8toQicUjlJ3Ak+bxxwsWd7Qv4PTylJzVCRPSpprqSXsoqMv35YMwNPk4/MVDKXlyKAcq2L/GpUrw6NMsBXpojmJDM8FmLfPy6s4avWVM60nqq4NasGTqIrifh2EgZAFfz+7u/T9rWM+YE2jl7T73d7Pg/gngK5VPkktWPcPhnXaMM4s63fG14TAqB1rwSe2jUM5hORpsNV0LlWT7lAs8sT5uADcMoLWYns0T3AymFZhrIapDY6NF2Giysy527Tr47kuC40jixRi24tGk3bpqXZuJJRoZpWfUM8F2JCF7AAtX4Ygd5n/hY72IoH13/G0eUcFp46WUn5kNcExnb+r4+CuPswDHBmvDei7L1z9lr2IzvAKggnLucVAbSueBMSSe4He+YaUP06kzbh6xFluFEgjX4S0rBQAHC4n3c7USTVcKtVul9MGFXvMmz122qvxs8sMB2PskSdBO/jT0zUmMyVF6uTSGU0oNvA+tNqFt7K0s4saWspxLnrQKg4lDcYCDt1zK0ee5j5tnIYOOlO6UDssJcFm2R+tureAThH78aYjy8llufzp1uSYi/SJeJNmy+j5wrtBaLp/zLVQISXmUj57OgPWmGZew079CimcT2/pd1HD1piQ9cthIA2P2Wx7REWxN9qmC5N0cjNqRkK0FCP3qwzmhLi5vJmeWf1cwsWdyBOk3bAQXB9Sjewz+Ziu3DGwjz0NdEQ3Pce2It/mxt3YRwKFZDp1K+lnh/O0hJBgGv3rESivlmnGSSeqfmIcS+28K+fIb8BhrKrrhj40mIBG7ITZI0dpxRdybrZA+uKdPpzcFXDQFoDOrZe8pG+ss2SLicHkFjLUvQZuJaOqYwOrX1H8fcuwpd23wi34dyXIYy9IKbW1qkiFDk1inW4lRZWMn4xjvm2nDVto18diXTGqYeLg5DFyKGxAFQbhctBDUKtwIbM6Ads1Oifak5xkLVP4fxjyYmZphhj0JeMpkGeNVuN+Rz0CJarNc/3i7KTBWP2nNII9Dp9LcUTrAGlMvHcPrze4aMMdrWVpi/sp8FAyyT24H4wPpIuLlsrwp8nDcEla4ePO7Jc74koxyJ6gcZ2eTo9BQRqXx4hoAp6EalmARa4DlWHlKFE66nBd5D4Q2mTQOFJx4sRKm88V+1W5x35H5Ui2BqG+kXlTM++ZP+wBYl/PLrImex4u5gr5R0UFTr8//H5TlKqzHD52L2FWbOwliX8TnyMNq/Vl4ZRWpRtZIIm3XBl9Odfr+p5DMgZSwxz3yGSDTixuoB5DCtQ5AIamn2lJ0rlhWfJqp+y0/CK2Kw5lmtH5PvkjtYlkuxJcm/Z7AneokQBYP+3Tojk7Lb1DkT3MZJ4Y+j X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1a723dec-b4a2-4659-729f-08db16c72756 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 00:28:01.9680 (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: Wbv1ssqKrrAG8iocbbKTWgBS358fQ6AdJkpnICGWZ7K16MU+IMUBcjR3C5j7h4zR X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8019 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. 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 2584f9038b29a2..a1f87193057385 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" @@ -121,3 +122,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) + return -EOPNOTSUPP; + + idev = iommufd_get_device(ucmd, cmd->dev_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + + ioas = iommufd_get_ioas(ucmd, cmd->pt_id); + if (IS_ERR(ioas)) { + rc = PTR_ERR(idev); + goto out_put_idev; + } + + mutex_lock(&ioas->mutex); + hwpt = iommufd_hw_pagetable_alloc(ucmd->ictx, ioas, idev, false); + mutex_unlock(&ioas->mutex); + if (IS_ERR(hwpt)) { + rc = PTR_ERR(idev); + goto out_put_ioas; + } + + 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_put_ioas; + +out_hwpt: + iommufd_object_abort_and_destroy(ucmd->ictx, &hwpt->obj); +out_put_ioas: + 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 cfcda73942b533..c9acc70d84f794 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -261,6 +261,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); +int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd); static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, struct iommufd_hw_pagetable *hwpt) @@ -296,6 +297,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 9cba592d0482e7..694da191e4b155 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -261,6 +261,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; @@ -292,6 +293,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..ccd36acad36a3f 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 normal HWPT will be created with the 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 Sat Feb 25 00:27: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: 13151887 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 13516C64ED8 for ; Sat, 25 Feb 2023 00:28:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229684AbjBYA2R (ORCPT ); Fri, 24 Feb 2023 19:28:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229676AbjBYA2P (ORCPT ); Fri, 24 Feb 2023 19:28:15 -0500 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2061.outbound.protection.outlook.com [40.107.92.61]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 347401A4AC; Fri, 24 Feb 2023 16:28:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=N9ooRtsnp6DS1wKhQ4701a0abNg/VZiGo0+kIyXNILtUEmYfSmj1GwHLfN9Fsxdl9ZUWIwdlNWVafexwjTHo8jiS6GmSpNO44u5py9YiluUTfLfF9YTyLJKpXFT5z2rZTdNEU61pJFJDVVGOMeCPE3xMvF6F0aNmkIiWi3Ko3UKt4oGrR3lDq3HN2cugM2LGuaOUhhKE+5DB4i4sUsj8ViIdh06GqQuruRYsQZQCCVeRoEyhicaY5i/mfagOmGGZgsvf6ngBKBF8kywQJDwTcQAiJGTH397hFh9MMze76/MjERIAehBvY/nf7vLkO3CHNOHNpG+gRvyNKV/9CXXnLg== 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=zpFyVsyIrY75L4aZxMwQJVdSVVOpwhFv5KjASqlkCek=; b=PhiPiR/ZyoH9S0bNBhKFMFuxTN/LUmt9pB6XdDsdTnyM8aTksAr1nk1PnqE73V+dxnOpQNL55o7PEwV8SnVkOZhlaESA+/ScyJrT7hJwc8+fA0Bmtg/KlidQGiWsG6mqgYxmSd/N2quzumQluGqZQSPZsSdqX4/8xkM9WIRrigYYtqwFJ7KUEY3c2fIQO5/lZl8KjJ+iGe3AmIRXsO3DiuqYQB0vmGVKJc0BUTLlEyXUWkC1AXAPlQfF9ZK8kDZtYh4gWjvqL3eLEBPCbNxQxIf1KMp90fx8I/QR5JWIUKdWtaMsSzp/RIdzkDi8BM0t/e2RsAyqHZdEEL62iGj60Q== 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=zpFyVsyIrY75L4aZxMwQJVdSVVOpwhFv5KjASqlkCek=; b=raEMj6TI70rmWtg5rb21rNzvqSmGjq0o3Z+uJmhKbchg34jOMTxqEtU+SmZqH7wMiWKLFshVedOxZ0UBd91ecN3gq2BEWz9dtxAFojWJQagJD5IZm9cnFgFRtQgxhFJLBmb3ABhROM34sUJJifgqtBs+jlD+BdZtKQiBcc1qvzo+OrhuOPOy6jL1lt7D3uss0GKH0XNXcJ37NvMRi7gIocH5EANOL0xNAyvWAg6MDkG0XXhuCN1qbsMYA4eBR41fyoUfGWTuFgb0dvomdzpkuOe6va3g9uqIkAW6eIckz8KmnYHIQ9ggYAM3PbTAOdU8C6sMxKgLPl8o6aL+KdRHzA== 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 DS0PR12MB8019.namprd12.prod.outlook.com (2603:10b6:8:14e::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.24; Sat, 25 Feb 2023 00:28:08 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6134.021; Sat, 25 Feb 2023 00:28:08 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH 13/14] iommufd/selftest: Return the real idev id from selftest mock_domain Date: Fri, 24 Feb 2023 20:27:58 -0400 Message-Id: <13-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR20CA0011.namprd20.prod.outlook.com (2603:10b6:208:e8::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_|DS0PR12MB8019:EE_ X-MS-Office365-Filtering-Correlation-Id: 9cb2bd04-1cbe-4d8a-09ac-08db16c72853 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nVPsfQXHukqy/i2Xdt+yNGSupVWhnyWl824aCoMSiM5JxDvxcU355d87jlrKwMDH6kDplAkFZ5Dl3UBKFCjKanCyl49R7AHK30hUY0zLH66sKPNNUwQGbr48xgnFClFBGcx9X/NB4brhRq57MGKyr57euM5fZHh1JMSodskVjnkY15/Epvd8kxmIiTH2mNhE1nBfAQuDkuS2iL4EeABA80J95xfg3ExpxqBoDebc5zamP/gLrl5K9u5i8MsEANiAnk7aXuLGD9bFOklQrMFYLu4kAvLfMDh8cT0VXUMG91COOe6fZPn8eti52Xd724Xg9hAlRqo10JkdHQjng/uXFmIKZJCVSGsMOBeFtRB2zILvvKAGzYzMMnmVIMSn8ER7F7yZhBl9VPxyyS4cWTHsl32IiaVvG34iRDP8/UXjLwPV4tCYwfpu2Dv7+KscHb+lYarbXRwMJveAFc+v2QF1AfV0kD4J2k1ODmxYvQD8+phQ5vXs0eeghMLiVZLAQHMzvWrjMe8BLJdCr7IqL9DgcH2jPXpPRE89d6Os9GKaqhboWVAJU3XG7IJmhac6xQqvVh83kWKZF0zbo5keRs0VCGj9PBd1RdYBgTqSGDXsrAu8FrGVD0UfqHbyeIaGWkvSqg+tdpdAjhDZ4fgyW4w42A== 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:(13230025)(4636009)(39860400002)(376002)(396003)(136003)(346002)(366004)(451199018)(36756003)(2616005)(316002)(54906003)(86362001)(6486002)(6666004)(26005)(186003)(66946007)(2906002)(8676002)(41300700001)(66476007)(6506007)(478600001)(6512007)(5660300002)(66556008)(4326008)(8936002)(83380400001)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: kolXuKMogYENc466WEt8oVTcMNhSNGtzOiLXzNiHby8zZ18rFkd65bo/QTdNKatvSwXnsdt77i21kKOWT7c/e6Dvcs9+j5vd4XD0BkDPpymUCCzJhAGBc+iuDJ5F0B76Ewa0adnAS/eXkxCJyG3w0Ns0F2FUCd6Zgj+bpZSR/L4ouQST+KMknJz22SgBZcLGT9otZra8ZEUr/++/T/ZAW0Lta+xkkm9u6/tLMYmZjnIcsLAwE7L46JSHTepUI5TWWxWL2NCg/8LK1BWVVoSKJ7FGk+TSCSiPqWU5MIjiERxd+J3n+Dz/HJT/cdsUS6PkX0lm62nxARjjpaOuLPqJTyCsrVfhcHMuTXI+kW6asH9+i6vG8RmHnfPhscnyFX9t8wPzbNt/mq7FMLb2b5R3h5rklx+O7bUHfT7tnVY49erQDOpGu5CJQlLiuqB8e1t/b9ECG8Yxg1VuhWT+4VMmfddCXjWAkbsfu6tqEIDVP7zVPL1Vl+YLWpgjUtUwuA7uUHK5j3yqG2hF2JZYizEMjK5sqcZNvBtd/LxSYmUYWAbSVbjSIzCfhP38mQHdlrlnrBDypEOmFmtLeCf1olWF8uOYcHfsBL1MC3xTliiNdhNthGMxhFJiOFYij/sel5p32rFM5z2XmFPmerQNwt81bFtXEh8UqeaFEJNQB3FcIBAdhgtMLIfCNIwREDGrBxUIeusHUwL7pg9kdmOsmePUgApyWfOhakfWu2ckUAQba4rT5NFfSArhkIEiJJmsCPtGDo6XeEJ7SnikEyVF/P268x6aZRC1NSH/lbRpaolF5IDvBwso6cuIhfpWBDPIe2xxcY7ZhWaYGTM0G/Rkj+MFKlAa3z2DlBL7X1V6zJF12WBnELfUrcF0pgHlitWhFpPhq1uid01NRmvY4Leg24MrWsKTmlCuQg4cB2FjefIy7wFkY6LwLd6QueYM2NYxdoJukYNb60LkuNzhcO5ZDTZ1adi/8Nh6mqGiQcyXOMZg16FzGXhLZIlvoPTuJ8jynj72rMb4e4nXQcPPpx4shKJm+2y7WJMAvVgittIp5zt8LUFh4CdDR+GRN/CMsS+Nv/ec5uZUcDijtL5XbmKJIBAFAORWgZOxjUOod6UcQpekt+xng0ZB8HM6Gjftgt6WoLCvrsOiVzORsUW4laXw+o96Cs9sSl7mCl+i/r+UJp8g8JsEmLMcOsBpd7oE9X715tgWFq5tVMhTbmXJAVndMO2J+ZnnCkWzHkrAWb20y3B/LawMK7qPcHS3/uc0eYSCLJ5ilMUTvzwYkqd+V6zqtfGgW6gmhLhhZx+HJq6AWF/nfFtA2FYWyujGXONsdTCD2CzOJJjXrDIGQsOVO+VbM5/z2DragQ7nFnyxcKbJIBN1h1qD2SXGn7wCvGB7l8VoKEccg0HZXmhXK1yPYRTM9kTLyLPlAupzx4QW5r/88C5mPMmh40NEkIA2KSV+fpIMPHTDUQ9T0ImkRqxgNS7PwQEnpKZKbnGR+SFNzQ9BaPb84fIGNijq9DpxnMQL4rVgC5Tmz7YzyXUYA6b4C6mCH83apv1uhrt4lc0PVlWJX7OBfPZ+huqhKzxZi1ZefI53uIIO X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9cb2bd04-1cbe-4d8a-09ac-08db16c72853 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 00:28:03.5921 (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: Pa4tqA/y5zuxhM1GShrYCfk0Y4AiVLXFiIvSjVDiGWgkyq4lYukmvKhHFi80q3tn X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8019 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. Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/iommufd_test.h | 3 +++ drivers/iommu/iommufd/selftest.c | 1 + tools/testing/selftests/iommu/iommufd.c | 17 +++++++------- .../selftests/iommu/iommufd_fail_nth.c | 23 ++++++++++++------- tools/testing/selftests/iommu/iommufd_utils.h | 10 ++++---- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index c60c501775e9bb..d35620158c8b15 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -50,8 +50,11 @@ struct iommu_test_cmd { __aligned_u64 length; } add_reserved; struct { + /* out_device_id is the selftest object */ __u32 out_device_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 701d6d6be3f06d..9cb206b31374fd 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_device_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 e1252a874f0b32..7e6fe263e1b62e 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -214,7 +214,7 @@ FIXTURE_SETUP(iommufd_ioas) for (i = 0; i != variant->mock_domains; i++) { test_cmd_mock_domain(self->ioas_id, &self->device_id, - &self->hwpt_id); + &self->hwpt_id, NULL); self->base_iova = MOCK_APERTURE_START; } } @@ -265,7 +265,7 @@ TEST_F(iommufd_ioas, hwpt_attach) { /* Create a device attached directly to a hwpt */ if (self->device_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); } @@ -650,7 +650,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_device_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), @@ -805,7 +805,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); @@ -854,7 +854,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])); @@ -1002,6 +1002,7 @@ FIXTURE(iommufd_mock_domain) uint32_t hwpt_id; uint32_t hwpt_ids[2]; uint32_t device_ids[2]; + uint32_t idev_ids[2]; int mmap_flags; size_t mmap_buf_size; }; @@ -1024,7 +1025,7 @@ FIXTURE_SETUP(iommufd_mock_domain) for (i = 0; i != variant->mock_domains; i++) test_cmd_mock_domain(self->ioas_id, &self->device_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; @@ -1218,7 +1219,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_device_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, @@ -1427,7 +1428,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 352835cfe84de0..f2012db43fbc16 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -313,7 +313,8 @@ TEST_FAIL_NTH(basic_fail_nth, map_domain) fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id, &hwpt_id, + NULL)) return -1; if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, 262144, &iova, @@ -324,7 +325,8 @@ TEST_FAIL_NTH(basic_fail_nth, map_domain) if (_test_ioctl_destroy(self->fd, device_id)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id, &hwpt_id, + NULL)) return -1; return 0; } @@ -348,12 +350,14 @@ 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, &device_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id, &hwpt_id, + NULL)) return -1; fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id2, &hwpt_id2)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id2, &hwpt_id2, + NULL)) return -1; if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, 262144, &iova, @@ -367,9 +371,11 @@ TEST_FAIL_NTH(basic_fail_nth, map_two_domains) if (_test_ioctl_destroy(self->fd, device_id2)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id, &hwpt_id, + NULL)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id2, &hwpt_id2)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id2, &hwpt_id2, + NULL)) return -1; return 0; } @@ -526,7 +532,8 @@ 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, &device_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id, &hwpt_id, + NULL)) return -1; if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, BUFFER_SIZE, &iova, @@ -588,7 +595,7 @@ TEST_FAIL_NTH(basic_fail_nth, device) fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id, NULL)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id, NULL, NULL)) return -1; if (_test_cmd_mock_domain_replace(self->fd, device_id, ioas_id2, NULL)) diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 68dbb59f9a515a..807a2421121b51 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -39,7 +39,7 @@ static unsigned long BUFFER_SIZE; }) static int _test_cmd_mock_domain(int fd, unsigned int ioas_id, __u32 *device_id, - __u32 *hwpt_id) + __u32 *hwpt_id, __u32 *idev_id) { struct iommu_test_cmd cmd = { .size = sizeof(cmd), @@ -57,14 +57,16 @@ static int _test_cmd_mock_domain(int fd, unsigned int ioas_id, __u32 *device_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, device_id, hwpt_id) \ +#define test_cmd_mock_domain(ioas_id, device_id, hwpt_id, idev_id) \ ASSERT_EQ(0, _test_cmd_mock_domain(self->fd, ioas_id, device_id, \ - hwpt_id)) + hwpt_id, idev_id)) #define test_err_mock_domain(_errno, ioas_id, device_id, hwpt_id) \ EXPECT_ERRNO(_errno, _test_cmd_mock_domain(self->fd, ioas_id, \ - device_id, hwpt_id)) + device_id, hwpt_id, NULL)) static int _test_cmd_mock_domain_replace(int fd, __u32 device_id, __u32 pt_id, __u32 *hwpt_id) From patchwork Sat Feb 25 00:27: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: 13151886 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 E86D7C7EE2F for ; Sat, 25 Feb 2023 00:28:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229656AbjBYA2R (ORCPT ); Fri, 24 Feb 2023 19:28:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229607AbjBYA2O (ORCPT ); Fri, 24 Feb 2023 19:28:14 -0500 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2082.outbound.protection.outlook.com [40.107.94.82]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DB4B1BAEB; Fri, 24 Feb 2023 16:28:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=m8OfYasEZUjO/6OpQjFtrFZPfNuJy25NohqrS6nF/1Sm9NWIUCUG1jmd7KQ07cz9gogwp8OAUgVdUGYkzZMbAYZ5bQNmPEk8+FU2ckRdmxOPoebqjW2n3gZJCZRFJcdcD+xTI2owuSBRxYKMjktlaPhbijuZgfK6YjknCITqL5ELviJCdEpGZbVDV9/m83DP63FdK01i0belTUCO37ghVEfur83CH/kh3ML2Q/8b3Gx/qHUeUSAVcty3BxgI3L7nn3+JeDbSWkLGsbJ6ZCWjPhwMB5V4PNtDca2ONYv4rVpFW+3Z1pr8CFyZlq79+qjubVuJBX4rM+dbTRMiJKZj/A== 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=swjuu6Z5wkpRyJTkrod1mEtKh0a9mnBCrV1OjTmR1SA=; b=j9/ZZsu3wz73jwa4v9xkhPSK9kn5QNW3Wd+hWCumsGSN06CsRqISznaHfdXKoVucFE9z52esMEzKbgUHeWGO5SCskxBSy6X0+k4+bj6Tdh7nuvMiYBWGPNQEca5n1l28sQGR22/sc5gtRlKaYwYC2zicMOh3AmT0KSh6VC0JYYQigVmrF9istbdQZLb36nfXLAT2UmtwRPn/fyGkwlXQ10nQtrgIC+xkd1ljZxc98IdosIIycLkDovsvm0ykNctnuoEN8EUNqKn8EL7gEigfIY3EkiUCW8w4zC/uq+BgkmVfL2fu2li9ZYqhFSjKgl2SSvlTh0wSausZbNouQEtZrw== 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=swjuu6Z5wkpRyJTkrod1mEtKh0a9mnBCrV1OjTmR1SA=; b=qvxZvSfQVE8suaAjTpXmDqWB/2Y2cqL4+qZJkrTCDakuGBBSwIIhQc5YzE642M43v/tnh+E3ksG2eHW8+H1suot4J6+mzrvPw/3gZC6+8VPT3rbtCitSSxdsZM9vCpcRN/pwn3aKK7W6g2TxFug3/nOJWQmmSFxZllZIS6QmDzcBTMVOrNTx4NZ9Ngbyq3BaO4D9sq33wPjahWx2Ud3VWUcs9+ncl4mlX81iKyVyW+D3TfDmex0vP+SfTS5zRnhhxsj9Wwz5nNPSOVKG2O2voNkREqHU2CL573Vls6x2omQZkM8rH2512Yhcn4gc+pqFlMw+2BlfFaStU9G0r8RKRA== 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 BL0PR12MB5507.namprd12.prod.outlook.com (2603:10b6:208:1c4::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.24; Sat, 25 Feb 2023 00:28:07 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6134.021; Sat, 25 Feb 2023 00:28:07 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH 14/14] iommufd/selftest: Add a selftest for IOMMU_HWPT_ALLOC Date: Fri, 24 Feb 2023 20:27:59 -0400 Message-Id: <14-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v1-7612f88c19f5+2f21-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR07CA0025.namprd07.prod.outlook.com (2603:10b6:208:1a0::35) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL0PR12MB5507:EE_ X-MS-Office365-Filtering-Correlation-Id: 3e9273bb-a72e-4bf7-fa9f-08db16c727f3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: K+IFwpLN3lATH/pNxLG7+w61zEpOk871Pef+24N5aUMN5Zg0abs0D7/AB/sv4CHKf4LA91eaRjCJlZmgAWtPryjbmdI/pcLMexcgqZuys0Bptkh0HAYryt56cU8KRWBB9bUgJjLyXyslZg4G9Ys/OyVFcADqAzCHBkTeQMoUGMKT6KWk1iAGw9tYDDZ5MgApzT8mCeU8WByMoKDpMX2ZHZPlWmoQQbSGhOWuFYoch48fT78ki1Sd7wb4na/pkWGfU2eRkOOWNuvV45EXpZM8AaLeMliVgnea0nIXEZFFQDtlDD55mRYZR3DBPXYYhgCJVrlFQwreMYNy/JLVEZhrV40PUPAUfR7P02iQ1HznNvwWwCMOaw2WjvO3i4cyGkXs4tEBZBGyabAkXAF/G4zbttiW/2S96wIQKUP6HpP/rPR4U3Fb2FX0k1KBzulIAROvNDOPpbJwYrTzjI7NqEwZ/GnLDD62D4a/9J2ODFR+wD3BVSCaxZa0uUqZ8482/aHODd+GKJTvTNw4GXJEA6RIzba67dYw/WlanRNYyyfMWphdTS/fb6PhblpfBm7B2G+yhkhmIAzme54yu6uEA+314glKK9lEAWOEtVP5R9z/MU1rzS21AW00N1dNTcSDcwMbB97wlkkNJ12xHLt/d2wbhg== 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:(13230025)(4636009)(346002)(136003)(396003)(39860400002)(376002)(366004)(451199018)(26005)(6506007)(186003)(6512007)(6666004)(41300700001)(83380400001)(86362001)(36756003)(38100700002)(2616005)(66476007)(66946007)(66556008)(8676002)(4326008)(2906002)(8936002)(5660300002)(6486002)(54906003)(478600001)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: RTxWt1FHw28ZJcQTVQU4gC9UhzqvYgteNJVpQrMkj4hbWzdohL8vhRLgfGBaC59bHTTvy74FLvE7MgVYkSdJShSw+d0lUk8nCStc0Z0Sh9HwnR4ue8OhD9WCgxQ2gLdh2GQzfHxTNVH+SJ0SYyj6tPTE5BxnNHI22zNauQmvgUA92W42nZ0CtCZ8bGWoxcz7WrrhBCNQdsfALHuGRCidk37sOuT060Jg+F7GxOeyQrSzDNA3niBrHu3eCtEV0lShfi9m12nJAezAZn7mrIBiitLhZj0KFjaP1aKiKvuRgdz8sg8rfSDlIsTp4nXYHXqc8XGOQLtuAqO9Udp8YwLcVjaUNUQzjHLsnv75HbcjaY+J+Gh0VV0E33QtLxNr5xyGtRilVvwtR9BGu5c/uTOuFyULFK++8gkEj21Hdhs1oAv3GiRHsEHoS+HxaMlkMWshJHiIpENx4UNuLtS3eoWQQ7eNTBJUVeYc7k299T4w7TRt744M0VcqPEDueMon0+LUJbOOfhzbZRrm8XBAjv2AgsOdukrrkPSavvh4yzhvyHtD8WqRBCPRv1mH9uqLQSfoZl3qfDLPp9/C735ZTuLzbrScAMP6lh6F7pQWpSbm26Z1a4kDK4ZMPHuQ8nSCeTymb3F65kKm4O3Yi6KmiaORL0wN4dgjeZTABQkHB69pBV+ShNBX/EuTMpSN6feypVxO7P0E9jvPjPBEvy6LbWqKGY8I/YAoMSwpuAqRgbnPQ2N5SQgv+tVsDeQMkYB9aTMdCZWSMfqhtR1pH0dwobeVQkIln/R55Iw5JqyM4/u1cuVelNe4BzL6eZv/2Yyalgb1qIDgGBJAn5W93vENFMZPaxx4dKuRcaJeCmZmLjjR5xfnWYY/SSNvZlTqpjOEIVUdAwLZ7DR1f+G4ej94OuI+1arULJR49xaDHq6eVJE151y+7llawt3ax5y8SCcZ3L+mPQW2HDZDVtZrIg+q3rK6Gku49ymbgmVQriPgsA2cglY7kBbAH+0gUb6yFkCJX4m7bc0vpbk+RN6Jfqmi2f5p7x6ZK/7YCOw3ZqHBNDa96AJ4V54XysYXBLSXctonkk+0s5qRy5L5ZE/cxBOtGeJWAm2nmd+Ytis0XfAuxcPQVNvRZwuNXyHQW1kbQyB0qP09IN75pRqQ6QmOYqOd11I4g8iasJqnbcZ2Be4zO/eFqqz69dygiJCYOhMbTQnS++ThVtjMuuA474X/E/UoOpT6+Q8xwMluytMuj5QBHYIAVXRWtaurtjIzOrWrUkJkGsZ4nraiwB/iLcG9lERtaYuXgCfYA9cIgSWCkONpr7PRmNZau4Q08ArYCMv2LOi8gUmyBpY6BVZ1H4ERjzxeLr8LdexCXvZ4shecRSXNmiRUI9DHkL8HkS/1lQEmLc6SBVyXvqaa+x8KgnoqH5vpHLQUsxuv+XL5f0KpFbeu/GbExON6vqEvKhPSRR8rVUjhpU8es/3aVqwDFpy3XEA3gyT9AvCxJ589EbsFUWvePeN0U44sKCnfu4HoO4DYJDeSIwd0poVWgTgbrXYYzhhUyGlB5tKLnlHa/5rloYHjP/JoX/rB1hta+KD4Rcdm5erVmT7f X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3e9273bb-a72e-4bf7-fa9f-08db16c727f3 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 00:28:02.9203 (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: dTCj+qZFnEi8QkcDXfoN/ZOom6CRAlTQmhQf22f8szfWjyAFQx/MdZCo0KWbzUzb X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL0PR12MB5507 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Test the basic flow. Signed-off-by: Jason Gunthorpe --- tools/testing/selftests/iommu/iommufd.c | 15 +++++++++++++ .../selftests/iommu/iommufd_fail_nth.c | 11 +++++++++- tools/testing/selftests/iommu/iommufd_utils.h | 21 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 7e6fe263e1b62e..65f1847de5a542 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -1307,6 +1307,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 hwpt_id; + uint32_t device_id; + + test_cmd_hwpt_alloc(self->idev_ids[0], self->ioas_id, &hwpt_id); + test_cmd_mock_domain(hwpt_id, &device_id, NULL, NULL); + test_ioctl_destroy(device_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 f2012db43fbc16..5f293825fc37a0 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -582,6 +582,8 @@ TEST_FAIL_NTH(basic_fail_nth, device) uint32_t ioas_id; uint32_t ioas_id2; uint32_t device_id; + uint32_t idev_id; + uint32_t hwpt_id; self->fd = open("/dev/iommu", O_RDWR); if (self->fd == -1) @@ -595,11 +597,18 @@ TEST_FAIL_NTH(basic_fail_nth, device) fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id, NULL, NULL)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &device_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, device_id, ioas_id2, NULL)) return -1; + + if (_test_cmd_mock_domain_replace(self->fd, device_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 807a2421121b51..bc0ca8973e7951 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -96,6 +96,27 @@ static int _test_cmd_mock_domain_replace(int fd, __u32 device_id, __u32 pt_id, EXPECT_ERRNO(_errno, _test_cmd_mock_domain_replace( \ self->fd, device_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) {