Skip to contentSkip to conversation

PM: Handle unregistering devices during suspend/hibernation

PATCH In review Rafael J. Wysocki 2 participants 9 messages Feb 22, 2008 – Feb 23, 2008

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PM: Handle unregistering devices during suspend/hibernation

Hi Greg,

The appended patch fixes the issue with the new code for suspending/resuming devices, related to the fact that some device drivers and CPU hotplug notifiers unregister device objects while suspend is in progress, which leads to deadlocks.

Please consider taking it for 2.6.25.

Thanks,
Rafael

---
From: Rafael J. Wysocki <[email protected]>

Introduce a mechanism preventing drivers and CPU hotplug notifiers from deadlocking suspend/hibernation by unregistering device objects while it is in progress. Specifically, make device_del() detect if it has been called by the suspending task and automatically defer the removal of the device object if that's the case.

---
 drivers/base/core.c        |    5 +++++
 drivers/base/power/main.c  |    9 +++++++++
 drivers/base/power/power.h |    5 +++++
 3 files changed, 19 insertions(+)
drivers/base/power/main.c +9 -0
Index: linux-2.6/drivers/base/power/main.c
===================================================================
@@ -59,6 +59,13 @@ static DECLARE_RWSEM(pm_sleep_rwsem);
 
 int (*platform_enable_wakeup)(struct device *dev, int is_on);
 
+static struct task_struct *suspending_task;
+
+bool in_suspend_context(void)
+{
+	return (suspending_task == current);
+}
+
 /**
  *	device_pm_add - add a device to the list of active devices
  *	@dev:	Device to be added to the list
@@ -272,6 +279,7 @@ static void dpm_resume(void)
 		mutex_lock(&dpm_list_mtx);
 	}
 	mutex_unlock(&dpm_list_mtx);
+	suspending_task = NULL;
 }
 
 /**
@@ -460,6 +468,7 @@ static int dpm_suspend(pm_message_t stat
 {
 	int error = 0;
 
+	suspending_task = current;
 	mutex_lock(&dpm_list_mtx);
 	while (!list_empty(&dpm_locked)) {
 		struct list_head *entry = dpm_locked.prev;
drivers/base/core.c +5 -0
Index: linux-2.6/drivers/base/core.c
===================================================================
@@ -929,6 +929,11 @@ void device_del(struct device *dev)
 	struct device *parent = dev->parent;
 	struct class_interface *class_intf;
 
+	if (in_suspend_context()) {
+		get_device(dev);
+		device_pm_schedule_removal(dev);
+		return;
+	}
 	device_pm_remove(dev);
 	if (parent)
 		klist_del(&dev->knode_parent);
drivers/base/power/power.h +5 -0
Index: linux-2.6/drivers/base/power/power.h
===================================================================
@@ -11,6 +11,7 @@ static inline struct device *to_device(s
 	return container_of(entry, struct device, power.entry);
 }
 
+extern bool in_suspend_context(void);
 extern void device_pm_add(struct device *);
 extern void device_pm_remove(struct device *);
 extern int pm_sleep_lock(void);
@@ -18,6 +19,10 @@ extern void pm_sleep_unlock(void);
 
 #else /* CONFIG_PM_SLEEP */
 
+static inline bool in_suspend_context(void)
+{
+	return false;
+}
 
 static inline void device_pm_add(struct device *dev)
 {
#1 lore raw
Quoted 68 lines · Rafael J. Wysocki

Hi Greg,

The appended patch fixes the issue with the new code for suspending/resuming devices, related to the fact that some device drivers and CPU hotplug notifiers unregister device objects while suspend is in progress, which leads to deadlocks.

Please consider taking it for 2.6.25.

Thanks,
Rafael

---
From: Rafael J. Wysocki <[email protected]>

Introduce a mechanism preventing drivers and CPU hotplug notifiers from deadlocking suspend/hibernation by unregistering device objects while it is in progress. Specifically, make device_del() detect if it has been called by the suspending task and automatically defer the removal of the device object if that's the case.

---
 drivers/base/core.c        |    5 +++++
 drivers/base/power/main.c  |    9 +++++++++
 drivers/base/power/power.h |    5 +++++
 3 files changed, 19 insertions(+)

Index: linux-2.6/drivers/base/power/main.c
===================================================================

drivers/base/power/main.c +9 -0
@@ -59,6 +59,13 @@ static DECLARE_RWSEM(pm_sleep_rwsem);
 
 int (*platform_enable_wakeup)(struct device *dev, int is_on);
 
+static struct task_struct *suspending_task;
+
+bool in_suspend_context(void)
+{
+	return (suspending_task == current);
+}
+
 /**
  *	device_pm_add - add a device to the list of active devices
  *	@dev:	Device to be added to the list
@@ -272,6 +279,7 @@ static void dpm_resume(void)
 		mutex_lock(&dpm_list_mtx);
 	}
 	mutex_unlock(&dpm_list_mtx);
+	suspending_task = NULL;
 }
 
 /**
@@ -460,6 +468,7 @@ static int dpm_suspend(pm_message_t stat
 {
 	int error = 0;
 
+	suspending_task = current;
 	mutex_lock(&dpm_list_mtx);
 	while (!list_empty(&dpm_locked)) {
 		struct list_head *entry = dpm_locked.prev;

Index: linux-2.6/drivers/base/core.c
===================================================================

drivers/base/core.c void device_del(struct device *dev) · line 936

Quoted 8 lines
drivers/base/core.c +5 -0
@@ -929,6 +929,11 @@ void device_del(struct device *dev)
 	struct device *parent = dev->parent;
 	struct class_interface *class_intf;
 
+	if (in_suspend_context()) {
+		get_device(dev);
+		device_pm_schedule_removal(dev);
+		return;
+	}

Why are you grabbing an additional reference to the device here? That would seem to get out of balance when the device is later scheduled for removal, right?

thanks,

greg k-h

#2 lore raw
In reply to Rafael J. Wysocki
Quoted 68 lines · Rafael J. Wysocki

Hi Greg,

The appended patch fixes the issue with the new code for suspending/resuming devices, related to the fact that some device drivers and CPU hotplug notifiers unregister device objects while suspend is in progress, which leads to deadlocks.

Please consider taking it for 2.6.25.

Thanks,
Rafael

---
From: Rafael J. Wysocki <[email protected]>

Introduce a mechanism preventing drivers and CPU hotplug notifiers from deadlocking suspend/hibernation by unregistering device objects while it is in progress. Specifically, make device_del() detect if it has been called by the suspending task and automatically defer the removal of the device object if that's the case.

---
 drivers/base/core.c        |    5 +++++
 drivers/base/power/main.c  |    9 +++++++++
 drivers/base/power/power.h |    5 +++++
 3 files changed, 19 insertions(+)

Index: linux-2.6/drivers/base/power/main.c
===================================================================

drivers/base/power/main.c +9 -0
@@ -59,6 +59,13 @@ static DECLARE_RWSEM(pm_sleep_rwsem);
 
 int (*platform_enable_wakeup)(struct device *dev, int is_on);
 
+static struct task_struct *suspending_task;
+
+bool in_suspend_context(void)
+{
+	return (suspending_task == current);
+}
+
 /**
  *	device_pm_add - add a device to the list of active devices
  *	@dev:	Device to be added to the list
@@ -272,6 +279,7 @@ static void dpm_resume(void)
 		mutex_lock(&dpm_list_mtx);
 	}
 	mutex_unlock(&dpm_list_mtx);
+	suspending_task = NULL;
 }
 
 /**
@@ -460,6 +468,7 @@ static int dpm_suspend(pm_message_t stat
 {
 	int error = 0;
 
+	suspending_task = current;
 	mutex_lock(&dpm_list_mtx);
 	while (!list_empty(&dpm_locked)) {
 		struct list_head *entry = dpm_locked.prev;

Index: linux-2.6/drivers/base/core.c
===================================================================

drivers/base/core.c void device_del(struct device *dev) · line 936

Quoted 8 lines
drivers/base/core.c +5 -0
@@ -929,6 +929,11 @@ void device_del(struct device *dev)
 	struct device *parent = dev->parent;
 	struct class_interface *class_intf;
 
+	if (in_suspend_context()) {
+		get_device(dev);
+		device_pm_schedule_removal(dev);
+		return;
+	}

Why are you grabbing an additional reference to the device here? That would seem to get out of balance when the device is later scheduled for removal, right?

thanks,

greg k-h

#3 lore raw
Quoted 72 lines · Greg KH
Quoted 71 lines · Rafael J. Wysocki

Hi Greg,

The appended patch fixes the issue with the new code for suspending/resuming devices, related to the fact that some device drivers and CPU hotplug notifiers unregister device objects while suspend is in progress, which leads to deadlocks.

Please consider taking it for 2.6.25.

Thanks,
Rafael

---
From: Rafael J. Wysocki <[email protected]>

Introduce a mechanism preventing drivers and CPU hotplug notifiers from deadlocking suspend/hibernation by unregistering device objects while it is in progress. Specifically, make device_del() detect if it has been called by the suspending task and automatically defer the removal of the device object if that's the case.

---
 drivers/base/core.c        |    5 +++++
 drivers/base/power/main.c  |    9 +++++++++
 drivers/base/power/power.h |    5 +++++
 3 files changed, 19 insertions(+)

Index: linux-2.6/drivers/base/power/main.c
===================================================================

drivers/base/power/main.c +9 -0
@@ -59,6 +59,13 @@ static DECLARE_RWSEM(pm_sleep_rwsem);
 
 int (*platform_enable_wakeup)(struct device *dev, int is_on);
 
+static struct task_struct *suspending_task;
+
+bool in_suspend_context(void)
+{
+	return (suspending_task == current);
+}
+
 /**
  *	device_pm_add - add a device to the list of active devices
  *	@dev:	Device to be added to the list
@@ -272,6 +279,7 @@ static void dpm_resume(void)
 		mutex_lock(&dpm_list_mtx);
 	}
 	mutex_unlock(&dpm_list_mtx);
+	suspending_task = NULL;
 }
 
 /**
@@ -460,6 +468,7 @@ static int dpm_suspend(pm_message_t stat
 {
 	int error = 0;
 
+	suspending_task = current;
 	mutex_lock(&dpm_list_mtx);
 	while (!list_empty(&dpm_locked)) {
 		struct list_head *entry = dpm_locked.prev;

Index: linux-2.6/drivers/base/core.c
===================================================================

drivers/base/core.c +0 -0
@@ -929,6 +929,11 @@ void device_del(struct device *dev)
 	struct device *parent = dev->parent;
 	struct class_interface *class_intf;
 
Quoted 9 lines

drivers/base/core.c void device_del(struct device *dev) · line 936

Quoted 5 lines
drivers/base/core.c +5 -0
+	if (in_suspend_context()) {
+		get_device(dev);
+		device_pm_schedule_removal(dev);
+		return;
+	}

Why are you grabbing an additional reference to the device here? That would seem to get out of balance when the device is later scheduled for removal, right?

No, IMO the reference is necessary, because unregister_dropped_devices() uses device_unregister() that does the put_device() eventually.

If we are called by device_unregister(), the get_device() is needed to balance the put_device() that will be called by device_unregister() after we return.

OTOH, if we are called directly, then we need to balance the put_device() that will be done by device_unregister() called from unregister_dropped_devices().

Thanks,
Rafael

#4 lore raw
In reply to Greg KH
Quoted 72 lines · Greg KH
Quoted 71 lines · Rafael J. Wysocki

Hi Greg,

The appended patch fixes the issue with the new code for suspending/resuming devices, related to the fact that some device drivers and CPU hotplug notifiers unregister device objects while suspend is in progress, which leads to deadlocks.

Please consider taking it for 2.6.25.

Thanks,
Rafael

---
From: Rafael J. Wysocki <[email protected]>

Introduce a mechanism preventing drivers and CPU hotplug notifiers from deadlocking suspend/hibernation by unregistering device objects while it is in progress. Specifically, make device_del() detect if it has been called by the suspending task and automatically defer the removal of the device object if that's the case.

---
 drivers/base/core.c        |    5 +++++
 drivers/base/power/main.c  |    9 +++++++++
 drivers/base/power/power.h |    5 +++++
 3 files changed, 19 insertions(+)

Index: linux-2.6/drivers/base/power/main.c
===================================================================

drivers/base/power/main.c +9 -0
@@ -59,6 +59,13 @@ static DECLARE_RWSEM(pm_sleep_rwsem);
 
 int (*platform_enable_wakeup)(struct device *dev, int is_on);
 
+static struct task_struct *suspending_task;
+
+bool in_suspend_context(void)
+{
+	return (suspending_task == current);
+}
+
 /**
  *	device_pm_add - add a device to the list of active devices
  *	@dev:	Device to be added to the list
@@ -272,6 +279,7 @@ static void dpm_resume(void)
 		mutex_lock(&dpm_list_mtx);
 	}
 	mutex_unlock(&dpm_list_mtx);
+	suspending_task = NULL;
 }
 
 /**
@@ -460,6 +468,7 @@ static int dpm_suspend(pm_message_t stat
 {
 	int error = 0;
 
+	suspending_task = current;
 	mutex_lock(&dpm_list_mtx);
 	while (!list_empty(&dpm_locked)) {
 		struct list_head *entry = dpm_locked.prev;

Index: linux-2.6/drivers/base/core.c
===================================================================

drivers/base/core.c +0 -0
@@ -929,6 +929,11 @@ void device_del(struct device *dev)
 	struct device *parent = dev->parent;
 	struct class_interface *class_intf;
 
Quoted 9 lines

drivers/base/core.c void device_del(struct device *dev) · line 936

Quoted 5 lines
drivers/base/core.c +5 -0
+	if (in_suspend_context()) {
+		get_device(dev);
+		device_pm_schedule_removal(dev);
+		return;
+	}

Why are you grabbing an additional reference to the device here? That would seem to get out of balance when the device is later scheduled for removal, right?

No, IMO the reference is necessary, because unregister_dropped_devices() uses device_unregister() that does the put_device() eventually.

If we are called by device_unregister(), the get_device() is needed to balance the put_device() that will be called by device_unregister() after we return.

OTOH, if we are called directly, then we need to balance the put_device() that will be done by device_unregister() called from unregister_dropped_devices().

Thanks,
Rafael

#5 lore raw
Quoted 10 lines · Rafael J. Wysocki
Quoted 9 lines · Greg KH
Quoted 8 lines · Rafael J. Wysocki

Hi Greg,

The appended patch fixes the issue with the new code for suspending/resuming devices, related to the fact that some device drivers and CPU hotplug notifiers unregister device objects while suspend is in progress, which leads to deadlocks.

Please consider taking it for 2.6.25.

Ouch, please disregard this patch.

Alan rightfully noticed that it may confuse subsystems assuming that after device_unregister() has returned, the driver's ->release() method has run.

Unfortunately, he did that in a Bugzilla comment that has never reached my mailbox (strangely enough, I haven't been receiving any messages from the Bugzilla for the last two days or so).

Sorry for the trouble.

Thanks,
Rafael

Quoted 87 lines
Quoted 59 lines
Quoted 59 lines

---
From: Rafael J. Wysocki <[email protected]>

Introduce a mechanism preventing drivers and CPU hotplug notifiers from deadlocking suspend/hibernation by unregistering device objects while it is in progress. Specifically, make device_del() detect if it has been called by the suspending task and automatically defer the removal of the device object if that's the case.

---
 drivers/base/core.c        |    5 +++++
 drivers/base/power/main.c  |    9 +++++++++
 drivers/base/power/power.h |    5 +++++
 3 files changed, 19 insertions(+)

Index: linux-2.6/drivers/base/power/main.c
===================================================================

drivers/base/power/main.c +9 -0
@@ -59,6 +59,13 @@ static DECLARE_RWSEM(pm_sleep_rwsem);
 
 int (*platform_enable_wakeup)(struct device *dev, int is_on);
 
+static struct task_struct *suspending_task;
+
+bool in_suspend_context(void)
+{
+	return (suspending_task == current);
+}
+
 /**
  *	device_pm_add - add a device to the list of active devices
  *	@dev:	Device to be added to the list
@@ -272,6 +279,7 @@ static void dpm_resume(void)
 		mutex_lock(&dpm_list_mtx);
 	}
 	mutex_unlock(&dpm_list_mtx);
+	suspending_task = NULL;
 }
 
 /**
@@ -460,6 +468,7 @@ static int dpm_suspend(pm_message_t stat
 {
 	int error = 0;
 
+	suspending_task = current;
 	mutex_lock(&dpm_list_mtx);
 	while (!list_empty(&dpm_locked)) {
 		struct list_head *entry = dpm_locked.prev;

Index: linux-2.6/drivers/base/core.c
===================================================================

drivers/base/core.c +0 -0
@@ -929,6 +929,11 @@ void device_del(struct device *dev)
 	struct device *parent = dev->parent;
 	struct class_interface *class_intf;
 
Quoted 9 lines

drivers/base/core.c void device_del(struct device *dev) · line 936

Quoted 5 lines
drivers/base/core.c +5 -0
+	if (in_suspend_context()) {
+		get_device(dev);
+		device_pm_schedule_removal(dev);
+		return;
+	}

Why are you grabbing an additional reference to the device here? That would seem to get out of balance when the device is later scheduled for removal, right?

No, IMO the reference is necessary, because unregister_dropped_devices() uses device_unregister() that does the put_device() eventually.

If we are called by device_unregister(), the get_device() is needed to balance the put_device() that will be called by device_unregister() after we return.

OTOH, if we are called directly, then we need to balance the put_device() that will be done by device_unregister() called from unregister_dropped_devices().

Thanks,
Rafael
-
To unsubscribe from this list: send the line "unsubscribe linux-acpi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 1 line
"Premature optimization is the root of all evil." - Donald Knuth
#6 lore raw
In reply to Rafael J. Wysocki
Quoted 10 lines · Rafael J. Wysocki
Quoted 9 lines · Greg KH
Quoted 8 lines · Rafael J. Wysocki

Hi Greg,

The appended patch fixes the issue with the new code for suspending/resuming devices, related to the fact that some device drivers and CPU hotplug notifiers unregister device objects while suspend is in progress, which leads to deadlocks.

Please consider taking it for 2.6.25.

Ouch, please disregard this patch.

Alan rightfully noticed that it may confuse subsystems assuming that after device_unregister() has returned, the driver's ->release() method has run.

Unfortunately, he did that in a Bugzilla comment that has never reached my mailbox (strangely enough, I haven't been receiving any messages from the Bugzilla for the last two days or so).

Sorry for the trouble.

Thanks,
Rafael

Quoted 87 lines
Quoted 59 lines
Quoted 59 lines

---
From: Rafael J. Wysocki <[email protected]>

Introduce a mechanism preventing drivers and CPU hotplug notifiers from deadlocking suspend/hibernation by unregistering device objects while it is in progress. Specifically, make device_del() detect if it has been called by the suspending task and automatically defer the removal of the device object if that's the case.

---
 drivers/base/core.c        |    5 +++++
 drivers/base/power/main.c  |    9 +++++++++
 drivers/base/power/power.h |    5 +++++
 3 files changed, 19 insertions(+)

Index: linux-2.6/drivers/base/power/main.c
===================================================================

drivers/base/power/main.c +9 -0
@@ -59,6 +59,13 @@ static DECLARE_RWSEM(pm_sleep_rwsem);
 
 int (*platform_enable_wakeup)(struct device *dev, int is_on);
 
+static struct task_struct *suspending_task;
+
+bool in_suspend_context(void)
+{
+	return (suspending_task == current);
+}
+
 /**
  *	device_pm_add - add a device to the list of active devices
  *	@dev:	Device to be added to the list
@@ -272,6 +279,7 @@ static void dpm_resume(void)
 		mutex_lock(&dpm_list_mtx);
 	}
 	mutex_unlock(&dpm_list_mtx);
+	suspending_task = NULL;
 }
 
 /**
@@ -460,6 +468,7 @@ static int dpm_suspend(pm_message_t stat
 {
 	int error = 0;
 
+	suspending_task = current;
 	mutex_lock(&dpm_list_mtx);
 	while (!list_empty(&dpm_locked)) {
 		struct list_head *entry = dpm_locked.prev;

Index: linux-2.6/drivers/base/core.c
===================================================================

drivers/base/core.c +0 -0
@@ -929,6 +929,11 @@ void device_del(struct device *dev)
 	struct device *parent = dev->parent;
 	struct class_interface *class_intf;
 
Quoted 9 lines

drivers/base/core.c void device_del(struct device *dev) · line 936

Quoted 5 lines
drivers/base/core.c +5 -0
+	if (in_suspend_context()) {
+		get_device(dev);
+		device_pm_schedule_removal(dev);
+		return;
+	}

Why are you grabbing an additional reference to the device here? That would seem to get out of balance when the device is later scheduled for removal, right?

No, IMO the reference is necessary, because unregister_dropped_devices() uses device_unregister() that does the put_device() eventually.

If we are called by device_unregister(), the get_device() is needed to balance the put_device() that will be called by device_unregister() after we return.

OTOH, if we are called directly, then we need to balance the put_device() that will be done by device_unregister() called from unregister_dropped_devices().

Thanks,
Rafael
-
To unsubscribe from this list: send the line "unsubscribe linux-acpi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 1 line
"Premature optimization is the root of all evil." - Donald Knuth
#7 lore raw
Quoted 14 lines · Rafael J. Wysocki
Quoted 10 lines · Rafael J. Wysocki
Quoted 9 lines · Greg KH
Quoted 8 lines · Rafael J. Wysocki

Hi Greg,

The appended patch fixes the issue with the new code for suspending/resuming devices, related to the fact that some device drivers and CPU hotplug notifiers unregister device objects while suspend is in progress, which leads to deadlocks.

Please consider taking it for 2.6.25.

Ouch, please disregard this patch.

Quoted 8 lines

Alan rightfully noticed that it may confuse subsystems assuming that after device_unregister() has returned, the driver's ->release() method has run.

Unfortunately, he did that in a Bugzilla comment that has never reached my mailbox (strangely enough, I haven't been receiving any messages from the Bugzilla for the last two days or so).

Sorry for the trouble.

No problem at all.

thanks,

greg k-h

#8 lore raw
In reply to Rafael J. Wysocki
Quoted 14 lines · Rafael J. Wysocki
Quoted 10 lines · Rafael J. Wysocki
Quoted 9 lines · Greg KH
Quoted 8 lines · Rafael J. Wysocki

Hi Greg,

The appended patch fixes the issue with the new code for suspending/resuming devices, related to the fact that some device drivers and CPU hotplug notifiers unregister device objects while suspend is in progress, which leads to deadlocks.

Please consider taking it for 2.6.25.

Ouch, please disregard this patch.

Quoted 8 lines

Alan rightfully noticed that it may confuse subsystems assuming that after device_unregister() has returned, the driver's ->release() method has run.

Unfortunately, he did that in a Bugzilla comment that has never reached my mailbox (strangely enough, I haven't been receiving any messages from the Bugzilla for the last two days or so).

Sorry for the trouble.

No problem at all.

thanks,

greg k-h