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