From nobody Mon Feb 9 17:35:05 2026 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (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 62EF330E820 for ; Sat, 3 Jan 2026 15:24:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767453875; cv=none; b=cZqoPidiWAWF2l1P5Jlp/uqWZ6G+hWft96Ms6jZpXlzuz0rHHNStshg0bhqUgR0pQDicNGZtt0lhWY31qrBG8RkQXI24sDa7dR1YUweL1qKbLbhGIwOG4kXRGnMuUw+ZbohXsxpN/2hMSPeNNqFDVKQyCZ9E4umBrOGWdf9Bvr0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767453875; c=relaxed/simple; bh=7eJ6OYjTg8naQ1BSuXx/9safQEXenDmq3eBCbZ6CO0A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=H642dHLyly5JUtQW07ZE+rEj5RyELmC538Di671dEp2uT7W0qxFSDB1VB0NGK/qnwFDRNE034ZjgFnCdfEFDdaOpiSZ7guw4eYqMdxJ5lpltrHFxw5hnvJZBHDYCCbbajtrRwzaZg/5vbtk3pspaMNkyMQtqL7KLBHA7nCr40F4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=K3P6EBuJ; arc=none smtp.client-ip=209.85.216.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="K3P6EBuJ" Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-34c565b888dso14242048a91.0 for ; Sat, 03 Jan 2026 07:24:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767453873; x=1768058673; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZbTfmd3NSnTt9lXFsPx3VDeaOg/UBFYzN+h8hVF3FMQ=; b=K3P6EBuJdKh4NsNhxOdih8wAEyeO61Xf5RI4WjmqxUZ89FtT1oxadcKDOYw8Bm8FFB JgIHWnTY8ZuMmqNhwwC1waErrDDdYzeSnX/xCA8pv2jlwuzknrRSbfxwG26L7D3aKO76 O8geyOHL0c7I5MrQLX1DyVFEZ5aD0Hgk7r+5Pk9vRdHAcRELrYqL4oD8LJ7S9yYgVfy9 2SM4ep7nNZpt6d2ciQlnhDYKE2rwwZu+kF78q2iqLjSfNwiyh/3ZhWNQqiLjPursOLI+ iDqjn2+KZk+3xbh7Rc8i/lU7OgKZhIBgJJO/9djkgdcOI+UpijEkqVeU7AbJr2Ph/nP0 LUww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767453873; x=1768058673; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=ZbTfmd3NSnTt9lXFsPx3VDeaOg/UBFYzN+h8hVF3FMQ=; b=eEq1miYRVWN9rYFHO7wmWtA4igKNWIS0bfy9iOHyWLMMAIUdTXjzetBXeOiIOVE/ve yA3+JY/cWwaPkg6bvKnJm7Hpm5Go+gd9b6lga9LxEY9vGNdGd8QCo1MN1UDXnuHF7ITp wWSE+QpGW9Pv4TgDtX5rzZUL85aFAYnSQHKRtGjaRjsYQC3LHU7abWrDiSP1JE8ZtJw/ xI27Ad9YYvahkIaeHkxaEn0DIj+ja/v3G0z6LdZsOi8IsnhFhOJ1kzkQg+RVVHqoodqK fsIwAFPw92pUF9ELnx8hDkFCChFZxJMWClWOCBqQIs7a5s+C5NwlsLwoxWQdGSv4DLJ0 wz6A== X-Forwarded-Encrypted: i=1; AJvYcCVNSah7YGUmfpQvetZs+VtTzSaezrzVTY86+tnqqn9gCfb+ZNYpi/J98cWg9Mbq++UapmU/REhq71MfUbg=@vger.kernel.org X-Gm-Message-State: AOJu0YxgFTOK7RX22eh6eynrj6M52p7l1fbHoV46txMGJ6Wfk/Rl4n7n hMJd9TbL7fhQPbiu8ecj6738wMFp32dRKJWI2B5yDUDOmda1XiBhZRDZ X-Gm-Gg: AY/fxX5buANnyrhrurKD+/k944X4ea5no3PDGzQBVnqTLPEY6G5rew27ZyCOsixjttc fQXsK81LCG9InCdprwMsfy/p2VklSy/GxuAiLVp4UDyx2jAmVKR5j1O86yimSGCh8LQqjfiYr10 4728qm2lFZDk/e1hcCRmIEs6bv/53nAtM9jQa6b8WIyaD9xJVKl2Qo0B1BOLjJVFkYDrDJuFKRK jdmicJt/MEX47nbOUOuipEcFn/4OrOj87wKUcm6VsGzU0mmLLd7DW6YfwhHvcLSAQVCvNQG2zrN Zxu7KCpb1W/RZEymfIrmkNk4JwoJCIPApAFSJ+GyI2oK/mEZiTVWgC2JIl4bdsRQNVUKFSB55lk W0/pQLEMdNAaKrpB72qe7zi7qPiYAAXDRLfcA6iHrX6RvAuFOvSvo3cjSfsGHOdCu0xlGnKPRrI eHsVM6VBSZBCqcKOTlNJ2NmLKkBGc9ZZjVNjXCSUxcsOSOSZ8CvwZCRwWEVGh98M39 X-Google-Smtp-Source: AGHT+IFAqFIt8xnOvelgL+iXx2mpTPUOQaCU3Q7/FdJxFlUC1bycD0YxqtWt/Ab1RF9sp9aze5LfoQ== X-Received: by 2002:a17:90b:4c45:b0:34c:fbf0:fa55 with SMTP id 98e67ed59e1d1-34e921beaf0mr39991715a91.21.1767453872679; Sat, 03 Jan 2026 07:24:32 -0800 (PST) Received: from localhost.localdomain (123-48-16-240.area55c.commufa.jp. [123.48.16.240]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-34f4770a256sm2107802a91.12.2026.01.03.07.24.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 03 Jan 2026 07:24:32 -0800 (PST) From: Naohiko Shimizu To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu Cc: alex@ghiti.fr, anup@brainfault.org, atish.patra@linux.dev, daniel.lezcano@linaro.org, tglx@linutronix.de, nick.hu@sifive.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, Naohiko Shimizu Subject: [PATCH v2 2/3] riscv: kvm: Fix vstimecmp update hazard on RV32 Date: Sun, 4 Jan 2026 00:23:59 +0900 Message-Id: <20260103152400.552-3-naohiko.shimizu@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260103152400.552-1-naohiko.shimizu@gmail.com> References: <20260103152400.552-1-naohiko.shimizu@gmail.com> 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" Signed-off-by: Naohiko Shimizu riscv: fix timer register update hazard on RV32 On RV32, updating the 64-bit stimecmp (or vstimecmp) CSR requires two separate 32-bit writes. A race condition exists if the timer triggers during these two writes. The RISC-V Privileged Specification (e.g., Section 3.2.1 for mtimecmp) recommends a specific 3-step sequence to avoid spurious interrupts when updating 64-bit comparison registers on 32-bit systems: 1. Set the low-order bits (stimecmp) to all ones (ULONG_MAX). 2. Set the high-order bits (stimecmph) to the desired value. 3. Set the low-order bits (stimecmp) to the desired value. Current implementation writes the LSB first without ensuring a future value, which may lead to a transient state where the 64-bit comparison is incorrectly evaluated as "expired" by the hardware. This results in spurious timer interrupts. This patch adopts the spec-recommended 3-step sequence to ensure the intermediate 64-bit state is never smaller than the current time. --- arch/riscv/kvm/vcpu_timer.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/riscv/kvm/vcpu_timer.c b/arch/riscv/kvm/vcpu_timer.c index 85a7262115e1..f36247e4c783 100644 --- a/arch/riscv/kvm/vcpu_timer.c +++ b/arch/riscv/kvm/vcpu_timer.c @@ -72,8 +72,9 @@ static int kvm_riscv_vcpu_timer_cancel(struct kvm_vcpu_ti= mer *t) static int kvm_riscv_vcpu_update_vstimecmp(struct kvm_vcpu *vcpu, u64 ncyc= les) { #if defined(CONFIG_32BIT) - ncsr_write(CSR_VSTIMECMP, ncycles & 0xFFFFFFFF); + ncsr_write(CSR_VSTIMECMP, ULONG_MAX); ncsr_write(CSR_VSTIMECMPH, ncycles >> 32); + ncsr_write(CSR_VSTIMECMP, (u32)ncycles); #else ncsr_write(CSR_VSTIMECMP, ncycles); #endif @@ -307,8 +308,9 @@ void kvm_riscv_vcpu_timer_restore(struct kvm_vcpu *vcpu) return; =20 #if defined(CONFIG_32BIT) - ncsr_write(CSR_VSTIMECMP, (u32)t->next_cycles); + ncsr_write(CSR_VSTIMECMP, ULONG_MAX); ncsr_write(CSR_VSTIMECMPH, (u32)(t->next_cycles >> 32)); + ncsr_write(CSR_VSTIMECMP, (u32)(t->next_cycles)); #else ncsr_write(CSR_VSTIMECMP, t->next_cycles); #endif --=20 2.39.5