mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3] platform/x86: lenovo-yoga-tab2-pro-1380-fastcharger: cancel work on teardown
@ 2026-11-16 12:20 Hyunjung Ko
  2026-11-19 13:12 ` Rong Zhang
  0 siblings, 1 reply; 2+ messages in thread
From: Hyunjung Ko @ 2026-11-16 12:20 UTC (permalink / raw)
  To: Mark Pearson, Derek J. Clark, Rong Zhang, Hans de Goede,
	Ilpo Järvinen
  Cc: platform-driver-x86, linux-kernel, stable, Hyunjung Ko

fc->work is scheduled from the extcon notifier callback
yt2_1380_fc_extcon_evt() (and once from probe), but it is never cancelled.
The containing struct yt2_1380_fc and the extcon notifier are both
device-managed on the serdev device, and the serdev driver has no
->remove callback, so teardown happens entirely via devres unwinding
when yt2_1380_fc_pdev_remove() calls serdev_device_remove().

devm_extcon_register_notifier_all() unregistering on teardown only stops
future notifications; it does not drain a work item that the notifier has
already queued. A fast-charger extcon event that races device unbind can
therefore queue fc->work just before teardown; the work then runs after
devres has torn the device down. yt2_1380_fc_worker() dereferences not
only fc itself but also fc->pinctrl (pinctrl_select_state()) and the
serdev (serdev_device_write_buf()), so it must be drained before those
device-managed resources are released, not merely before fc is freed.

Initialize the work with devm_work_autocancel(), placed after the serdev
is opened and pinctrl is acquired but before
devm_extcon_register_notifier_all(). Because devres runs cleanup in
reverse order, teardown then unregisters the notifier first (so it can no
longer queue the work), next cancels the work while fc->pinctrl and the
serdev are still valid, and only then closes the serdev, releases pinctrl
and frees fc. This closes both the re-arm window and the use-after-free
of fc and of its sub-resources in the worker.

Found by static analysis / code review; compile-tested only, not
reproduced on hardware (requires the Lenovo Yoga Tablet 2 1380 charger).

Fixes: b2ed33e8d486 ("platform/x86: Add lenovo-yoga-tab2-pro-1380-fastcharger driver")
Cc: stable@vger.kernel.org
Signed-off-by: Hyunjung Ko <hj351016@gmail.com>
---
v3:
 - Use devm_work_autocancel() instead of a hand-rolled
   devm_add_action_or_reset(cancel_work_sync) (suggested by Ilpo Jarvinen).
   It is placed at the same point in probe (after devm_serdev_device_open()/
   devm_pinctrl_get(), before devm_extcon_register_notifier_all()) so the
   reverse-order devres teardown still drains the work after the notifier is
   unregistered but while fc->pinctrl and the serdev are still live. Moving
   INIT_WORK() down into devm_work_autocancel() is safe: fc->work is only
   scheduled from the extcon notifier and from the probe-tail schedule_work(),
   both after this point.
 - Add Cc: stable (this is a use-after-free fix).
v2:
 - Move the work-cancel registration from right after INIT_WORK to after
   devm_serdev_device_open()/devm_pinctrl_get() and before the extcon
   notifier, so devres (LIFO) drains the work while fc->pinctrl / serdev are
   still valid rather than after they are released.
 .../x86/lenovo/yoga-tab2-pro-1380-fastcharger.c    | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c b/drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c
index 8551ab4d2c7d..c2fe919a3ce9 100644
--- a/drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c
+++ b/drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c
@@ -8,6 +8,7 @@
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/delay.h>
+#include <linux/devm-helpers.h>
 #include <linux/err.h>
 #include <linux/errno.h>
 #include <linux/extcon.h>
@@ -160,7 +161,6 @@ static int yt2_1380_fc_serdev_probe(struct serdev_device *serdev)
 
 	fc->dev = dev;
 	fc->nb.notifier_call = yt2_1380_fc_extcon_evt;
-	INIT_WORK(&fc->work, yt2_1380_fc_worker);
 
 	/*
 	 * Do this first since it may return -EPROBE_DEFER.
@@ -209,6 +209,18 @@ static int yt2_1380_fc_serdev_probe(struct serdev_device *serdev)
 	serdev_device_set_baudrate(serdev, 600);
 	serdev_device_set_flow_control(serdev, false);
 
+	/*
+	 * Initialize fc->work with automatic cancellation on teardown.  This is
+	 * registered after the serdev is opened and pinctrl is acquired, but
+	 * before the extcon notifier: devres unwinds in reverse, so teardown
+	 * unregisters the notifier first (no new work can be queued), then
+	 * cancels the work while fc->pinctrl and the serdev are still valid, and
+	 * only then closes the serdev and releases pinctrl.
+	 */
+	ret = devm_work_autocancel(dev, &fc->work, yt2_1380_fc_worker);
+	if (ret)
+		return ret;
+
 	ret = devm_extcon_register_notifier_all(dev, fc->extcon, &fc->nb);
 	if (ret)
 		return dev_err_probe(dev, ret, "registering extcon notifier\n");
-- 
2.43.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v3] platform/x86: lenovo-yoga-tab2-pro-1380-fastcharger: cancel work on teardown
  2026-11-16 12:20 [PATCH v3] platform/x86: lenovo-yoga-tab2-pro-1380-fastcharger: cancel work on teardown Hyunjung Ko
@ 2026-11-19 13:12 ` Rong Zhang
  0 siblings, 0 replies; 2+ messages in thread
From: Rong Zhang @ 2026-11-19 13:12 UTC (permalink / raw)
  To: Hyunjung Ko, Mark Pearson, Derek J. Clark, Hans de Goede,
	Ilpo Järvinen
  Cc: platform-driver-x86, linux-kernel, stable

Hi Hyunjung,

On Mon, 2026-11-16 at 21:20 +0900, Hyunjung Ko wrote:
> fc->work is scheduled from the extcon notifier callback
> yt2_1380_fc_extcon_evt() (and once from probe), but it is never cancelled.
> The containing struct yt2_1380_fc and the extcon notifier are both
> device-managed on the serdev device, and the serdev driver has no
> ->remove callback, so teardown happens entirely via devres unwinding
> when yt2_1380_fc_pdev_remove() calls serdev_device_remove().
> 
> devm_extcon_register_notifier_all() unregistering on teardown only stops
> future notifications; it does not drain a work item that the notifier has
> already queued. A fast-charger extcon event that races device unbind can
> therefore queue fc->work just before teardown; the work then runs after
> devres has torn the device down. yt2_1380_fc_worker() dereferences not
> only fc itself but also fc->pinctrl (pinctrl_select_state()) and the
> serdev (serdev_device_write_buf()), so it must be drained before those
> device-managed resources are released, not merely before fc is freed.
> 
> Initialize the work with devm_work_autocancel(), placed after the serdev
> is opened and pinctrl is acquired but before
> devm_extcon_register_notifier_all(). Because devres runs cleanup in
> reverse order, teardown then unregisters the notifier first (so it can no
> longer queue the work), next cancels the work while fc->pinctrl and the
> serdev are still valid, and only then closes the serdev, releases pinctrl
> and frees fc. This closes both the re-arm window and the use-after-free
> of fc and of its sub-resources in the worker.
> 
> Found by static analysis / code review; compile-tested only, not
> reproduced on hardware (requires the Lenovo Yoga Tablet 2 1380 charger).
> 
> Fixes: b2ed33e8d486 ("platform/x86: Add lenovo-yoga-tab2-pro-1380-fastcharger driver")
> Cc: stable@vger.kernel.org
> Signed-off-by: Hyunjung Ko <hj351016@gmail.com>

Reviewed-by: Rong Zhang <i@rong.moe>

Thanks,
Rong

> ---
> v3:
>  - Use devm_work_autocancel() instead of a hand-rolled
>    devm_add_action_or_reset(cancel_work_sync) (suggested by Ilpo Jarvinen).
>    It is placed at the same point in probe (after devm_serdev_device_open()/
>    devm_pinctrl_get(), before devm_extcon_register_notifier_all()) so the
>    reverse-order devres teardown still drains the work after the notifier is
>    unregistered but while fc->pinctrl and the serdev are still live. Moving
>    INIT_WORK() down into devm_work_autocancel() is safe: fc->work is only
>    scheduled from the extcon notifier and from the probe-tail schedule_work(),
>    both after this point.
>  - Add Cc: stable (this is a use-after-free fix).
> v2:
>  - Move the work-cancel registration from right after INIT_WORK to after
>    devm_serdev_device_open()/devm_pinctrl_get() and before the extcon
>    notifier, so devres (LIFO) drains the work while fc->pinctrl / serdev are
>    still valid rather than after they are released.
>  .../x86/lenovo/yoga-tab2-pro-1380-fastcharger.c    | 14 +++++++++++++-
>  1 file changed, 13 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c b/drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c
> index 8551ab4d2c7d..c2fe919a3ce9 100644
> --- a/drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c
> +++ b/drivers/platform/x86/lenovo/yoga-tab2-pro-1380-fastcharger.c
> @@ -8,6 +8,7 @@
>  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
>  
>  #include <linux/delay.h>
> +#include <linux/devm-helpers.h>
>  #include <linux/err.h>
>  #include <linux/errno.h>
>  #include <linux/extcon.h>
> @@ -160,7 +161,6 @@ static int yt2_1380_fc_serdev_probe(struct serdev_device *serdev)
>  
>  	fc->dev = dev;
>  	fc->nb.notifier_call = yt2_1380_fc_extcon_evt;
> -	INIT_WORK(&fc->work, yt2_1380_fc_worker);
>  
>  	/*
>  	 * Do this first since it may return -EPROBE_DEFER.
> @@ -209,6 +209,18 @@ static int yt2_1380_fc_serdev_probe(struct serdev_device *serdev)
>  	serdev_device_set_baudrate(serdev, 600);
>  	serdev_device_set_flow_control(serdev, false);
>  
> +	/*
> +	 * Initialize fc->work with automatic cancellation on teardown.  This is
> +	 * registered after the serdev is opened and pinctrl is acquired, but
> +	 * before the extcon notifier: devres unwinds in reverse, so teardown
> +	 * unregisters the notifier first (no new work can be queued), then
> +	 * cancels the work while fc->pinctrl and the serdev are still valid, and
> +	 * only then closes the serdev and releases pinctrl.
> +	 */
> +	ret = devm_work_autocancel(dev, &fc->work, yt2_1380_fc_worker);
> +	if (ret)
> +		return ret;
> +
>  	ret = devm_extcon_register_notifier_all(dev, fc->extcon, &fc->nb);
>  	if (ret)
>  		return dev_err_probe(dev, ret, "registering extcon notifier\n");

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-19 13:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 12:20 [PATCH v3] platform/x86: lenovo-yoga-tab2-pro-1380-fastcharger: cancel work on teardown Hyunjung Ko
2026-11-19 13:12 ` Rong Zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®