iommu/hyperv: Don't free a paging domain the hypervisor did not delete
Subsystems (3)
v3 cover iommu/hyperv: Don't free a paging domain the hypervisor did not delete
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/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
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
Signature · 1 line
2.56.1
PATCH v3 1/2 iommu/hyperv: Don't free a paging domain the hypervisor did not delete
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.
--- drivers/iommu/hyperv/hv-iommu-guest.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
drivers/iommu/hyperv/hv-iommu-guest.c +14 -5
@@ -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); }
Signature · 1 line
2.56.1
PATCH v3 2/2 iommu/hyperv: Keep a new domain's page tables if it cannot be deleted
In reply to Yuho ChoiWhen 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.
--- drivers/iommu/hyperv/hv-iommu-guest.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/iommu/hyperv/hv-iommu-guest.c +14 -1
@@ -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:
Signature · 1 line
2.56.1
Re: [PATCH v3 2/2] iommu/hyperv: Keep a new domain's page tables if it cannot be deleted
Quoted 56 lines · Yuho Choi
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 withpt_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_DOMAINfails, 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, ashv_iommu_paging_domain_free()does.Reported-by Jacob Pan <[email protected]>Reviewed-by Jason Gunthorpe <[email protected]>Reviewed-by Easwar Hariharan <[email protected]>Signed-off-by Yuho Choi <[email protected]>--- drivers/iommu/hyperv/hv-iommu-guest.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)drivers/iommu/hyperv/hv-iommu-guest.c +14
-1@@ -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:Signature · 1 line
2.56.1
B.R.
Yu