Skip to contentSkip to conversation

drm/amdgpu: fix the ww_mutex and GEM reference leaks

PATCH In review Shiwu Zhang 3 participants 5 messages May 20 – May 21

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

PATCH 1/3 drm/amdgpu: fix the ww_mutex and GEM reference leaks

In case of kvcalloc failure, ww_mutex should be released and BO's GEM reference should be dropped as well.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +4 -2
index a93c64732bf4..b5fa4b0affc6 100644
@@ -1261,8 +1261,10 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 		 * be retried.
 		 */
 		vm_entries = kvcalloc(args->num_entries, sizeof(*vm_entries), GFP_KERNEL);
-		if (!vm_entries)
-			return -ENOMEM;
+		if (!vm_entries) {
+			r = -ENOMEM;
+			goto out_exec;
+		}
 
 		amdgpu_vm_bo_va_for_each_valid_mapping(bo_va, mapping) {
 			if (num_mappings < args->num_entries) {
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/3 drm/amdgpu: fix buffer overflow during vBIOS update

Clamp the buffer postion to write by setting the bin attribute to the maximum buffer size so that VFS layer will block the out-of-bounds accessing.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +1 -1
index f88df51a2558..576372ead63e 100644
@@ -5022,7 +5022,7 @@ static const struct bin_attribute psp_vbflash_bin_attr = {
 static struct bin_attribute psp_vbflash_bin_attr = {
 #endif
 	.attr = {.name = "psp_vbflash", .mode = 0660},
-	.size = 0,
+	.size = AMD_VBIOS_FILE_MAX_SIZE_B,
 	.write = amdgpu_psp_vbflash_write,
 	.read = amdgpu_psp_vbflash_read,
 };
Signature · 1 line
2.43.0
#2 lore raw

PATCH 3/3 drm/amdgpu: fix duplicated buffer allocation for concurrent

In reply to Shiwu Zhang

In case of concurrent calling to the bin file writing, use the mutex to avoid allocating the temporary buffer more than once.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +5 -2
index 576372ead63e..60a1d86ba9fe 100644
@@ -4942,14 +4942,17 @@ static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
 		return -ENOMEM;
 	}
 
+	mutex_lock(&adev->psp.mutex);
+
 	/* TODO Just allocate max for now and optimize to realloc later if needed */
 	if (!adev->psp.vbflash_tmp_buf) {
 		adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
-		if (!adev->psp.vbflash_tmp_buf)
+		if (!adev->psp.vbflash_tmp_buf) {
+			mutex_unlock(&adev->psp.mutex);
 			return -ENOMEM;
+		}
 	}
 
-	mutex_lock(&adev->psp.mutex);
 	memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
 	adev->psp.vbflash_image_size += count;
 	mutex_unlock(&adev->psp.mutex);
Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 3/3
#3 lore raw

Re: [PATCH 3/3] drm/amdgpu: fix duplicated buffer allocation for concurrent

AMD General

Series is:

________________________________
From: amd-gfx <[email protected]> on behalf of Shiwu Zhang <[email protected]>
Sent: Wednesday, May 20, 2026 4:34 AM
To: [email protected] <[email protected]>
Subject: [PATCH 3/3] drm/amdgpu: fix duplicated buffer allocation for concurrent

In case of concurrent calling to the bin file writing, use the mutex to avoid allocating the temporary buffer more than once.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +5 -2
index 576372ead63e..60a1d86ba9fe 100644
@@ -4942,14 +4942,17 @@ static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
                 return -ENOMEM;
         }
 
+       mutex_lock(&adev->psp.mutex);
+
         /* TODO Just allocate max for now and optimize to realloc later if needed */
         if (!adev->psp.vbflash_tmp_buf) {
                 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
-               if (!adev->psp.vbflash_tmp_buf)
+               if (!adev->psp.vbflash_tmp_buf) {
+                       mutex_unlock(&adev->psp.mutex);
                         return -ENOMEM;
+               }
         }
 
-       mutex_lock(&adev->psp.mutex);
         memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
         adev->psp.vbflash_image_size += count;
         mutex_unlock(&adev->psp.mutex);

--
2.43.0

Reviewed-by on v1 1/3
#4 lore raw
In reply to Shiwu Zhang

AMD General

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Shiwu Zhang
Sent: Wednesday, May 20, 2026 4:35 PM
To: [email protected]
Subject: [PATCH 1/3] drm/amdgpu: fix the ww_mutex and GEM reference leaks

In case of kvcalloc failure, ww_mutex should be released and BO's GEM reference should be dropped as well.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +4 -2
index a93c64732bf4..b5fa4b0affc6 100644
@@ -1261,8 +1261,10 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
                 * be retried.
                 */
                vm_entries = kvcalloc(args->num_entries, sizeof(*vm_entries), GFP_KERNEL);
-               if (!vm_entries)
-                       return -ENOMEM;
+               if (!vm_entries) {
+                       r = -ENOMEM;
+                       goto out_exec;
+               }
 
                amdgpu_vm_bo_va_for_each_valid_mapping(bo_va, mapping) {
                        if (num_mappings < args->num_entries) {

--
2.43.0