Linux-HyperV List
 help / color / mirror / Atom feed
* [PATCH v3 0/2] iommu/hyperv: Don't free a paging domain the hypervisor did not delete
@ 2026-11-23  6:01 Yuho Choi
  2026-11-23  6:01 ` [PATCH v3 1/2] " Yuho Choi
  2026-11-23  6:01 ` [PATCH v3 2/2] iommu/hyperv: Keep a new domain's page tables if it cannot be deleted Yuho Choi
  0 siblings, 2 replies; 3+ messages in thread
From: Yuho Choi @ 2026-11-23  6:01 UTC (permalink / raw)
  To: Joerg Roedel (AMD), Will Deacon, Wei Liu
  Cc: Robin Murphy, Haiyang Zhang, Dexuan Cui, Yu Zhang,
	Easwar Hariharan, Jacob Pan, Michael Kelley, Jason Gunthorpe,
	iommu, linux-hyperv, linux-kernel, Yuho Choi

The Hyper-V IOMMU driver frees a paging domain's page tables even when
HVCALL_DELETE_DEVICE_DOMAIN fails and the hypervisor still has the
domain. Patch 1 fixes this when a domain is freed. Patch 2 handles the
same failure when the allocation of a new domain fails after the domain
has been created. As Yu pointed out, the current Hyper-V implementation
does not adopt the page table root in that case, so patch 2 is
defensive.

Changes since v2:
- Patch 1: Added Yu's Reviewed-by. No other change.
- Patch 2: Reworded the commit message and the comment to say that the
  current Hyper-V implementation does not adopt the page table root
  when HVCALL_CONFIGURE_DEVICE_DOMAIN fails, and that the change is
  defensive (Yu). Dropped the Fixes tag for the same reason. Added the
  Reviewed-by tags from Jason and Easwar. No code change other than
  the comment.
- Rebased on current mainline (8dac6df92a2e).

Both are compile-tested only (x86_64 with HYPERV and HYPERV_PVIOMMU,
W=1). I have no Hyper-V guest with the para-virtualized IOMMU to test
them on.

v2: https://lore.kernel.org/all/20261112195710.1417744-4-oss.patchbox@gmail.com/
v1: https://lore.kernel.org/all/20261105192738.70664-2-oss.patchbox@gmail.com/

Yuho Choi (2):
  iommu/hyperv: Don't free a paging domain the hypervisor did not delete
  iommu/hyperv: Keep a new domain's page tables if it cannot be deleted

 drivers/iommu/hyperv/hv-iommu-guest.c | 34 ++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 6 deletions(-)


base-commit: 8dac6df92a2e66e2098bbc69061964cfe7e8375b
-- 
2.56.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v3 1/2] iommu/hyperv: Don't free a paging domain the hypervisor did not delete
  2026-11-23  6:01 [PATCH v3 0/2] iommu/hyperv: Don't free a paging domain the hypervisor did not delete Yuho Choi
@ 2026-11-23  6:01 ` Yuho Choi
  2026-11-23  6:01 ` [PATCH v3 2/2] iommu/hyperv: Keep a new domain's page tables if it cannot be deleted Yuho Choi
  1 sibling, 0 replies; 3+ messages in thread
From: Yuho Choi @ 2026-11-23  6:01 UTC (permalink / raw)
  To: Joerg Roedel (AMD), Will Deacon, Wei Liu
  Cc: Robin Murphy, Haiyang Zhang, Dexuan Cui, Yu Zhang,
	Easwar Hariharan, Jacob Pan, Michael Kelley, Jason Gunthorpe,
	iommu, linux-hyperv, linux-kernel, Yuho Choi, Jason Gunthorpe

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 <easwar.hariharan@linux.microsoft.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Yu Zhang <zhangyu1@linux.microsoft.com>
Signed-off-by: Yuho Choi <oss.patchbox@gmail.com>
---
 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.56.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH v3 2/2] iommu/hyperv: Keep a new domain's page tables if it cannot be deleted
  2026-11-23  6:01 [PATCH v3 0/2] iommu/hyperv: Don't free a paging domain the hypervisor did not delete Yuho Choi
  2026-11-23  6:01 ` [PATCH v3 1/2] " Yuho Choi
@ 2026-11-23  6:01 ` Yuho Choi
  1 sibling, 0 replies; 3+ messages in thread
From: Yuho Choi @ 2026-11-23  6:01 UTC (permalink / raw)
  To: Joerg Roedel (AMD), Will Deacon, Wei Liu
  Cc: Robin Murphy, Haiyang Zhang, Dexuan Cui, Yu Zhang,
	Easwar Hariharan, Jacob Pan, Michael Kelley, Jason Gunthorpe,
	iommu, linux-hyperv, linux-kernel, Yuho Choi, Jason Gunthorpe

When hv_iommu_domain_alloc_paging() fails to configure a new device
domain, hv_create_configure_device_domain() deletes the domain again,
and the caller then frees the page tables with pt_iommu_deinit(). The
page tables are freed even if the delete fails and the hypervisor
still has the domain.

The current Hyper-V implementation does not adopt the page table root
when HVCALL_CONFIGURE_DEVICE_DOMAIN fails, so the page tables are not
referenced in this case today. This change is defensive: it avoids
relying on that while the hypervisor still has the domain.

Create and configure the domain in hv_iommu_domain_alloc_paging()
itself, and leak the page tables and the domain when the delete fails,
as hv_iommu_paging_domain_free() does.

Reported-by: Jacob Pan <jacob.pan@linux.microsoft.com>
Closes: https://lore.kernel.org/all/20261105141427.00005946@linux.microsoft.com/
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Easwar Hariharan <easwar.hariharan@linux.microsoft.com>
Signed-off-by: Yuho Choi <oss.patchbox@gmail.com>
---
 drivers/iommu/hyperv/hv-iommu-guest.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/drivers/iommu/hyperv/hv-iommu-guest.c b/drivers/iommu/hyperv/hv-iommu-guest.c
index aa1db9db1e97..0110a1529a23 100644
--- a/drivers/iommu/hyperv/hv-iommu-guest.c
+++ b/drivers/iommu/hyperv/hv-iommu-guest.c
@@ -540,10 +540,23 @@ static struct iommu_domain *hv_iommu_domain_alloc_paging(struct device *dev)
 	settings.page_table_root = pt_info.gcr3_pt;
 	settings.flags.first_stage_paging_mode = pt_info.levels == 5;
 
-	ret = hv_create_configure_device_domain(hv_domain, &settings);
+	ret = hv_create_device_domain(hv_domain);
 	if (ret)
 		goto err_pt_deinit;
 
+	ret = hv_configure_device_domain(hv_domain, &settings);
+	if (ret) {
+		/*
+		 * The current hypervisor does not adopt the page table root
+		 * when the configure call fails. Still, keep the page tables
+		 * while it has the domain, as hv_iommu_paging_domain_free()
+		 * does.
+		 */
+		if (hv_delete_device_domain(hv_domain))
+			return ERR_PTR(ret);
+		goto err_pt_deinit;
+	}
+
 	return &hv_domain->domain;
 
 err_pt_deinit:
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-23  6:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23  6:01 [PATCH v3 0/2] iommu/hyperv: Don't free a paging domain the hypervisor did not delete Yuho Choi
2026-11-23  6:01 ` [PATCH v3 1/2] " Yuho Choi
2026-11-23  6:01 ` [PATCH v3 2/2] iommu/hyperv: Keep a new domain's page tables if it cannot be deleted Yuho Choi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox