]> asedeno.scripts.mit.edu Git - linux.git/blob - kernel/watchdog.c
sched/headers: Prepare for new header dependencies before moving code to <linux/sched...
[linux.git] / kernel / watchdog.c
1 /*
2  * Detect hard and soft lockups on a system
3  *
4  * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
5  *
6  * Note: Most of this code is borrowed heavily from the original softlockup
7  * detector, so thanks to Ingo for the initial implementation.
8  * Some chunks also taken from the old x86-specific nmi watchdog code, thanks
9  * to those contributors as well.
10  */
11
12 #define pr_fmt(fmt) "NMI watchdog: " fmt
13
14 #include <linux/mm.h>
15 #include <linux/cpu.h>
16 #include <linux/nmi.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/sysctl.h>
20 #include <linux/smpboot.h>
21 #include <linux/sched/rt.h>
22 #include <linux/tick.h>
23 #include <linux/workqueue.h>
24 #include <linux/sched/clock.h>
25
26 #include <asm/irq_regs.h>
27 #include <linux/kvm_para.h>
28 #include <linux/kthread.h>
29
30 static DEFINE_MUTEX(watchdog_proc_mutex);
31
32 #if defined(CONFIG_HAVE_NMI_WATCHDOG) || defined(CONFIG_HARDLOCKUP_DETECTOR)
33 unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED|NMI_WATCHDOG_ENABLED;
34 #else
35 unsigned long __read_mostly watchdog_enabled = SOFT_WATCHDOG_ENABLED;
36 #endif
37 int __read_mostly nmi_watchdog_enabled;
38 int __read_mostly soft_watchdog_enabled;
39 int __read_mostly watchdog_user_enabled;
40 int __read_mostly watchdog_thresh = 10;
41
42 #ifdef CONFIG_SMP
43 int __read_mostly sysctl_softlockup_all_cpu_backtrace;
44 int __read_mostly sysctl_hardlockup_all_cpu_backtrace;
45 #endif
46 static struct cpumask watchdog_cpumask __read_mostly;
47 unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
48
49 /* Helper for online, unparked cpus. */
50 #define for_each_watchdog_cpu(cpu) \
51         for_each_cpu_and((cpu), cpu_online_mask, &watchdog_cpumask)
52
53 atomic_t watchdog_park_in_progress = ATOMIC_INIT(0);
54
55 /*
56  * The 'watchdog_running' variable is set to 1 when the watchdog threads
57  * are registered/started and is set to 0 when the watchdog threads are
58  * unregistered/stopped, so it is an indicator whether the threads exist.
59  */
60 static int __read_mostly watchdog_running;
61 /*
62  * If a subsystem has a need to deactivate the watchdog temporarily, it
63  * can use the suspend/resume interface to achieve this. The content of
64  * the 'watchdog_suspended' variable reflects this state. Existing threads
65  * are parked/unparked by the lockup_detector_{suspend|resume} functions
66  * (see comment blocks pertaining to those functions for further details).
67  *
68  * 'watchdog_suspended' also prevents threads from being registered/started
69  * or unregistered/stopped via parameters in /proc/sys/kernel, so the state
70  * of 'watchdog_running' cannot change while the watchdog is deactivated
71  * temporarily (see related code in 'proc' handlers).
72  */
73 static int __read_mostly watchdog_suspended;
74
75 static u64 __read_mostly sample_period;
76
77 static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts);
78 static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog);
79 static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer);
80 static DEFINE_PER_CPU(bool, softlockup_touch_sync);
81 static DEFINE_PER_CPU(bool, soft_watchdog_warn);
82 static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts);
83 static DEFINE_PER_CPU(unsigned long, soft_lockup_hrtimer_cnt);
84 static DEFINE_PER_CPU(struct task_struct *, softlockup_task_ptr_saved);
85 static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved);
86 static unsigned long soft_lockup_nmi_warn;
87
88 unsigned int __read_mostly softlockup_panic =
89                         CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
90
91 static int __init softlockup_panic_setup(char *str)
92 {
93         softlockup_panic = simple_strtoul(str, NULL, 0);
94
95         return 1;
96 }
97 __setup("softlockup_panic=", softlockup_panic_setup);
98
99 static int __init nowatchdog_setup(char *str)
100 {
101         watchdog_enabled = 0;
102         return 1;
103 }
104 __setup("nowatchdog", nowatchdog_setup);
105
106 static int __init nosoftlockup_setup(char *str)
107 {
108         watchdog_enabled &= ~SOFT_WATCHDOG_ENABLED;
109         return 1;
110 }
111 __setup("nosoftlockup", nosoftlockup_setup);
112
113 #ifdef CONFIG_SMP
114 static int __init softlockup_all_cpu_backtrace_setup(char *str)
115 {
116         sysctl_softlockup_all_cpu_backtrace =
117                 !!simple_strtol(str, NULL, 0);
118         return 1;
119 }
120 __setup("softlockup_all_cpu_backtrace=", softlockup_all_cpu_backtrace_setup);
121 static int __init hardlockup_all_cpu_backtrace_setup(char *str)
122 {
123         sysctl_hardlockup_all_cpu_backtrace =
124                 !!simple_strtol(str, NULL, 0);
125         return 1;
126 }
127 __setup("hardlockup_all_cpu_backtrace=", hardlockup_all_cpu_backtrace_setup);
128 #endif
129
130 /*
131  * Hard-lockup warnings should be triggered after just a few seconds. Soft-
132  * lockups can have false positives under extreme conditions. So we generally
133  * want a higher threshold for soft lockups than for hard lockups. So we couple
134  * the thresholds with a factor: we make the soft threshold twice the amount of
135  * time the hard threshold is.
136  */
137 static int get_softlockup_thresh(void)
138 {
139         return watchdog_thresh * 2;
140 }
141
142 /*
143  * Returns seconds, approximately.  We don't need nanosecond
144  * resolution, and we don't need to waste time with a big divide when
145  * 2^30ns == 1.074s.
146  */
147 static unsigned long get_timestamp(void)
148 {
149         return running_clock() >> 30LL;  /* 2^30 ~= 10^9 */
150 }
151
152 static void set_sample_period(void)
153 {
154         /*
155          * convert watchdog_thresh from seconds to ns
156          * the divide by 5 is to give hrtimer several chances (two
157          * or three with the current relation between the soft
158          * and hard thresholds) to increment before the
159          * hardlockup detector generates a warning
160          */
161         sample_period = get_softlockup_thresh() * ((u64)NSEC_PER_SEC / 5);
162 }
163
164 /* Commands for resetting the watchdog */
165 static void __touch_watchdog(void)
166 {
167         __this_cpu_write(watchdog_touch_ts, get_timestamp());
168 }
169
170 /**
171  * touch_softlockup_watchdog_sched - touch watchdog on scheduler stalls
172  *
173  * Call when the scheduler may have stalled for legitimate reasons
174  * preventing the watchdog task from executing - e.g. the scheduler
175  * entering idle state.  This should only be used for scheduler events.
176  * Use touch_softlockup_watchdog() for everything else.
177  */
178 void touch_softlockup_watchdog_sched(void)
179 {
180         /*
181          * Preemption can be enabled.  It doesn't matter which CPU's timestamp
182          * gets zeroed here, so use the raw_ operation.
183          */
184         raw_cpu_write(watchdog_touch_ts, 0);
185 }
186
187 void touch_softlockup_watchdog(void)
188 {
189         touch_softlockup_watchdog_sched();
190         wq_watchdog_touch(raw_smp_processor_id());
191 }
192 EXPORT_SYMBOL(touch_softlockup_watchdog);
193
194 void touch_all_softlockup_watchdogs(void)
195 {
196         int cpu;
197
198         /*
199          * this is done lockless
200          * do we care if a 0 races with a timestamp?
201          * all it means is the softlock check starts one cycle later
202          */
203         for_each_watchdog_cpu(cpu)
204                 per_cpu(watchdog_touch_ts, cpu) = 0;
205         wq_watchdog_touch(-1);
206 }
207
208 void touch_softlockup_watchdog_sync(void)
209 {
210         __this_cpu_write(softlockup_touch_sync, true);
211         __this_cpu_write(watchdog_touch_ts, 0);
212 }
213
214 /* watchdog detector functions */
215 bool is_hardlockup(void)
216 {
217         unsigned long hrint = __this_cpu_read(hrtimer_interrupts);
218
219         if (__this_cpu_read(hrtimer_interrupts_saved) == hrint)
220                 return true;
221
222         __this_cpu_write(hrtimer_interrupts_saved, hrint);
223         return false;
224 }
225
226 static int is_softlockup(unsigned long touch_ts)
227 {
228         unsigned long now = get_timestamp();
229
230         if ((watchdog_enabled & SOFT_WATCHDOG_ENABLED) && watchdog_thresh){
231                 /* Warn about unreasonable delays. */
232                 if (time_after(now, touch_ts + get_softlockup_thresh()))
233                         return now - touch_ts;
234         }
235         return 0;
236 }
237
238 static void watchdog_interrupt_count(void)
239 {
240         __this_cpu_inc(hrtimer_interrupts);
241 }
242
243 /*
244  * These two functions are mostly architecture specific
245  * defining them as weak here.
246  */
247 int __weak watchdog_nmi_enable(unsigned int cpu)
248 {
249         return 0;
250 }
251 void __weak watchdog_nmi_disable(unsigned int cpu)
252 {
253 }
254
255 static int watchdog_enable_all_cpus(void);
256 static void watchdog_disable_all_cpus(void);
257
258 /* watchdog kicker functions */
259 static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
260 {
261         unsigned long touch_ts = __this_cpu_read(watchdog_touch_ts);
262         struct pt_regs *regs = get_irq_regs();
263         int duration;
264         int softlockup_all_cpu_backtrace = sysctl_softlockup_all_cpu_backtrace;
265
266         if (atomic_read(&watchdog_park_in_progress) != 0)
267                 return HRTIMER_NORESTART;
268
269         /* kick the hardlockup detector */
270         watchdog_interrupt_count();
271
272         /* kick the softlockup detector */
273         wake_up_process(__this_cpu_read(softlockup_watchdog));
274
275         /* .. and repeat */
276         hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
277
278         if (touch_ts == 0) {
279                 if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
280                         /*
281                          * If the time stamp was touched atomically
282                          * make sure the scheduler tick is up to date.
283                          */
284                         __this_cpu_write(softlockup_touch_sync, false);
285                         sched_clock_tick();
286                 }
287
288                 /* Clear the guest paused flag on watchdog reset */
289                 kvm_check_and_clear_guest_paused();
290                 __touch_watchdog();
291                 return HRTIMER_RESTART;
292         }
293
294         /* check for a softlockup
295          * This is done by making sure a high priority task is
296          * being scheduled.  The task touches the watchdog to
297          * indicate it is getting cpu time.  If it hasn't then
298          * this is a good indication some task is hogging the cpu
299          */
300         duration = is_softlockup(touch_ts);
301         if (unlikely(duration)) {
302                 /*
303                  * If a virtual machine is stopped by the host it can look to
304                  * the watchdog like a soft lockup, check to see if the host
305                  * stopped the vm before we issue the warning
306                  */
307                 if (kvm_check_and_clear_guest_paused())
308                         return HRTIMER_RESTART;
309
310                 /* only warn once */
311                 if (__this_cpu_read(soft_watchdog_warn) == true) {
312                         /*
313                          * When multiple processes are causing softlockups the
314                          * softlockup detector only warns on the first one
315                          * because the code relies on a full quiet cycle to
316                          * re-arm.  The second process prevents the quiet cycle
317                          * and never gets reported.  Use task pointers to detect
318                          * this.
319                          */
320                         if (__this_cpu_read(softlockup_task_ptr_saved) !=
321                             current) {
322                                 __this_cpu_write(soft_watchdog_warn, false);
323                                 __touch_watchdog();
324                         }
325                         return HRTIMER_RESTART;
326                 }
327
328                 if (softlockup_all_cpu_backtrace) {
329                         /* Prevent multiple soft-lockup reports if one cpu is already
330                          * engaged in dumping cpu back traces
331                          */
332                         if (test_and_set_bit(0, &soft_lockup_nmi_warn)) {
333                                 /* Someone else will report us. Let's give up */
334                                 __this_cpu_write(soft_watchdog_warn, true);
335                                 return HRTIMER_RESTART;
336                         }
337                 }
338
339                 pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
340                         smp_processor_id(), duration,
341                         current->comm, task_pid_nr(current));
342                 __this_cpu_write(softlockup_task_ptr_saved, current);
343                 print_modules();
344                 print_irqtrace_events(current);
345                 if (regs)
346                         show_regs(regs);
347                 else
348                         dump_stack();
349
350                 if (softlockup_all_cpu_backtrace) {
351                         /* Avoid generating two back traces for current
352                          * given that one is already made above
353                          */
354                         trigger_allbutself_cpu_backtrace();
355
356                         clear_bit(0, &soft_lockup_nmi_warn);
357                         /* Barrier to sync with other cpus */
358                         smp_mb__after_atomic();
359                 }
360
361                 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
362                 if (softlockup_panic)
363                         panic("softlockup: hung tasks");
364                 __this_cpu_write(soft_watchdog_warn, true);
365         } else
366                 __this_cpu_write(soft_watchdog_warn, false);
367
368         return HRTIMER_RESTART;
369 }
370
371 static void watchdog_set_prio(unsigned int policy, unsigned int prio)
372 {
373         struct sched_param param = { .sched_priority = prio };
374
375         sched_setscheduler(current, policy, &param);
376 }
377
378 static void watchdog_enable(unsigned int cpu)
379 {
380         struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
381
382         /* kick off the timer for the hardlockup detector */
383         hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
384         hrtimer->function = watchdog_timer_fn;
385
386         /* Enable the perf event */
387         watchdog_nmi_enable(cpu);
388
389         /* done here because hrtimer_start can only pin to smp_processor_id() */
390         hrtimer_start(hrtimer, ns_to_ktime(sample_period),
391                       HRTIMER_MODE_REL_PINNED);
392
393         /* initialize timestamp */
394         watchdog_set_prio(SCHED_FIFO, MAX_RT_PRIO - 1);
395         __touch_watchdog();
396 }
397
398 static void watchdog_disable(unsigned int cpu)
399 {
400         struct hrtimer *hrtimer = raw_cpu_ptr(&watchdog_hrtimer);
401
402         watchdog_set_prio(SCHED_NORMAL, 0);
403         hrtimer_cancel(hrtimer);
404         /* disable the perf event */
405         watchdog_nmi_disable(cpu);
406 }
407
408 static void watchdog_cleanup(unsigned int cpu, bool online)
409 {
410         watchdog_disable(cpu);
411 }
412
413 static int watchdog_should_run(unsigned int cpu)
414 {
415         return __this_cpu_read(hrtimer_interrupts) !=
416                 __this_cpu_read(soft_lockup_hrtimer_cnt);
417 }
418
419 /*
420  * The watchdog thread function - touches the timestamp.
421  *
422  * It only runs once every sample_period seconds (4 seconds by
423  * default) to reset the softlockup timestamp. If this gets delayed
424  * for more than 2*watchdog_thresh seconds then the debug-printout
425  * triggers in watchdog_timer_fn().
426  */
427 static void watchdog(unsigned int cpu)
428 {
429         __this_cpu_write(soft_lockup_hrtimer_cnt,
430                          __this_cpu_read(hrtimer_interrupts));
431         __touch_watchdog();
432
433         /*
434          * watchdog_nmi_enable() clears the NMI_WATCHDOG_ENABLED bit in the
435          * failure path. Check for failures that can occur asynchronously -
436          * for example, when CPUs are on-lined - and shut down the hardware
437          * perf event on each CPU accordingly.
438          *
439          * The only non-obvious place this bit can be cleared is through
440          * watchdog_nmi_enable(), so a pr_info() is placed there.  Placing a
441          * pr_info here would be too noisy as it would result in a message
442          * every few seconds if the hardlockup was disabled but the softlockup
443          * enabled.
444          */
445         if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
446                 watchdog_nmi_disable(cpu);
447 }
448
449 static struct smp_hotplug_thread watchdog_threads = {
450         .store                  = &softlockup_watchdog,
451         .thread_should_run      = watchdog_should_run,
452         .thread_fn              = watchdog,
453         .thread_comm            = "watchdog/%u",
454         .setup                  = watchdog_enable,
455         .cleanup                = watchdog_cleanup,
456         .park                   = watchdog_disable,
457         .unpark                 = watchdog_enable,
458 };
459
460 /*
461  * park all watchdog threads that are specified in 'watchdog_cpumask'
462  *
463  * This function returns an error if kthread_park() of a watchdog thread
464  * fails. In this situation, the watchdog threads of some CPUs can already
465  * be parked and the watchdog threads of other CPUs can still be runnable.
466  * Callers are expected to handle this special condition as appropriate in
467  * their context.
468  *
469  * This function may only be called in a context that is protected against
470  * races with CPU hotplug - for example, via get_online_cpus().
471  */
472 static int watchdog_park_threads(void)
473 {
474         int cpu, ret = 0;
475
476         atomic_set(&watchdog_park_in_progress, 1);
477
478         for_each_watchdog_cpu(cpu) {
479                 ret = kthread_park(per_cpu(softlockup_watchdog, cpu));
480                 if (ret)
481                         break;
482         }
483
484         atomic_set(&watchdog_park_in_progress, 0);
485
486         return ret;
487 }
488
489 /*
490  * unpark all watchdog threads that are specified in 'watchdog_cpumask'
491  *
492  * This function may only be called in a context that is protected against
493  * races with CPU hotplug - for example, via get_online_cpus().
494  */
495 static void watchdog_unpark_threads(void)
496 {
497         int cpu;
498
499         for_each_watchdog_cpu(cpu)
500                 kthread_unpark(per_cpu(softlockup_watchdog, cpu));
501 }
502
503 /*
504  * Suspend the hard and soft lockup detector by parking the watchdog threads.
505  */
506 int lockup_detector_suspend(void)
507 {
508         int ret = 0;
509
510         get_online_cpus();
511         mutex_lock(&watchdog_proc_mutex);
512         /*
513          * Multiple suspend requests can be active in parallel (counted by
514          * the 'watchdog_suspended' variable). If the watchdog threads are
515          * running, the first caller takes care that they will be parked.
516          * The state of 'watchdog_running' cannot change while a suspend
517          * request is active (see related code in 'proc' handlers).
518          */
519         if (watchdog_running && !watchdog_suspended)
520                 ret = watchdog_park_threads();
521
522         if (ret == 0)
523                 watchdog_suspended++;
524         else {
525                 watchdog_disable_all_cpus();
526                 pr_err("Failed to suspend lockup detectors, disabled\n");
527                 watchdog_enabled = 0;
528         }
529
530         mutex_unlock(&watchdog_proc_mutex);
531
532         return ret;
533 }
534
535 /*
536  * Resume the hard and soft lockup detector by unparking the watchdog threads.
537  */
538 void lockup_detector_resume(void)
539 {
540         mutex_lock(&watchdog_proc_mutex);
541
542         watchdog_suspended--;
543         /*
544          * The watchdog threads are unparked if they were previously running
545          * and if there is no more active suspend request.
546          */
547         if (watchdog_running && !watchdog_suspended)
548                 watchdog_unpark_threads();
549
550         mutex_unlock(&watchdog_proc_mutex);
551         put_online_cpus();
552 }
553
554 static int update_watchdog_all_cpus(void)
555 {
556         int ret;
557
558         ret = watchdog_park_threads();
559         if (ret)
560                 return ret;
561
562         watchdog_unpark_threads();
563
564         return 0;
565 }
566
567 static int watchdog_enable_all_cpus(void)
568 {
569         int err = 0;
570
571         if (!watchdog_running) {
572                 err = smpboot_register_percpu_thread_cpumask(&watchdog_threads,
573                                                              &watchdog_cpumask);
574                 if (err)
575                         pr_err("Failed to create watchdog threads, disabled\n");
576                 else
577                         watchdog_running = 1;
578         } else {
579                 /*
580                  * Enable/disable the lockup detectors or
581                  * change the sample period 'on the fly'.
582                  */
583                 err = update_watchdog_all_cpus();
584
585                 if (err) {
586                         watchdog_disable_all_cpus();
587                         pr_err("Failed to update lockup detectors, disabled\n");
588                 }
589         }
590
591         if (err)
592                 watchdog_enabled = 0;
593
594         return err;
595 }
596
597 static void watchdog_disable_all_cpus(void)
598 {
599         if (watchdog_running) {
600                 watchdog_running = 0;
601                 smpboot_unregister_percpu_thread(&watchdog_threads);
602         }
603 }
604
605 #ifdef CONFIG_SYSCTL
606
607 /*
608  * Update the run state of the lockup detectors.
609  */
610 static int proc_watchdog_update(void)
611 {
612         int err = 0;
613
614         /*
615          * Watchdog threads won't be started if they are already active.
616          * The 'watchdog_running' variable in watchdog_*_all_cpus() takes
617          * care of this. If those threads are already active, the sample
618          * period will be updated and the lockup detectors will be enabled
619          * or disabled 'on the fly'.
620          */
621         if (watchdog_enabled && watchdog_thresh)
622                 err = watchdog_enable_all_cpus();
623         else
624                 watchdog_disable_all_cpus();
625
626         return err;
627
628 }
629
630 /*
631  * common function for watchdog, nmi_watchdog and soft_watchdog parameter
632  *
633  * caller             | table->data points to | 'which' contains the flag(s)
634  * -------------------|-----------------------|-----------------------------
635  * proc_watchdog      | watchdog_user_enabled | NMI_WATCHDOG_ENABLED or'ed
636  *                    |                       | with SOFT_WATCHDOG_ENABLED
637  * -------------------|-----------------------|-----------------------------
638  * proc_nmi_watchdog  | nmi_watchdog_enabled  | NMI_WATCHDOG_ENABLED
639  * -------------------|-----------------------|-----------------------------
640  * proc_soft_watchdog | soft_watchdog_enabled | SOFT_WATCHDOG_ENABLED
641  */
642 static int proc_watchdog_common(int which, struct ctl_table *table, int write,
643                                 void __user *buffer, size_t *lenp, loff_t *ppos)
644 {
645         int err, old, new;
646         int *watchdog_param = (int *)table->data;
647
648         get_online_cpus();
649         mutex_lock(&watchdog_proc_mutex);
650
651         if (watchdog_suspended) {
652                 /* no parameter changes allowed while watchdog is suspended */
653                 err = -EAGAIN;
654                 goto out;
655         }
656
657         /*
658          * If the parameter is being read return the state of the corresponding
659          * bit(s) in 'watchdog_enabled', else update 'watchdog_enabled' and the
660          * run state of the lockup detectors.
661          */
662         if (!write) {
663                 *watchdog_param = (watchdog_enabled & which) != 0;
664                 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
665         } else {
666                 err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
667                 if (err)
668                         goto out;
669
670                 /*
671                  * There is a race window between fetching the current value
672                  * from 'watchdog_enabled' and storing the new value. During
673                  * this race window, watchdog_nmi_enable() can sneak in and
674                  * clear the NMI_WATCHDOG_ENABLED bit in 'watchdog_enabled'.
675                  * The 'cmpxchg' detects this race and the loop retries.
676                  */
677                 do {
678                         old = watchdog_enabled;
679                         /*
680                          * If the parameter value is not zero set the
681                          * corresponding bit(s), else clear it(them).
682                          */
683                         if (*watchdog_param)
684                                 new = old | which;
685                         else
686                                 new = old & ~which;
687                 } while (cmpxchg(&watchdog_enabled, old, new) != old);
688
689                 /*
690                  * Update the run state of the lockup detectors. There is _no_
691                  * need to check the value returned by proc_watchdog_update()
692                  * and to restore the previous value of 'watchdog_enabled' as
693                  * both lockup detectors are disabled if proc_watchdog_update()
694                  * returns an error.
695                  */
696                 if (old == new)
697                         goto out;
698
699                 err = proc_watchdog_update();
700         }
701 out:
702         mutex_unlock(&watchdog_proc_mutex);
703         put_online_cpus();
704         return err;
705 }
706
707 /*
708  * /proc/sys/kernel/watchdog
709  */
710 int proc_watchdog(struct ctl_table *table, int write,
711                   void __user *buffer, size_t *lenp, loff_t *ppos)
712 {
713         return proc_watchdog_common(NMI_WATCHDOG_ENABLED|SOFT_WATCHDOG_ENABLED,
714                                     table, write, buffer, lenp, ppos);
715 }
716
717 /*
718  * /proc/sys/kernel/nmi_watchdog
719  */
720 int proc_nmi_watchdog(struct ctl_table *table, int write,
721                       void __user *buffer, size_t *lenp, loff_t *ppos)
722 {
723         return proc_watchdog_common(NMI_WATCHDOG_ENABLED,
724                                     table, write, buffer, lenp, ppos);
725 }
726
727 /*
728  * /proc/sys/kernel/soft_watchdog
729  */
730 int proc_soft_watchdog(struct ctl_table *table, int write,
731                         void __user *buffer, size_t *lenp, loff_t *ppos)
732 {
733         return proc_watchdog_common(SOFT_WATCHDOG_ENABLED,
734                                     table, write, buffer, lenp, ppos);
735 }
736
737 /*
738  * /proc/sys/kernel/watchdog_thresh
739  */
740 int proc_watchdog_thresh(struct ctl_table *table, int write,
741                          void __user *buffer, size_t *lenp, loff_t *ppos)
742 {
743         int err, old, new;
744
745         get_online_cpus();
746         mutex_lock(&watchdog_proc_mutex);
747
748         if (watchdog_suspended) {
749                 /* no parameter changes allowed while watchdog is suspended */
750                 err = -EAGAIN;
751                 goto out;
752         }
753
754         old = ACCESS_ONCE(watchdog_thresh);
755         err = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
756
757         if (err || !write)
758                 goto out;
759
760         /*
761          * Update the sample period. Restore on failure.
762          */
763         new = ACCESS_ONCE(watchdog_thresh);
764         if (old == new)
765                 goto out;
766
767         set_sample_period();
768         err = proc_watchdog_update();
769         if (err) {
770                 watchdog_thresh = old;
771                 set_sample_period();
772         }
773 out:
774         mutex_unlock(&watchdog_proc_mutex);
775         put_online_cpus();
776         return err;
777 }
778
779 /*
780  * The cpumask is the mask of possible cpus that the watchdog can run
781  * on, not the mask of cpus it is actually running on.  This allows the
782  * user to specify a mask that will include cpus that have not yet
783  * been brought online, if desired.
784  */
785 int proc_watchdog_cpumask(struct ctl_table *table, int write,
786                           void __user *buffer, size_t *lenp, loff_t *ppos)
787 {
788         int err;
789
790         get_online_cpus();
791         mutex_lock(&watchdog_proc_mutex);
792
793         if (watchdog_suspended) {
794                 /* no parameter changes allowed while watchdog is suspended */
795                 err = -EAGAIN;
796                 goto out;
797         }
798
799         err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
800         if (!err && write) {
801                 /* Remove impossible cpus to keep sysctl output cleaner. */
802                 cpumask_and(&watchdog_cpumask, &watchdog_cpumask,
803                             cpu_possible_mask);
804
805                 if (watchdog_running) {
806                         /*
807                          * Failure would be due to being unable to allocate
808                          * a temporary cpumask, so we are likely not in a
809                          * position to do much else to make things better.
810                          */
811                         if (smpboot_update_cpumask_percpu_thread(
812                                     &watchdog_threads, &watchdog_cpumask) != 0)
813                                 pr_err("cpumask update failed\n");
814                 }
815         }
816 out:
817         mutex_unlock(&watchdog_proc_mutex);
818         put_online_cpus();
819         return err;
820 }
821
822 #endif /* CONFIG_SYSCTL */
823
824 void __init lockup_detector_init(void)
825 {
826         set_sample_period();
827
828 #ifdef CONFIG_NO_HZ_FULL
829         if (tick_nohz_full_enabled()) {
830                 pr_info("Disabling watchdog on nohz_full cores by default\n");
831                 cpumask_copy(&watchdog_cpumask, housekeeping_mask);
832         } else
833                 cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
834 #else
835         cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
836 #endif
837
838         if (watchdog_enabled)
839                 watchdog_enable_all_cpus();
840 }