From nobody Fri Dec 19 21:45:36 2025 Received: from CH1PR05CU001.outbound.protection.outlook.com (mail-northcentralusazon11010006.outbound.protection.outlook.com [52.101.193.6]) (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 49E452D7DDE for ; Sat, 8 Nov 2025 08:08:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.193.6 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762589319; cv=fail; b=mrNLsCTWHwBz4k2cnCpRKZtCGajIEs9ScrPd5jSTXH0b/bxcdmM2CLd+2OBXyd2a0o++h+pMS0d0s6tBURktOPCPB0pjrCPJeXqIkv4imzMxtdZJRqQ7hpOE6l5TVoiotwcwSjMY+MrtTW3Eaw/6aqgpb45LZR8+8LS+/tn0s80= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762589319; c=relaxed/simple; bh=xgzFpVrLkzYdxQafr/ZsDBol0x3vUnokHIREs/O0aLE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=FtRpwqMELDEGjxV7kTUMDq0g7JfBfLsUZB66iHOTKpfeL0TMe4Xlud5g1jIENDO7SViCb/jiCcd132E+R4rAzUNfNmREJDhAXfJEEfFquogGMPnnYuORgHwLpvVvJ0T/1FUfKOS5nBo9XD4gM5+dDoyrzj7dhT6XbRGI+sxnqWI= 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=RSpuv50J; arc=fail smtp.client-ip=52.101.193.6 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="RSpuv50J" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=MwtdNC4BBGTSfLHEzdd3xAkX0s+bnVRUQq5ms2xYcLKz4ICJh3OFmmBDkWqV0plPRQGPRgDQNnlsm2E2E9xcYUM2EGPrZPve9nXYGLmRDN+vBeRVg8CDXfN90FsODAbZlGD8/sHJ3bGcTQYg60ZD6uzLwX8RmwlES7SxtrKFS0Hc7SN8C2Kufy28HTJnee0UbCiueyrGDVOi9AdCMM/ogWuhZS3Tk8thCTf6gkeyjbMIuZUCVMxXrax3gNsxxMPTTtc/bt6x07mFo0vDvpJPcR/yJ4H0R0c69tq34AVksz24DKJy7FL7FPdxXCxBj+mmqg/+pHRIfrmEjAhmQt8HRw== 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=wyp2uxdJSaWI/GLKYnSpX8UVcm+Vvxd+5Dyzn0Jm0eA=; b=Fgvt21apYNdFdwYVKBpzAUYQG3qURi2Cb1iCM7DEMjFZL/QZ3orYOmJeSckefBggjSRnbowl5inhuqYEBvgonKkHhQHdFf6+iQPoUjsUEx65Azb8WBgIFRCV12q66OrJdF6wmo7DXTst9nKbeLAefhN+C0CAurwxPJ/BWHQp8mBPw3NbEhFnydlYmcXxJASH3A3NpVjFTHcHbQCZ6FqxiyP0ShxTpi3bDWp136soAEb1HFkID6tDh1+/IOTqVvprB9KPcImNAUl66w8FP/LhiVGAiY0fG6f87Hc6cEMUlvIilactPV2nrcCQH62eSi9wiF+JRxDFgpqtbk7dzxlsyg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) 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=wyp2uxdJSaWI/GLKYnSpX8UVcm+Vvxd+5Dyzn0Jm0eA=; b=RSpuv50JIP659yMzxuGvkHOh9YeiO0CHWLRJc/UUND2ZGLqrCUvBaDla+xFq2s2ir/a/fZqyIFcBc8qUerRqdburav3Mh0yfeLNsteVGqpAm+B0guzE8Dc0CmpbzUCtymwqUKA12AWxdhdFHa6kT/A3C0yWcQMq60qwk3MKWt5RxJi4WPO/VOpjveFBTVgmAyfEeaEN1lsQcnexTbjRpAjPy1Xvna7V7iuefzcqixTUjgG0FyAeKRI5d4eJamErMy6T94dEI3GWJpryLLRdNJwNtcb8hz3RiYGNLgOqYxgFXAPm6dYtSeMox7wCZINBH1PgWQSEiZkQJLGQ66UG73Q== Received: from MN2PR14CA0016.namprd14.prod.outlook.com (2603:10b6:208:23e::21) by IA1PR12MB7589.namprd12.prod.outlook.com (2603:10b6:208:42b::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9298.13; Sat, 8 Nov 2025 08:08:33 +0000 Received: from MN1PEPF0000F0DE.namprd04.prod.outlook.com (2603:10b6:208:23e:cafe::7) by MN2PR14CA0016.outlook.office365.com (2603:10b6:208:23e::21) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9298.15 via Frontend Transport; Sat, 8 Nov 2025 08:08:33 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) 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.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by MN1PEPF0000F0DE.mail.protection.outlook.com (10.167.242.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9320.13 via Frontend Transport; Sat, 8 Nov 2025 08:08:32 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 8 Nov 2025 00:08:18 -0800 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 8 Nov 2025 00:08:18 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Sat, 8 Nov 2025 00:08:17 -0800 From: Nicolin Chen To: CC: , , , , , , , , , , , Subject: [PATCH v5 1/7] iommu/arm-smmu-v3: Explicitly set smmu_domain->stage for SVA Date: Sat, 8 Nov 2025 00:08:02 -0800 Message-ID: <65c692221d1b1cf0e8df7329ba9183b92932c40c.1762588839.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: MN1PEPF0000F0DE:EE_|IA1PR12MB7589:EE_ X-MS-Office365-Filtering-Correlation-Id: 6b725529-cd4b-4383-7828-08de1e9e0297 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|36860700013|7416014|376014|82310400026; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?NhUYSMY3HbZ/TKrMOFZMWy3Jdzktw7fWVgZy1rxYgc1Tzg/VjSh0lpW0SL1u?= =?us-ascii?Q?+uAYIc7pC8A2RPgMd537sl+NdR/kboRaCBTm6C2Bn8IpYBPctCliMbMAIexh?= =?us-ascii?Q?Ht9cpjfd8kV9Ea17WbnnExeA4kugbJN9JYSqzjcXfP5yNj0S4ylwq6ricqX1?= =?us-ascii?Q?ZwQBXlhZ4PtIi7pLSC03p+rUBStaDiecDyGsF1VK/dtIBV1pJDc3gH3L0EJr?= =?us-ascii?Q?QGc8oN4yY9Z1e0OBHEjwvi0bAcSoCpRZOJUSnCS7ZfIzyti3dKsxR2vWmWNE?= =?us-ascii?Q?mP5ASshYSeYif4ZcsNShkIWALqQIr86jcpGPpj1qehoq7aeeznoZsNxrLjF+?= =?us-ascii?Q?CRro/VfIRS6adv3qSKp2CrogY+PjX4Ipv0VFlfbGAfnt2BJ9dlRqBTppqFhl?= =?us-ascii?Q?YbKVJtT2fa0dcomdO+srld7GfnkjpzB1qPtqcF+5N1wwdltUwMJB9NlmJDd/?= =?us-ascii?Q?FRByGVPzWXJV/ZzcdFhge/jncNFftCKsaDvFBA+PPGp0LD5uumIhIt2GpbB0?= =?us-ascii?Q?2HzR8bAZ37XRy1gSj4b7SIvdtw37ccLMY3bDUhMGOsG+iuKiAIEBMuCVaTP/?= =?us-ascii?Q?YNkI5Jqx3M3Qa94lt0fm56mQy9Xf/UG/EP+KVZAzPpBauEB+bQ82tLEn/ggd?= =?us-ascii?Q?kjqh+ravv1Ig8zA6d5EQlrKx3k9ox9v9WlBJ0dIOiXX8h2oxc4VB4bHyVJ1a?= =?us-ascii?Q?hKMiCMfT0R8RClkwzeqYTAr82DkNpw1gCOQAR5OEpbRtJcawJQBIYouGFGLA?= =?us-ascii?Q?fhbaFy3Nh0zf6btnKrQEDBLiOg2UxWtttL4Z8s4ZZ0AgmZqN8yOUbAZoHzEK?= =?us-ascii?Q?bhxmpwb6scvu5h98FOcKYaeU1rEClkPOJAIipzkZlGvxToS8hSu3nJl7+NAp?= =?us-ascii?Q?vAAuXCqFidhWI/vrJD0oZ/ijpIFEjxE9BQKTU1euudf58gSMD0x1nChAn0hW?= =?us-ascii?Q?cZQLNn1gix9Jh3s2M2IDsB4B1sMg1mnP1ogiYzYZaK4Omk+nBk9Q4/gwG0gY?= =?us-ascii?Q?GgVrkjsDREHnKsi1NkB0BbdyR5Dgpx/2AOnhQ/kVBm+dYxl2Y77/OFbWdxRn?= =?us-ascii?Q?xykge3edmNx6eT/GR9WUmkg1wCx2w6aXYzXTzzHCyLkcgsAxDl1rei8m9d+y?= =?us-ascii?Q?MtNL+arJX1uz+QQQBVgF65fImAgOJLYiK6Jg7FTJsEJifTNqnPtHpA/gqR9X?= =?us-ascii?Q?gZIdOdnvSew0EnKk8nfh5za2/bstJmO6YU4W6+yOJBU9jFLP7tmuMw2oSaFq?= =?us-ascii?Q?mQ52N1TymAmWHsutdKYstuwdZ2cvsrr7LZyP/YjDLbX9Z+MbitPAAwljx0Hp?= =?us-ascii?Q?8fp35ALuSjaQiUO+qfuRCUywUwz6njXQDTBK0NAW25dgG/OvjbsXfg/nuK/t?= =?us-ascii?Q?rAIv12BHw4mEg09jObDJleNgqoNj2kxbvn5C3cHhKdFLdDPAW93aE1sSHJMh?= =?us-ascii?Q?EXwczLBFLAhdHoYDhHthpT1jVgVbJ3RjHSywAGdqW6Z/ozfeODwMRaekE6r+?= =?us-ascii?Q?Fe7iZ6fQwIA0Y1+FqLuOsfPjfs+jdEfMl5k4XtweNxoV4z4guLCIkuxg2lEx?= =?us-ascii?Q?BGgCigKjt2UZIijDkbg=3D?= X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(1800799024)(36860700013)(7416014)(376014)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2025 08:08:32.8418 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6b725529-cd4b-4383-7828-08de1e9e0297 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: MN1PEPF0000F0DE.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB7589 Content-Type: text/plain; charset="utf-8" Both the ARM_SMMU_DOMAIN_S1 case and the SVA case use ASID, requiring ASID based invalidation commands to flush the TLB. Define an ARM_SMMU_DOMAIN_SVA to make the SVA case clear to share the same path with the ARM_SMMU_DOMAIN_S1 case, which will be a part of the routine to build a new per-domain invalidation array. There is no function change. Suggested-by: Jason Gunthorpe Acked-by: Balbir Singh Reviewed-by: Jason Gunthorpe Signed-off-by: Nicolin Chen --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 1 + drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 1 + drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 3 +++ 3 files changed, 5 insertions(+) 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 ae23aacc38402..5c0b38595d209 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -858,6 +858,7 @@ struct arm_smmu_master { enum arm_smmu_domain_stage { ARM_SMMU_DOMAIN_S1 =3D 0, ARM_SMMU_DOMAIN_S2, + ARM_SMMU_DOMAIN_SVA, }; =20 struct arm_smmu_domain { diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iomm= u/arm/arm-smmu-v3/arm-smmu-v3-sva.c index 59a480974d80f..6097f1f540d87 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -346,6 +346,7 @@ struct iommu_domain *arm_smmu_sva_domain_alloc(struct d= evice *dev, * ARM_SMMU_FEAT_RANGE_INV is present */ smmu_domain->domain.pgsize_bitmap =3D PAGE_SIZE; + smmu_domain->stage =3D ARM_SMMU_DOMAIN_SVA; smmu_domain->smmu =3D smmu; =20 ret =3D xa_alloc(&arm_smmu_asid_xa, &asid, smmu_domain, 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 2a8b46b948f05..0312bb79f1247 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3064,6 +3064,9 @@ static int arm_smmu_attach_dev(struct iommu_domain *d= omain, struct device *dev) arm_smmu_install_ste_for_dev(master, &target); arm_smmu_clear_cd(master, IOMMU_NO_PASID); break; + default: + WARN_ON(true); + break; } =20 arm_smmu_attach_commit(&state); --=20 2.43.0 From nobody Fri Dec 19 21:45:36 2025 Received: from CH1PR05CU001.outbound.protection.outlook.com (mail-northcentralusazon11010020.outbound.protection.outlook.com [52.101.193.20]) (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 1E06F4A0C for ; Sat, 8 Nov 2025 08:08:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.193.20 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762589307; cv=fail; b=AlnHKV+MYAU+RxMh5JP8rZ4EYKVZ0uCEXFdf2srAxRonyv8CPKe7L2bwg989WG/TMT0Ve65NT6kCLbmvU4Y54cJKLKRflCek57SukcRKfqNOB40ayIl5nnJmZ6Vf3zyt0EsJTA+P3g+1920cGq1l0nQIHBO6y+KkEVD97/ap+2w= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762589307; c=relaxed/simple; bh=KZIZIWn1zQChJgTIOl+S6E7L57MRaogqPgwFyGpOb6k=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=f4k+Txi6oOciUCaHBdHg41KPidSuH5zSHRkyrPVpADJxCBTiG+Bf+mxj82+Llry7if30wZuW6bxQYL17vgWetOT/pwFflF8b+XzR0FUCKYdDz+YsJJh2LHxDLZdNTqG6oPBBPhYok/GNbNzXtwnINbzGHOYTKDTOH7SS6nOGHIk= 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=FO3nMoFQ; arc=fail smtp.client-ip=52.101.193.20 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="FO3nMoFQ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=XKEsmggk0DcnkaCN2p9VIh0SDjc49HS9pNnQBjN5uXfzze1Uk7QD9qQfCEiV5eIv69PCsU+SMdXq8iUMevjlqo9WaX9Me9THnx+gh25onCTtUFnhxTSBzPdSA8FfjrbiLVIWR0xd1VN74WMun00rlU6EQrtoJWcG1k+iwh1UaAJZ/tQmjKEJ9f7CiEBy9LuJ+rxj1EW4AKfW+5uXKF5lOjlb6fCEQPV+XrUtHbFDVKTeTEkI9VOPIkEyERiBHEZB9M9P/dMLC66T5BHLAovv4QCh/5icPWRVoHOSQ9U8HDBZuvdru0DhU3buENFKPeUPWtrwsYLmW/M332yqnKRqPg== 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=6BTa8QmYcLNHh7ScbSBZc6eV9icoFmX+5SNT1U2iCBc=; b=V7EWVC8Q1jvJJUrEM1701qAwqkM0y+an0q4oPmfYU+f1Tx/IDXLWv0CzzsDa45FbegSByEZYjO0AO0juiVpyO7cYbDAooVybTUk4YPMpXU1JDcO3bwLJAEy7ciEOifvhYIegbmYiRMFfzepaWx5vWp8lWdJRVTPFbG8GJU3PxUU1nnO4CdYoVnA5uCCTInMfMLADL/4k98OjCff0/IkjBnoKlAcwJdUhx2kjlYriszwv4XET7MRMTanmPIBl6dTvZnA1ERF8e4A74NF62m2ylXh9Wae7/Vhclirzi/h5fUoGFNSdTtXKk5DkeiD+I91vVPhabN0ll8oX7QKC+o+8Ow== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) 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=6BTa8QmYcLNHh7ScbSBZc6eV9icoFmX+5SNT1U2iCBc=; b=FO3nMoFQsKGXeFGf29J1OdKBAHSiO7BTe7ywN+BnF3ISTiTPE3kjGlxnwut/QMzZCPZQ1RdgfUFmy8l5XLAIqm2EhUXK0cE04bDpfyBRHw95Xu58WWdmbGENVCGQbywUx+gcuO/3J+o7v+xM3cRkv5HjMdyrKM/msHcjk+ibrVq3u6u7+7H3ug279s3gbGpir+YUj3IKxRd19JnKRqmqg33cwy9n36L6jKGnN4CTFVeOq1JoqNlexg8xLlmC/h1DENxRXQNYtrwq2vqHO/s2GKLGJAn0+uxk38gJXk6jXp16pnEY3L4MRAHqmW5fVb1XSEJANFkBnC6mXeVcsfGpGg== Received: from SJ0PR03CA0297.namprd03.prod.outlook.com (2603:10b6:a03:39e::32) by DM4PR12MB8499.namprd12.prod.outlook.com (2603:10b6:8:181::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9298.12; Sat, 8 Nov 2025 08:08:22 +0000 Received: from SJ1PEPF00002315.namprd03.prod.outlook.com (2603:10b6:a03:39e:cafe::fb) by SJ0PR03CA0297.outlook.office365.com (2603:10b6:a03:39e::32) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9298.14 via Frontend Transport; Sat, 8 Nov 2025 08:08:18 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) 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.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by SJ1PEPF00002315.mail.protection.outlook.com (10.167.242.169) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9320.13 via Frontend Transport; Sat, 8 Nov 2025 08:08:22 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 8 Nov 2025 00:08:19 -0800 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 8 Nov 2025 00:08:19 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Sat, 8 Nov 2025 00:08:18 -0800 From: Nicolin Chen To: CC: , , , , , , , , , , , Subject: [PATCH v5 2/7] iommu/arm-smmu-v3: Add an inline arm_smmu_domain_free() Date: Sat, 8 Nov 2025 00:08:03 -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: SJ1PEPF00002315:EE_|DM4PR12MB8499:EE_ X-MS-Office365-Filtering-Correlation-Id: c3d8357a-0bad-4a7f-ee79-08de1e9dfc1a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|36860700013|7416014|376014|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?+3qLre1b/t2id0ZBLRpqcGudmqDIPBioCFBR8WizKe+PKKzUhcxZkoQIejyN?= =?us-ascii?Q?W8IXtdnuGgOI9ciZZ5FxcLS9haO6BU7gHC91E41wZyZA7nm8rcKTZeLZsjpF?= =?us-ascii?Q?EYvaG2XGdqYZd8VISu0tjYOksyw0Dg2LLvZz/vD0nEoY8Y/D19CBikk1GIxQ?= =?us-ascii?Q?xFSDCgthYjnHthu2FxYwChGEJrm3U4k+Omp66HK/ETVIwpb4Ay6o4DXoFWX6?= =?us-ascii?Q?ZtmojylT6+tb0RBLo2EWyWg+ye2JqMS6Q7NAdRynkZH5L4CbC96Hwvq3pExK?= =?us-ascii?Q?/c1LZ8ef+KLAWedexOVGmfrA9c6u7+3vkR0LfBRXni+cXJ20t20wdCzwILv8?= =?us-ascii?Q?XceIm/fPgAJ6sTvPWPN4dqGDOKjw1pkava8Fvz4YXFirwstkxbhlihl33lNt?= =?us-ascii?Q?cZoSJ+W+HJOlEmPPcNy0dsR0ar6Jp/GSaisz5XWRh9xoHc3+F+OhYKIS4Q2o?= =?us-ascii?Q?RPuySbQ3SDeOi8Rc1uZ5bmlK10VJgqQca6+tx9BoFg+cvuExdcurgSWj5rjA?= =?us-ascii?Q?e87JrkWjlh/CAOamY7t15hdKmblVDWqtnx9o/P3hApS+ASipDAi+45E3Y+Jw?= =?us-ascii?Q?/B0THnJsR021E0tbTPIDM7sGNxj9F4DPXjuqmGsGm6jz5yLJUjYTHwr3CtPd?= =?us-ascii?Q?Ej7VQwboovBqFSFY30uyz9KUll27doopyKa7cFvLGcB2HKtK51OLS5efaUn5?= =?us-ascii?Q?wj6+/GfAa2QeoQYMrJG2qYjl8R2cmA6EshifaNc06sLW0r7/6MJInFr4ohNo?= =?us-ascii?Q?c8nVhCVuqI3tbJ4GvOkwXB18cYy9IQHo2IbzbXMLgNStRj2Wvn3qzdMR3UoJ?= =?us-ascii?Q?B+zR7TYzxDxjqkVy0umcJWHeySvhU7/a+81Ty9csIKbjKi8tBiHl87F+wYyC?= =?us-ascii?Q?uRib9PRwykID+221ubrCJA2Qvru3+/IZJk1QIyy6ScLJbwOLf/82wIsMK9Hd?= =?us-ascii?Q?fLAQG/YfAfYvKj9wWveVVYUw7q8WwkgDSSzaKCIJX6Xj+jV3MIy9Lliejuh9?= =?us-ascii?Q?yZf+03xFbjzvGmm8Q4JiGNATHCW45IMGIcElitz3pWqfYKiqE9fseWi5cHwl?= =?us-ascii?Q?2rgQtAd5pruoTootMwgkpkIUwsiIHE6l01IQllcNjh8zfpLHGQAKfBGau/qi?= =?us-ascii?Q?w6DTacvGdjA5lpt/dOsxlM+1kVwLLBZuAU1eWxK9FGgtciIf14ElxbNX/wQ8?= =?us-ascii?Q?vmOz52Iat/j7efB1CHbhaLK9M2BPWFGOcQ4Mbfl0SLOHa0EWDARKA0v1+V5Q?= =?us-ascii?Q?PDzU2JlAvwICPIbqpTu4ypxUsShH/oCFJC4Uo853FBRzkB6fYbfE0HoRD/wM?= =?us-ascii?Q?o+m7QMBlFpptIhYXmFutqY7Sd26NJXiR2CrjBq5/OUV2YXLXt6SIr3bkuF7Z?= =?us-ascii?Q?R2o+D+7+XYV2sx/gcPZo6WHy3aKStvqVIEh+S4mbtUMhxhqtNlipEBkc+hdR?= =?us-ascii?Q?eJSSBK7hOMH9/gI6T0nggtYRmY7WDdWXoCrxIAgR5Ka75+a2t3Lzuha2HTBO?= =?us-ascii?Q?dUr+J5PrX/MmT+IupI0C6ItH8ZjsIXYAwPIkv3lf6AYOdclGERrM2mcbT07N?= =?us-ascii?Q?swijV0ttC2GbBrOGY5I=3D?= X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230040)(82310400026)(36860700013)(7416014)(376014)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2025 08:08:22.1088 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c3d8357a-0bad-4a7f-ee79-08de1e9dfc1a X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00002315.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB8499 Content-Type: text/plain; charset="utf-8" There will be a bit more things to free than smmu_domain itself. So keep a simple inline function in the header to share aross files. Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe Acked-by: Balbir Singh Signed-off-by: Nicolin Chen --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 5 +++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 2 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 4 ++-- 3 files changed, 8 insertions(+), 3 deletions(-) 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 5c0b38595d209..96a23ca633cb6 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -954,6 +954,11 @@ extern struct mutex arm_smmu_asid_lock; =20 struct arm_smmu_domain *arm_smmu_domain_alloc(void); =20 +static inline void arm_smmu_domain_free(struct arm_smmu_domain *smmu_domai= n) +{ + kfree(smmu_domain); +} + void arm_smmu_clear_cd(struct arm_smmu_master *master, ioasid_t ssid); struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, u32 ssid); diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iomm= u/arm/arm-smmu-v3/arm-smmu-v3-sva.c index 6097f1f540d87..fc601b494e0af 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -365,6 +365,6 @@ struct iommu_domain *arm_smmu_sva_domain_alloc(struct d= evice *dev, err_asid: xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); err_free: - kfree(smmu_domain); + arm_smmu_domain_free(smmu_domain); return ERR_PTR(ret); } 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 0312bb79f1247..00d43080efaa8 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2492,7 +2492,7 @@ static void arm_smmu_domain_free_paging(struct iommu_= domain *domain) ida_free(&smmu->vmid_map, cfg->vmid); } =20 - kfree(smmu_domain); + arm_smmu_domain_free(smmu_domain); } =20 static int arm_smmu_domain_finalise_s1(struct arm_smmu_device *smmu, @@ -3353,7 +3353,7 @@ arm_smmu_domain_alloc_paging_flags(struct device *dev= , u32 flags, return &smmu_domain->domain; =20 err_free: - kfree(smmu_domain); + arm_smmu_domain_free(smmu_domain); return ERR_PTR(ret); } =20 --=20 2.43.0 From nobody Fri Dec 19 21:45:36 2025 Received: from BYAPR05CU005.outbound.protection.outlook.com (mail-westusazon11010014.outbound.protection.outlook.com [52.101.85.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 DFE6C2E427C for ; Sat, 8 Nov 2025 08:08:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.85.14 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762589326; cv=fail; b=ZncW06AvCRWFY39YvONyX/HxwEsOMqeKWzCBHO44DMl/KRmAy9EYnAr+1u92IBLxed0vY7WS19jpD7BP9Ugyn253XQvUAHFzrJ+vC5/FXulG/cG1uHuXa25yunzVhkpfl/5XfF5eTC7YupZmFZ6TSrU629qrRX0whC8lxAg12E0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762589326; c=relaxed/simple; bh=RuSR8VYXJZkAfLUh0qOOKE00uyvzuipUWcrEFyHzHYY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Eqc/Ov2FWgQShBar5VQLj9z80DBEeQXZUAF18JWdBmMMoBNLBch0WH+K1nZwrmqvol9/HcZMiHrJm6OwaukZ+WSIZ3it4OrSgk1PUBdUegjWYdZCiSGganfYfb2y0MioaGAXktPbEHAQ5hjmZFoFv/HHvobJyHAhEzs0al4zuf4= 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=SdcOo+Wm; arc=fail smtp.client-ip=52.101.85.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="SdcOo+Wm" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Mthpx5J77D7hAPeNLcGN9FQW3BDZfhBcaeqt4LUjPtcfBVU/fGE2n2DgcQHTpIq0agbqZdpgv+EwYiv9hgR+hKoXhAePUSRyg9CTbjljuyMgbuZkz1E1EjDzhtrmqUi9ajz03xeIEH4Qox0COIk6+WMENei6r+ClKYmrw7zV8TCCoBkpY3tuOuamHL6D4tVLvbPMabZjFDmD58ITeUixdd6vgh5GXnOSceWvzcyZsBDV/XK8TNYHQDukspYA/EdE7HUBNMmIKQUEf6seNsMrMjiOOla9hewvlVR0E6MmXzPcj4XfgtwPhL2Frq7fwsTYSSge6tfNyvOgRcTpaKpFLw== 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=jThY2bIf+npU83qxVYZFlf4ThlbG8Y0f10w5VCXARFw=; b=oH9YGQ4GhwZ0SiPBWzbP3ArM22TCE+kk1zoId2YbCvCLpxFf7z4tX+RaVMt79PyDmFK2ChEsdqtRdEO6WEkdkk4k7+eFkhRVxQY99M1OWU3krLBgO4uf5MX1SDcB3byu1+ziWmOMHRMPbTjSegf4LsMxlHxCM2i3ROHaZKEIAqihvhwJXQAky8eFxm2dNdvQZ2qycPs+qlUUTPI+bVnu4QURoDlqHdVi9FNinx549yQG9X5uF8AX1rWzQlW76DGWq+EAl7QRTlmrnciiz+5UXjhyyuGnmO9jD7+jKi63WnckyvGrgSFDekiyMkKfqT7+FOkzt5+fEWM9LdjBVXesAw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) 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=jThY2bIf+npU83qxVYZFlf4ThlbG8Y0f10w5VCXARFw=; b=SdcOo+WmBCG7nCYxpRjYhqmL1WT6yUnp7z91uqBVGpTdv+1w35GDomD6mZKqB9CwJhBkl9oEMnq9MB5ZwKB1Jb4EpZQd+80eUuupAqZf2B7ArxZd5+4zpjnAlLvZay0lXRu5GtufleljSafFH4Mwlk5Clq5DSZd74iZeQFAR5kywTpzyhmSsf3HFY1s3M4BJ/DNK/Z97wpsJnadltd4To8AnLb08T1kfbY9SHOglw+in5eqcSyWH3OVnSgflIGTvtZGFCLT3UgJDmRWNVCMuRqISwNCGuRnTlEPCEEJ0SnX+wJTG0Zp4Tejc8hygIe46pFIv/dRYuE0/otB1bpeLJQ== Received: from MN2PR14CA0002.namprd14.prod.outlook.com (2603:10b6:208:23e::7) by DS7PR12MB6093.namprd12.prod.outlook.com (2603:10b6:8:9e::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9298.12; Sat, 8 Nov 2025 08:08:35 +0000 Received: from MN1PEPF0000F0DE.namprd04.prod.outlook.com (2603:10b6:208:23e:cafe::39) by MN2PR14CA0002.outlook.office365.com (2603:10b6:208:23e::7) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9298.15 via Frontend Transport; Sat, 8 Nov 2025 08:08:04 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) 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.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by MN1PEPF0000F0DE.mail.protection.outlook.com (10.167.242.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9320.13 via Frontend Transport; Sat, 8 Nov 2025 08:08:34 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 8 Nov 2025 00:08:21 -0800 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 8 Nov 2025 00:08:20 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Sat, 8 Nov 2025 00:08:19 -0800 From: Nicolin Chen To: CC: , , , , , , , , , , , Subject: [PATCH v5 3/7] iommu/arm-smmu-v3: Introduce a per-domain arm_smmu_invs array Date: Sat, 8 Nov 2025 00:08:04 -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: MN1PEPF0000F0DE:EE_|DS7PR12MB6093:EE_ X-MS-Office365-Filtering-Correlation-Id: faaf8171-63df-4f48-b7af-08de1e9e03c7 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?vxMBbYzl6Z2zgnzh3ThaG4hc+7w8lx5eBX71eUpHuFBp6P07mqSVAM5FjljR?= =?us-ascii?Q?4niL81Qn+LOwzcK85RhfJuRKMVAudkgPAjz3ilGn6yXDWYQU85MMIQT8Dg59?= =?us-ascii?Q?xDZ+iG/p4DnYPap5redrVfSuXAHrByPY96WCOstIRvTBy95Rm7Z/VZD8RK7i?= =?us-ascii?Q?aC3ZpcdwSWgx52H640AqRMi9d/ak/6Id1xBRcD72ogiHBCmEsPigrZiXE20n?= =?us-ascii?Q?NY893BK1dVv1mhL+xCXT5W0NlBfs+Qc3P9nEHMxmo36CdoF0T87FVplwztYv?= =?us-ascii?Q?iWPooVyK+CULGbpnqZ9xj7ZMvv+TOmZQ7zf/ZaOnKPjhTl9KHRqUSQOXVe78?= =?us-ascii?Q?kIEunemR+coIAWYOliYyjmM5q55tGsJp5n5TbGEl1jIe4JB1S0ol6QAGiwc4?= =?us-ascii?Q?mNC4NIql4ZFy9oWPPy8TEwzARVkWOB2O3Tg1QLdmjxRqex7M+mUfgxhVJkoW?= =?us-ascii?Q?hZZ4PEVJC4fK3rdlqGKUiUuiXwVzTGLBRqhHIC15R3fEBDWvL2kqUiiUzfWv?= =?us-ascii?Q?zdGN/ZVwptgbgOtkiHd2qMIg45caWPYpvfvaDleazEcn9pAiRojlriDrEXel?= =?us-ascii?Q?R0kxNc+e9mveMVkXA/1yh5wfKonvSAoRgVLI8+t711z+32uTw/NQo+OfXGoz?= =?us-ascii?Q?EvIoETbPmDaeHgyBU2AXw5nDnrVwuFjEwWtIZyxX8pKxuLB1pZSvtC+GMFpz?= =?us-ascii?Q?LONNdg5qDjOpCG5BPjSD95WWSBiooOdExrxqVZfr76qTPjZftkcRhxtFWweH?= =?us-ascii?Q?Kg2JQAYHpiLMZ2dqdl758Cr/WMWm1visnwUGKZf0tMpp99aB7rRqvKrPi2aK?= =?us-ascii?Q?h2RIdU9sKYn7Sn881JOedeznHM8+DIFkDJdJxI6C5pCa2WYeV91DoNcnobEL?= =?us-ascii?Q?JCNG9uiqlSy0N9SLsCR1R+tC3b+Bn8jo083OcHvdBeFEWvvMtow51sPeObRX?= =?us-ascii?Q?6frS/DVNwXONRJ3bha08UGvHOihKd17Vd4Bop/CRL5K4u4Tb/ghirLXfoqXX?= =?us-ascii?Q?NLG3sItn0dxPbE2p+b6eC6+3bxaucdnCNzssydILkOv+9cJ16DmsJu9oJstr?= =?us-ascii?Q?FIT3P/T5JdkBr/sz2JmOgqafobMh/Wp+s+rFt2Lu6Ikd57NONANaCt4VRkty?= =?us-ascii?Q?AyIWN9ZlF6BM7bOfTdQKSNIHV/wfN64973Qc32/zd2ozj9PJXbLr+a8kOxGt?= =?us-ascii?Q?ICfbVRZRYx07b5jEqa0lfkvVvJLQ4NPCtNFPgjHNzlljyepL+45xtuyEuJUP?= =?us-ascii?Q?f7P9CN+FIODXu0o/1z2gskGvQXY/PMXpmHFCW+8H3M7BryK67FrVEBSSEIl2?= =?us-ascii?Q?56IOdRlUUvhBFekFqcoRES2d+CBsiA9xVFB+HTFFvpAECjGZWhGKwrULa7la?= =?us-ascii?Q?Gedta19gnAvA5csfoThLgNLJbseTFmxV9D/iSxFo5nERM0VoFCO00MWjHSsa?= =?us-ascii?Q?zUGDdTTU8uYkXmJmZt2Zur3y7IEHK9/aCIRu1qMB9WBW21mkL9bAqInh56R3?= =?us-ascii?Q?dYbGh8Ukw4CzZZS9QdaZPMBxbzs1qdCwjmxkuzEn0XXpEpg7TFQRsHDbfETM?= =?us-ascii?Q?u4TdBZi7gen4UcGjlQU=3D?= X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(376014)(7416014)(36860700013)(1800799024)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2025 08:08:34.8381 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: faaf8171-63df-4f48-b7af-08de1e9e03c7 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: MN1PEPF0000F0DE.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6093 Content-Type: text/plain; charset="utf-8" From: Jason Gunthorpe Create a new data structure to hold an array of invalidations that need to be performed for the domain based on what masters are attached, to replace the single smmu pointer and linked list of masters in the current design. Each array entry holds one of the invalidation actions - S1_ASID, S2_VMID, ATS or their variant with information to feed invalidation commands to HW. It is structured so that multiple SMMUs can participate in the same array, removing one key limitation of the current system. To maximize performance, a sorted array is used as the data structure. It allows grouping SYNCs together to parallelize invalidations. For instance, it will group all the ATS entries after the ASID/VMID entry, so they will all be pushed to the PCI devices in parallel with one SYNC. To minimize the locking cost on the invalidation fast path (reader of the invalidation array), the array is managed with RCU. Provide a set of APIs to add/delete entries to/from an array, which cover cannot-fail attach cases, e.g. attaching to arm_smmu_blocked_domain. Also add kunit coverage for those APIs. Signed-off-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe Co-developed-by: Nicolin Chen Signed-off-by: Nicolin Chen --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 91 +++++++ .../iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c | 93 +++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 248 ++++++++++++++++++ 3 files changed, 432 insertions(+) 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 96a23ca633cb6..757158b9ea655 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -649,6 +649,85 @@ struct arm_smmu_cmdq_batch { int num; }; =20 +/* + * The order here also determines the sequence in which commands are sent = to the + * command queue. E.g. TLBI must be done before ATC_INV. + */ +enum arm_smmu_inv_type { + INV_TYPE_S1_ASID, + INV_TYPE_S2_VMID, + INV_TYPE_S2_VMID_S1_CLEAR, + INV_TYPE_ATS, + INV_TYPE_ATS_FULL, +}; + +struct arm_smmu_inv { + struct arm_smmu_device *smmu; + u8 type; + u8 size_opcode; + u8 nsize_opcode; + u32 id; /* ASID or VMID or SID */ + union { + size_t pgsize; /* ARM_SMMU_FEAT_RANGE_INV */ + u32 ssid; /* INV_TYPE_ATS */ + }; + + refcount_t users; /* users=3D0 to mark as a trash to be purged */ +}; + +static inline bool arm_smmu_inv_is_ats(struct arm_smmu_inv *inv) +{ + return inv->type =3D=3D INV_TYPE_ATS || inv->type =3D=3D INV_TYPE_ATS_FUL= L; +} + +/** + * struct arm_smmu_invs - Per-domain invalidation array + * @num_invs: number of invalidations in the flexible array + * @rwlock: optional rwlock to fench ATS operations + * @has_ats: flag if the array contains an INV_TYPE_ATS or INV_TYPE_ATS_FU= LL + * @rcu: rcu head for kfree_rcu() + * @inv: flexible invalidation array + * + * The arm_smmu_invs is an RCU data structure. During a ->attach_dev callb= ack, + * arm_smmu_invs_merge(), arm_smmu_invs_unref() and arm_smmu_invs_purge() = will + * be used to allocate a new copy of an old array for addition and deletio= n in + * the old domain's and new domain's invs arrays. + * + * The arm_smmu_invs_unref() mutates a given array, by internally reducing= the + * users counts of some given entries. This exists to support a no-fail ro= utine + * like attaching to an IOMMU_DOMAIN_BLOCKED. And it could pair with a fol= lowup + * arm_smmu_invs_purge() call to generate a new clean array. + * + * Concurrent invalidation thread will push every invalidation described i= n the + * array into the command queue for each invalidation event. It is designe= d like + * this to optimize the invalidation fast path by avoiding locks. + * + * A domain can be shared across SMMU instances. When an instance gets rem= oved, + * it would delete all the entries that belong to that SMMU instance. Then= , a + * synchronize_rcu() would have to be called to sync the array, to prevent= any + * concurrent invalidation thread accessing the old array from issuing com= mands + * to the command queue of a removed SMMU instance. + */ +struct arm_smmu_invs { + size_t num_invs; + rwlock_t rwlock; + bool has_ats; + struct rcu_head rcu; + struct arm_smmu_inv inv[]; +}; + +static inline struct arm_smmu_invs *arm_smmu_invs_alloc(size_t num_invs) +{ + struct arm_smmu_invs *new_invs; + + new_invs =3D kzalloc(struct_size(new_invs, inv, num_invs), GFP_KERNEL); + if (!new_invs) + return ERR_PTR(-ENOMEM); + rwlock_init(&new_invs->rwlock); + new_invs->num_invs =3D num_invs; + return new_invs; +} + struct arm_smmu_evtq { struct arm_smmu_queue q; struct iopf_queue *iopf; @@ -875,6 +954,8 @@ struct arm_smmu_domain { =20 struct iommu_domain domain; =20 + struct arm_smmu_invs __rcu *invs; + /* List of struct arm_smmu_master_domain */ struct list_head devices; spinlock_t devices_lock; @@ -923,6 +1004,14 @@ void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *t= arget, void arm_smmu_make_sva_cd(struct arm_smmu_cd *target, struct arm_smmu_master *master, struct mm_struct *mm, u16 asid); + +struct arm_smmu_invs *arm_smmu_invs_merge(struct arm_smmu_invs *invs, + struct arm_smmu_invs *to_merge); +size_t arm_smmu_invs_unref(struct arm_smmu_invs *invs, + struct arm_smmu_invs *to_unref, + void (*flush_fn)(struct arm_smmu_inv *inv)); +struct arm_smmu_invs *arm_smmu_invs_purge(struct arm_smmu_invs *invs, + size_t num_trashes); #endif =20 struct arm_smmu_master_domain { @@ -956,6 +1045,8 @@ struct arm_smmu_domain *arm_smmu_domain_alloc(void); =20 static inline void arm_smmu_domain_free(struct arm_smmu_domain *smmu_domai= n) { + /* No concurrency with invalidation is possible at this point */ + kfree(rcu_dereference_protected(smmu_domain->invs, true)); kfree(smmu_domain); } =20 diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c b/drivers/iom= mu/arm/arm-smmu-v3/arm-smmu-v3-test.c index d2671bfd37981..a37a55480b3ff 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-test.c @@ -567,6 +567,98 @@ static void arm_smmu_v3_write_cd_test_sva_release(stru= ct kunit *test) NUM_EXPECTED_SYNCS(2)); } =20 +static void arm_smmu_v3_invs_test_verify(struct kunit *test, + struct arm_smmu_invs *invs, int num, + const int *ids, const int *users) +{ + KUNIT_EXPECT_EQ(test, invs->num_invs, num); + while (num--) { + KUNIT_EXPECT_EQ(test, invs->inv[num].id, ids[num]); + KUNIT_EXPECT_EQ(test, refcount_read(&invs->inv[num].users), + users[num]); + } +} + +static struct arm_smmu_invs invs1 =3D { + .num_invs =3D 3, + .inv =3D { { .type =3D INV_TYPE_S2_VMID, .id =3D 1, }, + { .type =3D INV_TYPE_S2_VMID, .id =3D 2, }, + { .type =3D INV_TYPE_S2_VMID, .id =3D 3, }, }, +}; + +static struct arm_smmu_invs invs2 =3D { + .num_invs =3D 3, + .inv =3D { { .type =3D INV_TYPE_S2_VMID, .id =3D 1, }, /* duplicated */ + { .type =3D INV_TYPE_ATS, .id =3D 4, }, + { .type =3D INV_TYPE_ATS, .id =3D 5, }, }, +}; + +static struct arm_smmu_invs invs3 =3D { + .num_invs =3D 3, + .inv =3D { { .type =3D INV_TYPE_S2_VMID, .id =3D 1, }, /* duplicated */ + { .type =3D INV_TYPE_ATS, .id =3D 5, }, /* recover a trash */ + { .type =3D INV_TYPE_ATS, .id =3D 6, }, }, +}; + +static void arm_smmu_v3_invs_test(struct kunit *test) +{ + const int results1[2][3] =3D { { 1, 2, 3, }, { 1, 1, 1, }, }; + const int results2[2][5] =3D { { 1, 2, 3, 4, 5, }, { 2, 1, 1, 1, 1, }, }; + const int results3[2][3] =3D { { 1, 2, 3, }, { 1, 1, 1, }, }; + const int results4[2][5] =3D { { 1, 2, 3, 5, 6, }, { 2, 1, 1, 1, 1, }, }; + const int results5[2][5] =3D { { 1, 2, 3, 5, 6, }, { 1, 0, 0, 1, 1, }, }; + const int results6[2][3] =3D { { 1, 5, 6, }, { 1, 1, 1, }, }; + struct arm_smmu_invs *test_a, *test_b; + size_t num_trashes; + + /* New array */ + test_a =3D arm_smmu_invs_alloc(0); + KUNIT_EXPECT_EQ(test, test_a->num_invs, 0); + + /* Test1: merge invs1 (new array) */ + test_b =3D arm_smmu_invs_merge(test_a, &invs1); + kfree(test_a); + arm_smmu_v3_invs_test_verify(test, test_b, ARRAY_SIZE(results1[0]), + results1[0], results1[1]); + + /* Test2: merge invs2 (new array) */ + test_a =3D arm_smmu_invs_merge(test_b, &invs2); + kfree(test_b); + arm_smmu_v3_invs_test_verify(test, test_a, ARRAY_SIZE(results2[0]), + results2[0], results2[1]); + + /* Test3: unref invs2 (same array) */ + num_trashes =3D arm_smmu_invs_unref(test_a, &invs2, NULL); + arm_smmu_v3_invs_test_verify(test, test_a, ARRAY_SIZE(results3[0]), + results3[0], results3[1]); + KUNIT_EXPECT_EQ(test, num_trashes, 0); + + /* Test4: merge invs3 (new array) */ + test_b =3D arm_smmu_invs_merge(test_a, &invs3); + kfree(test_a); + arm_smmu_v3_invs_test_verify(test, test_b, ARRAY_SIZE(results4[0]), + results4[0], results4[1]); + + /* Test5: unref invs1 (same array) */ + num_trashes =3D arm_smmu_invs_unref(test_b, &invs1, NULL); + arm_smmu_v3_invs_test_verify(test, test_b, ARRAY_SIZE(results5[0]), + results5[0], results5[1]); + KUNIT_EXPECT_EQ(test, num_trashes, 2); + + /* Test6: purge test_b (new array) */ + test_a =3D arm_smmu_invs_purge(test_b, num_trashes); + kfree(test_b); + arm_smmu_v3_invs_test_verify(test, test_a, ARRAY_SIZE(results6[0]), + results6[0], results6[1]); + + /* Test7: unref invs3 (same array) */ + num_trashes =3D arm_smmu_invs_unref(test_a, &invs3, NULL); + KUNIT_EXPECT_EQ(test, test_a->num_invs, 0); + KUNIT_EXPECT_EQ(test, num_trashes, 0); + + kfree(test_a); +} + static struct kunit_case arm_smmu_v3_test_cases[] =3D { KUNIT_CASE(arm_smmu_v3_write_ste_test_bypass_to_abort), KUNIT_CASE(arm_smmu_v3_write_ste_test_abort_to_bypass), @@ -590,6 +682,7 @@ static struct kunit_case arm_smmu_v3_test_cases[] =3D { KUNIT_CASE(arm_smmu_v3_write_ste_test_s2_to_s1_stall), KUNIT_CASE(arm_smmu_v3_write_cd_test_sva_clear), KUNIT_CASE(arm_smmu_v3_write_cd_test_sva_release), + KUNIT_CASE(arm_smmu_v3_invs_test), {}, }; =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 00d43080efaa8..8266d0839a927 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -1015,6 +1016,245 @@ static void arm_smmu_page_response(struct device *d= ev, struct iopf_fault *unused */ } =20 +/* Invalidation array manipulation functions */ +static int arm_smmu_inv_cmp(const struct arm_smmu_inv *l, + const struct arm_smmu_inv *r) +{ + if (l->smmu !=3D r->smmu) + return cmp_int((uintptr_t)l->smmu, (uintptr_t)r->smmu); + if (l->type !=3D r->type) + return cmp_int(l->type, r->type); + return cmp_int(l->id, r->id); +} + +/* + * Compare of two sorted arrays items. If one side is past the end of the = array, + * return the other side to let it run out the iteration. + */ +static inline int arm_smmu_invs_cmp(const struct arm_smmu_invs *l, size_t = l_idx, + const struct arm_smmu_invs *r, size_t r_idx) +{ + if (l_idx < l->num_invs && r_idx < r->num_invs) + return arm_smmu_inv_cmp(&l->inv[l_idx], &r->inv[r_idx]); + if (l_idx < l->num_invs) + return -1; + return 1; +} + +/** + * arm_smmu_invs_merge() - Merge @to_merge into @invs and generate a new a= rray + * @invs: the base invalidation array + * @to_merge: an array of invlidations to merge + * + * Return: a newly allocated array on success, or ERR_PTR + * + * This function must be locked and serialized with arm_smmu_invs_unref() = and + * arm_smmu_invs_purge(), but do not lockdep on any lock for KUNIT test. + * + * Both @invs and @to_merge must be sorted, to ensure the returned array w= ill be + * sorted as well. + * + * Caller is resposible for freeing the @invs and the returned new one. + * + * Entries marked as trash will be purged in the returned array. + */ +VISIBLE_IF_KUNIT +struct arm_smmu_invs *arm_smmu_invs_merge(struct arm_smmu_invs *invs, + struct arm_smmu_invs *to_merge) +{ + struct arm_smmu_invs *new_invs; + struct arm_smmu_inv *new; + size_t num_trashes =3D 0; + size_t num_adds =3D 0; + size_t i, j; + + for (i =3D j =3D 0; i < invs->num_invs || j < to_merge->num_invs;) { + int cmp; + + /* Skip any trash entry */ + if (i < invs->num_invs && !refcount_read(&invs->inv[i].users)) { + num_trashes++; + i++; + continue; + } + + cmp =3D arm_smmu_invs_cmp(invs, i, to_merge, j); + if (cmp < 0) { + /* not found in to_merge, leave alone */ + i++; + } else if (cmp =3D=3D 0) { + /* same item */ + i++; + j++; + } else { + /* unique to to_merge */ + num_adds++; + j++; + } + } + + new_invs =3D arm_smmu_invs_alloc(invs->num_invs - num_trashes + num_adds); + if (IS_ERR(new_invs)) + return new_invs; + + new =3D new_invs->inv; + for (i =3D j =3D 0; i < invs->num_invs || j < to_merge->num_invs;) { + int cmp; + + if (i < invs->num_invs && !refcount_read(&invs->inv[i].users)) { + i++; + continue; + } + + cmp =3D arm_smmu_invs_cmp(invs, i, to_merge, j); + if (cmp < 0) { + *new =3D invs->inv[i]; + i++; + } else if (cmp =3D=3D 0) { + *new =3D invs->inv[i]; + refcount_inc(&new->users); + i++; + j++; + } else { + *new =3D to_merge->inv[j]; + refcount_set(&new->users, 1); + j++; + } + + /* + * Check that the new array is sorted. This also validates that + * to_merge is sorted. + */ + if (new !=3D new_invs->inv) + WARN_ON_ONCE(arm_smmu_inv_cmp(new - 1, new) =3D=3D 1); + new++; + } + + WARN_ON(new !=3D new_invs->inv + new_invs->num_invs); + + return new_invs; +} +EXPORT_SYMBOL_IF_KUNIT(arm_smmu_invs_merge); + +/** + * arm_smmu_invs_unref() - Find in @invs for all entries in @to_unref, dec= rease + * the user counts without deletions + * @invs: the base invalidation array + * @to_unref: an array of invlidations to decrease their user counts + * @flush_fn: A callback function to invoke, when an entry's user count re= duces + * to 0 + * + * Return: the number of trash entries in the array, for arm_smmu_invs_pur= ge() + * + * This function will not fail. Any entry with users=3D0 will be marked as= trash. + * All tailing trash entries in the array will be dropped. And the size of= the + * array will be trimmed properly. All trash entries in-between will remai= n in + * the @invs until being completely deleted by the next arm_smmu_invs_merg= e() + * or an arm_smmu_invs_purge() function call. + * + * This function must be locked and serialized with arm_smmu_invs_merge() = and + * arm_smmu_invs_purge(), but do not lockdep on any mutex for KUNIT test. + * + * Note that the final @invs->num_invs might not reflect the actual number= of + * invalidations due to trash entries. Any reader should take the read loc= k to + * iterate each entry and check its users counter till the last entry. + */ +VISIBLE_IF_KUNIT +size_t arm_smmu_invs_unref(struct arm_smmu_invs *invs, + struct arm_smmu_invs *to_unref, + void (*flush_fn)(struct arm_smmu_inv *inv)) +{ + unsigned long flags; + size_t num_trashes =3D 0; + size_t num_invs =3D 0; + size_t i, j; + + for (i =3D j =3D 0; i < invs->num_invs || j < to_unref->num_invs;) { + int cmp; + + /* Skip any trash entry */ + if (i < invs->num_invs && !refcount_read(&invs->inv[i].users)) { + num_trashes++; + i++; + continue; + } + + cmp =3D arm_smmu_invs_cmp(invs, i, to_unref, j); + if (cmp < 0) { + /* not found in to_unref, leave alone */ + i++; + num_invs =3D i; + } else if (cmp =3D=3D 0) { + /* same item */ + if (refcount_dec_and_test(&invs->inv[i].users)) { + /* KUNIT test doesn't pass in a flush_fn */ + if (flush_fn) + flush_fn(&invs->inv[i]); + num_trashes++; + } else { + num_invs =3D i + 1; + } + i++; + j++; + } else { + /* item in to_unref is not in invs or already a trash */ + WARN_ON(true); + j++; + } + } + + /* Exclude any tailing trash */ + num_trashes -=3D invs->num_invs - num_invs; + + /* The lock is required to fence concurrent ATS operations. */ + write_lock_irqsave(&invs->rwlock, flags); + WRITE_ONCE(invs->num_invs, num_invs); /* Remove tailing trash entries */ + write_unlock_irqrestore(&invs->rwlock, flags); + + return num_trashes; +} +EXPORT_SYMBOL_IF_KUNIT(arm_smmu_invs_unref); + +/** + * arm_smmu_invs_purge() - Purge all the trash entries in the @invs + * @invs: the base invalidation array + * @num_trashes: expected number of trash entries, typically returned by a= prior + * arm_smmu_invs_unref() call + * + * Return: a newly allocated array on success removing all the trash entri= es, or + * NULL on failure + * + * This function must be locked and serialized with arm_smmu_invs_merge() = and + * arm_smmu_invs_unref(), but do not lockdep on any lock for KUNIT test. + * + * Caller is resposible for freeing the @invs and the returned new one. + */ +VISIBLE_IF_KUNIT +struct arm_smmu_invs *arm_smmu_invs_purge(struct arm_smmu_invs *invs, + size_t num_trashes) +{ + struct arm_smmu_invs *new_invs; + size_t i, j; + + if (WARN_ON(invs->num_invs < num_trashes)) + return NULL; + + new_invs =3D arm_smmu_invs_alloc(invs->num_invs - num_trashes); + if (IS_ERR(new_invs)) + return NULL; + + for (i =3D j =3D 0; i < invs->num_invs; i++) { + if (!refcount_read(&invs->inv[i].users)) + continue; + new_invs->inv[j] =3D invs->inv[i]; + j++; + } + + WARN_ON(j !=3D new_invs->num_invs); + return new_invs; +} +EXPORT_SYMBOL_IF_KUNIT(arm_smmu_invs_purge); + /* Context descriptor manipulation functions */ void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid) { @@ -2462,13 +2702,21 @@ static bool arm_smmu_enforce_cache_coherency(struct= iommu_domain *domain) struct arm_smmu_domain *arm_smmu_domain_alloc(void) { struct arm_smmu_domain *smmu_domain; + struct arm_smmu_invs *new_invs; =20 smmu_domain =3D kzalloc(sizeof(*smmu_domain), GFP_KERNEL); if (!smmu_domain) return ERR_PTR(-ENOMEM); =20 + new_invs =3D arm_smmu_invs_alloc(0); + if (IS_ERR(new_invs)) { + kfree(smmu_domain); + return ERR_CAST(new_invs); + } + INIT_LIST_HEAD(&smmu_domain->devices); spin_lock_init(&smmu_domain->devices_lock); + rcu_assign_pointer(smmu_domain->invs, new_invs); =20 return smmu_domain; } --=20 2.43.0 From nobody Fri Dec 19 21:45:36 2025 Received: from DM1PR04CU001.outbound.protection.outlook.com (mail-centralusazon11010017.outbound.protection.outlook.com [52.101.61.17]) (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 3278A2E22A3 for ; Sat, 8 Nov 2025 08:08:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.61.17 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762589323; cv=fail; b=rxwQc1kal8zVzSIzqnjB0/499Tz6QerQZvSxjzuJ4LFHvgiuoiGcH+8Cx5QrbzdND1OcKSwelvz38/W2iTyNZ+rSkBH5O14ysGdjOIkMBarscAXFTnlpKe1vepaT/O3hlkwmFPNa6pwlTW9MALmoXQaqruzKBaqBEYPUZQC++jo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762589323; c=relaxed/simple; bh=UAlgo1Wq0Qsp0y4d5WYTyFrY0TqBuR2JQh13F86Afdk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=nr6PHNmSdFSlSveDijEdSAbxZpOIVXeMWWO2ItqX+/RbnqSYa5LQu1hlizkCScx/U6HpdZhJvMpxITOcOUhZo26l1sKQfuNO6wvlXxNmly8mKKOjOyF4nlwtbsDhdmPYmYdNNfpLqbmjpr29n1wszHlogFyAPa+zAZ/Hi/fUESc= 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=m7SBb/VJ; arc=fail smtp.client-ip=52.101.61.17 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="m7SBb/VJ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=HzJQQWTa0xYLOCtofu/VZiM1JoUj8ZzwsLXF2ZClJ6KfdLbdyqDfBkeRhSGg3KEt6K0Lmld02nMWe57CwauL1raQoaZS6Fd9MIL7aVcJdmHaKKGH4kXnFfDP1rCPfvwpmzmk/5FOKUD0u048WMOQxHDh5yCAu0s2F4khDxGMoyL8KO3F5obCULbdxuhtef3t65+tFX6rr6rtElYL/gK9XEJLTUUwbRn/lc9M4nN5dmgTsmWj+nwIp4QUDJAPPolGcjRRYW0MQ3qB3Sn0mMN8LUQUUZtP6sxlF82Lg64lvNj8LjOmcBAQUX9W6WoLoVXH3p9TtXjl9vG2U+OJZz5qew== 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=wA7clLHnhSUvZ6fwm9sqhTd4FwmpY4o09XO41gupfjg=; b=FgCuK0dQFufHyhwkmptS59bEZf0lvtS1aqhnFV/3f0nF1D7qVXjAtP6Ht0VgIBZ1ZDnEfZHW68ZPwzmpWlhdgjCasZsB1D7nGiNoPS/CsRRrBnHe5IMCMHZGjWTJMqQenYHvynhHoB8wYpPtu5BDDR9Bgal5qqPBBMafN0JkxpBGwSjpImbIaO6rOR0PunqrXXfxlWQTYcMPhhgkOIUXICPvL+XCH/20k2HxQR/3blQN+0sKCjgNY31SEevdClknM49m8j57OiWNdMoSLdxkv4qugBdJOgWfn2V35jEKnNlzDxPK56SA5It+Uic4G3Zbm8iJfV5xRAxxA6Uh3UKtOg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) 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=wA7clLHnhSUvZ6fwm9sqhTd4FwmpY4o09XO41gupfjg=; b=m7SBb/VJQVkLuUmK+SUwWmhx/ju89uHzmua4T2p1v36Lyw7LnsE7faQNSWepGVJjU7ElU+7CHF9HoufI2HVdI0PtfGKYnd0b6ZADaD4ff/EI5aM5uzncQxI+9z4J4UVNsaoyJBgIe09gPU1CLgbKnaZW8GGzHkFDiIEUET27yolW8dBhFoe6JDNE3dwKBaaSaG1BiNk4qU77G8l6wJYznJVKvSRovBGhRWVfkZts0vBj3SGFx+KBZakSl5jox319HRDg4VLCehKbvUhjTuSF/UkrPSd8yXJUBQteZ2Fvibv0+TI/Lxm/Qw9nP1SEJFsaj8RCM+YHScxh398OJOD3vA== Received: from MN2PR14CA0009.namprd14.prod.outlook.com (2603:10b6:208:23e::14) by LV8PR12MB9715.namprd12.prod.outlook.com (2603:10b6:408:2a0::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9298.13; Sat, 8 Nov 2025 08:08:36 +0000 Received: from MN1PEPF0000F0DE.namprd04.prod.outlook.com (2603:10b6:208:23e:cafe::14) by MN2PR14CA0009.outlook.office365.com (2603:10b6:208:23e::14) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9298.14 via Frontend Transport; Sat, 8 Nov 2025 08:08:12 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) 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.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by MN1PEPF0000F0DE.mail.protection.outlook.com (10.167.242.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9320.13 via Frontend Transport; Sat, 8 Nov 2025 08:08:36 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 8 Nov 2025 00:08:22 -0800 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 8 Nov 2025 00:08:21 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Sat, 8 Nov 2025 00:08:21 -0800 From: Nicolin Chen To: CC: , , , , , , , , , , , Subject: [PATCH v5 4/7] iommu/arm-smmu-v3: Pre-allocate a per-master invalidation array Date: Sat, 8 Nov 2025 00:08:05 -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: MN1PEPF0000F0DE:EE_|LV8PR12MB9715:EE_ X-MS-Office365-Filtering-Correlation-Id: 31d7729b-2dd7-4f60-1aff-08de1e9e048b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|36860700013|7416014|376014|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?5rKbvqDbuXny1Wk06p6gi5XtqV4oS6UF100bA8Puxcf5/PIdWEtJAUb+oXv+?= =?us-ascii?Q?til1n9xDqK4Ik5NQzsa/51ahdnlUY7eR4522MavgVvbDcH1HBz+Aa0VkJGe3?= =?us-ascii?Q?2SmucIG8t1vkfLztwjETh38loBJ1f5R2sq0RZW1mye2yL97ktNHCHKMJoHuN?= =?us-ascii?Q?Fx68x8mzxnF1nDXzMFPdvk6pJM6FlfosXXzcGA0vtWCyCLz77FPN+iaE+EpE?= =?us-ascii?Q?92DsP/DOQPKLNtP2j2Xf9Ybo2siyOtTnOwf+aN5Wtbb3n3BTS7byMtUKM5h9?= =?us-ascii?Q?kYke1CDNOf9vmMG5jABjS+XT/h58fJW2bBY3OTwfW/pLQwl+3olyu4QoKUUw?= =?us-ascii?Q?Cgk5AfEm2GjjvIq6LIknjrO/ahOGcqO0ig7oz+WomNl469Xr1SmlBy+r0Vsa?= =?us-ascii?Q?ZI1Ph1iTy5CpFVWYpq9kwzWAc8OV+n+oSSnD+DfNkHq5Gnl+k3bOEcD52u1Y?= =?us-ascii?Q?AEqaUPN+0OXHbM0nSG+biBInWDBGE7TI9txNzlpfoRx3pZctpPhFeZzfeew4?= =?us-ascii?Q?OZleupJW7ufNAXb7NVlg3UV5am7Wao9a8S1ECZ3fEQaZoylzUJ5LI5cI6Ve8?= =?us-ascii?Q?3MFRLaP/y6feTlcugvth87gSGuAXtP/Vu1CZ0DWl34X8irvMxRLncrq7rNqV?= =?us-ascii?Q?SupkEmoa2CEojR4LoBPtpm2yF2v62EuE2vEG7VeX07exrSfx9iiGeQ9BWU01?= =?us-ascii?Q?ENa8pp8baJTmf+kJKMPq/Z5i/BHJkTATa/1b2tSXLASw5IeDOpAuUVgDxyD0?= =?us-ascii?Q?FYr4cA6KjQvjEhdNEy3itytce5jUMEsC0iAz6OZYdxp1V+gVohUUvl7lUVaB?= =?us-ascii?Q?gd/cYYjZl9uvCW0cA9YJf/Qwp+jcnjlcX5S+Z9DghHICsLHQH2LpiJ5lq6l3?= =?us-ascii?Q?FpeSY+v/jJT07BAQUBxMXscIZ/761iwlOYBwoBy9gLgValutklzrXoq0Bi8o?= =?us-ascii?Q?22e6bdob3vrlnjGN3M8PYzjX/H2yyZGVhjZHipwERBSE92G0Tqjm6AyMYAEg?= =?us-ascii?Q?8dNmmDkaypG+Q3lCaO2J9kUSxQ93fyvmB83hEAP9ZAylhX4P+oD2Cj9xS5DQ?= =?us-ascii?Q?jF12B8sqEEq+3VFbqHrfTnL5YZ6DXyXvegMpv0M+nZXNOllZHYUeIPNp2Pnv?= =?us-ascii?Q?HTfLhFPxH9p4u0dZU0ZC8qAMEG6daBPpIFTmPZfjLsczMhHtrsmRKgwDhU07?= =?us-ascii?Q?P4E2sIFL6py4cToybyJNXfALHTPGmpfryoF0uKQUQAhVUT38QBF64IsewxQ4?= =?us-ascii?Q?Wrry1WaYEIUDiZg9sOpSvFtJ8fER0YDZkj5MxyWWCWg0Gl4mLs6lRmIwNxBn?= =?us-ascii?Q?M9t20OOHR9o/hDcIyyKQs5SIQR7F23Mm07cCU2rE8w6Ndu2BFetElUkbk0/v?= =?us-ascii?Q?MQ5QqUWku27tqSsgwWPLRZePsE2rPalAb7RzvrbT/7n2pbrsjvN7f/aCf97f?= =?us-ascii?Q?hEjq/IYj/FOUL4qFUelDIRDsUhLKcDb4rnrMS7KLIbyH71hKug5wNrKyisM7?= =?us-ascii?Q?D1LcgWLyCqK3K5JMFRyJMpDp8QOwQE2lq9bdJC/955HJXZfA9gBYMynov6kf?= =?us-ascii?Q?w7o6MesNyWS/WYYw6lM=3D?= X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(82310400026)(36860700013)(7416014)(376014)(1800799024);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2025 08:08:36.0989 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 31d7729b-2dd7-4f60-1aff-08de1e9e048b X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: MN1PEPF0000F0DE.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV8PR12MB9715 Content-Type: text/plain; charset="utf-8" When a master is attached from an old domain to a new domain, it needs to build an invalidation array to delete and add the array entries from/onto the invalidation arrays of those two domains, passed via the to_merge and to_unref arguments into arm_smmu_invs_merge/unref() respectively. Since the master->num_streams might differ across masters, a memory would have to be allocated when building an to_merge/to_unref array which might fail with -ENOMEM. On the other hand, an attachment to arm_smmu_blocked_domain must not fail so it's the best to avoid any memory allocation in that path. Pre-allocate a fixed size invalidation array for every master. This array will be used as a scratch to fill dynamically when building a to_merge or to_unref invs array. Sort fwspec->ids in an ascending order to fit to the arm_smmu_invs_merge() function. 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 | 8 ++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 27 +++++++++++++++++++++ 2 files changed, 35 insertions(+) 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 757158b9ea655..7b81a82c0dfe4 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -922,6 +922,14 @@ struct arm_smmu_master { struct arm_smmu_device *smmu; struct device *dev; struct arm_smmu_stream *streams; + /* + * Scratch memory for a to_merge or to_unref array to build a per-domain + * invalidation array. It'll be pre-allocated with enough enries for all + * possible build scenarios. It can be used by only one caller at a time + * until the arm_smmu_invs_merge/unref() finishes. Must be locked by the + * iommu_group mutex. + */ + struct arm_smmu_invs *build_invs; struct arm_smmu_vmaster *vmaster; /* use smmu->streams_mutex */ /* Locked by the iommu core using the group mutex */ struct arm_smmu_ctx_desc_cfg cd_table; 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 8266d0839a927..26b8492a13f20 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3693,12 +3693,22 @@ static int arm_smmu_init_sid_strtab(struct arm_smmu= _device *smmu, u32 sid) return 0; } =20 +static int arm_smmu_ids_cmp(const void *_l, const void *_r) +{ + const typeof_member(struct iommu_fwspec, ids[0]) *l =3D _l; + const typeof_member(struct iommu_fwspec, ids[0]) *r =3D _r; + + return cmp_int(*l, *r); +} + static int arm_smmu_insert_master(struct arm_smmu_device *smmu, struct arm_smmu_master *master) { int i; int ret =3D 0; struct iommu_fwspec *fwspec =3D dev_iommu_fwspec_get(master->dev); + bool ats_supported =3D dev_is_pci(master->dev) && + pci_ats_supported(to_pci_dev(master->dev)); =20 master->streams =3D kcalloc(fwspec->num_ids, sizeof(*master->streams), GFP_KERNEL); @@ -3706,6 +3716,21 @@ static int arm_smmu_insert_master(struct arm_smmu_de= vice *smmu, return -ENOMEM; master->num_streams =3D fwspec->num_ids; =20 + if (!ats_supported) { + /* Base case has 1 ASID entry or maximum 2 VMID entries */ + master->build_invs =3D arm_smmu_invs_alloc(2); + } else { + /* Put the ids into order for sorted to_merge/to_unref arrays */ + sort_nonatomic(fwspec->ids, fwspec->num_ids, + sizeof(fwspec->ids[0]), arm_smmu_ids_cmp, NULL); + /* ATS case adds num_ids of entries, on top of the base case */ + master->build_invs =3D arm_smmu_invs_alloc(2 + fwspec->num_ids); + } + if (IS_ERR(master->build_invs)) { + kfree(master->streams); + return PTR_ERR(master->build_invs); + } + mutex_lock(&smmu->streams_mutex); for (i =3D 0; i < fwspec->num_ids; i++) { struct arm_smmu_stream *new_stream =3D &master->streams[i]; @@ -3743,6 +3768,7 @@ static int arm_smmu_insert_master(struct arm_smmu_dev= ice *smmu, for (i--; i >=3D 0; i--) rb_erase(&master->streams[i].node, &smmu->streams); kfree(master->streams); + kfree(master->build_invs); } mutex_unlock(&smmu->streams_mutex); =20 @@ -3764,6 +3790,7 @@ static void arm_smmu_remove_master(struct arm_smmu_ma= ster *master) mutex_unlock(&smmu->streams_mutex); =20 kfree(master->streams); + kfree(master->build_invs); } =20 static struct iommu_device *arm_smmu_probe_device(struct device *dev) --=20 2.43.0 From nobody Fri Dec 19 21:45:36 2025 Received: from SN4PR2101CU001.outbound.protection.outlook.com (mail-southcentralusazon11012032.outbound.protection.outlook.com [40.93.195.32]) (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 2EC472D663D for ; Sat, 8 Nov 2025 08:08:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.93.195.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762589320; cv=fail; b=nhqhChK9ch4zsvCciwLmlyLRyMdJDRK/SgIXj90QnKNPZ2pEpErgCjBxL0jMvQfEAGAH1Mu6FY02Nel2hVCG4Nse2JtsxY82aJD3kYR5lor3FB86I5zGlV+HfsHDXrcVbWrbHsdbXP19JAPfcnmrnnhP/0Qc+S4k+uDRc2DLqt0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762589320; c=relaxed/simple; bh=1jiDumrLVko2oxt4yWBghC3O8VGwrSRJqdOHc1QzbFI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ESyk/JcODLi4WjmhHpzR8iDG6xf6twrFRvRQmBwxTx6N5P9wg9JzOHcjkp2uFii2hO8u/oteyIJB8kK354G7HhpKFPMwjcHFe7BnhCG589UHE1bV7sGFa74liHBEQuGvfzklXDaNnhEkMrV/VmWWWVE78rjdk8BamSNIiB4nFxE= 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=RYgQKV+Z; arc=fail smtp.client-ip=40.93.195.32 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="RYgQKV+Z" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=NVIOtc2VsFfRSN6lUC4l9AhfgiOSrsdcQXzNb2Fiwj7Kw07qp++NeREomrKlSBaP1A4Aq4dYDD5GI9EDlAhlo0gHJ7hfrlQFT7hK2bxw0p0fvUUm0jzLoLKtE6eYtA+DvDaNz3bPilYaQnK+tE93w7oxFIR7mpOa0sP7zdv7Xv/9f/BpMmt+zn0UOznwBP3RM5idIvMp3RWlmB2R0Cnvc55RGFI5c77n8NjO9xDR8tXKG53vr5ikqB/mzZLL3tv9/J//0DbK46VNKd1lMvnoUxEQgEXz9ut2THyiv7Mpy7rWv7kkHV0CnY9YXuII6uakMefsbne8pHgn2atKyDaFeA== 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=sZVRgVj3uSkvZFLSxeQdQTLxMboSYQ4zXKMA7kARAyU=; b=HIx35NwYSjA/+KKeJ+tTEmASJbZeB8kD28m5BM8vKMkOWhvIxbtqiM+w82MzN5apapiRR/hckDDCtsT2RJElrROugdEPEbWqEDg2e+zx7Zkf3IrFLCESilxbrx97mhcMudGkDWVXMtcblYMO85u8NnZnXB9JXE1BPSakasYdHgMLOanJaYvyqO96vFx21WrzVq/B/GSIlsXzjveEB7hU2+zgZ7cK0VTUOloWOrXY0qkSpgh1IHb2RAwymU08i+8AORGnVPUJlDnF3XIQ7Q5ORNuod6BD+zZU49cZdTPFu1rrkhshPNl9yUwjMIJP4//mZi1WQwxmm0Ae2FqVdHSNrQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) 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=sZVRgVj3uSkvZFLSxeQdQTLxMboSYQ4zXKMA7kARAyU=; b=RYgQKV+ZEmJoZAl4C+h+tUF/+i5XWRWiQFEEWbMrTuPDnKB6Ipvd3+Vvp6GQg0d4EzOB2j15jB4egdGHVOsNRHHX90fm0Ny1eucpiUfB+3n9OgKwigZvTj+lOD8Whc37MI9mOGjlATI20xSp6PgStvHiG7XrtI4AbaJgl4j/AHMzZb72h56OVqUOuNBB7DPVFaiEqxyYd1Bi9KZdr9epLRv554QyD9h4rFaek0kya1tiV5q9/uhOFQb2a9MujlAHY+wH0omGtPrO6SLW6gUD6eyI1cNPh8uYLurL5qbz+OB8fXVSdCR5hsyRstknNTZLNqNoHqtYUxQ1432HOboRxw== Received: from MW4PR04CA0097.namprd04.prod.outlook.com (2603:10b6:303:83::12) by PH7PR12MB9104.namprd12.prod.outlook.com (2603:10b6:510:2f3::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9298.13; Sat, 8 Nov 2025 08:08:31 +0000 Received: from SJ1PEPF00002315.namprd03.prod.outlook.com (2603:10b6:303:83:cafe::ad) by MW4PR04CA0097.outlook.office365.com (2603:10b6:303:83::12) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9298.13 via Frontend Transport; Sat, 8 Nov 2025 08:08:27 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) 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.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by SJ1PEPF00002315.mail.protection.outlook.com (10.167.242.169) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9320.13 via Frontend Transport; Sat, 8 Nov 2025 08:08:31 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 8 Nov 2025 00:08:23 -0800 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 8 Nov 2025 00:08:22 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Sat, 8 Nov 2025 00:08:22 -0800 From: Nicolin Chen To: CC: , , , , , , , , , , , Subject: [PATCH v5 5/7] iommu/arm-smmu-v3: Populate smmu_domain->invs when attaching masters Date: Sat, 8 Nov 2025 00:08:06 -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: SJ1PEPF00002315:EE_|PH7PR12MB9104:EE_ X-MS-Office365-Filtering-Correlation-Id: 9054e927-eed9-4d94-3daf-08de1e9e01a4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|7416014|82310400026|36860700013; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?DqnPYcXiY2/TM32V4xcyea7x6YMkhVDHxawT6vDmlKbmiSijlQPv4jlyq/Hd?= =?us-ascii?Q?qlqEdomhJOwPh7BT/Baq7VCW34ABWPDnTabxLgOWeIRyQlgtG96n/m4ipkWX?= =?us-ascii?Q?pwAg3+07CJKKMB2k1Go6hnKPcmZIl1nOjEeGh9l+KWGQVBmfhpnX1zsQVgFa?= =?us-ascii?Q?Q+1whuOaBgh4a3Uz17ADPdcBme/4JnrKZ1UkQ6cGt5/HAEdrmVEU79JP8XUD?= =?us-ascii?Q?yi7J/7U5mMswAxhnacJ17VmasbUGpdya/doSlwUwnOki+e2kRx6Bpz2aRDOv?= =?us-ascii?Q?BNZrZcqWn0iDkvOEl/AB3oIsGywvn8/22qfuBj0stlZNPoDTIm4Sy4bJxLUj?= =?us-ascii?Q?fZdlomurne0k0LUmUkYnm/nxn7G8YqdciAX2UqFaogSg13L9qTXponr6gw8G?= =?us-ascii?Q?3bMPolIdvRuXZFAhhH3lbioNdtlKovA3bQnQdcGPl5BATa9i0W3Nneq2p6m2?= =?us-ascii?Q?VVzSdqVtsqLRv9SSKcA67n+EUlmauuoif28zO8thZ7cQQ1gFGXqRJAV4YZ6y?= =?us-ascii?Q?Pnqh5mjHO8QnMwGR0spOLCv71hrMb65iKlzCO5/A2D3t4tW/sI8PDbCfqMHW?= =?us-ascii?Q?ztq2YXXZl87ZgH5timY766TNTwuiSPCqYSax/IaR33Tn5lZEMUrWS3J+c4+s?= =?us-ascii?Q?RJu4C3SdjI0GorzBMb6hvJ66bl6+LFhYYrxtaiAqwe39NxLLKuE7ZN7ewg3e?= =?us-ascii?Q?a48CmGd5BaMRtJFrqQCGnjPFLsM5qUXG8bAIfsSpgu/Wtak1IIReAkGuG8qb?= =?us-ascii?Q?5+lLgYYrh+L4z48kRR4JB3D/r/ogD9NI1rZuBnS/eqlKJOVpYWOwZoswnk+f?= =?us-ascii?Q?cC0Y8JuSvj13fBgvS2sEVvgzh4t/C9XCgmXJuVivpn3HS6QFlQKGkcwaCutG?= =?us-ascii?Q?TdfSwJgj5+XE2xg2yXAqqNOQyYby+4g14I0iBlsOVBwEeVEerX/vcPqJA8fa?= =?us-ascii?Q?wEVXaEdohbEbRHW3GDuJc3wLDLVOQ/ID8tn8JgWEkC/nGZbvFGvn3B4lhArh?= =?us-ascii?Q?mymISpV6Odu/6FaiTo5ePjVatGZoSzqbnQeeeCNdPRsIL2hDy49eSMGiuf9Q?= =?us-ascii?Q?9YEaLrrD2rC31arKn8kdoa7/eQe1wprDIZfE/9eF8ty3vw1r49YscKekmlEg?= =?us-ascii?Q?BUnmtbne3l2cHl2y52jybpc8pBo770rdAZ9JIwhdokkMF5Jdo2r54ugAE5Dz?= =?us-ascii?Q?BIuHzWx+B6/0y1TKiLAQZNQ6ZZTI4y2QEWCIF85OSD9nOPkMWggQUPhgvcdg?= =?us-ascii?Q?6P74Kjmubl1fzAD3xfGwseDABdRzvuxNbAxO6cFwY+UQlID9HgnbueOoJlyo?= =?us-ascii?Q?+J5wVp7ZIV+RXlM9yzPrYLVZthPBKC5sG4KlfN37OUZL5Ln9laddGTy+oyo4?= =?us-ascii?Q?gcHhfGqIdCuPMbjCReavU5oRbIh3Xm8KTJIVgIpdeUSmB5Pm7fS1KDw+Rinu?= =?us-ascii?Q?kKR3KR2lCrA4oKgViQtpiarPP+5SoOOA4UkSObwdhwyJCl9KNEKO5RYXzQGo?= =?us-ascii?Q?ax3upCSOa5GOMXhF+LAms/U931bMa9zqS4QF2EGNY6yEPzR+Z6Lh2yGbTdkP?= =?us-ascii?Q?qbHIGIBf+zSiSDlKVyw=3D?= X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230040)(1800799024)(376014)(7416014)(82310400026)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2025 08:08:31.4006 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9054e927-eed9-4d94-3daf-08de1e9e01a4 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00002315.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB9104 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 | 242 +++++++++++++++++++- 2 files changed, 258 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 7b81a82c0dfe4..5899429a514ab 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -1094,6 +1094,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 domin->= 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; @@ -1103,6 +1118,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 26b8492a13f20..22875a951488d 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3058,6 +3058,97 @@ static void arm_smmu_disable_iopf(struct arm_smmu_ma= ster *master, iopf_queue_remove_device(master->smmu->evtq.iopf, master->dev); } =20 +/* + * 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 e2h =3D master->smmu->features & ARM_SMMU_FEAT_E2H; + struct arm_smmu_invs *build_invs =3D master->build_invs; + const bool nesting =3D smmu_domain->nest_parent; + struct arm_smmu_inv *cur; + + iommu_group_mutex_assert(master->dev); + + cur =3D build_invs->inv; + + switch (smmu_domain->stage) { + case ARM_SMMU_DOMAIN_SVA: + case ARM_SMMU_DOMAIN_S1: + *cur =3D (struct arm_smmu_inv){ + .smmu =3D master->smmu, + .type =3D INV_TYPE_S1_ASID, + .id =3D smmu_domain->cd.asid, + .size_opcode =3D e2h ? CMDQ_OP_TLBI_EL2_VA : + CMDQ_OP_TLBI_NH_VA, + .nsize_opcode =3D e2h ? CMDQ_OP_TLBI_EL2_ASID : + CMDQ_OP_TLBI_NH_ASID + }; + break; + case ARM_SMMU_DOMAIN_S2: + *cur =3D (struct arm_smmu_inv){ + .smmu =3D master->smmu, + .type =3D INV_TYPE_S2_VMID, + .id =3D smmu_domain->s2_cfg.vmid, + .size_opcode =3D CMDQ_OP_TLBI_S2_IPA, + .nsize_opcode =3D CMDQ_OP_TLBI_S12_VMALL, + }; + break; + default: + WARN_ON(true); + return NULL; + } + + /* Range-based invalidation requires the leaf pgsize for calculation */ + if (master->smmu->features & ARM_SMMU_FEAT_RANGE_INV) + cur->pgsize =3D __ffs(smmu_domain->domain.pgsize_bitmap); + cur++; + + /* All the nested S1 ASIDs have to be flushed when S2 parent changes */ + if (nesting) { + *cur =3D (struct arm_smmu_inv){ + .smmu =3D master->smmu, + .type =3D INV_TYPE_S2_VMID_S1_CLEAR, + .id =3D smmu_domain->s2_cfg.vmid, + .size_opcode =3D CMDQ_OP_TLBI_NH_ALL, + .nsize_opcode =3D CMDQ_OP_TLBI_NH_ALL, + }; + cur++; + } + + if (ats_enabled) { + size_t i; + + for (i =3D 0; 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. + */ + *cur =3D (struct arm_smmu_inv){ + .smmu =3D master->smmu, + .type =3D nesting ? INV_TYPE_ATS_FULL : + INV_TYPE_ATS, + .id =3D master->streams[i].id, + .ssid =3D ssid, + .size_opcode =3D CMDQ_OP_ATC_INV, + .nsize_opcode =3D CMDQ_OP_ATC_INV, + }; + cur++; + } + } + + /* Note this build_invs must have been sorted */ + + build_invs->num_invs =3D cur - build_invs->inv; + return build_invs; +} + static void arm_smmu_remove_master_domain(struct arm_smmu_master *master, struct iommu_domain *domain, ioasid_t ssid) @@ -3087,6 +3178,146 @@ 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; + + /* A re-attach case doesn't need to update invs array */ + if (new_smmu_domain =3D=3D old_smmu_domain) + return 0; + + /* + * 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; + 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); + /* + * We are committed to updating the STE. Ensure the invalidation array + * is visable to concurrent map/unmap threads, and acquire any racying + * IOPTE updates. + */ + smp_mb(); + kfree_rcu(invst->old_invs, rcu); +} + +/* + * When an array entry's users count reaches zero, it means the ASID/VMID = is no + * longer being invalidated by map/unmap and must be cleaned. The rule is = that + * all ASIDs/VMIDs not in an invalidation array are left cleared in the IO= TLB. + */ +static void arm_smmu_invs_flush_iotlb_tags(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; + size_t num_trashes; + + lockdep_assert_held(&arm_smmu_asid_lock); + + if (!invst->invs_ptr) + return; + + num_trashes =3D arm_smmu_invs_unref(old_invs, invst->new_invs, + arm_smmu_invs_flush_iotlb_tags); + if (!num_trashes) + return; + + new_invs =3D arm_smmu_invs_purge(old_invs, num_trashes); + if (!new_invs) + return; + + rcu_assign_pointer(*invst->invs_ptr, new_invs); + /* + * We are committed to updating the STE. Ensure the invalidation array + * is visable to concurrent map/unmap threads, and acquire any racying + * IOPTE updates. + */ + smp_mb(); + kfree_rcu(old_invs, rcu); +} + /* * Start the sequence to attach a domain to a master. The sequence contain= s three * steps: @@ -3144,12 +3375,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); @@ -3197,6 +3432,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) { @@ -3216,6 +3453,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 @@ -3247,6 +3486,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 --=20 2.43.0 From nobody Fri Dec 19 21:45:36 2025 Received: from BYAPR05CU005.outbound.protection.outlook.com (mail-westusazon11010069.outbound.protection.outlook.com [52.101.85.69]) (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 DAEC92E5B08 for ; Sat, 8 Nov 2025 08:08:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.85.69 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762589327; cv=fail; b=J6P7/BBQQX8V2JfxDZyz6KYSuuZxigwmFCemOvA4zyeIksVqAXZ0TXscjlq7MDkYl+HqdReCRKprlKkaBO6HAVvgWbSJZQQO4TA6CKZylAnfrpAzfTeyS+zMbutv8HxB1nIvQqBAlmdQUJjLnoIBULZRgpgq4ql6ox3+DCgxh7U= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762589327; c=relaxed/simple; bh=K9mxEV/R02TT/W8b4vKH8AbtpY1o0jb1Fsb8R/YhIJE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=p0UUnE2ZQdUiktqMkqKun2+gSJ27C1zswd06xo66WW9I24PBASDseTUt2TiuBFXm9fr0Nbny8+yXaa7sDprFQuONnMcuooGIEMCbxa3vrxIf1i4ENThtD8Un6MY9jrQNBwIPU6bUfNLltCmth42NWpTHm5OrlLdvhC5wib3XMUM= 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=NM6WmNKV; arc=fail smtp.client-ip=52.101.85.69 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="NM6WmNKV" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=cH1+6HVUcVh0ZeI9G148tqLrlEzEvRZVl+OHKkvVw6d6QyaP0eoMDPJi72uWNagoKiCMzYfdaIdMTZcYjlWeT+hOzr8Yi7bK49hIn/QOzvksh9pfd4dRIpzXX1wD4Vh0UoTMXL7n1ReELuRphThFbJTbahaI667TVENQ76xTFAIrMHz0wesVPOFThuLonc2DBGVOH8B4skA6qeIo76WyXawOIUxTZwT45WgfYJccI/KjO/+u7jqivm0urtuuRjjRVbOkTgvuwOmbLvA8yg9JuiGB+kkIjdUATNDxX24NMDP450OvQJ9g8hdAcBy8ww0NXhbyE9LfYekNehtWYs79RQ== 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=vJpIC3tTG1L7fuNcEi9MLjosQFDoXonN1TFmD3+HiIM=; b=eG+/wTZ2aE090gBADwBHuZUOzdWnJVoMX8W/7BmoEORmLgNV4FVIAX+xP0ktkaBxFzF7K9YQKqwTk9MOmLXPAsg9C4pM0rrKcIue62LgaaQ6SC/Zf994gazgkl2s0UPBewPY3uwk/KzmAEJWyxeatXWT8ZY+jwHSSkZTRil/ms6Vpfe/3ISfq7IYBoMrSo5BLkJ5VKqhMnc6ceXH7cfRkaxMzezLMJYo9rwkTb+uwdCgJqZsiieL3P/3TEQuJ5kFwFX6q0l/PqgOyws0iku5wjNrDD76pQjoXt6XRNQm8ywIMqfm4cythoWCud70bXy4z3xKvvYuir1FxfL8eUloXQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) 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=vJpIC3tTG1L7fuNcEi9MLjosQFDoXonN1TFmD3+HiIM=; b=NM6WmNKV4xS06FLmZno61fsvDccC6gKo7ybDcDUnkb82BWD1xPawOKFQa++P1QTU6VpE8i7ZZcN0fgFxlKOTxhdCnz38lG9CsScebEk+jRLjxgKWU4+gSkf5XdyZbJDg6/Ee7khnppds2YjYrycvVLx4eIazsupdGujtEbMPf0yfK90AEt0aj97rQYYd+qvRSkcu6mnenMkrM9l1uU5Ee1H1iUM5ifyYPsK/+tHk3Jj6bf2dHIi1DtcoLg6FEio1Wcr3m7JcQHKD4q1RCguhEYXwc0OihDy+KThv36hcRzp5+VQNHOpvp7ZLBDFgu59gMCsbVGbi7HUYYhdyepGb0A== Received: from MN2PR14CA0002.namprd14.prod.outlook.com (2603:10b6:208:23e::7) by DS7PR12MB8204.namprd12.prod.outlook.com (2603:10b6:8:e1::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9298.13; Sat, 8 Nov 2025 08:08:38 +0000 Received: from MN1PEPF0000F0DE.namprd04.prod.outlook.com (2603:10b6:208:23e:cafe::51) by MN2PR14CA0002.outlook.office365.com (2603:10b6:208:23e::7) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9298.15 via Frontend Transport; Sat, 8 Nov 2025 08:08:07 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) 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.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by MN1PEPF0000F0DE.mail.protection.outlook.com (10.167.242.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9320.13 via Frontend Transport; Sat, 8 Nov 2025 08:08:37 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 8 Nov 2025 00:08:24 -0800 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 8 Nov 2025 00:08:24 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Sat, 8 Nov 2025 00:08:23 -0800 From: Nicolin Chen To: CC: , , , , , , , , , , , Subject: [PATCH v5 6/7] iommu/arm-smmu-v3: Add arm_smmu_invs based arm_smmu_domain_inv_range() Date: Sat, 8 Nov 2025 00:08:07 -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: MN1PEPF0000F0DE:EE_|DS7PR12MB8204:EE_ X-MS-Office365-Filtering-Correlation-Id: 26c809d9-0c85-4e5e-9107-08de1e9e0590 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|36860700013|376014|82310400026|7416014|3613699012; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?JsofqGou97eN9qWMnTfeL8vXplseEdoY6BKSjAgGew2HxBroaF9eeCDpbH9I?= =?us-ascii?Q?c3L2DyvPuuTdBUe3qSVqpaTE9gUp7nMFD1M7FAMPEq1ymm1HUem+hM8FW4NF?= =?us-ascii?Q?Hv1zUsO/YC+yl3jusAoDI2CLKGFoW2fW8Pylm4tA0c04ael2Sf8DJuXTd0mw?= =?us-ascii?Q?+lTXuXfynpG6q4RQofrRWFDsxx7zId5nGU9qBni9GadO5XI5uf8+vOMrMSpz?= =?us-ascii?Q?O7nf7X/yFhOilQscPpc1th587aa9B2+rqy8xbkl3hvxRHVoFeRMxQxgzt3J1?= =?us-ascii?Q?DsRAEm1rIRn4RRutkQDb/JzYfuqBp9s+dNVE1TVv7+yLvNZFXadYc65934Zd?= =?us-ascii?Q?Ovko+AT9UazQbOLVSI3NE6ffAXh2O3NfdTWjne1R50p+iJ5mflkA5SWiF5JD?= =?us-ascii?Q?YdYDuzuvan/Dt9zKy1Znia0tvm92w0BeRqpbT9MJWOzV2zR4SGvuDxTkoWoY?= =?us-ascii?Q?aifI969Okwi0bJQxcfhjnCkOyiBZCNlAkY/om3Y7xLA9R5OT8UC3SzoNeBIh?= =?us-ascii?Q?Ljn/Yo+wMjQm8M8H41o0tnJSbjjcg+5pmKaPcwaHYELtPp6bxlcHzakjNxM7?= =?us-ascii?Q?THoAPGfecO5debFxOPFZ/JBAL6rA2GV2Ki9AVF/8R/cmXShDPQM2tSEIH1Mh?= =?us-ascii?Q?RSUrDaw6NwlWgMb1vKGN8KoMdtTKuhc3p145He4bSoAd+8IlZh9M/bej/jEP?= =?us-ascii?Q?X1RyAK3Ic/vhofLRwZ7M/ymMZko09/4YlgxTqVI85tRwMpKKqBZRoUAK7JI4?= =?us-ascii?Q?/rT8T9Qukh44Mb2fArXr+/JpD8mEdQf9l9iudr2Wifg3UjjfD5vqeNbK97Im?= =?us-ascii?Q?VjjV9+XJ263QFulPWyCQl48s5dTRi43YShMSJ6shVdUTLj4/ebuMx1O5c+CU?= =?us-ascii?Q?5JxCKfRyi384eX8hePGvzFJ5GdGtDwaxeW9j9WNK2VFl+ir/sJVosSF8iDya?= =?us-ascii?Q?9zIgpsZhiEQhF+VRMUMV0JJqivjczctz8fjwo1tNnEQP9l4MuKSzPEiPgwfr?= =?us-ascii?Q?7r3ExL7+bg0N9qJpsiNIcJmabfFHBtbCMGSl3TAFxzYY0erwuRx3596jXhjw?= =?us-ascii?Q?uNnJnc8d9CxS4i6CBcbyYMjJ27Ubv1q9qcQHeXiSh/GLRzdHGuclkCj+S71P?= =?us-ascii?Q?HZJBucteXqXoTFGAYRy/+VF6Hpu59s39vuv0q0qZRc3rigQrSb8BKCjAsEzY?= =?us-ascii?Q?2GLlzTzitRv1ntTJY4zhiiotSta/2FAvVrBWcWlV4Itubi90l1F3bUttvdyG?= =?us-ascii?Q?Ju4XcpPgam5vud5A+NGQPPGrzkepOZd8/8I5+volhmGBYsN4hV1ShRyhZCM+?= =?us-ascii?Q?SnTdevvwl9RyX6L/8bUbozZQGgByBLHuT1wS8UHdriNj0wCQMlP3TKX3FFuI?= =?us-ascii?Q?fBob7LARovDdvds/eEBpdCqlx/A/SiZv/Jy2UCC0CSZGasaHMygDAOSQspHy?= =?us-ascii?Q?jSk2Tc89bv3nNRrTrbCoNu0V9i2o4miUviuWNcrAKPZChpxOszKjlpxlqvkn?= =?us-ascii?Q?SZ0SEhgrp4V5QlUvI5wtnUTy1cs2EAAC7xrKznw90ztUhuavIBQGjHw7CId/?= =?us-ascii?Q?uApE88ei/6uz9PBFOKTrkszcCU3slmUJYFeHxEyE?= X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(1800799024)(36860700013)(376014)(82310400026)(7416014)(3613699012);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2025 08:08:37.8364 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 26c809d9-0c85-4e5e-9107-08de1e9e0590 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: MN1PEPF0000F0DE.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB8204 Content-Type: text/plain; charset="utf-8" Each smmu_domain now has an arm_smmu_invs that specifies the invalidation steps to perform after any change the IOPTEs. This includes supports for basic ASID/VMID, the special case for nesting, and ATC invalidations. Introduce a new arm_smmu_domain_inv helper iterating smmu_domain->invs to convert the invalidation array to commands. Any invalidation request with no size specified means an entire flush over a range based one. Take advantage of the sorted array to compatible batch operations together to the same SMMU. For instance, ATC invaliations for multiple SIDs can be pushed as a batch. ATC invalidations must be completed before the driver disables ATS. Or the device is permitted to ignore any racing invalidation that would cause an SMMU timeout. The sequencing is done with a rwlock where holding the write side of the rwlock means that there are no outstanding ATC invalidations. If ATS is not used the rwlock is ignored, similar to the existing code. 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 | 9 + drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 224 ++++++++++++++++++-- 2 files changed, 220 insertions(+), 13 deletions(-) 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 5899429a514ab..593f94a699863 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -1079,6 +1079,15 @@ void arm_smmu_tlb_inv_range_asid(unsigned long iova,= size_t size, int asid, int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, unsigned long iova, size_t size); =20 +void arm_smmu_domain_inv_range(struct arm_smmu_domain *smmu_domain, + unsigned long iova, size_t size, + unsigned int granule, bool leaf); + +static inline void arm_smmu_domain_inv(struct arm_smmu_domain *smmu_domain) +{ + arm_smmu_domain_inv_range(smmu_domain, 0, 0, 0, false); +} + void __arm_smmu_cmdq_skip_err(struct arm_smmu_device *smmu, struct arm_smmu_cmdq *cmdq); int arm_smmu_init_one_queue(struct arm_smmu_device *smmu, 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 22875a951488d..9f4f4e95a3603 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2506,23 +2506,19 @@ static void arm_smmu_tlb_inv_context(void *cookie) arm_smmu_atc_inv_domain(smmu_domain, 0, 0); } =20 -static void __arm_smmu_tlb_inv_range(struct arm_smmu_cmdq_ent *cmd, - unsigned long iova, size_t size, - size_t granule, - struct arm_smmu_domain *smmu_domain) +static void arm_smmu_cmdq_batch_add_range(struct arm_smmu_device *smmu, + struct arm_smmu_cmdq_batch *cmds, + struct arm_smmu_cmdq_ent *cmd, + unsigned long iova, size_t size, + size_t granule, size_t pgsize) { - struct arm_smmu_device *smmu =3D smmu_domain->smmu; - unsigned long end =3D iova + size, num_pages =3D 0, tg =3D 0; + unsigned long end =3D iova + size, num_pages =3D 0, tg =3D pgsize; size_t inv_range =3D granule; - struct arm_smmu_cmdq_batch cmds; =20 if (!size) return; =20 if (smmu->features & ARM_SMMU_FEAT_RANGE_INV) { - /* Get the leaf page size */ - tg =3D __ffs(smmu_domain->domain.pgsize_bitmap); - num_pages =3D size >> tg; =20 /* Convert page size of 12,14,16 (log2) to 1,2,3 */ @@ -2542,8 +2538,6 @@ static void __arm_smmu_tlb_inv_range(struct arm_smmu_= cmdq_ent *cmd, num_pages++; } =20 - arm_smmu_cmdq_batch_init(smmu, &cmds, cmd); - while (iova < end) { if (smmu->features & ARM_SMMU_FEAT_RANGE_INV) { /* @@ -2571,9 +2565,26 @@ static void __arm_smmu_tlb_inv_range(struct arm_smmu= _cmdq_ent *cmd, } =20 cmd->tlbi.addr =3D iova; - arm_smmu_cmdq_batch_add(smmu, &cmds, cmd); + arm_smmu_cmdq_batch_add(smmu, cmds, cmd); iova +=3D inv_range; } +} + +static void __arm_smmu_tlb_inv_range(struct arm_smmu_cmdq_ent *cmd, + unsigned long iova, size_t size, + size_t granule, + struct arm_smmu_domain *smmu_domain) +{ + struct arm_smmu_device *smmu =3D smmu_domain->smmu; + struct arm_smmu_cmdq_batch cmds; + size_t pgsize; + + /* Get the leaf page size */ + pgsize =3D __ffs(smmu_domain->domain.pgsize_bitmap); + + arm_smmu_cmdq_batch_init(smmu, &cmds, cmd); + arm_smmu_cmdq_batch_add_range(smmu, &cmds, cmd, iova, size, granule, + pgsize); arm_smmu_cmdq_batch_submit(smmu, &cmds); } =20 @@ -2629,6 +2640,193 @@ void arm_smmu_tlb_inv_range_asid(unsigned long iova= , size_t size, int asid, __arm_smmu_tlb_inv_range(&cmd, iova, size, granule, smmu_domain); } =20 +static bool arm_smmu_inv_size_too_big(struct arm_smmu_device *smmu, size_t= size, + size_t granule) +{ + size_t max_tlbi_ops; + + /* 0 size means invalidate all */ + if (!size || size =3D=3D SIZE_MAX) + return true; + + if (smmu->features & ARM_SMMU_FEAT_RANGE_INV) + return false; + + /* + * Borrowed from the MAX_TLBI_OPS in arch/arm64/include/asm/tlbflush.h, + * this is used as a threshold to replace "size_opcode" commands with a + * single "nsize_opcode" command, when SMMU doesn't implement the range + * invalidation feature, where there can be too many per-granule TLBIs, + * resulting in a soft lockup. + */ + max_tlbi_ops =3D 1 << (ilog2(granule) - 3); + return size >=3D max_tlbi_ops * granule; +} + +/* Used by non INV_TYPE_ATS* invalidations */ +static void arm_smmu_inv_to_cmdq_batch(struct arm_smmu_inv *inv, + struct arm_smmu_cmdq_batch *cmds, + struct arm_smmu_cmdq_ent *cmd, + unsigned long iova, size_t size, + unsigned int granule) +{ + if (arm_smmu_inv_size_too_big(inv->smmu, size, granule)) { + cmd->opcode =3D inv->nsize_opcode; + arm_smmu_cmdq_batch_add(inv->smmu, cmds, cmd); + return; + } + + cmd->opcode =3D inv->size_opcode; + arm_smmu_cmdq_batch_add_range(inv->smmu, cmds, cmd, iova, size, granule, + inv->pgsize); +} + +static inline bool arm_smmu_invs_end_batch(struct arm_smmu_inv *cur, + struct arm_smmu_inv *next) +{ + /* Changing smmu means changing command queue */ + if (cur->smmu !=3D next->smmu) + return true; + /* The batch for S2 TLBI must be done before nested S1 ASIDs */ + if (cur->type !=3D INV_TYPE_S2_VMID_S1_CLEAR && + next->type =3D=3D INV_TYPE_S2_VMID_S1_CLEAR) + return true; + /* ATS must be after a sync of the S1/S2 invalidations */ + if (!arm_smmu_inv_is_ats(cur) && arm_smmu_inv_is_ats(next)) + return true; + return false; +} + +static void __arm_smmu_domain_inv_range(struct arm_smmu_invs *invs, + unsigned long iova, size_t size, + unsigned int granule, bool leaf) +{ + struct arm_smmu_cmdq_batch cmds =3D {}; + struct arm_smmu_inv *cur; + struct arm_smmu_inv *end; + + cur =3D invs->inv; + end =3D cur + READ_ONCE(invs->num_invs); + /* Skip any leading entry marked as a trash */ + for (; cur !=3D end; cur++) + if (refcount_read(&cur->users)) + break; + while (cur !=3D end) { + struct arm_smmu_device *smmu =3D cur->smmu; + struct arm_smmu_cmdq_ent cmd =3D { + /* + * Pick size_opcode to run arm_smmu_get_cmdq(). This can + * be changed to nsize_opcode, which would result in the + * same CMDQ pointer. + */ + .opcode =3D cur->size_opcode, + }; + struct arm_smmu_inv *next; + + if (!cmds.num) + arm_smmu_cmdq_batch_init(smmu, &cmds, &cmd); + + switch (cur->type) { + case INV_TYPE_S1_ASID: + cmd.tlbi.asid =3D cur->id; + cmd.tlbi.leaf =3D leaf; + arm_smmu_inv_to_cmdq_batch(cur, &cmds, &cmd, iova, size, + granule); + break; + case INV_TYPE_S2_VMID: + cmd.tlbi.vmid =3D cur->id; + cmd.tlbi.leaf =3D leaf; + arm_smmu_inv_to_cmdq_batch(cur, &cmds, &cmd, iova, size, + granule); + break; + case INV_TYPE_S2_VMID_S1_CLEAR: + /* CMDQ_OP_TLBI_S12_VMALL already flushed S1 entries */ + if (arm_smmu_inv_size_too_big(cur->smmu, size, granule)) + continue; + cmd.tlbi.vmid =3D cur->id; + arm_smmu_cmdq_batch_add(smmu, &cmds, &cmd); + break; + case INV_TYPE_ATS: + arm_smmu_atc_inv_to_cmd(cur->ssid, iova, size, &cmd); + cmd.atc.sid =3D cur->id; + arm_smmu_cmdq_batch_add(smmu, &cmds, &cmd); + break; + case INV_TYPE_ATS_FULL: + arm_smmu_atc_inv_to_cmd(IOMMU_NO_PASID, 0, 0, &cmd); + cmd.atc.sid =3D cur->id; + arm_smmu_cmdq_batch_add(smmu, &cmds, &cmd); + break; + default: + WARN_ON_ONCE(1); + continue; + } + + /* Skip any trash entry in-between */ + for (next =3D cur + 1; next !=3D end; next++) + if (refcount_read(&next->users)) + break; + + if (cmds.num && + (next =3D=3D end || arm_smmu_invs_end_batch(cur, next))) { + arm_smmu_cmdq_batch_submit(smmu, &cmds); + cmds.num =3D 0; + } + cur =3D next; + } +} + +void arm_smmu_domain_inv_range(struct arm_smmu_domain *smmu_domain, + unsigned long iova, size_t size, + unsigned int granule, bool leaf) +{ + struct arm_smmu_invs *invs; + + /* + * An invalidation request must follow some IOPTE change and then load + * an invalidation array. In the meantime, a domain attachment mutates + * the array and then stores an STE/CD asking SMMU HW to acquire those + * changed IOPTEs. In other word, these two are interdependent and can + * race. + * + * In a race, the RCU design (with its underlying memory barriers) can + * ensure the invalidation array to always get updated before loaded. + * + * smp_mb() is used here, paired with the smp_mb() following the array + * update in a concurrent attach, to ensure: + * - HW sees the new IOPTEs if it walks after STE installation + * - Invalidation thread sees the updated array with the new ASID. + * + * [CPU0] | [CPU1] + * | + * change IOPTEs and TLB flush: | + * arm_smmu_domain_inv_range() { | arm_smmu_install_new_domain_invs { + * ... | rcu_assign_pointer(new_invs); + * smp_mb(); // ensure IOPTEs | smp_mb(); // ensure new_invs + * ... | kfree_rcu(old_invs, rcu); + * // load invalidation array | } + * invs =3D rcu_dereference(); | arm_smmu_install_ste_for_dev { + * | STE =3D TTB0 // read new IOPTEs + */ + smp_mb(); + + rcu_read_lock(); + invs =3D rcu_dereference(smmu_domain->invs); + + /* + * Avoid locking unless ATS is being used. No ATC invalidation can be + * going on after a domain is detached. + */ + if (invs->has_ats) { + read_lock(&invs->rwlock); + __arm_smmu_domain_inv_range(invs, iova, size, granule, leaf); + read_unlock(&invs->rwlock); + } else { + __arm_smmu_domain_inv_range(invs, iova, size, granule, leaf); + } + + rcu_read_unlock(); +} + static void arm_smmu_tlb_inv_page_nosync(struct iommu_iotlb_gather *gather, unsigned long iova, size_t granule, void *cookie) --=20 2.43.0 From nobody Fri Dec 19 21:45:36 2025 Received: from CH5PR02CU005.outbound.protection.outlook.com (mail-northcentralusazon11012008.outbound.protection.outlook.com [40.107.200.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 1A52D2E6CD8 for ; Sat, 8 Nov 2025 08:08:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.200.8 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762589330; cv=fail; b=WkTVKFy+mD2SL4/bX9/mzYAVDoa/fPByJIVtQ+oqX9ctWKyfClSCcMKdK5Okm8JLfhwsg9ufX7VoZ4GMzjrONqiTDk5BPxK352sViymDj6KDrSIo1HHaf2sT35W05lnR3HC/lNsqKrfm88ecjfBd2DW57MaNd00dqTIuPTefPxE= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762589330; c=relaxed/simple; bh=Z1G2BDjV39byU1DISWC2PwyZ+BCfBILrEWc1PAUhfnM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Yf82Rq24g4LlrGcAiKsTrXb3OaXsO/h3OhB2kFVaY4WbXl7duQf00VMlb4Li3VbCDMTJT6k5oB5jYdzo17aik84kEoFLlmLa8KqcNyl/NYkNU8h5seqMU0O5I4A3wJOitX1k+kyr6R47IRNASCPAy0HPeLoNlUzim9NRQej+/zs= 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=aFxABt4X; arc=fail smtp.client-ip=40.107.200.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="aFxABt4X" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=G1Ah7hxeqQhJfBpkzZngtEJkkMWgls9gWVoD4ZtwgxY3CTv2GlOM1ILxDv8RSX/YrYJeEIPjdpURknYYKID1n+U/ZbaQ4qJhebpAergg25jYMUyBLheIMdsOF4UlsvtYn9YaM3c5gp4crBJWW8wIDkS7sLC08ORXGDigLnVOGBqQKoeU3v5kwUKG8489TFcB/nhSK1S3aTSi6OcKcnskIokMP3HA85gAyLxClz90OiQzKxK+yvIPR2omkWmTsQvxT8IwI25jzm5sbra5J+EbK40K7Z30R9yA/2fVX1jTnFOFyOMo/YQOJGPz4UaBtRAHej9runDRxLt/0vhdmRLZVw== 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=2uJ/mnIrDByO0C8OruBAIwUKgzZHwGw7ZPJwxf/SC3M=; b=H7jbBXtXM7q+RRndCzcUpve6K3OXBny6AhiPfNSs4lZIUWdpvyYvur5aoE8IDcTH/V8qa7j1bHBmE/DM/5tLTc0BKsB2yL/nhh+BqtpF+BDH1sjPwnLjGSv0saUffVq+Irc77xdJNzXLrKu3foVi5jxesspsRAZ/41UYMcr5G6yULFFWSiPMNW3yoKoEvscAmFZ0cOLt5ZRNVWzUGSC61RgAKGHxrVWXZDX3rYW6thyTEqHsqsWCh0DgRRuqfTqavWqeU0D+JR7hqLJrcXS6cGUNngQmiy6cLEMb+XpuwFdUywz6LxeWflbxa/QqxcZ1NkIX3VP76GZmUWCWdm+rsQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) 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=2uJ/mnIrDByO0C8OruBAIwUKgzZHwGw7ZPJwxf/SC3M=; b=aFxABt4X+VeEQ9knY4tQWsEuHpSHK+yz4V7qMr3MVhEWr+eCUe/u+uQCibgM77Dw0Kn71IoppYPh24FxICBv/+6GzbG0jg7P3uP1HrFu2KpjzrTavu/5ZipUPCEA4EvVgbualSisugxWzfOrpGduYS5jd3JuBVlrywIFgXrN3F5VToBDZ/Wcr0hJeUx2ZE581iDdh+ooFVydvLZFGwvFrWzko3EYFdZ34OOd67L3/5q6rdHXeFJw+iE9Gq58+VcS4370f6DLv5pj8PL45YXaSVvDLXH1wBjU3MFzIj+QDmdUWEkoYYWP4crtM6L9kZEZiFpK4f55H72ohYKyCKjKyg== Received: from BLAP220CA0017.NAMP220.PROD.OUTLOOK.COM (2603:10b6:208:32c::22) by IA0PR12MB8694.namprd12.prod.outlook.com (2603:10b6:208:488::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9298.13; Sat, 8 Nov 2025 08:08:39 +0000 Received: from MN1PEPF0000F0E5.namprd04.prod.outlook.com (2603:10b6:208:32c:cafe::a) by BLAP220CA0017.outlook.office365.com (2603:10b6:208:32c::22) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9298.8 via Frontend Transport; Sat, 8 Nov 2025 08:08:27 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) 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.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by MN1PEPF0000F0E5.mail.protection.outlook.com (10.167.242.43) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9320.13 via Frontend Transport; Sat, 8 Nov 2025 08:08:38 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 8 Nov 2025 00:08:25 -0800 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Sat, 8 Nov 2025 00:08:25 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Sat, 8 Nov 2025 00:08:24 -0800 From: Nicolin Chen To: CC: , , , , , , , , , , , Subject: [PATCH v5 7/7] iommu/arm-smmu-v3: Perform per-domain invalidations using arm_smmu_invs Date: Sat, 8 Nov 2025 00:08:08 -0800 Message-ID: <8d155b495f926b957ddd36666f1107b8796df1d5.1762588839.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: MN1PEPF0000F0E5:EE_|IA0PR12MB8694:EE_ X-MS-Office365-Filtering-Correlation-Id: 8efeea6f-8008-4320-3e0b-08de1e9e062a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|1800799024|7416014|82310400026|376014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?WhtBvmncGlN273sf57hwiB+T+Yi1Or9Y95kawAsc6i1IJ1/CKf8ruc25k/S7?= =?us-ascii?Q?/pEik38d5qZDwFSkKsTcSYJEUwLdutNVf/8vL2tyb2ITqWsbOrsybWyva/GV?= =?us-ascii?Q?yz1/66O5zhldPQ8jOFK885aj+aHkXEYgacVgMt0TXN07od0IMBfkWFcl5aKy?= =?us-ascii?Q?R/Aa5X5bgT2hv5EW28j0BC1Kf0V5Ilm7tw9JciUx7oHsCuJ6r10Ga6Iemm3z?= =?us-ascii?Q?D9giFqXw71XkD2jGPew/Wv9dLg5BGSHI2ZGrrmq1KmY934PsWWlDXLsj6m/r?= =?us-ascii?Q?RMWUR3QQNahSfzmHh1Vcf8lW+boozCVCKkhqP+IdScd0I+Wm5Ia8rTlb+JI2?= =?us-ascii?Q?m91awz4ybCksF/g+mdyJf6YW8Rp4JmHwkDv3S7fmyGcYObDj//NtASMAEXJO?= =?us-ascii?Q?o7trNAN4pIUqVjhx9r2h/bFffC1sQOtd5tlYkUrIFS1bu0VmFGEDJJ12BSEW?= =?us-ascii?Q?BR90mHMBFKS5bzU4tVzbnO2jWtQwHr5N57g10hO6FZtddrVLNcNVk3MPK6ui?= =?us-ascii?Q?swtGuUTE7w0WIHv5+2k2rEgg6IOY75h6nNhBvJ2V8HrCdpZhj0v6LF+qj3TG?= =?us-ascii?Q?ZU1v5vhjBMEmtye9BpOish8nqfAUShOs7EPWv51uHUMp2yC/nHlzXyGZtCM1?= =?us-ascii?Q?st2ve2EtGdTTYHBnYSoscKLuwGjzqRnBGYKV5cJWoWvkCwbuqYNN/8ZosHIj?= =?us-ascii?Q?m/m9Y1GJF6h582t6CQqI3M/Wg3gpjQjPpA1SumeJXJ3cMoJWLP1gTDCjGgBO?= =?us-ascii?Q?iWnvTpO++tI6pXq8W5BdyuBGlE+8kh/wWMbzAz+zBFGA4sxDUmmqgiNtFnAf?= =?us-ascii?Q?Tt4A0jqExRUkmKPF2vLgEHD42hWl53BrIrQiTKAO0E/RxN1OvEBlinleiOsI?= =?us-ascii?Q?/xQu/ALzEka48hXZPzAYJaYWQn5BBvRaIWVeSZd5jXUZ3km38MYLA6t/Tkfd?= =?us-ascii?Q?j9SvFBPHoV1l9ErJoBS+V/+6wV2n2n3x34kb2IixpuNdSAotuGVzoKmbFI4H?= =?us-ascii?Q?h6mq8XRH08p5jGVOIGbo9YWTJmL2DNGhV/s+DyD4X8E2uvRxgy5KsoPgkVAA?= =?us-ascii?Q?zGRw0u+LJtgIVl6eyDp79UVqOEJnByR0dq31nD4X8+K6h2hTtH697mAtDxpb?= =?us-ascii?Q?/d/8bNPNcj0kSbJ6Q+uBh6MLuDK1pPPE+WQdJk+oWg4shcv6b2/bXwWZ+467?= =?us-ascii?Q?YcjC0pErJDm27RIvya5UA7V6u10coxtNsptpKApbsEBP3RH5I1KRpkZjgxly?= =?us-ascii?Q?KplxTvV78Fq1bUquyM7SOJm43ib8qLKtpiX2YrN0WL1vWQz+IJDoxLu9Y/1O?= =?us-ascii?Q?X7Dl7Klmf62myUrG3UARSEdOKQqGSSjTf3Kw4A/ATA7qh5WsQOqDliaMTALJ?= =?us-ascii?Q?WPCP64plp0ZcRPuDv0ZAQhzhpiQd2hf6ZMzE2D8jmRpT8+9+IZN1EGXgi8S/?= =?us-ascii?Q?dDYkIpEqtTDqfOiKWI43WK4Eloet0GSGLuiEGRnm1g3ab5eCBsqg9UAFWJKc?= =?us-ascii?Q?5HzGssiR1VGKycSSywpkQ+4O3Ki2lbdMaQw9hR8Hm3ZoolurZobJgdAnTKCV?= =?us-ascii?Q?PGPa4bBCCsNBwteEKt0=3D?= X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(36860700013)(1800799024)(7416014)(82310400026)(376014);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2025 08:08:38.8434 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8efeea6f-8008-4320-3e0b-08de1e9e062a X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: MN1PEPF0000F0E5.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB8694 Content-Type: text/plain; charset="utf-8" Replace the old invalidation functions with arm_smmu_domain_inv_range() in all the existing invalidation routines. And deprecate the old functions. The new arm_smmu_domain_inv_range() handles the CMDQ_MAX_TLBI_OPS as well, so drop it in the SVA function. Since arm_smmu_cmdq_batch_add_range() has only one caller now, and it must be given a valid size, add a WARN_ON_ONCE to catch any missed case. Reviewed-by: Jason Gunthorpe Signed-off-by: Nicolin Chen --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 7 - .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 29 +-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 165 +----------------- 3 files changed, 11 insertions(+), 190 deletions(-) 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 593f94a699863..3f19e4c75b933 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -1072,13 +1072,6 @@ int arm_smmu_set_pasid(struct arm_smmu_master *maste= r, struct arm_smmu_domain *smmu_domain, ioasid_t pasid, struct arm_smmu_cd *cd, struct iommu_domain *old); =20 -void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid); -void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid, - size_t granule, bool leaf, - struct arm_smmu_domain *smmu_domain); -int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, - unsigned long iova, size_t size); - void arm_smmu_domain_inv_range(struct arm_smmu_domain *smmu_domain, unsigned long iova, size_t size, unsigned int granule, bool leaf); diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iomm= u/arm/arm-smmu-v3/arm-smmu-v3-sva.c index fc601b494e0af..048b53f79b144 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -122,15 +122,6 @@ void arm_smmu_make_sva_cd(struct arm_smmu_cd *target, } EXPORT_SYMBOL_IF_KUNIT(arm_smmu_make_sva_cd); =20 -/* - * Cloned from the MAX_TLBI_OPS in arch/arm64/include/asm/tlbflush.h, this - * is used as a threshold to replace per-page TLBI commands to issue in the - * command queue with an address-space TLBI command, when SMMU w/o a range - * invalidation feature handles too many per-page TLBI commands, which will - * otherwise result in a soft lockup. - */ -#define CMDQ_MAX_TLBI_OPS (1 << (PAGE_SHIFT - 3)) - static void arm_smmu_mm_arch_invalidate_secondary_tlbs(struct mmu_notifier= *mn, struct mm_struct *mm, unsigned long start, @@ -146,21 +137,8 @@ static void arm_smmu_mm_arch_invalidate_secondary_tlbs= (struct mmu_notifier *mn, * range. So do a simple translation here by calculating size correctly. */ size =3D end - start; - if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_RANGE_INV)) { - if (size >=3D CMDQ_MAX_TLBI_OPS * PAGE_SIZE) - size =3D 0; - } else { - if (size =3D=3D ULONG_MAX) - size =3D 0; - } - - if (!size) - arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_domain->cd.asid); - else - arm_smmu_tlb_inv_range_asid(start, size, smmu_domain->cd.asid, - PAGE_SIZE, false, smmu_domain); =20 - arm_smmu_atc_inv_domain(smmu_domain, start, size); + arm_smmu_domain_inv_range(smmu_domain, start, size, PAGE_SIZE, false); } =20 static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct = *mm) @@ -191,8 +169,7 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn= , struct mm_struct *mm) } spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); =20 - arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_domain->cd.asid); - arm_smmu_atc_inv_domain(smmu_domain, 0, 0); + arm_smmu_domain_inv(smmu_domain); } =20 static void arm_smmu_mmu_notifier_free(struct mmu_notifier *mn) @@ -301,7 +278,7 @@ static void arm_smmu_sva_domain_free(struct iommu_domai= n *domain) /* * Ensure the ASID is empty in the iommu cache before allowing reuse. */ - arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_domain->cd.asid); + arm_smmu_domain_inv(smmu_domain); =20 /* * Notice that the arm_smmu_mm_arch_invalidate_secondary_tlbs op can 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 9f4f4e95a3603..f9ffc29661927 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1256,16 +1256,6 @@ struct arm_smmu_invs *arm_smmu_invs_purge(struct arm= _smmu_invs *invs, EXPORT_SYMBOL_IF_KUNIT(arm_smmu_invs_purge); =20 /* Context descriptor manipulation functions */ -void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid) -{ - struct arm_smmu_cmdq_ent cmd =3D { - .opcode =3D smmu->features & ARM_SMMU_FEAT_E2H ? - CMDQ_OP_TLBI_EL2_ASID : CMDQ_OP_TLBI_NH_ASID, - .tlbi.asid =3D asid, - }; - - arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd); -} =20 /* * Based on the value of ent report which bits of the STE the HW will acce= ss. It @@ -2420,74 +2410,10 @@ static int arm_smmu_atc_inv_master(struct arm_smmu_= master *master, return arm_smmu_cmdq_batch_submit(master->smmu, &cmds); } =20 -int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, - unsigned long iova, size_t size) -{ - struct arm_smmu_master_domain *master_domain; - int i; - unsigned long flags; - struct arm_smmu_cmdq_ent cmd =3D { - .opcode =3D CMDQ_OP_ATC_INV, - }; - struct arm_smmu_cmdq_batch cmds; - - if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_ATS)) - return 0; - - /* - * Ensure that we've completed prior invalidation of the main TLBs - * before we read 'nr_ats_masters' in case of a concurrent call to - * arm_smmu_enable_ats(): - * - * // unmap() // arm_smmu_enable_ats() - * TLBI+SYNC atomic_inc(&nr_ats_masters); - * smp_mb(); [...] - * atomic_read(&nr_ats_masters); pci_enable_ats() // writel() - * - * Ensures that we always see the incremented 'nr_ats_masters' count if - * ATS was enabled at the PCI device before completion of the TLBI. - */ - smp_mb(); - if (!atomic_read(&smmu_domain->nr_ats_masters)) - return 0; - - arm_smmu_cmdq_batch_init(smmu_domain->smmu, &cmds, &cmd); - - spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_for_each_entry(master_domain, &smmu_domain->devices, - devices_elm) { - struct arm_smmu_master *master =3D master_domain->master; - - if (!master->ats_enabled) - continue; - - if (master_domain->nested_ats_flush) { - /* - * If a S2 used as a nesting parent is changed we have - * no option but to completely flush the ATC. - */ - arm_smmu_atc_inv_to_cmd(IOMMU_NO_PASID, 0, 0, &cmd); - } else { - arm_smmu_atc_inv_to_cmd(master_domain->ssid, iova, size, - &cmd); - } - - for (i =3D 0; i < master->num_streams; i++) { - cmd.atc.sid =3D master->streams[i].id; - arm_smmu_cmdq_batch_add(smmu_domain->smmu, &cmds, &cmd); - } - } - spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); - - return arm_smmu_cmdq_batch_submit(smmu_domain->smmu, &cmds); -} - /* IO_PGTABLE API */ static void arm_smmu_tlb_inv_context(void *cookie) { struct arm_smmu_domain *smmu_domain =3D cookie; - struct arm_smmu_device *smmu =3D smmu_domain->smmu; - struct arm_smmu_cmdq_ent cmd; =20 /* * NOTE: when io-pgtable is in non-strict mode, we may get here with @@ -2496,14 +2422,7 @@ static void arm_smmu_tlb_inv_context(void *cookie) * insertion to guarantee those are observed before the TLBI. Do be * careful, 007. */ - if (smmu_domain->stage =3D=3D ARM_SMMU_DOMAIN_S1) { - arm_smmu_tlb_inv_asid(smmu, smmu_domain->cd.asid); - } else { - cmd.opcode =3D CMDQ_OP_TLBI_S12_VMALL; - cmd.tlbi.vmid =3D smmu_domain->s2_cfg.vmid; - arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd); - } - arm_smmu_atc_inv_domain(smmu_domain, 0, 0); + arm_smmu_domain_inv(smmu_domain); } =20 static void arm_smmu_cmdq_batch_add_range(struct arm_smmu_device *smmu, @@ -2515,7 +2434,7 @@ static void arm_smmu_cmdq_batch_add_range(struct arm_= smmu_device *smmu, unsigned long end =3D iova + size, num_pages =3D 0, tg =3D pgsize; size_t inv_range =3D granule; =20 - if (!size) + if (WARN_ON_ONCE(!size)) return; =20 if (smmu->features & ARM_SMMU_FEAT_RANGE_INV) { @@ -2570,76 +2489,6 @@ static void arm_smmu_cmdq_batch_add_range(struct arm= _smmu_device *smmu, } } =20 -static void __arm_smmu_tlb_inv_range(struct arm_smmu_cmdq_ent *cmd, - unsigned long iova, size_t size, - size_t granule, - struct arm_smmu_domain *smmu_domain) -{ - struct arm_smmu_device *smmu =3D smmu_domain->smmu; - struct arm_smmu_cmdq_batch cmds; - size_t pgsize; - - /* Get the leaf page size */ - pgsize =3D __ffs(smmu_domain->domain.pgsize_bitmap); - - arm_smmu_cmdq_batch_init(smmu, &cmds, cmd); - arm_smmu_cmdq_batch_add_range(smmu, &cmds, cmd, iova, size, granule, - pgsize); - arm_smmu_cmdq_batch_submit(smmu, &cmds); -} - -static void arm_smmu_tlb_inv_range_domain(unsigned long iova, size_t size, - size_t granule, bool leaf, - struct arm_smmu_domain *smmu_domain) -{ - struct arm_smmu_cmdq_ent cmd =3D { - .tlbi =3D { - .leaf =3D leaf, - }, - }; - - if (smmu_domain->stage =3D=3D ARM_SMMU_DOMAIN_S1) { - cmd.opcode =3D smmu_domain->smmu->features & ARM_SMMU_FEAT_E2H ? - CMDQ_OP_TLBI_EL2_VA : CMDQ_OP_TLBI_NH_VA; - cmd.tlbi.asid =3D smmu_domain->cd.asid; - } else { - cmd.opcode =3D CMDQ_OP_TLBI_S2_IPA; - cmd.tlbi.vmid =3D smmu_domain->s2_cfg.vmid; - } - __arm_smmu_tlb_inv_range(&cmd, iova, size, granule, smmu_domain); - - if (smmu_domain->nest_parent) { - /* - * When the S2 domain changes all the nested S1 ASIDs have to be - * flushed too. - */ - cmd.opcode =3D CMDQ_OP_TLBI_NH_ALL; - arm_smmu_cmdq_issue_cmd_with_sync(smmu_domain->smmu, &cmd); - } - - /* - * Unfortunately, this can't be leaf-only since we may have - * zapped an entire table. - */ - arm_smmu_atc_inv_domain(smmu_domain, iova, size); -} - -void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid, - size_t granule, bool leaf, - struct arm_smmu_domain *smmu_domain) -{ - struct arm_smmu_cmdq_ent cmd =3D { - .opcode =3D smmu_domain->smmu->features & ARM_SMMU_FEAT_E2H ? - CMDQ_OP_TLBI_EL2_VA : CMDQ_OP_TLBI_NH_VA, - .tlbi =3D { - .asid =3D asid, - .leaf =3D leaf, - }, - }; - - __arm_smmu_tlb_inv_range(&cmd, iova, size, granule, smmu_domain); -} - static bool arm_smmu_inv_size_too_big(struct arm_smmu_device *smmu, size_t= size, size_t granule) { @@ -2840,7 +2689,9 @@ static void arm_smmu_tlb_inv_page_nosync(struct iommu= _iotlb_gather *gather, static void arm_smmu_tlb_inv_walk(unsigned long iova, size_t size, size_t granule, void *cookie) { - arm_smmu_tlb_inv_range_domain(iova, size, granule, false, cookie); + struct arm_smmu_domain *smmu_domain =3D cookie; + + arm_smmu_domain_inv_range(smmu_domain, iova, size, granule, false); } =20 static const struct iommu_flush_ops arm_smmu_flush_ops =3D { @@ -4084,9 +3935,9 @@ static void arm_smmu_iotlb_sync(struct iommu_domain *= domain, if (!gather->pgsize) return; =20 - arm_smmu_tlb_inv_range_domain(gather->start, - gather->end - gather->start + 1, - gather->pgsize, true, smmu_domain); + arm_smmu_domain_inv_range(smmu_domain, gather->start, + gather->end - gather->start + 1, + gather->pgsize, true); } =20 static phys_addr_t --=20 2.43.0