aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/acpi/battery.c
diff options
context:
space:
mode:
authorKrzysztof Kozlowski <k.kozlowski@samsung.com>2015-03-12 03:44:11 -0400
committerSebastian Reichel <sre@kernel.org>2015-03-13 18:15:51 -0400
commit297d716f6260cc9421d971b124ca196b957ee458 (patch)
tree32a666d3374d7f0653258c766252bd6a841f05ab /drivers/acpi/battery.c
parentb70229bca127283c3d30e5f471d30b1acccd7096 (diff)
power_supply: Change ownership from driver to core
Change the ownership of power_supply structure from each driver implementing the class to the power supply core. The patch changes power_supply_register() function thus all drivers implementing power supply class are adjusted. Each driver provides the implementation of power supply. However it should not be the owner of power supply class instance because it is exposed by core to other subsystems with power_supply_get_by_name(). These other subsystems have no knowledge when the driver will unregister the power supply. This leads to several issues when driver is unbound - mostly because user of power supply accesses freed memory. Instead let the core own the instance of struct 'power_supply'. Other users of this power supply will still access valid memory because it will be freed when device reference count reaches 0. Currently this means "it will leak" but power_supply_put() call in next patches will solve it. This solves invalid memory references in following race condition scenario: Thread 1: charger manager Thread 2: power supply driver, used by charger manager THREAD 1 (charger manager) THREAD 2 (power supply driver) ========================== ============================== psy = power_supply_get_by_name() Driver unbind, .remove power_supply_unregister() Device fully removed psy->get_property() The 'get_property' call is executed in invalid context because the driver was unbound and struct 'power_supply' memory was freed. This could be observed easily with charger manager driver (here compiled with max17040 fuel gauge): $ cat /sys/devices/virtual/power_supply/cm-battery/capacity & $ echo "1-0036" > /sys/bus/i2c/drivers/max17040/unbind [ 55.725123] Unable to handle kernel NULL pointer dereference at virtual address 00000000 [ 55.732584] pgd = d98d4000 [ 55.734060] [00000000] *pgd=5afa2831, *pte=00000000, *ppte=00000000 [ 55.740318] Internal error: Oops: 80000007 [#1] PREEMPT SMP ARM [ 55.746210] Modules linked in: [ 55.749259] CPU: 1 PID: 2936 Comm: cat Tainted: G W 3.19.0-rc1-next-20141226-00048-gf79f475f3c44-dirty #1496 [ 55.760190] Hardware name: SAMSUNG EXYNOS (Flattened Device Tree) [ 55.766270] task: d9b76f00 ti: daf54000 task.ti: daf54000 [ 55.771647] PC is at 0x0 [ 55.774182] LR is at charger_get_property+0x2f4/0x36c [ 55.779201] pc : [<00000000>] lr : [<c034b0b4>] psr: 60000013 [ 55.779201] sp : daf55e90 ip : 00000003 fp : 00000000 [ 55.790657] r10: 00000000 r9 : c06e2878 r8 : d9b26c68 [ 55.795865] r7 : dad81610 r6 : daec7410 r5 : daf55ebc r4 : 00000000 [ 55.802367] r3 : 00000000 r2 : daf55ebc r1 : 0000002a r0 : d9b26c68 [ 55.808879] Flags: nZCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment user [ 55.815994] Control: 10c5387d Table: 598d406a DAC: 00000015 [ 55.821723] Process cat (pid: 2936, stack limit = 0xdaf54210) [ 55.827451] Stack: (0xdaf55e90 to 0xdaf56000) [ 55.831795] 5e80: 60000013 c01459c4 0000002a c06f8ef8 [ 55.839956] 5ea0: db651000 c06f8ef8 daebac00 c04cb668 daebac08 c0346864 00000000 c01459c4 [ 55.848115] 5ec0: d99eaa80 c06f8ef8 00000fff 00001000 db651000 c027f25c c027f240 d99eaa80 [ 55.856274] 5ee0: d9a06c00 c0146218 daf55f18 00001000 d99eaa80 db4c18c0 00000001 00000001 [ 55.864468] 5f00: daf55f80 c0144c78 c0144c54 c0107f90 00015000 d99eaab0 00000000 00000000 [ 55.872603] 5f20: 000051c7 00000000 db4c18c0 c04a9370 00015000 00001000 daf55f80 00001000 [ 55.880763] 5f40: daf54000 00015000 00000000 c00e53dc db4c18c0 c00e548c 0000000d 00008124 [ 55.888937] 5f60: 00000001 00000000 00000000 db4c18c0 db4c18c0 00001000 00015000 c00e5550 [ 55.897099] 5f80: 00000000 00000000 00001000 00001000 00015000 00000003 00000003 c000f364 [ 55.905239] 5fa0: 00000000 c000f1a0 00001000 00015000 00000003 00015000 00001000 0001333c [ 55.913399] 5fc0: 00001000 00015000 00000003 00000003 00000002 00000000 00000000 00000000 [ 55.921560] 5fe0: 7fffe000 be999850 0000a225 b6f3c19c 60000010 00000003 00000000 00000000 [ 55.929744] [<c034b0b4>] (charger_get_property) from [<c0346864>] (power_supply_show_property+0x48/0x20c) [ 55.939286] [<c0346864>] (power_supply_show_property) from [<c027f25c>] (dev_attr_show+0x1c/0x48) [ 55.948130] [<c027f25c>] (dev_attr_show) from [<c0146218>] (sysfs_kf_seq_show+0x84/0x104) [ 55.956298] [<c0146218>] (sysfs_kf_seq_show) from [<c0144c78>] (kernfs_seq_show+0x24/0x28) [ 55.964536] [<c0144c78>] (kernfs_seq_show) from [<c0107f90>] (seq_read+0x1b0/0x484) [ 55.972172] [<c0107f90>] (seq_read) from [<c00e53dc>] (__vfs_read+0x18/0x4c) [ 55.979188] [<c00e53dc>] (__vfs_read) from [<c00e548c>] (vfs_read+0x7c/0x100) [ 55.986304] [<c00e548c>] (vfs_read) from [<c00e5550>] (SyS_read+0x40/0x8c) [ 55.993164] [<c00e5550>] (SyS_read) from [<c000f1a0>] (ret_fast_syscall+0x0/0x48) [ 56.000626] Code: bad PC value [ 56.011652] ---[ end trace 7b64343fbdae8ef1 ]--- Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com> Reviewed-by: Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com> [for the nvec part] Reviewed-by: Marc Dietrich <marvin24@gmx.de> [for compal-laptop.c] Acked-by: Darren Hart <dvhart@linux.intel.com> [for the mfd part] Acked-by: Lee Jones <lee.jones@linaro.org> [for the hid part] Acked-by: Jiri Kosina <jkosina@suse.cz> [for the acpi part] Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Signed-off-by: Sebastian Reichel <sre@kernel.org>
Diffstat (limited to 'drivers/acpi/battery.c')
-rw-r--r--drivers/acpi/battery.c55
1 files changed, 30 insertions, 25 deletions
diff --git a/drivers/acpi/battery.c b/drivers/acpi/battery.c
index fd8c06f492a1..fdc16ce9d272 100644
--- a/drivers/acpi/battery.c
+++ b/drivers/acpi/battery.c
@@ -117,7 +117,8 @@ enum {
117struct acpi_battery { 117struct acpi_battery {
118 struct mutex lock; 118 struct mutex lock;
119 struct mutex sysfs_lock; 119 struct mutex sysfs_lock;
120 struct power_supply bat; 120 struct power_supply *bat;
121 struct power_supply_desc bat_desc;
121 struct acpi_device *device; 122 struct acpi_device *device;
122 struct notifier_block pm_nb; 123 struct notifier_block pm_nb;
123 unsigned long update_time; 124 unsigned long update_time;
@@ -149,7 +150,7 @@ struct acpi_battery {
149 unsigned long flags; 150 unsigned long flags;
150}; 151};
151 152
152#define to_acpi_battery(x) container_of(x, struct acpi_battery, bat) 153#define to_acpi_battery(x) power_supply_get_drvdata(x)
153 154
154static inline int acpi_battery_present(struct acpi_battery *battery) 155static inline int acpi_battery_present(struct acpi_battery *battery)
155{ 156{
@@ -608,41 +609,45 @@ static struct device_attribute alarm_attr = {
608 609
609static int sysfs_add_battery(struct acpi_battery *battery) 610static int sysfs_add_battery(struct acpi_battery *battery)
610{ 611{
611 int result; 612 struct power_supply_config psy_cfg = { .drv_data = battery, };
612 613
613 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) { 614 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
614 battery->bat.properties = charge_battery_props; 615 battery->bat_desc.properties = charge_battery_props;
615 battery->bat.num_properties = 616 battery->bat_desc.num_properties =
616 ARRAY_SIZE(charge_battery_props); 617 ARRAY_SIZE(charge_battery_props);
617 } else { 618 } else {
618 battery->bat.properties = energy_battery_props; 619 battery->bat_desc.properties = energy_battery_props;
619 battery->bat.num_properties = 620 battery->bat_desc.num_properties =
620 ARRAY_SIZE(energy_battery_props); 621 ARRAY_SIZE(energy_battery_props);
621 } 622 }
622 623
623 battery->bat.name = acpi_device_bid(battery->device); 624 battery->bat_desc.name = acpi_device_bid(battery->device);
624 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY; 625 battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
625 battery->bat.get_property = acpi_battery_get_property; 626 battery->bat_desc.get_property = acpi_battery_get_property;
626 627
627 result = power_supply_register_no_ws(&battery->device->dev, 628 battery->bat = power_supply_register_no_ws(&battery->device->dev,
628 &battery->bat, NULL); 629 &battery->bat_desc, &psy_cfg);
629 630
630 if (result) 631 if (IS_ERR(battery->bat)) {
632 int result = PTR_ERR(battery->bat);
633
634 battery->bat = NULL;
631 return result; 635 return result;
632 return device_create_file(battery->bat.dev, &alarm_attr); 636 }
637 return device_create_file(&battery->bat->dev, &alarm_attr);
633} 638}
634 639
635static void sysfs_remove_battery(struct acpi_battery *battery) 640static void sysfs_remove_battery(struct acpi_battery *battery)
636{ 641{
637 mutex_lock(&battery->sysfs_lock); 642 mutex_lock(&battery->sysfs_lock);
638 if (!battery->bat.dev) { 643 if (!battery->bat) {
639 mutex_unlock(&battery->sysfs_lock); 644 mutex_unlock(&battery->sysfs_lock);
640 return; 645 return;
641 } 646 }
642 647
643 device_remove_file(battery->bat.dev, &alarm_attr); 648 device_remove_file(&battery->bat->dev, &alarm_attr);
644 power_supply_unregister(&battery->bat); 649 power_supply_unregister(battery->bat);
645 battery->bat.dev = NULL; 650 battery->bat = NULL;
646 mutex_unlock(&battery->sysfs_lock); 651 mutex_unlock(&battery->sysfs_lock);
647} 652}
648 653
@@ -739,7 +744,7 @@ static int acpi_battery_update(struct acpi_battery *battery, bool resume)
739 return result; 744 return result;
740 acpi_battery_init_alarm(battery); 745 acpi_battery_init_alarm(battery);
741 } 746 }
742 if (!battery->bat.dev) { 747 if (!battery->bat) {
743 result = sysfs_add_battery(battery); 748 result = sysfs_add_battery(battery);
744 if (result) 749 if (result)
745 return result; 750 return result;
@@ -765,7 +770,7 @@ static void acpi_battery_refresh(struct acpi_battery *battery)
765{ 770{
766 int power_unit; 771 int power_unit;
767 772
768 if (!battery->bat.dev) 773 if (!battery->bat)
769 return; 774 return;
770 775
771 power_unit = battery->power_unit; 776 power_unit = battery->power_unit;
@@ -1063,11 +1068,11 @@ static void acpi_battery_remove_fs(struct acpi_device *device)
1063static void acpi_battery_notify(struct acpi_device *device, u32 event) 1068static void acpi_battery_notify(struct acpi_device *device, u32 event)
1064{ 1069{
1065 struct acpi_battery *battery = acpi_driver_data(device); 1070 struct acpi_battery *battery = acpi_driver_data(device);
1066 struct device *old; 1071 struct power_supply *old;
1067 1072
1068 if (!battery) 1073 if (!battery)
1069 return; 1074 return;
1070 old = battery->bat.dev; 1075 old = battery->bat;
1071 /* 1076 /*
1072 * On Acer Aspire V5-573G notifications are sometimes triggered too 1077 * On Acer Aspire V5-573G notifications are sometimes triggered too
1073 * early. For example, when AC is unplugged and notification is 1078 * early. For example, when AC is unplugged and notification is
@@ -1084,8 +1089,8 @@ static void acpi_battery_notify(struct acpi_device *device, u32 event)
1084 acpi_battery_present(battery)); 1089 acpi_battery_present(battery));
1085 acpi_notifier_call_chain(device, event, acpi_battery_present(battery)); 1090 acpi_notifier_call_chain(device, event, acpi_battery_present(battery));
1086 /* acpi_battery_update could remove power_supply object */ 1091 /* acpi_battery_update could remove power_supply object */
1087 if (old && battery->bat.dev) 1092 if (old && battery->bat)
1088 power_supply_changed(&battery->bat); 1093 power_supply_changed(battery->bat);
1089} 1094}
1090 1095
1091static int battery_notify(struct notifier_block *nb, 1096static int battery_notify(struct notifier_block *nb,
@@ -1101,7 +1106,7 @@ static int battery_notify(struct notifier_block *nb,
1101 if (!acpi_battery_present(battery)) 1106 if (!acpi_battery_present(battery))
1102 return 0; 1107 return 0;
1103 1108
1104 if (!battery->bat.dev) { 1109 if (battery->bat) {
1105 result = acpi_battery_get_info(battery); 1110 result = acpi_battery_get_info(battery);
1106 if (result) 1111 if (result)
1107 return result; 1112 return result;