Hello, On Monday, December 06, 2010 11:53 AM Marek Szyprowski wrote: > From: Pawel Osciak <p.osciak@xxxxxxxxxxx> > > Add generic memory handling routines for userspace pointer handling, > contiguous memory verification and mapping. > > Signed-off-by: Pawel Osciak <p.osciak@xxxxxxxxxxx> > Signed-off-by: Marek Szyprowski <m.szyprowski@xxxxxxxxxxx> > Signed-off-by: Kyungmin Park <kyungmin.park@xxxxxxxxxxx> > CC: Pawel Osciak <pawel@xxxxxxxxxx> > --- > drivers/media/video/Kconfig | 3 + > drivers/media/video/Makefile | 1 + > drivers/media/video/videobuf2-memops.c | 232 ++++++++++++++++++++++++++++++++ > include/media/videobuf2-memops.h | 44 ++++++ > 4 files changed, 280 insertions(+), 0 deletions(-) > create mode 100644 drivers/media/video/videobuf2-memops.c > create mode 100644 include/media/videobuf2-memops.h > > diff --git a/drivers/media/video/Kconfig b/drivers/media/video/Kconfig > index fef6a14..83ce858 100644 > --- a/drivers/media/video/Kconfig > +++ b/drivers/media/video/Kconfig > @@ -52,6 +52,9 @@ config V4L2_MEM2MEM_DEV > config VIDEOBUF2_CORE > tristate > > +config VIDEOBUF2_MEMOPS > + tristate > + > # > # Multimedia Video device configuration > # > diff --git a/drivers/media/video/Makefile b/drivers/media/video/Makefile > index 77c4f85..a97a2a0 100644 > --- a/drivers/media/video/Makefile > +++ b/drivers/media/video/Makefile > @@ -115,6 +115,7 @@ obj-$(CONFIG_VIDEOBUF_DVB) += videobuf-dvb.o > obj-$(CONFIG_VIDEO_BTCX) += btcx-risc.o > > obj-$(CONFIG_VIDEOBUF2_CORE) += videobuf2-core.o > +obj-$(CONFIG_VIDEOBUF2_MEMOPS) += videobuf2-memops.o > > obj-$(CONFIG_V4L2_MEM2MEM_DEV) += v4l2-mem2mem.o > > diff --git a/drivers/media/video/videobuf2-memops.c b/drivers/media/video/videobuf2-memops.c > new file mode 100644 > index 0000000..69aa6dc > --- /dev/null > +++ b/drivers/media/video/videobuf2-memops.c > @@ -0,0 +1,232 @@ > +/* > + * videobuf2-memops.c - generic memory handling routines for videobuf2 > + * > + * Copyright (C) 2010 Samsung Electronics > + * > + * Author: Pawel Osciak <p.osciak@xxxxxxxxxxx> > + * Marek Szyprowski <m.szyprowski@xxxxxxxxxxx> > + * > + * This program is free software; you can redistribute it and/or modify > + * it under the terms of the GNU General Public License as published by > + * the Free Software Foundation. > + */ > + > +#include <linux/slab.h> > +#include <linux/module.h> > +#include <linux/dma-mapping.h> > +#include <linux/vmalloc.h> > +#include <linux/cma.h> > +#include <linux/mm.h> > +#include <linux/sched.h> > +#include <linux/file.h> > +#include <linux/slab.h> > + > +#include <media/videobuf2-core.h> > +#include <media/videobuf2-memops.h> > + > +/** > + * vb2_get_vma() - acquire and lock the memory area > + * @vaddr: virtual userspace address to the given area > + * > + * This function attempts to acquire an area mapped in the userspace for > + * the duration of a hardware operation. > + * > + * Returns a virtual memory region associated with the given vaddr on success > + * or NULL. > + */ > +struct vm_area_struct *vb2_get_vma(struct vm_area_struct *vma) > +{ > + struct vm_area_struct *vma_copy; > + > + vma_copy = kmalloc(sizeof(*vma_copy), GFP_KERNEL); > + if (vma_copy == NULL) > + return NULL; > + > + if (vma->vm_ops && vma->vm_ops->open) > + vma->vm_ops->open(vma); > + > + if (vma->vm_file) > + get_file(vma->vm_file); > + > + memcpy(vma_copy, vma, sizeof(*vma)); > + > + vma_copy->vm_mm = NULL; > + vma_copy->vm_next = NULL; > + vma_copy->vm_prev = NULL; > + > + return vma_copy; > +} > + > +/** > + * vb2_put_userptr() - release a userspace memory area > + * @vma: virtual memory region associated with the area to be released > + * > + * This function releases the previously acquired memory area after a hardware > + * operation. > + */ > +void vb2_put_vma(struct vm_area_struct *vma) > +{ > + if (!vma) > + return; > + > + if (vma->vm_file) > + fput(vma->vm_file); > + > + if (vma->vm_ops && vma->vm_ops->close) > + vma->vm_ops->close(vma); > + > + kfree(vma); > +} > + > +/** > + * vb2_contig_verify_userptr() - verify contiguity of a userspace-mapped memory > + * @vma: virtual memory region which maps the physical memory > + * to be verified > + * @vaddr: starting virtual address of the area to be verified > + * @size: size of the area to be verified > + * @paddr: will return physical address for the given vaddr > + * > + * This function will go through memory area of size size mapped at vaddr and > + * verify that the underlying physical pages are contiguous. > + * > + * Returns 0 on success and a physical address to the memory pointed > + * to by vaddr in paddr. > + */ > +int vb2_get_contig_userptr(unsigned long vaddr, unsigned long size, > + struct vm_area_struct **res_vma, dma_addr_t *res_pa) > +{ > + struct mm_struct *mm = current->mm; > + struct vm_area_struct *vma; > + unsigned long offset, start, end; > + unsigned long this_pfn, prev_pfn; > + dma_addr_t pa = 0; > + int ret = -EFAULT; > + > + start = vaddr; > + offset = start & ~PAGE_MASK; > + end = start + size; > + > + down_read(&mm->mmap_sem); > + vma = find_vma(mm, start); > + > + if (vma == NULL || vma->vm_end < end) > + goto done; > + > + for (prev_pfn = 0; start <= end; start += PAGE_SIZE) { I did an ugly off-by-one bug here. "start < end" comparison should be used instead. > + ret = follow_pfn(vma, start, &this_pfn); > + if (ret) > + goto done; > + > + if (prev_pfn == 0) > + pa = this_pfn << PAGE_SHIFT; > + else if (this_pfn != prev_pfn + 1) { > + ret = -EFAULT; > + goto done; > + } > + prev_pfn = this_pfn; > + } > + > + /* > + * Memory is contigous, lock vma and return to the caller > + */ > + *res_vma = vb2_get_vma(vma); > + if (*res_vma == NULL) { > + ret = -ENOMEM; > + goto done; > + } > + *res_pa = pa + offset; > + ret = 0; > + > +done: > + up_read(&mm->mmap_sem); > + return ret; > +} > + <snip> Best regards -- Marek Szyprowski Samsung Poland R&D Center -- To unsubscribe from this list: send the line "unsubscribe linux-media" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html