Skip to contentSkip to conversation

thunderbolt: cancel pending DP bandwidth requests on teardown

RFC PATCH In review Yaohan Li 3 participants 4 messages Nov 19 – Nov 24

Subsystems (2)
lore
Share & tools
lore

Yaohan Li

author
#0 lore raw

RFC PATCH 1/1 thunderbolt: cancel pending DP bandwidth requests on teardown

tb_queue_dp_bandwidth_request() queues its delayed work and keeps no record of it. The retry path in tb_handle_dp_bandwidth_request() queues the work again with a 50 ms delay, and tb_deinit() only cancels the bandwidth group release works. The flush_workqueue() in tb_domain_remove() runs before tb_deinit() but it does not cover a delayed work whose timer has not expired yet, so a request that is waiting for its retry can run after the domain has been removed and the tb structure has been freed.

Record the queued requests in tb_cm and cancel the remaining ones in tb_deinit(), which runs after the workqueue has been flushed and before the domain device is unregistered. A request is recorded and queued while holding dp_bandwidth_lock, so tb_deinit() either sees it and cancels it or the request is rejected before it is queued at all. A work item that is already running hands the ownership of its request back to tb_deinit() through the canceled flag.

This covers the list and the delayed work, not the lifetime of @tb. dp_bandwidth_closed keeps new work from being queued, but it is not a reference on @tb. Both in-tree callers are reached from the DP bandwidth handling in tb.c - one from the bandwidth work that runs on tb->wq, one from the control channel event path that tb_ctl_stop() stops - and tb_deinit() runs after both have been drained.

---
Prepared against v7.4-rc1-1969-g666539eecdf4.

Compile tested only: arm64 with aarch64-linux-gnu-gcc 11.4 and CONFIG_USB4=m. The affected hardware was not available, so the teardown path was not exercised and no fault or race injection was run.

AI assistance: the analysis, the change and its review notes were prepared with Claude (Anthropic) as a coding assistant. The author reviewed the reasoning and takes responsibility for the content.

---
 drivers/thunderbolt/tb.c | 59 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 58 insertions(+), 1 deletion(-)
drivers/thunderbolt/tb.c +58 -1
index 57c11ba13459..470e32e8b9f7 100644
@@ -57,6 +57,9 @@ MODULE_PARM_DESC(asym_threshold,
  *		    events and exit if this is not set (it needs to
  *		    acquire the lock one more time). Used to drain wq
  *		    after cfg has been paused.
+ * @dp_bandwidth_requests: Pending DP bandwidth allocation requests
+ * @dp_bandwidth_lock: Protects @dp_bandwidth_requests
+ * @dp_bandwidth_closed: New DP bandwidth requests are rejected
  * @remove_work: Work used to remove any unplugged routers after
  *		 runtime resume
  * @groups: Bandwidth groups used in this domain.
@@ -64,7 +67,10 @@ MODULE_PARM_DESC(asym_threshold,
 struct tb_cm {
 	struct list_head tunnel_list;
 	struct list_head dp_resources;
+	struct list_head dp_bandwidth_requests;
+	spinlock_t dp_bandwidth_lock;	/* Serializes the request list. */
 	bool hotplug_active;
+	bool dp_bandwidth_closed;
 	struct delayed_work remove_work;
 	struct tb_bandwidth_group groups[MAX_GROUPS];
 };
@@ -76,10 +82,12 @@ static inline struct tb *tcm_to_tb(struct tb_cm *tcm)
 
 struct tb_hotplug_event {
 	struct delayed_work work;
+	struct list_head list;
 	struct tb *tb;
 	u64 route;
 	u8 port;
 	bool unplug;
+	bool canceled;
 	int retry;
 };
 
@@ -2752,6 +2760,7 @@ static void tb_handle_dp_bandwidth_request(struct work_struct *work)
 	struct tb_cm *tcm = tb_priv(tb);
 	struct tb_switch *sw;
 	struct tb_port *in;
+	unsigned long flags;
 
 	pm_runtime_get_sync(&tb->dev);
 
@@ -2872,13 +2881,22 @@ static void tb_handle_dp_bandwidth_request(struct work_struct *work)
 	pm_runtime_mark_last_busy(&tb->dev);
 	pm_runtime_put_autosuspend(&tb->dev);
 
-	kfree(ev);
+	spin_lock_irqsave(&tcm->dp_bandwidth_lock, flags);
+	if (!ev->canceled) {
+		list_del_init(&ev->list);
+		spin_unlock_irqrestore(&tcm->dp_bandwidth_lock, flags);
+		kfree(ev);
+		return;
+	}
+	spin_unlock_irqrestore(&tcm->dp_bandwidth_lock, flags);
 }
 
 static void tb_queue_dp_bandwidth_request(struct tb *tb, u64 route, u8 port,
 					  int retry, unsigned long delay)
 {
+	struct tb_cm *tcm = tb_priv(tb);
 	struct tb_hotplug_event *ev;
+	unsigned long flags;
 
 	ev = kmalloc_obj(*ev);
 	if (!ev)
@@ -2888,8 +2906,23 @@ static void tb_queue_dp_bandwidth_request(struct tb *tb, u64 route, u8 port,
 	ev->route = route;
 	ev->port = port;
 	ev->retry = retry;
+	ev->canceled = false;
 	INIT_DELAYED_WORK(&ev->work, tb_handle_dp_bandwidth_request);
+
+	/*
+	 * Record the request and queue the work while holding the lock so
+	 * that tb_deinit() either sees it and cancels it, or rejects the
+	 * request before it is queued at all.
+	 */
+	spin_lock_irqsave(&tcm->dp_bandwidth_lock, flags);
+	if (tcm->dp_bandwidth_closed) {
+		spin_unlock_irqrestore(&tcm->dp_bandwidth_lock, flags);
+		kfree(ev);
+		return;
+	}
+	list_add_tail(&ev->list, &tcm->dp_bandwidth_requests);
 	queue_delayed_work(tb->wq, &ev->work, delay);
+	spin_unlock_irqrestore(&tcm->dp_bandwidth_lock, flags);
 }
 
 static void tb_handle_notification(struct tb *tb, u64 route,
@@ -2996,11 +3029,33 @@ static void tb_stop(struct tb *tb)
 static void tb_deinit(struct tb *tb)
 {
 	struct tb_cm *tcm = tb_priv(tb);
+	struct tb_hotplug_event *ev, *tmp;
+	LIST_HEAD(requests);
+	unsigned long flags;
 	int i;
 
 	/* Cancel all the release bandwidth workers */
 	for (i = 0; i < ARRAY_SIZE(tcm->groups); i++)
 		cancel_delayed_work_sync(&tcm->groups[i].release_work);
+
+	/*
+	 * Cancel the DP bandwidth allocation requests that are still waiting
+	 * for their retry timer. They are not covered by the flush_workqueue()
+	 * done before this function is called because their delayed work has
+	 * not expired yet.
+	 */
+	spin_lock_irqsave(&tcm->dp_bandwidth_lock, flags);
+	tcm->dp_bandwidth_closed = true;
+	list_splice_init(&tcm->dp_bandwidth_requests, &requests);
+	list_for_each_entry(ev, &requests, list)
+		ev->canceled = true;
+	spin_unlock_irqrestore(&tcm->dp_bandwidth_lock, flags);
+
+	list_for_each_entry_safe(ev, tmp, &requests, list) {
+		cancel_delayed_work_sync(&ev->work);
+		list_del_init(&ev->list);
+		kfree(ev);
+	}
 }
 
 static int tb_scan_finalize_switch(struct device *dev, void *data)
@@ -3353,6 +3408,8 @@ struct tb *tb_probe(struct tb_nhi *nhi)
 	tcm = tb_priv(tb);
 	INIT_LIST_HEAD(&tcm->tunnel_list);
 	INIT_LIST_HEAD(&tcm->dp_resources);
+	INIT_LIST_HEAD(&tcm->dp_bandwidth_requests);
+	spin_lock_init(&tcm->dp_bandwidth_lock);
 	INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work);
 	tb_init_bandwidth_groups(tcm);
 
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] thunderbolt: missing Fixes trailer for DP bandwidth request teardown fix --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] thunderbolt: missing Fixes trailer for DP bandwidth request teardown fix
--

commit a1d5bdb78755e49d17ae3a6f04ee54979d0f9eb0
Author: Yaohan Li <[email protected]>

Quoted 19 lines

thunderbolt: cancel pending DP bandwidth requests on teardown

tb_queue_dp_bandwidth_request() queues its delayed work and keeps no record of it. The retry path in tb_handle_dp_bandwidth_request() queues the work again with a 50 ms delay, and tb_deinit() only cancels the bandwidth group release works. The flush_workqueue() in tb_domain_remove() runs before tb_deinit() but it does not cover a delayed work whose timer has not expired yet, so a request that is waiting for its retry can run after the domain has been removed and the tb structure has been freed.

Record the queued requests in tb_cm and cancel the remaining ones in tb_deinit(), which runs after the workqueue has been flushed and before the domain device is unregistered. A request is recorded and queued while holding dp_bandwidth_lock, so tb_deinit() either sees it and cancels it or the request is rejected before it is queued at all. A work item that is already running hands the ownership of its request back to tb_deinit() through the canceled flag.

Quoted 8 lines

This covers the list and the delayed work, not the lifetime of @tb. dp_bandwidth_closed keeps new work from being queued, but it is not a reference on @tb. Both in-tree callers are reached from the DP bandwidth handling in tb.c - one from the bandwidth work that runs on tb->wq, one from the control channel event path that tb_ctl_stop() stops - and tb_deinit() runs after both have been drained.

[Severity: Low]
Commit d6d458d42e1e ("thunderbolt: Handle DisplayPort tunnel activation asynchronously") converted tb_hotplug_event.work to a delayed_work and added the 50 ms retry path in tb_handle_dp_bandwidth_request(). Should this include a Fixes tag?

Fixes d6d458d42e1e ("thunderbolt: Handle DisplayPort tunnel activation asynchronously")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Yaohan Li

Hi,

Quoted 26 lines · Yaohan Li

tb_queue_dp_bandwidth_request() queues its delayed work and keeps no record of it. The retry path in tb_handle_dp_bandwidth_request() queues the work again with a 50 ms delay, and tb_deinit() only cancels the bandwidth group release works. The flush_workqueue() in tb_domain_remove() runs before tb_deinit() but it does not cover a delayed work whose timer has not expired yet, so a request that is waiting for its retry can run after the domain has been removed and the tb structure has been freed.

Record the queued requests in tb_cm and cancel the remaining ones in tb_deinit(), which runs after the workqueue has been flushed and before the domain device is unregistered. A request is recorded and queued while holding dp_bandwidth_lock, so tb_deinit() either sees it and cancels it or the request is rejected before it is queued at all. A work item that is already running hands the ownership of its request back to tb_deinit() through the canceled flag.

This covers the list and the delayed work, not the lifetime of @tb. dp_bandwidth_closed keeps new work from being queued, but it is not a reference on @tb. Both in-tree callers are reached from the DP bandwidth handling in tb.c - one from the bandwidth work that runs on tb->wq, one from the control channel event path that tb_ctl_stop() stops - and tb_deinit() runs after both have been drained.

Quoted 7 lines

---
Prepared against v7.4-rc1-1969-g666539eecdf4.

Compile tested only: arm64 with aarch64-linux-gnu-gcc 11.4 and CONFIG_USB4=m. The affected hardware was not available, so the teardown path was not exercised and no fault or race injection was run.

For complex fixes like this, I would definitely want a real hardware repro, and also a proof that your change actually fix it and does not cause any unexpected "side effects".

author
#3 lore raw

Hi Mika,

Thanks for the feedback.

I don't have access to Thunderbolt or USB4 hardware at the moment, so I cannot provide the hardware reproduction or the regression testing you asked for. I will put both RFCs on hold until I have that evidence.

I also take your points about keeping the fixes small and writing the changelogs myself.

Thanks,
Yaohan