Skip to contentSkip to conversation

media: venus: fix reported size of 0-length buffers

PATCH In review Alexandre Courbot 4 participants 10 messages Nov 13, 2018 – Nov 26, 2018 1 message not in the archive

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

PATCH 1/1 media: venus: fix reported size of 0-length buffers

The last buffer is often signaled by an empty buffer with the V4L2_BUF_FLAG_LAST flag set. Such buffers were returned with the bytesused field set to the full size of the OPB, which leads user-space to believe that the buffer actually contains useful data. Fix this by passing the number of bytes reported used by the firmware.

---
 drivers/media/platform/qcom/venus/vdec.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/media/platform/qcom/venus/vdec.c +1 -3
index 189ec975c6bb..282de21cf2e1 100644
@@ -885,10 +885,8 @@ static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type,
 	vbuf->field = V4L2_FIELD_NONE;
 
 	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
-		unsigned int opb_sz = venus_helper_get_opb_size(inst);
-
 		vb = &vbuf->vb2_buf;
-		vb2_set_plane_payload(vb, 0, bytesused ? : opb_sz);
+		vb2_set_plane_payload(vb, 0, bytesused);
 		vb->planes[0].data_offset = data_offset;
 		vb->timestamp = timestamp_us * NSEC_PER_USEC;
 		vbuf->sequence = inst->sequence_cap++;
Signature · 1 line
2.19.1.930.g4563a0d9d0-goog

A message that is not in the archive (sent off-list or not yet received): CAKQmDh-91tHP1VxLisW1A3GR9G7du3F-Y2XrrgoFU=gvhGoP6w@mail.gmail.com

#2 lore raw
Quoted 5 lines · Nicolas Dufresne
Quoted 1 line · Alexandre Courbot
Quoted 8 lines
Quoted 5 lines

The last buffer is often signaled by an empty buffer with the V4L2_BUF_FLAG_LAST flag set. Such buffers were returned with the bytesused field set to the full size of the OPB, which leads user-space to believe that the buffer actually contains useful data. Fix this by passing the number of bytes reported used by the firmware.

That means the driver does not know on time which one is last. Why not just returned EPIPE to userspace on DQBUF and ovoid this useless roundtrip ?

Sorry, I don't understand what you mean. EPIPE is supposed to be returned after a buffer with V4L2_BUF_FLAG_LAST is made available for dequeue. This patch amends the code that prepares this LAST-flagged buffer. How could we avoid a roundtrip in this case?

Quoted 26 lines
Quoted 26 lines
---
 drivers/media/platform/qcom/venus/vdec.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/media/platform/qcom/venus/vdec.c +1 -4
index 189ec975c6bb..282de21cf2e1 100644
@@ -885,10 +885,8 @@ static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type,
        vbuf->field = V4L2_FIELD_NONE;
 
        if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
-               unsigned int opb_sz = venus_helper_get_opb_size(inst);
-
                vb = &vbuf->vb2_buf;
-               vb2_set_plane_payload(vb, 0, bytesused ? : opb_sz);
+               vb2_set_plane_payload(vb, 0, bytesused);
                vb->planes[0].data_offset = data_offset;
                vb->timestamp = timestamp_us * NSEC_PER_USEC;
                vbuf->sequence = inst->sequence_cap++;
--

2.19.1.930.g4563a0d9d0-goog

#3 lore raw
Quoted 10 lines · Alexandre Courbot
Quoted 9 lines · Nicolas Dufresne
Quoted 5 lines · Alexandre Courbot

The last buffer is often signaled by an empty buffer with the V4L2_BUF_FLAG_LAST flag set. Such buffers were returned with the bytesused field set to the full size of the OPB, which leads user-space to believe that the buffer actually contains useful data. Fix this by passing the number of bytes reported used by the firmware.

Quoted 6 lines
Quoted 1 line

That means the driver does not know on time which one is last. Why not just returned EPIPE to userspace on DQBUF and ovoid this useless roundtrip ?

Sorry, I don't understand what you mean. EPIPE is supposed to be returned after a buffer with V4L2_BUF_FLAG_LAST is made available for dequeue. This patch amends the code that prepares this LAST-flagged buffer. How could we avoid a roundtrip in this case?

Maybe it has changed, but when this was introduced, we found that some firmware (Exynos MFC) could not know which one is last. Instead, it gets an event saying there will be no more buffers.

Sending buffers with payload size to 0 just for the sake of setting the V4L2_BUF_FLAG_LAST was considered a waste. Specially that after that, every polls should return EPIPE. So in the end, we decided the it should just unblock the userspace and return EPIPE.

If you look at the related GStreamer code, it completely ignores the LAST flag. With fake buffer of size 0, userspace will endup dequeuing and throwing away. This is not useful to the process of terminating the decoding. To me, this LAST flag is not useful in this context.

Nicolas

#4 lore raw
Quoted 4 lines · Nicolas Dufresne

Sending buffers with payload size to 0 just for the sake of setting the V4L2_BUF_FLAG_LAST was considered a waste. Specially that after that, every polls should return EPIPE. So in the end, we decided the it should just unblock the userspace and return EPIPE.

errata, DQBUF returns EPIPE, not sure why I keep saying poll.

sorry for that,
Nicolas

#5 lore raw
In reply to Nicolas Dufresne
Quoted 14 lines · Nicolas Dufresne
Quoted 12 lines · Alexandre Courbot
Quoted 10 lines · Nicolas Dufresne
Quoted 5 lines · Alexandre Courbot

The last buffer is often signaled by an empty buffer with the V4L2_BUF_FLAG_LAST flag set. Such buffers were returned with the bytesused field set to the full size of the OPB, which leads user-space to believe that the buffer actually contains useful data. Fix this by passing the number of bytes reported used by the firmware.

That means the driver does not know on time which one is last. Why not just returned EPIPE to userspace on DQBUF and ovoid this useless roundtrip ?

Quoted 9 lines
Quoted 4 lines

Sorry, I don't understand what you mean. EPIPE is supposed to be returned after a buffer with V4L2_BUF_FLAG_LAST is made available for dequeue. This patch amends the code that prepares this LAST-flagged buffer. How could we avoid a roundtrip in this case?

Maybe it has changed, but when this was introduced, we found that some firmware (Exynos MFC) could not know which one is last. Instead, it gets an event saying there will be no more buffers.

It was never the case with the MFC (firmware/driver) we were using on Chrome OS and it doesn't seem to be the case for the current upstream s5p-mfc driver.

Quoted 9 lines

Sending buffers with payload size to 0 just for the sake of setting the V4L2_BUF_FLAG_LAST was considered a waste. Specially that after that, every polls should return EPIPE. So in the end, we decided the it should just unblock the userspace and return EPIPE.

If you look at the related GStreamer code, it completely ignores the LAST flag. With fake buffer of size 0, userspace will endup dequeuing and throwing away. This is not useful to the process of terminating the decoding. To me, this LAST flag is not useful in this context.

Except that -EPIPE is actually signaled by the vb2 core and it happens after the user space dequeues a buffer with the LAST flag set:
https://elixir.bootlin.com/linux/v4.20-rc2/source/drivers/media/common/videobuf2/videobuf2-core.c#L1634
https://elixir.bootlin.com/linux/v4.20-rc2/source/drivers/media/common/videobuf2/videobuf2-v4l2.c#L555

Best regards,
Tomasz

#6 lore raw
In reply to Nicolas Dufresne
Quoted 28 lines · Nicolas Dufresne
Quoted 10 lines · Alexandre Courbot
Quoted 9 lines · Nicolas Dufresne
Quoted 5 lines · Alexandre Courbot

The last buffer is often signaled by an empty buffer with the V4L2_BUF_FLAG_LAST flag set. Such buffers were returned with the bytesused field set to the full size of the OPB, which leads user-space to believe that the buffer actually contains useful data. Fix this by passing the number of bytes reported used by the firmware.

Quoted 6 lines
Quoted 1 line

That means the driver does not know on time which one is last. Why not just returned EPIPE to userspace on DQBUF and ovoid this useless roundtrip ?

Sorry, I don't understand what you mean. EPIPE is supposed to be returned after a buffer with V4L2_BUF_FLAG_LAST is made available for dequeue. This patch amends the code that prepares this LAST-flagged buffer. How could we avoid a roundtrip in this case?

Maybe it has changed, but when this was introduced, we found that some firmware (Exynos MFC) could not know which one is last. Instead, it gets an event saying there will be no more buffers.

Sending buffers with payload size to 0 just for the sake of setting the V4L2_BUF_FLAG_LAST was considered a waste. Specially that after that, every polls should return EPIPE. So in the end, we decided the it should just unblock the userspace and return EPIPE.

Quoted 4 lines

If you look at the related GStreamer code, it completely ignores the LAST flag. With fake buffer of size 0, userspace will endup dequeuing and throwing away. This is not useful to the process of terminating the decoding. To me, this LAST flag is not useful in this context.

Note that this patch does not interfere with DQBUF returning -EPIPE after the last buffer has been dequeued. It just fixes an invalid size that was returned for the last buffer.

Note also that if I understand the doc properly, the kernel driver *must* set the V4L2_BUF_FLAG_LAST on the last buffer. With Venus the last buffer is signaled by the firmware with an empty buffer. That's not something we can change or predict earlier, so in order to respect the specification we need to return that empty buffer. After that DQBUF will behave as expected (returning -EPIPE), so GStreamer should be happy as well.

Without the proposed fix however, GStreamer would receive the last buffer with an incorrect size, and thus interpret random data as a frame.

So to me this fix seems to be both correct, and needed. Isn't it?

Cheers,
Alex.

#7 lore raw
Quoted 44 lines · Alexandre Courbot
Quoted 26 lines · Nicolas Dufresne
Quoted 9 lines · Alexandre Courbot
Quoted 8 lines · Nicolas Dufresne
Quoted 5 lines · Alexandre Courbot

The last buffer is often signaled by an empty buffer with the V4L2_BUF_FLAG_LAST flag set. Such buffers were returned with the bytesused field set to the full size of the OPB, which leads user-space to believe that the buffer actually contains useful data. Fix this by passing the number of bytes reported used by the firmware.

Quoted 6 lines
Quoted 1 line

That means the driver does not know on time which one is last. Why not just returned EPIPE to userspace on DQBUF and ovoid this useless roundtrip ?

Sorry, I don't understand what you mean. EPIPE is supposed to be returned after a buffer with V4L2_BUF_FLAG_LAST is made available for dequeue. This patch amends the code that prepares this LAST-flagged buffer. How could we avoid a roundtrip in this case?

Maybe it has changed, but when this was introduced, we found that some firmware (Exynos MFC) could not know which one is last. Instead, it gets an event saying there will be no more buffers.

Sending buffers with payload size to 0 just for the sake of setting the V4L2_BUF_FLAG_LAST was considered a waste. Specially that after that, every polls should return EPIPE. So in the end, we decided the it should just unblock the userspace and return EPIPE.

Quoted 4 lines

If you look at the related GStreamer code, it completely ignores the LAST flag. With fake buffer of size 0, userspace will endup dequeuing and throwing away. This is not useful to the process of terminating the decoding. To me, this LAST flag is not useful in this context.

Note that this patch does not interfere with DQBUF returning -EPIPE after the last buffer has been dequeued. It just fixes an invalid size that was returned for the last buffer.

Note also that if I understand the doc properly, the kernel driver *must* set the V4L2_BUF_FLAG_LAST on the last buffer. With Venus the last buffer is signaled by the firmware with an empty buffer. That's not something we can change or predict earlier, so in order to respect the specification we need to return that empty buffer. After that DQBUF will behave as expected (returning -EPIPE), so GStreamer should be happy as well.

Quoted 5 lines

Without the proposed fix however, GStreamer would receive the last buffer with an incorrect size, and thus interpret random data as a frame.

So to me this fix seems to be both correct, and needed. Isn't it?

Totally, thanks for the extra clarification.

Quoted 3 lines

Cheers,
Alex.

#8 lore raw
Quoted 46 lines · Nicolas Dufresne
Quoted 44 lines · Alexandre Courbot
Quoted 26 lines · Nicolas Dufresne
Quoted 9 lines · Alexandre Courbot
Quoted 8 lines · Nicolas Dufresne
Quoted 5 lines · Alexandre Courbot

The last buffer is often signaled by an empty buffer with the V4L2_BUF_FLAG_LAST flag set. Such buffers were returned with the bytesused field set to the full size of the OPB, which leads user-space to believe that the buffer actually contains useful data. Fix this by passing the number of bytes reported used by the firmware.

Quoted 6 lines
Quoted 1 line

That means the driver does not know on time which one is last. Why not just returned EPIPE to userspace on DQBUF and ovoid this useless roundtrip ?

Sorry, I don't understand what you mean. EPIPE is supposed to be returned after a buffer with V4L2_BUF_FLAG_LAST is made available for dequeue. This patch amends the code that prepares this LAST-flagged buffer. How could we avoid a roundtrip in this case?

Maybe it has changed, but when this was introduced, we found that some firmware (Exynos MFC) could not know which one is last. Instead, it gets an event saying there will be no more buffers.

Sending buffers with payload size to 0 just for the sake of setting the V4L2_BUF_FLAG_LAST was considered a waste. Specially that after that, every polls should return EPIPE. So in the end, we decided the it should just unblock the userspace and return EPIPE.

Quoted 4 lines

If you look at the related GStreamer code, it completely ignores the LAST flag. With fake buffer of size 0, userspace will endup dequeuing and throwing away. This is not useful to the process of terminating the decoding. To me, this LAST flag is not useful in this context.

Note that this patch does not interfere with DQBUF returning -EPIPE after the last buffer has been dequeued. It just fixes an invalid size that was returned for the last buffer.

Note also that if I understand the doc properly, the kernel driver *must* set the V4L2_BUF_FLAG_LAST on the last buffer. With Venus the last buffer is signaled by the firmware with an empty buffer. That's not something we can change or predict earlier, so in order to respect the specification we need to return that empty buffer. After that DQBUF will behave as expected (returning -EPIPE), so GStreamer should be happy as well.

Quoted 7 lines
Quoted 5 lines

Without the proposed fix however, GStreamer would receive the last buffer with an incorrect size, and thus interpret random data as a frame.

So to me this fix seems to be both correct, and needed. Isn't it?

Totally, thanks for the extra clarification.

Awesome, thanks for confirming!

Stanimir, can we have your thoughts about this change?

#9 lore raw
In reply to Alexandre Courbot

Hi Alex,

Quoted 34 lines · Alexandre Courbot
Quoted 28 lines · Nicolas Dufresne
Quoted 10 lines · Alexandre Courbot
Quoted 9 lines · Nicolas Dufresne
Quoted 5 lines · Alexandre Courbot

The last buffer is often signaled by an empty buffer with the V4L2_BUF_FLAG_LAST flag set. Such buffers were returned with the bytesused field set to the full size of the OPB, which leads user-space to believe that the buffer actually contains useful data. Fix this by passing the number of bytes reported used by the firmware.

Quoted 6 lines
Quoted 1 line

That means the driver does not know on time which one is last. Why not just returned EPIPE to userspace on DQBUF and ovoid this useless roundtrip ?

Sorry, I don't understand what you mean. EPIPE is supposed to be returned after a buffer with V4L2_BUF_FLAG_LAST is made available for dequeue. This patch amends the code that prepares this LAST-flagged buffer. How could we avoid a roundtrip in this case?

Maybe it has changed, but when this was introduced, we found that some firmware (Exynos MFC) could not know which one is last. Instead, it gets an event saying there will be no more buffers.

Sending buffers with payload size to 0 just for the sake of setting the V4L2_BUF_FLAG_LAST was considered a waste. Specially that after that, every polls should return EPIPE. So in the end, we decided the it should just unblock the userspace and return EPIPE.

Quoted 4 lines

If you look at the related GStreamer code, it completely ignores the LAST flag. With fake buffer of size 0, userspace will endup dequeuing and throwing away. This is not useful to the process of terminating the decoding. To me, this LAST flag is not useful in this context.

Quoted 7 lines

Note that this patch does not interfere with DQBUF returning -EPIPE after the last buffer has been dequeued. It just fixes an invalid size that was returned for the last buffer.

Note also that if I understand the doc properly, the kernel driver *must* set the V4L2_BUF_FLAG_LAST on the last buffer. With Venus the last buffer is signaled by the firmware with an empty buffer. That's

Small correction, the firmware signals EoS with HFI_BUFFERFLAG_EOS flag with the returned buffer, then we set V4L2_BUF_FLAG_LAST.

Usually with v1 and v3 when HFI_BUFFERFLAG_EOS is set the bytesused is zero.

Quoted 14 lines

not something we can change or predict earlier, so in order to respect the specification we need to return that empty buffer. After that DQBUF will behave as expected (returning -EPIPE), so GStreamer should be happy as well.

Without the proposed fix however, GStreamer would receive the last buffer with an incorrect size, and thus interpret random data as a frame.

So to me this fix seems to be both correct, and needed. Isn't it?

Cheers,
Alex.

Signature · 2 lines
regards,
Stan
Acked-by on v1 1/1
#10 lore raw
In reply to Alexandre Courbot

Hi Alex,

Thanks for the patch!

Quoted 10 lines · Alexandre Courbot

The last buffer is often signaled by an empty buffer with the V4L2_BUF_FLAG_LAST flag set. Such buffers were returned with the bytesused field set to the full size of the OPB, which leads user-space to believe that the buffer actually contains useful data. Fix this by passing the number of bytes reported used by the firmware.

---
 drivers/media/platform/qcom/venus/vdec.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 18 lines
drivers/media/platform/qcom/venus/vdec.c +1 -3
index 189ec975c6bb..282de21cf2e1 100644
@@ -885,10 +885,8 @@ static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type,
 	vbuf->field = V4L2_FIELD_NONE;
 
 	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
-		unsigned int opb_sz = venus_helper_get_opb_size(inst);
-
 		vb = &vbuf->vb2_buf;
-		vb2_set_plane_payload(vb, 0, bytesused ? : opb_sz);
+		vb2_set_plane_payload(vb, 0, bytesused);
 		vb->planes[0].data_offset = data_offset;
 		vb->timestamp = timestamp_us * NSEC_PER_USEC;
 		vbuf->sequence = inst->sequence_cap++;
Signature · 2 lines
regards,
Stan