Re: [PATCH v2] clk: qcom: gdsc: Remove direct runtime PM calls

From: Johan Hovold
Date: Thu Nov 03 2022 - 10:31:38 EST


On Wed, Nov 02, 2022 at 10:07:17AM -0700, Stephen Boyd wrote:
> We shouldn't be calling runtime PM APIs from within the genpd
> enable/disable path for a couple reasons.
>
> First, this causes an AA lockdep splat because genpd can call into genpd
> code again while holding the genpd lock.
>
> WARNING: possible recursive locking detected

> Second, this confuses runtime PM on CoachZ for the camera devices by
> causing the camera clock controller's runtime PM usage_count to go
> negative after resuming from suspend. This is because runtime PM is
> being used on the clock controller while runtime PM is disabled for the
> device.
>
> The reason for the negative count is because a GDSC is represented as a
> genpd and each genpd that is attached to a device is resumed during the
> noirq phase of system wide suspend/resume (see the noirq suspend ops
> assignment in pm_genpd_init() for more details). The camera GDSCs are
> attached to camera devices with the 'power-domains' property in DT.
> Every device has runtime PM disabled in the late system suspend phase
> via __device_suspend_late(). Runtime PM is not usable until runtime PM
> is enabled in device_resume_early(). The noirq phases run after the
> 'late' and before the 'early' phase of suspend/resume. When the genpds
> are resumed in genpd_resume_noirq(), we call down into gdsc_enable()
> that calls pm_runtime_resume_and_get() and that returns -EACCES to
> indicate failure to resume because runtime PM is disabled for all
> devices.

Probably worth mentioning the fact that those runtime PM calls
unconditionally failing during resume means that the GDSCs are never
even enabled.

Seems like the PM runtime usage counters would still be balanced after
this though as they are decremented also on failure during suspend (i.e.
domain remains off and no usage counter is incremented during resume).

But this is clearly just very broken.

> Upon closer inspection, calling runtime PM APIs like this in the GDSC
> driver doesn't make sense. It was intended to make sure the GDSC for the
> clock controller providing other GDSCs was enabled, specifically the
> MMCX GDSC for the display clk controller on SM8250 (sm8250-dispcc), so
> that GDSC register accesses succeeded. That will already happen because
> we make the 'dev->pm_domain' a parent domain of each GDSC we register in
> gdsc_register() via pm_genpd_add_subdomain(). When any of these GDSCs
> are accessed, we'll enable the parent domain (in this specific case
> MMCX).
>
> We also remove any getting of runtime PM during registration, because
> when a genpd is registered it increments the count on the parent if the
> genpd itself is already enabled. And finally, the runtime PM state of
> the clk controller registering the GDSC shouldn't matter to the
> subdomain setup. Therefore we always assign 'dev' unconditionally so
> when GDSCs are removed we properly unlink the GDSC from the clk
> controller's pm_domain.

This last bit makes no sense as 'dev' was only used for the runtime PM
management and should be removed by this patch.

> Cc: Dmitry Baryshkov <dmitry.baryshkov@xxxxxxxxxx>
> Cc: Johan Hovold <johan+linaro@xxxxxxxxxx>
> Cc: Ulf Hansson <ulf.hansson@xxxxxxxxxx>
> Cc: Taniya Das <quic_tdas@xxxxxxxxxxx>
> Cc: Satya Priya <quic_c_skakit@xxxxxxxxxxx>
> Cc: Douglas Anderson <dianders@xxxxxxxxxxxx>
> Cc: Matthias Kaehlcke <mka@xxxxxxxxxxxx>
> Reported-by: Stephen Boyd <swboyd@xxxxxxxxxxxx>
> Fixes: 1b771839de05 ("clk: qcom: gdsc: enable optional power domain support")
> Signed-off-by: Stephen Boyd <swboyd@xxxxxxxxxxxx>
> ---
>
> Changes from v1 (https://lore.kernel.org/r/20221101233421.997149-1-swboyd@xxxxxxxxxxxx):
> * Fix ret thinko
> * Update kerneldoc on 'dev' member

No credit to reviewers (e.g. names in parentheses)?

> drivers/clk/qcom/gdsc.c | 62 +++++------------------------------------
> drivers/clk/qcom/gdsc.h | 2 +-
> 2 files changed, 8 insertions(+), 56 deletions(-)
>
> diff --git a/drivers/clk/qcom/gdsc.c b/drivers/clk/qcom/gdsc.c
> index 7cf5e130e92f..36d44eec03b6 100644
> --- a/drivers/clk/qcom/gdsc.c
> +++ b/drivers/clk/qcom/gdsc.c

> @@ -56,22 +55,6 @@ enum gdsc_status {
> GDSC_ON
> };
>
> -static int gdsc_pm_runtime_get(struct gdsc *sc)
> -{
> - if (!sc->dev)
> - return 0;
> -
> - return pm_runtime_resume_and_get(sc->dev);
> -}
> -
> -static int gdsc_pm_runtime_put(struct gdsc *sc)
> -{
> - if (!sc->dev)
> - return 0;
> -
> - return pm_runtime_put_sync(sc->dev);
> -}
> -

> -static int gdsc_enable(struct generic_pm_domain *domain)
> +static int gdsc_disable(struct generic_pm_domain *domain)
> {
> struct gdsc *sc = domain_to_gdsc(domain);
> int ret;
>
> - ret = gdsc_pm_runtime_get(sc);
> - if (ret)
> - return ret;
> -
> - return _gdsc_enable(sc);
> -}

> -static int gdsc_disable(struct generic_pm_domain *domain)
> -{
> - struct gdsc *sc = domain_to_gdsc(domain);
> - int ret;
> -
> - ret = _gdsc_disable(sc);
> -
> - gdsc_pm_runtime_put(sc);
> -
> - return ret;
> -}

> @@ -541,8 +494,7 @@ int gdsc_register(struct gdsc_desc *desc,
> for (i = 0; i < num; i++) {
> if (!scs[i])
> continue;
> - if (pm_runtime_enabled(dev))
> - scs[i]->dev = dev;
> + scs[i]->dev = dev;

As mentioned above, the gdsc dev pointer is now unused and should be
removed.

> scs[i]->regmap = regmap;
> scs[i]->rcdev = rcdev;
> ret = gdsc_init(scs[i]);
> diff --git a/drivers/clk/qcom/gdsc.h b/drivers/clk/qcom/gdsc.h
> index 981a12c8502d..47de72834a85 100644
> --- a/drivers/clk/qcom/gdsc.h
> +++ b/drivers/clk/qcom/gdsc.h
> @@ -30,7 +30,7 @@ struct reset_controller_dev;
> * @resets: ids of resets associated with this gdsc
> * @reset_count: number of @resets
> * @rcdev: reset controller
> - * @dev: the device holding the GDSC, used for pm_runtime calls
> + * @dev: the device providing the GDSC
> */
> struct gdsc {
> struct generic_pm_domain pd;
>
> base-commit: 9abf2313adc1ca1b6180c508c25f22f9395cc780

Johan