Skip to contentSkip to conversation

drm/gem: add mutex lock when using drm_gem_mmap_obj

PATCH Superseded Seung-Woo Kim 5 participants 8 messages Jun 26, 2013 – Jun 27, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/gem: add mutex lock when using drm_gem_mmap_obj

From: YoungJun Cho <[email protected]>

The drm_gem_mmap_obj() has to be protected with dev->struct_mutex, but some caller functions do not. So it adds mutex lock to missing callers and adds WARN_ON assertion whether drm_gem_mmap_obj() is called with mutex lock or not.

---
This patch is based on drm-next branch.

 drivers/gpu/drm/drm_gem.c                 |    4 ++++
 drivers/gpu/drm/drm_gem_cma_helper.c      |    3 +++
 drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c |    3 +++
 3 files changed, 10 insertions(+), 0 deletions(-)
drivers/gpu/drm/drm_gem.c +4 -0
index 4321713..b19bba0 100644
@@ -661,6 +661,8 @@ EXPORT_SYMBOL(drm_gem_vm_close);
  * the GEM object is not looked up based on its fake offset. To implement the
  * DRM mmap operation, drivers should use the drm_gem_mmap() function.
  *
+ * NOTE: This function has to be protected with dev->struct_mutex
+ *
  * Return 0 or success or -EINVAL if the object size is smaller than the VMA
  * size, or if no gem_vm_ops are provided.
  */
@@ -669,6 +671,8 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
 {
 	struct drm_device *dev = obj->dev;
 
+	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
+
 	/* Check for valid size. */
 	if (obj_size < vma->vm_end - vma->vm_start)
 		return -EINVAL;
drivers/gpu/drm/drm_gem_cma_helper.c +3 -0
index 9efabce..ce06397 100644
@@ -487,9 +487,12 @@ static int drm_gem_cma_dmabuf_mmap(struct dma_buf *dmabuf,
 {
 	struct drm_gem_cma_object *cma_obj = dmabuf->priv;
 	struct drm_gem_object *gem_obj = &cma_obj->base;
+	struct drm_device *dev = gem_obj->dev;
 	int ret;
 
+	mutex_lock(&dev->struct_mutex);
 	ret = drm_gem_mmap_obj(gem_obj, gem_obj->size, vma);
+	mutex_unlock(&dev->struct_mutex);
 	if (ret < 0)
 		return ret;
 
drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c +3 -0
index 3256693..4fcca8d 100644
@@ -140,12 +140,15 @@ static int omap_gem_dmabuf_mmap(struct dma_buf *buffer,
 		struct vm_area_struct *vma)
 {
 	struct drm_gem_object *obj = buffer->priv;
+	struct drm_device *dev = obj->dev;
 	int ret = 0;
 
 	if (WARN_ON(!obj->filp))
 		return -EINVAL;
 
+	mutex_lock(&dev->struct_mutex);
 	ret = drm_gem_mmap_obj(obj, omap_gem_mmap_size(obj), vma);
+	mutex_unlock(&dev->struct_mutex);
 	if (ret < 0)
 		return ret;
 
Signature · 1 line
1.7.4.1
#1 lore raw

Op 26-06-13 04:14, Seung-Woo Kim schreef:

Quoted 34 lines

From: YoungJun Cho <[email protected]>

The drm_gem_mmap_obj() has to be protected with dev->struct_mutex, but some caller functions do not. So it adds mutex lock to missing callers and adds WARN_ON assertion whether drm_gem_mmap_obj() is called with mutex lock or not.

---
This patch is based on drm-next branch.

 drivers/gpu/drm/drm_gem.c                 |    4 ++++
 drivers/gpu/drm/drm_gem_cma_helper.c      |    3 +++
 drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c |    3 +++
 3 files changed, 10 insertions(+), 0 deletions(-)
drivers/gpu/drm/drm_gem.c +2 -0
index 4321713..b19bba0 100644
@@ -661,6 +661,8 @@ EXPORT_SYMBOL(drm_gem_vm_close);
  * the GEM object is not looked up based on its fake offset. To implement the
  * DRM mmap operation, drivers should use the drm_gem_mmap() function.
  *
+ * NOTE: This function has to be protected with dev->struct_mutex
+ *
  * Return 0 or success or -EINVAL if the object size is smaller than the VMA
  * size, or if no gem_vm_ops are provided.
  */

drivers/gpu/drm/drm_gem.c int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size, · line 675

Quoted 5 lines
drivers/gpu/drm/drm_gem.c +1 -0
@@ -669,6 +671,8 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
 {
 	struct drm_device *dev = obj->dev;
 
+	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
 

Please don't use mutex_is_locked, use lockdep_assert_held, so the cost only exists when PROVE_LOCKING is used..

I know some current code does it wrong, but that is the correct function to use.

~Maarten

#2 lore raw
Quoted 22 lines · Maarten Lankhorst

Op 26-06-13 04:14, Seung-Woo Kim schreef:

Quoted 20 lines

From: YoungJun Cho <[email protected]>

The drm_gem_mmap_obj() has to be protected with dev->struct_mutex, but some caller functions do not. So it adds mutex lock to missing callers and adds WARN_ON assertion whether drm_gem_mmap_obj() is called with mutex lock or not.

---
This patch is based on drm-next branch.

 drivers/gpu/drm/drm_gem.c                 |    4 ++++
 drivers/gpu/drm/drm_gem_cma_helper.c      |    3 +++
 drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c |    3 +++
 3 files changed, 10 insertions(+), 0 deletions(-)
Quoted 6 lines

drivers/gpu/drm/drm_gem.c EXPORT_SYMBOL(drm_gem_vm_close); · line 661

Quoted 6 lines
drivers/gpu/drm/drm_gem.c +0 -0
index 4321713..b19bba0 100644
@@ -661,6 +661,8 @@ EXPORT_SYMBOL(drm_gem_vm_close);
  * the GEM object is not looked up based on its fake offset. To

implement the

Quoted 5 lines

drivers/gpu/drm/drm_gem.c EXPORT_SYMBOL(drm_gem_vm_close);

Quoted 5 lines
drivers/gpu/drm/drm_gem.c +2 -0
  * DRM mmap operation, drivers should use the drm_gem_mmap() function.
  *
+ * NOTE: This function has to be protected with dev->struct_mutex
+ *
  * Return 0 or success or -EINVAL if the object size is smaller than

the VMA

Quoted 3 lines

drivers/gpu/drm/drm_gem.c int drm_gem_mmap_obj(struct drm_gem_object *obj,

Quoted 3 lines
drivers/gpu/drm/drm_gem.c +0 -0
  * size, or if no gem_vm_ops are provided.
  */
@@ -669,6 +671,8 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj,

unsigned long obj_size,

Quoted 6 lines

drivers/gpu/drm/drm_gem.c int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,

Quoted 5 lines
drivers/gpu/drm/drm_gem.c +1 -0
 {
      struct drm_device *dev = obj->dev;
 
+     WARN_ON(!mutex_is_locked(&dev->struct_mutex));
 

Please don't use mutex_is_locked, use lockdep_assert_held, so the cost

only exists when PROVE_LOCKING is used..

Quoted 2 lines

I know some current code does it wrong, but that is the correct function

to use.

Quoted 3 lines

~Maarten

Thank you for nice comments!
I will update it again.

Best regards YJ
_______________________________________________

Quoted 3 lines

dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw

PATCH v2 1/1 drm/gem: add mutex lock when using drm_gem_mmap_obj

In reply to Maarten Lankhorst

From: YoungJun Cho <[email protected]>

The drm_gem_mmap_obj() has to be protected with dev->struct_mutex, but some caller functions do not. So it adds mutex lock to missing callers and adds assertion to check whether drm_gem_mmap_obj() is called with mutex lock or not.

---
This patch is based on drm-next branch.

Changes since v1:
- Use lockdep_assert_held() instead of mutex_is_locked() as Maarten commented
- Fix commit message about assertion

 drivers/gpu/drm/drm_gem.c                 |    4 ++++
 drivers/gpu/drm/drm_gem_cma_helper.c      |    3 +++
 drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c |    3 +++
 3 files changed, 10 insertions(+)
drivers/gpu/drm/drm_gem.c +4 -0
index 4321713..34c0be7 100644
@@ -661,6 +661,8 @@ EXPORT_SYMBOL(drm_gem_vm_close);
  * the GEM object is not looked up based on its fake offset. To implement the
  * DRM mmap operation, drivers should use the drm_gem_mmap() function.
  *
+ * NOTE: This function has to be protected with dev->struct_mutex
+ *
  * Return 0 or success or -EINVAL if the object size is smaller than the VMA
  * size, or if no gem_vm_ops are provided.
  */
@@ -669,6 +671,8 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
 {
 	struct drm_device *dev = obj->dev;
 
+	lockdep_assert_held(&dev->struct_mutex);
+
 	/* Check for valid size. */
 	if (obj_size < vma->vm_end - vma->vm_start)
 		return -EINVAL;
drivers/gpu/drm/drm_gem_cma_helper.c +3 -0
index 9efabce..ce06397 100644
@@ -487,9 +487,12 @@ static int drm_gem_cma_dmabuf_mmap(struct dma_buf *dmabuf,
 {
 	struct drm_gem_cma_object *cma_obj = dmabuf->priv;
 	struct drm_gem_object *gem_obj = &cma_obj->base;
+	struct drm_device *dev = gem_obj->dev;
 	int ret;
 
+	mutex_lock(&dev->struct_mutex);
 	ret = drm_gem_mmap_obj(gem_obj, gem_obj->size, vma);
+	mutex_unlock(&dev->struct_mutex);
 	if (ret < 0)
 		return ret;
 
drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c +3 -0
index 3256693..4fcca8d 100644
@@ -140,12 +140,15 @@ static int omap_gem_dmabuf_mmap(struct dma_buf *buffer,
 		struct vm_area_struct *vma)
 {
 	struct drm_gem_object *obj = buffer->priv;
+	struct drm_device *dev = obj->dev;
 	int ret = 0;
 
 	if (WARN_ON(!obj->filp))
 		return -EINVAL;
 
+	mutex_lock(&dev->struct_mutex);
 	ret = drm_gem_mmap_obj(obj, omap_gem_mmap_size(obj), vma);
+	mutex_unlock(&dev->struct_mutex);
 	if (ret < 0)
 		return ret;
 
Signature · 1 line
1.7.9.5
Reviewed-by on v2 1/1
#4 lore raw

Op 27-06-13 01:39, Seung-Woo Kim schreef:

Quoted 7 lines

From: YoungJun Cho <[email protected]>

The drm_gem_mmap_obj() has to be protected with dev->struct_mutex, but some caller functions do not. So it adds mutex lock to missing callers and adds assertion to check whether drm_gem_mmap_obj() is called with mutex lock or not.

Quoted 7 lines

---

Reviewed-by on v2 1/1
#5 lore raw
In reply to Seung-Woo Kim

Hi,

Quoted 7 lines · On Thursday 27 June 2013 08:39:58 Seung-Woo Kim wrote:

From: YoungJun Cho <[email protected]>

The drm_gem_mmap_obj() has to be protected with dev->struct_mutex, but some caller functions do not. So it adds mutex lock to missing callers and adds assertion to check whether drm_gem_mmap_obj() is called with mutex lock or not.

Quoted 5 lines

Thanks for the patch.

Quoted 70 lines

---
This patch is based on drm-next branch.

Changes since v1:
- Use lockdep_assert_held() instead of mutex_is_locked() as Maarten commented
- Fix commit message about assertion

 drivers/gpu/drm/drm_gem.c                 |    4 ++++
 drivers/gpu/drm/drm_gem_cma_helper.c      |    3 +++
 drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c |    3 +++
 3 files changed, 10 insertions(+)
drivers/gpu/drm/drm_gem.c +0 -0
index 4321713..34c0be7 100644
@@ -661,6 +661,8 @@ EXPORT_SYMBOL(drm_gem_vm_close);
  * the GEM object is not looked up based on its fake offset. To implement

the * DRM mmap operation, drivers should use the drm_gem_mmap() function. *
+ * NOTE: This function has to be protected with dev->struct_mutex
+ *
* Return 0 or success or -EINVAL if the object size is smaller than the
VMA * size, or if no gem_vm_ops are provided.
*/

drivers/gpu/drm/drm_gem.c +2 -0
@@ -669,6 +671,8 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj,
 unsigned long obj_size, {
 	struct drm_device *dev = obj->dev;
 
+	lockdep_assert_held(&dev->struct_mutex);
+
 	/* Check for valid size. */
 	if (obj_size < vma->vm_end - vma->vm_start)
 		return -EINVAL;
(quoted diff) +0 -0

b/drivers/gpu/drm/drm_gem_cma_helper.c index 9efabce..ce06397 100644

drivers/gpu/drm/drm_gem_cma_helper.c +3 -0
@@ -487,9 +487,12 @@ static int drm_gem_cma_dmabuf_mmap(struct dma_buf
 *dmabuf, {
 	struct drm_gem_cma_object *cma_obj = dmabuf->priv;
 	struct drm_gem_object *gem_obj = &cma_obj->base;
+	struct drm_device *dev = gem_obj->dev;
 	int ret;
 
+	mutex_lock(&dev->struct_mutex);
 	ret = drm_gem_mmap_obj(gem_obj, gem_obj->size, vma);
+	mutex_unlock(&dev->struct_mutex);
 	if (ret < 0)
 		return ret;
 
(quoted diff) +0 -0

b/drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c index 3256693..4fcca8d 100644

drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c +3 -0
@@ -140,12 +140,15 @@ static int omap_gem_dmabuf_mmap(struct dma_buf
 *buffer, struct vm_area_struct *vma)
 {
 	struct drm_gem_object *obj = buffer->priv;
+	struct drm_device *dev = obj->dev;
 	int ret = 0;
 
 	if (WARN_ON(!obj->filp))
 		return -EINVAL;
 
+	mutex_lock(&dev->struct_mutex);
 	ret = drm_gem_mmap_obj(obj, omap_gem_mmap_size(obj), vma);
+	mutex_unlock(&dev->struct_mutex);
 	if (ret < 0)
 		return ret;
Signature · 3 lines
Regards,

Laurent Pinchart
Reviewed-by on v2 1/1
#6 lore raw
In reply to Seung-Woo Kim
Quoted 6 lines · Seung-Woo Kim

From: YoungJun Cho <[email protected]>

The drm_gem_mmap_obj() has to be protected with dev->struct_mutex, but some caller functions do not. So it adds mutex lock to missing callers and adds assertion to check whether drm_gem_mmap_obj() is called with mutex lock or not.

Yeah, looks like since drm_gem_mmap_obj() is using drm_vm_open_locked() (vs drm_vm_open() / vm_ops->open()), we need this. I missed that when reviewing the original patch to drm_gem_mmap_obj()'ify things.

Quoted 79 lines

---
This patch is based on drm-next branch.

Changes since v1:
- Use lockdep_assert_held() instead of mutex_is_locked() as Maarten commented
- Fix commit message about assertion

 drivers/gpu/drm/drm_gem.c                 |    4 ++++
 drivers/gpu/drm/drm_gem_cma_helper.c      |    3 +++
 drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c |    3 +++
 3 files changed, 10 insertions(+)
drivers/gpu/drm/drm_gem.c +4 -0
index 4321713..34c0be7 100644
@@ -661,6 +661,8 @@ EXPORT_SYMBOL(drm_gem_vm_close);
  * the GEM object is not looked up based on its fake offset. To implement the
  * DRM mmap operation, drivers should use the drm_gem_mmap() function.
  *
+ * NOTE: This function has to be protected with dev->struct_mutex
+ *
  * Return 0 or success or -EINVAL if the object size is smaller than the VMA
  * size, or if no gem_vm_ops are provided.
  */
@@ -669,6 +671,8 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
 {
        struct drm_device *dev = obj->dev;
 
+       lockdep_assert_held(&dev->struct_mutex);
+
        /* Check for valid size. */
        if (obj_size < vma->vm_end - vma->vm_start)
                return -EINVAL;
drivers/gpu/drm/drm_gem_cma_helper.c +3 -0
index 9efabce..ce06397 100644
@@ -487,9 +487,12 @@ static int drm_gem_cma_dmabuf_mmap(struct dma_buf *dmabuf,
 {
        struct drm_gem_cma_object *cma_obj = dmabuf->priv;
        struct drm_gem_object *gem_obj = &cma_obj->base;
+       struct drm_device *dev = gem_obj->dev;
        int ret;
 
+       mutex_lock(&dev->struct_mutex);
        ret = drm_gem_mmap_obj(gem_obj, gem_obj->size, vma);
+       mutex_unlock(&dev->struct_mutex);
        if (ret < 0)
                return ret;
 
drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c +3 -1
index 3256693..4fcca8d 100644
@@ -140,12 +140,15 @@ static int omap_gem_dmabuf_mmap(struct dma_buf *buffer,
                struct vm_area_struct *vma)
 {
        struct drm_gem_object *obj = buffer->priv;
+       struct drm_device *dev = obj->dev;
        int ret = 0;
 
        if (WARN_ON(!obj->filp))
                return -EINVAL;
 
+       mutex_lock(&dev->struct_mutex);
        ret = drm_gem_mmap_obj(obj, omap_gem_mmap_size(obj), vma);
+       mutex_unlock(&dev->struct_mutex);
        if (ret < 0)
                return ret;
 
--

1.7.9.5

Reviewed-by on v2 1/1
#7 lore raw
In reply to Seung-Woo Kim

Op 27-06-13 01:39, Seung-Woo Kim schreef:

Quoted 7 lines

From: YoungJun Cho <[email protected]>

The drm_gem_mmap_obj() has to be protected with dev->struct_mutex, but some caller functions do not. So it adds mutex lock to missing callers and adds assertion to check whether drm_gem_mmap_obj() is called with mutex lock or not.

Quoted 6 lines