A master's StreamID list may contain duplicates due to PCI DMA aliases. arm_smmu_insert_master() skips duplicate IDs belonging to the same master, leaving the corresponding rb_nodes unlinked. Both arm_smmu_remove_master() and the insertion error path still pass these nodes to rb_erase(). Erasing a zeroed, unlinked node clears the tree root, losing SID-to-master mappings for other devices on the SMMU. Mark the skipped nodes with RB_CLEAR_NODE() and skip them in both removal paths. Set the marker after sorting the stream array, since RB_CLEAR_NODE() stores the node's own address. Fixes: b00d24997a11 ("iommu/arm-smmu-v3: Fix iommu_device_probe bug due to duplicated stream ids") Cc: stable@vger.kernel.org Signed-off-by: zhoumin --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 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 5732f3ba0122..2627d496921e 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -4141,8 +4141,10 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu, ->master; /* Bridged PCI devices may end up with duplicated IDs */ - if (existing_master == master) + if (existing_master == master) { + RB_CLEAR_NODE(&new_stream->node); continue; + } dev_warn(master->dev, "Aliasing StreamID 0x%x (from %s) unsupported, expect DMA to be broken\n", @@ -4154,7 +4156,8 @@ static int arm_smmu_insert_master(struct arm_smmu_device *smmu, if (ret) { for (i--; i >= 0; i--) - rb_erase(&master->streams[i].node, &smmu->streams); + if (!RB_EMPTY_NODE(&master->streams[i].node)) + rb_erase(&master->streams[i].node, &smmu->streams); kfree(master->streams); kfree(master->build_invs); } @@ -4174,7 +4177,8 @@ static void arm_smmu_remove_master(struct arm_smmu_master *master) mutex_lock(&smmu->streams_mutex); for (i = 0; i < fwspec->num_ids; i++) - rb_erase(&master->streams[i].node, &smmu->streams); + if (!RB_EMPTY_NODE(&master->streams[i].node)) + rb_erase(&master->streams[i].node, &smmu->streams); mutex_unlock(&smmu->streams_mutex); kfree(master->streams); -- 2.43.0