From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.74.67.75 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795443935; b=hFEfzWg1h1jG4qt1Qdn82F3EHvEamN8w+E1LOkVFwPlJVMubVZCN/sTO9PlcvHUM6wmS NLdRaJRE6/rkXf8j0HpW6G+hRGRlEYJAfbMEHD47dQKt6wmBOP7KiuHVqeBswS1h0giWQ suLOTjPHpLmyigzoZT5TZIcZLXQSDlzNr9ar0qnm67Ne7DzlznEpiuLKA8rhJNIhiUkqA WI7PRS3rs1x7iei8tTg8NlfYcdWUbC3T100u4HPocHEjRHpi4F41MEDpbfnOhZU5abQOd PkJuOkk+QIs5YrtxfsKCYanG6P8VEH1SCuGupFtLY1b2ekTsNCGjwOmkUJhvWC2XrWA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795443935; h=DMARC-Filter:Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding: DKIM-Signature; bh=7b33Jap8LbKbkO4Xoms8+UwJkMsmtqS+GX5xZmwtFzw=; b=A9ubGSxGC9XeoqVpxv0E0bGV5FBq8SKpkQJv538r/CpFKOZ09nuKFYST6q+H07YXGw14 99KBwrQkAClHgYl3Zz7YpZb+gnib0y5xeFH6H6pc/6PKMk0fv9SVlob59vBMxpz8d3Zf0 38UK+ZpeDc6fB4zCi1sDEvJkvbhQ0gF7O+qOzHCwB2luvX9EhDySA6sKDAmP3CG9rY3Ig eV/4lwcByXRlLv9pqUpvHIjrFsM5swUv5hNA006FfNyUa4AN0/yATgd+CVxOMZzEmgJm+ DiFbORfK1y+VijTwDTlt2lzZrJdX0ptLH0Yubxffmhiqvb/QzMse+Ol60PtogKkWyNQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=poczta.fm; spf=pass smtp.mailfrom=poczta.fm; dkim=pass header.d=poczta.fm header.i=@poczta.fm header.a=rsa-sha256 header.s=dk header.b=eImv0GLz; arc=none smtp.remote-ip=217.74.67.75 Received: from smtpo75.interia.pl (smtpo75.interia.pl [217.74.67.75]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5843C606A31A for ; Mon, 23 Nov 2026 14:25:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5843C606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=poczta.fm Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=poczta.fm Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=poczta.fm header.i=@poczta.fm header.a=rsa-sha256 header.s=dk header.b=eImv0GLz Received: from localhost (unknown [80.68.231.31]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-256) server-digest SHA256) (No client certificate requested) by poczta.interia.pl (INTERIA.PL) with ESMTPSA; Mon, 23 Nov 2026 15:25:30 +0100 (CET) From: Slawomir Stepien To: gregkh@linuxfoundation.org, jiashengjiangcool@gmail.com, yun.zhou@windriver.com, zzzccc427@gmail.com, bigeasy@linutronix.de, nab@linux-iscsi.org Cc: Slawomir Stepien , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+2799146a53c356f8017d@syzkaller.appspotmail.com, stable@vger.kernel.org Subject: [PATCH v2 1/3] usb: gadget: f_tcm: don't hold dep_lock across unregister_gadget_item() Date: Mon, 23 Nov 2026 15:25:11 +0100 Message-ID: <20261123142513.204652-2-sst@poczta.fm> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261123142513.204652-1-sst@poczta.fm> References: <20261123142513.204652-1-sst@poczta.fm> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=poczta.fm; s=dk; t=1795443931; bh=7b33Jap8LbKbkO4Xoms8+UwJkMsmtqS+GX5xZmwtFzw=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=eImv0GLzOVl1GuwgT/+stU7op8F3SlOoXxrYXDhjCnGYRRRp7DTpCYZXKnlK+0mVZ wS3XCZqx0MLNPy7I7sTHo0xK4TQrEIXt6M6cpxOtbNgqF1ZT38a0VHYpeQiZXRYzQO s3reOYiLNW1L6B9nQRHCLKdwX5A69INe/EMeh34Q= tcm_unregister_callback() runs when a TPG is disabled and unbinds the configfs gadget via unregister_gadget_item() while holding opts->dep_lock. unregister_gadget_item() takes gi->lock. The opposite order is taken when a UDC is assigned to the gadget: gadget_dev_desc_UDC_store() holds gi->lock across usb_gadget_register_driver(), which binds the configuration and ends up in tcm_bind(), which takes opts->dep_lock: CPU0: echo 0 > .../tpgt_N/enable CPU1: echo > .../UDC tcm_unregister_callback() gadget_dev_desc_UDC_store() mutex_lock(&opts->dep_lock) mutex_lock(&gi->lock) ... tcm_bind() mutex_lock(&opts->dep_lock) unregister_gadget_item() mutex_lock(&gi->lock) Disabling a TPG while a UDC is being assigned can therefore deadlock. Here dep_lock only needs to cover the can_attach update, so clear can_attach under the lock and call unregister_gadget_item() after dropping it. Once can_attach is false, any later tcm_bind() fails with -ENODEV, and a bind that already passed the check is undone by the unregister that follows. The function item, and with it the gadget item reached via ci_parent, stays alive because the TPG still holds a configfs dependency on it. Fixes: 4bb8548df632 ("usb: gadget: f_tcm: add configfs support") Cc: stable@vger.kernel.org Signed-off-by: Slawomir Stepien --- drivers/usb/gadget/function/f_tcm.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c index 9e6d4f39900a..110a7d334afa 100644 --- a/drivers/usb/gadget/function/f_tcm.c +++ b/drivers/usb/gadget/function/f_tcm.c @@ -2637,10 +2637,11 @@ static void tcm_unregister_callback(struct usb_function_instance *f) struct f_tcm_opts *opts = container_of(f, struct f_tcm_opts, func_inst); mutex_lock(&opts->dep_lock); - unregister_gadget_item(opts-> - func_inst.group.cg_item.ci_parent->ci_parent); opts->can_attach = false; mutex_unlock(&opts->dep_lock); + + /* Takes gi->lock; UDC_store holds gi->lock while tcm_bind() takes dep_lock. */ + unregister_gadget_item(opts->func_inst.group.cg_item.ci_parent->ci_parent); } static int usbg_attach(struct usbg_tpg *tpg) -- 2.56.0