Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "David Woodhouse" <dwmw2@infradead.org>, kvm@vger.kernel.org
Subject: Re: [PATCH v3 03/11] KVM: x86/xen: Wait out memslot updates in the Xen attribute ioctls
Date: Fri, 16 Oct 2026 15:11:10 +0000	[thread overview]
Message-ID: <sashiko-outbox-174534@kernel.org> (raw)
In-Reply-To: <20261016145646.579369-4-dwmw2@infradead.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] kvm: x86/xen: torn guest wallclock read due to concurrent writers
--

commit dd1c2ed3d111f222bc79a42aa91b7852c3e338f8
Author: David Woodhouse <dwmw@amazon.co.uk>

KVM: x86/xen: Wait out memslot updates in the Xen attribute ioctls

This commit updates Xen attribute ioctls to wait out memslot updates. To
prevent deadlocks, the waiting is performed without holding the xen_lock,
explicitly allowing some operations to run concurrently.

> This also means two kvm_xen_shared_info_init() calls can publish the
> wallclock concurrently, no longer excluded by xen_lock. That is
> harmless: concurrent writers compute the same epoch, modulo
> nanoseconds of jitter, and each re-reads long_mode to pick the
> destination, so the location for the final mode is always freshly
> written.

[Severity: Medium]
While the computed epochs are nearly identical, could concurrent writers
cause a torn read for the guest if the clock readings straddle a second
boundary?

If thread A and thread B compute the time concurrently, they might obtain
readings on opposite sides of a second boundary:
  Thread A: sec = 100, nsec = 999999999
  Thread B: sec = 101, nsec = 0

Because they both write to the shared structure non-atomically, their
updates can interleave:
  Thread A writes wc->sec = 100
  Thread B writes wc->nsec = 0

This would produce a time shifted by a full second (sec = 100, nsec = 0).

Because both threads increment the version counter using the same
read-modify-write pattern, the seqlock ends up in a valid, even state.
Would the guest then accept this torn time since the writers are not
serialized?

> diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c
> --- a/arch/x86/kvm/xen.c
> +++ b/arch/x86/kvm/xen.c
> @@ -44,17 +44,24 @@ static int kvm_xen_shared_info_init(struct kvm *kvm)
>  	u32 wc_version;
>  	u64 wall_nsec;
>  	int ret = 0;
> -	int idx = srcu_read_lock(&kvm->srcu);
> +	int idx;
>  
> -	read_lock_irq(&gpc->lock);
> -	while (!kvm_gpc_check(gpc, PAGE_SIZE)) {
> +	/*
> +	 * kvm_gpc_check() consults the memslots, so take kvm->srcu around
> +	 * each check and the write through the mapping — but not across
> +	 * the refresh, which may wait for a memslot update to complete.
> +	 */
> +	for (;;) {
> +		idx = srcu_read_lock(&kvm->srcu);
> +		read_lock_irq(&gpc->lock);

[Severity: Medium]
Is using an rwlock for reading here what directly enables the concurrent
writes to the guest wallclock? Since multiple CPUs can acquire the read lock
simultaneously, the seqlock in the guest is no longer protected by serialized
writers.

[ ... ]

> @@ -98,9 +105,9 @@ static int kvm_xen_shared_info_init(struct kvm *kvm)
>  
>  	wc->version = wc_version + 1;
>  	read_unlock_irq(&gpc->lock);
> +	srcu_read_unlock(&kvm->srcu, idx);
>  
>  out:
> -	srcu_read_unlock(&kvm->srcu, idx);
>  	return ret;
>  }

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261016145646.579369-1-dwmw2@infradead.org?part=3

  reply	other threads:[~2026-10-16 15:11 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-16 14:56 [PATCH v3 00/11] KVM: x86/xen: Don't drop event deliveries which race memslot updates David Woodhouse
2026-10-16 14:56 ` [PATCH v3 01/11] KVM: pfncache: Return -EAGAIN for a lookup which hits an invalid memslot David Woodhouse
2026-10-16 14:56 ` [PATCH v3 02/11] KVM: pfncache: Add a 'wait' argument to the refresh and activate functions David Woodhouse
2026-10-16 14:56 ` [PATCH v3 03/11] KVM: x86/xen: Wait out memslot updates in the Xen attribute ioctls David Woodhouse
2026-10-16 15:11   ` sashiko-bot [this message]
2026-10-16 16:37     ` David Woodhouse
2026-10-16 14:56 ` [PATCH v3 04/11] KVM: x86/xen: Don't lose the timer event when delivery fails transiently David Woodhouse
2026-10-16 14:56 ` [PATCH v3 05/11] KVM: x86/xen: Retry event channel delivery which races a memslot update David Woodhouse
2026-10-16 15:11   ` sashiko-bot
2026-10-16 16:45     ` David Woodhouse
2026-10-16 14:56 ` [PATCH v3 06/11] KVM: x86/xen: Extract delivery of event to vCPU into a separate helper David Woodhouse
2026-10-16 14:56 ` [PATCH v3 07/11] KVM: x86/xen: Explicitly tag "shared info" page as never being dirty tracked David Woodhouse
2026-10-16 14:56 ` [PATCH v3 08/11] KVM: x86/xen: Don't dirty track "vCPU info" page David Woodhouse
2026-10-16 14:56 ` [PATCH v3 09/11] KVM: x86: Request the guest TLB flush from record_steal_time() David Woodhouse
2026-10-16 14:56 ` [PATCH v3 10/11] KVM: selftests: Add Xen evtchn vs memslot-update race test David Woodhouse
2026-10-16 14:56 ` [PATCH v3 11/11] KVM: selftests: Fix unreachable timer recovery in xen_shinfo_test David Woodhouse

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-174534@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dwmw2@infradead.org \
    --cc=kvm@vger.kernel.org \
    --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