[PATCH] [RFC] watchdog/softlockup: Fix softlockup_stop_all() hungtask bug

From: Jinhui Guo
Date: Thu Sep 16 2021 - 13:59:57 EST


If NR_CPUS equal to 1, it would trigger hungtask, it can be
triggered by follow command:
echo 0 > /proc/sys/kernel/watchdog
echo 1 > /proc/sys/kernel/watchdog
The hungtask stack:
__schedule
schedule
schedule_timeout
__wait_for_common
softlockup_stop_fn
lockup_detector_reconfigure
proc_watchdog_common
proc_watchdog
proc_sys_call_handler
vfs_write
ksys_write
The watchdog_allowed_mask is completely cleared when the
watchdog is disabled. But the macro for_each_cpu() assume
all masks are "1" when macro NR_CPUS equal to 1. It makes
watchdog_allowed_mask not work at all.

Fixes: be45bf5395e0 ("watchdog/softlockup: Fix cpu_stop_queue_work() double-queue bug")

Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Petr Mladek <pmladek@xxxxxxxx>
Signed-off-by: Jinhui Guo <guojinhui@xxxxxxxxxx>
---
include/linux/cpumask.h | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
index 5d4d07a9e1ed..1a35dbcc397d 100644
--- a/include/linux/cpumask.h
+++ b/include/linux/cpumask.h
@@ -175,10 +175,11 @@ static inline int cpumask_any_distribute(const struct cpumask *srcp)
return cpumask_first(srcp);
}

+/* It should check cpumask in some special case, such as watchdog */
#define for_each_cpu(cpu, mask) \
- for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
+ for ((cpu) = 0; (cpu) < 1 && test_bit(0, cpumask_bits(mask)); (cpu)++)
#define for_each_cpu_not(cpu, mask) \
- for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
+ for ((cpu) = 0; (cpu) < 1 && !test_bit(0, cpumask_bits(mask)); (cpu)++)
#define for_each_cpu_wrap(cpu, mask, start) \
for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)(start))
#define for_each_cpu_and(cpu, mask1, mask2) \
--
2.12.3