All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/amdgpu: use the per-remote UALink generation count in Source-Alias mode
@ 2026-12-02 19:56 ` bob via B4 Relay
  0 siblings, 0 replies; 3+ messages in thread
From: bob @ 2026-12-02 19:56 UTC (permalink / raw)
  To: Alex Deucher, Christian König, David Airlie, Simona Vetter,
	Mukul Joshi, Felix Kuehling, Harish Kasiviswanathan
  Cc: amd-gfx, dri-devel, bob

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.

In Source-Alias mode the unmap of the shared NPA address is still done
once, for the first remote that passes the check. This patch does not
change that policy.

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 <bobmcd@tuta.io>
---
Note for the amdgpu maintainers (not part of the commit message):

This patch only fixes which generation count is used. It deliberately
leaves the Source-Alias unmap policy alone, and I am not sure it is
right: amdgpu_ualink_unmap_all_npa_addr() and
amdgpu_ualink_force_retry_rpcs() act once on the shared NPA address,
gated on the first set-bit remote whose connection check passes. If no
importer passes, the shared NPA range is still freed
(amdgpu_ualink_free_all_npa_va()) without the unmap. Should the
alias-mode PTE.X and unmap of the shared NPA range run unconditionally,
since they do not depend on the remote?

Also note the check becomes stricter in one case: when entry 0's
generation_count is 0 it used to match any remote, and now each remote
is compared exactly. An importer whose bit survived a reconnect can
therefore cause the only unmap to be skipped.
---
 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 d025b4183b29..1546c5d5c675 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);
 		}
 	}
 }
@@ -2755,7 +2763,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;
-	u32 remote_acc_id;
+	u32 remote_acc_id, gen_count;
 	int r;
 
 	size = amdgpu_bo_ngpu_pages(exp_xa_node->bo);
@@ -2774,9 +2782,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);
 			continue;

---
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
change-id: 20261203-fix-u5-amdgpu-ualink-alias-gencount-f68079768059

Best regards,
--  
bob <bobmcd@tuta.io>


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH] drm/amdgpu: use the per-remote UALink generation count in Source-Alias mode
@ 2026-12-02 19:56 ` bob via B4 Relay
  0 siblings, 0 replies; 3+ messages in thread
From: bob via B4 Relay @ 2026-12-02 19:56 UTC (permalink / raw)
  To: Alex Deucher, Christian König, David Airlie, Simona Vetter,
	Mukul Joshi, Felix Kuehling, Harish Kasiviswanathan
  Cc: amd-gfx, dri-devel, bob

From: bob <bobmcd@tuta.io>

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.

In Source-Alias mode the unmap of the shared NPA address is still done
once, for the first remote that passes the check. This patch does not
change that policy.

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 <bobmcd@tuta.io>
---
Note for the amdgpu maintainers (not part of the commit message):

This patch only fixes which generation count is used. It deliberately
leaves the Source-Alias unmap policy alone, and I am not sure it is
right: amdgpu_ualink_unmap_all_npa_addr() and
amdgpu_ualink_force_retry_rpcs() act once on the shared NPA address,
gated on the first set-bit remote whose connection check passes. If no
importer passes, the shared NPA range is still freed
(amdgpu_ualink_free_all_npa_va()) without the unmap. Should the
alias-mode PTE.X and unmap of the shared NPA range run unconditionally,
since they do not depend on the remote?

Also note the check becomes stricter in one case: when entry 0's
generation_count is 0 it used to match any remote, and now each remote
is compared exactly. An importer whose bit survived a reconnect can
therefore cause the only unmap to be skipped.
---
 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 d025b4183b29..1546c5d5c675 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);
 		}
 	}
 }
@@ -2755,7 +2763,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;
-	u32 remote_acc_id;
+	u32 remote_acc_id, gen_count;
 	int r;
 
 	size = amdgpu_bo_ngpu_pages(exp_xa_node->bo);
@@ -2774,9 +2782,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);
 			continue;

---
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
change-id: 20261203-fix-u5-amdgpu-ualink-alias-gencount-f68079768059

Best regards,
--  
bob <bobmcd@tuta.io>



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/amdgpu: use the per-remote UALink generation count in Source-Alias mode
  2026-12-02 19:56 ` bob via B4 Relay
  (?)
@ 2026-12-02 20:06 ` sashiko-bot
  -1 siblings, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-02 20:06 UTC (permalink / raw)
  To: bob; +Cc: amd-gfx, dri-devel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] drm/amdgpu: skipped NPA VM unmap in
  amdgpu_ualink_unmap_all_npa_addr()
--

commit b97443989d15a388f71900159633c07744fbba8b
Author: bob <bobmcd@tuta.io>

drm/amdgpu: use the per-remote UALink generation count in Source-Alias mode

This commit uses the remote importer's own generation_count rather than
importer_entries[0].generation_count when checking connection state and
handling connection resets during UALink export cleanup in Source-Alias
mode.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
> index d025b4183b296..1546c5d5c6758 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
[ ... ]
> @@ -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);
>  		}
>  	}
>  }
> @@ -2755,7 +2763,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;
> -	u32 remote_acc_id;
> +	u32 remote_acc_id, gen_count;
>  	int r;
>  
>  	size = amdgpu_bo_ngpu_pages(exp_xa_node->bo);
> @@ -2774,9 +2782,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);
>  			continue;

[Severity: High]
Can this leave stale valid PTEs in adev->ualink.npa_vm pointing to freed
VRAM pages when an importer fails TLB shootdown or reconnects?

Before queuing amdgpu_ualink_exp_cleanup_worker(),
amdgpu_ualink_revoke_exported_memory() unlinks every imp_entry->list from
adev->ualink.exp_handles_list, so a later connection reset cannot find or
unmap exp_xa_node via amdgpu_ualink_cleanup_exp_xa_entries().

In Source-Alias mode when accelerator 0 had not imported the handle,
imp_entry->generation_count was previously 0, so
amdgpu_ualink_handle_connection_reset() did not match the established
connection's non-zero generation and amdgpu_ualink_check_conn_ready()
treated 0 as a wildcard and still unmapped the shared NPA range.  With
gen_count set to the remote's generation, a failed
amdgpu_ualink_remote_shootdown() marks conn_state[remote_acc_id] as
AMDGPU_UALINK_CONN_NOT_READY (or a prior reconnect mismatches gen_count
and clears remote_acc_id from exp_xa_node->importers_bitmap).

When amdgpu_ualink_unmap_all_npa_addr() then runs, no importer passes
amdgpu_ualink_check_conn_ready(), so amdgpu_vm_update_range() is skipped.
amdgpu_ualink_exp_cleanup_worker() still unpins and unreferences
exp_xa_node->bo and calls amdgpu_ualink_free_all_npa_va() with
orig_importers_bitmap, freeing the BO and the NPA VA range while the PTEs
(with PTE.X = 1 set by amdgpu_ualink_force_retry_rpcs()) remain mapped in
adev->ualink.npa_vm.  Unmapping the local NPA VM mappings unconditionally
for all allocated NPA ranges, regardless of remote connection state, would
avoid leaving stale PTEs behind.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-fix-u5-amdgpu-ualink-alias-gencount-v1-1-a2e17004b91c@tuta.io?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02 20:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 19:56 [PATCH] drm/amdgpu: use the per-remote UALink generation count in Source-Alias mode bob
2026-12-02 19:56 ` bob via B4 Relay
2026-12-02 20:06 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.