From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dl1-f48.google.com (mail-dl1-f48.google.com [74.125.82.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72DAC3E1CF8 for ; Fri, 26 Jun 2026 08:24:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782462243; cv=none; b=O1WBXi6H2vcdXDS7IxLJrjbwkCTeI8oKLVYXIe0yN8II29sHNQit89SccgGfX1z/Vrdvk2etyMuUJHILtQPYotdr4lyB74KCKvuULjfp1ucf02Xf9VU8FiAB+9FAvWVQFnMVUNxlwtBf/ofM7ONqP3JneSADxAgcDxQOB74E/Go= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782462243; c=relaxed/simple; bh=VCtdpA7YF5advtu0bupY3qxS+FfCYS20j33S3YKJEL0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dAOzc1ghjEfEpJ21qBGDp9EC3KjUOW5LUnJsT20iF9KDvxi4zLzeMZcWLnRJYfWPDOvxLQgaV1ivxsDzVGermPzZiNrfslMusIzJjKfAzXnpraULl2Fj1G1nmjPsXtvnuKElY6Ech/XoBKCWNfQTLJp1KwZGK+ww/K8mIc3Gzpc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com; spf=pass smtp.mailfrom=sifive.com; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b=ZFfB8sbG; arc=none smtp.client-ip=74.125.82.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="ZFfB8sbG" Received: by mail-dl1-f48.google.com with SMTP id a92af1059eb24-1384ebe7a10so529154c88.1 for ; Fri, 26 Jun 2026 01:24:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1782462241; x=1783067041; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=k8TjMER5gISFlQ4SyOMczlZr2aeQOkgr25LHVZl1LkM=; b=ZFfB8sbGw6PYU726rPQYVhJksyYvYa2B+reA9AocpI8M1LXBXakEdSkos+9vatXn6l klJIXvhdlfcMGA0GGxzC7Nv/nwqGiTAVt3V8b9ZPyVuozKbiHn0r1vUjbiM3JDEssDWR hFK4f3UIhLFPP4IzYq6UrdWBtWlYl7x7LKVN/bX8c2v+XL3ELvG/36EpoN/h00xWchwc iasuOxQNemLhNcJZnNkjHiIWB9pvFxBnM67XsoUrGhS1+y1ZvYW3gZAYkbTvjV2ypqgE qJ7nAPCUTHo29ej6erdoD6jBcwP/G/AA90VrYLGYlzZ62m0t5OytDhYhfbLxAknKg0uP xRpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782462241; x=1783067041; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=k8TjMER5gISFlQ4SyOMczlZr2aeQOkgr25LHVZl1LkM=; b=b8p8YZCpiFJchfJPRBAx5N787eVRm6Nn4P48XWZcu8Wo8uq+hUFG+Kj73X63N7O2oA Z2iNE3nEbry9D23tFA9R/JcZKAaN1WN5CUjtA82btQzUmbfkhUW6Twl2TfSZODV2wCwU Lgbvjg9xiNwT1vQFAy1XVpOKvaowEtKrKd9JqgZr7dBGriD/es0TsIAMR3h+su37P8PH Ne7muo+bkX5NJEmgGPPiF9lWsVk5pFuRh+Ez3AYbXBVGIFJUhmb4GphgAc1lR3AjV8lo n0UO+04wgAe90ctpNbooxJcyS1AUVjrNyZQ/A4pSQ3pQkUay+jumMt90zQ3jtl6oh+Ad yJjA== X-Forwarded-Encrypted: i=1; AFNElJ8l5t/0LG+RBrw+6F8RiQybAk78lq6ZvF+XejBhRf0gCFBOUBlbQROMM5cIWAJU5CxbEOf+dFWK/CPVoSU=@vger.kernel.org X-Gm-Message-State: AOJu0YwE3hJX2WgCvJkYvpqe1LBfgKu4T9mI5ORr8J1lsBLx03vGjg7N ErKmFfpWTnuWiq3+3wY3iZ2FtVi+/bDx4HPCChkYECIgvAe7c85KO5/jOcC+1ct1DU8= X-Gm-Gg: AfdE7ckZms85pmv3nSBomias1472lmyEqTUPVR7hUg4jcoC4xKAwvoDM2f6qmEpkxBj wKEyuZO/eun8dIpqsf4w9inmSVv7zEzuO0IY0ykkDbqHIhGPdJMBVUMOTpBXSgBLV/m7S1tF9rT HeR9b7GKZFiQAvSh9EOazCbN0xvgjEl+9z/un1hHKhlDvto7AUH2xd3ZK9II5czmbAM1YSu8vw+ MT2YFg3hXI+P8zYVi4PHFEuSDldDsG8yeJHdVJNi2LbS8Mzu1XsJ4Oq/YVUmHQfWul1gOeqPHqn 6hJvUdHv3shKkTcNqPjYvvhUwd90TcMjEy420Kta/c4WBj79+dTlzcxbxT5y7481aqCj0KhWo09 QgJAWVYUzNnr1f8z537WJaIBUDQJEUHKTMZSxEuRdILCV4NDa2NUpu86cR5TjvbyMI7wUORNQjV IjfIoZTlVvrfcy9+P0mmK7JELJzh4TLx6r X-Received: by 2002:a05:7022:ebc5:b0:137:feeb:3fc9 with SMTP id a92af1059eb24-139dbb5b2d9mr6090271c88.18.1782462240516; Fri, 26 Jun 2026 01:24:00 -0700 (PDT) Received: from sw07.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-139d8f318e7sm15557199c88.3.2026.06.26.01.23.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jun 2026 01:23:59 -0700 (PDT) From: Yong-Xuan Wang Date: Fri, 26 Jun 2026 01:23:21 -0700 Subject: [PATCH v2 1/2] KVM: RISC-V: Clear former VCPU cache on virtualization disable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260626-kvm-cpu-pm-v2-1-478e5ef8dc9b@sifive.com> References: <20260626-kvm-cpu-pm-v2-0-478e5ef8dc9b@sifive.com> In-Reply-To: <20260626-kvm-cpu-pm-v2-0-478e5ef8dc9b@sifive.com> To: Anup Patel , Atish Patra , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , =?utf-8?q?Radim_Kr=C4=8Dm=C3=A1=C5=99?= , Andrew Jones , Nutty Liu , Jinyu Tang Cc: greentime.hu@sifive.com, vincent.chen@sifive.com, zong.li@sifive.com, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Yong-Xuan Wang X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1782462237; l=2593; i=yongxuan.wang@sifive.com; s=20260424; h=from:subject:message-id; bh=VCtdpA7YF5advtu0bupY3qxS+FfCYS20j33S3YKJEL0=; b=PLsuyWmTEcgztSc4jAigjlQNrBKATP06+H1C5DMVQ6nyjljp9p0vtpvzJiWdtN2jkenD9DYIc v2Jc5qMTS2+D+On88Jk0ju4NdP5P1vHLUHdxSAETIrJn4I7SmX333Ra X-Developer-Key: i=yongxuan.wang@sifive.com; a=ed25519; pk=+8NCHB1ZJvZthQAmZspOAaqjo+/snaW8mFSiDx45HxY= When a CPU is taken offline or enters deep idle states, hypervisor CSR state is lost. The kvm_former_vcpu fast-path optimization caches the last VCPU that ran on each CPU to avoid expensive CSR restoration when the same VCPU is re-scheduled on the same CPU. However, if this cache is not cleared when CSR state is lost, the next VCPU entry will incorrectly skip CSR restoration, leading to corrupt hypervisor state. Add kvm_riscv_clear_former_vcpu() to invalidate the per-CPU cache and call it from kvm_arch_disable_virtualization_cpu() to ensure proper CSR restoration after CPU offline or system suspend events. Fixes: 1323a5cfe52c ("KVM: riscv: Skip CSR restore if VCPU is reloaded on the same core") Signed-off-by: Yong-Xuan Wang --- arch/riscv/include/asm/kvm_host.h | 2 ++ arch/riscv/kvm/main.c | 2 ++ arch/riscv/kvm/vcpu.c | 11 +++++++++++ 3 files changed, 15 insertions(+) diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h index 60017ceec9d2..808b4ee1bd10 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -319,6 +319,8 @@ bool kvm_riscv_vcpu_stopped(struct kvm_vcpu *vcpu); void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu); +void kvm_riscv_clear_former_vcpu(void); + /* Flags representing implementation specific details */ DECLARE_STATIC_KEY_FALSE(kvm_riscv_vsstage_tlb_no_gpa); diff --git a/arch/riscv/kvm/main.c b/arch/riscv/kvm/main.c index 0924c75100a2..350e4f097d6e 100644 --- a/arch/riscv/kvm/main.c +++ b/arch/riscv/kvm/main.c @@ -69,6 +69,8 @@ void kvm_arch_disable_virtualization_cpu(void) csr_write(CSR_HEDELEG, 0); csr_write(CSR_HIDELEG, 0); + kvm_riscv_clear_former_vcpu(); + kvm_riscv_nacl_disable(); } diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index cf6e231e76e2..e48338f907f4 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -26,6 +26,17 @@ static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_former_vcpu); +void kvm_riscv_clear_former_vcpu(void) +{ + /* + * Clear the per-CPU former VCPU pointer because hypervisor CSR state + * will be lost. This ensures that the next VCPU entry will properly + * restore all CSRs instead of incorrectly skipping CSR restoration + * via the fast-path optimization. + */ + __this_cpu_write(kvm_former_vcpu, NULL); +} + const struct kvm_stats_desc kvm_vcpu_stats_desc[] = { KVM_GENERIC_VCPU_STATS(), STATS_DESC_COUNTER(VCPU, ecall_exit_stat), -- 2.43.7