755ca344409bbc001155a6cf09c4b62040ac47fe
[openwrt/staging/blogic.git] /
1 From 16cf378051d7fff6772a7acaecbacddec7822330 Mon Sep 17 00:00:00 2001
2 From: John Sheu <sheu@chromium.org>
3 Date: Thu, 15 Nov 2018 10:57:16 -0500
4 Subject: [PATCH] media: vb2: Allow reqbufs(0) with "in use" MMAP
5 buffers
6
7 Upstream commit d644cca50f366cd109845ae92e37c09ed79adf81
8
9 Videobuf2 presently does not allow VIDIOC_REQBUFS to destroy outstanding
10 buffers if the queue is of type V4L2_MEMORY_MMAP, and if the buffers are
11 considered "in use". This is different behavior than for other memory
12 types and prevents us from deallocating buffers in following two cases:
13
14 1) There are outstanding mmap()ed views on the buffer. However even if
15 we put the buffer in reqbufs(0), there will be remaining references,
16 due to vma .open/close() adjusting vb2 buffer refcount appropriately.
17 This means that the buffer will be in fact freed only when the last
18 mmap()ed view is unmapped.
19
20 2) Buffer has been exported as a DMABUF. Refcount of the vb2 buffer
21 is managed properly by VB2 DMABUF ops, i.e. incremented on DMABUF
22 get and decremented on DMABUF release. This means that the buffer
23 will be alive until all importers release it.
24
25 Considering both cases above, there does not seem to be any need to
26 prevent reqbufs(0) operation, because buffer lifetime is already
27 properly managed by both mmap() and DMABUF code paths. Let's remove it
28 and allow userspace freeing the queue (and potentially allocating a new
29 one) even though old buffers might be still in processing.
30
31 To let userspace know that the kernel now supports orphaning buffers
32 that are still in use, add a new V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS
33 to be set by reqbufs and create_bufs.
34
35 [p.zabel@pengutronix.de: added V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS,
36 updated documentation, and added back debug message]
37
38 Signed-off-by: John Sheu <sheu@chromium.org>
39 Reviewed-by: Pawel Osciak <posciak@chromium.org>
40 Signed-off-by: Tomasz Figa <tfiga@chromium.org>
41 Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
42 Acked-by: Sakari Ailus <sakari.ailus@linux.intel.com>
43 Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
44 [hverkuil-cisco@xs4all.nl: added V4L2-BUF-CAP-SUPPORTS-ORPHANED-BUFS ref]
45 Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
46 ---
47 Documentation/media/uapi/v4l/vidioc-reqbufs.rst | 17 ++++++++++++++---
48 drivers/media/common/videobuf2/videobuf2-core.c | 8 +++-----
49 drivers/media/common/videobuf2/videobuf2-v4l2.c | 2 +-
50 include/uapi/linux/videodev2.h | 1 +
51 4 files changed, 19 insertions(+), 9 deletions(-)
52
53 --- a/Documentation/media/uapi/v4l/vidioc-reqbufs.rst
54 +++ b/Documentation/media/uapi/v4l/vidioc-reqbufs.rst
55 @@ -59,9 +59,14 @@ When the I/O method is not supported the
56 code.
57
58 Applications can call :ref:`VIDIOC_REQBUFS` again to change the number of
59 -buffers, however this cannot succeed when any buffers are still mapped.
60 -A ``count`` value of zero frees all buffers, after aborting or finishing
61 -any DMA in progress, an implicit
62 +buffers. Note that if any buffers are still mapped or exported via DMABUF,
63 +then :ref:`VIDIOC_REQBUFS` can only succeed if the
64 +``V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS`` capability is set. Otherwise
65 +:ref:`VIDIOC_REQBUFS` will return the ``EBUSY`` error code.
66 +If ``V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS`` is set, then these buffers are
67 +orphaned and will be freed when they are unmapped or when the exported DMABUF
68 +fds are closed. A ``count`` value of zero frees or orphans all buffers, after
69 +aborting or finishing any DMA in progress, an implicit
70 :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>`.
71
72
73 @@ -112,6 +117,7 @@ any DMA in progress, an implicit
74 .. _V4L2-BUF-CAP-SUPPORTS-USERPTR:
75 .. _V4L2-BUF-CAP-SUPPORTS-DMABUF:
76 .. _V4L2-BUF-CAP-SUPPORTS-REQUESTS:
77 +.. _V4L2-BUF-CAP-SUPPORTS-ORPHANED-BUFS:
78
79 .. cssclass:: longtable
80
81 @@ -132,6 +138,11 @@ any DMA in progress, an implicit
82 * - ``V4L2_BUF_CAP_SUPPORTS_REQUESTS``
83 - 0x00000008
84 - This buffer type supports :ref:`requests <media-request-api>`.
85 + * - ``V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS``
86 + - 0x00000010
87 + - The kernel allows calling :ref:`VIDIOC_REQBUFS` while buffers are still
88 + mapped or exported via DMABUF. These orphaned buffers will be freed
89 + when they are unmapped or when the exported DMABUF fds are closed.
90
91 Return Value
92 ============
93 --- a/drivers/media/common/videobuf2/videobuf2-core.c
94 +++ b/drivers/media/common/videobuf2/videobuf2-core.c
95 @@ -684,11 +684,9 @@ int vb2_core_reqbufs(struct vb2_queue *q
96 * are not in use and can be freed.
97 */
98 mutex_lock(&q->mmap_lock);
99 - if (q->memory == VB2_MEMORY_MMAP && __buffers_in_use(q)) {
100 - mutex_unlock(&q->mmap_lock);
101 - dprintk(1, "memory in use, cannot free\n");
102 - return -EBUSY;
103 - }
104 + if (debug && q->memory == VB2_MEMORY_MMAP &&
105 + __buffers_in_use(q))
106 + dprintk(1, "memory in use, orphaning buffers\n");
107
108 /*
109 * Call queue_cancel to clean up any buffers in the PREPARED or
110 --- a/drivers/media/common/videobuf2/videobuf2-v4l2.c
111 +++ b/drivers/media/common/videobuf2/videobuf2-v4l2.c
112 @@ -484,7 +484,7 @@ EXPORT_SYMBOL(vb2_querybuf);
113
114 static void fill_buf_caps(struct vb2_queue *q, u32 *caps)
115 {
116 - *caps = 0;
117 + *caps = V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS;
118 if (q->io_modes & VB2_MMAP)
119 *caps |= V4L2_BUF_CAP_SUPPORTS_MMAP;
120 if (q->io_modes & VB2_USERPTR)
121 --- a/include/uapi/linux/videodev2.h
122 +++ b/include/uapi/linux/videodev2.h
123 @@ -881,6 +881,7 @@ struct v4l2_requestbuffers {
124 #define V4L2_BUF_CAP_SUPPORTS_USERPTR (1 << 1)
125 #define V4L2_BUF_CAP_SUPPORTS_DMABUF (1 << 2)
126 #define V4L2_BUF_CAP_SUPPORTS_REQUESTS (1 << 3)
127 +#define V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS (1 << 4)
128
129 /**
130 * struct v4l2_plane - plane info for multi-planar buffers