drm/amdgpu: Reject UALink handle ioctl on devices without UALink
Subsystems (2)
PATCH v2 1/5 drm/amdgpu: Reject UALink handle ioctl on devices without UALink
amdgpu_gem_ualink_handle_ioctl() dereferences adev->ualink.info before validating it, but that structure is only allocated when the UALink IP block is present, which is currently added only for GFX 12.1 only. The ioctl is registered for every amdgpu device with DRM_RENDER_ALLOW, so on any other ASIC a render node user can oops the kernel with a single call.
Return -EOPNOTSUPP when the UALink software state is absent.
---v1->v2:
- Updated the failure message.
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +9 -4
@@ -4072,10 +4072,15 @@ int amdgpu_gem_ualink_handle_ioctl(struct drm_device *dev, void *data,u32 gem_handle;int r, fd =-1 ; -if (adev->ualink.info->accel_state != -AMDGPU_UALINK_ACCEL_STATE_ACTIVE ) { -dev_err (adev->dev, -"ualink device is not in active state in vpod\n" ); +/* The ioctl is registered for every amdgpu device, but the UALink +* software state only exists on ASICs that instantiate the UALink IP +* block. Bail out if it is absent, and short-circuit before the +* accel_state dereference below. +*/ +if (!adev->ualink.info || + adev->ualink.info->accel_state !=AMDGPU_UALINK_ACCEL_STATE_ACTIVE ) { +dev_dbg (adev->dev, +"ualink device not available or not in active state\n" );return -EOPNOTSUPP ; }
Signature · 1 line
2.55.0
PATCH v2 2/5 drm/amdgpu: Fix BO reference leak in UALink export error path
amdgpu_ualink_export_handle() takes an explicit amdgpu_bo_ref() before building the exporter node, and the out: label only drops the reference from drm_gem_object_lookup(). The xarray insert failure path unrefs the BO, but the drm_gem_prime_handle_to_dmabuf() failure path does not.
Userspace can hit that path at will, for example by exhausting its file descriptor table so the dmabuf export returns -EMFILE, leaking a BO reference and its pinned VRAM on every attempt.
---v1->v2:
- Update to using exp_xa_node->bo everywhere in the function.
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +2 -1
@@ -4023,6 +4023,7 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,if (IS_ERR (exp_xa_node->dmabuf)) { r =PTR_ERR (exp_xa_node->dmabuf);dev_err (adev->dev,"Failed to generate DMABuf for the BO\n" ); +amdgpu_bo_unref (&exp_xa_node->bo);kfree (exp_xa_node); goto out; }
@@ -4036,7 +4037,7 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,if (r) {dev_err (adev->dev,"Failed to insert exp_xa_node into XA: %d\n" , r);dma_buf_put (exp_xa_node->dmabuf); -amdgpu_bo_unref (&robj); +amdgpu_bo_unref (&exp_xa_node->bo);kfree (exp_xa_node); goto out; }
Signature · 1 line
2.55.0
PATCH v2 3/5 drm/amdgpu: Fix race on ualink_handle_lo in UALink handle export
In reply to Mukul Joshiamdgpu_ualink_export_handle() tests robj->ualink_handle_lo and stores the generated handle into it without any lock; xa_lock only covers handle generation and the xarray insert.
Two threads can therefore both observe a zero handle for the same BO and both allocate an exporter node, take an amdgpu_bo_ref() and insert into exp_xa, with the last store to ualink_handle_lo winning. Teardown only erases the handle recorded in the BO, so the losing node keeps its BO reference and pinned VRAM until device teardown.
Each racing thread still generates its own handle and inserts its own node, but the handle is published to the BO under the BO reservation lock after re-checking that ualink_handle_lo is still zero. Exactly one thread makes that transition; the others find a non-zero value, roll back their node (erase from exp_xa, drop the DMABuf and BO references, free it) and return -EAGAIN so the caller retries and picks up the winning handle. This keeps a single exporter node per BO.
---v1->v2:
- Use amdgpu_bo_reserve() to update the ualink_handle_lo in the BO. If it is not zero, then roll back the changes.
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 31 +++++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +24 -7
@@ -4023,9 +4023,7 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,if (IS_ERR (exp_xa_node->dmabuf)) { r =PTR_ERR (exp_xa_node->dmabuf);dev_err (adev->dev,"Failed to generate DMABuf for the BO\n" ); -amdgpu_bo_unref (&exp_xa_node->bo); -kfree (exp_xa_node); - goto out; + goto err_unref_bo; }xa_lock (&adev->ualink.exp_xa);
@@ -4036,13 +4034,22 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,xa_unlock (&adev->ualink.exp_xa);if (r) {dev_err (adev->dev,"Failed to insert exp_xa_node into XA: %d\n" , r); -dma_buf_put (exp_xa_node->dmabuf); -amdgpu_bo_unref (&exp_xa_node->bo); -kfree (exp_xa_node); - goto out; + goto err_put_dmabuf; + } + + r =amdgpu_bo_reserve (robj, false); +if (r) + goto err_erase_xa; + +if (robj->ualink_handle_lo) { +amdgpu_bo_unreserve (robj); + r = -EAGAIN ; + goto err_erase_xa; } robj->ualink_handle_lo = handle.handle_lo; +amdgpu_bo_unreserve (robj); +/* Return the generated handle back to the caller */ *handle_out = handle; }else {
@@ -4059,6 +4066,16 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp, handle_out->handle_hi = exp_xa_node->handle.handle_hi; } +drm_gem_object_put (gobj); +return 0 ; + +err_erase_xa: +xa_erase (&adev->ualink.exp_xa, handle.handle_lo); +err_put_dmabuf: +dma_buf_put (exp_xa_node->dmabuf); +err_unref_bo: +amdgpu_bo_unref (&exp_xa_node->bo); +kfree (exp_xa_node); out:drm_gem_object_put (gobj);return r;
Signature · 1 line
2.55.0
Re: [PATCHv2 3/5] drm/amdgpu: Fix race on ualink_handle_lo in UALink handle export
Quoted 18 lines · Mukul Joshi
amdgpu_ualink_export_handle()testsrobj->ualink_handle_loand stores the generated handle into it without any lock;xa_lockonly covers handle generation and the xarray insert.Two threads can therefore both observe a zero handle for the same BO and both allocate an exporter node, take an
amdgpu_bo_ref()and insert intoexp_xa, with the last store toualink_handle_lowinning. Teardown only erases the handle recorded in the BO, so the losing node keeps its BO reference and pinned VRAM until device teardown.Each racing thread still generates its own handle and inserts its own node, but the handle is published to the BO under the BO reservation lock after re-checking that
ualink_handle_lois still zero. Exactly one thread makes that transition; the others find a non-zero value, roll back their node (erase fromexp_xa, drop the DMABuf and BO references, free it) and return-EAGAINso the caller retries and picks up the winning handle. This keeps a single exporter node per BO.
Quoted 3 lines
Fixes 6b1a5ad90778 ("drm/amdgpu: Implement UALink handle export")Reported-by Sashiko-bot <[email protected]>Signed-off-by Mukul Joshi <[email protected]>
Quoted 67 lines
---
v1->v2:
- Useamdgpu_bo_reserve()to update theualink_handle_loin the BO. If it is not zero, then roll back the changes.drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 31 +++++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +24
-7@@ -4023,9 +4023,7 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,if (IS_ERR (exp_xa_node->dmabuf)) { r =PTR_ERR (exp_xa_node->dmabuf);dev_err (adev->dev,"Failed to generate DMABuf for the BO\n" ); -amdgpu_bo_unref (&exp_xa_node->bo); -kfree (exp_xa_node); - goto out; + goto err_unref_bo; }xa_lock (&adev->ualink.exp_xa);@@ -4036,13 +4034,22 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp,xa_unlock (&adev->ualink.exp_xa);if (r) {dev_err (adev->dev,"Failed to insert exp_xa_node into XA: %d\n" , r); -dma_buf_put (exp_xa_node->dmabuf); -amdgpu_bo_unref (&exp_xa_node->bo); -kfree (exp_xa_node); - goto out; + goto err_put_dmabuf; + } + + r =amdgpu_bo_reserve (robj, false); +if (r) + goto err_erase_xa; + +if (robj->ualink_handle_lo) { +amdgpu_bo_unreserve (robj); + r = -EAGAIN ; + goto err_erase_xa; } robj->ualink_handle_lo = handle.handle_lo; +amdgpu_bo_unreserve (robj); +/* Return the generated handle back to the caller */ *handle_out = handle; }else {@@ -4059,6 +4066,16 @@ int amdgpu_ualink_export_handle(struct drm_device *dev, struct drm_file *filp, handle_out->handle_hi = exp_xa_node->handle.handle_hi; } +drm_gem_object_put (gobj); +return 0 ; + +err_erase_xa: +xa_erase (&adev->ualink.exp_xa, handle.handle_lo); +err_put_dmabuf: +dma_buf_put (exp_xa_node->dmabuf); +err_unref_bo: +amdgpu_bo_unref (&exp_xa_node->bo); +kfree (exp_xa_node); out:drm_gem_object_put (gobj);return r;
PATCH v2 4/5 drm/amdgpu: re-check connection state before sending NPA-REVOKE
In reply to Mukul JoshiThe export cleanup worker checks amdgpu_ualink_check_conn_ready() once when building npa_release_bitmap, but not again in the send loop. Cleanup workers run concurrently on the unbounded NPA WQ (one per exported handle), so when a peer's egress dies many workers have already seeded npa_release_bitmap from a connection that was still ESTABLISHED and all issue an LSDMA copy to the dead peer, flooding the log with repeated NPA-REVOKE send failures.
Re-check the connection immediately before each send, as the tlb shootdown, force_retry_rpcs and unmap_all_npa_addr loops already do, so once the first worker marks a peer NOT_READY the rest skip it. With the re-check in place, seed npa_release_bitmap with a single bitmap_copy() of importers_bitmap instead of a separate build loop; peers that fail the re-check or the send are dropped from the bitmap in the send loop.
---v1->v2:
- We were using 2 for loops, once for building the npa_release_bitmap, and another for sending the NPA-REVOKEs.
- Replace it by using a single loop instead.
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 31 +++++++++++++--------- 1 file changed, 19 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +19 -12
@@ -2936,26 +2936,34 @@ static void amdgpu_ualink_exp_cleanup_worker(struct work_struct *work)amdgpu_bo_unref (&bo); exp_xa_node->bo =NULL ; -/* Build the full npa_release_bitmap before sending any NPA-REVOKE. */ +/* Seed npa_release_bitmap with all importers before any NPA-REVOKE, so +* the release IRQ only sees it empty once every responder has replied. +* Dead peers are dropped from it in the send loop below. +*/ +bitmap_copy (exp_xa_node->npa_release_bitmap, + exp_xa_node->importers_bitmap,AMDGPU_UALINK_ACCEL_MAX ); + +/* Send NPA-REVOKE to all importers which have imported this memory. +* On send failure clear the bit (no response will arrive) and mark the +* connection NOT_READY so other cleanups skip this unreachable peer. +*/ for_each_set_bit (remote_acc_id, exp_xa_node->importers_bitmap,AMDGPU_UALINK_ACCEL_MAX ) { imp_entry = &exp_xa_node->importer_entries[remote_acc_id]; + +/* Re-check the connection right before the send: concurrent +* cleanup workers all seed the bitmap while the peer is still +* ESTABLISHED, so once its egress dies and the first worker +* marks it NOT_READY, this skips the rest instead of flooding +* the log with failed LSDMA sends to the same dead peer. +*/ if (!amdgpu_ualink_check_conn_ready (adev, remote_acc_id, imp_entry->generation_count)) {clear_bit (remote_acc_id, - exp_xa_node->importers_bitmap); + exp_xa_node->npa_release_bitmap);continue ; } -set_bit (remote_acc_id, exp_xa_node->npa_release_bitmap); - } - -/* Send NPA-REVOKE to all importers which have imported this memory. -* On send failure clear the bit (no response will arrive) and mark the -* connection NOT_READY so other cleanups skip this unreachable peer. -*/ -for_each_set_bit (remote_acc_id, exp_xa_node->importers_bitmap, -AMDGPU_UALINK_ACCEL_MAX ) {dev_dbg (adev->dev,"EXP-CLEANUP: Sending NPA-REVOKE to remote:%u\n" , remote_acc_id);
@@ -2966,7 +2974,6 @@ static void amdgpu_ualink_exp_cleanup_worker(struct work_struct *work) remote_acc_id);clear_bit (remote_acc_id, exp_xa_node->npa_release_bitmap); - imp_entry = &exp_xa_node->importer_entries[remote_acc_id];amdgpu_ualink_handle_connection_reset (adev, remote_acc_id,AMDGPU_UALINK_CONN_NOT_READY , imp_entry->generation_count);
Signature · 1 line
2.55.0
Re: [PATCHv2 4/5] drm/amdgpu: re-check connection state before sending NPA-REVOKE
Quoted 50 lines · Mukul Joshi
The export cleanup worker checks
amdgpu_ualink_check_conn_ready()once when buildingnpa_release_bitmap, but not again in the send loop. Cleanup workers run concurrently on the unbounded NPA WQ (one per exported handle), so when a peer's egress dies many workers have already seedednpa_release_bitmapfrom a connection that was still ESTABLISHED and all issue an LSDMA copy to the dead peer, flooding the log with repeated NPA-REVOKE send failures.Re-check the connection immediately before each send, as the tlb shootdown,
force_retry_rpcsandunmap_all_npa_addrloops already do, so once the first worker marks a peerNOT_READYthe rest skip it. With the re-check in place, seednpa_release_bitmapwith a singlebitmap_copy()ofimporters_bitmapinstead of a separate build loop; peers that fail the re-check or the send are dropped from the bitmap in the send loop.Signed-off-by Mukul Joshi <[email protected]>---
v1->v2:
- We were using 2 for loops, once for building thenpa_release_bitmap, and another for sending the NPA-REVOKEs.
- Replace it by using a single loop instead.drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 31 +++++++++++++--------- 1 file changed, 19 insertions(+), 12 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +13
-1@@ -2936,26 +2936,34 @@ static void amdgpu_ualink_exp_cleanup_worker(struct work_struct *work)amdgpu_bo_unref (&bo); exp_xa_node->bo =NULL ; -/* Build the full npa_release_bitmap before sending any NPA-REVOKE. */ +/* Seed npa_release_bitmap with all importers before any NPA-REVOKE, so +* the release IRQ only sees it empty once every responder has replied. +* Dead peers are dropped from it in the send loop below. +*/ +bitmap_copy (exp_xa_node->npa_release_bitmap, + exp_xa_node->importers_bitmap,AMDGPU_UALINK_ACCEL_MAX ); + +/* Send NPA-REVOKE to all importers which have imported this memory. +* On send failure clear the bit (no response will arrive) and mark the +* connection NOT_READY so other cleanups skip this unreachable peer. +*/ for_each_set_bit (remote_acc_id, exp_xa_node->importers_bitmap,AMDGPU_UALINK_ACCEL_MAX ) { imp_entry = &exp_xa_node->importer_entries[remote_acc_id]; + +/* Re-check the connection right before the send: concurrent
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c static void amdgpu_ualink_exp_cleanup_worker(struct work_struct *work) · line 2963
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +6
-1+* cleanup workers all seed the bitmap while the peer is still +* ESTABLISHED, so once its egress dies and the first worker +* marks it NOT_READY, this skips the rest instead of flooding +* the log with failed LSDMA sends to the same dead peer. +*/ if (!amdgpu_ualink_check_conn_ready (adev, remote_acc_id, imp_entry->generation_count)) {clear_bit (remote_acc_id, - exp_xa_node->importers_bitmap); + exp_xa_node->npa_release_bitmap);
It look like you never clear bits in importers_bitmap now. So maybe you could get rid of orig_importers_bitmap now as well. But that could be a separate cleanup patch if this one is urgent.
Either way, this patch is
Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +0
-10continue ; } -set_bit (remote_acc_id, exp_xa_node->npa_release_bitmap); - } - -/* Send NPA-REVOKE to all importers which have imported this memory. -* On send failure clear the bit (no response will arrive) and mark the -* connection NOT_READY so other cleanups skip this unreachable peer. -*/ -for_each_set_bit (remote_acc_id, exp_xa_node->importers_bitmap, -AMDGPU_UALINK_ACCEL_MAX ) {dev_dbg (adev->dev,"EXP-CLEANUP: Sending NPA-REVOKE to remote:%u\n" , remote_acc_id);@@ -2966,7 +2974,6 @@ static void amdgpu_ualink_exp_cleanup_worker(struct work_struct *work) remote_acc_id);clear_bit (remote_acc_id, exp_xa_node->npa_release_bitmap); - imp_entry = &exp_xa_node->importer_entries[remote_acc_id];amdgpu_ualink_handle_connection_reset (adev, remote_acc_id,AMDGPU_UALINK_CONN_NOT_READY , imp_entry->generation_count);
PATCH v2 5/5 drm/amdgpu: Fix NPA BO memory accounting stats during import invalidation
In reply to Mukul JoshiUse amdgpu_vm_bo_move() for updating the NPA BO memory accounting stats when the NPA BO is moved out from NPA domain into an empty placement. Without this, when the app finishes, it spits out a warning:
"VM memory stats for proc .. task .. is non-zero when fini"
This was only affecting the memory accounting stats, we were not leaking any memory here.
---v1->v2:
- New change added.
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +1 -6
@@ -3056,12 +3056,7 @@ static void amdgpu_ualink_invalidate_import_mappings(struct amdgpu_bo *bo) } } -/* FIXME: This should be after the "if", but needs a fix to make sure -* DMABuf imports are initialized in the right VM list. -*/ -amdgpu_vm_bo_invalidate (bo, false); -if (!bo->tbo.resource || bo->tbo.resource->mem_type ==TTM_PL_SYSTEM ) - goto fini; +amdgpu_vm_bo_move (bo,NULL , false); r =ttm_bo_validate (&bo->tbo, &placement, &ctx);if (r) {
Signature · 1 line
2.55.0
Re: [PATCHv2 5/5] drm/amdgpu: Fix NPA BO memory accounting stats during import invalidation
Quoted 9 lines · Mukul Joshi
Use
amdgpu_vm_bo_move()for updating the NPA BO memory accounting stats when the NPA BO is moved out from NPA domain into an empty placement. Without this, when the app finishes, it spits out a warning:
"VM memory stats for proc .. task .. is non-zero when fini"This was only affecting the memory accounting stats, we were not leaking any memory here.
Signed-off-by Mukul Joshi <[email protected]>
Quoted 25 lines
---
v1->v2:
- New change added.drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +1
-6@@ -3056,12 +3056,7 @@ static void amdgpu_ualink_invalidate_import_mappings(struct amdgpu_bo *bo) } } -/* FIXME: This should be after the "if", but needs a fix to make sure -* DMABuf imports are initialized in the right VM list. -*/ -amdgpu_vm_bo_invalidate (bo, false); -if (!bo->tbo.resource || bo->tbo.resource->mem_type ==TTM_PL_SYSTEM ) - goto fini; +amdgpu_vm_bo_move (bo,NULL , false); r =ttm_bo_validate (&bo->tbo, &placement, &ctx);if (r) {