On Thu, Nov 12, 2026 at 02:56:59PM -0500, Yuho Choi wrote:
> hv_iommu_paging_domain_free() frees all page-table pages with
> pt_iommu_deinit() and only then asks the hypervisor to delete the
> device domain that points at them. If HVCALL_DELETE_DEVICE_DOMAIN
> fails, hv_delete_device_domain() only logs the error, returns the
> domain ID to the IDA, and the domain is freed anyway.
>
> The hypervisor then keeps a device domain whose page tables have been
> freed and can be reused for anything. The ID is also handed out again
> by the next paging domain allocation, while the hypervisor still has a
> domain with that ID.
>
> Delete the domain before freeing its page tables, and keep the ID, the
> page tables and the domain if the hypervisor fails to delete it. WARN
> when the delete fails, as the domain is then leaked.
>
> Fixes: 2690d4156b51 ("iommu/hyperv: Add para-virtualized IOMMU support for
> Hyper-V guest")
> Reviewed-by: Easwar Hariharan <[email protected]>
> Reviewed-by: Jason Gunthorpe <[email protected]>
> Signed-off-by: Yuho Choi <[email protected]>
> ---
> drivers/iommu/hyperv/hv-iommu-guest.c | 19 ++++++++++++++-----
> 1 file changed, 14 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/iommu/hyperv/hv-iommu-guest.c
> b/drivers/iommu/hyperv/hv-iommu-guest.c
> index 9f4fa4315091..aa1db9db1e97 100644
> --- a/drivers/iommu/hyperv/hv-iommu-guest.c
> +++ b/drivers/iommu/hyperv/hv-iommu-guest.c
> @@ -107,7 +107,7 @@ static int hv_create_device_domain(struct hv_iommu_domain
> *hv_domain)
> return hv_result_to_errno(status);
> }
>
> -static void hv_delete_device_domain(struct hv_iommu_domain *hv_domain)
> +static int hv_delete_device_domain(struct hv_iommu_domain *hv_domain)
> {
> u64 status;
> unsigned long flags;
> @@ -122,11 +122,15 @@ static void hv_delete_device_domain(struct
> hv_iommu_domain *hv_domain)
>
> local_irq_restore(flags);
>
> - if (!hv_result_success(status))
> - hv_status_err(status, "HVCALL_DELETE_DEVICE_DOMAIN failed\n");
> + /* If the domain still exists, its ID cannot be reused */
> + if (WARN(!hv_result_success(status),
> + "HVCALL_DELETE_DEVICE_DOMAIN failed: %#llx (%s)\n",
> + status, hv_result_to_string(status)))
> + return hv_result_to_errno(status);
>
> ida_free(&hv_domain->hv_iommu->domain_ids,
> hv_domain->device_domain.domain_id.id);
> + return 0;
> }
>
> static int
> @@ -466,11 +470,16 @@ static void hv_iommu_paging_domain_free(struct
> iommu_domain *domain)
> {
> struct hv_iommu_domain *hv_domain = to_hv_iommu_domain(domain);
>
> + /*
> + * If the hypervisor still has the domain, it may still use its page
> + * tables, so leak them.
> + */
> + if (hv_delete_device_domain(hv_domain))
> + return;
> +
> /* Free all remaining mappings */
> pt_iommu_deinit(&hv_domain->pt_iommu);
>
> - hv_delete_device_domain(hv_domain);
> -
> kfree(hv_domain);
> }
>
> --
> 2.43.0
>
Reviewed-by: Yu Zhang <[email protected]>
B.R.
Yu