Skip to content

Commit 2bef9ae

Browse files
jlintonarmgregkh
authored andcommitted
usb: usbfs: correct kernel->user page attribute mismatch
On some architectures (e.g. arm64) requests for IO coherent memory may use non-cachable attributes if the relevant device isn't cache coherent. If these pages are then remapped into userspace as cacheable, they may not be coherent with the non-cacheable mappings. In particular this happens with libusb, when it attempts to create zero-copy buffers for use by rtl-sdr (https://github.com/osmocom/rtl-sdr/). On low end arm devices with non-coherent USB ports, the application will be unexpectedly killed, while continuing to work fine on arm machines with coherent USB controllers. This bug has been discovered/reported a few times over the last few years. In the case of rtl-sdr a compile time option to enable/disable zero copy was implemented to work around it. Rather than relaying on application specific workarounds, dma_mmap_coherent() can be used instead of remap_pfn_range(). The page cache/etc attributes will then be correctly set in userspace to match the kernel mapping. Signed-off-by: Jeremy Linton <[email protected]> Cc: stable <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent e283f5e commit 2bef9ae

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

drivers/usb/core/devio.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ static int usbdev_mmap(struct file *file, struct vm_area_struct *vma)
217217
{
218218
struct usb_memory *usbm = NULL;
219219
struct usb_dev_state *ps = file->private_data;
220+
struct usb_hcd *hcd = bus_to_hcd(ps->dev->bus);
220221
size_t size = vma->vm_end - vma->vm_start;
221222
void *mem;
222223
unsigned long flags;
@@ -250,9 +251,7 @@ static int usbdev_mmap(struct file *file, struct vm_area_struct *vma)
250251
usbm->vma_use_count = 1;
251252
INIT_LIST_HEAD(&usbm->memlist);
252253

253-
if (remap_pfn_range(vma, vma->vm_start,
254-
virt_to_phys(usbm->mem) >> PAGE_SHIFT,
255-
size, vma->vm_page_prot) < 0) {
254+
if (dma_mmap_coherent(hcd->self.sysdev, vma, mem, dma_handle, size)) {
256255
dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
257256
return -EAGAIN;
258257
}

0 commit comments

Comments
 (0)