Whamcloud - gitweb
95a0177b4d580e2eabfea1b94e8e28a326aa285c
[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, 2014, 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 <linux/kthread.h>
44 #include <libcfs/libcfs.h>
45 #include "tracefile.h"
46
47 struct lc_watchdog {
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 *);
55         void                    *lcw_data;
56
57         pid_t                   lcw_pid;
58
59         enum {
60                 LC_WATCHDOG_DISABLED,
61                 LC_WATCHDOG_ENABLED,
62                 LC_WATCHDOG_EXPIRED
63         } lcw_state;
64 };
65
66 #ifdef WITH_WATCHDOG
67 /*
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.
71  */
72 static struct completion lcw_start_completion;
73 static struct completion  lcw_stop_completion;
74 static wait_queue_head_t lcw_event_waitq;
75
76 /*
77  * Set this and wake lcw_event_waitq to stop the dispatcher.
78  */
79 enum {
80         LCW_FLAG_STOP = 0
81 };
82 static unsigned long lcw_flags = 0;
83
84 /*
85  * Number of outstanding watchdogs.
86  * When it hits 1, we start the dispatcher.
87  * When it hits 0, we stop the dispatcher.
88  */
89 static __u32         lcw_refcount = 0;
90 static DEFINE_MUTEX(lcw_refcount_mutex);
91
92 /*
93  * List of timers that have fired that need their callbacks run by the
94  * dispatcher.
95  */
96 /* BH lock! */
97 static DEFINE_SPINLOCK(lcw_pending_timers_lock);
98 static struct list_head lcw_pending_timers = LIST_HEAD_INIT(lcw_pending_timers);
99
100 /* Last time a watchdog expired */
101 static cfs_time_t lcw_last_watchdog_time;
102 static int lcw_recent_watchdog_count;
103
104 static void
105 lcw_dump(struct lc_watchdog *lcw)
106 {
107         ENTRY;
108         rcu_read_lock();
109        if (lcw->lcw_task == NULL) {
110                 LCONSOLE_WARN("Process %d was not found in the task "
111                               "list; watchdog callback may be incomplete\n",
112                               (int)lcw->lcw_pid);
113         } else {
114                 libcfs_debug_dumpstack(lcw->lcw_task);
115         }
116
117         rcu_read_unlock();
118         EXIT;
119 }
120
121 static void lcw_cb(uintptr_t data)
122 {
123         struct lc_watchdog *lcw = (struct lc_watchdog *)data;
124         ENTRY;
125
126         if (lcw->lcw_state != LC_WATCHDOG_ENABLED) {
127                 EXIT;
128                 return;
129         }
130
131         lcw->lcw_state = LC_WATCHDOG_EXPIRED;
132
133         spin_lock_bh(&lcw->lcw_lock);
134         LASSERT(list_empty(&lcw->lcw_list));
135
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);
140
141         spin_unlock_bh(&lcw_pending_timers_lock);
142         spin_unlock_bh(&lcw->lcw_lock);
143         EXIT;
144 }
145
146 static int is_watchdog_fired(void)
147 {
148         int rc;
149
150         if (test_bit(LCW_FLAG_STOP, &lcw_flags))
151                 return 1;
152
153         spin_lock_bh(&lcw_pending_timers_lock);
154         rc = !list_empty(&lcw_pending_timers);
155         spin_unlock_bh(&lcw_pending_timers_lock);
156         return rc;
157 }
158
159 static void lcw_dump_stack(struct lc_watchdog *lcw)
160 {
161         cfs_time_t      current_time;
162         cfs_duration_t  delta_time;
163         struct timeval  timediff;
164
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);
168
169         /*
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.
172          */
173         delta_time = cfs_duration_sec(cfs_time_sub(current_time,
174                                                    lcw_last_watchdog_time));
175
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",
181                               (int)lcw->lcw_pid,
182                               timediff.tv_sec,
183                               timediff.tv_usec / 10000,
184                               libcfs_watchdog_ratelimit);
185         } else {
186                 if (delta_time < libcfs_watchdog_ratelimit) {
187                         lcw_recent_watchdog_count++;
188                 } else {
189                         memcpy(&lcw_last_watchdog_time, &current_time,
190                                sizeof(current_time));
191                         lcw_recent_watchdog_count = 0;
192                 }
193
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:"
198                               "\n",
199                               (int)lcw->lcw_pid,
200                               timediff.tv_sec,
201                               timediff.tv_usec / 10000);
202                 lcw_dump(lcw);
203         }
204 }
205
206 /*
207  * Provided watchdog handlers
208  */
209
210 static void lc_watchdog_dumplog(pid_t pid, void *data)
211 {
212         libcfs_debug_dumplog_internal((void *)((uintptr_t)pid));
213 }
214
215 static int lcw_dispatch_main(void *data)
216 {
217         int                 rc = 0;
218         struct lc_watchdog *lcw;
219         struct list_head zombies = LIST_HEAD_INIT(zombies);
220
221         ENTRY;
222
223         complete(&lcw_start_completion);
224
225         while (1) {
226                 int dumplog = 1;
227
228                 rc = wait_event_interruptible(lcw_event_waitq,
229                                               is_watchdog_fired());
230                 CDEBUG(D_INFO, "Watchdog got woken up...\n");
231                 if (test_bit(LCW_FLAG_STOP, &lcw_flags)) {
232                         CDEBUG(D_INFO, "LCW_FLAG_STOP set, shutting down...\n");
233
234                         spin_lock_bh(&lcw_pending_timers_lock);
235                         rc = !list_empty(&lcw_pending_timers);
236                         spin_unlock_bh(&lcw_pending_timers_lock);
237                         if (rc) {
238                                 CERROR("pending timers list was not empty at "
239                                        "time of watchdog dispatch shutdown\n");
240                         }
241                         break;
242                 }
243
244                 spin_lock_bh(&lcw_pending_timers_lock);
245                 while (!list_empty(&lcw_pending_timers)) {
246                         int is_dumplog;
247
248                         lcw = list_entry(lcw_pending_timers.next,
249                                          struct lc_watchdog, lcw_list);
250                         /* +1 ref for callback to make sure lwc wouldn't be
251                          * deleted after releasing lcw_pending_timers_lock */
252                         lcw->lcw_refcount++;
253                         spin_unlock_bh(&lcw_pending_timers_lock);
254
255                         /* lock ordering */
256                         spin_lock_bh(&lcw->lcw_lock);
257                         spin_lock_bh(&lcw_pending_timers_lock);
258
259                         if (list_empty(&lcw->lcw_list)) {
260                                 /* already removed from pending list */
261                                 lcw->lcw_refcount--; /* -1 ref for callback */
262                                 if (lcw->lcw_refcount == 0)
263                                         list_add(&lcw->lcw_list, &zombies);
264                                 spin_unlock_bh(&lcw->lcw_lock);
265                                 /* still hold lcw_pending_timers_lock */
266                                 continue;
267                         }
268
269                         list_del_init(&lcw->lcw_list);
270                         lcw->lcw_refcount--; /* -1 ref for pending list */
271
272                         spin_unlock_bh(&lcw_pending_timers_lock);
273                         spin_unlock_bh(&lcw->lcw_lock);
274
275                         CDEBUG(D_INFO, "found lcw for pid %d\n",
276                                lcw->lcw_pid);
277                         lcw_dump_stack(lcw);
278
279                         is_dumplog = lcw->lcw_callback == lc_watchdog_dumplog;
280                         if (lcw->lcw_state != LC_WATCHDOG_DISABLED &&
281                             (dumplog || !is_dumplog)) {
282                                 lcw->lcw_callback(lcw->lcw_pid, lcw->lcw_data);
283                                 if (dumplog && is_dumplog)
284                                         dumplog = 0;
285                         }
286
287                         spin_lock_bh(&lcw_pending_timers_lock);
288                         lcw->lcw_refcount--; /* -1 ref for callback */
289                         if (lcw->lcw_refcount == 0)
290                                 list_add(&lcw->lcw_list, &zombies);
291                 }
292                 spin_unlock_bh(&lcw_pending_timers_lock);
293
294                 while (!list_empty(&zombies)) {
295                         lcw = list_entry(zombies.next,
296                                              struct lc_watchdog, lcw_list);
297                         list_del_init(&lcw->lcw_list);
298                         LIBCFS_FREE(lcw, sizeof(*lcw));
299                 }
300         }
301
302         complete(&lcw_stop_completion);
303
304         RETURN(rc);
305 }
306
307 static void lcw_dispatch_start(void)
308 {
309         struct task_struct *task;
310
311         ENTRY;
312         LASSERT(lcw_refcount == 1);
313
314         init_completion(&lcw_stop_completion);
315         init_completion(&lcw_start_completion);
316         init_waitqueue_head(&lcw_event_waitq);
317
318         CDEBUG(D_INFO, "starting dispatch thread\n");
319         task = kthread_run(lcw_dispatch_main, NULL, "lc_watchdogd");
320         if (IS_ERR(task)) {
321                 CERROR("error spawning watchdog dispatch thread: %ld\n",
322                         PTR_ERR(task));
323                 EXIT;
324                 return;
325         }
326         wait_for_completion(&lcw_start_completion);
327         CDEBUG(D_INFO, "watchdog dispatcher initialization complete.\n");
328
329         EXIT;
330 }
331
332 static void lcw_dispatch_stop(void)
333 {
334         ENTRY;
335         LASSERT(lcw_refcount == 0);
336
337         CDEBUG(D_INFO, "trying to stop watchdog dispatcher.\n");
338
339         set_bit(LCW_FLAG_STOP, &lcw_flags);
340         wake_up(&lcw_event_waitq);
341
342         wait_for_completion(&lcw_stop_completion);
343
344         CDEBUG(D_INFO, "watchdog dispatcher has shut down.\n");
345
346         EXIT;
347 }
348
349 struct lc_watchdog *lc_watchdog_add(int timeout,
350                                     void (*callback)(pid_t, void *),
351                                     void *data)
352 {
353         struct lc_watchdog *lcw = NULL;
354         ENTRY;
355
356         LIBCFS_ALLOC(lcw, sizeof(*lcw));
357         if (lcw == NULL) {
358                 CDEBUG(D_INFO, "Could not allocate new lc_watchdog\n");
359                 RETURN(ERR_PTR(-ENOMEM));
360         }
361
362         spin_lock_init(&lcw->lcw_lock);
363         lcw->lcw_refcount = 1; /* refcount for owner */
364         lcw->lcw_task     = current;
365         lcw->lcw_pid      = current_pid();
366         lcw->lcw_callback = (callback != NULL) ? callback : lc_watchdog_dumplog;
367         lcw->lcw_data     = data;
368         lcw->lcw_state    = LC_WATCHDOG_DISABLED;
369
370         INIT_LIST_HEAD(&lcw->lcw_list);
371         cfs_timer_init(&lcw->lcw_timer, lcw_cb, lcw);
372
373         mutex_lock(&lcw_refcount_mutex);
374         if (++lcw_refcount == 1)
375                 lcw_dispatch_start();
376         mutex_unlock(&lcw_refcount_mutex);
377
378         /* Keep this working in case we enable them by default */
379         if (lcw->lcw_state == LC_WATCHDOG_ENABLED) {
380                 lcw->lcw_last_touched = cfs_time_current();
381                 cfs_timer_arm(&lcw->lcw_timer, cfs_time_seconds(timeout) +
382                               cfs_time_current());
383         }
384
385         RETURN(lcw);
386 }
387 EXPORT_SYMBOL(lc_watchdog_add);
388
389 static void lcw_update_time(struct lc_watchdog *lcw, const char *message)
390 {
391         cfs_time_t newtime = cfs_time_current();
392
393         if (lcw->lcw_state == LC_WATCHDOG_EXPIRED) {
394                 struct timeval timediff;
395                 cfs_time_t delta_time = cfs_time_sub(newtime,
396                                                      lcw->lcw_last_touched);
397                 cfs_duration_usec(delta_time, &timediff);
398
399                 LCONSOLE_WARN("Service thread pid %u %s after %lu.%.02lus. "
400                               "This indicates the system was overloaded (too "
401                               "many service threads, or there were not enough "
402                               "hardware resources).\n",
403                               lcw->lcw_pid,
404                               message,
405                               timediff.tv_sec,
406                               timediff.tv_usec / 10000);
407         }
408         lcw->lcw_last_touched = newtime;
409 }
410
411 static void lc_watchdog_del_pending(struct lc_watchdog *lcw)
412 {
413         spin_lock_bh(&lcw->lcw_lock);
414         if (unlikely(!list_empty(&lcw->lcw_list))) {
415                 spin_lock_bh(&lcw_pending_timers_lock);
416                 list_del_init(&lcw->lcw_list);
417                 lcw->lcw_refcount--; /* -1 ref for pending list */
418                 spin_unlock_bh(&lcw_pending_timers_lock);
419         }
420
421         spin_unlock_bh(&lcw->lcw_lock);
422 }
423
424 void lc_watchdog_touch(struct lc_watchdog *lcw, int timeout)
425 {
426         ENTRY;
427         LASSERT(lcw != NULL);
428
429         lc_watchdog_del_pending(lcw);
430
431         lcw_update_time(lcw, "resumed");
432
433         cfs_timer_arm(&lcw->lcw_timer, cfs_time_current() +
434                       cfs_time_seconds(timeout));
435         lcw->lcw_state = LC_WATCHDOG_ENABLED;
436
437         EXIT;
438 }
439 EXPORT_SYMBOL(lc_watchdog_touch);
440
441 void lc_watchdog_disable(struct lc_watchdog *lcw)
442 {
443         ENTRY;
444         LASSERT(lcw != NULL);
445
446         lc_watchdog_del_pending(lcw);
447
448         lcw_update_time(lcw, "completed");
449         lcw->lcw_state = LC_WATCHDOG_DISABLED;
450
451         EXIT;
452 }
453 EXPORT_SYMBOL(lc_watchdog_disable);
454
455 void lc_watchdog_delete(struct lc_watchdog *lcw)
456 {
457         int dead;
458
459         ENTRY;
460         LASSERT(lcw != NULL);
461
462         cfs_timer_disarm(&lcw->lcw_timer);
463
464         lcw_update_time(lcw, "stopped");
465
466         spin_lock_bh(&lcw->lcw_lock);
467         spin_lock_bh(&lcw_pending_timers_lock);
468         if (unlikely(!list_empty(&lcw->lcw_list))) {
469                 list_del_init(&lcw->lcw_list);
470                 lcw->lcw_refcount--; /* -1 ref for pending list */
471         }
472
473         lcw->lcw_refcount--; /* -1 ref for owner */
474         dead = lcw->lcw_refcount == 0;
475         spin_unlock_bh(&lcw_pending_timers_lock);
476         spin_unlock_bh(&lcw->lcw_lock);
477
478         if (dead)
479                 LIBCFS_FREE(lcw, sizeof(*lcw));
480
481         mutex_lock(&lcw_refcount_mutex);
482         if (--lcw_refcount == 0)
483                 lcw_dispatch_stop();
484         mutex_unlock(&lcw_refcount_mutex);
485
486         EXIT;
487 }
488 EXPORT_SYMBOL(lc_watchdog_delete);
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