media: v4l2-mem2mem: Apply DST_QUEUE_OFF_BASE on MMAP buffers across ioctls
authorChen-Yu Tsai <wenst@chromium.org>
Thu, 9 Dec 2021 16:38:03 +0000 (17:38 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 3 Nov 2022 14:50:51 +0000 (23:50 +0900)
commit 8310ca94075e784bbb06593cd6c068ee6b6e4ca6 upstream.

DST_QUEUE_OFF_BASE is applied to offset/mem_offset on MMAP capture buffers
only for the VIDIOC_QUERYBUF ioctl, while the userspace fields (including
offset/mem_offset) are filled in for VIDIOC_{QUERY,PREPARE,Q,DQ}BUF
ioctls. This leads to differences in the values presented to userspace.
If userspace attempts to mmap the capture buffer directly using values
from DQBUF, it will fail.

Move the code that applies the magic offset into a helper, and call
that helper from all four ioctl entry points.

[hverkuil: drop unnecessary '= 0' in v4l2_m2m_querybuf() for ret]

Fixes: 7f98639def42 ("V4L/DVB: add memory-to-memory device helper framework for videobuf")
Fixes: 908a0d7c588e ("[media] v4l: mem2mem: port to videobuf2")
Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
[OP: adjusted return logic for 4.14]
Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/media/v4l2-core/v4l2-mem2mem.c

index f62e68aa04c42712060a9898a55ae51511c2cd55..dcb869c588f285f1b6027a7da8a90aa5c3c35ea9 100644 (file)
@@ -358,19 +358,14 @@ int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 }
 EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs);
 
-int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
-                     struct v4l2_buffer *buf)
+static void v4l2_m2m_adjust_mem_offset(struct vb2_queue *vq,
+                                      struct v4l2_buffer *buf)
 {
-       struct vb2_queue *vq;
-       int ret = 0;
-       unsigned int i;
-
-       vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
-       ret = vb2_querybuf(vq, buf);
-
        /* Adjust MMAP memory offsets for the CAPTURE queue */
        if (buf->memory == V4L2_MEMORY_MMAP && !V4L2_TYPE_IS_OUTPUT(vq->type)) {
                if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) {
+                       unsigned int i;
+
                        for (i = 0; i < buf->length; ++i)
                                buf->m.planes[i].m.mem_offset
                                        += DST_QUEUE_OFF_BASE;
@@ -378,8 +373,23 @@ int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
                        buf->m.offset += DST_QUEUE_OFF_BASE;
                }
        }
+}
 
-       return ret;
+int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
+                     struct v4l2_buffer *buf)
+{
+       struct vb2_queue *vq;
+       int ret;
+
+       vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
+       ret = vb2_querybuf(vq, buf);
+       if (ret)
+               return ret;
+
+       /* Adjust MMAP memory offsets for the CAPTURE queue */
+       v4l2_m2m_adjust_mem_offset(vq, buf);
+
+       return 0;
 }
 EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf);
 
@@ -391,10 +401,15 @@ int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 
        vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
        ret = vb2_qbuf(vq, buf);
-       if (!ret)
-               v4l2_m2m_try_schedule(m2m_ctx);
+       if (ret)
+               return ret;
 
-       return ret;
+       /* Adjust MMAP memory offsets for the CAPTURE queue */
+       v4l2_m2m_adjust_mem_offset(vq, buf);
+
+       v4l2_m2m_try_schedule(m2m_ctx);
+
+       return 0;
 }
 EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf);
 
@@ -402,9 +417,17 @@ int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
                   struct v4l2_buffer *buf)
 {
        struct vb2_queue *vq;
+       int ret;
 
        vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
-       return vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK);
+       ret = vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK);
+       if (ret)
+               return ret;
+
+       /* Adjust MMAP memory offsets for the CAPTURE queue */
+       v4l2_m2m_adjust_mem_offset(vq, buf);
+
+       return 0;
 }
 EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf);
 
@@ -416,10 +439,15 @@ int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 
        vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
        ret = vb2_prepare_buf(vq, buf);
-       if (!ret)
-               v4l2_m2m_try_schedule(m2m_ctx);
+       if (ret)
+               return ret;
 
-       return ret;
+       /* Adjust MMAP memory offsets for the CAPTURE queue */
+       v4l2_m2m_adjust_mem_offset(vq, buf);
+
+       v4l2_m2m_try_schedule(m2m_ctx);
+
+       return 0;
 }
 EXPORT_SYMBOL_GPL(v4l2_m2m_prepare_buf);