media: uvcvideo: Protect queue internals with helper
authorKieran Bingham <kieran.bingham@ideasonboard.com>
Thu, 23 Mar 2017 12:25:14 +0000 (08:25 -0400)
committerMauro Carvalho Chehab <mchehab+samsung@kernel.org>
Wed, 5 Dec 2018 08:10:06 +0000 (03:10 -0500)
The URB completion operation obtains the current buffer by reading
directly into the queue internal interface.

Protect this queue abstraction by providing a helper
uvc_queue_get_current_buffer() which can be used by both the decode
task, and the uvc_queue_next_buffer() functions.

Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
drivers/media/usb/uvc/uvc_queue.c
drivers/media/usb/uvc/uvc_video.c
drivers/media/usb/uvc/uvcvideo.h

index 8964e16f2b22d4ae3388dfaabfdec378c29ad65e..74f9483911d09c5194f9c2196febfafa8d811b97 100644 (file)
@@ -430,6 +430,33 @@ void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
        spin_unlock_irqrestore(&queue->irqlock, flags);
 }
 
+/*
+ * uvc_queue_get_current_buffer: Obtain the current working output buffer
+ *
+ * Buffers may span multiple packets, and even URBs, therefore the active buffer
+ * remains on the queue until the EOF marker.
+ */
+static struct uvc_buffer *
+__uvc_queue_get_current_buffer(struct uvc_video_queue *queue)
+{
+       if (list_empty(&queue->irqqueue))
+               return NULL;
+
+       return list_first_entry(&queue->irqqueue, struct uvc_buffer, queue);
+}
+
+struct uvc_buffer *uvc_queue_get_current_buffer(struct uvc_video_queue *queue)
+{
+       struct uvc_buffer *nextbuf;
+       unsigned long flags;
+
+       spin_lock_irqsave(&queue->irqlock, flags);
+       nextbuf = __uvc_queue_get_current_buffer(queue);
+       spin_unlock_irqrestore(&queue->irqlock, flags);
+
+       return nextbuf;
+}
+
 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
                struct uvc_buffer *buf)
 {
@@ -446,11 +473,7 @@ struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
 
        spin_lock_irqsave(&queue->irqlock, flags);
        list_del(&buf->queue);
-       if (!list_empty(&queue->irqqueue))
-               nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
-                                          queue);
-       else
-               nextbuf = NULL;
+       nextbuf = __uvc_queue_get_current_buffer(queue);
        spin_unlock_irqrestore(&queue->irqlock, flags);
 
        buf->state = buf->error ? UVC_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
index 6d4384695964a578f0c6e6018e686693e187e5d8..7a7779e1b466e0cee43395cfd8ec9149398719e8 100644 (file)
@@ -1484,11 +1484,7 @@ static void uvc_video_complete(struct urb *urb)
                return;
        }
 
-       spin_lock_irqsave(&queue->irqlock, flags);
-       if (!list_empty(&queue->irqqueue))
-               buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
-                                      queue);
-       spin_unlock_irqrestore(&queue->irqlock, flags);
+       buf = uvc_queue_get_current_buffer(queue);
 
        if (vb2_qmeta) {
                spin_lock_irqsave(&qmeta->irqlock, flags);
index 35ba487692443db5f13ea6228a3ace79f671923a..f6109a4dde5bbf3c22eac805a365e8f862a73c93 100644 (file)
@@ -728,6 +728,7 @@ int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type);
 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect);
 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
                                         struct uvc_buffer *buf);
+struct uvc_buffer *uvc_queue_get_current_buffer(struct uvc_video_queue *queue);
 int uvc_queue_mmap(struct uvc_video_queue *queue,
                   struct vm_area_struct *vma);
 __poll_t uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,