All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Tao Cui" <cui.tao@linux.dev>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH 4/6] LoongArch: KVM: Rebase steal time counter in vcpu context
Date: Sun, 27 Sep 2026 08:11:40 +0000	[thread overview]
Message-ID: <20260927081141.68D741F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260927075240.3007947-5-cui.tao@linux.dev>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] kvm: LoongArch: steal time drops first preemption delay due to 0 sentinel
--

commit c0824aa7f32e751198a471b8af7a3432b9a7ad2e
Author: Tao Cui <cuitao@kylinos.cn>

LoongArch: KVM: Rebase steal time counter in vcpu context

KVM_SET_DEVICE_ATTR(PVTIME GPA) initializes st.last_steal from the
ioctl thread's run_delay, but kvm_update_stolen_time() accumulates the
vcpu thread's run_delay; the first delta can be negative and wraps in
u64, so the guest reads a steal time close to 2^64.

Drop the initialization from the attr path and lazily rebase the
counter on the first steal update, which runs in vcpu context like the
hypercall path.

> diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
> index ed9e092c97ba7..714bd87d1cea5 100644
> --- a/arch/loongarch/kvm/vcpu.c
> +++ b/arch/loongarch/kvm/vcpu.c
[ ... ]
> @@ -187,8 +188,14 @@ static void kvm_update_stolen_time(struct kvm_vcpu *vcpu)
>  	smp_wmb();
>  
>  	unsafe_get_user(steal, &st->steal, out);
> -	steal += current->sched_info.run_delay - vcpu->arch.st.last_steal;
> -	vcpu->arch.st.last_steal = current->sched_info.run_delay;
> +	/* acquire pairs with the smp_wmb in the attr path */
> +	smp_rmb();
> +	last_steal = READ_ONCE(vcpu->arch.st.last_steal);
> +	if (!last_steal)
> +		/* first update in vcpu context: rebase the counter */
> +		last_steal = current->sched_info.run_delay;
> +	steal += current->sched_info.run_delay - last_steal;
> +	WRITE_ONCE(vcpu->arch.st.last_steal, current->sched_info.run_delay);

[Severity: Medium]
Since last_steal uses 0 as a sentinel value to indicate an uninitialized
baseline, what happens if a fresh VCPU thread's current->sched_info.run_delay
is exactly 0 during its first call to kvm_update_stolen_time()?

If run_delay is 0, last_steal remains 0. When the VCPU is later preempted
and run_delay grows, the next update will still see last_steal == 0.
It appears this treats it as an uninitialized state, rebasing to the new
run_delay without adding the preemption delay to the guest's steal time.
Does this permanently swallow the first preemption delay?

[ ... ]
> @@ -1204,8 +1211,10 @@ static int kvm_loongarch_pvtime_set_attr(struct kvm_vcpu *vcpu,
>  	srcu_read_unlock(&kvm->srcu, idx);
>  
>  	if (!ret) {
> -		vcpu->arch.st.guest_addr = gpa;
> -		vcpu->arch.st.last_steal = current->sched_info.run_delay;
> +		WRITE_ONCE(vcpu->arch.st.last_steal, 0);

[Severity: Medium]
This sets 0 as the sentinel, establishing the ambiguity between an
uninitialized state and a valid run_delay value of 0. Could a different
sentinel value (like U64_MAX) be used instead to avoid overlapping with
a zero run_delay?

> +		/* publish the new address only after clearing the rebase sentinel */
> +		smp_wmb();
> +		WRITE_ONCE(vcpu->arch.st.guest_addr, gpa);
>  		kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
>  	}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260927075240.3007947-1-cui.tao@linux.dev?part=4

  reply	other threads:[~2026-09-27  8:11 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-27  7:52 [PATCH 0/6] LoongArch: KVM: irqchip and steal time fixes Tao Cui
2026-09-27  7:52 ` [PATCH 1/6] LoongArch: KVM: Clear device pointer in irqchip destroy callbacks Tao Cui
2026-09-28  4:07   ` Bibo Mao
2026-09-27  7:52 ` [PATCH 2/6] LoongArch: KVM: Guard against NULL irqchip in irq injection Tao Cui
2026-09-28  4:14   ` Bibo Mao
2026-09-28  9:24     ` Tao Cui
2026-09-27  7:52 ` [PATCH 3/6] LoongArch: KVM: Load dmsintc pointer once in pch_msi_set_irq Tao Cui
2026-09-27  8:05   ` sashiko-bot
2026-09-27  7:52 ` [PATCH 4/6] LoongArch: KVM: Rebase steal time counter in vcpu context Tao Cui
2026-09-27  8:11   ` sashiko-bot [this message]
2026-09-28  7:23   ` Bibo Mao
2026-09-28  9:28     ` Tao Cui
2026-09-27  7:52 ` [PATCH 5/6] LoongArch: KVM: Propagate real error code in kvm_pch_pic_create Tao Cui
2026-09-28  7:25   ` Bibo Mao
2026-09-27  7:52 ` [PATCH 6/6] LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT Tao Cui
2026-09-28  8:01   ` Bibo Mao
2026-09-28  9:31     ` Tao Cui

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=20260927081141.68D741F00893@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=cui.tao@linux.dev \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.