Skip to contentSkip to conversation

usb: typec: tcpm: fix tcpm unregister port but leave a pending timer

PATCH Superseded Xu Yang 3 participants 5 messages Nov 18, 2021 – Nov 19, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Xu Yang

author
#0 lore raw

PATCH 1/1 usb: typec: tcpm: fix tcpm unregister port but leave a pending timer

In current design, when the tcpm port is unregisterd, the kthread_worker will be destroyed in the last step. Inside the kthread_destroy_worker(), the worker will flush all the works and wait for them to end. However, if one of the works calls hrtimer_start(), this hrtimer will be pending for timeout even through tcpm port is removed. Once the hrtimer timeout, many strange kernel dumps will appear.

Thus, we can first complete kthread_destroy_worker(), then cancel all the hrtimers. This will guarantee that no hrtimer is pending at the end.

Fixes 3ed8e1c2ac99 ("usb: typec: tcpm: Migrate workqueue to RT priority for processing events")
---
 drivers/usb/typec/tcpm/tcpm.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/usb/typec/tcpm/tcpm.c +11 -5
index 7f2f3ff1b391..91136d71640f 100644
@@ -6295,7 +6295,8 @@ static enum hrtimer_restart state_machine_timer_handler(struct hrtimer *timer)
 {
 	struct tcpm_port *port = container_of(timer, struct tcpm_port, state_machine_timer);
 
-	kthread_queue_work(port->wq, &port->state_machine);
+	if (port->wq)
+		kthread_queue_work(port->wq, &port->state_machine);
 	return HRTIMER_NORESTART;
 }
 
@@ -6303,7 +6304,8 @@ static enum hrtimer_restart vdm_state_machine_timer_handler(struct hrtimer *time
 {
 	struct tcpm_port *port = container_of(timer, struct tcpm_port, vdm_state_machine_timer);
 
-	kthread_queue_work(port->wq, &port->vdm_state_machine);
+	if (port->wq)
+		kthread_queue_work(port->wq, &port->vdm_state_machine);
 	return HRTIMER_NORESTART;
 }
 
@@ -6311,7 +6313,8 @@ static enum hrtimer_restart enable_frs_timer_handler(struct hrtimer *timer)
 {
 	struct tcpm_port *port = container_of(timer, struct tcpm_port, enable_frs_timer);
 
-	kthread_queue_work(port->wq, &port->enable_frs);
+	if (port->wq)
+		kthread_queue_work(port->wq, &port->enable_frs);
 	return HRTIMER_NORESTART;
 }
 
@@ -6319,7 +6322,8 @@ static enum hrtimer_restart send_discover_timer_handler(struct hrtimer *timer)
 {
 	struct tcpm_port *port = container_of(timer, struct tcpm_port, send_discover_timer);
 
-	kthread_queue_work(port->wq, &port->send_discover_work);
+	if (port->wq)
+		kthread_queue_work(port->wq, &port->send_discover_work);
 	return HRTIMER_NORESTART;
 }
 
@@ -6428,6 +6432,9 @@ void tcpm_unregister_port(struct tcpm_port *port)
 {
 	int i;
 
+	kthread_destroy_worker(port->wq);
+	port->wq = NULL;
+
 	hrtimer_cancel(&port->send_discover_timer);
 	hrtimer_cancel(&port->enable_frs_timer);
 	hrtimer_cancel(&port->vdm_state_machine_timer);
@@ -6439,7 +6446,6 @@ void tcpm_unregister_port(struct tcpm_port *port)
 	typec_unregister_port(port->typec_port);
 	usb_role_switch_put(port->role_sw);
 	tcpm_debugfs_exit(port);
-	kthread_destroy_worker(port->wq);
 }
 EXPORT_SYMBOL_GPL(tcpm_unregister_port);
 
Signature · 1 line
2.25.1
maintainer
#1 lore raw

Hi,

Review void tcpm_unregister_port(struct tcpm_port *port) · line 6433

Quoted 3 lines · Xu Yang
(quoted diff) +0 -0
@@ -6428,6 +6432,9 @@ void tcpm_unregister_port(struct tcpm_port *port)
 {
 	int i;

You need to take the port lock here, no?

        mutex_lock(&port->lock);

drivers/usb/typec/tcpm/tcpm.c void tcpm_unregister_port(struct tcpm_port *port) · line 6436

Quoted 2 lines
drivers/usb/typec/tcpm/tcpm.c +2 -0
+	kthread_destroy_worker(port->wq);
+	port->wq = NULL;
        mutex_unlock(&port->lock);

drivers/usb/typec/tcpm/tcpm.c void tcpm_unregister_port(struct tcpm_port *port) · line 6450

Quoted 10 lines
drivers/usb/typec/tcpm/tcpm.c +0 -1
 	hrtimer_cancel(&port->send_discover_timer);
 	hrtimer_cancel(&port->enable_frs_timer);
 	hrtimer_cancel(&port->vdm_state_machine_timer);
@@ -6439,7 +6446,6 @@ void tcpm_unregister_port(struct tcpm_port *port)
 	typec_unregister_port(port->typec_port);
 	usb_role_switch_put(port->role_sw);
 	tcpm_debugfs_exit(port);
-	kthread_destroy_worker(port->wq);
 }
 EXPORT_SYMBOL_GPL(tcpm_unregister_port);

thanks,

Signature · 1 line
heikki
#2 lore raw
Quoted 9 lines · Heikki Krogerus

Hi,

Review void tcpm_unregister_port(struct tcpm_port *port) · line 6433

Quoted 3 lines · Xu Yang
(quoted diff) +0 -0
@@ -6428,6 +6432,9 @@ void tcpm_unregister_port(struct tcpm_port *port)
  {
  	int i;

You need to take the port lock here, no?

Quoted 7 lines
         mutex_lock(&port->lock);

drivers/usb/typec/tcpm/tcpm.c void tcpm_unregister_port(struct tcpm_port *port) · line 6436

Quoted 2 lines
drivers/usb/typec/tcpm/tcpm.c +2 -0
+	kthread_destroy_worker(port->wq);
+	port->wq = NULL;
         mutex_unlock(&port->lock);

I don't think the timer code runs under the lock, so that won't help. But, yes, the code is inherently racy. At the very least, port->wq would have to be set to NULL first, but even that would have to be synchronized. I wonder how other drivers handle that situation.

Guenter

Quoted 13 lines

drivers/usb/typec/tcpm/tcpm.c void tcpm_unregister_port(struct tcpm_port *port) · line 6450

Quoted 10 lines
drivers/usb/typec/tcpm/tcpm.c +0 -1
  	hrtimer_cancel(&port->send_discover_timer);
  	hrtimer_cancel(&port->enable_frs_timer);
  	hrtimer_cancel(&port->vdm_state_machine_timer);
@@ -6439,7 +6446,6 @@ void tcpm_unregister_port(struct tcpm_port *port)
  	typec_unregister_port(port->typec_port);
  	usb_role_switch_put(port->role_sw);
  	tcpm_debugfs_exit(port);
-	kthread_destroy_worker(port->wq);
  }
  EXPORT_SYMBOL_GPL(tcpm_unregister_port);

thanks,

author
#3 lore raw
In reply to Heikki Krogerus

Hi,

Quoted 21 lines

-----Original Message-----
From: Heikki Krogerus <[email protected]>
Sent: Thursday, November 18, 2021 9:18 PM
To: Xu Yang <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; Jun Li <[email protected]>; dl-linux-imx <linux- [email protected]>
Subject: [EXT] Re: [PATCH] usb: typec: tcpm: fix tcpm unregister port but leave a pending timer

Caution: EXT Email

Hi,

Review void tcpm_unregister_port(struct tcpm_port · line 6433

Quoted 3 lines · Xu Yang
(quoted diff) +0 -0
@@ -6428,6 +6432,9 @@ void tcpm_unregister_port(struct tcpm_port
 *port)  {
      int i;

You need to take the port lock here, no?

Quoted 6 lines
        mutex_lock(&port->lock);

drivers/usb/typec/tcpm/tcpm.c void tcpm_unregister_port(struct tcpm_port *port) · line 6436

Quoted 2 lines
drivers/usb/typec/tcpm/tcpm.c +2 -0
+     kthread_destroy_worker(port->wq);
+     port->wq = NULL;
        mutex_unlock(&port->lock);

I think we should not take the port lock before kthread_destroy_worker() since a deadlock might occur. Considering a work is pending and tcpm_unregister_port is called at this time, the worker needs to flush all the works after taking the port lock in tcpm_unregister_port(). However, the work can't take the port lock anymore.

Xu Yang

Quoted 17 lines

drivers/usb/typec/tcpm/tcpm.c void tcpm_unregister_port(struct tcpm_port

Quoted 5 lines
drivers/usb/typec/tcpm/tcpm.c +0 -0
      hrtimer_cancel(&port->send_discover_timer);
      hrtimer_cancel(&port->enable_frs_timer);
      hrtimer_cancel(&port->vdm_state_machine_timer);
@@ -6439,7 +6446,6 @@ void tcpm_unregister_port(struct tcpm_port

*port)

drivers/usb/typec/tcpm/tcpm.c void tcpm_unregister_port(struct tcpm_port *port) · line 6450

Quoted 6 lines
drivers/usb/typec/tcpm/tcpm.c +0 -1
      typec_unregister_port(port->typec_port);
      usb_role_switch_put(port->role_sw);
      tcpm_debugfs_exit(port);
-     kthread_destroy_worker(port->wq);
 }
 EXPORT_SYMBOL_GPL(tcpm_unregister_port);

thanks,

--
heikki

maintainer
#4 lore raw
Quoted 28 lines · Xu Yang

Hi,

Quoted 26 lines

-----Original Message-----
From: Heikki Krogerus <[email protected]>
Sent: Thursday, November 18, 2021 9:18 PM
To: Xu Yang <[email protected]>
Cc: [email protected]; [email protected]; linux- [email protected]; Jun Li <[email protected]>; dl-linux-imx <linux- [email protected]>
Subject: [EXT] Re: [PATCH] usb: typec: tcpm: fix tcpm unregister port but leave a pending timer

Caution: EXT Email

Hi,

Review void tcpm_unregister_port(struct tcpm_port · line 6433

Quoted 3 lines · Xu Yang
(quoted diff) +0 -0
@@ -6428,6 +6432,9 @@ void tcpm_unregister_port(struct tcpm_port
 *port)  {
      int i;

You need to take the port lock here, no?

        mutex_lock(&port->lock);
Quoted 2 lines
drivers/usb/typec/tcpm/tcpm.c +2 -0
+     kthread_destroy_worker(port->wq);
+     port->wq = NULL;
Quoted 7 lines
Quoted 1 line
        mutex_unlock(&port->lock);

I think we should not take the port lock before kthread_destroy_worker() since a deadlock might occur. Considering a work is pending and tcpm_unregister_port is called at this time, the worker needs to flush all the works after taking the port lock in tcpm_unregister_port(). However, the work can't take the port lock anymore.

The point is that you create a race with that code. If the port lock is not useful in this case, there needs to be something else.

thanks,

Signature · 1 line
heikki