All of lore.kernel.org
 help / color / mirror / Atom feed
* What should CREATE_BUFS do if count == 0?
@ 2012-06-06 22:09 Hans Verkuil
  2012-06-07  9:59 ` Guennadi Liakhovetski
  0 siblings, 1 reply; 2+ messages in thread
From: Hans Verkuil @ 2012-06-06 22:09 UTC (permalink / raw)
  To: linux-media; +Cc: Guennadi Liakhovetski, Pawel Osciak

Hi all,

I'm extending v4l2-compliance with support for VIDIOC_REQBUFS and VIDIOC_CREATE_BUFS,
and I ran into an undefined issue: what happens if VIDIOC_CREATE_BUFS is called with
count set to 0?

I think there should be a separate test for that. Right now queue_setup will receive
a request for 0 buffers, and I don't know if drivers expect a zero value there.

I suggest that CREATE_BUFS with a count of 0 will only check whether memory and
format.type are valid, and if they are it will just return 0 and do nothing.

Also note that this code in vb2_create_bufs is wrong:

        ret = __vb2_queue_alloc(q, create->memory, num_buffers,
                                num_planes);
        if (ret < 0) {
                dprintk(1, "Memory allocation failed with error: %d\n", ret);
                return ret;
        }

It should be:

		if (ret == 0) {

__vb2_queue_alloc() returns the number of buffers it managed to allocate,
which is never < 0.

I propose to add the patch included below.

Comments are welcome!

Regards,

	Hans

diff --git a/drivers/media/video/videobuf2-core.c b/drivers/media/video/videobuf2-core.c
index a0702fd..01a8312 100644
--- a/drivers/media/video/videobuf2-core.c
+++ b/drivers/media/video/videobuf2-core.c
@@ -647,6 +647,9 @@ int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
 		return -EINVAL;
 	}
 
+	if (create->count == 0)
+		return 0;
+
 	if (q->num_buffers == VIDEO_MAX_FRAME) {
 		dprintk(1, "%s(): maximum number of buffers already allocated\n",
 			__func__);
@@ -675,7 +678,7 @@ int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
 	/* Finally, allocate buffers and video memory */
 	ret = __vb2_queue_alloc(q, create->memory, num_buffers,
 				num_planes);
-	if (ret < 0) {
+	if (ret == 0) {
 		dprintk(1, "Memory allocation failed with error: %d\n", ret);
 		return ret;
 	}

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: What should CREATE_BUFS do if count == 0?
  2012-06-06 22:09 What should CREATE_BUFS do if count == 0? Hans Verkuil
@ 2012-06-07  9:59 ` Guennadi Liakhovetski
  0 siblings, 0 replies; 2+ messages in thread
From: Guennadi Liakhovetski @ 2012-06-07  9:59 UTC (permalink / raw)
  To: Hans Verkuil; +Cc: linux-media, Pawel Osciak

Hi Hans

On Thu, 7 Jun 2012, Hans Verkuil wrote:

> Hi all,
> 
> I'm extending v4l2-compliance with support for VIDIOC_REQBUFS and VIDIOC_CREATE_BUFS,
> and I ran into an undefined issue: what happens if VIDIOC_CREATE_BUFS is called with
> count set to 0?
> 
> I think there should be a separate test for that. Right now queue_setup will receive
> a request for 0 buffers, and I don't know if drivers expect a zero value there.
> 
> I suggest that CREATE_BUFS with a count of 0 will only check whether memory and
> format.type are valid, and if they are it will just return 0 and do nothing.

Sounds good to me.

> Also note that this code in vb2_create_bufs is wrong:
> 
>         ret = __vb2_queue_alloc(q, create->memory, num_buffers,
>                                 num_planes);
>         if (ret < 0) {
>                 dprintk(1, "Memory allocation failed with error: %d\n", ret);
>                 return ret;
>         }
> 
> It should be:
> 
> 		if (ret == 0) {

Indeed you're right. But then the return above should be "return -ENOMEM," 
shouldn't it?

> 
> __vb2_queue_alloc() returns the number of buffers it managed to allocate,
> which is never < 0.
> 
> I propose to add the patch included below.
> 
> Comments are welcome!
> 
> Regards,
> 
> 	Hans
> 
> diff --git a/drivers/media/video/videobuf2-core.c b/drivers/media/video/videobuf2-core.c
> index a0702fd..01a8312 100644
> --- a/drivers/media/video/videobuf2-core.c
> +++ b/drivers/media/video/videobuf2-core.c
> @@ -647,6 +647,9 @@ int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
>  		return -EINVAL;
>  	}
>  
> +	if (create->count == 0)
> +		return 0;
> +
>  	if (q->num_buffers == VIDEO_MAX_FRAME) {
>  		dprintk(1, "%s(): maximum number of buffers already allocated\n",
>  			__func__);
> @@ -675,7 +678,7 @@ int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
>  	/* Finally, allocate buffers and video memory */
>  	ret = __vb2_queue_alloc(q, create->memory, num_buffers,
>  				num_planes);
> -	if (ret < 0) {
> +	if (ret == 0) {
>  		dprintk(1, "Memory allocation failed with error: %d\n", ret);
>  		return ret;

See above, should return an error code here.

>  	}
> 

Thanks
Guennadi
---
Guennadi Liakhovetski, Ph.D.
Freelance Open-Source Software Developer
http://www.open-technology.de/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-06-07 10:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-06 22:09 What should CREATE_BUFS do if count == 0? Hans Verkuil
2012-06-07  9:59 ` Guennadi Liakhovetski

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.