From patchwork Fri Jun 14 07:36:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 13698116 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C50E0148FE8 for ; Fri, 14 Jun 2024 07:37:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718350632; cv=none; b=GemUZ+RTMuAmLi66eNsC0giVcoplC4XnAD2+9ql4fs+4Xh3Tg21apcyZz1Qe2tdzZpLoov+rckTwL+xtP3qyqhjnAo08LigBBeDoMBQcqOSmU2J8dBJtud/sJvvjoS10UpkXEDEvLjEATateVBFoLm3kLpPsxgtlbWnZVXsdL/s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718350632; c=relaxed/simple; bh=BleE3YrUqki2L6wdH1PL/JIfe6lof/p6CEI/YRYhsC0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hQ26bGI/B6/EpVHgWtrp2dFfX/KZQsk2+CuqH10TmApovum6GuMymkAi01HlAHAF9zz2w9ZIZTG5TkwB2P6ZwuyTlvU1zpwFbgfAXXKN68sK7vvvWMINnrp/edA29l5WWBeuam/4Y9nadISkgmvDhyjvF2PluUmpUbKh8lgRrTo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=WG81ZioM; arc=none smtp.client-ip=209.85.210.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="WG81ZioM" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-7046211e455so1454281b3a.3 for ; Fri, 14 Jun 2024 00:37:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1718350630; x=1718955430; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WgjUsjxAuhE7yxtL7mhkpWVXoSQBuyVhR4+JqVL2fPk=; b=WG81ZioM4vZQQHkhxvP6pARwjUFEA87tHzZi5A/2juoxH/224ZaGsCtstn7PscpNmH dkwq/hDmY4bmag5jSB75F3c3AQJaRlnezMQEce4eK4+QRa/JW1eiPFf+6OJ60aIuGmii qoaYX56PZK3GLclX+MEiPohQMAeM89ElCFMkE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718350630; x=1718955430; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WgjUsjxAuhE7yxtL7mhkpWVXoSQBuyVhR4+JqVL2fPk=; b=XPYbUKQi/nggD8yjHBhz/h/3qvf7Y4gf0MTN8Y2ZONuT2Y0bjPMn3RDbiYNW2lAyDV f1xyshRuPJ7m/Em/8trerZ3nq9ITniw7wUVX9GNAra/ZgLDcb45Js1EN8iaerqHr5VJE 2qWsfYXmzkY/uiCCDGKTRDSgUDas15Nme/REldDaTkJJU8sITgqF8A0JnGad7Mg7QEnE bpHHGFNTM8IG8TIEQQ5ex3L6W2KZ52iI27dYAsOLqgGLvofVFRzIx683YYnvbgPMEL67 w7zwzyy61J2wPTXyAOK8+ZjFAqnFtaYobxtLpN3f+YjzpCXjNf0PuyUikGoMC7Hnv0gM mRoQ== X-Gm-Message-State: AOJu0YwTX4kXTgOTtz7YR6TZLYZggV9WbxcSGfcZnboxfKXU7KvUgfKE xHfl7T5fjRxWV0c6ZMkf2Y1hVyDfmUA1UMh2EoweyHPQ2vW3lSrUJeanewlzA/Ts5fifki9hCPw = X-Google-Smtp-Source: AGHT+IG8G1B3MaGMB870Lg8J+uEaR37wTY5u7XVmYr7MDJRFsIWvwgB0U8NhCR3jmpBcRnnefm2MRw== X-Received: by 2002:a05:6a00:bcf:b0:704:3491:d74e with SMTP id d2e1a72fcca58-705d71a6e30mr1788476b3a.22.1718350630074; Fri, 14 Jun 2024 00:37:10 -0700 (PDT) Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:336:17cf:71d5:5733]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-705cc922643sm2468382b3a.31.2024.06.14.00.37.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 00:37:09 -0700 (PDT) From: Yunke Cao To: Tomasz Figa , Marek Szyprowski , Mauro Carvalho Chehab , Hans Verkuil Cc: linux-media@vger.kernel.org, Yunke Cao Subject: [PATCH v4 1/4] media: videobuf2-core: clear memory related fields in __vb2_plane_dmabuf_put() Date: Fri, 14 Jun 2024 16:36:59 +0900 Message-ID: <20240614073702.316810-2-yunkec@chromium.org> X-Mailer: git-send-email 2.45.2.627.g7a2c4fd464-goog In-Reply-To: <20240614073702.316810-1-yunkec@chromium.org> References: <20240614073702.316810-1-yunkec@chromium.org> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Clear vb2_plane's memory related fields in __vb2_plane_dmabuf_put(), including bytesused, length, fd and data_offset. Remove the duplicated code in __prepare_dmabuf(). Signed-off-by: Yunke Cao --- drivers/media/common/videobuf2/videobuf2-core.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index 358f1fe42975..a26130706506 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -312,6 +312,10 @@ static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p) p->mem_priv = NULL; p->dbuf = NULL; p->dbuf_mapped = 0; + p->bytesused = 0; + p->length = 0; + p->m.fd = 0; + p->data_offset = 0; } /* @@ -1421,10 +1425,6 @@ static int __prepare_dmabuf(struct vb2_buffer *vb) /* Release previously acquired memory if present */ __vb2_plane_dmabuf_put(vb, &vb->planes[plane]); - vb->planes[plane].bytesused = 0; - vb->planes[plane].length = 0; - vb->planes[plane].m.fd = 0; - vb->planes[plane].data_offset = 0; /* Acquire each plane's memory */ mem_priv = call_ptr_memop(attach_dmabuf, From patchwork Fri Jun 14 07:37:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 13698117 Received: from mail-oa1-f47.google.com (mail-oa1-f47.google.com [209.85.160.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2AEB1148FE8 for ; Fri, 14 Jun 2024 07:37:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718350634; cv=none; b=dRDZezbIjmy4nJLfg6EqJI0I7GEP4vccCOgDVzKJPAetHGvMIIGwgj72Fn52G1adIcHnuy1kMzxwRlHrPZkrLUFlUokBEFhVBnhWmkXPzVR+mkex6TNX2gHBdUKjMnfgAUmTkmNqkySw1hN2L0clOpUjWJoyn1BAMDz+FW1D148= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718350634; c=relaxed/simple; bh=ihcsatofbEbFqoisSbzP/jK3zeZAf0bdRdbp/wxAHxY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KaAGYnNMdcOxlBca+6X7G1JD2jQYM9pxVBzMY3oxyIWMBzyPeEZD927noNSjfUuvpuPXn5uC9O/ffDXutr2DYug1Kzwcw65NX+JZH1tI+BHDWJIsodxfBz5bgbzm9MA/KFGwijGz2hXcpVGIVRMB6Nmpt0SEaX+cUr8Urqca5us= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=H/5mXIWh; arc=none smtp.client-ip=209.85.160.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="H/5mXIWh" Received: by mail-oa1-f47.google.com with SMTP id 586e51a60fabf-24542b8607fso942382fac.1 for ; Fri, 14 Jun 2024 00:37:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1718350632; x=1718955432; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=iwDoOanHUygDwmb8b9OjRT4/rpr+scmAMggH3DnTSsE=; b=H/5mXIWhTOVNyr+iYHgObzLUOVzhE+BEecVD3JqAzAqDrMabtzviZz9V/q6debf710 3mHTOn3NoUJ9v+Q6iqbrdZfpDhBn3lRIOoBMz1P94kVNeUO3oEl3mjqePTAl4WxFqoxU cTzlsR3tGvkp6ADa31Ts6M4bBSwhK7a+UW7Lg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718350632; x=1718955432; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iwDoOanHUygDwmb8b9OjRT4/rpr+scmAMggH3DnTSsE=; b=PpfgJQlTD2CuXWtfmW+481N/mGAfd+uJazzesbxuEZXBI/Uvh5bWHMZ5GC4/YYBEr5 Uv0RcOLjkZco/xH1nhTltYAah62tJli9fn07fX/BrHdVYtAc5+Vu5sqJ2ebeYbHiP1lS IdH0MMu3o+oBZTMYrF07H1h9u5LvBqmP6CVXHHceb9hGaeAK5CTI5dJBZnmq3ZLZrqOp e0YqkC3pKQZ42I/FooDIE5rLyHzAVEJMulXf9ziU1CU0MmEUQDLU8P+vxLCascOaSId5 KQVV6uon+z/gRsYJR0+MDopXWqwc3+14JX+1vvXxOnrw7LTzQPen5JRjD3Pz+NSsmqkr 4npA== X-Gm-Message-State: AOJu0YzI0UffnNvqDnwKIgfSl5ckhV4puqRQLFnRe04R5vSJOWhXz2dH AOdhbMqWXKgNwewQM6MtG3yDRVjBb5EhBm+Jn95Sj0MyJ0E1vEC98YaaNFtOm18SVM9zu5Y9PMo = X-Google-Smtp-Source: AGHT+IH0l4nVAAZB0BJ4stqCpG3BfjrbOs3wTFD+MZ2nW3B58NslSpoIyUpchOjkMuaw8oJr3QqAqA== X-Received: by 2002:a05:6870:89a8:b0:254:963e:cc47 with SMTP id 586e51a60fabf-25842896ab1mr1972169fac.10.1718350632052; Fri, 14 Jun 2024 00:37:12 -0700 (PDT) Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:336:17cf:71d5:5733]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-705cc922643sm2468382b3a.31.2024.06.14.00.37.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 00:37:11 -0700 (PDT) From: Yunke Cao To: Tomasz Figa , Marek Szyprowski , Mauro Carvalho Chehab , Hans Verkuil Cc: linux-media@vger.kernel.org, Yunke Cao Subject: [PATCH v4 2/4] media: videobuf2-core: release all planes first in __prepare_dmabuf() Date: Fri, 14 Jun 2024 16:37:00 +0900 Message-ID: <20240614073702.316810-3-yunkec@chromium.org> X-Mailer: git-send-email 2.45.2.627.g7a2c4fd464-goog In-Reply-To: <20240614073702.316810-1-yunkec@chromium.org> References: <20240614073702.316810-1-yunkec@chromium.org> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In the existing implementation, validating planes, checking if the planes changed, releasing previous planes and reaquiring new planes all happens in the same for loop. Split the for loop into 3 parts 1. In the first for loop, validate planes and check if planes changed. 2. Call __vb2_buf_dmabuf_put() to release all planes. 3. In the second for loop, reaquire new planes. Signed-off-by: Yunke Cao --- v3: Applied Tomasz's review comment - Rename err_put_dbuf as err_put_planes. - Move code that only executed once into if (reacquired) to simply it. - In error handling, only call dma_buf_put() for valid pointers. --- .../media/common/videobuf2/videobuf2-core.c | 115 +++++++++--------- 1 file changed, 59 insertions(+), 56 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index a26130706506..a4fbc7a57ee0 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -1388,11 +1388,13 @@ 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); + planes[plane].dbuf = dbuf; + if (IS_ERR_OR_NULL(dbuf)) { dprintk(q, 1, "invalid dmabuf fd for plane %d\n", plane); ret = -EINVAL; - goto err; + goto err_put_planes; } /* use DMABUF size if length is not provided */ @@ -1403,76 +1405,68 @@ static int __prepare_dmabuf(struct vb2_buffer *vb) dprintk(q, 1, "invalid dmabuf length %u for plane %d, minimum length %u\n", planes[plane].length, plane, vb->planes[plane].min_length); - dma_buf_put(dbuf); ret = -EINVAL; - goto err; + goto err_put_planes; } /* Skip the plane if already verified */ if (dbuf == vb->planes[plane].dbuf && - vb->planes[plane].length == planes[plane].length) { - dma_buf_put(dbuf); + vb->planes[plane].length == planes[plane].length) continue; - } dprintk(q, 3, "buffer for plane %d changed\n", plane); - if (!reacquired) { - reacquired = true; + reacquired = true; + } + + if (reacquired) { + if (vb->planes[0].mem_priv) { vb->copied_timestamp = 0; call_void_vb_qop(vb, buf_cleanup, vb); + __vb2_buf_dmabuf_put(vb); } - /* Release previously acquired memory if present */ - __vb2_plane_dmabuf_put(vb, &vb->planes[plane]); - - /* Acquire each plane's memory */ - mem_priv = call_ptr_memop(attach_dmabuf, - vb, - q->alloc_devs[plane] ? : q->dev, - dbuf, - planes[plane].length); - if (IS_ERR(mem_priv)) { - dprintk(q, 1, "failed to attach dmabuf\n"); - ret = PTR_ERR(mem_priv); - dma_buf_put(dbuf); - goto err; - } - - vb->planes[plane].dbuf = dbuf; - vb->planes[plane].mem_priv = mem_priv; - } + for (plane = 0; plane < vb->num_planes; ++plane) { + /* Acquire each plane's memory */ + mem_priv = call_ptr_memop(attach_dmabuf, + vb, + q->alloc_devs[plane] ? : q->dev, + planes[plane].dbuf, + planes[plane].length); + if (IS_ERR(mem_priv)) { + dprintk(q, 1, "failed to attach dmabuf\n"); + ret = PTR_ERR(mem_priv); + goto err_put_vb2_buf; + } - /* - * This pins the buffer(s) with dma_buf_map_attachment()). It's done - * here instead just before the DMA, while queueing the buffer(s) so - * userspace knows sooner rather than later if the dma-buf map fails. - */ - for (plane = 0; plane < vb->num_planes; ++plane) { - if (vb->planes[plane].dbuf_mapped) - continue; + vb->planes[plane].dbuf = planes[plane].dbuf; + vb->planes[plane].mem_priv = mem_priv; - ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv); - if (ret) { - dprintk(q, 1, "failed to map dmabuf for plane %d\n", - plane); - goto err; + /* + * This pins the buffer(s) with dma_buf_map_attachment()). It's done + * here instead just before the DMA, while queueing the buffer(s) so + * userspace knows sooner rather than later if the dma-buf map fails. + */ + ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv); + if (ret) { + dprintk(q, 1, "failed to map dmabuf for plane %d\n", + plane); + goto err_put_vb2_buf; + } + vb->planes[plane].dbuf_mapped = 1; } - vb->planes[plane].dbuf_mapped = 1; - } - /* - * Now that everything is in order, copy relevant information - * provided by userspace. - */ - for (plane = 0; plane < vb->num_planes; ++plane) { - vb->planes[plane].bytesused = planes[plane].bytesused; - vb->planes[plane].length = planes[plane].length; - vb->planes[plane].m.fd = planes[plane].m.fd; - vb->planes[plane].data_offset = planes[plane].data_offset; - } + /* + * Now that everything is in order, copy relevant information + * provided by userspace. + */ + for (plane = 0; plane < vb->num_planes; ++plane) { + vb->planes[plane].bytesused = planes[plane].bytesused; + vb->planes[plane].length = planes[plane].length; + vb->planes[plane].m.fd = planes[plane].m.fd; + vb->planes[plane].data_offset = planes[plane].data_offset; + } - if (reacquired) { /* * Call driver-specific initialization on the newly acquired buffer, * if provided. @@ -1480,19 +1474,28 @@ static int __prepare_dmabuf(struct vb2_buffer *vb) ret = call_vb_qop(vb, buf_init, vb); if (ret) { dprintk(q, 1, "buffer initialization failed\n"); - goto err; + goto err_put_vb2_buf; } + } else { + for (plane = 0; plane < vb->num_planes; ++plane) + dma_buf_put(planes[plane].dbuf); } ret = call_vb_qop(vb, buf_prepare, vb); if (ret) { dprintk(q, 1, "buffer preparation failed\n"); call_void_vb_qop(vb, buf_cleanup, vb); - goto err; + goto err_put_vb2_buf; } return 0; -err: + +err_put_planes: + for (plane = 0; plane < vb->num_planes; ++plane) { + if (!IS_ERR_OR_NULL(planes[plane].dbuf)) + dma_buf_put(planes[plane].dbuf); + } +err_put_vb2_buf: /* In case of errors, release planes that were already acquired */ __vb2_buf_dmabuf_put(vb); From patchwork Fri Jun 14 07:37:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 13698118 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA3F2149C57 for ; Fri, 14 Jun 2024 07:37:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718350636; cv=none; b=QJYOMd+ewOWCfqd07qMBEZWSkwo0cC1VMsLd14yBr+MEePbev8mIZBFgiyUnCa+L2xI1QX0LOAKGWsQBYxHKFiTZ573BqzHkF+kcyqcT2VpscWPIDcNu5nBBTNcO+W+JF89cWi8YLtLS6e5WgtMCivD5W1hADvfxRdB8jukCnio= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718350636; c=relaxed/simple; bh=D71EkkuxqkwRO/GAnZuGd0W8JtPETqZ3Q//eAElCFWo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mn3GJW0Mq1t1b2Y8IH78t49NHlKnZPTUi0GhavY8D9J+I4zmGYqJ+7OcDhinnOlfa1pfwLO3EDSBKZXk+zMKiPs0RZSlbCHTAKaQ2XexYrUVXyVc2o14VshIBfqg/hdXoKeKRq4B/pFqhJ+XQh+9WwhBAoh35Lq9w/ICuQTWUcM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=MjuWMaix; arc=none smtp.client-ip=209.85.210.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="MjuWMaix" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-704189f1225so1775859b3a.0 for ; Fri, 14 Jun 2024 00:37:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1718350634; x=1718955434; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=F0WIEvpwjfC5Ybl+IxqglwPlMlShgzBFiqQKqyJAB3o=; b=MjuWMaixIDE/2Mh2G4JDQ8vw6lo048tYpCWsD1Bx6gugkBUvZv5GfEgcb3b2V/a/YM MQXDaFDZiGgY3CACPFJufwNme4G6o0MMYZL6zg5W+Iz+K/r51C0Q6pLBLb92PnN7OKvs pB2Q4wgMJ42xQOvvhM8Z2xdK/Z0xGi0Nloypw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718350634; x=1718955434; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=F0WIEvpwjfC5Ybl+IxqglwPlMlShgzBFiqQKqyJAB3o=; b=W1WfvLHofI1N7UyeJrajc04Yq1X/OJYEpt+VRAyRKV63Rx7qYf5y+Hw6RsnX8mmBkw LK3wTEXAz3Hiqz9PHCh1ZFdyUpdThMPdW0MURlCxckyy7w7dE5xXWEUamn86XqL2ZUgp ZDdhAb+FfeRJKvYt77TJWvGP+SODIGy8JdtjIssKf7bm1HE4pX3kOOWDKE0yJX+YPwZJ PJbO5FQoKqithd5N7QaFAUQnNwbR0GqmVvah5efxs6+JTlgPtv7ARiH+PGel93TQ67i2 XSL9E9T/9h/j7ms/EHZN+4gED9YK2+H21YgMeK6y8rSnBOAmVgK6Pwt4GXOB7JEolYV3 GEmA== X-Gm-Message-State: AOJu0YxZ/KsJFEyUW0IjXPqlfI7c+Befuu5lcS/xJsjWzFnsffq9E8no VZqgFFzQGLHdnv0SdlA689otgxUfAPLN9DkI3Ccs8aBkoZFoCVmQJAZXnf5G5Q== X-Google-Smtp-Source: AGHT+IE7nmSOAC6cCtRaQCtQ1RQtZXtFDqvBSe87aUt2IKYJCaFisqgI1b9oXfUxGmpO3LpYbyxkKw== X-Received: by 2002:aa7:8c05:0:b0:705:b1ed:118d with SMTP id d2e1a72fcca58-705d7192ff0mr1955611b3a.26.1718350633920; Fri, 14 Jun 2024 00:37:13 -0700 (PDT) Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:336:17cf:71d5:5733]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-705cc922643sm2468382b3a.31.2024.06.14.00.37.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 00:37:13 -0700 (PDT) From: Yunke Cao To: Tomasz Figa , Marek Szyprowski , Mauro Carvalho Chehab , Hans Verkuil Cc: linux-media@vger.kernel.org, Yunke Cao Subject: [PATCH v4 3/4] media: videobuf2-core: reverse the iteration order in __vb2_buf_dmabuf_put Date: Fri, 14 Jun 2024 16:37:01 +0900 Message-ID: <20240614073702.316810-4-yunkec@chromium.org> X-Mailer: git-send-email 2.45.2.627.g7a2c4fd464-goog In-Reply-To: <20240614073702.316810-1-yunkec@chromium.org> References: <20240614073702.316810-1-yunkec@chromium.org> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Release the planes from num_planes - 1 to 0. Signed-off-by: Yunke Cao --- v3: - Change local variable to an integer to make the code cleaner. --- drivers/media/common/videobuf2/videobuf2-core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index a4fbc7a57ee0..cbc8928f0418 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -324,9 +324,9 @@ static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p) */ static void __vb2_buf_dmabuf_put(struct vb2_buffer *vb) { - unsigned int plane; + int plane; - for (plane = 0; plane < vb->num_planes; ++plane) + for (plane = vb->num_planes - 1; plane >= 0; --plane) __vb2_plane_dmabuf_put(vb, &vb->planes[plane]); } From patchwork Fri Jun 14 07:37:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 13698119 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 76D1014F9FC for ; Fri, 14 Jun 2024 07:37:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718350637; cv=none; b=XOXa+zkELd5XrGH3YzE1HlrYOZrpO8s6X6b4lAZEWp3efwjDU3hQrT9Pa5AdMDZmtQx5KpE5PTYYYy1CxRvc2uK2rIQugym+zzS+NQKL1wgv8E3uX6vXA4sVt44q7pL+UaTFl+Oz7DYmGzO0US39sooeg8y0vz8xvyewON+M7gM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718350637; c=relaxed/simple; bh=R1muw++Z7H6mN1jgPNwV+vJ/av+1NjaBT0rCAs4IuR0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ltfyJ5KTjz1511JXLZvZVZVqlVRQavH0pT9BZh8q63EWO+6ibmfPetTmPtqIdFy2TBA2ua0Jy7ab0mkWTXNqqxjkmWOerTMQoPVI2QSwWsnQE3JksTkotkPRCkzcLI6knrMBUjdLTHF3JYIiYSt5SY3VBwC0SHtdO2RWAKIVBMA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=AVMrkxLB; arc=none smtp.client-ip=209.85.210.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="AVMrkxLB" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-704313fa830so1500558b3a.3 for ; Fri, 14 Jun 2024 00:37:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1718350636; x=1718955436; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=402pCG13entEg8MQsJhYqCgKO7kxugEROaOLmrqgiYI=; b=AVMrkxLBCYcj3xH0Z5WVdqPJhBGkcW0kSVpXLxCHFiBnPQcxZJEyd6pDOhGAlEqUG3 XrLiG3l+HPDJphv0NnSo2br+nliIrt1ASWHibiS8a0ePep79rV7j2YsGNwcWfWl74MDk 0ARhZ4ogWlL1s7cNFS2GsbDUyh4HnXJSq1Pwc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718350636; x=1718955436; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=402pCG13entEg8MQsJhYqCgKO7kxugEROaOLmrqgiYI=; b=T2sxv5uyc0SSvdpDxuMvHsMulgBo1iM6iSSYFbr7O/784D8BBRZ5tYzQpvqrq/OYGL XGH+rsLf0kldCpixrn9RV66/p74RqIXEZd1J7VM62e1vSyMQiaoYkSlJfRmPJIh5pBhI PSTJUeP+D5iy6jGwOUyJYUZfawajaoCAYYy6h2jDQQwh4r/6D7W5GFIXnQtRmovMBNZw UF4qWmavYKkqHk+XeskcykcQwe6bKbHTi4ohDtuWmYXnWoLIZbzkS/PwM8FpM4IFAr/H eXhoj0+NNKbpkTjUM5UiL2h3sUSuhci5EYhAmOgYMTFoBtJOAXiZSJrFrQ5c1DTEqYqt UyCA== X-Gm-Message-State: AOJu0Yzzmrt4lw+fHdu3U/WkZWqL7D1Vk8/3diwir6gz7SVeCwg2t2bb qczuT9ktfYcIsyPYYHnltA0iqs8o1w9SoyBChnknhRBYy8wNkOO6RE4iabJJQw== X-Google-Smtp-Source: AGHT+IHIScl9s/lWW1BTCnqrVm8pxG49H8tuhHvqfsuV7vO3BFjkD3bQD2VyEHDcQYlJlpjA2d2dhw== X-Received: by 2002:a05:6a00:1796:b0:705:9b04:70f with SMTP id d2e1a72fcca58-705d7200894mr2114551b3a.30.1718350635879; Fri, 14 Jun 2024 00:37:15 -0700 (PDT) Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:336:17cf:71d5:5733]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-705cc922643sm2468382b3a.31.2024.06.14.00.37.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 00:37:15 -0700 (PDT) From: Yunke Cao To: Tomasz Figa , Marek Szyprowski , Mauro Carvalho Chehab , Hans Verkuil Cc: linux-media@vger.kernel.org, Yunke Cao Subject: [PATCH v4 4/4] media: videobuf2-core: attach once if multiple planes share the same dbuf Date: Fri, 14 Jun 2024 16:37:02 +0900 Message-ID: <20240614073702.316810-5-yunkec@chromium.org> X-Mailer: git-send-email 2.45.2.627.g7a2c4fd464-goog In-Reply-To: <20240614073702.316810-1-yunkec@chromium.org> References: <20240614073702.316810-1-yunkec@chromium.org> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When multiple planes use the same dma buf, each plane will have its own dma buf attachment and mapping. It is a waste of IOVA space. This patch adds a dbuf_duplicated boolean in vb2_plane. If a plane's dbuf is the same as an existing plane, do not create another attachment and mapping. Signed-off-by: Yunke Cao --- v4 - Only set dbuf_duplicated when alloc_devs are equal. v3 - Adjust the patch according to the previous patches to resolve conflicts. - Add comment to explain the purpose of the change. v2 - Separate out the refactor changes out to previous patches. - Fix mem_priv check. --- .../media/common/videobuf2/videobuf2-core.c | 30 ++++++++++++++++--- include/media/videobuf2-core.h | 3 ++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index cbc8928f0418..c19f1df3a4d2 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -304,10 +304,13 @@ static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p) if (!p->mem_priv) return; - if (p->dbuf_mapped) - call_void_memop(vb, unmap_dmabuf, p->mem_priv); + if (!p->dbuf_duplicated) { + if (p->dbuf_mapped) + call_void_memop(vb, unmap_dmabuf, p->mem_priv); + + call_void_memop(vb, detach_dmabuf, p->mem_priv); + } - call_void_memop(vb, detach_dmabuf, p->mem_priv); dma_buf_put(p->dbuf); p->mem_priv = NULL; p->dbuf = NULL; @@ -316,6 +319,7 @@ static void __vb2_plane_dmabuf_put(struct vb2_buffer *vb, struct vb2_plane *p) p->length = 0; p->m.fd = 0; p->data_offset = 0; + p->dbuf_duplicated = false; } /* @@ -1374,7 +1378,7 @@ static int __prepare_dmabuf(struct vb2_buffer *vb) struct vb2_plane planes[VB2_MAX_PLANES]; struct vb2_queue *q = vb->vb2_queue; void *mem_priv; - unsigned int plane; + unsigned int plane, i; int ret = 0; bool reacquired = vb->planes[0].mem_priv == NULL; @@ -1427,6 +1431,24 @@ static int __prepare_dmabuf(struct vb2_buffer *vb) } for (plane = 0; plane < vb->num_planes; ++plane) { + /* + * This is an optimization to reduce dma_buf attachment/mapping. + * When the same dma_buf is used for multiple planes, there is no need + * to create duplicated attachments. + */ + for (i = 0; i < plane; ++i) { + if (planes[plane].dbuf == vb->planes[i].dbuf && + q->alloc_devs[plane] == q->alloc_devs[i]) { + vb->planes[plane].dbuf_duplicated = true; + vb->planes[plane].dbuf = vb->planes[i].dbuf; + vb->planes[plane].mem_priv = vb->planes[i].mem_priv; + break; + } + } + + if (vb->planes[plane].dbuf_duplicated) + continue; + /* Acquire each plane's memory */ mem_priv = call_ptr_memop(attach_dmabuf, vb, diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h index 955237ac503d..053ced60595f 100644 --- a/include/media/videobuf2-core.h +++ b/include/media/videobuf2-core.h @@ -154,6 +154,8 @@ struct vb2_mem_ops { * @mem_priv: private data with this plane. * @dbuf: dma_buf - shared buffer object. * @dbuf_mapped: flag to show whether dbuf is mapped or not + * @duplicated_dbuf: boolean to show whether dbuf is duplicated with a + * previous plane of the buffer. * @bytesused: number of bytes occupied by data in the plane (payload). * @length: size of this plane (NOT the payload) in bytes. The maximum * valid size is MAX_UINT - PAGE_SIZE. @@ -179,6 +181,7 @@ struct vb2_plane { void *mem_priv; struct dma_buf *dbuf; unsigned int dbuf_mapped; + bool dbuf_duplicated; unsigned int bytesused; unsigned int length; unsigned int min_length;