Skip to contentSkip to conversation

USB: usbfs: check cpu address range before remap

PATCH In review Nagarjuna Kristam 2 participants 2 messages Sep 4, 2019 – Oct 4, 2019

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

PATCH 1/1 USB: usbfs: check cpu address range before remap

In usbfs mmap, usb_alloc_coherent is used to allocate memory. This Memory is then remapped to user space memory using remap_pfn_range. remap_pfn_range needs phy address of memory, for which virt_to_phy API is used. This API works only if memory is allocated using kmalloc. But usb_alloc_coherent can allocate memory in vmalloc range and this causes wrong remap of user memory.

Check if address returned by usb_alloc_coherent failed in vmalloc range and if yes, use remap_vmalloc_range API, else use remap_pfn_range to remap address to user space.

---
 drivers/usb/core/devio.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/usb/core/devio.c +6 -1
index a945ad7..20999de 100644
@@ -250,7 +250,12 @@ 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,
+	if (is_vmalloc_addr(usbm->mem)) {
+		if (remap_vmalloc_range(vma, usbm->mem, 0) < 0) {
+			dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
+			return -EAGAIN;
+		}
+	} else if (remap_pfn_range(vma, vma->vm_start,
 			virt_to_phys(usbm->mem) >> PAGE_SHIFT,
 			size, vma->vm_page_prot) < 0) {
 		dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
Signature · 1 line
2.7.4
maintainer
#1 lore raw
Quoted 37 lines · Nagarjuna Kristam

In usbfs mmap, usb_alloc_coherent is used to allocate memory. This Memory is then remapped to user space memory using remap_pfn_range. remap_pfn_range needs phy address of memory, for which virt_to_phy API is used. This API works only if memory is allocated using kmalloc. But usb_alloc_coherent can allocate memory in vmalloc range and this causes wrong remap of user memory.

Check if address returned by usb_alloc_coherent failed in vmalloc range and if yes, use remap_vmalloc_range API, else use remap_pfn_range to remap address to user space.

---
 drivers/usb/core/devio.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/usb/core/devio.c +6 -1
index a945ad7..20999de 100644
@@ -250,7 +250,12 @@ 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,
+	if (is_vmalloc_addr(usbm->mem)) {
+		if (remap_vmalloc_range(vma, usbm->mem, 0) < 0) {
+			dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
+			return -EAGAIN;
+		}
+	} else if (remap_pfn_range(vma, vma->vm_start,
 			virt_to_phys(usbm->mem) >> PAGE_SHIFT,
 			size, vma->vm_page_prot) < 0) {
 		dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
Signature · 1 line
2.7.4

With the recent dma changes, is this patch still needed and correct?

thanks,

greg k-h