4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * libcfs/libcfs/watchdog.c
34 * Author: Jacob Berkman <jacob@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include <linux/kthread.h>
40 #include <libcfs/libcfs.h>
41 #include "tracefile.h"
44 spinlock_t lcw_lock; /* check or change lcw_list */
45 int lcw_refcount; /* must hold lcw_pending_timers_lock */
46 struct timer_list lcw_timer; /* kernel timer */
47 struct list_head lcw_list; /* chain on pending list */
48 ktime_t lcw_last_touched;/* last touched stamp */
49 struct task_struct *lcw_task; /* owner task */
50 void (*lcw_callback)(pid_t, void *);
64 * The dispatcher will complete lcw_start_completion when it starts,
65 * and lcw_stop_completion when it exits.
66 * Wake lcw_event_waitq to signal timer callback dispatches.
68 static struct completion lcw_start_completion;
69 static struct completion lcw_stop_completion;
70 static wait_queue_head_t lcw_event_waitq;
73 * Set this and wake lcw_event_waitq to stop the dispatcher.
78 static unsigned long lcw_flags = 0;
81 * Number of outstanding watchdogs.
82 * When it hits 1, we start the dispatcher.
83 * When it hits 0, we stop the dispatcher.
85 static __u32 lcw_refcount = 0;
86 static DEFINE_MUTEX(lcw_refcount_mutex);
89 * List of timers that have fired that need their callbacks run by the
93 static DEFINE_SPINLOCK(lcw_pending_timers_lock);
94 static struct list_head lcw_pending_timers = LIST_HEAD_INIT(lcw_pending_timers);
96 /* Last time a watchdog expired */
97 static time64_t lcw_last_watchdog_time;
98 static int lcw_recent_watchdog_count;
101 lcw_dump(struct lc_watchdog *lcw)
105 if (lcw->lcw_task == NULL) {
106 LCONSOLE_WARN("Process %d was not found in the task "
107 "list; watchdog callback may be incomplete\n",
110 libcfs_debug_dumpstack(lcw->lcw_task);
117 static void lcw_cb(uintptr_t data)
119 struct lc_watchdog *lcw = (struct lc_watchdog *)data;
122 if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
127 lcw->lcw_state = LC_WATCHDOG_EXPIRED;
129 spin_lock_bh(&lcw->lcw_lock);
130 LASSERT(list_empty(&lcw->lcw_list));
132 spin_lock_bh(&lcw_pending_timers_lock);
133 lcw->lcw_refcount++; /* +1 for pending list */
134 list_add(&lcw->lcw_list, &lcw_pending_timers);
135 wake_up(&lcw_event_waitq);
137 spin_unlock_bh(&lcw_pending_timers_lock);
138 spin_unlock_bh(&lcw->lcw_lock);
142 static int is_watchdog_fired(void)
146 if (test_bit(LCW_FLAG_STOP, &lcw_flags))
149 spin_lock_bh(&lcw_pending_timers_lock);
150 rc = !list_empty(&lcw_pending_timers);
151 spin_unlock_bh(&lcw_pending_timers_lock);
155 static void lcw_dump_stack(struct lc_watchdog *lcw)
157 time64_t current_time = ktime_get_seconds();
158 struct timespec64 timediff;
161 timediff = ktime_to_timespec64(ktime_sub(ktime_get(),
162 lcw->lcw_last_touched));
164 * Check to see if we should throttle the watchdog timer to avoid
165 * too many dumps going to the console thus triggering an NMI.
167 delta_time = current_time - lcw_last_watchdog_time;
168 if (delta_time < libcfs_watchdog_ratelimit &&
169 lcw_recent_watchdog_count > 3) {
170 LCONSOLE_WARN("Service thread pid %u was inactive for %lu.%.02lus. Watchdog stack traces are limited to 3 per %d seconds, skipping this one.\n",
173 timediff.tv_nsec / (NSEC_PER_SEC / 100),
174 libcfs_watchdog_ratelimit);
176 if (delta_time < libcfs_watchdog_ratelimit) {
177 lcw_recent_watchdog_count++;
179 memcpy(&lcw_last_watchdog_time, ¤t_time,
180 sizeof(current_time));
181 lcw_recent_watchdog_count = 0;
184 LCONSOLE_WARN("Service thread pid %u was inactive for %lu.%.02lus. The thread might be hung, or it might only be slow and will resume later. Dumping the stack trace for debugging purposes:\n",
187 timediff.tv_nsec / (NSEC_PER_SEC / 100));
193 * Provided watchdog handlers
196 static void lc_watchdog_dumplog(pid_t pid, void *data)
198 libcfs_debug_dumplog_internal((void *)((uintptr_t)pid));
201 static int lcw_dispatch_main(void *data)
204 struct lc_watchdog *lcw;
205 struct list_head zombies = LIST_HEAD_INIT(zombies);
209 complete(&lcw_start_completion);
214 rc = wait_event_interruptible(lcw_event_waitq,
215 is_watchdog_fired());
216 CDEBUG(D_INFO, "Watchdog got woken up...\n");
217 if (test_bit(LCW_FLAG_STOP, &lcw_flags)) {
218 CDEBUG(D_INFO, "LCW_FLAG_STOP set, shutting down...\n");
220 spin_lock_bh(&lcw_pending_timers_lock);
221 rc = !list_empty(&lcw_pending_timers);
222 spin_unlock_bh(&lcw_pending_timers_lock);
224 CERROR("pending timers list was not empty at "
225 "time of watchdog dispatch shutdown\n");
230 spin_lock_bh(&lcw_pending_timers_lock);
231 while (!list_empty(&lcw_pending_timers)) {
234 lcw = list_entry(lcw_pending_timers.next,
235 struct lc_watchdog, lcw_list);
236 /* +1 ref for callback to make sure lwc wouldn't be
237 * deleted after releasing lcw_pending_timers_lock */
239 spin_unlock_bh(&lcw_pending_timers_lock);
242 spin_lock_bh(&lcw->lcw_lock);
243 spin_lock_bh(&lcw_pending_timers_lock);
245 if (list_empty(&lcw->lcw_list)) {
246 /* already removed from pending list */
247 lcw->lcw_refcount--; /* -1 ref for callback */
248 if (lcw->lcw_refcount == 0)
249 list_add(&lcw->lcw_list, &zombies);
250 spin_unlock_bh(&lcw->lcw_lock);
251 /* still hold lcw_pending_timers_lock */
255 list_del_init(&lcw->lcw_list);
256 lcw->lcw_refcount--; /* -1 ref for pending list */
258 spin_unlock_bh(&lcw_pending_timers_lock);
259 spin_unlock_bh(&lcw->lcw_lock);
261 CDEBUG(D_INFO, "found lcw for pid %d\n",
265 is_dumplog = lcw->lcw_callback == lc_watchdog_dumplog;
266 if (lcw->lcw_state != LC_WATCHDOG_DISABLED &&
267 (dumplog || !is_dumplog)) {
268 lcw->lcw_callback(lcw->lcw_pid, lcw->lcw_data);
269 if (dumplog && is_dumplog)
273 spin_lock_bh(&lcw_pending_timers_lock);
274 lcw->lcw_refcount--; /* -1 ref for callback */
275 if (lcw->lcw_refcount == 0)
276 list_add(&lcw->lcw_list, &zombies);
278 spin_unlock_bh(&lcw_pending_timers_lock);
280 while (!list_empty(&zombies)) {
281 lcw = list_entry(zombies.next,
282 struct lc_watchdog, lcw_list);
283 list_del_init(&lcw->lcw_list);
284 LIBCFS_FREE(lcw, sizeof(*lcw));
288 complete(&lcw_stop_completion);
293 static void lcw_dispatch_start(void)
295 struct task_struct *task;
298 LASSERT(lcw_refcount == 1);
300 init_completion(&lcw_stop_completion);
301 init_completion(&lcw_start_completion);
302 init_waitqueue_head(&lcw_event_waitq);
304 CDEBUG(D_INFO, "starting dispatch thread\n");
305 task = kthread_run(lcw_dispatch_main, NULL, "lc_watchdogd");
307 CERROR("error spawning watchdog dispatch thread: %ld\n",
312 wait_for_completion(&lcw_start_completion);
313 CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
318 static void lcw_dispatch_stop(void)
321 LASSERT(lcw_refcount == 0);
323 CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
325 set_bit(LCW_FLAG_STOP, &lcw_flags);
326 wake_up(&lcw_event_waitq);
328 wait_for_completion(&lcw_stop_completion);
330 CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
335 struct lc_watchdog *lc_watchdog_add(int timeout,
336 void (*callback)(pid_t, void *),
339 struct lc_watchdog *lcw = NULL;
342 LIBCFS_ALLOC(lcw, sizeof(*lcw));
344 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
345 RETURN(ERR_PTR(-ENOMEM));
348 spin_lock_init(&lcw->lcw_lock);
349 lcw->lcw_refcount = 1; /* refcount for owner */
350 lcw->lcw_task = current;
351 lcw->lcw_pid = current_pid();
352 lcw->lcw_callback = (callback != NULL) ? callback : lc_watchdog_dumplog;
353 lcw->lcw_data = data;
354 lcw->lcw_state = LC_WATCHDOG_DISABLED;
356 INIT_LIST_HEAD(&lcw->lcw_list);
357 setup_timer(&lcw->lcw_timer, lcw_cb, (unsigned long)lcw);
359 mutex_lock(&lcw_refcount_mutex);
360 if (++lcw_refcount == 1)
361 lcw_dispatch_start();
362 mutex_unlock(&lcw_refcount_mutex);
364 /* Keep this working in case we enable them by default */
365 if (lcw->lcw_state == LC_WATCHDOG_ENABLED) {
366 lcw->lcw_last_touched = ktime_get();
367 mod_timer(&lcw->lcw_timer, cfs_time_seconds(timeout) +
373 EXPORT_SYMBOL(lc_watchdog_add);
375 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
377 ktime_t newtime = ktime_get();
379 if (lcw->lcw_state == LC_WATCHDOG_EXPIRED) {
380 ktime_t lapse = ktime_sub(newtime, lcw->lcw_last_touched);
381 struct timespec64 timediff;
383 timediff = ktime_to_timespec64(lapse);
384 LCONSOLE_WARN("Service thread pid %u %s after %lu.%.02lus. This indicates the system was overloaded (too many service threads, or there were not enough hardware resources).\n",
385 lcw->lcw_pid, message,
387 timediff.tv_nsec / (NSEC_PER_SEC / 100));
389 lcw->lcw_last_touched = newtime;
392 static void lc_watchdog_del_pending(struct lc_watchdog *lcw)
394 spin_lock_bh(&lcw->lcw_lock);
395 if (unlikely(!list_empty(&lcw->lcw_list))) {
396 spin_lock_bh(&lcw_pending_timers_lock);
397 list_del_init(&lcw->lcw_list);
398 lcw->lcw_refcount--; /* -1 ref for pending list */
399 spin_unlock_bh(&lcw_pending_timers_lock);
402 spin_unlock_bh(&lcw->lcw_lock);
405 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
408 LASSERT(lcw != NULL);
410 lc_watchdog_del_pending(lcw);
412 lcw_update_time(lcw, "resumed");
414 mod_timer(&lcw->lcw_timer, jiffies + cfs_time_seconds(timeout));
415 lcw->lcw_state = LC_WATCHDOG_ENABLED;
419 EXPORT_SYMBOL(lc_watchdog_touch);
421 void lc_watchdog_disable(struct lc_watchdog *lcw)
424 LASSERT(lcw != NULL);
426 lc_watchdog_del_pending(lcw);
428 lcw_update_time(lcw, "completed");
429 lcw->lcw_state = LC_WATCHDOG_DISABLED;
433 EXPORT_SYMBOL(lc_watchdog_disable);
435 void lc_watchdog_delete(struct lc_watchdog *lcw)
440 LASSERT(lcw != NULL);
442 del_timer(&lcw->lcw_timer);
444 lcw_update_time(lcw, "stopped");
446 spin_lock_bh(&lcw->lcw_lock);
447 spin_lock_bh(&lcw_pending_timers_lock);
448 if (unlikely(!list_empty(&lcw->lcw_list))) {
449 list_del_init(&lcw->lcw_list);
450 lcw->lcw_refcount--; /* -1 ref for pending list */
453 lcw->lcw_refcount--; /* -1 ref for owner */
454 dead = lcw->lcw_refcount == 0;
455 spin_unlock_bh(&lcw_pending_timers_lock);
456 spin_unlock_bh(&lcw->lcw_lock);
459 LIBCFS_FREE(lcw, sizeof(*lcw));
461 mutex_lock(&lcw_refcount_mutex);
462 if (--lcw_refcount == 0)
464 mutex_unlock(&lcw_refcount_mutex);
468 EXPORT_SYMBOL(lc_watchdog_delete);
470 #else /* !defined(WITH_WATCHDOG) */
472 struct lc_watchdog *lc_watchdog_add(int timeout,
473 void (*callback)(pid_t pid, void *),
476 static struct lc_watchdog watchdog;
479 EXPORT_SYMBOL(lc_watchdog_add);
481 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
484 EXPORT_SYMBOL(lc_watchdog_touch);
486 void lc_watchdog_disable(struct lc_watchdog *lcw)
489 EXPORT_SYMBOL(lc_watchdog_disable);
491 void lc_watchdog_delete(struct lc_watchdog *lcw)
494 EXPORT_SYMBOL(lc_watchdog_delete);