Skip to contentSkip to conversation

drm/amdgpu: Fix null pointer access of BO

PATCH Superseded xinhui pan 2 participants 2 messages Dec 2, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

xinhui pan

author
#0 lore raw

PATCH 1/1 drm/amdgpu: Fix null pointer access of BO

TTM want bo->resource to be valid during BO's life.
But ttm_bo_mem_space might fail and bo->resource point to NULL. Many code touch bo->resource and hit panic then.

As old and new mem might overlap, move ttm_resource_free after ttm_bo_mem_space is not an option.
We could assign one sysmem node to BO to make bo->resource valid.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +12 -2
index c4317343967f..697fac0b82a3 100644
@@ -359,6 +359,7 @@ int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
 			       struct amdgpu_bo **bo_ptr, void **cpu_addr)
 {
 	struct ttm_operation_ctx ctx = { false, false };
+	struct ttm_resource *tmp_res;
 	unsigned int i;
 	int r;
 
@@ -380,17 +381,26 @@ int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
 	if (cpu_addr)
 		amdgpu_bo_kunmap(*bo_ptr);
 
-	ttm_resource_free(&(*bo_ptr)->tbo, &(*bo_ptr)->tbo.resource);
+	/* Assign one sysmem node to BO as we want bo->resource to be valid. */
+	amdgpu_bo_placement_from_domain(*bo_ptr, AMDGPU_GEM_DOMAIN_CPU);
+	r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement,
+			     &tmp_res, &ctx);
+	if (r)
+		goto error;
+
+	ttm_bo_move_null(&(*bo_ptr)->tbo, tmp_res);
 
 	for (i = 0; i < (*bo_ptr)->placement.num_placement; ++i) {
 		(*bo_ptr)->placements[i].fpfn = offset >> PAGE_SHIFT;
 		(*bo_ptr)->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
 	}
 	r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement,
-			     &(*bo_ptr)->tbo.resource, &ctx);
+			     &tmp_res, &ctx);
 	if (r)
 		goto error;
 
+	ttm_bo_move_null(&(*bo_ptr)->tbo, tmp_res);
+
 	if (cpu_addr) {
 		r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
 		if (r)
Signature · 1 line
2.25.1
maintainer
#1 lore raw

Am 02.12.21 um 05:55 schrieb xinhui pan:

Quoted 28 lines

TTM want bo->resource to be valid during BO's life.
But ttm_bo_mem_space might fail and bo->resource point to NULL. Many code touch bo->resource and hit panic then.

As old and new mem might overlap, move ttm_resource_free after ttm_bo_mem_space is not an option.
We could assign one sysmem node to BO to make bo->resource valid.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 14 ++++++++++++--
  1 file changed, 12 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -0
index c4317343967f..697fac0b82a3 100644
@@ -359,6 +359,7 @@ int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
  			       struct amdgpu_bo **bo_ptr, void **cpu_addr)
  {
  	struct ttm_operation_ctx ctx = { false, false };
+	struct ttm_resource *tmp_res;
  	unsigned int i;
  	int r;
 
@@ -380,17 +381,26 @@ int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
  	if (cpu_addr)
  		amdgpu_bo_kunmap(*bo_ptr);

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev, · line 391

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +8 -1
 
-	ttm_resource_free(&(*bo_ptr)->tbo, &(*bo_ptr)->tbo.resource);
+	/* Assign one sysmem node to BO as we want bo->resource to be valid. */
+	amdgpu_bo_placement_from_domain(*bo_ptr, AMDGPU_GEM_DOMAIN_CPU);
+	r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement,
+			     &tmp_res, &ctx);
+	if (r)
+		goto error;
+
+	ttm_bo_move_null(&(*bo_ptr)->tbo, tmp_res);

That's much easier fixable if you just create the BO in the CPU domain in the first place.

E.g. modify the call to amdgpu_bo_create_reserved() above to use AMDGPU_GEM_DOMAIN_CPU instead of the provided one.

This way you can indeed move the ttm_resource_free() after the assignment.

Regards,
Christian.

Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +3 -1
  	for (i = 0; i < (*bo_ptr)->placement.num_placement; ++i) {
  		(*bo_ptr)->placements[i].fpfn = offset >> PAGE_SHIFT;
  		(*bo_ptr)->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
  	}
  	r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement,
-			     &(*bo_ptr)->tbo.resource, &ctx);
+			     &tmp_res, &ctx);
  	if (r)
  		goto error;
 
+	ttm_bo_move_null(&(*bo_ptr)->tbo, tmp_res);
+
  	if (cpu_addr) {
  		r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
  		if (r)