]> git.itanic.dy.fi Git - linux-stable/commit
power: supply: bq27xxx: Fix bq27xxx_battery_update() race condition
authorHans de Goede <hdegoede@redhat.com>
Sat, 15 Apr 2023 18:23:32 +0000 (20:23 +0200)
committerSebastian Reichel <sebastian.reichel@collabora.com>
Mon, 8 May 2023 13:29:11 +0000 (15:29 +0200)
commit5c34c0aef185dcd10881847b9ebf20046aa77cb4
tree72160169304a620a964924a1eef63bdc275bcb15
parente4484643991e0f6b89060092563f0dbab9450cbb
power: supply: bq27xxx: Fix bq27xxx_battery_update() race condition

bq27xxx_battery_update() assumes / requires that it is only run once,
not multiple times at the same time. But there are 3 possible callers:

1. bq27xxx_battery_poll() delayed_work item handler
2. bq27xxx_battery_irq_handler_thread() I2C IRQ handler
3. bq27xxx_battery_setup()

And there is no protection against these racing with each other,
fix this race condition by making all callers take di->lock:

- Rename bq27xxx_battery_update() to bq27xxx_battery_update_unlocked()

- Add new bq27xxx_battery_update() which takes di->lock and then calls
  bq27xxx_battery_update_unlocked()

- Make stale cache check code in bq27xxx_battery_get_property(), which
  already takes di->lock directly to check the jiffies, call
  bq27xxx_battery_update_unlocked() instead of messing with
  the delayed_work item

- Make bq27xxx_battery_update_unlocked() mod the delayed-work item
  so that the next poll is delayed to poll_interval milliseconds after
  the last update independent of the source of the update

Fixes: 740b755a3b34 ("bq27x00: Poll battery state")
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
drivers/power/supply/bq27xxx_battery.c