* [PATCH v2] PM: Introduce DEFINE_PM_GENERIC_FUNC macro to reduce code duplication
@ 2025-10-14 7:22 Kaushlendra Kumar
2025-10-14 9:55 ` Dhruva Gole
0 siblings, 1 reply; 3+ messages in thread
From: Kaushlendra Kumar @ 2025-10-14 7:22 UTC (permalink / raw)
To: rafael, pavel, dakr, gregkh; +Cc: linux-pm, 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.
Signed-off-by: Kaushlendra Kumar <kaushlendra.kumar@intel.com>
---
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(-)
diff --git a/drivers/base/power/generic_ops.c b/drivers/base/power/generic_ops.c
index 6502720bb564..0afea5d8f8ef 100644
--- a/drivers/base/power/generic_ops.c
+++ b/drivers/base/power/generic_ops.c
@@ -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.
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v2] PM: Introduce DEFINE_PM_GENERIC_FUNC macro to reduce code duplication
2025-10-14 7:22 [PATCH v2] PM: Introduce DEFINE_PM_GENERIC_FUNC macro to reduce code duplication Kaushlendra Kumar
@ 2025-10-14 9:55 ` Dhruva Gole
2025-10-14 17:12 ` Rafael J. Wysocki
0 siblings, 1 reply; 3+ messages in thread
From: Dhruva Gole @ 2025-10-14 9:55 UTC (permalink / raw)
To: Kaushlendra Kumar; +Cc: rafael, pavel, dakr, gregkh, linux-pm
On Oct 14, 2025 at 12:52:03 +0530, Kaushlendra Kumar wrote:
> 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.
>
> Signed-off-by: Kaushlendra Kumar <kaushlendra.kumar@intel.com>
> ---
> 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(-)
>
> diff --git a/drivers/base/power/generic_ops.c b/drivers/base/power/generic_ops.c
> index 6502720bb564..0afea5d8f8ef 100644
> --- a/drivers/base/power/generic_ops.c
> +++ b/drivers/base/power/generic_ops.c
> @@ -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)
> +
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.
> #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);
[...]
--
Best regards,
Dhruva Gole
Texas Instruments Incorporated
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v2] PM: Introduce DEFINE_PM_GENERIC_FUNC macro to reduce code duplication
2025-10-14 9:55 ` Dhruva Gole
@ 2025-10-14 17:12 ` Rafael J. Wysocki
0 siblings, 0 replies; 3+ messages in thread
From: Rafael J. Wysocki @ 2025-10-14 17:12 UTC (permalink / raw)
To: Dhruva Gole; +Cc: Kaushlendra Kumar, rafael, pavel, dakr, gregkh, linux-pm
On Tue, Oct 14, 2025 at 11:55 AM Dhruva Gole <d-gole@ti.com> wrote:
>
> On Oct 14, 2025 at 12:52:03 +0530, Kaushlendra Kumar wrote:
> > 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.
> >
> > Signed-off-by: Kaushlendra Kumar <kaushlendra.kumar@intel.com>
> > ---
> > 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(-)
> >
> > diff --git a/drivers/base/power/generic_ops.c b/drivers/base/power/generic_ops.c
> > index 6502720bb564..0afea5d8f8ef 100644
> > --- a/drivers/base/power/generic_ops.c
> > +++ b/drivers/base/power/generic_ops.c
> > @@ -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)
> > +
>
> 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.
OK, so it was my comment that triggered this, maybe you'd prefer the
previous version:
https://lore.kernel.org/linux-pm/20250919124437.3075016-1-kaushlendra.kumar@intel.com/
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-10-14 17:13 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-14 7:22 [PATCH v2] PM: Introduce DEFINE_PM_GENERIC_FUNC macro to reduce code duplication Kaushlendra Kumar
2025-10-14 9:55 ` Dhruva Gole
2025-10-14 17:12 ` Rafael J. Wysocki
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox