From: sashiko-bot@kernel.org
To: "Yaohan Li" <shunsheng1218@gmail.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [RFC PATCH v2] thunderbolt: cancel the queued control requests on teardown
Date: Thu, 19 Nov 2026 11:43:52 +0000 [thread overview]
Message-ID: <sashiko-outbox-223054@kernel.org> (raw)
In-Reply-To: <20261119111907.3616041-1-shunsheng1218@gmail.com>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] thunderbolt: use-after-free race in tb_ctl_sync_exit() and
tb_ctl_submit_exit()
- [Medium] thunderbolt: timeout race in tb_cfg_request_sync() bypasses
sync_users drain
- [Medium] thunderbolt: unhandled -ENOTCONN in tb_cfg_read() and
tb_cfg_write() hits WARN
- [Medium] thunderbolt: spurious dev_WARN and unlocked queue check in
tb_ctl_stop()
--
commit 7ad987d4a0cdf2650a891259a5534997151f16c9
Author: Yaohan Li <shunsheng1218@gmail.com>
thunderbolt: cancel the queued control requests on teardown
This commit adds tb_ctl_cancel_requests() and request tracking counters to
cancel queued control requests and wait for active submitters and
synchronous users before freeing the control channel on teardown.
> diff --git a/drivers/thunderbolt/ctl.c b/drivers/thunderbolt/ctl.c
> index 965988b18608f..5ff0e04b8265c 100644
> --- a/drivers/thunderbolt/ctl.c
> +++ b/drivers/thunderbolt/ctl.c
[ ... ]
> @@ -528,12 +534,73 @@ static void tb_cfg_request_work(struct work_struct *work)
> 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);
> +}
[Severity: Medium]
Can tb_ctl_sync_exit() and tb_ctl_submit_exit() touch ctl after it has
been freed?
wait_event() in tb_ctl_cancel_requests() (and tb_ctl_stop()) tests
!READ_ONCE(ctl->sync_users) without taking ctl->request_queue_lock, and
neither tb_ctl_cancel_requests() nor tb_ctl_free() acquires
ctl->request_queue_lock afterwards:
CPU0 CPU1
tb_ctl_sync_exit(ctl)
mutex_lock(&ctl->request_queue_lock)
ctl->sync_users--; // becomes 0
tb_ctl_cancel_requests(ctl)
wait_event(..., !ctl->sync_users)
...
tb_ctl_free(ctl)
kfree(ctl)
wake_up_all(&ctl->idle_wait)
mutex_unlock(&ctl->request_queue_lock) <-- use-after-free
Acquiring and releasing ctl->request_queue_lock after wait_event() in
tb_ctl_stop() and tb_ctl_cancel_requests() would ensure the exiting thread
has finished unlocking before teardown proceeds.
[ ... ]
> @@ -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;
> }
[Severity: Medium]
Can a timeout racing with teardown let callers of tb_cfg_request_sync()
dereference ctl after ctl->sync_users has already dropped to zero?
CPU0 CPU1
tb_cfg_read()
tb_cfg_read_raw()
tb_cfg_request_sync(ctl, ...)
wait_for_completion_timeout()
tb_ctl_stop(ctl)
ctl->running = false
tb_cfg_request_cancel(..., -ETIMEDOUT)
tb_cfg_request_set_error(..., -ETIMEDOUT)
tb_ctl_cancel_requests(ctl)
tb_cfg_request_finish_cancel(..., -ENOTCONN)
tb_ctl_sync_exit(ctl)
ctl->sync_users--; // 0
wait_event(..., !ctl->sync_users)
...
tb_ctl_free(ctl)
kfree(ctl)
usleep_range(10, 100)
tb_cfg_request_sync(ctl, ...) <-- use-after-free
Because tb_cfg_request_set_error() keeps the first error written,
req->result.err stays -ETIMEDOUT even though ctl->running is already
false. Once tb_ctl_sync_exit() drops ctl->sync_users to 0,
tb_ctl_cancel_requests() finishes while tb_cfg_read_raw() (or
tb_cfg_write_raw() / icm_request()) sleeps in its retry loop and then
dereferences ctl again in tb_cfg_request_sync() and tb_ctl_warn().
> @@ -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");
> }
[Severity: Medium]
Does tb_ctl_stop() now trigger a dev_WARN backtrace whenever a request is
left in ctl->request_queue for tb_ctl_cancel_requests() to cancel?
tb_domain_add() and tb_domain_remove() call tb_ctl_stop() before
tb_ctl_cancel_requests(), so any request still queued at teardown hits
tb_ctl_WARN() here before tb_ctl_cancel_requests() can run. In addition,
list_empty(&ctl->request_queue) is checked without holding
ctl->request_queue_lock while completion work or a timeout can still be
dequeuing requests in tb_cfg_request_dequeue().
> +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);
> + }
[Severity: Medium]
Does canceling or rejecting a synchronous config read or write with
-ENOTCONN trigger a WARN() in tb_cfg_read() and tb_cfg_write()?
When a tb_cfg_read() or tb_cfg_write() call running outside tb->lock (such
as tb_xdp_handle_request() -> tb_port_read()) is canceled here or rejected
by tb_ctl_sync_enter() after tb_ctl_stop(), tb_cfg_read_raw() and
tb_cfg_write_raw() return res.err = -ENOTCONN, which falls into the
default branch in tb_cfg_read() and tb_cfg_write():
drivers/thunderbolt/ctl.c:tb_cfg_read() {
...
switch (res.err) {
case 0:
return 0;
case 1:
tb_cfg_print_error(ctl, route, &res);
return -EIO;
case -ETIMEDOUT:
tb_ctl_warn(ctl, "%llx: timeout reading config space %u from %#x\n",
route, space, offset);
break;
default:
WARN(1, "tb_cfg_read: %d\n", res.err);
break;
}
...
}
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119111907.3616041-1-shunsheng1218@gmail.com?part=1
next prev parent reply other threads:[~2026-11-19 11:43 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
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
2026-11-19 11:43 ` sashiko-bot [this message]
2026-11-20 4:27 ` [RFC PATCH v3] " Yaohan Li
2026-11-20 4:39 ` sashiko-bot
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=sashiko-outbox-223054@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=shunsheng1218@gmail.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.