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

Signed-off-by: Shiwu Zhang <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
index 576372ead63e..60a1d86ba9fe 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
@@ -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

Reply via email to