Skip to content

Commit 02683ff

Browse files
Andrew G. Harveygregkh
authored andcommitted
UIO: Fix mapping of logical and virtual memory
mmap() doesn't work as expected for UIO_MEM_LOGICAL or UIO_MEM_VIRTUAL mappings. The offset into the memory needs to be added, otherwise uio_vma_fault always returns the first page only. Note that for UIO userspace calls mmap() with offset = N * getpagesize() to access mapping N. This must be compensated when calculating the offset. A comment was added to explain this since it is not obvious. Signed-off-by: Andrew G. Harvey <agh@cisco.com> Signed-off-by: Hans J. Koch <hjk@linutronix.de> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent a6030fc commit 02683ff

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

‎drivers/uio/uio.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,15 +490,23 @@ static int uio_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
490490
{
491491
struct uio_device *idev = vma->vm_private_data;
492492
struct page *page;
493+
unsigned long offset;
493494

494495
int mi = uio_find_mem_index(vma);
495496
if (mi < 0)
496497
return VM_FAULT_SIGBUS;
497498

499+
/*
500+
* We need to subtract mi because userspace uses offset = N*PAGE_SIZE
501+
* to use mem[N].
502+
*/
503+
offset = (vmf->pgoff - mi) << PAGE_SHIFT;
504+
498505
if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
499-
page = virt_to_page(idev->info->mem[mi].addr);
506+
page = virt_to_page(idev->info->mem[mi].addr + offset);
500507
else
501-
page = vmalloc_to_page((void*)idev->info->mem[mi].addr);
508+
page = vmalloc_to_page((void *)idev->info->mem[mi].addr
509+
+ offset);
502510
get_page(page);
503511
vmf->page = page;
504512
return 0;

0 commit comments

Comments
 (0)