kvm-riscv.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Anup Patel <anup@brainfault.org>
To: Zhe Qiao <qiaozhe@iscas.ac.cn>
Cc: atish.patra@linux.dev, paul.walmsley@sifive.com,
	palmer@dabbelt.com,  aou@eecs.berkeley.edu, alex@ghiti.fr,
	linux-riscv@lists.infradead.org,  kvm-riscv@lists.infradead.org,
	linux-kernel@vger.kernel.org,  kvm@vger.kernel.org
Subject: Re: [PATCH] RISCV: KVM: Add support for userspace to suspend a vCPU
Date: Thu, 6 Nov 2025 12:01:01 +0530	[thread overview]
Message-ID: <CAAhSdy2asCgoU+2q2wTQVCMD+LvdQjhJSbKUc-yRaGqmHnQoLQ@mail.gmail.com> (raw)
In-Reply-To: <20250918073927.403410-1-qiaozhe@iscas.ac.cn>

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

      reply	other threads:[~2025-11-06  6:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

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=CAAhSdy2asCgoU+2q2wTQVCMD+LvdQjhJSbKUc-yRaGqmHnQoLQ@mail.gmail.com \
    --to=anup@brainfault.org \
    --cc=alex@ghiti.fr \
    --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=paul.walmsley@sifive.com \
    --cc=qiaozhe@iscas.ac.cn \
    /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;
as well as URLs for NNTP newsgroup(s).