2010-10-11 21:56:41 +08:00
|
|
|
/*
|
2015-09-22 21:30:29 +08:00
|
|
|
* videobuf2-core.c - video buffer 2 core framework
|
2010-10-11 21:56:41 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Samsung Electronics
|
|
|
|
*
|
2011-03-14 02:23:32 +08:00
|
|
|
* Author: Pawel Osciak <pawel@osciak.com>
|
2010-10-11 21:56:41 +08:00
|
|
|
* Marek Szyprowski <m.szyprowski@samsung.com>
|
|
|
|
*
|
2014-04-14 18:33:00 +08:00
|
|
|
* The vb2_thread implementation was based on code from videobuf-dvb.c:
|
|
|
|
* (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
|
|
|
|
*
|
2010-10-11 21:56:41 +08:00
|
|
|
* 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/err.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/sched.h>
|
2014-04-14 18:33:00 +08:00
|
|
|
#include <linux/freezer.h>
|
|
|
|
#include <linux/kthread.h>
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2015-10-06 17:37:49 +08:00
|
|
|
#include <media/videobuf2-core.h>
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
#include <trace/events/vb2.h>
|
2015-07-10 21:49:26 +08:00
|
|
|
|
2015-10-06 17:37:49 +08:00
|
|
|
#include "videobuf2-internal.h"
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2015-10-06 17:37:49 +08:00
|
|
|
int vb2_debug;
|
|
|
|
EXPORT_SYMBOL_GPL(vb2_debug);
|
|
|
|
module_param_named(debug, vb2_debug, int, 0644);
|
2011-04-12 21:14:13 +08:00
|
|
|
|
2014-02-28 23:49:18 +08:00
|
|
|
static void __vb2_queue_cancel(struct vb2_queue *q);
|
2015-01-20 23:18:16 +08:00
|
|
|
static void __enqueue_in_driver(struct vb2_buffer *vb);
|
2014-02-28 23:49:18 +08:00
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
/**
|
|
|
|
* __vb2_buf_mem_alloc() - allocate video memory for the given buffer
|
|
|
|
*/
|
2011-08-24 17:36:26 +08:00
|
|
|
static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
|
2010-10-11 21:56:41 +08:00
|
|
|
{
|
|
|
|
struct vb2_queue *q = vb->vb2_queue;
|
2014-11-18 20:50:59 +08:00
|
|
|
enum dma_data_direction dma_dir =
|
2015-10-06 17:37:47 +08:00
|
|
|
q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
|
2010-10-11 21:56:41 +08:00
|
|
|
void *mem_priv;
|
|
|
|
int plane;
|
|
|
|
|
2013-04-19 18:18:01 +08:00
|
|
|
/*
|
|
|
|
* Allocate memory for all planes in this buffer
|
|
|
|
* NOTE: mmapped areas should be page aligned
|
|
|
|
*/
|
2010-10-11 21:56:41 +08:00
|
|
|
for (plane = 0; plane < vb->num_planes; ++plane) {
|
2013-04-19 18:18:01 +08:00
|
|
|
unsigned long size = PAGE_ALIGN(q->plane_sizes[plane]);
|
|
|
|
|
2014-03-17 20:54:21 +08:00
|
|
|
mem_priv = call_ptr_memop(vb, alloc, q->alloc_ctx[plane],
|
2014-11-18 20:50:59 +08:00
|
|
|
size, dma_dir, q->gfp_flags);
|
2011-03-22 20:24:58 +08:00
|
|
|
if (IS_ERR_OR_NULL(mem_priv))
|
2010-10-11 21:56:41 +08:00
|
|
|
goto free;
|
|
|
|
|
|
|
|
/* Associate allocator private data with this plane */
|
|
|
|
vb->planes[plane].mem_priv = mem_priv;
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
vb->planes[plane].length = q->plane_sizes[plane];
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
free:
|
|
|
|
/* Free already allocated memory if one of the allocations failed */
|
2011-12-15 16:53:06 +08:00
|
|
|
for (; plane > 0; --plane) {
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_memop(vb, put, vb->planes[plane - 1].mem_priv);
|
2011-12-15 16:53:06 +08:00
|
|
|
vb->planes[plane - 1].mem_priv = NULL;
|
|
|
|
}
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __vb2_buf_mem_free() - free memory of the given buffer
|
|
|
|
*/
|
|
|
|
static void __vb2_buf_mem_free(struct vb2_buffer *vb)
|
|
|
|
{
|
|
|
|
unsigned int plane;
|
|
|
|
|
|
|
|
for (plane = 0; plane < vb->num_planes; ++plane) {
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_memop(vb, put, vb->planes[plane].mem_priv);
|
2010-10-11 21:56:41 +08:00
|
|
|
vb->planes[plane].mem_priv = NULL;
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
dprintk(3, "freed plane %d of buffer %d\n", plane, vb->index);
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __vb2_buf_userptr_put() - release userspace memory associated with
|
|
|
|
* a USERPTR buffer
|
|
|
|
*/
|
|
|
|
static void __vb2_buf_userptr_put(struct vb2_buffer *vb)
|
|
|
|
{
|
|
|
|
unsigned int plane;
|
|
|
|
|
|
|
|
for (plane = 0; plane < vb->num_planes; ++plane) {
|
2011-12-15 16:53:06 +08:00
|
|
|
if (vb->planes[plane].mem_priv)
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
|
2011-12-15 16:53:06 +08:00
|
|
|
vb->planes[plane].mem_priv = NULL;
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-14 21:37:37 +08:00
|
|
|
/**
|
|
|
|
* __vb2_plane_dmabuf_put() - release memory associated with
|
|
|
|
* a DMABUF shared plane
|
|
|
|
*/
|
2014-01-29 22:53:25 +08:00
|
|
|
static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p)
|
2012-06-14 21:37:37 +08:00
|
|
|
{
|
|
|
|
if (!p->mem_priv)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (p->dbuf_mapped)
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_memop(vb, unmap_dmabuf, p->mem_priv);
|
2012-06-14 21:37:37 +08:00
|
|
|
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_memop(vb, detach_dmabuf, p->mem_priv);
|
2012-06-14 21:37:37 +08:00
|
|
|
dma_buf_put(p->dbuf);
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
p->mem_priv = NULL;
|
|
|
|
p->dbuf = NULL;
|
|
|
|
p->dbuf_mapped = 0;
|
2012-06-14 21:37:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __vb2_buf_dmabuf_put() - release memory associated with
|
|
|
|
* a DMABUF shared buffer
|
|
|
|
*/
|
|
|
|
static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb)
|
|
|
|
{
|
|
|
|
unsigned int plane;
|
|
|
|
|
|
|
|
for (plane = 0; plane < vb->num_planes; ++plane)
|
2014-01-29 22:53:25 +08:00
|
|
|
__vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
|
2012-06-14 21:37:37 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 22:14:05 +08:00
|
|
|
/**
|
|
|
|
* __setup_lengths() - setup initial lengths for every plane in
|
|
|
|
* every buffer on the queue
|
|
|
|
*/
|
|
|
|
static void __setup_lengths(struct vb2_queue *q, unsigned int n)
|
|
|
|
{
|
|
|
|
unsigned int buffer, plane;
|
|
|
|
struct vb2_buffer *vb;
|
|
|
|
|
|
|
|
for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
|
|
|
|
vb = q->bufs[buffer];
|
|
|
|
if (!vb)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (plane = 0; plane < vb->num_planes; ++plane)
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
vb->planes[plane].length = q->plane_sizes[plane];
|
2013-12-04 22:14:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
/**
|
|
|
|
* __setup_offsets() - setup unique offsets ("cookies") for every plane in
|
|
|
|
* every buffer on the queue
|
|
|
|
*/
|
2011-09-28 20:23:02 +08:00
|
|
|
static void __setup_offsets(struct vb2_queue *q, unsigned int n)
|
2010-10-11 21:56:41 +08:00
|
|
|
{
|
|
|
|
unsigned int buffer, plane;
|
|
|
|
struct vb2_buffer *vb;
|
2011-09-28 20:23:02 +08:00
|
|
|
unsigned long off;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2011-09-28 20:23:02 +08:00
|
|
|
if (q->num_buffers) {
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
struct vb2_plane *p;
|
2011-09-28 20:23:02 +08:00
|
|
|
vb = q->bufs[q->num_buffers - 1];
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
p = &vb->planes[vb->num_planes - 1];
|
|
|
|
off = PAGE_ALIGN(p->m.offset + p->length);
|
2011-09-28 20:23:02 +08:00
|
|
|
} else {
|
|
|
|
off = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (buffer = q->num_buffers; buffer < q->num_buffers + n; ++buffer) {
|
2010-10-11 21:56:41 +08:00
|
|
|
vb = q->bufs[buffer];
|
|
|
|
if (!vb)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (plane = 0; plane < vb->num_planes; ++plane) {
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
vb->planes[plane].m.offset = off;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(3, "buffer %d, plane %d offset 0x%08lx\n",
|
2010-10-11 21:56:41 +08:00
|
|
|
buffer, plane, off);
|
|
|
|
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
off += vb->planes[plane].length;
|
2010-10-11 21:56:41 +08:00
|
|
|
off = PAGE_ALIGN(off);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __vb2_queue_alloc() - allocate videobuf buffer structures and (for MMAP type)
|
|
|
|
* video buffer memory for all buffers/planes on the queue and initializes the
|
|
|
|
* queue
|
|
|
|
*
|
|
|
|
* Returns the number of buffers successfully allocated.
|
|
|
|
*/
|
2015-10-06 17:37:47 +08:00
|
|
|
static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory,
|
2011-08-24 17:36:26 +08:00
|
|
|
unsigned int num_buffers, unsigned int num_planes)
|
2010-10-11 21:56:41 +08:00
|
|
|
{
|
|
|
|
unsigned int buffer;
|
|
|
|
struct vb2_buffer *vb;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
for (buffer = 0; buffer < num_buffers; ++buffer) {
|
|
|
|
/* Allocate videobuf buffer structures */
|
|
|
|
vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
|
|
|
|
if (!vb) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "memory alloc for buffer struct failed\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
vb->state = VB2_BUF_STATE_DEQUEUED;
|
|
|
|
vb->vb2_queue = q;
|
|
|
|
vb->num_planes = num_planes;
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
vb->index = q->num_buffers + buffer;
|
|
|
|
vb->type = q->type;
|
|
|
|
vb->memory = memory;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
/* Allocate video buffer memory for the MMAP type */
|
2015-10-06 17:37:47 +08:00
|
|
|
if (memory == VB2_MEMORY_MMAP) {
|
2011-08-24 17:36:26 +08:00
|
|
|
ret = __vb2_buf_mem_alloc(vb);
|
2010-10-11 21:56:41 +08:00
|
|
|
if (ret) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "failed allocating memory for "
|
2010-10-11 21:56:41 +08:00
|
|
|
"buffer %d\n", buffer);
|
|
|
|
kfree(vb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Call the driver-provided buffer initialization
|
|
|
|
* callback, if given. An error in initialization
|
|
|
|
* results in queue setup failure.
|
|
|
|
*/
|
2014-01-29 22:53:25 +08:00
|
|
|
ret = call_vb_qop(vb, buf_init, vb);
|
2010-10-11 21:56:41 +08:00
|
|
|
if (ret) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "buffer %d %p initialization"
|
2010-10-11 21:56:41 +08:00
|
|
|
" failed\n", buffer, vb);
|
|
|
|
__vb2_buf_mem_free(vb);
|
|
|
|
kfree(vb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 20:23:02 +08:00
|
|
|
q->bufs[q->num_buffers + buffer] = vb;
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 22:14:05 +08:00
|
|
|
__setup_lengths(q, buffer);
|
2015-10-06 17:37:47 +08:00
|
|
|
if (memory == VB2_MEMORY_MMAP)
|
2013-09-19 15:37:29 +08:00
|
|
|
__setup_offsets(q, buffer);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "allocated %d buffers, %d plane(s) each\n",
|
2011-09-28 20:23:02 +08:00
|
|
|
buffer, num_planes);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __vb2_free_mem() - release all video buffer memory for a given queue
|
|
|
|
*/
|
2011-09-28 20:23:02 +08:00
|
|
|
static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers)
|
2010-10-11 21:56:41 +08:00
|
|
|
{
|
|
|
|
unsigned int buffer;
|
|
|
|
struct vb2_buffer *vb;
|
|
|
|
|
2011-09-28 20:23:02 +08:00
|
|
|
for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
|
|
|
|
++buffer) {
|
2010-10-11 21:56:41 +08:00
|
|
|
vb = q->bufs[buffer];
|
|
|
|
if (!vb)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Free MMAP buffers or release USERPTR buffers */
|
2015-10-06 17:37:47 +08:00
|
|
|
if (q->memory == VB2_MEMORY_MMAP)
|
2010-10-11 21:56:41 +08:00
|
|
|
__vb2_buf_mem_free(vb);
|
2015-10-06 17:37:47 +08:00
|
|
|
else if (q->memory == VB2_MEMORY_DMABUF)
|
2012-06-14 21:37:37 +08:00
|
|
|
__vb2_buf_dmabuf_put(vb);
|
2010-10-11 21:56:41 +08:00
|
|
|
else
|
|
|
|
__vb2_buf_userptr_put(vb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-09-28 20:23:02 +08:00
|
|
|
* __vb2_queue_free() - free buffers at the end of the queue - video memory and
|
|
|
|
* related information, if no buffers are left return the queue to an
|
|
|
|
* uninitialized state. Might be called even if the queue has already been freed.
|
2010-10-11 21:56:41 +08:00
|
|
|
*/
|
2013-12-14 00:13:40 +08:00
|
|
|
static int __vb2_queue_free(struct vb2_queue *q, unsigned int buffers)
|
2010-10-11 21:56:41 +08:00
|
|
|
{
|
|
|
|
unsigned int buffer;
|
|
|
|
|
2013-12-14 00:13:40 +08:00
|
|
|
/*
|
|
|
|
* Sanity check: when preparing a buffer the queue lock is released for
|
|
|
|
* a short while (see __buf_prepare for the details), which would allow
|
|
|
|
* a race with a reqbufs which can call this function. Removing the
|
|
|
|
* buffers from underneath __buf_prepare is obviously a bad idea, so we
|
|
|
|
* check if any of the buffers is in the state PREPARING, and if so we
|
|
|
|
* just return -EAGAIN.
|
|
|
|
*/
|
|
|
|
for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
|
|
|
|
++buffer) {
|
|
|
|
if (q->bufs[buffer] == NULL)
|
|
|
|
continue;
|
|
|
|
if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "preparing buffers, cannot free\n");
|
2013-12-14 00:13:40 +08:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
/* Call driver-provided cleanup function for each buffer, if provided */
|
2014-01-29 22:53:25 +08:00
|
|
|
for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
|
|
|
|
++buffer) {
|
2014-01-30 00:36:53 +08:00
|
|
|
struct vb2_buffer *vb = q->bufs[buffer];
|
|
|
|
|
|
|
|
if (vb && vb->planes[0].mem_priv)
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_vb_qop(vb, buf_cleanup, vb);
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Release video buffer memory */
|
2011-09-28 20:23:02 +08:00
|
|
|
__vb2_free_mem(q, buffers);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2014-01-29 22:53:25 +08:00
|
|
|
#ifdef CONFIG_VIDEO_ADV_DEBUG
|
|
|
|
/*
|
|
|
|
* Check that all the calls were balances during the life-time of this
|
|
|
|
* queue. If not (or if the debug level is 1 or up), then dump the
|
|
|
|
* counters to the kernel log.
|
|
|
|
*/
|
|
|
|
if (q->num_buffers) {
|
|
|
|
bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming ||
|
|
|
|
q->cnt_wait_prepare != q->cnt_wait_finish;
|
|
|
|
|
2015-10-06 17:37:49 +08:00
|
|
|
if (unbalanced || vb2_debug) {
|
2014-01-29 22:53:25 +08:00
|
|
|
pr_info("vb2: counters for queue %p:%s\n", q,
|
|
|
|
unbalanced ? " UNBALANCED!" : "");
|
|
|
|
pr_info("vb2: setup: %u start_streaming: %u stop_streaming: %u\n",
|
|
|
|
q->cnt_queue_setup, q->cnt_start_streaming,
|
|
|
|
q->cnt_stop_streaming);
|
|
|
|
pr_info("vb2: wait_prepare: %u wait_finish: %u\n",
|
|
|
|
q->cnt_wait_prepare, q->cnt_wait_finish);
|
|
|
|
}
|
|
|
|
q->cnt_queue_setup = 0;
|
|
|
|
q->cnt_wait_prepare = 0;
|
|
|
|
q->cnt_wait_finish = 0;
|
|
|
|
q->cnt_start_streaming = 0;
|
|
|
|
q->cnt_stop_streaming = 0;
|
|
|
|
}
|
|
|
|
for (buffer = 0; buffer < q->num_buffers; ++buffer) {
|
|
|
|
struct vb2_buffer *vb = q->bufs[buffer];
|
|
|
|
bool unbalanced = vb->cnt_mem_alloc != vb->cnt_mem_put ||
|
|
|
|
vb->cnt_mem_prepare != vb->cnt_mem_finish ||
|
|
|
|
vb->cnt_mem_get_userptr != vb->cnt_mem_put_userptr ||
|
|
|
|
vb->cnt_mem_attach_dmabuf != vb->cnt_mem_detach_dmabuf ||
|
|
|
|
vb->cnt_mem_map_dmabuf != vb->cnt_mem_unmap_dmabuf ||
|
|
|
|
vb->cnt_buf_queue != vb->cnt_buf_done ||
|
|
|
|
vb->cnt_buf_prepare != vb->cnt_buf_finish ||
|
|
|
|
vb->cnt_buf_init != vb->cnt_buf_cleanup;
|
|
|
|
|
2015-10-06 17:37:49 +08:00
|
|
|
if (unbalanced || vb2_debug) {
|
2014-01-29 22:53:25 +08:00
|
|
|
pr_info("vb2: counters for queue %p, buffer %d:%s\n",
|
|
|
|
q, buffer, unbalanced ? " UNBALANCED!" : "");
|
|
|
|
pr_info("vb2: buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
|
|
|
|
vb->cnt_buf_init, vb->cnt_buf_cleanup,
|
|
|
|
vb->cnt_buf_prepare, vb->cnt_buf_finish);
|
|
|
|
pr_info("vb2: buf_queue: %u buf_done: %u\n",
|
|
|
|
vb->cnt_buf_queue, vb->cnt_buf_done);
|
|
|
|
pr_info("vb2: alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
|
|
|
|
vb->cnt_mem_alloc, vb->cnt_mem_put,
|
|
|
|
vb->cnt_mem_prepare, vb->cnt_mem_finish,
|
|
|
|
vb->cnt_mem_mmap);
|
|
|
|
pr_info("vb2: get_userptr: %u put_userptr: %u\n",
|
|
|
|
vb->cnt_mem_get_userptr, vb->cnt_mem_put_userptr);
|
|
|
|
pr_info("vb2: attach_dmabuf: %u detach_dmabuf: %u map_dmabuf: %u unmap_dmabuf: %u\n",
|
|
|
|
vb->cnt_mem_attach_dmabuf, vb->cnt_mem_detach_dmabuf,
|
|
|
|
vb->cnt_mem_map_dmabuf, vb->cnt_mem_unmap_dmabuf);
|
|
|
|
pr_info("vb2: get_dmabuf: %u num_users: %u vaddr: %u cookie: %u\n",
|
|
|
|
vb->cnt_mem_get_dmabuf,
|
|
|
|
vb->cnt_mem_num_users,
|
|
|
|
vb->cnt_mem_vaddr,
|
|
|
|
vb->cnt_mem_cookie);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
/* Free videobuf buffers */
|
2011-09-28 20:23:02 +08:00
|
|
|
for (buffer = q->num_buffers - buffers; buffer < q->num_buffers;
|
|
|
|
++buffer) {
|
2010-10-11 21:56:41 +08:00
|
|
|
kfree(q->bufs[buffer]);
|
|
|
|
q->bufs[buffer] = NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-28 20:23:02 +08:00
|
|
|
q->num_buffers -= buffers;
|
2014-02-25 00:41:20 +08:00
|
|
|
if (!q->num_buffers) {
|
2011-09-28 20:23:02 +08:00
|
|
|
q->memory = 0;
|
2014-02-25 00:41:20 +08:00
|
|
|
INIT_LIST_HEAD(&q->queued_list);
|
|
|
|
}
|
2013-12-14 00:13:40 +08:00
|
|
|
return 0;
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
|
2011-08-24 17:49:35 +08:00
|
|
|
/**
|
2015-10-06 17:37:48 +08:00
|
|
|
* vb2_buffer_in_use() - return true if the buffer is in use and
|
2011-08-24 17:49:35 +08:00
|
|
|
* the queue cannot be freed (by the means of REQBUFS(0)) call
|
|
|
|
*/
|
2015-10-06 17:37:49 +08:00
|
|
|
bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb)
|
2011-08-24 17:49:35 +08:00
|
|
|
{
|
|
|
|
unsigned int plane;
|
|
|
|
for (plane = 0; plane < vb->num_planes; ++plane) {
|
2011-10-13 00:09:53 +08:00
|
|
|
void *mem_priv = vb->planes[plane].mem_priv;
|
2011-08-24 17:49:35 +08:00
|
|
|
/*
|
|
|
|
* If num_users() has not been provided, call_memop
|
|
|
|
* will return 0, apparently nobody cares about this
|
|
|
|
* case anyway. If num_users() returns more than 1,
|
|
|
|
* we are not the only user of the plane's memory.
|
|
|
|
*/
|
2014-01-29 22:53:25 +08:00
|
|
|
if (mem_priv && call_memop(vb, num_users, mem_priv) > 1)
|
2011-08-24 17:49:35 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-06 17:37:49 +08:00
|
|
|
EXPORT_SYMBOL(vb2_buffer_in_use);
|
2011-08-24 17:49:35 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* __buffers_in_use() - return true if any buffers on the queue are in use and
|
|
|
|
* the queue cannot be freed (by the means of REQBUFS(0)) call
|
|
|
|
*/
|
|
|
|
static bool __buffers_in_use(struct vb2_queue *q)
|
|
|
|
{
|
|
|
|
unsigned int buffer;
|
|
|
|
for (buffer = 0; buffer < q->num_buffers; ++buffer) {
|
2015-10-06 17:37:48 +08:00
|
|
|
if (vb2_buffer_in_use(q, q->bufs[buffer]))
|
2011-08-24 17:49:35 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
/**
|
|
|
|
* vb2_core_querybuf() - query video buffer information
|
|
|
|
* @q: videobuf queue
|
|
|
|
* @index: id number of the buffer
|
|
|
|
* @pb: buffer struct passed from userspace
|
|
|
|
*
|
|
|
|
* Should be called from vidioc_querybuf ioctl handler in driver.
|
|
|
|
* The passed buffer should have been verified.
|
|
|
|
* This function fills the relevant information for the userspace.
|
|
|
|
*
|
|
|
|
* The return values from this function are intended to be directly returned
|
|
|
|
* from vidioc_querybuf handler in driver.
|
|
|
|
*/
|
2015-10-06 17:37:49 +08:00
|
|
|
int vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb)
|
2015-10-06 17:37:48 +08:00
|
|
|
{
|
|
|
|
return call_bufop(q, fill_user_buffer, q->bufs[index], pb);
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
2015-10-06 17:37:49 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vb2_core_querybuf);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* __verify_userptr_ops() - verify that all memory operations required for
|
|
|
|
* USERPTR queue type have been provided
|
|
|
|
*/
|
|
|
|
static int __verify_userptr_ops(struct vb2_queue *q)
|
|
|
|
{
|
|
|
|
if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr ||
|
|
|
|
!q->mem_ops->put_userptr)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __verify_mmap_ops() - verify that all memory operations required for
|
|
|
|
* MMAP queue type have been provided
|
|
|
|
*/
|
|
|
|
static int __verify_mmap_ops(struct vb2_queue *q)
|
|
|
|
{
|
|
|
|
if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc ||
|
|
|
|
!q->mem_ops->put || !q->mem_ops->mmap)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-14 21:37:37 +08:00
|
|
|
/**
|
|
|
|
* __verify_dmabuf_ops() - verify that all memory operations required for
|
|
|
|
* DMABUF queue type have been provided
|
|
|
|
*/
|
|
|
|
static int __verify_dmabuf_ops(struct vb2_queue *q)
|
|
|
|
{
|
|
|
|
if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf ||
|
|
|
|
!q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf ||
|
|
|
|
!q->mem_ops->unmap_dmabuf)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
/**
|
2015-10-06 17:37:48 +08:00
|
|
|
* vb2_verify_memory_type() - Check whether the memory type and buffer type
|
2012-06-28 04:10:30 +08:00
|
|
|
* passed to a buffer operation are compatible with the queue.
|
|
|
|
*/
|
2015-10-06 17:37:49 +08:00
|
|
|
int vb2_verify_memory_type(struct vb2_queue *q,
|
2015-10-06 17:37:47 +08:00
|
|
|
enum vb2_memory memory, unsigned int type)
|
2012-06-28 04:10:30 +08:00
|
|
|
{
|
2015-10-06 17:37:47 +08:00
|
|
|
if (memory != VB2_MEMORY_MMAP && memory != VB2_MEMORY_USERPTR &&
|
|
|
|
memory != VB2_MEMORY_DMABUF) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "unsupported memory type\n");
|
2012-06-28 04:10:30 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type != q->type) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "requested type is incorrect\n");
|
2012-06-28 04:10:30 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure all the required memory ops for given memory type
|
|
|
|
* are available.
|
|
|
|
*/
|
2015-10-06 17:37:47 +08:00
|
|
|
if (memory == VB2_MEMORY_MMAP && __verify_mmap_ops(q)) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "MMAP for current setup unsupported\n");
|
2012-06-28 04:10:30 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:47 +08:00
|
|
|
if (memory == VB2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "USERPTR for current setup unsupported\n");
|
2012-06-28 04:10:30 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:47 +08:00
|
|
|
if (memory == VB2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "DMABUF for current setup unsupported\n");
|
2012-06-14 21:37:37 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2012-06-28 04:10:30 +08:00
|
|
|
/*
|
|
|
|
* Place the busy tests at the end: -EBUSY can be ignored when
|
|
|
|
* create_bufs is called with count == 0, but count == 0 should still
|
|
|
|
* do the memory and type validation.
|
|
|
|
*/
|
2014-04-07 20:23:50 +08:00
|
|
|
if (vb2_fileio_is_active(q)) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "file io in progress\n");
|
2012-06-28 04:10:30 +08:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-06 17:37:49 +08:00
|
|
|
EXPORT_SYMBOL(vb2_verify_memory_type);
|
2012-06-28 04:10:30 +08:00
|
|
|
|
|
|
|
/**
|
2015-10-06 17:37:48 +08:00
|
|
|
* vb2_core_reqbufs() - Initiate streaming
|
2010-10-11 21:56:41 +08:00
|
|
|
* @q: videobuf2 queue
|
2015-10-06 17:37:48 +08:00
|
|
|
* @memory: memory type
|
|
|
|
* @count: requested buffer count
|
2010-10-11 21:56:41 +08:00
|
|
|
*
|
|
|
|
* Should be called from vidioc_reqbufs ioctl handler of a driver.
|
|
|
|
* This function:
|
|
|
|
* 1) verifies streaming parameters passed from the userspace,
|
|
|
|
* 2) sets up the queue,
|
|
|
|
* 3) negotiates number of buffers and planes per buffer with the driver
|
|
|
|
* to be used during streaming,
|
|
|
|
* 4) allocates internal buffer structures (struct vb2_buffer), according to
|
|
|
|
* the agreed parameters,
|
|
|
|
* 5) for MMAP memory type, allocates actual video memory, using the
|
|
|
|
* memory handling/allocation routines provided during queue initialization
|
|
|
|
*
|
|
|
|
* If req->count is 0, all the memory will be freed instead.
|
|
|
|
* If the queue has been allocated previously (by a previous vb2_reqbufs) call
|
|
|
|
* and the queue is not busy, memory will be reallocated.
|
|
|
|
*
|
|
|
|
* The return values from this function are intended to be directly returned
|
|
|
|
* from vidioc_reqbufs handler in driver.
|
|
|
|
*/
|
2015-10-06 17:37:49 +08:00
|
|
|
int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
|
2015-10-06 17:37:48 +08:00
|
|
|
unsigned int *count)
|
2010-10-11 21:56:41 +08:00
|
|
|
{
|
2011-09-28 20:23:02 +08:00
|
|
|
unsigned int num_buffers, allocated_buffers, num_planes = 0;
|
2012-06-28 04:10:30 +08:00
|
|
|
int ret;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
if (q->streaming) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "streaming active\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
if (*count == 0 || q->num_buffers != 0 || q->memory != memory) {
|
2010-10-11 21:56:41 +08:00
|
|
|
/*
|
|
|
|
* We already have buffers allocated, so first check if they
|
|
|
|
* are not in use and can be freed.
|
|
|
|
*/
|
[media] videobuf2: fix lockdep warning
The following lockdep warning has been there ever since commit a517cca6b24fc54ac209e44118ec8962051662e3
one year ago:
[ 403.117947] ======================================================
[ 403.117949] [ INFO: possible circular locking dependency detected ]
[ 403.117953] 3.16.0-rc6-test-media #961 Not tainted
[ 403.117954] -------------------------------------------------------
[ 403.117956] v4l2-ctl/15377 is trying to acquire lock:
[ 403.117959] (&dev->mutex#3){+.+.+.}, at: [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.117974]
[ 403.117974] but task is already holding lock:
[ 403.117976] (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.117987]
[ 403.117987] which lock already depends on the new lock.
[ 403.117987]
[ 403.117990]
[ 403.117990] the existing dependency chain (in reverse order) is:
[ 403.117992]
[ 403.117992] -> #1 (&mm->mmap_sem){++++++}:
[ 403.117997] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118006] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118010] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118014] [<ffffffff8118c9ec>] might_fault+0x7c/0xb0
[ 403.118018] [<ffffffffa0028a25>] video_usercopy+0x425/0x610 [videodev]
[ 403.118028] [<ffffffffa0028c25>] video_ioctl2+0x15/0x20 [videodev]
[ 403.118034] [<ffffffffa0022764>] v4l2_ioctl+0x184/0x1a0 [videodev]
[ 403.118040] [<ffffffff811d77d0>] do_vfs_ioctl+0x2f0/0x4f0
[ 403.118307] [<ffffffff811d7a51>] SyS_ioctl+0x81/0xa0
[ 403.118311] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118319]
[ 403.118319] -> #0 (&dev->mutex#3){+.+.+.}:
[ 403.118324] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118329] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118333] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118336] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118340] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118344] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118349] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118354] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118359] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118363] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118366] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118369] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118376] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118381]
[ 403.118381] other info that might help us debug this:
[ 403.118381]
[ 403.118383] Possible unsafe locking scenario:
[ 403.118383]
[ 403.118385] CPU0 CPU1
[ 403.118387] ---- ----
[ 403.118388] lock(&mm->mmap_sem);
[ 403.118391] lock(&dev->mutex#3);
[ 403.118394] lock(&mm->mmap_sem);
[ 403.118397] lock(&dev->mutex#3);
[ 403.118400]
[ 403.118400] *** DEADLOCK ***
[ 403.118400]
[ 403.118403] 1 lock held by v4l2-ctl/15377:
[ 403.118405] #0: (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.118411]
[ 403.118411] stack backtrace:
[ 403.118415] CPU: 0 PID: 15377 Comm: v4l2-ctl Not tainted 3.16.0-rc6-test-media #961
[ 403.118418] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/31/2013
[ 403.118420] ffffffff82a6c9d0 ffff8800af37fb00 ffffffff819916a2 ffffffff82a6c9d0
[ 403.118425] ffff8800af37fb40 ffffffff810d5715 ffff8802308e4200 0000000000000000
[ 403.118429] ffff8802308e4a48 ffff8802308e4a48 ffff8802308e4200 0000000000000001
[ 403.118433] Call Trace:
[ 403.118441] [<ffffffff819916a2>] dump_stack+0x4e/0x7a
[ 403.118445] [<ffffffff810d5715>] print_circular_bug+0x1d5/0x2a0
[ 403.118449] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118455] [<ffffffff8119c172>] ? find_vmap_area+0x42/0x70
[ 403.118459] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118463] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118468] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118472] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118476] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118480] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118484] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118488] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118493] [<ffffffff810d8055>] ? mark_held_locks+0x75/0xa0
[ 403.118497] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118502] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118506] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118510] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118513] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118517] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118521] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118525] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
The reason is that vb2_fop_mmap and vb2_fop_get_unmapped_area take the core lock
while they are called with the mmap_sem semaphore held. But elsewhere in the code
the core lock is taken first but calls to copy_to/from_user() can take the mmap_sem
semaphore as well, potentially causing a classical A-B/B-A deadlock.
However, the mmap/get_unmapped_area calls really shouldn't take the core lock
at all. So what would happen if they don't take the core lock anymore?
There are two situations that need to be taken into account: calling mmap while
new buffers are being added and calling mmap while buffers are being deleted.
The first case works almost fine without a lock: in all cases mmap relies on
correctly filled-in q->num_buffers/q->num_planes values and those are only
updated by reqbufs and create_buffers *after* any new buffers have been
initialized completely. Except in one case: if an error occurred while allocating
the buffers it will increase num_buffers and rely on __vb2_queue_free to
decrease it again. So there is a short period where the buffer information
may be wrong.
The second case definitely does pose a problem: buffers may be in the process
of being deleted, without the internal structure being updated.
In order to fix this a new mutex is added to vb2_queue that is taken when
buffers are allocated or deleted, and in vb2_mmap. That way vb2_mmap won't
get stale buffer data. Note that this is a problem only for MEMORY_MMAP, so
even though __qbuf_userptr and __qbuf_dmabuf also mess around with buffers
(mem_priv in particular), this doesn't clash with vb2_mmap or
vb2_get_unmapped_area since those are MMAP specific.
As an additional bonus the hack in __buf_prepare, the USERPTR case, can be
removed as well since mmap() no longer takes the core lock.
All in all a much cleaner solution.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2014-08-07 14:47:14 +08:00
|
|
|
mutex_lock(&q->mmap_lock);
|
2015-10-06 17:37:47 +08:00
|
|
|
if (q->memory == VB2_MEMORY_MMAP && __buffers_in_use(q)) {
|
[media] videobuf2: fix lockdep warning
The following lockdep warning has been there ever since commit a517cca6b24fc54ac209e44118ec8962051662e3
one year ago:
[ 403.117947] ======================================================
[ 403.117949] [ INFO: possible circular locking dependency detected ]
[ 403.117953] 3.16.0-rc6-test-media #961 Not tainted
[ 403.117954] -------------------------------------------------------
[ 403.117956] v4l2-ctl/15377 is trying to acquire lock:
[ 403.117959] (&dev->mutex#3){+.+.+.}, at: [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.117974]
[ 403.117974] but task is already holding lock:
[ 403.117976] (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.117987]
[ 403.117987] which lock already depends on the new lock.
[ 403.117987]
[ 403.117990]
[ 403.117990] the existing dependency chain (in reverse order) is:
[ 403.117992]
[ 403.117992] -> #1 (&mm->mmap_sem){++++++}:
[ 403.117997] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118006] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118010] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118014] [<ffffffff8118c9ec>] might_fault+0x7c/0xb0
[ 403.118018] [<ffffffffa0028a25>] video_usercopy+0x425/0x610 [videodev]
[ 403.118028] [<ffffffffa0028c25>] video_ioctl2+0x15/0x20 [videodev]
[ 403.118034] [<ffffffffa0022764>] v4l2_ioctl+0x184/0x1a0 [videodev]
[ 403.118040] [<ffffffff811d77d0>] do_vfs_ioctl+0x2f0/0x4f0
[ 403.118307] [<ffffffff811d7a51>] SyS_ioctl+0x81/0xa0
[ 403.118311] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118319]
[ 403.118319] -> #0 (&dev->mutex#3){+.+.+.}:
[ 403.118324] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118329] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118333] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118336] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118340] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118344] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118349] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118354] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118359] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118363] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118366] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118369] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118376] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118381]
[ 403.118381] other info that might help us debug this:
[ 403.118381]
[ 403.118383] Possible unsafe locking scenario:
[ 403.118383]
[ 403.118385] CPU0 CPU1
[ 403.118387] ---- ----
[ 403.118388] lock(&mm->mmap_sem);
[ 403.118391] lock(&dev->mutex#3);
[ 403.118394] lock(&mm->mmap_sem);
[ 403.118397] lock(&dev->mutex#3);
[ 403.118400]
[ 403.118400] *** DEADLOCK ***
[ 403.118400]
[ 403.118403] 1 lock held by v4l2-ctl/15377:
[ 403.118405] #0: (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.118411]
[ 403.118411] stack backtrace:
[ 403.118415] CPU: 0 PID: 15377 Comm: v4l2-ctl Not tainted 3.16.0-rc6-test-media #961
[ 403.118418] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/31/2013
[ 403.118420] ffffffff82a6c9d0 ffff8800af37fb00 ffffffff819916a2 ffffffff82a6c9d0
[ 403.118425] ffff8800af37fb40 ffffffff810d5715 ffff8802308e4200 0000000000000000
[ 403.118429] ffff8802308e4a48 ffff8802308e4a48 ffff8802308e4200 0000000000000001
[ 403.118433] Call Trace:
[ 403.118441] [<ffffffff819916a2>] dump_stack+0x4e/0x7a
[ 403.118445] [<ffffffff810d5715>] print_circular_bug+0x1d5/0x2a0
[ 403.118449] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118455] [<ffffffff8119c172>] ? find_vmap_area+0x42/0x70
[ 403.118459] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118463] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118468] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118472] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118476] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118480] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118484] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118488] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118493] [<ffffffff810d8055>] ? mark_held_locks+0x75/0xa0
[ 403.118497] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118502] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118506] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118510] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118513] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118517] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118521] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118525] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
The reason is that vb2_fop_mmap and vb2_fop_get_unmapped_area take the core lock
while they are called with the mmap_sem semaphore held. But elsewhere in the code
the core lock is taken first but calls to copy_to/from_user() can take the mmap_sem
semaphore as well, potentially causing a classical A-B/B-A deadlock.
However, the mmap/get_unmapped_area calls really shouldn't take the core lock
at all. So what would happen if they don't take the core lock anymore?
There are two situations that need to be taken into account: calling mmap while
new buffers are being added and calling mmap while buffers are being deleted.
The first case works almost fine without a lock: in all cases mmap relies on
correctly filled-in q->num_buffers/q->num_planes values and those are only
updated by reqbufs and create_buffers *after* any new buffers have been
initialized completely. Except in one case: if an error occurred while allocating
the buffers it will increase num_buffers and rely on __vb2_queue_free to
decrease it again. So there is a short period where the buffer information
may be wrong.
The second case definitely does pose a problem: buffers may be in the process
of being deleted, without the internal structure being updated.
In order to fix this a new mutex is added to vb2_queue that is taken when
buffers are allocated or deleted, and in vb2_mmap. That way vb2_mmap won't
get stale buffer data. Note that this is a problem only for MEMORY_MMAP, so
even though __qbuf_userptr and __qbuf_dmabuf also mess around with buffers
(mem_priv in particular), this doesn't clash with vb2_mmap or
vb2_get_unmapped_area since those are MMAP specific.
As an additional bonus the hack in __buf_prepare, the USERPTR case, can be
removed as well since mmap() no longer takes the core lock.
All in all a much cleaner solution.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2014-08-07 14:47:14 +08:00
|
|
|
mutex_unlock(&q->mmap_lock);
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "memory in use, cannot free\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2014-02-28 23:49:18 +08:00
|
|
|
/*
|
|
|
|
* Call queue_cancel to clean up any buffers in the PREPARED or
|
|
|
|
* QUEUED state which is possible if buffers were prepared or
|
|
|
|
* queued without ever calling STREAMON.
|
|
|
|
*/
|
|
|
|
__vb2_queue_cancel(q);
|
2013-12-14 00:13:40 +08:00
|
|
|
ret = __vb2_queue_free(q, q->num_buffers);
|
[media] videobuf2: fix lockdep warning
The following lockdep warning has been there ever since commit a517cca6b24fc54ac209e44118ec8962051662e3
one year ago:
[ 403.117947] ======================================================
[ 403.117949] [ INFO: possible circular locking dependency detected ]
[ 403.117953] 3.16.0-rc6-test-media #961 Not tainted
[ 403.117954] -------------------------------------------------------
[ 403.117956] v4l2-ctl/15377 is trying to acquire lock:
[ 403.117959] (&dev->mutex#3){+.+.+.}, at: [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.117974]
[ 403.117974] but task is already holding lock:
[ 403.117976] (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.117987]
[ 403.117987] which lock already depends on the new lock.
[ 403.117987]
[ 403.117990]
[ 403.117990] the existing dependency chain (in reverse order) is:
[ 403.117992]
[ 403.117992] -> #1 (&mm->mmap_sem){++++++}:
[ 403.117997] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118006] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118010] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118014] [<ffffffff8118c9ec>] might_fault+0x7c/0xb0
[ 403.118018] [<ffffffffa0028a25>] video_usercopy+0x425/0x610 [videodev]
[ 403.118028] [<ffffffffa0028c25>] video_ioctl2+0x15/0x20 [videodev]
[ 403.118034] [<ffffffffa0022764>] v4l2_ioctl+0x184/0x1a0 [videodev]
[ 403.118040] [<ffffffff811d77d0>] do_vfs_ioctl+0x2f0/0x4f0
[ 403.118307] [<ffffffff811d7a51>] SyS_ioctl+0x81/0xa0
[ 403.118311] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118319]
[ 403.118319] -> #0 (&dev->mutex#3){+.+.+.}:
[ 403.118324] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118329] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118333] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118336] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118340] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118344] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118349] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118354] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118359] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118363] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118366] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118369] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118376] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118381]
[ 403.118381] other info that might help us debug this:
[ 403.118381]
[ 403.118383] Possible unsafe locking scenario:
[ 403.118383]
[ 403.118385] CPU0 CPU1
[ 403.118387] ---- ----
[ 403.118388] lock(&mm->mmap_sem);
[ 403.118391] lock(&dev->mutex#3);
[ 403.118394] lock(&mm->mmap_sem);
[ 403.118397] lock(&dev->mutex#3);
[ 403.118400]
[ 403.118400] *** DEADLOCK ***
[ 403.118400]
[ 403.118403] 1 lock held by v4l2-ctl/15377:
[ 403.118405] #0: (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.118411]
[ 403.118411] stack backtrace:
[ 403.118415] CPU: 0 PID: 15377 Comm: v4l2-ctl Not tainted 3.16.0-rc6-test-media #961
[ 403.118418] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/31/2013
[ 403.118420] ffffffff82a6c9d0 ffff8800af37fb00 ffffffff819916a2 ffffffff82a6c9d0
[ 403.118425] ffff8800af37fb40 ffffffff810d5715 ffff8802308e4200 0000000000000000
[ 403.118429] ffff8802308e4a48 ffff8802308e4a48 ffff8802308e4200 0000000000000001
[ 403.118433] Call Trace:
[ 403.118441] [<ffffffff819916a2>] dump_stack+0x4e/0x7a
[ 403.118445] [<ffffffff810d5715>] print_circular_bug+0x1d5/0x2a0
[ 403.118449] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118455] [<ffffffff8119c172>] ? find_vmap_area+0x42/0x70
[ 403.118459] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118463] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118468] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118472] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118476] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118480] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118484] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118488] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118493] [<ffffffff810d8055>] ? mark_held_locks+0x75/0xa0
[ 403.118497] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118502] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118506] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118510] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118513] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118517] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118521] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118525] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
The reason is that vb2_fop_mmap and vb2_fop_get_unmapped_area take the core lock
while they are called with the mmap_sem semaphore held. But elsewhere in the code
the core lock is taken first but calls to copy_to/from_user() can take the mmap_sem
semaphore as well, potentially causing a classical A-B/B-A deadlock.
However, the mmap/get_unmapped_area calls really shouldn't take the core lock
at all. So what would happen if they don't take the core lock anymore?
There are two situations that need to be taken into account: calling mmap while
new buffers are being added and calling mmap while buffers are being deleted.
The first case works almost fine without a lock: in all cases mmap relies on
correctly filled-in q->num_buffers/q->num_planes values and those are only
updated by reqbufs and create_buffers *after* any new buffers have been
initialized completely. Except in one case: if an error occurred while allocating
the buffers it will increase num_buffers and rely on __vb2_queue_free to
decrease it again. So there is a short period where the buffer information
may be wrong.
The second case definitely does pose a problem: buffers may be in the process
of being deleted, without the internal structure being updated.
In order to fix this a new mutex is added to vb2_queue that is taken when
buffers are allocated or deleted, and in vb2_mmap. That way vb2_mmap won't
get stale buffer data. Note that this is a problem only for MEMORY_MMAP, so
even though __qbuf_userptr and __qbuf_dmabuf also mess around with buffers
(mem_priv in particular), this doesn't clash with vb2_mmap or
vb2_get_unmapped_area since those are MMAP specific.
As an additional bonus the hack in __buf_prepare, the USERPTR case, can be
removed as well since mmap() no longer takes the core lock.
All in all a much cleaner solution.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2014-08-07 14:47:14 +08:00
|
|
|
mutex_unlock(&q->mmap_lock);
|
2013-12-14 00:13:40 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2011-03-10 01:03:24 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In case of REQBUFS(0) return immediately without calling
|
|
|
|
* driver's queue_setup() callback and allocating resources.
|
|
|
|
*/
|
2015-10-06 17:37:48 +08:00
|
|
|
if (*count == 0)
|
2011-03-10 01:03:24 +08:00
|
|
|
return 0;
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure the requested values and current defaults are sane.
|
|
|
|
*/
|
2015-10-06 17:37:48 +08:00
|
|
|
num_buffers = min_t(unsigned int, *count, VB2_MAX_FRAME);
|
2014-05-09 23:32:10 +08:00
|
|
|
num_buffers = max_t(unsigned int, num_buffers, q->min_buffers_needed);
|
2011-08-24 17:36:26 +08:00
|
|
|
memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
|
2010-10-11 21:56:41 +08:00
|
|
|
memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
|
2015-10-06 17:37:48 +08:00
|
|
|
q->memory = memory;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ask the driver how many buffers and planes per buffer it requires.
|
|
|
|
* Driver also sets the size and allocator context for each plane.
|
|
|
|
*/
|
2015-10-28 10:50:37 +08:00
|
|
|
ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
|
2011-08-24 17:36:26 +08:00
|
|
|
q->plane_sizes, q->alloc_ctx);
|
2014-03-17 20:54:21 +08:00
|
|
|
if (ret)
|
2010-10-11 21:56:41 +08:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Finally, allocate buffers and video memory */
|
2015-10-06 17:37:48 +08:00
|
|
|
allocated_buffers =
|
|
|
|
__vb2_queue_alloc(q, memory, num_buffers, num_planes);
|
2014-02-25 00:41:20 +08:00
|
|
|
if (allocated_buffers == 0) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "memory allocation failed\n");
|
2011-06-28 19:29:02 +08:00
|
|
|
return -ENOMEM;
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
|
2014-02-25 00:51:03 +08:00
|
|
|
/*
|
|
|
|
* There is no point in continuing if we can't allocate the minimum
|
|
|
|
* number of buffers needed by this vb2_queue.
|
|
|
|
*/
|
|
|
|
if (allocated_buffers < q->min_buffers_needed)
|
|
|
|
ret = -ENOMEM;
|
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
/*
|
|
|
|
* Check if driver can handle the allocated number of buffers.
|
|
|
|
*/
|
2014-02-25 00:51:03 +08:00
|
|
|
if (!ret && allocated_buffers < num_buffers) {
|
2011-09-28 20:23:02 +08:00
|
|
|
num_buffers = allocated_buffers;
|
2015-10-28 10:50:37 +08:00
|
|
|
/*
|
|
|
|
* num_planes is set by the previous queue_setup(), but since it
|
|
|
|
* signals to queue_setup() whether it is called from create_bufs()
|
|
|
|
* vs reqbufs() we zero it here to signal that queue_setup() is
|
|
|
|
* called for the reqbufs() case.
|
|
|
|
*/
|
|
|
|
num_planes = 0;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2015-10-28 10:50:37 +08:00
|
|
|
ret = call_qop(q, queue_setup, q, &num_buffers,
|
2011-08-24 21:30:21 +08:00
|
|
|
&num_planes, q->plane_sizes, q->alloc_ctx);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2011-09-28 20:23:02 +08:00
|
|
|
if (!ret && allocated_buffers < num_buffers)
|
2010-10-11 21:56:41 +08:00
|
|
|
ret = -ENOMEM;
|
|
|
|
|
|
|
|
/*
|
2011-09-28 20:23:02 +08:00
|
|
|
* Either the driver has accepted a smaller number of buffers,
|
|
|
|
* or .queue_setup() returned an error
|
2010-10-11 21:56:41 +08:00
|
|
|
*/
|
2011-09-28 20:23:02 +08:00
|
|
|
}
|
|
|
|
|
[media] videobuf2: fix lockdep warning
The following lockdep warning has been there ever since commit a517cca6b24fc54ac209e44118ec8962051662e3
one year ago:
[ 403.117947] ======================================================
[ 403.117949] [ INFO: possible circular locking dependency detected ]
[ 403.117953] 3.16.0-rc6-test-media #961 Not tainted
[ 403.117954] -------------------------------------------------------
[ 403.117956] v4l2-ctl/15377 is trying to acquire lock:
[ 403.117959] (&dev->mutex#3){+.+.+.}, at: [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.117974]
[ 403.117974] but task is already holding lock:
[ 403.117976] (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.117987]
[ 403.117987] which lock already depends on the new lock.
[ 403.117987]
[ 403.117990]
[ 403.117990] the existing dependency chain (in reverse order) is:
[ 403.117992]
[ 403.117992] -> #1 (&mm->mmap_sem){++++++}:
[ 403.117997] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118006] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118010] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118014] [<ffffffff8118c9ec>] might_fault+0x7c/0xb0
[ 403.118018] [<ffffffffa0028a25>] video_usercopy+0x425/0x610 [videodev]
[ 403.118028] [<ffffffffa0028c25>] video_ioctl2+0x15/0x20 [videodev]
[ 403.118034] [<ffffffffa0022764>] v4l2_ioctl+0x184/0x1a0 [videodev]
[ 403.118040] [<ffffffff811d77d0>] do_vfs_ioctl+0x2f0/0x4f0
[ 403.118307] [<ffffffff811d7a51>] SyS_ioctl+0x81/0xa0
[ 403.118311] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118319]
[ 403.118319] -> #0 (&dev->mutex#3){+.+.+.}:
[ 403.118324] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118329] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118333] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118336] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118340] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118344] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118349] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118354] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118359] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118363] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118366] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118369] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118376] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118381]
[ 403.118381] other info that might help us debug this:
[ 403.118381]
[ 403.118383] Possible unsafe locking scenario:
[ 403.118383]
[ 403.118385] CPU0 CPU1
[ 403.118387] ---- ----
[ 403.118388] lock(&mm->mmap_sem);
[ 403.118391] lock(&dev->mutex#3);
[ 403.118394] lock(&mm->mmap_sem);
[ 403.118397] lock(&dev->mutex#3);
[ 403.118400]
[ 403.118400] *** DEADLOCK ***
[ 403.118400]
[ 403.118403] 1 lock held by v4l2-ctl/15377:
[ 403.118405] #0: (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.118411]
[ 403.118411] stack backtrace:
[ 403.118415] CPU: 0 PID: 15377 Comm: v4l2-ctl Not tainted 3.16.0-rc6-test-media #961
[ 403.118418] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/31/2013
[ 403.118420] ffffffff82a6c9d0 ffff8800af37fb00 ffffffff819916a2 ffffffff82a6c9d0
[ 403.118425] ffff8800af37fb40 ffffffff810d5715 ffff8802308e4200 0000000000000000
[ 403.118429] ffff8802308e4a48 ffff8802308e4a48 ffff8802308e4200 0000000000000001
[ 403.118433] Call Trace:
[ 403.118441] [<ffffffff819916a2>] dump_stack+0x4e/0x7a
[ 403.118445] [<ffffffff810d5715>] print_circular_bug+0x1d5/0x2a0
[ 403.118449] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118455] [<ffffffff8119c172>] ? find_vmap_area+0x42/0x70
[ 403.118459] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118463] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118468] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118472] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118476] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118480] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118484] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118488] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118493] [<ffffffff810d8055>] ? mark_held_locks+0x75/0xa0
[ 403.118497] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118502] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118506] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118510] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118513] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118517] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118521] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118525] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
The reason is that vb2_fop_mmap and vb2_fop_get_unmapped_area take the core lock
while they are called with the mmap_sem semaphore held. But elsewhere in the code
the core lock is taken first but calls to copy_to/from_user() can take the mmap_sem
semaphore as well, potentially causing a classical A-B/B-A deadlock.
However, the mmap/get_unmapped_area calls really shouldn't take the core lock
at all. So what would happen if they don't take the core lock anymore?
There are two situations that need to be taken into account: calling mmap while
new buffers are being added and calling mmap while buffers are being deleted.
The first case works almost fine without a lock: in all cases mmap relies on
correctly filled-in q->num_buffers/q->num_planes values and those are only
updated by reqbufs and create_buffers *after* any new buffers have been
initialized completely. Except in one case: if an error occurred while allocating
the buffers it will increase num_buffers and rely on __vb2_queue_free to
decrease it again. So there is a short period where the buffer information
may be wrong.
The second case definitely does pose a problem: buffers may be in the process
of being deleted, without the internal structure being updated.
In order to fix this a new mutex is added to vb2_queue that is taken when
buffers are allocated or deleted, and in vb2_mmap. That way vb2_mmap won't
get stale buffer data. Note that this is a problem only for MEMORY_MMAP, so
even though __qbuf_userptr and __qbuf_dmabuf also mess around with buffers
(mem_priv in particular), this doesn't clash with vb2_mmap or
vb2_get_unmapped_area since those are MMAP specific.
As an additional bonus the hack in __buf_prepare, the USERPTR case, can be
removed as well since mmap() no longer takes the core lock.
All in all a much cleaner solution.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2014-08-07 14:47:14 +08:00
|
|
|
mutex_lock(&q->mmap_lock);
|
2011-09-28 20:23:02 +08:00
|
|
|
q->num_buffers = allocated_buffers;
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2014-02-25 00:41:20 +08:00
|
|
|
/*
|
|
|
|
* Note: __vb2_queue_free() will subtract 'allocated_buffers'
|
|
|
|
* from q->num_buffers.
|
|
|
|
*/
|
2011-09-28 20:23:02 +08:00
|
|
|
__vb2_queue_free(q, allocated_buffers);
|
[media] videobuf2: fix lockdep warning
The following lockdep warning has been there ever since commit a517cca6b24fc54ac209e44118ec8962051662e3
one year ago:
[ 403.117947] ======================================================
[ 403.117949] [ INFO: possible circular locking dependency detected ]
[ 403.117953] 3.16.0-rc6-test-media #961 Not tainted
[ 403.117954] -------------------------------------------------------
[ 403.117956] v4l2-ctl/15377 is trying to acquire lock:
[ 403.117959] (&dev->mutex#3){+.+.+.}, at: [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.117974]
[ 403.117974] but task is already holding lock:
[ 403.117976] (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.117987]
[ 403.117987] which lock already depends on the new lock.
[ 403.117987]
[ 403.117990]
[ 403.117990] the existing dependency chain (in reverse order) is:
[ 403.117992]
[ 403.117992] -> #1 (&mm->mmap_sem){++++++}:
[ 403.117997] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118006] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118010] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118014] [<ffffffff8118c9ec>] might_fault+0x7c/0xb0
[ 403.118018] [<ffffffffa0028a25>] video_usercopy+0x425/0x610 [videodev]
[ 403.118028] [<ffffffffa0028c25>] video_ioctl2+0x15/0x20 [videodev]
[ 403.118034] [<ffffffffa0022764>] v4l2_ioctl+0x184/0x1a0 [videodev]
[ 403.118040] [<ffffffff811d77d0>] do_vfs_ioctl+0x2f0/0x4f0
[ 403.118307] [<ffffffff811d7a51>] SyS_ioctl+0x81/0xa0
[ 403.118311] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118319]
[ 403.118319] -> #0 (&dev->mutex#3){+.+.+.}:
[ 403.118324] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118329] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118333] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118336] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118340] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118344] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118349] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118354] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118359] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118363] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118366] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118369] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118376] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118381]
[ 403.118381] other info that might help us debug this:
[ 403.118381]
[ 403.118383] Possible unsafe locking scenario:
[ 403.118383]
[ 403.118385] CPU0 CPU1
[ 403.118387] ---- ----
[ 403.118388] lock(&mm->mmap_sem);
[ 403.118391] lock(&dev->mutex#3);
[ 403.118394] lock(&mm->mmap_sem);
[ 403.118397] lock(&dev->mutex#3);
[ 403.118400]
[ 403.118400] *** DEADLOCK ***
[ 403.118400]
[ 403.118403] 1 lock held by v4l2-ctl/15377:
[ 403.118405] #0: (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.118411]
[ 403.118411] stack backtrace:
[ 403.118415] CPU: 0 PID: 15377 Comm: v4l2-ctl Not tainted 3.16.0-rc6-test-media #961
[ 403.118418] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/31/2013
[ 403.118420] ffffffff82a6c9d0 ffff8800af37fb00 ffffffff819916a2 ffffffff82a6c9d0
[ 403.118425] ffff8800af37fb40 ffffffff810d5715 ffff8802308e4200 0000000000000000
[ 403.118429] ffff8802308e4a48 ffff8802308e4a48 ffff8802308e4200 0000000000000001
[ 403.118433] Call Trace:
[ 403.118441] [<ffffffff819916a2>] dump_stack+0x4e/0x7a
[ 403.118445] [<ffffffff810d5715>] print_circular_bug+0x1d5/0x2a0
[ 403.118449] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118455] [<ffffffff8119c172>] ? find_vmap_area+0x42/0x70
[ 403.118459] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118463] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118468] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118472] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118476] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118480] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118484] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118488] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118493] [<ffffffff810d8055>] ? mark_held_locks+0x75/0xa0
[ 403.118497] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118502] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118506] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118510] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118513] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118517] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118521] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118525] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
The reason is that vb2_fop_mmap and vb2_fop_get_unmapped_area take the core lock
while they are called with the mmap_sem semaphore held. But elsewhere in the code
the core lock is taken first but calls to copy_to/from_user() can take the mmap_sem
semaphore as well, potentially causing a classical A-B/B-A deadlock.
However, the mmap/get_unmapped_area calls really shouldn't take the core lock
at all. So what would happen if they don't take the core lock anymore?
There are two situations that need to be taken into account: calling mmap while
new buffers are being added and calling mmap while buffers are being deleted.
The first case works almost fine without a lock: in all cases mmap relies on
correctly filled-in q->num_buffers/q->num_planes values and those are only
updated by reqbufs and create_buffers *after* any new buffers have been
initialized completely. Except in one case: if an error occurred while allocating
the buffers it will increase num_buffers and rely on __vb2_queue_free to
decrease it again. So there is a short period where the buffer information
may be wrong.
The second case definitely does pose a problem: buffers may be in the process
of being deleted, without the internal structure being updated.
In order to fix this a new mutex is added to vb2_queue that is taken when
buffers are allocated or deleted, and in vb2_mmap. That way vb2_mmap won't
get stale buffer data. Note that this is a problem only for MEMORY_MMAP, so
even though __qbuf_userptr and __qbuf_dmabuf also mess around with buffers
(mem_priv in particular), this doesn't clash with vb2_mmap or
vb2_get_unmapped_area since those are MMAP specific.
As an additional bonus the hack in __buf_prepare, the USERPTR case, can be
removed as well since mmap() no longer takes the core lock.
All in all a much cleaner solution.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2014-08-07 14:47:14 +08:00
|
|
|
mutex_unlock(&q->mmap_lock);
|
2011-09-28 20:23:02 +08:00
|
|
|
return ret;
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
[media] videobuf2: fix lockdep warning
The following lockdep warning has been there ever since commit a517cca6b24fc54ac209e44118ec8962051662e3
one year ago:
[ 403.117947] ======================================================
[ 403.117949] [ INFO: possible circular locking dependency detected ]
[ 403.117953] 3.16.0-rc6-test-media #961 Not tainted
[ 403.117954] -------------------------------------------------------
[ 403.117956] v4l2-ctl/15377 is trying to acquire lock:
[ 403.117959] (&dev->mutex#3){+.+.+.}, at: [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.117974]
[ 403.117974] but task is already holding lock:
[ 403.117976] (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.117987]
[ 403.117987] which lock already depends on the new lock.
[ 403.117987]
[ 403.117990]
[ 403.117990] the existing dependency chain (in reverse order) is:
[ 403.117992]
[ 403.117992] -> #1 (&mm->mmap_sem){++++++}:
[ 403.117997] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118006] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118010] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118014] [<ffffffff8118c9ec>] might_fault+0x7c/0xb0
[ 403.118018] [<ffffffffa0028a25>] video_usercopy+0x425/0x610 [videodev]
[ 403.118028] [<ffffffffa0028c25>] video_ioctl2+0x15/0x20 [videodev]
[ 403.118034] [<ffffffffa0022764>] v4l2_ioctl+0x184/0x1a0 [videodev]
[ 403.118040] [<ffffffff811d77d0>] do_vfs_ioctl+0x2f0/0x4f0
[ 403.118307] [<ffffffff811d7a51>] SyS_ioctl+0x81/0xa0
[ 403.118311] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118319]
[ 403.118319] -> #0 (&dev->mutex#3){+.+.+.}:
[ 403.118324] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118329] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118333] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118336] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118340] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118344] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118349] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118354] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118359] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118363] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118366] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118369] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118376] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118381]
[ 403.118381] other info that might help us debug this:
[ 403.118381]
[ 403.118383] Possible unsafe locking scenario:
[ 403.118383]
[ 403.118385] CPU0 CPU1
[ 403.118387] ---- ----
[ 403.118388] lock(&mm->mmap_sem);
[ 403.118391] lock(&dev->mutex#3);
[ 403.118394] lock(&mm->mmap_sem);
[ 403.118397] lock(&dev->mutex#3);
[ 403.118400]
[ 403.118400] *** DEADLOCK ***
[ 403.118400]
[ 403.118403] 1 lock held by v4l2-ctl/15377:
[ 403.118405] #0: (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.118411]
[ 403.118411] stack backtrace:
[ 403.118415] CPU: 0 PID: 15377 Comm: v4l2-ctl Not tainted 3.16.0-rc6-test-media #961
[ 403.118418] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/31/2013
[ 403.118420] ffffffff82a6c9d0 ffff8800af37fb00 ffffffff819916a2 ffffffff82a6c9d0
[ 403.118425] ffff8800af37fb40 ffffffff810d5715 ffff8802308e4200 0000000000000000
[ 403.118429] ffff8802308e4a48 ffff8802308e4a48 ffff8802308e4200 0000000000000001
[ 403.118433] Call Trace:
[ 403.118441] [<ffffffff819916a2>] dump_stack+0x4e/0x7a
[ 403.118445] [<ffffffff810d5715>] print_circular_bug+0x1d5/0x2a0
[ 403.118449] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118455] [<ffffffff8119c172>] ? find_vmap_area+0x42/0x70
[ 403.118459] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118463] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118468] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118472] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118476] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118480] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118484] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118488] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118493] [<ffffffff810d8055>] ? mark_held_locks+0x75/0xa0
[ 403.118497] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118502] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118506] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118510] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118513] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118517] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118521] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118525] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
The reason is that vb2_fop_mmap and vb2_fop_get_unmapped_area take the core lock
while they are called with the mmap_sem semaphore held. But elsewhere in the code
the core lock is taken first but calls to copy_to/from_user() can take the mmap_sem
semaphore as well, potentially causing a classical A-B/B-A deadlock.
However, the mmap/get_unmapped_area calls really shouldn't take the core lock
at all. So what would happen if they don't take the core lock anymore?
There are two situations that need to be taken into account: calling mmap while
new buffers are being added and calling mmap while buffers are being deleted.
The first case works almost fine without a lock: in all cases mmap relies on
correctly filled-in q->num_buffers/q->num_planes values and those are only
updated by reqbufs and create_buffers *after* any new buffers have been
initialized completely. Except in one case: if an error occurred while allocating
the buffers it will increase num_buffers and rely on __vb2_queue_free to
decrease it again. So there is a short period where the buffer information
may be wrong.
The second case definitely does pose a problem: buffers may be in the process
of being deleted, without the internal structure being updated.
In order to fix this a new mutex is added to vb2_queue that is taken when
buffers are allocated or deleted, and in vb2_mmap. That way vb2_mmap won't
get stale buffer data. Note that this is a problem only for MEMORY_MMAP, so
even though __qbuf_userptr and __qbuf_dmabuf also mess around with buffers
(mem_priv in particular), this doesn't clash with vb2_mmap or
vb2_get_unmapped_area since those are MMAP specific.
As an additional bonus the hack in __buf_prepare, the USERPTR case, can be
removed as well since mmap() no longer takes the core lock.
All in all a much cleaner solution.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2014-08-07 14:47:14 +08:00
|
|
|
mutex_unlock(&q->mmap_lock);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the number of successfully allocated buffers
|
|
|
|
* to the userspace.
|
|
|
|
*/
|
2015-10-06 17:37:48 +08:00
|
|
|
*count = allocated_buffers;
|
2015-10-06 17:37:47 +08:00
|
|
|
q->waiting_for_buffers = !q->is_output;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-06 17:37:49 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2011-09-28 20:23:02 +08:00
|
|
|
/**
|
2015-10-06 17:37:48 +08:00
|
|
|
* vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
|
2011-09-28 20:23:02 +08:00
|
|
|
* @q: videobuf2 queue
|
2015-10-06 17:37:48 +08:00
|
|
|
* @memory: memory type
|
|
|
|
* @count: requested buffer count
|
|
|
|
* @parg: parameter passed to device driver
|
2011-09-28 20:23:02 +08:00
|
|
|
*
|
|
|
|
* Should be called from vidioc_create_bufs ioctl handler of a driver.
|
|
|
|
* This function:
|
|
|
|
* 1) verifies parameter sanity
|
|
|
|
* 2) calls the .queue_setup() queue operation
|
|
|
|
* 3) performs any necessary memory allocations
|
|
|
|
*
|
|
|
|
* The return values from this function are intended to be directly returned
|
|
|
|
* from vidioc_create_bufs handler in driver.
|
|
|
|
*/
|
2015-10-06 17:37:49 +08:00
|
|
|
int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
|
2015-10-28 10:50:37 +08:00
|
|
|
unsigned int *count, unsigned requested_planes,
|
|
|
|
const unsigned requested_sizes[])
|
2011-09-28 20:23:02 +08:00
|
|
|
{
|
|
|
|
unsigned int num_planes = 0, num_buffers, allocated_buffers;
|
2012-06-28 04:10:30 +08:00
|
|
|
int ret;
|
2011-09-28 20:23:02 +08:00
|
|
|
|
2015-10-06 17:37:47 +08:00
|
|
|
if (q->num_buffers == VB2_MAX_FRAME) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "maximum number of buffers already allocated\n");
|
2011-09-28 20:23:02 +08:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!q->num_buffers) {
|
|
|
|
memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
|
|
|
|
memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
|
2015-10-06 17:37:48 +08:00
|
|
|
q->memory = memory;
|
2015-10-06 17:37:47 +08:00
|
|
|
q->waiting_for_buffers = !q->is_output;
|
2011-09-28 20:23:02 +08:00
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);
|
2011-09-28 20:23:02 +08:00
|
|
|
|
2015-10-28 10:50:37 +08:00
|
|
|
if (requested_planes && requested_sizes) {
|
|
|
|
num_planes = requested_planes;
|
|
|
|
memcpy(q->plane_sizes, requested_sizes, sizeof(q->plane_sizes));
|
|
|
|
}
|
|
|
|
|
2011-09-28 20:23:02 +08:00
|
|
|
/*
|
|
|
|
* Ask the driver, whether the requested number of buffers, planes per
|
|
|
|
* buffer and their sizes are acceptable
|
|
|
|
*/
|
2015-10-28 10:50:37 +08:00
|
|
|
ret = call_qop(q, queue_setup, q, &num_buffers,
|
2011-09-28 20:23:02 +08:00
|
|
|
&num_planes, q->plane_sizes, q->alloc_ctx);
|
2014-03-17 20:54:21 +08:00
|
|
|
if (ret)
|
2011-09-28 20:23:02 +08:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Finally, allocate buffers and video memory */
|
2015-10-06 17:37:48 +08:00
|
|
|
allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers,
|
2011-09-28 20:23:02 +08:00
|
|
|
num_planes);
|
2014-02-25 00:41:20 +08:00
|
|
|
if (allocated_buffers == 0) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "memory allocation failed\n");
|
2012-06-22 16:44:14 +08:00
|
|
|
return -ENOMEM;
|
2011-09-28 20:23:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if driver can handle the so far allocated number of buffers.
|
|
|
|
*/
|
2014-02-25 00:41:20 +08:00
|
|
|
if (allocated_buffers < num_buffers) {
|
|
|
|
num_buffers = allocated_buffers;
|
2011-09-28 20:23:02 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* q->num_buffers contains the total number of buffers, that the
|
|
|
|
* queue driver has set up
|
|
|
|
*/
|
2015-10-28 10:50:37 +08:00
|
|
|
ret = call_qop(q, queue_setup, q, &num_buffers,
|
2011-09-28 20:23:02 +08:00
|
|
|
&num_planes, q->plane_sizes, q->alloc_ctx);
|
|
|
|
|
|
|
|
if (!ret && allocated_buffers < num_buffers)
|
|
|
|
ret = -ENOMEM;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Either the driver has accepted a smaller number of buffers,
|
|
|
|
* or .queue_setup() returned an error
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
[media] videobuf2: fix lockdep warning
The following lockdep warning has been there ever since commit a517cca6b24fc54ac209e44118ec8962051662e3
one year ago:
[ 403.117947] ======================================================
[ 403.117949] [ INFO: possible circular locking dependency detected ]
[ 403.117953] 3.16.0-rc6-test-media #961 Not tainted
[ 403.117954] -------------------------------------------------------
[ 403.117956] v4l2-ctl/15377 is trying to acquire lock:
[ 403.117959] (&dev->mutex#3){+.+.+.}, at: [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.117974]
[ 403.117974] but task is already holding lock:
[ 403.117976] (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.117987]
[ 403.117987] which lock already depends on the new lock.
[ 403.117987]
[ 403.117990]
[ 403.117990] the existing dependency chain (in reverse order) is:
[ 403.117992]
[ 403.117992] -> #1 (&mm->mmap_sem){++++++}:
[ 403.117997] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118006] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118010] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118014] [<ffffffff8118c9ec>] might_fault+0x7c/0xb0
[ 403.118018] [<ffffffffa0028a25>] video_usercopy+0x425/0x610 [videodev]
[ 403.118028] [<ffffffffa0028c25>] video_ioctl2+0x15/0x20 [videodev]
[ 403.118034] [<ffffffffa0022764>] v4l2_ioctl+0x184/0x1a0 [videodev]
[ 403.118040] [<ffffffff811d77d0>] do_vfs_ioctl+0x2f0/0x4f0
[ 403.118307] [<ffffffff811d7a51>] SyS_ioctl+0x81/0xa0
[ 403.118311] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118319]
[ 403.118319] -> #0 (&dev->mutex#3){+.+.+.}:
[ 403.118324] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118329] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118333] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118336] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118340] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118344] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118349] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118354] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118359] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118363] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118366] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118369] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118376] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118381]
[ 403.118381] other info that might help us debug this:
[ 403.118381]
[ 403.118383] Possible unsafe locking scenario:
[ 403.118383]
[ 403.118385] CPU0 CPU1
[ 403.118387] ---- ----
[ 403.118388] lock(&mm->mmap_sem);
[ 403.118391] lock(&dev->mutex#3);
[ 403.118394] lock(&mm->mmap_sem);
[ 403.118397] lock(&dev->mutex#3);
[ 403.118400]
[ 403.118400] *** DEADLOCK ***
[ 403.118400]
[ 403.118403] 1 lock held by v4l2-ctl/15377:
[ 403.118405] #0: (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.118411]
[ 403.118411] stack backtrace:
[ 403.118415] CPU: 0 PID: 15377 Comm: v4l2-ctl Not tainted 3.16.0-rc6-test-media #961
[ 403.118418] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/31/2013
[ 403.118420] ffffffff82a6c9d0 ffff8800af37fb00 ffffffff819916a2 ffffffff82a6c9d0
[ 403.118425] ffff8800af37fb40 ffffffff810d5715 ffff8802308e4200 0000000000000000
[ 403.118429] ffff8802308e4a48 ffff8802308e4a48 ffff8802308e4200 0000000000000001
[ 403.118433] Call Trace:
[ 403.118441] [<ffffffff819916a2>] dump_stack+0x4e/0x7a
[ 403.118445] [<ffffffff810d5715>] print_circular_bug+0x1d5/0x2a0
[ 403.118449] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118455] [<ffffffff8119c172>] ? find_vmap_area+0x42/0x70
[ 403.118459] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118463] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118468] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118472] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118476] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118480] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118484] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118488] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118493] [<ffffffff810d8055>] ? mark_held_locks+0x75/0xa0
[ 403.118497] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118502] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118506] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118510] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118513] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118517] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118521] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118525] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
The reason is that vb2_fop_mmap and vb2_fop_get_unmapped_area take the core lock
while they are called with the mmap_sem semaphore held. But elsewhere in the code
the core lock is taken first but calls to copy_to/from_user() can take the mmap_sem
semaphore as well, potentially causing a classical A-B/B-A deadlock.
However, the mmap/get_unmapped_area calls really shouldn't take the core lock
at all. So what would happen if they don't take the core lock anymore?
There are two situations that need to be taken into account: calling mmap while
new buffers are being added and calling mmap while buffers are being deleted.
The first case works almost fine without a lock: in all cases mmap relies on
correctly filled-in q->num_buffers/q->num_planes values and those are only
updated by reqbufs and create_buffers *after* any new buffers have been
initialized completely. Except in one case: if an error occurred while allocating
the buffers it will increase num_buffers and rely on __vb2_queue_free to
decrease it again. So there is a short period where the buffer information
may be wrong.
The second case definitely does pose a problem: buffers may be in the process
of being deleted, without the internal structure being updated.
In order to fix this a new mutex is added to vb2_queue that is taken when
buffers are allocated or deleted, and in vb2_mmap. That way vb2_mmap won't
get stale buffer data. Note that this is a problem only for MEMORY_MMAP, so
even though __qbuf_userptr and __qbuf_dmabuf also mess around with buffers
(mem_priv in particular), this doesn't clash with vb2_mmap or
vb2_get_unmapped_area since those are MMAP specific.
As an additional bonus the hack in __buf_prepare, the USERPTR case, can be
removed as well since mmap() no longer takes the core lock.
All in all a much cleaner solution.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2014-08-07 14:47:14 +08:00
|
|
|
mutex_lock(&q->mmap_lock);
|
2011-09-28 20:23:02 +08:00
|
|
|
q->num_buffers += allocated_buffers;
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2014-02-25 00:41:20 +08:00
|
|
|
/*
|
|
|
|
* Note: __vb2_queue_free() will subtract 'allocated_buffers'
|
|
|
|
* from q->num_buffers.
|
|
|
|
*/
|
2011-09-28 20:23:02 +08:00
|
|
|
__vb2_queue_free(q, allocated_buffers);
|
[media] videobuf2: fix lockdep warning
The following lockdep warning has been there ever since commit a517cca6b24fc54ac209e44118ec8962051662e3
one year ago:
[ 403.117947] ======================================================
[ 403.117949] [ INFO: possible circular locking dependency detected ]
[ 403.117953] 3.16.0-rc6-test-media #961 Not tainted
[ 403.117954] -------------------------------------------------------
[ 403.117956] v4l2-ctl/15377 is trying to acquire lock:
[ 403.117959] (&dev->mutex#3){+.+.+.}, at: [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.117974]
[ 403.117974] but task is already holding lock:
[ 403.117976] (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.117987]
[ 403.117987] which lock already depends on the new lock.
[ 403.117987]
[ 403.117990]
[ 403.117990] the existing dependency chain (in reverse order) is:
[ 403.117992]
[ 403.117992] -> #1 (&mm->mmap_sem){++++++}:
[ 403.117997] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118006] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118010] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118014] [<ffffffff8118c9ec>] might_fault+0x7c/0xb0
[ 403.118018] [<ffffffffa0028a25>] video_usercopy+0x425/0x610 [videodev]
[ 403.118028] [<ffffffffa0028c25>] video_ioctl2+0x15/0x20 [videodev]
[ 403.118034] [<ffffffffa0022764>] v4l2_ioctl+0x184/0x1a0 [videodev]
[ 403.118040] [<ffffffff811d77d0>] do_vfs_ioctl+0x2f0/0x4f0
[ 403.118307] [<ffffffff811d7a51>] SyS_ioctl+0x81/0xa0
[ 403.118311] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118319]
[ 403.118319] -> #0 (&dev->mutex#3){+.+.+.}:
[ 403.118324] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118329] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118333] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118336] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118340] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118344] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118349] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118354] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118359] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118363] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118366] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118369] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118376] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118381]
[ 403.118381] other info that might help us debug this:
[ 403.118381]
[ 403.118383] Possible unsafe locking scenario:
[ 403.118383]
[ 403.118385] CPU0 CPU1
[ 403.118387] ---- ----
[ 403.118388] lock(&mm->mmap_sem);
[ 403.118391] lock(&dev->mutex#3);
[ 403.118394] lock(&mm->mmap_sem);
[ 403.118397] lock(&dev->mutex#3);
[ 403.118400]
[ 403.118400] *** DEADLOCK ***
[ 403.118400]
[ 403.118403] 1 lock held by v4l2-ctl/15377:
[ 403.118405] #0: (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.118411]
[ 403.118411] stack backtrace:
[ 403.118415] CPU: 0 PID: 15377 Comm: v4l2-ctl Not tainted 3.16.0-rc6-test-media #961
[ 403.118418] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/31/2013
[ 403.118420] ffffffff82a6c9d0 ffff8800af37fb00 ffffffff819916a2 ffffffff82a6c9d0
[ 403.118425] ffff8800af37fb40 ffffffff810d5715 ffff8802308e4200 0000000000000000
[ 403.118429] ffff8802308e4a48 ffff8802308e4a48 ffff8802308e4200 0000000000000001
[ 403.118433] Call Trace:
[ 403.118441] [<ffffffff819916a2>] dump_stack+0x4e/0x7a
[ 403.118445] [<ffffffff810d5715>] print_circular_bug+0x1d5/0x2a0
[ 403.118449] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118455] [<ffffffff8119c172>] ? find_vmap_area+0x42/0x70
[ 403.118459] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118463] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118468] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118472] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118476] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118480] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118484] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118488] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118493] [<ffffffff810d8055>] ? mark_held_locks+0x75/0xa0
[ 403.118497] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118502] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118506] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118510] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118513] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118517] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118521] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118525] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
The reason is that vb2_fop_mmap and vb2_fop_get_unmapped_area take the core lock
while they are called with the mmap_sem semaphore held. But elsewhere in the code
the core lock is taken first but calls to copy_to/from_user() can take the mmap_sem
semaphore as well, potentially causing a classical A-B/B-A deadlock.
However, the mmap/get_unmapped_area calls really shouldn't take the core lock
at all. So what would happen if they don't take the core lock anymore?
There are two situations that need to be taken into account: calling mmap while
new buffers are being added and calling mmap while buffers are being deleted.
The first case works almost fine without a lock: in all cases mmap relies on
correctly filled-in q->num_buffers/q->num_planes values and those are only
updated by reqbufs and create_buffers *after* any new buffers have been
initialized completely. Except in one case: if an error occurred while allocating
the buffers it will increase num_buffers and rely on __vb2_queue_free to
decrease it again. So there is a short period where the buffer information
may be wrong.
The second case definitely does pose a problem: buffers may be in the process
of being deleted, without the internal structure being updated.
In order to fix this a new mutex is added to vb2_queue that is taken when
buffers are allocated or deleted, and in vb2_mmap. That way vb2_mmap won't
get stale buffer data. Note that this is a problem only for MEMORY_MMAP, so
even though __qbuf_userptr and __qbuf_dmabuf also mess around with buffers
(mem_priv in particular), this doesn't clash with vb2_mmap or
vb2_get_unmapped_area since those are MMAP specific.
As an additional bonus the hack in __buf_prepare, the USERPTR case, can be
removed as well since mmap() no longer takes the core lock.
All in all a much cleaner solution.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2014-08-07 14:47:14 +08:00
|
|
|
mutex_unlock(&q->mmap_lock);
|
2012-06-22 16:44:14 +08:00
|
|
|
return -ENOMEM;
|
2011-09-28 20:23:02 +08:00
|
|
|
}
|
[media] videobuf2: fix lockdep warning
The following lockdep warning has been there ever since commit a517cca6b24fc54ac209e44118ec8962051662e3
one year ago:
[ 403.117947] ======================================================
[ 403.117949] [ INFO: possible circular locking dependency detected ]
[ 403.117953] 3.16.0-rc6-test-media #961 Not tainted
[ 403.117954] -------------------------------------------------------
[ 403.117956] v4l2-ctl/15377 is trying to acquire lock:
[ 403.117959] (&dev->mutex#3){+.+.+.}, at: [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.117974]
[ 403.117974] but task is already holding lock:
[ 403.117976] (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.117987]
[ 403.117987] which lock already depends on the new lock.
[ 403.117987]
[ 403.117990]
[ 403.117990] the existing dependency chain (in reverse order) is:
[ 403.117992]
[ 403.117992] -> #1 (&mm->mmap_sem){++++++}:
[ 403.117997] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118006] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118010] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118014] [<ffffffff8118c9ec>] might_fault+0x7c/0xb0
[ 403.118018] [<ffffffffa0028a25>] video_usercopy+0x425/0x610 [videodev]
[ 403.118028] [<ffffffffa0028c25>] video_ioctl2+0x15/0x20 [videodev]
[ 403.118034] [<ffffffffa0022764>] v4l2_ioctl+0x184/0x1a0 [videodev]
[ 403.118040] [<ffffffff811d77d0>] do_vfs_ioctl+0x2f0/0x4f0
[ 403.118307] [<ffffffff811d7a51>] SyS_ioctl+0x81/0xa0
[ 403.118311] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118319]
[ 403.118319] -> #0 (&dev->mutex#3){+.+.+.}:
[ 403.118324] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118329] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118333] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118336] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118340] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118344] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118349] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118354] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118359] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118363] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118366] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118369] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118376] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118381]
[ 403.118381] other info that might help us debug this:
[ 403.118381]
[ 403.118383] Possible unsafe locking scenario:
[ 403.118383]
[ 403.118385] CPU0 CPU1
[ 403.118387] ---- ----
[ 403.118388] lock(&mm->mmap_sem);
[ 403.118391] lock(&dev->mutex#3);
[ 403.118394] lock(&mm->mmap_sem);
[ 403.118397] lock(&dev->mutex#3);
[ 403.118400]
[ 403.118400] *** DEADLOCK ***
[ 403.118400]
[ 403.118403] 1 lock held by v4l2-ctl/15377:
[ 403.118405] #0: (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.118411]
[ 403.118411] stack backtrace:
[ 403.118415] CPU: 0 PID: 15377 Comm: v4l2-ctl Not tainted 3.16.0-rc6-test-media #961
[ 403.118418] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/31/2013
[ 403.118420] ffffffff82a6c9d0 ffff8800af37fb00 ffffffff819916a2 ffffffff82a6c9d0
[ 403.118425] ffff8800af37fb40 ffffffff810d5715 ffff8802308e4200 0000000000000000
[ 403.118429] ffff8802308e4a48 ffff8802308e4a48 ffff8802308e4200 0000000000000001
[ 403.118433] Call Trace:
[ 403.118441] [<ffffffff819916a2>] dump_stack+0x4e/0x7a
[ 403.118445] [<ffffffff810d5715>] print_circular_bug+0x1d5/0x2a0
[ 403.118449] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118455] [<ffffffff8119c172>] ? find_vmap_area+0x42/0x70
[ 403.118459] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118463] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118468] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118472] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118476] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118480] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118484] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118488] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118493] [<ffffffff810d8055>] ? mark_held_locks+0x75/0xa0
[ 403.118497] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118502] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118506] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118510] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118513] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118517] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118521] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118525] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
The reason is that vb2_fop_mmap and vb2_fop_get_unmapped_area take the core lock
while they are called with the mmap_sem semaphore held. But elsewhere in the code
the core lock is taken first but calls to copy_to/from_user() can take the mmap_sem
semaphore as well, potentially causing a classical A-B/B-A deadlock.
However, the mmap/get_unmapped_area calls really shouldn't take the core lock
at all. So what would happen if they don't take the core lock anymore?
There are two situations that need to be taken into account: calling mmap while
new buffers are being added and calling mmap while buffers are being deleted.
The first case works almost fine without a lock: in all cases mmap relies on
correctly filled-in q->num_buffers/q->num_planes values and those are only
updated by reqbufs and create_buffers *after* any new buffers have been
initialized completely. Except in one case: if an error occurred while allocating
the buffers it will increase num_buffers and rely on __vb2_queue_free to
decrease it again. So there is a short period where the buffer information
may be wrong.
The second case definitely does pose a problem: buffers may be in the process
of being deleted, without the internal structure being updated.
In order to fix this a new mutex is added to vb2_queue that is taken when
buffers are allocated or deleted, and in vb2_mmap. That way vb2_mmap won't
get stale buffer data. Note that this is a problem only for MEMORY_MMAP, so
even though __qbuf_userptr and __qbuf_dmabuf also mess around with buffers
(mem_priv in particular), this doesn't clash with vb2_mmap or
vb2_get_unmapped_area since those are MMAP specific.
As an additional bonus the hack in __buf_prepare, the USERPTR case, can be
removed as well since mmap() no longer takes the core lock.
All in all a much cleaner solution.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2014-08-07 14:47:14 +08:00
|
|
|
mutex_unlock(&q->mmap_lock);
|
2011-09-28 20:23:02 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the number of successfully allocated buffers
|
|
|
|
* to the userspace.
|
|
|
|
*/
|
2015-10-06 17:37:48 +08:00
|
|
|
*count = allocated_buffers;
|
2011-09-28 20:23:02 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-06 17:37:49 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vb2_core_create_bufs);
|
2011-09-28 20:23:02 +08:00
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
/**
|
|
|
|
* vb2_plane_vaddr() - Return a kernel virtual address of a given plane
|
|
|
|
* @vb: vb2_buffer to which the plane in question belongs to
|
|
|
|
* @plane_no: plane number for which the address is to be returned
|
|
|
|
*
|
|
|
|
* This function returns a kernel virtual address of a given plane if
|
|
|
|
* such a mapping exist, NULL otherwise.
|
|
|
|
*/
|
|
|
|
void *vb2_plane_vaddr(struct vb2_buffer *vb, unsigned int plane_no)
|
|
|
|
{
|
2011-12-15 16:53:06 +08:00
|
|
|
if (plane_no > vb->num_planes || !vb->planes[plane_no].mem_priv)
|
2010-10-11 21:56:41 +08:00
|
|
|
return NULL;
|
|
|
|
|
2014-03-17 20:54:21 +08:00
|
|
|
return call_ptr_memop(vb, vaddr, vb->planes[plane_no].mem_priv);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vb2_plane_vaddr);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vb2_plane_cookie() - Return allocator specific cookie for the given plane
|
|
|
|
* @vb: vb2_buffer to which the plane in question belongs to
|
|
|
|
* @plane_no: plane number for which the cookie is to be returned
|
|
|
|
*
|
|
|
|
* This function returns an allocator specific cookie for a given plane if
|
|
|
|
* available, NULL otherwise. The allocator should provide some simple static
|
|
|
|
* inline function, which would convert this cookie to the allocator specific
|
|
|
|
* type that can be used directly by the driver to access the buffer. This can
|
|
|
|
* be for example physical address, pointer to scatter list or IOMMU mapping.
|
|
|
|
*/
|
|
|
|
void *vb2_plane_cookie(struct vb2_buffer *vb, unsigned int plane_no)
|
|
|
|
{
|
2014-08-22 10:28:21 +08:00
|
|
|
if (plane_no >= vb->num_planes || !vb->planes[plane_no].mem_priv)
|
2010-10-11 21:56:41 +08:00
|
|
|
return NULL;
|
|
|
|
|
2014-03-17 20:54:21 +08:00
|
|
|
return call_ptr_memop(vb, cookie, vb->planes[plane_no].mem_priv);
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vb2_plane_cookie);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
|
|
|
|
* @vb: vb2_buffer returned from the driver
|
2015-01-20 23:18:16 +08:00
|
|
|
* @state: either VB2_BUF_STATE_DONE if the operation finished successfully,
|
|
|
|
* VB2_BUF_STATE_ERROR if the operation finished with an error or
|
|
|
|
* VB2_BUF_STATE_QUEUED if the driver wants to requeue buffers.
|
2014-02-25 00:51:03 +08:00
|
|
|
* If start_streaming fails then it should return buffers with state
|
|
|
|
* VB2_BUF_STATE_QUEUED to put them back into the queue.
|
2010-10-11 21:56:41 +08:00
|
|
|
*
|
|
|
|
* This function should be called by the driver after a hardware operation on
|
|
|
|
* a buffer is finished and the buffer may be returned to userspace. The driver
|
|
|
|
* cannot use this buffer anymore until it is queued back to it by videobuf
|
|
|
|
* by the means of buf_queue callback. Only buffers previously queued to the
|
|
|
|
* driver by buf_queue can be passed to this function.
|
2014-02-25 00:51:03 +08:00
|
|
|
*
|
|
|
|
* While streaming a buffer can only be returned in state DONE or ERROR.
|
|
|
|
* The start_streaming op can also return them in case the DMA engine cannot
|
|
|
|
* be started for some reason. In that case the buffers should be returned with
|
|
|
|
* state QUEUED.
|
2010-10-11 21:56:41 +08:00
|
|
|
*/
|
|
|
|
void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
|
|
|
|
{
|
|
|
|
struct vb2_queue *q = vb->vb2_queue;
|
|
|
|
unsigned long flags;
|
2012-06-14 21:37:43 +08:00
|
|
|
unsigned int plane;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2014-02-25 00:51:03 +08:00
|
|
|
if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE))
|
2010-10-11 21:56:41 +08:00
|
|
|
return;
|
|
|
|
|
2014-08-04 18:14:14 +08:00
|
|
|
if (WARN_ON(state != VB2_BUF_STATE_DONE &&
|
|
|
|
state != VB2_BUF_STATE_ERROR &&
|
2015-07-03 15:37:07 +08:00
|
|
|
state != VB2_BUF_STATE_QUEUED &&
|
|
|
|
state != VB2_BUF_STATE_REQUEUEING))
|
2014-08-04 18:14:14 +08:00
|
|
|
state = VB2_BUF_STATE_ERROR;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2014-01-29 22:53:25 +08:00
|
|
|
#ifdef CONFIG_VIDEO_ADV_DEBUG
|
|
|
|
/*
|
|
|
|
* Although this is not a callback, it still does have to balance
|
|
|
|
* with the buf_queue op. So update this counter manually.
|
|
|
|
*/
|
|
|
|
vb->cnt_buf_done++;
|
|
|
|
#endif
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(4, "done processing on buffer %d, state: %d\n",
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
vb->index, state);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2012-06-14 21:37:43 +08:00
|
|
|
/* sync buffers */
|
|
|
|
for (plane = 0; plane < vb->num_planes; ++plane)
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_memop(vb, finish, vb->planes[plane].mem_priv);
|
2012-06-14 21:37:43 +08:00
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
spin_lock_irqsave(&q->done_lock, flags);
|
2015-07-03 15:37:07 +08:00
|
|
|
if (state == VB2_BUF_STATE_QUEUED ||
|
|
|
|
state == VB2_BUF_STATE_REQUEUEING) {
|
|
|
|
vb->state = VB2_BUF_STATE_QUEUED;
|
|
|
|
} else {
|
|
|
|
/* Add the buffer to the done buffers list */
|
2014-02-25 00:51:03 +08:00
|
|
|
list_add_tail(&vb->done_entry, &q->done_list);
|
2015-07-03 15:37:07 +08:00
|
|
|
vb->state = state;
|
|
|
|
}
|
2014-02-06 16:46:11 +08:00
|
|
|
atomic_dec(&q->owned_by_drv_count);
|
2010-10-11 21:56:41 +08:00
|
|
|
spin_unlock_irqrestore(&q->done_lock, flags);
|
|
|
|
|
2015-07-10 21:49:26 +08:00
|
|
|
trace_vb2_buf_done(q, vb);
|
|
|
|
|
2015-07-03 15:37:07 +08:00
|
|
|
switch (state) {
|
|
|
|
case VB2_BUF_STATE_QUEUED:
|
|
|
|
return;
|
|
|
|
case VB2_BUF_STATE_REQUEUEING:
|
2015-01-20 23:18:16 +08:00
|
|
|
if (q->start_streaming_called)
|
|
|
|
__enqueue_in_driver(vb);
|
2014-02-25 00:51:03 +08:00
|
|
|
return;
|
2015-07-03 15:37:07 +08:00
|
|
|
default:
|
|
|
|
/* Inform any processes that may be waiting for buffers */
|
|
|
|
wake_up(&q->done_wq);
|
|
|
|
break;
|
2015-01-20 23:18:16 +08:00
|
|
|
}
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vb2_buffer_done);
|
|
|
|
|
2014-03-10 08:42:52 +08:00
|
|
|
/**
|
|
|
|
* vb2_discard_done() - discard all buffers marked as DONE
|
|
|
|
* @q: videobuf2 queue
|
|
|
|
*
|
|
|
|
* This function is intended to be used with suspend/resume operations. It
|
|
|
|
* discards all 'done' buffers as they would be too old to be requested after
|
|
|
|
* resume.
|
|
|
|
*
|
|
|
|
* Drivers must stop the hardware and synchronize with interrupt handlers and/or
|
|
|
|
* delayed works before calling this function to make sure no buffer will be
|
|
|
|
* touched by the driver and/or hardware.
|
|
|
|
*/
|
|
|
|
void vb2_discard_done(struct vb2_queue *q)
|
|
|
|
{
|
|
|
|
struct vb2_buffer *vb;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&q->done_lock, flags);
|
|
|
|
list_for_each_entry(vb, &q->done_list, done_entry)
|
|
|
|
vb->state = VB2_BUF_STATE_ERROR;
|
|
|
|
spin_unlock_irqrestore(&q->done_lock, flags);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vb2_discard_done);
|
|
|
|
|
2014-03-10 23:23:13 +08:00
|
|
|
/**
|
|
|
|
* __qbuf_mmap() - handle qbuf of an MMAP buffer
|
|
|
|
*/
|
2015-10-06 17:37:48 +08:00
|
|
|
static int __qbuf_mmap(struct vb2_buffer *vb, const void *pb)
|
2014-03-10 23:23:13 +08:00
|
|
|
{
|
2015-10-06 17:37:48 +08:00
|
|
|
int ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
|
|
|
|
vb, pb, vb->planes);
|
|
|
|
return ret ? ret : call_vb_qop(vb, buf_prepare, vb);
|
2014-03-10 23:23:13 +08:00
|
|
|
}
|
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
/**
|
|
|
|
* __qbuf_userptr() - handle qbuf of a USERPTR buffer
|
|
|
|
*/
|
2015-10-06 17:37:48 +08:00
|
|
|
static int __qbuf_userptr(struct vb2_buffer *vb, const void *pb)
|
2010-10-11 21:56:41 +08:00
|
|
|
{
|
2015-10-06 17:37:47 +08:00
|
|
|
struct vb2_plane planes[VB2_MAX_PLANES];
|
2010-10-11 21:56:41 +08:00
|
|
|
struct vb2_queue *q = vb->vb2_queue;
|
|
|
|
void *mem_priv;
|
|
|
|
unsigned int plane;
|
|
|
|
int ret;
|
2014-11-18 20:50:58 +08:00
|
|
|
enum dma_data_direction dma_dir =
|
2015-10-06 17:37:47 +08:00
|
|
|
q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
|
2014-01-30 00:36:53 +08:00
|
|
|
bool reacquired = vb->planes[0].mem_priv == NULL;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2014-04-07 19:44:56 +08:00
|
|
|
memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
|
2012-09-28 17:12:53 +08:00
|
|
|
/* Copy relevant information provided by the userspace */
|
2015-10-06 17:37:48 +08:00
|
|
|
ret = call_bufop(vb->vb2_queue, fill_vb2_buffer, vb, pb, planes);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
for (plane = 0; plane < vb->num_planes; ++plane) {
|
|
|
|
/* Skip the plane if already verified */
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
if (vb->planes[plane].m.userptr &&
|
|
|
|
vb->planes[plane].m.userptr == planes[plane].m.userptr
|
|
|
|
&& vb->planes[plane].length == planes[plane].length)
|
2010-10-11 21:56:41 +08:00
|
|
|
continue;
|
|
|
|
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(3, "userspace address for plane %d changed, "
|
2010-10-11 21:56:41 +08:00
|
|
|
"reacquiring memory\n", plane);
|
|
|
|
|
2011-08-24 17:36:26 +08:00
|
|
|
/* Check if the provided plane buffer is large enough */
|
|
|
|
if (planes[plane].length < q->plane_sizes[plane]) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "provided buffer size %u is less than "
|
2013-08-20 15:48:06 +08:00
|
|
|
"setup size %u for plane %d\n",
|
|
|
|
planes[plane].length,
|
|
|
|
q->plane_sizes[plane], plane);
|
2011-10-03 14:21:45 +08:00
|
|
|
ret = -EINVAL;
|
2011-08-24 17:36:26 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
/* Release previously acquired memory if present */
|
2014-01-30 00:36:53 +08:00
|
|
|
if (vb->planes[plane].mem_priv) {
|
|
|
|
if (!reacquired) {
|
|
|
|
reacquired = true;
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_vb_qop(vb, buf_cleanup, vb);
|
2014-01-30 00:36:53 +08:00
|
|
|
}
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
|
2014-01-30 00:36:53 +08:00
|
|
|
}
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
vb->planes[plane].mem_priv = NULL;
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
vb->planes[plane].bytesused = 0;
|
|
|
|
vb->planes[plane].length = 0;
|
|
|
|
vb->planes[plane].m.userptr = 0;
|
|
|
|
vb->planes[plane].data_offset = 0;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
/* Acquire each plane's memory */
|
2014-03-17 20:54:21 +08:00
|
|
|
mem_priv = call_ptr_memop(vb, get_userptr, q->alloc_ctx[plane],
|
2011-12-15 16:53:06 +08:00
|
|
|
planes[plane].m.userptr,
|
2014-11-18 20:50:58 +08:00
|
|
|
planes[plane].length, dma_dir);
|
2011-12-15 16:53:06 +08:00
|
|
|
if (IS_ERR_OR_NULL(mem_priv)) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "failed acquiring userspace "
|
2010-10-11 21:56:41 +08:00
|
|
|
"memory for plane %d\n", plane);
|
2011-12-15 16:53:06 +08:00
|
|
|
ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL;
|
|
|
|
goto err;
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
2011-12-15 16:53:06 +08:00
|
|
|
vb->planes[plane].mem_priv = mem_priv;
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that everything is in order, copy relevant information
|
|
|
|
* provided by userspace.
|
|
|
|
*/
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
for (plane = 0; plane < vb->num_planes; ++plane) {
|
|
|
|
vb->planes[plane].bytesused = planes[plane].bytesused;
|
|
|
|
vb->planes[plane].length = planes[plane].length;
|
|
|
|
vb->planes[plane].m.userptr = planes[plane].m.userptr;
|
|
|
|
vb->planes[plane].data_offset = planes[plane].data_offset;
|
|
|
|
}
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2014-01-30 00:36:53 +08:00
|
|
|
if (reacquired) {
|
|
|
|
/*
|
|
|
|
* One or more planes changed, so we must call buf_init to do
|
|
|
|
* the driver-specific initialization on the newly acquired
|
|
|
|
* buffer, if provided.
|
|
|
|
*/
|
|
|
|
ret = call_vb_qop(vb, buf_init, vb);
|
|
|
|
if (ret) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "buffer initialization failed\n");
|
2014-01-30 00:36:53 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = call_vb_qop(vb, buf_prepare, vb);
|
|
|
|
if (ret) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "buffer preparation failed\n");
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_vb_qop(vb, buf_cleanup, vb);
|
2014-01-30 00:36:53 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
/* In case of errors, release planes that were already acquired */
|
2011-08-24 17:36:26 +08:00
|
|
|
for (plane = 0; plane < vb->num_planes; ++plane) {
|
|
|
|
if (vb->planes[plane].mem_priv)
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
call_void_memop(vb, put_userptr,
|
|
|
|
vb->planes[plane].mem_priv);
|
2011-08-24 17:36:26 +08:00
|
|
|
vb->planes[plane].mem_priv = NULL;
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
vb->planes[plane].m.userptr = 0;
|
|
|
|
vb->planes[plane].length = 0;
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-06-14 21:37:37 +08:00
|
|
|
/**
|
|
|
|
* __qbuf_dmabuf() - handle qbuf of a DMABUF buffer
|
|
|
|
*/
|
2015-10-06 17:37:48 +08:00
|
|
|
static int __qbuf_dmabuf(struct vb2_buffer *vb, const void *pb)
|
2012-06-14 21:37:37 +08:00
|
|
|
{
|
2015-10-06 17:37:47 +08:00
|
|
|
struct vb2_plane planes[VB2_MAX_PLANES];
|
2012-06-14 21:37:37 +08:00
|
|
|
struct vb2_queue *q = vb->vb2_queue;
|
|
|
|
void *mem_priv;
|
|
|
|
unsigned int plane;
|
|
|
|
int ret;
|
2014-11-18 20:50:58 +08:00
|
|
|
enum dma_data_direction dma_dir =
|
2015-10-06 17:37:47 +08:00
|
|
|
q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
|
2014-01-30 00:36:53 +08:00
|
|
|
bool reacquired = vb->planes[0].mem_priv == NULL;
|
2012-06-14 21:37:37 +08:00
|
|
|
|
2014-04-07 19:44:56 +08:00
|
|
|
memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
|
2014-01-01 20:10:48 +08:00
|
|
|
/* Copy relevant information provided by the userspace */
|
2015-10-06 17:37:48 +08:00
|
|
|
ret = call_bufop(vb->vb2_queue, fill_vb2_buffer, vb, pb, planes);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2012-06-14 21:37:37 +08:00
|
|
|
|
|
|
|
for (plane = 0; plane < vb->num_planes; ++plane) {
|
|
|
|
struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
|
|
|
|
|
|
|
|
if (IS_ERR_OR_NULL(dbuf)) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "invalid dmabuf fd for plane %d\n",
|
2012-06-14 21:37:37 +08:00
|
|
|
plane);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* use DMABUF size if length is not provided */
|
|
|
|
if (planes[plane].length == 0)
|
|
|
|
planes[plane].length = dbuf->size;
|
|
|
|
|
2014-04-07 19:44:56 +08:00
|
|
|
if (planes[plane].length < q->plane_sizes[plane]) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "invalid dmabuf length for plane %d\n",
|
2013-11-29 15:50:29 +08:00
|
|
|
plane);
|
2012-06-14 21:37:37 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip the plane if already verified */
|
|
|
|
if (dbuf == vb->planes[plane].dbuf &&
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
vb->planes[plane].length == planes[plane].length) {
|
2012-06-14 21:37:37 +08:00
|
|
|
dma_buf_put(dbuf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "buffer for plane %d changed\n", plane);
|
2012-06-14 21:37:37 +08:00
|
|
|
|
2014-01-30 00:36:53 +08:00
|
|
|
if (!reacquired) {
|
|
|
|
reacquired = true;
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_vb_qop(vb, buf_cleanup, vb);
|
2014-01-30 00:36:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 21:37:37 +08:00
|
|
|
/* Release previously acquired memory if present */
|
2014-01-29 22:53:25 +08:00
|
|
|
__vb2_plane_dmabuf_put(vb, &vb->planes[plane]);
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
vb->planes[plane].bytesused = 0;
|
|
|
|
vb->planes[plane].length = 0;
|
|
|
|
vb->planes[plane].m.fd = 0;
|
|
|
|
vb->planes[plane].data_offset = 0;
|
2012-06-14 21:37:37 +08:00
|
|
|
|
|
|
|
/* Acquire each plane's memory */
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
mem_priv = call_ptr_memop(vb, attach_dmabuf,
|
|
|
|
q->alloc_ctx[plane], dbuf, planes[plane].length,
|
|
|
|
dma_dir);
|
2012-06-14 21:37:37 +08:00
|
|
|
if (IS_ERR(mem_priv)) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "failed to attach dmabuf\n");
|
2012-06-14 21:37:37 +08:00
|
|
|
ret = PTR_ERR(mem_priv);
|
|
|
|
dma_buf_put(dbuf);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
vb->planes[plane].dbuf = dbuf;
|
|
|
|
vb->planes[plane].mem_priv = mem_priv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: This pins the buffer(s) with dma_buf_map_attachment()).. but
|
|
|
|
* really we want to do this just before the DMA, not while queueing
|
|
|
|
* the buffer(s)..
|
|
|
|
*/
|
|
|
|
for (plane = 0; plane < vb->num_planes; ++plane) {
|
2014-01-29 22:53:25 +08:00
|
|
|
ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv);
|
2012-06-14 21:37:37 +08:00
|
|
|
if (ret) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "failed to map dmabuf for plane %d\n",
|
2012-06-14 21:37:37 +08:00
|
|
|
plane);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
vb->planes[plane].dbuf_mapped = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that everything is in order, copy relevant information
|
|
|
|
* provided by userspace.
|
|
|
|
*/
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
for (plane = 0; plane < vb->num_planes; ++plane) {
|
|
|
|
vb->planes[plane].bytesused = planes[plane].bytesused;
|
|
|
|
vb->planes[plane].length = planes[plane].length;
|
|
|
|
vb->planes[plane].m.fd = planes[plane].m.fd;
|
|
|
|
vb->planes[plane].data_offset = planes[plane].data_offset;
|
|
|
|
}
|
2012-06-14 21:37:37 +08:00
|
|
|
|
2014-01-30 00:36:53 +08:00
|
|
|
if (reacquired) {
|
|
|
|
/*
|
|
|
|
* Call driver-specific initialization on the newly acquired buffer,
|
|
|
|
* if provided.
|
|
|
|
*/
|
|
|
|
ret = call_vb_qop(vb, buf_init, vb);
|
|
|
|
if (ret) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "buffer initialization failed\n");
|
2014-01-30 00:36:53 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = call_vb_qop(vb, buf_prepare, vb);
|
|
|
|
if (ret) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "buffer preparation failed\n");
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_vb_qop(vb, buf_cleanup, vb);
|
2014-01-30 00:36:53 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2012-06-14 21:37:37 +08:00
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
/* In case of errors, release planes that were already acquired */
|
|
|
|
__vb2_buf_dmabuf_put(vb);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
/**
|
|
|
|
* __enqueue_in_driver() - enqueue a vb2_buffer in driver for processing
|
|
|
|
*/
|
|
|
|
static void __enqueue_in_driver(struct vb2_buffer *vb)
|
|
|
|
{
|
|
|
|
struct vb2_queue *q = vb->vb2_queue;
|
2012-06-14 21:37:43 +08:00
|
|
|
unsigned int plane;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
vb->state = VB2_BUF_STATE_ACTIVE;
|
2014-02-06 16:46:11 +08:00
|
|
|
atomic_inc(&q->owned_by_drv_count);
|
2012-06-14 21:37:43 +08:00
|
|
|
|
2015-07-10 21:49:26 +08:00
|
|
|
trace_vb2_buf_queue(q, vb);
|
|
|
|
|
2012-06-14 21:37:43 +08:00
|
|
|
/* sync buffers */
|
|
|
|
for (plane = 0; plane < vb->num_planes; ++plane)
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_memop(vb, prepare, vb->planes[plane].mem_priv);
|
2012-06-14 21:37:43 +08:00
|
|
|
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_vb_qop(vb, buf_queue, vb);
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
static int __buf_prepare(struct vb2_buffer *vb, const void *pb)
|
2011-08-31 17:51:10 +08:00
|
|
|
{
|
|
|
|
struct vb2_queue *q = vb->vb2_queue;
|
|
|
|
int ret;
|
|
|
|
|
2014-06-04 05:53:25 +08:00
|
|
|
if (q->error) {
|
|
|
|
dprintk(1, "fatal error occurred on queue\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2013-12-14 00:13:38 +08:00
|
|
|
vb->state = VB2_BUF_STATE_PREPARING;
|
2014-02-25 01:44:50 +08:00
|
|
|
|
2011-08-31 17:51:10 +08:00
|
|
|
switch (q->memory) {
|
2015-10-06 17:37:47 +08:00
|
|
|
case VB2_MEMORY_MMAP:
|
2015-10-06 17:37:48 +08:00
|
|
|
ret = __qbuf_mmap(vb, pb);
|
2011-08-31 17:51:10 +08:00
|
|
|
break;
|
2015-10-06 17:37:47 +08:00
|
|
|
case VB2_MEMORY_USERPTR:
|
2015-10-06 17:37:48 +08:00
|
|
|
ret = __qbuf_userptr(vb, pb);
|
2011-08-31 17:51:10 +08:00
|
|
|
break;
|
2015-10-06 17:37:47 +08:00
|
|
|
case VB2_MEMORY_DMABUF:
|
2015-10-06 17:37:48 +08:00
|
|
|
ret = __qbuf_dmabuf(vb, pb);
|
2012-06-14 21:37:37 +08:00
|
|
|
break;
|
2011-08-31 17:51:10 +08:00
|
|
|
default:
|
|
|
|
WARN(1, "Invalid queue type\n");
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "buffer preparation failed: %d\n", ret);
|
2013-12-14 00:13:38 +08:00
|
|
|
vb->state = ret ? VB2_BUF_STATE_DEQUEUED : VB2_BUF_STATE_PREPARED;
|
2011-08-31 17:51:10 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
/**
|
|
|
|
* vb2_core_prepare_buf() - Pass ownership of a buffer from userspace
|
|
|
|
* to the kernel
|
|
|
|
* @q: videobuf2 queue
|
|
|
|
* @index: id number of the buffer
|
|
|
|
* @pb: buffer structure passed from userspace to vidioc_prepare_buf
|
|
|
|
* handler in driver
|
|
|
|
*
|
|
|
|
* Should be called from vidioc_prepare_buf ioctl handler of a driver.
|
|
|
|
* The passed buffer should have been verified.
|
|
|
|
* This function calls buf_prepare callback in the driver (if provided),
|
|
|
|
* in which driver-specific buffer initialization can be performed,
|
|
|
|
*
|
|
|
|
* The return values from this function are intended to be directly returned
|
|
|
|
* from vidioc_prepare_buf handler in driver.
|
|
|
|
*/
|
2015-10-06 17:37:49 +08:00
|
|
|
int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb)
|
2015-10-06 17:37:48 +08:00
|
|
|
{
|
|
|
|
struct vb2_buffer *vb;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
vb = q->bufs[index];
|
|
|
|
if (vb->state != VB2_BUF_STATE_DEQUEUED) {
|
|
|
|
dprintk(1, "invalid buffer state %d\n",
|
|
|
|
vb->state);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = __buf_prepare(vb, pb);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Fill buffer information for the userspace */
|
|
|
|
ret = call_bufop(q, fill_user_buffer, vb, pb);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
dprintk(1, "prepare of buffer %d succeeded\n", vb->index);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2015-10-06 17:37:49 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vb2_core_prepare_buf);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2013-12-14 00:13:42 +08:00
|
|
|
/**
|
|
|
|
* vb2_start_streaming() - Attempt to start streaming.
|
|
|
|
* @q: videobuf2 queue
|
|
|
|
*
|
2014-02-25 00:51:03 +08:00
|
|
|
* Attempt to start streaming. When this function is called there must be
|
|
|
|
* at least q->min_buffers_needed buffers queued up (i.e. the minimum
|
|
|
|
* number of buffers required for the DMA engine to function). If the
|
|
|
|
* @start_streaming op fails it is supposed to return all the driver-owned
|
|
|
|
* buffers back to vb2 in state QUEUED. Check if that happened and if
|
|
|
|
* not warn and reclaim them forcefully.
|
2013-12-14 00:13:42 +08:00
|
|
|
*/
|
|
|
|
static int vb2_start_streaming(struct vb2_queue *q)
|
|
|
|
{
|
2014-02-25 00:51:03 +08:00
|
|
|
struct vb2_buffer *vb;
|
2013-12-14 00:13:42 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
2014-02-25 00:51:03 +08:00
|
|
|
* If any buffers were queued before streamon,
|
|
|
|
* we can now pass them to driver for processing.
|
2013-12-14 00:13:42 +08:00
|
|
|
*/
|
2014-02-25 00:51:03 +08:00
|
|
|
list_for_each_entry(vb, &q->queued_list, queued_entry)
|
|
|
|
__enqueue_in_driver(vb);
|
|
|
|
|
|
|
|
/* Tell the driver to start streaming */
|
2014-06-24 05:00:22 +08:00
|
|
|
q->start_streaming_called = 1;
|
2014-02-25 00:51:03 +08:00
|
|
|
ret = call_qop(q, start_streaming, q,
|
|
|
|
atomic_read(&q->owned_by_drv_count));
|
|
|
|
if (!ret)
|
2013-12-14 00:13:42 +08:00
|
|
|
return 0;
|
2014-02-25 00:51:03 +08:00
|
|
|
|
2014-06-24 05:00:22 +08:00
|
|
|
q->start_streaming_called = 0;
|
|
|
|
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "driver refused to start streaming\n");
|
2014-08-04 13:33:53 +08:00
|
|
|
/*
|
|
|
|
* If you see this warning, then the driver isn't cleaning up properly
|
|
|
|
* after a failed start_streaming(). See the start_streaming()
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
* documentation in videobuf2-core.h for more information how buffers
|
2014-08-04 13:33:53 +08:00
|
|
|
* should be returned to vb2 in start_streaming().
|
|
|
|
*/
|
2014-02-25 00:51:03 +08:00
|
|
|
if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Forcefully reclaim buffers if the driver did not
|
|
|
|
* correctly return them to vb2.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < q->num_buffers; ++i) {
|
|
|
|
vb = q->bufs[i];
|
|
|
|
if (vb->state == VB2_BUF_STATE_ACTIVE)
|
|
|
|
vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED);
|
|
|
|
}
|
|
|
|
/* Must be zero now */
|
|
|
|
WARN_ON(atomic_read(&q->owned_by_drv_count));
|
2013-12-14 00:13:42 +08:00
|
|
|
}
|
2014-08-04 18:14:14 +08:00
|
|
|
/*
|
|
|
|
* If done_list is not empty, then start_streaming() didn't call
|
|
|
|
* vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED) but STATE_ERROR or
|
|
|
|
* STATE_DONE.
|
|
|
|
*/
|
|
|
|
WARN_ON(!list_empty(&q->done_list));
|
2013-12-14 00:13:42 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
/**
|
|
|
|
* vb2_core_qbuf() - Queue a buffer from userspace
|
|
|
|
* @q: videobuf2 queue
|
|
|
|
* @index: id number of the buffer
|
|
|
|
* @pb: buffer structure passed from userspace to vidioc_qbuf handler
|
|
|
|
* in driver
|
|
|
|
*
|
|
|
|
* Should be called from vidioc_qbuf ioctl handler of a driver.
|
|
|
|
* The passed buffer should have been verified.
|
|
|
|
* This function:
|
|
|
|
* 1) if necessary, calls buf_prepare callback in the driver (if provided), in
|
|
|
|
* which driver-specific buffer initialization can be performed,
|
|
|
|
* 2) if streaming is on, queues the buffer in driver by the means of buf_queue
|
|
|
|
* callback for processing.
|
|
|
|
*
|
|
|
|
* The return values from this function are intended to be directly returned
|
|
|
|
* from vidioc_qbuf handler in driver.
|
|
|
|
*/
|
2015-10-06 17:37:49 +08:00
|
|
|
int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb)
|
2013-08-09 19:11:26 +08:00
|
|
|
{
|
2013-12-14 00:13:39 +08:00
|
|
|
struct vb2_buffer *vb;
|
2015-10-06 17:37:48 +08:00
|
|
|
int ret;
|
2013-12-14 00:13:39 +08:00
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
vb = q->bufs[index];
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2011-08-31 17:51:10 +08:00
|
|
|
switch (vb->state) {
|
|
|
|
case VB2_BUF_STATE_DEQUEUED:
|
2015-10-06 17:37:48 +08:00
|
|
|
ret = __buf_prepare(vb, pb);
|
2011-08-31 17:51:10 +08:00
|
|
|
if (ret)
|
2013-08-09 19:11:26 +08:00
|
|
|
return ret;
|
2013-12-14 00:13:39 +08:00
|
|
|
break;
|
2011-08-31 17:51:10 +08:00
|
|
|
case VB2_BUF_STATE_PREPARED:
|
|
|
|
break;
|
2013-12-14 00:13:38 +08:00
|
|
|
case VB2_BUF_STATE_PREPARING:
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "buffer still being prepared\n");
|
2013-12-14 00:13:38 +08:00
|
|
|
return -EINVAL;
|
2011-08-31 17:51:10 +08:00
|
|
|
default:
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "invalid buffer state %d\n", vb->state);
|
2013-08-09 19:11:26 +08:00
|
|
|
return -EINVAL;
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add to the queued buffers list, a buffer will stay on it until
|
|
|
|
* dequeued in dqbuf.
|
|
|
|
*/
|
|
|
|
list_add_tail(&vb->queued_entry, &q->queued_list);
|
2014-02-25 00:51:03 +08:00
|
|
|
q->queued_count++;
|
2014-09-21 03:16:35 +08:00
|
|
|
q->waiting_for_buffers = false;
|
2010-10-11 21:56:41 +08:00
|
|
|
vb->state = VB2_BUF_STATE_QUEUED;
|
2015-10-06 17:37:48 +08:00
|
|
|
|
2015-11-03 18:16:38 +08:00
|
|
|
call_bufop(q, copy_timestamp, vb, pb);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2015-07-10 21:49:26 +08:00
|
|
|
trace_vb2_qbuf(q, vb);
|
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
/*
|
|
|
|
* If already streaming, give the buffer to driver for processing.
|
|
|
|
* If not, the buffer will be given to driver on next streamon.
|
|
|
|
*/
|
2014-02-25 00:51:03 +08:00
|
|
|
if (q->start_streaming_called)
|
2010-10-11 21:56:41 +08:00
|
|
|
__enqueue_in_driver(vb);
|
|
|
|
|
2013-12-14 00:13:39 +08:00
|
|
|
/* Fill buffer information for the userspace */
|
2015-10-06 17:37:48 +08:00
|
|
|
ret = call_bufop(q, fill_user_buffer, vb, pb);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2011-09-28 18:23:27 +08:00
|
|
|
|
2014-02-25 00:51:03 +08:00
|
|
|
/*
|
|
|
|
* If streamon has been called, and we haven't yet called
|
|
|
|
* start_streaming() since not enough buffers were queued, and
|
|
|
|
* we now have reached the minimum number of queued buffers,
|
|
|
|
* then we can finally call start_streaming().
|
|
|
|
*/
|
|
|
|
if (q->streaming && !q->start_streaming_called &&
|
|
|
|
q->queued_count >= q->min_buffers_needed) {
|
2013-12-14 00:13:42 +08:00
|
|
|
ret = vb2_start_streaming(q);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
dprintk(1, "qbuf of buffer %d succeeded\n", vb->index);
|
2013-12-14 00:13:39 +08:00
|
|
|
return 0;
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
2015-10-06 17:37:49 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vb2_core_qbuf);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* __vb2_wait_for_done_vb() - wait for a buffer to become available
|
|
|
|
* for dequeuing
|
|
|
|
*
|
|
|
|
* Will sleep if required for nonblocking == false.
|
|
|
|
*/
|
|
|
|
static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* All operations on vb_done_list are performed under done_lock
|
|
|
|
* spinlock protection. However, buffers may be removed from
|
|
|
|
* it and returned to userspace only while holding both driver's
|
|
|
|
* lock and the done_lock spinlock. Thus we can be sure that as
|
|
|
|
* long as we hold the driver's lock, the list will remain not
|
|
|
|
* empty if list_empty() check succeeds.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!q->streaming) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "streaming off, will not wait for buffers\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-06-04 05:53:25 +08:00
|
|
|
if (q->error) {
|
|
|
|
dprintk(1, "Queue in error state, will not wait for buffers\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2015-05-04 18:51:06 +08:00
|
|
|
if (q->last_buffer_dequeued) {
|
|
|
|
dprintk(3, "last buffer dequeued already, will not wait for buffers\n");
|
|
|
|
return -EPIPE;
|
|
|
|
}
|
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
if (!list_empty(&q->done_list)) {
|
|
|
|
/*
|
|
|
|
* Found a buffer that we were waiting for.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nonblocking) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "nonblocking and no buffers to dequeue, "
|
2010-10-11 21:56:41 +08:00
|
|
|
"will not wait\n");
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We are streaming and blocking, wait for another buffer to
|
|
|
|
* become ready or for streamoff. Driver's lock is released to
|
|
|
|
* allow streamoff or qbuf to be called while waiting.
|
|
|
|
*/
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_qop(q, wait_prepare, q);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* All locks have been released, it is safe to sleep now.
|
|
|
|
*/
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(3, "will sleep waiting for buffers\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
ret = wait_event_interruptible(q->done_wq,
|
2014-06-04 05:53:25 +08:00
|
|
|
!list_empty(&q->done_list) || !q->streaming ||
|
|
|
|
q->error);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to reevaluate both conditions again after reacquiring
|
|
|
|
* the locks or return an error if one occurred.
|
|
|
|
*/
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_qop(q, wait_finish, q);
|
2012-09-28 17:12:53 +08:00
|
|
|
if (ret) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "sleep was interrupted\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
return ret;
|
2012-09-28 17:12:53 +08:00
|
|
|
}
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __vb2_get_done_vb() - get a buffer ready for dequeuing
|
|
|
|
*
|
|
|
|
* Will sleep if required for nonblocking == false.
|
|
|
|
*/
|
|
|
|
static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb,
|
2015-10-06 17:37:48 +08:00
|
|
|
int nonblocking)
|
2010-10-11 21:56:41 +08:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for at least one buffer to become available on the done_list.
|
|
|
|
*/
|
|
|
|
ret = __vb2_wait_for_done_vb(q, nonblocking);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Driver's lock has been held since we last verified that done_list
|
|
|
|
* is not empty, so no need for another list_empty(done_list) check.
|
|
|
|
*/
|
|
|
|
spin_lock_irqsave(&q->done_lock, flags);
|
|
|
|
*vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry);
|
2012-09-28 17:12:53 +08:00
|
|
|
/*
|
|
|
|
* Only remove the buffer from done_list if v4l2_buffer can handle all
|
|
|
|
* the planes.
|
2015-10-06 17:37:48 +08:00
|
|
|
* Verifying planes is NOT necessary since it already has been checked
|
|
|
|
* before the buffer is queued/prepared. So it can never fail.
|
2012-09-28 17:12:53 +08:00
|
|
|
*/
|
2015-10-06 17:37:48 +08:00
|
|
|
list_del(&(*vb)->done_entry);
|
2010-10-11 21:56:41 +08:00
|
|
|
spin_unlock_irqrestore(&q->done_lock, flags);
|
|
|
|
|
2012-09-28 17:12:53 +08:00
|
|
|
return ret;
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vb2_wait_for_all_buffers() - wait until all buffers are given back to vb2
|
|
|
|
* @q: videobuf2 queue
|
|
|
|
*
|
|
|
|
* This function will wait until all buffers that have been given to the driver
|
|
|
|
* by buf_queue() are given back to vb2 with vb2_buffer_done(). It doesn't call
|
|
|
|
* wait_prepare, wait_finish pair. It is intended to be called with all locks
|
|
|
|
* taken, for example from stop_streaming() callback.
|
|
|
|
*/
|
|
|
|
int vb2_wait_for_all_buffers(struct vb2_queue *q)
|
|
|
|
{
|
|
|
|
if (!q->streaming) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "streaming off, will not wait for buffers\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-02-25 00:51:03 +08:00
|
|
|
if (q->start_streaming_called)
|
2014-02-06 16:46:11 +08:00
|
|
|
wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count));
|
2010-10-11 21:56:41 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
|
|
|
|
|
2012-06-14 21:37:37 +08:00
|
|
|
/**
|
|
|
|
* __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
|
|
|
|
*/
|
|
|
|
static void __vb2_dqbuf(struct vb2_buffer *vb)
|
|
|
|
{
|
|
|
|
struct vb2_queue *q = vb->vb2_queue;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* nothing to do if the buffer is already dequeued */
|
|
|
|
if (vb->state == VB2_BUF_STATE_DEQUEUED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
vb->state = VB2_BUF_STATE_DEQUEUED;
|
|
|
|
|
|
|
|
/* unmap DMABUF buffer */
|
2015-10-06 17:37:47 +08:00
|
|
|
if (q->memory == VB2_MEMORY_DMABUF)
|
2012-06-14 21:37:37 +08:00
|
|
|
for (i = 0; i < vb->num_planes; ++i) {
|
|
|
|
if (!vb->planes[i].dbuf_mapped)
|
|
|
|
continue;
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_memop(vb, unmap_dmabuf, vb->planes[i].mem_priv);
|
2012-06-14 21:37:37 +08:00
|
|
|
vb->planes[i].dbuf_mapped = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
/**
|
|
|
|
* vb2_dqbuf() - Dequeue a buffer to the userspace
|
|
|
|
* @q: videobuf2 queue
|
|
|
|
* @pb: buffer structure passed from userspace to vidioc_dqbuf handler
|
|
|
|
* in driver
|
|
|
|
* @nonblocking: if true, this call will not sleep waiting for a buffer if no
|
|
|
|
* buffers ready for dequeuing are present. Normally the driver
|
|
|
|
* would be passing (file->f_flags & O_NONBLOCK) here
|
|
|
|
*
|
|
|
|
* Should be called from vidioc_dqbuf ioctl handler of a driver.
|
|
|
|
* The passed buffer should have been verified.
|
|
|
|
* This function:
|
|
|
|
* 1) calls buf_finish callback in the driver (if provided), in which
|
|
|
|
* driver can perform any additional operations that may be required before
|
|
|
|
* returning the buffer to userspace, such as cache sync,
|
|
|
|
* 2) the buffer struct members are filled with relevant information for
|
|
|
|
* the userspace.
|
|
|
|
*
|
|
|
|
* The return values from this function are intended to be directly returned
|
|
|
|
* from vidioc_dqbuf handler in driver.
|
|
|
|
*/
|
2015-10-06 17:37:49 +08:00
|
|
|
int vb2_core_dqbuf(struct vb2_queue *q, void *pb, bool nonblocking)
|
2010-10-11 21:56:41 +08:00
|
|
|
{
|
|
|
|
struct vb2_buffer *vb = NULL;
|
|
|
|
int ret;
|
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
ret = __vb2_get_done_vb(q, &vb, nonblocking);
|
2012-09-28 17:12:53 +08:00
|
|
|
if (ret < 0)
|
2010-10-11 21:56:41 +08:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (vb->state) {
|
|
|
|
case VB2_BUF_STATE_DONE:
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(3, "returning done buffer\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
break;
|
|
|
|
case VB2_BUF_STATE_ERROR:
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(3, "returning done buffer with errors\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
break;
|
|
|
|
default:
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "invalid buffer state\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_vb_qop(vb, buf_finish, vb);
|
2014-03-01 00:30:48 +08:00
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
/* Fill buffer information for the userspace */
|
2015-10-06 17:37:48 +08:00
|
|
|
ret = call_bufop(q, fill_user_buffer, vb, pb);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
/* Remove from videobuf queue */
|
|
|
|
list_del(&vb->queued_entry);
|
2014-02-25 00:51:03 +08:00
|
|
|
q->queued_count--;
|
2015-07-10 21:49:26 +08:00
|
|
|
|
|
|
|
trace_vb2_dqbuf(q, vb);
|
|
|
|
|
2012-06-14 21:37:37 +08:00
|
|
|
/* go back to dequeued state */
|
|
|
|
__vb2_dqbuf(vb);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
dprintk(1, "dqbuf of buffer %d, with state %d\n",
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
vb->index, vb->state);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
return 0;
|
2015-10-06 17:37:48 +08:00
|
|
|
|
|
|
|
}
|
2015-10-06 17:37:49 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vb2_core_dqbuf);
|
2015-10-06 17:37:48 +08:00
|
|
|
|
2015-10-06 17:37:49 +08:00
|
|
|
/**
|
|
|
|
* __vb2_queue_cancel() - cancel and stop (pause) streaming
|
|
|
|
*
|
|
|
|
* Removes all queued buffers from driver's queue and all buffers queued by
|
|
|
|
* userspace from videobuf's queue. Returns to state after reqbufs.
|
|
|
|
*/
|
|
|
|
static void __vb2_queue_cancel(struct vb2_queue *q)
|
2015-10-06 17:37:48 +08:00
|
|
|
{
|
2015-10-06 17:37:49 +08:00
|
|
|
unsigned int i;
|
2011-08-29 19:51:49 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Tell driver to stop all transactions and release all queued
|
|
|
|
* buffers.
|
|
|
|
*/
|
2014-02-25 00:51:03 +08:00
|
|
|
if (q->start_streaming_called)
|
2014-04-17 13:47:21 +08:00
|
|
|
call_void_qop(q, stop_streaming, q);
|
2014-02-25 00:51:03 +08:00
|
|
|
|
2014-08-04 13:33:53 +08:00
|
|
|
/*
|
|
|
|
* If you see this warning, then the driver isn't cleaning up properly
|
|
|
|
* in stop_streaming(). See the stop_streaming() documentation in
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
* videobuf2-core.h for more information how buffers should be returned
|
2014-08-04 13:33:53 +08:00
|
|
|
* to vb2 in stop_streaming().
|
|
|
|
*/
|
2014-02-25 00:51:03 +08:00
|
|
|
if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
|
|
|
|
for (i = 0; i < q->num_buffers; ++i)
|
|
|
|
if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE)
|
|
|
|
vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR);
|
|
|
|
/* Must be zero now */
|
|
|
|
WARN_ON(atomic_read(&q->owned_by_drv_count));
|
|
|
|
}
|
2011-08-29 19:51:49 +08:00
|
|
|
|
2014-04-21 07:55:41 +08:00
|
|
|
q->streaming = 0;
|
|
|
|
q->start_streaming_called = 0;
|
|
|
|
q->queued_count = 0;
|
2014-06-04 05:53:25 +08:00
|
|
|
q->error = 0;
|
2014-04-21 07:55:41 +08:00
|
|
|
|
2011-08-29 19:51:49 +08:00
|
|
|
/*
|
|
|
|
* Remove all buffers from videobuf's list...
|
|
|
|
*/
|
|
|
|
INIT_LIST_HEAD(&q->queued_list);
|
|
|
|
/*
|
|
|
|
* ...and done list; userspace will not receive any buffers it
|
|
|
|
* has not already dequeued before initiating cancel.
|
|
|
|
*/
|
|
|
|
INIT_LIST_HEAD(&q->done_list);
|
2014-02-06 16:46:11 +08:00
|
|
|
atomic_set(&q->owned_by_drv_count, 0);
|
2011-08-29 19:51:49 +08:00
|
|
|
wake_up_all(&q->done_wq);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reinitialize all buffers for next use.
|
2014-03-04 18:34:49 +08:00
|
|
|
* Make sure to call buf_finish for any queued buffers. Normally
|
|
|
|
* that's done in dqbuf, but that's not going to happen when we
|
|
|
|
* cancel the whole queue. Note: this code belongs here, not in
|
|
|
|
* __vb2_dqbuf() since in vb2_internal_dqbuf() there is a critical
|
|
|
|
* call to __fill_v4l2_buffer() after buf_finish(). That order can't
|
|
|
|
* be changed, so we can't move the buf_finish() to __vb2_dqbuf().
|
2011-08-29 19:51:49 +08:00
|
|
|
*/
|
2014-03-04 18:34:49 +08:00
|
|
|
for (i = 0; i < q->num_buffers; ++i) {
|
|
|
|
struct vb2_buffer *vb = q->bufs[i];
|
|
|
|
|
|
|
|
if (vb->state != VB2_BUF_STATE_DEQUEUED) {
|
|
|
|
vb->state = VB2_BUF_STATE_PREPARED;
|
2014-03-17 20:54:21 +08:00
|
|
|
call_void_vb_qop(vb, buf_finish, vb);
|
2014-03-04 18:34:49 +08:00
|
|
|
}
|
|
|
|
__vb2_dqbuf(vb);
|
|
|
|
}
|
2011-08-29 19:51:49 +08:00
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:49 +08:00
|
|
|
int vb2_core_streamon(struct vb2_queue *q, unsigned int type)
|
2010-10-11 21:56:41 +08:00
|
|
|
{
|
2011-03-21 06:26:41 +08:00
|
|
|
int ret;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
if (type != q->type) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "invalid stream type\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (q->streaming) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(3, "already streaming\n");
|
2013-11-08 18:08:45 +08:00
|
|
|
return 0;
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
|
|
|
|
2014-01-08 16:01:33 +08:00
|
|
|
if (!q->num_buffers) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "no buffers have been allocated\n");
|
2014-01-08 16:01:33 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-02-25 00:51:03 +08:00
|
|
|
if (q->num_buffers < q->min_buffers_needed) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "need at least %u allocated buffers\n",
|
2014-02-25 00:51:03 +08:00
|
|
|
q->min_buffers_needed);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2014-01-08 16:01:33 +08:00
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
/*
|
2014-02-25 00:51:03 +08:00
|
|
|
* Tell driver to start streaming provided sufficient buffers
|
|
|
|
* are available.
|
2010-10-11 21:56:41 +08:00
|
|
|
*/
|
2014-02-25 00:51:03 +08:00
|
|
|
if (q->queued_count >= q->min_buffers_needed) {
|
|
|
|
ret = vb2_start_streaming(q);
|
|
|
|
if (ret) {
|
|
|
|
__vb2_queue_cancel(q);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-03-21 06:26:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
q->streaming = 1;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(3, "successful\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-10-06 17:37:49 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vb2_core_streamon);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2014-06-04 05:53:25 +08:00
|
|
|
/**
|
|
|
|
* vb2_queue_error() - signal a fatal error on the queue
|
|
|
|
* @q: videobuf2 queue
|
|
|
|
*
|
|
|
|
* Flag that a fatal unrecoverable error has occurred and wake up all processes
|
|
|
|
* waiting on the queue. Polling will now set POLLERR and queuing and dequeuing
|
|
|
|
* buffers will return -EIO.
|
|
|
|
*
|
|
|
|
* The error flag will be cleared when cancelling the queue, either from
|
|
|
|
* vb2_streamoff or vb2_queue_release. Drivers should thus not call this
|
|
|
|
* function before starting the stream, otherwise the error flag will remain set
|
|
|
|
* until the queue is released when closing the device node.
|
|
|
|
*/
|
|
|
|
void vb2_queue_error(struct vb2_queue *q)
|
|
|
|
{
|
|
|
|
q->error = 1;
|
|
|
|
|
|
|
|
wake_up_all(&q->done_wq);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vb2_queue_error);
|
|
|
|
|
2015-10-06 17:37:49 +08:00
|
|
|
int vb2_core_streamoff(struct vb2_queue *q, unsigned int type)
|
2013-12-14 00:13:41 +08:00
|
|
|
{
|
2010-10-11 21:56:41 +08:00
|
|
|
if (type != q->type) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "invalid stream type\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cancel will pause streaming and remove all buffers from the driver
|
|
|
|
* and videobuf, effectively returning control over them to userspace.
|
2014-02-25 20:42:45 +08:00
|
|
|
*
|
|
|
|
* Note that we do this even if q->streaming == 0: if you prepare or
|
|
|
|
* queue buffers, and then call streamoff without ever having called
|
|
|
|
* streamon, you would still expect those buffers to be returned to
|
|
|
|
* their normal dequeued state.
|
2010-10-11 21:56:41 +08:00
|
|
|
*/
|
|
|
|
__vb2_queue_cancel(q);
|
2015-10-06 17:37:47 +08:00
|
|
|
q->waiting_for_buffers = !q->is_output;
|
2015-05-04 18:51:06 +08:00
|
|
|
q->last_buffer_dequeued = false;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(3, "successful\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-10-06 17:37:49 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vb2_core_streamoff);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* __find_plane_by_offset() - find plane associated with the given offset off
|
|
|
|
*/
|
|
|
|
static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off,
|
|
|
|
unsigned int *_buffer, unsigned int *_plane)
|
|
|
|
{
|
|
|
|
struct vb2_buffer *vb;
|
|
|
|
unsigned int buffer, plane;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Go over all buffers and their planes, comparing the given offset
|
|
|
|
* with an offset assigned to each plane. If a match is found,
|
|
|
|
* return its buffer and plane numbers.
|
|
|
|
*/
|
|
|
|
for (buffer = 0; buffer < q->num_buffers; ++buffer) {
|
|
|
|
vb = q->bufs[buffer];
|
|
|
|
|
|
|
|
for (plane = 0; plane < vb->num_planes; ++plane) {
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
if (vb->planes[plane].m.offset == off) {
|
2010-10-11 21:56:41 +08:00
|
|
|
*_buffer = buffer;
|
|
|
|
*_plane = plane;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2012-06-14 22:32:24 +08:00
|
|
|
/**
|
2015-10-06 17:37:48 +08:00
|
|
|
* vb2_core_expbuf() - Export a buffer as a file descriptor
|
2012-06-14 22:32:24 +08:00
|
|
|
* @q: videobuf2 queue
|
2015-10-06 17:37:48 +08:00
|
|
|
* @fd: file descriptor associated with DMABUF (set by driver) *
|
|
|
|
* @type: buffer type
|
|
|
|
* @index: id number of the buffer
|
|
|
|
* @plane: index of the plane to be exported, 0 for single plane queues
|
|
|
|
* @flags: flags for newly created file, currently only O_CLOEXEC is
|
|
|
|
* supported, refer to manual of open syscall for more details
|
2012-06-14 22:32:24 +08:00
|
|
|
*
|
|
|
|
* The return values from this function are intended to be directly returned
|
|
|
|
* from vidioc_expbuf handler in driver.
|
|
|
|
*/
|
2015-10-06 17:37:49 +08:00
|
|
|
int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type,
|
2015-10-06 17:37:48 +08:00
|
|
|
unsigned int index, unsigned int plane, unsigned int flags)
|
2012-06-14 22:32:24 +08:00
|
|
|
{
|
|
|
|
struct vb2_buffer *vb = NULL;
|
|
|
|
struct vb2_plane *vb_plane;
|
|
|
|
int ret;
|
|
|
|
struct dma_buf *dbuf;
|
|
|
|
|
2015-10-06 17:37:47 +08:00
|
|
|
if (q->memory != VB2_MEMORY_MMAP) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "queue is not currently set up for mmap\n");
|
2012-06-14 22:32:24 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!q->mem_ops->get_dmabuf) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "queue does not support DMA buffer exporting\n");
|
2012-06-14 22:32:24 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
if (flags & ~(O_CLOEXEC | O_ACCMODE)) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "queue does support only O_CLOEXEC and access mode flags\n");
|
2012-06-14 22:32:24 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
if (type != q->type) {
|
2014-04-07 20:08:47 +08:00
|
|
|
dprintk(1, "invalid buffer type\n");
|
2012-06-14 22:32:24 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
if (index >= q->num_buffers) {
|
2012-06-14 22:32:24 +08:00
|
|
|
dprintk(1, "buffer index out of range\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
vb = q->bufs[index];
|
2012-06-14 22:32:24 +08:00
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
if (plane >= vb->num_planes) {
|
2012-06-14 22:32:24 +08:00
|
|
|
dprintk(1, "buffer plane out of range\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-04-07 20:23:50 +08:00
|
|
|
if (vb2_fileio_is_active(q)) {
|
|
|
|
dprintk(1, "expbuf: file io in progress\n");
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
vb_plane = &vb->planes[plane];
|
2012-06-14 22:32:24 +08:00
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
dbuf = call_ptr_memop(vb, get_dmabuf, vb_plane->mem_priv,
|
|
|
|
flags & O_ACCMODE);
|
2012-06-14 22:32:24 +08:00
|
|
|
if (IS_ERR_OR_NULL(dbuf)) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "failed to export buffer %d, plane %d\n",
|
2015-10-06 17:37:48 +08:00
|
|
|
index, plane);
|
2012-06-14 22:32:24 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:37:48 +08:00
|
|
|
ret = dma_buf_fd(dbuf, flags & ~O_ACCMODE);
|
2012-06-14 22:32:24 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
|
2015-10-06 17:37:48 +08:00
|
|
|
index, plane, ret);
|
2012-06-14 22:32:24 +08:00
|
|
|
dma_buf_put(dbuf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
|
2015-10-06 17:37:48 +08:00
|
|
|
index, plane, ret);
|
|
|
|
*fd = ret;
|
2012-06-14 22:32:24 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-06 17:37:49 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vb2_core_expbuf);
|
2012-06-14 22:32:24 +08:00
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
/**
|
|
|
|
* vb2_mmap() - map video buffers into application address space
|
|
|
|
* @q: videobuf2 queue
|
|
|
|
* @vma: vma passed to the mmap file operation handler in the driver
|
|
|
|
*
|
|
|
|
* Should be called from mmap file operation handler of a driver.
|
|
|
|
* This function maps one plane of one of the available video buffers to
|
|
|
|
* userspace. To map whole video memory allocated on reqbufs, this function
|
|
|
|
* has to be called once per each plane per each buffer previously allocated.
|
|
|
|
*
|
|
|
|
* When the userspace application calls mmap, it passes to it an offset returned
|
|
|
|
* to it earlier by the means of vidioc_querybuf handler. That offset acts as
|
|
|
|
* a "cookie", which is then used to identify the plane to be mapped.
|
|
|
|
* This function finds a plane with a matching offset and a mapping is performed
|
|
|
|
* by the means of a provided memory operation.
|
|
|
|
*
|
|
|
|
* The return values from this function are intended to be directly returned
|
|
|
|
* from the mmap handler in driver.
|
|
|
|
*/
|
|
|
|
int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
|
|
|
|
struct vb2_buffer *vb;
|
[media] vb2: fix compiler warning
When compiling this for older kernels using the compatibility build
the compiler complains about uninitialized variables:
In file included from include/linux/kernel.h:20:0,
from include/linux/cache.h:4,
from include/linux/time.h:7,
from include/linux/input.h:13,
from /home/hans/work/build/media_build/v4l/compat.h:9,
from <command-line>:0:
/home/hans/work/build/media_build/v4l/videobuf2-core.c: In function 'vb2_mmap':
include/linux/dynamic_debug.h:60:9: warning: 'plane' may be used uninitialized in this function [-Wmaybe-uninitialized]
printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
^
/home/hans/work/build/media_build/v4l/videobuf2-core.c:2381:23: note: 'plane' was declared here
unsigned int buffer, plane;
^
In file included from include/linux/kernel.h:20:0,
from include/linux/cache.h:4,
from include/linux/time.h:7,
from include/linux/input.h:13,
from /home/hans/work/build/media_build/v4l/compat.h:9,
from <command-line>:0:
include/linux/dynamic_debug.h:60:9: warning: 'buffer' may be used uninitialized in this function [-Wmaybe-uninitialized]
printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
^
/home/hans/work/build/media_build/v4l/videobuf2-core.c:2381:15: note: 'buffer' was declared here
unsigned int buffer, plane;
^
While these warnings are bogus (the call to __find_plane_by_offset will
set buffer and plane), it doesn't hurt to initialize these variables.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2014-04-17 14:17:08 +08:00
|
|
|
unsigned int buffer = 0, plane = 0;
|
2010-10-11 21:56:41 +08:00
|
|
|
int ret;
|
2013-04-19 18:18:01 +08:00
|
|
|
unsigned long length;
|
2010-10-11 21:56:41 +08:00
|
|
|
|
2015-10-06 17:37:47 +08:00
|
|
|
if (q->memory != VB2_MEMORY_MMAP) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "queue is not currently set up for mmap\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check memory area access mode.
|
|
|
|
*/
|
|
|
|
if (!(vma->vm_flags & VM_SHARED)) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "invalid vma flags, VM_SHARED needed\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2015-10-06 17:37:47 +08:00
|
|
|
if (q->is_output) {
|
2010-10-11 21:56:41 +08:00
|
|
|
if (!(vma->vm_flags & VM_WRITE)) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "invalid vma flags, VM_WRITE needed\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!(vma->vm_flags & VM_READ)) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "invalid vma flags, VM_READ needed\n");
|
2010-10-11 21:56:41 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
2014-04-07 20:23:50 +08:00
|
|
|
if (vb2_fileio_is_active(q)) {
|
|
|
|
dprintk(1, "mmap: file io in progress\n");
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the plane corresponding to the offset passed by userspace.
|
|
|
|
*/
|
|
|
|
ret = __find_plane_by_offset(q, off, &buffer, &plane);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
vb = q->bufs[buffer];
|
|
|
|
|
2013-04-19 18:18:01 +08:00
|
|
|
/*
|
|
|
|
* MMAP requires page_aligned buffers.
|
|
|
|
* The buffer length was page_aligned at __vb2_buf_mem_alloc(),
|
|
|
|
* so, we need to do the same here.
|
|
|
|
*/
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
length = PAGE_ALIGN(vb->planes[plane].length);
|
2013-04-19 18:18:01 +08:00
|
|
|
if (length < (vma->vm_end - vma->vm_start)) {
|
|
|
|
dprintk(1,
|
|
|
|
"MMAP invalid, as it would overflow buffer length\n");
|
2013-04-12 10:57:57 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
[media] videobuf2: fix lockdep warning
The following lockdep warning has been there ever since commit a517cca6b24fc54ac209e44118ec8962051662e3
one year ago:
[ 403.117947] ======================================================
[ 403.117949] [ INFO: possible circular locking dependency detected ]
[ 403.117953] 3.16.0-rc6-test-media #961 Not tainted
[ 403.117954] -------------------------------------------------------
[ 403.117956] v4l2-ctl/15377 is trying to acquire lock:
[ 403.117959] (&dev->mutex#3){+.+.+.}, at: [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.117974]
[ 403.117974] but task is already holding lock:
[ 403.117976] (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.117987]
[ 403.117987] which lock already depends on the new lock.
[ 403.117987]
[ 403.117990]
[ 403.117990] the existing dependency chain (in reverse order) is:
[ 403.117992]
[ 403.117992] -> #1 (&mm->mmap_sem){++++++}:
[ 403.117997] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118006] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118010] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118014] [<ffffffff8118c9ec>] might_fault+0x7c/0xb0
[ 403.118018] [<ffffffffa0028a25>] video_usercopy+0x425/0x610 [videodev]
[ 403.118028] [<ffffffffa0028c25>] video_ioctl2+0x15/0x20 [videodev]
[ 403.118034] [<ffffffffa0022764>] v4l2_ioctl+0x184/0x1a0 [videodev]
[ 403.118040] [<ffffffff811d77d0>] do_vfs_ioctl+0x2f0/0x4f0
[ 403.118307] [<ffffffff811d7a51>] SyS_ioctl+0x81/0xa0
[ 403.118311] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118319]
[ 403.118319] -> #0 (&dev->mutex#3){+.+.+.}:
[ 403.118324] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118329] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118333] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118336] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118340] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118344] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118349] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118354] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118359] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118363] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118366] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118369] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118376] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118381]
[ 403.118381] other info that might help us debug this:
[ 403.118381]
[ 403.118383] Possible unsafe locking scenario:
[ 403.118383]
[ 403.118385] CPU0 CPU1
[ 403.118387] ---- ----
[ 403.118388] lock(&mm->mmap_sem);
[ 403.118391] lock(&dev->mutex#3);
[ 403.118394] lock(&mm->mmap_sem);
[ 403.118397] lock(&dev->mutex#3);
[ 403.118400]
[ 403.118400] *** DEADLOCK ***
[ 403.118400]
[ 403.118403] 1 lock held by v4l2-ctl/15377:
[ 403.118405] #0: (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.118411]
[ 403.118411] stack backtrace:
[ 403.118415] CPU: 0 PID: 15377 Comm: v4l2-ctl Not tainted 3.16.0-rc6-test-media #961
[ 403.118418] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/31/2013
[ 403.118420] ffffffff82a6c9d0 ffff8800af37fb00 ffffffff819916a2 ffffffff82a6c9d0
[ 403.118425] ffff8800af37fb40 ffffffff810d5715 ffff8802308e4200 0000000000000000
[ 403.118429] ffff8802308e4a48 ffff8802308e4a48 ffff8802308e4200 0000000000000001
[ 403.118433] Call Trace:
[ 403.118441] [<ffffffff819916a2>] dump_stack+0x4e/0x7a
[ 403.118445] [<ffffffff810d5715>] print_circular_bug+0x1d5/0x2a0
[ 403.118449] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118455] [<ffffffff8119c172>] ? find_vmap_area+0x42/0x70
[ 403.118459] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118463] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118468] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118472] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118476] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118480] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118484] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118488] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118493] [<ffffffff810d8055>] ? mark_held_locks+0x75/0xa0
[ 403.118497] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118502] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118506] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118510] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118513] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118517] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118521] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118525] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
The reason is that vb2_fop_mmap and vb2_fop_get_unmapped_area take the core lock
while they are called with the mmap_sem semaphore held. But elsewhere in the code
the core lock is taken first but calls to copy_to/from_user() can take the mmap_sem
semaphore as well, potentially causing a classical A-B/B-A deadlock.
However, the mmap/get_unmapped_area calls really shouldn't take the core lock
at all. So what would happen if they don't take the core lock anymore?
There are two situations that need to be taken into account: calling mmap while
new buffers are being added and calling mmap while buffers are being deleted.
The first case works almost fine without a lock: in all cases mmap relies on
correctly filled-in q->num_buffers/q->num_planes values and those are only
updated by reqbufs and create_buffers *after* any new buffers have been
initialized completely. Except in one case: if an error occurred while allocating
the buffers it will increase num_buffers and rely on __vb2_queue_free to
decrease it again. So there is a short period where the buffer information
may be wrong.
The second case definitely does pose a problem: buffers may be in the process
of being deleted, without the internal structure being updated.
In order to fix this a new mutex is added to vb2_queue that is taken when
buffers are allocated or deleted, and in vb2_mmap. That way vb2_mmap won't
get stale buffer data. Note that this is a problem only for MEMORY_MMAP, so
even though __qbuf_userptr and __qbuf_dmabuf also mess around with buffers
(mem_priv in particular), this doesn't clash with vb2_mmap or
vb2_get_unmapped_area since those are MMAP specific.
As an additional bonus the hack in __buf_prepare, the USERPTR case, can be
removed as well since mmap() no longer takes the core lock.
All in all a much cleaner solution.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2014-08-07 14:47:14 +08:00
|
|
|
mutex_lock(&q->mmap_lock);
|
2014-01-29 22:53:25 +08:00
|
|
|
ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma);
|
[media] videobuf2: fix lockdep warning
The following lockdep warning has been there ever since commit a517cca6b24fc54ac209e44118ec8962051662e3
one year ago:
[ 403.117947] ======================================================
[ 403.117949] [ INFO: possible circular locking dependency detected ]
[ 403.117953] 3.16.0-rc6-test-media #961 Not tainted
[ 403.117954] -------------------------------------------------------
[ 403.117956] v4l2-ctl/15377 is trying to acquire lock:
[ 403.117959] (&dev->mutex#3){+.+.+.}, at: [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.117974]
[ 403.117974] but task is already holding lock:
[ 403.117976] (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.117987]
[ 403.117987] which lock already depends on the new lock.
[ 403.117987]
[ 403.117990]
[ 403.117990] the existing dependency chain (in reverse order) is:
[ 403.117992]
[ 403.117992] -> #1 (&mm->mmap_sem){++++++}:
[ 403.117997] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118006] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118010] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118014] [<ffffffff8118c9ec>] might_fault+0x7c/0xb0
[ 403.118018] [<ffffffffa0028a25>] video_usercopy+0x425/0x610 [videodev]
[ 403.118028] [<ffffffffa0028c25>] video_ioctl2+0x15/0x20 [videodev]
[ 403.118034] [<ffffffffa0022764>] v4l2_ioctl+0x184/0x1a0 [videodev]
[ 403.118040] [<ffffffff811d77d0>] do_vfs_ioctl+0x2f0/0x4f0
[ 403.118307] [<ffffffff811d7a51>] SyS_ioctl+0x81/0xa0
[ 403.118311] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118319]
[ 403.118319] -> #0 (&dev->mutex#3){+.+.+.}:
[ 403.118324] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118329] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118333] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118336] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118340] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118344] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118349] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118354] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118359] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118363] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118366] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118369] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118376] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118381]
[ 403.118381] other info that might help us debug this:
[ 403.118381]
[ 403.118383] Possible unsafe locking scenario:
[ 403.118383]
[ 403.118385] CPU0 CPU1
[ 403.118387] ---- ----
[ 403.118388] lock(&mm->mmap_sem);
[ 403.118391] lock(&dev->mutex#3);
[ 403.118394] lock(&mm->mmap_sem);
[ 403.118397] lock(&dev->mutex#3);
[ 403.118400]
[ 403.118400] *** DEADLOCK ***
[ 403.118400]
[ 403.118403] 1 lock held by v4l2-ctl/15377:
[ 403.118405] #0: (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.118411]
[ 403.118411] stack backtrace:
[ 403.118415] CPU: 0 PID: 15377 Comm: v4l2-ctl Not tainted 3.16.0-rc6-test-media #961
[ 403.118418] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/31/2013
[ 403.118420] ffffffff82a6c9d0 ffff8800af37fb00 ffffffff819916a2 ffffffff82a6c9d0
[ 403.118425] ffff8800af37fb40 ffffffff810d5715 ffff8802308e4200 0000000000000000
[ 403.118429] ffff8802308e4a48 ffff8802308e4a48 ffff8802308e4200 0000000000000001
[ 403.118433] Call Trace:
[ 403.118441] [<ffffffff819916a2>] dump_stack+0x4e/0x7a
[ 403.118445] [<ffffffff810d5715>] print_circular_bug+0x1d5/0x2a0
[ 403.118449] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118455] [<ffffffff8119c172>] ? find_vmap_area+0x42/0x70
[ 403.118459] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118463] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118468] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118472] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118476] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118480] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118484] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118488] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118493] [<ffffffff810d8055>] ? mark_held_locks+0x75/0xa0
[ 403.118497] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118502] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118506] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118510] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118513] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118517] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118521] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118525] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
The reason is that vb2_fop_mmap and vb2_fop_get_unmapped_area take the core lock
while they are called with the mmap_sem semaphore held. But elsewhere in the code
the core lock is taken first but calls to copy_to/from_user() can take the mmap_sem
semaphore as well, potentially causing a classical A-B/B-A deadlock.
However, the mmap/get_unmapped_area calls really shouldn't take the core lock
at all. So what would happen if they don't take the core lock anymore?
There are two situations that need to be taken into account: calling mmap while
new buffers are being added and calling mmap while buffers are being deleted.
The first case works almost fine without a lock: in all cases mmap relies on
correctly filled-in q->num_buffers/q->num_planes values and those are only
updated by reqbufs and create_buffers *after* any new buffers have been
initialized completely. Except in one case: if an error occurred while allocating
the buffers it will increase num_buffers and rely on __vb2_queue_free to
decrease it again. So there is a short period where the buffer information
may be wrong.
The second case definitely does pose a problem: buffers may be in the process
of being deleted, without the internal structure being updated.
In order to fix this a new mutex is added to vb2_queue that is taken when
buffers are allocated or deleted, and in vb2_mmap. That way vb2_mmap won't
get stale buffer data. Note that this is a problem only for MEMORY_MMAP, so
even though __qbuf_userptr and __qbuf_dmabuf also mess around with buffers
(mem_priv in particular), this doesn't clash with vb2_mmap or
vb2_get_unmapped_area since those are MMAP specific.
As an additional bonus the hack in __buf_prepare, the USERPTR case, can be
removed as well since mmap() no longer takes the core lock.
All in all a much cleaner solution.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2014-08-07 14:47:14 +08:00
|
|
|
mutex_unlock(&q->mmap_lock);
|
2014-03-17 20:54:21 +08:00
|
|
|
if (ret)
|
2010-10-11 21:56:41 +08:00
|
|
|
return ret;
|
|
|
|
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(3, "buffer %d, plane %d successfully mapped\n", buffer, plane);
|
2010-10-11 21:56:41 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vb2_mmap);
|
|
|
|
|
2011-09-21 20:25:23 +08:00
|
|
|
#ifndef CONFIG_MMU
|
|
|
|
unsigned long vb2_get_unmapped_area(struct vb2_queue *q,
|
|
|
|
unsigned long addr,
|
|
|
|
unsigned long len,
|
|
|
|
unsigned long pgoff,
|
|
|
|
unsigned long flags)
|
|
|
|
{
|
|
|
|
unsigned long off = pgoff << PAGE_SHIFT;
|
|
|
|
struct vb2_buffer *vb;
|
|
|
|
unsigned int buffer, plane;
|
[media] videobuf2: fix lockdep warning
The following lockdep warning has been there ever since commit a517cca6b24fc54ac209e44118ec8962051662e3
one year ago:
[ 403.117947] ======================================================
[ 403.117949] [ INFO: possible circular locking dependency detected ]
[ 403.117953] 3.16.0-rc6-test-media #961 Not tainted
[ 403.117954] -------------------------------------------------------
[ 403.117956] v4l2-ctl/15377 is trying to acquire lock:
[ 403.117959] (&dev->mutex#3){+.+.+.}, at: [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.117974]
[ 403.117974] but task is already holding lock:
[ 403.117976] (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.117987]
[ 403.117987] which lock already depends on the new lock.
[ 403.117987]
[ 403.117990]
[ 403.117990] the existing dependency chain (in reverse order) is:
[ 403.117992]
[ 403.117992] -> #1 (&mm->mmap_sem){++++++}:
[ 403.117997] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118006] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118010] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118014] [<ffffffff8118c9ec>] might_fault+0x7c/0xb0
[ 403.118018] [<ffffffffa0028a25>] video_usercopy+0x425/0x610 [videodev]
[ 403.118028] [<ffffffffa0028c25>] video_ioctl2+0x15/0x20 [videodev]
[ 403.118034] [<ffffffffa0022764>] v4l2_ioctl+0x184/0x1a0 [videodev]
[ 403.118040] [<ffffffff811d77d0>] do_vfs_ioctl+0x2f0/0x4f0
[ 403.118307] [<ffffffff811d7a51>] SyS_ioctl+0x81/0xa0
[ 403.118311] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118319]
[ 403.118319] -> #0 (&dev->mutex#3){+.+.+.}:
[ 403.118324] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118329] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118333] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118336] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118340] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118344] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118349] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118354] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118359] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118363] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118366] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118369] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118376] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118381]
[ 403.118381] other info that might help us debug this:
[ 403.118381]
[ 403.118383] Possible unsafe locking scenario:
[ 403.118383]
[ 403.118385] CPU0 CPU1
[ 403.118387] ---- ----
[ 403.118388] lock(&mm->mmap_sem);
[ 403.118391] lock(&dev->mutex#3);
[ 403.118394] lock(&mm->mmap_sem);
[ 403.118397] lock(&dev->mutex#3);
[ 403.118400]
[ 403.118400] *** DEADLOCK ***
[ 403.118400]
[ 403.118403] 1 lock held by v4l2-ctl/15377:
[ 403.118405] #0: (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.118411]
[ 403.118411] stack backtrace:
[ 403.118415] CPU: 0 PID: 15377 Comm: v4l2-ctl Not tainted 3.16.0-rc6-test-media #961
[ 403.118418] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/31/2013
[ 403.118420] ffffffff82a6c9d0 ffff8800af37fb00 ffffffff819916a2 ffffffff82a6c9d0
[ 403.118425] ffff8800af37fb40 ffffffff810d5715 ffff8802308e4200 0000000000000000
[ 403.118429] ffff8802308e4a48 ffff8802308e4a48 ffff8802308e4200 0000000000000001
[ 403.118433] Call Trace:
[ 403.118441] [<ffffffff819916a2>] dump_stack+0x4e/0x7a
[ 403.118445] [<ffffffff810d5715>] print_circular_bug+0x1d5/0x2a0
[ 403.118449] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118455] [<ffffffff8119c172>] ? find_vmap_area+0x42/0x70
[ 403.118459] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118463] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118468] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118472] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118476] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118480] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118484] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118488] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118493] [<ffffffff810d8055>] ? mark_held_locks+0x75/0xa0
[ 403.118497] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118502] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118506] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118510] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118513] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118517] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118521] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118525] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
The reason is that vb2_fop_mmap and vb2_fop_get_unmapped_area take the core lock
while they are called with the mmap_sem semaphore held. But elsewhere in the code
the core lock is taken first but calls to copy_to/from_user() can take the mmap_sem
semaphore as well, potentially causing a classical A-B/B-A deadlock.
However, the mmap/get_unmapped_area calls really shouldn't take the core lock
at all. So what would happen if they don't take the core lock anymore?
There are two situations that need to be taken into account: calling mmap while
new buffers are being added and calling mmap while buffers are being deleted.
The first case works almost fine without a lock: in all cases mmap relies on
correctly filled-in q->num_buffers/q->num_planes values and those are only
updated by reqbufs and create_buffers *after* any new buffers have been
initialized completely. Except in one case: if an error occurred while allocating
the buffers it will increase num_buffers and rely on __vb2_queue_free to
decrease it again. So there is a short period where the buffer information
may be wrong.
The second case definitely does pose a problem: buffers may be in the process
of being deleted, without the internal structure being updated.
In order to fix this a new mutex is added to vb2_queue that is taken when
buffers are allocated or deleted, and in vb2_mmap. That way vb2_mmap won't
get stale buffer data. Note that this is a problem only for MEMORY_MMAP, so
even though __qbuf_userptr and __qbuf_dmabuf also mess around with buffers
(mem_priv in particular), this doesn't clash with vb2_mmap or
vb2_get_unmapped_area since those are MMAP specific.
As an additional bonus the hack in __buf_prepare, the USERPTR case, can be
removed as well since mmap() no longer takes the core lock.
All in all a much cleaner solution.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2014-08-07 14:47:14 +08:00
|
|
|
void *vaddr;
|
2011-09-21 20:25:23 +08:00
|
|
|
int ret;
|
|
|
|
|
2015-10-06 17:37:47 +08:00
|
|
|
if (q->memory != VB2_MEMORY_MMAP) {
|
2014-04-07 20:13:22 +08:00
|
|
|
dprintk(1, "queue is not currently set up for mmap\n");
|
2011-09-21 20:25:23 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the plane corresponding to the offset passed by userspace.
|
|
|
|
*/
|
|
|
|
ret = __find_plane_by_offset(q, off, &buffer, &plane);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
vb = q->bufs[buffer];
|
|
|
|
|
[media] videobuf2: fix lockdep warning
The following lockdep warning has been there ever since commit a517cca6b24fc54ac209e44118ec8962051662e3
one year ago:
[ 403.117947] ======================================================
[ 403.117949] [ INFO: possible circular locking dependency detected ]
[ 403.117953] 3.16.0-rc6-test-media #961 Not tainted
[ 403.117954] -------------------------------------------------------
[ 403.117956] v4l2-ctl/15377 is trying to acquire lock:
[ 403.117959] (&dev->mutex#3){+.+.+.}, at: [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.117974]
[ 403.117974] but task is already holding lock:
[ 403.117976] (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.117987]
[ 403.117987] which lock already depends on the new lock.
[ 403.117987]
[ 403.117990]
[ 403.117990] the existing dependency chain (in reverse order) is:
[ 403.117992]
[ 403.117992] -> #1 (&mm->mmap_sem){++++++}:
[ 403.117997] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118006] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118010] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118014] [<ffffffff8118c9ec>] might_fault+0x7c/0xb0
[ 403.118018] [<ffffffffa0028a25>] video_usercopy+0x425/0x610 [videodev]
[ 403.118028] [<ffffffffa0028c25>] video_ioctl2+0x15/0x20 [videodev]
[ 403.118034] [<ffffffffa0022764>] v4l2_ioctl+0x184/0x1a0 [videodev]
[ 403.118040] [<ffffffff811d77d0>] do_vfs_ioctl+0x2f0/0x4f0
[ 403.118307] [<ffffffff811d7a51>] SyS_ioctl+0x81/0xa0
[ 403.118311] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118319]
[ 403.118319] -> #0 (&dev->mutex#3){+.+.+.}:
[ 403.118324] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118329] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118333] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118336] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118340] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118344] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118349] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118354] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118359] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118363] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118366] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118369] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118376] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118381]
[ 403.118381] other info that might help us debug this:
[ 403.118381]
[ 403.118383] Possible unsafe locking scenario:
[ 403.118383]
[ 403.118385] CPU0 CPU1
[ 403.118387] ---- ----
[ 403.118388] lock(&mm->mmap_sem);
[ 403.118391] lock(&dev->mutex#3);
[ 403.118394] lock(&mm->mmap_sem);
[ 403.118397] lock(&dev->mutex#3);
[ 403.118400]
[ 403.118400] *** DEADLOCK ***
[ 403.118400]
[ 403.118403] 1 lock held by v4l2-ctl/15377:
[ 403.118405] #0: (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.118411]
[ 403.118411] stack backtrace:
[ 403.118415] CPU: 0 PID: 15377 Comm: v4l2-ctl Not tainted 3.16.0-rc6-test-media #961
[ 403.118418] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/31/2013
[ 403.118420] ffffffff82a6c9d0 ffff8800af37fb00 ffffffff819916a2 ffffffff82a6c9d0
[ 403.118425] ffff8800af37fb40 ffffffff810d5715 ffff8802308e4200 0000000000000000
[ 403.118429] ffff8802308e4a48 ffff8802308e4a48 ffff8802308e4200 0000000000000001
[ 403.118433] Call Trace:
[ 403.118441] [<ffffffff819916a2>] dump_stack+0x4e/0x7a
[ 403.118445] [<ffffffff810d5715>] print_circular_bug+0x1d5/0x2a0
[ 403.118449] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118455] [<ffffffff8119c172>] ? find_vmap_area+0x42/0x70
[ 403.118459] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118463] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118468] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118472] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118476] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118480] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118484] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118488] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118493] [<ffffffff810d8055>] ? mark_held_locks+0x75/0xa0
[ 403.118497] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118502] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118506] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118510] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118513] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118517] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118521] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118525] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
The reason is that vb2_fop_mmap and vb2_fop_get_unmapped_area take the core lock
while they are called with the mmap_sem semaphore held. But elsewhere in the code
the core lock is taken first but calls to copy_to/from_user() can take the mmap_sem
semaphore as well, potentially causing a classical A-B/B-A deadlock.
However, the mmap/get_unmapped_area calls really shouldn't take the core lock
at all. So what would happen if they don't take the core lock anymore?
There are two situations that need to be taken into account: calling mmap while
new buffers are being added and calling mmap while buffers are being deleted.
The first case works almost fine without a lock: in all cases mmap relies on
correctly filled-in q->num_buffers/q->num_planes values and those are only
updated by reqbufs and create_buffers *after* any new buffers have been
initialized completely. Except in one case: if an error occurred while allocating
the buffers it will increase num_buffers and rely on __vb2_queue_free to
decrease it again. So there is a short period where the buffer information
may be wrong.
The second case definitely does pose a problem: buffers may be in the process
of being deleted, without the internal structure being updated.
In order to fix this a new mutex is added to vb2_queue that is taken when
buffers are allocated or deleted, and in vb2_mmap. That way vb2_mmap won't
get stale buffer data. Note that this is a problem only for MEMORY_MMAP, so
even though __qbuf_userptr and __qbuf_dmabuf also mess around with buffers
(mem_priv in particular), this doesn't clash with vb2_mmap or
vb2_get_unmapped_area since those are MMAP specific.
As an additional bonus the hack in __buf_prepare, the USERPTR case, can be
removed as well since mmap() no longer takes the core lock.
All in all a much cleaner solution.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2014-08-07 14:47:14 +08:00
|
|
|
vaddr = vb2_plane_vaddr(vb, plane);
|
|
|
|
return vaddr ? (unsigned long)vaddr : -EINVAL;
|
2011-09-21 20:25:23 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
|
|
|
|
#endif
|
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
/**
|
2015-10-06 17:37:48 +08:00
|
|
|
* vb2_core_queue_init() - initialize a videobuf2 queue
|
2010-10-11 21:56:41 +08:00
|
|
|
* @q: videobuf2 queue; this structure should be allocated in driver
|
|
|
|
*
|
|
|
|
* The vb2_queue structure should be allocated by the driver. The driver is
|
|
|
|
* responsible of clearing it's content and setting initial values for some
|
|
|
|
* required entries before calling this function.
|
|
|
|
* q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
|
[media] media: videobuf2: Restructure vb2_buffer
Remove v4l2 stuff - v4l2_buf, v4l2_plane - from struct vb2_buffer.
Add new member variables - bytesused, length, offset, userptr, fd,
data_offset - to struct vb2_plane in order to cover all information
of v4l2_plane.
struct vb2_plane {
<snip>
unsigned int bytesused;
unsigned int length;
union {
unsigned int offset;
unsigned long userptr;
int fd;
} m;
unsigned int data_offset;
}
Replace v4l2_buf with new member variables - index, type, memory - which
are common fields for buffer management.
struct vb2_buffer {
<snip>
unsigned int index;
unsigned int type;
unsigned int memory;
unsigned int num_planes;
struct vb2_plane planes[VIDEO_MAX_PLANES];
<snip>
};
v4l2 specific fields - flags, field, timestamp, timecode,
sequence - are moved to vb2_v4l2_buffer in videobuf2-v4l2.c
struct vb2_v4l2_buffer {
struct vb2_buffer vb2_buf;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
};
Signed-off-by: Junghak Sung <jh1009.sung@samsung.com>
Signed-off-by: Geunyoung Kim <nenggun.kim@samsung.com>
Acked-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Acked-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
2015-09-22 21:30:30 +08:00
|
|
|
* to the struct vb2_queue description in include/media/videobuf2-core.h
|
2010-10-11 21:56:41 +08:00
|
|
|
* for more information.
|
|
|
|
*/
|
2015-10-06 17:37:49 +08:00
|
|
|
int vb2_core_queue_init(struct vb2_queue *q)
|
2010-10-11 21:56:41 +08:00
|
|
|
{
|
2012-09-18 01:59:30 +08:00
|
|
|
/*
|
|
|
|
* Sanity check
|
|
|
|
*/
|
|
|
|
if (WARN_ON(!q) ||
|
|
|
|
WARN_ON(!q->ops) ||
|
|
|
|
WARN_ON(!q->mem_ops) ||
|
|
|
|
WARN_ON(!q->type) ||
|
|
|
|
WARN_ON(!q->io_modes) ||
|
|
|
|
WARN_ON(!q->ops->queue_setup) ||
|
2015-10-06 17:37:48 +08:00
|
|
|
WARN_ON(!q->ops->buf_queue))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&q->queued_list);
|
|
|
|
INIT_LIST_HEAD(&q->done_list);
|
|
|
|
spin_lock_init(&q->done_lock);
|
|
|
|
mutex_init(&q->mmap_lock);
|
|
|
|
init_waitqueue_head(&q->done_wq);
|
|
|
|
|
|
|
|
if (q->buf_struct_size == 0)
|
|
|
|
q->buf_struct_size = sizeof(struct vb2_buffer);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-06 17:37:49 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vb2_core_queue_init);
|
2010-10-11 21:56:41 +08:00
|
|
|
|
|
|
|
/**
|
2015-10-06 17:37:48 +08:00
|
|
|
* vb2_core_queue_release() - stop streaming, release the queue and free memory
|
2010-10-11 21:56:41 +08:00
|
|
|
* @q: videobuf2 queue
|
|
|
|
*
|
|
|
|
* This function stops streaming and performs necessary clean ups, including
|
|
|
|
* freeing video buffer memory. The driver is responsible for freeing
|
|
|
|
* the vb2_queue structure itself.
|
|
|
|
*/
|
2015-10-06 17:37:49 +08:00
|
|
|
void vb2_core_queue_release(struct vb2_queue *q)
|
2010-10-11 21:56:41 +08:00
|
|
|
{
|
|
|
|
__vb2_queue_cancel(q);
|
[media] videobuf2: fix lockdep warning
The following lockdep warning has been there ever since commit a517cca6b24fc54ac209e44118ec8962051662e3
one year ago:
[ 403.117947] ======================================================
[ 403.117949] [ INFO: possible circular locking dependency detected ]
[ 403.117953] 3.16.0-rc6-test-media #961 Not tainted
[ 403.117954] -------------------------------------------------------
[ 403.117956] v4l2-ctl/15377 is trying to acquire lock:
[ 403.117959] (&dev->mutex#3){+.+.+.}, at: [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.117974]
[ 403.117974] but task is already holding lock:
[ 403.117976] (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.117987]
[ 403.117987] which lock already depends on the new lock.
[ 403.117987]
[ 403.117990]
[ 403.117990] the existing dependency chain (in reverse order) is:
[ 403.117992]
[ 403.117992] -> #1 (&mm->mmap_sem){++++++}:
[ 403.117997] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118006] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118010] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118014] [<ffffffff8118c9ec>] might_fault+0x7c/0xb0
[ 403.118018] [<ffffffffa0028a25>] video_usercopy+0x425/0x610 [videodev]
[ 403.118028] [<ffffffffa0028c25>] video_ioctl2+0x15/0x20 [videodev]
[ 403.118034] [<ffffffffa0022764>] v4l2_ioctl+0x184/0x1a0 [videodev]
[ 403.118040] [<ffffffff811d77d0>] do_vfs_ioctl+0x2f0/0x4f0
[ 403.118307] [<ffffffff811d7a51>] SyS_ioctl+0x81/0xa0
[ 403.118311] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118319]
[ 403.118319] -> #0 (&dev->mutex#3){+.+.+.}:
[ 403.118324] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118329] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118333] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118336] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118340] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118344] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118349] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118354] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118359] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118363] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118366] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118369] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118376] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118381]
[ 403.118381] other info that might help us debug this:
[ 403.118381]
[ 403.118383] Possible unsafe locking scenario:
[ 403.118383]
[ 403.118385] CPU0 CPU1
[ 403.118387] ---- ----
[ 403.118388] lock(&mm->mmap_sem);
[ 403.118391] lock(&dev->mutex#3);
[ 403.118394] lock(&mm->mmap_sem);
[ 403.118397] lock(&dev->mutex#3);
[ 403.118400]
[ 403.118400] *** DEADLOCK ***
[ 403.118400]
[ 403.118403] 1 lock held by v4l2-ctl/15377:
[ 403.118405] #0: (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.118411]
[ 403.118411] stack backtrace:
[ 403.118415] CPU: 0 PID: 15377 Comm: v4l2-ctl Not tainted 3.16.0-rc6-test-media #961
[ 403.118418] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/31/2013
[ 403.118420] ffffffff82a6c9d0 ffff8800af37fb00 ffffffff819916a2 ffffffff82a6c9d0
[ 403.118425] ffff8800af37fb40 ffffffff810d5715 ffff8802308e4200 0000000000000000
[ 403.118429] ffff8802308e4a48 ffff8802308e4a48 ffff8802308e4200 0000000000000001
[ 403.118433] Call Trace:
[ 403.118441] [<ffffffff819916a2>] dump_stack+0x4e/0x7a
[ 403.118445] [<ffffffff810d5715>] print_circular_bug+0x1d5/0x2a0
[ 403.118449] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118455] [<ffffffff8119c172>] ? find_vmap_area+0x42/0x70
[ 403.118459] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118463] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118468] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118472] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118476] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118480] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118484] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118488] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118493] [<ffffffff810d8055>] ? mark_held_locks+0x75/0xa0
[ 403.118497] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118502] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118506] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118510] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118513] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118517] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118521] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118525] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
The reason is that vb2_fop_mmap and vb2_fop_get_unmapped_area take the core lock
while they are called with the mmap_sem semaphore held. But elsewhere in the code
the core lock is taken first but calls to copy_to/from_user() can take the mmap_sem
semaphore as well, potentially causing a classical A-B/B-A deadlock.
However, the mmap/get_unmapped_area calls really shouldn't take the core lock
at all. So what would happen if they don't take the core lock anymore?
There are two situations that need to be taken into account: calling mmap while
new buffers are being added and calling mmap while buffers are being deleted.
The first case works almost fine without a lock: in all cases mmap relies on
correctly filled-in q->num_buffers/q->num_planes values and those are only
updated by reqbufs and create_buffers *after* any new buffers have been
initialized completely. Except in one case: if an error occurred while allocating
the buffers it will increase num_buffers and rely on __vb2_queue_free to
decrease it again. So there is a short period where the buffer information
may be wrong.
The second case definitely does pose a problem: buffers may be in the process
of being deleted, without the internal structure being updated.
In order to fix this a new mutex is added to vb2_queue that is taken when
buffers are allocated or deleted, and in vb2_mmap. That way vb2_mmap won't
get stale buffer data. Note that this is a problem only for MEMORY_MMAP, so
even though __qbuf_userptr and __qbuf_dmabuf also mess around with buffers
(mem_priv in particular), this doesn't clash with vb2_mmap or
vb2_get_unmapped_area since those are MMAP specific.
As an additional bonus the hack in __buf_prepare, the USERPTR case, can be
removed as well since mmap() no longer takes the core lock.
All in all a much cleaner solution.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2014-08-07 14:47:14 +08:00
|
|
|
mutex_lock(&q->mmap_lock);
|
2011-09-28 20:23:02 +08:00
|
|
|
__vb2_queue_free(q, q->num_buffers);
|
[media] videobuf2: fix lockdep warning
The following lockdep warning has been there ever since commit a517cca6b24fc54ac209e44118ec8962051662e3
one year ago:
[ 403.117947] ======================================================
[ 403.117949] [ INFO: possible circular locking dependency detected ]
[ 403.117953] 3.16.0-rc6-test-media #961 Not tainted
[ 403.117954] -------------------------------------------------------
[ 403.117956] v4l2-ctl/15377 is trying to acquire lock:
[ 403.117959] (&dev->mutex#3){+.+.+.}, at: [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.117974]
[ 403.117974] but task is already holding lock:
[ 403.117976] (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.117987]
[ 403.117987] which lock already depends on the new lock.
[ 403.117987]
[ 403.117990]
[ 403.117990] the existing dependency chain (in reverse order) is:
[ 403.117992]
[ 403.117992] -> #1 (&mm->mmap_sem){++++++}:
[ 403.117997] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118006] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118010] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118014] [<ffffffff8118c9ec>] might_fault+0x7c/0xb0
[ 403.118018] [<ffffffffa0028a25>] video_usercopy+0x425/0x610 [videodev]
[ 403.118028] [<ffffffffa0028c25>] video_ioctl2+0x15/0x20 [videodev]
[ 403.118034] [<ffffffffa0022764>] v4l2_ioctl+0x184/0x1a0 [videodev]
[ 403.118040] [<ffffffff811d77d0>] do_vfs_ioctl+0x2f0/0x4f0
[ 403.118307] [<ffffffff811d7a51>] SyS_ioctl+0x81/0xa0
[ 403.118311] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118319]
[ 403.118319] -> #0 (&dev->mutex#3){+.+.+.}:
[ 403.118324] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118329] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118333] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118336] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118340] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118344] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118349] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118354] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118359] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118363] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118366] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118369] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118376] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
[ 403.118381]
[ 403.118381] other info that might help us debug this:
[ 403.118381]
[ 403.118383] Possible unsafe locking scenario:
[ 403.118383]
[ 403.118385] CPU0 CPU1
[ 403.118387] ---- ----
[ 403.118388] lock(&mm->mmap_sem);
[ 403.118391] lock(&dev->mutex#3);
[ 403.118394] lock(&mm->mmap_sem);
[ 403.118397] lock(&dev->mutex#3);
[ 403.118400]
[ 403.118400] *** DEADLOCK ***
[ 403.118400]
[ 403.118403] 1 lock held by v4l2-ctl/15377:
[ 403.118405] #0: (&mm->mmap_sem){++++++}, at: [<ffffffff8118291f>] vm_mmap_pgoff+0x6f/0xc0
[ 403.118411]
[ 403.118411] stack backtrace:
[ 403.118415] CPU: 0 PID: 15377 Comm: v4l2-ctl Not tainted 3.16.0-rc6-test-media #961
[ 403.118418] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/31/2013
[ 403.118420] ffffffff82a6c9d0 ffff8800af37fb00 ffffffff819916a2 ffffffff82a6c9d0
[ 403.118425] ffff8800af37fb40 ffffffff810d5715 ffff8802308e4200 0000000000000000
[ 403.118429] ffff8802308e4a48 ffff8802308e4a48 ffff8802308e4200 0000000000000001
[ 403.118433] Call Trace:
[ 403.118441] [<ffffffff819916a2>] dump_stack+0x4e/0x7a
[ 403.118445] [<ffffffff810d5715>] print_circular_bug+0x1d5/0x2a0
[ 403.118449] [<ffffffff810d6a96>] check_prevs_add+0x746/0x9f0
[ 403.118455] [<ffffffff8119c172>] ? find_vmap_area+0x42/0x70
[ 403.118459] [<ffffffff810d733c>] validate_chain.isra.39+0x5fc/0x9a0
[ 403.118463] [<ffffffff810d8bc3>] __lock_acquire+0x4d3/0xd30
[ 403.118468] [<ffffffff810d9da7>] lock_acquire+0xa7/0x160
[ 403.118472] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118476] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118480] [<ffffffff81999664>] mutex_lock_interruptible_nested+0x64/0x640
[ 403.118484] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118488] [<ffffffffa005a6c3>] ? vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118493] [<ffffffff810d8055>] ? mark_held_locks+0x75/0xa0
[ 403.118497] [<ffffffffa005a6c3>] vb2_fop_mmap+0x33/0x90 [videobuf2_core]
[ 403.118502] [<ffffffffa0022122>] v4l2_mmap+0x62/0xa0 [videodev]
[ 403.118506] [<ffffffff81197270>] mmap_region+0x3d0/0x5d0
[ 403.118510] [<ffffffff8119778d>] do_mmap_pgoff+0x31d/0x400
[ 403.118513] [<ffffffff81182940>] vm_mmap_pgoff+0x90/0xc0
[ 403.118517] [<ffffffff81195cef>] SyS_mmap_pgoff+0x1df/0x2a0
[ 403.118521] [<ffffffff810085c2>] SyS_mmap+0x22/0x30
[ 403.118525] [<ffffffff8199dc69>] system_call_fastpath+0x16/0x1b
The reason is that vb2_fop_mmap and vb2_fop_get_unmapped_area take the core lock
while they are called with the mmap_sem semaphore held. But elsewhere in the code
the core lock is taken first but calls to copy_to/from_user() can take the mmap_sem
semaphore as well, potentially causing a classical A-B/B-A deadlock.
However, the mmap/get_unmapped_area calls really shouldn't take the core lock
at all. So what would happen if they don't take the core lock anymore?
There are two situations that need to be taken into account: calling mmap while
new buffers are being added and calling mmap while buffers are being deleted.
The first case works almost fine without a lock: in all cases mmap relies on
correctly filled-in q->num_buffers/q->num_planes values and those are only
updated by reqbufs and create_buffers *after* any new buffers have been
initialized completely. Except in one case: if an error occurred while allocating
the buffers it will increase num_buffers and rely on __vb2_queue_free to
decrease it again. So there is a short period where the buffer information
may be wrong.
The second case definitely does pose a problem: buffers may be in the process
of being deleted, without the internal structure being updated.
In order to fix this a new mutex is added to vb2_queue that is taken when
buffers are allocated or deleted, and in vb2_mmap. That way vb2_mmap won't
get stale buffer data. Note that this is a problem only for MEMORY_MMAP, so
even though __qbuf_userptr and __qbuf_dmabuf also mess around with buffers
(mem_priv in particular), this doesn't clash with vb2_mmap or
vb2_get_unmapped_area since those are MMAP specific.
As an additional bonus the hack in __buf_prepare, the USERPTR case, can be
removed as well since mmap() no longer takes the core lock.
All in all a much cleaner solution.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2014-08-07 14:47:14 +08:00
|
|
|
mutex_unlock(&q->mmap_lock);
|
2010-10-11 21:56:41 +08:00
|
|
|
}
|
2015-10-06 17:37:49 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vb2_core_queue_release);
|
2012-07-02 16:59:18 +08:00
|
|
|
|
2010-10-11 21:56:41 +08:00
|
|
|
MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
|
2011-03-14 02:23:32 +08:00
|
|
|
MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
|
2010-10-11 21:56:41 +08:00
|
|
|
MODULE_LICENSE("GPL");
|