Skip to contentSkip to conversation

usb: devio: fix mmap() on non-coherent DMA architectures

PATCH In review [email protected] 2 participants 3 messages Aug 1, 2019 – Aug 5, 2019 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: devio: fix mmap() on non-coherent DMA architectures

From: Gavin Li <[email protected]>

On architectures that are not (or are optionally) DMA coherent, dma_alloc_coherent() returns an address into the vmalloc space, and calling virt_to_phys() on this address returns an unusable physical address.

This patch replaces the raw remap_pfn_range() call with a call to dmap_mmap_coherent(), which takes care of the differences between coherent and non-coherent code paths.

Tested on an arm64 rk3399 board.

---
 drivers/usb/core/devio.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/usb/core/devio.c +3 -4
index a02448105527..76ec9aef3eff 100644
@@ -241,11 +241,10 @@ static int usbdev_mmap(struct file *file, struct vm_area_struct *vma)
 	usbm->vma_use_count = 1;
 	INIT_LIST_HEAD(&usbm->memlist);
 
-	if (remap_pfn_range(vma, vma->vm_start,
-			virt_to_phys(usbm->mem) >> PAGE_SHIFT,
-			size, vma->vm_page_prot) < 0) {
+	ret = dma_mmap_coherent(ps->dev->bus->sysdev, vma, mem, dma_handle, size);
+	if (ret) {
 		dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
-		return -EAGAIN;
+		return ret;
 	}
 
 	vma->vm_flags |= VM_IO;
Signature · 1 line
2.22.0
#1 lore raw

From: [email protected]

Quoted 6 lines

Sent: 01 August 2019 23:02

On architectures that are not (or are optionally) DMA coherent, dma_alloc_coherent() returns an address into the vmalloc space, and calling virt_to_phys() on this address returns an unusable physical address.

So? what is the code trying to use the return value of virt_to_phys() for?

The 'cpu physical address' isn't (usually) a very interesting number. The value you normally want is the address the hardware should use in order to access the memory - this isn't (in general) the same value. (It might be different for different devices.)

ISTR that dma_alloc_coherent() returns this value to the caller.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

author
#2 lore raw

The purpose of this section of code is to map that memory into userspace, and the code before this patch would incorrectly calculate the pfn required to do so. This patch simply changes it to use the correct function to do so rather than doing it from scratch.

Quoted 24 lines · David Laight

From: [email protected]

Quoted 6 lines

Sent: 01 August 2019 23:02

On architectures that are not (or are optionally) DMA coherent, dma_alloc_coherent() returns an address into the vmalloc space, and calling virt_to_phys() on this address returns an unusable physical address.

So? what is the code trying to use the return value of virt_to_phys() for?

The 'cpu physical address' isn't (usually) a very interesting number. The value you normally want is the address the hardware should use in order to access the memory - this isn't (in general) the same value. (It might be different for different devices.)

ISTR that dma_alloc_coherent() returns this value to the caller.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)