public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Device driver memory 'mmap()' function helper cleanup
@ 2013-04-17  3:12 Linus Torvalds
  2013-04-17  7:20 ` Takashi Iwai
                   ` (4 more replies)
  0 siblings, 5 replies; 21+ messages in thread
From: Linus Torvalds @ 2013-04-17  3:12 UTC (permalink / raw)
  To: Linux Kernel Mailing List, Clemens Ladisch, Arnd Bergmann,
	Takashi Iwai, Mauro Carvalho Chehab

[-- Attachment #1: Type: text/plain, Size: 2985 bytes --]

Guys, I just pushed out a new helper function intended for cleaning up
various device driver mmap functions, because they are rather messy,
and at least part of the problem was the bad impedance between what a
driver author would want to have, and the VM interfaces to map a
memory range into user space with mmap.

Some drivers would end up doing extensive checks on the length of the
mappings and the page offset within the mapping, while other drivers
would end up doing no checks at all.

The new helper is in commit b4cbb197c7e7 ("vm: add vm_iomap_memory()
helper function"), but I didn't actually commit any *users* of it,
because I just have this untested patch-collection for a few random
drivers (picked across a few different driver subsystems, just to make
it interesting).  I did that largely just to check the different use
cases, but I don't actually tend to *use* all that many fancy drivers,
so I don't have much of a way of testing it.

The media layer has a few users of [io_]remap_pfn_range() that look
like they could do with some tender loving too, but they don't match
this particular pattern of "allow users to map a part of a fixed range
of memory". In fact, the media pattern seems to be single-page
mappings, which probably should use "vm_insert_page()" instead, but
that's a whole separate thing. But I didn't check all the media cases
(and there's a lot of remap_pfn_range use outside of media drivers I
didn't check either), so there might be code that could use the new
helper.

Anyway, I'm attaching the *untested* patch to several drivers. Guys,
mind taking a look? The point here is to simplify the interface,
avoiding bugs, but also:

 5 files changed, 21 insertions(+), 87 deletions(-)

it needs current -git for the new helper function.

NOTE! The driver subsystem .mmap functions seem to almost universally do

    if (io_remap_pfn_range(..))
        return -EAGAIN;
    return 0;

and I didn't make the new helper function do that "turn all
remap_pfn_range errors into EAGAIN". My *suspicion* is that this is
just really old copy-pasta and makes no sense, but maybe there is some
actual reasoning behind EAGAIN vs ENOMEM, for example. EAGAIN is
documented to be about file/memory locking, which means that it really
doesn't make any sense, but obviously there might be some binary that
actally depends on this, so I'm perfectly willing to make the helper
do that odd error case, I'd just like to know (and a add a comment)
WHY.

My personal guess is that nobody actually cares (we return other error
codes for other cases, notably EINVAL for various out-of-mapping-range
issues), and the whole EAGAIN return value is just a completely
historical oddity.

(And yes, I know the mtdchar code is actually disabled right now. But
that was a good example of a driver that had a bug in this area and
that I touched myself not too long ago, and recent stable noise
reminded me of it, so I did that one despite it not being active).

                Linus

[-- Attachment #2: patch.diff --]
[-- Type: application/octet-stream, Size: 6437 bytes --]

 drivers/char/bsr.c      | 17 +----------------
 drivers/char/hpet.c     |  8 ++------
 drivers/mtd/mtdchar.c   | 32 ++------------------------------
 drivers/video/fbmem.c   | 39 ++++++++++++++-------------------------
 sound/core/pcm_native.c | 12 ++----------
 5 files changed, 21 insertions(+), 87 deletions(-)

diff --git a/drivers/char/bsr.c b/drivers/char/bsr.c
index 97467053a01b..7c8785388798 100644
--- a/drivers/char/bsr.c
+++ b/drivers/char/bsr.c
@@ -124,22 +124,7 @@ static int bsr_mmap(struct file *filp, struct vm_area_struct *vma)
 	int ret;
 
 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
-
-	/* check for the case of a small BSR device and map one 4k page for it*/
-	if (dev->bsr_len < PAGE_SIZE && size == PAGE_SIZE)
-		ret = remap_4k_pfn(vma, vma->vm_start, dev->bsr_addr >> 12,
-				   vma->vm_page_prot);
-	else if (size <= dev->bsr_len)
-		ret = io_remap_pfn_range(vma, vma->vm_start,
-					 dev->bsr_addr >> PAGE_SHIFT,
-					 size, vma->vm_page_prot);
-	else
-		return -EINVAL;
-
-	if (ret)
-		return -EAGAIN;
-
-	return 0;
+	return vm_iomap_memory(vma, dev->bsr_addr, dev->bsr_len);
 }
 
 static int bsr_open(struct inode * inode, struct file * filp)
diff --git a/drivers/char/hpet.c b/drivers/char/hpet.c
index e3f9a99b8522..0f238a886a2e 100644
--- a/drivers/char/hpet.c
+++ b/drivers/char/hpet.c
@@ -382,13 +382,9 @@ static int hpet_mmap(struct file *file, struct vm_area_struct *vma)
 	if (addr & (PAGE_SIZE - 1))
 		return -ENOSYS;
 
-	vma->vm_flags |= VM_IO;
 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
-
-	if (io_remap_pfn_range(vma, vma->vm_start, addr >> PAGE_SHIFT,
-					PAGE_SIZE, vma->vm_page_prot)) {
-		printk(KERN_ERR "%s: io_remap_pfn_range failed\n",
-			__func__);
+	if (vm_iomap_memory(vma, addr, PAGE_SIZE)) {
+		printk(KERN_ERR "%s: vm_iomap_memory failed\n", __func__);
 		return -EAGAIN;
 	}
 
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index 92ab30ab00dc..61a1b22c05e7 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -1159,45 +1159,17 @@ static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
 	struct mtd_file_info *mfi = file->private_data;
 	struct mtd_info *mtd = mfi->mtd;
 	struct map_info *map = mtd->priv;
-	resource_size_t start, off;
-	unsigned long len, vma_len;
 
         /* This is broken because it assumes the MTD device is map-based
 	   and that mtd->priv is a valid struct map_info.  It should be
 	   replaced with something that uses the mtd_get_unmapped_area()
 	   operation properly. */
 	if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) {
-		off = get_vm_offset(vma);
-		start = map->phys;
-		len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size);
-		start &= PAGE_MASK;
-		vma_len = get_vm_size(vma);
-
-		/* Overflow in off+len? */
-		if (vma_len + off < off)
-			return -EINVAL;
-		/* Does it fit in the mapping? */
-		if (vma_len + off > len)
-			return -EINVAL;
-
-		off += start;
-		/* Did that overflow? */
-		if (off < start)
-			return -EINVAL;
-		if (set_vm_offset(vma, off) < 0)
-			return -EINVAL;
-		vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
-
 #ifdef pgprot_noncached
-		if (file->f_flags & O_DSYNC || off >= __pa(high_memory))
+		if (file->f_flags & O_DSYNC || map->phys >= __pa(high_memory))
 			vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
 #endif
-		if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
-				       vma->vm_end - vma->vm_start,
-				       vma->vm_page_prot))
-			return -EAGAIN;
-
-		return 0;
+		return vm_iomap_memory(vma, map->phys, map->size);
 	}
 	return -ENOSYS;
 #else
diff --git a/drivers/video/fbmem.c b/drivers/video/fbmem.c
index 7c254084b6a0..86291dcd964a 100644
--- a/drivers/video/fbmem.c
+++ b/drivers/video/fbmem.c
@@ -1373,15 +1373,12 @@ fb_mmap(struct file *file, struct vm_area_struct * vma)
 {
 	struct fb_info *info = file_fb_info(file);
 	struct fb_ops *fb;
-	unsigned long off;
+	unsigned long mmio_pgoff;
 	unsigned long start;
 	u32 len;
 
 	if (!info)
 		return -ENODEV;
-	if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
-		return -EINVAL;
-	off = vma->vm_pgoff << PAGE_SHIFT;
 	fb = info->fbops;
 	if (!fb)
 		return -ENODEV;
@@ -1393,32 +1390,24 @@ fb_mmap(struct file *file, struct vm_area_struct * vma)
 		return res;
 	}
 
-	/* frame buffer memory */
+	/*
+	 * Ugh. This can be either the frame buffer mapping, or
+	 * if pgoff points past it, the mmio mapping.
+	 */
 	start = info->fix.smem_start;
-	len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.smem_len);
-	if (off >= len) {
-		/* memory mapped io */
-		off -= len;
-		if (info->var.accel_flags) {
-			mutex_unlock(&info->mm_lock);
-			return -EINVAL;
-		}
+	len = info->fix.smem_len;
+	mmio_pgoff = PAGE_ALIGN((start & ~PAGE_MASK) + len) >> PAGE_SHIFT;
+	if (vma->vm_pgoff >= mmio_pgoff) {
+		vma->vm_pgoff -= mmio_pgoff;
 		start = info->fix.mmio_start;
-		len = PAGE_ALIGN((start & ~PAGE_MASK) + info->fix.mmio_len);
+		len = info->fix.mmio_len;
 	}
 	mutex_unlock(&info->mm_lock);
-	start &= PAGE_MASK;
-	if ((vma->vm_end - vma->vm_start + off) > len)
-		return -EINVAL;
-	off += start;
-	vma->vm_pgoff = off >> PAGE_SHIFT;
-	/* VM_IO | VM_DONTEXPAND | VM_DONTDUMP are set by io_remap_pfn_range()*/
+
 	vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
-	fb_pgprotect(file, vma, off);
-	if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
-			     vma->vm_end - vma->vm_start, vma->vm_page_prot))
-		return -EAGAIN;
-	return 0;
+	fb_pgprotect(file, vma, start);
+
+	return vm_iomap_memory(vma, start, len);
 }
 
 static int
diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 71ae86ca64ac..eb560fa32321 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -3222,18 +3222,10 @@ EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
 			   struct vm_area_struct *area)
 {
-	long size;
-	unsigned long offset;
+	struct snd_pcm_runtime *runtime = substream->runtime;;
 
 	area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
-	area->vm_flags |= VM_IO;
-	size = area->vm_end - area->vm_start;
-	offset = area->vm_pgoff << PAGE_SHIFT;
-	if (io_remap_pfn_range(area, area->vm_start,
-				(substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
-				size, area->vm_page_prot))
-		return -EAGAIN;
-	return 0;
+	return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
 }
 
 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);

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

end of thread, other threads:[~2013-05-13 14:23 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-04-17  3:12 Device driver memory 'mmap()' function helper cleanup Linus Torvalds
2013-04-17  7:20 ` Takashi Iwai
2013-04-17  9:15 ` Arnd Bergmann
2013-04-17  9:45   ` Clemens Ladisch
2013-04-17 17:58   ` Linus Torvalds
2013-04-17 21:28     ` Arnd Bergmann
2013-04-17 21:31       ` Linus Torvalds
2013-04-17 10:43 ` Mauro Carvalho Chehab
2013-04-17 12:22   ` [PATCH 1/2] [media] videobuf-dma-contig: remove support for cached mem Mauro Carvalho Chehab
2013-04-17 12:22     ` [PATCH 2/2] [media] videobuf-dma-contig: use vm_iomap_memory() Mauro Carvalho Chehab
2013-04-17 12:56       ` Mauro Carvalho Chehab
2013-04-17 12:49     ` [PATCH 1/2] [media] videobuf-dma-contig: remove support for cached mem Hans Verkuil
2013-05-12 21:52   ` Device driver memory 'mmap()' function helper cleanup Sakari Ailus
2013-04-17 11:34 ` Tomi Valkeinen
2013-04-17 14:44   ` Linus Torvalds
2013-04-17 17:11     ` David Miller
2013-04-17 17:20       ` Linus Torvalds
2013-04-17 17:27         ` David Miller
2013-04-17 17:48           ` Linus Torvalds
2013-04-19 15:43 ` Michel Lespinasse
2013-04-19 23:07   ` Linus Torvalds

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox