Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1538059rdb; Mon, 2 Oct 2023 12:42:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEuIFxC5E8nbTeqbWCOtc8UfZOr5/isVLkL0Xqw86tQb/Yzhy98b6J8cO05eu/2AjIEu1ve X-Received: by 2002:a05:6a21:3e0a:b0:162:c4db:1dd4 with SMTP id bk10-20020a056a213e0a00b00162c4db1dd4mr12240801pzc.26.1696275746584; Mon, 02 Oct 2023 12:42:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696275746; cv=none; d=google.com; s=arc-20160816; b=BHS2j8UhoCpjts0wVfM55piFvp3/K/k/M5V2AZOsMN0PlRg9wGc1nBV5X3SB/IVhxf SDni2ddtVs5knxZLfYSlrl8pWcurU2KJTV0/ajvefkjJoBTAGZ1TTdzQ3eTnsNocJ3FO H+1z+wz1A/8799RlynuUqzThxVxfGPUIttRSk76euo67c/wvDB99taUjRERyw5gE3B2c LhcI7sHHmXNiY32N+8maA8uJ3DDn3qod74dFSCnrANCSxMz1WI0c2DQQN5YpI27VerL1 pspxHOc0dOAKt4foAVkAZD4o0lFAS5xCVsTT9P04oyOh0CXddqeKNOuREQbvnaD9gVuA S4DA== 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=NjW0TZWZo3eJcClHkSKiB0tMFA3N7DMTyRdCYowohA4=; fh=ceA81l8d2hZW6pWH7VE5TwJxtgXUzAubfWIMcikPA1M=; b=bipARpI0Gu8jb/GAlqpvu72eO3pmIl7LuQAu0F1UURDCnTnfnNNIqP6W1KK9CnR6PX gt6uPPi0GgdW08EXK28597OlnGrw7oKLACnUAL2Da6JI7fTUXMDfSgJtEHFw0j6Xyguk SeaJUgS9nIHcLLBLQJXbrm2o+SmDBLhcrG/bWyU9frGGy2+Mt+Axv6l1OlVPGhsrdm7P JzHfKA6FWSgJexe+bOSl1FjJ/eVXqSUvvmrAbBOL1J6eRMtMedKUGt63s2b6pfDYaIil GD1vgbMIttv5qeJBIhZzoCleTyJWHjmc+PDcq7Jlz42VMDK+sGH8EEaHFFToAcIT7HP9 54lg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=lsWfn7aK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id cg20-20020a056a00291400b00690d255b5a7si12272428pfb.45.2023.10.02.12.42.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:42:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=lsWfn7aK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 4596C80293FB; Mon, 2 Oct 2023 05:06:48 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236943AbjJBMGn (ORCPT + 99 others); Mon, 2 Oct 2023 08:06:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236927AbjJBMGa (ORCPT ); Mon, 2 Oct 2023 08:06:30 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3423D3; Mon, 2 Oct 2023 05:06:26 -0700 (PDT) Received: from benjamin-XPS-13-9310.. (unknown [IPv6:2a01:e0a:120:3210:9537:67ca:c85e:d0ae]) (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 F2A8D6607243; Mon, 2 Oct 2023 13:06:24 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1696248385; bh=8xj+A2BpzIDj7xwabS651YXoIHIdIQ3kHPb3/Idtc2I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lsWfn7aKtK9coLq3dZZhJ0CQoy25PkH8wBnF0N/97O9Q7VvxuzihaW73qT7uFb+WT 2NGy1/KPl5aNfz80nNt43jYQofJMl+bm6tf+iSwkwp5YrzU1fLkuj4WLvb+fdEA+sa CrU9QZjlH2LhWWjkPibeRgGGnMCP3HtdIoalqmECsUwbrufRPsei0f/U+zdJNF93Tg uUu4DEjnjyOJDha8Nt9WGjyrigR/70YS9RV5XToRgp3S2WQucLP4LM9fnn+utExQ04 iwdtbOurUjkfVXdR8wxVPL4CODAqHs47GPFW93o4/36EA/5HOrlNCHuWJz7HZVG9U0 qQXrqOFtj3zVg== 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 v9 01/53] media: videobuf2: Rework offset 'cookie' encoding pattern Date: Mon, 2 Oct 2023 14:05:25 +0200 Message-Id: <20231002120617.119602-2-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231002120617.119602-1-benjamin.gaignard@collabora.com> References: <20231002120617.119602-1-benjamin.gaignard@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 02 Oct 2023 05:06:48 -0700 (PDT) Change how offset 'cookie' field value is computed to make possible to use more buffers. The maximum number of buffers depends of PAGE_SHIFT value and can go up to 0x7fff when PAGE_SHIFT = 12. With this encoding pattern we know the maximum number that a queue could store so we can check it at queue init time. It also make easier and faster to find buffer and plane from using the offset field. Change __find_plane_by_offset() prototype to return the video buffer itself rather than it index. Signed-off-by: Benjamin Gaignard --- changes in version 9: - BUFFER_INDEX_MASK now depends on PAGE_SHIFT value to match architectures requirements. .../media/common/videobuf2/videobuf2-core.c | 71 +++++++++---------- 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index cf6727d9c81f..a0540b2e461f 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -31,6 +31,10 @@ #include +#define PLANE_INDEX_SHIFT (PAGE_SHIFT + 3) +#define PLANE_INDEX_MASK 0x7 +#define BUFFER_INDEX_MASK (BIT_MASK(30 - PLANE_INDEX_SHIFT) - 1) + static int debug; module_param(debug, int, 0644); @@ -358,21 +362,24 @@ static void __setup_offsets(struct vb2_buffer *vb) unsigned int plane; unsigned long off = 0; - if (vb->index) { - struct vb2_buffer *prev = q->bufs[vb->index - 1]; - struct vb2_plane *p = &prev->planes[prev->num_planes - 1]; - - off = PAGE_ALIGN(p->m.offset + p->length); - } + /* + * Offsets cookies value have the following constraints: + * - a buffer could have up to 8 planes. + * - v4l2 mem2mem use bit 30 to distinguish between source and destination buffers. + * - must be page aligned + * That led to this bit mapping when PAGE_SHIFT = 12: + * |30 |29 15|14 12|11 0| + * |DST_QUEUE_OFF_BASE|buffer index|plane index| 0 | + * where there are 15 bits to store buffer index. + * Depending on PAGE_SHIFT value we can have else bits to store buffer index. + */ + off = vb->index << PLANE_INDEX_SHIFT; for (plane = 0; plane < vb->num_planes; ++plane) { - vb->planes[plane].m.offset = off; + vb->planes[plane].m.offset = off + (plane << PAGE_SHIFT); dprintk(q, 3, "buffer %d, plane %d offset 0x%08lx\n", vb->index, plane, off); - - off += vb->planes[plane].length; - off = PAGE_ALIGN(off); } } @@ -2185,13 +2192,12 @@ int vb2_core_streamoff(struct vb2_queue *q, unsigned int type) EXPORT_SYMBOL_GPL(vb2_core_streamoff); /* - * __find_plane_by_offset() - find plane associated with the given offset off + * __find_plane_by_offset() - find video buffer and plane associated with the given offset off */ static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off, - unsigned int *_buffer, unsigned int *_plane) + struct vb2_buffer **vb, unsigned int *plane) { - struct vb2_buffer *vb; - unsigned int buffer, plane; + unsigned int buffer; /* * Sanity checks to ensure the lock is held, MEMORY_MMAP is @@ -2209,24 +2215,15 @@ static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off, return -EBUSY; } - /* - * Go over all buffers and their planes, comparing the given offset - * with an offset assigned to each plane. If a match is found, - * return its buffer and plane numbers. - */ - for (buffer = 0; buffer < q->num_buffers; ++buffer) { - vb = q->bufs[buffer]; + /* Get buffer and plane from the offset */ + buffer = (off >> PLANE_INDEX_SHIFT) & BUFFER_INDEX_MASK; + *plane = (off >> PAGE_SHIFT) & PLANE_INDEX_MASK; - for (plane = 0; plane < vb->num_planes; ++plane) { - if (vb->planes[plane].m.offset == off) { - *_buffer = buffer; - *_plane = plane; - return 0; - } - } - } + if (buffer >= q->num_buffers || *plane >= q->bufs[buffer]->num_planes) + return -EINVAL; - return -EINVAL; + *vb = q->bufs[buffer]; + return 0; } int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type, @@ -2306,7 +2303,7 @@ int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma) { unsigned long off = vma->vm_pgoff << PAGE_SHIFT; struct vb2_buffer *vb; - unsigned int buffer = 0, plane = 0; + unsigned int plane = 0; int ret; unsigned long length; @@ -2335,12 +2332,10 @@ int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma) * Find the plane corresponding to the offset passed by userspace. This * will return an error if not MEMORY_MMAP or file I/O is in progress. */ - ret = __find_plane_by_offset(q, off, &buffer, &plane); + ret = __find_plane_by_offset(q, off, &vb, &plane); if (ret) goto unlock; - vb = q->bufs[buffer]; - /* * MMAP requires page_aligned buffers. * The buffer length was page_aligned at __vb2_buf_mem_alloc(), @@ -2368,7 +2363,7 @@ int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma) if (ret) return ret; - dprintk(q, 3, "buffer %d, plane %d successfully mapped\n", buffer, plane); + dprintk(q, 3, "buffer %u, plane %d successfully mapped\n", vb->index, plane); return 0; } EXPORT_SYMBOL_GPL(vb2_mmap); @@ -2382,7 +2377,7 @@ unsigned long vb2_get_unmapped_area(struct vb2_queue *q, { unsigned long off = pgoff << PAGE_SHIFT; struct vb2_buffer *vb; - unsigned int buffer, plane; + unsigned int plane; void *vaddr; int ret; @@ -2392,12 +2387,10 @@ unsigned long vb2_get_unmapped_area(struct vb2_queue *q, * Find the plane corresponding to the offset passed by userspace. This * will return an error if not MEMORY_MMAP or file I/O is in progress. */ - ret = __find_plane_by_offset(q, off, &buffer, &plane); + ret = __find_plane_by_offset(q, off, &vb, &plane); if (ret) goto unlock; - vb = q->bufs[buffer]; - vaddr = vb2_plane_vaddr(vb, plane); mutex_unlock(&q->mmap_lock); return vaddr ? (unsigned long)vaddr : -EINVAL; -- 2.39.2