From 0b400d86c0eaf6f221b54e5076b805fdcc925ca3 Mon Sep 17 00:00:00 2001 From: Andy Ross Date: Sun, 14 Nov 2021 14:27:30 -0800 Subject: [PATCH] drivers/timer/cavs_timer: Fix race in k_cycle_get_64() In commit 918a574c88c3 ("clock: add k_cycle_get_64") this driver was augmented with a count64() method to get a 64 bit cycle output from the two-32-bit-word device registers. Unfortunately it appeared to be trying to use a spinlock around the two (low/high) reads to protect against overflow. But that doesn't work: spinlocks protect against other CPU code using the same spinlock, not against a hardware counter that is incrementing in real time! Thankfully there was already a count() routine in place that does a detect-overflow-and-retry loop to solve this. Use that. Signed-off-by: Andy Ross --- drivers/timer/cavs_timer.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/drivers/timer/cavs_timer.c b/drivers/timer/cavs_timer.c index 3c2f2519bf6..9d824b4e812 100644 --- a/drivers/timer/cavs_timer.c +++ b/drivers/timer/cavs_timer.c @@ -77,17 +77,6 @@ static uint32_t count32(void) return shim_regs->walclk32_lo; } -static uint64_t count64(void) -{ - k_spinlock_key_t key = k_spin_lock(&lock); - uint64_t ret = shim_regs->walclk32_lo; - - ret |= (uint64_t)shim_regs->walclk32_hi << 32; - - k_spin_unlock(&lock, key); - return ret; -} - static void compare_isr(const void *arg) { ARG_UNUSED(arg); @@ -195,7 +184,7 @@ uint32_t sys_clock_cycle_get_32(void) uint64_t sys_clock_cycle_get_64(void) { - return count64(); + return count(); } /* Runs on secondary cores */