aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/acpi/sbs.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/sbs.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/sbs.c')
-rw-r--r--drivers/acpi/sbs.c68
1 files changed, 41 insertions, 27 deletions
diff --git a/drivers/acpi/sbs.c b/drivers/acpi/sbs.c
index 2038ec1d021d..cd827625cf07 100644
--- a/drivers/acpi/sbs.c
+++ b/drivers/acpi/sbs.c
@@ -74,7 +74,8 @@ static const struct acpi_device_id sbs_device_ids[] = {
74MODULE_DEVICE_TABLE(acpi, sbs_device_ids); 74MODULE_DEVICE_TABLE(acpi, sbs_device_ids);
75 75
76struct acpi_battery { 76struct acpi_battery {
77 struct power_supply bat; 77 struct power_supply *bat;
78 struct power_supply_desc bat_desc;
78 struct acpi_sbs *sbs; 79 struct acpi_sbs *sbs;
79 unsigned long update_time; 80 unsigned long update_time;
80 char name[8]; 81 char name[8];
@@ -101,10 +102,10 @@ struct acpi_battery {
101 u8 have_sysfs_alarm:1; 102 u8 have_sysfs_alarm:1;
102}; 103};
103 104
104#define to_acpi_battery(x) container_of(x, struct acpi_battery, bat) 105#define to_acpi_battery(x) power_supply_get_drvdata(x)
105 106
106struct acpi_sbs { 107struct acpi_sbs {
107 struct power_supply charger; 108 struct power_supply *charger;
108 struct acpi_device *device; 109 struct acpi_device *device;
109 struct acpi_smb_hc *hc; 110 struct acpi_smb_hc *hc;
110 struct mutex lock; 111 struct mutex lock;
@@ -115,7 +116,7 @@ struct acpi_sbs {
115 u8 charger_exists:1; 116 u8 charger_exists:1;
116}; 117};
117 118
118#define to_acpi_sbs(x) container_of(x, struct acpi_sbs, charger) 119#define to_acpi_sbs(x) power_supply_get_drvdata(x)
119 120
120static int acpi_sbs_remove(struct acpi_device *device); 121static int acpi_sbs_remove(struct acpi_device *device);
121static int acpi_battery_get_state(struct acpi_battery *battery); 122static int acpi_battery_get_state(struct acpi_battery *battery);
@@ -303,6 +304,13 @@ static enum power_supply_property sbs_energy_battery_props[] = {
303 POWER_SUPPLY_PROP_MANUFACTURER, 304 POWER_SUPPLY_PROP_MANUFACTURER,
304}; 305};
305 306
307static const struct power_supply_desc acpi_sbs_charger_desc = {
308 .name = "sbs-charger",
309 .type = POWER_SUPPLY_TYPE_MAINS,
310 .properties = sbs_ac_props,
311 .num_properties = ARRAY_SIZE(sbs_ac_props),
312 .get_property = sbs_get_ac_property,
313};
306 314
307/* -------------------------------------------------------------------------- 315/* --------------------------------------------------------------------------
308 Smart Battery System Management 316 Smart Battery System Management
@@ -519,6 +527,7 @@ static int acpi_battery_read(struct acpi_battery *battery)
519static int acpi_battery_add(struct acpi_sbs *sbs, int id) 527static int acpi_battery_add(struct acpi_sbs *sbs, int id)
520{ 528{
521 struct acpi_battery *battery = &sbs->battery[id]; 529 struct acpi_battery *battery = &sbs->battery[id];
530 struct power_supply_config psy_cfg = { .drv_data = battery, };
522 int result; 531 int result;
523 532
524 battery->id = id; 533 battery->id = id;
@@ -528,23 +537,27 @@ static int acpi_battery_add(struct acpi_sbs *sbs, int id)
528 return result; 537 return result;
529 538
530 sprintf(battery->name, ACPI_BATTERY_DIR_NAME, id); 539 sprintf(battery->name, ACPI_BATTERY_DIR_NAME, id);
531 battery->bat.name = battery->name; 540 battery->bat_desc.name = battery->name;
532 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY; 541 battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
533 if (!acpi_battery_mode(battery)) { 542 if (!acpi_battery_mode(battery)) {
534 battery->bat.properties = sbs_charge_battery_props; 543 battery->bat_desc.properties = sbs_charge_battery_props;
535 battery->bat.num_properties = 544 battery->bat_desc.num_properties =
536 ARRAY_SIZE(sbs_charge_battery_props); 545 ARRAY_SIZE(sbs_charge_battery_props);
537 } else { 546 } else {
538 battery->bat.properties = sbs_energy_battery_props; 547 battery->bat_desc.properties = sbs_energy_battery_props;
539 battery->bat.num_properties = 548 battery->bat_desc.num_properties =
540 ARRAY_SIZE(sbs_energy_battery_props); 549 ARRAY_SIZE(sbs_energy_battery_props);
541 } 550 }
542 battery->bat.get_property = acpi_sbs_battery_get_property; 551 battery->bat_desc.get_property = acpi_sbs_battery_get_property;
543 result = power_supply_register(&sbs->device->dev, &battery->bat, NULL); 552 battery->bat = power_supply_register(&sbs->device->dev,
544 if (result) 553 &battery->bat_desc, &psy_cfg);
554 if (IS_ERR(battery->bat)) {
555 result = PTR_ERR(battery->bat);
556 battery->bat = NULL;
545 goto end; 557 goto end;
558 }
546 559
547 result = device_create_file(battery->bat.dev, &alarm_attr); 560 result = device_create_file(&battery->bat->dev, &alarm_attr);
548 if (result) 561 if (result)
549 goto end; 562 goto end;
550 battery->have_sysfs_alarm = 1; 563 battery->have_sysfs_alarm = 1;
@@ -559,28 +572,29 @@ static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
559{ 572{
560 struct acpi_battery *battery = &sbs->battery[id]; 573 struct acpi_battery *battery = &sbs->battery[id];
561 574
562 if (battery->bat.dev) { 575 if (battery->bat) {
563 if (battery->have_sysfs_alarm) 576 if (battery->have_sysfs_alarm)
564 device_remove_file(battery->bat.dev, &alarm_attr); 577 device_remove_file(&battery->bat->dev, &alarm_attr);
565 power_supply_unregister(&battery->bat); 578 power_supply_unregister(battery->bat);
566 } 579 }
567} 580}
568 581
569static int acpi_charger_add(struct acpi_sbs *sbs) 582static int acpi_charger_add(struct acpi_sbs *sbs)
570{ 583{
571 int result; 584 int result;
585 struct power_supply_config psy_cfg = { .drv_data = sbs, };
572 586
573 result = acpi_ac_get_present(sbs); 587 result = acpi_ac_get_present(sbs);
574 if (result) 588 if (result)
575 goto end; 589 goto end;
576 590
577 sbs->charger_exists = 1; 591 sbs->charger_exists = 1;
578 sbs->charger.name = "sbs-charger"; 592 sbs->charger = power_supply_register(&sbs->device->dev,
579 sbs->charger.type = POWER_SUPPLY_TYPE_MAINS; 593 &acpi_sbs_charger_desc, &psy_cfg);
580 sbs->charger.properties = sbs_ac_props; 594 if (IS_ERR(sbs->charger)) {
581 sbs->charger.num_properties = ARRAY_SIZE(sbs_ac_props); 595 result = PTR_ERR(sbs->charger);
582 sbs->charger.get_property = sbs_get_ac_property; 596 sbs->charger = NULL;
583 power_supply_register(&sbs->device->dev, &sbs->charger, NULL); 597 }
584 printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n", 598 printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n",
585 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), 599 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
586 ACPI_AC_DIR_NAME, sbs->charger_present ? "on-line" : "off-line"); 600 ACPI_AC_DIR_NAME, sbs->charger_present ? "on-line" : "off-line");
@@ -590,8 +604,8 @@ static int acpi_charger_add(struct acpi_sbs *sbs)
590 604
591static void acpi_charger_remove(struct acpi_sbs *sbs) 605static void acpi_charger_remove(struct acpi_sbs *sbs)
592{ 606{
593 if (sbs->charger.dev) 607 if (sbs->charger)
594 power_supply_unregister(&sbs->charger); 608 power_supply_unregister(sbs->charger);
595} 609}
596 610
597static void acpi_sbs_callback(void *context) 611static void acpi_sbs_callback(void *context)
@@ -605,7 +619,7 @@ static void acpi_sbs_callback(void *context)
605 if (sbs->charger_exists) { 619 if (sbs->charger_exists) {
606 acpi_ac_get_present(sbs); 620 acpi_ac_get_present(sbs);
607 if (sbs->charger_present != saved_charger_state) 621 if (sbs->charger_present != saved_charger_state)
608 kobject_uevent(&sbs->charger.dev->kobj, KOBJ_CHANGE); 622 kobject_uevent(&sbs->charger->dev.kobj, KOBJ_CHANGE);
609 } 623 }
610 624
611 if (sbs->manager_present) { 625 if (sbs->manager_present) {
@@ -617,7 +631,7 @@ static void acpi_sbs_callback(void *context)
617 acpi_battery_read(bat); 631 acpi_battery_read(bat);
618 if (saved_battery_state == bat->present) 632 if (saved_battery_state == bat->present)
619 continue; 633 continue;
620 kobject_uevent(&bat->bat.dev->kobj, KOBJ_CHANGE); 634 kobject_uevent(&bat->bat->dev.kobj, KOBJ_CHANGE);
621 } 635 }
622 } 636 }
623} 637}