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 2/3] RISC-V: KVM: Batch user accesses when updating steal time
Date: Sun, 22 Nov 2026 02:35:25 +0900 [thread overview]
Message-ID: <20261121173526.892344-3-den@valinux.co.jp> (raw)
In-Reply-To: <20261121173526.892344-1-den@valinux.co.jp>
Updating the SBI STA shared structure performs 2x get_user() + 3x
put_user() calls, each of which toggles sstatus.SUM.
Keep SUM enabled across all five accesses to the shared structure.
Signed-off-by: Koichiro Den <den@valinux.co.jp>
---
Changes since v1:
- Drop might_fault(). (Andrew)
- Use scoped_user_rw_access(). (Andrew)
- Use le32_add_cpu() and le64_add_cpu() to avoid the extra temporaries.
Note:
- I haven't carried over Andrew's Reviewed-by since the scoped_
conversion reworks the fault paths. Would appreciate another look.
- As in v1, I kept WARN_ON() for uaccess failures.
v1: https://lore.kernel.org/r/20261118062242.2914385-2-den@valinux.co.jp/
arch/riscv/kvm/vcpu_sbi_sta.c | 60 +++++++++++++++++------------------
1 file changed, 30 insertions(+), 30 deletions(-)
diff --git a/arch/riscv/kvm/vcpu_sbi_sta.c b/arch/riscv/kvm/vcpu_sbi_sta.c
index f1be90b514b2..68d54696dbae 100644
--- a/arch/riscv/kvm/vcpu_sbi_sta.c
+++ b/arch/riscv/kvm/vcpu_sbi_sta.c
@@ -8,6 +8,7 @@
#include <linux/kvm_host.h>
#include <linux/mm.h>
#include <linux/sizes.h>
+#include <linux/uaccess.h>
#include <asm/barrier.h>
#include <asm/bug.h>
@@ -15,7 +16,6 @@
#include <asm/kvm_vcpu_sbi.h>
#include <asm/page.h>
#include <asm/sbi.h>
-#include <asm/uaccess.h>
static void kvm_riscv_vcpu_sbi_sta_reset(struct kvm_vcpu *vcpu)
{
@@ -27,13 +27,9 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
{
gpa_t shmem = vcpu->arch.sta.shmem;
u64 last_steal = vcpu->arch.sta.last_steal;
- __le32 __user *sequence_ptr;
- __le64 __user *steal_ptr;
- __le32 sequence_le;
- __le64 steal_le;
- u32 sequence;
- u64 steal;
+ struct sbi_sta_struct __user *st;
unsigned long hva;
+ bool updated = false;
gfn_t gfn;
if (shmem == INVALID_GPA)
@@ -52,35 +48,39 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
return;
}
- sequence_ptr = (__le32 __user *)(hva + offset_in_page(shmem) +
- offsetof(struct sbi_sta_struct, sequence));
- steal_ptr = (__le64 __user *)(hva + offset_in_page(shmem) +
- offsetof(struct sbi_sta_struct, steal));
+ st = (struct sbi_sta_struct __user *)(hva + offset_in_page(shmem));
+ scoped_user_rw_access(st, no_write) {
+ bool wrote_steal = false;
+ __le32 sequence;
+ __le64 steal;
- if (WARN_ON(get_user(sequence_le, sequence_ptr)))
- return;
-
- sequence = le32_to_cpu(sequence_le);
- sequence += 1;
-
- if (WARN_ON(put_user(cpu_to_le32(sequence), sequence_ptr)))
- return;
+ unsafe_get_user(sequence, &st->sequence, no_write);
+ le32_add_cpu(&sequence, 1);
+ unsafe_put_user(sequence, &st->sequence, no_write);
- /* 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);
+ /* Publish the odd sequence before updating stolen time. */
+ smp_wmb();
+ unsafe_get_user(steal, &st->steal, finish_update);
vcpu->arch.sta.last_steal = READ_ONCE(current->sched_info.run_delay);
- steal += vcpu->arch.sta.last_steal - last_steal;
- WARN_ON(put_user(cpu_to_le64(steal), steal_ptr));
+ le64_add_cpu(&steal, vcpu->arch.sta.last_steal - last_steal);
+ unsafe_put_user(steal, &st->steal, finish_update);
+ wrote_steal = true;
+
+finish_update:
+ /* Publish stolen time before the sequence becomes even. */
+ smp_wmb();
+ le32_add_cpu(&sequence, 1);
+ unsafe_put_user(sequence, &st->sequence, mark_dirty);
+ updated = wrote_steal;
}
- /* Publish stolen time before the sequence becomes even. */
- smp_wmb();
- sequence += 1;
- WARN_ON(put_user(cpu_to_le32(sequence), sequence_ptr));
-
+mark_dirty:
+ WARN_ON(!updated);
kvm_vcpu_mark_page_dirty(vcpu, gfn);
+ return;
+
+no_write:
+ WARN_ON(1);
}
static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vcpu *vcpu)
--
2.54.0
next prev parent 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 ` [PATCH v2 1/3] RISC-V: KVM: Order writes to the SBI STA shared structure Koichiro Den
2026-11-21 17:35 ` Koichiro Den [this message]
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-3-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®