From: bob <[email protected]>

The exporter keeps one importer_entry per remote accelerator, and stores
the connection generation count the remote imported the handle under in
that remote's own entry (importer_entries[remote_acc_id], see
amdgpu_ualink_process_npa_req_msg()). The revoke and timeout loops of
the cleanup worker read it from there.

In Source-Alias mode all remotes share one NPA address, which lives in
importer_entries[0], and four loops select imp_entry = &importer_entries[0]
to read npa_addr. They also pass imp_entry->generation_count to
amdgpu_ualink_check_conn_ready() and to
amdgpu_ualink_handle_connection_reset(): in
amdgpu_ualink_force_retry_rpcs(), twice in
amdgpu_ualink_send_tlb_shootdown() and in
amdgpu_ualink_unmap_all_npa_addr(). That is the generation of accelerator
0, whatever the remote is. Since a generation count of 0 means "any
generation" and a non-zero one is matched exactly, the result is wrong in
both directions:

 - if accelerator 0 did not import the handle, entry 0 holds 0 and every
   remote is accepted, including one whose connection has been
   re-established since it imported the handle;
 - if accelerator 0 imported the handle under generation G0 and another
   importer R imported it under a different generation, R fails the
   check: its bit is cleared without sending a TLB shootdown, and the
   connection reset for R is skipped or done against the wrong
   generation.

Exact matching makes the check stricter in the first case. That no
longer affects the unmap: since the previous patch the NPA range is
unmapped whether or not the importer passes the check.

Use the generation count of the remote's own entry in the four places, and
keep imp_entry for the shared NPA address. In Source-Ident mode imp_entry
already is the remote's entry, so nothing changes there.

This issue was found by manual code review with the help of an AI coding
assistant (Claude Opus 5.5), which also drafted this patch. It has been
compile-tested with W=1 and smatch on x86_64 allmodconfig (no new
warnings; the only warnings are four pre-existing ones in this file). It
has not been runtime-tested: it needs UALink hardware and several nodes,
and no reproducer was built.

Fixes: 8ad8d0cdb4c3 ("drm/amdgpu: Cleanup exported UALink handles")
Fixes: 003da01f2022 ("drm/amdgpu: Send TLB shootdown on exported memory unmap")
Fixes: e6b969fe8d28 ("drm/amdgpu: Reset UALink connection on failed 
TLB-shootdown send")
Signed-off-by: bob <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
index f23dfed0c92e..9b5f3949ec81 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
@@ -2614,7 +2614,7 @@ static void amdgpu_ualink_force_retry_rpcs(struct 
amdgpu_device *adev,
        struct dma_fence *fence = NULL;
        struct amdgpu_bo *bo;
        struct drm_exec exec;
-       u32 remote_acc_id;
+       u32 remote_acc_id, gen_count;
        int r;
 
        bo = exp_xa_node->bo;
@@ -2633,6 +2633,10 @@ static void amdgpu_ualink_force_retry_rpcs(struct 
amdgpu_device *adev,
                else
                        imp_entry = 
&exp_xa_node->importer_entries[remote_acc_id];
                npa_addr = imp_entry->npa_addr;
+               /* The generation count is per remote, also in Source-Alias
+                * mode where imp_entry is the shared importer_entries[0].
+                */
+               gen_count = 
exp_xa_node->importer_entries[remote_acc_id].generation_count;
 
                /* If the connection state changed while we are freeing
                 * the BO, then ignore this importer. We will unmap this
@@ -2641,7 +2645,7 @@ static void amdgpu_ualink_force_retry_rpcs(struct 
amdgpu_device *adev,
                 * the importer_bitmap in the same function.
                 */
                if (!amdgpu_ualink_check_conn_ready(adev, remote_acc_id,
-                                       imp_entry->generation_count))
+                                       gen_count))
                        continue;
 
                /* Set PTE.X = 1 */
@@ -2695,7 +2699,7 @@ static void amdgpu_ualink_send_tlb_shootdown(struct 
amdgpu_device *adev,
        u32 accel_id = adev->ualink.info->ppod.accel_id;
        struct amdgpu_ualink_importer_entry *imp_entry;
        u64 npa_addr, size;
-       u32 remote_acc_id;
+       u32 remote_acc_id, gen_count;
        int r;
 
        size = amdgpu_bo_ngpu_pages(exp_xa_node->bo);
@@ -2712,9 +2716,13 @@ static void amdgpu_ualink_send_tlb_shootdown(struct 
amdgpu_device *adev,
 
                npa_addr = STRIP_NPA(imp_entry->npa_addr);
                npa_addr = GENERATE_NPA(npa_addr, accel_id);
+               /* The generation count is per remote, also in Source-Alias
+                * mode where imp_entry is the shared importer_entries[0].
+                */
+               gen_count = 
exp_xa_node->importer_entries[remote_acc_id].generation_count;
 
                if (!amdgpu_ualink_check_conn_ready(adev, remote_acc_id,
-                                       imp_entry->generation_count)) {
+                                       gen_count)) {
                        clear_bit(remote_acc_id,
                                  exp_xa_node->importers_bitmap);
                        continue;
@@ -2737,7 +2745,7 @@ static void amdgpu_ualink_send_tlb_shootdown(struct 
amdgpu_device *adev,
                         */
                        amdgpu_ualink_handle_connection_reset(adev, 
remote_acc_id,
                                                AMDGPU_UALINK_CONN_NOT_READY,
-                                               imp_entry->generation_count);
+                                               gen_count);
                }
        }
 }
@@ -2757,7 +2765,7 @@ static void amdgpu_ualink_unmap_all_npa_addr(struct 
amdgpu_device *adev,
        struct drm_exec exec;
        u64 npa_addr, size;
        bool unmapped = false;
-       u32 remote_acc_id;
+       u32 remote_acc_id, gen_count;
        int r;
 
        size = amdgpu_bo_ngpu_pages(exp_xa_node->bo);
@@ -2776,9 +2784,13 @@ static void amdgpu_ualink_unmap_all_npa_addr(struct 
amdgpu_device *adev,
                else
                        imp_entry = 
&exp_xa_node->importer_entries[remote_acc_id];
                npa_addr = imp_entry->npa_addr;
+               /* The generation count is per remote, also in Source-Alias
+                * mode where imp_entry is the shared importer_entries[0].
+                */
+               gen_count = 
exp_xa_node->importer_entries[remote_acc_id].generation_count;
 
                if (!amdgpu_ualink_check_conn_ready(adev, remote_acc_id,
-                                       imp_entry->generation_count))
+                                       gen_count))
                        clear_bit(remote_acc_id,
                                  exp_xa_node->importers_bitmap);
 

-- 
2.56.1


Reply via email to