From patchwork Wed Mar 8 00:35:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13165014 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 0DA67C6FD1F for ; Wed, 8 Mar 2023 00:36:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229951AbjCHAgP (ORCPT ); Tue, 7 Mar 2023 19:36:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229901AbjCHAgG (ORCPT ); Tue, 7 Mar 2023 19:36:06 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2071.outbound.protection.outlook.com [40.107.93.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23A8B9CBD8; Tue, 7 Mar 2023 16:36:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Z71HYQAlDdpQvGEftDKFgg6zDoTAKjJmBa8OTSjaPRr5nDEUEe//V6dcB/NDdbM3yoGfYN/elB0uKSP8HQ/tDK7cdqoRfhrbLBa+psf+Rv7kvNTBPBVUaivtzXfxM6XFHzqGtsyk5dE1aeilVywP7vQPSs8pXzzJLZ9ijJZcmKE08AjdxnXh8FNBQVhjLkDXC0XjRkoY+aNnxL7vbD/547axG97q6Fv9tKYetOHwUre6s1UpaiHFK/5hqGpIwnKjDwPTHry0swDTez2Ausy+sdUjokKA+yxKQZehtOEDDLDhPuGA4SoMGF0+GPFwGBCuX8cqUmhsTRJkgec2XfZiBQ== 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=+e9gB5bvMQqjMzypSoEUjYdF+vGAcoSKwTmEQPZUlBQ=; b=gjhcQcEHfnzUL3BQ8isR86s3BR1gTX/2L89c65cZphyZM+I6GgS4C0pOw1lRdrAv9++nAJbUlmzUuFlcqDH8IxSnF8LJ84+NeSBEZYJ8h58tScVFvE0Lup6gWeaQziYNvkPQTyyVvh6Evj2yLwu5C0ySmO7DaSLLxDt25aLw2CHJELx8sT5UHc4DNP7o7eVqMeWLtBGSNxNmGvs85ZQQY2g/WAk01E3oKtbTO8Y+gsLI5n+4v8ooSktfLBJ0epgn320WGBEGYkl4KW36ByganiVZjFthx0pVEjLEIdFMtyRpOnP9TSL60+8/ULV/JlYquacfWuMkLT4dNYSYaz9Pqg== 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=+e9gB5bvMQqjMzypSoEUjYdF+vGAcoSKwTmEQPZUlBQ=; b=B5kWvFyD3bD1APGCGGZWfXPk45AdjU6F7BO/VkS7uZrjDtZgJeeFOKdkhw7gJZMt+nvaa+tkTOPo6ymHOKI3cefptez55KoEgHFYeU7UVI+RwLxap0EYaAMwCX5eGWa3/r4HREb6r55QlwLd9ZSzOzE4RnK4znWf2+gHN1oC/jLYVginX60c0WUFFXgeGn/R6P1n0TyJFcxePhQ8oQysNGUlUVedU1Mr7nkzZkEzT/21lqPwY6KvB21AuMt119ziTA9F7y6in82JwYWqsW/Vue0sqnAwyp23Z0vTc9JZHuzkr+wEBT2gkLk3VkjXBJhTq6ctpPSddD0OXaa74Eu3dQ== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:35:59 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:35:59 +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 v2 01/17] iommufd: Move isolated msi enforcement to iommufd_device_bind() Date: Tue, 7 Mar 2023 20:35:40 -0400 Message-Id: <1-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0306.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:10e::9) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: b3b16c03-64fc-4090-1540-08db1f6d15aa X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MgTx6Lfq5sgYPv7MGgGg0Ph5ow+F2GsjDU8wEFJf1FKKP6+vNsb1JS+3peH250h9/aSrZ/HdjFA89dcA+IeCD90ioR+c1As+Ib9bqK8HxoPmWz9GSE20jja4TD2Yct/ClJwdj5WiIMCStQLit9UfkCjoUwPuGwLMxLIYPMMwz19//mBq3gkW1DPnY1IzwVu48I435fWffBNoSdSuBWEnuq4J9mR+8FUzpNqF+JbURJNWhhT2zfYCrWKGqBgDl0ZDWlXa/Q+JdkvzXqG5kSKtyI6m4AaVjnCt45eZdZFqGXT+9YTpwA4EWUyHqfxJ7a1fEg83E3FfsprMZZtrK3Hx/j9Lo+tdaxQDD1/xEgzqNuaVyHMyx4231+JB7mENvWDTG/MrJ3os5rb2NSHC/ATcMivW1bKsa2XFy8inNG0697kumGxxaqm4LHI4kcxIVSXInti0m0M+NPdNlSsEMdWALbcrp8FYx68rBjraKVpV9NWT9OS9uwHTdTLIfR+xeiSmn9z9XrZIVkhElJL8XHCkQIo2l9h9uYDjLgYPQ0aVHBUxe5iyXn0sS5vpqTMBIfPTVTeoSP+Mc1e9VeE2EpEpkEBIn2lJoau+ZNCh5EspgwAGK5uDHoESw3uyzchcdl/HxBT8ns1L7NpUva5YH4XKVgAEqE4LVw7ZzxcDAxlYl+JyRtxbML5f7uNx9RK+5oVu 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Im/x485B7+3Dv55AQihb9/P2jKmo4zZmgh45zBdjrAxvcv2TCGZcltHeFynWEYjaAJMBDejHVsOKyONrPQ7SNzL5CaXH8uG+f7vy1Al86wh1HhlbAPhW+HkCQeG6VwYEUJs30244MjiC7nqQ41koiySymtGcqNMNpdC6q/lTsWpaw1AJfIcgaab7S+DU7ncl4XKnNzdCm7JBBVMH+CnF44InmVEl2pmpx1tRGTtupt2HUbz+TwR1tZowRTEzCgEuSP57TsLryXgx8izU1uQXZjA7cyF3lFmt/uKKJf3l5FgOGFR2VBTYd7ue8fckzq6mC+kxodbtxyUqMtAlwLYkgQydNIbpB1BREpgkRpe7uL3ONuF7W8Ucx5rvQkddvlxlcS7nI0Im2qnFkpUNBz+1lEwY4fDqw/gP8M8P+MNX1BxjSzTMIMWmiuifIW2P4xyF1NbrIB+kl7U82tHVAHmC5Rkc+YyGkApX7x3uTh8OKpRuGJCQcXdEWnN+QAgYpVxKEESvwWDsV4QApLRl1JNQ9dVifeqMzGca3O3MCWa2flSruBzEDbud/nPTUbZRgR+sIuHVaSg6RdojBZVrFDSNTIiUJKloLarJ/EenlO0rry4PbMzeLeRILPyIauO4HTQET9r+1Fg6nrf/zaAH3BLReT7McJLgsJhKl6f7x2xP1izQkWWU/hCs4ai8MbxS4xuSc6FQxRbwEFVVCGa1WzE7VkR4+wKQHqhvpA1IwEIYxXpJVl8cGanHhkLvzcuUl09dHPrEBd1Yf1ZkaNFf8hfzYYVxvEjJysUrZ3Kf/an79k4Fdtvc6I3WdEgaDDTfboo9UzzfvDbP9UGKYMXAmdC4h7oKRquSRwzziT2Il20LVcN/DKgplyq/yecM27uM9cCXzTe+mM23sNUKDPqY2NC0Izf1KNmZe7cKj1+lvjcyLLYAYkkBEqyZgQCSoeiBMtB08S70To6C4LlM0gU/eeaPQr3IUvU8fUzg72fPwcAsjULjmP8kb1dFuM7PFan48Di8JtiFWAtrJQsSDEKqFVGcP6Tr/2vj1q7vkLmS8fcpQPKwXn/aMOUDZuRuelAQ3uGJyPwhECJ/q9swlQo2QYiBFobcZ+bFt7Jce7bauhkejrhvVp8FYbFIzbZOSrKLF42dJOgfg6uzvd+Z4XyTcrBfkVbEqtsdSLdyGMZcvY0zd1WaxZcIfEN9LyRSTgT4x2z4GkMSeLNe3/EeY3zyKA3tiGoKUw//lU8jhWiEioOZoxNg4Gj/KwFXOGjiU4mKM0RVnzRH5KARMdnSbLcgJ3HRU2ROq33liBTCHVIkJXS7DHMbqflCJUp6E85/kY83RSgMiOYLtOwGaz+nh6uT0Oh3QdERPgIvVqIUbbXNmfTe67/6gHRoNJh4joVqxzD2p8g8sjarWKBymWvH/dHAtVMjhWSEaELYg7TPED7CBHjrdzOlVYQ0XH6VRRFzL3J1VJ4YpanwTEsu0aWpkRQgWB1EFzSagQO/+65JxImuB8hEL8apNsCmLxSEvKk8Ny/unL8URBCF6VZVA+OcrgOpQJeEau4tya6so1QHjjulPH3l08ham9vHM1tkRZ2ShChmuD4j X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: b3b16c03-64fc-4090-1540-08db1f6d15aa X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:57.9984 (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: fi+b/QAOlaVe+9VZflE3nOfuDWxftObu8fmq5fKlZTt18/1pGLr+btqRF/KxJgSY X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org With the recent rework this no longer needs to be done at domain attachment time, we know if the device is usable by iommufd when we bind it. The value of msi_device_has_isolated_msi() is not allowed to change while a driver is bound. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe Reviewed-by: Lu Baolu --- 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 Wed Mar 8 00:35:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13165011 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 C6B13C678D5 for ; Wed, 8 Mar 2023 00:36:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229890AbjCHAgE (ORCPT ); Tue, 7 Mar 2023 19:36:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229477AbjCHAgC (ORCPT ); Tue, 7 Mar 2023 19:36:02 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2071.outbound.protection.outlook.com [40.107.93.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AC549CBEB; Tue, 7 Mar 2023 16:36:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=S/3iR3TuTPTIO20ft6JttpcTnOUpwxMHibQbUwl9ZVmODzE17BHxZkHxARWtHUiAnZW8eH4SmAHOYVLcEqp13qAdPmhTdHWvv501ZMVf+pkoh473svvnwdSAehNJqLwGjCDGHwTabCFJaqRAS6fOe9IdSehStZHrJtxCcc3Ydk1vSUuQFNQFPv1uz4l8E6zkKMRTakCncvQxH2lqlzax0e3n9g9rek8QbujwJkBxLDr3pXiZpJ05VvA+UqJllXON2dy48RGq79fenEV4uEo64mgdmP2j0ZYHuU3HOxMc4uSWusqcCfdB43EncmlPKNEQFfXykJdNwXsP90aupfIuGg== 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=I67c8Nx/P56b1cVrMhLp2zEXkkfWcgqtbVYazjmWVeE=; b=TvsT6FZLNc3PAeAQoWTDjZ6ULkxu/iG7krCVkRKCgTEg4m70BzEBqz7zEfhQH9XQ/GZyRZ6TGZfl1cbzlZodfcQ/L4C56tE8i6cp2P9bN+MRgd4MndMGtRQ/Tr5DKWt6QflGJkspXC0dskMQ8vAldrznlz+G4Pybf5C47DcGerXDADWWxvRZsRn/5A3m3GDp/KW/vfC95i+812to05HTWraOg6inHR+jMc1BUt4c9Q2LcaQaeK2gWtom8yNPmmFKfbU8HFarq4sqeH56H5zfwA0vJRZiRylY+gjwGNb1YQ28Mw+h3WAtDNrGpZexE/AxzE0PHbXUxMSjhlf/eGK2uw== 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=I67c8Nx/P56b1cVrMhLp2zEXkkfWcgqtbVYazjmWVeE=; b=kvOXyuofOt2E6FwmkYIXCHBPg6yTHEtEEpQ+1G53HJBRMLF4xfkbhHhhb/oq4gAJxVCEL9RKbRuIXa3eQG6Nbi90FhrNYLpmJI0Gtaq9y45uYdZjBQOX2+B0BMVoC1t7S3kgL1ceiaweIsw79imUr6exD63BmuXWzTNVzxtwoc/pD1QqjeGcQCqv86sWE+H91IJo721czQ8tQf0JvXOiOMMvbJoJIWi4gqhqT3jHT/R9FMBypqDlpmLgvBc3TQHKVOuRQeiuk4GE7EK1npVxxpPeJQyjfAHNqvRHOSJyjtf7G8uSvgW2VRzarFkcgYKnJxh0bAyclUsYwLKAkR3LXg== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:35:57 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:35:57 +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 v2 02/17] iommufd: Add iommufd_group Date: Tue, 7 Mar 2023 20:35:41 -0400 Message-Id: <2-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0247.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:10f::7) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: 2ab70f60-0cbd-415c-6b8e-08db1f6d155b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xeIuCKL0ppEMTTqGklo2gIeRs6Gx953WyejDm57C4ZnWg37V933OFi9CJfqPdZEmRKviFatFHesU8tg78YinPmPxgRi1Hidg8K9M9/rJNImHJJoa58uc+N1L+l6VSto2S/GBdHFtVNctfP0poMBpBI1XJwmD48SQtlSPXtmAT0kKr2qM1k4SJzfySG069wd+5jESnoDUzf7R4BYh3zSPexHlD2NK5PBOyF3OyiTZSo5Hs3xa6nsdHpGiNI8dQ/398v1XQXCpgYzMo8T/6LHzeShF7MWW2TVzx61nygzCKA4pA7jsT/MY0MGm9Ldk0cgqQ8IR3mZVJAzEYxg//5oz+Vv8ugoKAHfsyxxv32qWvyD7WCFV59appe46041riW/wXoCNJLZ8cAZWY8Ek/1C6LJuulQTl6C2WrkAxMMpDWqBh1b2rSkd9LOxy+VWVhx7TfS2gTQZSRRfjoVr98p/HXPF+tQSmVhbJSFw+6KQW4h/BEgpzs5/cSGPMxOLiwRUXGngEga7Cpm7ehBzjAUo3+i/SyA+Aumhmy2oaTyeiwJ7LcDI2ie4XEGjGNRAI2BDWTPu+D42fCHlsMJpBcIAlON6PAxytli2wBuOdwutGSpJgPdoSd8wwWqHMf8txT4RHAcS8aIKIHGZAqpdo0eiMBEZzLgP5yzIsVy8zhWtcbOpYt5tjz4ua/MwygYGH9pXT 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: TWdLG34fiwS4dII5RBcyXSkpNG4NLjtEF6klsvd5fWbNNhPdQqQ9cFJVQBszCdO2mFbMtGV2XosujLuiry9n2DURh57MavGVFXI4nWWSz2fcarMDEVA+kBm327HWc+wCgUEQyahyDXILoD1cScLeUCcmBEfUmudZ/cvUlg03A0TVCXK9dFuyyXxeXj50RKZxHb1SSaZEOa2Chu8U5gPJMGtUvdrQ+nQJUS+xC4xHyFRjI44LJkAEPy7fQvv0ya2zwZ6Xemd9knjt/FezRu912TOd/8O2kY+LleKRVmd5ovZICuU5D7Hcq1S/Oraycp6A4Mjt9PrfQZFZuxG2eFxyF5OdKW3VKImfQ6oOXHLFHoexR1jjCNGe0spVkNGTyOO6l/f0xYOzoLstD0jyniblobs1l/BMdp5xIYOh/n+mFcfFXj0R3u/Sdw4KCykVQurwgwOr5jj7/V3x7ZfSKqoGDogGodTx00VnKPfAfBn358wUC6H3nZl52aoExSJf2OkhBa/cy3DQxZSdKiKKL5rQXYHb9rXKO2DGHJUKBuvryYyTtys7cBxL4Yl3DuI/G2PmMwlU/1vIoOeOs6QzdA1dt3wl6VrMrGD64CggGV1NCdl8hQvwN+0LCpmnQQbLgvqSjG7S8gwMRQfl7v0nzc2jZfYuj8ySMka57xdCsHPz/C54lQ0Qad4scDo0XMLODnd9Bnq9m4fUYeC5UBD1JUTHQXfPNvYr/3QG551nt5+5FWLwOGkbxGtbnmD6bnmuO0fVPJJYWyTd5RzS5bLnno7vznNJfdCZ0VbYvKtMkq+NeXfvjM7+PPgOOKQ1N8jZyRNp+N88ERD4dAHnAR4hbg0pc8pP2+jir1SFqHqfpcEP8OQegZkTIVigp1If/8oo4OiHGyZP3rXJOz1KRGLAjbu8uxLQulrSdtvpdU90NGi5wRNlzFKN9UPueWESdhOzK+ZcK1aHwFVsihwkuESQ5W3es3sCzwQo4sss+B9YVeP3HW1X6mdMfNSIW/iYm56ZOHdLLKGkAqt/CTU7QW63+7mvNGYtQBbOkr48oqoDoyhh6hBxMYfMepChmsudAw26eZmsGplQfWyqRPfKlpxp0J+8P9Wtu/YOR5T15HnoBgaUJuqpb1WtRp8gZGf66fLWJjvZOtJxhAFGnAvZuqfMDpn5HGLye89/YweCnLTJYF4D64/ocuMBCwzoQY591cIbZa/9O816qHEEg4sRtordDlN/h5Dm8tFwbgNkoHxTa+vqDFMUlqnpQEUTNAwIQqf9i6UWWEGlEnXLPbmNI6WGKOY0BxIzb3j8iQQnpsyXiqNJTYnzyGTOk5xlMNAFDLe6FncmiJj5uxrVpssn4+bpf0Wl3fC099IxMblFPxQSJQFSPxhJ/qf/gF111tCMaRLl5WSQqIigCoge3GTyvpZPQypJP7tdiIYj+SF0uKDDDEKP634LCE+lqJRctJkN59pj0GvoWfSqAjm46WG+GTVNUTUhnISORfKdhv7ZJs5u1TRSBQTFVsdaVaHlFcZexfyyzzsyIcXtFpoNG7Wv5wlMA3ytdte8QX3NE5s+xRbxCmrOkz0p04LjqRGLshfo4TtZ668o X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2ab70f60-0cbd-415c-6b8e-08db1f6d155b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:57.5453 (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: 4QB536dL4nw2bzdppX0l4V7YsODIzKuXbba7XtEdYmfIQteJaPQv+keU7Yc56nu4 X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@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 Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 141 +++++++++++++++++++++--- drivers/iommu/iommufd/iommufd_private.h | 9 +- drivers/iommu/iommufd/main.c | 2 + 3 files changed, 135 insertions(+), 17 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 63b65cdfe97f29..71dd9c7bed48f4 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -15,13 +15,121 @@ MODULE_PARM_DESC( "Allow IOMMUFD to bind to devices even if the platform cannot isolate " "the MSI interrupt window. Enabling this is a security weakness."); +static void iommufd_group_release(struct kref *kref) +{ + struct iommufd_group *igroup = + container_of(kref, struct iommufd_group, ref); + + xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), igroup, + NULL, GFP_KERNEL); + iommu_group_put(igroup->group); + kfree(igroup); +} + +static void iommufd_put_group(struct iommufd_group *group) +{ + kref_put(&group->ref, iommufd_group_release); +} + +static bool iommufd_group_try_get(struct iommufd_group *igroup, + struct iommu_group *group) +{ + if (!igroup) + return false; + /* + * group ID's cannot be re-used until the group is put back which does + * not happen if we could get an igroup pointer under the xa_lock. + */ + if (WARN_ON(igroup->group != group)) + return false; + return kref_get_unless_zero(&igroup->ref); +} + +/* + * iommufd needs to store some more data for each iommu_group, we keep a + * parallel xarray indexed by iommu_group id to hold this instead of putting it + * in the core structure. To keep things simple the iommufd_group memory is + * unique within the iommufd_ctx. This makes it easy to check there are no + * memory leaks. + */ +static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, + struct device *dev) +{ + struct iommufd_group *new_igroup; + struct iommufd_group *cur_igroup; + struct iommufd_group *igroup; + struct iommu_group *group; + unsigned int id; + + group = iommu_group_get(dev); + if (!group) + return ERR_PTR(-ENODEV); + + id = iommu_group_id(group); + + xa_lock(&ictx->groups); + igroup = xa_load(&ictx->groups, id); + if (iommufd_group_try_get(igroup, group)) { + xa_unlock(&ictx->groups); + iommu_group_put(group); + return igroup; + } + xa_unlock(&ictx->groups); + + new_igroup = kzalloc(sizeof(*new_igroup), GFP_KERNEL); + if (!new_igroup) { + iommu_group_put(group); + return ERR_PTR(-ENOMEM); + } + + kref_init(&new_igroup->ref); + /* group reference moves into new_igroup */ + new_igroup->group = group; + + /* + * The ictx is not additionally refcounted here becase all objects using + * an igroup must put it before their destroy completes. + */ + new_igroup->ictx = ictx; + + /* + * We dropped the lock so igroup is invalid. NULL is a safe and likely + * value to assume for the xa_cmpxchg algorithm. + */ + cur_igroup = NULL; + xa_lock(&ictx->groups); + while (true) { + igroup = __xa_cmpxchg(&ictx->groups, id, cur_igroup, new_igroup, + GFP_KERNEL); + if (xa_is_err(igroup)) { + xa_unlock(&ictx->groups); + iommufd_put_group(new_igroup); + return ERR_PTR(xa_err(igroup)); + } + + /* new_group was successfully installed */ + if (cur_igroup == igroup) { + xa_unlock(&ictx->groups); + return new_igroup; + } + + /* Check again if the current group is any good */ + if (iommufd_group_try_get(igroup, group)) { + xa_unlock(&ictx->groups); + iommufd_put_group(new_igroup); + return igroup; + } + cur_igroup = igroup; + } +} + void iommufd_device_destroy(struct iommufd_object *obj) { struct iommufd_device *idev = container_of(obj, struct iommufd_device, obj); iommu_device_release_dma_owner(idev->dev); - iommu_group_put(idev->group); + iommufd_put_group(idev->igroup); if (!iommufd_selftest_is_mock_dev(idev->dev)) iommufd_ctx_put(idev->ictx); } @@ -46,7 +154,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, struct device *dev, u32 *id) { struct iommufd_device *idev; - struct iommu_group *group; + struct iommufd_group *igroup; int rc; /* @@ -56,9 +164,9 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, if (!device_iommu_capable(dev, IOMMU_CAP_CACHE_COHERENCY)) return ERR_PTR(-EINVAL); - group = iommu_group_get(dev); - if (!group) - return ERR_PTR(-ENODEV); + igroup = iommufd_get_group(ictx, dev); + if (IS_ERR(igroup)) + return ERR_CAST(igroup); /* * For historical compat with VFIO the insecure interrupt path is @@ -67,7 +175,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, * interrupt outside this iommufd context. */ if (!iommufd_selftest_is_mock_dev(dev) && - !iommu_group_has_isolated_msi(group)) { + !iommu_group_has_isolated_msi(igroup->group)) { if (!allow_unsafe_interrupts) { rc = -EPERM; goto out_group_put; @@ -97,8 +205,8 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY); /* The calling driver is a user until iommufd_device_unbind() */ refcount_inc(&idev->obj.users); - /* group refcount moves into iommufd_device */ - idev->group = group; + /* igroup refcount moves into iommufd_device */ + idev->igroup = igroup; /* * If the caller fails after this success it must call @@ -113,7 +221,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, out_release_owner: iommu_device_release_dma_owner(dev); out_group_put: - iommu_group_put(group); + iommufd_put_group(igroup); return ERR_PTR(rc); } EXPORT_SYMBOL_NS_GPL(iommufd_device_bind, IOMMUFD); @@ -170,14 +278,14 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, } static bool iommufd_hw_pagetable_has_group(struct iommufd_hw_pagetable *hwpt, - struct iommu_group *group) + struct iommufd_group *igroup) { struct iommufd_device *cur_dev; lockdep_assert_held(&hwpt->devices_lock); list_for_each_entry(cur_dev, &hwpt->devices, devices_item) - if (cur_dev->group == group) + if (cur_dev->igroup->group == igroup->group) return true; return false; } @@ -211,7 +319,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, } rc = iopt_table_enforce_group_resv_regions(&hwpt->ioas->iopt, idev->dev, - idev->group, &sw_msi_start); + idev->igroup->group, + &sw_msi_start); if (rc) return rc; @@ -223,8 +332,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, * FIXME: Hack around missing a device-centric iommu api, only attach to * the group once for the first device that is in the group. */ - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) { - rc = iommu_attach_group(hwpt->domain, idev->group); + if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) { + rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; } @@ -237,8 +346,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev) { - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) - iommu_detach_group(hwpt->domain, idev->group); + if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) + iommu_detach_group(hwpt->domain, idev->igroup->group); iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); } diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index d523ef12890e1e..2544f10dae9aef 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -17,6 +17,7 @@ struct iommufd_device; struct iommufd_ctx { struct file *file; struct xarray objects; + struct xarray groups; u8 account_mode; /* Compatibility with VFIO no iommu */ @@ -262,6 +263,12 @@ void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +struct iommufd_group { + struct kref ref; + struct iommufd_ctx *ictx; + struct iommu_group *group; +}; + /* * A iommufd_device object represents the binding relationship between a * consuming driver and the iommufd. These objects are created/destroyed by @@ -270,12 +277,12 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); struct iommufd_device { struct iommufd_object obj; struct iommufd_ctx *ictx; + struct iommufd_group *igroup; struct iommufd_hw_pagetable *hwpt; /* Head at iommufd_hw_pagetable::devices */ struct list_head devices_item; /* always the physical device */ struct device *dev; - struct iommu_group *group; bool enforce_cache_coherency; }; diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 3fbe636c3d8a69..e5ed5dfa91a0b5 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -183,6 +183,7 @@ static int iommufd_fops_open(struct inode *inode, struct file *filp) } xa_init_flags(&ictx->objects, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT); + xa_init(&ictx->groups); ictx->file = filp; filp->private_data = ictx; return 0; @@ -218,6 +219,7 @@ static int iommufd_fops_release(struct inode *inode, struct file *filp) if (WARN_ON(!destroyed)) break; } + WARN_ON(!xa_empty(&ictx->groups)); kfree(ictx); return 0; } From patchwork Wed Mar 8 00:35:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13165019 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 12F4DC6FD1F for ; Wed, 8 Mar 2023 00:36:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229945AbjCHAg0 (ORCPT ); Tue, 7 Mar 2023 19:36:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229591AbjCHAgJ (ORCPT ); Tue, 7 Mar 2023 19:36:09 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2071.outbound.protection.outlook.com [40.107.93.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E326DA02AA; Tue, 7 Mar 2023 16:36:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RlUvTyQDKO/SemSldAsPtlZqD932LgrMQXWCz0kqpy196YpVvJrxbtNJQ3jtyv/8FrC2AjE0UxQkUwyC4LHbMtOcE1dYY3hYZ9lG6HET7b0b92SyZCh0iV6WXtYcBD0lGswxC56VqruahM+uZMLsaxV+89oQyvwFg6zVyb+ulvFJ+eiB7ugUP8CsJCfrCDmmVYCfsYXIpiVcqnCmoxjLbbhAAxrsuVGbJiLync44OTFcJ+WlLnfzmeDrz4Iq0aHrY99Mz/9T/K7UvrZ6tT/ffW/DJWEeeUboskqzQBSeYV69/42bZ+MHu63vsFkVySmGfKj4+MyOxIMSPX6gDk8dZA== 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=hU+KKTz895e9K0iZGakjDj6U186H1wZKr+EMEj6AxeQ=; b=DYlbJwFCLIxHJMI3evcYSrLsYJjJg7MyAiS9JAjYXd2VnTrY5F1LAOjmCZyysxIOQmUnlAo17LUQ6iia1vNipLtv6NG7CX9V5p79Dzqh24e65xYzgOuLNSdtHDfhfzsUwItTXyJ5muXVZzx6IbITKnTK/8rma8lbuggarSsLzQyBPDET1jg4Q12+6oap6WvAg2z4Q9zC7QiBblPkYNkkPBLaoTDW9w1vxaCFRan6t9I6BZwkVVX9/hQ7N2vacbg+yfPAfi78Fpt+5CmaH+t/ybKcxxZ01hHLdKhrwP9xdqLgj1+3rWL+d8F/m9xUiRPK6SqLI4HwDn/ossbHh+QV2Q== 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=hU+KKTz895e9K0iZGakjDj6U186H1wZKr+EMEj6AxeQ=; b=CsK+aEltDgBK9GS85TCZTJ0ZkKz6hUCFZCy7Dkku5KXi+uDHCJ9pLFeFwhIrWPTxa0DKnuBgb5Pk9v+Cmrk+SsJ6vXeRWqCir8gGBbG76ahM3gCjesbaW5jX0qNb7LSkZzmH7qTS+hl+10yn9ACyzshRNDPzGtWDcNDTYpCOSBHDA989w+o5mg2fiofBYP7avsd0QTLnG00VhbjPT0iEIA0IoP/eT8jLdmBa625j9RtOJqwsOMrKLIN4WwZLkCeuLlilqQgyXRNh+3xdkKeI7WFcf2gj1cNNYmt79JXTRhqEDGhuC6n9b+LtuSmZgdgmqsPuwcE8GHaCBeT8aoYiqw== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36:00 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36:00 +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 v2 03/17] iommufd: Replace the hwpt->devices list with iommufd_group Date: Tue, 7 Mar 2023 20:35:42 -0400 Message-Id: <3-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT1P288CA0034.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01::47) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: d2114e55-333f-4cfa-9b3c-08db1f6d15d2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dX3G+/An2Izy8/yRjVv/XCSv+moKGEr5HvzlVZApxJZlViBzVfCVf9rJDvKVbCWssyIe1OLzMxMi2SCykwxVpVSLXS/t8ayOuD5tztVNc+yyDIbM7q5pFhLKQ+xv+J/Rb/prx2TddnBReNRtiZU6A0Svu72avNp9ok/c71L2nKErRFdKiinU3hI4iMeSq1faj9096EnsiLp8ydR0lRUgdpigC7hQJj+6XP+awES6a1FI+S+UGcfIYj64rBTxdMfr/qKsaflOjUi2BTBkFMW0eOv/qMgHjSNxAQzzNpbDjPnq4GybksijHJoe21Uoktk9X9qZFFVE4moUjSQ2YkvMxgrVd7A9of3+OKD2hqKIn6I75Uvp8VQXNZvsCfg0yEJACG0Q361Ox+Bs2lnwI8EHaFHHi/EwMrtDFWaltb6uGoUFe/qnT1Zt+MC/fCArjOCu1t2QdnXv3JythqPVDe7gL7gIVjkQ+90Z9tEZsgZ98uZT9zIWGC14Te7qMuU0lQJzcXjn+0bw8tSGdPEoPpdWoeN91ysZlsAs/s6qlTw1cNFUA6G2jKxWDgM9UhlCS4/ys9Jf0pCDebq759orjl2U2U+fWmY6MiMa90zbVc5OU+/y3/fp9tB+HbqZdxhctA4NmTMyyPE87SS5NlQr7tjK16liCmydoQt1AGx85UYXa5YujJNs7zY3PTV+6d7mkOPu 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(30864003)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: oBsfMAz0pxUnaLb4ywgelJ4x1XKtOUd4m1AAdNxDqKkQFfAzX6lUouFqCBYWb838NoYUczk6NtImv9ggdoUIB2wsVKBYylr6uoOncLJmbOfZiRFlOU2bhd1WbV/o0/J9el8u3Z2A7IWekRdvTBggX3Mz3yZnwBR/qaxsCIWmm9LZ9ZNYI06cVdoUigaDAk82kP/ZA7uMhFx2HrNP1CG3tKqdchdfWqKStF34NTT8J5isJlmWfS5/0HiO5yW7wqtMgBKN+kIGvrWK9QK4DkOeEbQaxGdaqXPbX1LvxnfNmThXtcbW4L3qqnAQQwom5DTA9pQoReMGoGk82RYZB68/iA3DqC7aTMeHWbNOXPftd51+T2+r6+s8Gw3UoqYyt/bPpPj4wJTj8JWr670f4AVEPTUCmT0PLl1N9aIO+NruWvAGoCI9dscbikEd4M/DNsrkT/44kaWNLB7/vcGdSzaN2nmEVlYFqcqYdKZLw/2db/Jn8RsjDipzin+jZWEWuDlMScDhCRQ5b34ZS6fU7/ZOasPBOzoulfDlQhDY7aM60Mk81XSCAQAm2JArWp66eNpfRU/o/oRveEhnnjtrvC4t5nl/YzFMP1iaIdXnamFLtsdrEd8HTVHTixbUXdf2naN9053PnH5f7irIn7JWNcVHN0Fq/x7KOOa0AX+ptzngDAeXuLoe3b/juqRioDfSE96DxSoWCVSTj3OVIISezQ1IYLfIgttvDZsWS2baipCXcw7a49iuZxx5RlFScQ5GGw5LaVCZvJJJ6wunu68IOcoAjHKbej9J375uuJm6ehL5S7ee9zzz544CnKisorxCQEThKSRZhmc7ZL3AzRQ/hIPsR5nG/n3+20T6Dy/ALpbgtAA7DFAPHi8VdTmfvLx/g8zq4ANsR+f/vuTBkR3BePypaG9Coy3GqYcA9bg56LEZoWRNP6XiQFEKto1nbJWDuTL7k/0QSzJ35SdFSNQgDjLTjNn0TS34zPLOhT+AHb4ANWKQ8npKo+GW4yfUjwXw37Qczw+/tNql1WLc0wn4DvJH7xHu2ap9Kq9zPmI+PPfUjQjy8IUbTs2oBOMjCleEuPH28ZF/rlGUZIXkRZG7qjkf34GFznlXFx1+m11lUt52wtpaNW6U7m3bcdN8UcOIcbRR7Y9GFXG8pO+DA1xR7jbtyeHphGig+Qc1ghPxoWiAEYn/ZH8JZdidvxl34ug3P3SfPz2btAgbdRcoyAF7wNBQM5yCbJPq0G9JpU3P3Q0pYjBWwQ2QUFX2A98Hc3ZV5u5nw7QffJTnm1Wgi4O8rRjnXnSSETH+c4g+lzJd2gVCJieTIRKfUuF5s8QYspQju5s7B6LFYEFxiVVeLy3rXLmgyZ+nQfntBPYbtaTXCWl+VCanpkAhuSO5airXuO1pfLp3/cm0+bnxNlQJIHUqu2Cn44/nc7S/vSBQ/d75tGnyreeoyg94Sx+IjQY389wd/q+4I2Mb/ke0iX5zdpvkEwHlLuFGe71LtO7tnvqTuCDkFHuJneMkPFZyyD8KEO56tuuYUBGOxQNqz2A/Ywz3MD3vUDNCmbzc9NO30AMKdlDDABKCwyVeuUiIg4xxtvTHROrv X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d2114e55-333f-4cfa-9b3c-08db1f6d15d2 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:58.3108 (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: QPO4n/5BmnuRe5OQkTTJFHvu6P5ZTKMmSTdjKYvX5at1CrmxmKLaGa7zySID2g+9 X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The devices list was used as a simple way to avoid having per-group information. Now that this seems to be unavoidable, just commit to per-group information fully and remove the devices list from the HWPT. The iommufd_group stores the currently assigned HWPT for the entire group and we can manage the per-device attach/detach with a list in the iommufd_group. For destruction the flow is organized to make the following patches easier, the actual call to iommufd_object_destroy_user() is done at the top of the call chain without holding any locks. The HWPT to be destroyed is returned out from the locked region to make this possible. Later patches create locking that requires this. Signed-off-by: Jason Gunthorpe Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 77 ++++++++++++------------- drivers/iommu/iommufd/hw_pagetable.c | 23 +++----- drivers/iommu/iommufd/iommufd_private.h | 13 ++--- 3 files changed, 50 insertions(+), 63 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 71dd9c7bed48f4..a4bf4c5826ded2 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -20,9 +20,12 @@ static void iommufd_group_release(struct kref *kref) struct iommufd_group *igroup = container_of(kref, struct iommufd_group, ref); + WARN_ON(igroup->hwpt || !list_empty(&igroup->device_list)); + xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), igroup, NULL, GFP_KERNEL); iommu_group_put(igroup->group); + mutex_destroy(&igroup->lock); kfree(igroup); } @@ -83,6 +86,8 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, } kref_init(&new_igroup->ref); + mutex_init(&new_igroup->lock); + INIT_LIST_HEAD(&new_igroup->device_list); /* group reference moves into new_igroup */ new_igroup->group = group; @@ -277,28 +282,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; /* @@ -313,7 +305,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(list_empty(&idev->igroup->device_list)); return -EINVAL; } } @@ -329,26 +321,39 @@ 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 (list_empty(&idev->igroup->device_list)) { rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; + idev->igroup->hwpt = hwpt; } + refcount_inc(&hwpt->obj.users); + list_add_tail(&idev->group_item, &idev->igroup->device_list); 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); + + list_del(&idev->group_item); + if (list_empty(&idev->igroup->device_list)) { iommu_detach_group(hwpt->domain, idev->igroup->group); + idev->igroup->hwpt = NULL; + } iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); + /* Caller must destroy hwpt */ + return hwpt; } static int iommufd_device_do_attach(struct iommufd_device *idev, @@ -356,16 +361,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; } @@ -375,7 +373,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; @@ -402,6 +400,7 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, */ if (rc == -EINVAL) continue; + *pt_id = hwpt->obj.id; goto out_unlock; } @@ -411,6 +410,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); @@ -455,7 +455,7 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) struct iommufd_ioas *ioas = container_of(pt_obj, struct iommufd_ioas, obj); - rc = iommufd_device_auto_get_domain(idev, ioas); + rc = iommufd_device_auto_get_domain(idev, ioas, pt_id); if (rc) goto out_put_pt_obj; break; @@ -466,7 +466,6 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) } refcount_inc(&idev->obj.users); - *pt_id = idev->hwpt->obj.id; rc = 0; out_put_pt_obj: @@ -484,13 +483,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..2ff192777f27d3 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -250,8 +250,6 @@ struct iommufd_hw_pagetable { bool msi_cookie : 1; /* Head at iommufd_ioas::hwpt_list */ struct list_head hwpt_item; - struct mutex devices_lock; - struct list_head devices; }; struct iommufd_hw_pagetable * @@ -259,14 +257,17 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, struct iommufd_device *idev, bool immediate_attach); int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); -void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, - struct iommufd_device *idev); +struct iommufd_hw_pagetable * +iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); struct iommufd_group { struct kref ref; + struct mutex lock; struct iommufd_ctx *ictx; struct iommu_group *group; + struct iommufd_hw_pagetable *hwpt; + struct list_head device_list; }; /* @@ -278,9 +279,7 @@ struct iommufd_device { struct iommufd_object obj; struct iommufd_ctx *ictx; struct iommufd_group *igroup; - struct iommufd_hw_pagetable *hwpt; - /* Head at iommufd_hw_pagetable::devices */ - struct list_head devices_item; + struct list_head group_item; /* always the physical device */ struct device *dev; bool enforce_cache_coherency; From patchwork Wed Mar 8 00:35:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13165015 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 BF566C74A4B for ; Wed, 8 Mar 2023 00:36:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229972AbjCHAgS (ORCPT ); Tue, 7 Mar 2023 19:36:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229918AbjCHAgG (ORCPT ); Tue, 7 Mar 2023 19:36:06 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on20608.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::608]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1DE89CFEE; Tue, 7 Mar 2023 16:36:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=R1I2pqWfkcqnK+iYall3XMYWMwl2sdHxheuWq3st5n3h6wWWRLHwYWnp7DARdGSBsJv3u+HcCAXnfOaBtCRbRILdLiWS8IFFKKLL8Y7r/K/7CbPabwaZDq/Id2kYD7sHEgFcSOrIziXZkJ4NM1mWdVh3Nps6z6QVFdqXGhLhImjIEJkdXMlmzy0jDopH7PDTg42EZswkK3H5rR7ALbSQgga9RjrFc3bJIlPiuRFWxxuUctx2VCwUamc6wKw6Mk+s95IRlwACnfTeXD/Lqk9HfGugbxhXNf9oZnmnSagsZBaiLhKO286/Ts9Wf9YhP1zDo52CmrKPRJgGAcA/jwqwVw== 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=dfnuLljLZxxBwEw0tPqdr24L5v0XUTVZQV21QBqUWZY=; b=UH/nlsNRb+/eszgs5pEgZ40pcVGH1lY/hswQ2qZGogkDPnqI6h+bvVoqtHgjXW6vBlSM9ZpWc6e7S1l/4zdPe1+8QIIaUIgU2ZMkFYfx83bb2RtSvbAvkvRLvHCvc1/LAqxumosZRfTgJwlKo4ALJz2kKK2JbstiGRxejULLXZgVz89eXUz+VtLzGmCyCRYc4e1nJLWq+V6UVjOw9DVBqejS+kpd1lfij6e22Crr+m0oJJwVJU9hPARFw3FIy7wFciC8xS3ESRKEG3Muy560VcG6HQOI9BqhZRMvQSn/++AuUN7szxu3PVh9vBbnrlSPZMAoCqiqFT0p1hsNbdJhqg== 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=dfnuLljLZxxBwEw0tPqdr24L5v0XUTVZQV21QBqUWZY=; b=Fa6aNUcNG73KJSky/vRsBeY4l78Fm+L1gVhzJZLUUUSDnihhto2XP5d6JMlWNAlKEVrTd6zRCG9DqBOZb6nxPwZl+xUEd1Ee/ms8PXZClJGU/DPfmt253GoJ2vlP14vALUkNACl6j4t3FHcVUb2tphysEJGUw66h8RFPKJ2wemUm3gTEWZOiAacUgM5pf1/3uv60ecD4/WEwQikbx+3uuoiEggMYIfSe8zOphsAI4m86jpiocNaq26qXl3H5ZzVb15fKflm9RR4c/6VW650WLBLDxvuNzz1FHfsRaLBp4D9mdHTgFTStLmAceD+NpvpGV+d3/jYeAbwpSzME3FyK3A== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:35:59 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:35:59 +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 v2 04/17] iommu: Export iommu_get_resv_regions() Date: Tue, 7 Mar 2023 20:35:43 -0400 Message-Id: <4-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0306.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:10e::9) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: 73bae30b-6d9d-401c-036b-08db1f6d1597 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CTuyuCr9Qe4xvxDGXJgX4AUj9ndo7qzDjBDpzHwQKom1Gyt3CaMZeEBhiYXjZUaVuXLONATAxSLcAupJD5iJe3XsM/jl0tFSy37SGCMvhTvK6fgjq17HQtBnzwfLk0mIAf0T6GedV+JzU0nmMAvwP/SmnSiAS6wH37XtV8x4QYVeBdaaJStY6Izmj3pODfP6Tw9KNDcM8JJvjEgJxrfq54q0HtKX8bGSfKmDuzlhRJdD0L01V3AHBwu+X+eMbtiamuUKoXiDzLwaWmGTwxx+O8ZfueatkVKWxh+WZByjniq9n5E4DZdzwR8QvDMrJLDXnB3ZcghHrMV89BdxxZqvLFtvQDCDJsiosmrHY0Y0TDXNy4pMd32FwfTiDK6siju5vftlAaBdkPpFzLLDkAGdY2v+mQQqoN9aFv9rbfieG/04C2j9dclK6xWkFyn7iRkvLqdFjJew7RUxH2j4RmivTu6H1m9ORBFy1tuWbcBmhl8QMIuBas4T+Qd7uQ3o/X5beh/Nnczra3YHDXN1EuKwYkAiBxg/Fgh12ZfBY2YkT+YbzoPEzuMwjpr3m4uwcrGrbU/NDIzulumX+mUZ9KII097aRJ/QevCkNgGHlPAID8t/8PunUl1y8KfbCRjFQ8HLxoSO+MSY/Ob5CXK3gcRz75V0jTGNIQe3FRS/EUlmFhnTBuCu+7HPFQcucjqR2rc8 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: oNbNFT4GmjeWHebttao6FVYwdqBObU0+hxwSQG9xJWKMQ5AqBJ7yzZyInaBrbxfCxTATULpawUcxoBm6pmpUuHdPWiHdq1XHRhtfUf0gry65nqwtmVHzoVtbwBOBWoKrijUTsvmfTjq8APeBK5/2l3ARmvkO8ByKlB4aeZAdYJtFNlKHyktXDAbp5Ei4xxB7Y33ZMMnVFjYbg/Rwa+bXCST/404M+QOdxlEV5YbezIc21ttNlmiVHE75B5gDYlQiGZpy0bWI1/Mdyj+yvYX6YCTnS3j5F+dvfUWCRzNQsSHGGF3VoparU/cxAPKGtVcHRRBETwvqURH3qe4W7mQYWN//0GtiPW2bwi6B74hDZzs1BM/oyNaId2aM9y01Fv3IzrKGzahDn9p3JqxIkMV3rckv/MQqOAn/vx/fIME0VBEUbyS6TsLTv6BmwI6yYfajtnT3NzOh8wTdF5zONfxrQcYDsjsGFPHlpYYR26lHUZRJxVI0fR3ugx+1DudJHwvH5G1RIXTb2/PBuQGA8wnrty5gEXlS8LnKee5H4XgWy+V2HSVaegI/FWvVhPKGgcCK0yLVSr7w4ABGnbDQ5wJG0BcagDOEdV6/woqREcfpQQnZUcP2FQITfpxTYELdrKwYdR75D1unTDWmjsekJkEVbEIuAeNsyUl22WCDDuYms/r6IDjGAq9kTZ32iopGWCjUVCqL0DzOJ3b6eUoMyWAEiupt6IR40Kb/jdGKkk+nGKseUDm3gaMId0oodzk84MNKwXGnHpCp4KfoaarzBpJrSLv6MgdJuOEIW66LoJ6mJVGJaT08KXZDJYBxFCpr+1osutIpV63JmhKh1bjWXt82hNcNp3cuyRbOLE6TicVpQYjEQUCDW9XC+/O+4jTIMzdM0brtUHBk5IUzo/8/9O3Kt181msjLK7S4angJlMAiKELzugWYbb0dMIezrdBb0NkAAYmkMNj9XKlPXnXqhZqKd9zTkxdpM+j+SG2D4eCY/BlfKYjkZ0jd5d2WjybwodXp/TBhKb4A1+xwUD77lGM+bhzkKcza58YtB3EOSI81b9SNqdRc5IRkjXMHaZqlavwFFLl296WOjVBgnvHEhBatLXKrVSiigrVHwZdDVbWFunEYFDmmwQMzGWGzZpQnNj3NqiEOG4lhcmDVuFHyEerv6RTj/pp8I54W1EzkZmspPmfrtoKzOnTVZplH4ixAPaX1hroQoRSB07MEJeou6tjpGnww4h7FeF+yFbGxntJT7r3Y3h7y0ss9NkMWlV/IOCJaCmJPoPkMpDzBJ5aUOLylTolFJ6O21VXooB81dzpTk97L1XN4qU9ByCEV07zX59lwsy19lMIpl8BJZ9FRDVA1QJjAd+UT8Dw60Am5xUvHfnXhO8Imq6zSaiRD1PCamvi6X4sf0OySDORq4u2ZHqTFZGSf89jilEseZ3JipCyUIEJcSlFUmHmSIsgKSuhZFXUCHToGJt37cqZKMF/wPmgMY7HCVjstKDpiVwTcZnsI2kExcbA6SjNAnNtiBYNMbxOuFGuVxWd8iljZ5JLdbyl4U1T9B5XiuQQA/qnK2QtW6BZl/10ijnigFc0h2OYP/BVz X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 73bae30b-6d9d-401c-036b-08db1f6d1597 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:57.8890 (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: zXfEGmgiQbsyT5ieu6HD4dBnauSCjZebb6tG/TnNdW81RUBZfQGDtYCjuYMYZ5je X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org iommufd wants to use this in the next patch. For some reason the iommu_put_resv_regions() was already exported. Signed-off-by: Jason Gunthorpe Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian --- drivers/iommu/iommu.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 10db680acaed5a..e57462bb06d9f0 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2639,6 +2639,14 @@ int iommu_set_pgtable_quirks(struct iommu_domain *domain, } EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks); +/** + * iommu_get_resv_regions - get reserved regions + * @dev: device for which to get reserved regions + * @list: reserved region list for device + * + * This returns a list of reserved IOVA regions specific to this device. + * A domain user should not map IOVA to these ranges. + */ void iommu_get_resv_regions(struct device *dev, struct list_head *list) { const struct iommu_ops *ops = dev_iommu_ops(dev); @@ -2646,9 +2654,10 @@ void iommu_get_resv_regions(struct device *dev, struct list_head *list) if (ops->get_resv_regions) ops->get_resv_regions(dev, list); } +EXPORT_SYMBOL_GPL(iommu_get_resv_regions); /** - * iommu_put_resv_regions - release resered regions + * iommu_put_resv_regions - release reserved regions * @dev: device for which to free reserved regions * @list: reserved region list for device * From patchwork Wed Mar 8 00:35:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13165028 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 2D041C6FA99 for ; Wed, 8 Mar 2023 00:36:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229989AbjCHAgq (ORCPT ); Tue, 7 Mar 2023 19:36:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230015AbjCHAgV (ORCPT ); Tue, 7 Mar 2023 19:36:21 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on20608.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::608]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 961ECA1885; Tue, 7 Mar 2023 16:36:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=a6qZ8IWjoRe/N5l6Vko5Y3g9mx/BM2jRqH900FBTRgUjEqtkPAnyxK8F48yjhaq6nOmN4/Kf2dj0DLxyl6OnLyOr+UDumnlOsxaJTMTDmeja0sgbZFGOVu7tpef8sglGzFihqQd50r0Kr0B7QoBu9dBg5UvG4aZVbU0Wqby+QWRXiWhmUCF4YNJPfCrDJMplzedVfT1tVbHOousrMbZCdA55FXA3mOQWdcT0MtH5H0DDkm1aUz8BERsXo6yIzwXwU2/6TMFbsi77klfIrbzrya7MrApi1ROzxbCpCKa1yDBeXaNjAEOUmr6PeTlIrLm0WNmfQ1ogCQ69BAg3Sgo1cw== 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=lCrnwdJ/fchIkiEu4BbW6TZ+vXYWyS87+T9zrb7TGcU=; b=gj+iI5XdU5IdXjH2fYNEb0nsjK5Qth3NRhm1CLEXFjSuNTpB2XoLfBd9K32H4/1AFhY8RSWC7LHfb2tuOCaq3TxBLNHO8xLCVeYYkPLsC8UYOEjS1phKwWg7z41c0yRz+fKcc6yiRMn7c8bldU3wDRj8y4iV3sCvjpDqZ7aazssJk66wF8CWCifQziq6YRhO3jL/zsR8xdNiW9iIV8CxCAClPoQ7GaOSAIzUA94hXjZqtcSRtbXS3apYWfhcmy+R1xSuD34wyd/f2Wq6NaxAExEm6RkXsM75cEkxSv6HxRZdqhUnaNIR/ZNOkRilqBUtE+E32X3Luy/5ZqiwAQALhw== 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=lCrnwdJ/fchIkiEu4BbW6TZ+vXYWyS87+T9zrb7TGcU=; b=E3lCphvjJxtH1xthvDmcP73OtmhSdWbKX4xrPpvwrr/6HL2XDs0Klw9plPO9SupMrIs289f+wS7mXSsDG8pCPHW84gZ39TBdRftXPOCGOaba9ozTbHk8c8MCwgEP8V5IULdYo5+RHcvvoxEr6UfvKzVjf7AcsBNE1Q8VBTvIKjaLICLMoVj3RVi0Zy96e58tD1KgoPdGpdt0Vu+ki0eYGrfqQztIYTghzSCbW80S68KkrC/JznqwgZx3MU5j2wN/LGNjLcb5zgl0fb3yFqyhN33doecoiG1aamhMsrZa+GRfySo1h3LF1bBA5zhkbwV4VdxNtjtESuenP4kFsygU+Q== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:35:58 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:35:58 +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 v2 05/17] iommufd: Keep track of each device's reserved regions instead of groups Date: Tue, 7 Mar 2023 20:35:44 -0400 Message-Id: <5-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4P288CA0075.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01:d0::8) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: bf198648-e08d-4413-6896-08db1f6d157a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: S+lMWK6hB8OJ1w4iHeXkTPwXwhi6Jl/WHkSqwPco21DBqRMl6St4iI5zMZC3ysQ3OT7H0Fq4E0S94Be/IJobTlLkn3xLBYMsgMQWvAU8MOxMYNJOtCbXvDKvwVNCXeReWaDkPwJrYFxa8oUK+otcIIGsI1kjzYYTsF6U1sz5GvOzX+hUktjJVIpSFornpKjG6OGP8zXJuNVohN1fxS5zAp3T8/EZxKeQEcOvLOUuV25JBJgPqFKeCG1rGh8KqE5M5xVlSKqWxrslkVYdiPxJRgcs1IvMVZ6CCGJHqpGCFB8C7jMJODepNHDWKwvVQwhnkV87A1UF3KjgyEk0TQaOHDR1GVsQgkgcdDwthCtA3zVLGw9FcgYUjCUNj3zZHpcy73BKY4fev5ME5MQRCHIM8IUyUUxPvTON4ErTIbFzwW4GoT2O82IZEV0C+0oUtk5zGlTENKXlU7x67Gh5D6uVL31TKdb9bDU2vAgbCL9cXMSeoyDEfGekkii7dKx4C1uy+vrEyp7C9dJJ3Mlp/feIv5Sh9IUrQrXA5pRCrjYB+HCfA7qvim5gHvuxJdHoRQ6SIu7puHIxeSjsX2A2ZuC9AZ2BD9njj7vaVnYEh1/QNv0H4hFpz1B2t20wMPpQbUFyfFjknKMrQgKgjCC7K0xvEcRLLL8yJBdWWNVfniBK2FP/ZtHEQRxz1huYuH7uxmoM 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(66899018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 1f2Da2h+1pV6AszqW6jQV/d0yXNrVvCtYgZhtC7yKn8/IfPCCNuQs2wNMoR1ltc1JuwUPstxgH9cAgeuYCTiU1J/J8DmQKvYacfGhUe2PWowDanhqWefoo+cu/sxw9nssO2bUnKgH1NyJalnd4hAyEK4nPTtaRDwH6m/nlXcyBqFpEEKV/38UQfMB4BpR23ad9Nv9ywd0WR2VY02q2S+sc4xHAyBB/tvGkHBBRzucumIWXyW2R779sLZ7VT48uKRZh1oBQJ6Q+e0lg944FdyeTzzCYOsq6E8lsNOSO23ZeTdwUINC9jAQDjLL0HEiUZVQubELBr+gtRmWCa4wzK9w5bH4BRo5/EDm9uz+X52uKZMgaiHgGGuk19gNUdNvonh9jGBOy3mL8pHoAj2LKUED1VrM4nLwLQB54ycfSikndv3M7Hr/bZ1NmodWxY2B80gCzEbWD7VO2OPcOvsXTD03TuEn1kre0v9/9KA2k4Y6pb2Ha0IzOWmaMOrWz4sqVygR+E/r/q03oRj5stEpk7YyN1GXisybxiLQRZpnr9MZlVPnJUtL/uaKHL1PF8+CroLM+lDyjFB7rEGawUmLRqtFReuqdS4eM/ydYbSgPPv29jen6DoXUOnS91bjRx3F+k7F3nGaIKDZu3r9lAhGHJzMs5vwE6o0FZ7iWCg6KutKG5jGKjUtMcfN+wn5/KuYVKK/WeBO7GedoY17C6loqckExPQ3xqAuDi8edKN2lGgE9v+ZGSCYGGjBeXPOKsS8IrZunBEacrqncGUD20gJk4LzsOeyhZiI2SIZeqUPjClzAqMEYhHwT2XbFDXbL5tpxtjBn5iLvt0VNE1ARWN0eaD/IOdnjSKNPdkYICSpjHClRgvLbogAk59hXkAKlahh1C8oBW226M/tFMTcyxcbxT6nzsTdh1UZUg5R1oWk9aIwk7WpP5RG0osYFpnSbRhES6CvhOsK4Z8TOcA3VlMKlCn1aGgC9sdulRC+YmjGoD+l6gd1iwo4KnDyreCZ2mI0nrrn1vIuXEHJIa/mrlLfJcfWwt70noPxKYk5q96+6As/Ax8OCbizgX4I+1IamMk9HcnYGPtPYa3GOzPf+ggG6xaIC1p3pa7bp7E9celibtG0jwNxbT51s0sjLRUEAniMHAyu2NfgI7FFNYiuMN4ltHoEMRm0HzzAqoCgmXL6I8tGFVpy8MzPM5JyXZTJ5nPjWTODnfO9XWx4gTpgXjlNKgHtWCNcwPFAzNmgJRAUINwgsenFr7K5Ud6MpF7kPPTHR6l/M73l4GlcEpkJKLriATwQFsHkjI8h6It61B85nMBOiBPetPaw9AhL8ql3yvfZNCtp7WfwRy+KOQYgWLWaz825HzimuaqvtLa1k+Lf0escNzRgbvyo2Gk2dI5tgCoFR4YrllWn14BydF9nn8BEOth84/ooSC/W9oNOLIfXEaEgrRQuu2n9JUeZYCzljTRxEvl+G1gefPcm5hOEbcDY8gGWIQ4P2UwLEFBKpnR8ty0SxgFe9UbaP8L4439IDBR1+vIcpNDToVYSD2bTRhAGgX1hr79gJR5k5ZMTnFQIKYDtUpn+oOeB9t8fR+c1DIG9Zhb X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: bf198648-e08d-4413-6896-08db1f6d157a X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:57.7171 (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: QrDz+WBasI8vlVeUAw3OIlWrpMB50Wq7O2flv6tjpsij+igvsI+LV6EkhsP3TOLS X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The driver facing API in the iommu core makes the reserved regions per-device. An algorithm in the core code consolidates the regions of all the devices in a group to return the group view. To allow for devices to be hotplugged into the group iommufd would re-load the entire group's reserved regions for each device, just in case they changed. Further iommufd already has to deal with duplicated/overlapping reserved regions as it must union all the groups together. Thus simplify all of this to just use the device reserved regions interface directly from the iommu driver. Suggested-by: Kevin Tian Signed-off-by: Jason Gunthorpe Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 5 ++--- drivers/iommu/iommufd/io_pagetable.c | 25 ++++++++++--------------- drivers/iommu/iommufd/iommufd_private.h | 7 +++---- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index a4bf4c5826ded2..b546250dd1e226 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -310,9 +310,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, } } - rc = iopt_table_enforce_group_resv_regions(&hwpt->ioas->iopt, idev->dev, - idev->igroup->group, - &sw_msi_start); + rc = iopt_table_enforce_dev_resv_regions( + &hwpt->ioas->iopt, idev->dev, &sw_msi_start); if (rc) return rc; diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c index e0ae72b9e67f86..427f0cc0f07955 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -1162,24 +1162,21 @@ 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) +int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt, + struct device *dev, + phys_addr_t *sw_msi_start) { struct iommu_resv_region *resv; - struct iommu_resv_region *tmp; - LIST_HEAD(group_resv_regions); + LIST_HEAD(resv_regions); unsigned int num_hw_msi = 0; unsigned int num_sw_msi = 0; int rc; down_write(&iopt->iova_rwsem); - rc = iommu_get_group_resv_regions(group, &group_resv_regions); - if (rc) - goto out_unlock; + /* FIXME: drivers allocate memory but there is no failure propogated */ + iommu_get_resv_regions(dev, &resv_regions); - list_for_each_entry(resv, &group_resv_regions, list) { + list_for_each_entry(resv, &resv_regions, list) { if (resv->type == IOMMU_RESV_DIRECT_RELAXABLE) continue; @@ -1191,7 +1188,7 @@ int iopt_table_enforce_group_resv_regions(struct io_pagetable *iopt, } rc = iopt_reserve_iova(iopt, resv->start, - resv->length - 1 + resv->start, device); + resv->length - 1 + resv->start, dev); if (rc) goto out_reserved; } @@ -1206,11 +1203,9 @@ int iopt_table_enforce_group_resv_regions(struct io_pagetable *iopt, goto out_free_resv; out_reserved: - __iopt_remove_reserved_iova(iopt, device); + __iopt_remove_reserved_iova(iopt, dev); out_free_resv: - list_for_each_entry_safe(resv, tmp, &group_resv_regions, list) - kfree(resv); -out_unlock: + iommu_put_resv_regions(dev, &resv_regions); up_write(&iopt->iova_rwsem); return rc; } diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 2ff192777f27d3..22863759c3bfb0 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -76,10 +76,9 @@ int iopt_table_add_domain(struct io_pagetable *iopt, struct iommu_domain *domain); void iopt_table_remove_domain(struct io_pagetable *iopt, struct iommu_domain *domain); -int iopt_table_enforce_group_resv_regions(struct io_pagetable *iopt, - struct device *device, - struct iommu_group *group, - phys_addr_t *sw_msi_start); +int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt, + struct device *dev, + phys_addr_t *sw_msi_start); int iopt_set_allow_iova(struct io_pagetable *iopt, struct rb_root_cached *allowed_iova); int iopt_reserve_iova(struct io_pagetable *iopt, unsigned long start, From patchwork Wed Mar 8 00:35:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13165024 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 9F777C6FA99 for ; Wed, 8 Mar 2023 00:36:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230029AbjCHAgh (ORCPT ); Tue, 7 Mar 2023 19:36:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229970AbjCHAgS (ORCPT ); Tue, 7 Mar 2023 19:36:18 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2071.outbound.protection.outlook.com [40.107.93.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7294DA225E; Tue, 7 Mar 2023 16:36:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=b4Qv/BIkn2j7vhlLD5v6i5SBqy6bm/kkNaU0sj0Qq1bNMT8BTAqfqFCHOiuqRPcLe+rujyKGeSu+oDNdjzH0HQAA41A2Z0TE9nJ3xzosiVp5rCRnBKQqkAWHKQP7W2OGeMGozmWwjYL9iKbsdIQ0EKRAP6JOYv6VIIMjsNebI+8C+aEu6Jp7KhN1QvgchJ0NK2FBAEZaVbH0zxODnCmmWWMTj0rAVpEiGMD6T3NsD7fH82mzNmNUs3mUcW5vZMtNbr+WqnlIOsiAw6qltEMUmFfQulNcDx8vzeRvGWlsd+zyaaNd1UbiKAPz42EVrQ98c9YN3EnZTiHylleYbhQyeQ== 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=zwytGq9WjFtn9115TCY/bO2n76D7AUTe00Juo2oVHRo=; b=EyrpwYngQpW9fWjHpU4BqU7LVkDNzNqBOcVxhYkLo7Je03o3DWVedxvczUmTJ+TjBS+OCHWNc2wX/KFaWDE8vWCwvE9WlWyiVSBBdpQGdltlk0m2LM+WPE2VXA0Pvpu9DnSAVUMir21Uadd8jSWhxxznyf8Eno+bkA3ZxwQ/Z44qgJ7WBBAfydqxW4rxmYPTSQkUoHGH4Ridd9WbWHnx5xYuwplnPuyiOC/Bgou7AlQ3wX2PnwPQfbHknXDi5+RurAXHMtSVdoQ17nuPZ+4EUY27H2oNY5JLp36Ba5Z3Y0CePub3SxgPS+x49RHxxONMdmPMF0XfDbtMtFC8GXA0iQ== 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=zwytGq9WjFtn9115TCY/bO2n76D7AUTe00Juo2oVHRo=; b=QHsw+AXId+T38wirTEZkC5S/xh7eh+Uw5n9atFbPL83qvj4bGcJqIa0QGxCMbayAjWATIaVV0T5f8pPK6blfWd7OYl8Q69mNGSLtYxZO7fkO9WFKmNvi8uyuGh1v2bjuuB/JD/ZaAtcGNFFtY+FaMobeCm/OH50dj9kkX+tCy+a6SmD2cYL/hoNnRKPp8Mok6HmOpjZyXxw3BxSvLCFjbjxhaVhpcu/EGEphUgtzPCKZ5YEgxNSQ43j2F62BQAzZn9IIC7Ij+PMO/84A1NOTqyurrnP6uLxLEJVZh9UeQQA2yhyjId/haeWb6iRaLcVDIDCtrlXRfC6MK5BTWrgEdg== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36: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%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36: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 v2 06/17] iommufd: Use the iommufd_group to avoid duplicate MSI setup Date: Tue, 7 Mar 2023 20:35:45 -0400 Message-Id: <6-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0259.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:10f::14) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: f5606781-df76-4e65-16ad-08db1f6d16cd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: egtrWuDkRK9D1KYrCn91SktnxVCs7eFaSdBKsC7auV+ospNC0PVr8mAkGqR5oBBw6hWTMa5HkhFD+6e532Y0xI9VNYiWHx6w7twBPBOkqslrHy0P4bhMa1rKS2yYPTHMv3Twbu6ax/jzrTsRKpoPNcnI2wxFcPJpqSTEOdQtSS0AHrVarQybe4Mu0nI2QDCO6ioMrFMQ0cguqkZ09REDnWxFhtW6Xj6v82HRHeP7pjCiOjzgH+yhZu4FK0Ty5v0HNLoufgfWpZo3X3QF2crSq4cz2BOywa1obyVLtKNYKGAuy1hC1wmxz1oQj5YUl3UhSUvF6YgwiHw9Eu4cP0kJR0GJAdRlytVnvrGxRFQotFZP37xxGnnYh5sFtSXomvFe2RvD+XOgTgn3xO4lokT0sQ0OMtf9anyl8QnNpa1YJjizxcMgweO4bnl4SD7reooOlo1PFeIIDXAWJ79El5ZWCFb4Su6oj/zOrMdkYIl8EgboWab5r3k8r9OWzgRMhSbutq9nDG4d7/F4tnSz92W+VooPdwVkpb30PfBtMsw6RGquvp0sEVEbrszB95M39egmd7ABJMSQF7PonlgZ8QHT/VIbqI3M5YVFgTN1ip1OnG3RHjvgDQatyS5q7jhS4GZWmOhhNf0Lw8AGOkXRhTSA/lG+5qzAoGr3+C78gLczCHRehT8YYadzS7qLl6C19nG8 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: FeCy6xn+zIIi2qvQDAFM1XVDTf2aFSV79tXOO1oNdkl1wQFGMjZNzDIm2PjLQ1rGi1BqvSHiIL2yWJLhO0tsm42oV6cEB3T5HKuTaPer52hW9wAzx33NoVbQyDVoSk0qJgjjx4y2ARhGWsvIt+j/G1kv/4UTCsV//e+H/p4tYw3SjCwxQvHVhwH4m2wfDwthAaL5iRTjBk2rHX3BOolvkHQ1ily3pKkMXQCfVfUoDdn5zG3TM8U72U+WSd8CK1WQFk/cuqN4uNUcdIWqrHsuRf+O8AHx0c7PVPa4c4pzQE4DP8xFtPrFiScKXptkZx8/MXvsQpi9UVVYVrhmyqHIuOpIUoTnweyscIkSpkn0fT4sQXVMe1bsgBQM6twNJVMlPtgm2CL2yk84KOg31dGzsKYFHRFGRM1sy6I+Jb44W6Elr6IOXQGyFGxDh+d7+5SxrC45+b/464TXewKSkVf6FvJV4pFhnWInhr3e75NhbFtf3ttwVz0bAVFaGKlU1AKqLDDX5shrqbxSU4gPeyxrJfyvSiB6bOUIoIm9UEIxTq0egYYCV0gOWzB/58O7NdwlT0M78RM98BqXXKwoeOk7hgBiq9yPSOTi9DjusbpfsZOE4gT9g2WlbSPKx94SNP5mX3euIgQXPg5WKSMIsgoODfp8A6OIptx0GQUEKOqqkVv+m1HB0D64TMiPE9Ht6QzZWG/3yJhbm7jNW39FUXwM+15Uxxxyv2r80ewRqiL2FoVis9pXg9L/W6y1dSFdv5X6mTTqLqbgfyZuhrqwL9IavcNoKBxp526aP6HS/pHEAOe1n3nLrwHkDIl6dK7wSZrrw8c6A+8RNAqZKHi+KAVAluPWs+arSP7HIQ0lONNZD+w0x9qOybdgJ5Bu/fUoY99TxhExXSeEfzgYrnOj+IWZvp+myQsxrgtUqJPdnn+PVd1k5/RTor5IeJb7jTuGxmp5PUghl7iCJs9T4jIKbcTVdUSI2sa6IUsD3GRpFGRKrOcx+DI9h0YWmFDB/FDyLchGFCIOCeU4AggbE6yRoHNjIZ496yAdrxtQx+9wzwPoXJcr5iLx9/U6KrnjDVnB8Fxuav1YAYeXzkobh3A6IFxyKICZheZmIsfHFPZBTxW85MEh8yt1n5dAfOcMmgb27VVqy5dEan5rDmFsxHD6bm12FTk5SfUUWTcw7hv0clmWi33/i5a3wEUwsuohJ3KvBVovo2XN9iR0Itk/IERGTAfGJY+PmyG+FFP1lqvpUCgsd9eYsuVHhzPEA14tpvc5IAT4wvBMG8wPCM8YAX9b/ivFuPFl0V0cIhA+bdLoOhMC0ocf9DgelIWAazhs0qh54beIZkjn1i7DgpoVvxv/GrIzcu2+PhZPEtYV6ELdyExuuIR0ksuMz5I7EB4oHIguX7WqtAySdKLXwNjA6+mTu0ozxhI6PY+NMyxi2YFGYV24dIsiwRD17JDjfP7vtsyYzU/kBua+X7XjukF/E4kPBrOirY/KubRV7WZh/wrjs9PUreyWL6Zzzb2Lxqp5rDLYvPa+tG1crXGxNaYyMeBCiVnTlTxHT1taOyoxsOwO/e8YyhczE3JE+LgvNaYbJbLQn9lP X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: f5606781-df76-4e65-16ad-08db1f6d16cd X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:59.8898 (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: RuMy4j6ipwRFpMIcF78aEUtmgItWvh2wuCskCtgpiLccq4UEsj87PiytDOuBxqj9 X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This only needs to be done once per group, not once per device. The once per device was a way to make the device list work. Since we are abandoning this we can optimize things a bit. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index b546250dd1e226..4331c7a4e6831f 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -315,16 +315,16 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, if (rc) return rc; - rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); - if (rc) - goto err_unresv; - /* * Only attach to the group once for the first device that is in the * group. All the other devices will follow this attachment. * The user can attach every device individually as well. */ if (list_empty(&idev->igroup->device_list)) { + rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); + if (rc) + goto err_unresv; + rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; From patchwork Wed Mar 8 00:35: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: 13165016 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 845DDC74A44 for ; Wed, 8 Mar 2023 00:36:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230007AbjCHAgU (ORCPT ); Tue, 7 Mar 2023 19:36:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229922AbjCHAgH (ORCPT ); Tue, 7 Mar 2023 19:36:07 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2071.outbound.protection.outlook.com [40.107.93.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5D4D9E318; Tue, 7 Mar 2023 16:36:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=imcsO8yXRpRlYqs7lIvokjubogR3DOWAXAGIVXtngG+g7z1D9H20Ua//YEnBIwdROb3Yl92Cr2wrn4ykWZkIbFlCfeTFygx/zsxX0h4lYBb+DrUvJPnuvtwwTr5rWXqlhnloYyDZkQvYAtsyFX4iLIXW++1ScDjwkT1hkWtimY1Fk1ahYQknB+y7i+DAAkCvqu6yWVpCl6Mg5Mtb34PdJonBWVWEogi709lPmgKMd9J0W0clISnnrs6w80WcTdTvm88ozH2aXjbPq74NDrsqSh8dotE8YpkBh66G5Qee9lHWi1fvYXGHYLcU8GXdnq8A/6eigL/z1ePhM3KJUgrpeA== 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=HmHNNba0KPlAptRRIeLUOw4J3NHbvj/B35ZYFxCwzVE=; b=N+fPzlZUmkah8oeCqs9woj9C1mTa2J4wEXRlDpB/RWaSsAi/2cYJQ33j6/qryKKSP+qNEdTLwPYv2U+DhIsMPfSi8suO5yHZfqQOHRfhQYx3IiCMyawXqC1GuCyBj3Riw+dmjKgOiBVl3OAMrNX08IdTXcahNJ/Cb7DAatNe4kedPj/8okcSfZaU3Ld7bAbr98xg2ksd7S3ju5K7BEydz0Nk4h40/gzNWfOQ1o7+bUVX3FLP49NcXMU4nFAU91IL9FuaDRKwqbxuAHh0NNe2YhmoUDclCxw6YvXFwvU2ySuo3duQ+hYRycgYCpjPCSFpr0XQOiLsNpE4eZt/9sVCMg== 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=HmHNNba0KPlAptRRIeLUOw4J3NHbvj/B35ZYFxCwzVE=; b=PmyiiLGfP3AbwiDYgievMb7oFch5R1eSNORp7hBTGFaVB2lXxo9sTq0jtSYMpuX0bEMynhpn8lmftfI3u+mnlezm1ExwbE9u5vzM5665dLUmHfUl686o8sWwsJkj01bN8kl46Bw0BRm0e9VDQZCq5s1lEGYGHDYgXQcijpr+Y4ePJrXBq2YdmAz78FEPcs6ho7E7V4hT5sKd9ajg97xrd8299IwUthuqoyj89XOf8xZFFr8CH6dsLAgSUPfS1aI2J9ZnNuOidIClZytJ1ILPv23RHcVGu0mxS8/tnxKelOR+sKreDUkQ4OyH3AXPkLULASszq5aMGmaVCa1a6HsbZg== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:35:59 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:35:59 +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 v2 07/17] iommufd: Make sw_msi_start a group global Date: Tue, 7 Mar 2023 20:35:46 -0400 Message-Id: <7-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0308.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:10e::19) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: 96291453-4006-4a5f-ed6a-08db1f6d15c1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ek5/lJ3Wk27GXBvjyYZLBfW2zWGA7o0jbR34NCuJuRDAObKbaF88DptnvqTjH6IoHJuFUtv+UkGOutQB8EwXMbVnC4GHVeXItQ5TZKzOhEKn/oW9zrkzYcGrTRE+mCZAjJvH/rUp3dEZ6uYPpKTcVF0d6vbPkgZ+tJytMvFsTLhpgIrNIgWX5pHnFn5o3HQFQEVa4MtU4yPHI9Ov3MDiFkC//eD+yVkhx0uXBX7rSeBIpqUroc15tc+7tGhKKTWH+dztbRrF57rz4lf1MCgiTxjQH41FjvhjNJVT5IMkBVqLgcBzgu8Vduy0BcMHW8gZJZfAJLevip7tdhkRKLzgwtTnb1ZC+e+DORVc6J26M9q07lrJ04DbMPiR2x58aNtS/BEAP4+OrhAt6LthhyseJnWIFzHXDoogGvaz11EmI6tAFB4t+A6SRjjdr6lp0amOsGD0niWa65ZJ6IU2yb8QtU6jXD1D3LFiagthUgFz/+LS/78aOGGktWpCyIcjCqfzTAw0cK3EBjUa5v12gQ17CH7neMAvYd5dLeoLxLaH2GuZQfAjYBRbEVDiw8mhA/e4M+jDM2cZN/rx15QWZmEO5w7csfDOYb9GI8wtWvN9CYkwafv6qdrwmNiJxLqMuizmQpVjK3+9qEjFSX5d1uhlXZt8K8nNnhUm+JQ/iWxR6qaODDA6n2vLXyTovUzTf+Ss 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: eH/MIN9m2G7eCm4Z1zQdBlEWpxItcf450HaGuNyRjYvrs3ZBN5GQiqyNL2Rulq60BxLXrpblr9ZEfKjypKq08h+evpk0NwzRRyD5jl9dIyTDT5jFkz/oT78XfFPxpRpDQa9gmUixIZft20Rbu5OI3EujPNZPcuyUSa3IGHFiKiZgs21aWpTxOLr/UkJ+Ei15SEKty6ouSn21w/NWOrqve5q+sFdw9yNVemcP6YH3LrTa+JNu73zPu7S8t1UM9ekN8wySIxiVEJg37/DKkSGKgFKgWyLNFyBJtIcdMfGXveQLqMvcJvGkggFE0hqZW8MfXHSnMutJdnEP5wS5Be9daz8liPqsVriAk2cGpLSZMmuRjd3+oPLWaAdlWF1RE0Nq5HuPxIXhRZTA9Y/x1GhSivB2qc22TgjG6DTV6hxCv/Z9c+3sFct9m9qAiu6qj8UwGvGgKbty+RIQ68Lgg/q1Ets31QgxPFCqMhar+Ko/1dfFYMoFjlSV7LIEeU3XZjnl+pJKDeQB+JvqJw+Mf7GMI6u95LS8mJZk6dzXUyjiC9F5SSYJsGqRksyJSzMjNQJvXNQwWwNXMBpDFCXhGgtsDjvtLMNVbf2vyIxMPp3NCikCjKtwOP1O4/LnBo3KIsN2lSeXkmZR7uqys/1WCAWGvtrXSvAapAXsyrCNTHPljKZvX2T9/7P5R8ueLM0/cTxAvxlNI2ihzT0aEdZIgBakmUcPq0gi5UA8j5XDG9v5eZ7hGvHKtqi1ryb4ChBbWBNEZ9xp/iXyL5hh5Nc7gU73mAgcyzSJrkdhX6vVEVcJY5O1Zh7uA0UUJ2krXnrD4dNjN5AUYsAo9xN1wDPLKKfhoke6AP55FTw0PseXvorlE9aXBP0YyBNqfEqgUWDljH2fVJYnaApiVk1rapwiCHO+HYXwE/ajnY/fz59e8zP2SrcKzU7TCkDtEZBtKKnF+btw11gr/HzM+rJofZtON6MkTEMLr0uRjR4PRy3fxaaxHmRRP7In8ixPHYWBfkzdqZzatFzZVtMRfM3/k2Oh5iQEzyEAAP8BCk0it25AGwuq51WvP+u06kFAYlbUH8ccvfapWvW6kughJm/m+vehNj6lGDfw2F/xKzOxaJQrGXBvxm3pOnnmqSmAiza5fkavbZoM/dYWVsYLHG3ATZRB6p9+2YrflhBjaeU16zAqrGn9jhEGxp66eiD4bUG19y2Eo1kGdBt5CLLIAwFd59v2FgL6+N8zWDha0TPSl8mRBLVzcKvgASjPfqJPBfTuBb+tXqZQL48XPFmvLCX2sen8Y2lB5H+/aopy3CIZ56aJPQ91klzcLs8gC79pVHgYlPV+g23n/lJsBZAbkDnfsyy/vWLArSfrAKPv+BlFxE7Ukh28f88HUJn+57p9Lavcvl02GE0+Y+N8VPBGXKzBM9gouxRwgmwMpoLRV9BEQ9EQ4D5Qrr0XAKsL/ZbQRnXR5V9vwjqthZun3zt9DDyFBuXrR89LVhkTvMSnB3zyzBSIatGkAamLmf6Veq7MJRf8t1ZKTaa8HFWvlS5hs3SAhx0WxK+IpbwTetjy+WrXNn3pNMrQr6UvnKmCPIcEefC1Z5ghWZtl X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 96291453-4006-4a5f-ed6a-08db1f6d15c1 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:58.2170 (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: oj1szcx9V7BUgTaU2uoaVhe52+3VLtJsYlLhKC8P2okU1GNLuwOrdSzouW4y7tom X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The sw_msi_start is only set by the ARM drivers and it is always constant. Due to the way vfio/iommufd allow domains to be re-used between devices we have a built in assumption that there is only one value for sw_msi_start and it is global to the system. To make replace simpler where we may not reparse the iommu_get_resv_regions() move the sw_msi_start to the iommufd_group so it is always available once any HWPT has been attached. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 14 +++++++------- drivers/iommu/iommufd/iommufd_private.h | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 4331c7a4e6831f..ba84d314127ba6 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -88,6 +88,7 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, kref_init(&new_igroup->ref); mutex_init(&new_igroup->lock); INIT_LIST_HEAD(&new_igroup->device_list); + new_igroup->sw_msi_start = PHYS_ADDR_MAX; /* group reference moves into new_igroup */ new_igroup->group = group; @@ -249,10 +250,10 @@ void iommufd_device_unbind(struct iommufd_device *idev) } EXPORT_SYMBOL_NS_GPL(iommufd_device_unbind, IOMMUFD); -static int iommufd_device_setup_msi(struct iommufd_device *idev, - struct iommufd_hw_pagetable *hwpt, - phys_addr_t sw_msi_start) +static int iommufd_group_setup_msi(struct iommufd_group *igroup, + struct iommufd_hw_pagetable *hwpt) { + phys_addr_t sw_msi_start = igroup->sw_msi_start; int rc; /* @@ -285,7 +286,6 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev) { - phys_addr_t sw_msi_start = PHYS_ADDR_MAX; int rc; lockdep_assert_held(&idev->igroup->lock); @@ -310,8 +310,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, } } - rc = iopt_table_enforce_dev_resv_regions( - &hwpt->ioas->iopt, idev->dev, &sw_msi_start); + rc = iopt_table_enforce_dev_resv_regions(&hwpt->ioas->iopt, idev->dev, + &idev->igroup->sw_msi_start); if (rc) return rc; @@ -321,7 +321,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, * The user can attach every device individually as well. */ if (list_empty(&idev->igroup->device_list)) { - rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); + rc = iommufd_group_setup_msi(idev->igroup, hwpt); if (rc) goto err_unresv; diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 22863759c3bfb0..7f4936cf537be4 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -267,6 +267,7 @@ struct iommufd_group { struct iommu_group *group; struct iommufd_hw_pagetable *hwpt; struct list_head device_list; + phys_addr_t sw_msi_start; }; /* From patchwork Wed Mar 8 00:35: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: 13165018 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 47D49C742A7 for ; Wed, 8 Mar 2023 00:36:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230034AbjCHAgZ (ORCPT ); Tue, 7 Mar 2023 19:36:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229815AbjCHAgJ (ORCPT ); Tue, 7 Mar 2023 19:36:09 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on20608.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::608]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6216A0B1C; Tue, 7 Mar 2023 16:36:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ajvjv22Y4xzQ9W58t2w1GcikEm5fAED5Ind82jZsl+Dek+vkAQNOMOPObcq4+NQ5+fWTHgAwYAKxo3v/MENcGH8dz3MGMRrELKGdMi7mkQHs27EGI6M+SInOu1L4OV1Wr7rbQzgI4thhVK1Y5h/rBEN2wkAUe+GUxmoYtE15vk76fT4PYR4TSlAAMcbgTV3kqokoRIAvMSCTfwg1n/F8cN0E3gquTCa/ykuuRMpRWIxLLvIP4+2jvbrZjBZ/atShMrj1VyJ/iecFbmNKIwGrMHOQzrZ6NZyaZ1CgTtbyP70dnWnV4FIZQ38Ef6kDoHklhVF53j0kJVUfzdoE+7Cupw== 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=g2uSXyjzMKj8RobrWyTGtXixO/W/4Bi+rTaNKYRG4bU=; b=ShXZs8YN60ZW7ThFhq9A9e246zraCTZAKvb4Q2Ua9AcY+1Q+ffNB2+rMl2pWnX7n4+U/SFKzizcS+/oie3lMtpKwC6YPef9TmCBR26gW84rUrywYKi8tN++Ayzu4RzLSHq5A3CFlHa8SeXwcZhoRhKf/KQWDL88AbcGJxWUPFDMf3ay+5Q8e2qOHj6mUM7hqcOZ4fbWtY8euFOPLqUNscYXb2LOULRZENOR7JWMCR0pfsLB9Td1zAsGbzX0NlsbkpLxmeeGTicnv+79O5t3TFDr1oS8CbfL28O9IjGarGtrBZNDiWOWxBcybDumPtA6Ne9B8tOojHCI/rXn0pNhCpA== 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=g2uSXyjzMKj8RobrWyTGtXixO/W/4Bi+rTaNKYRG4bU=; b=djzi37TKbgzZjt5F+Seli0N2zWBd3oHKIpNd96Uqqx08+FwgCVALHZKUa66hLvRVq0m5S3TcysLFngyboetaurzSdstprJgyELsqrdMearnDlpWzcNoyRD7vjZEpaAcN4rCgsTsUz1oQI7rS/zhiHDXe2WrpZeJwFmLMw+5R7X9Z0ej9LRUIS49VMVHIc1uYvS++ZA1xNyME2Ep37yzfKffTZUsceaWYfMmxXqKc1uBSc7X8qkiQRxAgxt7FP3z7rRZXb/Rt++YDnn1U+9YgY5loxmZ5iFN+w/mTWOQ3p56b5Fb2ImE70cFxTBeqLMVE01i0Xo78WlJb7TNqSLqatA== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36:00 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36:00 +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 v2 08/17] iommufd: Move putting a hwpt to a helper function Date: Tue, 7 Mar 2023 20:35:47 -0400 Message-Id: <8-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT1P288CA0031.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01::44) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: 93957e39-7d8e-4ef1-65bd-08db1f6d1666 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cQ4nuoOHqcRA2b5lE/rFWxxMQCH8Tj6RY+Lejv3Kba8JPbV8O4ppV8UV01+Q/p34NDT4AWcK1X14cNT0dtQU5RBl/Ip3ZYcGlMuXniDjGk0og7u9qyw7uoI4IggmvFDwXbrdjBdF32WbtV2dQtEKkXK/eRt/HqlkkE+dJfHRNsbGIBpu2Y+uX8Uuumz0P9rG10GP5EL6H1tNdGpGhIRQEmSr84v/YwFcGYPr534JV3Alv2x/wg8XMLXcUCHY3s8HMWHqZ8bKyG5dTMzlUGGbyh0JQYzfAGsFENCV4LJG5ZehBQENtpN9nmbA81fTdYEPWFTnf61gEQwsIzUyuTG5IH9I/xuQs5J7BVz9EF/2atN+dRPFrZFHQ86adcSK0pjRaKpCx7UqFMXZbcrnsHoQhz5ckQuOmnhklETaMunF9tAt+eG6U6COKdebRA6qFFRPWB1smcZNmIZYhAgxHg5f/p7YYcQJEf/UjS4M+JlUAzEGsukvVZ896UXF3OypZJnUwY9PHzvRLU5xiY/sdhmO1MVvnfnfVW/FEYQcxkxBrSe18myyTRYbLFLPv+9r6B6KdVtVYZIYITp7OhjtCVsM7E83ZHnzdUa1n0/CzPxkQetD3zIcSkZxo1ehk2CMVXM9QEFYomTpRXo0NZdLO3Krb2V3uP9ATIxazr2KprORyLQ608MSZvy89kCBZD1wu7Sk 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: kx/AUsCcp/VLziSxZHBV4wYKldp4z92DhMmS5BJH4Se2rIiXFA7epjx4bxJePMV6tA7YdWE4xwnkUf2MdjmNs2abKqCQrc86wEo+mO80OBqB4b8W8X7qSkms1DH0PfpUjuY/iC0sh2eZkaTooW7IoENx2+AzbP+z1f5FyKnJDzh5OQpys1qt+JeItmihjKAs/C4p6g8gJonvOp3rPM9xxHvaNEllasLRZf4jYDmpTV9NE9YYS1oX11oVP4X6VrGX/4OflHbamf/XlMnPjlChAPWmPXHr+RvOIiilLEHn9VdvDUVZpRU6ykNPpEr4EOBa0wSxFvIZ5uSYZdsq+wI3YpM9CE9Vu9uDtUozN0uTUw9qqC39SHsRvl9U0rmGapUFN5J5P7Wux220XbyhYFviAhNL6ThNNL4WpIceoign9to5lZEG8lqHcmuy+Ph4baB0D1nixLMorcFBJkGKUiIB+YJSJN55JyLGjPIJkgwfHeOuY3cNbm0a4NSJsLjO8GXS931tYQtw17Y+CoTJOIyJ84Z5tUMGaazBA/BVoJ9zljWeZqcpTDEbTlWDQbA1VXywTyOPhXDK+y+iyjnxTeBuNux1lJ7iANaPpVQjYKKA+jz2UbwecJc7YW8GPzbKPMhWx0HQXjclvv4y3BawEGmjw2WpqqKNkWYOQoASJf5IB/fnxDWwujEZO57PI3ua/TYjpwQcYLgMGTaLNZMfn9hqbHDs4TMK/IVe23NWIsprm3UNoLXqZRJ2wOjKADaHCBfL2EUJuxB5D79+3WDGjDnRKqN7k+DMmXqJerWXU0fxLnjGXB4mQWTlxIhhujOdVzm3QN5bN/l/GD4KOkpSqj7qoXFR+PaNOKyDX7AvoeV+PWa21EAixDghZQMDt0aVlMrjonjXeC10OvUZfiLyZZF8aJkA8j4UI9PeEyOy/d2oj0tSykiwq14uyUewjw6A8r158BfZACw0hCLkHxqVbaH3xA0PGIQP0EI3JY6HBwWk9+U3S2lIGbcDiPk5kQ+TXGwjwa04o50yxKzMI2tpvUvhBA177nFC6O9p9659SKfSoYQav06PLbHZxDUY714opS3cpgwPaKqeupFruunOLF46jIFTsDv5XH9xewSikOson4XvPOGhKLgkz6EPD2JAv40QOk3ozgwX5yx0C4ZmRIr8Aqb4akEAp5hXya0e/HV6Y2aY42cU0V9KZWHpo/QxVZRE/T7YEpNqWQYRmc1tRFGF6YQGjvIyf0SA8UtAzasfEE8APDFzf+YLV7QT0hZrqw+BUyxMDwxcscnsQ6Z9GJMqXm7aeOzvAOqfEOJXEg5ODLGygT/vAtAo7l+gFsqJEHxZbqwy4W0IT924E1t5eSFLMoQdyJ69UuwRjoswPptsLeZLVw+rYpPGoZNaedZszX0n7JrLnZkV7VLeZrlCXSz/xy7NpYY4di8jgwUUwXQ4Jd3LMJNUlsHzIkJ6a+FDODK6AfLKibE7gtTBHvP95ZjDkYK0ivxvaSbJ9tYWQZcz2Xis9LSEjXFLVvjhwg8g02tvxRJEMkxo3vvu8HDXGO+7u2++47aqhPe43orW08yJiOjA3NGPyRuqeHJrGcGziueE X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 93957e39-7d8e-4ef1-65bd-08db1f6d1666 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:59.2168 (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: lLXAaRJpS7XXWXwCZ84+KP7s4I8Y84QbqTUab76LV8yvQN4k84YBE5EIGnHWZizx X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Next patch will need to call this from two places. Signed-off-by: Jason Gunthorpe Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian --- 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 ba84d314127ba6..d6d5c2fcc33889 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -488,11 +488,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 7f4936cf537be4..8bf053f4d4a9ce 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -260,6 +260,16 @@ struct iommufd_hw_pagetable * iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, + struct iommufd_hw_pagetable *hwpt) +{ + lockdep_assert_not_held(&hwpt->ioas->mutex); + if (hwpt->auto_domain) + iommufd_object_destroy_user(ictx, &hwpt->obj); + else + refcount_dec(&hwpt->obj.users); +} + struct iommufd_group { struct kref ref; struct mutex lock; From patchwork Wed Mar 8 00:35: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: 13165026 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 77CFDC678D5 for ; Wed, 8 Mar 2023 00:36:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229937AbjCHAgm (ORCPT ); Tue, 7 Mar 2023 19:36:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229977AbjCHAgT (ORCPT ); Tue, 7 Mar 2023 19:36:19 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2071.outbound.protection.outlook.com [40.107.93.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 13638A6160; Tue, 7 Mar 2023 16:36:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HsIdmkRCe9xCZCh3EbqnHMEhna2ATc3Frpp3iTO4wcIwm2WPXqi5RKsE6aLi/357yinh8nA4ZPAXXIKa9sejx4tvcajT1ngoTKrg4Eiz2V5e3z83VUbXqKyBOK1LtFpy+JBJisHKAISW6mw62iKkgto5AVLCnVERlawI1JKH8ZJ8J4zxTkvjtwOaTtWzfeWRRXGiDRpd7N5lOy9JfovQZgC1wHFy8grlGvSvO20DsVMCThqynkL5QCadnQieIcj+cWhUqAQl4FdR4rzeC0ZEvCL0j0Z70FaQN+QXXFWfJ8pu6KHiCrL3qukqvEj/ucCycYGnbK4vTa8nbeNp/PWwuA== 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=HraoYaiG6bH0MrmthsTdxK0oZRIy6v+ayoUpyWu7zFA=; b=CyHQXOUAB72Xga3PUKtBjKCQ1xRZwR3trNZPfuMWpvsXpaOfKOT21MANF55oCqE2E1sQYl096ARTLu5NklWpwxoweGNNWE0zefje00koM+MQs7Py8q/gv4ObEI2AI/T+BvpB7HKA0GqbPIMP+V/JlyJc3Q86eMXxvp7tmOvUKntfPy1e5yhRROjzD/Mf98TbHa7Y8B9xZQ5KEcyb4RQ/6Y0tnBZVC/yEU55lEDE6eLXCnZ5yFewmcaDqdwswd/QupOCJMje2OMoQQtEFR+NV262JURxz4wh4Y/kYYyvRjk4xdeY5GBSlRtyxYPaK8Pp9PgtKK51bcftKY+5R3aEHjA== 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=HraoYaiG6bH0MrmthsTdxK0oZRIy6v+ayoUpyWu7zFA=; b=GdmRbRWaWNesK/EbamKE0RqCO/Ttdw7o4HcNr/cSibnXAz2SEpc+r9UyMDMz4sQ9p4klDkhdOz9NHTAe0CMvTWzGwxpnQsfyCmuUdCPeRk3YThU2+pJ9XEcUbS5wL4+hVLoFy/Va1IR+p8RtpFdIKF3WBfUeNs7mtHWsto9d6DwtKO7kbA82E4E2vUKEH7VqRA5RPv+gxQeD7BhsTdkrrpxwYCo7Gr7bUAlVSK/athYHd0IoR1TsVrmHOtcBeDWEg1soURzkRt/Hzr+s5NKssGMEEygT0mKDL1Pvyn8CgrP8AP9BqBmKW6Va22H8rkwJvbEyORitinpv/KbUZVqnbA== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36: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%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36: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 v2 09/17] iommufd: Add enforced_cache_coherency to iommufd_hw_pagetable_alloc() Date: Tue, 7 Mar 2023 20:35:48 -0400 Message-Id: <9-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4P288CA0084.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01:d0::18) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: 6680fdf6-4f77-4d61-16ad-08db1f6d16cd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bfI9WYK+uLY69ir74u4FKm16mOIsAu4TFbXyk/993HzApZuGR8m+tiXimH9BGXVMnH3St4J7JQxnKyYJ2fuSCAsQZh0r28GNJ0Tc3GgAW3Y6r4Ab7C+fGdeR2+47UuAMAkmMvSZEyobGPUnvMRVYnnXb3CTxAhX/2TADeTGA0P9/C1DMadHRWKjdqqVt8p9DZMj62/vRDHSwuLzZ5hNOnfOmNkHW2ikEWQJfaIG+4Bcfz+w91beFAPM8wMh2z0KLR9xB/Tb3fRvK0nfdhPEr/EDSU78i40AL5TpecPoF2hAvgS6vRvqcJTSp8xfSU28QZYyoYNm99gFUNzw5ok1+BHJodFY5FyoG+gV1GnKN6iNx8QjbCDc5a3K+xVixrsgI69V7bkhXZNnctI6jHrc98LNKBJMqxmYnj1eztwtRrhQ8GhdBsijltjuU7H21E6/MELWMePpP8Hywd3vZW2DCtPWj6QEyVp13uC7yPlhHXG7h8QQTxbnJjIk/UcL/QGyDOVwTU2N+0zdlcvyYFjUPjDYJqPYCgUQ71YKKt/1P+YqbTPFifSdRbTDYPHLg4wdOSByZs1KHrhHb/ltuqc19rPGxUSt/n/+YTf7rJrI3odxYCCqM6NhQhRBfbRWyVQ9f3hN5mgmkg2KjyiOJxoflPoXbBo0R17wknze7dLTG+OxGHlmu2oWCXH8vBkYCYfp2 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: lftEaMLRTLYHtMkuIt9Kn/UFimuow8wwHwRO9CTYxztP8mgeGek0M7Gzr8M5lWa9TCBnfOLyK89TN4aDAIPulOZ0IObfYUaDRfXWTLuzCiBiXa75bElR8Ws1lBiY9lDJLHXS8mV8u7+I7Qg4HXhJHuH6qEsVjRehmMf0BTtQrEZNKgg82MVtEePgtzME/lTuawpdDUGsut0Xhm22e1x7/2kcmnhMEIyCOHoTgEydtGnA3f0kly1ZnMdB0/fH7h1ZmoikxWfVl2tzbAHpwOdKYpkG0KtAX2U9nhCOuTyOg4kH30DWn61j6eSkvQfJMbrk8fKPDZUZjDGm6aeVzH7l90F+8rp4NRGLC74m8i9U1GLK7CZWyP6+w1NWpYacy+w04nZ07r2nmfTKOYVOJM6Fvdy78OhyOmP6nNazl10v3tqGHu+XCgc+sgnKAEvJgL4bmwbxhnQq0t071+ZchqEIqWOSYWA+M8kUyn4Jx0AE2IBjnAG4w4RRHndPQfe7ASjJba+0NDYzOVgUmrfOCEzBsKDFV9fmM7cCYyo/Jj6MAfdfguRdAWM59DK/pb4Bk7hByhfAf1N6gmKP5S2tNHeXFtyV2Q9jjxYKzLtJFGhFdMKfAYT1g996avdSjG7CvGfMa3ahLK5wExgeK5BVfOJ2tukqXcLgV3etPlELls7q/bmjgmBmncZi2rFPsTZ4GW9hHYDklJj8EhrWVB0IZ4+qMXyRaMOGQ0DA8sPMy538+oCQG3IqDjgomntB0h5V5dTaGoWVa56bIWEJGGgvpqA8YvuxSXV0xMLKXo8BRE9B+QZN47c4GBy78IR0rDnu9PcSyw9HymzGNopxRgKP9/e3dU45ynDMrt9u4hz/4t7tOrp7rIMF0tzs3NPYa2WxI7FI1qugTO9fiCqb/yUxljD5EHJZMBibdEycJ9xrKfUGPs13YY6Sxj9FmujVI/hadU2IxPZr24V5kwnAqc1M0WVO27CWAvEhN1x3VaDoVm6Xl7cpKd1PnDvhGQw6bysM95PijGIKotmOW+EW6KGLJTx8s/Iv3KPeXzzVCQ64T33igaduABUCC3qwWDPM6m9AcuNrT7SkUvFv7tT0jZpMALwrUm01Iy3JL2NoqNOYs6TaL+RpJNVuGUPyQ/1aHfptHEVVwSNEiJ8HGptZffT/g3NU9VNQvRgOnK0E8MM7qdHFxUHuYqvecrIlA+310qeSqIHn5QfkegYhI9UEH/ckMwUU1fp4V2ueXceGK2q3IaV5gUkQU3P59y1LkD9ZiewJYTfnjq/8iuHTn5zSd/fEyvw5vvQ4Ql75M20Ywz9kdgFsTq55IZCAOGh23ZDpyUEmH1OB/dZlrNAlQaWEOWbgmzjgXfygKOwKKJ54QKF1dM4Bi7LjXN6yuGK53ugP5ipWdvjzmhG7WBa729jwnkfKp/Q/nVC6On7yBYeGcOYKRVdCPp7vq+q2wrrOIqN/u4Rtc/iZQKk4cBswjpAM/bsTTkzRBBgpeyvAXj3NYSefXE94ahH3AYS62sO8FUqdb+CUeda3WqEOQ4NqqLl4sL9zGO8kjc5mrccApBe0ah6UGLZo+qAJBqXbmM0ZS5bcvgUr2ZTE X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6680fdf6-4f77-4d61-16ad-08db1f6d16cd X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:59.9055 (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: Khf+QDvErM7qY43GstWSbyDsaK6j4vF4u/HLjTWtPtySi0CWQiaevtIZGSTi1bTE X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Logically the HWPT should have the coherency set properly for the device that it is being created for when it is created. This was happening implicitly if the immediate_attach was set because iommufd_hw_pagetable_attach() does it as the first thing. Do it unconditionally so !immediate_attach works properly. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe Reviewed-by: Lu Baolu --- 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 d6d5c2fcc33889..ddde14d6d1352c 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -293,21 +293,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(list_empty(&idev->igroup->device_list)); - 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; } rc = iopt_table_enforce_dev_resv_regions(&hwpt->ioas->iopt, idev->dev, diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 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 8bf053f4d4a9ce..471a3fdff1e0b6 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -254,6 +254,7 @@ struct iommufd_hw_pagetable { struct iommufd_hw_pagetable * iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, struct iommufd_device *idev, bool immediate_attach); +int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt); int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); struct iommufd_hw_pagetable * From patchwork Wed Mar 8 00:35: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: 13165017 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 9D172C6FA99 for ; Wed, 8 Mar 2023 00:36:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230025AbjCHAgX (ORCPT ); Tue, 7 Mar 2023 19:36:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59374 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229793AbjCHAgI (ORCPT ); Tue, 7 Mar 2023 19:36:08 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on20608.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::608]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3E6A9E322; Tue, 7 Mar 2023 16:36:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FbPYFq5MZry6+TBOUjdfZnrYZvFzrU8Oi2HFAcq63Pboe3hgY8nz5lpR1LN4IhCxMOYJql1Gz1Oj07bas5wughVdf2XtxiKpor6dpO8pHMnGzv5aw+bX7S3NB3W+VVFYWcTI8s89NKyKUFvgH50LMjfM/Dt4WDdjdY2bpRKtP/WDEAgkC5kJvfY4wSOrHZ5wtjmxyfKlEnAVW07F+aFXtb+MPh3Wtq4taI/6F5Bo4s85z4vDoNtVOR8ppd35/KeqE9P/UsDjoni+CZ6VgSukJ2ml3wzTCn8d8SlhCz2AV+ikZaIXtG2ajz0KSu9FErdXBPugePtUaRWb+A7o/LmREQ== 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=jxXDxb7yShZBgJIQpBZ1v+WAP276Yp+uWU6odHNvTGg=; b=hePpelqkRO1cQIpFlGGYyFS8FdnmcPWvPHIjcXx8KuSAnNlxO4pfgyQ8OG1fzwHveZr7I+mFKf7IB0ku4Ffh9Cq08a82FOGnPvNPcCR4bnFExTJJ9OQsxlWESmU5/RDu75NTKmx/lapyvDlVI9x4V+eDGB5AKa+mkK+rDcMy2Ly1hJcEFmID7Q8vrXqf/0LXtHrqypMuRo5bdptew/jC4kzrHss1JVUG2ZVNDIb7ywW5P5Yx724S3iQzDAi04ZF9YsqNpJUHVWQs9Qo63ojd0+dloZ1/9evNCTzQx+Log1Lp8K859VufFz+BaXpnDUE/dWPbJ9VDsO65TPI1o/GP7Q== 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=jxXDxb7yShZBgJIQpBZ1v+WAP276Yp+uWU6odHNvTGg=; b=SuqHOYmjSBUmfojalRX1+bgAGHUX9PRXuOsynYHkp5Lq4tCwzhDiAMLBmbFcFlX/HpmdWAGal+kozK2IabC/qcSqbxkbp8jC/v0l7FqirsF9vHOHEwbv99zNSA0pwkJHe2z6dMUjMjV4ZtR/pszJmkuRYAWKec1nRAci9aPlqc6X0f+8AeXS3uxxw+pt9YhfaMPXeG8gKKHeBERkRiLehgUVKZACgYFTGtcHtZne+P36dCJnqChfOuVfy/ATdhHKVBs6MH1clHhpz3xTG6zUVOfArFoU21ghTwH7ArUstbMskSBgbL/dk0NUHMc0IDZ/4L21fD0PFbIjQQNnvOZX+Q== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36:00 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36:00 +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 v2 10/17] iommufd: Reorganize iommufd_device_attach into iommufd_device_change_pt Date: Tue, 7 Mar 2023 20:35:49 -0400 Message-Id: <10-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT1P288CA0026.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01::39) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: a3c27baa-3b9c-41c5-d6b4-08db1f6d15cd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DRWKEjW1V7kdAEEuodc+X9tfB+Pi3OkNAVf8WUZGpZTP0qgRHkH/TH+ajB/MJjAXpXS65hC1a1g/sm83jlfb+qWj0YbV5FmoBNSP1+UqVyc1vlEzaLpS7JniYc/pUGt20zq/b+vkEkTtazmD8bsNPhxAN88rt9ziWBS049vpJ+KAiP/pcBsSSnE2N9qaDaUf25h6UdenfIG/pCrjAYEFzOIVWGl4P6SMJL1ACYOvYFHhUwBJJllPJdWHODiA59YVY+vKX1LR5kLFMyK9Xf1/suFsnB6+a1Drdpx1+HdQ7RDW9DVN4clUG4v0nlgLCvh4YrbWIlGekJChPLJLLhSthbpqw9XcrzO3Zjozh6hwel724abqXyeJ5gCMOfnY/jedo2VbrZktkl9ROm0aRGrTTXshjAanN0m8Mh5d7We6t1SB5Ch4tkLIbdn7mRt0h58/CCYMbwq2Gs8SGo18WhEa6dvP72lX5YJLqP38388xXuK+LyNSMVSjO7muf9/SH4wOSnIEaBugLDkE1lrwY4ouEq1zAgprPRSyVbLFIjQmsJjSFxuVoGgL/sBDNFdZFJvqXCefId57dnoYB5RRZAaHD0zVnmT0c6DITzQ55FCzkj6HB2kRHqFvzCX4C5VaoLQSWaHgvV+gnpZAFwyIrksopQ== 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: BUOANU3SIba/E0SAAU+M+y0I8uhXrT8HwZ49C3dYKtIC6BWRQXQgZXQ7MdzUbT77xZ5OX4EgSmbk+aHxv0m0lJY35dxkgBgqF9bygdRvvY/q3LKsFAl9PXhBAtQz5Cwrc4Ot2cAyMNkWGXLZ5HcDnhX5t2klsqnhUGPBG7+mFuO7iAGiaNQOuPhrWFt+XhVbrlgZnNBtSFJuzau+alFHsZDa+e2g49s1Lq6zbgTaQsaP42SMcIGvTAqB/FWv4/GEKYYLYFA5Ee/hwSFm9l+zYiN/5t2d103T/ie+xGrN7eKzwBoE+coPVpiAIVIg2KcSIbrJPDrxGNlqE5hFTbAu11YYu6P+5q0MkjW7g8kqrVEYTBNozW2oy5zNyngNEB5KMOKW/OB23DcqdiYeGIAtvem9o1ovhDfNtkgvEkxmgDqm7411ZoWuOom1P99vDdxxG4AF4A8iauJH1056bNdhT7rvgcGUEBdYLZ/3Zk4Nufc2XKDUYCwc/pPvkcZVUIUBdKDbDcJVqAQl3D8KuFdrw2ERDECKaayU5fFEnd9b9KyWBYoaksuRQC1S3hJ76hYrDBTq+fK9IFEDPdPXQEEjWUpnta7HmYRS8TPdGGpadP6Bzvyu0avFGYyrJnwBSlgQO0NzXC9AM+U2BDuXVHcdd2oR54FM3pxn1qhWTlNSO8flbGmp5X+sd9Bit6ndB3g6X9E051aklmhdnJ7wkGKnJqaXtnhUx7/bFEU5dY6bVrE0I2vCnSERfvjH6fvGhrADBrJjdyJO1quJnw8GydByqdmOIg8Txes0Nfj5ltuY2C4eDMKQnOfzIOtfsxPvbvenIUY+BkABCr96RBeKYJJcuopZE/W2prvszOSSNljqFp/AOp5gE6Gp9VO+oTsO+ENlJfduWkvSQ0ZCwq1GoN45MNDYFfrF7ThVS6fSdvqhnHEs7M7JZkCi8BuHUKMqNXsc4xS3QiAplf3FM7PHNE4gr0/4e4yxc+UKC9LNaaf85/J5XpTbwTUdlHijpqzD0Zu2mG1pngUVpTm2Sb3ROoAQon5iVi8+csyq+Y9C2cKYje6AOe471/7F9KEo46aEuI2DTXV5dhpr341n1fyqCGVvkuPa7SUaKp1VwRWMH5tLVlHPiRyF9jpqk7kPU4kKStOqgnFBwV/px20BgHwNBhzNaZidhwzj/CmWBBxmUnmZ3eVn8gDD+swb+zb6WnQOg1KIs/t9BxWGL2p0OwIH0e0gkIRvQrfsUmR9mftFyb1eyvNRXqJvAPvXPw3kOdwljjz6pvrOMPpwQnv2MXxYQYwpkTcTVWY1pZlqbiQCoFYd2+gvf4rHQy5ei+FsD3FweRGW6hLhf+yUrOys1uXYJ0SCC96fN2acpx1it1OrMEXS0q8fMuyXyXG2G1DZ8u9KW6Xdl2XoCAtXTK3tSV88oDhzQg9yZ3Squv0NzPqcL2Hmie9H0UvIROYdhqzmAtzNSK1MayJLK0T7qjPEMtzgdxrcJzUhIeyutElYOdZgCzhL19YYlQF6XjbJ1IfcQWzs9yqUhyNDw6jnL4ZEir8fD8Tor9hc0RAq6tUAoC7tcwSZZk/Aq/9Tkb6c2atk2rq64Euh X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a3c27baa-3b9c-41c5-d6b4-08db1f6d15cd X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:58.2795 (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: Sx1DK2wJWIH1Xh7ZD2JKYB0yZiFVmHaeloH9/+xULH+3BG4rtgVHg0TalEWMOpb1 X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The code flow for first time attaching a PT and replacing a PT is very similar except for the lowest do_attach step. Reorganize this so that the do_attach step is a function pointer. Replace requires destroying the old HWPT once it is replaced. This destruction cannot be done under all the locks that are held in the function pointer, so the signature allows returning a HWPT which will be destroyed by the caller after everything is unlocked. Signed-off-by: Jason Gunthorpe Reviewed-by: Lu Baolu --- drivers/iommu/iommufd/device.c | 130 ++++++++++++++++++++++----------- 1 file changed, 88 insertions(+), 42 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index ddde14d6d1352c..0256e65476f045 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -345,27 +345,36 @@ 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; } +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 @@ -379,52 +388,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)) @@ -435,8 +449,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; } @@ -444,22 +458,54 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) struct iommufd_ioas *ioas = container_of(pt_obj, struct iommufd_ioas, obj); - rc = iommufd_device_auto_get_domain(idev, ioas, pt_id); - if (rc) + destroy_hwpt = iommufd_device_auto_get_domain(idev, ioas, pt_id, + do_attach); + if (IS_ERR(destroy_hwpt)) goto out_put_pt_obj; break; } default: - rc = -EINVAL; + destroy_hwpt = ERR_PTR(-EINVAL); goto out_put_pt_obj; } + iommufd_put_object(pt_obj); - refcount_inc(&idev->obj.users); - rc = 0; + /* This destruction has to be after we unlock everything */ + if (destroy_hwpt) + iommufd_hw_pagetable_put(idev->ictx, destroy_hwpt); + return 0; out_put_pt_obj: iommufd_put_object(pt_obj); - return rc; + return PTR_ERR(destroy_hwpt); +} + +/** + * iommufd_device_attach - Connect a device to an iommu_domain + * @idev: device to attach + * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HW_PAGETABLE + * Output the IOMMUFD_OBJ_HW_PAGETABLE ID + * + * This connects the device to an iommu_domain, either automatically or manually + * selected. Once this completes the device could do DMA. + * + * The caller should return the resulting pt_id back to userspace. + * This function is undone by calling iommufd_device_detach(). + */ +int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) +{ + int rc; + + rc = iommufd_device_change_pt(idev, pt_id, &iommufd_device_do_attach); + if (rc) + return rc; + + /* + * Pairs with iommufd_device_detach() - catches caller bugs attempting + * to destroy a device with an attachment. + */ + refcount_inc(&idev->obj.users); + return 0; } EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, IOMMUFD); From patchwork Wed Mar 8 00:35: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: 13165013 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 DCE71C6FD1F for ; Wed, 8 Mar 2023 00:36:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229927AbjCHAgM (ORCPT ); Tue, 7 Mar 2023 19:36:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229818AbjCHAgF (ORCPT ); Tue, 7 Mar 2023 19:36:05 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2071.outbound.protection.outlook.com [40.107.93.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A6E69CBEB; Tue, 7 Mar 2023 16:36:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LzYsfjgovuireCVq6nc8V4B1zF7qCOwD6erUFiG4xd9QTjpkDi7IesMwM4/I6NhouC70uHhBsrW3r7IEosFkoFSBalm4z+C9zrrr3oUbcbXRmy5YD7xCk6FsY1FfracxN1nU+4jvIt0ZJFZT5vP4yGKPX2/4aOiyGCHG9J0keL1vxUCf1Rsg+vVrAht52n4z6HARhFpYdhyoUOV0QILIteJ64HK9D4YvCN8MMvr9aI+SETHIAeywOGWSQX+o4hzE6gMs9mI0dZi1tlGHGYrU/pz7AGQ3Tq9Ox7UZmyDqm/vXVerNy2qAKtE73wD0gYd01xrfUTCpXeyTvVmB7hNSpw== 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=Je6yzDNdMf08l4gjaNgXgbzq7TVME8YmFB6ue1iVNRE=; b=MIQRSwaBgKsDqnh32J0BXeJEYESt6RjiaVRQ8FUkR1IL+LZng7IBjkabSQW6CxAooIQUrC8+z9ccqJDl/334V3IZSTRafDbwglstmEud7OzCV0jEZAFdkaxM5vLjjkHOuGAMi7dEPR2+eD0JsXpoDRc8M50i50ZvAgyVy62L5FzxBYB3q5twn6Wuag1kJuu9dLDoKpx+GbqV6CUPyl53JeLErxokV+VcskUjNt4xOgKqm66v846f1KxHyc4+OiW4/A6FnznjY1KC+NCJsyIehoD1RXsTt/mmE+fnsKWnm80bDsWhnBVV/3MpY4fUc9mf462pVARCO/oRXUiPyHpyLA== 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=Je6yzDNdMf08l4gjaNgXgbzq7TVME8YmFB6ue1iVNRE=; b=rxuHRtFJExZfBUZKjGpDoWna5G0qnwald6JSfAic3yFPgVTtaXm/B1I1n6y9WQh7dYBxuWX3r2+17VOxt8sOuRkxIIHis/FA46djxyckXYPYP/CzC3QQe85HwmlG33V8Dx6r0q2BkhI4cfpH9DlRpA/BHCUm0qYsUQV5NOQBxxSRQgLHglb2D5a3EWN4IIwoSS98Txzz/Mn0UYgsL3e6CqHGZlnNAH8PZiOKL4qhDjHKHTQxIKK9rTi1DxGg3RGFvDMtH/vfRYdQfZpZnjuHa/OAagy/0hnGrJfMmEab4+t1mu8QTIZl8mJjigqk+17hgTDcKO3Att8T2RIa4/a48g== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:35:58 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:35:58 +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 v2 11/17] iommu: Introduce a new iommu_group_replace_domain() API Date: Tue, 7 Mar 2023 20:35:50 -0400 Message-Id: <11-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0295.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:10e::6) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: d6d12519-2c64-4d1b-df8a-08db1f6d158f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +e2zEnnXYo5seRpvYwzGVuiJryeYcRCbf+SH0vQKPWJFXiE7cPUVLWDy/ZvwxmjzlZQaMKR9kIUs5QzLPwTw2NML4WmLKF3rvZMam3HPcOiFTgQtG+hUWzU9jC7IxUZWq9EGRbw/cXPMVkCVfvUMy1gL9i0n/jPhGkMVXnw1cmKNd+KBSZ4HXlzliF3Y6MJgyI4PgYDI1Uil2tA+DZ/AU/nyMBBxXqoL5cy+03CCH49LW5Q3S6iv8vjrPYS/horLezDWM/B2uHoCh2Bt0jimJWdVeNsEEoq3URBtNBN/Xx8W8V8VytJBzHGnuboCyDDYAYgc+tpHCup+LRUvOCVQ7Q/RR2yzCnsYogJoi16VSPI8SjzSWhNxpykQstMXD5CjsaqBAjzV8LNE+apkhp6oU1ewDGYvQgqxTs/9428ksRJ0OmRD941f/AGsttJSmh7IrG4Vc5X+nPfX8Qvh0qy2W7dNrYC9c/u5Pc95+yy4pD2be3xp1xCJU3XgrKJ9p9Oh5deHiKa6ALzGPSDqJqxUDEspPFk6yfWie9CIn9nK0rBxazYwKl2bIKebbgIqO4QAvoUKR2bDPlRPVtjIc06SgCOf1T1I2KrMXmaslVjXc4k4WMI5g7L+80L/DGxnQEXe6+248O6RF3VBg53vF9SvFQ== 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: gvTchg4OuiHLxryGqQSr8yv0i/WLm81pqNUuwoqueUgdAiW02Vz1V2ba2AUo+OV+b1eCrLzWF7Uv+AIOVMjXopi2zNXba0t7+AKnqV8sFnfn334jmZ7PVaiZ5/BRCNcf/xNbqi1rt//EFtTz4fZB/cgOaHRadHHaZ7iSfpT7TgDTN5f2ILqNaCVNbMoiKx5CehoQ5On/+XxLV7sI9QS8aj397ppj9ffYkEFS6fPmy07VmEdo/OP9bMGZTV5OUco1IA3TS/VmMPUh6rbQ1WL+ZeAVwlE7OKULijR0rkgxZYZLPgBzGqbwAMUN/8nPc0OfCuACbPR7SDE8hrW4+i/tdoJIiI2jTRa9/Dbg1ktrv9SRnXPIGWZ4KaKh7m5LT71tY0cCRxGNMkaLvCsQK/NepvHNYG+eB2ai663WiYDoDBUgxRdeQqeMbM3KDlXTILp7MBLaFTG3i5xxj3nXVpwuPNQCneUpLPJFrnWEEz+3QPe22nMRHITuIDH7kOkCNgTODNGX/4k/ieLJdSRAdYFX9HmB5utVT5CUvl3XCpz/1msXef8Kouyikjx7qy4iuxVP8KBRAeydMvcaMFYCUpMikr8eqR4f2C4otHntykDLOcIzIQ/wT8dY3Yq21zFa/JWZNYQeK8qJF8+Bxl7tH1P+tDFEAdP6+R6kMWB6yDZN96Br/trkl0gFfKspG1+ON5tzFmCU9ymXRBHh0k5BEzVVlHeGsj7CMml+ed7+e8lqu/EpxIZiGLizPoUA1eyC9slIqr2f3jX67bUPYFtrzjyfTpOU6nbLC5uXlxviiUtoKjczbfu4cDNMVoM5B8eysAOpNpUhg70iVI8d4OlGCxqUqOqNaDdf77PaAgH/98IuUNe3BcsSo8H2o1EeWTJUUTavyTptUWis+BQHGYkx2ktytaY1dqv1baQnWkGzqC3256IUz94IX+naWGmhM818aLsqg+Mt/7pI/bf5/Gi2aEIKp/oOnzcSbVK2OY+D95K5MoqnaC4E3qKX6+xCL4PVRrdghZH3h3MBdOoMl7bozEANiA8W9VH3nchyRV078J9Qc0aWGeJ41dI7iVfsMap16NFKG4/KmoYCWU0yr4Md6UC8rLN+m7C9NoMWvsu+dnjrfqBR5QKDaI/lFDE/EzL9ZmHnzGDsCWtueL/Asx7tv9eifiYg5a4tKUsUKki6xniSjFkzizotDegasAEoJ9tHbGsXyPahITG7tPhCZIW6DoIVBgC89JU3UJqVkJM0yTASkTrF48rh5LhCd1Ltz+6NNyITDeNaZe/FviIxn/tF21uiY2mBJ5EQhEVRHaVNJjhnoIqSyuV/vDst1SpFUf72VBNYa+Zqz/AZx0WndCYwE0LqbILzFKRY1AkrKY7FEqr10O9K+I2/qL5/g2DQsxYTQcGpj8dklym5u28CNef5Oh1uU2/3tz5Vtz3Xn4t4W1vwdqOJQVvqRSzcM53uFX3Nd3kiglZeniK3E0OMHfxBraT+CUHud2G1JRUtQiToNNrQU5Z+U5shPMriTWcW6muo9h0+xstUqKaEyC1QgWWL1lAqyvY42TWj7fRRunzCknnfWsmN2xzJsclSXQ8EzWWcPeRk X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d6d12519-2c64-4d1b-df8a-08db1f6d158f X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:57.8265 (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: bUx8LTfCsPGXSvbGbCy0K1E5Dcz3n4CMF2tvB0bIFJgEQXhQt75LfayJXlZ0IopW X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Nicolin Chen qemu has a need to replace the translations associated with a domain when the guest does large-scale operations like switching between an IDENTITY domain and, say, dma-iommu.c. Currently, it does this by replacing all the mappings in a single domain, but this is very inefficient and means that domains have to be per-device rather than per-translation. Provide a high-level API to allow replacements of one domain with another. This is similar to a detach/attach cycle except it doesn't force the group to go to the blocking domain in-between. By removing this forced blocking domain the iommu driver has the opportunity to implement a non-disruptive replacement of the domain to the greatest extent its hardware allows. This allows the qemu emulation of the vIOMMU to be more complete, as real hardware often has a non-distruptive replacement capability. It could be possible to address this by simply removing the protection from the iommu_attach_group(), but it is not so clear if that is safe for the few users. Thus, add a new API to serve this new purpose. All drivers are already required to support changing between active UNMANAGED domains when using their attach_dev ops. This API is expected to be used only by IOMMUFD, so add to the iommu-priv header and mark it as IOMMUFD_INTERNAL. Suggested-by: Jason Gunthorpe Reviewed-by: Kevin Tian Signed-off-by: Nicolin Chen Signed-off-by: Jason Gunthorpe Reviewed-by: Lu Baolu --- 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 e57462bb06d9f0..0eb54efe01ce92 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 Wed Mar 8 00:35: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: 13165021 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 E210AC6FA99 for ; Wed, 8 Mar 2023 00:36:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229628AbjCHAga (ORCPT ); Tue, 7 Mar 2023 19:36:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229933AbjCHAgM (ORCPT ); Tue, 7 Mar 2023 19:36:12 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2071.outbound.protection.outlook.com [40.107.93.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33985A0F0A; Tue, 7 Mar 2023 16:36:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=X+k4BkaQ/tFjBJnpxW8/lfkjA9yOPv519S2p2AkFQ0lyy3Pb0IjJki987J/9hbOu4Tk+btDtDMgdmkeImFm5wMfoT1cc+8I2geEarm72EdDeIOy4GfuwOrSL+2ZCSpcRvDeipa2XaPu0P0NfK3dUUPhlQP1g6PNJo1g+LUjBIHI6mGjnKmdN+LvCzqHnEuCgHzyxUDsJ+d75IHd1zb0/8SGKj3wcmtFxTFZNitiVNpy1pMZrtfMMyDZ7t44OcpasTlZnVz6hfZZbK1Rhnkd6Ar/QzwxmLa49xTk3Nt+u98i68Kw/rcJNxWYWwqyrCk8SHvopZAoieatabv+iyrLEiA== 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=VdVNZFAFb4WmTzu/R0rG9qtc19on6HWKswpFF7vtbo8=; b=fSvEwPNI6ZMnzfm56yj5ZNBm7hp/n5T6Z03WfS+J8a4j/55qdI+aaY/jM56wqbZBfKLzF4FF8dPI/IQYZnpPXPrz9qNfx08UeU9ebfE8wiajs/1EmEuuFq4FNLK2oL/jfHbbwH9IPw9+dhM0ChBh68FXWGVYxKcHd05bZqvNrlbgJo2MRE56+qjEPea8C8lUlnUfLevYP9gvSmBuPX+mQX/52FGk+7pt+zkddB1w3yXx1tYaW1hXb1vBeCBAEPVGUER0bYOCPuaH4vnaUFbrvoTchDvJ4NikZbWyWzcmeWhEkrD0MwO084fHMFm8VTb6NUjhW5wh5q4rcAIHU69t8Q== 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=VdVNZFAFb4WmTzu/R0rG9qtc19on6HWKswpFF7vtbo8=; b=Sy5uUC5ahXiwqacI52/C+Z1LUafgoWGa3EQsk5xYNmPTFUpPQLm8zjYWTVwYiHDarR0e/ePj3AQYzwqY0b4WIoXy02aQDOLo3v6QjZF/GXAhRu8r0VD8NmLnAT3t66Bq76jJej9TUFGLOSzRlRw63SbP7eYE/0hxX9I1BtZ3X4bW3h0XpMDDDSIxRq0yQd9o9Lu6j7R3sZRUFnKUY5OlVR92ezqOd5DaU2t3nqhhaOjvdbwd5gBk5mLx/qz3F2ub5n8wOsbW8wDqOfQ+G+CClr/sCXCNaosS6EE0LnV5NFsTGHE9zaqOYh1KEjoFra00Q/3dY2rDjhUGscnKV6U2PA== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36: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%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36: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 v2 12/17] iommufd: Add iommufd_device_replace() Date: Tue, 7 Mar 2023 20:35:51 -0400 Message-Id: <12-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4P288CA0082.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01:d0::12) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: 468e701b-b461-455b-1cb1-08db1f6d168f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6N0AmhazAySZsEo8CKuXZgfceG8q/KhD1X3ywb4kFMTtdntOaiY+K0qe09H4zJ2KNbfaz26WkgxXVpszVH+IEEWP3cjcjsvUXVw6JJCTQmjj/RuSvUcuaTNCzqLyR2FcAqZnCDXcnsN+14PVuHM2O4LaG+dPUknevsib4r5FvcteYqVLwQshM0+ZuQiRdUZFJdMCiJKxGLX8cSxRw8LnxEtVkZUVxEAbyt5ioLFL/HGPyM0LelvMe26Vn7XZk8gX3BiFxmNeI6ccVjUOyCfBSGNAcs4U8zSU2dpJ24gTebaTe6KLldVftegNvfcR2QsZn9nb3m6WOIrO/FwdQZ02kn6w0TrLsQAXAvLTkywCHeEkAynOhIVHmELZN8wALZBURsf9b4gnKlSJ4Rnu2Ae35tPnLiMnXzp1I2FRkGyXPCzncGCvaunysZi5dvmQLFyTKaQQwvsxVdSkUcKMARxt+GLkihJSq6vBUxhL1aSaJaBBpdeegWuQudjELqAU4tzp8VWFiG4KKmkia4Ya1v0rwXz2Eq9QBWshOsDlPtomohf8VgO+dGvqgbsIPldKmwk/Mq26TZ40s5WQ9Vw8kJNLERJMypei18JWwrbZw814zG+fHNPSYH9f1pVH7OsVzcQENrcnc67Zd8HD22tYBRSkcQ== 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: SrzMdrZFiZa0Kvq5mLECxRKuCzVsrdxBksMXsl8bmTT/GGl8b1YLYWzg9ytdM1Iis2qE4NM8pHRZz7rfDYSZtnpWLHW2qB800busKYFDPK/YlWdQo6Ghrji4jD2T62x0X07n1+6+9EKkzMVsQYnnQ73r8gbzE/PSi8yzjcz+q80A3KVVTgXG2ZD+AzY5TjQjxtYH9vIthUQbIgijPmZxW/xYrVTp4Q8CUCp79qvzsvdPqyOgWyuRYG+P9LBPejKynVoQYrhJWB6bTUVQ+nbeW1cYi/JVWAkf6EknRByOUHCB8UOGyu7VNZ+Ibe/XWAyxErHa+wuPbeOCZ7SxaEf0wBExYi1GqmjCVak22CdfHR5xMyr+YamQXWIDST9pokITv0RGey0Gu6o9b+DbuBBI5pDDF02F+LB/l/X3qbn88x+tS/GcED5/fOMikMXJGKOsM8qAKPyLGlJxf5D4VjBQdPOrcfp4eEgWnyW218sdf5gOimMeoYlqBCGWnjlf6G904poewRJbc2RaquWWb55z/p50+TH1KBTCK1bcHlGETGINgUELYVoJCIKK/EyzUPMgAWaB6+8ruTxZLg/PP7bLm2bkwpMP7ZazEBKZrJGyUfr+imRIcRZm/Ed8gX50VIRaX1NWPjlffmhi0j6uPn9YKUIUnqUIzRaL69cB8129qY6EMjPMaDq0u+qnTNufB4yJgKQzZ4xZ+EpLIwBpvkZ0FS3+R3JRWL5TWiBKc2uwqWj05FHK+JCLTlppfy4TLYqsrCwsVgwceb+E7bsewF12AwlGREU7CjiEWlPAV2GKQOLsG/dH1eIAgJlNeBQRL8GFWN0Yq9PxHgLcPOBdvx65TCoHmoSAroNzl2XwcK9K7a0I2Jt5ZUymzFrGts25H9wuYZZxL/jxL3qY/L7bXNYGOxfImHA0b/rZ6vXPfWd9kPMrfqVj5cJd0SPS4yhoijRt3Ql3cuT3rvS6hYjyi0QbE5OxZRYL4bGA3Od96yieGB8fSJDaqq+T64SsBDWc8K1m58LFxBGPF6to4CcHZTHKC2zGywsmm2FZ2FlyKEXyJ/S7COLd+m5ds2vxhZAB1/cCM2EhJwX5Jf+d+v5x0ujMTgGEgoSCCuDyl2m9LDUiQu2jT2Tm8vdrALG37Z1iU6HNfpEzDCECBpcocEDNK4T0o4GSff3zDJfEW3tdkr7M4m0r2qvpG1gvDBd57dTbnJ22XE2uOygEYiK7H6llVzkcTntBdAUU3f04e/Zq4PCBZnFZBt+A/tB3l4ebt0bkp3Oo6WsotlIMDp3JQCs6LfoPAfyxWoxd8jxZEvKikZNkPS0AcICjSyEYHyK6/fEbmbKewRsuzwfNu6KBF8so4RMImwqrPE7AZKpnQqWkpD5+3R0nPfrZynZWIuZqlrt0Ozmv8KWxjGsiLWNooqv3zuq+GHnYa0u1Ix+vzXmLTd6cPBTXqyV/451mfuBJWBh2j/qlc86YXUP+FS6ZkVn0p09wKQTEV3UIM8EL2WnNQRsB961BdW5q+XmkvYVzVxf6XT1Cs+CvSNe78I3Yzf6nD24Qemo1Rhetn1wiNlPlvWs501bjXV+h66SsKXTNRUmj6fi4 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 468e701b-b461-455b-1cb1-08db1f6d168f X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:59.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: yqO9+ZWLPRyX1FqLsJLHx5K9JkAmjE6aeyGC8pxDpPgRTLSEAgmI7gXLj31h9eD1 X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@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 Reviewed-by: Lu Baolu --- drivers/iommu/iommufd/device.c | 91 ++++++++++++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 1 + 2 files changed, 92 insertions(+) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 0256e65476f045..68d1bee1cdb68c 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" @@ -359,6 +360,76 @@ iommufd_device_do_attach(struct iommufd_device *idev, return NULL; } +static struct iommufd_hw_pagetable * +iommufd_device_do_replace(struct iommufd_device *idev, + struct iommufd_hw_pagetable *hwpt) +{ + struct iommufd_group *igroup = idev->igroup; + struct iommufd_hw_pagetable *old_hwpt; + unsigned int num_devices = 0; + struct iommufd_device *cur; + int rc; + + mutex_lock(&idev->igroup->lock); + + if (hwpt == igroup->hwpt) { + mutex_unlock(&idev->igroup->lock); + return NULL; + } + + /* Try to upgrade the domain we have */ + list_for_each_entry(cur, &igroup->device_list, group_item) { + num_devices++; + if (cur->enforce_cache_coherency) { + rc = iommufd_hw_pagetable_enforce_cc(hwpt); + if (rc) + goto err_unlock; + } + } + + old_hwpt = igroup->hwpt; + if (hwpt->ioas != old_hwpt->ioas) { + list_for_each_entry(cur, &igroup->device_list, group_item) { + rc = iopt_table_enforce_dev_resv_regions( + &hwpt->ioas->iopt, cur->dev, NULL); + if (rc) + goto err_unresv; + } + } + + rc = iommufd_group_setup_msi(idev->igroup, hwpt); + if (rc) + goto err_unresv; + + rc = iommu_group_replace_domain(igroup->group, hwpt->domain); + if (rc) + goto err_unresv; + + if (hwpt->ioas != old_hwpt->ioas) { + list_for_each_entry(cur, &igroup->device_list, group_item) + iopt_remove_reserved_iova(&old_hwpt->ioas->iopt, + cur->dev); + } + + igroup->hwpt = hwpt; + + /* Move the refcounts held by the device_list to the new hwpt */ + refcount_add(num_devices, &hwpt->obj.users); + if (num_devices > 1) + WARN_ON(refcount_sub_and_test(num_devices - 1, + &old_hwpt->obj.users)); + mutex_unlock(&idev->igroup->lock); + + /* Caller must destroy old_hwpt */ + return old_hwpt; +err_unresv: + list_for_each_entry(cur, &igroup->device_list, group_item) + iopt_remove_reserved_iova(&hwpt->ioas->iopt, cur->dev); +err_unlock: + mutex_unlock(&idev->igroup->lock); + return ERR_PTR(rc); +} + typedef struct iommufd_hw_pagetable *(*attach_fn)( struct iommufd_device *idev, struct iommufd_hw_pagetable *hwpt); @@ -509,6 +580,26 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) } EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, IOMMUFD); +/** + * iommufd_device_replace - Change the device's iommu_domain + * @idev: device to change + * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HW_PAGETABLE + * Output the IOMMUFD_OBJ_HW_PAGETABLE ID + * + * This is the same as + * iommufd_device_detach(); + * iommufd_device_attach(); + * If it fails then no change is made to the attachment. The iommu driver may + * implement this so there is no disruption in translation. This can only be + * called if iommufd_device_attach() has already succeeded. + */ +int iommufd_device_replace(struct iommufd_device *idev, u32 *pt_id) +{ + return iommufd_device_change_pt(idev, pt_id, + &iommufd_device_do_replace); +} +EXPORT_SYMBOL_NS_GPL(iommufd_device_replace, IOMMUFD); + /** * iommufd_device_detach - Disconnect a device to an iommu_domain * @idev: device to detach diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 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 Wed Mar 8 00:35: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: 13165022 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 068C4C678D5 for ; Wed, 8 Mar 2023 00:36:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230014AbjCHAgc (ORCPT ); Tue, 7 Mar 2023 19:36:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229937AbjCHAgM (ORCPT ); Tue, 7 Mar 2023 19:36:12 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on20608.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::608]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7F259F23D; Tue, 7 Mar 2023 16:36:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HZpgO0mCjtxa4Qi8CNB9Q/1y2HOUApnVGQhTqTPr4yDwyNw0MNclahBgF8TNi/vsOH+YmKntIngDqak/dwNcLVJeoRIfHKaOwjrmCc0c6ik2sEsGoii7E9xZ+J6z+6k2IQxCnWQ9x6Z/I/27kUU+Yn9BiQ4/kCMSn7X/5uwwKPoucC8cQnlEdDuRUJtScrNLyR+NkRoYIDpV/hv75PtPNKVmvs7ZspVcAs3vnpYwa/smhb0NWLqWvxe0dmYboc/XNC3OOjvHHfQ0SwuS4CVE2cWvaPcVO1uKzdVkzDAocB1cE0asM/kTAWkyiFpL1gPHtVBQkllwOqYkERyvde8RtA== 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=RV8l/ak8wlRmNk6ShwOnKqrD+JMedCjmNa0A97+8B0c=; b=dqZLV3Azk9Wz4s85dCgUrIN28IwwBTc+y271o7eWT21z6B5tE7vhMfhKAtGRphRnYj9QicIx28yBQ8mMeZBbgJkSfjDMa9gF8+lSn2+U6tg7EF1paSpYeg8MnsDhSiDCLRSK05tlzrevSNcy8nkJUjhP5Nx4oO+kl4A3MKq1sAHUO4kxKKaH12c64BiQtX8V8MtQQKa++2Bbz8O4mXcxywpkbzl+N3Rv1NMCdU03/JobBcQDlFDH5khjwmhO1JIzf+KG3k5u3vnZbibBlbIpeeqxDjB17PWXdtITLALVaMfKoGuPEnel4QZFz/SYXGYXV+mnemZQAEQmqgXuuf82dQ== 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=RV8l/ak8wlRmNk6ShwOnKqrD+JMedCjmNa0A97+8B0c=; b=MvCtmbnwPj3SBYkEw9xL9pgldQNpmQDFwP7DJ9R7U/mkera22DxNetmnNeFP8Ky0BXlaEh4WUaKA0si3hZ+Kdf54lXL6zpEULMydYQoR5kVfCnbSGE/zL5JX44Z2dUG9FY5s81IypJAjW/tKOzwjgPz/s5iUh8QLf6kG01UZ32WvdY5fFvI1WxMdabZ/CzJA+H2i6t/a502SItLXYsywGKxpcfPajY1uP4WwRWtlbQLwjb5BfM6EDdGTCS5qg1LaDLBZ0MSg3b+9VgznAnVh6GiQ3siX36w0wm4WZMZA+RraErgJJ08df8WB778ycDabGe2MLSHSMM3szn3mYT3gSw== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36: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%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36: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 v2 13/17] iommufd: Make destroy_rwsem use a lock class per object type Date: Tue, 7 Mar 2023 20:35:52 -0400 Message-Id: <13-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0300.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:10e::25) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: c345664b-a51c-47a3-cc2d-08db1f6d169f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: KpghV9MgXdEBDbhIl55O162MC9V0ygO3AnYC0rJGYk6MbsS7n8Nhqn+e8NclGyn41obFZt71Sfd7VXoXeQyOTbQkoCLv3usYmvJGcNfio1k3FggY1Kgazgb2/1hFIMT5Na8aaNL6bRCBVFXddQkhKLWOm61K1r1V8QJEW8fcGLksPrPi22+fDJfFEo70v+MI6a0GQFrK87FFRmQ2g0fChN831+vva0SSzubiu0q0S3I5C4xzt7zJb4ivlrzvqRmvoscu1JPRY+azPnUoEkfeNRbIKirOIBxSIX2KGPbmsrxYXGXYdx9u04OyJO2M4/X8HE8coYsV9VRt2nMLvbxDhUnWyxZki/LWyHYgbRdd7F28uGhC869hS679pGLd4MiE5B8FmgNF1YrHMapNclKeNrG4BNfAmZaRMBoBGhNQa5JqrBaO9gNBE2UmqD7bslkaICuxApUwO/D2DxRNUSElb6nWToUOu3gSVn6YbTFmZfXCKH0i54KjWmF8Jj2HR+iExcSIK2zGU1veCWBR4J1C/qijyhmPIYliS/qJvhyeKamxc3f6wl85TORcSPVzeOkoAkx1NrVoJBH20pamoFYR5cfKDpiPL4bLGbliqjOY9W0fenzy4FfovPtsKgRV5f8XsTTZ4CgHvrBpgYQNDVwanA== 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: lCwRfi2ELfMoCFOfoqPRicGv6kwJS9U31sTK7RJN9O/g00lYCJFZXPPtUfSG4qlvB8uZBXU6+tsUJyvF+He/Rf1/6NUUbTAP7KUEFdaV95ClxbvR/2JCPMN1LrUVKLuJ1zm4oznXtsKWcKKJANP/NvedlKwOxQ+vZ45bQ0lYo45Z2DgPZlyIw8JjUQ3uvqn+kXy9QoV0HNwY9Qv4KdeHmWdHn9QcaTfRJJJW/4lm6j3LsUUD4a44cbugs7vx8hTUJ4kVsBz4HKeiojj4XNcWfsScYy2rNuXqOd8PrGEr19EcDvd70aiVqOouO+dV+WR98CaitBrSaO+Qi2nrBpbkEqnykDuJ0S9VZtBtGeakF2SZwaSylIHX+Ypk3lpEtd0EN9rDgez9zA417YJok9PXDbG5YJo4rFVj4uMVpq9yKmyxtiWHdBjCii3vzqzMmvINhoSqOT49Jw5VYRFR32bYVVKNrthRno0eH3d3voDAFXfHsbKx/R4EH7eRUgViYAr1ewG0Ozpu8zHjiULyAnngkFiGcdGUGtBQr9yfrJ+0YqrumQSdhvKS0kqp/7Szk5e1lkbC94fjGSC7UPz1QEie+TUIhfp77DGyf5hv8dEIuXe5Zb5WhQ/oxnoZf98vdgd8NDXyRyggGVQC42zebv6xTivm4/UtZRb65JYeXh3J0JfDyvDGXJ6cJpZH6xPIuxIICppt7ai9YMuCk5hu2OrV0KEInyXGvDj2Mrth8fSZdStgLusO+GmukTte6lhoWhC7IwSkvmAhsxLYTUkJV2kftnfLhpHUlwmW+ZY5MY8trjWpcEjXHhY/djMuUMKTAvuqDMvShR1Z9u3UPFtTZ1SspsAqXPak1CNfDLAYTfIRFMK1cRmXyQnpdSQzpcgphanptUAJ4FfVVAstoWZ+e/z/+5Z5u6Mb5EjusukFqXn9DQNa3ceFv2MWXclV5kXGJ1JqOD8+gZqWuC9Eyr6Bi514UnA2qJo4l1rYcSKbkEyeAFTcW7Mo0YTU2gbwm7oyGLZY377HitRe3xJtqqCrumzQDU7e7FyuagaecUj2zDm9JLeSCJ0stH9qWTuERGpN5erPNi3BBd+jNq61ncxtLoVxIhZrhc9F7nkULaO0tII2IgxSVyjHx1S46cJM9C5Y6wn8RNjWILptM5OXa9HHgq5qxfetFFSMImRwitJy7B6DSnD1+CHA4vIx08ooCjhGqhCe2U5GvyTWhy+f82updhzgp2z1he3pnrCb0/NmM8igMAnZ30Jbnb0Dm89DjTZIpC/vMxJ9BixhjRuHZ74e7vK4FMdIauWK5P2KCO5ECKdaCMnUnderliw6CVGvK3pwoFikHbwnuAO0FD+ZhGBKOhDGRVM9CCoybGkW3mLnWICshcREFpMIPblZisSBwPZn3VP5rnLCibwoGr+ATg3UsUzdvJbXuEQKp5xnUimg+jOZhbdTp/GXhC7NoNGGgosw/2mmBu5U4H7V29H48Pib5f6byz8fOf05unXD4N5Br4bUNN/7fgPqd3S0wt8JnQ/5hFjJKFkTbUJ4wA9LGCOinCDOIpgSo+SmNDmT7fRNwF2FfIxC+KWgPi+0tIl71I07gTIX X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c345664b-a51c-47a3-cc2d-08db1f6d169f X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:59.6399 (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: onRLvNSzHojjwfyseDJrj/hMmQmaV6IhecroV7cPoqr0bKb2wR02bafN49UELmQq X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@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 471a3fdff1e0b6..f80b012e1bc200 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 Wed Mar 8 00:35: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: 13165023 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 69239C6FD1F for ; Wed, 8 Mar 2023 00:36:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229793AbjCHAge (ORCPT ); Tue, 7 Mar 2023 19:36:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229975AbjCHAgS (ORCPT ); Tue, 7 Mar 2023 19:36:18 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on20608.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::608]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C03CB58495; Tue, 7 Mar 2023 16:36:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dHAmNSzOT5mg8K083Ub3pC8eO+xYYTVC0cZyvA65cgcT2Z++N4rwUpjfRUZyEl0+/mVKcD0wayF1tLkMCuCeb2s9sfnCZ4Jab9gqqUTuQmfj/qG6yCiqrkehM4bc2LekcMRfOHqOF5evAi05qaVtKMsRpSIp978zbUg2rtOaRMYCf57/LiAPnlZS1hJ4Tp8vuk3hRA8+Vd8OvXqmPKG5VxeCWq5Ax1GDqnkAfzGPoW4x9B2yNvZwDS2KUdROjwmbrxy8kg1hxqlajR12jT6+6RoUVsA24fCdRFno3HmlDWJAWMQUfLQWyWQvSzU4z4r+kK4QVl0Ak7a6X1fkmhkAsw== 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=qQ7eAL0iM4ezK6Fs66vWPlxFOinFHhdwE4p0UCl83vM=; b=bJQYVDvCPoXRr/9eiODZ5qJn5v6H/47c0kAQYw22jIZh9PEkFm+wpk+9PCkPHTne+Hrp78xNAobGYvqr+xi5Pk4SjkweKkyKz0z0fONGNOaBZJzs8Qk8bI+pPD9nlFNhCXgt/gBVkBByuSdGPjz61bU9JB1sE8yvvhsdcj4uc4CT+6B6UQT4djg1xRQCDpi61HmF5Y6o+3C+TUBSmnGHnhONFp7VGiPgnZVlNpmdLKyPDhA0CMPPl2gbVSzBgsHMm97oXQL1iyatEeikztaMQz+Dmcr45SQC/Fni+RKPeRfTEdqlxL23Tz7NOZSqNz/FG1sigFrSP8wnIrDVUL6zrg== 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=qQ7eAL0iM4ezK6Fs66vWPlxFOinFHhdwE4p0UCl83vM=; b=IpLdri9/YCy/uyiwd4UYyP5ZxqGvG+Q79tVM9/UUrgs6UL08MHPuJqCZbKent5cVOa4AL+VZ5dZnr6rxWvB9Wj53TC7TRL4AhmliX7X0fRo5Rj0RuX4gcXjs4VDt5A6ZDqC2nlMMGfSLkXrqfytf7gmyphCF0nRgXgm3Rs1/AHNj8t6W05s4jjwatk76gPUu+L0efqmXnDvZ2M1GGkVZIIxqcuyg4b+xnCz74hi5TL1EWD7srEjT6O5zeN0nFneaZESL8+17zdJdbwCYfjDNFcyyXEF6SfwUHbcTYs/K9xZQetcjPxWpxDn5j0OUw32/FXkkrCTe/WBbR0GXEzJVfQ== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36: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%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36: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 v2 14/17] iommufd/selftest: Test iommufd_device_replace() Date: Tue, 7 Mar 2023 20:35:53 -0400 Message-Id: <14-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT1P288CA0025.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01::38) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: 9d134498-8934-4bb8-07c9-08db1f6d169b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: M8OCmUnRahMnopMNqJoLa5WhrPtvLQCGrapHv5NOGV5XS9iauJ2vvt6zVGTenQxF28YKry47SjqIklCj995y57p1Qjyo2GeI+1NWJM2xDpSyZp4a12bFzACdp0jPDaLR5qR/8hWacEjOJdGg7md1wzd+uRkFy4AxDbBVQUaaomp8m+jPLLbSo+6BUKzLBluOfHZrapz4dHHuprcwe6nCuywS8qVUczOb1sa1ozFQX9V94W0icGxnvnFjms910efRj3B6RfOijSMPJpCbLmbTSNlUFJ1cLFJ2Z5Opx4mMyhJpEDMvLrx95EYG4dMybUOZ7jNMCWGeU5RrVHBq3u1bRrkslO4khrsjiJLVOZidBtgFLs1WZElCVmX8KVu9qtqjKUbe2ujWEoyYVuzC5OevKntOE/A+xX1d8LPudCmHNNMxZvEiLrPV+nXu9ZYzeLhDmE5KBunZXsMe/UcwIchsqMOJKdyv0BiCDi2tGnFjPpJEe43Hw0H8DweMriAWaqqHsuPJmUJJGEbl7xDYK5lvHW5ktuXmvghNPfik97Z86EvCC7VTj9Y/OxhOBwbMcmB9ktyEO2YarmH6cAf2znYF80IKTSWmw5Xqd8GUhJKZMDfBojCOVuk98bHLyAZI+DoRSQc2brPbq7/PFFVjrjVS0g== 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(66899018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: hCi/0hbdNCHEr8j84kECsuHQmMVNC3+ZCaOQQSic4pU85qH1YM1Xb/bEroxWFz9g9Finu1oXJPpbvqX99lZLigzFd/RS9ebRyQc5/MAcXS7Wq7ER1ZK21i8MY7N/Ukqas3+52vSDSA3g26FhMfiqPSRAp5Qyd8eGJmK0w6Tu/2MqSf28HPAo2drGpYXwueI+ViIKaxIYZSi+bTL534CJLMOnx1m4dm8MUPOjqCHXoegHDC9Vrnph0eksEeD0H04BA3xCnS8cXv/uesZG+XVrfMzyVU3pCHtLwePHVO4pJGfQIhN1e+b8waD5lwOPZ9HX6CDJ8fdcc784XQnEfkdw4pn4sYOwA4yzfG2JGZsx+deUdXwqUD057Vpm306t8skahG0Fi9QhKkQlF8Uqdsel6W/v49Lfg5QGvPP8aS5j7aBFkViEKHHQMunx6fmeYqU09W2r16lTRb/A+R8NhhLUmxLW+xKf1XlXcGplMrN8SNxDAEVLkWI7U0ekpctPOtutL1bCroau8ikG5eW7QR8SqEXTjqccUBxlWf6Pfc1E98fhA/n6yDqr5hRykJJLJgaDfcTiOD6/wZvG8BnotqLXYEejF8YtKXF6JNlqa0f6/+S/v6bHStR6XRJtxTDh89pfxcy4Ffy6OQo1XJblLanvk2TfEL+vOFTIdF4YTMUoCdxjbvMBp3ysnAU1TyPFblppm7Zla+/xHvfgocsvQBIT6yx7eoe8V7qZeUPQQf/OkYya/+iJTTpnqFsmg2A2VkY7/S/3yd3TsqKIHq2NgB6UsHESj4fGIDvqaR6DQ3laFcZlQyHbpbcCP87P546vO1H0h3RMn4T8FpZCR9+7Pd1yRJh3g7ULgLpOFrdb56iGOVuuCSJYGw3hlHCDKf4eNH8Ang0535GU5G8K562aBNrnn3lFtFyyaebeN+7To2unvX8cb89HdMuLZ/eAc4SAF2ZVF6sPrHdbSMGsgxu7jAjUzGFjPMhK2vmPgLF/1Fagu4w70cvrSLoyrcHJf3VOqcxQBhs9AyCpp2VnueUaSKpsTfr65tEESdL0UAF4LstND/yY8sPUW+ebUYL0Y/vfHe7BoaAi3bec9UWmaBUylR7V+e/vewQb9n8VCMYoWIzo0iWyTu0OWDidqX4xJ4eOI6tHa9Yyp3glenY4DJYUvCrZ/Yn6EQALcx9qiYleRSOyYD1VevrshB0U4rqeIMXlB7ISbnSJm4v1YIStw8B6Ri9XKn65uv5byk2uPkZlIuKMP+yY+XPvGqONpg9tU9XhPQiq4DisV8RgkpoSbYZz15N3+qyajojZJJhA92v9ef/hUmMWGGuPidFvWVuY1rqJ2NvEuZdjxNdWL0CPAqMnWzqbsV10p3jCk0T+fY8Mcuu7lVH3KzSNV+qKZ42yGhWE1LBjeU6J/OSbjpaE28MzSKRG7C2cwGYM0HRuEl1MaVyBHsGJZ7b19Ff71PA3GIVpsZTrqiYCQDRmKq2Wi6qUj2YN29y+3fx+JVV1QV3jzYe1ShASaecDIWOS4s+yNIYlpnw9DMo9WqTZbcW/WFxOMasyq8m1TFuZFTMalp/VPk9u5fzmXyCxdBTjyofdgU73Q72g X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9d134498-8934-4bb8-07c9-08db1f6d169b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:59.6868 (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: FNs6fz+QEo+stRwUfXERHt7vmRld3KDLFW2x0qgmp+Fi1MrZN40W8kjLCp0c2Dwi X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@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 b3d69cca77295c..e3f1035cbd6464 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -17,6 +17,7 @@ enum { IOMMU_TEST_OP_ACCESS_PAGES, IOMMU_TEST_OP_ACCESS_RW, IOMMU_TEST_OP_SET_TEMP_MEMORY_LIMIT, + IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE, }; enum { @@ -52,6 +53,9 @@ struct iommu_test_cmd { __u32 out_stdev_id; __u32 out_hwpt_id; } mock_domain; + struct { + __u32 pt_id; + } mock_domain_replace; struct { __aligned_u64 iova; __aligned_u64 length; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 17cb7b95eb2757..866d77247a07fc 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 da0443ba16830f..77b0601fd13a71 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 stdev_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->stdev_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->stdev_ids[0], ioas_id); + + /* + * Replacing the IOAS causes the prior HWPT to be deallocated, thus we + * should get enoent when we try to use it. + */ + if (variant->mock_domains == 1) + test_err_mock_domain_replace(ENOENT, self->stdev_ids[0], + self->hwpt_ids[0]); + + test_cmd_mock_domain_replace(self->stdev_ids[0], ioas_id); + if (variant->mock_domains >= 2) { + test_cmd_mock_domain_replace(self->stdev_ids[0], + self->hwpt_ids[1]); + test_cmd_mock_domain_replace(self->stdev_ids[0], + self->hwpt_ids[1]); + test_cmd_mock_domain_replace(self->stdev_ids[0], + self->hwpt_ids[0]); + } + + test_cmd_mock_domain_replace(self->stdev_ids[0], self->ioas_id); + test_ioctl_destroy(ioas_id); +} + /* VFIO compatibility IOCTLs */ TEST_F(iommufd, simple_ioctls) diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c index d9afcb23810e1a..baaea15b355297 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 stdev_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, &stdev_id, NULL)) + return -1; + + if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL)) + return -1; + return 0; +} + TEST_HARNESS_MAIN diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 85d6662ef8e867..3222f246600422 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 *stdev_id, EXPECT_ERRNO(_errno, _test_cmd_mock_domain(self->fd, ioas_id, \ stdev_id, hwpt_id)) +static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id, + __u32 *hwpt_id) +{ + struct iommu_test_cmd cmd = { + .size = sizeof(cmd), + .op = IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE, + .id = stdev_id, + .mock_domain_replace = { + .pt_id = pt_id, + }, + }; + int ret; + + ret = ioctl(fd, IOMMU_TEST_CMD, &cmd); + if (ret) + return ret; + if (hwpt_id) + *hwpt_id = cmd.mock_domain_replace.pt_id; + return 0; +} + +#define test_cmd_mock_domain_replace(stdev_id, pt_id) \ + ASSERT_EQ(0, _test_cmd_mock_domain_replace(self->fd, stdev_id, pt_id, \ + NULL)) +#define test_err_mock_domain_replace(_errno, stdev_id, pt_id) \ + EXPECT_ERRNO(_errno, _test_cmd_mock_domain_replace(self->fd, stdev_id, \ + pt_id, NULL)) + static int _test_cmd_create_access(int fd, unsigned int ioas_id, __u32 *access_id, unsigned int flags) { From patchwork Wed Mar 8 00:35: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: 13165027 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 1A958C6FA99 for ; Wed, 8 Mar 2023 00:36:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229977AbjCHAgo (ORCPT ); Tue, 7 Mar 2023 19:36:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229984AbjCHAgT (ORCPT ); Tue, 7 Mar 2023 19:36:19 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2071.outbound.protection.outlook.com [40.107.93.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8899A7680; Tue, 7 Mar 2023 16:36:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EfoQw4x3kGI5SA+Lka6KGityZSlBFobxDd+ynJm1fitbiPaZC6QwhU5VrFSpeP/KMO4//0pobvO0AQXTvJJR8YPUmwviHCRLSPEid/mONdgWIXIxNdv3LO96bsYaWpRI/GIbYdiJsByPd+48DB/Bs4duD3cmd+st1u5UR/6zCMay5uMtRvkaLaGvjQng4wsYK+6Jk36/BhKnXKtR4SEe8H9A06dxDdRCt2DmKP21SzYOloyHR12Qpktcz1jbz7fA6Ot4dsf0EYfig0eN1SgnfYogEBAnnMhp/AWJZhty/yJfml8eYqICKzZj5PFhBZVQcp8K60DB8rRe9H9Fd4IixQ== 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=WgOTId1p1reb/9qEk6FfJIw48Djw7Ii6fn7uA3GafNY=; b=nmE/72dwzkV0Tnmcl+J4KKVU6y155I70b5NUIb5CRB3xHS8Xbc+IKQdy06aCw2G2IuSXU7REkw95u9PuxiDlOWGGUXXV2V2zrVpnCXWbNLiCDep77EDcej/yru5WcoED3ilVK/GeDfGjv+Cq2Jy7Ag9SpBF1PmaYnpMBEEJtYmqACgM3YYBYrQqkTYBkBZxBc1k+FNrruXwULABkci4GdZil64Pqim/TlUkWSiESTDKsXg3Q/ogEtuzWw+pO96ytg4FZzSIwtNps4bkSNsUAyeVz8s09h/ttINp40DiP9WgzvG00V0eglty11MGSYUWdv2KZnG92OwX8VxrPBnfKXw== 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=WgOTId1p1reb/9qEk6FfJIw48Djw7Ii6fn7uA3GafNY=; b=agPK6wAKyGy46o5zim+wCyEOcEfcCgEmfDYadeJt0vh9jFBD7mQIKPTzQJ722TaMLO/WjNYsE7Ul8xUZiP4UNaiYeZ0m2j4H/u8gEiB5jp8t0KKWl37aH6dIqW5whJm5Qt1ttGrFWMSwBdIW4u8FbJv3I86tTfjBPZRPXZg+7pU4BscfvMFnGSgo/gxTaJv/7/EASua1Vfamq1+bgdmFrDfYwTrzbBf+LzdcIDPBWapg5Ka5HHqbVQQwr/4oTlHEYJT1SSkbeNGOvnC5hgF/PW0F4yinfp7xw7do3NKmwytuz4QBiWmVrZxZXH9k3aYPtREKsj42jhj31XfvzYc3YQ== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36: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%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36: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 v2 15/17] iommufd: Add IOMMU_HWPT_ALLOC Date: Tue, 7 Mar 2023 20:35:54 -0400 Message-Id: <15-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT1P288CA0004.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01::17) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: afc13432-32e5-4326-20dd-08db1f6d17d1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Vu5gstXlLWuGS59tSsQMzS7pnUEEMvfnqZA5Y4YNzY8F/d1Wb/eRRr7Tq0WH4EH+7dDLGND+lF0Gsdc4/8gatvB0rdPzfbM6UK/BWr7wu1GTS9uAMR8LTxCWkap0v1QFizFnTKeChSAZf9qTby4bytZCZ+/0ysZdK9xnQ8PpIGHgaReI80lieuH6TBEgzPMUy11Royx33VBqEf0GuYdJ2WLhbOllhbQIMkTPQgeSsX+u+C6mTf6wJREJAG4JhUyVN6wPlkQD1KxLq05Z9uz13FbZCmn9yq0pBTweSaykFry60wqnhS9qZTrbkTyBW3u9x/omUOkcwMMSMdkGo/cnDaIMaxifPQUG+skpJMAzhfg4hqAl/5M3MkAlBL9+VxYaVEqA+2PxgK/hPvT6DmywDWcti4xjJ5vR3VSQFv4BnHCqo+fxUKa0xuhKKhD5HwOjP53eCQdYVRypVsMGfAyebXbDIk0FPt1OVzZ5khZxFWOKt1FjH7PUL5B6KIxIJqs8nCxct5RSD4x6uEQYxTmVHzt4zpB1G9ZSc3X5BjkFUZFlEe0rurC0/9vNcSm8B36840/DV5cQBonq0vuXJyQVm8lNQL4NAjTgLXrym+tH3PDyuD2TfztEeNw8GPIi4FzlvurVhHPOfI2gcIKh8GC+xA== 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: hFgWYRrbEfhOYxmpQCQTT7KQaMtuU12zZPvLkZX4nUI/eZnGe9q9KzIOKL3rnU3x47yOFiX8E3dofYcj/qKTUgbNrwBZdtGC7e0RVl9rMPEkKUQs9eHNHKux52YKDhZiNSmsJa3xsVZ+jdW6/AIuDdnoO99Fz748B4kKBdeIzCDFzSWPreTe4pPOU2vhcT+rBAano8sXVDx295Lp1OJQYgnChU6w9B31i3DCgF9p1U2DzcERmCuNcpkXeWDSDb3X3Ob9DY3cgFJJb8mzvhDUMR/DVre8vMRH3e8fKCnpVuvrSrCqKBicOtdlwcEgrmgwCCzb2o8hURpgwUkMIc5G5JZry0lr9ix6yQNzjrOJYgkgnFUESS07Ng783C1YBAyDlCjCNQlrdOTpKastFgxwbT7UoE/NXktgEXok4GIIPkj+DPjViatjEIUqDU7fqYN/RTubcnQ+NdCUR9Rfw+IbJ/mdNqpdjQRBNOh6Y9ca/X5qEdltCVu+87fwQfeF1bh3PiHrtrNiEw7fct4JnnCs9OVGzxO6dFCUo8giajVfUi8+/Isu4j96oRrXjkloWm7i8n7X3yY96YsJhFV4/RCUkQ5DRV0GsMQVKb2ZKXOhFOymdDXwLUSfNT4ln0hZAlx5YzkCqQVp/C1J7FDcnnlo2XE3TIcBtV1XMilsar71O1Jr4mromDnrpvTqWKm6/+a/dT6kNffj7dMd0AkGsKqeXZuuY/XrVC6Enx+bGLD+G6cFX+dwLVy/VXa6ZHo/rhL1L6+YrDDxBPXsGQ9nomGiIvjZH+tVFkT4Ch4Wv7WMj3Q7055oobkv4eMhAmdPK3bjcehk+j3rJIGRp40msmQM3znFl1//nCKP0DR5b/D0tw7Wba8SwKRmZ0CLtN9qrr0PEVsAkxLUlbn92kmeNk5VmZ4lB8Cp27DB/jxKe1AvMD54VXT0wAxRxJh9CBh10cm2L8nchc2tU/38P4pOGeDl4elKSGjvV1eH8S49hn1dKHU+Oh/GwR6jQnIRIa3Li7mlQPYLDWRU0ZRgg7RTkff0KCQ1hOFp6naVmre8IiEQaWwFPA4vSeKMiSdI5uiJGfhccaeHZstYXez/U6Toj+VFiAf9jNTrdl++iqIOZuFnAJvo1j8/R6Ty1jrGMPgqvrTjJwfzP+2Yrj/mbIq11NLDmMmb4hwrH/D54UzSaWi60Im3qfFKqYXcu8mGgBg4dBITZLzV5NqOr9kVZsGmDku1yExQM3ye8GWGY/TCg5SApXTKFnnabpiou66jwpMvwtFy8yvNwI+bytR4K4vmXrQ7QTlLJwX7eLMBILm5dripoDNXMOjGDjdqEKQce693dcNkqB37BqukHnYgPwY2wp2E5EyWkWM40rWpZ66hcvfHE87xe0g+hgM//919QvP9/fpMDXmLv91CDdgSLrDrwcCmkAdnbUiGTWDcQiYJsY+fLa7T0FhrOv9sPbTqw/LbLsutl/rLjZjzNXxlFT0vS3bEGxBo1QaplpAFRQ3E4TwlPHsJjLp6nhMLRXuddwHmaWy2qPsLhiKYTUMpJJWGscGghxbNv6hGqrNWYPjQ+ELLeYDCgHu29VJZ5b/nq2iVpuxf X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: afc13432-32e5-4326-20dd-08db1f6d17d1 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:36:01.6253 (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: X71+2LUPVn/qL4AAXRLJjK8UGobP9kAoNeRMA3/BcM44kR6G5diMcEny4MILVS+J X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@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 Reviewed-by: Lu Baolu --- 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..13bdab4c801bd9 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(ioas); + 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(hwpt); + 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 f80b012e1bc200..cb693190bf51c5 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) @@ -297,6 +298,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 Wed Mar 8 00:35: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: 13165025 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 0D28EC6FA99 for ; Wed, 8 Mar 2023 00:36:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230045AbjCHAgk (ORCPT ); Tue, 7 Mar 2023 19:36:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229983AbjCHAgT (ORCPT ); Tue, 7 Mar 2023 19:36:19 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on20608.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::608]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95D05A3B43; Tue, 7 Mar 2023 16:36:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BpF/SZmUjbrOOZipzoBoM0eb/hWNBNIDvvmXNwKNHThfGqfoD0vUMmlZQDoe//ZP317/gg6l79L3znhdaFnMXwx4Bptjdg+HvgZJ8O7RCdaQ7O4sF43H/q/Hzk5gn8Yv1tMJbg30/Fsn3Iapd7GSYx1TVemV99nK0HRMDOiSxnEIP+6KleMvj1Z5EGr2Yh1/rCh8QGGU7pkCqHScgHxP3/lUR4jJDLo1m5BMq1BXpu3g+gwB1DW8xjSdZiJMs7G+V7DZLtN1QtDlumOBMB5xj+IGI6cSOHWDNhu9Kf45//O84aflBkgAK/G5uvEAAqshqvEDZ0aBgJACs/wnmMKrOQ== 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=UWNmTsLY5vQfv6E+IgOlY+ECzeopldYwfZ8lazNJcjg=; b=Yg5H+62cg0gdEPnEodWPLK6iTr6EcWtgP4J0Em6Hwx8NiRErbPs3VTWvvuFtxmubb3T865cgg6oOcpIPTNRrXHPtFKCsvdwW5NFMQRpnxKL3Raz7hRWfJ6ABViWlXb1cWH/FrUBTIjAAHv7vzCdPybgjmzCnJ4HbdWP0R2TSaUEOYwrPTSPPi+zQGQmdM0cHaIs1i8j8eNbKcLQLF+nUHzN8f9SKjnSjIAPSKfXzuu3J2XoIjrKktDwHruc+V9Hh62ahvmwdjMIN1AeoWGaE/RQdwjS6rJeIa3usBTMxTJN/XieZAWV5sHx9BPKFPJkXOgqyjSr/AOC7G82gtayNzQ== 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=UWNmTsLY5vQfv6E+IgOlY+ECzeopldYwfZ8lazNJcjg=; b=b6pd2ESKQGYfuaxFs3SsmPnSH5XDW3DduD9wliDTK3eYjaPz9pBlfgwR+3FQjYDMkcWmp85FhjSm9IQKt5ySz5vUxb8BRIELXyHzTQSmMgjzbn/qVdeB13h0WbHCkL0193hHVD/tWfLYxT5GH+MA8PdTKz6n5J5ODe9lYaGSXriBkZjan+UHvVdrsnZT6mfWqTZooCeu5YlP+bN42BWZzrfSzz+DvYXd1NUd6TLmz0FSmS7vntVbFpSfa66V4B72hIG5FGX+5dFI8OdQ1z/wa4SuL8/pHPhpjxwRHU/ImHHtMaUJIGG9dV9voiU6cHjkSm3NrfSAMjUuULk0NSPzow== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36: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%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36: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 v2 16/17] iommufd/selftest: Return the real idev id from selftest mock_domain Date: Tue, 7 Mar 2023 20:35:55 -0400 Message-Id: <16-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4P288CA0085.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01:d0::16) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: d6b1dc7d-62ab-43aa-db2a-08db1f6d16d1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: iBE4TF69IcH0leFODuUW10nVOtSkr2qJDNqvJ1NuoNl8SEjefL6ttVRns92B6h/2fTzuefteP2oFudYgxpfgfGGknPFEGfZH2i/4VqCqDQuq9yoRk71xGXZ2WASika0mqqJKiK1xm3HrQHiyA0DO7Hf+FHQtpWbUrlHcdmdtw0Cdv8VSPknuukh6VadCkYCl+bqL2O52hz9hqrWT8OvkVCuTA+u9ut/Jv5VpzFzH0CJbjZxGyKwGwpVhPoQUCo2w15PIhgMIm6T1YPkuVPetV4PGwu756l5mFHUh0y9T65bqDi+VnRozRT2UxIveNwfYDn63704t7e8sFj9yWUsTlUpsSOOkWXbkLn61Se+/Wz/eM7n01qT1I76H2yZSN44el6A8qsZ6cNTRd+DOwDdOzsPqUJqc/rELSKXmZM2EEbykiiOizcn22DzGuiJN3/LceQDnoFnZyjnCNZy+IersPQGSRCOCPcojz+iMbXQibh0JDsmDjSHoYhiEiK+kDrIVf9Z+7ahD0ofyFXiY9fAEL3wvMDAoJ/lYwJHkCpqi+VXyfboCFQy2+uV+gbd4seXUXOWgpHsYhzhF+e7/9rBqgo3gvaBKsPcfg2hWQGC24yGrbUXox32+sdQnPemZXNJ6B/bAgXNkwoRTVTXApt5/JQ== 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2qAtph4BuGJKIFsNpDOXH/YtM7tj+vWHIY+LQjgREAaQd/yEZf9gzFc+yo2GrQA026JjYtqdHIV9pv9YzB49ZBwKx7G4rCcK3cPJT8R6l3tOMQIArQYVIXk8ppZ36HT5pH7nvpncddjT9Z2G2OS/vg18TX+3k9n6bjmVyhcQRFadxUVM8hnnbxMYzHmrYn7sJg7QZ8Itk8JSL1QlN5KI2GMZExzv+hoovhQb+s1tm9Mit3r5malilJsamAO4qFBm1f60TqFMG0kPmLY1ICDUvNnIi/kkzQ4JLuJBM3wz9G12tN1h0p/GOxm5aF9LUGwAOYN6Fa5be9karz0lQHOBJOKC3Bd0iCZUI/+5wpb866+pj8xfBBbFm8JHGUpN97Gv90i1GOVlEAYywV5XlMHKgaXdUhaBB7yUBQ87Sm+FhHWiH3Pf/07GoSbAtVeQzgZdSiYptO0W5R0LZ2D2oAIidN0fvDhPtSIO+zbiRGwaJpvIqw0UCIRG7MOwB65ZcRh5E9W0XIIYO52O122B6pWnRbNUducHIjaFlyXAnb2l64liiD92c2zqeBc4hGZ2VLsjw/eJf1BN4T+neJCtRgo7djMznHk6w4k9PG80DnU/2A6sfZtp7IFB6qnAgio0htd/dLvtZVAbu9wN5fyvC1s+4vaoL2GR27VqX9C4n9oGc0gYGbf75nZ5W2mtuKNjBsrhMeskWkAnQ0s1wVBalZ6oLCIlpcyow29fMCQAk1qodVulhwVMTPL7AYH1uC9PD0yr8k1cATHyBRClQvPWaiAXl81cSCUKWNFIRc8bYMvb9mJ1yjkQRFnhxJFyQZg7N2f8CCCCHu4N26ddaDXj5zyXCf0cQ+kU9b9u4u3s6bLt0Io2JiGSyS3REwuSzDMED8RIex5+L3Z4mdy9FIarohJqzKOW7ed07E7nIeqFX4KY9WZw3SzqFlkcM+Hr7zV0lDqwMB3yGj2Mtejw8M5vgNj4aVO2hEXw1kGbJjW7JMpWNpQyQO6LsxvUCXl5bTAgHLKfaGIPUDqzchKbgG3XQ9d6TZ7+WcoQblwdOH8mRVqHyy5P/508L5pTlPwj0A3vaAek55ZeMT3c+VUPVOywdJ0Gb+4dxqztlJWrzuINIc1FdlD04qF9NC1IjZBtU/6UVmLpuRkOyTfC2FXwI53zttvvTw9whooi5BWRoNsh6fsoSoD19N/oLkOxO3AwGTfkKMnFU/4eo35AnQaVun5vJghfh2ihpCbl7AIR9m0qrU2SqsuCNP6E0CcXq9umqiViAJVfW2ootuAWEYKoHRzp4PCMCBtNuX8gz4MmoA9Rz/hSUrEhbWI51/HY0lL4E+Z001fZM/B1gduj4MV5LCmTgGTkrvf5NU9xAhhBER17tr1zafTK/ZlXdzX8hULy9jEGvt58yRif4KlB/5zA6nYlUo+ldCAbaFRAq+OZB/YkC3yG7mV6omt6Wqy7n8T8IIL2SUha+IbO5mAjm1IfOukzYs8xolPcLziPwsxd9/sIkOWfAyasqbGO/4xXz9MF6q3quBZBZVl4z1zQW2VkBPtHNvreuqJYkPsLNs3KZHN8WsCQ9NHFLk23iynLFo1BXliSAF5E X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d6b1dc7d-62ab-43aa-db2a-08db1f6d16d1 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:36:00.0461 (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: uYt/NuB3bWxOYmUduW7gtQn5CTnZp+hzaUZq9g3dtUe6zBKWhRN5yn7josOrLp4f X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@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 | 2 ++ drivers/iommu/iommufd/selftest.c | 1 + tools/testing/selftests/iommu/iommufd.c | 17 +++++++++-------- .../testing/selftests/iommu/iommufd_fail_nth.c | 18 ++++++++++-------- tools/testing/selftests/iommu/iommufd_utils.h | 12 +++++++----- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index e3f1035cbd6464..dd9168a20ddf4c 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -52,6 +52,8 @@ struct iommu_test_cmd { struct { __u32 out_stdev_id; __u32 out_hwpt_id; + /* out_idev_id is the standard iommufd_bind object */ + __u32 out_idev_id; } mock_domain; struct { __u32 pt_id; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 866d77247a07fc..3e2284811fb326 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -443,6 +443,7 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd, /* Userspace must destroy the device_id to destroy the object */ cmd->mock_domain.out_hwpt_id = pt_id; cmd->mock_domain.out_stdev_id = sobj->obj.id; + cmd->mock_domain.out_idev_id = idev_id; iommufd_object_finalize(ucmd->ictx, &sobj->obj); return iommufd_ucmd_respond(ucmd, sizeof(*cmd)); diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 77b0601fd13a71..83c98a371f9e7a 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->stdev_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->stdev_id) { - test_cmd_mock_domain(self->hwpt_id, NULL, NULL); + test_cmd_mock_domain(self->hwpt_id, NULL, NULL, NULL); } else { test_err_mock_domain(ENOENT, self->hwpt_id, NULL, NULL); } @@ -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_stdev_id, - &mock_hwpt_id); + &mock_hwpt_id, NULL); check_map_cmd.id = mock_hwpt_id; ASSERT_EQ(0, ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_MD_CHECK_MAP), @@ -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 stdev_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->stdev_ids[i], - &self->hwpt_ids[i]); + &self->hwpt_ids[i], &self->idev_ids[i]); self->hwpt_id = self->hwpt_ids[0]; self->mmap_flags = MAP_SHARED | MAP_ANONYMOUS; @@ -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_stdev_id, - &self->hwpt_ids[1]); + &self->hwpt_ids[1], NULL); check_mock_iova(buf + start, iova, length); check_refs(buf + start / PAGE_SIZE * PAGE_SIZE, @@ -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 baaea15b355297..7a3d149019da92 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -313,7 +313,7 @@ TEST_FAIL_NTH(basic_fail_nth, map_domain) fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, 262144, &iova, @@ -324,7 +324,7 @@ TEST_FAIL_NTH(basic_fail_nth, map_domain) if (_test_ioctl_destroy(self->fd, stdev_id)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; return 0; } @@ -348,12 +348,13 @@ TEST_FAIL_NTH(basic_fail_nth, map_two_domains) if (_test_ioctl_set_temp_memory_limit(self->fd, 32)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2, + NULL)) return -1; if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, 262144, &iova, @@ -367,9 +368,10 @@ TEST_FAIL_NTH(basic_fail_nth, map_two_domains) if (_test_ioctl_destroy(self->fd, stdev_id2)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2, + NULL)) return -1; return 0; } @@ -526,7 +528,7 @@ TEST_FAIL_NTH(basic_fail_nth, access_pin_domain) if (_test_ioctl_set_temp_memory_limit(self->fd, 32)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, BUFFER_SIZE, &iova, @@ -588,7 +590,7 @@ TEST_FAIL_NTH(basic_fail_nth, device) fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL, NULL)) return -1; if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL)) diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 3222f246600422..e67a929a5c87d3 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 *stdev_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 *stdev_id, assert(cmd.id != 0); if (hwpt_id) *hwpt_id = cmd.mock_domain.out_hwpt_id; + if (idev_id) + *idev_id = cmd.mock_domain.out_idev_id; return 0; } -#define test_cmd_mock_domain(ioas_id, stdev_id, hwpt_id) \ - ASSERT_EQ(0, \ - _test_cmd_mock_domain(self->fd, ioas_id, stdev_id, hwpt_id)) +#define test_cmd_mock_domain(ioas_id, stdev_id, hwpt_id, idev_id) \ + ASSERT_EQ(0, _test_cmd_mock_domain(self->fd, ioas_id, stdev_id, \ + hwpt_id, idev_id)) #define test_err_mock_domain(_errno, ioas_id, stdev_id, hwpt_id) \ EXPECT_ERRNO(_errno, _test_cmd_mock_domain(self->fd, ioas_id, \ - stdev_id, hwpt_id)) + stdev_id, hwpt_id, NULL)) static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id, __u32 *hwpt_id) From patchwork Wed Mar 8 00:35: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: 13165020 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 EE4C8C678D5 for ; Wed, 8 Mar 2023 00:36:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229974AbjCHAg2 (ORCPT ); Tue, 7 Mar 2023 19:36:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229925AbjCHAgL (ORCPT ); Tue, 7 Mar 2023 19:36:11 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on20608.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::608]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1DABA0F02; Tue, 7 Mar 2023 16:36:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=I+hGzXjGDXze51t999kA2jdVnUeEbGuYWDUMdZ+8+RtVK7pkYeBgj6xc6Cvo10WvUy7IUY8H/m0gfqByoTTlx2ZxBPGbzB5f/6BNuL3kFlOWqEGlyZ8KDI0S0SmC5HtMew/KHLLMOWvAjpxvR+s/fPvgbsyOFUXdGTv/PPM4amlzy25pE9Yvpn/3NDECYgDPiAbly61wg6JaFw9rr5tUU/H8lb/pTKL7hK45mZRqmGTlk7M6FFBZ099MyvH/pGXl4w5thGRi8oRL+N689WfZYr0K/qhFh8pQXbCF1BfBddgUmHbT6iD9znrt8i8wN3P0x6eMCbaX+udyygtjfbYceg== 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=N7FhxAsju1x7JDRfFiXFpOtrJm/Kpj/Olha+nn/QV/I=; b=JzTjrb0dCA4yPP9sKq/Lk4V9Qi8bHiuU5YtkApl/vFgZ0IeTWShScn/NYZ6ICcH89mlOOtnx9yep1y/5mt7So/CsBWPeci2WYnp4neOWc3psHfsUf+JHSpK3IHoigyGszkF5n0r9ORm/uBlEKqdf1YOjyE4nyaevdmi9dwVptwvQI4rOPxndIJMZdKG82jrWo19HixEEUN/M0uRQjfo0bKMCYlPfG/V56mi12vBVPCy0DmmgjqzXj5DTlmlmCngVC4wvQLg46f/mrl/FSS1eqYYk6Rpzo+wRHzMNngtjXz4Sqv/1GpYl46SsFwpKWoNYDtVJG0n1rreYFSwrbhx3DA== 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=N7FhxAsju1x7JDRfFiXFpOtrJm/Kpj/Olha+nn/QV/I=; b=etMUmp5Tn3qO1VwcF6PFCwdQ1v4oAdGPBa5p+M2c3Yj8xS4d1IvnDbZvfAiTcbWWSGS4oL4E1feelopFe5buBxtNwwS2KDvVJz4Ho1t9zbcwJ3dBd4kMcYjxJAf/C7orkvK4OsZtQPnW57n8WJMlbmMskaRahFlrLhMOYS7KfE0n0QifjjgJPM4KXlSq7AMUfvEH5k75UebApNzzxFkeHwk1iD0sVz9LFmbf4VdAJs/MEDzeXeNrrFQ2Nb9GN4Meltif1RukutiO/acUo9CLj6P1vXGRat7yHPYNr2EUUj5dgNtmVjetZMn9SoEJ+KPdvJcRacbm8cN8WAUxg9165g== 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 IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36: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%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36:00 +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 v2 17/17] iommufd/selftest: Add a selftest for IOMMU_HWPT_ALLOC Date: Tue, 7 Mar 2023 20:35:56 -0400 Message-Id: <17-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT1P288CA0005.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01::18) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: 6370f496-6178-44c5-6b0b-08db1f6d167e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xfMoEYuJAYCsiRwhtQnf4RKfupp1HYxTyFvlBV/4KoJUmxIshW3QyM5ZdqYJo7Q8tNP41Z2baQwbBZvTm9hnj8jZoOqBefJgYzvzJGATMsPSZBLnrCA/n/a3UJGxmetS4EZSu/JM0VY0VksK1Ps0HKO/tkfLGFwiCfpFacTOqDAOr7dkhrqBJN0p7ieiFz0vTDxrkNR3RUcPA82pnT3UZoCy4wV49y2KorCKGOAzNxWCoRU0tLai2cwmh39uxZ8Mgadrv9b7uYGxmwLnGQ76uwOxF7jE+jHVL06IVXpYrrtUKOuUTe+lh/A4SLfM+esG33zCGcnlfGqpyR2705nijZw72HzaTW5jtq9AzgyAdgV0c0z3+ZDLmEXPzRqVpFXveOqvF0K6IFUTcBf8C6p2G11z1vL6ZCClHcoV6tnGgESsd+dZL1PXYejyZ8ROdQRY3FW91iVjrYhyuPwPQHgt8S93TqS2MRjbI2r9VREA8XdO6uP/WC89eVpfuuABPs2uvPFT17cCNW5laHn0rdkWMS+HssHTk2M7eFbPCwlPdHuaSS680OkdaciqR1smGq3K9Ok3DQtHmZpF4+68C+5VGk/6HxJopnHGaeb+cdj+CdfOD4Mt1Tj7bQE1tBrhe5WLjh3woILzxVag0yat9zyk3Q== 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)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 0nwDcAq6WTJvMvX/jwAT5QI7SgK0OiO4x6e+glP9Q33DJFJ56hmAUt4QlqmW+HWnCrE08wc8GWGbY8eTC5RvISMcYPn06FcZMxE7IRf5e+vDo9mwtH13CKlq1bsUZUcB9y5RNRDwmwlHr1CrvWw5W+JQE4hEiBzzhHOvutURXxJ0Z5EppetRBRYlvOl16aDsPXv7jznjRrbsWoPjtH/LTqlzK0CGS4CZNn3sz0hCTRKrZj1Z1xwBgmOKd/jFKFPAD2cYm/CrYDkeARVJTyxyNOrWtzldaaZDGzMsEgWYkUXKF5Wlr95sw3+6A6mnCG03ajnX3jDjxORXi4f8azFVkfXuRzNMXwsiVTv+HU6ZdS4x9H/NLZPgc2JCqZ5e13za1b9Dk3/kU1zB/b2PWoCKVT8okQC8+3dp059rXNYE4barHQpBkwpDkdIzRbQGLRIef17LUwewDtS/F5INtXAME+NLaJIqSRjVSl0YCn2JsruK8ne1i3iCsc+8peIP94M6yVLTzukqpFJoGlSZWXpl3SG1YrLTTX54wlQ4eSDqbImDUQ43kvILQteVXWaAaw+Sl7fRB/0c585X88mOjQIFEF5bHRNjcfRM8m4nVACaX2pkAxghVfLYQdTUc09hJ9ffZ5OTGegiCz+cum+i+IlxYjFU9RCvvYCKLogpUSULtnf1FZeKzD6jgbsKjhCg2sbFbzQHCmgUeSI8T60iIFfVH6QOw0euOMrb6dOqo3UF6nsGYYJ+VWNtmkIOVB0vomBqWA2p2Q3taSAXBO/AVlH7njdwVkJUtW/bygu906PkdZOww7lm0ZXaOGkMjqylIwMLpk/9I/aLryvXtCjGh4NCDbN4oMQ0SdWUwsIZM9wntj+KG5XEKKlCT/vXBvgWgeHaXiZJUSDUJBTScT2pP2UD2rJXCNV1HORH4a8+X2DugK07539cx0/FfSd/kkS8bAb+D7GzNKCwm1P82tH/b996VArkUWq3Bxnwj2e162pgZB1POVuSQmqTeq71LI65vPSFk15mTsdItp922dk4kr6m5M9Adj4N93ytUOgcOMikau+XXlhf1rXcJ8XvPY0pGMeG2AiAOSI4z9DdOWTDv7sTN1kafv1nNdcjBynynDXLaLm/L80xAEV8PLVgp98eh6p+z1EacGeigvARI5vs/TzW02MNCkyiRUvEmjRZ1mtoXLTL5WIs1GneEdtSl2lk6B36HTBNoyZp5e7W+uyRCmejFi9lAct+js9VxoDBCHqLcNAw+hp4fxq10+LHVNuBL+Xj9B2E4LHQ9PaJtmb0Nr/UqzQgelCwxyhTfZwpaiGq3sxGYPdJJVuy1UtTfqgpUz/YNHcBrLQHOb1MPC4XZ9xsB1/ejz/nlMN0+ySawucGSBVoysIHgBjI73hBdCkYNNsfD3kZPQD8i9Ur2zGFm25wOjRXgXIYPzn50jz0URChLTtB4cJxJbtp40nZJncmTw6eBBskJ2Vro8XJQ44dlWrY/w7mrfugv5wDmg7kH9xXUcl0WxoetqA99eTAdI+ABWWCk6QlWsqd8tUlmsCLiYJ+ZCc88hFcQuj3lswvhFnGEng5gsFGFGSCExX/4Q2RiWU5 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6370f496-6178-44c5-6b0b-08db1f6d167e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:59.3899 (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: +qzUFCT2sJtSpan6SVIXTGIwd8qo9NMoticpAHYOql+l78SoMY0cXzGq2ZZlZVxE X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: kvm@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 83c98a371f9e7a..c07252dbf62d72 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 stddev_id; + uint32_t hwpt_id; + + test_cmd_hwpt_alloc(self->idev_ids[0], self->ioas_id, &hwpt_id); + test_cmd_mock_domain(hwpt_id, &stddev_id, NULL, NULL); + test_ioctl_destroy(stddev_id); + test_ioctl_destroy(hwpt_id); + } +} + /* VFIO compatibility IOCTLs */ TEST_F(iommufd, simple_ioctls) diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c index 7a3d149019da92..7e1afb6ff9bd8d 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -577,6 +577,8 @@ TEST_FAIL_NTH(basic_fail_nth, device) uint32_t ioas_id; uint32_t ioas_id2; uint32_t stdev_id; + uint32_t idev_id; + uint32_t hwpt_id; self->fd = open("/dev/iommu", O_RDWR); if (self->fd == -1) @@ -590,11 +592,18 @@ TEST_FAIL_NTH(basic_fail_nth, device) fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL, NULL)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL, + &idev_id)) + return -1; + + if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, &hwpt_id)) return -1; if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL)) return -1; + + if (_test_cmd_mock_domain_replace(self->fd, stdev_id, hwpt_id, NULL)) + return -1; return 0; } diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index e67a929a5c87d3..9b6dcb921750b6 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 stdev_id, __u32 pt_id, EXPECT_ERRNO(_errno, _test_cmd_mock_domain_replace(self->fd, stdev_id, \ pt_id, NULL)) +static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, + __u32 *hwpt_id) +{ + struct iommu_hwpt_alloc cmd = { + .size = sizeof(cmd), + .dev_id = device_id, + .pt_id = pt_id, + }; + int ret; + + ret = ioctl(fd, IOMMU_HWPT_ALLOC, &cmd); + if (ret) + return ret; + if (hwpt_id) + *hwpt_id = cmd.out_hwpt_id; + return 0; +} + +#define test_cmd_hwpt_alloc(device_id, pt_id, hwpt_id) \ + ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, hwpt_id)) + static int _test_cmd_create_access(int fd, unsigned int ioas_id, __u32 *access_id, unsigned int flags) {