Skip to contentSkip to conversation

power: suspend: Move dpm_watchdog to suspend.c and enhance it

PATCH Superseded Joseph Jang 3 participants 3 messages Oct 23, 2020 – Nov 16, 2020

Subsystems (6)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 power: suspend: Move dpm_watchdog to suspend.c and enhance it

Since dpm_watchdog just cover device power management, we proposed to move it to core power suspend.c to cover not only device power management hang issues, but also core power management hand issues.

Since dpm_watchdog stands for device pm, we propose to use new name sleep timer and new timeout handler to cover more sleep hang issues. The new timeout handler will dump disk sleep task at first round timeout and trigger kernel panic at second round timeout.
The default timer for each round is defined in CONFIG_PM_SLEEP_TIMER_TIMEOUT.

---
Changes since v2:
- Add kernel/power/suspend_timer.h in MAINTAINERS
- Share some corner cases that dpm_watchdog cannot cover. Case#A: dpm_watchdog cannot cover suspend hang in suspend_enter().
File: kernel/power/suspend.c
int suspend_devices_and_enter(suspend_state_t state)
{
... <SNIP>

         suspend_test_start();
         error = dpm_suspend_start(PMSG_SUSPEND);  <== dpm_watchdog will be enabled/disabled in this fucntion ...
         if (error) {
                 pr_err("Some devices failed to suspend, or early wake event detected\n");
                 goto Recover_platform;
         }
         suspend_test_finish("suspend devices");
         if (suspend_test(TEST_DEVICES))
                 goto Recover_platform;

         do {
                 error = suspend_enter(state, &wakeup);  <== but we encounter hang inside function suspend_enter() ...
         } while (!error && !wakeup && platform_suspend_again(state));

Case#B: dpm_watchdog cannot cover resume hang in dpm_resume_early() because it enable/disable in device_resume().

  Call trace:
   __switch_to+0x174/0x194
   __schedule+0xa60/0x1464
   __cancel_work_timer+0x120/0x234
   chg_pm_resume+0x2c/0xd8
   dpm_run_callback+0x27c/0x624
   device_resume_early+0x1e4/0x1f8
   dpm_resume_early+0x350/0x8f4
   suspend_devices_and_enter+0xffc/0x168c
   pm_suspend+0xb54/0xdac

File: drivers/base/power/main.c.
static int device_resume(struct device *dev, pm_message_t state, bool async)

      if (!dpm_wait_for_superior(dev, async))
          goto Complete;
  
      dpm_watchdog_set(&wd, dev);
      device_lock(dev);
  
      /*

... <SNIP>

      Unlock:
          device_unlock(dev);
          dpm_watchdog_clear(&wd);

Case#C: dpm_watchdog cannot cover suspend hang in ksys_sync().

   Call trace:
    __switch_to+0x1b0/0x1cc
    __schedule+0xac8/0x1314
    io_schedule+0x94/0xc8
    wait_on_page_bit+0x1f8/0x3a4
    __filemap_fdatawait_range+0x134/0x150
    sync_inodes_sb+0x368/0x584
    sync_inodes_one_sb+0x18/0x24
    iterate_supers+0x12c/0x2b8
    ksys_sync+0x48/0x12c
    enter_state+0x294/0x7bc
    pm_suspend+0x164/0x2a8

- Explain some enhancement by following.
Q1: Why not use the existing one?

  struct dpm_watchdog {
      struct device           *dev;
      struct task_struct      *tsk;
      struct timer_list       timer;

};

A1: In kernel/power/suspend.c, we don't have "struct device " because suspend.c is for core PM instead of device PM. So we propose to use sleep_timer struct.

  struct sleep_timer {
      struct task_struct	*tsk;
      struct timer_list	timer;
  };
  
  
  Q2: Why not use stack memory for timer struct?
  static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
  {
      ... <SNIP>  
      timer_setup_on_stack(timer, dpm_watchdog_handler, 0);  <== dpm_watchdog use stack memory for timer struct.
      /* use same timeout value for both suspend and resume */
      timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
      add_timer(timer);
  }

A2: dpm_watchdog use stack memory has limitation.
It cannot support starting watchdog at end of function like s2idle_enter().
We cannot use stack memory for this case because the timer struct will be free when go back to caller.

  File: kernel/power/suspend.c
  static void s2idle_enter(void)
    {
          trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, true);
  
  +       stop_sleep_timer(&st);
  +
          raw_spin_lock_irq(&s2idle_lock);
          if (pm_wakeup_pending())
                  goto out;
  ... <SNIP>
          s2idle_state = S2IDLE_STATE_NONE;
          raw_spin_unlock_irq(&s2idle_lock);
  
  +       start_sleep_timer(&st);
  +
          trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, false);
    }

So we propose to declare a global sleep timer struct in suspend.c like following.

  File: kernel/power/suspend.c
   static DEFINE_RAW_SPINLOCK(s2idle_lock);

+DECLARE_SLEEP_TIMER(st);
+
/**
* pm_suspend_default_s2idle - Check if suspend-to-idle is the default suspend.

Using global timer has another advantage is we could support double calling add_timer() because add_timer() can easily to delete pending timer and add new timer by using the same timer struct.

Q3: Why do you need to change the timeout handler?
A3: dpm_watchdog_handler() need device struct to get device information, but suspend.c doesn't have it. In most of cases, we could know which device driver hang by tracing the suspend thread call stack logs without device struct information for debugging. So we propose to remove device struct and related information like "dev_driver_string(wd->dev)" and "dev_name(wd->dev)" from timeout handler.

   static void dpm_watchdog_handler(struct timer_list *t)
   {
       struct dpm_watchdog *wd = from_timer(wd, t, timer);
       dev_emerg(wd->dev, "**** DPM device timeout ****\n");
       show_stack(wd->tsk, NULL, KERN_EMERG);
       panic("%s %s: unrecoverable failure\n",
           dev_driver_string(wd->dev), dev_name(wd->dev));  <== we don't need it.

}

We propose to dump all disk sleep tasks call stack log twice to double confirm suspend thread hang at the same function and make sure other sleep tasks were stuck at the same function for a long time. We also try to resume console if the console is suspended.
At the end of watchdog timeout handler, we propose to trigger kernel panic to prevent system hang like dpm_watchdog.

  static void sleep_timeout_handler(struct timer_list *t)
  {
      struct sleep_timer *st = from_timer(st, t, timer);
      static int timeout_count;
      pr_info("Sleep timeout (timer is %d seconds)\n",
          (CONFIG_PM_SLEEP_TIMER_TIMEOUT));
      show_stack(st->tsk, NULL, KERN_EMERG);
      show_state_filter(TASK_UNINTERRUPTIBLE);
  
      if (timeout_count < 1) {
          timeout_count++;
          start_sleep_timer(st);
          return;
      }
  
      if (console_is_suspended())
          resume_console();
  
      panic("Sleep timeout and panic\n");

}

Changes since v1:
- Add commit message to explain why move dpm_watchdog to kernel/power/suspend.c.
- Remove dpm_watchdog related functions in drivers/base/power/main.c.
- Move suspend_timer.h from include/linux/ to kernel/power/.

---
 MAINTAINERS                  |  2 +
 drivers/base/power/main.c    | 69 ---------------------------
 include/linux/console.h      |  1 +
 kernel/power/Kconfig         | 27 ++++++-----
 kernel/power/suspend.c       | 19 ++++++++
 kernel/power/suspend_timer.h | 90 ++++++++++++++++++++++++++++++++++++
 kernel/printk/printk.c       |  5 ++
 7 files changed, 130 insertions(+), 83 deletions(-)
 create mode 100644 kernel/power/suspend_timer.h
MAINTAINERS +2 -0
index 867157311dc8..370e7c68f0c3 100644
@@ -7787,6 +7787,7 @@ F:	drivers/base/power/
 F:	include/linux/freezer.h
 F:	include/linux/pm.h
 F:	include/linux/suspend.h
+F:	kernel/power/suspend_timer.h
 F:	kernel/power/
 
 HID CORE LAYER
@@ -16629,6 +16630,7 @@ F:	drivers/base/power/
 F:	include/linux/freezer.h
 F:	include/linux/pm.h
 F:	include/linux/suspend.h
+F:	kernel/power/suspend_timer.h
 F:	kernel/power/
 
 SVGA HANDLING
drivers/base/power/main.c +0 -69

111 lines not on this page · raw message

include/linux/console.h +1 -0
index 0670d3491e0e..5436d8dc600f 100644
@@ -192,6 +192,7 @@ static inline void console_sysfs_notify(void)
 { }
 #endif
 extern bool console_suspend_enabled;
+extern int console_is_suspended(void);
 
 /* Suspend and resume console messages over PM events */
 extern void suspend_console(void);
kernel/power/Kconfig +13 -14
index a7320f07689d..fa021837473b 100644
@@ -207,21 +207,20 @@ config PM_SLEEP_DEBUG
 	def_bool y
 	depends on PM_DEBUG && PM_SLEEP
 
-config DPM_WATCHDOG
-	bool "Device suspend/resume watchdog"
-	depends on PM_DEBUG && PSTORE && EXPERT
+config PM_SLEEP_MONITOR
+	bool "Linux kernel suspend/resume process monitor"
+	depends on PM_SLEEP
 	help
-	  Sets up a watchdog timer to capture drivers that are
-	  locked up attempting to suspend/resume a device.
-	  A detected lockup causes system panic with message
-	  captured in pstore device for inspection in subsequent
-	  boot session.
-
-config DPM_WATCHDOG_TIMEOUT
-	int "Watchdog timeout in seconds"
-	range 1 120
-	default 120
-	depends on DPM_WATCHDOG
+	This option will enable sleep timer to prevent device stuck
+	during suspend/resume process. Sleep timeout handler will dump
+	disk sleep task at first round timeout and trigger kernel panic
+	at second round timeout. The timer for each round is defined in
+	CONFIG_PM_SLEEP_TIMER_TIMEOUT.
+
+config PM_SLEEP_TIMER_TIMEOUT
+	int "Sleep timer timeout in seconds"
+	range 30 120
+	depends on PM_SLEEP_MONITOR
 
 config PM_TRACE
 	bool
kernel/power/suspend.c +19 -0
index 8b1bb5ee7e5d..0272494e0d0e 100644
@@ -32,6 +32,7 @@
 #include <linux/moduleparam.h>
 
 #include "power.h"
+#include "suspend_timer.h"
 
 const char * const pm_labels[] = {
 	[PM_SUSPEND_TO_IDLE] = "freeze",
@@ -61,6 +62,8 @@ static DECLARE_SWAIT_QUEUE_HEAD(s2idle_wait_head);
 enum s2idle_states __read_mostly s2idle_state;
 static DEFINE_RAW_SPINLOCK(s2idle_lock);
 
+DECLARE_SLEEP_TIMER(st);
+
 /**
  * pm_suspend_default_s2idle - Check if suspend-to-idle is the default suspend.
  *
@@ -89,6 +92,8 @@ static void s2idle_enter(void)
 {
 	trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, true);
 
+	stop_sleep_timer(&st);
+
 	raw_spin_lock_irq(&s2idle_lock);
 	if (pm_wakeup_pending())
 		goto out;
@@ -114,6 +119,8 @@ static void s2idle_enter(void)
 	s2idle_state = S2IDLE_STATE_NONE;
 	raw_spin_unlock_irq(&s2idle_lock);
 
+	start_sleep_timer(&st);
+
 	trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, false);
 }
 
@@ -179,6 +186,8 @@ void __init pm_states_init(void)
 	 * initialize mem_sleep_states[] accordingly here.
 	 */
 	mem_sleep_states[PM_SUSPEND_TO_IDLE] = mem_sleep_labels[PM_SUSPEND_TO_IDLE];
+
+	init_sleep_timer(&st, sleep_timeout_handler);
 }
 
 static int __init mem_sleep_default_setup(char *str)
@@ -426,6 +435,8 @@ static int suspend_enter(suspend_state_t state, bool *wakeup)
 	if (error || suspend_test(TEST_CPUS))
 		goto Enable_cpus;
 
+	stop_sleep_timer(&st);
+
 	arch_suspend_disable_irqs();
 	BUG_ON(!irqs_disabled());
 
@@ -451,6 +462,8 @@ static int suspend_enter(suspend_state_t state, bool *wakeup)
 	arch_suspend_enable_irqs();
 	BUG_ON(irqs_disabled());
 
+	start_sleep_timer(&st);
+
  Enable_cpus:
 	suspend_enable_secondary_cpus();
 
@@ -610,6 +623,9 @@ int pm_suspend(suspend_state_t state)
 		return -EINVAL;
 
 	pr_info("suspend entry (%s)\n", mem_sleep_labels[state]);
+
+	start_sleep_timer(&st);
+
 	error = enter_state(state);
 	if (error) {
 		suspend_stats.fail++;
@@ -617,6 +633,9 @@ int pm_suspend(suspend_state_t state)
 	} else {
 		suspend_stats.success++;
 	}
+
+	stop_sleep_timer(&st);
+
 	pr_info("suspend exit\n");
 	return error;
 }
kernel/power/suspend_timer.h new file +90 -0

90 lines not on this page · raw message

kernel/printk/printk.c +5 -0
index 9b75f6bfc333..488cbc9abaaf 100644
@@ -2259,6 +2259,11 @@ module_param_named(console_suspend, console_suspend_enabled,
 MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
 	" and hibernate operations");
 
+int console_is_suspended(void)
+{
+	return console_suspended;
+}
+
 /**
  * suspend_console - suspend the console subsystem
  *
Signature · 1 line
2.29.0.rc2.309.g374f81d7ae-goog
#1 lore raw
Quoted 4 lines · Joseph Jang

Since dpm_watchdog just cover device power management, we proposed to move it to core power suspend.c to cover not only device power management hang issues, but also core power management hand issues.

Quoted 8 lines

Since dpm_watchdog stands for device pm, we propose to use new name sleep timer and new timeout handler to cover more sleep hang issues. The new timeout handler will dump disk sleep task at first round timeout and trigger kernel panic at second round timeout.
The default timer for each round is defined in CONFIG_PM_SLEEP_TIMER_TIMEOUT.

It still seems odd you can't use a watchdog for this, as you really have just implemented the same thing here, but "open coded" the logic.

I'll let Rafael chime in here too, as he knows this area much better than I as to if this is viable or not.

Some coding comments:

include/linux/console.h static inline void console_sysfs_notify(void) · line 195

Quoted 9 lines
include/linux/console.h +1 -0
index 0670d3491e0e..5436d8dc600f 100644
@@ -192,6 +192,7 @@ static inline void console_sysfs_notify(void)
 { }
 #endif
 extern bool console_suspend_enabled;
+extern int console_is_suspended(void);

Why do you care about the console? This feels like it should be separate and agreed upon by others as to if this is ok to get access to or not.

Quoted 55 lines
include/linux/console.h +0 -0
 /* Suspend and resume console messages over PM events */
 extern void suspend_console(void);
kernel/power/Kconfig +13 -14
index a7320f07689d..fa021837473b 100644
@@ -207,21 +207,20 @@ config PM_SLEEP_DEBUG
 	def_bool y
 	depends on PM_DEBUG && PM_SLEEP
 
-config DPM_WATCHDOG
-	bool "Device suspend/resume watchdog"
-	depends on PM_DEBUG && PSTORE && EXPERT
+config PM_SLEEP_MONITOR
+	bool "Linux kernel suspend/resume process monitor"
+	depends on PM_SLEEP
 	help
-	  Sets up a watchdog timer to capture drivers that are
-	  locked up attempting to suspend/resume a device.
-	  A detected lockup causes system panic with message
-	  captured in pstore device for inspection in subsequent
-	  boot session.
-
-config DPM_WATCHDOG_TIMEOUT
-	int "Watchdog timeout in seconds"
-	range 1 120
-	default 120
-	depends on DPM_WATCHDOG
+	This option will enable sleep timer to prevent device stuck
+	during suspend/resume process. Sleep timeout handler will dump
+	disk sleep task at first round timeout and trigger kernel panic
+	at second round timeout. The timer for each round is defined in
+	CONFIG_PM_SLEEP_TIMER_TIMEOUT.
+
+config PM_SLEEP_TIMER_TIMEOUT
+	int "Sleep timer timeout in seconds"
+	range 30 120
+	depends on PM_SLEEP_MONITOR
 
 config PM_TRACE
 	bool
kernel/power/suspend.c +1 -0
index 8b1bb5ee7e5d..0272494e0d0e 100644
@@ -32,6 +32,7 @@
 #include <linux/moduleparam.h>
 
 #include "power.h"
+#include "suspend_timer.h"
 
 const char * const pm_labels[] = {
 	[PM_SUSPEND_TO_IDLE] = "freeze",

kernel/power/suspend.c static DECLARE_SWAIT_QUEUE_HEAD(s2idle_wait_head); · line 65

Quoted 4 lines
kernel/power/suspend.c +1 -0
@@ -61,6 +62,8 @@ static DECLARE_SWAIT_QUEUE_HEAD(s2idle_wait_head);
 enum s2idle_states __read_mostly s2idle_state;
 static DEFINE_RAW_SPINLOCK(s2idle_lock);
 
+DECLARE_SLEEP_TIMER(st);

Why is this not static? "st" is a very bad name for a global symbol in the kernel namespace :(

Quoted 75 lines
kernel/power/suspend_timer.h new file +72 -0
@@ -0,0 +1,90 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_SLEEP_TIMER_H
+#define _LINUX_SLEEP_TIMER_H
+
+#include <linux/sched/debug.h>
+
+#ifdef CONFIG_PM_SLEEP_MONITOR
+struct sleep_timer {
+	struct task_struct	*tsk;
+	struct timer_list	timer;
+};
+
+#define DECLARE_SLEEP_TIMER(st) \
+	struct sleep_timer st
+
+/**
+ * init_sleep_timer - Initialize sleep timer.
+ * @st: Sleep timer to initialize.
+ * @func: Sleep timer timeout handler.
+ */
+static void init_sleep_timer(struct sleep_timer *st, void (*func))
+{
+	struct timer_list *timer = &st->timer;
+
+	timer_setup(timer, func, 0);
+}
+
+/**
+ * start_sleep_timer - Enable sleep timer to monitor suspend thread.
+ * @st: Sleep timer to enable.
+ */
+static void start_sleep_timer(struct sleep_timer *st)
+{
+	struct timer_list *timer = &st->timer;
+
+	st->tsk = current;
+
+	/* use same timeout value for both suspend and resume */
+	timer->expires = jiffies + HZ * CONFIG_PM_SLEEP_TIMER_TIMEOUT;
+	add_timer(timer);
+}
+
+/**
+ * stop_sleep_timer - Disable sleep timer.
+ * @st: sleep timer to disable.
+ */
+static void stop_sleep_timer(struct sleep_timer *st)
+{
+	struct timer_list *timer = &st->timer;
+
+	del_timer_sync(timer);
+}
+
+/**
+ * sleep_timeout_handler - sleep timer timeout handler.
+ * @t: The timer list that sleep timer depends on.
+ *
+ * Called when suspend thread has timeout suspending or resuming.
+ * Dump all uninterruptible tasks' call stack and call panic() to
+ * reboot system in second round timeout.
+ */
+static void sleep_timeout_handler(struct timer_list *t)
+{
+	struct sleep_timer *st = from_timer(st, t, timer);
+	static int timeout_count;
+
+	pr_info("Sleep timeout (timer is %d seconds)\n",
+		(CONFIG_PM_SLEEP_TIMER_TIMEOUT));
+	show_stack(st->tsk, NULL, KERN_EMERG);
+	show_state_filter(TASK_UNINTERRUPTIBLE);
+
+	if (timeout_count < 1) {

kernel/power/suspend_timer.h line 82

Quoted 10 lines
kernel/power/suspend_timer.h new file +10 -0
+		timeout_count++;
+		start_sleep_timer(st);
+		return;
+	}
+
+	if (console_is_suspended())
+		resume_console();
+
+	panic("Sleep timeout and panic\n");
+}

You have a lot of logic in a .h file, that should all be in a .c file.

Just put the function prototypes in here and put the real logic in a .c file, and only link that in if the config option is enabled. As it is, this isn't ok no matter what.

kernel/power/suspend_timer.h line 87

Quoted 5 lines
kernel/power/suspend_timer.h new file +5 -0
+#else
+#define DECLARE_SLEEP_TIMER(st)
+#define init_sleep_timer(x, y)
+#define start_sleep_timer(x)
+#define stop_sleep_timer(x)

Use inline functions so you get proper type checking, not #define.

Quoted 8 lines
kernel/power/suspend_timer.h new file +3 -0
+#endif
+
+#endif /* _LINUX_SLEEP_TIMER_H */
kernel/printk/printk.c +0 -0
index 9b75f6bfc333..488cbc9abaaf 100644

kernel/printk/printk.c module_param_named(console_suspend, console_suspend_enabled, · line 2265

Quoted 7 lines
kernel/printk/printk.c +4 -0
@@ -2259,6 +2259,11 @@ module_param_named(console_suspend, console_suspend_enabled,
 MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
 	" and hibernate operations");
 
+int console_is_suspended(void)
+{
+	return console_suspended;
+}

Again, I don't think this is ok. What happens if this changes _right_ after you check it? Why does it matter?

thanks,

greg k-h

#2 lore raw
Quoted 13 lines · Greg Kroah-Hartman
Quoted 11 lines · Joseph Jang

Since dpm_watchdog just cover device power management, we proposed to move it to core power suspend.c to cover not only device power management hang issues, but also core power management hand issues.

Since dpm_watchdog stands for device pm, we propose to use new name sleep timer and new timeout handler to cover more sleep hang issues. The new timeout handler will dump disk sleep task at first round timeout and trigger kernel panic at second round timeout.
The default timer for each round is defined in CONFIG_PM_SLEEP_TIMER_TIMEOUT.

Quoted 7 lines
Quoted 1 line

It still seems odd you can't use a watchdog for this, as you really have just implemented the same thing here, but "open coded" the logic.

I'll let Rafael chime in here too, as he knows this area much better than I as to if this is viable or not.

It is viable AFAICS.

Just make the existing thing work with dev == NULL and use it as needed.