* [PATCH 0/3] riscv: Fix potential spurious timer interrupts on RV32
@ 2026-01-03 9:44 Naohiko Shimizu
2026-01-03 9:44 ` [PATCH 1/3] riscv: clocksource: Fix stimecmp update hazard " Naohiko Shimizu
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Naohiko Shimizu @ 2026-01-03 9:44 UTC (permalink / raw)
To: pjw, palmer, aou
Cc: alex, anup, atish.patra, daniel.lezcano, tglx, nick.hu,
linux-riscv, linux-kernel, kvm, kvm-riscv, Naohiko Shimizu
This patch series addresses a potential race condition when updating 64-bit timer comparison registers (stimecmp/vstimecmp) on RV32 systems.
According to the RISC-V Privileged Specification (specifically section 3.2.1 for mtimecmp, which applies logically to stimcmp as well), updating a 64-bit comparison register via two 32-bit writes can cause a spurious interrupt if the intermediate state is evaluated by the hardware.
Currently, the Linux kernel (including v6.12) often writes the LSB first or does not use the recommended 3-step sequence (setting LSB to 0xFFFFFFFF first) on RV32. This can lead to a transient state where Time >= Compare is incorrectly true, triggering a "ghost" pending interrupt.
Changes:
1. Fix suspend_restore_csrs to use the 3-step sequence for stimecmp.
2. Fix KVM vcpu timer updates to prevent vstimecmp hazards.
3. Fix riscv_clock_next_event in the clocksource driver.
Although this hazard is difficult to observe in QEMU due to its instruction-boundary interrupt polling, it is a critical correctness issue for physical RV32 hardware implementations with asynchronous comparators.
Signed-off-by: Naohiko Shimizu <naohiko.shimizu@gmail.com>
Naohiko Shimizu (3):
riscv: clocksource: Fix stimecmp update hazard on RV32
riscv: kvm: Fix vstimecmp update hazard on RV32
riscv: suspend: Fix stimecmp update hazard on RV32
arch/riscv/kernel/suspend.c | 3 ++-
arch/riscv/kvm/vcpu_timer.c | 6 ++++--
drivers/clocksource/timer-riscv.c | 3 ++-
3 files changed, 8 insertions(+), 4 deletions(-)
--
2.39.5
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 1/3] riscv: clocksource: Fix stimecmp update hazard on RV32
2026-01-03 9:44 [PATCH 0/3] riscv: Fix potential spurious timer interrupts on RV32 Naohiko Shimizu
@ 2026-01-03 9:44 ` Naohiko Shimizu
2026-01-03 14:46 ` Anup Patel
2026-01-03 9:45 ` [PATCH 2/3] riscv: kvm: Fix vstimecmp " Naohiko Shimizu
2026-01-03 9:45 ` [PATCH 3/3] riscv: suspend: Fix stimecmp " Naohiko Shimizu
2 siblings, 1 reply; 8+ messages in thread
From: Naohiko Shimizu @ 2026-01-03 9:44 UTC (permalink / raw)
To: pjw, palmer, aou
Cc: alex, anup, atish.patra, daniel.lezcano, tglx, nick.hu,
linux-riscv, linux-kernel, kvm, kvm-riscv, Naohiko Shimizu
Signed-off-by: Naohiko Shimizu <naohiko.shimizu@gmail.com>
---
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,
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
--
2.39.5
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 2/3] riscv: kvm: Fix vstimecmp update hazard on RV32
2026-01-03 9:44 [PATCH 0/3] riscv: Fix potential spurious timer interrupts on RV32 Naohiko Shimizu
2026-01-03 9:44 ` [PATCH 1/3] riscv: clocksource: Fix stimecmp update hazard " Naohiko Shimizu
@ 2026-01-03 9:45 ` Naohiko Shimizu
2026-01-03 14:46 ` Anup Patel
2026-01-03 9:45 ` [PATCH 3/3] riscv: suspend: Fix stimecmp " Naohiko Shimizu
2 siblings, 1 reply; 8+ messages in thread
From: Naohiko Shimizu @ 2026-01-03 9:45 UTC (permalink / raw)
To: pjw, palmer, aou
Cc: alex, anup, atish.patra, daniel.lezcano, tglx, nick.hu,
linux-riscv, linux-kernel, kvm, kvm-riscv, Naohiko Shimizu
Signed-off-by: Naohiko Shimizu <naohiko.shimizu@gmail.com>
---
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_timer *t)
static int kvm_riscv_vcpu_update_vstimecmp(struct kvm_vcpu *vcpu, u64 ncycles)
{
#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;
#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
--
2.39.5
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 3/3] riscv: suspend: Fix stimecmp update hazard on RV32
2026-01-03 9:44 [PATCH 0/3] riscv: Fix potential spurious timer interrupts on RV32 Naohiko Shimizu
2026-01-03 9:44 ` [PATCH 1/3] riscv: clocksource: Fix stimecmp update hazard " Naohiko Shimizu
2026-01-03 9:45 ` [PATCH 2/3] riscv: kvm: Fix vstimecmp " Naohiko Shimizu
@ 2026-01-03 9:45 ` Naohiko Shimizu
2 siblings, 0 replies; 8+ messages in thread
From: Naohiko Shimizu @ 2026-01-03 9:45 UTC (permalink / raw)
To: pjw, palmer, aou
Cc: alex, anup, atish.patra, daniel.lezcano, tglx, nick.hu,
linux-riscv, linux-kernel, kvm, kvm-riscv, Naohiko Shimizu
Signed-off-by: Naohiko Shimizu <naohiko.shimizu@gmail.com>
---
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 *context)
#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);
}
csr_write(CSR_SATP, context->satp);
--
2.39.5
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 1/3] riscv: clocksource: Fix stimecmp update hazard on RV32
2026-01-03 9:44 ` [PATCH 1/3] riscv: clocksource: Fix stimecmp update hazard " Naohiko Shimizu
@ 2026-01-03 14:46 ` Anup Patel
2026-01-03 15:03 ` Naohiko Shimizu
2026-01-03 15:27 ` Naohiko Shimizu
0 siblings, 2 replies; 8+ messages in thread
From: Anup Patel @ 2026-01-03 14:46 UTC (permalink / raw)
To: Naohiko Shimizu
Cc: pjw, palmer, aou, alex, anup, atish.patra, daniel.lezcano, tglx,
nick.hu, linux-riscv, linux-kernel, kvm, kvm-riscv
On Sat, Jan 3, 2026 at 3:16 PM Naohiko Shimizu
<naohiko.shimizu@gmail.com> wrote:
Please add a detailed commit description about why the
new way of programming stimecmp is better. Also, explain
what the current Priv spec says in this context.
>
> Signed-off-by: Naohiko Shimizu <naohiko.shimizu@gmail.com>
> ---
> 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,
>
> 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
> --
> 2.39.5
>
>
Regards,
Anup
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 2/3] riscv: kvm: Fix vstimecmp update hazard on RV32
2026-01-03 9:45 ` [PATCH 2/3] riscv: kvm: Fix vstimecmp " Naohiko Shimizu
@ 2026-01-03 14:46 ` Anup Patel
0 siblings, 0 replies; 8+ messages in thread
From: Anup Patel @ 2026-01-03 14:46 UTC (permalink / raw)
To: Naohiko Shimizu
Cc: pjw, palmer, aou, alex, anup, atish.patra, daniel.lezcano, tglx,
nick.hu, linux-riscv, linux-kernel, kvm, kvm-riscv
On Sat, Jan 3, 2026 at 3:16 PM Naohiko Shimizu
<naohiko.shimizu@gmail.com> wrote:
Same comment as PATCH1.
>
> Signed-off-by: Naohiko Shimizu <naohiko.shimizu@gmail.com>
> ---
> 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_timer *t)
> static int kvm_riscv_vcpu_update_vstimecmp(struct kvm_vcpu *vcpu, u64 ncycles)
> {
> #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;
>
> #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
> --
> 2.39.5
>
>
> --
> kvm-riscv mailing list
> kvm-riscv@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/kvm-riscv
Regards,
Anup
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 1/3] riscv: clocksource: Fix stimecmp update hazard on RV32
2026-01-03 14:46 ` Anup Patel
@ 2026-01-03 15:03 ` Naohiko Shimizu
2026-01-03 15:27 ` Naohiko Shimizu
1 sibling, 0 replies; 8+ messages in thread
From: Naohiko Shimizu @ 2026-01-03 15:03 UTC (permalink / raw)
To: Anup Patel
Cc: pjw, palmer, aou, alex, anup, atish.patra, daniel.lezcano, tglx,
nick.hu, linux-riscv, linux-kernel, kvm, kvm-riscv
Hi Anup,
Thank you for your review.
I agree. I will update the commit description in v2 to explicitly cite
the RISC-V Privileged Specification and explain why the 3-step
programming sequence is necessary to avoid timer hazards on RV32.
Briefly, the current code writes the LSB first without ensuring it's
in the future, which can trigger a spurious interrupt if Time >=
{stimecmph(old), stimecmp(new)} becomes true momentarily. The new
sequence follows the spec's recommendation for mtimecmp (Section
3.2.1) to ensure the 64-bit value is always "in the future" during the
update.
I will apply the same detailed description to [PATCH 2/3] as well.
Regards, Naohiko Shimizu
On Sat, Jan 3, 2026 at 11:46 PM Anup Patel <apatel@ventanamicro.com> wrote:
>
> On Sat, Jan 3, 2026 at 3:16 PM Naohiko Shimizu
> <naohiko.shimizu@gmail.com> wrote:
>
> Please add a detailed commit description about why the
> new way of programming stimecmp is better. Also, explain
> what the current Priv spec says in this context.
>
> >
> > Signed-off-by: Naohiko Shimizu <naohiko.shimizu@gmail.com>
> > ---
> > 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,
> >
> > 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
> > --
> > 2.39.5
> >
> >
>
> Regards,
> Anup
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 1/3] riscv: clocksource: Fix stimecmp update hazard on RV32
2026-01-03 14:46 ` Anup Patel
2026-01-03 15:03 ` Naohiko Shimizu
@ 2026-01-03 15:27 ` Naohiko Shimizu
1 sibling, 0 replies; 8+ messages in thread
From: Naohiko Shimizu @ 2026-01-03 15:27 UTC (permalink / raw)
To: Anup Patel
Cc: pjw, palmer, aou, alex, anup, atish.patra, daniel.lezcano, tglx,
nick.hu, linux-riscv, linux-kernel, kvm, kvm-riscv
Hi Anup,
I have just submitted the v2 series with the detailed descriptions as
you suggested.
v2 link: https://lore.kernel.org/lkml/20260103152400.552-1-naohiko.shimizu@gmail.com/T/#u
Regards, Naohiko Shimizu
On Sat, Jan 3, 2026 at 11:46 PM Anup Patel <apatel@ventanamicro.com> wrote:
>
> On Sat, Jan 3, 2026 at 3:16 PM Naohiko Shimizu
> <naohiko.shimizu@gmail.com> wrote:
>
> Please add a detailed commit description about why the
> new way of programming stimecmp is better. Also, explain
> what the current Priv spec says in this context.
>
> >
> > Signed-off-by: Naohiko Shimizu <naohiko.shimizu@gmail.com>
> > ---
> > 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,
> >
> > 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
> > --
> > 2.39.5
> >
> >
>
> Regards,
> Anup
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-01-03 15:27 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-03 9:44 [PATCH 0/3] riscv: Fix potential spurious timer interrupts on RV32 Naohiko Shimizu
2026-01-03 9:44 ` [PATCH 1/3] riscv: clocksource: Fix stimecmp update hazard " Naohiko Shimizu
2026-01-03 14:46 ` Anup Patel
2026-01-03 15:03 ` Naohiko Shimizu
2026-01-03 15:27 ` Naohiko Shimizu
2026-01-03 9:45 ` [PATCH 2/3] riscv: kvm: Fix vstimecmp " Naohiko Shimizu
2026-01-03 14:46 ` Anup Patel
2026-01-03 9:45 ` [PATCH 3/3] riscv: suspend: Fix stimecmp " Naohiko Shimizu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®