Whamcloud - gitweb
0981a2c86d0b3f5b5752bc75c8376e9520b03999
[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         cfs_timer_t     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         cfs_task_t     *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 cfs_waitq_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 #if defined(HAVE_TASKLIST_LOCK)
108         read_lock(&tasklist_lock);
109 #else
110         rcu_read_lock();
111 #endif
112        if (lcw->lcw_task == NULL) {
113                 LCONSOLE_WARN("Process " LPPID " was not found in the task "
114                               "list; watchdog callback may be incomplete\n",
115                               (int)lcw->lcw_pid);
116         } else {
117                 libcfs_debug_dumpstack(lcw->lcw_task);
118         }
119
120 #if defined(HAVE_TASKLIST_LOCK)
121         read_unlock(&tasklist_lock);
122 #else
123         rcu_read_unlock();
124 #endif
125         EXIT;
126 }
127
128 static void lcw_cb(ulong_ptr_t data)
129 {
130         struct lc_watchdog *lcw = (struct lc_watchdog *)data;
131         ENTRY;
132
133         if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
134                 EXIT;
135                 return;
136         }
137
138         lcw->lcw_state = LC_WATCHDOG_EXPIRED;
139
140         spin_lock_bh(&lcw->lcw_lock);
141         LASSERT(cfs_list_empty(&lcw->lcw_list));
142
143         spin_lock_bh(&lcw_pending_timers_lock);
144         lcw->lcw_refcount++; /* +1 for pending list */
145         cfs_list_add(&lcw->lcw_list, &lcw_pending_timers);
146         cfs_waitq_signal(&lcw_event_waitq);
147
148         spin_unlock_bh(&lcw_pending_timers_lock);
149         spin_unlock_bh(&lcw->lcw_lock);
150         EXIT;
151 }
152
153 static int is_watchdog_fired(void)
154 {
155         int rc;
156
157         if (test_bit(LCW_FLAG_STOP, &lcw_flags))
158                 return 1;
159
160         spin_lock_bh(&lcw_pending_timers_lock);
161         rc = !cfs_list_empty(&lcw_pending_timers);
162         spin_unlock_bh(&lcw_pending_timers_lock);
163         return rc;
164 }
165
166 static void lcw_dump_stack(struct lc_watchdog *lcw)
167 {
168         cfs_time_t      current_time;
169         cfs_duration_t  delta_time;
170         struct timeval  timediff;
171
172         current_time = cfs_time_current();
173         delta_time = cfs_time_sub(current_time, lcw->lcw_last_touched);
174         cfs_duration_usec(delta_time, &timediff);
175
176         /*
177          * Check to see if we should throttle the watchdog timer to avoid
178          * too many dumps going to the console thus triggering an NMI.
179          */
180         delta_time = cfs_duration_sec(cfs_time_sub(current_time,
181                                                    lcw_last_watchdog_time));
182
183         if (delta_time < libcfs_watchdog_ratelimit &&
184             lcw_recent_watchdog_count > 3) {
185                 LCONSOLE_WARN("Service thread pid %u was inactive for "
186                               "%lu.%.02lus. Watchdog stack traces are limited "
187                               "to 3 per %d seconds, skipping this one.\n",
188                               (int)lcw->lcw_pid,
189                               timediff.tv_sec,
190                               timediff.tv_usec / 10000,
191                               libcfs_watchdog_ratelimit);
192         } else {
193                 if (delta_time < libcfs_watchdog_ratelimit) {
194                         lcw_recent_watchdog_count++;
195                 } else {
196                         memcpy(&lcw_last_watchdog_time, &current_time,
197                                sizeof(current_time));
198                         lcw_recent_watchdog_count = 0;
199                 }
200
201                 LCONSOLE_WARN("Service thread pid %u was inactive for "
202                               "%lu.%.02lus. The thread might be hung, or it "
203                               "might only be slow and will resume later. "
204                               "Dumping the stack trace for debugging purposes:"
205                               "\n",
206                               (int)lcw->lcw_pid,
207                               timediff.tv_sec,
208                               timediff.tv_usec / 10000);
209                 lcw_dump(lcw);
210         }
211 }
212
213 static int lcw_dispatch_main(void *data)
214 {
215         int                 rc = 0;
216         struct lc_watchdog *lcw;
217         CFS_LIST_HEAD      (zombies);
218
219         ENTRY;
220
221         complete(&lcw_start_completion);
222
223         while (1) {
224                 int dumplog = 1;
225
226                 cfs_wait_event_interruptible(lcw_event_waitq,
227                                              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 set, shutting down...\n");
231
232                         spin_lock_bh(&lcw_pending_timers_lock);
233                         rc = !cfs_list_empty(&lcw_pending_timers);
234                         spin_unlock_bh(&lcw_pending_timers_lock);
235                         if (rc) {
236                                 CERROR("pending timers list was not empty at "
237                                        "time of watchdog dispatch shutdown\n");
238                         }
239                         break;
240                 }
241
242                 spin_lock_bh(&lcw_pending_timers_lock);
243                 while (!cfs_list_empty(&lcw_pending_timers)) {
244                         int is_dumplog;
245
246                         lcw = cfs_list_entry(lcw_pending_timers.next,
247                                              struct lc_watchdog, lcw_list);
248                         /* +1 ref for callback to make sure lwc wouldn't be
249                          * deleted after releasing lcw_pending_timers_lock */
250                         lcw->lcw_refcount++;
251                         spin_unlock_bh(&lcw_pending_timers_lock);
252
253                         /* lock ordering */
254                         spin_lock_bh(&lcw->lcw_lock);
255                         spin_lock_bh(&lcw_pending_timers_lock);
256
257                         if (cfs_list_empty(&lcw->lcw_list)) {
258                                 /* already removed from pending list */
259                                 lcw->lcw_refcount--; /* -1 ref for callback */
260                                 if (lcw->lcw_refcount == 0)
261                                         cfs_list_add(&lcw->lcw_list, &zombies);
262                                 spin_unlock_bh(&lcw->lcw_lock);
263                                 /* still hold lcw_pending_timers_lock */
264                                 continue;
265                         }
266
267                         cfs_list_del_init(&lcw->lcw_list);
268                         lcw->lcw_refcount--; /* -1 ref for pending list */
269
270                         spin_unlock_bh(&lcw_pending_timers_lock);
271                         spin_unlock_bh(&lcw->lcw_lock);
272
273                         CDEBUG(D_INFO, "found lcw for pid " LPPID "\n",
274                                lcw->lcw_pid);
275                         lcw_dump_stack(lcw);
276
277                         is_dumplog = lcw->lcw_callback == lc_watchdog_dumplog;
278                         if (lcw->lcw_state != LC_WATCHDOG_DISABLED &&
279                             (dumplog || !is_dumplog)) {
280                                 lcw->lcw_callback(lcw->lcw_pid, lcw->lcw_data);
281                                 if (dumplog && is_dumplog)
282                                         dumplog = 0;
283                         }
284
285                         spin_lock_bh(&lcw_pending_timers_lock);
286                         lcw->lcw_refcount--; /* -1 ref for callback */
287                         if (lcw->lcw_refcount == 0)
288                                 cfs_list_add(&lcw->lcw_list, &zombies);
289                 }
290                 spin_unlock_bh(&lcw_pending_timers_lock);
291
292                 while (!cfs_list_empty(&zombies)) {
293                         lcw = cfs_list_entry(lcw_pending_timers.next,
294                                          struct lc_watchdog, lcw_list);
295                         cfs_list_del(&lcw->lcw_list);
296                         LIBCFS_FREE(lcw, sizeof(*lcw));
297                 }
298         }
299
300         complete(&lcw_stop_completion);
301
302         RETURN(rc);
303 }
304
305 static void lcw_dispatch_start(void)
306 {
307         cfs_task_t *task;
308
309         ENTRY;
310         LASSERT(lcw_refcount == 1);
311
312         init_completion(&lcw_stop_completion);
313         init_completion(&lcw_start_completion);
314         cfs_waitq_init(&lcw_event_waitq);
315
316         CDEBUG(D_INFO, "starting dispatch thread\n");
317         task = kthread_run(lcw_dispatch_main, NULL, "lc_watchdogd");
318         if (IS_ERR(task)) {
319                 CERROR("error spawning watchdog dispatch thread: %ld\n",
320                         PTR_ERR(task));
321                 EXIT;
322                 return;
323         }
324         wait_for_completion(&lcw_start_completion);
325         CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
326
327         EXIT;
328 }
329
330 static void lcw_dispatch_stop(void)
331 {
332         ENTRY;
333         LASSERT(lcw_refcount == 0);
334
335         CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
336
337         set_bit(LCW_FLAG_STOP, &lcw_flags);
338         cfs_waitq_signal(&lcw_event_waitq);
339
340         wait_for_completion(&lcw_stop_completion);
341
342         CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
343
344         EXIT;
345 }
346
347 struct lc_watchdog *lc_watchdog_add(int timeout,
348                                     void (*callback)(pid_t, void *),
349                                     void *data)
350 {
351         struct lc_watchdog *lcw = NULL;
352         ENTRY;
353
354         LIBCFS_ALLOC(lcw, sizeof(*lcw));
355         if (lcw == NULL) {
356                 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
357                 RETURN(ERR_PTR(-ENOMEM));
358         }
359
360         spin_lock_init(&lcw->lcw_lock);
361         lcw->lcw_refcount = 1; /* refcount for owner */
362         lcw->lcw_task     = cfs_current();
363         lcw->lcw_pid      = cfs_curproc_pid();
364         lcw->lcw_callback = (callback != NULL) ? callback : lc_watchdog_dumplog;
365         lcw->lcw_data     = data;
366         lcw->lcw_state    = LC_WATCHDOG_DISABLED;
367
368         CFS_INIT_LIST_HEAD(&lcw->lcw_list);
369         cfs_timer_init(&lcw->lcw_timer, lcw_cb, lcw);
370
371         mutex_lock(&lcw_refcount_mutex);
372         if (++lcw_refcount == 1)
373                 lcw_dispatch_start();
374         mutex_unlock(&lcw_refcount_mutex);
375
376         /* Keep this working in case we enable them by default */
377         if (lcw->lcw_state == LC_WATCHDOG_ENABLED) {
378                 lcw->lcw_last_touched = cfs_time_current();
379                 cfs_timer_arm(&lcw->lcw_timer, cfs_time_seconds(timeout) +
380                               cfs_time_current());
381         }
382
383         RETURN(lcw);
384 }
385 EXPORT_SYMBOL(lc_watchdog_add);
386
387 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
388 {
389         cfs_time_t newtime = cfs_time_current();;
390
391         if (lcw->lcw_state == LC_WATCHDOG_EXPIRED) {
392                 struct timeval timediff;
393                 cfs_time_t delta_time = cfs_time_sub(newtime,
394                                                      lcw->lcw_last_touched);
395                 cfs_duration_usec(delta_time, &timediff);
396
397                 LCONSOLE_WARN("Service thread pid %u %s after %lu.%.02lus. "
398                               "This indicates the system was overloaded (too "
399                               "many service threads, or there were not enough "
400                               "hardware resources).\n",
401                               lcw->lcw_pid,
402                               message,
403                               timediff.tv_sec,
404                               timediff.tv_usec / 10000);
405         }
406         lcw->lcw_last_touched = newtime;
407 }
408
409 static void lc_watchdog_del_pending(struct lc_watchdog *lcw)
410 {
411         spin_lock_bh(&lcw->lcw_lock);
412         if (unlikely(!cfs_list_empty(&lcw->lcw_list))) {
413                 spin_lock_bh(&lcw_pending_timers_lock);
414                 cfs_list_del_init(&lcw->lcw_list);
415                 lcw->lcw_refcount--; /* -1 ref for pending list */
416                 spin_unlock_bh(&lcw_pending_timers_lock);
417         }
418
419         spin_unlock_bh(&lcw->lcw_lock);
420 }
421
422 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
423 {
424         ENTRY;
425         LASSERT(lcw != NULL);
426
427         lc_watchdog_del_pending(lcw);
428
429         lcw_update_time(lcw, "resumed");
430         lcw->lcw_state = LC_WATCHDOG_ENABLED;
431
432         cfs_timer_arm(&lcw->lcw_timer, cfs_time_current() +
433                       cfs_time_seconds(timeout));
434
435         EXIT;
436 }
437 EXPORT_SYMBOL(lc_watchdog_touch);
438
439 void lc_watchdog_disable(struct lc_watchdog *lcw)
440 {
441         ENTRY;
442         LASSERT(lcw != NULL);
443
444         lc_watchdog_del_pending(lcw);
445
446         lcw_update_time(lcw, "completed");
447         lcw->lcw_state = LC_WATCHDOG_DISABLED;
448
449         EXIT;
450 }
451 EXPORT_SYMBOL(lc_watchdog_disable);
452
453 void lc_watchdog_delete(struct lc_watchdog *lcw)
454 {
455         int dead;
456
457         ENTRY;
458         LASSERT(lcw != NULL);
459
460         cfs_timer_disarm(&lcw->lcw_timer);
461
462         lcw_update_time(lcw, "stopped");
463
464         spin_lock_bh(&lcw->lcw_lock);
465         spin_lock_bh(&lcw_pending_timers_lock);
466         if (unlikely(!cfs_list_empty(&lcw->lcw_list))) {
467                 cfs_list_del_init(&lcw->lcw_list);
468                 lcw->lcw_refcount--; /* -1 ref for pending list */
469         }
470
471         lcw->lcw_refcount--; /* -1 ref for owner */
472         dead = lcw->lcw_refcount == 0;
473         spin_unlock_bh(&lcw_pending_timers_lock);
474         spin_unlock_bh(&lcw->lcw_lock);
475
476         if (dead)
477                 LIBCFS_FREE(lcw, sizeof(*lcw));
478
479         mutex_lock(&lcw_refcount_mutex);
480         if (--lcw_refcount == 0)
481                 lcw_dispatch_stop();
482         mutex_unlock(&lcw_refcount_mutex);
483
484         EXIT;
485 }
486 EXPORT_SYMBOL(lc_watchdog_delete);
487
488 /*
489  * Provided watchdog handlers
490  */
491
492 void lc_watchdog_dumplog(pid_t pid, void *data)
493 {
494         libcfs_debug_dumplog_internal((void *)((long_ptr_t)pid));
495 }
496 EXPORT_SYMBOL(lc_watchdog_dumplog);
497
498 #else   /* !defined(WITH_WATCHDOG) */
499
500 struct lc_watchdog *lc_watchdog_add(int timeout,
501                                     void (*callback)(pid_t pid, void *),
502                                     void *data)
503 {
504         static struct lc_watchdog      watchdog;
505         return &watchdog;
506 }
507 EXPORT_SYMBOL(lc_watchdog_add);
508
509 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
510 {
511 }
512 EXPORT_SYMBOL(lc_watchdog_touch);
513
514 void lc_watchdog_disable(struct lc_watchdog *lcw)
515 {
516 }
517 EXPORT_SYMBOL(lc_watchdog_disable);
518
519 void lc_watchdog_delete(struct lc_watchdog *lcw)
520 {
521 }
522 EXPORT_SYMBOL(lc_watchdog_delete);
523
524 #endif