Skip to contentSkip to conversation

PM: Make PM core handle device registrations concurrent with suspend/hibernation

RFC PATCH In review Rafael J. Wysocki 3 participants 33 messages Mar 3, 2008 – Mar 6, 2008

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

RFC PATCH 1/1 PM: Make PM core handle device registrations concurrent with suspend/hibernation

Hi,

The appended patch is intended to fix the issue with the PM core that it allows device registrations to complete successfully even if they run concurrently with the suspending of their parents, which may lead to a wrong ordering of devices on the dpm_active list and, as a result, to failures during suspend and hibernation transitions.

Comments welcome.

Thanks,
Rafael

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

Modify the PM core to protect its data structures, specifically the dpm_active list, from being corrupted if a child of the currently suspending device is registered concurrently with its ->suspend() callback. In that case, since the new device (the child) is added to dpm_active after its parent, the PM core will attempt to suspend it after the parent, which is wrong.

Introduce a new member of struct dev_pm_info, called 'sleeping', and use it to check if the parent of the device being added to dpm_active has been suspended, in which case the device registration fails. Also, use 'sleeping' for checking if the ordering of devices on dpm_active is correct.

Remove pm_sleep_rwsem which is not necessary any more.

Special thanks to Alan Stern for discussions and suggestions that lead to the creation of this patch.

---
 Documentation/power/devices.txt |    4 +++
 drivers/base/core.c             |    6 ++++-
 drivers/base/power/main.c       |   47 ++++++++++++----------------------------
 drivers/base/power/power.h      |   23 ++-----------------
 include/linux/pm.h              |    1 
 5 files changed, 28 insertions(+), 53 deletions(-)
include/linux/pm.h +1 -0
Index: linux-2.6/include/linux/pm.h
===================================================================
@@ -186,6 +186,7 @@ struct dev_pm_info {
 #ifdef	CONFIG_PM_SLEEP
 	unsigned		should_wakeup:1;
 	struct list_head	entry;
+	bool			sleeping;	/* Owned by the PM core */
 #endif
 };
 
drivers/base/power/main.c +15 -32

101 lines not on this page · raw message

drivers/base/power/power.h +3 -20
Index: linux-2.6/drivers/base/power/power.h
===================================================================
@@ -11,30 +11,13 @@ static inline struct device *to_device(s
 	return container_of(entry, struct device, power.entry);
 }
 
-extern void device_pm_add(struct device *);
+extern int device_pm_add(struct device *);
 extern void device_pm_remove(struct device *);
-extern int pm_sleep_lock(void);
-extern void pm_sleep_unlock(void);
 
 #else /* CONFIG_PM_SLEEP */
 
-
-static inline void device_pm_add(struct device *dev)
-{
-}
-
-static inline void device_pm_remove(struct device *dev)
-{
-}
-
-static inline int pm_sleep_lock(void)
-{
-	return 0;
-}
-
-static inline void pm_sleep_unlock(void)
-{
-}
+static inline int device_pm_add(struct device *dev) { return 0; }
+static inline void device_pm_remove(struct device *dev) {}
 
 #endif
 
drivers/base/core.c +5 -1
Index: linux-2.6/drivers/base/core.c
===================================================================
@@ -814,7 +814,11 @@ int device_add(struct device *dev)
 	error = dpm_sysfs_add(dev);
 	if (error)
 		goto PMError;
-	device_pm_add(dev);
+	error = device_pm_add(dev);
+	if (error) {
+		dpm_sysfs_remove(dev);
+		goto PMError;
+	}
 	error = bus_add_device(dev);
 	if (error)
 		goto BusError;
Documentation/power/devices.txt +4 -0
Index: linux-2.6/Documentation/power/devices.txt
===================================================================
@@ -196,6 +196,10 @@ its parent; and can't be removed or susp
 
 The policy is that the device tree should match hardware bus topology.
 (Or at least the control bus, for devices which use multiple busses.)
+In particular, this means that a device registration may fail if the parent of
+the device is suspending (ie. has just been chosen by the PM core as the next
+device to suspend) or has already suspended.  Device drivers must be prepared
+to cope with such situations.
 
 
 Suspending Devices
#1 lore raw
Quoted 15 lines · Rafael J. Wysocki

Hi,

The appended patch is intended to fix the issue with the PM core that it allows device registrations to complete successfully even if they run concurrently with the suspending of their parents, which may lead to a wrong ordering of devices on the dpm_active list and, as a result, to failures during suspend and hibernation transitions.

Comments welcome.

Index: linux-2.6/include/linux/pm.h
===================================================================

include/linux/pm.h struct dev_pm_info { · line 191

Quoted 6 lines
include/linux/pm.h +1 -0
@@ -186,6 +186,7 @@ struct dev_pm_info {
 #ifdef	CONFIG_PM_SLEEP
 	unsigned		should_wakeup:1;
 	struct list_head	entry;
+	bool			sleeping;	/* Owned by the PM core */
 #endif
 };

Drivers might want to use this field without having to add protective "#ifdef CONFIG_PM_SLEEP" lines. You can change it to a single-bit bitfield and place it adjacent to can_wakeup.

include/linux/pm.h

Quoted 12 lines
include/linux/pm.h +1 -1
-void device_pm_add(struct device *dev)
+int device_pm_add(struct device *dev)

{

include/linux/pm.h +4 -1
+	int error = 0;
+
 	pr_debug("PM: Adding info for %s:%s\n",
 		 dev->bus ? dev->bus->name : "No Bus",
 		 kobject_name(&dev->kobj));
 	mutex_lock(&dpm_list_mtx);
-	list_add_tail(&dev->power.entry, &dpm_active);
+	if (dev->parent && dev->parent->power.sleeping)
+		error = -EBUSY;

Add a stack dump? When this isn't a race, it's the kind of bug we want to fix.

Quoted 7 lines
include/linux/pm.h +3 -0
+	else
+		list_add_tail(&dev->power.entry, &dpm_active);
 	mutex_unlock(&dpm_list_mtx);
+	return error;
 }

include/linux/pm.h static int dpm_suspend(pm_message_t stat · line 410

Quoted 7 lines
include/linux/pm.h +4 -0
@@ -426,6 +404,11 @@ static int dpm_suspend(pm_message_t stat
 		struct list_head *entry = dpm_active.prev;
 		struct device *dev = to_device(entry);
 
+		if (dev->parent && dev->parent->power.sleeping) {
+			error = -EAGAIN;
+			break;
+		}

It's not clear that we want to have this check. It would cause problems if the device ordering got mixed up by device_move(), which is pretty much the only way it could be triggered.

If you do want to leave it in, add a stack dump (and perhaps make it not return an error). This would help force people to figure out safe ways to use device_move().

include/linux/pm.h static int dpm_suspend(pm_message_t stat · line 411

Quoted 1 line
include/linux/pm.h +1 -0
+		dev->power.sleeping = true;;

Extra ';'.

Alan Stern

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

Hi,

The appended patch is intended to fix the issue with the PM core that it allows device registrations to complete successfully even if they run concurrently with the suspending of their parents, which may lead to a wrong ordering of devices on the dpm_active list and, as a result, to failures during suspend and hibernation transitions.

Comments welcome.

Index: linux-2.6/include/linux/pm.h
===================================================================

include/linux/pm.h struct dev_pm_info { · line 191

Quoted 6 lines
include/linux/pm.h +1 -0
@@ -186,6 +186,7 @@ struct dev_pm_info {
 #ifdef	CONFIG_PM_SLEEP
 	unsigned		should_wakeup:1;
 	struct list_head	entry;
+	bool			sleeping;	/* Owned by the PM core */
 #endif
 };

Drivers might want to use this field without having to add protective "#ifdef CONFIG_PM_SLEEP" lines. You can change it to a single-bit bitfield and place it adjacent to can_wakeup.

include/linux/pm.h

Quoted 12 lines
include/linux/pm.h +1 -1
-void device_pm_add(struct device *dev)
+int device_pm_add(struct device *dev)

{

include/linux/pm.h +4 -1
+	int error = 0;
+
 	pr_debug("PM: Adding info for %s:%s\n",
 		 dev->bus ? dev->bus->name : "No Bus",
 		 kobject_name(&dev->kobj));
 	mutex_lock(&dpm_list_mtx);
-	list_add_tail(&dev->power.entry, &dpm_active);
+	if (dev->parent && dev->parent->power.sleeping)
+		error = -EBUSY;

Add a stack dump? When this isn't a race, it's the kind of bug we want to fix.

Quoted 7 lines
include/linux/pm.h +3 -0
+	else
+		list_add_tail(&dev->power.entry, &dpm_active);
 	mutex_unlock(&dpm_list_mtx);
+	return error;
 }

include/linux/pm.h static int dpm_suspend(pm_message_t stat · line 410

Quoted 7 lines
include/linux/pm.h +4 -0
@@ -426,6 +404,11 @@ static int dpm_suspend(pm_message_t stat
 		struct list_head *entry = dpm_active.prev;
 		struct device *dev = to_device(entry);
 
+		if (dev->parent && dev->parent->power.sleeping) {
+			error = -EAGAIN;
+			break;
+		}

It's not clear that we want to have this check. It would cause problems if the device ordering got mixed up by device_move(), which is pretty much the only way it could be triggered.

If you do want to leave it in, add a stack dump (and perhaps make it not return an error). This would help force people to figure out safe ways to use device_move().

include/linux/pm.h static int dpm_suspend(pm_message_t stat · line 411

Quoted 1 line
include/linux/pm.h +1 -0
+		dev->power.sleeping = true;;

Extra ';'.

Alan Stern

#3 lore raw
Quoted 12 lines · Alan Stern
Quoted 9 lines · Rafael J. Wysocki

Hi,

The appended patch is intended to fix the issue with the PM core that it allows device registrations to complete successfully even if they run concurrently with the suspending of their parents, which may lead to a wrong ordering of devices on the dpm_active list and, as a result, to failures during suspend and hibernation transitions.

Comments welcome.

Quoted 14 lines

include/linux/pm.h struct dev_pm_info { · line 191

Quoted 11 lines

Index: linux-2.6/include/linux/pm.h
===================================================================

include/linux/pm.h +1 -0
@@ -186,6 +186,7 @@ struct dev_pm_info {
 #ifdef	CONFIG_PM_SLEEP
 	unsigned		should_wakeup:1;
 	struct list_head	entry;
+	bool			sleeping;	/* Owned by the PM core */
 #endif
 };

Drivers might want to use this field without having to add protective "#ifdef CONFIG_PM_SLEEP" lines.

I guess you mean that driver writers may not want to make each piece of code referring to this flag depend on CONFIG_PM_SLEEP. Any driver that actually writes to it will be buggy ...

Quoted 2 lines

You can change it to a single-bit bitfield and place it adjacent to can_wakeup.

Yeah, good catch. I will.

Quoted 7 lines
Quoted 7 lines
include/linux/pm.h +1 -1
-void device_pm_add(struct device *dev)
+int device_pm_add(struct device *dev)

{

include/linux/pm.h +2 -0
+	int error = 0;
+
 	pr_debug("PM: Adding info for %s:%s\n",
 		 dev->bus ? dev->bus->name : "No Bus",
Quoted 8 lines

include/linux/pm.h

Quoted 5 lines
include/linux/pm.h +2 -1
 		 kobject_name(&dev->kobj));
 	mutex_lock(&dpm_list_mtx);
-	list_add_tail(&dev->power.entry, &dpm_active);
+	if (dev->parent && dev->parent->power.sleeping)
+		error = -EBUSY;

Add a stack dump? When this isn't a race, it's the kind of bug we want to fix.

Yes, I'll do that.

Quoted 10 lines
Quoted 10 lines
include/linux/pm.h +3 -0
+	else
+		list_add_tail(&dev->power.entry, &dpm_active);
 	mutex_unlock(&dpm_list_mtx);
+	return error;
 }
@@ -426,6 +404,11 @@ static int dpm_suspend(pm_message_t stat
 		struct list_head *entry = dpm_active.prev;
 		struct device *dev = to_device(entry);
 
Quoted 8 lines

include/linux/pm.h static int dpm_suspend(pm_message_t stat · line 410

Quoted 4 lines
include/linux/pm.h +4 -0
+		if (dev->parent && dev->parent->power.sleeping) {
+			error = -EAGAIN;
+			break;
+		}

It's not clear that we want to have this check. It would cause problems if the device ordering got mixed up by device_move(), which is pretty much the only way it could be triggered.

Well, isn't it actually dangerous to suspend parents before their children? What happens, for example, if putting the parent into D3 causes power to be cut from the children and then we attempt to suspend them?

Quoted 3 lines

If you do want to leave it in, add a stack dump (and perhaps make it not return an error). This would help force people to figure out safe ways to use device_move().

Yes, I'll add a stack dump here.

In fact, I'm going to add WARN_ON(true) in both places.

Quoted 3 lines

include/linux/pm.h static int dpm_suspend(pm_message_t stat · line 411

Quoted 1 line
include/linux/pm.h +1 -0
+		dev->power.sleeping = true;;

Extra ';'.

Hah, thanks, will remove.

Rafael

#4 lore raw
In reply to Alan Stern
Quoted 12 lines · Alan Stern
Quoted 9 lines · Rafael J. Wysocki

Hi,

The appended patch is intended to fix the issue with the PM core that it allows device registrations to complete successfully even if they run concurrently with the suspending of their parents, which may lead to a wrong ordering of devices on the dpm_active list and, as a result, to failures during suspend and hibernation transitions.

Comments welcome.

Quoted 14 lines

include/linux/pm.h struct dev_pm_info { · line 191

Quoted 11 lines

Index: linux-2.6/include/linux/pm.h
===================================================================

include/linux/pm.h +1 -0
@@ -186,6 +186,7 @@ struct dev_pm_info {
 #ifdef	CONFIG_PM_SLEEP
 	unsigned		should_wakeup:1;
 	struct list_head	entry;
+	bool			sleeping;	/* Owned by the PM core */
 #endif
 };

Drivers might want to use this field without having to add protective "#ifdef CONFIG_PM_SLEEP" lines.

I guess you mean that driver writers may not want to make each piece of code referring to this flag depend on CONFIG_PM_SLEEP. Any driver that actually writes to it will be buggy ...

Quoted 2 lines

You can change it to a single-bit bitfield and place it adjacent to can_wakeup.

Yeah, good catch. I will.

Quoted 7 lines
Quoted 7 lines
include/linux/pm.h +1 -1
-void device_pm_add(struct device *dev)
+int device_pm_add(struct device *dev)

{

include/linux/pm.h +2 -0
+	int error = 0;
+
 	pr_debug("PM: Adding info for %s:%s\n",
 		 dev->bus ? dev->bus->name : "No Bus",
Quoted 8 lines

include/linux/pm.h

Quoted 5 lines
include/linux/pm.h +2 -1
 		 kobject_name(&dev->kobj));
 	mutex_lock(&dpm_list_mtx);
-	list_add_tail(&dev->power.entry, &dpm_active);
+	if (dev->parent && dev->parent->power.sleeping)
+		error = -EBUSY;

Add a stack dump? When this isn't a race, it's the kind of bug we want to fix.

Yes, I'll do that.

Quoted 10 lines
Quoted 10 lines
include/linux/pm.h +3 -0
+	else
+		list_add_tail(&dev->power.entry, &dpm_active);
 	mutex_unlock(&dpm_list_mtx);
+	return error;
 }
@@ -426,6 +404,11 @@ static int dpm_suspend(pm_message_t stat
 		struct list_head *entry = dpm_active.prev;
 		struct device *dev = to_device(entry);
 
Quoted 8 lines

include/linux/pm.h static int dpm_suspend(pm_message_t stat · line 410

Quoted 4 lines
include/linux/pm.h +4 -0
+		if (dev->parent && dev->parent->power.sleeping) {
+			error = -EAGAIN;
+			break;
+		}

It's not clear that we want to have this check. It would cause problems if the device ordering got mixed up by device_move(), which is pretty much the only way it could be triggered.

Well, isn't it actually dangerous to suspend parents before their children? What happens, for example, if putting the parent into D3 causes power to be cut from the children and then we attempt to suspend them?

Quoted 3 lines

If you do want to leave it in, add a stack dump (and perhaps make it not return an error). This would help force people to figure out safe ways to use device_move().

Yes, I'll add a stack dump here.

In fact, I'm going to add WARN_ON(true) in both places.

Quoted 3 lines

include/linux/pm.h static int dpm_suspend(pm_message_t stat · line 411

Quoted 1 line
include/linux/pm.h +1 -0
+		dev->power.sleeping = true;;

Extra ';'.

Hah, thanks, will remove.

Rafael

#5 lore raw
Quoted 7 lines · Rafael J. Wysocki
Quoted 3 lines

It's not clear that we want to have this check. It would cause problems if the device ordering got mixed up by device_move(), which is pretty much the only way it could be triggered.

Well, isn't it actually dangerous to suspend parents before their children? What happens, for example, if putting the parent into D3 causes power to be cut from the children and then we attempt to suspend them?

It depends on the devices involved. For PCI devices, obviously there will be problems of the sort you described. But for other devices there might not be.

For example, the USB Bluetooth driver will sometimes create a new TTY device and then move the existing Bluetooth device underneath it (this description is oversimplified and probably wrong in some important respects, but you get the idea). Suspending the Bluetooth device before suspending the TTY won't cause any power-related problems, because a TTY is a purely logical device with no power implications at all.

There might still be logical problems, of course... We need to add a mechanism for reordering the dpm_active list when device_move() is called. But first we need to get in touch with the people using device_move() -- basically just Marcel and Cornelia -- and see what sort of mechanism they will need.

Alan Stern

#6 lore raw
Quoted 5 lines · Alan Stern

There might still be logical problems, of course... We need to add a mechanism for reordering the dpm_active list when device_move() is called. But first we need to get in touch with the people using device_move() -- basically just Marcel and Cornelia -- and see what sort of mechanism they will need.

On s390 we currently don't do anything suspend related at all - I'd need to think about that. (I use device_move() to reflect topology changes on the (virtual) hardware, so I'd guess the "don't suspend parent before child rule" should still apply afterwards. But of course, that also depends on what we actually need to do on the parent's bus and what on the child's bus.)

#7 lore raw
In reply to Alan Stern
Quoted 5 lines · Alan Stern

There might still be logical problems, of course... We need to add a mechanism for reordering the dpm_active list when device_move() is called. But first we need to get in touch with the people using device_move() -- basically just Marcel and Cornelia -- and see what sort of mechanism they will need.

On s390 we currently don't do anything suspend related at all - I'd need to think about that. (I use device_move() to reflect topology changes on the (virtual) hardware, so I'd guess the "don't suspend parent before child rule" should still apply afterwards. But of course, that also depends on what we actually need to do on the parent's bus and what on the child's bus.)

#8 lore raw
In reply to Alan Stern
Quoted 12 lines · Alan Stern
Quoted 9 lines · Rafael J. Wysocki

Hi,

The appended patch is intended to fix the issue with the PM core that it allows device registrations to complete successfully even if they run concurrently with the suspending of their parents, which may lead to a wrong ordering of devices on the dpm_active list and, as a result, to failures during suspend and hibernation transitions.

Comments welcome.

Quoted 15 lines

include/linux/pm.h struct dev_pm_info { · line 191

Quoted 11 lines

Index: linux-2.6/include/linux/pm.h
===================================================================

include/linux/pm.h +1 -0
@@ -186,6 +186,7 @@ struct dev_pm_info {
 #ifdef	CONFIG_PM_SLEEP
 	unsigned		should_wakeup:1;
 	struct list_head	entry;
+	bool			sleeping;	/* Owned by the PM core */
 #endif
 };

Drivers might want to use this field without having to add protective "#ifdef CONFIG_PM_SLEEP" lines. You can change it to a single-bit bitfield and place it adjacent to can_wakeup.

Below is a refined version that should address all of your comments. Please have a look.

Thanks,
Rafael

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

Modify the PM core to protect its data structures, specifically the dpm_active list, from being corrupted if a child of the currently suspending device is registered concurrently with its ->suspend() callback. In that case, since the new device (the child) is added to dpm_active after its parent, the PM core will attempt to suspend it after the parent, which is wrong.

Introduce a new member of struct dev_pm_info, called 'sleeping', and use it to check if the parent of the device being added to dpm_active has been suspended, in which case the device registration fails. Also, use 'sleeping' for checking if the ordering of devices on dpm_active is correct.

Remove pm_sleep_rwsem which is not necessary any more.

Special thanks to Alan Stern for discussions and suggestions that lead to the creation of this patch.

---
 Documentation/power/devices.txt |    4 +++
 drivers/base/core.c             |    6 ++++
 drivers/base/power/main.c       |   50 ++++++++++++++--------------------------
 drivers/base/power/power.h      |   23 ++----------------
 include/linux/pm.h              |    1 
 5 files changed, 31 insertions(+), 53 deletions(-)
include/linux/pm.h +1 -0
Index: linux-2.6/include/linux/pm.h
===================================================================
@@ -183,6 +183,7 @@ typedef struct pm_message {
 struct dev_pm_info {
 	pm_message_t		power_state;
 	unsigned		can_wakeup:1;
+	bool			sleeping:1;	/* Owned by the PM core */
 #ifdef	CONFIG_PM_SLEEP
 	unsigned		should_wakeup:1;
 	struct list_head	entry;
drivers/base/power/main.c +18 -32

104 lines not on this page · raw message

drivers/base/power/power.h +3 -20
Index: linux-2.6/drivers/base/power/power.h
===================================================================
@@ -11,30 +11,13 @@ static inline struct device *to_device(s
 	return container_of(entry, struct device, power.entry);
 }
 
-extern void device_pm_add(struct device *);
+extern int device_pm_add(struct device *);
 extern void device_pm_remove(struct device *);
-extern int pm_sleep_lock(void);
-extern void pm_sleep_unlock(void);
 
 #else /* CONFIG_PM_SLEEP */
 
-
-static inline void device_pm_add(struct device *dev)
-{
-}
-
-static inline void device_pm_remove(struct device *dev)
-{
-}
-
-static inline int pm_sleep_lock(void)
-{
-	return 0;
-}
-
-static inline void pm_sleep_unlock(void)
-{
-}
+static inline int device_pm_add(struct device *dev) { return 0; }
+static inline void device_pm_remove(struct device *dev) {}
 
 #endif
 
drivers/base/core.c +5 -1
Index: linux-2.6/drivers/base/core.c
===================================================================
@@ -814,7 +814,11 @@ int device_add(struct device *dev)
 	error = dpm_sysfs_add(dev);
 	if (error)
 		goto PMError;
-	device_pm_add(dev);
+	error = device_pm_add(dev);
+	if (error) {
+		dpm_sysfs_remove(dev);
+		goto PMError;
+	}
 	error = bus_add_device(dev);
 	if (error)
 		goto BusError;
Documentation/power/devices.txt +4 -0
Index: linux-2.6/Documentation/power/devices.txt
===================================================================
@@ -196,6 +196,10 @@ its parent; and can't be removed or susp
 
 The policy is that the device tree should match hardware bus topology.
 (Or at least the control bus, for devices which use multiple busses.)
+In particular, this means that a device registration may fail if the parent of
+the device is suspending (ie. has been chosen by the PM core as the next
+device to suspend) or has already suspended.  Device drivers must be prepared
+to cope with such situations.
 
 
 Suspending Devices
#9 lore raw
In reply to Alan Stern
Quoted 12 lines · Alan Stern
Quoted 9 lines · Rafael J. Wysocki

Hi,

The appended patch is intended to fix the issue with the PM core that it allows device registrations to complete successfully even if they run concurrently with the suspending of their parents, which may lead to a wrong ordering of devices on the dpm_active list and, as a result, to failures during suspend and hibernation transitions.

Comments welcome.

Quoted 15 lines

include/linux/pm.h struct dev_pm_info { · line 191

Quoted 11 lines

Index: linux-2.6/include/linux/pm.h
===================================================================

include/linux/pm.h +1 -0
@@ -186,6 +186,7 @@ struct dev_pm_info {
 #ifdef	CONFIG_PM_SLEEP
 	unsigned		should_wakeup:1;
 	struct list_head	entry;
+	bool			sleeping;	/* Owned by the PM core */
 #endif
 };

Drivers might want to use this field without having to add protective "#ifdef CONFIG_PM_SLEEP" lines. You can change it to a single-bit bitfield and place it adjacent to can_wakeup.

Below is a refined version that should address all of your comments. Please have a look.

Thanks,
Rafael

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

Modify the PM core to protect its data structures, specifically the dpm_active list, from being corrupted if a child of the currently suspending device is registered concurrently with its ->suspend() callback. In that case, since the new device (the child) is added to dpm_active after its parent, the PM core will attempt to suspend it after the parent, which is wrong.

Introduce a new member of struct dev_pm_info, called 'sleeping', and use it to check if the parent of the device being added to dpm_active has been suspended, in which case the device registration fails. Also, use 'sleeping' for checking if the ordering of devices on dpm_active is correct.

Remove pm_sleep_rwsem which is not necessary any more.

Special thanks to Alan Stern for discussions and suggestions that lead to the creation of this patch.

---
 Documentation/power/devices.txt |    4 +++
 drivers/base/core.c             |    6 ++++
 drivers/base/power/main.c       |   50 ++++++++++++++--------------------------
 drivers/base/power/power.h      |   23 ++----------------
 include/linux/pm.h              |    1 
 5 files changed, 31 insertions(+), 53 deletions(-)
include/linux/pm.h +1 -0
Index: linux-2.6/include/linux/pm.h
===================================================================
@@ -183,6 +183,7 @@ typedef struct pm_message {
 struct dev_pm_info {
 	pm_message_t		power_state;
 	unsigned		can_wakeup:1;
+	bool			sleeping:1;	/* Owned by the PM core */
 #ifdef	CONFIG_PM_SLEEP
 	unsigned		should_wakeup:1;
 	struct list_head	entry;
drivers/base/power/main.c +18 -32

104 lines not on this page · raw message

drivers/base/power/power.h +3 -20
Index: linux-2.6/drivers/base/power/power.h
===================================================================
@@ -11,30 +11,13 @@ static inline struct device *to_device(s
 	return container_of(entry, struct device, power.entry);
 }
 
-extern void device_pm_add(struct device *);
+extern int device_pm_add(struct device *);
 extern void device_pm_remove(struct device *);
-extern int pm_sleep_lock(void);
-extern void pm_sleep_unlock(void);
 
 #else /* CONFIG_PM_SLEEP */
 
-
-static inline void device_pm_add(struct device *dev)
-{
-}
-
-static inline void device_pm_remove(struct device *dev)
-{
-}
-
-static inline int pm_sleep_lock(void)
-{
-	return 0;
-}
-
-static inline void pm_sleep_unlock(void)
-{
-}
+static inline int device_pm_add(struct device *dev) { return 0; }
+static inline void device_pm_remove(struct device *dev) {}
 
 #endif
 
drivers/base/core.c +5 -1
Index: linux-2.6/drivers/base/core.c
===================================================================
@@ -814,7 +814,11 @@ int device_add(struct device *dev)
 	error = dpm_sysfs_add(dev);
 	if (error)
 		goto PMError;
-	device_pm_add(dev);
+	error = device_pm_add(dev);
+	if (error) {
+		dpm_sysfs_remove(dev);
+		goto PMError;
+	}
 	error = bus_add_device(dev);
 	if (error)
 		goto BusError;
Documentation/power/devices.txt +4 -0
Index: linux-2.6/Documentation/power/devices.txt
===================================================================
@@ -196,6 +196,10 @@ its parent; and can't be removed or susp
 
 The policy is that the device tree should match hardware bus topology.
 (Or at least the control bus, for devices which use multiple busses.)
+In particular, this means that a device registration may fail if the parent of
+the device is suspending (ie. has been chosen by the PM core as the next
+device to suspend) or has already suspended.  Device drivers must be prepared
+to cope with such situations.
 
 
 Suspending Devices
#10 lore raw

drivers/base/power/main.c static LIST_HEAD(dpm_destroy); · line 72

Quoted 12 lines · Rafael J. Wysocki
drivers/base/power/main.c +5 -2
-void device_pm_add(struct device *dev)
+int device_pm_add(struct device *dev)
 {
+	int error = 0;
+
 	pr_debug("PM: Adding info for %s:%s\n",
 		 dev->bus ? dev->bus->name : "No Bus",
 		 kobject_name(&dev->kobj));
 	mutex_lock(&dpm_list_mtx);
-	list_add_tail(&dev->power.entry, &dpm_active);
+	if (dev->parent && dev->parent->power.sleeping) {
+		WARN_ON(true);

I would prefer to put a dev_warn() line here, so that people reading the kernel log can easily tell which device caused the problem and what sort of problem it is. Something like this:

		dev_warn(dev, "device added while parent %s is asleep\n",
				dev->parent->bus_id);
		WARN_ON(true);

drivers/base/power/main.c static int dpm_suspend(pm_message_t stat · line 412

Quoted 8 lines
drivers/base/power/main.c +4 -0
@@ -426,6 +406,12 @@ static int dpm_suspend(pm_message_t stat
 		struct list_head *entry = dpm_active.prev;
 		struct device *dev = to_device(entry);
 
+		if (dev->parent && dev->parent->power.sleeping) {
+			WARN_ON(true);
+			error = -EAGAIN;
+			break;

Again, a dev_warn() would be appropriate.

And you might consider taking out the "error = -EAGAIN" and the "break". When this occurs it doesn't mean that devices were suspended in the wrong order; it means that the ordering of the parent pointers fails to match the ordering of dpm_active. The only way for this to happen is if the parent pointers are messed up by device_move() -- dpm_active will still be correct.

The rest of the patch looks fine.

Alan Stern

#11 lore raw
Quoted 19 lines · Alan Stern

Review

Quoted 12 lines · Rafael J. Wysocki
(quoted diff) +5 -2
-void device_pm_add(struct device *dev)
+int device_pm_add(struct device *dev)
 {
+	int error = 0;
+
 	pr_debug("PM: Adding info for %s:%s\n",
 		 dev->bus ? dev->bus->name : "No Bus",
 		 kobject_name(&dev->kobj));
 	mutex_lock(&dpm_list_mtx);
-	list_add_tail(&dev->power.entry, &dpm_active);
+	if (dev->parent && dev->parent->power.sleeping) {
+		WARN_ON(true);

I would prefer to put a dev_warn() line here, so that people reading the kernel log can easily tell which device caused the problem and what sort of problem it is. Something like this:

Quoted 3 lines
		dev_warn(dev, "device added while parent %s is asleep\n",
				dev->parent->bus_id);
		WARN_ON(true);

Added.

Quoted 10 lines

Review static int dpm_suspend(pm_message_t stat · line 412

Quoted 8 lines
(quoted diff) +4 -0
@@ -426,6 +406,12 @@ static int dpm_suspend(pm_message_t stat
 		struct list_head *entry = dpm_active.prev;
 		struct device *dev = to_device(entry);
 
+		if (dev->parent && dev->parent->power.sleeping) {
+			WARN_ON(true);
+			error = -EAGAIN;
+			break;

Again, a dev_warn() would be appropriate.

I chose just "WARN_ON(dev->parent && dev->parent->power.sleeping)", which is shorter. :-)

I don't really expect it to appear and if it's reported, it'll be easy to figure out everything from the stack trace.

Revised patch below.

Thanks,
Rafael

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

Modify the PM core to protect its data structures, specifically the dpm_active list, from being corrupted if a child of the currently suspending device is registered concurrently with its ->suspend() callback. In that case, since the new device (the child) is added to dpm_active after its parent, the PM core will attempt to suspend it after the parent, which is wrong.

Introduce a new member of struct dev_pm_info, called 'sleeping', and use it to check if the parent of the device being added to dpm_active has been suspended, in which case the device registration fails. Also, use 'sleeping' for checking if the ordering of devices on dpm_active is correct.

Remove pm_sleep_rwsem which is not necessary any more.

Special thanks to Alan Stern for discussions and suggestions that lead to the creation of this patch.

---
 Documentation/power/devices.txt |    4 +++
 drivers/base/core.c             |    6 ++++
 drivers/base/power/main.c       |   50 ++++++++++++++--------------------------
 drivers/base/power/power.h      |   23 ++----------------
 include/linux/pm.h              |    1 
 5 files changed, 31 insertions(+), 53 deletions(-)
include/linux/pm.h +1 -0
Index: linux-2.6/include/linux/pm.h
===================================================================
@@ -183,6 +183,7 @@ typedef struct pm_message {
 struct dev_pm_info {
 	pm_message_t		power_state;
 	unsigned		can_wakeup:1;
+	bool			sleeping:1;	/* Owned by the PM core */
 #ifdef	CONFIG_PM_SLEEP
 	unsigned		should_wakeup:1;
 	struct list_head	entry;
drivers/base/power/main.c +18 -32

104 lines not on this page · raw message

drivers/base/power/power.h +3 -20
Index: linux-2.6/drivers/base/power/power.h
===================================================================
@@ -11,30 +11,13 @@ static inline struct device *to_device(s
 	return container_of(entry, struct device, power.entry);
 }
 
-extern void device_pm_add(struct device *);
+extern int device_pm_add(struct device *);
 extern void device_pm_remove(struct device *);
-extern int pm_sleep_lock(void);
-extern void pm_sleep_unlock(void);
 
 #else /* CONFIG_PM_SLEEP */
 
-
-static inline void device_pm_add(struct device *dev)
-{
-}
-
-static inline void device_pm_remove(struct device *dev)
-{
-}
-
-static inline int pm_sleep_lock(void)
-{
-	return 0;
-}
-
-static inline void pm_sleep_unlock(void)
-{
-}
+static inline int device_pm_add(struct device *dev) { return 0; }
+static inline void device_pm_remove(struct device *dev) {}
 
 #endif
 
drivers/base/core.c +5 -1
Index: linux-2.6/drivers/base/core.c
===================================================================
@@ -815,7 +815,11 @@ int device_add(struct device *dev)
 	error = dpm_sysfs_add(dev);
 	if (error)
 		goto PMError;
-	device_pm_add(dev);
+	error = device_pm_add(dev);
+	if (error) {
+		dpm_sysfs_remove(dev);
+		goto PMError;
+	}
 	error = bus_add_device(dev);
 	if (error)
 		goto BusError;
Documentation/power/devices.txt +4 -0
Index: linux-2.6/Documentation/power/devices.txt
===================================================================
@@ -196,6 +196,10 @@ its parent; and can't be removed or susp
 
 The policy is that the device tree should match hardware bus topology.
 (Or at least the control bus, for devices which use multiple busses.)
+In particular, this means that a device registration may fail if the parent of
+the device is suspending (ie. has been chosen by the PM core as the next
+device to suspend) or has already suspended.  Device drivers must be prepared
+to cope with such situations.
 
 
 Suspending Devices
#12 lore raw
In reply to Alan Stern
Quoted 19 lines · Alan Stern

Review

Quoted 12 lines · Rafael J. Wysocki
(quoted diff) +5 -2
-void device_pm_add(struct device *dev)
+int device_pm_add(struct device *dev)
 {
+	int error = 0;
+
 	pr_debug("PM: Adding info for %s:%s\n",
 		 dev->bus ? dev->bus->name : "No Bus",
 		 kobject_name(&dev->kobj));
 	mutex_lock(&dpm_list_mtx);
-	list_add_tail(&dev->power.entry, &dpm_active);
+	if (dev->parent && dev->parent->power.sleeping) {
+		WARN_ON(true);

I would prefer to put a dev_warn() line here, so that people reading the kernel log can easily tell which device caused the problem and what sort of problem it is. Something like this:

Quoted 3 lines
		dev_warn(dev, "device added while parent %s is asleep\n",
				dev->parent->bus_id);
		WARN_ON(true);

Added.

Quoted 10 lines

Review static int dpm_suspend(pm_message_t stat · line 412

Quoted 8 lines
(quoted diff) +4 -0
@@ -426,6 +406,12 @@ static int dpm_suspend(pm_message_t stat
 		struct list_head *entry = dpm_active.prev;
 		struct device *dev = to_device(entry);
 
+		if (dev->parent && dev->parent->power.sleeping) {
+			WARN_ON(true);
+			error = -EAGAIN;
+			break;

Again, a dev_warn() would be appropriate.

I chose just "WARN_ON(dev->parent && dev->parent->power.sleeping)", which is shorter. :-)

I don't really expect it to appear and if it's reported, it'll be easy to figure out everything from the stack trace.

Revised patch below.

Thanks,
Rafael

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

Modify the PM core to protect its data structures, specifically the dpm_active list, from being corrupted if a child of the currently suspending device is registered concurrently with its ->suspend() callback. In that case, since the new device (the child) is added to dpm_active after its parent, the PM core will attempt to suspend it after the parent, which is wrong.

Introduce a new member of struct dev_pm_info, called 'sleeping', and use it to check if the parent of the device being added to dpm_active has been suspended, in which case the device registration fails. Also, use 'sleeping' for checking if the ordering of devices on dpm_active is correct.

Remove pm_sleep_rwsem which is not necessary any more.

Special thanks to Alan Stern for discussions and suggestions that lead to the creation of this patch.

---
 Documentation/power/devices.txt |    4 +++
 drivers/base/core.c             |    6 ++++
 drivers/base/power/main.c       |   50 ++++++++++++++--------------------------
 drivers/base/power/power.h      |   23 ++----------------
 include/linux/pm.h              |    1 
 5 files changed, 31 insertions(+), 53 deletions(-)
include/linux/pm.h +1 -0
Index: linux-2.6/include/linux/pm.h
===================================================================
@@ -183,6 +183,7 @@ typedef struct pm_message {
 struct dev_pm_info {
 	pm_message_t		power_state;
 	unsigned		can_wakeup:1;
+	bool			sleeping:1;	/* Owned by the PM core */
 #ifdef	CONFIG_PM_SLEEP
 	unsigned		should_wakeup:1;
 	struct list_head	entry;
drivers/base/power/main.c +18 -32

104 lines not on this page · raw message

drivers/base/power/power.h +3 -20
Index: linux-2.6/drivers/base/power/power.h
===================================================================
@@ -11,30 +11,13 @@ static inline struct device *to_device(s
 	return container_of(entry, struct device, power.entry);
 }
 
-extern void device_pm_add(struct device *);
+extern int device_pm_add(struct device *);
 extern void device_pm_remove(struct device *);
-extern int pm_sleep_lock(void);
-extern void pm_sleep_unlock(void);
 
 #else /* CONFIG_PM_SLEEP */
 
-
-static inline void device_pm_add(struct device *dev)
-{
-}
-
-static inline void device_pm_remove(struct device *dev)
-{
-}
-
-static inline int pm_sleep_lock(void)
-{
-	return 0;
-}
-
-static inline void pm_sleep_unlock(void)
-{
-}
+static inline int device_pm_add(struct device *dev) { return 0; }
+static inline void device_pm_remove(struct device *dev) {}
 
 #endif
 
drivers/base/core.c +5 -1
Index: linux-2.6/drivers/base/core.c
===================================================================
@@ -815,7 +815,11 @@ int device_add(struct device *dev)
 	error = dpm_sysfs_add(dev);
 	if (error)
 		goto PMError;
-	device_pm_add(dev);
+	error = device_pm_add(dev);
+	if (error) {
+		dpm_sysfs_remove(dev);
+		goto PMError;
+	}
 	error = bus_add_device(dev);
 	if (error)
 		goto BusError;
Documentation/power/devices.txt +4 -0
Index: linux-2.6/Documentation/power/devices.txt
===================================================================
@@ -196,6 +196,10 @@ its parent; and can't be removed or susp
 
 The policy is that the device tree should match hardware bus topology.
 (Or at least the control bus, for devices which use multiple busses.)
+In particular, this means that a device registration may fail if the parent of
+the device is suspending (ie. has been chosen by the PM core as the next
+device to suspend) or has already suspended.  Device drivers must be prepared
+to cope with such situations.
 
 
 Suspending Devices
Acked-by
#13 lore raw
Quoted 37 lines · Rafael J. Wysocki
Quoted 19 lines · Alan Stern

drivers/base/power/main.c static LIST_HEAD(dpm_destroy); · line 75

Quoted 12 lines · Rafael J. Wysocki
drivers/base/power/main.c +5 -2
-void device_pm_add(struct device *dev)
+int device_pm_add(struct device *dev)
 {
+	int error = 0;
+
 	pr_debug("PM: Adding info for %s:%s\n",
 		 dev->bus ? dev->bus->name : "No Bus",
 		 kobject_name(&dev->kobj));
 	mutex_lock(&dpm_list_mtx);
-	list_add_tail(&dev->power.entry, &dpm_active);
+	if (dev->parent && dev->parent->power.sleeping) {
+		WARN_ON(true);

I would prefer to put a dev_warn() line here, so that people reading the kernel log can easily tell which device caused the problem and what sort of problem it is. Something like this:

Quoted 3 lines
		dev_warn(dev, "device added while parent %s is asleep\n",
				dev->parent->bus_id);
		WARN_ON(true);

Added.

Quoted 10 lines

Review static int dpm_suspend(pm_message_t stat · line 412

Quoted 8 lines
(quoted diff) +4 -0
@@ -426,6 +406,12 @@ static int dpm_suspend(pm_message_t stat
 		struct list_head *entry = dpm_active.prev;
 		struct device *dev = to_device(entry);
 
+		if (dev->parent && dev->parent->power.sleeping) {
+			WARN_ON(true);
+			error = -EAGAIN;
+			break;

Again, a dev_warn() would be appropriate.

Quoted 5 lines

I chose just "WARN_ON(dev->parent && dev->parent->power.sleeping)", which is shorter. :-)

I don't really expect it to appear and if it's reported, it'll be easy to figure out everything from the stack trace.

Okay.

Quoted 1 line

Revised patch below.

It looks good. Let's hope it doesn't mess up ACPI too badly... :-)

Alan Stern

Acked-by
#14 lore raw
In reply to Rafael J. Wysocki
Quoted 37 lines · Rafael J. Wysocki
Quoted 19 lines · Alan Stern

drivers/base/power/main.c static LIST_HEAD(dpm_destroy); · line 75

Quoted 12 lines · Rafael J. Wysocki
drivers/base/power/main.c +5 -2
-void device_pm_add(struct device *dev)
+int device_pm_add(struct device *dev)
 {
+	int error = 0;
+
 	pr_debug("PM: Adding info for %s:%s\n",
 		 dev->bus ? dev->bus->name : "No Bus",
 		 kobject_name(&dev->kobj));
 	mutex_lock(&dpm_list_mtx);
-	list_add_tail(&dev->power.entry, &dpm_active);
+	if (dev->parent && dev->parent->power.sleeping) {
+		WARN_ON(true);

I would prefer to put a dev_warn() line here, so that people reading the kernel log can easily tell which device caused the problem and what sort of problem it is. Something like this:

Quoted 3 lines
		dev_warn(dev, "device added while parent %s is asleep\n",
				dev->parent->bus_id);
		WARN_ON(true);

Added.

Quoted 10 lines

Review static int dpm_suspend(pm_message_t stat · line 412

Quoted 8 lines
(quoted diff) +4 -0
@@ -426,6 +406,12 @@ static int dpm_suspend(pm_message_t stat
 		struct list_head *entry = dpm_active.prev;
 		struct device *dev = to_device(entry);
 
+		if (dev->parent && dev->parent->power.sleeping) {
+			WARN_ON(true);
+			error = -EAGAIN;
+			break;

Again, a dev_warn() would be appropriate.

Quoted 5 lines

I chose just "WARN_ON(dev->parent && dev->parent->power.sleeping)", which is shorter. :-)

I don't really expect it to appear and if it's reported, it'll be easy to figure out everything from the stack trace.

Okay.

Quoted 1 line

Revised patch below.

It looks good. Let's hope it doesn't mess up ACPI too badly... :-)

Alan Stern

#15 lore raw
Quoted 45 lines · Alan Stern
Quoted 37 lines · Rafael J. Wysocki
Quoted 19 lines · Alan Stern

Review

Quoted 12 lines · Rafael J. Wysocki
(quoted diff) +5 -2
-void device_pm_add(struct device *dev)
+int device_pm_add(struct device *dev)
 {
+	int error = 0;
+
 	pr_debug("PM: Adding info for %s:%s\n",
 		 dev->bus ? dev->bus->name : "No Bus",
 		 kobject_name(&dev->kobj));
 	mutex_lock(&dpm_list_mtx);
-	list_add_tail(&dev->power.entry, &dpm_active);
+	if (dev->parent && dev->parent->power.sleeping) {
+		WARN_ON(true);

I would prefer to put a dev_warn() line here, so that people reading the kernel log can easily tell which device caused the problem and what sort of problem it is. Something like this:

Quoted 3 lines
		dev_warn(dev, "device added while parent %s is asleep\n",
				dev->parent->bus_id);
		WARN_ON(true);

Added.

Quoted 10 lines

Review static int dpm_suspend(pm_message_t stat · line 412

Quoted 8 lines
(quoted diff) +4 -0
@@ -426,6 +406,12 @@ static int dpm_suspend(pm_message_t stat
 		struct list_head *entry = dpm_active.prev;
 		struct device *dev = to_device(entry);
 
+		if (dev->parent && dev->parent->power.sleeping) {
+			WARN_ON(true);
+			error = -EAGAIN;
+			break;

Again, a dev_warn() would be appropriate.

Quoted 5 lines

I chose just "WARN_ON(dev->parent && dev->parent->power.sleeping)", which is shorter. :-)

I don't really expect it to appear and if it's reported, it'll be easy to figure out everything from the stack trace.

Quoted 7 lines

Okay.

Quoted 1 line

Revised patch below.

It looks good. Let's hope it doesn't mess up ACPI too badly... :-)

Thanks!

Okay, I also have the appended patch on top of this one. :-)

Rafael

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

Include dpm_sysfs_add() into device_pm_add(), in analogy with device_pm_remove(), and modify device_add() to call the latter after bus_add_device(), to avoid situations in which the PM core may attempt to suspend a device the registration of which has not been successful.

---
 drivers/base/core.c       |   15 +++++----------
 drivers/base/power/main.c |    4 +++-
 2 files changed, 8 insertions(+), 11 deletions(-)
drivers/base/core.c +5 -10
Index: linux-2.6/drivers/base/core.c
===================================================================
@@ -812,17 +812,12 @@ int device_add(struct device *dev)
 	error = device_add_attrs(dev);
 	if (error)
 		goto AttrsError;
-	error = dpm_sysfs_add(dev);
-	if (error)
-		goto PMError;
-	error = device_pm_add(dev);
-	if (error) {
-		dpm_sysfs_remove(dev);
-		goto PMError;
-	}
 	error = bus_add_device(dev);
 	if (error)
 		goto BusError;
+	error = device_pm_add(dev);
+	if (error)
+		goto PMError;
 	kobject_uevent(&dev->kobj, KOBJ_ADD);
 	bus_attach_device(dev);
 	if (parent)
@@ -842,9 +837,9 @@ int device_add(struct device *dev)
  Done:
 	put_device(dev);
 	return error;
- BusError:
-	device_pm_remove(dev);
  PMError:
+	bus_remove_device(dev);
+ BusError:
 	if (dev->bus)
 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
 					     BUS_NOTIFY_DEL_DEVICE, dev);
drivers/base/power/main.c +3 -1
Index: linux-2.6/drivers/base/power/main.c
===================================================================
@@ -75,7 +75,9 @@ int device_pm_add(struct device *dev)
 		WARN_ON(true);
 		error = -EBUSY;
 	} else {
-		list_add_tail(&dev->power.entry, &dpm_active);
+		error = dpm_sysfs_add(dev);
+		if (!error)
+			list_add_tail(&dev->power.entry, &dpm_active);
 	}
 	mutex_unlock(&dpm_list_mtx);
 	return error;
#16 lore raw
In reply to Alan Stern
Quoted 45 lines · Alan Stern
Quoted 37 lines · Rafael J. Wysocki
Quoted 19 lines · Alan Stern

Review

Quoted 12 lines · Rafael J. Wysocki
(quoted diff) +5 -2
-void device_pm_add(struct device *dev)
+int device_pm_add(struct device *dev)
 {
+	int error = 0;
+
 	pr_debug("PM: Adding info for %s:%s\n",
 		 dev->bus ? dev->bus->name : "No Bus",
 		 kobject_name(&dev->kobj));
 	mutex_lock(&dpm_list_mtx);
-	list_add_tail(&dev->power.entry, &dpm_active);
+	if (dev->parent && dev->parent->power.sleeping) {
+		WARN_ON(true);

I would prefer to put a dev_warn() line here, so that people reading the kernel log can easily tell which device caused the problem and what sort of problem it is. Something like this:

Quoted 3 lines
		dev_warn(dev, "device added while parent %s is asleep\n",
				dev->parent->bus_id);
		WARN_ON(true);

Added.

Quoted 10 lines

Review static int dpm_suspend(pm_message_t stat · line 412

Quoted 8 lines
(quoted diff) +4 -0
@@ -426,6 +406,12 @@ static int dpm_suspend(pm_message_t stat
 		struct list_head *entry = dpm_active.prev;
 		struct device *dev = to_device(entry);
 
+		if (dev->parent && dev->parent->power.sleeping) {
+			WARN_ON(true);
+			error = -EAGAIN;
+			break;

Again, a dev_warn() would be appropriate.

Quoted 5 lines

I chose just "WARN_ON(dev->parent && dev->parent->power.sleeping)", which is shorter. :-)

I don't really expect it to appear and if it's reported, it'll be easy to figure out everything from the stack trace.

Quoted 7 lines

Okay.

Quoted 1 line

Revised patch below.

It looks good. Let's hope it doesn't mess up ACPI too badly... :-)

Thanks!

Okay, I also have the appended patch on top of this one. :-)

Rafael

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

Include dpm_sysfs_add() into device_pm_add(), in analogy with device_pm_remove(), and modify device_add() to call the latter after bus_add_device(), to avoid situations in which the PM core may attempt to suspend a device the registration of which has not been successful.

---
 drivers/base/core.c       |   15 +++++----------
 drivers/base/power/main.c |    4 +++-
 2 files changed, 8 insertions(+), 11 deletions(-)
drivers/base/core.c +5 -10
Index: linux-2.6/drivers/base/core.c
===================================================================
@@ -812,17 +812,12 @@ int device_add(struct device *dev)
 	error = device_add_attrs(dev);
 	if (error)
 		goto AttrsError;
-	error = dpm_sysfs_add(dev);
-	if (error)
-		goto PMError;
-	error = device_pm_add(dev);
-	if (error) {
-		dpm_sysfs_remove(dev);
-		goto PMError;
-	}
 	error = bus_add_device(dev);
 	if (error)
 		goto BusError;
+	error = device_pm_add(dev);
+	if (error)
+		goto PMError;
 	kobject_uevent(&dev->kobj, KOBJ_ADD);
 	bus_attach_device(dev);
 	if (parent)
@@ -842,9 +837,9 @@ int device_add(struct device *dev)
  Done:
 	put_device(dev);
 	return error;
- BusError:
-	device_pm_remove(dev);
  PMError:
+	bus_remove_device(dev);
+ BusError:
 	if (dev->bus)
 		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
 					     BUS_NOTIFY_DEL_DEVICE, dev);
drivers/base/power/main.c +3 -1
Index: linux-2.6/drivers/base/power/main.c
===================================================================
@@ -75,7 +75,9 @@ int device_pm_add(struct device *dev)
 		WARN_ON(true);
 		error = -EBUSY;
 	} else {
-		list_add_tail(&dev->power.entry, &dpm_active);
+		error = dpm_sysfs_add(dev);
+		if (!error)
+			list_add_tail(&dev->power.entry, &dpm_active);
 	}
 	mutex_unlock(&dpm_list_mtx);
 	return error;
#17 lore raw
Quoted 7 lines · Rafael J. Wysocki
Quoted 5 lines
Quoted 1 line

Revised patch below.

It looks good. Let's hope it doesn't mess up ACPI too badly... :-)

Thanks!

I thought of one more thing you might want to add: device_pm_add() doesn't handle the case where dev->parent is NULL. You could put in that static "all_devices_sleeping" flag, which gets set at the end of dpm_suspend() and cleared at the start of dpm_resume().

Quoted 4 lines

Okay, I also have the appended patch on top of this one. :-)

Rafael

Quoted 8 lines

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

Include dpm_sysfs_add() into device_pm_add(), in analogy with device_pm_remove(), and modify device_add() to call the latter after bus_add_device(), to avoid situations in which the PM core may attempt to suspend a device the registration of which has not been successful.

Clean and simple. Ack.

Alan Stern

#18 lore raw
In reply to Rafael J. Wysocki
Quoted 7 lines · Rafael J. Wysocki
Quoted 5 lines
Quoted 1 line

Revised patch below.

It looks good. Let's hope it doesn't mess up ACPI too badly... :-)

Thanks!

I thought of one more thing you might want to add: device_pm_add() doesn't handle the case where dev->parent is NULL. You could put in that static "all_devices_sleeping" flag, which gets set at the end of dpm_suspend() and cleared at the start of dpm_resume().

Quoted 4 lines

Okay, I also have the appended patch on top of this one. :-)

Rafael

Quoted 8 lines

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

Include dpm_sysfs_add() into device_pm_add(), in analogy with device_pm_remove(), and modify device_add() to call the latter after bus_add_device(), to avoid situations in which the PM core may attempt to suspend a device the registration of which has not been successful.

Clean and simple. Ack.

Alan Stern

#19 lore raw
Quoted 4 lines · Alan Stern
Quoted 2 lines · Rafael J. Wysocki
Quoted 2 lines
Quoted 1 line

Revised patch below.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

It looks good. Let's hope it doesn't mess up ACPI too badly... :-)

Thanks!

I thought of one more thing you might want to add: device_pm_add() doesn't handle the case where dev->parent is NULL.

I'm not sure what you mean.

If dev->parent is NULL, we get into the "successful" branch where the device is added to dpm_active. Do you think we should add any extra handling of this case?

Quoted 2 lines

You could put in that static "all_devices_sleeping" flag, which gets set at the end of dpm_suspend() and cleared at the start of dpm_resume().

Well, I don't think it's necessary. dpm_active is empty in that case and we can use the list_empty(&dpm_active) check instead.

Thanks,
Rafael

#20 lore raw
In reply to Alan Stern
Quoted 4 lines · Alan Stern
Quoted 2 lines · Rafael J. Wysocki
Quoted 2 lines
Quoted 1 line

Revised patch below.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

It looks good. Let's hope it doesn't mess up ACPI too badly... :-)

Thanks!

I thought of one more thing you might want to add: device_pm_add() doesn't handle the case where dev->parent is NULL.

I'm not sure what you mean.

If dev->parent is NULL, we get into the "successful" branch where the device is added to dpm_active. Do you think we should add any extra handling of this case?

Quoted 2 lines

You could put in that static "all_devices_sleeping" flag, which gets set at the end of dpm_suspend() and cleared at the start of dpm_resume().

Well, I don't think it's necessary. dpm_active is empty in that case and we can use the list_empty(&dpm_active) check instead.

Thanks,
Rafael

#21 lore raw
Quoted 8 lines · Rafael J. Wysocki
Quoted 2 lines

I thought of one more thing you might want to add: device_pm_add() doesn't handle the case where dev->parent is NULL.

I'm not sure what you mean.

If dev->parent is NULL, we get into the "successful" branch where the device is added to dpm_active. Do you think we should add any extra handling of this case?

If a device is registered after dpm_suspend() has returned, the device won't be suspended properly before the system goes to sleep.

If the device has a parent then you're okay, because the parent must already be suspended and so device_pm_add() will fail. But if the device doesn't have a parent then device_pm_add() will succeed, which you don't want.

Quoted 5 lines
Quoted 2 lines

You could put in that static "all_devices_sleeping" flag, which gets set at the end of dpm_suspend() and cleared at the start of dpm_resume().

Well, I don't think it's necessary. dpm_active is empty in that case and we can use the list_empty(&dpm_active) check instead.

What would happen the very first time the system registers a device during startup?

Alan Stern

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

I thought of one more thing you might want to add: device_pm_add() doesn't handle the case where dev->parent is NULL.

I'm not sure what you mean.

If dev->parent is NULL, we get into the "successful" branch where the device is added to dpm_active. Do you think we should add any extra handling of this case?

If a device is registered after dpm_suspend() has returned, the device won't be suspended properly before the system goes to sleep.

If the device has a parent then you're okay, because the parent must already be suspended and so device_pm_add() will fail. But if the device doesn't have a parent then device_pm_add() will succeed, which you don't want.

Quoted 5 lines
Quoted 2 lines

You could put in that static "all_devices_sleeping" flag, which gets set at the end of dpm_suspend() and cleared at the start of dpm_resume().

Well, I don't think it's necessary. dpm_active is empty in that case and we can use the list_empty(&dpm_active) check instead.

What would happen the very first time the system registers a device during startup?

Alan Stern

#23 lore raw
Quoted 11 lines · Alan Stern
Quoted 8 lines · Rafael J. Wysocki
Quoted 2 lines

I thought of one more thing you might want to add: device_pm_add() doesn't handle the case where dev->parent is NULL.

I'm not sure what you mean.

If dev->parent is NULL, we get into the "successful" branch where the device is added to dpm_active. Do you think we should add any extra handling of this case?

Quoted 7 lines

If a device is registered after dpm_suspend() has returned, the device won't be suspended properly before the system goes to sleep.

If the device has a parent then you're okay, because the parent must already be suspended and so device_pm_add() will fail. But if the device doesn't have a parent then device_pm_add() will succeed, which you don't want.

Well, can it happen in practice? If it can, then what way can it happen?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

You could put in that static "all_devices_sleeping" flag, which gets set at the end of dpm_suspend() and cleared at the start of dpm_resume().

Well, I don't think it's necessary. dpm_active is empty in that case and we can use the list_empty(&dpm_active) check instead.

What would happen the very first time the system registers a device during startup?

For any other uses than in device_pm_add(), the list_empty(&dpm_active) test should be sufficient, IMHO.

It seems to me that we're discussing purely academic stuff here. If it turns out to be of any practical relevance, it will be trivial to add the handling of it in the future.

Thanks,
Rafael

#24 lore raw
In reply to Alan Stern
Quoted 11 lines · Alan Stern
Quoted 8 lines · Rafael J. Wysocki
Quoted 2 lines

I thought of one more thing you might want to add: device_pm_add() doesn't handle the case where dev->parent is NULL.

I'm not sure what you mean.

If dev->parent is NULL, we get into the "successful" branch where the device is added to dpm_active. Do you think we should add any extra handling of this case?

Quoted 7 lines

If a device is registered after dpm_suspend() has returned, the device won't be suspended properly before the system goes to sleep.

If the device has a parent then you're okay, because the parent must already be suspended and so device_pm_add() will fail. But if the device doesn't have a parent then device_pm_add() will succeed, which you don't want.

Well, can it happen in practice? If it can, then what way can it happen?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

You could put in that static "all_devices_sleeping" flag, which gets set at the end of dpm_suspend() and cleared at the start of dpm_resume().

Well, I don't think it's necessary. dpm_active is empty in that case and we can use the list_empty(&dpm_active) check instead.

What would happen the very first time the system registers a device during startup?

For any other uses than in device_pm_add(), the list_empty(&dpm_active) test should be sufficient, IMHO.

It seems to me that we're discussing purely academic stuff here. If it turns out to be of any practical relevance, it will be trivial to add the handling of it in the future.

Thanks,
Rafael

#25 lore raw
Quoted 15 lines · Rafael J. Wysocki
Quoted 14 lines · Alan Stern
Quoted 8 lines · Rafael J. Wysocki
Quoted 2 lines

I thought of one more thing you might want to add: device_pm_add() doesn't handle the case where dev->parent is NULL.

I'm not sure what you mean.

If dev->parent is NULL, we get into the "successful" branch where the device is added to dpm_active. Do you think we should add any extra handling of this case?

If a device is registered after dpm_suspend() has returned, the device won't be suspended properly before the system goes to sleep.

Quoted 6 lines
Quoted 4 lines

If the device has a parent then you're okay, because the parent must already be suspended and so device_pm_add() will fail. But if the device doesn't have a parent then device_pm_add() will succeed, which you don't want.

Well, can it happen in practice? If it can, then what way can it happen?

Yes, it can happen in practice when a new module is loaded. In some ways, modules' init routines are like probe methods.

Maybe it can happen some other ways too, but I don't know of any.

Quoted 3 lines

It seems to me that we're discussing purely academic stuff here. If it turns out to be of any practical relevance, it will be trivial to add the handling of it in the future.

To be safe, I think we should make system sleep mutually exclusive with module loading.

Alan Stern

#26 lore raw
In reply to Rafael J. Wysocki
Quoted 15 lines · Rafael J. Wysocki
Quoted 14 lines · Alan Stern
Quoted 8 lines · Rafael J. Wysocki
Quoted 2 lines

I thought of one more thing you might want to add: device_pm_add() doesn't handle the case where dev->parent is NULL.

I'm not sure what you mean.

If dev->parent is NULL, we get into the "successful" branch where the device is added to dpm_active. Do you think we should add any extra handling of this case?

If a device is registered after dpm_suspend() has returned, the device won't be suspended properly before the system goes to sleep.

Quoted 6 lines
Quoted 4 lines

If the device has a parent then you're okay, because the parent must already be suspended and so device_pm_add() will fail. But if the device doesn't have a parent then device_pm_add() will succeed, which you don't want.

Well, can it happen in practice? If it can, then what way can it happen?

Yes, it can happen in practice when a new module is loaded. In some ways, modules' init routines are like probe methods.

Maybe it can happen some other ways too, but I don't know of any.

Quoted 3 lines

It seems to me that we're discussing purely academic stuff here. If it turns out to be of any practical relevance, it will be trivial to add the handling of it in the future.

To be safe, I think we should make system sleep mutually exclusive with module loading.

Alan Stern

#27 lore raw
Quoted 22 lines · Alan Stern
Quoted 20 lines · Rafael J. Wysocki
Quoted 18 lines · Alan Stern
Quoted 8 lines · Rafael J. Wysocki
Quoted 2 lines

I thought of one more thing you might want to add: device_pm_add() doesn't handle the case where dev->parent is NULL.

I'm not sure what you mean.

If dev->parent is NULL, we get into the "successful" branch where the device is added to dpm_active. Do you think we should add any extra handling of this case?

If a device is registered after dpm_suspend() has returned, the device won't be suspended properly before the system goes to sleep.

If the device has a parent then you're okay, because the parent must already be suspended and so device_pm_add() will fail. But if the device doesn't have a parent then device_pm_add() will succeed, which you don't want.

Quoted 4 lines
Quoted 1 line

Well, can it happen in practice? If it can, then what way can it happen?

Yes, it can happen in practice when a new module is loaded. In some ways, modules' init routines are like probe methods.

Hmm, I'm not sure if it really is possible to load a module when all devices have been suspended. Never mind, though.

Quoted 8 lines

Maybe it can happen some other ways too, but I don't know of any.

Quoted 3 lines

It seems to me that we're discussing purely academic stuff here. If it turns out to be of any practical relevance, it will be trivial to add the handling of it in the future.

To be safe, I think we should make system sleep mutually exclusive with module loading.

Okay, is the (yet another) version of the patch below fine by you?

Rafael

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

Modify the PM core to protect its data structures, specifically the dpm_active list, from being corrupted if a child of the currently suspending device is registered concurrently with its ->suspend() callback. In that case, since the new device (the child) is added to dpm_active after its parent, the PM core will attempt to suspend it after the parent, which is wrong.

Introduce a new member of struct dev_pm_info, called 'sleeping', and use it to check if the parent of the device being added to dpm_active has been suspended, in which case the device registration fails. Also, use 'sleeping' for checking if the ordering of devices on dpm_active is correct.

Remove pm_sleep_rwsem which is not necessary any more.

Special thanks to Alan Stern for discussions and suggestions that lead to the creation of this patch.

---
 Documentation/power/devices.txt |    5 +++
 drivers/base/core.c             |    6 +++-
 drivers/base/power/main.c       |   57 ++++++++++++++++++----------------------
 drivers/base/power/power.h      |   23 ++--------------
 include/linux/pm.h              |    1 
 5 files changed, 40 insertions(+), 52 deletions(-)
include/linux/pm.h +1 -0
Index: linux-2.6/include/linux/pm.h
===================================================================
@@ -183,6 +183,7 @@ typedef struct pm_message {
 struct dev_pm_info {
 	pm_message_t		power_state;
 	unsigned		can_wakeup:1;
+	bool			sleeping:1;	/* Owned by the PM core */
 #ifdef	CONFIG_PM_SLEEP
 	unsigned		should_wakeup:1;
 	struct list_head	entry;
drivers/base/power/main.c +26 -31

121 lines not on this page · raw message

drivers/base/power/power.h +3 -20
Index: linux-2.6/drivers/base/power/power.h
===================================================================
@@ -11,30 +11,13 @@ static inline struct device *to_device(s
 	return container_of(entry, struct device, power.entry);
 }
 
-extern void device_pm_add(struct device *);
+extern int device_pm_add(struct device *);
 extern void device_pm_remove(struct device *);
-extern int pm_sleep_lock(void);
-extern void pm_sleep_unlock(void);
 
 #else /* CONFIG_PM_SLEEP */
 
-
-static inline void device_pm_add(struct device *dev)
-{
-}
-
-static inline void device_pm_remove(struct device *dev)
-{
-}
-
-static inline int pm_sleep_lock(void)
-{
-	return 0;
-}
-
-static inline void pm_sleep_unlock(void)
-{
-}
+static inline int device_pm_add(struct device *dev) { return 0; }
+static inline void device_pm_remove(struct device *dev) {}
 
 #endif
 
drivers/base/core.c +5 -1
Index: linux-2.6/drivers/base/core.c
===================================================================
@@ -815,7 +815,11 @@ int device_add(struct device *dev)
 	error = dpm_sysfs_add(dev);
 	if (error)
 		goto PMError;
-	device_pm_add(dev);
+	error = device_pm_add(dev);
+	if (error) {
+		dpm_sysfs_remove(dev);
+		goto PMError;
+	}
 	error = bus_add_device(dev);
 	if (error)
 		goto BusError;
Documentation/power/devices.txt +5 -0
Index: linux-2.6/Documentation/power/devices.txt
===================================================================
@@ -196,6 +196,11 @@ its parent; and can't be removed or susp
 
 The policy is that the device tree should match hardware bus topology.
 (Or at least the control bus, for devices which use multiple busses.)
+In particular, this means that a device registration may fail if the parent of
+the device is suspending (ie. has been chosen by the PM core as the next
+device to suspend) or has already suspended, as well as after all of the other
+devices have been suspended.  Device drivers must be prepared to cope with such
+situations.
 
 
 Suspending Devices
#28 lore raw
In reply to Alan Stern
Quoted 22 lines · Alan Stern
Quoted 20 lines · Rafael J. Wysocki
Quoted 18 lines · Alan Stern
Quoted 8 lines · Rafael J. Wysocki
Quoted 2 lines

I thought of one more thing you might want to add: device_pm_add() doesn't handle the case where dev->parent is NULL.

I'm not sure what you mean.

If dev->parent is NULL, we get into the "successful" branch where the device is added to dpm_active. Do you think we should add any extra handling of this case?

If a device is registered after dpm_suspend() has returned, the device won't be suspended properly before the system goes to sleep.

If the device has a parent then you're okay, because the parent must already be suspended and so device_pm_add() will fail. But if the device doesn't have a parent then device_pm_add() will succeed, which you don't want.

Quoted 4 lines
Quoted 1 line

Well, can it happen in practice? If it can, then what way can it happen?

Yes, it can happen in practice when a new module is loaded. In some ways, modules' init routines are like probe methods.

Hmm, I'm not sure if it really is possible to load a module when all devices have been suspended. Never mind, though.

Quoted 8 lines

Maybe it can happen some other ways too, but I don't know of any.

Quoted 3 lines

It seems to me that we're discussing purely academic stuff here. If it turns out to be of any practical relevance, it will be trivial to add the handling of it in the future.

To be safe, I think we should make system sleep mutually exclusive with module loading.

Okay, is the (yet another) version of the patch below fine by you?

Rafael

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

Modify the PM core to protect its data structures, specifically the dpm_active list, from being corrupted if a child of the currently suspending device is registered concurrently with its ->suspend() callback. In that case, since the new device (the child) is added to dpm_active after its parent, the PM core will attempt to suspend it after the parent, which is wrong.

Introduce a new member of struct dev_pm_info, called 'sleeping', and use it to check if the parent of the device being added to dpm_active has been suspended, in which case the device registration fails. Also, use 'sleeping' for checking if the ordering of devices on dpm_active is correct.

Remove pm_sleep_rwsem which is not necessary any more.

Special thanks to Alan Stern for discussions and suggestions that lead to the creation of this patch.

---
 Documentation/power/devices.txt |    5 +++
 drivers/base/core.c             |    6 +++-
 drivers/base/power/main.c       |   57 ++++++++++++++++++----------------------
 drivers/base/power/power.h      |   23 ++--------------
 include/linux/pm.h              |    1 
 5 files changed, 40 insertions(+), 52 deletions(-)
include/linux/pm.h +1 -0
Index: linux-2.6/include/linux/pm.h
===================================================================
@@ -183,6 +183,7 @@ typedef struct pm_message {
 struct dev_pm_info {
 	pm_message_t		power_state;
 	unsigned		can_wakeup:1;
+	bool			sleeping:1;	/* Owned by the PM core */
 #ifdef	CONFIG_PM_SLEEP
 	unsigned		should_wakeup:1;
 	struct list_head	entry;
drivers/base/power/main.c +26 -31

121 lines not on this page · raw message

drivers/base/power/power.h +3 -20
Index: linux-2.6/drivers/base/power/power.h
===================================================================
@@ -11,30 +11,13 @@ static inline struct device *to_device(s
 	return container_of(entry, struct device, power.entry);
 }
 
-extern void device_pm_add(struct device *);
+extern int device_pm_add(struct device *);
 extern void device_pm_remove(struct device *);
-extern int pm_sleep_lock(void);
-extern void pm_sleep_unlock(void);
 
 #else /* CONFIG_PM_SLEEP */
 
-
-static inline void device_pm_add(struct device *dev)
-{
-}
-
-static inline void device_pm_remove(struct device *dev)
-{
-}
-
-static inline int pm_sleep_lock(void)
-{
-	return 0;
-}
-
-static inline void pm_sleep_unlock(void)
-{
-}
+static inline int device_pm_add(struct device *dev) { return 0; }
+static inline void device_pm_remove(struct device *dev) {}
 
 #endif
 
drivers/base/core.c +5 -1
Index: linux-2.6/drivers/base/core.c
===================================================================
@@ -815,7 +815,11 @@ int device_add(struct device *dev)
 	error = dpm_sysfs_add(dev);
 	if (error)
 		goto PMError;
-	device_pm_add(dev);
+	error = device_pm_add(dev);
+	if (error) {
+		dpm_sysfs_remove(dev);
+		goto PMError;
+	}
 	error = bus_add_device(dev);
 	if (error)
 		goto BusError;
Documentation/power/devices.txt +5 -0
Index: linux-2.6/Documentation/power/devices.txt
===================================================================
@@ -196,6 +196,11 @@ its parent; and can't be removed or susp
 
 The policy is that the device tree should match hardware bus topology.
 (Or at least the control bus, for devices which use multiple busses.)
+In particular, this means that a device registration may fail if the parent of
+the device is suspending (ie. has been chosen by the PM core as the next
+device to suspend) or has already suspended, as well as after all of the other
+devices have been suspended.  Device drivers must be prepared to cope with such
+situations.
 
 
 Suspending Devices
#29 lore raw
Quoted 7 lines · Rafael J. Wysocki
Quoted 4 lines
Quoted 1 line

Well, can it happen in practice? If it can, then what way can it happen?

Yes, it can happen in practice when a new module is loaded. In some ways, modules' init routines are like probe methods.

Hmm, I'm not sure if it really is possible to load a module when all devices have been suspended. Never mind, though.

You can load the module before devices are suspended, and then its init routine can run while the suspend is starting.

Quoted 4 lines
Quoted 2 lines

To be safe, I think we should make system sleep mutually exclusive with module loading.

Okay, is the (yet another) version of the patch below fine by you?

Yes, it's fine. Mutual exclusion with module loading can be added later. (Ironically, it may require putting pm_sleep_rwsem back!)

Alan Stern

#30 lore raw
In reply to Rafael J. Wysocki
Quoted 7 lines · Rafael J. Wysocki
Quoted 4 lines
Quoted 1 line

Well, can it happen in practice? If it can, then what way can it happen?

Yes, it can happen in practice when a new module is loaded. In some ways, modules' init routines are like probe methods.

Hmm, I'm not sure if it really is possible to load a module when all devices have been suspended. Never mind, though.

You can load the module before devices are suspended, and then its init routine can run while the suspend is starting.

Quoted 4 lines
Quoted 2 lines

To be safe, I think we should make system sleep mutually exclusive with module loading.

Okay, is the (yet another) version of the patch below fine by you?

Yes, it's fine. Mutual exclusion with module loading can be added later. (Ironically, it may require putting pm_sleep_rwsem back!)

Alan Stern

#31 lore raw
Quoted 4 lines · Alan Stern
Quoted 2 lines · Rafael J. Wysocki
Quoted 2 lines
Quoted 1 line

Well, can it happen in practice? If it can, then what way can it happen?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Yes, it can happen in practice when a new module is loaded. In some ways, modules' init routines are like probe methods.

Hmm, I'm not sure if it really is possible to load a module when all devices have been suspended. Never mind, though.

You can load the module before devices are suspended, and then its init routine can run while the suspend is starting.

Yeah.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

To be safe, I think we should make system sleep mutually exclusive with module loading.

Okay, is the (yet another) version of the patch below fine by you?

Yes, it's fine. Mutual exclusion with module loading can be added later. (Ironically, it may require putting pm_sleep_rwsem back!)

I'm going to send this patch and the "include dpm_sysfs_add() into device_pm_add()" patch for -mm/linux-next testing, if you don't mind.

I'm working on a new version of the "PM: Separate suspend and hibernation callbacks" patch, on top of the two.

Thanks,
Rafael

#32 lore raw
In reply to Alan Stern
Quoted 4 lines · Alan Stern
Quoted 2 lines · Rafael J. Wysocki
Quoted 2 lines
Quoted 1 line

Well, can it happen in practice? If it can, then what way can it happen?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Yes, it can happen in practice when a new module is loaded. In some ways, modules' init routines are like probe methods.

Hmm, I'm not sure if it really is possible to load a module when all devices have been suspended. Never mind, though.

You can load the module before devices are suspended, and then its init routine can run while the suspend is starting.

Yeah.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

To be safe, I think we should make system sleep mutually exclusive with module loading.

Okay, is the (yet another) version of the patch below fine by you?

Yes, it's fine. Mutual exclusion with module loading can be added later. (Ironically, it may require putting pm_sleep_rwsem back!)

I'm going to send this patch and the "include dpm_sysfs_add() into device_pm_add()" patch for -mm/linux-next testing, if you don't mind.

I'm working on a new version of the "PM: Separate suspend and hibernation callbacks" patch, on top of the two.

Thanks,
Rafael