From: Slawomir Stepien <sst@poczta.fm>
To: gregkh@linuxfoundation.org, jiashengjiangcool@gmail.com,
yun.zhou@windriver.com, zzzccc427@gmail.com,
bigeasy@linutronix.de, nab@linux-iscsi.org
Cc: Slawomir Stepien <sst@poczta.fm>,
linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
syzbot+2799146a53c356f8017d@syzkaller.appspotmail.com,
stable@vger.kernel.org
Subject: [PATCH v2 2/3] usb: gadget: f_tcm: detach gadget when dropping an enabled TPG
Date: Mon, 23 Nov 2026 15:25:12 +0100 [thread overview]
Message-ID: <20261123142513.204652-3-sst@poczta.fm> (raw)
In-Reply-To: <20261123142513.204652-1-sst@poczta.fm>
Removing a TPG directory via configfs rmdir ends up in usbg_drop_tpg()
through target_fabric_tpg_release(). The target core does not disable
the TPG first, so if the TPG is still enabled and the gadget is bound to
a UDC, usbg_drop_tpg() destroys the TPG workqueue and frees the TPG
while the bound function still references it through fu->tpg.
Subsequent USB traffic (tcm_setup(), bot_submit_command(),
usbg_submit_command(), ...) then dereferences freed memory and queues
work onto a destroyed workqueue.
Disable the TPG through usbg_enable_tpg() before tearing it down, so the
gadget is unbound in the same way as when writing 0 to the enable
attribute. Only do so when the gadget is connected: for the legacy
g_target driver, detaching a never-attached TPG would call
usb_composite_unregister() on a driver that was never registered.
This goes through tcm_unregister_callback(), which no longer holds
dep_lock across unregister_gadget_item() after the previous patch.
With it, a TPG is never freed while a function is still bound to it.
fu->tpg is still only set at link time; the next patch fixes that.
Fixes: c52661d60f63 ("usb-gadget: Initial merge of target module for UASP + BOT")
Cc: stable@vger.kernel.org
Signed-off-by: Slawomir Stepien <sst@poczta.fm>
---
drivers/usb/gadget/function/f_tcm.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
index 110a7d334afa..3949b5c50cc0 100644
--- a/drivers/usb/gadget/function/f_tcm.c
+++ b/drivers/usb/gadget/function/f_tcm.c
@@ -1739,6 +1739,7 @@ static struct se_portal_group *usbg_make_tpg(struct se_wwn *wwn,
}
static int tcm_usbg_drop_nexus(struct usbg_tpg *);
+static int usbg_enable_tpg(struct se_portal_group *se_tpg, bool enable);
static void usbg_drop_tpg(struct se_portal_group *se_tpg)
{
@@ -1747,6 +1748,9 @@ static void usbg_drop_tpg(struct se_portal_group *se_tpg)
unsigned i;
struct f_tcm_opts *opts;
+ if (tpg->gadget_connect)
+ usbg_enable_tpg(se_tpg, false);
+
tcm_usbg_drop_nexus(tpg);
core_tpg_deregister(se_tpg);
destroy_workqueue(tpg->workqueue);
--
2.56.0
next prev parent reply other threads:[~2026-11-23 14:25 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 14:25 [PATCH v2 0/3] usb: gadget: f_tcm: fix TPG lifetime and lock ordering Slawomir Stepien
2026-11-23 14:25 ` [PATCH v2 1/3] usb: gadget: f_tcm: don't hold dep_lock across unregister_gadget_item() Slawomir Stepien
2026-11-23 14:25 ` Slawomir Stepien [this message]
2026-11-23 14:25 ` [PATCH v2 3/3] usb: gadget: f_tcm: take the TPG in tcm_bind() instead of tcm_alloc() Slawomir Stepien
2026-12-03 21:00 ` Greg KH
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=20261123142513.204652-3-sst@poczta.fm \
--to=sst@poczta.fm \
--cc=bigeasy@linutronix.de \
--cc=gregkh@linuxfoundation.org \
--cc=jiashengjiangcool@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=nab@linux-iscsi.org \
--cc=stable@vger.kernel.org \
--cc=syzbot+2799146a53c356f8017d@syzkaller.appspotmail.com \
--cc=yun.zhou@windriver.com \
--cc=zzzccc427@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 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®