From nobody Mon Feb 9 23:00:55 2026 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 A44D9176ADE for ; Sun, 4 Jan 2026 14:00:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767535221; cv=none; b=L+GQMrNougHqHbLPnXZF0tNL2P6WpzQ6zs0KBcQF/Gq/oy0lWM+WMhGafWAeKy0rcL0uJ6pwL6lhNh6i+Ac0q24e4h67iZkNQo+zkeK/6mmcIrOwnDWkEfzykb5MzOqmmF7IeZ0DctZBaiGXuv1/Zut6clQ2+PQSdAt35hyCSBQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767535221; c=relaxed/simple; bh=mlSQR4TVYtAE0buXP9RYEyWgOci8JJdg/d+zuqGHUyw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LB2CtqnGl/PY2CfrC0JBrLlgBFXBoY8aXD4cFnOjQa38xTyabQQNz/heCJb8wQ91vgJ4IEQwmiwY8xdSh/IRbv6OdMRu8EnAOlTgmY6l1/cyLBfOhD8Yjw2rahmGRYyrew+m8AWfYhyTxbxvs2shg+lM8PRuQc19SAbkOLfFSn0= 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=QgwPQI0Y; arc=none smtp.client-ip=209.85.214.181 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="QgwPQI0Y" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2a07f8dd9cdso136439225ad.1 for ; Sun, 04 Jan 2026 06:00:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767535219; x=1768140019; 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=WKWm2q1i1+0+Z99C46AW9QGvFUwK/OoedXO5dcU1Nq0=; b=QgwPQI0YOrazCFkZYLEWTWWHOc7JerxfJEH4gLfBqmmhak86c+BMgD3y9CD1SwzSxG OFISPM+Stz1uct0/FmROHxLbKAwIW4sDb92A6MswUXkvfWGtzJMcWx03MYz7F0pSvbGP UTFuoT4AtknuzFyyElEtvSg4dfK3d7vO6bKXxB3dnD0QG9bbUAM7v/6hN+gUvFMyIpjf o4fJgDjhg1o9iJagft096lw6QVZ7rUI5YecmS5I8xpFZNZBEpT7oFhW+v3GcWD4uGX8k fDI/VLyWI70L08pO5wqpoDDdTRDYeu1z5rNJAfHHx9gX0FX63S00HigaJqBiqWHbf9/V EmZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767535219; x=1768140019; 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=WKWm2q1i1+0+Z99C46AW9QGvFUwK/OoedXO5dcU1Nq0=; b=auXzhJC3B2rb/O72j3rEtJSXnVIoxSkLImF8c7Fsw2jY2Q4hc63zWOpgUhVrlYcvKc w4eUg1WzN6LDZnurswfqkXhJ0CRTLb1cH0m4WTa/1o697AZHNkFI7GRMovT239CoOY4f ZQdZOSaGjKB3FBJv7Gb3HlyfO2D5AMngZotL7dzx0zHK44w8znmFYsW/I4vpJ3SlDzg4 t2WPHTeuRgpeo7AjK9opH6DT6R/jDu8PxdOtJrczhkb5Z4cGR9GTnqxXHXUbmJ3XOSoM xjMuvZz/Z+cCWI8T6cTccYdlFzUwWt9zV+8hoR61SZ739KGDCxjE9nl6S9gUrv5oeXrp 9/7A== X-Forwarded-Encrypted: i=1; AJvYcCXLY8SAcvdFfoVlZ7ly1ylemfSmshVzio0c/3Puqk3FHwC7oILDRrVg04gF1Hi2cIs4hUOTykh0X4Ust2E=@vger.kernel.org X-Gm-Message-State: AOJu0Yw0FxSeLn16inukIBgjA1PpYlAKgTcFQE5Be0q/UOmTf//5uExt ASDv8/3VeGnm0M7kAXM9ikFTsEQfLKJH+cFDfUryAR2uGqHrcSU6yCze X-Gm-Gg: AY/fxX7YLTgxCT+wkzIsOSmwTlFrdJAJBL7CtLfRl15Uftx632b5tq0yDlufTXhlp+5 Flruql9tYEB8nMwIeYbosXY5lNy86bL8KoOmYGRoK797ywHEsViC303uy+nHiifLIlmad9oMZtz taRF//pS8zidPdqFou+AObzF5T+2jCi4UEN+s4rNXTg0AEYvv7ya6aSHYBzV2lyLZwpjr0YmPPI TNBtE8YdzFiUylxpvOhp5mXPocCSEEtL8/7jK69egrRe+OcRKRt47byIfSesDbbdeB/3v9CqokO yIpZ/ZkZUnTlniFYD5gaLq0B/1ptb1JQp0OW0DU+/oOM91tkIq6Dj6SM2SHqF4LiGFTzv/stydN ZSdECosrEE4UR3R5dnUQyI7k4nXf6RRYtvi+IxPRS0w/JGslERgd65aMIGiCSinHfT3HVsyhA9p exrfn1V4mXoJXz6BH/3DRnn6WLmfPIRr09mMfYiznFOZYO5Lz9gpVSOfTBR/nY9AbK X-Google-Smtp-Source: AGHT+IFmQkAn76a6jPoUFSWskVsApd4EjT9EaQS9Lmch2wXWsdjxCu1JoZ0JGs1XSVlb+dqOkzOctw== X-Received: by 2002:a17:903:32cf:b0:2a2:dc3f:be4c with SMTP id d9443c01a7336-2a2f220d883mr423015965ad.10.1767535215342; Sun, 04 Jan 2026 06:00:15 -0800 (PST) Received: from localhost.localdomain (g163-131-201-140.scn-net.ne.jp. [163.131.201.140]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a2f3d4cb25sm432327535ad.56.2026.01.04.06.00.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Jan 2026 06:00:14 -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 v3 2/3] riscv: kvm: Fix vstimecmp update hazard on RV32 Date: Sun, 4 Jan 2026 22:59:37 +0900 Message-Id: <20260104135938.524-3-naohiko.shimizu@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260104135938.524-1-naohiko.shimizu@gmail.com> References: <20260104135938.524-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" 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. Fixes: 8f5cb44b1bae ("RISC-V: KVM: Support sstc extension") Signed-off-by: Naohiko Shimizu Reviewed-by: Anup Patel --- 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