Commit b3379c6201bb3555298cdbf0aa004af260f2a6a4

Authored by Hans Verkuil
Committed by Mauro Carvalho Chehab
1 parent a7afcaccfa

[media] vb2: only call start_streaming if sufficient buffers are queued

In commit 02f142ecd24aaf891324ffba8527284c1731b561 support was added to
start_streaming to return -ENOBUFS if insufficient buffers were queued
for the DMA engine to start. The vb2 core would attempt calling
start_streaming again if another buffer would be queued up.

Later analysis uncovered problems with the queue management if start_streaming
would return an error: the buffers are enqueued to the driver before the
start_streaming op is called, so after an error they are never returned to
the vb2 core. The solution for this is to let the driver return them to
the vb2 core in case of an error while starting the DMA engine. However,
in the case of -ENOBUFS that would be weird: it is not a real error, it
just says that more buffers are needed. Requiring start_streaming to give
them back only to have them requeued again the next time the application
calls QBUF is inefficient.

This patch changes this mechanism: it adds a 'min_buffers_needed' field
to vb2_queue that drivers can set with the minimum number of buffers
required to start the DMA engine. The start_streaming op is only called
if enough buffers are queued. The -ENOBUFS handling has been dropped in
favor of this new method.

Drivers are expected to return buffers back to vb2 core with state QUEUED
if start_streaming would return an error. The vb2 core checks for this
and produces a warning if that didn't happen and it will forcefully
reclaim such buffers to ensure that the internal vb2 core state remains
consistent and all buffer-related resources have been correctly freed
and all op calls have been balanced.

__reqbufs() has been updated to check that at least min_buffers_needed
buffers could be allocated. If fewer buffers were allocated then __reqbufs
will free what was allocated and return -ENOMEM. Based on a suggestion from
Pawel Osciak.

__create_bufs() doesn't do that check, since the use of __create_bufs
assumes some advance scenario where the user might want more control.
Instead streamon will check if enough buffers were allocated to prevent
streaming with fewer than the minimum required number of buffers.

Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>

Showing 7 changed files with 116 additions and 73 deletions Side-by-side Diff

drivers/media/platform/davinci/vpbe_display.c
... ... @@ -344,11 +344,6 @@
344 344 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
345 345 int ret;
346 346  
347   - /* If buffer queue is empty, return error */
348   - if (list_empty(&layer->dma_queue)) {
349   - v4l2_err(&vpbe_dev->v4l2_dev, "buffer queue is empty\n");
350   - return -ENOBUFS;
351   - }
352 347 /* Get the next frame from the buffer queue */
353 348 layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
354 349 struct vpbe_disp_buffer, list);
... ... @@ -1416,6 +1411,7 @@
1416 1411 q->mem_ops = &vb2_dma_contig_memops;
1417 1412 q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
1418 1413 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1414 + q->min_buffers_needed = 1;
1419 1415  
1420 1416 ret = vb2_queue_init(q);
1421 1417 if (ret) {
drivers/media/platform/davinci/vpif_capture.c
... ... @@ -272,13 +272,7 @@
272 272 unsigned long flags;
273 273 int ret;
274 274  
275   - /* If buffer queue is empty, return error */
276 275 spin_lock_irqsave(&common->irqlock, flags);
277   - if (list_empty(&common->dma_queue)) {
278   - spin_unlock_irqrestore(&common->irqlock, flags);
279   - vpif_dbg(1, debug, "buffer queue is empty\n");
280   - return -ENOBUFS;
281   - }
282 276  
283 277 /* Get the next frame from the buffer queue */
284 278 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
... ... @@ -1024,6 +1018,7 @@
1024 1018 q->mem_ops = &vb2_dma_contig_memops;
1025 1019 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1026 1020 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1021 + q->min_buffers_needed = 1;
1027 1022  
1028 1023 ret = vb2_queue_init(q);
1029 1024 if (ret) {
drivers/media/platform/davinci/vpif_display.c
... ... @@ -234,13 +234,7 @@
234 234 unsigned long flags;
235 235 int ret;
236 236  
237   - /* If buffer queue is empty, return error */
238 237 spin_lock_irqsave(&common->irqlock, flags);
239   - if (list_empty(&common->dma_queue)) {
240   - spin_unlock_irqrestore(&common->irqlock, flags);
241   - vpif_err("buffer queue is empty\n");
242   - return -ENOBUFS;
243   - }
244 238  
245 239 /* Get the next frame from the buffer queue */
246 240 common->next_frm = common->cur_frm =
... ... @@ -984,6 +978,7 @@
984 978 q->mem_ops = &vb2_dma_contig_memops;
985 979 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
986 980 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  981 + q->min_buffers_needed = 1;
987 982  
988 983 ret = vb2_queue_init(q);
989 984 if (ret) {
drivers/media/platform/s5p-tv/mixer_video.c
... ... @@ -946,11 +946,6 @@
946 946  
947 947 mxr_dbg(mdev, "%s\n", __func__);
948 948  
949   - if (count == 0) {
950   - mxr_dbg(mdev, "no output buffers queued\n");
951   - return -ENOBUFS;
952   - }
953   -
954 949 /* block any changes in output configuration */
955 950 mxr_output_get(mdev);
956 951  
... ... @@ -1124,6 +1119,7 @@
1124 1119 .drv_priv = layer,
1125 1120 .buf_struct_size = sizeof(struct mxr_buffer),
1126 1121 .ops = &mxr_video_qops,
  1122 + .min_buffers_needed = 1,
1127 1123 .mem_ops = &vb2_dma_contig_memops,
1128 1124 };
1129 1125  
drivers/media/v4l2-core/videobuf2-core.c
... ... @@ -818,6 +818,7 @@
818 818 * Make sure the requested values and current defaults are sane.
819 819 */
820 820 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
  821 + num_buffers = max_t(unsigned int, req->count, q->min_buffers_needed);
821 822 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
822 823 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
823 824 q->memory = req->memory;
824 825  
... ... @@ -841,9 +842,16 @@
841 842 }
842 843  
843 844 /*
  845 + * There is no point in continuing if we can't allocate the minimum
  846 + * number of buffers needed by this vb2_queue.
  847 + */
  848 + if (allocated_buffers < q->min_buffers_needed)
  849 + ret = -ENOMEM;
  850 +
  851 + /*
844 852 * Check if driver can handle the allocated number of buffers.
845 853 */
846   - if (allocated_buffers < num_buffers) {
  854 + if (!ret && allocated_buffers < num_buffers) {
847 855 num_buffers = allocated_buffers;
848 856  
849 857 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
850 858  
... ... @@ -1051,13 +1059,20 @@
1051 1059 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
1052 1060 * @vb: vb2_buffer returned from the driver
1053 1061 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
1054   - * or VB2_BUF_STATE_ERROR if the operation finished with an error
  1062 + * or VB2_BUF_STATE_ERROR if the operation finished with an error.
  1063 + * If start_streaming fails then it should return buffers with state
  1064 + * VB2_BUF_STATE_QUEUED to put them back into the queue.
1055 1065 *
1056 1066 * This function should be called by the driver after a hardware operation on
1057 1067 * a buffer is finished and the buffer may be returned to userspace. The driver
1058 1068 * cannot use this buffer anymore until it is queued back to it by videobuf
1059 1069 * by the means of buf_queue callback. Only buffers previously queued to the
1060 1070 * driver by buf_queue can be passed to this function.
  1071 + *
  1072 + * While streaming a buffer can only be returned in state DONE or ERROR.
  1073 + * The start_streaming op can also return them in case the DMA engine cannot
  1074 + * be started for some reason. In that case the buffers should be returned with
  1075 + * state QUEUED.
1061 1076 */
1062 1077 void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
1063 1078 {
1064 1079  
... ... @@ -1065,11 +1080,17 @@
1065 1080 unsigned long flags;
1066 1081 unsigned int plane;
1067 1082  
1068   - if (vb->state != VB2_BUF_STATE_ACTIVE)
  1083 + if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE))
1069 1084 return;
1070 1085  
1071   - if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR)
1072   - return;
  1086 + if (!q->start_streaming_called) {
  1087 + if (WARN_ON(state != VB2_BUF_STATE_QUEUED))
  1088 + state = VB2_BUF_STATE_QUEUED;
  1089 + } else if (!WARN_ON(!q->start_streaming_called)) {
  1090 + if (WARN_ON(state != VB2_BUF_STATE_DONE &&
  1091 + state != VB2_BUF_STATE_ERROR))
  1092 + state = VB2_BUF_STATE_ERROR;
  1093 + }
1073 1094  
1074 1095 #ifdef CONFIG_VIDEO_ADV_DEBUG
1075 1096 /*
1076 1097  
... ... @@ -1088,10 +1109,14 @@
1088 1109 /* Add the buffer to the done buffers list */
1089 1110 spin_lock_irqsave(&q->done_lock, flags);
1090 1111 vb->state = state;
1091   - list_add_tail(&vb->done_entry, &q->done_list);
  1112 + if (state != VB2_BUF_STATE_QUEUED)
  1113 + list_add_tail(&vb->done_entry, &q->done_list);
1092 1114 atomic_dec(&q->owned_by_drv_count);
1093 1115 spin_unlock_irqrestore(&q->done_lock, flags);
1094 1116  
  1117 + if (state == VB2_BUF_STATE_QUEUED)
  1118 + return;
  1119 +
1095 1120 /* Inform any processes that may be waiting for buffers */
1096 1121 wake_up(&q->done_wq);
1097 1122 }
1098 1123  
1099 1124  
1100 1125  
1101 1126  
1102 1127  
1103 1128  
... ... @@ -1588,34 +1613,49 @@
1588 1613 * vb2_start_streaming() - Attempt to start streaming.
1589 1614 * @q: videobuf2 queue
1590 1615 *
1591   - * If there are not enough buffers, then retry_start_streaming is set to
1592   - * 1 and 0 is returned. The next time a buffer is queued and
1593   - * retry_start_streaming is 1, this function will be called again to
1594   - * retry starting the DMA engine.
  1616 + * Attempt to start streaming. When this function is called there must be
  1617 + * at least q->min_buffers_needed buffers queued up (i.e. the minimum
  1618 + * number of buffers required for the DMA engine to function). If the
  1619 + * @start_streaming op fails it is supposed to return all the driver-owned
  1620 + * buffers back to vb2 in state QUEUED. Check if that happened and if
  1621 + * not warn and reclaim them forcefully.
1595 1622 */
1596 1623 static int vb2_start_streaming(struct vb2_queue *q)
1597 1624 {
  1625 + struct vb2_buffer *vb;
1598 1626 int ret;
1599 1627  
1600   - /* Tell the driver to start streaming */
1601   - ret = call_qop(q, start_streaming, q, atomic_read(&q->owned_by_drv_count));
1602   - if (ret)
1603   - fail_qop(q, start_streaming);
1604   -
1605 1628 /*
1606   - * If there are not enough buffers queued to start streaming, then
1607   - * the start_streaming operation will return -ENOBUFS and you have to
1608   - * retry when the next buffer is queued.
  1629 + * If any buffers were queued before streamon,
  1630 + * we can now pass them to driver for processing.
1609 1631 */
1610   - if (ret == -ENOBUFS) {
1611   - dprintk(1, "qbuf: not enough buffers, retry when more buffers are queued.\n");
1612   - q->retry_start_streaming = 1;
  1632 + list_for_each_entry(vb, &q->queued_list, queued_entry)
  1633 + __enqueue_in_driver(vb);
  1634 +
  1635 + /* Tell the driver to start streaming */
  1636 + ret = call_qop(q, start_streaming, q,
  1637 + atomic_read(&q->owned_by_drv_count));
  1638 + q->start_streaming_called = ret == 0;
  1639 + if (!ret)
1613 1640 return 0;
  1641 +
  1642 + fail_qop(q, start_streaming);
  1643 + dprintk(1, "qbuf: driver refused to start streaming\n");
  1644 + if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
  1645 + unsigned i;
  1646 +
  1647 + /*
  1648 + * Forcefully reclaim buffers if the driver did not
  1649 + * correctly return them to vb2.
  1650 + */
  1651 + for (i = 0; i < q->num_buffers; ++i) {
  1652 + vb = q->bufs[i];
  1653 + if (vb->state == VB2_BUF_STATE_ACTIVE)
  1654 + vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED);
  1655 + }
  1656 + /* Must be zero now */
  1657 + WARN_ON(atomic_read(&q->owned_by_drv_count));
1614 1658 }
1615   - if (ret)
1616   - dprintk(1, "qbuf: driver refused to start streaming\n");
1617   - else
1618   - q->retry_start_streaming = 0;
1619 1659 return ret;
1620 1660 }
1621 1661  
... ... @@ -1651,6 +1691,7 @@
1651 1691 * dequeued in dqbuf.
1652 1692 */
1653 1693 list_add_tail(&vb->queued_entry, &q->queued_list);
  1694 + q->queued_count++;
1654 1695 vb->state = VB2_BUF_STATE_QUEUED;
1655 1696 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1656 1697 /*
1657 1698  
... ... @@ -1669,13 +1710,20 @@
1669 1710 * If already streaming, give the buffer to driver for processing.
1670 1711 * If not, the buffer will be given to driver on next streamon.
1671 1712 */
1672   - if (q->streaming)
  1713 + if (q->start_streaming_called)
1673 1714 __enqueue_in_driver(vb);
1674 1715  
1675 1716 /* Fill buffer information for the userspace */
1676 1717 __fill_v4l2_buffer(vb, b);
1677 1718  
1678   - if (q->retry_start_streaming) {
  1719 + /*
  1720 + * If streamon has been called, and we haven't yet called
  1721 + * start_streaming() since not enough buffers were queued, and
  1722 + * we now have reached the minimum number of queued buffers,
  1723 + * then we can finally call start_streaming().
  1724 + */
  1725 + if (q->streaming && !q->start_streaming_called &&
  1726 + q->queued_count >= q->min_buffers_needed) {
1679 1727 ret = vb2_start_streaming(q);
1680 1728 if (ret)
1681 1729 return ret;
... ... @@ -1830,7 +1878,7 @@
1830 1878 return -EINVAL;
1831 1879 }
1832 1880  
1833   - if (!q->retry_start_streaming)
  1881 + if (q->start_streaming_called)
1834 1882 wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count));
1835 1883 return 0;
1836 1884 }
... ... @@ -1891,6 +1939,7 @@
1891 1939 __fill_v4l2_buffer(vb, b);
1892 1940 /* Remove from videobuf queue */
1893 1941 list_del(&vb->queued_entry);
  1942 + q->queued_count--;
1894 1943 /* go back to dequeued state */
1895 1944 __vb2_dqbuf(vb);
1896 1945  
1897 1946  
1898 1947  
1899 1948  
... ... @@ -1941,19 +1990,24 @@
1941 1990 {
1942 1991 unsigned int i;
1943 1992  
1944   - if (q->retry_start_streaming) {
1945   - q->retry_start_streaming = 0;
1946   - q->streaming = 0;
1947   - }
1948   -
1949 1993 /*
1950 1994 * Tell driver to stop all transactions and release all queued
1951 1995 * buffers.
1952 1996 */
1953   - if (q->streaming)
  1997 + if (q->start_streaming_called)
1954 1998 call_qop(q, stop_streaming, q);
1955 1999 q->streaming = 0;
  2000 + q->start_streaming_called = 0;
  2001 + q->queued_count = 0;
1956 2002  
  2003 + if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
  2004 + for (i = 0; i < q->num_buffers; ++i)
  2005 + if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE)
  2006 + vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR);
  2007 + /* Must be zero now */
  2008 + WARN_ON(atomic_read(&q->owned_by_drv_count));
  2009 + }
  2010 +
1957 2011 /*
1958 2012 * Remove all buffers from videobuf's list...
1959 2013 */
... ... @@ -1988,7 +2042,6 @@
1988 2042  
1989 2043 static int vb2_internal_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1990 2044 {
1991   - struct vb2_buffer *vb;
1992 2045 int ret;
1993 2046  
1994 2047 if (type != q->type) {
1995 2048  
1996 2049  
... ... @@ -2010,19 +2063,22 @@
2010 2063 dprintk(1, "streamon: no buffers have been allocated\n");
2011 2064 return -EINVAL;
2012 2065 }
  2066 + if (q->num_buffers < q->min_buffers_needed) {
  2067 + dprintk(1, "streamon: need at least %u allocated buffers\n",
  2068 + q->min_buffers_needed);
  2069 + return -EINVAL;
  2070 + }
2013 2071  
2014 2072 /*
2015   - * If any buffers were queued before streamon,
2016   - * we can now pass them to driver for processing.
  2073 + * Tell driver to start streaming provided sufficient buffers
  2074 + * are available.
2017 2075 */
2018   - list_for_each_entry(vb, &q->queued_list, queued_entry)
2019   - __enqueue_in_driver(vb);
2020   -
2021   - /* Tell driver to start streaming. */
2022   - ret = vb2_start_streaming(q);
2023   - if (ret) {
2024   - __vb2_queue_cancel(q);
2025   - return ret;
  2076 + if (q->queued_count >= q->min_buffers_needed) {
  2077 + ret = vb2_start_streaming(q);
  2078 + if (ret) {
  2079 + __vb2_queue_cancel(q);
  2080 + return ret;
  2081 + }
2026 2082 }
2027 2083  
2028 2084 q->streaming = 1;
drivers/staging/media/davinci_vpfe/vpfe_video.c
... ... @@ -1201,8 +1201,6 @@
1201 1201 unsigned long addr;
1202 1202 int ret;
1203 1203  
1204   - if (count == 0)
1205   - return -ENOBUFS;
1206 1204 ret = mutex_lock_interruptible(&video->lock);
1207 1205 if (ret)
1208 1206 goto streamoff;
... ... @@ -1327,6 +1325,7 @@
1327 1325 q->type = req_buf->type;
1328 1326 q->io_modes = VB2_MMAP | VB2_USERPTR;
1329 1327 q->drv_priv = fh;
  1328 + q->min_buffers_needed = 1;
1330 1329 q->ops = &video_qops;
1331 1330 q->mem_ops = &vb2_dma_contig_memops;
1332 1331 q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
include/media/videobuf2-core.h
... ... @@ -356,20 +356,24 @@
356 356 * @gfp_flags: additional gfp flags used when allocating the buffers.
357 357 * Typically this is 0, but it may be e.g. GFP_DMA or __GFP_DMA32
358 358 * to force the buffer allocation to a specific memory zone.
  359 + * @min_buffers_needed: the minimum number of buffers needed before
  360 + * start_streaming() can be called. Used when a DMA engine
  361 + * cannot be started unless at least this number of buffers
  362 + * have been queued into the driver.
359 363 *
360 364 * @memory: current memory type used
361 365 * @bufs: videobuf buffer structures
362 366 * @num_buffers: number of allocated/used buffers
363 367 * @queued_list: list of buffers currently queued from userspace
  368 + * @queued_count: number of buffers queued and ready for streaming.
364 369 * @owned_by_drv_count: number of buffers owned by the driver
365 370 * @done_list: list of buffers ready to be dequeued to userspace
366 371 * @done_lock: lock to protect done_list list
367 372 * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued
368 373 * @alloc_ctx: memory type/allocator-specific contexts for each plane
369 374 * @streaming: current streaming state
370   - * @retry_start_streaming: start_streaming() was called, but there were not enough
371   - * buffers queued. If set, then retry calling start_streaming when
372   - * queuing a new buffer.
  375 + * @start_streaming_called: start_streaming() was called successfully and we
  376 + * started streaming.
373 377 * @fileio: file io emulator internal data, used only if emulator is active
374 378 */
375 379 struct vb2_queue {
... ... @@ -385,6 +389,7 @@
385 389 unsigned int buf_struct_size;
386 390 u32 timestamp_flags;
387 391 gfp_t gfp_flags;
  392 + u32 min_buffers_needed;
388 393  
389 394 /* private: internal use only */
390 395 enum v4l2_memory memory;
... ... @@ -392,6 +397,7 @@
392 397 unsigned int num_buffers;
393 398  
394 399 struct list_head queued_list;
  400 + unsigned int queued_count;
395 401  
396 402 atomic_t owned_by_drv_count;
397 403 struct list_head done_list;
... ... @@ -402,7 +408,7 @@
402 408 unsigned int plane_sizes[VIDEO_MAX_PLANES];
403 409  
404 410 unsigned int streaming:1;
405   - unsigned int retry_start_streaming:1;
  411 + unsigned int start_streaming_called:1;
406 412  
407 413 struct vb2_fileio_data *fileio;
408 414