Re: [PATCH] PM: Move to_device() out of CONFIG_PM_SLEEP protection

From: Zhongqiu Han

Date: Wed Mar 04 2026 - 05:43:33 EST


On 3/2/2026 6:54 PM, Pengpeng Hou wrote:
The helper function to_device() is used to convert a list_head structure
(specifically from dev->power.entry) back to the corresponding struct
device. Currently, this function is only available when CONFIG_PM_SLEEP
is enabled. However, some generic power management code may need to iterate
through the device list even if sleep states (suspend/hibernate) are not
supported.

Hi Pengpeng,

Thank you for the patch. However, I have some concerns about this
change:

1.Your commit message states that "some generic power management code
may need to iterate through the device list even if sleep states are not
supported." Could you please provide a concrete example of such code?

2.The to_device() function accesses dev->power.entry, which is only
present when CONFIG_PM_SLEEP is enabled:

struct dev_pm_info {
...
#ifdef CONFIG_PM_SLEEP
struct list_head entry;
}

This means any code trying to use to_device() with CONFIG_PM_SLEEP=n would _FAIL_ to compile when actually called.



There are currently two approaches for moving the function.The first one:
judging from the current conditions where the function is called, all calls
are not wrapped by any macros, so it is appropriate to move the function
definition to the global scope.The second one: if the compilation of the
files where the function is called is controlled by CONFIG_PM, it is more
appropriate to move the function definition into CONFIG_PM.

To ensure that the availability of the function is consistent with its
usage scenarios and to avoid undefined reference compilation errors, the
first approach is adopted.

Signed-off-by: Pengpeng Hou <pengpeng.hou@xxxxxxxxxxxxxxxx>
---
drivers/base/power/power.h | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/base/power/power.h b/drivers/base/power/power.h
index 922ed457d..0a40bb546 100644
--- a/drivers/base/power/power.h
+++ b/drivers/base/power/power.h
@@ -106,10 +106,6 @@ extern int pm_async_enabled;
/* drivers/base/power/main.c */
extern struct list_head dpm_list; /* The active device list */
-static inline struct device *to_device(struct list_head *entry)
-{
- return container_of(entry, struct device, power.entry);
-}
extern void device_pm_sleep_init(struct device *dev);
extern void device_pm_add(struct device *);
@@ -162,6 +158,11 @@ static inline int pm_wakeup_source_sysfs_add(struct device *parent)
#endif /* !CONFIG_PM_SLEEP */
+static inline struct device *to_device(struct list_head *entry)
+{
+ return container_of(entry, struct device, power.entry);
+}
+
static inline void device_pm_init(struct device *dev)
{
device_pm_init_common(dev);


--
Thx and BRs,
Zhongqiu Han