Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

power_supply: power_supply_read_temp only if use_cnt > 0

Change power_supply_read_temp() to use power_supply_get_property()
so that it will check the use_cnt and ensure it 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

Also make the same change to ps_get_max_charge_cntl_limit() and
ps_get_cur_chrage_cntl_limit() to be safe. Lastly, change the return
value of power_supply_get_property() to -EAGAIN from -ENODEV if
use_cnt <= 0.

Fixes: 297d716f6260 ("power_supply: Change ownership from driver to core")
Cc: stable@vger.kernel.org
Signed-off-by: Rhyland Klein <rklein@nvidia.com>
Reviewed-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
Signed-off-by: Sebastian Reichel <sre@kernel.org>

authored by

Rhyland Klein and committed by
Sebastian Reichel
5bc28b93 1a695a90

+16 -11
+16 -11
drivers/power/power_supply_core.c
··· 565 565 566 566 WARN_ON(tzd == NULL); 567 567 psy = tzd->devdata; 568 - ret = psy->desc->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val); 568 + ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val); 569 + if (ret) 570 + return ret; 569 571 570 572 /* Convert tenths of degree Celsius to milli degree Celsius. */ 571 - if (!ret) 572 - *temp = val.intval * 100; 573 + *temp = val.intval * 100; 573 574 574 575 return ret; 575 576 } ··· 613 612 int ret; 614 613 615 614 psy = tcd->devdata; 616 - ret = psy->desc->get_property(psy, 617 - POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val); 618 - if (!ret) 619 - *state = val.intval; 615 + ret = power_supply_get_property(psy, 616 + POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val); 617 + if (ret) 618 + return ret; 619 + 620 + *state = val.intval; 620 621 621 622 return ret; 622 623 } ··· 631 628 int ret; 632 629 633 630 psy = tcd->devdata; 634 - ret = psy->desc->get_property(psy, 635 - POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val); 636 - if (!ret) 637 - *state = val.intval; 631 + ret = power_supply_get_property(psy, 632 + POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val); 633 + if (ret) 634 + return ret; 635 + 636 + *state = val.intval; 638 637 639 638 return ret; 640 639 }