Commit 968ced78 authored by Mauro Carvalho Chehab's avatar Mauro Carvalho Chehab

V4L/DVB (7552): videbuf-vmalloc: Corrects mmap code

There were some bugs on videobuf-vmalloc.

Basically, remap were called with a wrong parameter. Due to that, a later remap
were needed, generating the need of some hacks on videobuf-vmalloc and
videobuf-core.

This patch fixes the remap and removes the hacks.

TODO:

- V4L2_MEMORY_USERPTR is not implemented yet. This method should be
  properly implemented, in order to work with a few userspace applications.

- The driver also doesn't implement V4L2_MEMORY_OVERLAY. This method is used
  only by a few applications, and are becaming obsolete, due to the increment
  of cpu performance. So, most apps prefer to retrieve data to an internal
  buffer, doing some processing like de-interlacing.
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@infradead.org>
parent fbde31d5
...@@ -91,20 +91,6 @@ int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb, ...@@ -91,20 +91,6 @@ int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
MAGIC_CHECK(vb->magic, MAGIC_BUFFER); MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS); MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
/* This is required to avoid OOPS on some cases,
since mmap_mapper() method should be called before _iolock.
On some cases, the mmap_mapper() is called only after scheduling.
*/
if (vb->memory == V4L2_MEMORY_MMAP) {
wait_event_timeout(vb->done, q->is_mmapped,
msecs_to_jiffies(100));
if (!q->is_mmapped) {
printk(KERN_ERR
"Error: mmap_mapper() never called!\n");
return -EINVAL;
}
}
return CALL(q, iolock, q, vb, fbuf); return CALL(q, iolock, q, vb, fbuf);
} }
......
...@@ -124,45 +124,75 @@ static int __videobuf_iolock (struct videobuf_queue* q, ...@@ -124,45 +124,75 @@ static int __videobuf_iolock (struct videobuf_queue* q,
struct videobuf_buffer *vb, struct videobuf_buffer *vb,
struct v4l2_framebuffer *fbuf) struct v4l2_framebuffer *fbuf)
{ {
int pages; struct videobuf_vmalloc_memory *mem = vb->priv;
struct videobuf_vmalloc_memory *mem=vb->priv;
BUG_ON(!mem); BUG_ON(!mem);
MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM); MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT; switch (vb->memory) {
case V4L2_MEMORY_MMAP:
dprintk(1, "%s memory method MMAP\n", __func__);
/* Currently, doesn't support V4L2_MEMORY_OVERLAY */ /* All handling should be done by __videobuf_mmap_mapper() */
if ((vb->memory != V4L2_MEMORY_MMAP) && if (!mem->vmalloc) {
(vb->memory != V4L2_MEMORY_USERPTR) ) { printk(KERN_ERR "memory is not alloced/mmapped.\n");
printk(KERN_ERR "Method currently unsupported.\n"); return -EINVAL;
return -EINVAL; }
} break;
case V4L2_MEMORY_USERPTR:
{
int pages = PAGE_ALIGN(vb->size);
/* FIXME: should be tested with kernel mmap mem */ dprintk(1, "%s memory method USERPTR\n", __func__);
mem->vmalloc=vmalloc_user (PAGE_ALIGN(vb->size));
if (NULL == mem->vmalloc) { #if 1
printk(KERN_ERR "vmalloc (%d pages) failed\n",pages); if (vb->baddr) {
return -ENOMEM; printk(KERN_ERR "USERPTR is currently not supported\n");
} return -EINVAL;
}
#endif
dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n", /* The only USERPTR currently supported is the one needed for
(unsigned long)mem->vmalloc, read() method.
pages << PAGE_SHIFT); */
/* It seems that some kernel versions need to do remap *after* mem->vmalloc = vmalloc_user(pages);
the mmap() call if (!mem->vmalloc) {
*/ printk(KERN_ERR "vmalloc (%d pages) failed\n", pages);
if (mem->vma) { return -ENOMEM;
int retval=remap_vmalloc_range(mem->vma, mem->vmalloc,0);
kfree(mem->vma);
mem->vma=NULL;
if (retval<0) {
dprintk(1,"mmap app bug: remap_vmalloc_range area %p error %d\n",
mem->vmalloc,retval);
return retval;
} }
dprintk(1, "vmalloc is at addr %p (%d pages)\n",
mem->vmalloc, pages);
#if 0
int rc;
/* Kernel userptr is used also by read() method. In this case,
there's no need to remap, since data will be copied to user
*/
if (!vb->baddr)
return 0;
/* FIXME: to properly support USERPTR, remap should occur.
The code bellow won't work, since mem->vma = NULL
*/
/* Try to remap memory */
rc = remap_vmalloc_range(mem->vma, (void *)vb->baddr, 0);
if (rc < 0) {
printk(KERN_ERR "mmap: remap failed with error %d. ", rc);
return -ENOMEM;
}
#endif
break;
}
case V4L2_MEMORY_OVERLAY:
default:
dprintk(1, "%s memory method OVERLAY/unknown\n", __func__);
/* Currently, doesn't support V4L2_MEMORY_OVERLAY */
printk(KERN_ERR "Memory method currently unsupported.\n");
return -EINVAL;
} }
return 0; return 0;
...@@ -178,6 +208,7 @@ static int __videobuf_mmap_free(struct videobuf_queue *q) ...@@ -178,6 +208,7 @@ static int __videobuf_mmap_free(struct videobuf_queue *q)
{ {
unsigned int i; unsigned int i;
dprintk(1, "%s\n", __func__);
for (i = 0; i < VIDEO_MAX_FRAME; i++) { for (i = 0; i < VIDEO_MAX_FRAME; i++) {
if (q->bufs[i]) { if (q->bufs[i]) {
if (q->bufs[i]->map) if (q->bufs[i]->map)
...@@ -194,10 +225,11 @@ static int __videobuf_mmap_mapper(struct videobuf_queue *q, ...@@ -194,10 +225,11 @@ static int __videobuf_mmap_mapper(struct videobuf_queue *q,
struct videobuf_vmalloc_memory *mem; struct videobuf_vmalloc_memory *mem;
struct videobuf_mapping *map; struct videobuf_mapping *map;
unsigned int first; unsigned int first;
int retval; int retval, pages;
unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED)) dprintk(1, "%s\n", __func__);
if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
return -EINVAL; return -EINVAL;
/* look for first buffer to map */ /* look for first buffer to map */
...@@ -217,46 +249,55 @@ static int __videobuf_mmap_mapper(struct videobuf_queue *q, ...@@ -217,46 +249,55 @@ static int __videobuf_mmap_mapper(struct videobuf_queue *q,
} }
/* create mapping + update buffer list */ /* create mapping + update buffer list */
map = q->bufs[first]->map = kzalloc(sizeof(struct videobuf_mapping),GFP_KERNEL); map = kzalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
if (NULL == map) if (NULL == map)
return -ENOMEM; return -ENOMEM;
q->bufs[first]->map = map;
map->start = vma->vm_start; map->start = vma->vm_start;
map->end = vma->vm_end; map->end = vma->vm_end;
map->q = q; map->q = q;
q->bufs[first]->baddr = vma->vm_start; q->bufs[first]->baddr = vma->vm_start;
vma->vm_ops = &videobuf_vm_ops; mem = q->bufs[first]->priv;
vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED; BUG_ON(!mem);
vma->vm_private_data = map; MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
mem=q->bufs[first]->priv; pages = PAGE_ALIGN(vma->vm_end - vma->vm_start);
BUG_ON (!mem); mem->vmalloc = vmalloc_user(pages);
MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM); if (!mem->vmalloc) {
printk(KERN_ERR "vmalloc (%d pages) failed\n", pages);
goto error;
}
dprintk(1, "vmalloc is at addr %p (%d pages)\n",
mem->vmalloc, pages);
/* Try to remap memory */ /* Try to remap memory */
retval=remap_vmalloc_range(vma, mem->vmalloc,0); retval = remap_vmalloc_range(vma, mem->vmalloc, 0);
if (retval<0) { if (retval < 0) {
dprintk(1,"mmap: postponing remap_vmalloc_range\n"); printk(KERN_ERR "mmap: remap failed with error %d. ", retval);
vfree(mem->vmalloc);
mem->vma=kmalloc(sizeof(*vma),GFP_KERNEL); goto error;
if (!mem->vma) {
kfree(map);
q->bufs[first]->map=NULL;
return -ENOMEM;
}
memcpy(mem->vma,vma,sizeof(*vma));
} }
vma->vm_ops = &videobuf_vm_ops;
vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
vma->vm_private_data = map;
dprintk(1,"mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n", dprintk(1,"mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n",
map,q,vma->vm_start,vma->vm_end, map, q, vma->vm_start, vma->vm_end,
(long int) q->bufs[first]->bsize, (long int) q->bufs[first]->bsize,
vma->vm_pgoff,first); vma->vm_pgoff, first);
videobuf_vm_open(vma); videobuf_vm_open(vma);
return (0); return 0;
error:
mem = NULL;
kfree(map);
return -ENOMEM;
} }
static int __videobuf_copy_to_user ( struct videobuf_queue *q, static int __videobuf_copy_to_user ( struct videobuf_queue *q,
...@@ -347,13 +388,19 @@ EXPORT_SYMBOL_GPL(videobuf_to_vmalloc); ...@@ -347,13 +388,19 @@ EXPORT_SYMBOL_GPL(videobuf_to_vmalloc);
void videobuf_vmalloc_free (struct videobuf_buffer *buf) void videobuf_vmalloc_free (struct videobuf_buffer *buf)
{ {
struct videobuf_vmalloc_memory *mem=buf->priv; struct videobuf_vmalloc_memory *mem = buf->priv;
BUG_ON (!mem);
MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM); if (!mem)
return;
MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
vfree(mem->vmalloc); vfree(mem->vmalloc);
mem->vmalloc=NULL; mem->vmalloc = NULL;
/* FIXME: need to do buf->priv = NULL? */
return; return;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment