From nobody Sat Jul 25 05:59:23 2026 Received: from mail-oa1-f72.google.com (mail-oa1-f72.google.com [209.85.160.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7788C3655C4 for ; Fri, 24 Jul 2026 20:41:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.72 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784925698; cv=none; b=VafQ+/qgKLjwFmKNKPHge07uZTcNSleVb/HLo4/4HMSarUjiViQLLp5zt9jTpjXk0xJBn/2F278mW5gl2vASn1+J4Y/AtqhFkO7QcOUlWUIYGHb2Osec+1og4CMYqXH5SP0F++/ynU8UexWlOzV355WGydt6GTWt38/8eLQVdZ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784925698; c=relaxed/simple; bh=PzbXWJQs72R9iV99XX9jksyQUQ+nRv5EhQapM0F3FKw=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=QsTJ+0LddROTOKFcyh29I9KCuk4rZhJSbno9xzZZeDTXegCsTTcCwgQcj5RNihxBJePIQXGpkVUiNx1qtYF6YVpVSJ12+KU/PgE5+3Mklh4HoiaaXwvWq8qdoTw0lTG3uqItsUR+pW+peBJrnTviH0N11NwyQDBUCSZGXQX3HCY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--nkapron.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=TV3dIdxy; arc=none smtp.client-ip=209.85.160.72 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--nkapron.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TV3dIdxy" Received: by mail-oa1-f72.google.com with SMTP id 586e51a60fabf-43cd2d12617so821390fac.3 for ; Fri, 24 Jul 2026 13:41:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784925695; x=1785530495; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Iajl6b3K8rXMmm9gJwdCmJTZEBGl8q0xqBBTV9E/nkk=; b=TV3dIdxyMzImq0rmOMOVfzx7v095aWPcmXdttXdbYawpoHLU1JpU7c0MWRAqQGNkFj 83yKEbnKgJAW8D7MajYhV/hXqAkQm3QtkMEC8E+hn8Z0Ie6vUYQjpY9RdrBvuEX+Ujav BCplSPd7eL1DDSWxdUUoBMA+D++DaYYqVVMnRi0BSBmVEnTRMPiwSaiUKhdDLztnxnDj apvHnN9oByb3ZVJohnjZ0UfgHgKaKYU6u/GfPCSyHCDsWlbFo3B9dk305hPqQhDN7F9W 559F5qJb8PLA4fpUxBaJItvpdQZwrJwRboS/2d1C3gYr4LQdVsZ4tyMkonsJxdyXcc6B Gliw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784925695; x=1785530495; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Iajl6b3K8rXMmm9gJwdCmJTZEBGl8q0xqBBTV9E/nkk=; b=IPq4Onvk40mthpmVUdij2HgVv2G2+78uQ+X+pYHYFflKzwUVXEsTVc1qnMquWNUGSr OIDKlIX4alr38VQo/XEc0nxIvTFi2E5Ob5WPkuuVEM35Ju6jG7fDDqs5W58zuIPzrS/Q /BtOCY/J5zMd9Z6ij8AL/k4rnh6I1SQShozdINmtqbuLEWgVthBP72/AGZ8e2yLKmU/j RepqsGoxNCXXNPX4rY+oVlS8lFsFwG8UC4oM/mxOh4UcpW0hBGIabwORWX6luDiaSsdN qSTxw91Zo3rqO2ahlPy0U2j0BrwY/b3IUGD2lFYvJgVouf1W2zy/SbKTQj7OKk/tzPRT GHew== X-Forwarded-Encrypted: i=1; AHgh+RqVnZMyRn3IUJtDMO8qw0/L7MHnRkG+8xjYEmpZiRsbp6AFw1kUd1ZMWzRmEyt8V8eeJUxvMLE9TclWIUU=@vger.kernel.org X-Gm-Message-State: AOJu0YxvUVRLWRG/Xynlhr5WLZbfkHgSopYJnx2jGXdf+k4aPtnnxNjM fd2lcfCvWyC2y+/CuigpolFy9JZ6Q064KCQEXbayx1U7nEmEQZwliH0mkORh8aCSUSCCjP4X11V zW3oKG8N0yw== X-Received: from jaqt21.prod.google.com ([2002:a05:6638:1f5:b0:5e8:f77c:fa88]) (user=nkapron job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:180a:b0:497:cd5c:7f0b with SMTP id 5614622812f47-4ab6a1bbb09mr2339b6e.30.1784925694976; Fri, 24 Jul 2026 13:41:34 -0700 (PDT) Date: Fri, 24 Jul 2026 20:41:16 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.55.0.229.g6434b31f56-goog Message-ID: <20260724204117.4036015-1-nkapron@google.com> Subject: [PATCH] usb: gadget: f_fs: Prevent deadlock during ep0 read loop From: Neill Kapron To: Greg Kroah-Hartman , Michal Nazarewicz Cc: nkapron@google.com, kernel-team@android.com, stable@vger.kernel.org, Greg Kroah-Hartman , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, ffs_ep0_read() holds ffs->mutex when it prepares to go to sleep waiting for an event. When no setup events are pending, it calls wait_event_interruptible_exclusive_locked_irq() with the mutex still held. The wait macro deliberately drops the waitqueue spinlock before sleeping but does not drop the mutex. If a userspace daemon is polling ep0 via read() and the gadget is asynchronously torn down via configfs (e.g., echo "" > UDC), a deadlock can occur: 1. The configfs teardown calls functionfs_unbind(), which queues a FUNCTIONFS_UNBIND event. 2. The daemon wakes up, consumes the event, and drops the mutex. 3. However, if the daemon loops and immediately issues another read() before exiting, it reacquires ffs->mutex and again goes into an interruptible sleep. 4. Meanwhile, functionfs_unbind() continues execution and attempts to acquire ffs->mutex to tear down ep0req. 5. The kernel deadlocks because the configfs thread is stuck in an uninterruptible sleep waiting for the mutex, while the userspace daemon is in an interruptible sleep holding the mutex forever because no more events will arrive. To fix this, we drop both the waitqueue spinlock and ffs->mutex before going to sleep, and use wait_event_interruptible_exclusive() instead. Upon waking up, we jump back to the `retry` label to safely reacquire the mutex and re-evaluate the state machine. By not sleeping with ffs->mutex held, we natively decouple gadget teardowns (which require the mutex) from userspace polling. Fixes: ddf8abd25994 ("USB: f_fs: the FunctionFS driver") Cc: stable@vger.kernel.org Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Neill Kapron --- drivers/usb/gadget/function/f_fs.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/functi= on/f_fs.c index 073c4cbd90fb..cfa05266e551 100644 --- a/drivers/usb/gadget/function/f_fs.c +++ b/drivers/usb/gadget/function/f_fs.c @@ -551,6 +551,7 @@ static ssize_t ffs_ep0_read(struct file *file, char __u= ser *buf, if (ffs_setup_state_clear_cancelled(ffs) =3D=3D FFS_SETUP_CANCELLED) return -EIDRM; =20 +retry: /* Acquire mutex */ ret =3D ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); if (ret < 0) @@ -585,10 +586,15 @@ static ssize_t ffs_ep0_read(struct file *file, char _= _user *buf, break; } =20 - if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq, - ffs->ev.count)) { - ret =3D -EINTR; - break; + if (!ffs->ev.count) { + spin_unlock_irq(&ffs->ev.waitq.lock); + mutex_unlock(&ffs->mutex); + + if (wait_event_interruptible_exclusive(ffs->ev.waitq, + ffs->ev.count)) + return -EINTR; + + goto retry; } =20 /* unlocks spinlock */ --=20 2.55.0.229.g6434b31f56-goog