* [PATCH] RISCV: KVM: Add support for userspace to suspend a vCPU
@ 2025-09-18 7:39 Zhe Qiao
2025-11-06 6:31 ` Anup Patel
0 siblings, 1 reply; 2+ messages in thread
From: Zhe Qiao @ 2025-09-18 7:39 UTC (permalink / raw)
To: anup, atish.patra, paul.walmsley, palmer, aou, alex, qiaozhe
Cc: linux-riscv, kvm-riscv, linux-kernel, kvm
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@iscas.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
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] RISCV: KVM: Add support for userspace to suspend a vCPU
2025-09-18 7:39 [PATCH] RISCV: KVM: Add support for userspace to suspend a vCPU Zhe Qiao
@ 2025-11-06 6:31 ` Anup Patel
0 siblings, 0 replies; 2+ messages in thread
From: Anup Patel @ 2025-11-06 6:31 UTC (permalink / raw)
To: Zhe Qiao
Cc: atish.patra, paul.walmsley, palmer, aou, alex, linux-riscv,
kvm-riscv, linux-kernel, kvm
On Thu, Sep 18, 2025 at 1:09 PM Zhe Qiao <qiaozhe@iscas.ac.cn> wrote:
>
> 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.
There is no clear use-case why KVM user-space would put a VCPU in
suspended state while it is running.
>
> Introduce a new system event type, KVM_SYSTEM_EVENT_WAKEUP, to notify
As the name suggests, the KVM_SYSTEM_EVENT_WAKEUP is for
system-level (or vm-level) wake-up and not VCPU-level wake-up.
> 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@iscas.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);
Why ignore the return value of kvm_riscv_handle_suspend() ?
> +
> if (kvm_dirty_ring_check_request(vcpu))
> return 0;
> }
> --
> 2.43.0
>
Regards,
Anup
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-11-06 6:31 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-18 7:39 [PATCH] RISCV: KVM: Add support for userspace to suspend a vCPU Zhe Qiao
2025-11-06 6:31 ` Anup Patel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).