Skip to contentSkip to conversation

PM: Introduce DEFINE_PM_GENERIC_FUNC macro to reduce code duplication

PATCH NAK Kaushlendra Kumar 3 participants 3 messages Oct 14, 2025

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

PATCH v2 1/1 PM: Introduce DEFINE_PM_GENERIC_FUNC macro to reduce code duplication

Add DEFINE_PM_GENERIC_FUNC macro to completely eliminate repetitive code patterns in power management generic operations. This macro generates the entire function definition including signature, implementation, and symbol export for each pm_generic_* function.

This reduces code duplication significantly while maintaining the same functionality and improving code maintainability.

---
Changes in v2:
- Include function signature and symbol export in macro as suggested

---
 drivers/base/power/generic_ops.c | 158 +++++--------------------------
 1 file changed, 26 insertions(+), 132 deletions(-)
drivers/base/power/generic_ops.c +26 -132
index 6502720bb564..0afea5d8f8ef 100644
@@ -8,6 +8,14 @@
 #include <linux/pm_runtime.h>
 #include <linux/export.h>
 
+#define DEFINE_PM_GENERIC_FUNC(func_name, op_name) \
+int pm_generic_##func_name(struct device *dev) \
+{ \
+	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; \
+	return pm && pm->op_name ? pm->op_name(dev) : 0; \
+} \
+EXPORT_SYMBOL_GPL(pm_generic_##func_name)
+
 #ifdef CONFIG_PM
 /**
  * pm_generic_runtime_suspend - Generic runtime suspend callback for subsystems.
@@ -17,16 +25,7 @@
  * ->runtime_suspend(), execute it and return its error code.  Otherwise,
  * return 0.
  */
-int pm_generic_runtime_suspend(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-	int ret;
-
-	ret = pm && pm->runtime_suspend ? pm->runtime_suspend(dev) : 0;
-
-	return ret;
-}
-EXPORT_SYMBOL_GPL(pm_generic_runtime_suspend);
+DEFINE_PM_GENERIC_FUNC(runtime_suspend, runtime_suspend);
 
 /**
  * pm_generic_runtime_resume - Generic runtime resume callback for subsystems.
@@ -36,16 +35,7 @@ EXPORT_SYMBOL_GPL(pm_generic_runtime_suspend);
  * ->runtime_resume(), execute it and return its error code.  Otherwise,
  * return 0.
  */
-int pm_generic_runtime_resume(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-	int ret;
-
-	ret = pm && pm->runtime_resume ? pm->runtime_resume(dev) : 0;
-
-	return ret;
-}
-EXPORT_SYMBOL_GPL(pm_generic_runtime_resume);
+DEFINE_PM_GENERIC_FUNC(runtime_resume, runtime_resume);
 #endif /* CONFIG_PM */
 
 #ifdef CONFIG_PM_SLEEP
@@ -70,193 +60,97 @@ int pm_generic_prepare(struct device *dev)
  * pm_generic_suspend_noirq - Generic suspend_noirq callback for subsystems.
  * @dev: Device to suspend.
  */
-int pm_generic_suspend_noirq(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-
-	return pm && pm->suspend_noirq ? pm->suspend_noirq(dev) : 0;
-}
-EXPORT_SYMBOL_GPL(pm_generic_suspend_noirq);
+DEFINE_PM_GENERIC_FUNC(suspend_noirq, suspend_noirq);
 
 /**
  * pm_generic_suspend_late - Generic suspend_late callback for subsystems.
  * @dev: Device to suspend.
  */
-int pm_generic_suspend_late(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-
-	return pm && pm->suspend_late ? pm->suspend_late(dev) : 0;
-}
-EXPORT_SYMBOL_GPL(pm_generic_suspend_late);
+DEFINE_PM_GENERIC_FUNC(suspend_late, suspend_late);
 
 /**
  * pm_generic_suspend - Generic suspend callback for subsystems.
  * @dev: Device to suspend.
  */
-int pm_generic_suspend(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-
-	return pm && pm->suspend ? pm->suspend(dev) : 0;
-}
-EXPORT_SYMBOL_GPL(pm_generic_suspend);
+DEFINE_PM_GENERIC_FUNC(suspend, suspend);
 
 /**
  * pm_generic_freeze_noirq - Generic freeze_noirq callback for subsystems.
  * @dev: Device to freeze.
  */
-int pm_generic_freeze_noirq(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-
-	return pm && pm->freeze_noirq ? pm->freeze_noirq(dev) : 0;
-}
-EXPORT_SYMBOL_GPL(pm_generic_freeze_noirq);
+DEFINE_PM_GENERIC_FUNC(freeze_noirq, freeze_noirq);
 
 /**
  * pm_generic_freeze - Generic freeze callback for subsystems.
  * @dev: Device to freeze.
  */
-int pm_generic_freeze(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-
-	return pm && pm->freeze ? pm->freeze(dev) : 0;
-}
-EXPORT_SYMBOL_GPL(pm_generic_freeze);
+DEFINE_PM_GENERIC_FUNC(freeze, freeze);
 
 /**
  * pm_generic_poweroff_noirq - Generic poweroff_noirq callback for subsystems.
  * @dev: Device to handle.
  */
-int pm_generic_poweroff_noirq(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-
-	return pm && pm->poweroff_noirq ? pm->poweroff_noirq(dev) : 0;
-}
-EXPORT_SYMBOL_GPL(pm_generic_poweroff_noirq);
+DEFINE_PM_GENERIC_FUNC(poweroff_noirq, poweroff_noirq);
 
 /**
  * pm_generic_poweroff_late - Generic poweroff_late callback for subsystems.
  * @dev: Device to handle.
  */
-int pm_generic_poweroff_late(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-
-	return pm && pm->poweroff_late ? pm->poweroff_late(dev) : 0;
-}
-EXPORT_SYMBOL_GPL(pm_generic_poweroff_late);
+DEFINE_PM_GENERIC_FUNC(poweroff_late, poweroff_late);
 
 /**
  * pm_generic_poweroff - Generic poweroff callback for subsystems.
  * @dev: Device to handle.
  */
-int pm_generic_poweroff(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-
-	return pm && pm->poweroff ? pm->poweroff(dev) : 0;
-}
-EXPORT_SYMBOL_GPL(pm_generic_poweroff);
+DEFINE_PM_GENERIC_FUNC(poweroff, poweroff);
 
 /**
  * pm_generic_thaw_noirq - Generic thaw_noirq callback for subsystems.
  * @dev: Device to thaw.
  */
-int pm_generic_thaw_noirq(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-
-	return pm && pm->thaw_noirq ? pm->thaw_noirq(dev) : 0;
-}
-EXPORT_SYMBOL_GPL(pm_generic_thaw_noirq);
+DEFINE_PM_GENERIC_FUNC(thaw_noirq, thaw_noirq);
 
 /**
  * pm_generic_thaw - Generic thaw callback for subsystems.
  * @dev: Device to thaw.
  */
-int pm_generic_thaw(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-
-	return pm && pm->thaw ? pm->thaw(dev) : 0;
-}
-EXPORT_SYMBOL_GPL(pm_generic_thaw);
+DEFINE_PM_GENERIC_FUNC(thaw, thaw);
 
 /**
  * pm_generic_resume_noirq - Generic resume_noirq callback for subsystems.
  * @dev: Device to resume.
  */
-int pm_generic_resume_noirq(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-
-	return pm && pm->resume_noirq ? pm->resume_noirq(dev) : 0;
-}
-EXPORT_SYMBOL_GPL(pm_generic_resume_noirq);
+DEFINE_PM_GENERIC_FUNC(resume_noirq, resume_noirq);
 
 /**
  * pm_generic_resume_early - Generic resume_early callback for subsystems.
  * @dev: Device to resume.
  */
-int pm_generic_resume_early(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-
-	return pm && pm->resume_early ? pm->resume_early(dev) : 0;
-}
-EXPORT_SYMBOL_GPL(pm_generic_resume_early);
+DEFINE_PM_GENERIC_FUNC(resume_early, resume_early);
 
 /**
  * pm_generic_resume - Generic resume callback for subsystems.
  * @dev: Device to resume.
  */
-int pm_generic_resume(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-
-	return pm && pm->resume ? pm->resume(dev) : 0;
-}
-EXPORT_SYMBOL_GPL(pm_generic_resume);
+DEFINE_PM_GENERIC_FUNC(resume, resume);
 
 /**
  * pm_generic_restore_noirq - Generic restore_noirq callback for subsystems.
  * @dev: Device to restore.
  */
-int pm_generic_restore_noirq(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-
-	return pm && pm->restore_noirq ? pm->restore_noirq(dev) : 0;
-}
-EXPORT_SYMBOL_GPL(pm_generic_restore_noirq);
+DEFINE_PM_GENERIC_FUNC(restore_noirq, restore_noirq);
 
 /**
  * pm_generic_restore_early - Generic restore_early callback for subsystems.
  * @dev: Device to resume.
  */
-int pm_generic_restore_early(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-
-	return pm && pm->restore_early ? pm->restore_early(dev) : 0;
-}
-EXPORT_SYMBOL_GPL(pm_generic_restore_early);
+DEFINE_PM_GENERIC_FUNC(restore_early, restore_early);
 
 /**
  * pm_generic_restore - Generic restore callback for subsystems.
  * @dev: Device to restore.
  */
-int pm_generic_restore(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-
-	return pm && pm->restore ? pm->restore(dev) : 0;
-}
-EXPORT_SYMBOL_GPL(pm_generic_restore);
+DEFINE_PM_GENERIC_FUNC(restore, restore);
 
 /**
  * pm_generic_complete - Generic routine completing a device power transition.
Signature · 1 line
2.34.1
#1 lore raw
Quoted 22 lines · Kaushlendra Kumar

Add DEFINE_PM_GENERIC_FUNC macro to completely eliminate repetitive code patterns in power management generic operations. This macro generates the entire function definition including signature, implementation, and symbol export for each pm_generic_* function.

This reduces code duplication significantly while maintaining the same functionality and improving code maintainability.

---
Changes in v2:
- Include function signature and symbol export in macro as suggested

---
 drivers/base/power/generic_ops.c | 158 +++++--------------------------
 1 file changed, 26 insertions(+), 132 deletions(-)
drivers/base/power/generic_ops.c +0 -0
index 6502720bb564..0afea5d8f8ef 100644
@@ -8,6 +8,14 @@
 #include <linux/pm_runtime.h>

drivers/base/power/generic_ops.c line 18

Quoted 10 lines
drivers/base/power/generic_ops.c +8 -0
 #include <linux/export.h>
 
+#define DEFINE_PM_GENERIC_FUNC(func_name, op_name) \
+int pm_generic_##func_name(struct device *dev) \
+{ \
+	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; \
+	return pm && pm->op_name ? pm->op_name(dev) : 0; \
+} \
+EXPORT_SYMBOL_GPL(pm_generic_##func_name)
+

NAK.

Honestly, I am not okay with this level of optimization just to reduce lines of code. It was much more verbose and clear earlier and I don't really understand the full benefit of this patch.

If we look at this from the eyes of a newcomer to the kernel, who does not follow the PM lists/ is coming from older kernels and tries to jump to the definitions of pm_generic_runtime_suspend or whatever - it's going to break for them. I use ctags to jump around kernel definitions extensively and after this patch I will have to jump through hoops to find what pm_generic_XYZ is doing and where it even is defined. grep will break too.

Unless there's real proof that this greatly improves code readability or performance that I can't think of right now.

Perhaps Greg/ Rafael can comment on whether this is truly justified from a maintainence perspective, but as a PM susbsys user it will make things just complicated for consumers of these ops to dig into IMO.

Quoted 18 lines
drivers/base/power/generic_ops.c +1 -10
 #ifdef CONFIG_PM
 /**
  * pm_generic_runtime_suspend - Generic runtime suspend callback for subsystems.
@@ -17,16 +25,7 @@
  * ->runtime_suspend(), execute it and return its error code.  Otherwise,
  * return 0.
  */
-int pm_generic_runtime_suspend(struct device *dev)
-{
-	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
-	int ret;
-
-	ret = pm && pm->runtime_suspend ? pm->runtime_suspend(dev) : 0;
-
-	return ret;
-}
-EXPORT_SYMBOL_GPL(pm_generic_runtime_suspend);
+DEFINE_PM_GENERIC_FUNC(runtime_suspend, runtime_suspend);

[...]

Signature · 3 lines
Best regards,
Dhruva Gole
Texas Instruments Incorporated
#2 lore raw
Quoted 49 lines · Dhruva Gole
Quoted 22 lines · Kaushlendra Kumar

Add DEFINE_PM_GENERIC_FUNC macro to completely eliminate repetitive code patterns in power management generic operations. This macro generates the entire function definition including signature, implementation, and symbol export for each pm_generic_* function.

This reduces code duplication significantly while maintaining the same functionality and improving code maintainability.

---
Changes in v2:
- Include function signature and symbol export in macro as suggested

---
 drivers/base/power/generic_ops.c | 158 +++++--------------------------
 1 file changed, 26 insertions(+), 132 deletions(-)
drivers/base/power/generic_ops.c +0 -0
index 6502720bb564..0afea5d8f8ef 100644
@@ -8,6 +8,14 @@
 #include <linux/pm_runtime.h>

drivers/base/power/generic_ops.c line 18

Quoted 10 lines
drivers/base/power/generic_ops.c +8 -0
 #include <linux/export.h>
 
+#define DEFINE_PM_GENERIC_FUNC(func_name, op_name) \
+int pm_generic_##func_name(struct device *dev) \
+{ \
+     const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; \
+     return pm && pm->op_name ? pm->op_name(dev) : 0; \
+} \
+EXPORT_SYMBOL_GPL(pm_generic_##func_name)
+

NAK.

Honestly, I am not okay with this level of optimization just to reduce lines of code. It was much more verbose and clear earlier and I don't really understand the full benefit of this patch.

If we look at this from the eyes of a newcomer to the kernel, who does not follow the PM lists/ is coming from older kernels and tries to jump to the definitions of pm_generic_runtime_suspend or whatever - it's going to break for them. I use ctags to jump around kernel definitions extensively and after this patch I will have to jump through hoops to find what pm_generic_XYZ is doing and where it even is defined. grep will break too.

Quoted 6 lines

Unless there's real proof that this greatly improves code readability or performance that I can't think of right now.

Perhaps Greg/ Rafael can comment on whether this is truly justified from a maintainence perspective, but as a PM susbsys user it will make things just complicated for consumers of these ops to dig into IMO.

OK, so it was my comment that triggered this, maybe you'd prefer the previous version:

https://lore.kernel.org/linux-pm/[email protected]/