mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Koichiro Den <den@valinux.co.jp>
To: Anup Patel <anup@brainfault.org>,
	Atish Patra <atish.patra@linux.dev>,
	Andrew Jones <andrew.jones@linux.dev>
Cc: Paul Walmsley <pjw@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>,
	Alexandre Ghiti <alex@ghiti.fr>,
	Bingyu Xian <shanbeeyoo@gmail.com>,
	kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
	linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 1/3] RISC-V: KVM: Order writes to the SBI STA shared structure
Date: Sun, 22 Nov 2026 02:35:24 +0900	[thread overview]
Message-ID: <20261121173526.892344-2-den@valinux.co.jp> (raw)
In-Reply-To: <20261121173526.892344-1-den@valinux.co.jp>

The guest checks the sequence before and after reading steal time. If
the final even sequence becomes visible before the updated steal time,
the guest can accept a stale value. CSR accesses are weakly ordered;
toggling sstatus.SUM in get_user() and put_user() does not guarantee
that the guest observes those stores in order.

Pair the read barriers in pv_time_steal_clock() with write barriers.

Fixes: e9f12b5fff8a ("RISC-V: KVM: Implement SBI STA extension")
Signed-off-by: Koichiro Den <den@valinux.co.jp>
---
Changes since v1:
  - New patch. Split the write barriers out of v1 patch #1 into a
    separate fix. (Andrew)

 arch/riscv/kvm/vcpu_sbi_sta.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/riscv/kvm/vcpu_sbi_sta.c b/arch/riscv/kvm/vcpu_sbi_sta.c
index 60e50296a008..f1be90b514b2 100644
--- a/arch/riscv/kvm/vcpu_sbi_sta.c
+++ b/arch/riscv/kvm/vcpu_sbi_sta.c
@@ -9,6 +9,7 @@
 #include <linux/mm.h>
 #include <linux/sizes.h>
 
+#include <asm/barrier.h>
 #include <asm/bug.h>
 #include <asm/current.h>
 #include <asm/kvm_vcpu_sbi.h>
@@ -65,6 +66,8 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
 	if (WARN_ON(put_user(cpu_to_le32(sequence), sequence_ptr)))
 		return;
 
+	/* Publish the odd sequence before updating stolen time. */
+	smp_wmb();
 	if (!WARN_ON(get_user(steal_le, steal_ptr))) {
 		steal = le64_to_cpu(steal_le);
 		vcpu->arch.sta.last_steal = READ_ONCE(current->sched_info.run_delay);
@@ -72,6 +75,8 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
 		WARN_ON(put_user(cpu_to_le64(steal), steal_ptr));
 	}
 
+	/* Publish stolen time before the sequence becomes even. */
+	smp_wmb();
 	sequence += 1;
 	WARN_ON(put_user(cpu_to_le32(sequence), sequence_ptr));
 
-- 
2.54.0


  reply	other threads:[~2026-11-21 17:35 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21 17:35 [PATCH v2 0/3] RISC-V: KVM: Lessen steal time update overhead Koichiro Den
2026-11-21 17:35 ` Koichiro Den [this message]
2026-11-21 17:35 ` [PATCH v2 2/3] RISC-V: KVM: Batch user accesses when updating steal time Koichiro Den
2026-11-21 17:35 ` [PATCH v2 3/3] RISC-V: KVM: Skip steal-time updates when run_delay is unchanged Koichiro Den

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=20261121173526.892344-2-den@valinux.co.jp \
    --to=den@valinux.co.jp \
    --cc=alex@ghiti.fr \
    --cc=andrew.jones@linux.dev \
    --cc=anup@brainfault.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=atish.patra@linux.dev \
    --cc=kvm-riscv@lists.infradead.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    --cc=shanbeeyoo@gmail.com \
    /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®