From patchwork Fri May 8 13:42:43 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sumit Semwal X-Patchwork-Id: 48168 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f197.google.com (mail-lb0-f197.google.com [209.85.217.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 2B9D42121F for ; Fri, 8 May 2015 13:43:55 +0000 (UTC) Received: by lbos2 with SMTP id s2sf21210093lbo.2 for ; Fri, 08 May 2015 06:43:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=lAyIxzL/kjIz2WMMk1rwABdU1aAvDYMnOEEpjYQ0yM0=; b=LvnXfAQPLCd1U2JOUJUoqIy6xlM0mkmImIk1jASliog1jc4ofaR8faDJQ9VTNjAYqD ZAxVX/ro5igq7i08eMzlbzpEl/UH85v7yL/LLhMOLJ7pQdyA//inTHpIfDij+xuWSiOl 3P7PK/3jSbJj6Sw/eF7bVm2ZmufEct/o1cYV4Fs7K6mA7oa9ZE+5kW2X6qTfk8f+IOhQ Cp8xV2QXmWk7L5JBIfDHQb0mfSkQoJERfEhP2tmT6fidrx+gfxrpPqot+ZknDOzbLOL9 5J4rIpckJ54pgyeqlfg6Ag+Tp6CoHW+7215O44NxWi7nxj58QPCtNo/hJb4XkoOjQbrF dXoQ== X-Gm-Message-State: ALoCoQkDn1XC5PkK4AMVJZ/i1lYvhl/u3HPlOvpah2eW8kGauHHYx+52kuB37zuGUZN6svqwAO68 X-Received: by 10.112.151.146 with SMTP id uq18mr2116339lbb.20.1431092633675; Fri, 08 May 2015 06:43:53 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.163.200 with SMTP id yk8ls325917lab.95.gmail; Fri, 08 May 2015 06:43:53 -0700 (PDT) X-Received: by 10.152.26.230 with SMTP id o6mr3049109lag.7.1431092633525; Fri, 08 May 2015 06:43:53 -0700 (PDT) Received: from mail-lb0-f169.google.com (mail-lb0-f169.google.com. [209.85.217.169]) by mx.google.com with ESMTPS id o1si3352188lbc.8.2015.05.08.06.43.53 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 May 2015 06:43:53 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.169 as permitted sender) client-ip=209.85.217.169; Received: by lbbqq2 with SMTP id qq2so53457381lbb.3 for ; Fri, 08 May 2015 06:43:53 -0700 (PDT) X-Received: by 10.152.37.228 with SMTP id b4mr2896736lak.117.1431092633379; Fri, 08 May 2015 06:43:53 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.108.230 with SMTP id hn6csp32609lbb; Fri, 8 May 2015 06:43:52 -0700 (PDT) X-Received: by 10.67.24.33 with SMTP id if1mr6674955pad.24.1431092631323; Fri, 08 May 2015 06:43:51 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ah10si7182002pbd.100.2015.05.08.06.43.50; Fri, 08 May 2015 06:43:51 -0700 (PDT) Received-SPF: none (google.com: linux-media-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753413AbbEHNns (ORCPT + 4 others); Fri, 8 May 2015 09:43:48 -0400 Received: from mail-pd0-f178.google.com ([209.85.192.178]:36071 "EHLO mail-pd0-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753286AbbEHNns (ORCPT ); Fri, 8 May 2015 09:43:48 -0400 Received: by pdea3 with SMTP id a3so84425738pde.3 for ; Fri, 08 May 2015 06:43:47 -0700 (PDT) X-Received: by 10.70.128.110 with SMTP id nn14mr6640669pdb.135.1431092577391; Fri, 08 May 2015 06:42:57 -0700 (PDT) Received: from ss-ubuntu.lan ([106.51.225.38]) by mx.google.com with ESMTPSA id wt1sm5314189pbc.4.2015.05.08.06.42.52 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 08 May 2015 06:42:56 -0700 (PDT) From: Sumit Semwal To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-arm-kernel@lists.infradead.org, rmk+kernel@arm.linux.org.uk, airlied@linux.ie, kgene@kernel.org, thierry.reding@gmail.com, pawel@osciak.com, m.szyprowski@samsung.com, mchehab@osg.samsung.com, gregkh@linuxfoundation.org Cc: linaro-kernel@lists.linaro.org, robdclark@gmail.com, daniel@ffwll.ch, Sumit Semwal Subject: [PATCH v3] dma-buf: add ref counting for module as exporter Date: Fri, 8 May 2015 19:12:43 +0530 Message-Id: <1431092563-19799-1-git-send-email-sumit.semwal@linaro.org> X-Mailer: git-send-email 1.9.1 Sender: linux-media-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: sumit.semwal@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.169 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Add reference counting on a kernel module that exports dma-buf and implements its operations. This prevents the module from being unloaded while DMABUF file is in use. The original patch [1] was submitted by Tomasz Stanislawski, but this is a simpler way to do it. v3: call module_put() as late as possible, per gregkh's comment. v2: move owner to struct dma_buf, and use DEFINE_DMA_BUF_EXPORT_INFO macro to simplify the change. Signed-off-by: Sumit Semwal [1]: https://lkml.org/lkml/2012/8/8/163 --- drivers/dma-buf/dma-buf.c | 10 +++++++++- include/linux/dma-buf.h | 10 ++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index c5a9138a6a8d..63a9914e42b8 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -72,6 +73,7 @@ static int dma_buf_release(struct inode *inode, struct file *file) if (dmabuf->resv == (struct reservation_object *)&dmabuf[1]) reservation_object_fini(dmabuf->resv); + module_put(dmabuf->owner); kfree(dmabuf); return 0; } @@ -302,14 +304,20 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info) return ERR_PTR(-EINVAL); } + if (!try_module_get(exp_info->owner)) + return ERR_PTR(-ENOENT); + dmabuf = kzalloc(alloc_size, GFP_KERNEL); - if (dmabuf == NULL) + if (!dmabuf) { + module_put(exp_info->owner); return ERR_PTR(-ENOMEM); + } dmabuf->priv = exp_info->priv; dmabuf->ops = exp_info->ops; dmabuf->size = exp_info->size; dmabuf->exp_name = exp_info->exp_name; + dmabuf->owner = exp_info->owner; init_waitqueue_head(&dmabuf->poll); dmabuf->cb_excl.poll = dmabuf->cb_shared.poll = &dmabuf->poll; dmabuf->cb_excl.active = dmabuf->cb_shared.active = 0; diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index 2f0b431b73e0..f98bd7068d55 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -115,6 +115,8 @@ struct dma_buf_ops { * @attachments: list of dma_buf_attachment that denotes all devices attached. * @ops: dma_buf_ops associated with this buffer object. * @exp_name: name of the exporter; useful for debugging. + * @owner: pointer to exporter module; used for refcounting when exporter is a + * kernel module. * @list_node: node for dma_buf accounting and debugging. * @priv: exporter specific private data for this buffer object. * @resv: reservation object linked to this dma-buf @@ -129,6 +131,7 @@ struct dma_buf { unsigned vmapping_counter; void *vmap_ptr; const char *exp_name; + struct module *owner; struct list_head list_node; void *priv; struct reservation_object *resv; @@ -164,7 +167,8 @@ struct dma_buf_attachment { /** * struct dma_buf_export_info - holds information needed to export a dma_buf - * @exp_name: name of the exporting module - useful for debugging. + * @exp_name: name of the exporter - useful for debugging. + * @owner: pointer to exporter module - used for refcounting kernel module * @ops: Attach allocator-defined dma buf ops to the new buffer * @size: Size of the buffer * @flags: mode flags for the file @@ -176,6 +180,7 @@ struct dma_buf_attachment { */ struct dma_buf_export_info { const char *exp_name; + struct module *owner; const struct dma_buf_ops *ops; size_t size; int flags; @@ -187,7 +192,8 @@ struct dma_buf_export_info { * helper macro for exporters; zeros and fills in most common values */ #define DEFINE_DMA_BUF_EXPORT_INFO(a) \ - struct dma_buf_export_info a = { .exp_name = KBUILD_MODNAME } + struct dma_buf_export_info a = { .exp_name = KBUILD_MODNAME, \ + .owner = THIS_MODULE } /** * get_dma_buf - convenience wrapper for get_file.