Skip to contentSkip to conversation

drm/amdgpu/mes: Add NULL check for mes_hung_db_array allocation

PATCH In review Geoffrey McRae 2 participants 2 messages Jun 28 – Jun 29

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

PATCH 1/1 drm/amdgpu/mes: Add NULL check for mes_hung_db_array allocation

kcalloc but does not check for failure. If the allocation fails, the pointer remains NULL but the function returns success. Subsequent code using this buffer will dereference a NULL pointer, causing a kernel oops. Add a check to return -ENOMEM if the allocation fails.

Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +9 -4
index 6c0dde3786e3..261ddc19c840 100644
@@ -250,11 +250,16 @@ int amdgpu_mes_init(struct amdgpu_device *adev)
 				goto error_doorbell;
 			}
 		}
-	}
 
-	adev->gfx.mec.mes_hung_db_array =
-		kcalloc(amdgpu_mes_get_hung_queue_db_array_size(adev),
-			sizeof(u32), GFP_KERNEL);
+		adev->gfx.mec.mes_hung_db_array =
+			kcalloc(amdgpu_mes_get_hung_queue_db_array_size(adev),
+				sizeof(u32), GFP_KERNEL);
+
+		if (!adev->gfx.mec.mes_hung_db_array) {
+			r = -ENOMEM;
+			goto error_doorbell;
+		}
+	}
 
 	return 0;
 
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Geoffrey McRae

kcalloc but does not check for failure. If the allocation fails, the pointer remains NULL but the function returns success. Subsequent code using this buffer will dereference a NULL pointer, causing a kernel oops. Add a check to return -ENOMEM if the allocation fails.

Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
Quoted 32 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +9 -5
index 6c0dde3786e3..261ddc19c840 100644
@@ -250,11 +250,16 @@ int amdgpu_mes_init(struct amdgpu_device *adev)
                                goto error_doorbell;
                        }
                }
-       }
 
-       adev->gfx.mec.mes_hung_db_array =
-               kcalloc(amdgpu_mes_get_hung_queue_db_array_size(adev),
-                       sizeof(u32), GFP_KERNEL);
+               adev->gfx.mec.mes_hung_db_array =
+                       kcalloc(amdgpu_mes_get_hung_queue_db_array_size(adev),
+                               sizeof(u32), GFP_KERNEL);
+
+               if (!adev->gfx.mec.mes_hung_db_array) {
+                       r = -ENOMEM;
+                       goto error_doorbell;
+               }
+       }
 
        return 0;
 
--

2.43.0