Skip to content

Commit 9fb6033

Browse files
committed
clocksource: Make watchdog reset lockless
KGDB needs to trylock watchdog_lock when trying to reset the clocksource watchdog after the system has been stopped to avoid a potential deadlock. When the trylock fails TSC usually becomes unstable. We can be more clever by using an atomic counter and checking it in the clocksource_watchdog callback. We restart the watchdog whenever the counter is > 0 and only decrement the counter when we ran through a full update cycle. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: John Stultz <johnstul@us.ibm.com> Acked-by: Jason Wessel <jason.wessel@windriver.com> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/alpine.LFD.2.02.1109121326280.2723@ionos Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent e8abccb commit 9fb6033

1 file changed

Lines changed: 18 additions & 20 deletions

File tree

‎kernel/time/clocksource.c‎

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ static struct timer_list watchdog_timer;
186186
static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
187187
static DEFINE_SPINLOCK(watchdog_lock);
188188
static int watchdog_running;
189+
static atomic_t watchdog_reset_pending;
189190

190191
static int clocksource_watchdog_kthread(void *data);
191192
static void __clocksource_change_rating(struct clocksource *cs, int rating);
@@ -247,12 +248,14 @@ static void clocksource_watchdog(unsigned long data)
247248
struct clocksource *cs;
248249
cycle_t csnow, wdnow;
249250
int64_t wd_nsec, cs_nsec;
250-
int next_cpu;
251+
int next_cpu, reset_pending;
251252

252253
spin_lock(&watchdog_lock);
253254
if (!watchdog_running)
254255
goto out;
255256

257+
reset_pending = atomic_read(&watchdog_reset_pending);
258+
256259
list_for_each_entry(cs, &watchdog_list, wd_list) {
257260

258261
/* Clocksource already marked unstable? */
@@ -268,7 +271,8 @@ static void clocksource_watchdog(unsigned long data)
268271
local_irq_enable();
269272

270273
/* Clocksource initialized ? */
271-
if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) {
274+
if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
275+
atomic_read(&watchdog_reset_pending)) {
272276
cs->flags |= CLOCK_SOURCE_WATCHDOG;
273277
cs->wd_last = wdnow;
274278
cs->cs_last = csnow;
@@ -283,8 +287,11 @@ static void clocksource_watchdog(unsigned long data)
283287
cs->cs_last = csnow;
284288
cs->wd_last = wdnow;
285289

290+
if (atomic_read(&watchdog_reset_pending))
291+
continue;
292+
286293
/* Check the deviation from the watchdog clocksource. */
287-
if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) {
294+
if ((abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD)) {
288295
clocksource_unstable(cs, cs_nsec - wd_nsec);
289296
continue;
290297
}
@@ -302,6 +309,13 @@ static void clocksource_watchdog(unsigned long data)
302309
}
303310
}
304311

312+
/*
313+
* We only clear the watchdog_reset_pending, when we did a
314+
* full cycle through all clocksources.
315+
*/
316+
if (reset_pending)
317+
atomic_dec(&watchdog_reset_pending);
318+
305319
/*
306320
* Cycle through CPUs to check if the CPUs stay synchronized
307321
* to each other.
@@ -344,23 +358,7 @@ static inline void clocksource_reset_watchdog(void)
344358

345359
static void clocksource_resume_watchdog(void)
346360
{
347-
unsigned long flags;
348-
349-
/*
350-
* We use trylock here to avoid a potential dead lock when
351-
* kgdb calls this code after the kernel has been stopped with
352-
* watchdog_lock held. When watchdog_lock is held we just
353-
* return and accept, that the watchdog might trigger and mark
354-
* the monitored clock source (usually TSC) unstable.
355-
*
356-
* This does not affect the other caller clocksource_resume()
357-
* because at this point the kernel is UP, interrupts are
358-
* disabled and nothing can hold watchdog_lock.
359-
*/
360-
if (!spin_trylock_irqsave(&watchdog_lock, flags))
361-
return;
362-
clocksource_reset_watchdog();
363-
spin_unlock_irqrestore(&watchdog_lock, flags);
361+
atomic_inc(&watchdog_reset_pending);
364362
}
365363

366364
static void clocksource_enqueue_watchdog(struct clocksource *cs)

0 commit comments

Comments
 (0)