Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=40.107.209.57 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796378810; b=Zx943vhgLXset44OBPUaYXaCxXxPILmon6aj5inxXReKGQzz85hd14zGhoXotCTwcRtB PD1n7QLZRfBDFLIc5j+9NZBwYNhRxQ2Q1WW6jaqwmXZFyAfX+IMvFwtjej7p+hhgbumXO 0B/oRTXiuuRRt+ldcz7bcsKR1LC25Bh7dOAMJJ+L++J6woki51a4EEVibEB112sVeW5iE 9oh1JDMNy7G7JoZhJe3vD5v7idbtCCAcGh01Act1kroHHNTBysWmvRfPg6Z0hvz4mbuVh ipVkV3IaDpSEr/4VBv8Z+O54y86xSP93h54Xui73NeI7pZT/9SeURsN/FtlgBrcDy9g== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796378810; h=DMARC-Filter:DKIM-Signature:Received:Received: X-MS-Exchange-Authentication-Results:Received-SPF:Received:Received: From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Content-Type:X-Originating-IP: X-ClientProxiedBy:X-EOPAttributedMessage:X-MS-PublicTrafficType: X-MS-TrafficTypeDiagnostic:X-MS-Office365-Filtering-Correlation-Id: X-MS-Exchange-SenderADCheck:X-MS-Exchange-AntiSpam-Relay: X-Microsoft-Antispam:X-Microsoft-Antispam-Message-Info: X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-Id: X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=7K8tCTTaq5STMJwkhXPilUeHHp/ySlftttwlnamNBYM=; b=uGjMaKu+fuk6i0RtKe9NCBWUyep5syEqBydltJWAOMBQhOGcEtSrOIYKlROs6b5JM+V7 QEjH4Z5Ie6iabSp/fhgO8efy48G+j7AyXDeIMZJ2Unm7vV3xrUIszFFAByPmNkejUKHE9 57AWb8OOVkoZm+a1TIhNN+W6T983DRfw7LgtCKiW4uoBq1/Q4IcKjSPUz3OwHnumF7B2n 4GLFORTDbNnlxK/QAabJBPh7wym1ZXxPBkZAT1D0NjioBPB4rjjuARm1tptdfnEeGz2+/ FSOfbxGI1oYcfr7opj+qlUY6W0XfoPJPTpmJpBQKbmzc9D0XKKrEDmAIG6wjAy0Bm3w== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=vOfmd1Vq; arc=pass header.oldest-pass=0 smtp.remote-ip=40.107.209.57 Received: from PH8PR06CU001.outbound.protection.outlook.com (mail-westus3azon11012057.outbound.protection.outlook.com [40.107.209.57]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 15467606A322 for ; Fri, 4 Dec 2026 10:06:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 15467606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=vOfmd1Vq ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=HYaO0vLYqVDv7CeV1fplcDQYJIIWxyeVqYPiTwiO0Tk0aZWf5Jeg8HkHV+y/NtIyxfY6L55QiEL0VTAPctwVGyCY2jmeRN+8DDRacUK6FhoKenEmb4g6UY46D+o3kyUCDqcBWZbUCSXuE4gRsiInn8+Ksy7J4TWxiSDiirtXSHxm/it8pNCT1mSmWnKAmH1PjopYFz0CYrfjpLHyN8TW88yZVrB6pwtW2pXQykazICTmX2e5pfQh3tSZRTqJQlFADFscbk+ZyMHi9SiTQ+NKOGBYqOIvjRErs04eOuI2y+xg75wNc4bCEmDSdsGTnGJxuP4beVKSVce/be03M/IP/w== 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=7K8tCTTaq5STMJwkhXPilUeHHp/ySlftttwlnamNBYM=; b=RdOwpHlgELZbUrrBLcp7Atx4Q47FVk0f6KCo7A49XtAFijoMFKE2vOcNh/dTkYwxjCqiq0HN0p5yAFpYS0bkaRW5stWGNCjPashn2fNjDk8q4HbWcRwYKuKRUCgFbYI28sJ8/fuzziuNj21hDYdjFvwyQggKGbyT8dy2HuSQqM/2x5Ql/eXNIoWM8GjFgkI7NDZYUCW+jUsVKoqbYWZkNecLR31/XVen+8ZIBQNo9E/xHES7FBAqyUyNccBl3d/SRwzLceO3pcKtvRVoLGRWwbZRKaaMw+40K72P5+pod7aGmUHPCxj3zuKUcAnfp2kPGKjf0EtztKi/d28DYhmcHA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=8bytes.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=7K8tCTTaq5STMJwkhXPilUeHHp/ySlftttwlnamNBYM=; b=vOfmd1VqPOc94/i90AIHVtCcv04oqC3Juxc6oGSL2AJ5iJipzJO5WOIrycjsGTftRaBIf9f6eKa6189I6hBNFp9elp7p7wjYddR6wqjxQvaye1kEAoNe9f69svbPrrkTNtYBNANlNnRYNAea/ae22LsUC5yOA2WLg+jvzzkpvNo= Received: from MW4PR04CA0113.namprd04.prod.outlook.com (2603:10b6:303:83::28) by CH3PR12MB8933.namprd12.prod.outlook.com (2603:10b6:610:17a::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.10; Fri, 4 Dec 2026 10:06:38 +0000 Received: from SJ5PEPF000001CD.namprd05.prod.outlook.com (2603:10b6:303:83:cafe::3b) by MW4PR04CA0113.outlook.office365.com (2603:10b6:303:83::28) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.688.13 via Frontend Transport; Fri, 4 Dec 2026 10:06:38 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by SJ5PEPF000001CD.mail.protection.outlook.com (10.167.242.42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.709.6 via Frontend Transport; Fri, 4 Dec 2026 10:06:38 +0000 Received: from kali.amd.com (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Fri, 4 Dec 2026 04:06:36 -0600 From: Vasant Hegde To: CC: Subject: [PATCH 37/38] iommu/amd: Don't detach SVA PASIDs from the mm release notifier Date: Fri, 4 Dec 2026 09:56:35 +0000 Message-ID: <20261204095636.335222-38-vasant.hegde@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261204095636.335222-1-vasant.hegde@amd.com> References: <20261204095636.335222-1-vasant.hegde@amd.com> X-Mailing-List: iommu@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ5PEPF000001CD:EE_|CH3PR12MB8933:EE_ X-MS-Office365-Filtering-Correlation-Id: 00fbb35b-26d6-4755-4533-08df51ee493b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|23010399003|376014|36860700016|1800799024|6133799003|22082099003|18002099003|261026140227099003|5023799004|56012099006|10067099003|261009223027099003|261021123027099003|11063799006|261028144727099003|261022105027099003|261021121427099003|261102123227099003|261022075727099003|261026121527099003; X-Microsoft-Antispam-Message-Info: 05IzsM7rkeJBw3IrDaCT3diTXIUxFsujRmOSYo4718VaIljJJEjNAL1UGM1DvkI60AQN7+3jOQLnQBwzvwDckgmVi5fzrnsltFjQngYnN2oP1KjwX9/rovQqQbZeFOalAmVw2sR523uIxRjis/BBjaMuxUGloRvlj2o1hOt/jZD0cSi1j+pkxg8fF7Eo/BpBQq5Y/odhToNigy1/zz5yOVMt7yp2Uzm3RzxKFhTB0Dhlm24kaCHFHqEkXv7d2XvjQfULm339flRZtn5Jr2FiejcGhCSxGTAvMHOPmxf4wTQdhCcSivZkkDJ5xc3hGYAf75V7p8Y5LXEHaQz1pK+8pD+o6pvc4Y+fSiq72s/l+pJ9esqeAEfGvvfxqj2mu8/5A1Sw3hNx59Wap7IWIX1uJXqSK112RxCRNsIyHMwbsHKrwFYfTuaSxzeUPPubQO2tRw1fOPy8LQgigr8hNGZFJ8z/2wdMJNfpHt+n0BO0Mw914D6IPbtZOahrz8SuegHtTWOmVzqn/bSGeSpSlbvZnstKisq1ie0xKHMFbZj2CwxuzdT/hX61dBTo/nZpHvfT371BNR3fPJ8ofqLI4kHC6HjI5NlPmQnScDNOSaR4nLRSRgVYNysoPjHcT4HZJJlmL5NM7BFFrCEJnbAbodYzIVdPQmoyBygVDALDNdM+H88JNGcjaqCQMf/wLb80SjUVxV2F/YPQtYhkkBFK9lHa4A== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(23010399003)(376014)(36860700016)(1800799024)(6133799003)(22082099003)(18002099003)(261026140227099003)(5023799004)(56012099006)(10067099003)(261009223027099003)(261021123027099003)(11063799006)(261028144727099003)(261022105027099003)(261021121427099003)(261102123227099003)(261022075727099003)(261026121527099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: M7S5+E723hTVJ4pIHJuOFSPs1yG0r3KEp5fBHf9yHQQ+wf/62R7sZdLZ3UYils6yU3KukUgfVc0E2I1AYWf1bNbIBfmOaScAJD9UH50sI2H/rd+Ucv7RC3se+1zS8LNhhEEJlyzd3GePGomLKb53R1LYFo2iQEHPqa7YqPQGfHBhn3TgP+YbfIeiDtYRdrF0lIOCrrfmDlaQ9AoNcg1TIjJ5kBP5E8BNgLdHg6nuALaBdugLWYXcuiR6hycadCH7lbumbfEjjV8TLAVifIpxkkvguxHEqxtEyhmjZ0ToIGbsPIiddox4f93q3xfQoe5jWyajHoUgnBQc3IGqJe2lfzU07nk6Te0O/mViaq6kJvJLhDrtpyIj6lA3QkJq4oI/RQ39CDodvEbrVzFq4Bs2EgP08yJYV3CHZ6VTXo1JpicoiEIa3cnmAHmwRA1BhaEp X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2026 10:06:38.1850 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 00fbb35b-26d6-4755-4533-08df51ee493b X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[satlexmb07.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ5PEPF000001CD.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8933 sva_mn_release() runs when the mm page tables are about to be freed. It calls remove_dev_pasid(), which invokes amd_iommu_clear_gcr3() without the group mutex and trips its lockdep assertion. It also tears down the PASID attachment and driver metadata behind the IOMMU core's back. Stop removing the attachment from the notifier. The notifier only has to keep the IOMMU from walking page tables that are about to be freed. Leave pasid_cnt and pdom_dev_data intact and let the normal core-driven detach take the group mutex, clear the entry and remove the attachment. Do not clear the GCR3 entry in the notifier either. A cleared entry does not produce a recoverable page request, so a device that still issues PASID traffic would report an IOMMU error until amd_iommu_remove_dev_pasid() runs. Instead, allocate dummy page table root, shared by all IOMMUs. sva_mn_release() path replaces actual GCR3 pointer with dummy page table root. A walk through the root faults instead of following freed memory. Fixes: a5a91e54846d ("iommu/amd: Add SVA domain support") Reported-by: Max Zhen Cc: Liang Wendy Cc: Jason Gunthorpe Signed-off-by: Vasant Hegde --- drivers/iommu/amd/amd_iommu.h | 4 ++++ drivers/iommu/amd/amd_iommu_types.h | 2 -- drivers/iommu/amd/init.c | 11 ++++++++++ drivers/iommu/amd/iommu.c | 32 +++++++++++++++++++++++++++++ drivers/iommu/amd/pasid.c | 13 ++++++++---- 5 files changed, 56 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h index e0cb8cbc6f36..76cb2f99f9c8 100644 --- a/drivers/iommu/amd/amd_iommu.h +++ b/drivers/iommu/amd/amd_iommu.h @@ -77,6 +77,10 @@ void amd_iommu_iopf_remove_device(struct amd_iommu *iommu, int amd_iommu_set_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid, unsigned long gcr3); int amd_iommu_clear_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid); +int amd_iommu_set_gcr3_fault_pgd(struct iommu_dev_data *dev_data, + ioasid_t pasid); +int __init amd_iommu_sva_fault_pgd_alloc(void); +void __init amd_iommu_sva_fault_pgd_free(void); /* PPR */ int __init amd_iommu_alloc_ppr_log(struct amd_iommu *iommu); diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_iommu_types.h index ac34169b5913..26c75b5289e8 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -453,8 +453,6 @@ extern bool amdr_ivrs_remap_support; /* Making iterating over protection_domain->dev_data_list easier */ #define for_each_pdom_dev_data(pdom_dev_data, pdom) \ list_for_each_entry(pdom_dev_data, &pdom->dev_data_list, list) -#define for_each_pdom_dev_data_safe(pdom_dev_data, next, pdom) \ - list_for_each_entry_safe((pdom_dev_data), (next), &pdom->dev_data_list, list) #define for_each_ivhd_dte_flags(entry) \ list_for_each_entry((entry), &amd_ivhd_dev_flags_list, list) diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index 3a4624f5cc24..f8953a2bc1eb 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -2451,6 +2451,16 @@ static int __init amd_iommu_init_pci(void) iommu_set_cwwb_range(iommu); } + /* + * iommu_init_pci() populates amd_iommu_efr from the IOMMU, so check + * PASID support only after it runs. + */ + if (amd_iommu_pasid_supported()) { + ret = amd_iommu_sva_fault_pgd_alloc(); + if (ret) + goto out; + } + /* * Order is important here to make sure any unity map requirements are * fulfilled. The unity mappings are created and written to the device @@ -3281,6 +3291,7 @@ static struct syscore amd_iommu_syscore = { static void __init free_iommu_resources(void) { free_iommu_all(); + amd_iommu_sva_fault_pgd_free(); amd_iommu_pdom_id_destroy(); free_pci_segments(); } diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 7203672f36cb..517225edb257 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -2065,6 +2065,38 @@ int amd_iommu_set_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid, return ret; } +/* Dummy page table root, used in sva_mn_release() path */ +static void *sva_fault_pgd; + +int __init amd_iommu_sva_fault_pgd_alloc(void) +{ + sva_fault_pgd = iommu_alloc_pages_node_sz(NUMA_NO_NODE, GFP_KERNEL, + SZ_4K); + return sva_fault_pgd ? 0 : -ENOMEM; +} + +void __init amd_iommu_sva_fault_pgd_free(void) +{ + if (sva_fault_pgd) + iommu_free_pages(sva_fault_pgd); + + sva_fault_pgd = NULL; +} + +/* + * Point a still-attached PASID at the permanent zero root. + * pasid_cnt stays put; amd_iommu_clear_gcr3() drops the slot on detach. + */ +int amd_iommu_set_gcr3_fault_pgd(struct iommu_dev_data *dev_data, + ioasid_t pasid) +{ + if (WARN_ON_ONCE(!sva_fault_pgd)) + return -ENODEV; + + return update_gcr3(dev_data, pasid, + iommu_virt_to_phys(sva_fault_pgd), true); +} + int amd_iommu_clear_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid) { struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info; diff --git a/drivers/iommu/amd/pasid.c b/drivers/iommu/amd/pasid.c index d0e2bf7921bf..6d126246d850 100644 --- a/drivers/iommu/amd/pasid.c +++ b/drivers/iommu/amd/pasid.c @@ -79,7 +79,7 @@ static void sva_arch_invalidate_secondary_tlbs(struct mmu_notifier *mn, static void sva_mn_release(struct mmu_notifier *mn, struct mm_struct *mm) { - struct pdom_dev_data *pdom_dev_data, *next; + struct pdom_dev_data *pdom_dev_data; struct protection_domain *sva_pdom; unsigned long flags; @@ -87,9 +87,14 @@ static void sva_mn_release(struct mmu_notifier *mn, struct mm_struct *mm) spin_lock_irqsave(&sva_pdom->lock, flags); - /* Assume dev_data_list contains same PASID with different devices */ - for_each_pdom_dev_data_safe(pdom_dev_data, next, sva_pdom) - remove_dev_pasid(pdom_dev_data); + /* + * The mm page tables are freed when this returns. Point each + * still-attached PASID at the permanent zero root so a later + * access faults instead of walking freed memory. + */ + for_each_pdom_dev_data(pdom_dev_data, sva_pdom) + amd_iommu_set_gcr3_fault_pgd(pdom_dev_data->dev_data, + pdom_dev_data->pasid); spin_unlock_irqrestore(&sva_pdom->lock, flags); } -- 2.31.1