From patchwork Wed Mar 8 00:35:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 660805 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 98190C6FA99 for ; Wed, 8 Mar 2023 00:36:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229648AbjCHAgD (ORCPT ); Tue, 7 Mar 2023 19:36:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229477AbjCHAgC (ORCPT ); Tue, 7 Mar 2023 19:36:02 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2071.outbound.protection.outlook.com [40.107.93.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AC549CBEB; Tue, 7 Mar 2023 16:36:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=S/3iR3TuTPTIO20ft6JttpcTnOUpwxMHibQbUwl9ZVmODzE17BHxZkHxARWtHUiAnZW8eH4SmAHOYVLcEqp13qAdPmhTdHWvv501ZMVf+pkoh473svvnwdSAehNJqLwGjCDGHwTabCFJaqRAS6fOe9IdSehStZHrJtxCcc3Ydk1vSUuQFNQFPv1uz4l8E6zkKMRTakCncvQxH2lqlzax0e3n9g9rek8QbujwJkBxLDr3pXiZpJ05VvA+UqJllXON2dy48RGq79fenEV4uEo64mgdmP2j0ZYHuU3HOxMc4uSWusqcCfdB43EncmlPKNEQFfXykJdNwXsP90aupfIuGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=I67c8Nx/P56b1cVrMhLp2zEXkkfWcgqtbVYazjmWVeE=; b=TvsT6FZLNc3PAeAQoWTDjZ6ULkxu/iG7krCVkRKCgTEg4m70BzEBqz7zEfhQH9XQ/GZyRZ6TGZfl1cbzlZodfcQ/L4C56tE8i6cp2P9bN+MRgd4MndMGtRQ/Tr5DKWt6QflGJkspXC0dskMQ8vAldrznlz+G4Pybf5C47DcGerXDADWWxvRZsRn/5A3m3GDp/KW/vfC95i+812to05HTWraOg6inHR+jMc1BUt4c9Q2LcaQaeK2gWtom8yNPmmFKfbU8HFarq4sqeH56H5zfwA0vJRZiRylY+gjwGNb1YQ28Mw+h3WAtDNrGpZexE/AxzE0PHbXUxMSjhlf/eGK2uw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=I67c8Nx/P56b1cVrMhLp2zEXkkfWcgqtbVYazjmWVeE=; b=kvOXyuofOt2E6FwmkYIXCHBPg6yTHEtEEpQ+1G53HJBRMLF4xfkbhHhhb/oq4gAJxVCEL9RKbRuIXa3eQG6Nbi90FhrNYLpmJI0Gtaq9y45uYdZjBQOX2+B0BMVoC1t7S3kgL1ceiaweIsw79imUr6exD63BmuXWzTNVzxtwoc/pD1QqjeGcQCqv86sWE+H91IJo721czQ8tQf0JvXOiOMMvbJoJIWi4gqhqT3jHT/R9FMBypqDlpmLgvBc3TQHKVOuRQeiuk4GE7EK1npVxxpPeJQyjfAHNqvRHOSJyjtf7G8uSvgW2VRzarFkcgYKnJxh0bAyclUsYwLKAkR3LXg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:35:57 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:35:57 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v2 02/17] iommufd: Add iommufd_group Date: Tue, 7 Mar 2023 20:35:41 -0400 Message-Id: <2-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0247.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:10f::7) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: 2ab70f60-0cbd-415c-6b8e-08db1f6d155b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xeIuCKL0ppEMTTqGklo2gIeRs6Gx953WyejDm57C4ZnWg37V933OFi9CJfqPdZEmRKviFatFHesU8tg78YinPmPxgRi1Hidg8K9M9/rJNImHJJoa58uc+N1L+l6VSto2S/GBdHFtVNctfP0poMBpBI1XJwmD48SQtlSPXtmAT0kKr2qM1k4SJzfySG069wd+5jESnoDUzf7R4BYh3zSPexHlD2NK5PBOyF3OyiTZSo5Hs3xa6nsdHpGiNI8dQ/398v1XQXCpgYzMo8T/6LHzeShF7MWW2TVzx61nygzCKA4pA7jsT/MY0MGm9Ldk0cgqQ8IR3mZVJAzEYxg//5oz+Vv8ugoKAHfsyxxv32qWvyD7WCFV59appe46041riW/wXoCNJLZ8cAZWY8Ek/1C6LJuulQTl6C2WrkAxMMpDWqBh1b2rSkd9LOxy+VWVhx7TfS2gTQZSRRfjoVr98p/HXPF+tQSmVhbJSFw+6KQW4h/BEgpzs5/cSGPMxOLiwRUXGngEga7Cpm7ehBzjAUo3+i/SyA+Aumhmy2oaTyeiwJ7LcDI2ie4XEGjGNRAI2BDWTPu+D42fCHlsMJpBcIAlON6PAxytli2wBuOdwutGSpJgPdoSd8wwWqHMf8txT4RHAcS8aIKIHGZAqpdo0eiMBEZzLgP5yzIsVy8zhWtcbOpYt5tjz4ua/MwygYGH9pXT X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230025)(4636009)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: TWdLG34fiwS4dII5RBcyXSkpNG4NLjtEF6klsvd5fWbNNhPdQqQ9cFJVQBszCdO2mFbMtGV2XosujLuiry9n2DURh57MavGVFXI4nWWSz2fcarMDEVA+kBm327HWc+wCgUEQyahyDXILoD1cScLeUCcmBEfUmudZ/cvUlg03A0TVCXK9dFuyyXxeXj50RKZxHb1SSaZEOa2Chu8U5gPJMGtUvdrQ+nQJUS+xC4xHyFRjI44LJkAEPy7fQvv0ya2zwZ6Xemd9knjt/FezRu912TOd/8O2kY+LleKRVmd5ovZICuU5D7Hcq1S/Oraycp6A4Mjt9PrfQZFZuxG2eFxyF5OdKW3VKImfQ6oOXHLFHoexR1jjCNGe0spVkNGTyOO6l/f0xYOzoLstD0jyniblobs1l/BMdp5xIYOh/n+mFcfFXj0R3u/Sdw4KCykVQurwgwOr5jj7/V3x7ZfSKqoGDogGodTx00VnKPfAfBn358wUC6H3nZl52aoExSJf2OkhBa/cy3DQxZSdKiKKL5rQXYHb9rXKO2DGHJUKBuvryYyTtys7cBxL4Yl3DuI/G2PmMwlU/1vIoOeOs6QzdA1dt3wl6VrMrGD64CggGV1NCdl8hQvwN+0LCpmnQQbLgvqSjG7S8gwMRQfl7v0nzc2jZfYuj8ySMka57xdCsHPz/C54lQ0Qad4scDo0XMLODnd9Bnq9m4fUYeC5UBD1JUTHQXfPNvYr/3QG551nt5+5FWLwOGkbxGtbnmD6bnmuO0fVPJJYWyTd5RzS5bLnno7vznNJfdCZ0VbYvKtMkq+NeXfvjM7+PPgOOKQ1N8jZyRNp+N88ERD4dAHnAR4hbg0pc8pP2+jir1SFqHqfpcEP8OQegZkTIVigp1If/8oo4OiHGyZP3rXJOz1KRGLAjbu8uxLQulrSdtvpdU90NGi5wRNlzFKN9UPueWESdhOzK+ZcK1aHwFVsihwkuESQ5W3es3sCzwQo4sss+B9YVeP3HW1X6mdMfNSIW/iYm56ZOHdLLKGkAqt/CTU7QW63+7mvNGYtQBbOkr48oqoDoyhh6hBxMYfMepChmsudAw26eZmsGplQfWyqRPfKlpxp0J+8P9Wtu/YOR5T15HnoBgaUJuqpb1WtRp8gZGf66fLWJjvZOtJxhAFGnAvZuqfMDpn5HGLye89/YweCnLTJYF4D64/ocuMBCwzoQY591cIbZa/9O816qHEEg4sRtordDlN/h5Dm8tFwbgNkoHxTa+vqDFMUlqnpQEUTNAwIQqf9i6UWWEGlEnXLPbmNI6WGKOY0BxIzb3j8iQQnpsyXiqNJTYnzyGTOk5xlMNAFDLe6FncmiJj5uxrVpssn4+bpf0Wl3fC099IxMblFPxQSJQFSPxhJ/qf/gF111tCMaRLl5WSQqIigCoge3GTyvpZPQypJP7tdiIYj+SF0uKDDDEKP634LCE+lqJRctJkN59pj0GvoWfSqAjm46WG+GTVNUTUhnISORfKdhv7ZJs5u1TRSBQTFVsdaVaHlFcZexfyyzzsyIcXtFpoNG7Wv5wlMA3ytdte8QX3NE5s+xRbxCmrOkz0p04LjqRGLshfo4TtZ668o X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2ab70f60-0cbd-415c-6b8e-08db1f6d155b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:57.5453 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 4QB536dL4nw2bzdppX0l4V7YsODIzKuXbba7XtEdYmfIQteJaPQv+keU7Yc56nu4 X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org When the hwpt to device attachment is fairly static we could get away with the simple approach of keeping track of the groups via a device list. But with replace this is infeasible. Add an automatically managed struct that is 1:1 with the iommu_group per-ictx so we can store the necessary tracking information there. Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 141 +++++++++++++++++++++--- drivers/iommu/iommufd/iommufd_private.h | 9 +- drivers/iommu/iommufd/main.c | 2 + 3 files changed, 135 insertions(+), 17 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 63b65cdfe97f29..71dd9c7bed48f4 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -15,13 +15,121 @@ MODULE_PARM_DESC( "Allow IOMMUFD to bind to devices even if the platform cannot isolate " "the MSI interrupt window. Enabling this is a security weakness."); +static void iommufd_group_release(struct kref *kref) +{ + struct iommufd_group *igroup = + container_of(kref, struct iommufd_group, ref); + + xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), igroup, + NULL, GFP_KERNEL); + iommu_group_put(igroup->group); + kfree(igroup); +} + +static void iommufd_put_group(struct iommufd_group *group) +{ + kref_put(&group->ref, iommufd_group_release); +} + +static bool iommufd_group_try_get(struct iommufd_group *igroup, + struct iommu_group *group) +{ + if (!igroup) + return false; + /* + * group ID's cannot be re-used until the group is put back which does + * not happen if we could get an igroup pointer under the xa_lock. + */ + if (WARN_ON(igroup->group != group)) + return false; + return kref_get_unless_zero(&igroup->ref); +} + +/* + * iommufd needs to store some more data for each iommu_group, we keep a + * parallel xarray indexed by iommu_group id to hold this instead of putting it + * in the core structure. To keep things simple the iommufd_group memory is + * unique within the iommufd_ctx. This makes it easy to check there are no + * memory leaks. + */ +static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, + struct device *dev) +{ + struct iommufd_group *new_igroup; + struct iommufd_group *cur_igroup; + struct iommufd_group *igroup; + struct iommu_group *group; + unsigned int id; + + group = iommu_group_get(dev); + if (!group) + return ERR_PTR(-ENODEV); + + id = iommu_group_id(group); + + xa_lock(&ictx->groups); + igroup = xa_load(&ictx->groups, id); + if (iommufd_group_try_get(igroup, group)) { + xa_unlock(&ictx->groups); + iommu_group_put(group); + return igroup; + } + xa_unlock(&ictx->groups); + + new_igroup = kzalloc(sizeof(*new_igroup), GFP_KERNEL); + if (!new_igroup) { + iommu_group_put(group); + return ERR_PTR(-ENOMEM); + } + + kref_init(&new_igroup->ref); + /* group reference moves into new_igroup */ + new_igroup->group = group; + + /* + * The ictx is not additionally refcounted here becase all objects using + * an igroup must put it before their destroy completes. + */ + new_igroup->ictx = ictx; + + /* + * We dropped the lock so igroup is invalid. NULL is a safe and likely + * value to assume for the xa_cmpxchg algorithm. + */ + cur_igroup = NULL; + xa_lock(&ictx->groups); + while (true) { + igroup = __xa_cmpxchg(&ictx->groups, id, cur_igroup, new_igroup, + GFP_KERNEL); + if (xa_is_err(igroup)) { + xa_unlock(&ictx->groups); + iommufd_put_group(new_igroup); + return ERR_PTR(xa_err(igroup)); + } + + /* new_group was successfully installed */ + if (cur_igroup == igroup) { + xa_unlock(&ictx->groups); + return new_igroup; + } + + /* Check again if the current group is any good */ + if (iommufd_group_try_get(igroup, group)) { + xa_unlock(&ictx->groups); + iommufd_put_group(new_igroup); + return igroup; + } + cur_igroup = igroup; + } +} + void iommufd_device_destroy(struct iommufd_object *obj) { struct iommufd_device *idev = container_of(obj, struct iommufd_device, obj); iommu_device_release_dma_owner(idev->dev); - iommu_group_put(idev->group); + iommufd_put_group(idev->igroup); if (!iommufd_selftest_is_mock_dev(idev->dev)) iommufd_ctx_put(idev->ictx); } @@ -46,7 +154,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, struct device *dev, u32 *id) { struct iommufd_device *idev; - struct iommu_group *group; + struct iommufd_group *igroup; int rc; /* @@ -56,9 +164,9 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, if (!device_iommu_capable(dev, IOMMU_CAP_CACHE_COHERENCY)) return ERR_PTR(-EINVAL); - group = iommu_group_get(dev); - if (!group) - return ERR_PTR(-ENODEV); + igroup = iommufd_get_group(ictx, dev); + if (IS_ERR(igroup)) + return ERR_CAST(igroup); /* * For historical compat with VFIO the insecure interrupt path is @@ -67,7 +175,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, * interrupt outside this iommufd context. */ if (!iommufd_selftest_is_mock_dev(dev) && - !iommu_group_has_isolated_msi(group)) { + !iommu_group_has_isolated_msi(igroup->group)) { if (!allow_unsafe_interrupts) { rc = -EPERM; goto out_group_put; @@ -97,8 +205,8 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY); /* The calling driver is a user until iommufd_device_unbind() */ refcount_inc(&idev->obj.users); - /* group refcount moves into iommufd_device */ - idev->group = group; + /* igroup refcount moves into iommufd_device */ + idev->igroup = igroup; /* * If the caller fails after this success it must call @@ -113,7 +221,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, out_release_owner: iommu_device_release_dma_owner(dev); out_group_put: - iommu_group_put(group); + iommufd_put_group(igroup); return ERR_PTR(rc); } EXPORT_SYMBOL_NS_GPL(iommufd_device_bind, IOMMUFD); @@ -170,14 +278,14 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, } static bool iommufd_hw_pagetable_has_group(struct iommufd_hw_pagetable *hwpt, - struct iommu_group *group) + struct iommufd_group *igroup) { struct iommufd_device *cur_dev; lockdep_assert_held(&hwpt->devices_lock); list_for_each_entry(cur_dev, &hwpt->devices, devices_item) - if (cur_dev->group == group) + if (cur_dev->igroup->group == igroup->group) return true; return false; } @@ -211,7 +319,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, } rc = iopt_table_enforce_group_resv_regions(&hwpt->ioas->iopt, idev->dev, - idev->group, &sw_msi_start); + idev->igroup->group, + &sw_msi_start); if (rc) return rc; @@ -223,8 +332,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, * FIXME: Hack around missing a device-centric iommu api, only attach to * the group once for the first device that is in the group. */ - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) { - rc = iommu_attach_group(hwpt->domain, idev->group); + if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) { + rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; } @@ -237,8 +346,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev) { - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) - iommu_detach_group(hwpt->domain, idev->group); + if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) + iommu_detach_group(hwpt->domain, idev->igroup->group); iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); } diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index d523ef12890e1e..2544f10dae9aef 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -17,6 +17,7 @@ struct iommufd_device; struct iommufd_ctx { struct file *file; struct xarray objects; + struct xarray groups; u8 account_mode; /* Compatibility with VFIO no iommu */ @@ -262,6 +263,12 @@ void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +struct iommufd_group { + struct kref ref; + struct iommufd_ctx *ictx; + struct iommu_group *group; +}; + /* * A iommufd_device object represents the binding relationship between a * consuming driver and the iommufd. These objects are created/destroyed by @@ -270,12 +277,12 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); struct iommufd_device { struct iommufd_object obj; struct iommufd_ctx *ictx; + struct iommufd_group *igroup; struct iommufd_hw_pagetable *hwpt; /* Head at iommufd_hw_pagetable::devices */ struct list_head devices_item; /* always the physical device */ struct device *dev; - struct iommu_group *group; bool enforce_cache_coherency; }; diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 3fbe636c3d8a69..e5ed5dfa91a0b5 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -183,6 +183,7 @@ static int iommufd_fops_open(struct inode *inode, struct file *filp) } xa_init_flags(&ictx->objects, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT); + xa_init(&ictx->groups); ictx->file = filp; filp->private_data = ictx; return 0; @@ -218,6 +219,7 @@ static int iommufd_fops_release(struct inode *inode, struct file *filp) if (WARN_ON(!destroyed)) break; } + WARN_ON(!xa_empty(&ictx->groups)); kfree(ictx); return 0; } From patchwork Wed Mar 8 00:35:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 660801 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 78F80C6FD1F for ; Wed, 8 Mar 2023 00:36:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229993AbjCHAgT (ORCPT ); Tue, 7 Mar 2023 19:36:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229591AbjCHAgJ (ORCPT ); Tue, 7 Mar 2023 19:36:09 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2071.outbound.protection.outlook.com [40.107.93.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E326DA02AA; Tue, 7 Mar 2023 16:36:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RlUvTyQDKO/SemSldAsPtlZqD932LgrMQXWCz0kqpy196YpVvJrxbtNJQ3jtyv/8FrC2AjE0UxQkUwyC4LHbMtOcE1dYY3hYZ9lG6HET7b0b92SyZCh0iV6WXtYcBD0lGswxC56VqruahM+uZMLsaxV+89oQyvwFg6zVyb+ulvFJ+eiB7ugUP8CsJCfrCDmmVYCfsYXIpiVcqnCmoxjLbbhAAxrsuVGbJiLync44OTFcJ+WlLnfzmeDrz4Iq0aHrY99Mz/9T/K7UvrZ6tT/ffW/DJWEeeUboskqzQBSeYV69/42bZ+MHu63vsFkVySmGfKj4+MyOxIMSPX6gDk8dZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=hU+KKTz895e9K0iZGakjDj6U186H1wZKr+EMEj6AxeQ=; b=DYlbJwFCLIxHJMI3evcYSrLsYJjJg7MyAiS9JAjYXd2VnTrY5F1LAOjmCZyysxIOQmUnlAo17LUQ6iia1vNipLtv6NG7CX9V5p79Dzqh24e65xYzgOuLNSdtHDfhfzsUwItTXyJ5muXVZzx6IbITKnTK/8rma8lbuggarSsLzQyBPDET1jg4Q12+6oap6WvAg2z4Q9zC7QiBblPkYNkkPBLaoTDW9w1vxaCFRan6t9I6BZwkVVX9/hQ7N2vacbg+yfPAfi78Fpt+5CmaH+t/ybKcxxZ01hHLdKhrwP9xdqLgj1+3rWL+d8F/m9xUiRPK6SqLI4HwDn/ossbHh+QV2Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=hU+KKTz895e9K0iZGakjDj6U186H1wZKr+EMEj6AxeQ=; b=CsK+aEltDgBK9GS85TCZTJ0ZkKz6hUCFZCy7Dkku5KXi+uDHCJ9pLFeFwhIrWPTxa0DKnuBgb5Pk9v+Cmrk+SsJ6vXeRWqCir8gGBbG76ahM3gCjesbaW5jX0qNb7LSkZzmH7qTS+hl+10yn9ACyzshRNDPzGtWDcNDTYpCOSBHDA989w+o5mg2fiofBYP7avsd0QTLnG00VhbjPT0iEIA0IoP/eT8jLdmBa625j9RtOJqwsOMrKLIN4WwZLkCeuLlilqQgyXRNh+3xdkKeI7WFcf2gj1cNNYmt79JXTRhqEDGhuC6n9b+LtuSmZgdgmqsPuwcE8GHaCBeT8aoYiqw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36:00 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36:00 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v2 03/17] iommufd: Replace the hwpt->devices list with iommufd_group Date: Tue, 7 Mar 2023 20:35:42 -0400 Message-Id: <3-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT1P288CA0034.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01::47) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: d2114e55-333f-4cfa-9b3c-08db1f6d15d2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dX3G+/An2Izy8/yRjVv/XCSv+moKGEr5HvzlVZApxJZlViBzVfCVf9rJDvKVbCWssyIe1OLzMxMi2SCykwxVpVSLXS/t8ayOuD5tztVNc+yyDIbM7q5pFhLKQ+xv+J/Rb/prx2TddnBReNRtiZU6A0Svu72avNp9ok/c71L2nKErRFdKiinU3hI4iMeSq1faj9096EnsiLp8ydR0lRUgdpigC7hQJj+6XP+awES6a1FI+S+UGcfIYj64rBTxdMfr/qKsaflOjUi2BTBkFMW0eOv/qMgHjSNxAQzzNpbDjPnq4GybksijHJoe21Uoktk9X9qZFFVE4moUjSQ2YkvMxgrVd7A9of3+OKD2hqKIn6I75Uvp8VQXNZvsCfg0yEJACG0Q361Ox+Bs2lnwI8EHaFHHi/EwMrtDFWaltb6uGoUFe/qnT1Zt+MC/fCArjOCu1t2QdnXv3JythqPVDe7gL7gIVjkQ+90Z9tEZsgZ98uZT9zIWGC14Te7qMuU0lQJzcXjn+0bw8tSGdPEoPpdWoeN91ysZlsAs/s6qlTw1cNFUA6G2jKxWDgM9UhlCS4/ys9Jf0pCDebq759orjl2U2U+fWmY6MiMa90zbVc5OU+/y3/fp9tB+HbqZdxhctA4NmTMyyPE87SS5NlQr7tjK16liCmydoQt1AGx85UYXa5YujJNs7zY3PTV+6d7mkOPu X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230025)(4636009)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(30864003)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: oBsfMAz0pxUnaLb4ywgelJ4x1XKtOUd4m1AAdNxDqKkQFfAzX6lUouFqCBYWb838NoYUczk6NtImv9ggdoUIB2wsVKBYylr6uoOncLJmbOfZiRFlOU2bhd1WbV/o0/J9el8u3Z2A7IWekRdvTBggX3Mz3yZnwBR/qaxsCIWmm9LZ9ZNYI06cVdoUigaDAk82kP/ZA7uMhFx2HrNP1CG3tKqdchdfWqKStF34NTT8J5isJlmWfS5/0HiO5yW7wqtMgBKN+kIGvrWK9QK4DkOeEbQaxGdaqXPbX1LvxnfNmThXtcbW4L3qqnAQQwom5DTA9pQoReMGoGk82RYZB68/iA3DqC7aTMeHWbNOXPftd51+T2+r6+s8Gw3UoqYyt/bPpPj4wJTj8JWr670f4AVEPTUCmT0PLl1N9aIO+NruWvAGoCI9dscbikEd4M/DNsrkT/44kaWNLB7/vcGdSzaN2nmEVlYFqcqYdKZLw/2db/Jn8RsjDipzin+jZWEWuDlMScDhCRQ5b34ZS6fU7/ZOasPBOzoulfDlQhDY7aM60Mk81XSCAQAm2JArWp66eNpfRU/o/oRveEhnnjtrvC4t5nl/YzFMP1iaIdXnamFLtsdrEd8HTVHTixbUXdf2naN9053PnH5f7irIn7JWNcVHN0Fq/x7KOOa0AX+ptzngDAeXuLoe3b/juqRioDfSE96DxSoWCVSTj3OVIISezQ1IYLfIgttvDZsWS2baipCXcw7a49iuZxx5RlFScQ5GGw5LaVCZvJJJ6wunu68IOcoAjHKbej9J375uuJm6ehL5S7ee9zzz544CnKisorxCQEThKSRZhmc7ZL3AzRQ/hIPsR5nG/n3+20T6Dy/ALpbgtAA7DFAPHi8VdTmfvLx/g8zq4ANsR+f/vuTBkR3BePypaG9Coy3GqYcA9bg56LEZoWRNP6XiQFEKto1nbJWDuTL7k/0QSzJ35SdFSNQgDjLTjNn0TS34zPLOhT+AHb4ANWKQ8npKo+GW4yfUjwXw37Qczw+/tNql1WLc0wn4DvJH7xHu2ap9Kq9zPmI+PPfUjQjy8IUbTs2oBOMjCleEuPH28ZF/rlGUZIXkRZG7qjkf34GFznlXFx1+m11lUt52wtpaNW6U7m3bcdN8UcOIcbRR7Y9GFXG8pO+DA1xR7jbtyeHphGig+Qc1ghPxoWiAEYn/ZH8JZdidvxl34ug3P3SfPz2btAgbdRcoyAF7wNBQM5yCbJPq0G9JpU3P3Q0pYjBWwQ2QUFX2A98Hc3ZV5u5nw7QffJTnm1Wgi4O8rRjnXnSSETH+c4g+lzJd2gVCJieTIRKfUuF5s8QYspQju5s7B6LFYEFxiVVeLy3rXLmgyZ+nQfntBPYbtaTXCWl+VCanpkAhuSO5airXuO1pfLp3/cm0+bnxNlQJIHUqu2Cn44/nc7S/vSBQ/d75tGnyreeoyg94Sx+IjQY389wd/q+4I2Mb/ke0iX5zdpvkEwHlLuFGe71LtO7tnvqTuCDkFHuJneMkPFZyyD8KEO56tuuYUBGOxQNqz2A/Ywz3MD3vUDNCmbzc9NO30AMKdlDDABKCwyVeuUiIg4xxtvTHROrv X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d2114e55-333f-4cfa-9b3c-08db1f6d15d2 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:58.3108 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: QPO4n/5BmnuRe5OQkTTJFHvu6P5ZTKMmSTdjKYvX5at1CrmxmKLaGa7zySID2g+9 X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: 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. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 77 ++++++++++++------------- drivers/iommu/iommufd/hw_pagetable.c | 23 +++----- drivers/iommu/iommufd/iommufd_private.h | 13 ++--- 3 files changed, 50 insertions(+), 63 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 71dd9c7bed48f4..a4bf4c5826ded2 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -20,9 +20,12 @@ static void iommufd_group_release(struct kref *kref) struct iommufd_group *igroup = container_of(kref, struct iommufd_group, ref); + WARN_ON(igroup->hwpt || !list_empty(&igroup->device_list)); + xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), igroup, NULL, GFP_KERNEL); iommu_group_put(igroup->group); + mutex_destroy(&igroup->lock); kfree(igroup); } @@ -83,6 +86,8 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, } kref_init(&new_igroup->ref); + mutex_init(&new_igroup->lock); + INIT_LIST_HEAD(&new_igroup->device_list); /* group reference moves into new_igroup */ new_igroup->group = group; @@ -277,28 +282,15 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, return 0; } -static bool iommufd_hw_pagetable_has_group(struct iommufd_hw_pagetable *hwpt, - struct iommufd_group *igroup) -{ - struct iommufd_device *cur_dev; - - lockdep_assert_held(&hwpt->devices_lock); - - list_for_each_entry(cur_dev, &hwpt->devices, devices_item) - if (cur_dev->igroup->group == igroup->group) - return true; - return false; -} - int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev) { phys_addr_t sw_msi_start = PHYS_ADDR_MAX; int rc; - lockdep_assert_held(&hwpt->devices_lock); + lockdep_assert_held(&idev->igroup->lock); - if (WARN_ON(idev->hwpt)) + if (idev->igroup->hwpt != NULL && idev->igroup->hwpt != hwpt) return -EINVAL; /* @@ -313,7 +305,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, hwpt->domain->ops->enforce_cache_coherency( hwpt->domain); if (!hwpt->enforce_cache_coherency) { - WARN_ON(list_empty(&hwpt->devices)); + WARN_ON(list_empty(&idev->igroup->device_list)); return -EINVAL; } } @@ -329,26 +321,39 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, goto err_unresv; /* - * FIXME: Hack around missing a device-centric iommu api, only attach to - * the group once for the first device that is in the group. + * Only attach to the group once for the first device that is in the + * group. All the other devices will follow this attachment. + * The user can attach every device individually as well. */ - if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) { + if (list_empty(&idev->igroup->device_list)) { rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; + idev->igroup->hwpt = hwpt; } + refcount_inc(&hwpt->obj.users); + list_add_tail(&idev->group_item, &idev->igroup->device_list); return 0; err_unresv: iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); return rc; } -void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, - struct iommufd_device *idev) +struct iommufd_hw_pagetable * +iommufd_hw_pagetable_detach(struct iommufd_device *idev) { - if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) + struct iommufd_hw_pagetable *hwpt = idev->igroup->hwpt; + + lockdep_assert_held(&idev->igroup->lock); + + list_del(&idev->group_item); + if (list_empty(&idev->igroup->device_list)) { iommu_detach_group(hwpt->domain, idev->igroup->group); + idev->igroup->hwpt = NULL; + } iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); + /* Caller must destroy hwpt */ + return hwpt; } static int iommufd_device_do_attach(struct iommufd_device *idev, @@ -356,16 +361,9 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, { int rc; - mutex_lock(&hwpt->devices_lock); + mutex_lock(&idev->igroup->lock); rc = iommufd_hw_pagetable_attach(hwpt, idev); - if (rc) - goto out_unlock; - - idev->hwpt = hwpt; - refcount_inc(&hwpt->obj.users); - list_add(&idev->devices_item, &hwpt->devices); -out_unlock: - mutex_unlock(&hwpt->devices_lock); + mutex_unlock(&idev->igroup->lock); return rc; } @@ -375,7 +373,7 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, * Automatic domain selection will never pick a manually created domain. */ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, - struct iommufd_ioas *ioas) + struct iommufd_ioas *ioas, u32 *pt_id) { struct iommufd_hw_pagetable *hwpt; int rc; @@ -402,6 +400,7 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, */ if (rc == -EINVAL) continue; + *pt_id = hwpt->obj.id; goto out_unlock; } @@ -411,6 +410,7 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, goto out_unlock; } hwpt->auto_domain = true; + *pt_id = hwpt->obj.id; mutex_unlock(&ioas->mutex); iommufd_object_finalize(idev->ictx, &hwpt->obj); @@ -455,7 +455,7 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) struct iommufd_ioas *ioas = container_of(pt_obj, struct iommufd_ioas, obj); - rc = iommufd_device_auto_get_domain(idev, ioas); + rc = iommufd_device_auto_get_domain(idev, ioas, pt_id); if (rc) goto out_put_pt_obj; break; @@ -466,7 +466,6 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) } refcount_inc(&idev->obj.users); - *pt_id = idev->hwpt->obj.id; rc = 0; out_put_pt_obj: @@ -484,13 +483,11 @@ EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, IOMMUFD); */ void iommufd_device_detach(struct iommufd_device *idev) { - struct iommufd_hw_pagetable *hwpt = idev->hwpt; + struct iommufd_hw_pagetable *hwpt; - mutex_lock(&hwpt->devices_lock); - list_del(&idev->devices_item); - idev->hwpt = NULL; - iommufd_hw_pagetable_detach(hwpt, idev); - mutex_unlock(&hwpt->devices_lock); + mutex_lock(&idev->igroup->lock); + hwpt = iommufd_hw_pagetable_detach(idev); + mutex_unlock(&idev->igroup->lock); if (hwpt->auto_domain) iommufd_object_destroy_user(idev->ictx, &hwpt->obj); diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 6cdb6749d359f3..566eba0cd9b917 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -11,8 +11,6 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) struct iommufd_hw_pagetable *hwpt = container_of(obj, struct iommufd_hw_pagetable, obj); - WARN_ON(!list_empty(&hwpt->devices)); - if (!list_empty(&hwpt->hwpt_item)) { mutex_lock(&hwpt->ioas->mutex); list_del(&hwpt->hwpt_item); @@ -25,7 +23,6 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) iommu_domain_free(hwpt->domain); refcount_dec(&hwpt->ioas->obj.users); - mutex_destroy(&hwpt->devices_lock); } /** @@ -52,9 +49,7 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, if (IS_ERR(hwpt)) return hwpt; - INIT_LIST_HEAD(&hwpt->devices); INIT_LIST_HEAD(&hwpt->hwpt_item); - mutex_init(&hwpt->devices_lock); /* Pairs with iommufd_hw_pagetable_destroy() */ refcount_inc(&ioas->obj.users); hwpt->ioas = ioas; @@ -65,13 +60,16 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, goto out_abort; } - mutex_lock(&hwpt->devices_lock); + mutex_lock(&idev->igroup->lock); /* * immediate_attach exists only to accommodate iommu drivers that cannot * directly allocate a domain. These drivers do not finish creating the * domain until attach is completed. Thus we must have this call * sequence. Once those drivers are fixed this should be removed. + * + * Note we hold the igroup->lock here which prevents any other thread + * from observing igroup->hwpt until we finish setting it up. */ if (immediate_attach) { rc = iommufd_hw_pagetable_attach(hwpt, idev); @@ -84,21 +82,14 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, goto out_detach; list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); - if (immediate_attach) { - /* See iommufd_device_do_attach() */ - refcount_inc(&hwpt->obj.users); - idev->hwpt = hwpt; - list_add(&idev->devices_item, &hwpt->devices); - } - - mutex_unlock(&hwpt->devices_lock); + mutex_unlock(&idev->igroup->lock); return hwpt; out_detach: if (immediate_attach) - iommufd_hw_pagetable_detach(hwpt, idev); + iommufd_hw_pagetable_detach(idev); out_unlock: - mutex_unlock(&hwpt->devices_lock); + mutex_unlock(&idev->igroup->lock); out_abort: iommufd_object_abort_and_destroy(ictx, &hwpt->obj); return ERR_PTR(rc); diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 2544f10dae9aef..2ff192777f27d3 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -250,8 +250,6 @@ struct iommufd_hw_pagetable { bool msi_cookie : 1; /* Head at iommufd_ioas::hwpt_list */ struct list_head hwpt_item; - struct mutex devices_lock; - struct list_head devices; }; struct iommufd_hw_pagetable * @@ -259,14 +257,17 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, struct iommufd_device *idev, bool immediate_attach); int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); -void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, - struct iommufd_device *idev); +struct iommufd_hw_pagetable * +iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); struct iommufd_group { struct kref ref; + struct mutex lock; struct iommufd_ctx *ictx; struct iommu_group *group; + struct iommufd_hw_pagetable *hwpt; + struct list_head device_list; }; /* @@ -278,9 +279,7 @@ struct iommufd_device { struct iommufd_object obj; struct iommufd_ctx *ictx; struct iommufd_group *igroup; - struct iommufd_hw_pagetable *hwpt; - /* Head at iommufd_hw_pagetable::devices */ - struct list_head devices_item; + struct list_head group_item; /* always the physical device */ struct device *dev; bool enforce_cache_coherency; From patchwork Wed Mar 8 00:35:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 660803 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 167D9C742A7 for ; Wed, 8 Mar 2023 00:36:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229938AbjCHAgN (ORCPT ); Tue, 7 Mar 2023 19:36:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229918AbjCHAgG (ORCPT ); Tue, 7 Mar 2023 19:36:06 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on20608.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::608]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1DE89CFEE; Tue, 7 Mar 2023 16:36:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=R1I2pqWfkcqnK+iYall3XMYWMwl2sdHxheuWq3st5n3h6wWWRLHwYWnp7DARdGSBsJv3u+HcCAXnfOaBtCRbRILdLiWS8IFFKKLL8Y7r/K/7CbPabwaZDq/Id2kYD7sHEgFcSOrIziXZkJ4NM1mWdVh3Nps6z6QVFdqXGhLhImjIEJkdXMlmzy0jDopH7PDTg42EZswkK3H5rR7ALbSQgga9RjrFc3bJIlPiuRFWxxuUctx2VCwUamc6wKw6Mk+s95IRlwACnfTeXD/Lqk9HfGugbxhXNf9oZnmnSagsZBaiLhKO286/Ts9Wf9YhP1zDo52CmrKPRJgGAcA/jwqwVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=dfnuLljLZxxBwEw0tPqdr24L5v0XUTVZQV21QBqUWZY=; b=UH/nlsNRb+/eszgs5pEgZ40pcVGH1lY/hswQ2qZGogkDPnqI6h+bvVoqtHgjXW6vBlSM9ZpWc6e7S1l/4zdPe1+8QIIaUIgU2ZMkFYfx83bb2RtSvbAvkvRLvHCvc1/LAqxumosZRfTgJwlKo4ALJz2kKK2JbstiGRxejULLXZgVz89eXUz+VtLzGmCyCRYc4e1nJLWq+V6UVjOw9DVBqejS+kpd1lfij6e22Crr+m0oJJwVJU9hPARFw3FIy7wFciC8xS3ESRKEG3Muy560VcG6HQOI9BqhZRMvQSn/++AuUN7szxu3PVh9vBbnrlSPZMAoCqiqFT0p1hsNbdJhqg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=dfnuLljLZxxBwEw0tPqdr24L5v0XUTVZQV21QBqUWZY=; b=Fa6aNUcNG73KJSky/vRsBeY4l78Fm+L1gVhzJZLUUUSDnihhto2XP5d6JMlWNAlKEVrTd6zRCG9DqBOZb6nxPwZl+xUEd1Ee/ms8PXZClJGU/DPfmt253GoJ2vlP14vALUkNACl6j4t3FHcVUb2tphysEJGUw66h8RFPKJ2wemUm3gTEWZOiAacUgM5pf1/3uv60ecD4/WEwQikbx+3uuoiEggMYIfSe8zOphsAI4m86jpiocNaq26qXl3H5ZzVb15fKflm9RR4c/6VW650WLBLDxvuNzz1FHfsRaLBp4D9mdHTgFTStLmAceD+NpvpGV+d3/jYeAbwpSzME3FyK3A== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:35:59 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:35:59 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v2 04/17] iommu: Export iommu_get_resv_regions() Date: Tue, 7 Mar 2023 20:35:43 -0400 Message-Id: <4-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0306.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:10e::9) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: 73bae30b-6d9d-401c-036b-08db1f6d1597 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CTuyuCr9Qe4xvxDGXJgX4AUj9ndo7qzDjBDpzHwQKom1Gyt3CaMZeEBhiYXjZUaVuXLONATAxSLcAupJD5iJe3XsM/jl0tFSy37SGCMvhTvK6fgjq17HQtBnzwfLk0mIAf0T6GedV+JzU0nmMAvwP/SmnSiAS6wH37XtV8x4QYVeBdaaJStY6Izmj3pODfP6Tw9KNDcM8JJvjEgJxrfq54q0HtKX8bGSfKmDuzlhRJdD0L01V3AHBwu+X+eMbtiamuUKoXiDzLwaWmGTwxx+O8ZfueatkVKWxh+WZByjniq9n5E4DZdzwR8QvDMrJLDXnB3ZcghHrMV89BdxxZqvLFtvQDCDJsiosmrHY0Y0TDXNy4pMd32FwfTiDK6siju5vftlAaBdkPpFzLLDkAGdY2v+mQQqoN9aFv9rbfieG/04C2j9dclK6xWkFyn7iRkvLqdFjJew7RUxH2j4RmivTu6H1m9ORBFy1tuWbcBmhl8QMIuBas4T+Qd7uQ3o/X5beh/Nnczra3YHDXN1EuKwYkAiBxg/Fgh12ZfBY2YkT+YbzoPEzuMwjpr3m4uwcrGrbU/NDIzulumX+mUZ9KII097aRJ/QevCkNgGHlPAID8t/8PunUl1y8KfbCRjFQ8HLxoSO+MSY/Ob5CXK3gcRz75V0jTGNIQe3FRS/EUlmFhnTBuCu+7HPFQcucjqR2rc8 X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230025)(4636009)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: oNbNFT4GmjeWHebttao6FVYwdqBObU0+hxwSQG9xJWKMQ5AqBJ7yzZyInaBrbxfCxTATULpawUcxoBm6pmpUuHdPWiHdq1XHRhtfUf0gry65nqwtmVHzoVtbwBOBWoKrijUTsvmfTjq8APeBK5/2l3ARmvkO8ByKlB4aeZAdYJtFNlKHyktXDAbp5Ei4xxB7Y33ZMMnVFjYbg/Rwa+bXCST/404M+QOdxlEV5YbezIc21ttNlmiVHE75B5gDYlQiGZpy0bWI1/Mdyj+yvYX6YCTnS3j5F+dvfUWCRzNQsSHGGF3VoparU/cxAPKGtVcHRRBETwvqURH3qe4W7mQYWN//0GtiPW2bwi6B74hDZzs1BM/oyNaId2aM9y01Fv3IzrKGzahDn9p3JqxIkMV3rckv/MQqOAn/vx/fIME0VBEUbyS6TsLTv6BmwI6yYfajtnT3NzOh8wTdF5zONfxrQcYDsjsGFPHlpYYR26lHUZRJxVI0fR3ugx+1DudJHwvH5G1RIXTb2/PBuQGA8wnrty5gEXlS8LnKee5H4XgWy+V2HSVaegI/FWvVhPKGgcCK0yLVSr7w4ABGnbDQ5wJG0BcagDOEdV6/woqREcfpQQnZUcP2FQITfpxTYELdrKwYdR75D1unTDWmjsekJkEVbEIuAeNsyUl22WCDDuYms/r6IDjGAq9kTZ32iopGWCjUVCqL0DzOJ3b6eUoMyWAEiupt6IR40Kb/jdGKkk+nGKseUDm3gaMId0oodzk84MNKwXGnHpCp4KfoaarzBpJrSLv6MgdJuOEIW66LoJ6mJVGJaT08KXZDJYBxFCpr+1osutIpV63JmhKh1bjWXt82hNcNp3cuyRbOLE6TicVpQYjEQUCDW9XC+/O+4jTIMzdM0brtUHBk5IUzo/8/9O3Kt181msjLK7S4angJlMAiKELzugWYbb0dMIezrdBb0NkAAYmkMNj9XKlPXnXqhZqKd9zTkxdpM+j+SG2D4eCY/BlfKYjkZ0jd5d2WjybwodXp/TBhKb4A1+xwUD77lGM+bhzkKcza58YtB3EOSI81b9SNqdRc5IRkjXMHaZqlavwFFLl296WOjVBgnvHEhBatLXKrVSiigrVHwZdDVbWFunEYFDmmwQMzGWGzZpQnNj3NqiEOG4lhcmDVuFHyEerv6RTj/pp8I54W1EzkZmspPmfrtoKzOnTVZplH4ixAPaX1hroQoRSB07MEJeou6tjpGnww4h7FeF+yFbGxntJT7r3Y3h7y0ss9NkMWlV/IOCJaCmJPoPkMpDzBJ5aUOLylTolFJ6O21VXooB81dzpTk97L1XN4qU9ByCEV07zX59lwsy19lMIpl8BJZ9FRDVA1QJjAd+UT8Dw60Am5xUvHfnXhO8Imq6zSaiRD1PCamvi6X4sf0OySDORq4u2ZHqTFZGSf89jilEseZ3JipCyUIEJcSlFUmHmSIsgKSuhZFXUCHToGJt37cqZKMF/wPmgMY7HCVjstKDpiVwTcZnsI2kExcbA6SjNAnNtiBYNMbxOuFGuVxWd8iljZ5JLdbyl4U1T9B5XiuQQA/qnK2QtW6BZl/10ijnigFc0h2OYP/BVz X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 73bae30b-6d9d-401c-036b-08db1f6d1597 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:57.8890 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: zXfEGmgiQbsyT5ieu6HD4dBnauSCjZebb6tG/TnNdW81RUBZfQGDtYCjuYMYZ5je X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org iommufd wants to use this in the next patch. For some reason the iommu_put_resv_regions() was already exported. Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommu.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 10db680acaed5a..e57462bb06d9f0 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2639,6 +2639,14 @@ int iommu_set_pgtable_quirks(struct iommu_domain *domain, } EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks); +/** + * iommu_get_resv_regions - get reserved regions + * @dev: device for which to get reserved regions + * @list: reserved region list for device + * + * This returns a list of reserved IOVA regions specific to this device. + * A domain user should not map IOVA to these ranges. + */ void iommu_get_resv_regions(struct device *dev, struct list_head *list) { const struct iommu_ops *ops = dev_iommu_ops(dev); @@ -2646,9 +2654,10 @@ void iommu_get_resv_regions(struct device *dev, struct list_head *list) if (ops->get_resv_regions) ops->get_resv_regions(dev, list); } +EXPORT_SYMBOL_GPL(iommu_get_resv_regions); /** - * iommu_put_resv_regions - release resered regions + * iommu_put_resv_regions - release reserved regions * @dev: device for which to free reserved regions * @list: reserved region list for device * From patchwork Wed Mar 8 00:35:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 660802 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 A776AC742A7 for ; Wed, 8 Mar 2023 00:36:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229976AbjCHAgS (ORCPT ); Tue, 7 Mar 2023 19:36:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229815AbjCHAgJ (ORCPT ); Tue, 7 Mar 2023 19:36:09 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on20608.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::608]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6216A0B1C; Tue, 7 Mar 2023 16:36:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ajvjv22Y4xzQ9W58t2w1GcikEm5fAED5Ind82jZsl+Dek+vkAQNOMOPObcq4+NQ5+fWTHgAwYAKxo3v/MENcGH8dz3MGMRrELKGdMi7mkQHs27EGI6M+SInOu1L4OV1Wr7rbQzgI4thhVK1Y5h/rBEN2wkAUe+GUxmoYtE15vk76fT4PYR4TSlAAMcbgTV3kqokoRIAvMSCTfwg1n/F8cN0E3gquTCa/ykuuRMpRWIxLLvIP4+2jvbrZjBZ/atShMrj1VyJ/iecFbmNKIwGrMHOQzrZ6NZyaZ1CgTtbyP70dnWnV4FIZQ38Ef6kDoHklhVF53j0kJVUfzdoE+7Cupw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=g2uSXyjzMKj8RobrWyTGtXixO/W/4Bi+rTaNKYRG4bU=; b=ShXZs8YN60ZW7ThFhq9A9e246zraCTZAKvb4Q2Ua9AcY+1Q+ffNB2+rMl2pWnX7n4+U/SFKzizcS+/oie3lMtpKwC6YPef9TmCBR26gW84rUrywYKi8tN++Ayzu4RzLSHq5A3CFlHa8SeXwcZhoRhKf/KQWDL88AbcGJxWUPFDMf3ay+5Q8e2qOHj6mUM7hqcOZ4fbWtY8euFOPLqUNscYXb2LOULRZENOR7JWMCR0pfsLB9Td1zAsGbzX0NlsbkpLxmeeGTicnv+79O5t3TFDr1oS8CbfL28O9IjGarGtrBZNDiWOWxBcybDumPtA6Ne9B8tOojHCI/rXn0pNhCpA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=g2uSXyjzMKj8RobrWyTGtXixO/W/4Bi+rTaNKYRG4bU=; b=djzi37TKbgzZjt5F+Seli0N2zWBd3oHKIpNd96Uqqx08+FwgCVALHZKUa66hLvRVq0m5S3TcysLFngyboetaurzSdstprJgyELsqrdMearnDlpWzcNoyRD7vjZEpaAcN4rCgsTsUz1oQI7rS/zhiHDXe2WrpZeJwFmLMw+5R7X9Z0ej9LRUIS49VMVHIc1uYvS++ZA1xNyME2Ep37yzfKffTZUsceaWYfMmxXqKc1uBSc7X8qkiQRxAgxt7FP3z7rRZXb/Rt++YDnn1U+9YgY5loxmZ5iFN+w/mTWOQ3p56b5Fb2ImE70cFxTBeqLMVE01i0Xo78WlJb7TNqSLqatA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36:00 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36:00 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v2 08/17] iommufd: Move putting a hwpt to a helper function Date: Tue, 7 Mar 2023 20:35:47 -0400 Message-Id: <8-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT1P288CA0031.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01::44) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: 93957e39-7d8e-4ef1-65bd-08db1f6d1666 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cQ4nuoOHqcRA2b5lE/rFWxxMQCH8Tj6RY+Lejv3Kba8JPbV8O4ppV8UV01+Q/p34NDT4AWcK1X14cNT0dtQU5RBl/Ip3ZYcGlMuXniDjGk0og7u9qyw7uoI4IggmvFDwXbrdjBdF32WbtV2dQtEKkXK/eRt/HqlkkE+dJfHRNsbGIBpu2Y+uX8Uuumz0P9rG10GP5EL6H1tNdGpGhIRQEmSr84v/YwFcGYPr534JV3Alv2x/wg8XMLXcUCHY3s8HMWHqZ8bKyG5dTMzlUGGbyh0JQYzfAGsFENCV4LJG5ZehBQENtpN9nmbA81fTdYEPWFTnf61gEQwsIzUyuTG5IH9I/xuQs5J7BVz9EF/2atN+dRPFrZFHQ86adcSK0pjRaKpCx7UqFMXZbcrnsHoQhz5ckQuOmnhklETaMunF9tAt+eG6U6COKdebRA6qFFRPWB1smcZNmIZYhAgxHg5f/p7YYcQJEf/UjS4M+JlUAzEGsukvVZ896UXF3OypZJnUwY9PHzvRLU5xiY/sdhmO1MVvnfnfVW/FEYQcxkxBrSe18myyTRYbLFLPv+9r6B6KdVtVYZIYITp7OhjtCVsM7E83ZHnzdUa1n0/CzPxkQetD3zIcSkZxo1ehk2CMVXM9QEFYomTpRXo0NZdLO3Krb2V3uP9ATIxazr2KprORyLQ608MSZvy89kCBZD1wu7Sk X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230025)(4636009)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: kx/AUsCcp/VLziSxZHBV4wYKldp4z92DhMmS5BJH4Se2rIiXFA7epjx4bxJePMV6tA7YdWE4xwnkUf2MdjmNs2abKqCQrc86wEo+mO80OBqB4b8W8X7qSkms1DH0PfpUjuY/iC0sh2eZkaTooW7IoENx2+AzbP+z1f5FyKnJDzh5OQpys1qt+JeItmihjKAs/C4p6g8gJonvOp3rPM9xxHvaNEllasLRZf4jYDmpTV9NE9YYS1oX11oVP4X6VrGX/4OflHbamf/XlMnPjlChAPWmPXHr+RvOIiilLEHn9VdvDUVZpRU6ykNPpEr4EOBa0wSxFvIZ5uSYZdsq+wI3YpM9CE9Vu9uDtUozN0uTUw9qqC39SHsRvl9U0rmGapUFN5J5P7Wux220XbyhYFviAhNL6ThNNL4WpIceoign9to5lZEG8lqHcmuy+Ph4baB0D1nixLMorcFBJkGKUiIB+YJSJN55JyLGjPIJkgwfHeOuY3cNbm0a4NSJsLjO8GXS931tYQtw17Y+CoTJOIyJ84Z5tUMGaazBA/BVoJ9zljWeZqcpTDEbTlWDQbA1VXywTyOPhXDK+y+iyjnxTeBuNux1lJ7iANaPpVQjYKKA+jz2UbwecJc7YW8GPzbKPMhWx0HQXjclvv4y3BawEGmjw2WpqqKNkWYOQoASJf5IB/fnxDWwujEZO57PI3ua/TYjpwQcYLgMGTaLNZMfn9hqbHDs4TMK/IVe23NWIsprm3UNoLXqZRJ2wOjKADaHCBfL2EUJuxB5D79+3WDGjDnRKqN7k+DMmXqJerWXU0fxLnjGXB4mQWTlxIhhujOdVzm3QN5bN/l/GD4KOkpSqj7qoXFR+PaNOKyDX7AvoeV+PWa21EAixDghZQMDt0aVlMrjonjXeC10OvUZfiLyZZF8aJkA8j4UI9PeEyOy/d2oj0tSykiwq14uyUewjw6A8r158BfZACw0hCLkHxqVbaH3xA0PGIQP0EI3JY6HBwWk9+U3S2lIGbcDiPk5kQ+TXGwjwa04o50yxKzMI2tpvUvhBA177nFC6O9p9659SKfSoYQav06PLbHZxDUY714opS3cpgwPaKqeupFruunOLF46jIFTsDv5XH9xewSikOson4XvPOGhKLgkz6EPD2JAv40QOk3ozgwX5yx0C4ZmRIr8Aqb4akEAp5hXya0e/HV6Y2aY42cU0V9KZWHpo/QxVZRE/T7YEpNqWQYRmc1tRFGF6YQGjvIyf0SA8UtAzasfEE8APDFzf+YLV7QT0hZrqw+BUyxMDwxcscnsQ6Z9GJMqXm7aeOzvAOqfEOJXEg5ODLGygT/vAtAo7l+gFsqJEHxZbqwy4W0IT924E1t5eSFLMoQdyJ69UuwRjoswPptsLeZLVw+rYpPGoZNaedZszX0n7JrLnZkV7VLeZrlCXSz/xy7NpYY4di8jgwUUwXQ4Jd3LMJNUlsHzIkJ6a+FDODK6AfLKibE7gtTBHvP95ZjDkYK0ivxvaSbJ9tYWQZcz2Xis9LSEjXFLVvjhwg8g02tvxRJEMkxo3vvu8HDXGO+7u2++47aqhPe43orW08yJiOjA3NGPyRuqeHJrGcGziueE X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 93957e39-7d8e-4ef1-65bd-08db1f6d1666 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:59.2168 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: lLXAaRJpS7XXWXwCZ84+KP7s4I8Y84QbqTUab76LV8yvQN4k84YBE5EIGnHWZizx X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Next patch will need to call this from two places. Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 6 +----- drivers/iommu/iommufd/iommufd_private.h | 10 ++++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index ba84d314127ba6..d6d5c2fcc33889 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -488,11 +488,7 @@ void iommufd_device_detach(struct iommufd_device *idev) hwpt = iommufd_hw_pagetable_detach(idev); mutex_unlock(&idev->igroup->lock); - if (hwpt->auto_domain) - iommufd_object_destroy_user(idev->ictx, &hwpt->obj); - else - refcount_dec(&hwpt->obj.users); - + iommufd_hw_pagetable_put(idev->ictx, hwpt); refcount_dec(&idev->obj.users); } EXPORT_SYMBOL_NS_GPL(iommufd_device_detach, IOMMUFD); diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 7f4936cf537be4..8bf053f4d4a9ce 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -260,6 +260,16 @@ struct iommufd_hw_pagetable * iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, + struct iommufd_hw_pagetable *hwpt) +{ + lockdep_assert_not_held(&hwpt->ioas->mutex); + if (hwpt->auto_domain) + iommufd_object_destroy_user(ictx, &hwpt->obj); + else + refcount_dec(&hwpt->obj.users); +} + struct iommufd_group { struct kref ref; struct mutex lock; From patchwork Wed Mar 8 00:35:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 660804 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 A3BE4C6FA99 for ; Wed, 8 Mar 2023 00:36:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229706AbjCHAgJ (ORCPT ); Tue, 7 Mar 2023 19:36:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229818AbjCHAgF (ORCPT ); Tue, 7 Mar 2023 19:36:05 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2071.outbound.protection.outlook.com [40.107.93.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A6E69CBEB; Tue, 7 Mar 2023 16:36:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LzYsfjgovuireCVq6nc8V4B1zF7qCOwD6erUFiG4xd9QTjpkDi7IesMwM4/I6NhouC70uHhBsrW3r7IEosFkoFSBalm4z+C9zrrr3oUbcbXRmy5YD7xCk6FsY1FfracxN1nU+4jvIt0ZJFZT5vP4yGKPX2/4aOiyGCHG9J0keL1vxUCf1Rsg+vVrAht52n4z6HARhFpYdhyoUOV0QILIteJ64HK9D4YvCN8MMvr9aI+SETHIAeywOGWSQX+o4hzE6gMs9mI0dZi1tlGHGYrU/pz7AGQ3Tq9Ox7UZmyDqm/vXVerNy2qAKtE73wD0gYd01xrfUTCpXeyTvVmB7hNSpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Je6yzDNdMf08l4gjaNgXgbzq7TVME8YmFB6ue1iVNRE=; b=MIQRSwaBgKsDqnh32J0BXeJEYESt6RjiaVRQ8FUkR1IL+LZng7IBjkabSQW6CxAooIQUrC8+z9ccqJDl/334V3IZSTRafDbwglstmEud7OzCV0jEZAFdkaxM5vLjjkHOuGAMi7dEPR2+eD0JsXpoDRc8M50i50ZvAgyVy62L5FzxBYB3q5twn6Wuag1kJuu9dLDoKpx+GbqV6CUPyl53JeLErxokV+VcskUjNt4xOgKqm66v846f1KxHyc4+OiW4/A6FnznjY1KC+NCJsyIehoD1RXsTt/mmE+fnsKWnm80bDsWhnBVV/3MpY4fUc9mf462pVARCO/oRXUiPyHpyLA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Je6yzDNdMf08l4gjaNgXgbzq7TVME8YmFB6ue1iVNRE=; b=rxuHRtFJExZfBUZKjGpDoWna5G0qnwald6JSfAic3yFPgVTtaXm/B1I1n6y9WQh7dYBxuWX3r2+17VOxt8sOuRkxIIHis/FA46djxyckXYPYP/CzC3QQe85HwmlG33V8Dx6r0q2BkhI4cfpH9DlRpA/BHCUm0qYsUQV5NOQBxxSRQgLHglb2D5a3EWN4IIwoSS98Txzz/Mn0UYgsL3e6CqHGZlnNAH8PZiOKL4qhDjHKHTQxIKK9rTi1DxGg3RGFvDMtH/vfRYdQfZpZnjuHa/OAagy/0hnGrJfMmEab4+t1mu8QTIZl8mJjigqk+17hgTDcKO3Att8T2RIa4/a48g== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:35:58 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:35:58 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v2 11/17] iommu: Introduce a new iommu_group_replace_domain() API Date: Tue, 7 Mar 2023 20:35:50 -0400 Message-Id: <11-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0295.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:10e::6) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: d6d12519-2c64-4d1b-df8a-08db1f6d158f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +e2zEnnXYo5seRpvYwzGVuiJryeYcRCbf+SH0vQKPWJFXiE7cPUVLWDy/ZvwxmjzlZQaMKR9kIUs5QzLPwTw2NML4WmLKF3rvZMam3HPcOiFTgQtG+hUWzU9jC7IxUZWq9EGRbw/cXPMVkCVfvUMy1gL9i0n/jPhGkMVXnw1cmKNd+KBSZ4HXlzliF3Y6MJgyI4PgYDI1Uil2tA+DZ/AU/nyMBBxXqoL5cy+03CCH49LW5Q3S6iv8vjrPYS/horLezDWM/B2uHoCh2Bt0jimJWdVeNsEEoq3URBtNBN/Xx8W8V8VytJBzHGnuboCyDDYAYgc+tpHCup+LRUvOCVQ7Q/RR2yzCnsYogJoi16VSPI8SjzSWhNxpykQstMXD5CjsaqBAjzV8LNE+apkhp6oU1ewDGYvQgqxTs/9428ksRJ0OmRD941f/AGsttJSmh7IrG4Vc5X+nPfX8Qvh0qy2W7dNrYC9c/u5Pc95+yy4pD2be3xp1xCJU3XgrKJ9p9Oh5deHiKa6ALzGPSDqJqxUDEspPFk6yfWie9CIn9nK0rBxazYwKl2bIKebbgIqO4QAvoUKR2bDPlRPVtjIc06SgCOf1T1I2KrMXmaslVjXc4k4WMI5g7L+80L/DGxnQEXe6+248O6RF3VBg53vF9SvFQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230025)(4636009)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: gvTchg4OuiHLxryGqQSr8yv0i/WLm81pqNUuwoqueUgdAiW02Vz1V2ba2AUo+OV+b1eCrLzWF7Uv+AIOVMjXopi2zNXba0t7+AKnqV8sFnfn334jmZ7PVaiZ5/BRCNcf/xNbqi1rt//EFtTz4fZB/cgOaHRadHHaZ7iSfpT7TgDTN5f2ILqNaCVNbMoiKx5CehoQ5On/+XxLV7sI9QS8aj397ppj9ffYkEFS6fPmy07VmEdo/OP9bMGZTV5OUco1IA3TS/VmMPUh6rbQ1WL+ZeAVwlE7OKULijR0rkgxZYZLPgBzGqbwAMUN/8nPc0OfCuACbPR7SDE8hrW4+i/tdoJIiI2jTRa9/Dbg1ktrv9SRnXPIGWZ4KaKh7m5LT71tY0cCRxGNMkaLvCsQK/NepvHNYG+eB2ai663WiYDoDBUgxRdeQqeMbM3KDlXTILp7MBLaFTG3i5xxj3nXVpwuPNQCneUpLPJFrnWEEz+3QPe22nMRHITuIDH7kOkCNgTODNGX/4k/ieLJdSRAdYFX9HmB5utVT5CUvl3XCpz/1msXef8Kouyikjx7qy4iuxVP8KBRAeydMvcaMFYCUpMikr8eqR4f2C4otHntykDLOcIzIQ/wT8dY3Yq21zFa/JWZNYQeK8qJF8+Bxl7tH1P+tDFEAdP6+R6kMWB6yDZN96Br/trkl0gFfKspG1+ON5tzFmCU9ymXRBHh0k5BEzVVlHeGsj7CMml+ed7+e8lqu/EpxIZiGLizPoUA1eyC9slIqr2f3jX67bUPYFtrzjyfTpOU6nbLC5uXlxviiUtoKjczbfu4cDNMVoM5B8eysAOpNpUhg70iVI8d4OlGCxqUqOqNaDdf77PaAgH/98IuUNe3BcsSo8H2o1EeWTJUUTavyTptUWis+BQHGYkx2ktytaY1dqv1baQnWkGzqC3256IUz94IX+naWGmhM818aLsqg+Mt/7pI/bf5/Gi2aEIKp/oOnzcSbVK2OY+D95K5MoqnaC4E3qKX6+xCL4PVRrdghZH3h3MBdOoMl7bozEANiA8W9VH3nchyRV078J9Qc0aWGeJ41dI7iVfsMap16NFKG4/KmoYCWU0yr4Md6UC8rLN+m7C9NoMWvsu+dnjrfqBR5QKDaI/lFDE/EzL9ZmHnzGDsCWtueL/Asx7tv9eifiYg5a4tKUsUKki6xniSjFkzizotDegasAEoJ9tHbGsXyPahITG7tPhCZIW6DoIVBgC89JU3UJqVkJM0yTASkTrF48rh5LhCd1Ltz+6NNyITDeNaZe/FviIxn/tF21uiY2mBJ5EQhEVRHaVNJjhnoIqSyuV/vDst1SpFUf72VBNYa+Zqz/AZx0WndCYwE0LqbILzFKRY1AkrKY7FEqr10O9K+I2/qL5/g2DQsxYTQcGpj8dklym5u28CNef5Oh1uU2/3tz5Vtz3Xn4t4W1vwdqOJQVvqRSzcM53uFX3Nd3kiglZeniK3E0OMHfxBraT+CUHud2G1JRUtQiToNNrQU5Z+U5shPMriTWcW6muo9h0+xstUqKaEyC1QgWWL1lAqyvY42TWj7fRRunzCknnfWsmN2xzJsclSXQ8EzWWcPeRk X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d6d12519-2c64-4d1b-df8a-08db1f6d158f X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:57.8265 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: bUx8LTfCsPGXSvbGbCy0K1E5Dcz3n4CMF2tvB0bIFJgEQXhQt75LfayJXlZ0IopW X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: 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 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 e57462bb06d9f0..0eb54efe01ce92 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -33,6 +33,7 @@ #include #include "dma-iommu.h" +#include "iommu-priv.h" #include "iommu-sva.h" @@ -2191,6 +2192,35 @@ int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) } EXPORT_SYMBOL_GPL(iommu_attach_group); +/** + * iommu_group_replace_domain - replace the domain that a group is attached to + * @new_domain: new IOMMU domain to replace with + * @group: IOMMU group that will be attached to the new domain + * + * This API allows the group to switch domains without being forced to go to + * the blocking domain in-between. + * + * If the currently attached domain is a core domain (e.g. a default_domain), + * it will act just like the iommu_attach_group(). + */ +int iommu_group_replace_domain(struct iommu_group *group, + struct iommu_domain *new_domain) +{ + int ret; + + if (!new_domain) + return -EINVAL; + + mutex_lock(&group->mutex); + ret = __iommu_group_set_domain(group, new_domain); + if (ret) + __iommu_group_for_each_dev(group, group->domain, + iommu_group_do_attach_device); + mutex_unlock(&group->mutex); + return ret; +} +EXPORT_SYMBOL_NS_GPL(iommu_group_replace_domain, IOMMUFD_INTERNAL); + static int iommu_group_do_set_platform_dma(struct device *dev, void *data) { const struct iommu_ops *ops = dev_iommu_ops(dev); From patchwork Wed Mar 8 00:35:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 660800 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 AA7A2C678D5 for ; Wed, 8 Mar 2023 00:36:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229952AbjCHAgW (ORCPT ); Tue, 7 Mar 2023 19:36:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229933AbjCHAgM (ORCPT ); Tue, 7 Mar 2023 19:36:12 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2071.outbound.protection.outlook.com [40.107.93.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33985A0F0A; Tue, 7 Mar 2023 16:36:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=X+k4BkaQ/tFjBJnpxW8/lfkjA9yOPv519S2p2AkFQ0lyy3Pb0IjJki987J/9hbOu4Tk+btDtDMgdmkeImFm5wMfoT1cc+8I2geEarm72EdDeIOy4GfuwOrSL+2ZCSpcRvDeipa2XaPu0P0NfK3dUUPhlQP1g6PNJo1g+LUjBIHI6mGjnKmdN+LvCzqHnEuCgHzyxUDsJ+d75IHd1zb0/8SGKj3wcmtFxTFZNitiVNpy1pMZrtfMMyDZ7t44OcpasTlZnVz6hfZZbK1Rhnkd6Ar/QzwxmLa49xTk3Nt+u98i68Kw/rcJNxWYWwqyrCk8SHvopZAoieatabv+iyrLEiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=VdVNZFAFb4WmTzu/R0rG9qtc19on6HWKswpFF7vtbo8=; b=fSvEwPNI6ZMnzfm56yj5ZNBm7hp/n5T6Z03WfS+J8a4j/55qdI+aaY/jM56wqbZBfKLzF4FF8dPI/IQYZnpPXPrz9qNfx08UeU9ebfE8wiajs/1EmEuuFq4FNLK2oL/jfHbbwH9IPw9+dhM0ChBh68FXWGVYxKcHd05bZqvNrlbgJo2MRE56+qjEPea8C8lUlnUfLevYP9gvSmBuPX+mQX/52FGk+7pt+zkddB1w3yXx1tYaW1hXb1vBeCBAEPVGUER0bYOCPuaH4vnaUFbrvoTchDvJ4NikZbWyWzcmeWhEkrD0MwO084fHMFm8VTb6NUjhW5wh5q4rcAIHU69t8Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=VdVNZFAFb4WmTzu/R0rG9qtc19on6HWKswpFF7vtbo8=; b=Sy5uUC5ahXiwqacI52/C+Z1LUafgoWGa3EQsk5xYNmPTFUpPQLm8zjYWTVwYiHDarR0e/ePj3AQYzwqY0b4WIoXy02aQDOLo3v6QjZF/GXAhRu8r0VD8NmLnAT3t66Bq76jJej9TUFGLOSzRlRw63SbP7eYE/0hxX9I1BtZ3X4bW3h0XpMDDDSIxRq0yQd9o9Lu6j7R3sZRUFnKUY5OlVR92ezqOd5DaU2t3nqhhaOjvdbwd5gBk5mLx/qz3F2ub5n8wOsbW8wDqOfQ+G+CClr/sCXCNaosS6EE0LnV5NFsTGHE9zaqOYh1KEjoFra00Q/3dY2rDjhUGscnKV6U2PA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36:01 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36:01 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v2 12/17] iommufd: Add iommufd_device_replace() Date: Tue, 7 Mar 2023 20:35:51 -0400 Message-Id: <12-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4P288CA0082.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01:d0::12) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: 468e701b-b461-455b-1cb1-08db1f6d168f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6N0AmhazAySZsEo8CKuXZgfceG8q/KhD1X3ywb4kFMTtdntOaiY+K0qe09H4zJ2KNbfaz26WkgxXVpszVH+IEEWP3cjcjsvUXVw6JJCTQmjj/RuSvUcuaTNCzqLyR2FcAqZnCDXcnsN+14PVuHM2O4LaG+dPUknevsib4r5FvcteYqVLwQshM0+ZuQiRdUZFJdMCiJKxGLX8cSxRw8LnxEtVkZUVxEAbyt5ioLFL/HGPyM0LelvMe26Vn7XZk8gX3BiFxmNeI6ccVjUOyCfBSGNAcs4U8zSU2dpJ24gTebaTe6KLldVftegNvfcR2QsZn9nb3m6WOIrO/FwdQZ02kn6w0TrLsQAXAvLTkywCHeEkAynOhIVHmELZN8wALZBURsf9b4gnKlSJ4Rnu2Ae35tPnLiMnXzp1I2FRkGyXPCzncGCvaunysZi5dvmQLFyTKaQQwvsxVdSkUcKMARxt+GLkihJSq6vBUxhL1aSaJaBBpdeegWuQudjELqAU4tzp8VWFiG4KKmkia4Ya1v0rwXz2Eq9QBWshOsDlPtomohf8VgO+dGvqgbsIPldKmwk/Mq26TZ40s5WQ9Vw8kJNLERJMypei18JWwrbZw814zG+fHNPSYH9f1pVH7OsVzcQENrcnc67Zd8HD22tYBRSkcQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230025)(4636009)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: SrzMdrZFiZa0Kvq5mLECxRKuCzVsrdxBksMXsl8bmTT/GGl8b1YLYWzg9ytdM1Iis2qE4NM8pHRZz7rfDYSZtnpWLHW2qB800busKYFDPK/YlWdQo6Ghrji4jD2T62x0X07n1+6+9EKkzMVsQYnnQ73r8gbzE/PSi8yzjcz+q80A3KVVTgXG2ZD+AzY5TjQjxtYH9vIthUQbIgijPmZxW/xYrVTp4Q8CUCp79qvzsvdPqyOgWyuRYG+P9LBPejKynVoQYrhJWB6bTUVQ+nbeW1cYi/JVWAkf6EknRByOUHCB8UOGyu7VNZ+Ibe/XWAyxErHa+wuPbeOCZ7SxaEf0wBExYi1GqmjCVak22CdfHR5xMyr+YamQXWIDST9pokITv0RGey0Gu6o9b+DbuBBI5pDDF02F+LB/l/X3qbn88x+tS/GcED5/fOMikMXJGKOsM8qAKPyLGlJxf5D4VjBQdPOrcfp4eEgWnyW218sdf5gOimMeoYlqBCGWnjlf6G904poewRJbc2RaquWWb55z/p50+TH1KBTCK1bcHlGETGINgUELYVoJCIKK/EyzUPMgAWaB6+8ruTxZLg/PP7bLm2bkwpMP7ZazEBKZrJGyUfr+imRIcRZm/Ed8gX50VIRaX1NWPjlffmhi0j6uPn9YKUIUnqUIzRaL69cB8129qY6EMjPMaDq0u+qnTNufB4yJgKQzZ4xZ+EpLIwBpvkZ0FS3+R3JRWL5TWiBKc2uwqWj05FHK+JCLTlppfy4TLYqsrCwsVgwceb+E7bsewF12AwlGREU7CjiEWlPAV2GKQOLsG/dH1eIAgJlNeBQRL8GFWN0Yq9PxHgLcPOBdvx65TCoHmoSAroNzl2XwcK9K7a0I2Jt5ZUymzFrGts25H9wuYZZxL/jxL3qY/L7bXNYGOxfImHA0b/rZ6vXPfWd9kPMrfqVj5cJd0SPS4yhoijRt3Ql3cuT3rvS6hYjyi0QbE5OxZRYL4bGA3Od96yieGB8fSJDaqq+T64SsBDWc8K1m58LFxBGPF6to4CcHZTHKC2zGywsmm2FZ2FlyKEXyJ/S7COLd+m5ds2vxhZAB1/cCM2EhJwX5Jf+d+v5x0ujMTgGEgoSCCuDyl2m9LDUiQu2jT2Tm8vdrALG37Z1iU6HNfpEzDCECBpcocEDNK4T0o4GSff3zDJfEW3tdkr7M4m0r2qvpG1gvDBd57dTbnJ22XE2uOygEYiK7H6llVzkcTntBdAUU3f04e/Zq4PCBZnFZBt+A/tB3l4ebt0bkp3Oo6WsotlIMDp3JQCs6LfoPAfyxWoxd8jxZEvKikZNkPS0AcICjSyEYHyK6/fEbmbKewRsuzwfNu6KBF8so4RMImwqrPE7AZKpnQqWkpD5+3R0nPfrZynZWIuZqlrt0Ozmv8KWxjGsiLWNooqv3zuq+GHnYa0u1Ix+vzXmLTd6cPBTXqyV/451mfuBJWBh2j/qlc86YXUP+FS6ZkVn0p09wKQTEV3UIM8EL2WnNQRsB961BdW5q+XmkvYVzVxf6XT1Cs+CvSNe78I3Yzf6nD24Qemo1Rhetn1wiNlPlvWs501bjXV+h66SsKXTNRUmj6fi4 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 468e701b-b461-455b-1cb1-08db1f6d168f X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:59.4837 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: yqO9+ZWLPRyX1FqLsJLHx5K9JkAmjE6aeyGC8pxDpPgRTLSEAgmI7gXLj31h9eD1 X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Replace allows all the devices in a group to move in one step to a new HWPT. Further, the HWPT move is done without going through a blocking domain so that the IOMMU driver can implement some level of non-distruption to ongoing DMA if that has meaning for it (eg for future special driver domains) Replace uses a lot of the same logic as normal attach, except the actual domain change over has different restrictions, and we are careful to sequence things so that failure is going to leave everything the way it was, and not get trapped in a blocking domain or something if there is ENOMEM. Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 91 ++++++++++++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 1 + 2 files changed, 92 insertions(+) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 0256e65476f045..68d1bee1cdb68c 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -4,6 +4,7 @@ #include #include #include +#include "../iommu-priv.h" #include "io_pagetable.h" #include "iommufd_private.h" @@ -359,6 +360,76 @@ iommufd_device_do_attach(struct iommufd_device *idev, return NULL; } +static struct iommufd_hw_pagetable * +iommufd_device_do_replace(struct iommufd_device *idev, + struct iommufd_hw_pagetable *hwpt) +{ + struct iommufd_group *igroup = idev->igroup; + struct iommufd_hw_pagetable *old_hwpt; + unsigned int num_devices = 0; + struct iommufd_device *cur; + int rc; + + mutex_lock(&idev->igroup->lock); + + if (hwpt == igroup->hwpt) { + mutex_unlock(&idev->igroup->lock); + return NULL; + } + + /* Try to upgrade the domain we have */ + list_for_each_entry(cur, &igroup->device_list, group_item) { + num_devices++; + if (cur->enforce_cache_coherency) { + rc = iommufd_hw_pagetable_enforce_cc(hwpt); + if (rc) + goto err_unlock; + } + } + + old_hwpt = igroup->hwpt; + if (hwpt->ioas != old_hwpt->ioas) { + list_for_each_entry(cur, &igroup->device_list, group_item) { + rc = iopt_table_enforce_dev_resv_regions( + &hwpt->ioas->iopt, cur->dev, NULL); + if (rc) + goto err_unresv; + } + } + + rc = iommufd_group_setup_msi(idev->igroup, hwpt); + if (rc) + goto err_unresv; + + rc = iommu_group_replace_domain(igroup->group, hwpt->domain); + if (rc) + goto err_unresv; + + if (hwpt->ioas != old_hwpt->ioas) { + list_for_each_entry(cur, &igroup->device_list, group_item) + iopt_remove_reserved_iova(&old_hwpt->ioas->iopt, + cur->dev); + } + + igroup->hwpt = hwpt; + + /* Move the refcounts held by the device_list to the new hwpt */ + refcount_add(num_devices, &hwpt->obj.users); + if (num_devices > 1) + WARN_ON(refcount_sub_and_test(num_devices - 1, + &old_hwpt->obj.users)); + mutex_unlock(&idev->igroup->lock); + + /* Caller must destroy old_hwpt */ + return old_hwpt; +err_unresv: + list_for_each_entry(cur, &igroup->device_list, group_item) + iopt_remove_reserved_iova(&hwpt->ioas->iopt, cur->dev); +err_unlock: + mutex_unlock(&idev->igroup->lock); + return ERR_PTR(rc); +} + typedef struct iommufd_hw_pagetable *(*attach_fn)( struct iommufd_device *idev, struct iommufd_hw_pagetable *hwpt); @@ -509,6 +580,26 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) } EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, IOMMUFD); +/** + * iommufd_device_replace - Change the device's iommu_domain + * @idev: device to change + * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HW_PAGETABLE + * Output the IOMMUFD_OBJ_HW_PAGETABLE ID + * + * This is the same as + * iommufd_device_detach(); + * iommufd_device_attach(); + * If it fails then no change is made to the attachment. The iommu driver may + * implement this so there is no disruption in translation. This can only be + * called if iommufd_device_attach() has already succeeded. + */ +int iommufd_device_replace(struct iommufd_device *idev, u32 *pt_id) +{ + return iommufd_device_change_pt(idev, pt_id, + &iommufd_device_do_replace); +} +EXPORT_SYMBOL_NS_GPL(iommufd_device_replace, IOMMUFD); + /** * iommufd_device_detach - Disconnect a device to an iommu_domain * @idev: device to detach diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index e5ed5dfa91a0b5..8597f2fb88da3a 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -461,5 +461,6 @@ module_exit(iommufd_exit); MODULE_ALIAS_MISCDEV(VFIO_MINOR); MODULE_ALIAS("devname:vfio/vfio"); #endif +MODULE_IMPORT_NS(IOMMUFD_INTERNAL); MODULE_DESCRIPTION("I/O Address Space Management for passthrough devices"); MODULE_LICENSE("GPL"); From patchwork Wed Mar 8 00:35:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 660799 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 33277C74A44 for ; Wed, 8 Mar 2023 00:36:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229932AbjCHAgY (ORCPT ); Tue, 7 Mar 2023 19:36:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229975AbjCHAgS (ORCPT ); Tue, 7 Mar 2023 19:36:18 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on20608.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::608]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C03CB58495; Tue, 7 Mar 2023 16:36:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dHAmNSzOT5mg8K083Ub3pC8eO+xYYTVC0cZyvA65cgcT2Z++N4rwUpjfRUZyEl0+/mVKcD0wayF1tLkMCuCeb2s9sfnCZ4Jab9gqqUTuQmfj/qG6yCiqrkehM4bc2LekcMRfOHqOF5evAi05qaVtKMsRpSIp978zbUg2rtOaRMYCf57/LiAPnlZS1hJ4Tp8vuk3hRA8+Vd8OvXqmPKG5VxeCWq5Ax1GDqnkAfzGPoW4x9B2yNvZwDS2KUdROjwmbrxy8kg1hxqlajR12jT6+6RoUVsA24fCdRFno3HmlDWJAWMQUfLQWyWQvSzU4z4r+kK4QVl0Ak7a6X1fkmhkAsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=qQ7eAL0iM4ezK6Fs66vWPlxFOinFHhdwE4p0UCl83vM=; b=bJQYVDvCPoXRr/9eiODZ5qJn5v6H/47c0kAQYw22jIZh9PEkFm+wpk+9PCkPHTne+Hrp78xNAobGYvqr+xi5Pk4SjkweKkyKz0z0fONGNOaBZJzs8Qk8bI+pPD9nlFNhCXgt/gBVkBByuSdGPjz61bU9JB1sE8yvvhsdcj4uc4CT+6B6UQT4djg1xRQCDpi61HmF5Y6o+3C+TUBSmnGHnhONFp7VGiPgnZVlNpmdLKyPDhA0CMPPl2gbVSzBgsHMm97oXQL1iyatEeikztaMQz+Dmcr45SQC/Fni+RKPeRfTEdqlxL23Tz7NOZSqNz/FG1sigFrSP8wnIrDVUL6zrg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=qQ7eAL0iM4ezK6Fs66vWPlxFOinFHhdwE4p0UCl83vM=; b=IpLdri9/YCy/uyiwd4UYyP5ZxqGvG+Q79tVM9/UUrgs6UL08MHPuJqCZbKent5cVOa4AL+VZ5dZnr6rxWvB9Wj53TC7TRL4AhmliX7X0fRo5Rj0RuX4gcXjs4VDt5A6ZDqC2nlMMGfSLkXrqfytf7gmyphCF0nRgXgm3Rs1/AHNj8t6W05s4jjwatk76gPUu+L0efqmXnDvZ2M1GGkVZIIxqcuyg4b+xnCz74hi5TL1EWD7srEjT6O5zeN0nFneaZESL8+17zdJdbwCYfjDNFcyyXEF6SfwUHbcTYs/K9xZQetcjPxWpxDn5j0OUw32/FXkkrCTe/WBbR0GXEzJVfQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36:01 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36:01 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v2 14/17] iommufd/selftest: Test iommufd_device_replace() Date: Tue, 7 Mar 2023 20:35:53 -0400 Message-Id: <14-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT1P288CA0025.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01::38) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: 9d134498-8934-4bb8-07c9-08db1f6d169b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: M8OCmUnRahMnopMNqJoLa5WhrPtvLQCGrapHv5NOGV5XS9iauJ2vvt6zVGTenQxF28YKry47SjqIklCj995y57p1Qjyo2GeI+1NWJM2xDpSyZp4a12bFzACdp0jPDaLR5qR/8hWacEjOJdGg7md1wzd+uRkFy4AxDbBVQUaaomp8m+jPLLbSo+6BUKzLBluOfHZrapz4dHHuprcwe6nCuywS8qVUczOb1sa1ozFQX9V94W0icGxnvnFjms910efRj3B6RfOijSMPJpCbLmbTSNlUFJ1cLFJ2Z5Opx4mMyhJpEDMvLrx95EYG4dMybUOZ7jNMCWGeU5RrVHBq3u1bRrkslO4khrsjiJLVOZidBtgFLs1WZElCVmX8KVu9qtqjKUbe2ujWEoyYVuzC5OevKntOE/A+xX1d8LPudCmHNNMxZvEiLrPV+nXu9ZYzeLhDmE5KBunZXsMe/UcwIchsqMOJKdyv0BiCDi2tGnFjPpJEe43Hw0H8DweMriAWaqqHsuPJmUJJGEbl7xDYK5lvHW5ktuXmvghNPfik97Z86EvCC7VTj9Y/OxhOBwbMcmB9ktyEO2YarmH6cAf2znYF80IKTSWmw5Xqd8GUhJKZMDfBojCOVuk98bHLyAZI+DoRSQc2brPbq7/PFFVjrjVS0g== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230025)(4636009)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(66899018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: hCi/0hbdNCHEr8j84kECsuHQmMVNC3+ZCaOQQSic4pU85qH1YM1Xb/bEroxWFz9g9Finu1oXJPpbvqX99lZLigzFd/RS9ebRyQc5/MAcXS7Wq7ER1ZK21i8MY7N/Ukqas3+52vSDSA3g26FhMfiqPSRAp5Qyd8eGJmK0w6Tu/2MqSf28HPAo2drGpYXwueI+ViIKaxIYZSi+bTL534CJLMOnx1m4dm8MUPOjqCHXoegHDC9Vrnph0eksEeD0H04BA3xCnS8cXv/uesZG+XVrfMzyVU3pCHtLwePHVO4pJGfQIhN1e+b8waD5lwOPZ9HX6CDJ8fdcc784XQnEfkdw4pn4sYOwA4yzfG2JGZsx+deUdXwqUD057Vpm306t8skahG0Fi9QhKkQlF8Uqdsel6W/v49Lfg5QGvPP8aS5j7aBFkViEKHHQMunx6fmeYqU09W2r16lTRb/A+R8NhhLUmxLW+xKf1XlXcGplMrN8SNxDAEVLkWI7U0ekpctPOtutL1bCroau8ikG5eW7QR8SqEXTjqccUBxlWf6Pfc1E98fhA/n6yDqr5hRykJJLJgaDfcTiOD6/wZvG8BnotqLXYEejF8YtKXF6JNlqa0f6/+S/v6bHStR6XRJtxTDh89pfxcy4Ffy6OQo1XJblLanvk2TfEL+vOFTIdF4YTMUoCdxjbvMBp3ysnAU1TyPFblppm7Zla+/xHvfgocsvQBIT6yx7eoe8V7qZeUPQQf/OkYya/+iJTTpnqFsmg2A2VkY7/S/3yd3TsqKIHq2NgB6UsHESj4fGIDvqaR6DQ3laFcZlQyHbpbcCP87P546vO1H0h3RMn4T8FpZCR9+7Pd1yRJh3g7ULgLpOFrdb56iGOVuuCSJYGw3hlHCDKf4eNH8Ang0535GU5G8K562aBNrnn3lFtFyyaebeN+7To2unvX8cb89HdMuLZ/eAc4SAF2ZVF6sPrHdbSMGsgxu7jAjUzGFjPMhK2vmPgLF/1Fagu4w70cvrSLoyrcHJf3VOqcxQBhs9AyCpp2VnueUaSKpsTfr65tEESdL0UAF4LstND/yY8sPUW+ebUYL0Y/vfHe7BoaAi3bec9UWmaBUylR7V+e/vewQb9n8VCMYoWIzo0iWyTu0OWDidqX4xJ4eOI6tHa9Yyp3glenY4DJYUvCrZ/Yn6EQALcx9qiYleRSOyYD1VevrshB0U4rqeIMXlB7ISbnSJm4v1YIStw8B6Ri9XKn65uv5byk2uPkZlIuKMP+yY+XPvGqONpg9tU9XhPQiq4DisV8RgkpoSbYZz15N3+qyajojZJJhA92v9ef/hUmMWGGuPidFvWVuY1rqJ2NvEuZdjxNdWL0CPAqMnWzqbsV10p3jCk0T+fY8Mcuu7lVH3KzSNV+qKZ42yGhWE1LBjeU6J/OSbjpaE28MzSKRG7C2cwGYM0HRuEl1MaVyBHsGJZ7b19Ff71PA3GIVpsZTrqiYCQDRmKq2Wi6qUj2YN29y+3fx+JVV1QV3jzYe1ShASaecDIWOS4s+yNIYlpnw9DMo9WqTZbcW/WFxOMasyq8m1TFuZFTMalp/VPk9u5fzmXyCxdBTjyofdgU73Q72g X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9d134498-8934-4bb8-07c9-08db1f6d169b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:35:59.6868 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: FNs6fz+QEo+stRwUfXERHt7vmRld3KDLFW2x0qgmp+Fi1MrZN40W8kjLCp0c2Dwi X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Nicolin Chen Allow the selftest to call the function on the mock idev, add some tests to exercise it. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/iommufd_test.h | 4 ++ drivers/iommu/iommufd/selftest.c | 39 +++++++++++++++++++ include/linux/iommufd.h | 1 + tools/testing/selftests/iommu/iommufd.c | 34 +++++++++++++++- .../selftests/iommu/iommufd_fail_nth.c | 27 +++++++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 28 +++++++++++++ 6 files changed, 132 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index b3d69cca77295c..e3f1035cbd6464 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -17,6 +17,7 @@ enum { IOMMU_TEST_OP_ACCESS_PAGES, IOMMU_TEST_OP_ACCESS_RW, IOMMU_TEST_OP_SET_TEMP_MEMORY_LIMIT, + IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE, }; enum { @@ -52,6 +53,9 @@ struct iommu_test_cmd { __u32 out_stdev_id; __u32 out_hwpt_id; } mock_domain; + struct { + __u32 pt_id; + } mock_domain_replace; struct { __aligned_u64 iova; __aligned_u64 length; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 17cb7b95eb2757..866d77247a07fc 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -455,6 +455,42 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd, return rc; } +/* Replace the mock domain with a manually allocated hw_pagetable */ +static int iommufd_test_mock_domain_replace(struct iommufd_ucmd *ucmd, + unsigned int device_id, u32 pt_id, + struct iommu_test_cmd *cmd) +{ + struct iommufd_object *dev_obj; + struct selftest_obj *sobj; + int rc; + + /* + * Prefer to use the OBJ_SELFTEST because the destroy_rwsem will ensure + * it doesn't race with detach, which is not allowed. + */ + dev_obj = + iommufd_get_object(ucmd->ictx, device_id, IOMMUFD_OBJ_SELFTEST); + if (IS_ERR(dev_obj)) + return PTR_ERR(dev_obj); + + sobj = container_of(dev_obj, struct selftest_obj, obj); + if (sobj->type != TYPE_IDEV) { + rc = -EINVAL; + goto out_dev_obj; + } + + rc = iommufd_device_replace(sobj->idev.idev, &pt_id); + if (rc) + goto out_dev_obj; + + cmd->mock_domain_replace.pt_id = pt_id; + rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); + +out_dev_obj: + iommufd_put_object(dev_obj); + return rc; +} + /* Add an additional reserved IOVA to the IOAS */ static int iommufd_test_add_reserved(struct iommufd_ucmd *ucmd, unsigned int mockpt_id, @@ -939,6 +975,9 @@ int iommufd_test(struct iommufd_ucmd *ucmd) cmd->add_reserved.length); case IOMMU_TEST_OP_MOCK_DOMAIN: return iommufd_test_mock_domain(ucmd, cmd); + case IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE: + return iommufd_test_mock_domain_replace( + ucmd, cmd->id, cmd->mock_domain_replace.pt_id, cmd); case IOMMU_TEST_OP_MD_CHECK_MAP: return iommufd_test_md_check_pa( ucmd, cmd->id, cmd->check_map.iova, diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index c0b5b3ac34f1e0..3044a432a83e22 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -22,6 +22,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, void iommufd_device_unbind(struct iommufd_device *idev); int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id); +int iommufd_device_replace(struct iommufd_device *idev, u32 *pt_id); void iommufd_device_detach(struct iommufd_device *idev); struct iommufd_access_ops { diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index da0443ba16830f..77b0601fd13a71 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -1001,6 +1001,7 @@ FIXTURE(iommufd_mock_domain) uint32_t ioas_id; uint32_t hwpt_id; uint32_t hwpt_ids[2]; + uint32_t stdev_ids[2]; int mmap_flags; size_t mmap_buf_size; }; @@ -1022,7 +1023,8 @@ FIXTURE_SETUP(iommufd_mock_domain) ASSERT_GE(ARRAY_SIZE(self->hwpt_ids), variant->mock_domains); for (i = 0; i != variant->mock_domains; i++) - test_cmd_mock_domain(self->ioas_id, NULL, &self->hwpt_ids[i]); + test_cmd_mock_domain(self->ioas_id, &self->stdev_ids[i], + &self->hwpt_ids[i]); self->hwpt_id = self->hwpt_ids[0]; self->mmap_flags = MAP_SHARED | MAP_ANONYMOUS; @@ -1274,6 +1276,36 @@ TEST_F(iommufd_mock_domain, user_copy) test_ioctl_destroy(ioas_id); } +TEST_F(iommufd_mock_domain, replace) +{ + uint32_t ioas_id; + + test_ioctl_ioas_alloc(&ioas_id); + + test_cmd_mock_domain_replace(self->stdev_ids[0], ioas_id); + + /* + * Replacing the IOAS causes the prior HWPT to be deallocated, thus we + * should get enoent when we try to use it. + */ + if (variant->mock_domains == 1) + test_err_mock_domain_replace(ENOENT, self->stdev_ids[0], + self->hwpt_ids[0]); + + test_cmd_mock_domain_replace(self->stdev_ids[0], ioas_id); + if (variant->mock_domains >= 2) { + test_cmd_mock_domain_replace(self->stdev_ids[0], + self->hwpt_ids[1]); + test_cmd_mock_domain_replace(self->stdev_ids[0], + self->hwpt_ids[1]); + test_cmd_mock_domain_replace(self->stdev_ids[0], + self->hwpt_ids[0]); + } + + test_cmd_mock_domain_replace(self->stdev_ids[0], self->ioas_id); + test_ioctl_destroy(ioas_id); +} + /* VFIO compatibility IOCTLs */ TEST_F(iommufd, simple_ioctls) diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c index d9afcb23810e1a..baaea15b355297 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -569,4 +569,31 @@ TEST_FAIL_NTH(basic_fail_nth, access_pin_domain) return 0; } +/* device.c */ +TEST_FAIL_NTH(basic_fail_nth, device) +{ + uint32_t ioas_id; + uint32_t ioas_id2; + uint32_t stdev_id; + + self->fd = open("/dev/iommu", O_RDWR); + if (self->fd == -1) + return -1; + + if (_test_ioctl_ioas_alloc(self->fd, &ioas_id)) + return -1; + + if (_test_ioctl_ioas_alloc(self->fd, &ioas_id2)) + return -1; + + fail_nth_enable(); + + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL)) + return -1; + + if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL)) + return -1; + return 0; +} + TEST_HARNESS_MAIN diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 85d6662ef8e867..3222f246600422 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -66,6 +66,34 @@ static int _test_cmd_mock_domain(int fd, unsigned int ioas_id, __u32 *stdev_id, EXPECT_ERRNO(_errno, _test_cmd_mock_domain(self->fd, ioas_id, \ stdev_id, hwpt_id)) +static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id, + __u32 *hwpt_id) +{ + struct iommu_test_cmd cmd = { + .size = sizeof(cmd), + .op = IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE, + .id = stdev_id, + .mock_domain_replace = { + .pt_id = pt_id, + }, + }; + int ret; + + ret = ioctl(fd, IOMMU_TEST_CMD, &cmd); + if (ret) + return ret; + if (hwpt_id) + *hwpt_id = cmd.mock_domain_replace.pt_id; + return 0; +} + +#define test_cmd_mock_domain_replace(stdev_id, pt_id) \ + ASSERT_EQ(0, _test_cmd_mock_domain_replace(self->fd, stdev_id, pt_id, \ + NULL)) +#define test_err_mock_domain_replace(_errno, stdev_id, pt_id) \ + EXPECT_ERRNO(_errno, _test_cmd_mock_domain_replace(self->fd, stdev_id, \ + pt_id, NULL)) + static int _test_cmd_create_access(int fd, unsigned int ioas_id, __u32 *access_id, unsigned int flags) { From patchwork Wed Mar 8 00:35:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 660797 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 8C774C6FA99 for ; Wed, 8 Mar 2023 00:36:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229960AbjCHAg2 (ORCPT ); Tue, 7 Mar 2023 19:36:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229984AbjCHAgT (ORCPT ); Tue, 7 Mar 2023 19:36:19 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2071.outbound.protection.outlook.com [40.107.93.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8899A7680; Tue, 7 Mar 2023 16:36:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EfoQw4x3kGI5SA+Lka6KGityZSlBFobxDd+ynJm1fitbiPaZC6QwhU5VrFSpeP/KMO4//0pobvO0AQXTvJJR8YPUmwviHCRLSPEid/mONdgWIXIxNdv3LO96bsYaWpRI/GIbYdiJsByPd+48DB/Bs4duD3cmd+st1u5UR/6zCMay5uMtRvkaLaGvjQng4wsYK+6Jk36/BhKnXKtR4SEe8H9A06dxDdRCt2DmKP21SzYOloyHR12Qpktcz1jbz7fA6Ot4dsf0EYfig0eN1SgnfYogEBAnnMhp/AWJZhty/yJfml8eYqICKzZj5PFhBZVQcp8K60DB8rRe9H9Fd4IixQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=WgOTId1p1reb/9qEk6FfJIw48Djw7Ii6fn7uA3GafNY=; b=nmE/72dwzkV0Tnmcl+J4KKVU6y155I70b5NUIb5CRB3xHS8Xbc+IKQdy06aCw2G2IuSXU7REkw95u9PuxiDlOWGGUXXV2V2zrVpnCXWbNLiCDep77EDcej/yru5WcoED3ilVK/GeDfGjv+Cq2Jy7Ag9SpBF1PmaYnpMBEEJtYmqACgM3YYBYrQqkTYBkBZxBc1k+FNrruXwULABkci4GdZil64Pqim/TlUkWSiESTDKsXg3Q/ogEtuzWw+pO96ytg4FZzSIwtNps4bkSNsUAyeVz8s09h/ttINp40DiP9WgzvG00V0eglty11MGSYUWdv2KZnG92OwX8VxrPBnfKXw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=WgOTId1p1reb/9qEk6FfJIw48Djw7Ii6fn7uA3GafNY=; b=agPK6wAKyGy46o5zim+wCyEOcEfcCgEmfDYadeJt0vh9jFBD7mQIKPTzQJ722TaMLO/WjNYsE7Ul8xUZiP4UNaiYeZ0m2j4H/u8gEiB5jp8t0KKWl37aH6dIqW5whJm5Qt1ttGrFWMSwBdIW4u8FbJv3I86tTfjBPZRPXZg+7pU4BscfvMFnGSgo/gxTaJv/7/EASua1Vfamq1+bgdmFrDfYwTrzbBf+LzdcIDPBWapg5Ka5HHqbVQQwr/4oTlHEYJT1SSkbeNGOvnC5hgF/PW0F4yinfp7xw7do3NKmwytuz4QBiWmVrZxZXH9k3aYPtREKsj42jhj31XfvzYc3YQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36:03 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36:03 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v2 15/17] iommufd: Add IOMMU_HWPT_ALLOC Date: Tue, 7 Mar 2023 20:35:54 -0400 Message-Id: <15-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT1P288CA0004.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01::17) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: afc13432-32e5-4326-20dd-08db1f6d17d1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Vu5gstXlLWuGS59tSsQMzS7pnUEEMvfnqZA5Y4YNzY8F/d1Wb/eRRr7Tq0WH4EH+7dDLGND+lF0Gsdc4/8gatvB0rdPzfbM6UK/BWr7wu1GTS9uAMR8LTxCWkap0v1QFizFnTKeChSAZf9qTby4bytZCZ+/0ysZdK9xnQ8PpIGHgaReI80lieuH6TBEgzPMUy11Royx33VBqEf0GuYdJ2WLhbOllhbQIMkTPQgeSsX+u+C6mTf6wJREJAG4JhUyVN6wPlkQD1KxLq05Z9uz13FbZCmn9yq0pBTweSaykFry60wqnhS9qZTrbkTyBW3u9x/omUOkcwMMSMdkGo/cnDaIMaxifPQUG+skpJMAzhfg4hqAl/5M3MkAlBL9+VxYaVEqA+2PxgK/hPvT6DmywDWcti4xjJ5vR3VSQFv4BnHCqo+fxUKa0xuhKKhD5HwOjP53eCQdYVRypVsMGfAyebXbDIk0FPt1OVzZ5khZxFWOKt1FjH7PUL5B6KIxIJqs8nCxct5RSD4x6uEQYxTmVHzt4zpB1G9ZSc3X5BjkFUZFlEe0rurC0/9vNcSm8B36840/DV5cQBonq0vuXJyQVm8lNQL4NAjTgLXrym+tH3PDyuD2TfztEeNw8GPIi4FzlvurVhHPOfI2gcIKh8GC+xA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230025)(4636009)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: hFgWYRrbEfhOYxmpQCQTT7KQaMtuU12zZPvLkZX4nUI/eZnGe9q9KzIOKL3rnU3x47yOFiX8E3dofYcj/qKTUgbNrwBZdtGC7e0RVl9rMPEkKUQs9eHNHKux52YKDhZiNSmsJa3xsVZ+jdW6/AIuDdnoO99Fz748B4kKBdeIzCDFzSWPreTe4pPOU2vhcT+rBAano8sXVDx295Lp1OJQYgnChU6w9B31i3DCgF9p1U2DzcERmCuNcpkXeWDSDb3X3Ob9DY3cgFJJb8mzvhDUMR/DVre8vMRH3e8fKCnpVuvrSrCqKBicOtdlwcEgrmgwCCzb2o8hURpgwUkMIc5G5JZry0lr9ix6yQNzjrOJYgkgnFUESS07Ng783C1YBAyDlCjCNQlrdOTpKastFgxwbT7UoE/NXktgEXok4GIIPkj+DPjViatjEIUqDU7fqYN/RTubcnQ+NdCUR9Rfw+IbJ/mdNqpdjQRBNOh6Y9ca/X5qEdltCVu+87fwQfeF1bh3PiHrtrNiEw7fct4JnnCs9OVGzxO6dFCUo8giajVfUi8+/Isu4j96oRrXjkloWm7i8n7X3yY96YsJhFV4/RCUkQ5DRV0GsMQVKb2ZKXOhFOymdDXwLUSfNT4ln0hZAlx5YzkCqQVp/C1J7FDcnnlo2XE3TIcBtV1XMilsar71O1Jr4mromDnrpvTqWKm6/+a/dT6kNffj7dMd0AkGsKqeXZuuY/XrVC6Enx+bGLD+G6cFX+dwLVy/VXa6ZHo/rhL1L6+YrDDxBPXsGQ9nomGiIvjZH+tVFkT4Ch4Wv7WMj3Q7055oobkv4eMhAmdPK3bjcehk+j3rJIGRp40msmQM3znFl1//nCKP0DR5b/D0tw7Wba8SwKRmZ0CLtN9qrr0PEVsAkxLUlbn92kmeNk5VmZ4lB8Cp27DB/jxKe1AvMD54VXT0wAxRxJh9CBh10cm2L8nchc2tU/38P4pOGeDl4elKSGjvV1eH8S49hn1dKHU+Oh/GwR6jQnIRIa3Li7mlQPYLDWRU0ZRgg7RTkff0KCQ1hOFp6naVmre8IiEQaWwFPA4vSeKMiSdI5uiJGfhccaeHZstYXez/U6Toj+VFiAf9jNTrdl++iqIOZuFnAJvo1j8/R6Ty1jrGMPgqvrTjJwfzP+2Yrj/mbIq11NLDmMmb4hwrH/D54UzSaWi60Im3qfFKqYXcu8mGgBg4dBITZLzV5NqOr9kVZsGmDku1yExQM3ye8GWGY/TCg5SApXTKFnnabpiou66jwpMvwtFy8yvNwI+bytR4K4vmXrQ7QTlLJwX7eLMBILm5dripoDNXMOjGDjdqEKQce693dcNkqB37BqukHnYgPwY2wp2E5EyWkWM40rWpZ66hcvfHE87xe0g+hgM//919QvP9/fpMDXmLv91CDdgSLrDrwcCmkAdnbUiGTWDcQiYJsY+fLa7T0FhrOv9sPbTqw/LbLsutl/rLjZjzNXxlFT0vS3bEGxBo1QaplpAFRQ3E4TwlPHsJjLp6nhMLRXuddwHmaWy2qPsLhiKYTUMpJJWGscGghxbNv6hGqrNWYPjQ+ELLeYDCgHu29VJZ5b/nq2iVpuxf X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: afc13432-32e5-4326-20dd-08db1f6d17d1 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:36:01.6253 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: X71+2LUPVn/qL4AAXRLJjK8UGobP9kAoNeRMA3/BcM44kR6G5diMcEny4MILVS+J X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This allows userspace to manually create HWPTs on IOAS's and then use those HWPTs as inputs to iommufd_device_attach/replace(). Following series will extend this to allow creating iommu_domains with driver specific parameters. Signed-off-by: Jason Gunthorpe Reviewed-by: Lu Baolu --- drivers/iommu/iommufd/hw_pagetable.c | 46 +++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 9 +++++ drivers/iommu/iommufd/main.c | 3 ++ include/uapi/linux/iommufd.h | 26 ++++++++++++++ 4 files changed, 84 insertions(+) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 2584f9038b29a2..13bdab4c801bd9 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -3,6 +3,7 @@ * Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES */ #include +#include #include "iommufd_private.h" @@ -121,3 +122,48 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, iommufd_object_abort_and_destroy(ictx, &hwpt->obj); return ERR_PTR(rc); } + +int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) +{ + struct iommu_hwpt_alloc *cmd = ucmd->cmd; + struct iommufd_hw_pagetable *hwpt; + struct iommufd_device *idev; + struct iommufd_ioas *ioas; + int rc; + + if (cmd->flags) + return -EOPNOTSUPP; + + idev = iommufd_get_device(ucmd, cmd->dev_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + + ioas = iommufd_get_ioas(ucmd, cmd->pt_id); + if (IS_ERR(ioas)) { + rc = PTR_ERR(ioas); + goto out_put_idev; + } + + mutex_lock(&ioas->mutex); + hwpt = iommufd_hw_pagetable_alloc(ucmd->ictx, ioas, idev, false); + mutex_unlock(&ioas->mutex); + if (IS_ERR(hwpt)) { + rc = PTR_ERR(hwpt); + goto out_put_ioas; + } + + cmd->out_hwpt_id = hwpt->obj.id; + rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); + if (rc) + goto out_hwpt; + iommufd_object_finalize(ucmd->ictx, &hwpt->obj); + goto out_put_ioas; + +out_hwpt: + iommufd_object_abort_and_destroy(ucmd->ictx, &hwpt->obj); +out_put_ioas: + iommufd_put_object(&ioas->obj); +out_put_idev: + iommufd_put_object(&idev->obj); + return rc; +} diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index f80b012e1bc200..cb693190bf51c5 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -261,6 +261,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_hw_pagetable * iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd); static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, struct iommufd_hw_pagetable *hwpt) @@ -297,6 +298,14 @@ struct iommufd_device { bool enforce_cache_coherency; }; +static inline struct iommufd_device * +iommufd_get_device(struct iommufd_ucmd *ucmd, u32 id) +{ + return container_of(iommufd_get_object(ucmd->ictx, id, + IOMMUFD_OBJ_DEVICE), + struct iommufd_device, obj); +} + void iommufd_device_destroy(struct iommufd_object *obj); struct iommufd_access { diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 9cba592d0482e7..694da191e4b155 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -261,6 +261,7 @@ static int iommufd_option(struct iommufd_ucmd *ucmd) union ucmd_buffer { struct iommu_destroy destroy; + struct iommu_hwpt_alloc hwpt; struct iommu_ioas_alloc alloc; struct iommu_ioas_allow_iovas allow_iovas; struct iommu_ioas_copy ioas_copy; @@ -292,6 +293,8 @@ struct iommufd_ioctl_op { } static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { IOCTL_OP(IOMMU_DESTROY, iommufd_destroy, struct iommu_destroy, id), + IOCTL_OP(IOMMU_HWPT_ALLOC, iommufd_hwpt_alloc, struct iommu_hwpt_alloc, + __reserved), IOCTL_OP(IOMMU_IOAS_ALLOC, iommufd_ioas_alloc_ioctl, struct iommu_ioas_alloc, out_ioas_id), IOCTL_OP(IOMMU_IOAS_ALLOW_IOVAS, iommufd_ioas_allow_iovas, diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 98ebba80cfa1fc..ccd36acad36a3f 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -45,6 +45,7 @@ enum { IOMMUFD_CMD_IOAS_UNMAP, IOMMUFD_CMD_OPTION, IOMMUFD_CMD_VFIO_IOAS, + IOMMUFD_CMD_HWPT_ALLOC, }; /** @@ -344,4 +345,29 @@ struct iommu_vfio_ioas { __u16 __reserved; }; #define IOMMU_VFIO_IOAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VFIO_IOAS) + +/** + * struct iommu_hwpt_alloc - ioctl(IOMMU_HWPT_ALLOC) + * @size: sizeof(struct iommu_hwpt_alloc) + * @flags: Must be 0 + * @dev_id: The device to allocate this HWPT for + * @pt_id: The IOAS to connect this HWPT to + * @out_hwpt_id: The ID of the new HWPT + * @__reserved: Must be 0 + * + * Explicitly allocate a hardware page table object. This is the same object + * type that is returned by iommufd_device_attach() and represents the + * underlying iommu driver's iommu_domain kernel object. + * + * A normal HWPT will be created with the mappings from the given IOAS. + */ +struct iommu_hwpt_alloc { + __u32 size; + __u32 flags; + __u32 dev_id; + __u32 pt_id; + __u32 out_hwpt_id; + __u32 __reserved; +}; +#define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC) #endif From patchwork Wed Mar 8 00:35:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 660798 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 4379AC678D5 for ; Wed, 8 Mar 2023 00:36:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229939AbjCHAg0 (ORCPT ); Tue, 7 Mar 2023 19:36:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229983AbjCHAgT (ORCPT ); Tue, 7 Mar 2023 19:36:19 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on20608.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::608]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95D05A3B43; Tue, 7 Mar 2023 16:36:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BpF/SZmUjbrOOZipzoBoM0eb/hWNBNIDvvmXNwKNHThfGqfoD0vUMmlZQDoe//ZP317/gg6l79L3znhdaFnMXwx4Bptjdg+HvgZJ8O7RCdaQ7O4sF43H/q/Hzk5gn8Yv1tMJbg30/Fsn3Iapd7GSYx1TVemV99nK0HRMDOiSxnEIP+6KleMvj1Z5EGr2Yh1/rCh8QGGU7pkCqHScgHxP3/lUR4jJDLo1m5BMq1BXpu3g+gwB1DW8xjSdZiJMs7G+V7DZLtN1QtDlumOBMB5xj+IGI6cSOHWDNhu9Kf45//O84aflBkgAK/G5uvEAAqshqvEDZ0aBgJACs/wnmMKrOQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=UWNmTsLY5vQfv6E+IgOlY+ECzeopldYwfZ8lazNJcjg=; b=Yg5H+62cg0gdEPnEodWPLK6iTr6EcWtgP4J0Em6Hwx8NiRErbPs3VTWvvuFtxmubb3T865cgg6oOcpIPTNRrXHPtFKCsvdwW5NFMQRpnxKL3Raz7hRWfJ6ABViWlXb1cWH/FrUBTIjAAHv7vzCdPybgjmzCnJ4HbdWP0R2TSaUEOYwrPTSPPi+zQGQmdM0cHaIs1i8j8eNbKcLQLF+nUHzN8f9SKjnSjIAPSKfXzuu3J2XoIjrKktDwHruc+V9Hh62ahvmwdjMIN1AeoWGaE/RQdwjS6rJeIa3usBTMxTJN/XieZAWV5sHx9BPKFPJkXOgqyjSr/AOC7G82gtayNzQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UWNmTsLY5vQfv6E+IgOlY+ECzeopldYwfZ8lazNJcjg=; b=b6pd2ESKQGYfuaxFs3SsmPnSH5XDW3DduD9wliDTK3eYjaPz9pBlfgwR+3FQjYDMkcWmp85FhjSm9IQKt5ySz5vUxb8BRIELXyHzTQSmMgjzbn/qVdeB13h0WbHCkL0193hHVD/tWfLYxT5GH+MA8PdTKz6n5J5ODe9lYaGSXriBkZjan+UHvVdrsnZT6mfWqTZooCeu5YlP+bN42BWZzrfSzz+DvYXd1NUd6TLmz0FSmS7vntVbFpSfa66V4B72hIG5FGX+5dFI8OdQ1z/wa4SuL8/pHPhpjxwRHU/ImHHtMaUJIGG9dV9voiU6cHjkSm3NrfSAMjUuULk0NSPzow== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by IA0PR12MB7625.namprd12.prod.outlook.com (2603:10b6:208:439::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.28; Wed, 8 Mar 2023 00:36:02 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%5]) with mapi id 15.20.6156.029; Wed, 8 Mar 2023 00:36:02 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v2 16/17] iommufd/selftest: Return the real idev id from selftest mock_domain Date: Tue, 7 Mar 2023 20:35:55 -0400 Message-Id: <16-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v2-51b9896e7862+8a8c-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4P288CA0085.CANP288.PROD.OUTLOOK.COM (2603:10b6:b01:d0::16) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA0PR12MB7625:EE_ X-MS-Office365-Filtering-Correlation-Id: d6b1dc7d-62ab-43aa-db2a-08db1f6d16d1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: iBE4TF69IcH0leFODuUW10nVOtSkr2qJDNqvJ1NuoNl8SEjefL6ttVRns92B6h/2fTzuefteP2oFudYgxpfgfGGknPFEGfZH2i/4VqCqDQuq9yoRk71xGXZ2WASika0mqqJKiK1xm3HrQHiyA0DO7Hf+FHQtpWbUrlHcdmdtw0Cdv8VSPknuukh6VadCkYCl+bqL2O52hz9hqrWT8OvkVCuTA+u9ut/Jv5VpzFzH0CJbjZxGyKwGwpVhPoQUCo2w15PIhgMIm6T1YPkuVPetV4PGwu756l5mFHUh0y9T65bqDi+VnRozRT2UxIveNwfYDn63704t7e8sFj9yWUsTlUpsSOOkWXbkLn61Se+/Wz/eM7n01qT1I76H2yZSN44el6A8qsZ6cNTRd+DOwDdOzsPqUJqc/rELSKXmZM2EEbykiiOizcn22DzGuiJN3/LceQDnoFnZyjnCNZy+IersPQGSRCOCPcojz+iMbXQibh0JDsmDjSHoYhiEiK+kDrIVf9Z+7ahD0ofyFXiY9fAEL3wvMDAoJ/lYwJHkCpqi+VXyfboCFQy2+uV+gbd4seXUXOWgpHsYhzhF+e7/9rBqgo3gvaBKsPcfg2hWQGC24yGrbUXox32+sdQnPemZXNJ6B/bAgXNkwoRTVTXApt5/JQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230025)(4636009)(376002)(136003)(396003)(366004)(346002)(39860400002)(451199018)(36756003)(41300700001)(66556008)(4326008)(8936002)(8676002)(2906002)(5660300002)(66946007)(86362001)(66476007)(38100700002)(6666004)(478600001)(54906003)(6486002)(316002)(6512007)(83380400001)(6506007)(2616005)(186003)(26005); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2qAtph4BuGJKIFsNpDOXH/YtM7tj+vWHIY+LQjgREAaQd/yEZf9gzFc+yo2GrQA026JjYtqdHIV9pv9YzB49ZBwKx7G4rCcK3cPJT8R6l3tOMQIArQYVIXk8ppZ36HT5pH7nvpncddjT9Z2G2OS/vg18TX+3k9n6bjmVyhcQRFadxUVM8hnnbxMYzHmrYn7sJg7QZ8Itk8JSL1QlN5KI2GMZExzv+hoovhQb+s1tm9Mit3r5malilJsamAO4qFBm1f60TqFMG0kPmLY1ICDUvNnIi/kkzQ4JLuJBM3wz9G12tN1h0p/GOxm5aF9LUGwAOYN6Fa5be9karz0lQHOBJOKC3Bd0iCZUI/+5wpb866+pj8xfBBbFm8JHGUpN97Gv90i1GOVlEAYywV5XlMHKgaXdUhaBB7yUBQ87Sm+FhHWiH3Pf/07GoSbAtVeQzgZdSiYptO0W5R0LZ2D2oAIidN0fvDhPtSIO+zbiRGwaJpvIqw0UCIRG7MOwB65ZcRh5E9W0XIIYO52O122B6pWnRbNUducHIjaFlyXAnb2l64liiD92c2zqeBc4hGZ2VLsjw/eJf1BN4T+neJCtRgo7djMznHk6w4k9PG80DnU/2A6sfZtp7IFB6qnAgio0htd/dLvtZVAbu9wN5fyvC1s+4vaoL2GR27VqX9C4n9oGc0gYGbf75nZ5W2mtuKNjBsrhMeskWkAnQ0s1wVBalZ6oLCIlpcyow29fMCQAk1qodVulhwVMTPL7AYH1uC9PD0yr8k1cATHyBRClQvPWaiAXl81cSCUKWNFIRc8bYMvb9mJ1yjkQRFnhxJFyQZg7N2f8CCCCHu4N26ddaDXj5zyXCf0cQ+kU9b9u4u3s6bLt0Io2JiGSyS3REwuSzDMED8RIex5+L3Z4mdy9FIarohJqzKOW7ed07E7nIeqFX4KY9WZw3SzqFlkcM+Hr7zV0lDqwMB3yGj2Mtejw8M5vgNj4aVO2hEXw1kGbJjW7JMpWNpQyQO6LsxvUCXl5bTAgHLKfaGIPUDqzchKbgG3XQ9d6TZ7+WcoQblwdOH8mRVqHyy5P/508L5pTlPwj0A3vaAek55ZeMT3c+VUPVOywdJ0Gb+4dxqztlJWrzuINIc1FdlD04qF9NC1IjZBtU/6UVmLpuRkOyTfC2FXwI53zttvvTw9whooi5BWRoNsh6fsoSoD19N/oLkOxO3AwGTfkKMnFU/4eo35AnQaVun5vJghfh2ihpCbl7AIR9m0qrU2SqsuCNP6E0CcXq9umqiViAJVfW2ootuAWEYKoHRzp4PCMCBtNuX8gz4MmoA9Rz/hSUrEhbWI51/HY0lL4E+Z001fZM/B1gduj4MV5LCmTgGTkrvf5NU9xAhhBER17tr1zafTK/ZlXdzX8hULy9jEGvt58yRif4KlB/5zA6nYlUo+ldCAbaFRAq+OZB/YkC3yG7mV6omt6Wqy7n8T8IIL2SUha+IbO5mAjm1IfOukzYs8xolPcLziPwsxd9/sIkOWfAyasqbGO/4xXz9MF6q3quBZBZVl4z1zQW2VkBPtHNvreuqJYkPsLNs3KZHN8WsCQ9NHFLk23iynLFo1BXliSAF5E X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d6b1dc7d-62ab-43aa-db2a-08db1f6d16d1 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Mar 2023 00:36:00.0461 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: uYt/NuB3bWxOYmUduW7gtQn5CTnZp+hzaUZq9g3dtUe6zBKWhRN5yn7josOrLp4f X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7625 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Now that we actually call iommufd_device_bind() we can return the idev_id from that function to userspace for use in other APIs. Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/iommufd_test.h | 2 ++ drivers/iommu/iommufd/selftest.c | 1 + tools/testing/selftests/iommu/iommufd.c | 17 +++++++++-------- .../testing/selftests/iommu/iommufd_fail_nth.c | 18 ++++++++++-------- tools/testing/selftests/iommu/iommufd_utils.h | 12 +++++++----- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index e3f1035cbd6464..dd9168a20ddf4c 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -52,6 +52,8 @@ struct iommu_test_cmd { struct { __u32 out_stdev_id; __u32 out_hwpt_id; + /* out_idev_id is the standard iommufd_bind object */ + __u32 out_idev_id; } mock_domain; struct { __u32 pt_id; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 866d77247a07fc..3e2284811fb326 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -443,6 +443,7 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd, /* Userspace must destroy the device_id to destroy the object */ cmd->mock_domain.out_hwpt_id = pt_id; cmd->mock_domain.out_stdev_id = sobj->obj.id; + cmd->mock_domain.out_idev_id = idev_id; iommufd_object_finalize(ucmd->ictx, &sobj->obj); return iommufd_ucmd_respond(ucmd, sizeof(*cmd)); diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 77b0601fd13a71..83c98a371f9e7a 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -214,7 +214,7 @@ FIXTURE_SETUP(iommufd_ioas) for (i = 0; i != variant->mock_domains; i++) { test_cmd_mock_domain(self->ioas_id, &self->stdev_id, - &self->hwpt_id); + &self->hwpt_id, NULL); self->base_iova = MOCK_APERTURE_START; } } @@ -265,7 +265,7 @@ TEST_F(iommufd_ioas, hwpt_attach) { /* Create a device attached directly to a hwpt */ if (self->stdev_id) { - test_cmd_mock_domain(self->hwpt_id, NULL, NULL); + test_cmd_mock_domain(self->hwpt_id, NULL, NULL, NULL); } else { test_err_mock_domain(ENOENT, self->hwpt_id, NULL, NULL); } @@ -650,7 +650,7 @@ TEST_F(iommufd_ioas, access_pin) _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_PAGES), &access_cmd)); test_cmd_mock_domain(self->ioas_id, &mock_stdev_id, - &mock_hwpt_id); + &mock_hwpt_id, NULL); check_map_cmd.id = mock_hwpt_id; ASSERT_EQ(0, ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_MD_CHECK_MAP), @@ -805,7 +805,7 @@ TEST_F(iommufd_ioas, fork_gone) * If a domain already existed then everything was pinned within * the fork, so this copies from one domain to another. */ - test_cmd_mock_domain(self->ioas_id, NULL, NULL); + test_cmd_mock_domain(self->ioas_id, NULL, NULL, NULL); check_access_rw(_metadata, self->fd, access_id, MOCK_APERTURE_START, 0); @@ -854,7 +854,7 @@ TEST_F(iommufd_ioas, fork_present) ASSERT_EQ(8, read(efd, &tmp, sizeof(tmp))); /* Read pages from the remote process */ - test_cmd_mock_domain(self->ioas_id, NULL, NULL); + test_cmd_mock_domain(self->ioas_id, NULL, NULL, NULL); check_access_rw(_metadata, self->fd, access_id, MOCK_APERTURE_START, 0); ASSERT_EQ(0, close(pipefds[1])); @@ -1002,6 +1002,7 @@ FIXTURE(iommufd_mock_domain) uint32_t hwpt_id; uint32_t hwpt_ids[2]; uint32_t stdev_ids[2]; + uint32_t idev_ids[2]; int mmap_flags; size_t mmap_buf_size; }; @@ -1024,7 +1025,7 @@ FIXTURE_SETUP(iommufd_mock_domain) for (i = 0; i != variant->mock_domains; i++) test_cmd_mock_domain(self->ioas_id, &self->stdev_ids[i], - &self->hwpt_ids[i]); + &self->hwpt_ids[i], &self->idev_ids[i]); self->hwpt_id = self->hwpt_ids[0]; self->mmap_flags = MAP_SHARED | MAP_ANONYMOUS; @@ -1218,7 +1219,7 @@ TEST_F(iommufd_mock_domain, all_aligns_copy) /* Add and destroy a domain while the area exists */ old_id = self->hwpt_ids[1]; test_cmd_mock_domain(self->ioas_id, &mock_stdev_id, - &self->hwpt_ids[1]); + &self->hwpt_ids[1], NULL); check_mock_iova(buf + start, iova, length); check_refs(buf + start / PAGE_SIZE * PAGE_SIZE, @@ -1427,7 +1428,7 @@ FIXTURE_SETUP(vfio_compat_mock_domain) /* Create what VFIO would consider a group */ test_ioctl_ioas_alloc(&self->ioas_id); - test_cmd_mock_domain(self->ioas_id, NULL, NULL); + test_cmd_mock_domain(self->ioas_id, NULL, NULL, NULL); /* Attach it to the vfio compat */ vfio_ioas_cmd.ioas_id = self->ioas_id; diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c index baaea15b355297..7a3d149019da92 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -313,7 +313,7 @@ TEST_FAIL_NTH(basic_fail_nth, map_domain) fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, 262144, &iova, @@ -324,7 +324,7 @@ TEST_FAIL_NTH(basic_fail_nth, map_domain) if (_test_ioctl_destroy(self->fd, stdev_id)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; return 0; } @@ -348,12 +348,13 @@ TEST_FAIL_NTH(basic_fail_nth, map_two_domains) if (_test_ioctl_set_temp_memory_limit(self->fd, 32)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2, + NULL)) return -1; if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, 262144, &iova, @@ -367,9 +368,10 @@ TEST_FAIL_NTH(basic_fail_nth, map_two_domains) if (_test_ioctl_destroy(self->fd, stdev_id2)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2, + NULL)) return -1; return 0; } @@ -526,7 +528,7 @@ TEST_FAIL_NTH(basic_fail_nth, access_pin_domain) if (_test_ioctl_set_temp_memory_limit(self->fd, 32)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, BUFFER_SIZE, &iova, @@ -588,7 +590,7 @@ TEST_FAIL_NTH(basic_fail_nth, device) fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL, NULL)) return -1; if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL)) diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 3222f246600422..e67a929a5c87d3 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -39,7 +39,7 @@ static unsigned long BUFFER_SIZE; }) static int _test_cmd_mock_domain(int fd, unsigned int ioas_id, __u32 *stdev_id, - __u32 *hwpt_id) + __u32 *hwpt_id, __u32 *idev_id) { struct iommu_test_cmd cmd = { .size = sizeof(cmd), @@ -57,14 +57,16 @@ static int _test_cmd_mock_domain(int fd, unsigned int ioas_id, __u32 *stdev_id, assert(cmd.id != 0); if (hwpt_id) *hwpt_id = cmd.mock_domain.out_hwpt_id; + if (idev_id) + *idev_id = cmd.mock_domain.out_idev_id; return 0; } -#define test_cmd_mock_domain(ioas_id, stdev_id, hwpt_id) \ - ASSERT_EQ(0, \ - _test_cmd_mock_domain(self->fd, ioas_id, stdev_id, hwpt_id)) +#define test_cmd_mock_domain(ioas_id, stdev_id, hwpt_id, idev_id) \ + ASSERT_EQ(0, _test_cmd_mock_domain(self->fd, ioas_id, stdev_id, \ + hwpt_id, idev_id)) #define test_err_mock_domain(_errno, ioas_id, stdev_id, hwpt_id) \ EXPECT_ERRNO(_errno, _test_cmd_mock_domain(self->fd, ioas_id, \ - stdev_id, hwpt_id)) + stdev_id, hwpt_id, NULL)) static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id, __u32 *hwpt_id)