]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
media: core: Rework how create_buf index returned value is computed
authorBenjamin Gaignard <benjamin.gaignard@collabora.com>
Thu, 14 Mar 2024 15:32:21 +0000 (16:32 +0100)
committerHans Verkuil <hverkuil-cisco@xs4all.nl>
Mon, 25 Mar 2024 11:00:44 +0000 (12:00 +0100)
When REMOVE_BUFS will be introduced holes could created in bufs array.
To be able to reuse these unused indices reworking how create->index
is set is mandatory.
Let __vb2_queue_alloc() decide which first index is correct and
forward this to the caller.

Signed-off-by: Benjamin Gaignard <benjamin.gaignard@collabora.com>
Acked-by: Mauro Carvalho Chehab <mchehab@kernel.org>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
drivers/media/common/videobuf2/videobuf2-core.c
drivers/media/common/videobuf2/videobuf2-v4l2.c
include/media/videobuf2-core.h

index 58c495b253ce2965c21e7b8405d7396261fc9731..8e819d198c348adcd22e84c4484964249e69dba1 100644 (file)
@@ -442,12 +442,15 @@ static void vb2_queue_remove_buffer(struct vb2_buffer *vb)
  * __vb2_queue_alloc() - allocate vb2 buffer structures and (for MMAP type)
  * video buffer memory for all buffers/planes on the queue and initializes the
  * queue
+ * @first_index: index of the first created buffer, all newly allocated buffers
+ *              have indices in the range [first_index..first_index+count-1]
  *
  * Returns the number of buffers successfully allocated.
  */
 static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory,
                             unsigned int num_buffers, unsigned int num_planes,
-                            const unsigned plane_sizes[VB2_MAX_PLANES])
+                            const unsigned int plane_sizes[VB2_MAX_PLANES],
+                            unsigned int *first_index)
 {
        unsigned int q_num_buffers = vb2_get_num_buffers(q);
        unsigned int buffer, plane;
@@ -461,6 +464,8 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory,
        num_buffers = min_t(unsigned int, num_buffers,
                            q->max_num_buffers - q_num_buffers);
 
+       *first_index = q_num_buffers;
+
        for (buffer = 0; buffer < num_buffers; ++buffer) {
                /* Allocate vb2 buffer structures */
                vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
@@ -820,7 +825,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
        unsigned int q_num_bufs = vb2_get_num_buffers(q);
        unsigned plane_sizes[VB2_MAX_PLANES] = { };
        bool non_coherent_mem = flags & V4L2_MEMORY_FLAG_NON_COHERENT;
-       unsigned int i;
+       unsigned int i, first_index;
        int ret = 0;
 
        if (q->streaming) {
@@ -907,8 +912,10 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
 
        /* Finally, allocate buffers and video memory */
        allocated_buffers =
-               __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes);
+               __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes, &first_index);
        if (allocated_buffers == 0) {
+               /* There shouldn't be any buffers allocated, so first_index == 0 */
+               WARN_ON(first_index);
                dprintk(q, 1, "memory allocation failed\n");
                ret = -ENOMEM;
                goto error;
@@ -982,7 +989,8 @@ EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
 int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
                         unsigned int flags, unsigned int *count,
                         unsigned int requested_planes,
-                        const unsigned int requested_sizes[])
+                        const unsigned int requested_sizes[],
+                        unsigned int *first_index)
 {
        unsigned int num_planes = 0, num_buffers, allocated_buffers;
        unsigned plane_sizes[VB2_MAX_PLANES] = { };
@@ -1044,7 +1052,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
 
        /* Finally, allocate buffers and video memory */
        allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers,
-                               num_planes, plane_sizes);
+                               num_planes, plane_sizes, first_index);
        if (allocated_buffers == 0) {
                dprintk(q, 1, "memory allocation failed\n");
                ret = -ENOMEM;
index c575198e83547ab99719eca7e81dc6e7c0e601d4..03e8080a68a83337104c43cee7863ecd0d35af2b 100644 (file)
@@ -795,11 +795,15 @@ int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
        for (i = 0; i < requested_planes; i++)
                if (requested_sizes[i] == 0)
                        return -EINVAL;
-       return ret ? ret : vb2_core_create_bufs(q, create->memory,
-                                               create->flags,
-                                               &create->count,
-                                               requested_planes,
-                                               requested_sizes);
+       if (ret)
+               return ret;
+
+       return vb2_core_create_bufs(q, create->memory,
+                                   create->flags,
+                                   &create->count,
+                                   requested_planes,
+                                   requested_sizes,
+                                   &create->index);
 }
 EXPORT_SYMBOL_GPL(vb2_create_bufs);
 
index 4a8b9135cec834032b66dd7c889b1abd558808a2..42526e289c8e8eb74def7d7c0b462e8bfad6444e 100644 (file)
@@ -826,6 +826,8 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
  * @count: requested buffer count.
  * @requested_planes: number of planes requested.
  * @requested_sizes: array with the size of the planes.
+ * @first_index: index of the first created buffer, all allocated buffers have
+ *              indices in the range [first_index..first_index+count-1]
  *
  * Videobuf2 core helper to implement VIDIOC_CREATE_BUFS() operation. It is
  * called internally by VB2 by an API-specific handler, like
@@ -842,7 +844,8 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
 int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
                         unsigned int flags, unsigned int *count,
                         unsigned int requested_planes,
-                        const unsigned int requested_sizes[]);
+                        const unsigned int requested_sizes[],
+                        unsigned int *first_index);
 
 /**
  * vb2_core_prepare_buf() - Pass ownership of a buffer from userspace