Skip to content

Commit fe4ba3c

Browse files
Chris Metcalftorvalds
authored andcommitted
watchdog: add watchdog_cpumask sysctl to assist nohz
Change the default behavior of watchdog so it only runs on the housekeeping cores when nohz_full is enabled at build and boot time. Allow modifying the set of cores the watchdog is currently running on with a new kernel.watchdog_cpumask sysctl. In the current system, the watchdog subsystem runs a periodic timer that schedules the watchdog kthread to run. However, nohz_full cores are designed to allow userspace application code running on those cores to have 100% access to the CPU. So the watchdog system prevents the nohz_full application code from being able to run the way it wants to, thus the motivation to suppress the watchdog on nohz_full cores, which this patchset provides by default. However, if we disable the watchdog globally, then the housekeeping cores can't benefit from the watchdog functionality. So we allow disabling it only on some cores. See Documentation/lockup-watchdogs.txt for more information. [jhubbard@nvidia.com: fix a watchdog crash in some configurations] Signed-off-by: Chris Metcalf <cmetcalf@ezchip.com> Acked-by: Don Zickus <dzickus@redhat.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Ulrich Obergfell <uobergfe@redhat.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Frederic Weisbecker <fweisbec@gmail.com> Signed-off-by: John Hubbard <jhubbard@nvidia.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b5242e9 commit fe4ba3c

6 files changed

Lines changed: 112 additions & 5 deletions

File tree

‎Documentation/lockup-watchdogs.txt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,21 @@ As explained above, a kernel knob is provided that allows
6161
administrators to configure the period of the hrtimer and the perf
6262
event. The right value for a particular environment is a trade-off
6363
between fast response to lockups and detection overhead.
64+
65+
By default, the watchdog runs on all online cores. However, on a
66+
kernel configured with NO_HZ_FULL, by default the watchdog runs only
67+
on the housekeeping cores, not the cores specified in the "nohz_full"
68+
boot argument. If we allowed the watchdog to run by default on
69+
the "nohz_full" cores, we would have to run timer ticks to activate
70+
the scheduler, which would prevent the "nohz_full" functionality
71+
from protecting the user code on those cores from the kernel.
72+
Of course, disabling it by default on the nohz_full cores means that
73+
when those cores do enter the kernel, by default we will not be
74+
able to detect if they lock up. However, allowing the watchdog
75+
to continue to run on the housekeeping (non-tickless) cores means
76+
that we will continue to detect lockups properly on those cores.
77+
78+
In either case, the set of cores excluded from running the watchdog
79+
may be adjusted via the kernel.watchdog_cpumask sysctl. For
80+
nohz_full cores, this may be useful for debugging a case where the
81+
kernel seems to be hanging on the nohz_full cores.

‎Documentation/sysctl/kernel.txt‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,27 @@ and nmi_watchdog.
923923

924924
==============================================================
925925

926+
watchdog_cpumask:
927+
928+
This value can be used to control on which cpus the watchdog may run.
929+
The default cpumask is all possible cores, but if NO_HZ_FULL is
930+
enabled in the kernel config, and cores are specified with the
931+
nohz_full= boot argument, those cores are excluded by default.
932+
Offline cores can be included in this mask, and if the core is later
933+
brought online, the watchdog will be started based on the mask value.
934+
935+
Typically this value would only be touched in the nohz_full case
936+
to re-enable cores that by default were not running the watchdog,
937+
if a kernel lockup was suspected on those cores.
938+
939+
The argument value is the standard cpulist format for cpumasks,
940+
so for example to enable the watchdog on cores 0, 2, 3, and 4 you
941+
might say:
942+
943+
echo 0,2-4 > /proc/sys/kernel/watchdog_cpumask
944+
945+
==============================================================
946+
926947
watchdog_thresh:
927948

928949
This value can be used to control the frequency of hrtimer and NMI

‎include/linux/nmi.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ extern int nmi_watchdog_enabled;
6767
extern int soft_watchdog_enabled;
6868
extern int watchdog_user_enabled;
6969
extern int watchdog_thresh;
70+
extern unsigned long *watchdog_cpumask_bits;
7071
extern int sysctl_softlockup_all_cpu_backtrace;
7172
struct ctl_table;
7273
extern int proc_watchdog(struct ctl_table *, int ,
@@ -77,6 +78,8 @@ extern int proc_soft_watchdog(struct ctl_table *, int ,
7778
void __user *, size_t *, loff_t *);
7879
extern int proc_watchdog_thresh(struct ctl_table *, int ,
7980
void __user *, size_t *, loff_t *);
81+
extern int proc_watchdog_cpumask(struct ctl_table *, int,
82+
void __user *, size_t *, loff_t *);
8083
#endif
8184

8285
#ifdef CONFIG_HAVE_ACPI_APEI_NMI

‎kernel/smpboot.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ EXPORT_SYMBOL_GPL(smpboot_unregister_percpu_thread);
338338
*
339339
* The cpumask field in the smp_hotplug_thread must not be updated directly
340340
* by the client, but only by calling this function.
341+
* This function can only be called on a registered smp_hotplug_thread.
341342
*/
342343
int smpboot_update_cpumask_percpu_thread(struct smp_hotplug_thread *plug_thread,
343344
const struct cpumask *new)

‎kernel/sysctl.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,13 @@ static struct ctl_table kern_table[] = {
871871
.extra1 = &zero,
872872
.extra2 = &one,
873873
},
874+
{
875+
.procname = "watchdog_cpumask",
876+
.data = &watchdog_cpumask_bits,
877+
.maxlen = NR_CPUS,
878+
.mode = 0644,
879+
.proc_handler = proc_watchdog_cpumask,
880+
},
874881
{
875882
.procname = "softlockup_panic",
876883
.data = &softlockup_panic,

‎kernel/watchdog.c‎

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/sysctl.h>
2020
#include <linux/smpboot.h>
2121
#include <linux/sched/rt.h>
22+
#include <linux/tick.h>
2223

2324
#include <asm/irq_regs.h>
2425
#include <linux/kvm_para.h>
@@ -58,6 +59,12 @@ int __read_mostly sysctl_softlockup_all_cpu_backtrace;
5859
#else
5960
#define sysctl_softlockup_all_cpu_backtrace 0
6061
#endif
62+
static struct cpumask watchdog_cpumask __read_mostly;
63+
unsigned long *watchdog_cpumask_bits = cpumask_bits(&watchdog_cpumask);
64+
65+
/* Helper for online, unparked cpus. */
66+
#define for_each_watchdog_cpu(cpu) \
67+
for_each_cpu_and((cpu), cpu_online_mask, &watchdog_cpumask)
6168

6269
static int __read_mostly watchdog_running;
6370
static u64 __read_mostly sample_period;
@@ -207,7 +214,7 @@ void touch_all_softlockup_watchdogs(void)
207214
* do we care if a 0 races with a timestamp?
208215
* all it means is the softlock check starts one cycle later
209216
*/
210-
for_each_online_cpu(cpu)
217+
for_each_watchdog_cpu(cpu)
211218
per_cpu(watchdog_touch_ts, cpu) = 0;
212219
}
213220

@@ -616,7 +623,7 @@ void watchdog_nmi_enable_all(void)
616623
goto unlock;
617624

618625
get_online_cpus();
619-
for_each_online_cpu(cpu)
626+
for_each_watchdog_cpu(cpu)
620627
watchdog_nmi_enable(cpu);
621628
put_online_cpus();
622629

@@ -634,7 +641,7 @@ void watchdog_nmi_disable_all(void)
634641
goto unlock;
635642

636643
get_online_cpus();
637-
for_each_online_cpu(cpu)
644+
for_each_watchdog_cpu(cpu)
638645
watchdog_nmi_disable(cpu);
639646
put_online_cpus();
640647

@@ -696,7 +703,7 @@ static void update_watchdog_all_cpus(void)
696703
int cpu;
697704

698705
get_online_cpus();
699-
for_each_online_cpu(cpu)
706+
for_each_watchdog_cpu(cpu)
700707
update_watchdog(cpu);
701708
put_online_cpus();
702709
}
@@ -709,8 +716,12 @@ static int watchdog_enable_all_cpus(void)
709716
err = smpboot_register_percpu_thread(&watchdog_threads);
710717
if (err)
711718
pr_err("Failed to create watchdog threads, disabled\n");
712-
else
719+
else {
720+
if (smpboot_update_cpumask_percpu_thread(
721+
&watchdog_threads, &watchdog_cpumask))
722+
pr_err("Failed to set cpumask for watchdog threads\n");
713723
watchdog_running = 1;
724+
}
714725
} else {
715726
/*
716727
* Enable/disable the lockup detectors or
@@ -879,12 +890,58 @@ int proc_watchdog_thresh(struct ctl_table *table, int write,
879890
mutex_unlock(&watchdog_proc_mutex);
880891
return err;
881892
}
893+
894+
/*
895+
* The cpumask is the mask of possible cpus that the watchdog can run
896+
* on, not the mask of cpus it is actually running on. This allows the
897+
* user to specify a mask that will include cpus that have not yet
898+
* been brought online, if desired.
899+
*/
900+
int proc_watchdog_cpumask(struct ctl_table *table, int write,
901+
void __user *buffer, size_t *lenp, loff_t *ppos)
902+
{
903+
int err;
904+
905+
mutex_lock(&watchdog_proc_mutex);
906+
err = proc_do_large_bitmap(table, write, buffer, lenp, ppos);
907+
if (!err && write) {
908+
/* Remove impossible cpus to keep sysctl output cleaner. */
909+
cpumask_and(&watchdog_cpumask, &watchdog_cpumask,
910+
cpu_possible_mask);
911+
912+
if (watchdog_running) {
913+
/*
914+
* Failure would be due to being unable to allocate
915+
* a temporary cpumask, so we are likely not in a
916+
* position to do much else to make things better.
917+
*/
918+
if (smpboot_update_cpumask_percpu_thread(
919+
&watchdog_threads, &watchdog_cpumask) != 0)
920+
pr_err("cpumask update failed\n");
921+
}
922+
}
923+
mutex_unlock(&watchdog_proc_mutex);
924+
return err;
925+
}
926+
882927
#endif /* CONFIG_SYSCTL */
883928

884929
void __init lockup_detector_init(void)
885930
{
886931
set_sample_period();
887932

933+
#ifdef CONFIG_NO_HZ_FULL
934+
if (tick_nohz_full_enabled()) {
935+
if (!cpumask_empty(tick_nohz_full_mask))
936+
pr_info("Disabling watchdog on nohz_full cores by default\n");
937+
cpumask_andnot(&watchdog_cpumask, cpu_possible_mask,
938+
tick_nohz_full_mask);
939+
} else
940+
cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
941+
#else
942+
cpumask_copy(&watchdog_cpumask, cpu_possible_mask);
943+
#endif
944+
888945
if (watchdog_enabled)
889946
watchdog_enable_all_cpus();
890947
}

0 commit comments

Comments
 (0)