From patchwork Thu Jun 22 13:13:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 111661 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp5062031vqr; Thu, 22 Jun 2023 06:17:53 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7ZAGq2isNNWHumnbRukoqHtinWINsA6e/MwixUNfVDgWSxYchPW+3BxVFvM9Tz7xMPOqAH X-Received: by 2002:a17:90a:ead2:b0:25e:b066:8084 with SMTP id ev18-20020a17090aead200b0025eb0668084mr11684785pjb.18.1687439873262; Thu, 22 Jun 2023 06:17:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687439873; cv=none; d=google.com; s=arc-20160816; b=tZAOQlX7B4or3Y0nYs72PdGUKcWGwYQ5LxVUsdMD6t1mcOGI5J2xtM6r7losAp1SvH jxMcl1N2zlw+vuby2TclvHc1MpNTZNVKovmvU/vl6kDmVd9jpj0iKgO3+odzTa7Au+pO yc4bZ52jc18izBTxueRqjhycPkyxybC5A9my1oYUtahgHXVgwuDkMmjqg2jAx/y/e3bV D47HQb00ezlWShZMjYcFmdo0Ren7tmlrXMyb+bkvEABVnX7C0M8MMesv16/iYrZcNsgc 5loys9DqxLBEVwqxftuf0IeEloF03RcJgJr64UcbYqX17YTekoa7sKvBGg2Ll2KibA53 ihvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tUqmDmDOiKsYiM3yp3gWwrv6+DRsJqG/khjYHHKOPZA=; b=diE1g74ap1v2kU6+aETg6nb3KQk6aFoL8IVEfoe2N6Xy9bPeRBOx74NTjmQI+5lkkF fx1bmTCGq5jbkGSGOd6McBXPcgQoLo0ZKoO9Vt1b5JHdJo+Whw2MzyQCouUnTTUcuKT0 yKnVo1VxJiF5b2yrq0wVhhAJfilkxdtRUE1lX5xg6kFxHmSlfQX7k8d8u2ZvpNHb0mgQ TRN9GaoPfJug4KhToFKfnjvtkV3+0uSKtaxFKHuuxdT2RSOQAQSTnvGbLbIcmGvA3us5 FmcnYlw4qEtfY5lOwcrkl48r/Jj+CWcdBIxELUcC2Ov0t4SgdXFseYKYwhRrLGZwaByu ciZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=cvR797bh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kk14-20020a17090b4a0e00b0025df9f1f7a1si7390832pjb.94.2023.06.22.06.17.40; Thu, 22 Jun 2023 06:17:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=cvR797bh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231616AbjFVNOF (ORCPT + 99 others); Thu, 22 Jun 2023 09:14:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231558AbjFVNOB (ORCPT ); Thu, 22 Jun 2023 09:14:01 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e5ab]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2C761BD3; 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 4FB41660711E; Thu, 22 Jun 2023 14:13:58 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1687439638; bh=t8kITWrGl0OsCDx7lvzHoBExx5sUqFU5dAXRs/EKO4E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cvR797bhp0zTiCSdScfhlbTHiIQr+utHyzNqQOongrdtKYpDwzUY0dWzKZPiFEqnU Vp+M7Y2JVVVAbeAA1bPTwIAWMVRo1zoQ0Jo632vU3uYP8DwzoI6ae2TvgAxP4N7hGb tC991tunarQYTHZ42wBjAFs76L3DMPSL4Z3l/u4D5NyNX/M1MdoAi2HKtFFo5zj5dv LCHML8nEK+y2kfaAqa+i4SV7jLe/BQQRK0t1vmXPQVo/UKPWsX035tkjUJO/rrc/Np rcZUErqSo9hyzmJXEaydmOtL+Wr2cCXILV8i/zNvZ5ymW326nqxtRdxJawJSvSIt07 4SjxbEmqKZYvw== 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 02/11] media: videobuf2: Use Xarray instead of static buffers array Date: Thu, 22 Jun 2023 15:13:40 +0200 Message-Id: <20230622131349.144160-3-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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769408952708443009?= X-GMAIL-MSGID: =?utf-8?q?1769408952708443009?= Instead of a static array change bufs to a dynamically allocated array. This will allow to store more video buffers if needed. Signed-off-by: Benjamin Gaignard --- .../media/common/videobuf2/videobuf2-core.c | 38 +++++++++++-------- include/media/videobuf2-core.h | 6 +-- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index 42fd3984c2bc..f1ff7af34a9f 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -409,18 +409,24 @@ static void init_buffer_cache_hints(struct vb2_queue *q, struct vb2_buffer *vb) * 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 *vb, int index) +static bool vb2_queue_add_buffer(struct vb2_queue *q, struct vb2_buffer *vb) { - if (index < VB2_MAX_FRAME && !q->bufs[index]) { - q->bufs[index] = vb; - vb->index = index; - vb->vb2_queue = q; - return true; - } + struct xa_limit range = { + .max = UINT_MAX, + .min = q->num_buffers, + }; + u32 index; + int ret; - return false; + ret = xa_alloc(&q->bufs, &index, vb, range, GFP_KERNEL); + if (ret) + return false; + + vb->index = index; + vb->vb2_queue = q; + + return true; } /** @@ -430,10 +436,8 @@ static bool vb2_queue_add_buffer(struct vb2_queue *q, struct vb2_buffer *vb, int */ static void vb2_queue_remove_buffer(struct vb2_queue *q, struct vb2_buffer *vb) { - if (vb->index < VB2_MAX_FRAME) { - q->bufs[vb->index] = NULL; - vb->vb2_queue = NULL; - } + xa_erase(&q->bufs, vb->index); + vb->vb2_queue = NULL; } /* @@ -474,7 +478,7 @@ static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory, } call_void_bufop(q, init_buffer, vb); - if (!vb2_queue_add_buffer(q, vb, q->num_buffers + buffer)) { + if (!vb2_queue_add_buffer(q, vb)) { dprintk(q, 1, "failed adding buffer %d to queue\n", buffer); kfree(vb); break; @@ -930,7 +934,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, } mutex_lock(&q->mmap_lock); - q->num_buffers = allocated_buffers; + q->num_buffers += allocated_buffers; if (ret < 0) { /* @@ -2547,6 +2551,9 @@ int vb2_core_queue_init(struct vb2_queue *q) mutex_init(&q->mmap_lock); init_waitqueue_head(&q->done_wq); + xa_init_flags(&q->bufs, XA_FLAGS_ALLOC); + q->num_buffers = 0; + q->memory = VB2_MEMORY_UNKNOWN; if (q->buf_struct_size == 0) @@ -2574,6 +2581,7 @@ void vb2_core_queue_release(struct vb2_queue *q) mutex_lock(&q->mmap_lock); __vb2_queue_free(q, q->num_buffers); mutex_unlock(&q->mmap_lock); + xa_destroy(&q->bufs); } EXPORT_SYMBOL_GPL(vb2_core_queue_release); diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h index 4b6a9d2ea372..77921cf894ef 100644 --- a/include/media/videobuf2-core.h +++ b/include/media/videobuf2-core.h @@ -619,7 +619,7 @@ struct vb2_queue { struct mutex mmap_lock; unsigned int memory; enum dma_data_direction dma_dir; - struct vb2_buffer *bufs[VB2_MAX_FRAME]; + struct xarray bufs; unsigned int num_buffers; struct list_head queued_list; @@ -1239,9 +1239,7 @@ static inline void vb2_clear_last_buffer_dequeued(struct vb2_queue *q) static inline struct vb2_buffer *vb2_get_buffer(struct vb2_queue *q, unsigned int index) { - if (index < q->num_buffers) - return q->bufs[index]; - return NULL; + return xa_load(&q->bufs, index); } /*