Skip to contentSkip to conversation

media: rkvdec: Fix .buf_prepare

PATCH Superseded Andrzej Pietrasiewicz 2 participants 7 messages May 4, 2021 – May 5, 2021

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: rkvdec: Fix .buf_prepare

From: Ezequiel Garcia <[email protected]>

The driver should only set the payload on .buf_prepare if the buffer is CAPTURE type. If an OUTPUT buffer has a zero bytesused set by userspace then v4l2-core will set it to buffer length.

Fixes cd33c830448ba ("media: rkvdec: Add the rkvdec driver")

---
@Hans: I haven't had anyone complain about the issue. The fix is needed for the rkvdec vp9 work, so I think 5.14 is fine.

 drivers/staging/media/rkvdec/rkvdec.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/staging/media/rkvdec/rkvdec.c +9 -1
index d821661d30f3..ef2166043127 100644
@@ -481,7 +481,15 @@ static int rkvdec_buf_prepare(struct vb2_buffer *vb)
 		if (vb2_plane_size(vb, i) < sizeimage)
 			return -EINVAL;
 	}
-	vb2_set_plane_payload(vb, 0, f->fmt.pix_mp.plane_fmt[0].sizeimage);
+
+	/*
+	 * Buffer bytesused is written by driver for CAPTURE buffers.
+	 * (if userspace passes 0 bytesused for OUTPUT buffers, v4l2-core sets
+	 * it to buffer length).
+	 */
+	if (!V4L2_TYPE_IS_OUTPUT(vq->type))
+		vb2_set_plane_payload(vb, 0, f->fmt.pix_mp.plane_fmt[0].sizeimage);
+
 	return 0;
 }
 

base-commit: 0b276e470a4d43e1365d3eb53c608a3d208cabd4

Signature · 1 line
2.17.1
#1 lore raw

Hi Andrzej,

Thanks a lot for picking this up.

Quoted 24 lines · Andrzej Pietrasiewicz

From: Ezequiel Garcia <[email protected]>

The driver should only set the payload on .buf_prepare if the buffer is CAPTURE type. If an OUTPUT buffer has a zero bytesused set by userspace then v4l2-core will set it to buffer length.

Fixes cd33c830448ba ("media: rkvdec: Add the rkvdec driver")

---
@Hans: I haven't had anyone complain about the issue. The fix is needed for the rkvdec vp9 work, so I think 5.14 is fine.

 drivers/staging/media/rkvdec/rkvdec.c | 10 +++++++++-

 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/staging/media/rkvdec/rkvdec.c +0 -0
index d821661d30f3..ef2166043127 100644
@@ -481,7 +481,15 @@ static int rkvdec_buf_prepare(struct vb2_buffer *vb)
                 if (vb2_plane_size(vb, i) < sizeimage)

drivers/staging/media/rkvdec/rkvdec.c static int rkvdec_buf_prepare(struct vb2_buffer *vb) · line 490

Quoted 10 lines
drivers/staging/media/rkvdec/rkvdec.c +7 -1
                         return -EINVAL;
         }
-       vb2_set_plane_payload(vb, 0, f->fmt.pix_mp.plane_fmt[0].sizeimage);
+
+       /*
+        * Buffer bytesused is written by driver for CAPTURE buffers.
+        * (if userspace passes 0 bytesused for OUTPUT buffers, v4l2-core sets
+        * it to buffer length).
+        */
+       if (!V4L2_TYPE_IS_OUTPUT(vq->type))

Please use V4L2_TYPE_IS_CAPTURE here.

Also, why is this change needed in rkvdec, but not in cedrus or hantro?

Thanks!
Ezequiel

#2 lore raw

Hi Ezequiel,

W dniu 04.05.2021 o 13:56, Ezequiel Garcia pisze:

Quoted 42 lines

Hi Andrzej,

Thanks a lot for picking this up.

Quoted 24 lines · Andrzej Pietrasiewicz

From: Ezequiel Garcia <[email protected]>

The driver should only set the payload on .buf_prepare if the buffer is CAPTURE type. If an OUTPUT buffer has a zero bytesused set by userspace then v4l2-core will set it to buffer length.

Fixes cd33c830448ba ("media: rkvdec: Add the rkvdec driver")

---
@Hans: I haven't had anyone complain about the issue. The fix is needed for the rkvdec vp9 work, so I think 5.14 is fine.

  drivers/staging/media/rkvdec/rkvdec.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
drivers/staging/media/rkvdec/rkvdec.c +0 -0
index d821661d30f3..ef2166043127 100644
@@ -481,7 +481,15 @@ static int rkvdec_buf_prepare(struct vb2_buffer *vb)
                 if (vb2_plane_size(vb, i) < sizeimage)

drivers/staging/media/rkvdec/rkvdec.c static int rkvdec_buf_prepare(struct vb2_buffer *vb) · line 490

Quoted 10 lines
drivers/staging/media/rkvdec/rkvdec.c +7 -1
                         return -EINVAL;
         }
-       vb2_set_plane_payload(vb, 0, f->fmt.pix_mp.plane_fmt[0].sizeimage);
+
+       /*
+        * Buffer bytesused is written by driver for CAPTURE buffers.
+        * (if userspace passes 0 bytesused for OUTPUT buffers, v4l2-core sets
+        * it to buffer length).
+        */
+       if (!V4L2_TYPE_IS_OUTPUT(vq->type))

Please use V4L2_TYPE_IS_CAPTURE here.

Quoted 3 lines

Also, why is this change needed in rkvdec, but not in cedrus or hantro?

As a matter of fact I think it is needed in all three, because later on, whenever a driver uses vb2_get_plane_payload(), without such a patch it will get an invalid number and write that to a hardware register, causing incorrect behavior.

I will respond with a v2 series.

Regards,

Andrzej

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

#3 lore raw

v2 cover Fix .buf_prepare

Drivers should only set the payload on .buf_prepare if the buffer is CAPTURE type. If an OUTPUT buffer has a zero bytesused set by userspace then v4l2-core will set it to buffer length.

If we overwrite bytesused for OUTPUT buffers, too, then vb2_get_plane_payload() will return incorrect value which might be then written to hw registers by the driver.

Andrzej Pietrasiewicz (2):
media: hantro: Fix .buf_prepare
media: cedrus: Fix .buf_prepare

Ezequiel Garcia (1):
media: rkvdec: Fix .buf_prepare

 drivers/staging/media/hantro/hantro_v4l2.c        |  9 ++++++++-
 drivers/staging/media/rkvdec/rkvdec.c             | 10 +++++++++-
 drivers/staging/media/sunxi/cedrus/cedrus_video.c |  8 +++++++-
 3 files changed, 24 insertions(+), 3 deletions(-)

base-commit: 0b276e470a4d43e1365d3eb53c608a3d208cabd4

Signature · 1 line
2.17.1
#4 lore raw

PATCH v2 1/3 media: rkvdec: Fix .buf_prepare

From: Ezequiel Garcia <[email protected]>

The driver should only set the payload on .buf_prepare if the buffer is CAPTURE type. If an OUTPUT buffer has a zero bytesused set by userspace then v4l2-core will set it to buffer length.

If we overwrite bytesused for OUTPUT buffers, too, then vb2_get_plane_payload() will return incorrect value which might be then written to hw registers by the driver in rkvdec-h264.c.

Fixes cd33c830448ba ("media: rkvdec: Add the rkvdec driver")
[Changed the comment and used V4L2_TYPE_IS_CAPTURE macro]
---
 drivers/staging/media/rkvdec/rkvdec.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/staging/media/rkvdec/rkvdec.c +9 -1
index d821661d30f3..cd65ad2af8d4 100644
@@ -481,7 +481,15 @@ static int rkvdec_buf_prepare(struct vb2_buffer *vb)
 		if (vb2_plane_size(vb, i) < sizeimage)
 			return -EINVAL;
 	}
-	vb2_set_plane_payload(vb, 0, f->fmt.pix_mp.plane_fmt[0].sizeimage);
+
+	/*
+	 * Buffer's bytesused must be written by driver for CAPTURE buffers.
+	 * (for OUTPUT buffers, if userspace passes 0 bytesused, v4l2-core sets
+	 * it to buffer length).
+	 */
+	if (V4L2_TYPE_IS_CAPTURE(vq->type))
+		vb2_set_plane_payload(vb, 0, f->fmt.pix_mp.plane_fmt[0].sizeimage);
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
#5 lore raw

PATCH v2 2/3 media: hantro: Fix .buf_prepare

In reply to Andrzej Pietrasiewicz

The driver should only set the payload on .buf_prepare if the buffer is CAPTURE type. If an OUTPUT buffer has a zero bytesused set by userspace then v4l2-core will set it to buffer length.

If we overwrite bytesused for OUTPUT buffers, too, then vb2_get_plane_payload() will return incorrect value which might be then written to hw registers by the driver in hantro_g1_h264_dec.c.

---
 drivers/staging/media/hantro/hantro_v4l2.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/staging/media/hantro/hantro_v4l2.c +8 -1
index 1bc118e375a1..7ccc6405036a 100644
@@ -639,7 +639,14 @@ static int hantro_buf_prepare(struct vb2_buffer *vb)
 	ret = hantro_buf_plane_check(vb, pix_fmt);
 	if (ret)
 		return ret;
-	vb2_set_plane_payload(vb, 0, pix_fmt->plane_fmt[0].sizeimage);
+	/*
+	 * Buffer's bytesused must be written by driver for CAPTURE buffers.
+	 * (for OUTPUT buffers, if userspace passes 0 bytesused, v4l2-core sets
+	 * it to buffer length).
+	 */
+	if (V4L2_TYPE_IS_CAPTURE(vq->type))
+		vb2_set_plane_payload(vb, 0, pix_fmt->plane_fmt[0].sizeimage);
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
#6 lore raw

PATCH v2 3/3 media: cedrus: Fix .buf_prepare

In reply to Andrzej Pietrasiewicz

The driver should only set the payload on .buf_prepare if the buffer is CAPTURE type. If an OUTPUT buffer has a zero bytesused set by userspace then v4l2-core will set it to buffer length.

If we overwrite bytesused for OUTPUT buffers, too, then vb2_get_plane_payload() will return incorrect value which might be then written to hw registers by the driver in cedrus_h264.c or cedrus_vp8.c.

---
 drivers/staging/media/sunxi/cedrus/cedrus_video.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/staging/media/sunxi/cedrus/cedrus_video.c +7 -1
index b62eb8e84057..bf731caf2ed5 100644
@@ -457,7 +457,13 @@ static int cedrus_buf_prepare(struct vb2_buffer *vb)
 	if (vb2_plane_size(vb, 0) < pix_fmt->sizeimage)
 		return -EINVAL;
 
-	vb2_set_plane_payload(vb, 0, pix_fmt->sizeimage);
+	/*
+	 * Buffer's bytesused must be written by driver for CAPTURE buffers.
+	 * (for OUTPUT buffers, if userspace passes 0 bytesused, v4l2-core sets
+	 * it to buffer length).
+	 */
+	if (V4L2_TYPE_IS_CAPTURE(vq->type))
+		vb2_set_plane_payload(vb, 0, pix_fmt->sizeimage);
 
 	return 0;
 }
Signature · 1 line
2.17.1