From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=13.77.154.182 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796412877; b=XfqediAq1z+icfXh9FknCnFaAiKWcjnmiJyUvy6uxtR2zqLhZK3DVjAnIRkwByV0NLEg g4/Ar7skgD7FHHcHPxyE4Bp2NWedEtVehRebolhEXQyku/ZVld2sNwkQ0cEMVC/59GB/T j506RVJzl0gtwgijvHkqaBMpb5r8mBGJTBnRL3fCwVKx7m+liqB72ZyhphqSdxv0qZS7w 0gUUzo2BFc1HVuyjQ9P0P3CBwTlod4XOSD5N48KgvlHBd5iqU8xi5Mqu6hgk/CPjDxt+n c6zgNP2uZohLwDTfGrTlrcNudChhQzfRyZKrE3NhLK9ciIKA5OxILJ21puPrd7kcLaQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796412877; h=DMARC-Filter:Received:DKIM-Filter:DKIM-Signature:Message-ID:Date: MIME-Version:User-Agent:Subject:To:Cc:References:Content-Language: From:In-Reply-To:Content-Type:Content-Transfer-Encoding:X-Spam-Status: X-Spam-Checker-Version; bh=BGg51Oznrkdw3BgoblulMWyPb57s/rJiyiNVpJKoRHk=; b=EpyxOa228CdeSHxjjSBsHoqP4ql9LqNRYgxxv23Q8/rAg9q5f1ir9P4+glMgNcrJamUT gvZ8Uw0vKPToiGMoWPr7ab4T6zvALZHnXdr5y0YcFmhZJQalcBF3qJJx4JQzsc0G8a/R7 tNiwAmpOEVPF31r4IvP2/wncAA29sxzjyGwl4t85YRyUAP7Okyesz81VOuSXH1qdDH+EF WPMLxuZz8DcPIeDKUxKqVw0JwB2wfJztIZIdiAC22vB3CC18ALuCGZ0HKaZsOVKh+jFLb hIEmToGOjvWISByVuB8U7+nuU9oPK8c5i9n/Pnyp8wEhD0jFYtfkCWWg5bOPEVWbCcw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass header.d=linux.microsoft.com header.i=@linux.microsoft.com header.a=rsa-sha256 header.s=default header.b=QiyoalAz; arc=none smtp.remote-ip=13.77.154.182 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6E5CC606A322; Fri, 4 Dec 2026 19:34:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6E5CC606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.a=rsa-sha256 header.s=default header.b=QiyoalAz Received: from [10.0.0.239] (unknown [4.194.122.170]) by linux.microsoft.com (Postfix) with ESMTPSA id 249DB20B7178; Fri, 4 Dec 2026 11:34:22 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 249DB20B7178 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1796412869; bh=BGg51Oznrkdw3BgoblulMWyPb57s/rJiyiNVpJKoRHk=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=QiyoalAzoUxbWu7F0ZAGdJBQTjEEe7L0Os1k450kSty/6I1gNBfO06ITM23ggMLuL sAbMYeV8AIQ+V3/eTH7e+VGLh5i4vWCfVYF2T1sQvNl+6kw97Al+oTq31IHmp0vp0a BaExLs54NwEiDmx0Yh4ImX5hJb2PgCpfAJelrc7I= Message-ID: <8c5802dd-89b8-4c00-a2d6-e278ff152ed9@linux.microsoft.com> Date: Fri, 4 Dec 2026 11:34:20 -0800 X-Mailing-List: linux-hyperv@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3] mshv_vtl: Add a per-CPU transition statistics ioctl To: Naman Jain , haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, corbet@lwn.net Cc: skhan@linuxfoundation.org, rdunlap@infradead.org, mhklinux@outlook.com, romank@linux.microsoft.com, skinsburskii@linux.microsoft.com, muislam@microsoft.com, anbelski@linux.microsoft.com, linux-hyperv@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, ssengar@linux.microsoft.com References: <20261203112422.52576-1-namjain@linux.microsoft.com> Content-Language: en-US From: Hardik Garg In-Reply-To: <20261203112422.52576-1-namjain@linux.microsoft.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 12/3/2026 3:24 AM, Naman Jain wrote: > 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. > It is needed in production, where debugfs and tracing are not enabled. > Add MSHV_GET_VTL_STATS IOCTL so userspace can query them and print this > information periodically or on error handling paths. Report switch > counts in each direction, 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. > > Add MSHV_CAP_VTL_STATS so userspace can detect the ioctl. Include a > flags field and reserved space so the interface can be extended later. > > Use ioctl number 0x46 and capability 0x5 to avoid numbers already used > by out-of-tree OpenHCL interfaces. > > 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. > > Signed-off-by: Naman Jain > --- > Changes since v2: > https://lore.kernel.org/all/20261203054302.3544702-1-namjain@linux.microsoft.com/ > Address Sashiko AI review - > - Use per-CPU sequence counters so readers do not block transition updates. > - Cacheline-align the per-CPU statistics with DEFINE_PER_CPU_SHARED_ALIGNED(). > - Remove the contention-related EAGAIN error and update the documentation. > Other changes: > - Add MSHV_CAP_VTL_STATS so userspace can detect the ioctl. > - Add a flags field and reserved space so the struct can be extended. > - Rename the count and timestamp fields to state the switch direction. > > Changes since v1: > https://lore.kernel.org/all/20261117081211.2063781-1-namjain@linux.microsoft.com/ > Address Hardik's review comments - > - Rename the per-CPU variable to mshv_vtl_cpu_stats. > - Change the ioctl number from 0x41 to 0x46 to avoid existing OpenHCL > allocations, and explain the choice in the commit message. > > Documentation/virt/hyperv/index.rst | 1 + > Documentation/virt/hyperv/mshv-vtl.rst | 106 +++++++++++++++++++++++++ > drivers/hv/mshv_vtl_main.c | 77 +++++++++++++++++- > include/uapi/linux/mshv.h | 43 ++++++++++ > 4 files changed, 225 insertions(+), 2 deletions(-) > create mode 100644 Documentation/virt/hyperv/mshv-vtl.rst > > diff --git a/Documentation/virt/hyperv/index.rst b/Documentation/virt/hyperv/index.rst > index c84c40fd61c91..9fcff8c0f7db8 100644 > --- a/Documentation/virt/hyperv/index.rst > +++ b/Documentation/virt/hyperv/index.rst > @@ -13,3 +13,4 @@ Hyper-V Enlightenments > vpci > hibernation > coco > + mshv-vtl > diff --git a/Documentation/virt/hyperv/mshv-vtl.rst b/Documentation/virt/hyperv/mshv-vtl.rst > new file mode 100644 > index 0000000000000..d28bc08686c81 > --- /dev/null > +++ b/Documentation/virt/hyperv/mshv-vtl.rst > @@ -0,0 +1,106 @@ > +.. SPDX-License-Identifier: GPL-2.0 > + > +MSHV VTL userspace interface > +============================ > + > +The MSHV VTL driver allows a userspace VMM running in VTL2 to manage > +and run a lower VTL. > + > +The ioctls described below operate on a VTL file descriptor returned > +by ``MSHV_CREATE_VTL``. > + > +MSHV_GET_VTL_STATS > +------------------ > + > +Return a snapshot of transition counts and timing for one Linux CPU. > + > +Set the ``cpu`` field in ``struct mshv_vtl_stats`` and set ``flags`` to > +zero before calling the ioctl. All other fields are output; ``pad`` and > +``reserved`` are returned as zero. Possible CPUs are accepted even when > +offline. Statistics are shared across VTL file descriptors and accumulate > +for the lifetime of the loaded driver. > + > +Userspace can check for this ioctl by calling ``MSHV_CHECK_EXTENSION`` > +with ``MSHV_CAP_VTL_STATS`` on ``/dev/mshv``. It returns 1 when the ioctl > +is supported. > + > +.. kernel-doc:: include/uapi/linux/mshv.h > + :identifiers: mshv_vtl_stats > + > +Accounting > +~~~~~~~~~~ > + > +Each switch from VTL2 to the lower VTL increments ``to_lower_vtl_count``, > +and each switch back to VTL2 increments ``to_vtl2_count``. Every switch in > +an in-kernel re-entry loop is counted separately. Work or cancellation before a switch does not > +add a transition. > + > +All timestamps and durations use CLOCK_MONOTONIC nanoseconds. > + > +* Lower-VTL intervals run from a switch to the lower VTL to the next > + switch back to VTL2. > +* VTL2 intervals run from a switch back to VTL2 to the next switch to > + the lower VTL. > +* Totals and maxima include completed intervals only. > +* The initial VTL2 interval before the first switch is not measured. > + > +The statistics work without tracing. They measure elapsed time, > +including switch overhead and descheduling, not exclusive CPU > +execution time. Only switches made through this driver's x86-64 Hyper-V > +return path are measured. Intervals are not split by target VTL. > + > +There is no reset, histogram, or transition-history buffer. Accounting > +adds two clock reads and short per-CPU critical sections to each switch. > + > +Current interval > +~~~~~~~~~~~~~~~~ > + > +The ``state`` field reports the last observed software state: > + > +* UNKNOWN: No switch has been recorded. Do not calculate an interval age. > +* VTL2: Use ``snapshot_ns - last_to_vtl2_ns``. > +* LOWER_VTL: Use ``snapshot_ns - last_to_lower_vtl_ns``. > + > +Counts, durations, and transition timestamps are zero before the first > +switch. The state does not prove that the CPU is currently executing in > +that VTL. An offline CPU retains its last recorded statistics. > + > +Errors > +~~~~~~ > + > +* ``EINVAL``: The CPU number is outside the possible CPU set, or > + ``flags`` is not zero. > +* ``EFAULT``: The userspace buffer could not be accessed. > + > +Reading another CPU's statistics does not send an IPI to that CPU. > +Readers retry if an update overlaps the snapshot; they do not hold a lock > +that blocks the transition path. > + > +Example > +~~~~~~~ > + > +Using an existing VTL file descriptor:: > + > + struct mshv_vtl_stats stats = { .cpu = 0 }; > + > + if (ioctl(vtl_fd, MSHV_GET_VTL_STATS, &stats) == -1) { > + perror("MSHV_GET_VTL_STATS"); > + return -1; > + } > + > + printf("cpu=%u state=%u\n" > + "to_lower_vtl_count=%llu to_vtl2_count=%llu\n" > + "last_to_lower_vtl_ns=%llu last_to_vtl2_ns=%llu\n" > + "lower_vtl_total_ns=%llu lower_vtl_max_ns=%llu\n" > + "vtl2_total_ns=%llu vtl2_max_ns=%llu\n" > + "snapshot_ns=%llu\n", > + stats.cpu, stats.state, > + (unsigned long long)stats.to_lower_vtl_count, > + (unsigned long long)stats.to_vtl2_count, > + (unsigned long long)stats.last_to_lower_vtl_ns, > + (unsigned long long)stats.last_to_vtl2_ns, > + (unsigned long long)stats.lower_vtl_total_ns, > + (unsigned long long)stats.lower_vtl_max_ns, > + (unsigned long long)stats.vtl2_total_ns, > + (unsigned long long)stats.vtl2_max_ns, > + (unsigned long long)stats.snapshot_ns); > diff --git a/drivers/hv/mshv_vtl_main.c b/drivers/hv/mshv_vtl_main.c > index 8e374c02128f3..4cbb17a2a6402 100644 > --- a/drivers/hv/mshv_vtl_main.c > +++ b/drivers/hv/mshv_vtl_main.c > @@ -19,6 +19,8 @@ > #include > #include > #include > +#include > +#include > #include > #include > #include > @@ -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); > + > +struct mshv_vtl_cpu_stats { > + seqcount_t seq; > + struct mshv_vtl_stats stats; > +}; > + > +static DEFINE_PER_CPU_SHARED_ALIGNED(struct mshv_vtl_cpu_stats, mshv_vtl_cpu_stats) = { > + .seq = SEQCNT_ZERO(mshv_vtl_cpu_stats.seq), > +}; > + > static DEFINE_PER_CPU(struct mshv_vtl_per_cpu, mshv_vtl_per_cpu); > > static const union hv_input_vtl input_vtl_zero; > @@ -158,6 +169,8 @@ mshv_ioctl_check_extension(void __user *user_arg) > return mshv_vsm_capabilities.return_action_available; > case MSHV_CAP_DR6_SHARED: > return mshv_vsm_capabilities.dr6_shared; > + case MSHV_CAP_VTL_STATS: > + return 1; > } > > return -EOPNOTSUPP; > @@ -649,6 +662,37 @@ static int mshv_vtl_get_set_reg(struct hv_register_assoc *regs, bool set) > return 1; > } > > +static void mshv_vtl_record_transition(bool to_lower_vtl) > +{ > + 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; > + > + /* Only the local CPU writes, with preemption and interrupts disabled. */ > + now = ktime_get_ns(); > + write_seqcount_begin(&cpu_stats->seq); > + > + if (to_lower_vtl) { > + if (stats->state == MSHV_VTL_STATS_STATE_VTL2) { > + elapsed = now - stats->last_to_vtl2_ns; > + stats->vtl2_total_ns += elapsed; > + stats->vtl2_max_ns = max(stats->vtl2_max_ns, elapsed); > + } > + stats->to_lower_vtl_count++; > + stats->last_to_lower_vtl_ns = now; > + stats->state = MSHV_VTL_STATS_STATE_LOWER_VTL; > + } else { > + elapsed = now - stats->last_to_lower_vtl_ns; > + stats->lower_vtl_total_ns += elapsed; > + stats->lower_vtl_max_ns = max(stats->lower_vtl_max_ns, elapsed); > + stats->to_vtl2_count++; > + stats->last_to_vtl2_ns = now; > + stats->state = MSHV_VTL_STATS_STATE_VTL2; > + } > + > + write_seqcount_end(&cpu_stats->seq); > +} > + > static void mshv_vtl_return(struct mshv_vtl_cpu_context *vtl0) > { > struct hv_vp_assist_page *hvp; > @@ -672,7 +716,9 @@ static void mshv_vtl_return(struct mshv_vtl_cpu_context *vtl0) > min_t(u32, offset, sizeof(hvp->vtl_ret_actions))); > } > > + mshv_vtl_record_transition(true); > mshv_vtl_return_call(vtl0); > + mshv_vtl_record_transition(false); > } > > static bool mshv_vtl_process_intercept(void) > @@ -725,7 +771,6 @@ static int mshv_vtl_ioctl_return_to_lower_vtl(void) > local_irq_restore(irq_flags); > > hvp = hv_vp_assist_page[smp_processor_id()]; > - this_cpu_inc(num_vtl0_transitions); > switch (hvp->vtl_entry_reason) { > case MSHV_ENTRY_REASON_INTERRUPT: > if (!mshv_vsm_capabilities.intercept_page_available && > @@ -807,6 +852,31 @@ mshv_vtl_ioctl_set_regs(void __user *user_args) > return ret; > } > > +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 int seq; > + u32 cpu, flags; > + > + if (get_user(cpu, &user_stats->cpu) || > + get_user(flags, &user_stats->flags)) > + return -EFAULT; > + if (flags || cpu >= nr_cpu_ids || !cpu_possible(cpu)) > + return -EINVAL; > + > + cpu_stats = per_cpu_ptr(&mshv_vtl_cpu_stats, cpu); > + do { > + seq = read_seqcount_begin(&cpu_stats->seq); > + stats = cpu_stats->stats; > + stats.snapshot_ns = ktime_get_ns(); > + } while (read_seqcount_retry(&cpu_stats->seq, seq)); > + stats.cpu = cpu; > + stats.flags = flags; > + > + return copy_to_user(user_stats, &stats, sizeof(stats)) ? -EFAULT : 0; > +} > + > static long > mshv_vtl_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) > { > @@ -826,6 +896,9 @@ mshv_vtl_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) > case MSHV_RETURN_TO_LOWER_VTL: > ret = mshv_vtl_ioctl_return_to_lower_vtl(); > break; > + case MSHV_GET_VTL_STATS: > + ret = mshv_vtl_ioctl_get_stats((void __user *)arg); > + break; > case MSHV_ADD_VTL0_MEMORY: > ret = mshv_vtl_ioctl_add_vtl0_mem(vtl, (void __user *)arg); > break; > diff --git a/include/uapi/linux/mshv.h b/include/uapi/linux/mshv.h > index 32ff92b6342b2..ae3167b93d256 100644 > --- a/include/uapi/linux/mshv.h > +++ b/include/uapi/linux/mshv.h > @@ -330,6 +330,7 @@ struct mshv_get_set_vp_state { > #define MSHV_CAP_REGISTER_PAGE 0x1 > #define MSHV_CAP_VTL_RETURN_ACTION 0x2 > #define MSHV_CAP_DR6_SHARED 0x3 > +#define MSHV_CAP_VTL_STATS 0x5 > #define MSHV_MAX_RUN_MSG_SIZE 256 > > struct mshv_vp_registers { > @@ -365,6 +366,47 @@ 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. > + * @flags: Input flags; must be zero. > + * @state: Last observed MSHV_VTL_STATS_STATE_* software state. > + * @pad: Reserved; returned as zero. > + * @to_lower_vtl_count: Number of switches from VTL2 to the lower VTL. > + * @to_vtl2_count: Number of switches back to VTL2. > + * @last_to_lower_vtl_ns: CLOCK_MONOTONIC time of the last lower-VTL switch. > + * @last_to_vtl2_ns: CLOCK_MONOTONIC time of the last switch back to VTL2. > + * @lower_vtl_total_ns: Total time of completed intervals in the lower VTL. > + * @lower_vtl_max_ns: Longest completed interval in the lower VTL. > + * @vtl2_total_ns: Total time of completed intervals in VTL2. > + * @vtl2_max_ns: Longest completed interval in VTL2. > + * @snapshot_ns: CLOCK_MONOTONIC timestamp of this snapshot. > + * @reserved: Reserved for future fields; returned as zero. > + * > + * All fields other than @cpu and @flags are output. > + * Durations are elapsed nanoseconds, not exclusive CPU execution time. > + */ > +struct mshv_vtl_stats { > + __u32 cpu; > + __u32 flags; > + __u32 state; > + __u32 pad; > + __u64 to_lower_vtl_count; > + __u64 to_vtl2_count; > + __u64 last_to_lower_vtl_ns; > + __u64 last_to_vtl2_ns; > + __u64 lower_vtl_total_ns; > + __u64 lower_vtl_max_ns; > + __u64 vtl2_total_ns; > + __u64 vtl2_max_ns; > + __u64 snapshot_ns; > + __u64 reserved[4]; > +}; > + > struct mshv_vtl_hvcall_setup { > __u64 bitmap_array_size; /* stores number of bytes */ > __u64 allow_bitmap_ptr; > @@ -392,6 +434,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) > > > base-commit: 799dd936553182df5770dde7f38d94c8337df75a Reviewed-by: Hardik Garg hargar@linux.microsoft.com Thanks, Hardik