* [PATCH 0/2] RISC-V: KVM: Lessen steal time update overhead
@ 2026-11-18 6:22 Koichiro Den
2026-11-18 6:22 ` [PATCH 1/2] RISC-V: KVM: Batch user accesses when updating steal time Koichiro Den
2026-11-18 6:22 ` [PATCH 2/2] RISC-V: KVM: Skip steal-time updates when run_delay is unchanged Koichiro Den
0 siblings, 2 replies; 7+ messages in thread
From: Koichiro Den @ 2026-11-18 6:22 UTC (permalink / raw)
To: Anup Patel, Atish Patra
Cc: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
Bingyu Xian, kvm, kvm-riscv, linux-riscv, linux-kernel
Hi,
For guests that register an SBI STA shared-memory area, this small patch
set reduces update overhead by batching user accesses and also skipping
updates when run_delay is unchanged.
Tested on SpacemiT K3:
The benchmark uses KVM selftest helpers. The guest registers its STA
shared memory and calls GUEST_UCALL_NONE() in a loop. The vCPU thread is
pinned to one host CPU and re-enters KVM_RUN after each exit.
The table shows the median vCPU thread CPU time per round trip (from
host userspace to the guest and back), measured with
CLOCK_THREAD_CPUTIME_ID. Each case has five runs of 1,000,000
iterations, preceded by 10,000 warm-up iterations per run.
Below, "contended" adds a busy loop on that CPU.
Before After
------------ ---------- -------------------
uncontended 2966.51 ns 2703.26 ns (-8.87%)
contended 2973.46 ns 2717.72 ns (-8.60%)
Based off of riscv_kvm_next, commit e9bf49bbc422 ("RISC-V: KVM: Fix
G-stage page table memory leak on VM destroy")
Best regards,
Koichiro Den
Koichiro Den (2):
RISC-V: KVM: Batch user accesses when updating steal time
RISC-V: KVM: Skip steal-time updates when run_delay is unchanged
arch/riscv/kvm/vcpu_sbi_sta.c | 61 +++++++++++++++++++++--------------
1 file changed, 37 insertions(+), 24 deletions(-)
base-commit: e9bf49bbc4227048685c78f97e1446845760df4f
--
2.54.0
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 1/2] RISC-V: KVM: Batch user accesses when updating steal time
2026-11-18 6:22 [PATCH 0/2] RISC-V: KVM: Lessen steal time update overhead Koichiro Den
@ 2026-11-18 6:22 ` Koichiro Den
2026-11-20 16:40 ` Andrew Jones
2026-11-18 6:22 ` [PATCH 2/2] RISC-V: KVM: Skip steal-time updates when run_delay is unchanged Koichiro Den
1 sibling, 1 reply; 7+ messages in thread
From: Koichiro Den @ 2026-11-18 6:22 UTC (permalink / raw)
To: Anup Patel, Atish Patra
Cc: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
Bingyu Xian, kvm, kvm-riscv, linux-riscv, linux-kernel
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.
Note that write barriers are added to order the odd sequence store
before the steal-time update and the even sequence store after it.
Signed-off-by: Koichiro Den <den@valinux.co.jp>
---
To reviewers: please let me know if you'd prefer
scoped_user_rw_access() here.
arch/riscv/kvm/vcpu_sbi_sta.c | 59 ++++++++++++++++++++---------------
1 file changed, 34 insertions(+), 25 deletions(-)
diff --git a/arch/riscv/kvm/vcpu_sbi_sta.c b/arch/riscv/kvm/vcpu_sbi_sta.c
index 60e50296a008..cfc6f22c3e33 100644
--- a/arch/riscv/kvm/vcpu_sbi_sta.c
+++ b/arch/riscv/kvm/vcpu_sbi_sta.c
@@ -8,13 +8,14 @@
#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>
#include <asm/current.h>
#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)
{
@@ -26,13 +27,13 @@ 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;
+ struct sbi_sta_struct __user *st;
__le32 sequence_le;
__le64 steal_le;
u32 sequence;
u64 steal;
unsigned long hva;
+ bool dirty = false, success = false;
gfn_t gfn;
if (shmem == INVALID_GPA)
@@ -51,31 +52,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));
-
- 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)))
+ st = (struct sbi_sta_struct __user *)(hva + offset_in_page(shmem));
+ might_fault();
+ if (WARN_ON(!user_access_begin(st, sizeof(*st))))
return;
- 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);
- steal += vcpu->arch.sta.last_steal - last_steal;
- WARN_ON(put_user(cpu_to_le64(steal), steal_ptr));
- }
-
+ unsafe_get_user(sequence_le, &st->sequence, fault);
+ sequence = le32_to_cpu(sequence_le) + 1;
+ unsafe_put_user(cpu_to_le32(sequence), &st->sequence, fault);
+ dirty = true;
+
+ /* Publish the odd sequence before updating stolen time. */
+ smp_wmb();
+ unsafe_get_user(steal_le, &st->steal, update_sequence);
+ steal = le64_to_cpu(steal_le);
+ vcpu->arch.sta.last_steal = READ_ONCE(current->sched_info.run_delay);
+ steal += vcpu->arch.sta.last_steal - last_steal;
+ unsafe_put_user(cpu_to_le64(steal), &st->steal, update_sequence);
+ success = true;
+
+update_sequence:
+ /* Publish stolen time before completing the update. */
+ smp_wmb();
sequence += 1;
- WARN_ON(put_user(cpu_to_le32(sequence), sequence_ptr));
-
- kvm_vcpu_mark_page_dirty(vcpu, gfn);
+ unsafe_put_user(cpu_to_le32(sequence), &st->sequence, fault);
+ goto out;
+
+fault:
+ success = false;
+out:
+ user_access_end();
+ WARN_ON(!success);
+ if (dirty)
+ kvm_vcpu_mark_page_dirty(vcpu, gfn);
}
static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vcpu *vcpu)
--
2.54.0
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 2/2] RISC-V: KVM: Skip steal-time updates when run_delay is unchanged
2026-11-18 6:22 [PATCH 0/2] RISC-V: KVM: Lessen steal time update overhead Koichiro Den
2026-11-18 6:22 ` [PATCH 1/2] RISC-V: KVM: Batch user accesses when updating steal time Koichiro Den
@ 2026-11-18 6:22 ` Koichiro Den
2026-11-20 16:45 ` Andrew Jones
1 sibling, 1 reply; 7+ messages in thread
From: Koichiro Den @ 2026-11-18 6:22 UTC (permalink / raw)
To: Anup Patel, Atish Patra
Cc: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
Bingyu Xian, kvm, kvm-riscv, linux-riscv, linux-kernel
Check run_delay against last_steal and skip the update if they match.
This avoids accessing the SBI STA shared structure and marking its page
dirty on vCPU loads with no new steal time to report.
Signed-off-by: Koichiro Den <den@valinux.co.jp>
---
arch/riscv/kvm/vcpu_sbi_sta.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/arch/riscv/kvm/vcpu_sbi_sta.c b/arch/riscv/kvm/vcpu_sbi_sta.c
index cfc6f22c3e33..8b2499876fb4 100644
--- a/arch/riscv/kvm/vcpu_sbi_sta.c
+++ b/arch/riscv/kvm/vcpu_sbi_sta.c
@@ -31,7 +31,7 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
__le32 sequence_le;
__le64 steal_le;
u32 sequence;
- u64 steal;
+ u64 steal, run_delay;
unsigned long hva;
bool dirty = false, success = false;
gfn_t gfn;
@@ -52,6 +52,10 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
return;
}
+ run_delay = READ_ONCE(current->sched_info.run_delay);
+ if (run_delay == last_steal)
+ return;
+
st = (struct sbi_sta_struct __user *)(hva + offset_in_page(shmem));
might_fault();
if (WARN_ON(!user_access_begin(st, sizeof(*st))))
@@ -66,7 +70,7 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
smp_wmb();
unsafe_get_user(steal_le, &st->steal, update_sequence);
steal = le64_to_cpu(steal_le);
- vcpu->arch.sta.last_steal = READ_ONCE(current->sched_info.run_delay);
+ vcpu->arch.sta.last_steal = run_delay;
steal += vcpu->arch.sta.last_steal - last_steal;
unsafe_put_user(cpu_to_le64(steal), &st->steal, update_sequence);
success = true;
--
2.54.0
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] RISC-V: KVM: Batch user accesses when updating steal time
2026-11-18 6:22 ` [PATCH 1/2] RISC-V: KVM: Batch user accesses when updating steal time Koichiro Den
@ 2026-11-20 16:40 ` Andrew Jones
2026-11-20 17:20 ` Koichiro Den
0 siblings, 1 reply; 7+ messages in thread
From: Andrew Jones @ 2026-11-20 16:40 UTC (permalink / raw)
To: Koichiro Den
Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt, Albert Ou,
Alexandre Ghiti, Bingyu Xian, kvm, kvm-riscv, linux-riscv,
linux-kernel
On Wed, Nov 18, 2026 at 03:22:41PM +0900, Koichiro Den wrote:
> 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.
>
> Note that write barriers are added to order the odd sequence store
> before the steal-time update and the even sequence store after it.
The barrier additions should probably be posted separately with a Fixes
tag.
>
> Signed-off-by: Koichiro Den <den@valinux.co.jp>
> ---
> To reviewers: please let me know if you'd prefer
> scoped_user_rw_access() here.
>
> arch/riscv/kvm/vcpu_sbi_sta.c | 59 ++++++++++++++++++++---------------
> 1 file changed, 34 insertions(+), 25 deletions(-)
>
> diff --git a/arch/riscv/kvm/vcpu_sbi_sta.c b/arch/riscv/kvm/vcpu_sbi_sta.c
> index 60e50296a008..cfc6f22c3e33 100644
> --- a/arch/riscv/kvm/vcpu_sbi_sta.c
> +++ b/arch/riscv/kvm/vcpu_sbi_sta.c
> @@ -8,13 +8,14 @@
> #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>
> #include <asm/current.h>
> #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)
> {
> @@ -26,13 +27,13 @@ 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;
> + struct sbi_sta_struct __user *st;
> __le32 sequence_le;
> __le64 steal_le;
> u32 sequence;
> u64 steal;
> unsigned long hva;
> + bool dirty = false, success = false;
> gfn_t gfn;
>
> if (shmem == INVALID_GPA)
> @@ -51,31 +52,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));
> -
> - 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)))
> + st = (struct sbi_sta_struct __user *)(hva + offset_in_page(shmem));
> + might_fault();
We're probably safe to drop this might_fault(). But either way it's fine.
> + if (WARN_ON(!user_access_begin(st, sizeof(*st))))
> return;
>
> - 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);
> - steal += vcpu->arch.sta.last_steal - last_steal;
> - WARN_ON(put_user(cpu_to_le64(steal), steal_ptr));
> - }
> -
> + unsafe_get_user(sequence_le, &st->sequence, fault);
> + sequence = le32_to_cpu(sequence_le) + 1;
> + unsafe_put_user(cpu_to_le32(sequence), &st->sequence, fault);
> + dirty = true;
> +
> + /* Publish the odd sequence before updating stolen time. */
> + smp_wmb();
> + unsafe_get_user(steal_le, &st->steal, update_sequence);
> + steal = le64_to_cpu(steal_le);
> + vcpu->arch.sta.last_steal = READ_ONCE(current->sched_info.run_delay);
> + steal += vcpu->arch.sta.last_steal - last_steal;
> + unsafe_put_user(cpu_to_le64(steal), &st->steal, update_sequence);
> + success = true;
> +
> +update_sequence:
> + /* Publish stolen time before completing the update. */
> + smp_wmb();
> sequence += 1;
> - WARN_ON(put_user(cpu_to_le32(sequence), sequence_ptr));
> -
> - kvm_vcpu_mark_page_dirty(vcpu, gfn);
> + unsafe_put_user(cpu_to_le32(sequence), &st->sequence, fault);
> + goto out;
> +
> +fault:
> + success = false;
> +out:
> + user_access_end();
> + WARN_ON(!success);
> + if (dirty)
> + kvm_vcpu_mark_page_dirty(vcpu, gfn);
> }
>
> static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vcpu *vcpu)
> --
> 2.54.0
>
Otherwise,
Reviewed-by: Andrew Jones <andrew.jones@linux.dev>
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] RISC-V: KVM: Skip steal-time updates when run_delay is unchanged
2026-11-18 6:22 ` [PATCH 2/2] RISC-V: KVM: Skip steal-time updates when run_delay is unchanged Koichiro Den
@ 2026-11-20 16:45 ` Andrew Jones
2026-11-20 17:25 ` Koichiro Den
0 siblings, 1 reply; 7+ messages in thread
From: Andrew Jones @ 2026-11-20 16:45 UTC (permalink / raw)
To: Koichiro Den
Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt, Albert Ou,
Alexandre Ghiti, Bingyu Xian, kvm, kvm-riscv, linux-riscv,
linux-kernel
On Wed, Nov 18, 2026 at 03:22:42PM +0900, Koichiro Den wrote:
> Check run_delay against last_steal and skip the update if they match.
> This avoids accessing the SBI STA shared structure and marking its page
> dirty on vCPU loads with no new steal time to report.
>
> Signed-off-by: Koichiro Den <den@valinux.co.jp>
> ---
> arch/riscv/kvm/vcpu_sbi_sta.c | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/arch/riscv/kvm/vcpu_sbi_sta.c b/arch/riscv/kvm/vcpu_sbi_sta.c
> index cfc6f22c3e33..8b2499876fb4 100644
> --- a/arch/riscv/kvm/vcpu_sbi_sta.c
> +++ b/arch/riscv/kvm/vcpu_sbi_sta.c
> @@ -31,7 +31,7 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
> __le32 sequence_le;
> __le64 steal_le;
> u32 sequence;
> - u64 steal;
> + u64 steal, run_delay;
> unsigned long hva;
> bool dirty = false, success = false;
> gfn_t gfn;
> @@ -52,6 +52,10 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
> return;
> }
>
> + run_delay = READ_ONCE(current->sched_info.run_delay);
> + if (run_delay == last_steal)
> + return;
> +
If we move the new check to just below the 'if (shmem == INVALID_GPA)'
check then we also avoid a memslot lookup.
> st = (struct sbi_sta_struct __user *)(hva + offset_in_page(shmem));
> might_fault();
> if (WARN_ON(!user_access_begin(st, sizeof(*st))))
> @@ -66,7 +70,7 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
> smp_wmb();
> unsafe_get_user(steal_le, &st->steal, update_sequence);
> steal = le64_to_cpu(steal_le);
> - vcpu->arch.sta.last_steal = READ_ONCE(current->sched_info.run_delay);
> + vcpu->arch.sta.last_steal = run_delay;
> steal += vcpu->arch.sta.last_steal - last_steal;
> unsafe_put_user(cpu_to_le64(steal), &st->steal, update_sequence);
> success = true;
> --
> 2.54.0
>
Otherwise,
Reviewed-by: Andrew Jones <andrew.jones@linux.dev>
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] RISC-V: KVM: Batch user accesses when updating steal time
2026-11-20 16:40 ` Andrew Jones
@ 2026-11-20 17:20 ` Koichiro Den
0 siblings, 0 replies; 7+ messages in thread
From: Koichiro Den @ 2026-11-20 17:20 UTC (permalink / raw)
To: Andrew Jones
Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt, Albert Ou,
Alexandre Ghiti, Bingyu Xian, kvm, kvm-riscv, linux-riscv,
linux-kernel
On Fri, Nov 20, 2026 at 05:40:29PM +0100, Andrew Jones wrote:
> On Wed, Nov 18, 2026 at 03:22:41PM +0900, Koichiro Den wrote:
> > 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.
> >
> > Note that write barriers are added to order the odd sequence store
> > before the steal-time update and the even sequence store after it.
Hi Andrew, thanks for the review.
>
> The barrier additions should probably be posted separately with a Fixes
> tag.
That's exactly what I wasn't sure about when I read the ISA spec.
It was unclear to me from the Zicsr ordering rules whether the SUM toggle in
the old code's get_user() and put_user() calls also order the stores to 'sequence'
and 'steal' as seen by another hart.
(I don't think they do, but I'm not 100% sure. It feels a bit like trying to
prove a negative, as I couldn't find a clear statement. I may have missed one
though. OTOH, the new code surely needs the barrier, so I included them.)
Do you read the spec as allowing another hart to observe those stores out of
order in the old code? If so, I agree the barriers can go in a separate patch.
>
> >
> > Signed-off-by: Koichiro Den <den@valinux.co.jp>
> > ---
> > To reviewers: please let me know if you'd prefer
> > scoped_user_rw_access() here.
> >
> > arch/riscv/kvm/vcpu_sbi_sta.c | 59 ++++++++++++++++++++---------------
> > 1 file changed, 34 insertions(+), 25 deletions(-)
> >
> > diff --git a/arch/riscv/kvm/vcpu_sbi_sta.c b/arch/riscv/kvm/vcpu_sbi_sta.c
> > index 60e50296a008..cfc6f22c3e33 100644
> > --- a/arch/riscv/kvm/vcpu_sbi_sta.c
> > +++ b/arch/riscv/kvm/vcpu_sbi_sta.c
> > @@ -8,13 +8,14 @@
> > #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>
> > #include <asm/current.h>
> > #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)
> > {
> > @@ -26,13 +27,13 @@ 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;
> > + struct sbi_sta_struct __user *st;
> > __le32 sequence_le;
> > __le64 steal_le;
> > u32 sequence;
> > u64 steal;
> > unsigned long hva;
> > + bool dirty = false, success = false;
> > gfn_t gfn;
> >
> > if (shmem == INVALID_GPA)
> > @@ -51,31 +52,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));
> > -
> > - 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)))
> > + st = (struct sbi_sta_struct __user *)(hva + offset_in_page(shmem));
> > + might_fault();
>
> We're probably safe to drop this might_fault(). But either way it's fine.
I kept it to preserve the check we get from get_user()/put_user().
I don't think it's indispensable here either. I'll probably drop it in v2.
Best regards,
Koichiro Den
>
> > + if (WARN_ON(!user_access_begin(st, sizeof(*st))))
> > return;
> >
> > - 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);
> > - steal += vcpu->arch.sta.last_steal - last_steal;
> > - WARN_ON(put_user(cpu_to_le64(steal), steal_ptr));
> > - }
> > -
> > + unsafe_get_user(sequence_le, &st->sequence, fault);
> > + sequence = le32_to_cpu(sequence_le) + 1;
> > + unsafe_put_user(cpu_to_le32(sequence), &st->sequence, fault);
> > + dirty = true;
> > +
> > + /* Publish the odd sequence before updating stolen time. */
> > + smp_wmb();
> > + unsafe_get_user(steal_le, &st->steal, update_sequence);
> > + steal = le64_to_cpu(steal_le);
> > + vcpu->arch.sta.last_steal = READ_ONCE(current->sched_info.run_delay);
> > + steal += vcpu->arch.sta.last_steal - last_steal;
> > + unsafe_put_user(cpu_to_le64(steal), &st->steal, update_sequence);
> > + success = true;
> > +
> > +update_sequence:
> > + /* Publish stolen time before completing the update. */
> > + smp_wmb();
> > sequence += 1;
> > - WARN_ON(put_user(cpu_to_le32(sequence), sequence_ptr));
> > -
> > - kvm_vcpu_mark_page_dirty(vcpu, gfn);
> > + unsafe_put_user(cpu_to_le32(sequence), &st->sequence, fault);
> > + goto out;
> > +
> > +fault:
> > + success = false;
> > +out:
> > + user_access_end();
> > + WARN_ON(!success);
> > + if (dirty)
> > + kvm_vcpu_mark_page_dirty(vcpu, gfn);
> > }
> >
> > static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vcpu *vcpu)
> > --
> > 2.54.0
> >
>
> Otherwise,
>
> Reviewed-by: Andrew Jones <andrew.jones@linux.dev>
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] RISC-V: KVM: Skip steal-time updates when run_delay is unchanged
2026-11-20 16:45 ` Andrew Jones
@ 2026-11-20 17:25 ` Koichiro Den
0 siblings, 0 replies; 7+ messages in thread
From: Koichiro Den @ 2026-11-20 17:25 UTC (permalink / raw)
To: Andrew Jones
Cc: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt, Albert Ou,
Alexandre Ghiti, Bingyu Xian, kvm, kvm-riscv, linux-riscv,
linux-kernel
On Fri, Nov 20, 2026 at 05:45:09PM +0100, Andrew Jones wrote:
> On Wed, Nov 18, 2026 at 03:22:42PM +0900, Koichiro Den wrote:
> > Check run_delay against last_steal and skip the update if they match.
> > This avoids accessing the SBI STA shared structure and marking its page
> > dirty on vCPU loads with no new steal time to report.
> >
> > Signed-off-by: Koichiro Den <den@valinux.co.jp>
> > ---
> > arch/riscv/kvm/vcpu_sbi_sta.c | 8 ++++++--
> > 1 file changed, 6 insertions(+), 2 deletions(-)
> >
> > diff --git a/arch/riscv/kvm/vcpu_sbi_sta.c b/arch/riscv/kvm/vcpu_sbi_sta.c
> > index cfc6f22c3e33..8b2499876fb4 100644
> > --- a/arch/riscv/kvm/vcpu_sbi_sta.c
> > +++ b/arch/riscv/kvm/vcpu_sbi_sta.c
> > @@ -31,7 +31,7 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
> > __le32 sequence_le;
> > __le64 steal_le;
> > u32 sequence;
> > - u64 steal;
> > + u64 steal, run_delay;
> > unsigned long hva;
> > bool dirty = false, success = false;
> > gfn_t gfn;
> > @@ -52,6 +52,10 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
> > return;
> > }
> >
> > + run_delay = READ_ONCE(current->sched_info.run_delay);
> > + if (run_delay == last_steal)
> > + return;
> > +
>
> If we move the new check to just below the 'if (shmem == INVALID_GPA)'
> check then we also avoid a memslot lookup.
Indeed. Thanks for pointing that out! Will move the check in v2.
Best regards,
Koichiro Den
>
> > st = (struct sbi_sta_struct __user *)(hva + offset_in_page(shmem));
> > might_fault();
> > if (WARN_ON(!user_access_begin(st, sizeof(*st))))
> > @@ -66,7 +70,7 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
> > smp_wmb();
> > unsafe_get_user(steal_le, &st->steal, update_sequence);
> > steal = le64_to_cpu(steal_le);
> > - vcpu->arch.sta.last_steal = READ_ONCE(current->sched_info.run_delay);
> > + vcpu->arch.sta.last_steal = run_delay;
> > steal += vcpu->arch.sta.last_steal - last_steal;
> > unsafe_put_user(cpu_to_le64(steal), &st->steal, update_sequence);
> > success = true;
> > --
> > 2.54.0
> >
>
> Otherwise,
>
> Reviewed-by: Andrew Jones <andrew.jones@linux.dev>
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-20 17:25 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 6:22 [PATCH 0/2] RISC-V: KVM: Lessen steal time update overhead Koichiro Den
2026-11-18 6:22 ` [PATCH 1/2] RISC-V: KVM: Batch user accesses when updating steal time Koichiro Den
2026-11-20 16:40 ` Andrew Jones
2026-11-20 17:20 ` Koichiro Den
2026-11-18 6:22 ` [PATCH 2/2] RISC-V: KVM: Skip steal-time updates when run_delay is unchanged Koichiro Den
2026-11-20 16:45 ` Andrew Jones
2026-11-20 17:25 ` Koichiro Den
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox