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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/watchdog.c
38 * Author: Jacob Berkman <jacob@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_LNET
43 #include <linux/kthread.h>
44 #include <libcfs/libcfs.h>
45 #include "tracefile.h"
48 spinlock_t lcw_lock; /* check or change lcw_list */
49 int lcw_refcount; /* must hold lcw_pending_timers_lock */
50 struct timer_list lcw_timer; /* kernel timer */
51 struct list_head lcw_list; /* chain on pending list */
52 cfs_time_t lcw_last_touched;/* last touched stamp */
53 struct task_struct *lcw_task; /* owner task */
54 void (*lcw_callback)(pid_t, void *);
68 * The dispatcher will complete lcw_start_completion when it starts,
69 * and lcw_stop_completion when it exits.
70 * Wake lcw_event_waitq to signal timer callback dispatches.
72 static struct completion lcw_start_completion;
73 static struct completion lcw_stop_completion;
74 static wait_queue_head_t lcw_event_waitq;
77 * Set this and wake lcw_event_waitq to stop the dispatcher.
82 static unsigned long lcw_flags = 0;
85 * Number of outstanding watchdogs.
86 * When it hits 1, we start the dispatcher.
87 * When it hits 0, we stop the dispatcher.
89 static __u32 lcw_refcount = 0;
90 static DEFINE_MUTEX(lcw_refcount_mutex);
93 * List of timers that have fired that need their callbacks run by the
97 static DEFINE_SPINLOCK(lcw_pending_timers_lock);
98 static struct list_head lcw_pending_timers = LIST_HEAD_INIT(lcw_pending_timers);
100 /* Last time a watchdog expired */
101 static cfs_time_t lcw_last_watchdog_time;
102 static int lcw_recent_watchdog_count;
105 lcw_dump(struct lc_watchdog *lcw)
109 if (lcw->lcw_task == NULL) {
110 LCONSOLE_WARN("Process " LPPID " was not found in the task "
111 "list; watchdog callback may be incomplete\n",
114 libcfs_debug_dumpstack(lcw->lcw_task);
121 static void lcw_cb(ulong_ptr_t data)
123 struct lc_watchdog *lcw = (struct lc_watchdog *)data;
126 if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
131 lcw->lcw_state = LC_WATCHDOG_EXPIRED;
133 spin_lock_bh(&lcw->lcw_lock);
134 LASSERT(list_empty(&lcw->lcw_list));
136 spin_lock_bh(&lcw_pending_timers_lock);
137 lcw->lcw_refcount++; /* +1 for pending list */
138 list_add(&lcw->lcw_list, &lcw_pending_timers);
139 wake_up(&lcw_event_waitq);
141 spin_unlock_bh(&lcw_pending_timers_lock);
142 spin_unlock_bh(&lcw->lcw_lock);
146 static int is_watchdog_fired(void)
150 if (test_bit(LCW_FLAG_STOP, &lcw_flags))
153 spin_lock_bh(&lcw_pending_timers_lock);
154 rc = !list_empty(&lcw_pending_timers);
155 spin_unlock_bh(&lcw_pending_timers_lock);
159 static void lcw_dump_stack(struct lc_watchdog *lcw)
161 cfs_time_t current_time;
162 cfs_duration_t delta_time;
163 struct timeval timediff;
165 current_time = cfs_time_current();
166 delta_time = cfs_time_sub(current_time, lcw->lcw_last_touched);
167 cfs_duration_usec(delta_time, &timediff);
170 * Check to see if we should throttle the watchdog timer to avoid
171 * too many dumps going to the console thus triggering an NMI.
173 delta_time = cfs_duration_sec(cfs_time_sub(current_time,
174 lcw_last_watchdog_time));
176 if (delta_time < libcfs_watchdog_ratelimit &&
177 lcw_recent_watchdog_count > 3) {
178 LCONSOLE_WARN("Service thread pid %u was inactive for "
179 "%lu.%.02lus. Watchdog stack traces are limited "
180 "to 3 per %d seconds, skipping this one.\n",
183 timediff.tv_usec / 10000,
184 libcfs_watchdog_ratelimit);
186 if (delta_time < libcfs_watchdog_ratelimit) {
187 lcw_recent_watchdog_count++;
189 memcpy(&lcw_last_watchdog_time, ¤t_time,
190 sizeof(current_time));
191 lcw_recent_watchdog_count = 0;
194 LCONSOLE_WARN("Service thread pid %u was inactive for "
195 "%lu.%.02lus. The thread might be hung, or it "
196 "might only be slow and will resume later. "
197 "Dumping the stack trace for debugging purposes:"
201 timediff.tv_usec / 10000);
206 static int lcw_dispatch_main(void *data)
209 struct lc_watchdog *lcw;
210 struct list_head zombies = LIST_HEAD_INIT(zombies);
214 complete(&lcw_start_completion);
219 rc = wait_event_interruptible(lcw_event_waitq,
220 is_watchdog_fired());
221 CDEBUG(D_INFO, "Watchdog got woken up...\n");
222 if (test_bit(LCW_FLAG_STOP, &lcw_flags)) {
223 CDEBUG(D_INFO, "LCW_FLAG_STOP set, shutting down...\n");
225 spin_lock_bh(&lcw_pending_timers_lock);
226 rc = !list_empty(&lcw_pending_timers);
227 spin_unlock_bh(&lcw_pending_timers_lock);
229 CERROR("pending timers list was not empty at "
230 "time of watchdog dispatch shutdown\n");
235 spin_lock_bh(&lcw_pending_timers_lock);
236 while (!list_empty(&lcw_pending_timers)) {
239 lcw = list_entry(lcw_pending_timers.next,
240 struct lc_watchdog, lcw_list);
241 /* +1 ref for callback to make sure lwc wouldn't be
242 * deleted after releasing lcw_pending_timers_lock */
244 spin_unlock_bh(&lcw_pending_timers_lock);
247 spin_lock_bh(&lcw->lcw_lock);
248 spin_lock_bh(&lcw_pending_timers_lock);
250 if (list_empty(&lcw->lcw_list)) {
251 /* already removed from pending list */
252 lcw->lcw_refcount--; /* -1 ref for callback */
253 if (lcw->lcw_refcount == 0)
254 list_add(&lcw->lcw_list, &zombies);
255 spin_unlock_bh(&lcw->lcw_lock);
256 /* still hold lcw_pending_timers_lock */
260 list_del_init(&lcw->lcw_list);
261 lcw->lcw_refcount--; /* -1 ref for pending list */
263 spin_unlock_bh(&lcw_pending_timers_lock);
264 spin_unlock_bh(&lcw->lcw_lock);
266 CDEBUG(D_INFO, "found lcw for pid " LPPID "\n",
270 is_dumplog = lcw->lcw_callback == lc_watchdog_dumplog;
271 if (lcw->lcw_state != LC_WATCHDOG_DISABLED &&
272 (dumplog || !is_dumplog)) {
273 lcw->lcw_callback(lcw->lcw_pid, lcw->lcw_data);
274 if (dumplog && is_dumplog)
278 spin_lock_bh(&lcw_pending_timers_lock);
279 lcw->lcw_refcount--; /* -1 ref for callback */
280 if (lcw->lcw_refcount == 0)
281 list_add(&lcw->lcw_list, &zombies);
283 spin_unlock_bh(&lcw_pending_timers_lock);
285 while (!list_empty(&zombies)) {
286 lcw = list_entry(zombies.next,
287 struct lc_watchdog, lcw_list);
288 list_del_init(&lcw->lcw_list);
289 LIBCFS_FREE(lcw, sizeof(*lcw));
293 complete(&lcw_stop_completion);
298 static void lcw_dispatch_start(void)
300 struct task_struct *task;
303 LASSERT(lcw_refcount == 1);
305 init_completion(&lcw_stop_completion);
306 init_completion(&lcw_start_completion);
307 init_waitqueue_head(&lcw_event_waitq);
309 CDEBUG(D_INFO, "starting dispatch thread\n");
310 task = kthread_run(lcw_dispatch_main, NULL, "lc_watchdogd");
312 CERROR("error spawning watchdog dispatch thread: %ld\n",
317 wait_for_completion(&lcw_start_completion);
318 CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
323 static void lcw_dispatch_stop(void)
326 LASSERT(lcw_refcount == 0);
328 CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
330 set_bit(LCW_FLAG_STOP, &lcw_flags);
331 wake_up(&lcw_event_waitq);
333 wait_for_completion(&lcw_stop_completion);
335 CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
340 struct lc_watchdog *lc_watchdog_add(int timeout,
341 void (*callback)(pid_t, void *),
344 struct lc_watchdog *lcw = NULL;
347 LIBCFS_ALLOC(lcw, sizeof(*lcw));
349 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
350 RETURN(ERR_PTR(-ENOMEM));
353 spin_lock_init(&lcw->lcw_lock);
354 lcw->lcw_refcount = 1; /* refcount for owner */
355 lcw->lcw_task = current;
356 lcw->lcw_pid = current_pid();
357 lcw->lcw_callback = (callback != NULL) ? callback : lc_watchdog_dumplog;
358 lcw->lcw_data = data;
359 lcw->lcw_state = LC_WATCHDOG_DISABLED;
361 INIT_LIST_HEAD(&lcw->lcw_list);
362 cfs_timer_init(&lcw->lcw_timer, lcw_cb, lcw);
364 mutex_lock(&lcw_refcount_mutex);
365 if (++lcw_refcount == 1)
366 lcw_dispatch_start();
367 mutex_unlock(&lcw_refcount_mutex);
369 /* Keep this working in case we enable them by default */
370 if (lcw->lcw_state == LC_WATCHDOG_ENABLED) {
371 lcw->lcw_last_touched = cfs_time_current();
372 cfs_timer_arm(&lcw->lcw_timer, cfs_time_seconds(timeout) +
378 EXPORT_SYMBOL(lc_watchdog_add);
380 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
382 cfs_time_t newtime = cfs_time_current();
384 if (lcw->lcw_state == LC_WATCHDOG_EXPIRED) {
385 struct timeval timediff;
386 cfs_time_t delta_time = cfs_time_sub(newtime,
387 lcw->lcw_last_touched);
388 cfs_duration_usec(delta_time, &timediff);
390 LCONSOLE_WARN("Service thread pid %u %s after %lu.%.02lus. "
391 "This indicates the system was overloaded (too "
392 "many service threads, or there were not enough "
393 "hardware resources).\n",
397 timediff.tv_usec / 10000);
399 lcw->lcw_last_touched = newtime;
402 static void lc_watchdog_del_pending(struct lc_watchdog *lcw)
404 spin_lock_bh(&lcw->lcw_lock);
405 if (unlikely(!list_empty(&lcw->lcw_list))) {
406 spin_lock_bh(&lcw_pending_timers_lock);
407 list_del_init(&lcw->lcw_list);
408 lcw->lcw_refcount--; /* -1 ref for pending list */
409 spin_unlock_bh(&lcw_pending_timers_lock);
412 spin_unlock_bh(&lcw->lcw_lock);
415 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
418 LASSERT(lcw != NULL);
420 lc_watchdog_del_pending(lcw);
422 lcw_update_time(lcw, "resumed");
424 cfs_timer_arm(&lcw->lcw_timer, cfs_time_current() +
425 cfs_time_seconds(timeout));
426 lcw->lcw_state = LC_WATCHDOG_ENABLED;
430 EXPORT_SYMBOL(lc_watchdog_touch);
432 void lc_watchdog_disable(struct lc_watchdog *lcw)
435 LASSERT(lcw != NULL);
437 lc_watchdog_del_pending(lcw);
439 lcw_update_time(lcw, "completed");
440 lcw->lcw_state = LC_WATCHDOG_DISABLED;
444 EXPORT_SYMBOL(lc_watchdog_disable);
446 void lc_watchdog_delete(struct lc_watchdog *lcw)
451 LASSERT(lcw != NULL);
453 cfs_timer_disarm(&lcw->lcw_timer);
455 lcw_update_time(lcw, "stopped");
457 spin_lock_bh(&lcw->lcw_lock);
458 spin_lock_bh(&lcw_pending_timers_lock);
459 if (unlikely(!list_empty(&lcw->lcw_list))) {
460 list_del_init(&lcw->lcw_list);
461 lcw->lcw_refcount--; /* -1 ref for pending list */
464 lcw->lcw_refcount--; /* -1 ref for owner */
465 dead = lcw->lcw_refcount == 0;
466 spin_unlock_bh(&lcw_pending_timers_lock);
467 spin_unlock_bh(&lcw->lcw_lock);
470 LIBCFS_FREE(lcw, sizeof(*lcw));
472 mutex_lock(&lcw_refcount_mutex);
473 if (--lcw_refcount == 0)
475 mutex_unlock(&lcw_refcount_mutex);
479 EXPORT_SYMBOL(lc_watchdog_delete);
482 * Provided watchdog handlers
485 void lc_watchdog_dumplog(pid_t pid, void *data)
487 libcfs_debug_dumplog_internal((void *)((long_ptr_t)pid));
489 EXPORT_SYMBOL(lc_watchdog_dumplog);
491 #else /* !defined(WITH_WATCHDOG) */
493 struct lc_watchdog *lc_watchdog_add(int timeout,
494 void (*callback)(pid_t pid, void *),
497 static struct lc_watchdog watchdog;
500 EXPORT_SYMBOL(lc_watchdog_add);
502 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
505 EXPORT_SYMBOL(lc_watchdog_touch);
507 void lc_watchdog_disable(struct lc_watchdog *lcw)
510 EXPORT_SYMBOL(lc_watchdog_disable);
512 void lc_watchdog_delete(struct lc_watchdog *lcw)
515 EXPORT_SYMBOL(lc_watchdog_delete);