From nobody Fri Dec 19 20:16:25 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747647721865770.165430761592; Mon, 19 May 2025 02:42:01 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uGwza-0002vj-Pl; Mon, 19 May 2025 05:41:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uGwzT-0002vU-Uf for qemu-devel@nongnu.org; Mon, 19 May 2025 05:40:59 -0400 Received: from [218.76.62.146] (helo=ultrarisc.com) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uGwzN-0000hf-Vk for qemu-devel@nongnu.org; Mon, 19 May 2025 05:40:59 -0400 Received: from xb$ultrarisc.com ( [192.168.100.1] ) by ajax-webmail-localhost.localdomain (Coremail) ; Mon, 19 May 2025 17:41:36 +0800 (GMT+08:00) X-Originating-IP: [192.168.100.1] Date: Mon, 19 May 2025 17:41:36 +0800 (GMT+08:00) X-CM-HeaderCharset: UTF-8 From: =?UTF-8?B?6LCi5rOi?= To: qemu-devel@nongnu.org Cc: alistair.francis@wdc.com, palmer@dabbelt.com, pbonzini@redhat.com Subject: [PATCH] target/riscv/kvm/kvm-cpu: Fixed the issue of resume after QEMU+KVM migration X-Priority: 3 X-Mailer: Coremail Webmail Server Version 2024.1-cmXT6 build 20240625(a75f206e) Copyright (c) 2002-2025 www.mailtech.cn mispb-8cc425bc-7df6-4523-bb46-a48cc1a964e6-ultrarisc.com Content-Transfer-Encoding: quoted-printable X-CM-CTRLDATA: 5f4svGZvb3Rlcl90eHQ9MjM0Nzo4NDA= MIME-Version: 1.0 Message-ID: <58a08318.d0.196e7eb9068.Coremail.xb@ultrarisc.com> X-Coremail-Locale: zh_CN X-CM-TRANSID: AQAAfwDXUNDR_CpodCECAA--.199W X-CM-SenderInfo: l0e63zxwud2x1vfou0bp/1tbiAQAHB2gqi9gADAACse X-Coremail-Antispam: 1Ur529EdanIXcx71UUUUU7IcSsGvfJ3iIAIbVAYjsxI4VW3Jw CS07vEb4IE77IF4wCS07vE1I0E4x80FVAKz4kxMIAIbVAFxVCaYxvI4VCIwcAKzIAtYxBI daVFxhVjvjDU= X-Host-Lookup-Failed: Reverse DNS lookup failed for 218.76.62.146 (failed) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=218.76.62.146; envelope-from=xb@ultrarisc.com; helo=ultrarisc.com X-Spam_score_int: 8 X-Spam_score: 0.8 X-Spam_bar: / X-Spam_report: (0.8 / 5.0 requ) BAYES_20=-0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RDNS_NONE=0.793, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1747647723846116600 Content-Type: text/plain; charset="utf-8" This patch fixes two critical issues in QEMU with KVM: 1. Post-Migration Failure in User Mode: When QEMU with KVM is running in us= er mode, the guest may fail to function correctly after migration due to in= correct privilege state restoration. 2. Multi-Core Guest Inconsistency: After migration, only the first CPU (cor= e 0) remains functional, while all other cores become unresponsive. This pa= tch ensures all cores are properly set to runnable state after migration. Changes include: - Properly restoring guest privileged state during register synchronization. - Correctly updating multi-core state after migration to ensure all cores a= re active. Signed-off-by: Xie Bo --- target/riscv/kvm/kvm-cpu.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) --- a/target/riscv/kvm/kvm-cpu.c +++ b/target/riscv/kvm/kvm-cpu.c @@ -576,6 +576,14 @@ static int kvm_riscv_get_regs_core(CPUState *cs) } env->pc =3D reg; + /* Restore guest privilege level after migration */ + ret =3D kvm_get_one_reg(cs, RISCV_CORE_REG(env, mode), ®); + if (ret) { + return ret; + } + if (reg !=3D 3) { + env->priv =3D reg; + } for (i =3D 1; i < 32; i++) { uint64_t id =3D kvm_riscv_reg_id_ulong(env, KVM_REG_RISCV_CORE, i); ret =3D kvm_get_one_reg(cs, id, ®); @@ -601,6 +609,15 @@ static int kvm_riscv_put_regs_core(CPUState *cs) return ret; } + /* Save guest privilege level before migration */ + reg =3D env->priv; + if (reg !=3D 3) { + ret =3D kvm_set_one_reg(cs, RISCV_CORE_REG(env, mode), ®); + if (ret) { + return ret; + } + } + for (i =3D 1; i < 32; i++) { uint64_t id =3D kvm_riscv_reg_id_ulong(env, KVM_REG_RISCV_CORE, i); reg =3D env->gpr[i]; @@ -1289,6 +1306,12 @@ int kvm_arch_put_registers(CPUState *cs, int level, = Error **errp) return ret; } + /* Ensure all non-core 0 CPUs are runnable after migration */ + if ((level =3D=3D KVM_PUT_FULL_STATE) && (cs->cpu_index !=3D 0)) { + RISCVCPU *cpu =3D RISCV_CPU(cs); + ret =3D kvm_riscv_sync_mpstate_to_kvm(cpu, KVM_MP_STATE_RUNNABLE); + if (ret) { + return ret; + } + } if (KVM_PUT_RESET_STATE =3D=3D level) { RISCVCPU *cpu =3D RISCV_CPU(cs); if (cs->cpu_index =3D=3D 0) { --=20 2.34.1 ______________________www.ultrarisc.com =E9=87=8D=E8=A6=81=E6=8F=90=E7=A4=BA=EF=BC=9A=E6=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83=EF=BC=8C=E4=B8=8D=E5=8F=AF=E8=A2=AB=E5=8D=95=E7=8B=AC=E4=BD=9C=E4= =B8=BA=E4=BB=BB=E4=BD=95=E6=83=85=E5=BD=A2=E4=B8=8B=E7=9A=84=E8=AF=81=E6=8D= =AE=E6=88=96=E4=BE=9D=E6=8D=AE=E3=80=82=E6=84=9F=E8=B0=A2=E6=82=A8=E7=9A=84= =E7=90=86=E8=A7=A3=E4=B8=8E=E9=85=8D=E5=90=88=E3=80=82=E7=89=88=E6=9D=83=E6= =89=80=E6=9C=89=E3=80=82IMPORTANT NOTICE: This email, including its attachm= ent if any, is confidential. If you are not the intended recipient, please = delete it from your computer immediately. Any disclosure, copying, or distr= ibution of this message, or taking of any action based on it is strictly pr= ohibited. Any opinions and suggestions contained in this email are subject= to the terms and conditions expressed and defined by us and should not be = relied upon unconditionally under any circumstances unless they are confirm= ed in official written clarification or authorization from us. Thank you f= or your understanding and cooperation.All rights reserved.