Skip to contentSkip to conversation

UIO: Fix mapping of logical and virtual memory

PATCH Applied Hans J. Koch 2 participants 2 messages Sep 23, 2008 – Oct 1, 2008 Userspace I/O (UIO)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 UIO: Fix mapping of logical and virtual memory

I received this code by private mail from Andrew G. Harvey who found the bug and provided a fix for it. He also tested the patch below, we discussed it for a while, and I believe this to be correct. I simply took his code and made a patch from it, see below.

Thanks to Andrew for pointing this out!

Hans
---------8<----------------

From: "Andrew G. Harvey" <[email protected]>
To: LKML <[email protected]>
Cc: "Greg KH" <[email protected]>, "Hans J. Koch" <[email protected]>
Date: Wed, 24 Sep 2008 00:43:13 +0200
Subject: 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.

---
 drivers/uio/uio.c |   12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/uio/uio.c +10 -2
Index: linux-2.6.27-rc/drivers/uio/uio.c
===================================================================
@@ -490,15 +490,23 @@
 {
 	struct uio_device *idev = vma->vm_private_data;
 	struct page *page;
+	unsigned long offset;
 
 	int mi = uio_find_mem_index(vma);
 	if (mi < 0)
 		return VM_FAULT_SIGBUS;
 
+	/*
+	 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
+	 * to use mem[N].
+	 */
+	offset = (vmf->pgoff - mi) << PAGE_SHIFT;
+
 	if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
-		page = virt_to_page(idev->info->mem[mi].addr);
+		page = virt_to_page(idev->info->mem[mi].addr + offset);
 	else
-		page = vmalloc_to_page((void*)idev->info->mem[mi].addr);
+		page = vmalloc_to_page((void *)idev->info->mem[mi].addr
+							+ offset);
 	get_page(page);
 	vmf->page = page;
 	return 0;
#1 lore raw

patch uio-fix-mapping-of-logical-and-virtual-memory.patch added to gregkh-2.6 tree

This is a note to let you know that I've just added the patch titled

Subject: UIO: Fix mapping of logical and virtual memory

to my gregkh-2.6 tree. Its filename is

uio-fix-mapping-of-logical-and-virtual-memory.patch

This tree can be found at
http://www.kernel.org/pub/linux/kernel/people/gregkh/gregkh-2.6/patches/

Quoted 1 line

From [email protected] Wed Oct 1 14:01:23 2008

From: Andrew Harvey <[email protected]>
Date: Wed, 24 Sep 2008 01:10:02 +0200
Subject: UIO: Fix mapping of logical and virtual memory
To: LKML <[email protected]>
Cc: Greg KH <[email protected]>, "Andrew G. Harvey" <[email protected]>
Message-ID: <20080923230959.GB4066@local>
Content-Disposition: inline

From: "Andrew G. Harvey" <[email protected]>

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.

---
 drivers/uio/uio.c |   12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/uio/uio.c +10 -2
@@ -490,15 +490,23 @@ static int uio_vma_fault(struct vm_area_
 {
 	struct uio_device *idev = vma->vm_private_data;
 	struct page *page;
+	unsigned long offset;
 
 	int mi = uio_find_mem_index(vma);
 	if (mi < 0)
 		return VM_FAULT_SIGBUS;
 
+	/*
+	 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
+	 * to use mem[N].
+	 */
+	offset = (vmf->pgoff - mi) << PAGE_SHIFT;
+
 	if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
-		page = virt_to_page(idev->info->mem[mi].addr);
+		page = virt_to_page(idev->info->mem[mi].addr + offset);
 	else
-		page = vmalloc_to_page((void*)idev->info->mem[mi].addr);
+		page = vmalloc_to_page((void *)idev->info->mem[mi].addr
+							+ offset);
 	get_page(page);
 	vmf->page = page;
 	return 0;

Patches currently in gregkh-2.6 which might be from [email protected] are

driver-core/uio-fix-mapping-of-logical-and-virtual-memory.patch