mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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
Subject: [PATCH v2 0/3] usb: gadget: f_tcm: fix TPG lifetime and lock ordering
Date: Mon, 23 Nov 2026 15:25:10 +0100	[thread overview]
Message-ID: <20261123142513.204652-1-sst@poczta.fm> (raw)

This series fixes three f_tcm bugs. All of them come from the gadget
and the target configfs trees being set up and torn down independently:

1. Disabling a TPG while a UDC is being assigned can deadlock on
   dep_lock vs gi->lock.
2. Removing an enabled TPG while the gadget is bound frees the TPG
   under the bound function (use-after-free).
3. fu->tpg is copied when the function is linked, so it is NULL if the
   TPG is created later and stale after the TPG is recreated. Taking
   tpg_instances_lock there also causes the syzbot lockdep report.

Patch 2 makes sure a TPG is never freed while a function is bound to
it. Patch 3 relies on that: fu->tpg is taken in tcm_bind() and cannot
change until tcm_unbind(), so it needs no extra locking or reference
counting.

Tested with dummy_hcd, KASAN and lockdep: linking the function before
the TPG exists, rmdir of an enabled and bound TPG, recreating the TPG
and rebinding, enable/disable racing with UDC writes, and the legacy
g_target. Without the respective patch these hit a NULL dereference, a
use-after-free and the deadlock.

Not addressed here: usbg_make_tpg() calls configfs_depend_item_unlocked()
under tpg_instances_lock, which gives a separate lockdep report
(su_mutex -> tpg_instances_lock -> configfs inode lock). It is already
there on v7.3 and is not changed by this series. Since it turns lockdep
off, I used a local hack (lockdep_off()/lockdep_on()) to avoid it
during testing.

AI assistance: patch 3 was first drafted with Gemini (gemini-4-argon,
gemini-3.8-flash) in the syzbot AI job linked from it. Patches 1 and 2
were written with Claude (claude-opus-5-5), which I also used to rework
patch 3, update the changelogs, draft this cover letter and run the
tests above (virtme-ng, dummy_hcd, KASAN, lockdep).

Changes in v2:
- Reorder: the dep_lock deadlock fix and the detach-on-drop fix come
  first, and the tcm_bind() patch is last, so a TPG can no longer
  change or go away under a bound function (Greg).
- Patch 3: drop the dep_lock around the opts->tpg writes (Greg), the
  can_attach reset in usbg_drop_tpg() and the !opts->tpg check in
  tcm_bind(). They were only needed because of the old order. Document
  why opts->tpg cannot change while the function is bound.
- Patch 2: note that it relies on patch 1, and that patch 3 fixes the
  remaining stale fu->tpg.

v1: https://lore.kernel.org/all/20261022134926.1125473-1-sst@poczta.fm/

Slawomir Stepien (3):
  usb: gadget: f_tcm: don't hold dep_lock across
    unregister_gadget_item()
  usb: gadget: f_tcm: detach gadget when dropping an enabled TPG
  usb: gadget: f_tcm: take the TPG in tcm_bind() instead of tcm_alloc()

 drivers/usb/gadget/function/f_tcm.c | 32 ++++++++++++++---------------
 drivers/usb/gadget/function/u_tcm.h |  6 ++++++
 2 files changed, 21 insertions(+), 17 deletions(-)


base-commit: c03c1a14507dfaf8b856c2608f9ac216021fc222
-- 
2.56.0


             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 Slawomir Stepien [this message]
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 ` [PATCH v2 2/3] usb: gadget: f_tcm: detach gadget when dropping an enabled TPG Slawomir Stepien
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-1-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=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®