Skip to contentSkip to conversation

PCI passthru on Hyper-V

PATCH Superseded Mukesh R 3 participants 27 messages Jul 18 – Jul 28

Subsystems (6)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Mukesh R

author
#0 lore raw

v4 cover PCI passthru on Hyper-V

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. This supports both full device passthru and SR-IOV based VFs.

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

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

Testing:
o Most testing done on hyperv-next:a4ffc59238be using Cloud Hypervisor (51).
o All tests involved passthru of devices using MSIx.
o Tested on Baremetal dom0/root:
test 1: Mellanox ConnectX-6 Lx passthru
test 2: NVIDIA Tesla Tesla T4 GPU.
test 3: Both of above simultaneous passthru
o VF: Mellanox ConnectX-6 Lx passthru

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

Changes in V4:
o Completely remove direct device attach (DDA) support
o Remove first two patches that were submitted separately and are already merged.
o Change hv_iommu_unmap_pages() to unmap in batches of HV_REP_COUNT_MAX, otherwise if a physical range happens to be larger than that, there would be errors from the hypercall.
o Use large page (2M) flag for mmio mappings as internal testing found that to speed things up greatly.
o rename hv_no_movbl_pgs to hv_memreg in last patch.

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

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

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

             patch 11.

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

Thanks,
-Mukesh

Mukesh R (9):
mshv: Provide a way to get partition ID if running in a VMM process
mshv: Add declarations and definitions for VFIO-MSHV bridge device
mshv: Introduce basic mshv bridge device for VFIO to build upon
mshv: Add ioctl support for MSHV-VFIO bridge device
mshv: Import data structs around device passthru from hyperv headers
PCI: hv: Export hv_build_devid_type_pci() and change return type x86/hyperv: Implement Hyper-V virtual IOMMU
mshv: Populate mmio mappings for PCI passthru
mshv: Disable movable regions upfront if device passthru

 arch/x86/hyperv/irqdomain.c          |   9 +-
 arch/x86/include/asm/mshyperv.h      |   6 +
 arch/x86/kernel/pci-dma.c            |   2 +
 drivers/hv/Makefile                  |   3 +-
 drivers/hv/mshv_root.h               |  24 +-
 drivers/hv/mshv_root_hv_call.c       | 101 +++-
 drivers/hv/mshv_root_main.c          | 259 +++++++++-
 drivers/hv/mshv_vfio.c               | 211 ++++++++
 drivers/iommu/Kconfig                |   1 +
 drivers/iommu/hyperv/Kconfig         |  15 +
 drivers/iommu/hyperv/Makefile        |   1 +
 drivers/iommu/hyperv/hv-iommu-root.c | 704 +++++++++++++++++++++++++++
 include/asm-generic/mshyperv.h       |   8 +
 include/hyperv/hvgdk_mini.h          |   8 +
 include/hyperv/hvhdk_mini.h          |  67 +++
 include/linux/hyperv.h               |   6 +
 include/uapi/linux/mshv.h            |  31 ++
 17 files changed, 1408 insertions(+), 48 deletions(-)
 create mode 100644 drivers/hv/mshv_vfio.c
 create mode 100644 drivers/iommu/hyperv/Kconfig
 create mode 100644 drivers/iommu/hyperv/hv-iommu-root.c
Signature · 1 line
2.51.2.vfs.0.1
author
#1 lore raw

PATCH v4 1/9 mshv: Provide a way to get partition ID if running in a VMM process

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

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

Re: [PATCH V4 1/9] mshv: Provide a way to get partition ID if running in a VMM process

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] Using global current->tgid to track and authorize partition ownership introduces a PID reuse vulnerability and breaks support for VMMs managing multiple partitions.
- [High] mshv_current_partid() returns a raw partition ID without acquiring a reference, leading to a TOCTOU race where the partition can be freed and its ID reused.

Pre-existing issues:
- [High] VPs are freed before their parent partition is unlinked from the RCU hash table, causing a Use-After-Free for concurrent RCU readers. --

--- Patch [1]: [PATCH V4 1/9] mshv: Provide a way to get partition ID if running in a VMM process --- commit ce04f426ae72519f5c20b1bad8ffd34a07810200
Author: Mukesh R <[email protected]>

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

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

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

Quoted 7 lines
drivers/hv/mshv_root_main.c +0 -0
index 146726cc4e9ba..f1ff45a77b42c 100644
@@ -1908,6 +1908,27 @@ mshv_partition_release(struct inode *inode, struct file *filp)
 	return 0;
 }

[Severity: High]
This is a pre-existing issue, but there appears to be a use-after-free condition when a partition is being destroyed.

In destroy_partition(), child mshv_vp objects are freed while the parent partition remains linked in the RCU-protected mshv_root.pt_htable:

destroy_partition() {
    ...
    kfree(vp);
    partition->pt_vp_array[i] = NULL;
    ...
    remove_partition(partition);
}

If a concurrent asynchronous interrupt (like handle_bitset_message) looks up the partition via mshv_partition_find() and accesses the VP before remove_partition() unlinks the partition and waits for a grace period, could the ISR call kick_vp(vp) on freed memory?

Quoted 5 lines
drivers/hv/mshv_root_main.c +5 -0
+/* Given a process tgid, return partition id if it is a VMM process */
+u64 mshv_current_partid(void)
+{
+	struct mshv_partition *pt;
+	int i;

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

Quoted 10 lines
drivers/hv/mshv_root_main.c +10 -0
+	u64 ret_ptid = HV_PARTITION_ID_INVALID;
+
+	rcu_read_lock();
+
+	hash_for_each_rcu(mshv_root.pt_htable, i, pt, pt_hnode) {
+		if (pt->pt_vmm_tgid == current->tgid) {
+			ret_ptid = pt->pt_id;
+			break;
+		}
+	}

[Severity: High]
Is it safe to track partition ownership using a global transient identifier like current->tgid instead of an explicit file descriptor or reference-counted token?

If a single VMM process creates multiple partitions, wouldn't mshv_current_partid() arbitrarily return the first one found in the hash table, breaking PCI passthru operations for all subsequent partitions?

Furthermore, could PID recycling lead to a situation where a new VMM process accidentally matches an old partition whose fd was passed to a long-running process that outlived the original creator?

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

Quoted 3 lines
drivers/hv/mshv_root_main.c +3 -0
+
+	rcu_read_unlock();
+	return ret_ptid;

[Severity: High]
Does returning the raw pt_id here without acquiring a reference to the partition object introduce a race condition?

Once the RCU read lock is dropped, a concurrent thread could close the partition fd, causing destroy_partition() to delete it in the hypervisor.

If the hypervisor reuses this ID for a newly created partition belonging to a different VMM, could the original caller's subsequent PCI passthru hypercall mistakenly target the unrelated partition?

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

Re: [PATCH V4 1/9] mshv: Provide a way to get partition ID if running in a VMM process

In reply to Mukesh R

Hi Mukesh,

Quoted 60 lines · On Fri, 17 Jul 2026 19:19:41 -0700 Mukesh R <[email protected]> wrote:

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

---
 drivers/hv/mshv_root.h         |  1 +
 drivers/hv/mshv_root_main.c    | 22 ++++++++++++++++++++++
 include/asm-generic/mshyperv.h |  5 +++++
 3 files changed, 28 insertions(+)
drivers/hv/mshv_root.h +1 -0
index 1f086dcb7aa1..a85c24dcc701 100644
@@ -138,6 +138,7 @@ struct mshv_partition {
 
 	struct mshv_girq_routing_table __rcu *pt_girq_tbl;
 	u64 isolation_type;
+	pid_t pt_vmm_tgid;
 	bool import_completed;
 	bool pt_initialized;
 #if IS_ENABLED(CONFIG_DEBUG_FS)
drivers/hv/mshv_root_main.c +20 -0
index bd1359eb58dd..02c107458be9 100644
@@ -1908,6 +1908,27 @@ mshv_partition_release(struct inode *inode,
 struct file *filp) return 0;
 }
 
+/* Given a process tgid, return partition id if it is a VMM process
 */ +u64 mshv_current_partid(void)
+{
+	struct mshv_partition *pt;
+	int i;
+	u64 ret_ptid = HV_PARTITION_ID_INVALID;
+
+	rcu_read_lock();
+
+	hash_for_each_rcu(mshv_root.pt_htable, i, pt, pt_hnode) {
+		if (pt->pt_vmm_tgid == current->tgid) {
+			ret_ptid = pt->pt_id;
+			break;
+		}
+	}
+
+	rcu_read_unlock();
+	return ret_ptid;
+}
+EXPORT_SYMBOL_GPL(mshv_current_partid);
+
 static int
 add_partition(struct mshv_partition *partition)
 {

drivers/hv/mshv_root_main.c mshv_ioctl_create_partition(void __user · line 2097

Quoted 4 lines
drivers/hv/mshv_root_main.c +1 -0
@@ -2073,6 +2094,7 @@ mshv_ioctl_create_partition(void __user
 *user_arg, struct device *module_dev) goto cleanup_irq_srcu;
 
 	partition->pt_id = pt_id;
+	partition->pt_vmm_tgid = current->tgid;

I wonder how robust this mechanism is to identify target partition via tgid.
1) what prevents a VMM process create more than one partition? in that case each partition would have the same tgid.
2) IIUC, the lifetime of the partition is tied to FD, which is different than the lifetime of a PID. The partition FD can be inherited or passed to another process. The VMM tg can exit while the FDs can be alive. Then the tgid can be reused by another unrelated process, right?

Would it be more robust to based this on the partition FD instead of tgid?

Quoted 25 lines
drivers/hv/mshv_root_main.c +0 -0
 	ret = add_partition(partition);
 	if (ret)
(quoted diff) +0 -0

b/include/asm-generic/mshyperv.h index bf601d67cecb..e8cbc4e3f7ad 100644 --- a/include/asm-generic/mshyperv.h
+++ b/include/asm-generic/mshyperv.h

(quoted diff) +0 -0
@@ -350,6 +350,7 @@ int hv_call_add_logical_proc(int node, u32

lp_index, u32 acpi_id); int hv_call_notify_all_processors_started(void); bool hv_lp_exists(u32 lp_index); int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags); +u64 mshv_current_partid(void);

#else /* CONFIG_MSHV_ROOT */
static inline bool hv_root_partition(void) { return false; }

include/asm-generic/mshyperv.h +4 -0
@@ -380,6 +381,10 @@ static inline int hv_call_create_vp(int node,
 u64 partition_id, u32 vp_index, u3 {
 	return -EOPNOTSUPP;
 }
+static inline u64 mshv_current_partid(void)
+{
+	return HV_PARTITION_ID_INVALID;
+}
 #endif /* CONFIG_MSHV_ROOT */
 
 static inline int hv_deposit_memory(u64 partition_id, u64 status)
author
#4 lore raw

Re: [PATCH V4 1/9] mshv: Provide a way to get partition ID if running in a VMM process

Quoted 1 line · Jacob Pan

Hi Mukesh,

Hey Jacob, pl see inline..

Quoted 2 lines

On Fri, 17 Jul 2026 19:19:41 -0700
Mukesh R <[email protected]> wrote:

... snip...

Quoted 6 lines
Quoted 6 lines
  static int
  add_partition(struct mshv_partition *partition)
  {
(quoted diff) +0 -0
@@ -2073,6 +2094,7 @@ mshv_ioctl_create_partition(void __user
 *user_arg, struct device *module_dev) goto cleanup_irq_srcu;
 
Quoted 6 lines

drivers/hv/mshv_root_main.c mshv_ioctl_create_partition(void __user *user_arg, struct device *module_dev) · line 2097

Quoted 2 lines
drivers/hv/mshv_root_main.c +1 -0
  	partition->pt_id = pt_id;
+	partition->pt_vmm_tgid = current->tgid;

I wonder how robust this mechanism is to identify target partition via tgid.
1) what prevents a VMM process create more than one partition? in that case each partition would have the same tgid.

Currently, none of the VMMs we support do that, and doesn't look like there is much of a demand for it.

Quoted 5 lines

2) IIUC, the lifetime of the partition is tied to FD, which is different than the lifetime of a PID. The partition FD can be inherited or passed to another process. The VMM tg can exit while the FDs can be alive. Then the tgid can be reused by another unrelated process, right?

yeah, AI keeps telling me that, but not super accurate imo.

we are using tgid and not pid. tgid is process group id, and that will stay around as long as there is at least one process in it. if we used pid, then that would be the case.

Quoted 2 lines

Would it be more robust to based this on the partition FD instead of tgid?

it might be, but problem with that is we need pt-id in other cases where that is not available: for example in hv_iommu_domain_alloc_paging and in irq remapping paths for direct attached devices. if we can sort that out somehow, then we can do that. but i suspect, it would take some time to figure that out, so i hope we can make that a future enhancement.

For now, i've been thinking of just putting a check and returning ENOTSUPP if a vmm tries to create another partition.

Thanks,
-Mukesh

Quoted 25 lines
Quoted 25 lines
drivers/hv/mshv_root_main.c +0 -0
  	ret = add_partition(partition);
  	if (ret)
(quoted diff) +0 -0

b/include/asm-generic/mshyperv.h index bf601d67cecb..e8cbc4e3f7ad 100644 --- a/include/asm-generic/mshyperv.h
+++ b/include/asm-generic/mshyperv.h

(quoted diff) +0 -0
@@ -350,6 +350,7 @@ int hv_call_add_logical_proc(int node, u32

lp_index, u32 acpi_id); int hv_call_notify_all_processors_started(void); bool hv_lp_exists(u32 lp_index); int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags); +u64 mshv_current_partid(void);

#else /* CONFIG_MSHV_ROOT */
static inline bool hv_root_partition(void) { return false; }

include/asm-generic/mshyperv.h +4 -0
@@ -380,6 +381,10 @@ static inline int hv_call_create_vp(int node,
 u64 partition_id, u32 vp_index, u3 {
  	return -EOPNOTSUPP;
  }
+static inline u64 mshv_current_partid(void)
+{
+	return HV_PARTITION_ID_INVALID;
+}
  #endif /* CONFIG_MSHV_ROOT */
 
  static inline int hv_deposit_memory(u64 partition_id, u64 status)
#5 lore raw

Re: [PATCH V4 1/9] mshv: Provide a way to get partition ID if running in a VMM process

Hi Mukesh,

Quoted 23 lines · On Fri, 24 Jul 2026 12:14:08 -0700 Mukesh R <[email protected]> wrote:
Quoted 1 line · Jacob Pan

Hi Mukesh,

Hey Jacob, pl see inline..

Quoted 2 lines

On Fri, 17 Jul 2026 19:19:41 -0700
Mukesh R <[email protected]> wrote:

   ... snip...
Quoted 6 lines
Quoted 6 lines
  static int
  add_partition(struct mshv_partition *partition)
  {
(quoted diff) +0 -0
@@ -2073,6 +2094,7 @@ mshv_ioctl_create_partition(void __user
 *user_arg, struct device *module_dev) goto cleanup_irq_srcu;
 
Quoted 6 lines

drivers/hv/mshv_root_main.c mshv_ioctl_create_partition(void __user *user_arg, struct device *module_dev) · line 2097

Quoted 2 lines
drivers/hv/mshv_root_main.c +1 -0
  	partition->pt_id = pt_id;
+	partition->pt_vmm_tgid = current->tgid;  

I wonder how robust this mechanism is to identify target partition via tgid.
1) what prevents a VMM process create more than one partition? in that case each partition would have the same tgid.

Quoted 3 lines

Currently, none of the VMMs we support do that, and doesn't look like there is much of a demand for it.

My point is that from kernel UAPI pov, we cannot count on user behavior nor current VMM's implementation.

Quoted 6 lines
Quoted 5 lines

2) IIUC, the lifetime of the partition is tied to FD, which is different than the lifetime of a PID. The partition FD can be inherited or passed to another process. The VMM tg can exit while the FDs can be alive. Then the tgid can be reused by another unrelated process, right?

Quoted 6 lines

yeah, AI keeps telling me that, but not super accurate imo.

we are using tgid and not pid. tgid is process group id, and that will stay around as long as there is at least one process in it. if we used pid, then that would be the case.

I understand it is tgid but don't think tgid makes difference in terms of FD lifetime.
e.g. process A creates a partition, then passes the partition fd to process B over a Unix socket. Process A can then exit while process B still holds a reference to the partition file. At that point the tgid that was stored in pt_vmm_tgid can be reused by an unrelated process C, while the partition's file object remains alive.

Quoted 10 lines
Quoted 2 lines

Would it be more robust to based this on the partition FD instead of tgid?

it might be, but problem with that is we need pt-id in other cases where that is not available: for example in hv_iommu_domain_alloc_paging and in irq remapping paths for direct attached devices. if we can sort that out somehow, then we can do that. but i suspect, it would take some time to figure that out, so i hope we can make that a future enhancement.

Quoted 3 lines

For now, i've been thinking of just putting a check and returning ENOTSUPP if a vmm tries to create another partition.

IMHO, that only solves the uniqueness issue of partition ID but not the lifetime issue.
maybe EEXIST instead of ENOTSUPP?

Quoted 29 lines

Thanks,
-Mukesh

Quoted 26 lines
Quoted 26 lines
drivers/hv/mshv_root_main.c +0 -0
  	ret = add_partition(partition);
  	if (ret)
(quoted diff) +0 -0

b/include/asm-generic/mshyperv.h index bf601d67cecb..e8cbc4e3f7ad 100644 --- a/include/asm-generic/mshyperv.h
+++ b/include/asm-generic/mshyperv.h

(quoted diff) +0 -0
@@ -350,6 +350,7 @@ int hv_call_add_logical_proc(int node, u32

lp_index, u32 acpi_id); int hv_call_notify_all_processors_started(void); bool hv_lp_exists(u32 lp_index); int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags); +u64 mshv_current_partid(void);

#else /* CONFIG_MSHV_ROOT */
static inline bool hv_root_partition(void) { return false; }

include/asm-generic/mshyperv.h +4 -0
@@ -380,6 +381,10 @@ static inline int hv_call_create_vp(int node,
 u64 partition_id, u32 vp_index, u3 {
  	return -EOPNOTSUPP;
  }
+static inline u64 mshv_current_partid(void)
+{
+	return HV_PARTITION_ID_INVALID;
+}
  #endif /* CONFIG_MSHV_ROOT */
 
  static inline int hv_deposit_memory(u64 partition_id, u64

status)

author
#6 lore raw

Re: [PATCH V4 1/9] mshv: Provide a way to get partition ID if running in a VMM process

Quoted 28 lines · Jacob Pan

Hi Mukesh,

Quoted 23 lines · On Fri, 24 Jul 2026 12:14:08 -0700 Mukesh R <[email protected]> wrote:
Quoted 1 line · Jacob Pan

Hi Mukesh,

Hey Jacob, pl see inline..

Quoted 2 lines

On Fri, 17 Jul 2026 19:19:41 -0700
Mukesh R <[email protected]> wrote:

    ... snip...
Quoted 12 lines

drivers/hv/mshv_root_main.c mshv_ioctl_create_partition(void __user · line 2097

Quoted 8 lines
   static int
   add_partition(struct mshv_partition *partition)
   {
drivers/hv/mshv_root_main.c +1 -0
@@ -2073,6 +2094,7 @@ mshv_ioctl_create_partition(void __user
 *user_arg, struct device *module_dev) goto cleanup_irq_srcu;
 
   	partition->pt_id = pt_id;
+	partition->pt_vmm_tgid = current->tgid;

I wonder how robust this mechanism is to identify target partition via tgid.
1) what prevents a VMM process create more than one partition? in that case each partition would have the same tgid.

Quoted 5 lines
Quoted 3 lines

Currently, none of the VMMs we support do that, and doesn't look like there is much of a demand for it.

My point is that from kernel UAPI pov, we cannot count on user behavior nor current VMM's implementation.

True, but as UAPI implementors, I thought we could list its limitations. For example: an fwrite() implementation can say if you don't fflush(), fwrite() can lose data!

Quoted 12 lines
Quoted 6 lines
Quoted 5 lines

2) IIUC, the lifetime of the partition is tied to FD, which is different than the lifetime of a PID. The partition FD can be inherited or passed to another process. The VMM tg can exit while the FDs can be alive. Then the tgid can be reused by another unrelated process, right?

Quoted 6 lines

yeah, AI keeps telling me that, but not super accurate imo.

we are using tgid and not pid. tgid is process group id, and that will stay around as long as there is at least one process in it. if we used pid, then that would be the case.

Quoted 7 lines

I understand it is tgid but don't think tgid makes difference in terms of FD lifetime.
e.g. process A creates a partition, then passes the partition fd to process B over a Unix socket. Process A can then exit while process B still holds a reference to the partition file. At that point the tgid that was stored in pt_vmm_tgid can be reused by an unrelated process C, while the partition's file object remains alive.

Confused whether you are saying that C should still be able to manage the VM without errors, or that C could cause harm because it has access to possibly recycled pt_vmm_tgid. If latter, C cannot use pt_vmm_tgid in harmful way because:

        if (pt->pt_vmm_tgid == current->tgid) {
                ret_ptid = pt->pt_id;

        else return : HV_PARTITION_ID_INVALID

So, because C has different tgid, it will return HV_PARTITION_ID_INVALID causing hypercalls to fail with invalid parameter.

Quoted 10 lines
Quoted 10 lines
Quoted 2 lines

Would it be more robust to based this on the partition FD instead of tgid?

it might be, but problem with that is we need pt-id in other cases where that is not available: for example in hv_iommu_domain_alloc_paging and in irq remapping paths for direct attached devices. if we can sort that out somehow, then we can do that. but i suspect, it would take some time to figure that out, so i hope we can make that a future enhancement.

Quoted 6 lines
Quoted 3 lines

For now, i've been thinking of just putting a check and returning ENOTSUPP if a vmm tries to create another partition.

IMHO, that only solves the uniqueness issue of partition ID but not the lifetime issue.
maybe EEXIST instead of ENOTSUPP?

EEXIST: A VM already exists for this process group
ENOTSUPP: more than one VM is not supported for a proc group

I think either are ok, but if you prefer EEXIST, that's fine too.

Quoted 29 lines
Quoted 29 lines

Thanks,
-Mukesh

Quoted 26 lines
Quoted 26 lines
drivers/hv/mshv_root_main.c +0 -0
   	ret = add_partition(partition);
   	if (ret)
(quoted diff) +0 -0

b/include/asm-generic/mshyperv.h index bf601d67cecb..e8cbc4e3f7ad 100644 --- a/include/asm-generic/mshyperv.h
+++ b/include/asm-generic/mshyperv.h

(quoted diff) +0 -0
@@ -350,6 +350,7 @@ int hv_call_add_logical_proc(int node, u32

lp_index, u32 acpi_id); int hv_call_notify_all_processors_started(void); bool hv_lp_exists(u32 lp_index); int hv_call_create_vp(int node, u64 partition_id, u32

vp_index, u32 flags); +u64 mshv_current_partid(void);
   
   #else /* CONFIG_MSHV_ROOT */
   static inline bool hv_root_partition(void) { return false; }
include/asm-generic/mshyperv.h +4 -0
@@ -380,6 +381,10 @@ static inline int hv_call_create_vp(int node,
 u64 partition_id, u32 vp_index, u3 {
   	return -EOPNOTSUPP;
   }
+static inline u64 mshv_current_partid(void)
+{
+	return HV_PARTITION_ID_INVALID;
+}
   #endif /* CONFIG_MSHV_ROOT */
 
   static inline int hv_deposit_memory(u64 partition_id, u64

status)

author
#7 lore raw

PATCH v4 2/9 mshv: Add declarations and definitions for VFIO-MSHV bridge device

In reply to Mukesh R

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

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

Re: [PATCH V4 2/9] mshv: Add declarations and definitions for VFIO-MSHV bridge device

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] The UAPI ioctl command macro for MSHV_CREATE_DEVICE is missing. - [Medium] The ops structure…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The UAPI ioctl command macro for MSHV_CREATE_DEVICE is missing.
- [Medium] The ops structure mshv_vfio_device_ops is not declared as const. --

--- Patch [2]: [PATCH V4 2/9] mshv: Add declarations and definitions for VFIO-MSHV bridge device --- commit dbad27877cef927b38632a1b77ab492ceeebae34
Author: Mukesh R <[email protected]>

mshv: Add declarations and definitions for VFIO-MSHV bridge device

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

Quoted 4 lines
drivers/hv/mshv_root.h +0 -0
index a85c24dcc701a..b9880d0bdc4df 100644

[ ... ]

drivers/hv/mshv_root.h struct port_table_info { · line 247

Quoted 11 lines
drivers/hv/mshv_root.h +11 -0
+struct mshv_device_ops {
+	const char *device_name;
+	long (*device_create)(struct mshv_device *dev);
+	void (*device_release)(struct mshv_device *dev);
+	long (*device_set_attr)(struct mshv_device *dev,
+				struct mshv_device_attr *attr);
+	long (*device_has_attr)(struct mshv_device *dev,
+				struct mshv_device_attr *attr);
+};
+
+extern struct mshv_device_ops mshv_vfio_device_ops;

[Severity: Medium]
Should mshv_vfio_device_ops be declared as const?

Operations structures containing function pointers are typically placed in read-only memory as a security hardening measure. Missing the const keyword on the extern declaration prevents the future definition from being const without triggering compilation errors.

Quoted 4 lines
include/uapi/linux/mshv.h +0 -0
index 32ff92b6342b2..be6fe3ee87078 100644

[ ... ]

Quoted 18 lines
include/uapi/linux/mshv.h +18 -0
+/* Device passhthru */
+#define MSHV_CREATE_DEVICE_TEST		1
+
+enum {
+	MSHV_DEV_TYPE_VFIO,
+	MSHV_DEV_TYPE_MAX,
+};
+
+struct mshv_create_device {
+	__u32	type;	     /* in: MSHV_DEV_TYPE_xxx */
+	__u32	fd;	     /* out: device handle */
+	__u32	flags;	     /* in: MSHV_CREATE_DEVICE_xxx */
+};
+
+#define MSHV_DEV_VFIO_FILE      1
+#define MSHV_DEV_VFIO_FILE_ADD	1
+#define MSHV_DEV_VFIO_FILE_DEL	2
+

include/uapi/linux/mshv.h struct mshv_sint_mask { · line 435

Quoted 10 lines
include/uapi/linux/mshv.h +10 -0
+struct mshv_device_attr {
+	__u32	flags;		/* no flags currently defined */
+	__u32	group;		/* device-defined */
+	__u64	attr;		/* group-defined */
+	__u64	addr;		/* userspace address of attr data */
+};
+
+/* Device fds created with MSHV_CREATE_DEVICE */
+#define MSHV_SET_DEVICE_ATTR	_IOW(MSHV_IOCTL, 0x00, struct mshv_device_attr)
+#define MSHV_HAS_DEVICE_ATTR	_IOW(MSHV_IOCTL, 0x01, struct mshv_device_attr)

[Severity: High]
Is the UAPI ioctl command macro for MSHV_CREATE_DEVICE missing from this header?

The struct mshv_create_device is defined for the ioctl payload, and the comments reference MSHV_CREATE_DEVICE, but the actual ioctl command macro appears to be absent. Without this definition, userspace cannot compile or invoke the ioctl to create a device.

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

PATCH v4 3/9 mshv: Introduce basic mshv bridge device for VFIO to build upon

In reply to Mukesh R

Add a new file to implement basic 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. This is a basic version to build upon.

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

211 lines not on this page · raw message

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

Re: [PATCH V4 3/9] mshv: Introduce basic mshv bridge device for VFIO to build upon

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The MSHV_CREATE_DEVICE ioctl handler is missing in the core MSHV framework, rendering the…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The MSHV_CREATE_DEVICE ioctl handler is missing in the core MSHV framework, rendering the VFIO-MSHV bridge device completely unreachable. --

--- Patch [3]: [PATCH V4 3/9] mshv: Introduce basic mshv bridge device for VFIO to build upon --- commit 58c1a16396f01cf3f0ff1eb2d7b11c08f90ca678
Author: Mukesh R <[email protected]>

mshv: Introduce basic mshv bridge device for VFIO to build upon

This commit introduces a basic VFIO-MSHV bridge pseudo device based on the VFIO-KVM bridge. It adds the mshv_vfio.c implementation and a new UAPI ioctl MSHV_CREATE_DEVICE.

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

[ ... ]

drivers/hv/mshv_vfio.c line 211

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

[Severity: Medium]
Since the ioctl to create this device is missing from the core ioctl handlers, does this device operations struct and the rest of the VFIO-MSHV bridge implementation become unreachable dead code?

include/uapi/linux/mshv.h struct mshv_root_hvcall { · line 257

Quoted 9 lines
include/uapi/linux/mshv.h +1 -0
index be6fe3ee87078..b038a79786d2d 100644
@@ -254,6 +254,7 @@ struct mshv_root_hvcall {
 #define MSHV_GET_GPAP_ACCESS_BITMAP	_IOWR(MSHV_IOCTL, 0x06, struct mshv_gpap_access_bitmap)
 /* Generic hypercall */
 #define MSHV_ROOT_HVCALL		_IOWR(MSHV_IOCTL, 0x07, struct mshv_root_hvcall)
+#define MSHV_CREATE_DEVICE             _IOWR(MSHV_IOCTL, 0x08, struct mshv_create_device)

[Severity: Medium]
Is this ioctl actually wired up anywhere in the core MSHV framework?

I can't find an implementation for MSHV_CREATE_DEVICE in any ioctl dispatcher (like mshv_partition_ioctl()). Will userspace simply receive -ENOTTY when attempting to use this command?

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

PATCH v4 4/9 mshv: Add ioctl support for MSHV-VFIO bridge device

In reply to Mukesh R

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

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

Re: [PATCH V4 4/9] mshv: Add ioctl support for MSHV-VFIO bridge device

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] File descriptor and associated kernel resources leak on `copy_to_user` error path. - [Medium] The…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] File descriptor and associated kernel resources leak on copy_to_user error path.
- [Medium] The MSHV_CREATE_DEVICE_TEST flag check completely ignores other unknown flags, breaking forward compatibility. --

--- Patch [4]: [PATCH V4 4/9] mshv: Add ioctl support for MSHV-VFIO bridge device --- commit 4db495dcb20f06e9a8bed00e54250347faa9f23f
Author: Mukesh R <[email protected]>

mshv: Add ioctl support for MSHV-VFIO bridge device

This commit adds ioctl support for creating MSHV devices for a partition. It currently supports VFIO device types and performs initialization for the bridge code. The implementation includes file operations and partition ioctl handlers.

Quoted 4 lines
drivers/hv/mshv_root_main.c +0 -0
index f1ff45a77b42c..56422f801ee2f 100644

[ ... ]

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

[ ... ]

Quoted 10 lines
drivers/hv/mshv_root_main.c +10 -0
+static long mshv_partition_ioctl_create_device(struct mshv_partition *partition,
+					       void __user *uarg)
+{
+	long rc;
+	struct mshv_create_device devargk;
+	struct mshv_device *mshv_dev;
+	const struct mshv_device_ops *vfio_ops;
+
+	if (copy_from_user(&devargk, uarg, sizeof(devargk)))
+		return -EFAULT;

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

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

[Severity: Medium]
Does this logic break forward compatibility by ignoring unknown flags when the test bit is set?

If userspace queries support for a future flag by passing:
devargk.flags = MSHV_CREATE_DEVICE_TEST | FUTURE_FLAG

The kernel will immediately return 0. This could mislead userspace into believing the flag is supported. However, when userspace attempts to use the flag without the test bit, the kernel will reject it.

Would it be better to check for unknown flags before checking the test flag?

[ ... ]

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

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

[Severity: High]
Can this leak the file descriptor and associated resources if copy_to_user fails?

The file descriptor is installed into the process fd table via anon_inode_getfd(). If the subsequent copy_to_user() fails, the kernel returns -EFAULT, but userspace does not receive the allocated fd number and cannot close it.

Should this approach use get_unused_fd_flags() and anon_inode_getfile(), delaying fd_install() until after copy_to_user() succeeds?

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

Re: [PATCH V4 4/9] mshv: Add ioctl support for MSHV-VFIO bridge device

In reply to Mukesh R

Hi Mukesh,

Quoted 12 lines · On Fri, 17 Jul 2026 19:19:44 -0700 Mukesh R <[email protected]> wrote:
(quoted diff) +1 -0
+	hlist_add_head(&mshv_dev->device_ptnode,

&partition->pt_devices); +

(quoted diff) +2 -0
+	mshv_partition_get(partition);
+	rc = anon_inode_getfd(vfio_ops->device_name,

&mshv_device_fops,

drivers/hv/mshv_root_main.c +7 -0
+			      mshv_dev, O_RDWR | O_CLOEXEC);
+	if (rc < 0)
+		goto undo_out;
+
+	devargk.fd = rc;
+	if (copy_to_user(uarg, &devargk, sizeof(devargk)))
+		return -EFAULT;    /* cleanup in
Quoted 1 line

mshv_device_fop_release() */ +

In failure, user never gets the fd, so it never close it. We are leaking fd until process exit, right?

Maybe we should do anon_inode_getfile(...) and do fd_install() only if copy_to_user succeeds.

Thanks,

Jacob

author
#14 lore raw

Re: [PATCH V4 4/9] mshv: Add ioctl support for MSHV-VFIO bridge device

Quoted 21 lines · Jacob Pan

Hi Mukesh,

Quoted 12 lines · On Fri, 17 Jul 2026 19:19:44 -0700 Mukesh R <[email protected]> wrote:
(quoted diff) +1 -0
+	hlist_add_head(&mshv_dev->device_ptnode,

&partition->pt_devices); +

(quoted diff) +2 -0
+	mshv_partition_get(partition);
+	rc = anon_inode_getfd(vfio_ops->device_name,

&mshv_device_fops,

drivers/hv/mshv_root_main.c +7 -0
+			      mshv_dev, O_RDWR | O_CLOEXEC);
+	if (rc < 0)
+		goto undo_out;
+
+	devargk.fd = rc;
+	if (copy_to_user(uarg, &devargk, sizeof(devargk)))
+		return -EFAULT;    /* cleanup in
Quoted 1 line

mshv_device_fop_release() */ +

In failure, user never gets the fd, so it never close it. We are leaking fd until process exit, right?

Quoted 6 lines

Maybe we should do anon_inode_getfile(...) and do fd_install() only if copy_to_user succeeds.

Thanks,

Jacob

Hey,

right, but efault should result in sigsegv and immediate exit... unless some rogue vmm is trapping it and doing something malicous in which case all it can do is fill up only its own file descriptor table.

given that the above is same as kvm_ioctl_create_device(), i think it is ok to leave as is. if you think both need changing, lmk.

Thanks,
-Mukesh

author
#15 lore raw

PATCH v4 5/9 mshv: Import data structs around device passthru from hyperv headers

In reply to Mukesh R

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

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

71 lines not on this page · raw message

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

Re: [PATCH V4 5/9] mshv: Import data structs around device passthru from hyperv headers

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The memory layout of `union hv_device_domain_id` is inverted compared to the expected Hyper-V ABI…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The memory layout of union hv_device_domain_id is inverted compared to the expected Hyper-V ABI, resulting in corrupted device domain IDs being passed to the hypervisor. --

--- Patch [5]: [PATCH V4 5/9] mshv: Import data structs around device passthru from hyperv headers --- commit 02ed2cd292e85f0b6e754b6a29df473f95394315
Author: Mukesh R <[email protected]>

mshv: Import data structs around device passthru from hyperv headers

This commit copies and imports definitions and declarations from Hyper-V public headers. These additions are related to creating domains in the hypervisor and attaching devices to them.

Quoted 4 lines
include/hyperv/hvhdk_mini.h +0 -0
index b4cb2fa26e9bc..90497635c3cbd 100644

[ ... ]

Quoted 7 lines
include/hyperv/hvhdk_mini.h +7 -0
+/* 3 domain types: stage 1, stage 2, and SOC */
+#define HV_DEVICE_DOMAIN_TYPE_S2  0 /* HV_DEVICE_DOMAIN_ID_TYPE_S2 */
+#define HV_DEVICE_DOMAIN_TYPE_S1  1 /* HV_DEVICE_DOMAIN_ID_TYPE_S1 */
+#define HV_DEVICE_DOMAIN_TYPE_SOC 2 /* HV_DEVICE_DOMAIN_ID_TYPE_SOC */
+
+/* ID for stage 2 default domain and NULL domain */
+#define HV_DEVICE_DOMAIN_ID_S2_DEFAULT 0

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

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

[Severity: High]
Is the memory layout of union hv_device_domain_id inverted compared to the expected Hyper-V ABI?

The macro HV_DEVICE_DOMAIN_ID_S2_NULL is defined as 0xFFFFFFFFULL, which places 0xFFFFFFFF in the lower 32 bits and 0 in the upper 32 bits. Given that Stage 2 (S2) domains have a type of 0, this macro implies that the id should be in the lower 32 bits and the type should be in the upper 32 bits.

However, union hv_device_domain_id defines type as the first bitfield (occupying the lower 32 bits) and id as the second (occupying the upper 32 bits).

Under this layout, casting 0xFFFFFFFFULL evaluates to type = 15 and id = 0, instead of the expected type = 0 and id = 0xFFFFFFFF. Could this cause malformed domain IDs to be sent to the hypervisor?

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

Re: [PATCH V4 5/9] mshv: Import data structs around device passthru from hyperv headers

Quoted 34 lines · On 7/17/26 19:30, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The memory layout of union hv_device_domain_id is inverted compared to the expected Hyper-V ABI, resulting in corrupted device domain IDs being passed to the hypervisor. --

--- Patch [5]: [PATCH V4 5/9] mshv: Import data structs around device passthru from hyperv headers --- commit 02ed2cd292e85f0b6e754b6a29df473f95394315
Author: Mukesh R <[email protected]>

mshv: Import data structs around device passthru from hyperv headers

This commit copies and imports definitions and declarations from Hyper-V public headers. These additions are related to creating domains in the hypervisor and attaching devices to them.

Quoted 4 lines
include/hyperv/hvhdk_mini.h +0 -0
index b4cb2fa26e9bc..90497635c3cbd 100644

[ ... ]

Quoted 13 lines
include/hyperv/hvhdk_mini.h +13 -0
+/* 3 domain types: stage 1, stage 2, and SOC */
+#define HV_DEVICE_DOMAIN_TYPE_S2  0 /* HV_DEVICE_DOMAIN_ID_TYPE_S2 */
+#define HV_DEVICE_DOMAIN_TYPE_S1  1 /* HV_DEVICE_DOMAIN_ID_TYPE_S1 */
+#define HV_DEVICE_DOMAIN_TYPE_SOC 2 /* HV_DEVICE_DOMAIN_ID_TYPE_SOC */
+
+/* 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;
Quoted 8 lines

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

Quoted 4 lines
include/hyperv/hvhdk_mini.h +4 -0
+		u32 reserved : 28;
+		u32 id;
+	};
+} __packed;

[Severity: High]
Is the memory layout of union hv_device_domain_id inverted compared to the expected Hyper-V ABI?

No.

Quoted 4 lines

The macro HV_DEVICE_DOMAIN_ID_S2_NULL is defined as 0xFFFFFFFFULL, which places 0xFFFFFFFF in the lower 32 bits and 0 in the upper 32 bits. Given that Stage 2 (S2) domains have a type of 0, this macro implies that the id should be in the lower 32 bits and the type should be in the upper 32 bits.

HV_DEVICE_DOMAIN_ID_S2_NULL is never assigned to any data struct of type "union hv_device_domain_id".

Quoted 8 lines

However, union hv_device_domain_id defines type as the first bitfield (occupying the lower 32 bits) and id as the second (occupying the upper 32 bits).

Under this layout, casting 0xFFFFFFFFULL evaluates to type = 15 and id = 0, instead of the expected type = 0 and id = 0xFFFFFFFF. Could this cause malformed domain IDs to be sent to the hypervisor?

author
#18 lore raw

PATCH v4 6/9 PCI: hv: Export hv_build_devid_type_pci() and change return type

In reply to Mukesh R

On Hyper-V, most hypercalls related to PCI passthru to map/unmap regions, interrupts, etc need a device ID as a parameter. This device ID refers to that specific device during the lifetime of passthru. So, make hv_build_devid_type_pci() public and change return type to u64 to enforce it's size.

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

Re: [PATCH V4 6/9] PCI: hv: Export hv_build_devid_type_pci() and change return type

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines

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. So, make hv_build_devid_type_pci() public and change return type to u64 to enforce it's size.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH v4 7/9 x86/hyperv: Implement Hyper-V virtual IOMMU

In reply to Mukesh R

Add a new file to implement a virtual IOMMU that works with Microsoft Hyper-V hypervisor. The hypervisor claims the IOMMU upon boot, and this driver communicates with it for creating and deleting paging domains, mapping and unmapping of pages, etc. The default domain type is IDENTITY. Lastly, this is only supported on baremetal or nested root/dom0, and not L1VH root/dom0.

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

704 lines not on this page · raw message

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

Re: [PATCH V4 7/9] x86/hyperv: Implement Hyper-V virtual IOMMU

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider: - [High] Partial IOMMU unmaps corrupt the interval tree state and silently leak DMA mappings in the…

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [High] Partial IOMMU unmaps corrupt the interval tree state and silently leak DMA mappings in the hypervisor, allowing potential DMA use-after-free.
- [High] hv_iommu_domain_alloc_paging unconditionally rejects domain allocation if the calling process is not an active VMM, breaking VFIO for host applications.
- [High] The hv_iommu_map_pages callback ignores the caller-provided gfp flags and implicitly uses GFP_KERNEL, risking sleep-in-atomic panics.
- [High] Unsafe interval tree iteration during node removal breaks RB-tree invariants and causes memory corruption or skipped nodes.
- [Medium] The pci_devs_to_skip string parsing logic incorrectly aborts on a successful parse, completely breaking the hv_iommu_skip= boot parameter.
- [Medium] hv_iommu_domain_free does not free remaining hv_iommu_mapping nodes in the mappings_tree, leading to a memory leak.
- [Medium] Failure in hv_iommu_attach_dev leaves the hardware detached while failing to restore the device to its previous domain.
--

--- Patch [7]: [PATCH V4 7/9] x86/hyperv: Implement Hyper-V virtual IOMMU --- commit eedf8d68fd442834b8bd7d597c6c152d43c81395
Author: Mukesh R <[email protected]>
Subject: x86/hyperv: Implement Hyper-V virtual IOMMU

This commit adds a new virtual IOMMU driver for Microsoft Hyper-V. The hypervisor claims the IOMMU upon boot, and this driver communicates with it to create and delete paging domains, map and unmap pages, and handle default identity domains.

Quoted 4 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +0 -0
new file mode 100644

[ ... ]

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

Quoted 9 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +9 -0
+static struct iommu_domain *hv_iommu_domain_alloc_paging(struct device *dev)
+{
+	struct hv_domain *hvdom;
+	int rc;
+	u32 unique_id;
+	u64 ptid = hv_get_current_partid();
+
+	if (ptid == HV_PARTITION_ID_INVALID)
+		return NULL;

[Severity: High]
The commit message explicitly mentions support for host apps like SPDK, but could this check unintentionally reject domain allocation for them?

Since host applications like SPDK are standard userspace processes rather than mshv VMM threads, hv_get_current_partid() will return HV_PARTITION_ID_INVALID, which blocks the allocation entirely.

[ ... ]

Quoted 19 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +19 -0
+static void hv_iommu_domain_free(struct iommu_domain *immdom)
+{
+	unsigned long flags;
+	u64 status;
+	struct hv_input_delete_device_domain *input;
+	struct hv_input_device_domain *ddp;
+	struct hv_domain *hvdom = to_hv_domain(immdom);
+
+	if (hv_special_domain(hvdom))
+		return;
+
+	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;

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

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+
+	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);
+}

[Severity: Medium]
When an IOMMU domain is destroyed, does this leave the dynamically allocated nodes inside hvdom->mappings_tree leaked?

The function frees the hvdom structure but there does not appear to be a cleanup loop iterating over and freeing the struct hv_iommu_mapping objects left in the tree.

[ ... ]

Quoted 21 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +21 -0
+static int hv_iommu_attach_dev(struct iommu_domain *immdom, struct device *dev,
+			       struct iommu_domain *old)
+{
+	struct pci_dev *pdev;
+	int rc;
+	struct hv_domain *hvdom_new = to_hv_domain(immdom);
+	struct hv_domain *hvdom_prev = to_hv_domain(old);
+
+	/* Only allow PCI devices for now */
+	if (!dev_is_pci(dev))
+		return -EINVAL;
+
+	pdev = to_pci_dev(dev);
+
+	/* There are no 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 && !hv_special_domain(hvdom_prev))
+		hv_iommu_detach_dev(hvdom_prev, dev);

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

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+
+	rc = hv_iommu_att_dev2dom(hvdom_new, pdev);
+
+	if (rc == 0)
+		dev_iommu_priv_set(dev, hvdom_new);  /* sets "private" field */
+	else
+		dev_iommu_priv_set(dev, NULL);
+
+	return rc;
+}

[Severity: Medium]
If the device successfully detaches from the old domain but the new attach hypercall fails, is the hardware left in limbo?

Since there is no rollback to re-attach the device to the previous domain, the subsystem logic might believe the device is safely detached and handle the error, while the hardware is left without an assigned domain.

[ ... ]

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

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+static int hv_iommu_add_tree_mapping(struct hv_domain *hvdom,
+				     unsigned long iova, phys_addr_t paddr,
+				     size_t size, u32 flags)
+{
+	unsigned long irqflags;
+	struct hv_iommu_mapping *mapping;
+
+	mapping = kzalloc_obj(struct hv_iommu_mapping);
+	if (!mapping)
+		return -ENOMEM;

[Severity: High]
Can this allocation trigger a panic by sleeping in atomic context?

The kzalloc_obj macro implicitly uses a GFP_KERNEL allocation by default. The caller, hv_iommu_map_pages(), receives a gfp_t parameter specifically because DMA mapping operations can be called with GFP_ATOMIC inside interrupt handlers, but that parameter is discarded before calling this function.

[ ... ]

Quoted 5 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +5 -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;

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

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -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);

[Severity: High]
Is it safe to use a cached next pointer for interval tree iteration after modifying the tree?

Calling interval_tree_remove() later in this loop restructures the underlying RB-tree and changes parent and child relationships. Continuing the iteration with a pre-cached pointer on the mutated tree might break the iterator's traversal logic, leading to skipped nodes or memory corruption.

Quoted 4 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +4 -0
+
+		/* Trying to split a mapping? Not supported for now. */
+		if (mapping->iova.start < iova)
+			break;

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

Quoted 10 lines
drivers/iommu/hyperv/hv-iommu-root.c new file +10 -0
+
+		unmapped += mapping->iova.last - mapping->iova.start + 1;
+
+		interval_tree_remove(node, &hvdom->mappings_tree);
+		kfree(mapping);
+	}
+	spin_lock_irqrestore(&hvdom->mappings_lock, flags);
+
+	return unmapped;
+}

[Severity: High]
What happens if a caller unmaps a subset of a mapping?

If the unmap starts exactly at the IOVA, hv_iommu_del_tree_mappings() removes the entire node from the tree and returns the node's full size. Then, hv_iommu_unmap_pages() only unmaps the originally requested size from the hypervisor.

This appears to permanently lose the remainder of the mapping from the tree while leaving it mapped in the hypervisor, causing a desync and potential DMA memory leak.

[ ... ]

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

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

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

[Severity: Medium]
Does this condition inadvertently terminate the loop on a successful parse?

The sscanf function returns the number of successfully matched items. If the configuration string is parsed correctly, rc will be greater than zero and the loop will immediately break before checking the parsed fields or matching the devices to skip.

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

PATCH v4 8/9 mshv: Populate mmio mappings for PCI passthru

In reply to Mukesh R

Upon guest accesses, in case of missing mmio mappings, the hypervisor generates unmapped gpa intercepts. 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). Try to use 2M page size as much as possible for significant performance gains.

Also, remove existing code that is using vma->vm_pgoff to map mmio space, it is broken and will cause panics.

---
 drivers/hv/mshv_root.h         |   3 +-
 drivers/hv/mshv_root_hv_call.c | 101 ++++++++++++++++++++++++--------
 drivers/hv/mshv_root_main.c    | 103 +++++++++++++++++++++++++++------
 3 files changed, 165 insertions(+), 42 deletions(-)
drivers/hv/mshv_root.h +2 -1
index b9880d0bdc4d..c97a1b5da9ad 100644
@@ -301,7 +301,8 @@ int hv_call_create_partition(u64 flags,
 int hv_call_initialize_partition(u64 partition_id);
 int hv_call_finalize_partition(u64 partition_id);
 int hv_call_delete_partition(u64 partition_id);
-int hv_call_map_mmio_pages(u64 partition_id, u64 gfn, u64 mmio_spa, u64 numpgs);
+int hv_map_mmio_pages(u64 partition_id, struct mshv_mem_region *reg, u64 gfn,
+		      u64 mmio_mfn);
 int hv_call_map_gpa_pages(u64 partition_id, u64 gpa_target, u64 page_count,
 			  u32 flags, struct page **pages);
 int hv_call_unmap_gpa_pages(u64 partition_id, u64 gpa_target, u64 page_count,
drivers/hv/mshv_root_hv_call.c +77 -24

146 lines not on this page · raw message

drivers/hv/mshv_root_main.c +86 -17

139 lines not on this page · raw message

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

Re: [PATCH V4 8/9] mshv: Populate mmio mappings for PCI passthru

Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:

New issues:
- [Critical] hv_map_mmio_pages extrapolates the physical base address for an entire MMIO region from a single page fault, allowing unauthorized arbitrary physical memory access.
- [High] Flawed alignment logic allows mapping unaligned addresses as 2M large pages and fails to iterate the physical frame variable.
- [High] Unconditional use of FAULT_FLAG_WRITE in mshv_chk_get_mmio_start_pfn breaks read-only MMIO mappings.
- [High] Missing cleanup of partial mappings in hv_map_mmio_pages on failure.
- [High] Resolving user space PFNs without synchronizing with MMU notifiers introduces a TOCTOU race.

Pre-existing issues:
- [Critical] Guest MMIO mappings are never torn down when host userspace unmaps the VMA or detaches the device.
- [High] Synchronous, unbounded loop over page_is_ram for the entire region size can cause soft lockups.
- [High] Error handling rollback in hv_do_map_gpa_hcall fails to unmap pages due to a mismatched page count unit. --

--- Patch [8]: [PATCH V4 8/9] mshv: Populate mmio mappings for PCI passthru --- commit 65da49ecfde6e092de4c941dffc7d00deae59b78
Author: Mukesh R <[email protected]>

mshv: Populate mmio mappings for PCI passthru

This commit handles unmapped GPA intercepts by resolving the PCI resource pfn for a guest gpa and dynamically asking the hypervisor to map it via hypercalls. It attempts to use 2M page sizes where possible and removes the old vma->vm_pgoff mapping approach.

Quoted 3 lines
drivers/hv/mshv_root_hv_call.c +0 -0

[ ... ]

Quoted 18 lines
drivers/hv/mshv_root_hv_call.c +3 -7
@@ -189,31 +189,33 @@ int hv_call_delete_partition(u64 partition_id)
 }
 
 /* Ask the hypervisor to map guest ram pages or the guest mmio space */
-static int hv_do_map_gpa_hcall(u64 partition_id, u64 gfn, u64 page_struct_count,
-			       u32 flags, struct page **pages, u64 mmio_spa)
+static int hv_do_map_gpa_hcall(u64 partition_id, u64 gfn, u64 page_count,
+			       u32 flags, struct page **pages, u64 mmio_mfn)
 {
 	struct hv_input_map_gpa_pages *input_page;
 	u64 status, *pfnlist;
 	unsigned long irq_flags, large_shift = 0;
-	int ret = 0, done = 0;
-	u64 page_count = page_struct_count;
-
-	if (page_count == 0 || (pages && mmio_spa))
-		return -EINVAL;
+	int i, ret = 0, done = 0;

drivers/hv/mshv_root_hv_call.c int hv_call_delete_partition(u64 partition_id) · line 206

Quoted 10 lines
drivers/hv/mshv_root_hv_call.c +6 -2
+	u64 adj_page_count = page_count;
 
-	if (flags & HV_MAP_GPA_LARGE_PAGE) {
-		if (mmio_spa)
+	if (mmio_mfn) {
+		if (pages)
 			return -EINVAL;
+		for (i = 0; i < page_count; i++)
+			if (page_is_ram(mmio_mfn + i))
+				return -EINVAL;

[Severity: High]
This is a pre-existing issue, but could this synchronous loop cause soft lockups?

If a user maps a multi-gigabyte MMIO region (like a large GPU BAR), this loops page_count times calling page_is_ram() synchronously for every 4KB page without preemption, which could trigger kernel soft lockup watchdogs.

[ ... ]

drivers/hv/mshv_root_hv_call.c static int hv_do_map_gpa_hcall(u64 partition_id, u64 gfn, u64 page_struct_count, · line 265

Quoted 7 lines
drivers/hv/mshv_root_hv_call.c +3 -0
 		} else if (!hv_result_success(status)) {
+			pr_err("%s: failed to map pages at gfn %#llx: completed %u/%llu, flags=%#x, status=%#llx (%s)\n",
+			       __func__, gfn, done, page_count, flags, status,
+			       hv_result_to_string(hv_result(status)));
 			ret = hv_result_to_errno(status);
 			break;
 		}

[Severity: High]
This isn't a bug introduced by this patch, but does the error rollback at the end of hv_do_map_gpa_hcall() pass the wrong unit to hv_call_unmap_gpa_pages()?

If an error occurs during mapping and HV_MAP_GPA_LARGE_PAGE is set, "done" contains the number of mapped 2M pages. This is passed to hv_call_unmap_gpa_pages(), which expects a 4K page count:

hv_do_map_gpa_hcall() {
    ...
    if (ret && done) {
        u32 unmap_flags = 0;
        if (flags & HV_MAP_GPA_LARGE_PAGE)
            unmap_flags |= HV_UNMAP_GPA_LARGE_PAGE;
        hv_call_unmap_gpa_pages(partition_id, gfn, done, unmap_flags);
    }
}

Since hv_call_unmap_gpa_pages() expects 4K pages, it evaluates !HV_PAGE_COUNT_2M_ALIGNED(done), which fails for small integers and aborts with -EINVAL, leaking the memory mappings in the hypervisor's page tables.

[ ... ]

Quoted 13 lines
drivers/hv/mshv_root_hv_call.c +7 -5
-/* Ask the hypervisor to map guest mmio space */
-int hv_call_map_mmio_pages(u64 partition_id, u64 gfn, u64 mmio_spa, u64 numpgs)
+/*
+ * Ask the hypervisor to map guest mmio space. Don't set HV_MAP_GPA_NOT_CACHED
+ * in hcall flags for slightly better performance, and in that case the guest
+ * stage 1 page table will control caching.
+ */
+int hv_map_mmio_pages(u64 partition_id, struct mshv_mem_region *reg, u64 gfn,
+		      u64 mmio_mfn)
 {
-	int i;
-	u32 flags = HV_MAP_GPA_READABLE | HV_MAP_GPA_WRITABLE |
-		    HV_MAP_GPA_NOT_CACHED;

drivers/hv/mshv_root_hv_call.c int hv_call_map_gpa_pages(u64 partition_id, u64 gpa_target, u64 page_count, · line 306

Quoted 10 lines
drivers/hv/mshv_root_hv_call.c +10 -0
+	int rc;
+	u32 flags = HV_MAP_GPA_READABLE;
+	u64 hpages, numpgs, numpgs_in_hpage = HPAGE_SIZE / PAGE_SIZE;
+
+	if (reg->hv_map_flags & HV_MAP_GPA_WRITABLE)
+		flags |= HV_MAP_GPA_WRITABLE;
+	if (reg->hv_map_flags & HV_MAP_GPA_EXECUTABLE)
+		flags |= HV_MAP_GPA_EXECUTABLE;
+
+	mmio_mfn = mmio_mfn - (gfn - reg->start_gfn);	/* start of the range */

[Severity: Critical]
Does extrapolating the physical base address from a single page fault allow mapping arbitrary physical memory?

If a user creates a large MSHV region backed by a sparse or smaller VM_PFNMAP VMA, mshv_handle_unmapped_gpa() resolves a single PFN. This extrapolation then calculates the base PFN for the entire region and maps reg->nr_pages of contiguous physical memory into the guest. This would map the holes or unrelated physical memory (like other devices' MSI-X tables or kernel RAM), bypassing VFIO's memory protections.

drivers/hv/mshv_root_hv_call.c int hv_call_map_gpa_pages(u64 partition_id, u64 gpa_target, u64 page_count, · line 315

Quoted 9 lines
drivers/hv/mshv_root_hv_call.c +9 -0
+
+	numpgs = 0;
+	gfn = reg->start_gfn;
+	while (!HV_PAGE_COUNT_2M_ALIGNED(gfn) &&
+	       !HV_PAGE_COUNT_2M_ALIGNED(mmio_mfn) &&
+	       numpgs < reg->nr_pages) {
+		numpgs++;
+		gfn++;
+	}

[Severity: High]
Does this loop condition prematurely exit, and are we failing to iterate mmio_mfn?

If gfn and mmio_mfn have different 2M offsets, the logical AND causes the loop to exit as soon as one of them is aligned. Furthermore, mmio_mfn is never incremented inside this loop, which leaves it misaligned when it is later passed to the large page mapping request.

Quoted 13 lines
drivers/hv/mshv_root_hv_call.c +13 -0
+
+	if (numpgs) {
+		rc = hv_do_map_gpa_hcall(partition_id, reg->start_gfn, numpgs,
+					 flags, NULL, mmio_mfn);
+		if (rc || numpgs == reg->nr_pages)
+			return rc;
+	}
+
+	mmio_mfn = mmio_mfn + numpgs;
+	numpgs = reg->nr_pages - numpgs;
+
+	if (numpgs < numpgs_in_hpage)
+		return hv_do_map_gpa_hcall(partition_id, gfn, numpgs, flags,

drivers/hv/mshv_root_hv_call.c int hv_call_map_gpa_pages(u64 partition_id, u64 gpa_target, u64 page_count, · line 338

Quoted 10 lines
drivers/hv/mshv_root_hv_call.c +10 -0
+					   NULL, mmio_mfn);
+
+	for (hpages = 0; numpgs >= numpgs_in_hpage;) {
+		hpages++;
+		numpgs = numpgs - numpgs_in_hpage;
+	}
+	rc = hv_do_map_gpa_hcall(partition_id, gfn, hpages * numpgs_in_hpage,
+				 flags | HV_MAP_GPA_LARGE_PAGE, NULL, mmio_mfn);
+	if (rc)
+		return rc;

[Severity: High]
Are we leaking partial mappings here if the hypercall batch fails?

If the initial batch of 4K pages succeeds but this second batch for 2M pages fails, the function returns the error code directly. This skips cleaning up the pages mapped during the first hv_do_map_gpa_hcall(), leaking those mappings in the hypervisor.

Quoted 3 lines
drivers/hv/mshv_root_main.c +0 -0

[ ... ]

Quoted 10 lines
drivers/hv/mshv_root_main.c +10 -0
+static int mshv_chk_get_mmio_start_pfn(u64 uaddr, u64 *mmio_pfnp)
+{
+	struct vm_area_struct *vma;
+	bool is_mmio;
+	struct follow_pfnmap_args pfnmap_args;
+	int rc = -EINVAL;
+
+	mmap_read_lock(current->mm);
+	vma = vma_lookup(current->mm, uaddr);
+	is_mmio = vma ? !!(vma->vm_flags & (VM_IO | VM_PFNMAP)) : 0;

drivers/hv/mshv_root_main.c mshv_partition_region_by_gfn_get(struct mshv_partition *p, u64 gfn) · line 667

Quoted 10 lines
drivers/hv/mshv_root_main.c +10 -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);

[Severity: High]
Does this unconditional use of FAULT_FLAG_WRITE break read-only MMIO mappings?

When resolving an unmapped GPA intercept for a read-only VMA, the fixup_user_fault() call will fail with -EFAULT because the VMA lacks write permissions, completely denying the guest access to read-only MMIO regions.

Quoted 23 lines
drivers/hv/mshv_root_main.c +23 -0
+		if (rc)
+			goto unlock_mmap_out;
+
+		rc = follow_pfnmap_start(&pfnmap_args);
+		if (rc)
+			goto unlock_mmap_out;
+	}
+
+	*mmio_pfnp = pfnmap_args.pfn;
+	follow_pfnmap_end(&pfnmap_args);
+
+unlock_mmap_out:
+	mmap_read_unlock(current->mm);
+	return rc;
+}
+
+/*
+ * Check if the unmapped gpa belongs to mmio space. If yes, resolve it.
+ *
+ * Returns: True if valid mmio intercept and handled, else false.
+ */
+static bool mshv_handle_unmapped_gpa(struct mshv_vp *vp)
+{

[ ... ]

drivers/hv/mshv_root_main.c mshv_partition_region_by_gfn_get(struct mshv_partition *p, u64 gfn) · line 718

Quoted 5 lines
drivers/hv/mshv_root_main.c +5 -0
+	rc = mshv_chk_get_mmio_start_pfn(uaddr, &mmio_mfn);
+	if (rc)
+		goto put_rg_out;
+
+	rc = hv_map_mmio_pages(pt->pt_id, rg, gfn, mmio_mfn);

[Severity: High]
Is there a TOCTOU race here when mapping the resolved PFN?

mshv_chk_get_mmio_start_pfn() resolves the PFN and drops mmap_read_lock before returning. There is no synchronization with the MMU interval notifier sequence lock prior to calling hv_map_mmio_pages(). If the VMA is unmapped concurrently after the lock is dropped, could the hypervisor map a stale or freed PFN into the guest?

[ ... ]

drivers/hv/mshv_root_main.c mshv_map_user_memory(struct mshv_partition *partition, · line 1454

Quoted 12 lines
drivers/hv/mshv_root_main.c +1 -5
@@ -1376,11 +1449,7 @@ mshv_map_user_memory(struct mshv_partition *partition,
 					    region->nr_pages,
 					    HV_MAP_GPA_NO_ACCESS, NULL);
 		break;
-	case MSHV_REGION_TYPE_MMIO:
-		ret = hv_call_map_mmio_pages(partition->pt_id,
-					     region->start_gfn,
-					     mmio_pfn,
-					     region->nr_pages);
+	default:
 		break;
 	}

[Severity: Critical]
This is a pre-existing issue, but are guest MMIO mappings ever torn down when host userspace unmaps the VMA or detaches the device?

Because MMIO regions fall into the default case, they skip mshv_prepare_pinned_region() and do not register MMU interval notifiers. When the host userspace calls munmap() on the VFIO region or the device is detached, the guest's stage-2 page tables are never invalidated, leaving the guest with arbitrary access to reassigned physical memory.

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

Re: [PATCH V4 8/9] mshv: Populate mmio mappings for PCI passthru

Quoted 7 lines · On 7/17/26 19:33, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:

New issues:
- [Critical] hv_map_mmio_pages extrapolates the physical base address for an entire MMIO region from a single page fault, allowing unauthorized arbitrary physical memory access.

- [High] Flawed alignment logic allows mapping unaligned addresses as 2M large pages and fails to iterate the physical frame variable.
- [High] Unconditional use of FAULT_FLAG_WRITE in mshv_chk_get_mmio_start_pfn breaks read-only MMIO mappings.

Above 2 addressed in next version.

Thanks,
-Mukesh

... snip ...

author
#25 lore raw

PATCH v4 9/9 mshv: Disable movable regions upfront if device passthru

In reply to Mukesh R

If a VM is started with device attached, the mem regions must be marked non-movable as the device attach hypercall right away allows the use of SLAT for IOMMU. Marking them non-movable forces mapping of the entire guest RAM in the SLAT at the time of region creation along with the region pinned. Also, because a device could be dynamically attached much later in a VM, create a boot parameter to allow a user to pin them upfront if they anticipate such an action. It is understood that VFIO pins the regions also, so there will be double pinning. This will be enhanced in the near future with a third region type that is not movable nor pinned.

---
 drivers/hv/mshv_root.h      |  1 +
 drivers/hv/mshv_root_main.c | 15 ++++++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
drivers/hv/mshv_root.h +1 -0
index c97a1b5da9ad..0282f489ad90 100644
@@ -141,6 +141,7 @@ struct mshv_partition {
 	pid_t pt_vmm_tgid;
 	bool import_completed;
 	bool pt_initialized;
+	bool pt_regions_pinned;
 #if IS_ENABLED(CONFIG_DEBUG_FS)
 	struct dentry *pt_stats_dentry;
 	struct dentry *pt_vp_dentry;
drivers/hv/mshv_root_main.c +14 -1
index 6bce4123c5ff..d7cc131652af 100644
@@ -46,6 +46,10 @@ MODULE_DESCRIPTION("Microsoft Hyper-V root partition VMM interface /dev/mshv");
 #define HV_VP_COUNTER_ROOT_DISPATCH_THREAD_BLOCKED 95
 #endif
 
+static uint hv_memreg;  /* mshv_mem_region allocations type */
+module_param(hv_memreg, uint, 0644);
+MODULE_PARM_DESC(hv_memreg, "0: movable (default), 1: pinned");
+
 struct mshv_root mshv_root;
 
 enum hv_scheduler_type hv_scheduler_type;
@@ -1293,6 +1297,12 @@ static void mshv_async_hvcall_handler(void *data, u64 *status)
 	*status = partition->async_hypercall_status;
 }
 
+static bool mshv_do_pt_regions_pinned(struct mshv_partition *pt)
+{
+	return pt->pt_regions_pinned || mshv_partition_encrypted(pt) ||
+	       hv_memreg == 1;
+}
+
 /*
  * NB: caller checks and makes sure mem->size is page aligned
  * Returns: 0 with regionpp updated on success, or -errno
@@ -1323,7 +1333,7 @@ static int mshv_partition_create_region(struct mshv_partition *partition,
 
 	if (is_mmio)
 		rg->mreg_type = MSHV_REGION_TYPE_MMIO;
-	else if (mshv_partition_encrypted(partition) ||
+	else if (mshv_do_pt_regions_pinned(partition) ||
 		 !mshv_region_movable_init(rg))
 		rg->mreg_type = MSHV_REGION_TYPE_MEM_PINNED;
 	else
@@ -1801,6 +1811,9 @@ static long mshv_partition_ioctl_create_device(struct mshv_partition *partition,
 	if (copy_to_user(uarg, &devargk, sizeof(devargk)))
 		return -EFAULT;    /* cleanup in mshv_device_fop_release() */
 
+	/* For now, just mark regions pinned if there is device passthru. */
+	partition->pt_regions_pinned = true;
+
 	return 0;
 
 undo_out:
Signature · 1 line
2.51.2.vfs.0.1
#26 lore raw

Re: [PATCH V4 9/9] mshv: Disable movable regions upfront if device passthru

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Malicious userspace can bypass memory pinning enforcement by forcing copy_to_user() to fail.…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Malicious userspace can bypass memory pinning enforcement by forcing copy_to_user() to fail.
- [Low] Module parameter hv_memreg is exposed as runtime-writable despite being designed as a boot parameter. --

--- Patch [9]: [PATCH V4 9/9] mshv: Disable movable regions upfront if device passthru --- commit c768ee85b3efb1c981444ab02e4e4d365693795e
Author: Mukesh R <[email protected]>

mshv: Disable movable regions upfront if device passthru

If a VM is started with a device attached, the memory regions must be marked non-movable since the device attach hypercall allows the use of SLAT for IOMMU. This patch marks them non-movable upfront upon device passthru and adds a boot parameter to allow a user to pin them manually if anticipated.

drivers/hv/mshv_root_main.c MODULE_DESCRIPTION("Microsoft Hyper-V root partition VMM interface /dev/mshv"); · line 50

Quoted 10 lines
drivers/hv/mshv_root_main.c +2 -0
index 58cf61cb227ba..b085fa13574f0 100644
@@ -46,6 +46,10 @@ MODULE_DESCRIPTION("Microsoft Hyper-V root partition VMM interface /dev/mshv");
 #define HV_VP_COUNTER_ROOT_DISPATCH_THREAD_BLOCKED 95
 #endif
 
+static uint hv_memreg;  /* mshv_mem_region allocations type */
+module_param(hv_memreg, uint, 0644);

[Severity: Low]
The commit message states this is a "boot parameter". Is it intentional that this module parameter is created with 0644 permissions? This allows root to change the allocation policy at runtime, whereas 0444 would correctly enforce its boot-only nature.

Quoted 3 lines
drivers/hv/mshv_root_main.c +2 -0
+MODULE_PARM_DESC(hv_memreg, "0: movable (default), 1: pinned");
+
 struct mshv_root mshv_root;

[ ... ]

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

Quoted 3 lines
drivers/hv/mshv_root_main.c +0 -0
@@ -1801,6 +1811,9 @@ static long mshv_partition_ioctl_create_device(struct mshv_partition *partition,
 	if (copy_to_user(uarg, &devargk, sizeof(devargk)))
 		return -EFAULT;    /* cleanup in mshv_device_fop_release() */

[Severity: High]
If userspace passes a read-only memory buffer to the MSHV_CREATE_DEVICE ioctl, copy_from_user() can succeed but copy_to_user() will fault and return -EFAULT.

Because the file descriptor has already been installed via anon_inode_getfd() prior to the copy_to_user() call, the user can recover the functional device FD via /proc/self/fd/ even after the ioctl returns an error.

Does this early return allow malicious userspace to bypass the memory pinning enforcement by forcing copy_to_user() to fail, intentionally skipping the partition->pt_regions_pinned = true assignment?

Quoted 7 lines
drivers/hv/mshv_root_main.c +3 -0
+	/* For now, just mark regions pinned if there is device passthru. */
+	partition->pt_regions_pinned = true;
+
 	return 0;
 
 undo_out:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9