From nobody Fri Oct 3 12:12:55 2025 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2076.outbound.protection.outlook.com [40.107.237.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A52FD272E66; Sun, 31 Aug 2025 23:32:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.76 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756683171; cv=fail; b=U8fDw42EOs4EopPv/P448uqn1MhJ2/IEWV1eAMGu2eGfVbgXj1mZ84qlDN7CCn5rc5y/52xaMuywXFeiRZE3CLqrcSvQ5pL+AnlMSBp6akYSq6hXj+ruIFoLi9UeRIlKV5P4rBukqHQ13mL6K/7ZmG/ewAUj9KbkPdFLYMjpk2c= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756683171; c=relaxed/simple; bh=ZU4aAd/YjLc/j6TYx+FF+rTqQlFU1ZsNGMFL3+5AjgY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=AsX/c5ub5Qv0YwCiZl8a6REL9KKFYe3rbOTTKP+m8ffchZaUmGGNr7l92iav533zfJTB+cs5417PwPsQd+apoJ6dw53NkTRipgcFsO4GzHZQpWR3n1m5OG3ztIQ5Mttfn3Lk19owMnfol+MJkpkwZgNhU6yTeC/YR45vvw+yT68= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=m5mcKvxj; arc=fail smtp.client-ip=40.107.237.76 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="m5mcKvxj" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=QtESnVE2yaIFH16vArqS6gnczit4lp1tsaNBSX/+fEjDzBgp4qpTVKJAIle/Jps621ymoy36jZ92M4kIiaFxAOtHN9GzYnmlDaTdTRjkBHV7CYhQZf4QieTErREmuWUDNq4R3h3+H+38OeESqVIgmezx2qxeXJjoJ2FJtXe9gEAoP0Uvypd1NSu0mwdCieZub9xkhumuHHwVnGWUw9CS/NgYLpCPJT+GaJUu4+PCf7JjYKEG0+3V5OVgus8JsfCGF/ZvMyn0OUs6fDS1a1b2vIDF/KA3Pmlkcq/CMhDK3HweRvOx6frPkyI3cqgpn+jHydD1Hky6giaAQpNHEy4o2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=nLthldXggp3+AYls+acQj8/XBDMDbNe4wa438UBBpDU=; b=g9UV4/6mrBeOacKpmHGpM0Y34G6kXte5hGk+ywt4ANPAdyYtSOPLoG+po3NkMYTrd+4I3yS35z+fesNyoI5wPRCu9t/LeATs+IzvSgslqL6Ipg8vAavWGAyt1hA2zSDSZ9W3DYM9vNGK5S7m9r6DmufI/27ExvNCdM3Am7w998C/SbLbBTEnfcaOmRDXz8dIqZxO1M5nW2y2l+Bq2sGsLaVuNcOKC4MwpbcCLJaOB5cwgVjNPI99Hu+SSv9pu7UDKj22BxIFvFG5LWiuy+pmsIfuRL8wdAzgNPqjgf0kqQUWvlhezCVMP3zbSl+DVz+hUs6ulxR3NhlpkxRMGiuGNg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=8bytes.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=nLthldXggp3+AYls+acQj8/XBDMDbNe4wa438UBBpDU=; b=m5mcKvxjP7h7N8me8GKxFKL5eQsCMxPw3nbpR2s08bXPrWyyWCGNzzNYOdAIbyPrITQIy84pANJcQVWDdaSssR1kIP7R5UfFgUMZwNTrHjtovJRM5FR6u22cGhCgHg7bse7NMmm/OyWYMmLeMbdiZsSmICtDJdcYOSSmeA16w/T8uFrYtExjVgNR0/hAxiuhdWXa1L5baaxugJbHhsiAq3rUeZLQQZAw2g/dK1g1jiGhHX6sKtPNxu/1R/5VFpd5P20WLUfSPA6yA57NvOjaCqPya3r877dUdhptJSTq3IK38Xin3Ra/+EYn4b7LBP/WRA7uIQWsPQnGKvuF5AUJwQ== Received: from SJ0PR03CA0349.namprd03.prod.outlook.com (2603:10b6:a03:39c::24) by DS0PR12MB7746.namprd12.prod.outlook.com (2603:10b6:8:135::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9073.25; Sun, 31 Aug 2025 23:32:45 +0000 Received: from SJ1PEPF000026C5.namprd04.prod.outlook.com (2603:10b6:a03:39c:cafe::f8) by SJ0PR03CA0349.outlook.office365.com (2603:10b6:a03:39c::24) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9073.26 via Frontend Transport; Sun, 31 Aug 2025 23:32:44 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by SJ1PEPF000026C5.mail.protection.outlook.com (10.167.244.102) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9073.11 via Frontend Transport; Sun, 31 Aug 2025 23:32:44 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Sun, 31 Aug 2025 16:32:27 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Sun, 31 Aug 2025 16:32:23 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.14) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.14 via Frontend Transport; Sun, 31 Aug 2025 16:32:19 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 1/7] iommu/arm-smmu-v3: Add release_domain to attach prior to release_dev() Date: Sun, 31 Aug 2025 16:31:53 -0700 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF000026C5:EE_|DS0PR12MB7746:EE_ X-MS-Office365-Filtering-Correlation-Id: 881cf294-7bf2-4d66-6f38-08dde8e6aff1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|82310400026|7416014|376014|36860700013; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?bVzTaSdMStU/qCn09ERYjoJRbQHIcLq/Zgbh/iiFqxWoUouFvwrl/h/fI1xe?= =?us-ascii?Q?07gGA4S3a0k3SQ+KfzY3o/QvmkBAdlVLVCiNzHbQaDZgq9gvVlKoHQNbdqdU?= =?us-ascii?Q?+SLBTV3zbPdCSdxM5y9ab+uqVu7ehLUmzBnuK2LfS/yT7LTAcuEb63P+YWwN?= =?us-ascii?Q?Pk3pEWTa9Rc1br84TjCBa3NAo436TIqRyZvuM4+tt5vQ1O70duUCXirAznXe?= =?us-ascii?Q?sHoJP3lPitWsGf2JjdEpfe+8McgXKivi49hvQyAcM98onBcDEohOP9CA9F9R?= =?us-ascii?Q?uGJoB3gUYYcO/sE8KJfpv7Onl8Ain5XR8izyAGfutYkvtZqEiEt8VWc35lnB?= =?us-ascii?Q?6m85wJyjqLFUkz46sAu2fekef25mPsbXBimSxE2y7GYW+Iwp8FkjpuiDbD+k?= =?us-ascii?Q?kj2maZqYILCqYbhk34odJCAnp/phFivHXAM/CLoEIAXydJiQLm6zbffCrfpy?= =?us-ascii?Q?a7j5qtChhUyfo4i8+gsHZz8h2B9jwmrGr//FzI5cgJwI3DK+hj2CzhXlrVO7?= =?us-ascii?Q?Es2uuB6soqwv9xGPySv/FSseUq+e1LbBMaZSnNCrCWXJuvKcw07pkAhvkBog?= =?us-ascii?Q?qr3G8qg85Eg9axndPrtMKhVYIxIHT1J6hpjMzxyXZAEAXwg6visct/1l8tay?= =?us-ascii?Q?aGRKnzNVwIkEAvukec6F+GnNlmzCG/MacS8xX2a9kNiqjnkQ92vRtfA1WZlJ?= =?us-ascii?Q?OoKLsWvy52mVTWWXysR95oa09flja2ScyNFP0M5aJr82WBv2SqArrvlJo1bs?= =?us-ascii?Q?jM0VSrCSCn4fkoj2QUzBRWHxR8p02ksEuOgoB9RJ/jcfq4CYONYvQIPcEXDh?= =?us-ascii?Q?dvwuwS7FPXblJTHlM4zwU8c7uHkbtN8+EtCDK/u3zBpVZWU2Mz1Xh1yz17Mw?= =?us-ascii?Q?gAiH8n3V6ZHk2CqYYUfSwJWos/JAg38NPZsv481ylAvQN8f0K/r9jWheFCzB?= =?us-ascii?Q?X0YSLITYtb2b+HqnkNp3d4vUHklENF6uh/b8qry3z4lxvmC3cdqPWJE0J1s7?= =?us-ascii?Q?QMGWsi4Z3F2PIlc3gWV6J4U5stNLjw3hU1EddEbwcVxlHVig+rD0GmhgNOD4?= =?us-ascii?Q?fUFyt5G3nnewj5R/ofBD2eBqJ+Fe3Cc6wqBOnjLPV8LpHnd/9DclpfWizKB/?= =?us-ascii?Q?QtvWhDxW3qJmFlQkm9EXJfDzvYzMQfH1L7ol7s75R1WGlTHvdJfcL5i7Ulrc?= =?us-ascii?Q?dWyFOrZFiyHQ5Sh8FoEWg3AfGqf0Q6fuzD3HFTnN7IC1vCec2spQgBg8S0L1?= =?us-ascii?Q?tjdiBgEJbTgif4jOneUmHbCSlj5/CSdfgcuKSysmvvRxMc7bmJUwMDcRKswh?= =?us-ascii?Q?imyP47l7xpluP8GOK8FWNaOHQZx68WhleyHH5UgrD0w2WD5UvJc55Ov4zN07?= =?us-ascii?Q?QD3bFNv85i982gWVupUhMfV3VDF8f371TYqndsQYZVnYiNJNwr65wgdOzsvK?= =?us-ascii?Q?Vtke1MFcwttUPM4FYOUOnoZsFz8h4p/mqUyUk/7i+ufV+9PdXF2UHbNJNFRr?= =?us-ascii?Q?2RrAjw2OLBVP4tRUwEXw0aT7/kuXfVcNw6JF?= X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(1800799024)(82310400026)(7416014)(376014)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Aug 2025 23:32:44.7412 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 881cf294-7bf2-4d66-6f38-08dde8e6aff1 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF000026C5.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB7746 Content-Type: text/plain; charset="utf-8" The iommu_get_domain_for_dev() helper will be reworked to check a per-gdv flag, so it will need to hold the group->mutex. This will give trouble to existing attach_dev callback functions that call the helper for currently attached old domains, since group->mutex is already held in an attach_dev context. To address this, step one is to pass in the attached "old" domain pointer to the attach_dev op, similar to set_dev_pasid op. However, the release_dev op is tricky in the iommu core, because it could be invoked when the group isn't allocated, i.e. no way of guarateeing the group->mutex being held. Thus, it would not be able to do any attachment in the release_dev callback function, arm_smmu_release_device() here. Add a release_domain, moving the attach from arm_smmu_release_device() to the iommu_deinit_device() in the core, so that arm_smmu_release_device() will not need to worry about the group->mutex. Signed-off-by: Nicolin Chen --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 34 ++++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/ar= m/arm-smmu-v3/arm-smmu-v3.c index 5968043ac8023..1a21d1a2dd454 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3291,6 +3291,31 @@ static struct iommu_domain arm_smmu_blocked_domain = =3D { .ops =3D &arm_smmu_blocked_ops, }; =20 +static int arm_smmu_attach_dev_release(struct iommu_domain *domain, + struct device *dev) +{ + struct arm_smmu_master *master =3D dev_iommu_priv_get(dev); + + WARN_ON(master->iopf_refcount); + + /* Put the STE back to what arm_smmu_init_strtab() sets */ + if (dev->iommu->require_direct) + arm_smmu_attach_dev_identity(&arm_smmu_identity_domain, dev); + else + arm_smmu_attach_dev_blocked(&arm_smmu_blocked_domain, dev); + + return 0; +} + +static const struct iommu_domain_ops arm_smmu_release_ops =3D { + .attach_dev =3D arm_smmu_attach_dev_release, +}; + +static struct iommu_domain arm_smmu_release_domain =3D { + .type =3D IOMMU_DOMAIN_BLOCKED, + .ops =3D &arm_smmu_release_ops, +}; + static struct iommu_domain * arm_smmu_domain_alloc_paging_flags(struct device *dev, u32 flags, const struct iommu_user_data *user_data) @@ -3580,14 +3605,6 @@ static void arm_smmu_release_device(struct device *d= ev) { struct arm_smmu_master *master =3D dev_iommu_priv_get(dev); =20 - WARN_ON(master->iopf_refcount); - - /* Put the STE back to what arm_smmu_init_strtab() sets */ - if (dev->iommu->require_direct) - arm_smmu_attach_dev_identity(&arm_smmu_identity_domain, dev); - else - arm_smmu_attach_dev_blocked(&arm_smmu_blocked_domain, dev); - arm_smmu_disable_pasid(master); arm_smmu_remove_master(master); if (arm_smmu_cdtab_allocated(&master->cd_table)) @@ -3678,6 +3695,7 @@ static int arm_smmu_def_domain_type(struct device *de= v) static const struct iommu_ops arm_smmu_ops =3D { .identity_domain =3D &arm_smmu_identity_domain, .blocked_domain =3D &arm_smmu_blocked_domain, + .release_domain =3D &arm_smmu_release_domain, .capable =3D arm_smmu_capable, .hw_info =3D arm_smmu_hw_info, .domain_alloc_sva =3D arm_smmu_sva_domain_alloc, --=20 2.43.0 From nobody Fri Oct 3 12:12:55 2025 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2077.outbound.protection.outlook.com [40.107.243.77]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 43604270ED7; Sun, 31 Aug 2025 23:32:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.243.77 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756683169; cv=fail; b=r9t3UJL1v56vG9BUYeRLDyqBQRYyjkGABqQACbjCS7XZ72+yAyNpWvzii4j1va3ZIyF3N56IEf0uXveUof0tJQgDycBdqtgid0gMwMicYMXQ+vlV/bQn1U/hCoJkA4UrtsL8McNGAe3LGHgHvH+9s+nMaIVT8191M77atul92SA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756683169; c=relaxed/simple; bh=pQrvzfH1++pH1/dI+ath2DuH2+T2SN2qPv2HBhREq0Q=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Li7kgCn/v58Hw7nQoT58h7LYnyBpY9oBDN9OIHZ4gcmK2uWd9s/RMp3H+2VLKO0eq9TZgyMbR9DcAL605rmxMe/KZTaJJX1xO5X+aROrs9/R8/ljYTDmd0mZQ1B3izbhmw76A2wDWVMNR5H7GKf8mpHexWF7c1LmeXf3UT2SHwk= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=IWERACGH; arc=fail smtp.client-ip=40.107.243.77 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="IWERACGH" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Fdyh+YDBlrDAfpBObUOSTI+cj0k8Ck84UZiyhzdBke5XObp/+rLyheHKwTJjJOirsHSuj66rryGbKsoKCRHTQAtIrZeoY4V/pwuBnZ4+wMVaniwY/l75Xg9LtWaX5/kPsKsN1S+clwibF2pXc0PQVbvGib3nLkLvqeGu8FMgaokIwTPduOfJncrVA3wfcPqBoxgWDTE7offAzMOvsOqLKgJPVHRXuiD2H4cPt5s7rCse4NPyn1aC7GsZw9ly9UseFg4ehv+vWq+ZOomJYO+moLUXCaTr/4JPVyTEZinb5bsDV/cESNQH8UBoQBP6GveP9AuTOFk7Vwcrxma9pQV9ew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=++4zNiHar61MvfKvDki/q1sHSNo+h62pZZu+KLBPtdc=; b=WUAUo3QWkvsu8fbEG30XiQmAJSbz6ZouSpDZWA7KbsbbsTDx0smMZlMyuqqYxTMBcvjvSz90lB0R7D77X6LUqpGouABSfYqD+Kg/9lh8dd9DOnzGzHRCdtEXG2neLQ31CFsTeADGYFg2KOYxzrC0hh3irx73jqVFPdG1+nwNiZbgwnlb9Lt2TdQ3WpLa5Nhn/TbZaTM1Uf35qYw94yfMkl2Ailt2+f5UIPdk6vVV8R3XHbM5t6CkDNCr5HEaW/zEjpNCrkgcRgxgI+WRYivNLA0KS+XxTeq3HUYxt1rT89/wB2KEiyUUdoFs/fpmcziefd4zleMD2KzHhgJgmA9I1Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=8bytes.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=++4zNiHar61MvfKvDki/q1sHSNo+h62pZZu+KLBPtdc=; b=IWERACGHg8sqWYAN2cMTrvjuB5VRN5op/YqIVh5yNllaEwM8iK9zuWf9TA0MKAQLP4yB56kMWtMlAfX/IARj/eSY03Zb4wepBh7CAEOYa43MgaAG5yJrfgvCwpOVRLfduf3uozCUcZ7CfwPcBKIGkEVfn8ghIqvBurvH5hT5k0eVSZWIF2s+F51vZUlNGPiYmdMwa2OSVlS1Zh4PR+jamZwFuotukyHxx6FM+e5LEjp9Z5WwsCFQtqwl0OUmgg/Yf0Ln34Q/a+lPgoNcKvb+400p71JmTZ+4jbmVAjavafOS9XTTZ1DJPrjGAc4typCneIej3ecWSZldASK+65bhhA== Received: from DS7P222CA0027.NAMP222.PROD.OUTLOOK.COM (2603:10b6:8:2e::35) by SJ2PR12MB9114.namprd12.prod.outlook.com (2603:10b6:a03:567::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9073.25; Sun, 31 Aug 2025 23:32:41 +0000 Received: from DS3PEPF0000C37E.namprd04.prod.outlook.com (2603:10b6:8:2e:cafe::5a) by DS7P222CA0027.outlook.office365.com (2603:10b6:8:2e::35) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9073.26 via Frontend Transport; Sun, 31 Aug 2025 23:32:41 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by DS3PEPF0000C37E.mail.protection.outlook.com (10.167.23.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9094.14 via Frontend Transport; Sun, 31 Aug 2025 23:32:41 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Sun, 31 Aug 2025 16:32:28 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Sun, 31 Aug 2025 16:32:28 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.14) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.14 via Frontend Transport; Sun, 31 Aug 2025 16:32:24 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 2/7] iommu: Lock group->mutex in iommu_deferred_attach() Date: Sun, 31 Aug 2025 16:31:54 -0700 Message-ID: <9b9199e03c87c3cf8152cf93dc403a95c883811b.1756682135.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS3PEPF0000C37E:EE_|SJ2PR12MB9114:EE_ X-MS-Office365-Filtering-Correlation-Id: 0f80da3e-5828-4417-9dab-08dde8e6ade1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|1800799024|376014|36860700013|82310400026; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?xc/kY8mnb0+InFbZzniWLWupmEfNxWs9A0a+f6d64i1HveYYBS5de8lfD/1G?= =?us-ascii?Q?QChygISbx7U0IK7GfRwtPl6nrcBvIozHj2CnnhZwugx9mVUSJbiZToU99Lr1?= =?us-ascii?Q?Ci3QGTg1krLqGisih2NYjoWNGbgud9Mk6kVIeqvVLYAaNR84HEdQOU3OGYns?= =?us-ascii?Q?bjV0tVAhCOWA0aGTFPr4y0CSKoxU+WVlSv2MvuSK5H4haaV0oO5kGPxNxkP4?= =?us-ascii?Q?SIQFWSFzjbIJ8d4h4tsT2FMUnOcZbFnQBipYyKkMrP0R6uAGe6gvXS15nrPj?= =?us-ascii?Q?hDgVxZvZnmUlKRyPpTTmnEJR1ZjzwruudklrMwRS814MDROrn2DSWgrvrVDi?= =?us-ascii?Q?Wc9/FMA26zNrAtXQ59Z73sOrrSnlt0laIYP67j49nvyJGzkbpMygOsQYx1XC?= =?us-ascii?Q?QAMDa6gnNKvR8+DmDAC6tJZbON7tn93Ttj4RUBH85/6jUm5avgzTbaJV339B?= =?us-ascii?Q?Es2yUziit7UmCRiiCVThJKdxyRfA/8oZGdX4x2DGY7VqGVCLonLex3IxKcy3?= =?us-ascii?Q?VRKZ2oFck9S3fDKt+yzXe3Yr3cXl6NOnanorywuqM3EGkkeZH0bhj+KQAYRs?= =?us-ascii?Q?jSFzCjGvOfas4VaZCOKBaPmrZQ8aXgUrTIaphqN3SHMKTk8zkzk/mDBml17C?= =?us-ascii?Q?Bxp347sY1evjt/2Y+dg/SLqWp+fyn3upKubS4Psqkn3GBaQT6YSDqne67GyP?= =?us-ascii?Q?aLu3g2kpcq5oR+Zk5g5pOo7fu2+uQJDRxHe1oOz6WxJbA5uCGkfpO7dcmz9K?= =?us-ascii?Q?StopJeCNn0QniSG4ywFKCBU8MCX8EEC3OIvSDd02q10zENj6rVj6sVKET/Tf?= =?us-ascii?Q?r4K9NvrNYYKspFeJGidGbfpRO90QzZH8B2Y5j0hQvqhfmR7IyMU8aqOhqQrG?= =?us-ascii?Q?xlFP1tGrXvRQ2kctRQ7tx0II5TPH/Yiu+948/jiwmOmMHocj+QkG030o95mw?= =?us-ascii?Q?ZQgKeHQ4kdl+VcsSAwKl91XenC5gD8YVVw7BMci3Q913kmn+RMLhrMlexKxV?= =?us-ascii?Q?mTnzOE4W7BzbL6NE2VatE9x/AtInl17RxfhJbwAj8+Fo5ePJm+jZn+HemAvx?= =?us-ascii?Q?0U6b3Ok0emqypEhhh0WElQwOzd4G84RI4UTtZCzzSmOZ5Y2IXhicls6Lk0s9?= =?us-ascii?Q?sGU/nX2jNW5XxNpghLl+0v6iM87CmlBYcvk2Z12vCy6QiijiLNNv0VReb1NN?= =?us-ascii?Q?rBjTyZL15OSeH1ZZfqIrQbmeYuuHCKE+rsPno4JenZA6xaW3yf0+sfKNidKH?= =?us-ascii?Q?/CoyKhTTiJp0DG8FY7ABXwZViX+84umoski8IxF/sqxiRr0esKsrMhkiQahf?= =?us-ascii?Q?KR2DWeOigbZBXo9h/rM7VBVI4SA96bJlRnBRqQtjIVY8iupmHNGkeKN9aA2n?= =?us-ascii?Q?Zx5hZAEimtB9O9jqmLJEVxLrqQUTgAhTowvMsKElOtXJhE8V1ToJ4vtvRroe?= =?us-ascii?Q?49L3zwkjYtAh8p1oAt+2cOB2ogO27JtW66L10aPgiZl8xdvLuDz7DDplhiGY?= =?us-ascii?Q?tPh1rP5e2vrnz185uPqCFdzoOo+LgRZQOz8y?= X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230040)(7416014)(1800799024)(376014)(36860700013)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Aug 2025 23:32:41.2709 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0f80da3e-5828-4417-9dab-08dde8e6ade1 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF0000C37E.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9114 Content-Type: text/plain; charset="utf-8" The iommu_deferred_attach() function invokes __iommu_attach_device() while not holding the group->mutex, like other __iommu_attach_device() callers. Though there is no pratical bug being triggered so far, it would be better to apply the same locking to this __iommu_attach_device(), since the IOMMU drivers nowaday are more aware of the group->mutex -- some of them use the iommu_group_mutex_assert() function that could be potentially in the path of an attach_dev callback function invoked by the __iommu_attach_device(). The iommu_deferred_attach() will soon need to verify a new flag stored in the struct group_device. To iterate the gdev list, the group->mutex should be held for this matter too. So, grab the mutex to guard __iommu_attach_device() like other callers. Reviewed-by: Jason Gunthorpe Signed-off-by: Nicolin Chen Reviewed-by: Kevin Tian --- drivers/iommu/iommu.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 060ebe330ee16..1e0116bce0762 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2144,10 +2144,17 @@ EXPORT_SYMBOL_GPL(iommu_attach_device); =20 int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain) { - if (dev->iommu && dev->iommu->attach_deferred) - return __iommu_attach_device(domain, dev); + /* + * This is called on the dma mapping fast path so avoid locking. This is + * racy, but we have an expectation that the driver will setup its DMAs + * inside probe while being single threaded to avoid racing. + */ + if (!dev->iommu || !dev->iommu->attach_deferred) + return 0; =20 - return 0; + guard(mutex)(&dev->iommu_group->mutex); + + return __iommu_attach_device(domain, dev); } =20 void iommu_detach_device(struct iommu_domain *domain, struct device *dev) --=20 2.43.0 From nobody Fri Oct 3 12:12:55 2025 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2058.outbound.protection.outlook.com [40.107.223.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 87085277037; Sun, 31 Aug 2025 23:32:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.58 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756683177; cv=fail; b=MQVbvvyXzB+QZnqibpFeMFy1wsAOBk6sAW/8ZspYIp/GiszdOpFXSCXQgqShtu7HPGRPviw1vpSCxq36KQLWa09F0hAiz3QTDlZPjaEuwqJvjQ6FajpJD2d4dJ0JLRGvIifSyEo4/PbDhth+ySFcPWTNV9htbAkqdwQ04tvqlEQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756683177; c=relaxed/simple; bh=83ta9xj8m9WPHeUls1AQSdS7lGIjuYJA9Wgyo4Njxq8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QSADAQiv5QMvWHcfM9IG4qDIHJ257kYWmQr214W7oLdThweLGnG5TT1CJzoVpxQTPen5ykYJP2JbpF7zYVxim5L4l7vKyCKO/41cUZ0/7Ir6ZGSyQIU/+g19hWRLkt/W7ERBGWYzMPBQS+I+KO30TqIehMMp8ujWa0jk6crRbm8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=UaulSTRf; arc=fail smtp.client-ip=40.107.223.58 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="UaulSTRf" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=RSZqUc7YqfpvOp6skixN+gGPnZF4hI1aKLYFLy1i98EiDQ5gxCwrdMygBhPwvW5+mrPbtCh8Ui9aLB06noS6s413K8lM/Ngu2hYcAXwWzqtdked72KOCXca0+9CM03AVCwQeVIHJYoKN60v1nxbV6XIpFaYuT2YmygKG8vbYEbzpXPlhEStGuOeS03OQa5EAeB2hzVo3cyRU+8ie6TSus7ci7D8xuKuAIg+WzbLSKmhLrqmQZfGJpy09u/Myzlstny/x/VkfO7fmF0Q9GLBq7YCZ2vyNjjfv3Sd8fkYkZnmachY8ezYc56tOMUG8QHO3N+pFEF0YMFBd+eDu3ya4IQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=8r5EdWbzpnmh3fw+iP4isRqY/R2qYq5XCyfI56FOvIQ=; b=k3JFHLFkZpNT0TrEYtK6hvluj7fQ/9o5gKiyLrh1V9yJa8gtRccL8xZL94MBFsRLwLhnhCFoLupYPQYDPFW9vg6wJ9kzfL8mYyklt10h0TdgmGLpwwygIgtnLqoXP30jpMOBKYjIuP9NcwvClK9BpL1QVFFegCfHOpA0vcbLNTWuJ05CIi/U8Pb0ZVrtGMdQZ48DLwWYXaFRmkDDyEC0wRzOEs8JJ3ZlfaFh9sPwNyya+1+RTUbQqxA0SX8sJHES1564zqihOaYBb8AERjhtM6t3DmmL9InYKQYWPGvshhcZ5ef34BqrjFnA16QtzovaO9ZoQmhjHT1Ybk+ZRjNtog== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=8bytes.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=8r5EdWbzpnmh3fw+iP4isRqY/R2qYq5XCyfI56FOvIQ=; b=UaulSTRfTi+Qy+kIi+2cHvvyGYGEyDxViG8rSTFhmnfx2urnznMNdl7XG58QNBUn5x2ouCKQjeKn6DGG6spgguFdp4rds5EjE2oxmFHD0SGElcThEEPXZLGiBzq/weh/En+XUa8KsiiBd/ZBHhTy00H0dFtklJAqP6vsvsiyKYjc2lVqbxHpvcohTsxsQ3ziqRGGB+mMY0bMXEyM+zfq8oZhJ3yAQk7kmfvTXddU3M2YgUWCdmQfup+frxK8ynugJe/FLBds0VodRc6evF2hbFLHx7lWhdXwFWy33MWa+Z1vsA0NencsQELMJEHo9EFUYm5e2uSDOuZz0qj3Im72xw== Received: from PH7PR17CA0048.namprd17.prod.outlook.com (2603:10b6:510:323::11) by SA0PR12MB4495.namprd12.prod.outlook.com (2603:10b6:806:70::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9073.26; Sun, 31 Aug 2025 23:32:51 +0000 Received: from SN1PEPF000252A3.namprd05.prod.outlook.com (2603:10b6:510:323:cafe::e3) by PH7PR17CA0048.outlook.office365.com (2603:10b6:510:323::11) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9073.26 via Frontend Transport; Sun, 31 Aug 2025 23:32:51 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by SN1PEPF000252A3.mail.protection.outlook.com (10.167.242.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9094.14 via Frontend Transport; Sun, 31 Aug 2025 23:32:51 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Sun, 31 Aug 2025 16:32:33 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Sun, 31 Aug 2025 16:32:32 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.14) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.14 via Frontend Transport; Sun, 31 Aug 2025 16:32:28 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 3/7] iommu: Pass in gdev to __iommu_device_set_domain Date: Sun, 31 Aug 2025 16:31:55 -0700 Message-ID: <24ed472debf8c57d3cef6582400c7636e080ed28.1756682135.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SN1PEPF000252A3:EE_|SA0PR12MB4495:EE_ X-MS-Office365-Filtering-Correlation-Id: d765abd1-b268-4fa8-4d9f-08dde8e6b3e2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|1800799024|36860700013|7416014|376014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?r7BEKp7vZDjmf89wjYU0LPtBIJorKjXuFfwJQxdOq4/SZquiZitLdehFPjBd?= =?us-ascii?Q?g8BvY5gXMntKgFsR5ij/IvQvKxGk7UTTFmgcd75PfAttdv42aIGUqIcLHor7?= =?us-ascii?Q?YKXwJ+uBGI22miHh9PyQwsB2kFVhLdeGEaQD3GFTh85MAtHudyHXRTSKJ3tn?= =?us-ascii?Q?Hy6Uo0OGBIxMokgK1qzr+0hDfxF9VOtF1CKeOX42l1eFPM4glpFWCdZTK0Vr?= =?us-ascii?Q?xIxzKZaOpZhgPzSCgaMbyDclkAp1f1Q7aPndFFfx3b5rksH24JkIKDXtdeT9?= =?us-ascii?Q?5BzG28Tp78Ns3QiUyPv+yuTc2gxc/FnfY80TsoCYQij9jbl7OfjM/E9Dipmg?= =?us-ascii?Q?mZN2A9btk46WuhGJJvdE6YG2tMUOqDw7vJokIr2P6Z5Ew9ZtEe/yri3aaIvR?= =?us-ascii?Q?ApKSeB4mg87rroJFtsoRm8wDwdjOCMO74EJYJhHR/2JNDwoRvvvnHhOa7lb1?= =?us-ascii?Q?Wr/IhGoUTMH70Xx0dVCXxyNPjro1XhOrmUJpsWkDbDGVSZA1EDfQfLaRjK90?= =?us-ascii?Q?dnFJGiWd3dAGM63sTEflE+XMqPADHvqs5uH5ZlQWog81NQw7yjj/ooI/KYXn?= =?us-ascii?Q?mUAGwiNB0sPYqMMno3BVB32q40MpMRP94UmGJkydgIp4XLEiRxilqpWO/H7I?= =?us-ascii?Q?VQGf+2O8/QJFhEMLQxFcSvsdEII/dtFjA2SX/7WuW1bjo8X24mMf8elhUe3V?= =?us-ascii?Q?+A80MwCnQ79QxL/hX5CLJ+wK3df7uHGnJM0v0hFGd9uWimJjHktlLxj7F6Qx?= =?us-ascii?Q?yNvcSnKK1s5D4nPKwFiTKa0h3ilkwInEtnA1qEcHLrQbzf2m6VFfIMbJoZ5B?= =?us-ascii?Q?08Uw+V8F5GC1Yvn8b4QBndui5WYZBO7kH+Sca0rzlTeXY4IY2SVLBtgAEmPm?= =?us-ascii?Q?8aQgWVnBxgRtbf5g8r01WXPBx20vKposc+7oPPHopIlcYSY5Q7241HPiqwrK?= =?us-ascii?Q?uBSo8wvFhO9dPqjuyOEHVOZX6j1ZvZczqJp4CdfKqnoTTUvUPW7pGoUL2sOV?= =?us-ascii?Q?kZlWLQfCOEFCPszGGked5prIvJqY6wrVNZwJGUqVaWwSOkQFB/jJ0AzJb+hb?= =?us-ascii?Q?MKOzadZrz//HP+1R+HWF8EncnYOsdft5u5f66wC5HINigccpsUdniNfOW1F8?= =?us-ascii?Q?NZ/SRNkuG5tcuBVqfUu4cRTO/Lzt3qPQua1M2JCSCu4h/XxY6/VyjhutPmJW?= =?us-ascii?Q?KmwhEkb4psqWAsuWWAZLPrT0xJ47TcwHPidKJl69KPZ42ZAXxojXF1e18Aji?= =?us-ascii?Q?W0VXa/3+aAVH9rJanXyYZnzT18BSPGvjpDqvgFA5JyN0otg1iyjPcwJGOI/e?= =?us-ascii?Q?PK4Yn3PRQMYfnjYrUutni8VP4S5xxVXePcER50MEChNhnzKCkCHU9+GNb4/p?= =?us-ascii?Q?O9PzG9wA/s6pQrVEpQ3p33pV2pIfW4RBzfBODF2mLZvhg5UWqHJNd4UnVyYS?= =?us-ascii?Q?AjKxe1v5MvKTC69OkSYZcZfQM7u1u3T9fuxZvrC+BC137nhvU6Hj7R2lYZn5?= =?us-ascii?Q?thfuqfUZ7c6zIdKHw9D4Ofq/nys4GA/xbm//?= X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(82310400026)(1800799024)(36860700013)(7416014)(376014);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Aug 2025 23:32:51.3537 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d765abd1-b268-4fa8-4d9f-08dde8e6b3e2 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: SN1PEPF000252A3.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4495 Content-Type: text/plain; charset="utf-8" The device under the reset will be attached to a blocked domain, while not updating the group->domain pointer. So there needs to be a per-device flag to indicate the reset state, for other iommu core functions to check so as not to shift the attached domain during the reset state. The regular device pointer can't store any private iommu flag. So the flag has to be in the gdev structure. Pass in the gdev pointer instead to the functions that will check that per device flag. Reviewed-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Lu Baolu Signed-off-by: Nicolin Chen --- drivers/iommu/iommu.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 1e0116bce0762..e6a66dacce1b8 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -112,7 +112,7 @@ enum { }; =20 static int __iommu_device_set_domain(struct iommu_group *group, - struct device *dev, + struct group_device *gdev, struct iommu_domain *new_domain, unsigned int flags); static int __iommu_group_set_domain_internal(struct iommu_group *group, @@ -602,7 +602,7 @@ static int __iommu_probe_device(struct device *dev, str= uct list_head *group_list if (group->default_domain) iommu_create_device_direct_mappings(group->default_domain, dev); if (group->domain) { - ret =3D __iommu_device_set_domain(group, dev, group->domain, 0); + ret =3D __iommu_device_set_domain(group, gdev, group->domain, 0); if (ret) goto err_remove_gdev; } else if (!group->default_domain && !group_list) { @@ -2263,10 +2263,11 @@ int iommu_attach_group(struct iommu_domain *domain,= struct iommu_group *group) EXPORT_SYMBOL_GPL(iommu_attach_group); =20 static int __iommu_device_set_domain(struct iommu_group *group, - struct device *dev, + struct group_device *gdev, struct iommu_domain *new_domain, unsigned int flags) { + struct device *dev =3D gdev->dev; int ret; =20 /* @@ -2346,8 +2347,7 @@ static int __iommu_group_set_domain_internal(struct i= ommu_group *group, */ result =3D 0; for_each_group_device(group, gdev) { - ret =3D __iommu_device_set_domain(group, gdev->dev, new_domain, - flags); + ret =3D __iommu_device_set_domain(group, gdev, new_domain, flags); if (ret) { result =3D ret; /* @@ -2379,7 +2379,7 @@ static int __iommu_group_set_domain_internal(struct i= ommu_group *group, */ if (group->domain) WARN_ON(__iommu_device_set_domain( - group, gdev->dev, group->domain, + group, gdev, group->domain, IOMMU_SET_DOMAIN_MUST_SUCCEED)); if (gdev =3D=3D last_gdev) break; --=20 2.43.0 From nobody Fri Oct 3 12:12:55 2025 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2060.outbound.protection.outlook.com [40.107.223.60]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D5202270EDF; Sun, 31 Aug 2025 23:33:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.60 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756683189; cv=fail; b=t0lLVdwUKYcrziYZ2hpR75ZgpPI9jcMbfs26mbtv9Mtu0bd3HWT6JSuyjCLwFSse0ieQR5pbGn3BOGRShyU/ft812Ig1hZHHWTHOrIrWHiwiIIazJoMPBG78OM7qeU8VgJFV49G3ZkuiJ/rh4cwAZyVPtiUIQyFl901CjOpe9/g= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756683189; c=relaxed/simple; bh=spKs9lB5UZmvDxLWOT0ld3dY7fAYboJd1wHPG0Wq/xY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=l+97gIBHrcW81M4YU3Z98ah05Tu1SmV1OpxexxeTqxNiLPQb8Gs0rqanBspHvqNTVu3y0zzFeXgdPyWR+uAlHhCuHUlWmSjGl5zTssa0GxLQizzAbd0aBwTaqsNLBkqTBtjV0AKhyqCWgbBy7S0aNL/5vDhT/l5/sT/Kx6+4yK0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=qHvrl2Eo; arc=fail smtp.client-ip=40.107.223.60 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="qHvrl2Eo" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=TvDILzlxb5CTUsRVWb9lVoYt4DiHIje9gro6glNlxLIdotFs9b3TJVJ1vAIk9N2BDJpb11ztKFaEnZM+TpBf9tJMLSefpLfQ99oUoUNgydTMxMKt1Hyz2pySfvt22awClV3mKh31KwCbWRekMxkvR3oZei7WpA6BpRZZ0BtBVw/zFElVjL9AIevbzq25QlWl9PDK8jhDEMZF53IQaxtG2MDaZwcDrqnzq+Mv6r7tyu9ZyN+l8ac2OzZOqU/BuckvR4fBsJqUPRha7D5wAFXXu0KXhgR2zGxGeylUxYFxa4j+gNtxIPpkfAR9iqX2iDAn4uLRgVzAnAHH9lBMzCa7Bg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=h/aI4b7yzFWOkKzRJhP6UXSZl0AMe6oNZNZzBkdLTLI=; b=S14xx2sWVQTJOim0uQrNKrbISz4pmaZTAnRi9kDZBKvCmpUKJO9zpIq0gIfA/L2JRq5WqlRYWxEl17Re9jpHFWcfp++rd+dE0j3bqcbv6n+KPjXFseJYLA/FFAI3wdOzSfqTX9MruF9/ucWkDa9zVZG7sUrurBAONKhtlc2R7Z9uG5T5jTsjTWbP7VGg4L9IqWkSpcvkPC1W5cziUh/cXZGbhbjpYd0ZiR2wY0LL8fWHFZIDA1hQTrmGwErLL8vyVMkb0u/ipTjAHZqTGDjj909hHdpNXiStuF10HSGGoRj4sO8xWJ+puB3ymLqpgdE/nCkDratjJirBVNsd/659HQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=8bytes.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=h/aI4b7yzFWOkKzRJhP6UXSZl0AMe6oNZNZzBkdLTLI=; b=qHvrl2EoAz08fGgY8cuUNN71vlZUyYd9ZTkev1Cg46RP9Hnab7KkoveRaCjKpX+x7hiEYxzoDm6JNtlBBTpGS9/+Ug+nSytUlTAlZsYf06fVRU2OkEG+Q0QwftpBgzpIi50OCuJzZ0VlfMDnQfnDsSmcIABXUqLPGjR6pdJoxYD27jwmUBATTBOGm1zmzC2kB3qboL8ij2SZEQdbA8wDOPx5tkeZbwHzQfN0/geeAa27hPVpN4fooypT+/1POgPuC0wShgttNAoUO8bc+xxRlK9xhLRlcZW/Mv/rHSwB3rDm6c29MceGTdQq8+ixLFPBGQyZ86UFXxjpo7qw2CgvNg== Received: from MN2PR16CA0041.namprd16.prod.outlook.com (2603:10b6:208:234::10) by DS0PR12MB7680.namprd12.prod.outlook.com (2603:10b6:8:11c::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9073.21; Sun, 31 Aug 2025 23:32:58 +0000 Received: from MN1PEPF0000F0E3.namprd04.prod.outlook.com (2603:10b6:208:234:cafe::7b) by MN2PR16CA0041.outlook.office365.com (2603:10b6:208:234::10) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9073.26 via Frontend Transport; Sun, 31 Aug 2025 23:32:56 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by MN1PEPF0000F0E3.mail.protection.outlook.com (10.167.242.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9094.14 via Frontend Transport; Sun, 31 Aug 2025 23:32:56 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Sun, 31 Aug 2025 16:32:37 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Sun, 31 Aug 2025 16:32:36 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.14) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.14 via Frontend Transport; Sun, 31 Aug 2025 16:32:32 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 4/7] iommu: Pass in old domain to attach_dev callback functions Date: Sun, 31 Aug 2025 16:31:56 -0700 Message-ID: <19570f350d15528e13447168b7dcd95795afdbf3.1756682135.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MN1PEPF0000F0E3:EE_|DS0PR12MB7680:EE_ X-MS-Office365-Filtering-Correlation-Id: 9134af2d-de0e-4934-14be-08dde8e6b6b4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|82310400026|7416014|376014|36860700013; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?L644wAf5uXYq3+ZdbjBK4NjCKiG9j8h/1EPngdU4A21bdOTSmAO7Yi768WAp?= =?us-ascii?Q?rC0R/AK8J6AWcDE/LBLCDp0/e8NnTY9P2fU+ZoB+nlB7wg2L/OrFEMbn/Nbn?= =?us-ascii?Q?VQ4RV6T7wqeakQnZMWlrpsurTERieKCmj51CP3e/5uO25+RiqhGcrdKTBsuE?= =?us-ascii?Q?4qDh+DrsO8+sypY6fJQ4yJZUEYLDuCxF7Ds2vqWzdIzNOp1y+5FTp0bUGMvR?= =?us-ascii?Q?IZe4bvK/ngavscoL1/frDlF98RYzsDP93gOUHqMzxLe3B1Q3uO8fOX1ZqIMi?= =?us-ascii?Q?tDLo4e1nMNTZlR6M8tWpLMkV08ZRU2Ld1s6d+WyxVsbjqi1Frz2qR7aZH4xd?= =?us-ascii?Q?pBMjPg0+0lLcho/PSN4V5gpuQqE0HGoeEDx+S6xpqq++Coz470xY9Wxh0BKU?= =?us-ascii?Q?8O8wyj8i0bw5+I8b42CH8/aLarhnMp8ecGSkoBWlTsRfBwEknoesHz16d2oN?= =?us-ascii?Q?TplbM41fl/tnJn420zQCHzd2yi5lcvP4rSJiPPpdswQ1cEcBKAKhR8hnCNQp?= =?us-ascii?Q?pC6NsdYGrN1mE0HwRmd57yyzsk44i+RgIeGVhqYlhcSR/Aab0h36cX4+jeH1?= =?us-ascii?Q?YwhbHYhOI/e2cBQw2Slau4ccppBPkj+BewPwftQVdZ6lGYqAdd/SxYibAq1F?= =?us-ascii?Q?VOA5P2xw+UVe8pgb6dtXSHsPeAFqhgSOq1u3TJJLR84Ld+u2K0ZxcMA6rKkX?= =?us-ascii?Q?nUYSKAr07yvdmjYJTG/TEiANs5nN1fRKPJhpJxE0cCCD8OR1CzpuglgPA72O?= =?us-ascii?Q?6rFeYb2rY1qcVQJopoC2VYmDuJ0s+j4GB/suvjiSUNm9fr6ugMvdQA0+Iuuz?= =?us-ascii?Q?6TVdorXV+hBZqTP1q6MEY/eislO3tRNwCLwtSlNJbYXZA69Mp/pvn4alSFmg?= =?us-ascii?Q?E88xFSQqFCKDOMIZhh1WJ2tEzd2WAybYrr7ob0pC4Jp5kl+lVLHBa6YY/dIr?= =?us-ascii?Q?fF/0zA+r5n8MIZde31An7xMZJOrEszJyymtH4wr3hdYBEdpqT/y4C6AFFyNp?= =?us-ascii?Q?oE2eYKmRuOrhHrX3DJls8erjKxKl2p/0vvMsjfz/LDuruOGqqeVtscPQcwYm?= =?us-ascii?Q?wpsiPc/Eq4DN+wa/lnZ3754plBy6eMwHhKCrVJXq/4kXowNTZmYrVfs+LEAZ?= =?us-ascii?Q?7AY1eIJiYCU7HlT/clu9t0cJTzBtr8/RkNGrtAmTiTn1pvKUYWkYULcUiIBL?= =?us-ascii?Q?yOXAM14o5L5RchU2MiCuZp7MKyokbt26Cw5j5gaUsFKsx6mRfW1y+MvB2ZQZ?= =?us-ascii?Q?aTKjSJT9kOf376WS84c8Uv6LIA6OiebkiCPoY5jKm3xp8K95iYEfpgznNCKI?= =?us-ascii?Q?Yt63VJSuliFocD8pSeYF0mbeFxvT/RI8PWcB1xv9mzUA/fZAfM7ZyxnuGEfs?= =?us-ascii?Q?VUAOB1+FuS9idvdtQPQrzNKsTjW17vf4jck6vqsZAz1M0c4QR8kdgqBW9b5A?= =?us-ascii?Q?95Yr506FJnubNzLltK3+aPYcW/tTOpMfvYJDZdtlOwmgJNuciDgB/GQyICm4?= =?us-ascii?Q?bfNYJ6nOypqxs/THjUnGoZw8iDfk0BRORmwp?= X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(1800799024)(82310400026)(7416014)(376014)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Aug 2025 23:32:56.0246 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9134af2d-de0e-4934-14be-08dde8e6b6b4 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: MN1PEPF0000F0E3.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB7680 Content-Type: text/plain; charset="utf-8" The IOMMU core attaches each device to a default domain on probe(). Then, every new "attach" operation has a fundamental meaning of two-fold: - detach from its currently attached (old) domain - attach to a given new domain Modern IOMMU drivers following this pattern usually want to clean up the things related to the old domain, so they call iommu_get_domain_for_dev() to fetch the old domain. Pass in the old domain pointer from the core to drivers, aligning with the set_dev_pasid op that passes in already. Ensure all low-level attach fcuntions in the core can forward the correct old domain pointer. Thus, rework those functions as well. Suggested-by: Jason Gunthorpe Signed-off-by: Nicolin Chen Reviewed-by: Kevin Tian --- include/linux/iommu.h | 3 +- drivers/iommu/amd/iommu.c | 11 ++++--- drivers/iommu/apple-dart.c | 9 +++-- .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 5 +-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 33 ++++++++++++------- drivers/iommu/arm/arm-smmu/arm-smmu.c | 9 +++-- drivers/iommu/arm/arm-smmu/qcom_iommu.c | 11 ++++--- drivers/iommu/exynos-iommu.c | 6 ++-- drivers/iommu/fsl_pamu_domain.c | 12 +++---- drivers/iommu/intel/iommu.c | 10 ++++-- drivers/iommu/intel/nested.c | 2 +- drivers/iommu/iommu.c | 26 +++++++++------ drivers/iommu/iommufd/selftest.c | 2 +- drivers/iommu/ipmmu-vmsa.c | 10 +++--- drivers/iommu/msm_iommu.c | 8 ++--- drivers/iommu/mtk_iommu.c | 8 ++--- drivers/iommu/mtk_iommu_v1.c | 7 ++-- drivers/iommu/omap-iommu.c | 12 +++---- drivers/iommu/riscv/iommu.c | 9 +++-- drivers/iommu/rockchip-iommu.c | 20 ++++++++--- drivers/iommu/s390-iommu.c | 9 +++-- drivers/iommu/sprd-iommu.c | 3 +- drivers/iommu/sun50i-iommu.c | 8 +++-- drivers/iommu/tegra-smmu.c | 10 +++--- drivers/iommu/virtio-iommu.c | 6 ++-- 25 files changed, 152 insertions(+), 97 deletions(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index c30d12e16473d..801b2bd9e8d49 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -751,7 +751,8 @@ struct iommu_ops { * @free: Release the domain after use. */ struct iommu_domain_ops { - int (*attach_dev)(struct iommu_domain *domain, struct device *dev); + int (*attach_dev)(struct iommu_domain *domain, struct device *dev, + struct iommu_domain *old); int (*set_dev_pasid)(struct iommu_domain *domain, struct device *dev, ioasid_t pasid, struct iommu_domain *old); =20 diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index eb348c63a8d09..8a18a3bfa5a2d 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -69,8 +69,8 @@ int amd_iommu_max_glx_val =3D -1; */ DEFINE_IDA(pdom_ids); =20 -static int amd_iommu_attach_device(struct iommu_domain *dom, - struct device *dev); +static int amd_iommu_attach_device(struct iommu_domain *dom, struct device= *dev, + struct iommu_domain *old); =20 static void set_dte_entry(struct amd_iommu *iommu, struct iommu_dev_data *dev_data); @@ -2634,7 +2634,8 @@ void amd_iommu_domain_free(struct iommu_domain *dom) } =20 static int blocked_domain_attach_device(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct iommu_dev_data *dev_data =3D dev_iommu_priv_get(dev); =20 @@ -2692,8 +2693,8 @@ static struct iommu_domain release_domain =3D { } }; =20 -static int amd_iommu_attach_device(struct iommu_domain *dom, - struct device *dev) +static int amd_iommu_attach_device(struct iommu_domain *dom, struct device= *dev, + struct iommu_domain *old) { struct iommu_dev_data *dev_data =3D dev_iommu_priv_get(dev); struct protection_domain *domain =3D to_pdomain(dom); diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 190f28d766151..88648e051e783 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -660,7 +660,8 @@ static int apple_dart_domain_add_streams(struct apple_d= art_domain *domain, } =20 static int apple_dart_attach_dev_paging(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { int ret, i; struct apple_dart_stream_map *stream_map; @@ -681,7 +682,8 @@ static int apple_dart_attach_dev_paging(struct iommu_do= main *domain, } =20 static int apple_dart_attach_dev_identity(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct apple_dart_master_cfg *cfg =3D dev_iommu_priv_get(dev); struct apple_dart_stream_map *stream_map; @@ -705,7 +707,8 @@ static struct iommu_domain apple_dart_identity_domain = =3D { }; =20 static int apple_dart_attach_dev_blocked(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct apple_dart_master_cfg *cfg =3D dev_iommu_priv_get(dev); struct apple_dart_stream_map *stream_map; diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c b/drivers/= iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c index 8cd8929bbfdf8..313201a616991 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c @@ -138,14 +138,15 @@ void arm_smmu_master_clear_vmaster(struct arm_smmu_ma= ster *master) } =20 static int arm_smmu_attach_dev_nested(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old_domain) { struct arm_smmu_nested_domain *nested_domain =3D to_smmu_nested_domain(domain); struct arm_smmu_master *master =3D dev_iommu_priv_get(dev); struct arm_smmu_attach_state state =3D { .master =3D master, - .old_domain =3D iommu_get_domain_for_dev(dev), + .old_domain =3D old_domain, .ssid =3D IOMMU_NO_PASID, }; struct arm_smmu_ste ste; diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/ar= m/arm-smmu-v3/arm-smmu-v3.c index 1a21d1a2dd454..de02eeb524c15 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3002,7 +3002,8 @@ void arm_smmu_attach_commit(struct arm_smmu_attach_st= ate *state) arm_smmu_remove_master_domain(master, state->old_domain, state->ssid); } =20 -static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device = *dev) +static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device = *dev, + struct iommu_domain *old_domain) { int ret =3D 0; struct arm_smmu_ste target; @@ -3010,7 +3011,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *d= omain, struct device *dev) struct arm_smmu_device *smmu; struct arm_smmu_domain *smmu_domain =3D to_smmu_domain(domain); struct arm_smmu_attach_state state =3D { - .old_domain =3D iommu_get_domain_for_dev(dev), + .old_domain =3D old_domain, .ssid =3D IOMMU_NO_PASID, }; struct arm_smmu_master *master; @@ -3186,7 +3187,7 @@ static int arm_smmu_blocking_set_dev_pasid(struct iom= mu_domain *new_domain, =20 /* * When the last user of the CD table goes away downgrade the STE back - * to a non-cd_table one. + * to a non-cd_table one, by re-attaching its sid_domain. */ if (!arm_smmu_ssids_in_use(&master->cd_table)) { struct iommu_domain *sid_domain =3D @@ -3194,12 +3195,14 @@ static int arm_smmu_blocking_set_dev_pasid(struct i= ommu_domain *new_domain, =20 if (sid_domain->type =3D=3D IOMMU_DOMAIN_IDENTITY || sid_domain->type =3D=3D IOMMU_DOMAIN_BLOCKED) - sid_domain->ops->attach_dev(sid_domain, dev); + sid_domain->ops->attach_dev(sid_domain, dev, + sid_domain); } return 0; } =20 static void arm_smmu_attach_dev_ste(struct iommu_domain *domain, + struct iommu_domain *old_domain, struct device *dev, struct arm_smmu_ste *ste, unsigned int s1dss) @@ -3207,7 +3210,7 @@ static void arm_smmu_attach_dev_ste(struct iommu_doma= in *domain, struct arm_smmu_master *master =3D dev_iommu_priv_get(dev); struct arm_smmu_attach_state state =3D { .master =3D master, - .old_domain =3D iommu_get_domain_for_dev(dev), + .old_domain =3D old_domain, .ssid =3D IOMMU_NO_PASID, }; =20 @@ -3248,14 +3251,16 @@ static void arm_smmu_attach_dev_ste(struct iommu_do= main *domain, } =20 static int arm_smmu_attach_dev_identity(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old_domain) { struct arm_smmu_ste ste; struct arm_smmu_master *master =3D dev_iommu_priv_get(dev); =20 arm_smmu_master_clear_vmaster(master); arm_smmu_make_bypass_ste(master->smmu, &ste); - arm_smmu_attach_dev_ste(domain, dev, &ste, STRTAB_STE_1_S1DSS_BYPASS); + arm_smmu_attach_dev_ste(domain, old_domain, dev, &ste, + STRTAB_STE_1_S1DSS_BYPASS); return 0; } =20 @@ -3269,14 +3274,15 @@ static struct iommu_domain arm_smmu_identity_domain= =3D { }; =20 static int arm_smmu_attach_dev_blocked(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old_domain) { struct arm_smmu_ste ste; struct arm_smmu_master *master =3D dev_iommu_priv_get(dev); =20 arm_smmu_master_clear_vmaster(master); arm_smmu_make_abort_ste(&ste); - arm_smmu_attach_dev_ste(domain, dev, &ste, + arm_smmu_attach_dev_ste(domain, old_domain, dev, &ste, STRTAB_STE_1_S1DSS_TERMINATE); return 0; } @@ -3292,7 +3298,8 @@ static struct iommu_domain arm_smmu_blocked_domain = =3D { }; =20 static int arm_smmu_attach_dev_release(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old_domain) { struct arm_smmu_master *master =3D dev_iommu_priv_get(dev); =20 @@ -3300,9 +3307,11 @@ static int arm_smmu_attach_dev_release(struct iommu_= domain *domain, =20 /* Put the STE back to what arm_smmu_init_strtab() sets */ if (dev->iommu->require_direct) - arm_smmu_attach_dev_identity(&arm_smmu_identity_domain, dev); + arm_smmu_attach_dev_identity(&arm_smmu_identity_domain, dev, + old_domain); else - arm_smmu_attach_dev_blocked(&arm_smmu_blocked_domain, dev); + arm_smmu_attach_dev_blocked(&arm_smmu_blocked_domain, dev, + old_domain); =20 return 0; } diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.c b/drivers/iommu/arm/arm-= smmu/arm-smmu.c index 4ced4b5bee4df..5e690cf85ec96 100644 --- a/drivers/iommu/arm/arm-smmu/arm-smmu.c +++ b/drivers/iommu/arm/arm-smmu/arm-smmu.c @@ -1165,7 +1165,8 @@ static void arm_smmu_master_install_s2crs(struct arm_= smmu_master_cfg *cfg, } } =20 -static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device = *dev) +static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device = *dev, + struct iommu_domain *old) { struct arm_smmu_domain *smmu_domain =3D to_smmu_domain(domain); struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(dev); @@ -1234,7 +1235,8 @@ static int arm_smmu_attach_dev_type(struct device *de= v, } =20 static int arm_smmu_attach_dev_identity(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { return arm_smmu_attach_dev_type(dev, S2CR_TYPE_BYPASS); } @@ -1249,7 +1251,8 @@ static struct iommu_domain arm_smmu_identity_domain = =3D { }; =20 static int arm_smmu_attach_dev_blocked(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { return arm_smmu_attach_dev_type(dev, S2CR_TYPE_FAULT); } diff --git a/drivers/iommu/arm/arm-smmu/qcom_iommu.c b/drivers/iommu/arm/ar= m-smmu/qcom_iommu.c index c5be95e560317..9222a4a48bb33 100644 --- a/drivers/iommu/arm/arm-smmu/qcom_iommu.c +++ b/drivers/iommu/arm/arm-smmu/qcom_iommu.c @@ -359,7 +359,8 @@ static void qcom_iommu_domain_free(struct iommu_domain = *domain) kfree(qcom_domain); } =20 -static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct devic= e *dev) +static int qcom_iommu_attach_dev(struct iommu_domain *domain, + struct device *dev, struct iommu_domain *old) { struct qcom_iommu_dev *qcom_iommu =3D dev_iommu_priv_get(dev); struct qcom_iommu_domain *qcom_domain =3D to_qcom_iommu_domain(domain); @@ -388,18 +389,18 @@ static int qcom_iommu_attach_dev(struct iommu_domain = *domain, struct device *dev } =20 static int qcom_iommu_identity_attach(struct iommu_domain *identity_domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { - struct iommu_domain *domain =3D iommu_get_domain_for_dev(dev); struct qcom_iommu_domain *qcom_domain; struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(dev); struct qcom_iommu_dev *qcom_iommu =3D dev_iommu_priv_get(dev); unsigned int i; =20 - if (domain =3D=3D identity_domain || !domain) + if (old =3D=3D identity_domain || !old) return 0; =20 - qcom_domain =3D to_qcom_iommu_domain(domain); + qcom_domain =3D to_qcom_iommu_domain(old); if (WARN_ON(!qcom_domain->iommu)) return -EINVAL; =20 diff --git a/drivers/iommu/exynos-iommu.c b/drivers/iommu/exynos-iommu.c index b6edd178fe25e..b30d2bb87fa96 100644 --- a/drivers/iommu/exynos-iommu.c +++ b/drivers/iommu/exynos-iommu.c @@ -984,7 +984,8 @@ static void exynos_iommu_domain_free(struct iommu_domai= n *iommu_domain) } =20 static int exynos_iommu_identity_attach(struct iommu_domain *identity_doma= in, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct exynos_iommu_owner *owner =3D dev_iommu_priv_get(dev); struct exynos_iommu_domain *domain; @@ -1035,7 +1036,8 @@ static struct iommu_domain exynos_identity_domain =3D= { }; =20 static int exynos_iommu_attach_device(struct iommu_domain *iommu_domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct exynos_iommu_domain *domain =3D to_exynos_domain(iommu_domain); struct exynos_iommu_owner *owner =3D dev_iommu_priv_get(dev); diff --git a/drivers/iommu/fsl_pamu_domain.c b/drivers/iommu/fsl_pamu_domai= n.c index 5f08523f97cb9..9664ef9840d2c 100644 --- a/drivers/iommu/fsl_pamu_domain.c +++ b/drivers/iommu/fsl_pamu_domain.c @@ -238,7 +238,7 @@ static int update_domain_stash(struct fsl_dma_domain *d= ma_domain, u32 val) } =20 static int fsl_pamu_attach_device(struct iommu_domain *domain, - struct device *dev) + struct device *dev, struct iommu_domain *old) { struct fsl_dma_domain *dma_domain =3D to_fsl_dma_domain(domain); unsigned long flags; @@ -298,9 +298,9 @@ static int fsl_pamu_attach_device(struct iommu_domain *= domain, * switches to what looks like BLOCKING. */ static int fsl_pamu_platform_attach(struct iommu_domain *platform_domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { - struct iommu_domain *domain =3D iommu_get_domain_for_dev(dev); struct fsl_dma_domain *dma_domain; const u32 *prop; int len; @@ -311,11 +311,11 @@ static int fsl_pamu_platform_attach(struct iommu_doma= in *platform_domain, * Hack to keep things working as they always have, only leaving an * UNMANAGED domain makes it BLOCKING. */ - if (domain =3D=3D platform_domain || !domain || - domain->type !=3D IOMMU_DOMAIN_UNMANAGED) + if (old =3D=3D platform_domain || !old || + old->type !=3D IOMMU_DOMAIN_UNMANAGED) return 0; =20 - dma_domain =3D to_fsl_dma_domain(domain); + dma_domain =3D to_fsl_dma_domain(old); =20 /* * Use LIODN of the PCI controller while detaching a diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 9c3ab9d9f69a3..e9fbe9f6cc6cd 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -3225,7 +3225,8 @@ void device_block_translation(struct device *dev) } =20 static int blocking_domain_attach_dev(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct device_domain_info *info =3D dev_iommu_priv_get(dev); =20 @@ -3532,7 +3533,8 @@ int paging_domain_compatible(struct iommu_domain *dom= ain, struct device *dev) } =20 static int intel_iommu_attach_device(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { int ret; =20 @@ -4396,7 +4398,9 @@ static int device_setup_pass_through(struct device *d= ev) context_setup_pass_through_cb, dev); } =20 -static int identity_domain_attach_dev(struct iommu_domain *domain, struct = device *dev) +static int identity_domain_attach_dev(struct iommu_domain *domain, + struct device *dev, + struct iommu_domain *old) { struct device_domain_info *info =3D dev_iommu_priv_get(dev); struct intel_iommu *iommu =3D info->iommu; diff --git a/drivers/iommu/intel/nested.c b/drivers/iommu/intel/nested.c index 1b6ad9c900a5a..760d7aa2ade84 100644 --- a/drivers/iommu/intel/nested.c +++ b/drivers/iommu/intel/nested.c @@ -19,7 +19,7 @@ #include "pasid.h" =20 static int intel_nested_attach_dev(struct iommu_domain *domain, - struct device *dev) + struct device *dev, struct iommu_domain *old) { struct device_domain_info *info =3D dev_iommu_priv_get(dev); struct dmar_domain *dmar_domain =3D to_dmar_domain(domain); diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index e6a66dacce1b8..ef3fd7bd1b553 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -100,7 +100,7 @@ static int iommu_bus_notifier(struct notifier_block *nb, unsigned long action, void *data); static void iommu_release_device(struct device *dev); static int __iommu_attach_device(struct iommu_domain *domain, - struct device *dev); + struct device *dev, struct iommu_domain *old); static int __iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group); static struct iommu_domain *__iommu_paging_domain_alloc_flags(struct devic= e *dev, @@ -114,6 +114,7 @@ enum { static int __iommu_device_set_domain(struct iommu_group *group, struct group_device *gdev, struct iommu_domain *new_domain, + struct iommu_domain *old_domain, unsigned int flags); static int __iommu_group_set_domain_internal(struct iommu_group *group, struct iommu_domain *new_domain, @@ -517,7 +518,8 @@ static void iommu_deinit_device(struct device *dev) * should still avoid touching any hardware configuration either. */ if (!dev->iommu->attach_deferred && ops->release_domain) - ops->release_domain->ops->attach_dev(ops->release_domain, dev); + ops->release_domain->ops->attach_dev(ops->release_domain, dev, + group->domain); =20 if (ops->release_device) ops->release_device(dev); @@ -602,7 +604,8 @@ static int __iommu_probe_device(struct device *dev, str= uct list_head *group_list if (group->default_domain) iommu_create_device_direct_mappings(group->default_domain, dev); if (group->domain) { - ret =3D __iommu_device_set_domain(group, gdev, group->domain, 0); + ret =3D __iommu_device_set_domain(group, gdev, group->domain, + NULL, 0); if (ret) goto err_remove_gdev; } else if (!group->default_domain && !group_list) { @@ -2089,14 +2092,14 @@ static void __iommu_group_set_core_domain(struct io= mmu_group *group) } =20 static int __iommu_attach_device(struct iommu_domain *domain, - struct device *dev) + struct device *dev, struct iommu_domain *old) { int ret; =20 if (unlikely(domain->ops->attach_dev =3D=3D NULL)) return -ENODEV; =20 - ret =3D domain->ops->attach_dev(domain, dev); + ret =3D domain->ops->attach_dev(domain, dev, old); if (ret) return ret; dev->iommu->attach_deferred =3D 0; @@ -2154,7 +2157,7 @@ int iommu_deferred_attach(struct device *dev, struct = iommu_domain *domain) =20 guard(mutex)(&dev->iommu_group->mutex); =20 - return __iommu_attach_device(domain, dev); + return __iommu_attach_device(domain, dev, NULL); } =20 void iommu_detach_device(struct iommu_domain *domain, struct device *dev) @@ -2265,6 +2268,7 @@ EXPORT_SYMBOL_GPL(iommu_attach_group); static int __iommu_device_set_domain(struct iommu_group *group, struct group_device *gdev, struct iommu_domain *new_domain, + struct iommu_domain *old_domain, unsigned int flags) { struct device *dev =3D gdev->dev; @@ -2291,7 +2295,7 @@ static int __iommu_device_set_domain(struct iommu_gro= up *group, dev->iommu->attach_deferred =3D 0; } =20 - ret =3D __iommu_attach_device(new_domain, dev); + ret =3D __iommu_attach_device(new_domain, dev, old_domain); if (ret) { /* * If we have a blocking domain then try to attach that in hopes @@ -2301,7 +2305,8 @@ static int __iommu_device_set_domain(struct iommu_gro= up *group, if ((flags & IOMMU_SET_DOMAIN_MUST_SUCCEED) && group->blocking_domain && group->blocking_domain !=3D new_domain) - __iommu_attach_device(group->blocking_domain, dev); + __iommu_attach_device(group->blocking_domain, dev, + old_domain); return ret; } return 0; @@ -2347,7 +2352,8 @@ static int __iommu_group_set_domain_internal(struct i= ommu_group *group, */ result =3D 0; for_each_group_device(group, gdev) { - ret =3D __iommu_device_set_domain(group, gdev, new_domain, flags); + ret =3D __iommu_device_set_domain(group, gdev, new_domain, + group->domain, flags); if (ret) { result =3D ret; /* @@ -2379,7 +2385,7 @@ static int __iommu_group_set_domain_internal(struct i= ommu_group *group, */ if (group->domain) WARN_ON(__iommu_device_set_domain( - group, gdev, group->domain, + group, gdev, group->domain, new_domain, IOMMU_SET_DOMAIN_MUST_SUCCEED)); if (gdev =3D=3D last_gdev) break; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selft= est.c index 61686603c7693..fa4359bb15e84 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -216,7 +216,7 @@ static inline struct selftest_obj *to_selftest_obj(stru= ct iommufd_object *obj) } =20 static int mock_domain_nop_attach(struct iommu_domain *domain, - struct device *dev) + struct device *dev, struct iommu_domain *old) { struct mock_dev *mdev =3D to_mock_dev(dev); struct mock_viommu *new_viommu =3D NULL; diff --git a/drivers/iommu/ipmmu-vmsa.c b/drivers/iommu/ipmmu-vmsa.c index ffa892f657140..6667ecc331f01 100644 --- a/drivers/iommu/ipmmu-vmsa.c +++ b/drivers/iommu/ipmmu-vmsa.c @@ -590,7 +590,7 @@ static void ipmmu_domain_free(struct iommu_domain *io_d= omain) } =20 static int ipmmu_attach_device(struct iommu_domain *io_domain, - struct device *dev) + struct device *dev, struct iommu_domain *old) { struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(dev); struct ipmmu_vmsa_device *mmu =3D to_ipmmu(dev); @@ -637,17 +637,17 @@ static int ipmmu_attach_device(struct iommu_domain *i= o_domain, } =20 static int ipmmu_iommu_identity_attach(struct iommu_domain *identity_domai= n, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { - struct iommu_domain *io_domain =3D iommu_get_domain_for_dev(dev); struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(dev); struct ipmmu_vmsa_domain *domain; unsigned int i; =20 - if (io_domain =3D=3D identity_domain || !io_domain) + if (old =3D=3D identity_domain || !old) return 0; =20 - domain =3D to_vmsa_domain(io_domain); + domain =3D to_vmsa_domain(old); for (i =3D 0; i < fwspec->num_ids; ++i) ipmmu_utlb_disable(domain, fwspec->ids[i]); =20 diff --git a/drivers/iommu/msm_iommu.c b/drivers/iommu/msm_iommu.c index 43a61ba021a51..2629fbd0606d3 100644 --- a/drivers/iommu/msm_iommu.c +++ b/drivers/iommu/msm_iommu.c @@ -441,19 +441,19 @@ static int msm_iommu_attach_dev(struct iommu_domain *= domain, struct device *dev) } =20 static int msm_iommu_identity_attach(struct iommu_domain *identity_domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { - struct iommu_domain *domain =3D iommu_get_domain_for_dev(dev); struct msm_priv *priv; unsigned long flags; struct msm_iommu_dev *iommu; struct msm_iommu_ctx_dev *master; int ret =3D 0; =20 - if (domain =3D=3D identity_domain || !domain) + if (old =3D=3D identity_domain || !old) return 0; =20 - priv =3D to_msm_priv(domain); + priv =3D to_msm_priv(old); free_io_pgtable_ops(priv->iop); =20 spin_lock_irqsave(&msm_iommu_lock, flags); diff --git a/drivers/iommu/mtk_iommu.c b/drivers/iommu/mtk_iommu.c index 0e0285348d2b8..9747ef1644138 100644 --- a/drivers/iommu/mtk_iommu.c +++ b/drivers/iommu/mtk_iommu.c @@ -705,7 +705,7 @@ static void mtk_iommu_domain_free(struct iommu_domain *= domain) } =20 static int mtk_iommu_attach_device(struct iommu_domain *domain, - struct device *dev) + struct device *dev, struct iommu_domain *old) { struct mtk_iommu_data *data =3D dev_iommu_priv_get(dev), *frstdata; struct mtk_iommu_domain *dom =3D to_mtk_domain(domain); @@ -773,12 +773,12 @@ static int mtk_iommu_attach_device(struct iommu_domai= n *domain, } =20 static int mtk_iommu_identity_attach(struct iommu_domain *identity_domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { - struct iommu_domain *domain =3D iommu_get_domain_for_dev(dev); struct mtk_iommu_data *data =3D dev_iommu_priv_get(dev); =20 - if (domain =3D=3D identity_domain || !domain) + if (old =3D=3D identity_domain || !old) return 0; =20 mtk_iommu_config(data, dev, false, 0); diff --git a/drivers/iommu/mtk_iommu_v1.c b/drivers/iommu/mtk_iommu_v1.c index 10cc0b1197e80..3b45650263ac3 100644 --- a/drivers/iommu/mtk_iommu_v1.c +++ b/drivers/iommu/mtk_iommu_v1.c @@ -303,7 +303,9 @@ static void mtk_iommu_v1_domain_free(struct iommu_domai= n *domain) kfree(to_mtk_domain(domain)); } =20 -static int mtk_iommu_v1_attach_device(struct iommu_domain *domain, struct = device *dev) +static int mtk_iommu_v1_attach_device(struct iommu_domain *domain, + struct device *dev, + struct iommu_domain *old) { struct mtk_iommu_v1_data *data =3D dev_iommu_priv_get(dev); struct mtk_iommu_v1_domain *dom =3D to_mtk_domain(domain); @@ -329,7 +331,8 @@ static int mtk_iommu_v1_attach_device(struct iommu_doma= in *domain, struct device } =20 static int mtk_iommu_v1_identity_attach(struct iommu_domain *identity_doma= in, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct mtk_iommu_v1_data *data =3D dev_iommu_priv_get(dev); =20 diff --git a/drivers/iommu/omap-iommu.c b/drivers/iommu/omap-iommu.c index 6fb93927bdb98..a38f69debac09 100644 --- a/drivers/iommu/omap-iommu.c +++ b/drivers/iommu/omap-iommu.c @@ -1431,8 +1431,8 @@ static void omap_iommu_detach_fini(struct omap_iommu_= domain *odomain) odomain->iommus =3D NULL; } =20 -static int -omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev) +static int omap_iommu_attach_dev(struct iommu_domain *domain, + struct device *dev, struct iommu_domain *old) { struct omap_iommu_arch_data *arch_data =3D dev_iommu_priv_get(dev); struct omap_iommu_domain *omap_domain =3D to_omap_domain(domain); @@ -1536,15 +1536,15 @@ static void _omap_iommu_detach_dev(struct omap_iomm= u_domain *omap_domain, } =20 static int omap_iommu_identity_attach(struct iommu_domain *identity_domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { - struct iommu_domain *domain =3D iommu_get_domain_for_dev(dev); struct omap_iommu_domain *omap_domain; =20 - if (domain =3D=3D identity_domain || !domain) + if (old =3D=3D identity_domain || !old) return 0; =20 - omap_domain =3D to_omap_domain(domain); + omap_domain =3D to_omap_domain(old); spin_lock(&omap_domain->lock); _omap_iommu_detach_dev(omap_domain, dev); spin_unlock(&omap_domain->lock); diff --git a/drivers/iommu/riscv/iommu.c b/drivers/iommu/riscv/iommu.c index 2d0d31ba28860..9c58413ad641e 100644 --- a/drivers/iommu/riscv/iommu.c +++ b/drivers/iommu/riscv/iommu.c @@ -1319,7 +1319,8 @@ static bool riscv_iommu_pt_supported(struct riscv_iom= mu_device *iommu, int pgd_m } =20 static int riscv_iommu_attach_paging_domain(struct iommu_domain *iommu_dom= ain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct riscv_iommu_domain *domain =3D iommu_domain_to_riscv(iommu_domain); struct riscv_iommu_device *iommu =3D dev_to_iommu(dev); @@ -1424,7 +1425,8 @@ static struct iommu_domain *riscv_iommu_alloc_paging_= domain(struct device *dev) } =20 static int riscv_iommu_attach_blocking_domain(struct iommu_domain *iommu_d= omain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct riscv_iommu_device *iommu =3D dev_to_iommu(dev); struct riscv_iommu_info *info =3D dev_iommu_priv_get(dev); @@ -1445,7 +1447,8 @@ static struct iommu_domain riscv_iommu_blocking_domai= n =3D { }; =20 static int riscv_iommu_attach_identity_domain(struct iommu_domain *iommu_d= omain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct riscv_iommu_device *iommu =3D dev_to_iommu(dev); struct riscv_iommu_info *info =3D dev_iommu_priv_get(dev); diff --git a/drivers/iommu/rockchip-iommu.c b/drivers/iommu/rockchip-iommu.c index 0861dd469bd86..85f3667e797c3 100644 --- a/drivers/iommu/rockchip-iommu.c +++ b/drivers/iommu/rockchip-iommu.c @@ -960,7 +960,8 @@ static int rk_iommu_enable(struct rk_iommu *iommu) } =20 static int rk_iommu_identity_attach(struct iommu_domain *identity_domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct rk_iommu *iommu; struct rk_iommu_domain *rk_domain; @@ -1005,7 +1006,7 @@ static struct iommu_domain rk_identity_domain =3D { }; =20 static int rk_iommu_attach_device(struct iommu_domain *domain, - struct device *dev) + struct device *dev, struct iommu_domain *old) { struct rk_iommu *iommu; struct rk_iommu_domain *rk_domain =3D to_rk_domain(domain); @@ -1026,7 +1027,7 @@ static int rk_iommu_attach_device(struct iommu_domain= *domain, if (iommu->domain =3D=3D domain) return 0; =20 - ret =3D rk_iommu_identity_attach(&rk_identity_domain, dev); + ret =3D rk_iommu_identity_attach(&rk_identity_domain, dev, old); if (ret) return ret; =20 @@ -1041,8 +1042,17 @@ static int rk_iommu_attach_device(struct iommu_domai= n *domain, return 0; =20 ret =3D rk_iommu_enable(iommu); - if (ret) - WARN_ON(rk_iommu_identity_attach(&rk_identity_domain, dev)); + if (ret) { + /* + * Note rk_iommu_identity_attach() might fail before physically + * attaching the dev to iommu->domain, in which case the actual + * old domain for this revert should be rk_identity_domain v.s. + * iommu->domain. Since rk_iommu_identity_attach() does not care + * about the old domain argument for now, this is not a problem. + */ + WARN_ON(rk_iommu_identity_attach(&rk_identity_domain, dev, + iommu->domain)); + } =20 pm_runtime_put(iommu->dev); =20 diff --git a/drivers/iommu/s390-iommu.c b/drivers/iommu/s390-iommu.c index 9c80d61deb2c0..f2f58bb21720b 100644 --- a/drivers/iommu/s390-iommu.c +++ b/drivers/iommu/s390-iommu.c @@ -653,7 +653,8 @@ int zpci_iommu_register_ioat(struct zpci_dev *zdev, u8 = *status) } =20 static int blocking_domain_attach_device(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct zpci_dev *zdev =3D to_zpci_dev(dev); struct s390_domain *s390_domain; @@ -677,7 +678,8 @@ static int blocking_domain_attach_device(struct iommu_d= omain *domain, } =20 static int s390_iommu_attach_device(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct s390_domain *s390_domain =3D to_s390_domain(domain); struct zpci_dev *zdev =3D to_zpci_dev(dev); @@ -1113,7 +1115,8 @@ static int __init s390_iommu_init(void) subsys_initcall(s390_iommu_init); =20 static int s390_attach_dev_identity(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct zpci_dev *zdev =3D to_zpci_dev(dev); u8 status; diff --git a/drivers/iommu/sprd-iommu.c b/drivers/iommu/sprd-iommu.c index c7ca1d8a0b153..555d4505c747a 100644 --- a/drivers/iommu/sprd-iommu.c +++ b/drivers/iommu/sprd-iommu.c @@ -247,7 +247,8 @@ static void sprd_iommu_domain_free(struct iommu_domain = *domain) } =20 static int sprd_iommu_attach_device(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct sprd_iommu_device *sdev =3D dev_iommu_priv_get(dev); struct sprd_iommu_domain *dom =3D to_sprd_domain(domain); diff --git a/drivers/iommu/sun50i-iommu.c b/drivers/iommu/sun50i-iommu.c index de10b569d9a94..d3b190be18b5a 100644 --- a/drivers/iommu/sun50i-iommu.c +++ b/drivers/iommu/sun50i-iommu.c @@ -771,7 +771,8 @@ static void sun50i_iommu_detach_domain(struct sun50i_io= mmu *iommu, } =20 static int sun50i_iommu_identity_attach(struct iommu_domain *identity_doma= in, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct sun50i_iommu *iommu =3D dev_iommu_priv_get(dev); struct sun50i_iommu_domain *sun50i_domain; @@ -797,7 +798,8 @@ static struct iommu_domain sun50i_iommu_identity_domain= =3D { }; =20 static int sun50i_iommu_attach_device(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { struct sun50i_iommu_domain *sun50i_domain =3D to_sun50i_domain(domain); struct sun50i_iommu *iommu; @@ -813,7 +815,7 @@ static int sun50i_iommu_attach_device(struct iommu_doma= in *domain, if (iommu->domain =3D=3D domain) return 0; =20 - sun50i_iommu_identity_attach(&sun50i_iommu_identity_domain, dev); + sun50i_iommu_identity_attach(&sun50i_iommu_identity_domain, dev, old); =20 sun50i_iommu_attach_domain(iommu, sun50i_domain); =20 diff --git a/drivers/iommu/tegra-smmu.c b/drivers/iommu/tegra-smmu.c index 36cdd5fbab077..336e0a3ff41fb 100644 --- a/drivers/iommu/tegra-smmu.c +++ b/drivers/iommu/tegra-smmu.c @@ -490,7 +490,7 @@ static void tegra_smmu_as_unprepare(struct tegra_smmu *= smmu, } =20 static int tegra_smmu_attach_dev(struct iommu_domain *domain, - struct device *dev) + struct device *dev, struct iommu_domain *old) { struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(dev); struct tegra_smmu *smmu =3D dev_iommu_priv_get(dev); @@ -524,9 +524,9 @@ static int tegra_smmu_attach_dev(struct iommu_domain *d= omain, } =20 static int tegra_smmu_identity_attach(struct iommu_domain *identity_domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { - struct iommu_domain *domain =3D iommu_get_domain_for_dev(dev); struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(dev); struct tegra_smmu_as *as; struct tegra_smmu *smmu; @@ -535,10 +535,10 @@ static int tegra_smmu_identity_attach(struct iommu_do= main *identity_domain, if (!fwspec) return -ENODEV; =20 - if (domain =3D=3D identity_domain || !domain) + if (old =3D=3D identity_domain || !old) return 0; =20 - as =3D to_smmu_as(domain); + as =3D to_smmu_as(old); smmu =3D as->smmu; for (index =3D 0; index < fwspec->num_ids; index++) { tegra_smmu_disable(smmu, fwspec->ids[index], as->id); diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c index 532db1de201ba..bd1af8a77005f 100644 --- a/drivers/iommu/virtio-iommu.c +++ b/drivers/iommu/virtio-iommu.c @@ -730,7 +730,8 @@ static struct iommu_domain *viommu_domain_alloc_identit= y(struct device *dev) return domain; } =20 -static int viommu_attach_dev(struct iommu_domain *domain, struct device *d= ev) +static int viommu_attach_dev(struct iommu_domain *domain, struct device *d= ev, + struct iommu_domain *old) { int ret =3D 0; struct virtio_iommu_req_attach req; @@ -781,7 +782,8 @@ static int viommu_attach_dev(struct iommu_domain *domai= n, struct device *dev) } =20 static int viommu_attach_identity_domain(struct iommu_domain *domain, - struct device *dev) + struct device *dev, + struct iommu_domain *old) { int ret =3D 0; struct virtio_iommu_req_attach req; --=20 2.43.0 From nobody Fri Oct 3 12:12:55 2025 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2051.outbound.protection.outlook.com [40.107.100.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 098A7257834; Sun, 31 Aug 2025 23:33:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.100.51 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756683185; cv=fail; b=cH1UTk6Bel3iAheur1XuI+uwWtBwXtcUzdSXE9Fefv/790CbeSkJuKMFEavElMlf7Sv8TufUXXQ1A5/9MVYcuXnkpzEi4qdxQDl0qiLCv7uBBpvQX4RKAq8kwCoR/+tIbZf2USPpDB8Lf4zvfbcFyPmML7Q+zuyzgS9ubrXtupk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756683185; c=relaxed/simple; bh=XLBEVyf9HmB4IlNa6tFyzmsSZPIWxKfB/B4mIGDrOqI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Jz90fPKsk0HtxKcuzGcZriNF0fLJv0BvmCATmPTM6aHIqSgDPD3+QTVjNg06m1W6OUkb4qlDA0iDSsCHbMAqfQSr0D+aDcfr9Unnm+cN+hwK5wOY2WRx4QixgroW97I4B4JSaMA2/Zaa8QXl6RC8V3oOZr79puJ+9x8xyKnNo/8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=qfxdWacj; arc=fail smtp.client-ip=40.107.100.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="qfxdWacj" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=y35Vw3g/34GfQuQA3XlT1LXtjrDcnrf4Ax5H68uEsVs69s9Yhp2qj1DW42BtbPxhZOM8ke1iVtISzh29r5u8uxB3ulFfsvwxdp4EBQ39pF5K7CjV2LBkYxQ+IsROBJ3ucbQblsPToSeaXbbyjdlzL6viiq3nXRl7kzMb/2t/PBBzlruA7gMJmkLvqGZM4wfhkGWw5yB1KGNcBGpkH2Z/hAs3D5V5CGNedjM/TZ2rQCJR0P5+0kguDSj8OldTPJnJ7PUow63iRX6Vre0vq5Z4LWcIhTIJEjbjhRY0y6aiIYhp3DjnJ9OMmVPsb698xPtmboykm4lFbMLqapuuzWCZUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=rSBSRq5jGNZAxRbZvVVRpLykqAMfoH8P1hSz4oPA3Ng=; b=cAUHUdhQaDirsHLUVFSTnkBBzwXdk0KuHNrmZxNwZWfJdFz7OI1Vs0FkPROo9rzyrNDI/SRdqFaLmtv1xYqWqfV8ePYX1k+3khFIhPPw2PJDyFU/Jfq9zThkaIJSDYGmJXr0PeQsmTpyA/9sE+jLpadEpJYTrQzaa1mozMKuM0FycymDv0L3ihuT0W4FCDZy2Px/ALfBUzu8DdvPMnvYe8iUjbzNiiTgJU7DvRsDWPLheOFPjTNObDI13s1gvSlIOkNpJPR1A/s8xGZCpxA6EQovg+4BRkCu41nFW4Hmjz8u9S/+uJHl6GY1DF6sgmrCWTPdRaZTYRKtQr3ol61ELg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=8bytes.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=rSBSRq5jGNZAxRbZvVVRpLykqAMfoH8P1hSz4oPA3Ng=; b=qfxdWacjO+7JI3UaubasPiGoNBPl/CVU6oayULNeNcZcfEKGJAR4PgcNzXGjTocGhbwLpGQzuI69bBzZeOozIlrJ+Rsc3iIpxSB4QEvjIHiUTrmoGvkAVqsRIKSUH/mIEyLXtbLf6k7FQJQ9q2NsagdlCNWDU7WzWxyobB9VfhO3cWqYW+5VR0Gpblojn2ocfzgMxyDXtmgh5POrosxnQOIJWfn/C7v8M6xboCTC5aHOetonleWRM2nskwrKoGGel46HE04DLRqVU8fvmuqhUD1Ub4UK+RgTKoosId1PfY76LkJddrbkFYo3Bzymzx+wSqKKOuljhaQJf/MrNke5mQ== Received: from BN9PR03CA0771.namprd03.prod.outlook.com (2603:10b6:408:13a::26) by SJ0PR12MB6686.namprd12.prod.outlook.com (2603:10b6:a03:479::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9073.25; Sun, 31 Aug 2025 23:32:57 +0000 Received: from BL6PEPF0001AB55.namprd02.prod.outlook.com (2603:10b6:408:13a:cafe::20) by BN9PR03CA0771.outlook.office365.com (2603:10b6:408:13a::26) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9073.26 via Frontend Transport; Sun, 31 Aug 2025 23:32:56 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by BL6PEPF0001AB55.mail.protection.outlook.com (10.167.241.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9094.14 via Frontend Transport; Sun, 31 Aug 2025 23:32:56 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Sun, 31 Aug 2025 16:32:41 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Sun, 31 Aug 2025 16:32:41 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.14) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.14 via Frontend Transport; Sun, 31 Aug 2025 16:32:37 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 5/7] iommu: Add iommu_get_domain_for_dev_locked() helper Date: Sun, 31 Aug 2025 16:31:57 -0700 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL6PEPF0001AB55:EE_|SJ0PR12MB6686:EE_ X-MS-Office365-Filtering-Correlation-Id: 33e55966-cb35-4aef-6e87-08dde8e6b70c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|7416014|376014|82310400026|36860700013; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?6gJhwfQ8HIMS6hUlloYFaRUQiOvLaFwspJo3gR8HF58t+mkFzIM5s6ksyw/F?= =?us-ascii?Q?qiRDKkZvYjYoNJeODvNSHd8JVR+wuaTZXaQSgquwtOWxZxJ2QlAcm9xY8OZL?= =?us-ascii?Q?TXKfqJ52E1S/IxyPzyB/D8aGuVKbrZozoBPkO7XTsXKzoVSu93tWvSP4IYMf?= =?us-ascii?Q?T1s2iqaBcWleQtTb6yOGZJ15OoyeiAYjm2z1MONOgjdJGlVAwQRSxdZRvkRt?= =?us-ascii?Q?gnEx/DxvEQ5RusYPTMUU+shoJK8SsDQh6H6JjrPLvgwF99bk2BxunJs19A++?= =?us-ascii?Q?NTAFstACrfLx5kNp9tSvnonbPLPfOcj2fZ5dKb8M7YoJFismDZgpGcMKxqhB?= =?us-ascii?Q?H+0sVk60Bsf2VonXv1ZitvyiAepoLKDx1uqbG35rn9nqihML65jgmG4A53AB?= =?us-ascii?Q?Fe5b1d1grr0KJX1TTm+SpafCp7+Cbzfj6Ur6fJAWOwXnEpR+LWtnaCcRSe3Y?= =?us-ascii?Q?7aspuvzD5el7M76Kejxui+sSvcAQEroi6PdSdEAOTDiX94e+gsOS79kyewyx?= =?us-ascii?Q?XlCyHPq/vKe34dqDj16P0lh2KonrrtxTHDfmh9cXfLlVili8D3qFBjxcIjGb?= =?us-ascii?Q?0akb3Z7d3Q8hTda48gWzBsaekzG5A9EP0p2UzjSlFZHChhuyZIlEujYGFpLS?= =?us-ascii?Q?fplEMtIu5Hm+wt3FiUnG/PXim3iID0C+Gb1XAmB2tzr9qTAnrO31zK9sLHfH?= =?us-ascii?Q?SDuv98NeMcuCh1ugiIl1pYDP1vxSczrJqkIGOQMAxryZmTjmPkhOgvP7LV22?= =?us-ascii?Q?bgIQ1cRcpXHvfJI/8mMgPqNGuovTvnLDJOa07ApFZH5YvIIMjX/a1F75wDNI?= =?us-ascii?Q?ewq2L4z79gPBKNFwBWexJA7HCikEjjDTV9SOJi67T6z3iNsqYCN3NDI6tsXI?= =?us-ascii?Q?ohmkWD6ZVu4lcEoOuzMbVpIFbUJYvtnV/eFYNZe+mdtizTVoJkoPXFnM2UIi?= =?us-ascii?Q?qQFUH874qSgcP7k/TDVaizEe6r3FOSdQ0zFiDo6j35OsmuX+mWhkPeOX1Eue?= =?us-ascii?Q?jJ8ifIcYiXECRy51tpxeJJW4yr+pPnrgRmdmKhuC7N2HRQhZvOYkYD/Yng2l?= =?us-ascii?Q?oE5+ZOXu+ZhcLwyR7PUtCXd9PQcU0nChilHHruEqrdNGmhWxcaP0zeSZrjoL?= =?us-ascii?Q?XorCSQzuGY3XgNCabAx1dT52ta4M0b2YISnOKCaFjqbqOUG0bPJHWXe8yD8h?= =?us-ascii?Q?2ZcQ5yOll3Z2ylm28+qKZeYw4gzRez1wmdCfDflbV2w7t/p9wvPMfF90v9lK?= =?us-ascii?Q?vI90O3lLT4+yNcPgyyYexD7xsN85YYlfLI5X/gmzFuSlEmUtGsLpCIX7fPDy?= =?us-ascii?Q?6iokFUzoIAlwBGCfC/tGBKF0e4J5Eu+zgpslzVNqLS2qRYZ3THPU1EjhOj+J?= =?us-ascii?Q?g0IyoeVxAqz+8DQDm2WBNEsVC+7LCGYy30RjkboypdEehmV2VEgSJx/6ddQp?= =?us-ascii?Q?oxmj1HGVvGfU6UjLPhJu1uji4COt4UahLH73WD1XMB8Ueh1c8p03uldu4RQw?= =?us-ascii?Q?hgQWAnGnNQYXPossyriyE+cPjf68R2ZzWZD+?= X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230040)(1800799024)(7416014)(376014)(82310400026)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Aug 2025 23:32:56.6071 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 33e55966-cb35-4aef-6e87-08dde8e6b70c X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL6PEPF0001AB55.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6686 Content-Type: text/plain; charset="utf-8" There is a need to stage a PCI device that's under a reset to temporally the blocked domain (i.e. detach it from its previously attached domain), and then to reattach it back to its previous domain (i.e. detach it from the blocked domain) after reset. During the reset stage, there can be races from other attach/detachment. To solve this, a per-gdev reset flag will be introduced so that all the attach functions will reject any concurrent attach_dev callbacks. So, iommu_get_domain_for_dev() function always returns the group->domain that needs to be changed to the blocked domain by checking the per-gdev flag, for which iommu_get_domain_for_dev() must hold the group->mutex. On the other hand, caller like the SMMUv3 driver invoke it in one of its set_dev_pasid functions where the group->mutex is held, while some other callers like non-IOMMU drivers invoke it outside IOMMU callback functions so the group->mutex is not held. Apparently, this makes it difficult to add the lock to the existing iommu_get_domain_for_dev(). Introduce a new iommu_get_domain_for_dev_locked() helper to be used in a a context that is already under the protection of the group->mutex. Add a lockdep_assert_not_held to the existing iommu_get_domain_for_dev() to note that it would be only used outside the group->mutex. Signed-off-by: Nicolin Chen Reviewed-by: Kevin Tian --- include/linux/iommu.h | 1 + drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 5 +++-- drivers/iommu/dma-iommu.c | 2 +- drivers/iommu/iommu.c | 14 ++++++++++++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 801b2bd9e8d49..6d6d068c3de48 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -910,6 +910,7 @@ extern int iommu_attach_device(struct iommu_domain *dom= ain, extern void iommu_detach_device(struct iommu_domain *domain, struct device *dev); extern struct iommu_domain *iommu_get_domain_for_dev(struct device *dev); +struct iommu_domain *iommu_get_domain_for_dev_locked(struct device *dev); extern struct iommu_domain *iommu_get_dma_domain(struct device *dev); extern int iommu_map(struct iommu_domain *domain, unsigned long iova, phys_addr_t paddr, size_t size, int prot, gfp_t gfp); diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/ar= m/arm-smmu-v3/arm-smmu-v3.c index de02eeb524c15..4a68bd121287a 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3125,7 +3125,8 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, struct arm_smmu_domain *smmu_domain, ioasid_t pasid, struct arm_smmu_cd *cd, struct iommu_domain *old) { - struct iommu_domain *sid_domain =3D iommu_get_domain_for_dev(master->dev); + struct iommu_domain *sid_domain =3D + iommu_get_domain_for_dev_locked(master->dev); struct arm_smmu_attach_state state =3D { .master =3D master, .ssid =3D pasid, @@ -3191,7 +3192,7 @@ static int arm_smmu_blocking_set_dev_pasid(struct iom= mu_domain *new_domain, */ if (!arm_smmu_ssids_in_use(&master->cd_table)) { struct iommu_domain *sid_domain =3D - iommu_get_domain_for_dev(master->dev); + iommu_get_domain_for_dev_locked(master->dev); =20 if (sid_domain->type =3D=3D IOMMU_DOMAIN_IDENTITY || sid_domain->type =3D=3D IOMMU_DOMAIN_BLOCKED) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index ea2ef53bd4fef..99680cdb57265 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -2097,7 +2097,7 @@ EXPORT_SYMBOL_GPL(dma_iova_destroy); =20 void iommu_setup_dma_ops(struct device *dev) { - struct iommu_domain *domain =3D iommu_get_domain_for_dev(dev); + struct iommu_domain *domain =3D iommu_get_domain_for_dev_locked(dev); =20 if (dev_is_pci(dev)) dev->iommu->pci_32bit_workaround =3D !iommu_dma_forcedac; diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index ef3fd7bd1b553..f08c177f30de8 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2179,6 +2179,7 @@ void iommu_detach_device(struct iommu_domain *domain,= struct device *dev) } EXPORT_SYMBOL_GPL(iommu_detach_device); =20 +/* Caller must be a general/external function that isn't an IOMMU callback= */ struct iommu_domain *iommu_get_domain_for_dev(struct device *dev) { /* Caller must be a probed driver on dev */ @@ -2187,10 +2188,23 @@ struct iommu_domain *iommu_get_domain_for_dev(struc= t device *dev) if (!group) return NULL; =20 + lockdep_assert_not_held(&group->mutex); + return group->domain; } EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev); =20 +/* Caller must be an IOMMU callback/internal function that holds group->mu= tex */ +struct iommu_domain *iommu_get_domain_for_dev_locked(struct device *dev) +{ + struct iommu_group *group =3D dev->iommu_group; + + lockdep_assert_held(&group->mutex); + + return group->domain; +} +EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev_locked); + /* * For IOMMU_DOMAIN_DMA implementations which already provide their own * guarantees that the group and its default domain are valid and correct. --=20 2.43.0 From nobody Fri Oct 3 12:12:55 2025 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2061.outbound.protection.outlook.com [40.107.223.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C4C83279351; Sun, 31 Aug 2025 23:33:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.61 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756683195; cv=fail; b=e/i2JOuHB5X4e/o2iQxMW+qRBB1XsCoy29mFopE0TRxufR6NCnoYQBlfX0w18KHuJb3cVJumi9wJ0ie3RVNy1XjrXoqCPaRDf8zEsdQckBGgCS5AXoabUVl3sdkPbyK138hjXAmVNoI9Dl3QIm3qtBi6y1uTcTR/W7H7cMS9gKw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756683195; c=relaxed/simple; bh=/PbH6azi+ZlLOLTtSdG6EM0UCqTE4Z8kQuhPX2pssH0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rI0L3+EQjxdmAsw/YofmTNA+0vqZ3arkhq4engr9wemEgAqYxjDmsA8MfI6Z19LB+gpRgqKNDQT5UMNaURefpJSAj6ivEbhgmYWHOtigTqX+zV8I5qsixtoqlrfoq/EfQwpqgm6w3RzxqbVDp6+k9d2eJo8ipzrEf8gL4P3A5AE= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=Rri5+zJ/; arc=fail smtp.client-ip=40.107.223.61 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="Rri5+zJ/" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=nMhvm74WV9OtXtDcz+VZTsid7acjxTfN1dug+zUBh+vktP5beqm95U3E3WFhjWd6USE2hs2FDnTMKl9ZNu99jIaQRljvOQBShuwa41Gt31UvilpBCeIpUgl5b+XdUoOegx3UnM+x7rRKY6ANlKytB0GnpcRMwOBiDWN7Q/HKOOqyr+I0PmWfuXu7UYdCgTs8Xr5Fd7M+nCcswOmMrCHDfarddz/mUY1GjDJ0PiuaJG5LhO4epZLozGTP5IKa5urezs3Ib61/bmJVQmZ9MTEaSBFSUDIfvzKtxleDIZm/mtRS6WAC/E0Dw9Pr01qeaf/lUtZj4SunCzs6b8iCYzIOGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=UHtVmei2QPyKqvT1UN7x5VSVzas3RncF2N/M/e6SA9s=; b=VNkqxeg1jBP3JWkawkHBwNIAb4eYYokOENjEHhMpoh92ii+bhQWpetYBAsBXNjRMQ/+9qDf3Og5X8YEeIb0N9aGg5xDrgZhl/fing0WMmB1wgi/WuCVby61grSrCFK054Q7Y+P2rqZ+aqS2FoLL0OVmMEEIE1KZ8Rdq4GRtwjpChuiLIqZyYeJQBBBF0oeXRTO0K1cXmdKtC9zPWYKr5ve6kDba2UGQtXYsDEJDtfnwjryIjA/UUGQDWy1x+OyIugW8spTGeDP2wDqN9ZnCaMD9sIFYYWkSGKSYEB88OJy1A5LfSZ/9j2GH4pR1Qra2BvmKIdOurWUrj1H2kvCEl5w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=8bytes.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=UHtVmei2QPyKqvT1UN7x5VSVzas3RncF2N/M/e6SA9s=; b=Rri5+zJ/7GyFSnc1dQlAFauxNWvvUw9NJ09Z18vDz35W063WbNhVK5DyEiVoLM6/TEc47s894mTdDtOaqnYxZctfK1VuvpsyKgIVqqGYsERJBHw+VXRtsOHkMJa2GhnzrjRmZHu8mRqwsZR/Wz0q/0Gev5D5ZYYtZYzApp999ps2UfJjKvttA4ItuzoaphV2rPFzl79fXP9jyX2UAM1aYrpBhT/Yo6DWePYuXOoma0AeF9greCyjh0shG7+m2qSn95fRPjOwWqDfn51w1G3rTQphKWB4bah40YLuinRP2pl284Al5Y2sIyoo2qdpSqaWt53ZaUHoTpX3xV64FTYT6A== Received: from CH0PR04CA0103.namprd04.prod.outlook.com (2603:10b6:610:75::18) by DM4PR12MB7671.namprd12.prod.outlook.com (2603:10b6:8:104::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9073.21; Sun, 31 Aug 2025 23:33:05 +0000 Received: from CH2PEPF0000013B.namprd02.prod.outlook.com (2603:10b6:610:75:cafe::5f) by CH0PR04CA0103.outlook.office365.com (2603:10b6:610:75::18) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9073.26 via Frontend Transport; Sun, 31 Aug 2025 23:33:05 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CH2PEPF0000013B.mail.protection.outlook.com (10.167.244.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9094.14 via Frontend Transport; Sun, 31 Aug 2025 23:33:04 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Sun, 31 Aug 2025 16:32:46 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Sun, 31 Aug 2025 16:32:45 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.14) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.14 via Frontend Transport; Sun, 31 Aug 2025 16:32:41 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 6/7] iommu: Introduce iommu_dev_reset_prepare() and iommu_dev_reset_done() Date: Sun, 31 Aug 2025 16:31:58 -0700 Message-ID: <0f6021b500c74db33af8118210dd7a2b2fd31b3c.1756682135.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PEPF0000013B:EE_|DM4PR12MB7671:EE_ X-MS-Office365-Filtering-Correlation-Id: 5144388b-ffae-4cbd-b02b-08dde8e6bbff X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|82310400026|1800799024|7416014|376014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?qodEQecZJnPpLZV1ulg/q8uOrx7MdPWdK9kS3/23GOKMjTnowKaie4JOZWIJ?= =?us-ascii?Q?t6+clqhqg6ISz0FiIeuGyEAF+amPBt7TWUo5tMkv6A5sbzt4UEvK6Q6RWskW?= =?us-ascii?Q?J8y2DCZrRJvHB7FJaSTip9kbNXrTw4boM7s/5O3B/y6g9vRvoWdPrRperShU?= =?us-ascii?Q?oLh2XBM/iX1HfM7+3qUF0TBhvNz+W7oFp8JY2eNFiK2g+YPeN9LpFNdDzQho?= =?us-ascii?Q?hvbGK0iGQbDngnmsJLBmwU+1HlJ2K4Ae+BX/WJYS/zhL6WFuJ4FKDqgupf7d?= =?us-ascii?Q?a9t6jBNd3rQFE8TUrBN8eSEprKy10+XszBCfUriousS49jtNUOKRekyFt+tz?= =?us-ascii?Q?qO8oARgVBD9+Eq18pmEKguqOCZoukckBnU5BcKoYVf/H/6yReK5jSTPKIiVw?= =?us-ascii?Q?XGr2ONXhPOzjCMZ7Rkl735L0NFkn7T5g3Wi0kYnbAhDx8+MagaTGSgH1KgNn?= =?us-ascii?Q?znWZl3+qZbguoi0w6MSlE51ni0JNM/OTDPL3ka4Qnbg2hPtA0eebiKi9P37J?= =?us-ascii?Q?VK23UR+M54y+VusqnWrhM7pvmGbgl/LVDgZkS6BxDr///uyRIex1WeiqGCH0?= =?us-ascii?Q?1FyyfaKZJsR3wPepWEe6F6tOz7QW6mMhdTtP6QRFADbEEiApNFAzNW/9FCQA?= =?us-ascii?Q?JylqTU1ikN0tooj8G/d5GnmXLnZ2tPGfwjuifMY1br98njvsGQN1MQ9GrV5F?= =?us-ascii?Q?Q71QZSqwqJCaa3ENl2Yv5VKZAYU1suWkgSr4fl9Jp+WUi4xwtoSqIWhG8N/2?= =?us-ascii?Q?dWtP+WQ9W/JvM0LW1nnzaKKGD4S9B2jl0UOS+gANQWscphxQtZTnq1fdblKU?= =?us-ascii?Q?sWGUzGlFf1YQ50/92Q8QBYpyJGJj6TGibjlzyONYXWIIBGdKqXa16NWRSps/?= =?us-ascii?Q?rQ87/rqftOn/p/eLGrC4L8HxZyvqKG2R6qB0jaioyhJLobIgL0ZXaK3vmkWw?= =?us-ascii?Q?ynCFgzLwy5YfSQ7/tHdIbyZ5PUEAmEzjVc8zlb/gFq9MCsKwTJOKsmRHTae5?= =?us-ascii?Q?O8CQiCNgUEZjWovkUof1aLw4NMYd0ms28B293qqC8n5xyIvxE2CCB3/ks993?= =?us-ascii?Q?mOpBdHpR/IjZxWKgO4XTdrF4MEUu0ovFaF4Dskwe3CL2tCjPsozRQW0Byy5A?= =?us-ascii?Q?HZRJvHFNGUWgTjJkRBA3MAoQCwuL+H5d4SUHv7mM+HKh8swh4wJS/BdS9tni?= =?us-ascii?Q?9BefC4pnlLQlmOiTwls4aSo5viJCs7NWU6gHaZsDX5yagZVrFvr6WCUn17wv?= =?us-ascii?Q?eDwajQiGmLyy4cdObbR1thnurA/ACCu0Q4h6caE/v5VBs2VMWvO1JWRusihK?= =?us-ascii?Q?kIzI26R8DOLLUW8LgVJe5fSog410s0PMEtqkyE24JSn25hULb/37WiAeryAX?= =?us-ascii?Q?ep7/TpRfO9Eveug58k1kP91XXPtzU5Rol6ukp9spmxnDloRZVu4b+lN//BAl?= =?us-ascii?Q?WY0SP66AoS3D8T+hMRlBESUYxluYfWwFbaXi4uclOus8ARpvhOH7CI4+Kv/5?= =?us-ascii?Q?O3Qlou/IM7uwnzn39dKWiPyfNCGdHpsgYHpU?= X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230040)(36860700013)(82310400026)(1800799024)(7416014)(376014);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Aug 2025 23:33:04.0533 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5144388b-ffae-4cbd-b02b-08dde8e6bbff X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CH2PEPF0000013B.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB7671 Content-Type: text/plain; charset="utf-8" PCIe permits a device to ignore ATS invalidation TLPs, while processing a reset. This creates a problem visible to the OS where an ATS invalidation command will time out. E.g. an SVA domain will have no coordination with a reset event and can racily issue ATS invalidations to a resetting device. The OS should do something to mitigate this as we do not want production systems to be reporting critical ATS failures, especially in a hypervisor environment. Broadly, OS could arrange to ignore the timeouts, block page table mutations to prevent invalidations, or disable and block ATS. The PCIe spec in sec 10.3.1 IMPLEMENTATION NOTE recommends to disable and block ATS before initiating a Function Level Reset. It also mentions that other reset methods could have the same vulnerability as well. Provide a callback from the PCI subsystem that will enclose the reset and have the iommu core temporarily change all the attached domain to BLOCKED. After attaching a BLOCKED domain, IOMMU hardware would fence any incoming ATS queries. And IOMMU drivers should also synchronously stop issuing new ATS invalidations and wait for all ATS invalidations to complete. This can avoid any ATS invaliation timeouts. However, if there is a domain attachment/replacement happening during an ongoing reset, ATS routines may be re-activated between the two function calls. So, introduce a new pending_reset flag in group_device, and reject any concurrent attach_dev/set_dev_pasid call during a reset for a concern of compatibility failure. There are two corner cases that won't work: 1. Alias devices that share the same RID Blocking one device also blocks the other alias devices that might not want a reset. Given that it's very rare for an alias device to support ATS, simply skip the blocking routine. 2. SRIOV devices that its PF is resetting while its VF isn't. Both PF and VF should block RID and PASIDs. But, since VF is not aware of the reset, it is difficult to block it and reject concurrent attach calls, because it's not logically reasonable to reject a VF attachment due to a resetting PF unless the VF is resetting too. To address this, we won't be able to reject any concurrent attachment as simple as this patch does; instead we will need two new compatibility testing ops for attach_dev/set_dev_pasid to allowing caching a compatible attach. This itself, however, would be a big series. So, for now, skip the blocking routine for PF devices, and leave a note. Reviewed-by: Lu Baolu Signed-off-by: Nicolin Chen --- include/linux/iommu.h | 12 +++ drivers/iommu/iommu.c | 199 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 211 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 6d6d068c3de48..0d8e252929c89 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -1169,6 +1169,9 @@ void dev_iommu_priv_set(struct device *dev, void *pri= v); extern struct mutex iommu_probe_device_lock; int iommu_probe_device(struct device *dev); =20 +int iommu_dev_reset_prepare(struct device *dev); +void iommu_dev_reset_done(struct device *dev); + int iommu_device_use_default_domain(struct device *dev); void iommu_device_unuse_default_domain(struct device *dev); =20 @@ -1453,6 +1456,15 @@ static inline int iommu_fwspec_add_ids(struct device= *dev, u32 *ids, return -ENODEV; } =20 +static inline int iommu_dev_reset_prepare(struct device *dev) +{ + return 0; +} + +static inline void iommu_dev_reset_done(struct device *dev) +{ +} + static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev) { return NULL; diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index f08c177f30de8..bcc239f3592f4 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -71,12 +71,29 @@ struct group_device { struct list_head list; struct device *dev; char *name; + bool pending_reset : 1; }; =20 /* Iterate over each struct group_device in a struct iommu_group */ #define for_each_group_device(group, pos) \ list_for_each_entry(pos, &(group)->devices, list) =20 +/* Callers must hold the dev->iommu_group->mutex */ +static struct group_device *device_to_group_device(struct device *dev) +{ + struct iommu_group *group =3D dev->iommu_group; + struct group_device *gdev; + + lockdep_assert_held(&group->mutex); + + /* gdev must be in the list */ + for_each_group_device(group, gdev) { + if (gdev->dev =3D=3D dev) + break; + } + return gdev; +} + struct iommu_group_attribute { struct attribute attr; ssize_t (*show)(struct iommu_group *group, char *buf); @@ -2157,6 +2174,12 @@ int iommu_deferred_attach(struct device *dev, struct= iommu_domain *domain) =20 guard(mutex)(&dev->iommu_group->mutex); =20 + /* + * There is a concurrent attach while the device is resetting. Defer it + * until iommu_dev_reset_done() attaching the device to group->domain. + */ + if (device_to_group_device(dev)->pending_reset) + return -EBUSY; return __iommu_attach_device(domain, dev, NULL); } =20 @@ -2201,6 +2224,16 @@ struct iommu_domain *iommu_get_domain_for_dev_locked= (struct device *dev) =20 lockdep_assert_held(&group->mutex); =20 + /* + * Driver handles the low-level __iommu_attach_device(), including the + * one invoked by iommu_dev_reset_done(), in which case the driver must + * get the blocking domain over group->domain caching the one prior to + * iommu_dev_reset_prepare(), so that it wouldn't end up with attaching + * the device from group->domain (old) to group->domain (new). + */ + if (device_to_group_device(dev)->pending_reset) + return group->blocking_domain; + return group->domain; } EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev_locked); @@ -2309,6 +2342,13 @@ static int __iommu_device_set_domain(struct iommu_gr= oup *group, dev->iommu->attach_deferred =3D 0; } =20 + /* + * There is a concurrent attach while the device is resetting. Defer it + * until iommu_dev_reset_done() attaching the device to group->domain. + */ + if (gdev->pending_reset) + return -EBUSY; + ret =3D __iommu_attach_device(new_domain, dev, old_domain); if (ret) { /* @@ -3394,6 +3434,15 @@ static int __iommu_set_group_pasid(struct iommu_doma= in *domain, int ret; =20 for_each_group_device(group, device) { + /* + * There is a concurrent attach while the device is resetting. + * Defer it until iommu_dev_reset_done() attaching the device to + * group->domain. + */ + if (device->pending_reset) { + ret =3D -EBUSY; + goto err_revert; + } if (device->dev->iommu->max_pasids > 0) { ret =3D domain->ops->set_dev_pasid(domain, device->dev, pasid, old); @@ -3815,6 +3864,156 @@ int iommu_replace_group_handle(struct iommu_group *= group, } EXPORT_SYMBOL_NS_GPL(iommu_replace_group_handle, "IOMMUFD_INTERNAL"); =20 +/** + * iommu_dev_reset_prepare() - Block IOMMU to prepare for a device reset + * @dev: device that is going to enter a reset routine + * + * When certain device is entering a reset routine, it wants to block any = IOMMU + * activity during the reset routine. This includes blocking any translati= on as + * well as cache invalidation too (especially the device cache). + * + * This function attaches all RID/PASID of the device's to IOMMU_DOMAIN_BL= OCKED + * allowing any blocked-domain-supporting IOMMU driver to pause translatio= n and + * cahce invalidation, but leaves the software domain pointers intact so l= ater + * the iommu_dev_reset_done() can restore everything. + * + * Return: 0 on success or negative error code if the preparation failed. + * + * Caller must use iommu_dev_reset_prepare() and iommu_dev_reset_done() to= gether + * before/after the core-level reset routine, to unclear the pending_reset= flag. + * + * These two functions are designed to be used by PCI reset functions that= would + * not invoke any racy iommu_release_device(), since PCI sysfs node gets r= emoved + * before it notifies with a BUS_NOTIFY_REMOVED_DEVICE. When using them in= other + * case, callers must ensure there will be no racy iommu_release_device() = call, + * which otherwise would UAF the dev->iommu_group pointer. + */ +int iommu_dev_reset_prepare(struct device *dev) +{ + struct iommu_group *group =3D dev->iommu_group; + struct group_device *gdev; + unsigned long pasid; + void *entry; + int ret =3D 0; + + if (!dev_has_iommu(dev)) + return 0; + + /* + * FIXME resetting a PF will reset any VF in the hardware level, so this + * should basically block both the PF and its VFs. On the other hand, VF + * software might not go through a reset, so it can run into any regular + * operation like invoking a concurrent attach_dev/set_dev_pasid call. + * + * Due to compatibility concern, any concurrent attach_dev/set_dev_pasid + * is being rejected with -EBUSY. For a PF, this rejection is reasonable + * and simple since a concurrent attachment would not be sane. For a VF, + * however, it would be difficult to justify. + * + * One way to work this out is to have a new op running a compatibility + * test for a concurrent attachment. Then, so long as it is compatible, + * the attachment would be deferred to iommu_dev_reset_done(). Bypass PF + * devices for now. + */ + if (dev_is_pci(dev) && pci_num_vf(to_pci_dev(dev)) > 0) + return 0; + + guard(mutex)(&group->mutex); + + /* We cannot block an RID that is shared with another device */ + if (dev_is_pci(dev)) { + for_each_group_device(group, gdev) { + if (gdev->dev !=3D dev && dev_is_pci(gdev->dev) && + pci_devs_are_dma_aliases(to_pci_dev(gdev->dev), + to_pci_dev(dev))) + return 0; + } + } + + ret =3D __iommu_group_alloc_blocking_domain(group); + if (ret) + return ret; + + /* Stage RID domain at blocking_domain while retaining group->domain */ + if (group->domain !=3D group->blocking_domain) { + ret =3D __iommu_attach_device(group->blocking_domain, dev, + group->domain); + if (ret) + return ret; + } + + /* + * Stage PASID domains at blocking_domain while retaining pasid_array. + * + * The pasid_array is mostly fenced by group->mutex, except one reader + * in iommu_attach_handle_get(), so it's safe to read without xa_lock. + */ + xa_for_each_start(&group->pasid_array, pasid, entry, 1) + iommu_remove_dev_pasid(dev, pasid, + pasid_array_entry_to_domain(entry)); + + device_to_group_device(dev)->pending_reset =3D true; + return ret; +} +EXPORT_SYMBOL_GPL(iommu_dev_reset_prepare); + +/** + * iommu_dev_reset_done() - Restore IOMMU after a device reset is finished + * @dev: device that has finished a reset routine + * + * When certain device has finished a reset routine, it wants to restore i= ts + * IOMMU activity, including new translation as well as cache invalidation= , by + * re-attaching all RID/PASID of the device's back to the domains retained= in + * the core-level structure. + * + * Caller must pair it with a successfully returned iommu_dev_reset_prepar= e(). + * + * Note that, although unlikely, there is a risk that re-attaching domains= might + * fail due to some unexpected happening like OOM. + */ +void iommu_dev_reset_done(struct device *dev) +{ + struct iommu_group *group =3D dev->iommu_group; + struct group_device *gdev; + unsigned long pasid; + void *entry; + + if (!dev_has_iommu(dev)) + return; + + guard(mutex)(&group->mutex); + + gdev =3D device_to_group_device(dev); + + /* iommu_dev_reset_prepare() was not successfully called */ + if (WARN_ON(!group->blocking_domain)) + return; + + /* iommu_dev_reset_prepare() was bypassed for the device */ + if (!gdev->pending_reset) + return; + + /* Re-attach RID domain back to group->domain */ + if (group->domain !=3D group->blocking_domain) { + WARN_ON(__iommu_attach_device(group->domain, dev, + group->blocking_domain)); + } + + /* + * Re-attach PASID domains back to the domains retained in pasid_array. + * + * The pasid_array is mostly fenced by group->mutex, except one reader + * in iommu_attach_handle_get(), so it's safe to read without xa_lock. + */ + xa_for_each_start(&group->pasid_array, pasid, entry, 1) + WARN_ON(__iommu_set_group_pasid( + pasid_array_entry_to_domain(entry), group, pasid, + group->blocking_domain)); + + gdev->pending_reset =3D false; +} +EXPORT_SYMBOL_GPL(iommu_dev_reset_done); + #if IS_ENABLED(CONFIG_IRQ_MSI_IOMMU) /** * iommu_dma_prepare_msi() - Map the MSI page in the IOMMU domain --=20 2.43.0 From nobody Fri Oct 3 12:12:55 2025 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2070.outbound.protection.outlook.com [40.107.102.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9E3AB274FDB; Sun, 31 Aug 2025 23:33:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.102.70 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756683200; cv=fail; b=THx+zEVdLWac6q0PDqVZgFsrRcKTf+0mYkt2gwaKDRga+v8NT0Tb/9kfEfWNwsHfLzFKz5NOr4T51zsxUoqsQU2O2R1vLnfBlQLQU4n9lwGV/zDjACeGMtO0fbPzmkgmmjD4e6KaIIZfPn6icxOD5bLyy9MleyeDrhSz8qBcKt8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756683200; c=relaxed/simple; bh=Jp9sAvHL6CX98353pWEllyNpXDMmNK8irA/KH0Z2yZg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Y/Na+wvBX67LNjTswPIns2LNN1mUWP67EE0eVwoq1Wo5dx5TlWvobMC0sLnOdQVhYwMA3ivj1jBQn2aV7ueX5GhI7a7IR8qHJDFAdhvk7FnAvKzuKOGPwxmokRlFam8NvsvxMQ4rAQFFUMUUl5t32zQTucMod/xx01exN6n4GAs= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=OKKAfkN4; arc=fail smtp.client-ip=40.107.102.70 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="OKKAfkN4" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ZFcGyiEh9noqs9S0RlRu87ofpYInhqitxRYb3u4tFi7rkDbmJSwX5utrMbcibCaX8WarAd8HyioN3f4xVsWugoDXsMOe46MiGto2BWKORJWEoDQjLJxyoT/S9a5U1v4KeWvcRbF21JHn6YlyKh05wbXDw1xfmUh3Qg8Dv8qxcy39yPHY7k6LU+Tus2wKIXkUFAwRqlfD7HlkUQ7qnikgk6EPS6MiMZxdlITojbBKjxzpCKhDzog30Y9jUFqZefhndWA3bIjE/IoSDt3umcklE+ELUaSbFT6N8fNt+L0dlE8WWU3lCRWJN+rFblgn5VZ0/fl2sCVY3QPOBnEP+iHPYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=t7XAfQegnIapW7HTU9nrmLiGfFWJaStkTZMtYA/OKR8=; b=wmj6xEUWvErqOB2MfLaSqZbs5fwBSMH94zqcPaGu5w9IZQi1ttIsf49qxneLbk+xn+cKFBNI9IySbSTQk0yniwRgMRFaq4kZ8Ob2QrTwmWvo4HAYF85dcak+nFxfFEqu6B+PSyXVUYW6yZzKnA3/eK+JBHvqjgppTkqM2YbPieADZi57f5wOXVLdYIiEpmEFaMQIqcs66flcUurYccSStdQAuecyA4XbNzPHX5bKYtSIVGUF3AM5yKeULQGLgr6lRZr7sFofLeE8W6DttCBWz/VF7ZXq/MqgvFH3zuvMfTs6qp1AJbNjVUzxZWgRN2CdujwgzV+C4snhGXo6C+lUCQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=8bytes.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=t7XAfQegnIapW7HTU9nrmLiGfFWJaStkTZMtYA/OKR8=; b=OKKAfkN4gYh7FYE13VwyUVlwLzOP7IXwdcMlonZjMqpUr90U0lRinAnLswM2kAfGt/Og1YrcOjn0AI0sqUhzJY6MFjS9zW0lmfTQKJk/MtjVy7I0tr8HufOhIKzFljUS9tUsLCt0arW3cree9/75IlRaOl+ZrK83MQpkJRpck7R7SqumRX4MzkDubY62EJ7R+RSxw7PLJRu2pbGJO6JexS2oquWZ7N5GX2r5vUem2HWuCGkraLsnA3PgOMBGK4zssmlAta93xCoY/oHXxtBRyV/wwSwmD0KqeLmYTh68e7t8DULugvH619Hpn6YnUFfaaNUpOA6DCaqle3VxFwlrdQ== Received: from BY5PR13CA0014.namprd13.prod.outlook.com (2603:10b6:a03:180::27) by IA1PR12MB7520.namprd12.prod.outlook.com (2603:10b6:208:42f::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9073.25; Sun, 31 Aug 2025 23:33:13 +0000 Received: from CY4PEPF0000EE30.namprd05.prod.outlook.com (2603:10b6:a03:180:cafe::3) by BY5PR13CA0014.outlook.office365.com (2603:10b6:a03:180::27) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9094.15 via Frontend Transport; Sun, 31 Aug 2025 23:33:12 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by CY4PEPF0000EE30.mail.protection.outlook.com (10.167.242.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9094.14 via Frontend Transport; Sun, 31 Aug 2025 23:33:12 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Sun, 31 Aug 2025 16:32:51 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Sun, 31 Aug 2025 16:32:50 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.14) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.14 via Frontend Transport; Sun, 31 Aug 2025 16:32:46 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 7/7] pci: Suspend iommu function prior to resetting a device Date: Sun, 31 Aug 2025 16:31:59 -0700 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000EE30:EE_|IA1PR12MB7520:EE_ X-MS-Office365-Filtering-Correlation-Id: e96c6974-ef33-4031-2b67-08dde8e6c061 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|82310400026|1800799024|376014|7416014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?sJNl4fQixJTM+mpj0B9nZbfPWHbarYkGcbAg4fePGMtvlMyFYdi5LMDtrQ6r?= =?us-ascii?Q?OiiUhNJ7IgLYGG4e+VvMs7ABpEf4pt2CHt6VFbcnGEz0hLOuZklOr82DP6EF?= =?us-ascii?Q?HKYhuQX1THvnoeD2ssE3YLiXOVNEAun6CxYMwZZuuNbfPdyPyt+oBWTr4FSc?= =?us-ascii?Q?95qLDJy726RxaejX7D2P3SMolhz1Dd7bXt8LSlRPdsEkGPcYT1YOG/8sPjZ8?= =?us-ascii?Q?zTwY+CigYrzuZoqJmZkjovqX5WTu/M4YQmS1xUijyhd2Nf4diWpd6cnNRoHO?= =?us-ascii?Q?Y2Riv82hW/KkqLlqnihwtZuDffZOF4KBRCAr6eqOqLLNS+LCE1bx3cOqYn12?= =?us-ascii?Q?OqeI0IppOrKOBDT/AVj9mVZ+l5GqVBLvcg2M0JptNjiaedmqeDk8N058Iihg?= =?us-ascii?Q?cx9SKjoBdTFZbgVEoeYXUNAGtXP47ORBDgLlVJi2Z7xKrFQ6yWQPme19NRey?= =?us-ascii?Q?7w8khm355UpENIFWb/LTy09X9BIDWaX+WNRgWvoCr/0PyUrrKww/MM8MpBfq?= =?us-ascii?Q?uJdG3/iwumwMrI58nnR7fgJ3jc/qPQJdv2DxFS8EY1rhmMSpKbT8joM05NaY?= =?us-ascii?Q?/yqEdhez79nl1Q3pq7d20fs/eSLnV2QXlDgWWEOuTJ+gbGRvDQocP0hR0LDC?= =?us-ascii?Q?gmYbnCj4CqjtUGUoSXN++sWNqCVjU7IAGzq6BOqC3RloMLHv9EglC5ovaSfN?= =?us-ascii?Q?GHFtLm3xnZNIN8oFERplH41XaVx7gpSd/6MNWRHSIMMN6jOsAjxgxDTxW51D?= =?us-ascii?Q?SlXE41oNEKtsr0+mrUks/go1zoxWoLsdYG797rqpENn2sIW76eJSZr74BGmT?= =?us-ascii?Q?nCZiKnpGQdW42u5KiH5hocm7hYX1fNZndwLrXEDfc5ANewGZYXSCkU2TDX1G?= =?us-ascii?Q?GORrbdz/O8PyS7XX61Aa/QLhTU72oBmHVlAbkz83g8ofmYO8G1uoIQypIrSD?= =?us-ascii?Q?5vu0DBkpgBwasjZnRSSu+4zrik61dbnUGe9CsOg1HdAanXfIkjOLhHrjs7OX?= =?us-ascii?Q?s5DymIIhw3+n47USZkBIwkAxfoJto2JsDo/2x+5hzHLKeOys/Z5sHRA0z1/O?= =?us-ascii?Q?UAgH3YECNmtpUZwALX9uG5mxSu1ctcYsmizFbSUjuZTJy1PAHi893BZPEGeT?= =?us-ascii?Q?jeRX7RStkbfPwRepw8vD9hJenjj89wTr18riXWh5BV8rOrlNMTA7MOSsivTi?= =?us-ascii?Q?IkaiIPFNgpxZ5d+jQcqJoNjgYjQ4hPtjwGUwz9c7WbQoEAMEnHvglKMhLdWB?= =?us-ascii?Q?KrKMgU/WzNrZfqRZ6YErB8F7hxxNmOYKHYl1IbBtgDoEEGCq+v0EjVTu9jdN?= =?us-ascii?Q?M1BkX47OXHzmZnv/HID0mBylnGAH3PiBBiaAG7ZZ922Ai3BWL/Pr8X07wFub?= =?us-ascii?Q?uIDgeGtbFNO9ChVMO5ISnqKbGye+njJnviJVE4ZG+su2LlYetM6KqezRLHvu?= =?us-ascii?Q?EJ00yySqzxzPjOjHCfK/V4QWi1hjH2LqQBXztx0Cy+OePobj28Xh66qmeTKS?= =?us-ascii?Q?E9bywo03XOa031v3EBWHt6c7Xf+l9LdlpKre?= X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(36860700013)(82310400026)(1800799024)(376014)(7416014);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Aug 2025 23:33:12.2393 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e96c6974-ef33-4031-2b67-08dde8e6c061 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000EE30.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB7520 Content-Type: text/plain; charset="utf-8" PCIe permits a device to ignore ATS invalidation TLPs, while processing a reset. This creates a problem visible to the OS where an ATS invalidation command will time out: e.g. an SVA domain will have no coordination with a reset event and can racily issue ATS invalidations to a resetting device. The PCIe spec in sec 10.3.1 IMPLEMENTATION NOTE recommends to disable and block ATS before initiating a Function Level Reset. It also mentions that other reset methods could have the same vulnerability as well. Now iommu_dev_reset_prepare/done() helpers are introduced for this matter. Use them in all the existing reset functions, which will attach the device to an IOMMU_DOMAIN_BLOCKED during a reset, so as to allow IOMMU driver to: - invoke pci_disable_ats() and pci_enable_ats(), if necessary - wait for all ATS invalidations to complete - stop issuing new ATS invalidations - fence any incoming ATS queries Signed-off-by: Nicolin Chen --- drivers/pci/pci.h | 2 ++ drivers/pci/pci-acpi.c | 12 ++++++-- drivers/pci/pci.c | 68 ++++++++++++++++++++++++++++++++++++++---- drivers/pci/quirks.c | 18 ++++++++++- 4 files changed, 92 insertions(+), 8 deletions(-) diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 34f65d69662e9..9700ebca55771 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -106,6 +106,8 @@ void pci_init_reset_methods(struct pci_dev *dev); int pci_bridge_secondary_bus_reset(struct pci_dev *dev); int pci_bus_error_reset(struct pci_dev *dev); int __pci_reset_bus(struct pci_bus *bus); +int pci_reset_iommu_prepare(struct pci_dev *dev); +void pci_reset_iommu_done(struct pci_dev *dev); =20 struct pci_cap_saved_data { u16 cap_nr; diff --git a/drivers/pci/pci-acpi.c b/drivers/pci/pci-acpi.c index ddb25960ea47d..3291424730824 100644 --- a/drivers/pci/pci-acpi.c +++ b/drivers/pci/pci-acpi.c @@ -969,6 +969,7 @@ void pci_set_acpi_fwnode(struct pci_dev *dev) int pci_dev_acpi_reset(struct pci_dev *dev, bool probe) { acpi_handle handle =3D ACPI_HANDLE(&dev->dev); + int ret =3D 0; =20 if (!handle || !acpi_has_method(handle, "_RST")) return -ENOTTY; @@ -976,12 +977,19 @@ int pci_dev_acpi_reset(struct pci_dev *dev, bool prob= e) if (probe) return 0; =20 + ret =3D pci_reset_iommu_prepare(dev); + if (ret) { + pci_err(dev, "failed to stop IOMMU\n"); + return ret; + } + if (ACPI_FAILURE(acpi_evaluate_object(handle, "_RST", NULL, NULL))) { pci_warn(dev, "ACPI _RST failed\n"); - return -ENOTTY; + ret =3D -ENOTTY; } =20 - return 0; + pci_reset_iommu_done(dev); + return ret; } =20 bool acpi_pci_power_manageable(struct pci_dev *dev) diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index b0f4d98036cdd..b4ca44ea6f494 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -25,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -95,6 +97,23 @@ bool pci_reset_supported(struct pci_dev *dev) return dev->reset_methods[0] !=3D 0; } =20 +/* + * Per PCIe r6.3, sec 10.3.1 IMPLEMENTATION NOTE, software disables ATS be= fore + * initiating a reset. Notify the iommu driver that enabled ATS. + */ +int pci_reset_iommu_prepare(struct pci_dev *dev) +{ + if (pci_ats_supported(dev)) + return iommu_dev_reset_prepare(&dev->dev); + return 0; +} + +void pci_reset_iommu_done(struct pci_dev *dev) +{ + if (pci_ats_supported(dev)) + iommu_dev_reset_done(&dev->dev); +} + #ifdef CONFIG_PCI_DOMAINS int pci_domains_supported =3D 1; #endif @@ -4529,13 +4548,22 @@ EXPORT_SYMBOL(pci_wait_for_pending_transaction); */ int pcie_flr(struct pci_dev *dev) { + int ret =3D 0; + if (!pci_wait_for_pending_transaction(dev)) pci_err(dev, "timed out waiting for pending transaction; performing func= tion level reset anyway\n"); =20 + /* Have to call it after waiting for pending DMA transaction */ + ret =3D pci_reset_iommu_prepare(dev); + if (ret) { + pci_err(dev, "failed to stop IOMMU\n"); + return ret; + } + pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR); =20 if (dev->imm_ready) - return 0; + goto done; =20 /* * Per PCIe r4.0, sec 6.6.2, a device must complete an FLR within @@ -4544,7 +4572,10 @@ int pcie_flr(struct pci_dev *dev) */ msleep(100); =20 - return pci_dev_wait(dev, "FLR", PCIE_RESET_READY_POLL_MS); + ret =3D pci_dev_wait(dev, "FLR", PCIE_RESET_READY_POLL_MS); +done: + pci_reset_iommu_done(dev); + return ret; } EXPORT_SYMBOL_GPL(pcie_flr); =20 @@ -4572,6 +4603,7 @@ EXPORT_SYMBOL_GPL(pcie_reset_flr); =20 static int pci_af_flr(struct pci_dev *dev, bool probe) { + int ret =3D 0; int pos; u8 cap; =20 @@ -4598,10 +4630,17 @@ static int pci_af_flr(struct pci_dev *dev, bool pro= be) PCI_AF_STATUS_TP << 8)) pci_err(dev, "timed out waiting for pending transaction; performing AF f= unction level reset anyway\n"); =20 + /* Have to call it after waiting for pending DMA transaction */ + ret =3D pci_reset_iommu_prepare(dev); + if (ret) { + pci_err(dev, "failed to stop IOMMU\n"); + return ret; + } + pci_write_config_byte(dev, pos + PCI_AF_CTRL, PCI_AF_CTRL_FLR); =20 if (dev->imm_ready) - return 0; + goto done; =20 /* * Per Advanced Capabilities for Conventional PCI ECN, 13 April 2006, @@ -4611,7 +4650,10 @@ static int pci_af_flr(struct pci_dev *dev, bool prob= e) */ msleep(100); =20 - return pci_dev_wait(dev, "AF_FLR", PCIE_RESET_READY_POLL_MS); + ret =3D pci_dev_wait(dev, "AF_FLR", PCIE_RESET_READY_POLL_MS); +done: + pci_reset_iommu_done(dev); + return ret; } =20 /** @@ -4632,6 +4674,7 @@ static int pci_af_flr(struct pci_dev *dev, bool probe) static int pci_pm_reset(struct pci_dev *dev, bool probe) { u16 csr; + int ret; =20 if (!dev->pm_cap || dev->dev_flags & PCI_DEV_FLAGS_NO_PM_RESET) return -ENOTTY; @@ -4646,6 +4689,12 @@ static int pci_pm_reset(struct pci_dev *dev, bool pr= obe) if (dev->current_state !=3D PCI_D0) return -EINVAL; =20 + ret =3D pci_reset_iommu_prepare(dev); + if (ret) { + pci_err(dev, "failed to stop IOMMU\n"); + return ret; + } + csr &=3D ~PCI_PM_CTRL_STATE_MASK; csr |=3D PCI_D3hot; pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr); @@ -4656,7 +4705,9 @@ static int pci_pm_reset(struct pci_dev *dev, bool pro= be) pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, csr); pci_dev_d3_sleep(dev); =20 - return pci_dev_wait(dev, "PM D3hot->D0", PCIE_RESET_READY_POLL_MS); + ret =3D pci_dev_wait(dev, "PM D3hot->D0", PCIE_RESET_READY_POLL_MS); + pci_reset_iommu_done(dev); + return ret; } =20 /** @@ -5111,6 +5162,12 @@ static int cxl_reset_bus_function(struct pci_dev *de= v, bool probe) if (rc) return -ENOTTY; =20 + rc =3D pci_reset_iommu_prepare(dev); + if (rc) { + pci_err(dev, "failed to stop IOMMU\n"); + return rc; + } + if (reg & PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR) { val =3D reg; } else { @@ -5125,6 +5182,7 @@ static int cxl_reset_bus_function(struct pci_dev *dev= , bool probe) pci_write_config_word(bridge, dvsec + PCI_DVSEC_CXL_PORT_CTL, reg); =20 + pci_reset_iommu_done(dev); return rc; } =20 diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c index d97335a401930..c1c32e57fe267 100644 --- a/drivers/pci/quirks.c +++ b/drivers/pci/quirks.c @@ -4225,6 +4225,22 @@ static const struct pci_dev_reset_methods pci_dev_re= set_methods[] =3D { { 0 } }; =20 +static int __pci_dev_specific_reset(struct pci_dev *dev, bool probe, + const struct pci_dev_reset_methods *i) +{ + int ret; + + ret =3D pci_reset_iommu_prepare(dev); + if (ret) { + pci_err(dev, "failed to stop IOMMU\n"); + return ret; + } + + ret =3D i->reset(dev, probe); + pci_reset_iommu_done(dev); + return ret; +} + /* * These device-specific reset methods are here rather than in a driver * because when a host assigns a device to a guest VM, the host may need @@ -4239,7 +4255,7 @@ int pci_dev_specific_reset(struct pci_dev *dev, bool = probe) i->vendor =3D=3D (u16)PCI_ANY_ID) && (i->device =3D=3D dev->device || i->device =3D=3D (u16)PCI_ANY_ID)) - return i->reset(dev, probe); + return __pci_dev_specific_reset(dev, probe, i); } =20 return -ENOTTY; --=20 2.43.0