Skip to content

Commit 7062af3

Browse files
jpbruckerjoergroedel
authored andcommitted
iommu/virtio: Fix freeing of incomplete domains
Calling viommu_domain_free() on a domain that hasn't been finalised (not attached to any device, for example) can currently cause an Oops, because we attempt to call ida_free() on ID 0, which may either be unallocated or used by another domain. Only initialise the vdomain->viommu pointer, which denotes a finalised domain, at the end of a successful viommu_domain_finalise(). Fixes: edcd69a ("iommu: Add virtio-iommu driver") Reported-by: Eric Auger <[email protected]> Signed-off-by: Jean-Philippe Brucker <[email protected]> Reviewed-by: Robin Murphy <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joerg Roedel <[email protected]>
1 parent 3f84b96 commit 7062af3

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

drivers/iommu/virtio-iommu.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -613,18 +613,20 @@ static int viommu_domain_finalise(struct viommu_dev *viommu,
613613
int ret;
614614
struct viommu_domain *vdomain = to_viommu_domain(domain);
615615

616-
vdomain->viommu = viommu;
617-
vdomain->map_flags = viommu->map_flags;
616+
ret = ida_alloc_range(&viommu->domain_ids, viommu->first_domain,
617+
viommu->last_domain, GFP_KERNEL);
618+
if (ret < 0)
619+
return ret;
620+
621+
vdomain->id = (unsigned int)ret;
618622

619623
domain->pgsize_bitmap = viommu->pgsize_bitmap;
620624
domain->geometry = viommu->geometry;
621625

622-
ret = ida_alloc_range(&viommu->domain_ids, viommu->first_domain,
623-
viommu->last_domain, GFP_KERNEL);
624-
if (ret >= 0)
625-
vdomain->id = (unsigned int)ret;
626+
vdomain->map_flags = viommu->map_flags;
627+
vdomain->viommu = viommu;
626628

627-
return ret > 0 ? 0 : ret;
629+
return 0;
628630
}
629631

630632
static void viommu_domain_free(struct iommu_domain *domain)

0 commit comments

Comments
 (0)