From: Greg Kroah-Hartman <gregkh@suse.de>
To: linux-kernel@vger.kernel.org
Cc: "Rafael J. Wysocki" <rjw@sisk.pl>, Greg Kroah-Hartman <gregkh@suse.de>
Subject: [PATCH 19/36] PM: Handle device registrations during suspend/resume
Date: Sun, 20 Apr 2008 03:45:49 -0700 [thread overview]
Message-ID: <1208688366-9252-19-git-send-email-gregkh@suse.de> (raw)
In-Reply-To: <20080420104516.GA9225@suse.de>
From: Rafael J. Wysocki <rjw@sisk.pl>
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.
Introduce variable 'all_sleeping' that will be set to 'true' once all
devices have been suspended and make new device registrations fail
until 'all_sleeping' is reset to 'false', in order to avoid having
unsuspended devices around while the system is going into a sleep state.
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.
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Acked-by: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
---
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(-)
diff --git a/Documentation/power/devices.txt b/Documentation/power/devices.txt
index 461e4f1..421e7d0 100644
--- a/Documentation/power/devices.txt
+++ b/Documentation/power/devices.txt
@@ -196,6 +196,11 @@ its parent; and can't be removed or suspended after that parent.
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
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 24198ad..79848e6 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -820,7 +820,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;
diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
index 26de2c0..0e3991a 100644
--- a/drivers/base/power/main.c
+++ b/drivers/base/power/main.c
@@ -54,7 +54,8 @@ static LIST_HEAD(dpm_destroy);
static DEFINE_MUTEX(dpm_list_mtx);
-static DECLARE_RWSEM(pm_sleep_rwsem);
+/* 'true' if all devices have been suspended, protected by dpm_list_mtx */
+static bool all_sleeping;
int (*platform_enable_wakeup)(struct device *dev, int is_on);
@@ -62,14 +63,28 @@ int (*platform_enable_wakeup)(struct device *dev, int is_on);
* device_pm_add - add a device to the list of active devices
* @dev: Device to be added to the list
*/
-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) || all_sleeping) {
+ if (dev->parent->power.sleeping)
+ dev_warn(dev,
+ "parent %s is sleeping, will not add\n",
+ dev->parent->bus_id);
+ else
+ dev_warn(dev, "devices are sleeping, will not add\n");
+ WARN_ON(true);
+ error = -EBUSY;
+ } else {
+ list_add_tail(&dev->power.entry, &dpm_active);
+ }
mutex_unlock(&dpm_list_mtx);
+ return error;
}
/**
@@ -107,32 +122,6 @@ void device_pm_schedule_removal(struct device *dev)
}
EXPORT_SYMBOL_GPL(device_pm_schedule_removal);
-/**
- * pm_sleep_lock - mutual exclusion for registration and suspend
- *
- * Returns 0 if no suspend is underway and device registration
- * may proceed, otherwise -EBUSY.
- */
-int pm_sleep_lock(void)
-{
- if (down_read_trylock(&pm_sleep_rwsem))
- return 0;
-
- return -EBUSY;
-}
-
-/**
- * pm_sleep_unlock - mutual exclusion for registration and suspend
- *
- * This routine undoes the effect of device_pm_add_lock
- * when a device's registration is complete.
- */
-void pm_sleep_unlock(void)
-{
- up_read(&pm_sleep_rwsem);
-}
-
-
/*------------------------- Resume routines -------------------------*/
/**
@@ -242,11 +231,13 @@ static int resume_device(struct device *dev)
static void dpm_resume(void)
{
mutex_lock(&dpm_list_mtx);
+ all_sleeping = false;
while(!list_empty(&dpm_off)) {
struct list_head *entry = dpm_off.next;
struct device *dev = to_device(entry);
list_move_tail(entry, &dpm_active);
+ dev->power.sleeping = false;
mutex_unlock(&dpm_list_mtx);
resume_device(dev);
mutex_lock(&dpm_list_mtx);
@@ -285,7 +276,6 @@ void device_resume(void)
might_sleep();
dpm_resume();
unregister_dropped_devices();
- up_write(&pm_sleep_rwsem);
}
EXPORT_SYMBOL_GPL(device_resume);
@@ -421,6 +411,9 @@ static int dpm_suspend(pm_message_t state)
struct list_head *entry = dpm_active.prev;
struct device *dev = to_device(entry);
+ WARN_ON(dev->parent && dev->parent->power.sleeping);
+
+ dev->power.sleeping = true;
mutex_unlock(&dpm_list_mtx);
error = suspend_device(dev, state);
mutex_lock(&dpm_list_mtx);
@@ -432,11 +425,14 @@ static int dpm_suspend(pm_message_t state)
(error == -EAGAIN ?
" (please convert to suspend_late)" :
""));
+ dev->power.sleeping = false;
break;
}
if (!list_empty(&dev->power.entry))
list_move(&dev->power.entry, &dpm_off);
}
+ if (!error)
+ all_sleeping = true;
mutex_unlock(&dpm_list_mtx);
return error;
@@ -454,7 +450,6 @@ int device_suspend(pm_message_t state)
int error;
might_sleep();
- down_write(&pm_sleep_rwsem);
error = dpm_suspend(state);
if (error)
device_resume();
diff --git a/drivers/base/power/power.h b/drivers/base/power/power.h
index e32d3bd..a6894f2 100644
--- a/drivers/base/power/power.h
+++ b/drivers/base/power/power.h
@@ -11,30 +11,13 @@ static inline struct device *to_device(struct list_head *entry)
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
diff --git a/include/linux/pm.h b/include/linux/pm.h
index 015b735..e6b9f29 100644
--- a/include/linux/pm.h
+++ b/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;
--
1.5.4.5
next prev parent reply other threads:[~2008-04-20 10:46 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-04-20 10:45 [GIT PATCH] driver core changes against 2.6.25-git Greg KH
2008-04-20 10:45 ` [PATCH 01/36] kobject: catch kobjects that are not initialized Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 02/36] uio: Kconfig improvements Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 03/36] uio: mark pci_device_id hilscher_pci_ids[] __devinitdata Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 04/36] UIO: arch/arm/Kconfig: Make UIO available on ARM architecture Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 05/36] UIO: Remove needless PCI_DEVICE_ID definition from uio_cif.c Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 06/36] UIO: Implement a UIO interface for the SMX Cryptengine Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 07/36] UIO: hold a reference to the device's owner while the device is open Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 08/36] driver core: memory: semaphore to mutex Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 09/36] driver core: register_memory/unregister_memory clean ups and bugfix Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 10/36] driver core: Convert debug functions declared inline __attribute__((format (printf,x,y) to statement expression macros Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 11/36] sysfs: small header file cleanup for SYSFS=n Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 12/36] firmware: move firmware_class from Documentation/ to samples/ Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 13/36] firmware: clean up samples for coding style issues Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 14/36] PNP: add all PNP card device id's as individual aliases Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 15/36] power_state: remove it from driver core Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 16/36] driver core: cpu: fix section mismatch in cpu.c:store_online Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 17/36] sysdev: detect multiple driver registrations Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 18/36] block: send disk "change" event for rescan_partitions() Greg Kroah-Hartman
2008-04-20 10:45 ` Greg Kroah-Hartman [this message]
2008-04-20 10:45 ` [PATCH 20/36] Driver core: Call device_pm_add() after bus_add_device() in device_add() Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 21/36] PM: Fix misuse of wakeup flag accessors in serial core Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 22/36] PM: Make wakeup flags available whenever CONFIG_PM is set Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 23/36] PM: Convert wakeup flag accessors to inline functions Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 24/36] Driver core: make device_is_registered() work for class devices Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 25/36] SYSFS: Explicitly include required header file slab.h Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 26/36] Kobject: Replace list_for_each() with list_for_each_entry() Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 27/36] PM: Remove legacy PM (fix) Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 28/36] Firmware: add iSCSI iBFT Support Greg Kroah-Hartman
2008-04-20 10:45 ` [PATCH 29/36] PM: Remove destroy_suspended_device() Greg Kroah-Hartman
2008-04-20 10:46 ` [PATCH 30/36] sysfs: refill attribute buffer when reading from offset 0 Greg Kroah-Hartman
2008-04-20 10:46 ` [PATCH 31/36] driver core: replace remaining __FUNCTION__ occurrences Greg Kroah-Hartman
2008-04-20 10:46 ` [PATCH 32/36] memstick: convert struct class_device to struct device Greg Kroah-Hartman
2008-04-20 10:46 ` [PATCH 33/36] IB: " Greg Kroah-Hartman
2008-04-20 17:23 ` Roland Dreier
2008-04-20 10:46 ` [PATCH 34/36] IB: rename "dev" to "srp_dev" in srp_host structure Greg Kroah-Hartman
2008-04-20 10:46 ` [PATCH 35/36] DRM: remove unused dev_class Greg Kroah-Hartman
2008-04-20 10:46 ` [PATCH 36/36] SCSI: convert struct class_device to struct device Greg Kroah-Hartman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1208688366-9252-19-git-send-email-gregkh@suse.de \
--to=gregkh@suse.de \
--cc=linux-kernel@vger.kernel.org \
--cc=rjw@sisk.pl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.