Skip to contentSkip to conversation

PCI passthru on Hyper-V (Part I)

PATCH Superseded Mukesh R 8 participants 34 messages May 12 – May 21

Subsystems (7)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Mukesh R

author
#0 lore raw

v3 cover PCI passthru on Hyper-V (Part I)

Implement passthru of PCI devices to unprivileged virtual machines (VMs) when Linux is running as a privileged VM on Microsoft Hyper-V hypervisor. This support is made to fit within the workings of VFIO framework, and any VMM needing to use it must use the VFIO subsystem. This supports both full device passthru and SR-IOV based VFs.

At a high level, the hypervisor supports traditional mapped iommu domains that use explicit map and unmap hypercalls for mapping and unmapping guest RAM into the iommu subsystem. Hyper-V also has a concept of direct attach devices whereby the iommu subsystem simply uses the guest HW page table (ept/npt/..). This series adds support for both, and both are made to work with the VFIO subsystem.

While this Part I focuses on memory mappings, Part II focuses on irq remapping and irq migrations.

This series rebased to: 5170a82e8921 (origin/hyperv-next)

Testing:
o Most testing done on hyperv-next:e733a9e28180 using Cloud Hypervisor (51).
o Limited testing on : 5170a82e8921
o Tested with impending Part II irq patches.
o All tests involved passthru of devices using MSIx.
o Following combinations were tested doing PF passthru:
- L1VH(1): test 1: Mellanox ConnectX-6 Lx passthru
test 2: NVIDIA Tesla Tesla T4 GPU.
test 3: Both of above simultaneous passthru
- Baremetal dom0/root: All of above.
o VF: Mellanox ConnectX-6 Lx passthru on baremetal dom0/root.

(1) L1VH: this is a semi privileged VM that runs on Windows root on Hyper-V, and allows users to create more child VMs.

This series strives to establish a base line. Some pending work items:
o arm64 : some delta to make this work on arm64 (in progress).
o Qemu and OpenVMM support (in progress).
o Further VF testing on l1vh
o device sleep/wakeup.
o More stress testing with high end GPUs

Changes in V3:
o patch #8: fix compiler issues incase of !CONFIG_HYPERV. Also, do forward declaration of struct pci_dev instead of including pci.h.
o patch #9: minor changes to comments. Pass hv_domain instead of iommu_domain to hv_iommu_detach_dev() since that's what it needs. Set device private to null if attach fails. Clam down number of PFNs passed to hv_iommu_map_pgs().

Changes in V2:
o rebase to 5170a82e8921
o minor fixes for arm64 build
o drop patch 03: "x86/hyperv: add insufficient memory support in irqdomain.c" as it that path is no longer used
o drop patch 08: "PCI: hv: rename hv_compose_msi_msg .. " and do it separately outside this series.
o minor updates to commit messages

Changes in V1:
o patch 1: Don't tie hyperv-irq.c to CONFIG_HYPERV_IOMMU.
o patch 4: Redesigned to address security vulnerability found by copilot with passing tgid as a parameter. Also, do tgid setting right after setting pt_id.
o patch 5: Remove unused type parameter from mshv_device_ops.device_create
o patch 7: mshv_partition_ioctl_create_device cleanup on copy_to_user.
o patch 10: Add export of hv_build_devid_type_pci here to get rid of

             patch 11.

o patch 12: Move functions to build device ids from patch 11 here for the benefit of arm64. Rename file to: hyperv-iommu-root.c.
o patch 13: removed to be made part of interrupt part II of this support.
o patch 14: get rid of fast path to reduce review noise.
o New (last) patch to pin ram regions if device passthru to a VM.

Thanks,
-Mukesh

Mukesh R (11):
iommu/hyperv: Rename hyperv-iommu.c to hyperv-irq.c x86/hyperv: Cosmetic changes in irqdomain.c for readability
mshv: Provide a way to get partition ID if running in a VMM process
mshv: Declarations and definitions for VFIO-MSHV bridge device
mshv: Implement mshv bridge device for VFIO
mshv: Add ioctl support for MSHV-VFIO bridge device
mshv: Import data structs around device passthru from hyperv headers
PCI: hv: VMBus and PCI device IDs for PCI passthru x86/hyperv: Implement Hyper-V virtual IOMMU
mshv: Populate mmio mappings for PCI passthru
mshv: Mark mem regions as non-movable upfront if device passthru

 MAINTAINERS                                   |   3 +-
 arch/x86/hyperv/irqdomain.c                   | 199 ++--
 arch/x86/include/asm/mshyperv.h               |   6 +
 arch/x86/kernel/pci-dma.c                     |   2 +
 drivers/hv/Makefile                           |   3 +-
 drivers/hv/mshv_root.h                        |  21 +
 drivers/hv/mshv_root_main.c                   | 266 ++++-
 drivers/hv/mshv_vfio.c                        | 211 ++++
 drivers/iommu/Kconfig                         |   5 +-
 drivers/iommu/Makefile                        |   3 +-
 drivers/iommu/hyperv-iommu-root.c             | 918 ++++++++++++++++++
 .../iommu/{hyperv-iommu.c => hyperv-irq.c}    |   6 +-
 drivers/iommu/irq_remapping.c                 |   2 +-
 drivers/pci/controller/pci-hyperv.c           |  24 +
 include/asm-generic/mshyperv.h                |  33 +
 include/hyperv/hvgdk_mini.h                   |  11 +
 include/hyperv/hvhdk_mini.h                   | 112 +++
 include/linux/hyperv.h                        |   6 +
 include/uapi/linux/mshv.h                     |  31 +
 19 files changed, 1740 insertions(+), 122 deletions(-)
 create mode 100644 drivers/hv/mshv_vfio.c
 create mode 100644 drivers/iommu/hyperv-iommu-root.c
 rename drivers/iommu/{hyperv-iommu.c => hyperv-irq.c} (99%)
Signature · 1 line
2.51.2.vfs.0.1
author
#1 lore raw

PATCH v3 1/11 iommu/hyperv: Rename hyperv-iommu.c to hyperv-irq.c

This file actually implements irq remapping, so rename to more appropriate hyperv-irq.c. A new file to implement hyperv iommu will be introduced later. Also, it should not be tied to HYPERV_IOMMU, but to CONFIG_HYPERV and IRQ_REMAP. The file already has #ifdef CONFIG_IRQ_REMAP.

---
 MAINTAINERS                                    | 2 +-
 drivers/iommu/Makefile                         | 2 +-
 drivers/iommu/{hyperv-iommu.c => hyperv-irq.c} | 6 +++---
 drivers/iommu/irq_remapping.c                  | 2 +-
 4 files changed, 6 insertions(+), 6 deletions(-)
 rename drivers/iommu/{hyperv-iommu.c => hyperv-irq.c} (99%)
MAINTAINERS +1 -1
index d1cc0e12fe1f..f803a6a38fee 100644
@@ -11914,7 +11914,7 @@ F:	drivers/clocksource/hyperv_timer.c
 F:	drivers/hid/hid-hyperv.c
 F:	drivers/hv/
 F:	drivers/input/serio/hyperv-keyboard.c
-F:	drivers/iommu/hyperv-iommu.c
+F:	drivers/iommu/hyperv-irq.c
 F:	drivers/net/ethernet/microsoft/
 F:	drivers/net/hyperv/
 F:	drivers/pci/controller/pci-hyperv-intf.c
drivers/iommu/Makefile +1 -1
index 0275821f4ef9..335ea77cced6 100644
@@ -30,7 +30,7 @@ obj-$(CONFIG_TEGRA_IOMMU_SMMU) += tegra-smmu.o
 obj-$(CONFIG_EXYNOS_IOMMU) += exynos-iommu.o
 obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o
 obj-$(CONFIG_S390_IOMMU) += s390-iommu.o
-obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o
+obj-$(CONFIG_HYPERV) += hyperv-irq.o
 obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o
 obj-$(CONFIG_IOMMU_SVA) += iommu-sva.o
 obj-$(CONFIG_IOMMU_IOPF) += io-pgfault.o
drivers/iommu/hyperv-irq.c renamed from drivers/iommu/hyperv-iommu.c +3 -3
similarity index 99%
rename from drivers/iommu/hyperv-iommu.c
rename to drivers/iommu/hyperv-irq.c
index 479103261ae6..d11076f906fb 100644
@@ -8,6 +8,8 @@
  * Author : Lan Tianyu <[email protected]>
  */
 
+#ifdef CONFIG_IRQ_REMAP
+
 #include <linux/types.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
@@ -24,8 +26,6 @@
 
 #include "irq_remapping.h"
 
-#ifdef CONFIG_IRQ_REMAP
-
 /*
  * According 82093AA IO-APIC spec , IO APIC has a 24-entry Interrupt
  * Redirection Table. Hyper-V exposes one single IO-APIC and so define
@@ -331,4 +331,4 @@ static const struct irq_domain_ops hyperv_root_ir_domain_ops = {
 	.free = hyperv_root_irq_remapping_free,
 };
 
-#endif
+#endif  /* CONFIG_IRQ_REMAP */
drivers/iommu/irq_remapping.c +1 -1
index c2443659812a..41bf65e4ea88 100644
@@ -108,7 +108,7 @@ int __init irq_remapping_prepare(void)
 	else if (IS_ENABLED(CONFIG_AMD_IOMMU) &&
 		 amd_iommu_irq_ops.prepare() == 0)
 		remap_ops = &amd_iommu_irq_ops;
-	else if (IS_ENABLED(CONFIG_HYPERV_IOMMU) &&
+	else if (IS_ENABLED(CONFIG_HYPERV) &&
 		 hyperv_irq_remap_ops.prepare() == 0)
 		remap_ops = &hyperv_irq_remap_ops;
 	else
Signature · 1 line
2.51.2.vfs.0.1
author
#7 lore raw

PATCH v3 2/11 x86/hyperv: Cosmetic changes in irqdomain.c for readability

In reply to Mukesh R

Make cosmetic changes:
o Rename struct pci_dev *dev to *pdev since there are cases of struct device *dev in the file and all over the kernel
o Rename hv_build_pci_dev_id to hv_build_devid_type_pci in anticipation of building different types of device IDs
o Fix checkpatch.pl issues with return and extraneous printk
o Replace spaces with tabs
o Rename struct hv_devid *xxx to struct hv_devid *hv_devid given code paths involve many types of device IDs
o Fix indentation in a large if block by using goto.

There are no functional changes.

---
 arch/x86/hyperv/irqdomain.c | 198 +++++++++++++++++++-----------------
 1 file changed, 104 insertions(+), 94 deletions(-)
arch/x86/hyperv/irqdomain.c +104 -94
index 365e364268d9..b3ad50a874dc 100644
@@ -1,5 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0
-
 /*
  * Irqdomain for Linux to run as the root partition on Microsoft Hypervisor.
  *
@@ -14,8 +13,8 @@
 #include <linux/irqchip/irq-msi-lib.h>
 #include <asm/mshyperv.h>
 
-static int hv_map_interrupt(union hv_device_id device_id, bool level,
-		int cpu, int vector, struct hv_interrupt_entry *entry)
+static int hv_map_interrupt(union hv_device_id hv_devid, bool level,
+		int cpu, int vector, struct hv_interrupt_entry *ret_entry)
 {
 	struct hv_input_map_device_interrupt *input;
 	struct hv_output_map_device_interrupt *output;
@@ -32,7 +31,7 @@ static int hv_map_interrupt(union hv_device_id device_id, bool level,
 	intr_desc = &input->interrupt_descriptor;
 	memset(input, 0, sizeof(*input));
 	input->partition_id = hv_current_partition_id;
-	input->device_id = device_id.as_uint64;
+	input->device_id = hv_devid.as_uint64;
 	intr_desc->interrupt_type = HV_X64_INTERRUPT_TYPE_FIXED;
 	intr_desc->vector_count = 1;
 	intr_desc->target.vector = vector;
@@ -44,7 +43,7 @@ static int hv_map_interrupt(union hv_device_id device_id, bool level,
 
 	intr_desc->target.vp_set.valid_bank_mask = 0;
 	intr_desc->target.vp_set.format = HV_GENERIC_SET_SPARSE_4K;
-	nr_bank = cpumask_to_vpset(&(intr_desc->target.vp_set), cpumask_of(cpu));
+	nr_bank = cpumask_to_vpset(&intr_desc->target.vp_set, cpumask_of(cpu));
 	if (nr_bank < 0) {
 		local_irq_restore(flags);
 		pr_err("%s: unable to generate VP set\n", __func__);
@@ -61,7 +60,7 @@ static int hv_map_interrupt(union hv_device_id device_id, bool level,
 
 	status = hv_do_rep_hypercall(HVCALL_MAP_DEVICE_INTERRUPT, 0, var_size,
 			input, output);
-	*entry = output->interrupt_entry;
+	*ret_entry = output->interrupt_entry;
 
 	local_irq_restore(flags);
 
@@ -71,21 +70,19 @@ static int hv_map_interrupt(union hv_device_id device_id, bool level,
 	return hv_result_to_errno(status);
 }
 
-static int hv_unmap_interrupt(u64 id, struct hv_interrupt_entry *old_entry)
+static int hv_unmap_interrupt(u64 id, struct hv_interrupt_entry *irq_entry)
 {
 	unsigned long flags;
 	struct hv_input_unmap_device_interrupt *input;
-	struct hv_interrupt_entry *intr_entry;
 	u64 status;
 
 	local_irq_save(flags);
 	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
 
 	memset(input, 0, sizeof(*input));
-	intr_entry = &input->interrupt_entry;
 	input->partition_id = hv_current_partition_id;
 	input->device_id = id;
-	*intr_entry = *old_entry;
+	input->interrupt_entry = *irq_entry;
 
 	status = hv_do_hypercall(HVCALL_UNMAP_DEVICE_INTERRUPT, input, NULL);
 	local_irq_restore(flags);
@@ -115,67 +112,71 @@ static int get_rid_cb(struct pci_dev *pdev, u16 alias, void *data)
 	return 0;
 }
 
-static union hv_device_id hv_build_pci_dev_id(struct pci_dev *dev)
+static union hv_device_id hv_build_devid_type_pci(struct pci_dev *pdev)
 {
-	union hv_device_id dev_id;
+	int pos;
+	union hv_device_id hv_devid;
 	struct rid_data data = {
 		.bridge = NULL,
-		.rid = PCI_DEVID(dev->bus->number, dev->devfn)
+		.rid = PCI_DEVID(pdev->bus->number, pdev->devfn)
 	};
 
-	pci_for_each_dma_alias(dev, get_rid_cb, &data);
+	pci_for_each_dma_alias(pdev, get_rid_cb, &data);
 
-	dev_id.as_uint64 = 0;
-	dev_id.device_type = HV_DEVICE_TYPE_PCI;
-	dev_id.pci.segment = pci_domain_nr(dev->bus);
+	hv_devid.as_uint64 = 0;
+	hv_devid.device_type = HV_DEVICE_TYPE_PCI;
+	hv_devid.pci.segment = pci_domain_nr(pdev->bus);
 
-	dev_id.pci.bdf.bus = PCI_BUS_NUM(data.rid);
-	dev_id.pci.bdf.device = PCI_SLOT(data.rid);
-	dev_id.pci.bdf.function = PCI_FUNC(data.rid);
-	dev_id.pci.source_shadow = HV_SOURCE_SHADOW_NONE;
+	hv_devid.pci.bdf.bus = PCI_BUS_NUM(data.rid);
+	hv_devid.pci.bdf.device = PCI_SLOT(data.rid);
+	hv_devid.pci.bdf.function = PCI_FUNC(data.rid);
+	hv_devid.pci.source_shadow = HV_SOURCE_SHADOW_NONE;
 
-	if (data.bridge) {
-		int pos;
+	if (data.bridge == NULL)
+		goto out;
 
-		/*
-		 * Microsoft Hypervisor requires a bus range when the bridge is
-		 * running in PCI-X mode.
-		 *
-		 * To distinguish conventional vs PCI-X bridge, we can check
-		 * the bridge's PCI-X Secondary Status Register, Secondary Bus
-		 * Mode and Frequency bits. See PCI Express to PCI/PCI-X Bridge
-		 * Specification Revision 1.0 5.2.2.1.3.
-		 *
-		 * Value zero means it is in conventional mode, otherwise it is
-		 * in PCI-X mode.
-		 */
+	/*
+	 * Microsoft Hypervisor requires a bus range when the bridge is
+	 * running in PCI-X mode.
+	 *
+	 * To distinguish conventional vs PCI-X bridge, we can check
+	 * the bridge's PCI-X Secondary Status Register, Secondary Bus
+	 * Mode and Frequency bits. See PCI Express to PCI/PCI-X Bridge
+	 * Specification Revision 1.0 5.2.2.1.3.
+	 *
+	 * Value zero means it is in conventional mode, otherwise it is
+	 * in PCI-X mode.
+	 */
 
-		pos = pci_find_capability(data.bridge, PCI_CAP_ID_PCIX);
-		if (pos) {
-			u16 status;
+	pos = pci_find_capability(data.bridge, PCI_CAP_ID_PCIX);
+	if (pos) {
+		u16 status;
 
-			pci_read_config_word(data.bridge, pos +
-					PCI_X_BRIDGE_SSTATUS, &status);
+		pci_read_config_word(data.bridge, pos + PCI_X_BRIDGE_SSTATUS,
+				     &status);
 
-			if (status & PCI_X_SSTATUS_FREQ) {
-				/* Non-zero, PCI-X mode */
-				u8 sec_bus, sub_bus;
+		if (status & PCI_X_SSTATUS_FREQ) {
+			/* Non-zero, PCI-X mode */
+			u8 sec_bus, sub_bus;
 
-				dev_id.pci.source_shadow = HV_SOURCE_SHADOW_BRIDGE_BUS_RANGE;
+			hv_devid.pci.source_shadow =
+					     HV_SOURCE_SHADOW_BRIDGE_BUS_RANGE;
 
-				pci_read_config_byte(data.bridge, PCI_SECONDARY_BUS, &sec_bus);
-				dev_id.pci.shadow_bus_range.secondary_bus = sec_bus;
-				pci_read_config_byte(data.bridge, PCI_SUBORDINATE_BUS, &sub_bus);
-				dev_id.pci.shadow_bus_range.subordinate_bus = sub_bus;
-			}
+			pci_read_config_byte(data.bridge, PCI_SECONDARY_BUS,
+					     &sec_bus);
+			hv_devid.pci.shadow_bus_range.secondary_bus = sec_bus;
+			pci_read_config_byte(data.bridge, PCI_SUBORDINATE_BUS,
+					     &sub_bus);
+			hv_devid.pci.shadow_bus_range.subordinate_bus = sub_bus;
 		}
 	}
 
-	return dev_id;
+out:
+	return hv_devid;
 }
 
-/**
- * hv_map_msi_interrupt() - "Map" the MSI IRQ in the hypervisor.
+/*
+ * hv_map_msi_interrupt() - Map the MSI IRQ in the hypervisor.
  * @data:      Describes the IRQ
  * @out_entry: Hypervisor (MSI) interrupt entry (can be NULL)
  *
@@ -188,22 +189,23 @@ int hv_map_msi_interrupt(struct irq_data *data,
 {
 	struct irq_cfg *cfg = irqd_cfg(data);
 	struct hv_interrupt_entry dummy;
-	union hv_device_id device_id;
+	union hv_device_id hv_devid;
 	struct msi_desc *msidesc;
-	struct pci_dev *dev;
+	struct pci_dev *pdev;
 	int cpu;
 
 	msidesc = irq_data_get_msi_desc(data);
-	dev = msi_desc_to_pci_dev(msidesc);
-	device_id = hv_build_pci_dev_id(dev);
+	pdev = msi_desc_to_pci_dev(msidesc);
+	hv_devid = hv_build_devid_type_pci(pdev);
 	cpu = cpumask_first(irq_data_get_effective_affinity_mask(data));
 
-	return hv_map_interrupt(device_id, false, cpu, cfg->vector,
+	return hv_map_interrupt(hv_devid, false, cpu, cfg->vector,
 				out_entry ? out_entry : &dummy);
 }
 EXPORT_SYMBOL_GPL(hv_map_msi_interrupt);
 
-static inline void entry_to_msi_msg(struct hv_interrupt_entry *entry, struct msi_msg *msg)
+static void entry_to_msi_msg(struct hv_interrupt_entry *entry,
+			     struct msi_msg *msg)
 {
 	/* High address is always 0 */
 	msg->address_hi = 0;
@@ -211,17 +213,19 @@ static inline void entry_to_msi_msg(struct hv_interrupt_entry *entry, struct msi
 	msg->data = entry->msi_entry.data.as_uint32;
 }
 
-static int hv_unmap_msi_interrupt(struct pci_dev *dev, struct hv_interrupt_entry *old_entry);
+static int hv_unmap_msi_interrupt(struct pci_dev *pdev,
+				  struct hv_interrupt_entry *irq_entry);
+
 static void hv_irq_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
 {
 	struct hv_interrupt_entry *stored_entry;
 	struct irq_cfg *cfg = irqd_cfg(data);
 	struct msi_desc *msidesc;
-	struct pci_dev *dev;
+	struct pci_dev *pdev;
 	int ret;
 
 	msidesc = irq_data_get_msi_desc(data);
-	dev = msi_desc_to_pci_dev(msidesc);
+	pdev = msi_desc_to_pci_dev(msidesc);
 
 	if (!cfg) {
 		pr_debug("%s: cfg is NULL", __func__);
@@ -240,7 +244,7 @@ static void hv_irq_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
 		stored_entry = data->chip_data;
 		data->chip_data = NULL;
 
-		ret = hv_unmap_msi_interrupt(dev, stored_entry);
+		ret = hv_unmap_msi_interrupt(pdev, stored_entry);
 
 		kfree(stored_entry);
 
@@ -249,10 +253,8 @@ static void hv_irq_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
 	}
 
 	stored_entry = kzalloc_obj(*stored_entry, GFP_ATOMIC);
-	if (!stored_entry) {
-		pr_debug("%s: failed to allocate chip data\n", __func__);
+	if (!stored_entry)
 		return;
-	}
 
 	ret = hv_map_msi_interrupt(data, stored_entry);
 	if (ret) {
@@ -262,18 +264,21 @@ static void hv_irq_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
 
 	data->chip_data = stored_entry;
 	entry_to_msi_msg(data->chip_data, msg);
-
-	return;
 }
 
-static int hv_unmap_msi_interrupt(struct pci_dev *dev, struct hv_interrupt_entry *old_entry)
+static int hv_unmap_msi_interrupt(struct pci_dev *pdev,
+				  struct hv_interrupt_entry *irq_entry)
 {
-	return hv_unmap_interrupt(hv_build_pci_dev_id(dev).as_uint64, old_entry);
+	union hv_device_id hv_devid;
+
+	hv_devid = hv_build_devid_type_pci(pdev);
+	return hv_unmap_interrupt(hv_devid.as_uint64, irq_entry);
 }
 
-static void hv_teardown_msi_irq(struct pci_dev *dev, struct irq_data *irqd)
+/* NB: during map, hv_interrupt_entry is saved via data->chip_data */
+static void hv_teardown_msi_irq(struct pci_dev *pdev, struct irq_data *irqd)
 {
-	struct hv_interrupt_entry old_entry;
+	struct hv_interrupt_entry irq_entry;
 	struct msi_msg msg;
 
 	if (!irqd->chip_data) {
@@ -281,13 +286,13 @@ static void hv_teardown_msi_irq(struct pci_dev *dev, struct irq_data *irqd)
 		return;
 	}
 
-	old_entry = *(struct hv_interrupt_entry *)irqd->chip_data;
-	entry_to_msi_msg(&old_entry, &msg);
+	irq_entry = *(struct hv_interrupt_entry *)irqd->chip_data;
+	entry_to_msi_msg(&irq_entry, &msg);
 
 	kfree(irqd->chip_data);
 	irqd->chip_data = NULL;
 
-	(void)hv_unmap_msi_interrupt(dev, &old_entry);
+	(void)hv_unmap_msi_interrupt(pdev, &irq_entry);
 }
 
 /*
@@ -302,7 +307,8 @@ static struct irq_chip hv_pci_msi_controller = {
 };
 
 static bool hv_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
-				 struct irq_domain *real_parent, struct msi_domain_info *info)
+				 struct irq_domain *real_parent,
+				 struct msi_domain_info *info)
 {
 	struct irq_chip *chip = info->chip;
 
@@ -317,7 +323,8 @@ static bool hv_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
 }
 
 #define HV_MSI_FLAGS_SUPPORTED	(MSI_GENERIC_FLAGS_MASK | MSI_FLAG_PCI_MSIX)
-#define HV_MSI_FLAGS_REQUIRED	(MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS)
+#define HV_MSI_FLAGS_REQUIRED	(MSI_FLAG_USE_DEF_DOM_OPS |	\
+				 MSI_FLAG_USE_DEF_CHIP_OPS)
 
 static struct msi_parent_ops hv_msi_parent_ops = {
 	.supported_flags	= HV_MSI_FLAGS_SUPPORTED,
@@ -329,14 +336,14 @@ static struct msi_parent_ops hv_msi_parent_ops = {
 	.init_dev_msi_info	= hv_init_dev_msi_info,
 };
 
-static int hv_msi_domain_alloc(struct irq_domain *d, unsigned int virq, unsigned int nr_irqs,
-			       void *arg)
+/* Allocate nr_irqs IRQs for the given irq domain */
+static int hv_msi_domain_alloc(struct irq_domain *d, unsigned int virq,
+			       unsigned int nr_irqs, void *arg)
 {
 	/*
-	 * TODO: The allocation bits of hv_irq_compose_msi_msg(), i.e. everything except
-	 * entry_to_msi_msg() should be in here.
+	 * TODO: The allocation bits of hv_irq_compose_msi_msg(), i.e.
+	 *	 everything except entry_to_msi_msg() should be in here.
 	 */
-
 	int ret;
 
 	ret = irq_domain_alloc_irqs_parent(d, virq, nr_irqs, arg);
@@ -344,13 +351,15 @@ static int hv_msi_domain_alloc(struct irq_domain *d, unsigned int virq, unsigned
 		return ret;
 
 	for (int i = 0; i < nr_irqs; ++i) {
-		irq_domain_set_info(d, virq + i, 0, &hv_pci_msi_controller, NULL,
-				    handle_edge_irq, NULL, "edge");
+		irq_domain_set_info(d, virq + i, 0, &hv_pci_msi_controller,
+				    NULL, handle_edge_irq, NULL, "edge");
 	}
+
 	return 0;
 }
 
-static void hv_msi_domain_free(struct irq_domain *d, unsigned int virq, unsigned int nr_irqs)
+static void hv_msi_domain_free(struct irq_domain *d, unsigned int virq,
+			       unsigned int nr_irqs)
 {
 	for (int i = 0; i < nr_irqs; ++i) {
 		struct irq_data *irqd = irq_domain_get_irq_data(d, virq);
@@ -362,6 +371,7 @@ static void hv_msi_domain_free(struct irq_domain *d, unsigned int virq, unsigned
 
 		hv_teardown_msi_irq(to_pci_dev(desc->dev), irqd);
 	}
+
 	irq_domain_free_irqs_top(d, virq, nr_irqs);
 }
 
@@ -394,25 +404,25 @@ struct irq_domain * __init hv_create_pci_msi_domain(void)
 
 int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry)
 {
-	union hv_device_id device_id;
+	union hv_device_id hv_devid;
 
-	device_id.as_uint64 = 0;
-	device_id.device_type = HV_DEVICE_TYPE_IOAPIC;
-	device_id.ioapic.ioapic_id = (u8)ioapic_id;
+	hv_devid.as_uint64 = 0;
+	hv_devid.device_type = HV_DEVICE_TYPE_IOAPIC;
+	hv_devid.ioapic.ioapic_id = (u8)ioapic_id;
 
-	return hv_unmap_interrupt(device_id.as_uint64, entry);
+	return hv_unmap_interrupt(hv_devid.as_uint64, entry);
 }
 EXPORT_SYMBOL_GPL(hv_unmap_ioapic_interrupt);
 
 int hv_map_ioapic_interrupt(int ioapic_id, bool level, int cpu, int vector,
 		struct hv_interrupt_entry *entry)
 {
-	union hv_device_id device_id;
+	union hv_device_id hv_devid;
 
-	device_id.as_uint64 = 0;
-	device_id.device_type = HV_DEVICE_TYPE_IOAPIC;
-	device_id.ioapic.ioapic_id = (u8)ioapic_id;
+	hv_devid.as_uint64 = 0;
+	hv_devid.device_type = HV_DEVICE_TYPE_IOAPIC;
+	hv_devid.ioapic.ioapic_id = (u8)ioapic_id;
 
-	return hv_map_interrupt(device_id, level, cpu, vector, entry);
+	return hv_map_interrupt(hv_devid, level, cpu, vector, entry);
 }
 EXPORT_SYMBOL_GPL(hv_map_ioapic_interrupt);
Signature · 1 line
2.51.2.vfs.0.1
author
#10 lore raw

PATCH v3 3/11 mshv: Provide a way to get partition ID if running in a VMM process

In reply to Mukesh R

Many PCI passthru related hypercalls require partition ID of the target guest. Guests are actually managed by MSHV driver and the partition ID is only maintained there. Add a field in the partition struct in MSHV driver to save the tgid of the VMM process creating the partition, and add a function there to retrieve partition ID if the current process is a VMM process.

---
 drivers/hv/mshv_root.h         |  1 +
 drivers/hv/mshv_root_main.c    | 22 ++++++++++++++++++++++
 include/asm-generic/mshyperv.h |  5 +++++
 3 files changed, 28 insertions(+)
drivers/hv/mshv_root.h +1 -0
index 1f086dcb7aa1..a85c24dcc701 100644
@@ -138,6 +138,7 @@ struct mshv_partition {
 
 	struct mshv_girq_routing_table __rcu *pt_girq_tbl;
 	u64 isolation_type;
+	pid_t pt_vmm_tgid;
 	bool import_completed;
 	bool pt_initialized;
 #if IS_ENABLED(CONFIG_DEBUG_FS)
drivers/hv/mshv_root_main.c +22 -0
index bd1359eb58dd..02c107458be9 100644
@@ -1908,6 +1908,27 @@ mshv_partition_release(struct inode *inode, struct file *filp)
 	return 0;
 }
 
+/* Given a process tgid, return partition id if it is a VMM process */
+u64 mshv_current_partid(void)
+{
+	struct mshv_partition *pt;
+	int i;
+	u64 ret_ptid = HV_PARTITION_ID_INVALID;
+
+	rcu_read_lock();
+
+	hash_for_each_rcu(mshv_root.pt_htable, i, pt, pt_hnode) {
+		if (pt->pt_vmm_tgid == current->tgid) {
+			ret_ptid = pt->pt_id;
+			break;
+		}
+	}
+
+	rcu_read_unlock();
+	return ret_ptid;
+}
+EXPORT_SYMBOL_GPL(mshv_current_partid);
+
 static int
 add_partition(struct mshv_partition *partition)
 {
@@ -2073,6 +2094,7 @@ mshv_ioctl_create_partition(void __user *user_arg, struct device *module_dev)
 		goto cleanup_irq_srcu;
 
 	partition->pt_id = pt_id;
+	partition->pt_vmm_tgid = current->tgid;
 
 	ret = add_partition(partition);
 	if (ret)
include/asm-generic/mshyperv.h +5 -0
index bf601d67cecb..e8cbc4e3f7ad 100644
@@ -350,6 +350,7 @@ int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
 int hv_call_notify_all_processors_started(void);
 bool hv_lp_exists(u32 lp_index);
 int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
+u64 mshv_current_partid(void);
 
 #else /* CONFIG_MSHV_ROOT */
 static inline bool hv_root_partition(void) { return false; }
@@ -380,6 +381,10 @@ static inline int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u3
 {
 	return -EOPNOTSUPP;
 }
+static inline u64 mshv_current_partid(void)
+{
+	return HV_PARTITION_ID_INVALID;
+}
 #endif /* CONFIG_MSHV_ROOT */
 
 static inline int hv_deposit_memory(u64 partition_id, u64 status)
Signature · 1 line
2.51.2.vfs.0.1
author
#12 lore raw

PATCH v3 4/11 mshv: Declarations and definitions for VFIO-MSHV bridge device

In reply to Mukesh R

Add data structs needed by the subsequent patch that introduces a new module to implement VFIO-MSHV pseudo device.

---
 drivers/hv/mshv_root.h    | 19 +++++++++++++++++++
 include/uapi/linux/mshv.h | 30 ++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
drivers/hv/mshv_root.h +19 -0
index a85c24dcc701..b9880d0bdc4d 100644
@@ -227,6 +227,25 @@ struct port_table_info {
 	};
 };
 
+struct mshv_device {
+	const struct mshv_device_ops *device_ops;
+	struct mshv_partition *device_pt;
+	void *device_private;
+	struct hlist_node device_ptnode;
+};
+
+struct mshv_device_ops {
+	const char *device_name;
+	long (*device_create)(struct mshv_device *dev);
+	void (*device_release)(struct mshv_device *dev);
+	long (*device_set_attr)(struct mshv_device *dev,
+				struct mshv_device_attr *attr);
+	long (*device_has_attr)(struct mshv_device *dev,
+				struct mshv_device_attr *attr);
+};
+
+extern struct mshv_device_ops mshv_vfio_device_ops;
+
 int mshv_update_routing_table(struct mshv_partition *partition,
 			      const struct mshv_user_irq_entry *entries,
 			      unsigned int numents);
include/uapi/linux/mshv.h +30 -0
index 32ff92b6342b..be6fe3ee8707 100644
@@ -404,4 +404,34 @@ struct mshv_sint_mask {
 /* hv_hvcall device */
 #define MSHV_HVCALL_SETUP        _IOW(MSHV_IOCTL, 0x1E, struct mshv_vtl_hvcall_setup)
 #define MSHV_HVCALL              _IOWR(MSHV_IOCTL, 0x1F, struct mshv_vtl_hvcall)
+
+/* Device passhthru */
+#define MSHV_CREATE_DEVICE_TEST		1
+
+enum {
+	MSHV_DEV_TYPE_VFIO,
+	MSHV_DEV_TYPE_MAX,
+};
+
+struct mshv_create_device {
+	__u32	type;	     /* in: MSHV_DEV_TYPE_xxx */
+	__u32	fd;	     /* out: device handle */
+	__u32	flags;	     /* in: MSHV_CREATE_DEVICE_xxx */
+};
+
+#define MSHV_DEV_VFIO_FILE      1
+#define MSHV_DEV_VFIO_FILE_ADD	1
+#define MSHV_DEV_VFIO_FILE_DEL	2
+
+struct mshv_device_attr {
+	__u32	flags;		/* no flags currently defined */
+	__u32	group;		/* device-defined */
+	__u64	attr;		/* group-defined */
+	__u64	addr;		/* userspace address of attr data */
+};
+
+/* Device fds created with MSHV_CREATE_DEVICE */
+#define MSHV_SET_DEVICE_ATTR	_IOW(MSHV_IOCTL, 0x00, struct mshv_device_attr)
+#define MSHV_HAS_DEVICE_ATTR	_IOW(MSHV_IOCTL, 0x01, struct mshv_device_attr)
+
 #endif
Signature · 1 line
2.51.2.vfs.0.1
author
#14 lore raw

PATCH v3 5/11 mshv: Implement mshv bridge device for VFIO

In reply to Mukesh R

Add a new file to implement VFIO-MSHV bridge pseudo device. These functions are called in the VFIO framework, and credits to kvm/vfio.c as this file was adapted from it.

---
 drivers/hv/Makefile       |   3 +-
 drivers/hv/mshv_vfio.c    | 211 ++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/mshv.h |   1 +
 3 files changed, 214 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hv/mshv_vfio.c
drivers/hv/Makefile +2 -1
index 888a748cc7cb..9ab6fc254c38 100644
@@ -14,7 +14,8 @@ hv_vmbus-y := vmbus_drv.o \
 hv_vmbus-$(CONFIG_HYPERV_TESTING)	+= hv_debugfs.o
 hv_utils-y := hv_util.o hv_kvp.o hv_snapshot.o hv_utils_transport.o
 mshv_root-y := mshv_root_main.o mshv_synic.o mshv_eventfd.o mshv_irq.o \
-	       mshv_root_hv_call.o mshv_portid_table.o mshv_regions.o
+	       mshv_root_hv_call.o mshv_portid_table.o mshv_regions.o  \
+               mshv_vfio.o
 mshv_root-$(CONFIG_DEBUG_FS) += mshv_debugfs.o
 mshv_root-$(CONFIG_TRACEPOINTS) += mshv_trace.o
 mshv_vtl-y := mshv_vtl_main.o
drivers/hv/mshv_vfio.c new file +211 -0

211 lines not on this page · raw message

include/uapi/linux/mshv.h +1 -0
index be6fe3ee8707..b038a79786d2 100644
@@ -254,6 +254,7 @@ struct mshv_root_hvcall {
 #define MSHV_GET_GPAP_ACCESS_BITMAP	_IOWR(MSHV_IOCTL, 0x06, struct mshv_gpap_access_bitmap)
 /* Generic hypercall */
 #define MSHV_ROOT_HVCALL		_IOWR(MSHV_IOCTL, 0x07, struct mshv_root_hvcall)
+#define MSHV_CREATE_DEVICE             _IOWR(MSHV_IOCTL, 0x08, struct mshv_create_device)
 
 /*
  ********************************
Signature · 1 line
2.51.2.vfs.0.1
author
#16 lore raw

PATCH v3 6/11 mshv: Add ioctl support for MSHV-VFIO bridge device

In reply to Mukesh R

Add ioctl support for creating MSHV devices for a partition. At present only VFIO device types are supported, but more could be added. At a high level, a partition ioctl to create device verifies it is of type VFIO and does some setup for bridge code in mshv_vfio.c. Adapted from KVM device ioctls.

---
 drivers/hv/mshv_root_main.c | 116 ++++++++++++++++++++++++++++++++++++
 1 file changed, 116 insertions(+)
drivers/hv/mshv_root_main.c +116 -0
index 02c107458be9..6ceb5f608589 100644
@@ -1625,6 +1625,119 @@ mshv_partition_ioctl_initialize(struct mshv_partition *partition)
 	return ret;
 }
 
+static long mshv_device_attr_ioctl(struct mshv_device *mshv_dev, int cmd,
+				   ulong uarg)
+{
+	struct mshv_device_attr attr;
+	const struct mshv_device_ops *devops = mshv_dev->device_ops;
+
+	if (copy_from_user(&attr, (void __user *)uarg, sizeof(attr)))
+		return -EFAULT;
+
+	switch (cmd) {
+	case MSHV_SET_DEVICE_ATTR:
+		if (devops->device_set_attr)
+			return devops->device_set_attr(mshv_dev, &attr);
+		break;
+	case MSHV_HAS_DEVICE_ATTR:
+		if (devops->device_has_attr)
+			return devops->device_has_attr(mshv_dev, &attr);
+		break;
+	}
+
+	return -EPERM;
+}
+
+static long mshv_device_fop_ioctl(struct file *filp, unsigned int cmd,
+				  ulong uarg)
+{
+	struct mshv_device *mshv_dev = filp->private_data;
+
+	switch (cmd) {
+	case MSHV_SET_DEVICE_ATTR:
+	case MSHV_HAS_DEVICE_ATTR:
+		return mshv_device_attr_ioctl(mshv_dev, cmd, uarg);
+	}
+
+	return -ENOTTY;
+}
+
+static int mshv_device_fop_release(struct inode *inode, struct file *filp)
+{
+	struct mshv_device *mshv_dev = filp->private_data;
+	struct mshv_partition *partition = mshv_dev->device_pt;
+
+	if (mshv_dev->device_ops->device_release) {
+		mutex_lock(&partition->pt_mutex);
+		hlist_del(&mshv_dev->device_ptnode);
+		mshv_dev->device_ops->device_release(mshv_dev);
+		mutex_unlock(&partition->pt_mutex);
+	}
+
+	mshv_partition_put(partition);
+	return 0;
+}
+
+static const struct file_operations mshv_device_fops = {
+	.owner = THIS_MODULE,
+	.unlocked_ioctl = mshv_device_fop_ioctl,
+	.release = mshv_device_fop_release,
+};
+
+static long mshv_partition_ioctl_create_device(struct mshv_partition *partition,
+					       void __user *uarg)
+{
+	long rc;
+	struct mshv_create_device devargk;
+	struct mshv_device *mshv_dev;
+	const struct mshv_device_ops *vfio_ops;
+
+	if (copy_from_user(&devargk, uarg, sizeof(devargk)))
+		return -EFAULT;
+
+	/* At present, only VFIO is supported */
+	if (devargk.type != MSHV_DEV_TYPE_VFIO)
+		return -ENODEV;
+
+	if (devargk.flags & MSHV_CREATE_DEVICE_TEST)
+		return 0;
+
+	/* This is freed later by mshv_vfio_release_device() */
+	mshv_dev = kzalloc(sizeof(*mshv_dev), GFP_KERNEL_ACCOUNT);
+	if (mshv_dev == NULL)
+		return -ENOMEM;
+
+	vfio_ops = &mshv_vfio_device_ops;
+	mshv_dev->device_ops = vfio_ops;
+	mshv_dev->device_pt = partition;
+
+	rc = vfio_ops->device_create(mshv_dev);
+	if (rc < 0) {
+		kfree(mshv_dev);
+		return rc;
+	}
+
+	hlist_add_head(&mshv_dev->device_ptnode, &partition->pt_devices);
+
+	mshv_partition_get(partition);
+	rc = anon_inode_getfd(vfio_ops->device_name, &mshv_device_fops,
+			      mshv_dev, O_RDWR | O_CLOEXEC);
+	if (rc < 0)
+		goto undo_out;
+
+	devargk.fd = rc;
+	if (copy_to_user(uarg, &devargk, sizeof(devargk)))
+		return -EFAULT;    /* cleanup in mshv_device_fop_release() */
+
+	return 0;
+
+undo_out:
+	hlist_del(&mshv_dev->device_ptnode);
+	vfio_ops->device_release(mshv_dev);    /* will kfree(mshv_dev) */
+	mshv_partition_put(partition);
+	return rc;
+}
+
 static long
 mshv_partition_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
 {
@@ -1661,6 +1774,9 @@ mshv_partition_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
 	case MSHV_ROOT_HVCALL:
 		ret = mshv_ioctl_passthru_hvcall(partition, true, uarg);
 		break;
+	case MSHV_CREATE_DEVICE:
+		ret = mshv_partition_ioctl_create_device(partition, uarg);
+		break;
 	default:
 		ret = -ENOTTY;
 	}
Signature · 1 line
2.51.2.vfs.0.1
author
#18 lore raw

PATCH v3 7/11 mshv: Import data structs around device passthru from hyperv headers

In reply to Mukesh R

Copy/import from Hyper-V public headers, definitions and declarations that are related to attaching and detaching of device domains, and building device ids for those purposes.

---
 include/hyperv/hvgdk_mini.h |  11 ++++
 include/hyperv/hvhdk_mini.h | 112 ++++++++++++++++++++++++++++++++++++
 2 files changed, 123 insertions(+)
include/hyperv/hvgdk_mini.h +11 -0
index 6a4e8b9d570f..da622fb06440 100644
@@ -326,6 +326,9 @@ 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)
+#define HV_S1_DEVICE_DOMAIN_AVAILABLE			BIT(25)
+
 /*
  * Implementation recommendations. Indicates which behaviors the hypervisor
  * recommends the OS implement for optimal performance.
@@ -475,6 +478,8 @@ union hv_vp_assist_msr_contents {	 /* HV_REGISTER_VP_ASSIST_PAGE */
 #define HVCALL_MAP_DEVICE_INTERRUPT			0x007c
 #define HVCALL_UNMAP_DEVICE_INTERRUPT			0x007d
 #define HVCALL_RETARGET_INTERRUPT			0x007e
+#define HVCALL_ATTACH_DEVICE                            0x0082
+#define HVCALL_DETACH_DEVICE                            0x0083
 #define HVCALL_NOTIFY_PARTITION_EVENT                   0x0087
 #define HVCALL_ENTER_SLEEP_STATE			0x0084
 #define HVCALL_NOTIFY_PORT_RING_EMPTY			0x008b
@@ -486,9 +491,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
include/hyperv/hvhdk_mini.h +112 -0

122 lines not on this page · raw message

Signature · 1 line
2.51.2.vfs.0.1
author
#19 lore raw

PATCH v3 8/11 PCI: hv: VMBus and PCI device IDs for PCI passthru

In reply to Mukesh R

On Hyper-V, most hypercalls related to PCI passthru to map/unmap regions, interrupts, etc need a device ID as a parameter. This device ID refers to that specific device during the lifetime of passthru.

An L1VH VM only contains VMBus based devices. A device ID for a VMBus device is slightly different in that it uses the hv_pcibus_device info for building it to make sure it matches exactly what the hypervisor expects. This VMBus based device ID is needed when attaching devices in an L1VH based guest VM. Add a function to build and export it. Before building it, a check is done to make sure the device is a valid VMBus device.

In remaining cases, PCI device ID is used. So, also make PCI device ID build function hv_build_devid_type_pci() public.

---
 arch/x86/hyperv/irqdomain.c         |  9 +++++----
 arch/x86/include/asm/mshyperv.h     |  6 ++++++
 drivers/pci/controller/pci-hyperv.c | 24 ++++++++++++++++++++++++
 include/asm-generic/mshyperv.h      | 11 +++++++++++
 4 files changed, 46 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..2ef34001f8d3 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 */
 
+#if IS_ENABLED(CONFIG_HYPERV_IOMMU)
+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 /* IS_ENABLED(CONFIG_HYPERV_IOMMU) */
+
 #else /* CONFIG_HYPERV */
 static inline void hyperv_init(void) {}
 static inline void hyperv_setup_mmu_ops(void) {}
drivers/pci/controller/pci-hyperv.c +24 -0
index cfc8fa403dad..50d793ca8f31 100644
@@ -573,6 +573,7 @@ struct hv_pci_compl {
 };
 
 static void hv_pci_onchannelcallback(void *context);
+static bool hv_vmbus_pci_device(struct pci_bus *pbus);
 
 #ifdef CONFIG_X86
 #define DELIVERY_MODE		APIC_DELIVERY_MODE_FIXED
@@ -1005,6 +1006,24 @@ static struct irq_domain *hv_pci_get_root_domain(void)
 static void hv_arch_irq_unmask(struct irq_data *data) { }
 #endif /* CONFIG_ARM64 */
 
+u64 hv_pci_vmbus_device_id(struct pci_dev *pdev)
+{
+	struct hv_pcibus_device *hbus;
+	struct pci_bus *pbus = pdev->bus;
+
+	if (!hv_vmbus_pci_device(pbus))
+		return 0;
+
+	hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
+
+	return	(hbus->hdev->dev_instance.b[5] << 24) |
+		(hbus->hdev->dev_instance.b[4] << 16) |
+		(hbus->hdev->dev_instance.b[7] << 8) |
+		(hbus->hdev->dev_instance.b[6] & 0xf8) |
+		PCI_FUNC(pdev->devfn);
+}
+EXPORT_SYMBOL_GPL(hv_pci_vmbus_device_id);
+
 /**
  * hv_pci_generic_compl() - Invoked for a completion packet
  * @context:		Set up by the sender of the packet.
@@ -1403,6 +1422,11 @@ static struct pci_ops hv_pcifront_ops = {
 	.write = hv_pcifront_write_config,
 };
 
+static bool hv_vmbus_pci_device(struct pci_bus *pbus)
+{
+	return pbus->ops == &hv_pcifront_ops;
+}
+
 /*
  * Paravirtual backchannel
  *
include/asm-generic/mshyperv.h +11 -0
index e8cbc4e3f7ad..25ac7ca0fd8b 100644
@@ -204,6 +204,9 @@ extern u64 (*hv_read_reference_counter)(void);
 /* Sentinel value for an uninitialized entry in hv_vp_index array */
 #define VP_INVAL	U32_MAX
 
+/* Forward declarations */
+struct pci_dev;
+
 int __init hv_common_init(void);
 void __init hv_get_partition_id(void);
 void __init hv_common_free(void);
@@ -316,6 +319,14 @@ void hv_para_set_synic_register(unsigned int reg, u64 val);
 void hyperv_cleanup(void);
 bool hv_query_ext_cap(u64 cap_query);
 void hv_setup_dma_ops(struct device *dev, bool coherent);
+
+#if IS_ENABLED(CONFIG_PCI_HYPERV)
+u64 hv_pci_vmbus_device_id(struct pci_dev *pdev);
+#else
+static inline u64 hv_pci_vmbus_device_id(struct pci_dev *pdev)
+{ return 0; }
+#endif /* IS_ENABLED(CONFIG_PCI_HYPERV) */
+
 #else /* CONFIG_HYPERV */
 static inline void hv_identify_partition_type(void) {}
 static inline bool hv_is_hyperv_initialized(void) { return false; }
Signature · 1 line
2.51.2.vfs.0.1
author
#24 lore raw

PATCH v3 9/11 x86/hyperv: Implement Hyper-V virtual IOMMU

In reply to Mukesh R

Add a new file to implement management of device domains, mapping and unmapping of IOMMU memory, and other iommu_ops to fit within the VFIO framework for PCI passthru on Hyper-V running Linux as baremetal root or L1VH root. This also implements direct attach mechanism (see below), a special feature of Hyper-V for PCI passthru, and it is also made to work within the VFIO framework.

At a high level, during boot the hypervisor creates a default identity domain and attaches all devices to it. This nicely maps to Linux IOMMU subsystem IOMMU_DOMAIN_IDENTITY domain. As a result, Linux does not need to explicitly ask Hyper-V to attach devices and do maps/unmaps during boot. As mentioned previously, Hyper-V supports two ways to do PCI passthru:

1. Device Domain (aka Domain Attach): root must create a device domain in the hypervisor, and do map/unmap hypercalls for mapping and unmapping guest RAM for DMA. All hypervisor communications use device ID of type PCI for identifying and referencing the device.

2. Direct Attach: the hypervisor will simply use the guest's HW page table for mappings, thus the root need not map/unmap guest memory for DMA. As such, direct attach passthru setup during guest boot is extremely fast. A direct attached device must always be referenced via logical device ID and not via the PCI device ID.

At present, L1VH root only supports direct attaches. Also direct attach is default in non-L1VH cases because there are some significant performance issues with domain attach implementations currently for guests with higher RAM (say more than 8GB), and that unfortunately cannot be addressed in the short term.

---
 MAINTAINERS                       |   1 +
 arch/x86/kernel/pci-dma.c         |   2 +
 drivers/iommu/Kconfig             |   5 +-
 drivers/iommu/Makefile            |   1 +
 drivers/iommu/hyperv-iommu-root.c | 918 ++++++++++++++++++++++++++++++
 include/asm-generic/mshyperv.h    |  17 +
 include/linux/hyperv.h            |   6 +
 7 files changed, 947 insertions(+), 3 deletions(-)
 create mode 100644 drivers/iommu/hyperv-iommu-root.c
MAINTAINERS +1 -0
index f803a6a38fee..8ae040b89a56 100644
@@ -11914,6 +11914,7 @@ F:	drivers/clocksource/hyperv_timer.c
 F:	drivers/hid/hid-hyperv.c
 F:	drivers/hv/
 F:	drivers/input/serio/hyperv-keyboard.c
+F:	drivers/iommu/hyperv-iommu-root.c
 F:	drivers/iommu/hyperv-irq.c
 F:	drivers/net/ethernet/microsoft/
 F:	drivers/net/hyperv/
arch/x86/kernel/pci-dma.c +2 -0
index 6267363e0189..cfeee6505e17 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>
@@ -105,6 +106,7 @@ void __init pci_iommu_alloc(void)
 	gart_iommu_hole_init();
 	amd_iommu_detect();
 	detect_intel_iommu();
+	hv_iommu_detect();
 	swiotlb_init(x86_swiotlb_enable, x86_swiotlb_flags);
 }
 
drivers/iommu/Kconfig +2 -3
index f86262b11416..7909cf4373a6 100644
@@ -352,13 +352,12 @@ config MTK_IOMMU_V1
 	  if unsure, say N here.
 
 config HYPERV_IOMMU
-	bool "Hyper-V IRQ Handling"
+	bool "Hyper-V IOMMU Unit"
 	depends on HYPERV && X86
 	select IOMMU_API
 	default HYPERV
 	help
-	  Stub IOMMU driver to handle IRQs to support Hyper-V Linux
-	  guest and root partitions.
+	  Hyper-V pseudo IOMMU unit.
 
 config VIRTIO_IOMMU
 	tristate "Virtio IOMMU driver"
drivers/iommu/Makefile +1 -0
index 335ea77cced6..296fbc6ca829 100644
@@ -31,6 +31,7 @@ obj-$(CONFIG_EXYNOS_IOMMU) += exynos-iommu.o
 obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o
 obj-$(CONFIG_S390_IOMMU) += s390-iommu.o
 obj-$(CONFIG_HYPERV) += hyperv-irq.o
+obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu-root.o
 obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o
 obj-$(CONFIG_IOMMU_SVA) += iommu-sva.o
 obj-$(CONFIG_IOMMU_IOPF) += io-pgfault.o
drivers/iommu/hyperv-iommu-root.c new file +918 -0

918 lines not on this page · raw message

include/asm-generic/mshyperv.h +17 -0
index 25ac7ca0fd8b..8d5c610da99a 100644
@@ -327,6 +327,23 @@ static inline u64 hv_pci_vmbus_device_id(struct pci_dev *pdev)
 { return 0; }
 #endif /* IS_ENABLED(CONFIG_PCI_HYPERV) */
 
+#if IS_ENABLED(CONFIG_HYPERV_IOMMU)
+u64 hv_get_current_partid(void);
+bool hv_pcidev_is_attached_dev(struct pci_dev *pdev);
+bool hv_pcidev_is_pthru_dev(struct pci_dev *pdev);
+u64 hv_build_devid_oftype(struct pci_dev *pdev, enum hv_device_type type);
+#else
+static inline bool hv_pcidev_is_attached_dev(struct pci_dev *pdev)
+{ return false; }
+static inline bool hv_pcidev_is_pthru_dev(struct pci_dev *pdev)
+{ return false; }
+static inline u64 hv_build_devid_oftype(struct pci_dev *pdev,
+					enum hv_device_type type)
+{ return 0; }
+static inline u64 hv_get_current_partid(void)
+{ return HV_PARTITION_ID_INVALID; }
+#endif /* IS_ENABLED(CONFIG_HYPERV_IOMMU) */
+
 #else /* CONFIG_HYPERV */
 static inline void hv_identify_partition_type(void) {}
 static inline bool hv_is_hyperv_initialized(void) { return false; }
include/linux/hyperv.h +6 -0
index 5459e776ec17..6eee1cbf6f23 100644
@@ -1769,4 +1769,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_IOMMU
+void __init hv_iommu_detect(void);
+#else
+static inline void hv_iommu_detect(void) { }
+#endif /* CONFIG_HYPERV_IOMMU */
+
 #endif /* _HYPERV_H */
Signature · 1 line
2.51.2.vfs.0.1
author
#30 lore raw

PATCH v3 10/11 mshv: Populate mmio mappings for PCI passthru

In reply to Mukesh R

Upon guest access, in case of missing mmio mapping, the hypervisor generates an unmapped gpa intercept. In this path, lookup the PCI resource pfn for the guest gpa, and ask the hypervisor to map it via hypercall. The PCI resource pfn is maintained by the VFIO driver, and obtained via fixup_user_fault call (similar to KVM).

Also, VFIO no longer puts the mmio pfn in vma->vm_pgoff. So, remove code that is using it to map mmio space. It is broken and will cause panic.

---
 drivers/hv/mshv_root_main.c | 113 ++++++++++++++++++++++++++++++------
 1 file changed, 96 insertions(+), 17 deletions(-)
drivers/hv/mshv_root_main.c +96 -17
index 6ceb5f608589..a7864463961b 100644
@@ -46,6 +46,9 @@ MODULE_DESCRIPTION("Microsoft Hyper-V root partition VMM interface /dev/mshv");
 #define HV_VP_COUNTER_ROOT_DISPATCH_THREAD_BLOCKED 95
 #endif
 
+static bool hv_nofull_mmio;	/* don't map entire mmio region upon fault */
+module_param(hv_nofull_mmio, bool, 0644);
+
 struct mshv_root mshv_root;
 
 enum hv_scheduler_type hv_scheduler_type;
@@ -641,6 +644,94 @@ mshv_partition_region_by_gfn_get(struct mshv_partition *p, u64 gfn)
 	return region;
 }
 
+/*
+ * Check if uaddr is for mmio range. If yes, return 0 with mmio_pfn filled in
+ * else just return -errno.
+ */
+static int mshv_chk_get_mmio_start_pfn(u64 uaddr, u64 *mmio_pfnp)
+{
+	struct vm_area_struct *vma;
+	bool is_mmio;
+	struct follow_pfnmap_args pfnmap_args;
+	int rc = -EINVAL;
+
+	mmap_read_lock(current->mm);
+	vma = vma_lookup(current->mm, uaddr);
+	is_mmio = vma ? !!(vma->vm_flags & (VM_IO | VM_PFNMAP)) : 0;
+	if (!is_mmio)
+		goto unlock_mmap_out;
+
+	pfnmap_args.vma = vma;
+	pfnmap_args.address = uaddr;
+
+	rc = follow_pfnmap_start(&pfnmap_args);
+	if (rc) {
+		rc = fixup_user_fault(current->mm, uaddr, FAULT_FLAG_WRITE,
+				      NULL);
+		if (rc)
+			goto unlock_mmap_out;
+
+		rc = follow_pfnmap_start(&pfnmap_args);
+		if (rc)
+			goto unlock_mmap_out;
+	}
+
+	*mmio_pfnp = pfnmap_args.pfn;
+	follow_pfnmap_end(&pfnmap_args);
+
+unlock_mmap_out:
+	mmap_read_unlock(current->mm);
+	return rc;
+}
+
+/*
+ * Check if the unmapped gpa belongs to mmio space. If yes, resolve it.
+ *
+ * Returns: True if valid mmio intercept and handled, else false.
+ */
+static bool mshv_handle_unmapped_gpa(struct mshv_vp *vp)
+{
+	struct hv_message *hvmsg = vp->vp_intercept_msg_page;
+	u64 gfn, uaddr, mmio_spa, numpgs;
+	struct mshv_mem_region *rg;
+	int rc = -EINVAL;
+	struct mshv_partition *pt = vp->vp_partition;
+#if defined(CONFIG_X86_64)
+	struct hv_x64_memory_intercept_message *msg =
+		(struct hv_x64_memory_intercept_message *)hvmsg->u.payload;
+#elif defined(CONFIG_ARM64)
+	struct hv_arm64_memory_intercept_message *msg =
+		(struct hv_arm64_memory_intercept_message *)hvmsg->u.payload;
+#endif
+
+	gfn = msg->guest_physical_address >> HV_HYP_PAGE_SHIFT;
+
+	rg = mshv_partition_region_by_gfn_get(pt, gfn);
+	if (rg == NULL)
+		return false;
+	if (rg->mreg_type != MSHV_REGION_TYPE_MMIO)
+		goto put_rg_out;
+
+	uaddr = rg->start_uaddr + ((gfn - rg->start_gfn) << HV_HYP_PAGE_SHIFT);
+
+	rc = mshv_chk_get_mmio_start_pfn(uaddr, &mmio_spa);
+	if (rc)
+		goto put_rg_out;
+
+	if (!hv_nofull_mmio) {		/* default case */
+		mmio_spa = mmio_spa - (gfn - rg->start_gfn);
+		gfn = rg->start_gfn;
+		numpgs = rg->nr_pages;
+	} else
+		numpgs = 1;
+
+	rc = hv_call_map_mmio_pages(pt->pt_id, gfn, mmio_spa, numpgs);
+
+put_rg_out:
+	mshv_region_put(rg);
+	return rc == 0;
+}
+
 /**
  * mshv_handle_gpa_intercept - Handle GPA (Guest Physical Address) intercepts.
  * @vp: Pointer to the virtual processor structure.
@@ -699,6 +790,8 @@ static bool mshv_handle_gpa_intercept(struct mshv_vp *vp)
 static bool mshv_vp_handle_intercept(struct mshv_vp *vp)
 {
 	switch (vp->vp_intercept_msg_page->header.message_type) {
+	case HVMSG_UNMAPPED_GPA:
+		return mshv_handle_unmapped_gpa(vp);
 	case HVMSG_GPA_INTERCEPT:
 		return mshv_handle_gpa_intercept(vp);
 	}
@@ -1322,16 +1415,8 @@ static int mshv_prepare_pinned_region(struct mshv_mem_region *region)
 }
 
 /*
- * This maps two things: guest RAM and for pci passthru mmio space.
- *
- * mmio:
- *  - vfio overloads vm_pgoff to store the mmio start pfn/spa.
- *  - Two things need to happen for mapping mmio range:
- *	1. mapped in the uaddr so VMM can access it.
- *	2. mapped in the hwpt (gfn <-> mmio phys addr) so guest can access it.
- *
- *   This function takes care of the second. The first one is managed by vfio,
- *   and hence is taken care of via vfio_pci_mmap_fault().
+ * This is called for both user ram and mmio space. The mmio space is not
+ * mapped here, but later during intercept on demand.
  */
 static long
 mshv_map_user_memory(struct mshv_partition *partition,
@@ -1340,7 +1425,6 @@ mshv_map_user_memory(struct mshv_partition *partition,
 	struct mshv_mem_region *region;
 	struct vm_area_struct *vma;
 	bool is_mmio;
-	ulong mmio_pfn;
 	long ret;
 
 	if (mem->flags & BIT(MSHV_SET_MEM_BIT_UNMAP) ||
@@ -1350,7 +1434,6 @@ mshv_map_user_memory(struct mshv_partition *partition,
 	mmap_read_lock(current->mm);
 	vma = vma_lookup(current->mm, mem->userspace_addr);
 	is_mmio = vma ? !!(vma->vm_flags & (VM_IO | VM_PFNMAP)) : 0;
-	mmio_pfn = is_mmio ? vma->vm_pgoff : 0;
 	mmap_read_unlock(current->mm);
 
 	if (!vma)
@@ -1376,11 +1459,7 @@ mshv_map_user_memory(struct mshv_partition *partition,
 					    region->nr_pages,
 					    HV_MAP_GPA_NO_ACCESS, NULL);
 		break;
-	case MSHV_REGION_TYPE_MMIO:
-		ret = hv_call_map_mmio_pages(partition->pt_id,
-					     region->start_gfn,
-					     mmio_pfn,
-					     region->nr_pages);
+	default:
 		break;
 	}
 
Signature · 1 line
2.51.2.vfs.0.1
author
#32 lore raw

PATCH v3 11/11 mshv: Mark mem regions as non-movable upfront if device passthru

In reply to Mukesh R

If a VM is started with device attached, the mem regions must be marked non-movable as the device attach hypercall right away allows the use of SLAT for IOMMU. Marking them non-movable forces mapping of the entire guest RAM in the SLAT at the time of region creation along with the region pinned. Also, because a device could be dynamically attached much later in a VM, create a boot parameter to disable movable pages that users can set if they anticipate such an action.

---
 drivers/hv/mshv_root.h      |  1 +
 drivers/hv/mshv_root_main.c | 15 ++++++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
drivers/hv/mshv_root.h +1 -0
index b9880d0bdc4d..d57c26950203 100644
@@ -141,6 +141,7 @@ struct mshv_partition {
 	pid_t pt_vmm_tgid;
 	bool import_completed;
 	bool pt_initialized;
+	bool pt_regions_pinned;
 #if IS_ENABLED(CONFIG_DEBUG_FS)
 	struct dentry *pt_stats_dentry;
 	struct dentry *pt_vp_dentry;
drivers/hv/mshv_root_main.c +14 -1
index a7864463961b..ac71534733bd 100644
@@ -49,6 +49,10 @@ MODULE_DESCRIPTION("Microsoft Hyper-V root partition VMM interface /dev/mshv");
 static bool hv_nofull_mmio;	/* don't map entire mmio region upon fault */
 module_param(hv_nofull_mmio, bool, 0644);
 
+static bool hv_no_movbl_pgs;	/* disable movable pages completely */
+module_param(hv_no_movbl_pgs, bool, 0644);
+MODULE_PARM_DESC(hv_no_movbl_pgs, "If set, don't do movable pages for VMs");
+
 struct mshv_root mshv_root;
 
 enum hv_scheduler_type hv_scheduler_type;
@@ -1303,6 +1307,12 @@ static void mshv_async_hvcall_handler(void *data, u64 *status)
 	*status = partition->async_hypercall_status;
 }
 
+static bool mshv_do_pt_regions_pinned(struct mshv_partition *pt)
+{
+	return pt->pt_regions_pinned || mshv_partition_encrypted(pt) ||
+	       hv_no_movbl_pgs;
+}
+
 /*
  * NB: caller checks and makes sure mem->size is page aligned
  * Returns: 0 with regionpp updated on success, or -errno
@@ -1333,7 +1343,7 @@ static int mshv_partition_create_region(struct mshv_partition *partition,
 
 	if (is_mmio)
 		rg->mreg_type = MSHV_REGION_TYPE_MMIO;
-	else if (mshv_partition_encrypted(partition) ||
+	else if (mshv_do_pt_regions_pinned(partition) ||
 		 !mshv_region_movable_init(rg))
 		rg->mreg_type = MSHV_REGION_TYPE_MEM_PINNED;
 	else
@@ -1808,6 +1818,9 @@ static long mshv_partition_ioctl_create_device(struct mshv_partition *partition,
 	if (copy_to_user(uarg, &devargk, sizeof(devargk)))
 		return -EFAULT;    /* cleanup in mshv_device_fop_release() */
 
+	/* For now, all regions must be pinned if there is device passthru. */
+	partition->pt_regions_pinned = true;
+
 	return 0;
 
 undo_out:
Signature · 1 line
2.51.2.vfs.0.1
Reviewed-by on v3 1/11
#2 lore raw

Re: [PATCH V3 01/11] iommu/hyperv: Rename hyperv-iommu.c to hyperv-irq.c

In reply to Mukesh R
Quoted 6 lines · Mukesh R

This file actually implements irq remapping, so rename to more appropriate hyperv-irq.c. A new file to implement hyperv iommu will be introduced later. Also, it should not be tied to HYPERV_IOMMU, but to CONFIG_HYPERV and IRQ_REMAP. The file already has #ifdef CONFIG_IRQ_REMAP.

Quoted 82 lines
---
 MAINTAINERS                                    | 2 +-
 drivers/iommu/Makefile                         | 2 +-
 drivers/iommu/{hyperv-iommu.c => hyperv-irq.c} | 6 +++---
 drivers/iommu/irq_remapping.c                  | 2 +-
 4 files changed, 6 insertions(+), 6 deletions(-)
 rename drivers/iommu/{hyperv-iommu.c => hyperv-irq.c} (99%)
MAINTAINERS +1 -1
index d1cc0e12fe1f..f803a6a38fee 100644
@@ -11914,7 +11914,7 @@ F:	drivers/clocksource/hyperv_timer.c
 F:	drivers/hid/hid-hyperv.c
 F:	drivers/hv/
 F:	drivers/input/serio/hyperv-keyboard.c
-F:	drivers/iommu/hyperv-iommu.c
+F:	drivers/iommu/hyperv-irq.c
 F:	drivers/net/ethernet/microsoft/
 F:	drivers/net/hyperv/
 F:	drivers/pci/controller/pci-hyperv-intf.c
drivers/iommu/Makefile +1 -1
index 0275821f4ef9..335ea77cced6 100644
@@ -30,7 +30,7 @@ obj-$(CONFIG_TEGRA_IOMMU_SMMU) += tegra-smmu.o
 obj-$(CONFIG_EXYNOS_IOMMU) += exynos-iommu.o
 obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o
 obj-$(CONFIG_S390_IOMMU) += s390-iommu.o
-obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o
+obj-$(CONFIG_HYPERV) += hyperv-irq.o
 obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o
 obj-$(CONFIG_IOMMU_SVA) += iommu-sva.o
 obj-$(CONFIG_IOMMU_IOPF) += io-pgfault.o
drivers/iommu/hyperv-irq.c renamed from drivers/iommu/hyperv-iommu.c +3 -3
similarity index 99%
rename from drivers/iommu/hyperv-iommu.c
rename to drivers/iommu/hyperv-irq.c
index 479103261ae6..d11076f906fb 100644
@@ -8,6 +8,8 @@
  * Author : Lan Tianyu <[email protected]>
  */
 
+#ifdef CONFIG_IRQ_REMAP
+
 #include <linux/types.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
@@ -24,8 +26,6 @@
 
 #include "irq_remapping.h"
 
-#ifdef CONFIG_IRQ_REMAP
-
 /*
  * According 82093AA IO-APIC spec , IO APIC has a 24-entry Interrupt
  * Redirection Table. Hyper-V exposes one single IO-APIC and so define
@@ -331,4 +331,4 @@ static const struct irq_domain_ops hyperv_root_ir_domain_ops = {
 	.free = hyperv_root_irq_remapping_free,
 };
 
-#endif
+#endif  /* CONFIG_IRQ_REMAP */
drivers/iommu/irq_remapping.c +1 -1
index c2443659812a..41bf65e4ea88 100644
@@ -108,7 +108,7 @@ int __init irq_remapping_prepare(void)
 	else if (IS_ENABLED(CONFIG_AMD_IOMMU) &&
 		 amd_iommu_irq_ops.prepare() == 0)
 		remap_ops = &amd_iommu_irq_ops;
-	else if (IS_ENABLED(CONFIG_HYPERV_IOMMU) &&
+	else if (IS_ENABLED(CONFIG_HYPERV) &&
 		 hyperv_irq_remap_ops.prepare() == 0)
 		remap_ops = &hyperv_irq_remap_ops;
 	else
Signature · 1 line
2.51.2.vfs.0.1
Reviewed-by on v3 4/11
#13 lore raw

Re: [PATCH V3 04/11] mshv: Declarations and definitions for VFIO-MSHV bridge device

In reply to Mukesh R
Quoted 3 lines · Mukesh R

Add data structs needed by the subsequent patch that introduces a new module to implement VFIO-MSHV pseudo device.

Quoted 78 lines
---
 drivers/hv/mshv_root.h    | 19 +++++++++++++++++++
 include/uapi/linux/mshv.h | 30 ++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
drivers/hv/mshv_root.h +19 -0
index a85c24dcc701..b9880d0bdc4d 100644
@@ -227,6 +227,25 @@ struct port_table_info {
 	};
 };
 
+struct mshv_device {
+	const struct mshv_device_ops *device_ops;
+	struct mshv_partition *device_pt;
+	void *device_private;
+	struct hlist_node device_ptnode;
+};
+
+struct mshv_device_ops {
+	const char *device_name;
+	long (*device_create)(struct mshv_device *dev);
+	void (*device_release)(struct mshv_device *dev);
+	long (*device_set_attr)(struct mshv_device *dev,
+				struct mshv_device_attr *attr);
+	long (*device_has_attr)(struct mshv_device *dev,
+				struct mshv_device_attr *attr);
+};
+
+extern struct mshv_device_ops mshv_vfio_device_ops;
+
 int mshv_update_routing_table(struct mshv_partition *partition,
 			      const struct mshv_user_irq_entry *entries,
 			      unsigned int numents);
include/uapi/linux/mshv.h +30 -0
index 32ff92b6342b..be6fe3ee8707 100644
@@ -404,4 +404,34 @@ struct mshv_sint_mask {
 /* hv_hvcall device */
 #define MSHV_HVCALL_SETUP        _IOW(MSHV_IOCTL, 0x1E, struct mshv_vtl_hvcall_setup)
 #define MSHV_HVCALL              _IOWR(MSHV_IOCTL, 0x1F, struct mshv_vtl_hvcall)
+
+/* Device passhthru */
+#define MSHV_CREATE_DEVICE_TEST		1
+
+enum {
+	MSHV_DEV_TYPE_VFIO,
+	MSHV_DEV_TYPE_MAX,
+};
+
+struct mshv_create_device {
+	__u32	type;	     /* in: MSHV_DEV_TYPE_xxx */
+	__u32	fd;	     /* out: device handle */
+	__u32	flags;	     /* in: MSHV_CREATE_DEVICE_xxx */
+};
+
+#define MSHV_DEV_VFIO_FILE      1
+#define MSHV_DEV_VFIO_FILE_ADD	1
+#define MSHV_DEV_VFIO_FILE_DEL	2
+
+struct mshv_device_attr {
+	__u32	flags;		/* no flags currently defined */
+	__u32	group;		/* device-defined */
+	__u64	attr;		/* group-defined */
+	__u64	addr;		/* userspace address of attr data */
+};
+
+/* Device fds created with MSHV_CREATE_DEVICE */
+#define MSHV_SET_DEVICE_ATTR	_IOW(MSHV_IOCTL, 0x00, struct mshv_device_attr)
+#define MSHV_HAS_DEVICE_ATTR	_IOW(MSHV_IOCTL, 0x01, struct mshv_device_attr)
+
 #endif
Signature · 1 line
2.51.2.vfs.0.1
Reviewed-by on v3 2/11
#8 lore raw

Re: [PATCH V3 02/11] x86/hyperv: Cosmetic changes in irqdomain.c for readability

In reply to Mukesh R
Quoted 11 lines · Mukesh R

Make cosmetic changes:
o Rename struct pci_dev *dev to *pdev since there are cases of struct device *dev in the file and all over the kernel
o Rename hv_build_pci_dev_id to hv_build_devid_type_pci in anticipation of building different types of device IDs
o Fix checkpatch.pl issues with return and extraneous printk
o Replace spaces with tabs
o Rename struct hv_devid *xxx to struct hv_devid *hv_devid given code paths involve many types of device IDs
o Fix indentation in a large if block by using goto.

Quoted 3 lines

There are no functional changes.

Quoted 416 lines
---
 arch/x86/hyperv/irqdomain.c | 198 +++++++++++++++++++-----------------
 1 file changed, 104 insertions(+), 94 deletions(-)
arch/x86/hyperv/irqdomain.c +104 -94
index 365e364268d9..b3ad50a874dc 100644
@@ -1,5 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0
-
 /*
  * Irqdomain for Linux to run as the root partition on Microsoft Hypervisor.
  *
@@ -14,8 +13,8 @@
 #include <linux/irqchip/irq-msi-lib.h>
 #include <asm/mshyperv.h>
 
-static int hv_map_interrupt(union hv_device_id device_id, bool level,
-		int cpu, int vector, struct hv_interrupt_entry *entry)
+static int hv_map_interrupt(union hv_device_id hv_devid, bool level,
+		int cpu, int vector, struct hv_interrupt_entry *ret_entry)
 {
 	struct hv_input_map_device_interrupt *input;
 	struct hv_output_map_device_interrupt *output;
@@ -32,7 +31,7 @@ static int hv_map_interrupt(union hv_device_id device_id, bool level,
 	intr_desc = &input->interrupt_descriptor;
 	memset(input, 0, sizeof(*input));
 	input->partition_id = hv_current_partition_id;
-	input->device_id = device_id.as_uint64;
+	input->device_id = hv_devid.as_uint64;
 	intr_desc->interrupt_type = HV_X64_INTERRUPT_TYPE_FIXED;
 	intr_desc->vector_count = 1;
 	intr_desc->target.vector = vector;
@@ -44,7 +43,7 @@ static int hv_map_interrupt(union hv_device_id device_id, bool level,
 
 	intr_desc->target.vp_set.valid_bank_mask = 0;
 	intr_desc->target.vp_set.format = HV_GENERIC_SET_SPARSE_4K;
-	nr_bank = cpumask_to_vpset(&(intr_desc->target.vp_set), cpumask_of(cpu));
+	nr_bank = cpumask_to_vpset(&intr_desc->target.vp_set, cpumask_of(cpu));
 	if (nr_bank < 0) {
 		local_irq_restore(flags);
 		pr_err("%s: unable to generate VP set\n", __func__);
@@ -61,7 +60,7 @@ static int hv_map_interrupt(union hv_device_id device_id, bool level,
 
 	status = hv_do_rep_hypercall(HVCALL_MAP_DEVICE_INTERRUPT, 0, var_size,
 			input, output);
-	*entry = output->interrupt_entry;
+	*ret_entry = output->interrupt_entry;
 
 	local_irq_restore(flags);
 
@@ -71,21 +70,19 @@ static int hv_map_interrupt(union hv_device_id device_id, bool level,
 	return hv_result_to_errno(status);
 }
 
-static int hv_unmap_interrupt(u64 id, struct hv_interrupt_entry *old_entry)
+static int hv_unmap_interrupt(u64 id, struct hv_interrupt_entry *irq_entry)
 {
 	unsigned long flags;
 	struct hv_input_unmap_device_interrupt *input;
-	struct hv_interrupt_entry *intr_entry;
 	u64 status;
 
 	local_irq_save(flags);
 	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
 
 	memset(input, 0, sizeof(*input));
-	intr_entry = &input->interrupt_entry;
 	input->partition_id = hv_current_partition_id;
 	input->device_id = id;
-	*intr_entry = *old_entry;
+	input->interrupt_entry = *irq_entry;
 
 	status = hv_do_hypercall(HVCALL_UNMAP_DEVICE_INTERRUPT, input, NULL);
 	local_irq_restore(flags);
@@ -115,67 +112,71 @@ static int get_rid_cb(struct pci_dev *pdev, u16 alias, void *data)
 	return 0;
 }
 
-static union hv_device_id hv_build_pci_dev_id(struct pci_dev *dev)
+static union hv_device_id hv_build_devid_type_pci(struct pci_dev *pdev)
 {
-	union hv_device_id dev_id;
+	int pos;
+	union hv_device_id hv_devid;
 	struct rid_data data = {
 		.bridge = NULL,
-		.rid = PCI_DEVID(dev->bus->number, dev->devfn)
+		.rid = PCI_DEVID(pdev->bus->number, pdev->devfn)
 	};
 
-	pci_for_each_dma_alias(dev, get_rid_cb, &data);
+	pci_for_each_dma_alias(pdev, get_rid_cb, &data);
 
-	dev_id.as_uint64 = 0;
-	dev_id.device_type = HV_DEVICE_TYPE_PCI;
-	dev_id.pci.segment = pci_domain_nr(dev->bus);
+	hv_devid.as_uint64 = 0;
+	hv_devid.device_type = HV_DEVICE_TYPE_PCI;
+	hv_devid.pci.segment = pci_domain_nr(pdev->bus);
 
-	dev_id.pci.bdf.bus = PCI_BUS_NUM(data.rid);
-	dev_id.pci.bdf.device = PCI_SLOT(data.rid);
-	dev_id.pci.bdf.function = PCI_FUNC(data.rid);
-	dev_id.pci.source_shadow = HV_SOURCE_SHADOW_NONE;
+	hv_devid.pci.bdf.bus = PCI_BUS_NUM(data.rid);
+	hv_devid.pci.bdf.device = PCI_SLOT(data.rid);
+	hv_devid.pci.bdf.function = PCI_FUNC(data.rid);
+	hv_devid.pci.source_shadow = HV_SOURCE_SHADOW_NONE;
 
-	if (data.bridge) {
-		int pos;
+	if (data.bridge == NULL)
+		goto out;
 
-		/*
-		 * Microsoft Hypervisor requires a bus range when the bridge is
-		 * running in PCI-X mode.
-		 *
-		 * To distinguish conventional vs PCI-X bridge, we can check
-		 * the bridge's PCI-X Secondary Status Register, Secondary Bus
-		 * Mode and Frequency bits. See PCI Express to PCI/PCI-X Bridge
-		 * Specification Revision 1.0 5.2.2.1.3.
-		 *
-		 * Value zero means it is in conventional mode, otherwise it is
-		 * in PCI-X mode.
-		 */
+	/*
+	 * Microsoft Hypervisor requires a bus range when the bridge is
+	 * running in PCI-X mode.
+	 *
+	 * To distinguish conventional vs PCI-X bridge, we can check
+	 * the bridge's PCI-X Secondary Status Register, Secondary Bus
+	 * Mode and Frequency bits. See PCI Express to PCI/PCI-X Bridge
+	 * Specification Revision 1.0 5.2.2.1.3.
+	 *
+	 * Value zero means it is in conventional mode, otherwise it is
+	 * in PCI-X mode.
+	 */
 
-		pos = pci_find_capability(data.bridge, PCI_CAP_ID_PCIX);
-		if (pos) {
-			u16 status;
+	pos = pci_find_capability(data.bridge, PCI_CAP_ID_PCIX);
+	if (pos) {
+		u16 status;
 
-			pci_read_config_word(data.bridge, pos +
-					PCI_X_BRIDGE_SSTATUS, &status);
+		pci_read_config_word(data.bridge, pos + PCI_X_BRIDGE_SSTATUS,
+				     &status);
 
-			if (status & PCI_X_SSTATUS_FREQ) {
-				/* Non-zero, PCI-X mode */
-				u8 sec_bus, sub_bus;
+		if (status & PCI_X_SSTATUS_FREQ) {
+			/* Non-zero, PCI-X mode */
+			u8 sec_bus, sub_bus;
 
-				dev_id.pci.source_shadow = HV_SOURCE_SHADOW_BRIDGE_BUS_RANGE;
+			hv_devid.pci.source_shadow =
+					     HV_SOURCE_SHADOW_BRIDGE_BUS_RANGE;
 
-				pci_read_config_byte(data.bridge, PCI_SECONDARY_BUS, &sec_bus);
-				dev_id.pci.shadow_bus_range.secondary_bus = sec_bus;
-				pci_read_config_byte(data.bridge, PCI_SUBORDINATE_BUS, &sub_bus);
-				dev_id.pci.shadow_bus_range.subordinate_bus = sub_bus;
-			}
+			pci_read_config_byte(data.bridge, PCI_SECONDARY_BUS,
+					     &sec_bus);
+			hv_devid.pci.shadow_bus_range.secondary_bus = sec_bus;
+			pci_read_config_byte(data.bridge, PCI_SUBORDINATE_BUS,
+					     &sub_bus);
+			hv_devid.pci.shadow_bus_range.subordinate_bus = sub_bus;
 		}
 	}
 
-	return dev_id;
+out:
+	return hv_devid;
 }
 
-/**
- * hv_map_msi_interrupt() - "Map" the MSI IRQ in the hypervisor.
+/*
+ * hv_map_msi_interrupt() - Map the MSI IRQ in the hypervisor.
  * @data:      Describes the IRQ
  * @out_entry: Hypervisor (MSI) interrupt entry (can be NULL)
  *
@@ -188,22 +189,23 @@ int hv_map_msi_interrupt(struct irq_data *data,
 {
 	struct irq_cfg *cfg = irqd_cfg(data);
 	struct hv_interrupt_entry dummy;
-	union hv_device_id device_id;
+	union hv_device_id hv_devid;
 	struct msi_desc *msidesc;
-	struct pci_dev *dev;
+	struct pci_dev *pdev;
 	int cpu;
 
 	msidesc = irq_data_get_msi_desc(data);
-	dev = msi_desc_to_pci_dev(msidesc);
-	device_id = hv_build_pci_dev_id(dev);
+	pdev = msi_desc_to_pci_dev(msidesc);
+	hv_devid = hv_build_devid_type_pci(pdev);
 	cpu = cpumask_first(irq_data_get_effective_affinity_mask(data));
 
-	return hv_map_interrupt(device_id, false, cpu, cfg->vector,
+	return hv_map_interrupt(hv_devid, false, cpu, cfg->vector,
 				out_entry ? out_entry : &dummy);
 }
 EXPORT_SYMBOL_GPL(hv_map_msi_interrupt);
 
-static inline void entry_to_msi_msg(struct hv_interrupt_entry *entry, struct msi_msg *msg)
+static void entry_to_msi_msg(struct hv_interrupt_entry *entry,
+			     struct msi_msg *msg)
 {
 	/* High address is always 0 */
 	msg->address_hi = 0;
@@ -211,17 +213,19 @@ static inline void entry_to_msi_msg(struct hv_interrupt_entry *entry, struct msi
 	msg->data = entry->msi_entry.data.as_uint32;
 }
 
-static int hv_unmap_msi_interrupt(struct pci_dev *dev, struct hv_interrupt_entry *old_entry);
+static int hv_unmap_msi_interrupt(struct pci_dev *pdev,
+				  struct hv_interrupt_entry *irq_entry);
+
 static void hv_irq_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
 {
 	struct hv_interrupt_entry *stored_entry;
 	struct irq_cfg *cfg = irqd_cfg(data);
 	struct msi_desc *msidesc;
-	struct pci_dev *dev;
+	struct pci_dev *pdev;
 	int ret;
 
 	msidesc = irq_data_get_msi_desc(data);
-	dev = msi_desc_to_pci_dev(msidesc);
+	pdev = msi_desc_to_pci_dev(msidesc);
 
 	if (!cfg) {
 		pr_debug("%s: cfg is NULL", __func__);
@@ -240,7 +244,7 @@ static void hv_irq_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
 		stored_entry = data->chip_data;
 		data->chip_data = NULL;
 
-		ret = hv_unmap_msi_interrupt(dev, stored_entry);
+		ret = hv_unmap_msi_interrupt(pdev, stored_entry);
 
 		kfree(stored_entry);
 
@@ -249,10 +253,8 @@ static void hv_irq_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
 	}
 
 	stored_entry = kzalloc_obj(*stored_entry, GFP_ATOMIC);
-	if (!stored_entry) {
-		pr_debug("%s: failed to allocate chip data\n", __func__);
+	if (!stored_entry)
 		return;
-	}
 
 	ret = hv_map_msi_interrupt(data, stored_entry);
 	if (ret) {
@@ -262,18 +264,21 @@ static void hv_irq_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
 
 	data->chip_data = stored_entry;
 	entry_to_msi_msg(data->chip_data, msg);
-
-	return;
 }
 
-static int hv_unmap_msi_interrupt(struct pci_dev *dev, struct hv_interrupt_entry *old_entry)
+static int hv_unmap_msi_interrupt(struct pci_dev *pdev,
+				  struct hv_interrupt_entry *irq_entry)
 {
-	return hv_unmap_interrupt(hv_build_pci_dev_id(dev).as_uint64, old_entry);
+	union hv_device_id hv_devid;
+
+	hv_devid = hv_build_devid_type_pci(pdev);
+	return hv_unmap_interrupt(hv_devid.as_uint64, irq_entry);
 }
 
-static void hv_teardown_msi_irq(struct pci_dev *dev, struct irq_data *irqd)
+/* NB: during map, hv_interrupt_entry is saved via data->chip_data */
+static void hv_teardown_msi_irq(struct pci_dev *pdev, struct irq_data *irqd)
 {
-	struct hv_interrupt_entry old_entry;
+	struct hv_interrupt_entry irq_entry;
 	struct msi_msg msg;
 
 	if (!irqd->chip_data) {
@@ -281,13 +286,13 @@ static void hv_teardown_msi_irq(struct pci_dev *dev, struct irq_data *irqd)
 		return;
 	}
 
-	old_entry = *(struct hv_interrupt_entry *)irqd->chip_data;
-	entry_to_msi_msg(&old_entry, &msg);
+	irq_entry = *(struct hv_interrupt_entry *)irqd->chip_data;
+	entry_to_msi_msg(&irq_entry, &msg);
 
 	kfree(irqd->chip_data);
 	irqd->chip_data = NULL;
 
-	(void)hv_unmap_msi_interrupt(dev, &old_entry);
+	(void)hv_unmap_msi_interrupt(pdev, &irq_entry);
 }
 
 /*
@@ -302,7 +307,8 @@ static struct irq_chip hv_pci_msi_controller = {
 };
 
 static bool hv_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
-				 struct irq_domain *real_parent, struct msi_domain_info *info)
+				 struct irq_domain *real_parent,
+				 struct msi_domain_info *info)
 {
 	struct irq_chip *chip = info->chip;
 
@@ -317,7 +323,8 @@ static bool hv_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
 }
 
 #define HV_MSI_FLAGS_SUPPORTED	(MSI_GENERIC_FLAGS_MASK | MSI_FLAG_PCI_MSIX)
-#define HV_MSI_FLAGS_REQUIRED	(MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS)
+#define HV_MSI_FLAGS_REQUIRED	(MSI_FLAG_USE_DEF_DOM_OPS |	\
+				 MSI_FLAG_USE_DEF_CHIP_OPS)
 
 static struct msi_parent_ops hv_msi_parent_ops = {
 	.supported_flags	= HV_MSI_FLAGS_SUPPORTED,
@@ -329,14 +336,14 @@ static struct msi_parent_ops hv_msi_parent_ops = {
 	.init_dev_msi_info	= hv_init_dev_msi_info,
 };
 
-static int hv_msi_domain_alloc(struct irq_domain *d, unsigned int virq, unsigned int nr_irqs,
-			       void *arg)
+/* Allocate nr_irqs IRQs for the given irq domain */
+static int hv_msi_domain_alloc(struct irq_domain *d, unsigned int virq,
+			       unsigned int nr_irqs, void *arg)
 {
 	/*
-	 * TODO: The allocation bits of hv_irq_compose_msi_msg(), i.e. everything except
-	 * entry_to_msi_msg() should be in here.
+	 * TODO: The allocation bits of hv_irq_compose_msi_msg(), i.e.
+	 *	 everything except entry_to_msi_msg() should be in here.
 	 */
-
 	int ret;
 
 	ret = irq_domain_alloc_irqs_parent(d, virq, nr_irqs, arg);
@@ -344,13 +351,15 @@ static int hv_msi_domain_alloc(struct irq_domain *d, unsigned int virq, unsigned
 		return ret;
 
 	for (int i = 0; i < nr_irqs; ++i) {
-		irq_domain_set_info(d, virq + i, 0, &hv_pci_msi_controller, NULL,
-				    handle_edge_irq, NULL, "edge");
+		irq_domain_set_info(d, virq + i, 0, &hv_pci_msi_controller,
+				    NULL, handle_edge_irq, NULL, "edge");
 	}
+
 	return 0;
 }
 
-static void hv_msi_domain_free(struct irq_domain *d, unsigned int virq, unsigned int nr_irqs)
+static void hv_msi_domain_free(struct irq_domain *d, unsigned int virq,
+			       unsigned int nr_irqs)
 {
 	for (int i = 0; i < nr_irqs; ++i) {
 		struct irq_data *irqd = irq_domain_get_irq_data(d, virq);
@@ -362,6 +371,7 @@ static void hv_msi_domain_free(struct irq_domain *d, unsigned int virq, unsigned
 
 		hv_teardown_msi_irq(to_pci_dev(desc->dev), irqd);
 	}
+
 	irq_domain_free_irqs_top(d, virq, nr_irqs);
 }
 
@@ -394,25 +404,25 @@ struct irq_domain * __init hv_create_pci_msi_domain(void)
 
 int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry)
 {
-	union hv_device_id device_id;
+	union hv_device_id hv_devid;
 
-	device_id.as_uint64 = 0;
-	device_id.device_type = HV_DEVICE_TYPE_IOAPIC;
-	device_id.ioapic.ioapic_id = (u8)ioapic_id;
+	hv_devid.as_uint64 = 0;
+	hv_devid.device_type = HV_DEVICE_TYPE_IOAPIC;
+	hv_devid.ioapic.ioapic_id = (u8)ioapic_id;
 
-	return hv_unmap_interrupt(device_id.as_uint64, entry);
+	return hv_unmap_interrupt(hv_devid.as_uint64, entry);
 }
 EXPORT_SYMBOL_GPL(hv_unmap_ioapic_interrupt);
 
 int hv_map_ioapic_interrupt(int ioapic_id, bool level, int cpu, int vector,
 		struct hv_interrupt_entry *entry)
 {
-	union hv_device_id device_id;
+	union hv_device_id hv_devid;
 
-	device_id.as_uint64 = 0;
-	device_id.device_type = HV_DEVICE_TYPE_IOAPIC;
-	device_id.ioapic.ioapic_id = (u8)ioapic_id;
+	hv_devid.as_uint64 = 0;
+	hv_devid.device_type = HV_DEVICE_TYPE_IOAPIC;
+	hv_devid.ioapic.ioapic_id = (u8)ioapic_id;
 
-	return hv_map_interrupt(device_id, level, cpu, vector, entry);
+	return hv_map_interrupt(hv_devid, level, cpu, vector, entry);
 }
 EXPORT_SYMBOL_GPL(hv_map_ioapic_interrupt);
Signature · 1 line
2.51.2.vfs.0.1
#20 lore raw

Re: [PATCH V3 08/11] PCI: hv: VMBus and PCI device IDs for PCI passthru

In reply to Mukesh R
Quoted 12 lines · Mukesh R

On Hyper-V, most hypercalls related to PCI passthru to map/unmap regions, interrupts, etc need a device ID as a parameter. This device ID refers to that specific device during the lifetime of passthru.

An L1VH VM only contains VMBus based devices. A device ID for a VMBus device is slightly different in that it uses the hv_pcibus_device info for building it to make sure it matches exactly what the hypervisor expects. This VMBus based device ID is needed when attaching devices in an L1VH based guest VM. Add a function to build and export it. Before building it, a check is done to make sure the device is a valid VMBus device.

Quoted 2 lines

In remaining cases, PCI device ID is used. So, also make PCI device ID build function hv_build_devid_type_pci() public.

The subject line should start with a verb to help say what this patch does. I guess it's something about adding a function to build the device IDs expected by the hypervisor? Would be good to include the function name too, e.g., something like this:

PCI: hv: Add foo() to build hypervisor device IDs

#3 lore raw

Re: [PATCH V3 01/11] iommu/hyperv: Rename hyperv-iommu.c to hyperv-irq.c

In reply to Mukesh R

Hi Mukesh,

Quoted 6 lines · On Mon, 11 May 2026 19:02:49 -0700 Mukesh R <[email protected]> wrote:

This file actually implements irq remapping, so rename to more appropriate hyperv-irq.c. A new file to implement hyperv iommu will be introduced later. Also, it should not be tied to HYPERV_IOMMU, but to CONFIG_HYPERV and IRQ_REMAP. The file already has #ifdef CONFIG_IRQ_REMAP.

Quoted 6 lines
---
 MAINTAINERS                                    | 2 +-
 drivers/iommu/Makefile                         | 2 +-
 drivers/iommu/{hyperv-iommu.c => hyperv-irq.c} | 6 +++---

Given that we have multiple Hyper-V IOMMU-related files — this renamed hyperv-irq.c, the existing hyperv-iommu code, iommu-root (this series) and the recently posted guest pvIOMMU driver — should we create a drivers/iommu/hyperv/ directory to consolidate them?

Quoted 74 lines
 drivers/iommu/irq_remapping.c                  | 2 +-
 4 files changed, 6 insertions(+), 6 deletions(-)
 rename drivers/iommu/{hyperv-iommu.c => hyperv-irq.c} (99%)
MAINTAINERS +1 -1
index d1cc0e12fe1f..f803a6a38fee 100644
@@ -11914,7 +11914,7 @@ F:	drivers/clocksource/hyperv_timer.c
 F:	drivers/hid/hid-hyperv.c
 F:	drivers/hv/
 F:	drivers/input/serio/hyperv-keyboard.c
-F:	drivers/iommu/hyperv-iommu.c
+F:	drivers/iommu/hyperv-irq.c
 F:	drivers/net/ethernet/microsoft/
 F:	drivers/net/hyperv/
 F:	drivers/pci/controller/pci-hyperv-intf.c
drivers/iommu/Makefile +1 -1
index 0275821f4ef9..335ea77cced6 100644
@@ -30,7 +30,7 @@ obj-$(CONFIG_TEGRA_IOMMU_SMMU) += tegra-smmu.o
 obj-$(CONFIG_EXYNOS_IOMMU) += exynos-iommu.o
 obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o
 obj-$(CONFIG_S390_IOMMU) += s390-iommu.o
-obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o
+obj-$(CONFIG_HYPERV) += hyperv-irq.o
 obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o
 obj-$(CONFIG_IOMMU_SVA) += iommu-sva.o
 obj-$(CONFIG_IOMMU_IOPF) += io-pgfault.o
drivers/iommu/hyperv-irq.c renamed from drivers/iommu/hyperv-iommu.c +2 -2
similarity index 99%
rename from drivers/iommu/hyperv-iommu.c
rename to drivers/iommu/hyperv-irq.c
index 479103261ae6..d11076f906fb 100644
@@ -8,6 +8,8 @@
  * Author : Lan Tianyu <[email protected]>
  */
 
+#ifdef CONFIG_IRQ_REMAP
+
 #include <linux/types.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
@@ -24,8 +26,6 @@
 
 #include "irq_remapping.h"
 
-#ifdef CONFIG_IRQ_REMAP
-
 /*
  * According 82093AA IO-APIC spec , IO APIC has a 24-entry Interrupt
  * Redirection Table. Hyper-V exposes one single IO-APIC and so

define @@ -331,4 +331,4 @@ static const struct irq_domain_ops hyperv_root_ir_domain_ops = { .free = hyperv_root_irq_remapping_free,
};

drivers/iommu/hyperv-irq.c renamed from drivers/iommu/hyperv-iommu.c +1 -1
-#endif
+#endif  /* CONFIG_IRQ_REMAP */
(quoted diff) +0 -0

b/drivers/iommu/irq_remapping.c index c2443659812a..41bf65e4ea88 100644 --- a/drivers/iommu/irq_remapping.c
+++ b/drivers/iommu/irq_remapping.c

drivers/iommu/irq_remapping.c +1 -1
@@ -108,7 +108,7 @@ int __init irq_remapping_prepare(void)
 	else if (IS_ENABLED(CONFIG_AMD_IOMMU) &&
 		 amd_iommu_irq_ops.prepare() == 0)
 		remap_ops = &amd_iommu_irq_ops;
-	else if (IS_ENABLED(CONFIG_HYPERV_IOMMU) &&
+	else if (IS_ENABLED(CONFIG_HYPERV) &&
 		 hyperv_irq_remap_ops.prepare() == 0)
 		remap_ops = &hyperv_irq_remap_ops;
 	else
author
#4 lore raw

Re: [PATCH V3 01/11] iommu/hyperv: Rename hyperv-iommu.c to hyperv-irq.c

Quoted 17 lines · Jacob Pan

Hi Mukesh,

Quoted 6 lines · On Mon, 11 May 2026 19:02:49 -0700 Mukesh R <[email protected]> wrote:

This file actually implements irq remapping, so rename to more appropriate hyperv-irq.c. A new file to implement hyperv iommu will be introduced later. Also, it should not be tied to HYPERV_IOMMU, but to CONFIG_HYPERV and IRQ_REMAP. The file already has #ifdef CONFIG_IRQ_REMAP.

Quoted 6 lines
---
  MAINTAINERS                                    | 2 +-
  drivers/iommu/Makefile                         | 2 +-
  drivers/iommu/{hyperv-iommu.c => hyperv-irq.c} | 6 +++---
Quoted 4 lines

Given that we have multiple Hyper-V IOMMU-related files ? this renamed hyperv-irq.c, the existing hyperv-iommu code, iommu-root (this series) and the recently posted guest pvIOMMU driver ? should we create a drivers/iommu/hyperv/ directory to consolidate them?

This came up briefly during synup with arm64 devs whether to split the file into arch specific and common. We decided to wait till arm is working so we can tell how intrusive the #ifdefs are. We can decide as part of the arm port patches. I am ok also if you want to do it as part of the pv-iommu patches as follow up once this is merged.

Thanks,
-Mukesh

Quoted 74 lines
Quoted 74 lines
  drivers/iommu/irq_remapping.c                  | 2 +-
  4 files changed, 6 insertions(+), 6 deletions(-)

rename drivers/iommu/{hyperv-iommu.c => hyperv-irq.c} (99%)

MAINTAINERS +1 -1
index d1cc0e12fe1f..f803a6a38fee 100644
@@ -11914,7 +11914,7 @@ F:	drivers/clocksource/hyperv_timer.c
  F:	drivers/hid/hid-hyperv.c
  F:	drivers/hv/
  F:	drivers/input/serio/hyperv-keyboard.c
-F:	drivers/iommu/hyperv-iommu.c
+F:	drivers/iommu/hyperv-irq.c
  F:	drivers/net/ethernet/microsoft/
  F:	drivers/net/hyperv/
  F:	drivers/pci/controller/pci-hyperv-intf.c
drivers/iommu/Makefile +1 -1
index 0275821f4ef9..335ea77cced6 100644
@@ -30,7 +30,7 @@ obj-$(CONFIG_TEGRA_IOMMU_SMMU) += tegra-smmu.o
  obj-$(CONFIG_EXYNOS_IOMMU) += exynos-iommu.o
  obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o
  obj-$(CONFIG_S390_IOMMU) += s390-iommu.o
-obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o
+obj-$(CONFIG_HYPERV) += hyperv-irq.o
  obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o
  obj-$(CONFIG_IOMMU_SVA) += iommu-sva.o
  obj-$(CONFIG_IOMMU_IOPF) += io-pgfault.o
drivers/iommu/hyperv-irq.c renamed from drivers/iommu/hyperv-iommu.c +2 -2
similarity index 99%
rename from drivers/iommu/hyperv-iommu.c
rename to drivers/iommu/hyperv-irq.c
index 479103261ae6..d11076f906fb 100644
@@ -8,6 +8,8 @@
   * Author : Lan Tianyu <[email protected]>
   */
 
+#ifdef CONFIG_IRQ_REMAP
+
  #include <linux/types.h>
  #include <linux/interrupt.h>
  #include <linux/irq.h>
@@ -24,8 +26,6 @@
 
  #include "irq_remapping.h"
 
-#ifdef CONFIG_IRQ_REMAP
-
  /*
   * According 82093AA IO-APIC spec , IO APIC has a 24-entry Interrupt
   * Redirection Table. Hyper-V exposes one single IO-APIC and so

define @@ -331,4 +331,4 @@ static const struct irq_domain_ops hyperv_root_ir_domain_ops = { .free = hyperv_root_irq_remapping_free,
};

drivers/iommu/hyperv-irq.c renamed from drivers/iommu/hyperv-iommu.c +1 -1
-#endif
+#endif  /* CONFIG_IRQ_REMAP */
(quoted diff) +0 -0

b/drivers/iommu/irq_remapping.c index c2443659812a..41bf65e4ea88 100644 --- a/drivers/iommu/irq_remapping.c
+++ b/drivers/iommu/irq_remapping.c

drivers/iommu/irq_remapping.c +1 -1
@@ -108,7 +108,7 @@ int __init irq_remapping_prepare(void)
  	else if (IS_ENABLED(CONFIG_AMD_IOMMU) &&
  		 amd_iommu_irq_ops.prepare() == 0)
  		remap_ops = &amd_iommu_irq_ops;
-	else if (IS_ENABLED(CONFIG_HYPERV_IOMMU) &&
+	else if (IS_ENABLED(CONFIG_HYPERV) &&
  		 hyperv_irq_remap_ops.prepare() == 0)
  		remap_ops = &hyperv_irq_remap_ops;
  	else
#5 lore raw

RE: [PATCH V3 01/11] iommu/hyperv: Rename hyperv-iommu.c to hyperv-irq.c

In reply to Jacob Pan

From: Jacob Pan <[email protected]> Sent: Tuesday, May 12, 2026 4:46 PM

Quoted 19 lines

Hi Mukesh,

Quoted 6 lines · On Mon, 11 May 2026 19:02:49 -0700 Mukesh R <[email protected]> wrote:

This file actually implements irq remapping, so rename to more appropriate hyperv-irq.c. A new file to implement hyperv iommu will be introduced later. Also, it should not be tied to HYPERV_IOMMU, but to CONFIG_HYPERV and IRQ_REMAP. The file already has #ifdef CONFIG_IRQ_REMAP.

Quoted 6 lines
---
 MAINTAINERS                                    | 2 +-
 drivers/iommu/Makefile                         | 2 +-
 drivers/iommu/{hyperv-iommu.c => hyperv-irq.c} | 6 +++---
Quoted 4 lines

Given that we have multiple Hyper-V IOMMU-related files — this renamed hyperv-irq.c, the existing hyperv-iommu code, iommu-root (this series) and the recently posted guest pvIOMMU driver — should we create a drivers/iommu/hyperv/ directory to consolidate them?

Patch 1/4 in the guest pvIOMMU driver [1] that was recently posted by Yu Zhang does as you suggest.

Michael

[1] https://lore.kernel.org/linux-hyperv/[email protected]/

Quoted 76 lines
Quoted 75 lines
 drivers/iommu/irq_remapping.c                  | 2 +-
 4 files changed, 6 insertions(+), 6 deletions(-)
 rename drivers/iommu/{hyperv-iommu.c => hyperv-irq.c} (99%)
MAINTAINERS +1 -1
index d1cc0e12fe1f..f803a6a38fee 100644
@@ -11914,7 +11914,7 @@ F:	drivers/clocksource/hyperv_timer.c
 F:	drivers/hid/hid-hyperv.c
 F:	drivers/hv/
 F:	drivers/input/serio/hyperv-keyboard.c
-F:	drivers/iommu/hyperv-iommu.c
+F:	drivers/iommu/hyperv-irq.c
 F:	drivers/net/ethernet/microsoft/
 F:	drivers/net/hyperv/
 F:	drivers/pci/controller/pci-hyperv-intf.c
drivers/iommu/Makefile +1 -1
index 0275821f4ef9..335ea77cced6 100644
@@ -30,7 +30,7 @@ obj-$(CONFIG_TEGRA_IOMMU_SMMU) += tegra-smmu.o
 obj-$(CONFIG_EXYNOS_IOMMU) += exynos-iommu.o
 obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o
 obj-$(CONFIG_S390_IOMMU) += s390-iommu.o
-obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o
+obj-$(CONFIG_HYPERV) += hyperv-irq.o
 obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o
 obj-$(CONFIG_IOMMU_SVA) += iommu-sva.o
 obj-$(CONFIG_IOMMU_IOPF) += io-pgfault.o
drivers/iommu/hyperv-irq.c renamed from drivers/iommu/hyperv-iommu.c +2 -2
similarity index 99%
rename from drivers/iommu/hyperv-iommu.c
rename to drivers/iommu/hyperv-irq.c
index 479103261ae6..d11076f906fb 100644
@@ -8,6 +8,8 @@
  * Author : Lan Tianyu <[email protected]>
  */
 
+#ifdef CONFIG_IRQ_REMAP
+
 #include <linux/types.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
@@ -24,8 +26,6 @@
 
 #include "irq_remapping.h"
 
-#ifdef CONFIG_IRQ_REMAP
-
 /*
  * According 82093AA IO-APIC spec , IO APIC has a 24-entry Interrupt
  * Redirection Table. Hyper-V exposes one single IO-APIC and so

define @@ -331,4 +331,4 @@ static const struct irq_domain_ops hyperv_root_ir_domain_ops = { .free = hyperv_root_irq_remapping_free,
};

drivers/iommu/hyperv-irq.c renamed from drivers/iommu/hyperv-iommu.c +1 -1
-#endif
+#endif  /* CONFIG_IRQ_REMAP */
(quoted diff) +0 -0

b/drivers/iommu/irq_remapping.c index c2443659812a..41bf65e4ea88 100644 --- a/drivers/iommu/irq_remapping.c
+++ b/drivers/iommu/irq_remapping.c

drivers/iommu/irq_remapping.c +1 -1
@@ -108,7 +108,7 @@ int __init irq_remapping_prepare(void)
 	else if (IS_ENABLED(CONFIG_AMD_IOMMU) &&
 		 amd_iommu_irq_ops.prepare() == 0)
 		remap_ops = &amd_iommu_irq_ops;
-	else if (IS_ENABLED(CONFIG_HYPERV_IOMMU) &&
+	else if (IS_ENABLED(CONFIG_HYPERV) &&
 		 hyperv_irq_remap_ops.prepare() == 0)
 		remap_ops = &hyperv_irq_remap_ops;
 	else
#9 lore raw

Re: [PATCH V3 02/11] x86/hyperv: Cosmetic changes in irqdomain.c for readability

In reply to Mukesh R
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Resource leak in `hv_msi_domain_free()` when freeing multiple IRQs. - [High] Uninitialized memory…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Resource leak in hv_msi_domain_free() when freeing multiple IRQs.
- [High] Uninitialized memory write to hardware MSI registers in hv_irq_compose_msi_msg() error paths.
- [High] Invalid hardware state read in hv_build_devid_type_pci() for Type 0 PCI-X leaf devices. --

commit 8c84576bf69ec6b4d58407408095bf71a9940bf2
Author: Mukesh R <[email protected]>

x86/hyperv: Cosmetic changes in irqdomain.c for readability

Make cosmetic changes such as renaming variables for consistency, fixing checkpatch warnings, updating indentation, and replacing spaces with tabs.

Quoted 3 lines
arch/x86/hyperv/irqdomain.c +0 -0

[ ... ]

Quoted 7 lines
arch/x86/hyperv/irqdomain.c +1 -1
@@ -115,67 +112,71 @@ static int get_rid_cb(struct pci_dev *pdev, u16 alias, void *data)
 	return 0;
 }
 
-static union hv_device_id hv_build_pci_dev_id(struct pci_dev *dev)
+static union hv_device_id hv_build_devid_type_pci(struct pci_dev *pdev)
 {

[ ... ]

Quoted 8 lines
arch/x86/hyperv/irqdomain.c +2 -5
-	if (data.bridge) {
-		int pos;
+	if (data.bridge == NULL)
+		goto out;
 
-		/*
-		 * Microsoft Hypervisor requires a bus range when the bridge is
-		 * running in PCI-X mode.

[ ... ]

arch/x86/hyperv/irqdomain.c static int get_rid_cb(struct pci_dev *pdev, u16 alias, void *data) · line 153

Quoted 6 lines
arch/x86/hyperv/irqdomain.c +3 -3
-		pos = pci_find_capability(data.bridge, PCI_CAP_ID_PCIX);
-		if (pos) {
-			u16 status;
+	pos = pci_find_capability(data.bridge, PCI_CAP_ID_PCIX);
+	if (pos) {
+		u16 status;

If the leaf device is on the root bus, data.bridge will point to the leaf device itself (a Type 0 device) after pci_for_each_dma_alias() completes. Does this code incorrectly assume data.bridge is always a Type 1 bridge?

If it evaluates a Type 0 device here, reading PCI_X_BRIDGE_SSTATUS aliases the Type 0 Command register. This could cause the code to read standard Type 1 PCI_SECONDARY_BUS and PCI_SUBORDINATE_BUS offsets (which fall into the middle of BAR2 for a Type 0 device), sending arbitrary BAR values as shadow bus ranges to the hypervisor.

[ ... ]

arch/x86/hyperv/irqdomain.c static void hv_irq_compose_msi_msg(struct irq_data *data, struct msi_msg *msg) · line 255

Quoted 9 lines
arch/x86/hyperv/irqdomain.c +1 -3
@@ -249,10 +253,8 @@ static void hv_irq_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
 	}
 
 	stored_entry = kzalloc_obj(*stored_entry, GFP_ATOMIC);
-	if (!stored_entry) {
-		pr_debug("%s: failed to allocate chip data\n", __func__);
+	if (!stored_entry)
 		return;
-	}

If kzalloc_obj() or hv_map_msi_interrupt() fails, this returns early without populating the struct msi_msg *msg parameter.

Since callers (like msi_domain_activate() or irq_chip_compose_msi_msg()) allocate msg on the stack and cannot check for errors due to the void return type, does this result in uninitialized stack memory being blindly written to the hardware MSI registers?

[ ... ]

Quoted 9 lines
arch/x86/hyperv/irqdomain.c +2 -2
@@ -344,13 +351,15 @@ static int hv_msi_domain_alloc(struct irq_domain *d, unsigned int virq, unsigned
 		return ret;
 
 	for (int i = 0; i < nr_irqs; ++i) {
-		irq_domain_set_info(d, virq + i, 0, &hv_pci_msi_controller, NULL,
-				    handle_edge_irq, NULL, "edge");
+		irq_domain_set_info(d, virq + i, 0, &hv_pci_msi_controller,
+				    NULL, handle_edge_irq, NULL, "edge");
 	}

arch/x86/hyperv/irqdomain.c static int hv_msi_domain_alloc(struct irq_domain *d, unsigned int virq, unsigned · line 365

Quoted 10 lines
arch/x86/hyperv/irqdomain.c +3 -1
+
 	return 0;
 }
 
-static void hv_msi_domain_free(struct irq_domain *d, unsigned int virq, unsigned int nr_irqs)
+static void hv_msi_domain_free(struct irq_domain *d, unsigned int virq,
+			       unsigned int nr_irqs)
 {
 	for (int i = 0; i < nr_irqs; ++i) {
 		struct irq_data *irqd = irq_domain_get_irq_data(d, virq);

Does this loop leak chip_data and leave IRQs mapped in the hypervisor for the remaining nr_irqs - 1 IRQs?

It appears to call irq_domain_get_irq_data() using the base virq on every iteration instead of virq + i, causing hv_teardown_msi_irq() to be repeatedly invoked on the first IRQ only.

Quoted 7 lines
arch/x86/hyperv/irqdomain.c +1 -0
@@ -362,6 +371,7 @@ static void hv_msi_domain_free(struct irq_domain *d, unsigned int virq, unsigned
 
 		hv_teardown_msi_irq(to_pci_dev(desc->dev), irqd);
 	}
+
 	irq_domain_free_irqs_top(d, virq, nr_irqs);
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#11 lore raw

Re: [PATCH V3 03/11] mshv: Provide a way to get partition ID if running in a VMM process

In reply to Mukesh R
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Tying VM partition IDs to process TGIDs (`current->tgid`) is an architectural flaw causing…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Tying VM partition IDs to process TGIDs (current->tgid) is an architectural flaw causing isolation breakage and security risks.
- [High] mshv_current_partid() returns a raw partition ID without taking a reference, creating a TOCTOU vulnerability.
- [High] Pre-existing Use-After-Free in destroy_partition() due to RCU-001 violation during VP teardown.
--

commit 528e38745b2ae16c0fd8ad4876d893d1b1fea027
Author: Mukesh R <[email protected]>

mshv: Provide a way to get partition ID if running in a VMM process

This commit introduces mshv_current_partid() to allow retrieving the partition id for the current vmm process. It adds a pt_vmm_tgid field to the mshv_partition struct and sets it during partition creation. The partition id is found by iterating the global hash table.

Quoted 9 lines
drivers/hv/mshv_root_main.c +1 -0
index bd1359eb58dd4..02c107458be90 100644
@@ -1908,6 +1908,27 @@ mshv_partition_release(struct inode *inode, struct file *filp)
 	return 0;
 }
 
+/* Given a process tgid, return partition id if it is a VMM process */

drivers/hv/mshv_root_main.c mshv_partition_release(struct inode *inode, struct file *filp) · line 1921

Quoted 10 lines
drivers/hv/mshv_root_main.c +10 -0
+u64 mshv_current_partid(void)
+{
+	struct mshv_partition *pt;
+	int i;
+	u64 ret_ptid = HV_PARTITION_ID_INVALID;
+
+	rcu_read_lock();
+
+	hash_for_each_rcu(mshv_root.pt_htable, i, pt, pt_hnode) {
+		if (pt->pt_vmm_tgid == current->tgid) {

Is tying the partition id to the process tgid safe?

Since a single vmm process can create multiple partitions, this loop returns the first partition matching the tgid. This could result in pci passthrough operations being misrouted to the wrong partition if a process manages multiple virtual machines.

Also, since the partition's lifecycle is tied to an anonymous file descriptor, passing the fd to another process and exiting would break mshv_current_partid() for the new process.

Additionally, if the operating system recycles the old tgid, an unrelated process might gain access to the partition id.

Should explicit file descriptor tracking be used instead of global process ids?

drivers/hv/mshv_root_main.c mshv_partition_release(struct inode *inode, struct file *filp) · line 1928

Quoted 7 lines
drivers/hv/mshv_root_main.c +7 -0
+			ret_ptid = pt->pt_id;
+			break;
+		}
+	}
+
+	rcu_read_unlock();
+	return ret_ptid;

Does returning a raw partition id without taking a reference create a race condition?

After rcu_read_unlock() is called and the pt_id is returned, a concurrent thread in the vmm could close the partition's file descriptor, causing the partition to be deleted in the hypervisor.

If the hypervisor recycles the pt_id for a newly created partition belonging to a completely different process, the pending iommu attachment or pci passthrough hypercall will use the stale pt_id.

Can this allow the vmm to inadvertently attach its devices to an unrelated partition?

drivers/hv/mshv_root_main.c mshv_partition_release(struct inode *inode, struct file *filp) · line 1929

Quoted 1 line
drivers/hv/mshv_root_main.c +1 -0
+}

This isn't a regression in this patch, but looking at the partition lifecycle code, does destroy_partition() have a use-after-free?

destroy_partition() frees the virtual processor structures (kfree(vp)) before calling remove_partition(partition), which removes the partition from the rcu-protected global hash table (pt_htable) and calls synchronize_rcu().

Because the partition remains visible to rcu readers during the teardown loop, can an interrupt handler (e.g., in mshv_synic.c) call mshv_partition_find(), successfully retrieve the partition, and read
vp = partition->pt_vp_array[vp_index]?

If this happens concurrently with or immediately after kfree(vp), the interrupt handler would access freed memory.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#15 lore raw

Re: [PATCH V3 05/11] mshv: Implement mshv bridge device for VFIO

In reply to Mukesh R
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Unprotected access to `pt_devices` list and missing lockdep assertion. - [Low] The…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Unprotected access to pt_devices list and missing lockdep assertion.
- [Low] The mshv_vfio_device_ops structure is missing the const qualifier. --

commit 96ab470ecc51b095ed8b1538e4abdf37d030b039
Author: Mukesh R <[email protected]>
Subject: mshv: Implement mshv bridge device for VFIO

This commit introduces a new VFIO-MSHV bridge pseudo device file, adapted from KVM's VFIO bridge. It provides the necessary device operations to link VFIO files with an MSHV partition.

Quoted 5 lines
drivers/hv/mshv_vfio.c new file +0 -0
new file mode 100644
index 0000000000000..00a97920e25ba

[ ... ]

drivers/hv/mshv_vfio.c line 175

Quoted 10 lines
drivers/hv/mshv_vfio.c new file +10 -0
+static long mshv_vfio_create_device(struct mshv_device *mshvdev)
+{
+	struct mshv_device *tmp;
+	struct mshv_vfio *mshv_vfio;
+
+	/* Only one VFIO "device" per VM */
+	hlist_for_each_entry(tmp, &mshvdev->device_pt->pt_devices,
+			     device_ptnode)
+		if (tmp->device_ops == &mshv_vfio_device_ops)
+			return -EBUSY;

Are we missing a lockdep assertion here to ensure the partition lock is held?

Since this iterates over the pt_devices list to enforce a singleton device, could concurrent ioctl calls bypass this check and corrupt the hlist if the caller doesn't acquire the lock?

[ ... ]

drivers/hv/mshv_vfio.c line 211

Quoted 7 lines
drivers/hv/mshv_vfio.c new file +7 -0
+struct mshv_device_ops mshv_vfio_device_ops = {
+	.device_name = "mshv-vfio",
+	.device_create = mshv_vfio_create_device,
+	.device_release = mshv_vfio_release_device,
+	.device_set_attr = mshv_vfio_set_attr,
+	.device_has_attr = mshv_vfio_has_attr,
+};

Should mshv_vfio_device_ops be declared as const?

The structure consists entirely of function pointers and is never modified after initialization. Making it const would place it in read-only memory.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#17 lore raw

Re: [PATCH V3 06/11] mshv: Add ioctl support for MSHV-VFIO bridge device

In reply to Mukesh R
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] FD leak and potential deadlock on copy_to_user() failure after anon_inode_getfd(). - [Medium]…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] FD leak and potential deadlock on copy_to_user() failure after anon_inode_getfd().
- [Medium] Asymmetric memory lifecycle and inconsistent device_release handling.
- [Medium] Missing validation of unknown flags in mshv_partition_ioctl_create_device(). --

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

mshv: Add ioctl support for MSHV-VFIO bridge device

This commit adds an ioctl for creating MSHV devices for a partition. Currently only VFIO device types are supported, doing setup for the bridge code in mshv_vfio.c.

Quoted 4 lines
drivers/hv/mshv_root_main.c +0 -0
index 02c107458be90..6ceb5f608589f 100644

[ ... ]

Quoted 1 line
drivers/hv/mshv_root_main.c +0 -0
@@ -1625,6 +1625,119 @@ mshv_partition_ioctl_initialize(struct mshv_partition *partition)

[ ... ]

drivers/hv/mshv_root_main.c mshv_partition_ioctl_initialize(struct mshv_partition *partition) · line 1675

Quoted 11 lines
drivers/hv/mshv_root_main.c +11 -0
+static int mshv_device_fop_release(struct inode *inode, struct file *filp)
+{
+	struct mshv_device *mshv_dev = filp->private_data;
+	struct mshv_partition *partition = mshv_dev->device_pt;
+
+	if (mshv_dev->device_ops->device_release) {
+		mutex_lock(&partition->pt_mutex);
+		hlist_del(&mshv_dev->device_ptnode);
+		mshv_dev->device_ops->device_release(mshv_dev);
+		mutex_unlock(&partition->pt_mutex);
+	}

What happens if a future device type does not implement the device_release callback? It looks like the device would remain indefinitely linked in partition->pt_devices while the file descriptor is closed, leading to list corruption and a memory leak.

Since the device structure is allocated by the core module via kzalloc(), would it be safer to handle the list removal and generic kfree() unconditionally in this function?

Quoted 18 lines
drivers/hv/mshv_root_main.c +18 -0
+
+	mshv_partition_put(partition);
+	return 0;
+}
+
+static const struct file_operations mshv_device_fops = {
+	.owner = THIS_MODULE,
+	.unlocked_ioctl = mshv_device_fop_ioctl,
+	.release = mshv_device_fop_release,
+};
+
+static long mshv_partition_ioctl_create_device(struct mshv_partition *partition,
+					       void __user *uarg)
+{
+	long rc;
+	struct mshv_create_device devargk;
+	struct mshv_device *mshv_dev;
+	const struct mshv_device_ops *vfio_ops;

drivers/hv/mshv_root_main.c mshv_partition_ioctl_initialize(struct mshv_partition *partition) · line 1703

Quoted 10 lines
drivers/hv/mshv_root_main.c +10 -0
+
+	if (copy_from_user(&devargk, uarg, sizeof(devargk)))
+		return -EFAULT;
+
+	/* At present, only VFIO is supported */
+	if (devargk.type != MSHV_DEV_TYPE_VFIO)
+		return -ENODEV;
+
+	if (devargk.flags & MSHV_CREATE_DEVICE_TEST)
+		return 0;

Should we explicitly validate that no undefined flags are set here? If a future kernel version introduces new flags for device creation, older kernels will silently ignore them and return success instead of -EINVAL, breaking forward compatibility.

Quoted 17 lines
drivers/hv/mshv_root_main.c +17 -0
+
+	/* This is freed later by mshv_vfio_release_device() */
+	mshv_dev = kzalloc(sizeof(*mshv_dev), GFP_KERNEL_ACCOUNT);
+	if (mshv_dev == NULL)
+		return -ENOMEM;
+
+	vfio_ops = &mshv_vfio_device_ops;
+	mshv_dev->device_ops = vfio_ops;
+	mshv_dev->device_pt = partition;
+
+	rc = vfio_ops->device_create(mshv_dev);
+	if (rc < 0) {
+		kfree(mshv_dev);
+		return rc;
+	}
+
+	hlist_add_head(&mshv_dev->device_ptnode, &partition->pt_devices);

drivers/hv/mshv_root_main.c mshv_partition_ioctl_initialize(struct mshv_partition *partition) · line 1730

Quoted 10 lines
drivers/hv/mshv_root_main.c +10 -0
+
+	mshv_partition_get(partition);
+	rc = anon_inode_getfd(vfio_ops->device_name, &mshv_device_fops,
+			      mshv_dev, O_RDWR | O_CLOEXEC);
+	if (rc < 0)
+		goto undo_out;
+
+	devargk.fd = rc;
+	if (copy_to_user(uarg, &devargk, sizeof(devargk)))
+		return -EFAULT;    /* cleanup in mshv_device_fop_release() */

Does this sequence leak the file descriptor if copy_to_user() fails?

The anon_inode_getfd() call installs the file descriptor into the process's fd table. If the subsequent copy_to_user() fails, the function returns -EFAULT, but the user space application never receives the fd number to explicitly close it, resulting in a leaked file descriptor.

Additionally, can this trigger a deadlock?

The ioctl is called with partition->pt_mutex held. If a malicious user provides a buffer backed by userfaultfd, the copy_to_user() call will block, holding the mutex. Concurrently, another thread could find the new fd in /proc/self/fd and call close(), which invokes mshv_device_fop_release(). That release function unconditionally attempts to acquire partition->pt_mutex, resulting in a deadlock.

Would it be better to separate file allocation and installation by using get_unused_fd_flags(), anon_inode_getfile(), and only calling fd_install() after copy_to_user() succeeds?

drivers/hv/mshv_root_main.c mshv_partition_ioctl_initialize(struct mshv_partition *partition) · line 1736

Quoted 6 lines
drivers/hv/mshv_root_main.c +6 -0
+
+	return 0;
+
+undo_out:
+	hlist_del(&mshv_dev->device_ptnode);
+	vfio_ops->device_release(mshv_dev);    /* will kfree(mshv_dev) */

If a future device type does not implement the device_release callback, could this result in a NULL pointer dereference?

Quoted 3 lines
drivers/hv/mshv_root_main.c +3 -0
+	mshv_partition_put(partition);
+	return rc;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#21 lore raw

Re: [PATCH V3 08/11] PCI: hv: VMBus and PCI device IDs for PCI passthru

In reply to Mukesh R
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Conflicting declarations and missing `#ifdef` guards for `hv_build_devid_type_pci()` cause build…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Conflicting declarations and missing #ifdef guards for hv_build_devid_type_pci() cause build failures under certain Kconfig combinations. --

commit 086da02227f217e959d3022c592e1b8698814d5b
Author: Mukesh R <[email protected]>

PCI: hv: VMBus and PCI device IDs for PCI passthru

This commit introduces functions to build and export VMBus and PCI device IDs for use with Hyper-V PCI passthru. It exposes hv_build_devid_type_pci() and adds hv_pci_vmbus_device_id() to generate the device IDs expected by the hypervisor.

Quoted 12 lines
arch/x86/hyperv/irqdomain.c +1 -1
index b3ad50a874dca..8780573a43326 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;

[ ... ]

Quoted 4 lines
arch/x86/include/asm/mshyperv.h +0 -0
index f64393e853ee3..2ef34001f8d31 100644

[ ... ]

arch/x86/include/asm/mshyperv.h void hv_crash_asm_end(void); · line 255

Quoted 9 lines
arch/x86/include/asm/mshyperv.h +5 -0
@@ -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 */
 
+#if IS_ENABLED(CONFIG_HYPERV_IOMMU)
+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 /* IS_ENABLED(CONFIG_HYPERV_IOMMU) */

Will this cause build failures under certain Kconfig combinations?

If CONFIG_PCI_MSI=y and CONFIG_HYPERV_IOMMU=n, arch/x86/hyperv/irqdomain.c provides a non-static definition of hv_build_devid_type_pci(), but this header provides a static inline definition, which leads to a redefinition error.

Conversely, if CONFIG_PCI_MSI=n and CONFIG_HYPERV_IOMMU=y, this header declares the function, but it is never defined because irqdomain.c is not compiled, resulting in linker errors.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#25 lore raw

Re: [PATCH V3 09/11] x86/hyperv: Implement Hyper-V virtual IOMMU

In reply to Mukesh R
Thank you for your contribution! Sashiko AI review found 10 potential issue(s) to consider: - [Critical] `hv_iommu_unmap_pages` makes a rep hypercall but ignores the completion count, leaving pages…

Thank you for your contribution! Sashiko AI review found 10 potential issue(s) to consider:
- [Critical] hv_iommu_unmap_pages makes a rep hypercall but ignores the completion count, leaving pages permanently mapped.
- [Critical] Integer truncation in hv_iommu_unmap_pages causes incomplete hardware unmapping.
- [Critical] Global unique_id data race leads to IOMMU domain collisions and cross-VM DMA sharing.
- [High] The sscanf return value check in hv_iommu_probe_device breaks the loop on success instead of failure.
- [High] Partial unmaps corrupt the interval tree state, desynchronizing hardware and software.
- [High] Unsafe interval tree iteration leads to skipped nodes during removal.
- [High] Memory leak of interval tree nodes on domain free.
- [High] Ignoring gfp flags causes premature OOM and risks sleeping in atomic context.
- [Medium] Missing NULL check on mapped pointer in fast path.
- [Low] The __setup handlers violate the API by returning 0.
--

commit 4a3c258bcace1805e7afe537c6eb3e451a6fd030
Author: Mukesh R <[email protected]>

x86/hyperv: Implement Hyper-V virtual IOMMU

Add a new file to implement management of device domains, mapping and unmapping of IOMMU memory, and other iommu_ops to fit within the VFIO framework for PCI passthru on Hyper-V running Linux as baremetal root or L1VH root.

Quoted 5 lines
drivers/iommu/hyperv-iommu-root.c new file +0 -0
new file mode 100644
index 0000000000000..a2e0f6cc78e66

[ ... ]

drivers/iommu/hyperv-iommu-root.c line 22

Quoted 7 lines
drivers/iommu/hyperv-iommu-root.c new file +7 -0
+static char *pci_devs_to_skip;
+static int __init hv_iommu_setup_skip(char *str)
+{
+	pci_devs_to_skip = str;
+
+	return 0;
+}

Should this return 1 instead of 0? Returning 0 causes the kernel to print an unknown command line parameters warning and passes the parameters to the init process.

drivers/iommu/hyperv-iommu-root.c line 32

Quoted 10 lines
drivers/iommu/hyperv-iommu-root.c new file +10 -0
+/* hv_iommu_skip=(SSSS:BB:DD.F)(SSSS:BB:DD.F) */
+__setup("hv_iommu_skip=", hv_iommu_setup_skip);
+
+bool hv_no_attdev;	 /* disable direct device attach for passthru */
+EXPORT_SYMBOL_GPL(hv_no_attdev);
+static int __init setup_hv_no_attdev(char *str)
+{
+	hv_no_attdev = true;
+	return 0;
+}

Should this also return 1 instead of 0 to indicate the parameter was handled?

[ ... ]

Quoted 4 lines
drivers/iommu/hyperv-iommu-root.c new file +4 -0
+static struct iommu_domain *hv_iommu_domain_alloc_paging(struct device *dev)
+{
+	struct hv_domain *hvdom;
+	int rc;

[ ... ]

drivers/iommu/hyperv-iommu-root.c line 269

Quoted 3 lines
drivers/iommu/hyperv-iommu-root.c new file +3 -0
+	/* Called under iommu group mutex, so single threaded */
+	if (++unique_id == HV_DEVICE_DOMAIN_ID_S2_NULL)   /* ie, UINTMAX */
+		goto out_err;

Can this increment of unique_id race if called concurrently?

iommu_domain_alloc() can be called by subsystems without holding global or group locks. If multiple allocations race, it could assign identical domid_num values to different domains, which might place them in the same S2 domain.

[ ... ]

Quoted 11 lines
drivers/iommu/hyperv-iommu-root.c new file +11 -0
+static void hv_iommu_domain_free(struct iommu_domain *immdom)
+{
+	struct hv_domain *hvdom = to_hv_domain(immdom);
+	unsigned long flags;
+	u64 status;
+	struct hv_input_delete_device_domain *input;
+
+	if (hv_special_domain(hvdom))
+		return;
+
+	if (!hv_dom_owner_is_vmm(hvdom) || hv_no_attdev) {

[ ... ]

drivers/iommu/hyperv-iommu-root.c line 291

Quoted 4 lines
drivers/iommu/hyperv-iommu-root.c new file +4 -0
+	}
+
+	kfree(hvdom);
+}

Does this code leak the interval tree nodes?

It looks like the remaining struct hv_iommu_mapping nodes in hvdom->mappings_tree are not freed before kfree(hvdom).

[ ... ]

drivers/iommu/hyperv-iommu-root.c line 534

Quoted 8 lines
drivers/iommu/hyperv-iommu-root.c new file +8 -0
+static int hv_iommu_add_tree_mapping(struct hv_domain *hvdom,
+				     unsigned long iova, phys_addr_t paddr,
+				     size_t size, u32 flags)
+{
+	unsigned long irqflags;
+	struct hv_iommu_mapping *mapping;
+
+	mapping = kzalloc(sizeof(*mapping), GFP_ATOMIC);

Should this use the gfp flag passed into hv_iommu_map_pages() instead of GFP_ATOMIC? It might avoid failing prematurely under memory pressure when the caller can sleep.

[ ... ]

Quoted 9 lines
drivers/iommu/hyperv-iommu-root.c new file +9 -0
+static size_t hv_iommu_del_tree_mappings(struct hv_domain *hvdom,
+					unsigned long iova, size_t size)
+{
+	unsigned long flags;
+	size_t unmapped = 0;
+	unsigned long last = iova + size - 1;
+	struct hv_iommu_mapping *mapping = NULL;
+	struct interval_tree_node *node, *next;
+

drivers/iommu/hyperv-iommu-root.c line 568

Quoted 10 lines
drivers/iommu/hyperv-iommu-root.c new file +10 -0
+	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);
+		next = interval_tree_iter_next(node, iova, last);
+
+		/* Trying to split a mapping? Not supported for now. */
+		if (mapping->iova.start < iova)
+			break;

Does breaking out early here leave stale mappings in the tree?

If the target region is unmapped in hardware, the tracking node would remain, causing stale lookups. Also, if unmapping the head of a mapping, is the entire node deleted even though the tail is still mapped?

drivers/iommu/hyperv-iommu-root.c line 572

Quoted 4 lines
drivers/iommu/hyperv-iommu-root.c new file +4 -0
+
+		unmapped += mapping->iova.last - mapping->iova.start + 1;
+
+		interval_tree_remove(node, &hvdom->mappings_tree);

Can erasing the node corrupt the iteration?

Since interval_tree_remove() rebalances the tree and alters parent pointers, calling interval_tree_iter_next(next) on the next loop iteration might skip overlapping nodes.

[ ... ]

Quoted 12 lines
drivers/iommu/hyperv-iommu-root.c new file +12 -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;
+	unsigned long npages, done = 0;
+	struct hv_domain *hvdom = to_hv_domain(immdom);
+	size_t size = pgsize * pgcount;
+
+	map_flags = HV_MAP_GPA_READABLE;	/* required */

drivers/iommu/hyperv-iommu-root.c line 640

Quoted 10 lines
drivers/iommu/hyperv-iommu-root.c new file +10 -0
+	map_flags |= prot & IOMMU_WRITE ? HV_MAP_GPA_WRITABLE : 0;
+
+	ret = hv_iommu_add_tree_mapping(hvdom, iova, paddr, size, map_flags);
+	if (ret)
+		return ret;
+
+	if (hvdom->attached_dom) {
+		*mapped = size;
+		return 0;
+	}

Is it possible for mapped to be NULL here?

The fast path assigns to *mapped without validating the pointer, while later in the function there are defensive checks like if (mapped).

[ ... ]

drivers/iommu/hyperv-iommu-root.c line 659

Quoted 4 lines
drivers/iommu/hyperv-iommu-root.c new file +4 -0
+		if (hv_result(status) == HV_STATUS_INSUFFICIENT_MEMORY) {
+			ret = hv_call_deposit_pages(NUMA_NO_NODE,
+						    hv_current_partition_id,
+						    256);

Could this trigger a sleeping in atomic context bug?

hv_call_deposit_pages() typically performs sleepable allocations, but the caller of hv_iommu_map_pages() might have provided a non-sleeping gfp flag which was not forwarded.

[ ... ]

Quoted 9 lines
drivers/iommu/hyperv-iommu-root.c new file +9 -0
+static size_t hv_iommu_unmap_pages(struct iommu_domain *immdom, ulong iova,
+				   size_t pgsize, size_t pgcount,
+				   struct iommu_iotlb_gather *gather)
+{
+	unsigned long flags, npages;
+	struct hv_input_unmap_device_gpa_pages *input;
+	u64 status;
+	struct hv_domain *hvdom = to_hv_domain(immdom);
+	size_t unmapped, size = pgsize * pgcount;

[ ... ]

drivers/iommu/hyperv-iommu-root.c line 720

Quoted 13 lines
drivers/iommu/hyperv-iommu-root.c new file +13 -0
+	npages = size >> HV_HYP_PAGE_SHIFT;
+
+	local_irq_save(flags);
+	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+	memset(input, 0, sizeof(*input));
+
+	input->device_domain.partition_id = HV_PARTITION_ID_SELF;
+	input->device_domain.domain_id.type = HV_DEVICE_DOMAIN_TYPE_S2;
+	input->device_domain.domain_id.id = hvdom->domid_num;
+	input->target_device_va_base = iova;
+
+	status = hv_do_rep_hypercall(HVCALL_UNMAP_DEVICE_GPA_PAGES, npages,
+				     0, input, NULL);

Can npages be truncated here?

The hypercall parameter rep_count in hv_do_rep_hypercall() expects a u16. If npages is >= 65536, it would be silently truncated, causing incomplete unmapping in the hardware.

Does this also need a loop to handle partial completions?

hv_do_rep_hypercall() might only partially complete. Since the completion count from hv_repcomp(status) is ignored, remaining pages could be left fully mapped in the hardware.

[ ... ]

Quoted 8 lines
drivers/iommu/hyperv-iommu-root.c new file +8 -0
+static struct iommu_device *hv_iommu_probe_device(struct device *dev)
+{
+	if (!dev_is_pci(dev))
+		return ERR_PTR(-ENODEV);
+
+	if (pci_devs_to_skip && *pci_devs_to_skip) {
+		int rc, pos = 0;
+		int parsed;

drivers/iommu/hyperv-iommu-root.c line 771

Quoted 10 lines
drivers/iommu/hyperv-iommu-root.c new file +10 -0
+		int segment, bus, slot, func;
+		struct pci_dev *pdev = to_pci_dev(dev);
+
+		do {
+			parsed = 0;
+
+			rc = sscanf(pci_devs_to_skip + pos, " (%x:%x:%x.%x) %n",
+				    &segment, &bus, &slot, &func, &parsed);
+			if (rc)
+				break;

Should this check for failure instead of success?

sscanf() returns the number of successfully matched items. If it succeeds, rc will be 4, causing the loop to break prematurely without checking the parsed values against the device.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Reviewed-by on v3 8/11
#22 lore raw

Re: [PATCH V3 08/11] PCI: hv: VMBus and PCI device IDs for PCI passthru

In reply to Mukesh R
Quoted 12 lines · Mukesh R

On Hyper-V, most hypercalls related to PCI passthru to map/unmap regions, interrupts, etc need a device ID as a parameter. This device ID refers to that specific device during the lifetime of passthru.

An L1VH VM only contains VMBus based devices. A device ID for a VMBus device is slightly different in that it uses the hv_pcibus_device info for building it to make sure it matches exactly what the hypervisor expects. This VMBus based device ID is needed when attaching devices in an L1VH based guest VM. Add a function to build and export it. Before building it, a check is done to make sure the device is a valid VMBus device.

Quoted 3 lines

In remaining cases, PCI device ID is used. So, also make PCI device ID build function hv_build_devid_type_pci() public.

Quoted 148 lines
---
 arch/x86/hyperv/irqdomain.c         |  9 +++++----
 arch/x86/include/asm/mshyperv.h     |  6 ++++++
 drivers/pci/controller/pci-hyperv.c | 24 ++++++++++++++++++++++++
 include/asm-generic/mshyperv.h      | 11 +++++++++++
 4 files changed, 46 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..2ef34001f8d3 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 */
 
+#if IS_ENABLED(CONFIG_HYPERV_IOMMU)
+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 /* IS_ENABLED(CONFIG_HYPERV_IOMMU) */
+
 #else /* CONFIG_HYPERV */
 static inline void hyperv_init(void) {}
 static inline void hyperv_setup_mmu_ops(void) {}
drivers/pci/controller/pci-hyperv.c +24 -0
index cfc8fa403dad..50d793ca8f31 100644
@@ -573,6 +573,7 @@ struct hv_pci_compl {
 };
 
 static void hv_pci_onchannelcallback(void *context);
+static bool hv_vmbus_pci_device(struct pci_bus *pbus);
 
 #ifdef CONFIG_X86
 #define DELIVERY_MODE		APIC_DELIVERY_MODE_FIXED
@@ -1005,6 +1006,24 @@ static struct irq_domain *hv_pci_get_root_domain(void)
 static void hv_arch_irq_unmask(struct irq_data *data) { }
 #endif /* CONFIG_ARM64 */
 
+u64 hv_pci_vmbus_device_id(struct pci_dev *pdev)
+{
+	struct hv_pcibus_device *hbus;
+	struct pci_bus *pbus = pdev->bus;
+
+	if (!hv_vmbus_pci_device(pbus))
+		return 0;
+
+	hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
+
+	return	(hbus->hdev->dev_instance.b[5] << 24) |
+		(hbus->hdev->dev_instance.b[4] << 16) |
+		(hbus->hdev->dev_instance.b[7] << 8) |
+		(hbus->hdev->dev_instance.b[6] & 0xf8) |
+		PCI_FUNC(pdev->devfn);
+}
+EXPORT_SYMBOL_GPL(hv_pci_vmbus_device_id);
+
 /**
  * hv_pci_generic_compl() - Invoked for a completion packet
  * @context:		Set up by the sender of the packet.
@@ -1403,6 +1422,11 @@ static struct pci_ops hv_pcifront_ops = {
 	.write = hv_pcifront_write_config,
 };
 
+static bool hv_vmbus_pci_device(struct pci_bus *pbus)
+{
+	return pbus->ops == &hv_pcifront_ops;
+}
+
 /*
  * Paravirtual backchannel
  *
include/asm-generic/mshyperv.h +11 -0
index e8cbc4e3f7ad..25ac7ca0fd8b 100644
@@ -204,6 +204,9 @@ extern u64 (*hv_read_reference_counter)(void);
 /* Sentinel value for an uninitialized entry in hv_vp_index array */
 #define VP_INVAL	U32_MAX
 
+/* Forward declarations */
+struct pci_dev;
+
 int __init hv_common_init(void);
 void __init hv_get_partition_id(void);
 void __init hv_common_free(void);
@@ -316,6 +319,14 @@ void hv_para_set_synic_register(unsigned int reg, u64 val);
 void hyperv_cleanup(void);
 bool hv_query_ext_cap(u64 cap_query);
 void hv_setup_dma_ops(struct device *dev, bool coherent);
+
+#if IS_ENABLED(CONFIG_PCI_HYPERV)
+u64 hv_pci_vmbus_device_id(struct pci_dev *pdev);
+#else
+static inline u64 hv_pci_vmbus_device_id(struct pci_dev *pdev)
+{ return 0; }
+#endif /* IS_ENABLED(CONFIG_PCI_HYPERV) */
+
 #else /* CONFIG_HYPERV */
 static inline void hv_identify_partition_type(void) {}
 static inline bool hv_is_hyperv_initialized(void) { return false; }
Signature · 1 line
2.51.2.vfs.0.1
#23 lore raw

Re: [PATCH V3 08/11] PCI: hv: VMBus and PCI device IDs for PCI passthru

Quoted 13 lines · Souradeep Chakrabarti
Quoted 12 lines · Mukesh R

On Hyper-V, most hypercalls related to PCI passthru to map/unmap regions, interrupts, etc need a device ID as a parameter. This device ID refers to that specific device during the lifetime of passthru.

An L1VH VM only contains VMBus based devices. A device ID for a VMBus device is slightly different in that it uses the hv_pcibus_device info for building it to make sure it matches exactly what the hypervisor expects. This VMBus based device ID is needed when attaching devices in an L1VH based guest VM. Add a function to build and export it. Before building it, a check is done to make sure the device is a valid VMBus device.

Quoted 3 lines
Quoted 3 lines

In remaining cases, PCI device ID is used. So, also make PCI device ID build function hv_build_devid_type_pci() public.

Quoted 149 lines
Quoted 148 lines
---
 arch/x86/hyperv/irqdomain.c         |  9 +++++----
 arch/x86/include/asm/mshyperv.h     |  6 ++++++
 drivers/pci/controller/pci-hyperv.c | 24 ++++++++++++++++++++++++
 include/asm-generic/mshyperv.h      | 11 +++++++++++
 4 files changed, 46 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..2ef34001f8d3 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 */
 
+#if IS_ENABLED(CONFIG_HYPERV_IOMMU)
+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 /* IS_ENABLED(CONFIG_HYPERV_IOMMU) */
+
 #else /* CONFIG_HYPERV */
 static inline void hyperv_init(void) {}
 static inline void hyperv_setup_mmu_ops(void) {}
drivers/pci/controller/pci-hyperv.c +24 -0
index cfc8fa403dad..50d793ca8f31 100644
@@ -573,6 +573,7 @@ struct hv_pci_compl {
 };
 
 static void hv_pci_onchannelcallback(void *context);
+static bool hv_vmbus_pci_device(struct pci_bus *pbus);
 
 #ifdef CONFIG_X86
 #define DELIVERY_MODE		APIC_DELIVERY_MODE_FIXED
@@ -1005,6 +1006,24 @@ static struct irq_domain *hv_pci_get_root_domain(void)
 static void hv_arch_irq_unmask(struct irq_data *data) { }
 #endif /* CONFIG_ARM64 */
 
+u64 hv_pci_vmbus_device_id(struct pci_dev *pdev)
+{
+	struct hv_pcibus_device *hbus;
+	struct pci_bus *pbus = pdev->bus;
+
+	if (!hv_vmbus_pci_device(pbus))
+		return 0;
+
+	hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
+
+	return	(hbus->hdev->dev_instance.b[5] << 24) |
+		(hbus->hdev->dev_instance.b[4] << 16) |
+		(hbus->hdev->dev_instance.b[7] << 8) |
+		(hbus->hdev->dev_instance.b[6] & 0xf8) |
+		PCI_FUNC(pdev->devfn);
+}
+EXPORT_SYMBOL_GPL(hv_pci_vmbus_device_id);
+
 /**
  * hv_pci_generic_compl() - Invoked for a completion packet
  * @context:		Set up by the sender of the packet.
@@ -1403,6 +1422,11 @@ static struct pci_ops hv_pcifront_ops = {
 	.write = hv_pcifront_write_config,
 };
 
+static bool hv_vmbus_pci_device(struct pci_bus *pbus)
+{
+	return pbus->ops == &hv_pcifront_ops;
+}
+
 /*
  * Paravirtual backchannel
  *
include/asm-generic/mshyperv.h +11 -0
index e8cbc4e3f7ad..25ac7ca0fd8b 100644
@@ -204,6 +204,9 @@ extern u64 (*hv_read_reference_counter)(void);
 /* Sentinel value for an uninitialized entry in hv_vp_index array */
 #define VP_INVAL	U32_MAX
 
+/* Forward declarations */
+struct pci_dev;
+
 int __init hv_common_init(void);
 void __init hv_get_partition_id(void);
 void __init hv_common_free(void);
@@ -316,6 +319,14 @@ void hv_para_set_synic_register(unsigned int reg, u64 val);
 void hyperv_cleanup(void);
 bool hv_query_ext_cap(u64 cap_query);
 void hv_setup_dma_ops(struct device *dev, bool coherent);
+
+#if IS_ENABLED(CONFIG_PCI_HYPERV)
+u64 hv_pci_vmbus_device_id(struct pci_dev *pdev);
+#else
+static inline u64 hv_pci_vmbus_device_id(struct pci_dev *pdev)
+{ return 0; }
+#endif /* IS_ENABLED(CONFIG_PCI_HYPERV) */
+
 #else /* CONFIG_HYPERV */
 static inline void hv_identify_partition_type(void) {}
 static inline bool hv_is_hyperv_initialized(void) { return false; }
Signature · 1 line
2.51.2.vfs.0.1
#31 lore raw

Re: [PATCH V3 10/11] mshv: Populate mmio mappings for PCI passthru

In reply to Mukesh R
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [Critical] Unvalidated physical memory mapping and integer overflow allow mapping arbitrary host RAM.…

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [Critical] Unvalidated physical memory mapping and integer overflow allow mapping arbitrary host RAM.
- [Critical] Missing MMU notifier registration and TOCTOU race in MMIO fault handling.
- [High] Architecture page size mismatch corrupts physical address mappings on ARM64.
- [High] Hardcoded write permissions bypass host read-only restrictions for MMIO regions.
- [Medium] Uninitialized stack variable pfnmap_args.
--

Note: The format of this report is altered to provide a free-form summary of the findings, rather than using traditional inline diff quotes, to adhere to recitation restrictions.

commit f5a60b27c18f019e0a418a3f28dcff85e1128ed4
Author: Mukesh R <[email protected]>
Subject: mshv: Populate mmio mappings for PCI passthru

This commit adds support for mapping mmio memory to the guest upon receiving an unmapped guest physical address intercept. It looks up the physical frame number from the driver and asks the hypervisor to establish the map.

While reviewing this code, a few questions came up regarding the handling of the memory mappings.

When the memory region is created from the user memory structure, it appears only the start address is validated against the virtual memory area. The page count is taken directly from user input. Later, in the intercept handler, a signed 32-bit integer is used to loop over the 64-bit page count. If a user provides an excessively large size, could the loop counter overflow to a negative value and prematurely terminate the validation loop? Would this bypass the ram checks and allow mapping arbitrary host memory into the guest, while also potentially causing a lockup without a scheduling point?

Is there a race condition when fetching the physical frame number? The memory map read lock is dropped after resolving the address, and the hypercall is made without holding it. Since there does not seem to be a memory management unit interval notifier sequence check here, what happens if the host virtual memory area is unmapped or modified concurrently? Could the guest retain access to stale or reallocated host memory?

Does the physical frame number calculation work correctly on architectures with varying page sizes? On arm64, the kernel page size might be 64KB, meaning the value returned by the pfnmap functions is based on 64KB units. However, the handler subtracts a 4KB-based offset from it. Will this result in an incorrect base physical address being passed to the hypervisor?

Will this logic handle read-only device memory properly? The code unconditionally requests a write fault resolution and then hardcodes the writable flag for the hypervisor map call. If userspace maps a read-only memory region, will the fault fixup fail? If it does succeed, would the guest be granted write access to a read-only region?

Does the pfnmap arguments structure need zero-initialization? The structure is declared on the stack and only the virtual memory area and address fields are explicitly populated. Could uninitialized padding or other structure members contain stack garbage that might affect the pfnmap operations?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#33 lore raw

Re: [PATCH V3 11/11] mshv: Mark mem regions as non-movable upfront if device passthru

In reply to Mukesh R
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] Failing `copy_to_user` during device creation bypasses the `pt_regions_pinned` enforcement…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] Failing copy_to_user during device creation bypasses the pt_regions_pinned enforcement while leaving the device fully accessible.
- [High] Device creation does not validate or pin pre-existing memory regions, allowing an unsafe mix of movable regions and device passthrough.
- [High] Missing synchronization between device creation and region creation allows concurrent bypass of the pinning constraint. --

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

mshv: Mark mem regions as non-movable upfront if device passthru

This commit ensures that if a VM is started with a device attached, its memory regions are marked as non-movable upfront to allow the use of SLAT for IOMMU. It also adds a boot parameter to disable movable pages globally.

Quoted 4 lines
drivers/hv/mshv_root_main.c +0 -0
index a7864463961bc..ac71534733bd8 100644

[ ... ]

drivers/hv/mshv_root_main.c static void mshv_async_hvcall_handler(void *data, u64 *status) · line 1314

Quoted 9 lines
drivers/hv/mshv_root_main.c +5 -0
@@ -1303,6 +1307,12 @@ static void mshv_async_hvcall_handler(void *data, u64 *status)
 	*status = partition->async_hypercall_status;
 }
 
+static bool mshv_do_pt_regions_pinned(struct mshv_partition *pt)
+{
+	return pt->pt_regions_pinned || mshv_partition_encrypted(pt) ||
+	       hv_no_movbl_pgs;
+}

Is there a potential race condition here between device creation and region creation?

pt->pt_regions_pinned is written in mshv_partition_ioctl_create_device() without holding any locks. Concurrently, mshv_partition_create_region() reads this flag via mshv_do_pt_regions_pinned() without synchronization.

If a region is created while a concurrent device creation thread is about to set pt_regions_pinned to true, could the region incorrectly be instantiated as movable while a passthrough device is active?

[ ... ]

drivers/hv/mshv_root_main.c static long mshv_partition_ioctl_create_device(struct mshv_partition *partition, · line 1822

Quoted 6 lines
drivers/hv/mshv_root_main.c +2 -0
@@ -1808,6 +1818,9 @@ static long mshv_partition_ioctl_create_device(struct mshv_partition *partition,
 	if (copy_to_user(uarg, &devargk, sizeof(devargk)))
 		return -EFAULT;    /* cleanup in mshv_device_fop_release() */
 
+	/* For now, all regions must be pinned if there is device passthru. */
+	partition->pt_regions_pinned = true;

If the copy_to_user() fails, the function returns early and skips setting pt_regions_pinned to true. However, anon_inode_getfd() has already installed the file descriptor in the process's file descriptor table, meaning the device remains fully accessible. Can this allow userspace to bypass the pt_regions_pinned enforcement?

Additionally, when a device is attached, this sets pt_regions_pinned to true for future memory region creations. Does this handle the case where a partition already has existing memory regions?

If userspace creates movable regions first and then attaches a device, is there a risk that page migration could cause the device to DMA into incorrect physical memory?

Quoted 3 lines
drivers/hv/mshv_root_main.c +0 -0
 	return 0;
 
 undo_out:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#6 lore raw

Re: [PATCH V3 01/11] iommu/hyperv: Rename hyperv-iommu.c to hyperv-irq.c

In reply to Michael Kelley
Quoted 25 lines · Michael Kelley

From: Jacob Pan <[email protected]> Sent: Tuesday, May 12, 2026 4:46 PM

Quoted 19 lines

Hi Mukesh,

Quoted 6 lines · On Mon, 11 May 2026 19:02:49 -0700 Mukesh R <[email protected]> wrote:

This file actually implements irq remapping, so rename to more appropriate hyperv-irq.c. A new file to implement hyperv iommu will be introduced later. Also, it should not be tied to HYPERV_IOMMU, but to CONFIG_HYPERV and IRQ_REMAP. The file already has #ifdef CONFIG_IRQ_REMAP.

Quoted 6 lines
---
 MAINTAINERS                                    | 2 +-
 drivers/iommu/Makefile                         | 2 +-
 drivers/iommu/{hyperv-iommu.c => hyperv-irq.c} | 6 +++---
Quoted 4 lines

Given that we have multiple Hyper-V IOMMU-related files — this renamed hyperv-irq.c, the existing hyperv-iommu code, iommu-root (this series) and the recently posted guest pvIOMMU driver — should we create a drivers/iommu/hyperv/ directory to consolidate them?

Quoted 7 lines

Patch 1/4 in the guest pvIOMMU driver [1] that was recently posted by Yu Zhang does as you suggest.

Michael

[1] https://lore.kernel.org/linux-hyperv/[email protected]/

Maybe we can send a standalone patch and get it merged first (move it to drivers/iommu/hyperv/irq_remapping.c)?

The rename itself is a meaningful cleanup regardless of either series, and in the future, both Mukesh and I can can then build on top of it without conflicts. :)

B.R.
Yu

#26 lore raw

Re: [PATCH V3 09/11] x86/hyperv: Implement Hyper-V virtual IOMMU

In reply to Mukesh R
Quoted 19 lines · Mukesh R
drivers/iommu/hyperv-iommu-root.c new file +19 -0
+static struct iommu_domain *hv_iommu_domain_alloc_paging(struct device *dev)
+{
+	struct hv_domain *hvdom;
+	int rc;
+
+	if (hv_l1vh_partition() && !hv_curr_thread_is_vmm()) {
+		pr_err("Hyper-V: l1vh iommu does not support host devices\n");
+		return NULL;
+	}
+
+	hvdom = kzalloc(sizeof(struct hv_domain), GFP_KERNEL);
+	if (hvdom == NULL)
+		return NULL;
+
+	spin_lock_init(&hvdom->mappings_lock);
+	hvdom->mappings_tree = RB_ROOT_CACHED;
+
+	/* Called under iommu group mutex, so single threaded */
+	if (++unique_id == HV_DEVICE_DOMAIN_ID_S2_NULL)   /* ie, UINTMAX */

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

Quoted 10 lines
drivers/iommu/hyperv-iommu-root.c new file +10 -0
+		goto out_err;
+
+	hvdom->domid_num = unique_id;
+	hvdom->partid = hv_get_current_partid();
+	hvdom->iommu_dom.geometry = default_geometry;
+	hvdom->iommu_dom.pgsize_bitmap = HV_IOMMU_PGSIZES;
+
+	/* For guests, by default we do direct attaches, so no domain in hyp */
+	if (hv_dom_owner_is_vmm(hvdom) && !hv_no_attdev)
+		hvdom->attached_dom = true;

What are you thinking sending something like this?!?!?

The function is called *alloc domain PAGING*, it does not, and can not allocate weird "special" domains that are not PAGING domains. I just spent a long time removing all this kind of crazyness from drivers.

There is alot of other things I don't like in this patch, but this is too much.

You have to drop this "direct attach" idea from the first iteration, Linux can't do it without alot more work, you should start with the basic paging domain mode.

Jason

#27 lore raw

Re: [PATCH V3 09/11] x86/hyperv: Implement Hyper-V virtual IOMMU

Hi Jason,

Quoted 40 lines · On Fri, 15 May 2026 15:23:22 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 30 lines · Mukesh R

+static struct iommu_domain *hv_iommu_domain_alloc_paging(struct device *dev) +{

drivers/iommu/hyperv-iommu-root.c new file +5 -0
+	struct hv_domain *hvdom;
+	int rc;
+
+	if (hv_l1vh_partition() && !hv_curr_thread_is_vmm()) {
+		pr_err("Hyper-V: l1vh iommu does not support host

devices\n");

drivers/iommu/hyperv-iommu-root.c new file +12 -0
+		return NULL;
+	}
+
+	hvdom = kzalloc(sizeof(struct hv_domain), GFP_KERNEL);
+	if (hvdom == NULL)
+		return NULL;
+
+	spin_lock_init(&hvdom->mappings_lock);
+	hvdom->mappings_tree = RB_ROOT_CACHED;
+
+	/* Called under iommu group mutex, so single threaded */
+	if (++unique_id == HV_DEVICE_DOMAIN_ID_S2_NULL)   /* ie,

UINTMAX */

drivers/iommu/hyperv-iommu-root.c new file +8 -0
+		goto out_err;
+
+	hvdom->domid_num = unique_id;
+	hvdom->partid = hv_get_current_partid();
+	hvdom->iommu_dom.geometry = default_geometry;
+	hvdom->iommu_dom.pgsize_bitmap = HV_IOMMU_PGSIZES;
+
+	/* For guests, by default we do direct attaches, so no

domain in hyp */

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

Quoted 2 lines
drivers/iommu/hyperv-iommu-root.c new file +2 -0
+	if (hv_dom_owner_is_vmm(hvdom) && !hv_no_attdev)
+		hvdom->attached_dom = true;  

What are you thinking sending something like this?!?!?

The function is called *alloc domain PAGING*, it does not, and can not allocate weird "special" domains that are not PAGING domains. I just spent a long time removing all this kind of crazyness from drivers.

Quoted 7 lines

There is alot of other things I don't like in this patch, but this is too much.

You have to drop this "direct attach" idea from the first iteration, Linux can't do it without alot more work, you should start with the basic paging domain mode.

Just wondering what work is needed to support this "direct attach"? I felt this issue is due to trying to cram two distinct domain types (paging domain & direct attach) into the VFIO container model where only unmanaged paging domain is supported.

I am thinking if we were to switch to iommufd and let user(vmm) have direct control of HWPT, vmm will be able to selectively use a different domain type to handle direct attach. IMHO, it is essentially the same as attaching nest parent domain without nested domain immediately attached. The unprivileged guest may attach nested domain directly with Hyper-V if nested translation is needed.

I understand nest parent is still a paging domain today and it is expected to work with nested domain. So maybe we can make iommufd accept nest parent w/o paging? i.e. no map/unmap and do not call iopt_table_add_domain()? or maybe a new object?

From this driver POV, it will allocate a 2nd stage only domain with different domain ops (w/o map/unmap) for "direct attach" thus avoid this hack.

Thanks,

Jacob

#28 lore raw

Re: [PATCH V3 09/11] x86/hyperv: Implement Hyper-V virtual IOMMU

Quoted 4 lines · Jacob Pan

Just wondering what work is needed to support this "direct attach"? I felt this issue is due to trying to cram two distinct domain types (paging domain & direct attach) into the VFIO container model where only unmanaged paging domain is supported.

Xen has the same issue and you two need to come up with a uniform solution.

VFIO container can't support it, that's out.

You should be focusing on a iommufd flow that accepts some FD representing the VM (ie KVM FD) that can be converted by the driver into a HWPT representing that FD's S2 translation.

Quoted 3 lines

I am thinking if we were to switch to iommufd and let user(vmm) have direct control of HWPT, vmm will be able to selectively use a different domain type to handle direct attach.

Yes

Quoted 4 lines

IMHO, it is essentially the same as attaching nest parent domain without nested domain immediately attached. The unprivileged guest may attach nested domain directly with Hyper-V if nested translation is needed.

nest parent domain is really for supporting the viommu objects.. If you don't have that flow you don't need to worry about that nest parent stuff.

Quoted 3 lines

From this driver POV, it will allocate a 2nd stage only domain with different domain ops (w/o map/unmap) for "direct attach" thus avoid this hack.

Yes, from a driver POV you need a unique iommu_domain allocator that returns an iommu_domain without an ops.

It should probably work similarly to the viommu where the iommufd path can send in a driver-specific tagged struct that can describe these special domains.

But be mindful of the lifetime rules, whatever ID is used to describe the VM at the hypercall boundary has to be bound into a linux FD and become immutable. The driver has to hold that FD as long as the domain exists.

Jason

author
#29 lore raw

Re: [PATCH V3 09/11] x86/hyperv: Implement Hyper-V virtual IOMMU

In reply to Jason Gunthorpe
Quoted 37 lines · Jason Gunthorpe
Quoted 19 lines · Mukesh R
drivers/iommu/hyperv-iommu-root.c new file +19 -0
+static struct iommu_domain *hv_iommu_domain_alloc_paging(struct device *dev)
+{
+	struct hv_domain *hvdom;
+	int rc;
+
+	if (hv_l1vh_partition() && !hv_curr_thread_is_vmm()) {
+		pr_err("Hyper-V: l1vh iommu does not support host devices\n");
+		return NULL;
+	}
+
+	hvdom = kzalloc(sizeof(struct hv_domain), GFP_KERNEL);
+	if (hvdom == NULL)
+		return NULL;
+
+	spin_lock_init(&hvdom->mappings_lock);
+	hvdom->mappings_tree = RB_ROOT_CACHED;
+
+	/* Called under iommu group mutex, so single threaded */
+	if (++unique_id == HV_DEVICE_DOMAIN_ID_S2_NULL)   /* ie, UINTMAX */

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

Quoted 10 lines
drivers/iommu/hyperv-iommu-root.c new file +10 -0
+		goto out_err;
+
+	hvdom->domid_num = unique_id;
+	hvdom->partid = hv_get_current_partid();
+	hvdom->iommu_dom.geometry = default_geometry;
+	hvdom->iommu_dom.pgsize_bitmap = HV_IOMMU_PGSIZES;
+
+	/* For guests, by default we do direct attaches, so no domain in hyp */
+	if (hv_dom_owner_is_vmm(hvdom) && !hv_no_attdev)
+		hvdom->attached_dom = true;

What are you thinking sending something like this?!?!?

The function is called *alloc domain PAGING*, it does not, and can not allocate weird "special" domains that are not PAGING domains. I just spent a long time removing all this kind of crazyness from drivers.

Quoted 8 lines

There is alot of other things I don't like in this patch, but this is too much.

You have to drop this "direct attach" idea from the first iteration, Linux can't do it without alot more work, you should start with the basic paging domain mode.

Jason

Yeah, agree. There was some ambivalence whether this could be a stop gap solution until the iommufd based solution is fully designed.

I'll remove the "direct attach" stuff and resend with just basic paging domain mode. Thanks for the review.

Thanks,
-Mukesh