From nobody Mon Nov 25 02:28:31 2024 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2088.outbound.protection.outlook.com [40.107.244.88]) (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 E864E1993AD for ; Thu, 31 Oct 2024 09:17:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.244.88 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730366241; cv=fail; b=R7S8clsbS4bVI5nxWNDYEHqKZ6ThWdBXnxqYNfSsAxao5MwqZQx2urlavFt+afS/ZpCZo7ut8Op889O54/O9rX7OtvTxasXDoqR0S/cSDrA5rpM6E6A0zDzDNEsHqoF4yyN0gr1G300/ErAkxjmRnswFlNduhJp/RvkiXytnb2o= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730366241; c=relaxed/simple; bh=UyEDKP88toYK/nbO11G/dr2DYFUAt9gaM+62LeGfsS0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=XDXfaAoO/hfuqxKp6qEddXWIWSiooqIJYfodl7waR0sZ4PvxRcpruo5e4acvUq5yNGavpSh7Enl1VO+2BkQ0ob+ULYhz+pIe3NE2G5DrFQNvyZnb3xZ26pRLp2mCmP62VB4P2SNgtTmb4FjsqmlLjGuhUkCgMziBQch++lE//Yo= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=OE6T9J5K; arc=fail smtp.client-ip=40.107.244.88 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) header.d=amd.com header.i=@amd.com header.b="OE6T9J5K" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=HYuug2+plUWvmba9aXEZ8Q0reQuenRS9h2COl82TyoXpbu3dlwpWUcS3g5L+YCFhAj1HtpdbUTfmfievazKoi7ZTaW34EWZfrLLUc0f9ACTHq4RcATMtHm2wCqqBSKutOAUEBknbcauhcGO+ban9+fSG0VgMrEPp7Vv6p2tIKvFrkQogmAqG9xltJqjhkTcYMvVKUQTK2QZYp1sr1b6eIPx7pUl2vSjWu/hxCdLCciCKAu9sSdMkWmskTsA8qomlr4Zzyllbz+StoAliHxuHyL9TW/gu8Ek72Qg5URY8trmnZ/KHjNFbe41LTHNX1pEwxtSBlTkcAB7eKAzN9DoFOA== 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=yg6FO3HYh8ssK5OECx8B2mzekZ17jc2imAZ8nPh3Zxs=; b=RkpfS3ngk4qALjawwCojqQOW1vGBjlQCI21ebb+ce+/nnSlBgkAqsl+6gryMFGRiCWwfDZqILhqMENXEFtXmGhxobI7MehBSeApnx7RQEC7aFmAcAT8K077DX+FH/KnkmR4wI6ndEEERq5N1na6C7x4PlXZGzPLQwxHCu55HSi7CzFJ96rvUK/jvMikSXh0oHbX86co0r06sJrLgxVNvR85eyYxzOsFAp+1ES5Ll7v3pud5yhZ27F7yPmSKxSNnahOQlpU7Oev8yUhoqMlJY0Z15wlxtUIAX4WEbFyDJD3Ho5VAyz4KMAYQ1hUuQP1gYSTjw4YMyvv1vjHxtZjGd+A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.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=yg6FO3HYh8ssK5OECx8B2mzekZ17jc2imAZ8nPh3Zxs=; b=OE6T9J5KA6axCvNi6hUmalvbFFAeE9Jpdp4wvfSd3iLY/svUhDA+QM+foQDwBd1XEAyGMytmnn5YPw4VkP6zE4UN1iz7pxJs5l6z3Y5EikaamYv598fr47lD8y2fR2mEtq2+W3v2ieD3PKo12W5V7ia+sKX6yc2TwLkfFDWTaj8= Received: from MW4PR04CA0069.namprd04.prod.outlook.com (2603:10b6:303:6b::14) by DM6PR12MB4057.namprd12.prod.outlook.com (2603:10b6:5:213::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8114.20; Thu, 31 Oct 2024 09:17:15 +0000 Received: from CO1PEPF000042AE.namprd03.prod.outlook.com (2603:10b6:303:6b:cafe::20) by MW4PR04CA0069.outlook.office365.com (2603:10b6:303:6b::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8114.20 via Frontend Transport; Thu, 31 Oct 2024 09:17:15 +0000 X-MS-Exchange-Authentication-Results: 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=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CO1PEPF000042AE.mail.protection.outlook.com (10.167.243.43) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8114.16 via Frontend Transport; Thu, 31 Oct 2024 09:17:15 +0000 Received: from purico-ed03host.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Thu, 31 Oct 2024 04:17:11 -0500 From: Suravee Suthikulpanit To: , CC: , , , , , , , , , Suravee Suthikulpanit Subject: [PATCH v7 09/10] iommu/amd: Lock DTE before updating the entry with WRITE_ONCE() Date: Thu, 31 Oct 2024 09:16:23 +0000 Message-ID: <20241031091624.4895-10-suravee.suthikulpanit@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241031091624.4895-1-suravee.suthikulpanit@amd.com> References: <20241031091624.4895-1-suravee.suthikulpanit@amd.com> 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-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1PEPF000042AE:EE_|DM6PR12MB4057:EE_ X-MS-Office365-Filtering-Correlation-Id: ce7ffd96-ee07-496c-f286-08dcf98ccf80 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|36860700013|376014|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?zrphaez3iUSyJgpru3Opeibm8Zueht8opbreMEMia32blQ7PxC3rBs/Hmaj7?= =?us-ascii?Q?24M3Di+5Jgj/fZvg+iATgjjHYZ0+EFmW2uGiWl7QwhBVXrHAitdlQDRGsyvl?= =?us-ascii?Q?B7KCUH6LgkQBw3fOh/P0tR1Fgv2dFUWKKC1yUeYXmIy3T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esvHoPgdMZbVCYdW2SAxQLGIFX0pOp3rotVfMqiSp?= =?us-ascii?Q?3BKk64QiNQRH5ob2J5GxzmvwIGiZlko/yU3zOigYjzpjpRQ2R81ue83m8MFm?= =?us-ascii?Q?kw7J6N00JOFP61AYpPJxlB3BEn2IzCj2wuU6tBJpFAlp4HU8u3HARXOxDVVz?= =?us-ascii?Q?AImxZOP71SRbU1WKTMXi14qyR6E0W7fi06UDoZ+xukIK6GVrcQ3dDb+bzv6f?= =?us-ascii?Q?Ed4D1ij8jJSffIi9OhVYOyWV6A0eVq3Fgi5uae1InE7wybAzGMdrrW2+E4vd?= =?us-ascii?Q?V95DTjzP5ZO3W9o+B4FoGLUTnHpYGgspPjizKegQXc2aESGVoHa5iX392okW?= =?us-ascii?Q?xpKZVwVUptOPhNjzfYaM008waKJHDiIf2BlizvsZS5GiZVwZAg=3D=3D?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(36860700013)(376014)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Oct 2024 09:17:15.0121 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ce7ffd96-ee07-496c-f286-08dcf98ccf80 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=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CO1PEPF000042AE.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4057 Content-Type: text/plain; charset="utf-8" When updating only within a 64-bit tuple of a DTE, just lock the DTE and use WRITE_ONCE() because it is writing to memory read back by HW. Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe Signed-off-by: Suravee Suthikulpanit --- drivers/iommu/amd/amd_iommu.h | 1 + drivers/iommu/amd/iommu.c | 43 +++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h index 35d1e40930a5..bfc61f7ed923 100644 --- a/drivers/iommu/amd/amd_iommu.h +++ b/drivers/iommu/amd/amd_iommu.h @@ -179,3 +179,4 @@ struct dev_table_entry *get_dev_table(struct amd_iommu = *iommu); #endif =20 struct dev_table_entry *amd_iommu_get_ivhd_dte_flags(u16 devid); +struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16 devid); diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index b71fb22d001a..9459695bdc12 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -333,7 +333,7 @@ static struct iommu_dev_data *alloc_dev_data(struct amd= _iommu *iommu, u16 devid) return dev_data; } =20 -static struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16= devid) +struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16 devid) { struct iommu_dev_data *dev_data; struct llist_node *node; @@ -2791,12 +2791,12 @@ static int amd_iommu_set_dirty_tracking(struct iomm= u_domain *domain, bool enable) { struct protection_domain *pdomain =3D to_pdomain(domain); - struct dev_table_entry *dev_table; + struct dev_table_entry *dte; struct iommu_dev_data *dev_data; bool domain_flush =3D false; struct amd_iommu *iommu; unsigned long flags; - u64 pte_root; + u64 new; =20 spin_lock_irqsave(&pdomain->lock, flags); if (!(pdomain->dirty_tracking ^ enable)) { @@ -2805,16 +2805,15 @@ static int amd_iommu_set_dirty_tracking(struct iomm= u_domain *domain, } =20 list_for_each_entry(dev_data, &pdomain->dev_list, list) { + spin_lock(&dev_data->dte_lock); iommu =3D get_amd_iommu_from_dev_data(dev_data); - - dev_table =3D get_dev_table(iommu); - pte_root =3D dev_table[dev_data->devid].data[0]; - - pte_root =3D (enable ? pte_root | DTE_FLAG_HAD : - pte_root & ~DTE_FLAG_HAD); + dte =3D &get_dev_table(iommu)[dev_data->devid]; + new =3D READ_ONCE(dte->data[0]); + new =3D (enable ? new | DTE_FLAG_HAD : new & ~DTE_FLAG_HAD); + WRITE_ONCE(dte->data[0], new); + spin_unlock(&dev_data->dte_lock); =20 /* Flush device DTE */ - dev_table[dev_data->devid].data[0] =3D pte_root; device_flush_dte(dev_data); domain_flush =3D true; } @@ -3079,17 +3078,23 @@ static void iommu_flush_irt_and_complete(struct amd= _iommu *iommu, u16 devid) static void set_dte_irq_entry(struct amd_iommu *iommu, u16 devid, struct irq_remap_table *table) { - u64 dte; - struct dev_table_entry *dev_table =3D get_dev_table(iommu); + u64 new; + struct dev_table_entry *dte =3D &get_dev_table(iommu)[devid]; + struct iommu_dev_data *dev_data =3D search_dev_data(iommu, devid); + + if (dev_data) + spin_lock(&dev_data->dte_lock); =20 - dte =3D dev_table[devid].data[2]; - dte &=3D ~DTE_IRQ_PHYS_ADDR_MASK; - dte |=3D iommu_virt_to_phys(table->table); - dte |=3D DTE_IRQ_REMAP_INTCTL; - dte |=3D DTE_INTTABLEN; - dte |=3D DTE_IRQ_REMAP_ENABLE; + new =3D READ_ONCE(dte->data[2]); + new &=3D ~DTE_IRQ_PHYS_ADDR_MASK; + new |=3D iommu_virt_to_phys(table->table); + new |=3D DTE_IRQ_REMAP_INTCTL; + new |=3D DTE_INTTABLEN; + new |=3D DTE_IRQ_REMAP_ENABLE; + WRITE_ONCE(dte->data[2], new); =20 - dev_table[devid].data[2] =3D dte; + if (dev_data) + spin_unlock(&dev_data->dte_lock); } =20 static struct irq_remap_table *get_irq_table(struct amd_iommu *iommu, u16 = devid) --=20 2.34.1