public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	patches@lists.linux.dev, Hans de Goede <hdegoede@redhat.com>,
	Sebastian Reichel <sebastian.reichel@collabora.com>
Subject: [PATCH 5.15 44/69] power: supply: bq27xxx: Fix bq27xxx_battery_update() race condition
Date: Sun, 28 May 2023 20:12:04 +0100	[thread overview]
Message-ID: <20230528190830.018072142@linuxfoundation.org> (raw)
In-Reply-To: <20230528190828.358612414@linuxfoundation.org>

From: Hans de Goede <hdegoede@redhat.com>

commit 5c34c0aef185dcd10881847b9ebf20046aa77cb4 upstream.

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>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/power/supply/bq27xxx_battery.c |   21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

--- a/drivers/power/supply/bq27xxx_battery.c
+++ b/drivers/power/supply/bq27xxx_battery.c
@@ -1755,7 +1755,7 @@ static int bq27xxx_battery_read_health(s
 	return POWER_SUPPLY_HEALTH_GOOD;
 }
 
-void bq27xxx_battery_update(struct bq27xxx_device_info *di)
+static void bq27xxx_battery_update_unlocked(struct bq27xxx_device_info *di)
 {
 	struct bq27xxx_reg_cache cache = {0, };
 	bool has_ci_flag = di->opts & BQ27XXX_O_HAS_CI;
@@ -1806,6 +1806,16 @@ void bq27xxx_battery_update(struct bq27x
 		di->cache = cache;
 
 	di->last_update = jiffies;
+
+	if (poll_interval > 0)
+		mod_delayed_work(system_wq, &di->work, poll_interval * HZ);
+}
+
+void bq27xxx_battery_update(struct bq27xxx_device_info *di)
+{
+	mutex_lock(&di->lock);
+	bq27xxx_battery_update_unlocked(di);
+	mutex_unlock(&di->lock);
 }
 EXPORT_SYMBOL_GPL(bq27xxx_battery_update);
 
@@ -1816,9 +1826,6 @@ static void bq27xxx_battery_poll(struct
 				     work.work);
 
 	bq27xxx_battery_update(di);
-
-	if (poll_interval > 0)
-		schedule_delayed_work(&di->work, poll_interval * HZ);
 }
 
 static bool bq27xxx_battery_is_full(struct bq27xxx_device_info *di, int flags)
@@ -1991,10 +1998,8 @@ static int bq27xxx_battery_get_property(
 	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
 
 	mutex_lock(&di->lock);
-	if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
-		cancel_delayed_work_sync(&di->work);
-		bq27xxx_battery_poll(&di->work.work);
-	}
+	if (time_is_before_jiffies(di->last_update + 5 * HZ))
+		bq27xxx_battery_update_unlocked(di);
 	mutex_unlock(&di->lock);
 
 	if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)



  parent reply	other threads:[~2023-05-28 19:49 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-28 19:11 [PATCH 5.15 00/69] 5.15.114-rc1 review Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 01/69] usb: gadget: Properly configure the device for remote wakeup Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 02/69] usb: dwc3: fix gadget mode suspend interrupt handler issue Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 03/69] dt-bindings: ata: ahci-ceva: convert to yaml Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 04/69] dt-bindings: ata: ahci-ceva: Cover all 4 iommus entries Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 05/69] watchdog: sp5100_tco: Immediately trigger upon starting Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 06/69] ARM: dts: stm32: fix AV96 board SAI2 pin muxing on stm32mp15 Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 07/69] spi: fsl-spi: Re-organise transfer bits_per_word adaptation Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 08/69] spi: fsl-cpm: Use 16 bit mode for large transfers with even size Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 09/69] ocfs2: Switch to security_inode_init_security() Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 10/69] arm64: Also reset KASAN tag if page is not PG_mte_tagged Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 11/69] ALSA: hda/ca0132: add quirk for EVGA X299 DARK Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 12/69] ALSA: hda: Fix unhandled register update during auto-suspend period Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 13/69] ALSA: hda/realtek: Enable headset onLenovo M70/M90 Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 14/69] mmc: sdhci-esdhc-imx: make "no-mmc-hs400" works Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 15/69] ASoC: rt5682: Disable jack detection interrupt during suspend Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 16/69] net: cdc_ncm: Deal with too low values of dwNtbOutMaxSize Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 17/69] m68k: Move signal frame following exception on 68020/030 Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 18/69] parisc: Handle kgdb breakpoints only in kernel context Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 19/69] parisc: Allow to reboot machine after system halt Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 20/69] gpio: mockup: Fix mode of debugfs files Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 21/69] btrfs: use nofs when cleaning up aborted transactions Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 22/69] dt-binding: cdns,usb3: Fix cdns,on-chip-buff-size type Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 23/69] x86/mm: Avoid incomplete Global INVLPG flushes Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 24/69] selftests/memfd: Fix unknown type name build failure Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 25/69] parisc: Fix flush_dcache_page() for usage from irq context Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 26/69] perf/x86/uncore: Correct the number of CHAs on SPR Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 27/69] x86/topology: Fix erroneous smp_num_siblings on Intel Hybrid platforms Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 28/69] debugobjects: Dont wake up kswapd from fill_pool() Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 29/69] fbdev: udlfb: Fix endpoint check Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 30/69] net: fix stack overflow when LRO is disabled for virtual interfaces Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 31/69] udplite: Fix NULL pointer dereference in __sk_mem_raise_allocated() Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 32/69] USB: core: Add routines for endpoint checks in old drivers Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 33/69] USB: sisusbvga: Add endpoint checks Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 34/69] media: radio-shark: " Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 35/69] ASoC: lpass: Fix for KASAN use_after_free out of bounds Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 36/69] net: fix skb leak in __skb_tstamp_tx() Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 37/69] selftests: fib_tests: mute cleanup error message Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 38/69] octeontx2-pf: Fix TSOv6 offload Greg Kroah-Hartman
2023-05-28 19:11 ` [PATCH 5.15 39/69] bpf: Fix mask generation for 32-bit narrow loads of 64-bit fields Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 40/69] ipv6: Fix out-of-bounds access in ipv6_find_tlv() Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 41/69] cifs: mapchars mount option ignored Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 42/69] power: supply: leds: Fix blink to LED on transition Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 43/69] power: supply: mt6360: add a check of devm_work_autocancel in mt6360_charger_probe Greg Kroah-Hartman
2023-05-28 19:12 ` Greg Kroah-Hartman [this message]
2023-05-28 19:12 ` [PATCH 5.15 45/69] power: supply: bq27xxx: Fix I2C IRQ race on remove Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 46/69] power: supply: bq27xxx: Fix poll_interval handling and races " Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 47/69] power: supply: bq27xxx: Add cache parameter to bq27xxx_battery_current_and_status() Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 48/69] power: supply: sbs-charger: Fix INHIBITED bit for Status reg Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 49/69] firmware: arm_ffa: Check if ffa_driver remove is present before executing Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 50/69] firmware: arm_ffa: Fix FFA device names for logical partitions Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 51/69] fs: fix undefined behavior in bit shift for SB_NOUSER Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 52/69] regulator: pca9450: Fix BUCK2 enable_mask Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 53/69] coresight: Fix signedness bug in tmc_etr_buf_insert_barrier_packet() Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 54/69] xen/pvcalls-back: fix double frees with pvcalls_new_active_socket() Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 55/69] x86/show_trace_log_lvl: Ensure stack pointer is aligned, again Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 56/69] ASoC: Intel: Skylake: Fix declaration of enum skl_ch_cfg Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 57/69] sctp: fix an issue that plpmtu can never go to complete state Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 58/69] forcedeth: Fix an error handling path in nv_probe() Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 59/69] platform/mellanox: mlxbf-pmc: fix sscanf() error checking Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 60/69] net/mlx5e: do as little as possible in napi poll when budget is 0 Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 61/69] net/mlx5: DR, Fix crc32 calculation to work on big-endian (BE) CPUs Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 62/69] net/mlx5: DR, Check force-loopback RC QP capability independently from RoCE Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 63/69] net/mlx5: Fix error message when failing to allocate device memory Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 64/69] net/mlx5: Devcom, fix error flow in mlx5_devcom_register_device Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 65/69] arm64: dts: imx8mn-var-som: fix PHY detection bug by adding deassert delay Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 66/69] firmware: arm_ffa: Set reserved/MBZ fields to zero in the memory descriptors Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 67/69] regulator: mt6359: add read check for PMIC MT6359 Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 68/69] 3c589_cs: Fix an error handling path in tc589_probe() Greg Kroah-Hartman
2023-05-28 19:12 ` [PATCH 5.15 69/69] net: phy: mscc: add VSC8502 to MODULE_DEVICE_TABLE Greg Kroah-Hartman
2023-05-29  5:03 ` [PATCH 5.15 00/69] 5.15.114-rc1 review Bagas Sanjaya
2023-05-29 15:52 ` Naresh Kamboju
2023-05-29 16:06 ` Guenter Roeck
2023-05-30  6:49 ` Ron Economos
2023-05-30 11:56 ` Chris Paterson
2023-05-30 17:00 ` Allen Pais
2023-05-30 18:52 ` Florian Fainelli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230528190830.018072142@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=hdegoede@redhat.com \
    --cc=patches@lists.linux.dev \
    --cc=sebastian.reichel@collabora.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox