From nobody Sun Feb 8 13:48:24 2026 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 B9C3828505E for ; Sun, 4 Jan 2026 14:00:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767535214; cv=none; b=P92GJdAhUVE+6GOvLFEhh6RCSYATdUxSKyRpm8v3/s8UGOnToxCdpsxUUa42jBAoV2oGMSW8o9Z9wCxNr7M5H/4M+U4wtnySo77vaPsB78UKnwQrruNwVp6qahXRsgtPZIAVkAC4EHxh2hAClYLiG0T3L8SOr0dmZs60refrndM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767535214; c=relaxed/simple; bh=NsZ65gDJWN1ovH7JAOaHR6jRLwtFs3DZftFiqLTRFl8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YQq08/c9nZP6ETDKgWsd2Hz5ZQmPX+Rfz3crn3XTS/sa4TlFcTerZpLW5rZXUnbPlPQLbnala2wzBc0uz/R7EQKN5PsODTNrQ2U6t5Cgv/K5cWKHocImmwEnpueZUoE5VCH7mNJ6Xv3vsfPSJz7ZY8g3jip7lod9DZhqCAj5C/Q= 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=fmEKechD; arc=none smtp.client-ip=209.85.214.170 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="fmEKechD" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-29f2676bb21so185171605ad.0 for ; Sun, 04 Jan 2026 06:00:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767535212; x=1768140012; 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=uKC0H97nVlNsmqfTcj9M3djnqsg1WIKTEJ/Ie/Lsi00=; b=fmEKechDYbaCy5Ehxa6MC+Pys6iRRo8LEXHn2bnXXeE8/5+Rs89bBXF1BSGozZYM7l In0sz5gPfEgFBTp5D9cKqfyXxdWq7ZWTGRXJITu68Y2ymzuplfMnScC+Tygvm+XezPYg a65S5YVln1uXwq1v/cd2Ujo2mKskY6+mL9Cf7qPDJKYISDqmerJQJa1HUzOWnY6R3YWP aDmXFobPtKHvBBpq5h8SR+WSjne/bcN4mD9B92DX/dOzJ+WckM/uEbGUtuOewUJUOOzk F48W80/8dBR5vsf7QMIEB0SxebcieXuvoJFFVsdVPcAesa0nze3dnLftm2oYH1Unw3t/ H+Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767535212; x=1768140012; 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=uKC0H97nVlNsmqfTcj9M3djnqsg1WIKTEJ/Ie/Lsi00=; b=vh4JfrHqPeuYjO3OtVmDt7Nm5caBFCYZ45WHVzlDYmSXhBET13qQB52JNQ7xhcn8Xn /1aVfWbi1YhApZ4POew/DGf7a2Rc9hYQW7j3UNIEE9oIxj0PmepIBPsOnNHSP4K1DTkd 1Jdy8fZ0pFJXJ1QutcnmBNpJYGJQ8rPSyAbwubVrOQL2VDtsNgw+0Oet8ihd9sguZJwg p+HbLuL+hISx8u5HqGxOmVBnyLVA4J3hjWod0X37H9hnYs3Jn8oiiatBXGozB3Ga1MRZ jegEgmGJq6oTB5eUeUuR0VvUYTz4Gigw67JQYDWPAEALL//nlHMwxJLS/Fj+pd+5tQZD J+yw== X-Forwarded-Encrypted: i=1; AJvYcCWGmLHnuAwkoxQnfMNtTucmlPdnreE7NEaMSthD5EzvNjV2j2ltVLMToD80yuvB3fADVstVrjCHNR9sRRY=@vger.kernel.org X-Gm-Message-State: AOJu0YzLtThwyvRdGzZ5PFCyzq85mRnuaQ+bRUEoPOIIma9Rz7sZ2Hhi W+xYhoda4KQeUjwiKpp+2gGOjDk42uYtflV9KjgJdUdCcVNWuf051St6 X-Gm-Gg: AY/fxX4eYGKj6D8I7TQI+uiualA7HOnNSNNVtGl9mwPAsiCNk1k7Snc5lO547BsDhX+ dFct3G++/t33hLwZIFh5L82+JsGosPjmf0aj5Lk4IqdyjEBaNNx9SV5MTa/6VXMsisAu0YjPOgb kXrc3gyo2QZaE3srhEdo8pAU5qYhHxEWC2bMLeH0u90ktMoI1yRLjtrOn2UK7YnqKTFHsUB8qeT vXIIa+e+G7ABB/SweC+ktOwfalgcxJBaEYwPQXEO3PCHA0yFPzC1IIQ+Voyc1ecJrP2C7e4XXEO 03Z70YZXi5jsCsd7LBtjlTap9VPM77h6BELeQ3BEnvAEUQs9m7HVM33xyvpnxKc0iqnwLKMBWSl ytzcnfgDfFblWYgZA/f5hmR1qgFBpn/KxuHr6VvfVW1A79QBLwobjtQUj49lZ87XElJGTyGUXCS Oog9ujOl74UciRoOv2Nb04MUMCxGpPAiHmwF5m8un4jR3FR2PMYTyBjwwp0VMnvuZW X-Google-Smtp-Source: AGHT+IFWhEWVgZjHasmxigK04Dcv536ZNizp8RTdkFnv5HseXa6ZLIKf5NEo+F1Jf/zNHtSMHWTSZw== X-Received: by 2002:a17:903:2345:b0:2a0:d629:9035 with SMTP id d9443c01a7336-2a2f220cebfmr462612735ad.3.1767535211800; Sun, 04 Jan 2026 06:00:11 -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.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Jan 2026 06:00:11 -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 1/3] riscv: clocksource: Fix stimecmp update hazard on RV32 Date: Sun, 4 Jan 2026 22:59:36 +0900 Message-Id: <20260104135938.524-2-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: 9f7a8ff6391f ("RISC-V: Prefer sstc extension if available") Signed-off-by: Naohiko Shimizu Reviewed-by: Anup Patel --- drivers/clocksource/timer-riscv.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/clocksource/timer-riscv.c b/drivers/clocksource/timer-= riscv.c index 4d7cf338824a..cfc4d83c42c0 100644 --- a/drivers/clocksource/timer-riscv.c +++ b/drivers/clocksource/timer-riscv.c @@ -50,8 +50,9 @@ static int riscv_clock_next_event(unsigned long delta, =20 if (static_branch_likely(&riscv_sstc_available)) { #if defined(CONFIG_32BIT) - csr_write(CSR_STIMECMP, next_tval & 0xFFFFFFFF); + csr_write(CSR_STIMECMP, ULONG_MAX); csr_write(CSR_STIMECMPH, next_tval >> 32); + csr_write(CSR_STIMECMP, next_tval & 0xFFFFFFFF); #else csr_write(CSR_STIMECMP, next_tval); #endif --=20 2.39.5 From nobody Sun Feb 8 13:48:24 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 From nobody Sun Feb 8 13:48:24 2026 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 DDE343A1E99 for ; Sun, 4 Jan 2026 14:00:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767535221; cv=none; b=Ep24xm4F5lZ92G2Y1qnE+vdv/WFH/GEhYf2c7hbudWM4xToVaqPA3pi3W3Ga6tzMUJA6QiVvQWR3Johehfeod/1mkXXV885zjMKVAmrqJZmkW8SO0XqTTaWojy7bDUQgHi2fQMpkIIeOP6ZLGIrhCwyE2iSn5CMuJrV3dZ/GkPs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767535221; c=relaxed/simple; bh=smLKmAbNA+Lg2DxKtUMzNvxFdylFJ6K1oRlbE5gn/dA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MyIdqgLP4YyQy9iSq6p1QnzxDW2yWfGnkpOD7fXo+GgPNrFNCR3EPDFe3a26QlUw/8lWRusURQNcJFC6iGTxcZfofDK+y+whVcmvb3ouBgBPLkSXad8bIG5gJjedoH95iMqQG3kRa/Hzx0yq0ua2gGRne6PrcIwL3Tvu3CPCKkA= 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=hwd2jAOx; arc=none smtp.client-ip=209.85.214.173 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="hwd2jAOx" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2a07fac8aa1so143827205ad.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=qqmQmaFNmFjEy7S54wIHblwNMJ9e6UGwg3T65EY/Hjg=; b=hwd2jAOxW2xI41oyKa1XRrSNjRbA/Y/Vlo5XdM9e0RSYwP3h4R8K50TU1sbNfDOKnu +D+exPSq0RT0Eq2Bn8MOOjvD2icR1kOOaIMQe5b3VgbzJbHyF6WLzJtY1Sj7kSFh3/gl qnf9KdkaX6TqTGCv15TUTS1Zf2U5VQgs5zPMMV6ThS0Ut+JXWHoXSrOGqmu2QC0Vdjdy uHQKned6gexHGgF311kyycSgqSdpRrxlGadAXL5lK6ZOcXefa2Mq2iSdZHYS7Wno17GU CRXVY+4+WeyQnbfAtXzn4ndTxbBL/b/z/3Qt1Aa2HUtjvFXjjo50/H5IMfSphw9F5kNa NYYQ== 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=qqmQmaFNmFjEy7S54wIHblwNMJ9e6UGwg3T65EY/Hjg=; b=JOb3m6OJamvBLv4pmPw3QrB8c65UXj52biFo6xKqfOmsGno9rkc2+Y8GTRTokplEaI yA3A0IzoP5L/3f6ooVMRyUbZNGOO2pYVCCm++c90ZC/W0bLVZTym1xOD4WNJisMXiX1+ hQEWXEFuzwoBZm9rGrPJKX8Y4pLz4U3CuYh1DHm0qRx7ZrkNP1Dr4mI0xsZnRNArCzEw RvWhLmB43EVmeP5KxXKPMhVMte8+rwZorP0JAH9OIG/yo5ka4Q+bqOO0MtHSAHMHNm/V cQOooh299O2tkbTc3Pf1pSsGKL8r1afUW5Lrw71iv8st4nJ2uq2sKQeUu+9IigI5OW3r Cdaw== X-Forwarded-Encrypted: i=1; AJvYcCWv/pNZvOG+A0tYIwAWiNs07xtQEMa2bRxy6JaPDp4N4fwDyfAWDxIUt4t/1xKUp3iK2zwtpbEYTyAs7D8=@vger.kernel.org X-Gm-Message-State: AOJu0YzBWYzl8JyfDtRclfOvceGt7m2lzj9EDHj2I4DFqG857bl129KP jBn1Om9YHG7N/mcl6QGY9np3bR8VIcLUS3PsksNCMlL+EbSxbrEkwz9E X-Gm-Gg: AY/fxX7mWKKqezd68aooKLxWIAUrneM/nqLsIAWgrdMgcbcywcyxlxzNPloNcuxY+F8 NBqpmQPOwGkvCuJTVLR3VPIpFw4eRPCttDBQ1fth1KqDUJqc+aLSoulbtG/uqz+HPygAwp/Vt4+ 2Wz1ZY1zTfaETm/9hwvGnVy7BmllAUbOx4KHbqJAgAXfucGrrOgjiXMdQd+SBWgiSWInjFi08z6 ZCmuyFh9LkutTncA0UtV2un99wgUjpKClxXxqdJQnUbxfOABUBmZ/0wH/SMt4jON4PmalNutkBp v+pbB8xUnEbaKQ2QQRojCgoQoUScOYeB1FzXQdudvB6cfsoKbdJyVstwa9ojofFxMQQIEro1f2V GkauaZA8DmdR6V7FKPspJ2M/DlmusJ3dqTjX/IdT2ouO/rbC77fiRC028sQb9TisHzcBltkN1pe ruXTL3OPyqn+RPQYvx1u5Xdm2llw8sKGCZHwSQTG6S+TZhlO4EVPmgci3TtF++PyUk X-Google-Smtp-Source: AGHT+IFF0zPQhAdEGQIhG511RClUe4pVQFBGTYZnIh7qDrj28ymiDIGxnpivMIor8szJu6tv6fbZ2A== X-Received: by 2002:a17:902:f546:b0:2a0:c84f:412c with SMTP id d9443c01a7336-2a2f2a3be72mr478366475ad.57.1767535219101; Sun, 04 Jan 2026 06:00:19 -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.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Jan 2026 06:00:18 -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 3/3] riscv: suspend: Fix stimecmp update hazard on RV32 Date: Sun, 4 Jan 2026 22:59:38 +0900 Message-Id: <20260104135938.524-4-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: ffef54ad4110 ("riscv: Add stimecmp save and restore") Signed-off-by: Naohiko Shimizu Reviewed-by: Anup Patel --- arch/riscv/kernel/suspend.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/riscv/kernel/suspend.c b/arch/riscv/kernel/suspend.c index 24b3f57d467f..aff93090c4ef 100644 --- a/arch/riscv/kernel/suspend.c +++ b/arch/riscv/kernel/suspend.c @@ -51,10 +51,11 @@ void suspend_restore_csrs(struct suspend_context *conte= xt) =20 #ifdef CONFIG_MMU if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SSTC)) { - csr_write(CSR_STIMECMP, context->stimecmp); #if __riscv_xlen < 64 + csr_write(CSR_STIMECMP, ULONG_MAX); csr_write(CSR_STIMECMPH, context->stimecmph); #endif + csr_write(CSR_STIMECMP, context->stimecmp); } =20 csr_write(CSR_SATP, context->satp); --=20 2.39.5