From patchwork Tue Aug 14 15:34:51 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomasz Stanislawski X-Patchwork-Id: 1323541 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by patchwork2.kernel.org (Postfix) with ESMTP id F2118DF266 for ; Tue, 14 Aug 2012 21:30:21 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 055249E7B2 for ; Tue, 14 Aug 2012 14:30:22 -0700 (PDT) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mailout2.samsung.com (mailout2.samsung.com [203.254.224.25]) by gabe.freedesktop.org (Postfix) with ESMTP id 4CFA29EB86 for ; Tue, 14 Aug 2012 08:37:55 -0700 (PDT) Received: from epcpsbgm1.samsung.com (mailout2.samsung.com [203.254.224.25]) by mailout2.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0M8R009VA4QZY6D0@mailout2.samsung.com> for dri-devel@lists.freedesktop.org; Wed, 15 Aug 2012 00:37:54 +0900 (KST) X-AuditID: cbfee61a-b7fc66d0000043b7-f0-502a70d2673d Received: from epmmp1.local.host ( [203.254.227.16]) by epcpsbgm1.samsung.com (EPCPMTA) with SMTP id 03.9D.17335.2D07A205; Wed, 15 Aug 2012 00:37:54 +0900 (KST) Received: from mcdsrvbld02.digital.local ([106.116.37.23]) by mmp1.samsung.com (Oracle Communications Messaging Server 7u4-24.01 (7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0M8R004J44MBC810@mmp1.samsung.com> for dri-devel@lists.freedesktop.org; Wed, 15 Aug 2012 00:37:54 +0900 (KST) From: Tomasz Stanislawski To: linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org Subject: [PATCHv8 21/26] v4l: vb2-dma-contig: add reference counting for a device from allocator context Date: Tue, 14 Aug 2012 17:34:51 +0200 Message-id: <1344958496-9373-22-git-send-email-t.stanislaws@samsung.com> X-Mailer: git-send-email 1.7.10 In-reply-to: <1344958496-9373-1-git-send-email-t.stanislaws@samsung.com> References: <1344958496-9373-1-git-send-email-t.stanislaws@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrEJMWRmVeSWpSXmKPExsVy+t9jAd1LBVoBBn+bFC2ufH3P5sDocb/7 OFMAYxSXTUpqTmZZapG+XQJXxpctvWwF63gqNq+3bWBcyNXFyMkhIWAiMWnSZFYIW0ziwr31 bF2MXBxCAosYJc7dWM0I4axjkrg78xEzSBUbUMexJZ8ZQWwRAQeJ03dnMYMUMQscYpbY3/ua HSQhLJAl8enYHCYQm0VAVaJ70gcWEJtXwEPiSMdyJoh18hJP7/exgdicQPGXm7qBhnIAbXOX WPjTfwIj7wJGhlWMoqkFyQXFSem5hnrFibnFpXnpesn5uZsYwT5/JrWDcWWDxSFGAQ5GJR5e hTTNACHWxLLiytxDjBIczEoivEVBWgFCvCmJlVWpRfnxRaU5qcWHGKU5WJTEeY29v/oLCaQn lqRmp6YWpBbBZJk4OKUaGLuS94jmrMlVcDkSdPZK3sqgzvNik15FZist/XnhYODJjpOs7/RC vnw/UJsc51p3qbxqn9vKOavVOKa/6QhP6OHPUNGTPHhOef5iqXth/zrO3mhhj9ndZ3Vyw9wT 362/p7zY//rgO7fpzn/M3lhmBJj/tJm0w+BzEUv90vjN59X3XHsR/XmKxw4lluKMREMt5qLi RAADOkPg9QEAAA== X-TM-AS-MML: No Cc: t.stanislaws@samsung.com, pawel@osciak.com, mchehab@redhat.com, daniel.vetter@ffwll.ch, subashrp@gmail.com, linaro-mm-sig@lists.linaro.org, kyungmin.park@samsung.com, laurent.pinchart@ideasonboard.com, s.nawrocki@samsung.com, airlied@redhat.com, remi@remlab.net, g.liakhovetski@gmx.de, m.szyprowski@samsung.com X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dri-devel-bounces+patchwork-dri-devel=patchwork.kernel.org@lists.freedesktop.org Errors-To: dri-devel-bounces+patchwork-dri-devel=patchwork.kernel.org@lists.freedesktop.org This patch adds taking reference to the device for MMAP buffers. Such buffers, may be exported using DMABUF mechanism. If the driver that created a queue is unloaded then the queue is released, the device might be released too. However, buffers cannot be released if they are referenced by DMABUF descriptor(s). The device pointer kept in a buffer must be valid for the whole buffer's lifetime. Therefore MMAP buffers should take a reference to the device to avoid risk of dangling pointers. Signed-off-by: Tomasz Stanislawski --- drivers/media/video/videobuf2-dma-contig.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/media/video/videobuf2-dma-contig.c b/drivers/media/video/videobuf2-dma-contig.c index bb2b4ac8..d44766e 100644 --- a/drivers/media/video/videobuf2-dma-contig.c +++ b/drivers/media/video/videobuf2-dma-contig.c @@ -148,6 +148,7 @@ static void vb2_dc_put(void *buf_priv) kfree(buf->sgt_base); } dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr); + put_device(buf->dev); kfree(buf); } @@ -161,9 +162,13 @@ static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size) if (!buf) return ERR_PTR(-ENOMEM); + /* prevent the device from release while the buffer is exported */ + get_device(dev); + buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr, GFP_KERNEL); if (!buf->vaddr) { dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size); + put_device(dev); kfree(buf); return ERR_PTR(-ENOMEM); }