From nobody Tue Dec 2 01:26:16 2025 Received: from CH4PR04CU002.outbound.protection.outlook.com (mail-northcentralusazon11013065.outbound.protection.outlook.com [40.107.201.65]) (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 EDAFD13AD05; Sat, 22 Nov 2025 01:57:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.201.65 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763776679; cv=fail; b=DoDreYFmajmCjFmASwlkhrGr0wRJ857NxBC6vKOaaVYeObe34cY2Pf/2OflvRFfE1i9pCu1Qr/+vEDwbg77YMZeYNU3EuPzl3fWEWKxp77LieWjYhInC5eJJIoP3VWqeU9qnqejKQ43VjD/Lky/gKeduMsgeOWpISQ0oYj06/tc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763776679; c=relaxed/simple; bh=zygvyRf8iVJZfTeOONOEj6jHA6bECqcSFQ3liTODmy0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bcMm/+76CcZ0pq4QdhIeJ/8leKPOC62l5oYygfrN93mxVKxmT8UgjICOkiNTzpihiF2sCi56qNasZAnQQp81IizvhfnBLAD2k3asu4O/nUGrURVIirRbxJRXtHJTyYXaYnPuSBzf5MKwObot6wJQx8EMF4D2Yb58VY5oIXssTNw= 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=CmhGFo0i; arc=fail smtp.client-ip=40.107.201.65 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="CmhGFo0i" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=vQSdpwVdXIUbBtVWzcHJytMEj9d7icDuc2ebf9Hv/wGaOl6IZS+KGgNbOQCQQcAcszRrbBTEGG9AIyvQtXeDufPRH5B6Yb/puPB/YNo/esFm+d1GSDRy9mYt5diyT/KroQZ8qENF+hAiZ8RlnPmIQhETSETFQOvYFJYbCulFZ5x3nQrzDkrqTW3MGxiHjhdyoKygfRF718lJjm0tVe91KbxCbrXa1D1ha6bzajaR0Yrx3U+y+pbCiB7cet7njUBfMGJc+d5M5KRYkrlZDfkdkE6qne9bY9/OdsFMNhb2SwBgdWuZW3dR3zqKrXx8IskHk1478MXrAsDFD856M61Reg== 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=mNY9x1RtcvkkzqkoxtEh/9kLnwrlK2GzglZgK+WuMC0=; b=F+zEMag8HgUi7/v14ENra57DSmXow2xTDcZbRuO+2acubK+l+pyp5rE+4T87k40GgSVSUJ3W5QU/ot7T38N3peJMHVPuU6LxEXkj8uQ8WWl1pWTSMRzgOQV6CNLt+BDOPgaUtqiFOZfPDTAr6F6+q+moC93XxJx4YmQZruZdKe/gBQFj4V63qre2KmFjuVIU1qWwavdCaqcfI1OV+n/HmPwW+C7bdzI6HV0KglR8pB2E6LltISR9HdgyEkbpLH8AgjqXb/UdaQ3RiPoWnwalHLbq90WA/eJam36IaFuNB8IQKs0SMkUanSskanUcOtLF4/YDqv/eLv05I5LzgfvRbA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) 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=mNY9x1RtcvkkzqkoxtEh/9kLnwrlK2GzglZgK+WuMC0=; b=CmhGFo0ilh7R27IRidcUngcqJgzwWgn9t0KZg7y6Va30N/vMRc3BzAjbBvwnItien7NIpRa+ls1V9uPPHEOAY5zLiNVF5dyFe6ZHpumv0IN3+GsHSdzFaH514h8Yfms0nCDR8k0ZEwvhDmOGFZjQeYBVToLglHoBAhB5FG4pQJ2QxzGYMqf3DooohvRt2JyUvHcpX+4kTp50loanLrvmQRnQpBSRgsGVQZkhwgp4eVGkmRU4/AJPH2C5gvo6YHUsDDhfHlWD4h7c6Ry/1QVHaUyvU4zfB9Eera685qsW6hjqsfzW3mjSQbZaRCYxJM4TRTOBmp31GFR5eNhGWDPbGw== Received: from BL1PR13CA0383.namprd13.prod.outlook.com (2603:10b6:208:2c0::28) by LV8PR12MB9620.namprd12.prod.outlook.com (2603:10b6:408:2a1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9343.10; Sat, 22 Nov 2025 01:57:54 +0000 Received: from BL02EPF0001A104.namprd05.prod.outlook.com (2603:10b6:208:2c0:cafe::fa) by BL1PR13CA0383.outlook.office365.com (2603:10b6:208:2c0::28) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9343.10 via Frontend Transport; Sat, 22 Nov 2025 01:57:54 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) 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.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by BL02EPF0001A104.mail.protection.outlook.com (10.167.241.135) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9343.9 via Frontend Transport; Sat, 22 Nov 2025 01:57:54 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Fri, 21 Nov 2025 17:57:46 -0800 Received: from drhqmail202.nvidia.com (10.126.190.181) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Fri, 21 Nov 2025 17:57:45 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.126.190.181) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Fri, 21 Nov 2025 17:57:44 -0800 From: Nicolin Chen To: , , , , CC: , , , , , , , , , , , , , , , Subject: [PATCH v7 1/5] iommu: Lock group->mutex in iommu_deferred_attach() Date: Fri, 21 Nov 2025 17:57:28 -0800 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: BL02EPF0001A104:EE_|LV8PR12MB9620:EE_ X-MS-Office365-Filtering-Correlation-Id: 62534e43-b3c3-4174-4052-08de296a8d0d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|82310400026|36860700013|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?+kv2YfsnkNt0kMCYVKtsZ8zyP1GJ3R8/jmda4KS+5y20ZMZzyHODGCsSGl/R?= =?us-ascii?Q?hEJKGqNVz9r8DWBEDghN/kSlt//TYeCtQe9lIF9w0nO7qvv7TVYfBIawY3oI?= =?us-ascii?Q?vRPQvTpwZ3JvwiHSl4xXsfT0jLhzjI/IOpRuZ9hdPAc96WIafBbwdGK8uwPb?= =?us-ascii?Q?JtT2DxZxw6YD55rh/G5/yY1ZfGxHMoMkeySZGcOtnfrdOEqg7Cg2Oy21oyl2?= =?us-ascii?Q?0HtsWCd6oh3H+l7FjInTP5T8sCrc495SioIU3aeFRqp3vmCJP70wWaJthSxu?= =?us-ascii?Q?bGQ72YBiMp+O2dV+R+GQCJymSoOR+8sHqjKuzMac9Bte33LjA+0PycUyYv8/?= =?us-ascii?Q?LQrxLNaXJORmsR2mgIe25lLkPaA5nl2AopK6K1bXuzTRMfu9Sz+qxtIeChtg?= =?us-ascii?Q?+azQ/GkpPUX5Q5x2EgeBvLUxwW8SZ1UHx5PEhopQZfNZlZAy1wxIjobDEj9v?= =?us-ascii?Q?wZBCaaLj/taDYPpdErsEHNFBmfXS0sjPqLzfKstOWFD7LXoUrKAFwoMgp0X7?= =?us-ascii?Q?q9j8lE3MhrlrYXs3G+C/N742E1qoSHYfgz6+xqoX93ni+VYAlX91uQHp5Elf?= =?us-ascii?Q?NyMgl/decmiju0CiQaEvTbcUYuonK8bvkQkBYXmxeXTY5QtKnNlZdOwteZ4t?= =?us-ascii?Q?YZYt0Sw0Gwmi3THmumyMt99slLNEl/70c662f1Egdt3RgNYYaQerD9QhWFzP?= =?us-ascii?Q?tjI21y2n28H2byTkAWtjW6qXmQLyeuo5i9w7ou1LesS84LwGlO+kofW/tFG0?= =?us-ascii?Q?N3brHG1ePe7WaxWq06zNL1f7wPsHCL661xR6kHxTCKxjiTYziSYJS+BxKRTp?= =?us-ascii?Q?dI34OqeclD8Hy7urJm/qEyNb8b0f1ZwGDi+mH4j32mOZFB4V0yVRfPlYNDxk?= =?us-ascii?Q?NOYYa/A1ZCg9vh+3afaBsUrut1O2wa/fixAJW1j5X4fabj/fLLsrRQ2ANHOy?= =?us-ascii?Q?CpyhkyYzghsyOYCKe9M+A1VtYZAB2a1pib/aBY7RKIEadcGgdZDsVJFEanL1?= =?us-ascii?Q?IKveDvtA54uXagiihOrpEOEnodl4btaeRdrVkLSxweGgDHqb/gn45rL3pcsz?= =?us-ascii?Q?Qmbp4oJgsLdatDGxEuHeh6Hg8lJuE0s3XS2NCOz7tJlahDnN3vHmkzjATUNi?= =?us-ascii?Q?uX6X1A7IjCEekBTOTnLAUcVwAxzlE+6sDGVzgypNOAR6HBotrLYyPa/YR52H?= =?us-ascii?Q?wMyqAqWNrazOca/oUxNCYIIXxM2Po1skUWmXpo7Q630Mj6zQtIYGCZSKaZKU?= =?us-ascii?Q?bOjW09B/H33J50dFPbVuFGeuyYu2qA+muAaNqdP0UGLeEQSkhgumcFA2AVsA?= =?us-ascii?Q?1EpuQbCjCdg0pWje3nuRLZXj7B7WNm7EGmBKpGjxj2HVMyA5PFGEZ4PN6wN0?= =?us-ascii?Q?JWoqHveK7U172iNOA6zDJlZ1/mfDL7YZ8L1ZMySpRLrY07K7iw4jkxc7++GL?= =?us-ascii?Q?18NikgUrz3Yj4z37uli3JcJ/zag9HlyHLemlERd+QjPwGwVQyagjT8HChm72?= =?us-ascii?Q?uDJuaDcRNzF28cWd3NnyZ3evc4hC1GdsFcUrm5KjkrhB3veHWsv7Vwj6at8N?= =?us-ascii?Q?LLgrE8PqYSe0fhzgz3U=3D?= X-Forefront-Antispam-Report: CIP:216.228.118.232;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge1.nvidia.com;CAT:NONE;SFS:(13230040)(7416014)(376014)(82310400026)(36860700013)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Nov 2025 01:57:54.1226 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 62534e43-b3c3-4174-4052-08de296a8d0d X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.232];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0001A104.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV8PR12MB9620 Content-Type: text/plain; charset="utf-8" The iommu_deferred_attach() function invokes __iommu_attach_device(), but doesn't hold 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(). Worth mentioning that the iommu_deferred_attach() will soon need to check group->resetting_domain that must be locked also. Thus, grab the mutex to guard __iommu_attach_device() like other callers. Reviewed-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Lu Baolu Signed-off-by: Nicolin Chen Tested-by: Dheeraj Kumar Srivastava --- 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 2ca990dfbb884..170e522b5bda4 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2185,10 +2185,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, NULL); + /* + * 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, NULL); } =20 void iommu_detach_device(struct iommu_domain *domain, struct device *dev) --=20 2.43.0 From nobody Tue Dec 2 01:26:16 2025 Received: from CO1PR03CU002.outbound.protection.outlook.com (mail-westus2azon11010008.outbound.protection.outlook.com [52.101.46.8]) (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 ACFB4253F11; Sat, 22 Nov 2025 01:58:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.46.8 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763776682; cv=fail; b=TFd/g2Uct3BorDaY88/mj+5Q9d44ZwpZapdhrRXPFvN2IoiiuPp5XVJP3Xj1rHwe5lXajV4iZ8LYRLMXNoK5dXuDhAQX2Tfx9GluEVPT1Cm7MmY2TMMoQ2FfIHMcPmYzlHR0+dLzy53NR6uDHuNY0Lg0gz547p4IhWHPXJxB7Lg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763776682; c=relaxed/simple; bh=SYorAc+87wj3DkYWD88E+XlPWUaxf/WpFuAo+FbC2+c=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=r+yK3XJLk7QOi5SBf/whQMQmspdJl1q58TncP+kS66tjBZrdk/XppUp6VTZ1gQfD09G2GvHWlnRDR0mwM1c1tO2a9k5kpsqIcJpGwZXdT0awMOz3fCd3sY1tpfzpoBPpAQlt0dv/FCl3C1x3BMViNgjIlJCEHqAzTOwkg+YTa+o= 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=IFoSU8s7; arc=fail smtp.client-ip=52.101.46.8 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="IFoSU8s7" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=k/e6zyN+eBzVlvAYbyktxZqJPzmPrnJF3e0Uonl71bes86fnfbebmTb9eCK6ek59FtE347DULqwpgGVlIvJ6HjRPKjKuFkQny6FvXYcSStC8LmLBAZ3GipqvhKcE35hkdBc5x9QUpbRIZKG329WlDWNmEdMpFb0V8Xxt2TX3rSY08yAXIqV9yhYBZmCMgN9OVPWKllNDMFhQ/mF7fqiRbbqN2CRTgpusokJ0uldztByhiAf5B9OdoQPrcwmeLeVUlWG6nDvjGWZdjQAEg7thTSYyYp9VgLInG43lmeiwaSApFPnnZseE/Obj5HqZxFcLc+lNJXYDgNNH0VUMiYZabQ== 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=WUR4BM/DW5FAcf29ydQ8n6TSp2s4y3ASMhhwrnUl9P8=; b=CjqPRShhIlH02aBi5kQi6FtKZ4NEl6Vgv/ZInEMffEwUJ9uRRGfFmf+4bWMzivoTESTi5pzX5wjKyKt4DDFfD6y3TYVOgDfE6K7csTNbrXS7g28VpJs+k/L9HSmxI7TCW4e0e6vUobNhgyBUHnQkc00DWfq+FmoTzyJpb57TSMjntOGrsZSKss4NS8JiA/wspIUT7LxlyKfUTCvov2PuETQUm/axac8G7TTZMBnfuPrqYiVhgg+XguTSbhz5RJpFsGNNSrlCQVAzCNOQlqJrA2fMz22jxZQXyn3xsZH9Rwk9Huyed0sTkSReAgHmDe/Hmt7O6jxuOzvp8JTXRUPeUw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) 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=WUR4BM/DW5FAcf29ydQ8n6TSp2s4y3ASMhhwrnUl9P8=; b=IFoSU8s7l80kH35fYijegslq5V5thzBmGwC5ZU2A10osd5HKAcf2JpHyixpwjpVsfvgJz7cEOiFWdnvSlo+OXDRD5LgGB76gMb62tOVwM/s2OSlQQ8hf+BWXeWSs1162mjglrQr372m4IXjoIeSLThjzs9DjZpmmsbS/Z9YuLZvN0/jpO9ovIAQOMxX4o3TtsqbEFP0GMZ9ee5KoT0AM89q1Jyx/kD5Oebu4xiwCe/rsGKQOsR1w50WTWjA43F1fGxjz5iwzImHM/v7r2+WleMkDNUwWnEfoGO1MIyH9jhy1ZOqXpulI9ZCzSgTDVVDteoixE/4mUTjfr9CBRWxc0A== Received: from BN0PR04CA0118.namprd04.prod.outlook.com (2603:10b6:408:ec::33) by IA0PR12MB8982.namprd12.prod.outlook.com (2603:10b6:208:481::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9343.11; Sat, 22 Nov 2025 01:57:57 +0000 Received: from BN1PEPF00005FFD.namprd05.prod.outlook.com (2603:10b6:408:ec:cafe::e0) by BN0PR04CA0118.outlook.office365.com (2603:10b6:408:ec::33) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9343.14 via Frontend Transport; Sat, 22 Nov 2025 01:57:53 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) 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.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by BN1PEPF00005FFD.mail.protection.outlook.com (10.167.243.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9343.9 via Frontend Transport; Sat, 22 Nov 2025 01:57:57 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Fri, 21 Nov 2025 17:57:47 -0800 Received: from drhqmail202.nvidia.com (10.126.190.181) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Fri, 21 Nov 2025 17:57:46 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.126.190.181) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Fri, 21 Nov 2025 17:57:45 -0800 From: Nicolin Chen To: , , , , CC: , , , , , , , , , , , , , , , Subject: [PATCH v7 2/5] iommu: Tidy domain for iommu_setup_dma_ops() Date: Fri, 21 Nov 2025 17:57:29 -0800 Message-ID: <6e8aacd34b038e7534fc5cb3fa21ab31b1af01ad.1763775108.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: BN1PEPF00005FFD:EE_|IA0PR12MB8982:EE_ X-MS-Office365-Filtering-Correlation-Id: 880375d0-6254-4ecd-c0e6-08de296a8ee2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|7416014|376014|1800799024|36860700013; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?Csc5NEtKV+WRsdvh7U6xEH3fy9Ipr5MkJ2q7TsFNJ5DRKf8V45Zn8Th4dtKH?= =?us-ascii?Q?GSYDzfhar2vTUvM4/VL/vsHa8loHHlEreLMsF4ezLruJbw+LbCpEubUCajxF?= =?us-ascii?Q?GoxlucgREtzBxpScEZ2a2cGgqurjN8Wl9uWx4sFWTlUX3ZxN038WxNtwWKVT?= =?us-ascii?Q?DfC7JcWz6TaBnT3CnvLfzaNUz60HSksvGBz9yYLjFRNwhxpULUPAFTvHsSl2?= =?us-ascii?Q?/JcHjaglpaS4mXTfzADisimHmEePMewRU/OBrAbV4CZb8uS7b3jd4D4klNRk?= =?us-ascii?Q?xm+1J6krE7DidvfUBgE9r+aHD6c2O5a8yukQ/OfakXNkWak8Z01b2KKFSh62?= =?us-ascii?Q?PuNJgMMONVsd2L2ObxcV6UkXCg4ZXX3+Jnv27/Xi1D97KgMwztcdRqxdv4/Y?= =?us-ascii?Q?HXPW8nA5rmXuJtcZApJOwFeEcm4blj0TZL0jLOz8hca3WqYErVAWXX0//wCM?= =?us-ascii?Q?ImQBJm8vaStQkVwA36xqSLR8UfPcGQAt6cP6Pj7DIFB4gz6zxBuuCcSap5EF?= =?us-ascii?Q?h+B/hjpcLqxwj3ZkJ7vPbWYq1F14FTn+zUQVT5lKZqlHUCJePD/r3awFFV8n?= =?us-ascii?Q?cGDbPLIA2cl9M4qAhTDKSBVVKXJ6qRjFJEloFY1kjiaxQ2hGM9Kj9wqMd2E0?= =?us-ascii?Q?nWHNpP5vhjlFVK+e8TfmCODW2vOFJ57Ye7GcVHf4SeQqrTW3+Lm62/NDNqeC?= =?us-ascii?Q?AUhyQbsXu5WL1ILfLTb0AQP8PlV/3deXfI5mRMLmDnjbAMt+0jzJqICU3/tG?= =?us-ascii?Q?dGBh468hEF114rTmBD2SPhUbwR4vwMH7Xq0jtfi2Dy53n0V3JLuKE/NwJp8d?= =?us-ascii?Q?KKcMn4ik3gDfjgWNIy2eyplFisINzqHXFSiN8/5UI7VvDrsnY4XRU4gzHRP9?= =?us-ascii?Q?ii93qkdm9K8oXokYUXrCrUnN6aMrIdNaQfSSYP/0QS2pPtcvKzZuS1KWBxfN?= =?us-ascii?Q?nbLewXc0vM9+udh4O9JPNcfl5bxKz+EoNhYP892IfmGiOw+CNcF0hziKRRcY?= =?us-ascii?Q?Kc+ajGGoRwlP37TNKe9FDP2V5ogtq3vXKxaYFzQ9PX9epL1W9TqoGsY7JeG1?= =?us-ascii?Q?T506a1j4ck1O+8GbymBiDVd447U6d/QluKH1fdcmeweVw3W2mRtBQQH3JTZX?= =?us-ascii?Q?eHLkdqSK3dV2p5U8bYbeKx3fw7h5MZWvmCxIAS4Ftgo6jBK8CflNnT5GRWE5?= =?us-ascii?Q?IsMiVjQ21SCiN6GuMoe4J6rLDCI0GhrTpp+Y5NurXHxatFGqqoSJ7hseovmG?= =?us-ascii?Q?zy47eaeoOLO3UYiIGhfPR4IF+yXN6joS6w2V8/HcOq2CyLqHiU0RFgRZr8Qf?= =?us-ascii?Q?ll5yBJCQ5C2rfnrUPaOetzUxY6rL7664wKfLaphcejX7u8wZ6jM6eJjc7nmh?= =?us-ascii?Q?Xnoe6XecZrQaABfdFy9nv7C8rlXDBh+Ky8i7U7K8bmEYoW9tWy7JrZIvfztU?= =?us-ascii?Q?sn3kqwAATdBwtbtz8HhM+eznTY1ssQb2+dJZf5uWme5YkWRpote3aezsxbXK?= =?us-ascii?Q?/V88iBGWtBdFh2Ba6h2XWVcXjN2RzjhBv0iIe1hT3fo1Y1C2/RTuJ814GZ4x?= =?us-ascii?Q?DLCdZI99WZtDJV75gug=3D?= X-Forefront-Antispam-Report: CIP:216.228.118.233;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge2.nvidia.com;CAT:NONE;SFS:(13230040)(82310400026)(7416014)(376014)(1800799024)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Nov 2025 01:57:57.1474 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 880375d0-6254-4ecd-c0e6-08de296a8ee2 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.233];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN1PEPF00005FFD.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB8982 Content-Type: text/plain; charset="utf-8" This function can only be called on the default_domain. Trivally pass it in. In all three existing cases, the default domain was just attached to the device. This avoids iommu_setup_dma_ops() calling iommu_get_domain_for_dev() that will be used by external callers. Suggested-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Lu Baolu Signed-off-by: Nicolin Chen Reviewed-by: Jason Gunthorpe Tested-by: Dheeraj Kumar Srivastava --- drivers/iommu/dma-iommu.h | 5 +++-- drivers/iommu/dma-iommu.c | 4 +--- drivers/iommu/iommu.c | 6 +++--- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/drivers/iommu/dma-iommu.h b/drivers/iommu/dma-iommu.h index eca201c1f9639..040d002525632 100644 --- a/drivers/iommu/dma-iommu.h +++ b/drivers/iommu/dma-iommu.h @@ -9,7 +9,7 @@ =20 #ifdef CONFIG_IOMMU_DMA =20 -void iommu_setup_dma_ops(struct device *dev); +void iommu_setup_dma_ops(struct device *dev, struct iommu_domain *domain); =20 int iommu_get_dma_cookie(struct iommu_domain *domain); void iommu_put_dma_cookie(struct iommu_domain *domain); @@ -26,7 +26,8 @@ extern bool iommu_dma_forcedac; =20 #else /* CONFIG_IOMMU_DMA */ =20 -static inline void iommu_setup_dma_ops(struct device *dev) +static inline void iommu_setup_dma_ops(struct device *dev, + struct iommu_domain *domain) { } =20 diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 7944a3af4545e..e8ffb50c66dbf 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -2096,10 +2096,8 @@ void dma_iova_destroy(struct device *dev, struct dma= _iova_state *state, } EXPORT_SYMBOL_GPL(dma_iova_destroy); =20 -void iommu_setup_dma_ops(struct device *dev) +void iommu_setup_dma_ops(struct device *dev, struct iommu_domain *domain) { - struct iommu_domain *domain =3D iommu_get_domain_for_dev(dev); - if (dev_is_pci(dev)) dev->iommu->pci_32bit_workaround =3D !iommu_dma_forcedac; =20 diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 170e522b5bda4..1e322f87b1710 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -661,7 +661,7 @@ static int __iommu_probe_device(struct device *dev, str= uct list_head *group_list } =20 if (group->default_domain) - iommu_setup_dma_ops(dev); + iommu_setup_dma_ops(dev, group->default_domain); =20 mutex_unlock(&group->mutex); =20 @@ -1949,7 +1949,7 @@ static int bus_iommu_probe(const struct bus_type *bus) return ret; } for_each_group_device(group, gdev) - iommu_setup_dma_ops(gdev->dev); + iommu_setup_dma_ops(gdev->dev, group->default_domain); mutex_unlock(&group->mutex); =20 /* @@ -3155,7 +3155,7 @@ static ssize_t iommu_group_store_type(struct iommu_gr= oup *group, =20 /* Make sure dma_ops is appropriatley set */ for_each_group_device(group, gdev) - iommu_setup_dma_ops(gdev->dev); + iommu_setup_dma_ops(gdev->dev, group->default_domain); =20 out_unlock: mutex_unlock(&group->mutex); --=20 2.43.0 From nobody Tue Dec 2 01:26:16 2025 Received: from CH1PR05CU001.outbound.protection.outlook.com (mail-northcentralusazon11010022.outbound.protection.outlook.com [52.101.193.22]) (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 B13F82D0C95; Sat, 22 Nov 2025 01:58:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.193.22 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763776683; cv=fail; b=tSXHAN1+qHwK6hFzskSiG+ZONMfyUtCD/scapjhyDNDDhb9UOtQhK5Q0wKpOslHFZvZ5VX3ZxrtVx5QZA1eiUcHz/oCi9UitlCfE0wOcEfY+9QFCDHBHgT2Ra9B9i+SzCHHYVetKdtkvcP0JNIM9ugRq8ZZjyaRBTEgWTImGeos= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763776683; c=relaxed/simple; bh=A64zjTYF3W/ybqoUMOHjdlmEcE4yQ1qCDuT5mgR9AMM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ElFyIj2G/GopXEvt0nUM9/YzAs+ROIgvw3na9NYd6BIXrnF9IiuE5CkAkt4+wBlHOLRoJSgv3Jt6HHifaPQCIP44yHpjDX1515UvzG4YVygs9BeLCTdgDl/F8mb0uTdWUszpN4PqOFoGfjhr1tmNY9rBGJZ5ddN2Uj10BqHTBcI= 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=Nu5ryfps; arc=fail smtp.client-ip=52.101.193.22 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="Nu5ryfps" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=EUuwMmhQtJ+4LbgnboLrSw3TYEkQdje0vLQpWqpumJhbjm/unTeJidHO1B1l+yIwfiVMXL5dGfvGayFqj0JDLyvC0+pdFpk02GgoSyQAafc+jJWw7LDsSgvqQ9JKhXyVCLx5dkOCwHYcj6HsH/POezAuyS+0hacgmLLO4UoL26PCsjCs7gQovPM1q2okOvIQ3Gv/ldt7TjnKt+MVwI5/81Ct/KoRBLKoScI5gXupTBe9e+riXNiLh4zgh51eOHJ0wBkbtxrCOyFmTkr4BxrHOwbVK3QKig0UnUALKxbTG7FWm+9UJdogWZuzv8bQcgkkCaiU+xwjX+Sy1rJuXKFOJA== 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=Svzub6DeZ2jA3O3RdEx2V+Smcw0K4U3C6DATv57Flio=; b=r7ZOXV2Nkc+cOaG4aySBoMNYXIQ8YB8QhmDKn55hyw5S8hnFQxtZ+A4/IGPiwTHChBXwAQVHQ7/US+afPA9/UFmrmX4sDX8XCA5RKB1UNESOe277DnVPUsKQf3zypWF/LB7SNtdfZP+9WAZVybE5T/zuTCB0ZXjrCGlKTLMFWF8h9Aa+NmHhxkI6QY/1ZfsEsvcvhPrjEZju/poTx6eKMh26q2+76ES714tq5WBKmr+yjomqX7lvYDOYMFPYOvjnSY5FKVjG7XlNX11yLuQ1dS/MJXs9ef6LUoSAIm3TSwTbVAA5uC9CLkL3VzzcY8Q9oku/T7IQSZhx15slSAk20g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) 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=Svzub6DeZ2jA3O3RdEx2V+Smcw0K4U3C6DATv57Flio=; b=Nu5ryfps+5DdUj1g3dO9wHQ66vS5iEim4zH0/S3g8Xk4XEB2nlJoE9m0qsmKMTCxeYQv1Tqon4zXxyN6zbCrob2EFk8YpzUxcU+p9yRgQs++Aapr9pvS49rUGismmGTqm7e9RrWfndRMKHaINUGeHaxp9jJ6+VXV+/3G/RqsRg37+4I1RzQuGkdEXB41OAJxM4y1biGkjc4MzNILKesRjUIsbKGkSabNRKW3R7zmT8yeb7QJh8sUSJJjz4PUttVPlYKNb21IZXiqYx9ZRn+DZGN9bsj9u6kRlswCSJfmW+BiBIR8lvsGix4YW3r3RxwIUR39jjtQH2xRxp+O86e/gg== Received: from BLAPR03CA0068.namprd03.prod.outlook.com (2603:10b6:208:329::13) by SA1PR12MB7272.namprd12.prod.outlook.com (2603:10b6:806:2b6::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9343.10; Sat, 22 Nov 2025 01:57:56 +0000 Received: from BL02EPF0001A105.namprd05.prod.outlook.com (2603:10b6:208:329:cafe::ee) by BLAPR03CA0068.outlook.office365.com (2603:10b6:208:329::13) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9343.10 via Frontend Transport; Sat, 22 Nov 2025 01:57:44 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) 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.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by BL02EPF0001A105.mail.protection.outlook.com (10.167.241.137) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9343.9 via Frontend Transport; Sat, 22 Nov 2025 01:57:55 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Fri, 21 Nov 2025 17:57:48 -0800 Received: from drhqmail202.nvidia.com (10.126.190.181) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Fri, 21 Nov 2025 17:57:48 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.126.190.181) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Fri, 21 Nov 2025 17:57:47 -0800 From: Nicolin Chen To: , , , , CC: , , , , , , , , , , , , , , , Subject: [PATCH v7 3/5] iommu: Add iommu_driver_get_domain_for_dev() helper Date: Fri, 21 Nov 2025 17:57:30 -0800 Message-ID: <71f3af9b3c1cd02eb92484d3d3e9fa89dbb2a928.1763775108.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: BL02EPF0001A105:EE_|SA1PR12MB7272:EE_ X-MS-Office365-Filtering-Correlation-Id: 602caba6-d83c-4618-3958-08de296a8de0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|36860700013|1800799024|82310400026; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?gnkLIWgLd6AS2zJnv+p2dDCrgOJtg1vxddzmZ0jI2NYfHYgo2tc/8bA0/ZE2?= =?us-ascii?Q?OvTLPnV1+bvEDCndvtciyMiBE3Tsw3K//8YPfOoROa8RKZxPJD4/9ZZGvJDP?= =?us-ascii?Q?0T2TOBUxFqtAf30ay93ppH7ANYnnNc/ln6062rW/HdmGuiIzbk1M9gwsseAJ?= =?us-ascii?Q?eJqVq4rbLnkUw0yJRQ9yfzKzIphzA5TCiaL+yzPCZPj1ppQ/Y25D5grM0NZ+?= =?us-ascii?Q?8vnbml+B15Bpm+z7/HbugHn4PXvlEGwrDhK8BpOX6nQw9k3bpOgd9CDmotJ8?= =?us-ascii?Q?E4/3xQyz3AwYJCjDOOwcyn5C8fhKwWHTCE461FEsB9WH3BIl/l73d+Szn6tp?= =?us-ascii?Q?TXcWvDzGCYJJfHBYdifqdOgXnuu5jFjDMRQU383SXw7oZDn1BZ2yC/SKre3U?= =?us-ascii?Q?oe0nz3hYdRqLW7y1LwpQkexdce7NcAGzGS+b+RRML1n1t23RNCxTx5SChx53?= =?us-ascii?Q?AHNeN2Bom1DyRbdOmqFjC9J2ZGxr9C9dQ0/mLQ7ZBz3ouoiNdNYXElieP0tD?= =?us-ascii?Q?VIpywsuNfCdzjzio3SY/vAo5YIejMVWuP9bZskKgAcUFl9bB0YFqXK2RqbRd?= =?us-ascii?Q?6bJTUatqaPJc6WeTZlIGr9VyXP2iAZKywTDkTlHafvAtr02sNSvBS5ZyBo2J?= =?us-ascii?Q?ttUclhmNZi0wN/5oVaFbg5MebtTDwsBXGZnKNgGy1LUKMfp+Qryhw7+qwLwM?= =?us-ascii?Q?KH+sV2vETJQ8V5dEmi2skRpSCzQEMjcqN4q9sH8xRE9014B+OKahd0qB87ba?= =?us-ascii?Q?9aLQtz7EMGGknpO07uecqZtNvu+D0nfQ4oeCoY4CJWL2ulI+XAvYvc1kj/pi?= =?us-ascii?Q?pPpM70sZMndcvzEtxStg78LODbzZ1l+8sddtdlPwniUHydRhCux0U7D4Ey7s?= =?us-ascii?Q?HqbTNMZFFEccL5wzC0ONrJPOcklhAQ0R2Neqm0tTegQQSnn1w1t7OKnSmGVf?= =?us-ascii?Q?R0acFgUUstzoO+1fHu3Lu8WcWPTQ9sWJrrBBVZs6MmNNwtEyMKfqcIa62/h/?= =?us-ascii?Q?MplBbrTrDH76KiXFlVwVH/hstnBLrdpeN63IqBsQoup2xg4FtPuVjozxrAyG?= =?us-ascii?Q?epx4ihfkmNLP9m8X3h7v8P9OfaMTfdd2/NcIqAPH4On++pxdMofEVu00HXxQ?= =?us-ascii?Q?R0zZn3zfaWR2wVxxvr8MjS6tM0TGPky3/Aj9xuNKiydqY0zhuEZf1+wCqK6t?= =?us-ascii?Q?O5q92jhHcJc+7eo2eC4hDs997G+nl6dTXhBOtCEojtuWfvHjG7dEj35FdjMk?= =?us-ascii?Q?P1hjhEe1+qYlqFS860M6jwekjhSXN4FRNSBbFNflGyUctRWgGtAO+x+7+SfO?= =?us-ascii?Q?A7C3K7I4cbgQ7h9LWKo/wE7z3Hn+UTzhxFj4CIiTWNT6D4+Lq9usdwEBmIS1?= =?us-ascii?Q?yKBpBIJNCDZQWKnq45Cw4KBlyWVWXM3NzTL2JgX7Rhlj3O/MtZujCz3derBn?= =?us-ascii?Q?znSH1fAdFwuYs6GGZspU+ul4AwatCUXIcyEkKgmqXV8pngeebvfMT0GDtUNt?= =?us-ascii?Q?IFaKNQklrvo2BZTANVTFC2pG9OXwpujDrrrnNewkPtzYOB2gR5k93TxS1HsB?= =?us-ascii?Q?mDcUO8qt8MYEEBCZIlk=3D?= X-Forefront-Antispam-Report: CIP:216.228.118.232;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge1.nvidia.com;CAT:NONE;SFS:(13230040)(376014)(7416014)(36860700013)(1800799024)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Nov 2025 01:57:55.4927 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 602caba6-d83c-4618-3958-08de296a8de0 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.232];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0001A105.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB7272 Content-Type: text/plain; charset="utf-8" There is a need to stage a resetting PCI device to temporarily the blocked domain and then attach back to its previously attached domain after reset. This can be simply done by keeping the "previously attached domain" in the iommu_group->domain pointer while adding an iommu_group->resetting_domain, which gives troubles to IOMMU drivers using the iommu_get_domain_for_dev() for a device's physical domain in order to program IOMMU hardware. And in such for-driver use cases, the iommu_group->mutex must be held, so it doesn't fit in external callers that don't hold the iommu_group->mutex. Introduce a new iommu_driver_get_domain_for_dev() helper, exclusively for driver use cases that hold the iommu_group->mutex, to separate from those external use cases. Add a lockdep_assert_not_held to the existing iommu_get_domain_for_dev() and highlight that in a kdoc. Reviewed-by: Kevin Tian Reviewed-by: Lu Baolu Signed-off-by: Nicolin Chen Reviewed-by: Jason Gunthorpe Tested-by: Dheeraj Kumar Srivastava --- include/linux/iommu.h | 1 + drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 5 ++-- drivers/iommu/iommu.c | 28 +++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 801b2bd9e8d49..a42a2d1d7a0b7 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_driver_get_domain_for_dev(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 a33fbd12a0dd9..412d1a9b31275 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_driver_get_domain_for_dev(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_driver_get_domain_for_dev(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/iommu.c b/drivers/iommu/iommu.c index 1e322f87b1710..672597100e9a0 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2217,6 +2217,15 @@ void iommu_detach_device(struct iommu_domain *domain= , struct device *dev) } EXPORT_SYMBOL_GPL(iommu_detach_device); =20 +/** + * iommu_get_domain_for_dev() - Return the DMA API domain pointer + * @dev: Device to query + * + * This function can be called within a driver bound to dev. The returned + * pointer is valid for the lifetime of the bound driver. + * + * It should not be called by drivers with driver_managed_dma =3D true. + */ struct iommu_domain *iommu_get_domain_for_dev(struct device *dev) { /* Caller must be a probed driver on dev */ @@ -2225,10 +2234,29 @@ 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 +/** + * iommu_driver_get_domain_for_dev() - Return the driver-level domain poin= ter + * @dev: Device to query + * + * This function can be called by an iommu driver that wants to get the ph= ysical + * domain within an iommu callback function where group->mutex is held. + */ +struct iommu_domain *iommu_driver_get_domain_for_dev(struct device *dev) +{ + struct iommu_group *group =3D dev->iommu_group; + + lockdep_assert_held(&group->mutex); + + return group->domain; +} +EXPORT_SYMBOL_GPL(iommu_driver_get_domain_for_dev); + /* * 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 Tue Dec 2 01:26:16 2025 Received: from DM1PR04CU001.outbound.protection.outlook.com (mail-centralusazon11010057.outbound.protection.outlook.com [52.101.61.57]) (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 CDCEE2DAFB9; Sat, 22 Nov 2025 01:58:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.61.57 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763776687; cv=fail; b=AsmP+KTpbVm5fURKX9Z9Cop8rxpgbQDI0u4dcJKwuGAg0zBYuY7qWMtWnJZwjJYf+KsfDkMDPMhlUwfVX6bKM6wD4oLLl2WOle/oT6WKtm77lzm1e+SQEQ28nnd0iguVuFk+X3nITj6dEa0Bl1TNE/Bnc65eN4X7pkT9u6vAanQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763776687; c=relaxed/simple; bh=4wuHw2kyqu5XLIFw4TgRWDEpTPxvIn6sQRSJlDN/d/I=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=YpBMcDL/Hl1aPtpCjqE16yd2BZavxWRrYPQJVMpMPJVJ+nDxkSVJqca8cH6JKfe2fqzWYc96iOu4pLWWYn1p6zHqz1VbVNNNdQaFE+ObsREdVQE8C726U24zvpPKimtGRbKYLRTd8llTuLchlMMUMjpw1BvR33NoB7JGWydIc98= 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=QLfNHyy/; arc=fail smtp.client-ip=52.101.61.57 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="QLfNHyy/" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=rd5V7oEpaPbClFfB4q3gN5lQX5pkUhTBLLeT5re25gJUOyI1CrJqp4+50K4NgiMf3WWo2hKqGECYcByz6ks8qBw+NYsAz/wj+DDRHeLq4Ia2hFyPEfhoAviKZPxTJV1kdzQVknAfPa9UvjDCTTeVwHQrGwet+j4RsZiYOa+1DXQBawvSw+h0gpoXj5OXUGju1ORyOrMdRLx0O9UAl5FDs76T6W5u5k6ioosb/D3Y037OSZ2DHW7hBtC3OY6UykMHJQxWybYB20YhWeUodLVI++LI2d5Q+WNtMqdFSceTx2btHQNaBTmOXBK6cFJ7nkm20yMjeMzBGVu8BAUGllXoXg== 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=jRoOiK9onYNU01HzF9z8mdfTYC7CpXYN3EiE/3I8Upw=; b=MiEdxWlrMCeqpKi5Flvz9ilPsJOcf0vKMr88a3HqC7ui/AyMXpb40BPr7oTLKxByEV/Z2DSilxOO3xvuwla4znn2WIpTW+qn2kJacoT3G4Np8uaV0Cnn8AvrixUUVzpqs97F9GojK3uq9IxFNJKRNl4PyWdCN1zyFVS7MyJIspDG0I67fRS8ovWq3felFND9Y1xG7ijw1QJcuTZrArPml3VRVYnut7VeQRf0JFQUNraDMWT+/DNXQPD0F5lFCnNCKAYiDz7viEfoK08+UCukgYhs6RVDeplbJKkBioiNE2IcZkSO2S3Yux2rEGEtFdS9ht9VCW65JaVKFTP8B54VMg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) 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=jRoOiK9onYNU01HzF9z8mdfTYC7CpXYN3EiE/3I8Upw=; b=QLfNHyy/+6WdIXOQJeu/iUdshlJklAqh+ImB7nhJ+Muovj4LNIiFWX9dk/x3iRVfr3CcadsoHLfna8up954a911Hner7DnIzHJbqoqCOx4haBCAeGNu9fWYSMc4rlNZux04zjJiXyr4qnqjQCOURel61M5Ht2akRzY9sy6VAFnx+utFIfSQb2SWr5ug9tJfCfuMjZOG/W5/0FzJmY3hm8nlp39zsVQkFUwuz46OVZdz/UqqjaJRJ4lf/ms/78Aiz5KlLIHPxDURGrucJgldmBtBIf1C8JxnvbGgdAUlJQNdP0Pu5Tno+DgV63V1pY0tY8oiCM7Ft8v+O4i7nIYX4Sw== Received: from BLAPR03CA0081.namprd03.prod.outlook.com (2603:10b6:208:329::26) by MW3PR12MB4491.namprd12.prod.outlook.com (2603:10b6:303:5c::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9343.12; Sat, 22 Nov 2025 01:57:57 +0000 Received: from BL02EPF0001A105.namprd05.prod.outlook.com (2603:10b6:208:329:cafe::4c) by BLAPR03CA0081.outlook.office365.com (2603:10b6:208:329::26) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9343.11 via Frontend Transport; Sat, 22 Nov 2025 01:57:35 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) 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.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by BL02EPF0001A105.mail.protection.outlook.com (10.167.241.137) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9343.9 via Frontend Transport; Sat, 22 Nov 2025 01:57:57 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Fri, 21 Nov 2025 17:57:49 -0800 Received: from drhqmail202.nvidia.com (10.126.190.181) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Fri, 21 Nov 2025 17:57:49 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.126.190.181) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Fri, 21 Nov 2025 17:57:48 -0800 From: Nicolin Chen To: , , , , CC: , , , , , , , , , , , , , , , Subject: [PATCH v7 4/5] iommu: Introduce pci_dev_reset_iommu_prepare/done() Date: Fri, 21 Nov 2025 17:57:31 -0800 Message-ID: <31486e8017284e547b04d2be5110522a777d8379.1763775108.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: BL02EPF0001A105:EE_|MW3PR12MB4491:EE_ X-MS-Office365-Filtering-Correlation-Id: 17c84a73-576c-4830-c3f2-08de296a8ee4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|82310400026|36860700013|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?6cUlPzKVtTbGPQoH7v6sVaXB6+iB308VhmKs282SpuNOGo1Dna4ycD8mbmLa?= =?us-ascii?Q?iR/YSXaNIcDOI9pBtE8YGpWeWvciFod1dFjWwgOda2GlSPgiqKQfZVkGNfIC?= =?us-ascii?Q?vp6JyalCggSt+NH9XSkOLHdAo4wSNIoEJgEHSDWjTOZ6Q0+T/rbKaFWBoh0A?= =?us-ascii?Q?3rRNm4M5fcfrf0o43zOzR3glUpzxCtKJ27FVGDj4fWAhP6jmkc3bNl+sedf1?= =?us-ascii?Q?9mj9sFCtIQ3RvUfVS/kwCtW6jHaoym8S6I15SsNA4+7nRdBrCWp2pgZBl6Uv?= =?us-ascii?Q?1lasmHloyIOA5WPgBCzi8qIG2PlhZTyke43Ge1HGAHrJhb8TmgOZDiastjJ3?= =?us-ascii?Q?GL2JY8+h7ea2Ghfvm45r5+DR/kyqJN6zvhD4V22HJGg6uP77tYT7DLnH4+zR?= =?us-ascii?Q?MiYyedz/ngTlRfBpYP3BrKGaQX6nOQHbpPo0xo9uqGFgkEe/PerJMP2z8Kd7?= =?us-ascii?Q?FtStI2qqFv4QzKxNISn41bRH2HxJCFG4LfieQiKZBOj4kjpmxSu9cwL2SPkq?= =?us-ascii?Q?qW0Aaja7zrGd0VXRqaFitreYA5hJ2OONHEBYjHO4nzDopjvOcqhhgVLfwTJk?= =?us-ascii?Q?JjW7q8qq0cbys6hj3IDQneT0yldd55q+DfWO5gWRho6Gac2nJl9loNJIWnRa?= =?us-ascii?Q?vA65/ljdZEdRnFVjJ422ukYsGh86ntYjINJe9m1HvTjlCplEYvwucWD+81V9?= =?us-ascii?Q?hblIidSNbqFTH0oscUAdIs2Y+243TaTJwTsVZezOvJDtOSPXFJQEmPM9Dgk9?= =?us-ascii?Q?cMbXtxxRaI954sQBfNTZLAPo1OEBzSnMusGDIC7qCth407+7IpfgU4Czr6xj?= =?us-ascii?Q?FgtRAdcRO0Vm6E83+Fvc2hWz/478XO+OdkosYD+Q9th4Q+w8x08rj9HpwxY/?= =?us-ascii?Q?pv5t6iI+ysJ4+aiRueF6IlGErFv54dXmlXpaIYffk4k4t97ke2ySI7MOdH3J?= =?us-ascii?Q?DojD+aw6oiPYCY8ru2mDISu7Z6ey/jZszxD00XJpXw0rJZJ91Dm/Kgk6zsDk?= =?us-ascii?Q?tQ8NN8ULjVuek/Okye8cGw0QeeidGkEh1jffiki34naVv46zDFTb5f3nB5VN?= =?us-ascii?Q?6VsIA6OYEVTwUrOXqNoyuPmcaNHZvxC/197xR7prRMZRVpUnpJBecw6srkEn?= =?us-ascii?Q?900+gS1GkHalQXmOrq3DApYJ7iNCDrehUOsuWfJsONhquEV49s3kgj0Ro+f8?= =?us-ascii?Q?4nMlyAbRRRlQZzda+z+/HZzrC5vPNquvnbRDYt0F3lRRhPyROhCQ/X3/cCxY?= =?us-ascii?Q?tvtJIBlR1hfE1MEJNUTZLi2W2SgY8tdLpfMF5b2FvAC8KnQmbX56zp/LDkY+?= =?us-ascii?Q?PBEG6HfQ9eJ1zuhgwSmarJpeBe3E/IwT5dH++sqkinwGsaE1VzoxQRTffjQq?= =?us-ascii?Q?kvhAB/hpeEB3nwA0x8gjmE6wom0CUlwcsuJ30iFyjyZdYrgE0V4Bt+DYpZQw?= =?us-ascii?Q?qJnAPp/yAC95yOEKBQvBQLDjBBSehXayDFarokqKHLPd3Juch2sZJ93yuOtu?= =?us-ascii?Q?c1EeXwn4msU7ekkI04zH1gqH2V6fgMUoPIB2sQysIbgHgCDeBHLWoHhX33xz?= =?us-ascii?Q?I1f5wEWAHEw0WgRgIlM=3D?= X-Forefront-Antispam-Report: CIP:216.228.118.232;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge1.nvidia.com;CAT:NONE;SFS:(13230040)(376014)(7416014)(82310400026)(36860700013)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Nov 2025 01:57:57.2129 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 17c84a73-576c-4830-c3f2-08de296a8ee4 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.232];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0001A105.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4491 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 r6.0, sec 10.3.1 IMPLEMENTATION NOTE recommends SW 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 RID/PASID domains group->blocking_domain so that the 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 resetting_domain in the iommu_group structure to reject any concurrent attach_dev/set_dev_pasid call during a reset for a concern of compatibility failure. Since this changes the behavior of an attach operation, update the uAPI accordingly. Note that there are two corner cases: 1. Devices in the same iommu_group Since an attachment is always per iommu_group, this means that any sibling devices in the iommu_group cannot change domain, to prevent race conditions. 2. An SR-IOV PF that is being reset while its VF is not In such case, the VF itself is already broken. So, there is no point in preventing PF from going through the iommu reset. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Nicolin Chen Reviewed-by: Jason Gunthorpe Tested-by: Dheeraj Kumar Srivastava --- include/linux/iommu.h | 13 +++ include/uapi/linux/vfio.h | 4 + drivers/iommu/iommu.c | 173 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index a42a2d1d7a0b7..3e10fce05b7e3 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -1186,6 +1186,10 @@ void iommu_detach_device_pasid(struct iommu_domain *= domain, struct device *dev, ioasid_t pasid); ioasid_t iommu_alloc_global_pasid(struct device *dev); void iommu_free_global_pasid(ioasid_t pasid); + +/* PCI device reset functions */ +int pci_dev_reset_iommu_prepare(struct pci_dev *pdev); +void pci_dev_reset_iommu_done(struct pci_dev *pdev); #else /* CONFIG_IOMMU_API */ =20 struct iommu_ops {}; @@ -1509,6 +1513,15 @@ static inline ioasid_t iommu_alloc_global_pasid(stru= ct device *dev) } =20 static inline void iommu_free_global_pasid(ioasid_t pasid) {} + +static inline int pci_dev_reset_iommu_prepare(struct pci_dev *pdev) +{ + return 0; +} + +static inline void pci_dev_reset_iommu_done(struct pci_dev *pdev) +{ +} #endif /* CONFIG_IOMMU_API */ =20 #ifdef CONFIG_IRQ_MSI_IOMMU diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 75100bf009baf..4aee2af1b6cbe 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -963,6 +963,10 @@ struct vfio_device_bind_iommufd { * hwpt corresponding to the given pt_id. * * Return: 0 on success, -errno on failure. + * + * When a device is resetting, -EBUSY will be returned to reject any concu= rrent + * attachment to the resetting device itself or any sibling device in the = IOMMU + * group having the resetting device. */ struct vfio_device_attach_iommufd_pt { __u32 argsz; diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 672597100e9a0..0665dedd91b2d 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -61,6 +61,11 @@ struct iommu_group { int id; struct iommu_domain *default_domain; struct iommu_domain *blocking_domain; + /* + * During a group device reset, @resetting_domain points to the physical + * domain, while @domain points to the attached domain before the reset. + */ + struct iommu_domain *resetting_domain; struct iommu_domain *domain; struct list_head entry; unsigned int owner_cnt; @@ -2195,6 +2200,15 @@ int iommu_deferred_attach(struct device *dev, struct= iommu_domain *domain) =20 guard(mutex)(&dev->iommu_group->mutex); =20 + /* + * This is a concurrent attach during a device reset. Reject it until + * pci_dev_reset_iommu_done() attaches the device to group->domain. + * + * Note that this might fail the iommu_dma_map(). But there's nothing + * more we can do here. + */ + if (dev->iommu_group->resetting_domain) + return -EBUSY; return __iommu_attach_device(domain, dev, NULL); } =20 @@ -2253,6 +2267,17 @@ struct iommu_domain *iommu_driver_get_domain_for_dev= (struct device *dev) =20 lockdep_assert_held(&group->mutex); =20 + /* + * Driver handles the low-level __iommu_attach_device(), including the + * one invoked by pci_dev_reset_iommu_done() re-attaching the device to + * the cached group->domain. In this case, the driver must get the old + * domain from group->resetting_domain rather than group->domain. This + * prevents it from re-attaching the device from group->domain (old) to + * group->domain (new). + */ + if (group->resetting_domain) + return group->resetting_domain; + return group->domain; } EXPORT_SYMBOL_GPL(iommu_driver_get_domain_for_dev); @@ -2409,6 +2434,13 @@ static int __iommu_group_set_domain_internal(struct = iommu_group *group, if (WARN_ON(!new_domain)) return -EINVAL; =20 + /* + * This is a concurrent attach during a device reset. Reject it until + * pci_dev_reset_iommu_done() attaches the device to group->domain. + */ + if (group->resetting_domain) + return -EBUSY; + /* * Changing the domain is done by calling attach_dev() on the new * domain. This switch does not have to be atomic and DMA can be @@ -3527,6 +3559,16 @@ int iommu_attach_device_pasid(struct iommu_domain *d= omain, return -EINVAL; =20 mutex_lock(&group->mutex); + + /* + * This is a concurrent attach during a device reset. Reject it until + * pci_dev_reset_iommu_done() attaches the device to group->domain. + */ + if (group->resetting_domain) { + ret =3D -EBUSY; + goto out_unlock; + } + for_each_group_device(group, device) { /* * Skip PASID validation for devices without PASID support @@ -3610,6 +3652,16 @@ int iommu_replace_device_pasid(struct iommu_domain *= domain, return -EINVAL; =20 mutex_lock(&group->mutex); + + /* + * This is a concurrent attach during a device reset. Reject it until + * pci_dev_reset_iommu_done() attaches the device to group->domain. + */ + if (group->resetting_domain) { + ret =3D -EBUSY; + goto out_unlock; + } + entry =3D iommu_make_pasid_array_entry(domain, handle); curr =3D xa_cmpxchg(&group->pasid_array, pasid, NULL, XA_ZERO_ENTRY, GFP_KERNEL); @@ -3867,6 +3919,127 @@ int iommu_replace_group_handle(struct iommu_group *= group, } EXPORT_SYMBOL_NS_GPL(iommu_replace_group_handle, "IOMMUFD_INTERNAL"); =20 +/** + * pci_dev_reset_iommu_prepare() - Block IOMMU to prepare for a PCI device= reset + * @pdev: PCI device that is going to enter a reset routine + * + * The PCIe r6.0, sec 10.3.1 IMPLEMENTATION NOTE recommends to disable and= block + * ATS before initiating a reset. This means that a PCIe device during the= reset + * routine wants to block any IOMMU activity: translation and ATS invalida= tion. + * + * This function attaches the device's RID/PASID(s) the group->blocking_do= main, + * setting the group->resetting_domain. This allows the IOMMU driver pausi= ng any + * IOMMU activity while leaving the group->domain pointer intact. Later wh= en the + * reset is finished, pci_dev_reset_iommu_done() can restore everything. + * + * Caller must use pci_dev_reset_iommu_prepare() with pci_dev_reset_iommu_= done() + * before/after the core-level reset routine, to unset the resetting_domai= n. + * + * Return: 0 on success or negative error code if the preparation failed. + * + * 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 pci_dev_reset_iommu_prepare(struct pci_dev *pdev) +{ + struct iommu_group *group =3D pdev->dev.iommu_group; + unsigned long pasid; + void *entry; + int ret; + + if (!pci_ats_supported(pdev) || !dev_has_iommu(&pdev->dev)) + return 0; + + guard(mutex)(&group->mutex); + + /* Re-entry is not allowed */ + if (WARN_ON(group->resetting_domain)) + return -EBUSY; + + 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, &pdev->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(&pdev->dev, pasid, + pasid_array_entry_to_domain(entry)); + + group->resetting_domain =3D group->blocking_domain; + return ret; +} +EXPORT_SYMBOL_GPL(pci_dev_reset_iommu_prepare); + +/** + * pci_dev_reset_iommu_done() - Restore IOMMU after a PCI device reset is = done + * @pdev: PCI device that has finished a reset routine + * + * After a PCIe device finishes a reset routine, it wants to restore its I= OMMU + * 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 successful pci_dev_reset_iommu_prepare(). + * + * Note that, although unlikely, there is a risk that re-attaching domains= might + * fail due to some unexpected happening like OOM. + */ +void pci_dev_reset_iommu_done(struct pci_dev *pdev) +{ + struct iommu_group *group =3D pdev->dev.iommu_group; + unsigned long pasid; + void *entry; + + if (!pci_ats_supported(pdev) || !dev_has_iommu(&pdev->dev)) + return; + + guard(mutex)(&group->mutex); + + /* pci_dev_reset_iommu_prepare() was bypassed for the device */ + if (!group->resetting_domain) + return; + + /* pci_dev_reset_iommu_prepare() was not successfully called */ + if (WARN_ON(!group->blocking_domain)) + return; + + /* Re-attach RID domain back to group->domain */ + if (group->domain !=3D group->blocking_domain) { + WARN_ON(__iommu_attach_device(group->domain, &pdev->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)); + + group->resetting_domain =3D NULL; +} +EXPORT_SYMBOL_GPL(pci_dev_reset_iommu_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 Tue Dec 2 01:26:16 2025 Received: from CH4PR04CU002.outbound.protection.outlook.com (mail-northcentralusazon11013014.outbound.protection.outlook.com [40.107.201.14]) (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 A3C462DF153; Sat, 22 Nov 2025 01:58:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.201.14 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763776690; cv=fail; b=I4acrSAtKeFzLqiNKcflvavdC/a6bS4frSLlvz8jezXmWYtZGhU+dVTGX5hn40h7A3o/h2vUMVHwUIw9uKXQuaayfe6pG7MrGybirXHOzS0XyNCl+iB1lOgqcD9iG4tUe23XhmKYuaYGr5o82fB/0lfD47juhUgTdRewHpoyoPQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763776690; c=relaxed/simple; bh=qfEnrOiB6r3j6FK/VY9BO68j2aHsvbNRzWIwbH8dgHM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=oIlVjzsiybLJPH39+NEvk6AHHXw6xMbaXqH1bqcQRIAhwa0LKuYeAYL02HJQens8SuoADciY5R0iBXP/PJ2lO+pLaymfv4jqGzm76cf8Aw3SstW5d2e1LU6ZVJjxaqK2gSVWnPy3QPSjVdboHexCzxikN+x0DMtf7yGURRD6/MA= 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=nI34Tqv5; arc=fail smtp.client-ip=40.107.201.14 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="nI34Tqv5" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=cpojL8RrOd7/f3eILu95iTF4T3Tuxxgov5O2HDh1A37SKUUK1aZlC3XAOIXXbi2W35zCqpk5L0d/c9sV6cK5gQDiCxq0Pm/3uw/jfzOVwcVSldC8WQFPsz+7YyAx7PFVE5JgmQiYqwk08dkdWHg670shUb/o+oUkyQkW/bDo+KCe3XNvlNEG3fygLCPBM2FRcCIPJ0J7TsxX280470kcwNF+Rqe6WlN4WxV8gQSv1B5HcbL3BN0zE41lPjKvvUk8tXsb+BJKdjdTyhrOSpkBv+6bAoCTt70BfUXgZhy2B9R/oTW/6LDv2sbt6FMLkjXY+m+vUkV8tAfHDsTSxwlmgA== 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=DJyjdT9VsQxFprMzMoOoel6GSBU3MHSRF2/5TFDvkJ0=; b=rcQ/k0pRIUDt1NkQz/HR+Jqsc37rhKaD55U4BWXcvTSbap4t3FQ6R5b2nxnAAMzmn//KUWf8PcdhffRblFFzu2UfeWUsZ5MQoSTOLi6igzsIVRU3XDIxkDHvNslvc9ctY1vJrRlphmfMUMJIZNRX4HhD+mMdLDNkMqINqzsQnq93pVgfl6A7EH7aQ1OSfKEPitzRINT+0OD4iG0CcdJCZwX9k9lJ86ZfFFRUt2WvU+AKDA2jbhLos3q+76ig7X5oxUNuM5YE6+ihZ9k7k9pEXfQi41tCY1o063+AzWFipSXv5gYzESfLuCaKEKjpLc3IWy42x/Vb9ZmqDiMjkGoW9w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) 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=DJyjdT9VsQxFprMzMoOoel6GSBU3MHSRF2/5TFDvkJ0=; b=nI34Tqv51JyBqIoHD4N5s98YkPyTCxyw/2PQtxjVJLf4WLi8dP/AFWfYmZyXHgPAIeOG0fVcLhRm0mFMwXHZ11cczAJx6qgBFwxJAHXE6UadIBtzJJeqwVBYnE6c6tAOWk0MkXCse5ObnCOFeJsPRT3lClXrbyRmCwbNJ4Pfil19IayRGTL0/mmHRmzt+6JLyu++/jnMwJw0839wz62I6uXzzO96w7a0pYuJ3lhOEorqmyE2QPTvKg9YAb9OSVQrjuM6fqJ5/gYgeIXnikAjuwpBNqxLJgOnw9ofAr8dHDBRDqMeZlYUFXqobhl8ub2DMNmoy6EwsjbwyEytrJgIeg== Received: from BN0PR04CA0091.namprd04.prod.outlook.com (2603:10b6:408:ec::6) by PH7PR12MB6609.namprd12.prod.outlook.com (2603:10b6:510:213::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9343.11; Sat, 22 Nov 2025 01:58:02 +0000 Received: from BN1PEPF00005FFD.namprd05.prod.outlook.com (2603:10b6:408:ec:cafe::ae) by BN0PR04CA0091.outlook.office365.com (2603:10b6:408:ec::6) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9343.14 via Frontend Transport; Sat, 22 Nov 2025 01:58:01 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) 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.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by BN1PEPF00005FFD.mail.protection.outlook.com (10.167.243.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9343.9 via Frontend Transport; Sat, 22 Nov 2025 01:58:01 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Fri, 21 Nov 2025 17:57:50 -0800 Received: from drhqmail202.nvidia.com (10.126.190.181) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Fri, 21 Nov 2025 17:57:50 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.126.190.181) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Fri, 21 Nov 2025 17:57:49 -0800 From: Nicolin Chen To: , , , , CC: , , , , , , , , , , , , , , , Subject: [PATCH v7 5/5] PCI: Suspend iommu function prior to resetting a device Date: Fri, 21 Nov 2025 17:57:32 -0800 Message-ID: <4d2444cc52cf3885bfd5c8d86d5eeea8a5f67df8.1763775108.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: BN1PEPF00005FFD:EE_|PH7PR12MB6609:EE_ X-MS-Office365-Filtering-Correlation-Id: 3f2f76b7-61ed-4db0-941e-08de296a9147 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|82310400026|1800799024|36860700013; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?eERL8pFHXoxkPmJLmyrdTyxpeQHh4GAo2MVdWWbWAwvhCCe5ocOvNkF1xeWI?= =?us-ascii?Q?+fOqXKa/FKqN0ZHPNCS+5eWJLppPMe2rP9txPdBsnConZhoe71c04TNubaFl?= =?us-ascii?Q?bxzaK39Sf7wjun9sLfz7dRKxHn1mC70Uk4CvImAAt0YYVnasHJQtbMK6iHS1?= =?us-ascii?Q?HMHvs/eT9erP2FnysaPWK3zO53vLyFGTWN0Y+OgbJdDyIqNpSkqNP1dKa/Tc?= =?us-ascii?Q?+8hrwjsOEocvJDeAf47RpqUsUPVmFYIKBzU1P0UYBfN/HmwolG6Xa4iIc6Ek?= =?us-ascii?Q?US72enk05AWcjo9f1K04KcmYVjvk+5VRPupK1qupx9YPWK7Knrd8/N+8BQ+d?= =?us-ascii?Q?9Gi4cAPVeKuA6+G74ZqtmtAs2CsNJ5N5Pv31dB2uAI/VPN039mGVQCCtxAwH?= =?us-ascii?Q?v0S6vfyE5FnlIPY+PVyq3ivZqQPtUKp8Jc3gZV9Lcwjt/+n/UzAF594rYWdE?= =?us-ascii?Q?H0iJjFyaikVXoLL5jIqJQ/8o7bi/hu6fPR0w/QYZHivlknBH2dnw0d9BWVdz?= =?us-ascii?Q?bqnYhyc8nvJ1LvFROFArAlMqqKf2lHpvZ9vjVC4RLWy/CDJhHlM5JUJqYx32?= =?us-ascii?Q?K+z2wDoiTWgdwc/VQjmVUanBAC6g00NsfvcMta/0lK6v9DFJ1avWneCxZur4?= =?us-ascii?Q?Pd00SSbNFV6QZ/+Af1nwOo30OnU0vgUg+KvCd1Dd0RteiTMUcnetMoyHAu25?= =?us-ascii?Q?2ZYDkXHQsk7uk7cowYdSliqPEUAOdazovLKOuu+mrHPIVBVJHZC3CBgkDlox?= =?us-ascii?Q?NMSqvASQcEqz3QF+laKgJGVtU0ZGtIGEapVbToJPo3n3F4qQ7tT30BnZCyvB?= =?us-ascii?Q?0+GOQDT/LEr0Xf1KVuclgMh+8mFTIAGGxn2rWhD47GlUDP1vsTQdTpTpIYzw?= =?us-ascii?Q?tUbQKuaMeYKDfruenmp8NDZAFC/eTub14JXLqvr8sodE0ugqJfkTV2cUuzjy?= =?us-ascii?Q?SQGODGLbtXuVvtdZStmLHMzLEnVzDCJUaQyENsnc5YRa8hR0U5EpDrPJCBOD?= =?us-ascii?Q?BLJdG5m/pHFO4ShDGEI/XtHIyUyfcCDN/M+/+QvxesHdzFzq0IPDz1PXrupp?= =?us-ascii?Q?v86IoM3j0uAN9dvKF8BLktcG2DP0OTERp/tgLkghPU9HEeACNWRVyjiNGzxZ?= =?us-ascii?Q?86pbba/+bkYg1fLxPClDPgZP+kbAWmzOs3C+f8vOdJjmepV8F2Q/2LCS2ZlG?= =?us-ascii?Q?909+zYznUSLYL7y1FwddVMP0jOe1nCxeBNJZMx2HQ0nESP1LRdiw7XMbAYG9?= =?us-ascii?Q?APOu6h/S3byhcUHiCibTjdM+8V85/IqPvkUt5CWtPzWZ+1QLYSVVCgtKNhhz?= =?us-ascii?Q?TWYbYMY0pVQ5b4KdsECO1yOw1UD3VJOgPZheRf5uC3RY4w7k4XbhKU4k/jIm?= =?us-ascii?Q?Y5ROeFM3LDq+GgQIsfZ+2I7HLBx7D5X++sUQEGqpBtXKUO7fcNuONbSceo4E?= =?us-ascii?Q?nsYKeLMePq6+6TI7v0+R2QJ1Y6PLoRN+J304NDXyMHSU0yurDw0R4Mx/mrbY?= =?us-ascii?Q?u4NSIfTsIJsHwQyKuGRu/s/Dm+DnfibG86mp7SFRPKej3H82J/+dlIhUc4xo?= =?us-ascii?Q?UJ/abEZ5SQq444ldDlU=3D?= X-Forefront-Antispam-Report: CIP:216.228.118.233;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge2.nvidia.com;CAT:NONE;SFS:(13230040)(376014)(7416014)(82310400026)(1800799024)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Nov 2025 01:58:01.1263 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3f2f76b7-61ed-4db0-941e-08de296a9147 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.233];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN1PEPF00005FFD.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6609 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 r6.0, sec 10.3.1 IMPLEMENTATION NOTE recommends SW 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. The IOMMU subsystem provides pci_dev_reset_iommu_prepare/done() callback helpers for this matter. Use them in all the existing reset functions. This will attach the device to its iommu_group->blocking_domain during the device 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 Reviewed-by: Kevin Tian Signed-off-by: Nicolin Chen Acked-by: Bjorn Helgaas Reviewed-by: Jason Gunthorpe Tested-by: Dheeraj Kumar Srivastava --- drivers/pci/pci-acpi.c | 13 +++++++-- drivers/pci/pci.c | 65 +++++++++++++++++++++++++++++++++++++----- drivers/pci/quirks.c | 19 +++++++++++- 3 files changed, 87 insertions(+), 10 deletions(-) diff --git a/drivers/pci/pci-acpi.c b/drivers/pci/pci-acpi.c index 9369377725fa0..651d9b5561fff 100644 --- a/drivers/pci/pci-acpi.c +++ b/drivers/pci/pci-acpi.c @@ -9,6 +9,7 @@ =20 #include #include +#include #include #include #include @@ -971,6 +972,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; =20 if (!handle || !acpi_has_method(handle, "_RST")) return -ENOTTY; @@ -978,12 +980,19 @@ int pci_dev_acpi_reset(struct pci_dev *dev, bool prob= e) if (probe) return 0; =20 + ret =3D pci_dev_reset_iommu_prepare(dev); + if (ret) { + pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", ret); + 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_dev_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 b14dd064006cc..da0cf0f041516 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 @@ -4478,13 +4480,22 @@ EXPORT_SYMBOL(pci_wait_for_pending_transaction); */ int pcie_flr(struct pci_dev *dev) { + int ret; + 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_dev_reset_iommu_prepare(dev); + if (ret) { + pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", ret); + 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 @@ -4493,7 +4504,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_dev_reset_iommu_done(dev); + return ret; } EXPORT_SYMBOL_GPL(pcie_flr); =20 @@ -4521,6 +4535,7 @@ EXPORT_SYMBOL_GPL(pcie_reset_flr); =20 static int pci_af_flr(struct pci_dev *dev, bool probe) { + int ret; int pos; u8 cap; =20 @@ -4547,10 +4562,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_dev_reset_iommu_prepare(dev); + if (ret) { + pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", ret); + 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, @@ -4560,7 +4582,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_dev_reset_iommu_done(dev); + return ret; } =20 /** @@ -4581,6 +4606,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; @@ -4595,6 +4621,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_dev_reset_iommu_prepare(dev); + if (ret) { + pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", ret); + 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); @@ -4605,7 +4637,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_dev_reset_iommu_done(dev); + return ret; } =20 /** @@ -5033,10 +5067,20 @@ static int pci_reset_bus_function(struct pci_dev *d= ev, bool probe) return -ENOTTY; } =20 + rc =3D pci_dev_reset_iommu_prepare(dev); + if (rc) { + pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", rc); + return rc; + } + rc =3D pci_dev_reset_slot_function(dev, probe); if (rc !=3D -ENOTTY) - return rc; - return pci_parent_bus_reset(dev, probe); + goto done; + + rc =3D pci_parent_bus_reset(dev, probe); +done: + pci_dev_reset_iommu_done(dev); + return rc; } =20 static int cxl_reset_bus_function(struct pci_dev *dev, bool probe) @@ -5060,6 +5104,12 @@ static int cxl_reset_bus_function(struct pci_dev *de= v, bool probe) if (rc) return -ENOTTY; =20 + rc =3D pci_dev_reset_iommu_prepare(dev); + if (rc) { + pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", rc); + return rc; + } + if (reg & PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR) { val =3D reg; } else { @@ -5074,6 +5124,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_dev_reset_iommu_done(dev); return rc; } =20 diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c index b9c252aa6fe08..c6b999045c70a 100644 --- a/drivers/pci/quirks.c +++ b/drivers/pci/quirks.c @@ -21,6 +21,7 @@ #include #include /* isa_dma_bridge_buggy */ #include +#include #include #include #include @@ -4228,6 +4229,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_dev_reset_iommu_prepare(dev); + if (ret) { + pci_err(dev, "failed to stop IOMMU for a PCI reset: %d\n", ret); + return ret; + } + + ret =3D i->reset(dev, probe); + pci_dev_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 @@ -4242,7 +4259,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