media: videobuf2-vmalloc: get_userptr: buffers are always writable

In vb2_vmalloc_get_userptr() the framevector is created with the
'write' argument set to false when vb2_create_framevec() is called
for OUTPUT buffers. So the pages are marked as read-only.

However, userspace will write to these buffers since it will fill
in the data to output. Since get_userptr is only called if the userptr
of the queued buffer has changed since the last time that same buffer
was queued, this will fail when the buffer contents is updated and the
buffer is queued again.

E.g., userspace fills buffer 1 with the output video and queues it.
The first time get_userptr is called and the pages are grabbed and
pinned in memory and marked read-only. The second time buffer 1 is
filled with different video data and queued again. Since the userptr
hasn't changed the get_userptr() callback isn't called again. Since
the pages were marked as read-only the new contents isn't updated.

Just always call vb2_create_framevec() with FOLL_WRITE to always
allow writing to the buffers.

Using USERPTR streaming with OUTPUT devices is almost never done. And
when it is done it is via v4l2-compliance and a driver like vim2m. But
since v4l2-compliance doesn't actually inspect the capture buffer and
compare it to the original output buffer, this issue was never noticed.

But the vicodec driver actually needs to parse the bitstream in the
OUTPUT buffers and any errors there will be immediately noticed. So
this time v4l2-compliance failed the USERPTR streaming test.

Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
This commit is contained in:
Hans Verkuil 2019-04-04 09:15:00 -04:00 committed by Mauro Carvalho Chehab
parent 878344de61
commit 707947247e
5 changed files with 6 additions and 15 deletions

View File

@ -475,8 +475,7 @@ static void *vb2_dc_get_userptr(struct device *dev, unsigned long vaddr,
buf->dma_dir = dma_dir; buf->dma_dir = dma_dir;
offset = lower_32_bits(offset_in_page(vaddr)); offset = lower_32_bits(offset_in_page(vaddr));
vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE || vec = vb2_create_framevec(vaddr, size);
dma_dir == DMA_BIDIRECTIONAL);
if (IS_ERR(vec)) { if (IS_ERR(vec)) {
ret = PTR_ERR(vec); ret = PTR_ERR(vec);
goto fail_buf; goto fail_buf;

View File

@ -239,8 +239,7 @@ static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr,
buf->offset = vaddr & ~PAGE_MASK; buf->offset = vaddr & ~PAGE_MASK;
buf->size = size; buf->size = size;
buf->dma_sgt = &buf->sg_table; buf->dma_sgt = &buf->sg_table;
vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE || vec = vb2_create_framevec(vaddr, size);
dma_dir == DMA_BIDIRECTIONAL);
if (IS_ERR(vec)) if (IS_ERR(vec))
goto userptr_fail_pfnvec; goto userptr_fail_pfnvec;
buf->vec = vec; buf->vec = vec;

View File

@ -26,7 +26,6 @@
* vb2_create_framevec() - map virtual addresses to pfns * vb2_create_framevec() - map virtual addresses to pfns
* @start: Virtual user address where we start mapping * @start: Virtual user address where we start mapping
* @length: Length of a range to map * @length: Length of a range to map
* @write: Should we map for writing into the area
* *
* This function allocates and fills in a vector with pfns corresponding to * This function allocates and fills in a vector with pfns corresponding to
* virtual address range passed in arguments. If pfns have corresponding pages, * virtual address range passed in arguments. If pfns have corresponding pages,
@ -35,17 +34,13 @@
* failure. Returned vector needs to be freed via vb2_destroy_pfnvec(). * failure. Returned vector needs to be freed via vb2_destroy_pfnvec().
*/ */
struct frame_vector *vb2_create_framevec(unsigned long start, struct frame_vector *vb2_create_framevec(unsigned long start,
unsigned long length, unsigned long length)
bool write)
{ {
int ret; int ret;
unsigned long first, last; unsigned long first, last;
unsigned long nr; unsigned long nr;
struct frame_vector *vec; struct frame_vector *vec;
unsigned int flags = FOLL_FORCE; unsigned int flags = FOLL_FORCE | FOLL_WRITE;
if (write)
flags |= FOLL_WRITE;
first = start >> PAGE_SHIFT; first = start >> PAGE_SHIFT;
last = (start + length - 1) >> PAGE_SHIFT; last = (start + length - 1) >> PAGE_SHIFT;

View File

@ -87,8 +87,7 @@ static void *vb2_vmalloc_get_userptr(struct device *dev, 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 || vec = vb2_create_framevec(vaddr, size);
dma_dir == DMA_BIDIRECTIONAL);
if (IS_ERR(vec)) { if (IS_ERR(vec)) {
ret = PTR_ERR(vec); ret = PTR_ERR(vec);
goto fail_pfnvec_create; goto fail_pfnvec_create;

View File

@ -34,8 +34,7 @@ struct vb2_vmarea_handler {
extern const struct vm_operations_struct vb2_common_vm_ops; extern const struct vm_operations_struct vb2_common_vm_ops;
struct frame_vector *vb2_create_framevec(unsigned long start, struct frame_vector *vb2_create_framevec(unsigned long start,
unsigned long length, unsigned long length);
bool write);
void vb2_destroy_framevec(struct frame_vector *vec); void vb2_destroy_framevec(struct frame_vector *vec);
#endif #endif