Skip to content

【backport】KVM: riscv: Skip CSR restore if VCPU is reloaded on the same core - #359

Open
uestc-gr wants to merge 1 commit into
RVCK-Project:rvck-6.6from
uestc-gr:vcpu-reloard
Open

【backport】KVM: riscv: Skip CSR restore if VCPU is reloaded on the same core#359
uestc-gr wants to merge 1 commit into
RVCK-Project:rvck-6.6from
uestc-gr:vcpu-reloard

Conversation

@uestc-gr

@uestc-gr uestc-gr commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

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)

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>
@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown

开始测试 log: https://github.com/RVCK-Project/rvck/actions/runs/31158996872

参数解析结果
args value
repository RVCK-Project/rvck
head ref pull/359/head
base ref rvck-6.6
LAVA repo RVCK-Project/lavaci
LAVA hardware ['qemu']
LAVA Testcase path lava-testcases/common-test/ltp/ltp.yaml
need run job kunit-test,kernel-build,check-patch,lava-trigger

测试完成

详细结果:
check result
kunit-test success
kernel-build failure
check-patch success
lava-trigger-qemu skipped
lava-trigger-sg2042 skipped
lava-trigger-k1 skipped
lava-trigger-lpi4a skipped

Kunit Test Result

[07:51:32] Testing complete. Ran 482 tests: passed: 465, skipped: 17

Kernel Build Result

Check Patch Result

Total Errors 0
Total Warnings 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KVM RISC-V:同核 VCPU load时跳过 CSR 恢复

2 participants