Skip to contentSkip to conversation

drm/radeon: check bo_va->bo is non-NULL before using it

PATCH Superseded Pierre-Eric Pelloux-Prayer 2 participants 2 messages Jun 25, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/radeon: check bo_va->bo is non-NULL before using it

The call to radeon_vm_clear_freed might clear bo_va->bo, so we have to check it before dereferencing it.

---
 drivers/gpu/drm/radeon/radeon_gem.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/gpu/drm/radeon/radeon_gem.c +10 -1
index 3fec3acdaf28..0cf1a72091b7 100644
@@ -641,8 +641,17 @@ static void radeon_gem_va_update_vm(struct radeon_device *rdev,
 	if (r)
 		goto error_unlock;
 
-	if (bo_va->it.start)
+	if (bo_va->it.start) {
+		if (bo_va->bo == NULL) {
+			/* Buggy userspace can try to use RADEON_VA_UNMAP after
+			 * closing the BO. In this case, radeon_vm_clear_freed
+			 * will unset bo_va->bo.
+			 */
+			r = -ENOENT;
+			goto error_unlock;
+		}
 		r = radeon_vm_bo_update(rdev, bo_va, bo_va->bo->tbo.resource);
+	}
 
 error_unlock:
 	mutex_unlock(&bo_va->vm->mutex);
Signature · 1 line
2.45.2
maintainer
#1 lore raw

Am 25.06.24 um 14:41 schrieb Pierre-Eric Pelloux-Prayer:

Quoted 13 lines

The call to radeon_vm_clear_freed might clear bo_va->bo, so we have to check it before dereferencing it.

---
  drivers/gpu/drm/radeon/radeon_gem.c | 11 ++++++++++-
  1 file changed, 10 insertions(+), 1 deletion(-)
drivers/gpu/drm/radeon/radeon_gem.c +0 -0
index 3fec3acdaf28..0cf1a72091b7 100644

drivers/gpu/drm/radeon/radeon_gem.c static void radeon_gem_va_update_vm(struct radeon_device *rdev, · line 645

Quoted 6 lines
drivers/gpu/drm/radeon/radeon_gem.c +2 -1
@@ -641,8 +641,17 @@ static void radeon_gem_va_update_vm(struct radeon_device *rdev,
  	if (r)
  		goto error_unlock;
 
-	if (bo_va->it.start)
+	if (bo_va->it.start) {
+		if (bo_va->bo == NULL) {

In the kernel you should probably use !bo_va->bo instead.

drivers/gpu/drm/radeon/radeon_gem.c static void radeon_gem_va_update_vm(struct radeon_device *rdev, · line 649

Quoted 4 lines
drivers/gpu/drm/radeon/radeon_gem.c +4 -0
+			/* Buggy userspace can try to use RADEON_VA_UNMAP after
+			 * closing the BO. In this case, radeon_vm_clear_freed
+			 * will unset bo_va->bo.
+			 */

I think you can drop that comment, it's perfectly valid for userspace to unmap a region after closing the BO.

It's just that the radeon VM support was never designed very well.

Regards,
Christian.

Quoted 8 lines
drivers/gpu/drm/radeon/radeon_gem.c +4 -0
+			r = -ENOENT;
+			goto error_unlock;
+		}
  		r = radeon_vm_bo_update(rdev, bo_va, bo_va->bo->tbo.resource);
+	}
 
  error_unlock:
  	mutex_unlock(&bo_va->vm->mutex);