Linux-USB Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] USB: usbfs: check cpu address range before remap
@ 2019-09-04  8:17 Nagarjuna Kristam
  2019-10-04 12:21 ` Greg KH
  0 siblings, 1 reply; 2+ messages in thread
From: Nagarjuna Kristam @ 2019-09-04  8:17 UTC (permalink / raw)
  To: gregkh, stern, dmitry.torokhov
  Cc: linux-tegra, linux-kernel, linux-usb, 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.

Signed-off-by: Nagarjuna Kristam <nkristam@nvidia.com>
---
 drivers/usb/core/devio.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index a945ad7..20999de 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -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);
-- 
2.7.4


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] USB: usbfs: check cpu address range before remap
  2019-09-04  8:17 [PATCH] USB: usbfs: check cpu address range before remap Nagarjuna Kristam
@ 2019-10-04 12:21 ` Greg KH
  0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2019-10-04 12:21 UTC (permalink / raw)
  To: Nagarjuna Kristam
  Cc: stern, dmitry.torokhov, linux-tegra, linux-kernel, linux-usb

On Wed, Sep 04, 2019 at 01:47:31PM +0530, Nagarjuna Kristam wrote:
> 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.
> 
> Signed-off-by: Nagarjuna Kristam <nkristam@nvidia.com>
> ---
>  drivers/usb/core/devio.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
> index a945ad7..20999de 100644
> --- a/drivers/usb/core/devio.c
> +++ b/drivers/usb/core/devio.c
> @@ -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);
> -- 
> 2.7.4
> 

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

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-04  8:17 [PATCH] USB: usbfs: check cpu address range before remap Nagarjuna Kristam
2019-10-04 12:21 ` Greg KH

Linux-USB Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-usb/0 linux-usb/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-usb linux-usb/ https://lore.kernel.org/linux-usb \
		linux-usb@vger.kernel.org
	public-inbox-index linux-usb

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-usb


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git