From patchwork Fri May 19 18:42:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 684743 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 EAC6EC7EE2D for ; Fri, 19 May 2023 18:42:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229526AbjESSmk (ORCPT ); Fri, 19 May 2023 14:42:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230085AbjESSmc (ORCPT ); Fri, 19 May 2023 14:42:32 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2067.outbound.protection.outlook.com [40.107.96.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BBFDCE47 for ; Fri, 19 May 2023 11:42:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=N+hha18fz6a7De40QrhabatoUuiA5zyQ0RO3tpiLtpSgXE8ZxpA5lkRyDLa64J+PWoviFgQNLg84Ndo456GU8Y6FD1KoTiPe7b9CLwFdnIHTA73io7RRigZdGHfsF+4pV+AoVMubyezGDUFPoJ/C9bpvsjU5uRAr+Jv9uig4jgJqkf9G4gVvF71kv8ze53MfhS4JvE7r8a4BfykrdbgU03S5zE6EdXUEon2SKzzz2mHFYn9MsDhnDKm1wxWcysoBexdjBTSp2M0GjHw4qPT51EcFgTGHSunugyeK6s78iFI0Bx5RDTogM60p5Pn9j9Sd9v4Kc6Bmkcw59frZyTXw9Q== 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=uLCq0rN4b+G0Zc5V7KBmZ80W06Mpx3GOhDrwa6vK/Ug=; b=WmZJwgeEMFp0b9v0Ye/zVFuLTBoruI40rvjwidJGeVtfi01CcJhbXW4BC30C4ZgYAM0cKsZMfME+OrzeBEcuB8PLsBfOiuJMuLAMBZ+NbJP89PsOV+Bajow7claYE0VqTvUII0YOkTol2EhDD9Jw4sLTly30UejEERAMpBNMb5kbNl1QO9dl4VqPUeiTzzG2wLgoCcE2rqFdRBtsID73pOhFjvEolycUne8FNJaGVUnPWbgJ7BgRxX3qkSCMTtLJ1+7HBcphRjsl60Lmstox9eTam7txazD3penz5pYuZaYF6TzvHpByr0Pr9mkIk+dqiezk/5SuMvPQcwklezOwJg== 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=uLCq0rN4b+G0Zc5V7KBmZ80W06Mpx3GOhDrwa6vK/Ug=; b=VP4kBQfpL4jVOy+USq7j0Z91is/ZwKj8xE5G/ZBv4wS1JGY/VfG68ed6tJKKfT7OrAoheEwHYASbdsbjM3Mta0dgM9ZXaD9ARX02XHHQpD3KMMq+ZPS14C6ikZTKnft5nkBf6AIVzO2mtFcvd7ukqXOeXvMD8v6RCM1pkxeSyuiY4aPXU6ynUvNFIjAn3BrGsKvrA0uDRiIoWF+hvyuSIeTYr4r8iRe3MejsBCGab+Yf2LMgzTcyy77SkE0q7/jpYvKapOy0VSzn/YtCymFzAtfdG0J7HpA+iFB57TtgVA1Gn/SODgkgfLlxAxtzGWmy0pXL6Q3qOT5bLTRCxJFO6A== 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 PH7PR12MB5688.namprd12.prod.outlook.com (2603:10b6:510:130::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.17; Fri, 19 May 2023 18:42:26 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6411.021; Fri, 19 May 2023 18:42:26 +0000 From: Jason Gunthorpe To: Lu Baolu , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH v2 01/10] iommu: Have __iommu_probe_device() check for already probed devices Date: Fri, 19 May 2023 15:42:10 -0300 Message-Id: <1-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0356.namprd13.prod.outlook.com (2603:10b6:208:2c6::31) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB5688:EE_ X-MS-Office365-Filtering-Correlation-Id: fdbe3b32-d88c-4cf3-16e2-08db5898c7bd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: mdKeXHRXUW3YYA70p+tWLtjoqjFBLXRt+WD+Zd/IGFKtlfYEnTcWBM8ZdXQTqLJAbrJWy43s9CnOdAZHQK4iXrhQl8047VrFvBtXTP+ZxogBLHWj58J2JxwJchEmlDEhPk81r0chqtDdN9gV09eM2KY4XoOFAQWj1zLY0kspk6GvbuDVBWhDt06xh4hzIEsMKzOU2QYoWXku2edNx6Frdy11q2ThCa7/QC5lOPj34Cfth4XKMLGVarNXo3ZBPi7txcD31LVbsqJt/EinqKzGh43TKqKRl4afAbPmMJYbS1x05RXlngY/qHogAHunjGHySWJ4xMhtanfs3D1Q72hgVkgVQ5PTt/n2CrjD820OX6V0s42gMqvj8MVVJMrvGWKpqTcQqGHWW7zuuT+G4A9q75TZ3l5q2oDHtF6vi8IZFEFhhi2CG1jxtqQzlIZ386C5R0jQgE0ecFLFggrQRhy6KQy4irxFMxN7Y07XoUV0Rp9+dVK00qpN9I9H/I+Xncofdrx5rqfsTlTonlFdkN5R3czkufVWqviWainHNTYjmlBOGTc9H9TbbUuLIDmRcTPMd7x5vfHNVzqfRjQYqacl+kbjKFVaEPPJfvbugwXq1+M= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(346002)(376002)(39860400002)(136003)(366004)(396003)(451199021)(6512007)(6506007)(2906002)(186003)(107886003)(26005)(86362001)(83380400001)(2616005)(36756003)(38100700002)(316002)(41300700001)(6666004)(4326008)(6486002)(66556008)(66476007)(66946007)(110136005)(54906003)(7416002)(5660300002)(478600001)(8676002)(8936002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: mjkzeunwBQvy4G2I0f3OLohx2Pw/oHrPa917505N5ClxJSCcHvAZ07ffwPj5Kb1ye8RfZdM7vCxOG+gk8SL5UwgVfoLzW2AIpMhmQtX1SZYtqUhcNrn5XvY8dev/48U6d4rsLHFoN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0BcOBieycwHGGDrBSRPYNS5SL0T4SgeqdZotKnr6IjnkCBvv35aA9e8mmlB9dRmAYRkTKeJNItPHcjFM/Q6gnx8ME4RduPIbW7lCiSTM5Bdd9qdJNn4uL2wLGayz63I7NMFC6jS2fhcLnrIpdrz7V4aN9rtelpVwluc3GUpluyKkvguC/IcP2NRMcEApnan6EglzBFNf58xfweeJ/SA7X+ow0vzCcW8p7jSlQfcaVdx57xWQbZ2wJ85n9AjP55RfMDmA3x0ia2dPN3mutJ1u0ojVJytJKmMH9F/IptI/3l+XW6He9Zw6ORfoLw0pMbmTMFzOnuCxI7DnLQ1SqExFjuHof79rG4dHup5gaS1UdfvCnrtSI X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: fdbe3b32-d88c-4cf3-16e2-08db5898c7bd X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2023 18:42:21.3591 (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: 9r4Qaf5Yf09qd+BzAB/S++aP8+uzzyDlkrcZASGHG7Qnim4ISFn9a7qQIdTJyRpm X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5688 Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org This is a step toward making __iommu_probe_device() self contained. It should, under proper locking, check if the device is already associated with an iommu driver and resolve parallel probes. All but one of the callers open code this test using two different means, but they all rely on dev->iommu_group. Currently the bus_iommu_probe()/probe_iommu_group() and probe_acpi_namespace_devices() rejects already probed devices with an unlocked read of dev->iommu_group. The OF and ACPI "replay" functions use device_iommu_mapped() which is the same read without the pointless refcount. Move this test into __iommu_probe_device() and put it under the iommu_probe_device_lock. The store to dev->iommu_group is in iommu_group_add_device() which is also called under this lock for iommu driver devices, making it properly locked. The only path that didn't have this check is the hotplug path triggered by BUS_NOTIFY_ADD_DEVICE. The only way to get dev->iommu_group assigned outside the probe path is via iommu_group_add_device(). Today the only caller is VFIO no-iommu which never associates with an iommu driver. Thus adding this additional check is safe. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe Acked-by: Rafael J. Wysocki --- drivers/acpi/scan.c | 2 +- drivers/iommu/intel/iommu.c | 7 ------- drivers/iommu/iommu.c | 19 +++++++++---------- drivers/iommu/of_iommu.c | 2 +- 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 0c6f06abe3f47f..945866f3bd8ebd 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -1579,7 +1579,7 @@ static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev, * If we have reason to believe the IOMMU driver missed the initial * iommu_probe_device() call for dev, replay it to get things in order. */ - if (!err && dev->bus && !device_iommu_mapped(dev)) + if (!err && dev->bus) err = iommu_probe_device(dev); /* Ignore all other errors apart from EPROBE_DEFER */ diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index b871a6afd80321..3c37b50c121c2d 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -3763,7 +3763,6 @@ static int __init probe_acpi_namespace_devices(void) for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) { struct acpi_device_physical_node *pn; - struct iommu_group *group; struct acpi_device *adev; if (dev->bus != &acpi_bus_type) @@ -3773,12 +3772,6 @@ static int __init probe_acpi_namespace_devices(void) mutex_lock(&adev->physical_node_lock); list_for_each_entry(pn, &adev->physical_node_list, node) { - group = iommu_group_get(pn->dev); - if (group) { - iommu_group_put(group); - continue; - } - ret = iommu_probe_device(pn->dev); if (ret) break; diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 4d7010f2b260a7..6d4d6a4d692893 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -351,9 +351,16 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list * but for now enforcing a simple global ordering is fine. */ mutex_lock(&iommu_probe_device_lock); + + /* Device is probed already if in a group */ + if (dev->iommu_group) { + ret = 0; + goto out_unlock; + } + if (!dev_iommu_get(dev)) { ret = -ENOMEM; - goto err_unlock; + goto out_unlock; } if (!try_module_get(ops->owner)) { @@ -399,7 +406,7 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list err_free: dev_iommu_free(dev); -err_unlock: +out_unlock: mutex_unlock(&iommu_probe_device_lock); return ret; @@ -1711,16 +1718,8 @@ struct iommu_domain *iommu_group_default_domain(struct iommu_group *group) static int probe_iommu_group(struct device *dev, void *data) { struct list_head *group_list = data; - struct iommu_group *group; int ret; - /* Device is probed already if in a group */ - group = iommu_group_get(dev); - if (group) { - iommu_group_put(group); - return 0; - } - ret = __iommu_probe_device(dev, group_list); if (ret == -ENODEV) ret = 0; diff --git a/drivers/iommu/of_iommu.c b/drivers/iommu/of_iommu.c index 40f57d293a79d4..157b286e36bf3a 100644 --- a/drivers/iommu/of_iommu.c +++ b/drivers/iommu/of_iommu.c @@ -159,7 +159,7 @@ const struct iommu_ops *of_iommu_configure(struct device *dev, * If we have reason to believe the IOMMU driver missed the initial * probe for dev, replay it to get things in order. */ - if (!err && dev->bus && !device_iommu_mapped(dev)) + if (!err && dev->bus) err = iommu_probe_device(dev); /* Ignore all other errors apart from EPROBE_DEFER */ From patchwork Fri May 19 18:42:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 684747 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 D412DC7EE29 for ; Fri, 19 May 2023 18:42:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229896AbjESSm0 (ORCPT ); Fri, 19 May 2023 14:42:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229807AbjESSmZ (ORCPT ); Fri, 19 May 2023 14:42:25 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2067.outbound.protection.outlook.com [40.107.96.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 276DCE43 for ; Fri, 19 May 2023 11:42:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QvQaRVm4/nPoPRjp7JacOWb4CVuiKN3+WebDDgnLaLbTJZYROKMxsx4DrOiBMywoS2dHfQH8CPImQDU4O8XiHjLUGdaqEESJQ3QfLaxeVuXWCI9Qdp7QrvkOHP8I7+Dem2TFX3E7j9K1cAYAgsbUjucjA3V05jJe/LibbShn/ZPqU+luo/L6gHDbYR6N9Y2QYKyfb5NSkFbNEm8tGSwm+wPYC2wOSlSAMPd8XMoM/2zX2QJQZDiG91WO00CMWJXST9aG5596OPoGT+is3grBDRPuAHj67b9YTetxV818Am3tv/M9qvcO6jlESu68BcUFbzBUPLYnmCyARoWN3o3yMA== 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=unx8iixdguglJ86odjJJQvNzUSqu1X4Rja7CGPmUGK0=; b=VvcrneWZuI2TfWVc85Pz0b8b30TQXZyUOPySQPByoO3O+RGr6tIE6VKHOKx4Plw+vbzTBWmKirxKzyfICVHNFnE9LiDjjbY4GQos+qvW02a1JkSOZYdldz3ZwFMDqkoTw1g5hMHEwYsucV/7faf/jZapxAmPdUN5ZLXtd7IaTbjwm1V1fMpKjX04fxvyEkC00GB1i0HleMLMTFn+nVQHhJ4MTRPNpqzeTFG1zjvEOjfEoE/fNjD5eJXP8BjFnlqelm6puWdCaQn+3JiglMspPV3HapxIIYvKtG7YVSPKSp2nB7HAwWfDH2QNzBj0iEmUzlOUypqvy/Q2+FH8X8Nzyw== 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=unx8iixdguglJ86odjJJQvNzUSqu1X4Rja7CGPmUGK0=; b=MhtGPxW/PrqHqmv2XXFxge3NrPysfPwdGFycl76emEkgMLikHss0wl/sRdjyO3YJD5pXGK/QGj0lHq9SXqJOivz48hVOfi8TbkLuqAYZ5GegLkYx+H0Dso+bJ6r7bDqFP3noOJHVAoUYa4uE8aKA8GvWrqiPVPCeEtPiKX6mOjUawbMWhPBiNBpkIG0PSyadEA1EG4zOoNMzqbE5Y23dZeuaJ7c+kY1voyifWpGlkyZRpDA1+wF1ROXBWDJpWX8IInI//KAsh4oIDSVCVIHg+mdYA7ngeZ1ygcZiD6YxUsSsiSoeFLyOS/tWQtwOCZCuswMk5cAAR/AEz7IsMYVk2g== 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 PH7PR12MB5688.namprd12.prod.outlook.com (2603:10b6:510:130::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.17; Fri, 19 May 2023 18:42:22 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6411.021; Fri, 19 May 2023 18:42:22 +0000 From: Jason Gunthorpe To: Lu Baolu , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH v2 02/10] iommu: Use iommu_group_ref_get/put() for dev->iommu_group Date: Fri, 19 May 2023 15:42:11 -0300 Message-Id: <2-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR0102CA0055.prod.exchangelabs.com (2603:10b6:208:25::32) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB5688:EE_ X-MS-Office365-Filtering-Correlation-Id: 2da57a84-7859-4161-6150-08db5898c71d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PV6J/AaGdVvYazOPEGRj/fc+Qng8EcHnTl59p6WyFk/ndPhgb11/oLGWRXHWxgOmxluYSKSZbJ6r+7FCRaHXsSgdi1LuYGvz1ribj4jBGh5A6eQMuoGMIGl1ELEsdwUB9uuzJB5POIhxye52FkptpDUasngl/2zwHkaiWvRqreE+l08rJGpcM61K1B/jMj9G/igtHVnt2aA5SElpB6Ou/2jdV14ady7wbM2qStNxb9k63IVtMOvOvenrT7+JLjCOK4/NZdUYc9RZOPrV2QKWw6UUh+te7dDWsVvXANUVNctlfLN7r8jRwb7z5iMfAOuDa+67LJ/NF7hYL7idUnaxH2H52T7Lg6p7x88EUoUOfPQI2c3OkfhT29PrZvAtbXUvlPUoQVCePtjFFENbwRqXz8bXw947GVEQNzvgxhBfg7qTQiugVS+sxv91gjZ19yq4J43RWhjIMCqXiDZc1S04h5Pes72nKTP/dhfe6+ADVbjOQ8hHOqz59KHhJA/rQnNJHGD9Kknc20Y2nbdbYEIRYjzwGXRES+tAtl21iF3yZs8vBvBnQ3uiDCEIfKFr/Lgrj4/ar6KD01cNHGzeN5TqBRYO88gZigOTXM773XNamNg= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(346002)(376002)(39860400002)(136003)(366004)(396003)(451199021)(6512007)(6506007)(2906002)(186003)(4744005)(107886003)(26005)(86362001)(83380400001)(2616005)(36756003)(38100700002)(316002)(41300700001)(6666004)(4326008)(6486002)(66556008)(66476007)(66946007)(110136005)(54906003)(7416002)(5660300002)(478600001)(8676002)(8936002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: LChSyLa3fXo4scfl0a2s11EtsVmTQEJeB2PYkDr7cbZlYj6xiHudCE4AlWnz8uPM4gExWrDThHQpm53IUh3i9tG4LfJwv4VwxMRS38DBqlrLa7msKpkOGaZrnciGAnVxnfY61c4Eh2XNd5Thrdu/q0acNZEf22soLz4Unkpy+uG8TL9QjCn32ei8g91gv70EovaEviOmUDwwpSVRymxSjIDtSUeurlq0cfL669EDPakjGc6RPqEf+dshxzHz5okQVPBis+JY3xntPfw43J/Jq0m8RLVAapmqeISjz6ciT1tzHDb/ziG9GdesS4Bx705V1wRgOQXxsjq1k3GbR3S8prCymUUcZy8fwtI7AMOxTr4JeWzZScvEAhtAzpZBq1UwVeDenzG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wUAOPYq8WUddfHuUGo8yCpJYl200tijmxvApd5OHqqZAaVFQddoYGKgW8unInyPZNHEYlA9h+jf12o1xOgryNeOLTrl9pOIGIPo59Uak8i4ON1BGle/h/byMCZeLZj9oMo8Iq97Mpt7D X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2da57a84-7859-4161-6150-08db5898c71d X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2023 18:42:20.4124 (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: SSuk/vG5VPeNWxiDsgibEH55wgYeO/WlHSj9NSdmsg0vERPmbMjK+6s+PWmZnEkf X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5688 Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org No reason to open code this, use the proper helper functions. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommu.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 6d4d6a4d692893..35dadcc9999f8b 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -500,7 +500,7 @@ static void __iommu_group_release_device(struct iommu_group *group, kfree(grp_dev->name); kfree(grp_dev); dev->iommu_group = NULL; - kobject_put(group->devices_kobj); + iommu_group_put(group); } static void iommu_release_device(struct device *dev) @@ -1067,8 +1067,7 @@ int iommu_group_add_device(struct iommu_group *group, struct device *dev) goto err_free_name; } - kobject_get(group->devices_kobj); - + iommu_group_ref_get(group); dev->iommu_group = group; mutex_lock(&group->mutex); From patchwork Fri May 19 18:42:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 683953 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 BC863C7EE29 for ; Fri, 19 May 2023 18:42:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229964AbjESSmj (ORCPT ); Fri, 19 May 2023 14:42:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229965AbjESSma (ORCPT ); Fri, 19 May 2023 14:42:30 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2067.outbound.protection.outlook.com [40.107.96.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A625FE46 for ; Fri, 19 May 2023 11:42:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mZu3ldiixcawenS8p6RNYgvP3e3YEkiJdRoQ1NWIX8LUFODx+u55QhxqzdpsNFfNkv3/+bFSmSqCxYdyvfidZ88NXksGPdE2qsZg8x0aKDYdQVRdL286MdEwkvP2u4C/w+Du9iwYcPdMzXgR24jGJ4cJnWNSwiK7Q6V0sH/18z3RkEk2dIenN9tHPKLVrYOmMq5H/+HyJZBBiP+gXX575EJ72m+5Zgt5/GL9tPGTIsBXTjIoCV+IomwoX0qSegDKkOKEjVKLsiudlYwt9s8R/vNWbcSNtyvV03SUw8AIs9A8LKcKzLEDfH26vp4BMNYVduHEUpDrUpJ5h6ty8GnobA== 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=S8eyityGz2Z9BPkDXqtjTwMJyakT/MLhPoAbbHxF9QY=; b=JgkNIjMm2AskmqbbStaisHOCLezDda94cb0bzZVouokxrw6CdWTaVunbw8e+q1tHYS2aLhQEIcCDjToF6c8Myai+Vi4uue4ae5h898R5D4tTq0QiGDJN4Wr374PWeMFQ6QZmimF2FxZaJCZEFvWGohx/0vdZxHhAumjz5bDw809MeQAa0ASDUl6/pRID6Qpo+0Z4I7QvN2s28hROPLhHBCW39gjodggbhpGizLZ3TAEyqdOMY3sSDBfyY5Swlxo8j48v003aKsSmvRfsqPTd6fnB47R84Ts1tBpM1Wmfsrz2n4yu7zyTgFJ4y0vwk4HMPSJ8NShYmAecxiJsTYG2OA== 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=S8eyityGz2Z9BPkDXqtjTwMJyakT/MLhPoAbbHxF9QY=; b=sEggHflAevHd0yAq9/LKFNsqkKuFGOy1l4ZkRV+SmxInSrUiMhH4js64SB72mFG9vzO1iDtGSPKa+wyAJFInyiM45Q9ZpnOv7XCVs9SIDMezxDtehnF64tr92XBwshai033CpQjiOWyB+Fa6yjJM6UKZXqwa9SSHr/LievvqYWuRhE/skSMMlBapVr1fq9wdpmEYTsWLR61P7nlBQExeCBjGyRDrKW7jPPBtoQ6yMkp+DrQGilzJb35Vk2rRkLPyml1lDw1ZHvZ99QG8L9U1lQYKBYkG1srth5gIRNj40smsPOFv/C6TxtG3NruK5MSNFa1TN9XS2r7Q2cf6lJ9NQg== 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 PH7PR12MB5688.namprd12.prod.outlook.com (2603:10b6:510:130::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.17; Fri, 19 May 2023 18:42:25 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6411.021; Fri, 19 May 2023 18:42:25 +0000 From: Jason Gunthorpe To: Lu Baolu , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH v2 03/10] iommu: Inline iommu_group_get_for_dev() into __iommu_probe_device() Date: Fri, 19 May 2023 15:42:12 -0300 Message-Id: <3-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0357.namprd13.prod.outlook.com (2603:10b6:208:2c6::32) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB5688:EE_ X-MS-Office365-Filtering-Correlation-Id: a4087fa3-a73e-4e44-19da-08db5898c79e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PMqT1YPDnGRobDGdyKflaLsFJNDJRWNcWvtbv5cpqhZfs5065pS3BOtP/6/SXWQdJoFu0AI/VnPOHIB/xRrQzdyzC7p8X7d4Kt1wfJBgZoaGJJ4trUNaDY/Ub9zjTnaSYJzLg4aQo5+nGb+VpX1aVLrIglVIMlg8tWgLmUl2XCDuc0CmhoAj96H3Y0xxY4GDNIA0zZvsXMUqMIwkhW2ni1V0BkC3ZhbLJre0tN5glLRebD6UP+5qtakscbJExSO0ZH6HGbSNxSHe4aqeum+oL2lp51QIx3TeaFhQD3cEsSTLJyrZ6BQy/8eGw7JRMo1Uyj/gEZcm+4SZF8xOwAajYRz7hrT+i5KLuJlkR18BMQNLx6OcmPzBgqPTV0nqmTLe9Kv/gwr7VY63RIohHzQqc7KbMFaYNfR/H6PzzoTcojdEQdXqdpsmOcJZRyMANAiM56P3cK9dnBKxX4Z2+2j3jfM4UeYvlxl+RHRtcaSBH8syc/2Bjcw7ISka23SUTAnFwDJY2kRhaZrrXhKEmPWzBL10kAPvXjXpOiTTVX30WdQGIvZdRMMFQQw/aYzMv76FdDcs4UQp/41138d3PO6O/BYyjV7Bx0jP9YRX2r7otAo= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(346002)(376002)(39860400002)(136003)(366004)(396003)(451199021)(6512007)(6506007)(2906002)(186003)(107886003)(26005)(86362001)(83380400001)(2616005)(36756003)(38100700002)(316002)(41300700001)(6666004)(4326008)(6486002)(66556008)(66476007)(66946007)(110136005)(54906003)(7416002)(5660300002)(478600001)(8676002)(8936002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: rShZpthHw13x53VssbGTrnpaEQfDCBso2yfFLfpryjLNDEtKwj4LGOHHVvCFf4/fxZp0MGahMlGtznlVhCwcMIgx8R0itszrzN+P1cSm0+TWYctqUJe/6rtpNh0wlbVhU3OaskfN6ElOTSwmZi7/aZOH20tvwwSP34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Bw6Zh24BYdfpkAXK69bENVyvWB3KrjXupjv6WSKmSeTZq9fWi1Ekz6n14U24p8sC7buInHMNCO83ibO1DkxJXg0hhw0/2epJR6Q0L5hHgvmUf3hh86AgveROuqXP+rENVAqR0KbdNwTvYoml51pvxZN+aywJPVTVe3j/5VcdKeMM+IiIfikn1oHw8hGgq1n5brDnLqIxVgU6FDsPEII0cNqqpl32DsoZUMSJnpmto5HxtiULWoM9On1c1IvJ9d5iPTONr15DFJlnzkxeE2cph32v3Ge2P67LY57xpSS7o5o8Vk9u8rNc41HNy2R6J8EwZS65VN37tt0QARPCsw1tIq4mtZu78UtBk2UOGUoCv8 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a4087fa3-a73e-4e44-19da-08db5898c79e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2023 18:42:21.1854 (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: Shj7aqnkhPHd94Take0M2oonw3ZPge6g/baocjIfDtNLwttci4h3rC9Y5nMBaWUN X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5688 Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org This is the only caller, and it doesn't need the generality of the function. We already know there is no iommu_group, so it is simply two function calls. Moving it here allows the following patches to split the logic in these functions. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe Reviewed-by: Lu Baolu --- drivers/iommu/iommu.c | 50 ++++++++----------------------------------- 1 file changed, 9 insertions(+), 41 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 35dadcc9999f8b..6177e01ced67ab 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -127,7 +127,6 @@ static int iommu_setup_default_domain(struct iommu_group *group, int target_type); static int iommu_create_device_direct_mappings(struct iommu_domain *domain, struct device *dev); -static struct iommu_group *iommu_group_get_for_dev(struct device *dev); static ssize_t iommu_group_store_type(struct iommu_group *group, const char *buf, size_t count); @@ -379,12 +378,18 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list if (ops->is_attach_deferred) dev->iommu->attach_deferred = ops->is_attach_deferred(dev); - group = iommu_group_get_for_dev(dev); + group = ops->device_group(dev); + if (WARN_ON_ONCE(group == NULL)) + group = ERR_PTR(-EINVAL); if (IS_ERR(group)) { ret = PTR_ERR(group); goto out_release; } + ret = iommu_group_add_device(group, dev); + if (ret) + goto err_put_group; + mutex_lock(&group->mutex); if (group_list && !group->default_domain && list_empty(&group->entry)) list_add_tail(&group->entry, group_list); @@ -396,6 +401,8 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list return 0; +err_put_group: + iommu_group_put(group); out_release: if (ops->release_device) ops->release_device(dev); @@ -1670,45 +1677,6 @@ iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) return dom; } -/** - * iommu_group_get_for_dev - Find or create the IOMMU group for a device - * @dev: target device - * - * This function is intended to be called by IOMMU drivers and extended to - * support common, bus-defined algorithms when determining or creating the - * IOMMU group for a device. On success, the caller will hold a reference - * to the returned IOMMU group, which will already include the provided - * device. The reference should be released with iommu_group_put(). - */ -static struct iommu_group *iommu_group_get_for_dev(struct device *dev) -{ - const struct iommu_ops *ops = dev_iommu_ops(dev); - struct iommu_group *group; - int ret; - - group = iommu_group_get(dev); - if (group) - return group; - - group = ops->device_group(dev); - if (WARN_ON_ONCE(group == NULL)) - return ERR_PTR(-EINVAL); - - if (IS_ERR(group)) - return group; - - ret = iommu_group_add_device(group, dev); - if (ret) - goto out_put_group; - - return group; - -out_put_group: - iommu_group_put(group); - - return ERR_PTR(ret); -} - struct iommu_domain *iommu_group_default_domain(struct iommu_group *group) { return group->default_domain; From patchwork Fri May 19 18:42:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 684746 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 0863BC7EE29 for ; Fri, 19 May 2023 18:42:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229504AbjESSm2 (ORCPT ); Fri, 19 May 2023 14:42:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229568AbjESSm0 (ORCPT ); Fri, 19 May 2023 14:42:26 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2067.outbound.protection.outlook.com [40.107.96.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2401D1A8 for ; Fri, 19 May 2023 11:42:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cfNwRc3G6fFk2kIP+wqn2WRELvtAoE0pOD8zVD7iVsUkUk0Aal0Y5zvOq1IflG3/B8XCsDtbmPkKjTav519m2vXEoEuLLA5uAd8YOJ7qFuohBaVRrAr9tqUOang/SkrlbKMBjVqQRksP3EbRv/mu4Fasd7/Ri7m0rKWiiunBw9rHZky3833D7qh+6CQ8Hg0mIQs6wDTf0y3MaTMxp+1h5LcKGXo+eQsqHvMGWpatZKB3n/HoK7MutQC3LpaKy920JX2wQl/v432vaL19fFlHq9D0ll+n8UknsAyJMQ6+v76LvTtOmDBKcigalolX+AqxN+x0EJYhQEG7afQYJGW1hg== 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=oEFGh2NCg84u6GJbWjbXyhatr3JUaR3D6Q4yUm+j9TE=; b=YKnDMVgF1YPDuzhN7Yt5zNSLaaitV/eIioOuLkcxLV2xUZfwIBz0uZN39apRSZ79NZcJoWNuXwwBJjhY1DRerDRlXGG6F0GemiiZhPItmL/jtHdvPjyGU7IZnKc6hfxCF3TELQAJpcZNlfyqy4m2Fd55a/u1tvYOLpguHPUNmcexAfFH/licjlubhvKjXUkLju1NMmM72AEP9I/HYKLN2JN9UU2oJWvVf3DWf5iG1walQJe5Qt7aF+Hy0Wt88DgdL3CV72UVWwtbs3kUX8uN2v7EkgJb9miS2swIfLsBG9gcgnLRMT7QxduAJ3uohdH7G0TvS+rrCUIHezt8dgTOAQ== 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=oEFGh2NCg84u6GJbWjbXyhatr3JUaR3D6Q4yUm+j9TE=; b=JTlFNj6AHDbe/kSfVdoblekNjmv/N5rkeWMA/jBg6hTRIhrUuzUucfowHflut7kUAAkeWRntcs+HUzO74d/Wep9W/5ULFOIO0z37l3Kugn2GFdiS5k4xKdUopqYdQvNQjuhyMowapyytlLD/FOr+myEYaCqp7ZuNC++EdnWChpUWKX2AWBXtCja/FZmAVHiLhozgAMVJfN2UUROnAFkdbxQjB8mfQrYfLPCGt37kbXKR8JpuXthey0A6RL/cryJgY7PDIu+3vff/O5kQ0SXYka/JRdI6EGSZDfJHejpdVYUXJ6IAjNu41dAt0eNoPf45Sp7N64bNcTSMUuKKzIaC5g== 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 PH7PR12MB5688.namprd12.prod.outlook.com (2603:10b6:510:130::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.17; Fri, 19 May 2023 18:42:23 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6411.021; Fri, 19 May 2023 18:42:23 +0000 From: Jason Gunthorpe To: Lu Baolu , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH v2 04/10] iommu: Simplify the __iommu_group_remove_device() flow Date: Fri, 19 May 2023 15:42:13 -0300 Message-Id: <4-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0277.namprd13.prod.outlook.com (2603:10b6:208:2bc::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_|PH7PR12MB5688:EE_ X-MS-Office365-Filtering-Correlation-Id: 86af0670-cbf4-44a7-99d8-08db5898c737 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: F57yI+b33tIT7/lwU5bYqMsAq+d43JRCZHlMajEEh5tD2UqV08v+3FndaUoNi7u79DFtXtAuCiM8q5AkzLh1vkd/C6nTnigenujP7TLaqm1aBfpdPPCQqm0RXkVJ7lD4/CLdF5nEPcveGgwZqTnRVWGix9xDp0TMXzNLOsSciV11JQII0Ri3rbLHU98d3L6+SLRRQ+qEo4jqyb890TqUGvh+cJOwSij/v2Ty9dUW+08p//ScbeykK76uN0FuLCWRlSHta/u5jlmMjUUhXvBeSkzZxXvNMlKNGwYlz1Bf1m8zAWYVMEuJIiUiRTWcOCLoJyJvEHJ3YVZvMDpMiL2l+2IstzWM+FlT8tXDOT0iRTOV2L8urFWjGxyfBHQaZvSxMH3m74cSvrG/UMzeMgiCIhTpFgqm8Bqh8GZAilrXGIkTglOK4EtOqMKaaAxlFhcidYiUCbvAE72zNiMafJh4HeigrV/GxuM4a2Rh4bxmAjGp8iJqOJNSkKEWLoVozyfglIuTMBYrwHUTdDCmdYFvOpqMBpSUlHUaXy+4XeN42QsBwzxNf8uSBejjSKjItffhUpWSVpDUczW+Mh1YeAXihH1sw9co4/IQ19DPuJVkbkY= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(346002)(376002)(39860400002)(136003)(366004)(396003)(451199021)(6512007)(6506007)(2906002)(186003)(107886003)(26005)(86362001)(83380400001)(2616005)(36756003)(38100700002)(316002)(41300700001)(6666004)(4326008)(6486002)(66556008)(66476007)(66946007)(110136005)(54906003)(7416002)(5660300002)(478600001)(8676002)(8936002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: wtmZ3KBFyg2M6VgpirrPlNf4mmhLw+fkIl9yMwGnOv2HqF3EXRH8eH0gKF63ezvmY1T20+D+rSZVaoSyzQycw8X73CagovCmWqKGhRvupB3Qh7SyTkil91hO3SuyOdaZlc7dbxp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VIJbUh6Yoyc0YMu82F+rErqmoNtm8sntxCLvjzBVj4RVB0NHw7r+m2DJqDyATFm/nWdVHIBABjpPK0hGqq/vxrlyCRjm9NLUqh2eNvkTwIwktER61MaUxr8KMt+gsGMoh1Vk2S/LVYkHifkCmFYK4Kn9ferrbAV25M2XykNRlYWsePPuO4iTtmoSAu3fGeCw5VFhVIf/Rn/YPn9sTMQJjYbIRxTgGke7aIjjUwzKbIOOvCh10vbJOhnD0CmZMegOwZFTwdlE4hq85DmTCcXZgkmlzhMUoaCeoWxh4z79KPfIKalowYZDaNryvVL9KvklMjmnAAZz+/OeVPNkGv5hU8A6C5iTtOYQCa9OCgfRX08MDCKfoQqBgl4nMvlGaEvnKyyFpZ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 86af0670-cbf4-44a7-99d8-08db5898c737 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2023 18:42:20.5382 (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: /n4Gk87vOg/hDMcOksTCzhNKLVZVUnzMOVnrBZyMM1FTtgNf9yri2YjKEYXRfM/P X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5688 Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org Instead of returning the struct group_device and then later freeing it, do the entire free under the group->mutex and defer only putting the iommu_group. It is safe to remove the sysfs_links and free memory while holding that mutex. Move the sanity assert of the group status into __iommu_group_free_device(). The next patch will improve upon this and consolidate the group put and the mutex into __iommu_group_remove_device(). __iommu_group_free_device() is close to being the paired undo of iommu_group_add_device(), following patches will improve on that. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommu.c | 83 ++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 44 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 6177e01ced67ab..a87e2df5ce1238 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -470,32 +470,8 @@ int iommu_probe_device(struct device *dev) } -/* - * Remove a device from a group's device list and return the group device - * if successful. - */ -static struct group_device * -__iommu_group_remove_device(struct iommu_group *group, struct device *dev) -{ - struct group_device *device; - - lockdep_assert_held(&group->mutex); - for_each_group_device(group, device) { - if (device->dev == dev) { - list_del(&device->list); - return device; - } - } - - return NULL; -} - -/* - * Release a device from its group and decrements the iommu group reference - * count. - */ -static void __iommu_group_release_device(struct iommu_group *group, - struct group_device *grp_dev) +static void __iommu_group_free_device(struct iommu_group *group, + struct group_device *grp_dev) { struct device *dev = grp_dev->dev; @@ -504,16 +480,45 @@ static void __iommu_group_release_device(struct iommu_group *group, trace_remove_device_from_group(group->id, dev); + /* + * If the group has become empty then ownership must have been + * released, and the current domain must be set back to NULL or + * the default domain. + */ + if (list_empty(&group->devices)) + WARN_ON(group->owner_cnt || + group->domain != group->default_domain); + kfree(grp_dev->name); kfree(grp_dev); dev->iommu_group = NULL; - iommu_group_put(group); +} + +/* + * Remove the iommu_group from the struct device. The attached group must be put + * by the caller after releaseing the group->mutex. + */ +static void __iommu_group_remove_device(struct device *dev) +{ + struct iommu_group *group = dev->iommu_group; + struct group_device *device; + + lockdep_assert_held(&group->mutex); + for_each_group_device(group, device) { + if (device->dev != dev) + continue; + + list_del(&device->list); + __iommu_group_free_device(group, device); + /* Caller must put iommu_group */ + return; + } + WARN(true, "Corrupted iommu_group device_list"); } static void iommu_release_device(struct device *dev) { struct iommu_group *group = dev->iommu_group; - struct group_device *device; const struct iommu_ops *ops; if (!dev->iommu || !group) @@ -522,16 +527,7 @@ static void iommu_release_device(struct device *dev) iommu_device_unlink(dev->iommu->iommu_dev, dev); mutex_lock(&group->mutex); - device = __iommu_group_remove_device(group, dev); - - /* - * If the group has become empty then ownership must have been released, - * and the current domain must be set back to NULL or the default - * domain. - */ - if (list_empty(&group->devices)) - WARN_ON(group->owner_cnt || - group->domain != group->default_domain); + __iommu_group_remove_device(dev); /* * release_device() must stop using any attached domain on the device. @@ -547,8 +543,8 @@ static void iommu_release_device(struct device *dev) ops->release_device(dev); mutex_unlock(&group->mutex); - if (device) - __iommu_group_release_device(group, device); + /* Pairs with the get in iommu_group_add_device() */ + iommu_group_put(group); module_put(ops->owner); dev_iommu_free(dev); @@ -1107,7 +1103,6 @@ EXPORT_SYMBOL_GPL(iommu_group_add_device); void iommu_group_remove_device(struct device *dev) { struct iommu_group *group = dev->iommu_group; - struct group_device *device; if (!group) return; @@ -1115,11 +1110,11 @@ void iommu_group_remove_device(struct device *dev) dev_info(dev, "Removing from iommu group %d\n", group->id); mutex_lock(&group->mutex); - device = __iommu_group_remove_device(group, dev); + __iommu_group_remove_device(dev); mutex_unlock(&group->mutex); - if (device) - __iommu_group_release_device(group, device); + /* Pairs with the get in iommu_group_add_device() */ + iommu_group_put(group); } EXPORT_SYMBOL_GPL(iommu_group_remove_device); From patchwork Fri May 19 18:42:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 683956 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 7C80DC77B75 for ; Fri, 19 May 2023 18:42:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229807AbjESSm1 (ORCPT ); Fri, 19 May 2023 14:42:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229875AbjESSmZ (ORCPT ); Fri, 19 May 2023 14:42:25 -0400 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2080.outbound.protection.outlook.com [40.107.100.80]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5550E42 for ; Fri, 19 May 2023 11:42:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PGuFTQD3KT+0cfJmW0bG7VSCacgn0g/OlTcTRuiBSghjb7ACP3iYUN3aVGY1qYmof9g+eaJnT8EnVLDI3FoaTWyNCQCCDyl4CcjT64Oqs2zVgRrML2QV1cM/9nNZaRlDhPpoBKe0rzAotUlnFnu1Gg3ix1AlrotmwiI8Gd18anOp+GxIyxdb++z6rirPAF+3zst+CWh/SLFhXnf8mIcLlhpApkzPIS+eQFhLsHKLV/MUAgxHfRpbClU6kDL4LG8URfrjg0LTgnU3PtoOsw86fpFpJ+6vVMsjuoD4HBVDz5saGZsJIANQgPWfEIVl2snqci4Y+imAyOIBUVddR1d4/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=q5Hsf5WmlRmpquubWkA0QjrycF0ZlaAdT/eKa4hezI4=; b=VDrsYqrb5YWxinEPgqcXmD4MOk2hjlgD3kMLuU+SAkL8EVmLkYODwLG//b+dRe0nebnFy9Wf97YY0HVb/8FlzJaFxlNdES8wliIqlQ9EG1OhDqkUvWKh9fd2Us1kddVIxGM363QP+pQ5e7fyDSKvTa1iQXbnQkY8sd89iMhWHKPhUm8QrZmU4D2/mo+fqV3klKCDNCegPWaJ+s/nx2Wb97webMmx4gqAW/Zl3NjVFGxU32bXQHV1JUFqasB/y9mOxVD1pqRli5KwhMeUFXE9KZRJdvf4GG4tO24Nhbgzis2wwVxo6DGNZ0l7hltOVBebimlCmIXTECWGWIaarcUcag== 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=q5Hsf5WmlRmpquubWkA0QjrycF0ZlaAdT/eKa4hezI4=; b=qs1kS5DRC1TxOtK5Q4ZcaGxSkKUlpJ6Pbc+kkS8bhdtQEVo4Vgc0L8OSLd40ZnTb9gpaAkHyVieGh5vVnY4OqNobeIijcFw09gcPOyu617nM0ClUSqNRl5sM1Gm+E4M2M73NTLHMJdHB02UPhhaWiSoAzPJ9M6nSZeADTsAjnIuH0xs1JH+wED0DP7TZ3vxvHioeIpXNB7Bsn20cR+HLJu15DXWREcGHBZIazZSmAM/Hy1m2ghhVGzOEuXZ1Ye0L2NiZM8H11KVAM5VtB6msG8mPXXPhoBtp7WbXpe5rzTB54p5aNPIQozj9eEk7NtnXPEp5xCQdml6RmV2p3wygtw== 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 MW3PR12MB4395.namprd12.prod.outlook.com (2603:10b6:303:5c::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.19; Fri, 19 May 2023 18:42:21 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6411.021; Fri, 19 May 2023 18:42:20 +0000 From: Jason Gunthorpe To: Lu Baolu , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH v2 05/10] iommu: Add iommu_init/deinit_device() paired functions Date: Fri, 19 May 2023 15:42:14 -0300 Message-Id: <5-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR0102CA0055.prod.exchangelabs.com (2603:10b6:208:25::32) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4395:EE_ X-MS-Office365-Filtering-Correlation-Id: 2c26d129-172c-4d1e-8afe-08db5898c705 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: K3SzWpej6n4emt+ZUndJZrkSR0/WWiRQvzR8HOIZEOkckW+AZ+IhSpJTbO9w0+bxlHSr8pwOkcKmIFBGXtp0zNRw9OWQx/X49nYk8NAn7MBnODdDkz5459DbXzH07JVV2PDvBf0kV11MrenvApDkBN8Ry4top5e/iiv6t7zUsQJXMuMjX7XNXkCdllJCReWpxNzs9JvbmYmVTEKcMWKk4TfF0OI2uegQPPxyEf6f4f0He1xW/78F3tZq55/jTr6OxFz+4HeJHlFD3WFHhaV9i4OLwYlOjkDSF5PTmQwmfsQ7UZIo0P/Tpo99DFlZyRdtfv42+PPCzasmvnCEZQ9bAZPMzLDve8dImjhnAxiMiKncDxSpjIadGDkII48ZKpvZzE37CiqjVsryEOliiXQSIhSTp/bunzMPe9R+c/PQGBSKDLvtJMgcfdVC0J6H78nkrC6Jl3PyttLvhwLqVsOW2Nzlb27LsmNnrIRv1TvdsqJ6hB0J7ywB8qM4vn01jtOIXRVsZ0C0t/6qiDGkcppIoLTvpCkuEVAshyq5eVlYftsUmf0QRuN2ZSR8ez1pbGvS1b2HHMOcm5xg9+qWEd6094wbyBQwviaSHrmJ2dofkFM= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(39860400002)(136003)(396003)(366004)(376002)(346002)(451199021)(2906002)(316002)(478600001)(41300700001)(8936002)(8676002)(4326008)(6486002)(7416002)(110136005)(54906003)(5660300002)(6666004)(66946007)(66556008)(66476007)(6512007)(26005)(107886003)(6506007)(38100700002)(186003)(83380400001)(36756003)(2616005)(86362001)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: VI1dB1oEwkCNs27X98dKM1i49VG3wt1B1GzTzbbmEebkttygJxBx58jHaBxB6N6jlDjd0zchHphEgMq0+4Y4H5FnyE+oLMsB3SdfINit5N3oVOwAbN+TajVIMEx1ohgSem8pTR+srgvRYi2wGPTIaG1+t2kPxcqJy1lkm7U2sTW7m7GaQLBOEgMmAVk6Tasj0z9i8YcuPGCzdlEw9NKP+/elHzBodtKF+IC1CO2zJQGRBE7SnJ03VfMwd5SWRj6AScDtD4v3URihXo53qa81OZ7OBuxkOhDwppETrVVjq3O5go/kWHdJVFM12pXwFA8Fjranjuc4MJ2YOGoTYSntxa1cv/31ulm8+P+gYttuHkzhrUOdkqiGBfex0HLIIoX7JP5m5C9rPjiTmzXFOTLRGoJbAgu8dw43LOmKz9C535vwyQYlI/+fTaDUu0GbOmJ4zy4/jAcEQmFU4xKnaXOi7sWQENz0YOjFlhXaKejUUGjH/BbSriArFSDhCR6SgZwaBDijkWF7fxTOywpxFWP5uIR/a9EQUqhvhaicJWE0/0cnXY/eIqdLMult3h4EtKs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-4d1e-8afe-08db5898c705 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2023 18:42:20.2684 (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: Lt0bEuoHgb/QKCTMxs35MZGgQvN+B7TkfXHPOoyO9gjcjxtxvSxM2/jVVjBqNyUt X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4395 Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org Move the driver init and destruction code into two logically paired functions. There is a subtle ordering dependency in how the group's domains are freed, the current code does the kobject_put() on the group which will hopefully trigger the free of the domains before the module_put() that protects the domain->ops. Reorganize this to be explicit and documented. The domains are cleaned up by iommu_deinit_device() if it is the last device to be deinit'd from the group. This must be done in a specific order - after ops->release_device() and before the module_put(). Make it very clear and obvious by putting the order directly in one function. Leave WARN_ON's in case the refcounting gets messed up somehow. This also moves the module_put() and dev_iommu_free() under the group->mutex to keep the code simple. Building paired functions like this helps ensure that error cleanup flows in __iommu_probe_device() are correct because they share the same code that handles the normal flow. These details become relavent as following patches add more error unwind into __iommu_probe_device(), and ultimately a following series adds fine-grained locking to __iommu_probe_device(). Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe Reviewed-by: Lu Baolu --- drivers/iommu/iommu.c | 190 +++++++++++++++++++++++++----------------- 1 file changed, 112 insertions(+), 78 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index a87e2df5ce1238..2031cb4782b9b5 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -332,10 +332,99 @@ static u32 dev_iommu_get_max_pasids(struct device *dev) return min_t(u32, max_pasids, dev->iommu->iommu_dev->max_pasids); } +/* + * Init the dev->iommu and dev->iommu_group in the struct device and get the + * driver probed + */ +static int iommu_init_device(struct device *dev, const struct iommu_ops *ops) +{ + struct iommu_device *iommu_dev; + struct iommu_group *group; + int ret; + + if (!dev_iommu_get(dev)) + return -ENOMEM; + + if (!try_module_get(ops->owner)) { + ret = -EINVAL; + goto err_free; + } + + iommu_dev = ops->probe_device(dev); + if (IS_ERR(iommu_dev)) { + ret = PTR_ERR(iommu_dev); + goto err_module_put; + } + + group = ops->device_group(dev); + if (WARN_ON_ONCE(group == NULL)) + group = ERR_PTR(-EINVAL); + if (IS_ERR(group)) { + ret = PTR_ERR(group); + goto err_release; + } + dev->iommu_group = group; + + dev->iommu->iommu_dev = iommu_dev; + dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev); + if (ops->is_attach_deferred) + dev->iommu->attach_deferred = ops->is_attach_deferred(dev); + return 0; + +err_release: + if (ops->release_device) + ops->release_device(dev); +err_module_put: + module_put(ops->owner); +err_free: + dev_iommu_free(dev); + return ret; +} + +static void iommu_deinit_device(struct device *dev) +{ + struct iommu_group *group = dev->iommu_group; + const struct iommu_ops *ops = dev_iommu_ops(dev); + + lockdep_assert_held(&group->mutex); + + /* + * release_device() must stop using any attached domain on the device. + * If there are still other devices in the group they are not effected + * by this callback. + * + * The IOMMU driver must set the device to either an identity or + * blocking translation and stop using any domain pointer, as it is + * going to be freed. + */ + if (ops->release_device) + ops->release_device(dev); + + /* + * If this is the last driver to use the group then we must free the + * domains before we do the module_put(). + */ + if (list_empty(&group->devices)) { + if (group->default_domain) { + iommu_domain_free(group->default_domain); + group->default_domain = NULL; + } + if (group->blocking_domain) { + iommu_domain_free(group->blocking_domain); + group->blocking_domain = NULL; + } + group->domain = NULL; + } + + /* Caller must put iommu_group */ + dev->iommu_group = NULL; + module_put(ops->owner); + dev_iommu_free(dev); +} + static int __iommu_probe_device(struct device *dev, struct list_head *group_list) { const struct iommu_ops *ops = dev->bus->iommu_ops; - struct iommu_device *iommu_dev; struct iommu_group *group; static DEFINE_MUTEX(iommu_probe_device_lock); int ret; @@ -357,62 +446,30 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list goto out_unlock; } - if (!dev_iommu_get(dev)) { - ret = -ENOMEM; + ret = iommu_init_device(dev, ops); + if (ret) goto out_unlock; - } - - if (!try_module_get(ops->owner)) { - ret = -EINVAL; - goto err_free; - } - - iommu_dev = ops->probe_device(dev); - if (IS_ERR(iommu_dev)) { - ret = PTR_ERR(iommu_dev); - goto out_module_put; - } - - dev->iommu->iommu_dev = iommu_dev; - dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev); - if (ops->is_attach_deferred) - dev->iommu->attach_deferred = ops->is_attach_deferred(dev); - - group = ops->device_group(dev); - if (WARN_ON_ONCE(group == NULL)) - group = ERR_PTR(-EINVAL); - if (IS_ERR(group)) { - ret = PTR_ERR(group); - goto out_release; - } + group = dev->iommu_group; ret = iommu_group_add_device(group, dev); + mutex_lock(&group->mutex); if (ret) goto err_put_group; - mutex_lock(&group->mutex); if (group_list && !group->default_domain && list_empty(&group->entry)) list_add_tail(&group->entry, group_list); mutex_unlock(&group->mutex); iommu_group_put(group); mutex_unlock(&iommu_probe_device_lock); - iommu_device_link(iommu_dev, dev); + iommu_device_link(dev->iommu->iommu_dev, dev); return 0; err_put_group: + iommu_deinit_device(dev); + mutex_unlock(&group->mutex); iommu_group_put(group); -out_release: - if (ops->release_device) - ops->release_device(dev); - -out_module_put: - module_put(ops->owner); - -err_free: - dev_iommu_free(dev); - out_unlock: mutex_unlock(&iommu_probe_device_lock); @@ -491,18 +548,15 @@ static void __iommu_group_free_device(struct iommu_group *group, kfree(grp_dev->name); kfree(grp_dev); - dev->iommu_group = NULL; } -/* - * Remove the iommu_group from the struct device. The attached group must be put - * by the caller after releaseing the group->mutex. - */ +/* Remove the iommu_group from the struct device. */ static void __iommu_group_remove_device(struct device *dev) { struct iommu_group *group = dev->iommu_group; struct group_device *device; + mutex_lock(&group->mutex); lockdep_assert_held(&group->mutex); for_each_group_device(group, device) { if (device->dev != dev) @@ -510,44 +564,30 @@ static void __iommu_group_remove_device(struct device *dev) list_del(&device->list); __iommu_group_free_device(group, device); - /* Caller must put iommu_group */ - return; + if (dev->iommu && dev->iommu->iommu_dev) + iommu_deinit_device(dev); + else + dev->iommu_group = NULL; + goto out; } WARN(true, "Corrupted iommu_group device_list"); +out: + mutex_unlock(&group->mutex); + + /* Pairs with the get in iommu_group_add_device() */ + iommu_group_put(group); } static void iommu_release_device(struct device *dev) { struct iommu_group *group = dev->iommu_group; - const struct iommu_ops *ops; if (!dev->iommu || !group) return; iommu_device_unlink(dev->iommu->iommu_dev, dev); - mutex_lock(&group->mutex); __iommu_group_remove_device(dev); - - /* - * release_device() must stop using any attached domain on the device. - * If there are still other devices in the group they are not effected - * by this callback. - * - * The IOMMU driver must set the device to either an identity or - * blocking translation and stop using any domain pointer, as it is - * going to be freed. - */ - ops = dev_iommu_ops(dev); - if (ops->release_device) - ops->release_device(dev); - mutex_unlock(&group->mutex); - - /* Pairs with the get in iommu_group_add_device() */ - iommu_group_put(group); - - module_put(ops->owner); - dev_iommu_free(dev); } static int __init iommu_set_def_domain_type(char *str) @@ -808,10 +848,9 @@ static void iommu_group_release(struct kobject *kobj) ida_free(&iommu_group_ida, group->id); - if (group->default_domain) - iommu_domain_free(group->default_domain); - if (group->blocking_domain) - iommu_domain_free(group->blocking_domain); + /* Domains are free'd by iommu_deinit_device() */ + WARN_ON(group->default_domain); + WARN_ON(group->blocking_domain); kfree(group->name); kfree(group); @@ -1109,12 +1148,7 @@ void iommu_group_remove_device(struct device *dev) dev_info(dev, "Removing from iommu group %d\n", group->id); - mutex_lock(&group->mutex); __iommu_group_remove_device(dev); - mutex_unlock(&group->mutex); - - /* Pairs with the get in iommu_group_add_device() */ - iommu_group_put(group); } EXPORT_SYMBOL_GPL(iommu_group_remove_device); From patchwork Fri May 19 18:42:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 683952 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 EECD0C77B7A for ; Fri, 19 May 2023 18:42:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229965AbjESSml (ORCPT ); Fri, 19 May 2023 14:42:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230120AbjESSmg (ORCPT ); Fri, 19 May 2023 14:42:36 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2067.outbound.protection.outlook.com [40.107.96.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48B66E49 for ; Fri, 19 May 2023 11:42:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GxLn8C1xQqF9MusjRoQo7X/4Ib+BYLUJf+oUQSeQUWAjxvU8/kQciV1Llds9TACgW7F7vOC9gYL3Xy9MsYbJMh7ZE2W9RJCfV6OTutYjTergUWhUVa80JYFPWazbqlLswCRNrcoXBUJ6Mj+PSec2pdZxYk3d84TSNDLIac/q7+pSUETzlZt9ndQs0I1HjXkVhj7wSz6WNHLFlwiXrV3eXIFwAZrwVMQdkwit615H9s34Af4XzwGMsacT2uYxdrCTK2ioJ/gjzslPEgHlLba6eotNBJl8FIKk5HMiYDWd7bL7qsrtynOgnRnm3BGwP9pxO/nqliToGzyRFtkRpGBtOA== 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=712VHjNOOVUFixxgXWW99vsUB1tfz91eMtL0qnr03R4=; b=oQHvvEZ/0ujJZhgoejkKkmqUK5zipw5FuzLBnwJIf0IHvpUFcgIUgUHu3eWtE2MzyExGvFaNoftUXGyvVzUpDT9AB2WzHTdKY6isDBBVK5+u2Jf5jr3/fJZmF/ezOP4WqagBaY7ZHPSlwRtml1H26DVWaQr9CSqD9wdaUCN8id/e2oKOtUesaCYU+ZjqB7nENdnu2dzwGoCvrG+pNqJ/HpsZeKzI8ZRuId7I9FVAYtYIciW3mMxTX0ps+ZCHW+JClup2zSpjUd7m/tLtX/gPpvwlhj3qcO46uorFSgQlSxFqLjhHFIdIUaVJcQnC2FpWuaSPoOd3njJykRIkBgpLJQ== 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=712VHjNOOVUFixxgXWW99vsUB1tfz91eMtL0qnr03R4=; b=goAMQhX5IpKC4exK99Sc5wBEWcfFspNTp5Kli7fOBoIRQVHt54l5hS9G7kQItm9B+1bhv9oasj9KIxTPM1UUKxExYjfQmGiuHelrdst2g9c+mAs1FcRTJga7NYaaYbON1KdiXNT4xudwomknQfGqHFCXlOsUEw3mGjVqJ+CUc0vZZw0KZNoBfDvfxc2lL9JdHjSgrF2asHGbBJuzyDi2hllrx1p49QicBprpiaXSa09DWLvhNXDMOewtcsjIMFNFFzJT08uv/LMk1TgRwtyWYUdfpkr86dzL9QdrGoJDv7R1a1xP7us5e4ODrLGfjLUk+q1BQyOAiyUaOTiOfbmZxA== 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 PH7PR12MB5688.namprd12.prod.outlook.com (2603:10b6:510:130::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.17; Fri, 19 May 2023 18:42:27 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6411.021; Fri, 19 May 2023 18:42:27 +0000 From: Jason Gunthorpe To: Lu Baolu , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH v2 06/10] iommu: Move the iommu driver sysfs setup into iommu_init/deinit_device() Date: Fri, 19 May 2023 15:42:15 -0300 Message-Id: <6-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0354.namprd13.prod.outlook.com (2603:10b6:208:2c6::29) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB5688:EE_ X-MS-Office365-Filtering-Correlation-Id: c4bac90b-93c4-4023-c679-08db5898c7cd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: B8jXa+xmx6K+1BsxP4lqyG2PMlkpRxXOgToxlrVmAGK7flXxuVHs03PPHHjo9HAlv6Kv3QANrXYk2kvkCz8G+xetV0oI474UZtWoF8YJ9tGYf6HEzYzU1347uENPpNeSRJjnrkUjdyWNYZpmxY0wvkrk3xCnVOdE7WnuWApa2Vc7aK3Ioi+7TVNZbry1ioZRw1wyjf0PbcUNgEaW3Re+Xpwcj2XiNh3sgXX8V1gEqAbWjmeN/7GmCfREToqPzICKTAjm/xbFxJ6brrtyrm0hKpbSMYNWqDePLbHwAIHqV1KiYsU2/+OHSS7ZzjZBnd4mqu8dz/w08WhPYUrlFWK1ckRQqesT4QGsnD1s05ccgo5t/QrkxqXBcnbIBeUB/RA6r+TPD6fUWQzVDECNbeQwd7FW72l3XCf5MrrxT1tt5tNYtiIA3VyYn1PN+4/FtMeA0hCjR17cXiIGVQlh3KR1NDedmjSayGIKrVEEPJk5Pva48ztbgRV253nq4vS2cOCdJJPlvPsZSPrUnwxXTvb2zubLs8kgo766Q1yxHbZHyiUb/FfspL6SIRYuvvYOcxqgo7LXYFiqeSsNiw9tEAKKzC9ReGKSz0dC5SQrkbZ6luE= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(346002)(376002)(39860400002)(136003)(366004)(396003)(451199021)(6512007)(6506007)(2906002)(186003)(107886003)(26005)(86362001)(83380400001)(2616005)(36756003)(38100700002)(316002)(41300700001)(6666004)(4326008)(6486002)(66556008)(66476007)(66946007)(110136005)(54906003)(7416002)(5660300002)(478600001)(8676002)(8936002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7HyjBfKFmp/QrTEoCGozRwOcY+i5M9FZYDhwKIGvG57209879w7X/7RfHJa8gX7aqDsfJ7lyZYjFX50BmewC/YrBNfR3qISYNCUSZ+jU3aLHm0vUGQ370Fya21cILaG+Ze1s3ZU1d2PgjgNZJ2Pk6aQxpZkWHIPM7UOT+00HeTLjh2QJdJDNo/LWOiXT8e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v7kJ7Jq7QqBoW4K4f9DethX794OJp/bXf2GQiGDL1y6EmT3WIMV7O6NeyqKTdwo38mlfBw0Yxle5V/m8I1fU5BfCEobpm43JieLD8Ns9yn94uHXG7WlD+O6p8YCMbsTCP2j371AElRTHIzaCyh2yk4u4dNQdBv8l0EuP3YOUfy6p6aWbi2jlAD0EPE/usJKmDKcW1OdTWWr9cl9P4IqrLiplD7oNU20QUkImniqB+uuE7obQiTGyO18DPH7snNtqnO9tled2yA6aeCaE3FFMHB8X4OgBSkzdNH+QOXmStUXS6uvyRdJDTh0ANcY9iHmsJ8RlFSWSAdQ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c4bac90b-93c4-4023-c679-08db5898c7cd X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2023 18:42:21.5155 (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: FWASQLtxX8Ft0238blhe2BoEpsV6zfK4rBV9SNmaIxBNKbNdIqPhzaKMn9s3+bUh X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5688 Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org It makes logical sense that once the driver is attached to the device the sysfs links appear, even if we haven't fully created the group_device or attached the device to a domain. Fix the missing error handling on sysfs creation since iommu_init_device() can trivially handle this. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe Reviewed-by: Lu Baolu --- drivers/iommu/iommu-sysfs.c | 6 ------ drivers/iommu/iommu.c | 13 +++++++++---- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/drivers/iommu/iommu-sysfs.c b/drivers/iommu/iommu-sysfs.c index 99869217fbec7d..c8aba0e2a30d70 100644 --- a/drivers/iommu/iommu-sysfs.c +++ b/drivers/iommu/iommu-sysfs.c @@ -107,9 +107,6 @@ int iommu_device_link(struct iommu_device *iommu, struct device *link) { int ret; - if (!iommu || IS_ERR(iommu)) - return -ENODEV; - ret = sysfs_add_link_to_group(&iommu->dev->kobj, "devices", &link->kobj, dev_name(link)); if (ret) @@ -126,9 +123,6 @@ EXPORT_SYMBOL_GPL(iommu_device_link); void iommu_device_unlink(struct iommu_device *iommu, struct device *link) { - if (!iommu || IS_ERR(iommu)) - return; - sysfs_remove_link(&link->kobj, "iommu"); sysfs_remove_link_from_group(&iommu->dev->kobj, "devices", dev_name(link)); } diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 2031cb4782b9b5..69e4227bb7404f 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -356,12 +356,16 @@ static int iommu_init_device(struct device *dev, const struct iommu_ops *ops) goto err_module_put; } + ret = iommu_device_link(iommu_dev, dev); + if (ret) + goto err_release; + group = ops->device_group(dev); if (WARN_ON_ONCE(group == NULL)) group = ERR_PTR(-EINVAL); if (IS_ERR(group)) { ret = PTR_ERR(group); - goto err_release; + goto err_unlink; } dev->iommu_group = group; @@ -371,6 +375,8 @@ static int iommu_init_device(struct device *dev, const struct iommu_ops *ops) dev->iommu->attach_deferred = ops->is_attach_deferred(dev); return 0; +err_unlink: + iommu_device_unlink(iommu_dev, dev); err_release: if (ops->release_device) ops->release_device(dev); @@ -388,6 +394,8 @@ static void iommu_deinit_device(struct device *dev) lockdep_assert_held(&group->mutex); + iommu_device_unlink(dev->iommu->iommu_dev, dev); + /* * release_device() must stop using any attached domain on the device. * If there are still other devices in the group they are not effected @@ -462,7 +470,6 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list iommu_group_put(group); mutex_unlock(&iommu_probe_device_lock); - iommu_device_link(dev->iommu->iommu_dev, dev); return 0; @@ -585,8 +592,6 @@ static void iommu_release_device(struct device *dev) if (!dev->iommu || !group) return; - iommu_device_unlink(dev->iommu->iommu_dev, dev); - __iommu_group_remove_device(dev); } From patchwork Fri May 19 18:42:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 683957 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 67767C77B7A for ; Fri, 19 May 2023 18:42:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229887AbjESSmZ (ORCPT ); Fri, 19 May 2023 14:42:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229568AbjESSmY (ORCPT ); Fri, 19 May 2023 14:42:24 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2067.outbound.protection.outlook.com [40.107.96.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A60BA1A8 for ; Fri, 19 May 2023 11:42:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=l3rhMeZ59PPlLfbj0zidQxwAZ8EwAkAmQVv+K/ToltWBaoRnWk9QHfdS4vDhaw5eTMmvAePciTXqURfIED48YiVfDAM0q23hI/E2V/OkTrOEUY0CtM/5wO296ykJFzx7D+GZCWl/NkXh2Et3hKc0+q1dO9aop/b/40nWx6UnXXNANAAyYx68jxoBSIL6cvVXVORXi5RhoWkkLj8tHARpmM//MjQBPe6HKTslIqBe2c3288DctXP+sTdK0t/3k8LNN44JOfNhshu5TxKVS/2Ah/46c2Ch6zTVkbpEpW2HUMExh9f/ckIT05od0lFjpV7zVWxQwiIAs3kBz+BtCpD+wQ== 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=fKZN4z1vpGNiEpyjDYvozAQgaDB03JAE18GciEB5yOk=; b=L7o91UUA0j/TBMZCyucoaqf02ixfjG+8zJYZHcFQpiuV94wt5NYAmsMNXxJo9qyxHwxsupbxeC1JSRJ/+pp5KYYe2RVW6U6T9k3AEFKI5ba1sFIRzI/AvCKbn7lJFUF3m3nsrUZCsEULbL3kQmfCQwCKPCqknO+flHsSNTEClzND6tfmXq9Gy9nSXYeVweKaXUhuUKzMAtYMCYWAgiXkazrX3La8R8o8cV3iKCcUAy9u3G7f0URwQJPK9MRnOQzKpXUwSxQPVpv5YPrwaDoHNQh7zEH/TwIhwjr5TVPF4DLyRxjTvPTDAKHIaN9SFH6Tc3nirwQ6rwdftJHt+aLEMQ== 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=fKZN4z1vpGNiEpyjDYvozAQgaDB03JAE18GciEB5yOk=; b=fg9SGBISXTET5qrUgOOHyf4/RRWYGCA+0IOdNlVEbl1F4PU3Fr4cNXV31kJe/p5nkl6LPI4xV6+LkRfgrIO8r9GOZK/gz8Ut4n2/LJNM4toUr0sVHCVRz4f6k1qrsN2v3hYWCYAmlNmgdEFb7vSusoj+aHTK5p2umdeYW83GJaEiDu6LldcxHHKRqoXbP7TqqfBlxh7H21cgA8ul6PFH9VKapyl5mSPzuv9+jqMu/uBunDUonnT/AMcyc39TKB+vaTg3iApsr4ipEKmZ6m9hdIwqU3dYK21KGlXXOfLRhKGjCUM+Q6ur1PogOkozeRkIZ0M8u3EA28N1iGgkkkM9tg== 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 PH7PR12MB5688.namprd12.prod.outlook.com (2603:10b6:510:130::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.17; Fri, 19 May 2023 18:42:20 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6411.021; Fri, 19 May 2023 18:42:20 +0000 From: Jason Gunthorpe To: Lu Baolu , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH v2 07/10] iommu: Do not export iommu_device_link/unlink() Date: Fri, 19 May 2023 15:42:16 -0300 Message-Id: <7-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR0102CA0050.prod.exchangelabs.com (2603:10b6:208:25::27) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB5688:EE_ X-MS-Office365-Filtering-Correlation-Id: f37f23bb-e0c4-4376-792b-08db5898c6f2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PTjZPOUCEXGqN7OARQRN9KfdSQk0Gxf5apM8jR3BdoKXvtflixvQ3P+D1WzV+1P3pRv/wu+k9rIPaQo/1mpvwr4mejLRzT0wTwIVg9W30vIBGkqISMGj3PPnmMg5WASnkpwSjTtRBheFXLAb6bS25gLMrla7o/jqP/7ISjLkJRsxg/fxR27hOM2YE3MlTa/nBs38xwHlpCy7hplaLgpE4eg8QCBmaJDHQZ5nTz93UAVb9DTf4WI5RTFQUZExLjFL2C+YgKDeFvUBhIlFBLEEmGg7RcLJRwE7xenEYjfymGXdTOXoGSgf4cTan3AwdMWyx/8gJuEM6FwCLX4DdpJKNEWfhDMWaOWB7Y0AA+TRrBs+tLQSQUW0hwkq4cAU1FzFwePUoslrhBkDT7KTKYytwqsHZIwdIyT9raS6sGg2bXRQna4S2/ltYCMrYOcGoRU5+8pBRfWgcOAgrvyO5/esW9DzLpIZtFrKebqIIhkZAzpR7v2jF7W83NspC0eseFBHpGCftrODgreq56q2tLjV0lPqSGryxp5maHEdr3WZIfkPJjElDaGewZHsUUSOUyViksayW3ZB2jSVmkjHSoz5ocTVCTQPWy/RWyg7YoOIUkw= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(346002)(376002)(39860400002)(136003)(366004)(396003)(451199021)(6512007)(6506007)(2906002)(186003)(4744005)(107886003)(26005)(86362001)(83380400001)(2616005)(36756003)(38100700002)(316002)(41300700001)(6666004)(4326008)(6486002)(66556008)(66476007)(66946007)(110136005)(54906003)(7416002)(5660300002)(478600001)(8676002)(8936002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 027WqbhUAic9c8kzAclO2gX8iqHOVL+ewNtwVQlfuAThiVYiE1NlRigcHI0lrK648J7LdTIv91cZXaA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bJ0dQD5JGrEMnS01Vt5X2iQLP9S7Vni+mEdosxw7xAUTmpIqNSSWxqNHdYarAAqzLq3i4QLEdqP60bVpsdAGgmnERjhQfKQMgUI+/j3/L/kNxNp6UCZ0rExP9S7VZmNWuLzSCRI743FTkHTsftWcbIandE1A9LBeS3dSEidxyBhOJcdK6AzGtJ78nOuZyWUqjALfhv4XM0VAgm2GPLmV945HncE060et1BZxsT5oO7ZkYSgjBh4YI5yat5N1sj9T64g/yoVgX98m/0OImYJyt5nvtGjzyxFCG4NpY+4RjFMJC4e6KGGQrYgDpUqrAmkLgLLSnMwJhVcOrVs4K2Y261b1KtYVMDHkqRg9Q1T7WWAgFBJ6aGWguWl+4v2mfpgu+A9m7gExfDY+NgcjX9T/xSaDRXuzdSmuL6v/HSO97rj6aMcsgtz0pV+l X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: f37f23bb-e0c4-4376-792b-08db5898c6f2 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2023 18:42:20.0716 (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: AJI8A6V220LihiUiQ2HRXEs7i/jz3tkhYCs8tCiOfYrid3JuvZhQsU/HkJumoDjo X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5688 Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org These are not used outside iommu.c, they should not be available to modular code. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe Reviewed-by: Lu Baolu --- drivers/iommu/iommu-sysfs.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/drivers/iommu/iommu-sysfs.c b/drivers/iommu/iommu-sysfs.c index c8aba0e2a30d70..cbe378c34ba3eb 100644 --- a/drivers/iommu/iommu-sysfs.c +++ b/drivers/iommu/iommu-sysfs.c @@ -119,11 +119,9 @@ int iommu_device_link(struct iommu_device *iommu, struct device *link) return ret; } -EXPORT_SYMBOL_GPL(iommu_device_link); void iommu_device_unlink(struct iommu_device *iommu, struct device *link) { sysfs_remove_link(&link->kobj, "iommu"); sysfs_remove_link_from_group(&iommu->dev->kobj, "devices", dev_name(link)); } -EXPORT_SYMBOL_GPL(iommu_device_unlink); From patchwork Fri May 19 18:42:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 683954 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 347F0C77B7A for ; Fri, 19 May 2023 18:42:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229650AbjESSmh (ORCPT ); Fri, 19 May 2023 14:42:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229875AbjESSm2 (ORCPT ); Fri, 19 May 2023 14:42:28 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2067.outbound.protection.outlook.com [40.107.96.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8FE971A8 for ; Fri, 19 May 2023 11:42:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=i1P08YbrmpNxnRdrQndb8YNNHq8wfMno+MQqtPhsi3BVm+WCE/I5dkUGRDSrqYbcmzn8j8iirZJIld9PE7t5iaFwuykW1RBD6ID2et0RLSoG0wy0MVqLO1vLE3LcgpWu6oKMF4rQqecO+cAMPV2kpfKZfbjDUPVf40tvYpxW62biKGmZweuYWnEqabwMZborVYeYRNQyoliz6FuWv02evIp+903RX9D7sffoJyYjssVymzT9Habc/dp5JBRQhsyYVf7fvUeOq8sxgJp0SiRSv6nmBRJZpo3/eRdiPWCqef/26e1w4XiFRTYeKi4NbBmW8T9spzc1GVYOfyIEgnHcNQ== 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=T5sK9T7mR+u37FgOSN9zxH5lJAsl+6bhGCeOIHAvo6k=; b=mLN+LrJosFYo2T4WOrdYRRkv1EceFg42FG1EeOaqgbGEd6YagQVKQWQ1EG+xt9GGFPJxW3ysizXYvfsCQQf5w7rv8c87DgI7AXZ8VcoVFQJMAXuvj961Pctxwxuq4Wy8NKOyv6uO12COiUlb1CCjsC/hVN9ajneSWg82nci2klJnKyjDBYyg0KcqK1zXd0V5MN7Xslz3Go+IPHhDSHui/bbqP3YUxjww2yeOxKAt7B8w0/e6mL8xnqMmstz2OViZaoESLZ1W/OtYlDUGcBUCuspLeX7zd/3GBk2ADacSv0KWn4nSfShfQjZxRspJzHn8ag1UWnr6aDENe0gXBk+1LQ== 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=T5sK9T7mR+u37FgOSN9zxH5lJAsl+6bhGCeOIHAvo6k=; b=KsvGCEvzfMT+Nu2Jg1TEqZOdV6RQpEVIeuJDk35q+YQhAcH0TwgZvyoxogEXWmgjVnquwDkgpZcizU02TRr6HD+AU8DXq/m+Om3NK2G9Z1zt1os7Rn6o7J2BBOMzIS0nZzUNEqm+gIqQunQcuuJbuMjM4caW8Xhq4FQ2VKc8pIzq1x0qtVa0LCh9Uwo5TolSWFLCcGy1e34GN3zDQ09JOLH9QC306OwLj+DaNso0Y7biRAvIYN5JnSrRI1Bv0Z353InGDkRkqtuE2RHNonoHOoCwYPEe4S7jhKAgNnF0hpkD2sT7kGkc045Qtwwq6Xz24b5lQZXwZo+VNG8Tm+IhDQ== 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 PH7PR12MB5688.namprd12.prod.outlook.com (2603:10b6:510:130::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.17; Fri, 19 May 2023 18:42:24 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6411.021; Fri, 19 May 2023 18:42:24 +0000 From: Jason Gunthorpe To: Lu Baolu , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH v2 08/10] iommu: Always destroy the iommu_group during iommu_release_device() Date: Fri, 19 May 2023 15:42:17 -0300 Message-Id: <8-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0341.namprd13.prod.outlook.com (2603:10b6:208:2c6::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_|PH7PR12MB5688:EE_ X-MS-Office365-Filtering-Correlation-Id: 4fdc39ac-84c3-4211-cc6c-08db5898c78f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: XQzUP0+pOQoJEZhxEF7hpGPzAc4ux4C4e1YsACw0RlJo0OSUD6SpEA2LEalERvB7yGNQeXGrhr7hXRtiSx0d1WK8bS9Gkp6LysQPyD5QvHuSUjQS5oTmRM4LJPvqlg8VVa6O3dLFOf6+rS1C5PEZLWOaE5pxsDyY+Q+3/Y2mgagaPjVTJJkEA/fY3H3WJ98ggxeghFU+ICtZ7oodhnm35tVdZePwLYFk3EsGyIVbTQSdJiXQVQd2Pwu/szIHj4hbZaJcJgoaKT75hZ5fJXp/npnWPq7pt8NwzFy17hExjo/uR66si4VrMRkZzPpzQ+GPcfU2b0KjW6FTxjPM0Hwu+SZcoehc1x567L02MWyUz1HTR4pTPoPPtxVCly/NhTLejMx45WsvGAZRpZJYs8bcCfupKTZz4S/0hLvBHMJS1PqDSQP1bmMPL9iz72M9x+rTgIpSveTofkk9Vsx+r0G0Y72+f0+3BIVDPIA6wQassL99N5736GzWUx7uiIVoYNNcHt4MQmpQ8ehtky/y8ap+9HeLXFfKOM2fm8dq1QxFC6zg5raie63/qDeZRHBxatXxg/SWuYW1vhiPRlh6TkWlsc8lTwylXETuHqkoftrQbas= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(346002)(376002)(39860400002)(136003)(366004)(396003)(451199021)(6512007)(6506007)(2906002)(186003)(107886003)(26005)(86362001)(83380400001)(2616005)(36756003)(38100700002)(316002)(41300700001)(6666004)(4326008)(6486002)(66556008)(66476007)(66946007)(110136005)(54906003)(7416002)(5660300002)(478600001)(8676002)(8936002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ugLYxoKtvwomSLvbUAhQY7ygVELZ6HiESEOyRazqmC2/4Ao38ayG9otHr4pfBcP5nYCADzi+3eVyg65SZNe36f/fRj4jqBXlLMfuJfGHBIIo7LdC/9H30oCr/Qdk+dpNZSmJxpkAvEj58AmftV471B5Xp75s7PvhlaoE13iMtbyUVglf06vOEkfVY+zZnlkzmQHR9syiOHo+GxfkWf2dgQjtEoarFSuGqAoRFOWwQ/Y9aeM/GUXFXDnkCSOKy9/Y5y5sf7I1EqYkqNLmSwV4FwitMnM5PgEva4wCvPvIo7UMf9OMhlnQ4op7/5CnWPLPT/Csq4mGB5YWhn3kyUcOPxek9gwusHThlYbW1vZuLEwXP8llHGH9xir//UGcP8BH7WGUA1NTrvxu+mrqsz2tvOeUmAPGEeDlxY/lgNVXxPzld3h2bQ6hSIwebHYvSbntn5u9BvQbMtjYtZ4Ma7At56Rg1161FUNGn0Vrdp9ROARtvDJqXB3MbxJuZ3Rp49Wxh2Pi3kRpPNqM5JQlylwAM29n+ALkISh8sYi9yDPxTbjVyWYd3JyQaTK5B5pNWtxkrH0S6tDkc/wziHsWgQtIHTj5VDnqO8gfdGuat+mX6fhkKRzrIrOlAa6FUH3BLVTXzk3WTZdBOMRFKMkUQ9ZSlwGYqtgVeE7moLMGcTlkkvSlMa6lbGO9KJGR3e11aDbSteam/SbAI895VaV4YDc+ti/aLNRDs0RAkEIyd6UyJzVt0A5kms12uMO9+/E7FmenFVNIKdm/zDuc4jtNbKw2n71k8Ayiyjhn0lGsL8wKqZcFwQx6UqI4OtwQToCl/b/xCiLoaZtWFfmx+xiLImL4RzeSEDxsX+lTn6x2iDU7PGfbTPZsDtMjRcweUXSexA39HtQuiaaRES3Pki6U8vNbWA9gsmyhKibWClIa3/kFtt+C8mNuFh1d97rSNqhmO3P3GBa88xeJ21qEWlWkNBWhKd98wunBBn/Kckqz3BW/ihHOF2Ki9OCizYh1wyArbS+fvlIqx0mFr1D6sZI/ywf+lYIwQcvX6M8dr5F1hNqerxHmknUL7xSryEZa6+KD89OXIXF25uVJ1DclJA+10apfURTp5ymIBubtJLR3LKRkWK3BQhXVdUPj2izG21JRQt3WuF5SWP6t+tbPtb0YH21d/YbtTvZoB2NHrp6CHBAnioLLKvZd8npHUC793kZ5GUSno8FIQrTQKlkDrc3B1GtCLdbHW808Ms1iihRC7TO3LFomIrcqO8oqEML+6oWIDb97FKrRMzinEBzRMIXqzLyR0XXwpD/NavE11ivhw4gxHWqGOY2VrGN8KnWWUZwP033KqxIaHG3qjugqkreyVuLboRmS9qkcgrIYHMovBmQisTPDoDEziIPRwT/l3gF1a7M6qEZGObyvdz+O/4J/rycSX5I9uSUgHkcZmxb3mF8qn5OEtfbkqimWTMUkpk8uZrxIw8bzn1ZBaZ2dXBdHrrISJCr2in20NS3vVsLRdBIrlPpyvRWr7SucgzMvZ3GBmWkT9Cs1snKQw4dPYJ/giecUbQYN1e3EiELsfwYOhS861xaTCmk2iIG8d6tr5duUWIGP X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4fdc39ac-84c3-4211-cc6c-08db5898c78f X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2023 18:42:21.0787 (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: s48bNV2MMp7TRA+0xkdzBwhrB44YRsGTvx1vcvC8+NNeluvtBJXY1wDsIQGV1kGV X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5688 Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org Have release fully clean up the iommu related parts of the struct device, no matter what state they are in. Split the logic so that the three things owned by the iommu core are always cleaned up: - Any attached iommu_group - Any allocated dev->iommu and its contents including a fwsepc - Any attached driver via a struct group_device This fixes a minor bug where a fwspec created without an iommu_group being probed would not be freed. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe Reviewed-by: Lu Baolu --- drivers/iommu/iommu.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 69e4227bb7404f..6a8cbfd2274770 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -575,10 +575,8 @@ static void __iommu_group_remove_device(struct device *dev) iommu_deinit_device(dev); else dev->iommu_group = NULL; - goto out; + break; } - WARN(true, "Corrupted iommu_group device_list"); -out: mutex_unlock(&group->mutex); /* Pairs with the get in iommu_group_add_device() */ @@ -589,10 +587,12 @@ static void iommu_release_device(struct device *dev) { struct iommu_group *group = dev->iommu_group; - if (!dev->iommu || !group) - return; + if (group) + __iommu_group_remove_device(dev); - __iommu_group_remove_device(dev); + /* Free any fwspec if no iommu_driver was ever attached */ + if (dev->iommu) + dev_iommu_free(dev); } static int __init iommu_set_def_domain_type(char *str) From patchwork Fri May 19 18:42:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 684745 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 000CAC7EE29 for ; Fri, 19 May 2023 18:42:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229933AbjESSmh (ORCPT ); Fri, 19 May 2023 14:42:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229964AbjESSm1 (ORCPT ); Fri, 19 May 2023 14:42:27 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2067.outbound.protection.outlook.com [40.107.96.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47B11E42 for ; Fri, 19 May 2023 11:42:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MFDGpQhziTcVYF46RXd3Kskk9jHW3YiLz7y7g3c49sWyXXz7Mnj2f/kIpP79qKCkC4tKxTgBY9cRlzI7GZhUQBn/IPFw54NvXUl1ov5xmCDA9Itx4KgJ13J8GGivWWLkXD7qRhwYOJy3PFWyvsAG7e2R1Pg7H/TX3AP0+T4OUHt9jemDtr/HNi/lXnsh3LoXj/g9//40zvFj+1hWMa/vQM+cD8fAItcUkJdQtN5QW9kTQyCBmwB1zO7MkyS/A88jSiL1w7dxP0oeiJr3wdPdO8EweLUTzsZiE28xqED5mWS++sFBVMZLyLTb5o86vLbB8WSGlbv5L+Ty16YRNvpzLg== 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=oLLlAMqHhR7iIWaLBnWfT09kAEVZXH20lfKKe1NUVHU=; b=STnwuimcXn7DXXpsDg42CN1vrdy0LD97AFTjOvoXu5X16Uy8FaSE/8tnotrOgrXOjzLYhm7nxrVMezrE0z4i4ip26aiF81Sp1ZZ1i4QtziXn8GwUKtyubYLJ0VbnbJxWfarsIVQ5BMG9TNh7d6bkkTRar/Na1B+ESqESkbD7Rq6dycm98r03j6cicDjVrBUZVpiOA4UYJqhpe2VxUMW5MLBTp06uAlxRArDqugvo4UDcnL88p0YbLJgebwBuqh2hqp4pebWXpuw7pqv0p7j+pNr+WAk1R82WH/aP0JazJ2i7cODCfNm+jSUQOQA1WMzSQhztf1wWu3PYLbeIUpGwRQ== 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=oLLlAMqHhR7iIWaLBnWfT09kAEVZXH20lfKKe1NUVHU=; b=TjR/WdHdVNk7QdVRGSyuU3Lu5uJ2TpxDSaXGWcEKfR6XsGbaakF955wpxy8ZzZg/uLEeMchtM0Vuy6GjX6mNiS68gJAhF8ab3AvahddlKPvCjrE2sMc3ApE3l6bO+WY62vVXZedaCf/va9CXFbsmJSm39D8Bsztc0n3YziO/9CgghPs0pdHDibjAVhyN6T1UQn+a+CErOgX26vmWG6NkbSXrGM+3F4bXfd10SJuDiwIpKctHTZX1B78WLubBkUoirFshY0566rsFB9rYBiIkTUiIQXWBPhShhMeFOG9+HSeGK/Le1YWEI5H8tcRK81v2KYZT+U2CithBosvY6wROGA== 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 PH7PR12MB5688.namprd12.prod.outlook.com (2603:10b6:510:130::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.17; Fri, 19 May 2023 18:42:23 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6411.021; Fri, 19 May 2023 18:42:23 +0000 From: Jason Gunthorpe To: Lu Baolu , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH v2 09/10] iommu: Split iommu_group_add_device() Date: Fri, 19 May 2023 15:42:18 -0300 Message-Id: <9-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0340.namprd13.prod.outlook.com (2603:10b6:208:2c6::15) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB5688:EE_ X-MS-Office365-Filtering-Correlation-Id: dc5fb4d4-584b-427f-e74b-08db5898c745 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: pOcLTqLZRBfF1CwpQNuKJQYNtS19dmzq5NmJdOQpNBF61vXT0Cjw0SkzwpiRKUvYtZdN0s47IoquZcLBi3GIlczcsMNMnMIYUe3Qy3cL/29OdSDp/aXvCVRy18i8IyDRc4xa7qfUO758UBTPXgkeSMs2A/QPKUbwZ9/d8v0zZjMMfuTxnbH7qAiFxfim6TtpCEd1xkEGrwFXID4W6tjAYcZqO15PRHVgV5WeaQoy1pcPZAII6KKbCGQZZEKZO6IVp1TvjSgMLiZb0zOYAO+rSZ9/jyw9X6uNVnFrFE2KIB36WdQ0QutnOnds4l+hAaSN7yFq8mFYH6QJU96Xt1qbo8AQ1f3qwmz/UpM0O63NvkY5nLyrw4u4xb6GVksaqcWg0T12EdczcTbQosVPsRN3DvSvMzu6e0TynZoWNBmCk2e3dhTv09TeFI+H9ogII2L3AiwcMp3ngh2YYeYDRMmtxAfJzCaKbo5Oujr6dvlTJHMvit+bU2HYAa9uUEux1usWYXAKxoKSd1NKbwkU8ddZ+43ojGqOscKc0r8R5nQ1soHEHE+/GYLruNixjpafQ1ys9OgmLy1c6NtoKgtymfFBEfm2cnq4eGa6Ks7oANbe4yA= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(346002)(376002)(39860400002)(136003)(366004)(396003)(451199021)(6512007)(6506007)(2906002)(186003)(107886003)(26005)(86362001)(83380400001)(2616005)(36756003)(38100700002)(316002)(41300700001)(6666004)(4326008)(6486002)(66556008)(66476007)(66946007)(110136005)(54906003)(7416002)(5660300002)(478600001)(8676002)(8936002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: c0IAfaEJbIIuwjXlKcmsWnX3HWZw+R5vlqvBAiXenl7ePTcnVPqedI3zSAGX/mx4roi3nQnTqDXifCrxfIEm/oPqlZsvWMrv0vrOWHlPpGj6a6J570TvNZCgtVpEBVeNtPWQqM7ZoZ2o1XVlwh1McVPg7hYK3NTLxqEYh6fblXPBaAu/hrgahvVttax962Xg0UVyvvD9T16ti2WizkSnelVkkh6MnWReVahaSbZksoEWhS2HYyEw8gnHhEwZy1s1VrpOrhytiGbqto9/OiDJvb5kO9znY5oero6b1O6Dmdp8OnKHZrKdug2ku4bxmyAtktFU+ijfCUrgoQ6HrwwAkUMq6lf+6oJk6WAKiik8sK7zo27KDO089MVqyFSUUYLG2FEIcKQP3JyVUToake09pDmT0Bpi3jqTKBo1NqOZxVrur09IFMnMjYNfx7JB2oXt5SZh3ho33dx+X7ubSHa4waLwSWG+MsUdZDW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7pTaxkIRluPljwSx+DGrc+aMcgzxmBZ5KJ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: dc5fb4d4-584b-427f-e74b-08db5898c745 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2023 18:42:20.5760 (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: XbxXCQNDL2STiSavQ1pCPaEbP5BlPldJWEDTE0oTTa+66/lNW9UB/S7dbc6pU+wT X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5688 Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org Move the list_add_tail() for the group_device into the critical region that immediately follows in __iommu_probe_device(). This avoids one case of unlocking and immediately re-locking the group->mutex. Consistently make the caller responsible for setting dev->iommu_group, prior patches moved this into iommu_init_device(), make the no-driver path do this in iommu_group_add_device(). This completes making __iommu_group_free_device() and iommu_group_alloc_device() into pair'd functions. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/iommu/iommu.c | 66 ++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 6a8cbfd2274770..0d376a3dbd7a42 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -129,6 +129,8 @@ static int iommu_create_device_direct_mappings(struct iommu_domain *domain, struct device *dev); static ssize_t iommu_group_store_type(struct iommu_group *group, const char *buf, size_t count); +static struct group_device *iommu_group_alloc_device(struct iommu_group *group, + struct device *dev); #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \ struct iommu_group_attribute iommu_group_attr_##_name = \ @@ -435,6 +437,7 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list const struct iommu_ops *ops = dev->bus->iommu_ops; struct iommu_group *group; static DEFINE_MUTEX(iommu_probe_device_lock); + struct group_device *gdev; int ret; if (!ops) @@ -459,16 +462,17 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list goto out_unlock; group = dev->iommu_group; - ret = iommu_group_add_device(group, dev); + gdev = iommu_group_alloc_device(group, dev); mutex_lock(&group->mutex); - if (ret) + if (IS_ERR(gdev)) { + ret = PTR_ERR(gdev); goto err_put_group; + } + list_add_tail(&gdev->list, &group->devices); if (group_list && !group->default_domain && list_empty(&group->entry)) list_add_tail(&group->entry, group_list); mutex_unlock(&group->mutex); - iommu_group_put(group); - mutex_unlock(&iommu_probe_device_lock); return 0; @@ -579,7 +583,10 @@ static void __iommu_group_remove_device(struct device *dev) } mutex_unlock(&group->mutex); - /* Pairs with the get in iommu_group_add_device() */ + /* + * Pairs with the get in iommu_init_device() or + * iommu_group_add_device() + */ iommu_group_put(group); } @@ -1068,22 +1075,16 @@ static int iommu_create_device_direct_mappings(struct iommu_domain *domain, return ret; } -/** - * iommu_group_add_device - add a device to an iommu group - * @group: the group into which to add the device (reference should be held) - * @dev: the device - * - * This function is called by an iommu driver to add a device into a - * group. Adding a device increments the group reference count. - */ -int iommu_group_add_device(struct iommu_group *group, struct device *dev) +/* This is undone by __iommu_group_free_device() */ +static struct group_device *iommu_group_alloc_device(struct iommu_group *group, + struct device *dev) { int ret, i = 0; struct group_device *device; device = kzalloc(sizeof(*device), GFP_KERNEL); if (!device) - return -ENOMEM; + return ERR_PTR(-ENOMEM); device->dev = dev; @@ -1114,17 +1115,11 @@ int iommu_group_add_device(struct iommu_group *group, struct device *dev) goto err_free_name; } - iommu_group_ref_get(group); - dev->iommu_group = group; - - mutex_lock(&group->mutex); - list_add_tail(&device->list, &group->devices); - mutex_unlock(&group->mutex); trace_add_device_to_group(group->id, dev); dev_info(dev, "Adding to iommu group %d\n", group->id); - return 0; + return device; err_free_name: kfree(device->name); @@ -1133,7 +1128,32 @@ int iommu_group_add_device(struct iommu_group *group, struct device *dev) err_free_device: kfree(device); dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret); - return ret; + return ERR_PTR(ret); +} + +/** + * iommu_group_add_device - add a device to an iommu group + * @group: the group into which to add the device (reference should be held) + * @dev: the device + * + * This function is called by an iommu driver to add a device into a + * group. Adding a device increments the group reference count. + */ +int iommu_group_add_device(struct iommu_group *group, struct device *dev) +{ + struct group_device *gdev; + + gdev = iommu_group_alloc_device(group, dev); + if (IS_ERR(gdev)) + return PTR_ERR(gdev); + + iommu_group_ref_get(group); + dev->iommu_group = group; + + mutex_lock(&group->mutex); + list_add_tail(&gdev->list, &group->devices); + mutex_unlock(&group->mutex); + return 0; } EXPORT_SYMBOL_GPL(iommu_group_add_device); From patchwork Fri May 19 18:42:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 684744 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 AF66DC7EE2A for ; Fri, 19 May 2023 18:42:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229875AbjESSmi (ORCPT ); Fri, 19 May 2023 14:42:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229568AbjESSm3 (ORCPT ); Fri, 19 May 2023 14:42:29 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2067.outbound.protection.outlook.com [40.107.96.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79ECCE43 for ; Fri, 19 May 2023 11:42:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lE29d6Z6f8g1yn3ZLZO/LgIpbMTyMJk33cb6QlT+d2GkNL85ssgMUWpJdtQJdW4qR0VsGSifx42RKyTdI5F+Y5TgfXc8ZFbd1rng8R/u0BLZh7jwBIgwv3hHA3weOvlkdzhIsSLznHb7olSflVJARkamMbRT17H4gaKHPn02KGRczzjbBDAanOcFJJAQAGRMDPceSbe56HzM9Dyh2CRQQdDIhSsXA6+TqwnKQBdvIAQCSjrpnpEk86kI17ig8oWynUn0w2I5QQAN8ZkKoF2FF/cfENGRYbPJ4xd3Gu6FZU+9BuCU+4pXx1WUnM+vRVz8arUZntG9OOyDBYGyafOOmg== 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=g+Bq9zNXxrmJODoOeGaxt1lJSZMW0NsMqo/oobnZ6JI=; b=Pc7EW9Fuz3U0PW20ZXc7uKrSc7yyXHPMt/NDFw/uAEKGJX5gRX37/H5AZBVMqIsPjDk5iBpMCzy+HfOdVGzRbA+H3tPOstqGJ4LwJ2GuUCz5Gy+YL2tl0dsmacIRlbZUaMu/BoBUtBTRQvxEoKHxa32kOaNweeQ6xrLZ5lERkbxYZec5B6l8i2CWPxwezD9OX++rJKat9UNWGVoOfeM0Vj5JUmCVgYSb/buM6sdDUrDR90NDTaldqJe5jf5gVvBxQ2EDwRVLjpOSIezBseoOxewm3Znb+6H1715REM8gSV7WAPgaimHuqILM/Nz+8IJa6etqXT82I6IjQT6Az6eiVg== 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=g+Bq9zNXxrmJODoOeGaxt1lJSZMW0NsMqo/oobnZ6JI=; b=iZYPHgJgiP16hWEezonhtEtEZdHAlqrvyb2RIStNV/ZY4WFYT6mPdbiYiQMjQNy0ijx7G9K/DpiOFS6H5BImwkRHAPYWnidZn42wpDmMtcxw+bev+4jbDvDfGw/fDdb5HWnXoTXKGu9ijkZ34j6u4kEcAtOGIP/0p4e8n/5ohXKDBUyluvvHB07v7tCiF+R1AZAEixfIy2RZgW4QGhOTNKqNybfZ2xRFzPwT7ndguGDZ1+P/tAUlfXw7Lc87s8tWGwzVQ3rjMeDa3dGGLXj9Jko7mNcmPpBLGe+WXykzzeebyQoz4mwffgFNU7qzoAkBq2hiefYMy8VUfvAqNA9ciw== 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 PH7PR12MB5688.namprd12.prod.outlook.com (2603:10b6:510:130::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.17; Fri, 19 May 2023 18:42:24 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6411.021; Fri, 19 May 2023 18:42:24 +0000 From: Jason Gunthorpe To: Lu Baolu , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH v2 10/10] iommu: Avoid locking/unlocking for iommu_probe_device() Date: Fri, 19 May 2023 15:42:19 -0300 Message-Id: <10-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v2-3c3bb7aa6e48+1916b-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR06CA0010.namprd06.prod.outlook.com (2603:10b6:208:23d::15) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB5688:EE_ X-MS-Office365-Filtering-Correlation-Id: ec6d354e-42ce-497e-f321-08db5898c796 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: J0IUFTH0K8nR+3M9RtPA8Cpjq/uplvLafmaFPisVAligmab3KovqsA7j2VMKkGbamphiGAwAhscNMnue38w2CtYDgpqa+f+sc75SER8llHEMY27YxABrrXueNhj9tJVcrm2uXd7geaoS6TXv0h7TxIBqxrlU/QRVYAIp8TKDt17hnL9pV31Y64BOQpXCSz01lfE+n7PNuvZsEn/Xu1sYdKLw+f7EjtuXZkqaj1h1/dT6oWzKAc6rA9vmlEv49xXr4mvfQkPLAWCYwWsHaADL990xHoZ2iQQx0pTSJnQzQGtNKcK/A5FT9HRxnChfROjnvFWhtTjhN6TwR76eFxduohYzvVmvUS6HBplbO1K3LhMJMF5HImqfLArDC/4sWB5sBOiuuSvUq7GnDegz+nC8ILNY1JARRLwwjT0g2B5DljC5imwXRdL5OuR5UDexPlLsL/ZdYpQnbKB19Dcul7LA9+SHUGOv2yetJTTsAuKX8Vhv8Z5t8C21j4X2TewbwzzUiad/TdvUMTFawntw10z6c0+cbA06y6HYBQKGgKFGHg/faMiCWBQRWRsgHtHfjKdV X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(346002)(376002)(39860400002)(136003)(366004)(396003)(451199021)(6512007)(6506007)(2906002)(186003)(107886003)(26005)(86362001)(83380400001)(2616005)(36756003)(38100700002)(316002)(41300700001)(6666004)(4326008)(6486002)(66556008)(66476007)(66946007)(110136005)(54906003)(7416002)(5660300002)(478600001)(8676002)(8936002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: VZJ+HZas8OVbE+10g1AJnGELCOmr/9bLysG/NzaM5hKLOJp5jTzVkZeaJlTP1KfiRiGFkZmCLgfnhq4wt7Xy7f9W4B55yL2qql42nR+kDxk9r8KPf3Rvy3DzinLwQWGJ2Wo4PW+1r8zFBAj9n+3V5GOk1KilBN8BI7o6J+1jc70RtFPhLlVG1ofeTpSqCTRy0tbP/U1QigC7oj3e5knuciIRY+SvKuqxT4W6NCBbUEiamU3G0aEYa1wLPJ6sX/AufjfyPWcUYt0vhtX7WeqgXlk6V7/l9axRkPS2hZdBJfLGipypzvdxoPEFN8HEI0fbehILbnpzPD75d6eAszAsYpaCwxOlXp37VUmZftEXrWQl+CHTXna83JAet+/E7Zbinc8b3pDANvl6QsHbB3+PolyVX1ClCBCCn9SAoLmjTd2bKfoRtOaKT050T8v0x9vuCuWez/OfymuxSNsIuaQesVLg0Clv+U2S2Uujocma66XJQzi8CXomcOLQth42ygHKXshy8O4VJqIZi0p5dVcXKGKlv9nEft5BLEFejobSWD/V0GqIqTakJgD5Mssw7day9l7aiUHPefVfxFvS73RmB4ybvXgQvv+47Y1pSJczmYNm8Oatss2yYc3ygHUUobzrxvAOYLZETOanTNJyMzVGBlnQWXAUjTi2tiXzsKsMyMJUHGOPsJoDq1eCbX8LYH50kl68+By0IpKnFkMzlooBqxM8ujSKeQoizOfrE9UBq/8LDgb8tU7ORhfxY+xNvAEeEwxob1vShoRpOnv57noGs1p0CLOpuMDgJq18aC9AfjL6xfET8Vrh4zfrAZLcjdda2K2nRdJ9ftCkGlEx+WHA3PWy6Kvhh7SnH7uWxxM6UyLONOd4eQ6mpFp1bBsr7WaYvXcVS2Mt2EAsGLgHbE2qpp8d4xBQbWP3RbcH6WJ09TuwCjKNsu98pM/Uh6SC67aQOF7ldYjo9hTiPJUEKY843UR8p8KDSFyTkBhKca4zHK4KITklm/Xczeq6dnQxAiIBkix3J7vx2a7n/kzFr+1emzFyLdid5yu6z1BnpnQEvkFA68VYS0R97gYJgPDkODSVB2sxIBeQgsAqva8votlywrAy1jx2mJbg04BYr5VNg3+wE6cRUw6dIDfp0forrKR3vapONrMOvb4/zdud5kG50GG4VCj1SiSgHIIEgW+RE8Z7MadGZOhr4AN99sWouH1TeM/1fJQ3vajUT6Neb128vlrhkGq4sTzoa3w99ph9vbvOKQux0Rpcmb+qF6kLq4kNjMC/ZhMn3m22QKTcpDrDt4I9zD0PpkWvBnh3x+7Lwp/BJu16vRHdscnzvfup9KvML9lkPTxUW670ajk+bU38GdHqUoAJdt6JWsW/5hcMw+moQ24fITvutK6k2DYqzzkuGgbhmJxUnc/nG9FfBruKCkqWIPOzeUTPmGnDY5SzgeFtRD8NnYgXyl/3jtw6P2iyPy2vcV6fSeFyZ3UCJHQ0mXOkrBzqq58FtOeVBtiwjQD83gJkBTHoBGCbSlHbhYI9JFZyYS6wEvPL859B2Y+bQ5SSxLBMxUGvDimALXAi79PW/9M04wCzKiOPh7f2S2u3 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ec6d354e-42ce-497e-f321-08db5898c796 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2023 18:42:21.1364 (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: kKHZVDZnVV+nioc3LX2s9HZhlCrS/jSTftOeeJowc0f8sQGmNGyDA4vGenJItQC/ X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5688 Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org Remove the race where a hotplug of a device into an existing group will have the device installed in the group->devices, but not yet attached to the group's current domain. Move the group attachment logic from iommu_probe_device() and put it under the same mutex that updates the group->devices list so everything is atomic under the lock. We retain the two step setup of the default domain for the bus_iommu_probe() case solely so that we have a more complete view of the group when creating the default domain for boot time devices. This is not generally necessary with the current code structure but seems to be supporting some odd corner cases like alias RID's and IOMMU_RESV_DIRECT or driver bugs returning different default_domain types for the same group. During bus_iommu_probe() the group will have a device list but both group->default_domain and group->domain will be NULL. Signed-off-by: Jason Gunthorpe Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian --- drivers/iommu/iommu.c | 78 +++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 43 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 0d376a3dbd7a42..1bc75774626303 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -131,6 +131,8 @@ static ssize_t iommu_group_store_type(struct iommu_group *group, const char *buf, size_t count); static struct group_device *iommu_group_alloc_device(struct iommu_group *group, struct device *dev); +static void __iommu_group_free_device(struct iommu_group *group, + struct group_device *grp_dev); #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \ struct iommu_group_attribute iommu_group_attr_##_name = \ @@ -469,14 +471,39 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list goto err_put_group; } + /* + * The gdev must be in the list before calling + * iommu_setup_default_domain() + */ list_add_tail(&gdev->list, &group->devices); - if (group_list && !group->default_domain && list_empty(&group->entry)) - list_add_tail(&group->entry, group_list); + WARN_ON(group->default_domain && !group->domain); + if (group->default_domain) + iommu_create_device_direct_mappings(group->default_domain, dev); + if (group->domain) { + ret = __iommu_device_set_domain(group, dev, group->domain, 0); + if (ret) + goto err_remove_gdev; + } else if (!group->default_domain && !group_list) { + ret = iommu_setup_default_domain(group, 0); + if (ret) + goto err_remove_gdev; + } else if (!group->default_domain) { + /* + * With a group_list argument we defer the default_domain setup + * to the caller by providing a de-duplicated list of groups + * that need further setup. + */ + if (list_empty(&group->entry)) + list_add_tail(&group->entry, group_list); + } mutex_unlock(&group->mutex); mutex_unlock(&iommu_probe_device_lock); return 0; +err_remove_gdev: + list_del(&gdev->list); + __iommu_group_free_device(group, gdev); err_put_group: iommu_deinit_device(dev); mutex_unlock(&group->mutex); @@ -490,52 +517,17 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list int iommu_probe_device(struct device *dev) { const struct iommu_ops *ops; - struct iommu_group *group; int ret; ret = __iommu_probe_device(dev, NULL); if (ret) - goto err_out; - - group = iommu_group_get(dev); - if (!group) { - ret = -ENODEV; - goto err_release; - } - - mutex_lock(&group->mutex); - - if (group->default_domain) - iommu_create_device_direct_mappings(group->default_domain, dev); - - if (group->domain) { - ret = __iommu_device_set_domain(group, dev, group->domain, 0); - if (ret) - goto err_unlock; - } else if (!group->default_domain) { - ret = iommu_setup_default_domain(group, 0); - if (ret) - goto err_unlock; - } - - mutex_unlock(&group->mutex); - iommu_group_put(group); + return ret; ops = dev_iommu_ops(dev); if (ops->probe_finalize) ops->probe_finalize(dev); return 0; - -err_unlock: - mutex_unlock(&group->mutex); - iommu_group_put(group); -err_release: - iommu_release_device(dev); - -err_out: - return ret; - } static void __iommu_group_free_device(struct iommu_group *group, @@ -1816,11 +1808,6 @@ int bus_iommu_probe(const struct bus_type *bus) LIST_HEAD(group_list); int ret; - /* - * This code-path does not allocate the default domain when - * creating the iommu group, so do it after the groups are - * created. - */ ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group); if (ret) return ret; @@ -1833,6 +1820,11 @@ int bus_iommu_probe(const struct bus_type *bus) /* Remove item from the list */ list_del_init(&group->entry); + /* + * We go to the trouble of deferred default domain creation so + * that the cross-group default domain type and the setup of the + * IOMMU_RESV_DIRECT will work correctly in non-hotpug scenarios. + */ ret = iommu_setup_default_domain(group, 0); if (ret) { mutex_unlock(&group->mutex);