Skip to contentSkip to conversation

drm/amdgpu: clean up psp ip if hw_init failed v2

PATCH In review Alice Wong 2 participants 7 messages Apr 20, 2022 – Apr 28, 2022

Subsystems (2)
lore
Share & tools
lore

Alice Wong

author
#0 lore raw

PATCH 1/1 drm/amdgpu: clean up psp ip if hw_init failed v2

If at any point psp_hw_init failed, psp_hw_fini would not be called during unload due to ip_blocks[PSP].status.hw not being set to true. This could cause a memory leak when the driver unloads. As a rule of thumb, each IP block should cleanup themselves when their hw_init fails. Only previously intialized blocks should be cleaned up by the common framework.

v1: Call IP blocks' respective hw_fini when hw_init failed from
the common framework
v2: Call psp_hw_fini when psp_hw_init failed.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 57 +++++++++++++------------
 1 file changed, 29 insertions(+), 28 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +29 -28
index 5b9e48d44f5b..52b14efa848a 100644
@@ -2537,6 +2537,34 @@ static int psp_load_fw(struct amdgpu_device *adev)
 	return ret;
 }
 
+static int psp_hw_fini(void *handle)
+{
+	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
+	struct psp_context *psp = &adev->psp;
+
+	if (psp->ta_fw) {
+		psp_ras_terminate(psp);
+		psp_securedisplay_terminate(psp);
+		psp_rap_terminate(psp);
+		psp_dtm_terminate(psp);
+		psp_hdcp_terminate(psp);
+	}
+
+	psp_asd_terminate(psp);
+
+	psp_tmr_terminate(psp);
+	psp_ring_destroy(psp, PSP_RING_TYPE__KM);
+
+	amdgpu_bo_free_kernel(&psp->fw_pri_bo,
+			      &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
+	amdgpu_bo_free_kernel(&psp->fence_buf_bo,
+			      &psp->fence_buf_mc_addr, &psp->fence_buf);
+	amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
+			      (void **)&psp->cmd_buf_mem);
+
+	return 0;
+}
+
 static int psp_hw_init(void *handle)
 {
 	int ret;
@@ -2563,37 +2591,10 @@ static int psp_hw_init(void *handle)
 failed:
 	adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
 	mutex_unlock(&adev->firmware.mutex);
+	psp_hw_fini(handle);
 	return -EINVAL;
 }
 
-static int psp_hw_fini(void *handle)
-{
-	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
-	struct psp_context *psp = &adev->psp;
-
-	if (psp->ta_fw) {
-		psp_ras_terminate(psp);
-		psp_securedisplay_terminate(psp);
-		psp_rap_terminate(psp);
-		psp_dtm_terminate(psp);
-		psp_hdcp_terminate(psp);
-	}
-
-	psp_asd_terminate(psp);
-
-	psp_tmr_terminate(psp);
-	psp_ring_destroy(psp, PSP_RING_TYPE__KM);
-
-	amdgpu_bo_free_kernel(&psp->fw_pri_bo,
-			      &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
-	amdgpu_bo_free_kernel(&psp->fence_buf_bo,
-			      &psp->fence_buf_mc_addr, &psp->fence_buf);
-	amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
-			      (void **)&psp->cmd_buf_mem);
-
-	return 0;
-}
-
 static int psp_suspend(void *handle)
 {
 	int ret;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 8 lines · Alice Wong

If at any point psp_hw_init failed, psp_hw_fini would not be called during unload due to ip_blocks[PSP].status.hw not being set to true. This could cause a memory leak when the driver unloads. As a rule of thumb, each IP block should cleanup themselves when their hw_init fails. Only previously intialized blocks should be cleaned up by the common framework.

Quoted 5 lines

v1: Call IP blocks' respective hw_fini when hw_init failed from
the common framework
v2: Call psp_hw_fini when psp_hw_init failed.

I don't think this is a good idea. The hw programming in hw_fini makes no sense if the driver never set it up in the first place if hw_init failed before initializing the hw. It would be better to just properly unwind the relevant functions. Presumably the problem you are seeing is the failure to free the GPU memory allocated in fw_fini, depending on where it fails. We should just allocate the memory in sw_init; that is what we do in other IPs. Does the attached patch fix the issue you are seeing?

Alex

Quoted 83 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 57 +++++++++++++------------
 1 file changed, 29 insertions(+), 28 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +29 -29
index 5b9e48d44f5b..52b14efa848a 100644
@@ -2537,6 +2537,34 @@ static int psp_load_fw(struct amdgpu_device *adev)
        return ret;
 }
 
+static int psp_hw_fini(void *handle)
+{
+       struct amdgpu_device *adev = (struct amdgpu_device *)handle;
+       struct psp_context *psp = &adev->psp;
+
+       if (psp->ta_fw) {
+               psp_ras_terminate(psp);
+               psp_securedisplay_terminate(psp);
+               psp_rap_terminate(psp);
+               psp_dtm_terminate(psp);
+               psp_hdcp_terminate(psp);
+       }
+
+       psp_asd_terminate(psp);
+
+       psp_tmr_terminate(psp);
+       psp_ring_destroy(psp, PSP_RING_TYPE__KM);
+
+       amdgpu_bo_free_kernel(&psp->fw_pri_bo,
+                             &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
+       amdgpu_bo_free_kernel(&psp->fence_buf_bo,
+                             &psp->fence_buf_mc_addr, &psp->fence_buf);
+       amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
+                             (void **)&psp->cmd_buf_mem);
+
+       return 0;
+}
+
 static int psp_hw_init(void *handle)
 {
        int ret;
@@ -2563,37 +2591,10 @@ static int psp_hw_init(void *handle)
 failed:
        adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
        mutex_unlock(&adev->firmware.mutex);
+       psp_hw_fini(handle);
        return -EINVAL;
 }
 
-static int psp_hw_fini(void *handle)
-{
-       struct amdgpu_device *adev = (struct amdgpu_device *)handle;
-       struct psp_context *psp = &adev->psp;
-
-       if (psp->ta_fw) {
-               psp_ras_terminate(psp);
-               psp_securedisplay_terminate(psp);
-               psp_rap_terminate(psp);
-               psp_dtm_terminate(psp);
-               psp_hdcp_terminate(psp);
-       }
-
-       psp_asd_terminate(psp);
-
-       psp_tmr_terminate(psp);
-       psp_ring_destroy(psp, PSP_RING_TYPE__KM);
-
-       amdgpu_bo_free_kernel(&psp->fw_pri_bo,
-                             &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
-       amdgpu_bo_free_kernel(&psp->fence_buf_bo,
-                             &psp->fence_buf_mc_addr, &psp->fence_buf);
-       amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
-                             (void **)&psp->cmd_buf_mem);
-
-       return 0;
-}
-
 static int psp_suspend(void *handle)
 {
        int ret;
--
Quoted 2 lines

2.25.1

From f98a157d52e199c8530dc0efc91ba3bd5b7ce3cc Mon Sep 17 00:00:00 2001
From: Alex Deucher <[email protected]>
Date: Thu, 21 Apr 2022 01:21:52 -0400
Subject: [PATCH] drm/amdgpu/psp: move PSP memory alloc from hw_init to sw_init

Memory allocations should be done in sw_init. hw_init should just be hardware programming needed to initialize the IP block. This is how most other IP blocks work. Move the GPU memory allocations from psp hw_init to psp sw_init and move the memory free to sw_fini. This also fixes a potential GPU memory leak if psp hw_init fails.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 95 ++++++++++++-------------
 1 file changed, 47 insertions(+), 48 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +47 -48
index a6acec1a6155..21aa556a6bef 100644
@@ -357,7 +357,39 @@ static int psp_sw_init(void *handle)
 		}
 	}
 
+	ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
+				      amdgpu_sriov_vf(adev) ?
+				      AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
+				      &psp->fw_pri_bo,
+				      &psp->fw_pri_mc_addr,
+				      &psp->fw_pri_buf);
+	if (ret)
+		return ret;
+
+	ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
+				      AMDGPU_GEM_DOMAIN_VRAM,
+				      &psp->fence_buf_bo,
+				      &psp->fence_buf_mc_addr,
+				      &psp->fence_buf);
+	if (ret)
+		goto failed1;
+
+	ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
+				      AMDGPU_GEM_DOMAIN_VRAM,
+				      &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
+				      (void **)&psp->cmd_buf_mem);
+	if (ret)
+		goto failed2;
+
 	return 0;
+
+failed2:
+	amdgpu_bo_free_kernel(&psp->fw_pri_bo,
+			      &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
+failed1:
+	amdgpu_bo_free_kernel(&psp->fence_buf_bo,
+			      &psp->fence_buf_mc_addr, &psp->fence_buf);
+	return ret;
 }
 
 static int psp_sw_fini(void *handle)
@@ -391,6 +423,13 @@ static int psp_sw_fini(void *handle)
 	kfree(cmd);
 	cmd = NULL;
 
+	amdgpu_bo_free_kernel(&psp->fw_pri_bo,
+			      &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
+	amdgpu_bo_free_kernel(&psp->fence_buf_bo,
+			      &psp->fence_buf_mc_addr, &psp->fence_buf);
+	amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
+			      (void **)&psp->cmd_buf_mem);
+
 	return 0;
 }
 
@@ -2430,51 +2469,18 @@ static int psp_load_fw(struct amdgpu_device *adev)
 	struct psp_context *psp = &adev->psp;
 
 	if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
-		psp_ring_stop(psp, PSP_RING_TYPE__KM); /* should not destroy ring, only stop */
-		goto skip_memalloc;
-	}
-
-	if (amdgpu_sriov_vf(adev)) {
-		ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
-						AMDGPU_GEM_DOMAIN_VRAM,
-						&psp->fw_pri_bo,
-						&psp->fw_pri_mc_addr,
-						&psp->fw_pri_buf);
+		/* should not destroy ring, only stop */
+		psp_ring_stop(psp, PSP_RING_TYPE__KM);
 	} else {
-		ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
-						AMDGPU_GEM_DOMAIN_GTT,
-						&psp->fw_pri_bo,
-						&psp->fw_pri_mc_addr,
-						&psp->fw_pri_buf);
-	}
-
-	if (ret)
-		goto failed;
-
-	ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
-					AMDGPU_GEM_DOMAIN_VRAM,
-					&psp->fence_buf_bo,
-					&psp->fence_buf_mc_addr,
-					&psp->fence_buf);
-	if (ret)
-		goto failed;
-
-	ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
-				      AMDGPU_GEM_DOMAIN_VRAM,
-				      &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
-				      (void **)&psp->cmd_buf_mem);
-	if (ret)
-		goto failed;
+		memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
 
-	memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
-
-	ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
-	if (ret) {
-		DRM_ERROR("PSP ring init failed!\n");
-		goto failed;
+		ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
+		if (ret) {
+			DRM_ERROR("PSP ring init failed!\n");
+			goto failed;
+		}
 	}
 
-skip_memalloc:
 	ret = psp_hw_start(psp);
 	if (ret)
 		goto failed;
@@ -2592,13 +2598,6 @@ static int psp_hw_fini(void *handle)
 	psp_tmr_terminate(psp);
 	psp_ring_destroy(psp, PSP_RING_TYPE__KM);
 
-	amdgpu_bo_free_kernel(&psp->fw_pri_bo,
-			      &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
-	amdgpu_bo_free_kernel(&psp->fence_buf_bo,
-			      &psp->fence_buf_mc_addr, &psp->fence_buf);
-	amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
-			      (void **)&psp->cmd_buf_mem);
-
 	return 0;
 }
 
Signature · 1 line
2.35.1
#2 lore raw

[AMD Official Use Only]

Hi Alex,

The attached patch freed most of the allocated memory except for one allocated by psp_tmr_init during psp_load_fw. Combination of the attached patch and calling psp_hw_fini when psp_hw_init failed would fix the issue.

As a proper fix, we can call psp_tmr_terminate in psp_load_fw when psp_load_non_psp_fw failed. (attached patch) We can't move psp_tmr_init to sw_init because we need to load toc to get the TMR size.
Do you have any concerns with this approach?

Alice

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: April 21, 2022 1:31 AM
To: Wong, Alice <[email protected]>
Cc: amd-gfx list <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: clean up psp ip if hw_init failed v2

Quoted 8 lines · Alice Wong

If at any point psp_hw_init failed, psp_hw_fini would not be called during unload due to ip_blocks[PSP].status.hw not being set to true. This could cause a memory leak when the driver unloads.
As a rule of thumb, each IP block should cleanup themselves when their hw_init fails. Only previously intialized blocks should be cleaned up by the common framework.

Quoted 5 lines

v1: Call IP blocks' respective hw_fini when hw_init failed from
the common framework
v2: Call psp_hw_fini when psp_hw_init failed.

I don't think this is a good idea. The hw programming in hw_fini makes no sense if the driver never set it up in the first place if hw_init failed before initializing the hw. It would be better to just properly unwind the relevant functions. Presumably the problem you are seeing is the failure to free the GPU memory allocated in fw_fini, depending on where it fails. We should just allocate the memory in sw_init; that is what we do in other IPs. Does the attached patch fix the issue you are seeing?

Alex

Quoted 85 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 57 
+++++++++++++------------
 1 file changed, 29 insertions(+), 28 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +29 -29
index 5b9e48d44f5b..52b14efa848a 100644
@@ -2537,6 +2537,34 @@ static int psp_load_fw(struct amdgpu_device *adev)
        return ret;
 }
 
+static int psp_hw_fini(void *handle)
+{
+       struct amdgpu_device *adev = (struct amdgpu_device *)handle;
+       struct psp_context *psp = &adev->psp;
+
+       if (psp->ta_fw) {
+               psp_ras_terminate(psp);
+               psp_securedisplay_terminate(psp);
+               psp_rap_terminate(psp);
+               psp_dtm_terminate(psp);
+               psp_hdcp_terminate(psp);
+       }
+
+       psp_asd_terminate(psp);
+
+       psp_tmr_terminate(psp);
+       psp_ring_destroy(psp, PSP_RING_TYPE__KM);
+
+       amdgpu_bo_free_kernel(&psp->fw_pri_bo,
+                             &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
+       amdgpu_bo_free_kernel(&psp->fence_buf_bo,
+                             &psp->fence_buf_mc_addr, &psp->fence_buf);
+       amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
+                             (void **)&psp->cmd_buf_mem);
+
+       return 0;
+}
+
 static int psp_hw_init(void *handle)
 {
        int ret;
@@ -2563,37 +2591,10 @@ static int psp_hw_init(void *handle)
 failed:
        adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
        mutex_unlock(&adev->firmware.mutex);
+       psp_hw_fini(handle);
        return -EINVAL;
 }
 
-static int psp_hw_fini(void *handle)
-{
-       struct amdgpu_device *adev = (struct amdgpu_device *)handle;
-       struct psp_context *psp = &adev->psp;
-
-       if (psp->ta_fw) {
-               psp_ras_terminate(psp);
-               psp_securedisplay_terminate(psp);
-               psp_rap_terminate(psp);
-               psp_dtm_terminate(psp);
-               psp_hdcp_terminate(psp);
-       }
-
-       psp_asd_terminate(psp);
-
-       psp_tmr_terminate(psp);
-       psp_ring_destroy(psp, PSP_RING_TYPE__KM);
-
-       amdgpu_bo_free_kernel(&psp->fw_pri_bo,
-                             &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
-       amdgpu_bo_free_kernel(&psp->fence_buf_bo,
-                             &psp->fence_buf_mc_addr, &psp->fence_buf);
-       amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
-                             (void **)&psp->cmd_buf_mem);
-
-       return 0;
-}
-
 static int psp_suspend(void *handle)
 {
        int ret;
--
Quoted 2 lines

2.25.1

From 76938fc949a2c20a98e349ddfeb6a6dc98ee332a Mon Sep 17 00:00:00 2001
From: Alice Wong <[email protected]>
Date: Fri, 22 Apr 2022 15:44:19 -0400
Subject: [PATCH] drm/amdgpu/psp: terminate PSP tmr when psp_load_fw failed

Since psp_tmr_init allocates memory in hw_init, need to deallocate the memory using psp_tmr_terminate when psp_load_fw fails at any point after psp_tmr_init.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 105 ++++++++++++------------
 1 file changed, 53 insertions(+), 52 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +53 -52
index 5b9e48d44f5b..9f3113a96ba6 100644
@@ -351,7 +351,39 @@ static int psp_sw_init(void *handle)
 		}
 	}
 
+	ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
+				      amdgpu_sriov_vf(adev) ?
+				      AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
+				      &psp->fw_pri_bo,
+				      &psp->fw_pri_mc_addr,
+				      &psp->fw_pri_buf);
+	if (ret)
+		return ret;
+
+	ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
+				      AMDGPU_GEM_DOMAIN_VRAM,
+				      &psp->fence_buf_bo,
+				      &psp->fence_buf_mc_addr,
+				      &psp->fence_buf);
+	if (ret)
+		goto failed1;
+
+	ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
+				      AMDGPU_GEM_DOMAIN_VRAM,
+				      &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
+				      (void **)&psp->cmd_buf_mem);
+	if (ret)
+		goto failed2;
+
 	return 0;
+
+failed2:
+	amdgpu_bo_free_kernel(&psp->fw_pri_bo,
+			      &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
+failed1:
+	amdgpu_bo_free_kernel(&psp->fence_buf_bo,
+			      &psp->fence_buf_mc_addr, &psp->fence_buf);
+	return ret;
 }
 
 static int psp_sw_fini(void *handle)
@@ -385,6 +417,13 @@ static int psp_sw_fini(void *handle)
 	kfree(cmd);
 	cmd = NULL;
 
+	amdgpu_bo_free_kernel(&psp->fw_pri_bo,
+			      &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
+	amdgpu_bo_free_kernel(&psp->fence_buf_bo,
+			      &psp->fence_buf_mc_addr, &psp->fence_buf);
+	amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
+			      (void **)&psp->cmd_buf_mem);
+
 	return 0;
 }
 
@@ -2422,69 +2461,36 @@ static int psp_load_fw(struct amdgpu_device *adev)
 	struct psp_context *psp = &adev->psp;
 
 	if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
-		psp_ring_stop(psp, PSP_RING_TYPE__KM); /* should not destroy ring, only stop */
-		goto skip_memalloc;
-	}
-
-	if (amdgpu_sriov_vf(adev)) {
-		ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
-						AMDGPU_GEM_DOMAIN_VRAM,
-						&psp->fw_pri_bo,
-						&psp->fw_pri_mc_addr,
-						&psp->fw_pri_buf);
+		/* should not destroy ring, only stop */
+		psp_ring_stop(psp, PSP_RING_TYPE__KM);
 	} else {
-		ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
-						AMDGPU_GEM_DOMAIN_GTT,
-						&psp->fw_pri_bo,
-						&psp->fw_pri_mc_addr,
-						&psp->fw_pri_buf);
-	}
+		memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
 
-	if (ret)
-		goto failed;
-
-	ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
-					AMDGPU_GEM_DOMAIN_VRAM,
-					&psp->fence_buf_bo,
-					&psp->fence_buf_mc_addr,
-					&psp->fence_buf);
-	if (ret)
-		goto failed;
-
-	ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
-				      AMDGPU_GEM_DOMAIN_VRAM,
-				      &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
-				      (void **)&psp->cmd_buf_mem);
-	if (ret)
-		goto failed;
-
-	memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
-
-	ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
-	if (ret) {
-		DRM_ERROR("PSP ring init failed!\n");
-		goto failed;
+		ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
+		if (ret) {
+			DRM_ERROR("PSP ring init failed!\n");
+			goto failed;
+		}
 	}
 
-skip_memalloc:
 	ret = psp_hw_start(psp);
 	if (ret)
 		goto failed;
 
 	ret = psp_load_non_psp_fw(psp);
 	if (ret)
-		goto failed;
+		goto failed1;
 
 	ret = psp_asd_initialize(psp);
 	if (ret) {
 		DRM_ERROR("PSP load asd failed!\n");
-		return ret;
+		goto failed1;
 	}
 
 	ret = psp_rl_load(adev);
 	if (ret) {
 		DRM_ERROR("PSP load RL failed!\n");
-		return ret;
+		goto failed1;
 	}
 
 	if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
@@ -2528,6 +2534,9 @@ static int psp_load_fw(struct amdgpu_device *adev)
 
 	return 0;
 
+failed1:
+	psp_tmr_terminate(psp);
+
 failed:
 	/*
 	 * all cleanup jobs (xgmi terminate, ras terminate,
@@ -2584,13 +2593,6 @@ static int psp_hw_fini(void *handle)
 	psp_tmr_terminate(psp);
 	psp_ring_destroy(psp, PSP_RING_TYPE__KM);
 
-	amdgpu_bo_free_kernel(&psp->fw_pri_bo,
-			      &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
-	amdgpu_bo_free_kernel(&psp->fence_buf_bo,
-			      &psp->fence_buf_mc_addr, &psp->fence_buf);
-	amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
-			      (void **)&psp->cmd_buf_mem);
-
 	return 0;
 }
 
@@ -2781,7 +2783,6 @@ int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
 		AMDGPU_UCODE_ID_VCN0_RAM;
 	ucode.mc_addr = cmd_gpu_addr;
 	ucode.ucode_size = cmd_size;
-
 	return psp_execute_non_psp_fw_load(&adev->psp, &ucode);
 }
 
Signature · 1 line
2.25.1
#3 lore raw
Quoted 11 lines · Alice

[AMD Official Use Only]

Hi Alex,

The attached patch freed most of the allocated memory except for one allocated by psp_tmr_init during psp_load_fw. Combination of the attached patch and calling psp_hw_fini when psp_hw_init failed would fix the issue.

As a proper fix, we can call psp_tmr_terminate in psp_load_fw when psp_load_non_psp_fw failed. (attached patch) We can't move psp_tmr_init to sw_init because we need to load toc to get the TMR size.
Do you have any concerns with this approach?

That only covers failures in hw_init(). It doesn't handle resume(). Looks like all of the ta functions also potentially leak. I'm working on a cleanup to handle all of these.

Alex

Quoted 117 lines

Alice

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: April 21, 2022 1:31 AM
To: Wong, Alice <[email protected]>
Cc: amd-gfx list <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: clean up psp ip if hw_init failed v2

Quoted 8 lines · Alice Wong

If at any point psp_hw_init failed, psp_hw_fini would not be called during unload due to ip_blocks[PSP].status.hw not being set to true. This could cause a memory leak when the driver unloads.
As a rule of thumb, each IP block should cleanup themselves when their hw_init fails. Only previously intialized blocks should be cleaned up by the common framework.

Quoted 5 lines

v1: Call IP blocks' respective hw_fini when hw_init failed from
the common framework
v2: Call psp_hw_fini when psp_hw_init failed.

I don't think this is a good idea. The hw programming in hw_fini makes no sense if the driver never set it up in the first place if hw_init failed before initializing the hw. It would be better to just properly unwind the relevant functions. Presumably the problem you are seeing is the failure to free the GPU memory allocated in fw_fini, depending on where it fails. We should just allocate the memory in sw_init; that is what we do in other IPs. Does the attached patch fix the issue you are seeing?

Alex

Quoted 87 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 57
+++++++++++++------------
 1 file changed, 29 insertions(+), 28 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +29 -29
index 5b9e48d44f5b..52b14efa848a 100644
@@ -2537,6 +2537,34 @@ static int psp_load_fw(struct amdgpu_device *adev)
        return ret;
 }
 
+static int psp_hw_fini(void *handle)
+{
+       struct amdgpu_device *adev = (struct amdgpu_device *)handle;
+       struct psp_context *psp = &adev->psp;
+
+       if (psp->ta_fw) {
+               psp_ras_terminate(psp);
+               psp_securedisplay_terminate(psp);
+               psp_rap_terminate(psp);
+               psp_dtm_terminate(psp);
+               psp_hdcp_terminate(psp);
+       }
+
+       psp_asd_terminate(psp);
+
+       psp_tmr_terminate(psp);
+       psp_ring_destroy(psp, PSP_RING_TYPE__KM);
+
+       amdgpu_bo_free_kernel(&psp->fw_pri_bo,
+                             &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
+       amdgpu_bo_free_kernel(&psp->fence_buf_bo,
+                             &psp->fence_buf_mc_addr, &psp->fence_buf);
+       amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
+                             (void **)&psp->cmd_buf_mem);
+
+       return 0;
+}
+
 static int psp_hw_init(void *handle)
 {
        int ret;
@@ -2563,37 +2591,10 @@ static int psp_hw_init(void *handle)
 failed:
        adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
        mutex_unlock(&adev->firmware.mutex);
+       psp_hw_fini(handle);
        return -EINVAL;
 }
 
-static int psp_hw_fini(void *handle)
-{
-       struct amdgpu_device *adev = (struct amdgpu_device *)handle;
-       struct psp_context *psp = &adev->psp;
-
-       if (psp->ta_fw) {
-               psp_ras_terminate(psp);
-               psp_securedisplay_terminate(psp);
-               psp_rap_terminate(psp);
-               psp_dtm_terminate(psp);
-               psp_hdcp_terminate(psp);
-       }
-
-       psp_asd_terminate(psp);
-
-       psp_tmr_terminate(psp);
-       psp_ring_destroy(psp, PSP_RING_TYPE__KM);
-
-       amdgpu_bo_free_kernel(&psp->fw_pri_bo,
-                             &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
-       amdgpu_bo_free_kernel(&psp->fence_buf_bo,
-                             &psp->fence_buf_mc_addr, &psp->fence_buf);
-       amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
-                             (void **)&psp->cmd_buf_mem);
-
-       return 0;
-}
-
 static int psp_suspend(void *handle)
 {
        int ret;
--

2.25.1

#4 lore raw

How about these patches?

Alex

Quoted 129 lines · Alex Deucher
Quoted 11 lines · Alice

[AMD Official Use Only]

Hi Alex,

The attached patch freed most of the allocated memory except for one allocated by psp_tmr_init during psp_load_fw. Combination of the attached patch and calling psp_hw_fini when psp_hw_init failed would fix the issue.

As a proper fix, we can call psp_tmr_terminate in psp_load_fw when psp_load_non_psp_fw failed. (attached patch) We can't move psp_tmr_init to sw_init because we need to load toc to get the TMR size.
Do you have any concerns with this approach?

That only covers failures in hw_init(). It doesn't handle resume(). Looks like all of the ta functions also potentially leak. I'm working on a cleanup to handle all of these.

Alex

Quoted 109 lines

Alice

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: April 21, 2022 1:31 AM
To: Wong, Alice <[email protected]>
Cc: amd-gfx list <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: clean up psp ip if hw_init failed v2

Quoted 8 lines · Alice Wong

If at any point psp_hw_init failed, psp_hw_fini would not be called during unload due to ip_blocks[PSP].status.hw not being set to true. This could cause a memory leak when the driver unloads.
As a rule of thumb, each IP block should cleanup themselves when their hw_init fails. Only previously intialized blocks should be cleaned up by the common framework.

Quoted 5 lines

v1: Call IP blocks' respective hw_fini when hw_init failed from
the common framework
v2: Call psp_hw_fini when psp_hw_init failed.

I don't think this is a good idea. The hw programming in hw_fini makes no sense if the driver never set it up in the first place if hw_init failed before initializing the hw. It would be better to just properly unwind the relevant functions. Presumably the problem you are seeing is the failure to free the GPU memory allocated in fw_fini, depending on where it fails. We should just allocate the memory in sw_init; that is what we do in other IPs. Does the attached patch fix the issue you are seeing?

Alex

Quoted 79 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 57
+++++++++++++------------
 1 file changed, 29 insertions(+), 28 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +29 -26
index 5b9e48d44f5b..52b14efa848a 100644
@@ -2537,6 +2537,34 @@ static int psp_load_fw(struct amdgpu_device *adev)
        return ret;
 }
 
+static int psp_hw_fini(void *handle)
+{
+       struct amdgpu_device *adev = (struct amdgpu_device *)handle;
+       struct psp_context *psp = &adev->psp;
+
+       if (psp->ta_fw) {
+               psp_ras_terminate(psp);
+               psp_securedisplay_terminate(psp);
+               psp_rap_terminate(psp);
+               psp_dtm_terminate(psp);
+               psp_hdcp_terminate(psp);
+       }
+
+       psp_asd_terminate(psp);
+
+       psp_tmr_terminate(psp);
+       psp_ring_destroy(psp, PSP_RING_TYPE__KM);
+
+       amdgpu_bo_free_kernel(&psp->fw_pri_bo,
+                             &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
+       amdgpu_bo_free_kernel(&psp->fence_buf_bo,
+                             &psp->fence_buf_mc_addr, &psp->fence_buf);
+       amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
+                             (void **)&psp->cmd_buf_mem);
+
+       return 0;
+}
+
 static int psp_hw_init(void *handle)
 {
        int ret;
@@ -2563,37 +2591,10 @@ static int psp_hw_init(void *handle)
 failed:
        adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
        mutex_unlock(&adev->firmware.mutex);
+       psp_hw_fini(handle);
        return -EINVAL;
 }
 
-static int psp_hw_fini(void *handle)
-{
-       struct amdgpu_device *adev = (struct amdgpu_device *)handle;
-       struct psp_context *psp = &adev->psp;
-
-       if (psp->ta_fw) {
-               psp_ras_terminate(psp);
-               psp_securedisplay_terminate(psp);
-               psp_rap_terminate(psp);
-               psp_dtm_terminate(psp);
-               psp_hdcp_terminate(psp);
-       }
-
-       psp_asd_terminate(psp);
-
-       psp_tmr_terminate(psp);
-       psp_ring_destroy(psp, PSP_RING_TYPE__KM);
-
-       amdgpu_bo_free_kernel(&psp->fw_pri_bo,
-                             &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
-       amdgpu_bo_free_kernel(&psp->fence_buf_bo,
-                             &psp->fence_buf_mc_addr, &psp->fence_buf);
-       amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
-                             (void **)&psp->cmd_buf_mem);
-
-       return 0;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +0 -3
-}
-
 static int psp_suspend(void *handle)
 {
        int ret;
--

2.25.1

From 19b1b0afe03954f45ddf03305b209465121b06a8 Mon Sep 17 00:00:00 2001
From: Alex Deucher <[email protected]>
Date: Fri, 22 Apr 2022 17:19:29 -0400
Subject: [PATCH 4/4] drm/amdgpu/psp: move shared buffer frees into single
function

So we can properly clean up if any of the TAs or TMR fails to properly initialize or terminate. This avoids any memory leaks in the error case.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 114 ++++++++++++------------
 1 file changed, 55 insertions(+), 59 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +55 -59
index 1ef2aba2ac3f..b1b6f5dd35dd 100644
@@ -153,6 +153,36 @@ static int psp_early_init(void *handle)
 	return 0;
 }
 
+static void psp_free_shared_bufs(struct psp_context *psp)
+{
+	void *tmr_buf;
+	void **pptr;
+
+	/* free TMR memory buffer */
+	pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
+	amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
+
+	/* free xgmi shared memory */
+	psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
+
+	/* free ras shared memory */
+	psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
+
+	/* free hdcp shared memory */
+	psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
+
+	/* free dtm shared memory */
+	psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
+
+	/* free rap shared memory */
+	psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
+
+	/* free securedisplay shared memory */
+	psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
+
+
+}
+
 static void psp_memory_training_fini(struct psp_context *psp)
 {
 	struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
@@ -747,17 +777,7 @@ static int psp_tmr_unload(struct psp_context *psp)
 
 static int psp_tmr_terminate(struct psp_context *psp)
 {
-	int ret;
-	void *tmr_buf;
-	void **pptr;
-
-	ret = psp_tmr_unload(psp);
-
-	/* free TMR memory buffer */
-	pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
-	amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
-
-	return ret;
+	return psp_tmr_unload(psp);
 }
 
 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
@@ -1102,9 +1122,6 @@ int psp_xgmi_terminate(struct psp_context *psp)
 
 	psp->xgmi_context.context.initialized = false;
 
-	/* free xgmi shared memory */
-	psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
-
 	return ret;
 }
 
@@ -1465,9 +1482,6 @@ int psp_ras_terminate(struct psp_context *psp)
 
 	psp->ras_context.context.initialized = false;
 
-	/* free ras shared memory */
-	psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
-
 	return ret;
 }
 
@@ -1650,23 +1664,13 @@ static int psp_hdcp_terminate(struct psp_context *psp)
 	if (amdgpu_sriov_vf(psp->adev))
 		return 0;
 
-	if (!psp->hdcp_context.context.initialized) {
-		if (psp->hdcp_context.context.mem_context.shared_buf) {
-			ret = 0;
-			goto out;
-		} else {
-			return 0;
-		}
-	}
+	if (!psp->hdcp_context.context.initialized)
+		return 0;
 
 	ret = psp_ta_unload(psp, &psp->hdcp_context.context);
 
 	psp->hdcp_context.context.initialized = false;
 
-out:
-	/* free hdcp shared memory */
-	psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
-
 	return ret;
 }
 // HDCP end
@@ -1727,23 +1731,13 @@ static int psp_dtm_terminate(struct psp_context *psp)
 	if (amdgpu_sriov_vf(psp->adev))
 		return 0;
 
-	if (!psp->dtm_context.context.initialized) {
-		if (psp->dtm_context.context.mem_context.shared_buf) {
-			ret = 0;
-			goto out;
-		} else {
-			return 0;
-		}
-	}
+	if (!psp->dtm_context.context.initialized)
+		return 0;
 
 	ret = psp_ta_unload(psp, &psp->dtm_context.context);
 
 	psp->dtm_context.context.initialized = false;
 
-out:
-	/* free dtm shared memory */
-	psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
-
 	return ret;
 }
 // DTM end
@@ -1785,6 +1779,8 @@ static int psp_rap_initialize(struct psp_context *psp)
 	ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
 	if (ret || status != TA_RAP_STATUS__SUCCESS) {
 		psp_rap_terminate(psp);
+		/* free rap shared memory */
+		psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
 
 		dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
 			 ret, status);
@@ -1806,9 +1802,6 @@ static int psp_rap_terminate(struct psp_context *psp)
 
 	psp->rap_context.context.initialized = false;
 
-	/* free rap shared memory */
-	psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
-
 	return ret;
 }
 
@@ -1889,6 +1882,8 @@ static int psp_securedisplay_initialize(struct psp_context *psp)
 	ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
 	if (ret) {
 		psp_securedisplay_terminate(psp);
+		/* free securedisplay shared memory */
+		psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
 		dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
 		return -EINVAL;
 	}
@@ -1919,9 +1914,6 @@ static int psp_securedisplay_terminate(struct psp_context *psp)
 
 	psp->securedisplay_context.context.initialized = false;
 
-	/* free securedisplay shared memory */
-	psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
-
 	return ret;
 }
 
@@ -2524,16 +2516,18 @@ static int psp_hw_fini(void *handle)
 	}
 
 	psp_asd_terminate(psp);
-
 	psp_tmr_terminate(psp);
+
 	psp_ring_destroy(psp, PSP_RING_TYPE__KM);
 
+	psp_free_shared_bufs(psp);
+
 	return 0;
 }
 
 static int psp_suspend(void *handle)
 {
-	int ret;
+	int ret = 0;
 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 	struct psp_context *psp = &adev->psp;
 
@@ -2542,7 +2536,7 @@ static int psp_suspend(void *handle)
 		ret = psp_xgmi_terminate(psp);
 		if (ret) {
 			DRM_ERROR("Failed to terminate xgmi ta\n");
-			return ret;
+			goto out;
 		}
 	}
 
@@ -2550,49 +2544,51 @@ static int psp_suspend(void *handle)
 		ret = psp_ras_terminate(psp);
 		if (ret) {
 			DRM_ERROR("Failed to terminate ras ta\n");
-			return ret;
+			goto out;
 		}
 		ret = psp_hdcp_terminate(psp);
 		if (ret) {
 			DRM_ERROR("Failed to terminate hdcp ta\n");
-			return ret;
+			goto out;
 		}
 		ret = psp_dtm_terminate(psp);
 		if (ret) {
 			DRM_ERROR("Failed to terminate dtm ta\n");
-			return ret;
+			goto out;
 		}
 		ret = psp_rap_terminate(psp);
 		if (ret) {
 			DRM_ERROR("Failed to terminate rap ta\n");
-			return ret;
+			goto out;
 		}
 		ret = psp_securedisplay_terminate(psp);
 		if (ret) {
 			DRM_ERROR("Failed to terminate securedisplay ta\n");
-			return ret;
+			goto out;
 		}
 	}
 
 	ret = psp_asd_terminate(psp);
 	if (ret) {
 		DRM_ERROR("Failed to terminate asd\n");
-		return ret;
+		goto out;
 	}
 
 	ret = psp_tmr_terminate(psp);
 	if (ret) {
 		DRM_ERROR("Failed to terminate tmr\n");
-		return ret;
+		goto out;
 	}
 
 	ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
 	if (ret) {
 		DRM_ERROR("PSP ring stop failed\n");
-		return ret;
 	}
 
-	return 0;
+out:
+	psp_free_shared_bufs(psp);
+
+	return ret;
 }
 
 static int psp_resume(void *handle)
Signature · 1 line
2.35.1

From e4e0b8d66818cc3250e5373bfeb70dedcc7765c1 Mon Sep 17 00:00:00 2001
From: Alex Deucher <[email protected]>
Date: Fri, 22 Apr 2022 16:51:00 -0400
Subject: [PATCH 3/4] drm/amdgpu/psp: fix memory leak in terminate functions

Make sure we free the memory even if the unload fails.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 34 ++++++++++---------------
 1 file changed, 13 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +13 -21
index e9dc83641c71..1ef2aba2ac3f 100644
@@ -752,14 +752,12 @@ static int psp_tmr_terminate(struct psp_context *psp)
 	void **pptr;
 
 	ret = psp_tmr_unload(psp);
-	if (ret)
-		return ret;
 
 	/* free TMR memory buffer */
 	pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
 	amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
 
-	return 0;
+	return ret;
 }
 
 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
@@ -1101,15 +1099,13 @@ int psp_xgmi_terminate(struct psp_context *psp)
 		return 0;
 
 	ret = psp_ta_unload(psp, &psp->xgmi_context.context);
-	if (ret)
-		return ret;
 
 	psp->xgmi_context.context.initialized = false;
 
 	/* free xgmi shared memory */
 	psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
 
-	return 0;
+	return ret;
 }
 
 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
@@ -1466,15 +1462,13 @@ int psp_ras_terminate(struct psp_context *psp)
 		return 0;
 
 	ret = psp_ta_unload(psp, &psp->ras_context.context);
-	if (ret)
-		return ret;
 
 	psp->ras_context.context.initialized = false;
 
 	/* free ras shared memory */
 	psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
 
-	return 0;
+	return ret;
 }
 
 static int psp_ras_initialize(struct psp_context *psp)
@@ -1657,15 +1651,15 @@ static int psp_hdcp_terminate(struct psp_context *psp)
 		return 0;
 
 	if (!psp->hdcp_context.context.initialized) {
-		if (psp->hdcp_context.context.mem_context.shared_buf)
+		if (psp->hdcp_context.context.mem_context.shared_buf) {
+			ret = 0;
 			goto out;
-		else
+		} else {
 			return 0;
+		}
 	}
 
 	ret = psp_ta_unload(psp, &psp->hdcp_context.context);
-	if (ret)
-		return ret;
 
 	psp->hdcp_context.context.initialized = false;
 
@@ -1673,7 +1667,7 @@ static int psp_hdcp_terminate(struct psp_context *psp)
 	/* free hdcp shared memory */
 	psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
 
-	return 0;
+	return ret;
 }
 // HDCP end
 
@@ -1734,15 +1728,15 @@ static int psp_dtm_terminate(struct psp_context *psp)
 		return 0;
 
 	if (!psp->dtm_context.context.initialized) {
-		if (psp->dtm_context.context.mem_context.shared_buf)
+		if (psp->dtm_context.context.mem_context.shared_buf) {
+			ret = 0;
 			goto out;
-		else
+		} else {
 			return 0;
+		}
 	}
 
 	ret = psp_ta_unload(psp, &psp->dtm_context.context);
-	if (ret)
-		return ret;
 
 	psp->dtm_context.context.initialized = false;
 
@@ -1750,7 +1744,7 @@ static int psp_dtm_terminate(struct psp_context *psp)
 	/* free dtm shared memory */
 	psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
 
-	return 0;
+	return ret;
 }
 // DTM end
 
@@ -1922,8 +1916,6 @@ static int psp_securedisplay_terminate(struct psp_context *psp)
 		return 0;
 
 	ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
-	if (ret)
-		return ret;
 
 	psp->securedisplay_context.context.initialized = false;
 
Signature · 1 line
2.35.1

From 16ab75d27de3724ae0ff0e0311e137dc4c5c8ba5 Mon Sep 17 00:00:00 2001
From: Alex Deucher <[email protected]>
Date: Thu, 21 Apr 2022 01:21:52 -0400
Subject: [PATCH 1/4] drm/amdgpu/psp: move PSP memory alloc from hw_init to
sw_init

Memory allocations should be done in sw_init. hw_init should just be hardware programming needed to initialize the IP block. This is how most other IP blocks work. Move the GPU memory allocations from psp hw_init to psp sw_init and move the memory free to sw_fini. This also fixes a potential GPU memory leak if psp hw_init fails.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 95 ++++++++++++-------------
 1 file changed, 47 insertions(+), 48 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +47 -48

124 lines not on this page · raw message

Signature · 1 line
2.35.1

From ded8b2c39be83f943c22202b27ac5def0c1c4ff5 Mon Sep 17 00:00:00 2001
From: Alex Deucher <[email protected]>
Date: Fri, 22 Apr 2022 16:46:24 -0400
Subject: [PATCH 2/4] drm/amdgpu/psp: drop load/unload/init_shared_buf wrappers

Just call the load/unload/init_shared_buf functions directly. Makes the code easier to follow.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 143 ++++--------------------
 1 file changed, 21 insertions(+), 122 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +21 -122

320 lines not on this page · raw message

Signature · 1 line
2.35.1
#5 lore raw

[AMD Official Use Only - General]

Need this patch on top of your patches to handle psp_load_fw failure.

Alice

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: April 22, 2022 5:23 PM
To: Wong, Alice <[email protected]>
Cc: amd-gfx list <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: clean up psp ip if hw_init failed v2

How about these patches?

Alex

Quoted 126 lines · Alex Deucher
Quoted 11 lines · Alice

[AMD Official Use Only]

Hi Alex,

The attached patch freed most of the allocated memory except for one allocated by psp_tmr_init during psp_load_fw. Combination of the attached patch and calling psp_hw_fini when psp_hw_init failed would fix the issue.

As a proper fix, we can call psp_tmr_terminate in psp_load_fw when
psp_load_non_psp_fw failed. (attached patch) We can't move psp_tmr_init to sw_init because we need to load toc to get the TMR size. Do you have any concerns with this approach?

That only covers failures in hw_init(). It doesn't handle resume(). Looks like all of the ta functions also potentially leak. I'm working on a cleanup to handle all of these.

Alex

Quoted 106 lines

Alice

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: April 21, 2022 1:31 AM
To: Wong, Alice <[email protected]>
Cc: amd-gfx list <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: clean up psp ip if hw_init failed v2

Quoted 8 lines · Alice Wong

If at any point psp_hw_init failed, psp_hw_fini would not be called during unload due to ip_blocks[PSP].status.hw not being set to true. This could cause a memory leak when the driver unloads.
As a rule of thumb, each IP block should cleanup themselves when their hw_init fails. Only previously intialized blocks should be cleaned up by the common framework.

Quoted 5 lines

v1: Call IP blocks' respective hw_fini when hw_init failed from
the common framework
v2: Call psp_hw_fini when psp_hw_init failed.

I don't think this is a good idea. The hw programming in hw_fini makes no sense if the driver never set it up in the first place if hw_init failed before initializing the hw. It would be better to just properly unwind the relevant functions. Presumably the problem you are seeing is the failure to free the GPU memory allocated in fw_fini, depending on where it fails. We should just allocate the memory in sw_init; that is what we do in other IPs. Does the attached patch fix the issue you are seeing?

Alex

Quoted 75 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 57
+++++++++++++------------
 1 file changed, 29 insertions(+), 28 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +28 -24
index 5b9e48d44f5b..52b14efa848a 100644
@@ -2537,6 +2537,34 @@ static int psp_load_fw(struct amdgpu_device *adev)
        return ret;
 }
 
+static int psp_hw_fini(void *handle) {
+       struct amdgpu_device *adev = (struct amdgpu_device *)handle;
+       struct psp_context *psp = &adev->psp;
+
+       if (psp->ta_fw) {
+               psp_ras_terminate(psp);
+               psp_securedisplay_terminate(psp);
+               psp_rap_terminate(psp);
+               psp_dtm_terminate(psp);
+               psp_hdcp_terminate(psp);
+       }
+
+       psp_asd_terminate(psp);
+
+       psp_tmr_terminate(psp);
+       psp_ring_destroy(psp, PSP_RING_TYPE__KM);
+
+       amdgpu_bo_free_kernel(&psp->fw_pri_bo,
+                             &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
+       amdgpu_bo_free_kernel(&psp->fence_buf_bo,
+                             &psp->fence_buf_mc_addr, &psp->fence_buf);
+       amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
+                             (void **)&psp->cmd_buf_mem);
+
+       return 0;
+}
+
 static int psp_hw_init(void *handle)  {
        int ret;
@@ -2563,37 +2591,10 @@ static int psp_hw_init(void *handle)
 failed:
        adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
        mutex_unlock(&adev->firmware.mutex);
+       psp_hw_fini(handle);
        return -EINVAL;
 }
 
-static int psp_hw_fini(void *handle) -{
-       struct amdgpu_device *adev = (struct amdgpu_device *)handle;
-       struct psp_context *psp = &adev->psp;
-
-       if (psp->ta_fw) {
-               psp_ras_terminate(psp);
-               psp_securedisplay_terminate(psp);
-               psp_rap_terminate(psp);
-               psp_dtm_terminate(psp);
-               psp_hdcp_terminate(psp);
-       }
-
-       psp_asd_terminate(psp);
-
-       psp_tmr_terminate(psp);
-       psp_ring_destroy(psp, PSP_RING_TYPE__KM);
-
-       amdgpu_bo_free_kernel(&psp->fw_pri_bo,
-                             &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
-       amdgpu_bo_free_kernel(&psp->fence_buf_bo,
-                             &psp->fence_buf_mc_addr, &psp->fence_buf);
-       amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
-                             (void **)&psp->cmd_buf_mem);
-
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +0 -4
-       return 0;
-}
-
 static int psp_suspend(void *handle)  {
        int ret;
--

2.25.1

From 102246dc92a17ae0a981a85a17008f795db3073c Mon Sep 17 00:00:00 2001
From: Alice Wong <[email protected]>
Date: Wed, 27 Apr 2022 21:03:54 -0400
Subject: [PATCH] drm/amdgpu/psp: deallocate memory when psp_load_fw failed

psp_load_fw failure would cause memory leak for psp tmr and psp ring because psp_hw_init is not called as psp block is not fully initialized. Clean up psp tmr and psp ring when psp_load_fw fail by calling psp_free_shared_bufs and psp_ring_destroy.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +12 -9
index 4df13ce3c83c..7b94ffd56862 100644
@@ -153,6 +153,12 @@ static int psp_early_init(void *handle)
 	return 0;
 }
 
+static void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
+{
+	amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
+			      &mem_ctx->shared_buf);
+}
+
 static void psp_free_shared_bufs(struct psp_context *psp)
 {
 	void *tmr_buf;
@@ -1006,12 +1012,6 @@ static int psp_ta_init_shared_buf(struct psp_context *psp,
 	return ret;
 }
 
-static void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
-{
-	amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
-			      &mem_ctx->shared_buf);
-}
-
 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
 				       uint32_t ta_cmd_id,
 				       uint32_t session_id)
@@ -2366,18 +2366,18 @@ static int psp_load_fw(struct amdgpu_device *adev)
 
 	ret = psp_load_non_psp_fw(psp);
 	if (ret)
-		goto failed;
+		goto failed1;
 
 	ret = psp_asd_initialize(psp);
 	if (ret) {
 		DRM_ERROR("PSP load asd failed!\n");
-		return ret;
+		goto failed1;
 	}
 
 	ret = psp_rl_load(adev);
 	if (ret) {
 		DRM_ERROR("PSP load RL failed!\n");
-		return ret;
+		goto failed1;
 	}
 
 	if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
@@ -2421,12 +2421,15 @@ static int psp_load_fw(struct amdgpu_device *adev)
 
 	return 0;
 
+failed1:
+	psp_free_shared_bufs(psp);
 failed:
 	/*
 	 * all cleanup jobs (xgmi terminate, ras terminate,
 	 * ring destroy, cmd/fence/fw buffers destory,
 	 * psp->cmd destory) are delayed to psp_hw_fini
 	 */
+	psp_ring_destroy(psp, PSP_RING_TYPE__KM);
 	return ret;
 }
 
Signature · 1 line
2.25.1
#6 lore raw

Looks good. Thanks for fixing that. I've sent the patches out for review!

Alex

Quoted 153 lines · Alice

[AMD Official Use Only - General]

Need this patch on top of your patches to handle psp_load_fw failure.

Alice

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: April 22, 2022 5:23 PM
To: Wong, Alice <[email protected]>
Cc: amd-gfx list <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: clean up psp ip if hw_init failed v2

How about these patches?

Alex

Quoted 134 lines · Alex Deucher
Quoted 11 lines · Alice

[AMD Official Use Only]

Hi Alex,

The attached patch freed most of the allocated memory except for one allocated by psp_tmr_init during psp_load_fw. Combination of the attached patch and calling psp_hw_fini when psp_hw_init failed would fix the issue.

As a proper fix, we can call psp_tmr_terminate in psp_load_fw when
psp_load_non_psp_fw failed. (attached patch) We can't move psp_tmr_init to sw_init because we need to load toc to get the TMR size. Do you have any concerns with this approach?

That only covers failures in hw_init(). It doesn't handle resume(). Looks like all of the ta functions also potentially leak. I'm working on a cleanup to handle all of these.

Alex

Quoted 114 lines

Alice

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: April 21, 2022 1:31 AM
To: Wong, Alice <[email protected]>
Cc: amd-gfx list <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: clean up psp ip if hw_init failed v2

Quoted 8 lines · Alice Wong

If at any point psp_hw_init failed, psp_hw_fini would not be called during unload due to ip_blocks[PSP].status.hw not being set to true. This could cause a memory leak when the driver unloads.
As a rule of thumb, each IP block should cleanup themselves when their hw_init fails. Only previously intialized blocks should be cleaned up by the common framework.

Quoted 5 lines

v1: Call IP blocks' respective hw_fini when hw_init failed from
the common framework
v2: Call psp_hw_fini when psp_hw_init failed.

I don't think this is a good idea. The hw programming in hw_fini makes no sense if the driver never set it up in the first place if hw_init failed before initializing the hw. It would be better to just properly unwind the relevant functions. Presumably the problem you are seeing is the failure to free the GPU memory allocated in fw_fini, depending on where it fails. We should just allocate the memory in sw_init; that is what we do in other IPs. Does the attached patch fix the issue you are seeing?

Alex

Quoted 83 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 57
+++++++++++++------------
 1 file changed, 29 insertions(+), 28 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +28 -28
index 5b9e48d44f5b..52b14efa848a 100644
@@ -2537,6 +2537,34 @@ static int psp_load_fw(struct amdgpu_device *adev)
        return ret;
 }
 
+static int psp_hw_fini(void *handle) {
+       struct amdgpu_device *adev = (struct amdgpu_device *)handle;
+       struct psp_context *psp = &adev->psp;
+
+       if (psp->ta_fw) {
+               psp_ras_terminate(psp);
+               psp_securedisplay_terminate(psp);
+               psp_rap_terminate(psp);
+               psp_dtm_terminate(psp);
+               psp_hdcp_terminate(psp);
+       }
+
+       psp_asd_terminate(psp);
+
+       psp_tmr_terminate(psp);
+       psp_ring_destroy(psp, PSP_RING_TYPE__KM);
+
+       amdgpu_bo_free_kernel(&psp->fw_pri_bo,
+                             &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
+       amdgpu_bo_free_kernel(&psp->fence_buf_bo,
+                             &psp->fence_buf_mc_addr, &psp->fence_buf);
+       amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
+                             (void **)&psp->cmd_buf_mem);
+
+       return 0;
+}
+
 static int psp_hw_init(void *handle)  {
        int ret;
@@ -2563,37 +2591,10 @@ static int psp_hw_init(void *handle)
 failed:
        adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
        mutex_unlock(&adev->firmware.mutex);
+       psp_hw_fini(handle);
        return -EINVAL;
 }
 
-static int psp_hw_fini(void *handle) -{
-       struct amdgpu_device *adev = (struct amdgpu_device *)handle;
-       struct psp_context *psp = &adev->psp;
-
-       if (psp->ta_fw) {
-               psp_ras_terminate(psp);
-               psp_securedisplay_terminate(psp);
-               psp_rap_terminate(psp);
-               psp_dtm_terminate(psp);
-               psp_hdcp_terminate(psp);
-       }
-
-       psp_asd_terminate(psp);
-
-       psp_tmr_terminate(psp);
-       psp_ring_destroy(psp, PSP_RING_TYPE__KM);
-
-       amdgpu_bo_free_kernel(&psp->fw_pri_bo,
-                             &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
-       amdgpu_bo_free_kernel(&psp->fence_buf_bo,
-                             &psp->fence_buf_mc_addr, &psp->fence_buf);
-       amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
-                             (void **)&psp->cmd_buf_mem);
-
-       return 0;
-}
-
 static int psp_suspend(void *handle)  {
        int ret;
--

2.25.1