drm/amdgpu: add UAPI for user queue query status
Subsystems (3)
PATCH RESEND 1/4 drm/amdgpu: add UAPI for user queue query status
From: Alex Deucher <[email protected]>
Add an API to query whether a queue is broken because it is hung or VRAM has been lost. Report either condition through a single BROKEN flag.
--- include/uapi/drm/amdgpu_drm.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
include/uapi/drm/amdgpu_drm.h +11 -0
@@ -340,6 +340,7 @@ union drm_amdgpu_ctx {/* user queue IOCTL operations */ #define AMDGPU_USERQ_OP_CREATE 1#define AMDGPU_USERQ_OP_FREE 2 +#define AMDGPU_USERQ_OP_QUERY_STATUS 3/* queue priority levels */ /* low < normal low < normal high < high */
@@ -352,6 +353,9 @@ union drm_amdgpu_ctx {/* for queues that need access to protected content */ #define AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE (1 << 2) +/* the queue is broken because it is hung or VRAM was lost */ +#define AMDGPU_USERQ_QUERY_STATUS_FLAGS_BROKEN (1 << 0) +/* * This structure is a container to pass input configuration * info for all supported userqueue related operations.
@@ -433,9 +437,16 @@ struct drm_amdgpu_userq_out {__u32 _pad; }; +/* The structure to carry output of userqueue ops */ +struct drm_amdgpu_userq_out_query_state { +__u32 flags; +__u32 _pad; +}; +union drm_amdgpu_userq {struct drm_amdgpu_userq_in in;struct drm_amdgpu_userq_out out; +struct drm_amdgpu_userq_out_query_state out_qs; };/* GFX V11 IP specific MQD parameters */
Signature · 1 line
2.34.1
PATCH RESEND 2/4 drm/amdgpu/userq: implement support for query status
From: Alex Deucher <[email protected]>
Query the status of the user queue. Report BROKEN when the queue is hung or its VM generation indicates that VRAM has been lost.
v2: Misc cleanups
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 34 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 1 + 2 files changed, 35 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +34 -0
@@ -702,6 +702,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) queue->queue_type = args->in.ip_type; queue->vm = &fpriv->vm; queue->priority = priority; + queue->generation =amdgpu_vm_generation (adev, &fpriv->vm); queue->xcp_id = (fpriv->xcp_id !=AMDGPU_XCP_NO_PARTITION ) ? fpriv->xcp_id :0 ; queue->userq_mgr = uq_mgr;
@@ -817,6 +818,33 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)return r; } +static int +amdgpu_userq_query_status (struct drm_file *filp,union drm_amdgpu_userq *args) +{ +struct amdgpu_fpriv *fpriv = filp->driver_priv; +struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; +struct amdgpu_device *adev = uq_mgr->adev; +struct amdgpu_usermode_queue *queue; +int queue_id = args->in.queue_id; + +mutex_lock (&uq_mgr->userq_mutex); + + queue =amdgpu_userq_get (uq_mgr, queue_id); +if (!queue) { +mutex_unlock (&uq_mgr->userq_mutex); +return -EINVAL ; + } + + args->out_qs.flags =0 ; +if (queue->state ==AMDGPU_USERQ_STATE_HUNG || + queue->generation !=amdgpu_vm_generation (adev, &fpriv->vm)) + args->out_qs.flags |=AMDGPU_USERQ_QUERY_STATUS_FLAGS_BROKEN ; +amdgpu_userq_put (queue); +mutex_unlock (&uq_mgr->userq_mutex); + +return 0 ; +} +static int amdgpu_userq_input_args_validate (struct drm_device *dev,union drm_amdgpu_userq *args,struct drm_file *filp)
@@ -938,6 +966,12 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,break ; } +case AMDGPU_USERQ_OP_QUERY_STATUS : + r =amdgpu_userq_query_status (filp, args); +if (r) +drm_dbg_driver (dev,"Failed to query usermode queue status\n" ); +break ; +default :drm_dbg_driver (dev,"Invalid user queue op specified: %d\n" , args->in.op);return -EINVAL ;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
@@ -79,6 +79,7 @@ struct amdgpu_usermode_queue {struct dma_fence *last_fence;u32 xcp_id;int priority; +uint64_t generation;struct dentry *debugfs_queue;/**
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH RESEND 2/4] drm/amdgpu/userq: implement support for query status
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] drm/amdgpu/userq: unlocked queue state…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] drm/amdgpu/userq: unlocked queue state and unmap/map in mes_userq_reset() [1]
--
commit 8f1348ec761b7ada9e6ca46fe71c338002a3662a
Author: Alex Deucher <[email protected]>
drm/amdgpu/userq: implement support for query status
This commit implements the AMDGPU_USERQ_OP_QUERY_STATUS operation to report when a usermode queue is hung or its VM generation has changed due to lost VRAM.
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0
[ ... ]
Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +11
-0@@ -817,6 +818,33 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)return r; } +static int +amdgpu_userq_query_status (struct drm_file *filp,union drm_amdgpu_userq *args) +{ +struct amdgpu_fpriv *fpriv = filp->driver_priv; +struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; +struct amdgpu_device *adev = uq_mgr->adev; +struct amdgpu_usermode_queue *queue; +int queue_id = args->in.queue_id; + +mutex_lock (&uq_mgr->userq_mutex); +
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 841
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+ queue =amdgpu_userq_get (uq_mgr, queue_id); +if (!queue) { +mutex_unlock (&uq_mgr->userq_mutex); +return -EINVAL ; + } + + args->out_qs.flags =0 ; +if (queue->state ==AMDGPU_USERQ_STATE_HUNG || + queue->generation !=amdgpu_vm_generation (adev, &fpriv->vm)) + args->out_qs.flags |=AMDGPU_USERQ_QUERY_STATUS_FLAGS_BROKEN ;
[Severity: High]
This problem wasn't introduced by this patch, but can a concurrent eviction in amdgpu_userq_unmap_helper() race with mes_userq_reset_hw() [1] and overwrite AMDGPU_USERQ_STATE_HUNG?
amdgpu_userq_query_status() and amdgpu_userq_unmap_helper() serialize queue->state under uq_mgr->userq_mutex, whereas mes_userq_reset_hw() unmaps the queue and sets queue->state = AMDGPU_USERQ_STATE_HUNG without holding userq_mutex:
CPU0 CPU1amdgpu_userq_hang_detect_work() amdgpu_eviction_fence_suspend_worker()
mes_userq_reset() mutex_lock(&uq_mgr->userq_mutex)
mes_userq_reset_hw() amdgpu_userq_evict_all()
mes_userq_unmap(queue) amdgpu_userq_unmap_helper(queue)
// sees AMDGPU_USERQ_STATE_MAPPED
mes_userq_unmap(queue)
queue->state =
AMDGPU_USERQ_STATE_HUNG
queue->state =
AMDGPU_USERQ_STATE_UNMAPPED
<-- clobbers HUNG state
mutex_unlock(&uq_mgr->userq_mutex)Both paths call mes_userq_unmap() concurrently, issuing duplicate remove_hw_queue MES packets and double-freeing queue->gang_ctx_array_index. Overwriting AMDGPU_USERQ_STATE_HUNG with AMDGPU_USERQ_STATE_UNMAPPED also hides the broken status from amdgpu_userq_query_status() and allows amdgpu_userq_restore_worker() to remap and re-execute the hung queue.
[1] https://sashiko.dev/#/bug/linux-ea51473b-6c36-421a-880f-c4fff09847a7
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH RESEND 3/4 drm/amdgpu/userq: report invalid VA through the broken status flag
In reply to Prike LiangReport a queue with an invalid virtual address as BROKEN, alongside hung queues and queues whose VRAM has been lost. Use the existing BROKEN flag instead of exposing a separate INVALID_VA status flag.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 1 + include/uapi/drm/amdgpu_drm.h | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
@@ -837,6 +837,7 @@ amdgpu_userq_query_status(struct drm_file *filp, union drm_amdgpu_userq *args) args->out_qs.flags =0 ;if (queue->state ==AMDGPU_USERQ_STATE_HUNG || + queue->state ==AMDGPU_USERQ_STATE_INVALID_VA || queue->generation !=amdgpu_vm_generation (adev, &fpriv->vm)) args->out_qs.flags |=AMDGPU_USERQ_QUERY_STATUS_FLAGS_BROKEN ;amdgpu_userq_put (queue);
include/uapi/drm/amdgpu_drm.h +1 -1
@@ -353,7 +353,7 @@ union drm_amdgpu_ctx {/* for queues that need access to protected content */ #define AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE (1 << 2) -/* the queue is broken because it is hung or VRAM was lost */ +/* the queue is broken because it is hung, has an invalid VA, or VRAM was lost */ #define AMDGPU_USERQ_QUERY_STATUS_FLAGS_BROKEN (1 << 0)/*
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH RESEND 3/4] drm/amdgpu/userq: report invalid VA through the broken status flag
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/amdgpu/userq: missing resume_work flush in amdgpu_userq_query_status() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/amdgpu/userq: missing resume_work flush in amdgpu_userq_query_status()
--
commit dcee7ab7a079dac06e55c3b0a31589e130baf8e9
Author: Prike Liang <[email protected]>
drm/amdgpu/userq: report invalid VA through the broken status flag
This commit sets AMDGPU_USERQ_QUERY_STATUS_FLAGS_BROKEN in amdgpu_userq_query_status() when a usermode queue is in AMDGPU_USERQ_STATE_INVALID_VA, and updates the UAPI comment in amdgpu_drm.h.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_query_status(struct drm_file *filp, union drm_amdgpu_userq *args) · line 843
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0@@ -837,6 +837,7 @@ amdgpu_userq_query_status(struct drm_file *filp, union drm_amdgpu_userq *args) args->out_qs.flags =0 ;if (queue->state ==AMDGPU_USERQ_STATE_HUNG || + queue->state ==AMDGPU_USERQ_STATE_INVALID_VA || queue->generation !=amdgpu_vm_generation (adev, &fpriv->vm)) args->out_qs.flags |=AMDGPU_USERQ_QUERY_STATUS_FLAGS_BROKEN ;amdgpu_userq_put (queue);
[Severity: Medium]
Can amdgpu_userq_query_status() miss AMDGPU_USERQ_STATE_INVALID_VA when called before uq_mgr->resume_work finishes?
The only place that sets queue->state to AMDGPU_USERQ_STATE_INVALID_VA is amdgpu_userq_restore_all(), which runs asynchronously from uq_mgr->resume_work:
CPU0 kworkeramdgpu_gem_va_ioctl(UNMAP)
amdgpu_userq_gem_va_unmap_validate()
dma_resv_wait_timeout()
amdgpu_eviction_fence_suspend_worker()
amdgpu_userq_evict()
queue->state =
AMDGPU_USERQ_STATE_UNMAPPED
dma_fence_signal(ev_fence)
schedule_delayed_work(
&uq_mgr->resume_work, 0) drm_exec_fini()amdgpu_userq_query_status()
mutex_lock(&uq_mgr->userq_mutex) queue->state ==
AMDGPU_USERQ_STATE_UNMAPPED <-- BROKEN not set
mutex_unlock(&uq_mgr->userq_mutex)
amdgpu_userq_restore_worker()
...
amdgpu_userq_restore_all()
queue->state =
AMDGPU_USERQ_STATE_INVALID_VABecause amdgpu_gem_va_ioctl() holds the VM reservation lock until drm_exec_fini() just before returning to userspace, uq_mgr->resume_work cannot finish validating the VM and calling amdgpu_userq_restore_all() before the unmap ioctl returns. Unlike amdgpu_userq_ensure_ev_fence(), amdgpu_userq_query_status() does not call flush_delayed_work(&uq_mgr->resume_work) before acquiring uq_mgr->userq_mutex, so querying queue status after unmapping a queue VA reports flags as 0 instead of AMDGPU_USERQ_QUERY_STATUS_FLAGS_BROKEN.
Also, if a queue VA mapping is removed via AMDGPU_VA_OP_CLEAR or AMDGPU_VA_OP_REPLACE (amdgpu_vm_bo_clear_mappings()), amdgpu_userq_gem_va_unmap_validate() is never called to trigger eviction and transition the queue to AMDGPU_USERQ_STATE_INVALID_VA.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH RESEND 4/4 drm/amdgpu/userq: report GPU reset in progress
In reply to Prike LiangReport an ongoing GPU reset through RESET_IN_PROGRESS at bit 1, alongside BROKEN at bit 0, so userspace can query whether GPU recovery is ongoing.
Validate query inputs using the same rules as queue deletion and document the required fields. Drop the queue reference after releasing userq_mutex because the final put may destroy the queue and acquire that mutex.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 6 +++++- include/uapi/drm/amdgpu_drm.h | 8 +++++--- 2 files changed, 10 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -1
@@ -840,8 +840,11 @@ amdgpu_userq_query_status(struct drm_file *filp, union drm_amdgpu_userq *args) queue->state ==AMDGPU_USERQ_STATE_INVALID_VA || queue->generation != amdgpu_vm_generation(adev, &fpriv->vm)) args->out_qs.flags |= AMDGPU_USERQ_QUERY_STATUS_FLAGS_BROKEN; -amdgpu_userq_put (queue); +if (amdgpu_in_reset (adev)) + args->out_qs.flags |=AMDGPU_USERQ_QUERY_STATUS_FLAGS_RESET_IN_PROGRESS ;mutex_unlock (&uq_mgr->userq_mutex); +/* The final put may destroy the queue and take userq_mutex. */ +amdgpu_userq_put (queue);return 0 ; }
@@ -903,6 +906,7 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev, }break ;case AMDGPU_USERQ_OP_FREE : +case AMDGPU_USERQ_OP_QUERY_STATUS : if (args->in.ip_type || args->in.doorbell_handle || args->in.doorbell_offset ||
include/uapi/drm/amdgpu_drm.h +5 -3
@@ -355,19 +355,21 @@ union drm_amdgpu_ctx {/* the queue is broken because it is hung, has an invalid VA, or VRAM was lost */ #define AMDGPU_USERQ_QUERY_STATUS_FLAGS_BROKEN (1 << 0) +/* indicate that a GPU reset has not completed yet */ +#define AMDGPU_USERQ_QUERY_STATUS_FLAGS_RESET_IN_PROGRESS (1 << 1)/* * This structure is a container to pass input configuration * info for all supported userqueue related operations. * For operation AMDGPU_USERQ_OP_CREATE: user is expected * to set all fields, excep the parameter 'queue_id'. -* For operation AMDGPU_USERQ_OP_FREE: the only input parameter expected -* to be set is 'queue_id', eveything else is ignored. +* For operations AMDGPU_USERQ_OP_FREE and AMDGPU_USERQ_OP_QUERY_STATUS: +* only 'op' and 'queue_id' should be set; all other fields must be zero. */ struct drm_amdgpu_userq_in {/** AMDGPU_USERQ_OP_* */ __u32 op; -/** Queue id passed for operation USERQ_OP_FREE */ +/** Queue id passed for operations USERQ_OP_FREE and USERQ_OP_QUERY_STATUS */ __u32 queue_id;/** the target GPU engine to execute workload (AMDGPU_HW_IP_*) */ __u32 ip_type;
Signature · 1 line
2.34.1
RE: [PATCH RESEND 4/4] drm/amdgpu/userq: report GPU reset in progress
AMD General
Ping for the patch3 -4.
Regards,
Prike
Quoted 85 lines
-----Original Message-----
From: Liang, Prike <[email protected]>
Sent: Tuesday, November 17, 2026 3:24 PM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Pelloux-Prayer, Pierre-Eric <Pierre-eric.Pelloux- [email protected]>; Liang, Prike <[email protected]>
Subject: [PATCH RESEND 4/4] drm/amdgpu/userq: report GPU reset in progressReport an ongoing GPU reset through
RESET_IN_PROGRESSat bit 1, alongside BROKEN at bit 0, so userspace can query whether GPU recovery is ongoing.Validate query inputs using the same rules as queue deletion and document the required fields. Drop the queue reference after releasing
userq_mutexbecause the final put may destroy the queue and acquire that mutex.Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 6 +++++- include/uapi/drm/amdgpu_drm.h | 8 +++++--- 2 files changed, 10 insertions(+), 4 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6
-1@@ -840,8 +840,11 @@ amdgpu_userq_query_status(struct drm_file *filp, union drm_amdgpu_userq *args) queue->state == AMDGPU_USERQ_STATE_INVALID_VA || queue->generation != amdgpu_vm_generation(adev, &fpriv->vm)) args->out_qs.flags |= AMDGPU_USERQ_QUERY_STATUS_FLAGS_BROKEN; - amdgpu_userq_put(queue); + if (amdgpu_in_reset(adev)) + args->out_qs.flags |= +AMDGPU_USERQ_QUERY_STATUS_FLAGS_RESET_IN_PROGRESS; mutex_unlock(&uq_mgr->userq_mutex); + /* The final put may destroy the queue and take userq_mutex. */ + amdgpu_userq_put(queue); return 0; }@@ -903,6 +906,7 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev, }break ;case AMDGPU_USERQ_OP_FREE : +case AMDGPU_USERQ_OP_QUERY_STATUS : if (args->in.ip_type || args->in.doorbell_handle ||args->in.doorbell_offset ||include/uapi/drm/amdgpu_drm.h index renamed from include/uapi/drm/amdgpu_drm.h +0
-0
6c0992d06fcf..cde359344929100644include/uapi/drm/amdgpu_drm.h +3
-2@@ -355,19 +355,21 @@ union drm_amdgpu_ctx {/* the queue is broken because it is hung, has an invalid VA, or VRAM was lost */ #define AMDGPU_USERQ_QUERY_STATUS_FLAGS_BROKEN (1 << 0) +/* indicate that a GPU reset has not completed yet */ #define +AMDGPU_USERQ_QUERY_STATUS_FLAGS_RESET_IN_PROGRESS (1 << 1)/* * This structure is a container to pass input configuration * info for all supported userqueue related operations. * For operation AMDGPU_USERQ_OP_CREATE: user is expected * to set all fields, excep the parameter 'queue_id'. -* For operation AMDGPU_USERQ_OP_FREE: the only input parameter expected -* to be set is 'queue_id', eveything else is ignored. +* For operations AMDGPU_USERQ_OP_FREE and
AMDGPU_USERQ_OP_QUERY_STATUS:
+ * only 'op' and 'queue_id' should be set; all other fields must be zero. */struct drm_amdgpu_userq_in { /** AMDGPU_USERQ_OP_* */ __u32 op;include/uapi/drm/amdgpu_drm.h +2
-2-/** Queue id passed for operation USERQ_OP_FREE */ +/** Queue id passed for operations USERQ_OP_FREE and +USERQ_OP_QUERY_STATUS */ __u32 queue_id;/** the target GPU engine to execute workload (AMDGPU_HW_IP_*) */ __u32 ip_type; --2.34.1