[PATCH] iommufd/selftest: Fix wrong slot cleared on mock HW queue destroy

From: Peiyang He

Date: Fri Nov 06 2026 - 07:35:36 EST


mock_hw_queue_init_phys() publishes a new queue in
mock_viommu->hw_queue[index] but never stores the index in
mock_hw_queue->index, while mock_hw_queue_destroy() uses that field to
clear the slot. Since the object is zero-allocated, destroying any queue
always clears hw_queue[0] instead of the queue's own slot.

Record the index before publishing the queue so that destroy clears the
correct slot.

Fixes: 20896914da8a ("iommufd/selftest: Add coverage for IOMMUFD_CMD_HW_QUEUE_ALLOC")
Signed-off-by: Peiyang He <peiyang_he@xxxxxxxxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
This bug cannot be detected by any sanitizers or kernel warnings.
We observed this kind of weird situation and thus found this bug:
after allocating queues 0 and 1 and destroying queue 1,
later IOMMU_HW_QUEUE_ALLOC for index 1 still fails with -EEXIST.

drivers/iommu/iommufd/selftest.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c
index 25387969a..54d86bce7 100644
--- a/drivers/iommu/iommufd/selftest.c
+++ b/drivers/iommu/iommufd/selftest.c
@@ -759,6 +759,7 @@ static int mock_hw_queue_init_phys(struct iommufd_hw_queue *hw_queue, u32 index,

mock_hw_queue->prev = prev;
mock_hw_queue->mock_viommu = mock_viommu;
+ mock_hw_queue->index = index;
mock_viommu->hw_queue[index] = mock_hw_queue;

hw_queue->destroy = &mock_hw_queue_destroy;