Skip to contentSkip to conversation

Hyper-V: root VM iommu kernel only driver

PATCH Superseded Mukesh R 5 participants 15 messages Sep 24 – Sep 28

Subsystems (5)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Mukesh R

author
#0 lore raw

cover Hyper-V: root VM iommu kernel only driver

This short series introduces hyperv root VM iommu kernel only driver. This driver acts as liason between the hypervisor and iommu layer. Details are both in the actual commit and in the comments in the file implementing it.

Thanks,
-Mukesh

V0: based on upstream hyperv-next commit be0cfab740e5

V1:
- change order of lookup tree deletions to appease all AI agents, carving out hv_iommu_hyp_unmap_pages() from hv_iommu_unmap_pages().
- change unique_id to ida subsystem management so we never ever run out of domain ids.
- change hv_max_iova_width to hv_iommu_max_iova and generate proper mask.
- some other minor changes.

Mukesh R (3):
PCI: hv: Export hv_build_devid_type_pci() and change return type
mshv: Import data structs around device domains from hyperv headers x86/hyperv: Implement root VM IOMMU kernel only driver

 arch/x86/hyperv/irqdomain.c          |   9 +-
 arch/x86/include/asm/mshyperv.h      |   6 +
 arch/x86/kernel/pci-dma.c            |   2 +
 drivers/iommu/Kconfig                |   1 +
 drivers/iommu/hyperv/Kconfig         |  15 +
 drivers/iommu/hyperv/Makefile        |   1 +
 drivers/iommu/hyperv/hv-iommu-root.c | 656 +++++++++++++++++++++++++++
 include/asm-generic/mshyperv.h       |   3 +
 include/hyperv/hvgdk_mini.h          |   9 +
 include/hyperv/hvhdk_mini.h          |  87 ++++
 include/linux/hyperv.h               |   6 +
 11 files changed, 791 insertions(+), 4 deletions(-)
 create mode 100644 drivers/iommu/hyperv/Kconfig
 create mode 100644 drivers/iommu/hyperv/hv-iommu-root.c
Signature · 1 line
2.51.2.vfs.0.1
author
#1 lore raw

PATCH 1/3 PCI: hv: Export hv_build_devid_type_pci() and change return type

On Hyper-V, most hypercalls related to device domain management like attach device to a device domain, or to map/unmap regions, interrupts, etc need a device ID as a parameter. So, make hv_build_devid_type_pci() public and change return type to u64 to enforce its size.

---
 arch/x86/hyperv/irqdomain.c     | 9 +++++----
 arch/x86/include/asm/mshyperv.h | 6 ++++++
 2 files changed, 11 insertions(+), 4 deletions(-)
arch/x86/hyperv/irqdomain.c +5 -4
index b3ad50a874dc..8780573a4332 100644
@@ -112,7 +112,7 @@ static int get_rid_cb(struct pci_dev *pdev, u16 alias, void *data)
 	return 0;
 }
 
-static union hv_device_id hv_build_devid_type_pci(struct pci_dev *pdev)
+u64 hv_build_devid_type_pci(struct pci_dev *pdev)
 {
 	int pos;
 	union hv_device_id hv_devid;
@@ -172,8 +172,9 @@ static union hv_device_id hv_build_devid_type_pci(struct pci_dev *pdev)
 	}
 
 out:
-	return hv_devid;
+	return hv_devid.as_uint64;
 }
+EXPORT_SYMBOL_GPL(hv_build_devid_type_pci);
 
 /*
  * hv_map_msi_interrupt() - Map the MSI IRQ in the hypervisor.
@@ -196,7 +197,7 @@ int hv_map_msi_interrupt(struct irq_data *data,
 
 	msidesc = irq_data_get_msi_desc(data);
 	pdev = msi_desc_to_pci_dev(msidesc);
-	hv_devid = hv_build_devid_type_pci(pdev);
+	hv_devid.as_uint64 = hv_build_devid_type_pci(pdev);
 	cpu = cpumask_first(irq_data_get_effective_affinity_mask(data));
 
 	return hv_map_interrupt(hv_devid, false, cpu, cfg->vector,
@@ -271,7 +272,7 @@ static int hv_unmap_msi_interrupt(struct pci_dev *pdev,
 {
 	union hv_device_id hv_devid;
 
-	hv_devid = hv_build_devid_type_pci(pdev);
+	hv_devid.as_uint64 = hv_build_devid_type_pci(pdev);
 	return hv_unmap_interrupt(hv_devid.as_uint64, irq_entry);
 }
 
arch/x86/include/asm/mshyperv.h +6 -0
index f64393e853ee..8ebbd1cb7c8c 100644
@@ -248,6 +248,12 @@ void hv_crash_asm_end(void);
 static inline void hv_root_crash_init(void) {}
 #endif  /* CONFIG_MSHV_ROOT && CONFIG_CRASH_DUMP */
 
+#ifdef CONFIG_PCI_MSI
+u64 hv_build_devid_type_pci(struct pci_dev *pdev);
+#else
+static inline u64 hv_build_devid_type_pci(struct pci_dev *pdev) { return 0; }
+#endif
+
 #else /* CONFIG_HYPERV */
 static inline void hyperv_init(void) {}
 static inline void hyperv_setup_mmu_ops(void) {}
Signature · 1 line
2.51.2.vfs.0.1
author
#2 lore raw

PATCH 2/3 mshv: Import data structs around device domains from hyperv headers

In reply to Mukesh R

Copy/import from Hyper-V public headers, definitions and declarations that are related to creating iommu domains in the hypervisor, attaching devices to them, doing the reverse, etc.

---
 include/hyperv/hvgdk_mini.h |  9 ++++
 include/hyperv/hvhdk_mini.h | 87 +++++++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+)
include/hyperv/hvgdk_mini.h +9 -0
index 6a4e8b9d570f..3b74f3c0229d 100644
@@ -326,6 +326,8 @@ union hv_hypervisor_version_info {
 /* stimer Direct Mode is available */
 #define HV_STIMER_DIRECT_MODE_AVAILABLE			BIT(19)
 
+#define HV_DEVICE_DOMAIN_AVAILABLE			BIT(24)
+
 /*
  * Implementation recommendations. Indicates which behaviors the hypervisor
  * recommends the OS implement for optimal performance.
@@ -486,9 +488,15 @@ union hv_vp_assist_msr_contents {	 /* HV_REGISTER_VP_ASSIST_PAGE */
 #define HVCALL_GET_VP_INDEX_FROM_APIC_ID		0x009a
 #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE	0x00af
 #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_LIST	0x00b0
+#define HVCALL_CREATE_DEVICE_DOMAIN                     0x00b1
+#define HVCALL_ATTACH_DEVICE_DOMAIN                     0x00b2
+#define HVCALL_MAP_DEVICE_GPA_PAGES                     0x00b3
+#define HVCALL_UNMAP_DEVICE_GPA_PAGES                   0x00b4
 #define HVCALL_SIGNAL_EVENT_DIRECT			0x00c0
 #define HVCALL_POST_MESSAGE_DIRECT			0x00c1
 #define HVCALL_DISPATCH_VP				0x00c2
+#define HVCALL_DETACH_DEVICE_DOMAIN                     0x00c4
+#define HVCALL_DELETE_DEVICE_DOMAIN                     0x00c5
 #define HVCALL_GET_GPA_PAGES_ACCESS_STATES		0x00c9
 #define HVCALL_ACQUIRE_SPARSE_SPA_PAGE_HOST_ACCESS	0x00d7
 #define HVCALL_RELEASE_SPARSE_SPA_PAGE_HOST_ACCESS	0x00d8
@@ -502,6 +510,7 @@ union hv_vp_assist_msr_contents {	 /* HV_REGISTER_VP_ASSIST_PAGE */
 #define HVCALL_MMIO_READ				0x0106
 #define HVCALL_MMIO_WRITE				0x0107
 #define HVCALL_DISABLE_HYP_EX                           0x010f
+#define HVCALL_GET_IOMMU_CAPABILITIES			0x0125
 #define HVCALL_MAP_STATS_PAGE2				0x0131
 
 /* HV_HYPERCALL_INPUT */
include/hyperv/hvhdk_mini.h +87 -0

91 lines not on this page · raw message

Signature · 1 line
2.51.2.vfs.0.1
#3 lore raw

Re: [PATCH V1 2/3] mshv: Import data structs around device domains from hyperv headers

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] mshv: incorrect bitfield layout for union hv_device_domain_id corrupts hypercall ABI --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mshv: incorrect bitfield layout for union hv_device_domain_id corrupts hypercall ABI --

commit a37c5c0ca47021bec11c9aa7d2a0eb2626dc10ce
Author: Mukesh R <[email protected]>

mshv: Import data structs around device domains from hyperv headers

Copy/import from Hyper-V public headers, definitions and declarations that are related to creating iommu domains in the hypervisor, attaching devices to them, doing the reverse, etc.

Quoted 4 lines
include/hyperv/hvhdk_mini.h +0 -0
index 035ba20870f73..3dbfb338bcba1 100644

[ ... ]

include/hyperv/hvhdk_mini.h union hv_device_id { /* HV_DEVICE_ID */ · line 564

Quoted 10 lines
include/hyperv/hvhdk_mini.h +10 -0
+#define HV_DEVICE_DOMAIN_ID_S2_NULL    0xFFFFFFFFULL
+
+union hv_device_domain_id {
+	u64 as_uint64;
+	struct {
+		u32 type : 4;
+		u32 reserved : 28;
+		u32 id;
+	};
+} __packed;

[Severity: High]
Does this struct incorrectly define type and reserved before id, placing id in the upper 32 bits of the 64-bit union instead of the lower 32 bits?

The imported constant HV_DEVICE_DOMAIN_ID_S2_NULL is defined as 0xFFFFFFFFULL, which has 0xFFFFFFFF in the lower 32 bits and 0 in the upper 32 bits. This indicates the hypervisor expects the 32-bit id field to be in the lower 32 bits (LSB) and the type to be in the upper 32 bits.

Because C bitfields in little-endian architectures populate the lowest bits first, defining type and reserved before id places the type field in the lower 32 bits and the id field in the upper 32 bits.

Could this layout mismatch cause hypercalls to create or attach device domains to send a corrupted 64-bit domain ID? If the hypervisor misinterprets the domain type and ID, this would lead to hypercall failures and break IOMMU isolation for devices when the driver populates domain_id.type and domain_id.id.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH V1 2/3] mshv: Import data structs around device domains from hyperv headers

In reply to Mukesh R
Quoted 10 lines · Mukesh R

Copy/import from Hyper-V public headers, definitions and declarations that are related to creating iommu domains in the hypervisor, attaching devices to them, doing the reverse, etc.

---
 include/hyperv/hvgdk_mini.h |  9 ++++
 include/hyperv/hvhdk_mini.h | 87 +++++++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+)
author
#5 lore raw

PATCH 3/3 x86/hyperv: Implement root VM IOMMU kernel only driver

In reply to Mukesh R

Add a new file to implement a kernel only virtual IOMMU that works with Microsoft Hyper-V hypervisor (aka MSHV) on privileged VMs aka root VMs. The hypervisor claims the IOMMU upon boot, and this driver communicates with it for creating and deleting paging domains, attaching of devices, mapping and unmapping of pages, etc. During boot, hypervisor automatically creates identity and blocked domains, so there is no need to do hypercalls to create them. This is a kernel only driver and only supported on baremetal root (and not L1VH root) without any guest passthru support. Support for guest device passthru will be added incrementally.

---
 arch/x86/kernel/pci-dma.c            |   2 +
 drivers/iommu/Kconfig                |   1 +
 drivers/iommu/hyperv/Kconfig         |  15 +
 drivers/iommu/hyperv/Makefile        |   1 +
 drivers/iommu/hyperv/hv-iommu-root.c | 656 +++++++++++++++++++++++++++
 include/asm-generic/mshyperv.h       |   3 +
 include/linux/hyperv.h               |   6 +
 7 files changed, 684 insertions(+)
 create mode 100644 drivers/iommu/hyperv/Kconfig
 create mode 100644 drivers/iommu/hyperv/hv-iommu-root.c
arch/x86/kernel/pci-dma.c +2 -0
index 6267363e0189..37fed8c7a8c2 100644
@@ -8,6 +8,7 @@
 #include <linux/gfp.h>
 #include <linux/pci.h>
 #include <linux/amd-iommu.h>
+#include <linux/hyperv.h>
 
 #include <asm/proto.h>
 #include <asm/dma.h>
@@ -103,6 +104,7 @@ void __init pci_iommu_alloc(void)
 	}
 	pci_swiotlb_detect();
 	gart_iommu_hole_init();
+	hv_iommu_detect();
 	amd_iommu_detect();
 	detect_intel_iommu();
 	swiotlb_init(x86_swiotlb_enable, x86_swiotlb_flags);
drivers/iommu/Kconfig +1 -0
index 6e07bd69467a..3e410e0f3e1d 100644
@@ -197,6 +197,7 @@ source "drivers/iommu/arm/Kconfig"
 source "drivers/iommu/intel/Kconfig"
 source "drivers/iommu/iommufd/Kconfig"
 source "drivers/iommu/riscv/Kconfig"
+source "drivers/iommu/hyperv/Kconfig"
 
 config IRQ_REMAP
 	bool "Support for Interrupt Remapping"
drivers/iommu/hyperv/Kconfig new file +15 -0
new file mode 100644
index 000000000000..796845dd16d9
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Hyper-V IOMMU support
+
+config HYPERV_ROOT_IOMMU
+	bool "Hyper-V IOMMU Device in root VM"
+	depends on HYPERV && X86 && PCI_MSI && MSHV_ROOT
+	select IOMMU_API
+	default HYPERV
+	help
+	  This enables Hyper-V pseudo IOMMU device. When running as privileged
+          VM aka root on Microsoft Hyper-V hypervisor, this must be enabled
+          for doing any PCI passthru of devices to guest VMs. This applies to
+          both PFs and VFs. When enabling this, it is best to disable amd/intel
+          iommus via: intel_iommu=off amd_iommu=off as the hypervisor really
+          owns the iommu.
drivers/iommu/hyperv/Makefile +1 -0
index 6ef0ef97f3dd..c7e7d0dac2a4 100644
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_IRQ_REMAP) += hv-irq-remap-x86.o
+obj-$(CONFIG_HYPERV_ROOT_IOMMU) += hv-iommu-root.o
drivers/iommu/hyperv/hv-iommu-root.c new file +656 -0

656 lines not on this page · raw message

include/asm-generic/mshyperv.h +3 -0
index bf601d67cecb..fa8331c7c3a2 100644
@@ -28,6 +28,9 @@
 
 #define VTPM_BASE_ADDRESS 0xfed40000
 
+#define HV_REP_COUNT_MAX        \
+	(HV_HYPERCALL_REP_COMP_MASK >> HV_HYPERCALL_REP_COMP_OFFSET)
+
 enum hv_partition_type {
 	HV_PARTITION_TYPE_GUEST,
 	HV_PARTITION_TYPE_ROOT,
include/linux/hyperv.h +6 -0
index 9e109d91aa14..01a69f88cfa7 100644
@@ -1783,4 +1783,10 @@ static inline unsigned long virt_to_hvpfn(void *addr)
 #define HVPFN_DOWN(x)	((x) >> HV_HYP_PAGE_SHIFT)
 #define page_to_hvpfn(page)	(page_to_pfn(page) * NR_HV_HYP_PAGES_IN_PAGE)
 
+#ifdef CONFIG_HYPERV_ROOT_IOMMU
+void __init hv_iommu_detect(void);
+#else
+static inline void hv_iommu_detect(void) { }
+#endif /* CONFIG_HYPERV_ROOT_IOMMU */
+
 #endif /* _HYPERV_H */
Signature · 1 line
2.51.2.vfs.0.1
#6 lore raw

Re: [PATCH V1 3/3] x86/hyperv: Implement root VM IOMMU kernel only driver

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] hyperv: map_pages causes sleep in atomic context by ignoring gfp flag - [High] hyperv: memory leak…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] hyperv: map_pages causes sleep in atomic context by ignoring gfp flag
- [High] hyperv: memory leak and unmap failures due to unsafe interval tree iteration
- [Medium] hyperv: undefined behavior via 64-bit shift in hv_iommu_init() --

commit a7b3705b617a0eb04fd6acb14622b152cee82c8b
Author: Mukesh R <[email protected]>

x86/hyperv: Implement root VM IOMMU kernel only driver

Add a new file to implement a kernel only virtual IOMMU that works with Microsoft Hyper-V hypervisor (aka MSHV) on privileged VMs aka root VMs. The hypervisor claims the IOMMU upon boot, and this driver communicates with it for creating and deleting paging domains, attaching of devices, mapping and unmapping of pages, etc. During boot, hypervisor automatically creates identity and blocked domains, so there is no need to do hypercalls to create them. This is a kernel only driver and only supported on baremetal root (and not L1VH root) without any guest passthru support. Support for guest device passthru will be added incrementally.

Quoted 6 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +0 -0
new file mode 100644
index 0000000000000..4c6ebaa8fb971
@@ -0,0 +1,656 @@

[ ... ]

Quoted 14 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +14 -0
+static size_t hv_iommu_del_tree_mappings(struct hv_domain *hvdom, ulong iova,
+					 size_t size)
+{
+	ulong flags;
+	size_t unmapped = 0;
+	ulong last = iova + size - 1;
+	struct hv_iommu_mapping *mapping = NULL;
+	struct interval_tree_node *node, *next;
+
+	spin_lock_irqsave(&hvdom->mappings_lock, flags);
+	next = interval_tree_iter_first(&hvdom->mappings_tree, iova, last);
+	while (next) {
+		node = next;
+		mapping = container_of(node, struct hv_iommu_mapping, iova);

drivers/iommu/hyperv/hv-iommu-root.c line 130

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+		next = interval_tree_iter_next(node, iova, last);
+
+		/* Splitting of a mapping is not supported at present */
+		if (mapping->iova.start < iova)
+			break;
+
+		unmapped += mapping->iova.last - mapping->iova.start + 1;
+
+		interval_tree_remove(node, &hvdom->mappings_tree);
+		kfree(mapping);

[Severity: High]
Does this code skip overlapping mappings and leak memory?

This loop removes nodes from an interval tree using interval_tree_remove() while iterating over it using interval_tree_iter_next(). Since interval_tree_remove() alters the tree's structure (rebalances it and updates __subtree_last), invoking interval_tree_iter_next() after the tree has been rebalanced can invalidate the traversal path.

This can cause the traversal to skip remaining overlapping nodes, leaking memory and preventing the hypervisor from unmapping those IOVA pages.

Quoted 5 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +5 -0
+	}
+	spin_unlock_irqrestore(&hvdom->mappings_lock, flags);
+
+	return unmapped;
+}

[ ... ]

Quoted 4 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+static int hv_iommu_map_pages(struct iommu_domain *immdom, ulong iova,
+			      phys_addr_t paddr, size_t pgsize, size_t pgcount,
+			      int prot, gfp_t gfp, size_t *mapped)
+{

[ ... ]

Quoted 8 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +8 -0
+	npages = size >> HV_HYP_PAGE_SHIFT;
+	while (done < npages) {
+		ulong completed, remain = npages - done;
+
+		remain = min(remain, HV_MAP_DEVICE_GPA_BATCH_SIZE);
+
+		status = hv_iommu_map_pgs(hvdom, iova, paddr, remain,
+					  map_flags);

drivers/iommu/hyperv/hv-iommu-root.c line 432

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+
+		completed = hv_repcomp(status);
+		done = done + completed;
+		iova = iova + (completed << HV_HYP_PAGE_SHIFT);
+		paddr = paddr + (completed << HV_HYP_PAGE_SHIFT);
+
+		if (hv_result_needs_memory(status)) {
+			ret = hv_call_deposit_pages(NUMA_NO_NODE,
+						    hv_current_partition_id,
+						    256);

[Severity: High]
Can this sequence cause a "scheduling while atomic" panic?

IOMMU map operations are routinely called from atomic or interrupt contexts, such as DMA mapping via dma_map_single().

The caller-provided gfp flag is ignored here. The code calls hv_call_deposit_pages(), which unconditionally allocates memory using GFP_KERNEL (which can sleep) to satisfy hypervisor page table demands.

Quoted 7 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +7 -0
+			if (ret)
+				break;
+			continue;
+		}
+		if (!hv_result_success(status))
+			break;
+	}

[ ... ]

Quoted 4 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+static int __init hv_iommu_init(void)
+{
+	int rc;
+	struct iommu_device *iommup = &hv_virt_iommu;

drivers/iommu/hyperv/hv-iommu-root.c line 616

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+	struct hv_output_get_iommu_capabilities caps;
+
+	if (!hv_is_hyperv_initialized())
+		return -ENODEV;
+
+	rc = hv_iommu_get_caps(&caps);
+	if (rc)
+		return rc;
+
+	hv_iommu_max_iova = ((ulong)1 << caps.max_iova_width) - 1;

[Severity: Medium]
Will this evaluate to 0 if max_iova_width is 64?

If the hypervisor supports 64-bit addressing and returns caps.max_iova_width as 64, this line will compute (1UL << 64) - 1. On x86-64 architectures, shift amounts are masked modulo 64 (i.e. 64 & 63 = 0), so 1UL << 0 = 1, resulting in 1 - 1 = 0.

This undefined behavior could corrupt the IOMMU geometry's aperture_end and break the driver for hardware devices using high IOVAs.

Quoted 3 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +3 -0
+
+	rc = iommu_device_sysfs_add(iommup, NULL, NULL, "%s", "hyperv-iommu");
+	if (rc) {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer
#7 lore raw

Re: [PATCH V1 3/3] x86/hyperv: Implement root VM IOMMU kernel only driver

In reply to Mukesh R
Quoted 23 lines · Mukesh R
drivers/iommu/hyperv/hv-iommu-root.c new file +23 -0
+static int hv_iommu_map_pages(struct iommu_domain *immdom, ulong iova,
+			      phys_addr_t paddr, size_t pgsize, size_t pgcount,
+			      int prot, gfp_t gfp, size_t *mapped)
+{
+	u32 map_flags;
+	int ret;
+	u64 status = 0;
+	ulong npages, sav_iova = iova, done = 0;
+	struct hv_domain *hvdom = to_hv_domain(immdom);
+	phys_addr_t sav_paddr = paddr;
+	size_t done_size, size = pgsize * pgcount;
+
+	map_flags = HV_MAP_GPA_READABLE;	/* required */
+	map_flags |= prot & IOMMU_WRITE ? HV_MAP_GPA_WRITABLE : 0;
+
+	npages = size >> HV_HYP_PAGE_SHIFT;
+	while (done < npages) {
+		ulong completed, remain = npages - done;
+
+		remain = min(remain, HV_MAP_DEVICE_GPA_BATCH_SIZE);
+
+		status = hv_iommu_map_pgs(hvdom, iova, paddr, remain,
+					  map_flags);

drivers/iommu/hyperv/hv-iommu-root.c line 432

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+
+		completed = hv_repcomp(status);
+		done = done + completed;
+		iova = iova + (completed << HV_HYP_PAGE_SHIFT);
+		paddr = paddr + (completed << HV_HYP_PAGE_SHIFT);
+
+		if (hv_result_needs_memory(status)) {
+			ret = hv_call_deposit_pages(NUMA_NO_NODE,
+						    hv_current_partition_id,
+						    256);

hv_call_deposit_pages() can sleep and must not be called when gfp == GFP_ATOMIC.

Quoted 18 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +18 -0
+			if (ret)
+				break;
+			continue;
+		}
+		if (!hv_result_success(status))
+			break;
+	}
+
+	done_size = done << HV_HYP_PAGE_SHIFT;
+
+	if (mapped)
+		*mapped = done_size;
+
+	if (done_size == 0)
+		return hv_result_to_errno(status);
+
+	ret = hv_iommu_add_tree_mapping(hvdom, sav_iova, sav_paddr, done_size,
+					map_flags);

drivers/iommu/hyperv/hv-iommu-root.c line 460

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+	if (ret) {
+		size = hv_iommu_hyp_unmap_pages(immdom, sav_iova, done_size);
+		if (size != done_size)
+			WARN(1, "Failed to unmap exact sizes(%lx/%lx)\n",
+			     done_size, size);
+		if (mapped)
+			*mapped = done_size - size;
+
+		return ret;
+	}

A general question: Why is the tree mapping added after the hypervisor mappings are established. It seems easier and more robust the other way around. Same on the unmap path which should use strictly the reverse order.

With this order, if adding a tree entry fails and the unmap only partially succeeds, it ends up with a partial mapping which can not be unmapped anymore because there is no tree entry.

drivers/iommu/hyperv/hv-iommu-root.c line 615

Quoted 13 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +13 -0
+static int __init hv_iommu_init(void)
+{
+	int rc;
+	struct iommu_device *iommup = &hv_virt_iommu;
+	struct hv_output_get_iommu_capabilities caps;
+
+	if (!hv_is_hyperv_initialized())
+		return -ENODEV;
+
+	rc = hv_iommu_get_caps(&caps);
+	if (rc)
+		return rc;
+

The capabilities returned need more checking. I think at least it needs a check for HV_IOMMU_CAP_PRESENT and that PAGE_SIZE is set in the pgsize_bitmap.

drivers/iommu/hyperv/hv-iommu-root.c line 616

Quoted 1 line
drivers/iommu/hyperv/hv-iommu-root.c new file +1 -0
+	hv_iommu_max_iova = ((ulong)1 << caps.max_iova_width) - 1;

This is undefined behavior if caps.max_iova_width == 64. Better use DMA_BIT_MASK().

author
#8 lore raw

Re: [PATCH V1 3/3] x86/hyperv: Implement root VM IOMMU kernel only driver

Quoted 28 lines · J?rg R?del
Quoted 27 lines · Mukesh R
drivers/iommu/hyperv/hv-iommu-root.c new file +27 -0
+static int hv_iommu_map_pages(struct iommu_domain *immdom, ulong iova,
+			      phys_addr_t paddr, size_t pgsize, size_t pgcount,
+			      int prot, gfp_t gfp, size_t *mapped)
+{
+	u32 map_flags;
+	int ret;
+	u64 status = 0;
+	ulong npages, sav_iova = iova, done = 0;
+	struct hv_domain *hvdom = to_hv_domain(immdom);
+	phys_addr_t sav_paddr = paddr;
+	size_t done_size, size = pgsize * pgcount;
+
+	map_flags = HV_MAP_GPA_READABLE;	/* required */
+	map_flags |= prot & IOMMU_WRITE ? HV_MAP_GPA_WRITABLE : 0;
+
+	npages = size >> HV_HYP_PAGE_SHIFT;
+	while (done < npages) {
+		ulong completed, remain = npages - done;
+
+		remain = min(remain, HV_MAP_DEVICE_GPA_BATCH_SIZE);
+
+		status = hv_iommu_map_pgs(hvdom, iova, paddr, remain,
+					  map_flags);
+
+		completed = hv_repcomp(status);
+		done = done + completed;
+		iova = iova + (completed << HV_HYP_PAGE_SHIFT);
Quoted 8 lines

drivers/iommu/hyperv/hv-iommu-root.c line 432

Quoted 6 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +6 -0
+		paddr = paddr + (completed << HV_HYP_PAGE_SHIFT);
+
+		if (hv_result_needs_memory(status)) {
+			ret = hv_call_deposit_pages(NUMA_NO_NODE,
+						    hv_current_partition_id,
+						    256);

hv_call_deposit_pages() can sleep and must not be called when gfp == GFP_ATOMIC.

yes, indeed, i missed it. will address it...

Quoted 33 lines
Quoted 18 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +18 -0
+			if (ret)
+				break;
+			continue;
+		}
+		if (!hv_result_success(status))
+			break;
+	}
+
+	done_size = done << HV_HYP_PAGE_SHIFT;
+
+	if (mapped)
+		*mapped = done_size;
+
+	if (done_size == 0)
+		return hv_result_to_errno(status);
+
+	ret = hv_iommu_add_tree_mapping(hvdom, sav_iova, sav_paddr, done_size,
+					map_flags);

drivers/iommu/hyperv/hv-iommu-root.c line 460

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+	if (ret) {
+		size = hv_iommu_hyp_unmap_pages(immdom, sav_iova, done_size);
+		if (size != done_size)
+			WARN(1, "Failed to unmap exact sizes(%lx/%lx)\n",
+			     done_size, size);
+		if (mapped)
+			*mapped = done_size - size;
+
+		return ret;
+	}

A general question: Why is the tree mapping added after the hypervisor mappings are established. It seems easier and more robust the other way around. Same on the unmap path which should use strictly the reverse order.

Quoted 3 lines

With this order, if adding a tree entry fails and the unmap only partially succeeds, it ends up with a partial mapping which can not be unmapped anymore because there is no tree entry.

well, it was before in V0, but sashiko/copilot/claude/.. you name it all bots jumped on me that it would cause tree to go out of sync if map fails to map everything, since removal from tree then will remove the entire node in the cleanup path, thus leaving mappings in the hyp that are not in the tree. In this case above, we only add to the tree whatever succeeded. Problem at high level is, any of the two can fail, and undoing could also fail, sorta chicken and egg issue.. We discussed internally and thought adding WARN() would allow one to catch and maybe panic in those cases. I also noticed that other drivers also have similar issues and viommu_map_pages() just ignores if viommu_del_mappings() fails, and fails the callback. We fail the callback here also. My V0 did same as viommu_map_pages(), difference being, in our case, a hypercall can fail with partially done list.

So, I'm at a bit of loss. I could go back to V0 which did what what you suggested above, but it can also leave stale mappings when map hypercall succeeds partially and we delete the entire tree node in cleanup (but we could add to the tree back whatever succeeded, but that could fail also, but then we could unmap whatever was mapped, but that could fail also.. see :)...).

Open to any other suggestions you might have also.

Quoted 8 lines
Quoted 8 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +8 -0
+static int __init hv_iommu_init(void)
+{
+	int rc;
+	struct iommu_device *iommup = &hv_virt_iommu;
+	struct hv_output_get_iommu_capabilities caps;
+
+	if (!hv_is_hyperv_initialized())
+		return -ENODEV;
Quoted 8 lines

drivers/iommu/hyperv/hv-iommu-root.c line 615

Quoted 5 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +5 -0
+
+	rc = hv_iommu_get_caps(&caps);
+	if (rc)
+		return rc;
+

The capabilities returned need more checking. I think at least it needs a check for HV_IOMMU_CAP_PRESENT and that PAGE_SIZE is set in the pgsize_bitmap.

In general this caps hypercall is not available to root partitions, our only agreement is for hyp to provide max_iova_width. But, we check HV_DEVICE_DOMAIN_AVAILABLE in hv_iommu_detect() which sorta supersedes HV_IOMMU_CAP_PRESENT. As for page size, hyp owns the iommu and can chose the page size, meaning it can automatically use larger page size when possible. But the hypercall HVCALL_MAP_DEVICE_GPA_PAGES will only take 4k pfns as input, hence:

#define HV_IOMMU_PGSIZES SZ_4K /* for now, to be enhanced */

Quoted 2 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	hv_iommu_max_iova = ((ulong)1 << caps.max_iova_width) - 1; >

This is undefined behavior if caps.max_iova_width == 64. Better use

DMA_BIT_MASK().

Ok, I can change it back to DMA_BIT_MASK. (I removed after comment on V0 that this is not dma address).

Thanks a lot for the review.

-Mukesh

maintainer
#9 lore raw

Re: [PATCH V1 3/3] x86/hyperv: Implement root VM IOMMU kernel only driver

Quoted 7 lines · Mukesh R

So, I'm at a bit of loss. I could go back to V0 which did what what you suggested above, but it can also leave stale mappings when map hypercall succeeds partially and we delete the entire tree node in cleanup (but we could add to the tree back whatever succeeded, but that could fail also, but then we could unmap whatever was mapped, but that could fail also.. see :)...).

The root-problem seems to be that every operation in the step can fail, inluding the unmap operation. This makes it really hard to maintain a consistent state. Are there any guarantees Hyper-V gives on the unmap operation, e.g under which circumstances it can fail? It would really help if the code could assume that it will never fail, or treat a failure as a hard error.

If there are no guarantees HV can give, a solution might be to add a mapping-resize operation to the internal tree (which can not fail) to correctly update the tree structure on partially successful unmaps.

I still believe that the internal tree structure should be updated before the HV map operation takes place.

Quoted 25 lines
Quoted 8 lines
Quoted 8 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +8 -0
+static int __init hv_iommu_init(void)
+{
+	int rc;
+	struct iommu_device *iommup = &hv_virt_iommu;
+	struct hv_output_get_iommu_capabilities caps;
+
+	if (!hv_is_hyperv_initialized())
+		return -ENODEV;
Quoted 8 lines

drivers/iommu/hyperv/hv-iommu-root.c line 615

Quoted 5 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +5 -0
+
+	rc = hv_iommu_get_caps(&caps);
+	if (rc)
+		return rc;
+

The capabilities returned need more checking. I think at least it needs a check for HV_IOMMU_CAP_PRESENT and that PAGE_SIZE is set in the pgsize_bitmap.

In general this caps hypercall is not available to root partitions, our only agreement is for hyp to provide max_iova_width. But, we check HV_DEVICE_DOMAIN_AVAILABLE in hv_iommu_detect() which sorta supersedes HV_IOMMU_CAP_PRESENT. As for page size, hyp owns the iommu and can chose the page size, meaning it can automatically use larger page size when possible. But the hypercall HVCALL_MAP_DEVICE_GPA_PAGES will only take 4k pfns as input, hence:

Quoted 1 line

#define HV_IOMMU_PGSIZES SZ_4K /* for now, to be enhanced */

Okay, so this deserves a comment in the code to make it clear why there is no additional checking and what the hyper-v to guest contract for feature detection is.

Quoted 2 lines

Ok, I can change it back to DMA_BIT_MASK. (I removed after comment on V0 that this is not dma address).

An IOVA is a DMA address by definition :)

-Joerg

author
#10 lore raw

Re: [PATCH V1 3/3] x86/hyperv: Implement root VM IOMMU kernel only driver

Quoted 9 lines · J?rg R?del
Quoted 7 lines · Mukesh R

So, I'm at a bit of loss. I could go back to V0 which did what what you suggested above, but it can also leave stale mappings when map hypercall succeeds partially and we delete the entire tree node in cleanup (but we could add to the tree back whatever succeeded, but that could fail also, but then we could unmap whatever was mapped, but that could fail also.. see :)...).

Quoted 6 lines

The root-problem seems to be that every operation in the step can fail, inluding the unmap operation. This makes it really hard to maintain a consistent state. Are there any guarantees Hyper-V gives on the unmap operation, e.g under which circumstances it can fail? It would really help if the code could assume that it will never fail, or treat a failure as a hard error.

I talked to the hyp dev, and unmap is guaranteed to succeed unless the input is bad, even suggesting i could put a bug there. But i know there is aversion to putting lot of BUG()s, so i'll just leave the WARN there.

Quoted 3 lines

If there are no guarantees HV can give, a solution might be to add a mapping-resize operation to the internal tree (which can not fail) to correctly update the tree structure on partially successful unmaps.

Yeah, perhaps we could do that in future to benefit all drivers.

Quoted 2 lines

I still believe that the internal tree structure should be updated before the HV map operation takes place.

Yes, i'll move that before map.

Thanks again,
-Mukesh

Quoted 37 lines
Quoted 25 lines
Quoted 8 lines
Quoted 8 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +8 -0
+static int __init hv_iommu_init(void)
+{
+	int rc;
+	struct iommu_device *iommup = &hv_virt_iommu;
+	struct hv_output_get_iommu_capabilities caps;
+
+	if (!hv_is_hyperv_initialized())
+		return -ENODEV;
Quoted 8 lines

drivers/iommu/hyperv/hv-iommu-root.c line 615

Quoted 5 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +5 -0
+
+	rc = hv_iommu_get_caps(&caps);
+	if (rc)
+		return rc;
+

The capabilities returned need more checking. I think at least it needs a check for HV_IOMMU_CAP_PRESENT and that PAGE_SIZE is set in the pgsize_bitmap.

In general this caps hypercall is not available to root partitions, our only agreement is for hyp to provide max_iova_width. But, we check HV_DEVICE_DOMAIN_AVAILABLE in hv_iommu_detect() which sorta supersedes HV_IOMMU_CAP_PRESENT. As for page size, hyp owns the iommu and can chose the page size, meaning it can automatically use larger page size when possible. But the hypercall HVCALL_MAP_DEVICE_GPA_PAGES will only take 4k pfns as input, hence:

Quoted 1 line

#define HV_IOMMU_PGSIZES SZ_4K /* for now, to be enhanced */

Okay, so this deserves a comment in the code to make it clear why there is no additional checking and what the hyper-v to guest contract for feature detection is.

Quoted 2 lines

Ok, I can change it back to DMA_BIT_MASK. (I removed after comment on V0 that this is not dma address).

An IOVA is a DMA address by definition :)

-Joerg

#11 lore raw

Re: [PATCH V1 3/3] x86/hyperv: Implement root VM IOMMU kernel only driver

In reply to Mukesh R
Quoted 4 lines

[ ... 103 lines skipped ... ]

drivers/iommu/hyperv/hv-iommu-root.c new file +3 -0
+/*
+ * We will not claim these PCI devices. Eg hypervisor debugger is using it
+ * for a dynamic debug session. They cannot be enumerated under static ACPI

drivers/iommu/hyperv/hv-iommu-root.c line 26

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+ * device scope.
+ */
+static char *hv_skip_pci_devs;
+static int __init hv_iommu_setup_skip(char *str)
+{
+	hv_skip_pci_devs = str;
+	return 1;
+}
+/* Eg: hv_iommu_skip=(SSSS:BB:DD.F)(SSSS:BB:DD.F) */
+__setup("hv_iommu_skip=", hv_iommu_setup_skip);

I don't like this in a driver. This isn't really skipping anything, it is just leaving some devices in an identity mode.

If you have a use case for this as a general command line policy then come with a core code enhacmenet so everyone can choose per-device their boot time mode.

drivers/iommu/hyperv/hv-iommu-root.c line 38

Quoted 6 lines

[ ... 7 lines skipped ... ]

drivers/iommu/hyperv/hv-iommu-root.c new file +5 -0
+struct hv_domain {
+	struct iommu_domain iommu_dom;
+	u32 domid_num;			      /* as opposed to domain_id.type */
+	spinlock_t mappings_lock;	      /* protects mappings_tree */
+	struct rb_root_cached mappings_tree;  /* iova to pa lookup tree */

This seems basically identical to what virtio-iommu is doing, can you consider sharing its code?

drivers/iommu/hyperv/hv-iommu-root.c line 68

Quoted 5 lines

[ ... 26 lines skipped ... ]

drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+static bool hv_special_domain(struct hv_domain *hvdom)
+{
+	return hvdom == &hv_def_identity_dom || hvdom == &hv_def_blocked_dom;
+}

This is only called by hv_iommu_domain_free() which is only linked to hv_paging_domain_ops(), so it should be dead code

drivers/iommu/hyperv/hv-iommu-root.c line 278

Quoted 7 lines

[ ... 204 lines skipped ... ]

drivers/iommu/hyperv/hv-iommu-root.c new file +6 -0
+static int hv_iommu_attach_dev(struct iommu_domain *immdom, struct device *dev,
+			       struct iommu_domain *old)
+{
+	struct pci_dev *pdev;
+	int rc;
+	struct hv_domain *hvdom_new = to_hv_domain(immdom);

'new' is an odd variable name here

drivers/iommu/hyperv/hv-iommu-root.c line 528

Quoted 5 lines

[ ... 246 lines skipped ... ]

drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+static struct iommu_group *hv_iommu_device_group(struct device *dev)
+{
+	return pci_device_group(dev);
+}

No need for a wrapper, use the function directly in the ops

drivers/iommu/hyperv/hv-iommu-root.c line 537

Quoted 9 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +9 -0
+
+static void hv_iommu_get_resv_regions(struct device *dev,
+				      struct list_head *head)
+{
+	struct iommu_resv_region *reg;
+
+	/* reserve the entire LAPIC region */
+	reg = iommu_alloc_resv_region(0xfee00000, SZ_1M, 0, IOMMU_RESV_MSI,
+				      GFP_KERNEL);

There was some discussion to make a helper for this, I don't see it merged yet..

Quoted 16 lines

[ ... 65 lines skipped ... ]

drivers/iommu/hyperv/hv-iommu-root.c new file +15 -0
+static int __init hv_iommu_init(void)
+{
+	int rc;
+	struct iommu_device *iommup = &hv_virt_iommu;
+	struct hv_output_get_iommu_capabilities caps;
+
+	if (!hv_is_hyperv_initialized())
+		return -ENODEV;
+
+	rc = hv_iommu_get_caps(&caps);
+	if (rc)
+		return rc;
+
+	hv_iommu_max_iova = ((ulong)1 << caps.max_iova_width) - 1;
+

drivers/iommu/hyperv/hv-iommu-root.c line 627

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+	rc = iommu_device_sysfs_add(iommup, NULL, NULL, "%s", "hyperv-iommu");
+	if (rc) {
+		pr_err("Hyper-V: iommu_device_sysfs_add failed: %d\n", rc);
+		return rc;
+	}
+
+	/* This must come before iommu_device_register() because the latter
+	 * calls into the hooks.
+	 */
+	hv_initialize_special_domains();

This probably should be before doing anything with sysfs.

Signature · 1 line
Jason
#12 lore raw

Re: [PATCH V1 3/3] x86/hyperv: Implement root VM IOMMU kernel only driver

Quoted 2 lines · Jason Gunthorpe
Quoted 2 lines

[ ... 103 lines skipped ... ]
+/*

<snip>

Quoted 14 lines

drivers/iommu/hyperv/hv-iommu-root.c line 537

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +9 -0
+
+static void hv_iommu_get_resv_regions(struct device *dev,
+				      struct list_head *head)
+{
+	struct iommu_resv_region *reg;
+
+	/* reserve the entire LAPIC region */
+	reg = iommu_alloc_resv_region(0xfee00000, SZ_1M, 0, IOMMU_RESV_MSI,
+				      GFP_KERNEL);

There was some discussion to make a helper for this, I don't see it merged yet..
<snip>

Hi Jason,

That's patch 3 of the Hyper-V guest IOMMU driver series here:

https://lore.kernel.org/all/[email protected]/T/#m248fd782be21c7fdb79c97b1764658e6c3c5f623

That's awaiting an ack from you and Joerg to be picked up for the next merge window or additional feedback for the past couple of weeks.

Thanks,
Easwar (he/him)

author
#13 lore raw

Re: [PATCH V1 3/3] x86/hyperv: Implement root VM IOMMU kernel only driver

In reply to Jason Gunthorpe
Quoted 9 lines · Jason Gunthorpe
Quoted 9 lines

[ ... 103 lines skipped ... ]

drivers/iommu/hyperv/hv-iommu-root.c new file +8 -0
+/*
+ * We will not claim these PCI devices. Eg hypervisor debugger is using it
+ * for a dynamic debug session. They cannot be enumerated under static ACPI
+ * device scope.
+ */
+static char *hv_skip_pci_devs;
+static int __init hv_iommu_setup_skip(char *str)
+{
Quoted 8 lines

drivers/iommu/hyperv/hv-iommu-root.c line 26

Quoted 5 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +5 -0
+	hv_skip_pci_devs = str;
+	return 1;
+}
+/* Eg: hv_iommu_skip=(SSSS:BB:DD.F)(SSSS:BB:DD.F) */
+__setup("hv_iommu_skip=", hv_iommu_setup_skip);

I don't like this in a driver. This isn't really skipping anything, it is just leaving some devices in an identity mode.

Ok. I talked to the original author of that and i can just remove it. It's mostly for running hyp debugger and we can just carry the patch internally, at least for now.

Quoted 4 lines

If you have a use case for this as a general command line policy then come with a core code enhacmenet so everyone can choose per-device their boot time mode.

Quoted 9 lines

drivers/iommu/hyperv/hv-iommu-root.c line 38

Quoted 6 lines

[ ... 7 lines skipped ... ]

drivers/iommu/hyperv/hv-iommu-root.c new file +5 -0
+struct hv_domain {
+	struct iommu_domain iommu_dom;
+	u32 domid_num;			      /* as opposed to domain_id.type */
+	spinlock_t mappings_lock;	      /* protects mappings_tree */
+	struct rb_root_cached mappings_tree;  /* iova to pa lookup tree */

This seems basically identical to what virtio-iommu is doing, can you consider sharing its code?

yeah, the tree part is somewhat identical, but virtio-iommu has extra fields that we don't need. overall, i don't think there is enough here to refactor, just few lines of code around add/remove calling kernel interval tree apis. moreover, if hyp can provide the lookup in future, i'd like to just remove it from here.

Quoted 9 lines

drivers/iommu/hyperv/hv-iommu-root.c line 68

Quoted 5 lines

[ ... 26 lines skipped ... ]

drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+static bool hv_special_domain(struct hv_domain *hvdom)
+{
+	return hvdom == &hv_def_identity_dom || hvdom == &hv_def_blocked_dom;
+}

This is only called by hv_iommu_domain_free() which is only linked to hv_paging_domain_ops(), so it should be dead code

Quoted 9 lines

drivers/iommu/hyperv/hv-iommu-root.c line 278

Quoted 7 lines

[ ... 204 lines skipped ... ]

drivers/iommu/hyperv/hv-iommu-root.c new file +6 -0
+static int hv_iommu_attach_dev(struct iommu_domain *immdom, struct device *dev,
+			       struct iommu_domain *old)
+{
+	struct pci_dev *pdev;
+	int rc;
+	struct hv_domain *hvdom_new = to_hv_domain(immdom);

'new' is an odd variable name here

'current' is passed as 'old', so we are moving from old to new i thought. please tell me what would you like it called, thx.

Quoted 7 lines

drivers/iommu/hyperv/hv-iommu-root.c line 528

Quoted 5 lines

[ ... 246 lines skipped ... ]

drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+static struct iommu_group *hv_iommu_device_group(struct device *dev)
+{
+	return pci_device_group(dev);
+}

No need for a wrapper, use the function directly in the ops

it helps with quick debug... just set breakpoint in hv_iommu_device_group or add a printk here at the cost of one jmp instruction. but whatever.. i can remove it if it helps move this forward.

Quoted 4 lines
Quoted 4 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+
+static void hv_iommu_get_resv_regions(struct device *dev,
+				      struct list_head *head)
+{
Quoted 8 lines

drivers/iommu/hyperv/hv-iommu-root.c line 537

Quoted 5 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +5 -0
+	struct iommu_resv_region *reg;
+
+	/* reserve the entire LAPIC region */
+	reg = iommu_alloc_resv_region(0xfee00000, SZ_1M, 0, IOMMU_RESV_MSI,
+				      GFP_KERNEL);

There was some discussion to make a helper for this, I don't see it merged yet..

we are both waiting on each other, whoever goes first will leave the follower to address it i guess. i cannot test without this and i am not sure if that series will merge first or this.

Quoted 20 lines
Quoted 20 lines

[ ... 65 lines skipped ... ]

drivers/iommu/hyperv/hv-iommu-root.c new file +19 -0
+static int __init hv_iommu_init(void)
+{
+	int rc;
+	struct iommu_device *iommup = &hv_virt_iommu;
+	struct hv_output_get_iommu_capabilities caps;
+
+	if (!hv_is_hyperv_initialized())
+		return -ENODEV;
+
+	rc = hv_iommu_get_caps(&caps);
+	if (rc)
+		return rc;
+
+	hv_iommu_max_iova = ((ulong)1 << caps.max_iova_width) - 1;
+
+	rc = iommu_device_sysfs_add(iommup, NULL, NULL, "%s", "hyperv-iommu");
+	if (rc) {
+		pr_err("Hyper-V: iommu_device_sysfs_add failed: %d\n", rc);
+		return rc;
Quoted 8 lines

drivers/iommu/hyperv/hv-iommu-root.c line 627

Quoted 6 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +6 -0
+	}
+
+	/* This must come before iommu_device_register() because the latter
+	 * calls into the hooks.
+	 */
+	hv_initialize_special_domains();

This probably should be before doing anything with sysfs.

ok.

Thanks,
-Mukesh

#14 lore raw

Re: [PATCH V1 3/3] x86/hyperv: Implement root VM IOMMU kernel only driver

Quoted 12 lines · Mukesh R
Quoted 11 lines

drivers/iommu/hyperv/hv-iommu-root.c line 537

Quoted 8 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +8 -0
+static void hv_iommu_get_resv_regions(struct device *dev,
+				      struct list_head *head)
+{
+	struct iommu_resv_region *reg;
+
+	/* reserve the entire LAPIC region */
+	reg = iommu_alloc_resv_region(0xfee00000, SZ_1M, 0, IOMMU_RESV_MSI,
+				      GFP_KERNEL);

There was some discussion to make a helper for this, I don't see it merged yet..

Quoted 3 lines

we are both waiting on each other, whoever goes first will leave the follower to address it i guess. i cannot test without this and i am not sure if that series will merge first or this.

Maybe include those patches in this series too?

I think this is fine it should be able to go after you repost it with the minor changes and the clarification on the unmap behavior

Jason