Skip to contentSkip to conversation

dma-buf: add reference counting for exporter module

PATCH Superseded Tomasz Stanislawski 4 participants 5 messages Aug 8, 2012 – Aug 9, 2012

Subsystems (12)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dma-buf: add reference counting for exporter module

This patch adds reference counting on a module that exports dma-buf and implements its operations. This prevents the module from being unloaded while DMABUF file is in use.

---
 Documentation/dma-buf-sharing.txt          |    3 ++-
 drivers/base/dma-buf.c                     |   10 +++++++++-
 drivers/gpu/drm/exynos/exynos_drm_dmabuf.c |    1 +
 drivers/gpu/drm/i915/i915_gem_dmabuf.c     |    1 +
 drivers/gpu/drm/nouveau/nouveau_prime.c    |    1 +
 drivers/gpu/drm/radeon/radeon_prime.c      |    1 +
 drivers/staging/omapdrm/omap_gem_dmabuf.c  |    1 +
 include/linux/dma-buf.h                    |    2 ++
 8 files changed, 18 insertions(+), 2 deletions(-)
Documentation/dma-buf-sharing.txt +2 -1
index ad86fb8..2613057 100644
@@ -49,7 +49,8 @@ The dma_buf buffer sharing API usage contains the following steps:
    The buffer exporter announces its wish to export a buffer. In this, it
    connects its own private buffer data, provides implementation for operations
    that can be performed on the exported dma_buf, and flags for the file
-   associated with this buffer.
+   associated with this buffer. The operations structure has owner field.
+   You should initialize this to THIS_MODULE in most cases.
 
    Interface:
       struct dma_buf *dma_buf_export(void *priv, struct dma_buf_ops *ops,
drivers/base/dma-buf.c +9 -1
index c30f3e1..d14b2f5 100644
@@ -27,6 +27,7 @@
 #include <linux/dma-buf.h>
 #include <linux/anon_inodes.h>
 #include <linux/export.h>
+#include <linux/module.h>
 
 static inline int is_dma_buf_file(struct file *);
 
@@ -40,6 +41,7 @@ static int dma_buf_release(struct inode *inode, struct file *file)
 	dmabuf = file->private_data;
 
 	dmabuf->ops->release(dmabuf);
+	module_put(dmabuf->ops->owner);
 	kfree(dmabuf);
 	return 0;
 }
@@ -96,6 +98,7 @@ struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops,
 	struct file *file;
 
 	if (WARN_ON(!priv || !ops
+			  || !ops->owner
 			  || !ops->map_dma_buf
 			  || !ops->unmap_dma_buf
 			  || !ops->release
@@ -105,9 +108,14 @@ struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops,
 		return ERR_PTR(-EINVAL);
 	}
 
+	if (!try_module_get(ops->owner))
+		return ERR_PTR(-ENOENT);
+
 	dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL);
-	if (dmabuf == NULL)
+	if (dmabuf == NULL) {
+		module_put(ops->owner);
 		return ERR_PTR(-ENOMEM);
+	}
 
 	dmabuf->priv = priv;
 	dmabuf->ops = ops;
drivers/gpu/drm/exynos/exynos_drm_dmabuf.c +1 -0
index 613bf8a..cf3bc6d 100644
@@ -164,6 +164,7 @@ static void exynos_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
 }
 
 static struct dma_buf_ops exynos_dmabuf_ops = {
+	.owner			= THIS_MODULE,
 	.map_dma_buf		= exynos_gem_map_dma_buf,
 	.unmap_dma_buf		= exynos_gem_unmap_dma_buf,
 	.kmap			= exynos_gem_dmabuf_kmap,
drivers/gpu/drm/i915/i915_gem_dmabuf.c +1 -0
index aa308e1..07ff03b 100644
@@ -152,6 +152,7 @@ static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *
 }
 
 static const struct dma_buf_ops i915_dmabuf_ops =  {
+	.owner = THIS_MODULE,
 	.map_dma_buf = i915_gem_map_dma_buf,
 	.unmap_dma_buf = i915_gem_unmap_dma_buf,
 	.release = i915_gem_dmabuf_release,
drivers/gpu/drm/nouveau/nouveau_prime.c +1 -0
index a25cf2c..8605033 100644
@@ -127,6 +127,7 @@ static void nouveau_gem_prime_vunmap(struct dma_buf *dma_buf, void *vaddr)
 }
 
 static const struct dma_buf_ops nouveau_dmabuf_ops =  {
+	.owner = THIS_MODULE,
 	.map_dma_buf = nouveau_gem_map_dma_buf,
 	.unmap_dma_buf = nouveau_gem_unmap_dma_buf,
 	.release = nouveau_gem_dmabuf_release,
drivers/gpu/drm/radeon/radeon_prime.c +1 -0
index 6bef46a..4061fd3 100644
@@ -127,6 +127,7 @@ static void radeon_gem_prime_vunmap(struct dma_buf *dma_buf, void *vaddr)
 	mutex_unlock(&dev->struct_mutex);
 }
 const static struct dma_buf_ops radeon_dmabuf_ops =  {
+	.owner = THIS_MODULE,
 	.map_dma_buf = radeon_gem_map_dma_buf,
 	.unmap_dma_buf = radeon_gem_unmap_dma_buf,
 	.release = radeon_gem_dmabuf_release,
drivers/staging/omapdrm/omap_gem_dmabuf.c +1 -0
index 42728e0..6a4dd67 100644
@@ -179,6 +179,7 @@ out_unlock:
 }
 
 struct dma_buf_ops omap_dmabuf_ops = {
+		.owner = THIS_MODULE,
 		.map_dma_buf = omap_gem_map_dma_buf,
 		.unmap_dma_buf = omap_gem_unmap_dma_buf,
 		.release = omap_gem_dmabuf_release,
include/linux/dma-buf.h +2 -0
index eb48f38..22953de 100644
@@ -37,6 +37,7 @@ struct dma_buf_attachment;
 
 /**
  * struct dma_buf_ops - operations possible on struct dma_buf
+ * @owner: the module that implements dma_buf operations
  * @attach: [optional] allows different devices to 'attach' themselves to the
  *	    given buffer. It might return -EBUSY to signal that backing storage
  *	    is already allocated and incompatible with the requirements
@@ -70,6 +71,7 @@ struct dma_buf_attachment;
  * @vunmap: [optional] unmaps a vmap from the buffer
  */
 struct dma_buf_ops {
+	struct module *owner;
 	int (*attach)(struct dma_buf *, struct device *,
 			struct dma_buf_attachment *);
 
Signature · 1 line
1.7.9.5
#1 lore raw

Hi Tomasz,

Thanks for the patch.

Quoted 52 lines · On Wednesday 08 August 2012 12:17:41 Tomasz Stanislawski wrote:

This patch adds reference counting on a module that exports dma-buf and implements its operations. This prevents the module from being unloaded while DMABUF file is in use.

---
 Documentation/dma-buf-sharing.txt          |    3 ++-
 drivers/base/dma-buf.c                     |   10 +++++++++-
 drivers/gpu/drm/exynos/exynos_drm_dmabuf.c |    1 +
 drivers/gpu/drm/i915/i915_gem_dmabuf.c     |    1 +
 drivers/gpu/drm/nouveau/nouveau_prime.c    |    1 +
 drivers/gpu/drm/radeon/radeon_prime.c      |    1 +
 drivers/staging/omapdrm/omap_gem_dmabuf.c  |    1 +
 include/linux/dma-buf.h                    |    2 ++
 8 files changed, 18 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/Documentation/dma-buf-sharing.txt index ad86fb8..2613057 100644

Documentation/dma-buf-sharing.txt +0 -0
@@ -49,7 +49,8 @@ The dma_buf buffer sharing API usage contains the

following steps: The buffer exporter announces its wish to export a buffer. In this, it connects its own private buffer data, provides implementation for operations that can be performed on the exported dma_buf, and flags for the file

Documentation/dma-buf-sharing.txt +2 -1
-   associated with this buffer.
+   associated with this buffer. The operations structure has owner field.
+   You should initialize this to THIS_MODULE in most cases.
 
    Interface:
       struct dma_buf *dma_buf_export(void *priv, struct dma_buf_ops *ops,
drivers/base/dma-buf.c +2 -0
index c30f3e1..d14b2f5 100644
@@ -27,6 +27,7 @@
 #include <linux/dma-buf.h>
 #include <linux/anon_inodes.h>
 #include <linux/export.h>
+#include <linux/module.h>
 
 static inline int is_dma_buf_file(struct file *);
 
@@ -40,6 +41,7 @@ static int dma_buf_release(struct inode *inode, struct
 file *file) dmabuf = file->private_data;
 
 	dmabuf->ops->release(dmabuf);
+	module_put(dmabuf->ops->owner);
 	kfree(dmabuf);
 	return 0;
 }

drivers/base/dma-buf.c struct dma_buf *dma_buf_export(void *priv, const struct · line 101

Quoted 4 lines
drivers/base/dma-buf.c +1 -0
@@ -96,6 +98,7 @@ struct dma_buf *dma_buf_export(void *priv, const struct
 dma_buf_ops *ops, struct file *file;
 
 	if (WARN_ON(!priv || !ops
+			  || !ops->owner

THIS_MODULE is defined as ((struct module *)0) when the driver is built-in, this check should thus be removed.

Quoted 99 lines
drivers/base/dma-buf.c +6 -1
 			  || !ops->map_dma_buf
 			  || !ops->unmap_dma_buf
 			  || !ops->release
@@ -105,9 +108,14 @@ struct dma_buf *dma_buf_export(void *priv, const struct
 dma_buf_ops *ops, return ERR_PTR(-EINVAL);
 	}
 
+	if (!try_module_get(ops->owner))
+		return ERR_PTR(-ENOENT);
+
 	dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL);
-	if (dmabuf == NULL)
+	if (dmabuf == NULL) {
+		module_put(ops->owner);
 		return ERR_PTR(-ENOMEM);
+	}
 
 	dmabuf->priv = priv;
 	dmabuf->ops = ops;
(quoted diff) +0 -0

b/drivers/gpu/drm/exynos/exynos_drm_dmabuf.c index 613bf8a..cf3bc6d 100644

drivers/gpu/drm/exynos/exynos_drm_dmabuf.c +1 -0
@@ -164,6 +164,7 @@ static void exynos_gem_dmabuf_kunmap(struct dma_buf
 *dma_buf, }
 
 static struct dma_buf_ops exynos_dmabuf_ops = {
+	.owner			= THIS_MODULE,
 	.map_dma_buf		= exynos_gem_map_dma_buf,
 	.unmap_dma_buf		= exynos_gem_unmap_dma_buf,
 	.kmap			= exynos_gem_dmabuf_kmap,
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/i915_gem_dmabuf.c index aa308e1..07ff03b 100644

drivers/gpu/drm/i915/i915_gem_dmabuf.c +1 -0
@@ -152,6 +152,7 @@ static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf,
 struct vm_area_struct * }
 
 static const struct dma_buf_ops i915_dmabuf_ops =  {
+	.owner = THIS_MODULE,
 	.map_dma_buf = i915_gem_map_dma_buf,
 	.unmap_dma_buf = i915_gem_unmap_dma_buf,
 	.release = i915_gem_dmabuf_release,
(quoted diff) +0 -0

b/drivers/gpu/drm/nouveau/nouveau_prime.c index a25cf2c..8605033 100644

drivers/gpu/drm/nouveau/nouveau_prime.c +1 -0
@@ -127,6 +127,7 @@ static void nouveau_gem_prime_vunmap(struct dma_buf
 *dma_buf, void *vaddr) }
 
 static const struct dma_buf_ops nouveau_dmabuf_ops =  {
+	.owner = THIS_MODULE,
 	.map_dma_buf = nouveau_gem_map_dma_buf,
 	.unmap_dma_buf = nouveau_gem_unmap_dma_buf,
 	.release = nouveau_gem_dmabuf_release,
(quoted diff) +0 -0

b/drivers/gpu/drm/radeon/radeon_prime.c index 6bef46a..4061fd3 100644

drivers/gpu/drm/radeon/radeon_prime.c +1 -0
@@ -127,6 +127,7 @@ static void radeon_gem_prime_vunmap(struct dma_buf
 *dma_buf, void *vaddr) mutex_unlock(&dev->struct_mutex);
 }
 const static struct dma_buf_ops radeon_dmabuf_ops =  {
+	.owner = THIS_MODULE,
 	.map_dma_buf = radeon_gem_map_dma_buf,
 	.unmap_dma_buf = radeon_gem_unmap_dma_buf,
 	.release = radeon_gem_dmabuf_release,
(quoted diff) +0 -0

b/drivers/staging/omapdrm/omap_gem_dmabuf.c index 42728e0..6a4dd67 100644

drivers/staging/omapdrm/omap_gem_dmabuf.c +1 -0
@@ -179,6 +179,7 @@ out_unlock:
 }
 
 struct dma_buf_ops omap_dmabuf_ops = {
+		.owner = THIS_MODULE,
 		.map_dma_buf = omap_gem_map_dma_buf,
 		.unmap_dma_buf = omap_gem_unmap_dma_buf,
 		.release = omap_gem_dmabuf_release,
include/linux/dma-buf.h +1 -0
index eb48f38..22953de 100644
@@ -37,6 +37,7 @@ struct dma_buf_attachment;
 
 /**
  * struct dma_buf_ops - operations possible on struct dma_buf
+ * @owner: the module that implements dma_buf operations
  * @attach: [optional] allows different devices to 'attach' themselves to

the * given buffer. It might return -EBUSY to signal that backing
storage * is already allocated and incompatible with the requirements

include/linux/dma-buf.h +1 -0
@@ -70,6 +71,7 @@ struct dma_buf_attachment;
  * @vunmap: [optional] unmaps a vmap from the buffer
  */
 struct dma_buf_ops {
+	struct module *owner;
 	int (*attach)(struct dma_buf *, struct device *,
 			struct dma_buf_attachment *);
Signature · 3 lines
Regards,

Laurent Pinchart
#2 lore raw

Hi Laurent,

Quoted 21 lines · Laurent Pinchart

Hi Tomasz,

Thanks for the patch.

Quoted 16 lines · On Wednesday 08 August 2012 12:17:41 Tomasz Stanislawski wrote:

This patch adds reference counting on a module that exports dma-buf and implements its operations. This prevents the module from being unloaded while DMABUF file is in use.

---
 Documentation/dma-buf-sharing.txt          |    3 ++-
 drivers/base/dma-buf.c                     |   10 +++++++++-
 drivers/gpu/drm/exynos/exynos_drm_dmabuf.c |    1 +
 drivers/gpu/drm/i915/i915_gem_dmabuf.c     |    1 +
 drivers/gpu/drm/nouveau/nouveau_prime.c    |    1 +
 drivers/gpu/drm/radeon/radeon_prime.c      |    1 +
 drivers/staging/omapdrm/omap_gem_dmabuf.c  |    1 +
 include/linux/dma-buf.h                    |    2 ++
 8 files changed, 18 insertions(+), 2 deletions(-)

[snip]

Quoted 5 lines

drivers/base/dma-buf.c struct dma_buf *dma_buf_export(void *priv, const struct · line 101

Quoted 5 lines
drivers/base/dma-buf.c +1 -0
@@ -96,6 +98,7 @@ struct dma_buf *dma_buf_export(void *priv, const struct
 dma_buf_ops *ops, struct file *file;
 
 	if (WARN_ON(!priv || !ops
+			  || !ops->owner

Thank you for spotting this.
I didn'y know that try_get_module returned true is module was NULL.

BTW. Is it worth to add ".owner = THIS_MODULE," to all dma_buf exporters in this patch?

Regards,
Tomasz Stanislawski

Quoted 8 lines

THIS_MODULE is defined as ((struct module *)0) when the driver is built-in, this check should thus be removed.

Quoted 4 lines
drivers/base/dma-buf.c +0 -0
 			  || !ops->map_dma_buf
 			  || !ops->unmap_dma_buf
 			  || !ops->release
#3 lore raw
Quoted 29 lines · Tomasz Stanislawski

Hi Laurent,

Quoted 21 lines · Laurent Pinchart

Hi Tomasz,

Thanks for the patch.

Quoted 16 lines · On Wednesday 08 August 2012 12:17:41 Tomasz Stanislawski wrote:

This patch adds reference counting on a module that exports dma-buf and implements its operations. This prevents the module from being unloaded while DMABUF file is in use.

---
 Documentation/dma-buf-sharing.txt          |    3 ++-
 drivers/base/dma-buf.c                     |   10 +++++++++-
 drivers/gpu/drm/exynos/exynos_drm_dmabuf.c |    1 +
 drivers/gpu/drm/i915/i915_gem_dmabuf.c     |    1 +
 drivers/gpu/drm/nouveau/nouveau_prime.c    |    1 +
 drivers/gpu/drm/radeon/radeon_prime.c      |    1 +
 drivers/staging/omapdrm/omap_gem_dmabuf.c  |    1 +
 include/linux/dma-buf.h                    |    2 ++
 8 files changed, 18 insertions(+), 2 deletions(-)

[snip]

Quoted 3 lines
Quoted 3 lines
(quoted diff) +0 -0
@@ -96,6 +98,7 @@ struct dma_buf *dma_buf_export(void *priv, const struct
 dma_buf_ops *ops, struct file *file;
 
Quoted 8 lines
Quoted 2 lines

drivers/base/dma-buf.c struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops, · line 101

Quoted 2 lines
drivers/base/dma-buf.c +1 -0
 	if (WARN_ON(!priv || !ops
+			  || !ops->owner

Thank you for spotting this.
I didn'y know that try_get_module returned true is module was NULL.

BTW. Is it worth to add ".owner = THIS_MODULE," to all dma_buf exporters in this patch?

Yeah, I think that makes sense. Otherwise it might get lost somewhere, i.e. you can smash my Ack on this.
-Daniel

Signature · 3 lines
Daniel Vetter
Mail: [email protected]
Mobile: +41 (0)79 365 57 48
maintainer
#4 lore raw

Hi Tomasz,

Quoted 7 lines · Daniel Vetter
Quoted 6 lines · Tomasz Stanislawski

Hi Laurent,

Quoted 3 lines · Laurent Pinchart

Hi Tomasz,

Thanks for the patch.

Thanks for the patch; may I ask you to split it into 2 patches (1 dma-buf and 1 drm) and submit? That ways, either Dave or I can take the patches for either pull request.
With that, please feel free to add my Acked-by as well.

Quoted 39 lines
Quoted 23 lines
Quoted 18 lines · On Wednesday 08 August 2012 12:17:41 Tomasz Stanislawski wrote:
Quoted 16 lines

This patch adds reference counting on a module that exports dma-buf and implements its operations. This prevents the module from being unloaded while DMABUF file is in use.

---
 Documentation/dma-buf-sharing.txt          |    3 ++-
 drivers/base/dma-buf.c                     |   10 +++++++++-
 drivers/gpu/drm/exynos/exynos_drm_dmabuf.c |    1 +
 drivers/gpu/drm/i915/i915_gem_dmabuf.c     |    1 +
 drivers/gpu/drm/nouveau/nouveau_prime.c    |    1 +
 drivers/gpu/drm/radeon/radeon_prime.c      |    1 +
 drivers/staging/omapdrm/omap_gem_dmabuf.c  |    1 +
 include/linux/dma-buf.h                    |    2 ++
 8 files changed, 18 insertions(+), 2 deletions(-)

[snip]

Quoted 3 lines
Quoted 3 lines
(quoted diff) +0 -0
@@ -96,6 +98,7 @@ struct dma_buf *dma_buf_export(void *priv, const struct
 dma_buf_ops *ops, struct file *file;
 
Quoted 8 lines
Quoted 2 lines

drivers/base/dma-buf.c struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops, · line 101

Quoted 2 lines
drivers/base/dma-buf.c +1 -0
   if (WARN_ON(!priv || !ops
+                    || !ops->owner

Thank you for spotting this.
I didn'y know that try_get_module returned true is module was NULL.

BTW. Is it worth to add ".owner = THIS_MODULE," to all dma_buf exporters in this patch?

Yeah, I think that makes sense. Otherwise it might get lost somewhere, i.e. you can smash my Ack on this.
-Daniel
--
Daniel Vetter
Mail: [email protected]
Mobile: +41 (0)79 365 57 48

Signature · 2 lines
Thanks and regards,
Sumit Semwal.