* Re: [RFC PATCH] thunderbolt: cancel the queued control requests on teardown
2026-11-19 7:55 [RFC PATCH] thunderbolt: cancel the queued control requests on teardown Yaohan Li
@ 2026-11-19 8:12 ` sashiko-bot
2026-11-19 11:19 ` [RFC PATCH v2] " Yaohan Li
1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-19 8:12 UTC (permalink / raw)
To: Yaohan Li; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] thunderbolt: stack UAF on completion in tb_cfg_request_sync()
on cancel race
- [Medium] thunderbolt: leak of tb_cfg_request in
__tb_xdomain_response() on cancel
- [Medium] thunderbolt: tb_cfg_request_set_error() overwrites completed
request result
- [Low] thunderbolt: kernel-doc warnings in tb_ctl_submit_enter() and
structs
- [Low] thunderbolt: missing Fixes trailer for commit d7f781bfdbf4
--
commit 1583cf6e85652051b8456562f0d075e282549f62
Author: Yaohan Li <shunsheng1218@gmail.com>
> thunderbolt: cancel the queued control requests on teardown
>
> tb_domain_remove() stops the control channel but nothing cancels the
> requests still in its queue. Their completion work is already scheduled
> and the queue itself holds a reference, so tb_ctl_free() releases the
> channel, the rings and the frame pool while that work is still pending.
>
> Splice the queue instead, cancel the work and complete the callback data
> with -ENOTCONN. The reference the queue held is taken before the request
> becomes unprotected, so a completion that runs in the meantime cannot
> free it under the loop. The terminal error is published once under a
> small lock, because a request can be canceled by its owner and by the
> teardown at the same time: the value is written before the flag that
> announces it, so a canceler that loses the race never reads a stale
> result.
>
> A synchronous request waits on a completion that it passes as the
> callback data, and a canceled request never runs its callback. It
> installs that same completion as its cancel hook so the teardown wakes
> it, guarded by a one-shot flag because the owner and the teardown can
> both reach it.
>
> Two windows have to be covered and one counter cannot cover both. The
> first is the request that has been admitted and queued but has not been
> handed to the TX ring yet: the teardown cancels it, but the submitter
> still dereferences the channel to send it. The second is the synchronous
> user that keeps using the channel after the send, through the wait, the
> timeout and the cancel, and the teardown can run in between. Every user
> is therefore registered on the channel with two counters, both taken
> under request_queue_lock - the lock tb_ctl_stop() uses to clear @running,
> so no user can appear once the channel has been stopped. @submitting
> covers the first window, and the rings are not stopped before it drops to
> zero; @sync_users covers the second, and tb_ctl_cancel_requests() waits
> for it before the channel may be freed.
>
> tb_domain_add() calls tb_ctl_cancel_requests() on its error path as
> well: a failing driver_ready() or start() leaves requests behind and that
> domain is released without tb_domain_remove().
>
> Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
[Severity: Low]
This fixes a teardown use-after-free when queued requests are left in
ctl->request_queue. Should this include:
Fixes: d7f781bfdbf4 ("thunderbolt: Rework control channel to be more reliable")
> diff --git a/drivers/thunderbolt/ctl.c b/drivers/thunderbolt/ctl.c
> index 965988b18608f..58d0c68510131 100644
> --- a/drivers/thunderbolt/ctl.c
> +++ b/drivers/thunderbolt/ctl.c
[ ... ]
> @@ -546,12 +554,76 @@ static void tb_cfg_request_work(struct work_struct *work)
> *
> * Return: %0 on success, negative errno otherwise.
> */
> +/*
> + * Admission for everything that uses the channel. tb_ctl_stop() clears
> + * @running under @request_queue_lock, so taking the same lock here is what
> + * makes the waits below complete: once the channel has been stopped no new
> + * user can appear.
> + *
> + * @submitting covers a request from the moment it is admitted until it has
> + * been handed to the TX ring. Such a request is already queued, so the
> + * teardown cancels it, but it still dereferences @ctl in tb_ctl_tx() and the
> + * rings must not be stopped under it.
> + *
> + * @sync_users covers the longer window of a synchronous request, which keeps
> + * using @ctl after the send has completed: it waits, possibly times out and
> + * then cancels the request.
> + */
> +static int tb_ctl_submit_enter(struct tb_ctl *ctl)
[Severity: Low]
Inserting tb_ctl_submit_enter() and the other static helpers here separates
the tb_cfg_request() kernel-doc block above from tb_cfg_request(), causing
scripts/kernel-doc warnings on W=1 builds.
[ ... ]
> +/*
> + * If @callback did not get a chance to run then the owner of
> + * @callback_data did not get a chance to release it either. The hook runs
> + * at most once: a request can be canceled by its owner and by
> + * tb_ctl_cancel_requests() and the data it releases (or the completion it
> + * wakes) must not be touched twice.
> + */
> +static void tb_cfg_request_release_callback_data(struct tb_cfg_request *req)
> +{
> + if (req->cancel && !test_bit(TB_CFG_REQUEST_COMPLETED, &req->flags) &&
> + !test_and_set_bit(TB_CFG_REQUEST_CANCEL_HANDLED, &req->flags))
> + req->cancel(req->callback_data);
> +}
> +
> +/*
> + * The terminal error is published once. A request can be canceled by its
> + * owner after a timeout and by tb_ctl_cancel_requests() at the same time;
> + * the error is written before the flag that announces it, both under
> + * @result_lock, so a canceler that loses the race never observes the flag
> + * before the value it guards.
> + */
> +static void tb_cfg_request_set_error(struct tb_cfg_request *req, int err)
> +{
> + unsigned long flags;
> +
> + spin_lock_irqsave(&req->result_lock, flags);
> + if (!test_bit(TB_CFG_REQUEST_ERROR_SET, &req->flags)) {
> + req->result.err = err;
> + set_bit(TB_CFG_REQUEST_ERROR_SET, &req->flags);
> + }
> + spin_unlock_irqrestore(&req->result_lock, flags);
> +}
[Severity: Medium]
Can tb_cfg_request_set_error() overwrite req->result.err on a request that
has already completed?
Normal completion in req->copy() sets req->result without setting
TB_CFG_REQUEST_ERROR_SET, and tb_cfg_request_work() runs req->callback()
before tb_cfg_request_dequeue() takes ctl->request_queue_lock. If
tb_ctl_cancel_requests() splices ctl->request_queue in that window, both
sides race after tb_cfg_request_work() finishes:
CPU0 CPU1
tb_cfg_request_work()
req->callback(&done)
complete(&done)
set_bit(TB_CFG_REQUEST_COMPLETED)
tb_ctl_cancel_requests()
lock(&ctl->request_queue_lock)
/* splices req onto requests */
unlock(&ctl->request_queue_lock)
tb_cfg_request_dequeue(req)
cancel_work_sync(&req->work)
tb_cfg_request_set_error(req,
-ENOTCONN)
req->result.err = -ENOTCONN
tb_cfg_request_sync()
flush_work(&req->work)
res = req->result <-- data race / false -ENOTCONN
Unlike tb_cfg_request_release_callback_data(), tb_cfg_request_set_error()
does not check TB_CFG_REQUEST_COMPLETED.
[ ... ]
> @@ -621,18 +729,41 @@ struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
> DECLARE_COMPLETION_ONSTACK(done);
> int ret;
>
> - ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done);
> + /*
> + * A user that times out goes on to call tb_cfg_request_cancel(), which
> + * dereferences @ctl. Register so that the channel is not freed under
> + * it.
> + */
> + ret = tb_ctl_sync_enter(ctl);
> if (ret) {
> res.err = ret;
> return res;
> }
>
> + /*
> + * A synchronous request is completed by its callback, so the same
> + * function serves as the cancel hook: a teardown that cancels the
> + * request then wakes the waiter instead of leaving it to its timeout
> + * with the control channel already gone.
> + */
> + req->cancel = tb_cfg_request_complete;
> +
> + ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done);
> + if (ret) {
> + res.err = ret;
> + goto out;
> + }
> +
> if (!wait_for_completion_timeout(&done, timeout))
> tb_cfg_request_cancel(req, -ETIMEDOUT);
>
> flush_work(&req->work);
>
> - return req->result;
> + res = req->result;
> +out:
> + tb_ctl_sync_exit(ctl);
> +
> + return res;
> }
[Severity: High]
Can tb_ctl_cancel_requests() call complete(&done) after
tb_cfg_request_sync() has already timed out and returned?
test_and_set_bit(TB_CFG_REQUEST_CANCEL_HANDLED, &req->flags) in
tb_cfg_request_release_callback_data() ensures req->cancel() is entered only
once, but the losing caller returns immediately without waiting for the
winning caller to finish req->cancel(req->callback_data):
CPU0 CPU1
tb_cfg_request_sync() tb_ctl_cancel_requests()
wait_for_completion_timeout(&done)
/* times out */
tb_cfg_request_release_callback_data()
!test_and_set_bit(...CANCEL_HANDLED)
/* wins */
tb_cfg_request_cancel(req, ...)
tb_cfg_request_release_callback_data()
!test_and_set_bit(...CANCEL_HANDLED)
/* loses, returns immediately */
flush_work(&req->work)
tb_ctl_sync_exit(ctl)
return res; /* pops done */
req->cancel(&done)
complete(&done)
<-- stack use-after-free
flush_work(&req->work) does not serialize against req->cancel() because
tb_ctl_cancel_requests() calls tb_cfg_request_release_callback_data()
directly, and wait_event(ctl->idle_wait, !READ_ONCE(ctl->sync_users)) in
tb_ctl_cancel_requests() runs only after the cancellation loop finishes.
> diff --git a/drivers/thunderbolt/ctl.h b/drivers/thunderbolt/ctl.h
> index db1646eb4fd0b..8ebfdef35f2f4 100644
> --- a/drivers/thunderbolt/ctl.h
> +++ b/drivers/thunderbolt/ctl.h
[ ... ]
> @@ -89,6 +92,9 @@ struct tb_cfg_request {
> bool (*copy)(struct tb_cfg_request *req, const struct ctl_pkg *pkg);
> void (*callback)(void *callback_data);
> void *callback_data;
> + void (*cancel)(void *callback_data);
> + /* serializes the publication of the terminal error */
> + spinlock_t result_lock;
> unsigned long flags;
> struct work_struct work;
> struct tb_cfg_result result;
[Severity: Low]
struct tb_cfg_request has a kernel-doc comment above it, but @result_lock is
not documented there (and @stopped is similarly missing from struct icm's
kernel-doc comment in drivers/thunderbolt/icm.c).
> diff --git a/drivers/thunderbolt/icm.c b/drivers/thunderbolt/icm.c
> index a295a5e6cc9cb..1e6ab9ca55f43 100644
> --- a/drivers/thunderbolt/icm.c
> +++ b/drivers/thunderbolt/icm.c
[ ... ]
> @@ -2280,6 +2297,7 @@ static int icm_usb4_switch_nvm_authenticate(struct tb *tb, u64 route)
>
> req->match = icm_match;
> req->copy = icm_copy;
> + req->cancel = icm_usb4_switch_nvm_auth_cancel;
> req->request = &auth->request;
> req->request_size = sizeof(auth->request);
> req->request_type = TB_CFG_PKG_ICM_CMD;
[Severity: Medium]
Does __tb_xdomain_response() need a req->cancel hook as well so its
struct tb_cfg_request is not leaked when canceled on teardown?
In drivers/thunderbolt/xdomain.c, __tb_xdomain_response() transfers its
initial req reference to response_ready() once tb_cfg_request() succeeds:
drivers/thunderbolt/xdomain.c:__tb_xdomain_response() {
...
ret = tb_cfg_request(ctl, req, response_ready, req);
if (ret)
tb_cfg_request_put(req);
return ret;
}
Because req->response is NULL, tb_cfg_request() enqueues req (taking a
second reference for ctl->request_queue) and schedules req->work. If
tb_ctl_cancel_requests() runs before tb_cfg_request_work() executes,
cancel_work_sync(&req->work) prevents response_ready() from running and
tb_ctl_cancel_requests() drops only the queue's reference, leaking req with
kref == 1 unless req->cancel = response_ready is also set.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119075557.3399335-1-shunsheng1218@gmail.com?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread* [RFC PATCH v2] thunderbolt: cancel the queued control requests on teardown
2026-11-19 7:55 [RFC PATCH] thunderbolt: cancel the queued control requests on teardown Yaohan Li
2026-11-19 8:12 ` sashiko-bot
@ 2026-11-19 11:19 ` Yaohan Li
2026-11-19 11:43 ` sashiko-bot
1 sibling, 1 reply; 4+ messages in thread
From: Yaohan Li @ 2026-11-19 11:19 UTC (permalink / raw)
To: Mika Westerberg; +Cc: Andreas Noever, Yehezkel Bernat, linux-usb, linux-kernel
tb_domain_remove() stops the control channel but nothing cancels the
requests still in its queue. Their completion work is already scheduled
and the queue itself holds a reference, so tb_ctl_free() releases the
channel, the rings and the frame pool while that work is still pending.
Splice the queue instead, cancel the work and release the callback data
with -ENOTCONN. The reference the queue held is taken before the request
becomes unprotected, so a completion that runs in the meantime cannot
free it under the loop.
The terminal state is published once. A request can be canceled by its
owner after a timeout and by the teardown at the same time, so the error
and the cancellation hook run under one small lock, and only for a request
whose completion work has not finished: both callers reach this after
cancel_work_sync(), so a request whose callback already ran keeps the
result it published instead of being turned into -ENOTCONN.
A synchronous request waits on a completion that it passes as the callback
data, and a canceled request never runs its callback. It installs that
same completion as its cancel hook so the teardown wakes it. The hook runs
under the same lock, and the owner takes that lock before its stack
completion goes away and clears the hook, so a hook that is still running
on another thread has finished by the time the waiter returns.
The callback data can be the request itself: __tb_xdomain_response() hands
its reference to response_ready(), which is the callback. A request that
is canceled instead never runs it, so the same function is installed as the
cancel hook and releases the reference from there. The submission failure
path already drops it and keeps doing so.
Two windows have to be covered and one counter cannot cover both. The
first is the request that has been admitted and queued but has not been
handed to the TX ring yet: the teardown cancels it, but the submitter
still dereferences the channel to send it. The second is the synchronous
user that keeps using the channel after the send, through the wait, the
timeout and the cancel, and the teardown can run in between. Every user
is therefore registered on the channel with two counters, both taken
under request_queue_lock - the lock tb_ctl_stop() uses to clear @running,
so no user can appear once the channel has been stopped. @submitting
covers the first window, and the rings are not stopped before it drops to
zero; @sync_users covers the second, and tb_ctl_cancel_requests() waits
for it before the channel may be freed.
tb_domain_add() calls tb_ctl_cancel_requests() on its error path as
well: a failing driver_ready() or start() leaves requests behind and that
domain is released without tb_domain_remove().
Fixes: d7f781bfdbf4 ("thunderbolt: Rework control channel to be more reliable")
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
Prepared against v7.4-rc1-1969-g666539eecdf4.
Tested with a controlled interleaving test: drivers/thunderbolt/ctl.c is
compiled verbatim into a module on a KASAN kernel built from this tree
(x86_64, QEMU/KVM), with only the NHI ring backend replaced. The test parks
a submitter inside tb_ctl_tx() with a completion, calls tb_ctl_stop() from
another thread and checks that the rings are stopped only after the submitter
returned; it also checks that a synchronous waiter is woken by
tb_ctl_cancel_requests() with -ENOTCONN, that an owner cancel racing the
teardown cancel releases callback_data once and publishes one terminal error,
and that a submitter arriving after the stop is rejected without touching the
ring.
All 42 assertions pass on this patch. Against 666539eecdf4 the same
scenarios fail (the ring is stopped before the submitter returns, tx=2
stop=1). The five interleavings added for the cancellation protocol are:
- a cancel hook that is parked inside the lock blocks the second canceller
until it returns, and the hook runs exactly once;
- a synchronous owner that is woken by the teardown clears its stack
completion hook before it returns, and reads the published result;
- a request whose callback already ran keeps its result when the teardown
cancels it afterwards, and its cancel hook is not called;
- a request whose callback and cancel hook hand over the same reference
(the __tb_xdomain_response() pattern) releases it when it is canceled,
which the test checks through a reference it keeps itself;
- the worst case of the two: the synchronous owner runs into its timeout
while the teardown's hook is parked inside the lock just before it wakes
the owner. The owner must not return before that hook finishes, and the
result it reads is the one the teardown published.
The Thunderbolt controller itself was not available, so no hardware timing
was exercised.
The initialisation failure, suspend and cancellation paths were audited
rather than run. tb_ctl_alloc() hands partially initialised pointers to
tb_ctl_free(), which is safe: dma_pool_destroy() returns early for a NULL
pool and tb_ctl_pkg_free() accepts a NULL package. tb_domain_suspend() and
tb_domain_suspend_noirq() call tb_ctl_stop() with tb->lock held and leave the
queued requests for the resume, which is unchanged, and no submitter takes
tb->lock, so the new wait cannot invert against it. The owner-cancel and
teardown-cancel race is the one the test drives.
Checked against mainline 21a7f44fd076, where the patch applies, and against
the list traffic: no fix for this teardown is in flight. The "dangling
request in request_queue" warning that tb_ctl_stop() still emits is being
discussed separately for AMD USB4 routers ("Fix tb->lock deadlock during
hot-unplug on AMD USB4 routers", 2026-10); that work moves the router reset,
it does not cancel control requests.
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/ctl.c | 239 ++++++++++++++++++++++++++++++++--
drivers/thunderbolt/ctl.h | 18 +++
drivers/thunderbolt/domain.c | 14 ++
drivers/thunderbolt/icm.c | 21 +++
drivers/thunderbolt/xdomain.c | 6 +
5 files changed, 286 insertions(+), 12 deletions(-)
diff --git a/drivers/thunderbolt/ctl.c b/drivers/thunderbolt/ctl.c
index 965988b18608..5ff0e04b8265 100644
--- a/drivers/thunderbolt/ctl.c
+++ b/drivers/thunderbolt/ctl.c
@@ -31,6 +31,9 @@
* @request_queue_lock: Lock protecting @request_queue
* @request_queue: List of outstanding requests
* @running: Is the control channel running at the moment
+ * @submitting: Requests between the queue and the TX ring
+ * @sync_users: Synchronous users inside tb_cfg_request_sync()
+ * @idle_wait: Woken when the last submitter or synchronous user leaves
* @timeout_msec: Default timeout for non-raw control messages
* @callback: Callback called when hotplug message is received
* @callback_data: Data passed to @callback
@@ -46,6 +49,9 @@ struct tb_ctl {
struct mutex request_queue_lock;
struct list_head request_queue;
bool running;
+ int submitting;
+ int sync_users;
+ wait_queue_head_t idle_wait;
int timeout_msec;
event_cb callback;
@@ -528,12 +534,73 @@ static void tb_cfg_request_work(struct work_struct *work)
{
struct tb_cfg_request *req = container_of(work, typeof(*req), work);
- if (!tb_cfg_request_is_canceled(req))
+ if (!tb_cfg_request_is_canceled(req)) {
req->callback(req->callback_data);
+ set_bit(TB_CFG_REQUEST_COMPLETED, &req->flags);
+ }
tb_cfg_request_dequeue(req);
}
+/*
+ * Admission for everything that uses the channel. tb_ctl_stop() clears
+ * @running under @request_queue_lock, so taking the same lock here is what
+ * makes the waits below complete: once the channel has been stopped no new
+ * user can appear.
+ *
+ * @submitting covers a request from the moment it is admitted until it has
+ * been handed to the TX ring. Such a request is already queued, so the
+ * teardown cancels it, but it still dereferences @ctl in tb_ctl_tx() and the
+ * rings must not be stopped under it.
+ *
+ * @sync_users covers the longer window of a synchronous request, which keeps
+ * using @ctl after the send has completed: it waits, possibly times out and
+ * then cancels the request.
+ */
+static int tb_ctl_submit_enter(struct tb_ctl *ctl)
+{
+ int ret = 0;
+
+ mutex_lock(&ctl->request_queue_lock);
+ if (ctl->running)
+ ctl->submitting++;
+ else
+ ret = -ENOTCONN;
+ mutex_unlock(&ctl->request_queue_lock);
+
+ return ret;
+}
+
+static void tb_ctl_submit_exit(struct tb_ctl *ctl)
+{
+ mutex_lock(&ctl->request_queue_lock);
+ ctl->submitting--;
+ wake_up_all(&ctl->idle_wait);
+ mutex_unlock(&ctl->request_queue_lock);
+}
+
+static int tb_ctl_sync_enter(struct tb_ctl *ctl)
+{
+ int ret = 0;
+
+ mutex_lock(&ctl->request_queue_lock);
+ if (ctl->running)
+ ctl->sync_users++;
+ else
+ ret = -ENOTCONN;
+ mutex_unlock(&ctl->request_queue_lock);
+
+ return ret;
+}
+
+static void tb_ctl_sync_exit(struct tb_ctl *ctl)
+{
+ mutex_lock(&ctl->request_queue_lock);
+ ctl->sync_users--;
+ wake_up_all(&ctl->idle_wait);
+ mutex_unlock(&ctl->request_queue_lock);
+}
+
/**
* tb_cfg_request() - Start control request not waiting for it to complete
* @ctl: Control channel to use
@@ -551,7 +618,13 @@ int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
{
int ret;
+ ret = tb_ctl_submit_enter(ctl);
+ if (ret)
+ return ret;
+
req->flags = 0;
+ spin_lock_init(&req->result_lock);
+ mutex_init(&req->cancel_lock);
req->callback = callback;
req->callback_data = callback_data;
INIT_WORK(&req->work, tb_cfg_request_work);
@@ -559,23 +632,70 @@ int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
ret = tb_cfg_request_enqueue(ctl, req);
if (ret)
- return ret;
+ goto out;
ret = tb_ctl_tx(ctl, req->request, req->request_size,
req->request_type);
- if (ret)
- goto err_dequeue;
+ if (ret) {
+ tb_cfg_request_dequeue(req);
+ goto out;
+ }
if (!req->response)
schedule_work(&req->work);
- return 0;
+out:
+ tb_ctl_submit_exit(ctl);
-err_dequeue:
- tb_cfg_request_dequeue(req);
return ret;
}
+/*
+ * The terminal error is published once. A request can be canceled by its
+ * owner after a timeout and by tb_ctl_cancel_requests() at the same time;
+ * the error is written before the flag that announces it, both under
+ * @result_lock, so a canceler that loses the race never observes the flag
+ * before the value it guards.
+ */
+static void tb_cfg_request_set_error(struct tb_cfg_request *req, int err)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&req->result_lock, flags);
+ if (!test_bit(TB_CFG_REQUEST_ERROR_SET, &req->flags)) {
+ req->result.err = err;
+ set_bit(TB_CFG_REQUEST_ERROR_SET, &req->flags);
+ }
+ spin_unlock_irqrestore(&req->result_lock, flags);
+}
+
+/*
+ * Terminal state of a request that is canceled instead of completed. If
+ * @callback did not get a chance to run then the owner of @callback_data
+ * did not get a chance to release it either, so the cancel hook releases it
+ * instead; it runs at most once because a request can be canceled by its
+ * owner and by tb_ctl_cancel_requests() at the same time.
+ *
+ * @cancel_lock serializes the hook with the synchronous owner, which takes
+ * the same lock before the completion it passed as @callback_data goes
+ * away. Both callers reach this after cancel_work_sync(), so the completion
+ * work cannot be finishing the request at the same time: a request whose
+ * callback already ran keeps the result it published.
+ */
+static void tb_cfg_request_finish_cancel(struct tb_cfg_request *req, int err)
+{
+ mutex_lock(&req->cancel_lock);
+ if (!test_bit(TB_CFG_REQUEST_COMPLETED, &req->flags)) {
+ tb_cfg_request_set_error(req, err);
+
+ if (req->cancel &&
+ !test_and_set_bit(TB_CFG_REQUEST_CANCEL_HANDLED,
+ &req->flags))
+ req->cancel(req->callback_data);
+ }
+ mutex_unlock(&req->cancel_lock);
+}
+
/**
* tb_cfg_request_cancel() - Cancel a control request
* @req: Request to cancel
@@ -590,7 +710,7 @@ void tb_cfg_request_cancel(struct tb_cfg_request *req, int err)
set_bit(TB_CFG_REQUEST_CANCELED, &req->flags);
cancel_work_sync(&req->work);
tb_cfg_request_dequeue(req);
- req->result.err = err;
+ tb_cfg_request_finish_cancel(req, err);
}
static void tb_cfg_request_complete(void *data)
@@ -612,6 +732,7 @@ static void tb_cfg_request_complete(void *data)
* Return: &struct tb_cfg_result with non-zero @err field if error
* has occurred.
*/
+
struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
struct tb_cfg_request *req,
int timeout_msec)
@@ -621,18 +742,50 @@ struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
DECLARE_COMPLETION_ONSTACK(done);
int ret;
- ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done);
+ /*
+ * A user that times out goes on to call tb_cfg_request_cancel(), which
+ * dereferences @ctl. Register so that the channel is not freed under
+ * it.
+ */
+ ret = tb_ctl_sync_enter(ctl);
if (ret) {
res.err = ret;
return res;
}
+ /*
+ * A synchronous request is completed by its callback, so the same
+ * function serves as the cancel hook: a teardown that cancels the
+ * request then wakes the waiter instead of leaving it to its timeout
+ * with the control channel already gone.
+ */
+ req->cancel = tb_cfg_request_complete;
+
+ ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done);
+ if (ret) {
+ res.err = ret;
+ goto out;
+ }
+
if (!wait_for_completion_timeout(&done, timeout))
tb_cfg_request_cancel(req, -ETIMEDOUT);
flush_work(&req->work);
- return req->result;
+ /*
+ * A teardown can cancel the request on another thread and run the
+ * hook above while this one is already returning. Taking the same
+ * lock waits for that invocation, and clearing the hook keeps it away
+ * from @done once this function returns.
+ */
+ mutex_lock(&req->cancel_lock);
+ res = req->result;
+ req->cancel = NULL;
+ mutex_unlock(&req->cancel_lock);
+out:
+ tb_ctl_sync_exit(ctl);
+
+ return res;
}
/* public interface, alloc/start/stop/free */
@@ -664,6 +817,7 @@ struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, int index, int timeout_msec,
ctl->callback_data = cb_data;
mutex_init(&ctl->request_queue_lock);
+ init_waitqueue_head(&ctl->idle_wait);
INIT_LIST_HEAD(&ctl->request_queue);
ctl->frame_pool = dma_pool_create("thunderbolt_ctl", nhi->dev,
TB_FRAME_SIZE, 4, 0);
@@ -735,7 +889,9 @@ void tb_ctl_start(struct tb_ctl *ctl)
for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
tb_ctl_rx_submit(ctl->rx_packets[i]);
+ mutex_lock(&ctl->request_queue_lock);
ctl->running = true;
+ mutex_unlock(&ctl->request_queue_lock);
}
/**
@@ -743,7 +899,8 @@ void tb_ctl_start(struct tb_ctl *ctl)
* @ctl: Control channel to stop
*
* All invocations of ctl->callback will have finished after this method
- * returns.
+ * returns. The requests that are still in the request queue are left for
+ * tb_ctl_cancel_requests() to cancel.
*
* Must NOT be called from ctl->callback.
*/
@@ -753,15 +910,73 @@ void tb_ctl_stop(struct tb_ctl *ctl)
ctl->running = false;
mutex_unlock(&ctl->request_queue_lock);
+ /*
+ * A request that passed the admission check is between the queue and
+ * the TX ring and still hands its packet to the ring, so wait for it
+ * before the rings are stopped.
+ */
+ wait_event(ctl->idle_wait, !READ_ONCE(ctl->submitting));
+
tb_ring_stop(ctl->rx);
tb_ring_stop(ctl->tx);
if (!list_empty(&ctl->request_queue))
tb_ctl_WARN(ctl, "dangling request in request_queue\n");
- INIT_LIST_HEAD(&ctl->request_queue);
tb_ctl_dbg(ctl, "control channel stopped\n");
}
+/**
+ * tb_ctl_cancel_requests() - cancel the requests that are still queued
+ * @ctl: Control channel whose requests are canceled
+ *
+ * Cancels all the requests that are still in the request queue and releases
+ * the reference taken by tb_cfg_request_enqueue(). The request callbacks
+ * may take locks that the caller holds, so this must be called after
+ * tb_ctl_stop() and outside of any such lock. Each request is kept alive
+ * until it has been canceled, so a completion work that runs in the
+ * meantime cannot free it under this function.
+ *
+ * Must NOT be called from ctl->callback.
+ */
+void tb_ctl_cancel_requests(struct tb_ctl *ctl)
+{
+ struct tb_cfg_request *req, *n;
+ LIST_HEAD(requests);
+
+ mutex_lock(&ctl->request_queue_lock);
+ list_for_each_entry_safe(req, n, &ctl->request_queue, list) {
+ /*
+ * Take a reference and drop the one held by the queue while
+ * the request is still protected by the lock. The requests
+ * are canceled outside of it and their completion work may
+ * run in the meantime: without the extra reference it could
+ * drop the last one and free the request under us.
+ */
+ tb_cfg_request_get(req);
+ set_bit(TB_CFG_REQUEST_CANCELED, &req->flags);
+ clear_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
+ list_del_init(&req->list);
+ list_add_tail(&req->list, &requests);
+ tb_cfg_request_put(req);
+ }
+ mutex_unlock(&ctl->request_queue_lock);
+
+ list_for_each_entry_safe(req, n, &requests, list) {
+ cancel_work_sync(&req->work);
+ list_del_init(&req->list);
+ tb_cfg_request_finish_cancel(req, -ENOTCONN);
+ tb_cfg_request_put(req);
+ }
+
+ /*
+ * @running has been cleared, so no new synchronous user can register.
+ * Wait for the ones that did: a user whose wait timed out is about to
+ * take @request_queue_lock in tb_cfg_request_cancel(), and the channel
+ * must not be freed before it has returned.
+ */
+ wait_event(ctl->idle_wait, !READ_ONCE(ctl->sync_users));
+}
+
/* public interface, commands */
/**
diff --git a/drivers/thunderbolt/ctl.h b/drivers/thunderbolt/ctl.h
index db1646eb4fd0..38765ea267c0 100644
--- a/drivers/thunderbolt/ctl.h
+++ b/drivers/thunderbolt/ctl.h
@@ -25,6 +25,7 @@ struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, int index, int timeout_msec,
event_cb cb, void *cb_data);
void tb_ctl_start(struct tb_ctl *ctl);
void tb_ctl_stop(struct tb_ctl *ctl);
+void tb_ctl_cancel_requests(struct tb_ctl *ctl);
void tb_ctl_free(struct tb_ctl *ctl);
/* configuration commands */
@@ -65,6 +66,10 @@ struct ctl_pkg {
* @copy: Function used to copy the incoming packet to @response
* @callback: Callback called when the request is finished successfully
* @callback_data: Data to be passed to @callback
+ * @cancel: Callback called when the request is canceled before @callback
+ * had a chance to run
+ * @result_lock: Serializes the publication of the terminal error
+ * @cancel_lock: Serializes the cancellation hook with the request owner
* @flags: Flags for the request
* @work: Work item used to complete the request
* @result: Result after the request has been completed
@@ -89,6 +94,16 @@ struct tb_cfg_request {
bool (*copy)(struct tb_cfg_request *req, const struct ctl_pkg *pkg);
void (*callback)(void *callback_data);
void *callback_data;
+ void (*cancel)(void *callback_data);
+ /* serializes the publication of the terminal error */
+ spinlock_t result_lock;
+ /*
+ * Serializes the terminal error and the cancellation hook. A
+ * synchronous owner takes it before it returns, so a hook that the
+ * teardown runs on another thread is done with the completion the
+ * owner passed as @callback_data before that completion goes away.
+ */
+ struct mutex cancel_lock;
unsigned long flags;
struct work_struct work;
struct tb_cfg_result result;
@@ -97,6 +112,9 @@ struct tb_cfg_request {
#define TB_CFG_REQUEST_ACTIVE 0
#define TB_CFG_REQUEST_CANCELED 1
+#define TB_CFG_REQUEST_COMPLETED 2
+#define TB_CFG_REQUEST_CANCEL_HANDLED 3
+#define TB_CFG_REQUEST_ERROR_SET 4
struct tb_cfg_request *tb_cfg_request_alloc(void);
void tb_cfg_request_get(struct tb_cfg_request *req);
diff --git a/drivers/thunderbolt/domain.c b/drivers/thunderbolt/domain.c
index 24611f05b3cd..40898c1ecd80 100644
--- a/drivers/thunderbolt/domain.c
+++ b/drivers/thunderbolt/domain.c
@@ -488,6 +488,14 @@ int tb_domain_add(struct tb *tb, bool reset)
tb_ctl_stop(tb->ctl);
mutex_unlock(&tb->lock);
+ /*
+ * A failing driver_ready() or start() can leave requests behind, and
+ * this domain is released without tb_domain_remove(), so the queue has
+ * to be canceled here. The callbacks take tb->lock, hence the unlock
+ * above.
+ */
+ tb_ctl_cancel_requests(tb->ctl);
+
return ret;
}
@@ -507,6 +515,12 @@ void tb_domain_remove(struct tb *tb)
tb_ctl_stop(tb->ctl);
mutex_unlock(&tb->lock);
+ /*
+ * The requests that were still in the queue are canceled here, outside
+ * of tb->lock, because their callbacks take it.
+ */
+ tb_ctl_cancel_requests(tb->ctl);
+
flush_workqueue(tb->wq);
if (tb->cm_ops->deinit)
diff --git a/drivers/thunderbolt/icm.c b/drivers/thunderbolt/icm.c
index a295a5e6cc9c..1e86c3df4202 100644
--- a/drivers/thunderbolt/icm.c
+++ b/drivers/thunderbolt/icm.c
@@ -75,6 +75,8 @@ struct usb4_switch_nvm_auth {
* @max_boot_acl: Maximum number of preboot ACL entries (%0 if not supported)
* @rpm: Does the controller support runtime PM (RTD3)
* @can_upgrade_nvm: Can the NVM firmware be upgrade on this controller
+ * @stopped: Set while the domain is being removed; late NVM_AUTH results
+ * are dropped instead of touching the request that is gone
* @proto_version: Firmware protocol version
* @last_nvm_auth: Last USB4 router NVM_AUTH result (or %NULL if not set)
* @veto: Is RTD3 veto in effect
@@ -100,6 +102,8 @@ struct icm {
size_t max_boot_acl;
bool rpm;
bool can_upgrade_nvm;
+ /* set while the domain is being removed; late responses are dropped */
+ bool stopped;
u8 proto_version;
struct usb4_switch_nvm_auth *last_nvm_auth;
bool veto;
@@ -2229,6 +2233,7 @@ static void icm_stop(struct tb *tb)
tb_switch_remove(tb->root_switch);
tb->root_switch = NULL;
nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
+ icm->stopped = true;
kfree(icm->last_nvm_auth);
icm->last_nvm_auth = NULL;
}
@@ -2249,12 +2254,27 @@ static void icm_usb4_switch_nvm_auth_complete(void *data)
auth->reply.hdr.flags, auth->reply.status);
mutex_lock(&tb->lock);
+ /*
+ * The domain may have been stopped while the response was on its way.
+ * @last_nvm_auth was released by icm_stop() already, so release the
+ * response here instead of leaking it.
+ */
+ if (icm->stopped) {
+ mutex_unlock(&tb->lock);
+ kfree(auth);
+ return;
+ }
if (WARN_ON(icm->last_nvm_auth))
kfree(icm->last_nvm_auth);
icm->last_nvm_auth = auth;
mutex_unlock(&tb->lock);
}
+static void icm_usb4_switch_nvm_auth_cancel(void *data)
+{
+ kfree(data);
+}
+
static int icm_usb4_switch_nvm_authenticate(struct tb *tb, u64 route)
{
struct usb4_switch_nvm_auth *auth;
@@ -2280,6 +2300,7 @@ static int icm_usb4_switch_nvm_authenticate(struct tb *tb, u64 route)
req->match = icm_match;
req->copy = icm_copy;
+ req->cancel = icm_usb4_switch_nvm_auth_cancel;
req->request = &auth->request;
req->request_size = sizeof(auth->request);
req->request_type = TB_CFG_PKG_ICM_CMD;
diff --git a/drivers/thunderbolt/xdomain.c b/drivers/thunderbolt/xdomain.c
index 45e26833c953..d82056c3cbc0 100644
--- a/drivers/thunderbolt/xdomain.c
+++ b/drivers/thunderbolt/xdomain.c
@@ -150,6 +150,12 @@ static int __tb_xdomain_response(struct tb_ctl *ctl, const void *response,
req->request = response;
req->request_size = size;
req->request_type = type;
+ /*
+ * The reference taken here belongs to the callback once the request is
+ * submitted. A teardown that cancels the request never runs the
+ * callback, so the same function has to release it.
+ */
+ req->cancel = response_ready;
ret = tb_cfg_request(ctl, req, response_ready, req);
if (ret)
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread