[PATCH v10 0/2] target/riscv: Fix riscv64 KVM migration

Xie Bo <[email protected]> Thu, 30 Jul 2026 13:19:24 +0800
Newsgroups org.nongnu.qemu-riscv,org.nongnu.qemu-devel
Message-ID <[email protected]>
This series revives v9, which was applied to riscv-to-apply.next but did
not reach QEMU master.

RISC-V KVM migration currently loses two pieces of vCPU state: the
privilege mode core register and KVM MP state. The latter leaves secondary
vCPUs in their reset STOPPED state on the destination.

Synchronize the privilege mode through the existing generic CPU VMState,
and carry MP state in a capability-gated KVM-only subsection. Keeping MP
state in a subsection avoids changing the generic RISC-V CPU VMState
version or adding KVM state to TCG migration streams.

Migration streams from older QEMU versions that lack the subsection can
still be loaded, retaining their previous destination reset behavior.
When KVM_CAP_MP_STATE is available, streams produced by this series require
a destination that recognizes the new subsection; an older destination
rejects the stream instead of silently losing MP state.

Changes from v9:
- Rebase on QEMU master at e1705a25aff3.
- Drop the reset-to-S-mode change that is already present upstream.
- Keep the generic RISC-V CPU VMState version unchanged.
- Save MP state in a KVM-only subsection and restore it only when the
  subsection was loaded.
- Reset the subsection-loaded flag in the parent VMState pre-load callback.

Testing on a native riscv64 host with Linux 6.6.20 and GCC 13.3.0:
- KVM build: riscv64-softmmu, --enable-kvm.
- Non-KVM build: riscv64-softmmu, --disable-kvm.
- Four-vCPU KVM migration on unmodified master: all vCPUs failed to run
  after migration and the guest reported RCU stalls.
- The same migration with this series: CPU0 through CPU3 all ran after
  migration.
- Four user-mode busy workers, one pinned to each vCPU, all made forward
  progress across migration to exercise privilege-mode restoration.

Xie Bo (2):
  target/riscv/kvm: Synchronize privilege mode
  target/riscv/kvm: Preserve MP state across migration

 target/riscv/cpu.h           |  4 +++
 target/riscv/kvm/kvm-cpu.c   | 62 ++++++++++++++++++++++++++----------
 target/riscv/kvm/kvm_riscv.h |  2 +-
 target/riscv/machine.c       | 45 ++++++++++++++++++++++++++
 4 files changed, 96 insertions(+), 17 deletions(-)

-- 
2.17.1