Whamcloud - gitweb
93802acfd9001c2dca59fb835e69215ff2a0ec49
[fs/lustre-release.git] / libcfs / libcfs / watchdog.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/watchdog.c
37  *
38  * Author: Jacob Berkman <jacob@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_LNET
42
43 #include <libcfs/libcfs.h>
44 #include "tracefile.h"
45
46 struct lc_watchdog {
47         spinlock_t  lcw_lock;     /* check or change lcw_list */
48         int             lcw_refcount; /* must hold lcw_pending_timers_lock */
49         struct timer_list     lcw_timer;    /* kernel timer */
50         cfs_list_t      lcw_list;     /* chain on pending list */
51         cfs_time_t      lcw_last_touched; /* last touched stamp */
52         struct task_struct     *lcw_task;     /* owner task */
53         void          (*lcw_callback)(pid_t, void *);
54         void           *lcw_data;
55
56         pid_t           lcw_pid;
57
58         enum {
59                 LC_WATCHDOG_DISABLED,
60                 LC_WATCHDOG_ENABLED,
61                 LC_WATCHDOG_EXPIRED
62         } lcw_state;
63 };
64
65 #ifdef WITH_WATCHDOG
66 /*
67  * The dispatcher will complete lcw_start_completion when it starts,
68  * and lcw_stop_completion when it exits.
69  * Wake lcw_event_waitq to signal timer callback dispatches.
70  */
71 static struct completion lcw_start_completion;
72 static struct completion  lcw_stop_completion;
73 static wait_queue_head_t lcw_event_waitq;
74
75 /*
76  * Set this and wake lcw_event_waitq to stop the dispatcher.
77  */
78 enum {
79         LCW_FLAG_STOP = 0
80 };
81 static unsigned long lcw_flags = 0;
82
83 /*
84  * Number of outstanding watchdogs.
85  * When it hits 1, we start the dispatcher.
86  * When it hits 0, we stop the dispatcher.
87  */
88 static __u32         lcw_refcount = 0;
89 static DEFINE_MUTEX(lcw_refcount_mutex);
90
91 /*
92  * List of timers that have fired that need their callbacks run by the
93  * dispatcher.
94  */
95 /* BH lock! */
96 static DEFINE_SPINLOCK(lcw_pending_timers_lock);
97 static cfs_list_t lcw_pending_timers = CFS_LIST_HEAD_INIT(lcw_pending_timers);
98
99 /* Last time a watchdog expired */
100 static cfs_time_t lcw_last_watchdog_time;
101 static int lcw_recent_watchdog_count;
102
103 static void
104 lcw_dump(struct lc_watchdog *lcw)
105 {
106         ENTRY;
107         rcu_read_lock();
108        if (lcw->lcw_task == NULL) {
109                 LCONSOLE_WARN("Process " LPPID " was not found in the task "
110                               "list; watchdog callback may be incomplete\n",
111                               (int)lcw->lcw_pid);
112         } else {
113                 libcfs_debug_dumpstack(lcw->lcw_task);
114         }
115
116         rcu_read_unlock();
117         EXIT;
118 }
119
120 static void lcw_cb(ulong_ptr_t data)
121 {
122         struct lc_watchdog *lcw = (struct lc_watchdog *)data;
123         ENTRY;
124
125         if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
126                 EXIT;
127                 return;
128         }
129
130         lcw->lcw_state = LC_WATCHDOG_EXPIRED;
131
132         spin_lock_bh(&lcw->lcw_lock);
133         LASSERT(cfs_list_empty(&lcw->lcw_list));
134
135         spin_lock_bh(&lcw_pending_timers_lock);
136         lcw->lcw_refcount++; /* +1 for pending list */
137         cfs_list_add(&lcw->lcw_list, &lcw_pending_timers);
138         wake_up(&lcw_event_waitq);
139
140         spin_unlock_bh(&lcw_pending_timers_lock);
141         spin_unlock_bh(&lcw->lcw_lock);
142         EXIT;
143 }
144
145 static int is_watchdog_fired(void)
146 {
147         int rc;
148
149         if (test_bit(LCW_FLAG_STOP, &lcw_flags))
150                 return 1;
151
152         spin_lock_bh(&lcw_pending_timers_lock);
153         rc = !cfs_list_empty(&lcw_pending_timers);
154         spin_unlock_bh(&lcw_pending_timers_lock);
155         return rc;
156 }
157
158 static void lcw_dump_stack(struct lc_watchdog *lcw)
159 {
160         cfs_time_t      current_time;
161         cfs_duration_t  delta_time;
162         struct timeval  timediff;
163
164         current_time = cfs_time_current();
165         delta_time = cfs_time_sub(current_time, lcw->lcw_last_touched);
166         cfs_duration_usec(delta_time, &timediff);
167
168         /*
169          * Check to see if we should throttle the watchdog timer to avoid
170          * too many dumps going to the console thus triggering an NMI.
171          */
172         delta_time = cfs_duration_sec(cfs_time_sub(current_time,
173                                                    lcw_last_watchdog_time));
174
175         if (delta_time < libcfs_watchdog_ratelimit &&
176             lcw_recent_watchdog_count > 3) {
177                 LCONSOLE_WARN("Service thread pid %u was inactive for "
178                               "%lu.%.02lus. Watchdog stack traces are limited "
179                               "to 3 per %d seconds, skipping this one.\n",
180                               (int)lcw->lcw_pid,
181                               timediff.tv_sec,
182                               timediff.tv_usec / 10000,
183                               libcfs_watchdog_ratelimit);
184         } else {
185                 if (delta_time < libcfs_watchdog_ratelimit) {
186                         lcw_recent_watchdog_count++;
187                 } else {
188                         memcpy(&lcw_last_watchdog_time, &current_time,
189                                sizeof(current_time));
190                         lcw_recent_watchdog_count = 0;
191                 }
192
193                 LCONSOLE_WARN("Service thread pid %u was inactive for "
194                               "%lu.%.02lus. The thread might be hung, or it "
195                               "might only be slow and will resume later. "
196                               "Dumping the stack trace for debugging purposes:"
197                               "\n",
198                               (int)lcw->lcw_pid,
199                               timediff.tv_sec,
200                               timediff.tv_usec / 10000);
201                 lcw_dump(lcw);
202         }
203 }
204
205 static int lcw_dispatch_main(void *data)
206 {
207         int                 rc = 0;
208         struct lc_watchdog *lcw;
209         CFS_LIST_HEAD      (zombies);
210
211         ENTRY;
212
213         complete(&lcw_start_completion);
214
215         while (1) {
216                 int dumplog = 1;
217
218                 rc = wait_event_interruptible(lcw_event_waitq,
219                                               is_watchdog_fired());
220                 CDEBUG(D_INFO, "Watchdog got woken up...\n");
221                 if (test_bit(LCW_FLAG_STOP, &lcw_flags)) {
222                         CDEBUG(D_INFO, "LCW_FLAG_STOP set, shutting down...\n");
223
224                         spin_lock_bh(&lcw_pending_timers_lock);
225                         rc = !cfs_list_empty(&lcw_pending_timers);
226                         spin_unlock_bh(&lcw_pending_timers_lock);
227                         if (rc) {
228                                 CERROR("pending timers list was not empty at "
229                                        "time of watchdog dispatch shutdown\n");
230                         }
231                         break;
232                 }
233
234                 spin_lock_bh(&lcw_pending_timers_lock);
235                 while (!cfs_list_empty(&lcw_pending_timers)) {
236                         int is_dumplog;
237
238                         lcw = cfs_list_entry(lcw_pending_timers.next,
239                                              struct lc_watchdog, lcw_list);
240                         /* +1 ref for callback to make sure lwc wouldn't be
241                          * deleted after releasing lcw_pending_timers_lock */
242                         lcw->lcw_refcount++;
243                         spin_unlock_bh(&lcw_pending_timers_lock);
244
245                         /* lock ordering */
246                         spin_lock_bh(&lcw->lcw_lock);
247                         spin_lock_bh(&lcw_pending_timers_lock);
248
249                         if (cfs_list_empty(&lcw->lcw_list)) {
250                                 /* already removed from pending list */
251                                 lcw->lcw_refcount--; /* -1 ref for callback */
252                                 if (lcw->lcw_refcount == 0)
253                                         cfs_list_add(&lcw->lcw_list, &zombies);
254                                 spin_unlock_bh(&lcw->lcw_lock);
255                                 /* still hold lcw_pending_timers_lock */
256                                 continue;
257                         }
258
259                         cfs_list_del_init(&lcw->lcw_list);
260                         lcw->lcw_refcount--; /* -1 ref for pending list */
261
262                         spin_unlock_bh(&lcw_pending_timers_lock);
263                         spin_unlock_bh(&lcw->lcw_lock);
264
265                         CDEBUG(D_INFO, "found lcw for pid " LPPID "\n",
266                                lcw->lcw_pid);
267                         lcw_dump_stack(lcw);
268
269                         is_dumplog = lcw->lcw_callback == lc_watchdog_dumplog;
270                         if (lcw->lcw_state != LC_WATCHDOG_DISABLED &&
271                             (dumplog || !is_dumplog)) {
272                                 lcw->lcw_callback(lcw->lcw_pid, lcw->lcw_data);
273                                 if (dumplog && is_dumplog)
274                                         dumplog = 0;
275                         }
276
277                         spin_lock_bh(&lcw_pending_timers_lock);
278                         lcw->lcw_refcount--; /* -1 ref for callback */
279                         if (lcw->lcw_refcount == 0)
280                                 cfs_list_add(&lcw->lcw_list, &zombies);
281                 }
282                 spin_unlock_bh(&lcw_pending_timers_lock);
283
284                 while (!cfs_list_empty(&zombies)) {
285                         lcw = cfs_list_entry(lcw_pending_timers.next,
286                                          struct lc_watchdog, lcw_list);
287                         cfs_list_del(&lcw->lcw_list);
288                         LIBCFS_FREE(lcw, sizeof(*lcw));
289                 }
290         }
291
292         complete(&lcw_stop_completion);
293
294         RETURN(rc);
295 }
296
297 static void lcw_dispatch_start(void)
298 {
299         struct task_struct *task;
300
301         ENTRY;
302         LASSERT(lcw_refcount == 1);
303
304         init_completion(&lcw_stop_completion);
305         init_completion(&lcw_start_completion);
306         init_waitqueue_head(&lcw_event_waitq);
307
308         CDEBUG(D_INFO, "starting dispatch thread\n");
309         task = kthread_run(lcw_dispatch_main, NULL, "lc_watchdogd");
310         if (IS_ERR(task)) {
311                 CERROR("error spawning watchdog dispatch thread: %ld\n",
312                         PTR_ERR(task));
313                 EXIT;
314                 return;
315         }
316         wait_for_completion(&lcw_start_completion);
317         CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
318
319         EXIT;
320 }
321
322 static void lcw_dispatch_stop(void)
323 {
324         ENTRY;
325         LASSERT(lcw_refcount == 0);
326
327         CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
328
329         set_bit(LCW_FLAG_STOP, &lcw_flags);
330         wake_up(&lcw_event_waitq);
331
332         wait_for_completion(&lcw_stop_completion);
333
334         CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
335
336         EXIT;
337 }
338
339 struct lc_watchdog *lc_watchdog_add(int timeout,
340                                     void (*callback)(pid_t, void *),
341                                     void *data)
342 {
343         struct lc_watchdog *lcw = NULL;
344         ENTRY;
345
346         LIBCFS_ALLOC(lcw, sizeof(*lcw));
347         if (lcw == NULL) {
348                 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
349                 RETURN(ERR_PTR(-ENOMEM));
350         }
351
352         spin_lock_init(&lcw->lcw_lock);
353         lcw->lcw_refcount = 1; /* refcount for owner */
354         lcw->lcw_task     = current;
355         lcw->lcw_pid      = current_pid();
356         lcw->lcw_callback = (callback != NULL) ? callback : lc_watchdog_dumplog;
357         lcw->lcw_data     = data;
358         lcw->lcw_state    = LC_WATCHDOG_DISABLED;
359
360         CFS_INIT_LIST_HEAD(&lcw->lcw_list);
361         cfs_timer_init(&lcw->lcw_timer, lcw_cb, lcw);
362
363         mutex_lock(&lcw_refcount_mutex);
364         if (++lcw_refcount == 1)
365                 lcw_dispatch_start();
366         mutex_unlock(&lcw_refcount_mutex);
367
368         /* Keep this working in case we enable them by default */
369         if (lcw->lcw_state == LC_WATCHDOG_ENABLED) {
370                 lcw->lcw_last_touched = cfs_time_current();
371                 cfs_timer_arm(&lcw->lcw_timer, cfs_time_seconds(timeout) +
372                               cfs_time_current());
373         }
374
375         RETURN(lcw);
376 }
377 EXPORT_SYMBOL(lc_watchdog_add);
378
379 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
380 {
381         cfs_time_t newtime = cfs_time_current();;
382
383         if (lcw->lcw_state == LC_WATCHDOG_EXPIRED) {
384                 struct timeval timediff;
385                 cfs_time_t delta_time = cfs_time_sub(newtime,
386                                                      lcw->lcw_last_touched);
387                 cfs_duration_usec(delta_time, &timediff);
388
389                 LCONSOLE_WARN("Service thread pid %u %s after %lu.%.02lus. "
390                               "This indicates the system was overloaded (too "
391                               "many service threads, or there were not enough "
392                               "hardware resources).\n",
393                               lcw->lcw_pid,
394                               message,
395                               timediff.tv_sec,
396                               timediff.tv_usec / 10000);
397         }
398         lcw->lcw_last_touched = newtime;
399 }
400
401 static void lc_watchdog_del_pending(struct lc_watchdog *lcw)
402 {
403         spin_lock_bh(&lcw->lcw_lock);
404         if (unlikely(!cfs_list_empty(&lcw->lcw_list))) {
405                 spin_lock_bh(&lcw_pending_timers_lock);
406                 cfs_list_del_init(&lcw->lcw_list);
407                 lcw->lcw_refcount--; /* -1 ref for pending list */
408                 spin_unlock_bh(&lcw_pending_timers_lock);
409         }
410
411         spin_unlock_bh(&lcw->lcw_lock);
412 }
413
414 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
415 {
416         ENTRY;
417         LASSERT(lcw != NULL);
418
419         lc_watchdog_del_pending(lcw);
420
421         lcw_update_time(lcw, "resumed");
422         lcw->lcw_state = LC_WATCHDOG_ENABLED;
423
424         cfs_timer_arm(&lcw->lcw_timer, cfs_time_current() +
425                       cfs_time_seconds(timeout));
426
427         EXIT;
428 }
429 EXPORT_SYMBOL(lc_watchdog_touch);
430
431 void lc_watchdog_disable(struct lc_watchdog *lcw)
432 {
433         ENTRY;
434         LASSERT(lcw != NULL);
435
436         lc_watchdog_del_pending(lcw);
437
438         lcw_update_time(lcw, "completed");
439         lcw->lcw_state = LC_WATCHDOG_DISABLED;
440
441         EXIT;
442 }
443 EXPORT_SYMBOL(lc_watchdog_disable);
444
445 void lc_watchdog_delete(struct lc_watchdog *lcw)
446 {
447         int dead;
448
449         ENTRY;
450         LASSERT(lcw != NULL);
451
452         cfs_timer_disarm(&lcw->lcw_timer);
453
454         lcw_update_time(lcw, "stopped");
455
456         spin_lock_bh(&lcw->lcw_lock);
457         spin_lock_bh(&lcw_pending_timers_lock);
458         if (unlikely(!cfs_list_empty(&lcw->lcw_list))) {
459                 cfs_list_del_init(&lcw->lcw_list);
460                 lcw->lcw_refcount--; /* -1 ref for pending list */
461         }
462
463         lcw->lcw_refcount--; /* -1 ref for owner */
464         dead = lcw->lcw_refcount == 0;
465         spin_unlock_bh(&lcw_pending_timers_lock);
466         spin_unlock_bh(&lcw->lcw_lock);
467
468         if (dead)
469                 LIBCFS_FREE(lcw, sizeof(*lcw));
470
471         mutex_lock(&lcw_refcount_mutex);
472         if (--lcw_refcount == 0)
473                 lcw_dispatch_stop();
474         mutex_unlock(&lcw_refcount_mutex);
475
476         EXIT;
477 }
478 EXPORT_SYMBOL(lc_watchdog_delete);
479
480 /*
481  * Provided watchdog handlers
482  */
483
484 void lc_watchdog_dumplog(pid_t pid, void *data)
485 {
486         libcfs_debug_dumplog_internal((void *)((long_ptr_t)pid));
487 }
488 EXPORT_SYMBOL(lc_watchdog_dumplog);
489
490 #else   /* !defined(WITH_WATCHDOG) */
491
492 struct lc_watchdog *lc_watchdog_add(int timeout,
493                                     void (*callback)(pid_t pid, void *),
494                                     void *data)
495 {
496         static struct lc_watchdog      watchdog;
497         return &watchdog;
498 }
499 EXPORT_SYMBOL(lc_watchdog_add);
500
501 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
502 {
503 }
504 EXPORT_SYMBOL(lc_watchdog_touch);
505
506 void lc_watchdog_disable(struct lc_watchdog *lcw)
507 {
508 }
509 EXPORT_SYMBOL(lc_watchdog_disable);
510
511 void lc_watchdog_delete(struct lc_watchdog *lcw)
512 {
513 }
514 EXPORT_SYMBOL(lc_watchdog_delete);
515
516 #endif