Commit 48b25a3a authored by Jan Kara's avatar Jan Kara Committed by Mauro Carvalho Chehab

[media] vb2: Push mmap_sem down to memops

Currently vb2 core acquires mmap_sem just around call to
__qbuf_userptr(). However since commit f035eb4e (videobuf2: fix
lockdep warning) it isn't necessary to acquire it so early as we no
longer have to drop queue mutex before acquiring mmap_sem. So push
acquisition of mmap_sem down into .get_userptr and .put_userptr memops
so that the semaphore is acquired for a shorter time and it is clearer
what it is needed for.
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 9c3f2052
...@@ -1657,9 +1657,7 @@ static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b) ...@@ -1657,9 +1657,7 @@ static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)
ret = __qbuf_mmap(vb, b); ret = __qbuf_mmap(vb, b);
break; break;
case V4L2_MEMORY_USERPTR: case V4L2_MEMORY_USERPTR:
down_read(&current->mm->mmap_sem);
ret = __qbuf_userptr(vb, b); ret = __qbuf_userptr(vb, b);
up_read(&current->mm->mmap_sem);
break; break;
case V4L2_MEMORY_DMABUF: case V4L2_MEMORY_DMABUF:
ret = __qbuf_dmabuf(vb, b); ret = __qbuf_dmabuf(vb, b);
......
...@@ -532,7 +532,9 @@ static void vb2_dc_put_userptr(void *buf_priv) ...@@ -532,7 +532,9 @@ static void vb2_dc_put_userptr(void *buf_priv)
sg_free_table(sgt); sg_free_table(sgt);
kfree(sgt); kfree(sgt);
} }
down_read(&current->mm->mmap_sem);
vb2_put_vma(buf->vma); vb2_put_vma(buf->vma);
up_read(&current->mm->mmap_sem);
kfree(buf); kfree(buf);
} }
...@@ -616,6 +618,7 @@ static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr, ...@@ -616,6 +618,7 @@ static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
goto fail_buf; goto fail_buf;
} }
down_read(&current->mm->mmap_sem);
/* current->mm->mmap_sem is taken by videobuf2 core */ /* current->mm->mmap_sem is taken by videobuf2 core */
vma = find_vma(current->mm, vaddr); vma = find_vma(current->mm, vaddr);
if (!vma) { if (!vma) {
...@@ -642,6 +645,7 @@ static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr, ...@@ -642,6 +645,7 @@ static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
if (ret) { if (ret) {
unsigned long pfn; unsigned long pfn;
if (vb2_dc_get_user_pfn(start, n_pages, vma, &pfn) == 0) { if (vb2_dc_get_user_pfn(start, n_pages, vma, &pfn) == 0) {
up_read(&current->mm->mmap_sem);
buf->dma_addr = vb2_dc_pfn_to_dma(buf->dev, pfn); buf->dma_addr = vb2_dc_pfn_to_dma(buf->dev, pfn);
buf->size = size; buf->size = size;
kfree(pages); kfree(pages);
...@@ -651,6 +655,7 @@ static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr, ...@@ -651,6 +655,7 @@ static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
pr_err("failed to get user pages\n"); pr_err("failed to get user pages\n");
goto fail_vma; goto fail_vma;
} }
up_read(&current->mm->mmap_sem);
sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
if (!sgt) { if (!sgt) {
...@@ -713,10 +718,12 @@ static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr, ...@@ -713,10 +718,12 @@ static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
while (n_pages) while (n_pages)
put_page(pages[--n_pages]); put_page(pages[--n_pages]);
down_read(&current->mm->mmap_sem);
fail_vma: fail_vma:
vb2_put_vma(buf->vma); vb2_put_vma(buf->vma);
fail_pages: fail_pages:
up_read(&current->mm->mmap_sem);
kfree(pages); /* kfree is NULL-proof */ kfree(pages); /* kfree is NULL-proof */
fail_buf: fail_buf:
......
...@@ -263,6 +263,7 @@ static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr, ...@@ -263,6 +263,7 @@ static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr,
if (!buf->pages) if (!buf->pages)
goto userptr_fail_alloc_pages; goto userptr_fail_alloc_pages;
down_read(&current->mm->mmap_sem);
vma = find_vma(current->mm, vaddr); vma = find_vma(current->mm, vaddr);
if (!vma) { if (!vma) {
dprintk(1, "no vma for address %lu\n", vaddr); dprintk(1, "no vma for address %lu\n", vaddr);
...@@ -301,6 +302,7 @@ static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr, ...@@ -301,6 +302,7 @@ static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr,
1, /* force */ 1, /* force */
buf->pages, buf->pages,
NULL); NULL);
up_read(&current->mm->mmap_sem);
if (num_pages_from_user != buf->num_pages) if (num_pages_from_user != buf->num_pages)
goto userptr_fail_get_user_pages; goto userptr_fail_get_user_pages;
...@@ -328,8 +330,10 @@ static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr, ...@@ -328,8 +330,10 @@ static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr,
if (!vma_is_io(buf->vma)) if (!vma_is_io(buf->vma))
while (--num_pages_from_user >= 0) while (--num_pages_from_user >= 0)
put_page(buf->pages[num_pages_from_user]); put_page(buf->pages[num_pages_from_user]);
down_read(&current->mm->mmap_sem);
vb2_put_vma(buf->vma); vb2_put_vma(buf->vma);
userptr_fail_find_vma: userptr_fail_find_vma:
up_read(&current->mm->mmap_sem);
kfree(buf->pages); kfree(buf->pages);
userptr_fail_alloc_pages: userptr_fail_alloc_pages:
kfree(buf); kfree(buf);
...@@ -362,7 +366,9 @@ static void vb2_dma_sg_put_userptr(void *buf_priv) ...@@ -362,7 +366,9 @@ static void vb2_dma_sg_put_userptr(void *buf_priv)
put_page(buf->pages[i]); put_page(buf->pages[i]);
} }
kfree(buf->pages); kfree(buf->pages);
down_read(&current->mm->mmap_sem);
vb2_put_vma(buf->vma); vb2_put_vma(buf->vma);
up_read(&current->mm->mmap_sem);
kfree(buf); kfree(buf);
} }
......
...@@ -89,7 +89,7 @@ static void *vb2_vmalloc_get_userptr(void *alloc_ctx, unsigned long vaddr, ...@@ -89,7 +89,7 @@ static void *vb2_vmalloc_get_userptr(void *alloc_ctx, unsigned long vaddr,
offset = vaddr & ~PAGE_MASK; offset = vaddr & ~PAGE_MASK;
buf->size = size; buf->size = size;
down_read(&current->mm->mmap_sem);
vma = find_vma(current->mm, vaddr); vma = find_vma(current->mm, vaddr);
if (vma && (vma->vm_flags & VM_PFNMAP) && (vma->vm_pgoff)) { if (vma && (vma->vm_flags & VM_PFNMAP) && (vma->vm_pgoff)) {
if (vb2_get_contig_userptr(vaddr, size, &vma, &physp)) if (vb2_get_contig_userptr(vaddr, size, &vma, &physp))
...@@ -121,6 +121,7 @@ static void *vb2_vmalloc_get_userptr(void *alloc_ctx, unsigned long vaddr, ...@@ -121,6 +121,7 @@ static void *vb2_vmalloc_get_userptr(void *alloc_ctx, unsigned long vaddr,
if (!buf->vaddr) if (!buf->vaddr)
goto fail_get_user_pages; goto fail_get_user_pages;
} }
up_read(&current->mm->mmap_sem);
buf->vaddr += offset; buf->vaddr += offset;
return buf; return buf;
...@@ -133,6 +134,7 @@ static void *vb2_vmalloc_get_userptr(void *alloc_ctx, unsigned long vaddr, ...@@ -133,6 +134,7 @@ static void *vb2_vmalloc_get_userptr(void *alloc_ctx, unsigned long vaddr,
kfree(buf->pages); kfree(buf->pages);
fail_pages_array_alloc: fail_pages_array_alloc:
up_read(&current->mm->mmap_sem);
kfree(buf); kfree(buf);
return NULL; return NULL;
...@@ -144,6 +146,7 @@ static void vb2_vmalloc_put_userptr(void *buf_priv) ...@@ -144,6 +146,7 @@ static void vb2_vmalloc_put_userptr(void *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;
down_read(&current->mm->mmap_sem);
if (buf->pages) { if (buf->pages) {
if (vaddr) if (vaddr)
vm_unmap_ram((void *)vaddr, buf->n_pages); vm_unmap_ram((void *)vaddr, buf->n_pages);
...@@ -157,6 +160,7 @@ static void vb2_vmalloc_put_userptr(void *buf_priv) ...@@ -157,6 +160,7 @@ static void vb2_vmalloc_put_userptr(void *buf_priv)
vb2_put_vma(buf->vma); vb2_put_vma(buf->vma);
iounmap((__force void __iomem *)buf->vaddr); iounmap((__force void __iomem *)buf->vaddr);
} }
up_read(&current->mm->mmap_sem);
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