From: Slawomir Stepien <sst@poczta.fm>
To: Greg KH <gregkh@linuxfoundation.org>
Cc: andrzej.p@samsung.com, jiashengjiangcool@gmail.com,
yun.zhou@windriver.com, zzzccc427@gmail.com,
bigeasy@linutronix.de, nab@linux-iscsi.org,
linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
syzbot+2799146a53c356f8017d@syzkaller.appspotmail.com
Subject: Re: [PATCH 1/3] usb: gadget: f_tcm: take the TPG in tcm_bind() instead of tcm_alloc()
Date: Mon, 23 Nov 2026 10:34:08 +0100 [thread overview]
Message-ID: <awQIkANAqLndeXxy@nr200> (raw)
In-Reply-To: <2026112034-paralegal-democracy-b509@gregkh>
On lis 20, 2026 11:58, Greg KH wrote:
> On Thu, Oct 22, 2026 at 03:49:24PM +0200, Slawomir Stepien wrote:
> > tcm_alloc() runs when a TCM function is symlinked into a gadget
> > configuration. It searches tpg_instances[] for the function instance
> > and copies the TPG pointer into fu->tpg at that point, under
> > tpg_instances_lock. This has two problems.
> >
> > The function and the TPG can be set up in either order. If the
> > function is linked into a configuration before the TPG directory is
> > created, tpg_instances[i].tpg is still NULL and fu->tpg stays NULL.
> > Creating and enabling the TPG afterwards sets can_attach, so
> > tcm_bind() succeeds, and the first USB request that reaches the
> > target code dereferences the NULL fu->tpg.
> >
> > Removing a TPG also leaves can_attach set for configfs instances, so a
> > later bind is not refused even though the TPG it would use is gone.
> >
> > Store the TPG in struct f_tcm_opts instead: set it in usbg_make_tpg()
> > and clear it in usbg_drop_tpg(), under opts->dep_lock. In tcm_bind(),
> > refuse to bind unless both can_attach and opts->tpg are set, and copy
> > opts->tpg into fu->tpg there. Also clear can_attach in usbg_drop_tpg()
> > for configfs instances. The legacy g_target driver sets can_attach once
> > at module load, and its attach callback does not set it again, so
> > leave it alone in that case or g_target could never bind again after a
> > TPG is recreated.
> >
> > With fu->tpg no longer set in tcm_alloc(), the tpg_instances[] lookup
> > there serves no purpose: f->fi already identifies the instance for the
> > item's whole lifetime. Drop it, and with it the tpg_instances_lock
> > acquisition. config_usb_cfg_link() calls tcm_alloc() with gi->lock
> > held, so this also removes the gi->lock -> tpg_instances_lock edge
> > from the following lockdep report:
> >
> > WARNING: possible circular locking dependency detected
> > task is trying to acquire lock:
> > (tpg_instances_lock){+.+.}-{4:4}, at: tcm_alloc
> > but task is already holding lock:
> > (&gi->lock){+.+.}-{4:4}, at: config_usb_cfg_link
> >
> > the existing dependency chain (in reverse order) is:
> > -> #2 (&gi->lock):
> > config_usb_cfg_link
> > configfs_symlink
> > -> #1 (&sb->s_type->i_mutex_key#24):
> > configfs_depend_item_unlocked
> > usbg_make_tpg
> > target_fabric_make_tpg
> > configfs_mkdir
> > -> #0 (tpg_instances_lock):
> > tcm_alloc
> > usb_get_function
> > config_usb_cfg_link
> > configfs_symlink
> >
> > Fixes: 4bb8548df632 ("usb: gadget: f_tcm: add configfs support")
> > Reported-by: syzbot+2799146a53c356f8017d@syzkaller.appspotmail.com
> > Closes: https://syzkaller.appspot.com/bug?extid=2799146a53c356f8017d
> > Link: https://syzkaller.appspot.com/ai_job?id=51e1f189-6d91-4321-a6ba-07d4eb3cd8cd
> > Assisted-by: Gemini:gemini-4-argon Gemini:gemini-3.8-flash syzbot
> > Signed-off-by: Slawomir Stepien <sst@poczta.fm>
> > ---
> > drivers/usb/gadget/function/f_tcm.c | 29 +++++++++++------------------
> > drivers/usb/gadget/function/u_tcm.h | 4 ++++
> > 2 files changed, 15 insertions(+), 18 deletions(-)
> >
> > diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c
> > index 9e6d4f39900a..cc75d7e9792a 100644
> > --- a/drivers/usb/gadget/function/f_tcm.c
> > +++ b/drivers/usb/gadget/function/f_tcm.c
> > @@ -1720,6 +1720,9 @@ static struct se_portal_group *usbg_make_tpg(struct se_wwn *wwn,
> >
> > tpg_instances[i].tpg = tpg;
> > tpg->fi = tpg_instances[i].func_inst;
> > + mutex_lock(&opts->dep_lock);
> > + opts->tpg = tpg;
> > + mutex_unlock(&opts->dep_lock);
>
> Why is a lock needed to write one value?
Yeah it is not needed. I will drop it in v2 - can_attach (tcm_bind()) and tpg_instances_lock
(usbg_drop_tpg()) protect it.
I guess AI added it since in all the other places it is used.
> > mutex_unlock(&tpg_instances_lock);
> > return &tpg->se_tpg;
> >
> > @@ -1760,11 +1763,14 @@ static void usbg_drop_tpg(struct se_portal_group *se_tpg)
> > opts = container_of(tpg_instances[i].func_inst,
> > struct f_tcm_opts, func_inst);
> > mutex_lock(&opts->dep_lock);
> > - if (opts->has_dep)
> > + opts->tpg = NULL;
> > + if (opts->has_dep) {
> > module_put(opts->dependent);
>
> The whole module_put() logic should probably just be removed, right?
I'm not sure what you mean here. This series does not touch the legacy path.
Are you suggesting that we do not have legacy users and this whole legacy stuff can go?
> > - else
> > + } else {
> > + opts->can_attach = false;
> > configfs_undepend_item_unlocked(
> > &opts->func_inst.group.cg_item);
> > + }
> > mutex_unlock(&opts->dep_lock);
> > }
> > mutex_unlock(&tpg_instances_lock);
> > @@ -2300,10 +2306,11 @@ static int tcm_bind(struct usb_configuration *c, struct usb_function *f)
> > opts = container_of(f->fi, struct f_tcm_opts, func_inst);
> >
> > mutex_lock(&opts->dep_lock);
> > - if (!opts->can_attach) {
> > + if (!opts->can_attach || !opts->tpg) {
> > mutex_unlock(&opts->dep_lock);
> > return -ENODEV;
> > }
> > + fu->tpg = opts->tpg;
> > mutex_unlock(&opts->dep_lock);
>
> But opts->tpg could be changed now, but you saved it off? No reference
> counting at all? That feels very wrong.
You are right. I will change the order, so the patch 2 will be before this one and then the tpg
won't change while the function is bound.
Thanks!
--
Slawomir Stepien
next prev parent reply other threads:[~2026-11-23 9:34 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-22 13:49 Slawomir Stepien
2026-10-22 13:49 ` [PATCH 2/3] usb: gadget: f_tcm: detach gadget when dropping an enabled TPG Slawomir Stepien
2026-10-22 13:49 ` [PATCH 3/3] usb: gadget: f_tcm: don't hold dep_lock across unregister_gadget_item() Slawomir Stepien
2026-11-20 10:58 ` [PATCH 1/3] usb: gadget: f_tcm: take the TPG in tcm_bind() instead of tcm_alloc() Greg KH
2026-11-23 9:34 ` Slawomir Stepien [this message]
2026-11-23 12:18 ` 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=awQIkANAqLndeXxy@nr200 \
--to=sst@poczta.fm \
--cc=andrzej.p@samsung.com \
--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=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®