Skip to contentSkip to conversation

drm/shmem-helper: Fix a couple of error path bugs

PATCH Superseded Rob Clark 3 participants 10 messages Nov 29, 2022 – Nov 30, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Rob Clark

author
#0 lore raw

cover drm/shmem-helper: Fix a couple of error path bugs

From: Rob Clark <[email protected]>

A couple fixes for error paths that userspace could manage to trigger.

Rob Clark (2):
drm/shmem-helper: Remove errant put in error path drm/shmem-helper: Avoid vm_open error paths

 drivers/gpu/drm/drm_gem_shmem_helper.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
Signature · 1 line
2.38.1
author
#1 lore raw

PATCH 1/2 drm/shmem-helper: Remove errant put in error path

From: Rob Clark <[email protected]>

drm_gem_shmem_mmap() doesn't own this reference!

Fixes 2194a63a818d ("drm: Add library for shmem backed GEM objects")
---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 1 -
 1 file changed, 1 deletion(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +0 -1
index 35138f8a375c..110a9eac2af8 100644
@@ -623,7 +623,6 @@ int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct
 
 	ret = drm_gem_shmem_get_pages(shmem);
 	if (ret) {
-		drm_gem_vm_close(vma);
 		return ret;
 	}
 
Signature · 1 line
2.38.1
author
#4 lore raw

PATCH 2/2 drm/shmem-helper: Avoid vm_open error paths

In reply to Rob Clark

From: Rob Clark <[email protected]>

vm_open() is not allowed to fail. Fortunately we are guaranteed that the pages are already pinned, and only need to increment the refcnt. So just increment it directly.

Fixes 2194a63a818d ("drm: Add library for shmem backed GEM objects")
---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +11 -3
index 110a9eac2af8..9885ba64127f 100644
@@ -571,12 +571,20 @@ static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
 {
 	struct drm_gem_object *obj = vma->vm_private_data;
 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
-	int ret;
 
 	WARN_ON(shmem->base.import_attach);
 
-	ret = drm_gem_shmem_get_pages(shmem);
-	WARN_ON_ONCE(ret != 0);
+	mutex_lock(&shmem->pages_lock);
+
+	/*
+	 * We should have already pinned the pages, vm_open() just grabs
+	 * an additional reference for the new mm the vma is getting
+	 * copied into.
+	 */
+	WARN_ON_ONCE(!shmem->pages_use_count);
+
+	shmem->pages_use_count++;
+	mutex_unlock(&shmem->pages_lock);
 
 	drm_gem_vm_open(vma);
 }
Signature · 1 line
2.38.1
#2 lore raw

Re: [1/2] drm/shmem-helper: Remove errant put in error path

In reply to Rob Clark
Quoted 4 lines · Rob Clark

From: Rob Clark <[email protected]>

drm_gem_shmem_mmap() doesn't own this reference!

I think the impact should be explained further.

Quoted 12 lines
Fixes 2194a63a818d ("drm: Add library for shmem backed GEM objects")
---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 1 -
 1 file changed, 1 deletion(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +0 -0
index 35138f8a375c..110a9eac2af8 100644

drivers/gpu/drm/drm_gem_shmem_helper.c int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct · line 627

Quoted 6 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +0 -1
@@ -623,7 +623,6 @@ int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct
 
 	ret = drm_gem_shmem_get_pages(shmem);
 	if (ret) {
-		drm_gem_vm_close(vma);
 		return ret;
 	}

Drop the now unnecessary { }

Also, consider adding Reported-by: and possibly Suggested-by:.

Thanks,
Guenter

#5 lore raw

Re: [2/2] drm/shmem-helper: Avoid vm_open error paths

In reply to Rob Clark
Quoted 5 lines · Rob Clark

From: Rob Clark <[email protected]>

vm_open() is not allowed to fail. Fortunately we are guaranteed that the pages are already pinned, and only need to increment the refcnt. So just increment it directly.

I don't know anything about drm or gem, but I am wondering _how_ this would be guaranteed. Would it be through the pin function ? Just wondering, because that function does not seem to be mandatory.

Quoted 16 lines
Fixes 2194a63a818d ("drm: Add library for shmem backed GEM objects")
---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +0 -0
index 110a9eac2af8..9885ba64127f 100644
@@ -571,12 +571,20 @@ static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
 {
 	struct drm_gem_object *obj = vma->vm_private_data;
 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);

drivers/gpu/drm/drm_gem_shmem_helper.c static void drm_gem_shmem_vm_open(struct vm_area_struct *vma) · line 580

Quoted 10 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +4 -3
-	int ret;
 
 	WARN_ON(shmem->base.import_attach);
 
-	ret = drm_gem_shmem_get_pages(shmem);
-	WARN_ON_ONCE(ret != 0);
+	mutex_lock(&shmem->pages_lock);
+
+	/*
+	 * We should have already pinned the pages, vm_open() just grabs

should or guaranteed ? This sounds a bit weaker than the commit description.

drivers/gpu/drm/drm_gem_shmem_helper.c static void drm_gem_shmem_vm_open(struct vm_area_struct *vma) · line 587

Quoted 7 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +7 -0
+	 * an additional reference for the new mm the vma is getting
+	 * copied into.
+	 */
+	WARN_ON_ONCE(!shmem->pages_use_count);
+
+	shmem->pages_use_count++;
+	mutex_unlock(&shmem->pages_lock);

The previous code, in that situation, would not increment pages_use_count, and it would not set not set shmem->pages. Hopefully, it would not try to do anything with the pages it was unable to get. The new code assumes that shmem->pages is valid even if pages_use_count is 0, while at the same time taking into account that this can possibly happen (or the WARN_ON_ONCE would not be needed).

Again, I don't know anything about gem and drm, but it seems to me that there might now be a severe problem later on if the WARN_ON_ONCE() ever triggers.

Thanks,
Guenter

Quoted 3 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +0 -0
 	drm_gem_vm_open(vma);
 }
author
#6 lore raw

Re: [2/2] drm/shmem-helper: Avoid vm_open error paths

Quoted 11 lines · Guenter Roeck
Quoted 5 lines · Rob Clark

From: Rob Clark <[email protected]>

vm_open() is not allowed to fail. Fortunately we are guaranteed that the pages are already pinned, and only need to increment the refcnt. So just increment it directly.

I don't know anything about drm or gem, but I am wondering _how_ this would be guaranteed. Would it be through the pin function ? Just wondering, because that function does not seem to be mandatory.

We've pinned the pages already in mmap.. vm->open() is perhaps not the best name for the callback function, but it is called for copying an existing vma into a new process (and for some other cases which do not apply here because VM_DONTEXPAND).

(Other drivers pin pages in the fault handler, where there is actually potential to return an error, but that change was a bit more like re-writing shmem helper ;-))

BR,
-R

Quoted 55 lines
Quoted 16 lines
Fixes 2194a63a818d ("drm: Add library for shmem backed GEM objects")
---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +0 -0
index 110a9eac2af8..9885ba64127f 100644
@@ -571,12 +571,20 @@ static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
 {
      struct drm_gem_object *obj = vma->vm_private_data;
      struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);

drivers/gpu/drm/drm_gem_shmem_helper.c static void drm_gem_shmem_vm_open(struct vm_area_struct *vma) · line 580

Quoted 10 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +4 -3
-     int ret;
 
      WARN_ON(shmem->base.import_attach);
 
-     ret = drm_gem_shmem_get_pages(shmem);
-     WARN_ON_ONCE(ret != 0);
+     mutex_lock(&shmem->pages_lock);
+
+     /*
+      * We should have already pinned the pages, vm_open() just grabs

should or guaranteed ? This sounds a bit weaker than the commit description.

drivers/gpu/drm/drm_gem_shmem_helper.c static void drm_gem_shmem_vm_open(struct vm_area_struct *vma) · line 587

Quoted 7 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +7 -0
+      * an additional reference for the new mm the vma is getting
+      * copied into.
+      */
+     WARN_ON_ONCE(!shmem->pages_use_count);
+
+     shmem->pages_use_count++;
+     mutex_unlock(&shmem->pages_lock);

The previous code, in that situation, would not increment pages_use_count, and it would not set not set shmem->pages. Hopefully, it would not try to do anything with the pages it was unable to get. The new code assumes that shmem->pages is valid even if pages_use_count is 0, while at the same time taking into account that this can possibly happen (or the WARN_ON_ONCE would not be needed).

Again, I don't know anything about gem and drm, but it seems to me that there might now be a severe problem later on if the WARN_ON_ONCE() ever triggers.

Thanks,
Guenter

Quoted 3 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +0 -0
      drm_gem_vm_open(vma);
 }
#7 lore raw

Re: [2/2] drm/shmem-helper: Avoid vm_open error paths

Quoted 13 lines · Rob Clark
Quoted 11 lines · Guenter Roeck
Quoted 5 lines · Rob Clark

From: Rob Clark <[email protected]>

vm_open() is not allowed to fail. Fortunately we are guaranteed that the pages are already pinned, and only need to increment the refcnt. So just increment it directly.

I don't know anything about drm or gem, but I am wondering _how_ this would be guaranteed. Would it be through the pin function ? Just wondering, because that function does not seem to be mandatory.

Quoted 9 lines

We've pinned the pages already in mmap.. vm->open() is perhaps not the best name for the callback function, but it is called for copying an existing vma into a new process (and for some other cases which do not apply here because VM_DONTEXPAND).

(Other drivers pin pages in the fault handler, where there is actually potential to return an error, but that change was a bit more like re-writing shmem helper ;-))

Maybe add a bit of that (where the pinning happened) to the commit description and to the patch itself ?

Quoted 33 lines

BR,
-R

Quoted 30 lines
Quoted 16 lines
Fixes 2194a63a818d ("drm: Add library for shmem backed GEM objects")
---
  drivers/gpu/drm/drm_gem_shmem_helper.c | 14 +++++++++++---
  1 file changed, 11 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +0 -0
index 110a9eac2af8..9885ba64127f 100644
@@ -571,12 +571,20 @@ static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
  {
       struct drm_gem_object *obj = vma->vm_private_data;
       struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);

drivers/gpu/drm/drm_gem_shmem_helper.c static void drm_gem_shmem_vm_open(struct vm_area_struct *vma) · line 580

Quoted 10 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +4 -3
-     int ret;
 
       WARN_ON(shmem->base.import_attach);
 
-     ret = drm_gem_shmem_get_pages(shmem);
-     WARN_ON_ONCE(ret != 0);
+     mutex_lock(&shmem->pages_lock);
+
+     /*
+      * We should have already pinned the pages, vm_open() just grabs

should or guaranteed ? This sounds a bit weaker than the commit description.

like ... the pages were already pinned in (mmap function).

Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/drm_gem_shmem_helper.c static void drm_gem_shmem_vm_open(struct vm_area_struct *vma) · line 584

Quoted 4 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +4 -0
+      * an additional reference for the new mm the vma is getting
+      * copied into.
+      */
+     WARN_ON_ONCE(!shmem->pages_use_count);

If the code can't be trusted and still needs the warning, how about something like the following ?

	if (WARN_ON_ONCE(!shmem->pages_use_count)) {
		mutex_unlock(&shmem->pages_lock);
		return;
	}

Thanks,
Guenter

Quoted 21 lines
Quoted 21 lines

drivers/gpu/drm/drm_gem_shmem_helper.c static void drm_gem_shmem_vm_open(struct vm_area_struct *vma) · line 587

Quoted 3 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +3 -0
+
+     shmem->pages_use_count++;
+     mutex_unlock(&shmem->pages_lock);

The previous code, in that situation, would not increment pages_use_count, and it would not set not set shmem->pages. Hopefully, it would not try to do anything with the pages it was unable to get. The new code assumes that shmem->pages is valid even if pages_use_count is 0, while at the same time taking into account that this can possibly happen (or the WARN_ON_ONCE would not be needed).

Again, I don't know anything about gem and drm, but it seems to me that there might now be a severe problem later on if the WARN_ON_ONCE() ever triggers.

Thanks,
Guenter

Quoted 3 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +0 -0
       drm_gem_vm_open(vma);
  }
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] drm/shmem-helper: Remove errant put in error path

In reply to Rob Clark
Quoted 7 lines · Rob Clark

From: Rob Clark <[email protected]>

drm_gem_shmem_mmap() doesn't own this reference!

Fixes 2194a63a818d ("drm: Add library for shmem backed GEM objects")

With Guenter's nits addressed:

Quoted 19 lines
---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 1 -
 1 file changed, 1 deletion(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +0 -1
index 35138f8a375c..110a9eac2af8 100644
@@ -623,7 +623,6 @@ int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct
 
 	ret = drm_gem_shmem_get_pages(shmem);
 	if (ret) {
-		drm_gem_vm_close(vma);
 		return ret;
 	}
 
Signature · 1 line
2.38.1
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#8 lore raw

Re: [2/2] drm/shmem-helper: Avoid vm_open error paths

In reply to Rob Clark
Quoted 13 lines · Rob Clark
Quoted 11 lines · Guenter Roeck
Quoted 5 lines · Rob Clark

From: Rob Clark <[email protected]>

vm_open() is not allowed to fail. Fortunately we are guaranteed that the pages are already pinned, and only need to increment the refcnt. So just increment it directly.

I don't know anything about drm or gem, but I am wondering _how_ this would be guaranteed. Would it be through the pin function ? Just wondering, because that function does not seem to be mandatory.

Quoted 8 lines

We've pinned the pages already in mmap.. vm->open() is perhaps not the best name for the callback function, but it is called for copying an existing vma into a new process (and for some other cases which do not apply here because VM_DONTEXPAND).

(Other drivers pin pages in the fault handler, where there is actually potential to return an error, but that change was a bit more like re-writing shmem helper ;-))

Yhea vm_ops->open should really be called vm_ops->dupe or ->copy or something like that ...
-Daniel

Quoted 59 lines

BR,
-R

Quoted 55 lines
Quoted 16 lines
Fixes 2194a63a818d ("drm: Add library for shmem backed GEM objects")
---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +0 -0
index 110a9eac2af8..9885ba64127f 100644
@@ -571,12 +571,20 @@ static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
 {
      struct drm_gem_object *obj = vma->vm_private_data;
      struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);

drivers/gpu/drm/drm_gem_shmem_helper.c static void drm_gem_shmem_vm_open(struct vm_area_struct *vma) · line 580

Quoted 10 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +4 -3
-     int ret;
 
      WARN_ON(shmem->base.import_attach);
 
-     ret = drm_gem_shmem_get_pages(shmem);
-     WARN_ON_ONCE(ret != 0);
+     mutex_lock(&shmem->pages_lock);
+
+     /*
+      * We should have already pinned the pages, vm_open() just grabs

should or guaranteed ? This sounds a bit weaker than the commit description.

drivers/gpu/drm/drm_gem_shmem_helper.c static void drm_gem_shmem_vm_open(struct vm_area_struct *vma) · line 587

Quoted 7 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +7 -0
+      * an additional reference for the new mm the vma is getting
+      * copied into.
+      */
+     WARN_ON_ONCE(!shmem->pages_use_count);
+
+     shmem->pages_use_count++;
+     mutex_unlock(&shmem->pages_lock);

The previous code, in that situation, would not increment pages_use_count, and it would not set not set shmem->pages. Hopefully, it would not try to do anything with the pages it was unable to get. The new code assumes that shmem->pages is valid even if pages_use_count is 0, while at the same time taking into account that this can possibly happen (or the WARN_ON_ONCE would not be needed).

Again, I don't know anything about gem and drm, but it seems to me that there might now be a severe problem later on if the WARN_ON_ONCE() ever triggers.

Thanks,
Guenter

Quoted 3 lines
drivers/gpu/drm/drm_gem_shmem_helper.c +0 -0
      drm_gem_vm_open(vma);
 }
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#9 lore raw

Re: [PATCH 2/2] drm/shmem-helper: Avoid vm_open error paths

In reply to Rob Clark
Quoted 5 lines · Rob Clark

From: Rob Clark <[email protected]>

vm_open() is not allowed to fail. Fortunately we are guaranteed that the pages are already pinned, and only need to increment the refcnt. So just increment it directly.

Please mention hare that the only issue is the mutex_lock_interruptible, and the only way we've found to hit this is if you send a signal to the original process in a fork() at _just_ the right time.

With that: Reviewed-by: Daniel Vetter <[email protected]>

Quoted 37 lines
Fixes 2194a63a818d ("drm: Add library for shmem backed GEM objects")
---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +11 -4
index 110a9eac2af8..9885ba64127f 100644
@@ -571,12 +571,20 @@ static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
 {
      struct drm_gem_object *obj = vma->vm_private_data;
      struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
-     int ret;
 
      WARN_ON(shmem->base.import_attach);
 
-     ret = drm_gem_shmem_get_pages(shmem);
-     WARN_ON_ONCE(ret != 0);
+     mutex_lock(&shmem->pages_lock);
+
+     /*
+      * We should have already pinned the pages, vm_open() just grabs
+      * an additional reference for the new mm the vma is getting
+      * copied into.
+      */
+     WARN_ON_ONCE(!shmem->pages_use_count);
+
+     shmem->pages_use_count++;
+     mutex_unlock(&shmem->pages_lock);
 
      drm_gem_vm_open(vma);
 }
--
Quoted 2 lines

2.38.1

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch