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")
Signed-off-by: Lin Huiyi <[email protected]>
---
v2: rename the existing out_sdma_entities_fini label to
    out_peer_remote_fini instead of adding a second label. v1 added a new
    label and left the old one without any goto, which triggers
    -Wunused-label (caught by Sashiko review). No functional change.

 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
index b4fac5ecab..140f8141b7 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
@@ -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,12 +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_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

Reply via email to