Skip to contentSkip to conversation

drm/ttm: fix use-after-free races in vm fault handling

PATCH In review Nicolai Hähnle 5 participants 6 messages Feb 18, 2017 – Feb 27, 2017 3 messages not in the archive

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

PATCH 1/1 drm/ttm: fix use-after-free races in vm fault handling

From: Nicolai Hähnle <[email protected]>

The vm fault handler relies on the fact that the VMA owns a reference to the BO. However, once mmap_sem is released, other tasks are free to destroy the VMA, which can lead to the BO being freed. Fix two code paths where that can happen, both related to vm fault retries.

Found via a lock debugging warning which flagged &bo->wu_mutex as locked while being destroyed.

Fixes cbe12e74ee4e ("drm/ttm: Allow vm fault retries")

--
This does not fix the random memory corruption I've been seeing.

---
 drivers/gpu/drm/ttm/ttm_bo_vm.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/gpu/drm/ttm/ttm_bo_vm.c +12 -0
index a6ed9d5..750733a 100644
@@ -66,8 +66,11 @@ static int ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo,
 		if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
 			goto out_unlock;
 
+		ttm_bo_reference(bo);
 		up_read(&vma->vm_mm->mmap_sem);
 		(void) fence_wait(bo->moving, true);
+		ttm_bo_unreserve(bo);
+		ttm_bo_unref(&bo);
 		goto out_unlock;
 	}
 
@@ -120,8 +123,10 @@ static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
 
 		if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
 			if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
+				ttm_bo_reference(bo);
 				up_read(&vma->vm_mm->mmap_sem);
 				(void) ttm_bo_wait_unreserved(bo);
+				ttm_bo_unref(&bo);
 			}
 
 			return VM_FAULT_RETRY;
@@ -166,6 +171,13 @@ static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
 	ret = ttm_bo_vm_fault_idle(bo, vma, vmf);
 	if (unlikely(ret != 0)) {
 		retval = ret;
+
+		if (retval == VM_FAULT_RETRY &&
+		    !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
+			/* The BO has already been unreserved. */
+			return retval;
+		}
+
 		goto out_unlock;
 	}
 
Signature · 6 lines
2.9.3

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

Am 18.02.2017 um 23:50 schrieb Nicolai Hähnle:

Quoted 7 lines

From: Nicolai Hähnle <[email protected]>

The vm fault handler relies on the fact that the VMA owns a reference to the BO. However, once mmap_sem is released, other tasks are free to destroy the VMA, which can lead to the BO being freed. Fix two code paths where that can happen, both related to vm fault retries.

Quoted 5 lines

Found via a lock debugging warning which flagged &bo->wu_mutex as locked while being destroyed.

Fixes cbe12e74ee4e ("drm/ttm: Allow vm fault retries")

Good catch! Patch is Reviewed-by: Christian König <[email protected]>

Quoted 37 lines

--
This does not fix the random memory corruption I've been seeing.

---
  drivers/gpu/drm/ttm/ttm_bo_vm.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
drivers/gpu/drm/ttm/ttm_bo_vm.c +5 -0
index a6ed9d5..750733a 100644
@@ -66,8 +66,11 @@ static int ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo,
  		if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
  			goto out_unlock;
 
+		ttm_bo_reference(bo);
  		up_read(&vma->vm_mm->mmap_sem);
  		(void) fence_wait(bo->moving, true);
+		ttm_bo_unreserve(bo);
+		ttm_bo_unref(&bo);
  		goto out_unlock;
  	}
 
@@ -120,8 +123,10 @@ static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
 
  		if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
  			if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
+				ttm_bo_reference(bo);
  				up_read(&vma->vm_mm->mmap_sem);
  				(void) ttm_bo_wait_unreserved(bo);
+				ttm_bo_unref(&bo);
  			}
 
  			return VM_FAULT_RETRY;
@@ -166,6 +171,13 @@ static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  	ret = ttm_bo_vm_fault_idle(bo, vma, vmf);
  	if (unlikely(ret != 0)) {
  		retval = ret;

drivers/gpu/drm/ttm/ttm_bo_vm.c static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) · line 183

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +7 -0
+
+		if (retval == VM_FAULT_RETRY &&
+		    !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
+			/* The BO has already been unreserved. */
+			return retval;
+		}
+
  		goto out_unlock;
  	}
 

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

A message that is not in the archive (sent off-list or not yet received): 71bb618a-7a6a-eedd-e764-301aeb373da0-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#3 lore raw
Quoted 8 lines · Christian König

Am 18.02.2017 um 23:50 schrieb Nicolai Hähnle:

Quoted 7 lines

From: Nicolai Hähnle <[email protected]>

The vm fault handler relies on the fact that the VMA owns a reference to the BO. However, once mmap_sem is released, other tasks are free to destroy the VMA, which can lead to the BO being freed. Fix two code paths where that can happen, both related to vm fault retries.

Quoted 7 lines
Quoted 5 lines

Found via a lock debugging warning which flagged &bo->wu_mutex as locked while being destroyed.

Fixes cbe12e74ee4e ("drm/ttm: Allow vm fault retries")

Good catch! Patch is Reviewed-by: Christian König <[email protected]>

Since you have commit rights and all, care to push to drm-misc.git? -Daniel

Quoted 49 lines
Quoted 33 lines

--
This does not fix the random memory corruption I've been seeing.

---
  drivers/gpu/drm/ttm/ttm_bo_vm.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
drivers/gpu/drm/ttm/ttm_bo_vm.c +5 -0
index a6ed9d5..750733a 100644
@@ -66,8 +66,11 @@ static int ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo,
  		if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
  			goto out_unlock;
+		ttm_bo_reference(bo);
  		up_read(&vma->vm_mm->mmap_sem);
  		(void) fence_wait(bo->moving, true);
+		ttm_bo_unreserve(bo);
+		ttm_bo_unref(&bo);
  		goto out_unlock;
  	}
@@ -120,8 +123,10 @@ static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  		if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
  			if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
+				ttm_bo_reference(bo);
  				up_read(&vma->vm_mm->mmap_sem);
  				(void) ttm_bo_wait_unreserved(bo);
+				ttm_bo_unref(&bo);
  			}
  			return VM_FAULT_RETRY;
@@ -166,6 +171,13 @@ static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  	ret = ttm_bo_vm_fault_idle(bo, vma, vmf);
  	if (unlikely(ret != 0)) {

drivers/gpu/drm/ttm/ttm_bo_vm.c static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) · line 182

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +7 -0
  		retval = ret;
+
+		if (retval == VM_FAULT_RETRY &&
+		    !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
+			/* The BO has already been unreserved. */
+			return retval;
+		}
+
  		goto out_unlock;
  	}

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

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 20170226213526.hejvi2xgeq4u2y7c-dv86pmgwkMBes7Z6vYuT8azUEOm+Xw19@public.gmane.org

#5 lore raw

Am 26.02.2017 um 22:35 schrieb Daniel Vetter:

Quoted 9 lines · Christian König
Quoted 8 lines

Am 18.02.2017 um 23:50 schrieb Nicolai Hähnle:

Quoted 7 lines

From: Nicolai Hähnle <[email protected]>

The vm fault handler relies on the fact that the VMA owns a reference to the BO. However, once mmap_sem is released, other tasks are free to destroy the VMA, which can lead to the BO being freed. Fix two code paths where that can happen, both related to vm fault retries.

Quoted 7 lines
Quoted 6 lines
Quoted 5 lines

Found via a lock debugging warning which flagged &bo->wu_mutex as locked while being destroyed.

Fixes cbe12e74ee4e ("drm/ttm: Allow vm fault retries")

Good catch! Patch is Reviewed-by: Christian König <[email protected]>

Since you have commit rights and all, care to push to drm-misc.git?

Do I have to use dim or could I just push the patches using standard git?

See my problem with installing an extra tool in my dev environment is that I have 5+ hard disks setup from an image with all the neat stuff I need. Distributing something new in there would be rather painful for me.

On the other hand I could just setup my laptop and use that one as a bridge for pushing into drm-misc. That would work for single bug fixes like this, but would break my usual development workflow.

Christian.

Quoted 49 lines

-Daniel

Quoted 47 lines
Quoted 32 lines

--
This does not fix the random memory corruption I've been seeing.

---
   drivers/gpu/drm/ttm/ttm_bo_vm.c | 12 ++++++++++++
   1 file changed, 12 insertions(+)
drivers/gpu/drm/ttm/ttm_bo_vm.c +5 -0
index a6ed9d5..750733a 100644
@@ -66,8 +66,11 @@ static int ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo,
   		if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
   			goto out_unlock;
+		ttm_bo_reference(bo);
   		up_read(&vma->vm_mm->mmap_sem);
   		(void) fence_wait(bo->moving, true);
+		ttm_bo_unreserve(bo);
+		ttm_bo_unref(&bo);
   		goto out_unlock;
   	}
@@ -120,8 +123,10 @@ static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
   		if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
   			if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
+				ttm_bo_reference(bo);
   				up_read(&vma->vm_mm->mmap_sem);
   				(void) ttm_bo_wait_unreserved(bo);
+				ttm_bo_unref(&bo);
   			}
   			return VM_FAULT_RETRY;
@@ -166,6 +171,13 @@ static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
   	ret = ttm_bo_vm_fault_idle(bo, vma, vmf);
   	if (unlikely(ret != 0)) {

drivers/gpu/drm/ttm/ttm_bo_vm.c static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) · line 182

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +7 -0
   		retval = ret;
+
+		if (retval == VM_FAULT_RETRY &&
+		    !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
+			/* The BO has already been unreserved. */
+			return retval;
+		}
+
   		goto out_unlock;
   	}

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

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

A message that is not in the archive (sent off-list or not yet received): d70d368a-477e-a5e8-f429-95fd53d771ce-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#7 lore raw
Quoted 20 lines · Christian König

Am 26.02.2017 um 22:35 schrieb Daniel Vetter:

Quoted 9 lines · Christian König
Quoted 8 lines

Am 18.02.2017 um 23:50 schrieb Nicolai Hähnle:

Quoted 7 lines

From: Nicolai Hähnle <[email protected]>

The vm fault handler relies on the fact that the VMA owns a reference to the BO. However, once mmap_sem is released, other tasks are free to destroy the VMA, which can lead to the BO being freed. Fix two code paths where that can happen, both related to vm fault retries.

Quoted 7 lines
Quoted 6 lines
Quoted 5 lines

Found via a lock debugging warning which flagged &bo->wu_mutex as locked while being destroyed.

Fixes cbe12e74ee4e ("drm/ttm: Allow vm fault retries")

Good catch! Patch is Reviewed-by: Christian König <[email protected]>

Since you have commit rights and all, care to push to drm-misc.git?

Do I have to use dim or could I just push the patches using standard git?

Quoted 7 lines

See my problem with installing an extra tool in my dev environment is that I have 5+ hard disks setup from an image with all the neat stuff I need. Distributing something new in there would be rather painful for me.

On the other hand I could just setup my laptop and use that one as a bridge for pushing into drm-misc. That would work for single bug fixes like this, but would break my usual development workflow.

Atm the big magic in dim is in the integration tree construction and faning out conflict handling to everyone. There's also the upshot of being able to share sanity checks for silly fumbles, but we don't yet have much of these, so dim's indeed needed. I know it's annoying.

I can apply this one here if you don't want to fiddle with it all for now. -Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#8 lore raw
Quoted 29 lines · Daniel Vetter
Quoted 20 lines · Christian König

Am 26.02.2017 um 22:35 schrieb Daniel Vetter:

Quoted 9 lines · Christian König
Quoted 8 lines

Am 18.02.2017 um 23:50 schrieb Nicolai Hähnle:

Quoted 7 lines

From: Nicolai Hähnle <[email protected]>

The vm fault handler relies on the fact that the VMA owns a reference to the BO. However, once mmap_sem is released, other tasks are free to destroy the VMA, which can lead to the BO being freed. Fix two code paths where that can happen, both related to vm fault retries.

Quoted 7 lines
Quoted 6 lines
Quoted 5 lines

Found via a lock debugging warning which flagged &bo->wu_mutex as locked while being destroyed.

Fixes cbe12e74ee4e ("drm/ttm: Allow vm fault retries")

Good catch! Patch is Reviewed-by: Christian König <[email protected]>

Since you have commit rights and all, care to push to drm-misc.git?

Do I have to use dim or could I just push the patches using standard git?

Quoted 7 lines

See my problem with installing an extra tool in my dev environment is that I have 5+ hard disks setup from an image with all the neat stuff I need. Distributing something new in there would be rather painful for me.

On the other hand I could just setup my laptop and use that one as a bridge for pushing into drm-misc. That would work for single bug fixes like this, but would break my usual development workflow.

Quoted 6 lines

Atm the big magic in dim is in the integration tree construction and faning out conflict handling to everyone. There's also the upshot of being able to share sanity checks for silly fumbles, but we don't yet have much of these, so dim's indeed needed. I know it's annoying.

I can apply this one here if you don't want to fiddle with it all for now.

Well just noticed that Alex already applied this to his fixes pull, so all moot.
-Daniel

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