drm/amdgpu: secure userq lifecycle by its kref
Subsystems (2)
cover drm/amdgpu: secure userq lifecycle by its kref
A struct kref is embedded in user queue, which manages the lifecycle of a user queue. However, several code paths access user queues without hoding the kref of a user queue, especially from the doorbell XArray.
These accesses can race with the queue destruction process and result in use-after-free bugs.
To fix this issue, this commit:
1) Introduces a new helper amdgpu_lookup_queue_by_doorbell, which looks up a user queue with locking and hold its kref during access.
2) Implement asynchronous userq destruction routine, because the last put of a queue kref may be placed in a code path where can not sleep or conflict locking with the destruction process.
3) Hold kref during access the user queues
4) Keep the userq manager alive as long as its queues, to avoid UAF issues.
This seires passed amd_basic tests in igt tests
Link to previus discussions: https://lists.freedesktop.org/archives/amd-gfx/2026-August/151692.html
Please help review
Thanks
Lingshan
Zhu Lingshan (10):
drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell drm/amdgpu: keep the userq manager alive as long as its queues drm/amdgpu/gfx11: hold userq refs in private fault worker drm/amdgpu/gfx12: hold userq refs in private fault worker drm/amdgpu: implement asynchronous userq destruction routine drm/amdgpu: hold userq kref in MES reset
drm/amdgpu: hold userq kref during isolation scheduling drm/amdgpu: hold userq kref during suspend and resume drm/amdgpu: free userq by kref_put when fails to create drm/amdgpu: take queue kref in userq_create to avoid UAF
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 214 +++++++++++++++++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 18 ++ drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 7 +- drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 7 +- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 46 ++--- 5 files changed, 246 insertions(+), 46 deletions(-)
Signature · 1 line
2.53.0
PATCH RESEND 1/10 drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell
This commit introduces a new helperamdgpu_lookup_queue_by_doorbell which helps
look up a user queue with the given doorbell id
in a xarray.
This function takes a kref of the user space queue.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 ++ 2 files changed, 32 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +30 -0
@@ -609,6 +609,36 @@ struct amdgpu_usermode_queue *amdgpu_userq_get(struct amdgpu_userq_mgr *uq_mgr,return queue; } +/** +* amdgpu_lookup_queue_by_doorbell - look up a user queue by doorbell +* @xa: user queue XArray indexed by doorbell +* @doorbell: doorbell index +* +* Return: A queue with the doorbell indexed, or NULL if no such a queue found. +* +* This function increases kref of the queue, the caller +* must release the reference with amdgpu_userq_put(). +*/ +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell) +{ +struct amdgpu_usermode_queue *queue; +unsigned long flags; + +xa_lock_irqsave (xa, flags); + queue =xa_load (xa, doorbell); +if (!queue) + goto out_unlock; + +if (!kref_get_unless_zero (&queue->refcount)) + queue =NULL ; + +out_unlock: +xa_unlock_irqrestore (xa, flags); + +return queue; +} +void amdgpu_userq_put (struct amdgpu_usermode_queue *queue) {if (queue)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
@@ -151,6 +151,8 @@ struct amdgpu_db_info { };struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid); +struct amdgpu_usermode_queue * +amdgpu_lookup_queue_by_doorbell (struct xarray *xa,u32 doorbell);void amdgpu_userq_put (struct amdgpu_usermode_queue *queue);int amdgpu_userq_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);
Signature · 1 line
2.53.0
PATCH RESEND 2/10 drm/amdgpu: keep the userq manager alive as long as its queues
In reply to Zhu LingshanThe life cycle of a user queue is managed by its kref. However when destroy a userq manager, the kref_put of its queues in amdgpu_userq_mgr_fini may not be the last put, therefore the queues could be still alive after the userq manager has been destroyed, resulting in userq->userq_mgr use-after-free issues.
This commit fixes this problem by introduce a new counter refs representing for the number of its queues, and only free the userq_manager when refs == 0
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 9 +++++++ 2 files changed, 39 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +30 -0
@@ -27,6 +27,7 @@#include <linux/pm_runtime.h> #include <linux/overflow.h> #include <drm/drm_drv.h> +#include <linux/wait_bit.h> #include "amdgpu.h" #include "amdgpu_reset.h"
@@ -533,6 +534,17 @@ amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr,return r; } +static void amdgpu_userq_mgr_inc_refs (struct amdgpu_userq_mgr *uq_mgr) +{ +atomic_inc (&uq_mgr->refs); +} + +static void amdgpu_userq_mgr_dec_refs (struct amdgpu_userq_mgr *uq_mgr) +{ +if (atomic_dec_and_test (&uq_mgr->refs)) +wake_up_var (&uq_mgr->refs); +} +static int amdgpu_userq_destroy (struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_usermode_queue *queue) {
@@ -594,6 +606,8 @@ static void amdgpu_userq_kref_destroy(struct kref *kref) r =amdgpu_userq_destroy (uq_mgr, queue);if (r)drm_file_err (uq_mgr->file,"Failed to destroy usermode queue %d\n" , r); + +amdgpu_userq_mgr_dec_refs (uq_mgr); }struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid)
@@ -707,6 +721,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) queue->xcp_id = (fpriv->xcp_id !=AMDGPU_XCP_NO_PARTITION ) ? fpriv->xcp_id :0 ; queue->userq_mgr = uq_mgr; +amdgpu_userq_mgr_inc_refs (uq_mgr);INIT_DELAYED_WORK (&queue->hang_detect_work, amdgpu_userq_hang_detect_work);
@@ -819,6 +834,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) free_queue:trace_amdgpu_userq_create_end (queue, r);kfree (queue); +amdgpu_userq_mgr_dec_refs (uq_mgr); err_pm_runtime:pm_runtime_put_autosuspend (adev_to_drm (adev)->dev);return r;
@@ -1331,6 +1347,7 @@ int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f {mutex_init (&userq_mgr->userq_mutex);xa_init_flags (&userq_mgr->userq_xa,XA_FLAGS_ALLOC ); +atomic_set (&userq_mgr->refs,0 ); userq_mgr->adev = adev; userq_mgr->file = file_priv; userq_mgr->proc_ctx_allocated = false;
@@ -1380,6 +1397,19 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)amdgpu_userq_put (queue); } +/* +* The above amdgpu_userq_put() may not be the last put +* of the kref of a user queue, therefore there could +* be some queues still alive even when the userq manager +* has been destroyed. This wait_evet() blocks +* amdgpu_userq_mgr_fini(), so keep userq_mgr alive +* while any queues holding it. +* +* This prevents queue->userq_mgr use-after-free issues. +*/ +wait_var_event (&userq_mgr->refs, + !atomic_read_acquire (&userq_mgr->refs)); +xa_destroy (&userq_mgr->userq_xa);/*
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +9 -0
@@ -126,6 +126,15 @@ struct amdgpu_userq_mgr {*/ struct xarray userq_xa;struct mutex userq_mutex; + +/** +* @refs: +* +* Each queue increases this counter when join this manager, +* and decreases it when leave this manager. +*/ +atomic_t refs; +struct amdgpu_device *adev;struct delayed_work resume_work;struct drm_file *file;
Signature · 1 line
2.53.0
PATCH RESEND 3/10 drm/amdgpu/gfx11: hold userq refs in private fault worker
In reply to Zhu LingshanThe GFX11 user queue private fault woker loads the relevnt user queue from the userq doorbell xarray. However it does not hold the spin_lock of the xarray when walking the xarray, and does not increase the kref of the user queue, so it races with queue destruction path and may run into an use-after-free userq problem.
This commit fixes this UAF problem by utilizing amdgpu_lookup_queue_by_doorbell helper, which properly hoding the xarray spin lock and the kref of the user queue.
--- drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +5 -2
@@ -6733,9 +6733,12 @@ static void gfx_v11_0_userq_priv_fault_work(struct work_struct *work) doorbell = (db_ctrl &CP_RB_DOORBELL_CONTROL__DOORBELL_OFFSET_MASK ) >>CP_RB_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT ; - q =xa_load (&adev->userq_doorbell_xa, doorbell); -if (q) + q =amdgpu_lookup_queue_by_doorbell (&adev->userq_doorbell_xa, + doorbell); +if (q) {amdgpu_userq_start_hang_detect_work (q); +amdgpu_userq_put (q); + } } }
Signature · 1 line
2.53.0
PATCH RESEND 4/10 drm/amdgpu/gfx12: hold userq refs in private fault worker
In reply to Zhu LingshanThe GFX12 user queue private fault woker loads the relevnt user queue from the userq doorbell xarray. However it does not hold the spin_lock of the xarray when walking the xarray, and does not increase the kref of the user queue, so it races with queue destruction path and may run into an use-after-free userq problem.
This commit fixes this UAF problem by utilizing amdgpu_lookup_queue_by_doorbell helper, which properly hoding the xarray spin lock and the kref of the user queue.
--- drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +5 -2
@@ -5070,9 +5070,12 @@ static void gfx_v12_0_userq_priv_fault_work(struct work_struct *work) doorbell = (db_ctrl &CP_RB_DOORBELL_CONTROL__DOORBELL_OFFSET_MASK ) >>CP_RB_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT ; - q =xa_load (&adev->userq_doorbell_xa, doorbell); -if (q) + q =amdgpu_lookup_queue_by_doorbell (&adev->userq_doorbell_xa, + doorbell); +if (q) {amdgpu_userq_start_hang_detect_work (q); +amdgpu_userq_put (q); + } } }
Signature · 1 line
2.53.0
PATCH RESEND 5/10 drm/amdgpu: implement asynchronous userq destruction routine
In reply to Zhu LingshanThe last kref of a user queue may be released
from a context where can not sleep or
require an alrady hold lock(reset_domain->sem, reset_mutext, etc) in the same context.
This commit helps resovle these issues by refactoring the user queue destruction function to a deferred work item, so it does not conflict with the caller context.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 15 ++++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 7 +++++++ 2 files changed, 19 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +12 -3
@@ -596,11 +596,11 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_quereturn r; } -static void amdgpu_userq_kref_destroy (struct kref *kref) +static void amdgpu_userq_destroy_worker (struct work_struct *work) {int r;struct amdgpu_usermode_queue *queue = - container_of(kref, struct amdgpu_usermode_queue, refcount); + container_of(work, struct amdgpu_usermode_queue, destroy_work);struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr; r =amdgpu_userq_destroy (uq_mgr, queue);
@@ -610,6 +610,14 @@ static void amdgpu_userq_kref_destroy(struct kref *kref)amdgpu_userq_mgr_dec_refs (uq_mgr); } +static void amdgpu_userq_kref_release (struct kref *kref) +{ +struct amdgpu_usermode_queue *queue = + container_of(kref, struct amdgpu_usermode_queue, refcount); + +WARN_ON_ONCE (!queue_work (system_unbound_wq, &queue->destroy_work)); +} +struct amdgpu_usermode_queue *amdgpu_userq_get (struct amdgpu_userq_mgr *uq_mgr,u32 qid) {struct amdgpu_usermode_queue *queue;
@@ -656,7 +664,7 @@ amdgpu_lookup_queue_by_doorbell(struct xarray *xa, u32 doorbell)void amdgpu_userq_put (struct amdgpu_usermode_queue *queue) {if (queue) -kref_put (&queue->refcount, amdgpu_userq_kref_destroy); +kref_put (&queue->refcount, amdgpu_userq_kref_release); } static int amdgpu_userq_priority_permit(struct drm_file *filp,
@@ -724,6 +732,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)amdgpu_userq_mgr_inc_refs (uq_mgr);INIT_DELAYED_WORK (&queue->hang_detect_work, amdgpu_userq_hang_detect_work); +INIT_WORK (&queue->destroy_work, amdgpu_userq_destroy_worker); r =amdgpu_userq_fence_driver_alloc (adev, &queue->fence_drv);if (r)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +7 -0
@@ -87,6 +87,13 @@ struct amdgpu_usermode_queue {* Delayed work which runs when userq_fences time out. */ struct delayed_work hang_detect_work; + +/** +* @destroy_work: +* +* Work struct used to destroy the queue when kref == 0 +*/ +struct work_struct destroy_work;struct kref refcount;union {
Signature · 1 line
2.53.0
PATCH RESEND 6/10 drm/amdgpu: hold userq kref in MES reset
In reply to Zhu LingshanCurrent mes_userq_reset_queue() walks the user queue doorbell xarray without holding its spin lock and does not take a queue->kref when if finds a queue. So the queue could be unexpectedly deconstructed and casuing an use-after-free problem.
This commit fixes this problem by using the helper amdgpu_lookup_queue_by_doorbell() to properly acquire the spin lock of the xarray, and hold its kref during processing the queue.
--- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 46 +++++++++++----------- 1 file changed, 23 insertions(+), 23 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +23 -23
@@ -244,30 +244,30 @@ int mes_userq_reset_queue(struct amdgpu_device *adev, {struct amdgpu_usermode_queue *uq;bool use_mmio = adev->gfx.mec.use_mmio_for_reset; -unsigned long uq_id; -int r; +int r =0 ; -xa_for_each (&adev->userq_doorbell_xa, uq_id, uq) { -if (uq->queue_type == queue_type) { -if (uq == guilty_uq) -continue ; -if (uq->doorbell_index == db) { - uq->state =AMDGPU_USERQ_STATE_HUNG ; -if (use_mmio) - r =amdgpu_mes_reset_queue_mmio (adev, queue_type,0 ,1 , pipe, queue,0 ); -else - r =amdgpu_mes_reset_user_queue (adev, queue_type, db,0 ); -if (r) -return r; - r =mes_userq_unmap (uq); -if (r) -return r; -amdgpu_userq_fence_driver_force_completion (uq); -break ; - } - } - } -return 0 ; + uq =amdgpu_lookup_queue_by_doorbell (&adev->userq_doorbell_xa, db); +if (!uq) +return 0 ; + +if (uq == guilty_uq || uq->queue_type != queue_type) + goto put_queue; + + uq->state =AMDGPU_USERQ_STATE_HUNG ; +if (use_mmio) + r =amdgpu_mes_reset_queue_mmio (adev, queue_type,0 ,1 , pipe, queue,0 ); +else + r =amdgpu_mes_reset_user_queue (adev, queue_type, db,0 ); +if (r) + goto put_queue; + + r =mes_userq_unmap (uq); +if (!r) +amdgpu_userq_fence_driver_force_completion (uq); + +put_queue: +amdgpu_userq_put (uq); +return r; } static int mes_userq_create_ctx_space(struct amdgpu_userq_mgr *uq_mgr,
Signature · 1 line
2.53.0
PATCH RESEND 7/10 drm/amdgpu: hold userq kref during isolation scheduling
In reply to Zhu LingshanThe enforce-isolation stop and start handlers iterate the doorbell XArray, but process a user queue without holding its kref. Therefore, a concurrent queue destruction process can free a queue before these paths finish their work, causing use-after-free problems.
This commit fixes this issue by introducing a new helper amdgpu_userq_xa_find() which finds a queue from a XArray and hold its kref, and employ this helper in the enforce-isolation stop and start handlers.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 72 +++++++++++++++++++++-- 1 file changed, 68 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +68 -4
@@ -661,6 +661,54 @@ amdgpu_lookup_queue_by_doorbell(struct xarray *xa, u32 doorbell)return queue; } +/** +* amdgpu_userq_xa_find - search the XArray for a queue +* @xa: user queue XArray +* @index: first index to search, updated by every iteration of the search +* +* Return: a queue which has the lowest index that is at least @index, +* or NULL when no queue was found. +* +* The caller must release the kref of a queue with amdgpu_userq_put() after use. +*/ +static struct amdgpu_usermode_queue * +amdgpu_userq_xa_find (struct xarray *xa,unsigned long *index) +{ +struct amdgpu_usermode_queue *queue; +unsigned long flags; + +xa_lock_irqsave (xa, flags); + queue =xa_find (xa, index,ULONG_MAX ,XA_PRESENT ); +while (queue) { +/* +* If found a queue but failed to get a kref, +* it means the queue is in destruction process, +* so skip it by continuing the loop. +* +* If get a kref of the queue, break the loop and return it. +*/ +if (kref_get_unless_zero (&queue->refcount)) +break ; + +/* +* If the index is ULONG_MAX, we have reached the end of the XArray. +* Break the loop and return NULL because ULONG_MAX + 1 = 0, +* which is the start of the XArray. +*/ +if (*index ==ULONG_MAX ) { + queue =NULL ; +break ; + } + + (*index)++; + queue =xa_find (xa, index,ULONG_MAX ,XA_PRESENT ); + } + +xa_unlock_irqrestore (xa, flags); + +return queue; +} +void amdgpu_userq_put (struct amdgpu_usermode_queue *queue) {if (queue)
@@ -1496,7 +1544,7 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,u32 ip_mask =amdgpu_userq_get_supported_ip_mask (adev);struct amdgpu_usermode_queue *queue;struct amdgpu_userq_mgr *uqm; -unsigned long queue_id; +unsigned long queue_id =0 ;int ret =0 , r;/* only need to stop gfx/compute */
@@ -1506,7 +1554,8 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,if (adev->userq_halt_for_enforce_isolation)dev_warn (adev->dev,"userq scheduling already stopped!\n" ); adev->userq_halt_for_enforce_isolation = true; -xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + queue =amdgpu_userq_xa_find (&adev->userq_doorbell_xa, &queue_id); +while (queue) { uqm = queue->userq_mgr;cancel_delayed_work_sync (&uqm->resume_work);mutex_lock (&uqm->userq_mutex);
@@ -1518,6 +1567,13 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev, ret = r; }mutex_unlock (&uqm->userq_mutex); +amdgpu_userq_put (queue); + +if (queue_id ==ULONG_MAX ) +break ; + queue_id++; + queue =amdgpu_userq_xa_find (&adev->userq_doorbell_xa, + &queue_id); }return ret;
@@ -1529,7 +1585,7 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,u32 ip_mask =amdgpu_userq_get_supported_ip_mask (adev);struct amdgpu_usermode_queue *queue;struct amdgpu_userq_mgr *uqm; -unsigned long queue_id; +unsigned long queue_id =0 ;int ret =0 , r;/* only need to stop gfx/compute */
@@ -1541,7 +1597,8 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, adev->userq_halt_for_enforce_isolation = false; -xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + queue =amdgpu_userq_xa_find (&adev->userq_doorbell_xa, &queue_id); +while (queue) { uqm = queue->userq_mgr;mutex_lock (&uqm->userq_mutex); if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
@@ -1552,6 +1609,13 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, ret = r; }mutex_unlock (&uqm->userq_mutex); +amdgpu_userq_put (queue); + +if (queue_id ==ULONG_MAX ) +break ; + queue_id++; + queue =amdgpu_userq_xa_find (&adev->userq_doorbell_xa, + &queue_id); }return ret;
Signature · 1 line
2.53.0
PATCH RESEND 8/10 drm/amdgpu: hold userq kref during suspend and resume
In reply to Zhu LingshanThe userq suspend and resume handlers walks the doorbell XArray but process a user queuewithout holding its kref.
Therefore, a concurrent queue destruction process can free a queue before these handlers finish their work, causing an use-after-free problem.
This commit fixes this problem by using amdgpu_userq_xa_find helper which properly holds the kref of a queue in a loop of searching for queues.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 35 +++++++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +29 -6
@@ -1493,23 +1493,35 @@ int amdgpu_userq_suspend(struct amdgpu_device *adev)u32 ip_mask =amdgpu_userq_get_supported_ip_mask (adev);struct amdgpu_usermode_queue *queue;struct amdgpu_userq_mgr *uqm; -unsigned long queue_id; +unsigned long queue_id =0 ;int r;if (!ip_mask)return 0 ; -xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + queue =amdgpu_userq_xa_find (&adev->userq_doorbell_xa, &queue_id); +while (queue) { uqm = queue->userq_mgr;cancel_delayed_work_sync (&uqm->resume_work); -guard (mutex)(&uqm->userq_mutex); +mutex_lock (&uqm->userq_mutex);if (adev->in_s0ix) r =amdgpu_userq_preempt_helper (queue);else r =amdgpu_userq_unmap_helper (queue); +mutex_unlock (&uqm->userq_mutex); +amdgpu_userq_put (queue); +if (r)return r; + +if (queue_id ==ULONG_MAX ) +break ; + + queue_id++; + queue =amdgpu_userq_xa_find (&adev->userq_doorbell_xa, + &queue_id); } +return 0 ; }
@@ -1518,21 +1530,32 @@ int amdgpu_userq_resume(struct amdgpu_device *adev)u32 ip_mask =amdgpu_userq_get_supported_ip_mask (adev);struct amdgpu_usermode_queue *queue;struct amdgpu_userq_mgr *uqm; -unsigned long queue_id; +unsigned long queue_id =0 ;int r;if (!ip_mask)return 0 ; -xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + queue =amdgpu_userq_xa_find (&adev->userq_doorbell_xa, &queue_id); +while (queue) { uqm = queue->userq_mgr; -guard (mutex)(&uqm->userq_mutex); +mutex_lock (&uqm->userq_mutex);if (adev->in_s0ix) r =amdgpu_userq_restore_helper (queue);else r =amdgpu_userq_map_helper (queue); +mutex_unlock (&uqm->userq_mutex); +amdgpu_userq_put (queue); +if (r)return r; + +if (queue_id ==ULONG_MAX ) +break ; + + queue_id++; + queue =amdgpu_userq_xa_find (&adev->userq_doorbell_xa, + &queue_id); }return 0 ;
Signature · 1 line
2.53.0
PATCH RESEND 9/10 drm/amdgpu: free userq by kref_put when fails to create
In reply to Zhu LingshanIn amdgpu_userq_create(), once the user queue is published to userq_doorbell_xa, other threads could access the queue from the XArray. But the queue may fails to map the queue and kfree(queue), this causes the accessor use-after-free issue.
This commit fixes this issue by properly
get and put the kref of a queue to maintain
the lifecycle of a user queue.
There are some minor improvements in this commit:
1) Early detach the doorbell of a queue when fails to map a queue, because the map helper set the queue state to HUNG, and the asynchronous post reset helper amdgpu_userq_post_reset can find the queue from the XArray and remaps the queue again, which causes a queue leaking because the user space already receive an error code for the queue
from amdgpu_userq_create()
2) Use xa_cmpxchg_irq in amdgpu_userq_detach_doorbell(). Once fails to map a queue, the queue destruction process is asynchronous (a delayed worker), and a new created queue could re-use the doorbell, xa_cmpxchg_irq compares the doorbell which to be deleted with the doorbell which @index points to, so it does not detach the doorbell of the new created queue.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +15 -5
@@ -424,7 +424,9 @@ static void amdgpu_userq_detach_doorbell(struct amdgpu_usermode_queue *queue)struct amdgpu_device *adev = queue->userq_mgr->adev;down_read (&adev->reset_domain->sem); -xa_erase_irq (&adev->userq_doorbell_xa, queue->doorbell_index); +/* It doesn't alloc any memory here, so pass 0 to gfp */ +xa_cmpxchg_irq (&adev->userq_doorbell_xa, queue->doorbell_index, + queue,NULL ,0 );up_read (&adev->reset_domain->sem); }
@@ -836,6 +838,14 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)if (r) goto clean_mqd; +/* +* Once the queue has been published to doorbell_xa, +* it could be accessed by other threads, so it has to be +* destroyed through kref put. The destroy work decreases +* userq_count, so we have to increase it here. +*/ +atomic_inc (&uq_mgr->userq_count[queue->queue_type]); +amdgpu_userq_ensure_ev_fence (&fpriv->userq_mgr, &fpriv->evf_mgr);/* don't map the queue if scheduling is halted */
@@ -851,12 +861,14 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)if (r) {drm_file_err (uq_mgr->file,"Failed to map Queue\n" );trace_amdgpu_userq_create_end (queue, r); +amdgpu_userq_detach_doorbell (queue);mutex_unlock (&uq_mgr->userq_mutex); - goto erase_doorbell; +amdgpu_userq_put (queue); + +return r; } } -atomic_inc (&uq_mgr->userq_count[queue->queue_type]);mutex_unlock (&uq_mgr->userq_mutex); r = xa_alloc(&uq_mgr->userq_xa, &qid, queue,
@@ -877,8 +889,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) args->out.queue_id = qid;return 0 ; -erase_doorbell: -xa_erase_irq (&adev->userq_doorbell_xa, index); clean_mqd: uq_funcs->mqd_destroy (queue); clean_doorbell_bo:
Signature · 1 line
2.53.0
PATCH RESEND 10/10 drm/amdgpu: take queue kref in userq_create to avoid UAF
In reply to Zhu LingshanIn amdgpu_userq_create(), once a newly created user queue has been assigned a qid and published to userq_xa, a concurrent AMDGPU_USERQ_OP_FREE ioctl can free the queue, resulting in use-after-free issues in amdgpu_userq_create.
This is surely a user space bug, but kernel should not crash. This commit fixes this issue by taking an additional kref before xa_alloc()
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +11 -1
@@ -871,22 +871,32 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)mutex_unlock (&uq_mgr->userq_mutex); +/* +* A concurrent AMDGPU_USERQ_OP_FREE ioctl can free the queue once +* xa_alloc() publishes it and assigne a qid to it. +* Take a kref to avoid use-after-free issues. +*/ +kref_get (&queue->refcount); r =xa_alloc (&uq_mgr->userq_xa, &qid, queue,XA_LIMIT (1 ,AMDGPU_MAX_USERQ_COUNT ),GFP_KERNEL );if (r) {/* -* This drops the last reference which should take care of +* This drops the last two references which should take care of * all cleanup. */ trace_amdgpu_userq_create_end (queue, r);amdgpu_userq_put (queue); +amdgpu_userq_put (queue); +return r; }amdgpu_debugfs_userq_init (filp, queue, qid);trace_amdgpu_userq_create_end (queue,0 ); args->out.queue_id = qid; +amdgpu_userq_put (queue); +return 0 ; clean_mqd:
Signature · 1 line
2.53.0
AMD General
Gentle Ping Alex
-----Original Message-----
From: Zhu, Lingshan <[email protected]>
Sent: Monday, September 14, 2026 9:07 PM
To: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>
Cc: Huang, Ray <[email protected]>; [email protected]; Zhu, Lingshan <[email protected]>
Subject: [PATCH RESEND 00/10] drm/amdgpu: secure userq lifecycle by its kref
A struct kref is embedded in user queue, which manages the lifecycle of a user queue. However, several code paths access user queues without hoding the kref of a user queue, especially from the doorbell XArray.
These accesses can race with the queue destruction process and result in use-after-free bugs.
To fix this issue, this commit:
1) Introduces a new helper amdgpu_lookup_queue_by_doorbell, which looks up a user queue with locking and hold its kref during access.
2) Implement asynchronous userq destruction routine, because the last put of a queue kref may be placed in a code path where can not sleep or conflict locking with the destruction process.
3) Hold kref during access the user queues
4) Keep the userq manager alive as long as its queues, to avoid UAF issues.
This seires passed amd_basic tests in igt tests
Link to previus discussions: https://lists.freedesktop.org/archives/amd-gfx/2026-August/151692.html
Please help review
Thanks
Lingshan
Zhu Lingshan (10):
drm/amdgpu: introduce amdgpu_lookup_queue_by_doorbell drm/amdgpu: keep the userq manager alive as long as its queues drm/amdgpu/gfx11: hold userq refs in private fault worker drm/amdgpu/gfx12: hold userq refs in private fault worker drm/amdgpu: implement asynchronous userq destruction routine drm/amdgpu: hold userq kref in MES reset
drm/amdgpu: hold userq kref during isolation scheduling drm/amdgpu: hold userq kref during suspend and resume drm/amdgpu: free userq by kref_put when fails to create drm/amdgpu: take queue kref in userq_create to avoid UAF
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 214 +++++++++++++++++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 18 ++ drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 7 +- drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 7 +- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 46 ++--- 5 files changed, 246 insertions(+), 46 deletions(-)
--
2.53.0
Deucher, Alexander
maintainerAMD General
Quoted 9 lines
-----Original Message-----
From: Zhu, Lingshan <[email protected]>
Sent: Monday, September 14, 2026 9:55 PM
To: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>
Cc: Huang, Ray <[email protected]>; [email protected]
Subject: RE: [PATCH RESEND 00/10] drm/amdgpu: secure userq lifecycle by its kref
Quoted 3 lines
AMD General
Gentle Ping Alex
It doesn't look like Christian's concerns were addressed.
Alex
Quoted 63 lines
-----Original Message-----
From: Zhu, Lingshan <[email protected]>
Sent: Monday, September 14, 2026 9:07 PM
To: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>
Cc: Huang, Ray <[email protected]>; [email protected]; Zhu, Lingshan <[email protected]>
Subject: [PATCH RESEND 00/10] drm/amdgpu: secure userq lifecycle by its krefA struct kref is embedded in user queue, which manages the lifecycle of a user queue. However, several code paths access user queues without hoding the kref of a user queue, especially from the doorbell XArray.
These accesses can race with the queue destruction process and result in use- after-free bugs.
To fix this issue, this commit:
1) Introduces a new helperamdgpu_lookup_queue_by_doorbell, which looks up a user queue with locking and hold its kref during access.2) Implement asynchronous userq destruction routine, because the last put of a queue kref may be placed in a code path where can not sleep or conflict locking with the destruction process.
3) Hold kref during access the user queues
4) Keep the userq manager alive as long as its queues, to avoid UAF issues.
This seires passed
amd_basictests in igt testsLink to previus discussions: https://lists.freedesktop.org/archives/amd-
gfx/2026-August/151692.htmlPlease help review
Thanks
LingshanZhu Lingshan (10):
drm/amdgpu: introduceamdgpu_lookup_queue_by_doorbelldrm/amdgpu: keep the userq manager alive as long as its queues drm/amdgpu/gfx11: hold userq refs in private fault worker drm/amdgpu/gfx12: hold userq refs in private fault worker drm/amdgpu: implement asynchronous userq destruction routine drm/amdgpu: hold userq kref in MES reset
drm/amdgpu: hold userq kref during isolation scheduling drm/amdgpu: hold userq kref during suspend and resume drm/amdgpu: free userq bykref_putwhen fails to create drm/amdgpu: take queue kref inuserq_createto avoid UAFdrivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 214 +++++++++++++++++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 18 ++ drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 7 +- drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 7 +- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 46 ++--- 5 files changed, 246 insertions(+), 46 deletions(-)--
2.53.0
Quoted 11 lines · Alexander
AMD General
Quoted 9 lines
-----Original Message-----
From: Zhu, Lingshan <[email protected]>
Sent: Monday, September 14, 2026 9:55 PM
To: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>
Cc: Huang, Ray <[email protected]>; [email protected]
Subject: RE: [PATCH RESEND 00/10] drm/amdgpu: secure userq lifecycle by its kref
Quoted 4 lines
Quoted 3 lines
AMD General
Gentle Ping Alex
It doesn't look like Christian's concerns were addressed.
Hello Alex,
I didn't hear back from Christian for almost two weeks, hope I did not miss any emails
This series tries to fix a common use-after-free issues for user queue.
The user queues are destroyed when its kref get to zero. So any code paths that work on a user queue has to take a kref of the queue to make sure the queue is alive during its operation.
However there are several critical functions does not take a kref of a queue when operate on it, for example mes_userq_reset_queue(patch 6) and gfx_v11_0_userq_priv_fault_work(patch 3).
There could be use-after-free bugs if the user space invokes AMDGPU_USERQ_OP_FREE, or close the drm file, or the user space program unexpectedly crashes/exits when kernel processing the user queue without holding a kref.
This series tries to fix these problems, and this series is a dependency of the PMC work.
Please help Review
Thanks
Lingshan
Quoted 65 lines
Alex
Quoted 62 lines
-----Original Message-----
From: Zhu, Lingshan <[email protected]>
Sent: Monday, September 14, 2026 9:07 PM
To: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>
Cc: Huang, Ray <[email protected]>; [email protected]; Zhu, Lingshan <[email protected]>
Subject: [PATCH RESEND 00/10] drm/amdgpu: secure userq lifecycle by its krefA struct kref is embedded in user queue, which manages the lifecycle of a user queue. However, several code paths access user queues without hoding the kref of a user queue, especially from the doorbell XArray.
These accesses can race with the queue destruction process and result in use- after-free bugs.
To fix this issue, this commit:
1) Introduces a new helperamdgpu_lookup_queue_by_doorbell, which looks up a user queue with locking and hold its kref during access.2) Implement asynchronous userq destruction routine, because the last put of a queue kref may be placed in a code path where can not sleep or conflict locking with the destruction process.
3) Hold kref during access the user queues
4) Keep the userq manager alive as long as its queues, to avoid UAF issues.
This seires passed
amd_basictests in igt testsLink to previus discussions: https://lists.freedesktop.org/archives/amd-
gfx/2026-August/151692.htmlPlease help review
Thanks
LingshanZhu Lingshan (10):
drm/amdgpu: introduceamdgpu_lookup_queue_by_doorbelldrm/amdgpu: keep the userq manager alive as long as its queues drm/amdgpu/gfx11: hold userq refs in private fault worker drm/amdgpu/gfx12: hold userq refs in private fault worker drm/amdgpu: implement asynchronous userq destruction routine drm/amdgpu: hold userq kref in MES reset
drm/amdgpu: hold userq kref during isolation scheduling drm/amdgpu: hold userq kref during suspend and resume drm/amdgpu: free userq bykref_putwhen fails to create drm/amdgpu: take queue kref inuserq_createto avoid UAFdrivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 214 +++++++++++++++++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 18 ++ drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 7 +- drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 7 +- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 46 ++--- 5 files changed, 246 insertions(+), 46 deletions(-)--
2.53.0
Gentle Ping Alex
Quoted 103 lines · Lingshan
Quoted 11 lines · Alexander
AMD General
Quoted 9 lines
-----Original Message-----
From: Zhu, Lingshan <[email protected]>
Sent: Monday, September 14, 2026 9:55 PM
To: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>
Cc: Huang, Ray <[email protected]>; [email protected]
Subject: RE: [PATCH RESEND 00/10] drm/amdgpu: secure userq lifecycle by its krefQuoted 4 lines
Quoted 3 lines
AMD General
Gentle Ping Alex
It doesn't look like Christian's concerns were addressed.
Hello Alex,
I didn't hear back from Christian for almost two weeks, hope I did not miss any emails
This series tries to fix a common use-after-free issues for user queue.
The user queues are destroyed when its kref get to zero. So any code paths that work on a user queue has to take a kref of the queue to make sure the queue is alive during its operation.
However there are several critical functions does not take a kref of a queue when operate on it, for example
mes_userq_reset_queue(patch 6)andgfx_v11_0_userq_priv_fault_work(patch 3).There could be use-after-free bugs if the user space invokes
AMDGPU_USERQ_OP_FREE, or close the drm file, or the user space program unexpectedly crashes/exits when kernel processing the user queue without holding a kref.This series tries to fix these problems, and this series is a dependency of the PMC work.
Please help Review
Thanks
LingshanQuoted 64 lines
Alex
Quoted 62 lines
-----Original Message-----
From: Zhu, Lingshan <[email protected]>
Sent: Monday, September 14, 2026 9:07 PM
To: Deucher, Alexander <[email protected]>; Kuehling, Felix <[email protected]>
Cc: Huang, Ray <[email protected]>; [email protected]; Zhu, Lingshan <[email protected]>
Subject: [PATCH RESEND 00/10] drm/amdgpu: secure userq lifecycle by its krefA struct kref is embedded in user queue, which manages the lifecycle of a user queue. However, several code paths access user queues without hoding the kref of a user queue, especially from the doorbell XArray.
These accesses can race with the queue destruction process and result in use- after-free bugs.
To fix this issue, this commit:
1) Introduces a new helperamdgpu_lookup_queue_by_doorbell, which looks up a user queue with locking and hold its kref during access.2) Implement asynchronous userq destruction routine, because the last put of a queue kref may be placed in a code path where can not sleep or conflict locking with the destruction process.
3) Hold kref during access the user queues
4) Keep the userq manager alive as long as its queues, to avoid UAF issues.
This seires passed
amd_basictests in igt testsLink to previus discussions: https://lists.freedesktop.org/archives/amd-
gfx/2026-August/151692.htmlPlease help review
Thanks
LingshanZhu Lingshan (10):
drm/amdgpu: introduceamdgpu_lookup_queue_by_doorbelldrm/amdgpu: keep the userq manager alive as long as its queues drm/amdgpu/gfx11: hold userq refs in private fault worker drm/amdgpu/gfx12: hold userq refs in private fault worker drm/amdgpu: implement asynchronous userq destruction routine drm/amdgpu: hold userq kref in MES reset
drm/amdgpu: hold userq kref during isolation scheduling drm/amdgpu: hold userq kref during suspend and resume drm/amdgpu: free userq bykref_putwhen fails to create drm/amdgpu: take queue kref inuserq_createto avoid UAFdrivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 214 +++++++++++++++++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 18 ++ drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 7 +- drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 7 +- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 46 ++--- 5 files changed, 246 insertions(+), 46 deletions(-)--
2.53.0