Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C7859274B39 for ; Thu, 26 Feb 2026 15:04:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.168.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772118288; cv=none; b=Az0V3rbnP1bY+9x1VVUFEFUyz4AX0os2fxzee214J2IuL8Wl79BDnarnlqPrKbUYQtlZ6gySsfE9zYI3NliiFOR2BBL9W3TDgKJPu2rCqXsnWBrtyEkgywKwAULGiEnPcEfI35+rJIpkJXLvBr5el2EskSa67sNbj3+JSy7R0K4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772118288; c=relaxed/simple; bh=pYLIuNl20p7tOmh7vf2ZBTy44J0MNJ0SA/EEn65HWbQ=; h=Mime-Version:Content-Type:Date:Message-Id:To:From:Subject:Cc: References:In-Reply-To; b=tz5BeS+CEcKx7M8meDfg0O+LmEAkgtq+BIe+1SAd3z+2Avm9zzsS91qjCo/th+oxk5RQIgWfkHL4KFmmjxJOODdftmD7dg7fzTM2meby0HImiFqhD5sbDYRfjqCi3GM14oO3xl8lZE+8bCrlL83BpkM/rkXzZuwyI9GnbqzqGb0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=GzGEF53S; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=S4VUHJ1S; arc=none smtp.client-ip=205.220.168.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="GzGEF53S"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="S4VUHJ1S" Received: from pps.filterd (m0279867.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 61QALDG4350088 for ; Thu, 26 Feb 2026 15:04:47 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= qp3TU7bzUr1B0vZ7uPapTlnahGO7pQVMf5jftO4wjJI=; b=GzGEF53SIu6p5rL2 FSzlRnRl/1ny7hdPTi7cfN1pijd2a8AUO4dm00PcTez3Uq4OMf1cO2K4EigAUEJm 9oZ5mw1R+zCmREzqwxByTbrKpVLrDX8UtHagBJ4JjNSHczqKXEK+L9aRfz5s8c/e PXNZKU6e8gR/51X5T0nwNyiO2/GLfVpML2nvU/DEE3kv3Z6TNeSX/OaoXVw8ZQu5 3W9Cz5+M/wN6wq62tYf91lyVqwlmfyV7C8qyZ3ePUzRjlx5CkB4XsYEYFD7VneQy JvelPj8eN/8bsDXt/swz/lt7ZUFonpXnhSvP+Pp3mr6p1N2is5u3j8ECBgfC/XOY QmSXEA== Received: from mail-oo1-f69.google.com (mail-oo1-f69.google.com [209.85.161.69]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4cjc0gad61-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Thu, 26 Feb 2026 15:04:46 +0000 (GMT) Received: by mail-oo1-f69.google.com with SMTP id 006d021491bc7-679dda090fbso19510903eaf.2 for ; Thu, 26 Feb 2026 07:04:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1772118286; x=1772723086; darn=vger.kernel.org; h=in-reply-to:references:cc:subject:from:to:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=qp3TU7bzUr1B0vZ7uPapTlnahGO7pQVMf5jftO4wjJI=; b=S4VUHJ1SO/q/zDIvoqzhltshYm5mClD6s5zJBInBgA4/GLHppGNhVw+5DRf5aC+WnQ xxKNZa7gOPPcI8O3xWCZNa6mnZi+Z0YYQm5B/qWUeYbpDcA/ONfy6v8AF2qg6hU6nhhM xyrKIOFCfcfPak2kwmnEQoymvnrVTyNkSUsaSRuy7mhkJYCE4ekvJ7ua02hBRMPZqcpg yDSZQ+yH6f2OlCScPfnDGZ7caYz2gK5W5RkvvoANg1worKVT/EoBh+Pi4kjf0MiQ8MQ/ 5ksEZAn6tWxanWrp+zolAOIl+5XHhd53/HXaWvvH4asN/BJPmJiFAnhFLo7hPTTPaoiz ks4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772118286; x=1772723086; h=in-reply-to:references:cc:subject:from:to:message-id:date :content-transfer-encoding:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=qp3TU7bzUr1B0vZ7uPapTlnahGO7pQVMf5jftO4wjJI=; b=bRvx9Z+s+tPBGnc5X0T1c9Wi58JGG0ox6RzQ5/7cgmHMyIT6mUZZOvVN8pJaS5h+3S RNOi6FyOSHdiFauAZXTf1rXs+LZ1UQWtbWsOy/66P9Io7Roxu3iu3tH0YgMQi54k+0ly 0XpX5Gr8cZm4t6vndOKFh14wuaGOteijjWORJqrYAJSc6Hd5nYKPRGxqw0S6QegrXq8w WeQWyv91uFX3eNw724akDCcXhseHPR7Vq8cJJYIAiI47NBtRvEGemHeTGsorN/O7ZjyZ Fg4ua4bq1ShxAi2KqKrZSQq69JZKl2xp/oXJtLiQJVW0jKreoZXVcmmJggfCqPibwGNP MNvQ== X-Forwarded-Encrypted: i=1; AJvYcCXi4iSM6uRDy2a25iRci+SCKBA+6QM7S3yUQWtrgyEn419dIrLvdIQPhY8+MmnlsUbtp9vJ2FdH+ZIb32Q=@vger.kernel.org X-Gm-Message-State: AOJu0Yxi+NvB51kWTgMqP6fXRIvF1nt+cvn5TdDF8C/G+pJ2ryd6OyFv eKGmmTGjzm1VF8CPEcWVL9oyslY9MNn+FyHSJ2DyMlnDyze3ZTrC01L45hPUHaZ2IivCVw3sUFA J2LsTNTlQ2NAMbkW+37cNNDcqEQAAdX0qpDmDHjqutqkGNKA1Pq5GKlFzJjCkSIdTAg8= X-Gm-Gg: ATEYQzydV1Xca8eTuRVIhLlGsp4KYprF4xRXNLdfe9LET34XszTlBUe0dJiKDTzw3aN 5/UuGB1e8DTIyrxJl+/gL6ZPP/nEZ+0pl4sL4SBySOce2TPS7TRMdT52bjmWd2JPPywTwUmMmVD AR0hCY7hVRuY0kZn3/QfI5BL5cWz0/nBD8yOhaiHzm7GxvPnrjPEVljZqpSGKiPhV46NCdSITJZ sjR0FRXTymrRBbIIMItvefjuP1T8FjOXLXcnxrzvYMZGDuR3aQ+Ra8UQR+zDtmRaKggWxHmm81D CZ+v+oo0XG7sw7CsjbeLa0CosPViS1+/CMdJ3gVWEE53BqSdZeo5vV4d6BrK8rd/63lWY9eVA8V OC00SGorZQyr+fRD22Mn4hgw8brtgppBVrA+nqZP+5NA1IxBPpM0BAXjZwsqO X-Received: by 2002:a4a:ee84:0:b0:663:3c7:421d with SMTP id 006d021491bc7-679ef9b9587mr2113790eaf.75.1772118286067; Thu, 26 Feb 2026 07:04:46 -0800 (PST) X-Received: by 2002:a4a:ee84:0:b0:663:3c7:421d with SMTP id 006d021491bc7-679ef9b9587mr2113774eaf.75.1772118285481; Thu, 26 Feb 2026 07:04:45 -0800 (PST) Received: from localhost (ip-86-49-242-13.bb.vodafone.cz. [86.49.242.13]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b935ae6131fsm65716166b.40.2026.02.26.07.04.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Feb 2026 07:04:44 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Thu, 26 Feb 2026 15:04:43 +0000 Message-Id: To: "Jinyu Tang" , "Anup Patel" , "Atish Patra" , "Andrew Jones" , "Conor Dooley" , "Yong-Xuan Wang" , "Nutty Liu" , "Paul Walmsley" , "Paul Walmsley" , "Palmer Dabbelt" , "Albert Ou" , "Alexandre Ghiti" From: =?utf-8?q?Radim_Kr=C4=8Dm=C3=A1=C5=99?= Subject: Re: [PATCH v6] KVM: riscv: Skip CSR restore if VCPU is reloaded on the same core Cc: , , , References: <20260226123802.154855-1-tjytimi@163.com> In-Reply-To: <20260226123802.154855-1-tjytimi@163.com> X-Authority-Analysis: v=2.4 cv=XI49iAhE c=1 sm=1 tr=0 ts=69a0610e cx=c_pps a=lVi5GcDxkcJcfCmEjVJoaw==:117 a=9tUHzIdeCh+UoOnba06Qjw==:17 a=IkcTkHD0fZMA:10 a=HzLeVaNsDn8A:10 a=M51BFTxLslgA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=eoimf2acIAo5FJnRuUoq:22 a=Byx-y9mGAAAA:8 a=E61YY4l4AmgQ4UnPY0gA:9 a=QEXdDO2ut3YA:10 a=rBiNkAWo9uy_4UTK5NWh:22 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMjI2MDEzNiBTYWx0ZWRfX+FGXpJxRoLIu /eXGHDewurHH+OkTe0DwS264x2A+qwycVsXLnx9tYJnPSZxgTf2r3AwrHLxJ6O1pdKhNyiej0vT r24HBA761OHBA4EHoBh+rI8sTgh+/7TwZm0LtjxDwOYrp7JzMZ4BjfB+mJl7yEDUiqOCVrDurzv EIJjJM5RGJ6hZlLIdp0hCKpbxUQ6udvGsaIYXCd65zBt23mLzPBxhlLgAueroM6UKwre2G7Btg1 FhWaw1SDylR6MdfhdZqBk0LtQffXJswQfplbnM+aNjzJKqJNBG2hD/NrOMQNMltdTS7V851FtzS NqDvVDeUsMEkJLYObP5fJqxqXwIEbnP17xpwMmTZWZM8LJkZcgNsh6rKxJLZnMmzbQkb0UmpC+O yS95RSbGF5SNYRqJH9pEgZyWpZnl8hTOPnKDySUHgFS8ybdp534UB0DLL5fqHSKdGrldEM+Ny9x fokRKlDAOaKiLTTtXpg== X-Proofpoint-ORIG-GUID: eXdf1N7Y6HfcUgAS-v63Z8f9LTnphNTe X-Proofpoint-GUID: eXdf1N7Y6HfcUgAS-v63Z8f9LTnphNTe X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-02-26_01,2026-02-26_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 suspectscore=0 bulkscore=0 impostorscore=0 lowpriorityscore=0 clxscore=1015 spamscore=0 malwarescore=0 priorityscore=1501 adultscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2602130000 definitions=main-2602260136 2026-02-26T20:38:02+08:00, Jinyu Tang : > Currently, kvm_arch_vcpu_load() unconditionally restores guest CSRs and > HGATP. 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 are still valid. > > This patch optimizes the vcpu_load path by skipping the expensive CSR > writes if all the following conditions are met: > 1. It is being reloaded on the same CPU (vcpu->arch.last_exit_cpu =3D=3D = cpu). > 2. The CSRs are not dirty (!vcpu->arch.csr_dirty). > 3. No other VCPU used this CPU (vcpu =3D=3D __this_cpu_read(kvm_former_vc= pu)). > > 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. > - A new 'csr_dirty' flag is introduced to track runtime userspace > interventions. If userspace modifies guest configurations (e.g., > hedeleg via KVM_SET_GUEST_DEBUG, or CSRs via KVM_SET_ONE_REG) while > the VCPU is preempted, the flag is set to skip fast path. > > Note that kvm_riscv_vcpu_aia_load() is kept outside the skip logic > to ensure IMSIC/AIA interrupt states are always properly > synchronized. > > Signed-off-by: Jinyu Tang > --- > v3 -> v4: > - Introduced 'csr_dirty' flag to track dynamic userspace CSR/CONFIG > modifications (KVM_SET_ONE_REG, KVM_SET_GUEST_DEBUG), forcing a full > restore when debugging or modifying states at userspace. > - Kept kvm_riscv_vcpu_aia_load() out of the skip block to resolve IMSIC > VS-file instability. Excluding AIA is disturbing as we're writing only vsiselect, hviprio1, and hviprio2... It seems to me that it should be fine to optimize the AIA CSRs too. Wasn't the issue that you originally didn't track csr_dirty, and the bug just manifested through IMSICs? > diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c > @@ -581,6 +585,20 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int c= pu) > struct kvm_vcpu_csr *csr =3D &vcpu->arch.guest_csr; > struct kvm_vcpu_config *cfg =3D &vcpu->arch.cfg; > =20 > + /* > + * If VCPU is being reloaded on the same physical CPU and no > + * other KVM VCPU has run on this CPU since it was last put, > + * we can skip the expensive CSR and HGATP writes. > + * > + * Note: If a new CSR is added to this fast-path skip block, > + * make sure that 'csr_dirty' is set to true in any > + * ioctl (e.g., KVM_SET_ONE_REG) that modifies it. > + */ > + if (vcpu->arch.last_exit_cpu =3D=3D cpu && !vcpu->arch.csr_dirty && > + vcpu =3D=3D __this_cpu_read(kvm_former_vcpu)) > + goto csr_restore_done; I see a small optimization if we set the per-cpu variable here, instead of doing that in kvm_arch_vcpu_put: if (vcpu !=3D __this_cpu_read(kvm_former_vcpu)) __this_cpu_write(kvm_former_vcpu, vcpu); else if (vcpu->arch.last_exit_cpu =3D=3D cpu && !vcpu->arch.csr_dirty) goto csr_restore_done; This means we never have to read the per-cpu twice in the get/put sequence: faster put at the cost of slightly slower get. Thanks.