【backport】KVM: riscv: Skip CSR restore if VCPU is reloaded on the same core - #359
Open
uestc-gr wants to merge 1 commit into
Open
【backport】KVM: riscv: Skip CSR restore if VCPU is reloaded on the same core#359uestc-gr wants to merge 1 commit into
uestc-gr wants to merge 1 commit into
Conversation
mainline inclusion from Linux 7.0-rc7 commit 1323a5c category: feature bugzilla: RVCK-Project#358 -------------------------------- 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> Reviewed-by: Nutty Liu <nutty.liu@hotmail.com> Reviewed-by: Andrew Jones <andrew.jones@oss.qualcomm.com> Reviewed-by: Radim Krčmář <radim.krcmar@oss.qualcomm.com> Link: https://lore.kernel.org/r/20260227121008.442241-1-tjytimi@163.com Signed-off-by: Anup Patel <anup@brainfault.org> Signed-off-by: Gao Rui <gao.rui@zte.com.cn>
|
开始测试 log: https://github.com/RVCK-Project/rvck/actions/runs/31158996872 参数解析结果
测试完成 详细结果:
Kunit Test Result[07:51:32] Testing complete. Ran 482 tests: passed: 465, skipped: 17
Kernel Build Result
Check Patch Result
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixed: #358
当前 kvm_arch_vcpu_load() 会无条件恢复 guest CSRs、HGATP 和 AIA 状态。
然而,当一个 VCPU 在同一个物理 CPU 上重新加载,并且在它上次退出后没有其他 KVM VCPU 在该 CPU 上运行时,硬件 CSRs 和 AIA 寄存器仍然是有效的。
此补丁优化了 vcpu_load 路径,在满足以下条件时跳过昂贵的 CSR 和 AIA 写操作:
1、VCPU 在同一个 CPU 上重新加载(vcpu->arch.last_exit_cpu == cpu)。
2、CSRs 没有被标记为 dirty(!vcpu->arch.csr_dirty)。
3、没有其他 VCPU 使用过该 CPU(vcpu == __this_cpu_read(kvm_former_vcpu))。
测试方法
0001-selftest.patch
将其临时打入内核,编译出selftest的vcpu_load_perf_test,我们可以看到在同一个cpu的reload的平均耗时从184761.01 ns降低到157247.15 ns
补丁前
/home # ./vcpu_load_perf_test.static
=== Scenario A: Same-CPU reload ===
Valid samples: 50000
Average latency: 184761.01 ns
=== Scenario B: Cross-CPU migration ===
Valid samples: 50000
Average latency: 672092.85 ns
========== Summary ==========
Same-CPU reload: 184761.01 ns (n=50000)
Cross-CPU migrate: 672092.85 ns (n=50000)
补丁后
/home # ./vcpu_load_perf_test.static
=== Scenario A: Same-CPU reload ===
Valid samples: 50000
Average latency: 157247.15 ns
=== Scenario B: Cross-CPU migration ===
Valid samples: 50000
Average latency: 674056.24 ns
========== Summary ==========
Same-CPU reload: 157247.15 ns (n=50000)
Cross-CPU migrate: 674056.24 ns (n=50000)