Skip to contentSkip to conversation

RISC-V: KVM: Lessen steal time update overhead

PATCH In review Koichiro Den 3 participants 7 messages Nov 21 – Nov 29

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover RISC-V: KVM: Lessen steal time update overhead

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   2900.01 ns  2669.59 ns (-7.95%)
  contended     2876.42 ns  2670.01 ns (-7.18%)

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
---
v1->v2 changes:
- Address Andrew's feedback. See the individual patches for details.
- Rerun the benchmark on SpacemiT K3 and update the results.
- Add Andrew's Reviewed-by to patch 3.

v1: https://lore.kernel.org/r/[email protected]/

Koichiro Den (3):
RISC-V: KVM: Order writes to the SBI STA shared structure
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 | 68 ++++++++++++++++++++---------------
 1 file changed, 39 insertions(+), 29 deletions(-)

base-commit: e9bf49bbc4227048685c78f97e1446845760df4f

Signature · 1 line
2.54.0
#1 lore raw

PATCH v2 1/3 RISC-V: KVM: Order writes to the SBI STA shared structure

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")

---
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(+)
arch/riscv/kvm/vcpu_sbi_sta.c +5 -0
index 60e50296a008..f1be90b514b2 100644
@@ -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));
 
Signature · 1 line
2.54.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] RISC-V: KVM: Order writes to the SBI STA shared structure

Quoted 10 lines · Koichiro Den

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")
Quoted 8 lines

---
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(+)
#3 lore raw

PATCH v2 2/3 RISC-V: KVM: Batch user accesses when updating steal time

In reply to Koichiro Den

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.

---
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/[email protected]/

 arch/riscv/kvm/vcpu_sbi_sta.c | 60 +++++++++++++++++------------------
 1 file changed, 30 insertions(+), 30 deletions(-)
arch/riscv/kvm/vcpu_sbi_sta.c +30 -30
index f1be90b514b2..68d54696dbae 100644
@@ -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)
Signature · 1 line
2.54.0
Reviewed-by on v2 2/3
#4 lore raw

Re: [PATCH v2 2/3] RISC-V: KVM: Batch user accesses when updating steal time

Quoted 17 lines · Koichiro Den

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.

---
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.

Quoted 5 lines

v1: https://lore.kernel.org/r/[email protected]/

 arch/riscv/kvm/vcpu_sbi_sta.c | 60 +++++++++++++++++------------------
 1 file changed, 30 insertions(+), 30 deletions(-)
#5 lore raw

PATCH v2 3/3 RISC-V: KVM: Skip steal-time updates when run_delay is unchanged

In reply to Koichiro Den

Check run_delay against last_steal and skip the update if they match. This avoids looking up the memslot, accessing the SBI STA shared structure, and marking its page dirty on vCPU loads with no new steal time to report.

---
Changes since v1:
- Check run_delay before looking up the memslot. (Andrew)

v1: https://lore.kernel.org/r/[email protected]/

 arch/riscv/kvm/vcpu_sbi_sta.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/riscv/kvm/vcpu_sbi_sta.c +6 -1
index 68d54696dbae..ebdd1435abd4 100644
@@ -30,11 +30,16 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
 	struct sbi_sta_struct __user *st;
 	unsigned long hva;
 	bool updated = false;
+	u64 run_delay;
 	gfn_t gfn;
 
 	if (shmem == INVALID_GPA)
 		return;
 
+	run_delay = READ_ONCE(current->sched_info.run_delay);
+	if (run_delay == last_steal)
+		return;
+
 	/*
 	 * shmem is 64-byte aligned (see the enforcement in
 	 * kvm_sbi_sta_steal_time_set_shmem()) and the size of sbi_sta_struct
@@ -61,7 +66,7 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
 		/* 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);
+		vcpu->arch.sta.last_steal = run_delay;
 		le64_add_cpu(&steal, vcpu->arch.sta.last_steal - last_steal);
 		unsafe_put_user(steal, &st->steal, finish_update);
 		wrote_steal = true;
Signature · 1 line
2.54.0
maintainer
#6 lore raw
In reply to Koichiro Den
Quoted 42 lines · Koichiro Den

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   2900.01 ns  2669.59 ns (-7.95%)
  contended     2876.42 ns  2670.01 ns (-7.18%)

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
---
v1->v2 changes:
- Address Andrew's feedback. See the individual patches for details.
- Rerun the benchmark on SpacemiT K3 and update the results.
- Add Andrew's Reviewed-by to patch 3.

v1: https://lore.kernel.org/r/[email protected]/

Koichiro Den (3):
RISC-V: KVM: Order writes to the SBI STA shared structure
RISC-V: KVM: Batch user accesses when updating steal time
RISC-V: KVM: Skip steal-time updates when run_delay is unchanged

Quoted 8 lines
 arch/riscv/kvm/vcpu_sbi_sta.c | 68 ++++++++++++++++++++---------------
 1 file changed, 39 insertions(+), 29 deletions(-)

base-commit: e9bf49bbc4227048685c78f97e1446845760df4f --
2.54.0

Queued this series for Linux-7.5

Thanks,
Anup