Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp2411377pxb; Sun, 30 Jan 2022 15:39:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJxCYJYd+sMDiQ9YE/V3VE7PbJ7tHyb3vCfJMLHi24qOwHlrDxkRvqetR9ErcH44JJGMSdCR X-Received: by 2002:a17:907:6da6:: with SMTP id sb38mr15389696ejc.58.1643585997196; Sun, 30 Jan 2022 15:39:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643585997; cv=none; d=google.com; s=arc-20160816; b=iq9dBUGRysyWx60XQrU+psRLIcZSiPiN8oyoMOV5Thw4xHCVCJaV9xuaBYUQ7yhIK9 72vKPX+C9y+26Gl6877b9XzzZT3strRYgsm5Stm+l/dV+vh8O3tFRVINoQnRp4NQ3KRB aEoUtxGHo0QX9lVI3S++XPI5rv9LsMbNzuSEUtkeRXG+MRB7waqzQJxjI4DptqZ/I0F6 r7Q8R7hLjH4XMPREX8z08YlDXqOg9A7K/JPclo1EoaQ6BWxMjoT2cGO85CLHOxN9Eqgd wJBrWelOzhlx6slfPxBSYK4Gi+7hPcjKLHw9qd8Ac6uHD2LAqO2qWDDaW4RxQtaG1HCr mq9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=eNS8WtoSSZL1atwIu/Dp8sb9NPZRxBubM0mb4GEEu1w=; b=AzRI7CMtzeVRRgbjPqFLm2P5i8UO0zvVkDauvI8XP+mgtWN1fBQMTBAZTeSjwN9BK8 hipWMZDD9yjUE45Se2VQM7192dteOpavAh/6FV9vvLYVf5WH8nNmyCtWw6IyVj71ExqT MPrLczNu9ZnCriE3ZyTXglH2AQhw+kKEOxpULNlL3ffOK6Onon+/1aimiQ0jreJvZUtc lT5/qfA0BwEQajeV897+QMSKyIHUrelMbTBVipjC7otN3Z1EX5csLnDsl5FfWBOx4OPM 0YfnBcB5HSa+8HMxP8wa9ZXkdvAh3FgF5DiqW4dYa0JHNWuWsHt/RUL6ymX9fVRZ4FkF EioQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xs4all.nl Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i15si7531210ejw.332.2022.01.30.15.39.23; Sun, 30 Jan 2022 15:39:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xs4all.nl Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347862AbiA1KXw (ORCPT + 99 others); Fri, 28 Jan 2022 05:23:52 -0500 Received: from ams.source.kernel.org ([145.40.68.75]:41914 "EHLO ams.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229864AbiA1KXs (ORCPT ); Fri, 28 Jan 2022 05:23:48 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 3C7B2B82513; Fri, 28 Jan 2022 10:23:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B9541C340E0; Fri, 28 Jan 2022 10:23:44 +0000 (UTC) Message-ID: Date: Fri, 28 Jan 2022 11:23:43 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH 1/2] media: videobuf2: use dmabuf size for length Content-Language: en-US To: linux-media@vger.kernel.org, Helen Fornazier Cc: kernel@collabora.com, linux-kernel@vger.kernel.org, jc@kynesim.co.uk, laurent.pinchart@ideasonboard.com, dave.stevenson@raspberrypi.org, tfiga@chromium.org References: <20210325001712.197837-1-helen.koike@collabora.com> From: Hans Verkuil In-Reply-To: <20210325001712.197837-1-helen.koike@collabora.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Helen & others, I'm going through a bunch of (very) old patches in my patchwork TODO list that for one reason or another I never processed. This patch series is one of them. Given the discussion that followed the post of this series I've decided not to merge this. I'll mark the series as 'Changes Requested'. If someone wants to continue work on this (Helen left Collabora), then please do so! Regards, Hans On 25/03/2021 01:17, Helen Koike wrote: > Always use dmabuf size when considering the length of the buffer. > Discard userspace provided length. > Fix length check error in _verify_length(), which was handling single and > multiplanar diferently, and also not catching the case where userspace > provides a bigger length and bytesused then the underlying buffer. > > Suggested-by: Hans Verkuil > Signed-off-by: Helen Koike > --- > > Hello, > > As discussed on > https://patchwork.linuxtv.org/project/linux-media/patch/gh5kef5bkeel3o6b2dkgc2dfagu9klj4c0@4ax.com/ > > This patch also helps the conversion layer of the Ext API patchset, > where we are not exposing the length field. > > It was discussed that userspace might use a smaller length field to > limit the usage of the underlying buffer, but I'm not sure if this is > really usefull and just complicates things. > > If this is usefull, then we should also expose a length field in the Ext > API, and document this feature properly. > > What do you think? > --- > .../media/common/videobuf2/videobuf2-core.c | 21 ++++++++++++++++--- > .../media/common/videobuf2/videobuf2-v4l2.c | 8 +++---- > include/uapi/linux/videodev2.h | 7 +++++-- > 3 files changed, 27 insertions(+), 9 deletions(-) > > diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c > index 02281d13505f..2cbde14af051 100644 > --- a/drivers/media/common/videobuf2/videobuf2-core.c > +++ b/drivers/media/common/videobuf2/videobuf2-core.c > @@ -1205,6 +1205,7 @@ static int __prepare_dmabuf(struct vb2_buffer *vb) > > for (plane = 0; plane < vb->num_planes; ++plane) { > struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd); > + unsigned int bytesused; > > if (IS_ERR_OR_NULL(dbuf)) { > dprintk(q, 1, "invalid dmabuf fd for plane %d\n", > @@ -1213,9 +1214,23 @@ static int __prepare_dmabuf(struct vb2_buffer *vb) > goto err; > } > > - /* use DMABUF size if length is not provided */ > - if (planes[plane].length == 0) > - planes[plane].length = dbuf->size; > + planes[plane].length = dbuf->size; > + bytesused = planes[plane].bytesused ? > + planes[plane].bytesused : dbuf->size; > + > + if (planes[plane].bytesused > planes[plane].length) { > + dprintk(q, 1, "bytesused is bigger then dmabuf length for plane %d\n", > + plane); > + ret = -EINVAL; > + goto err; > + } > + > + if (planes[plane].data_offset >= bytesused) { > + dprintk(q, 1, "data_offset >= bytesused for plane %d\n", > + plane); > + ret = -EINVAL; > + goto err; > + } > > if (planes[plane].length < vb->planes[plane].min_length) { > dprintk(q, 1, "invalid dmabuf length %u for plane %d, minimum length %u\n", > diff --git a/drivers/media/common/videobuf2/videobuf2-v4l2.c b/drivers/media/common/videobuf2/videobuf2-v4l2.c > index 7e96f67c60ba..ffc7ed46f74a 100644 > --- a/drivers/media/common/videobuf2/videobuf2-v4l2.c > +++ b/drivers/media/common/videobuf2/videobuf2-v4l2.c > @@ -98,14 +98,14 @@ static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b) > unsigned int bytesused; > unsigned int plane; > > - if (V4L2_TYPE_IS_CAPTURE(b->type)) > + /* length check for dmabuf is performed in _prepare_dmabuf() */ > + if (V4L2_TYPE_IS_CAPTURE(b->type) || b->memory == VB2_MEMORY_DMABUF) > return 0; > > if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) { > for (plane = 0; plane < vb->num_planes; ++plane) { > - length = (b->memory == VB2_MEMORY_USERPTR || > - b->memory == VB2_MEMORY_DMABUF) > - ? b->m.planes[plane].length > + length = b->memory == VB2_MEMORY_USERPTR > + ? b->m.planes[plane].length > : vb->planes[plane].length; > bytesused = b->m.planes[plane].bytesused > ? b->m.planes[plane].bytesused : length; > diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h > index 8d15f6ccc4b4..79b3b2893513 100644 > --- a/include/uapi/linux/videodev2.h > +++ b/include/uapi/linux/videodev2.h > @@ -968,7 +968,9 @@ struct v4l2_requestbuffers { > /** > * struct v4l2_plane - plane info for multi-planar buffers > * @bytesused: number of bytes occupied by data in the plane (payload) > - * @length: size of this plane (NOT the payload) in bytes > + * @length: size of this plane (NOT the payload) in bytes. Filled > + * by userspace for USERPTR and by the driver for DMABUF > + * and MMAP. > * @mem_offset: when memory in the associated struct v4l2_buffer is > * V4L2_MEMORY_MMAP, equals the offset from the start of > * the device memory for this plane (or is a "cookie" that > @@ -1025,7 +1027,8 @@ struct v4l2_plane { > * @m: union of @offset, @userptr, @planes and @fd > * @length: size in bytes of the buffer (NOT its payload) for single-plane > * buffers (when type != *_MPLANE); number of elements in the > - * planes array for multi-plane buffers > + * planes array for multi-plane buffers. Filled by userspace for > + * USERPTR and by the driver for DMABUF and MMAP. > * @reserved2: drivers and applications must zero this field > * @request_fd: fd of the request that this buffer should use > * @reserved: for backwards compatibility with applications that do not know