From nobody Sun Feb 8 15:25:37 2026 Received: from DM5PR21CU001.outbound.protection.outlook.com (mail-centralusazon11011035.outbound.protection.outlook.com [52.101.62.35]) (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 1464530103F for ; Tue, 27 Jan 2026 03:09:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.62.35 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769483398; cv=fail; b=PdVTRGEEPb3RSuw8whcDyvGZeef07RT8WSs9aXeDfgPW/vS/aFIA+5c7L2DLQioBvU5f6RLMG1COz5pMqKT6Bs2CDMXr65pQ9EunZqy0M+c6QAhl6ugPSp5dvM8jLtIo42cvcAQ7sNw01R9QfozIatLtUaRJqES6EJYRdiW/vKQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769483398; c=relaxed/simple; bh=IkHFUbTnRo/zRIQgiPyQKeoZLCMo2+1mhh7MBzXvQMQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=pX3COksudnvPNFOJmBUsbP1GLM+UZFRZESPEjmIECe6vwuXt7E/BFfiNhR2j0smVEWdzz8i7QaEmg2oMnY5TnsHaRy2Pd19KVEOyyNozkfV5BahDtplDitxPNa/rErCGvO5/U+Eib4ipwgLopD/gE5vS0WruJgQTahkcSJrHfE4= 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=Q6lm3uoB; arc=fail smtp.client-ip=52.101.62.35 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="Q6lm3uoB" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=DnWPMsBbC5ZPx3+c19zNBIyBycRAmcYICRjdSqR7ZYe0LDcDBWg9O0fiVi3c94kSrh9ID7Ngo2259Ifb29OvUt+y2viPcgtwk+Iu7ml6V/TXh2FRh5rvGjW5xeXuKNGcY3Ww2b1D52qRJ1lgCckcd5ZFqMvk3idJdgPnVyP5YS+bRQ00CyYAJGu/B0o/dr+QJAU7z/U+BOyvMT2n20zA3yiX0alH9tPWodPU1yUKVLDRVdHfXwFTu59sI1x8dHxYw+PY8u7Nw5/eDUCL6k5Z4X57D3H4CVqSGrMc3pgxCn0HcnR1fnhWmiAdaqjJqUkaUw95U+VxL+x1/LJMUdByjw== 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=Qz3jKgI7Md+uqar7u2XPjC9ELAnk2UI/e5V+u4cvZuc=; b=Sa5ahsR/7MVaA23NNRkiWPvMWcZh/s2BXFSKiH3myw5Xrexhf1WBTkPQERuwbBzp0jpofKp69MC32xk5D0ID3EYbSu02X1Gp7IDfpjm6MUHLs4b5mz3aef0/T8AWYqv3oraXI7q+GKaJ5WUdworV2NiL4rBh/Z3/FnJLNQVXiWuEEVz9wbXTy3cx9xXjVXWOejP57r7qo1Q+VBbsC8hGFG84VHKYjbf7fTsZZZYbpg2KDqIJQDuKhaJsuIk7J/0I2BUwY0v9jAeAztgn82K6694OgTvhnrGUbhAHosfN59pJPa8LucXHvdrvuCDXQnaeuFLl3b+nPRid4VL9r7ms4w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) smtp.rcpttodomain=kernel.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=Qz3jKgI7Md+uqar7u2XPjC9ELAnk2UI/e5V+u4cvZuc=; b=Q6lm3uoB9PJde8wDZAMdfpCWmNCy3EmhZD0LU1+BNE5rO26r3H+WNkn9fyBv2X11PBxG565pnRUoHqaMXY7qaRzpN0QV5h7hfZS4rrXKuUDztA3wh+YAbph+Tj2IQ3wSSiXWwN6l0aJZjTabYaqL6n0g7vujT+zeYsz3AQ62SuRZF4VPwbK8Ec38epyAd3yZrm3gOD2FrU2SF8UBVS5u2VNHTZbDMbWO9JraxOF0gekNLx7nwTQVRhmjS5IhJfZoF2W9NPMzkWbhpk5JtFq1Qjx+GBzC3MJhVf/Z2oxkfUKU5EX5OZLQLV+BCC1qxWj9ZtxzXlQ9MHQ7zahDWV2eGQ== Received: from CH0PR03CA0414.namprd03.prod.outlook.com (2603:10b6:610:11b::32) by DS7PR12MB5936.namprd12.prod.outlook.com (2603:10b6:8:7f::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9542.15; Tue, 27 Jan 2026 03:09:50 +0000 Received: from CH1PEPF0000AD83.namprd04.prod.outlook.com (2603:10b6:610:11b:cafe::10) by CH0PR03CA0414.outlook.office365.com (2603:10b6:610:11b::32) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9542.16 via Frontend Transport; Tue, 27 Jan 2026 03:09:45 +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 CH1PEPF0000AD83.mail.protection.outlook.com (10.167.244.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9564.3 via Frontend Transport; Tue, 27 Jan 2026 03:09:50 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) 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; Mon, 26 Jan 2026 19:09:39 -0800 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Mon, 26 Jan 2026 19:09:38 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.14) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Mon, 26 Jan 2026 19:09:38 -0800 From: Nicolin Chen To: CC: , , , , , , , , , , , Subject: [PATCH v10 6/8] iommu/arm-smmu-v3: Populate smmu_domain->invs when attaching masters Date: Mon, 26 Jan 2026 19:09:17 -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: CH1PEPF0000AD83:EE_|DS7PR12MB5936:EE_ X-MS-Office365-Filtering-Correlation-Id: b8e0ac1a-7e36-410e-4d13-08de5d5188d9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|1800799024|82310400026|376014|7416014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?O7NLSQMlVJd12atEccgaCiDuKRqZ3CHDrB6+QY04IyKMlDfPorHQzFkKRT3w?= =?us-ascii?Q?cl38Bir6E5JzC/xmFCSCsdhL8q84Noa2PiyBzIUJnbVpxYV8L26JirDg2fQO?= =?us-ascii?Q?oEF5BI5zdFkgsdCQfeY3T7mMJqsZJ3WiMnJQJKsH1SBYvWnqvW191jIzL2WO?= =?us-ascii?Q?inF1LuYqzGfjROFvqfzieV1kLmnts2RRvtl5sCq3/XE/Q2J0GJG6yB1Vvks8?= =?us-ascii?Q?H3hdpEmQIzxZQaJ6alJ+Z3A9VVBtmnE/DWfMpinYFCdBPeHteS75dvSlTdlr?= =?us-ascii?Q?dl3sBkF+t6gu1b7HvyQLe9i0jdWb6RJLEsTktH4Dj7iULHO0i8GCqLU4FNLe?= =?us-ascii?Q?OIuTqdDCthXBr4wKxkTC9E+kDnnqdbHAPqbalt3zdHa66Cp1/Zcj0XUXy8ba?= =?us-ascii?Q?LLepFVjFQw1GTNLy0kimXIi5K8UNQdoFQ9d3dQefG4w/DBMeLrjtHiAxrtlg?= =?us-ascii?Q?wLHCXF4revNOLJ/aBVjZuUEJBXGmlx/Hky7J+WwfKtsS/AdcQeJWfk9sF/Kq?= =?us-ascii?Q?YQ6fUh8h2oru11ozMA6B3JaopRExo3/mWOHbvqrVauZP4v33IyeODMVOdmvU?= =?us-ascii?Q?dfyWBYgqMk6pJA7aMinRbBOu8qGNv4BV27GK0gopyPebVWa5oc9lS6W+d+dF?= =?us-ascii?Q?iNhbYq3V8u7CI+S8tZavmGwCe2DS3/26wSS/DJ4QNRZ/xM+hbvZt0bdJ3dHa?= =?us-ascii?Q?OCzp1qYJrSN0ZgKMxNTjjzeltn2kbrzdyhFkRSyVS1r5Dd6ykBmeU0W7FpLO?= =?us-ascii?Q?5Y41yMwosGfjvFGkM1shuZuPbM2dxRnWkXB8JJBU47gNlwphlu1YLUVZ9EJu?= =?us-ascii?Q?JPiQ/den5H2wVs54Wkem6/0VIwEay2nqjcHJZkowxhq21td5JIq1ER0UjgBK?= =?us-ascii?Q?j2BA9JtGUZlx7EHhQll2BS97YsWMOGx1GIiceOxsmh5XymTsCIjAXABNfRV5?= =?us-ascii?Q?2q8DkcNuInmUXnkHoUwd3OnoXg1EglWbbDWmmJMxXFKrMJ1e40Hf+pgiSqg3?= =?us-ascii?Q?4PW8l21xkceldeLRHxzZcr9r9BndR2qPD4ntMvTM56k3r0+Zj7PCxyAn9uRn?= =?us-ascii?Q?pOGUU3OhpUVvCClNsprtGWs/V4IoN6h4ZREMJWasD/03LU5+LFXaRb0PKMMA?= =?us-ascii?Q?3lwj+Rzqb4dFeHPyuiMQnaUiVo+4H0xK2gdEYbn5/zS48x3Dbqd2IWyXOB/Y?= =?us-ascii?Q?1zaafKt7cTAn0m66iixp5YZlShN2N+xCXHRL5KW0FVRFrpZrYRhY2GpWfPte?= =?us-ascii?Q?12/izHXi/WFYLcj8Iy7suFAhu/eMzHdFP+IfGLP/kUJcDyXQmRKx1tuRyTry?= =?us-ascii?Q?LEHEifkUvMEuTOqGb3OIMuu9ScMaOTYmlXy3tgFgMn/LJD0M7k3R5CiIlhCQ?= =?us-ascii?Q?tzQNjgsRNtoEu9k38xFQd0RCJwntIUIwdSHHiW16M5BPedyQppr3+uKxZ8mq?= =?us-ascii?Q?0gjInrX5384ka2BkdiG6FrnZCXgmTVyDQOVKZyaz3BwWk+8SQrZVHP5d6qgw?= =?us-ascii?Q?TzhO3NhvzivAl1eI44G3dl5+1Fw3gdQ1yIKsQI+37ofTvO0xYfEUS17aoFe+?= =?us-ascii?Q?iA7dL7F+Qo7G5f2ojkiLF7wrg4ydim2onjjjolvIf5J+BDsZo2MznaQ+g3AB?= =?us-ascii?Q?aIQn44f2s1I4X7+c6M3kVClGRCd09DyvLwu2Twqul2YrLkUQ7UHd/A3kDTBX?= =?us-ascii?Q?3Z8uoA=3D=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)(36860700013)(1800799024)(82310400026)(376014)(7416014);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jan 2026 03:09:50.1438 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b8e0ac1a-7e36-410e-4d13-08de5d5188d9 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: CH1PEPF0000AD83.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB5936 Content-Type: text/plain; charset="utf-8" Update the invs array with the invalidations required by each domain type during attachment operations. Only an SVA domain or a paging domain will have an invs array: a. SVA domain will add an INV_TYPE_S1_ASID per SMMU and an INV_TYPE_ATS per SID b. Non-nesting-parent paging domain with no ATS-enabled master will add a single INV_TYPE_S1_ASID or INV_TYPE_S2_VMID per SMMU c. Non-nesting-parent paging domain with ATS-enabled master(s) will do (b) and add an INV_TYPE_ATS per SID d. Nesting-parent paging domain will add an INV_TYPE_S2_VMID followed by an INV_TYPE_S2_VMID_S1_CLEAR per vSMMU. For an ATS-enabled master, it will add an INV_TYPE_ATS_FULL per SID Note that case #d prepares for a future implementation of VMID allocation which requires a followup series for S2 domain sharing. So when a nesting parent domain is attached through a vSMMU instance using a nested domain. VMID will be allocated per vSMMU instance v.s. currectly per S2 domain. The per-domain invalidation is not needed until the domain is attached to a master (when it starts to possibly use TLB). This will make it possible to attach the domain to multiple SMMUs and avoid unnecessary invalidation overhead during teardown if no STEs/CDs refer to the domain. It also means that when the last device is detached, the old domain must flush its ASID or VMID, since any new iommu_unmap() call would not trigger invalidations given an empty domain->invs array. Introduce some arm_smmu_invs helper functions for building scratch arrays, preparing and installing old/new domain's invalidation arrays. Co-developed-by: Jason Gunthorpe Signed-off-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe Signed-off-by: Nicolin Chen --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 17 ++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 260 +++++++++++++++++++- 2 files changed, 276 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/ar= m/arm-smmu-v3/arm-smmu-v3.h index 5e0e5055af1e..83d7e4952dff 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -1102,6 +1102,21 @@ static inline bool arm_smmu_master_canwbs(struct arm= _smmu_master *master) IOMMU_FWSPEC_PCI_RC_CANWBS; } =20 +/** + * struct arm_smmu_inv_state - Per-domain invalidation array state + * @invs_ptr: points to the domain->invs (unwinding nesting/etc.) or is NU= LL if + * no change should be made + * @old_invs: the original invs array + * @new_invs: for new domain, this is the new invs array to update domain-= >invs; + * for old domain, this is the master->build_invs to pass in as= the + * to_unref argument to an arm_smmu_invs_unref() call + */ +struct arm_smmu_inv_state { + struct arm_smmu_invs __rcu **invs_ptr; + struct arm_smmu_invs *old_invs; + struct arm_smmu_invs *new_invs; +}; + struct arm_smmu_attach_state { /* Inputs */ struct iommu_domain *old_domain; @@ -1111,6 +1126,8 @@ struct arm_smmu_attach_state { ioasid_t ssid; /* Resulting state */ struct arm_smmu_vmaster *vmaster; + struct arm_smmu_inv_state old_domain_invst; + struct arm_smmu_inv_state new_domain_invst; bool ats_enabled; }; =20 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 5a0a8b136352..4648d0aad693 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3143,6 +3143,121 @@ static void arm_smmu_disable_iopf(struct arm_smmu_m= aster *master, iopf_queue_remove_device(master->smmu->evtq.iopf, master->dev); } =20 +static struct arm_smmu_inv * +arm_smmu_master_build_inv(struct arm_smmu_master *master, + enum arm_smmu_inv_type type, u32 id, ioasid_t ssid, + size_t pgsize) +{ + struct arm_smmu_invs *build_invs =3D master->build_invs; + struct arm_smmu_inv *cur, inv =3D { + .smmu =3D master->smmu, + .type =3D type, + .id =3D id, + .pgsize =3D pgsize, + }; + + if (WARN_ON(build_invs->num_invs >=3D build_invs->max_invs)) + return NULL; + cur =3D &build_invs->inv[build_invs->num_invs]; + build_invs->num_invs++; + + *cur =3D inv; + switch (type) { + case INV_TYPE_S1_ASID: + /* + * For S1 page tables the driver always uses VMID=3D0, and the + * invalidation logic for this type will set it as well. + */ + if (master->smmu->features & ARM_SMMU_FEAT_E2H) { + cur->size_opcode =3D CMDQ_OP_TLBI_EL2_VA; + cur->nsize_opcode =3D CMDQ_OP_TLBI_EL2_ASID; + } else { + cur->size_opcode =3D CMDQ_OP_TLBI_NH_VA; + cur->nsize_opcode =3D CMDQ_OP_TLBI_NH_ASID; + } + break; + case INV_TYPE_S2_VMID: + cur->size_opcode =3D CMDQ_OP_TLBI_S2_IPA; + cur->nsize_opcode =3D CMDQ_OP_TLBI_S12_VMALL; + break; + case INV_TYPE_S2_VMID_S1_CLEAR: + cur->size_opcode =3D cur->nsize_opcode =3D CMDQ_OP_TLBI_NH_ALL; + break; + case INV_TYPE_ATS: + case INV_TYPE_ATS_FULL: + cur->size_opcode =3D cur->nsize_opcode =3D CMDQ_OP_ATC_INV; + cur->ssid =3D ssid; + break; + } + + return cur; +} + +/* + * Use the preallocated scratch array at master->build_invs, to build a to= _merge + * or to_unref array, to pass into a following arm_smmu_invs_merge/unref()= call. + * + * Do not free the returned invs array. It is reused, and will be overwrit= ten by + * the next arm_smmu_master_build_invs() call. + */ +static struct arm_smmu_invs * +arm_smmu_master_build_invs(struct arm_smmu_master *master, bool ats_enable= d, + ioasid_t ssid, struct arm_smmu_domain *smmu_domain) +{ + const bool nesting =3D smmu_domain->nest_parent; + size_t pgsize =3D 0, i; + + iommu_group_mutex_assert(master->dev); + + master->build_invs->num_invs =3D 0; + + /* Range-based invalidation requires the leaf pgsize for calculation */ + if (master->smmu->features & ARM_SMMU_FEAT_RANGE_INV) + pgsize =3D __ffs(smmu_domain->domain.pgsize_bitmap); + + switch (smmu_domain->stage) { + case ARM_SMMU_DOMAIN_SVA: + case ARM_SMMU_DOMAIN_S1: + if (!arm_smmu_master_build_inv(master, INV_TYPE_S1_ASID, + smmu_domain->cd.asid, + IOMMU_NO_PASID, pgsize)) + return NULL; + break; + case ARM_SMMU_DOMAIN_S2: + if (!arm_smmu_master_build_inv(master, INV_TYPE_S2_VMID, + smmu_domain->s2_cfg.vmid, + IOMMU_NO_PASID, pgsize)) + return NULL; + break; + default: + WARN_ON(true); + return NULL; + } + + /* All the nested S1 ASIDs have to be flushed when S2 parent changes */ + if (nesting) { + if (!arm_smmu_master_build_inv( + master, INV_TYPE_S2_VMID_S1_CLEAR, + smmu_domain->s2_cfg.vmid, IOMMU_NO_PASID, 0)) + return NULL; + } + + for (i =3D 0; ats_enabled && i < master->num_streams; i++) { + /* + * If an S2 used as a nesting parent is changed we have no + * option but to completely flush the ATC. + */ + if (!arm_smmu_master_build_inv( + master, nesting ? INV_TYPE_ATS_FULL : INV_TYPE_ATS, + master->streams[i].id, ssid, 0)) + return NULL; + } + + /* Note this build_invs must have been sorted */ + + return master->build_invs; +} + static void arm_smmu_remove_master_domain(struct arm_smmu_master *master, struct iommu_domain *domain, ioasid_t ssid) @@ -3172,6 +3287,133 @@ static void arm_smmu_remove_master_domain(struct ar= m_smmu_master *master, kfree(master_domain); } =20 +/* + * During attachment, the updates of the two domain->invs arrays are seque= nced: + * 1. new domain updates its invs array, merging master->build_invs + * 2. new domain starts to include the master during its invalidation + * 3. master updates its STE switching from the old domain to the new dom= ain + * 4. old domain still includes the master during its invalidation + * 5. old domain updates its invs array, unreferencing master->build_invs + * + * For 1 and 5, prepare the two updated arrays in advance, handling any ch= anges + * that can possibly failure. So the actual update of either 1 or 5 won't = fail. + * arm_smmu_asid_lock ensures that the old invs in the domains are intact = while + * we are sequencing to update them. + */ +static int arm_smmu_attach_prepare_invs(struct arm_smmu_attach_state *stat= e, + struct arm_smmu_domain *new_smmu_domain) +{ + struct arm_smmu_domain *old_smmu_domain =3D + to_smmu_domain_devices(state->old_domain); + struct arm_smmu_master *master =3D state->master; + ioasid_t ssid =3D state->ssid; + + /* + * At this point a NULL domain indicates the domain doesn't use the + * IOTLB, see to_smmu_domain_devices(). + */ + if (new_smmu_domain) { + struct arm_smmu_inv_state *invst =3D &state->new_domain_invst; + struct arm_smmu_invs *build_invs; + + invst->invs_ptr =3D &new_smmu_domain->invs; + invst->old_invs =3D rcu_dereference_protected( + new_smmu_domain->invs, + lockdep_is_held(&arm_smmu_asid_lock)); + build_invs =3D arm_smmu_master_build_invs( + master, state->ats_enabled, ssid, new_smmu_domain); + if (!build_invs) + return -EINVAL; + + invst->new_invs =3D + arm_smmu_invs_merge(invst->old_invs, build_invs); + if (IS_ERR(invst->new_invs)) + return PTR_ERR(invst->new_invs); + } + + if (old_smmu_domain) { + struct arm_smmu_inv_state *invst =3D &state->old_domain_invst; + + invst->invs_ptr =3D &old_smmu_domain->invs; + /* A re-attach case might have a different ats_enabled state */ + if (new_smmu_domain =3D=3D old_smmu_domain) + invst->old_invs =3D state->new_domain_invst.new_invs; + else + invst->old_invs =3D rcu_dereference_protected( + old_smmu_domain->invs, + lockdep_is_held(&arm_smmu_asid_lock)); + /* For old_smmu_domain, new_invs points to master->build_invs */ + invst->new_invs =3D arm_smmu_master_build_invs( + master, master->ats_enabled, ssid, old_smmu_domain); + } + + return 0; +} + +/* Must be installed before arm_smmu_install_ste_for_dev() */ +static void +arm_smmu_install_new_domain_invs(struct arm_smmu_attach_state *state) +{ + struct arm_smmu_inv_state *invst =3D &state->new_domain_invst; + + if (!invst->invs_ptr) + return; + + rcu_assign_pointer(*invst->invs_ptr, invst->new_invs); + kfree_rcu(invst->old_invs, rcu); +} + +static void arm_smmu_inv_flush_iotlb_tag(struct arm_smmu_inv *inv) +{ + struct arm_smmu_cmdq_ent cmd =3D {}; + + switch (inv->type) { + case INV_TYPE_S1_ASID: + cmd.tlbi.asid =3D inv->id; + break; + case INV_TYPE_S2_VMID: + /* S2_VMID using nsize_opcode covers S2_VMID_S1_CLEAR */ + cmd.tlbi.vmid =3D inv->id; + break; + default: + return; + } + + cmd.opcode =3D inv->nsize_opcode; + arm_smmu_cmdq_issue_cmd_with_sync(inv->smmu, &cmd); +} + +/* Should be installed after arm_smmu_install_ste_for_dev() */ +static void +arm_smmu_install_old_domain_invs(struct arm_smmu_attach_state *state) +{ + struct arm_smmu_inv_state *invst =3D &state->old_domain_invst; + struct arm_smmu_invs *old_invs =3D invst->old_invs; + struct arm_smmu_invs *new_invs; + + lockdep_assert_held(&arm_smmu_asid_lock); + + if (!invst->invs_ptr) + return; + + arm_smmu_invs_unref(old_invs, invst->new_invs); + /* + * When an IOTLB tag (the first entry in invs->new_invs) is no longer use= d, + * it means the ASID or VMID will no longer be invalidated by map/unmap a= nd + * must be cleaned right now. The rule is that any ASID/VMID not in an in= vs + * array must be left cleared in the IOTLB. + */ + if (!READ_ONCE(invst->new_invs->inv[0].users)) + arm_smmu_inv_flush_iotlb_tag(&invst->new_invs->inv[0]); + + new_invs =3D arm_smmu_invs_purge(old_invs); + if (!new_invs) + return; + + rcu_assign_pointer(*invst->invs_ptr, new_invs); + kfree_rcu(old_invs, rcu); +} + /* * Start the sequence to attach a domain to a master. The sequence contain= s three * steps: @@ -3229,12 +3471,16 @@ int arm_smmu_attach_prepare(struct arm_smmu_attach_= state *state, arm_smmu_ats_supported(master); } =20 + ret =3D arm_smmu_attach_prepare_invs(state, smmu_domain); + if (ret) + return ret; + if (smmu_domain) { if (new_domain->type =3D=3D IOMMU_DOMAIN_NESTED) { ret =3D arm_smmu_attach_prepare_vmaster( state, to_smmu_nested_domain(new_domain)); if (ret) - return ret; + goto err_unprepare_invs; } =20 master_domain =3D kzalloc(sizeof(*master_domain), GFP_KERNEL); @@ -3282,6 +3528,8 @@ int arm_smmu_attach_prepare(struct arm_smmu_attach_st= ate *state, atomic_inc(&smmu_domain->nr_ats_masters); list_add(&master_domain->devices_elm, &smmu_domain->devices); spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); + + arm_smmu_install_new_domain_invs(state); } =20 if (!state->ats_enabled && master->ats_enabled) { @@ -3301,6 +3549,8 @@ int arm_smmu_attach_prepare(struct arm_smmu_attach_st= ate *state, kfree(master_domain); err_free_vmaster: kfree(state->vmaster); +err_unprepare_invs: + kfree(state->new_domain_invst.new_invs); return ret; } =20 @@ -3332,6 +3582,7 @@ void arm_smmu_attach_commit(struct arm_smmu_attach_st= ate *state) } =20 arm_smmu_remove_master_domain(master, state->old_domain, state->ssid); + arm_smmu_install_old_domain_invs(state); master->ats_enabled =3D state->ats_enabled; } =20 @@ -3513,12 +3764,19 @@ static int arm_smmu_blocking_set_dev_pasid(struct i= ommu_domain *new_domain, { struct arm_smmu_domain *smmu_domain =3D to_smmu_domain(old_domain); struct arm_smmu_master *master =3D dev_iommu_priv_get(dev); + struct arm_smmu_attach_state state =3D { + .master =3D master, + .old_domain =3D old_domain, + .ssid =3D pasid, + }; =20 mutex_lock(&arm_smmu_asid_lock); + arm_smmu_attach_prepare_invs(&state, NULL); arm_smmu_clear_cd(master, pasid); if (master->ats_enabled) arm_smmu_atc_inv_master(master, pasid); arm_smmu_remove_master_domain(master, &smmu_domain->domain, pasid); + arm_smmu_install_old_domain_invs(&state); mutex_unlock(&arm_smmu_asid_lock); =20 /* --=20 2.43.0