All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/1] usb: gadget: f_fs: Fix synchronous read() behavior
@ 2026-11-20 14:59 Ingo Rohloff
  2026-11-20 14:59 ` [PATCH v2 1/1] " Ingo Rohloff
  0 siblings, 1 reply; 3+ messages in thread
From: Ingo Rohloff @ 2026-11-20 14:59 UTC (permalink / raw)
  To: gregkh
  Cc: viro, nkapron, me, nuno.sa, michael.bommarito, prostitisgabriel,
	kees, linux-usb, Ingo Rohloff

This 2nd version just changes the commit message.
I agree that the 1st varsion failed to explain the problem correctly.

Unfortunately **depending on the hardware** this patch MIGHT change
behavior for userspace, which is a no-no.
BUT: The behavior for userspace is already dependent on the hardware,
without the patch, so I don't see that as a blocker.

What the patch does is to make sure that userspace always gets
the same (correct?) behavior regardless of hardware.

There still is a border case, what to do with ZLPs (Zero Length Packets):
Userspace read() requests a multiple of MaxPacketSize and USB Host sends
exactly the length userspace requests followed by a ZLP.
It's unclear what to do with this extra ZLP.
OTOH: I already have seen many USB Host controllers, which don't handle
Zero Length Packets correctly, so ZLPs should anyway be avoided at all
costs.

Note: I really would like to get rid of "quirk_ep_out_aligned_size" in
"struct usb_gadget" completely.
But this flag is also used in "drivers/usb/gadget/function/u_ether.c",
and I have no clue about this code and no way to test it.

---
Changes in v2:
- Redo the commit message to try to explain the problem properly.
  Unfortunately this is now a wall of text, but the problem is complicted.

Link to v1: https://lore.kernel.org/linux-usb/20260903153737.58087-1-ingo.rohloff@lauterbach.com/

---
Ingo Rohloff (1):
  usb: gadget: f_fs: Fix synchronous read() behavior

 drivers/usb/gadget/function/f_fs.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

--
2.55.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 1/1] usb: gadget: f_fs: Fix synchronous read() behavior
  2026-11-20 14:59 [PATCH v2 0/1] usb: gadget: f_fs: Fix synchronous read() behavior Ingo Rohloff
@ 2026-11-20 14:59 ` Ingo Rohloff
  2026-11-20 15:09   ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Ingo Rohloff @ 2026-11-20 14:59 UTC (permalink / raw)
  To: gregkh
  Cc: viro, nkapron, me, nuno.sa, michael.bommarito, prostitisgabriel,
	kees, linux-usb, Ingo Rohloff

Some UDCs (USB Device Controllers) require, that USB OUT requests (host ->
device), have a size which is a multiple of the USB OUT Endpoint
MaxPacketSize.

If this is the case, then the flag "quirk_ep_out_aligned_size" in
"struct usb_gadget" is set.

The current implementation in "f_fs.c" for synchronous reads, behaves
differently for UDCs which have this flag set to UDCs which have this flag
NOT set, because ffs_epfile_io() uses usb_ep_align_maybe() and this
function then aligns the request size if the flag is set.

Example:
Userspace executes a read() with 128 bytes.  For a HighSpeed OUT transfer
(MaxPacketSize == 512), usb_ep_align_maybe() transforms this into a 512
byte request if quirk_ep_out_aligned_size is set.

The Host sends an OUT transfer with 264 bytes (136 bytes more than
userspace requested).  These extra 136 bytes are buffered in
"__ffs_epfile_read_data()" and returned for the next userspace read via
"__ffs_epfile_read_buffered()"

If "quirk_ep_out_aligned_size" is NOT set, then "ffs_epfile_io()" passes
the userspace read without modification to the UDC; so in this case the
UDC starts a 128 byte OUT request.

When the Host now sends an OUT transfer with 264 bytes, it's unclear what
happens, because this completely depends on the behavior of the UDC and
the UDC driver implementation.
Likely the extra data received from the Host gets just thrown away;
that's for example what the DWC3 UDC driver does.

So the behavior of an UDC, which sets "quirk_ep_out_aligned_size" is
different from the behavior of an UDC, which doesn't set this flag.

The first behavior is what userspace expects (no data should get thrown
away). This patch enforces the first behavior (code buffers extra data),
by **always** aligning the USB OUT request size to the Endpoints
MaxPacketSize.

Signed-off-by: Ingo Rohloff <ingo.rohloff@lauterbach.com>
---
 drivers/usb/gadget/function/f_fs.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index c64a268e98a4..53c92d149845 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -1078,11 +1078,12 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
 		}
 		data_len = iov_iter_count(&io_data->data);
 		/*
-		 * Controller may require buffer size to be aligned to
-		 * maxpacketsize of an out endpoint.
+		 * To expose a stream like interface for read(), make sure
+		 * the enqueued read request is a multiple of the maxpacketsize
+		 * of an out endpoint.
 		 */
 		if (io_data->read)
-			data_len = usb_ep_align_maybe(gadget, ep->ep, data_len);
+			data_len = usb_ep_align(ep->ep, data_len);

 		io_data->use_sg = gadget->sg_supported && data_len > PAGE_SIZE;
 		spin_unlock_irq(&epfile->ffs->eps_lock);
--
2.55.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 1/1] usb: gadget: f_fs: Fix synchronous read() behavior
  2026-11-20 14:59 ` [PATCH v2 1/1] " Ingo Rohloff
@ 2026-11-20 15:09   ` sashiko-bot
  0 siblings, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-20 15:09 UTC (permalink / raw)
  To: Ingo Rohloff; +Cc: linux-usb

> Some UDCs (USB Device Controllers) require, that USB OUT requests (host ->
> device), have a size which is a multiple of the USB OUT Endpoint
> MaxPacketSize.
> 
> If this is the case, then the flag "quirk_ep_out_aligned_size" in
> "struct usb_gadget" is set.
> 
> The current implementation in "f_fs.c" for synchronous reads, behaves
> differently for UDCs which have this flag set to UDCs which have this flag
> NOT set, because ffs_epfile_io() uses usb_ep_align_maybe() and this
> function then aligns the request size if the flag is set.
> 
> Example:
> Userspace executes a read() with 128 bytes.  For a HighSpeed OUT transfer
> (MaxPacketSize == 512), usb_ep_align_maybe() transforms this into a 512
> [ ... ]
> away). This patch enforces the first behavior (code buffers extra data),
> by **always** aligning the USB OUT request size to the Endpoints
> MaxPacketSize.
> 
> Signed-off-by: Ingo Rohloff <ingo.rohloff@lauterbach.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120145937.39772-1-ingo.rohloff@lauterbach.com?part=1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-20 15:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 14:59 [PATCH v2 0/1] usb: gadget: f_fs: Fix synchronous read() behavior Ingo Rohloff
2026-11-20 14:59 ` [PATCH v2 1/1] " Ingo Rohloff
2026-11-20 15:09   ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.