Skip to contentSkip to conversation

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

PATCH In review Thomas Zimmermann 3 participants 4 messages Jan 15, 2021

Subsystems (2)
lore
Share & tools
lore

Thomas Zimmermann

author maintainer
#0 lore raw

PATCH v2 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 already led 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.

v2:
* support (vm_ops == NULL) if mmap is given; required by VRAM helpers

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   | 19 ++++++++++---------
 drivers/gpu/drm/drm_prime.c |  2 ++
 2 files changed, 12 insertions(+), 9 deletions(-)
drivers/gpu/drm/drm_gem.c +10 -9
index 34b2f111c01c..c2ce78c4edc3 100644
@@ -1068,20 +1068,17 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
 	drm_gem_object_get(obj);
 
 	vma->vm_private_data = obj;
+	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;
+		if (!vma->vm_ops) {
+			ret = -EINVAL;
+			goto err_drm_gem_object_put;
 		}
 
 		vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
@@ -1090,6 +1087,10 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
 	}
 
 	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 +2 -0
index 683aa29ecd3b..2a54f86856af 100644
@@ -717,6 +717,8 @@ 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) {
+		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
#1 lore raw

Hi Thomas,

Quoted 6 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 already led 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.

Quoted 8 lines

v2:
* support (vm_ops == NULL) if mmap is given; required by VRAM helpers

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

Re-tested just fine this side ;-)
- https://paste.ubuntu.com/p/Jgz6xMKNJX/

Thanks

Kieran

Quoted 59 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   | 19 ++++++++++---------
 drivers/gpu/drm/drm_prime.c |  2 ++
 2 files changed, 12 insertions(+), 9 deletions(-)
drivers/gpu/drm/drm_gem.c +10 -9
index 34b2f111c01c..c2ce78c4edc3 100644
@@ -1068,20 +1068,17 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
 	drm_gem_object_get(obj);
 
 	vma->vm_private_data = obj;
+	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;
+		if (!vma->vm_ops) {
+			ret = -EINVAL;
+			goto err_drm_gem_object_put;
 		}
 
 		vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
@@ -1090,6 +1087,10 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
 	}
 
 	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..2a54f86856af 100644

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

Quoted 8 lines
drivers/gpu/drm/drm_prime.c +2 -0
@@ -717,6 +717,8 @@ 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) {
+		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

Reviewed-by
#2 lore raw
Quoted 12 lines · Kieran Bingham

Hi Thomas,

Quoted 9 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 already led 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.

v2:
* support (vm_ops == NULL) if mmap is given; required by VRAM helpers

I guess vram helpers need this because ttm has it's own vm_ops struct? Might be another thing worth unifying (eventually).

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

Re-tested just fine this side ;-)
- https://paste.ubuntu.com/p/Jgz6xMKNJX/

Quoted 65 lines

Thanks

Kieran

Quoted 60 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   | 19 ++++++++++---------
 drivers/gpu/drm/drm_prime.c |  2 ++
 2 files changed, 12 insertions(+), 9 deletions(-)
drivers/gpu/drm/drm_gem.c +10 -9
index 34b2f111c01c..c2ce78c4edc3 100644
@@ -1068,20 +1068,17 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
 	drm_gem_object_get(obj);
 
 	vma->vm_private_data = obj;
+	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;
+		if (!vma->vm_ops) {
+			ret = -EINVAL;
+			goto err_drm_gem_object_put;
 		}
 
 		vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
@@ -1090,6 +1087,10 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
 	}
 
 	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..2a54f86856af 100644
@@ -717,6 +717,8 @@ 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);
 
Quoted 3 lines

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

Quoted 3 lines
drivers/gpu/drm/drm_prime.c +2 -0
 	if (obj->funcs && obj->funcs->mmap) {
+		vma->vm_ops = obj->funcs->vm_ops;
+

Do you know how much we still need the non-obj->funcs path here? Maybe time to detele it and wrape the obj->funcs check in a WARN_ON? -Daniel

Quoted 5 lines
Quoted 4 lines
drivers/gpu/drm/drm_prime.c +0 -0
 		ret = obj->funcs->mmap(obj, vma);
 		if (ret)
 			return ret;
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#3 lore raw

Hi

Am 15.01.21 um 15:11 schrieb Daniel Vetter:

Quoted 10 lines · Kieran Bingham
Quoted 9 lines

Hi Thomas,

Quoted 6 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 already led 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.

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

v2:
* support (vm_ops == NULL) if mmap is given; required by VRAM helpers

I guess vram helpers need this because ttm has it's own vm_ops struct? Might be another thing worth unifying (eventually).

I've been working on converting drivers to struct drm_gem_object_funcs.mmap and using DRM helpers for the other mmap callbacks. Part of this would include some unifying of the vm_ops handling. TTM drivers are kind of special here.

Best regards
Thomas

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

Re-tested just fine this side ;-)
- https://paste.ubuntu.com/p/Jgz6xMKNJX/

Quoted 65 lines

Thanks

Kieran

Quoted 60 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   | 19 ++++++++++---------
  drivers/gpu/drm/drm_prime.c |  2 ++
  2 files changed, 12 insertions(+), 9 deletions(-)
drivers/gpu/drm/drm_gem.c +10 -9
index 34b2f111c01c..c2ce78c4edc3 100644
@@ -1068,20 +1068,17 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  	drm_gem_object_get(obj);
 
  	vma->vm_private_data = obj;
+	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;
+		if (!vma->vm_ops) {
+			ret = -EINVAL;
+			goto err_drm_gem_object_put;
  		}
 
  		vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
@@ -1090,6 +1087,10 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  	}
 
  	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..2a54f86856af 100644
@@ -717,6 +717,8 @@ 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);
 
Quoted 3 lines

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

Quoted 3 lines
drivers/gpu/drm/drm_prime.c +2 -0
  	if (obj->funcs && obj->funcs->mmap) {
+		vma->vm_ops = obj->funcs->vm_ops;
+

Do you know how much we still need the non-obj->funcs path here? Maybe time to detele it and wrape the obj->funcs check in a WARN_ON? -Daniel

Quoted 5 lines
Quoted 4 lines
drivers/gpu/drm/drm_prime.c +0 -0
  		ret = obj->funcs->mmap(obj, vma);
  		if (ret)
  			return ret;
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