[PATCH] PM / core: refactor PM_OPS initializers

From: Andrzej Hajda
Date: Mon May 14 2018 - 04:22:14 EST


With current implementation of PM_OPS initializers users should annotate
all PM callbacks with __maybe_unused attribute to prevent compiler from
complaining in case respective option is not enabled. Using ternary
operator with IS_ENABLED(symbol) as a condition allows to avoid marking
these functionsÂwith __maybe_unused.
Solution was tested successfully with multiple versions of gcc since
4.9.4 up to 7.2.1. No functional changes has been observed and callbacks
were compiled out if not used, as before.

Signed-off-by: Andrzej Hajda <a.hajda@xxxxxxxxxxx>
---
include/linux/pm.h | 61 ++++++++++++++++++----------------------------
1 file changed, 24 insertions(+), 37 deletions(-)

diff --git a/include/linux/pm.h b/include/linux/pm.h
index e723b78d8357..59f333922c15 100644
--- a/include/linux/pm.h
+++ b/include/linux/pm.h
@@ -313,50 +313,37 @@ struct dev_pm_ops {
int (*runtime_idle)(struct device *dev);
};

-#ifdef CONFIG_PM_SLEEP
+#define PM_SLEEP_PTR(ptr) (IS_ENABLED(CONFIG_PM_SLEEP) ? (ptr) : NULL)
+#define PM_PTR(ptr) (IS_ENABLED(CONFIG_PM) ? (ptr) : NULL)
+
#define SET_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
- .suspend = suspend_fn, \
- .resume = resume_fn, \
- .freeze = suspend_fn, \
- .thaw = resume_fn, \
- .poweroff = suspend_fn, \
- .restore = resume_fn,
-#else
-#define SET_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
-#endif
+ .suspend = PM_SLEEP_PTR(suspend_fn), \
+ .resume = PM_SLEEP_PTR(resume_fn), \
+ .freeze = PM_SLEEP_PTR(suspend_fn), \
+ .thaw = PM_SLEEP_PTR(resume_fn), \
+ .poweroff = PM_SLEEP_PTR(suspend_fn), \
+ .restore = PM_SLEEP_PTR(resume_fn),

-#ifdef CONFIG_PM_SLEEP
#define SET_LATE_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
- .suspend_late = suspend_fn, \
- .resume_early = resume_fn, \
- .freeze_late = suspend_fn, \
- .thaw_early = resume_fn, \
- .poweroff_late = suspend_fn, \
- .restore_early = resume_fn,
-#else
-#define SET_LATE_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
-#endif
+ .suspend_late = PM_SLEEP_PTR(suspend_fn), \
+ .resume_early = PM_SLEEP_PTR(resume_fn), \
+ .freeze_late = PM_SLEEP_PTR(suspend_fn), \
+ .thaw_early = PM_SLEEP_PTR(resume_fn), \
+ .poweroff_late = PM_SLEEP_PTR(suspend_fn), \
+ .restore_early = PM_SLEEP_PTR(resume_fn),

-#ifdef CONFIG_PM_SLEEP
#define SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
- .suspend_noirq = suspend_fn, \
- .resume_noirq = resume_fn, \
- .freeze_noirq = suspend_fn, \
- .thaw_noirq = resume_fn, \
- .poweroff_noirq = suspend_fn, \
- .restore_noirq = resume_fn,
-#else
-#define SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
-#endif
+ .suspend_noirq = PM_SLEEP_PTR(suspend_fn), \
+ .resume_noirq = PM_SLEEP_PTR(resume_fn), \
+ .freeze_noirq = PM_SLEEP_PTR(suspend_fn), \
+ .thaw_noirq = PM_SLEEP_PTR(resume_fn), \
+ .poweroff_noirq = PM_SLEEP_PTR(suspend_fn), \
+ .restore_noirq = PM_SLEEP_PTR(resume_fn),

-#ifdef CONFIG_PM
#define SET_RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \
- .runtime_suspend = suspend_fn, \
- .runtime_resume = resume_fn, \
- .runtime_idle = idle_fn,
-#else
-#define SET_RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn)
-#endif
+ .runtime_suspend = PM_PTR(suspend_fn), \
+ .runtime_resume = PM_PTR(resume_fn), \
+ .runtime_idle = PM_PTR(idle_fn),

/*
* Use this if you want to use the same suspend and resume callbacks for suspend
--
2.17.0