From: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Danilo Krummrich <dakr@kernel.org>,
Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>, Tejun Heo <tj@kernel.org>,
Lai Jiangshan <jiangshanlai@gmail.com>,
Tobias Schrammm <t.schramm@manjaro.org>,
Sebastian Reichel <sre@kernel.org>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Dan Carpenter <dan.carpenter@linaro.org>,
Krzysztof Kozlowski <krzk@kernel.org>, Lee Jones <lee@kernel.org>,
Dzmitry Sankouski <dsankouski@gmail.com>,
Matthias Brugger <matthias.bgg@gmail.com>,
AngeloGioacchino Del Regno
<angelogioacchino.delregno@collabora.com>,
Benson Leung <bleung@chromium.org>,
Tzung-Bi Shih <tzungbi@kernel.org>
Cc: Matti Vaittinen <mazziesaccount@gmail.com>,
driver-core@lists.linux.dev, linux-doc@vger.kernel.org,
linux-kernel@vger.kernel.org,
Sebastian Reichel <sebastian.reichel@collabora.com>,
linux-pm@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-mediatek@lists.infradead.org,
chrome-platform@lists.linux.dev,
Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
Subject: [PATCH v2 04/10] power: supply: max77705: Free allocated workqueue and fix removal order
Date: Thu, 05 Mar 2026 22:45:43 +0100 [thread overview]
Message-ID: <20260305-workqueue-devm-v2-4-66a38741c652@oss.qualcomm.com> (raw)
In-Reply-To: <20260305-workqueue-devm-v2-0-66a38741c652@oss.qualcomm.com>
Use devm interface for allocating workqueue to fix two bugs at the same
time:
1. Driver leaks the memory on remove(), because the workqueue is not
destroyed.
2. Driver allocates workqueue and then registers interrupt handlers
with devm interface. This means that probe error paths will not use a
reversed order, but first destroy the workqueue and then, via devm
release handlers, free the interrupt.
The interrupt handler schedules work on this exact workqueue, thus if
interrupt is hit in this short time window - after destroying
workqueue, but before devm() frees the interrupt - the schedulled
work will lead to use of freed memory.
Change is not equivalent in the workqueue itself: use non-legacy API
which does not set (__WQ_LEGACY | WQ_MEM_RECLAIM). The workqueue is
used to update power supply (power_supply_changed()) status, thus there
is no point to run it for memory reclaim. Note that dev_name() is not
directly used in second argument to prevent possible unlikely parsing
any "%" character in device name as format.
Fixes: 11741b8e382d ("power: supply: max77705: Fix workqueue error handling in probe")
Fixes: a6a494c8e3ce ("power: supply: max77705: Add charger driver for Maxim 77705")
Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
---
Changes in v2:
1. Use devm_alloc_ordered_workqueue(), mention this in commit msg
---
drivers/power/supply/max77705_charger.c | 28 +++++++++-------------------
1 file changed, 9 insertions(+), 19 deletions(-)
diff --git a/drivers/power/supply/max77705_charger.c b/drivers/power/supply/max77705_charger.c
index 0dfe4ab10919..63b0b4f0cd21 100644
--- a/drivers/power/supply/max77705_charger.c
+++ b/drivers/power/supply/max77705_charger.c
@@ -646,47 +646,37 @@ static int max77705_charger_probe(struct i2c_client *i2c)
if (ret)
return dev_err_probe(dev, ret, "failed to add irq chip\n");
- chg->wqueue = create_singlethread_workqueue(dev_name(dev));
+ chg->wqueue = devm_alloc_ordered_workqueue(dev, "%s", 0, dev_name(dev));
if (!chg->wqueue)
return -ENOMEM;
ret = devm_work_autocancel(dev, &chg->chgin_work, max77705_chgin_isr_work);
- if (ret) {
- dev_err_probe(dev, ret, "failed to initialize interrupt work\n");
- goto destroy_wq;
- }
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to initialize interrupt work\n");
ret = max77705_charger_initialize(chg);
- if (ret) {
- dev_err_probe(dev, ret, "failed to initialize charger IC\n");
- goto destroy_wq;
- }
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to initialize charger IC\n");
ret = devm_request_threaded_irq(dev, regmap_irq_get_virq(irq_data, MAX77705_CHGIN_I),
NULL, max77705_chgin_irq,
IRQF_TRIGGER_NONE,
"chgin-irq", chg);
if (ret)
- goto destroy_wq;
+ return ret;
ret = devm_request_threaded_irq(dev, regmap_irq_get_virq(irq_data, MAX77705_AICL_I),
NULL, max77705_aicl_irq,
IRQF_TRIGGER_NONE,
"aicl-irq", chg);
if (ret)
- goto destroy_wq;
+ return ret;
ret = max77705_charger_enable(chg);
- if (ret) {
- dev_err_probe(dev, ret, "failed to enable charge\n");
- goto destroy_wq;
- }
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to enable charge\n");
return devm_add_action_or_reset(dev, max77705_charger_disable, chg);
-
-destroy_wq:
- destroy_workqueue(chg->wqueue);
- return ret;
}
static const struct of_device_id max77705_charger_of_match[] = {
--
2.51.0
next prev parent reply other threads:[~2026-03-05 21:46 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-05 21:45 [PATCH v2 00/10] workqueue / drivers: Add device-managed allocate workqueue Krzysztof Kozlowski
2026-03-05 21:45 ` [PATCH v2 01/10] workqueue: devres: " Krzysztof Kozlowski
2026-03-06 4:08 ` Tejun Heo
2026-03-10 9:59 ` Krzysztof Kozlowski
2026-03-10 17:05 ` Tejun Heo
2026-03-05 21:45 ` [PATCH v2 02/10] power: supply: cw2015: Free allocated workqueue Krzysztof Kozlowski
2026-03-05 21:45 ` [PATCH v2 03/10] power: supply: max77705: Drop duplicated IRQ error message Krzysztof Kozlowski
2026-03-05 21:45 ` Krzysztof Kozlowski [this message]
2026-03-05 21:45 ` [PATCH v2 05/10] power: supply: mt6370: Simplify with devm_alloc_ordered_workqueue() Krzysztof Kozlowski
2026-03-05 21:45 ` [PATCH v2 06/10] power: supply: ipaq_micro: Simplify with devm Krzysztof Kozlowski
2026-03-05 21:45 ` [PATCH v2 07/10] mfd: ezx-pcap: Drop memory allocation error message Krzysztof Kozlowski
2026-03-05 21:45 ` [PATCH v2 08/10] mfd: ezx-pcap: Return directly instead of empty gotos Krzysztof Kozlowski
2026-03-05 21:45 ` [PATCH v2 09/10] mfd: ezx-pcap: Avoid rescheduling after destroying workqueue Krzysztof Kozlowski
2026-03-05 21:45 ` [PATCH v2 10/10] platform/chrome: cros_usbpd_logger: Simplify with devm Krzysztof Kozlowski
2026-03-10 3:26 ` Tzung-Bi Shih
2026-03-06 14:28 ` [PATCH v2 00/10] workqueue / drivers: Add device-managed allocate workqueue Andy Shevchenko
2026-03-11 7:10 ` (subset) " Sebastian Reichel
2026-03-19 16:21 ` Lee Jones
2026-03-20 3:06 ` Tzung-Bi Shih
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=20260305-workqueue-devm-v2-4-66a38741c652@oss.qualcomm.com \
--to=krzysztof.kozlowski@oss.qualcomm.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=angelogioacchino.delregno@collabora.com \
--cc=bleung@chromium.org \
--cc=chrome-platform@lists.linux.dev \
--cc=corbet@lwn.net \
--cc=dakr@kernel.org \
--cc=dan.carpenter@linaro.org \
--cc=driver-core@lists.linux.dev \
--cc=dsankouski@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=jiangshanlai@gmail.com \
--cc=krzk@kernel.org \
--cc=lee@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mediatek@lists.infradead.org \
--cc=linux-pm@vger.kernel.org \
--cc=matthias.bgg@gmail.com \
--cc=mazziesaccount@gmail.com \
--cc=rafael@kernel.org \
--cc=sebastian.reichel@collabora.com \
--cc=skhan@linuxfoundation.org \
--cc=sre@kernel.org \
--cc=t.schramm@manjaro.org \
--cc=tj@kernel.org \
--cc=tzungbi@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