git: u4b: Do not assume page-aligned busdma segments in the DMA page table
Aaron LI
aly at crater.dragonflybsd.org
Wed Nov 18 05:12:57 PST 2026
commit caceac66aca06ff7a052ee179993e5ec55ccf8ae
Author: David Bidner <david at crap.solutions>
Date: Mon Nov 2 16:36:38 2026 +0100
u4b: Do not assume page-aligned busdma segments in the DMA page table
usb_pc_common_mem_cb() walked the busdma segment list assuming every
segment after the first begins on a page boundary (rem reset to 0 and
the next entry always taken from the new segment's base address).
busdma splits segments at physical page boundaries, at physical
discontinuities and at maxsegsz; the first two begin a new segment on
a page boundary, but when a buffer starts mid-page and consecutive
pages are physically adjacent, the maxsegsz split ends a segment in
the middle of a page and its continuation begins mid-page. When that
happened, the walk recorded the continuation's frame a second time
under the next page index, shifting every later entry by one page.
usbd_get_page() then handed the host controller wrong bus addresses
for all pages after the duplicated entry, so multi-page USB storage
reads returned the content of the following page and left the tail of
the transfer with unrelated memory. This needs a buffer whose first
byte is not page aligned together with such a mid-page split, which is
why 1 KiB and 2 KiB block filesystems on USB storage were affected
while page-aligned buffers and single page transfers were not.
Index the page table by buffer byte position instead: entry j is the
frame containing buffer byte j * USB_PAGE_SIZE - rem, located in
whichever segment covers it. The rem adjustment in usbd_get_page()
already matches this layout.
FreeBSD's usb_busdma.c has the same walk.
Verified in QEMU with usb-storage: ext2 1 KiB, 2 KiB and 4 KiB
block and UFS reads, ext2 1 KiB reads over UHCI, raw device reads and
writes with sub-page buffers, and write-heavy remount cycles; the
corrupt remount reads are gone. On real xHCI hardware with a USB
stick (ext2 1 KiB), the corrupt remount reads occur with the old walk
and not with this one.
Bug: https://bugs.dragonflybsd.org/issues/3460
Summary of changes:
sys/bus/u4b/usb_busdma.c | 33 +++++++++++++++++++++------------
1 file changed, 21 insertions(+), 12 deletions(-)
http://gitweb.dragonflybsd.org/dragonfly.git/commitdiff/caceac66aca06ff7a052ee179993e5ec55ccf8ae
--
DragonFly BSD source repository
More information about the Commits
mailing list