Re: [PATCH 1/2] pm: runtime: Simplify pm_runtime_get_if_active() usage

Rodrigo Vivi
2024-01-09T17:35:37Z

On Tue, Jan 09, 2024 at 03:36:57PM +0200, Sakari Ailus wrote:
> There are two ways to opportunistically increment a device's runtime PM
> usage count, calling either pm_runtime_get_if_active() or
> pm_runtime_get_if_in_use(). The former has an argument to tell whether to
> ignore the usage count or not, and the latter simply calls the former with
> ign_usage_count set to false. The other users that want to ignore the
> usage_count will have to explitly set that argument to true which is a bit
> cumbersome.
>
> To make this function more practical to use, remove the ign_usage_count
> argument from the function. The main implementation is renamed as
> __pm_runtime_get_conditional().

Great idea! I recently found a bug in Xe caused by some confusion
with the ign_usage_count var.

> + * This function is not intended to be called by drivers, use
> + * pm_runtime_get_if_active() or pm_runtime_get_if_in_use() instead.
> */

Well, it is not intended, but we already have an usage for that.
So, shouldn't we avoid the '__' and just put as recommendation?
Like we have around the get_sync telling that the preferred way is
the resume_and_get for a cleaner code?

> -EXPORT_SYMBOL_GPL(pm_runtime_get_if_active);
> +EXPORT_SYMBOL_GPL(__pm_runtime_get_conditional);

Also the '__' in the exported and documented one doesn't look so nice.
Well, I know that this would be the case even without any driver using
because of the static inline anyway.

>
> /**
> * __pm_runtime_set_status - Set runtime PM status of a device.
> diff --git a/drivers/gpu/drm/i915/intel_runtime_pm.c b/drivers/gpu/drm/i915/intel_runtime_pm.c
> index 860b51b56a92..51cbca330bd5 100644
> --- a/drivers/gpu/drm/i915/intel_runtime_pm.c
> +++ b/drivers/gpu/drm/i915/intel_runtime_pm.c
> @@ -246,7 +246,7 @@ static intel_wakeref_t __intel_runtime_pm_get_if_active(struct intel_runtime_pm
> * function, since the power state is undefined. This applies
> * atm to the late/early system suspend/resume handlers.
> */
> - if (pm_runtime_get_if_active(rpm->kdev, ignore_usecount) <= 0)
> + if (__pm_runtime_get_conditional(rpm->kdev, ignore_usecount) <= 0)

Acked-by: Rodrigo Vivi <rodrigo.vivi@intel.com>

> return 0;
> }
>
> diff --git a/drivers/gpu/drm/xe/xe_pm.c b/drivers/gpu/drm/xe/xe_pm.c
> index b429c2876a76..dd110058bf74 100644
> --- a/drivers/gpu/drm/xe/xe_pm.c
> +++ b/drivers/gpu/drm/xe/xe_pm.c
> @@ -330,7 +330,7 @@ int xe_pm_runtime_put(struct xe_device *xe)
>
> int xe_pm_runtime_get_if_active(struct xe_device *xe)
> {
> - return pm_runtime_get_if_active(xe->drm.dev, true);
> + return pm_runtime_get_if_active(xe->drm.dev);
> }
>
> void xe_pm_assert_unbounded_bridge(struct xe_device *xe)
> diff --git a/drivers/media/i2c/ccs/ccs-core.c b/drivers/media/i2c/ccs/ccs-core.c
> index e21287d50c15..e1ae0f9fad43 100644
> --- a/drivers/media/i2c/ccs/ccs-core.c
> +++ b/drivers/media/i2c/ccs/ccs-core.c
> @@ -674,7 +674,7 @@ static int ccs_set_ctrl(struct v4l2_ctrl *ctrl)
> break;
> }
>
> - pm_status = pm_runtime_get_if_active(&client->dev, true);
> + pm_status = pm_runtime_get_if_active(&client->dev);
> if (!pm_status)
> return 0;
>
> diff --git a/drivers/media/i2c/ov64a40.c b/drivers/media/i2c/ov64a40.c
> index 4fba4c2cb064..541bf74581d2 100644
> --- a/drivers/media/i2c/ov64a40.c
> +++ b/drivers/media/i2c/ov64a40.c
> @@ -3287,7 +3287,7 @@ static int ov64a40_set_ctrl(struct v4l2_ctrl *ctrl)
> exp_max, 1, exp_val);
> }
>
> - pm_status = pm_runtime_get_if_active(ov64a40->dev, true);
> + pm_status = pm_runtime_get_if_active(ov64a40->dev);
> if (!pm_status)
> return 0;
>
> diff --git a/drivers/media/i2c/thp7312.c b/drivers/media/i2c/thp7312.c
> index 2806887514dc..19bd923a7315 100644
> --- a/drivers/media/i2c/thp7312.c
> +++ b/drivers/media/i2c/thp7312.c
> @@ -1052,7 +1052,7 @@ static int thp7312_s_ctrl(struct v4l2_ctrl *ctrl)
> if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
> return -EINVAL;
>
> - if (!pm_runtime_get_if_active(thp7312->dev, true))
> + if (!pm_runtime_get_if_active(thp7312->dev))
> return 0;
>
> switch (ctrl->id) {
> diff --git a/drivers/net/ipa/ipa_smp2p.c b/drivers/net/ipa/ipa_smp2p.c
> index 5620dc271fac..cbf3d4761ce3 100644
> --- a/drivers/net/ipa/ipa_smp2p.c
> +++ b/drivers/net/ipa/ipa_smp2p.c
> @@ -92,7 +92,7 @@ static void ipa_smp2p_notify(struct ipa_smp2p *smp2p)
> return;
>
> dev = &smp2p->ipa->pdev->dev;
> - smp2p->power_on = pm_runtime_get_if_active(dev, true) > 0;
> + smp2p->power_on = pm_runtime_get_if_active(dev) > 0;
>
> /* Signal whether the IPA power is enabled */
> mask = BIT(smp2p->enabled_bit);
> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> index d8f11a078924..f8293ae71389 100644
> --- a/drivers/pci/pci.c
> +++ b/drivers/pci/pci.c
> @@ -2510,7 +2510,7 @@ static void pci_pme_list_scan(struct work_struct *work)
> * If the device is in a low power state it
> * should not be polled either.
> */
> - pm_status = pm_runtime_get_if_active(dev, true);
> + pm_status = pm_runtime_get_if_active(dev);
> if (!pm_status)
> continue;
>
> diff --git a/include/linux/pm_runtime.h b/include/linux/pm_runtime.h
> index 7c9b35448563..13cd526634c1 100644
> --- a/include/linux/pm_runtime.h
> +++ b/include/linux/pm_runtime.h
> @@ -72,7 +72,8 @@ extern int pm_runtime_force_resume(struct device *dev);
> extern int __pm_runtime_idle(struct device *dev, int rpmflags);
> extern int __pm_runtime_suspend(struct device *dev, int rpmflags);
> extern int __pm_runtime_resume(struct device *dev, int rpmflags);
> -extern int pm_runtime_get_if_active(struct device *dev, bool ign_usage_count);
> +extern int __pm_runtime_get_conditional(struct device *dev,
> + bool ign_usage_count);
> extern int pm_schedule_suspend(struct device *dev, unsigned int delay);
> extern int __pm_runtime_set_status(struct device *dev, unsigned int status);
> extern int pm_runtime_barrier(struct device *dev);
> @@ -94,16 +95,33 @@ extern void pm_runtime_release_supplier(struct device_link *link);
>
> extern int devm_pm_runtime_enable(struct device *dev);
>
> +/**
> + * pm_runtime_get_if_active - Bump up runtime PM usage counter if the device is
> + * in active state
> + * @dev: Target device.
> + *
> + * Increment the runtime PM usage counter of @dev if its runtime PM status is
> + * %RPM_ACTIVE, in which case it returns 1. If the device is in a different
> + * state, 0 is returned. -EINVAL is returned if runtime PM is disabled for the
> + * device.
> + */
> +static inline int pm_runtime_get_if_active(struct device *dev)
> +{
> + return __pm_runtime_get_conditional(dev, true);
> +}
> +
> /**
> * pm_runtime_get_if_in_use - Conditionally bump up runtime PM usage counter.
> * @dev: Target device.
> *
> * Increment the runtime PM usage counter of @dev if its runtime PM status is
> - * %RPM_ACTIVE and its runtime PM usage counter is greater than 0.
> + * %RPM_ACTIVE and its runtime PM usage counter is greater than 0, in which case
> + * it returns 1. If the device is in a different state or its usage_count is 0,
> + * 0 is returned. -EINVAL is returned if runtime PM is disabled for the device.
> */
> static inline int pm_runtime_get_if_in_use(struct device *dev)
> {
> - return pm_runtime_get_if_active(dev, false);
> + return __pm_runtime_get_conditional(dev, false);
> }
>
> /**
> @@ -275,8 +293,12 @@ static inline int pm_runtime_get_if_in_use(struct device *dev)
> {
> return -EINVAL;
> }
> -static inline int pm_runtime_get_if_active(struct device *dev,
> - bool ign_usage_count)
> +static inline int pm_runtime_get_if_active(struct device *dev)
> +{
> + return -EINVAL;
> +}
> +static inline int __pm_runtime_get_conditional(struct device *dev,
> + bool ign_usage_count)
> {
> return -EINVAL;
> }
> diff --git a/sound/hda/hdac_device.c b/sound/hda/hdac_device.c
> index 7f7b67fe1b65..068c16e52dff 100644
> --- a/sound/hda/hdac_device.c
> +++ b/sound/hda/hdac_device.c
> @@ -612,7 +612,7 @@ EXPORT_SYMBOL_GPL(snd_hdac_power_up_pm);
> int snd_hdac_keep_power_up(struct hdac_device *codec)
> {
> if (!atomic_inc_not_zero(&codec->in_pm)) {
> - int ret = pm_runtime_get_if_active(&codec->dev, true);
> + int ret = pm_runtime_get_if_active(&codec->dev);
> if (!ret)
> return -1;
> if (ret < 0)
> --
> 2.39.2
>