Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.53.19 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796378402; b=pBW6ZFCXKSh41kNJ1QjtW20QpNtlbAGOfTv6PwYzabSL9GIaGlo7esajXQWR8ifCUda+ 9N6f5GrycgCngO/eIi+uVQTT6VbtIoMJBoZPIxleC6XQjQA73e60Qoz9BKg/nfYEze9T/ orucBiCrWROUtoaVRch6HOvbdcCdXFZYId+VCkoRyrm5NC6mZVy+eEbewVvT6C79An+Bp NioK2ISxnq5jNqCB5QD91YoaGDkTo1A5mUykWs4m5IUDkWJVxws0NC4+4SuPUhXYyNctT ujVViXuLDD/sGsyZHkhO4wX9LghN02CMCYNpQxFOzGxdrW9eCnJMMWBTavpv7x97Xmg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796378402; 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=pAuo12aAAkXS2d7OdpMohV9Sh4yhkjyyyzg8dPGhyQI=; b=wZO6Wnv1dfk6Hm74u9iZlRxKhN7/cLd9vd9MHo4YEcYWbs+hqQKWDul4wJzGC5umUY11 8S5x8om5MKB/zSaxXyIf5yFHwtGqxuhaE4NNKmYOqY5t46NWmLFjMltrCouFp8FtfQ9un /fFA0RlPMKOQ7Sf+PkdXOQKokOIOG56YQLAFuelNL9nKxasK64aQmSmuj5XPGlCkMPb0T sgqcuEVtB45xwRh1YTTJo5/0d6rxrfslFsVfXwLxx24jTSXHEJhMiLCdM1Yt5GA1N664c mboBFA2W+RICPjijooyjgz408eMWXbfwEXRCSL6t+5X2BAaqo1wL7DOKHpMeSx4kt5Q== 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=rHsV3x5T; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.53.19 Received: from BL0PR03CU003.outbound.protection.outlook.com (mail-eastusazon11012019.outbound.protection.outlook.com [52.101.53.19]) (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 41D3D606A322 for ; Fri, 4 Dec 2026 10:00:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 41D3D606A322 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=rHsV3x5T ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=kpG+qYJVD2vK9WW/mDw/XGql1ipIR4tMoQHMqVUkGWuEvHcgOF0QzbA3u6k2YwRlZEs1d9W6c4DVSHtnglxC8chyWl5LSOiXx5iS9HLrYJnyBhl9buzE/6dIKx3JYBHb45mXCW4BlAoHtqUYyh1yiEvbn77muP34Tnnm+FFiJQd6axEACcKDeGx61eUfHXTSzEEWyv1Cg5J7aCS4e16uJ5AVNjumHT2lpw3Bmt0RRtO1+j4mQhN6yjub6MLF144j4YV0XENBR6pHCIFBmvHixtxBMkkaCQC7OLl3vhhpOPadT1SFflkbcFZ+c0JviJ06C5Ys1ipkZ1tAogfu4VRAig== 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=pAuo12aAAkXS2d7OdpMohV9Sh4yhkjyyyzg8dPGhyQI=; b=XQqhsEeA1IGoAIQw312b0xGDsvSGMw+6fCruAjqFwOxhPDzIwo0xxbWI1yVROIgv4ripITcs1jp0L55HFiJ7Jsf9tyZkaIt43dOJ6oYq/onpPA5fhGnpb7e/yo94DPZ1R10evCNFwS8NmQLEZ6lgN7/ltXWgrviliMklKMKxwKl6NNRzb5GW158RFABJGZrH0Ui7fEtK4tZI8eYAK4kOOw3WuT1CC85QyC8m973Bdh0eqHVlxY49GURmNq/OBLJXofdvQCNFkBXUqbUJKnkl/42loo4MazDu3f9SFoOKKJFk1y+xpOKCZkp9qiO7ouPehisoxKb+JqwNu7+2718Rnw== 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=pAuo12aAAkXS2d7OdpMohV9Sh4yhkjyyyzg8dPGhyQI=; b=rHsV3x5TWgsxCr3ZMF4Hd8/HrUoXap1tQxG9JChZp4K0+3khgTlgncy4ypLRYWKxIghVR6QTfSC8zUZQK3zrRWBDTNGaLUlTi4dnImQG/bllVKWLjFOSWksCo2Sd+Dglkm0187fGJzCTuAfFBT23tEZvxg4N8mlSsQr9DRRWSo8= Received: from CH0PR04CA0089.namprd04.prod.outlook.com (2603:10b6:610:74::34) by LV0PR12MB668237.namprd12.prod.outlook.com (2603:10b6:408:3f9::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.12; Fri, 4 Dec 2026 09:59:56 +0000 Received: from CH2PEPF00016D4A.namprd02.prod.outlook.com (2603:10b6:610:74:cafe::90) by CH0PR04CA0089.outlook.office365.com (2603:10b6:610:74::34) 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 09:59:56 +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 CH2PEPF00016D4A.mail.protection.outlook.com (10.167.244.119) 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 09:59:56 +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 03:59:55 -0600 From: Vasant Hegde To: CC: Subject: [PATCH 13/38] iommu/amd: Serialize device-table writes with one per-segment lock Date: Fri, 4 Dec 2026 09:56:11 +0000 Message-ID: <20261204095636.335222-14-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: CH2PEPF00016D4A:EE_|LV0PR12MB668237:EE_ X-MS-Office365-Filtering-Correlation-Id: e7d8c836-d5f2-478e-bd61-08df51ed59f4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|376014|36860700016|1800799024|23010399003|11063799006|261009223027099003|22082099003|18002099003|10067099003|261028144727099003|261021121427099003|261102123227099003|261026121527099003|261026140227099003|261022105027099003|261021123027099003|56012099006; X-Microsoft-Antispam-Message-Info: joq74YAN6X/t4s5R6KSv2X5794pcOHgcgyvu60JW9N0oFHWdVLILwz0WamneGkApjfGs3mwQYJhNad60oInsQp8975KhuOi1juqXxTozanBQZzmKIhJR+3FdFk6NtLYOnuasYq2xlultTqpy+pXSqUmKoWFQc6SLO3peMLlQrYNb39jsAFy3D/MY80devc4NZt3m5GGa4tSLhzJYNIVbbAwSmEfuSgFCGgSUn4zTxlZffY3Oj3GAud+LlVL/cCEnoUV08Kh/5hpJXcCgRlUP9axXdjTTga1qszqN+r1doVeDfnvfMw6RlqNuSJ9+b94mnQ2FrRg0aTw441wqNgk8Bwf7zAQBZs54KnIlsRfXa+ftXw6IUJBYE5kFQ8/wcSotxdEgFUoTup193kVuys1XvnNtQ0KeSq/pYBlNkcOcyLUljS16rStUIxUf9gr7DqXWRMwkDgp3fjk0FC8l786uUsQQo7NzQ+aqMrx5XH0puUzCeiNn/jKfw2SB4Jh0A++YV2gXO1z523iDkhF93T2MrfT3PMrLy1MEkkiuwehtCwSTBXSIw9ObWs//LYevWieFnYySZ4RGpfgYCet7pk9r6TYCbLFPD/f+E6hadKw1HsMDKqcFmZ7NvwNxZO28YD/lGVdQoxMf62JXol3qgFnECyo0SOwlmHk/mJbKOAo7iK9j38uIQbhce2GsxeUxKClvHwgyODgDKYL0OEzJpwgD3w== 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)(376014)(36860700016)(1800799024)(23010399003)(11063799006)(261009223027099003)(22082099003)(18002099003)(10067099003)(261028144727099003)(261021121427099003)(261102123227099003)(261026121527099003)(261026140227099003)(261022105027099003)(261021123027099003)(56012099006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: TTl88ayLA+fZ8B4zJLZ6KD89lNUOCKpj8fgE1KTLN0jeuofOoULHouUy2dvxknT9YC/x2J3255hVo3mTzsdotO7gBbbxpQYs4NSv0T1DWBlpK9o39xr8fMNPz74nclWAPl0v9CSbZf7Kpg2sxxF3e8d1tLWYC3d70+bciwvx3I5gKqKRqUB+N+6GbB4nPMYp8OukBvxhV6V39bUFrwuixngvq2BqdLqnUHgN3uIa8tTjlwGTvSJL8ofr6taHF6SglCpi2WUtNtJ5KvLUcJ6Ji2VyE+3gg6jEqRuy86fCTWSfFUGtvnJcnpPHBbDaSkT1sfy88JVhz9Xl07aL86p+pCLGGmekTpGox3JI+v+XPrtsQREVItJ5zJ1OeeyBwu+noq6vuY6RC95aopX+0n44PmUkgFM0Hh6RrcsPepursa4opsBHWBguSKTqOrbpjRPz X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2026 09:59:56.7638 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e7d8c836-d5f2-478e-bd61-08df51ed59f4 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: CH2PEPF00016D4A.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV0PR12MB668237 The DTE update lock (dte_lock) currently lives in iommu_dev_data structure. Because of this, dte[devid] can only be serialized when a corresponding dev_data object exists. This leads to inconsistent locking: - The interrupt remapping path (set_dte_irq_entry()) skips the lock entirely when search_dev_data() returns NULL, leaving the DTE write unprotected. - An alias ID has no device object of its own, so clone_alias() has to create a dev_data for alias devices purely to have a lock to hold before updating the DTE. Move dte_lock to per PCI segment (amd_iommu_pci_seg). This guarantees that every code path holds the lock before updating DTE. A subsequent patch builds on this by removing the need to create dev_data objects solely for alias devices. A DTE update is not a fast-path operation, so using a single per-PCI- segment lock instead of a per-device lock does not affect performance. Signed-off-by: Vasant Hegde --- drivers/iommu/amd/amd_iommu_types.h | 2 +- drivers/iommu/amd/init.c | 1 + drivers/iommu/amd/iommu.c | 22 ++++++++++------------ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_iommu_types.h index 51db65740e41..bbfc561d6abf 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -567,6 +567,7 @@ struct amd_iommu_pci_seg { /* List of all available dev_data structures */ struct llist_head dev_data_list; + spinlock_t dte_lock; /* Device-table update lock */ /* PCI segment number */ u16 id; @@ -807,7 +808,6 @@ struct devid_map { struct iommu_dev_data { /*Protect against attach/detach races */ struct mutex mutex; - spinlock_t dte_lock; /* DTE lock for 256-bit access */ struct list_head list; /* For domain->dev_list */ struct llist_node dev_data_list; /* For global dev_data_list */ diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index 24bf65e35694..e020459513d8 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -1803,6 +1803,7 @@ static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id, pci_seg->id = id; init_llist_head(&pci_seg->dev_data_list); + spin_lock_init(&pci_seg->dte_lock); INIT_LIST_HEAD(&pci_seg->unity_map); list_add_tail(&pci_seg->list, &amd_iommu_pci_seg_list); diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 1840af314131..ce2e36dbc962 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -121,7 +121,7 @@ static void write_dte_upper128(struct dev_table_entry *ptr, struct dev_table_ent /* * Preserve DTE_DATA2_INTR_MASK. This needs to be * done here since it requires to be inside - * spin_lock(&dev_data->dte_lock) context. + * pci_seg->dte_lock context. */ new->data[2] &= ~DTE_DATA2_INTR_MASK; new->data[2] |= old.data[2] & DTE_DATA2_INTR_MASK; @@ -152,7 +152,7 @@ static void update_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_da struct dev_table_entry *dev_table = get_dev_table(iommu); struct dev_table_entry *ptr = &dev_table[dev_data->devid]; - spin_lock_irqsave(&dev_data->dte_lock, flags); + spin_lock_irqsave(&iommu->pci_seg->dte_lock, flags); if (!(ptr->data[0] & DTE_FLAG_V)) { /* Existing DTE is not valid. */ @@ -208,7 +208,7 @@ static void update_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_da write_dte_lower128(ptr, new); } - spin_unlock_irqrestore(&dev_data->dte_lock, flags); + spin_unlock_irqrestore(&iommu->pci_seg->dte_lock, flags); } void amd_iommu_update_dte(struct amd_iommu *iommu, @@ -230,10 +230,10 @@ static void get_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_data, ptr = &dev_table[dev_data->devid]; - spin_lock_irqsave(&dev_data->dte_lock, flags); + spin_lock_irqsave(&iommu->pci_seg->dte_lock, flags); dte->data128[0] = ptr->data128[0]; dte->data128[1] = ptr->data128[1]; - spin_unlock_irqrestore(&dev_data->dte_lock, flags); + spin_unlock_irqrestore(&iommu->pci_seg->dte_lock, flags); } static inline bool pdom_is_v2_pgtbl_mode(struct protection_domain *pdom) @@ -383,7 +383,6 @@ static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 devid) return NULL; mutex_init(&dev_data->mutex); - spin_lock_init(&dev_data->dte_lock); dev_data->devid = devid; ratelimit_default_init(&dev_data->rs); @@ -3121,13 +3120,13 @@ static int amd_iommu_set_dirty_tracking(struct iommu_domain *domain, } list_for_each_entry(dev_data, &pdomain->dev_list, list) { - spin_lock(&dev_data->dte_lock); iommu = get_amd_iommu_from_dev_data(dev_data); + spin_lock(&iommu->pci_seg->dte_lock); dte = &get_dev_table(iommu)[dev_data->devid]; new = dte->data[0]; new = (enable ? new | DTE_FLAG_HAD : new & ~DTE_FLAG_HAD); dte->data[0] = new; - spin_unlock(&dev_data->dte_lock); + spin_unlock(&iommu->pci_seg->dte_lock); /* Flush device DTE */ device_flush_dte(dev_data); @@ -3360,11 +3359,11 @@ static void set_dte_irq_entry(struct amd_iommu *iommu, u16 devid, struct irq_remap_table *table) { u64 new; + unsigned long flags; struct dev_table_entry *dte = &get_dev_table(iommu)[devid]; struct iommu_dev_data *dev_data = search_dev_data(iommu, devid); - if (dev_data) - spin_lock(&dev_data->dte_lock); + spin_lock_irqsave(&iommu->pci_seg->dte_lock, flags); new = READ_ONCE(dte->data[2]); new &= ~DTE_IRQ_PHYS_ADDR_MASK; @@ -3374,8 +3373,7 @@ static void set_dte_irq_entry(struct amd_iommu *iommu, u16 devid, new |= DTE_IRQ_REMAP_ENABLE; WRITE_ONCE(dte->data[2], new); - if (dev_data) - spin_unlock(&dev_data->dte_lock); + spin_unlock_irqrestore(&iommu->pci_seg->dte_lock, flags); } static struct irq_remap_table *get_irq_table(struct amd_iommu *iommu, u16 devid) -- 2.31.1