Skip to contentSkip to conversation

drm: Set vm_ops to GEM object's values during mmap

PATCH Superseded Thomas Zimmermann 2 participants 4 messages Jan 15, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Thomas Zimmermann

author maintainer
#0 lore raw

PATCH 1/1 drm: Set vm_ops to GEM object's values during mmap

The GEM mmap code relies on the GEM object's mmap callback to set the VMA's vm_ops field. This is easily forgotten and lead to a memory leak in the CMA helpers. Instead set the vm_ops field in the DRM core code to the GEM object's value. Drivers with different needs can override this in their mmap callback.

Fixes f5ca8eb6f9bd ("drm/cma-helper: Implement mmap as GEM CMA object functions")
Cc Maxime Ripard <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc David Airlie <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc Eric Anholt <[email protected]>
---
 drivers/gpu/drm/drm_gem.c   | 23 ++++++++++++-----------
 drivers/gpu/drm/drm_prime.c |  4 ++++
 2 files changed, 16 insertions(+), 11 deletions(-)
drivers/gpu/drm/drm_gem.c +12 -11
index 34b2f111c01c..54d95621fcbb 100644
@@ -1069,27 +1069,28 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
 
 	vma->vm_private_data = obj;
 
+	if (!obj->funcs->vm_ops) {
+		ret = -EINVAL;
+		goto err_drm_gem_object_put;
+	}
+	vma->vm_ops = obj->funcs->vm_ops;
+
 	if (obj->funcs->mmap) {
 		ret = obj->funcs->mmap(obj, vma);
-		if (ret) {
-			drm_gem_object_put(obj);
-			return ret;
-		}
+		if (ret)
+			goto err_drm_gem_object_put;
 		WARN_ON(!(vma->vm_flags & VM_DONTEXPAND));
 	} else {
-		if (obj->funcs->vm_ops)
-			vma->vm_ops = obj->funcs->vm_ops;
-		else {
-			drm_gem_object_put(obj);
-			return -EINVAL;
-		}
-
 		vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
 		vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
 		vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
 	}
 
 	return 0;
+
+err_drm_gem_object_put:
+	drm_gem_object_put(obj);
+	return ret;
 }
 EXPORT_SYMBOL(drm_gem_mmap_obj);
 
drivers/gpu/drm/drm_prime.c +4 -0
index 683aa29ecd3b..0549ec190583 100644
@@ -717,6 +717,10 @@ int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
 	vma->vm_pgoff += drm_vma_node_start(&obj->vma_node);
 
 	if (obj->funcs && obj->funcs->mmap) {
+		if (!obj->funcs->vm_ops)
+			return -EINVAL;
+		vma->vm_ops = obj->funcs->vm_ops;
+
 		ret = obj->funcs->mmap(obj, vma);
 		if (ret)
 			return ret;
Signature · 6 lines
2.29.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Tested-by on v1 1/1
#1 lore raw

Hi Thomas,

Quoted 2 lines · Thomas Zimmermann

The GEM mmap code relies on the GEM object's mmap callback to set the VMA's vm_ops field. This is easily forgotten and lead to a memory leak

s/lead/can lead/

Quoted 7 lines

in the CMA helpers. Instead set the vm_ops field in the DRM core code to the GEM object's value. Drivers with different needs can override this in their mmap callback.

Fixes f5ca8eb6f9bd ("drm/cma-helper: Implement mmap as GEM CMA object functions")

This also works here.
- https://paste.ubuntu.com/p/S2Q586rgwT/

Quoted 61 lines
Cc Maxime Ripard <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc David Airlie <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc Eric Anholt <[email protected]>
---
 drivers/gpu/drm/drm_gem.c   | 23 ++++++++++++-----------
 drivers/gpu/drm/drm_prime.c |  4 ++++
 2 files changed, 16 insertions(+), 11 deletions(-)
drivers/gpu/drm/drm_gem.c +12 -11
index 34b2f111c01c..54d95621fcbb 100644
@@ -1069,27 +1069,28 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
 
 	vma->vm_private_data = obj;
 
+	if (!obj->funcs->vm_ops) {
+		ret = -EINVAL;
+		goto err_drm_gem_object_put;
+	}
+	vma->vm_ops = obj->funcs->vm_ops;
+
 	if (obj->funcs->mmap) {
 		ret = obj->funcs->mmap(obj, vma);
-		if (ret) {
-			drm_gem_object_put(obj);
-			return ret;
-		}
+		if (ret)
+			goto err_drm_gem_object_put;
 		WARN_ON(!(vma->vm_flags & VM_DONTEXPAND));
 	} else {
-		if (obj->funcs->vm_ops)
-			vma->vm_ops = obj->funcs->vm_ops;
-		else {
-			drm_gem_object_put(obj);
-			return -EINVAL;
-		}
-
 		vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
 		vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
 		vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
 	}
 
 	return 0;
+
+err_drm_gem_object_put:
+	drm_gem_object_put(obj);
+	return ret;
 }
 EXPORT_SYMBOL(drm_gem_mmap_obj);
 
drivers/gpu/drm/drm_prime.c +0 -0
index 683aa29ecd3b..0549ec190583 100644

drivers/gpu/drm/drm_prime.c int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma) · line 726

Quoted 10 lines
drivers/gpu/drm/drm_prime.c +4 -0
@@ -717,6 +717,10 @@ int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
 	vma->vm_pgoff += drm_vma_node_start(&obj->vma_node);
 
 	if (obj->funcs && obj->funcs->mmap) {
+		if (!obj->funcs->vm_ops)
+			return -EINVAL;
+		vma->vm_ops = obj->funcs->vm_ops;
+
 		ret = obj->funcs->mmap(obj, vma);
 		if (ret)
 			return ret;

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

author maintainer
#2 lore raw

Hi

Am 15.01.21 um 10:17 schrieb Kieran Bingham:

Quoted 12 lines

Hi Thomas,

Quoted 2 lines · Thomas Zimmermann

The GEM mmap code relies on the GEM object's mmap callback to set the VMA's vm_ops field. This is easily forgotten and lead to a memory leak

s/lead/can lead/

Quoted 4 lines

in the CMA helpers. Instead set the vm_ops field in the DRM core code to the GEM object's value. Drivers with different needs can override this in their mmap callback.

Quoted 8 lines
Quoted 3 lines
Fixes f5ca8eb6f9bd ("drm/cma-helper: Implement mmap as GEM CMA object functions")

This also works here.
- https://paste.ubuntu.com/p/S2Q586rgwT/

Great! I have to send out another iteration of the patch, but I think the change is such that I can keep the Tested-by.

Best regards
Thomas

Quoted 78 lines
Quoted 62 lines
Cc Maxime Ripard <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc David Airlie <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc Eric Anholt <[email protected]>
---
  drivers/gpu/drm/drm_gem.c   | 23 ++++++++++++-----------
  drivers/gpu/drm/drm_prime.c |  4 ++++
  2 files changed, 16 insertions(+), 11 deletions(-)
drivers/gpu/drm/drm_gem.c +12 -11
index 34b2f111c01c..54d95621fcbb 100644
@@ -1069,27 +1069,28 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
 
  	vma->vm_private_data = obj;
 
+	if (!obj->funcs->vm_ops) {
+		ret = -EINVAL;
+		goto err_drm_gem_object_put;
+	}
+	vma->vm_ops = obj->funcs->vm_ops;
+
  	if (obj->funcs->mmap) {
  		ret = obj->funcs->mmap(obj, vma);
-		if (ret) {
-			drm_gem_object_put(obj);
-			return ret;
-		}
+		if (ret)
+			goto err_drm_gem_object_put;
  		WARN_ON(!(vma->vm_flags & VM_DONTEXPAND));
  	} else {
-		if (obj->funcs->vm_ops)
-			vma->vm_ops = obj->funcs->vm_ops;
-		else {
-			drm_gem_object_put(obj);
-			return -EINVAL;
-		}
-
  		vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
  		vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  		vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
  	}
 
  	return 0;
+
+err_drm_gem_object_put:
+	drm_gem_object_put(obj);
+	return ret;
  }
  EXPORT_SYMBOL(drm_gem_mmap_obj);
 
drivers/gpu/drm/drm_prime.c +0 -0
index 683aa29ecd3b..0549ec190583 100644

drivers/gpu/drm/drm_prime.c int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma) · line 726

Quoted 10 lines
drivers/gpu/drm/drm_prime.c +4 -0
@@ -717,6 +717,10 @@ int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
  	vma->vm_pgoff += drm_vma_node_start(&obj->vma_node);
 
  	if (obj->funcs && obj->funcs->mmap) {
+		if (!obj->funcs->vm_ops)
+			return -EINVAL;
+		vma->vm_ops = obj->funcs->vm_ops;
+
  		ret = obj->funcs->mmap(obj, vma);
  		if (ret)
  			return ret;

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

Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Felix Imendörffer

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

#3 lore raw
Quoted 20 lines · Thomas Zimmermann

Hi

Am 15.01.21 um 10:17 schrieb Kieran Bingham:

Quoted 17 lines

Hi Thomas,

Quoted 2 lines · Thomas Zimmermann

The GEM mmap code relies on the GEM object's mmap callback to set the VMA's vm_ops field. This is easily forgotten and lead to a memory leak

  s/lead/can lead/

Quoted 8 lines

in the CMA helpers. Instead set the vm_ops field in the DRM core code to the GEM object's value. Drivers with different needs can override this in their mmap callback.

Fixes f5ca8eb6f9bd ("drm/cma-helper: Implement mmap as GEM CMA

object functions")

Quoted 7 lines
Quoted 4 lines

This also works here.
  - https://paste.ubuntu.com/p/S2Q586rgwT/

Great! I have to send out another iteration of the patch, but I think the change is such that I can keep the Tested-by.

I have this test automated now, so I'll kick off a validation run on the new patch as well.

--
Kieran

Quoted 82 lines

Best regards
Thomas

Quoted 77 lines
Quoted 61 lines
Cc Maxime Ripard <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc David Airlie <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc Eric Anholt <[email protected]>

---
  drivers/gpu/drm/drm_gem.c   | 23 ++++++++++++----------- drivers/gpu/drm/drm_prime.c |  4 ++++

  2 files changed, 16 insertions(+), 11 deletions(-)
drivers/gpu/drm/drm_gem.c +7 -11
index 34b2f111c01c..54d95621fcbb 100644
@@ -1069,27 +1069,28 @@ int drm_gem_mmap_obj(struct drm_gem_object
 *obj, unsigned long obj_size,
         vma->vm_private_data = obj;
   +    if (!obj->funcs->vm_ops) {
+        ret = -EINVAL;
+        goto err_drm_gem_object_put;
+    }
+    vma->vm_ops = obj->funcs->vm_ops;
+
       if (obj->funcs->mmap) {
           ret = obj->funcs->mmap(obj, vma);
-        if (ret) {
-            drm_gem_object_put(obj);
-            return ret;
-        }
+        if (ret)
+            goto err_drm_gem_object_put;
           WARN_ON(!(vma->vm_flags & VM_DONTEXPAND));
       } else {
-        if (obj->funcs->vm_ops)
-            vma->vm_ops = obj->funcs->vm_ops;
-        else {
-            drm_gem_object_put(obj);
-            return -EINVAL;
-        }
-

          vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
          vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot); }
        return 0;

drivers/gpu/drm/drm_gem.c +4 -0
+
+err_drm_gem_object_put:
+    drm_gem_object_put(obj);
+    return ret;

  }
  EXPORT_SYMBOL(drm_gem_mmap_obj);
  diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c index 683aa29ecd3b..0549ec190583 100644

drivers/gpu/drm/drm_prime.c int drm_gem_prime_mmap(struct drm_gem_object · line 726

Quoted 10 lines
drivers/gpu/drm/drm_prime.c +4 -0
@@ -717,6 +717,10 @@ int drm_gem_prime_mmap(struct drm_gem_object
 *obj, struct vm_area_struct *vma)
       vma->vm_pgoff += drm_vma_node_start(&obj->vma_node);
         if (obj->funcs && obj->funcs->mmap) {
+        if (!obj->funcs->vm_ops)
+            return -EINVAL;
+        vma->vm_ops = obj->funcs->vm_ops;
+
           ret = obj->funcs->mmap(obj, vma);
           if (ret)
               return ret;

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

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