From patchwork Wed Apr 19 16:11:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 675085 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 1FAADC77B73 for ; Wed, 19 Apr 2023 16:12:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233238AbjDSQMX (ORCPT ); Wed, 19 Apr 2023 12:12:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232937AbjDSQMR (ORCPT ); Wed, 19 Apr 2023 12:12:17 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2056.outbound.protection.outlook.com [40.107.237.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79E0CA5C4 for ; Wed, 19 Apr 2023 09:12:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fP/nWE4Y+1zAJ6aT3Spz0TAjPJge/ox1xIf5jdb2bBCqQC5wvVWIm/+ivOaOWb2oT4WSubJfBz/CB7wl8V1FT4H2E41m9ysbfKIUhUOId9dJuMWwUibwUPfMlul60o31n6DSgOv3KUcFHtonFHJsydGdv4pjOy18yRqhuaBDcOwWz5+dYmPo0kgJnnDo6zPcSe/bYgofofHiBTGYIcDeZG+Pvw3WqdQqoakFrFlHvAk53kReH1ipgLxnZEWwjrXM5IorBIAtFjEpKciYe+CrEL1rUU2opsNvPxRVuXqFlI7Y8jQfup0I6Ns2XmFxPei5bejmK8PbUAdNYiUbmugpMQ== 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=IZIH2eqDTb7gvP2TE87a3iuY8EB0jHQh5ieF/dsAzvg=; b=Y11SXZ/HEKeNAeMqWqWrlFsaDSzSHn8M6DWpmcEELmVh9Z9AI6ACyGYHh24B6Jn1W5IvRfKwJfARg/HJypRt7JYHYGmIU1F5+VqilA5lcccyrY0vaOmXmVkqyLLKHggVhNJ0o4kMDSTATVuKc4ZiX+/woex81I9syDc/ZVx8fPxND3QR+DjlgEKfK8ttR6IZdXnthfhpBrsMHEWWH2X426YZ47C8FFv7KjdwgBV63gmDH8h+eeT42WE8pxbl76n8PkOxiE5poK2zVvIfVozTFaNRxAoOTS6PxFheLd9Gv4f3llhltqH+roBjUIlw6GEh1tsjjeXDNUiXHjAfCNl2lg== 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=IZIH2eqDTb7gvP2TE87a3iuY8EB0jHQh5ieF/dsAzvg=; b=sumJDtMhvEQMk+e3JmEt/P5+d/TJ3QMmxB9wb//9KL2e2pjT1+yN9UbuVMjoGYvVrW6Dbw35XfMuZeX4X58qLE2MS2ZDPQT3q1Okzy2yVgJV0SGl6QIDDKcZFJSOyUnrBj27awP2zHcY+8xI9J5i/tj6tOOYNF96BEP0xCRcft1pJf7N/3Nd70/T5Bsz6hIeJN1PclItFLxwJQNt5JvpPzgaMv2KqNtqNpmYCBwmuc8rVtxCI9D2AtJRRGHPCnlnFaUMrD1cRn0f8FWoczpWN3v0a7rK6I74KvC7Yd0FocozbsE3bI6Mz0X55HA9GrcZewlZjyar6jojWm9HtJb+4Q== 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 MN0PR12MB6342.namprd12.prod.outlook.com (2603:10b6:208:3c1::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.20; Wed, 19 Apr 2023 16:12:01 +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.6319.020; Wed, 19 Apr 2023 16:12:01 +0000 From: Jason Gunthorpe To: Lu Baolu , Christophe Leroy , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Nicholas Piggin , "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH 01/11] iommu: Have __iommu_probe_device() check for already probed devices Date: Wed, 19 Apr 2023 13:11:47 -0300 Message-Id: <1-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BY5PR13CA0006.namprd13.prod.outlook.com (2603:10b6:a03:180::19) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MN0PR12MB6342:EE_ X-MS-Office365-Filtering-Correlation-Id: 359c4b31-8f71-4895-c852-08db40f0ce4c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Dui5RwpWn8CPvnE9Lx/K3KH1P/b+5yoq0qOwMDZtjFX6xI8eCw5KJToNe9QPynLO/sS6u8uPFWgIhsXE/0Lnr+8VspEsAgD/cUTKAwPOnb6LEL69Z0/DNRakPBjAwD1rq7dZ7FZMgwZeOCsq9GHLpxon3aEQOcWJAjtujxItnOFPZaSY62gvVK4PcZD8kuMMysUA4qng7mQ5q51vIlkcprdAsxJ2lAIlo2PSJeQjxopwuZ7pCG4mHBdZb+d22QtfdOz4z4LwypHhvyX7g4mi6eCiViaSUr7q9VG5erHe7xIqZ5EQlrB98bvFynDHidUuR8+y+8iXZmUAjYWM49FNrwtdM+sO1L04YZP9Vqqj11GCl4yoVaG1zSJoArF8AU/iKoRXPMmDFN0z39w4/qKhGqiWziVPUWxW/00WAq1e0yXtccoUHzE9KMvUB+I8Wd1J5g+ZnNnmr/PJvke+RNVoFstg7YsxwNwab8hDqtjUqA0j3OkX878PpNdDUwNrirm8ysL7RGlkyDc6lOdFUgQS2M2tTAlT4x/rh/RTmuCHwW2QLZDRHU2tPrOExbT3Eo1cMMoC0INNnvC6Hx2h/UhQ33ClK6oYBx1510xBsJgUOOmUc5i9L0+pGv87/K1dkjRB 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)(396003)(376002)(39860400002)(366004)(346002)(136003)(451199021)(6666004)(6486002)(478600001)(110136005)(86362001)(36756003)(2616005)(83380400001)(107886003)(6512007)(186003)(6506007)(26005)(38100700002)(316002)(921005)(66946007)(66476007)(66556008)(2906002)(8676002)(5660300002)(8936002)(7416002)(41300700001)(4326008)(54906003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: uANyYfqmq+SNLUfRMinp2OALKebol8u0nKWuxCIceEaS/emHxahogcTgRFhj6NdaVdzir61ijQA6ULvqpQ/9XrP0D8IGuWjFPa8CazWiS19OsE55jmNwHmN+gKLlkgeImp5A+M77Mpk2QZG56kL+Cr66ZbBES18vLPiwLsblA7NUC8JyPJVsLcL++qXXf35wn4i4zPUwJZ8BKFBReat3UTYr9GCenyXb4tsqd0FR4qkQBCxj5mtOVkbak+2U/KvlmNioBeVXcq3ILpHG74qn4f6Zh8rYKZiyHXufxPlgmJFsobJqKH2uyh8Tukfhx5UWEA4tXlxS3t8M45bmuJH7Poxb0qhgfISBdZ7RV6j8aPXZezCnSc4ET/j9u26y3HUMoj3mXUQdRyH8WSp1d3EangJoq2haa4xzoF7jq6fEX2cLPdImvl+70CE/fo/5N8fo312wdJU1YM4SrcUoXNOBUmnmKQ2ujaZBVCjctnS1B1UiUf1qAmP1MPYw8N8CYFxWow0nJ0oPbcm7MoVmhnKwj3u+DMNf/k01j66/bP6opAXWrwqq2KiCnIQX1S+GXQXHqLptvReizgM4RLj4feYGtRFyItNBnP2kXdTnMZyN8dz3ZQ9moUCpIiNW52ozvmEmYUEvpJElQ5dAtoUgKXIofcz8FEBHt/gzwzxE1dej45gR/BJcUAZfD2FbNd3UM8OxVkA8HAj2/3k38d5oXM2Fkte6U2M5oKAQQjSwLIHE/OmyAq61aJWQ5lD7zsl7VjWzW/uO8jWzAKJUuGMuSNAr44mV8rkYO8YfPoUT4nG4bngRewOBXjgckHZRULRCQQvY5IBMu/ovx0zi63OGu5elfi7rJWHEeHE8zGLx2lMtVXuo/0pvatR5lRV9+oLIyvzt6vGn0czUzU9LRatTNvhdK5k/6c6x1jBDQAgIcrGjR43w6Vd7qTo7dueUfa61zpZ5mHxeqIudpy91J2ybEYTFva3N02Cmbf+SXntjUexXKxSRx1c6maDrZhV/A0zDGgp2JKc24jNAPh4IYTsWA0WZ9M8HNmeJMroBw0JexS1O5mjvk8RQAmGzTn4ga+cu8vzxb4dYBYN9M2rWFEMFlT3u3tQ6PQHXZh/7SRWLsMNhU8wRGQQfB9cQHCWqtpAQedxm0/V3MHqdmb3d3eZbH78dryVFk1GkQiyP1uSpMB9W3yrH3t8k8pYW+P7e/GHZ5XOFGxfd2IFGsFKTh8yMbftGqKVe9zVhkIsHsiq/QDoLdByIUK3dqYIaVUEBGt4ptcB+NO6mRGkxlI10Sh8aDkTKDo8KxAH0cY6Aoi8H3oFiSuUlcCvlL7N6HJWzww5WiI/6NR5buOwvFYNNzth3SrzOCTM38V7+adViyircsRc3ldflQOWoMHNX/cnyVmpYhD/htbJ8FRVg1WnO5XyxeBld2ZV8seGSpg4toir75pFaH/toA22mMrgLIa5aflGVP1sPBLegl7VK9wnCon+y5VI9RaUYpKe6DGXJrltgaAtCYVAjZfp6aOirOzMd+rulinUKJXmuYp+bxu8gxj15T/ineXaosAZ4rwO6JJ3TMiNZFPw4bIQQO1DR7gqRuWvDqo3F X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 359c4b31-8f71-4895-c852-08db40f0ce4c X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Apr 2023 16:12:00.1878 (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: rR1NovEdg2ODAgVuDXIGz36Fvkgn2L3R2I3FgKmwpxsP40lDdvII5v1gkLZgbCKg X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6342 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 there are only three callers, VFIO no-iommu, powernv and power pseries - none of these cases probe iommu drivers. Thus adding this additional check is safe. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- 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 6bd275fb640441..c486e648402d5c 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -347,9 +347,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)) { @@ -395,7 +402,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; @@ -1707,16 +1714,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 Wed Apr 19 16:11:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 675083 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 7006BC6FD18 for ; Wed, 19 Apr 2023 16:12:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233097AbjDSQMk (ORCPT ); Wed, 19 Apr 2023 12:12:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233291AbjDSQM1 (ORCPT ); Wed, 19 Apr 2023 12:12:27 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2056.outbound.protection.outlook.com [40.107.237.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 361E0273B for ; Wed, 19 Apr 2023 09:12:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PXXDKPNzylCtFiYzpKUMLwwVww23mnbJzrgwqCoVQFpo9JHOo26/TTSkBYbuyZu2R3kstectC4jD3yyrSE8G+yq0LtkkRF6+UrIbycsab+jDN3tIgeUEazAzepAQgck+t+aP7TuddXhgoPwsMCxfX1KlMi4G75dM/eafTyd0JXaRFevd7Lxmj4evwR/zvZ78nRHug50kqBaRsWeHuUJKqZln5/nNb+uH0Wnrbf5v58fGUPOphizEvd6Z0UTcoiKpgRj10RYJsII75S/ddAZdB2YY9yyzAP+Il5U02sOS9lRrS19iLtovJ2hSR8z4GxuYwhPBObvOuk5X1hqF3KVHXg== 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=Ob1XCKWMZ1f8+JVqs+EBlACIP6d3Jj0FvGWJMUiTryM=; b=hvjAO1pQyBaek0uTy6zrKzEHM5lu4PQwV10blFXbEHQsyYGGd2cGFez/OtcB4y5TcSZnNDjccwTI+pRYYJ66v81IGFNHBmVuMyf0rg9dLT0DZhwnBAKx91tS0q/uLhH+acH+CgWloolITgsl31PJ3+Uubze1uGRQQTOtCO5u58hr1WThIzlJ/bDQ00z84tgPdWdfelV407fcxD+sWm6qLZRyJFhBSUsc0aHW04yotWc+sz/Fm0O6d9wdrw4XFUCakjIab7CGBfuPYJaKfm3EQblXImD7+u53LGplkp45bPDvL1RQAXimkrdURfBpeMB/qS5NR//9tQGpAFXSzxFwDA== 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=Ob1XCKWMZ1f8+JVqs+EBlACIP6d3Jj0FvGWJMUiTryM=; b=svJsc6luABNNQFcI7BswDOsmGkEl2CpIXc0FaF9sDxxbZVzZUfwiSPsXbmL/dzxyEQ2xSFcnms+QqvVInmVKFjFgGwluM1m80YfhMSDZLqv1C1rc77jAuyjf3A24jEhdHx3OQ+eCnSnHKOzLy/vBD+FVHVLWP9fAaQufR0TM0E5KsaRASBbyv/LRMfQjpJ/f3aV/Szc/qCi4vobaSvZ5+nj0raj5ktloCKT9OlpRmmYJPOdz/Up8hkctxM6mtKGolW8GjduIt62nkHLy8gNNOC6GDy5QA/TmB0gwiXUCr8YNT5z8+0QdVk+N3SREJpDUdJNuqcVmFBpQps41LYIwBg== 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 MN0PR12MB6342.namprd12.prod.outlook.com (2603:10b6:208:3c1::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.20; Wed, 19 Apr 2023 16:12:02 +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.6319.020; Wed, 19 Apr 2023 16:12:02 +0000 From: Jason Gunthorpe To: Lu Baolu , Christophe Leroy , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Nicholas Piggin , "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH 02/11] iommu: Use iommu_group_ref_get/put() for dev->iommu_group Date: Wed, 19 Apr 2023 13:11:48 -0300 Message-Id: <2-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BY5PR13CA0008.namprd13.prod.outlook.com (2603:10b6:a03:180::21) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MN0PR12MB6342:EE_ X-MS-Office365-Filtering-Correlation-Id: 475ca1c8-411f-402f-485b-08db40f0ceb3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Rdc5Jut1iyB9vH0nN/YooH8lraIpWFKLaCSfhSFG070biEHGmacakRnHwFrMbuL3FkmLUqh+wAJlGkgiah5NLSleUho90952WrygMRP1+FGhwAfDzY59walc2vgc07gdsC3Cx2AxXDhVcYY+reDlEpM8332fNqeMOD/lhJe2eESox46vWnCNGq/koQFLBdFI9k9wZw7LH4aPk0n9q60c/S3QbujyaN6IB6WGz7M14Ec1YFCO5JUa7MlZYVhe1m88hYqcWSXr3rvxC1TtHZE7zZe1OdnOqhKE7ZOY6FJUSqGwN8A/uSwlHuHMlIqqh9Zkv+CmyyJpxLOIK/+99SkS1AjCaP9HC5E6k6ccZvfM9+ydFOG5Ue9MIqXmNoaxp1ooQtX7Yh2+rxwNR6Gs2HeVlJ+vBFXhU81IlxX8dU6vZajd+Ez3cflPzHvpnqs65Sas1wuL/zbrEYxIMbvBvMkGXc1a0F8TwlQI+njDhafJgCpFzCO67ZY+wH8YCPvWsytvUIQuIL/KMSD4syyXTI8kHn4h182mRWicS1fToUfi4QThfo/F5/oNg1MkR28hdtf+ZnS4MSXhTuogMRLJ4+nGr/r94hTu/Fi2yiYTTn5MXhGRUe2dvJ+nrOzQc6vvvnlL 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)(396003)(376002)(39860400002)(366004)(346002)(136003)(451199021)(6666004)(6486002)(478600001)(110136005)(86362001)(36756003)(2616005)(83380400001)(107886003)(6512007)(186003)(6506007)(26005)(38100700002)(316002)(921005)(66946007)(66476007)(66556008)(4744005)(2906002)(8676002)(5660300002)(8936002)(7416002)(41300700001)(4326008)(54906003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 1K5W1cDddxAqOWX1h4yXFUPe38nxM6RznUhliDnP2UsbCEjRB2T9b5fgpsqamGLbzRyhBaL15l4W+VGuD6ODUbCzh6EB17zEMUXh98ChXR2KGgV+uPBGukeRLAv4GXCdlUVVbGDGpGP2Q2KoRalNRM7P6cgTyaDyTGFrk2yriGlrY1FjpzT3n7mH56cmrpgwbgvw+Xw3PmFMaFg+MHO42qmusPxd97nSxR5JmmxDxoFPhPD6VopBciA1eXfl2ROYxdu9VrLP1QMgF5gvPXzY/MOj6dwhTV/INrsNnisKnYwyDmzwykWPDxyKrGntN8p1HCpZWNwP33ymMFrzZUkfQod3H43Id4xFm16Jk4HQfOX+P3YVQhRZudsFb3q78ihefk3jufYZlhi3zD+fGTQ3hxx07QE45hev4syyR5FHZ4S4glZJI+pLYTRSYDOt1WulNp9dOtaex0NffPiPIJug6l8PHH7ghRwYRoOQ8yTz7dvP0JnACxddKEL0YqeqhQQDxccDVFrn4ihG9RNalTOG2a4E844/swQmqfkM7rDDj4qOF1UdTdwZ/Gqhq70weB1nol907rO8iHDpx7tb81GgMJt7M3pUCBB8J+1kR3BB5Ouvjf6FCBz0ltb2rj+7rKg/HIZSilxggRLuMkdAt0o6/4LK2d7MuV5hWqO5eSWUxa7XlvWQc8gIwsje+yvDXTN+42Dbo475yAQ/bKftNfTpNyary5pDHb3b9UAnEIaTwW/nh/wMwxYj6IbEA5rxbkR7X4Fta2z8ILFGgT0oGEidibtLWGgUH4iLbE3V1XourVw9+gHn9xy1yqpLtZNrSZor0M0oFWvrdChlND3SdKOp2o3VJYfRiqzeM0i9yV2nVH9rLRZKmb/ElP0SvqjwR9v4L9zhFMu/iz66VU3UdeoZyII7JKRK5F6CfNMbiVubR5WuA7mdspQ8ODTsfzjaHacVmJkULIyt4u7h9umJHnFq1TrMUhZ8oPp1z43SlNcMNXbRFxr082CUjocm3I5WV6AVaZCFFBNVwVWsfS3T9weGxoWn+bRRXtG1UzqufZHbJe7SPo5NfuZMPTBF+ZWpMlIlOM3UdPGAVi5hHn2mLq5i2VHfQwrnPeDduVq1NJ7THkBkg5szqblcYzNeETMpZDidjseRvyVkZ8Z3uqv0Jm7+Desy1Zj1OJn/k/BsxpQar31h+sxEVcICUnUhXiUQBi4xTGx6370oUUgEjll+XtpII2jaCXANRgBhlcoTDW7IDw4LZNI5RNTsnPWgWyAVN7UH6dvstzEkUlcp3eNLBbCsZnIXipnYv1SDhJMXa34yneXfAlY/2HXmjtcdcwnIPMUA8jkF0VM7Aiv8k50NP8l+oNRO63WoGSn+Du+iQqm9yAyxQ79J57jxGKWNbKtq/TayrL0kBW9T5yV1OmEK2o6GMxOa8gvZL6RLzE9IAQlmBgKbhWZeZReMXEv8VrYsBo/9MUsx5cmlZUeyNVct8NxICDh5BoOuW7dZkrBCTTHXQNvGI+5nPdO1gV1sv6oI75LkJkNVejzY6F+GDT5sqDz80q0Hi8sblN8TaM/ErMN5owujlFISTEnTpAbaHQwSNf6z X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 475ca1c8-411f-402f-485b-08db40f0ceb3 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Apr 2023 16:12:00.9098 (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: RlQ+YSeIikrBcNmIqqrMQNrBudwIYaH/4aadbrPQx4XFuk9NjAVcYXw9J9R0wfY8 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6342 Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org No reason to open code this, use the proper helper functions. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- 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 c486e648402d5c..73e9f50fba9dd2 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -496,7 +496,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) @@ -1063,8 +1063,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 Wed Apr 19 16:11:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 675084 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 095EAC77B73 for ; Wed, 19 Apr 2023 16:12:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233442AbjDSQM3 (ORCPT ); Wed, 19 Apr 2023 12:12:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232360AbjDSQMY (ORCPT ); Wed, 19 Apr 2023 12:12:24 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2056.outbound.protection.outlook.com [40.107.237.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1F882D58 for ; Wed, 19 Apr 2023 09:12:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lskPeg9/4a1TV9FFDHrwxJ+Y9DyVVzCCfNsay5IUPHED/DSHWJ7w00XkCe+tLVt1ukRxrw2MQspsZ9Y6rxG6Bp2F9S8zPLxYzI49RsippnqYfCGxgKBut20MdgJQHMbxFi/hVL8/6A37gNoB1+hpeG+XNlfuxL1kuJj+8OzoXSFiHK4pV6EUr5jEwoOtpdiwHOob5q+1VwN+LE/LwZcd7kRF+ZO/2RjGvT+OEs/Vt/jJMJIoGJNwTbYBkKSCAdcQpW1EO6j38paiSzXWZQ0/32hTkua5hJ//Tivp4yx02UlCG6MQjPkyUDD1YD9Ha68iT67XTcrWL6C0UxU7PeJgrA== 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=tkhcwo1MvvS4uEyQLFGpPIJVUSByXI6UYKCVXZ7joNE=; b=D87dynAkOy2SOABlz9Pj0tMimzYItJBa2hYgyNDPDxEk3/zmimo1YKrrsOlXW+mJW/GoKBqQDDSBTXxn++l4ZTbzzU9tX0cmHLmEaZtWQ8znuJubu8AWCUYSHbpl/0DV/pMdzHxYN2Nvk4LScHrALV5HV1Rj71HsCVykMExFFkNd/WvVd+13o6s05JUOWxK8uEfINltbrlSoznTH3KhKUtaxE7qPpPVJ6o96UOjXLK7vy2YGuh5Wt38aFwn6M8ADmMnalsyf1Lhr7nOzgF7JPR/xbaP4Capi9KMpLp3Dupf2dVVd+iRipemR4AyFpRB9jMga+jjvhIqSOecQqlT0pw== 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=tkhcwo1MvvS4uEyQLFGpPIJVUSByXI6UYKCVXZ7joNE=; b=B5jsfNfFVK543HtMFQD3dDuZU+lSzlI+lQFASNyibQZN+ksqFPJqn/+MW+DO4ZDnGoq/7WXoE8IHOMhqMDOgOsOB6JeSFZrGtH6ZPN3ThwRF6pOnkK2i6nUzALD5L4s4erxRMI4YMDi4KrGCA130eWty8x0O8LpPoJQQmZZs9ZaEVqX5Bf6tQJBSGhqVqmm5i6sKHwf57pApnl9DIRfcO0bgG8DxoVsriJEEPJPqPFwSm0vPN9AXKNjmzU/Ntu5WJCJ+dRWd3LuSNBQ5feieV/XumhZ/kKNgFEaFkWI8UrOwaaGKKTKKsQYyy428RXs6S/eyUTZVdgMH5rFUqD35Aw== 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 MN0PR12MB6342.namprd12.prod.outlook.com (2603:10b6:208:3c1::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.20; Wed, 19 Apr 2023 16:12:01 +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.6319.020; Wed, 19 Apr 2023 16:12:01 +0000 From: Jason Gunthorpe To: Lu Baolu , Christophe Leroy , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Nicholas Piggin , "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH 03/11] iommu: Inline iommu_group_get_for_dev() into __iommu_probe_device() Date: Wed, 19 Apr 2023 13:11:49 -0300 Message-Id: <3-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR11CA0077.namprd11.prod.outlook.com (2603:10b6:a03:f4::18) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MN0PR12MB6342:EE_ X-MS-Office365-Filtering-Correlation-Id: 971a1756-062f-43b1-aa96-08db40f0ceb5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Z6SbkqeSJrZ2EN4nL5a2buEX0UhkYjQXurehhBztfMA0XhM3ygof08J3eWc+xb43aBG0IMo0I4CxkMo8exQjUswFIociBe6JrPX4+36cwinCEetiHRUQBXjLswZM2KQ9NS3gU0FlSpGKxuCbph5wji2ixp07I7V8lBUOCpMQbR9a5BjeowG67DxGCaVVOfRJUNzvXbfzRt+LXdAPncK/0F35kqbjeE568QpKWbI0VWl2Hao3Lg1ceKEi1jO610aWOBV1/x/LcgXV/sxd0TrOJ3B+KVF13hktquwauWwSYGviLRkanFdZHn28FZ7UvhWiC/BXsBZRhpX934+j4SvN10h+gGJuCIA8l2L+hSmeejJX89iK06m3FKAeEEztnMVZj8azlqg+Ef2PTITl94BFNBKXp88At83IJuUNYEhcdE6wZN5OQ/eQLWNHnuhadR+p8iYOD7jFqbDZsA2r7ndHX1c9R2Kq9g3ijJNU03OxsSVF83KBnzY/wquOEvQTbELBjtynkiTLK8uQsmz0ZTNXnldyC/wSvq678OZ8EJiqvPwKl9Qkinh+TYkSY59Hwb2ouSJE9iGNnUi72IFuMazkUfANguPgW0fXWR42WIWLYbeVtYNU3IVmMtpI21DJAcSI 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)(396003)(376002)(39860400002)(366004)(346002)(136003)(451199021)(6666004)(6486002)(478600001)(110136005)(86362001)(36756003)(2616005)(83380400001)(107886003)(6512007)(186003)(6506007)(26005)(38100700002)(316002)(921005)(66946007)(66476007)(66556008)(2906002)(8676002)(5660300002)(8936002)(7416002)(41300700001)(4326008)(54906003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: AdFI03mAuk7avDzQ9NEQg65jUrsnlIdNK36o3dsFTUVTuUjoC/k5Uq03HrkxlYSkTlNtU4KicNqDQLDKtJ8FB+1adhWOF905W7HQj5iXxXG9tLQUGRVVIfgAX0Etk7VYoSnmzj9jjlIOw30eoxSkGy16oRz1IBrzxM78guZF7TJLFes1rSVLXpOMUKmrv2Mg4YS5nI5MvtZ152ZHZ2huMgmEz9RygDVpPSIsFXTutPwiYHiEZtS8DICuevWGEs50VgcaVMkQroRR5DEovza694A+/are9ZFvHBbzOg1dquxyTHdlIv6z6ZwI6rPMUEnOb7H7+Tllc7s8tkKtBmmPIlheAzqNbnSFmodTWT2Di6fFuHF6TmaN7Oa2UhODX5XtkB2hAp1m9+YSf+RxDN0IMkOlgagijKDygIhwn4M1atV1R+y3YBMmHJqs44pSD8XLKGUXqXFLf8ODMJGNVFb/uwnbb0ViH2Fkuwe0uhTNQzMPDo9AVgBuJc29IIeGLXkImwdYAbMmRaP0P2unpf52MykJny/hYybgSK2IqAha4aXovScUS8MBkqwz+dob3WgX/SiYT36L/gDd69gbh3/BpPEIbRMzKy+ky8DUxc6e+IUrqq01nsVKFe1JsPtKUQUQqAJ2L2TM+gbKABSkV8/ATxoRRbCCQb83d+DP9UWjs7sLSZL5tRnx7qgKXHpyj6bZkARkicEfoNHhZ2g4WuS2VjYY9mKuDI8xDmQiNU1dIT1roGbHOwPi/dwVUMd89TY2rwfAs2EKCTwAWB65F//qUWKm1rOeerGvXx4ViDNZsQT/Utexep56BL9kkpoT941CIYZ46aZ0cS3ijL0XomNyuvIsYj+Cewhot5gycvNSHZ3X1hwjz8rjMnT8BXWnFLoDm82pFUzNVy2dSWnyOHuTbDb0+N0WRWDuSVfx0nNYtIIemQvTkgUkirYs/m31c9drvvDXxaxybBYjCorw8Ulx61RX6njVfhNGtwp8XrJv70OC4NvspKdK9Zb22YeKdtFA3g2n9m0O5B9YX/Qq7/WYMplOkeBW5C2gOFNggMZ+P7xAOYq1K8wzWzDIvX9tKLPgvczmbRV9acwCvSlXeaq5ZEmYzlAfnH/z+S7Ws4q4YI/1cO+ZrSLxXDpFgwuRw9MqyO/bu2/ygtxAxLdTQ9mxvV/Huoumw/xzhH29naneEw0TZT4NKRQyLa33yCWWCxeT6n5Q4nzXrjEVRzBQm8nQYQgus+kWDVYAvMw/XQVxT0/NyuAziGd5DMRKB566fuU+CxChsFjXyrdq/FiVhT8me9wMtJ6Fub5ejT6BD50wxxHofe007cN/4e59eQjhdg6m8D0F3RpvPsgcu3V4/rkXDkJsP/R9lftNRYYXo8RKHwIvrt+DFEAAoBh8FvGos0Jbx/VIINQgCoo4VHnIKDEbS56Z6Oa6AnfJEJBhHnkR57tkB55vWQe7Sy8DJwh1dMZ5QMGfi0lxOSqMYttfS4Va8yR1aZbaNJCexhkuW08S1NxFbVMo7AZvBlF4zTVVUHudjKDJTQQc03mD0P5Px8jyQtjcnFKeq2ZpirDuoESPzWt0dkSSApHR8r+/XtShBgxx X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 971a1756-062f-43b1-aa96-08db40f0ceb5 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Apr 2023 16:12:00.8786 (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: vkhff+qS+U68PG9NZ4RZMJeoYziZ2rNW4sYPk2BOBh0Qa8iJsDhlNiWNM1ptH42M X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6342 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. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- 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 73e9f50fba9dd2..e08856c17121d8 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -126,7 +126,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); @@ -375,12 +374,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); @@ -392,6 +397,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); @@ -1666,45 +1673,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 Wed Apr 19 16:11:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 675080 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 C447BC77B73 for ; Wed, 19 Apr 2023 16:12:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232360AbjDSQMp (ORCPT ); Wed, 19 Apr 2023 12:12:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233445AbjDSQMa (ORCPT ); Wed, 19 Apr 2023 12:12:30 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2056.outbound.protection.outlook.com [40.107.237.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E92483D8 for ; Wed, 19 Apr 2023 09:12:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bSoZaH26hpPKl/Pr9HfKsvSVxC4WWb5q/ZSG1KvI+L9X2eBPHLQaF7f+r7+Ch30f+JA6y2hUELkXj3xampnnMPoAT+bvhM30d2Pq/CpYrdnO+aqUIi/Emlw7o4fMsDu+pfzRd0nrYLaHGyrYL7eTfiskK3QBM4dJnzsM4RVumk1qxYx/3xd0rromER7Nf+Ji+u+t4dqRpniFTdnb/uOY9idW3FZgTU6TfbJOUwTH7+Bt0OYFm1XIEtjAp64dUEVCbk2ePQBQKOxp6SUYsLOp4JSV78NK1p/R21sU6LklsOje4hbru9/GQwV58i2rVFUakxWE5v9hUqnvyYEksToaNA== 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=HVLxJR9iE/gNszq8p/v3CUol3et1BHp7qOc/SpwCcY4=; b=NjXKG8jSc/ycOBFWcXpvYCAvn1VWmGuozXIJ3ZxGD+zVRvU5zdysa8VJciF5ATFB2xZOVoQqqXyDxrpOupxXL1TatDtdokmLeejoTD0udZOlk2mziIE3oMgQYomrcX8x/wM3Xawbmz+fv88xY8/oMhqIB2erq22sxsNXJO6JFCL+dGWe79ezxInHsM7N9bF8z6hbTXhNlWSjjRxGfVRZTIUyhmX48zsKfwUvyTE5/yzJgj24RdB6a3fdQj5Q5a7KPOb60kPRC+C6fgLT+0wDrECqcOI3O+cK3TvxIe5GE53e6/GpGxdZ+vi9veNLq/IKsvSF6qsg9xR3ztFiuYkw5g== 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=HVLxJR9iE/gNszq8p/v3CUol3et1BHp7qOc/SpwCcY4=; b=cuMB1G29RHRkNaRsjQJqnI4Cz5+tMg25uqtYrGOqG+eTnfn/TPSVT6KvYs6bLxxRG52Laa9aBz6+zjrLUlGU3ukUP8gZ0no6RcqzJzSh6+KQXJAypV+L/C8jzbyeHsKh6ljbbKtvdsT0RTPKtGvf0ikCssHLc7OdkkHBDRQvxo4IGhFvMhPKH924u42NojyJeMDPMUFeW/rYZKHz1nc4ubcCJ3U/Mu2yoBFaME3CUpxtGtuQgHOkmmwrVbmlFZhhvH78WOwTl/rFOIP++11HMYyJgHcisotiY159EgyroPDfTwkYAu9FElBJUZSCqPJPY8F3fRApMLOqMsluCqAeNg== 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 MN0PR12MB6342.namprd12.prod.outlook.com (2603:10b6:208:3c1::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.20; Wed, 19 Apr 2023 16:12:09 +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.6319.020; Wed, 19 Apr 2023 16:12:09 +0000 From: Jason Gunthorpe To: Lu Baolu , Christophe Leroy , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Nicholas Piggin , "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH 04/11] iommu: Simplify the __iommu_group_remove_device() flow Date: Wed, 19 Apr 2023 13:11:50 -0300 Message-Id: <4-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR11CA0082.namprd11.prod.outlook.com (2603:10b6:a03:f4::23) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MN0PR12MB6342:EE_ X-MS-Office365-Filtering-Correlation-Id: 95adde55-8713-47fb-8dd6-08db40f0d3ec X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: BBZoPp7TrYeU/TDsk3Btwqzskm1LCTkZ5Uh43rrAXn0k3aX6hUAKzzozUWxo5iAXJiBHsEHTBeXsJgym3tLlzKiipp/bLMQDfx1BIspBVdxuLzBhd5FmeKEx8wZBiJ/Q+1Jvs6Ldh+aDLg9qnSh88SXpaekcDcnvQAeg05VLOWgyTvTKyNxPLQC82UZN4ub2ZWTD05EXKmovTRBKyfKg1jBIgiVxO+fNHtPi3+VOzjXEAZ5rgxqQlt429I2oRktYUYGyaM/FXj0e4pdi20My4K3aZguD3KNq0SkDKQ9GR4qamMOiJJjMtthKKL4cvddYDPyeS30t+QCnWE1aDaIlwKiJ/9AVCB1t2pYo4aXlM5Q4ItboNTLdqNBTHyK4nr1ShYBP4Gd4SNc3sPUawZmENw7RQlZaAT16+9BUnhaNL3drNxUmqIMNcy2V23k+Pa2lJTeXmTHPfhxjUne2fY3YbFFYfHv8pgAeWbu+AWzmDjsL3OD9zrf2Xf9U6dfrYGcd5ktiD2BCl7kEbpOIqt90KJZys0Okfllu96cYRw2ffTOM+a+PQennPMKI4vOYPHPXn8k20P9n+IFABV/0dW5BwMqg9CHC+rWokyZMSi4DnS3x+Epqi0a6QqGGx8OZ8IMk 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)(396003)(376002)(39860400002)(366004)(346002)(136003)(451199021)(6666004)(6486002)(478600001)(110136005)(86362001)(36756003)(2616005)(83380400001)(107886003)(6512007)(186003)(6506007)(26005)(38100700002)(316002)(921005)(66946007)(66476007)(66556008)(2906002)(8676002)(5660300002)(8936002)(7416002)(41300700001)(4326008)(54906003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 8wVFQ7a1CNmKpJjLBfHyfUvUGdJoaIUBqGbsUlxA0GBTpL+bDbeA0mTL07Jou8vg0PRK9ML+oSNAASaEYpIa/qzP6rJy3s9PSlGAgUw5Ad8Ug2aOBTb6Xt50wAQy2kY+6Rfdd8NXhYwZ598QoJejcpbU+2EXsMUrM3Biu7svA8m0nkj0YrXCYDJx7Ovq5ui8kgPyOSqKMCp7uXTOtMIrol1uhGEVJkflIb5gRAj0eOrHhdYlqrAJ1Qui3JsDP6US4ID1/s7nTouQPUr7YYBA64LNP2TFpgbnbG5zJFffn6MWIQfHPeCGf1lAnBQod2r3rQOg9/eh65vkRC1Ltj40MZxTsZU45gRMCygzaw8H7q8iZ0qEy4pfOp76QZD9wL5sFLin8aAhA4vwc1FgEvFlkzG90LvzMUehp/E2vHuhVJWmM13jGJnO2ovAcKlwHX3g8LJ9FJm/dKDzcn4yslSrXnQm14xcPFUbtIF+Q/8tz2NVISakDJc+joUMy3sHWzH9dmbP5CXMVUnGpGKfPBV1jsYHSmmz+JkNwCLROcQKCnXJtXowVn9KjWrNd89781rrAudAkXV7NrpLZVUSM9zQYg3jbTR6ouLlpVstjlx9gL9E3ZX5dIh7B87MzhkejoRhfK/Q17BQ8rEF7oj3k7bGf7oDvGmK4Z1Fe07z0ufUXPcK660nTAB2PshhhhVuQaDihs7dKKWBDMxTgdwpgzNdeLBpeQVoeE9zeqYRuJVfKvIMglhOvoyY5qZHaxzW6ezzwVLg/HDZQXa3P1lChCBD0Wl/lf+kdF7gN0JJwaPNCZMBi3+AE51l5pOnBkcN8lq/nCtGpMRVkRxqXLMzXd+8/ijVUJKtPgDZ80ba3c9P36iSFnsaMxwk4U1eytE03OETWUFisX+xuvnm1opT1j6FAIiDdi98rLNT+NzsjVUdOB18jFFkPZdQFALC9yyMYyg/+VR8+1WUnvlpk7Teom2oBa1JWWYfWhxkk5xSXbF8pWN9XTSqdyRgHRJ/J89Q5C3VdaEp+eAFoxjAqscepvC2Fvy7a1muNCYE64HBMNHxyqdKa1hGk0VIJUKztsgwvbS+7wri2+GGW6iRtk8V6IicdUOMBmmu9EhYzq3QPQtVteSHVyuF3IujUhIe6dVtOh0ZThMokx0LwfoPvP6B+karWG36cudIyUao/rUI0VWKc1lqpx4WNN2UnwpF45zyrdBRVTSuODmYUZlzD9bqwLRJCqEUPPt4jL7E5gc1Ap5a25yGZCOgKUC0toG78J0zqrifkayk/yqgiSwWOltKhb1OgNRc8CdqVczOtb4UZFFHXGLD5Bgo+RyhEkx9jvMTLG9yDlJ6E8CXO8p4GJhr8pA75MqwX5k9aSYoAfaa/yYbe1sOOIVehQcbHkGTovq5gdK+77iPCmJcv0dvZ7JkaHnqHEBkUpC9uFQYiXo5S5IJGUiCatEFmN6J4q7ZfoIudotaXiNOUZMBimYTbIgynve4lewKS0scesil7FLnixl8G6PyRuBj0V6gpJfYcrJzm27Grz+g2ZhMIpg6jxBYOvVvJ4inCKXBGJENk6DgA+g/GELG6Iheg9LRwZSOdO6HUM8N X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 95adde55-8713-47fb-8dd6-08db40f0d3ec X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Apr 2023 16:12:09.6011 (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: xU7OcIT4vOODPbjpMoHGKnhV8W2dgwgLIyihO+B8INO7QnnuIZ59LSEcNkB101IG X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6342 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. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- 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 e08856c17121d8..471f19f7de8c4a 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -466,32 +466,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; @@ -500,16 +476,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); } -static void iommu_release_device(struct device *dev) +/* + * 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; const struct iommu_ops *ops; if (!dev->iommu || !group) @@ -518,16 +523,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. @@ -543,8 +539,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); @@ -1103,7 +1099,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; @@ -1111,11 +1106,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 Wed Apr 19 16:11:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 675912 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 9439DC77B78 for ; Wed, 19 Apr 2023 16:12:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232249AbjDSQMn (ORCPT ); Wed, 19 Apr 2023 12:12:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233433AbjDSQM3 (ORCPT ); Wed, 19 Apr 2023 12:12:29 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2056.outbound.protection.outlook.com [40.107.237.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 225738A51 for ; Wed, 19 Apr 2023 09:12:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PuBxsruVz3SWpp75c5ChlXpbEVAMhIszVoScb18ynJFNsYP9ezDGdeB5zhrvxhoJ61kCxPC+gKtASUl575e0LihqQHM/EgQ2JyZhWv7rzvgWRZyzBmzlOMEbsFVUbvxLdbQN+SXWZhCcm3i+i0/p1GVGgAssizcQQy5OmoY+sQn550DL0289mbzX/dWho5TrKJvUA+FFLnsuqmS9dgkU/ds3Gi3vVQTL3R3WWHOijHUuKXLSyBCdiCl2c5Fm4gTtWRazmF0N+cR1gSGlVRpx5F6x8fSdMAuoCigL2RmfNCQfq501nEMBOu6m+DRLSElDA1jjFTIk7GKvFlNZkq6DMg== 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=353FuJA1X3EhPJJ7umLqlJHQUoY7BY1Rk4U6jw1j/Xk=; b=HIIydWGN1Cs7Mn/DfWGL8h1/z6QiwB43j9psSBojxSeEE1YqEkE/GgNN1O+Eb8viQ+Ei3S+3UQgDzEPaMnkUju2td2Th0HriCyJFWacLCgJsPmhfPscGdCRIoCxQR7acNM6Ope0sMjwp9k4FPshP3oAIMN9jlYpyZR4xfCPYGjk1QMnHcjVPpIcJ/jlk9Na5kWW0V7eN0r4iUXJfsUejJ1o3a6KOEy8pKiGWPfrQAbNMX2iWXI9SP/tiAMC6XKnQKpUFmkNYe2N0TycFoDKAKEglE63dQn9QZFyTR2hjai1RhpZ8nN8CqIWchCZ+Tu4wn55Hf78bNQCTTifceZJrRQ== 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=353FuJA1X3EhPJJ7umLqlJHQUoY7BY1Rk4U6jw1j/Xk=; b=ukDxOGAH4E4sZwk1F2ezi72FLrkBU1Md5d/NK/XwsGzjF8rGoDJMUKvKbKSOF0cu+nnYdB+RERyqJH62JCxvXV/h5LfZpbeD9KWJnTH/r6t+g23BRXz6omy5/Fsya/NwJu8RQ7uhE+RE9n2yHWSYVHVPpSbkQsCTdpHtwOTdLJ2BEsvrh8joMflUxVkKLFtCiZISfXYJCFWxgI+1yZhEqims8wfK2d5NlbBctzf25VJS+O3fHp4Nb4gCR8/lFnfFJSeYkbmwNNl+g/t7g8rSveLXhmfBiMB5mltX6v38wKYjvYTmXPflJkYHI8Q+dQd2ya6x8qYFWvaXzODXZKu4rA== 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 MN0PR12MB6342.namprd12.prod.outlook.com (2603:10b6:208:3c1::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.20; Wed, 19 Apr 2023 16:12:04 +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.6319.020; Wed, 19 Apr 2023 16:12:03 +0000 From: Jason Gunthorpe To: Lu Baolu , Christophe Leroy , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Nicholas Piggin , "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH 05/11] iommu: Add iommu_init/deinit_driver() paired functions Date: Wed, 19 Apr 2023 13:11:51 -0300 Message-Id: <5-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BY5PR13CA0007.namprd13.prod.outlook.com (2603:10b6:a03:180::20) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MN0PR12MB6342:EE_ X-MS-Office365-Filtering-Correlation-Id: cbbbf07a-2158-4d11-4295-08db40f0cef1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: c3NIKHGRFvBJU+EZrvC2VI2xTfSa0Pr3rFRPZ5V2BHOtQzjvXEoPqtizVENAmCFKLBXP/OFAuyVnCx0unYa80HS3Y4k5uxnKEO+iF7e7L53AP8ijfdGma3c79g/1L0SKVHIlK0e1FQrc/UvhOGorsz6IJaxHsaM6LPbzRim90UNpMCvcBdQI9SKQGjGnaHDba6ITzIS2myG7QVHHg/qC1slPSYQ1X6sIh5dYeXW0VFEJr8L95VERU4c7cby84RUhO0OCiv8ZLDxUx93snZk4J5zylJ+3ZTXLXCoiACVbSedWAlbFh4D7RQgicr69+l0+g2gVP5mQI5Lr4a7Ztr7/H2gEthw3Ao3mlZETR6bIxnmw9FyYTU9T6Dn1Jv4SxXyH2DDoTQ0idK9gI30z3osVR3PVsAsR1GMwIBMId6A7i3dyU93li9RQYuJnniKT330s6jzW7BUFsiBikOvlZdJgdhb96vMMihDIBYl8stCSmzYCF/D5qefPqXCeMyOVFnSskpGglk1HTufc8MZtKO4uA+M3iykT4n7cd2SlID1VqqnP/BkRlvfdrCmt5u6DiynK+JpTe/jWVtlDP9I3v3XTBXLgnvlJgk0PqoE+tiMTH7fENuuUvS24X8+GD4TcwNAB 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)(396003)(376002)(39860400002)(366004)(346002)(136003)(451199021)(6666004)(6486002)(478600001)(110136005)(86362001)(36756003)(2616005)(83380400001)(107886003)(6512007)(186003)(6506007)(26005)(38100700002)(316002)(921005)(66946007)(66476007)(66556008)(2906002)(8676002)(5660300002)(8936002)(7416002)(41300700001)(4326008)(54906003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Ca+ySlVtvP0gIv9cJn1BidSGfrvGVbOUNQpx0NilhQWb3gJTCtJ2lHfcjVpcUGOJu0ehoi1IFTKbdvbO9uq3pH1MPWCMBYJrDvwMsdorGR8mV1vLDozKmpyqhW7qWosTpL2q5iqPKE0W0bm7KNZ4aPOoaC0C6NBeyk5QEeEnCVuszBoaMtie4yNdWay5V0jYGlfXzbV8yK7gNQjywJmxHi5i57xqUrjnHmAOnc9MhKBCOyuvt/X7Yq8bADvuNtL8/kDc+96f1KGx3Kt15HCZKqQs1cYdINyk/RYm1Dm3YVE62qasyPUik9sziA+x3Jtu+N4G2/TqHQI9itkNe9WaZEJgr8J7RQXixHjXgrMzjQ0nhmL8dJnGqrgJVZ8jIOBe6NgyVROn3nu6TM/r44vci1IG1n1vyk+Wuhtu/7S6VSYlalM2nyXvptjytZFAUQ7Ai+2LrhPNln/dn8h2Ym1QCnBXqB0yxv7xaJN2H5eWWhFtuU51A+ueOmZsW0ItJlLFDhZdo65xs9to1BPFQXNYfNIakHze1YWDP4WKhw3PjYYNNlwZj7XCSGCt++B//XUgv33w7XZ6LV/HqyqbvFdq3bpfFPkPD1X2XF0cjyL2UVYPsQCYyqDsTFvum8tJlVd6W86HqCwOBXxnQS934r00p4j6UX4tdi4BFzHeojycDolsI8jiMYx2Yej216MNeLntA6ObOoSRkHVOTH3fN5bm9iPI6YrTQO/+XpOu1+kg89dbZqHNfSn4felav8E92wJf0O3f8WqM/hGh24Rt6E+jb9WscGz7tcTaI0sI/AEi/2t9ZgQPQZ084qdOKmff2tQxbAl+A7rQrBs57QQ7Be2rZZnAJWo2pvihQm2uM+rmcdNGFGZAaJmEBO/3RiZIOpo5ptvyK9pD30Khx8gTTadsIG0kqa+tvP/eBvGLyecbXYsuXXhIUzA1ljXQHVmjnRv7b/1OWUdeh7ribRCDLMy4z4sjm/GtyVMVnHlE1EGioFZSFyxRsdlSVL0t3gIhAyaA+M8L3BWbohbfp8peWN1wcoG2FZvK0AorYwNoQ2H9zYjJScoAQZuXBQhhOmLuPwIWFJ0p4AZWsGms0OFqoQN3HpfLQnu0QgO2WGEFEEzITinZepZVUbgFcl6gZPDShVgtDkFEjZWJMGT7P6v1qDofw77czSDHLv3W3JJq8WtRdZZbEhCTsukhac6lGaV0EieCoV4lOlJQ8dZglBz06Ljz7Y3GLlfpUvpmOfPxXrX39tamrY+bck97UuQWB2JXbjY5/yjEmlxTM58ZUaDWJbA8IZkrBpsYRjmG9hVrINhCBMTc7CpSfki95xUyNzPNGgNiPJFVmT9mMhKUmqAh8vg59CenjkL+/chAuQbSaHxQLdSbeX9vKsiqB3A8IX6+xbW9rBij/zEEmu5Ww+toNO5I+toev384mmPJTf95kCxeBCmACYmfOflBJB5b4xggpbh6HYALJd55l9RKGwzSbFSsHMD5EcGO/cRBSw/KyiWHUV5X0ARZGqu69VZHN+qrS6LxaK8eymG/jS2R696/qWeyUmMUL69Ppa1V/ghxNBhhCjZlZbLDTNLAYb8EEMe7v1zs X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: cbbbf07a-2158-4d11-4295-08db40f0cef1 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Apr 2023 16:12:01.3975 (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: UFp7WeVSIizCOqhCIaDqDeUUD56a37aX4gC1ihYdnjxBMzSqsXpYotG6nuSjc1gC X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6342 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_driver() 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(). Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/iommu/iommu.c | 186 ++++++++++++++++++++++++------------------ 1 file changed, 108 insertions(+), 78 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 471f19f7de8c4a..e428de5b386833 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -328,10 +328,95 @@ static u32 dev_iommu_get_max_pasids(struct device *dev) return min_t(u32, max_pasids, dev->iommu->iommu_dev->max_pasids); } +static int iommu_init_driver(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_driver(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; @@ -353,62 +438,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_driver(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_driver(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); @@ -487,18 +540,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) @@ -506,44 +556,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_driver(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) @@ -804,10 +840,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_driver() */ + WARN_ON(group->default_domain); + WARN_ON(group->blocking_domain); kfree(group->name); kfree(group); @@ -1105,12 +1140,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 Wed Apr 19 16:11:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 675081 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 808E2C77B7A for ; Wed, 19 Apr 2023 16:12:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233434AbjDSQMo (ORCPT ); Wed, 19 Apr 2023 12:12:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232360AbjDSQMa (ORCPT ); Wed, 19 Apr 2023 12:12:30 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2075.outbound.protection.outlook.com [40.107.237.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BFA146AB for ; Wed, 19 Apr 2023 09:12:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=keFkU2Uzy6ztZdXDcCWHEvM7nvmIPSQhfzN2gjoCdXFvLivZ3y0Uel3dTuRW82Ehvm5w2lwnFuAx+0jG5ElWYzUq/uuhsLMPywecQ8V6QotniMUI/HdfcWHA/MmhebvH0ykxZhITCAMtWO8H09RNzsijJxAKUznWPH/IPOxFjbsjw5+MmWV+fgIpQAx/BILJDBWWGkdpVTsx5vSkq8ok6vrSK00cC4wp9uei5TrrmuaaSCfRVy4ypEcbdclHwmR2j63atlVhUinatb2H9GBIZTArzzEWCV9qxhT1CLNvQtkCuSvXoqdWevRu2VO+wV5vI/n2br/lvGpCVCJ/RQVGYA== 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=7zkU4TIkJCQSBw9Qz7BhF0/gnugPMjPL/sZ1r4i/1wQ=; b=BWtInYPoOV6M/Wu5Edvx2nHh9TuKyWDqRc5UJSafI0qyqfShh5Qxfktqatn7/nPeYkxCd+qjiMbCN1tD2unOT0bL1ct1u8gVqgIknsWIY/IYITpwng5+9ZhXM3QZaMt3U3kZfI73yGGGLRX7EO8l/1O6Egbgzv9aiN9Ce4UUsI/q55WGTsBGzK+JRXDHe3YXnNFxpLetqMf1gqWtnDywKcEYr6sOq4fqQVKm1sZR/zkJNRUcz/1qEhiZ06Vj2/wM9o5Y2XIBeEUDirbz9F51WahDFD31sSuuJhE199YTaSZxmJQc5907HT+T6x8B7s28/wbJZTT+e9NdjEb9x2rQ1Q== 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=7zkU4TIkJCQSBw9Qz7BhF0/gnugPMjPL/sZ1r4i/1wQ=; b=QnfcsrLprZyjHgDPNlP3cHofSHNhzT8s/ig0spL5urh2fDC7PBbFvnUN6Cqhq6qoLUrjohA7MOE7j1/IJkweXvhdbPf530CxaqGIonX6HZxrSOV1p4oZqPwLkL6cAAZ48/LcspTSIEjO2qExfWFfzCxhmRuoT5DFSqX1uv/CYIRjtaqm/iwjgxYud3FmnJ3x4QySS6fQAIL0FVWyd5qI62LxFTgY0vKNhBO/cqTXRR5jQGlNmMZtkWAxGAnHhZhy5FeVCss/8r7Y9RMgMUBbPe1a13T3j6zoNHLYd5luPuvUxGp5J7n5/tSbNpg3ramgw7TdZvc77CPFeF2OK4xFYA== 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 MN0PR12MB6342.namprd12.prod.outlook.com (2603:10b6:208:3c1::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.20; Wed, 19 Apr 2023 16:12:05 +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.6319.020; Wed, 19 Apr 2023 16:12:05 +0000 From: Jason Gunthorpe To: Lu Baolu , Christophe Leroy , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Nicholas Piggin , "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH 06/11] iommu: Move the iommu driver sysfs setup into iommu_init/deinit_driver() Date: Wed, 19 Apr 2023 13:11:52 -0300 Message-Id: <6-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BY5PR13CA0016.namprd13.prod.outlook.com (2603:10b6:a03:180::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_|MN0PR12MB6342:EE_ X-MS-Office365-Filtering-Correlation-Id: 351a8674-1faf-4110-725c-08db40f0d035 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: LnLpMnYjFug1wgGiznw47k4adfwZn83/0NNMsXO+uXlTetbzuCY/VC2wAjM0hcQor5qn6tuVosFT4GOP2ie5ypx1/rJz1p1VTxh8oryWDqOtXSgqEFgya0NWUU6UBZtQqFno2m68RVIb0d8Ryka+BxGij07YalnEk5CrI3mUy6DJq+rcZx4YJ7zydU/jXpXhFQ9vloREsIUKz1XD+6hIQnRmKqmjWZ9OHPY73CB9rowLwdGqUAplUK5SfXRM7htCqIsCd8YI3ZvLPpFtnovee4Kv0qnWdz9Nf3hdL9stBk8FAoiQsDImJLIyQFwCoIbegOkBEgoKMv8aJFG4A/M+mCxsRLJQrp1AZRmW/qYlpJJtkmQuYtArhmr5vOGcpQRO2iVz1SXKaYgemcGGx3J4TLuZCtDLUgqPIBzp3/Iig9cx55ixWMYPZWldsVYe8He1gRaNZWXXaNLUbSfhP26V8YZ+AEjaNwJM8D96iu1NECwYPVADDACBVIhn30Bcw+Qj1Fu912WZoQ4XV1sFH7deHFFxhGNb32aUpYWIv1jDUTIa7XHA/rs8P06PeaJ4Ks556HFXr8bU3YaHXRoLu1RMBnupyVzhC4RCTsrBFabPd6pqgf2FfYv+A63bTCji2tyg 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)(396003)(376002)(39860400002)(366004)(346002)(136003)(451199021)(6666004)(6486002)(478600001)(110136005)(86362001)(36756003)(2616005)(83380400001)(107886003)(6512007)(186003)(6506007)(26005)(38100700002)(316002)(921005)(66946007)(66476007)(66556008)(2906002)(8676002)(5660300002)(8936002)(7416002)(41300700001)(4326008)(54906003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: pH9XvJKxotuXbRbPyL2PTCKEZQAtvpBrRoy459H3VQdZTihv47WKa8ZeL3qozAf9HuJeBxLFg4t4r9mvJksAMy2n7+/hHt+seGfvrXR6A7oPA7qvywfy9TFjPmHabRK3BnqwAJ+KGlF2b9/E7MP7Ce9LjaR7u/qG9R/zkjuhIOhw7VqInW2Amtuy2A5x2ieBf//FR199NBsNVaF1uI64NAGgnMZ6QgHrhKq6XPM6Lr/3fqRVNjIEw4FKZtACruApGjgnr0RBPvoSSXj0gWcfAjmTCOCq9Fqj+zFKp3Y7OwwCoCYRPnCRKTCnymn2VdKHc3OjlT9shYKyjJd0Jq2B+HeRAWGjGbZ5k8mIP4MflIrGEUTYuUB5p5oI1tgEFLU5tV1mYEIpwb8Mev98oYH0uXmIt9fkYuocwF2CG5OJosS5VjEeySG8QjjPpv76jzCshpTVrfRspDRcsiZZ+/nCEAx4ZGcRbj/QzoGLAX7n84PWQGXUjtJxpY8hVwsbHFSy1hjctspKlWDT2udmbAMSyGBwy1njlKTUynLd3TOgRw4g5QyH2kj7cMFPTp/8G+GblEkIySu7a+ddhwhKNlkb1rySIrFKr4TM1PW7Pyr18dW2tL5nWQjAIIlhukuE4YNe/nOzDnVP80lkM7HNS1C8zbeZ5A9LeB7TTmntPd7gBoJe7QnKDVEIYTcJ9A7PVkV4mNB0pRjpTO2S4n/wjqMFjLCu10g6chFV6IhFEI42oeaHx7vloYJeIBvaxRpzKCQL2RKpfnFEwn5aVsGCIIorUSXZbJ3sCJi2eXfnWID9b651d+CUqUSRHWTGlVHlpy1B9yDESBPBvK9fBRa7CZG+2569Oz22yrgYTELhYQEwjDmhCNMV8uO11tkYbdM87moSDfm2Pjrp97xXE9EtAAB8uDAVma3PYVqDFEe0LZOLmEjMon2K/L7IxLrE9KAsnjRMtF3bfpNEAtsiN9cN4SLUA9+WLf3L/y8ZxVzT618hdSYIAciUjJZiUIFZz9LI0IfPrCeBYbvagQNRxI195m+6+2ZLNP2WBxYtsn14f2gFONXbhgVy4WfxxtRlhcOPQd1Wh6qY15aEopK1h6oUE9iVDwAYPqo/6dmnb4+t0dO0VtVlcpwBq5xtHAjBK9x+DBTZO/OqY3uRS6p+IVi/vR+6/ZTq4BfFgid3RgmAPL4InYcMLOvyzMnmGr+/38WOfua0ijiQDvhDEAFMLuO1NSVSNVvsgxqfkxNz+E4n8HcDg23pnbe/FfkU6HKc6YzW0Pc+ckCNDuGss352HyGAOxWikqSFgQRKnvaIAGUsZ7S5/5ejDvEPTibO2Yj+nDTCvxER3mOssXZNJvehcSXybUkBHnrXzNbSdiNmHzSpDNQtSrnjGlK76tnfdgY602n+QpIV2haDlzvCGZZ2wGx8ifQ2nNr6d30U+NQ1R+NE81l4cyijsMudIblURxfXPDjeYq6qRiojm/Nkcbfte73FZdWXBfX/+008atW7R2n+FcLrLkUBjsddk8IE7xL4Vllqh1Ii2eICXdKO/PIc2k0MeMt0gxTALaQxjWvps5vHxrFfFFq3xEsaHcdL2PGgUA9leNVu X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 351a8674-1faf-4110-725c-08db40f0d035 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Apr 2023 16:12:03.3599 (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: x6KXzfVx+gWCuWyygUlHgOzjF12ZiNG2JdvFdPfoiVWD3BUpowIUNJMyq6irMysg X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6342 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_driver() can trivially handle this. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- 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 e428de5b386833..dbaf3ed9012c45 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -348,12 +348,16 @@ static int iommu_init_driver(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; @@ -363,6 +367,8 @@ static int iommu_init_driver(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); @@ -380,6 +386,8 @@ static void iommu_deinit_driver(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 @@ -454,7 +462,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; @@ -577,8 +584,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 Wed Apr 19 16:11:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 675917 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 2CE08C77B73 for ; Wed, 19 Apr 2023 16:12:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229736AbjDSQMO (ORCPT ); Wed, 19 Apr 2023 12:12:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231230AbjDSQMN (ORCPT ); Wed, 19 Apr 2023 12:12:13 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2089.outbound.protection.outlook.com [40.107.244.89]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 916459EEE for ; Wed, 19 Apr 2023 09:12:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eb8/2yPeCMG9BNtBwnUxvDXvtc2OyLxLiQBH4CogdtrBLRq5EbXoUJe1E0RI10MTBI2wBMdPK0PgsAxl1J+9F0ohW7uo8zeGg+IOhkkfdvuC/LWhyzKwPX2dQZpSBPJU91CkEypB9AQzZE32ZGLYovQtCio+xVojZe2x2cM6bwpo60H/zKWsRwZ3QjKTMLQlHKxOy163XTw9/4rPSZ/njrs+KaUgV6on00iH8HABCAIhdPZ2twxe4guyVdNixlVIyeKw1UCUdufq8p7HLSipg+w0cB/Sn5iGI8RO0CvoTqXEEIoQZ5lGJFfjHlL9dpLMo+rPODUzL6w+5SxTAE40Ng== 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=75S+M9Ne7IDnCk2KalAFQ3AKD8oWpYrnc2oQngrvOLA=; b=FK8wTXrk1yE24BG04gyDPHLfwAKBiZ8EJf76j6Rjqvpkk63qGmfGQ+5QPDRkWuxQelHWTKTPFmumdpQSKD3PD/3fnvhEgVGpq9cMlkJuOgbcpK31okkaOv6fvyxDAnsVr1xgBZY6c0fzdUK+YlLlqjEhqMgLjipjaDOZ8toZdtvQ/oavxz9Lgayopj0R41gNRBIGhv8Yi4E8+KincXC1GgvkXMJNmI/N+mT/sA+La/MxEuiaFU6FiO15A5wCLmXWVW++FGaDAZwcr8yzGgpXkb7ZVZa5cBFDK8+yTs+K13iqnyTWnBkvJKnKQxFd1JUtCf5UHA+Sbr3IY0e0CNYtdQ== 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=75S+M9Ne7IDnCk2KalAFQ3AKD8oWpYrnc2oQngrvOLA=; b=uPe+7Eg+XoF8xLqlnmqKVO3NkeNgA5G+xPO+JP7bnzbGltvdDjnZ7Th+fk4Gxjx7GGjA4LyCfTLtawwEl80z9BvHDd+rsnCR4Ck2dEAZdR9Tot1GgkZ5dl3WClJkWl8Ls7IGSUSJqxqv/cYohbPnlcSjiv4SI9TeOpelIfEVbuUn3Gc+tP9rpypoEEhvtUYXNXBws4R/F8g/H0/4WLkvAJ4LyO+CaOYQWqrgLcdGprnAfprDovPSzqhyUUAw3LiLO/iRzvXxiSoi5D8Y2TRgrqnFTPfxRa8VvuMIM2cK+GDqawge50XmkranpfLnyAXVFMxZvFePaBZGN3LtCXBQZw== 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 DS7PR12MB6046.namprd12.prod.outlook.com (2603:10b6:8:85::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Wed, 19 Apr 2023 16:12:00 +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.6319.020; Wed, 19 Apr 2023 16:12:00 +0000 From: Jason Gunthorpe To: Lu Baolu , Christophe Leroy , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Nicholas Piggin , "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH 07/11] iommu: Do not export iommu_device_link/unlink() Date: Wed, 19 Apr 2023 13:11:53 -0300 Message-Id: <7-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BY5PR13CA0024.namprd13.prod.outlook.com (2603:10b6:a03:180::37) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|DS7PR12MB6046:EE_ X-MS-Office365-Filtering-Correlation-Id: a849e023-7022-4629-03dc-08db40f0ce48 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: jQ9yjkMKWaxvK7GgvPne8YQ6GVwHxk4Zywycy1BTgHIy0dbH9yl1pI0mpjprikICPhgdRzkfF+hXAbw8VY804SREqtaELBsufaz9K7AiMI4n115e5KopKnsqIWcFiOPFL0ZL0HKwbV1C4XB1SLDQ/VrSe8EOrheHepeSrqZdqrCRFaZTsvbk2u3LYWAxoKzdh+Yjk+D1gaO21VO4f5xhVitYTl7CCQGh5mgmkocWaRHd6As3HKCDF3kVCR2y86EcQ/8HJDCr0K18Z5z30zupjmoEpPShCTtBoAafMlxenfraPrmH4CRKfRW3lfoV+WKJ2JfNksNXus/y9EjIkbEwrmbHLBcsOsTvD4itIjTSrbE0cAedBYJuMGNG/DCrWeCbJm/ITsoAuS3WX40FOm+VS6dmjD3lBiOBtaJgxvAu0HhXWFcb7pjY78cyhclz2HSrLfD5Uyphe91ilYNV0U/s1A50OhZhOYSO8oKnF3Ax/G86PsQnh2I++O4TbyayPK+e45l+Xqae7Kb8OpJelwTx+6cQ/Wc1oP8qpRaZKdUnic9pzxpPYvp7KkH+de1K7A8sgtyb2Z7wIXcAZoUAVmRpPb7jbCbjywnpAGhVMcZL4zYmba6hqubAxLXCzwNru8NE X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(136003)(396003)(346002)(376002)(366004)(39860400002)(451199021)(2906002)(4744005)(8936002)(38100700002)(8676002)(7416002)(5660300002)(36756003)(86362001)(6486002)(6666004)(107886003)(6512007)(6506007)(26005)(110136005)(54906003)(478600001)(2616005)(83380400001)(186003)(316002)(4326008)(66556008)(66476007)(66946007)(921005)(41300700001)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: o0G7fL3YQYTpjmNoYpwoOTmoG4o8Q/6ynFCAWGO4bFD5npvpIvJqryFWV6eWguglYBbXV3+idQOXy2Iukdr+T03W3YyQUaYtmPzonybglZBa54ftBD9Rf9y343sDtmBqwbHT20JWZrYa+1d77cI516LGHWiwsh7c0AhQwK35UjETmQRIaZnrbu9b14+6p1T0r/O9nGyQTyBcTgA1iYSmmf6hGhMPYpBTvJzm3RyFffg/xjVLWqaufZN54CKYHaKWypn5/EaPmgzLfGaklRlclfEv2xiXsPBZP6A/XxCX+zSTxO+TVENCUXgrnGReOqmcmpItrfKdSpXF/iP1QcU4N39ockTlnnpY9O59+douNpmJDyGs0KU6vPOnWaTzrkFFj30lcIPWujLVHF76lBs1mZcb5L4yXA6dnk0+4M8N5udnsYu9IuIElcbVzjR8XwNwgRgUNcq4YIRBiaHAy4+ihBaMJWA+B+eCLFq3CJzC/z32GtgpLwqQnI9AVFCSmfsLe8lcGipM4ZNX33IWVnYUxIBN+ZwHioHnqKaNLJtnNmGuuop285jsl5xKcDplhXIBPhF2MUjr8NnN81k3ZBqTeE59vosIh/vX5Q3f+yTmDnfmx3cZyWOp+2bVHBLDI6szPv+Z1zGJ78RVR4QzywTaGtMkemnQyV4YszYL6QZVCAbFfrWzhwkk5jAoIOPUOFnEZ2rk4m2buQ/TpFX8lwFBt4LKG6+f9B32qxVaT5fRXBSgotsBxHztFsn0X0dUboIIBE+guRl38N7fqGDOYdBbLQfCIqyrOhDL4Q332MsPP0NsPbxAkfgit1klA57dLKtxuUpuweZtApE0uTHIJB+XyboPPQ6mYPBpoywcgNGaJCsle5os0btubcEsDMLGVphY9ute+624DKINplkXZYqlNuP/8NO0eM20gjJTMVydGPfxbzZ+DqyfHkE6rknrOEDtpYlg1Q8y/OpfafviGb8IZQjjLuMwTCL9Y/4ec5Hnq+n4aRHj8eEvhKVnVdI9lnSs1Bcfbx5gRuq7ANi4SM+wPqGBG2g6KRLWqfq2iqzGGu67htRjaXkSZ35vnsrfK86iGevOk05u/tDe3O88oLJ69XUDEO0fUFpk87Ro3izEIifcO2d5pdgBd4ffCil1NUtNMIn6sKwf3x8EwOM5ZSUWjRKxG7Y1wjruMH/T0kuUM4VRSpMQW1is0MBZ0yvY1DJlSeAnGivm29W1vAW4Uan2jXKYJGtUCBf1EilP+WksoTIZwbBPdyu7sBmmH7r0bw7puy4BKwWUAsbCaVdZpMtEI0cbDkm/A5FOi3LL5KoeVsx7UBwZ/QZSvgQ8ErlEK4IzaGua7rI8JrDFMoyF9d7GHGzy1s7IaIueHVB+x58riY9Z8ztjEIp7eOWggiPcygsf2J1FWr9P+3ho+QZzsJvB7mtgUgKQezM0m42ydCuDwg4dEWC/5o3m1EZYzK786PeGXrIhj/FBNBYOt6lINCK29bQXSQ42r7vPSVN1MdtWd9uYX03KsCrKwRuOuN9uOcT5eGgV8isHmtb27SPWn4yoXBGGy74rJ6rNDCaVOE3g1r26Klxqbf/zz3RZeJODhhW0 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a849e023-7022-4629-03dc-08db40f0ce48 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Apr 2023 16:12:00.1642 (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: ykdqqNC0fQ0C9b/n0u3BxFs8lNUWQeZ3hiGwax0qfBBdV2BI0hEJE/IceGJGWfQS X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6046 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. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- 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 Wed Apr 19 16:11:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 675913 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 16D59C6FD18 for ; Wed, 19 Apr 2023 16:12:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233170AbjDSQMm (ORCPT ); Wed, 19 Apr 2023 12:12:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233409AbjDSQM1 (ORCPT ); Wed, 19 Apr 2023 12:12:27 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2056.outbound.protection.outlook.com [40.107.237.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AF694692 for ; Wed, 19 Apr 2023 09:12:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=e2gdzuPiPGvLbAULbJRD77GDLD3HMF0Fr0IoYH4drYIf94FQynVyADSzBd5pudi7H97vgP7SJGvdVllsk7WsTyB6ix3USs+5AxtqBt+IRe5t5Baa6Mp8J83tGYrWGbHR++UhUmYLJQe8vkEqpNc+X3p2h/p8jO3bUtC7EnAoIfc+1AWgeWH4KAG1eHYN3dzx7WThop4FyemU+Hn3NGrx1a9+HOHU0IRiXjbzLqrOszSRiCnjPGC7iIzA3/uURlXfPHvwQLWn9U+C1RoAwhCpgjH9HGM/9XVFanRb9bb0UcJvqiGOM/3as08MAg5I8KYPTLNRR1Y1dCsSu1fytbf9xQ== 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=uXROLBZWq41chf0UWWjKrlZqXcyr2NcZSeQQ5Hphdt4=; b=DTB8rrC9chuqbtnsJlWkr0VZ5O6nyozhW0O1K0wCyJ+xxXTapXuIlbDS92QlY0xmzRoYGvhAB/H0X6uyKvbW31iP7IfCcX13QJtiFKbWoPZXHWbS2R5gZN3wxkxa9DhSCY5otcM8s++WF1uUB2v+OI3AxXLFbWc3lNMgQ3w0EGS0sXwkquR2pkF3Al8iglLxJh9H92eZTtt9DpKwn+E0xaPS4+s2LYBWrM8/k6rlN+Gz700jrcAn5FFidF82d50LQis4Htc2G6Oqdg9NIbfagkT4bBTk6wnoZDgFoBOdAU7E2IsIfrgtW1EWf1kjn6PMmXp1bnNcSx0qergDjR7R+g== 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=uXROLBZWq41chf0UWWjKrlZqXcyr2NcZSeQQ5Hphdt4=; b=Ik35qUtnrQo63VLUyOTrTMHHQEAH0Mz+jD1YTznEY8c2QLXIQqkuYjJEiXxtqY0B6OCXlIKIUxvosc8Y204GksMq/D9MUfbgXGNbWgtnIOzrKccT080iYIYemuyONfo3q3lWC1vwTcmJhZpEJdrU9pFp57X00tSHGNXJuclMk4Sp+as4v84kqkTuoOSVPl0ZPTEb7WzdFjqR50VSI1/On8YmPHObUksxGlnj6xglMFVu0p97VLvvMtYINUFzvomosKoAkP7p8HLovXysBkafrulkXv/ysqRBjxa4GwjQh6rn/6fvpP9LTulUvmXKRNiAfCcZw+AoTEiSAR2BebNHiA== 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 MN0PR12MB6342.namprd12.prod.outlook.com (2603:10b6:208:3c1::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.20; Wed, 19 Apr 2023 16:12:03 +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.6319.020; Wed, 19 Apr 2023 16:12:03 +0000 From: Jason Gunthorpe To: Lu Baolu , Christophe Leroy , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Nicholas Piggin , "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH 08/11] iommu: Always destroy the iommu_group during iommu_release_device() Date: Wed, 19 Apr 2023 13:11:54 -0300 Message-Id: <8-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR05CA0177.namprd05.prod.outlook.com (2603:10b6:a03:339::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_|MN0PR12MB6342:EE_ X-MS-Office365-Filtering-Correlation-Id: 1d768044-573e-4e16-4295-08db40f0cef1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DIKW/4pLKXCwL2bMw0GdK53xznvgV+ZM4zEbPV2DpRydBNQ8KIg7riHSv/X84+TTZRd+LfPvIbC6IJoj2eWgdrnPVvqcrsS1rhXQqr1QjGbuWjUCIcE/MIMmzhylRe9VMtc8nHotENmWdjXxPMa13RdPfXlTP/2KKkL+bcMOa3138N/04OGwHeMzp3sWWgaU+IKWLzevGnfLp6LDRctVW2NTX0MDQ1e4QTAPNYSnkmXNqsAAYPPYC0+rMtfS51xEWn6kheDVBKgp4WMaAc3iOVDkbiIRQdG+O1HsgPyxkGU2uVDqV4I0FR0ld7GR+l9mpdL+3HwOYrz/65TrEHS1b8AMTdzTdYeKmBfQ/8tbVkUY7PqPz9AxH1MoNLx2q4wv2yiSIl9IAYI0R84lWtAFA9vX1zX9ZUi87YgpUVkSw/SNYaVAlbaauO3LT4ZSZ2+mk30WytuKUmYF4mBbV5QPqJeX4Otylx1q1X5FkDZkDd6cve0bhIZCnkvOXxdMn+tZAIqwQalCxH/sE+B3eK7ucebM6ivo1vGVhnK9eozpuDFRPYgCsWNsYfJ6YJM6gLV9jfMpak1LfHhJdssUxzG5cGBmbE6kYKPg5LlqcdUzBJRidJKv+Jl3xNGQgPywoHwv 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)(396003)(376002)(39860400002)(366004)(346002)(136003)(451199021)(6666004)(6486002)(478600001)(110136005)(86362001)(36756003)(2616005)(83380400001)(107886003)(6512007)(186003)(6506007)(26005)(38100700002)(316002)(921005)(66946007)(66476007)(66556008)(2906002)(8676002)(5660300002)(8936002)(7416002)(41300700001)(4326008)(54906003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2NKee0phDVWu2C9sVDaLY0Qq1YK1KRYYghXDntxBW1C97Yy+Qsnhh5I8n7iC6um7iPT9NUMyV6WRYiDHw4Lo8qdXu2Azi2hM44/AJxUzMAWQbQKAhGq1sCSAQUTuYAJtjjz4ZNk0LwYlZf21FJwiQNMmIkfJ2LuKrj+LROH4OjIX5IX4CsKOszP0XAkX4MeCH3xZYODggNErjX21MQnvvJWIjI9E6ly3Pg+mHg5Umwfa49xNZocXe0eFoCk8Y0VpW4Re29frcl72J6y337m0YEpVRVupVo1G8bGtZRHJZWAJ9I+PLZ82rRfTiQn5pEgpzTmM32W4KnBSCJPpB+RRwwE549jGbMyOshHs2I6ziFYpo69y11oe7qpxd9eND/NrYU7SB7GGUiOK6k0l4+8Te7X6nuohIEpFvYFEAx2sZobz4nKSGfJEgUSHOJoKCrywJGbEdVYkm73dFXYlRauvuObCr4nJZiQw5bU2Fy/FedJpPmxewoYXPYSqjYzM1i3iD0CmogKxggnks9hPl0bZjmXwRPtA7J8Cumf5RGSBIvH1DM4PEjL0IKCJWbdXvfN5hB0Y1pwrD0l3GEXhX4HmqtAhV3na6isU33GwZ2DNQOwG4yLdX0upNLIZUPi4oSwsNEIyJzxichMtu/0Mm/kfKWpVEUpHmYMc/xU3bnuGZtVt0NIZo7LgckLOku9rZIEWEmsA1xQ6aZpR9PrK7XSLPMJHKbHx8DVS2unCIMevwXw/M2FHv21TGK49+A5FCsESqtxjDbh1uGvVw/dXGEmUk6GeoyxMwKLdtRecKw8iRkSBs9EW13JMKuvkqCXqcTgpcerj8GFNg5nfvlStsuswoQb3bz0Dvets3S4QDn5vDEGUsPBgzMyd/9K0JA4voGlZKwA7eim76MUWuFbuwxoMZ678sjU/kOvzpDd02SqbExNpmpE9rFfdQB5B9QpJOeggwCjxJ6K8/4HPbzk8of5G7JMy4qc/j4vcZ3HjvtU0UNn7WKSi7hSGuQTzkHYG4ObmX7rotY29UJ8ebPbM8h6vVWidwlRt67Owcedq8czq0serK9tecxfIBBGaIaPvB0aycGEsywpJm33mI6hFgT+d73AVQm9JiZFXT23N6FaxDxJDFKSQpjptL7CTPerEhyMzE5r4T9OwDIqkY9AvfAdVQhutRSPWBLbK2ZzkgRuew4QFSZPxl1nXMMz3zLhjyDvsDsSbcNh4vynHlt8YCclniTJCoBz/VkZ/DRQw01sEOUtnf0FA6pSeKRf7QT/OuLv/PAspxY28mE7s5vKuliv7CZqZX/RyYr7UM2618xaOkms4F49ZEb36FEmePUEKC6BYASnStB/KRV7+MJ68IjPpcyEdqCkX9DsAWQjCFFHkggXKfhQdPNX7gz8YP5+2GDKsLpPabjxU4DhXUZ05+bPenTA4ozjNMpvsrO8F1Ue4wdYLWBR8DOzEBY+RZ+jSjwveZoUIqpouhsoI/7H9WKMplaRl/vge7euOPVGopuEYBDXDCUBAf3eAFSupu6qO6yRFEIUyTQ3bsdLKup8a8/P3I7i3DzErRGBXYjC6hroUwwwBJmARXtIoR1Bn4FfN1GvA X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1d768044-573e-4e16-4295-08db40f0cef1 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Apr 2023 16:12:01.2197 (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: 1UmE3jrkFKhbNLr+EnP5Oebi5nxl8HqYpY54/AXJpGIiN242ykJG0z9eq9hSHWGR X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6342 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. POWER creates iommu_groups without drivers attached, and the next patch removes the open-coding of this same cleanup from POWER. 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, eg for fwsepc - Any attached driver via a struct group_device This fixes a bug where a fwspec created without an iommu_group being probed would not be freed. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/iommu/iommu.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index dbaf3ed9012c45..a82516c8ea87ad 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -569,7 +569,6 @@ static void __iommu_group_remove_device(struct device *dev) dev->iommu_group = NULL; goto out; } - WARN(true, "Corrupted iommu_group device_list"); out: mutex_unlock(&group->mutex); @@ -581,10 +580,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 Wed Apr 19 16:11:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 675082 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 78892C77B73 for ; Wed, 19 Apr 2023 16:12:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233521AbjDSQMm (ORCPT ); Wed, 19 Apr 2023 12:12:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230522AbjDSQM3 (ORCPT ); Wed, 19 Apr 2023 12:12:29 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2075.outbound.protection.outlook.com [40.107.237.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E201C7D8E for ; Wed, 19 Apr 2023 09:12:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FfkhuI7YmdaEPmPZ7kXjTXe1s40PKs2DOW3K3bgZn+ziZ+jJMaPjyM/bwCIUrQ3s7x8g37nqez7/3rl53gtzdAlWp8L7UAH+SwefTk3DUySVKhnBke9v4Q4NHXLn2S5sDVWsgRCTAFjDprTqwyzVWuXAhxaqJ/CHeH518AaAIHzpdEWvdc+7rr8/41TSTDzK9Qo+EnSC6kPWVbHO+m/Ta4k+WCfYFpw/zkIj+H5Xt4b0wZ2vc5zodmfh9ySQZdZL4vtSZ5Y1z/CMgYFpGn/UZxAeS4yZjE/SyhuYAlKqudm0nnfoQOrdDYydTtvggnMzIsgmabG7QDvkpgXMfib6JA== 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=FMAOlrhOsAjRHZPZVr7iC5jegG8NoY81ZiHzpk9P7no=; b=bjFF+fCiu60+XrvWlSZpTjxRuV9x4YReR3E9uZ0wNCenkhL/8iM2mI/ecgMa4cf0HSZYc35ldCSPREYGQ8saODkMELIFCEj/+T7Him1odkv/+p73fea1MthyNaiMMJD1GS5GWsJHjKM9Z1Uk4MV2RvyGxip+/VxfOePsj/PvEUpWf1V85hHKgu/dsB0A+8PpClUwvZHwuw2UagMsYTkrtOFpBrlFs0j8mdWN8wJ6y8MJCtnycaXFrNFcxZt1bgS1LEGiRH0EKcbbXLY61LzskTbQEZ7XCEQzBxZgQ1SmfCGyDqkNVoxDSYVVH6D3cL9nx00EFyrPDSgdN1IioiS8Iw== 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=FMAOlrhOsAjRHZPZVr7iC5jegG8NoY81ZiHzpk9P7no=; b=So9NzKOdYpsdUofKNB+c09S621WOhG7ajePS+4LtsS9H0AN+tyzoj8RN1FaAzCeAJl0JgeyGAGqhd0Yr/YucGIbp/jLRS5rmCRShDVNGoM4Nl9MSnLsykkjuwKTcynyh7uFVz2ySwf3YDdEnGKk74vdZrqmuxfsYCW1p67kWXtUxCO3lTuddJkTeWZxt6H9LYz06VpcBj3rp87+RS66Xkprmz5skaA/8elJddMmK4vDlIoIMqvbD/PuO+ak21/rTYsLvgm3OufVdNiOZz+Z/FJxlJPjss8F8VB00DYyzA4FnETv5eRmxrMzMK0koHiv9ODC/iEou2CBpwNaM0C/q7g== 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 MN0PR12MB6342.namprd12.prod.outlook.com (2603:10b6:208:3c1::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.20; Wed, 19 Apr 2023 16:12:03 +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.6319.020; Wed, 19 Apr 2023 16:12:03 +0000 From: Jason Gunthorpe To: Lu Baolu , Christophe Leroy , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Nicholas Piggin , "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH 09/11] iommu/power: Remove iommu_del_device() Date: Wed, 19 Apr 2023 13:11:55 -0300 Message-Id: <9-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR11CA0084.namprd11.prod.outlook.com (2603:10b6:a03:f4::25) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MN0PR12MB6342:EE_ X-MS-Office365-Filtering-Correlation-Id: 712be6ed-3a1e-4829-4295-08db40f0cef1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: WUtcRPzg+XeChLLrBeb1xMNbfaW9V1xjxX3my7i89e17GApwQ37NaoyLpPTwyBo5cTNGNIeCxE50sN1Kp6LhltJlQE7e8kcb5eX8SizAH62Uj/1GL0lHyKUo9/cHyIVQPCbQSvf7rTcmG8aalrrPuoMRRcGWDIjxQTZ76c7ADw/sz/FIbO+gZN0l14rYYd9lZ1ZckkHOJ1R6Sde6jyuyABjAjROfd26kRMYUKq0hojluNBi/qqNs+EuHsAIChtrvZiZ5A1WkcCkVZzLn+q+/Lbooeb87b7abRe/owt475xDJ9WeNmaAdDswgKbEKEberFL4etFNvVRNYxxCHjkwcW75XeCgXULHYv3jnZciUpdKy98yc7C2jQQtKRoIK4PfYwLGaZqKc1EC5hmh1Ms0nf005aVcyE6BK1VF4F4UvfDZnW9Z8AMIv5gpkXSe5+Kj0auebmFRkEQ3kyhmlKkRC0Cgb8SgC6uNZaaqoRmK6MUmFig7YA4/+DRfet19zGrUmZDbbygFVNFivXHuLWofU2/H8VKpT3p4rtBNAeJqqab8dMHl1o2Yr+/nl6ODqXoc+7wcICYaohvPNKIhtAQJSdAKxYlLmMnBCuWvWwkduhK9IcGg8QwcqE6EeAwWCZ11o 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)(396003)(376002)(39860400002)(366004)(346002)(136003)(451199021)(6666004)(6486002)(478600001)(110136005)(86362001)(36756003)(2616005)(83380400001)(107886003)(6512007)(186003)(6506007)(26005)(38100700002)(316002)(921005)(66946007)(66476007)(66556008)(2906002)(8676002)(5660300002)(8936002)(7416002)(41300700001)(4326008)(54906003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: BU/mgQlNxq9Rn2uAFXIYR3uQYs+bft4H3kHxVEvEekcRw8BlXlvN2YK6X5S2dgB2hkGjNOY0gXIu1NvocdreIgeK2Sbdb7pf+7KV9V1VZYJuxbO/cRsfmzrY9M8JrwP4K6jU1reZalsGW3juatUFGQEeFmoEMZP846JwkZa0i1UKRs9jx1HbBSVcLKI0BOsf6uN3c4d1IBMTBIqsB5cpeaN9nNr35eWEqQbGPC37S0Eeg6I1ODG1O8JXYQQ+J00GrhOPS29ChysP4OuFbhZqPAgi+/PvPYZrrRwOp1IQ23sqER21LDCCeeIhHt1qvHWcJIAh4G5yCIA7wr4WWkV87LraDHIA74wk1Hml9yoB7p4NydImQ+focmIwd/YufULPJuLBqQcmOxtKdWmSE7wlazCjudDjAanMPTqUIOqhR8YsRqhuZhzNlFx3urB7bO1s+k2Y3uVLSMQU9ZIHGcG/8R1ZiBeakf/Rf0D0DylDmiVtvhXMLCMZr/R2EUJbtjx1eSxqHA6LSouJMaUFd2lhHfSW+ZU/p7x6lxR33L+dQLt0sdjLMfeTfuXK+rFFVGH0BQIzidWVlYwj81s8/Rw/yjzw7fxV6n0YFqB11TQ4k0yguKiWbVxVD3c1iRacZM6qaWtXj7k0VL/gVDKTcUwq56fkfuEGVYhyU8PaR7BQRt+7QJdNGcfCG8ZpIIl0ZAWU2s3trg5mbnMxHsJyks5SWnAhHTyP7nfhTbdCiCvKpnewP2MJARFTyA3zF8awlKQ6gmjdaEOeO+1Kn6+/Ej8eCjp4hptFrld81Z84VJs9+uG04i9NUyQnyeHKyH4oP9P/SKFg9VEZgHW0soEz/6V9Vro5F9vV4FJ/ChcYKVuXGHIJcQsYN/QsyjciZabrMBpE8MEuzW/AUeA+l8LBfolMbLQgKviEaQYdqKOD3XLqlXj9C6XsJwyUtBI71GD50pM9nf6lVmM9eNYuUH2z7UZXm0skJZ5Dh0ElXhx9zxoZXwPIXVzGTsry5l5ubq8h3eZuBW/wPQScx2SrViKa5++VsAE631cAGUThM+syrA1C6j+fFJuUApWDzANdID0Bld9bGf5Dn9bmVjWrMPXasjnsUC6G8SSRpqT5mu4++SlFS4DEMtkoC8KFg+6momJ8A1wHXDEXFZHKSWRfJymX2YX/z4nmCDMSWXXn1WvyN10dKnPArHDiIcZh8/xz6n6RoFMg9ICIkQcwanki49g+YzlZzGgY6PnUykHLcDNkTJl6J2w5K/uRt7e49cOcON21FMuY/tCs+o8itoZOA4waFM1QYw2ENNFFP+hmTYWfuCkaYTLaSK4X/P05G1tCL0WLegaw6kwntTh15WIctql1iF0q+oWLMMNrIihDD71geVsidWhkG9FfL3rtCdZVGm+xituSAytwc7ZVcqjot6Wv4DPjBnNLVIuFgpNZj3ATVXMt1K/UAGiLFAxmn0LPw130XUQ/xv9njKGuWG60K/e9rjb6QyWMOYcyW0kxxQDUzqn1XYrAzfQNUveKT9zGlZtLT1wDZrXCqV5qbKaCW9x9I66/M4N746emp+LJxb0RzZkxkuDbkZIgYx4ootsk8HzBKdVt X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 712be6ed-3a1e-4829-4295-08db40f0cef1 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Apr 2023 16:12:01.2501 (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: mPf7828p9oFa+7OBdDGn6+nav52rZ3o4dxmpyPGr1RVP+WnEVUnhEAtGH9Vm8lby X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6342 Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org This is only called from a BUS_NOTIFY_DEL_DEVICE notifier and it only calls iommu_group_remove_device(). The core code now cleans up any iommu_group, even without a driver, during BUS_NOTIFY_REMOVED_DEVICE. There is no reason for POWER to install its own bus notifiers and duplicate the core code's work, remove this code. Signed-off-by: Jason Gunthorpe --- arch/powerpc/include/asm/iommu.h | 5 ----- arch/powerpc/kernel/iommu.c | 17 ----------------- arch/powerpc/platforms/powernv/pci.c | 25 ------------------------- arch/powerpc/platforms/pseries/iommu.c | 25 ------------------------- 4 files changed, 72 deletions(-) diff --git a/arch/powerpc/include/asm/iommu.h b/arch/powerpc/include/asm/iommu.h index 7e29c73e3dd48d..55d6213dbeaf42 100644 --- a/arch/powerpc/include/asm/iommu.h +++ b/arch/powerpc/include/asm/iommu.h @@ -205,7 +205,6 @@ extern void iommu_register_group(struct iommu_table_group *table_group, int pci_domain_number, unsigned long pe_num); extern int iommu_add_device(struct iommu_table_group *table_group, struct device *dev); -extern void iommu_del_device(struct device *dev); extern long iommu_tce_xchg(struct mm_struct *mm, struct iommu_table *tbl, unsigned long entry, unsigned long *hpa, enum dma_data_direction *direction); @@ -227,10 +226,6 @@ static inline int iommu_add_device(struct iommu_table_group *table_group, { return 0; } - -static inline void iommu_del_device(struct device *dev) -{ -} #endif /* !CONFIG_IOMMU_API */ u64 dma_iommu_get_required_mask(struct device *dev); diff --git a/arch/powerpc/kernel/iommu.c b/arch/powerpc/kernel/iommu.c index ee95937bdaf14e..f02dd2149394e2 100644 --- a/arch/powerpc/kernel/iommu.c +++ b/arch/powerpc/kernel/iommu.c @@ -1162,21 +1162,4 @@ int iommu_add_device(struct iommu_table_group *table_group, struct device *dev) return iommu_group_add_device(table_group->group, dev); } EXPORT_SYMBOL_GPL(iommu_add_device); - -void iommu_del_device(struct device *dev) -{ - /* - * Some devices might not have IOMMU table and group - * and we needn't detach them from the associated - * IOMMU groups - */ - if (!device_iommu_mapped(dev)) { - pr_debug("iommu_tce: skipping device %s with no tbl\n", - dev_name(dev)); - return; - } - - iommu_group_remove_device(dev); -} -EXPORT_SYMBOL_GPL(iommu_del_device); #endif /* CONFIG_IOMMU_API */ diff --git a/arch/powerpc/platforms/powernv/pci.c b/arch/powerpc/platforms/powernv/pci.c index 233a50e65fcedd..7725492097b627 100644 --- a/arch/powerpc/platforms/powernv/pci.c +++ b/arch/powerpc/platforms/powernv/pci.c @@ -865,28 +865,3 @@ void __init pnv_pci_init(void) /* Configure IOMMU DMA hooks */ set_pci_dma_ops(&dma_iommu_ops); } - -static int pnv_tce_iommu_bus_notifier(struct notifier_block *nb, - unsigned long action, void *data) -{ - struct device *dev = data; - - switch (action) { - case BUS_NOTIFY_DEL_DEVICE: - iommu_del_device(dev); - return 0; - default: - return 0; - } -} - -static struct notifier_block pnv_tce_iommu_bus_nb = { - .notifier_call = pnv_tce_iommu_bus_notifier, -}; - -static int __init pnv_tce_iommu_bus_notifier_init(void) -{ - bus_register_notifier(&pci_bus_type, &pnv_tce_iommu_bus_nb); - return 0; -} -machine_subsys_initcall_sync(powernv, pnv_tce_iommu_bus_notifier_init); diff --git a/arch/powerpc/platforms/pseries/iommu.c b/arch/powerpc/platforms/pseries/iommu.c index c74b71d4733d40..7818ace838ce61 100644 --- a/arch/powerpc/platforms/pseries/iommu.c +++ b/arch/powerpc/platforms/pseries/iommu.c @@ -1699,28 +1699,3 @@ static int __init disable_multitce(char *str) } __setup("multitce=", disable_multitce); - -static int tce_iommu_bus_notifier(struct notifier_block *nb, - unsigned long action, void *data) -{ - struct device *dev = data; - - switch (action) { - case BUS_NOTIFY_DEL_DEVICE: - iommu_del_device(dev); - return 0; - default: - return 0; - } -} - -static struct notifier_block tce_iommu_bus_nb = { - .notifier_call = tce_iommu_bus_notifier, -}; - -static int __init tce_iommu_bus_notifier_init(void) -{ - bus_register_notifier(&pci_bus_type, &tce_iommu_bus_nb); - return 0; -} -machine_subsys_initcall_sync(pseries, tce_iommu_bus_notifier_init); From patchwork Wed Apr 19 16:11:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 675914 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 7AFD5C77B7A for ; Wed, 19 Apr 2023 16:12:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233386AbjDSQMl (ORCPT ); Wed, 19 Apr 2023 12:12:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233397AbjDSQM1 (ORCPT ); Wed, 19 Apr 2023 12:12:27 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2075.outbound.protection.outlook.com [40.107.237.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93D854205 for ; Wed, 19 Apr 2023 09:12:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kI9N9npIIsGwxEj+GSBGKdOl9aJdrNc12M/IBSpVc2Li81sp2mkp87LPF/0aNvR1UQGSbukyQ+pjwZCy0bCdNRmYE8KH5OIBynhqdP/jiNBd7Bdm6nsfJlzaZHmdqbGM6oVFlvp/5wq+Xv2PfQ7KtbRqthMY0DXkJAxOlOH7wyBdieDdaDSzhAIF/v+g6YxkmOaN03VTJFcg/PdoV19ZpUC7Y7sUoBrvjomh0vQByDMxAIcBFhi8oq0rj/j45ezSQcDWJnL6qoWKPdiF5QTw/oxcSTCv7Sk0a84d8AKyzcmyWEAvPsD4vYi1ZZXTz0gWXbVb3csbKKHZz0U/XVgMLQ== 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=O6yoTcTxmlwEabQHB2MrRTymByLRYCzJGzpn5wU87Zg=; b=SNPGeIyzc1JOC/bwhvxAo9S8lMH2tnLjLxG7Dh4ypXzuHbaagdVA82gek2k/wTODFmWGQuS/ey1T3jj+sthf3vaTSABUlQ7QR5giHfNMXmMnFjdSLgH8g6U6ockbgnueSmt4agIFUve0F8JD9OKbKLEig0bmkPgeIlhF3j6yiHPUYkQF4HmhgD4Lh5bNvtH7BF6KPfKPv95pmgnmHTjbXf9BOsRCJti1AILdKJKIQscNE607vh1Y1SK98KRvQ4P0qs4XnV9fIV7hd9Yai6nbbJUvD1nsPVlR20O1teb+gzWSgX/rzIRsG09XKJA+L++eJ5e147oV4vF3HDQvkLSd7g== 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=O6yoTcTxmlwEabQHB2MrRTymByLRYCzJGzpn5wU87Zg=; b=ie7cqju+dXVqId1M8BmODfV2jqMFk0zsAtjbIFHUNSR9ti1YxBY/rXiRcBYZ7/L+s+tTpT/3EIJhv9+YrY/VO9yoMbPn0QWsKFrgvA00jAERRL1R5sdHqIgMZajPG5shkR8i0rhX+cZbtkcTAE+vpAf5BixP98RuwxsdA/Rqiv0TRCEdft3/4KeP4Mjd4VIAoNpMOSF6DlLEIU8RUlDSJ2pYMdPWkjm7Tj4TDV9W7h0o2R/jIHapvlbCDWe7cbaO9rB1+ATCOXH8SCEXugs+qw5PFAmNtdwzLISPrSNFROn8cFYavVusAuPUK3I+S8bopSt3BUjn3JfoBZfU7JlaMQ== 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 MN0PR12MB6342.namprd12.prod.outlook.com (2603:10b6:208:3c1::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.20; Wed, 19 Apr 2023 16:12:02 +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.6319.020; Wed, 19 Apr 2023 16:12:02 +0000 From: Jason Gunthorpe To: Lu Baolu , Christophe Leroy , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Nicholas Piggin , "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH 10/11] iommu: Split iommu_group_add_device() Date: Wed, 19 Apr 2023 13:11:56 -0300 Message-Id: <10-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: BY5PR13CA0007.namprd13.prod.outlook.com (2603:10b6:a03:180::20) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MN0PR12MB6342:EE_ X-MS-Office365-Filtering-Correlation-Id: 04094da9-b14e-46a8-ce5e-08db40f0cedb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bwEv5Wz3AmXBzdByF9FpEVDiw+Go6gqhWXN14twFPO9cIbv1FDZRledWRfINOwvTvGFee4l32fFMl3lxFiq9Uwg878A5TyLzj21gvueadhsWAX0E1jGv+Vuz7boTfdyjVsrvGCioop6seGD7IBIL4zHZTwuHMbC2A+qwHGK4DNB/JZgsZTF802YHBxaVfqegJ1iM2rTMSvFLDRpGLpbLgqBa/sVDIr6BEGajl/q2KOa9jBdkWB2V2FtxzCQEZqtfVQgRERLjqjUaSRlu+wfu637jkM9Zeti6VCv5RoU/MHcSuYuHTPJ3sttsfSDhmHFu2UlJGSDfy3mM51XsZXL4nkjN0CFe7RFFWOlKNjndYgJPbMzwGD5AqkzuuPZYdNBqaCpMxy3bFh8O/w2oN/FM9D9DuP3P7Coml98i6OH1Ms7OQvluoxsIE5S9WTHjwdgqhS3X3YYPTiPF4r9OPSmOCy1dEFETBlqOShqGpcPMfmeaSrSmFdLPlPGcOcDXh3NSux9vTIxej9U97/bLn8gYAoSxI6PYbTlD0+AdkjqlBA6r1ac5bioP1hQQVzGd4fjJlWULKUEu7X1DLz9fgF0l4Tgj4iaR0VvY9us+DBxI8nk= 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)(396003)(376002)(39860400002)(366004)(346002)(136003)(451199021)(6666004)(6486002)(478600001)(110136005)(86362001)(36756003)(2616005)(83380400001)(107886003)(6512007)(186003)(6506007)(26005)(38100700002)(316002)(921005)(66946007)(66476007)(66556008)(2906002)(8676002)(5660300002)(8936002)(7416002)(41300700001)(4326008)(54906003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: urQX/wxFoWXo6jZOaNp7Ov6BkEWfCP81ogw5obelhg55aGsfffpTx7/NFF9HcQ1nnmr+XBRS3P9dDJme0efR/mmqSpP2fMVykpTlnHdU+bSGZid7zW5nuS7qRWrVlejvaNsPVRkEi1LYMLCM9l4Ay17G3lyzC88xiqyCBMsuU929H4Kc50pIERbbSVg+O9uQmy1rdrP0vC/ON0xjyCRTmY8vWhoiRBgbpxuXyUqOTWYaGsBWxbn0q9jV1OQ+QyzRdeR4zEfrN70SYvfmQSV46jzJ/+fPwyslZJg+GfMWYJwSPkEG/1fTc64C64RhpzO7qV23SC5jsy6DaiURLgGdPQod8v+XZA9Yqr4QN7Dm2yxWORfBrSo1/LCHPZST14xQYomHbn13Topy11xtEUoRGV5ADnAt2bNTXt4B5/TwXpvfyEkWdMc/SLdjKfXlpMbTMymk+QiJjMt4nm32kNSKackrkcp4xFErxnahh+cRYqCKcSNNm/lN8g61OfVEW7Q7BAWycCDeyLMxEGtnLqlFe+1uKVxWlR52lwwmUHVTwkJhCPxkbk5IyQAn1ztEl3aRva8zxehtuMtOgsQ4GymW34hNREnnjJyanJvJsrpfg13xNwmPBT6s6yxVv9OAudsfg3ZZFA9DC9+BfEi2hZzoyjil2H2oju3nD59lF15KvS7EXaV083SZlFUtFH4Tutrqw3mfX9E7Sug+NaPaQegjrHaOTlWYoZU2O23R6Zab07hD+MwV5WGE9sWd7GjNiqoHr1zXig0D4JBx6ZBNpOvjJ0c2ToH1TDW+4Q9J8187h5N8OH3zIfGcelodKMMk9OXlhBP6Q24tAGuhNlHkILSsORHr1yuJL0FJw2D6+d9ooXpaQxMyr50Ve/kG+vRFmC4xAIX1cTZnix9mdqG+Jqd1pgtR2DnsnI+RXf41WSxtfr4Cr0v9IUWYlSD8lWlF+XE/Biq8vV6DsOvNSIVqCrmEkk0fJZqERgCHl5xj+UV/3ZjGsG52mNbt0itTsHlClzSZgVjUHSz0y5yp0Ha2lP3KVcBhi9QVquzM73OPxjnxTWfyhx4PbKMvR43ShIZdv1YcnEs63QiP6wRlockMfcVENwSO87EPiN2iTMxdMuu1r8KceXy50CezUWvLSK0PGDXRwuXbGcC3PQcU4WMrM9PTkGEKv/rYvTiJS1pZoXfyJAh8kf1mwoAIiBynOLPr9T/zTJrhjh5SCHHJkGnmXtxLre27aPU5ABuQRVTTG42gP029E6KQZFOalukk8m/wrU0LgJKcOUMT4DBSw5hzwx8R/Dgch9TIimGsfjF3e28hyu/o8vu8bi4a6JFDzQf0wWtKcRky3JhF8C2RT+ELJYVjuHN/ifGeLBeHsBSirQ2dfp+VoKPpE/L2HMJrNdhz49IhD+WTQVggqBwHLrEbXvpCipR4jmwiJk+rMgRCotp0nBaNzL7eHI3sBPFJaOmIxryUXVNFYW8hbnBki9QIskiz2f6+v757sgRbiYQYelqLV2uvRMMnCYL9wfZ+iFRbeew3qFk6525ARzPLoMuwDW3PcmqkFP8RIdmpRASZSiQAomTOXCpcEiUqCgWUDz2XsUEQ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 04094da9-b14e-46a8-ce5e-08db40f0cedb X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Apr 2023 16:12:01.1422 (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: 59cpjBG5U6OJWjn0eYNYiIaVV9dCdr9WhGsT06quSnkqGPcAKXThMByCyQI3GEub X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6342 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_driver(), 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 --- 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 a82516c8ea87ad..5ebff82041f2d1 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -128,6 +128,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 = \ @@ -427,6 +429,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) @@ -451,16 +454,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; @@ -572,7 +576,10 @@ static void __iommu_group_remove_device(struct device *dev) out: mutex_unlock(&group->mutex); - /* Pairs with the get in iommu_group_add_device() */ + /* + * Pairs with the get in iommu_init_driver() or + * iommu_group_add_device() + */ iommu_group_put(group); } @@ -1061,22 +1068,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; @@ -1107,17 +1108,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); @@ -1126,7 +1121,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 Wed Apr 19 16:11:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 675915 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 C9194C77B73 for ; Wed, 19 Apr 2023 16:12:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233453AbjDSQMj (ORCPT ); Wed, 19 Apr 2023 12:12:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232626AbjDSQM0 (ORCPT ); Wed, 19 Apr 2023 12:12:26 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2075.outbound.protection.outlook.com [40.107.237.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0AE1E3ABF for ; Wed, 19 Apr 2023 09:12:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=k3wim9rOk9KyxqVLuVMW3k9gx1Zf1fpIFiC0xx54Q2Dzt2eId5YMVtQl40nr6uDO+j8YBksI0Z/lm0iw03mx7yxrkQxV22USWLDcAQyGyCU3l7xINrVBzJcOSxOjDhxYc1JWnU/8rQyJKKnhF4UvH4/rUvniy9EiylEkXhYTtA1J2gT5lLKiw6GyMyOF/aVuBKy0nqoHRCyUjCBQ1B4Z5ZP4fPB8jfQ/s/K+bkIgierg9D4LGCPF7p0Gz6IK1SDkjaY9lqWwa3SIrrL6rj/b83QiUQImgGd84vNQTWbz36z+uniYu/bKmX+0liFu3sLJXl9VjBJn42UdUuYmPy5iUQ== 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=aQqb0UgFmuJH8irX2bNc4c5jSYqG6xBHUQ56qzVTFPw=; b=aDUiblCq258p5Qt0RPr2sKMpxcoB4rLqtJf23bbNDROyECVg2F5cRdCHsXZHLytpKcRC5dNWDsvTxg1Y9ne2U6ojn1mjeQ0hm0h8KdmiPk1J9/FFvpgOzxQLTHBDngs11icmA9hFdptolVw3tN/PP2IkmpTWzIeY2UPrOd9AflhE7vbwYXwZ2DFMoDZ+FXn9XcMIOFNuBMzmFUTtbTEov5JoOx6yhN8o1ZmR78ICNXOcXtBp5jQqIounSlolLNtkFcqt7p+i8CrUgTLNc68X2tuT+c/H8sYgmlUwT+vd/cNb3CsFBt7ZZjPs5+ZtTBJ0VU/55iuzdDAcWvFMDWvzig== 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=aQqb0UgFmuJH8irX2bNc4c5jSYqG6xBHUQ56qzVTFPw=; b=gMi0pJbrbphIWaZxxEg3MQXejjh5EB22UQNzp0f7fiWLSW1HhJNHRzQQ0/WkGGbi+MjDeeJN0MR8ZGWBcducNLeWgMyjMN3e0wy2NCTbcHBz1YAqIY1bOrnLbKNpNQ3E9NRj+pikU/DItgiJIMHu2+6NXrvOwHxK/k4KqsjV16pkaVJzfEy8XZrZzNuVYbW8Z4cbEabRo1BtjxSHV+xeEsOky6WSa8k8GRU1sQrBGf+lTTW+voyT16z4XAz27ugJt//W4k4OAioWQBNrCUxt9wh+/nuqLd6OW3/Ox9CqvFlC2HlXWhOjqQ5uIcAshVhcFxM0im5NBXAl6CJirygUSw== 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 MN0PR12MB6342.namprd12.prod.outlook.com (2603:10b6:208:3c1::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.20; Wed, 19 Apr 2023 16:12:02 +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.6319.020; Wed, 19 Apr 2023 16:12:02 +0000 From: Jason Gunthorpe To: Lu Baolu , Christophe Leroy , David Woodhouse , iommu@lists.linux.dev, Joerg Roedel , Len Brown , linux-acpi@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Michael Ellerman , Nicholas Piggin , "Rafael J. Wysocki" , Robin Murphy , Will Deacon Cc: Kevin Tian , Nicolin Chen Subject: [PATCH 11/11] iommu: Avoid locking/unlocking for iommu_probe_device() Date: Wed, 19 Apr 2023 13:11:57 -0300 Message-Id: <11-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> In-Reply-To: <0-v1-8aecc628b904+2f42-iommu_probe_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR05CA0167.namprd05.prod.outlook.com (2603:10b6:a03:339::22) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MN0PR12MB6342:EE_ X-MS-Office365-Filtering-Correlation-Id: 56d559e8-7f11-4551-0cdb-08db40f0ceb8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: TO/quZaleEzdGIZUYgKK96MbmNSuG90LyxpnZJNPO3f6jQQdB5vmfKF1dkukTzVIhwTKjLnqjT/xrBMlDVScUGhwwnPpGHBhYJcDsMPVmfEqtAiPzSi0pfm0Sw3PI2jEhBuJd6lg+JbHkOayN8dIBwrH8n/Z9TGCnj4dlhxcBT67ZkbAOd+Yq1zaGQPK5Fi1zy+yR+vR95mvNmy7zHDuoKb3uUlLybVbkw6oHiTOVoq0oDiX3IRJBZYQSJB+bKo4qgMRPwRPlsFGBGUpaV13xu9+gBtUYyOhVIXFE2hXhCgQlX9AeUnSV442kBtgMmDs/CcSOSGpZf/iz+0UhabN3M6lpCGFvIyA/GZMtfdgnmQG7Bfqo1UkvCC0742k+jS2TxzLVLHIPHw2meq80wb03jfX3NVSifBcupqPNU8TKVJiE+2HW7PnIeoBsRd5FtPI3rCXcLl+2aCt2A2jfVIjzdBifyISUQxV3RfTpGr2nTFiJvp6eG/RdjoA6vjBSDGhU0a1wXvoU3bVhu3mT/AEjgxnNEIjL2QioBBOvyHHeOuC9n107CBYoU/wxCUC5HZ+XR4sThw2xf2gswTcIwSHE5o8yeZpgTGuEW6N2eHwAVs= 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)(396003)(376002)(39860400002)(366004)(346002)(136003)(451199021)(6666004)(6486002)(478600001)(110136005)(86362001)(36756003)(2616005)(83380400001)(107886003)(6512007)(186003)(6506007)(26005)(38100700002)(316002)(921005)(66946007)(66476007)(66556008)(2906002)(8676002)(5660300002)(8936002)(7416002)(41300700001)(4326008)(54906003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2ztZWjsUIPpPJ26h5meE2waatIcYVvtktRBRA5LDH7voDmz5pvJXvOK+MRugt0NvYL8ARu9mkuRS86TirP8FEmr1YU5vTHw7kf9jt4jw9aRO9J6Jl7/tvAb3fTLCbdpEyqo2l0miMsdY/+0RMJmDZx6PNw9vr1+NOxLkEBs7K0df/T5L6qF0uOdhegjLGQdlW9h0NkHXUaoPMmXeXz+3E6kUBEXUS4KddhQEZsAB/Qa6/OIsWXzcQBIShpjvowUYY/UliSSmCgqWDE9FTfKCZ6j9sRyfumgeROmFVCyuhDFRGPouRn/pjgYIP8v/MJOFG1KmE3uix8gTBKLti/i3wBfiCF7CP11O5rRn9cle60LJJXmX0zTl/bE8DZCuUJvfi261yVRQ5iMU7BqgOK6kjPxTrIxhM7JGth7PZovNu/F2SaAwhtl/PsrX/3z59rJHbJORWBObv9ZPCWdshkv55lyqpd29S+rG90k/pd54Xp5KhwyGyC+6BAXJ/SePEzVZC1dIa4RKaj3uc1ghWWseGaUTAm+EjUe7lvfz9I4iAcvPYMN1UVo1gHXX7NXwzzaZIvtv8RhSafQE96SWL+0BLMcXLi3clbgVHpaY6Rymgp4c56Wqyhv53zVuKyIn2h3uxf4R3c/e7ALsktwyQA6ApBPjoNvPEzTEftOjURvnUWwgP4W/br2AbeA2Mrhy09mMdBgQmEjruFFhElgW+VH+bNATP1kyVxNWN1/u/cKMvDSf5VR6RQ4aPf9+F0JOAbPEfkC686Sct7vbwPbU24hiElWz/C9hJXgVWYfXAfAx6pH6BxyYPSGfTGuU7cXp5Gvttbbd+cuhO9D3Jtv9aC3zEeuLXQxyLY1sFkKgEDxlkuvpKS6OUwsPAZQm8GTcAT8GnfNhPrEJVC5ri4fGS1+rIh3zsFtLLSDzGdO8bmKZwpeDc+qohmFvvEqY98hJdATzQy1hrRzuG3KyOqBxcra7q7O5gbAui41TnXmD1JIJuAIXeFqTWrL2JDs02jp2w/UW80JPdc+RLUxOX3ZNCt4kPJkmcnwvJ1986dUSGVEMApHvtnpxu98UP8DqgiU25lbL0s9U9m9kjGnuN8VofgRI79JSHGuIem+mCVnrgJKmAwNe7CapBib9zTllqRgU3mONgz1aPsz0Q645kWg8aCnGAV/tZsD4VORiyO02hlopmwWRnYJW/amoYGrpIZtRwVZclbWXCy0E/Mb/spj+aE8KKJSMC8NqSUqrTevvxyLw2hNItYUdTzUObpOZW4Fzx9B3X24RSJ7FQmPt6sIIUtgx/3rMy33O+qikSt5BFkcHHCKXgbulL7G/qgDKrQW26R2v5zF6IxC+kCJ5WauayZ/zpO/o+ru/72SaUmOsqzFODI/6ufceSZSEz+aaomT4cmTD7Xrv6U/b7Ks4dZEiHWXSyxdPaR9HV+35bMzbE0z6ZmAy5Fz/7N6fKm0Lsnrxh9z1nsfZuUB4nvs147T9ZaQh7sfBREOXx72ylHBoNyyjDSvpe2f1tf4Rbc4nBJeCdvk9WA1kw6lrVcNj9hQP0P67in7rnq+erzYHdYZCq0Vidua+z4XWrirMxxQXwrRc2lDs X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 56d559e8-7f11-4551-0cdb-08db40f0ceb8 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Apr 2023 16:12:00.8861 (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: GqSX/dpJVQ8UztCjzRUyVHxbWyo3V89BUGQcUxOu6K6HVqbL/O+gthwpdazUvyL1 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6342 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 --- 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 5ebff82041f2d1..8fc230eb36d65f 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -130,6 +130,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 = \ @@ -461,14 +463,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_driver(dev); mutex_unlock(&group->mutex); @@ -482,52 +509,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, @@ -1809,11 +1801,6 @@ int bus_iommu_probe(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; @@ -1826,6 +1813,11 @@ int bus_iommu_probe(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);