diff mbox series

[BACKPORT,4.4.y] PM / sleep: prohibit devices probing during suspend/hibernation

Message ID 20190605111412.3461-1-zhang.chunyan@linaro.org
State New
Headers show
Series [BACKPORT,4.4.y] PM / sleep: prohibit devices probing during suspend/hibernation | expand

Commit Message

Chunyan Zhang June 5, 2019, 11:14 a.m. UTC
From: "Strashko, Grygorii" <grygorii.strashko@ti.com>


[ Upstream commit 013c074f8642d8e815ad670601f8e27155a74b57 ]

It is unsafe [1] if probing of devices will happen during suspend or
hibernation and system behavior will be unpredictable in this case.
So, let's prohibit device's probing in dpm_prepare() and defer their
probing instead. The normal behavior will be restored in
dpm_complete().

This patch introduces new DD core APIs:
 device_block_probing()
   It will disable probing of devices and defer their probes instead.
 device_unblock_probing()
   It will restore normal behavior and trigger re-probing of deferred
   devices.

[1] https://lkml.org/lkml/2015/9/11/554

Signed-off-by: Grygorii Strashko <grygorii.strashko@ti.com>

Acked-by: Pavel Machek <pavel@ucw.cz>

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

Signed-off-by: Chunyan Zhang <zhang.chunyan@linaro.org>

---
 drivers/base/base.h       |  2 ++
 drivers/base/dd.c         | 48 ++++++++++++++++++++++++++++++++++++++++++++++-
 drivers/base/power/main.c | 17 +++++++++++++++++
 3 files changed, 66 insertions(+), 1 deletion(-)

-- 
1.9.1

Comments

Sasha Levin June 6, 2019, 1:08 a.m. UTC | #1
On Wed, Jun 05, 2019 at 07:14:12PM +0800, Chunyan Zhang wrote:
>From: "Strashko, Grygorii" <grygorii.strashko@ti.com>

>

>[ Upstream commit 013c074f8642d8e815ad670601f8e27155a74b57 ]

>

>It is unsafe [1] if probing of devices will happen during suspend or

>hibernation and system behavior will be unpredictable in this case.

>So, let's prohibit device's probing in dpm_prepare() and defer their

>probing instead. The normal behavior will be restored in

>dpm_complete().

>

>This patch introduces new DD core APIs:

> device_block_probing()

>   It will disable probing of devices and defer their probes instead.

> device_unblock_probing()

>   It will restore normal behavior and trigger re-probing of deferred

>   devices.

>

>[1] https://lkml.org/lkml/2015/9/11/554

>

>Signed-off-by: Grygorii Strashko <grygorii.strashko@ti.com>

>Acked-by: Pavel Machek <pavel@ucw.cz>

>Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

>Signed-off-by: Chunyan Zhang <zhang.chunyan@linaro.org>


This patch had to be fixed a few times (see 015bb5e134 and 9a2a5a638f8),
we can't just take it as is.

It might be just simpler to move to a newer kernel at this point.

--
Thanks,
Sasha
diff mbox series

Patch

diff --git a/drivers/base/base.h b/drivers/base/base.h
index 1782f3a..e05db38 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -131,6 +131,8 @@  extern void device_remove_groups(struct device *dev,
 extern char *make_class_name(const char *name, struct kobject *kobj);
 
 extern int devres_release_all(struct device *dev);
+extern void device_block_probing(void);
+extern void device_unblock_probing(void);
 
 /* /sys/devices directory */
 extern struct kset *devices_kset;
diff --git a/drivers/base/dd.c b/drivers/base/dd.c
index d888741..ddee3456 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -55,6 +55,13 @@  static struct workqueue_struct *deferred_wq;
 static atomic_t deferred_trigger_count = ATOMIC_INIT(0);
 
 /*
+ * In some cases, like suspend to RAM or hibernation, It might be reasonable
+ * to prohibit probing of devices as it could be unsafe.
+ * Once defer_all_probes is true all drivers probes will be forcibly deferred.
+ */
+static bool defer_all_probes;
+
+/*
  * deferred_probe_work_func() - Retry probing devices in the active list.
  */
 static void deferred_probe_work_func(struct work_struct *work)
@@ -172,6 +179,30 @@  static void driver_deferred_probe_trigger(void)
 }
 
 /**
+ * device_block_probing() - Block/defere device's probes
+ *
+ *	It will disable probing of devices and defer their probes instead.
+ */
+void device_block_probing(void)
+{
+	defer_all_probes = true;
+	/* sync with probes to avoid races. */
+	wait_for_device_probe();
+}
+
+/**
+ * device_unblock_probing() - Unblock/enable device's probes
+ *
+ *	It will restore normal behavior and trigger re-probing of deferred
+ * devices.
+ */
+void device_unblock_probing(void)
+{
+	defer_all_probes = false;
+	driver_deferred_probe_trigger();
+}
+
+/**
  * deferred_probe_initcall() - Enable probing of deferred devices
  *
  * We don't want to get in the way when the bulk of drivers are getting probed.
@@ -279,9 +310,20 @@  static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
 
 static int really_probe(struct device *dev, struct device_driver *drv)
 {
-	int ret = 0;
+	int ret = -EPROBE_DEFER;
 	int local_trigger_count = atomic_read(&deferred_trigger_count);
 
+	if (defer_all_probes) {
+		/*
+		 * Value of defer_all_probes can be set only by
+		 * device_defer_all_probes_enable() which, in turn, will call
+		 * wait_for_device_probe() right after that to avoid any races.
+		 */
+		dev_dbg(dev, "Driver %s force probe deferral\n", drv->name);
+		driver_deferred_probe_add(dev);
+		return ret;
+	}
+
 	atomic_inc(&probe_count);
 	pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
 		 drv->bus->name, __func__, drv->name, dev_name(dev));
@@ -387,6 +429,10 @@  int driver_probe_done(void)
  */
 void wait_for_device_probe(void)
 {
+	/* wait for the deferred probe workqueue to finish */
+	if (driver_deferred_probe_enable)
+		flush_workqueue(deferred_wq);
+
 	/* wait for the known devices to complete their probing */
 	wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
 	async_synchronize_full();
diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
index 6c5bc3f..cf4384d 100644
--- a/drivers/base/power/main.c
+++ b/drivers/base/power/main.c
@@ -966,6 +966,9 @@  void dpm_complete(pm_message_t state)
 	}
 	list_splice(&list, &dpm_list);
 	mutex_unlock(&dpm_list_mtx);
+
+	/* Allow device probing and trigger re-probing of deferred devices */
+	device_unblock_probing();
 	trace_suspend_resume(TPS("dpm_complete"), state.event, false);
 }
 
@@ -1638,6 +1641,20 @@  int dpm_prepare(pm_message_t state)
 	trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
 	might_sleep();
 
+	/*
+	 * Give a chance for the known devices to complete their probes, before
+	 * disable probing of devices. This sync point is important at least
+	 * at boot time + hibernation restore.
+	 */
+	wait_for_device_probe();
+	/*
+	 * It is unsafe if probing of devices will happen during suspend or
+	 * hibernation and system behavior will be unpredictable in this case.
+	 * So, let's prohibit device's probing here and defer their probes
+	 * instead. The normal behavior will be restored in dpm_complete().
+	 */
+	device_block_probing();
+
 	mutex_lock(&dpm_list_mtx);
 	while (!list_empty(&dpm_list)) {
 		struct device *dev = to_device(dpm_list.next);