From patchwork Thu Oct 15 09:05:25 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomasz Figa X-Patchwork-Id: 31676 Received: from mail.tu-berlin.de ([130.149.7.33]) by www.linuxtv.org with esmtp (Exim 4.72) (envelope-from ) id 1ZmeTu-00048R-K7; Thu, 15 Oct 2015 11:05:50 +0200 X-tubIT-Incoming-IP: 209.132.180.67 Received: from vger.kernel.org ([209.132.180.67]) by mail.tu-berlin.de (exim-4.76/mailfrontend-7) with esmtp id 1ZmeTs-0007ve-1C; Thu, 15 Oct 2015 11:05:50 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751420AbbJOJFq (ORCPT + 1 other); Thu, 15 Oct 2015 05:05:46 -0400 Received: from mail-pa0-f49.google.com ([209.85.220.49]:35647 "EHLO mail-pa0-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750880AbbJOJFn (ORCPT ); Thu, 15 Oct 2015 05:05:43 -0400 Received: by pacao1 with SMTP id ao1so17372745pac.2 for ; Thu, 15 Oct 2015 02:05:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id; bh=WCL64GhEqZGAGXCkCNmljFsbW4+PlSVr8sTnXfkPtbo=; b=TTxqQBNxwSQHba1BxVG2XASBY2fNTuqyW0ZW0bNrntrioCLUayaDJv6Hfvt8vcPun6 dpevrYkoBMZk2CwjSzRYjFcLSjbJAXLQCfKk0SPTMieMmU6pFPFybtrjrUk7Zf/FBEDD A9XGdDfSPseXL31slFWVgASqpVkZR6HhpRcz0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=WCL64GhEqZGAGXCkCNmljFsbW4+PlSVr8sTnXfkPtbo=; b=XemM5raKENtQynAsd3lUtn/w96SPl0c4VoYTWuYMd8pbh05c4GCWBc6KrMCOLB970u 5KCXIjBk3SCnyPCtdLgAad609+SpQOchJ3ULiINAcBWHJoE0UDfUXdBnnv44n0y6eyNI PkLcAzRlmXyi6lmlie63zNuuGUKiCUGZ+cMg7qwqM1saG9fijydB+GcXOkW/+LgnkgO3 OgK+V14QMQ4A10taxO5b9IGeBzryaW4+dAy2mFWgZ4pITjFZA58MFIOo50Yf7eU5aNcP vz27X2NvNPxW7XtRhtKq0QL2cCn/HjdOnSsMbLv09Phi994PU4LdDsgqLXAHhjDiTUnW Y1mw== X-Gm-Message-State: ALoCoQmbsqUxUWYKP6VEakHv3XJKR5EznknRXCpIeJjKcOsGV2g93Qezde+tqdAGMEfxsv+a0Pwm X-Received: by 10.68.190.170 with SMTP id gr10mr8647272pbc.91.1444899943209; Thu, 15 Oct 2015 02:05:43 -0700 (PDT) Received: from basement.tok.corp.google.com ([172.23.69.229]) by smtp.gmail.com with ESMTPSA id bn1sm14176955pad.17.2015.10.15.02.05.40 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 15 Oct 2015 02:05:42 -0700 (PDT) From: Tomasz Figa To: linux-media@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Pawel Osciak , Marek Szyprowski , Kyungmin Park , Mauro Carvalho Chehab , Pawel Osciak , Daniel Kurtz , John Sheu , Tomasz Figa Subject: [PATCH] media: vb2: Allow reqbufs(0) with "in use" MMAP buffers Date: Thu, 15 Oct 2015 18:05:25 +0900 Message-Id: <1444899925-22608-1-git-send-email-tfiga@chromium.org> X-Mailer: git-send-email 2.6.0.rc2.230.g3dd15c0 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-PMX-Version: 6.0.0.2142326, Antispam-Engine: 2.7.2.2107409, Antispam-Data: 2015.10.15.85717 X-PMX-Spam: Gauge=IIIIIIII, Probability=8%, Report=' MULTIPLE_RCPTS 0.1, HTML_00_01 0.05, HTML_00_10 0.05, BODY_SIZE_3000_3999 0, BODY_SIZE_5000_LESS 0, BODY_SIZE_7000_LESS 0, DKIM_SIGNATURE 0, NO_URI_HTTPS 0, SINGLE_URI_IN_BODY 0, URI_ENDS_IN_HTML 0, __ANY_URI 0, __CP_URI_IN_BODY 0, __HAS_FROM 0, __HAS_MSGID 0, __HAS_X_MAILER 0, __HAS_X_MAILING_LIST 0, __MIME_TEXT_ONLY 0, __MULTIPLE_RCPTS_CC_X2 0, __SANE_MSGID 0, __SINGLE_URI_TEXT 0, __SUBJ_ALPHA_END 0, __TO_MALFORMED_2 0, __TO_NO_NAME 0, __URI_IN_BODY 0, __URI_NO_WWW 0, __URI_NS , __YOUTUBE_RCVD 0' From: John Sheu Videobuf2 presently does not allow VIDIOC_REQBUFS to destroy outstanding buffers if the queue is of type V4L2_MEMORY_MMAP, and if the buffers are considered "in use". This is different behavior than for other memory types and prevents us from deallocating buffers in following two cases: 1) There are outstanding mmap()ed views on the buffer. However even if we put the buffer in reqbufs(0), there will be remaining references, due to vma .open/close() adjusting vb2 buffer refcount appropriately. This means that the buffer will be in fact freed only when the last mmap()ed view is unmapped. 2) Buffer has been exported as a DMABUF. Refcount of the vb2 buffer is managed properly by VB2 DMABUF ops, i.e. incremented on DMABUF get and decremented on DMABUF release. This means that the buffer will be alive until all importers release it. Considering both cases above, there does not seem to be any need to prevent reqbufs(0) operation, because buffer lifetime is already properly managed by both mmap() and DMABUF code paths. Let's remove it and allow userspace freeing the queue (and potentially allocating a new one) even though old buffers might be still in processing. Signed-off-by: John Sheu Reviewed-by: Pawel Osciak Reviewed-by: Tomasz Figa Signed-off-by: Tomasz Figa --- drivers/media/v4l2-core/videobuf2-core.c | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/drivers/media/v4l2-core/videobuf2-core.c b/drivers/media/v4l2-core/videobuf2-core.c index 4f59b7e..931c5de 100644 --- a/drivers/media/v4l2-core/videobuf2-core.c +++ b/drivers/media/v4l2-core/videobuf2-core.c @@ -636,19 +636,6 @@ static bool __buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb) return false; } -/** - * __buffers_in_use() - return true if any buffers on the queue are in use and - * the queue cannot be freed (by the means of REQBUFS(0)) call - */ -static bool __buffers_in_use(struct vb2_queue *q) -{ - unsigned int buffer; - for (buffer = 0; buffer < q->num_buffers; ++buffer) { - if (__buffer_in_use(q, q->bufs[buffer])) - return true; - } - return false; -} /** * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be @@ -884,16 +871,7 @@ static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req) } if (req->count == 0 || q->num_buffers != 0 || q->memory != req->memory) { - /* - * We already have buffers allocated, so first check if they - * are not in use and can be freed. - */ mutex_lock(&q->mmap_lock); - if (q->memory == V4L2_MEMORY_MMAP && __buffers_in_use(q)) { - mutex_unlock(&q->mmap_lock); - dprintk(1, "memory in use, cannot free\n"); - return -EBUSY; - } /* * Call queue_cancel to clean up any buffers in the PREPARED or