1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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 <libcfs/libcfs.h>
44 #include "tracefile.h"
47 cfs_timer_t lcw_timer; /* kernel timer */
48 struct list_head lcw_list;
49 cfs_time_t lcw_last_touched;
52 void (*lcw_callback)(pid_t, void *);
66 * The dispatcher will complete lcw_start_completion when it starts,
67 * and lcw_stop_completion when it exits.
68 * Wake lcw_event_waitq to signal timer callback dispatches.
70 static struct completion lcw_start_completion;
71 static struct completion lcw_stop_completion;
72 static cfs_waitq_t lcw_event_waitq;
75 * Set this and wake lcw_event_waitq to stop the dispatcher.
80 static unsigned long lcw_flags = 0;
83 * Number of outstanding watchdogs.
84 * When it hits 1, we start the dispatcher.
85 * When it hits 0, we stop the distpatcher.
87 static __u32 lcw_refcount = 0;
88 static DECLARE_MUTEX(lcw_refcount_sem);
91 * List of timers that have fired that need their callbacks run by the
94 static spinlock_t lcw_pending_timers_lock = SPIN_LOCK_UNLOCKED; /* BH lock! */
95 static struct list_head lcw_pending_timers = \
96 CFS_LIST_HEAD_INIT(lcw_pending_timers);
98 /* Last time a watchdog expired */
99 static cfs_time_t lcw_last_watchdog_time;
100 static int lcw_recent_watchdog_count;
101 static spinlock_t lcw_last_watchdog_lock = SPIN_LOCK_UNLOCKED;
104 lcw_dump(struct lc_watchdog *lcw)
107 #if defined(HAVE_TASKLIST_LOCK)
108 read_lock(&tasklist_lock);
109 #elif defined(HAVE_TASK_RCU)
112 CERROR("unable to dump stack because of missing export\n");
115 if (lcw->lcw_task == NULL) {
116 CWARN("Process " LPPID " was not found in the task list; "
117 "watchdog callback may be incomplete\n",
120 libcfs_debug_dumpstack(lcw->lcw_task);
123 #if defined(HAVE_TASKLIST_LOCK)
124 read_unlock(&tasklist_lock);
125 #elif defined(HAVE_TASK_RCU)
131 static void lcw_cb(ulong_ptr_t data)
133 struct lc_watchdog *lcw = (struct lc_watchdog *)data;
134 cfs_time_t current_time;
135 cfs_duration_t delta_time;
136 struct timeval timediff;
140 if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
145 lcw->lcw_state = LC_WATCHDOG_EXPIRED;
146 current_time = cfs_time_current();
148 delta_time = cfs_time_sub(current_time, lcw->lcw_last_touched);
149 cfs_duration_usec(delta_time, &timediff);
151 /* Check to see if we should throttle the watchdog timer to avoid
152 * too many dumps going to the console thus triggering an NMI.
153 * Normally we would not hold the spin lock over the CWARN but in
154 * this case we hold it to ensure non ratelimited lcw_dumps are not
155 * interleaved on the console making them hard to read. */
156 spin_lock_bh(&lcw_last_watchdog_lock);
157 delta_time = cfs_duration_sec(cfs_time_sub(current_time,
158 lcw_last_watchdog_time));
160 if (delta_time < libcfs_watchdog_ratelimit &&
161 lcw_recent_watchdog_count > 3) {
162 CWARN("Refusing to fire watchdog for pid %d: it was inactive "
163 "for %lu.%.02lus. Rate limiting 3 per %d seconds.\n",
164 (int)lcw->lcw_pid, timediff.tv_sec,
165 timediff.tv_usec / 10000, libcfs_watchdog_ratelimit);
167 if (delta_time < libcfs_watchdog_ratelimit) {
168 lcw_recent_watchdog_count++;
170 memcpy(&lcw_last_watchdog_time, ¤t_time,
171 sizeof(current_time));
172 lcw_recent_watchdog_count = 0;
175 /* This warning should appear on the console, but may not get
176 * into the logs since we're running in a softirq handler */
177 CWARN("Watchdog triggered for pid %d: it was inactive for "
178 "%lu.%.02lus\n", (int)lcw->lcw_pid, timediff.tv_sec,
179 timediff.tv_usec / 10000);
183 spin_unlock_bh(&lcw_last_watchdog_lock);
184 spin_lock_bh(&lcw_pending_timers_lock);
186 if (list_empty(&lcw->lcw_list)) {
187 list_add(&lcw->lcw_list, &lcw_pending_timers);
188 cfs_waitq_signal(&lcw_event_waitq);
191 spin_unlock_bh(&lcw_pending_timers_lock);
196 static int is_watchdog_fired(void)
200 if (test_bit(LCW_FLAG_STOP, &lcw_flags))
203 spin_lock_bh(&lcw_pending_timers_lock);
204 rc = !list_empty(&lcw_pending_timers);
205 spin_unlock_bh(&lcw_pending_timers_lock);
209 static int lcw_dispatch_main(void *data)
213 struct lc_watchdog *lcw;
217 cfs_daemonize("lc_watchdogd");
219 SIGNAL_MASK_LOCK(current, flags);
220 sigfillset(¤t->blocked);
222 SIGNAL_MASK_UNLOCK(current, flags);
224 complete(&lcw_start_completion);
227 cfs_wait_event_interruptible(lcw_event_waitq, is_watchdog_fired(), rc);
228 CDEBUG(D_INFO, "Watchdog got woken up...\n");
229 if (test_bit(LCW_FLAG_STOP, &lcw_flags)) {
230 CDEBUG(D_INFO, "LCW_FLAG_STOP was set, shutting down...\n");
232 spin_lock_bh(&lcw_pending_timers_lock);
233 rc = !list_empty(&lcw_pending_timers);
234 spin_unlock_bh(&lcw_pending_timers_lock);
236 CERROR("pending timers list was not empty at "
237 "time of watchdog dispatch shutdown\n");
242 spin_lock_bh(&lcw_pending_timers_lock);
243 while (!list_empty(&lcw_pending_timers)) {
245 lcw = list_entry(lcw_pending_timers.next,
248 list_del_init(&lcw->lcw_list);
249 spin_unlock_bh(&lcw_pending_timers_lock);
251 CDEBUG(D_INFO, "found lcw for pid " LPPID "\n", lcw->lcw_pid);
253 if (lcw->lcw_state != LC_WATCHDOG_DISABLED)
254 lcw->lcw_callback(lcw->lcw_pid, lcw->lcw_data);
256 spin_lock_bh(&lcw_pending_timers_lock);
258 spin_unlock_bh(&lcw_pending_timers_lock);
261 complete(&lcw_stop_completion);
266 static void lcw_dispatch_start(void)
271 LASSERT(lcw_refcount == 1);
273 init_completion(&lcw_stop_completion);
274 init_completion(&lcw_start_completion);
275 cfs_waitq_init(&lcw_event_waitq);
277 CDEBUG(D_INFO, "starting dispatch thread\n");
278 rc = kernel_thread(lcw_dispatch_main, NULL, 0);
280 CERROR("error spawning watchdog dispatch thread: %d\n", rc);
284 wait_for_completion(&lcw_start_completion);
285 CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
290 static void lcw_dispatch_stop(void)
293 LASSERT(lcw_refcount == 0);
295 CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
297 set_bit(LCW_FLAG_STOP, &lcw_flags);
298 cfs_waitq_signal(&lcw_event_waitq);
300 wait_for_completion(&lcw_stop_completion);
302 CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
307 struct lc_watchdog *lc_watchdog_add(int timeout,
308 void (*callback)(pid_t, void *),
311 struct lc_watchdog *lcw = NULL;
314 LIBCFS_ALLOC(lcw, sizeof(*lcw));
316 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
317 RETURN(ERR_PTR(-ENOMEM));
320 lcw->lcw_task = cfs_current();
321 lcw->lcw_pid = cfs_curproc_pid();
322 lcw->lcw_callback = (callback != NULL) ? callback : lc_watchdog_dumplog;
323 lcw->lcw_data = data;
324 lcw->lcw_state = LC_WATCHDOG_DISABLED;
326 CFS_INIT_LIST_HEAD(&lcw->lcw_list);
327 cfs_timer_init(&lcw->lcw_timer, lcw_cb, lcw);
329 down(&lcw_refcount_sem);
330 if (++lcw_refcount == 1)
331 lcw_dispatch_start();
332 up(&lcw_refcount_sem);
334 /* Keep this working in case we enable them by default */
335 if (lcw->lcw_state == LC_WATCHDOG_ENABLED) {
336 lcw->lcw_last_touched = cfs_time_current();
337 cfs_timer_arm(&lcw->lcw_timer, cfs_time_seconds(timeout) +
343 EXPORT_SYMBOL(lc_watchdog_add);
345 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
347 cfs_time_t newtime = cfs_time_current();;
349 if (lcw->lcw_state == LC_WATCHDOG_EXPIRED) {
350 struct timeval timediff;
351 cfs_time_t delta_time = cfs_time_sub(newtime,
352 lcw->lcw_last_touched);
353 cfs_duration_usec(delta_time, &timediff);
355 CWARN("Expired watchdog for pid " LPPID " %s after %lu.%.02lus\n",
356 lcw->lcw_pid, message, timediff.tv_sec,
357 timediff.tv_usec / 10000);
359 lcw->lcw_last_touched = newtime;
362 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
365 LASSERT(lcw != NULL);
367 spin_lock_bh(&lcw_pending_timers_lock);
368 list_del_init(&lcw->lcw_list);
369 spin_unlock_bh(&lcw_pending_timers_lock);
371 lcw_update_time(lcw, "touched");
372 lcw->lcw_state = LC_WATCHDOG_ENABLED;
374 cfs_timer_arm(&lcw->lcw_timer, cfs_time_current() +
375 cfs_time_seconds(timeout));
379 EXPORT_SYMBOL(lc_watchdog_touch);
381 void lc_watchdog_disable(struct lc_watchdog *lcw)
384 LASSERT(lcw != NULL);
386 spin_lock_bh(&lcw_pending_timers_lock);
387 if (!list_empty(&lcw->lcw_list))
388 list_del_init(&lcw->lcw_list);
389 spin_unlock_bh(&lcw_pending_timers_lock);
391 lcw_update_time(lcw, "disabled");
392 lcw->lcw_state = LC_WATCHDOG_DISABLED;
396 EXPORT_SYMBOL(lc_watchdog_disable);
398 void lc_watchdog_delete(struct lc_watchdog *lcw)
401 LASSERT(lcw != NULL);
403 cfs_timer_disarm(&lcw->lcw_timer);
405 lcw_update_time(lcw, "deleted");
407 spin_lock_bh(&lcw_pending_timers_lock);
408 if (!list_empty(&lcw->lcw_list))
409 list_del_init(&lcw->lcw_list);
410 spin_unlock_bh(&lcw_pending_timers_lock);
412 down(&lcw_refcount_sem);
413 if (--lcw_refcount == 0)
415 up(&lcw_refcount_sem);
417 LIBCFS_FREE(lcw, sizeof(*lcw));
421 EXPORT_SYMBOL(lc_watchdog_delete);
424 * Provided watchdog handlers
427 void lc_watchdog_dumplog(pid_t pid, void *data)
429 libcfs_debug_dumplog_internal((void *)((long_ptr_t)pid));
431 EXPORT_SYMBOL(lc_watchdog_dumplog);
433 #else /* !defined(WITH_WATCHDOG) */
435 struct lc_watchdog *lc_watchdog_add(int timeout,
436 void (*callback)(pid_t pid, void *),
439 static struct lc_watchdog watchdog;
442 EXPORT_SYMBOL(lc_watchdog_add);
444 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
447 EXPORT_SYMBOL(lc_watchdog_touch);
449 void lc_watchdog_disable(struct lc_watchdog *lcw)
452 EXPORT_SYMBOL(lc_watchdog_disable);
454 void lc_watchdog_delete(struct lc_watchdog *lcw)
457 EXPORT_SYMBOL(lc_watchdog_delete);