From patchwork Mon May 15 14:00:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 684191 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 5E23AC7EE26 for ; Mon, 15 May 2023 14:00:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238905AbjEOOAs (ORCPT ); Mon, 15 May 2023 10:00:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238913AbjEOOAq (ORCPT ); Mon, 15 May 2023 10:00:46 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2067.outbound.protection.outlook.com [40.107.94.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CAC19E77; Mon, 15 May 2023 07:00:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lJdwEVlNRQ24eUbfzGzJ1LE6RL1eiBpL1fRxMN76JLilR5SetxSZ9pAXU1qTHyyyv5DRiEtVHnbQ7fkm+7kLec9p6pFfg+jGrCQ1vE23LIM70GsUb8evGhhE4FLQDOZ7oTR24JDJCrZisWxyJgt8cDsvYk5GKyCRueEVy4gTSpb31fTERbScJUq+AoTbpUCFWS+6OPUUUGgjLfFeaAMhxo08UJGrMBSF9uT2xROshuaKNVCpgQVYYtG6mTdYi/RO7f+lJHi9GOa4lTKxYvUZFoRbRTjbaEkVttSi70480nNZ+ALrUZX3woCni3nsCmLf5tkgNAvkv4nbIYHkDImv6g== 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=ns2MTbNcbfvDnJ4GIVxIgIUBBMOWFMFWeAaG2juq7K0=; b=m9s2JDqdZ6981+pA9yKhyhDS1hFLgNlDFTAvElRSnivL5DoGCwzmFu9L0dTafqD6eZpa5JsJDxQwp/igKqJFefCiwvDd89biOwdycq/r+ttL8xYtL1fEXPq575Mn1lKD6QZDk5J4cAOQpzkNED/QkTqVWeqrIYuh5pnfYX9gTRKbFLulaMjozbqkqRxpsxx9zT6wFdi8Gcwu3WdJAWk7NU5PeGbmlbidowltQRdzFIvEeIlNE9XE1tY6dG7X6f4tmJCEjc/j3eKbOggJBPF000mpAppRcVSCRGrHAprZy0LjgQkYFiLiJxLDsRhai2YRM7AT4KRIAUeL87IDSLTsFA== 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=ns2MTbNcbfvDnJ4GIVxIgIUBBMOWFMFWeAaG2juq7K0=; b=Um0huJtv9nSGqeLfxuqxK5d7voJnnHnX/3eLrAut5+SO++4vO9L2T3vrhdQOtKCy/ZbdMYP+cqFIgZgQNVgke4AIEtBuobWEGGwIJvn1w3xR+9apQEhpMUQkTR5o0WyYgaLVSXaxLG6OkghmGXJAaebTfz18Bd5bxU4Vgxx1ed+oK5gHr0g7sg1i6S+UKOmjyxGtTV/gSRR3nV74DY364KrvFUlZr6ODD9sG/uolgvDuYaP06uKKHKQ6dy7kwmmiPmFLcV9PWnM5r8sMekD4ligX/Tqk5LKw8+Sn/0nwr3JgafEmjxf+xLrSjHyDRKgvOVCqPPv5tv6Dq0wUeGlTtQ== 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 SN7PR12MB7954.namprd12.prod.outlook.com (2603:10b6:806:344::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6387.30; Mon, 15 May 2023 14:00:39 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6387.030; Mon, 15 May 2023 14:00:39 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v7 02/19] iommufd: Add iommufd_group Date: Mon, 15 May 2023 11:00:17 -0300 Message-Id: <2-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR17CA0004.namprd17.prod.outlook.com (2603:10b6:208:15e::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_|SN7PR12MB7954:EE_ X-MS-Office365-Filtering-Correlation-Id: d8eb6471-8239-49f7-41f8-08db554cc16c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9UwLy0UM39vPsVpNzzCS77CxXsmMKQpWWa04dG1WawvpLyvfLsOpx19DYhpuDcSAil9nGGhNBfTMLl0Js/28jpIhN3LRC7Yhz6279hUz5prC8Ex0RHoyDo5gPTX5uUt6TqFnL0uHi3/q0URi9Oewv65AMZxGJnT2EAvTXhjpaI82/OP3Z5VJUsj89Wz/ljYrIF+4VZkkX9dQmCPrnTk5CgE/Im22b6i44qoYrWXvMpSU6rBnwSzne3SQRrq3tRbwLmYUxUiwOoQ2rWPZ5n3UCtac/1GATibybowFMuy92S58mcRsnYUXu6TWaQOoZ1imJHom9mE5gHW+vtIFHNK4BobXhm2XLCAgAapVSPMew2/HFsrJRum19Mq25CvZwvNbKXOQGXlZ1Kv7gyQb0wcR1nCOSGxGSev8wLq5OttyWBsw2oEd7JNV+Zsjh5dEUlK9jl0KRlQbbD/Jl/048h1nkly4eYQberkVPKang+LqU1OQaQiiqtxS01ETb5qYSCOafxemDAtasw6Zd0jR2otW6U8meHAb1ZkDGRg1gt9+44Cfgln4ZKs/TA6ewQcMdrEBI9PUcrbnlTPFzakEUkE1WNfFQl6j0tEsJnVg9E/fFBI= 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)(136003)(396003)(39860400002)(366004)(346002)(376002)(451199021)(83380400001)(66476007)(66556008)(66946007)(2616005)(6486002)(26005)(6506007)(6512007)(478600001)(54906003)(6666004)(186003)(5660300002)(86362001)(8676002)(8936002)(36756003)(2906002)(316002)(4326008)(41300700001)(38100700002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: XP9FJJWw/hrqf38mcyE8n9X/5nZrhc+cHb6fzmbjQxXDqjAQgInIid260VRghN0JHDbyu1asplc4EXXTzixknTYYNpQubuIIYYk3gxErK4lMzWxth8PH40bOnTMfQu8PSYO3eDoO/UTiaosUjUh0b3uC4zQCcRz5qaqm7N09x9jjnQsULYO2ewCnVohYeJblZlWwKygbnqhOrYonwR7YlsAuGCifzzBolcg1M/mnJfIo3h1w12eya+HWoa2gc3oRYPlKTf+shYPoGMxfu4at18irTkCn1Z/NxRc27eY0NDvAdrdFzozT8XDRLe7oNJrwXY202/ePNwjjdeYOXH+PPLNhUGrk7bHALiRJqD5BMHGTWBLA6U6u/zSr+rBOjlzlp2qgmMF7wDlm0b/EPVNhrPyrLBomnSEe2YOhN5crC2WMeyaL2u1STsy0NajmY3CpWtZvwvz9koJ5QuWVjNImAbcH3qC/8uVMKuq1v4Q22DOCf8JnNfzPMmSClpZ+hI/54601lpu3C2ENt36kgzGImr/2D+82MjriQF7Dz4oniTi81enj5q6VuhZgeyjsSLDa1Qvq2WMkproeUKH2gVvOldcDtnL+/OEEpJwQZ9CyXBe1W6wZnVaqH9pk4322qIqHtar4c1abArYVVTSo+0Lh6mNKlkbI4DwHGA191YESZqEW29n9a4sY2G8nGRYSV5UBTJZ340VbvX93O6VLlLnluSLmNCPZEMSK8qPVc273brgo5Pt+iVV6hn8AsucC3MgwRrZFh2P0vO9h6MnZDwKUK61n79+eoF1JohIJfgypcXTxlQyKrvbHjCUw/AKLT5re9mkqibjibp4VS0h7ddCyBLzDgCEapugKZpQ8i+bTsYz2nVcZwh0zc16iEgQcYHv0euvm33223a9CmrocwWEuaWfNH3acZfKKWQL5kfDKy5BlGAQfFF0ijZOzKZ25/2ai6hUWVISAZpfzqldhqcgfxt0rR7pEtTOf4IBOnNIfNL0JTrjpcQDBl4lMfE7Oczy34zopRGwAImRvq9Vm703Zxzn82FfHGvBgTtxHMWFHZigL+oSO9STCMeD8+1XV0CpNuTMUxhDxwWcuzXIDPrc+kHceDag6uOqQtLOzW4z4IBVPbvmEbzVS9w/tQEAXfvFmDNxRtsPzUziKwQUebQDcmwKf9/EgmAZMp5MNTVg34446Y58br0dyNb9OYeQbgecZ0cTciyPv316t0za+r9LsZ4TBF/SF2omTG0P+RAJAunWEmMG78GzlblFPjSa91bt5Xfu0LaO3FyjpaIEUZ6c1ptzl2b346GhAi4xdEt2MxXhNo/5FwOnjddBUc9QTJ+F3p26uRj9f+8QFu/BcsJwy5UeT60+ZIRv3IO01BirV/pNzevjsRtu+h9+/PaEXIXfkDkl5ipvNnJQt9VYDUzr3waon3vaE8E6lJW7FUJZa33L+ql6k/Q2Wpm3W2A6ZwiEVlk+CLW4rH/IeZPPSib8zmqHKzmxfoKwKAoYtEa7GMhp39y0nvHk72nKSsTXb7zTD+UhuWnLI5saqS87Qh7JBDgkM2BtT+DMaY1U8JCb+7ZdudOFsaVOVNwDTYgYNhs0u X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d8eb6471-8239-49f7-41f8-08db554cc16c X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 May 2023 14:00:35.5900 (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: WcaeJ2IbxaprmBSHreYIwBkAJFTWXCNgc+P6YzG71Oz8qBmjSIC8nVZzP13id2El X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7954 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 927f2051715f64..8c27f6901446e8 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -15,13 +15,121 @@ MODULE_PARM_DESC( "Allow IOMMUFD to bind to devices even if the platform cannot isolate " "the MSI interrupt window. Enabling this is a security weakness."); +static void iommufd_group_release(struct kref *kref) +{ + struct iommufd_group *igroup = + container_of(kref, struct iommufd_group, ref); + + xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), igroup, + NULL, GFP_KERNEL); + iommu_group_put(igroup->group); + kfree(igroup); +} + +static void iommufd_put_group(struct iommufd_group *group) +{ + kref_put(&group->ref, iommufd_group_release); +} + +static bool iommufd_group_try_get(struct iommufd_group *igroup, + struct iommu_group *group) +{ + if (!igroup) + return false; + /* + * group ID's cannot be re-used until the group is put back which does + * not happen if we could get an igroup pointer under the xa_lock. + */ + if (WARN_ON(igroup->group != group)) + return false; + return kref_get_unless_zero(&igroup->ref); +} + +/* + * iommufd needs to store some more data for each iommu_group, we keep a + * parallel xarray indexed by iommu_group id to hold this instead of putting it + * in the core structure. To keep things simple the iommufd_group memory is + * unique within the iommufd_ctx. This makes it easy to check there are no + * memory leaks. + */ +static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, + struct device *dev) +{ + struct iommufd_group *new_igroup; + struct iommufd_group *cur_igroup; + struct iommufd_group *igroup; + struct iommu_group *group; + unsigned int id; + + group = iommu_group_get(dev); + if (!group) + return ERR_PTR(-ENODEV); + + id = iommu_group_id(group); + + xa_lock(&ictx->groups); + igroup = xa_load(&ictx->groups, id); + if (iommufd_group_try_get(igroup, group)) { + xa_unlock(&ictx->groups); + iommu_group_put(group); + return igroup; + } + xa_unlock(&ictx->groups); + + new_igroup = kzalloc(sizeof(*new_igroup), GFP_KERNEL); + if (!new_igroup) { + iommu_group_put(group); + return ERR_PTR(-ENOMEM); + } + + kref_init(&new_igroup->ref); + /* group reference moves into new_igroup */ + new_igroup->group = group; + + /* + * The ictx is not additionally refcounted here becase all objects using + * an igroup must put it before their destroy completes. + */ + new_igroup->ictx = ictx; + + /* + * We dropped the lock so igroup is invalid. NULL is a safe and likely + * value to assume for the xa_cmpxchg algorithm. + */ + cur_igroup = NULL; + xa_lock(&ictx->groups); + while (true) { + igroup = __xa_cmpxchg(&ictx->groups, id, cur_igroup, new_igroup, + GFP_KERNEL); + if (xa_is_err(igroup)) { + xa_unlock(&ictx->groups); + iommufd_put_group(new_igroup); + return ERR_PTR(xa_err(igroup)); + } + + /* new_group was successfully installed */ + if (cur_igroup == igroup) { + xa_unlock(&ictx->groups); + return new_igroup; + } + + /* Check again if the current group is any good */ + if (iommufd_group_try_get(igroup, group)) { + xa_unlock(&ictx->groups); + iommufd_put_group(new_igroup); + return igroup; + } + cur_igroup = igroup; + } +} + void iommufd_device_destroy(struct iommufd_object *obj) { struct iommufd_device *idev = container_of(obj, struct iommufd_device, obj); iommu_device_release_dma_owner(idev->dev); - iommu_group_put(idev->group); + iommufd_put_group(idev->igroup); if (!iommufd_selftest_is_mock_dev(idev->dev)) iommufd_ctx_put(idev->ictx); } @@ -46,7 +154,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, struct device *dev, u32 *id) { struct iommufd_device *idev; - struct iommu_group *group; + struct iommufd_group *igroup; int rc; /* @@ -56,9 +164,9 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, if (!device_iommu_capable(dev, IOMMU_CAP_CACHE_COHERENCY)) return ERR_PTR(-EINVAL); - group = iommu_group_get(dev); - if (!group) - return ERR_PTR(-ENODEV); + igroup = iommufd_get_group(ictx, dev); + if (IS_ERR(igroup)) + return ERR_CAST(igroup); /* * For historical compat with VFIO the insecure interrupt path is @@ -67,7 +175,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, * interrupt outside this iommufd context. */ if (!iommufd_selftest_is_mock_dev(dev) && - !iommu_group_has_isolated_msi(group)) { + !iommu_group_has_isolated_msi(igroup->group)) { if (!allow_unsafe_interrupts) { rc = -EPERM; goto out_group_put; @@ -97,8 +205,8 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY); /* The calling driver is a user until iommufd_device_unbind() */ refcount_inc(&idev->obj.users); - /* group refcount moves into iommufd_device */ - idev->group = group; + /* igroup refcount moves into iommufd_device */ + idev->igroup = igroup; /* * If the caller fails after this success it must call @@ -113,7 +221,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, out_release_owner: iommu_device_release_dma_owner(dev); out_group_put: - iommu_group_put(group); + iommufd_put_group(igroup); return ERR_PTR(rc); } EXPORT_SYMBOL_NS_GPL(iommufd_device_bind, IOMMUFD); @@ -170,14 +278,14 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, } static bool iommufd_hw_pagetable_has_group(struct iommufd_hw_pagetable *hwpt, - struct iommu_group *group) + struct iommufd_group *igroup) { struct iommufd_device *cur_dev; lockdep_assert_held(&hwpt->devices_lock); list_for_each_entry(cur_dev, &hwpt->devices, devices_item) - if (cur_dev->group == group) + if (cur_dev->igroup->group == igroup->group) return true; return false; } @@ -211,7 +319,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, } rc = iopt_table_enforce_group_resv_regions(&hwpt->ioas->iopt, idev->dev, - idev->group, &sw_msi_start); + idev->igroup->group, + &sw_msi_start); if (rc) return rc; @@ -223,8 +332,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, * FIXME: Hack around missing a device-centric iommu api, only attach to * the group once for the first device that is in the group. */ - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) { - rc = iommu_attach_group(hwpt->domain, idev->group); + if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) { + rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; } @@ -237,8 +346,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev) { - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) - iommu_detach_group(hwpt->domain, idev->group); + if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) + iommu_detach_group(hwpt->domain, idev->igroup->group); iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); } diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index b38e67d1988bdb..f45615f19798e6 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -17,6 +17,7 @@ struct iommufd_device; struct iommufd_ctx { struct file *file; struct xarray objects; + struct xarray groups; u8 account_mode; /* Compatibility with VFIO no iommu */ @@ -262,6 +263,12 @@ void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +struct iommufd_group { + struct kref ref; + struct iommufd_ctx *ictx; + struct iommu_group *group; +}; + /* * A iommufd_device object represents the binding relationship between a * consuming driver and the iommufd. These objects are created/destroyed by @@ -270,12 +277,12 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); struct iommufd_device { struct iommufd_object obj; struct iommufd_ctx *ictx; + struct iommufd_group *igroup; struct iommufd_hw_pagetable *hwpt; /* Head at iommufd_hw_pagetable::devices */ struct list_head devices_item; /* always the physical device */ struct device *dev; - struct iommu_group *group; bool enforce_cache_coherency; }; diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 3fbe636c3d8a69..e5ed5dfa91a0b5 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -183,6 +183,7 @@ static int iommufd_fops_open(struct inode *inode, struct file *filp) } xa_init_flags(&ictx->objects, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT); + xa_init(&ictx->groups); ictx->file = filp; filp->private_data = ictx; return 0; @@ -218,6 +219,7 @@ static int iommufd_fops_release(struct inode *inode, struct file *filp) if (WARN_ON(!destroyed)) break; } + WARN_ON(!xa_empty(&ictx->groups)); kfree(ictx); return 0; } From patchwork Mon May 15 14:00: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: 684194 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 61820C7EE2D for ; Mon, 15 May 2023 14:00:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238771AbjEOOAn (ORCPT ); Mon, 15 May 2023 10:00:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238599AbjEOOAl (ORCPT ); Mon, 15 May 2023 10:00:41 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2063.outbound.protection.outlook.com [40.107.244.63]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 548041BC6; Mon, 15 May 2023 07:00:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jvkCccrk0u/Jb4vcdzd2n5a56WRVWYWNKTWBAIlbh1UNkf24XlaiMJyS0hucT3pRU6Abt7+OaOdQ8cv8vhi90ueyuhCSgq9FwyoaOFPMQ+gDAogZYVQbBlzqR2KXFoI+axhB7s4RMWkblSUvdnVoT4JKkL81AWPrAQue5lwK/D624KCtXAKM/Nkt/Cs4PyayuUtoW95sWv0nvHjnO7obU+3+rVCcqyRHsCevWzSsZz8kkXjBAyXvLIb4I8ozEiyfaauOs4OYbaGfhXj+bra5QWEX2NZia3Ix+QJbbyI8pXVbcIBAUQbSer7VMD/eMFVZb2tIw+bUntDKUdtN9RS3pA== 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=2dhkBX3NGQPMFkppRP0PeyMsWOEl5rAoWp3gs2Gl7tY=; b=E3I7+DyrnEByae4nsxJ4UB1s2aKYiFzrEs9AIQoX3XZq/xMVRqEhRg/bvHL2VuyKc5Vfa+Pvv1j8YAmKPAoupW99jm51YpUVbOc+1uixdeCAXN8Ii44LOVE7Ighgae1ErH/9TB2RbEtct+CZFcLY5pXAbnrT14uBrUPrNszZbSqZkeXWbPmROzGv7sgW9Q+Woemy0kALiGkMiJGdciqx4AZutRlv3q15K4RofhHJJk8CpSvBNNJxWomRUCDZu1uqu6qBURSWayrxAAxtXK/1gIxyjuxTI7E/m7xJgSl1n3UnO4zhSYZ4AIUsyAlMUVEDRKuDicYSqmX8LBMp49iXHA== 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=2dhkBX3NGQPMFkppRP0PeyMsWOEl5rAoWp3gs2Gl7tY=; b=MYh7+y5Ve7w80SXtGYPf0baluiZrMEff3wWPHH20LW82l9mbwnCfkhapjQw+QafdlyP0SRDcdCGIBUYwPMTX4887IBN40fl5HCuAXtE3LymclrZRDB6gugdqgdgmmweqFEFzf9sa7ufLV4qUiZ+kqQIaeivytn45lTDPuFgTV89A+TB+IlOqMzqVzW1PS2XPX20ppUpOAurH/aGJmTtyD5VXJ9Ibc15cQ+O5HlmlhrLWE0AQuDpDGLickmbGLPvwmIDW2T0aWxeL71lArDXm1C3UdYVFHAKijA5qiFGipuhqaR54D2I7SsYNqt3Rea4sN/eyCLiA0pQtA6tBnCIEqQ== 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 SN7PR12MB7954.namprd12.prod.outlook.com (2603:10b6:806:344::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6387.30; Mon, 15 May 2023 14:00:36 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6387.030; Mon, 15 May 2023 14:00:36 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v7 03/19] iommufd: Replace the hwpt->devices list with iommufd_group Date: Mon, 15 May 2023 11:00:18 -0300 Message-Id: <3-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR10CA0022.namprd10.prod.outlook.com (2603:10b6:208:120::35) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SN7PR12MB7954:EE_ X-MS-Office365-Filtering-Correlation-Id: d0697ab8-1bf8-4afc-fa81-08db554cc11f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4/kwa4yWPSv4OfghTYuixkUcfnadFiQP3Pxi7cPJZlPUcFPuDW+RpS5Ftgtv675HIg3f1bsKtudeH+ury97cacsptYabyiAPmMjaFTut+jbTCSfey9yX4sBCYkVepjG3jK3M/ggCA5FGNtmMJDspKNt05VtC5n1446XFK0BE02bI53thKcUAV9fIXp9qQjCY1DGT6zjnMMOVVFs4io+G1miyj17dWnYNGNZSCSY/t99pNZ5GUk5Je2rLA4kkAQ3EGrt3IQz8WPa5z8VIVggp9MmEukVW/cSYLfub6hd1Ma84LJD2S6+GICaAoGW6pI8ZfSwskmaO2C5J5aoaJM+NzCbDRTl32OkFWm7S96YWNBWysuf4k54c3XAp9xx8hmxGgEtaxcT+fpjBXvzu2HA1BY7K3Cn+/f/jnomp5enLcXsLEFQR+tIskVR8HQTQcu8mEKpeBZT6020Tx76pdTOMa3D+r0UGWSZvILZ87hw5663AZkUFOh8JFNSKI3bsCuSuasy1KFWkpK/ZaxO3uI/fxmChI+ls3GVoATHyfaZWEBg2CwpKBI3j55GOcV2rpk1yKaLk0l5I8J3bEkPzrz/rMwa6m83g+h3fWwj/DBSYBeM= 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)(136003)(396003)(39860400002)(366004)(346002)(376002)(451199021)(83380400001)(66476007)(66556008)(66946007)(2616005)(6486002)(26005)(6506007)(6512007)(478600001)(54906003)(6666004)(186003)(5660300002)(30864003)(86362001)(8676002)(8936002)(36756003)(2906002)(316002)(4326008)(41300700001)(38100700002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: del+cPCu0H81MgcGAEs3Q8q9Yeo2JWhWM/fsAUX5wVVX5ALAqZNTnIIfrEtFyhfV3o6i1tfxXXvtVw+rV0GViwNPqpz+q0DJm1p2LrGbuwcPZuy2d4a3iiL3CfN+HFL0rwyO0P1Adj9UHsL7WzKNxpGoZk0kphaEtD8K0fCp/gjNRBL1sIWx6HsHs5KE9xvAPSSMs4j8SlG+D0lfuuE7tS2id8tYUU/Iet7es6gdW9crp8tMTen3kLR1cPDR0z4tcVjrhbV/VDN0j2WkeyDuZrGEUzFGzslyBOYAActaprQ8hQubWyWdiCXJdouL2S+DTWtsE9qNQMY5WXTrWVYvoeSgvPcoHlTcpVfPhYlPHXKMmENVjVgEH/wPzkQm+0ZZn8yfa6oVqIOYrKz0ydzjuXD1M6TUVzYbRi139i/6nd0ivTGypCMzJPi2TGZxgc8UrVDxb4v65dO3An8Qp93RdcHvJRdKxHLfk3PgNrTztrt4AWsoXMYB/vb38K8w0vJyoIyt2PdEKrL/sUXM2B+JxBeZt/NyNwQyLTJS8uf9mMekeuUc8ekygWSnsKrGYm0r1tN6cHIBb0olYogCBmTj9N+lvKEGvkPvtOfmIS3KgQqak9aUABBDrIRDgHuxeqQFNNzMiegtbWpVjqYwHpqLljKJvnNjT5W7ljh6ODq1ohr4PeEJZ6yERicLpoI5+Lho+DfdNp0N8gQdcImsxRTpvpInYmAvWrWQvyLPoJF8dtd1rIvhVWReMSg77WKchNkaw3JuCvek2pTXfQTlM7roHnVG2Z5HaEukjLQZjfY1T90WTaJk5sh+XdAfgUWEv4jzvCqQ8+2SCJYSCFgwu7wgi0tM39Zi+agGSOuTVIZG5ln3YRFjeRyoBns2IOkWQgOW1ne3HDkwocE7J5fmTp9rmVUFiybKoU+WUNW3sqkh/pXZz2Ok9x9xXCsO96mJzYnfByKS+S7OQg6JnjQOOY8u1edPNPTIfk/q6Nq8uv1rHda3PpPXhfhI2RX7y4+WtFU+lb3ya/7jkLJ0KM1ArjCTrTmqb/Ss668fUWXeamf0F2X0rj0cXJeObFUyr7OJ0hS+luJasTnjuG+10nasOpJ+LFKXoa5eAJzwfLdcCCGXfLTYylWnxBwZQktoSb+yd0CR0fs9z6exQc/GgVGmwanlpWX75ngRxBzS4WNcRP3Mb1yAhmaETaZqYCBlGT8MgpSIoyhr2knSUx5V23/RdhGhXtrRRT3S/sGuP2FdEHOfMvsJ5E3NeGN5+xmgBYJgl/+WGAl/xEDa9XMl0s2jzxhs9L16I3of+jXiypjy0mDmQkl6iSo6xTNi9TmWjjO78aQoD++C0IzJEUDITxIq9ss8bPoj6Y8tqhsw9eJFOCJTv4LEv81tpbpbxG39CIb/fMNvRBZ3t/hQoB7Z7CRNIDC0sqfNd5l98OMKi5IrBDbX8V7XehyF0T4T3pHPXkY/SAG4cwOaf1wmVwdCPw+3F1SDT/B806yrls6/DisnEQtBycBJTzv+XJ3X7OX75W+bJ5ZGQVHrCsMIrnzxLfaEjsz96EatofdweuQcNZtAN1OCiUMkmOa1AdzhFH69MlhLPE2g X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d0697ab8-1bf8-4afc-fa81-08db554cc11f X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 May 2023 14:00:35.1237 (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: ZCZtxLjL54SXFftY2o5nSuHDSEvtVqyiF5H0507HHNxVzPbX+rzYHgDZ/EAEKWbW X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7954 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The devices list was used as a simple way to avoid having per-group information. Now that this seems to be unavoidable, just commit to per-group information fully and remove the devices list from the HWPT. The iommufd_group stores the currently assigned HWPT for the entire group and we can manage the per-device attach/detach with a list in the iommufd_group. For destruction the flow is organized to make the following patches easier, the actual call to iommufd_object_destroy_user() is done at the top of the call chain without holding any locks. The HWPT to be destroyed is returned out from the locked region to make this possible. Later patches create locking that requires this. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 100 +++++++++++------------- drivers/iommu/iommufd/hw_pagetable.c | 22 +----- drivers/iommu/iommufd/iommufd_private.h | 13 ++- 3 files changed, 54 insertions(+), 81 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 8c27f6901446e8..23ebf2065e20de 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -20,9 +20,12 @@ static void iommufd_group_release(struct kref *kref) struct iommufd_group *igroup = container_of(kref, struct iommufd_group, ref); + WARN_ON(igroup->hwpt || !list_empty(&igroup->device_list)); + xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), igroup, NULL, GFP_KERNEL); iommu_group_put(igroup->group); + mutex_destroy(&igroup->lock); kfree(igroup); } @@ -83,6 +86,8 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, } kref_init(&new_igroup->ref); + mutex_init(&new_igroup->lock); + INIT_LIST_HEAD(&new_igroup->device_list); /* group reference moves into new_igroup */ new_igroup->group = group; @@ -277,29 +282,18 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, return 0; } -static bool iommufd_hw_pagetable_has_group(struct iommufd_hw_pagetable *hwpt, - struct iommufd_group *igroup) -{ - struct iommufd_device *cur_dev; - - lockdep_assert_held(&hwpt->devices_lock); - - list_for_each_entry(cur_dev, &hwpt->devices, devices_item) - if (cur_dev->igroup->group == igroup->group) - return true; - return false; -} - int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev) { phys_addr_t sw_msi_start = PHYS_ADDR_MAX; int rc; - lockdep_assert_held(&hwpt->devices_lock); + mutex_lock(&idev->igroup->lock); - if (WARN_ON(idev->hwpt)) - return -EINVAL; + if (idev->igroup->hwpt != NULL && idev->igroup->hwpt != hwpt) { + rc = -EINVAL; + goto err_unlock; + } /* * Try to upgrade the domain we have, it is an iommu driver bug to @@ -313,8 +307,9 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, hwpt->domain->ops->enforce_cache_coherency( hwpt->domain); if (!hwpt->enforce_cache_coherency) { - WARN_ON(list_empty(&hwpt->devices)); - return -EINVAL; + WARN_ON(list_empty(&idev->igroup->device_list)); + rc = -EINVAL; + goto err_unlock; } } @@ -322,51 +317,52 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, idev->igroup->group, &sw_msi_start); if (rc) - return rc; + goto err_unlock; rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); if (rc) goto err_unresv; /* - * FIXME: Hack around missing a device-centric iommu api, only attach to - * the group once for the first device that is in the group. + * Only attach to the group once for the first device that is in the + * group. All the other devices will follow this attachment. The user + * should attach every device individually to the hwpt as the per-device + * reserved regions are only updated during individual device + * attachment. */ - if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) { + if (list_empty(&idev->igroup->device_list)) { rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; + idev->igroup->hwpt = hwpt; } + refcount_inc(&hwpt->obj.users); + list_add_tail(&idev->group_item, &idev->igroup->device_list); + mutex_unlock(&idev->igroup->lock); return 0; err_unresv: iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); +err_unlock: + mutex_unlock(&idev->igroup->lock); return rc; } -void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, - struct iommufd_device *idev) +struct iommufd_hw_pagetable * +iommufd_hw_pagetable_detach(struct iommufd_device *idev) { - if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) + struct iommufd_hw_pagetable *hwpt = idev->igroup->hwpt; + + mutex_lock(&idev->igroup->lock); + list_del(&idev->group_item); + if (list_empty(&idev->igroup->device_list)) { iommu_detach_group(hwpt->domain, idev->igroup->group); + idev->igroup->hwpt = NULL; + } iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); -} + mutex_unlock(&idev->igroup->lock); -static int iommufd_device_do_attach(struct iommufd_device *idev, - struct iommufd_hw_pagetable *hwpt) -{ - int rc; - - mutex_lock(&hwpt->devices_lock); - rc = iommufd_hw_pagetable_attach(hwpt, idev); - if (rc) - goto out_unlock; - - idev->hwpt = hwpt; - refcount_inc(&hwpt->obj.users); - list_add(&idev->devices_item, &hwpt->devices); -out_unlock: - mutex_unlock(&hwpt->devices_lock); - return rc; + /* Caller must destroy hwpt */ + return hwpt; } /* @@ -375,7 +371,7 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, * Automatic domain selection will never pick a manually created domain. */ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, - struct iommufd_ioas *ioas) + struct iommufd_ioas *ioas, u32 *pt_id) { struct iommufd_hw_pagetable *hwpt; int rc; @@ -392,7 +388,7 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, if (!iommufd_lock_obj(&hwpt->obj)) continue; - rc = iommufd_device_do_attach(idev, hwpt); + rc = iommufd_hw_pagetable_attach(hwpt, idev); iommufd_put_object(&hwpt->obj); /* @@ -402,6 +398,7 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, */ if (rc == -EINVAL) continue; + *pt_id = hwpt->obj.id; goto out_unlock; } @@ -411,6 +408,7 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, goto out_unlock; } hwpt->auto_domain = true; + *pt_id = hwpt->obj.id; mutex_unlock(&ioas->mutex); iommufd_object_finalize(idev->ictx, &hwpt->obj); @@ -446,7 +444,7 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) struct iommufd_hw_pagetable *hwpt = container_of(pt_obj, struct iommufd_hw_pagetable, obj); - rc = iommufd_device_do_attach(idev, hwpt); + rc = iommufd_hw_pagetable_attach(hwpt, idev); if (rc) goto out_put_pt_obj; break; @@ -455,7 +453,7 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) struct iommufd_ioas *ioas = container_of(pt_obj, struct iommufd_ioas, obj); - rc = iommufd_device_auto_get_domain(idev, ioas); + rc = iommufd_device_auto_get_domain(idev, ioas, pt_id); if (rc) goto out_put_pt_obj; break; @@ -466,7 +464,6 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) } refcount_inc(&idev->obj.users); - *pt_id = idev->hwpt->obj.id; rc = 0; out_put_pt_obj: @@ -484,14 +481,9 @@ EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, IOMMUFD); */ void iommufd_device_detach(struct iommufd_device *idev) { - struct iommufd_hw_pagetable *hwpt = idev->hwpt; - - mutex_lock(&hwpt->devices_lock); - list_del(&idev->devices_item); - idev->hwpt = NULL; - iommufd_hw_pagetable_detach(hwpt, idev); - mutex_unlock(&hwpt->devices_lock); + struct iommufd_hw_pagetable *hwpt; + hwpt = iommufd_hw_pagetable_detach(idev); if (hwpt->auto_domain) iommufd_object_destroy_user(idev->ictx, &hwpt->obj); else diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 6cdb6749d359f3..bdb76cdb1dc347 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -11,8 +11,6 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) struct iommufd_hw_pagetable *hwpt = container_of(obj, struct iommufd_hw_pagetable, obj); - WARN_ON(!list_empty(&hwpt->devices)); - if (!list_empty(&hwpt->hwpt_item)) { mutex_lock(&hwpt->ioas->mutex); list_del(&hwpt->hwpt_item); @@ -25,7 +23,6 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) iommu_domain_free(hwpt->domain); refcount_dec(&hwpt->ioas->obj.users); - mutex_destroy(&hwpt->devices_lock); } /** @@ -52,9 +49,7 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, if (IS_ERR(hwpt)) return hwpt; - INIT_LIST_HEAD(&hwpt->devices); INIT_LIST_HEAD(&hwpt->hwpt_item); - mutex_init(&hwpt->devices_lock); /* Pairs with iommufd_hw_pagetable_destroy() */ refcount_inc(&ioas->obj.users); hwpt->ioas = ioas; @@ -65,8 +60,6 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, goto out_abort; } - mutex_lock(&hwpt->devices_lock); - /* * immediate_attach exists only to accommodate iommu drivers that cannot * directly allocate a domain. These drivers do not finish creating the @@ -76,29 +69,18 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, if (immediate_attach) { rc = iommufd_hw_pagetable_attach(hwpt, idev); if (rc) - goto out_unlock; + goto out_abort; } rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain); if (rc) goto out_detach; list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); - - if (immediate_attach) { - /* See iommufd_device_do_attach() */ - refcount_inc(&hwpt->obj.users); - idev->hwpt = hwpt; - list_add(&idev->devices_item, &hwpt->devices); - } - - mutex_unlock(&hwpt->devices_lock); return hwpt; out_detach: if (immediate_attach) - iommufd_hw_pagetable_detach(hwpt, idev); -out_unlock: - mutex_unlock(&hwpt->devices_lock); + iommufd_hw_pagetable_detach(idev); out_abort: iommufd_object_abort_and_destroy(ictx, &hwpt->obj); return ERR_PTR(rc); diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index f45615f19798e6..e2eb1db5f8f8ce 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -250,8 +250,6 @@ struct iommufd_hw_pagetable { bool msi_cookie : 1; /* Head at iommufd_ioas::hwpt_list */ struct list_head hwpt_item; - struct mutex devices_lock; - struct list_head devices; }; struct iommufd_hw_pagetable * @@ -259,14 +257,17 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, struct iommufd_device *idev, bool immediate_attach); int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); -void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, - struct iommufd_device *idev); +struct iommufd_hw_pagetable * +iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); struct iommufd_group { struct kref ref; + struct mutex lock; struct iommufd_ctx *ictx; struct iommu_group *group; + struct iommufd_hw_pagetable *hwpt; + struct list_head device_list; }; /* @@ -278,9 +279,7 @@ struct iommufd_device { struct iommufd_object obj; struct iommufd_ctx *ictx; struct iommufd_group *igroup; - struct iommufd_hw_pagetable *hwpt; - /* Head at iommufd_hw_pagetable::devices */ - struct list_head devices_item; + struct list_head group_item; /* always the physical device */ struct device *dev; bool enforce_cache_coherency; From patchwork Mon May 15 14:00:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 684195 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 1D3E4C7EE2F for ; Mon, 15 May 2023 14:00:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235175AbjEOOAo (ORCPT ); Mon, 15 May 2023 10:00:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238670AbjEOOAm (ORCPT ); Mon, 15 May 2023 10:00:42 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2063.outbound.protection.outlook.com [40.107.244.63]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2423D10F3; Mon, 15 May 2023 07:00:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VdWW4fGq4c90T+iGo7HGHf+r4rFZEceCyE1fYLlaB6hLb/2+MoXxjJvACURGynVMrM36YB9ncTFYf/tc+/GFplwl8dgSJJElgQ8n3KzfX9Z8zmu/A6f/6ZYYI5uifDHScATMNMIu34QAe4nQv1peSJ1bnBy4zepAV2ZAUQjNoAdupIZ8IY2W8oLlSiP2LM48MNsmcDEUrUoiF330AiiMrdIu0EKupO2MyMX0JEt0JDJXEQt5Pjn2BYGabWRmcWMJDoUpUDqBexS2nKgGfR5OSXjF/qUlnYwLYCVM7O4+kJ1LOHQqSI1fRTOu8PtRwxQBsW/nsboq03/6hUwkiJlq2g== 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=MfX5gH6CInzmht+WUqgUOpAiEksx5FbZDNUMJEQGNJw=; b=jJb7MFNVUDWchhkt2nRYJP55OFjes0pPmErscQGKRg+O+7ohmOrVb6H+4kvgqkIuywIyeKa9i1dbkXqqNrdFldq5w5ic6PN3E1l4RGZ2b9100fdHtaGtK6qH878lG0TjaH05FpBoGDftKllQaJwQp8K03E4nF7F6UN9F2M9X8hCfJ8H9AgT33cRXx1wpLf/ZmtPvWnsy41qEgRlLxBITvgB+Hy4hHU3PmIJGlSsgzSnvVExyQBxDkzAei5eOtW/zbNj34ZwLnsT7oaX+ZX5caVvcGFBSTSmhyAVo+Y/wgEM9PK/pE1SWjiicdpLO/MJFehvRZSzUpWzCcJH/hAYVfA== 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=MfX5gH6CInzmht+WUqgUOpAiEksx5FbZDNUMJEQGNJw=; b=DAr1BOYnWv6inast1zqbYFg6FLyL2JQTFYPePiMw/IIPqfQj9mcxIkHGDtf3xgcgEQBUvmZ5K4nyf7ByMG2pne49FPztwJTK7A/1t+P9nwV/9cDcyZx0qr7CecwXkbr3D2W1EnuewdVFVubWiDA+MxVOsSlyhZOTaNpDxgoYqzLdYnacQkdp2cVkG9jMpl4cAhbV8EhfvwuG94d+uX24cGhqmTi/D20JYQ5OlBLiiUAH5sVL+nSm1gmVcBzMDCXWJySfaORsoDfjEPXnFCmS6mj8HO0ouIYGa2KNNFkshn497aHnhHt6GjPx+BZkfIEGDiUDg6Va7WS9hrLAAL+xhg== 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 SN7PR12MB7954.namprd12.prod.outlook.com (2603:10b6:806:344::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6387.30; Mon, 15 May 2023 14:00:37 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6387.030; Mon, 15 May 2023 14:00:37 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v7 05/19] iommufd: Keep track of each device's reserved regions instead of groups Date: Mon, 15 May 2023 11:00:20 -0300 Message-Id: <5-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR03CA0014.namprd03.prod.outlook.com (2603:10b6:208:32b::19) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SN7PR12MB7954:EE_ X-MS-Office365-Filtering-Correlation-Id: 43a3c00c-4363-402d-e2bb-08db554cc14a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ph1isbtj9enj1Wo8B+pgsW2U9pmyL2oRzSblVCc9xua3q1Hn+WN3DBqvFYeeedGji1ny/8G0VmdQIaMPEJp3S33mkxciYia2UbU6jwIG0nM/PXPP5s9/xLuUGCcv0juO4THPzWBsQgb8KnbMOx3a3oc09Lysn8+E3eBAY5zPPBYyUrC61GS577I+TvtzMuDASy414MnRT96Iab8OU7OL01kdlnybYgA3bH2D+mrzhKdONjGKhWbLuRibBYgE8tT51DV+3La+VU2IVxoJti1KI3XQ9d9zJhzuyqwd/LWkOpyzgnLVU744k56b1YU/vyadzWh0RgyBHzJFzNddIuYjcu4uJfP2Ov+BqrmNKMMq9ahp5EL0U6K2iub2LpYoZmluswYRnDQvmILkz8X9fvcWbsp29f4n5DGEYknqDiztjWxK0+eW5/7XdC4kjAu32ET6+U0XebYxXIsnTeYb/bcyn9YcKdGK6MHubFuc9NEUzpVUkT0s0UrKHfdVIFMCeQNm86oo4UcFNVRkct1rqqa7atQjNukw9mb+0NVLMSX3NEwuq33GDLnVlzy5oohA/HVLlaQiMtRGEDDWsQKj6o1qW2KzaM9+T0ByP20uOBufODo= 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)(136003)(396003)(39860400002)(366004)(346002)(376002)(451199021)(83380400001)(66476007)(66556008)(66946007)(2616005)(6486002)(26005)(6506007)(6512007)(478600001)(54906003)(6666004)(186003)(5660300002)(86362001)(8676002)(8936002)(36756003)(2906002)(316002)(4326008)(41300700001)(38100700002)(66899021)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: GnBbKIC+47QYXIc4o9KEzxKC5SZP5cH7k3qvn0YDc6ybP7GASz/vvuGd1+geCh8DBtinCvfk32mkMxlnNYGD20P/huXr/CVi7HYxe/VMSlXZmQ8NqoC+kBKnKBgsr7gnwGkzSBFgL+ge9qQF/ojUxyAdvbFgL6L4tcX++gf/Nz+jtSKweZKGyVYZQ5sfrNAFjhp8G89HxA8jkYTzMSv5XdYYeSmBx+jJP2Aqh4fSK6/NQnMF0dKrUYDZK3UUfdc5K2vxZwh9GI/5R7+nTvNDbrp4S2VNss2etyFmMHBSW3VxWCmRY+OzqjazZzkyKo/2DN89tN8y1qXPilPqOR57JQSp8Kp0k7GT6Tege7jAj9YRRlgQGpfy3pWxzejNFeLWwSR8WUk8pbWpshbIOapC8Hg8qxImK3zDSQyKspGBCjOI/FNZgV4cOuRsERB4i5+kfXivgnlKdmypUDrknkbmLdNG5eIRXAo3zX6SMlQEWvFitWxV3Zhy9nam3HEnUtiZvcsv2EnMAkAYmLbagcOg4iFplKzI2uC9g9HCS7CeGbRTql3UAbi7UE/xUovicBgLytpJ/g8KvaxLJl7eK62SjJoOJ9VxqQfkkvoHhbRyKbq1EoVaEpiQX8EHkIDrRd8lk4O3ZcTNTdZhZHbrqm+5+G0npvrRr/0KifR40+ejFrlBnIPswnf+h/w2K+NxyyjRgGxxu5wvBlhNlVs1lIkdNI3AwN7qYeA2mfL5LsADBQfQMug+P03IcgF8BLEw2C2C2WWBCE+EMVitzLvFsBma/5jO4Thgum1GGSLx+LtZActikjR2Xa5VJILKq4sLvYdhVg8uUju3kc6pMem7s63yt3q01NBvAxNottP8oTWMe+VQVTMNdP/QLwZnvPAbb/bs4NCpIdMIggH5L0GNu5mDT0cGW4tKAZJ+hvX1WxniXVXz1MCbnthO4ZK7aJ13cU3e/X8kdGgrX6xB/KkPlwV8molCY4CtBFAWkI27IezHu1PaeMTyooIvViEXBAFybcjxGOC+kT8azJJZGv7tS6p1mO0YlETWAWDv1zHZcH3WxpUePkNgnnADnYWhn+4UCnafHn2fQta1V42B9rHQZJMXnWt4QTbezd0CZeXMG+zaHmy/gbiX8OTqJ/2Md5bsQL65GjdpGhg0Y7ZqF93Ja2y+fltN9wE+DoYN0+OvEtSylf2fhWzyBvzJIBBtZ+MBbvkQukmOHZKmh1idMxuPwpJ/r9QNdpmMZy2VzE7XQ5Yee3M1XwaREX2bC01WVA/o6uw07+I33BA44vDXVgPL8MKG/HrNJLCv7Bs2uMeAFBVMUwShnOFasnRWwOse7DI5w7TZ6yiWe9EQdaIoe6yN74cG1d45ScoD5Ly4l934EKIpVkEWA88ZicHyK9gSRgpkQBojlJVDK7dn/P7AW8tAy5rPhcJxVRteYYu89j6AVKuWn2xBZMg343y9AGQXBRxDQFfFwCW/pISYKBLPZNmmCXLl4Q6VXohmNtKc5Dhsl0p/oVIULSXwFKHKlWpLiBFjwFsKcEWG6Upd4CYfdh9BfpOF0iAlJDhZ2dikTZ9okSO9uHQmFTZ5dW/mJWPqIflxczcl X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 43a3c00c-4363-402d-e2bb-08db554cc14a X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 May 2023 14:00:35.3147 (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: m+qHiyAY/rLYRHtv8Vnali06SASLHqqngaSOZzfbxO/yM5ckZgmvMAhgucYKaoBM X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7954 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The driver facing API in the iommu core makes the reserved regions per-device. An algorithm in the core code consolidates the regions of all the devices in a group to return the group view. To allow for devices to be hotplugged into the group iommufd would re-load the entire group's reserved regions for each device, just in case they changed. Further iommufd already has to deal with duplicated/overlapping reserved regions as it must union all the groups together. Thus simplify all of this to just use the device reserved regions interface directly from the iommu driver. Suggested-by: Kevin Tian Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 5 ++--- drivers/iommu/iommufd/io_pagetable.c | 27 ++++++++++--------------- drivers/iommu/iommufd/iommufd_private.h | 7 +++---- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 23ebf2065e20de..cd8ded265495cc 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -313,9 +313,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, } } - rc = iopt_table_enforce_group_resv_regions(&hwpt->ioas->iopt, idev->dev, - idev->igroup->group, - &sw_msi_start); + rc = iopt_table_enforce_dev_resv_regions( + &hwpt->ioas->iopt, idev->dev, &sw_msi_start); if (rc) goto err_unlock; diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c index e0ae72b9e67f86..f842768b2e250b 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -1161,25 +1161,22 @@ void iopt_remove_access(struct io_pagetable *iopt, up_write(&iopt->domains_rwsem); } -/* Narrow the valid_iova_itree to include reserved ranges from a group. */ -int iopt_table_enforce_group_resv_regions(struct io_pagetable *iopt, - struct device *device, - struct iommu_group *group, - phys_addr_t *sw_msi_start) +/* Narrow the valid_iova_itree to include reserved ranges from a device. */ +int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt, + struct device *dev, + phys_addr_t *sw_msi_start) { struct iommu_resv_region *resv; - struct iommu_resv_region *tmp; - LIST_HEAD(group_resv_regions); + LIST_HEAD(resv_regions); unsigned int num_hw_msi = 0; unsigned int num_sw_msi = 0; int rc; down_write(&iopt->iova_rwsem); - rc = iommu_get_group_resv_regions(group, &group_resv_regions); - if (rc) - goto out_unlock; + /* FIXME: drivers allocate memory but there is no failure propogated */ + iommu_get_resv_regions(dev, &resv_regions); - list_for_each_entry(resv, &group_resv_regions, list) { + list_for_each_entry(resv, &resv_regions, list) { if (resv->type == IOMMU_RESV_DIRECT_RELAXABLE) continue; @@ -1191,7 +1188,7 @@ int iopt_table_enforce_group_resv_regions(struct io_pagetable *iopt, } rc = iopt_reserve_iova(iopt, resv->start, - resv->length - 1 + resv->start, device); + resv->length - 1 + resv->start, dev); if (rc) goto out_reserved; } @@ -1206,11 +1203,9 @@ int iopt_table_enforce_group_resv_regions(struct io_pagetable *iopt, goto out_free_resv; out_reserved: - __iopt_remove_reserved_iova(iopt, device); + __iopt_remove_reserved_iova(iopt, dev); out_free_resv: - list_for_each_entry_safe(resv, tmp, &group_resv_regions, list) - kfree(resv); -out_unlock: + iommu_put_resv_regions(dev, &resv_regions); up_write(&iopt->iova_rwsem); return rc; } diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index e2eb1db5f8f8ce..d5c3c627c0dff3 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -76,10 +76,9 @@ int iopt_table_add_domain(struct io_pagetable *iopt, struct iommu_domain *domain); void iopt_table_remove_domain(struct io_pagetable *iopt, struct iommu_domain *domain); -int iopt_table_enforce_group_resv_regions(struct io_pagetable *iopt, - struct device *device, - struct iommu_group *group, - phys_addr_t *sw_msi_start); +int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt, + struct device *dev, + phys_addr_t *sw_msi_start); int iopt_set_allow_iova(struct io_pagetable *iopt, struct rb_root_cached *allowed_iova); int iopt_reserve_iova(struct io_pagetable *iopt, unsigned long start, From patchwork Mon May 15 14:00:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 684192 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 C7894C7EE2F for ; Mon, 15 May 2023 14:00:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239058AbjEOOAt (ORCPT ); Mon, 15 May 2023 10:00:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238949AbjEOOAr (ORCPT ); Mon, 15 May 2023 10:00:47 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2063.outbound.protection.outlook.com [40.107.244.63]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 214E71FFD; Mon, 15 May 2023 07:00:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lsHLnOVI30rj+JedJpHQmt30LfuOxBREtfU5VPs74YPlO+44CuUxXLOatUrlyLWxn92aXAOvXekhBFcL7yHOWgujcgH4P2PGUzn8hPRqnG8Tg1GOGbKUEmQqZQ1eSXVxoSRcYmWK1Y0QTCPn/Bqrdb+7bJZ6nMgMqCWSumIGJLiR7j6NNhPESJ7VaHQOfQQ9N7mu0voSVTnTIKXj4KZiV/d+yplAPBK5tnEQ1peg9xA88+yEZ4tYObQ38c4GxlHfLY2x2nLIC9k7+Z2POP1fWAMqba5G2UVeCTS8oFLlJysGRE4G/0Nn0SkfRDWBrVhXrDn5+eI+aFIB2MvUsI5Acg== 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=oBHVMyUUS5meDFOuK7/NsbrN4Y5wXSJNz5cc/05khwI=; b=WyAA2LXVWK/n3IEwlvqsNE/rqvHEEgE82WtTjm/v96NZblqsa83JkW0On/p6oddsINILp5y+P5SaGgvsNzal7Klv3UykYs3BSwzBR2aZ0ndsfaFSBlgg351kl2CQNL62CX+bx/PUsjLniD6pAWyhx1IitdnKmd3I7GU1sEP11KNl02dGCg9dGjEnmbdRVkR/4Sle2zx+vkFP6FGZmNQ+yMHYMgSUD2g8XqotuNZbKCFn5NlQgMHyiJ3uC+8WI5xQY8u2Ui6kIyBk1VxoVMGdfJf2zXo0UniUyAANPwzKzyIqx2ehE0g4HL3yx/N3QJXerJ+pdzFmIq8dkd14zfzVlg== 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=oBHVMyUUS5meDFOuK7/NsbrN4Y5wXSJNz5cc/05khwI=; b=OmstpLd7t0EePLMn1iDaDXxz0fbYbQ4QbgR2khZR0q3vCLzN1Hg9uHq3Os8XSQu/WZJrYVn7Xuh9fwdDBQiqtLl3gg/oDm5M6vOwtuAqPkC0w/Ro4HHEqlJpsTPlYeQl4vP8ECKUEtdT8RvSfoiB00NJ622l7uiPd8GtPwJ4C6godo5hlRgVIypZvgFfw0NIGv9udzgSOOt+m2N4bPjg4Eym3UsGGWkpA2i0QYlRXCxlL1NKgiPOHPUUZrxPXsLJwucNhFInjlvrt/wd2Xlx7vzQpSitlKTL2z5caF276EOwIORhOb1bkHEH92yeggG6w2qJQIC1NN3bRnGPwPQiKA== 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 SN7PR12MB7954.namprd12.prod.outlook.com (2603:10b6:806:344::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6387.30; Mon, 15 May 2023 14:00:39 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6387.030; Mon, 15 May 2023 14:00:39 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v7 07/19] iommufd: Make sw_msi_start a group global Date: Mon, 15 May 2023 11:00:22 -0300 Message-Id: <7-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BL6PEPF00013DFC.NAMP222.PROD.OUTLOOK.COM (2603:10b6:22e:400:0:1001:0: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_|SN7PR12MB7954:EE_ X-MS-Office365-Filtering-Correlation-Id: ecefcfb8-1b2d-4e88-a15b-08db554cc18d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: WDrZ1pVFT9sUjD28//vFv3O3qCH24BHCCF7DLx5Glv4yCNK3nhZOtjnV8h/4UEKAcOjUNMnI3uQAEcFo3MeX88FGhkmX2sTGL+CXdWHww2KIgqtlXse4pm1NZIxpUhXLew3hWg4FCzW3W05BYjo6twGkKs5TNZOnKFgnq4M8yoY/321flfYpo6alzjvuBPZrKs+2BRd4RCxgctzxmoOCPiK5J7kF5khxMaIGFLsuMdUIS88Cer0ou4/84izWqwo4U5iKb+TJu+ChxikpufEsVhEuxpniCTJ5h7JzsV8KrGm5oP+tCtUSMiIAsU5TzMUPHkrFZLcJnDRSau5vyCaAom+HJsQztbC1fda0TceqD4c13gEYpg1gsOaApvWG7/ot+Q13xtm+NsCOMFSjqPVLR8ZhKwhnvacpMkRVBITNhr0tzAKbgLc2u8QarLK3MxII7/E4IOefFnzUSpJHJb6M6DqwAe93NDSp9GWdh/VWlhk7R4ZYJEkh6P1jAu7PgVA1xBHJhJq3tdbfhjwyA4/X1jGU1U+e7f9cXGyAnI1jDDr88m1ZvZ9zBDzODs6/Ge5ovn8oCpRscWfE0kkLSIwRXRVOzn6DPip/Rf+0KFQ8cpE= 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)(136003)(396003)(39860400002)(366004)(346002)(376002)(451199021)(83380400001)(66476007)(66556008)(66946007)(2616005)(6486002)(26005)(6506007)(6512007)(478600001)(54906003)(6666004)(186003)(5660300002)(86362001)(8676002)(8936002)(36756003)(2906002)(316002)(4326008)(41300700001)(38100700002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: cCkVlsGTaFX+WfeyN41X3/spyuXBXxN4e0gipLmD4AoBgcOP8qx9uksz2Nta6notD0CczOGXqY4ZqeUcj/CE+SGUkb+7AMKhNSvFZZHzJlaGSyMPNQZq2/Mx8NniIYSfK52l4DER8Z+UsVQpUpxXqVTYWyS66kOXla0Bq3/eRnkPG7b871PN7X5bXdIAkN+Fty2e5rkodWwaS0Hz8EwBhEJiVCacl1jHFSOhfgDXdj/Z0Fbt7mJVSeBW96v1H7ZYQGKlB/BWaW0v8ULkHsYEQJmjSlhUu5qakHG28YEBMeG7ZoXuzjwZtqrGe11Q5OOvWBG01H8ygi3oTlDb+otJAasYhTiQzAG/eVzWCdowzjBrV24P1xF52gPog4z8TUaKQB4z75vNLgwG2c/W01E3SkjNCUFNi9hveZ4Pv+7/MEqDSx3n7OYyZyo7MELbiIudjb87orpiAYtghdvzGZ4D393gf6cIRz1pg7hHG7rRGzvL4rwhHy+HXJUE7uO0a2GhE44B1owotrCJrs0XF9DjySZARYIJ3QKEJGg9duGMfu2gy4YDxFBySQkLBAqQExS2RV6otFPTKiwbWWwS7+xsggya0yh0GzqERL8PMEFeOpqTAcAo5qt2c0ckfBEso+yNVocYKjfijSyoXV92S3NzgvKK5kD6Wd5Shn1Hxi3aS8DvmX13lqxOId3HsJyKSuzZuokmDg0rm6c3uSJvCM8pxM0cHo5GWW7rn/jpYizXa1AE6THNU/G/VRTPcDCvCoGcQb6SRs9kJy4/qnViTBasQTMd8+tl9u45SL8l5nPp6ptKfFEtCfspLrnKUX0mqyQyk3Qoyc+QdJURcMMA+ciNb1aiwACqYoXrGSyPmnKlw27wgho9La/bWxZGmFk0x4W4CK2L1oOP9f+hbbK5OqNEmzwzjOJoIptWedWzl/K17uWKB0yJyYnN6/1tkx3D/WymEs/kvFN7oVeyjGwJ3/JPjmCAPjF7Njy9EkWn6MZJxPnmUqnlMb+Vgu958M80yg7ZJ45bRtANNd4g09681tl34gsWeL5UtkmrE2GHgRPfbaPCtzxVDnUwGTQNZm07iaUi6kijqMa1jn7Vf9vz8kgVE6jbfNCeeaIR4cL/aq762ahAnxn5AQQCBFOK9cTqX+tPpXAwk16bw6FoJ+3pe9ZEK5liCDZqswHA14jrpo0FIkAD9djYhCOO08Aov3JVV5n2L5b3tSP4RITaQxLD2WxdyMzw+VtDde5/aj375DgZGDq9WRzaMDMfoftpGOfsFCPP/MzACvIYMwplM49rsQHqjB2e/gp9WI6EuPApeukhG24wN1gc3RWsElmap/prHaULyAx6zpPm9U7gLDL8+oqGKnUFc72kQ+2hko53V6DCdjUrylxPJAVy5i/tgKiImEXyxFVzgz4lOJyeCdCYgfpMBdrDxwHsRr55jZi0VdUUABdSPKoym89sTDzSGm/1jUG43PneiDJG+/UgJv8UuGesDNOtNJT0YQGicZfTW2io+pq71jNSD+SZrT1eAYF6tyTbO44uhS32d1/Op1s3Iz8r7JiLxEWGrngbz4775PlFVMzKoXn8RruCyxUzyTQsrJTJ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ecefcfb8-1b2d-4e88-a15b-08db554cc18d X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 May 2023 14:00:35.7346 (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: h5ScccTTed9JBhyy2pUyCY3xjs514OhFw1vc7pY6YwphzsOMlEY+xGf1AWK1b74q X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7954 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 548288d9a5c167..b6bf447a6b1752 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -88,6 +88,7 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, kref_init(&new_igroup->ref); mutex_init(&new_igroup->lock); INIT_LIST_HEAD(&new_igroup->device_list); + new_igroup->sw_msi_start = PHYS_ADDR_MAX; /* group reference moves into new_igroup */ new_igroup->group = group; @@ -249,10 +250,10 @@ void iommufd_device_unbind(struct iommufd_device *idev) } EXPORT_SYMBOL_NS_GPL(iommufd_device_unbind, IOMMUFD); -static int iommufd_device_setup_msi(struct iommufd_device *idev, - struct iommufd_hw_pagetable *hwpt, - phys_addr_t sw_msi_start) +static int iommufd_group_setup_msi(struct iommufd_group *igroup, + struct iommufd_hw_pagetable *hwpt) { + phys_addr_t sw_msi_start = igroup->sw_msi_start; int rc; /* @@ -285,7 +286,6 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev) { - phys_addr_t sw_msi_start = PHYS_ADDR_MAX; int rc; mutex_lock(&idev->igroup->lock); @@ -313,8 +313,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, } } - rc = iopt_table_enforce_dev_resv_regions( - &hwpt->ioas->iopt, idev->dev, &sw_msi_start); + rc = iopt_table_enforce_dev_resv_regions(&hwpt->ioas->iopt, idev->dev, + &idev->igroup->sw_msi_start); if (rc) goto err_unlock; @@ -326,7 +326,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, * attachment. */ if (list_empty(&idev->igroup->device_list)) { - rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); + rc = iommufd_group_setup_msi(idev->igroup, hwpt); if (rc) goto err_unresv; diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index d5c3c627c0dff3..9ede238908e7b3 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -267,6 +267,7 @@ struct iommufd_group { struct iommu_group *group; struct iommufd_hw_pagetable *hwpt; struct list_head device_list; + phys_addr_t sw_msi_start; }; /* From patchwork Mon May 15 14:00:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 684188 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 D3A57C77B7D for ; Mon, 15 May 2023 14:01:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239056AbjEOOBA (ORCPT ); Mon, 15 May 2023 10:01:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239368AbjEOOAz (ORCPT ); Mon, 15 May 2023 10:00:55 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2063.outbound.protection.outlook.com [40.107.244.63]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF02D1BF0; Mon, 15 May 2023 07:00:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nR7Q0hNbAzURvdUP7i48+p5glMrotTsd1I2V+zeMBWWv1xex5g5Q/0spY7Tqbpmy3NkfYJv+OF7FxPZD4go8Rc8Jo78K9TtCf4TjRASiF8KJ7vENeJB7an7HnnO7qWiAPwmvx8PPhCZLrLeR1wnU+CwdCRvvAjeOGuWl3ARoZvO3aGjPptWe2HnU4kp9+wumc/W5953kCyXSBUuDxD1LC/F6VJhKcdRxsZYMthyDimbew9VMQixpGELSiQxnRdPgexGls2i8Zu2wcWrNXp+JK02gR7X1LSQ7nSTmA22nVFku3b3h9tn1ia4qgafFD4o4180K6uwHz3gBVHoXiErXgw== 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=tr9daLsL1aNCFr5AsxyhDDU/5nBie4tjazawW5BzZNQ=; b=OtQPWns6HEse4PlohYRzde5MrucC0r0oIgRn7dFUxuvRlj0c37ra9lrEm54OFMVqSt14FKHzTNEW4cnnKRJTDLoUrlLwiyPnnK81xQFEqrf/SrMbMSboTh9rBUpNN6UgJImKjVTgnF4MjvCdqJJ6n0J5U2XfoTFM3gRqOB83oKzOnALeaWRdJXIMesDFbzmMEkEU7W67316DhHQAMmRwFkBuByNsoyeVnhwkGGsRKBD09V4Ly5+wmeAR5m+UH3jV1Jxh32Uq5RVsZnzrOZ/Js3Ewq2B0+00rfv8kCDHymnQTsJLeYXDNuuHPEbdDux09D2P8xpg/sfioBhghEaQKKQ== 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=tr9daLsL1aNCFr5AsxyhDDU/5nBie4tjazawW5BzZNQ=; b=jm8sYy9ioqGFbaBxR8wzn86MstBUW6hS8E4l2dvfnLx6q2jh1f1dKim11/P9+R4uyyS6HrFMXuOqoFHAF6JKTQJgvJFgIaCK8VBw4yexLD56fvlecW4+jkIvkLJyKIZUWPAvBgQXZpllA6Mh6kbRAoh3fmsgPDFfaRI6KRW1pcIJ8pnOFV/iL7xu1MPW9j08DO52eMYfwa0Bw3kk55Hnd7dtZYDff88sOaq6mpd2TH0vcp8nYziTEtzLHdOkxKOo2UU7Y6tQpbkg/5Ws8xtbEH/j3KBA1rYOv1RTZ2+ZuNfeLRgXwUFrvPVEWG57jt86Yu6N9voGyzP8FCaVZ/8t1Q== 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 SN7PR12MB7954.namprd12.prod.outlook.com (2603:10b6:806:344::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6387.30; Mon, 15 May 2023 14:00:45 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6387.030; Mon, 15 May 2023 14:00:45 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v7 11/19] iommufd: Fix locking around hwpt allocation Date: Mon, 15 May 2023 11:00:26 -0300 Message-Id: <11-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR17CA0034.namprd17.prod.outlook.com (2603:10b6:208:15e::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_|SN7PR12MB7954:EE_ X-MS-Office365-Filtering-Correlation-Id: 656f6183-6516-47c6-77ad-08db554cc272 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: RhtQ1c6TP5Lql1rsaOilp8c20Ncp7cidhAFdGikYKlQHw2KbRxKv6QbCS++13+pO3ZjnRykWJJYfDlDW2o/QSEBqbktxCUkrf3jrg8eukSw4yevZ1qAwd7USYQDYeNNXHi5vSY3HJGyU5xR4e3c77Fkz0jrd1u+NpPEFSakwLl1/YS6HOCt2QaEA/ob5osuoKhHjfTBTQbBw4LNb3NAuj9SqdpQkrMNUvEziWkesrdEfnII+HJeCXe9HpKRZWDL0+aXYIOLSav5SJ5Lvj2cW1BKChzLhlANktMGwdHPaR9kdhDlsh7YnWLeevRSLqs33MiU94GxrLE4XSkkZIQBHs36L1CK91UzbISPsQpjVu9Of2Pc4+M9WwFYNJNSTbbIvQNY1d1WJaB0MjJVkeWG423aKKSNbS7HnXeoWEwoaVUG8WzHRzIzAll8Z4S06PL25j8gE7ffKQFyz8f3N8BYjRFTfLIBse597GMdOyMi4VHkv+Xb6xn0VJG0O41LhWt94LUMZXFtsptT/kY1an3HTz9SonuqOjdsZNPLj1i4geTcWrmVx3rRUx7LeechQ+XAF 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)(136003)(396003)(39860400002)(366004)(346002)(376002)(451199021)(83380400001)(66476007)(66556008)(66946007)(2616005)(6486002)(26005)(6506007)(6512007)(478600001)(54906003)(6666004)(186003)(5660300002)(86362001)(8676002)(8936002)(36756003)(2906002)(316002)(4326008)(41300700001)(38100700002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: TDSVVMkI94etiTsKi6aSoCRnKVXcx2Wm3qecKbuB9ahb+G/Z1FwZiE5fnkNd3FJAGvkiOAl+hpFX9JXBWjPEWGkVEI7I5svXrEQMJFlBSvwOaBUtlI6gc8aU9XF0uH9UY/bfCQtK8avoPOsTS8GWEHMFDjWbjAeZG6xowgDiGD8dmY7B86YtU2IJyyg3W3F2fzUXH3SLPUuK4u+T67la584BNXAwxvETKvfyk9GbLwkCFJIqVWQzSnZhqwoqDv9w5d9T2JXviHCdJbqvxO5nONyGxy7Ew+HjZcFDATZ+aVXoWjSK10T1MgUu1vDApW4p899CjUIx79L+FtHAoLZTPdIcWrlMTNkE/BpoQz4LRmuVg/XfxftQND7TqgV0G+8tlR6wP1G4Lru5oqNX6WkI0wmFdHLDiPx67hkQN7TZPogMRuDHpVyF6HxagfaLa3jqUefgFzqJYX0oa7ZKXE2fnSeGOSdQd/1xSeZmrhKpISQ+ehO/lixFLF5si+jDOK0Cds4Pe6SF2hdaFWC1V0GSe0Z+IYRjBAGmo17fOuNq/E840MaPqu39Q+Bbj8cvYFXfXhY+1JrfLKIuVrnB5in9un2vNQ3gYepjAvzhpFtZsgqn+gIt7i5gz5V4YT73B50A+4pTa71St7/jDtfuj5wgdmFJfdiUO2xV9TgKadI9+TRIyM6BcTZMB0v4+f40odu9ePdFkFGoEqcqnnpdlausMvXv49leU8M1aGqnInzH3nKh6ceOqgbevnRaFP+MHFL9JjKDnBzsci4U/6GZOT8q2ErvnJevM0iFzTbbKEswAXl1stNyZDhKp4SSEulhwghllQHQxIeGR2uAuI4R6xDSUfmM8YrzpVjkVQnp9ZkggIVF4ZdoTKjXV+Y8a3536Tu43Rlyyq4HXWeRGlU9bl9qBN5JFRqD7tsMPDrRNhnLAXam4GFa8dp+JcY2T06VydCP7jwvx3ry94UMiQ6CYLKvERLb0/WG8WeiQCUpULHSwtvjJedN3bL3iOKKymf/rJV2Ur5hsjjzg2cZosAJFxEkAfkXx2RvP027GP/nf06yrtNY+LvbAXo7JlS9BAq4Mz/dw6Mb3707biBDATRYG0BAJK+mHP9QXvqtruDuQ9vJMHP8J/68JjszFO3oAdhe9qj3kWyhcrQ8RPT0mTNFwYwXrf0Jz3ofOHKtsbgMEmVY/1tnPuhAoCQWAx/1D4IVUNY2L49eNtaSF67eqJcD65oaX3OMbTZAyIZOchrrpuj940wajCGoDFwyDtfHTBbN7skWXKlR6YlYCyfJvqeK09KAxOM4/+KeEPRNSIdeFQaZIYj04aiydMpJQ1it6G/jFqN6cATSK0H7REcZavHZ5cOTMpynl4J761gDDHlknrZDU1TU18mt2tmyumIo1wKtl1jKpfkdE/JQMIX0RrJUkv25biqSbjy88bKsfsZxwkQiaVMk6BHb/Aik5p6c1mKcoefmGE+imeXN/6HZ0VwExnpFPi1SxtlxQpG6Wmboshrw3EHCJNhoVmyCuBmebu3Dy1X6kccFloqSgMZ2zwrDOd9LzOWRzDc3ZEcOoUwfyvrd6/i/GYIcrP7mtUA+/onzagkP X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 656f6183-6516-47c6-77ad-08db554cc272 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 May 2023 14:00:37.2290 (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: iqjE1mufLEeTS/qeLyvae8TcuuGVYTmMjWYCgCW/LefSuTSWyi7EUV8v64Sctotg X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7954 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Due to the auto_domains mechanism the ioas->mutex must be held until the hwpt is completely setup by iommufd_object_abort_and_destroy() or iommufd_object_finalize(). This prevents a concurrent iommufd_device_auto_get_domain() from seeing an incompletely initialized object through the ioas->hwpt_list. To make this more consistent move the unlock until after finalize. Fixes: e8d57210035b ("iommufd: Add kAPI toward external drivers for physical devices") Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 2eec53c5d00e74..dc437409e70ffe 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -398,8 +398,8 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, hwpt->auto_domain = true; *pt_id = hwpt->obj.id; - mutex_unlock(&ioas->mutex); iommufd_object_finalize(idev->ictx, &hwpt->obj); + mutex_unlock(&ioas->mutex); return 0; out_unlock: mutex_unlock(&ioas->mutex); From patchwork Mon May 15 14:00:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 684193 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 A47BAC7EE2E for ; Mon, 15 May 2023 14:00:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238931AbjEOOAq (ORCPT ); Mon, 15 May 2023 10:00:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234915AbjEOOAo (ORCPT ); Mon, 15 May 2023 10:00:44 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2067.outbound.protection.outlook.com [40.107.94.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7DBDF10F8; Mon, 15 May 2023 07:00:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CKNZpwI8oJ9OgedpLafiHrOJSAg7foh95WtYDTGagXIALifzweW5wAWxGEOJctMQVRF6MtU6Lj0aBEXKpWY88SRk3zZggIeV4H8MYkzjobun8vHF50ywIMjgppP+ct/kEBH+HNiYUrKv4L6zb84XuyTyME5jk3ehLJ9osFKIf/dFe2H6ALGrQZa0O+qhfNbBPV7ACVDXvSIDV4y+HWdDzt9Kz5zrM1DLLAn3DtW17kfFvbVCyjYzj9bXZjl1vaUC2zvvYedeRExKNWSknP55Sb1lZrp549PUm5QqoWG9oBEfRxe6Z7owFkvcMLZXuEmZnIwJWBWcKt05kyESfM8DZg== 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=ebgzZQWSCLvsHkPx9jJnECsKQ6BZK+Kb866/ltNuntY=; b=XWCcI/4aBLHx4oJ/tcXv6pi/GrHnWt/CorfyrxcW2DoGeFxgZ+I7Oqe/HhRRqmFAQr/AsGUMe2b6/KV8S3ptQ3HV6mopVV+H1B3DmlqqMWMgIamZs2GOyvAuwLk1233dkBwatRS97UwJaIuaTVQ+WS6Zt0hq8H3NKOYXYVVOTZ/4KPn2O/SNRjg/YMQgKiYDxy+US4KjfabkxmmXg62ViIA4sf3mcAVBBqMDsMD8uKNhSsnT4Mz33AsZja04PgvoRHCu5nl3OXCz68t8n/4tI02M6OD1ToVwW260GYfJU0wJlU/bGxT5ASfoMh92366Mx4CShxGEAXjEzkO96fzxlg== 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=ebgzZQWSCLvsHkPx9jJnECsKQ6BZK+Kb866/ltNuntY=; b=ZPpSUvcalinI8zP0aAgxY5IjepYgLquCOKVZkT0DHJcf7npWOpS954n8aZwW9A3HnFJmFWE5Pi1xPfeFEJB/t4vLX86eC8o4ZkJXg9FX5BN0L4EI0P8sNiNUqOnc3DWrTNS5yTHUFFv5LquHr6k+ziymurafc3mJsIK1pZtbMCiA4Vy5mrGdnyCJUhpy7wSjTPIg8PmVgz9XIB41GiKHHmk430vp+JlTmacumQJDP8FHf+XAGAMj8wwLrSMpWYA1Jse6HGjr23fpVpPq3j21X0F6nXH31AJcWDIokTW3eeaysVLMsACVKPv50AfgbfDX+R1bLM/TTQQmHnUsaaUeJA== 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 SN7PR12MB7954.namprd12.prod.outlook.com (2603:10b6:806:344::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6387.30; Mon, 15 May 2023 14:00:37 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6387.030; Mon, 15 May 2023 14:00:37 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v7 13/19] iommu: Introduce a new iommu_group_replace_domain() API Date: Mon, 15 May 2023 11:00:28 -0300 Message-Id: <13-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR03CA0019.namprd03.prod.outlook.com (2603:10b6:208:32b::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_|SN7PR12MB7954:EE_ X-MS-Office365-Filtering-Correlation-Id: 4b8f7983-1d02-41a4-a73f-08db554cc14f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 1vXrlrWKYZ5kA1UioAjsKTZAg06Dh52vvU8tdLsC+qDspsTvuI++KRGcSkmgq+DRXGwIBfR4kzqDA28BWfVlI/bFeURq3kGQqm0/V/nlHFET9k+dCaiBkM5phf+q7tHAKa1cAaoIHjJaOozVBPyALrl/qdHT4FObA7tqEpT1P0wFohLsun4GE6fn7BR0bsttRqt7nt0+3tEZXnrToZ8qIoVMft51F0/u03Q9+TvGjIAu8gfbcZmX/a/nkeYMWpYEvIiqcSXPhMydHbsmtBSs14iWQmcr+2I+IASKvK6S/Nxr6ARk2kR3RSKvQv/QWbqIN2Uu8YUGvg1vFOYhkG+JAzFXHs0i+mw8qs4qvsHJIBzd9KLnKEma0/cuKCYiifPmsrehypG+9M01lInrFlQ4F89dds2iFfDGkp6bqBzERveaVJ+KwBou3hKLOXyko9kH3K2N6mXcbW08TP0IJUf3SJtHAku88UgDbhe0ZJSiHJBaJhV1y3gq4inqm3FVYDt9NLUuuQP2teRpT76vCnzn3uS4X/8iVAojbCN3W45Gqa9OpTQoOYI5dnnOnW5ZNdJ0 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)(136003)(396003)(39860400002)(366004)(346002)(376002)(451199021)(83380400001)(66476007)(66556008)(66946007)(2616005)(6486002)(26005)(6506007)(6512007)(478600001)(54906003)(6666004)(186003)(5660300002)(86362001)(8676002)(8936002)(36756003)(2906002)(316002)(4326008)(41300700001)(38100700002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: jMI83+/tlhfIx2kiu1NLJbGeVZf4/DTH+SRpwsRlXeAm3fQDweuh4RU7RmlM4lzjIgv6jwzZyxZFU8EdgxDgfKlnQ11a7UnEl3rDQoHEyIdE0vomYB5UDpBzX1L81yE6+FIrbRCC6rJcB2RnfXqhb1M9yXk/hFwYyDTerndxianNXq1sUSNg9fQTOIELIUhxQFLefNq6fFJOh3aykDaErsgX9QQVPTcazHFuk4qDul4z8uxKliNLkyfqNrGDIShlYAQKlBcDHJI2s3EW2dgpfM/m3fRzgC9UcczFe3c9TjqoiSuahh8R9Im4YeZ5nUZECo3xQO8hzwuC5mKAV0E5hVf4ZAd0W/B8OGz7J6UORJJS3lkH6xXThDbCuLxpYldb1iRV5qedJ2BC3ujswi1emnrx/I8c29fbG5gUroV2Vgz79Tyb6BLMUmJTbYHAME3eZ2DOYdKcH+Eu3Eyrxwq3UO/AbbIn6uY34yv01p1c+53grmUjxd3IYsTmi7JTyxHt8PLVKsXK4xl2Gx/L3/v/MSH9IrpDo60qH3hEXROcUW36ks00EIEfkrVgN/4blOybR7OHLPxL56KzXixstCyPMOvMGz0FyePYxCW1cox/f75LunxUES0gasjpOLkg+fjbo/rod5AXj4OJghS1ueymslj57OuG/ieI1gdSCjbnDbNoNdvEsKD8MiaknoMiV0NhX0j1mo9Wu86gg0RO+gT5S9JlSeADvSRs6n9oBmpnri/HOZUKk3aa4FSfuE91hbag4OQJVQ13I/bNI/sMm8XUOoHG2uVFepocNXzY2ufLbMlFLonpwMsC6T+jFwdn3Cb5T5XEZvAHb15hBzhi15gzvI6WrYtfW572WrIyBluHwGTMI+7yyGWB9r+I6WGetYYs6aABqVMsX7IzJHEm/T0anTRkrzWM9oIcnqrZjZ619Pq2eiu0C9whvMt2duFbePzgSkTDG6EQiWvMsK0FJICi2wJoUQTqpYb0hMk5AD8N0u01oifQf0uhXreW/rivyMJHJOd5yk8gJBQIq7vQWlzp0BlSIVNbXTa9QL9kTvrR6ngI115GagTgAteQT4Pt6dRG0Yj99ERTXJNCxIR73ARFA5k0yaWMvI0QGwrdYC1MHq4OF88q/pvfGx35w3iOne4th9hyetIJncxRB3IMH4MS+7CM6X4R1LJm9FV1pBws0t/+0IfyN3ra1xRS9Wxz09bOFBk974Cb9xLQzxBbWhR12IqDIKdW1kb9I8ThgG+O28hrxJqITI6d+ScQxFY0n3MFXWGvaA+2NMq/pqg7C/LQKY5sAA8fVZ+HjM98maGRp/6BC8dyjqA1sLUjQtBi63Pq8+5XyxYHVmq+uecWKHk+e7ZQrspW5mGIg3g+QBckFVgp1CynlYDQelRZFErr7KKFLvnMstCUXHgyHinIh4TOzBjA/ajv41nmNpKwtVRIyUCo9hMXAKMMVG3x0AJHiM9twO9K9nS5eBkn1ep8oOhh/LVZqC7AYVpYSPzGk1oczUUvEx4J6iF7ZLVT/p/cg4DGFCZyUEV5QCu9WW+6gEtdQlRoscIxbWtPl7pQ+M2vL2g8GoZ6m8y4SQkpFlh2Zvdt X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4b8f7983-1d02-41a4-a73f-08db554cc14f X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 May 2023 14:00:35.3556 (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: rxwiTZEy9GSbca1Hb36sN8xga84HwzI1OAeexKpNdG7SqiqhSpPI1ZZMpnVRtAvU X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7954 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 280585965bfec5..91573efd94881a 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -34,6 +34,7 @@ #include #include "dma-iommu.h" +#include "iommu-priv.h" #include "iommu-sva.h" @@ -2221,6 +2222,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 Mon May 15 14:00:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 684189 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 D1B19C7EE2D for ; Mon, 15 May 2023 14:00:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238825AbjEOOAy (ORCPT ); Mon, 15 May 2023 10:00:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239148AbjEOOAv (ORCPT ); Mon, 15 May 2023 10:00:51 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2067.outbound.protection.outlook.com [40.107.94.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BFEE10F8; Mon, 15 May 2023 07:00:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YUsbxoKFOqvbrySvQVKVhG46QlSciHeDtEueazBVLeDf90D9tOhWS3oGYPunyhMDa5srRQ4dNI+Y+LG4s+5/D4UKD4iSh3LCA9pVFftXev1VN3R7+qsD9YNPv+y2ZBGJm1FdJjRJIs3yXBYTHQRNuh543MnxsVdZ3ogmDGpSvxccVsLDl+Gqr2aEs1GVt+LVfHfRrXhaIgMgHXwZ0T43I0Clm5Bbe7CP2XVZHccE1k0ht6J/h/rO3+MMkO7ryTGrgohPhZNN47iN0xiXLDiU9Ddt2NZGdiaxcNDrGxw8NL7zfXNS+TqLPnQ8P3rPmv6dEfpTCyHm0JEcHVwM74gIDQ== 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=HWVwXgaFStTSomMNmDUotkjw1XsLuMadJ32RM9QmCko=; b=TaEZejwQuT6tOfdwEG/VIWQ3bwZ8hdwb/8kwFI5GJvqnbjRz273hpDjN7GZYGEgMk5zFhFvpUBiWyBqAMOkpsJDmU1noDWl9acVnqqApbL8guNcHQpA2aoJa4GQE9Ka6kZjWW8SjMpGuGZA1DOvsnf/xI1Fv1y+/DxuUtKefX/lXnYXyUqoZf9xmU6ixO9SSWeh0PvSmxwSZtgcULmKu1/UDfhKW7R8XurU64VyRVr1yDBTE9kpbAIfuImXnfv58TnLvnR8k0L6M3c+IRxDu8rUyIx3OKH15Ni7O62Zx/ogfE5C8WOF8j7tR8pjYrv8K8PHcInme0Y/4AGeD78o9Hw== 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=HWVwXgaFStTSomMNmDUotkjw1XsLuMadJ32RM9QmCko=; b=qr/sMmXK+G8b0o3PrW1dTYw5esSME/WpEfmDyKC3vVBs/rNyDwXWxWWTDEZQ53vfYIx3Sj09wtzlj6iaNLtoq2JJlBLKZhw4YjNmLcOWQxg7SDnEGIMvi8J6YpgpOF1p9tTP5AlxjcOK2KvleHeRZu6fnySpNRLY8ombG56iO1nmnh+a63tsf/emQhznZUu9D7tmXtKQ/YMKvEK80BsTkG/zO5UvsXeTYcuLBZIcUyz8cqvEZhymIh4fRYGSuvRtclhNJS/LtuJ4UDtnNKSu9CSxyV69bwWQma361OAxR2IUhWXoKULjLPzshS7hKuWRWdM1Df6mooR5vUb/Jllgpw== 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 SN7PR12MB7954.namprd12.prod.outlook.com (2603:10b6:806:344::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6387.30; Mon, 15 May 2023 14:00:42 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6387.030; Mon, 15 May 2023 14:00:42 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v7 15/19] iommufd: Make destroy_rwsem use a lock class per object type Date: Mon, 15 May 2023 11:00:30 -0300 Message-Id: <15-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BL6PEPF00013E04.NAMP222.PROD.OUTLOOK.COM (2603:10b6:22e:400:0:1001:0:19) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SN7PR12MB7954:EE_ X-MS-Office365-Filtering-Correlation-Id: a83669cd-a5f0-4fab-aa3a-08db554cc1fd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DSneXqWRa7pApvCaETCKpKupQdjBdWebQN7tIRnNzfUl5fLYl0F62/aXTiso2038jViMBwLscpQRkMDP4pFP+GlH/v5WeUR1EYN3L7t8NsmXRvldSFp5aPs5wXph4KwUTwVUZhTbXKQwzxWiiJahSyjqINRd7k/LOAD4DyXlg/yWqQZxhGQDR9kDoOy6zZ5KheOVLn+MpnfitdkEsmRdae3sGoLpNbAhes7Xu2t3nKsqOjd6YGbxkwjc3yNTWHJRMB7oNeRS+6tZ4C0bCsBviEEjbYt9S24/cl2yxcTHf3eNcB3IdDmfJIL3ZmbgWDbaZEIqNT/OzM1ITkEOFJtCZPA/hLzqVv0fsUD+Ti96bY39hoEuySAALmWk6qZg81oJX0yJ+h0gqHDNoQc/wU0Eqgp7Q3xSLsEgcMGiOrnSlB6kpf78j78eNM1mpRE25aNPB8/uYkYbYfaDvQ6KChLeS6tbunE3WqXvSFcxMYssmThax0Ve1Stg+iXvLyrq4aE6ArVqDw22rpdflEzGOu0AYT00ENRxfH/ihFqgtjnxTJHea0LM/UflGUuEgjdNSNx6 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)(136003)(396003)(39860400002)(366004)(346002)(376002)(451199021)(83380400001)(66476007)(66556008)(66946007)(2616005)(6486002)(26005)(6506007)(6512007)(478600001)(54906003)(6666004)(186003)(5660300002)(86362001)(8676002)(8936002)(36756003)(2906002)(316002)(4326008)(41300700001)(38100700002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: O9NqDnbMd9wKd4j7BOtl/nrcYkyUhCqL5Lp5cNHE+DhEWg7KClzjl/1LIXhlYrex+d/Ek/xDKIcW5fED5KjYUu8cismVghpbf9iIByPQ+pzpxDhp9+CRdWG5Ip7RzqjEfx6ZtCsFpP5z9RyA0L6c2Kdtmp9zhWRXDRXZk5RLw0UXylEw7ZE0J88SeH54UpFyxH0zZvD8GmOiTJLa8W7XmQLanhdQ6ny9/RuSjqxXkUIYc5ZfVYxfC3Mz+9eRfSM77f1bfzweGIX4kEiJcPPQtqgnqsPZYqjq3iwGBEjdRaUyngrLu5XSKl9VPgz8IKqNcd7ZXWwC5lnmBzE98sHKBHm2YV8cYiFRp6qqB4t4ZU2u94SGeZiiyfX8yVJc5hhTDlEMmwIss1jsnWxkcGCv0vX7aHXMvxPTB9kZjCN8oLhpsrXdoYEtO7RBnGfXlvfXlH3F4uU6UUBezwq/vtwiJfnTtYond0KVFhjqDhttQiU///gh9F2KJaEhNzyJLAItUou/nGj5kRxmxQEFh/jjDy2UHmnhYh+ZdCNM3iU1palI0Kbv3ANnVNRjOIiOkixGOZadPb5xveH2L9u9NivdE8+THuk2cQEThUkcNxH9gcECkQJj0lru148COfH+ekTwNOKPCkMQah+g+1sNmG17E3imfEpZ7UnP4TtxADjv1as5dN93ILa31ZeXu+41CxU6vi3cnyvOaCGXMrYA6KDmYtAkhCszt8x4C5yv4Y2yzb3Q7zWIoqhSH5K3uArjJufYwEWSjjDRWbRJBITF5/PhBrqJINbT0+6nmScX5+qEfAOKHTZS8H27Fhih31MMiOv7cNzRWOo3ZGKfbWMm8RTB1UcpRfTa1c9QzAxN2S75ndE0y0lQrAD5l5HHPaUowUmqy/aZ1YVrPewJYjO3chVVPy81eSu/PQ4dezvqUFyTtEisxS09sSfTeYil4iNbno+4uNSxkZ8TBEAu4O6HUxGw/lIVbP5zpqrZJ2IY2xNWxyfYV0FCggE6qzJLy1pFXwRi5062NiS0CDxyYajR7pHhKAQSFj6UfAyaLIBGxK1FZXhDTVEi9+DZjAmezCC+HyfBtgeXMHiHymwWMA8OszgIH+JFiwWGnGucZVcqve7hNsslJjmRCRzLATb+0thuHlLWssLnd3oGrmUlvK633Xb+2pUSvvuFgeBIo4EA3YOcMoQW0ebGLbh7aPlMK82mAygMFC3pTo55p196xWf3eqpG9nWgtfKsSaESeQH2Kp9li+K5P1kkgCJeFcnbxeNh8iAf56EhM4TB8SqpNRXeVt57eeURlxAAhlIyqUTQ96cqHumYgF4PcV+tI9P5ckhH+eKSJWUEuJ1/ggDlhWyf5Ode7yqm91rgBZVSu+yfEPlXGET3m3/n729kXGovtcDdJBbYSlxq3Mh8KzWtRUC/cg9KZxV0GggQ9tGMkyI7rrsCxVZyYwD7oiIbsbi7lNNPUXzHAwpTVNuRuAJn0vmdjVq8IP6FInzHHcr/Co9xi5TdB+av3hjhYNq+trgPNS+RkpZhklyDyLwitL6kVBecfnfZ66Ed6B0KqjdpBQ3baJxI8Jh2C169uC+oAyrBT1u/eqob X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a83669cd-a5f0-4fab-aa3a-08db554cc1fd X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 May 2023 14:00:36.4807 (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: Q7PQyEICxbOb6ktTVTPpNrIQdqaeMyj7vhBtkzdJ4JGTzRL7md3Q21OcVqpCm7Yu X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7954 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The selftest invokes things like replace under the object lock of its idev which protects the idev in a similar way to a real user. Unfortunately this triggers lockdep. A lock class per type will solve the problem. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/main.c | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 07d60d0640fee3..79942934a4d69a 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -119,6 +119,7 @@ enum iommufd_object_type { #ifdef CONFIG_IOMMUFD_TEST IOMMUFD_OBJ_SELFTEST, #endif + IOMMUFD_OBJ_MAX, }; /* Base struct for all objects with a userspace ID handle. */ diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 5b7f70543adb24..f219e47d882525 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -33,6 +33,7 @@ struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, size_t size, enum iommufd_object_type type) { + static struct lock_class_key obj_keys[IOMMUFD_OBJ_MAX]; struct iommufd_object *obj; int rc; @@ -40,7 +41,15 @@ struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, if (!obj) return ERR_PTR(-ENOMEM); obj->type = type; - init_rwsem(&obj->destroy_rwsem); + /* + * In most cases the destroy_rwsem is obtained with try so it doesn't + * interact with lockdep, however on destroy we have to sleep. This + * means if we have to destroy an object while holding a get on another + * object it triggers lockdep. Using one locking class per object type + * is a simple and reasonable way to avoid this. + */ + __init_rwsem(&obj->destroy_rwsem, "iommufd_object::destroy_rwsem", + &obj_keys[type]); refcount_set(&obj->users, 1); /* From patchwork Mon May 15 14:00:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 684190 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 496D5C7EE26 for ; Mon, 15 May 2023 14:00:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239129AbjEOOAv (ORCPT ); Mon, 15 May 2023 10:00:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239087AbjEOOAu (ORCPT ); Mon, 15 May 2023 10:00:50 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2067.outbound.protection.outlook.com [40.107.94.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D5A31FD7; Mon, 15 May 2023 07:00:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=R43DJ4Tk0kdNe1a3PLZqlFYDliC0Fwa4wf0CfFEwRMLmcAtApvNb2jSORi5BmtEmHnI2lY5KT2z9N6/4C2UHwD4RgF+6aT9Rf5cKVDge1otlkjyHUd5E17S14wrrlZExylG/vxI+rMrD4Bz74DAIQXY4v4oK9brOoiRaW11GUPX0fxNmQXNY3St4bVyG5M3k0W54lI6CBFz5CJ72POpvSDrWqMDjJvdEMcNPUFH6R3ujQqmYRDp8g1d1mjJU7VSi2Fs6sBioaii4trBUC6pOtFfn8JMs7k4MCbnbYvOlvZNohTGe3YAn5iWZRHgJT+Abqar8nsDx8+F7W8NMUuauBA== 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=OtRYv4r2IYN3tux4wNw8OdeFvoNFE7mP2gnPbZGZUXg=; b=T/5+Zr/0Cx0qYRNDq1ZMcIjh7tTXb7oRx0teEaFkehdYwwhlj1Bg46/3aL6TzPDkGCs/JSXJ4O8t6y4PXNMDvnnvsBkFBYCmyxhg5S6EyD5eSQKsaCErdbIQpEjk2giQRUH+ccMEThZipdcmbCjB6sP8w26rr8wSwlFtceSbADlMdj6A49rEKRnv5mc3//AePKSzoUQndfe6ujMaIlB3FM5DsGRCtSMHJaj1oTFdoxkSLFuyaTQEGN6rwPMbz2xur84YGIQu82pq10aGXIss06kH4ruhVrqjA13ucN2HK8RlfhN77Jon6w8SVFAQKdKm9VUObrqtCXRF95cMgufcVg== 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=OtRYv4r2IYN3tux4wNw8OdeFvoNFE7mP2gnPbZGZUXg=; b=VswrPXpeH3AnmXzwF1FanO+CnkO9e9sbNkyvfistxBkKYlSioBT48BL5tsGiAQJNtR2Y8t11husRd1sUjYwO2ABc8gzPbuEnZUOdXTeTCUE1H/Po6Kyw3upnt32KxsE8UmyyY6BjRi0tZ4TcK9wfx2+wtrKgnvmb688/iLHv255E3CbBueiv6gpAzey55471KTrhcrnuC0I8/GByKwLxXv1n795QuySsWAVZI7Z4JSuNr1VLE6vdIjBMzNvt9kmdk0LqMzi2ZFlsxQFms+rMTd/j6jIuGy/3m4YJyempnJ1ywFPsOVKspWCeSu2u57+sDPq56TCzc1ewWqW/yPnaRA== 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 SN7PR12MB7954.namprd12.prod.outlook.com (2603:10b6:806:344::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6387.30; Mon, 15 May 2023 14:00:41 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6387.030; Mon, 15 May 2023 14:00:41 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v7 17/19] iommufd: Add IOMMU_HWPT_ALLOC Date: Mon, 15 May 2023 11:00:32 -0300 Message-Id: <17-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR17CA0005.namprd17.prod.outlook.com (2603:10b6:208:15e::18) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SN7PR12MB7954:EE_ X-MS-Office365-Filtering-Correlation-Id: 8d38384c-a658-457a-126b-08db554cc1c2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: UZkr8c5LnelYMzbB5KVnkLAc/8lKC6NuYsBCswvIpj53mtcnsshlMX3TCYYVMWBZhN39qwg/DuNKSy7Qog2z5aVNMHNEh4fBYaOP8U8mT5mhazmNzbgP66jtXYXo/0L8ESXSKu/AiFs8K5wtOhN/eNvt8zl477qartEXs6KHvHpDh2+AAUKoXXLcfCyVBblkY1Xmyvac9dCQhoE7Sydg/NZrVfDi4VeX4z80Z3V3Uel6uJ2gvOU+qWr0G6F5U5mj15x40nqB39FzyOiN56qUeYxvj0/f1tNIh+c8JaUQl0CzApEnYjCrsjdesbgj0zgj1m0JcLVur6GD1Erc8t4ClX9FIniQNy+lmxlI6SULKw4iTP0QfFOaALHtcDVKxQTzTdxk3HFnH87p/yZGg5Cg4cQuBHA/+cw99UpWvYGDPUNa23y5h6PXPrOC9SlIJqHfdPHDVJjLg0Jz523boW/Gqj/4oGFdK5qjzs8a5cbCa9KntcuoxPrg8LxWmRQtY9hL89GEyxF5aQM4kDv7r06ITuzy9nAAHb5vlTBB+Y3augIJGry3GPubjlI3443prtcx 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)(136003)(396003)(39860400002)(366004)(346002)(376002)(451199021)(83380400001)(66476007)(66556008)(66946007)(2616005)(6486002)(26005)(6506007)(6512007)(478600001)(54906003)(6666004)(186003)(5660300002)(86362001)(8676002)(8936002)(36756003)(2906002)(316002)(4326008)(41300700001)(38100700002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: sgU5/eZxBqbepcB9N3OaBMY/zksQZ1126qL0EbW+yqs/6U6710sU40Ej0oNP9pa4Nwcfx0PDAAUa3v8Cqptip2EuDuuQh8lRyWziLLW9I9i3hPCRIvN/9YZNq19aoLg0BHux2lvHJBjsOkb8U+p3pgW+DfzILoJYewNXmy1v5jiUDl8e5fW9DhDvL+4G2llSDkmc2ukKzSSAIzU837lt5f5mkH2G/ppcLbfs86ypG+cr5UB5GSwQuL8myfW9dGCz4mNqEds//KKjHVPhIWhWkNL/ia2ddDQ4C4r5RQjA936Ebk0zLlWokHsbd3c8QQY6HKkeZUb0EW50LceXBVpoqQaZgd1lvzyN5ohP1HO1Wi5NPbzf51u/y6mYRDfEoi1RnOFUk/jgVXFmYbjotYGhSM6iMrqesIrltoz4Ezouzzm1shXF9uMcLgUqqVfI5xiV7LZ6UCYCZfwXLO07owZuh0cWZWvHii/D3PO4bktQEDLYR9WfKIwDFlffmE9wIN0Z0GdGs+HLvLfEtTmzKcTT2I49R5TfKDAwVBleCpFoxXQIR2PNUgZMZnhPFLAGIOhzR2/NVHuDWNXiDgPtP2L9HyJLAwIg6rVXQ0WCLW+KVCKatsvx4DvRd93rdgK+RSBndizvdP2khOVNFcEXOYho4gOWlRCCZH+XJ9+IqXEGVXXFNH86DcR/KwZxroiNPClwnsHrHtVeq+GJyOrVwUujCFdEh/wqtT0vFgEQ996kYf3oxKgWlfPN19tsRUAgDN0jLZALH24iwyQkBWYvdihyqSsSO6rejG4owrew8hhsLAwuP2C/AE0rGnK9aJLCKsyF4Pc2ZO9HE3KsDBYbInnfxKIc5GNzesvM3ioCrmw+IX4NlhS19mexdHwbYXGl9zKw0XxdPk7OOlhqOdq4JKuGuBfvjTaupuzZ0CnpIeoYFGuziAval1vQK+oSHFxEtUSUWiiB+sc4fpqR7e1/Tz+dtUDbcIgBLJXoFEc2jF5Ypu4F3EL58BOCe2Unwc6GZmnIvNHSdDORyYhN/2aEbJG1onCk5VQSolxpxCj1WRETzijWFCGiRPs8CtZYQ5i74v7IcABxjCj7rOolTCtPXLdVcS/0KCYUHhnMrGgPve7maynp2AVhThO/hiw2DeuUED4+8/5IXXt6DdlODON5j53LjlFG4CLgQxd/siAIZyu9fe0dYLYSJR2nodj6UAr9kJsZlyrRUnU6RvBFvK4ClFSELxIU3dnUGviKKJ7gIiABAgE9+ixo/+Q1/K+yYdKtdCYTTRTdvTpGCJamzsIWuJIr38GAXL9gBqUF+OQe5XHtMejkEOwsKwqvBVmH5UZy8w+XfLcucQ1NegbsljzFHB/lAi9DtsTPFBNbugSJoXB38sUIr93Z4ejmd9dqT2uN+XVMA7fQNTMyUvvLoMoccgcDNAVQmwWseANT7zU0MHPmcJ5uPNvm6xyxSDBkNEcX5Ym587QIDYo6jf0ZiPDdLsEj1o3UEBQwzHWhAERnDHdrvsULHjXspai8+O04YtJQ9MJk9EJZABIiBz+P+dtBXWjVpwvAcLdHSpLpM4uVENmwOoCeqbeS7tA/TSS0akarW5k3 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8d38384c-a658-457a-126b-08db554cc1c2 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 May 2023 14:00:36.2175 (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: XraEYZxEcazls8NI0UYVomPfRuB88qpkmLx2iYxGxvG0bYidu6I3x9Tuq3UKciML X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7954 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This allows userspace to manually create HWPTs on IOAS's and then use those HWPTs as inputs to iommufd_device_attach/replace(). Following series will extend this to allow creating iommu_domains with driver specific parameters. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/hw_pagetable.c | 46 +++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 9 +++++ drivers/iommu/iommufd/main.c | 3 ++ include/uapi/linux/iommufd.h | 26 ++++++++++++++ 4 files changed, 84 insertions(+) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 2087b51d9807ad..cf2c1504e20d84 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -3,6 +3,7 @@ * Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES */ #include +#include #include "iommufd_private.h" @@ -131,3 +132,48 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, iommufd_object_abort_and_destroy(ictx, &hwpt->obj); return ERR_PTR(rc); } + +int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) +{ + struct iommu_hwpt_alloc *cmd = ucmd->cmd; + struct iommufd_hw_pagetable *hwpt; + struct iommufd_device *idev; + struct iommufd_ioas *ioas; + int rc; + + if (cmd->flags || cmd->__reserved) + return -EOPNOTSUPP; + + idev = iommufd_get_device(ucmd, cmd->dev_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + + ioas = iommufd_get_ioas(ucmd->ictx, cmd->pt_id); + if (IS_ERR(ioas)) { + rc = PTR_ERR(ioas); + goto out_put_idev; + } + + mutex_lock(&ioas->mutex); + hwpt = iommufd_hw_pagetable_alloc(ucmd->ictx, ioas, idev, false); + if (IS_ERR(hwpt)) { + rc = PTR_ERR(hwpt); + goto out_unlock; + } + + cmd->out_hwpt_id = hwpt->obj.id; + rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); + if (rc) + goto out_hwpt; + iommufd_object_finalize(ucmd->ictx, &hwpt->obj); + goto out_unlock; + +out_hwpt: + iommufd_object_abort_and_destroy(ucmd->ictx, &hwpt->obj); +out_unlock: + mutex_unlock(&ioas->mutex); + iommufd_put_object(&ioas->obj); +out_put_idev: + iommufd_put_object(&idev->obj); + return rc; +} diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 79942934a4d69a..54916fcf8d2a3c 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -262,6 +262,7 @@ struct iommufd_hw_pagetable * iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); void iommufd_hw_pagetable_abort(struct iommufd_object *obj); +int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd); static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, struct iommufd_hw_pagetable *hwpt) @@ -298,6 +299,14 @@ struct iommufd_device { bool enforce_cache_coherency; }; +static inline struct iommufd_device * +iommufd_get_device(struct iommufd_ucmd *ucmd, u32 id) +{ + return container_of(iommufd_get_object(ucmd->ictx, id, + IOMMUFD_OBJ_DEVICE), + struct iommufd_device, obj); +} + void iommufd_device_destroy(struct iommufd_object *obj); struct iommufd_access { diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index f219e47d882525..73a91e96896252 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -265,6 +265,7 @@ static int iommufd_option(struct iommufd_ucmd *ucmd) union ucmd_buffer { struct iommu_destroy destroy; + struct iommu_hwpt_alloc hwpt; struct iommu_ioas_alloc alloc; struct iommu_ioas_allow_iovas allow_iovas; struct iommu_ioas_copy ioas_copy; @@ -296,6 +297,8 @@ struct iommufd_ioctl_op { } static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { IOCTL_OP(IOMMU_DESTROY, iommufd_destroy, struct iommu_destroy, id), + IOCTL_OP(IOMMU_HWPT_ALLOC, iommufd_hwpt_alloc, struct iommu_hwpt_alloc, + __reserved), IOCTL_OP(IOMMU_IOAS_ALLOC, iommufd_ioas_alloc_ioctl, struct iommu_ioas_alloc, out_ioas_id), IOCTL_OP(IOMMU_IOAS_ALLOW_IOVAS, iommufd_ioas_allow_iovas, diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 98ebba80cfa1fc..8245c01adca673 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -45,6 +45,7 @@ enum { IOMMUFD_CMD_IOAS_UNMAP, IOMMUFD_CMD_OPTION, IOMMUFD_CMD_VFIO_IOAS, + IOMMUFD_CMD_HWPT_ALLOC, }; /** @@ -344,4 +345,29 @@ struct iommu_vfio_ioas { __u16 __reserved; }; #define IOMMU_VFIO_IOAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VFIO_IOAS) + +/** + * struct iommu_hwpt_alloc - ioctl(IOMMU_HWPT_ALLOC) + * @size: sizeof(struct iommu_hwpt_alloc) + * @flags: Must be 0 + * @dev_id: The device to allocate this HWPT for + * @pt_id: The IOAS to connect this HWPT to + * @out_hwpt_id: The ID of the new HWPT + * @__reserved: Must be 0 + * + * Explicitly allocate a hardware page table object. This is the same object + * type that is returned by iommufd_device_attach() and represents the + * underlying iommu driver's iommu_domain kernel object. + * + * A HWPT will be created with the IOVA mappings from the given IOAS. + */ +struct iommu_hwpt_alloc { + __u32 size; + __u32 flags; + __u32 dev_id; + __u32 pt_id; + __u32 out_hwpt_id; + __u32 __reserved; +}; +#define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC) #endif From patchwork Mon May 15 14:00:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 684187 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 9CA81C77B7D for ; Mon, 15 May 2023 14:01:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239640AbjEOOBD (ORCPT ); Mon, 15 May 2023 10:01:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239474AbjEOOAz (ORCPT ); Mon, 15 May 2023 10:00:55 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2067.outbound.protection.outlook.com [40.107.94.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A68C61FDB; Mon, 15 May 2023 07:00:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lod8OiRg41VRk2sAPFYYruKjNi6OKTBoWbIZBg3QOSF0QGW2AowaQ/NpD0p3IIRzZFlrxSCTo4FuQhHxR7csQzU9FvoqksrngqOuPZWBppiNDsCsH0lc0mamGEI9QwuPztGb6OjMUDaBNzD2c32fgC9MJIrVT+UaS7qAhzaVgOzR83MwJq3Re/h0Hlf5toCnG3WJybL6h6ur/P2BV8pj5RP/gvHj2hWivMecT9ke3UDOosdGXraoZRu2Nqj6c3USMnd/HqONHTX0YJFHMz7buhnC5AxCCyDXpr9TBOkQZI2+MxPUwr5rmC+9WtV5+rGYIfOnjHWBbu7AcBuWpzPZzg== 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=yoDZT/Igs6qwPv5KuKGfSBYWF+vIWIAstp8wEac2eMA=; b=F6HUJYMneCehSZSkUlf7AskxjckKGW8u6jHAebe5Xp1CThVRfumN67JYk+UZwkAVMrqZqqHsUPhTeAGo8Pke/73ZoLS2+zY0iCTegAMqWp3Fi57T39TsDzLUBNPd75KglQ+0BxekcfrTqON65B7aeUIIyIU3yyD/hGvmdoRrPOI34Q6uW3moFqZU3JIH8j9CeAa9pf57NCsm8JfDFcj/GVEqkBvme7kqWS/MOCbFxn4IF0jCfaZ7bs3l2fG4UpO8+R7M2TLOenx5BUGFnbOa+CCYrQJX7D8g7aJyMk2Ac+DhkzfMy2RtFL50pBHBQhNLGbDPMBFjuE6G6XJRH3XzJg== 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=yoDZT/Igs6qwPv5KuKGfSBYWF+vIWIAstp8wEac2eMA=; b=CwOKvF7CknitgxFvn46i3PQVhST8PFtJfOLcNzvEkmMJ3xs+3IFztFC1HBGlNOCqeV5B66cQA0Hhm/k0PQYIRFCwqjcflYQNGyHokCJnGv6WV3/WqJPNSnKzhj2IR1HSs9KzF35pXuNqWTneqWohuao6PhRMitSMiOligSJeIbMfo3Ta1sc0x6vB3ZuNcfp2hxWatScH7WUCyaCxiYasDkeimIqo46ODLfoHLJ314knnrbjk9rM1FtCHCBKLFGw9QQk0KE8Po/gjAGoidD2Ce+Kegi+lJXOEHy6x2RmMMdF4k98Jv39Etz71FLIV3Wj7Czo0ZPUkPA34DSoYxYlSOg== 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 SN7PR12MB7954.namprd12.prod.outlook.com (2603:10b6:806:344::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6387.30; Mon, 15 May 2023 14:00:46 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6387.030; Mon, 15 May 2023 14:00:46 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v7 19/19] iommufd/selftest: Add a selftest for IOMMU_HWPT_ALLOC Date: Mon, 15 May 2023 11:00:34 -0300 Message-Id: <19-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR17CA0014.namprd17.prod.outlook.com (2603:10b6:208:15e::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_|SN7PR12MB7954:EE_ X-MS-Office365-Filtering-Correlation-Id: a3e5d1a1-a57e-41ae-a475-08db554cc308 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fhJ1NTTvolYNTTx0BqCM2mJlC8cUt8SuWLC/sZzwX6wXR3aDQJV96bkPpK21nwm0M/wST2IxhnNvXAJMkKwkH5u8o+MgvBzs6yzU9iL9YRoAFuo9DhG9fuSyFqppu/aI0p2TqAKSC/ITo3WZrefvz0ZC2DLxOkQSwrlUIh+medK2U6Gd0HHYlno5/hL5d4cy0ZQetkyUsk5rctO7VJmOGNdpvf/eBmUETqIoCG6Cu8wcJpHOY3A9Z9dENscMnHAgxuFMRzKD7G5fJxCUSExJlKpBWu1MrMKHz/zFVZcQGazDSeMAYIgpRKBneKkW+dQrkbDDos6dNQVNkyrNdPoIVKYszefHVFp1YonNFhb6LEvxWUC17FV7JaRV9r30uXCqV3ir+oMXf40RCHTtsfIeJKPPgIQ9szmczc8ZZxVtN5IP662owJPqCjtwQ6K0mBk+c/ZH3mW1QMiWfrrzFoceGicgHIucLcxuy6BdxHIpu32VjBZts1uN3z1fE/baZfbqdlx19fdm+CkkXFKnLqBvVFmWb2YdbttuD4iFWNPF8S6Ss1/CpPar23F3M3qGJEMA 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)(136003)(396003)(39860400002)(366004)(346002)(376002)(451199021)(83380400001)(66476007)(66556008)(66946007)(2616005)(6486002)(26005)(6506007)(6512007)(478600001)(54906003)(6666004)(186003)(5660300002)(86362001)(8676002)(8936002)(36756003)(2906002)(316002)(4326008)(41300700001)(38100700002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Z1MEP7rbFNb1bx39SaExL4fmnjthMIyUBxwX5PnF9MSHKbf9ZzN0ZosfB2lw9rRgtcBatK3T0sxf884s/j62lkIq+BJVL8iG5v+6/14OsnB4FMwl4X5iYpK9Gd5HugsKXb+rio0gYucxSvi/VS5L3P6FWoJoTEo8qInSVwxTixQIjettuuMexvoof3SQ9kPeA7uKtWrDj9/snkTMD0mrTiDjpWjpau+l7Md5KB44VfOowVlX95q+wYd6hfwuWJSptD0VmngHyeQorqHGLwn46vEmJhoIWMCvIZux/PwofYevvNfIX0sOxoh20/KuJWzXWqQWYCl4GInQp+lTyFhuIkbv80afYwfhriTEH7kpUxv8JcoKIt9Yv/FEzbuq7m4o9nu65v/icPcHEiWucFTs61vF+991fViHMHOy7oLzqAxQicYirFl7LdBdY1Ay4RSOiMnCJ21BhGG5iW6Gow2vAkackidE4QnUEor4ez+PR+6iJYsOuEJBL8iBWkPlERCVEMRtdmbjfHIcb7+Gq5DZTCVNjnxQDnP2fvl+ab2HmJLHdHgWrYYVAa+sFI2bjuCUpb1qgfwdCvDN4Z+lq1C2dAFkpA6TMcudE+mKWyrnKB144lgMOMtstevxjT2hT+Bz/C4M6n4CDauiJ5bH2NY8887Bu8wXmNc3K9dOCA4s2hexLNtbnfompvbZML1XU4wQtGetFOs8i7hXfNjqvSnbSGFT3u4Sy7zgMUcWIZZS73eHWqPV/P9PzJMq7a2gPK5ZCZ5Fi5ayopirifLnzKg+aY30iYzRizXbleaMRz/jk+HfTsl7Iz61tlsAIh+R7SQ6VMfRBkE4mJ59VrF4YHjw50MpC1McFvyeLGJkQArMDGGQeutL26ZWEyaZKxmeAjqDaWjT25wJv287LnPFPurBA2/Y2L9si3yepANmq3gQ0Tr3IIFaF1yFD+32ZYUc8hvvcOLBRvwCFZJ7ien0AMqSOE/t6oWQG6MiSAJgKMIU+o04kSN8KjNZ2eP2owfQee7wZygWzeX5uaTReZkBE+1eI7grQnlmXqV6Hq6/2r8pcb05FuiwVrTJqsg6RjQaJD4LtJAwR4walLduavLILUWiCHmHz4hx3J6NSLyCbbxWDDWQyUrwQVJFQzPN5nGL7fykbUtj9/nytjJJXpKQRK+PSop1KFXuNC2kMh6/t5tyjl7o62c6legWg2lButcja91nEXwZ2kqb25QoCBTSwiDghcIjWocsEByRi5w0/1R1/AyJ1EIA3+l/g+owFh+qK3Nc/5+rROcrkRJ6uSA1o5yhQblCHSS9o3vh1igEjPHNXLYBpGepSBvjCjWjCswgXTSaDy/RkD+rUmUdBbiJOoJHqeGR61xId2wBtCM36sz761nHPmAebdORJd1an3/uaMn2R7BHGK7x7LnzRU6qX1qC9atubuRA6m95NBZprMCCY9Q/g+Y64GwuYLxZ/SaV0wU5FdVeoTpgS2RcW8l/SAZteN5jN4lTQ1Rr5dZdIbzqP+5ASRZhpDtQoiRfdlwTjStwptjRWz0GWZEqly+R5+H13Sa8wV3JuQhA+gt1+HTE05FVgIDCnU4+zvqRqibEysBJ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a3e5d1a1-a57e-41ae-a475-08db554cc308 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 May 2023 14:00:38.2300 (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: m+uB8/0y0DVKSlM1dqmcb+zyQy9tZpbZzsCsBBxntxC7LpSUfm6wDp3VCwGqYjWD X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7954 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Test the basic flow. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/io_pagetable.c | 3 +++ tools/testing/selftests/iommu/iommufd.c | 15 +++++++++++++ .../selftests/iommu/iommufd_fail_nth.c | 11 +++++++++- tools/testing/selftests/iommu/iommufd_utils.h | 21 +++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c index f842768b2e250b..21052f64f95649 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -1172,6 +1172,9 @@ int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt, unsigned int num_sw_msi = 0; int rc; + if (iommufd_should_fail()) + return -EINVAL; + down_write(&iopt->iova_rwsem); /* FIXME: drivers allocate memory but there is no failure propogated */ iommu_get_resv_regions(dev, &resv_regions); diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 78841aba47d038..dc09c1de319f05 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -1338,6 +1338,21 @@ TEST_F(iommufd_mock_domain, replace) test_ioctl_destroy(ioas_id); } +TEST_F(iommufd_mock_domain, alloc_hwpt) +{ + int i; + + for (i = 0; i != variant->mock_domains; i++) { + uint32_t stddev_id; + uint32_t hwpt_id; + + test_cmd_hwpt_alloc(self->idev_ids[0], self->ioas_id, &hwpt_id); + test_cmd_mock_domain(hwpt_id, &stddev_id, NULL, NULL); + test_ioctl_destroy(stddev_id); + test_ioctl_destroy(hwpt_id); + } +} + /* VFIO compatibility IOCTLs */ TEST_F(iommufd, simple_ioctls) diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c index 8ab20df4edc8c7..d4c552e5694812 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -579,6 +579,8 @@ TEST_FAIL_NTH(basic_fail_nth, device) uint32_t ioas_id; uint32_t ioas_id2; uint32_t stdev_id; + uint32_t idev_id; + uint32_t hwpt_id; __u64 iova; self->fd = open("/dev/iommu", O_RDWR); @@ -605,11 +607,18 @@ TEST_FAIL_NTH(basic_fail_nth, device) fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL, NULL)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL, + &idev_id)) + return -1; + + if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, &hwpt_id)) return -1; if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL)) return -1; + + if (_test_cmd_mock_domain_replace(self->fd, stdev_id, hwpt_id, NULL)) + return -1; return 0; } diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 62e01412a7679e..53b4d3f2d9fc6c 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -98,6 +98,27 @@ static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id, EXPECT_ERRNO(_errno, _test_cmd_mock_domain_replace(self->fd, stdev_id, \ pt_id, NULL)) +static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, + __u32 *hwpt_id) +{ + struct iommu_hwpt_alloc cmd = { + .size = sizeof(cmd), + .dev_id = device_id, + .pt_id = pt_id, + }; + int ret; + + ret = ioctl(fd, IOMMU_HWPT_ALLOC, &cmd); + if (ret) + return ret; + if (hwpt_id) + *hwpt_id = cmd.out_hwpt_id; + return 0; +} + +#define test_cmd_hwpt_alloc(device_id, pt_id, hwpt_id) \ + ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, hwpt_id)) + static int _test_cmd_create_access(int fd, unsigned int ioas_id, __u32 *access_id, unsigned int flags) {