[<prev] [next>] [day] [month] [year] [list]
Message-ID: <20250918073927.403410-1-qiaozhe@iscas.ac.cn>
Date: Thu, 18 Sep 2025 15:39:27 +0800
From: Zhe Qiao <qiaozhe@...as.ac.cn>
To: anup@...infault.org,
atish.patra@...ux.dev,
paul.walmsley@...ive.com,
palmer@...belt.com,
aou@...s.berkeley.edu,
alex@...ti.fr,
qiaozhe@...as.ac.cn
Cc: linux-riscv@...ts.infradead.org,
kvm-riscv@...ts.infradead.org,
linux-kernel@...r.kernel.org,
kvm@...r.kernel.org
Subject: [PATCH] RISCV: KVM: Add support for userspace to suspend a vCPU
Add RISC-V architecture support for the KVM_MP_STATE_SUSPENDED vCPU
state, indicating that a vCPU is in suspended mode. While suspended,
the vCPU will block execution until a wakeup event is detected.
Introduce a new system event type, KVM_SYSTEM_EVENT_WAKEUP, to notify
userspace when KVM has recognized such a wakeup event. It is then
userspace’s responsibility to either make the vCPU runnable again or
keep it suspended until the next wakeup event occurs.
Signed-off-by: Zhe Qiao <qiaozhe@...as.ac.cn>
---
arch/riscv/include/asm/kvm_host.h | 2 ++
arch/riscv/kvm/vcpu.c | 37 +++++++++++++++++++++++++++++++
2 files changed, 39 insertions(+)
diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h
index d71d3299a335..dbc6391407ae 100644
--- a/arch/riscv/include/asm/kvm_host.h
+++ b/arch/riscv/include/asm/kvm_host.h
@@ -43,6 +43,8 @@
#define KVM_REQ_HFENCE \
KVM_ARCH_REQ_FLAGS(5, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
#define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(6)
+#define KVM_REQ_SUSPEND \
+ KVM_ARCH_REQ_FLAGS(7, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
#define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE
diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c
index 3ebcfffaa978..0881c78476b1 100644
--- a/arch/riscv/kvm/vcpu.c
+++ b/arch/riscv/kvm/vcpu.c
@@ -496,6 +496,18 @@ int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
return 0;
}
+static void kvm_riscv_vcpu_suspend(struct kvm_vcpu *vcpu)
+{
+ WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_SUSPENDED);
+ kvm_make_request(KVM_REQ_SUSPEND, vcpu);
+ kvm_vcpu_kick(vcpu);
+}
+
+static bool kvm_riscv_vcpu_suspended(struct kvm_vcpu *vcpu)
+{
+ return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_SUSPENDED;
+}
+
int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
struct kvm_mp_state *mp_state)
{
@@ -516,6 +528,9 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
else
ret = -EINVAL;
break;
+ case KVM_MP_STATE_SUSPENDED:
+ kvm_riscv_vcpu_suspend(vcpu);
+ break;
default:
ret = -EINVAL;
}
@@ -682,6 +697,25 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
}
}
+static int kvm_riscv_handle_suspend(struct kvm_vcpu *vcpu)
+{
+ if (!kvm_riscv_vcpu_suspended(vcpu))
+ return 1;
+
+ kvm_riscv_vcpu_wfi(vcpu);
+
+ kvm_make_request(KVM_REQ_SUSPEND, vcpu);
+
+ if (kvm_arch_vcpu_runnable(vcpu)) {
+ memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
+ vcpu->run->system_event.type = KVM_SYSTEM_EVENT_WAKEUP;
+ vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
+ return 0;
+ }
+
+ return 1;
+}
+
/**
* kvm_riscv_check_vcpu_requests - check and handle pending vCPU requests
* @vcpu: the VCPU pointer
@@ -731,6 +765,9 @@ static int kvm_riscv_check_vcpu_requests(struct kvm_vcpu *vcpu)
if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
kvm_riscv_vcpu_record_steal_time(vcpu);
+ if (kvm_check_request(KVM_REQ_SUSPEND, vcpu))
+ kvm_riscv_handle_suspend(vcpu);
+
if (kvm_dirty_ring_check_request(vcpu))
return 0;
}
--
2.43.0
Powered by blists - more mailing lists