media: coda: lock capture queue wakeup against encoder stop command
authorPhilipp Zabel <p.zabel@pengutronix.de>
Tue, 18 Jun 2019 16:45:30 +0000 (12:45 -0400)
committerMauro Carvalho Chehab <mchehab+samsung@kernel.org>
Mon, 24 Jun 2019 18:44:26 +0000 (14:44 -0400)
Make sure that an encoder stop command running concurrently with an
encoder finish_run always either flags the last returned buffer or wakes
up the capture queue to signal the end of stream condition afterwards.

Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
drivers/media/platform/coda/coda-bit.c
drivers/media/platform/coda/coda-common.c
drivers/media/platform/coda/coda.h

index 7bcdfe8dcf3d6bdc11cf627c5ecb0d730a588fa2..44085e3d43d5073815626ef68cba5cc921758b05 100644 (file)
@@ -1540,6 +1540,12 @@ static void coda_finish_encode(struct coda_ctx *ctx)
        struct coda_dev *dev = ctx->dev;
        u32 wr_ptr, start_ptr;
 
+       /*
+        * Lock to make sure that an encoder stop command running in parallel
+        * will either already have marked src_buf as last, or it will wake up
+        * the capture queue after the buffers are returned.
+        */
+       mutex_lock(&ctx->wakeup_mutex);
        src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
        dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
 
@@ -1580,6 +1586,7 @@ static void coda_finish_encode(struct coda_ctx *ctx)
 
        dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
        coda_m2m_buf_done(ctx, dst_buf, VB2_BUF_STATE_DONE);
+       mutex_unlock(&ctx->wakeup_mutex);
 
        ctx->gopcounter--;
        if (ctx->gopcounter < 0)
index 73c18be14cbf3497365575b5f39373dd5580f288..95c233b2cbf863e8ec312b1c150b0a65a786c40f 100644 (file)
@@ -1034,19 +1034,27 @@ static int coda_encoder_cmd(struct file *file, void *fh,
        if (ret < 0)
                return ret;
 
+       mutex_lock(&ctx->wakeup_mutex);
        buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
        if (buf) {
+               /*
+                * If the last output buffer is still on the queue, make sure
+                * that decoder finish_run will see the last flag and report it
+                * to userspace.
+                */
                buf->flags |= V4L2_BUF_FLAG_LAST;
        } else {
                /* Set the stream-end flag on this context */
                ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
 
-               flush_work(&ctx->pic_run_work);
-
-               /* If there is no buffer in flight, wake up */
-               if (!ctx->streamon_out || ctx->qsequence == ctx->osequence)
-                       coda_wake_up_capture_queue(ctx);
+               /*
+                * If the last output buffer has already been taken from the
+                * queue, wake up the capture queue and signal end of stream
+                * via the -EPIPE mechanism.
+                */
+               coda_wake_up_capture_queue(ctx);
        }
+       mutex_unlock(&ctx->wakeup_mutex);
 
        return 0;
 }
@@ -2466,6 +2474,7 @@ static int coda_open(struct file *file)
 
        mutex_init(&ctx->bitstream_mutex);
        mutex_init(&ctx->buffer_mutex);
+       mutex_init(&ctx->wakeup_mutex);
        INIT_LIST_HEAD(&ctx->buffer_meta_list);
        spin_lock_init(&ctx->buffer_meta_lock);
 
index 6911c1c811cefe735df8743634b4343f3e7e3a4c..97845e58fb8b8a8260ccdfae009d98243a82c9fc 100644 (file)
@@ -258,6 +258,12 @@ struct coda_ctx {
        bool                            use_bit;
        bool                            use_vdoa;
        struct vdoa_ctx                 *vdoa;
+       /*
+        * wakeup mutex used to serialize encoder stop command and finish_run,
+        * ensures that finish_run always either flags the last returned buffer
+        * or wakes up the capture queue to signal EOS afterwards.
+        */
+       struct mutex                    wakeup_mutex;
 };
 
 extern int coda_debug;