From: Sebastian Reichel <sebastian.reichel@collabora.com>
To: Fan Wu <fanwu01@zju.edu.cn>
Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
myungjoo.ham@samsung.com, stable@vger.kernel.org
Subject: Re: [PATCH] power: supply: charger-manager: tear down sysfs and disable charging before freeing regulators
Date: Sun, 26 Jul 2026 22:07:37 +0200 [thread overview]
Message-ID: <amZoF1JtJKEKUE9p@venus> (raw)
In-Reply-To: <20260726051540.35579-1-fanwu01@zju.edu.cn>
[-- Attachment #1: Type: text/plain, Size: 3561 bytes --]
Hi,
On Sun, Jul 26, 2026 at 05:15:40AM +0000, Fan Wu wrote:
> charger_manager_remove() and the err_reg_extcon probe error path call
> regulator_put() before tearing down the power_supply sysfs entries
> (power_supply_unregister()). charger_manager_remove() also calls
> try_charger_enable(cm, false) after the regulator_put() loop. A concurrent
> write to a charger's externally_control sysfs attribute that lands between
> regulator_put() and power_supply_unregister() can run
> charger_externally_control_store() and call try_charger_enable(), which,
> when charging is enabled, dereferences the already-freed consumer handle
> via regulator_enable(), regulator_disable(), regulator_is_enabled() and
> regulator_force_disable(). When charging is enabled, try_charger_enable(cm,
> false) in .remove() also dereferences the freed handles directly. Both
> leave use-after-free windows.
>
> Move power_supply_unregister() ahead of the regulator_put() loop on both
> paths. It ends in device_unregister(), which removes the sysfs attribute
> groups and drains active kernfs operations, so on return no
> externally_control store can be running or start again; it does not touch
> the regulator consumers, and psy->desc references the devm-managed
> cm->charger_psy_desc, which outlives the call. Move try_charger_enable(cm,
> false) before the regulator_put() loop in .remove() as well, so the
> synchronous disable also runs on valid handles.
>
> This does not address the separate extcon-notifier path, which needs its
> own synchronization design.
>
> Found by an in-house static analysis tool.
>
> Fixes: 3950c7865cd7 ("charger-manager: Add support sysfs entry for charger")
> Cc: stable@vger.kernel.org
> Assisted-by: Codex:gpt-5.6
> Signed-off-by: Fan Wu <fanwu01@zju.edu.cn>
> ---
If the power-supply unregister needs to happen before the regulator
unregister, the regulator also needs to be registered before the
power-supply device to avoid early userspace access to the sysfs
files before the regulator is registered. I.e. this patch should
also move the charger_manager_register_extcon() call.
Greetings,
-- Sebastian
> drivers/power/supply/charger-manager.c | 10 +++++-----
> 1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/power/supply/charger-manager.c b/drivers/power/supply/charger-manager.c
> index c49e0e4d02f7..c1df512f32a8 100644
> --- a/drivers/power/supply/charger-manager.c
> +++ b/drivers/power/supply/charger-manager.c
> @@ -1622,11 +1622,11 @@ static int charger_manager_probe(struct platform_device *pdev)
> return 0;
>
> err_reg_extcon:
> + power_supply_unregister(cm->charger_psy);
> +
> for (i = 0; i < desc->num_charger_regulators; i++)
> regulator_put(desc->charger_regulators[i].consumer);
>
> - power_supply_unregister(cm->charger_psy);
> -
> return ret;
> }
>
> @@ -1644,12 +1644,12 @@ static void charger_manager_remove(struct platform_device *pdev)
> cancel_work_sync(&setup_polling);
> cancel_delayed_work_sync(&cm_monitor_work);
>
> - for (i = 0 ; i < desc->num_charger_regulators ; i++)
> - regulator_put(desc->charger_regulators[i].consumer);
> + try_charger_enable(cm, false);
>
> power_supply_unregister(cm->charger_psy);
>
> - try_charger_enable(cm, false);
> + for (i = 0 ; i < desc->num_charger_regulators ; i++)
> + regulator_put(desc->charger_regulators[i].consumer);
> }
>
> static const struct platform_device_id charger_manager_id[] = {
> --
> 2.34.1
>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
prev parent reply other threads:[~2026-07-26 20:07 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-26 5:15 [PATCH] power: supply: charger-manager: tear down sysfs and disable charging before freeing regulators Fan Wu
2026-07-26 20:07 ` Sebastian Reichel [this message]
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=amZoF1JtJKEKUE9p@venus \
--to=sebastian.reichel@collabora.com \
--cc=fanwu01@zju.edu.cn \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=myungjoo.ham@samsung.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.