diff mbox

PM / Domains: defer dev_pm_domain_set() until genpd->attach_dev succeeds if present

Message ID 1498759251-11709-1-git-send-email-sudeep.holla@arm.com (mailing list archive)
State Changes Requested, archived
Delegated to: Rafael Wysocki
Headers show

Commit Message

Sudeep Holla June 29, 2017, 6 p.m. UTC
If the genpd->attach_dev or genpd->power_on fails, genpd_dev_pm_attach
may return -EPROBE_DEFER initially. However genpd_alloc_dev_data sets
the PM domain for the device unconditionally.

When subsequent attempts are made to call genpd_dev_pm_attach, it may
return -EEXISTS checking dev->pm_domain without re-attempting to call
attach_dev or power_on.

platform_drv_probe then attempts to call drv->probe as the return value
-EEXIST != -EPROBE_DEFER, which may end up in a situation where the
device is accessed without it's power domain switched on.

Cc: "Rafael J. Wysocki" <rjw@rjwysocki.net>
Cc: Kevin Hilman <khilman@kernel.org>
Cc: Ulf Hansson <ulf.hansson@linaro.org>
Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
---
 drivers/base/power/domain.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Rafael J. Wysocki July 4, 2017, 8:39 p.m. UTC | #1
On Thu, Jun 29, 2017 at 8:00 PM, Sudeep Holla <sudeep.holla@arm.com> wrote:
> If the genpd->attach_dev or genpd->power_on fails, genpd_dev_pm_attach
> may return -EPROBE_DEFER initially. However genpd_alloc_dev_data sets
> the PM domain for the device unconditionally.
>
> When subsequent attempts are made to call genpd_dev_pm_attach, it may
> return -EEXISTS checking dev->pm_domain without re-attempting to call
> attach_dev or power_on.
>
> platform_drv_probe then attempts to call drv->probe as the return value
> -EEXIST != -EPROBE_DEFER, which may end up in a situation where the
> device is accessed without it's power domain switched on.
>
> Cc: "Rafael J. Wysocki" <rjw@rjwysocki.net>
> Cc: Kevin Hilman <khilman@kernel.org>
> Cc: Ulf Hansson <ulf.hansson@linaro.org>
> Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>

Ulf, this looks like a genuine fix to me, any comments?

> ---
>  drivers/base/power/domain.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/base/power/domain.c b/drivers/base/power/domain.c
> index da49a8383dc3..b195d34de888 100644
> --- a/drivers/base/power/domain.c
> +++ b/drivers/base/power/domain.c
> @@ -1168,8 +1168,6 @@ static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
>
>         spin_unlock_irq(&dev->power.lock);
>
> -       dev_pm_domain_set(dev, &genpd->domain);
> -
>         return gpd_data;
>
>   err_free:
> @@ -1221,6 +1219,8 @@ static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
>         if (ret)
>                 goto out;
>
> +       dev_pm_domain_set(dev, &genpd->domain);
> +
>         genpd->device_count++;
>         genpd->max_off_time_changed = true;
>
> --
> 2.7.4
>
Kevin Hilman July 11, 2017, 6:53 p.m. UTC | #2
"Rafael J. Wysocki" <rafael@kernel.org> writes:

> On Thu, Jun 29, 2017 at 8:00 PM, Sudeep Holla <sudeep.holla@arm.com> wrote:
>> If the genpd->attach_dev or genpd->power_on fails, genpd_dev_pm_attach
>> may return -EPROBE_DEFER initially. However genpd_alloc_dev_data sets
>> the PM domain for the device unconditionally.
>>
>> When subsequent attempts are made to call genpd_dev_pm_attach, it may
>> return -EEXISTS checking dev->pm_domain without re-attempting to call
>> attach_dev or power_on.
>>
>> platform_drv_probe then attempts to call drv->probe as the return value
>> -EEXIST != -EPROBE_DEFER, which may end up in a situation where the
>> device is accessed without it's power domain switched on.
>>
>> Cc: "Rafael J. Wysocki" <rjw@rjwysocki.net>
>> Cc: Kevin Hilman <khilman@kernel.org>
>> Cc: Ulf Hansson <ulf.hansson@linaro.org>
>> Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
>
> Ulf, this looks like a genuine fix to me, any comments?
>

This looks like the right fix to me.

Acked-by: Kevin Hilman <khilman@baylibre.com>
Ulf Hansson July 12, 2017, 1:59 p.m. UTC | #3
On 29 June 2017 at 20:00, Sudeep Holla <sudeep.holla@arm.com> wrote:
> If the genpd->attach_dev or genpd->power_on fails, genpd_dev_pm_attach
> may return -EPROBE_DEFER initially. However genpd_alloc_dev_data sets
> the PM domain for the device unconditionally.
>
> When subsequent attempts are made to call genpd_dev_pm_attach, it may
> return -EEXISTS checking dev->pm_domain without re-attempting to call
> attach_dev or power_on.
>
> platform_drv_probe then attempts to call drv->probe as the return value
> -EEXIST != -EPROBE_DEFER, which may end up in a situation where the
> device is accessed without it's power domain switched on.

Right, this makes sense.

>
> Cc: "Rafael J. Wysocki" <rjw@rjwysocki.net>
> Cc: Kevin Hilman <khilman@kernel.org>
> Cc: Ulf Hansson <ulf.hansson@linaro.org>
> Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>

Could we perhaps work out which commit it fixes, or perhaps the
problem been there long time ago and we should just add a stable tag?

> ---
>  drivers/base/power/domain.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/base/power/domain.c b/drivers/base/power/domain.c
> index da49a8383dc3..b195d34de888 100644
> --- a/drivers/base/power/domain.c
> +++ b/drivers/base/power/domain.c
> @@ -1168,8 +1168,6 @@ static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
>
>         spin_unlock_irq(&dev->power.lock);
>
> -       dev_pm_domain_set(dev, &genpd->domain);
> -
>         return gpd_data;
>
>   err_free:
> @@ -1221,6 +1219,8 @@ static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
>         if (ret)
>                 goto out;
>
> +       dev_pm_domain_set(dev, &genpd->domain);
> +
>         genpd->device_count++;
>         genpd->max_off_time_changed = true;
>
> --
> 2.7.4
>

One piece is missing to make this fix complete.

More precisely, you must also move the call to dev_pm_domain_set(dev, NULL).

Currently that is done from genpd_free_dev_data(), as it corresponds
to genpd_alloc_dev_data(), but clearly the proper place to call it
should be from genpd_remove_device().

Kind regards
Uffe
Sudeep Holla July 12, 2017, 4:36 p.m. UTC | #4
On 12/07/17 14:59, Ulf Hansson wrote:
> On 29 June 2017 at 20:00, Sudeep Holla <sudeep.holla@arm.com> wrote:
>> If the genpd->attach_dev or genpd->power_on fails, genpd_dev_pm_attach
>> may return -EPROBE_DEFER initially. However genpd_alloc_dev_data sets
>> the PM domain for the device unconditionally.
>>
>> When subsequent attempts are made to call genpd_dev_pm_attach, it may
>> return -EEXISTS checking dev->pm_domain without re-attempting to call
>> attach_dev or power_on.
>>
>> platform_drv_probe then attempts to call drv->probe as the return value
>> -EEXIST != -EPROBE_DEFER, which may end up in a situation where the
>> device is accessed without it's power domain switched on.
> 
> Right, this makes sense.
> 

Thanks

>>
>> Cc: "Rafael J. Wysocki" <rjw@rjwysocki.net>
>> Cc: Kevin Hilman <khilman@kernel.org>
>> Cc: Ulf Hansson <ulf.hansson@linaro.org>
>> Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
> 
> Could we perhaps work out which commit it fixes, or perhaps the
> problem been there long time ago and we should just add a stable tag?
>

OK I will dig that out.

>> ---
>>  drivers/base/power/domain.c | 4 ++--
>>  1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/base/power/domain.c b/drivers/base/power/domain.c
>> index da49a8383dc3..b195d34de888 100644
>> --- a/drivers/base/power/domain.c
>> +++ b/drivers/base/power/domain.c
>> @@ -1168,8 +1168,6 @@ static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
>>
>>         spin_unlock_irq(&dev->power.lock);
>>
>> -       dev_pm_domain_set(dev, &genpd->domain);
>> -
>>         return gpd_data;
>>
>>   err_free:
>> @@ -1221,6 +1219,8 @@ static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
>>         if (ret)
>>                 goto out;
>>
>> +       dev_pm_domain_set(dev, &genpd->domain);
>> +
>>         genpd->device_count++;
>>         genpd->max_off_time_changed = true;
>>
>> --
>> 2.7.4
>>
> 
> One piece is missing to make this fix complete.
> 
> More precisely, you must also move the call to dev_pm_domain_set(dev, NULL).
> 

Sure will add.

> Currently that is done from genpd_free_dev_data(), as it corresponds
> to genpd_alloc_dev_data(), but clearly the proper place to call it
> should be from genpd_remove_device().
> 

OK, will also look into that.
Sudeep Holla July 14, 2017, 10:31 a.m. UTC | #5
On 12/07/17 17:36, Sudeep Holla wrote:
> 
> 
> On 12/07/17 14:59, Ulf Hansson wrote:
>> On 29 June 2017 at 20:00, Sudeep Holla <sudeep.holla@arm.com> wrote:
>>> If the genpd->attach_dev or genpd->power_on fails, genpd_dev_pm_attach
>>> may return -EPROBE_DEFER initially. However genpd_alloc_dev_data sets
>>> the PM domain for the device unconditionally.
>>>
>>> When subsequent attempts are made to call genpd_dev_pm_attach, it may
>>> return -EEXISTS checking dev->pm_domain without re-attempting to call
>>> attach_dev or power_on.
>>>
>>> platform_drv_probe then attempts to call drv->probe as the return value
>>> -EEXIST != -EPROBE_DEFER, which may end up in a situation where the
>>> device is accessed without it's power domain switched on.
>>
>> Right, this makes sense.
>>
> 
> Thanks
> 
>>>
>>> Cc: "Rafael J. Wysocki" <rjw@rjwysocki.net>
>>> Cc: Kevin Hilman <khilman@kernel.org>
>>> Cc: Ulf Hansson <ulf.hansson@linaro.org>
>>> Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
>>
>> Could we perhaps work out which commit it fixes, or perhaps the
>> problem been there long time ago and we should just add a stable tag?
>>
> 
> OK I will dig that out.
> 

It looks like commit 989561de9b51 ("PM / Domains: add setter for
dev.pm_domain") added the helper. I then follow it to commit
f104e1e5ef57 ("PM / Domains: Re-order initialization of
generic_pm_domain_data"). The code in the original commit 6ff7bb0d02f8
("PM / Domains: Cache device stop and domain power off governor results,
v3") looks OK to me.

So What should I do ? Add
Fixes: f104e1e5ef57 ("PM / Domains: Re-order initialization of
generic_pm_domain_data")

tag in the mainline. And when Greg or others fix up before v4.4, I need
to work out the fix for that stable version ?
diff mbox

Patch

diff --git a/drivers/base/power/domain.c b/drivers/base/power/domain.c
index da49a8383dc3..b195d34de888 100644
--- a/drivers/base/power/domain.c
+++ b/drivers/base/power/domain.c
@@ -1168,8 +1168,6 @@  static struct generic_pm_domain_data *genpd_alloc_dev_data(struct device *dev,
 
 	spin_unlock_irq(&dev->power.lock);
 
-	dev_pm_domain_set(dev, &genpd->domain);
-
 	return gpd_data;
 
  err_free:
@@ -1221,6 +1219,8 @@  static int genpd_add_device(struct generic_pm_domain *genpd, struct device *dev,
 	if (ret)
 		goto out;
 
+	dev_pm_domain_set(dev, &genpd->domain);
+
 	genpd->device_count++;
 	genpd->max_off_time_changed = true;