From: Yaohan Li <shunsheng1218@gmail.com>
To: westeri@kernel.org
Cc: andreas.noever@gmail.com, YehezkelShB@gmail.com,
linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [RFC PATCH] thunderbolt: cancel pending DP bandwidth requests on teardown
Date: Thu, 19 Nov 2026 10:35:58 +0800 [thread overview]
Message-ID: <20261119023558.1495581-1-shunsheng1218@gmail.com> (raw)
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.
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
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(-)
diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c
index 57c11ba13459..470e32e8b9f7 100644
--- a/drivers/thunderbolt/tb.c
+++ b/drivers/thunderbolt/tb.c
@@ -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);
--
2.34.1
reply other threads:[~2026-11-19 2:36 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261119023558.1495581-1-shunsheng1218@gmail.com \
--to=shunsheng1218@gmail.com \
--cc=YehezkelShB@gmail.com \
--cc=andreas.noever@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=westeri@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®