From patchwork Fri Apr 11 04:47:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 14047554 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CF144C36010 for ; Fri, 11 Apr 2025 04:49:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type: Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject:CC:To:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=OfkjrbYlMuifLR0ij9JE1tD+4mnpwymrnA4950GEcYQ=; b=jfGcyWV9oZDLGfkqsfwlYoojnp ccpEmBH0kaeSdNZsH4Ko2/6R1AWgEeDqD5+WGD+89ZIpVDTRn/FlYAxNOuggFxv068XgpF3E6tYLE GFUdZc4Vx3dAcO9UskUASA4zfnXS9ckD2umKpTVzRDiGfX4GwaikxfX6JhJmNDLUlHnDcThVFNDun jIRfmErQcezQAOyG+ckhE1UIRHrLI6rq5rVlTamnzlaGorcsNCYJXqtIKgyc7vgFUq254+qI/XB/Z tYkIoQb9IyU4MtIjHAC58LlTh+GDFw8YGokWvTzlZJbxl8JggfBk34thwGHap9lKO7/OtSg6h5OKb Zihmi1CQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1u36KT-0000000CUqg-1Fji; Fri, 11 Apr 2025 04:49:25 +0000 Received: from mail-bn8nam11on2061b.outbound.protection.outlook.com ([2a01:111:f403:2414::61b] helo=NAM11-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1u36If-0000000CUkI-0b6A for linux-arm-kernel@lists.infradead.org; Fri, 11 Apr 2025 04:47:34 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=bZr2+wZpV1KvYdye5e40nv2zgGTZ5cyVJagLCU2gXzVMr4DYjUrFg49FjK8sftgtD3pa40VgWpqqyEcApCUjItK/OQJgeKkwHhHLYt/uSp5Nx8ANWuX6JDf3KzbUvlQXDiyonJrpgfXVfSlEZfuoWUexnnzk3JfFY4qCEpjaKWtjEkOahHUhDih24IzaDF0j1ASRa3rkcvAD4L34M5ZXP1oecQzwaFNPkPM0wn47RfGp37WQfg0FwL7MAU6ahsRlPoV8ATS7Dw8FUEH9GPiaLS2+Gl/FjKvcgPyz84gtPGN9phduNZFfm/KuJLn63NDtKfSTnmU/87iMITzKeuuTzw== 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=OfkjrbYlMuifLR0ij9JE1tD+4mnpwymrnA4950GEcYQ=; b=V2JVmRRa+RgXKUTiKZ3iwRsfPHbYoDEcfrDbNmtREi+oBZaFevn8tAb8IZshvlURZemRwII3kcZAw4KZMvZGIDJMRxM2M14nsdrlfU/pOoh7SU+ZkpWjtprlhYgSezpviCjbr/C6mRvAvYa2HxEY2Y6FAP3ayCLfU4YLhBpt3FQX0XitBbg+kgdHMPBOk/0pz+sKd0ZPL8YpKDGQXlEcwLYjl0BpsJ9sTKFZHjfBC12kzUpiHdoqyVjTN2AAt24TzMBCwEb/Jxe3V5xDJkCNniQOR3oRdBHIjQX1hbMTAu2gAQ350s0M2x2cSLhOlilWhKTKY2TsQtJVAsoQqxXqBg== 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=OfkjrbYlMuifLR0ij9JE1tD+4mnpwymrnA4950GEcYQ=; b=rSj5x62UChW/9mNAZ1XTYJOd1oN39Zahf8DlNg/johV7P5R8yLcizqCykyapJcNeoqxxZCrJEAmnf+Vkl4s6rwML4+GdhxE9zNLeCg3fUksh/y/49D71NvGRcF/RHmmqgGHcLIazxBK+AMVk0+3QQNcb3uZDgBuHJhF7RZJM+acq90+oChXzW7KUT2IiT32ngnemjY40oYlxHmxlPgZOJrtQ86Bj+wg1aBGAm3JLlUUnrUsAd/xlpBlzEwID8W9O6BOL0UxirSfMV/JSzp67FEJ+ajA/nTZSSQy2uICthPfn1zSaRXP4Fwlsq2McS1MGs1uv5fsC4XbbCbfyYJLVhg== Received: from MW4PR04CA0326.namprd04.prod.outlook.com (2603:10b6:303:82::31) by IA0PR12MB9048.namprd12.prod.outlook.com (2603:10b6:208:408::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8632.22; Fri, 11 Apr 2025 04:47:19 +0000 Received: from SJ1PEPF000023D8.namprd21.prod.outlook.com (2603:10b6:303:82:cafe::4b) by MW4PR04CA0326.outlook.office365.com (2603:10b6:303:82::31) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8632.23 via Frontend Transport; Fri, 11 Apr 2025 04:47:19 +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 SJ1PEPF000023D8.mail.protection.outlook.com (10.167.244.73) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8655.0 via Frontend Transport; Fri, 11 Apr 2025 04:47:19 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) 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.1544.4; Thu, 10 Apr 2025 21:47:08 -0700 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Thu, 10 Apr 2025 21:47:08 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.1544.14 via Frontend Transport; Thu, 10 Apr 2025 21:47:07 -0700 From: Nicolin Chen To: , CC: , , , , , , Subject: [PATCH] iommu/arm-smmu-v3: Allow stream table to have nodes with the same ID Date: Thu, 10 Apr 2025 21:47:06 -0700 Message-ID: <20250411044706.356395-1-nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-NV-OnPremToCloud: AnonymousSubmission X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF000023D8:EE_|IA0PR12MB9048:EE_ X-MS-Office365-Filtering-Correlation-Id: 8411f44c-26d1-430b-6977-08dd78b3f0d0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|82310400026|36860700013; X-Microsoft-Antispam-Message-Info: Efo8pirebECpalDpA2SaaFh/ajzGv5za3LwbCYBgmA9XoNYecPzL+cGVHrzb7cLAVbdREjG/C294zuh1l+ngsgyL1ektYUS7eA9Muiv7s7fN3T1uvMmKJzt3bl2D97BFbCF4Uu+YOc0yY1t/VgANMbUC7YoAtJchYxtlh968xM75RvR6GY2yXb9rfXocrium5i9W02StinxzqOtKXlFWGx7hKyL3qjvplDRYsUoYvytSqqu9e8dC0f1N/RWGWhkL9hFDiVwKh+iYd36YG6nky48ENoi+mDf4DpSjmB35iFke+sk4ia5YxXnE5QL2KrQHRFzV0GDECKltz4hx9ZDl/K8u0pHVY4Ldc4in+Ek277whUBaVch+pEsseHasWseK0LmugXmhB8ejPuYaETp8G790h8LXaCKs2bfJVHuubGZvdxdr3MBkS8cGREYed3fltdTIyTNkVxUbIYWgi1DKgK/fwystGczf5DOc096qI/chOpvZLg7HE6qsdQSuOVsLDn2AQ97mrwWNB6asMWUmPNV0tPDbYcIeeKlA+qjaPT8FZfbLJmnHP+oyex0T1Lq6Inandxpuh2cfIEq3i4u+85tALOzHc+3p7m3tePEeDSgeiEpiyMCqqleydfH40v5zda7rUz1vqdV8efrIG05TRdaXJRCaV/pM1WNAd5gzdgclaRZRamI2+O2QCq6octolLFzbhdz+JKhf2u78WCecvia8a3ctc+Q83/dFToyjfi/LOBO1ev42vdxZfWJy/kvpNHnqOEereEbBx6Sk+ZUMyAjAPxcnnj3U+E9TEUoKRYhaHEL/g3Crh+iRPMNIWlHOIGC1falHPXaXKzxnblY5lrYaZgDDwEOB23y9ie7Yo9f5J46kUci3OcI18yPWoOd/ayv6IXqFSu2tyRApvBGJG8iexz0U+VpI2bpQbwzaxZdzXBFrg114RF8EZJiWqpuTDn9XPY+uy/IdxOhFPAlwOvZRUwcv86pfW6GR1hBT8TllAhEHAND7qBAJhTo+QIppD7qSDcJY6Ku3CGeTYKwccerZFeMNxElsvMV1oGImt3C18ENVIYlTVemmm2qyakCVl14BnPhgTq9pE64lpIU5xyJF5Tf5SDjX+WGXN5Sho5Ccy+kK2sZxXu4tWVthsYM8Q39NY0BXQi+qmVblZeAo7JyCAZO0U4f5PXOvMHOuw2f/IkKiG0zvZf+kKsMYv0Fa/0xw0XOMOSCoQyjfJtizCeoGIpvdF0usnbSAIG3GsB9vUQ6i0VkbaKSY3EhIzunIT6qpgMITjGEblIRsfBloTqeWm8GGPgWQOHmlH97Q7J9ERLwH13Eb1fMmvDbLxq9bZqYXwge9IySP2FNk8J6KCT3/L53OiCWcHUOpURMstNlUz7yqh8FsiGjVbk0Ql480233ZZsS+d0mgkE0hEEet7cGIy0FWgj60Iuwa9GPApOhKiXpJzaNEqAn4qJNc3B8+Fdd6ayoMdTZhSdl/qvh9aYtywQfT3omFPnZIscNZVvJzD2gAy/1KhovcKvE0YFvvD 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)(376014)(82310400026)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2025 04:47:19.0816 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8411f44c-26d1-430b-6977-08dd78b3f0d0 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: SJ1PEPF000023D8.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB9048 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250410_214733_176678_AD9B539D X-CRM114-Status: GOOD ( 21.31 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Jason Gunthorpe ASPEED VGA card has two built-in devices: 0008:06:00.0 PCI bridge: ASPEED Technology, Inc. AST1150 PCI-to-PCI Bridge (rev 06) 0008:07:00.0 VGA compatible controller: ASPEED Technology, Inc. ASPEED Graphics Family (rev 52) Its toplogy looks like this: +-[0008:00]---00.0-[01-09]--+-00.0-[02-09]--+-00.0-[03]----00.0 Sandisk Corp Device 5017 | +-01.0-[04]-- | +-02.0-[05]----00.0 NVIDIA Corporation Device | +-03.0-[06-07]----00.0-[07]----00.0 ASPEED Technology, Inc. ASPEED Graphics Family | +-04.0-[08]----00.0 Renesas Technology Corp. uPD720201 USB 3.0 Host Controller | \-05.0-[09]----00.0 Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller \-00.1 PMC-Sierra Inc. Device 4028 Being a legacy PCI device that does not have RID on the wire, the system does not preserve a RID for that PCI bridge (0008:06), so the IORT code has to dma alias for iort_pci_iommu_init() via pci_for_each_dma_alias(), resulting in both of them getting the same Stream ID. On a kernel prior to v6.15-rc1, there has been an overlooked warning: pci 0008:07:00.0: vgaarb: setting as boot VGA device pci 0008:07:00.0: vgaarb: bridge control possible pci 0008:07:00.0: vgaarb: VGA device added: decodes=io+mem,owns=none,locks=none pcieport 0008:06:00.0: Adding to iommu group 14 ast 0008:07:00.0: stream 67328 already in tree <===== WARNING ast 0008:07:00.0: enabling device (0002 -> 0003) ast 0008:07:00.0: Using default configuration ast 0008:07:00.0: AST 2600 detected ast 0008:07:00.0: [drm] Using analog VGA ast 0008:07:00.0: [drm] dram MCLK=396 Mhz type=1 bus_width=16 [drm] Initialized ast 0.1.0 for 0008:07:00.0 on minor 0 ast 0008:07:00.0: [drm] fb0: astdrmfb frame buffer device As such a legacy system might not use DMA at all, an iommu_probe_device() failure didn't actually break it, except that warning that does not block the system boot flow. With v6.15-rc, since the commit bcb81ac6ae3c ("iommu: Get DT/ACPI parsing into the proper probe path"), the error returned with the warning is moved to the IOMMU device probe flow, e.g. call trace with SMMUv3: arm_smmu_probe_device+0x15c/0x4c0 __iommu_probe_device+0x150/0x4f8 probe_iommu_group+0x44/0x80 bus_for_each_dev+0x7c/0x100 bus_iommu_probe+0x48/0x1a8 iommu_device_register+0xb8/0x178 arm_smmu_device_probe+0x1350/0x1db0 This then fails the entire SMMU driver probe: arm-smmu-v3 arm-smmu-v3.9.auto: found companion CMDQV device: NVDA200C:04 arm-smmu-v3 arm-smmu-v3.9.auto: option mask 0x10 arm-smmu-v3 arm-smmu-v3.9.auto: ias 48-bit, oas 48-bit (features 0x001e1fbf) arm-smmu-v3 arm-smmu-v3.9.auto: allocated 524288 entries for cmdq arm-smmu-v3 arm-smmu-v3.9.auto: allocated 524288 entries for evtq arm-smmu-v3 arm-smmu-v3.9.auto: allocated 524288 entries for priq arm-smmu-v3 arm-smmu-v3.9.auto: allocated 524288 entries for vcmdq0 arm-smmu-v3 arm-smmu-v3.9.auto: allocated 524288 entries for vcmdq1 arm-smmu-v3 arm-smmu-v3.9.auto: msi_domain absent - falling back to wired irqs arm-smmu-v3 arm-smmu-v3.9.auto: no priq irq - PRI will be broken pci 0008:00:00.0: Adding to iommu group 10 pci 0008:01:00.0: Adding to iommu group 11 pci 0008:01:00.1: Adding to iommu group 12 pci 0008:02:00.0: Adding to iommu group 13 pci 0008:02:01.0: Adding to iommu group 14 pci 0008:02:02.0: Adding to iommu group 15 pci 0008:02:03.0: Adding to iommu group 16 pci 0008:02:04.0: Adding to iommu group 17 pci 0008:02:05.0: Adding to iommu group 18 pci 0008:03:00.0: Adding to iommu group 19 pci 0008:05:00.0: Adding to iommu group 20 pci 0008:06:00.0: Adding to iommu group 21 pci 0008:07:00.0: stream 67328 already in tree arm-smmu-v3 arm-smmu-v3.9.auto: Failed to register iommu arm-smmu-v3 arm-smmu-v3.9.auto: probe with driver arm-smmu-v3 failed with error -22 Given that a device bundled with a legacy PCI bridge could have duplicated Stream IDs, the concept of a stream_id tree with unique node in the SMMUv3 driver doesn't work any more. Change the arm_smmu_streams_cmp_node() to allow the stream table to hold multiple nodes with the same Stream ID. Meanwhile, the reverse lookup from the Stream ID to a device pointer will have to be broken, i.e. the eventq handler will no longer find the device with a Stream ID in such cases. Signed-off-by: Jason Gunthorpe Tested-by: Nicolin Chen Signed-off-by: Nicolin Chen --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 31 +++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c index b4c21aaed126..5ce64dc78e12 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1762,8 +1762,25 @@ static int arm_smmu_streams_cmp_key(const void *lhs, const struct rb_node *rhs) static int arm_smmu_streams_cmp_node(struct rb_node *lhs, const struct rb_node *rhs) { - return arm_smmu_streams_cmp_key( - &rb_entry(lhs, struct arm_smmu_stream, node)->id, rhs); + struct arm_smmu_stream *stream_lhs = + rb_entry(lhs, struct arm_smmu_stream, node); + struct arm_smmu_stream *stream_rhs = + rb_entry(rhs, struct arm_smmu_stream, node); + + if (stream_lhs->id < stream_rhs->id) + return -1; + if (stream_lhs->id > stream_rhs->id) + return 1; + + /* + * The stream table can have multiple nodes with the same ID if there + * are DMA aliases. + */ + if (stream_lhs < stream_rhs) + return -1; + if (stream_lhs > stream_rhs) + return 1; + return 0; } static struct arm_smmu_master * @@ -1776,6 +1793,16 @@ arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid) node = rb_find(&sid, &smmu->streams, arm_smmu_streams_cmp_key); if (!node) return NULL; + /* + * If there are DMA alises then there are multiple devices with the same + * stream ID and we cannot reliably convert from SID to master. + */ + if (node->rb_left && + rb_entry(node->rb_left, struct arm_smmu_stream, node)->id == sid) + return NULL; + if (node->rb_right && + rb_entry(node->rb_right, struct arm_smmu_stream, node)->id == sid) + return NULL; return rb_entry(node, struct arm_smmu_stream, node)->master; }