Skip to contentSkip to conversation

drm/amdgpu: Unwind UALink peer GART mappings on sw_init failure

PATCH RESEND Superseded Lin Huiyi 3 participants 5 messages Nov 30 – Dec 1

Subsystems (2)
lore
Share & tools
lore

Lin Huiyi

author
#0 lore raw

PATCH RESEND 1/1 drm/amdgpu: Unwind UALink peer GART mappings on sw_init failure

amdgpu_ualink_peer_remote_init() maps a set of GART ranges for every remote peer. If one of the amdgpu_ualink_gart_map() calls fails, it breaks out of the loop and returns an error, but the mappings created for the peers processed so far are left behind.

amdgpu_ualink_sw_init() then takes the out_sdma_entities_fini path, which only tears down the SDMA entities and frees the remote structure without calling amdgpu_ualink_peer_remote_fini(). The already-allocated drm_mm_nodes stay registered in the shared GTT manager after the memory that tracked them is gone, corrupting the drm_mm state and leaking GART space on every failed activation.

Fix this by calling amdgpu_ualink_peer_remote_fini() on the error path. Move the peer mutex initialization ahead of the fallible mappings so that fini can safely destroy the locks even when only some peers were mapped; gart_unmap() already ignores unallocated nodes.

Fixes 6e3834ceaf2f ("drm/amdgpu: Add UALink cross-GPU TLB shootdown and remote interrupt")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +9 -2
index b4fac5ecab..74c9e72727 100644
@@ -6186,6 +6186,12 @@ static int amdgpu_ualink_peer_remote_init(struct amdgpu_device *adev)
 	dev_dbg(adev->dev, "src_accel_id %u gart mapping flags 0x%llx\n",
 		src_accel_id, flags);
 
+	/* Initialize all locks before any fallible mapping so fini can unwind. */
+	for_each_set_bit(dst_accel_id, remote->active_accel_bits, AMDGPU_UALINK_ACCEL_MAX) {
+		if (dst_accel_id != src_accel_id)
+			mutex_init(&remote->peer[dst_accel_id].lock);
+	}
+
 	for_each_set_bit(dst_accel_id, remote->active_accel_bits, AMDGPU_UALINK_ACCEL_MAX) {
 		if (dst_accel_id == src_accel_id)
 			continue;
@@ -6193,7 +6199,6 @@ static int amdgpu_ualink_peer_remote_init(struct amdgpu_device *adev)
 		dev_dbg(adev->dev, "peer_remote to dst_accel_id %u\n", dst_accel_id);
 
 		peer = &remote->peer[dst_accel_id];
-		mutex_init(&peer->lock);
 
 		interrupt = &peer->interrupt;
 		shootdown = &peer->shootdown;
@@ -6353,11 +6358,13 @@ int amdgpu_ualink_sw_init(struct amdgpu_device *adev)
 
 	r = amdgpu_ualink_peer_remote_init(adev);
 	if (r)
-		goto out_sdma_entities_fini;
+		goto out_peer_remote_fini;
 
 	dev_dbg(adev->dev, "ualink sw init succeed\n");
 	return 0;
 
+out_peer_remote_fini:
+	amdgpu_ualink_peer_remote_fini(adev);
 out_sdma_entities_fini:
 	amdgpu_ualink_sdma_entities_fini(adev);
 out_npa_unmap:
Signature · 1 line
2.56.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/amdgpu: unused label out_sdma_entities_fini in amdgpu_ualink_sw_init() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/amdgpu: unused label out_sdma_entities_fini in amdgpu_ualink_sw_init()
--

commit b9e3288ae5fab2693604cd2fa96cf9a0518f82d8
Author: Lin Huiyi <[email protected]>

drm/amdgpu: Unwind UALink peer GART mappings on sw_init failure

This commit initializes all peer mutexes before mapping GART ranges and calls amdgpu_ualink_peer_remote_fini() on the amdgpu_ualink_sw_init() failure path so partially created peer GART mappings are unwound.

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +0 -0
index fa4c3d27d2720..cf794c0feb3df 100644

[ ... ]

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +0 -1
@@ -6345,11 +6350,13 @@ int amdgpu_ualink_sw_init(struct amdgpu_device *adev)
 
 	r = amdgpu_ualink_peer_remote_init(adev);
 	if (r)
-		goto out_sdma_entities_fini;

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c int amdgpu_ualink_sw_init(struct amdgpu_device *adev) · line 6362

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +3 -0
+		goto out_peer_remote_fini;
 
 	dev_dbg(adev->dev, "ualink sw init succeed\n");
 	return 0;
 
+out_peer_remote_fini:
+	amdgpu_ualink_peer_remote_fini(adev);
 out_sdma_entities_fini:
 	amdgpu_ualink_sdma_entities_fini(adev);
 out_npa_unmap:

[Severity: Medium]
Does replacing the only jump to out_sdma_entities_fini leave that label unused, triggering a -Wunused-label warning (and a build failure with WERROR enabled)?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Lin Huiyi
Quoted 30 lines · Lin Huiyi

[Some people who received this message don't often get email [email protected]. Learn why this is important athttps://aka.ms/LearnAboutSenderIdentification ]

amdgpu_ualink_peer_remote_init() maps a set of GART ranges for every remote peer. If one of the amdgpu_ualink_gart_map() calls fails, it breaks out of the loop and returns an error, but the mappings created for the peers processed so far are left behind.

amdgpu_ualink_sw_init() then takes the out_sdma_entities_fini path, which only tears down the SDMA entities and frees the remote structure without calling amdgpu_ualink_peer_remote_fini(). The already-allocated drm_mm_nodes stay registered in the shared GTT manager after the memory that tracked them is gone, corrupting the drm_mm state and leaking GART space on every failed activation.

Fix this by calling amdgpu_ualink_peer_remote_fini() on the error path. Move the peer mutex initialization ahead of the fallible mappings so that fini can safely destroy the locks even when only some peers were mapped; gart_unmap() already ignores unallocated nodes.

Fixes 6e3834ceaf2f ("drm/amdgpu: Add UALink cross-GPU TLB shootdown and remote interrupt")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +0 -0
index b4fac5ecab..74c9e72727 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c static int amdgpu_ualink_peer_remote_init(struct amdgpu_device *adev) · line 6194

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +6 -0
@@ -6186,6 +6186,12 @@ static int amdgpu_ualink_peer_remote_init(struct amdgpu_device *adev)
         dev_dbg(adev->dev, "src_accel_id %u gart mapping flags 0x%llx\n",
                 src_accel_id, flags);
 
+       /* Initialize all locks before any fallible mapping so fini can unwind. */
+       for_each_set_bit(dst_accel_id, remote->active_accel_bits, AMDGPU_UALINK_ACCEL_MAX) {
+               if (dst_accel_id != src_accel_id)
+                       mutex_init(&remote->peer[dst_accel_id].lock);
+       }
+

Calling mutex_destroy() on an all-zero struct mutex is safe in every config, so this for loop isn't needed. Please remove it.

Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +0 -1
         for_each_set_bit(dst_accel_id, remote->active_accel_bits, AMDGPU_UALINK_ACCEL_MAX) {
                 if (dst_accel_id == src_accel_id)
                         continue;
@@ -6193,7 +6199,6 @@ static int amdgpu_ualink_peer_remote_init(struct amdgpu_device *adev)
                 dev_dbg(adev->dev, "peer_remote to dst_accel_id %u\n", dst_accel_id);
 
                 peer = &remote->peer[dst_accel_id];
-               mutex_init(&peer->lock);
 
                 interrupt = &peer->interrupt;
                 shootdown = &peer->shootdown;
@@ -6353,11 +6358,13 @@ int amdgpu_ualink_sw_init(struct amdgpu_device *adev)
 
         r = amdgpu_ualink_peer_remote_init(adev);

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c int amdgpu_ualink_sw_init(struct amdgpu_device *adev) · line 6368

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +3 -1
         if (r)
-               goto out_sdma_entities_fini;
+               goto out_peer_remote_fini;
 
         dev_dbg(adev->dev, "ualink sw init succeed\n");
         return 0;
 
+out_peer_remote_fini:
+       amdgpu_ualink_peer_remote_fini(adev);
  out_sdma_entities_fini:

The label out_sdma_entities_fini is no longer used, please remove itRegards, Philip

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +0 -1
         amdgpu_ualink_sdma_entities_fini(adev);
  out_npa_unmap:
--

2.56.1

author
#3 lore raw

PATCH v3 1/1 drm/amdgpu: Unwind UALink peer GART mappings on sw_init failure

amdgpu_ualink_peer_remote_init() maps a set of GART ranges for every remote peer. If one of the amdgpu_ualink_gart_map() calls fails, it breaks out of the loop and returns an error, but the mappings created for the peers processed so far are left behind.

amdgpu_ualink_sw_init() then takes the out_sdma_entities_fini path, which only tears down the SDMA entities and frees the remote structure without calling amdgpu_ualink_peer_remote_fini(). The already-allocated drm_mm_nodes stay registered in the shared GTT manager after the memory that tracked them is gone, corrupting the drm_mm state and leaking GART space on every failed activation.

Fix this by calling amdgpu_ualink_peer_remote_fini() on the error path. This is safe even for peers whose mappings were not created yet, since gart_unmap() ignores unallocated nodes and mutex_destroy() copes with a zero-initialized mutex.

Fixes 6e3834ceaf2f ("drm/amdgpu: Add UALink cross-GPU TLB shootdown and remote interrupt")

---
v3: drop the mutex_init loop and keep the per-peer mutex_init as
before, since mutex_destroy() is safe on a zero-initialized mutex, so the loop was unnecessary. Pointed out by Philip Yang.
v2: rename the existing out_sdma_entities_fini label to
out_peer_remote_fini instead of adding a second label, to avoid -Wunused-label (Sashiko).

 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +3 -2
index b4fac5ecab..df716e2370 100644
@@ -6353,12 +6353,13 @@ int amdgpu_ualink_sw_init(struct amdgpu_device *adev)
 
 	r = amdgpu_ualink_peer_remote_init(adev);
 	if (r)
-		goto out_sdma_entities_fini;
+		goto out_peer_remote_fini;
 
 	dev_dbg(adev->dev, "ualink sw init succeed\n");
 	return 0;
 
-out_sdma_entities_fini:
+out_peer_remote_fini:
+	amdgpu_ualink_peer_remote_fini(adev);
 	amdgpu_ualink_sdma_entities_fini(adev);
 out_npa_unmap:
 	amdgpu_ualink_metadata_npa_unmapping(adev);
Signature · 1 line
2.56.1
Reviewed-by on v3 1/1
#4 lore raw
Quoted 14 lines · Lin Huiyi

[Some people who received this message don't often get email [email protected]. Learn why this is important athttps://aka.ms/LearnAboutSenderIdentification ]

amdgpu_ualink_peer_remote_init() maps a set of GART ranges for every remote peer. If one of the amdgpu_ualink_gart_map() calls fails, it breaks out of the loop and returns an error, but the mappings created for the peers processed so far are left behind.

amdgpu_ualink_sw_init() then takes the out_sdma_entities_fini path, which only tears down the SDMA entities and frees the remote structure without calling amdgpu_ualink_peer_remote_fini(). The already-allocated drm_mm_nodes stay registered in the shared GTT manager after the memory that tracked them is gone, corrupting the drm_mm state and leaking GART space on every failed activation.

Quoted 7 lines

Fix this by calling amdgpu_ualink_peer_remote_fini() on the error path. This is safe even for peers whose mappings were not created yet, since gart_unmap() ignores unallocated nodes and mutex_destroy() copes with a zero-initialized mutex.

Fixes 6e3834ceaf2f ("drm/amdgpu: Add UALink cross-GPU TLB shootdown and remote interrupt")

I will apply this patch to amd-staging-drm-next branch.

Thanks,
Philip

Quoted 33 lines

---
v3: drop the mutex_init loop and keep the per-peer mutex_init as
before, since mutex_destroy() is safe on a zero-initialized mutex, so the loop was unnecessary. Pointed out by Philip Yang.
v2: rename the existing out_sdma_entities_fini label to
out_peer_remote_fini instead of adding a second label, to avoid -Wunused-label (Sashiko).

  drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +3 -3
index b4fac5ecab..df716e2370 100644
@@ -6353,12 +6353,13 @@ int amdgpu_ualink_sw_init(struct amdgpu_device *adev)
 
         r = amdgpu_ualink_peer_remote_init(adev);
         if (r)
-               goto out_sdma_entities_fini;
+               goto out_peer_remote_fini;
 
         dev_dbg(adev->dev, "ualink sw init succeed\n");
         return 0;
 
-out_sdma_entities_fini:
+out_peer_remote_fini:
+       amdgpu_ualink_peer_remote_fini(adev);
         amdgpu_ualink_sdma_entities_fini(adev);
  out_npa_unmap:
         amdgpu_ualink_metadata_npa_unmapping(adev);
--

2.56.1