Skip to contentSkip to conversation

usb: musb: host: don't add the sg offset twice in PIO TX

PATCH New Dmitry Dagunts 2 participants 2 messages Nov 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: musb: host: don't add the sg offset twice in PIO TX

A PIO OUT URB without a transfer_buffer gets its first packet through a sg_mapping_iter. sg_miter.addr already includes the offset of the sg entry, but musb_ep_program() adds urb->sg->offset again. With a nonzero offset the first packet is read from the wrong address: wrong data is sent, or the read faults if that address is past the mapped page. The packets that follow are loaded by musb_host_tx(), which uses sg_miter.addr as it is.

With usb-storage this needs CONFIG_HIGHMEM, because usb_sg_init() leaves transfer_buffer NULL only for highmem pages, and a sub-page write. The highmem check in usb-storage does not catch it, as musb sets HCD_DMA even if it is built PIO only.

Drop urb->actual_length too: for bulk and interrupt URBs it is always 0 if buf is NULL.

Seen as an oops when writing to a USB stick on an LG TV (MediaTek MT5398, 6.12.119, MUSB_PIO_ONLY, out-of-tree musb glue). Tested there with this change and a debug switch that selects the old address, using O_DIRECT writes of 512 to 4096 bytes from highmem, read back from the stick:

  sg offset            old address               new address
  0                    correct                   correct
  0x200, 0x400, 0x600  first 512 bytes wrong     correct
  0x800, 0xe00         not mapped (probed only)  correct

Only compile-tested on the current tree.

Claude Code (Claude Opus 5.5) was used to diagnose the oops, to write and run the test, and to write this fix and changelog.

Fixes 8e8a55165469 ("usb: musb: host: Handle highmem in PIO mode")
---
 drivers/usb/musb/musb_host.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/usb/musb/musb_host.c +1 -2
index 31b061248..6f78fd985 100644
@@ -821,8 +821,7 @@ static void musb_ep_program(struct musb *musb, u8 epnum,
 					sg_miter_stop(&qh->sg_miter);
 					goto finish;
 				}
-				buf = qh->sg_miter.addr + urb->sg->offset +
-					urb->actual_length;
+				buf = qh->sg_miter.addr;
 				load_count = min_t(u32, load_count,
 						qh->sg_miter.length);
 				musb_write_fifo(hw_ep, load_count, buf);

base-commit: 730acad0b83da0d7b658d99cf87ef979012ec638

Signature · 1 line
2.47.3
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 13 lines

A PIO OUT URB without a transfer_buffer gets its first packet through a sg_mapping_iter. sg_miter.addr already includes the offset of the sg entry, but musb_ep_program() adds urb->sg->offset again. With a nonzero offset the first packet is read from the wrong address: wrong data is sent, or the read faults if that address is past the mapped page. The packets that follow are loaded by musb_host_tx(), which uses sg_miter.addr as it is.

With usb-storage this needs CONFIG_HIGHMEM, because usb_sg_init() leaves transfer_buffer NULL only for highmem pages, and a sub-page write. The highmem check in usb-storage does not catch it, as musb sets HCD_DMA even if it is built PIO only.

Quoted 8 lines

Drop urb->actual_length too: for bulk and interrupt URBs it is always 0 if buf is NULL.
[ ... ]
run the test, and to write this fix and changelog.

Fixes 8e8a55165469 ("usb: musb: host: Handle highmem in PIO mode")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1