From: Kuan-Wei Chiu <visitorckw@gmail.com>
To: daniel.lezcano@kernel.org, tglx@kernel.org
Cc: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu,
geert@linux-m68k.org, laurent@vivier.eu, marscheng@google.com,
eleanor15x@gmail.com, jserv@ccns.ncku.edu.tw,
linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org,
linux-m68k@lists.linux-m68k.org,
Kuan-Wei Chiu <visitorckw@gmail.com>
Subject: [PATCH v2 1/9] clocksource/drivers/timer-goldfish: Fix latch corruption with spinlock
Date: Thu, 26 Nov 2026 18:10:53 +0000 [thread overview]
Message-ID: <20261126181101.3715754-2-visitorckw@gmail.com> (raw)
In-Reply-To: <20261126181101.3715754-1-visitorckw@gmail.com>
Reading TIMER_TIME_LOW latches the upper 32 bits of the current time
into TIMER_TIME_HIGH. Because goldfish_timer_read() reads these two
registers without synchronization, a concurrent reader from an interrupt
or another CPU can read TIMER_TIME_LOW in between and overwrite the
latched high bits.
Protect the register read sequence with a raw spinlock.
Fixes: c92e7ef16400 ("clocksource/drivers: Add a goldfish-timer clocksource")
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
drivers/clocksource/timer-goldfish.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/drivers/clocksource/timer-goldfish.c b/drivers/clocksource/timer-goldfish.c
index 3fe12c7ee7b7..cc02b012ff76 100644
--- a/drivers/clocksource/timer-goldfish.c
+++ b/drivers/clocksource/timer-goldfish.c
@@ -6,6 +6,7 @@
#include <linux/clockchips.h>
#include <linux/module.h>
#include <linux/slab.h>
+#include <linux/spinlock.h>
#include <linux/goldfish.h>
#include <clocksource/timer-goldfish.h>
@@ -14,6 +15,7 @@ struct goldfish_timer {
struct clock_event_device ced;
struct resource res;
void __iomem *base;
+ raw_spinlock_t lock;
};
static struct goldfish_timer *ced_to_gf(struct clock_event_device *ced)
@@ -30,6 +32,7 @@ static u64 goldfish_timer_read(struct clocksource *cs)
{
struct goldfish_timer *timerdrv = cs_to_gf(cs);
void __iomem *base = timerdrv->base;
+ unsigned long flags;
u32 time_low, time_high;
u64 ticks;
@@ -37,8 +40,10 @@ static u64 goldfish_timer_read(struct clocksource *cs)
* time_low: get low bits of current time and update time_high
* time_high: get high bits of time at last time_low read
*/
+ raw_spin_lock_irqsave(&timerdrv->lock, flags);
time_low = gf_ioread32(base + TIMER_TIME_LOW);
time_high = gf_ioread32(base + TIMER_TIME_HIGH);
+ raw_spin_unlock_irqrestore(&timerdrv->lock, flags);
ticks = ((u64)time_high << 32) | time_low;
@@ -107,6 +112,7 @@ int __init goldfish_timer_init(int irq, void __iomem *base)
return -ENOMEM;
timerdrv->base = base;
+ raw_spin_lock_init(&timerdrv->lock);
timerdrv->ced = (struct clock_event_device){
.name = "goldfish_timer",
--
2.56.1.839.g22a8290db9-goog
WARNING: multiple messages have this Message-ID (diff)
From: Kuan-Wei Chiu <visitorckw@gmail.com>
To: daniel.lezcano@kernel.org, tglx@kernel.org
Cc: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu,
geert@linux-m68k.org, laurent@vivier.eu, marscheng@google.com,
eleanor15x@gmail.com, jserv@ccns.ncku.edu.tw,
linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org,
linux-m68k@lists.linux-m68k.org,
Kuan-Wei Chiu <visitorckw@gmail.com>
Subject: [PATCH v2 1/9] clocksource/drivers/timer-goldfish: Fix latch corruption with spinlock
Date: Thu, 26 Nov 2026 18:10:53 +0000 [thread overview]
Message-ID: <20261126181101.3715754-2-visitorckw@gmail.com> (raw)
In-Reply-To: <20261126181101.3715754-1-visitorckw@gmail.com>
Reading TIMER_TIME_LOW latches the upper 32 bits of the current time
into TIMER_TIME_HIGH. Because goldfish_timer_read() reads these two
registers without synchronization, a concurrent reader from an interrupt
or another CPU can read TIMER_TIME_LOW in between and overwrite the
latched high bits.
Protect the register read sequence with a raw spinlock.
Fixes: c92e7ef16400 ("clocksource/drivers: Add a goldfish-timer clocksource")
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
drivers/clocksource/timer-goldfish.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/drivers/clocksource/timer-goldfish.c b/drivers/clocksource/timer-goldfish.c
index 3fe12c7ee7b7..cc02b012ff76 100644
--- a/drivers/clocksource/timer-goldfish.c
+++ b/drivers/clocksource/timer-goldfish.c
@@ -6,6 +6,7 @@
#include <linux/clockchips.h>
#include <linux/module.h>
#include <linux/slab.h>
+#include <linux/spinlock.h>
#include <linux/goldfish.h>
#include <clocksource/timer-goldfish.h>
@@ -14,6 +15,7 @@ struct goldfish_timer {
struct clock_event_device ced;
struct resource res;
void __iomem *base;
+ raw_spinlock_t lock;
};
static struct goldfish_timer *ced_to_gf(struct clock_event_device *ced)
@@ -30,6 +32,7 @@ static u64 goldfish_timer_read(struct clocksource *cs)
{
struct goldfish_timer *timerdrv = cs_to_gf(cs);
void __iomem *base = timerdrv->base;
+ unsigned long flags;
u32 time_low, time_high;
u64 ticks;
@@ -37,8 +40,10 @@ static u64 goldfish_timer_read(struct clocksource *cs)
* time_low: get low bits of current time and update time_high
* time_high: get high bits of time at last time_low read
*/
+ raw_spin_lock_irqsave(&timerdrv->lock, flags);
time_low = gf_ioread32(base + TIMER_TIME_LOW);
time_high = gf_ioread32(base + TIMER_TIME_HIGH);
+ raw_spin_unlock_irqrestore(&timerdrv->lock, flags);
ticks = ((u64)time_high << 32) | time_low;
@@ -107,6 +112,7 @@ int __init goldfish_timer_init(int irq, void __iomem *base)
return -ENOMEM;
timerdrv->base = base;
+ raw_spin_lock_init(&timerdrv->lock);
timerdrv->ced = (struct clock_event_device){
.name = "goldfish_timer",
--
2.56.1.839.g22a8290db9-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2026-11-26 18:11 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 18:10 [PATCH v2 0/9] clocksource/drivers/timer-goldfish: Fix bugs and add DT support Kuan-Wei Chiu
2026-11-26 18:10 ` Kuan-Wei Chiu
2026-11-26 18:10 ` Kuan-Wei Chiu [this message]
2026-11-26 18:10 ` [PATCH v2 1/9] clocksource/drivers/timer-goldfish: Fix latch corruption with spinlock Kuan-Wei Chiu
2026-11-26 18:10 ` [PATCH v2 2/9] clocksource/drivers/timer-goldfish: Fix oneshot mode spurious interrupt Kuan-Wei Chiu
2026-11-26 18:10 ` Kuan-Wei Chiu
2026-11-26 18:10 ` [PATCH v2 3/9] clocksource/drivers/timer-goldfish: Fix resource leaks in init Kuan-Wei Chiu
2026-11-26 18:10 ` Kuan-Wei Chiu
2026-11-26 18:10 ` [PATCH v2 4/9] clocksource/drivers/timer-goldfish: Set CLOCK_SOURCE_IS_CONTINUOUS flag Kuan-Wei Chiu
2026-11-26 18:10 ` Kuan-Wei Chiu
2026-11-26 18:10 ` [PATCH v2 5/9] clocksource/drivers/timer-goldfish: Drop redundant max_idle_ns setting Kuan-Wei Chiu
2026-11-26 18:10 ` Kuan-Wei Chiu
2026-11-26 18:10 ` [PATCH v2 6/9] clocksource/drivers/timer-goldfish: Set clockevent cpumask and irq Kuan-Wei Chiu
2026-11-26 18:10 ` Kuan-Wei Chiu
2026-11-26 18:10 ` [PATCH v2 7/9] clocksource/drivers/timer-goldfish: Set clock_event_device rating Kuan-Wei Chiu
2026-11-26 18:10 ` Kuan-Wei Chiu
2026-11-26 18:11 ` [PATCH v2 8/9] clocksource/drivers/timer-goldfish: Add device tree probing support Kuan-Wei Chiu
2026-11-26 18:11 ` Kuan-Wei Chiu
2026-11-26 18:11 ` [PATCH v2 9/9] MAINTAINERS: Add entry for goldfish timer driver Kuan-Wei Chiu
2026-11-26 18:11 ` Kuan-Wei Chiu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261126181101.3715754-2-visitorckw@gmail.com \
--to=visitorckw@gmail.com \
--cc=aou@eecs.berkeley.edu \
--cc=daniel.lezcano@kernel.org \
--cc=eleanor15x@gmail.com \
--cc=geert@linux-m68k.org \
--cc=jserv@ccns.ncku.edu.tw \
--cc=laurent@vivier.eu \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-m68k@lists.linux-m68k.org \
--cc=linux-riscv@lists.infradead.org \
--cc=marscheng@google.com \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=tglx@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.