linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] power_supply: power_supply_read_temp only if use_cnt > 0
@ 2016-06-03 20:30 Rhyland Klein
  2016-06-04 13:30 ` Sebastian Reichel
  0 siblings, 1 reply; 4+ messages in thread
From: Rhyland Klein @ 2016-06-03 20:30 UTC (permalink / raw)
  To: Sebastian Reichel, Dmitry Eremin-Solenikov, David Woodhouse
  Cc: linux-pm, linux-kernel, Jon Hunter, Krzysztof Kozlowski, Rhyland Klein

Add check to power_supply_read_temp() to only use the power_supply
get_property() callback if the use_cnt is > 0. The use_cnt will
be incremented at the end of __power_supply_register, so this will
block to case where get_property can be called before the supply
is fully registered. This fixes the issue show in the stack below:

[    1.452598] power_supply_read_temp+0x78/0x80
[    1.458680] thermal_zone_get_temp+0x5c/0x11c
[    1.464765] thermal_zone_device_update+0x34/0xb4
[    1.471195] thermal_zone_device_register+0x87c/0x8cc
[    1.477974] __power_supply_register+0x364/0x424
[    1.484317] power_supply_register_no_ws+0x10/0x18
[    1.490833] bq27xxx_battery_setup+0x10c/0x164
[    1.497003] bq27xxx_battery_i2c_probe+0xd0/0x1b0
[    1.503435] i2c_device_probe+0x174/0x240
[    1.509172] driver_probe_device+0x1fc/0x29c
[    1.515167] __driver_attach+0xa4/0xa8
[    1.520643] bus_for_each_dev+0x58/0x98
[    1.526204] driver_attach+0x20/0x28
[    1.531505] bus_add_driver+0x1c8/0x22c
[    1.537067] driver_register+0x68/0x108
[    1.542630] i2c_register_driver+0x38/0x7c
[    1.548457] bq27xxx_battery_i2c_driver_init+0x18/0x20
[    1.555321] do_one_initcall+0x38/0x12c
[    1.560886] kernel_init_freeable+0x148/0x1ec
[    1.566972] kernel_init+0x10/0xfc
[    1.572101] ret_from_fork+0x10/0x40

Signed-off-by: Rhyland Klein <rklein@nvidia.com>
---
 drivers/power/power_supply_core.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c
index 456987c88baa..070fec621d66 100644
--- a/drivers/power/power_supply_core.c
+++ b/drivers/power/power_supply_core.c
@@ -561,11 +561,16 @@ static int power_supply_read_temp(struct thermal_zone_device *tzd,
 {
 	struct power_supply *psy;
 	union power_supply_propval val;
-	int ret;
+	int ret = 0;
 
 	WARN_ON(tzd == NULL);
+
 	psy = tzd->devdata;
-	ret = psy->desc->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
+	WARN_ON(atomic_read(&psy->use_cnt) == 0);
+
+	if (atomic_read(&psy->use_cnt) > 0)
+		ret = psy->desc->get_property(psy, POWER_SUPPLY_PROP_TEMP,
+					      &val);
 
 	/* Convert tenths of degree Celsius to milli degree Celsius. */
 	if (!ret)
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] power_supply: power_supply_read_temp only if use_cnt > 0
  2016-06-03 20:30 [PATCH] power_supply: power_supply_read_temp only if use_cnt > 0 Rhyland Klein
@ 2016-06-04 13:30 ` Sebastian Reichel
  2016-06-06 15:39   ` Rhyland Klein
  0 siblings, 1 reply; 4+ messages in thread
From: Sebastian Reichel @ 2016-06-04 13:30 UTC (permalink / raw)
  To: Rhyland Klein
  Cc: Dmitry Eremin-Solenikov, David Woodhouse, linux-pm, linux-kernel,
	Jon Hunter, Krzysztof Kozlowski

[-- Attachment #1: Type: text/plain, Size: 2748 bytes --]

Hi Rhyland,

Thanks for the patch.

On Fri, Jun 03, 2016 at 04:30:50PM -0400, Rhyland Klein wrote:
> Add check to power_supply_read_temp() to only use the power_supply
> get_property() callback if the use_cnt is > 0. The use_cnt will
> be incremented at the end of __power_supply_register, so this will
> block to case where get_property can be called before the supply
> is fully registered. This fixes the issue show in the stack below:
> 
> [    1.452598] power_supply_read_temp+0x78/0x80
> [    1.458680] thermal_zone_get_temp+0x5c/0x11c
> [    1.464765] thermal_zone_device_update+0x34/0xb4
> [    1.471195] thermal_zone_device_register+0x87c/0x8cc
> [    1.477974] __power_supply_register+0x364/0x424
> [    1.484317] power_supply_register_no_ws+0x10/0x18
> [    1.490833] bq27xxx_battery_setup+0x10c/0x164
> [    1.497003] bq27xxx_battery_i2c_probe+0xd0/0x1b0
> [    1.503435] i2c_device_probe+0x174/0x240
> [    1.509172] driver_probe_device+0x1fc/0x29c
> [    1.515167] __driver_attach+0xa4/0xa8
> [    1.520643] bus_for_each_dev+0x58/0x98
> [    1.526204] driver_attach+0x20/0x28
> [    1.531505] bus_add_driver+0x1c8/0x22c
> [    1.537067] driver_register+0x68/0x108
> [    1.542630] i2c_register_driver+0x38/0x7c
> [    1.548457] bq27xxx_battery_i2c_driver_init+0x18/0x20
> [    1.555321] do_one_initcall+0x38/0x12c
> [    1.560886] kernel_init_freeable+0x148/0x1ec
> [    1.566972] kernel_init+0x10/0xfc
> [    1.572101] ret_from_fork+0x10/0x40

So I guess this should be backported to stable kernels.

> Signed-off-by: Rhyland Klein <rklein@nvidia.com>
> ---
>  drivers/power/power_supply_core.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c
> index 456987c88baa..070fec621d66 100644
> --- a/drivers/power/power_supply_core.c
> +++ b/drivers/power/power_supply_core.c
> @@ -561,11 +561,16 @@ static int power_supply_read_temp(struct thermal_zone_device *tzd,
>  {
>  	struct power_supply *psy;
>  	union power_supply_propval val;
> -	int ret;
> +	int ret = 0;

ret should probably be initialized to -EAGAIN to avoid val being
interpreted without get_property() having been called at all?

>  	WARN_ON(tzd == NULL);
> +
>  	psy = tzd->devdata;
> -	ret = psy->desc->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
> +	WARN_ON(atomic_read(&psy->use_cnt) == 0);

I guess the warning can be dropped if error code is properly reported?

> +	if (atomic_read(&psy->use_cnt) > 0)
> +		ret = psy->desc->get_property(psy, POWER_SUPPLY_PROP_TEMP,
> +					      &val);
>  
>  	/* Convert tenths of degree Celsius to milli degree Celsius. */
>  	if (!ret)

-- Sebastian

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] power_supply: power_supply_read_temp only if use_cnt > 0
  2016-06-04 13:30 ` Sebastian Reichel
@ 2016-06-06 15:39   ` Rhyland Klein
  0 siblings, 0 replies; 4+ messages in thread
From: Rhyland Klein @ 2016-06-06 15:39 UTC (permalink / raw)
  To: Sebastian Reichel
  Cc: Dmitry Eremin-Solenikov, David Woodhouse, linux-pm, linux-kernel,
	Jon Hunter, Krzysztof Kozlowski

On 6/4/2016 9:30 AM, Sebastian Reichel wrote:
> * PGP Signed by an unknown key
> 
> Hi Rhyland,
> 
> Thanks for the patch.
> 
> On Fri, Jun 03, 2016 at 04:30:50PM -0400, Rhyland Klein wrote:
>> Add check to power_supply_read_temp() to only use the power_supply
>> get_property() callback if the use_cnt is > 0. The use_cnt will
>> be incremented at the end of __power_supply_register, so this will
>> block to case where get_property can be called before the supply
>> is fully registered. This fixes the issue show in the stack below:
>>
>> [    1.452598] power_supply_read_temp+0x78/0x80
>> [    1.458680] thermal_zone_get_temp+0x5c/0x11c
>> [    1.464765] thermal_zone_device_update+0x34/0xb4
>> [    1.471195] thermal_zone_device_register+0x87c/0x8cc
>> [    1.477974] __power_supply_register+0x364/0x424
>> [    1.484317] power_supply_register_no_ws+0x10/0x18
>> [    1.490833] bq27xxx_battery_setup+0x10c/0x164
>> [    1.497003] bq27xxx_battery_i2c_probe+0xd0/0x1b0
>> [    1.503435] i2c_device_probe+0x174/0x240
>> [    1.509172] driver_probe_device+0x1fc/0x29c
>> [    1.515167] __driver_attach+0xa4/0xa8
>> [    1.520643] bus_for_each_dev+0x58/0x98
>> [    1.526204] driver_attach+0x20/0x28
>> [    1.531505] bus_add_driver+0x1c8/0x22c
>> [    1.537067] driver_register+0x68/0x108
>> [    1.542630] i2c_register_driver+0x38/0x7c
>> [    1.548457] bq27xxx_battery_i2c_driver_init+0x18/0x20
>> [    1.555321] do_one_initcall+0x38/0x12c
>> [    1.560886] kernel_init_freeable+0x148/0x1ec
>> [    1.566972] kernel_init+0x10/0xfc
>> [    1.572101] ret_from_fork+0x10/0x40
> 
> So I guess this should be backported to stable kernels.

That it would probably make sense.

> 
>> Signed-off-by: Rhyland Klein <rklein@nvidia.com>
>> ---
>>  drivers/power/power_supply_core.c | 9 +++++++--
>>  1 file changed, 7 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c
>> index 456987c88baa..070fec621d66 100644
>> --- a/drivers/power/power_supply_core.c
>> +++ b/drivers/power/power_supply_core.c
>> @@ -561,11 +561,16 @@ static int power_supply_read_temp(struct thermal_zone_device *tzd,
>>  {
>>  	struct power_supply *psy;
>>  	union power_supply_propval val;
>> -	int ret;
>> +	int ret = 0;
> 
> ret should probably be initialized to -EAGAIN to avoid val being
> interpreted without get_property() having been called at all?

Sure, that makes more sense than a 0 temp which could be misinterpreted.

> 
>>  	WARN_ON(tzd == NULL);
>> +
>>  	psy = tzd->devdata;
>> -	ret = psy->desc->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
>> +	WARN_ON(atomic_read(&psy->use_cnt) == 0);
> 
> I guess the warning can be dropped if error code is properly reported?

It looks like the thermal might not, but I will make a patch for that
too, since it is setup to handle it, but doesn't actually check it.

> 
>> +	if (atomic_read(&psy->use_cnt) > 0)
>> +		ret = psy->desc->get_property(psy, POWER_SUPPLY_PROP_TEMP,
>> +					      &val);
>>  
>>  	/* Convert tenths of degree Celsius to milli degree Celsius. */
>>  	if (!ret)
> 
> -- Sebastian
> 
-rhyland


-- 
nvpublic

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] power_supply: power_supply_read_temp only if use_cnt > 0
@ 2016-06-06 16:55 Rhyland Klein
  0 siblings, 0 replies; 4+ messages in thread
From: Rhyland Klein @ 2016-06-06 16:55 UTC (permalink / raw)
  To: Sebastian Reichel, Dmitry Eremin-Solenikov, David Woodhouse
  Cc: linux-pm, linux-kernel, Jon Hunter, Krzysztof Kozlowski,
	Rhyland Klein, stable

Add check to power_supply_read_temp() to only use the power_supply
get_property() callback if the use_cnt is > 0. The use_cnt will
be incremented at the end of __power_supply_register, so this will
block to case where get_property can be called before the supply
is fully registered. This fixes the issue show in the stack below:

[    1.452598] power_supply_read_temp+0x78/0x80
[    1.458680] thermal_zone_get_temp+0x5c/0x11c
[    1.464765] thermal_zone_device_update+0x34/0xb4
[    1.471195] thermal_zone_device_register+0x87c/0x8cc
[    1.477974] __power_supply_register+0x364/0x424
[    1.484317] power_supply_register_no_ws+0x10/0x18
[    1.490833] bq27xxx_battery_setup+0x10c/0x164
[    1.497003] bq27xxx_battery_i2c_probe+0xd0/0x1b0
[    1.503435] i2c_device_probe+0x174/0x240
[    1.509172] driver_probe_device+0x1fc/0x29c
[    1.515167] __driver_attach+0xa4/0xa8
[    1.520643] bus_for_each_dev+0x58/0x98
[    1.526204] driver_attach+0x20/0x28
[    1.531505] bus_add_driver+0x1c8/0x22c
[    1.537067] driver_register+0x68/0x108
[    1.542630] i2c_register_driver+0x38/0x7c
[    1.548457] bq27xxx_battery_i2c_driver_init+0x18/0x20
[    1.555321] do_one_initcall+0x38/0x12c
[    1.560886] kernel_init_freeable+0x148/0x1ec
[    1.566972] kernel_init+0x10/0xfc
[    1.572101] ret_from_fork+0x10/0x40

Cc: stable@vger.kernel.org
Signed-off-by: Rhyland Klein <rklein@nvidia.com>
---
v2:
 - Added cc stable
 - changed return to -EAGAIN in case of use_cnt < 1
 - Removed WARNING
 - return value check added in additional patch:
  "thermal: helpers: Check return value of get_temp"

 drivers/power/power_supply_core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c
index 456987c88baa..bbd80631f31e 100644
--- a/drivers/power/power_supply_core.c
+++ b/drivers/power/power_supply_core.c
@@ -561,11 +561,15 @@ static int power_supply_read_temp(struct thermal_zone_device *tzd,
 {
 	struct power_supply *psy;
 	union power_supply_propval val;
-	int ret;
+	int ret = -EAGAIN;
 
 	WARN_ON(tzd == NULL);
+
 	psy = tzd->devdata;
-	ret = psy->desc->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
+
+	if (atomic_read(&psy->use_cnt) > 0)
+		ret = psy->desc->get_property(psy, POWER_SUPPLY_PROP_TEMP,
+					      &val);
 
 	/* Convert tenths of degree Celsius to milli degree Celsius. */
 	if (!ret)
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-06-06 16:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-03 20:30 [PATCH] power_supply: power_supply_read_temp only if use_cnt > 0 Rhyland Klein
2016-06-04 13:30 ` Sebastian Reichel
2016-06-06 15:39   ` Rhyland Klein
2016-06-06 16:55 Rhyland Klein

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).