From patchwork Thu May 7 13:28:44 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sumit Semwal X-Patchwork-Id: 48113 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f200.google.com (mail-lb0-f200.google.com [209.85.217.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id F214520553 for ; Thu, 7 May 2015 13:29:14 +0000 (UTC) Received: by lbbqq2 with SMTP id qq2sf12420025lbb.0 for ; Thu, 07 May 2015 06:29:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:delivered-to:from:to:subject:date :message-id:cc:precedence:list-id:list-unsubscribe:list-archive :list-post:list-help:list-subscribe:mime-version:content-type :content-transfer-encoding:errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list; bh=+tb6QnkwKYS70+DbV3Y2RCE8Rh/o6J4lQc7l/Zvpr5M=; b=FnZVjpZpKNgHtxAiU+RTZv4DUAcjS1CkSCo4cIK8vfc93sPrSDhJRZoNV4eR8eCQi0 N7qQ5Ts3lkqpWiwFpSm0fx+auvmKQk/l8KGwJb47QoJGFuO17q/u+5IQU0PdAlQNnH+f rV+A4aWAksEM+tig3rvU75EVT24fLt3iJe3jTssVgxkBjADHJL66qCQUFL/t/fXkgpES WMapF8o66d9KrzgbgFT2z4x8cefQvkXft0rNRX15z8UQ1XOm9F61C4BWZ4o/xhdpoX7p ELXT1UTNtFrR7lQM2ntLvbt59YA0E8HSCrIH3PgtKHKaQBrCTsao0snX7rVJ4+7UTL9J 0abQ== X-Gm-Message-State: ALoCoQkVHnrcd6iybD3aZRzEkPrIdgV8go2xR9/t/EP8g5WPGZP5Il4SErAUcdiEpEmRyH34Qywv X-Received: by 10.112.142.170 with SMTP id rx10mr2231049lbb.12.1431005353848; Thu, 07 May 2015 06:29:13 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.37.40 with SMTP id v8ls187021laj.88.gmail; Thu, 07 May 2015 06:29:13 -0700 (PDT) X-Received: by 10.152.19.70 with SMTP id c6mr3063410lae.121.1431005353588; Thu, 07 May 2015 06:29:13 -0700 (PDT) Received: from mail-la0-f52.google.com (mail-la0-f52.google.com. [209.85.215.52]) by mx.google.com with ESMTPS id i6si1283997laa.144.2015.05.07.06.29.13 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 May 2015 06:29:13 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.52 as permitted sender) client-ip=209.85.215.52; Received: by layy10 with SMTP id y10so30627831lay.0 for ; Thu, 07 May 2015 06:29:13 -0700 (PDT) X-Received: by 10.153.6.36 with SMTP id cr4mr3174194lad.56.1431005353417; Thu, 07 May 2015 06:29:13 -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.67.65 with SMTP id l1csp3442283lbt; Thu, 7 May 2015 06:29:12 -0700 (PDT) X-Received: by 10.66.141.231 with SMTP id rr7mr7134270pab.154.1431005351293; Thu, 07 May 2015 06:29:11 -0700 (PDT) Received: from gabe.freedesktop.org (gabe.freedesktop.org. [131.252.210.177]) by mx.google.com with ESMTP id uj2si2797991pab.146.2015.05.07.06.29.10; Thu, 07 May 2015 06:29:11 -0700 (PDT) Received-SPF: none (google.com: dri-devel-bounces@lists.freedesktop.org does not designate permitted sender hosts) client-ip=131.252.210.177; Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 828776E091; Thu, 7 May 2015 06:29:09 -0700 (PDT) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-pa0-f54.google.com (mail-pa0-f54.google.com [209.85.220.54]) by gabe.freedesktop.org (Postfix) with ESMTP id 303466E091 for ; Thu, 7 May 2015 06:29:08 -0700 (PDT) Received: by pabsx10 with SMTP id sx10so40301885pab.3 for ; Thu, 07 May 2015 06:29:08 -0700 (PDT) X-Received: by 10.68.250.229 with SMTP id zf5mr7020502pbc.158.1431005347935; Thu, 07 May 2015 06:29:07 -0700 (PDT) Received: from ss-ubuntu.lan ([106.51.225.38]) by mx.google.com with ESMTPSA id g4sm2220973pdk.82.2015.05.07.06.29.02 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 07 May 2015 06:29:06 -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 Subject: [PATCH v2] dma-buf: add ref counting for module as exporter Date: Thu, 7 May 2015 18:58:44 +0530 Message-Id: <1431005324-22234-1-git-send-email-sumit.semwal@linaro.org> X-Mailer: git-send-email 1.9.1 Cc: linaro-kernel@lists.linaro.org X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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.215.52 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 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. 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..0eff4bf56ef6 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 @@ -64,6 +65,7 @@ static int dma_buf_release(struct inode *inode, struct file *file) BUG_ON(dmabuf->cb_shared.active || dmabuf->cb_excl.active); dmabuf->ops->release(dmabuf); + module_put(dmabuf->owner); mutex_lock(&db_list.lock); list_del(&dmabuf->list_node); @@ -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.