Re: twl4030_charger: worker not cancelled on remove?

Sebastian Reichel <[email protected]>
Newsgroups org.kernel.vger.linux-pm,org.kernel.vger.linux-kernel
Message-ID <amP-R95JDCAznCza@venus>
Hello Maoyi,

On Fri, Jul 03, 2026 at 01:21:28AM +0800, Maoyi Xie wrote:
> twl4030_charger looks like it has the worker race on remove that bq25890
> fixed (commit 7e6fb67808ab). I would appreciate it if you could take a look.
> 
> The driver data is allocated with devm in probe:
> 
> 	bci = devm_kzalloc(&pdev->dev, sizeof(*bci), GFP_KERNEL);
> 
> It has two workers that both dereference bci:
> 
> 	INIT_WORK(&bci->work, twl4030_bci_usb_work);
> 	INIT_DELAYED_WORK(&bci->current_worker, twl4030_current_worker);
> 
> twl4030_bci_remove() disables charging and masks interrupts. It cancels
> neither worker. A worker still pending at remove runs after devm frees bci,
> so it touches freed memory.
> 
> bq25890 fixed the same shape by unregistering the USB notifier, then
> cancel_work_sync() on remove. twl4030 is different. Its notifier comes from
> devm_usb_get_phy_by_node(), so devm unregisters it after remove() returns.
> A plain cancel in remove() could then still race a reschedule. So a single
> cancel in remove() is not enough.
> 
> current_worker does not depend on the notifier. It reschedules itself, so it
> needs cancel_delayed_work_sync regardless.
> 
> Does this look like a real bug? If it does, I am happy to send a patch. I am
> not sure of the best shape here. One option is to move the phy off devm and
> mirror bq25890. Another is to coordinate the cancel differently. What would
> you prefer?
> 
> I do not have the hardware. The use-after-free is from a KASAN harness. It
> runs the worker after the bci free.

The proper fix is to use devm_delayed_work_autocancel() instead
of INIT_DELAYED_WORK() and devm_work_autocancel() instead of
INIT_WORK(), so that everything happens at the right time.

Greetings,

-- Sebastian
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEE72YNB0Y/i3JqeVQT2O7X88g7+poFAmpkAFAACgkQ2O7X88g7
+ppq5A/9GSYARwF28QWmYIfkKE88qjoT9Sap45Ib1I2HaCf8p37rElH4YEA+dQpt
R8uPSdhV4U7w+X5Mbv/CaRkULFixCyi63r1Y7pBnh4k9UWJ6P9xn7qQuUzYndYp+
bYCWGpbiUXfOwTlp6B2eoCXJz5IeckLJz9+DfNWzztuMrGt6A5wyXeq0gEJho7Hf
OGFyrSywheJxvJtI0Utdf4iBA3WfHqe9penA1cgNE7PTg3x78NA7T7xsKFYbdgEr
BgqwH3nXjr+hq7hCwmw7iWGcuZCEfNRsWC8F1Eyyz1GlCaj0dqyqvmGT+YWAiOJh
ir5oZDWSY7Tb6RYeH0Mu1DFC8ljI5EZKtJKzl4JGR0Zp82mehj1yD61AqWY+27Xh
g1wDSnlli68OJ9VHo0MRioppjj20L8+VpQtZGrlHHS4b1n99OoV3paBq9/lfME+C
0V4umvyY5lqzTj9RHT/346P9QTOLhtuHIZbXIrNCzpw+YnMklI8ITbThH4KLypsU
C5MwsvJygI0sJzTWxebKo5tK0hse+CBvP0UrjIuJ2DendXj9zccLBCwqiuvqDbvZ
VdZb6NZemtfbiUPPBHhu483bMXJSVIubCMfKaILpkg6LNcUqF/arwvufdW0uUxuB
HDQVbX88xn0UpaVHSsB7CUD6uD3pySJLD6YAGAKHklRK0p81jCg=
=V3cu
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.