From patchwork Tue Apr 11 16:43:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 672542 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 847D5C77B6F for ; Tue, 11 Apr 2023 16:43:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230008AbjDKQnt (ORCPT ); Tue, 11 Apr 2023 12:43:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229927AbjDKQns (ORCPT ); Tue, 11 Apr 2023 12:43:48 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E565526C; Tue, 11 Apr 2023 09:43:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ONRGkcB3D2JDc+eLIhMITRSWtgWzA7G0t19zQ84pOWDBmVb01Ft99tCsfWR90In/jWBbL1fjlyyO3/DWPuZnOefUV9WKK80qZeN4eZoIarZWgR5f1imyyjGqjXcYzc3LAKeUXnyFGvpRjY1j+RK45rONHvS8Ox/R18z82DP/9r1XglhP28dhsTihKixKzElRLmoWQIOJd2n6Y4oYfSS1WWYp1gzFBdkJcRshf82e3S2q9e5nj6Ph0DYVw6OcKPm1NewF2bkQUjiJZn0U4SFFtwAlluJcS66EVPm5yQ4/toVwLzEy91ipjkmAIfbiT26AkaWUc1FpUZ1FCG8KZnETfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=CZKnolzfT66UVjhCW5P9kzq/7vlXczbFBSK46RGNX3A=; b=RqFaqhpKG/tDRJy/u/xsyBBa8GzLFRq1XcuZjSNfSd95E05k1TW7vnQQzaR3DkAOtZt9RITFNcXausWvICcxNjrrlJt/rt3kE93zPjCwyTm/5tIV+tgXt1KLvCF4BkQRW4A7nc5DwGod0UkaU7cMGlW5oKQfum5l+a/HzIXHf2/pcizT0fsU4PiDFMYwpAnB78l99GlMwq68bXPtVGNdbhhk5iW6UvdT8ljkOQYf5JrrKVd/hUZiUGF2lf1SqeOIUxaW7MvHz1f9jr7nMHGB3LRip3ehaaRr/6aJwZb+fxPlFo85dQeA6jgOLwo69F1dmXuI0cb47N5LvlMYlBacHA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=CZKnolzfT66UVjhCW5P9kzq/7vlXczbFBSK46RGNX3A=; b=WiFhodRLXZ9skqEHkF+6tEcMykjk182FfgK3WfBJ/QQ6xakwqPhBtRqiGDEpXQS1YaNhMqdHuUwAftCTG/7Ss95w+RsD0AlWUTqwMi3jcp0zZNThtlefpFf5/zXESd88zXOtDLcXS8ACKOGQAj/buzG2HnX7amCrJb/oa+gsWN102McZpohqKTKUkYTfwGv+ungDn8klx2VxP3mSCPA8FQSW/krq7qJ4fIPP1mmkmkiOgH30RDodDhVkt9R8xel2yN/gmImBFyC/7ZBeT7lDFGVGpEiDID6XVuDoVJceXeV1NwRcr7p5Tg6/Il8j68IsCwsvMAEhU8EN5tiW6hNljA== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:23 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:23 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 02/17] iommufd: Add iommufd_group Date: Tue, 11 Apr 2023 13:43:03 -0300 Message-Id: <2-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR01CA0040.prod.exchangelabs.com (2603:10b6:a03:94::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_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 661b45a8-44d2-4efe-cdf3-08db3aabdc9b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: eehLm5lnsBzOGWYVaezJgpenm+sWNFHJwwZ6K2JDmNKpfB495zrOq5RY4ylFTJAvfwUtKvfbWxpFWlT//q/tbUWN3NsnlZyN70ZYLF5jewQ9M6H8hJ7sNf/QTswhOQhzEYAOpcTN1T59MGMaaqNF8CRSzPk2psdJZXEc6kar2rqFAYyfbbDxXQ1Y16MgPI+dl2pMGhSLwA+LOBZvXyGtVbvk6FHv2dpVuKeWEfTmhrtuQVLXi7GyxIkBAbA15jy3OZgBrwY0v/UT4pbfz7srpHRNhz7V7j5O6X0Wi5Zyjh1AepUbsG4rgtCZiFpPOaHSpWm1IUY6XZ/nwhbE7PQa3Sb2SbeUnxFlvX2/0/TPqMCWQL+73Q1bznlMh74PGwSo/5M1MlpGul50wCMePacZv9Mz8PBzmjfTOMEZdplzlp6b67FxXNr/SV4Ef7726NDYCMQL4BEhkADfNL7BN6SVeBpg5voXuaCJAFsmTOt8bwoFnOyctzf+bscVM1LN+32as8XiRpTxdZ/uvHWnQK9Vsxope8ZlxxFkuxUk2P8NLVQ2fI5Zc8CcEqTbrjRbF+0PgxH+ibtjPRtDfu5r3EntUIT5FCeAjw4/O8bmOZG/+Wo= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: vatV5KZQsx2BGP2gwDs6D164tQJvmE6H/B3tWJr/RUvf6TxwMydPhNibvU4QFmvS61CF7SMAPZ9bPk+5sYmurV/NLXapwUv+0m5qgEAYiiNYGDkPs+MqX2IlrVxldiyQnyRIxWpMLUXK/mD1xuMcQY0TRCahVRjjdgr4woVRFvl4rRjYq4EyPZ3h80oH7yDZy4pRR2qDPEZY/4Zn2exBPEMfDc7gzPsIRRYHrvGhKnoFktslhOesDfs9iAv8EVR23U4XJrJI1piwDS2yX5m3oJERzUZ6qLf4koanMBfxuHMhS48ZQof2Ifr1l+DwYBdCZ1bF2l80PEAR5uxFWjUjBxCIH3VW2Qk4ZJSDvetIHgjqIeqFim28l+0LHDGO55GOPvgvb7F8aTJaJTh3+VRgceL5LYiKWqM0s4L9WUCcN+tdjJqfmyDn2JLy1TtnQLwkkgffYFlELpbN3n28HCWlsodOGNKcLXHABu0NdFDDAIfRQ19IYisQuCE0MH2S/QWmqjM/ooA6AXAZIw5xnXNZ+fuUU9UzvPNDqwMnTKHOeTk9g/8ei4chzmHa2QSoloVINrUAsXQgpio4v2vUiryAd5spzI/AgBipjq9ezwHTohSQdSMQRm8V2hKP9WUx2cKqAi50CzwTa6lDrFUFSISxa5CiWTfB7yhoFXQCt/RApvlFDcoRlogIZDxd4YOxWad8buV2J6Yac60M7CXvUn6ubDZ9WSmhp5mSDhlNNoqd70MnkCy+lNVKGWtLCbE7nARvw0JkhCJP9039MoMAgnzmqG2bkD5yBvobrRnrMTeBl9VXJzAgQ5CcLhsdQ8pxMvOxH7Pl1vfstNtw12AbXzedjP2k4Rd97F8H840iyH+8ZlsMoSq2thn1JVD80qSy/8G2OEIQl/mhWSlJlCOMx5eMQHF0PCyVh4wN3KFo+g9s1uf6pZ2e+DTXhH5mNS81HqBZ0Nh4BAlK9fuA0hUM+IgoKAGrxN/6dbfde8N4T5Ul2RWTsC9K4dcXOiTFQoQoaYWlO/oX77Q8YIr12N7eqmopQUY8TB35Zg7+yEOhOUblL0HVy9clEJRFrJP5ECxoUVf7nP6VYanifZQpzpURP/URS7+2jiQxGL2w2AVvQ7bx+UKi5WpXGoiIq2yYTARaC7hH282s9klGIKxrSQtKaMGeRsjZsZGiz9UvNfsA4/u5/OYFhViE5h00wxPqi5cl6XhNb4SV6XAb1Rbm5MvvH8TBAxYAn4L3f5J8+jxD8pZmlBJep8tMkBO7e2gPv1IMgb022GwfDpeu9am8m36TXjrfVIhvj05F/mSRy8CaMoI+kD+jeFD2DSIpIYkKOydR1ohgt8j7z7NpIWW+tumra2TGL1FR03kVoxvhVQP08Z8lJXabk6Q5kEnz/1oWU/pfjTScc344Re7MydogmQdsKAiT9XNvAGFqqAeMxPp04dC5LVBwkqnFJyAjra2hWJ4VnBI7pIeLEvzQ6no6i0JieF9rXFMaB2rW5q5KsOBqqpoZ756P64RRY/HU1/tFtjbUteZlf0lX1UawclBT48D6KhFmtFnOQlYTOXNj+vKmmgA3u+di+LwpQ4PaZk0prlO+S1Xp X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 661b45a8-44d2-4efe-cdf3-08db3aabdc9b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:22.1709 (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: FQATDTp6CjwLGTheFxtrQQEJB4nq9G82p41f1q2MooPmNwXk7+jF2ONFbzljX1uR X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org When the hwpt to device attachment is fairly static we could get away with the simple approach of keeping track of the groups via a device list. But with replace this is infeasible. Add an automatically managed struct that is 1:1 with the iommu_group per-ictx so we can store the necessary tracking information there. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 141 +++++++++++++++++++++--- drivers/iommu/iommufd/iommufd_private.h | 9 +- drivers/iommu/iommufd/main.c | 2 + 3 files changed, 135 insertions(+), 17 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index e66303c17c894c..3fd623208c691f 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -15,13 +15,121 @@ MODULE_PARM_DESC( "Allow IOMMUFD to bind to devices even if the platform cannot isolate " "the MSI interrupt window. Enabling this is a security weakness."); +static void iommufd_group_release(struct kref *kref) +{ + struct iommufd_group *igroup = + container_of(kref, struct iommufd_group, ref); + + xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), igroup, + NULL, GFP_KERNEL); + iommu_group_put(igroup->group); + kfree(igroup); +} + +static void iommufd_put_group(struct iommufd_group *group) +{ + kref_put(&group->ref, iommufd_group_release); +} + +static bool iommufd_group_try_get(struct iommufd_group *igroup, + struct iommu_group *group) +{ + if (!igroup) + return false; + /* + * group ID's cannot be re-used until the group is put back which does + * not happen if we could get an igroup pointer under the xa_lock. + */ + if (WARN_ON(igroup->group != group)) + return false; + return kref_get_unless_zero(&igroup->ref); +} + +/* + * iommufd needs to store some more data for each iommu_group, we keep a + * parallel xarray indexed by iommu_group id to hold this instead of putting it + * in the core structure. To keep things simple the iommufd_group memory is + * unique within the iommufd_ctx. This makes it easy to check there are no + * memory leaks. + */ +static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, + struct device *dev) +{ + struct iommufd_group *new_igroup; + struct iommufd_group *cur_igroup; + struct iommufd_group *igroup; + struct iommu_group *group; + unsigned int id; + + group = iommu_group_get(dev); + if (!group) + return ERR_PTR(-ENODEV); + + id = iommu_group_id(group); + + xa_lock(&ictx->groups); + igroup = xa_load(&ictx->groups, id); + if (iommufd_group_try_get(igroup, group)) { + xa_unlock(&ictx->groups); + iommu_group_put(group); + return igroup; + } + xa_unlock(&ictx->groups); + + new_igroup = kzalloc(sizeof(*new_igroup), GFP_KERNEL); + if (!new_igroup) { + iommu_group_put(group); + return ERR_PTR(-ENOMEM); + } + + kref_init(&new_igroup->ref); + /* group reference moves into new_igroup */ + new_igroup->group = group; + + /* + * The ictx is not additionally refcounted here becase all objects using + * an igroup must put it before their destroy completes. + */ + new_igroup->ictx = ictx; + + /* + * We dropped the lock so igroup is invalid. NULL is a safe and likely + * value to assume for the xa_cmpxchg algorithm. + */ + cur_igroup = NULL; + xa_lock(&ictx->groups); + while (true) { + igroup = __xa_cmpxchg(&ictx->groups, id, cur_igroup, new_igroup, + GFP_KERNEL); + if (xa_is_err(igroup)) { + xa_unlock(&ictx->groups); + iommufd_put_group(new_igroup); + return ERR_PTR(xa_err(igroup)); + } + + /* new_group was successfully installed */ + if (cur_igroup == igroup) { + xa_unlock(&ictx->groups); + return new_igroup; + } + + /* Check again if the current group is any good */ + if (iommufd_group_try_get(igroup, group)) { + xa_unlock(&ictx->groups); + iommufd_put_group(new_igroup); + return igroup; + } + cur_igroup = igroup; + } +} + void iommufd_device_destroy(struct iommufd_object *obj) { struct iommufd_device *idev = container_of(obj, struct iommufd_device, obj); iommu_device_release_dma_owner(idev->dev); - iommu_group_put(idev->group); + iommufd_put_group(idev->igroup); if (!iommufd_selftest_is_mock_dev(idev->dev)) iommufd_ctx_put(idev->ictx); } @@ -46,7 +154,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, struct device *dev, u32 *id) { struct iommufd_device *idev; - struct iommu_group *group; + struct iommufd_group *igroup; int rc; /* @@ -56,9 +164,9 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, if (!device_iommu_capable(dev, IOMMU_CAP_CACHE_COHERENCY)) return ERR_PTR(-EINVAL); - group = iommu_group_get(dev); - if (!group) - return ERR_PTR(-ENODEV); + igroup = iommufd_get_group(ictx, dev); + if (IS_ERR(igroup)) + return ERR_CAST(igroup); /* * For historical compat with VFIO the insecure interrupt path is @@ -67,7 +175,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, * interrupt outside this iommufd context. */ if (!iommufd_selftest_is_mock_dev(dev) && - !iommu_group_has_isolated_msi(group)) { + !iommu_group_has_isolated_msi(igroup->group)) { if (!allow_unsafe_interrupts) { rc = -EPERM; goto out_group_put; @@ -97,8 +205,8 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY); /* The calling driver is a user until iommufd_device_unbind() */ refcount_inc(&idev->obj.users); - /* group refcount moves into iommufd_device */ - idev->group = group; + /* igroup refcount moves into iommufd_device */ + idev->igroup = igroup; /* * If the caller fails after this success it must call @@ -113,7 +221,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, out_release_owner: iommu_device_release_dma_owner(dev); out_group_put: - iommu_group_put(group); + iommufd_put_group(igroup); return ERR_PTR(rc); } EXPORT_SYMBOL_NS_GPL(iommufd_device_bind, IOMMUFD); @@ -170,14 +278,14 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, } static bool iommufd_hw_pagetable_has_group(struct iommufd_hw_pagetable *hwpt, - struct iommu_group *group) + struct iommufd_group *igroup) { struct iommufd_device *cur_dev; lockdep_assert_held(&hwpt->devices_lock); list_for_each_entry(cur_dev, &hwpt->devices, devices_item) - if (cur_dev->group == group) + if (cur_dev->igroup->group == igroup->group) return true; return false; } @@ -211,7 +319,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, } rc = iopt_table_enforce_group_resv_regions(&hwpt->ioas->iopt, idev->dev, - idev->group, &sw_msi_start); + idev->igroup->group, + &sw_msi_start); if (rc) return rc; @@ -223,8 +332,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, * FIXME: Hack around missing a device-centric iommu api, only attach to * the group once for the first device that is in the group. */ - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) { - rc = iommu_attach_group(hwpt->domain, idev->group); + if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) { + rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; } @@ -237,8 +346,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev) { - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) - iommu_detach_group(hwpt->domain, idev->group); + if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) + iommu_detach_group(hwpt->domain, idev->igroup->group); iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); } diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index d523ef12890e1e..2544f10dae9aef 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -17,6 +17,7 @@ struct iommufd_device; struct iommufd_ctx { struct file *file; struct xarray objects; + struct xarray groups; u8 account_mode; /* Compatibility with VFIO no iommu */ @@ -262,6 +263,12 @@ void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +struct iommufd_group { + struct kref ref; + struct iommufd_ctx *ictx; + struct iommu_group *group; +}; + /* * A iommufd_device object represents the binding relationship between a * consuming driver and the iommufd. These objects are created/destroyed by @@ -270,12 +277,12 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); struct iommufd_device { struct iommufd_object obj; struct iommufd_ctx *ictx; + struct iommufd_group *igroup; struct iommufd_hw_pagetable *hwpt; /* Head at iommufd_hw_pagetable::devices */ struct list_head devices_item; /* always the physical device */ struct device *dev; - struct iommu_group *group; bool enforce_cache_coherency; }; diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 3fbe636c3d8a69..e5ed5dfa91a0b5 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -183,6 +183,7 @@ static int iommufd_fops_open(struct inode *inode, struct file *filp) } xa_init_flags(&ictx->objects, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT); + xa_init(&ictx->groups); ictx->file = filp; filp->private_data = ictx; return 0; @@ -218,6 +219,7 @@ static int iommufd_fops_release(struct inode *inode, struct file *filp) if (WARN_ON(!destroyed)) break; } + WARN_ON(!xa_empty(&ictx->groups)); kfree(ictx); return 0; } From patchwork Tue Apr 11 16:43:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 672541 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 670EBC77B73 for ; Tue, 11 Apr 2023 16:43:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229630AbjDKQn4 (ORCPT ); Tue, 11 Apr 2023 12:43:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229562AbjDKQny (ORCPT ); Tue, 11 Apr 2023 12:43:54 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E61649F8; Tue, 11 Apr 2023 09:43:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PqOIFYQNJK+3tEtwpfx63GGMAi2iJ1fj02wWdxbVvPt+j6V9GgA2L+eapcuW/MZybpknFyouhWHyjMYpMWYWMWilFZpJYKi94q+IlnGKUobKPaewFgj+9seiHJdW2zGmBaWbLPRwtZFe6EAO/c0LCrVZZHYcbYSszO3q+KFUBSWdGXHzM8qZaR4FOk0zH2jmBlRQ/dwGbEbcKBEVFXMyfOmb/FBr0+rwhOmIV+MtEh+Pm+zpomXzjgBnHIaa6FCJuqQqW6hBXwuZdbSh6ilUJBrGf8dJL1iAOuc3rhAACvCejfO2FwohJ171yLFsqHpp84pFSojCtHkSqianq2+emw== 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=VMFQ5E4y7XFpx0TNpN5drsychS1gbvFhFrR/KXAmAZM=; b=SG8Nh62OzhwmfFuI6v36uxDHUUCi0HibFgLyQ/aL6YOhJMqVRjumjbLIk0Cvdvl3SF8bKJKlmtd2jc4gDgcBxq+cl0xlj6ami7m74V4u6vpgbr0R4qDxbaiVPG4xrZVLjNJ5YlR7sypabUwGpfIev3SRjh946BTiq0utEMYVE4DbXdRHAXJO5ni/bbgiSi68YY4PBuiIJCSMsSySDMKWdIVQVteKQfpIbvuFZxpnVz+zufHpcu1i9EysZzKB18njhS2WJikqOY5GNBs/vymZ+B0buCgu/xSf+eWrdM0zNURMmJpBp98uP50nWvVEALtwjhl2nW4Bx6An4GLozyAq4A== 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=VMFQ5E4y7XFpx0TNpN5drsychS1gbvFhFrR/KXAmAZM=; b=Y7Hperd20uMsTPyhAhYieXHCT6DO33BNJN8SZuZbvYLlpw5cfBZiRkafkAPeJiR/KrQQ/KLilxD7GubejxeM1KA6k7hOTSvKhRTRPznHqTsn2w00Zm80jaHh3v97q5l+R87XEGWq8RBZVwx5FusxyzrMiEycyED1gnsw9S0qzHj3TMyWelU5wwsiK6p5Cz4gZ+iQM/114RGSOu7+Tu3NxA7wpnkPh4Ecw6lXGGvX/1tvsba2pT39ysUuQiBMR3fZh1y8J/ggyoIEWtAWkZzjDUR6mUxNw4JXrX6u5q5oQPZhuZgFSxjYcYowIr2DFVB8LYCNFi28cO5a5UWtw0ALcw== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:23 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:23 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 03/17] iommufd: Replace the hwpt->devices list with iommufd_group Date: Tue, 11 Apr 2023 13:43:04 -0300 Message-Id: <3-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR03CA0097.namprd03.prod.outlook.com (2603:10b6:a03:333::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_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 786e2856-3244-4bc4-a448-08db3aabdcb3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: oV/jLNSwcxMkqi7j1Gr+UbRMbeJtRK6CGs3h10rnSC+0Om9kXtM6rdza4h5lBS8Mz/2Pyvpm8BwLcInU4lYgHctPmJkSecsw0BqjOphi7Aa3DEhqi8walXdgidUSs/RINM+P6RcKhwsp3GMXqWJM8SBNaBLy2T8dfhpApwa0miaG7d60o05OW7GcLaIO68FVfQ0cvnEyq/1tS3vOODwgyXtTQdP9m3R5sbOO3BUlPbQ+ClMiU7ZlaabsU3cKAEquzoImBAEFstjNbprVfhZiRiLLOyTb/E0FXTsnytkFslwaePRTRBjHjmZaBkGVPkSQLwitv/TmsIvcrobPmhAXCDLvC3v9KBGzlE18jG7j+TJpOm6OI4Z3KQUbxrZylOoZi8lbZ8Gg6t9EnQpG4Sm8qdGn1ocSv+3be6gKopAXxYcyPU8Vkg9vm4dOrkKjms1BQQIdAaHw2a+k7+LwX/Zpi7z2xuac+bBvZRw2QvGr1Eqe/KhV3VODP27wgvs54dEmZUmSUnNsWDtOUWdvkeI/Q15FzGPj1E0uehH0uNAkGccZbMLE2KOHWpBvDhvS8RjiQYIG8W4glFd7CwXVqyWet7Oc142ysYJVRyyQZDyZ7Y8= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(30864003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 4+m5W1FcxsQXIXoa1z4pmuSm80js9PJXHNPNJ4uvRE8K5CL7bC+JfZns+LV0UWPUlmRMQE8QUpyKvMkvyCC4CYLP38QKEIezCgH2RWOC2TzY9gbUN4IHL+GWQzU1AE5PdIh10fVMD/UhzSusVLNuTNWCEc9Y3pamxw45NpatIQZNY+Jo63bCyDK7tNUobaa/hlbEq8h66oTwGBHOB+0PvcgvlYkJl5b90NKRGCgZxUdlYMJU48/3K1xTfY0L6p6oImvYkFroTK7MqMCk8nBiLlviCAzwg6IUqTOaig/4yhEvQNi01QkBOQcIGKEV3anWWlLFFDQf8Cc2GNtBWHF3oiFjrWh2l2UkuSMfYQOMwMn+WLeZCHNE4zKIHZe1FUjbnjmRG2My3hS1xNfMuMpmF/KP8BCfRV9a2yIjg7S5hOzH72GeaObvIaj+ED72h917TvxBjDxH+2ALLuSKE+tG8vkrEjnduDsSyKyHMJciPPOSV95HBPMppzPEdEAhe5zgXneFmi7iBJCXaE8HJjZ+YO8NqtJ04XArjDSFEZ8Sru6teh/eiHk3daO2hdWZqySsorcCAV3wsOHIZhssZuZiHkY3aX2M0J3OVVeJo9JvxhNfsXHpPnK8O80s1Xf8VD+2seIDDEJnn9GF1RpLjX4U3/ECWmdX9Wj1subv2y3MfeJRxjklUceel3gypQc3IFCN2+mgFRxMSH9hNAq74f/t/ufLEfUwNVbj/bE8MFVniaDN+9DKUiNebtxvgN5b0a5hBZ/KlFqc5jCYF2aPlR7JPDfDNqo1EREveGkwn5d9R6O2znwDEGapavN0VR5k44XW7X8s++vmb6YYwCTYU4aAa3IhbaOck5O6sVchSB1fQseREuQiDt1wSu1OWs4cvm9iK4LdCh52TMDc/u9bolsKKh3KQDqgzPTElNiINs2aVhh6Hcfk16yVUXTF6+pnDOqNYflwzd0ScIaxBtRA4BJVVNF7iCGgSKKeDUgi2cz60uaNM3lNMCKjyaugcIgexvYfY2saY2hlAwNbN3hJHHTI36lpWeWL8Am5nDSwVmwiNqg97ajVXPT81U/vYQVpQunj78joN1H0zSvIdhpzd5CBnAsx7wwT5mKUzyVGYub7W8smTWlS9rCEMvZwakrT5U5YW7UW+cQR16Q0vGBXoG9lJzGzxNFKSQPXUsE1cQBtPqFEW+84fS5uq7bfbMmm3Wqo/NJXHvoCsXIDoCw1oSFmOyF/ao/3aC5gJXQRl818yB31V6GVZuwxSjzKb+zhri90I90oqqhP8acnVIfeMPoNmKg1vbr+HTl+hf7iJ6Ad0PmWp/h55cm0emvFN5E9yhS4ld8lhNga73mEGtF3dreXw4Qxe/ExPFk+nh6bD63P8fxzJFpXwQfeWK9EGSJbmfxbxol60dCmx6/+FAFjwKn3SMJvbgltk077HOBYYDqAeQ1/Y/jFLUbnXfsuTkZpX6GKmqoHJck4OmGOjt2KlGTfK0aoA4XWbGW+CddzKShJI4l+V8oBr6GKV6xCBTyGb2gvRKLgaeuTCh/a5EdRmjtlvKKUv3LIt8DjW2uT343a38w+ISISDzl/fxGQqVu0YrYb X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 786e2856-3244-4bc4-a448-08db3aabdcb3 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:22.2641 (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: jLporIcboHvjvBDN+6UD16wCMy9BcMkkSpyFt1KQs46hm6ozW8G48c62rJr7myHJ X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The devices list was used as a simple way to avoid having per-group information. Now that this seems to be unavoidable, just commit to per-group information fully and remove the devices list from the HWPT. The iommufd_group stores the currently assigned HWPT for the entire group and we can manage the per-device attach/detach with a list in the iommufd_group. For destruction the flow is organized to make the following patches easier, the actual call to iommufd_object_destroy_user() is done at the top of the call chain without holding any locks. The HWPT to be destroyed is returned out from the locked region to make this possible. Later patches create locking that requires this. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 79 ++++++++++++------------- drivers/iommu/iommufd/hw_pagetable.c | 23 +++---- drivers/iommu/iommufd/iommufd_private.h | 13 ++-- 3 files changed, 52 insertions(+), 63 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 3fd623208c691f..366df246d4d1f6 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,41 @@ 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 + * should attach every device individually to the hwpt as the per-device + * reserved regions are only updated during individual device + * attachment. */ - if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) { + if (list_empty(&idev->igroup->device_list)) { rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; + idev->igroup->hwpt = hwpt; } + refcount_inc(&hwpt->obj.users); + list_add_tail(&idev->group_item, &idev->igroup->device_list); 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 +363,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 +375,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 +402,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 +412,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 +457,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 +468,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 +485,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 Tue Apr 11 16:43:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 672538 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 14E9EC77B6F for ; Tue, 11 Apr 2023 16:44:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229580AbjDKQo4 (ORCPT ); Tue, 11 Apr 2023 12:44:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230064AbjDKQoj (ORCPT ); Tue, 11 Apr 2023 12:44:39 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A8C359E7; Tue, 11 Apr 2023 09:44:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fV/Pt3JYrLOsbbp4Qdd2BtGVTWSVTEpja0N7LyXz55qhRpeREOVgj0TmCyVJ3U0iRMM+/XqrSZjbR12r9qjoEzzkDEKFwEYLQMnOwNxHYks9sDefG0Ba+VIHVVYdGyse3Bs4CyM3xvtZJY41CuhXW96Wvc/NigzNqOnyCzD/9uG2hoRjh9qQscduEvOv4vYZIIfKPao3WJx0VDWCWxqGKYPNUSSelL6W/L6JkkKo/YPkfU7c8EOT2NcBW/B9nHDdbVqLLWTrYPWjWZ7hcKAqmsh28e8vJFX52HPrmoWbl+/Dl59UlCPJ1iuseMmTNEgisvis1Q9KxmzxLxqLKotVjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=MhnQUk2S8XQINgCIGa8jmbCAbr0NSnVgaNKgYsPyH9U=; b=YnJ9Z068D+cbe/uRfZDN9uMjsh1qVINxf7w5a+mF0SR/xFikgJy7Cxxqph3Eu1G43D/2pCnrWSSGrE4L3IfV0AcT0a+PVEAXnr3yw/xL6H9fEQLTGXC/mUi50VYOLvBP3i/QwBjbRNDV9veJrNNXVJu/ECUu0NBgFixJw0556wJ+3sWeypyY5gs8tY7HYjtp1pLqLh8rKlwESvZeEdaRSA8+W1WUAa4izFGxzoPzLGlefn97raaDXCm9hKMM6bmoYsHNSF8zxjORP0j+WEmOS+0/Qq/dTmvW8IvCBC5XPK0Od4AT9CPBJXhiHYz+WBORZ5LkYpLaCZArZkJEJnhScA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=MhnQUk2S8XQINgCIGa8jmbCAbr0NSnVgaNKgYsPyH9U=; b=iZd2D2OVGvRcoK/F9ySiLp8YKY5igVYgstJsj7r16iRIWlPdvCcSQNtXmA6x5TsIw0XWUWV04W3/k3nRDVCJZ4UsROgJSKfGqxTrkTSjmGVmn52Dy+/QTucTp3QHeweb+U6ZQMcCcskVE0V/8voH6vfKw8qc9ESWnfM6H0zjbnf99a/zSzPDC7xhiZd/hazadFS6QxGR2EKUeWDPFAnDCCpn52Ncc3GNsMlHXkauKbYNoAtX/OZ3+5Wtyve0VH+1mW0WN/nA9uIyq3sgtmgTcb4iZhKG4G58rPF47W4a4t/dmQSA1Elz3a7+lGv4mvWJ9uA8qE2b3SxPeq9rvAWWgA== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:26 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:26 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 04/17] iommu: Export iommu_get_resv_regions() Date: Tue, 11 Apr 2023 13:43:05 -0300 Message-Id: <4-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR13CA0169.namprd13.prod.outlook.com (2603:10b6:a03:2c7::24) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 4fc4b219-5aab-4fd4-3c4a-08db3aabde82 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hk1N4R71Tz4L8gCrXZloZhz9RDXouFDrUMe/Dsau5thfMkFS/rdEbYhta7ks2y0esgFkNczDVaE4Sz766zdueyF7ym2srXh0Pshf3p391+3e8WaF0HWJRIpze9Ic/NnOk/OsZIGDK37hRXM1FaDhgxxUIYpywxCfLltf0jK+mjcnFd3lCjgfe4sHbOX+LiLNGR7m+MUwsPys9QHljO/e3z5RW3TqKlswvRGqkaLR02gY++qO9OYQAiiBR7Vdgzj7NVaxspYWPsEm3BGw+5bUp7p8CI5Zr7BXSV2zm43tNCFt7bU3tHa8g6SFv+fFaZCoa+tw17bfjkAgu6o/M+81JEvuFQqDpwGSfPqex84nMfszZfsWeuavD9lzXNhDiX7kVcJwH/A9P2jFyUizADkA++kg6lOr+XVBJ5RfX5LoBc0jjzVX3LQiOgLBBS10byxHuDnf5fyD4/w4sW0xo6K/iRsvSKrRquJFxusdcICZNA5ODdKFV6PAXkhq2pq7Wfhu5DLEJA5un3xOgAAvv460kh7WGk/6B23FZ5mTrbzioOpBXW3tHtbpeiQtc8TyjCpZg8HNPtnTFwObCXO00UM6P/lIH3HCtjcw3oGEtPSU5ng= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: C9OCN/vM3zNptu0Hn+bDWa+kl3ynZo9K51CUUraKrzia6CNX1Ta7FtRdI5ZJN6G6twrq5OsCAfducMnydUJKUfpTdgE3AdzToer+aadJBUmkYymCvhEKuq+sO1vCmSnjdjDOjoee7qZ++XQvfl2XZ0qyM8Gty3i/WQX0dkWyB7NMtoksiGwIuWf1qU7rT3sr60zY6zl5pC4w5mRd0rpaBGtFAFUmMSuTNACZhc4cTAEBzL7GFATbiTKIwwvGl+0Jf3FsO6GmND1CrWpjMYP+7ynZSPusXXUwLWESLkbox1Omn7tmmFr60tSbNIzvSs3Y3YsBcIWoibvei3FBlmwCg6PUqRJrp9gC1Qp+8F0amSlnU4NndzkL+pz473fEEWwDyzh3EkZhkRDcLwggkCmiBg0FPn3aQDaCNkbro3/L9jtoi1FTYASTGFUEvbMp+55i99nJ45ggNMC/JY4GaDFQxIHPChRBVNqVo2NuL0iMfUVHr7YYLradBo0HhfWsBxVnNXaUwmrTtGaIkNeBYUlWScP3NGh0qUr9X6l4+uAudMnBUYHhq3HbPq+bJPitDX2LiCxMbseT7RUn85t+MaDymiA/lJm8ONFO2AhTp6W8tC2wU52wVu+RZ+HH6hN1h4iPt0w4s79D6SYKVOxtPCwXhrsXSkY01hSC91kDygFMcYmw6MOo/dBCflttrNXLCgbbItc0x1cMcKQsQX8cvG6hbvB/aLe6V/tE1naNslLVySnukXaUFQ15p/RW8S6yh1RQtL3YC7uuHxanynJ6wlNkdcBA7Gj0UnYJEiVeItxJYNTXrcizNnDrDjV7omw7WICSKF0gk8FVPMxdpH+hBpUgfrrqAQmXCfOEXqUCu/d5WpP4CuA+KdsWucvhlFJyytRU+l5YFjszv8FeH3c3x3G2BdgB8D2ECFle1gAg0bNl+m8XgayAQhS+e448XiqpYI8s9nrQQEoLx2Jbwl91au5rOf20NAd+q3Q94VA2eXyYDNetPzviYpIKVxyO0i+Es+qK9/q6TTreAMEiuGkjrYvHTofVI7smkB/sxwUywiqP/PIzYJhLYZZjB2veK8g5FEQUYuBnMuCJdttuB4GNH55OkXfiDe+LicVvhQ7MMzH3wCGnbjw0fQcQnwTV6ATWn0jw5peo1Tq6fmL80z6g++KK/Yyc3SNahmCi5ZNI8zTmeHRsOG1kFdLx26UlRW3MCX09Z93jDPQnRrdOBpff3a9VZ2+Fgi3SZ0bnH4NiXLZhmuUvn9T28b5/wCwx78TIlBq43GyR///jmnrO+x4XazIt5V6/fS3aALj7S2rgrsmrCYfm5jJ96AukjN079ZEouXXId7cb3yHKO5PJLgotb0lTRe67QZUijoLRAIwRD9bGpTDIN/y9+gy8aaqiuTWn/JEy/17lZFmDWpxLDqpzev1dE5jLbpZ/SfpBkG3l8cG56XzNxHDAilc1HyfPx+zjGdGpee34kZr2mEorhzsfALd7j1fGVh1yeDDt5109wDI+mOIExMaoe7+RFgeAUVxSGvMwaVGA+KMa+AOi03gHNMwvSryoTBYUoT3fc5vo7O+UJdKhXENIEkyQYLA8t/QG2jbI X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4fc4b219-5aab-4fd4-3c4a-08db3aabde82 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:25.1446 (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: MZTanoVXJx82isBOvruIyTDLEwpGTjPhX7HaTXHu5ep78d1zD6ZUZcv+RkvAH5NQ X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org iommufd wants to use this in the next patch. For some reason the iommu_put_resv_regions() was already exported. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommu.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 10db680acaed5a..76969904b93af4 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2639,6 +2639,14 @@ int iommu_set_pgtable_quirks(struct iommu_domain *domain, } EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks); +/** + * iommu_get_resv_regions - get reserved regions + * @dev: device for which to get reserved regions + * @list: reserved region list for device + * + * This returns a list of reserved IOVA regions specific to this device. + * A domain user should not map IOVA in these ranges. + */ void iommu_get_resv_regions(struct device *dev, struct list_head *list) { const struct iommu_ops *ops = dev_iommu_ops(dev); @@ -2646,9 +2654,10 @@ void iommu_get_resv_regions(struct device *dev, struct list_head *list) if (ops->get_resv_regions) ops->get_resv_regions(dev, list); } +EXPORT_SYMBOL_GPL(iommu_get_resv_regions); /** - * iommu_put_resv_regions - release resered regions + * iommu_put_resv_regions - release reserved regions * @dev: device for which to free reserved regions * @list: reserved region list for device * From patchwork Tue Apr 11 16:43:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 672546 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 AFB56C77B6F for ; Tue, 11 Apr 2023 16:43:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229835AbjDKQn1 (ORCPT ); Tue, 11 Apr 2023 12:43:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229651AbjDKQn0 (ORCPT ); Tue, 11 Apr 2023 12:43:26 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02B83526A; Tue, 11 Apr 2023 09:43:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=J9Lj8uCruHkD9CgSkeCQXtCw4bCgszVillP95YFkwV0VX60KuYlqJ57g1vfrAy4giKfvtQGlx3fBNAznGrY4KDgkVIz/m/qujf924untYbvxwpO84LJ36KWps9ZGms7tWq3BUx35Qg2D+cSDMks38PAfB5tpBej6Q9YvabbPoHaQEvyO4mkkkL/rc4eYwl0ll58HB4YpRNxu5O3Rt4amrZvTQEf6G5Fz+F6Yicfmt/MBOS8uUJG1bxs15LbnuiJlucl8W/tt09tLab1Qzkb0+NC+bGDGOO86/gm5L/iE4pNQ318bZpY5MaNRFRfkuSVruxUhjkBpwp2Ju4JkhrHWAg== 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=YKjugjFWk+NnRMEu//zOrspSb5+B3eyWSMVIVoXMZug=; b=CvJy/ErX8UWE55wpKf8YBvhrV3w4aCPm1Q+4W1mKX8EuYQH3spP9hKtI3oyE2S1S5zZtoXB0TQMORaHMsZh2Q7Nx/QTKZmpRtVsePc1Z234etgnf6QwZF+ekdUkCgeMLURg7j6NIS1sR3MPB5pAnxGw2tGSo4qrzXFmutjU/7g75dpRxG1vE6PD7vjtzjh4VTc8Xhr98Ab6pmvFDw3CWIlUmrBbqvVMPE2bdqA5N/M+mtF/ucPuzwyw9CbNSFlNuVCSbnfUSCwVo2o/r+/xNrKQX/1BQzBHgKzXM6CwP5C3+31exuBouWKpQJ9LSlSHtjh9M2E75E/L1Nn6motmcGw== 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=YKjugjFWk+NnRMEu//zOrspSb5+B3eyWSMVIVoXMZug=; b=pgUczT0YoNOQJOqzNT+2BasVsS7evDLPDOGcHqBbTK5LhPojHfytYBIanun2u8usOzRLnRA8o+hww/wkDMKM9rizHbS+JntC/g2sMsDuTsQfEaXl3pWjfzvh5EO81QEymMnWUr7mTlStRAZHBUEwyZsnmwKa05Tw325tx/K1Z4gwxPSaVZ/wa3HDnK4tIdDnC/DdW63mDyA9z6ixmh7VZ0GrJzlAhIz2KrPB+sRPsjhUBa9OF+qBpu2QlyHVJvSyzv29RtIRylKp+5FKqzYtU1CHDjMkgCw7rNcK/SC+liReeBI0Vvvq+3AYeh2YPGDs47jedpHflY79R8F32JXxhA== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:22 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:22 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 06/17] iommufd: Use the iommufd_group to avoid duplicate MSI setup Date: Tue, 11 Apr 2023 13:43:07 -0300 Message-Id: <6-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR06CA0008.namprd06.prod.outlook.com (2603:10b6:a03:d4::21) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 40dca913-a6fb-4fa9-71b7-08db3aabdc5b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: XgojiSt2K5/y7ourEWtwLQpqPMTM2ztljIAFXNY0qDD6sVhQr1beWA3LOV+CsMBa+6hdjfq7qnOtd5mvH4oK7MJ0PgjsqG22xsljfP+iAGkVlA91X7l097gekZhw83bq5+UwIZhp/DZDAM4X/tLgSecrDCcwLAke6ZTBQ5sU4T8O4qzltfOIY1VzGnGcYHz5BL9CWDl5wX9GBpiJDw5TSt/YnU0ZHJYDVsPxDP6wLfGGBFoRjKzhJqcEIEY5HDc96yZ5/4numzWRm+jnyTQhk0s9WgwGXyutJKk4b8DNjtkYl75lo06Uf/K6GKmI0/235eTVCkVCxg7chOBREqcGIMr8uNN8zBHt276c6iF42YxAo+TvX6SLR1jw2o9EAKJcN0P84n8qGezXguC4uZHrQpfYjRoXlKeZlXFRxwpX5Sm2FWNHdy4ytEl12nGWovZDGuF960w3+jUcUgN7vHQrP8fpshmhNjGHkXyJ9zenKgwLPHmbEB4/TFInpsDYvlRT+qa2w2nA6GVF5MqiAQfi9lr1e6MZAc5u4qO1FV618jpPCRtime3cgHZdYdC6RlnLAELIglzk1lToIspA3CZD2B8LdfQE0PSGlLoaVmApYrc= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7MD/m+yJ13jjrraXZbGoDeARikLWgJ3OFUlQva2B2wD1WeCnGPgfmOTyIsFSx7J/vSuryb/DkeEztpbONtGLgB1Y+Cv2bookCyu4caXafScXSJ2UEB8Np2PqqgeD++8LrgMGMMEm6zocmGi8lhXahHKcc9rbG7NWUiZAREfF4yTtX4qRJ3wW/X4N3HibPtAuDKk4Ne0lGE2Z6AjrTNKUOq6sJJ//ykLXSwNgBeubiZJPt74q0EHS9OaJEQ1OiuwOKW53SceQgmVEexBgwGGZJQAImKpFugHvjToCD6WbSyzgaPpmIGksIrFwqScsZVrIEPqmuro+YWoiXJGT4Mxyilq47EM8UUf727Pp9RMx7Y4CmpRAAPq5cKmBjFf1lcZrwyDVRPClY3qlL1lTc+rU+/6vQWK2B0MifSBX1k6sFCS3SW0U4Vhz+mX/onvcnjelPKQhY78D0XoDRKuiDeq1aX7DpEvt9Z6kjwF3usfCbk5GgDeytxvTOPmUmOWePLF3z+UXVcggp8taO14H79oVKsW/RjxED9UYOko5CXCry6CkNzcbmPHzsOlhx6e/Dw2w/RoFiV9TgceMRBbTuufxR6k2JkO/WrkDmhDvBLKTxdYcqYA5ZQV6ZMpD6xv2UQsIMOebEac+5VKSkaAOUizhwPylsTFev5sSXvlScImh0ZX05jMwsX6VPIyGGv7bC2HRjG3AZIOq3yAk6ghxJ3WVABuCgpg5bFlXoM3tqgzMyxhv6Sf0M6Imw+3hx4is3oe550cDMdwPRfdAfQ9hE7khIbwBGQ49HyqA9q7gGXrs+8qjV9vmLJwFMGk/b813N19zYyM/RKCmkXS9zimTWwIF98jNq53Sjoezl3P0gtr7tmfiRXbRE9ZmrcPvlakUdp50N1emwZNUCCp+dUMexCkrYwabqOQi3f/mT7aemEulfcixARGUcV5egfQTn1B7IwpC7IDMt+IZ9TZRi7OcQmvLRWljWv2NHSgEWzUwpFmV8E723G2W5Rj3Ua9tPRWHgZx67TK5As6YZbzQ9jmi2cf3myMiF1v2dqstUzH0e2pHkF4xfVIVviRWHO68MxkhRGDN6VCjewpuzUbjCF7A+5/RV6leo8rcGebZ+w1h2w/v9Bgg3FnUoPwROAtATVaOzGzSTgVLaRHvLblLXNtouascFXcfhVEG5RakGBT715SbaLoTeN7BMPjsawyrg5RVbmQmtFm6dUBn4S3KVYjpf61Rzwtf1I52Yt3PfKjtRgvXXf+U2l5K0J6tW+PmEMiKYNPsBnpkTidXu/06jhEPdP1HHyXf5Z8sUo0/qsk+b6G6FZ8+QpLJgHf5AVkWEKdTfO6dH7wcgPRGWc+WzIYjPSoK7sFft9uxId20mT2pWigFoBwxXc1XHw+6YLJuuCpuGvTZaxep8z3nz4loiLDy93b1hbtT58k+ZAtTZ4czsJy6/qRdPq5XoYs7DsMmNc14MUxz4SLZvIOtV/Edw87NsZmKKFxVjNZp6MCdDp2+Cj2KaNLy07JoaTg7KUcgHHE6dcieo0omNtkKfZ2+zO4XHHO95AiaZOGt96fnOpm+yHIOMKSx6oLzQx52KWW/X4juiiWu X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 40dca913-a6fb-4fa9-71b7-08db3aabdc5b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:21.5910 (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: HGd2tqN5yO+E7de7tAlnyK1HHpRwOKDxogctmUeTdUuwWOPphpIFOnNZVGxjH4fj X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This only needs to be done once per group, not once per device. The once per device was a way to make the device list work. Since we are abandoning this we can optimize things a bit. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index d2bcea7d9da815..b02f98dc02c284 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -315,10 +315,6 @@ 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 @@ -327,6 +323,10 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, * attachment. */ if (list_empty(&idev->igroup->device_list)) { + rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); + if (rc) + goto err_unresv; + rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; From patchwork Tue Apr 11 16:43:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 672543 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 24C78C76196 for ; Tue, 11 Apr 2023 16:43:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229580AbjDKQnn (ORCPT ); Tue, 11 Apr 2023 12:43:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229848AbjDKQnl (ORCPT ); Tue, 11 Apr 2023 12:43:41 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2069.outbound.protection.outlook.com [40.107.93.69]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E80C49F8; Tue, 11 Apr 2023 09:43:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fvmY7uhMbucHNDo6wmCm2ThEDg67K6pptOqPjZJjB+CW215lXixjsajGPj7QdWsKSNeZW3mavm8i3zZGpSbqALWWl4PHN8uZ6qbu2mRrs3IZIt2v45JCI81RpDHcxkMYB+gsbdgQFydv5avGhxHj/0yXQ4YR5npXo0w9Zsc3aGKc4bCuySfKySBV0xdPqoyiw4JZvRoB57vcb3O0bX/aRQvEnAFbpeVUtHKENmttikBiIIfzMkWzxwACAAoaBcjXdXqlYlqAf2tfwF7QCwF3kfkrywZswcPtydM99nCyXvWSxs6hBDswe58m5MTftpE6xIBj3j5UPon+ezQsOvvf+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=zOPaiGig87vimgHT+ArD1kqF9Z2jpRmES37V6Tdvtpg=; b=RkDQNgFWZpKnjeYMA2URAPDj28dvEW2OxXNhqWXvbo1ewmBFwMay0FWHIBYoZKVq4QP+LtlUs+KDuViy3bMr6HojeICRYQRwsQal3f5sSu6tCmUxuGy7YmPFDk3q22VtuRywOHIJdPk638ReM8aJ0jkknZvGICeP/DsC5DDrDr4hgl8pigcR1tvk+c4bm0+Ue0t8S8vMiMP/ZX5E4RNxdmuW424RSBDA6IHyREJIM+9rZV7HC0nBarWqH3xfJ0dcevx+VavI0XMvYmxPBHxv3g1BiRM3SHzIMf44X75U80/B9lkPXmPpcNu8JdHTScPFFRCR2gzIZOmxXOjzzU+Egg== 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=zOPaiGig87vimgHT+ArD1kqF9Z2jpRmES37V6Tdvtpg=; b=rqdXjQkpMjTPp+G4UDq6FChbFsBSsQxfSHFOxx+XgH5uLeBgQ0H1eJBwuhq2Ic4fKlzbsX+m9SSlopwNbGQLruPj5ILC9WKUvqsejm0tSZBfLjd3x9MQ1lJ0ifM4hVX9fx1SGz9fQMNfuRINvsB7ujgAezeiwcqhaC1/seERNHsU0J9ZQ+GICALHKZCVgp1Yjf1Swwt1lN9LtqR9tUcf4wg5owELuPuGSzFes3Xx1ZZrDU9DEqY1hu8eNommqxL/3uEvJHR904tgFXVe0PT6rnP0fyP/+euJpACqPTWokkVpfqaO4xRA1VVq9qUoJ75yEj2xUzmB60TjImPcoPH4Dg== 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 MN0PR12MB6077.namprd12.prod.outlook.com (2603:10b6:208:3cb::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.38; Tue, 11 Apr 2023 16:43:26 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:26 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 07/17] iommufd: Make sw_msi_start a group global Date: Tue, 11 Apr 2023 13:43:08 -0300 Message-Id: <7-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR06CA0013.namprd06.prod.outlook.com (2603:10b6:a03:d4::26) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MN0PR12MB6077:EE_ X-MS-Office365-Filtering-Correlation-Id: 1ecadf78-26f7-4790-3956-08db3aabdea1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: UoRaL4kEczwbM3Iya8pVAALyFl0S5Lu8qGe35lKGkPpp/nECvdynRuMr6YxzQHszHAL6/CgHNpeW0WTghHtuhgpj8vWDveU9l1W/2rmQF0UMKM1hdyCmDL7K2MiFhils42xSDpp2m4676P0haZ2azj5eyjPFYUinLzO3PxlaU0UsFVkOBncLTSig+n7fBkcaQ/Qm3jS2s1E4Ro6NVgVumxmlY0Fiu7G9/kRPtPKiRDDqUOa0MAA1ugQdfhSmpZAMkX/b11SA4LeEm/dhv0soSzekYd+9TKK0nlEadfqFeQDLvHamnlsHJkPzRKAiZwJ4I2UpQQTOMkhXyp4IOFe1Av2Lw9Lnx6B1Yd7XogzomTBG5CSqPkXUAUrV/6l2z3SitXROglcc4d8Jx9nj7G6yqFQfdLcNAoKLwA1kAtincI5W7Z4aafmMBaODHvbszjOYIFr2M94/Qd16aeLHdA4uD1oJJS6g1N3u947Uo9guW0vPXP4KGn4EPn8JdXPjecitqTI4bBJWZXSRqw+WKrZrY1PP3dRcoc84lzb/VEvXne7XtdB7YbZqbXR6HiBV+Bg2QDHN3/MnqcGRCxqU4eNWTLmejvn7Ss9EKYjBDjomH04= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(346002)(136003)(396003)(366004)(39860400002)(376002)(451199021)(478600001)(26005)(316002)(6506007)(186003)(54906003)(2906002)(5660300002)(4326008)(66946007)(8676002)(6666004)(66476007)(6486002)(41300700001)(8936002)(66556008)(83380400001)(86362001)(36756003)(6512007)(2616005)(38100700002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: BBhr/1Bv+fWcDzejgjViz3a8d31EePTlzfnzcB78pL2APIpVtJfBmmBmzASBj9gwWBYB0GFGiITk/zJh/CD/PYSN/5z22QTiXqpNt7S6We/c3Lg5/a7/22CcXNrX0g9Q+Scrl1c7DsU3Ne5wn6EDQsRvgccnnlMhGr07FxvkDC//MhWSqj1cXjPxWxeZ+vLDl8FQRfIqjwUSQU7eqqQAFlZkj3tz+BXxHcql8ltwZlrZHBdyut1qDvZR0S6Bn5WGsQDe2bNi5bwKilyG79A+364617IdpdURl7QtEXPcFPH8++vuuAIrDpOUbaRODo0EG1/t4w7eaIG3jIOhmEyy72Igq90P1pLLJ0xyoVtpPW6CXOPSd9q6ahmYLDiO3G01goMwlxGvQ+6w91M++oG8yHHgWRoMJPa2g9lywn8k/qySMkeg/OyUFz43wSOQJZyk/+ij0DSOrHfph6c1vxyPt23imH9OxEyqNku89lgvUdwXm/pw4M+um638NgcwemqU8u9I6E8ix5vVAOCS4+fw7CTwlYohrGDUBp99rET7sJOpQroYWb0dw9xyM08ffLF8h+BUJQ82UMwwDkob+VZif69GiUZ6yXDFLEsqMESd1eDM+TLBKdXsR4rXDbquX5DweDkEr0v6WiRW1Nw+ZGDpf12a8jUs0J3a8t/ywwn1nWOzNP7q6cfKRmXAKXOI9VgXCTWIrvxUN2eoovV0GyZnPvz0skkF4HLqsAcXV2gEKKj3GWJ4ON4v3uGIIc3o/x77whQUoeVCfctOPRs3ovkpiFR1yTrofXUESrCLqq1XxDKcsPdwdRdOfpDh4wv4MtaDoAYFufARA9gpaTbvKPb4+RNjV/5ORP90WJWjQT6mIlutseLSxc/OLDltnXHIPtOtsj4o6OxGmx8vj4sLUoIO4LpdXqDse3ui7VB5vMhPSPf/xl6IH5ZDa6Ep5O3q9CObZFymYllM0O70IqCeLzBUtFJ/8HCGC4n9Rp/GW6T3/0aqG5RRHiI75XJzPFa8Vm1rMcZhvMz6mB95dOMUMlFN2LwvVtb5K3eeSPQnPgDrYRUgWN64htJin7nGl36T6NsWhkOFJDsiC36jUZKw1tLEmGNuS84NZhNvuAFGr7DZ8eEBghaq+4sckL0VDEahY51WwSO4BI/j5NnIW26wi/D63Fcvdt9C93rR8A1iQ1rzWsfVVjKXJvI4JJFud1yis1wWMLhwDBBeyRn3G1ssfpzXAnihUlCSoxK3DZ+p4Y18QxcfMjo9qyzIe5+OeuRjZ66yoEPaJQHrEt/AZ8EpuQPRPpRooYngO5MJaLzTWoKwQBpML5Wlz8JqHjKvBRHDfFAPMqB6ofd0qh9XHv3vzHv0sE7ccZoIb7oZ8N515M3tsU/bETv6H4RFvvbWXBooufzdSGCpNLIsTXM1FD14zzzrxQw/WYUAG9L3d+dsumDq4T0a5qzQLaXh4/6g3q0tSndbGoo/uy7GRNXNH8c/Z/0nW/n2uJ8zLN6ocTconcuw9JJKI44C/uiyFEhav2BnYVPDWdVn7aj4U7t2cqiQpgSGyW6HsffBmZb+ujlPakSCFpm8npUYmKERana3LuzSMzsp X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1ecadf78-26f7-4790-3956-08db3aabdea1 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:25.3470 (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: eQiV3R0FfY3xE36akrt1cUICErxjduFfVy0S43EwIdMfwfDNEluw/Qa5SzhY5stc X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6077 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The sw_msi_start is only set by the ARM drivers and it is always constant. Due to the way vfio/iommufd allow domains to be re-used between devices we have a built in assumption that there is only one value for sw_msi_start and it is global to the system. To make replace simpler where we may not reparse the iommu_get_resv_regions() move the sw_msi_start to the iommufd_group so it is always available once any HWPT has been attached. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 14 +++++++------- drivers/iommu/iommufd/iommufd_private.h | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index b02f98dc02c284..b828683f4d45a1 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; @@ -323,7 +323,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, * attachment. */ if (list_empty(&idev->igroup->device_list)) { - rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); + rc = iommufd_group_setup_msi(idev->igroup, hwpt); if (rc) goto err_unresv; diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 22863759c3bfb0..7f4936cf537be4 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -267,6 +267,7 @@ struct iommufd_group { struct iommu_group *group; struct iommufd_hw_pagetable *hwpt; struct list_head device_list; + phys_addr_t sw_msi_start; }; /* From patchwork Tue Apr 11 16:43:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 672544 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 0730DC77B6F for ; Tue, 11 Apr 2023 16:43:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229945AbjDKQng (ORCPT ); Tue, 11 Apr 2023 12:43:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229848AbjDKQnd (ORCPT ); Tue, 11 Apr 2023 12:43:33 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE3604ECE; Tue, 11 Apr 2023 09:43:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Sf2fgey8WuTqshz7/2pre1a7TbKEkt2T66mCgQB+U/Yf1zqAcbUROUzoJ+natXPa9v2oaMtYgtsvVmifE5tfosdH+hduMoJyqOpQ+ESoej+AereiZASoeLs0sxfW+tAQv8DLdnfHlAXvoZCqp1sFwcNpllymsqHD9WN8LfHt6I8wj/gIgVhklgCVv6Xj6525EyDEDBCDhA0lVIk84IzAHf8jQBWnPdOPQrmEuhhJ1dE361a6yC2Tcvf7/PjOw5maUHyXPbNy8azowHoar7vTS1gf3JxmKrYFGYZc3+h227o8zY6yFibra7xW/obxnYLp/M8Kh9dC/CdIii+T8ZA9Rg== 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=FGJkRPbIexIVH+xwRqYapeKZ3Zts1p7lbEGw4jgPNJw=; b=YMKaXVxP00mXV5a75bZddTg5YzBQ2Y4R0PLiIzrbIWQDWu0+iDAucNT6w8THsN9zz1z0kNSOoUbHRdNh6IKcx/v/OvgoCN3P4gZrT5zPj8juvT4j5DtHAT356b/dQbkxmaeKPFrZRPfwp34DEaIwtbNeEfS/8Wg0enXm5Qd1tF5tWJ9zTmsiXj9DbsRk1YQQlPjYKVh+mmx5b6ofICxlNiRJOygP7oHdHQNfFPwMtApYPao12vANWuTFP1pTwl/3jnqsoukC9g5VKMdmMV7qhvD6LRnZEZNociCKzcXDsCw87yodK2f151+24V3jmJaEbxEwNqRHLhkGHbe3i8Vi2g== 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=FGJkRPbIexIVH+xwRqYapeKZ3Zts1p7lbEGw4jgPNJw=; b=RnOsueGARIDuMSm0i1sWJb/iQphery+T2Sm3n7rBUbIGJ8TZDDTTQQAT2VvCE6BULEJ4wcv5jfjsbCUXQnah1NiFmtE7SGt535gAANSkWvikYDJQWC/uZqmUYxIMpguXNo2IHXuLx7ieDzJ6Uc2PvuhZPCcH3FGZy14G2tZhN4XWYWDse2Nch1NAFhKz21cI6icVBifTfDnSvPaD0YUARS9kKUVuSFP45+qMZ+WD2uuDmhfLKlTFbwAcsiig23ezsFNTbz+0jCZxWme9Xf0FkwAtqpZdirhbPuh+6+bE2AQej1fDjcT3qyIffkQv2av8C+JYxJXHf01XbenaCZLW1Q== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:22 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:22 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 08/17] iommufd: Move putting a hwpt to a helper function Date: Tue, 11 Apr 2023 13:43:09 -0300 Message-Id: <8-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR07CA0050.namprd07.prod.outlook.com (2603:10b6:a03:60::27) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 9d2cd564-ab7c-477e-44fb-08db3aabdc92 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JgEGMnhwtwTLGABf8lLvgBqdW+jy3r76Ads2ythOxqsPRqOYpj8XkZUFCBLtVG+MQ9jhIfhvWFwdSpfpDB/RvZnFa8fhyUx4ddxEf7lef5lHq040FJ9GuTFtrS4Zus95B+lVh+pc+DQu85HXATQ2XV0xxUvhCLe1YG+A7ax7vGnEKbSRtVT4V8W3lvXK5G0FnjxkudyHh0wG+Gd/tqVkxuZoYiNUvzTwYJxDBLEz+dvhY7GcwiB7rZDCJAQXG5YmVDhsB6wRYER63BWqp6deoVZuWQnhT5DOE+7tpfnp8hTnOcbi3uqxL64LIJ6pPtUkGI1RaSSL29KIrGEFk3o7t+rVdujJwiF7vW/8vea4GXf1pdomHCBuC52JOl+Bat3Y1KUhxO+I90YfPQ/rMAYTqdbxXgRypet0FZZyniLrArV80MxtvIHMcdZ5r9Nw289FQSggk8upf91xINobwZZ1lHdTLGXlhqINK7pWHHctwXKZKH51vwF2/Fyejd7YNLnvKtaMFPrQdRRsJM4CBLGpAv8Hx5Fl83rDNMebMjZi4H5OWUr5PX06wshyNQNkOCCgKvuowaBUMG8uvY4Zl5uXewYHb4M5WW5D3QxNBr3cROA= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ZsRXv22N++39zszLfgU4sxcgwQJDmRkkfUyZqh93fcHmm7AqVJG5gPclwfyYiBJ5E80ODVe7EQ2SDe5uHbDGEJ7IW3cGd4bDkWejDwIfng9VOsnIlxXvKYzwBgm43uGXafuNR/+dgF+D+o+5DjAV5dGXHzT9cp3sN+XyD9ZGmObL2Ig1KpmsSvEwK/oyRrMoJOSa9MhAE4b6ZQ12+iJf9EGKjX2GTJkxNdY7EdymtMG4jBIJKrMxASEJ1aDKhGAfXcTOcar49YbK6cTIpkY0NBz8GQSQ1W19ZIi16AOXD1e7KPP20jq9Mj7KCvrYl6wMFHEA0wyk7GZxmBhs1O9LDC7z2mIZrDAyVMR4COLlTJ+QocVofipCg+U98D9rL2OK/BGJve3L7l1xf017Hsj9eOsezz9GPPyq7XnWwQ+g+wcmnT4S2olbcZMBLytQfCFIw9PLGfY1OZ0euzSunnxft/SahZ/qLy0rWID8GBVY5sud/JDG2UxIRneZtKs++r32TOfxYTNAbciCyFFKpzGR1RNSH12qCQfdVmjcyCj80J86/INukDt4GxBmVo2M0pzHpB+F8lFwlyFUtpar4x4WNLs3VmMUxuloWL5otE91RO73uIP/4Hj1iM5+32PNOk6ZExfsE5kQgn2ib5CkIYt5erxP63JDuZqZeLN33GdLYGiFn9k1bRkXZAOGFV6TsOy7P3EaSUx8761FssafRYoWnms4C5sQtK9Ebqyrnn9gQQFtx5JgS7OU2QTUPhbQA8KEm16qLqzutTc7GDbMHGSTpXr0Jjf2oNUbePVa3WPfFs7QnC8+7+WS9TElP3EiTVL6/Hkml23aMCTHoJO/lnYsTp7Dk2uIVeAQHWkKQhyh6J/Ueac8F83TETOBFQOduI+c28Kl0z1QMWoMzh126vvSTAJYGa4d4cnzf1trP8QAmsqfL45B9OZLOhbPN1kllU3NViNkwD+AI6+eQcG8e5oaRlFbABAv4oOdxq5VIM5oqqXWinxQpcQsxXqgKWPyTm/9kVYiDiuJ7Jtq3wa8LM+f0r0qnzlkoKybixtFVIPD9tqrCkdQpY3xNmnwOYm2wV4U5HsVThmlYodgBLVBuqP437mz836jjg0lIIxYOgjTjz1bKEx3bJ+YSoozxYVVZBQaGfG6MrkUX698jo8TXtcHZ94480vTbSzxp1EyREZ1kD04onrvA3Vz9PLyoWrAdQoujzQjthyK/1pd+uat5kNJ5q2L26YDsT6aQiuXGSygaWWMBIYwuWEafwHfVXQ7k2GMZjqGA9HGBaawpW7prNhPdiwJ7lqS99b2+DD7b8YhcotZqr1EvgPYi5fS+oAL34mHoBOizI71f07WjutxDZTBaqa78ri+9tBLZcf1gti31+IKn8/6TJmDallvl1u9tb20tDc+9uDoa2utD3pwl0nWSU8+VuqTX3a+QtbbIrr/MjagWk+Rk0KJyt4Ok0WGVZblXzH7M+GB/t5x8kmGL79ruL6ETMgcctTMAeQRvFdu473QDnt3UeGCkORJ9Oa3oEMNonLxSDiFGPpYYm8vftRCsoVzLX7YXCdHHYGFX+Na7iBfRdfRxHC4iLqu+BIyqiRc X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9d2cd564-ab7c-477e-44fb-08db3aabdc92 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:21.8934 (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: hg6bh9WCfli5xDV2K3rZVnFSHCZUVu3vqqgCOZfQhVOedA8/dzj/Nngou8KNU9xz X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Next patch will need to call this from two places. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 6 +----- drivers/iommu/iommufd/iommufd_private.h | 10 ++++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index b828683f4d45a1..2ef20fcddbdd62 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -490,11 +490,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 Tue Apr 11 16:43:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 672539 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 43878C76196 for ; Tue, 11 Apr 2023 16:44:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230046AbjDKQoc (ORCPT ); Tue, 11 Apr 2023 12:44:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230099AbjDKQo1 (ORCPT ); Tue, 11 Apr 2023 12:44:27 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7238159CA; Tue, 11 Apr 2023 09:44:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ecoat5a7SogbwScP/aWowjCs6j3TtGfVRxUX9VH5o95JRfvCPrbxMhSjrsu3Ol6o89ENL1kexCuPuDwNdUbneoaosnH3O3xxB29lPXpNDyyv9ZYxk91/SICzPWHme1Yc3d7QgeOj7gUFxV+1Y7ZwtTfkNYJ0kyXWquEPgTGwwfqykSlStSoiPk/eG0jgrcMY8ELrIM4KyeRZ4HjIG/5ZgkyMj9KwySR7FZNW427YELDDgzERBFFCEUrAgMz5Lm6VKrduj9n0n/OZ5szTRranhULoMGBvf3583ONK6ol9e7LudX5Xz+EOUlSwqhuHEhn4N2xCQtQ+rYUFh0kgSDyH5A== 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=y/Ec/lkq3YkLGz7aon4IaNs52pGelXfT9OgoLQGpR9Y=; b=ZwmKXMRY4Bpw9x+uwopRk8LbrM9PXdPTXubF5aJxatRhuHS8mb92ROycM3b4fHp/l5CUbUXNNBJ1rKptvLVKcWTAtsK6KaGMevNhbyytkyFX0g3vjZsHG5jSr/ltZ0eHArulCIRgDruT7GJ2dKIQNPSjRf4ehue2oaSnSxvuOGhp68f3c/7EUg18LwVg44p7J51dFQYh+LUge7TxXEhRrvObT9crYQvGfWAYXJ8rprU+bkxDlIyDSBgJ5Bnvoxk14dRZi4EvSUoL++j/rtmnnkTuZ63vHDZfWbntm88FK1PNkj47Cr+uzXVCiKPnrGx4UaKR8Raa5qUTC6TmPpnDhA== 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=y/Ec/lkq3YkLGz7aon4IaNs52pGelXfT9OgoLQGpR9Y=; b=LJiD5W7FEWWyClaOZpUQLUeffu16+6AKCkUB1yrnRMpXXFfl9SmVWG8gPdt59N4qEaMYgotRifmBb4XqGQ2Wvh4eaxmFiZaVHa8JQilMW6kcyqQJgOHUxyhK0Aetv7LCNavGLQVSJS0/CB8K36Vnm/ow8lFHqCreeOxxLo7TpiHb7R65FUz26kfTAM+bRCodZZPz9TNiifuSQXg5Trugfiu9s1EP5RrNMcUXH8XpwnaniYvFlgyUECsLEyjNTns13qg3skIOdsUPwaSq22gCZvjFYqAg9CyNLT0Dt/Cn/SfASUCpr3B5GWUKm+Py33hLcjLTeXEu0HcZVCNfsHpz2A== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:25 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:25 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 09/17] iommufd: Add enforced_cache_coherency to iommufd_hw_pagetable_alloc() Date: Tue, 11 Apr 2023 13:43:10 -0300 Message-Id: <9-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR07CA0070.namprd07.prod.outlook.com (2603:10b6:a03:60::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_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 37194508-d5b2-4ad1-540e-08db3aabde57 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qHjtc7LUiBby8np6lq/XiP9tFPfNa+IPXM3Fwy0sZO5UUaQQSBYW3RfsYZLc+AXQgcLi9g1wqBtvYBvArP346XqRIb14odgWJpYk2UnZEJ36+P/8k9b1hEumOF5UEXfCkAPw7bxacXeO2fLsF4OYovhVqg6So1og4X1Gy0J6XRsR9XBAjd+iNKF4fiaIVs4KWhTK+Y5E3dM4siGIlmbS0BunOyahG4aUCXHQiSSwFQb4eK8U7tZLc9oqiEx3FltRTKDPLxbBe4hIqT2nbDPxhghs+oi/j+q2leTDTxn+jn7/2+KuDBhvX4McIczHQbcwU83dQh6vzo9aw0jY9DJnBKoWNw72gzm15z/fXxeLWE5KAVwA919zhdLjrexqTNV9nMGp9NaIg7ziDaTxBkRVabn1Y9YgoRJj+lElla/XwNfDeH05RYzz4FlT/wzZcOZK1albvNVD9nodvzqanS9GhAo7qfg4WAmx+WFdE4YM53I8Y28mG3JFDX3rwRFtpxp2USaNHzLW7VEjiNb/RAHE3qVmI9vbOv6Bm44j9sPypIJU9zPrix10nbHLCRJBI1gg9J+nKu7i+IGZqX49b/OwS9l2d67dVaiXRBwelW46C2A= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: +EEkImLw+dOVo4mLp173emcjXBAwu3+/ebjPNaFYxErB/KQXOMOOb+9l/0hlTChD9nWGizHKIUH/Vm3TftGjRNmjWrp5na/FEZsL23TcTiMbR3iFEKprj2WeGrdL86fkC0ztmDKxTkaQM+aDgqoE0Wr9bKCTekBahdCl3tdba1aAtsnmtGec0Z9EVxysHwEOPJWprGGMmh4iX5QpURYHFm6BOCvRgZ88bo5AJHj1F81+6jmwMuXAvgrgRfWenAYWSYgL98LKO9fRc6pGNZhCw2mM7yfhmVFs0K+GZbp7Q7+oYDWM7z9Ce2lVYnWul0SJPurFk7SzGH/twLvtOGaPHAO+Peh19INBK3qb1W0M+FJk1p5b5G41GDIi2LD9OWTLv9KC4jA0azjUDi2DwNP3rAqD74DKUfTHUYo+9ezONiBz8FvnNeA3i17wS+4ntYJgO0D90kcyM9GKgoDYc2zT/DTiag5fR5Nc2Sm5gsFSd/N7A1moVzHYPVeNQKCgc4UODUXQaL7BhfgUfhgkncjjzAbcZjOWh1Y7smR9Ssyh2GtwAWjU+C2b5+IWKGde0bhy/4QMwGfrr6esAJ2K5v6ujBZ+FpbPzukNKSevvLVIVxVE32NBpA5IXgIBntP2Lu6DHfunmcFvV+Xsy7lgCRv7u2WryRz/7U4JS1XE3yCuXqvaIkbxxe+uEbhSWtGqQ4tVKSSj2Fpph8Nazw5N1aO6j7UdTKQsk3YAwxurxbGdreUZTjw5gLf439mnkzz4K3sUOWs2KzbAWTung83ljvzMBMNhznTKSpvq+P1Kr2noDfY6eme6OB8BfKe+fLF04gKdTQ6oWTOWY/jbn7FTcJliMI45Fvs0FIjyfiCezBkCUOd75gvdxj9mPIS9emM/GwOHwQX3+XLLHyzg/+f4R2jT3/OxvKGA+lrqX2uN7D7Q/qTuEki2F3cTrTf7ObUXjsG2YfYSLx+gt/CUQb1sK/Sgu0Ie2CmXuzAIYs1JX9P1R8oebIxgmCPEj9TvgY9mB5ZVGMHuEqi9OVhRgazcROc6bcM1Z19dCAG1q6mQMdxQjPF0Qk9KwrNXkFghQaSd2nByE/HC4DTYnZh/66mOIt3ao4+oKp9VUvMFVIrAgVVkXKFGJY65YbLnnvhqDdT95MTtojZ1p+duVMcoyofI89sN7zBTXM2Z/4nFRn4Vdx600gXUZPaYiLm8ClUKDf71SOGG9QFDy2xXrqTgM07/hbzO5bF3ms9FLyikMZn+EcWSNkDMDS+7MCFqI7YD4mGOD5tN/hlw5lEvnGk2IHGt3zXjnRpAmt/GmME58dNfvPfAYmxlD9NUfENh7/aEXPEtULc7V438/cA7f2bA2r5T6lK0rV06/e8BjnDQpMdjinUE1s2dfqay5spF+tR+6hJWWA2FqgAZZwK/RpA/cjBNnNATOcUxrxx5IjGC5HzrCxCld9KYhTGQL3X71mDU3s8ru5qltRDdJtkNmc4UzE743Bc/V72kO3scJs5wwX37W+IXG5EW0MlX58if6CbKQcx8npJqmpsth2fmYDmwb0x8zVof6IktLF/oI2NfjFFGm23nrXhZr40y8pWOzQBdeLD3nhyI X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 37194508-d5b2-4ad1-540e-08db3aabde57 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:24.9079 (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: kdKbNsL2xr3lfCKxFwXA3oxY6E8JLkhKFj9CTK3+mzeyEJlmD3jbN/mJyaOoQeqT X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Logically the HWPT should have the coherency set properly for the device that it is being created for when it is created. This was happening implicitly if the immediate_attach was set because iommufd_hw_pagetable_attach() does it as the first thing. Do it unconditionally so !immediate_attach works properly. Reviewed-by: Kevin Tian Reviewed-by: Lu Baolu Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 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 2ef20fcddbdd62..8f72a528325f04 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 Tue Apr 11 16:43:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 672540 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 EA8BCC76196 for ; Tue, 11 Apr 2023 16:44:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229752AbjDKQoQ (ORCPT ); Tue, 11 Apr 2023 12:44:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229639AbjDKQoP (ORCPT ); Tue, 11 Apr 2023 12:44:15 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E76D55AF; Tue, 11 Apr 2023 09:43:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=i7o4pb0DIREuOIBFUJpayfIB/rC0ID8Pt6O9DwsfVp++dwJ4lyytMtib3dkz4HVrH+TqGx9Nfh6KRRZDmM7iVdnSyHwmE20YeZkLbXAhpK6O+5hVnSQPFIn4nHa2Tso0p32mvxFh5RawnFt3gvdRomemgalwc0CN07N4kvzEa0bQRDhfIW8d9QSVGA+4UiSBGkuyF267CLPSVcmtKTQggPhUHrE9+BhoFnyZcUfzWgpX3gCeFzm2hfhz3gWv5Qe+mXnCeCA8E+OcP3m3ra2TJNTTClVq6erxKymda7jpb1xRVeUPfkDCXY1DMGDDm+H6FNrUK7pu7Wqho+irYDuWMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=RFhH+VMaXLDWwRmCU+TbUC9YfLiad4RolEiHONxioxs=; b=Croa5QGThs6PZRxpp3Mh8bwlm+uU0lpfXzNyBIDAZwNRPNPMWo0YAefPbUN1mhgO4QlRErTMRGcQIbb584KuKDaJM6tVGjr8NR3KLEl7CQ8zsZRWrX1MU+4Q2/i3/GufDkeZNuAnswz4Ju7HB6KOBCyoMxTKi1cJWHHHiGEn0bn95+knNKbEM3JwL5jxh6076wC+mviuLRJYZfeWuQHhup54z6lfAcw/jyGQLoAaQibiaMeZYyDPuXM94WhaSxynMmlhcJMTwZdBJ5aQJGjg0X+RdHQIGu2Ts9Pp/vEJdkJaG00ZpPpiat+A6zk0iGXbakH17rFiPATfA3hvCRP+Yw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=RFhH+VMaXLDWwRmCU+TbUC9YfLiad4RolEiHONxioxs=; b=XhZgpf3Ta8bg3zkK3Zzkwf7Y7EsOnpVcMUAfN9pnP2ALk1cI6Ij74jNcEjn7cbRXEzlBDwIFpud6hj1m8qKlRtpZukqviDu9eTnaNH85b7G5KJxpECgNIQAslmrtJTkWIniXkurEfbsMZfX5mFA+qx37kq2cepFNhiPS3r6X9K825ZdbA6qdLu6CBRZAR0zs7oSEfZcJPck39Cm/8d+j5pTx+NlZtAZrC9UdY/Sq3zygkham1XcWNmaNOPg+RZ5j3A8nUUhX84LIwoWsts2wpHxd2cQbkXlWX4anEA8M5xHFdRcWqfQf8TGeio3s+Mt+75XhEZuxV3xvwespsnQDsQ== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:24 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:24 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 11/17] iommu: Introduce a new iommu_group_replace_domain() API Date: Tue, 11 Apr 2023 13:43:12 -0300 Message-Id: <11-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR06CA0011.namprd06.prod.outlook.com (2603:10b6:a03:d4::24) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: d8190818-4855-451c-6cd1-08db3aabde3b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: M/y5hUeR61qNQDGrdmblI5q+zcvpetl/9sylGSRjx2nlxZs/uUO4yTxRK8MkY0ZBUHQj8fFzifD/zwtuBM/JwfqnqMnY5+OCuzzsOxeTck53rb0OuaUcS1c0M66cejePtqPK4W/w1OtIbtTDPVGjgD7xggKzRLjIbz0S8nhcoIgiUld0+qckYi6CvVNIfUD2rzH/Of3k/tr86JjMknLX2OZSIOjUExk9ZjlElhxItfbVmDIjaL4/dPKdW054wziaVZMFy4YeOetg5gzPChbIo3/FHnxazrKOWa2pFV6Hu8JjU+wo/L6wg35xuLGQuncX6cABscZp9Lu51hTXPYafYBDvVDjVRLMxWvoWbunkDw6nI640/uFHCfzwqlxsiYRiZptcKK4wWY/q3DKFCQtaBoS+k+Xvw9FBcyXmm7NaR2fCqDMayE3RWArTvQaEjTqBTTUBr+YL0xegcbEI6DSj+bY6f54WXNcZLZjvZp7gPNt91ZzFb1RQYaGLZ4S2N0d2jc5EU4kYwxqEQo9XQxfmrZ1miiVJdmyK4fm4D5VHvA5/bqpsrRTDr3mO4leWybLs X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 0QVQNGd2eGRLV2+ITGB8NHtUPi9HDyzLl6AYGmtRad5RUMI1tDtmbGIU1NvnT3+9wiUBISvsWliXdptqu7Z1Fm6zivTUiR/7eZo10xDlEZXJ2Brb9JoKUaIa+S/bXVuIQC73fHfDQDaJYNvxEyC1U/SqvHntIXwsv9CRNQ9kAGVqQTw3X/1CBi4rf9t2xDzfHgCX6YrkZT1BDmfnss7xblD8XPWfR69E1UFiv7uhMqTlr4QHwGsbqig/V7M6/s5jvaURoSx6P8+hKyA+xgkTglIi2qxtkBz8yHa+lJRe1/XrUZhqwVVAKNLmBHk4HS4m4YQEkYuwtuE0EvL3/JB1qeFhB8x2zs0496XD8yE9YK4CcNnbcikATgybQZC4/hlkxEy/b1OtZK5TlEGUn65Sgpa1PheSgvud7TKjgdGmPtu/ELiG5JHAQLuNpOuCiF0VW0VPQS4cHp8tEMkPpMieokn1saFMR8ZTRQbutlWy9iTysn3fnGLBRcbrsTWPAHx70v33nShIGQFjUgtZUlwcjTGG+fW9zMpI/+2QuvdRNF6gxVVdOdkjJIhibq88HAfEUC1+HozC1sJI+zyhtVx0LadtfYb/lX+cXkQRMJEunGNUjtlSPcGOOwMy0xuBeydXVYr1gWLp5d2L4OTVb5qBijE14y0Sg2R0rQjby+XdK4QHOUfbNENw4quouxOPKmK8HY83GnZzihA0/0df8QfVIXpZaYMVwgmUwxR3gWgqtvgaLCg+27mV9gkNC537lx9QlXDJ5cie4qlXIaEtItmP8fmm4beZzTEKiYcg90TemPLjwnkA6GblhtSyFceRDUWfMTu5ALxhGtAmI+g3P5hiw+19AI/5ZMgNiikkIuYpY9rqRbeH/g8tkS8ibOIOI2T8kCM+n6TVkz9lf3C3uZ1PWsu9/9j7SaBHkaMqTFgQ/29WxA2yxNFP8R3ajQmC+sSRlxX3bxvJuZ9DfpkV3Xv7082ejXk6tSopbnps0PBGZzhGu8f9J3jLH/8OgEnkdCUEV9UxspJlGtfkrcwYjSUfGVS+EPjMosc5JT+W6FxWPdP6lZY1KVRRWgiJW1VoDY+RRQD5IGIL3DRPNjVQcDMJOlSqhkTFltMX/5n92RMV7FYJzhezrCM/eYFHIWJG6l6rAAL981wsgqyZuxLwD4Po6Q3Z69PHRl24Ht5SBkf7yXYCkqYZcgJN15wa9sqW1dbo2ErCRYXtEAa3zX85ti4rG4IjYIsQnDxHlNDBld2c64Jeh/BSOTZ45pwch4kI+a/HPQEFNPgTsNDKI6c16U18sNWrBryXlEmKXpsS101S/QlLaE3dycqfi9IMcOAjlRCjz/p3GMUY13wxPpfGRhu7AlIcDTAsH9Fbklr791hmdF3q1ULrbIMMQkwl8ZLTDbIarem9qzuAFIX7TX6LqvXvlVgxuVYIQ+Lb2WXW8AysQ6EK5auCBQdLWl7HCYdQeDsqboPwYkzr+1f9TokqrRMjdEYoCKFTPw4WotUVrfTCBB9Zaf9ADdc5MUlS9Z0zFKjvWFJqu6QKuCsh1CrJRFqp6S8h/JUgJvbWmlQOXpBVp7JvDWQh+T9FVhaVcnwoPOi5 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d8190818-4855-451c-6cd1-08db3aabde3b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:24.6776 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: OE+ID1B3GCskiDjkk8y16bhyLXwvgnUkPHUAJh3ZeVefbgc63Kf+KmPKYu7FliyO X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Nicolin Chen qemu has a need to replace the translations associated with a domain when the guest does large-scale operations like switching between an IDENTITY domain and, say, dma-iommu.c. Currently, it does this by replacing all the mappings in a single domain, but this is very inefficient and means that domains have to be per-device rather than per-translation. Provide a high-level API to allow replacements of one domain with another. This is similar to a detach/attach cycle except it doesn't force the group to go to the blocking domain in-between. By removing this forced blocking domain the iommu driver has the opportunity to implement a non-disruptive replacement of the domain to the greatest extent its hardware allows. This allows the qemu emulation of the vIOMMU to be more complete, as real hardware often has a non-distruptive replacement capability. It could be possible to address this by simply removing the protection from the iommu_attach_group(), but it is not so clear if that is safe for the few users. Thus, add a new API to serve this new purpose. All drivers are already required to support changing between active UNMANAGED domains when using their attach_dev ops. This API is expected to be used only by IOMMUFD, so add to the iommu-priv header and mark it as IOMMUFD_INTERNAL. Suggested-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Lu Baolu Signed-off-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommu-priv.h | 10 ++++++++++ drivers/iommu/iommu.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 drivers/iommu/iommu-priv.h diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h new file mode 100644 index 00000000000000..7c8011bfd15374 --- /dev/null +++ b/drivers/iommu/iommu-priv.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __LINUX_IOMMU_PRIV_H +#define __LINUX_IOMMU_PRIV_H + +#include + +int iommu_group_replace_domain(struct iommu_group *group, + struct iommu_domain *new_domain); + +#endif /* __LINUX_IOMMU_PRIV_H */ diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 76969904b93af4..91a948a76db5ee 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -33,6 +33,7 @@ #include #include "dma-iommu.h" +#include "iommu-priv.h" #include "iommu-sva.h" @@ -2191,6 +2192,35 @@ int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) } EXPORT_SYMBOL_GPL(iommu_attach_group); +/** + * iommu_group_replace_domain - replace the domain that a group is attached to + * @new_domain: new IOMMU domain to replace with + * @group: IOMMU group that will be attached to the new domain + * + * This API allows the group to switch domains without being forced to go to + * the blocking domain in-between. + * + * If the currently attached domain is a core domain (e.g. a default_domain), + * it will act just like the iommu_attach_group(). + */ +int iommu_group_replace_domain(struct iommu_group *group, + struct iommu_domain *new_domain) +{ + int ret; + + if (!new_domain) + return -EINVAL; + + mutex_lock(&group->mutex); + ret = __iommu_group_set_domain(group, new_domain); + if (ret) + __iommu_group_for_each_dev(group, group->domain, + iommu_group_do_attach_device); + mutex_unlock(&group->mutex); + return ret; +} +EXPORT_SYMBOL_NS_GPL(iommu_group_replace_domain, IOMMUFD_INTERNAL); + static int iommu_group_do_set_platform_dma(struct device *dev, void *data) { const struct iommu_ops *ops = dev_iommu_ops(dev); From patchwork Tue Apr 11 16:43:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 672545 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 A5915C76196 for ; Tue, 11 Apr 2023 16:43:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229938AbjDKQne (ORCPT ); Tue, 11 Apr 2023 12:43:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229949AbjDKQnc (ORCPT ); Tue, 11 Apr 2023 12:43:32 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEF875588; Tue, 11 Apr 2023 09:43:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SoPHg7di7xsWwcFsP6SpQsA2Nnj7yV7qqcHwe9+CMx//+B4U+Sy372RgMvP2FzAuJXJuMq/7enWnb1H2IGvV+X6wONne+F4oll4ufqY13r40JAl6UhIncPGCrnT8Aoyb8QsmLm2VGpT3EsoACS9om3teSmQtX3Uowyz8RyLMC0TBk6olzcL8SV4J8bvHyvUZzuyEzohWLX1iJDfRxfciL/qBFUGaZVzD7v03PyDCeIhboOTGFx+KBC8nbG0QIz1xrw3y9+kkbMKhLtnX2Sligw/vAymAGmNRNbNSbVGVT+IMiztBijvm78sVMcEvxJUGX26UYVZdy+ekRSj0Y+VfOw== 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=uD34Oq8oTy+sonXeAwv4BDmlwZH/r68vKzO3PLkSKu0=; b=jN+ajVNO+20b3IcSUOzwmreuMVIUsWJ36ylRH6ppKUXpeNh3pN4yKGcZGLQbRR/JgwMIJJ3wwfM9bPD2Uhig6LSyfmdElIy12RYy1ATJlZhzAGF8YFaQf/9QxE2zmO6LpwXsGVUt6dXmm8vduFcU8g5vdmz35pSBzrk3StqBEKZSEDg/ggGlEAEj7Bz397+GJWfkaH7qiFWAVEkby4tEqAMrs28lP9EMBO+TWLCvF5fABubZ4DXkvMCXtt6cD4Mvs10eyIX869qk7IsRIrnHvD+eJdgtiuvLklduFNsr2qpwQgvg1fjkWhMDll4lV0byyjl8u/80MrYM1L0D5xpdaQ== 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=uD34Oq8oTy+sonXeAwv4BDmlwZH/r68vKzO3PLkSKu0=; b=XdPJDvuh14kdbPe3Xl1j1SKzoOYm8FbGrd6CG38ZO32forLTjUGWBXoL4LGnjoigY+Hn91X5GQnhnAGVqy02Bn/6ibhkWQRjG4CHmnW5ntRP0Bj/NBLMIw8FWGwxxbhAk9nXhng7kgMQtMWiGEoJAYMVDbPzg93Whq0OsFhyC9Cs5Cba0WVVTh1yk+sI/P+NUnkgj0PmLX938uQuT2NzuZD6NxaY0NFNSgQeQqP2TzQJqNADRJyhok3kbcT75wkT0/rF6d38qpZVQUAbuiPES/YHoM4rYUn1DlzRfZ1CUTsDgAJy20puZ7CZcCJS9I/pWsFbXQ7/Qf2MKhtETW4gsA== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:22 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:22 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 17/17] iommufd/selftest: Add a selftest for IOMMU_HWPT_ALLOC Date: Tue, 11 Apr 2023 13:43:18 -0300 Message-Id: <17-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR07CA0040.namprd07.prod.outlook.com (2603:10b6:a03:60::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_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 4b102766-c51a-4bbd-bc14-08db3aabdc88 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ea0BkqGZw6jm7O2y7C7J8v1yL3oBdUBIwaAzl7ToRN9rAWqOBVFrus7aCSXLt9qZumV10jNk0VSP1nMsXG9orBLYqCzCWdAKkQNEWo306Yceq8yLSRklGvY3BB+IchpwGGK5A52BjIzJRQlyLWjWVawCqrQ3f65OJnfyxw4KEhrPhTLcAY/oS/8+MFXjAPn97daocY4usIMI1LMwrsNPngcgeu6jm0KOfsN+qflN99kwmevwcg5l17eD9aXMQzp6XfqXgon7vbk+KMQtm6+tCzESt3G7/wM+IXZRtqafxaMGGeVs6vid4kUC6/1roUFivQEC4ldCUgyih/c1asASrcZsu+Edg0XWhmfDjlqRITtj0AoOBIcGmh7UcwXeaESapdN0I4TBV9BfsqemiomQAPBEbO0CPXCcn+7pm8sjSDFoW8EGHRUBqlAfLrn9Q7IctY521FFRBUXtpUXsT39tRtYPdnz19EqPfF5vyRGPJSglSuMIOnXWAro4aEd0upPnCUGuxUVA7LQpb1AkJP7YNoro5fagw36JieOunHJjFAs= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9YdfXIcZMIcy3n9gDyE11s2AG7P/03uizc5rYeEi0BPtMAyhLT4OWybgaOen6XNI3l3IPxFaz+9MWFvGY2Tys51trUI3YYO1mjUJ+ClY0l0K4ae8OvunR6wZaq+aqMsqfgvea/ROIEJoIjeFFb/6JO1CNAqbcZsgd3gRzLm/4XB/8lSSIcEeTleGEjbnfxGBYT9tWbl5g0V6n6cxS9kiTnQpRFawBVjpi7vY6ox/fzMPtkbsaMEu45E5IjAqV1bLBWgEOCeYPNm1smwI9Zex0jva/YXg1Mn2GmNJeodX7O+/9djjfJO6/VLJlaTq598V6SYew2hiMzLLQf/9QZFxGiTybzoPR19OOLpk1iqN7NMLuxqF10zos32QmPw57OoG8E5mVKxtYiVBilCHogCAL8UnhC11Iw6yVBTKDaaDRhCppgqV91ZGM02DUGPHCUGwofWn3K5ITW+y7FkbJyyQ3DEntVywa4254NQEXKezcJCrEsy6pt2LnyTbZ4hAsHFV8Lx2DJAQzyPiMshG8ziGk6rI5C145gIsnpEP4zzv21o6EjiKYanLntmoIHC8ANrYhevBKSzJJkZQfUqzIjfMU/I8ERa6CJXHOcz6i9NjeYYnNafRtas+Wu/5dTc9LT1Li124bGS3VYoJUmJpk04xtjTpezMaKet1eXp7WI5eDm/I2XzNGNtyBaq1opnxDoo1diewpG02kU7JQLr4gvGnhqLh3lEvTi1I9maH49MbPmmJUGzXs5wJ5E3AbFHXEb5+Sm7sFDJXN9ZzfELxA+X2dnOZbma4j8X745gyrUkrJO6eYZKtpkWBH0nXfWYRnqXRPSQojMhTS7MiiKfgR3i5bW9cKPorGwOh/jsk0t7KaaGRIIkvDIUCPUQSOVQXYvbChK4qtur87nXYsqstfBmD6kdq4RVMlva1SehF/pL+8yHdoekGQmvMY9BRR85sIVjYRgknArWkoEAcDBYmu16P1xq1+XUWNhbnvASFpca6KVuVyw078NfLmlY+4W7owsfTKGo2qfVNlr4yRWs8HSALzuBWZ0PEeqGj8GvEShSUQrJSo9XW9A5hmWSAfrGwnb6E3KOXZBV8kFL4Je0gVd9zhS6EATQhGaYOv7N1nKGoG9KsxUyoSq02MLS4VKFkjcXoz3e6MycA2MmQnIsqJqTuJ0VrvAxL6seWCCBPam90/WvAV9ZGd8WgPjeq8q1/9IHB72W0qP8IfWx04OuGDRkwUHorVMqQmQ+K37yQ083n/B3ZQWFUpb5Zs76bhdLOlLY7gbGDctOcjjJzbyWdqhCYblWtI1BYE/0o8A3sqsmX1xzCxPNDxbtQ0FLHN90otAsIslRzdmFHcoo/F9+Jh36olnPe2aIGIqVETJesmmq7aZHd4eKyd7kTt+ueA/rSKE+vwNKUYBsdmvA0xfK6xjO63pGkQPYkgpDxOkIEmxZ/1FAwiiiEU9S/7xnC96kJgTKqBT8gapXZsVpvr86ZUVIFSrjZa4U16Hz5EZk2xIuUeP7KxEowe7KQ1DMo1MAFwh+c+xJ3ccY27J6azGnqm0Ix+C35KzkUE0l9jqtE69glA8QwSwJyp8STME1n+Mx4ELNk X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4b102766-c51a-4bbd-bc14-08db3aabdc88 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:21.8334 (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: HT1v7AriNyvH+I0dUUUi/pTI6BHccBhaXwkbBhteR4OgJv70lFnSvHg5Imls5EAe X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Test the basic flow. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- 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 a6bc090d4b38a9..8b2c18ac6a2864 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -1304,6 +1304,21 @@ TEST_F(iommufd_mock_domain, replace) test_ioctl_destroy(ioas_id); } +TEST_F(iommufd_mock_domain, alloc_hwpt) +{ + int i; + + for (i = 0; i != variant->mock_domains; i++) { + uint32_t stddev_id; + uint32_t hwpt_id; + + test_cmd_hwpt_alloc(self->idev_ids[0], self->ioas_id, &hwpt_id); + test_cmd_mock_domain(hwpt_id, &stddev_id, NULL, NULL); + test_ioctl_destroy(stddev_id); + test_ioctl_destroy(hwpt_id); + } +} + /* VFIO compatibility IOCTLs */ TEST_F(iommufd, simple_ioctls) diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c index 8ab20df4edc8c7..d4c552e5694812 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -579,6 +579,8 @@ TEST_FAIL_NTH(basic_fail_nth, device) uint32_t ioas_id; uint32_t ioas_id2; uint32_t stdev_id; + uint32_t idev_id; + uint32_t hwpt_id; __u64 iova; self->fd = open("/dev/iommu", O_RDWR); @@ -605,11 +607,18 @@ TEST_FAIL_NTH(basic_fail_nth, device) fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL, NULL)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL, + &idev_id)) + return -1; + + if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, &hwpt_id)) return -1; if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL)) return -1; + + if (_test_cmd_mock_domain_replace(self->fd, stdev_id, hwpt_id, NULL)) + return -1; return 0; } diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 62e01412a7679e..53b4d3f2d9fc6c 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -98,6 +98,27 @@ static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id, EXPECT_ERRNO(_errno, _test_cmd_mock_domain_replace(self->fd, stdev_id, \ pt_id, NULL)) +static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, + __u32 *hwpt_id) +{ + struct iommu_hwpt_alloc cmd = { + .size = sizeof(cmd), + .dev_id = device_id, + .pt_id = pt_id, + }; + int ret; + + ret = ioctl(fd, IOMMU_HWPT_ALLOC, &cmd); + if (ret) + return ret; + if (hwpt_id) + *hwpt_id = cmd.out_hwpt_id; + return 0; +} + +#define test_cmd_hwpt_alloc(device_id, pt_id, hwpt_id) \ + ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, hwpt_id)) + static int _test_cmd_create_access(int fd, unsigned int ioas_id, __u32 *access_id, unsigned int flags) {