* [PATCH v0 13/15] x86/hyperv: Basic interrupt support for direct attached devices
From: Mukesh R @ 2026-01-20 6:42 UTC (permalink / raw)
To: linux-kernel, linux-hyperv, linux-arm-kernel, iommu, linux-pci,
linux-arch
Cc: kys, haiyangz, wei.liu, decui, longli, catalin.marinas, will,
tglx, mingo, bp, dave.hansen, hpa, joro, lpieralisi, kwilczynski,
mani, robh, bhelgaas, arnd, nunodasneves, mhklinux, romank
In-Reply-To: <20260120064230.3602565-1-mrathor@linux.microsoft.com>
From: Mukesh Rathor <mrathor@linux.microsoft.com>
As mentioned previously, a direct attached device must be referenced
via logical device id which is formed in the initial attach hypercall.
Interrupt mapping paths for direct attached devices are almost same,
except we must use logical device ids instead of the PCI device ids.
L1VH only supports direct attaches for passing thru devices to its guests,
and devices on L1VH are VMBus based. However, the interrupts are mapped
via the map interrupt hypercall and not the traditional method of VMBus
messages.
Partition id for the relevant hypercalls is tricky. This because a device
could be moving from root to guest and then back to the root. In case
of L1VH, it could be moving from system host to L1VH root to a guest,
then back to the L1VH root. So, it is carefully crafted by keeping
track of whether the call is on behalf of a VMM process, whether the
device is attached device (as opposed to mapped), and whether we are in
an L1VH root/parent. If VMM process, we assume it is on behalf of a
guest. Otherwise, the device is being attached or detached during boot
or shutdown of the privileged partition.
Lastly, a dummy cpu and vector is used to map interrupt for a direct
attached device. This because, once a device is marked for direct attach,
hypervisor will not let any interrupts be mapped to host. So it is mapped
to guest dummy cpu and dummy vector. This is then correctly mapped during
guest boot via the retarget paths.
Signed-off-by: Mukesh Rathor <mrathor@linux.microsoft.com>
---
arch/arm64/include/asm/mshyperv.h | 15 +++++
arch/x86/hyperv/irqdomain.c | 57 +++++++++++++-----
arch/x86/include/asm/mshyperv.h | 4 ++
drivers/pci/controller/pci-hyperv.c | 91 +++++++++++++++++++++++++----
4 files changed, 142 insertions(+), 25 deletions(-)
diff --git a/arch/arm64/include/asm/mshyperv.h b/arch/arm64/include/asm/mshyperv.h
index b721d3134ab6..27da480f94f6 100644
--- a/arch/arm64/include/asm/mshyperv.h
+++ b/arch/arm64/include/asm/mshyperv.h
@@ -53,6 +53,21 @@ static inline u64 hv_get_non_nested_msr(unsigned int reg)
return hv_get_msr(reg);
}
+struct irq_data;
+struct msi_msg;
+struct pci_dev;
+static inline void hv_irq_compose_msi_msg(struct irq_data *data,
+ struct msi_msg *msg) {};
+static inline int hv_unmap_msi_interrupt(struct pci_dev *pdev,
+ struct hv_interrupt_entry *hvirqe)
+{
+ return -EOPNOTSUPP;
+}
+static inline bool hv_pcidev_is_attached_dev(struct pci_dev *pdev)
+{
+ return false;
+}
+
/* SMCCC hypercall parameters */
#define HV_SMCCC_FUNC_NUMBER 1
#define HV_FUNC_ID ARM_SMCCC_CALL_VAL( \
diff --git a/arch/x86/hyperv/irqdomain.c b/arch/x86/hyperv/irqdomain.c
index 33017aa0caa4..e6eb457f791e 100644
--- a/arch/x86/hyperv/irqdomain.c
+++ b/arch/x86/hyperv/irqdomain.c
@@ -13,6 +13,16 @@
#include <linux/irqchip/irq-msi-lib.h>
#include <asm/mshyperv.h>
+/*
+ * For direct attached devices (which use logical device ids), hypervisor will
+ * not allow mappings to host. But VFIO needs to bind the interrupt at the very
+ * start before the guest cpu/vector is known. So we use dummy cpu and vector
+ * to bind in such case, and later when the guest starts, retarget will move it
+ * to correct guest cpu and vector.
+ */
+#define HV_DDA_DUMMY_CPU 0
+#define HV_DDA_DUMMY_VECTOR 32
+
static u64 hv_map_interrupt_hcall(u64 ptid, union hv_device_id hv_devid,
bool level, int cpu, int vector,
struct hv_interrupt_entry *ret_entry)
@@ -24,6 +34,11 @@ static u64 hv_map_interrupt_hcall(u64 ptid, union hv_device_id hv_devid,
u64 status;
int nr_bank, var_size;
+ if (hv_devid.device_type == HV_DEVICE_TYPE_LOGICAL) {
+ cpu = HV_DDA_DUMMY_CPU;
+ vector = HV_DDA_DUMMY_VECTOR;
+ }
+
local_irq_save(flags);
input = *this_cpu_ptr(hyperv_pcpu_input_arg);
@@ -95,7 +110,8 @@ static int hv_map_interrupt(u64 ptid, 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 *irq_entry)
+static int hv_unmap_interrupt(union hv_device_id hv_devid,
+ struct hv_interrupt_entry *irq_entry)
{
unsigned long flags;
struct hv_input_unmap_device_interrupt *input;
@@ -103,10 +119,14 @@ static int hv_unmap_interrupt(u64 id, struct hv_interrupt_entry *irq_entry)
local_irq_save(flags);
input = *this_cpu_ptr(hyperv_pcpu_input_arg);
-
memset(input, 0, sizeof(*input));
- input->partition_id = hv_current_partition_id;
- input->device_id = id;
+
+ if (hv_devid.device_type == HV_DEVICE_TYPE_LOGICAL)
+ input->partition_id = hv_iommu_get_curr_partid();
+ else
+ input->partition_id = hv_current_partition_id;
+
+ input->device_id = hv_devid.as_uint64;
input->interrupt_entry = *irq_entry;
status = hv_do_hypercall(HVCALL_UNMAP_DEVICE_INTERRUPT, input, NULL);
@@ -263,6 +283,7 @@ static u64 hv_build_irq_devid(struct pci_dev *pdev)
int hv_map_msi_interrupt(struct irq_data *data,
struct hv_interrupt_entry *out_entry)
{
+ u64 ptid;
struct irq_cfg *cfg = irqd_cfg(data);
struct hv_interrupt_entry dummy;
union hv_device_id hv_devid;
@@ -275,8 +296,17 @@ int hv_map_msi_interrupt(struct irq_data *data,
hv_devid.as_uint64 = hv_build_irq_devid(pdev);
cpu = cpumask_first(irq_data_get_effective_affinity_mask(data));
- return hv_map_interrupt(hv_current_partition_id, hv_devid, false, cpu,
- cfg->vector, out_entry ? out_entry : &dummy);
+ if (hv_devid.device_type == HV_DEVICE_TYPE_LOGICAL)
+ if (hv_pcidev_is_attached_dev(pdev))
+ ptid = hv_iommu_get_curr_partid();
+ else
+ /* Device actually on l1vh root, not passthru'd to vm */
+ ptid = hv_current_partition_id;
+ else
+ ptid = hv_current_partition_id;
+
+ return hv_map_interrupt(ptid, hv_devid, false, cpu, cfg->vector,
+ out_entry ? out_entry : &dummy);
}
EXPORT_SYMBOL_GPL(hv_map_msi_interrupt);
@@ -289,10 +319,7 @@ static void entry_to_msi_msg(struct hv_interrupt_entry *entry,
msg->data = entry->msi_entry.data.as_uint32;
}
-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)
+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);
@@ -341,16 +368,18 @@ 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);
}
+EXPORT_SYMBOL_GPL(hv_irq_compose_msi_msg);
-static int hv_unmap_msi_interrupt(struct pci_dev *pdev,
- struct hv_interrupt_entry *irq_entry)
+int hv_unmap_msi_interrupt(struct pci_dev *pdev,
+ struct hv_interrupt_entry *irq_entry)
{
union hv_device_id hv_devid;
hv_devid.as_uint64 = hv_build_irq_devid(pdev);
- return hv_unmap_interrupt(hv_devid.as_uint64, irq_entry);
+ return hv_unmap_interrupt(hv_devid, irq_entry);
}
+EXPORT_SYMBOL_GPL(hv_unmap_msi_interrupt);
/* 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)
@@ -486,7 +515,7 @@ int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry)
hv_devid.device_type = HV_DEVICE_TYPE_IOAPIC;
hv_devid.ioapic.ioapic_id = (u8)ioapic_id;
- return hv_unmap_interrupt(hv_devid.as_uint64, entry);
+ return hv_unmap_interrupt(hv_devid, entry);
}
EXPORT_SYMBOL_GPL(hv_unmap_ioapic_interrupt);
diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h
index e4ccdbbf1d12..b6facd3a0f5e 100644
--- a/arch/x86/include/asm/mshyperv.h
+++ b/arch/x86/include/asm/mshyperv.h
@@ -204,11 +204,15 @@ static inline u64 hv_iommu_get_curr_partid(void)
#endif /* CONFIG_HYPERV_IOMMU */
u64 hv_pci_vmbus_device_id(struct pci_dev *pdev);
+void hv_irq_compose_msi_msg(struct irq_data *data, struct msi_msg *msg);
+extern bool hv_no_attdev;
struct irq_domain *hv_create_pci_msi_domain(void);
int hv_map_msi_interrupt(struct irq_data *data,
struct hv_interrupt_entry *out_entry);
+int hv_unmap_msi_interrupt(struct pci_dev *dev,
+ struct hv_interrupt_entry *hvirqe);
int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
struct hv_interrupt_entry *entry);
int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c
index 40f0b06bb966..71d1599dc4a8 100644
--- a/drivers/pci/controller/pci-hyperv.c
+++ b/drivers/pci/controller/pci-hyperv.c
@@ -660,15 +660,17 @@ static void hv_irq_retarget_interrupt(struct irq_data *data)
params = *this_cpu_ptr(hyperv_pcpu_input_arg);
memset(params, 0, sizeof(*params));
- params->partition_id = HV_PARTITION_ID_SELF;
+
+ if (hv_pcidev_is_attached_dev(pdev))
+ params->partition_id = hv_iommu_get_curr_partid();
+ else
+ params->partition_id = HV_PARTITION_ID_SELF;
+
params->int_entry.source = HV_INTERRUPT_SOURCE_MSI;
- params->int_entry.msi_entry.address.as_uint32 = int_desc->address & 0xffffffff;
+ params->int_entry.msi_entry.address.as_uint32 =
+ int_desc->address & 0xffffffff;
params->int_entry.msi_entry.data.as_uint32 = int_desc->data;
- params->device_id = (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);
+ params->device_id = hv_pci_vmbus_device_id(pdev);
params->int_target.vector = hv_msi_get_int_vector(data);
if (hbus->protocol_version >= PCI_PROTOCOL_VERSION_1_2) {
@@ -1263,6 +1265,15 @@ static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
mb();
}
spin_unlock_irqrestore(&hbus->config_lock, flags);
+ /*
+ * Make sure PCI_INTERRUPT_PIN is hard-wired to 0 since it may
+ * be read using a 32bit read which is skipped by the above
+ * emulation.
+ */
+ if (PCI_INTERRUPT_PIN >= where &&
+ PCI_INTERRUPT_PIN <= (where + size)) {
+ *((char *)val + PCI_INTERRUPT_PIN - where) = 0;
+ }
} else {
dev_err(dev, "Attempt to read beyond a function's config space.\n");
}
@@ -1731,14 +1742,22 @@ static void hv_msi_free(struct irq_domain *domain, unsigned int irq)
if (!int_desc)
return;
- irq_data->chip_data = NULL;
hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
if (!hpdev) {
+ irq_data->chip_data = NULL;
kfree(int_desc);
return;
}
- hv_int_desc_free(hpdev, int_desc);
+ if (hv_pcidev_is_attached_dev(pdev)) {
+ hv_unmap_msi_interrupt(pdev, irq_data->chip_data);
+ kfree(irq_data->chip_data);
+ irq_data->chip_data = NULL;
+ } else {
+ irq_data->chip_data = NULL;
+ hv_int_desc_free(hpdev, int_desc);
+ }
+
put_pcichild(hpdev);
}
@@ -2139,6 +2158,56 @@ static void hv_vmbus_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
msg->data = 0;
}
+/* Compose an msi message for a directly attached device */
+static void hv_dda_compose_msi_msg(struct irq_data *irq_data,
+ struct msi_desc *msi_desc,
+ struct msi_msg *msg)
+{
+ bool multi_msi;
+ struct hv_pcibus_device *hbus;
+ struct hv_pci_dev *hpdev;
+ struct pci_dev *pdev = msi_desc_to_pci_dev(msi_desc);
+
+ multi_msi = !msi_desc->pci.msi_attrib.is_msix &&
+ msi_desc->nvec_used > 1;
+
+ if (multi_msi) {
+ dev_err(&hbus->hdev->device,
+ "Passthru direct attach does not support multi msi\n");
+ goto outerr;
+ }
+
+ hbus = container_of(pdev->bus->sysdata, struct hv_pcibus_device,
+ sysdata);
+
+ hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
+ if (!hpdev)
+ goto outerr;
+
+ /* will unmap if needed and also update irq_data->chip_data */
+ hv_irq_compose_msi_msg(irq_data, msg);
+
+ put_pcichild(hpdev);
+ return;
+
+outerr:
+ memset(msg, 0, sizeof(*msg));
+}
+
+static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
+{
+ struct pci_dev *pdev;
+ struct msi_desc *msi_desc;
+
+ msi_desc = irq_data_get_msi_desc(data);
+ pdev = msi_desc_to_pci_dev(msi_desc);
+
+ if (hv_pcidev_is_attached_dev(pdev))
+ hv_dda_compose_msi_msg(data, msi_desc, msg);
+ else
+ hv_vmbus_compose_msi_msg(data, msg);
+}
+
static bool hv_pcie_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
struct irq_domain *real_parent, struct msi_domain_info *info)
{
@@ -2177,7 +2246,7 @@ static const struct msi_parent_ops hv_pcie_msi_parent_ops = {
/* HW Interrupt Chip Descriptor */
static struct irq_chip hv_msi_irq_chip = {
.name = "Hyper-V PCIe MSI",
- .irq_compose_msi_msg = hv_vmbus_compose_msi_msg,
+ .irq_compose_msi_msg = hv_compose_msi_msg,
.irq_set_affinity = irq_chip_set_affinity_parent,
.irq_ack = irq_chip_ack_parent,
.irq_eoi = irq_chip_eoi_parent,
@@ -4096,7 +4165,7 @@ static int hv_pci_restore_msi_msg(struct pci_dev *pdev, void *arg)
irq_data = irq_get_irq_data(entry->irq);
if (WARN_ON_ONCE(!irq_data))
return -EINVAL;
- hv_vmbus_compose_msi_msg(irq_data, &entry->msg);
+ hv_compose_msi_msg(irq_data, &entry->msg);
}
return 0;
}
--
2.51.2.vfs.0.1
^ permalink raw reply related
* [PATCH v0 12/15] x86/hyperv: Implement hyperv virtual iommu
From: Mukesh R @ 2026-01-20 6:42 UTC (permalink / raw)
To: linux-kernel, linux-hyperv, linux-arm-kernel, iommu, linux-pci,
linux-arch
Cc: kys, haiyangz, wei.liu, decui, longli, catalin.marinas, will,
tglx, mingo, bp, dave.hansen, hpa, joro, lpieralisi, kwilczynski,
mani, robh, bhelgaas, arnd, nunodasneves, mhklinux, romank
In-Reply-To: <20260120064230.3602565-1-mrathor@linux.microsoft.com>
From: Mukesh Rathor <mrathor@linux.microsoft.com>
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 root or L1VH
parent. This also implements direct attach mechanism 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: root must create a device domain in the hypervisor,
and do map/unmap hypercalls for mapping and unmapping guest RAM.
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 host need not do map/unmap
device memory hypercalls. As such, direct attach passthru setup
during guest boot is extremely fast. A direct attached device
must be referenced via logical device id and not via the PCI
device id.
At present, L1VH root/parent only supports direct attaches. Also direct
attach is default in non-L1VH cases because there are some significant
performance issues with device domain implementation currently for guests
with higher RAM (say more than 8GB), and that unfortunately cannot be
addressed in the short term.
Signed-off-by: Mukesh Rathor <mrathor@linux.microsoft.com>
---
MAINTAINERS | 1 +
arch/x86/include/asm/mshyperv.h | 7 +-
arch/x86/kernel/pci-dma.c | 2 +
drivers/iommu/Makefile | 2 +-
drivers/iommu/hyperv-iommu.c | 876 ++++++++++++++++++++++++++++++++
include/linux/hyperv.h | 6 +
6 files changed, 890 insertions(+), 4 deletions(-)
create mode 100644 drivers/iommu/hyperv-iommu.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 381a0e086382..63160cee942c 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -11741,6 +11741,7 @@ F: drivers/hid/hid-hyperv.c
F: drivers/hv/
F: drivers/infiniband/hw/mana/
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/
diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h
index 97477c5a8487..e4ccdbbf1d12 100644
--- a/arch/x86/include/asm/mshyperv.h
+++ b/arch/x86/include/asm/mshyperv.h
@@ -189,16 +189,17 @@ static inline void hv_apic_init(void) {}
#endif
#if IS_ENABLED(CONFIG_HYPERV_IOMMU)
-static inline bool hv_pcidev_is_attached_dev(struct pci_dev *pdev)
-{ return false; } /* temporary */
+bool hv_pcidev_is_attached_dev(struct pci_dev *pdev);
u64 hv_build_devid_oftype(struct pci_dev *pdev, enum hv_device_type type);
+u64 hv_iommu_get_curr_partid(void);
#else /* CONFIG_HYPERV_IOMMU */
static inline bool hv_pcidev_is_attached_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_iommu_get_curr_partid(void)
+{ return HV_PARTITION_ID_INVALID; }
#endif /* CONFIG_HYPERV_IOMMU */
diff --git a/arch/x86/kernel/pci-dma.c b/arch/x86/kernel/pci-dma.c
index 6267363e0189..cfeee6505e17 100644
--- a/arch/x86/kernel/pci-dma.c
+++ b/arch/x86/kernel/pci-dma.c
@@ -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);
}
diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile
index 598c39558e7d..cc9774864b00 100644
--- a/drivers/iommu/Makefile
+++ b/drivers/iommu/Makefile
@@ -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-irq.o
+obj-$(CONFIG_HYPERV_IOMMU) += hyperv-irq.o hyperv-iommu.o
obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o
obj-$(CONFIG_IOMMU_SVA) += iommu-sva.o
obj-$(CONFIG_IOMMU_IOPF) += io-pgfault.o
diff --git a/drivers/iommu/hyperv-iommu.c b/drivers/iommu/hyperv-iommu.c
new file mode 100644
index 000000000000..548483fec6b1
--- /dev/null
+++ b/drivers/iommu/hyperv-iommu.c
@@ -0,0 +1,876 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Hyper-V root vIOMMU driver.
+ * Copyright (C) 2026, Microsoft, Inc.
+ */
+
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/dmar.h>
+#include <linux/dma-map-ops.h>
+#include <linux/interval_tree.h>
+#include <linux/hyperv.h>
+#include "dma-iommu.h"
+#include <asm/iommu.h>
+#include <asm/mshyperv.h>
+
+/* We will not claim these PCI devices, eg hypervisor needs it for debugger */
+static char *pci_devs_to_skip;
+static int __init hv_iommu_setup_skip(char *str)
+{
+ pci_devs_to_skip = str;
+
+ return 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;
+}
+__setup("hv_no_attdev", setup_hv_no_attdev);
+
+/* Iommu device that we export to the world. HyperV supports max of one */
+static struct iommu_device hv_virt_iommu;
+
+struct hv_domain {
+ struct iommu_domain iommu_dom;
+ u32 domid_num; /* as opposed to domain_id.type */
+ u32 num_attchd; /* number of currently attached devices */
+ bool attached_dom; /* is this direct attached dom? */
+ spinlock_t mappings_lock; /* protects mappings_tree */
+ struct rb_root_cached mappings_tree; /* iova to pa lookup tree */
+};
+
+#define to_hv_domain(d) container_of(d, struct hv_domain, iommu_dom)
+
+struct hv_iommu_mapping {
+ phys_addr_t paddr;
+ struct interval_tree_node iova;
+ u32 flags;
+};
+
+/*
+ * By default, during boot the hypervisor creates one Stage 2 (S2) default
+ * domain. Stage 2 means that the page table is controlled by the hypervisor.
+ * S2 default: access to entire root partition memory. This for us easily
+ * maps to IOMMU_DOMAIN_IDENTITY in the iommu subsystem, and
+ * is called HV_DEVICE_DOMAIN_ID_S2_DEFAULT in the hypervisor.
+ *
+ * Device Management:
+ * There are two ways to manage device attaches to domains:
+ * 1. Domain Attach: A device domain is created in the hypervisor, the
+ * device is attached to this domain, and then memory
+ * ranges are mapped in the map callbacks.
+ * 2. Direct Attach: No need to create a domain in the hypervisor for direct
+ * attached devices. A hypercall is made to tell the
+ * hypervisor to attach the device to a guest. There is
+ * no need for explicit memory mappings because the
+ * hypervisor will just use the guest HW page table.
+ *
+ * Since a direct attach is much faster, it is the default. This can be
+ * changed via hv_no_attdev.
+ *
+ * L1VH: hypervisor only supports direct attach.
+ */
+
+/*
+ * Create dummy domain to correspond to hypervisor prebuilt default identity
+ * domain (dummy because we do not make hypercall to create them).
+ */
+static struct hv_domain hv_def_identity_dom;
+
+static bool hv_special_domain(struct hv_domain *hvdom)
+{
+ return hvdom == &hv_def_identity_dom;
+}
+
+struct iommu_domain_geometry default_geometry = (struct iommu_domain_geometry) {
+ .aperture_start = 0,
+ .aperture_end = -1UL,
+ .force_aperture = true,
+};
+
+/*
+ * Since the relevant hypercalls can only fit less than 512 PFNs in the pfn
+ * array, report 1M max.
+ */
+#define HV_IOMMU_PGSIZES (SZ_4K | SZ_1M)
+
+static u32 unique_id; /* unique numeric id of a new domain */
+
+static void hv_iommu_detach_dev(struct iommu_domain *immdom,
+ struct device *dev);
+static size_t hv_iommu_unmap_pages(struct iommu_domain *immdom, ulong iova,
+ size_t pgsize, size_t pgcount,
+ struct iommu_iotlb_gather *gather);
+
+/*
+ * If the current thread is a VMM thread, return the partition id of the VM it
+ * is managing, else return HV_PARTITION_ID_INVALID.
+ */
+u64 hv_iommu_get_curr_partid(void)
+{
+ u64 (*fn)(pid_t pid);
+ u64 partid;
+
+ fn = symbol_get(mshv_pid_to_partid);
+ if (!fn)
+ return HV_PARTITION_ID_INVALID;
+
+ partid = fn(current->tgid);
+ symbol_put(mshv_pid_to_partid);
+
+ return partid;
+}
+
+/* If this is a VMM thread, then this domain is for a guest VM */
+static bool hv_curr_thread_is_vmm(void)
+{
+ return hv_iommu_get_curr_partid() != HV_PARTITION_ID_INVALID;
+}
+
+static bool hv_iommu_capable(struct device *dev, enum iommu_cap cap)
+{
+ switch (cap) {
+ case IOMMU_CAP_CACHE_COHERENCY:
+ return true;
+ default:
+ return false;
+ }
+ return false;
+}
+
+/*
+ * Check if given pci device is a direct attached device. Caller must have
+ * verified pdev is a valid pci device.
+ */
+bool hv_pcidev_is_attached_dev(struct pci_dev *pdev)
+{
+ struct iommu_domain *iommu_domain;
+ struct hv_domain *hvdom;
+ struct device *dev = &pdev->dev;
+
+ iommu_domain = iommu_get_domain_for_dev(dev);
+ if (iommu_domain) {
+ hvdom = to_hv_domain(iommu_domain);
+ return hvdom->attached_dom;
+ }
+
+ return false;
+}
+EXPORT_SYMBOL_GPL(hv_pcidev_is_attached_dev);
+
+/* Create a new device domain in the hypervisor */
+static int hv_iommu_create_hyp_devdom(struct hv_domain *hvdom)
+{
+ u64 status;
+ unsigned long flags;
+ struct hv_input_device_domain *ddp;
+ struct hv_input_create_device_domain *input;
+
+ local_irq_save(flags);
+ input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+ memset(input, 0, sizeof(*input));
+
+ ddp = &input->device_domain;
+ ddp->partition_id = HV_PARTITION_ID_SELF;
+ ddp->domain_id.type = HV_DEVICE_DOMAIN_TYPE_S2;
+ ddp->domain_id.id = hvdom->domid_num;
+
+ input->create_device_domain_flags.forward_progress_required = 1;
+ input->create_device_domain_flags.inherit_owning_vtl = 0;
+
+ status = hv_do_hypercall(HVCALL_CREATE_DEVICE_DOMAIN, input, NULL);
+
+ local_irq_restore(flags);
+
+ if (!hv_result_success(status))
+ hv_status_err(status, "\n");
+
+ return hv_result_to_errno(status);
+}
+
+/* During boot, all devices are attached to this */
+static struct iommu_domain *hv_iommu_domain_alloc_identity(struct device *dev)
+{
+ return &hv_def_identity_dom.iommu_dom;
+}
+
+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() && !hv_no_attdev) {
+ 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)
+ goto out;
+
+ spin_lock_init(&hvdom->mappings_lock);
+ hvdom->mappings_tree = RB_ROOT_CACHED;
+
+ if (++unique_id == HV_DEVICE_DOMAIN_ID_S2_DEFAULT) /* ie, 0 */
+ goto out_free;
+
+ hvdom->domid_num = unique_id;
+ 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_curr_thread_is_vmm() && !hv_no_attdev)
+ hvdom->attached_dom = true;
+ else {
+ rc = hv_iommu_create_hyp_devdom(hvdom);
+ if (rc)
+ goto out_free_id;
+ }
+
+ return &hvdom->iommu_dom;
+
+out_free_id:
+ unique_id--;
+out_free:
+ kfree(hvdom);
+out:
+ return NULL;
+}
+
+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 (hvdom->num_attchd) {
+ pr_err("Hyper-V: can't free busy iommu domain (%p)\n", immdom);
+ return;
+ }
+
+ if (!hv_curr_thread_is_vmm() || hv_no_attdev) {
+ struct hv_input_device_domain *ddp;
+
+ local_irq_save(flags);
+ input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+ ddp = &input->device_domain;
+ memset(input, 0, sizeof(*input));
+
+ ddp->partition_id = HV_PARTITION_ID_SELF;
+ ddp->domain_id.type = HV_DEVICE_DOMAIN_TYPE_S2;
+ ddp->domain_id.id = hvdom->domid_num;
+
+ status = hv_do_hypercall(HVCALL_DELETE_DEVICE_DOMAIN, input,
+ NULL);
+ local_irq_restore(flags);
+
+ if (!hv_result_success(status))
+ hv_status_err(status, "\n");
+ }
+
+ kfree(hvdom);
+}
+
+/* Attach a device to a domain previously created in the hypervisor */
+static int hv_iommu_att_dev2dom(struct hv_domain *hvdom, struct pci_dev *pdev)
+{
+ unsigned long flags;
+ u64 status;
+ enum hv_device_type dev_type;
+ struct hv_input_attach_device_domain *input;
+
+ 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;
+
+ /* NB: Upon guest shutdown, device is re-attached to the default domain
+ * without explicit detach.
+ */
+ if (hv_l1vh_partition())
+ dev_type = HV_DEVICE_TYPE_LOGICAL;
+ else
+ dev_type = HV_DEVICE_TYPE_PCI;
+
+ input->device_id.as_uint64 = hv_build_devid_oftype(pdev, dev_type);
+
+ status = hv_do_hypercall(HVCALL_ATTACH_DEVICE_DOMAIN, input, NULL);
+ local_irq_restore(flags);
+
+ if (!hv_result_success(status))
+ hv_status_err(status, "\n");
+
+ return hv_result_to_errno(status);
+}
+
+/* Caller must have validated that dev is a valid pci dev */
+static int hv_iommu_direct_attach_device(struct pci_dev *pdev)
+{
+ struct hv_input_attach_device *input;
+ u64 status;
+ int rc;
+ unsigned long flags;
+ union hv_device_id host_devid;
+ enum hv_device_type dev_type;
+ u64 ptid = hv_iommu_get_curr_partid();
+
+ if (ptid == HV_PARTITION_ID_INVALID) {
+ pr_err("Hyper-V: Invalid partition id in direct attach\n");
+ return -EINVAL;
+ }
+
+ if (hv_l1vh_partition())
+ dev_type = HV_DEVICE_TYPE_LOGICAL;
+ else
+ dev_type = HV_DEVICE_TYPE_PCI;
+
+ host_devid.as_uint64 = hv_build_devid_oftype(pdev, dev_type);
+
+ do {
+ local_irq_save(flags);
+ input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+ memset(input, 0, sizeof(*input));
+ input->partition_id = ptid;
+ input->device_id = host_devid;
+
+ /* Hypervisor associates logical_id with this device, and in
+ * some hypercalls like retarget interrupts, logical_id must be
+ * used instead of the BDF. It is a required parameter.
+ */
+ input->attdev_flags.logical_id = 1;
+ input->logical_devid =
+ hv_build_devid_oftype(pdev, HV_DEVICE_TYPE_LOGICAL);
+
+ status = hv_do_hypercall(HVCALL_ATTACH_DEVICE, input, NULL);
+ local_irq_restore(flags);
+
+ if (hv_result(status) == HV_STATUS_INSUFFICIENT_MEMORY) {
+ rc = hv_call_deposit_pages(NUMA_NO_NODE, ptid, 1);
+ if (rc)
+ break;
+ }
+ } while (hv_result(status) == HV_STATUS_INSUFFICIENT_MEMORY);
+
+ if (!hv_result_success(status))
+ hv_status_err(status, "\n");
+
+ return hv_result_to_errno(status);
+}
+
+/* This to attach a device to both host app (like DPDK) and a guest VM */
+static int hv_iommu_attach_dev(struct iommu_domain *immdom, struct device *dev,
+ struct iommu_domain *old)
+{
+ struct pci_dev *pdev;
+ int rc;
+ struct hv_domain *hvdom_new = to_hv_domain(immdom);
+ struct hv_domain *hvdom_prev = dev_iommu_priv_get(dev);
+
+ /* Only allow PCI devices for now */
+ if (!dev_is_pci(dev))
+ return -EINVAL;
+
+ pdev = to_pci_dev(dev);
+
+ /* l1vh does not support host device (eg DPDK) passthru */
+ if (hv_l1vh_partition() && !hv_special_domain(hvdom_new) &&
+ !hvdom_new->attached_dom)
+ return -EINVAL;
+
+ /*
+ * VFIO does not do explicit detach calls, hence check first if we need
+ * to detach first. Also, in case of guest shutdown, it's the VMM
+ * thread that attaches it back to the hv_def_identity_dom, and
+ * hvdom_prev will not be null then. It is null during boot.
+ */
+ if (hvdom_prev)
+ if (!hv_l1vh_partition() || !hv_special_domain(hvdom_prev))
+ hv_iommu_detach_dev(&hvdom_prev->iommu_dom, dev);
+
+ if (hv_l1vh_partition() && hv_special_domain(hvdom_new)) {
+ dev_iommu_priv_set(dev, hvdom_new); /* sets "private" field */
+ return 0;
+ }
+
+ if (hvdom_new->attached_dom)
+ rc = hv_iommu_direct_attach_device(pdev);
+ else
+ rc = hv_iommu_att_dev2dom(hvdom_new, pdev);
+
+ if (rc && hvdom_prev) {
+ int rc1;
+
+ if (hvdom_prev->attached_dom)
+ rc1 = hv_iommu_direct_attach_device(pdev);
+ else
+ rc1 = hv_iommu_att_dev2dom(hvdom_prev, pdev);
+
+ if (rc1)
+ pr_err("Hyper-V: iommu could not restore orig device state.. dev:%s\n",
+ dev_name(dev));
+ }
+
+ if (rc == 0) {
+ dev_iommu_priv_set(dev, hvdom_new); /* sets "private" field */
+ hvdom_new->num_attchd++;
+ }
+
+ return rc;
+}
+
+static void hv_iommu_det_dev_from_guest(struct hv_domain *hvdom,
+ struct pci_dev *pdev)
+{
+ struct hv_input_detach_device *input;
+ u64 status, log_devid;
+ unsigned long flags;
+
+ log_devid = hv_build_devid_oftype(pdev, HV_DEVICE_TYPE_LOGICAL);
+
+ local_irq_save(flags);
+ input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+ memset(input, 0, sizeof(*input));
+
+ input->partition_id = hv_iommu_get_curr_partid();
+ input->logical_devid = log_devid;
+ status = hv_do_hypercall(HVCALL_DETACH_DEVICE, input, NULL);
+ local_irq_restore(flags);
+
+ if (!hv_result_success(status))
+ hv_status_err(status, "\n");
+}
+
+static void hv_iommu_det_dev_from_dom(struct hv_domain *hvdom,
+ struct pci_dev *pdev)
+{
+ u64 status, devid;
+ unsigned long flags;
+ struct hv_input_detach_device_domain *input;
+
+ devid = hv_build_devid_oftype(pdev, HV_DEVICE_TYPE_PCI);
+
+ local_irq_save(flags);
+ input = *this_cpu_ptr(hyperv_pcpu_input_arg);
+ memset(input, 0, sizeof(*input));
+
+ input->partition_id = HV_PARTITION_ID_SELF;
+ input->device_id.as_uint64 = devid;
+ status = hv_do_hypercall(HVCALL_DETACH_DEVICE_DOMAIN, input, NULL);
+ local_irq_restore(flags);
+
+ if (!hv_result_success(status))
+ hv_status_err(status, "\n");
+}
+
+static void hv_iommu_detach_dev(struct iommu_domain *immdom, struct device *dev)
+{
+ struct pci_dev *pdev;
+ struct hv_domain *hvdom = to_hv_domain(immdom);
+
+ /* See the attach function, only PCI devices for now */
+ if (!dev_is_pci(dev))
+ return;
+
+ if (hvdom->num_attchd == 0)
+ pr_warn("Hyper-V: num_attchd is zero (%s)\n", dev_name(dev));
+
+ pdev = to_pci_dev(dev);
+
+ if (hvdom->attached_dom) {
+ hv_iommu_det_dev_from_guest(hvdom, pdev);
+
+ /* Do not reset attached_dom, hv_iommu_unmap_pages happens
+ * next.
+ */
+ } else {
+ hv_iommu_det_dev_from_dom(hvdom, pdev);
+ }
+
+ hvdom->num_attchd--;
+}
+
+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);
+ if (!mapping)
+ return -ENOMEM;
+
+ mapping->paddr = paddr;
+ mapping->iova.start = iova;
+ mapping->iova.last = iova + size - 1;
+ mapping->flags = flags;
+
+ spin_lock_irqsave(&hvdom->mappings_lock, irqflags);
+ interval_tree_insert(&mapping->iova, &hvdom->mappings_tree);
+ spin_unlock_irqrestore(&hvdom->mappings_lock, irqflags);
+
+ return 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;
+
+ 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;
+
+ unmapped += mapping->iova.last - mapping->iova.start + 1;
+
+ interval_tree_remove(node, &hvdom->mappings_tree);
+ kfree(mapping);
+ }
+ spin_unlock_irqrestore(&hvdom->mappings_lock, flags);
+
+ return unmapped;
+}
+
+/* Return: must return exact status from the hypercall without changes */
+static u64 hv_iommu_map_pgs(struct hv_domain *hvdom,
+ unsigned long iova, phys_addr_t paddr,
+ unsigned long npages, u32 map_flags)
+{
+ u64 status;
+ int i;
+ struct hv_input_map_device_gpa_pages *input;
+ unsigned long flags, pfn = paddr >> 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->map_flags = map_flags;
+ input->target_device_va_base = iova;
+
+ pfn = paddr >> HV_HYP_PAGE_SHIFT;
+ for (i = 0; i < npages; i++, pfn++)
+ input->gpa_page_list[i] = pfn;
+
+ status = hv_do_rep_hypercall(HVCALL_MAP_DEVICE_GPA_PAGES, npages, 0,
+ input, NULL);
+
+ local_irq_restore(flags);
+ return status;
+}
+
+/*
+ * The core VFIO code loops over memory ranges calling this function with
+ * the largest size from HV_IOMMU_PGSIZES. cond_resched() is in vfio_iommu_map.
+ */
+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 */
+ 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;
+ }
+
+ npages = size >> HV_HYP_PAGE_SHIFT;
+ while (done < npages) {
+ ulong completed, remain = npages - done;
+
+ status = hv_iommu_map_pgs(hvdom, iova, paddr, remain,
+ map_flags);
+
+ completed = hv_repcomp(status);
+ done = done + completed;
+ iova = iova + (completed << HV_HYP_PAGE_SHIFT);
+ paddr = paddr + (completed << HV_HYP_PAGE_SHIFT);
+
+ if (hv_result(status) == HV_STATUS_INSUFFICIENT_MEMORY) {
+ ret = hv_call_deposit_pages(NUMA_NO_NODE,
+ hv_current_partition_id,
+ 256);
+ if (ret)
+ break;
+ }
+ if (!hv_result_success(status))
+ break;
+ }
+
+ if (!hv_result_success(status)) {
+ size_t done_size = done << HV_HYP_PAGE_SHIFT;
+
+ hv_status_err(status, "pgs:%lx/%lx iova:%lx\n",
+ done, npages, iova);
+ /*
+ * lookup tree has all mappings [0 - size-1]. Below unmap will
+ * only remove from [0 - done], we need to remove second chunk
+ * [done+1 - size-1].
+ */
+ hv_iommu_del_tree_mappings(hvdom, iova, size - done_size);
+ hv_iommu_unmap_pages(immdom, iova - done_size, pgsize,
+ done, NULL);
+ if (mapped)
+ *mapped = 0;
+ } else
+ if (mapped)
+ *mapped = size;
+
+ return hv_result_to_errno(status);
+}
+
+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;
+
+ unmapped = hv_iommu_del_tree_mappings(hvdom, iova, size);
+ if (unmapped < size)
+ pr_err("%s: could not delete all mappings (%lx:%lx/%lx)\n",
+ __func__, iova, unmapped, size);
+
+ if (hvdom->attached_dom)
+ return size;
+
+ 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);
+ local_irq_restore(flags);
+
+ if (!hv_result_success(status))
+ hv_status_err(status, "\n");
+
+ return unmapped;
+}
+
+static phys_addr_t hv_iommu_iova_to_phys(struct iommu_domain *immdom,
+ dma_addr_t iova)
+{
+ u64 paddr = 0;
+ unsigned long flags;
+ struct hv_iommu_mapping *mapping;
+ struct interval_tree_node *node;
+ struct hv_domain *hvdom = to_hv_domain(immdom);
+
+ spin_lock_irqsave(&hvdom->mappings_lock, flags);
+ node = interval_tree_iter_first(&hvdom->mappings_tree, iova, iova);
+ if (node) {
+ mapping = container_of(node, struct hv_iommu_mapping, iova);
+ paddr = mapping->paddr + (iova - mapping->iova.start);
+ }
+ spin_unlock_irqrestore(&hvdom->mappings_lock, flags);
+
+ return paddr;
+}
+
+/*
+ * Currently, hypervisor does not provide list of devices it is using
+ * dynamically. So use this to allow users to manually specify devices that
+ * should be skipped. (eg. hypervisor debugger using some network device).
+ */
+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;
+ 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;
+ if (parsed <= 0)
+ break;
+
+ if (pci_domain_nr(pdev->bus) == segment &&
+ pdev->bus->number == bus &&
+ PCI_SLOT(pdev->devfn) == slot &&
+ PCI_FUNC(pdev->devfn) == func) {
+
+ dev_info(dev, "skipped by Hyper-V IOMMU\n");
+ return ERR_PTR(-ENODEV);
+ }
+ pos += parsed;
+
+ } while (pci_devs_to_skip[pos]);
+ }
+
+ /* Device will be explicitly attached to the default domain, so no need
+ * to do dev_iommu_priv_set() here.
+ */
+
+ return &hv_virt_iommu;
+}
+
+static void hv_iommu_probe_finalize(struct device *dev)
+{
+ struct iommu_domain *immdom = iommu_get_domain_for_dev(dev);
+
+ if (immdom && immdom->type == IOMMU_DOMAIN_DMA)
+ iommu_setup_dma_ops(dev);
+ else
+ set_dma_ops(dev, NULL);
+}
+
+static void hv_iommu_release_device(struct device *dev)
+{
+ struct hv_domain *hvdom = dev_iommu_priv_get(dev);
+
+ /* Need to detach device from device domain if necessary. */
+ if (hvdom)
+ hv_iommu_detach_dev(&hvdom->iommu_dom, dev);
+
+ dev_iommu_priv_set(dev, NULL);
+ set_dma_ops(dev, NULL);
+}
+
+static struct iommu_group *hv_iommu_device_group(struct device *dev)
+{
+ if (dev_is_pci(dev))
+ return pci_device_group(dev);
+ else
+ return generic_device_group(dev);
+}
+
+static int hv_iommu_def_domain_type(struct device *dev)
+{
+ /* The hypervisor always creates this by default during boot */
+ return IOMMU_DOMAIN_IDENTITY;
+}
+
+static struct iommu_ops hv_iommu_ops = {
+ .capable = hv_iommu_capable,
+ .domain_alloc_identity = hv_iommu_domain_alloc_identity,
+ .domain_alloc_paging = hv_iommu_domain_alloc_paging,
+ .probe_device = hv_iommu_probe_device,
+ .probe_finalize = hv_iommu_probe_finalize,
+ .release_device = hv_iommu_release_device,
+ .def_domain_type = hv_iommu_def_domain_type,
+ .device_group = hv_iommu_device_group,
+ .default_domain_ops = &(const struct iommu_domain_ops) {
+ .attach_dev = hv_iommu_attach_dev,
+ .map_pages = hv_iommu_map_pages,
+ .unmap_pages = hv_iommu_unmap_pages,
+ .iova_to_phys = hv_iommu_iova_to_phys,
+ .free = hv_iommu_domain_free,
+ },
+ .owner = THIS_MODULE,
+};
+
+static void __init hv_initialize_special_domains(void)
+{
+ hv_def_identity_dom.iommu_dom.geometry = default_geometry;
+ hv_def_identity_dom.domid_num = HV_DEVICE_DOMAIN_ID_S2_DEFAULT; /* 0 */
+}
+
+static int __init hv_iommu_init(void)
+{
+ int ret;
+ struct iommu_device *iommup = &hv_virt_iommu;
+
+ if (!hv_is_hyperv_initialized())
+ return -ENODEV;
+
+ ret = iommu_device_sysfs_add(iommup, NULL, NULL, "%s", "hyperv-iommu");
+ if (ret) {
+ pr_err("Hyper-V: iommu_device_sysfs_add failed: %d\n", ret);
+ return ret;
+ }
+
+ /* This must come before iommu_device_register because the latter calls
+ * into the hooks.
+ */
+ hv_initialize_special_domains();
+
+ ret = iommu_device_register(iommup, &hv_iommu_ops, NULL);
+ if (ret) {
+ pr_err("Hyper-V: iommu_device_register failed: %d\n", ret);
+ goto err_sysfs_remove;
+ }
+
+ pr_info("Hyper-V IOMMU initialized\n");
+
+ return 0;
+
+err_sysfs_remove:
+ iommu_device_sysfs_remove(iommup);
+ return ret;
+}
+
+void __init hv_iommu_detect(void)
+{
+ if (no_iommu || iommu_detected)
+ return;
+
+ /* For l1vh, always expose an iommu unit */
+ if (!hv_l1vh_partition())
+ if (!(ms_hyperv.misc_features & HV_DEVICE_DOMAIN_AVAILABLE))
+ return;
+
+ iommu_detected = 1;
+ x86_init.iommu.iommu_init = hv_iommu_init;
+
+ pci_request_acs();
+}
diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h
index dfc516c1c719..2ad111727e82 100644
--- a/include/linux/hyperv.h
+++ b/include/linux/hyperv.h
@@ -1767,4 +1767,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 */
--
2.51.2.vfs.0.1
^ permalink raw reply related
* [PATCH v0 14/15] mshv: Remove mapping of mmio space during map user ioctl
From: Mukesh R @ 2026-01-20 6:42 UTC (permalink / raw)
To: linux-kernel, linux-hyperv, linux-arm-kernel, iommu, linux-pci,
linux-arch
Cc: kys, haiyangz, wei.liu, decui, longli, catalin.marinas, will,
tglx, mingo, bp, dave.hansen, hpa, joro, lpieralisi, kwilczynski,
mani, robh, bhelgaas, arnd, nunodasneves, mhklinux, romank
In-Reply-To: <20260120064230.3602565-1-mrathor@linux.microsoft.com>
From: Mukesh Rathor <mrathor@linux.microsoft.com>
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.
Signed-off-by: Mukesh Rathor <mrathor@linux.microsoft.com>
---
drivers/hv/mshv_root_main.c | 20 ++++----------------
1 file changed, 4 insertions(+), 16 deletions(-)
diff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c
index 27313419828d..03f3aa9f5541 100644
--- a/drivers/hv/mshv_root_main.c
+++ b/drivers/hv/mshv_root_main.c
@@ -1258,16 +1258,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.
*/
static long
mshv_map_user_memory(struct mshv_partition *partition,
@@ -1276,7 +1268,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) ||
@@ -1286,7 +1277,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)
@@ -1313,10 +1303,8 @@ mshv_map_user_memory(struct mshv_partition *partition,
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);
+ /* mmio mappings are handled later during intercepts */
+ ret = 0;
break;
}
--
2.51.2.vfs.0.1
^ permalink raw reply related
* [PATCH v0 15/15] mshv: Populate mmio mappings for PCI passthru
From: Mukesh R @ 2026-01-20 6:42 UTC (permalink / raw)
To: linux-kernel, linux-hyperv, linux-arm-kernel, iommu, linux-pci,
linux-arch
Cc: kys, haiyangz, wei.liu, decui, longli, catalin.marinas, will,
tglx, mingo, bp, dave.hansen, hpa, joro, lpieralisi, kwilczynski,
mani, robh, bhelgaas, arnd, nunodasneves, mhklinux, romank
In-Reply-To: <20260120064230.3602565-1-mrathor@linux.microsoft.com>
From: Mukesh Rathor <mrathor@linux.microsoft.com>
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).
Signed-off-by: Mukesh Rathor <mrathor@linux.microsoft.com>
---
drivers/hv/mshv_root_main.c | 115 ++++++++++++++++++++++++++++++++++++
1 file changed, 115 insertions(+)
diff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c
index 03f3aa9f5541..4c8bc7cd0888 100644
--- a/drivers/hv/mshv_root_main.c
+++ b/drivers/hv/mshv_root_main.c
@@ -56,6 +56,14 @@ struct hv_stats_page {
};
} __packed;
+bool hv_nofull_mmio; /* don't map entire mmio region upon fault */
+static int __init setup_hv_full_mmio(char *str)
+{
+ hv_nofull_mmio = true;
+ return 0;
+}
+__setup("hv_nofull_mmio", setup_hv_full_mmio);
+
struct mshv_root mshv_root;
enum hv_scheduler_type hv_scheduler_type;
@@ -612,6 +620,109 @@ mshv_partition_region_by_gfn(struct mshv_partition *partition, u64 gfn)
}
#ifdef CONFIG_X86_64
+
+/*
+ * 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(struct mshv_partition *pt, u64 gfn,
+ u64 *mmio_pfnp)
+{
+ struct vm_area_struct *vma;
+ bool is_mmio;
+ u64 uaddr;
+ struct mshv_mem_region *mreg;
+ struct follow_pfnmap_args pfnmap_args;
+ int rc = -EINVAL;
+
+ /*
+ * Do not allow mem region to be deleted beneath us. VFIO uses
+ * useraddr vma to lookup pci bar pfn.
+ */
+ spin_lock(&pt->pt_mem_regions_lock);
+
+ /* Get the region again under the lock */
+ mreg = mshv_partition_region_by_gfn(pt, gfn);
+ if (mreg == NULL || mreg->type != MSHV_REGION_TYPE_MMIO)
+ goto unlock_pt_out;
+
+ uaddr = mreg->start_uaddr +
+ ((gfn - mreg->start_gfn) << HV_HYP_PAGE_SHIFT);
+
+ 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);
+unlock_pt_out:
+ spin_unlock(&pt->pt_mem_regions_lock);
+ return rc;
+}
+
+/*
+ * At present, the only unmapped gpa is mmio space. Verify if it's mmio
+ * and resolve if possible.
+ * Returns: True if valid mmio intercept and it was handled, else false
+ */
+static bool mshv_handle_unmapped_gpa(struct mshv_vp *vp)
+{
+ struct hv_message *hvmsg = vp->vp_intercept_msg_page;
+ struct hv_x64_memory_intercept_message *msg;
+ union hv_x64_memory_access_info accinfo;
+ u64 gfn, mmio_spa, numpgs;
+ struct mshv_mem_region *mreg;
+ int rc;
+ struct mshv_partition *pt = vp->vp_partition;
+
+ msg = (struct hv_x64_memory_intercept_message *)hvmsg->u.payload;
+ accinfo = msg->memory_access_info;
+
+ if (!accinfo.gva_gpa_valid)
+ return false;
+
+ /* Do a fast check and bail if non mmio intercept */
+ gfn = msg->guest_physical_address >> HV_HYP_PAGE_SHIFT;
+ mreg = mshv_partition_region_by_gfn(pt, gfn);
+ if (mreg == NULL || mreg->type != MSHV_REGION_TYPE_MMIO)
+ return false;
+
+ rc = mshv_chk_get_mmio_start_pfn(pt, gfn, &mmio_spa);
+ if (rc)
+ return false;
+
+ if (!hv_nofull_mmio) { /* default case */
+ gfn = mreg->start_gfn;
+ mmio_spa = mmio_spa - (gfn - mreg->start_gfn);
+ numpgs = mreg->nr_pages;
+ } else
+ numpgs = 1;
+
+ rc = hv_call_map_mmio_pages(pt->pt_id, gfn, mmio_spa, numpgs);
+
+ return rc == 0;
+}
+
static struct mshv_mem_region *
mshv_partition_region_by_gfn_get(struct mshv_partition *p, u64 gfn)
{
@@ -666,13 +777,17 @@ static bool mshv_handle_gpa_intercept(struct mshv_vp *vp)
return ret;
}
+
#else /* CONFIG_X86_64 */
+static bool mshv_handle_unmapped_gpa(struct mshv_vp *vp) { return false; }
static bool mshv_handle_gpa_intercept(struct mshv_vp *vp) { return false; }
#endif /* CONFIG_X86_64 */
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);
}
--
2.51.2.vfs.0.1
^ permalink raw reply related
* Re: [PATCH v3 5/6] mshv: Add definitions for stats pages
From: Stanislav Kinsburskii @ 2026-01-20 15:52 UTC (permalink / raw)
To: Nuno Das Neves
Cc: linux-hyperv, linux-kernel, mhklinux, kys, haiyangz, wei.liu,
decui, longli, prapal, mrathor, paekkaladevi
In-Reply-To: <89385dc3-e702-4bf6-8ad7-f6e634851851@linux.microsoft.com>
On Thu, Jan 15, 2026 at 11:34:48AM -0800, Nuno Das Neves wrote:
> On 1/15/2026 8:19 AM, Stanislav Kinsburskii wrote:
> > On Wed, Jan 14, 2026 at 01:38:02PM -0800, Nuno Das Neves wrote:
> >> Add the definitions for hypervisor, logical processor, and partition
> >> stats pages.
> >>
> >
> > The definitions in for partition and virtual processor are outdated.
> > Now is the good time to sync the new values in.
> >
> > Thanks,
> > Stanislav
> >
>
> Good point, thanks, I will update it for v4.
>
> I'm finally noticing that these counters are not really from hvhdk.h, in
> the windows code, but their own file. Since I'm still iterating on this,
> what do you think about creating a file just for the counters?
> e.g. drivers/hv/hvcounters.h, which combines hvcountersarm64 and amd64.
>
I don't have a preference as I personanly think the the while idea of
keeping the headers in sync with Windows naming scheme isn't great, but
if you think it makes sense, go ahead.
> That would have a couple of advantages:
> 1. Not putting things in hvhdk.h which aren't actually there in the
> Windows source
> 2. Less visibility of CamelCase naming outside our driver
> 3. I could define the enums using "X macro"s to generate the show() code
> more cleanly in mshv_debugfs.c, which is something Michael suggested
> here:
> https://lore.kernel.org/linux-hyperv/SN6PR02MB4157938404BC0D12978ACD9BD4A2A@SN6PR02MB4157.namprd02.prod.outlook.com/
>
> It would look something like this:
>
> In hvcounters.h:
>
> #if is_enabled(CONFIG_X86_64)
>
> #define HV_COUNTER_VP_LIST(X) \
> X(VpTotalRunTime, 1), \
> X(VpHypervisorRunTime, 2), \
> X(VpRemoteNodeRunTime, 3), \
> /* <snip> */
>
> #elif is_enabled(CONFIG_ARM64)
>
> /* <snip> */
>
> #endif
>
> Just like now, it's a copy/paste from Windows + simple pattern
> replacement. Note with this approach we need separate lists for arm64
> and x86, but that matches how the enums are defined in Windows.
>
> Then, in mshv_debugfs.c:
>
> /*
> * We need the strings paired with their enum values.
> * This structure can be used for all the different stat types.
> */
> struct hv_counter_entry {
> char *name;
> int idx;
> };
>
> /* Define an array entry (again, reusable) */
> #define HV_COUNTER_LIST(name, idx) \
> { __stringify(name), idx },
>
> /* Create our static array */
> static struct hv_counter_entry hv_counter_vp_array[] = {
> HV_ST_COUNTER_VP(HV_COUNTER_VP)
> };
>
> static int vp_stats_show(struct seq_file *m, void *v)
> {
> const struct hv_stats_page **pstats = m->private;
> int i;
>
> for (i = 0; i < ARRAY_SIZE(hv_counter_vp_array); ++i) {
> struct hv_counter_entry entry = hv_counter_vp_array[i];
> u64 parent_val = pstats[HV_STATS_AREA_PARENT]->vp_cntrs[entry.idx];
> u64 self_val = pstats[HV_STATS_AREA_SELF]->vp_cntrs[entry.idx];
>
> /* Prioritize the PARENT area value */
> seq_printf(m, "%-30s: %llu\n", entry.name,
> parent_val ? parent_val : self_val);
> }
> }
>
> Any thoughts? I was originally going to just go with the pattern we had,
> but since these definitions aren't from the hv*dk.h files, we can maybe
> get more creative and make the resulting code look a bit better.
>
Looks good to me.
Thanks,
Stanislav
> Thanks
> Nuno
>
> >> Move the definition for the VP stats page to its rightful place in
> >> hvhdk.h, and add the missing members.
> >>
> >> While at it, correct the ARM64 value of VpRootDispatchThreadBlocked,
> >> (which is not yet used, so there is no impact).
> >>
> >> These enum members retain their CamelCase style, since they are imported
> >> directly from the hypervisor code. They will be stringified when
> >> printing the stats out, and retain more readability in this form.
> >>
> >> Signed-off-by: Nuno Das Neves <nunodasneves@linux.microsoft.com>
> >> ---
> >> drivers/hv/mshv_root_main.c | 17 --
> >> include/hyperv/hvhdk.h | 437 ++++++++++++++++++++++++++++++++++++
> >> 2 files changed, 437 insertions(+), 17 deletions(-)
> >>
> >> diff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c
> >> index fbfc9e7d9fa4..724bbaa0b08c 100644
> >> --- a/drivers/hv/mshv_root_main.c
> >> +++ b/drivers/hv/mshv_root_main.c
> >> @@ -39,23 +39,6 @@ MODULE_AUTHOR("Microsoft");
> >> MODULE_LICENSE("GPL");
> >> MODULE_DESCRIPTION("Microsoft Hyper-V root partition VMM interface /dev/mshv");
> >>
> >> -/* TODO move this to another file when debugfs code is added */
> >> -enum hv_stats_vp_counters { /* HV_THREAD_COUNTER */
> >> -#if defined(CONFIG_X86)
> >> - VpRootDispatchThreadBlocked = 202,
> >> -#elif defined(CONFIG_ARM64)
> >> - VpRootDispatchThreadBlocked = 94,
> >> -#endif
> >> - VpStatsMaxCounter
> >> -};
> >> -
> >> -struct hv_stats_page {
> >> - union {
> >> - u64 vp_cntrs[VpStatsMaxCounter]; /* VP counters */
> >> - u8 data[HV_HYP_PAGE_SIZE];
> >> - };
> >> -} __packed;
> >> -
> >> struct mshv_root mshv_root;
> >>
> >> enum hv_scheduler_type hv_scheduler_type;
> >> diff --git a/include/hyperv/hvhdk.h b/include/hyperv/hvhdk.h
> >> index 469186df7826..8bddd11feeba 100644
> >> --- a/include/hyperv/hvhdk.h
> >> +++ b/include/hyperv/hvhdk.h
> >> @@ -10,6 +10,443 @@
> >> #include "hvhdk_mini.h"
> >> #include "hvgdk.h"
> >>
> >> +enum hv_stats_hypervisor_counters { /* HV_HYPERVISOR_COUNTER */
> >> + HvLogicalProcessors = 1,
> >> + HvPartitions = 2,
> >> + HvTotalPages = 3,
> >> + HvVirtualProcessors = 4,
> >> + HvMonitoredNotifications = 5,
> >> + HvModernStandbyEntries = 6,
> >> + HvPlatformIdleTransitions = 7,
> >> + HvHypervisorStartupCost = 8,
> >> + HvIOSpacePages = 10,
> >> + HvNonEssentialPagesForDump = 11,
> >> + HvSubsumedPages = 12,
> >> + HvStatsMaxCounter
> >> +};
> >> +
> >> +enum hv_stats_partition_counters { /* HV_PROCESS_COUNTER */
> >> + PartitionVirtualProcessors = 1,
> >> + PartitionTlbSize = 3,
> >> + PartitionAddressSpaces = 4,
> >> + PartitionDepositedPages = 5,
> >> + PartitionGpaPages = 6,
> >> + PartitionGpaSpaceModifications = 7,
> >> + PartitionVirtualTlbFlushEntires = 8,
> >> + PartitionRecommendedTlbSize = 9,
> >> + PartitionGpaPages4K = 10,
> >> + PartitionGpaPages2M = 11,
> >> + PartitionGpaPages1G = 12,
> >> + PartitionGpaPages512G = 13,
> >> + PartitionDevicePages4K = 14,
> >> + PartitionDevicePages2M = 15,
> >> + PartitionDevicePages1G = 16,
> >> + PartitionDevicePages512G = 17,
> >> + PartitionAttachedDevices = 18,
> >> + PartitionDeviceInterruptMappings = 19,
> >> + PartitionIoTlbFlushes = 20,
> >> + PartitionIoTlbFlushCost = 21,
> >> + PartitionDeviceInterruptErrors = 22,
> >> + PartitionDeviceDmaErrors = 23,
> >> + PartitionDeviceInterruptThrottleEvents = 24,
> >> + PartitionSkippedTimerTicks = 25,
> >> + PartitionPartitionId = 26,
> >> +#if IS_ENABLED(CONFIG_X86_64)
> >> + PartitionNestedTlbSize = 27,
> >> + PartitionRecommendedNestedTlbSize = 28,
> >> + PartitionNestedTlbFreeListSize = 29,
> >> + PartitionNestedTlbTrimmedPages = 30,
> >> + PartitionPagesShattered = 31,
> >> + PartitionPagesRecombined = 32,
> >> + PartitionHwpRequestValue = 33,
> >> +#elif IS_ENABLED(CONFIG_ARM64)
> >> + PartitionHwpRequestValue = 27,
> >> +#endif
> >> + PartitionStatsMaxCounter
> >> +};
> >> +
> >> +enum hv_stats_vp_counters { /* HV_THREAD_COUNTER */
> >> + VpTotalRunTime = 1,
> >> + VpHypervisorRunTime = 2,
> >> + VpRemoteNodeRunTime = 3,
> >> + VpNormalizedRunTime = 4,
> >> + VpIdealCpu = 5,
> >> + VpHypercallsCount = 7,
> >> + VpHypercallsTime = 8,
> >> +#if IS_ENABLED(CONFIG_X86_64)
> >> + VpPageInvalidationsCount = 9,
> >> + VpPageInvalidationsTime = 10,
> >> + VpControlRegisterAccessesCount = 11,
> >> + VpControlRegisterAccessesTime = 12,
> >> + VpIoInstructionsCount = 13,
> >> + VpIoInstructionsTime = 14,
> >> + VpHltInstructionsCount = 15,
> >> + VpHltInstructionsTime = 16,
> >> + VpMwaitInstructionsCount = 17,
> >> + VpMwaitInstructionsTime = 18,
> >> + VpCpuidInstructionsCount = 19,
> >> + VpCpuidInstructionsTime = 20,
> >> + VpMsrAccessesCount = 21,
> >> + VpMsrAccessesTime = 22,
> >> + VpOtherInterceptsCount = 23,
> >> + VpOtherInterceptsTime = 24,
> >> + VpExternalInterruptsCount = 25,
> >> + VpExternalInterruptsTime = 26,
> >> + VpPendingInterruptsCount = 27,
> >> + VpPendingInterruptsTime = 28,
> >> + VpEmulatedInstructionsCount = 29,
> >> + VpEmulatedInstructionsTime = 30,
> >> + VpDebugRegisterAccessesCount = 31,
> >> + VpDebugRegisterAccessesTime = 32,
> >> + VpPageFaultInterceptsCount = 33,
> >> + VpPageFaultInterceptsTime = 34,
> >> + VpGuestPageTableMaps = 35,
> >> + VpLargePageTlbFills = 36,
> >> + VpSmallPageTlbFills = 37,
> >> + VpReflectedGuestPageFaults = 38,
> >> + VpApicMmioAccesses = 39,
> >> + VpIoInterceptMessages = 40,
> >> + VpMemoryInterceptMessages = 41,
> >> + VpApicEoiAccesses = 42,
> >> + VpOtherMessages = 43,
> >> + VpPageTableAllocations = 44,
> >> + VpLogicalProcessorMigrations = 45,
> >> + VpAddressSpaceEvictions = 46,
> >> + VpAddressSpaceSwitches = 47,
> >> + VpAddressDomainFlushes = 48,
> >> + VpAddressSpaceFlushes = 49,
> >> + VpGlobalGvaRangeFlushes = 50,
> >> + VpLocalGvaRangeFlushes = 51,
> >> + VpPageTableEvictions = 52,
> >> + VpPageTableReclamations = 53,
> >> + VpPageTableResets = 54,
> >> + VpPageTableValidations = 55,
> >> + VpApicTprAccesses = 56,
> >> + VpPageTableWriteIntercepts = 57,
> >> + VpSyntheticInterrupts = 58,
> >> + VpVirtualInterrupts = 59,
> >> + VpApicIpisSent = 60,
> >> + VpApicSelfIpisSent = 61,
> >> + VpGpaSpaceHypercalls = 62,
> >> + VpLogicalProcessorHypercalls = 63,
> >> + VpLongSpinWaitHypercalls = 64,
> >> + VpOtherHypercalls = 65,
> >> + VpSyntheticInterruptHypercalls = 66,
> >> + VpVirtualInterruptHypercalls = 67,
> >> + VpVirtualMmuHypercalls = 68,
> >> + VpVirtualProcessorHypercalls = 69,
> >> + VpHardwareInterrupts = 70,
> >> + VpNestedPageFaultInterceptsCount = 71,
> >> + VpNestedPageFaultInterceptsTime = 72,
> >> + VpPageScans = 73,
> >> + VpLogicalProcessorDispatches = 74,
> >> + VpWaitingForCpuTime = 75,
> >> + VpExtendedHypercalls = 76,
> >> + VpExtendedHypercallInterceptMessages = 77,
> >> + VpMbecNestedPageTableSwitches = 78,
> >> + VpOtherReflectedGuestExceptions = 79,
> >> + VpGlobalIoTlbFlushes = 80,
> >> + VpGlobalIoTlbFlushCost = 81,
> >> + VpLocalIoTlbFlushes = 82,
> >> + VpLocalIoTlbFlushCost = 83,
> >> + VpHypercallsForwardedCount = 84,
> >> + VpHypercallsForwardingTime = 85,
> >> + VpPageInvalidationsForwardedCount = 86,
> >> + VpPageInvalidationsForwardingTime = 87,
> >> + VpControlRegisterAccessesForwardedCount = 88,
> >> + VpControlRegisterAccessesForwardingTime = 89,
> >> + VpIoInstructionsForwardedCount = 90,
> >> + VpIoInstructionsForwardingTime = 91,
> >> + VpHltInstructionsForwardedCount = 92,
> >> + VpHltInstructionsForwardingTime = 93,
> >> + VpMwaitInstructionsForwardedCount = 94,
> >> + VpMwaitInstructionsForwardingTime = 95,
> >> + VpCpuidInstructionsForwardedCount = 96,
> >> + VpCpuidInstructionsForwardingTime = 97,
> >> + VpMsrAccessesForwardedCount = 98,
> >> + VpMsrAccessesForwardingTime = 99,
> >> + VpOtherInterceptsForwardedCount = 100,
> >> + VpOtherInterceptsForwardingTime = 101,
> >> + VpExternalInterruptsForwardedCount = 102,
> >> + VpExternalInterruptsForwardingTime = 103,
> >> + VpPendingInterruptsForwardedCount = 104,
> >> + VpPendingInterruptsForwardingTime = 105,
> >> + VpEmulatedInstructionsForwardedCount = 106,
> >> + VpEmulatedInstructionsForwardingTime = 107,
> >> + VpDebugRegisterAccessesForwardedCount = 108,
> >> + VpDebugRegisterAccessesForwardingTime = 109,
> >> + VpPageFaultInterceptsForwardedCount = 110,
> >> + VpPageFaultInterceptsForwardingTime = 111,
> >> + VpVmclearEmulationCount = 112,
> >> + VpVmclearEmulationTime = 113,
> >> + VpVmptrldEmulationCount = 114,
> >> + VpVmptrldEmulationTime = 115,
> >> + VpVmptrstEmulationCount = 116,
> >> + VpVmptrstEmulationTime = 117,
> >> + VpVmreadEmulationCount = 118,
> >> + VpVmreadEmulationTime = 119,
> >> + VpVmwriteEmulationCount = 120,
> >> + VpVmwriteEmulationTime = 121,
> >> + VpVmxoffEmulationCount = 122,
> >> + VpVmxoffEmulationTime = 123,
> >> + VpVmxonEmulationCount = 124,
> >> + VpVmxonEmulationTime = 125,
> >> + VpNestedVMEntriesCount = 126,
> >> + VpNestedVMEntriesTime = 127,
> >> + VpNestedSLATSoftPageFaultsCount = 128,
> >> + VpNestedSLATSoftPageFaultsTime = 129,
> >> + VpNestedSLATHardPageFaultsCount = 130,
> >> + VpNestedSLATHardPageFaultsTime = 131,
> >> + VpInvEptAllContextEmulationCount = 132,
> >> + VpInvEptAllContextEmulationTime = 133,
> >> + VpInvEptSingleContextEmulationCount = 134,
> >> + VpInvEptSingleContextEmulationTime = 135,
> >> + VpInvVpidAllContextEmulationCount = 136,
> >> + VpInvVpidAllContextEmulationTime = 137,
> >> + VpInvVpidSingleContextEmulationCount = 138,
> >> + VpInvVpidSingleContextEmulationTime = 139,
> >> + VpInvVpidSingleAddressEmulationCount = 140,
> >> + VpInvVpidSingleAddressEmulationTime = 141,
> >> + VpNestedTlbPageTableReclamations = 142,
> >> + VpNestedTlbPageTableEvictions = 143,
> >> + VpFlushGuestPhysicalAddressSpaceHypercalls = 144,
> >> + VpFlushGuestPhysicalAddressListHypercalls = 145,
> >> + VpPostedInterruptNotifications = 146,
> >> + VpPostedInterruptScans = 147,
> >> + VpTotalCoreRunTime = 148,
> >> + VpMaximumRunTime = 149,
> >> + VpHwpRequestContextSwitches = 150,
> >> + VpWaitingForCpuTimeBucket0 = 151,
> >> + VpWaitingForCpuTimeBucket1 = 152,
> >> + VpWaitingForCpuTimeBucket2 = 153,
> >> + VpWaitingForCpuTimeBucket3 = 154,
> >> + VpWaitingForCpuTimeBucket4 = 155,
> >> + VpWaitingForCpuTimeBucket5 = 156,
> >> + VpWaitingForCpuTimeBucket6 = 157,
> >> + VpVmloadEmulationCount = 158,
> >> + VpVmloadEmulationTime = 159,
> >> + VpVmsaveEmulationCount = 160,
> >> + VpVmsaveEmulationTime = 161,
> >> + VpGifInstructionEmulationCount = 162,
> >> + VpGifInstructionEmulationTime = 163,
> >> + VpEmulatedErrataSvmInstructions = 164,
> >> + VpPlaceholder1 = 165,
> >> + VpPlaceholder2 = 166,
> >> + VpPlaceholder3 = 167,
> >> + VpPlaceholder4 = 168,
> >> + VpPlaceholder5 = 169,
> >> + VpPlaceholder6 = 170,
> >> + VpPlaceholder7 = 171,
> >> + VpPlaceholder8 = 172,
> >> + VpPlaceholder9 = 173,
> >> + VpPlaceholder10 = 174,
> >> + VpSchedulingPriority = 175,
> >> + VpRdpmcInstructionsCount = 176,
> >> + VpRdpmcInstructionsTime = 177,
> >> + VpPerfmonPmuMsrAccessesCount = 178,
> >> + VpPerfmonLbrMsrAccessesCount = 179,
> >> + VpPerfmonIptMsrAccessesCount = 180,
> >> + VpPerfmonInterruptCount = 181,
> >> + VpVtl1DispatchCount = 182,
> >> + VpVtl2DispatchCount = 183,
> >> + VpVtl2DispatchBucket0 = 184,
> >> + VpVtl2DispatchBucket1 = 185,
> >> + VpVtl2DispatchBucket2 = 186,
> >> + VpVtl2DispatchBucket3 = 187,
> >> + VpVtl2DispatchBucket4 = 188,
> >> + VpVtl2DispatchBucket5 = 189,
> >> + VpVtl2DispatchBucket6 = 190,
> >> + VpVtl1RunTime = 191,
> >> + VpVtl2RunTime = 192,
> >> + VpIommuHypercalls = 193,
> >> + VpCpuGroupHypercalls = 194,
> >> + VpVsmHypercalls = 195,
> >> + VpEventLogHypercalls = 196,
> >> + VpDeviceDomainHypercalls = 197,
> >> + VpDepositHypercalls = 198,
> >> + VpSvmHypercalls = 199,
> >> + VpBusLockAcquisitionCount = 200,
> >> + VpLoadAvg = 201,
> >> + VpRootDispatchThreadBlocked = 202,
> >> +#elif IS_ENABLED(CONFIG_ARM64)
> >> + VpSysRegAccessesCount = 9,
> >> + VpSysRegAccessesTime = 10,
> >> + VpSmcInstructionsCount = 11,
> >> + VpSmcInstructionsTime = 12,
> >> + VpOtherInterceptsCount = 13,
> >> + VpOtherInterceptsTime = 14,
> >> + VpExternalInterruptsCount = 15,
> >> + VpExternalInterruptsTime = 16,
> >> + VpPendingInterruptsCount = 17,
> >> + VpPendingInterruptsTime = 18,
> >> + VpGuestPageTableMaps = 19,
> >> + VpLargePageTlbFills = 20,
> >> + VpSmallPageTlbFills = 21,
> >> + VpReflectedGuestPageFaults = 22,
> >> + VpMemoryInterceptMessages = 23,
> >> + VpOtherMessages = 24,
> >> + VpLogicalProcessorMigrations = 25,
> >> + VpAddressDomainFlushes = 26,
> >> + VpAddressSpaceFlushes = 27,
> >> + VpSyntheticInterrupts = 28,
> >> + VpVirtualInterrupts = 29,
> >> + VpApicSelfIpisSent = 30,
> >> + VpGpaSpaceHypercalls = 31,
> >> + VpLogicalProcessorHypercalls = 32,
> >> + VpLongSpinWaitHypercalls = 33,
> >> + VpOtherHypercalls = 34,
> >> + VpSyntheticInterruptHypercalls = 35,
> >> + VpVirtualInterruptHypercalls = 36,
> >> + VpVirtualMmuHypercalls = 37,
> >> + VpVirtualProcessorHypercalls = 38,
> >> + VpHardwareInterrupts = 39,
> >> + VpNestedPageFaultInterceptsCount = 40,
> >> + VpNestedPageFaultInterceptsTime = 41,
> >> + VpLogicalProcessorDispatches = 42,
> >> + VpWaitingForCpuTime = 43,
> >> + VpExtendedHypercalls = 44,
> >> + VpExtendedHypercallInterceptMessages = 45,
> >> + VpMbecNestedPageTableSwitches = 46,
> >> + VpOtherReflectedGuestExceptions = 47,
> >> + VpGlobalIoTlbFlushes = 48,
> >> + VpGlobalIoTlbFlushCost = 49,
> >> + VpLocalIoTlbFlushes = 50,
> >> + VpLocalIoTlbFlushCost = 51,
> >> + VpFlushGuestPhysicalAddressSpaceHypercalls = 52,
> >> + VpFlushGuestPhysicalAddressListHypercalls = 53,
> >> + VpPostedInterruptNotifications = 54,
> >> + VpPostedInterruptScans = 55,
> >> + VpTotalCoreRunTime = 56,
> >> + VpMaximumRunTime = 57,
> >> + VpWaitingForCpuTimeBucket0 = 58,
> >> + VpWaitingForCpuTimeBucket1 = 59,
> >> + VpWaitingForCpuTimeBucket2 = 60,
> >> + VpWaitingForCpuTimeBucket3 = 61,
> >> + VpWaitingForCpuTimeBucket4 = 62,
> >> + VpWaitingForCpuTimeBucket5 = 63,
> >> + VpWaitingForCpuTimeBucket6 = 64,
> >> + VpHwpRequestContextSwitches = 65,
> >> + VpPlaceholder2 = 66,
> >> + VpPlaceholder3 = 67,
> >> + VpPlaceholder4 = 68,
> >> + VpPlaceholder5 = 69,
> >> + VpPlaceholder6 = 70,
> >> + VpPlaceholder7 = 71,
> >> + VpPlaceholder8 = 72,
> >> + VpContentionTime = 73,
> >> + VpWakeUpTime = 74,
> >> + VpSchedulingPriority = 75,
> >> + VpVtl1DispatchCount = 76,
> >> + VpVtl2DispatchCount = 77,
> >> + VpVtl2DispatchBucket0 = 78,
> >> + VpVtl2DispatchBucket1 = 79,
> >> + VpVtl2DispatchBucket2 = 80,
> >> + VpVtl2DispatchBucket3 = 81,
> >> + VpVtl2DispatchBucket4 = 82,
> >> + VpVtl2DispatchBucket5 = 83,
> >> + VpVtl2DispatchBucket6 = 84,
> >> + VpVtl1RunTime = 85,
> >> + VpVtl2RunTime = 86,
> >> + VpIommuHypercalls = 87,
> >> + VpCpuGroupHypercalls = 88,
> >> + VpVsmHypercalls = 89,
> >> + VpEventLogHypercalls = 90,
> >> + VpDeviceDomainHypercalls = 91,
> >> + VpDepositHypercalls = 92,
> >> + VpSvmHypercalls = 93,
> >> + VpLoadAvg = 94,
> >> + VpRootDispatchThreadBlocked = 95,
> >> +#endif
> >> + VpStatsMaxCounter
> >> +};
> >> +
> >> +enum hv_stats_lp_counters { /* HV_CPU_COUNTER */
> >> + LpGlobalTime = 1,
> >> + LpTotalRunTime = 2,
> >> + LpHypervisorRunTime = 3,
> >> + LpHardwareInterrupts = 4,
> >> + LpContextSwitches = 5,
> >> + LpInterProcessorInterrupts = 6,
> >> + LpSchedulerInterrupts = 7,
> >> + LpTimerInterrupts = 8,
> >> + LpInterProcessorInterruptsSent = 9,
> >> + LpProcessorHalts = 10,
> >> + LpMonitorTransitionCost = 11,
> >> + LpContextSwitchTime = 12,
> >> + LpC1TransitionsCount = 13,
> >> + LpC1RunTime = 14,
> >> + LpC2TransitionsCount = 15,
> >> + LpC2RunTime = 16,
> >> + LpC3TransitionsCount = 17,
> >> + LpC3RunTime = 18,
> >> + LpRootVpIndex = 19,
> >> + LpIdleSequenceNumber = 20,
> >> + LpGlobalTscCount = 21,
> >> + LpActiveTscCount = 22,
> >> + LpIdleAccumulation = 23,
> >> + LpReferenceCycleCount0 = 24,
> >> + LpActualCycleCount0 = 25,
> >> + LpReferenceCycleCount1 = 26,
> >> + LpActualCycleCount1 = 27,
> >> + LpProximityDomainId = 28,
> >> + LpPostedInterruptNotifications = 29,
> >> + LpBranchPredictorFlushes = 30,
> >> +#if IS_ENABLED(CONFIG_X86_64)
> >> + LpL1DataCacheFlushes = 31,
> >> + LpImmediateL1DataCacheFlushes = 32,
> >> + LpMbFlushes = 33,
> >> + LpCounterRefreshSequenceNumber = 34,
> >> + LpCounterRefreshReferenceTime = 35,
> >> + LpIdleAccumulationSnapshot = 36,
> >> + LpActiveTscCountSnapshot = 37,
> >> + LpHwpRequestContextSwitches = 38,
> >> + LpPlaceholder1 = 39,
> >> + LpPlaceholder2 = 40,
> >> + LpPlaceholder3 = 41,
> >> + LpPlaceholder4 = 42,
> >> + LpPlaceholder5 = 43,
> >> + LpPlaceholder6 = 44,
> >> + LpPlaceholder7 = 45,
> >> + LpPlaceholder8 = 46,
> >> + LpPlaceholder9 = 47,
> >> + LpPlaceholder10 = 48,
> >> + LpReserveGroupId = 49,
> >> + LpRunningPriority = 50,
> >> + LpPerfmonInterruptCount = 51,
> >> +#elif IS_ENABLED(CONFIG_ARM64)
> >> + LpCounterRefreshSequenceNumber = 31,
> >> + LpCounterRefreshReferenceTime = 32,
> >> + LpIdleAccumulationSnapshot = 33,
> >> + LpActiveTscCountSnapshot = 34,
> >> + LpHwpRequestContextSwitches = 35,
> >> + LpPlaceholder2 = 36,
> >> + LpPlaceholder3 = 37,
> >> + LpPlaceholder4 = 38,
> >> + LpPlaceholder5 = 39,
> >> + LpPlaceholder6 = 40,
> >> + LpPlaceholder7 = 41,
> >> + LpPlaceholder8 = 42,
> >> + LpPlaceholder9 = 43,
> >> + LpSchLocalRunListSize = 44,
> >> + LpReserveGroupId = 45,
> >> + LpRunningPriority = 46,
> >> +#endif
> >> + LpStatsMaxCounter
> >> +};
> >> +
> >> +/*
> >> + * Hypervisor statistics page format
> >> + */
> >> +struct hv_stats_page {
> >> + union {
> >> + u64 hv_cntrs[HvStatsMaxCounter]; /* Hypervisor counters */
> >> + u64 pt_cntrs[PartitionStatsMaxCounter]; /* Partition counters */
> >> + u64 vp_cntrs[VpStatsMaxCounter]; /* VP counters */
> >> + u64 lp_cntrs[LpStatsMaxCounter]; /* LP counters */
> >> + u8 data[HV_HYP_PAGE_SIZE];
> >> + };
> >> +} __packed;
> >> +
> >> /* Bits for dirty mask of hv_vp_register_page */
> >> #define HV_X64_REGISTER_CLASS_GENERAL 0
> >> #define HV_X64_REGISTER_CLASS_IP 1
> >> --
> >> 2.34.1
^ permalink raw reply
* Re: [PATCH v0 06/15] mshv: Implement mshv bridge device for VFIO
From: Stanislav Kinsburskii @ 2026-01-20 16:09 UTC (permalink / raw)
To: Mukesh R
Cc: linux-kernel, linux-hyperv, linux-arm-kernel, iommu, linux-pci,
linux-arch, kys, haiyangz, wei.liu, decui, longli,
catalin.marinas, will, tglx, mingo, bp, dave.hansen, hpa, joro,
lpieralisi, kwilczynski, mani, robh, bhelgaas, arnd, nunodasneves,
mhklinux, romank
In-Reply-To: <20260120064230.3602565-7-mrathor@linux.microsoft.com>
On Mon, Jan 19, 2026 at 10:42:21PM -0800, Mukesh R wrote:
> From: Mukesh Rathor <mrathor@linux.microsoft.com>
>
> 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.
>
> Original author: Wei Liu <wei.liu@kernel.org>
> (Slightly modified from the original version).
>
There is a Linux standard for giving credits when code is adapted from.
This doesn't follow that standard. Please fix.
> Signed-off-by: Mukesh Rathor <mrathor@linux.microsoft.com>
> ---
> drivers/hv/Makefile | 3 +-
> drivers/hv/mshv_vfio.c | 210 +++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 212 insertions(+), 1 deletion(-)
> create mode 100644 drivers/hv/mshv_vfio.c
>
> diff --git a/drivers/hv/Makefile b/drivers/hv/Makefile
> index a49f93c2d245..eae003c4cb8f 100644
> --- a/drivers/hv/Makefile
> +++ b/drivers/hv/Makefile
> @@ -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_vtl-y := mshv_vtl_main.o
>
> # Code that must be built-in
> diff --git a/drivers/hv/mshv_vfio.c b/drivers/hv/mshv_vfio.c
> new file mode 100644
> index 000000000000..6ea4d99a3bd2
> --- /dev/null
> +++ b/drivers/hv/mshv_vfio.c
> @@ -0,0 +1,210 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * VFIO-MSHV bridge pseudo device
> + *
> + * Heavily inspired by the VFIO-KVM bridge pseudo device.
> + */
> +#include <linux/errno.h>
> +#include <linux/file.h>
> +#include <linux/list.h>
> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/slab.h>
> +#include <linux/vfio.h>
> +
> +#include "mshv.h"
> +#include "mshv_root.h"
> +
> +struct mshv_vfio_file {
> + struct list_head node;
> + struct file *file; /* list of struct mshv_vfio_file */
> +};
> +
> +struct mshv_vfio {
> + struct list_head file_list;
> + struct mutex lock;
> +};
> +
> +static bool mshv_vfio_file_is_valid(struct file *file)
> +{
> + bool (*fn)(struct file *file);
> + bool ret;
> +
> + fn = symbol_get(vfio_file_is_valid);
> + if (!fn)
> + return false;
> +
> + ret = fn(file);
> +
> + symbol_put(vfio_file_is_valid);
> +
> + return ret;
> +}
> +
> +static long mshv_vfio_file_add(struct mshv_device *mshvdev, unsigned int fd)
> +{
> + struct mshv_vfio *mshv_vfio = mshvdev->device_private;
> + struct mshv_vfio_file *mvf;
> + struct file *filp;
> + long ret = 0;
> +
> + filp = fget(fd);
> + if (!filp)
> + return -EBADF;
> +
> + /* Ensure the FD is a vfio FD. */
> + if (!mshv_vfio_file_is_valid(filp)) {
> + ret = -EINVAL;
> + goto out_fput;
> + }
> +
> + mutex_lock(&mshv_vfio->lock);
> +
> + list_for_each_entry(mvf, &mshv_vfio->file_list, node) {
> + if (mvf->file == filp) {
> + ret = -EEXIST;
> + goto out_unlock;
> + }
> + }
> +
> + mvf = kzalloc(sizeof(*mvf), GFP_KERNEL_ACCOUNT);
> + if (!mvf) {
> + ret = -ENOMEM;
> + goto out_unlock;
> + }
> +
> + mvf->file = get_file(filp);
> + list_add_tail(&mvf->node, &mshv_vfio->file_list);
> +
> +out_unlock:
> + mutex_unlock(&mshv_vfio->lock);
> +out_fput:
> + fput(filp);
> + return ret;
> +}
> +
> +static long mshv_vfio_file_del(struct mshv_device *mshvdev, unsigned int fd)
> +{
> + struct mshv_vfio *mshv_vfio = mshvdev->device_private;
> + struct mshv_vfio_file *mvf;
> + long ret;
> +
> + CLASS(fd, f)(fd);
> +
> + if (fd_empty(f))
> + return -EBADF;
> +
> + ret = -ENOENT;
> + mutex_lock(&mshv_vfio->lock);
> +
> + list_for_each_entry(mvf, &mshv_vfio->file_list, node) {
> + if (mvf->file != fd_file(f))
> + continue;
> +
> + list_del(&mvf->node);
> + fput(mvf->file);
> + kfree(mvf);
> + ret = 0;
> + break;
> + }
> +
> + mutex_unlock(&mshv_vfio->lock);
> + return ret;
> +}
> +
> +static long mshv_vfio_set_file(struct mshv_device *mshvdev, long attr,
> + void __user *arg)
> +{
> + int32_t __user *argp = arg;
> + int32_t fd;
> +
> + switch (attr) {
> + case MSHV_DEV_VFIO_FILE_ADD:
> + if (get_user(fd, argp))
> + return -EFAULT;
> + return mshv_vfio_file_add(mshvdev, fd);
> +
> + case MSHV_DEV_VFIO_FILE_DEL:
> + if (get_user(fd, argp))
> + return -EFAULT;
> + return mshv_vfio_file_del(mshvdev, fd);
> + }
> +
> + return -ENXIO;
> +}
> +
> +static long mshv_vfio_set_attr(struct mshv_device *mshvdev,
> + struct mshv_device_attr *attr)
> +{
> + switch (attr->group) {
> + case MSHV_DEV_VFIO_FILE:
> + return mshv_vfio_set_file(mshvdev, attr->attr,
> + u64_to_user_ptr(attr->addr));
> + }
> +
> + return -ENXIO;
> +}
> +
> +static long mshv_vfio_has_attr(struct mshv_device *mshvdev,
> + struct mshv_device_attr *attr)
> +{
> + switch (attr->group) {
> + case MSHV_DEV_VFIO_FILE:
> + switch (attr->attr) {
> + case MSHV_DEV_VFIO_FILE_ADD:
> + case MSHV_DEV_VFIO_FILE_DEL:
> + return 0;
> + }
> +
> + break;
> + }
> +
> + return -ENXIO;
> +}
> +
> +static long mshv_vfio_create_device(struct mshv_device *mshvdev, u32 type)
> +{
> + 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;
> +
> + mshv_vfio = kzalloc(sizeof(*mshv_vfio), GFP_KERNEL_ACCOUNT);
> + if (mshv_vfio == NULL)
> + return -ENOMEM;
> +
> + INIT_LIST_HEAD(&mshv_vfio->file_list);
> + mutex_init(&mshv_vfio->lock);
> +
> + mshvdev->device_private = mshv_vfio;
> +
> + return 0;
> +}
> +
> +/* This is called from mshv_device_fop_release() */
> +static void mshv_vfio_release_device(struct mshv_device *mshvdev)
> +{
> + struct mshv_vfio *mv = mshvdev->device_private;
> + struct mshv_vfio_file *mvf, *tmp;
> +
> + list_for_each_entry_safe(mvf, tmp, &mv->file_list, node) {
> + fput(mvf->file);
This put must be sync as device must be detached from domain before
attempting partition destruction.
This was explicitly mentioned in the patch originated this code.
Please fix, add a comment and credits to the commit message.
Thanks,
Stanislav
> + list_del(&mvf->node);
> + kfree(mvf);
> + }
> +
> + kfree(mv);
> + kfree(mshvdev);
> +}
> +
> +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,
> +};
> --
> 2.51.2.vfs.0.1
>
^ permalink raw reply
* Re: [PATCH v0 07/15] mshv: Add ioctl support for MSHV-VFIO bridge device
From: Stanislav Kinsburskii @ 2026-01-20 16:13 UTC (permalink / raw)
To: Mukesh R
Cc: linux-kernel, linux-hyperv, linux-arm-kernel, iommu, linux-pci,
linux-arch, kys, haiyangz, wei.liu, decui, longli,
catalin.marinas, will, tglx, mingo, bp, dave.hansen, hpa, joro,
lpieralisi, kwilczynski, mani, robh, bhelgaas, arnd, nunodasneves,
mhklinux, romank
In-Reply-To: <20260120064230.3602565-8-mrathor@linux.microsoft.com>
On Mon, Jan 19, 2026 at 10:42:22PM -0800, Mukesh R wrote:
> From: Mukesh Rathor <mrathor@linux.microsoft.com>
>
> Add ioctl support for creating MSHV devices for a paritition. 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.
>
> Credits: Original author: Wei Liu <wei.liu@kernel.org>
> NB: Slightly modified from the original version.
>
> Signed-off-by: Mukesh Rathor <mrathor@linux.microsoft.com>
> ---
> drivers/hv/mshv_root_main.c | 126 ++++++++++++++++++++++++++++++++++++
> 1 file changed, 126 insertions(+)
>
> diff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c
> index 83c7bad269a0..27313419828d 100644
> --- a/drivers/hv/mshv_root_main.c
> +++ b/drivers/hv/mshv_root_main.c
> @@ -1551,6 +1551,129 @@ 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,
> +};
> +
> +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;
> + int type;
> +
> + if (copy_from_user(&devargk, uarg, sizeof(devargk))) {
> + rc = -EFAULT;
> + goto out;
> + }
> +
> + /* At present, only VFIO is supported */
> + if (devargk.type != MSHV_DEV_TYPE_VFIO) {
> + rc = -ENODEV;
> + goto out;
> + }
> +
> + if (devargk.flags & MSHV_CREATE_DEVICE_TEST) {
> + rc = 0;
> + goto out;
> + }
> +
> + mshv_dev = kzalloc(sizeof(*mshv_dev), GFP_KERNEL_ACCOUNT);
> + if (mshv_dev == NULL) {
> + rc = -ENOMEM;
> + goto out;
> + }
> +
> + vfio_ops = &mshv_vfio_device_ops;
> + mshv_dev->device_ops = vfio_ops;
> + mshv_dev->device_pt = partition;
> +
> + rc = vfio_ops->device_create(mshv_dev, type);
> + if (rc < 0) {
> + kfree(mshv_dev);
> + goto out;
> + }
> +
> + 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) {
> + mshv_partition_put(partition);
> + hlist_del(&mshv_dev->device_ptnode);
> + vfio_ops->device_release(mshv_dev);
> + goto out;
> + }
> +
> + devargk.fd = rc;
> + rc = 0;
> +
> + if (copy_to_user(uarg, &devargk, sizeof(devargk))) {
Shouldn't the partition be put here?
Thanks,
Stanislav
> + rc = -EFAULT;
> + goto out;
> + }
> +out:
> + return rc;
> +}
> +
> static long
> mshv_partition_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
> {
> @@ -1587,6 +1710,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;
> }
> --
> 2.51.2.vfs.0.1
>
^ permalink raw reply
* Re: [PATCH v0 01/15] iommu/hyperv: rename hyperv-iommu.c to hyperv-irq.c
From: kernel test robot @ 2026-01-20 19:08 UTC (permalink / raw)
To: Mukesh R, linux-kernel, linux-hyperv, linux-arm-kernel, iommu,
linux-pci, linux-arch
Cc: oe-kbuild-all, kys, haiyangz, wei.liu, decui, longli,
catalin.marinas, will, tglx, mingo, bp, dave.hansen, hpa, joro,
lpieralisi, kwilczynski, mani, robh, bhelgaas, arnd, nunodasneves,
mhklinux, romank
In-Reply-To: <20260120064230.3602565-2-mrathor@linux.microsoft.com>
Hi Mukesh,
kernel test robot noticed the following build errors:
[auto build test ERROR on tip/x86/core]
[also build test ERROR on pci/next pci/for-linus arm64/for-next/core clk/clk-next soc/for-next linus/master arnd-asm-generic/master v6.19-rc6 next-20260119]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Mukesh-R/iommu-hyperv-rename-hyperv-iommu-c-to-hyperv-irq-c/20260120-145832
base: tip/x86/core
patch link: https://lore.kernel.org/r/20260120064230.3602565-2-mrathor%40linux.microsoft.com
patch subject: [PATCH v0 01/15] iommu/hyperv: rename hyperv-iommu.c to hyperv-irq.c
config: i386-randconfig-001-20260120 (https://download.01.org/0day-ci/archive/20260121/202601210208.mg3YUkif-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260121/202601210208.mg3YUkif-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202601210208.mg3YUkif-lkp@intel.com/
All errors (new ones prefixed by >>):
In file included from drivers/acpi/pci_root.c:20:
>> include/linux/dmar.h:269:17: error: unknown type name '__u128'; did you mean '__u32'?
269 | __u128 irte;
| ^~~~~~
| __u32
Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for IRQ_REMAP
Depends on [n]: IOMMU_SUPPORT [=y] && X86_64 [=n] && X86_IO_APIC [=y] && PCI_MSI [=n] && ACPI [=y]
Selected by [y]:
- HYPERV_IOMMU [=y] && IOMMU_SUPPORT [=y] && HYPERV [=y] && X86 [=y]
vim +269 include/linux/dmar.h
2ae21010694e56 Suresh Siddha 2008-07-10 200
2ae21010694e56 Suresh Siddha 2008-07-10 201 struct irte {
b1fe7f2cda2a00 Peter Zijlstra 2023-05-31 202 union {
b1fe7f2cda2a00 Peter Zijlstra 2023-05-31 203 struct {
2ae21010694e56 Suresh Siddha 2008-07-10 204 union {
3bf17472226b00 Thomas Gleixner 2015-06-09 205 /* Shared between remapped and posted mode*/
2ae21010694e56 Suresh Siddha 2008-07-10 206 struct {
3bf17472226b00 Thomas Gleixner 2015-06-09 207 __u64 present : 1, /* 0 */
3bf17472226b00 Thomas Gleixner 2015-06-09 208 fpd : 1, /* 1 */
3bf17472226b00 Thomas Gleixner 2015-06-09 209 __res0 : 6, /* 2 - 6 */
3bf17472226b00 Thomas Gleixner 2015-06-09 210 avail : 4, /* 8 - 11 */
3bf17472226b00 Thomas Gleixner 2015-06-09 211 __res1 : 3, /* 12 - 14 */
3bf17472226b00 Thomas Gleixner 2015-06-09 212 pst : 1, /* 15 */
3bf17472226b00 Thomas Gleixner 2015-06-09 213 vector : 8, /* 16 - 23 */
3bf17472226b00 Thomas Gleixner 2015-06-09 214 __res2 : 40; /* 24 - 63 */
3bf17472226b00 Thomas Gleixner 2015-06-09 215 };
3bf17472226b00 Thomas Gleixner 2015-06-09 216
3bf17472226b00 Thomas Gleixner 2015-06-09 217 /* Remapped mode */
3bf17472226b00 Thomas Gleixner 2015-06-09 218 struct {
3bf17472226b00 Thomas Gleixner 2015-06-09 219 __u64 r_present : 1, /* 0 */
3bf17472226b00 Thomas Gleixner 2015-06-09 220 r_fpd : 1, /* 1 */
3bf17472226b00 Thomas Gleixner 2015-06-09 221 dst_mode : 1, /* 2 */
3bf17472226b00 Thomas Gleixner 2015-06-09 222 redir_hint : 1, /* 3 */
3bf17472226b00 Thomas Gleixner 2015-06-09 223 trigger_mode : 1, /* 4 */
3bf17472226b00 Thomas Gleixner 2015-06-09 224 dlvry_mode : 3, /* 5 - 7 */
3bf17472226b00 Thomas Gleixner 2015-06-09 225 r_avail : 4, /* 8 - 11 */
3bf17472226b00 Thomas Gleixner 2015-06-09 226 r_res0 : 4, /* 12 - 15 */
3bf17472226b00 Thomas Gleixner 2015-06-09 227 r_vector : 8, /* 16 - 23 */
3bf17472226b00 Thomas Gleixner 2015-06-09 228 r_res1 : 8, /* 24 - 31 */
3bf17472226b00 Thomas Gleixner 2015-06-09 229 dest_id : 32; /* 32 - 63 */
3bf17472226b00 Thomas Gleixner 2015-06-09 230 };
3bf17472226b00 Thomas Gleixner 2015-06-09 231
3bf17472226b00 Thomas Gleixner 2015-06-09 232 /* Posted mode */
3bf17472226b00 Thomas Gleixner 2015-06-09 233 struct {
3bf17472226b00 Thomas Gleixner 2015-06-09 234 __u64 p_present : 1, /* 0 */
3bf17472226b00 Thomas Gleixner 2015-06-09 235 p_fpd : 1, /* 1 */
3bf17472226b00 Thomas Gleixner 2015-06-09 236 p_res0 : 6, /* 2 - 7 */
3bf17472226b00 Thomas Gleixner 2015-06-09 237 p_avail : 4, /* 8 - 11 */
3bf17472226b00 Thomas Gleixner 2015-06-09 238 p_res1 : 2, /* 12 - 13 */
3bf17472226b00 Thomas Gleixner 2015-06-09 239 p_urgent : 1, /* 14 */
3bf17472226b00 Thomas Gleixner 2015-06-09 240 p_pst : 1, /* 15 */
3bf17472226b00 Thomas Gleixner 2015-06-09 241 p_vector : 8, /* 16 - 23 */
3bf17472226b00 Thomas Gleixner 2015-06-09 242 p_res2 : 14, /* 24 - 37 */
3bf17472226b00 Thomas Gleixner 2015-06-09 243 pda_l : 26; /* 38 - 63 */
2ae21010694e56 Suresh Siddha 2008-07-10 244 };
2ae21010694e56 Suresh Siddha 2008-07-10 245 __u64 low;
2ae21010694e56 Suresh Siddha 2008-07-10 246 };
2ae21010694e56 Suresh Siddha 2008-07-10 247
2ae21010694e56 Suresh Siddha 2008-07-10 248 union {
3bf17472226b00 Thomas Gleixner 2015-06-09 249 /* Shared between remapped and posted mode*/
2ae21010694e56 Suresh Siddha 2008-07-10 250 struct {
3bf17472226b00 Thomas Gleixner 2015-06-09 251 __u64 sid : 16, /* 64 - 79 */
3bf17472226b00 Thomas Gleixner 2015-06-09 252 sq : 2, /* 80 - 81 */
3bf17472226b00 Thomas Gleixner 2015-06-09 253 svt : 2, /* 82 - 83 */
3bf17472226b00 Thomas Gleixner 2015-06-09 254 __res3 : 44; /* 84 - 127 */
3bf17472226b00 Thomas Gleixner 2015-06-09 255 };
3bf17472226b00 Thomas Gleixner 2015-06-09 256
3bf17472226b00 Thomas Gleixner 2015-06-09 257 /* Posted mode*/
3bf17472226b00 Thomas Gleixner 2015-06-09 258 struct {
3bf17472226b00 Thomas Gleixner 2015-06-09 259 __u64 p_sid : 16, /* 64 - 79 */
3bf17472226b00 Thomas Gleixner 2015-06-09 260 p_sq : 2, /* 80 - 81 */
3bf17472226b00 Thomas Gleixner 2015-06-09 261 p_svt : 2, /* 82 - 83 */
3bf17472226b00 Thomas Gleixner 2015-06-09 262 p_res3 : 12, /* 84 - 95 */
3bf17472226b00 Thomas Gleixner 2015-06-09 263 pda_h : 32; /* 96 - 127 */
2ae21010694e56 Suresh Siddha 2008-07-10 264 };
2ae21010694e56 Suresh Siddha 2008-07-10 265 __u64 high;
2ae21010694e56 Suresh Siddha 2008-07-10 266 };
2ae21010694e56 Suresh Siddha 2008-07-10 267 };
b1fe7f2cda2a00 Peter Zijlstra 2023-05-31 268 #ifdef CONFIG_IRQ_REMAP
b1fe7f2cda2a00 Peter Zijlstra 2023-05-31 @269 __u128 irte;
b1fe7f2cda2a00 Peter Zijlstra 2023-05-31 270 #endif
b1fe7f2cda2a00 Peter Zijlstra 2023-05-31 271 };
b1fe7f2cda2a00 Peter Zijlstra 2023-05-31 272 };
423f085952fd72 Thomas Gleixner 2010-10-10 273
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply
* Re: [PATCH v0 15/15] mshv: Populate mmio mappings for PCI passthru
From: kernel test robot @ 2026-01-20 19:52 UTC (permalink / raw)
To: Mukesh R, linux-kernel, linux-hyperv, linux-arm-kernel, iommu,
linux-pci, linux-arch
Cc: oe-kbuild-all, kys, haiyangz, wei.liu, decui, longli,
catalin.marinas, will, tglx, mingo, bp, dave.hansen, hpa, joro,
lpieralisi, kwilczynski, mani, robh, bhelgaas, arnd, nunodasneves,
mhklinux, romank
In-Reply-To: <20260120064230.3602565-16-mrathor@linux.microsoft.com>
Hi Mukesh,
kernel test robot noticed the following build warnings:
[auto build test WARNING on tip/x86/core]
[also build test WARNING on pci/next pci/for-linus arm64/for-next/core soc/for-next linus/master v6.19-rc6]
[cannot apply to clk/clk-next arnd-asm-generic/master next-20260119]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Mukesh-R/iommu-hyperv-rename-hyperv-iommu-c-to-hyperv-irq-c/20260120-145832
base: tip/x86/core
patch link: https://lore.kernel.org/r/20260120064230.3602565-16-mrathor%40linux.microsoft.com
patch subject: [PATCH v0 15/15] mshv: Populate mmio mappings for PCI passthru
config: x86_64-randconfig-003-20260120 (https://download.01.org/0day-ci/archive/20260121/202601210255.2ZZOLtMV-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260121/202601210255.2ZZOLtMV-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202601210255.2ZZOLtMV-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> drivers/hv/mshv_root_main.c:60:19: warning: 'setup_hv_full_mmio' defined but not used [-Wunused-function]
60 | static int __init setup_hv_full_mmio(char *str)
| ^~~~~~~~~~~~~~~~~~
vim +/setup_hv_full_mmio +60 drivers/hv/mshv_root_main.c
58
59 bool hv_nofull_mmio; /* don't map entire mmio region upon fault */
> 60 static int __init setup_hv_full_mmio(char *str)
61 {
62 hv_nofull_mmio = true;
63 return 0;
64 }
65 __setup("hv_nofull_mmio", setup_hv_full_mmio);
66
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply
* Re: [PATCH v0 01/15] iommu/hyperv: rename hyperv-iommu.c to hyperv-irq.c
From: kernel test robot @ 2026-01-20 21:09 UTC (permalink / raw)
To: Mukesh R, linux-kernel, linux-hyperv, linux-arm-kernel, iommu,
linux-pci, linux-arch
Cc: oe-kbuild-all, kys, haiyangz, wei.liu, decui, longli,
catalin.marinas, will, tglx, mingo, bp, dave.hansen, hpa, joro,
lpieralisi, kwilczynski, mani, robh, bhelgaas, arnd, nunodasneves,
mhklinux, romank
In-Reply-To: <20260120064230.3602565-2-mrathor@linux.microsoft.com>
Hi Mukesh,
kernel test robot noticed the following build warnings:
[auto build test WARNING on tip/x86/core]
[also build test WARNING on pci/next pci/for-linus arm64/for-next/core clk/clk-next soc/for-next linus/master arnd-asm-generic/master v6.19-rc6 next-20260120]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Mukesh-R/iommu-hyperv-rename-hyperv-iommu-c-to-hyperv-irq-c/20260120-145832
base: tip/x86/core
patch link: https://lore.kernel.org/r/20260120064230.3602565-2-mrathor%40linux.microsoft.com
patch subject: [PATCH v0 01/15] iommu/hyperv: rename hyperv-iommu.c to hyperv-irq.c
config: i386-allmodconfig (https://download.01.org/0day-ci/archive/20260121/202601210423.wwOrf2K8-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260121/202601210423.wwOrf2K8-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202601210423.wwOrf2K8-lkp@intel.com/
All warnings (new ones prefixed by >>):
In file included from drivers/iommu/intel/irq_remapping.c:6:
include/linux/dmar.h:269:17: error: unknown type name '__u128'; did you mean '__u32'?
269 | __u128 irte;
| ^~~~~~
| __u32
drivers/iommu/intel/irq_remapping.c: In function 'modify_irte':
drivers/iommu/intel/irq_remapping.c:181:17: error: unknown type name 'u128'
181 | u128 old = irte->irte;
| ^~~~
In file included from arch/x86/include/asm/bug.h:193,
from arch/x86/include/asm/alternative.h:9,
from arch/x86/include/asm/barrier.h:5,
from include/asm-generic/bitops/generic-non-atomic.h:7,
from include/linux/bitops.h:28,
from include/linux/kernel.h:23,
from include/linux/interrupt.h:6,
from drivers/iommu/intel/irq_remapping.c:5:
include/linux/atomic/atomic-arch-fallback.h:326:14: error: void value not ignored as it ought to be
326 | ___r = raw_cmpxchg128((_ptr), ___o, (_new)); \
| ^
include/asm-generic/bug.h:110:32: note: in definition of macro 'WARN_ON'
110 | int __ret_warn_on = !!(condition); \
| ^~~~~~~~~
include/linux/atomic/atomic-instrumented.h:4956:9: note: in expansion of macro 'raw_try_cmpxchg128'
4956 | raw_try_cmpxchg128(__ai_ptr, __ai_oldp, __VA_ARGS__); \
| ^~~~~~~~~~~~~~~~~~
drivers/iommu/intel/irq_remapping.c:182:26: note: in expansion of macro 'try_cmpxchg128'
182 | WARN_ON(!try_cmpxchg128(&irte->irte, &old, irte_modified->irte));
| ^~~~~~~~~~~~~~
drivers/iommu/intel/irq_remapping.c: In function 'intel_ir_set_vcpu_affinity':
>> drivers/iommu/intel/irq_remapping.c:1270:40: warning: left shift count >= width of type [-Wshift-count-overflow]
1270 | ~(-1UL << PDA_HIGH_BIT);
| ^~
Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for IRQ_REMAP
Depends on [n]: IOMMU_SUPPORT [=y] && X86_64 [=n] && X86_IO_APIC [=y] && PCI_MSI [=y] && ACPI [=y]
Selected by [y]:
- HYPERV_IOMMU [=y] && IOMMU_SUPPORT [=y] && HYPERV [=y] && X86 [=y]
vim +1270 drivers/iommu/intel/irq_remapping.c
b106ee63abccbba drivers/iommu/intel_irq_remapping.c Jiang Liu 2015-04-13 1241
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1242 static int intel_ir_set_vcpu_affinity(struct irq_data *data, void *info)
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1243 {
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1244 struct intel_ir_data *ir_data = data->chip_data;
53527ea1b70224d drivers/iommu/intel/irq_remapping.c Sean Christopherson 2025-06-11 1245 struct intel_iommu_pi_data *pi_data = info;
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1246
ed1e48ea4370300 drivers/iommu/intel/irq_remapping.c Jacob Pan 2024-04-23 1247 /* stop posting interrupts, back to the default mode */
53527ea1b70224d drivers/iommu/intel/irq_remapping.c Sean Christopherson 2025-06-11 1248 if (!pi_data) {
2454823e97a63d8 drivers/iommu/intel/irq_remapping.c Sean Christopherson 2025-03-19 1249 __intel_ir_reconfigure_irte(data, true);
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1250 } else {
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1251 struct irte irte_pi;
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1252
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1253 /*
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1254 * We are not caching the posted interrupt entry. We
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1255 * copy the data from the remapped entry and modify
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1256 * the fields which are relevant for posted mode. The
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1257 * cached remapped entry is used for switching back to
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1258 * remapped mode.
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1259 */
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1260 memset(&irte_pi, 0, sizeof(irte_pi));
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1261 dmar_copy_shared_irte(&irte_pi, &ir_data->irte_entry);
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1262
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1263 /* Update the posted mode fields */
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1264 irte_pi.p_pst = 1;
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1265 irte_pi.p_urgent = 0;
53527ea1b70224d drivers/iommu/intel/irq_remapping.c Sean Christopherson 2025-06-11 1266 irte_pi.p_vector = pi_data->vector;
53527ea1b70224d drivers/iommu/intel/irq_remapping.c Sean Christopherson 2025-06-11 1267 irte_pi.pda_l = (pi_data->pi_desc_addr >>
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1268 (32 - PDA_LOW_BIT)) & ~(-1UL << PDA_LOW_BIT);
53527ea1b70224d drivers/iommu/intel/irq_remapping.c Sean Christopherson 2025-06-11 1269 irte_pi.pda_h = (pi_data->pi_desc_addr >> 32) &
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 @1270 ~(-1UL << PDA_HIGH_BIT);
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1271
688124cc541f60d drivers/iommu/intel/irq_remapping.c Sean Christopherson 2025-03-19 1272 ir_data->irq_2_iommu.posted_vcpu = true;
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1273 modify_irte(&ir_data->irq_2_iommu, &irte_pi);
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1274 }
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1275
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1276 return 0;
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1277 }
8541186faf3b596 drivers/iommu/intel_irq_remapping.c Feng Wu 2015-06-09 1278
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply
* Re: [PATCH v0 00/15] PCI passthru on Hyper-V (Part I)
From: Jacob Pan @ 2026-01-20 21:50 UTC (permalink / raw)
To: Mukesh R
Cc: linux-kernel, linux-hyperv, linux-arm-kernel, iommu, linux-pci,
linux-arch, kys, haiyangz, wei.liu, decui, longli,
catalin.marinas, will, tglx, mingo, bp, dave.hansen, hpa, joro,
lpieralisi, kwilczynski, mani, robh, bhelgaas, arnd, nunodasneves,
mhklinux
In-Reply-To: <20260120064230.3602565-1-mrathor@linux.microsoft.com>
Hi Mukesh,
On Mon, 19 Jan 2026 22:42:15 -0800
Mukesh R <mrathor@linux.microsoft.com> wrote:
> From: Mukesh Rathor <mrathor@linux.microsoft.com>
>
> 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.
>
> There are 3 cases where Linux can run as a privileged VM (aka MSHV):
> Baremetal root (meaning Hyper-V+Linux), L1VH, and Nested.
>
I think some introduction/background to L1VH would help.
> 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.
It may be clearer to state that the hypervisor supports Linux IOMMU
paging domains through map/unmap hypercalls, mapping GPAs to HPAs using
stage‑2 I/O page tables.
> 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 in VFIO type1 subsystem.
>
This may warrant introducing a new IOMMU domain feature flag, as it
performs mappings but does not support map/unmap semantics in the same
way as a paging domain.
> While this Part I focuses on memory mappings, upcoming Part II
> will focus on irq bypass along with some minor irq remapping
> updates.
>
> This patch series was tested using Cloud Hypervisor verion 48. Qemu
> support of MSHV is in the works, and that will be extended to include
> PCI passthru and SR-IOV support also in near future.
>
> Based on: 8f0b4cce4481 (origin/hyperv-next)
>
> Thanks,
> -Mukesh
>
> Mukesh Rathor (15):
> iommu/hyperv: rename hyperv-iommu.c to hyperv-irq.c
> x86/hyperv: cosmetic changes in irqdomain.c for readability
> x86/hyperv: add insufficient memory support in irqdomain.c
> 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
> PCI: hv: rename hv_compose_msi_msg to hv_vmbus_compose_msi_msg
> mshv: Import data structs around device domains and irq remapping
> PCI: hv: Build device id for a VMBus device
> x86/hyperv: Build logical device ids for PCI passthru hcalls
> x86/hyperv: Implement hyperv virtual iommu
> x86/hyperv: Basic interrupt support for direct attached devices
> mshv: Remove mapping of mmio space during map user ioctl
> mshv: Populate mmio mappings for PCI passthru
>
> MAINTAINERS | 1 +
> arch/arm64/include/asm/mshyperv.h | 15 +
> arch/x86/hyperv/irqdomain.c | 314 ++++++---
> arch/x86/include/asm/mshyperv.h | 21 +
> arch/x86/kernel/pci-dma.c | 2 +
> drivers/hv/Makefile | 3 +-
> drivers/hv/mshv_root.h | 24 +
> drivers/hv/mshv_root_main.c | 296 +++++++-
> drivers/hv/mshv_vfio.c | 210 ++++++
> drivers/iommu/Kconfig | 1 +
> drivers/iommu/Makefile | 2 +-
> drivers/iommu/hyperv-iommu.c | 1004
> +++++++++++++++++++++------ drivers/iommu/hyperv-irq.c |
> 330 +++++++++ drivers/pci/controller/pci-hyperv.c | 207 ++++--
> include/asm-generic/mshyperv.h | 1 +
> 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, 2182 insertions(+), 409 deletions(-)
> create mode 100644 drivers/hv/mshv_vfio.c
> create mode 100644 drivers/iommu/hyperv-irq.c
>
^ permalink raw reply
* Re: [PATCH v0 09/15] mshv: Import data structs around device domains and irq remapping
From: Stanislav Kinsburskii @ 2026-01-20 22:17 UTC (permalink / raw)
To: Mukesh R
Cc: linux-kernel, linux-hyperv, linux-arm-kernel, iommu, linux-pci,
linux-arch, kys, haiyangz, wei.liu, decui, longli,
catalin.marinas, will, tglx, mingo, bp, dave.hansen, hpa, joro,
lpieralisi, kwilczynski, mani, robh, bhelgaas, arnd, nunodasneves,
mhklinux, romank
In-Reply-To: <20260120064230.3602565-10-mrathor@linux.microsoft.com>
On Mon, Jan 19, 2026 at 10:42:24PM -0800, Mukesh R wrote:
> From: Mukesh Rathor <mrathor@linux.microsoft.com>
>
> Import/copy from Hyper-V public headers, definitions and declarations that
> are related to attaching and detaching of device domains and interrupt
> remapping, and building device ids for those purposes.
>
> Signed-off-by: Mukesh Rathor <mrathor@linux.microsoft.com>
> ---
> include/hyperv/hvgdk_mini.h | 11 ++++
> include/hyperv/hvhdk_mini.h | 112 ++++++++++++++++++++++++++++++++++++
> 2 files changed, 123 insertions(+)
>
<snip>
> +/* ID for stage 2 default domain and NULL domain */
> +#define HV_DEVICE_DOMAIN_ID_S2_DEFAULT 0
> +#define HV_DEVICE_DOMAIN_ID_S2_NULL 0xFFFFFFFFULL
> +
> +union hv_device_domain_id {
> + u64 as_uint64;
> + struct {
> + u32 type : 4;
> + u32 reserved : 28;
> + u32 id;
> + };
> +} __packed;
Shouldn't the inner struct be packed instead?
> +
> +struct hv_input_device_domain { /* HV_INPUT_DEVICE_DOMAIN */
> + u64 partition_id;
> + union hv_input_vtl owner_vtl;
> + u8 padding[7];
> + union hv_device_domain_id domain_id;
> +} __packed;
> +
> +union hv_create_device_domain_flags { /* HV_CREATE_DEVICE_DOMAIN_FLAGS */
> + u32 as_uint32;
> + struct {
> + u32 forward_progress_required : 1;
> + u32 inherit_owning_vtl : 1;
> + u32 reserved : 30;
> + } __packed;
> +} __packed;
Why should the union be packed?
Thanks,
Stanislav
> +
> +struct hv_input_create_device_domain { /* HV_INPUT_CREATE_DEVICE_DOMAIN */
> + struct hv_input_device_domain device_domain;
> + union hv_create_device_domain_flags create_device_domain_flags;
> +} __packed;
> +
> +struct hv_input_delete_device_domain { /* HV_INPUT_DELETE_DEVICE_DOMAIN */
> + struct hv_input_device_domain device_domain;
> +} __packed;
> +
> +struct hv_input_attach_device_domain { /* HV_INPUT_ATTACH_DEVICE_DOMAIN */
> + struct hv_input_device_domain device_domain;
> + union hv_device_id device_id;
> +} __packed;
> +
> +struct hv_input_detach_device_domain { /* HV_INPUT_DETACH_DEVICE_DOMAIN */
> + u64 partition_id;
> + union hv_device_id device_id;
> +} __packed;
> +
> +struct hv_input_map_device_gpa_pages { /* HV_INPUT_MAP_DEVICE_GPA_PAGES */
> + struct hv_input_device_domain device_domain;
> + union hv_input_vtl target_vtl;
> + u8 padding[3];
> + u32 map_flags;
> + u64 target_device_va_base;
> + u64 gpa_page_list[];
> +} __packed;
> +
> +struct hv_input_unmap_device_gpa_pages { /* HV_INPUT_UNMAP_DEVICE_GPA_PAGES */
> + struct hv_input_device_domain device_domain;
> + u64 target_device_va_base;
> +} __packed;
> +
> #endif /* _HV_HVHDK_MINI_H */
> --
> 2.51.2.vfs.0.1
>
^ permalink raw reply
* Re: [PATCH v0 10/15] PCI: hv: Build device id for a VMBus device
From: Stanislav Kinsburskii @ 2026-01-20 22:22 UTC (permalink / raw)
To: Mukesh R
Cc: linux-kernel, linux-hyperv, linux-arm-kernel, iommu, linux-pci,
linux-arch, kys, haiyangz, wei.liu, decui, longli,
catalin.marinas, will, tglx, mingo, bp, dave.hansen, hpa, joro,
lpieralisi, kwilczynski, mani, robh, bhelgaas, arnd, nunodasneves,
mhklinux, romank
In-Reply-To: <20260120064230.3602565-11-mrathor@linux.microsoft.com>
On Mon, Jan 19, 2026 at 10:42:25PM -0800, Mukesh R wrote:
> From: Mukesh Rathor <mrathor@linux.microsoft.com>
>
> 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. Before building it, a check is done to make sure
> the device is a valid VMBus device.
>
> Signed-off-by: Mukesh Rathor <mrathor@linux.microsoft.com>
> ---
> arch/x86/include/asm/mshyperv.h | 2 ++
> drivers/pci/controller/pci-hyperv.c | 29 +++++++++++++++++++++++++++++
> 2 files changed, 31 insertions(+)
>
> diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h
> index eef4c3a5ba28..0d7fdfb25e76 100644
> --- a/arch/x86/include/asm/mshyperv.h
> +++ b/arch/x86/include/asm/mshyperv.h
> @@ -188,6 +188,8 @@ bool hv_vcpu_is_preempted(int vcpu);
> static inline void hv_apic_init(void) {}
> #endif
>
> +u64 hv_pci_vmbus_device_id(struct pci_dev *pdev);
> +
> struct irq_domain *hv_create_pci_msi_domain(void);
>
> int hv_map_msi_interrupt(struct irq_data *data,
> diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c
> index 8bc6a38c9b5a..40f0b06bb966 100644
> --- a/drivers/pci/controller/pci-hyperv.c
> +++ b/drivers/pci/controller/pci-hyperv.c
> @@ -579,6 +579,8 @@ static void hv_pci_onchannelcallback(void *context);
> #define DELIVERY_MODE APIC_DELIVERY_MODE_FIXED
> #define HV_MSI_CHIP_FLAGS MSI_CHIP_FLAG_SET_ACK
>
> +static bool hv_vmbus_pci_device(struct pci_bus *pbus);
> +
Why not moving this static function definition above the called instead of
defining the prototype?
> static int hv_pci_irqchip_init(void)
> {
> return 0;
> @@ -598,6 +600,26 @@ static unsigned int hv_msi_get_int_vector(struct irq_data *data)
>
> #define hv_msi_prepare pci_msi_prepare
>
> +u64 hv_pci_vmbus_device_id(struct pci_dev *pdev)
> +{
> + u64 u64val;
This variable is redundant.
> + 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);
> + u64val = (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);
> +
It looks like this value always fits into 32 bit, so what is the value
in returning 64 bit?
Thanks,
Stanislav
> + return u64val;
> +}
> +EXPORT_SYMBOL_GPL(hv_pci_vmbus_device_id);
> +
> /**
> * hv_irq_retarget_interrupt() - "Unmask" the IRQ by setting its current
> * affinity.
> @@ -1404,6 +1426,13 @@ static struct pci_ops hv_pcifront_ops = {
> .write = hv_pcifront_write_config,
> };
>
> +#ifdef CONFIG_X86
> +static bool hv_vmbus_pci_device(struct pci_bus *pbus)
> +{
> + return pbus->ops == &hv_pcifront_ops;
> +}
> +#endif /* CONFIG_X86 */
> +
> /*
> * Paravirtual backchannel
> *
> --
> 2.51.2.vfs.0.1
>
^ permalink raw reply
* Re: [PATCH v0 11/15] x86/hyperv: Build logical device ids for PCI passthru hcalls
From: Stanislav Kinsburskii @ 2026-01-20 22:27 UTC (permalink / raw)
To: Mukesh R
Cc: linux-kernel, linux-hyperv, linux-arm-kernel, iommu, linux-pci,
linux-arch, kys, haiyangz, wei.liu, decui, longli,
catalin.marinas, will, tglx, mingo, bp, dave.hansen, hpa, joro,
lpieralisi, kwilczynski, mani, robh, bhelgaas, arnd, nunodasneves,
mhklinux, romank
In-Reply-To: <20260120064230.3602565-12-mrathor@linux.microsoft.com>
On Mon, Jan 19, 2026 at 10:42:26PM -0800, Mukesh R wrote:
> From: Mukesh Rathor <mrathor@linux.microsoft.com>
>
> On Hyper-V, most hypercalls related to PCI passthru to map/unmap regions,
> interrupts, etc need a device id as a parameter. A device id refers
> to a specific device. A device id is of two types:
> o Logical: used for direct attach (see below) hypercalls. A logical
> device id is a unique 62bit value that is created and
> sent during the initial device attach. Then all further
> communications (for interrupt remaps etc) must use this
> logical id.
> o PCI: used for device domain hypercalls such as map, unmap, etc.
> This is built using actual device BDF info.
>
> PS: Since an L1VH only supports direct attaches, a logical device id
> on an L1VH VM is always a VMBus device id. For non-L1VH cases,
> we just use PCI BDF info, altho not strictly needed, to build the
> logical device id.
>
> At a high level, Hyper-V supports two ways to do PCI passthru:
> 1. Device Domain: root must create a device domain in the hypervisor,
> and do map/unmap hypercalls for mapping and unmapping guest RAM.
> 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 host need not do map/unmap
> hypercalls. A direct attached device must be referenced
> via logical device id and never via the PCI device id. For an
> L1VH root/parent, Hyper-V only supports direct attaches.
>
> Signed-off-by: Mukesh Rathor <mrathor@linux.microsoft.com>
> ---
> arch/x86/hyperv/irqdomain.c | 60 ++++++++++++++++++++++++++++++---
> arch/x86/include/asm/mshyperv.h | 14 ++++++++
> 2 files changed, 70 insertions(+), 4 deletions(-)
>
> diff --git a/arch/x86/hyperv/irqdomain.c b/arch/x86/hyperv/irqdomain.c
> index ccbe5848a28f..33017aa0caa4 100644
> --- a/arch/x86/hyperv/irqdomain.c
> +++ b/arch/x86/hyperv/irqdomain.c
> @@ -137,7 +137,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)
> +static u64 hv_build_devid_type_pci(struct pci_dev *pdev)
> {
> int pos;
> union hv_device_id hv_devid;
> @@ -197,7 +197,58 @@ static union hv_device_id hv_build_devid_type_pci(struct pci_dev *pdev)
> }
>
> out:
> - return hv_devid;
> + return hv_devid.as_uint64;
> +}
> +
> +/* Build device id for direct attached devices */
> +static u64 hv_build_devid_type_logical(struct pci_dev *pdev)
> +{
> + hv_pci_segment segment;
> + union hv_device_id hv_devid;
> + union hv_pci_bdf bdf = {.as_uint16 = 0};
> + struct rid_data data = {
> + .bridge = NULL,
> + .rid = PCI_DEVID(pdev->bus->number, pdev->devfn)
> + };
> +
> + segment = pci_domain_nr(pdev->bus);
> + bdf.bus = PCI_BUS_NUM(data.rid);
> + bdf.device = PCI_SLOT(data.rid);
> + bdf.function = PCI_FUNC(data.rid);
> +
> + hv_devid.as_uint64 = 0;
> + hv_devid.device_type = HV_DEVICE_TYPE_LOGICAL;
> + hv_devid.logical.id = (u64)segment << 16 | bdf.as_uint16;
> +
> + return hv_devid.as_uint64;
> +}
> +
> +/* Build device id after the device has been attached */
> +u64 hv_build_devid_oftype(struct pci_dev *pdev, enum hv_device_type type)
> +{
> + if (type == HV_DEVICE_TYPE_LOGICAL) {
> + if (hv_l1vh_partition())
> + return hv_pci_vmbus_device_id(pdev);
Should this one be renamed into hv_build_devid_type_vmbus() to align
with the other two function names?
Thanks,
Stanislav
> + else
> + return hv_build_devid_type_logical(pdev);
> + } else if (type == HV_DEVICE_TYPE_PCI)
> + return hv_build_devid_type_pci(pdev);
> +
> + return 0;
> +}
> +EXPORT_SYMBOL_GPL(hv_build_devid_oftype);
> +
> +/* Build device id for the interrupt path */
> +static u64 hv_build_irq_devid(struct pci_dev *pdev)
> +{
> + enum hv_device_type dev_type;
> +
> + if (hv_pcidev_is_attached_dev(pdev) || hv_l1vh_partition())
> + dev_type = HV_DEVICE_TYPE_LOGICAL;
> + else
> + dev_type = HV_DEVICE_TYPE_PCI;
> +
> + return hv_build_devid_oftype(pdev, dev_type);
> }
>
> /*
> @@ -221,7 +272,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_irq_devid(pdev);
> cpu = cpumask_first(irq_data_get_effective_affinity_mask(data));
>
> return hv_map_interrupt(hv_current_partition_id, hv_devid, false, cpu,
> @@ -296,7 +347,8 @@ 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_irq_devid(pdev);
> +
> return hv_unmap_interrupt(hv_devid.as_uint64, irq_entry);
> }
>
> diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h
> index 0d7fdfb25e76..97477c5a8487 100644
> --- a/arch/x86/include/asm/mshyperv.h
> +++ b/arch/x86/include/asm/mshyperv.h
> @@ -188,6 +188,20 @@ bool hv_vcpu_is_preempted(int vcpu);
> static inline void hv_apic_init(void) {}
> #endif
>
> +#if IS_ENABLED(CONFIG_HYPERV_IOMMU)
> +static inline bool hv_pcidev_is_attached_dev(struct pci_dev *pdev)
> +{ return false; } /* temporary */
> +u64 hv_build_devid_oftype(struct pci_dev *pdev, enum hv_device_type type);
> +#else /* CONFIG_HYPERV_IOMMU */
> +static inline bool hv_pcidev_is_attached_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; }
> +
> +#endif /* CONFIG_HYPERV_IOMMU */
> +
> u64 hv_pci_vmbus_device_id(struct pci_dev *pdev);
>
> struct irq_domain *hv_create_pci_msi_domain(void);
> --
> 2.51.2.vfs.0.1
>
^ permalink raw reply
* Re: [PATCH v0 12/15] x86/hyperv: Implement hyperv virtual iommu
From: Stanislav Kinsburskii @ 2026-01-21 0:12 UTC (permalink / raw)
To: Mukesh R
Cc: linux-kernel, linux-hyperv, linux-arm-kernel, iommu, linux-pci,
linux-arch, kys, haiyangz, wei.liu, decui, longli,
catalin.marinas, will, tglx, mingo, bp, dave.hansen, hpa, joro,
lpieralisi, kwilczynski, mani, robh, bhelgaas, arnd, nunodasneves,
mhklinux, romank
In-Reply-To: <20260120064230.3602565-13-mrathor@linux.microsoft.com>
On Mon, Jan 19, 2026 at 10:42:27PM -0800, Mukesh R wrote:
> From: Mukesh Rathor <mrathor@linux.microsoft.com>
>
> 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 root or L1VH
> parent. This also implements direct attach mechanism 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: root must create a device domain in the hypervisor,
> and do map/unmap hypercalls for mapping and unmapping guest RAM.
> 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 host need not do map/unmap
> device memory hypercalls. As such, direct attach passthru setup
> during guest boot is extremely fast. A direct attached device
> must be referenced via logical device id and not via the PCI
> device id.
>
> At present, L1VH root/parent only supports direct attaches. Also direct
> attach is default in non-L1VH cases because there are some significant
> performance issues with device domain implementation currently for guests
> with higher RAM (say more than 8GB), and that unfortunately cannot be
> addressed in the short term.
>
<snip>
> +/*
> + * If the current thread is a VMM thread, return the partition id of the VM it
> + * is managing, else return HV_PARTITION_ID_INVALID.
> + */
> +u64 hv_iommu_get_curr_partid(void)
> +{
> + u64 (*fn)(pid_t pid);
> + u64 partid;
> +
> + fn = symbol_get(mshv_pid_to_partid);
> + if (!fn)
> + return HV_PARTITION_ID_INVALID;
> +
> + partid = fn(current->tgid);
> + symbol_put(mshv_pid_to_partid);
> +
> + return partid;
> +}
> +
> +/* If this is a VMM thread, then this domain is for a guest VM */
> +static bool hv_curr_thread_is_vmm(void)
> +{
> + return hv_iommu_get_curr_partid() != HV_PARTITION_ID_INVALID;
> +}
> +
> +static bool hv_iommu_capable(struct device *dev, enum iommu_cap cap)
> +{
> + switch (cap) {
> + case IOMMU_CAP_CACHE_COHERENCY:
> + return true;
> + default:
> + return false;
> + }
> + return false;
The return above is never reached.
> +}
> +
> +/*
> + * Check if given pci device is a direct attached device. Caller must have
> + * verified pdev is a valid pci device.
> + */
> +bool hv_pcidev_is_attached_dev(struct pci_dev *pdev)
> +{
> + struct iommu_domain *iommu_domain;
> + struct hv_domain *hvdom;
> + struct device *dev = &pdev->dev;
> +
> + iommu_domain = iommu_get_domain_for_dev(dev);
> + if (iommu_domain) {
> + hvdom = to_hv_domain(iommu_domain);
hvdom varaible is redundant.
> + return hvdom->attached_dom;
> + }
> +
> + return false;
> +}
> +EXPORT_SYMBOL_GPL(hv_pcidev_is_attached_dev);
> +
> +/* Create a new device domain in the hypervisor */
> +static int hv_iommu_create_hyp_devdom(struct hv_domain *hvdom)
> +{
> + u64 status;
> + unsigned long flags;
> + struct hv_input_device_domain *ddp;
> + struct hv_input_create_device_domain *input;
> +
> + local_irq_save(flags);
> + input = *this_cpu_ptr(hyperv_pcpu_input_arg);
> + memset(input, 0, sizeof(*input));
> +
> + ddp = &input->device_domain;
> + ddp->partition_id = HV_PARTITION_ID_SELF;
> + ddp->domain_id.type = HV_DEVICE_DOMAIN_TYPE_S2;
> + ddp->domain_id.id = hvdom->domid_num;
> +
> + input->create_device_domain_flags.forward_progress_required = 1;
> + input->create_device_domain_flags.inherit_owning_vtl = 0;
> +
> + status = hv_do_hypercall(HVCALL_CREATE_DEVICE_DOMAIN, input, NULL);
> +
> + local_irq_restore(flags);
> +
> + if (!hv_result_success(status))
> + hv_status_err(status, "\n");
> +
> + return hv_result_to_errno(status);
> +}
> +
> +/* During boot, all devices are attached to this */
> +static struct iommu_domain *hv_iommu_domain_alloc_identity(struct device *dev)
> +{
> + return &hv_def_identity_dom.iommu_dom;
> +}
> +
> +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() && !hv_no_attdev) {
> + 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)
> + goto out;
Why goto here and not return NULL like above?
> +
> + spin_lock_init(&hvdom->mappings_lock);
> + hvdom->mappings_tree = RB_ROOT_CACHED;
> +
> + if (++unique_id == HV_DEVICE_DOMAIN_ID_S2_DEFAULT) /* ie, 0 */
> + goto out_free;
> +
> + hvdom->domid_num = unique_id;
> + 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_curr_thread_is_vmm() && !hv_no_attdev)
> + hvdom->attached_dom = true;
> + else {
> + rc = hv_iommu_create_hyp_devdom(hvdom);
> + if (rc)
> + goto out_free_id;
> + }
> +
> + return &hvdom->iommu_dom;
> +
> +out_free_id:
> + unique_id--;
> +out_free:
> + kfree(hvdom);
> +out:
> + return NULL;
> +}
> +
> +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 (hvdom->num_attchd) {
> + pr_err("Hyper-V: can't free busy iommu domain (%p)\n", immdom);
> + return;
> + }
> +
> + if (!hv_curr_thread_is_vmm() || hv_no_attdev) {
> + struct hv_input_device_domain *ddp;
> +
> + local_irq_save(flags);
> + input = *this_cpu_ptr(hyperv_pcpu_input_arg);
> + ddp = &input->device_domain;
> + memset(input, 0, sizeof(*input));
> +
> + ddp->partition_id = HV_PARTITION_ID_SELF;
> + ddp->domain_id.type = HV_DEVICE_DOMAIN_TYPE_S2;
> + ddp->domain_id.id = hvdom->domid_num;
> +
> + status = hv_do_hypercall(HVCALL_DELETE_DEVICE_DOMAIN, input,
> + NULL);
> + local_irq_restore(flags);
> +
> + if (!hv_result_success(status))
> + hv_status_err(status, "\n");
> + }
> +
> + kfree(hvdom);
> +}
> +
> +/* Attach a device to a domain previously created in the hypervisor */
> +static int hv_iommu_att_dev2dom(struct hv_domain *hvdom, struct pci_dev *pdev)
> +{
> + unsigned long flags;
> + u64 status;
> + enum hv_device_type dev_type;
> + struct hv_input_attach_device_domain *input;
> +
> + 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;
> +
> + /* NB: Upon guest shutdown, device is re-attached to the default domain
> + * without explicit detach.
> + */
> + if (hv_l1vh_partition())
> + dev_type = HV_DEVICE_TYPE_LOGICAL;
> + else
> + dev_type = HV_DEVICE_TYPE_PCI;
> +
> + input->device_id.as_uint64 = hv_build_devid_oftype(pdev, dev_type);
> +
> + status = hv_do_hypercall(HVCALL_ATTACH_DEVICE_DOMAIN, input, NULL);
> + local_irq_restore(flags);
> +
> + if (!hv_result_success(status))
> + hv_status_err(status, "\n");
> +
> + return hv_result_to_errno(status);
> +}
> +
> +/* Caller must have validated that dev is a valid pci dev */
> +static int hv_iommu_direct_attach_device(struct pci_dev *pdev)
> +{
> + struct hv_input_attach_device *input;
> + u64 status;
> + int rc;
> + unsigned long flags;
> + union hv_device_id host_devid;
> + enum hv_device_type dev_type;
> + u64 ptid = hv_iommu_get_curr_partid();
> +
> + if (ptid == HV_PARTITION_ID_INVALID) {
> + pr_err("Hyper-V: Invalid partition id in direct attach\n");
> + return -EINVAL;
> + }
> +
> + if (hv_l1vh_partition())
> + dev_type = HV_DEVICE_TYPE_LOGICAL;
> + else
> + dev_type = HV_DEVICE_TYPE_PCI;
> +
> + host_devid.as_uint64 = hv_build_devid_oftype(pdev, dev_type);
> +
> + do {
> + local_irq_save(flags);
> + input = *this_cpu_ptr(hyperv_pcpu_input_arg);
> + memset(input, 0, sizeof(*input));
> + input->partition_id = ptid;
> + input->device_id = host_devid;
> +
> + /* Hypervisor associates logical_id with this device, and in
> + * some hypercalls like retarget interrupts, logical_id must be
> + * used instead of the BDF. It is a required parameter.
> + */
> + input->attdev_flags.logical_id = 1;
> + input->logical_devid =
> + hv_build_devid_oftype(pdev, HV_DEVICE_TYPE_LOGICAL);
> +
> + status = hv_do_hypercall(HVCALL_ATTACH_DEVICE, input, NULL);
> + local_irq_restore(flags);
> +
> + if (hv_result(status) == HV_STATUS_INSUFFICIENT_MEMORY) {
> + rc = hv_call_deposit_pages(NUMA_NO_NODE, ptid, 1);
> + if (rc)
> + break;
> + }
> + } while (hv_result(status) == HV_STATUS_INSUFFICIENT_MEMORY);
> +
> + if (!hv_result_success(status))
> + hv_status_err(status, "\n");
> +
> + return hv_result_to_errno(status);
> +}
> +
> +/* This to attach a device to both host app (like DPDK) and a guest VM */
> +static int hv_iommu_attach_dev(struct iommu_domain *immdom, struct device *dev,
> + struct iommu_domain *old)
> +{
> + struct pci_dev *pdev;
> + int rc;
> + struct hv_domain *hvdom_new = to_hv_domain(immdom);
> + struct hv_domain *hvdom_prev = dev_iommu_priv_get(dev);
> +
> + /* Only allow PCI devices for now */
> + if (!dev_is_pci(dev))
> + return -EINVAL;
> +
> + pdev = to_pci_dev(dev);
> +
> + /* l1vh does not support host device (eg DPDK) passthru */
> + if (hv_l1vh_partition() && !hv_special_domain(hvdom_new) &&
> + !hvdom_new->attached_dom)
> + return -EINVAL;
> +
> + /*
> + * VFIO does not do explicit detach calls, hence check first if we need
> + * to detach first. Also, in case of guest shutdown, it's the VMM
> + * thread that attaches it back to the hv_def_identity_dom, and
> + * hvdom_prev will not be null then. It is null during boot.
> + */
> + if (hvdom_prev)
> + if (!hv_l1vh_partition() || !hv_special_domain(hvdom_prev))
> + hv_iommu_detach_dev(&hvdom_prev->iommu_dom, dev);
> +
> + if (hv_l1vh_partition() && hv_special_domain(hvdom_new)) {
> + dev_iommu_priv_set(dev, hvdom_new); /* sets "private" field */
> + return 0;
> + }
> +
> + if (hvdom_new->attached_dom)
> + rc = hv_iommu_direct_attach_device(pdev);
> + else
> + rc = hv_iommu_att_dev2dom(hvdom_new, pdev);
> +
> + if (rc && hvdom_prev) {
> + int rc1;
> +
> + if (hvdom_prev->attached_dom)
> + rc1 = hv_iommu_direct_attach_device(pdev);
> + else
> + rc1 = hv_iommu_att_dev2dom(hvdom_prev, pdev);
> +
> + if (rc1)
> + pr_err("Hyper-V: iommu could not restore orig device state.. dev:%s\n",
> + dev_name(dev));
> + }
> +
> + if (rc == 0) {
> + dev_iommu_priv_set(dev, hvdom_new); /* sets "private" field */
> + hvdom_new->num_attchd++;
> + }
> +
> + return rc;
> +}
> +
> +static void hv_iommu_det_dev_from_guest(struct hv_domain *hvdom,
> + struct pci_dev *pdev)
> +{
> + struct hv_input_detach_device *input;
> + u64 status, log_devid;
> + unsigned long flags;
> +
> + log_devid = hv_build_devid_oftype(pdev, HV_DEVICE_TYPE_LOGICAL);
> +
> + local_irq_save(flags);
> + input = *this_cpu_ptr(hyperv_pcpu_input_arg);
> + memset(input, 0, sizeof(*input));
> +
> + input->partition_id = hv_iommu_get_curr_partid();
> + input->logical_devid = log_devid;
> + status = hv_do_hypercall(HVCALL_DETACH_DEVICE, input, NULL);
> + local_irq_restore(flags);
> +
> + if (!hv_result_success(status))
> + hv_status_err(status, "\n");
> +}
> +
> +static void hv_iommu_det_dev_from_dom(struct hv_domain *hvdom,
> + struct pci_dev *pdev)
> +{
> + u64 status, devid;
> + unsigned long flags;
> + struct hv_input_detach_device_domain *input;
> +
> + devid = hv_build_devid_oftype(pdev, HV_DEVICE_TYPE_PCI);
> +
> + local_irq_save(flags);
> + input = *this_cpu_ptr(hyperv_pcpu_input_arg);
> + memset(input, 0, sizeof(*input));
> +
> + input->partition_id = HV_PARTITION_ID_SELF;
> + input->device_id.as_uint64 = devid;
> + status = hv_do_hypercall(HVCALL_DETACH_DEVICE_DOMAIN, input, NULL);
> + local_irq_restore(flags);
> +
> + if (!hv_result_success(status))
> + hv_status_err(status, "\n");
> +}
> +
> +static void hv_iommu_detach_dev(struct iommu_domain *immdom, struct device *dev)
> +{
> + struct pci_dev *pdev;
> + struct hv_domain *hvdom = to_hv_domain(immdom);
> +
> + /* See the attach function, only PCI devices for now */
> + if (!dev_is_pci(dev))
> + return;
> +
> + if (hvdom->num_attchd == 0)
> + pr_warn("Hyper-V: num_attchd is zero (%s)\n", dev_name(dev));
> +
> + pdev = to_pci_dev(dev);
> +
> + if (hvdom->attached_dom) {
> + hv_iommu_det_dev_from_guest(hvdom, pdev);
> +
> + /* Do not reset attached_dom, hv_iommu_unmap_pages happens
> + * next.
> + */
> + } else {
> + hv_iommu_det_dev_from_dom(hvdom, pdev);
> + }
> +
> + hvdom->num_attchd--;
Shouldn't this be modified iff the detach succeeded?
> +}
> +
> +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);
> + if (!mapping)
> + return -ENOMEM;
> +
> + mapping->paddr = paddr;
> + mapping->iova.start = iova;
> + mapping->iova.last = iova + size - 1;
> + mapping->flags = flags;
> +
> + spin_lock_irqsave(&hvdom->mappings_lock, irqflags);
> + interval_tree_insert(&mapping->iova, &hvdom->mappings_tree);
> + spin_unlock_irqrestore(&hvdom->mappings_lock, irqflags);
> +
> + return 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;
> +
> + 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;
> +
> + unmapped += mapping->iova.last - mapping->iova.start + 1;
> +
> + interval_tree_remove(node, &hvdom->mappings_tree);
> + kfree(mapping);
> + }
> + spin_unlock_irqrestore(&hvdom->mappings_lock, flags);
> +
> + return unmapped;
> +}
> +
> +/* Return: must return exact status from the hypercall without changes */
> +static u64 hv_iommu_map_pgs(struct hv_domain *hvdom,
> + unsigned long iova, phys_addr_t paddr,
> + unsigned long npages, u32 map_flags)
> +{
> + u64 status;
> + int i;
> + struct hv_input_map_device_gpa_pages *input;
> + unsigned long flags, pfn = paddr >> 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->map_flags = map_flags;
> + input->target_device_va_base = iova;
> +
> + pfn = paddr >> HV_HYP_PAGE_SHIFT;
> + for (i = 0; i < npages; i++, pfn++)
> + input->gpa_page_list[i] = pfn;
> +
> + status = hv_do_rep_hypercall(HVCALL_MAP_DEVICE_GPA_PAGES, npages, 0,
> + input, NULL);
> +
> + local_irq_restore(flags);
> + return status;
> +}
> +
> +/*
> + * The core VFIO code loops over memory ranges calling this function with
> + * the largest size from HV_IOMMU_PGSIZES. cond_resched() is in vfio_iommu_map.
> + */
> +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 */
> + 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;
> + }
> +
> + npages = size >> HV_HYP_PAGE_SHIFT;
> + while (done < npages) {
> + ulong completed, remain = npages - done;
> +
> + status = hv_iommu_map_pgs(hvdom, iova, paddr, remain,
> + map_flags);
> +
> + completed = hv_repcomp(status);
> + done = done + completed;
> + iova = iova + (completed << HV_HYP_PAGE_SHIFT);
> + paddr = paddr + (completed << HV_HYP_PAGE_SHIFT);
> +
> + if (hv_result(status) == HV_STATUS_INSUFFICIENT_MEMORY) {
> + ret = hv_call_deposit_pages(NUMA_NO_NODE,
> + hv_current_partition_id,
> + 256);
> + if (ret)
> + break;
> + }
> + if (!hv_result_success(status))
> + break;
> + }
> +
> + if (!hv_result_success(status)) {
> + size_t done_size = done << HV_HYP_PAGE_SHIFT;
> +
> + hv_status_err(status, "pgs:%lx/%lx iova:%lx\n",
> + done, npages, iova);
> + /*
> + * lookup tree has all mappings [0 - size-1]. Below unmap will
> + * only remove from [0 - done], we need to remove second chunk
> + * [done+1 - size-1].
> + */
> + hv_iommu_del_tree_mappings(hvdom, iova, size - done_size);
> + hv_iommu_unmap_pages(immdom, iova - done_size, pgsize,
> + done, NULL);
> + if (mapped)
> + *mapped = 0;
> + } else
> + if (mapped)
> + *mapped = size;
> +
> + return hv_result_to_errno(status);
> +}
> +
> +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;
> +
> + unmapped = hv_iommu_del_tree_mappings(hvdom, iova, size);
> + if (unmapped < size)
> + pr_err("%s: could not delete all mappings (%lx:%lx/%lx)\n",
> + __func__, iova, unmapped, size);
> +
> + if (hvdom->attached_dom)
> + return size;
> +
> + 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);
> + local_irq_restore(flags);
> +
> + if (!hv_result_success(status))
> + hv_status_err(status, "\n");
> +
There is some inconsistency in namings and behaviour of paired
functions:
1. The pair of hv_iommu_unmap_pages is called hv_iommu_map_pgs
2. hv_iommu_map_pgs doesn't print status in case of error.
It would be much better to keep this code consistent.
> + return unmapped;
> +}
> +
> +static phys_addr_t hv_iommu_iova_to_phys(struct iommu_domain *immdom,
> + dma_addr_t iova)
> +{
> + u64 paddr = 0;
> + unsigned long flags;
> + struct hv_iommu_mapping *mapping;
> + struct interval_tree_node *node;
> + struct hv_domain *hvdom = to_hv_domain(immdom);
> +
> + spin_lock_irqsave(&hvdom->mappings_lock, flags);
> + node = interval_tree_iter_first(&hvdom->mappings_tree, iova, iova);
> + if (node) {
> + mapping = container_of(node, struct hv_iommu_mapping, iova);
> + paddr = mapping->paddr + (iova - mapping->iova.start);
> + }
> + spin_unlock_irqrestore(&hvdom->mappings_lock, flags);
> +
> + return paddr;
> +}
> +
> +/*
> + * Currently, hypervisor does not provide list of devices it is using
> + * dynamically. So use this to allow users to manually specify devices that
> + * should be skipped. (eg. hypervisor debugger using some network device).
> + */
> +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;
> + 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;
> + if (parsed <= 0)
> + break;
> +
> + if (pci_domain_nr(pdev->bus) == segment &&
> + pdev->bus->number == bus &&
> + PCI_SLOT(pdev->devfn) == slot &&
> + PCI_FUNC(pdev->devfn) == func) {
> +
> + dev_info(dev, "skipped by Hyper-V IOMMU\n");
> + return ERR_PTR(-ENODEV);
> + }
> + pos += parsed;
> +
> + } while (pci_devs_to_skip[pos]);
> + }
> +
> + /* Device will be explicitly attached to the default domain, so no need
> + * to do dev_iommu_priv_set() here.
> + */
> +
> + return &hv_virt_iommu;
> +}
> +
> +static void hv_iommu_probe_finalize(struct device *dev)
> +{
> + struct iommu_domain *immdom = iommu_get_domain_for_dev(dev);
> +
> + if (immdom && immdom->type == IOMMU_DOMAIN_DMA)
> + iommu_setup_dma_ops(dev);
> + else
> + set_dma_ops(dev, NULL);
> +}
> +
> +static void hv_iommu_release_device(struct device *dev)
> +{
> + struct hv_domain *hvdom = dev_iommu_priv_get(dev);
> +
> + /* Need to detach device from device domain if necessary. */
> + if (hvdom)
> + hv_iommu_detach_dev(&hvdom->iommu_dom, dev);
> +
> + dev_iommu_priv_set(dev, NULL);
> + set_dma_ops(dev, NULL);
> +}
> +
> +static struct iommu_group *hv_iommu_device_group(struct device *dev)
> +{
> + if (dev_is_pci(dev))
> + return pci_device_group(dev);
> + else
> + return generic_device_group(dev);
> +}
> +
> +static int hv_iommu_def_domain_type(struct device *dev)
> +{
> + /* The hypervisor always creates this by default during boot */
> + return IOMMU_DOMAIN_IDENTITY;
> +}
> +
> +static struct iommu_ops hv_iommu_ops = {
> + .capable = hv_iommu_capable,
> + .domain_alloc_identity = hv_iommu_domain_alloc_identity,
> + .domain_alloc_paging = hv_iommu_domain_alloc_paging,
> + .probe_device = hv_iommu_probe_device,
> + .probe_finalize = hv_iommu_probe_finalize,
> + .release_device = hv_iommu_release_device,
> + .def_domain_type = hv_iommu_def_domain_type,
> + .device_group = hv_iommu_device_group,
> + .default_domain_ops = &(const struct iommu_domain_ops) {
> + .attach_dev = hv_iommu_attach_dev,
> + .map_pages = hv_iommu_map_pages,
> + .unmap_pages = hv_iommu_unmap_pages,
> + .iova_to_phys = hv_iommu_iova_to_phys,
> + .free = hv_iommu_domain_free,
> + },
> + .owner = THIS_MODULE,
> +};
> +
> +static void __init hv_initialize_special_domains(void)
> +{
> + hv_def_identity_dom.iommu_dom.geometry = default_geometry;
> + hv_def_identity_dom.domid_num = HV_DEVICE_DOMAIN_ID_S2_DEFAULT; /* 0 */
hv_def_identity_dom is a static global variable.
Why not initialize hv_def_identity_dom upon definition instead of
introducing a new function?
> +}
> +
> +static int __init hv_iommu_init(void)
> +{
> + int ret;
> + struct iommu_device *iommup = &hv_virt_iommu;
> +
> + if (!hv_is_hyperv_initialized())
> + return -ENODEV;
> +
> + ret = iommu_device_sysfs_add(iommup, NULL, NULL, "%s", "hyperv-iommu");
> + if (ret) {
> + pr_err("Hyper-V: iommu_device_sysfs_add failed: %d\n", ret);
> + return ret;
> + }
> +
> + /* This must come before iommu_device_register because the latter calls
> + * into the hooks.
> + */
> + hv_initialize_special_domains();
> +
> + ret = iommu_device_register(iommup, &hv_iommu_ops, NULL);
It looks weird to initialize an object after creating sysfs entries for
it.
It should be the other way around.
Thanks,
Stanislav
> + if (ret) {
> + pr_err("Hyper-V: iommu_device_register failed: %d\n", ret);
> + goto err_sysfs_remove;
> + }
> +
> + pr_info("Hyper-V IOMMU initialized\n");
> +
> + return 0;
> +
> +err_sysfs_remove:
> + iommu_device_sysfs_remove(iommup);
> + return ret;
> +}
> +
> +void __init hv_iommu_detect(void)
> +{
> + if (no_iommu || iommu_detected)
> + return;
> +
> + /* For l1vh, always expose an iommu unit */
> + if (!hv_l1vh_partition())
> + if (!(ms_hyperv.misc_features & HV_DEVICE_DOMAIN_AVAILABLE))
> + return;
> +
> + iommu_detected = 1;
> + x86_init.iommu.iommu_init = hv_iommu_init;
> +
> + pci_request_acs();
> +}
> diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h
> index dfc516c1c719..2ad111727e82 100644
> --- a/include/linux/hyperv.h
> +++ b/include/linux/hyperv.h
> @@ -1767,4 +1767,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 */
> --
> 2.51.2.vfs.0.1
>
^ permalink raw reply
* Re: [PATCH v0 13/15] x86/hyperv: Basic interrupt support for direct attached devices
From: Stanislav Kinsburskii @ 2026-01-21 0:47 UTC (permalink / raw)
To: Mukesh R
Cc: linux-kernel, linux-hyperv, linux-arm-kernel, iommu, linux-pci,
linux-arch, kys, haiyangz, wei.liu, decui, longli,
catalin.marinas, will, tglx, mingo, bp, dave.hansen, hpa, joro,
lpieralisi, kwilczynski, mani, robh, bhelgaas, arnd, nunodasneves,
mhklinux, romank
In-Reply-To: <20260120064230.3602565-14-mrathor@linux.microsoft.com>
On Mon, Jan 19, 2026 at 10:42:28PM -0800, Mukesh R wrote:
> From: Mukesh Rathor <mrathor@linux.microsoft.com>
>
> As mentioned previously, a direct attached device must be referenced
> via logical device id which is formed in the initial attach hypercall.
> Interrupt mapping paths for direct attached devices are almost same,
> except we must use logical device ids instead of the PCI device ids.
>
> L1VH only supports direct attaches for passing thru devices to its guests,
> and devices on L1VH are VMBus based. However, the interrupts are mapped
> via the map interrupt hypercall and not the traditional method of VMBus
> messages.
>
> Partition id for the relevant hypercalls is tricky. This because a device
> could be moving from root to guest and then back to the root. In case
> of L1VH, it could be moving from system host to L1VH root to a guest,
> then back to the L1VH root. So, it is carefully crafted by keeping
> track of whether the call is on behalf of a VMM process, whether the
> device is attached device (as opposed to mapped), and whether we are in
> an L1VH root/parent. If VMM process, we assume it is on behalf of a
> guest. Otherwise, the device is being attached or detached during boot
> or shutdown of the privileged partition.
>
> Lastly, a dummy cpu and vector is used to map interrupt for a direct
> attached device. This because, once a device is marked for direct attach,
> hypervisor will not let any interrupts be mapped to host. So it is mapped
> to guest dummy cpu and dummy vector. This is then correctly mapped during
> guest boot via the retarget paths.
>
> Signed-off-by: Mukesh Rathor <mrathor@linux.microsoft.com>
> ---
> arch/arm64/include/asm/mshyperv.h | 15 +++++
> arch/x86/hyperv/irqdomain.c | 57 +++++++++++++-----
> arch/x86/include/asm/mshyperv.h | 4 ++
> drivers/pci/controller/pci-hyperv.c | 91 +++++++++++++++++++++++++----
> 4 files changed, 142 insertions(+), 25 deletions(-)
>
> diff --git a/arch/arm64/include/asm/mshyperv.h b/arch/arm64/include/asm/mshyperv.h
> index b721d3134ab6..27da480f94f6 100644
> --- a/arch/arm64/include/asm/mshyperv.h
> +++ b/arch/arm64/include/asm/mshyperv.h
> @@ -53,6 +53,21 @@ static inline u64 hv_get_non_nested_msr(unsigned int reg)
> return hv_get_msr(reg);
> }
>
> +struct irq_data;
> +struct msi_msg;
> +struct pci_dev;
> +static inline void hv_irq_compose_msi_msg(struct irq_data *data,
> + struct msi_msg *msg) {};
> +static inline int hv_unmap_msi_interrupt(struct pci_dev *pdev,
> + struct hv_interrupt_entry *hvirqe)
> +{
> + return -EOPNOTSUPP;
> +}
> +static inline bool hv_pcidev_is_attached_dev(struct pci_dev *pdev)
> +{
> + return false;
> +}
> +
> /* SMCCC hypercall parameters */
> #define HV_SMCCC_FUNC_NUMBER 1
> #define HV_FUNC_ID ARM_SMCCC_CALL_VAL( \
> diff --git a/arch/x86/hyperv/irqdomain.c b/arch/x86/hyperv/irqdomain.c
> index 33017aa0caa4..e6eb457f791e 100644
> --- a/arch/x86/hyperv/irqdomain.c
> +++ b/arch/x86/hyperv/irqdomain.c
> @@ -13,6 +13,16 @@
> #include <linux/irqchip/irq-msi-lib.h>
> #include <asm/mshyperv.h>
>
> +/*
> + * For direct attached devices (which use logical device ids), hypervisor will
> + * not allow mappings to host. But VFIO needs to bind the interrupt at the very
> + * start before the guest cpu/vector is known. So we use dummy cpu and vector
> + * to bind in such case, and later when the guest starts, retarget will move it
> + * to correct guest cpu and vector.
> + */
> +#define HV_DDA_DUMMY_CPU 0
> +#define HV_DDA_DUMMY_VECTOR 32
> +
> static u64 hv_map_interrupt_hcall(u64 ptid, union hv_device_id hv_devid,
> bool level, int cpu, int vector,
> struct hv_interrupt_entry *ret_entry)
> @@ -24,6 +34,11 @@ static u64 hv_map_interrupt_hcall(u64 ptid, union hv_device_id hv_devid,
> u64 status;
> int nr_bank, var_size;
>
> + if (hv_devid.device_type == HV_DEVICE_TYPE_LOGICAL) {
> + cpu = HV_DDA_DUMMY_CPU;
> + vector = HV_DDA_DUMMY_VECTOR;
> + }
> +
> local_irq_save(flags);
>
> input = *this_cpu_ptr(hyperv_pcpu_input_arg);
> @@ -95,7 +110,8 @@ static int hv_map_interrupt(u64 ptid, 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 *irq_entry)
> +static int hv_unmap_interrupt(union hv_device_id hv_devid,
> + struct hv_interrupt_entry *irq_entry)
> {
> unsigned long flags;
> struct hv_input_unmap_device_interrupt *input;
> @@ -103,10 +119,14 @@ static int hv_unmap_interrupt(u64 id, struct hv_interrupt_entry *irq_entry)
>
> local_irq_save(flags);
> input = *this_cpu_ptr(hyperv_pcpu_input_arg);
> -
> memset(input, 0, sizeof(*input));
> - input->partition_id = hv_current_partition_id;
> - input->device_id = id;
> +
> + if (hv_devid.device_type == HV_DEVICE_TYPE_LOGICAL)
> + input->partition_id = hv_iommu_get_curr_partid();
> + else
> + input->partition_id = hv_current_partition_id;
> +
> + input->device_id = hv_devid.as_uint64;
> input->interrupt_entry = *irq_entry;
>
> status = hv_do_hypercall(HVCALL_UNMAP_DEVICE_INTERRUPT, input, NULL);
> @@ -263,6 +283,7 @@ static u64 hv_build_irq_devid(struct pci_dev *pdev)
> int hv_map_msi_interrupt(struct irq_data *data,
> struct hv_interrupt_entry *out_entry)
> {
> + u64 ptid;
> struct irq_cfg *cfg = irqd_cfg(data);
> struct hv_interrupt_entry dummy;
> union hv_device_id hv_devid;
> @@ -275,8 +296,17 @@ int hv_map_msi_interrupt(struct irq_data *data,
> hv_devid.as_uint64 = hv_build_irq_devid(pdev);
> cpu = cpumask_first(irq_data_get_effective_affinity_mask(data));
>
> - return hv_map_interrupt(hv_current_partition_id, hv_devid, false, cpu,
> - cfg->vector, out_entry ? out_entry : &dummy);
> + if (hv_devid.device_type == HV_DEVICE_TYPE_LOGICAL)
> + if (hv_pcidev_is_attached_dev(pdev))
> + ptid = hv_iommu_get_curr_partid();
> + else
> + /* Device actually on l1vh root, not passthru'd to vm */
l1vh and root are mutually exclusive partitions.
If you wanted to highlight that it's l1vh itself and not its child guest, then
"l1vh parent" term would do.
> + ptid = hv_current_partition_id;
> + else
> + ptid = hv_current_partition_id;
Looks like the only special case is for attached logical devices,
otherwise hv_current_partition_id is used.
Can the logic simplified here?
Thanks,
Stanislav
> +
> + return hv_map_interrupt(ptid, hv_devid, false, cpu, cfg->vector,
> + out_entry ? out_entry : &dummy);
> }
> EXPORT_SYMBOL_GPL(hv_map_msi_interrupt);
>
> @@ -289,10 +319,7 @@ static void entry_to_msi_msg(struct hv_interrupt_entry *entry,
> msg->data = entry->msi_entry.data.as_uint32;
> }
>
> -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)
> +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);
> @@ -341,16 +368,18 @@ 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);
> }
> +EXPORT_SYMBOL_GPL(hv_irq_compose_msi_msg);
>
> -static int hv_unmap_msi_interrupt(struct pci_dev *pdev,
> - struct hv_interrupt_entry *irq_entry)
> +int hv_unmap_msi_interrupt(struct pci_dev *pdev,
> + struct hv_interrupt_entry *irq_entry)
> {
> union hv_device_id hv_devid;
>
> hv_devid.as_uint64 = hv_build_irq_devid(pdev);
>
> - return hv_unmap_interrupt(hv_devid.as_uint64, irq_entry);
> + return hv_unmap_interrupt(hv_devid, irq_entry);
> }
> +EXPORT_SYMBOL_GPL(hv_unmap_msi_interrupt);
>
> /* 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)
> @@ -486,7 +515,7 @@ int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry)
> hv_devid.device_type = HV_DEVICE_TYPE_IOAPIC;
> hv_devid.ioapic.ioapic_id = (u8)ioapic_id;
>
> - return hv_unmap_interrupt(hv_devid.as_uint64, entry);
> + return hv_unmap_interrupt(hv_devid, entry);
> }
> EXPORT_SYMBOL_GPL(hv_unmap_ioapic_interrupt);
>
> diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h
> index e4ccdbbf1d12..b6facd3a0f5e 100644
> --- a/arch/x86/include/asm/mshyperv.h
> +++ b/arch/x86/include/asm/mshyperv.h
> @@ -204,11 +204,15 @@ static inline u64 hv_iommu_get_curr_partid(void)
> #endif /* CONFIG_HYPERV_IOMMU */
>
> u64 hv_pci_vmbus_device_id(struct pci_dev *pdev);
> +void hv_irq_compose_msi_msg(struct irq_data *data, struct msi_msg *msg);
> +extern bool hv_no_attdev;
>
> struct irq_domain *hv_create_pci_msi_domain(void);
>
> int hv_map_msi_interrupt(struct irq_data *data,
> struct hv_interrupt_entry *out_entry);
> +int hv_unmap_msi_interrupt(struct pci_dev *dev,
> + struct hv_interrupt_entry *hvirqe);
> int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
> struct hv_interrupt_entry *entry);
> int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
> diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c
> index 40f0b06bb966..71d1599dc4a8 100644
> --- a/drivers/pci/controller/pci-hyperv.c
> +++ b/drivers/pci/controller/pci-hyperv.c
> @@ -660,15 +660,17 @@ static void hv_irq_retarget_interrupt(struct irq_data *data)
>
> params = *this_cpu_ptr(hyperv_pcpu_input_arg);
> memset(params, 0, sizeof(*params));
> - params->partition_id = HV_PARTITION_ID_SELF;
> +
> + if (hv_pcidev_is_attached_dev(pdev))
> + params->partition_id = hv_iommu_get_curr_partid();
> + else
> + params->partition_id = HV_PARTITION_ID_SELF;
> +
> params->int_entry.source = HV_INTERRUPT_SOURCE_MSI;
> - params->int_entry.msi_entry.address.as_uint32 = int_desc->address & 0xffffffff;
> + params->int_entry.msi_entry.address.as_uint32 =
> + int_desc->address & 0xffffffff;
> params->int_entry.msi_entry.data.as_uint32 = int_desc->data;
> - params->device_id = (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);
> + params->device_id = hv_pci_vmbus_device_id(pdev);
> params->int_target.vector = hv_msi_get_int_vector(data);
>
> if (hbus->protocol_version >= PCI_PROTOCOL_VERSION_1_2) {
> @@ -1263,6 +1265,15 @@ static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
> mb();
> }
> spin_unlock_irqrestore(&hbus->config_lock, flags);
> + /*
> + * Make sure PCI_INTERRUPT_PIN is hard-wired to 0 since it may
> + * be read using a 32bit read which is skipped by the above
> + * emulation.
> + */
> + if (PCI_INTERRUPT_PIN >= where &&
> + PCI_INTERRUPT_PIN <= (where + size)) {
> + *((char *)val + PCI_INTERRUPT_PIN - where) = 0;
> + }
> } else {
> dev_err(dev, "Attempt to read beyond a function's config space.\n");
> }
> @@ -1731,14 +1742,22 @@ static void hv_msi_free(struct irq_domain *domain, unsigned int irq)
> if (!int_desc)
> return;
>
> - irq_data->chip_data = NULL;
> hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
> if (!hpdev) {
> + irq_data->chip_data = NULL;
> kfree(int_desc);
> return;
> }
>
> - hv_int_desc_free(hpdev, int_desc);
> + if (hv_pcidev_is_attached_dev(pdev)) {
> + hv_unmap_msi_interrupt(pdev, irq_data->chip_data);
> + kfree(irq_data->chip_data);
> + irq_data->chip_data = NULL;
> + } else {
> + irq_data->chip_data = NULL;
> + hv_int_desc_free(hpdev, int_desc);
> + }
> +
> put_pcichild(hpdev);
> }
>
> @@ -2139,6 +2158,56 @@ static void hv_vmbus_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
> msg->data = 0;
> }
>
> +/* Compose an msi message for a directly attached device */
> +static void hv_dda_compose_msi_msg(struct irq_data *irq_data,
> + struct msi_desc *msi_desc,
> + struct msi_msg *msg)
> +{
> + bool multi_msi;
> + struct hv_pcibus_device *hbus;
> + struct hv_pci_dev *hpdev;
> + struct pci_dev *pdev = msi_desc_to_pci_dev(msi_desc);
> +
> + multi_msi = !msi_desc->pci.msi_attrib.is_msix &&
> + msi_desc->nvec_used > 1;
> +
> + if (multi_msi) {
> + dev_err(&hbus->hdev->device,
> + "Passthru direct attach does not support multi msi\n");
> + goto outerr;
> + }
> +
> + hbus = container_of(pdev->bus->sysdata, struct hv_pcibus_device,
> + sysdata);
> +
> + hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
> + if (!hpdev)
> + goto outerr;
> +
> + /* will unmap if needed and also update irq_data->chip_data */
> + hv_irq_compose_msi_msg(irq_data, msg);
> +
> + put_pcichild(hpdev);
> + return;
> +
> +outerr:
> + memset(msg, 0, sizeof(*msg));
> +}
> +
> +static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
> +{
> + struct pci_dev *pdev;
> + struct msi_desc *msi_desc;
> +
> + msi_desc = irq_data_get_msi_desc(data);
> + pdev = msi_desc_to_pci_dev(msi_desc);
> +
> + if (hv_pcidev_is_attached_dev(pdev))
> + hv_dda_compose_msi_msg(data, msi_desc, msg);
> + else
> + hv_vmbus_compose_msi_msg(data, msg);
> +}
> +
> static bool hv_pcie_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
> struct irq_domain *real_parent, struct msi_domain_info *info)
> {
> @@ -2177,7 +2246,7 @@ static const struct msi_parent_ops hv_pcie_msi_parent_ops = {
> /* HW Interrupt Chip Descriptor */
> static struct irq_chip hv_msi_irq_chip = {
> .name = "Hyper-V PCIe MSI",
> - .irq_compose_msi_msg = hv_vmbus_compose_msi_msg,
> + .irq_compose_msi_msg = hv_compose_msi_msg,
> .irq_set_affinity = irq_chip_set_affinity_parent,
> .irq_ack = irq_chip_ack_parent,
> .irq_eoi = irq_chip_eoi_parent,
> @@ -4096,7 +4165,7 @@ static int hv_pci_restore_msi_msg(struct pci_dev *pdev, void *arg)
> irq_data = irq_get_irq_data(entry->irq);
> if (WARN_ON_ONCE(!irq_data))
> return -EINVAL;
> - hv_vmbus_compose_msi_msg(irq_data, &entry->msg);
> + hv_compose_msi_msg(irq_data, &entry->msg);
> }
> return 0;
> }
> --
> 2.51.2.vfs.0.1
>
^ permalink raw reply
* Re: [PATCH v0 03/15] x86/hyperv: add insufficient memory support in irqdomain.c
From: kernel test robot @ 2026-01-21 0:53 UTC (permalink / raw)
To: Mukesh R, linux-kernel, linux-hyperv, linux-arm-kernel, iommu,
linux-pci, linux-arch
Cc: oe-kbuild-all, kys, haiyangz, wei.liu, decui, longli,
catalin.marinas, will, tglx, mingo, bp, dave.hansen, hpa, joro,
lpieralisi, kwilczynski, mani, robh, bhelgaas, arnd, nunodasneves,
mhklinux, romank
In-Reply-To: <20260120064230.3602565-4-mrathor@linux.microsoft.com>
Hi Mukesh,
kernel test robot noticed the following build warnings:
[auto build test WARNING on tip/x86/core]
[also build test WARNING on pci/next pci/for-linus arm64/for-next/core clk/clk-next soc/for-next linus/master v6.19-rc6 next-20260120]
[cannot apply to arnd-asm-generic/master]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Mukesh-R/iommu-hyperv-rename-hyperv-iommu-c-to-hyperv-irq-c/20260120-145832
base: tip/x86/core
patch link: https://lore.kernel.org/r/20260120064230.3602565-4-mrathor%40linux.microsoft.com
patch subject: [PATCH v0 03/15] x86/hyperv: add insufficient memory support in irqdomain.c
config: i386-randconfig-053-20260120 (https://download.01.org/0day-ci/archive/20260121/202601210731.f1WLdgcO-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202601210731.f1WLdgcO-lkp@intel.com/
cocci warnings: (new ones prefixed by >>)
>> arch/x86/hyperv/irqdomain.c:90:2-3: Unneeded semicolon
vim +90 arch/x86/hyperv/irqdomain.c
72
73 static int hv_map_interrupt(u64 ptid, union hv_device_id device_id, bool level,
74 int cpu, int vector,
75 struct hv_interrupt_entry *ret_entry)
76 {
77 u64 status;
78 int rc, deposit_pgs = 16; /* don't loop forever */
79
80 while (deposit_pgs--) {
81 status = hv_map_interrupt_hcall(ptid, device_id, level, cpu,
82 vector, ret_entry);
83
84 if (hv_result(status) != HV_STATUS_INSUFFICIENT_MEMORY)
85 break;
86
87 rc = hv_call_deposit_pages(NUMA_NO_NODE, ptid, 1);
88 if (rc)
89 break;
> 90 };
91
92 if (!hv_result_success(status))
93 hv_status_err(status, "\n");
94
95 return hv_result_to_errno(status);
96 }
97
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply
* Re: [PATCH v0 14/15] mshv: Remove mapping of mmio space during map user ioctl
From: Stanislav Kinsburskii @ 2026-01-21 1:41 UTC (permalink / raw)
To: Mukesh R
Cc: linux-kernel, linux-hyperv, linux-arm-kernel, iommu, linux-pci,
linux-arch, kys, haiyangz, wei.liu, decui, longli,
catalin.marinas, will, tglx, mingo, bp, dave.hansen, hpa, joro,
lpieralisi, kwilczynski, mani, robh, bhelgaas, arnd, nunodasneves,
mhklinux, romank
In-Reply-To: <20260120064230.3602565-15-mrathor@linux.microsoft.com>
On Mon, Jan 19, 2026 at 10:42:29PM -0800, Mukesh R wrote:
> From: Mukesh Rathor <mrathor@linux.microsoft.com>
>
> 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.
>
> Signed-off-by: Mukesh Rathor <mrathor@linux.microsoft.com>
> ---
> drivers/hv/mshv_root_main.c | 20 ++++----------------
> 1 file changed, 4 insertions(+), 16 deletions(-)
>
> diff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c
> index 27313419828d..03f3aa9f5541 100644
> --- a/drivers/hv/mshv_root_main.c
> +++ b/drivers/hv/mshv_root_main.c
> @@ -1258,16 +1258,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.
> */
> static long
> mshv_map_user_memory(struct mshv_partition *partition,
> @@ -1276,7 +1268,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) ||
> @@ -1286,7 +1277,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)
> @@ -1313,10 +1303,8 @@ mshv_map_user_memory(struct mshv_partition *partition,
> 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);
> + /* mmio mappings are handled later during intercepts */
> + ret = 0;
No need updating ret here: it's 0 after the previous call.
Thanks,
Stanislav
> break;
> }
>
> --
> 2.51.2.vfs.0.1
>
^ permalink raw reply
* Re: [PATCH v0 15/15] mshv: Populate mmio mappings for PCI passthru
From: Stanislav Kinsburskii @ 2026-01-21 1:53 UTC (permalink / raw)
To: Mukesh R
Cc: linux-kernel, linux-hyperv, linux-arm-kernel, iommu, linux-pci,
linux-arch, kys, haiyangz, wei.liu, decui, longli,
catalin.marinas, will, tglx, mingo, bp, dave.hansen, hpa, joro,
lpieralisi, kwilczynski, mani, robh, bhelgaas, arnd, nunodasneves,
mhklinux, romank
In-Reply-To: <20260120064230.3602565-16-mrathor@linux.microsoft.com>
On Mon, Jan 19, 2026 at 10:42:30PM -0800, Mukesh R wrote:
> From: Mukesh Rathor <mrathor@linux.microsoft.com>
>
> 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).
>
> Signed-off-by: Mukesh Rathor <mrathor@linux.microsoft.com>
> ---
> drivers/hv/mshv_root_main.c | 115 ++++++++++++++++++++++++++++++++++++
> 1 file changed, 115 insertions(+)
>
> diff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c
> index 03f3aa9f5541..4c8bc7cd0888 100644
> --- a/drivers/hv/mshv_root_main.c
> +++ b/drivers/hv/mshv_root_main.c
> @@ -56,6 +56,14 @@ struct hv_stats_page {
> };
> } __packed;
>
> +bool hv_nofull_mmio; /* don't map entire mmio region upon fault */
> +static int __init setup_hv_full_mmio(char *str)
> +{
> + hv_nofull_mmio = true;
> + return 0;
> +}
> +__setup("hv_nofull_mmio", setup_hv_full_mmio);
> +
> struct mshv_root mshv_root;
>
> enum hv_scheduler_type hv_scheduler_type;
> @@ -612,6 +620,109 @@ mshv_partition_region_by_gfn(struct mshv_partition *partition, u64 gfn)
> }
>
> #ifdef CONFIG_X86_64
> +
> +/*
> + * 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(struct mshv_partition *pt, u64 gfn,
> + u64 *mmio_pfnp)
> +{
> + struct vm_area_struct *vma;
> + bool is_mmio;
> + u64 uaddr;
> + struct mshv_mem_region *mreg;
> + struct follow_pfnmap_args pfnmap_args;
> + int rc = -EINVAL;
> +
> + /*
> + * Do not allow mem region to be deleted beneath us. VFIO uses
> + * useraddr vma to lookup pci bar pfn.
> + */
> + spin_lock(&pt->pt_mem_regions_lock);
> +
> + /* Get the region again under the lock */
> + mreg = mshv_partition_region_by_gfn(pt, gfn);
> + if (mreg == NULL || mreg->type != MSHV_REGION_TYPE_MMIO)
> + goto unlock_pt_out;
> +
> + uaddr = mreg->start_uaddr +
> + ((gfn - mreg->start_gfn) << HV_HYP_PAGE_SHIFT);
> +
> + mmap_read_lock(current->mm);
Semaphore can't be taken under spinlock.
Get it instead.
> + vma = vma_lookup(current->mm, uaddr);
> + is_mmio = vma ? !!(vma->vm_flags & (VM_IO | VM_PFNMAP)) : 0;
Why this check is needed again?
The region type is stored on the region itself.
And the type is checked on the caller side.
> + 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);
> +unlock_pt_out:
> + spin_unlock(&pt->pt_mem_regions_lock);
> + return rc;
> +}
> +
> +/*
> + * At present, the only unmapped gpa is mmio space. Verify if it's mmio
> + * and resolve if possible.
> + * Returns: True if valid mmio intercept and it was handled, else false
> + */
> +static bool mshv_handle_unmapped_gpa(struct mshv_vp *vp)
> +{
> + struct hv_message *hvmsg = vp->vp_intercept_msg_page;
> + struct hv_x64_memory_intercept_message *msg;
> + union hv_x64_memory_access_info accinfo;
> + u64 gfn, mmio_spa, numpgs;
> + struct mshv_mem_region *mreg;
> + int rc;
> + struct mshv_partition *pt = vp->vp_partition;
> +
> + msg = (struct hv_x64_memory_intercept_message *)hvmsg->u.payload;
> + accinfo = msg->memory_access_info;
> +
> + if (!accinfo.gva_gpa_valid)
> + return false;
> +
> + /* Do a fast check and bail if non mmio intercept */
> + gfn = msg->guest_physical_address >> HV_HYP_PAGE_SHIFT;
> + mreg = mshv_partition_region_by_gfn(pt, gfn);
This call needs to be protected by the spinlock.
Thanks,
Stanislav
> + if (mreg == NULL || mreg->type != MSHV_REGION_TYPE_MMIO)
> + return false;
> +
> + rc = mshv_chk_get_mmio_start_pfn(pt, gfn, &mmio_spa);
> + if (rc)
> + return false;
> +
> + if (!hv_nofull_mmio) { /* default case */
> + gfn = mreg->start_gfn;
> + mmio_spa = mmio_spa - (gfn - mreg->start_gfn);
> + numpgs = mreg->nr_pages;
> + } else
> + numpgs = 1;
> +
> + rc = hv_call_map_mmio_pages(pt->pt_id, gfn, mmio_spa, numpgs);
> +
> + return rc == 0;
> +}
> +
> static struct mshv_mem_region *
> mshv_partition_region_by_gfn_get(struct mshv_partition *p, u64 gfn)
> {
> @@ -666,13 +777,17 @@ static bool mshv_handle_gpa_intercept(struct mshv_vp *vp)
>
> return ret;
> }
> +
> #else /* CONFIG_X86_64 */
> +static bool mshv_handle_unmapped_gpa(struct mshv_vp *vp) { return false; }
> static bool mshv_handle_gpa_intercept(struct mshv_vp *vp) { return false; }
> #endif /* CONFIG_X86_64 */
>
> 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);
> }
> --
> 2.51.2.vfs.0.1
>
^ permalink raw reply
* [PATCH V0] x86/hyperv: Fix compiler warnings in hv_crash.c
From: Mukesh R @ 2026-01-21 2:40 UTC (permalink / raw)
To: linux-hyperv, linux-kernel; +Cc: wei.liu
Fix two compiler warnings:
o smp_ops is only defined if CONFIG_SMP
o status is set but not explicitly used.
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202512301641.FC6OAbGM-lkp@intel.com/
Signed-off-by: Mukesh R <mrathor@linux.microsoft.com>
---
arch/x86/hyperv/hv_crash.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/arch/x86/hyperv/hv_crash.c b/arch/x86/hyperv/hv_crash.c
index c0e22921ace1..82915b22ceae 100644
--- a/arch/x86/hyperv/hv_crash.c
+++ b/arch/x86/hyperv/hv_crash.c
@@ -279,7 +279,6 @@ static void hv_notify_prepare_hyp(void)
static noinline __noclone void crash_nmi_callback(struct pt_regs *regs)
{
struct hv_input_disable_hyp_ex *input;
- u64 status;
int msecs = 1000, ccpu = smp_processor_id();
if (ccpu == 0) {
@@ -313,7 +312,7 @@ static noinline __noclone void crash_nmi_callback(struct pt_regs *regs)
input->rip = trampoline_pa;
input->arg = devirt_arg;
- status = hv_do_hypercall(HVCALL_DISABLE_HYP_EX, input, NULL);
+ hv_do_hypercall(HVCALL_DISABLE_HYP_EX, input, NULL);
hv_panic_timeout_reboot();
}
@@ -628,8 +627,9 @@ void hv_root_crash_init(void)
if (rc)
goto err_out;
+#ifdef CONFIG_SMP
smp_ops.crash_stop_other_cpus = hv_crash_stop_other_cpus;
-
+#endif
crash_kexec_post_notifiers = true;
hv_crash_enabled = true;
pr_info("Hyper-V: both linux and hypervisor kdump support enabled\n");
--
2.51.2.vfs.0.1
^ permalink raw reply related
* Re: [net-next,v15,06/12] selftests/vsock: prepare vm management helpers for namespaces
From: Jakub Kicinski @ 2026-01-21 2:54 UTC (permalink / raw)
To: bobbyeshleman
Cc: Jakub Kicinski, sgarzare, kvm, linux-kernel, mst, horms,
bobbyeshleman, corbet, xuanzhuo, haiyangz, jasowang, linux-hyperv,
pabeni, kys, vishnu.dasa, longli, linux-doc, linux-kselftest,
netdev, stefanha, berrange, decui, bryan-bt.tan, eperezma,
wei.liu, davem, shuah, bcm-kernel-feedback-list, edumazet,
virtualization, sargun
In-Reply-To: <20260116-vsock-vmtest-v15-6-bbfd1a668548@meta.com>
In case you need to respin..
This is an AI-generated review of your patch.
For local reproduction steps see https://netdev-ai.bots.linux.dev/ai-local.html
---
selftests/vsock: prepare vm management helpers for namespaces
This commit adds namespace support to VM management helpers and wrapper
functions, requiring callers to explicitly pass "init_ns" for the init
namespace. The commit message states "No functional changes for existing
tests. All have been updated to pass 'init_ns' explicitly."
> diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selftests/vsock/vmtest.sh
> index c2bdc293b94c..1d03acb62347 100755
> --- a/tools/testing/selftests/vsock/vmtest.sh
> +++ b/tools/testing/selftests/vsock/vmtest.sh
> @@ -135,7 +135,18 @@ del_namespaces() {
> }
>
> vm_ssh() {
> - ssh -q -o UserKnownHostsFile=/dev/null -p ${SSH_HOST_PORT} localhost "$@"
> + local ns_exec
> +
> + if [[ "${1}" == init_ns ]]; then
> + ns_exec=""
> + else
> + ns_exec="ip netns exec ${1}"
> + fi
> +
> + shift
> +
> + ${ns_exec} ssh -q -o UserKnownHostsFile=/dev/null -p "${SSH_HOST_PORT}" localhost "$@"
> +
> return $?
> }
The vm_ssh() function now expects a namespace as its first argument, but
run_shared_vm_test() still calls vm_ssh without this parameter:
vm_oops_cnt_before=$(vm_ssh -- dmesg | grep -c -i 'Oops')
vm_warn_cnt_before=$(vm_ssh -- dmesg --level=warn | grep -c -i 'vsock')
vm_oops_cnt_after=$(vm_ssh -- dmesg | grep -i 'Oops' | wc -l)
vm_warn_cnt_after=$(vm_ssh -- dmesg --level=warn | grep -c -i 'vsock')
When called as "vm_ssh -- dmesg", the "--" is interpreted as the namespace
argument. Since "--" does not equal "init_ns", the function sets
ns_exec="ip netns exec --" which is invalid. The shift then consumes "--"
and the command becomes "ip netns exec -- ssh ... dmesg" which will fail.
Interestingly, the v9 version of this patch on lore correctly updated these
calls to pass "init_ns", but this change appears to have been lost between
v9 and v15. The fix for this regression is present later in the series in
commit 3ae984505e00 ("selftests/vsock: add vm_dmesg_{warn,oops}_count()
helpers").
[ ... ]
^ permalink raw reply
* [PATCH net-next v2] net: mana: Improve diagnostic logging for better debuggability
From: Erni Sri Satya Vennela @ 2026-01-21 6:56 UTC (permalink / raw)
To: kys, haiyangz, wei.liu, decui, longli, andrew+netdev, davem,
edumazet, kuba, pabeni, leon, kotaranov, shradhagupta, ernis,
yury.norov, dipayanroy, shirazsaleem, ssengar, gargaditya,
linux-hyperv, netdev, linux-kernel
Enhance MANA driver logging to provide better visibility into
hardware configuration and error states during driver initialization
and runtime operations.
Signed-off-by: Erni Sri Satya Vennela <ernis@linux.microsoft.com>
---
Changes in v2:
* Update commit message.
* Use "Enabled vPort ..." instead of "Configured vPort" in
mana_cfg_vport.
* Add info log in mana_uncfg_vport, mana_gd_verify_vf_version,
mana_gd_query_max_resources, mana_query_device_cfg and
mana_query_vport_cfg.
---
.../net/ethernet/microsoft/mana/gdma_main.c | 6 +++++
.../net/ethernet/microsoft/mana/hw_channel.c | 12 ++++++----
drivers/net/ethernet/microsoft/mana/mana_en.c | 23 ++++++++++++++-----
3 files changed, 30 insertions(+), 11 deletions(-)
diff --git a/drivers/net/ethernet/microsoft/mana/gdma_main.c b/drivers/net/ethernet/microsoft/mana/gdma_main.c
index 0055c231acf6..c7b65ddea651 100644
--- a/drivers/net/ethernet/microsoft/mana/gdma_main.c
+++ b/drivers/net/ethernet/microsoft/mana/gdma_main.c
@@ -152,6 +152,9 @@ static int mana_gd_query_max_resources(struct pci_dev *pdev)
if (gc->max_num_queues > gc->num_msix_usable - 1)
gc->max_num_queues = gc->num_msix_usable - 1;
+ dev_info(gc->dev, "Max Resources: msix_usable=%u max_queues=%u\n",
+ gc->num_msix_usable, gc->max_num_queues);
+
return 0;
}
@@ -1229,6 +1232,9 @@ int mana_gd_verify_vf_version(struct pci_dev *pdev)
}
dev_dbg(gc->dev, "set the hwc timeout to %u\n", hwc->hwc_timeout);
}
+
+ dev_info(gc->dev, "VF Version: protocol=0x%llx pf_caps=[0x%llx]\n",
+ resp.gdma_protocol_ver, gc->pf_cap_flags1);
return 0;
}
diff --git a/drivers/net/ethernet/microsoft/mana/hw_channel.c b/drivers/net/ethernet/microsoft/mana/hw_channel.c
index aa4e2731e2ba..71a18c70ecaf 100644
--- a/drivers/net/ethernet/microsoft/mana/hw_channel.c
+++ b/drivers/net/ethernet/microsoft/mana/hw_channel.c
@@ -853,6 +853,7 @@ int mana_hwc_send_request(struct hw_channel_context *hwc, u32 req_len,
struct hwc_caller_ctx *ctx;
u32 dest_vrcq = 0;
u32 dest_vrq = 0;
+ u32 command;
u16 msg_id;
int err;
@@ -877,6 +878,7 @@ int mana_hwc_send_request(struct hw_channel_context *hwc, u32 req_len,
req_msg->req.hwc_msg_id = msg_id;
+ command = req_msg->req.msg_type;
tx_wr->msg_size = req_len;
if (gc->is_pf) {
@@ -893,8 +895,8 @@ int mana_hwc_send_request(struct hw_channel_context *hwc, u32 req_len,
if (!wait_for_completion_timeout(&ctx->comp_event,
(msecs_to_jiffies(hwc->hwc_timeout)))) {
if (hwc->hwc_timeout != 0)
- dev_err(hwc->dev, "HWC: Request timed out: %u ms\n",
- hwc->hwc_timeout);
+ dev_err(hwc->dev, "HWC: Request timed out: %u ms for command 0x%x\n",
+ hwc->hwc_timeout, command);
/* Reduce further waiting if HWC no response */
if (hwc->hwc_timeout > 1)
@@ -914,9 +916,9 @@ int mana_hwc_send_request(struct hw_channel_context *hwc, u32 req_len,
err = -EOPNOTSUPP;
goto out;
}
- if (req_msg->req.msg_type != MANA_QUERY_PHY_STAT)
- dev_err(hwc->dev, "HWC: Failed hw_channel req: 0x%x\n",
- ctx->status_code);
+ if (command != MANA_QUERY_PHY_STAT)
+ dev_err(hwc->dev, "hw_channel command 0x%x failed with status: 0x%x\n",
+ command, ctx->status_code);
err = -EPROTO;
goto out;
}
diff --git a/drivers/net/ethernet/microsoft/mana/mana_en.c b/drivers/net/ethernet/microsoft/mana/mana_en.c
index 91c418097284..09064f9706b8 100644
--- a/drivers/net/ethernet/microsoft/mana/mana_en.c
+++ b/drivers/net/ethernet/microsoft/mana/mana_en.c
@@ -1026,8 +1026,8 @@ static int mana_send_request(struct mana_context *ac, void *in_buf,
if (req->req.msg_type != MANA_QUERY_PHY_STAT &&
mana_need_log(gc, err))
- dev_err(dev, "Failed to send mana message: %d, 0x%x\n",
- err, resp->status);
+ dev_err(dev, "Command 0x%x failed with status: 0x%x, err: %d\n",
+ req->req.msg_type, resp->status, err);
return err ? err : -EPROTO;
}
@@ -1222,6 +1222,9 @@ static int mana_query_device_cfg(struct mana_context *ac, u32 proto_major_ver,
else
*bm_hostmode = 0;
+ dev_info(dev, "Device Config: max_vports=%u adapter_mtu=%u bm_hostmode=%u\n",
+ *max_num_vports, gc->adapter_mtu, *bm_hostmode);
+
debugfs_create_u16("adapter-MTU", 0400, gc->mana_pci_debugfs, &gc->adapter_mtu);
return 0;
@@ -1268,6 +1271,9 @@ static int mana_query_vport_cfg(struct mana_port_context *apc, u32 vport_index,
apc->port_handle = resp.vport;
ether_addr_copy(apc->mac_addr, resp.mac_addr);
+ netdev_info(apc->ndev, "VPort Config: vport=0x%llx max_sq=%u max_rq=%u indir_ent=%u MAC=%pM",
+ apc->port_handle, *max_sq, *max_rq, *num_indir_entry, apc->mac_addr);
+
return 0;
}
@@ -1277,6 +1283,9 @@ void mana_uncfg_vport(struct mana_port_context *apc)
apc->vport_use_count--;
WARN_ON(apc->vport_use_count < 0);
mutex_unlock(&apc->vport_mutex);
+
+ netdev_info(apc->ndev, "Disabled vPort %llu MAC %pM\n",
+ apc->port_handle, apc->mac_addr);
}
EXPORT_SYMBOL_NS(mana_uncfg_vport, "NET_MANA");
@@ -1340,8 +1349,8 @@ int mana_cfg_vport(struct mana_port_context *apc, u32 protection_dom_id,
apc->tx_shortform_allowed = resp.short_form_allowed;
apc->tx_vp_offset = resp.tx_vport_offset;
- netdev_info(apc->ndev, "Configured vPort %llu PD %u DB %u\n",
- apc->port_handle, protection_dom_id, doorbell_pg_id);
+ netdev_info(apc->ndev, "Enabled vPort %llu PD %u DB %u MAC %pM\n",
+ apc->port_handle, protection_dom_id, doorbell_pg_id, apc->mac_addr);
out:
if (err)
mana_uncfg_vport(apc);
@@ -1412,8 +1421,10 @@ static int mana_cfg_vport_steering(struct mana_port_context *apc,
err = -EPROTO;
}
- netdev_info(ndev, "Configured steering vPort %llu entries %u\n",
- apc->port_handle, apc->indir_table_sz);
+ netdev_info(ndev,
+ "Configured steering vPort %llu entries %u MAC %pM [rx:%u rss:%u update_indirection_table:%u cqe_coalescing:%u]\n",
+ apc->port_handle, apc->indir_table_sz, apc->mac_addr,
+ rx, apc->rss_state, update_tab, req->cqe_coalescing_enable);
out:
kfree(req);
return err;
--
2.34.1
^ permalink raw reply related
* Re: [net-next,v15,06/12] selftests/vsock: prepare vm management helpers for namespaces
From: Bobby Eshleman @ 2026-01-21 5:16 UTC (permalink / raw)
To: Jakub Kicinski
Cc: sgarzare, kvm, linux-kernel, mst, horms, bobbyeshleman, corbet,
xuanzhuo, haiyangz, jasowang, linux-hyperv, pabeni, kys,
vishnu.dasa, longli, linux-doc, linux-kselftest, netdev, stefanha,
berrange, decui, bryan-bt.tan, eperezma, wei.liu, davem, shuah,
bcm-kernel-feedback-list, edumazet, virtualization, sargun
In-Reply-To: <20260121025420.1999992-2-kuba@kernel.org>
On Tue, Jan 20, 2026 at 06:54:21PM -0800, Jakub Kicinski wrote:
> In case you need to respin..
>
> This is an AI-generated review of your patch.
> For local reproduction steps see https://netdev-ai.bots.linux.dev/ai-local.html
> ---
> selftests/vsock: prepare vm management helpers for namespaces
>
> This commit adds namespace support to VM management helpers and wrapper
> functions, requiring callers to explicitly pass "init_ns" for the init
> namespace. The commit message states "No functional changes for existing
> tests. All have been updated to pass 'init_ns' explicitly."
>
> > diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selftests/vsock/vmtest.sh
> > index c2bdc293b94c..1d03acb62347 100755
> > --- a/tools/testing/selftests/vsock/vmtest.sh
> > +++ b/tools/testing/selftests/vsock/vmtest.sh
> > @@ -135,7 +135,18 @@ del_namespaces() {
> > }
> >
> > vm_ssh() {
> > - ssh -q -o UserKnownHostsFile=/dev/null -p ${SSH_HOST_PORT} localhost "$@"
> > + local ns_exec
> > +
> > + if [[ "${1}" == init_ns ]]; then
> > + ns_exec=""
> > + else
> > + ns_exec="ip netns exec ${1}"
> > + fi
> > +
> > + shift
> > +
> > + ${ns_exec} ssh -q -o UserKnownHostsFile=/dev/null -p "${SSH_HOST_PORT}" localhost "$@"
> > +
> > return $?
> > }
>
> The vm_ssh() function now expects a namespace as its first argument, but
> run_shared_vm_test() still calls vm_ssh without this parameter:
>
> vm_oops_cnt_before=$(vm_ssh -- dmesg | grep -c -i 'Oops')
> vm_warn_cnt_before=$(vm_ssh -- dmesg --level=warn | grep -c -i 'vsock')
> vm_oops_cnt_after=$(vm_ssh -- dmesg | grep -i 'Oops' | wc -l)
> vm_warn_cnt_after=$(vm_ssh -- dmesg --level=warn | grep -c -i 'vsock')
>
> When called as "vm_ssh -- dmesg", the "--" is interpreted as the namespace
> argument. Since "--" does not equal "init_ns", the function sets
> ns_exec="ip netns exec --" which is invalid. The shift then consumes "--"
> and the command becomes "ip netns exec -- ssh ... dmesg" which will fail.
>
> Interestingly, the v9 version of this patch on lore correctly updated these
> calls to pass "init_ns", but this change appears to have been lost between
> v9 and v15. The fix for this regression is present later in the series in
> commit 3ae984505e00 ("selftests/vsock: add vm_dmesg_{warn,oops}_count()
> helpers").
>
> [ ... ]
Got it, thank you.
Best,
Bobby
^ permalink raw reply
* Re: [PATCH net-next v15 01/12] vsock: add netns to vsock core
From: Stefano Garzarella @ 2026-01-21 14:48 UTC (permalink / raw)
To: Bobby Eshleman
Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Stefan Hajnoczi, Michael S. Tsirkin, Jason Wang,
Eugenio Pérez, Xuan Zhuo, K. Y. Srinivasan, Haiyang Zhang,
Wei Liu, Dexuan Cui, Bryan Tan, Vishnu Dasa,
Broadcom internal kernel review list, Shuah Khan, Long Li,
Jonathan Corbet, linux-kernel, virtualization, netdev, kvm,
linux-hyperv, linux-kselftest, berrange, Sargun Dhillon,
linux-doc, Bobby Eshleman
In-Reply-To: <20260116-vsock-vmtest-v15-1-bbfd1a668548@meta.com>
On Fri, Jan 16, 2026 at 01:28:41PM -0800, Bobby Eshleman wrote:
>From: Bobby Eshleman <bobbyeshleman@meta.com>
>
>Add netns logic to vsock core. Additionally, modify transport hook
>prototypes to be used by later transport-specific patches (e.g.,
>*_seqpacket_allow()).
>
>Namespaces are supported primarily by changing socket lookup functions
>(e.g., vsock_find_connected_socket()) to take into account the socket
>namespace and the namespace mode before considering a candidate socket a
>"match".
>
>This patch also introduces the sysctl /proc/sys/net/vsock/ns_mode to
>report the mode and /proc/sys/net/vsock/child_ns_mode to set the mode
>for new namespaces.
>
>Add netns functionality (initialization, passing to transports, procfs,
>etc...) to the af_vsock socket layer. Later patches that add netns
>support to transports depend on this patch.
nit: maybe we should mention here why we changed the random port
allocation
(not a big deal, only if you need to resend)
>
>dgram_allow(), stream_allow(), and seqpacket_allow() callbacks are
>modified to take a vsk in order to perform logic on namespace modes. In
>future patches, the net will also be used for socket
>lookups in these functions.
>
>Signed-off-by: Bobby Eshleman <bobbyeshleman@meta.com>
>---
>Changes in v15:
>- make static port in __vsock_bind_connectible per-netns
>- remove __net_initdata because we want the ops beyond just boot
>- add vsock_init_ns_mode kernel cmdline parameter to set init ns mode
>- use if (ret || !write) in __vsock_net_mode_string() (Stefano)
>- add vsock_net_mode_global() (Stefano)
>- hide !net == VSOCK_NET_MODE_GLOBAL inside vsock_net_mode() (Stefano)
>- clarify af_vsock.c comments on ns_mode/child_ns_mode (Stefano)
>
>Changes in v14:
>- include linux/sysctl.h in af_vsock.c
>- squash patch 'vsock: add per-net vsock NS mode state' into this patch
> (prior version can be found here):
> https://lore.kernel.org/all/20251223-vsock-vmtest-v13-1-9d6db8e7c80b@meta.com/)
>
>Changes in v13:
>- remove net_mode and replace with direct accesses to net->vsock.mode,
> since this is now immutable.
>- update comments about mode behavior and mutability, and sysctl API
>- only pass NULL for net when wanting global, instead of net_mode ==
> VSOCK_NET_MODE_GLOBAL. This reflects the new logic
> of vsock_net_check_mode() that only requires net pointers (not
> net_mode).
>- refactor sysctl string code into a re-usable function, because
> child_ns_mode and ns_mode both handle the same strings.
>- remove redundant vsock_net_init(&init_net) call in module init because
> pernet registration calls the callback on the init_net too
>
>Changes in v12:
>- return true in dgram_allow(), stream_allow(), and seqpacket_allow()
> only if net_mode == VSOCK_NET_MODE_GLOBAL (Stefano)
>- document bind(VMADDR_CID_ANY) case in af_vsock.c (Stefano)
>- change order of stream_allow() call in vmci so we can pass vsk
> to it
>
>Changes in v10:
>- add file-level comment about what happens to sockets/devices
> when the namespace mode changes (Stefano)
>- change the 'if (write)' boolean in vsock_net_mode_string() to
> if (!write), this simplifies a later patch which adds "goto"
> for mutex unlocking on function exit.
>
>Changes in v9:
>- remove virtio_vsock_alloc_rx_skb() (Stefano)
>- remove vsock_global_dummy_net, not needed as net=NULL +
> net_mode=VSOCK_NET_MODE_GLOBAL achieves identical result
>
>Changes in v7:
>- hv_sock: fix hyperv build error
>- explain why vhost does not use the dummy
>- explain usage of __vsock_global_dummy_net
>- explain why VSOCK_NET_MODE_STR_MAX is 8 characters
>- use switch-case in vsock_net_mode_string()
>- avoid changing transports as much as possible
>- add vsock_find_{bound,connected}_socket_net()
>- rename `vsock_hdr` to `sysctl_hdr`
>- add virtio_vsock_alloc_linear_skb() wrapper for setting dummy net and
> global mode for virtio-vsock, move skb->cb zero-ing into wrapper
>- explain seqpacket_allow() change
>- move net setting to __vsock_create() instead of vsock_create() so
> that child sockets also have their net assigned upon accept()
>
>Changes in v6:
>- unregister sysctl ops in vsock_exit()
>- af_vsock: clarify description of CID behavior
>- af_vsock: fix buf vs buffer naming, and length checking
>- af_vsock: fix length checking w/ correct ctl_table->maxlen
>
>Changes in v5:
>- vsock_global_net() -> vsock_global_dummy_net()
>- update comments for new uAPI
>- use /proc/sys/net/vsock/ns_mode instead of /proc/net/vsock_ns_mode
>- add prototype changes so patch remains c)mpilable
>---
> Documentation/admin-guide/kernel-parameters.txt | 14 +
> MAINTAINERS | 1 +
> drivers/vhost/vsock.c | 6 +-
> include/linux/virtio_vsock.h | 4 +-
> include/net/af_vsock.h | 61 ++++-
> include/net/net_namespace.h | 4 +
> include/net/netns/vsock.h | 21 ++
> net/vmw_vsock/af_vsock.c | 328 ++++++++++++++++++++++--
> net/vmw_vsock/hyperv_transport.c | 7 +-
> net/vmw_vsock/virtio_transport.c | 9 +-
> net/vmw_vsock/virtio_transport_common.c | 6 +-
> net/vmw_vsock/vmci_transport.c | 26 +-
> net/vmw_vsock/vsock_loopback.c | 8 +-
> 13 files changed, 444 insertions(+), 51 deletions(-)
>
>diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
>index a8d0afde7f85..b6e3bfe365a1 100644
>--- a/Documentation/admin-guide/kernel-parameters.txt
>+++ b/Documentation/admin-guide/kernel-parameters.txt
>@@ -8253,6 +8253,20 @@ Kernel parameters
> them quite hard to use for exploits but
> might break your system.
>
>+ vsock_init_ns_mode=
>+ [KNL,NET] Set the vsock namespace mode for the init
>+ (root) network namespace.
>+
>+ global [default] The init namespace operates in
>+ global mode where CIDs are system-wide and
>+ sockets can communicate across global
>+ namespaces.
>+
>+ local The init namespace operates in local mode
>+ where CIDs are private to the namespace and
>+ sockets can only communicate within the same
>+ namespace.
>+
My comment on v14 was more to start a discussion :-) sorry to not be
clear.
I briefly discussed it with Paolo in chat to better understand our
policy between cmdline parameters and module parameters, and it seems
that both are discouraged.
So he asked me if we have a use case for this, and thinking about it, I
don't have one at the moment. Also, if a user decides to set all netns
to local, whether init_net is local or global doesn't really matter,
right?
So perhaps before adding this, we should have a real use case.
Perhaps more than this feature, I would add a way to change the default
of all netns (including init_net) from global to local. But we can do
that later, since all netns have a way to understand what mode they are
in, so we don't break anything and the user has to explicitly change it,
knowing that they are breaking compatibility with pre-netns support.\
That said, at this point, maybe we can remove this, documenting that
init_net is always global, and if we have a use case in the future, we
can add this (or something else) to set the init_net mode (or change the
default for all netns).
Let's wait a bit before next version to wait a comment from Paolo or
Jakub on this. But I'm almost fine with both ways, so:
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
> vt.color= [VT] Default text color.
> Format: 0xYX, X = foreground, Y = background.
> Default: 0x07 = light gray on black.
[...]
>diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
>index a3505a4dcee0..3fc8160d51df 100644
>--- a/net/vmw_vsock/af_vsock.c
>+++ b/net/vmw_vsock/af_vsock.c
[...]
>@@ -235,33 +303,42 @@ static void __vsock_remove_connected(struct
>vsock_sock *vsk)
> sock_put(&vsk->sk);
> }
>
In the v14 I suggested to add some documentation on top of the
vsock_find*() vs vsock_find_*_net() to explain better which one should
be used by transports.
Again is not a big deal, we can fix later if you don't need to resend.
Thanks,
Stefano
>-static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr)
>+static struct sock *__vsock_find_bound_socket_net(struct sockaddr_vm *addr,
>+ struct net *net)
> {
> struct vsock_sock *vsk;
>
> list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table) {
>- if (vsock_addr_equals_addr(addr, &vsk->local_addr))
>- return sk_vsock(vsk);
>+ struct sock *sk = sk_vsock(vsk);
>+
>+ if (vsock_addr_equals_addr(addr, &vsk->local_addr) &&
>+ vsock_net_check_mode(sock_net(sk), net))
>+ return sk;
>
> if (addr->svm_port == vsk->local_addr.svm_port &&
> (vsk->local_addr.svm_cid == VMADDR_CID_ANY ||
>- addr->svm_cid == VMADDR_CID_ANY))
>- return sk_vsock(vsk);
>+ addr->svm_cid == VMADDR_CID_ANY) &&
>+ vsock_net_check_mode(sock_net(sk), net))
>+ return sk;
> }
>
> return NULL;
> }
>
>-static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src,
>- struct sockaddr_vm *dst)
>+static struct sock *
>+__vsock_find_connected_socket_net(struct sockaddr_vm *src,
>+ struct sockaddr_vm *dst, struct net *net)
> {
> struct vsock_sock *vsk;
>
> list_for_each_entry(vsk, vsock_connected_sockets(src, dst),
> connected_table) {
>+ struct sock *sk = sk_vsock(vsk);
>+
> if (vsock_addr_equals_addr(src, &vsk->remote_addr) &&
>- dst->svm_port == vsk->local_addr.svm_port) {
>- return sk_vsock(vsk);
>+ dst->svm_port == vsk->local_addr.svm_port &&
>+ vsock_net_check_mode(sock_net(sk), net)) {
>+ return sk;
> }
> }
>
>@@ -304,12 +381,13 @@ void vsock_remove_connected(struct vsock_sock *vsk)
> }
> EXPORT_SYMBOL_GPL(vsock_remove_connected);
>
>-struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr)
>+struct sock *vsock_find_bound_socket_net(struct sockaddr_vm *addr,
>+ struct net *net)
> {
> struct sock *sk;
>
> spin_lock_bh(&vsock_table_lock);
>- sk = __vsock_find_bound_socket(addr);
>+ sk = __vsock_find_bound_socket_net(addr, net);
> if (sk)
> sock_hold(sk);
>
>@@ -317,15 +395,22 @@ struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr)
>
> return sk;
> }
>+EXPORT_SYMBOL_GPL(vsock_find_bound_socket_net);
>+
>+struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr)
>+{
>+ return vsock_find_bound_socket_net(addr, NULL);
>+}
> EXPORT_SYMBOL_GPL(vsock_find_bound_socket);
>
>-struct sock *vsock_find_connected_socket(struct sockaddr_vm *src,
>- struct sockaddr_vm *dst)
>+struct sock *vsock_find_connected_socket_net(struct sockaddr_vm *src,
>+ struct sockaddr_vm *dst,
>+ struct net *net)
> {
> struct sock *sk;
>
> spin_lock_bh(&vsock_table_lock);
>- sk = __vsock_find_connected_socket(src, dst);
>+ sk = __vsock_find_connected_socket_net(src, dst, net);
> if (sk)
> sock_hold(sk);
>
>@@ -333,6 +418,13 @@ struct sock *vsock_find_connected_socket(struct sockaddr_vm *src,
>
> return sk;
> }
>+EXPORT_SYMBOL_GPL(vsock_find_connected_socket_net);
>+
>+struct sock *vsock_find_connected_socket(struct sockaddr_vm *src,
>+ struct sockaddr_vm *dst)
>+{
>+ return vsock_find_connected_socket_net(src, dst, NULL);
>+}
> EXPORT_SYMBOL_GPL(vsock_find_connected_socket);
>
> void vsock_remove_sock(struct vsock_sock *vsk)
^ permalink raw reply
* [PATCH net-next 1/9] net: benet: convert to use .get_rx_ring_count
From: Breno Leitao @ 2026-01-21 15:54 UTC (permalink / raw)
To: Ajit Khaparde, Sriharsha Basavapatna, Somnath Kotur, Andrew Lunn,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Igor Russkikh, Simon Horman, K. Y. Srinivasan, Haiyang Zhang,
Wei Liu, Dexuan Cui, Long Li, Alexander Duyck, kernel-team,
Edward Cree, Brett Creeley
Cc: netdev, linux-kernel, oss-drivers, linux-hyperv,
linux-net-drivers, Breno Leitao
In-Reply-To: <20260121-grxring_big_v4-v1-0-07655be56bcf@debian.org>
Use the newly introduced .get_rx_ring_count ethtool ops callback instead
of handling ETHTOOL_GRXRINGS directly in .get_rxnfc().
Since ETHTOOL_GRXRINGS was the only command handled by be_get_rxnfc(),
remove the function entirely.
Since the be_multi_rxq() check in be_get_rxnfc() previously blocked RSS
configuration on single-queue setups (via ethtool core validation), add
an equivalent check to be_set_rxfh() to preserve this behavior, as
suggested by Jakub.
Signed-off-by: Breno Leitao <leitao@debian.org>
---
drivers/net/ethernet/emulex/benet/be_ethtool.c | 37 ++++++++++----------------
1 file changed, 14 insertions(+), 23 deletions(-)
diff --git a/drivers/net/ethernet/emulex/benet/be_ethtool.c b/drivers/net/ethernet/emulex/benet/be_ethtool.c
index f55f1fd5d90fd..87dbbd5b7f4e6 100644
--- a/drivers/net/ethernet/emulex/benet/be_ethtool.c
+++ b/drivers/net/ethernet/emulex/benet/be_ethtool.c
@@ -1073,6 +1073,13 @@ static void be_set_msg_level(struct net_device *netdev, u32 level)
adapter->msg_enable = level;
}
+static u32 be_get_rx_ring_count(struct net_device *netdev)
+{
+ struct be_adapter *adapter = netdev_priv(netdev);
+
+ return adapter->num_rx_qs;
+}
+
static int be_get_rxfh_fields(struct net_device *netdev,
struct ethtool_rxfh_fields *cmd)
{
@@ -1117,28 +1124,6 @@ static int be_get_rxfh_fields(struct net_device *netdev,
return 0;
}
-static int be_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
- u32 *rule_locs)
-{
- struct be_adapter *adapter = netdev_priv(netdev);
-
- if (!be_multi_rxq(adapter)) {
- dev_info(&adapter->pdev->dev,
- "ethtool::get_rxnfc: RX flow hashing is disabled\n");
- return -EINVAL;
- }
-
- switch (cmd->cmd) {
- case ETHTOOL_GRXRINGS:
- cmd->data = adapter->num_rx_qs;
- break;
- default:
- return -EINVAL;
- }
-
- return 0;
-}
-
static int be_set_rxfh_fields(struct net_device *netdev,
const struct ethtool_rxfh_fields *cmd,
struct netlink_ext_ack *extack)
@@ -1293,6 +1278,12 @@ static int be_set_rxfh(struct net_device *netdev,
u8 *hkey = rxfh->key;
u8 rsstable[RSS_INDIR_TABLE_LEN];
+ if (!be_multi_rxq(adapter)) {
+ dev_info(&adapter->pdev->dev,
+ "ethtool::set_rxfh: RX flow hashing is disabled\n");
+ return -EINVAL;
+ }
+
/* We do not allow change in unsupported parameters */
if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
rxfh->hfunc != ETH_RSS_HASH_TOP)
@@ -1441,7 +1432,7 @@ const struct ethtool_ops be_ethtool_ops = {
.get_ethtool_stats = be_get_ethtool_stats,
.flash_device = be_do_flash,
.self_test = be_self_test,
- .get_rxnfc = be_get_rxnfc,
+ .get_rx_ring_count = be_get_rx_ring_count,
.get_rxfh_fields = be_get_rxfh_fields,
.set_rxfh_fields = be_set_rxfh_fields,
.get_rxfh_indir_size = be_get_rxfh_indir_size,
--
2.47.3
^ permalink raw reply related
page: next (older) | prev (newer) | latest
- recent:[subjects (threaded)|topics (new)|topics (active)]
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox