All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Radim Krčmář" <radim.krcmar@oss.qualcomm.com>
To: "Jinyu Tang" <tjytimi@163.com>,
	"Anup Patel" <anup@brainfault.org>,
	"Atish Patra" <atish.patra@linux.dev>,
	"Andrew Jones" <andrew.jones@oss.qualcomm.com>,
	"Conor Dooley" <conor.dooley@microchip.com>,
	"Yong-Xuan Wang" <yongxuan.wang@sifive.com>,
	"Nutty Liu" <nutty.liu@hotmail.com>,
	"Paul Walmsley" <pjw@kernel.org>,
	"Paul Walmsley" <paul.walmsley@sifive.com>,
	"Palmer Dabbelt" <palmer@dabbelt.com>,
	"Albert Ou" <aou@eecs.berkeley.edu>,
	"Alexandre Ghiti" <alex@ghiti.fr>
Cc: <kvm@vger.kernel.org>, <kvm-riscv@lists.infradead.org>,
	<linux-riscv@lists.infradead.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v7] KVM: riscv: Skip CSR restore if VCPU is reloaded on the same core
Date: Fri, 27 Feb 2026 14:00:43 +0000	[thread overview]
Message-ID: <DGPSROCWMJWD.33FHG4JOPD2V8@oss.qualcomm.com> (raw)
In-Reply-To: <20260227121008.442241-1-tjytimi@163.com>

2026-02-27T20:10:08+08:00, Jinyu Tang <tjytimi@163.com>:
> Currently, kvm_arch_vcpu_load() unconditionally restores guest CSRs,
> HGATP, and AIA state. However, when a VCPU is loaded back on the same
> physical CPU, and no other KVM VCPU has run on this CPU since it was
> last put, the hardware CSRs and AIA registers are still valid.
>
> This patch optimizes the vcpu_load path by skipping the expensive CSR
> and AIA writes if all the following conditions are met:
> 1. It is being reloaded on the same CPU (vcpu->arch.last_exit_cpu == cpu).
> 2. The CSRs are not dirty (!vcpu->arch.csr_dirty).
> 3. No other VCPU used this CPU (vcpu == __this_cpu_read(kvm_former_vcpu)).
>
> To ensure this fast-path doesn't break corner cases:
> - Live migration and VCPU reset are naturally safe. KVM initializes
>   last_exit_cpu to -1, which guarantees the fast-path won't trigger.
> - The 'csr_dirty' flag tracks runtime userspace interventions. If
>   userspace modifies guest configurations (e.g., hedeleg via
>   KVM_SET_GUEST_DEBUG, or CSRs including AIA via KVM_SET_ONE_REG),
>   the flag is set to skip the fast path.
>
> With the 'csr_dirty' safeguard proven effective, it is safe to
> include kvm_riscv_vcpu_aia_load() inside the skip logic now.
>
> Signed-off-by: Jinyu Tang <tjytimi@163.com>
> ---
>  v6 -> v7:
>  - Moved kvm_riscv_vcpu_aia_load() into the fast-path skip logic, as
>    suggested by Radim Krčmář.
>  - Verified the fix for the IMSIC instability issue reported in v3.
>    Testing was conducted on QEMU 10.0.2 with explicitly enabled AIA
>    (`-machine virt,aia=aplic-imsic`). The guest boots successfully 
>    using virtio-mmio devices like virtio-blk and virtio-net.

Reviewed-by: Radim Krčmář <radim.krcmar@oss.qualcomm.com>

Thanks.

-- 
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv

WARNING: multiple messages have this Message-ID (diff)
From: "Radim Krčmář" <radim.krcmar@oss.qualcomm.com>
To: "Jinyu Tang" <tjytimi@163.com>,
	"Anup Patel" <anup@brainfault.org>,
	"Atish Patra" <atish.patra@linux.dev>,
	"Andrew Jones" <andrew.jones@oss.qualcomm.com>,
	"Conor Dooley" <conor.dooley@microchip.com>,
	"Yong-Xuan Wang" <yongxuan.wang@sifive.com>,
	"Nutty Liu" <nutty.liu@hotmail.com>,
	"Paul Walmsley" <pjw@kernel.org>,
	"Paul Walmsley" <paul.walmsley@sifive.com>,
	"Palmer Dabbelt" <palmer@dabbelt.com>,
	"Albert Ou" <aou@eecs.berkeley.edu>,
	"Alexandre Ghiti" <alex@ghiti.fr>
Cc: <kvm@vger.kernel.org>, <kvm-riscv@lists.infradead.org>,
	<linux-riscv@lists.infradead.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v7] KVM: riscv: Skip CSR restore if VCPU is reloaded on the same core
Date: Fri, 27 Feb 2026 14:00:43 +0000	[thread overview]
Message-ID: <DGPSROCWMJWD.33FHG4JOPD2V8@oss.qualcomm.com> (raw)
In-Reply-To: <20260227121008.442241-1-tjytimi@163.com>

2026-02-27T20:10:08+08:00, Jinyu Tang <tjytimi@163.com>:
> Currently, kvm_arch_vcpu_load() unconditionally restores guest CSRs,
> HGATP, and AIA state. However, when a VCPU is loaded back on the same
> physical CPU, and no other KVM VCPU has run on this CPU since it was
> last put, the hardware CSRs and AIA registers are still valid.
>
> This patch optimizes the vcpu_load path by skipping the expensive CSR
> and AIA writes if all the following conditions are met:
> 1. It is being reloaded on the same CPU (vcpu->arch.last_exit_cpu == cpu).
> 2. The CSRs are not dirty (!vcpu->arch.csr_dirty).
> 3. No other VCPU used this CPU (vcpu == __this_cpu_read(kvm_former_vcpu)).
>
> To ensure this fast-path doesn't break corner cases:
> - Live migration and VCPU reset are naturally safe. KVM initializes
>   last_exit_cpu to -1, which guarantees the fast-path won't trigger.
> - The 'csr_dirty' flag tracks runtime userspace interventions. If
>   userspace modifies guest configurations (e.g., hedeleg via
>   KVM_SET_GUEST_DEBUG, or CSRs including AIA via KVM_SET_ONE_REG),
>   the flag is set to skip the fast path.
>
> With the 'csr_dirty' safeguard proven effective, it is safe to
> include kvm_riscv_vcpu_aia_load() inside the skip logic now.
>
> Signed-off-by: Jinyu Tang <tjytimi@163.com>
> ---
>  v6 -> v7:
>  - Moved kvm_riscv_vcpu_aia_load() into the fast-path skip logic, as
>    suggested by Radim Krčmář.
>  - Verified the fix for the IMSIC instability issue reported in v3.
>    Testing was conducted on QEMU 10.0.2 with explicitly enabled AIA
>    (`-machine virt,aia=aplic-imsic`). The guest boots successfully 
>    using virtio-mmio devices like virtio-blk and virtio-net.

Reviewed-by: Radim Krčmář <radim.krcmar@oss.qualcomm.com>

Thanks.

WARNING: multiple messages have this Message-ID (diff)
From: "Radim Krčmář" <radim.krcmar@oss.qualcomm.com>
To: "Jinyu Tang" <tjytimi@163.com>,
	"Anup Patel" <anup@brainfault.org>,
	"Atish Patra" <atish.patra@linux.dev>,
	"Andrew Jones" <andrew.jones@oss.qualcomm.com>,
	"Conor Dooley" <conor.dooley@microchip.com>,
	"Yong-Xuan Wang" <yongxuan.wang@sifive.com>,
	"Nutty Liu" <nutty.liu@hotmail.com>,
	"Paul Walmsley" <pjw@kernel.org>,
	"Paul Walmsley" <paul.walmsley@sifive.com>,
	"Palmer Dabbelt" <palmer@dabbelt.com>,
	"Albert Ou" <aou@eecs.berkeley.edu>,
	"Alexandre Ghiti" <alex@ghiti.fr>
Cc: <kvm@vger.kernel.org>, <kvm-riscv@lists.infradead.org>,
	<linux-riscv@lists.infradead.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v7] KVM: riscv: Skip CSR restore if VCPU is reloaded on the same core
Date: Fri, 27 Feb 2026 14:00:43 +0000	[thread overview]
Message-ID: <DGPSROCWMJWD.33FHG4JOPD2V8@oss.qualcomm.com> (raw)
In-Reply-To: <20260227121008.442241-1-tjytimi@163.com>

2026-02-27T20:10:08+08:00, Jinyu Tang <tjytimi@163.com>:
> Currently, kvm_arch_vcpu_load() unconditionally restores guest CSRs,
> HGATP, and AIA state. However, when a VCPU is loaded back on the same
> physical CPU, and no other KVM VCPU has run on this CPU since it was
> last put, the hardware CSRs and AIA registers are still valid.
>
> This patch optimizes the vcpu_load path by skipping the expensive CSR
> and AIA writes if all the following conditions are met:
> 1. It is being reloaded on the same CPU (vcpu->arch.last_exit_cpu == cpu).
> 2. The CSRs are not dirty (!vcpu->arch.csr_dirty).
> 3. No other VCPU used this CPU (vcpu == __this_cpu_read(kvm_former_vcpu)).
>
> To ensure this fast-path doesn't break corner cases:
> - Live migration and VCPU reset are naturally safe. KVM initializes
>   last_exit_cpu to -1, which guarantees the fast-path won't trigger.
> - The 'csr_dirty' flag tracks runtime userspace interventions. If
>   userspace modifies guest configurations (e.g., hedeleg via
>   KVM_SET_GUEST_DEBUG, or CSRs including AIA via KVM_SET_ONE_REG),
>   the flag is set to skip the fast path.
>
> With the 'csr_dirty' safeguard proven effective, it is safe to
> include kvm_riscv_vcpu_aia_load() inside the skip logic now.
>
> Signed-off-by: Jinyu Tang <tjytimi@163.com>
> ---
>  v6 -> v7:
>  - Moved kvm_riscv_vcpu_aia_load() into the fast-path skip logic, as
>    suggested by Radim Krčmář.
>  - Verified the fix for the IMSIC instability issue reported in v3.
>    Testing was conducted on QEMU 10.0.2 with explicitly enabled AIA
>    (`-machine virt,aia=aplic-imsic`). The guest boots successfully 
>    using virtio-mmio devices like virtio-blk and virtio-net.

Reviewed-by: Radim Krčmář <radim.krcmar@oss.qualcomm.com>

Thanks.

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

  reply	other threads:[~2026-02-27 14:01 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-27 12:10 [PATCH v7] KVM: riscv: Skip CSR restore if VCPU is reloaded on the same core Jinyu Tang
2026-02-27 12:10 ` Jinyu Tang
2026-02-27 12:10 ` Jinyu Tang
2026-02-27 14:00 ` Radim Krčmář [this message]
2026-02-27 14:00   ` Radim Krčmář
2026-02-27 14:00   ` Radim Krčmář
2026-03-02 22:18 ` Andrew Jones
2026-03-02 22:18   ` Andrew Jones
2026-03-02 22:18   ` Andrew Jones
2026-03-03  3:13 ` Nutty.Liu
2026-03-03  3:13   ` Nutty.Liu
2026-03-03  3:13   ` Nutty.Liu
2026-04-02 13:28 ` Anup Patel
2026-04-02 13:28   ` Anup Patel
2026-04-02 13:28   ` Anup Patel

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=DGPSROCWMJWD.33FHG4JOPD2V8@oss.qualcomm.com \
    --to=radim.krcmar@oss.qualcomm.com \
    --cc=alex@ghiti.fr \
    --cc=andrew.jones@oss.qualcomm.com \
    --cc=anup@brainfault.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=atish.patra@linux.dev \
    --cc=conor.dooley@microchip.com \
    --cc=kvm-riscv@lists.infradead.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=nutty.liu@hotmail.com \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    --cc=pjw@kernel.org \
    --cc=tjytimi@163.com \
    --cc=yongxuan.wang@sifive.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.