From nobody Thu Nov 14 17:14:56 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36FC3EB64DB for ; Thu, 22 Jun 2023 13:14:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231272AbjFVNOK (ORCPT ); Thu, 22 Jun 2023 09:14:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231591AbjFVNOC (ORCPT ); Thu, 22 Jun 2023 09:14:02 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82FD01BD2; Thu, 22 Jun 2023 06:13:59 -0700 (PDT) Received: from benjamin-XPS-13-9310.. (unknown [IPv6:2a01:e0a:120:3210:7d72:676c:e745:a6ef]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: benjamin.gaignard) by madras.collabora.co.uk (Postfix) with ESMTPSA id C61286607119; Thu, 22 Jun 2023 14:13:57 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1687439638; bh=V+z/vGA0JHcb48G7cBo9k4XKRA8Ie7dXRj1eCbEXorE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XA5h3zyGUpFXNMTpDtJqv4HvAdhzZHSFQBun+ZcIeDuWI0X+ZTiOMVi1yzagwuOBK mImSHD6b4w2uyf3x3xIlpC8ZGdmUAIJlxLlUz2plO9fQNCyHtmhptRrulJ1GtUSibC X2pUXig0SooKWNoCp5+FQRNamobpfSCEjgJTY8OVZKB3FqWCfwHN+o6vqQgv7yrvAi 92rUcuzv5z+rzsomPwmX4BQPH7hByZVLfl7rrWI8fL3yOOxvpZG8RCekHyZayF2bt4 Y26PUqWU999RKrJPfm+BdNOEc31FZObZbJKhA8lR8XWJmg+cqT0iuFd35q2kaHnCUb P4eILdB6JRoFQ== From: Benjamin Gaignard To: mchehab@kernel.org, tfiga@chromium.org, m.szyprowski@samsung.com, ming.qian@nxp.com, ezequiel@vanguardiasur.com.ar, p.zabel@pengutronix.de, gregkh@linuxfoundation.org, hverkuil-cisco@xs4all.nl, nicolas.dufresne@collabora.com Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-arm-msm@vger.kernel.org, linux-rockchip@lists.infradead.org, linux-staging@lists.linux.dev, kernel@collabora.com, Benjamin Gaignard Subject: [PATCH v3 01/11] media: videobuf2: Access vb2_queue bufs array through helper functions Date: Thu, 22 Jun 2023 15:13:39 +0200 Message-Id: <20230622131349.144160-2-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230622131349.144160-1-benjamin.gaignard@collabora.com> References: <20230622131349.144160-1-benjamin.gaignard@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The first step before changing how vb2 buffers are stored into queue is to avoid direct access to bufs arrays. This patch adds 2 helpers functions to add and remove vb2 buffers from a queue. With these 2 and vb2_get_buffer(), bufs field of struct vb2_queue becomes like a private member of the structure. After each call to vb2_get_buffer() we need to be sure that we get a valid pointer so check the return value of all of them. Signed-off-by: Benjamin Gaignard --- .../media/common/videobuf2/videobuf2-core.c | 215 ++++++++++++++---- .../media/common/videobuf2/videobuf2-v4l2.c | 28 ++- drivers/media/platform/amphion/vpu_dbg.c | 22 +- .../platform/mediatek/jpeg/mtk_jpeg_core.c | 6 +- .../vcodec/vdec/vdec_vp9_req_lat_if.c | 2 +- drivers/media/platform/st/sti/hva/hva-v4l2.c | 4 + drivers/media/test-drivers/visl/visl-dec.c | 28 ++- .../staging/media/atomisp/pci/atomisp_ioctl.c | 2 +- 8 files changed, 240 insertions(+), 67 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/medi= a/common/videobuf2/videobuf2-core.c index cf6727d9c81f..42fd3984c2bc 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -359,8 +359,16 @@ static void __setup_offsets(struct vb2_buffer *vb) unsigned long off =3D 0; =20 if (vb->index) { - struct vb2_buffer *prev =3D q->bufs[vb->index - 1]; - struct vb2_plane *p =3D &prev->planes[prev->num_planes - 1]; + struct vb2_buffer *prev; + struct vb2_plane *p; + + prev =3D vb2_get_buffer(q, vb->index - 1); + if (!prev) { + dprintk(q, 1, "can't find the requested buffer\n"); + return; + } + + p =3D &prev->planes[prev->num_planes - 1]; =20 off =3D PAGE_ALIGN(p->m.offset + p->length); } @@ -397,6 +405,37 @@ static void init_buffer_cache_hints(struct vb2_queue *= q, struct vb2_buffer *vb) vb->skip_cache_sync_on_finish =3D 1; } =20 +/** + * vb2_queue_add_buffer() - add a buffer to a queue + * @q: pointer to &struct vb2_queue with videobuf2 queue. + * @vb: pointer to &struct vb2_buffer to be added to the queue. + * @index: index where add vb2_buffer in the queue + */ +static bool vb2_queue_add_buffer(struct vb2_queue *q, struct vb2_buffer *v= b, int index) +{ + if (index < VB2_MAX_FRAME && !q->bufs[index]) { + q->bufs[index] =3D vb; + vb->index =3D index; + vb->vb2_queue =3D q; + return true; + } + + return false; +} + +/** + * vb2_queue_remove_buffer() - remove a buffer from a queue + * @q: pointer to &struct vb2_queue with videobuf2 queue. + * @vb: pointer to &struct vb2_buffer to be removed from the queue. + */ +static void vb2_queue_remove_buffer(struct vb2_queue *q, struct vb2_buffer= *vb) +{ + if (vb->index < VB2_MAX_FRAME) { + q->bufs[vb->index] =3D NULL; + vb->vb2_queue =3D NULL; + } +} + /* * __vb2_queue_alloc() - allocate vb2 buffer structures and (for MMAP type) * video buffer memory for all buffers/planes on the queue and initializes= the @@ -425,9 +464,7 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum = vb2_memory memory, } =20 vb->state =3D VB2_BUF_STATE_DEQUEUED; - vb->vb2_queue =3D q; vb->num_planes =3D num_planes; - vb->index =3D q->num_buffers + buffer; vb->type =3D q->type; vb->memory =3D memory; init_buffer_cache_hints(q, vb); @@ -437,7 +474,11 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum= vb2_memory memory, } call_void_bufop(q, init_buffer, vb); =20 - q->bufs[vb->index] =3D vb; + if (!vb2_queue_add_buffer(q, vb, q->num_buffers + buffer)) { + dprintk(q, 1, "failed adding buffer %d to queue\n", buffer); + kfree(vb); + break; + } =20 /* Allocate video buffer memory for the MMAP type */ if (memory =3D=3D VB2_MEMORY_MMAP) { @@ -445,7 +486,7 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum = vb2_memory memory, if (ret) { dprintk(q, 1, "failed allocating memory for buffer %d\n", buffer); - q->bufs[vb->index] =3D NULL; + vb2_queue_remove_buffer(q, vb); kfree(vb); break; } @@ -460,7 +501,7 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum = vb2_memory memory, dprintk(q, 1, "buffer %d %p initialization failed\n", buffer, vb); __vb2_buf_mem_free(vb); - q->bufs[vb->index] =3D NULL; + vb2_queue_remove_buffer(q, vb); kfree(vb); break; } @@ -483,7 +524,7 @@ static void __vb2_free_mem(struct vb2_queue *q, unsigne= d int buffers) =20 for (buffer =3D q->num_buffers - buffers; buffer < q->num_buffers; ++buffer) { - vb =3D q->bufs[buffer]; + vb =3D vb2_get_buffer(q, buffer); if (!vb) continue; =20 @@ -511,7 +552,7 @@ static void __vb2_queue_free(struct vb2_queue *q, unsig= ned int buffers) /* Call driver-provided cleanup function for each buffer, if provided */ for (buffer =3D q->num_buffers - buffers; buffer < q->num_buffers; ++buffer) { - struct vb2_buffer *vb =3D q->bufs[buffer]; + struct vb2_buffer *vb =3D vb2_get_buffer(q, buffer); =20 if (vb && vb->planes[0].mem_priv) call_void_vb_qop(vb, buf_cleanup, vb); @@ -551,15 +592,20 @@ static void __vb2_queue_free(struct vb2_queue *q, uns= igned int buffers) q->cnt_unprepare_streaming =3D 0; } for (buffer =3D 0; buffer < q->num_buffers; ++buffer) { - struct vb2_buffer *vb =3D q->bufs[buffer]; - bool unbalanced =3D vb->cnt_mem_alloc !=3D vb->cnt_mem_put || - vb->cnt_mem_prepare !=3D vb->cnt_mem_finish || - vb->cnt_mem_get_userptr !=3D vb->cnt_mem_put_userptr || - vb->cnt_mem_attach_dmabuf !=3D vb->cnt_mem_detach_dmabuf || - vb->cnt_mem_map_dmabuf !=3D vb->cnt_mem_unmap_dmabuf || - vb->cnt_buf_queue !=3D vb->cnt_buf_done || - vb->cnt_buf_prepare !=3D vb->cnt_buf_finish || - vb->cnt_buf_init !=3D vb->cnt_buf_cleanup; + struct vb2_buffer *vb =3D vb2_get_buffer(q, buffer); + bool unbalanced; + + if (!vb) + continue; + + unbalanced =3D vb->cnt_mem_alloc !=3D vb->cnt_mem_put || + vb->cnt_mem_prepare !=3D vb->cnt_mem_finish || + vb->cnt_mem_get_userptr !=3D vb->cnt_mem_put_userptr || + vb->cnt_mem_attach_dmabuf !=3D vb->cnt_mem_detach_dmabuf || + vb->cnt_mem_map_dmabuf !=3D vb->cnt_mem_unmap_dmabuf || + vb->cnt_buf_queue !=3D vb->cnt_buf_done || + vb->cnt_buf_prepare !=3D vb->cnt_buf_finish || + vb->cnt_buf_init !=3D vb->cnt_buf_cleanup; =20 if (unbalanced || debug) { pr_info(" counters for queue %p, buffer %d:%s\n", @@ -591,8 +637,13 @@ static void __vb2_queue_free(struct vb2_queue *q, unsi= gned int buffers) /* Free vb2 buffers */ for (buffer =3D q->num_buffers - buffers; buffer < q->num_buffers; ++buffer) { - kfree(q->bufs[buffer]); - q->bufs[buffer] =3D NULL; + struct vb2_buffer *vb =3D vb2_get_buffer(q, buffer); + + if (!vb) + continue; + + vb2_queue_remove_buffer(q, vb); + kfree(vb); } =20 q->num_buffers -=3D buffers; @@ -628,7 +679,12 @@ static bool __buffers_in_use(struct vb2_queue *q) { unsigned int buffer; for (buffer =3D 0; buffer < q->num_buffers; ++buffer) { - if (vb2_buffer_in_use(q, q->bufs[buffer])) + struct vb2_buffer *vb =3D vb2_get_buffer(q, buffer); + + if (!vb) + continue; + + if (vb2_buffer_in_use(q, vb)) return true; } return false; @@ -636,7 +692,10 @@ static bool __buffers_in_use(struct vb2_queue *q) =20 void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb) { - call_void_bufop(q, fill_user_buffer, q->bufs[index], pb); + struct vb2_buffer *vb =3D vb2_get_buffer(q, index); + + if (vb) + call_void_bufop(q, fill_user_buffer, vb, pb); } EXPORT_SYMBOL_GPL(vb2_core_querybuf); =20 @@ -1547,7 +1606,13 @@ int vb2_core_prepare_buf(struct vb2_queue *q, unsign= ed int index, void *pb) struct vb2_buffer *vb; int ret; =20 - vb =3D q->bufs[index]; + vb =3D vb2_get_buffer(q, index); + + if (!vb) { + dprintk(q, 1, "can't find the requested buffer\n"); + return -EINVAL; + } + if (vb->state !=3D VB2_BUF_STATE_DEQUEUED) { dprintk(q, 1, "invalid buffer state %s\n", vb2_state_name(vb->state)); @@ -1618,7 +1683,11 @@ static int vb2_start_streaming(struct vb2_queue *q) * correctly return them to vb2. */ for (i =3D 0; i < q->num_buffers; ++i) { - vb =3D q->bufs[i]; + vb =3D vb2_get_buffer(q, i); + + if (!vb) + continue; + if (vb->state =3D=3D VB2_BUF_STATE_ACTIVE) vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED); } @@ -1646,7 +1715,12 @@ int vb2_core_qbuf(struct vb2_queue *q, unsigned int = index, void *pb, return -EIO; } =20 - vb =3D q->bufs[index]; + vb =3D vb2_get_buffer(q, index); + + if (!vb) { + dprintk(q, 1, "can't find the requested buffer\n"); + return -EINVAL; + } =20 if (!req && vb->state !=3D VB2_BUF_STATE_IN_REQUEST && q->requires_requests) { @@ -2022,12 +2096,18 @@ static void __vb2_queue_cancel(struct vb2_queue *q) * to vb2 in stop_streaming(). */ if (WARN_ON(atomic_read(&q->owned_by_drv_count))) { - for (i =3D 0; i < q->num_buffers; ++i) - if (q->bufs[i]->state =3D=3D VB2_BUF_STATE_ACTIVE) { + for (i =3D 0; i < q->num_buffers; ++i) { + struct vb2_buffer *vb =3D vb2_get_buffer(q, i); + + if (!vb) + continue; + + if (vb->state =3D=3D VB2_BUF_STATE_ACTIVE) { pr_warn("driver bug: stop_streaming operation is leaving buf %p in act= ive state\n", - q->bufs[i]); - vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR); + vb); + vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); } + } /* Must be zero now */ WARN_ON(atomic_read(&q->owned_by_drv_count)); } @@ -2061,9 +2141,14 @@ static void __vb2_queue_cancel(struct vb2_queue *q) * be changed, so we can't move the buf_finish() to __vb2_dqbuf(). */ for (i =3D 0; i < q->num_buffers; ++i) { - struct vb2_buffer *vb =3D q->bufs[i]; - struct media_request *req =3D vb->req_obj.req; + struct vb2_buffer *vb; + struct media_request *req; =20 + vb =3D vb2_get_buffer(q, i); + if (!vb) + continue; + + req =3D vb->req_obj.req; /* * If a request is associated with this buffer, then * call buf_request_cancel() to give the driver to complete() @@ -2215,7 +2300,10 @@ static int __find_plane_by_offset(struct vb2_queue *= q, unsigned long off, * return its buffer and plane numbers. */ for (buffer =3D 0; buffer < q->num_buffers; ++buffer) { - vb =3D q->bufs[buffer]; + vb =3D vb2_get_buffer(q, buffer); + + if (!vb) + continue; =20 for (plane =3D 0; plane < vb->num_planes; ++plane) { if (vb->planes[plane].m.offset =3D=3D off) { @@ -2262,7 +2350,12 @@ int vb2_core_expbuf(struct vb2_queue *q, int *fd, un= signed int type, return -EINVAL; } =20 - vb =3D q->bufs[index]; + vb =3D vb2_get_buffer(q, index); + + if (!vb) { + dprintk(q, 1, "can't find the requested buffer\n"); + return -EINVAL; + } =20 if (plane >=3D vb->num_planes) { dprintk(q, 1, "buffer plane out of range\n"); @@ -2339,7 +2432,13 @@ int vb2_mmap(struct vb2_queue *q, struct vm_area_str= uct *vma) if (ret) goto unlock; =20 - vb =3D q->bufs[buffer]; + vb =3D vb2_get_buffer(q, buffer); + + if (!vb) { + dprintk(q, 1, "can't find the requested buffer\n"); + ret =3D -EINVAL; + goto unlock; + } =20 /* * MMAP requires page_aligned buffers. @@ -2396,7 +2495,12 @@ unsigned long vb2_get_unmapped_area(struct vb2_queue= *q, if (ret) goto unlock; =20 - vb =3D q->bufs[buffer]; + vb =3D vb2_get_buffer(q, buffer); + if (!vb) { + dprintk(q, 1, "can't find the requested buffer\n"); + ret =3D -EINVAL; + goto unlock; + } =20 vaddr =3D vb2_plane_vaddr(vb, plane); mutex_unlock(&q->mmap_lock); @@ -2625,6 +2729,7 @@ struct vb2_fileio_data { static int __vb2_init_fileio(struct vb2_queue *q, int read) { struct vb2_fileio_data *fileio; + struct vb2_buffer *vb; int i, ret; unsigned int count =3D 0; =20 @@ -2679,7 +2784,13 @@ static int __vb2_init_fileio(struct vb2_queue *q, in= t read) * Check if plane_count is correct * (multiplane buffers are not supported). */ - if (q->bufs[0]->num_planes !=3D 1) { + vb =3D vb2_get_buffer(q, 0); + if (!vb) { + ret =3D -EBUSY; + goto err_reqbufs; + } + + if (vb->num_planes !=3D 1) { ret =3D -EBUSY; goto err_reqbufs; } @@ -2688,12 +2799,17 @@ static int __vb2_init_fileio(struct vb2_queue *q, i= nt read) * Get kernel address of each buffer. */ for (i =3D 0; i < q->num_buffers; i++) { - fileio->bufs[i].vaddr =3D vb2_plane_vaddr(q->bufs[i], 0); + vb =3D vb2_get_buffer(q, i); + + if (!vb) + continue; + + fileio->bufs[i].vaddr =3D vb2_plane_vaddr(vb, 0); if (fileio->bufs[i].vaddr =3D=3D NULL) { ret =3D -EINVAL; goto err_reqbufs; } - fileio->bufs[i].size =3D vb2_plane_size(q->bufs[i], 0); + fileio->bufs[i].size =3D vb2_plane_size(vb, 0); } =20 /* @@ -2821,15 +2937,18 @@ static size_t __vb2_perform_fileio(struct vb2_queue= *q, char __user *data, size_ =20 fileio->cur_index =3D index; buf =3D &fileio->bufs[index]; - b =3D q->bufs[index]; + b =3D vb2_get_buffer(q, index); + + if (!b) + return -EINVAL; =20 /* * Get number of bytes filled by the driver */ buf->pos =3D 0; buf->queued =3D 0; - buf->size =3D read ? vb2_get_plane_payload(q->bufs[index], 0) - : vb2_plane_size(q->bufs[index], 0); + buf->size =3D read ? vb2_get_plane_payload(b, 0) + : vb2_plane_size(b, 0); /* Compensate for data_offset on read in the multiplanar case. */ if (is_multiplanar && read && b->planes[0].data_offset < buf->size) { @@ -2872,8 +2991,12 @@ static size_t __vb2_perform_fileio(struct vb2_queue = *q, char __user *data, size_ * Queue next buffer if required. */ if (buf->pos =3D=3D buf->size || (!read && fileio->write_immediately)) { - struct vb2_buffer *b =3D q->bufs[index]; + struct vb2_buffer *b =3D vb2_get_buffer(q, index); =20 + if (!b) { + dprintk(q, 1, "can't find the requested buffer\n"); + return -EINVAL; + } /* * Check if this is the last buffer to read. */ @@ -2899,7 +3022,7 @@ static size_t __vb2_perform_fileio(struct vb2_queue *= q, char __user *data, size_ */ buf->pos =3D 0; buf->queued =3D 1; - buf->size =3D vb2_plane_size(q->bufs[index], 0); + buf->size =3D vb2_plane_size(b, 0); fileio->q_count +=3D 1; /* * If we are queuing up buffers for the first time, then @@ -2970,7 +3093,9 @@ static int vb2_thread(void *data) * Call vb2_dqbuf to get buffer back. */ if (prequeue) { - vb =3D q->bufs[index++]; + vb =3D vb2_get_buffer(q, index++); + if (!vb) + continue; prequeue--; } else { call_void_qop(q, wait_finish, q); @@ -2979,7 +3104,7 @@ static int vb2_thread(void *data) call_void_qop(q, wait_prepare, q); dprintk(q, 5, "file io: vb2_dqbuf result: %d\n", ret); if (!ret) - vb =3D q->bufs[index]; + vb =3D vb2_get_buffer(q, index); } if (ret || threadio->stop) break; diff --git a/drivers/media/common/videobuf2/videobuf2-v4l2.c b/drivers/medi= a/common/videobuf2/videobuf2-v4l2.c index c7a54d82a55e..724135d41f7f 100644 --- a/drivers/media/common/videobuf2/videobuf2-v4l2.c +++ b/drivers/media/common/videobuf2/videobuf2-v4l2.c @@ -383,8 +383,7 @@ static int vb2_queue_or_prepare_buf(struct vb2_queue *q= , struct media_device *md return -EINVAL; } =20 - if (q->bufs[b->index] =3D=3D NULL) { - /* Should never happen */ + if (!vb2_get_buffer(q, b->index)) { dprintk(q, 1, "%s: buffer is NULL\n", opname); return -EINVAL; } @@ -394,7 +393,7 @@ static int vb2_queue_or_prepare_buf(struct vb2_queue *q= , struct media_device *md return -EINVAL; } =20 - vb =3D q->bufs[b->index]; + vb =3D vb2_get_buffer(q, b->index); vbuf =3D to_vb2_v4l2_buffer(vb); ret =3D __verify_planes_array(vb, b); if (ret) @@ -628,11 +627,18 @@ static const struct vb2_buf_ops v4l2_buf_ops =3D { struct vb2_buffer *vb2_find_buffer(struct vb2_queue *q, u64 timestamp) { unsigned int i; + struct vb2_buffer *vb2; =20 - for (i =3D 0; i < q->num_buffers; i++) - if (q->bufs[i]->copied_timestamp && - q->bufs[i]->timestamp =3D=3D timestamp) - return vb2_get_buffer(q, i); + for (i =3D 0; i < q->num_buffers; i++) { + vb2 =3D vb2_get_buffer(q, i); + + if (!vb2) + continue; + + if (vb2->copied_timestamp && + vb2->timestamp =3D=3D timestamp) + return vb2; + } return NULL; } EXPORT_SYMBOL_GPL(vb2_find_buffer); @@ -664,7 +670,13 @@ int vb2_querybuf(struct vb2_queue *q, struct v4l2_buff= er *b) dprintk(q, 1, "buffer index out of range\n"); return -EINVAL; } - vb =3D q->bufs[b->index]; + vb =3D vb2_get_buffer(q, b->index); + + if (!vb) { + dprintk(q, 1, "can't find the requested buffer\n"); + return -EINVAL; + } + ret =3D __verify_planes_array(vb, b); if (!ret) vb2_core_querybuf(q, b->index, b); diff --git a/drivers/media/platform/amphion/vpu_dbg.c b/drivers/media/platf= orm/amphion/vpu_dbg.c index 44b830ae01d8..e2c371c2fde5 100644 --- a/drivers/media/platform/amphion/vpu_dbg.c +++ b/drivers/media/platform/amphion/vpu_dbg.c @@ -133,11 +133,18 @@ static int vpu_dbg_instance(struct seq_file *s, void = *data) =20 vq =3D v4l2_m2m_get_src_vq(inst->fh.m2m_ctx); for (i =3D 0; i < vq->num_buffers; i++) { - struct vb2_buffer *vb =3D vq->bufs[i]; - struct vb2_v4l2_buffer *vbuf =3D to_vb2_v4l2_buffer(vb); + struct vb2_buffer *vb; + struct vb2_v4l2_buffer *vbuf; + + vb =3D vb2_get_buffer(vq, i); + if (!vb) + continue; =20 if (vb->state =3D=3D VB2_BUF_STATE_DEQUEUED) continue; + + vbuf =3D to_vb2_v4l2_buffer(vb); + num =3D scnprintf(str, sizeof(str), "output [%2d] state =3D %10s, %8s\n", i, vb2_stat_name[vb->state], @@ -148,11 +155,18 @@ static int vpu_dbg_instance(struct seq_file *s, void = *data) =20 vq =3D v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx); for (i =3D 0; i < vq->num_buffers; i++) { - struct vb2_buffer *vb =3D vq->bufs[i]; - struct vb2_v4l2_buffer *vbuf =3D to_vb2_v4l2_buffer(vb); + struct vb2_buffer *vb; + struct vb2_v4l2_buffer *vbuf; + + vb =3D vb2_get_buffer(vq, i); + if (!vb) + continue; =20 if (vb->state =3D=3D VB2_BUF_STATE_DEQUEUED) continue; + + vbuf =3D to_vb2_v4l2_buffer(vb); + num =3D scnprintf(str, sizeof(str), "capture[%2d] state =3D %10s, %8s\n", i, vb2_stat_name[vb->state], diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c b/drivers= /media/platform/mediatek/jpeg/mtk_jpeg_core.c index 0051f372a66c..ea37069ba355 100644 --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c @@ -603,7 +603,11 @@ static int mtk_jpeg_qbuf(struct file *file, void *priv= , struct v4l2_buffer *buf) return -EINVAL; } =20 - vb =3D vq->bufs[buf->index]; + vb =3D vb2_get_buffer(vq, buf->index); + if (!vb) { + dev_err(ctx->jpeg->dev, "buffer not found\n"); + return -EINVAL; + } jpeg_src_buf =3D mtk_jpeg_vb2_to_srcbuf(vb); jpeg_src_buf->bs_size =3D buf->m.planes[0].bytesused; =20 diff --git a/drivers/media/platform/mediatek/vcodec/vdec/vdec_vp9_req_lat_i= f.c b/drivers/media/platform/mediatek/vcodec/vdec/vdec_vp9_req_lat_if.c index cf16cf2807f0..6532a69f1fa8 100644 --- a/drivers/media/platform/mediatek/vcodec/vdec/vdec_vp9_req_lat_if.c +++ b/drivers/media/platform/mediatek/vcodec/vdec/vdec_vp9_req_lat_if.c @@ -1701,7 +1701,7 @@ static int vdec_vp9_slice_setup_core_buffer(struct vd= ec_vp9_slice_instance *inst =20 /* update internal buffer's width/height */ for (i =3D 0; i < vq->num_buffers; i++) { - if (vb =3D=3D vq->bufs[i]) { + if (vb =3D=3D vb2_get_buffer(vq, i)) { instance->dpb[i].width =3D w; instance->dpb[i].height =3D h; break; diff --git a/drivers/media/platform/st/sti/hva/hva-v4l2.c b/drivers/media/p= latform/st/sti/hva/hva-v4l2.c index 3a848ca32a0e..326be09bdb55 100644 --- a/drivers/media/platform/st/sti/hva/hva-v4l2.c +++ b/drivers/media/platform/st/sti/hva/hva-v4l2.c @@ -577,6 +577,10 @@ static int hva_qbuf(struct file *file, void *priv, str= uct v4l2_buffer *buf) } =20 vb2_buf =3D vb2_get_buffer(vq, buf->index); + if (!vb2_buf) { + dev_dbg(dev, "%s buffer index %d not found\n", ctx->name, buf->index); + return -EINVAL; + } stream =3D to_hva_stream(to_vb2_v4l2_buffer(vb2_buf)); stream->bytesused =3D buf->bytesused; } diff --git a/drivers/media/test-drivers/visl/visl-dec.c b/drivers/media/tes= t-drivers/visl/visl-dec.c index 318d675e5668..ba20ea998d19 100644 --- a/drivers/media/test-drivers/visl/visl-dec.c +++ b/drivers/media/test-drivers/visl/visl-dec.c @@ -290,13 +290,20 @@ static void visl_tpg_fill(struct visl_ctx *ctx, struc= t visl_run *run) for (i =3D 0; i < out_q->num_buffers; i++) { char entry[] =3D "index: %u, state: %s, request_fd: %d, "; u32 old_len =3D len; - char *q_status =3D visl_get_vb2_state(out_q->bufs[i]->state); + struct vb2_buffer *vb2; + char *q_status; + + vb2 =3D vb2_get_buffer(out_q, i); + if (!vb2) + continue; + + q_status =3D visl_get_vb2_state(vb2->state); =20 len +=3D scnprintf(&buf[len], TPG_STR_BUF_SZ - len, entry, i, q_status, - to_vb2_v4l2_buffer(out_q->bufs[i])->request_fd); + to_vb2_v4l2_buffer(vb2)->request_fd); =20 - len +=3D visl_fill_bytesused(to_vb2_v4l2_buffer(out_q->bufs[i]), + len +=3D visl_fill_bytesused(to_vb2_v4l2_buffer(vb2), &buf[len], TPG_STR_BUF_SZ - len); =20 @@ -342,13 +349,20 @@ static void visl_tpg_fill(struct visl_ctx *ctx, struc= t visl_run *run) len =3D 0; for (i =3D 0; i < cap_q->num_buffers; i++) { u32 old_len =3D len; - char *q_status =3D visl_get_vb2_state(cap_q->bufs[i]->state); + struct vb2_buffer *vb2; + char *q_status; + + vb2 =3D vb2_get_buffer(cap_q, i); + if (!vb2) + continue; + + q_status =3D visl_get_vb2_state(vb2->state); =20 len +=3D scnprintf(&buf[len], TPG_STR_BUF_SZ - len, "index: %u, status: %s, timestamp: %llu, is_held: %d", - cap_q->bufs[i]->index, q_status, - cap_q->bufs[i]->timestamp, - to_vb2_v4l2_buffer(cap_q->bufs[i])->is_held); + vb2->index, q_status, + vb2->timestamp, + to_vb2_v4l2_buffer(vb2)->is_held); =20 tpg_gen_text(&ctx->tpg, basep, line++ * line_height, 16, &buf[old_len]); frame_dprintk(ctx->dev, run->dst->sequence, "%s", &buf[old_len]); diff --git a/drivers/staging/media/atomisp/pci/atomisp_ioctl.c b/drivers/st= aging/media/atomisp/pci/atomisp_ioctl.c index 384f31fc66c5..ec7e05263c8e 100644 --- a/drivers/staging/media/atomisp/pci/atomisp_ioctl.c +++ b/drivers/staging/media/atomisp/pci/atomisp_ioctl.c @@ -1077,7 +1077,7 @@ static int atomisp_dqbuf_wrapper(struct file *file, v= oid *fh, struct v4l2_buffer if (ret) return ret; =20 - vb =3D pipe->vb_queue.bufs[buf->index]; + vb =3D vb2_get_buffer(pipe->vb_queue, buf->index); frame =3D vb_to_frame(vb); =20 buf->reserved =3D asd->frame_status[buf->index]; --=20 2.39.2