From nobody Mon Feb 9 16:16:59 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