mshv_vtl: Add a per-CPU transition statistics ioctl
PATCH v2 1/1 mshv_vtl: Add a per-CPU transition statistics ioctl
The VTL driver does not expose per-CPU transition counts or timing. This information is important to track number of VTL transitions based on the use-case, debug issues where a VP gets stuck in VTL2 etc. Add MSHV_GET_VTL_STATS IOCTL so userspace can query them and print this information periodically or on error handling paths. Report entry and return counts, the latest timestamps, and total and maximum interval durations. Include the last observed state so userspace
can calculate the age of an unfinished interval. These are elapsed times, not exclusive CPU execution times.
Use ioctl number 0x46 to avoid numbers already used by out-of-tree OpenHCL ioctls.
While at it, remove the unused num_vtl0_transitions counter. Add documentation in a generic VTL interface file. Documentation for existing ioctls is outside the scope of this patch.
--- Documentation/virt/hyperv/index.rst | 1 + Documentation/virt/hyperv/mshv-vtl.rst | 98 ++++++++++++++++++++++++++ drivers/hv/mshv_vtl_main.c | 72 ++++++++++++++++++- include/uapi/linux/mshv.h | 35 +++++++++ 4 files changed, 204 insertions(+), 2 deletions(-) create mode 100644 Documentation/virt/hyperv/mshv-vtl.rst
Documentation/virt/hyperv/index.rst +1 -0
@@ -13,3 +13,4 @@ Hyper-V Enlightenments vpci hibernation coco + mshv-vtl
Documentation/virt/hyperv/mshv-vtl.rst new file +98 -0
98 lines not on this page · raw message
drivers/hv/mshv_vtl_main.c +70 -2
115 lines not on this page · raw message
include/uapi/linux/mshv.h +35 -0
@@ -365,6 +365,40 @@ struct mshv_vtl_set_poll_file {__u32 fd; }; +#define MSHV_VTL_STATS_STATE_UNKNOWN 0 +#define MSHV_VTL_STATS_STATE_VTL2 1 +#define MSHV_VTL_STATS_STATE_LOWER_VTL 2 + +/** +* struct mshv_vtl_stats - arguments for MSHV_GET_VTL_STATS +* @cpu: Input Linux CPU number; all other fields are output. +* @state: Last observed MSHV_VTL_STATS_STATE_* software state. +* @entry_count: Number of lower-VTL switch attempts. +* @exit_count: Number of switch calls that returned to VTL2. +* @last_entry_ns: CLOCK_MONOTONIC timestamp of the last entry attempt. +* @last_exit_ns: CLOCK_MONOTONIC timestamp of the last return. +* @lower_vtl_total_ns: Total duration of completed entry-to-return intervals. +* @lower_vtl_max_ns: Maximum duration of a completed entry-to-return interval. +* @vtl2_total_ns: Total duration of completed return-to-next-entry intervals. +* @vtl2_max_ns: Maximum duration of a completed return-to-next-entry interval. +* @snapshot_ns: CLOCK_MONOTONIC timestamp of this snapshot. +* +* Durations are elapsed nanoseconds, not exclusive CPU execution time. +*/ +struct mshv_vtl_stats { +__u32 cpu; +__u32 state; +__u64 entry_count; +__u64 exit_count; +__u64 last_entry_ns; +__u64 last_exit_ns; +__u64 lower_vtl_total_ns; +__u64 lower_vtl_max_ns; +__u64 vtl2_total_ns; +__u64 vtl2_max_ns; +__u64 snapshot_ns; +}; +struct mshv_vtl_hvcall_setup {__u64 bitmap_array_size;/* stores number of bytes */ __u64 allow_bitmap_ptr;
@@ -392,6 +426,7 @@ struct mshv_sint_mask {#define MSHV_ADD_VTL0_MEMORY _IOW(MSHV_IOCTL, 0x21, struct mshv_vtl_ram_disposition)#define MSHV_SET_POLL_FILE _IOW(MSHV_IOCTL, 0x25, struct mshv_vtl_set_poll_file)#define MSHV_RETURN_TO_LOWER_VTL _IO(MSHV_IOCTL, 0x27) +#define MSHV_GET_VTL_STATS _IOWR(MSHV_IOCTL, 0x46, struct mshv_vtl_stats)#define MSHV_GET_VP_REGISTERS _IOWR(MSHV_IOCTL, 0x05, struct mshv_vp_registers)#define MSHV_SET_VP_REGISTERS _IOW(MSHV_IOCTL, 0x06, struct mshv_vp_registers)
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] mshv_vtl: raw_spinlock_t on VTL switch path causes VP stalls and EAGAIN UAPI - [Medium] mshv_vtl…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] mshv_vtl: raw_spinlock_t on VTL switch path causes VP stalls and EAGAIN UAPI
- [Medium] mshv_vtl: unaligned per-CPU mshv_vtl_cpu_stats causes cacheline false sharing
--
commit 24b3fa8944af399cfdbaaec1aa9176459c9fd6c9
Author: Naman Jain <[email protected]>
mshv_vtl: Add a per-CPU transition statistics ioctl
This commit adds the MSHV_GET_VTL_STATS ioctl and per-CPU accounting to track lower-VTL entry/exit counts, timestamps, and elapsed durations.
Quoted 4 lines
drivers/hv/mshv_vtl_main.c +0
-0
[ ... ]
Quoted 6 lines
drivers/hv/mshv_vtl_main.c +1
-1@@ -102,7 +104,16 @@ static union hv_register_vsm_page_offsets mshv_vsm_page_offsets;static union hv_register_vsm_capabilities mshv_vsm_capabilities; static DEFINE_PER_CPU(struct mshv_vtl_poll_file, mshv_vtl_poll_file); -static DEFINE_PER_CPU(unsigned long long, num_vtl0_transitions); +
drivers/hv/mshv_vtl_main.c static union hv_register_vsm_page_offsets mshv_vsm_page_offsets; · line 117
Quoted 10 lines
drivers/hv/mshv_vtl_main.c +9
-0+struct mshv_vtl_cpu_stats { +raw_spinlock_t lock; +struct mshv_vtl_stats stats; +}; + +static DEFINE_PER_CPU(struct mshv_vtl_cpu_stats, mshv_vtl_cpu_stats) = { + .lock =__RAW_SPIN_LOCK_UNLOCKED (mshv_vtl_cpu_stats.lock), +}; + static DEFINE_PER_CPU(struct mshv_vtl_per_cpu, mshv_vtl_per_cpu);
[Severity: Medium]
Should mshv_vtl_cpu_stats use DEFINE_PER_CPU_SHARED_ALIGNED() to avoid cacheline false sharing?
With plain DEFINE_PER_CPU(), this 88-byte structure sits unaligned between mshv_vtl_poll_file (80 bytes, ending with poll_file->cpu read remotely in mshv_vtl_poll_file_wake()) and mshv_vtl_per_cpu (read remotely via mshv_vtl_cpu_run() in mshv_vtl_cancel()). Local stats updates on every VTL switch then bounce cachelines shared with remote wakeups and stats queries, and can touch three cachelines instead of two.
[ ... ]
Quoted 12 lines
drivers/hv/mshv_vtl_main.c +8
-0@@ -649,6 +660,36 @@ static int mshv_vtl_get_set_reg(struct hv_register_assoc *regs, bool set)return 1 ; } +static void mshv_vtl_record_transition (bool entering) +{ +struct mshv_vtl_cpu_stats *cpu_stats =this_cpu_ptr (&mshv_vtl_cpu_stats); +struct mshv_vtl_stats *stats = &cpu_stats->stats; +u64 now, elapsed; + +raw_spin_lock (&cpu_stats->lock); + now =ktime_get_ns ();
[ ... ]
Quoted 2 lines
drivers/hv/mshv_vtl_main.c +2
-0+raw_spin_unlock (&cpu_stats->lock); +}
[ ... ]
Quoted 12 lines
drivers/hv/mshv_vtl_main.c +12
-0+static long mshv_vtl_ioctl_get_stats (struct mshv_vtl_stats __user *user_stats) +{ +struct mshv_vtl_cpu_stats *cpu_stats; +struct mshv_vtl_stats stats; +unsigned long flags; +u32 cpu; + +if (get_user (cpu, &user_stats->cpu)) +return -EFAULT ; +if (cpu >= nr_cpu_ids || !cpu_possible (cpu)) +return -EINVAL ; +
drivers/hv/mshv_vtl_main.c mshv_vtl_ioctl_set_regs(void __user *user_args) · line 873
Quoted 10 lines
drivers/hv/mshv_vtl_main.c +10
-0+ cpu_stats =per_cpu_ptr (&mshv_vtl_cpu_stats, cpu); +if (!raw_spin_trylock_irqsave (&cpu_stats->lock, flags)) +return -EAGAIN ; + stats = cpu_stats->stats; + stats.cpu = cpu; + stats.snapshot_ns =ktime_get_ns (); +raw_spin_unlock_irqrestore (&cpu_stats->lock, flags); + +return copy_to_user (user_stats, &stats,sizeof (stats)) ? -EFAULT :0 ; +}
[Severity: Medium]
Can this use a per-CPU seqcount_t instead of raw_spinlock_t?
mshv_vtl_record_transition() only runs from mshv_vtl_return() with preemption and IRQs disabled, so each per-CPU instance has a single local writer. Taking raw_spinlock_t adds two atomic RMW lock/unlock cycles and two ktime_get_ns() calls to every lower-VTL switch even when stats are never queried.
When userspace polls MSHV_GET_VTL_STATS from another CPU, raw_spin_trylock_irqsave() dirties the target VP's cacheline and holds the lock across the struct copy and ktime_get_ns(), stalling the IRQ-disabled VP in raw_spin_lock(). Because hv_vcpu_is_preempted() always returns false under Hyper-V, host preemption of the reader vCPU magnifies that spin stall.
Using trylock also exposes internal contention as -EAGAIN in the permanent UAPI (including between concurrent readers of an idle or offline CPU). A per-CPU seqcount_t keeps the local writer wait-free without atomic RMWs, prevents remote readers from stalling VPs, and removes -EAGAIN from the interface.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 7 lines · On 12/3/2026 11:38 AM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium]mshv_vtl:raw_spinlock_ton VTL switch path causes VP stalls and EAGAIN UAPI
- [Medium]mshv_vtl: unaligned per-CPUmshv_vtl_cpu_statscauses cacheline false sharing
--
I am going to resolve these two review comments, and add a couple more changes and send it as v3.
Regards,
Naman