Linux-HyperV List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Naman Jain" <namjain@linux.microsoft.com>
Cc: linux-hyperv@vger.kernel.org
Subject: Re: [PATCH v2] mshv_vtl: Add a per-CPU transition statistics ioctl
Date: Thu, 03 Dec 2026 06:08:11 +0000	[thread overview]
Message-ID: <sashiko-outbox-247221@kernel.org> (raw)
In-Reply-To: <20261203054302.3544702-1-namjain@linux.microsoft.com>

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 <namjain@linux.microsoft.com>

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.

> diff --git a/drivers/hv/mshv_vtl_main.c b/drivers/hv/mshv_vtl_main.c
> index 2d4fe24cecbff..b7cb5817dd5b3 100644
> --- a/drivers/hv/mshv_vtl_main.c
> +++ b/drivers/hv/mshv_vtl_main.c

[ ... ]

> @@ -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 {
> +	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.

[ ... ]

> @@ -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();

[ ... ]

> +	raw_spin_unlock(&cpu_stats->lock);
> +}

[ ... ]

> +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;
> +
> +	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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203054302.3544702-1-namjain@linux.microsoft.com?part=1

      reply	other threads:[~2026-12-03  6:08 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  5:43 [PATCH v2] mshv_vtl: Add a per-CPU transition statistics ioctl Naman Jain
2026-12-03  6:08 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-247221@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-hyperv@vger.kernel.org \
    --cc=namjain@linux.microsoft.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox