mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] usb: typec: tcpm: Cancel the timers before destroying the worker too
@ 2026-11-12  4:28 Arun Iyengar via B4 Relay
  2026-11-17 10:01 ` Heikki Krogerus
  0 siblings, 1 reply; 2+ messages in thread
From: Arun Iyengar via B4 Relay @ 2026-11-12  4:28 UTC (permalink / raw)
  To: Badhri Jagan Sridharan, Heikki Krogerus, Greg Kroah-Hartman,
	Guenter Roeck, Xu Yang
  Cc: linux-usb, linux-kernel, Arun Iyengar

From: Arun Iyengar <aiyengar1990@gmail.com>

tcpm_unregister_port() clears port->registered, destroys the kthread
worker and only then cancels the hrtimers. A timer handler running on
another CPU that has already seen port->registered set can still call
kthread_queue_work() once the worker is freed, or with port->wq
already NULL:

  CPU0                           CPU1
  tcpm_unregister_port()         state_machine_timer_handler()
                                   if (port->registered)
    port->registered = false;
    kthread_destroy_worker(port->wq);
    port->wq = NULL;
                                   kthread_queue_work(port->wq, ...);

The timers are cancelled after the worker is destroyed on purpose:
works that run while the worker is flushed can start a timer again
(see the commit in Fixes). Cancelling only before would bring that
back, so cancel them on both sides. hrtimer_cancel() waits for a
handler that is running, so after the first round no handler that
missed the cleared flag is left; the second round removes a timer that
a work started during the flush. tcpm_reset_port(), called later, only
cancels.

Fixes: ca4d8344a72b ("usb: typec: tcpm: fix tcpm unregister port but leave a pending timer")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-206898@kernel.org/
Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>
---
Sashiko found this while reviewing an unrelated tcpm patch of mine:
https://lore.kernel.org/all/sashiko-outbox-206898@kernel.org/
Its suggestion was to cancel the timers before destroying the worker
instead of after; that would undo the commit in Fixes, hence both.

Compile-tested only (arm64 W=1): the window is a few instructions in
the unbind path and I have no reproducer.

This patch was developed with Claude (Anthropic) as a coding assistant:
it wrote the change and the commit message, and I reviewed it.
---
 drivers/usb/typec/tcpm/tcpm.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index d721baef5210..c7e0e09b1cbe 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -8987,18 +8987,29 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
 }
 EXPORT_SYMBOL_GPL(tcpm_register_port);
 
+static void tcpm_cancel_timers(struct tcpm_port *port)
+{
+	hrtimer_cancel(&port->vdm_discovery_timer);
+	hrtimer_cancel(&port->enable_frs_timer);
+	hrtimer_cancel(&port->vdm_state_machine_timer);
+	hrtimer_cancel(&port->state_machine_timer);
+}
+
 void tcpm_unregister_port(struct tcpm_port *port)
 {
 	int i;
 
 	port->registered = false;
+	/*
+	 * A timer handler that saw port->registered still set may queue
+	 * work: wait for it before the worker goes away. Works that run
+	 * while the worker is flushed may start a timer again, so cancel
+	 * the timers once more afterwards.
+	 */
+	tcpm_cancel_timers(port);
 	kthread_destroy_worker(port->wq);
 	port->wq = NULL;
-
-	hrtimer_cancel(&port->vdm_discovery_timer);
-	hrtimer_cancel(&port->enable_frs_timer);
-	hrtimer_cancel(&port->vdm_state_machine_timer);
-	hrtimer_cancel(&port->state_machine_timer);
+	tcpm_cancel_timers(port);
 
 	power_supply_put_system_batteries(port->fixed_batt, port->fixed_batt_cnt);
 	tcpm_reset_port(port);

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261112-tcpm-unregister-timers-2fd159480999

Best regards,
--  
Arun Iyengar <aiyengar1990@gmail.com>



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

* Re: [PATCH] usb: typec: tcpm: Cancel the timers before destroying the worker too
  2026-11-12  4:28 [PATCH] usb: typec: tcpm: Cancel the timers before destroying the worker too Arun Iyengar via B4 Relay
@ 2026-11-17 10:01 ` Heikki Krogerus
  0 siblings, 0 replies; 2+ messages in thread
From: Heikki Krogerus @ 2026-11-17 10:01 UTC (permalink / raw)
  To: aiyengar1990
  Cc: Badhri Jagan Sridharan, Greg Kroah-Hartman, Guenter Roeck,
	Xu Yang, linux-usb, linux-kernel

On Thu, Nov 12, 2026 at 04:28:13AM +0000, Arun Iyengar via B4 Relay wrote:
> From: Arun Iyengar <aiyengar1990@gmail.com>
> 
> tcpm_unregister_port() clears port->registered, destroys the kthread
> worker and only then cancels the hrtimers. A timer handler running on
> another CPU that has already seen port->registered set can still call
> kthread_queue_work() once the worker is freed, or with port->wq
> already NULL:
> 
>   CPU0                           CPU1
>   tcpm_unregister_port()         state_machine_timer_handler()
>                                    if (port->registered)
>     port->registered = false;
>     kthread_destroy_worker(port->wq);
>     port->wq = NULL;
>                                    kthread_queue_work(port->wq, ...);
> 
> The timers are cancelled after the worker is destroyed on purpose:
> works that run while the worker is flushed can start a timer again
> (see the commit in Fixes). Cancelling only before would bring that
> back, so cancel them on both sides. hrtimer_cancel() waits for a
> handler that is running, so after the first round no handler that
> missed the cleared flag is left; the second round removes a timer that
> a work started during the flush. tcpm_reset_port(), called later, only
> cancels.
> 
> Fixes: ca4d8344a72b ("usb: typec: tcpm: fix tcpm unregister port but leave a pending timer")
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Closes: https://lore.kernel.org/all/sashiko-outbox-206898@kernel.org/
> Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>

Acked-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>

> ---
> Sashiko found this while reviewing an unrelated tcpm patch of mine:
> https://lore.kernel.org/all/sashiko-outbox-206898@kernel.org/
> Its suggestion was to cancel the timers before destroying the worker
> instead of after; that would undo the commit in Fixes, hence both.
> 
> Compile-tested only (arm64 W=1): the window is a few instructions in
> the unbind path and I have no reproducer.
> 
> This patch was developed with Claude (Anthropic) as a coding assistant:
> it wrote the change and the commit message, and I reviewed it.
> ---
>  drivers/usb/typec/tcpm/tcpm.c | 21 ++++++++++++++++-----
>  1 file changed, 16 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index d721baef5210..c7e0e09b1cbe 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
> @@ -8987,18 +8987,29 @@ struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
>  }
>  EXPORT_SYMBOL_GPL(tcpm_register_port);
>  
> +static void tcpm_cancel_timers(struct tcpm_port *port)
> +{
> +	hrtimer_cancel(&port->vdm_discovery_timer);
> +	hrtimer_cancel(&port->enable_frs_timer);
> +	hrtimer_cancel(&port->vdm_state_machine_timer);
> +	hrtimer_cancel(&port->state_machine_timer);
> +}
> +
>  void tcpm_unregister_port(struct tcpm_port *port)
>  {
>  	int i;
>  
>  	port->registered = false;
> +	/*
> +	 * A timer handler that saw port->registered still set may queue
> +	 * work: wait for it before the worker goes away. Works that run
> +	 * while the worker is flushed may start a timer again, so cancel
> +	 * the timers once more afterwards.
> +	 */
> +	tcpm_cancel_timers(port);
>  	kthread_destroy_worker(port->wq);
>  	port->wq = NULL;
> -
> -	hrtimer_cancel(&port->vdm_discovery_timer);
> -	hrtimer_cancel(&port->enable_frs_timer);
> -	hrtimer_cancel(&port->vdm_state_machine_timer);
> -	hrtimer_cancel(&port->state_machine_timer);
> +	tcpm_cancel_timers(port);
>  
>  	power_supply_put_system_batteries(port->fixed_batt, port->fixed_batt_cnt);
>  	tcpm_reset_port(port);
> 
> ---
> base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
> change-id: 20261112-tcpm-unregister-timers-2fd159480999
> 
> Best regards,
> --  
> Arun Iyengar <aiyengar1990@gmail.com>
> 

-- 
heikki

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

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

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-12  4:28 [PATCH] usb: typec: tcpm: Cancel the timers before destroying the worker too Arun Iyengar via B4 Relay
2026-11-17 10:01 ` Heikki Krogerus

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®