Commit 5a9e4dec authored by Jan Kara's avatar Jan Kara Committed by Mauro Carvalho Chehab

[media] media: vb2: Convert vb2_vmalloc_get_userptr() to use frame vector

Convert vb2_vmalloc_get_userptr() to use frame vector infrastructure.
When we are doing that there's no need to allocate page array and some
code can be simplified.
Tested-by: default avatarMarek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: default avatarJan Kara <jack@suse.cz>
Signed-off-by: default avatarHans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@osg.samsung.com>
parent 3336c24f
...@@ -23,11 +23,9 @@ ...@@ -23,11 +23,9 @@
struct vb2_vmalloc_buf { struct vb2_vmalloc_buf {
void *vaddr; void *vaddr;
struct page **pages; struct frame_vector *vec;
struct vm_area_struct *vma;
enum dma_data_direction dma_dir; enum dma_data_direction dma_dir;
unsigned long size; unsigned long size;
unsigned int n_pages;
atomic_t refcount; atomic_t refcount;
struct vb2_vmarea_handler handler; struct vb2_vmarea_handler handler;
struct dma_buf *dbuf; struct dma_buf *dbuf;
...@@ -76,10 +74,8 @@ static void *vb2_vmalloc_get_userptr(void *alloc_ctx, unsigned long vaddr, ...@@ -76,10 +74,8 @@ static void *vb2_vmalloc_get_userptr(void *alloc_ctx, unsigned long vaddr,
enum dma_data_direction dma_dir) enum dma_data_direction dma_dir)
{ {
struct vb2_vmalloc_buf *buf; struct vb2_vmalloc_buf *buf;
unsigned long first, last; struct frame_vector *vec;
int n_pages, offset; int n_pages, offset, i;
struct vm_area_struct *vma;
dma_addr_t physp;
buf = kzalloc(sizeof(*buf), GFP_KERNEL); buf = kzalloc(sizeof(*buf), GFP_KERNEL);
if (!buf) if (!buf)
...@@ -88,53 +84,36 @@ static void *vb2_vmalloc_get_userptr(void *alloc_ctx, unsigned long vaddr, ...@@ -88,53 +84,36 @@ static void *vb2_vmalloc_get_userptr(void *alloc_ctx, unsigned long vaddr,
buf->dma_dir = dma_dir; buf->dma_dir = dma_dir;
offset = vaddr & ~PAGE_MASK; offset = vaddr & ~PAGE_MASK;
buf->size = size; buf->size = size;
vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE);
down_read(&current->mm->mmap_sem); if (IS_ERR(vec))
vma = find_vma(current->mm, vaddr); goto fail_pfnvec_create;
if (vma && (vma->vm_flags & VM_PFNMAP) && (vma->vm_pgoff)) { buf->vec = vec;
if (vb2_get_contig_userptr(vaddr, size, &vma, &physp)) n_pages = frame_vector_count(vec);
goto fail_pages_array_alloc; if (frame_vector_to_pages(vec) < 0) {
buf->vma = vma; unsigned long *nums = frame_vector_pfns(vec);
buf->vaddr = (__force void *)ioremap_nocache(physp, size);
if (!buf->vaddr) /*
goto fail_pages_array_alloc; * We cannot get page pointers for these pfns. Check memory is
* physically contiguous and use direct mapping.
*/
for (i = 1; i < n_pages; i++)
if (nums[i-1] + 1 != nums[i])
goto fail_map;
buf->vaddr = (__force void *)
ioremap_nocache(nums[0] << PAGE_SHIFT, size);
} else { } else {
first = vaddr >> PAGE_SHIFT; buf->vaddr = vm_map_ram(frame_vector_pages(vec), n_pages, -1,
last = (vaddr + size - 1) >> PAGE_SHIFT;
buf->n_pages = last - first + 1;
buf->pages = kzalloc(buf->n_pages * sizeof(struct page *),
GFP_KERNEL);
if (!buf->pages)
goto fail_pages_array_alloc;
/* current->mm->mmap_sem is taken by videobuf2 core */
n_pages = get_user_pages(current, current->mm,
vaddr & PAGE_MASK, buf->n_pages,
dma_dir == DMA_FROM_DEVICE,
1, /* force */
buf->pages, NULL);
if (n_pages != buf->n_pages)
goto fail_get_user_pages;
buf->vaddr = vm_map_ram(buf->pages, buf->n_pages, -1,
PAGE_KERNEL); PAGE_KERNEL);
if (!buf->vaddr)
goto fail_get_user_pages;
} }
up_read(&current->mm->mmap_sem);
if (!buf->vaddr)
goto fail_map;
buf->vaddr += offset; buf->vaddr += offset;
return buf; return buf;
fail_get_user_pages: fail_map:
pr_debug("get_user_pages requested/got: %d/%d]\n", n_pages, vb2_destroy_framevec(vec);
buf->n_pages); fail_pfnvec_create:
while (--n_pages >= 0)
put_page(buf->pages[n_pages]);
kfree(buf->pages);
fail_pages_array_alloc:
up_read(&current->mm->mmap_sem);
kfree(buf); kfree(buf);
return NULL; return NULL;
...@@ -145,20 +124,21 @@ static void vb2_vmalloc_put_userptr(void *buf_priv) ...@@ -145,20 +124,21 @@ static void vb2_vmalloc_put_userptr(void *buf_priv)
struct vb2_vmalloc_buf *buf = buf_priv; struct vb2_vmalloc_buf *buf = buf_priv;
unsigned long vaddr = (unsigned long)buf->vaddr & PAGE_MASK; unsigned long vaddr = (unsigned long)buf->vaddr & PAGE_MASK;
unsigned int i; unsigned int i;
struct page **pages;
unsigned int n_pages;
if (buf->pages) { if (!buf->vec->is_pfns) {
n_pages = frame_vector_count(buf->vec);
pages = frame_vector_pages(buf->vec);
if (vaddr) if (vaddr)
vm_unmap_ram((void *)vaddr, buf->n_pages); vm_unmap_ram((void *)vaddr, n_pages);
for (i = 0; i < buf->n_pages; ++i) { if (buf->dma_dir == DMA_FROM_DEVICE)
if (buf->dma_dir == DMA_FROM_DEVICE) for (i = 0; i < n_pages; i++)
set_page_dirty_lock(buf->pages[i]); set_page_dirty_lock(pages[i]);
put_page(buf->pages[i]);
}
kfree(buf->pages);
} else { } else {
vb2_put_vma(buf->vma);
iounmap((__force void __iomem *)buf->vaddr); iounmap((__force void __iomem *)buf->vaddr);
} }
vb2_destroy_framevec(buf->vec);
kfree(buf); kfree(buf);
} }
......
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