From nobody Thu Jun 11 01:44:56 2026 Received: from sender4-op-o15.zoho.com (sender4-op-o15.zoho.com [136.143.188.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 11CA4382292; Wed, 10 Jun 2026 20:11:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.15 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781122278; cv=pass; b=GIZPePBCrwyielvY2CUhRDNgcVDlwT10x+9jAsT8Foco/WB7y9GbCeu4MFMu3Nc5G8pWF/oRwbTnGpqERWCkvgjNrD0j4Yk63raRHb6ShCFFHifGzfDHF0M/CeHJEq/Qy3fLCKG7Nd9i+N6cXQwpdEi8nZ99C7JSJcFPPivoZ2c= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781122278; c=relaxed/simple; bh=CnJRORpwe9bY2tAjSGoxGW1TZYkZBaH4MTTNeoFYnEo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=c5euY9wWAFAmQx5BrXF8dCUuEkvX9vM/WPDf5kKy3aoof5tL+ZDxNDXTp/5uIOwCtuudgHnTUs3Y7lqlNnMTBIX1vqSyEfRGloIR3U0d6evLrCHNZw4XT8peISDVKiCliyOG5/mGytM2KZlj+F+v5xwNA/uyK257EVOj5+NH0go= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b=Tu6ZiuYG; arc=pass smtp.client-ip=136.143.188.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b="Tu6ZiuYG" ARC-Seal: i=1; a=rsa-sha256; t=1781122261; cv=none; d=zohomail.com; s=zohoarc; b=lqLQqvALbomG1AhrJE2s8EtYHp+OlE7HKmCL7RSqxE5eg3nw20aaPkZCB42S24tZ3qNQ4YCiun4f/YPuls2sBd9X+WDQ2eUJocFntjKI7D+AYaqR8JcEOPglKyMfSha4MIUGmsKzzNmqnDVh3j14kRnksReCTHFiFXYkh1XEmZw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1781122261; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=4Z6qFnH6HqEphwj5C0HlaAZdh3GQRfD5xfYg2+bcN2k=; b=IfQmxYuc/SKYLUQu4Qy0XGWAKX4fIr8rDci3mz6BF/nEq8G15+ZF9GlYGv8kI46lwMuYjJ/FlkodCkSL/WOU9p9UBppGeJORoZVWT2Hr37iufqGNx6vPz9DJPUZlgglUiLqhi7lsGGYUbJJIM9+ePTXaFwfvGePr58sXV5L0dQs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1781122261; s=zmail2048; d=rong.moe; i=i@rong.moe; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=4Z6qFnH6HqEphwj5C0HlaAZdh3GQRfD5xfYg2+bcN2k=; b=Tu6ZiuYGTPmwYK024OqUMNiXkRXAFKQe5qV8Ahd0fy7PyDO8DTYkiJ4Udq+6oUob ymrLI1q7vs+eqPfqY3jK8gUNfooWnsGCjNIBglWHM2WAy2eU1QWWoyhUX10RHuGjgan P4ABHFJoPlpx7S7/sHKefdRbpRCB5vgh4A0hF0VJzhvNmEfIturel98uIb5dClE96sF tSny3toIrxGdQEXxCWqjo+OtUXkGqGr+/kF6ZJ4bRK6jh+KkXSnEyRdffwRQyQVp2E7 EmERo9G8wX3Gn+m2QU/bxqA8it7xy7YE0KY9OQYbPRZ98B3OkX3ufRhU2TYNxzELObV 5KZyH9OM8w== Received: by mx.zohomail.com with SMTPS id 17811222584778.060013424005888; Wed, 10 Jun 2026 13:10:58 -0700 (PDT) From: Rong Zhang Date: Thu, 11 Jun 2026 04:10:44 +0800 Subject: [PATCH v3 1/3] ACPI: battery: Merge consecutive battery notifications Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260611-b4-acpi-battery-notification-v3-1-f9390382c5a4@rong.moe> References: <20260611-b4-acpi-battery-notification-v3-0-f9390382c5a4@rong.moe> In-Reply-To: <20260611-b4-acpi-battery-notification-v3-0-f9390382c5a4@rong.moe> To: "Rafael J. Wysocki" , Len Brown Cc: "Rafael J. Wysocki" , =?utf-8?q?Jeffrey_W=C3=A4lti?= , Rick , Mark Pearson , linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, Rong Zhang , stable@vger.kernel.org X-Mailer: b4 0.16-dev-d5d98 X-ZohoMailClient: External It's a very common pattern to emit consecutive battery notifications, for example: Method (_Qxx, 0, NotSerialized) { Notify (BAT0, 0x80) // Status Change Notify (BAT0, 0x81) // Information Change } In this case, the current code path will update battery state twice within a short period, which is not optimal, as the same data are fetched twice. Moreover, both notifications are likely to call power_supply_changed(), causing power_supply_uevent() to read all battery properties in order to assemble uevents. Even worse, after the first uevent reaches userspace, some userspace processes start to read all battery properties in order to refresh their internal states, which competes with the second notification's handling and uevent assembling. This generates significant pressure on _STA, _BST and _BIX/_BIF methods. Not only that, power_supply_ext properties may also rely on some other ACPI methods, so both uevent assembling and userspace processes call them. It becomes a nightmare when all these methods share the same ACPI mutex protecting EC accesses and hence vulnerable to lock starvation. This is exactly the case of some Lenovo devices, where the mentioned EC query pattern eventually leads to a catastrophic situation that a bunch of ACPI methods (including but not limited to the mentioned ones) fail to acquire the same mutex due to timeout. These devices don't handle mutex acquisition failure gracefully and return garbage data, causing even more chaos. Improve battery notification handling by merging consecutive battery notifications within 10ms using a delayed work, so that they only refresh and/or update battery state once. ACPI netlink event and notifier call chain are still triggered multiple times in order not to break other components. Finally, call power_supply_changed() once and lead to a single uevent instead of a bunch, preventing userspace programs from causing too much pressure on power supply properties and underlying ACPI methods. Tested-by: Jeffrey W=C3=A4lti Cc: stable@vger.kernel.org Reported-by: Rick Closes: https://bugzilla.kernel.org/show_bug.cgi?id=3D221065 Signed-off-by: Rong Zhang --- Changes in v2: - Address Sashiko's concerns: - Return from acpi_battery_notification_worker() early when the fifo is empty - Use pr_err_ratelimited() for potential event storms - Add missing `\n' in a printk message - https://sashiko.dev/#/patchset/20260527-b4-acpi-battery-notification-v1= -0-2303bed8ec0b%40rong.moe - Minimalize the critical section of acpi_battery_notify() --- drivers/acpi/battery.c | 80 +++++++++++++++++++++++++++++++++++++++++++---= ---- 1 file changed, 70 insertions(+), 10 deletions(-) diff --git a/drivers/acpi/battery.c b/drivers/acpi/battery.c index b82dd67d98c9..5f476c074c68 100644 --- a/drivers/acpi/battery.c +++ b/drivers/acpi/battery.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -21,6 +22,7 @@ #include #include #include +#include =20 #include =20 @@ -43,6 +45,9 @@ =20 #define MAX_STRING_LENGTH 64 =20 +#define MAX_QUEUED_EVENTS 16 +#define NOTIF_MERGING_MS 10 + MODULE_AUTHOR("Paul Diefenbaugh"); MODULE_AUTHOR("Alexey Starikovskiy "); MODULE_DESCRIPTION("ACPI Battery Driver"); @@ -95,6 +100,8 @@ struct acpi_battery { struct power_supply_desc bat_desc; struct acpi_device *device; struct device *phys_dev; + struct kfifo acpi_notif_fifo; + struct delayed_work acpi_notif_dwork; struct notifier_block pm_nb; struct list_head list; unsigned long update_time; @@ -1059,14 +1066,24 @@ static void acpi_battery_refresh(struct acpi_batter= y *battery) } =20 /* Driver Interface */ -static void acpi_battery_notify(acpi_handle handle, u32 event, void *data) +static void acpi_battery_notification_worker(struct work_struct *work) { - struct acpi_battery *battery =3D data; + struct acpi_battery *battery =3D container_of(work, struct acpi_battery, + acpi_notif_dwork.work); struct acpi_device *device =3D battery->device; + u32 events[MAX_QUEUED_EVENTS]; struct power_supply *old; + unsigned int count, i; =20 guard(mutex)(&battery->update_lock); =20 + count =3D kfifo_out(&battery->acpi_notif_fifo, events, sizeof(events)); + count /=3D sizeof(events[0]); + if (!count) + return; + + pr_debug("merged %u battery notifications within %dms\n", count, NOTIF_ME= RGING_MS); + old =3D battery->bat; /* * On Acer Aspire V5-573G notifications are sometimes triggered too @@ -1076,19 +1093,50 @@ static void acpi_battery_notify(acpi_handle handle,= u32 event, void *data) */ if (battery_notification_delay_ms > 0) msleep(battery_notification_delay_ms); - if (event =3D=3D ACPI_BATTERY_NOTIFY_INFO) - acpi_battery_refresh(battery); + + for (i =3D 0; i < count; i++) { + if (events[i] =3D=3D ACPI_BATTERY_NOTIFY_INFO) { + acpi_battery_refresh(battery); + break; + } + } + acpi_battery_update(battery, false); - acpi_bus_generate_netlink_event(ACPI_BATTERY_CLASS, - dev_name(&device->dev), event, - acpi_battery_present(battery)); - acpi_notifier_call_chain(ACPI_BATTERY_CLASS, acpi_device_bid(device), - event, acpi_battery_present(battery)); + + for (i =3D 0; i < count; i++) { + acpi_bus_generate_netlink_event(ACPI_BATTERY_CLASS, + dev_name(&device->dev), events[i], + acpi_battery_present(battery)); + acpi_notifier_call_chain(ACPI_BATTERY_CLASS, acpi_device_bid(device), + events[i], acpi_battery_present(battery)); + } + /* acpi_battery_update could remove power_supply object */ if (old && battery->bat) power_supply_changed(battery->bat); } =20 +static void acpi_battery_notify(acpi_handle handle, u32 event, void *data) +{ + struct acpi_battery *battery =3D data; + bool queued =3D false; + + scoped_guard(mutex, &battery->update_lock) { + if (kfifo_avail(&battery->acpi_notif_fifo) >=3D sizeof(event)) { + kfifo_in(&battery->acpi_notif_fifo, &event, sizeof(event)); + queued =3D true; + } + } + + if (queued) { + schedule_delayed_work(&battery->acpi_notif_dwork, + msecs_to_jiffies(NOTIF_MERGING_MS)); + } else { + pr_err_ratelimited("too many battery notifications within %dms\n", + NOTIF_MERGING_MS); + } +} + static int battery_notify(struct notifier_block *nb, unsigned long mode, void *_unused) { @@ -1256,13 +1304,22 @@ static int acpi_battery_probe(struct platform_devic= e *pdev) =20 device_init_wakeup(&pdev->dev, true); =20 + result =3D kfifo_alloc(&battery->acpi_notif_fifo, + MAX_QUEUED_EVENTS * sizeof(u32), GFP_KERNEL); + if (result) + goto fail_pm; + + INIT_DELAYED_WORK(&battery->acpi_notif_dwork, acpi_battery_notification_w= orker); + result =3D acpi_dev_install_notify_handler(device, ACPI_ALL_NOTIFY, acpi_battery_notify, battery); if (result) - goto fail_pm; + goto fail_kfifo; =20 return 0; =20 +fail_kfifo: + kfifo_free(&battery->acpi_notif_fifo); fail_pm: device_init_wakeup(&pdev->dev, false); unregister_pm_notifier(&battery->pm_nb); @@ -1279,6 +1336,9 @@ static void acpi_battery_remove(struct platform_devic= e *pdev) acpi_dev_remove_notify_handler(battery->device, ACPI_ALL_NOTIFY, acpi_battery_notify); =20 + cancel_delayed_work_sync(&battery->acpi_notif_dwork); + kfifo_free(&battery->acpi_notif_fifo); + device_init_wakeup(&pdev->dev, false); unregister_pm_notifier(&battery->pm_nb); =20 --=20 2.53.0 From nobody Thu Jun 11 01:44:56 2026 Received: from sender4-op-o15.zoho.com (sender4-op-o15.zoho.com [136.143.188.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 63A78384CEA; Wed, 10 Jun 2026 20:11:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.15 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781122279; cv=pass; b=Ygrql4MoPkd2K5N/Fr/OTX8G9HLTfkWeznstLu0uTtxoPj/hJKUYiqitlBkeVpIvIUPqMdM9/tzIyZGBJd56GmFs8kGd1OVuPxxVlEjNaoiZIFSvVDHEnyuaw6ijMqxJ893yRHZlJcIHmQ+ppUVUlJbRJMRNUhCi6JaJgB2TNRA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781122279; c=relaxed/simple; bh=d/8aHu3sOJeRz6iy8fO/Ubrl6UiRR25wB9WPg43gakw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pNkhlBtF4BF3rS4U70Rsi42rk7+oev6paSX8muF6DI65O/mhHQilhZgY8vxl1ETv073Zvh84jjoYd6bcOrPejgN2HxuUgpQYl/eKkg7Z+lNRI8flPH/XKDiCBdM01VKjJzhLA7cvOB0/0iItaTevtsy2zNKLfNH9VYP1iFzOkM4= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b=i3T1KDU6; arc=pass smtp.client-ip=136.143.188.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b="i3T1KDU6" ARC-Seal: i=1; a=rsa-sha256; t=1781122263; cv=none; d=zohomail.com; s=zohoarc; b=jPolyxcizboKJoo+Z6IHDEHoYnA17uC/ILRfWDP+MN0iGd0TB0cYbYm7KMYitxj+ku+lMOO4noRextJtXrAa7NijiaAlO4GRsAdw/hnv6pFCR49DZ3AMpywkYNuNIvo7NNEdm59JV7R4b0EVOGdrlh6dEtgYWeYyshx0gBUT11Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1781122263; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=04pSwxHf+BNdMK+E7EF7iXIOB+o8V8MxaPFUMQsDMXU=; b=jipfKYhax/sHBkPsbSB6xtanZhAUj2CizPsp3Iup4DsR92JRFUKiNkj8q68zx+mKVdvVnf3CHjWrjqCkGfD2QF4kdEnoXrR3phS5H/5gZ0zc9QzwCctTavHt3xrTrCafHR4sSjjYAqTBkBNONe3+grlZIuGTE92ZaVXAcE3VMvc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1781122263; s=zmail2048; d=rong.moe; i=i@rong.moe; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=04pSwxHf+BNdMK+E7EF7iXIOB+o8V8MxaPFUMQsDMXU=; b=i3T1KDU6UwnUErqyWA4TmXV0CkuZV1KDtFh6fSzpFxqmdPpE+P+gd3p7zbo8OHAK qoWGqKAb1f+n5HHGHNSxQ+zpZd31qz7u0qGPRH+jkSB+rEDIUpWyhXKbbxBeEdexA4W JM7+MrT2cH/EkQvLDkf6e5a7HYcXhV91+F6MD9mMCcwZRVutSMxS3fRT3LWi8PC7vi5 Dj1vxcNKGWIfugnDXOPXWAh+PkXaBk2jzBCZkLRwvkbndQwiF5LtdwxmV49iXDivUM5 okXV//664Xew3DK+DA2IBoFX+8vCAcwf0EwDoOUvrEOeLBL9DzwahCT3zQ4XCD31zel aMFBJA5g/w== Received: by mx.zohomail.com with SMTPS id 1781122261034198.5783134596877; Wed, 10 Jun 2026 13:11:01 -0700 (PDT) From: Rong Zhang Date: Thu, 11 Jun 2026 04:10:45 +0800 Subject: [PATCH v3 2/3] ACPI: battery: Use kstrtoul() over sscanf("%lu\n") Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260611-b4-acpi-battery-notification-v3-2-f9390382c5a4@rong.moe> References: <20260611-b4-acpi-battery-notification-v3-0-f9390382c5a4@rong.moe> In-Reply-To: <20260611-b4-acpi-battery-notification-v3-0-f9390382c5a4@rong.moe> To: "Rafael J. Wysocki" , Len Brown Cc: "Rafael J. Wysocki" , =?utf-8?q?Jeffrey_W=C3=A4lti?= , Rick , Mark Pearson , linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, Rong Zhang X-Mailer: b4 0.16-dev-d5d98 X-ZohoMailClient: External It is more preferred to use kstrto*() to parse a single number. The function family properly returns an errno on error and is the correct mechanism to parse data from sysfs. The number base is set to 10 in order not to break the ABI. Signed-off-by: Rong Zhang Reported-by: Rick --- Changes in v3: - Address Sashiko's concerns on my last-minute changes: - Set the number base to 10 in order not to break the ABI - https://sashiko.dev/#/patchset/20260611-b4-acpi-battery-notification-v2= -0-4e8ed651a151%40rong.moe Changes in v2: - New patch in series - Since the series touches acpi_battery_alarm_store(), also convert the use of sscanf("%lu\n") into the more preferred kstrtoul() beforehand --- drivers/acpi/battery.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/acpi/battery.c b/drivers/acpi/battery.c index 5f476c074c68..68b7a48357b4 100644 --- a/drivers/acpi/battery.c +++ b/drivers/acpi/battery.c @@ -675,9 +675,13 @@ static ssize_t acpi_battery_alarm_store(struct device = *dev, { unsigned long x; struct acpi_battery *battery =3D to_acpi_battery(dev_get_drvdata(dev)); + int err; =20 - if (sscanf(buf, "%lu\n", &x) =3D=3D 1) - battery->alarm =3D x/1000; + err =3D kstrtoul(buf, 10, &x); + if (err) + return err; + + battery->alarm =3D x / 1000; if (acpi_battery_present(battery)) acpi_battery_set_alarm(battery); return count; --=20 2.53.0 From nobody Thu Jun 11 01:44:56 2026 Received: from sender4-op-o15.zoho.com (sender4-op-o15.zoho.com [136.143.188.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A0BFC233954; Wed, 10 Jun 2026 20:11:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.15 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781122284; cv=pass; b=KwNL6+ULugv1ClCeAUoh7fefwhXdVUJj6yITa16zQpCAG/PIz2bADJZcYKaV8EdwOd91Uf0HW2PI9tDRx7aI4RbNcVymhEkENtc0ehtPMnwabFtJaRBMT7UD2Iz961udH9oeD/3IZor3NUyozBiKpU6jqT34LnRWSqvzoCjFfcQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781122284; c=relaxed/simple; bh=a/d3+tO/eliJ/UIhQ/DCY/2aHVy8Gap5MuTvCLyVqpo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BT+CISOnmD1Rq4MrU3uh+E9lCPHVihf/QbUWuZWPZNQ1fO+8YASI2CFqJWf6rFx3d1demuNs1O3y33bZG1+AQBTSJCgFY+eclumIzE1mU3eBiIUrJb9cMuZlgnHkMnOQJNIUr7RRtOSQ+ys8HI08jVR2PuLiSHJb1CRw5dpLH2Q= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b=CyfEws4P; arc=pass smtp.client-ip=136.143.188.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=i@rong.moe header.b="CyfEws4P" ARC-Seal: i=1; a=rsa-sha256; t=1781122265; cv=none; d=zohomail.com; s=zohoarc; b=iE4i813eMEv1r3lmbi9sWT+Ot1xWM1WoBMLzRBRN0DWUyxnmo9cYE8a+6vU0PG1LxOxmbe8bZKFGUmLx5r8t6WhoTHgRbntQBqTHXrSNfCdERRZZif/2uBm9FsQHcHvQXV1LsIuCrYduu+qiU7gT4GpYPGFL0o0vdwlzoGzfHBQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1781122265; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=CjevIKorQrNKcm7Dhoc9R9oF3My0CTzg8vrXEBsFgxQ=; b=Ezj0UENqxGCPF+zb6TWSgImr9G8v7QnWFZiEPDc1r5t0DYJWyvvtvk6BJbews3J2RndUhdf3q4RyqvAWsQM21s6bcnZ2g56FWp1qNgvz1cN+uQqKdv1s5J5gB9jddr0qitdHKbA0eZR0C4Fak2QZ+/LUKfLpVpFDfvWDGnZD4es= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1781122265; s=zmail2048; d=rong.moe; i=i@rong.moe; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=CjevIKorQrNKcm7Dhoc9R9oF3My0CTzg8vrXEBsFgxQ=; b=CyfEws4Pclh7FU8G1Grxrk7SXHmmcmxRW0Ylgh1WGOT/Dsjq6y4U3W1N9+w8Ylzz HmcD1OCZ/d+QB/h1XjD25fVI1x2UdZdryOuDNNFImZi/dIlqc16oszz/hoTjgVgj03O 7v7mV92NdG/S4HK85TY11rlHGEa8FFKmZrsmj7a494WOP9pQVdOXWbO3M7kWLQVbWTi ISqP1bJMksnRBLwWbZ5UwNlnQcU+qB/yyx85ogU44m0Fuv2AUdOzDtTeSKxmu/aZd7U w8MUcqYWVhkkqQmSXBrg/cT49HiveHFamMroChZuq4dCOtxcOhP6Cz3qYI6KYkOYdBl 4owwDF3nyw== Received: by mx.zohomail.com with SMTPS id 1781122263769894.3529284931614; Wed, 10 Jun 2026 13:11:03 -0700 (PDT) From: Rong Zhang Date: Thu, 11 Jun 2026 04:10:46 +0800 Subject: [PATCH v3 3/3] ACPI: battery: Protect all properties with a separated mutex Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260611-b4-acpi-battery-notification-v3-3-f9390382c5a4@rong.moe> References: <20260611-b4-acpi-battery-notification-v3-0-f9390382c5a4@rong.moe> In-Reply-To: <20260611-b4-acpi-battery-notification-v3-0-f9390382c5a4@rong.moe> To: "Rafael J. Wysocki" , Len Brown Cc: "Rafael J. Wysocki" , =?utf-8?q?Jeffrey_W=C3=A4lti?= , Rick , Mark Pearson , linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, Rong Zhang X-Mailer: b4 0.16-dev-d5d98 X-ZohoMailClient: External The acpi_battery_get_property() callback calls acpi_battery_get_state() without any lock held, which could lead to race conditions, e.g., when multiple tasks read power supply properties simultaneously, or when other callbacks are called during its execution. Moreover, some devices' _BST method relies on a heavily shared ACPI mutex which protects EC accesses, so it cannot tolerate too much pressure or else other methods will time out. The lack of synchronization sometimes nullifies the cache mechanism of acpi_battery_get_state() when multiple processes read power supply properties simultaneously, which usually happens after a uevent. Normally, emitting a uevent implies that the cache must have been refreshed due to power_supply_uevent() reading all properties, so the mentioned processes should have seen cache hits. Unfortunately, these fragile devices' power_supply_ext properties are somehow slow to read after battery events, resulting in cache expiration before power_supply_uevent() finishes. Hence, once the uevent reaches userspace, the _BST method will be executed multiple times within a short period due to userspace processes reading all properties again. The coincidence causes lock starvation, resulting in a catastrophic situation that a lot of ACPI methods fail to acquire the shared ACPI mutex due to timeout and return garbage data thanks to the firmware's poorly designed error paths. The said "other synchronized methods" are protected by update_lock, leaving acpi_battery_get_property() to be the last desynchronized code path. Unfortunately, update_lock is not applicapable for acpi_battery_get_property(), as it protects too many fields, far more than necessary. What's worse, some code path could call or wait for acpi_battery_get_property() while an outer functions holding update_lock. Therefore, introduce a mutex to protect all accesses to battery properties, so that acpi_battery_get_property() can take the advantage of the mutex and synchronize itself. The helper function acpi_battery_handle_discharging() for quirky devices has to be inlined due to the change, as the mutex must be unlocked before calling the expensive power_supply_is_system_supplied() helper function. Reported-by: Rick Closes: https://bugzilla.kernel.org/show_bug.cgi?id=3D221065 Signed-off-by: Rong Zhang --- Changes in v3: - Address Sashiko's concerns on my last-minute changes: - Do not overwrite the initial value of `ret' in acpi_battery_get_property() - https://sashiko.dev/#/patchset/20260611-b4-acpi-battery-notification-v2= -0-4e8ed651a151%40rong.moe Changes in v2: - Address Sashiko's concerns: - Use a separated mutex to protect all properties instead of reusing update_lock - https://sashiko.dev/#/patchset/20260527-b4-acpi-battery-notification-v1= -0-2303bed8ec0b%40rong.moe - Dropped Tested-by due to massive rewrite --- drivers/acpi/battery.c | 147 +++++++++++++++++++++++++++++++++------------= ---- 1 file changed, 101 insertions(+), 46 deletions(-) diff --git a/drivers/acpi/battery.c b/drivers/acpi/battery.c index 68b7a48357b4..d8e37aec27d5 100644 --- a/drivers/acpi/battery.c +++ b/drivers/acpi/battery.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -104,6 +105,9 @@ struct acpi_battery { struct delayed_work acpi_notif_dwork; struct notifier_block pm_nb; struct list_head list; + unsigned long flags; + + struct mutex property_lock; /* Protects properties below. */ unsigned long update_time; int revision; int rate_now; @@ -130,7 +134,6 @@ struct acpi_battery { char oem_info[MAX_STRING_LENGTH]; int state; int power_unit; - unsigned long flags; }; =20 #define to_acpi_battery(x) power_supply_get_drvdata(x) @@ -187,20 +190,6 @@ static bool acpi_battery_is_degraded(struct acpi_batte= ry *battery) battery->full_charge_capacity < battery->design_capacity; } =20 -static int acpi_battery_handle_discharging(struct acpi_battery *battery) -{ - /* - * Some devices wrongly report discharging if the battery's charge level - * was above the device's start charging threshold atm the AC adapter - * was plugged in and the device thus did not start a new charge cycle. - */ - if ((battery_ac_is_broken || power_supply_is_system_supplied()) && - battery->rate_now =3D=3D 0) - return POWER_SUPPLY_STATUS_NOT_CHARGING; - - return POWER_SUPPLY_STATUS_DISCHARGING; -} - static int acpi_battery_get_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val) @@ -208,15 +197,41 @@ static int acpi_battery_get_property(struct power_sup= ply *psy, int full_capacity =3D ACPI_BATTERY_VALUE_UNKNOWN, ret =3D 0; struct acpi_battery *battery =3D to_acpi_battery(psy); =20 - if (acpi_battery_present(battery)) { - /* run battery update only if it is present */ - acpi_battery_get_state(battery); - } else if (psp !=3D POWER_SUPPLY_PROP_PRESENT) - return -ENODEV; + /* run battery update only if it is present */ + if (!acpi_battery_present(battery)) { + switch (psp) { + case POWER_SUPPLY_PROP_PRESENT: + val->intval =3D 0; + return 0; + default: + return -ENODEV; + } + } + + mutex_lock(&battery->property_lock); + + acpi_battery_get_state(battery); + switch (psp) { case POWER_SUPPLY_PROP_STATUS: + /* + * Some devices wrongly report discharging if the battery's charge level + * was above the device's start charging threshold atm the AC adapter + * was plugged in and the device thus did not start a new charge cycle. + */ if (battery->state & ACPI_BATTERY_STATE_DISCHARGING) - val->intval =3D acpi_battery_handle_discharging(battery); + if (battery->rate_now !=3D 0) { + val->intval =3D POWER_SUPPLY_STATUS_DISCHARGING; + } else if (battery_ac_is_broken) { + val->intval =3D POWER_SUPPLY_STATUS_NOT_CHARGING; + } else { + mutex_unlock(&battery->property_lock); + + val->intval =3D power_supply_is_system_supplied() + ? POWER_SUPPLY_STATUS_NOT_CHARGING + : POWER_SUPPLY_STATUS_DISCHARGING; + return 0; + } else if (battery->state & ACPI_BATTERY_STATE_CHARGING) /* Validate the status by checking the current. */ if (battery->rate_now !=3D ACPI_BATTERY_VALUE_UNKNOWN && @@ -318,6 +333,8 @@ static int acpi_battery_get_property(struct power_suppl= y *psy, default: ret =3D -EINVAL; } + + mutex_unlock(&battery->property_lock); return ret; } =20 @@ -540,6 +557,8 @@ static int acpi_battery_get_info(struct acpi_battery *b= attery) int use_bix; int result =3D -ENODEV; =20 + lockdep_assert_held(&battery->property_lock); + if (!acpi_battery_present(battery)) return 0; =20 @@ -579,6 +598,8 @@ static int acpi_battery_get_state(struct acpi_battery *= battery) acpi_status status =3D 0; struct acpi_buffer buffer =3D { ACPI_ALLOCATE_BUFFER, NULL }; =20 + lockdep_assert_held(&battery->property_lock); + if (!acpi_battery_present(battery)) return 0; =20 @@ -632,6 +653,8 @@ static int acpi_battery_set_alarm(struct acpi_battery *= battery) { acpi_status status =3D 0; =20 + lockdep_assert_held(&battery->property_lock); + if (!acpi_battery_present(battery) || !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags)) return -ENODEV; @@ -649,6 +672,8 @@ static int acpi_battery_set_alarm(struct acpi_battery *= battery) =20 static int acpi_battery_init_alarm(struct acpi_battery *battery) { + lockdep_assert_held(&battery->property_lock); + /* See if alarms are supported, and if so, set default */ if (!acpi_has_method(battery->device->handle, "_BTP")) { clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); @@ -666,6 +691,8 @@ static ssize_t acpi_battery_alarm_show(struct device *d= ev, { struct acpi_battery *battery =3D to_acpi_battery(dev_get_drvdata(dev)); =20 + guard(mutex)(&battery->property_lock); + return sysfs_emit(buf, "%d\n", battery->alarm * 1000); } =20 @@ -681,6 +708,8 @@ static ssize_t acpi_battery_alarm_store(struct device *= dev, if (err) return err; =20 + guard(mutex)(&battery->property_lock); + battery->alarm =3D x / 1000; if (acpi_battery_present(battery)) acpi_battery_set_alarm(battery); @@ -865,12 +894,17 @@ static int sysfs_add_battery(struct acpi_battery *bat= tery) .no_wakeup_source =3D true, }; bool full_cap_broken =3D false; + int power_unit; =20 - if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) && - !ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity)) - full_cap_broken =3D true; + scoped_guard(mutex, &battery->property_lock) { + power_unit =3D battery->power_unit; =20 - if (battery->power_unit =3D=3D ACPI_BATTERY_POWER_UNIT_MA) { + if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) && + !ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity)) + full_cap_broken =3D true; + } + + if (power_unit =3D=3D ACPI_BATTERY_POWER_UNIT_MA) { if (full_cap_broken) { battery->bat_desc.properties =3D charge_battery_full_cap_broken_props; @@ -924,6 +958,9 @@ static void sysfs_remove_battery(struct acpi_battery *b= attery) static void find_battery(const struct dmi_header *dm, void *private) { struct acpi_battery *battery =3D (struct acpi_battery *)private; + + lockdep_assert_held(&battery->property_lock); + /* Note: the hardcoded offsets below have been extracted from * the source code of dmidecode. */ @@ -955,6 +992,8 @@ static void find_battery(const struct dmi_header *dm, v= oid *private) */ static void acpi_battery_quirks(struct acpi_battery *battery) { + lockdep_assert_held(&battery->property_lock); + if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) return; =20 @@ -1007,30 +1046,38 @@ static void acpi_battery_quirks(struct acpi_battery= *battery) static int acpi_battery_update(struct acpi_battery *battery, bool resume) { int result =3D acpi_battery_get_status(battery); + bool wakeup; =20 if (result) return result; =20 if (!acpi_battery_present(battery)) { sysfs_remove_battery(battery); - battery->update_time =3D 0; + scoped_guard(mutex, &battery->property_lock) + battery->update_time =3D 0; return 0; } =20 if (resume) return 0; =20 - if (!battery->update_time) { - result =3D acpi_battery_get_info(battery); + scoped_guard(mutex, &battery->property_lock) { + if (!battery->update_time) { + result =3D acpi_battery_get_info(battery); + if (result) + return result; + acpi_battery_init_alarm(battery); + } + + result =3D acpi_battery_get_state(battery); if (result) return result; - acpi_battery_init_alarm(battery); - } + acpi_battery_quirks(battery); =20 - result =3D acpi_battery_get_state(battery); - if (result) - return result; - acpi_battery_quirks(battery); + wakeup =3D ((battery->state & ACPI_BATTERY_STATE_CRITICAL) || + (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) && + (battery->capacity_now <=3D battery->alarm))); + } =20 if (!battery->bat) { result =3D sysfs_add_battery(battery); @@ -1042,9 +1089,7 @@ static int acpi_battery_update(struct acpi_battery *b= attery, bool resume) * Wakeup the system if battery is critical low * or lower than the alarm level */ - if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) || - (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) && - (battery->capacity_now <=3D battery->alarm))) + if (wakeup) acpi_pm_wakeup_event(battery->phys_dev); =20 return result; @@ -1057,12 +1102,14 @@ static void acpi_battery_refresh(struct acpi_batter= y *battery) if (!battery->bat) return; =20 - power_unit =3D battery->power_unit; + scoped_guard(mutex, &battery->property_lock) { + power_unit =3D battery->power_unit; =20 - acpi_battery_get_info(battery); + acpi_battery_get_info(battery); =20 - if (power_unit =3D=3D battery->power_unit) - return; + if (power_unit =3D=3D battery->power_unit) + return; + } =20 /* The battery has changed its reporting units. */ sysfs_remove_battery(battery); @@ -1158,17 +1205,21 @@ static int battery_notify(struct notifier_block *nb, } else { int result; =20 - result =3D acpi_battery_get_info(battery); - if (result) - return result; + scoped_guard(mutex, &battery->property_lock) { + result =3D acpi_battery_get_info(battery); + if (result) + return result; + } =20 result =3D sysfs_add_battery(battery); if (result) return result; } =20 - acpi_battery_init_alarm(battery); - acpi_battery_get_state(battery); + scoped_guard(mutex, &battery->property_lock) { + acpi_battery_init_alarm(battery); + acpi_battery_get_state(battery); + } } =20 return 0; @@ -1291,6 +1342,10 @@ static int acpi_battery_probe(struct platform_device= *pdev) if (result) return result; =20 + result =3D devm_mutex_init(&pdev->dev, &battery->property_lock); + if (result) + return result; + if (acpi_has_method(battery->device->handle, "_BIX")) set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags); =20 --=20 2.53.0