From patchwork Wed Jun 5 07:40:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 13686284 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 2F8BB18C33D for ; Wed, 5 Jun 2024 07:40:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717573244; cv=none; b=UeYy6GH9I0Sb9kDoslH0GMb2aNiiLrndqHv1XC0+0RhqafUXaAlCtOOzXbI7fPf6JsQkfSvftPe6GoykrXSI39QUVFFHFOwnpZPifeYaOnCXfw0jn/1CdlqQU22CB3zG58+ABNGKvHA9RVE8rJMLEqfgA6BRtA46E+PykavH4UA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717573244; c=relaxed/simple; bh=cBUdsEQEmRsJtFBO41zY8/qOnOSKknyAZhoMVlwwvD0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=B0NhC0HQOrSlxBztRJHyLpBdjKAchBj6t8q8/vXxNt3fuYACWi7X36ilszkvwT6F2B3EQv0IwaCrkucrhTbghj1JMUlICXJxHs2gqL5eVixPuST1cDGXYMI9Ngo3isERq7HHyJdvhkNtbJ156w6XULUAasdweuQJC6xXfhGGNG0= 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=kaEB8mcs; arc=none smtp.client-ip=209.85.214.172 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="kaEB8mcs" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1f32a3b9491so47265875ad.0 for ; Wed, 05 Jun 2024 00:40:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1717573242; x=1718178042; 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=K99XTOadvLNAR21f4ztYi4Gw0/02GndsgmS6EKAyR3w=; b=kaEB8mcsLlPnDgjq9bVJ8oquhvb5KOuAkcB03UmduIee6nP+/58iLTSZBPanWz35sl BVxqaD1RXGeCvcNj271L1lhbpiWAC6YxGr/jZfhDorXPWOPbdh7vYIbQItc1iEGT+vIy Z+oNIolmPSwy7jicTbFs9c0oQqBqsQUOgq5hg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717573242; x=1718178042; 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=K99XTOadvLNAR21f4ztYi4Gw0/02GndsgmS6EKAyR3w=; b=tWbuevnf7Sv8qjHv43/EuSYfQ+1a29+wTIzCMTFfakvD+l+KMvGPQJtZB9QkcxELHv kvQ8AoNnqI47a+0tDio5dOCSvApyzb10D+6orCWFX6ZBW4zjZH5NfuhsJ0GdG47fpF0k sLBR0zAvj5gp9dmcuAgV8PZgDPKVa565VGdbO5MIK3pwJkNtd0kH41/nxpOaQRA+SEfi ZKT3FFgIyBn4ZCPwZrzMAhH7lw/rpbUvuTB0Z4Wp+IXh3Frv/VVpWbXen2ibfopfrwwF IsC404G2epWyG2y5nD/sDIjkBPmgEGGrqE/iqTZ6Aeeiw6xwUGfh6j+7DIOlyg+mgE4M fZEw== X-Gm-Message-State: AOJu0Yyn9eYFjxu43k9CXe4ki7qaAw9rAknrujbSAhaylDTDn3McABAe uGyR806zEtLHBIVp6FC8miJxLuvHuNpBeNa8DvL3yb0odagBiuSaOXTP/ovjNIhOOzaWQW6nzvw = X-Google-Smtp-Source: AGHT+IHPwq9i7kIhUGoDcsxLyX+vdeh4twogd5MEOG278kTBY3wsKeKIF7cPiwKP7rUD8aOeDDZ/2A== X-Received: by 2002:a17:902:e80f:b0:1f6:8c07:ca41 with SMTP id d9443c01a7336-1f6a5a24e23mr20674905ad.36.1717573242405; Wed, 05 Jun 2024 00:40:42 -0700 (PDT) Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:f834:a68:ab2:f744]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6324e54besm95500405ad.254.2024.06.05.00.40.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 00:40:42 -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 v3 1/4] media: videobuf2-core: clear memory related fields in __vb2_plane_dmabuf_put() Date: Wed, 5 Jun 2024 16:40:32 +0900 Message-ID: <20240605074035.2620140-2-yunkec@chromium.org> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog In-Reply-To: <20240605074035.2620140-1-yunkec@chromium.org> References: <20240605074035.2620140-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 Wed Jun 5 07:40:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 13686285 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) (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 17C4218F2C4 for ; Wed, 5 Jun 2024 07:40:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717573246; cv=none; b=Gq1SgHgDKmmvB/ZFsUPnKeUybCHMDZz6wkypvxUYltnnNEh6aQoGF3s2aqERDp+A9fxoRMlz58p8fFRsg9rr2pV5NCQ4Ir7v9Pk7Jf9TUKwq6VhksTULvKnU7/kW6N0SI5KleD5BdoSrsgMeo8i7NWmNuSQs2RP1jYDmT1XB6FY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717573246; c=relaxed/simple; bh=ypiKIisSHOrrOIkwn7HkY6IwpIa5NiyrqnfYxXQIyMQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Xhtq1m7nXZgc0jplePIz20X2R7KErTo0gOT/meTCQP7cgbj5julQ8/fClPsO2LPucZfHaq3biUvun+XrDfHX21wrjyjYWJVoInIVPso1LT87UTfpP8hpKoiVUQfC06UTJXNg6sCTU6odK+slex2QwbHZSAnolFCpl6tCO6cJTDQ= 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=Ync3gavv; arc=none smtp.client-ip=209.85.210.176 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="Ync3gavv" Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-703ed15b273so260933b3a.1 for ; Wed, 05 Jun 2024 00:40:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1717573244; x=1718178044; 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=2cqysvyrWpSWwrRs/+n9EY6jDo5oCwiSq1EpZvbmuEo=; b=Ync3gavveVAx4WtfSP6aEX05XNd/E1FbKp0ofthP51odHZ6ku7XMANhPVPkcuX2yHf Qa4aks8KEF82tbc2Qy7hUB/euSTKpNuOg6se+SQDVGPKotmdIkfFc80ct5ggo1RtbGuu WtpcS6OnbDOhND7uswHiyOYVxDTMVjjbrD8/U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717573244; x=1718178044; 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=2cqysvyrWpSWwrRs/+n9EY6jDo5oCwiSq1EpZvbmuEo=; b=cH4pExSpwU3boB4oAdov6g67ZKNCM4Byx9l0nigrNQskd7gjlWNpnpphDP0f8yDcwR i7w9ws9nJYGY+zI3eIIWBvTnYwUl5PTnW5SZOQFbyUdtOQ+UqEJ4+0tfSF9shLVGA8ZX cGD7DiwiYld8EiWRIwg+tl3FLKoqiVgmASAHuGM5VDXsPwyfS6bReKfTrFAUo8VRpDTF mKxRxRvfXdhj7r6Wu/IKoyF0lvPczxKf40h7cvWB0cy1pEzj15wkLIsQpDQgCqqeZIuf 1fuuu4v1tiuvKVy4C+BuPAvXJCmVoSOU85UWVgvY0dQprln9KhIx2jcjH1Ka0HDwY8wp 0DAQ== X-Gm-Message-State: AOJu0YymFbJIIwJwjnZKiRB9x4yNY34/9CLH9WdpxkwMZ9aXvEDeuPB3 SV+/IbmBj2niPt3nAQNVNmcDV5kocnALLr8WZ8LSsmaBenfloiJqnATmWEnYDg== X-Google-Smtp-Source: AGHT+IFzwusOHJPZAP/24fHZXt4voVCtlK1TIQpTu6sLqq3w1TsMtfwt0KaToFo6z47uyP4f+sZ0ag== X-Received: by 2002:a05:6a20:d80d:b0:1af:881c:e924 with SMTP id adf61e73a8af0-1b2b763736fmr2044374637.62.1717573244236; Wed, 05 Jun 2024 00:40:44 -0700 (PDT) Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:f834:a68:ab2:f744]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6324e54besm95500405ad.254.2024.06.05.00.40.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 00:40:44 -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 v3 2/4] media: videobuf2-core: release all planes first in __prepare_dmabuf() Date: Wed, 5 Jun 2024 16:40:33 +0900 Message-ID: <20240605074035.2620140-3-yunkec@chromium.org> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog In-Reply-To: <20240605074035.2620140-1-yunkec@chromium.org> References: <20240605074035.2620140-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 Wed Jun 5 07:40:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 13686286 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 0724418F2D9 for ; Wed, 5 Jun 2024 07:40:46 +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=1717573248; cv=none; b=u5roZ4ua5FTGWTaEkkuPgGslfiIGhVqOBTJRjnPun1WhtqFmh+Kv1/HleMsTaEisPJPqhvloERoN7UOmrLNGeT7ggAkuRAMcxVOKtIFJqoXtfiZRE+U0OMivK9ecQlxlrINe1p1evgkRRnCZ3rHOTyrCjDLa0RURYIrk0J/1OMo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717573248; c=relaxed/simple; bh=n5fDZYdf8tnquDv4cDTSmY4cutIjFCCgX8eNH7j0RRo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AZ9urWbg0sVpeYBzIEUxaf37o1Reql59QkgDAtp0dOSrSp05NOU78UCpWIGZnJ2L7Wx5vzUDlZ58am4pDo7HYwhIGSzBZGGCuoB0i0/4PYE38/+fP9g6DvNg1FeuISxhsuaasIrdgAKeMc3lLO7w4SDbO6UQUmrtiZCVOeec2Cs= 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=li7uccOq; 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="li7uccOq" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-7024d571d8eso4036425b3a.0 for ; Wed, 05 Jun 2024 00:40:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1717573246; x=1718178046; 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=kvM/pmj9mOQJ/m+0J3H6muW7b88nPJP7pDjAngMaRbo=; b=li7uccOqkJ9mcKQn37MJCz9IGgvMUx95k8T/HjTWbClL54ZI1hLGE1xrJVH58ALaIu ub6K4ohPYce4ujoyAKjuHicdGayXFkeFO5Uth7CSGa+/m8eOVyV5yjIC8sT+pcu7FXp1 H8GuC8W4aqw34OUFuP6+5sXU1hmq/XgD5mvgM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717573246; x=1718178046; 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=kvM/pmj9mOQJ/m+0J3H6muW7b88nPJP7pDjAngMaRbo=; b=Hxlwt6j2UiYuwC1OcFN1WD8DCrCYR4bMqcYO/3EMUTncdhGCK/M38vxiVUaeMfojXX 4SQu9fmBpAjWb28p+EWffYHH18kAjhnQRdLJBDp1fy8+6FLe4675s3tr5jK2CQwPooI7 twi66B9SUogzxgBxKN6iLg6W62h41vB/ufAE2JDZoup8/4FIWRVyw8mso3AhMGiCvIeU vGlVhuvB/gKpW2d0nFiIwZgqH0C3oQMB3KGV9EXyGJtOIuJaknOV9jlBmR0uREGs8f5t xUBC7fqOallTuKPVA2MEAeEcmORj6mO2f5NaoOkVap6xocpiAwnpTKp874/YpDal35A+ 0G+A== X-Gm-Message-State: AOJu0YyiMkgZtICUqiId0NkpGiLDH7GPhndq62n9QmhRRvxDukNdvaPQ knw5GblhQ/DKDWxueN49ffUVM9A1MLJ/HkSiAuRl7QOg8h3FoGs8mR5qF9b3mg== X-Google-Smtp-Source: AGHT+IHeF4pUp6YVL0a+U+Df6O/eJtfdEKN0/mNghRnvTagvD1al64Cp0U7hAHg4LClX+W24kzDteA== X-Received: by 2002:a05:6a21:188:b0:1b1:d7a3:a76b with SMTP id adf61e73a8af0-1b2b6fca806mr2279743637.33.1717573246083; Wed, 05 Jun 2024 00:40:46 -0700 (PDT) Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:f834:a68:ab2:f744]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6324e54besm95500405ad.254.2024.06.05.00.40.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 00:40:45 -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 v3 3/4] media: videobuf2-core: reverse the iteration order in __vb2_buf_dmabuf_put Date: Wed, 5 Jun 2024 16:40:34 +0900 Message-ID: <20240605074035.2620140-4-yunkec@chromium.org> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog In-Reply-To: <20240605074035.2620140-1-yunkec@chromium.org> References: <20240605074035.2620140-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 Wed Jun 5 07:40:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 13686287 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (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 B908D18C356 for ; Wed, 5 Jun 2024 07:40:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717573250; cv=none; b=mpXMF/DiwFvSNmr0LwkFD1eRqnqLtWEndZhnGhWybVmVbs9N7To6bUnpaZXCOYz017E8nBFwaFHtx4PUbds2yo4SFuK/dG41x4Gn/I6Eq2PsCsgApmYoUdOIXl0VtAR0qFB3l0S0Wslcm8J5CeoxOTwwR3bDlNazlpgHexQvxc4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717573250; c=relaxed/simple; bh=/ASCOX5eT+IGEco2MTmPN5goM4pJFVnmbynShjYA9C8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fRw34GBpmgTC+/dGDbQeSyTQNWRqAi9ThwLwncZFgiluT4oU6gWjLFz3pSDC6nVyorapINNxKbqltteeNPHvKIWi4XDbLr7/SFRh718BzgWWCmNwvikNTF2lnIkEsTX3ZY4IfDwfcFTZJ5mUYoKrbCpulnenJGjq54ejQTZfMx0= 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=mK5hivPZ; arc=none smtp.client-ip=209.85.210.180 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="mK5hivPZ" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-703e5a09c11so552892b3a.2 for ; Wed, 05 Jun 2024 00:40:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1717573248; x=1718178048; 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=t04at/1az1gpesu5WPsR4YAcHcPjXq1Mids7Upubl5M=; b=mK5hivPZhVuBssCdES496CMP2iKxIY3mbWbZpc7F89nl1+HZwsugWKGgEy1vXBLBuo ZDHJfe4zUPNw4W/bPhjgC0QG5PZI0dYccEFMFzrpY0ZfN61hS1YE8CIjZzh3aReODjWV 6yTvS79d4CuWizDNg0H/pxCcRLZo9xvf4P48M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717573248; x=1718178048; 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=t04at/1az1gpesu5WPsR4YAcHcPjXq1Mids7Upubl5M=; b=AlmAzf5f3VSngcjg16De5JDQV6sCTqShRbiOfQq+yj0t/UTJw1aVl9rjktWCyWNT/w g6gUZcI2Ei24GU+pp+BRQG0SIoI6ZIvDoH7Lh0Xrn0tQlFTF3k5Yq1WYs1P+Zn+uOWGv SuTpInP/V0oFt+7S+Q0Fv/BCBz3YLITI7CqG0NkB8RjdAfWQH7qzRZssdhikfegjDCcF 2D3prj6Izlj3Hpr4ov8wJ6Gzb8j/JuEBR2dWltVRKbW+dUxXSfmgVF6xah2ot4PWJA8s CDU6BNCKetR4ZGgOx8QWTo1+BTMO9L1wXvtxfb4mjPFLl90GMjuADSD3tYw76NNEIPPG Wsgw== X-Gm-Message-State: AOJu0YyYexlJJ4LTxURZEj9I9kQk2fJX1uYBhSLZ9+VzXa7Y2wFv26zt FMld1/c/yNgngfWovJjC5ixXh8h17FF2fiyjWwQGxd7DivzbF3o0KKEpL/BzGg== X-Google-Smtp-Source: AGHT+IGDNcfAOhvHZeEUbC2a/gQw1w1ZyjIGSZhGGpNBEwE1axh1itrdawQ2fyddNhyLVFxeqxWCEg== X-Received: by 2002:a05:6a20:3c8a:b0:1b0:2472:da14 with SMTP id adf61e73a8af0-1b2b6e4b4c5mr2346362637.2.1717573247958; Wed, 05 Jun 2024 00:40:47 -0700 (PDT) Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:f834:a68:ab2:f744]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6324e54besm95500405ad.254.2024.06.05.00.40.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 00:40:47 -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 v3 4/4] media: videobuf2-core: attach once if multiple planes share the same dbuf Date: Wed, 5 Jun 2024 16:40:35 +0900 Message-ID: <20240605074035.2620140-5-yunkec@chromium.org> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog In-Reply-To: <20240605074035.2620140-1-yunkec@chromium.org> References: <20240605074035.2620140-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 --- 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 | 29 ++++++++++++++++--- include/media/videobuf2-core.h | 3 ++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index cbc8928f0418..90b65bf6c463 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,23 @@ 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) { + 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;