From: Tao Cui <cui.tao@linux.dev>
To: Bibo Mao <maobibo@loongson.cn>,
gaosong@loongson.cn, zhaotianrui@loongson.cn
Cc: cui.tao@linux.dev, loongarch@lists.linux.dev,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
chenhuacai@kernel.org, kernel@xen0n.name,
nagachaithanya9911@gmail.com, Tao Cui <cuitao@kylinos.cn>
Subject: Re: [PATCH 4/6] LoongArch: KVM: Rebase steal time counter in vcpu context
Date: Mon, 28 Sep 2026 17:28:58 +0800 [thread overview]
Message-ID: <272a796e-3d2b-4766-9e68-28d9092073a1@linux.dev> (raw)
In-Reply-To: <789eaa76-50a1-61bf-0419-0f45beb45a92@loongson.cn>
在 2026/9/28 15:23, Bibo Mao 写道:
>
>
> On 2026/9/27 下午3:52, Tao Cui wrote:
>> From: Tao Cui <cuitao@kylinos.cn>
>>
>> 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
> I think that ioctl thread is the vCPU thread itself in KVM mode, there will be many potential problems when one thread set registers of vCPU while vCPU is running.
>
You're right. I assumed KVM_SET_DEVICE_ATTR() could be issued
from a thread different from the vCPU thread. Looking at the expected
userspace flow again, the pvtime GPA is registered by the guest kernel
via hypercall (which runs in vCPU context), or set by the vCPU thread
itself before entering KVM_RUN. In both cases `last_steal` and the
subsequent steal updates use the same task's `run_delay`, so the
negative delta I described cannot occur.
I'll drop this patch as well.
Thanks,
Tao> Regards
> Bibo Mao
>> 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. Re-registering a GPA clears the rebase sentinel first
>> and publishes the address with a write barrier; the reader side loads
>> the address and sentinel with READ_ONCE and a read barrier so a
>> concurrent re-registration is not observed half-applied.
>>
>> Fixes: b4ba157044ea ("LoongArch: KVM: Add PV steal time support in host side")
>> Signed-off-by: Tao Cui <cuitao@kylinos.cn>
>> ---
>> arch/loongarch/kvm/vcpu.c | 25 +++++++++++++++++--------
>> 1 file changed, 17 insertions(+), 8 deletions(-)
>>
>> diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
>> index 8e028be3f0a9..d931a5a802f6 100644
>> --- a/arch/loongarch/kvm/vcpu.c
>> +++ b/arch/loongarch/kvm/vcpu.c
>> @@ -154,12 +154,13 @@ static void kvm_update_stolen_time(struct kvm_vcpu *vcpu)
>> u32 version;
>> u64 steal;
>> gpa_t gpa;
>> + u64 last_steal;
>> struct kvm_memslots *slots;
>> struct kvm_steal_time __user *st;
>> struct gfn_to_hva_cache *ghc;
>> ghc = &vcpu->arch.st.cache;
>> - gpa = vcpu->arch.st.guest_addr;
>> + gpa = READ_ONCE(vcpu->arch.st.guest_addr);
>> if (!(gpa & KVM_STEAL_PHYS_VALID))
>> return;
>> @@ -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);
>> unsafe_put_user(steal, &st->steal, out);
>> smp_wmb();
>> @@ -1122,7 +1129,7 @@ static int kvm_loongarch_pvtime_get_attr(struct kvm_vcpu *vcpu,
>> || attr->attr != KVM_LOONGARCH_VCPU_PVTIME_GPA)
>> return -ENXIO;
>> - gpa = vcpu->arch.st.guest_addr;
>> + gpa = READ_ONCE(vcpu->arch.st.guest_addr);
>> if (put_user(gpa, user))
>> return -EFAULT;
>> @@ -1197,7 +1204,7 @@ static int kvm_loongarch_pvtime_set_attr(struct kvm_vcpu *vcpu,
>> return -EINVAL;
>> if (!(gpa & KVM_STEAL_PHYS_VALID)) {
>> - vcpu->arch.st.guest_addr = gpa;
>> + WRITE_ONCE(vcpu->arch.st.guest_addr, gpa);
>> return 0;
>> }
>> @@ -1208,8 +1215,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);
>> + /* 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);
>> }
>> @@ -1800,7 +1809,7 @@ static void kvm_vcpu_set_pv_preempted(struct kvm_vcpu *vcpu)
>> struct kvm_memslots *slots;
>> struct kvm_steal_time __user *st;
>> - gpa = vcpu->arch.st.guest_addr;
>> + gpa = READ_ONCE(vcpu->arch.st.guest_addr);
>> if (!(gpa & KVM_STEAL_PHYS_VALID))
>> return;
>>
>
next prev parent reply other threads:[~2026-09-28 9:29 UTC|newest]
Thread overview: 15+ 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 7:52 ` [PATCH 4/6] LoongArch: KVM: Rebase steal time counter in vcpu context Tao Cui
2026-09-28 7:23 ` Bibo Mao
2026-09-28 9:28 ` Tao Cui [this message]
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=272a796e-3d2b-4766-9e68-28d9092073a1@linux.dev \
--to=cui.tao@linux.dev \
--cc=chenhuacai@kernel.org \
--cc=cuitao@kylinos.cn \
--cc=gaosong@loongson.cn \
--cc=kernel@xen0n.name \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=loongarch@lists.linux.dev \
--cc=maobibo@loongson.cn \
--cc=nagachaithanya9911@gmail.com \
--cc=zhaotianrui@loongson.cn \
/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
all inboxes | Powered by JetHome®