[PATCH 1/8] clocksource/drivers/timer-goldfish: Fix latch corruption with spinlock
Kuan-Wei Chiu
visitorckw at gmail.com
Thu Nov 5 08:16:55 PST 2026
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 at 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.0.398.g882cbad275-goog
More information about the linux-riscv
mailing list