From: bob <[email protected]>
amdgpu_ualink_exp_cleanup_worker() revokes an exported BO in this order:
copy the importer bitmap, set PTE.X, send TLB shootdowns, unmap the NPA
range(s) from the local NPA VM in amdgpu_ualink_unmap_all_npa_addr(),
unpin and unreference the BO, and free the NPA VA range(s) of every
importer in the copied bitmap with amdgpu_ualink_free_all_npa_va().
The unmap is skipped for importers whose connection is no longer ready,
for instance because the link dropped or the remote reconnected under a
new generation after the revoke, or because its TLB shootdown failed:
amdgpu_ualink_send_tlb_shootdown() clears their bits in
exp_xa_node->importers_bitmap, which amdgpu_ualink_unmap_all_npa_addr()
walks, and the latter also skips any importer that fails the connection
check itself. Nothing unmaps those ranges later:
amdgpu_ualink_revoke_exported_memory() already removed the importer
entries from exp_handles_list, so a connection reset no longer finds
them in amdgpu_ualink_cleanup_exp_xa_entries(). The BO is freed and its
VRAM can be reused while the NPA VM still holds valid PTEs pointing at
it. In Source-Alias mode this happens whenever no importer passes the
checks, because the shared range is unmapped at most once.
The NPA VM is local to this device, so unmapping it does not depend on
the remote. Pass amdgpu_ualink_unmap_all_npa_addr() the caller's copy of
the bitmap, the same one used to free the VA ranges, and unmap every
range in it: once per importer in Source-Ident mode, once for the shared
range in Source-Alias mode. Importers that are not ready still get their
bit cleared. A connection reset that ran before the cleanup worker has already
cleared its importer's bit, unmapped and freed that range under a
reference that keeps the cleanup worker from running, so no range is
unmapped after its VA was freed. The other caller,
amdgpu_ualink_teardown_exp_xa_node(), passes the bitmap it frees the
VA ranges with.
Reported by the Sashiko AI review on v1 of the generation count fix.
This patch was drafted with the help of an AI coding assistant (Claude
Opus 5.5) from manual review of the code. It has been compile-tested
with W=1 on x86_64 allmodconfig. It has not been runtime-tested: it
needs UALink hardware and several nodes.
Fixes: 8ad8d0cdb4c3 ("drm/amdgpu: Cleanup exported UALink handles")
Signed-off-by: bob <[email protected]>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 26 ++++++++++++++++++--------
1 file changed, 18 insertions(+), 8 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
index d025b4183b29..f23dfed0c92e 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
@@ -2746,7 +2746,8 @@ static void amdgpu_ualink_send_tlb_shootdown(struct
amdgpu_device *adev,
* only in Source Identification mode.
*/
static void amdgpu_ualink_unmap_all_npa_addr(struct amdgpu_device *adev,
- struct amdgpu_ualink_exp_xa_node *exp_xa_node)
+ struct amdgpu_ualink_exp_xa_node *exp_xa_node,
+ const unsigned long *importers_bitmap)
{
struct amdgpu_vm *vm = &adev->ualink.npa_vm;
u32 addr_mode = adev->ualink.info->vpod.addr_mode;
@@ -2755,6 +2756,7 @@ static void amdgpu_ualink_unmap_all_npa_addr(struct
amdgpu_device *adev,
struct dma_fence *fence = NULL;
struct drm_exec exec;
u64 npa_addr, size;
+ bool unmapped = false;
u32 remote_acc_id;
int r;
@@ -2767,7 +2769,7 @@ static void amdgpu_ualink_unmap_all_npa_addr(struct
amdgpu_device *adev,
return;
}
- for_each_set_bit(remote_acc_id, exp_xa_node->importers_bitmap,
+ for_each_set_bit(remote_acc_id, importers_bitmap,
AMDGPU_UALINK_ACCEL_MAX) {
if (addr_mode == AMDGPU_UALINK_ADDR_MODE_SOURCE_ALIAS)
imp_entry = &exp_xa_node->importer_entries[0];
@@ -2776,11 +2778,18 @@ static void amdgpu_ualink_unmap_all_npa_addr(struct
amdgpu_device *adev,
npa_addr = imp_entry->npa_addr;
if (!amdgpu_ualink_check_conn_ready(adev, remote_acc_id,
- imp_entry->generation_count)) {
+ imp_entry->generation_count))
clear_bit(remote_acc_id,
exp_xa_node->importers_bitmap);
+
+ /* Unmap even if the importer's connection is gone, and walk
+ * the caller's copy of the bitmap since the TLB shootdown has
+ * already cleared the bits of such importers: the NPA VM is
+ * local, and the BO and the NPA VA range(s) of every importer
+ * in that copy are freed right after this.
+ */
+ if (addr_mode == AMDGPU_UALINK_ADDR_MODE_SOURCE_ALIAS &&
unmapped)
continue;
- }
dev_dbg(adev->dev,
"UNMAP-NPA: Unmapping NPA:%llx, handle:%llx:%llx
remote:%u pte:0x%llx\n",
@@ -2798,8 +2807,7 @@ static void amdgpu_ualink_unmap_all_npa_addr(struct
amdgpu_device *adev,
npa_addr, exp_xa_node->handle.handle_hi,
exp_xa_node->handle.handle_lo, remote_acc_id);
- if (addr_mode == AMDGPU_UALINK_ADDR_MODE_SOURCE_ALIAS)
- break;
+ unmapped = true;
}
r = amdgpu_vm_update_pdes(adev, vm, false);
@@ -2915,7 +2923,8 @@ static void amdgpu_ualink_exp_cleanup_worker(struct
work_struct *work)
amdgpu_ualink_send_tlb_shootdown(adev, exp_xa_node);
/* Unmap all NPA addresses for this BO from NPA VM */
- amdgpu_ualink_unmap_all_npa_addr(adev, exp_xa_node);
+ amdgpu_ualink_unmap_all_npa_addr(adev, exp_xa_node,
+ orig_importers_bitmap);
dev_dbg(adev->dev, "EXP-CLEANUP: handle:%llx:%llx Unpin BO,
pin_count:%u, importers:%u\n",
handle.handle_hi, handle.handle_lo, bo->tbo.pin_count,
@@ -4243,7 +4252,8 @@ static void amdgpu_ualink_teardown_exp_xa_node(struct
amdgpu_device *adev,
AMDGPU_UALINK_ACCEL_MAX, importers_bitmap);
if (!bitmap_empty(importers_bitmap, AMDGPU_UALINK_ACCEL_MAX)) {
- amdgpu_ualink_unmap_all_npa_addr(adev, exp_xa_node);
+ amdgpu_ualink_unmap_all_npa_addr(adev, exp_xa_node,
+ importers_bitmap);
/* Unlink importers and unpin the BO once per importer. */
if (likely(!amdgpu_bo_reserve(bo, true))) {
--
2.56.1