From patchwork Wed Aug 14 02:06:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 819897 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) (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 9860E182B9 for ; Wed, 14 Aug 2024 02:06:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723601216; cv=none; b=Sw5eTcTfGZN8GxYv48HfDnbGeoctvfXPjSFRa2/ISPeiXZzjCwz+S+NdwkC1de1IljuqVlmBLlo+G6W5GcfsBrHhbQm+BPDQsNhRTJNFKuzSQNk54kpPwV2FP7qWK8ZXD4gzIA5rMJdlD+IS8w+mqWIwqrAvyptXBnmeAVY5nCQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723601216; c=relaxed/simple; bh=Xne+rVjNAcc5kRmcQZLto5wJQ7I2JV1rtsao/d8sfqk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FDmFylT+u5VIDZOACNOS5QVrZh6J1J2f2DqL2yAwnUGNnfoJaVdXY988ryd0Ji9g4TfeyzQFTmhmnuGmCnujUrXIbgTkLeeCqbEPHcXI/N+8CXIwyeIbsH+NDTJQCrrMRLICzANIoQMOK84o4quE2Av6zLIaliNuwsoe+OEz9ho= 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=ZS60BXT3; arc=none smtp.client-ip=209.85.216.48 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="ZS60BXT3" Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-2d396f891c4so1060222a91.3 for ; Tue, 13 Aug 2024 19:06:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1723601214; x=1724206014; 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=oi3V3fI46LiV16A3WIik80YAuBbVgPRhK72w/FdirxA=; b=ZS60BXT3TYyWm0Dy176fPHAdJui4sRq7ondGkYFn2NeatQzF8hQ+0UBvJlbXDXyNKZ j05eGybLeIJm8G8LfqTcvqICuUVDUqz1aWN3fdiakwscYSYgDE05Q8f8XQYcGU8Obf7d wq7d4q44jP8CI5tTSzuNp3UY6k5W16aQ205z8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723601214; x=1724206014; 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=oi3V3fI46LiV16A3WIik80YAuBbVgPRhK72w/FdirxA=; b=PGzzz8spHOJDzQnf+bD68pAN8kArL4zuMPoxn7ePovFcP1f+bGT+EiRQYmmEYfhaG5 piYx0Cw+inzEQZjoGprXR3mCDLMkGBQhPnjL0Dd+ZvNkGnkrBl98yyPCVLpoKV1nBBKw hgpMGK946g0WyE+LRyj5pN0mE+/Y40kQ8gOry1DCG2P+MZRMyZ+ymmjXfL7lNIPIUnCJ Ns5HvA0OOwKeO5jN9el4VwpdFcyZVHdzXb+qydMNZgDW1Xos//kNYYIJZQo3UpXSdumJ nadupavIaeAXckpquk8vG48fM4kkhwDFLcyOFy+bicG0p7ll5cXNB9IROWmpGfxV6h8R DKIA== X-Gm-Message-State: AOJu0Yy6JXpC8LMLIOVhuRXpTX7r7REJmsed3ivfTgA7d4acBmruu/cd t7fbN5N6imHpiGrsppOpPbCHYatQOn9Lv8GFtKXcSba7n8MpuXJtOqoBiQX3tw== X-Google-Smtp-Source: AGHT+IHzY55y/xX+ZkwrM/W0HMovzqMcNN221t6HuM8ICTReT7tbG357Y7ppjGo+SlrBVdjiIOtb1A== X-Received: by 2002:a17:90b:33c5:b0:2c9:888a:7a7b with SMTP id 98e67ed59e1d1-2d3aaab9b0dmr1626611a91.25.1723601213936; Tue, 13 Aug 2024 19:06:53 -0700 (PDT) Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:269a:7c5:e4f2:518c]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d3ac7d65b1sm303064a91.4.2024.08.13.19.06.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Aug 2024 19:06:53 -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 v5 1/4] media: videobuf2-core: clear memory related fields in __vb2_plane_dmabuf_put() Date: Wed, 14 Aug 2024 11:06:40 +0900 Message-ID: <20240814020643.2229637-2-yunkec@chromium.org> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog In-Reply-To: <20240814020643.2229637-1-yunkec@chromium.org> References: <20240814020643.2229637-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 Acked-by: Tomasz Figa --- 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 0217392fcc0d..4d232b08f950 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -311,6 +311,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; } /* @@ -1420,10 +1424,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 Aug 14 02:06:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 819301 Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) (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 3B8F3182B9 for ; Wed, 14 Aug 2024 02:06:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723601219; cv=none; b=m9ax3ws19ydF/ikoMBdYknzm2EA3XpvzhX9WtRoogExW9G6RXKAu+dFwzsoD3MropJ5IH/zaISCNdkcOtY3+SqnD9lWqwNFoICANAbTdYV5FuRGbIiSmTftwD2lxsapCXs6tZnHPaLmER7cuVtpfKQ8x6gYI8oBLAC584on8c30= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723601219; c=relaxed/simple; bh=MHivubr+aSfdTDoAx9G+VE8FPPqxd+dMJCQilgPia9A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PcwYDnaaOb15PN1HJniL32YOfSbF5JgGhJFBpG+nzrIC5kMTuEIGnNX7ubPnxlfhaAhm1GZWp/0PQDtxUsGaakQx9ZBIHZX2WxUVDEHPB2Vt+BUFaJtCN0F8ACn3sceHgXu+NJXQir7YCoxBHT38KG+sDLolj6T8k60eV6RoeZU= 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=S1l/ri05; arc=none smtp.client-ip=209.85.215.175 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="S1l/ri05" Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-7a115c427f1so4057008a12.0 for ; Tue, 13 Aug 2024 19:06:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1723601216; x=1724206016; 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=gqSR9lUPhshSAqptcvIC1X38m1fnyWXPJXD2Hh6SXwg=; b=S1l/ri05zISZDJGtoHs2BlB3SaX4PX3pOEwHwz222myQQhwfG3zPhn/v9Jo4i0mTJI WpBhLNWKvZ5ZgDv67neTKI3vTsLjujgO4BX+oovflFCvP4DEuhJWgYwhe+97emr9SsxS 7fTA4gO20EE/lHqfcE29xSaUEMNdcLNO5DUcg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723601216; x=1724206016; 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=gqSR9lUPhshSAqptcvIC1X38m1fnyWXPJXD2Hh6SXwg=; b=mDxBtmfD3mYoBB0KeQxtil5lAZQb3OV8BKoVZ2WR7WR1de6FjnEjQrMggD2wP2g/TB ZyFJZQZnz+Gb6P5NJG0qdYQ0FkDZLWqLy7sN8qZPo4sPrT4S5H9L3Ec2fkPXITyV6aDC aFJcI1vBufTEYEQxqadyBm+Ca7y1G8HKNpr6ZKUQg3Wp+I3XvLh13OsfqgbakUYnxpOl T1ksJ0okH6ZkPA025WJRrhApAtGT9oZ+SgjezG1JHUzWnzxtmb44sI5MuR+xIGhaM2WL R1BO98Id+/ZL6Z5GPrA2JrAZrmcJQtKF4qcSVI8Pei4IavC7YrEIshBsRIf1ZraH6wlJ Lvug== X-Gm-Message-State: AOJu0Yw0TPrlqCi51qxqXLIVHorGN+YvE8ZvvdtGGFA6ncgFzwNGFhq4 sCvz5Cn77G+5r4cc9BgNwryeij50hjUutvrl6JgYhYZTKJI3VMU5ghpwUQJj1w== X-Google-Smtp-Source: AGHT+IGDKtYIcxHkPm11eZ2ZMO94UifhkPiMwSygfHWqPNpvh9HGHPVTJ43n3/gL3mD58NV6RjmFJg== X-Received: by 2002:a05:6a20:438e:b0:1c6:a825:8bc1 with SMTP id adf61e73a8af0-1c8eae97e97mr2189401637.29.1723601216392; Tue, 13 Aug 2024 19:06:56 -0700 (PDT) Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:269a:7c5:e4f2:518c]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d3ac7d65b1sm303064a91.4.2024.08.13.19.06.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Aug 2024 19:06:55 -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 v5 2/4] media: videobuf2-core: release all planes first in __prepare_dmabuf() Date: Wed, 14 Aug 2024 11:06:41 +0900 Message-ID: <20240814020643.2229637-3-yunkec@chromium.org> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog In-Reply-To: <20240814020643.2229637-1-yunkec@chromium.org> References: <20240814020643.2229637-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 Acked-by: Tomasz Figa --- 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 4d232b08f950..b53d94659e30 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -1387,11 +1387,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 */ @@ -1402,76 +1404,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. @@ -1479,19 +1473,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 Aug 14 02:06:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 819896 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.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 8CA8F1C694 for ; Wed, 14 Aug 2024 02:06:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723601220; cv=none; b=Jw1Q0f/AaSvDzkGaH8KvDH41a3aumbxbCisLZP6Bt8AgVNlTsqRLGOG2rPKoHDfVuNzBSP+qsas4vUKxs9Q/pHC+IXeQdbWprnBq00ghl2HwyCG9jTOW47AEOvtgxtLetXIBeTq6FK4nv4ooRKMfxHG+eZdm6I70nppcdSKfHMo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723601220; c=relaxed/simple; bh=8kDhKi1RqGp+MT7W/FRCaDuugxh1wL7FBntj3egggRk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VMNXk0TBGvUke0Hog3oTbDlS14BuZcCrJwkBdazzH88pl1ZZce1igJodVRc7XfcC4WV+sg+xe4+sMqZO7yp50IF3n+rrzh2NGIfLMFfFzKQJUR+FE2d5GkhwDc+tbgaSBujGk3AJd/8zHwhl2fJ+H6kvAY7DRwDv1OSLwRWjIKY= 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=JKd/Yk3m; arc=none smtp.client-ip=209.85.216.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="JKd/Yk3m" Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-2d1daa2577bso3800483a91.2 for ; Tue, 13 Aug 2024 19:06:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1723601219; x=1724206019; 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=Py3r/mWLV/egkP5BiPd7Tab/P3CsruVhWzLlB8uYSYU=; b=JKd/Yk3mIe7gzCHbnY2LREpXdPRXsHhwRbSdK4aX3Cesg3E0YYjnWTuKV8aYyL1UJt uAS1LcIoee4+7IjT93fCtZRWzT/83zk3qEnUBzgyFKKyW5ZcW6xDA7xwCYOhVPrHzICN HM4JNL1x7is9kwKucBK1t8lV8HWnfUc6Zmqu0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723601219; x=1724206019; 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=Py3r/mWLV/egkP5BiPd7Tab/P3CsruVhWzLlB8uYSYU=; b=kvggjcnAn+3S+CgnYB1o7ux92xHYVumijPD/r8BT0xPXNkAL3N5MnK9adcMCelMEXc FoZY+lI3SlC44E+yDkQY444DH467Ezl0A2dwycqWiRvivGAlYq34IM55SNIajauSXSq2 mNJg/4y+OPHNykjoI2DHEUQCIS7rsnst3vzaS1dBJ06iJAQ11tqzSkCXFA+8L8v07tfJ LSHc7kDWvwJqk3EUgAp/90kh96ezfPPW+5u70Yp/1EY6VZsnuyhQtny3hMmHEkDId1+S yP7H573d6FwjM2G/kz0+88dJsdiaBpURPhKfka/pUwYZCdjdsn/x4nQF0sbE3nSQY3tL pAsQ== X-Gm-Message-State: AOJu0YwVPu/iXF0pIk1OC84iQtQr3o6bNfOo082iRYBVd24GvWs4xCf7 tdjulX6ZFEXGHBGnyzaf8+5JaYLDsMbogDmVpzcKupammm8Wy0Q4vZP1FmmczA== X-Google-Smtp-Source: AGHT+IE8DMrWJ6UVX8hj0o/EARX1VUwjdml2R+xMPBDr7mac8mLpCY3xkKsiXGJJ0er6UONr5aA3kQ== X-Received: by 2002:a17:90b:3b87:b0:2c9:65df:f871 with SMTP id 98e67ed59e1d1-2d3aaa99367mr1448039a91.15.1723601218799; Tue, 13 Aug 2024 19:06:58 -0700 (PDT) Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:269a:7c5:e4f2:518c]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d3ac7d65b1sm303064a91.4.2024.08.13.19.06.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Aug 2024 19:06:58 -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 v5 3/4] media: videobuf2-core: reverse the iteration order in __vb2_buf_dmabuf_put Date: Wed, 14 Aug 2024 11:06:42 +0900 Message-ID: <20240814020643.2229637-4-yunkec@chromium.org> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog In-Reply-To: <20240814020643.2229637-1-yunkec@chromium.org> References: <20240814020643.2229637-1-yunkec@chromium.org> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This patch prepares for allowing multiple planes to share the same DMA buffer attachment. Release the planes from num_planes - 1 to 0 so that we don't leave invalid mem_priv pointers behind. Signed-off-by: Yunke Cao Acked-by: Tomasz Figa --- v5: - Add comment to explain why. - Update commit message to explain why. v3: - Change local variable to an integer to make the code cleaner. --- drivers/media/common/videobuf2/videobuf2-core.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index b53d94659e30..e6af963307e3 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -323,9 +323,15 @@ 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) + /* + * When multiple planes share the same DMA buffer attachment, the plane + * with the lowest index owns the mem_priv. + * Put planes in the reversed order so that we don't leave invalid + * mem_priv behind. + */ + for (plane = vb->num_planes - 1; plane >= 0; --plane) __vb2_plane_dmabuf_put(vb, &vb->planes[plane]); } From patchwork Wed Aug 14 02:06:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunke Cao X-Patchwork-Id: 819300 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.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 F3820A21 for ; Wed, 14 Aug 2024 02:07:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723601223; cv=none; b=Kr0diTxOCmFb2hGmD/90iQqo6OpIy50wipnZ+DR0xmzbkJmCNGtA7V6GZ4V3in5dXYH0zG0kUPEfvi8A1T4Y/fYYXm80v+W/HaqmCwjn4dyzetbNkgQTC9R7yqBPSHDSP+KIds3Y+1OGuXhq1gZXAtL2AFIP8vF3S0Vp1BnoM8U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723601223; c=relaxed/simple; bh=kvN5a0n/wJKUJuiQB2mVD9jODWNuNf3o55USPgryGbU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hOE0ebsDWDxE8PZH1/IKbVHHKI6nEjqiPhqNA8X50eb/xRHp5MMWhvZQEvnEAUCdwcBoNrJOmM29CRkmQXZitA3GK/Otrvo5EbaV6vkCac3DrXGTySCLryKBqhKAeSndWV5QAt+52jJ1xlJLPhtAVf+ykGsKPxjZQ8556KV+MNo= 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=leDFCHXn; arc=none smtp.client-ip=209.85.215.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="leDFCHXn" Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-7bcf8077742so4411115a12.0 for ; Tue, 13 Aug 2024 19:07:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1723601221; x=1724206021; 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=Oer7Z/BAZa2AXUlghr5uJcQkvUFgqodj+Ovxovv0c/Y=; b=leDFCHXnZ1NG1XKldJfkXIqrcWkW4jsEmkNxN7XROm/qW+KbaqgUZIRd1BvQrQv5Gg y3yUu9Lvuh7nBwQe9xbpGa8jnfV3D6746DQZHBXMUPXXpa+Gop0U3NhZe4j80sRAa2rw urTmbuUfjrWWzHfL7zjZ0MNrQoM5RA5WF07os= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723601221; x=1724206021; 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=Oer7Z/BAZa2AXUlghr5uJcQkvUFgqodj+Ovxovv0c/Y=; b=ISeRK/kfPjGnhtDi1Stt17Dk/DORosl8kvyTk+6Q0PZXCRjj1B13hsOnCbPqNYKtjp rQNkoy9eTnbWDQ9H4O2HaTqe7Tbs+5VpfixWpuSTYtBp+I2OHXTV0Zt6vukPQB4WH2A3 wZ3HqACUWT7jRows8Z43B0Cjmqg1eSxf05rxtWZUh2pJUBOedTS0/NIldfGrmrYxqxz4 uZEk2f9ncu1K5QSebMtQShnpXGBAz7PMsdbt+QSq2S8IBQG3IKST3LHjGjGVQ/dEY6xN orAiIcy9v3cs3PhsNK4+O9LQOkZr0w+yHbItXjLgvReu9uxTvuk69qh/upGtoC9xZXDN Mjdw== X-Gm-Message-State: AOJu0YzgJzIMpEeUx/AX3JldsMPZHSHIbXCsxrIKDM9N/hdADUSsHyiq Q+9mVrcDloEEr7I9Ni3DSfEQEA2vd/daNZOl+/+r+17W/G2kC8c4oavurmM9Jw== X-Google-Smtp-Source: AGHT+IHVclrjZEyEWaLysG4E5RO5jiTzGq+2N5shf9v6IlJzRd5ZB5yenL00/YE5KnXH6uSMa2JAxQ== X-Received: by 2002:a05:6a20:d706:b0:1c6:ecee:1850 with SMTP id adf61e73a8af0-1c8eaf8e7fcmr2067307637.49.1723601221185; Tue, 13 Aug 2024 19:07:01 -0700 (PDT) Received: from yunkec1.tok.corp.google.com ([2401:fa00:8f:203:269a:7c5:e4f2:518c]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d3ac7d65b1sm303064a91.4.2024.08.13.19.06.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Aug 2024 19:07:00 -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 v5 4/4] media: videobuf2-core: attach once if multiple planes share the same dbuf Date: Wed, 14 Aug 2024 11:06:43 +0900 Message-ID: <20240814020643.2229637-5-yunkec@chromium.org> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog In-Reply-To: <20240814020643.2229637-1-yunkec@chromium.org> References: <20240814020643.2229637-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 Acked-by: Tomasz Figa --- v5 - Fix typo in videobuf2-core.h comments. 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 e6af963307e3..500a4e0c84ab 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -303,10 +303,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; @@ -315,6 +318,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; } /* @@ -1379,7 +1383,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; @@ -1432,6 +1436,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..9b02aeba4108 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 + * @dbuf_duplicated: 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;