drm/amdgpu: Convert amdgpu userqueue management from IDR to XArray
Subsystems (2)
PATCH v6 1/2 drm/amdgpu: Convert amdgpu userqueue management from IDR to XArray
This commit refactors the AMDGPU userqueue management subsystem to replace IDR (ID Allocation) with XArray for improved performance, scalability, and maintainability. The changes address several issues with the previous IDR implementation and provide better locking semantics.
Key changes:
1. **Global XArray Introduction**:
- Added userq_doorbell_xa to struct amdgpu_device for global queue tracking
- Uses doorbell_index as key for efficient global lookup
- Replaces the previous userq_mgr_list linked list approach
2. **Per-process XArray Conversion**:
- Replaced userq_idr with userq_mgr_xa in struct amdgpu_userq_mgr
- Maintains per-process queue tracking with queue_id as key
- Uses XA_FLAGS_ALLOC for automatic ID allocation
3. **Locking Improvements**:
- Removed global userq_mutex from struct amdgpu_device
- Replaced with fine-grained XArray locking using XArray's internal spinlocks
4. **Runtime Idle Check Optimization**:
- Updated amdgpu_runtime_idle_check_userq() to use xa_empty
5. **Queue Management Functions**:
- Converted all IDR operations to equivalent XArray functions:
- idr_alloc() → xa_alloc()
- idr_find() → xa_load()
- idr_remove() → xa_erase()
- idr_for_each() → xa_for_each()
Benefits:
- **Performance**: XArray provides better scalability for large numbers of queues
- **Memory Efficiency**: Reduced memory overhead compared to IDR
- **Thread Safety**: Improved locking semantics with XArray's internal spinlocks
v2: rename userq_global_xa/userq_xa to userq_doorbell_xa/userq_mgr_xa
Remove xa_lock and use its own lock.
v3: Set queue->userq_mgr = uq_mgr in amdgpu_userq_create()
--- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 8 +- drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 3 +- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 16 +- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 143 +++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 8 +- .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 4 +- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 22 ++- 7 files changed, 97 insertions(+), 107 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +6 -2
@@ -1174,6 +1174,12 @@ struct amdgpu_device {* queue fence. */ struct xarray userq_xa; +/** +* @userq_doorbell_xa: Global user queue map (doorbell index → queue) +* Key: doorbell_index (unique global identifier for the queue) +* Value: struct amdgpu_usermode_queue +*/ +struct xarray userq_doorbell_xa;/* df */ struct amdgpu_df df;
@@ -1308,8 +1314,6 @@ struct amdgpu_device {*/ bool apu_prefer_gtt; -struct list_head userq_mgr_list; -struct mutex userq_mutex;bool userq_halt_for_enforce_isolation;struct amdgpu_uid *uid_info;
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -2
@@ -4483,7 +4483,6 @@ int amdgpu_device_init(struct amdgpu_device *adev,mutex_init (&adev->gfx.userq_sch_mutex);mutex_init (&adev->gfx.workload_profile_mutex);mutex_init (&adev->vcn.workload_profile_mutex); -mutex_init (&adev->userq_mutex);amdgpu_device_init_apu_flags (adev);
@@ -4511,7 +4510,7 @@ int amdgpu_device_init(struct amdgpu_device *adev,INIT_LIST_HEAD (&adev->pm.od_kobj_list); -INIT_LIST_HEAD (&adev->userq_mgr_list); +xa_init (&adev->userq_doorbell_xa);INIT_DELAYED_WORK (&adev->delayed_init_work, amdgpu_device_delayed_init_work_handler);
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -15
@@ -2771,22 +2771,8 @@ static int amdgpu_runtime_idle_check_userq(struct device *dev)struct pci_dev *pdev =to_pci_dev (dev);struct drm_device *drm_dev =pci_get_drvdata (pdev);struct amdgpu_device *adev =drm_to_adev (drm_dev); -struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp; -int queue_id; -int ret =0 ; - -mutex_lock (&adev->userq_mutex); -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { - ret = -EBUSY ; - goto done; - } - } -done: -mutex_unlock (&adev->userq_mutex); -return ret; +return xa_empty (&adev->userq_doorbell_xa) ?0 : -EBUSY ; }static int amdgpu_pmops_runtime_suspend (struct device *dev)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +71 -72
319 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6 -2
@@ -88,11 +88,15 @@ struct amdgpu_userq_funcs {/* Usermode queues for gfx */ struct amdgpu_userq_mgr { -struct idr userq_idr; +/** +* @userq_mgr_xa: Per-process user queue map (queue ID → queue) +* Key: queue_id (unique ID within the process's userq manager) +* Value: struct amdgpu_usermode_queue +*/ +struct xarray userq_mgr_xa;struct mutex userq_mutex;struct amdgpu_device *adev;struct delayed_work resume_work; -struct list_head list;struct drm_file *file; };
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -2
@@ -537,7 +537,7 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data, }/* Retrieve the user queue */ - queue =idr_find (&userq_mgr->userq_idr, args->queue_id); + queue =xa_load (&userq_mgr->userq_mgr_xa, args->queue_id);if (!queue) { r = -ENOENT ; goto put_gobj_write;
@@ -899,7 +899,7 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,*/ num_fences =dma_fence_dedup_array (fences, num_fences); - waitq =idr_find (&userq_mgr->userq_idr, wait_info->waitq_id); + waitq =xa_load (&userq_mgr->userq_mgr_xa, wait_info->waitq_id);if (!waitq) { r = -EINVAL ; goto free_fences;
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +10 -12
@@ -205,9 +205,9 @@ static int mes_userq_detect_and_reset(struct amdgpu_device *adev,int db_array_size =amdgpu_mes_get_hung_queue_db_array_size (adev);struct mes_detect_and_reset_queue_input input;struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp;unsigned int hung_db_num =0 ; -int queue_id, r, i; +unsigned long queue_id; +int r, i;u32 db_array[4 ];if (db_array_size >4 ) {
@@ -227,16 +227,14 @@ static int mes_userq_detect_and_reset(struct amdgpu_device *adev,if (r) {dev_err (adev->dev,"Failed to detect and reset queues, err (%d)\n" , r); }else if (hung_db_num) { -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { -if (queue->queue_type == queue_type) { -for (i =0 ; i < hung_db_num; i++) { -if (queue->doorbell_index == db_array[i]) { - queue->state =AMDGPU_USERQ_STATE_HUNG ; -atomic_inc (&adev->gpu_reset_counter); -amdgpu_userq_fence_driver_force_completion (queue); -drm_dev_wedged_event (adev_to_drm (adev),DRM_WEDGE_RECOVERY_NONE ,NULL ); - } +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { +if (queue->queue_type == queue_type) { +for (i =0 ; i < hung_db_num; i++) { +if (queue->doorbell_index == db_array[i]) { + queue->state =AMDGPU_USERQ_STATE_HUNG ; +atomic_inc (&adev->gpu_reset_counter); +amdgpu_userq_fence_driver_force_completion (queue); +drm_dev_wedged_event (adev_to_drm (adev),DRM_WEDGE_RECOVERY_NONE ,NULL ); } } }
Signature · 1 line
2.49.0
PATCH v6 2/2 drm/amdgpu: Implement user queue reset functionality
This patch adds robust reset handling for user queues (userq) to improve recovery from queue failures. The key components include:
1. Queue detection and reset logic:
- amdgpu_userq_detect_and_reset_queues() identifies failed queues
- Per-IP detect_and_reset callbacks for targeted recovery
- Falls back to full GPU reset when needed
2. Reset infrastructure:
- Adds userq_reset_work workqueue for async reset handling
- Implements pre/post reset handlers for queue state management
- Integrates with existing GPU reset framework
3. Error handling improvements:
- Enhanced state tracking with HUNG state
- Automatic reset triggering on critical failures
- VRAM loss handling during recovery
4. Integration points:
- Added to device init/reset paths
- Called during queue destroy, suspend, and isolation events
- Handles both individual queue and full GPU resets
The reset functionality works with both gfx/compute and sdma queues, providing better resilience against queue failures while minimizing disruption to unaffected queues.
v2: add detection and reset calls when preemption/unmaped fails.
add a per device userq counter for each user queue type.(Alex)
v3: make sure we hold the adev->userq_mutex when we call amdgpu_userq_detect_and_reset_queues. (Alex)
warn if the adev->userq_mutex is not held.
v4: make sure we have all of the uqm->userq_mutex held.
warn if the uqm->userq_mutex is not held.
v5: Use array for user queue type counters.(Alex)
all of the uqm->userq_mutex need to be held when calling detect and reset. (Alex)
v6: fix lock dep warning in amdgpu_userq_fence_dence_driver_process
--- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 8 + drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 186 ++++++++++++++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 5 + .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 13 +- 6 files changed, 198 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +1 -0
@@ -1315,6 +1315,7 @@ struct amdgpu_device {bool apu_prefer_gtt;bool userq_halt_for_enforce_isolation; +struct work_struct userq_reset_work;struct amdgpu_uid *uid_info;/* KFD
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +8 -0
@@ -4533,6 +4533,7 @@ int amdgpu_device_init(struct amdgpu_device *adev, }INIT_WORK (&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func); +INIT_WORK (&adev->userq_reset_work, amdgpu_userq_reset_work); adev->gfx.gfx_off_req_count =1 ; adev->gfx.gfx_off_residency =0 ;
@@ -5954,6 +5955,10 @@ int amdgpu_device_reinit_after_reset(struct amdgpu_reset_context *reset_context)if (r) goto out; + r =amdgpu_userq_post_reset (tmp_adev, vram_lost); +if (r) + goto out; +drm_client_dev_resume (adev_to_drm (tmp_adev), false);/*
@@ -6176,6 +6181,7 @@ static inline void amdgpu_device_stop_pending_resets(struct amdgpu_device *adev)if (!amdgpu_sriov_vf (adev))cancel_work (&adev->reset_work);#endif +cancel_work (&adev->userq_reset_work);if (adev->kfd.dev)cancel_work (&adev->kfd.reset_work);
@@ -6296,6 +6302,8 @@ static void amdgpu_device_halt_activities(struct amdgpu_device *adev, amdgpu_device_ip_need_full_reset(tmp_adev))amdgpu_ras_suspend (tmp_adev); +amdgpu_userq_pre_reset (tmp_adev); +for (i =0 ; i <AMDGPU_MAX_RINGS ; ++i) {struct amdgpu_ring *ring = tmp_adev->rings[i];
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +1 -0
@@ -83,6 +83,7 @@ enum amdgpu_ring_type {AMDGPU_RING_TYPE_MES ,AMDGPU_RING_TYPE_UMSCH_MM ,AMDGPU_RING_TYPE_CPER , + AMDGPU_RING_TYPE_MAX, }; enum amdgpu_ib_pool_type {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +174 -12
319 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +5 -0
@@ -98,6 +98,7 @@ struct amdgpu_userq_mgr {struct amdgpu_device *adev;struct delayed_work resume_work;struct drm_file *file; +atomic_t userq_count[AMDGPU_RING_TYPE_MAX ]; };struct amdgpu_db_info {
@@ -141,6 +142,10 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,int amdgpu_userq_start_sched_for_enforce_isolation (struct amdgpu_device *adev,u32 idx); +void amdgpu_userq_reset_work (struct work_struct *work); +void amdgpu_userq_pre_reset (struct amdgpu_device *adev); +int amdgpu_userq_post_reset (struct amdgpu_device *adev,bool vram_lost); +int amdgpu_userq_input_va_validate (struct amdgpu_vm *vm,u64 addr,u64 expected_size);#endif
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +9 -4
@@ -151,15 +151,20 @@ void amdgpu_userq_fence_driver_process(struct amdgpu_userq_fence_driver *fence_d {struct amdgpu_userq_fence *userq_fence, *tmp;struct dma_fence *fence; +unsigned long flags;u64 rptr;int i;if (!fence_drv)return ; - +/* +* Use interrupt-safe spinlock since this function can be called from: +* 1. Interrupt context (IRQ handlers like gfx_v11_0_eop_irq) +* 2. Process context (workqueues like eviction suspend worker) +* Using regular spin_lock() in interrupt context causes lockdep warnings. +*/ +spin_lock_irqsave (&fence_drv->fence_list_lock, flags); rptr =amdgpu_userq_fence_read (fence_drv); - -spin_lock (&fence_drv->fence_list_lock);list_for_each_entry_safe (userq_fence, tmp, &fence_drv->fences, link) { fence = &userq_fence->base;
@@ -174,7 +179,7 @@ void amdgpu_userq_fence_driver_process(struct amdgpu_userq_fence_driver *fence_dlist_del (&userq_fence->link);dma_fence_put (fence); } -spin_unlock (&fence_drv->fence_list_lock); +spin_unlock_irqrestore (&fence_drv->fence_list_lock, flags); }void amdgpu_userq_fence_driver_destroy (struct kref *ref)
Signature · 1 line
2.49.0
Christian König
maintainerQuoted 133 lines · Jesse.Zhang
This commit refactors the AMDGPU userqueue management subsystem to replace IDR (ID Allocation) with XArray for improved performance, scalability, and maintainability. The changes address several issues with the previous IDR implementation and provide better locking semantics.
Key changes:
1. **Global XArray Introduction**:
- Addeduserq_doorbell_xatostruct amdgpu_devicefor global queue tracking
- Usesdoorbell_indexas key for efficient global lookup
- Replaces the previoususerq_mgr_listlinked list approach2. **Per-process XArray Conversion**:
- Replaceduserq_idrwithuserq_mgr_xainstruct amdgpu_userq_mgr
- Maintains per-process queue tracking withqueue_idas key
- UsesXA_FLAGS_ALLOCfor automatic ID allocation3. **Locking Improvements**:
- Removed globaluserq_mutexfromstruct amdgpu_device
- Replaced with fine-grained XArray locking using XArray's internal spinlocks4. **Runtime Idle Check Optimization**:
- Updatedamdgpu_runtime_idle_check_userq()to usexa_empty5. **Queue Management Functions**:
- Converted all IDR operations to equivalent XArray functions:
-idr_alloc()→xa_alloc()
-idr_find()→xa_load()
-idr_remove()→xa_erase()
-idr_for_each()→xa_for_each()Benefits:
- **Performance**: XArray provides better scalability for large numbers of queues
- **Memory Efficiency**: Reduced memory overhead compared to IDR
- **Thread Safety**: Improved locking semantics with XArray's internal spinlocksv2: rename userq_global_xa/userq_xa to userq_doorbell_xa/userq_mgr_xa
Removexa_lockand use its own lock.v3: Set
queue->userq_mgr=uq_mgrinamdgpu_userq_create()Suggested-by Christian König <[email protected]>Signed-off-by Jesse Zhang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 8 +- drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 3 +- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 16 +- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 143 +++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 8 +- .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 4 +- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 22 ++- 7 files changed, 97 insertions(+), 107 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu.h +6
-2@@ -1174,6 +1174,12 @@ struct amdgpu_device {* queue fence. */ struct xarray userq_xa; +/** +* @userq_doorbell_xa: Global user queue map (doorbell index → queue) +* Key: doorbell_index (unique global identifier for the queue) +* Value: struct amdgpu_usermode_queue +*/ +struct xarray userq_doorbell_xa;/* df */ struct amdgpu_df df;@@ -1308,8 +1314,6 @@ struct amdgpu_device {*/ bool apu_prefer_gtt; -struct list_head userq_mgr_list; -struct mutex userq_mutex;bool userq_halt_for_enforce_isolation;struct amdgpu_uid *uid_info;drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1
-2@@ -4483,7 +4483,6 @@ int amdgpu_device_init(struct amdgpu_device *adev,mutex_init (&adev->gfx.userq_sch_mutex);mutex_init (&adev->gfx.workload_profile_mutex);mutex_init (&adev->vcn.workload_profile_mutex); -mutex_init (&adev->userq_mutex);amdgpu_device_init_apu_flags (adev);@@ -4511,7 +4510,7 @@ int amdgpu_device_init(struct amdgpu_device *adev,INIT_LIST_HEAD (&adev->pm.od_kobj_list); -INIT_LIST_HEAD (&adev->userq_mgr_list); +xa_init (&adev->userq_doorbell_xa);INIT_DELAYED_WORK (&adev->delayed_init_work, amdgpu_device_delayed_init_work_handler);drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1
-15@@ -2771,22 +2771,8 @@ static int amdgpu_runtime_idle_check_userq(struct device *dev)struct pci_dev *pdev =to_pci_dev (dev);struct drm_device *drm_dev =pci_get_drvdata (pdev);struct amdgpu_device *adev =drm_to_adev (drm_dev); -struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp; -int queue_id; -int ret =0 ; - -mutex_lock (&adev->userq_mutex); -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { - ret = -EBUSY ; - goto done; - } - } -done: -mutex_unlock (&adev->userq_mutex); -return ret; +return xa_empty (&adev->userq_doorbell_xa) ?0 : -EBUSY ; }static int amdgpu_pmops_runtime_suspend (struct device *dev)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr, · line 188
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-1@@ -180,17 +180,28 @@ amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr, {struct amdgpu_device *adev = uq_mgr->adev;const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type]; +unsigned long flags; uq_funcs->mqd_destroy (uq_mgr, queue);amdgpu_userq_fence_driver_free (queue); -idr_remove (&uq_mgr->userq_idr, queue_id); + +/* Use interrupt-safe locking since IRQ handlers may access these XArrays */
Wait a second where are IRQ handlers accessing those XAs?
Quoted 42 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +13
-3+xa_lock_irqsave (&uq_mgr->userq_mgr_xa, flags); +__xa_erase (&uq_mgr->userq_mgr_xa, (unsigned long )queue_id); +xa_unlock_irqrestore (&uq_mgr->userq_mgr_xa, flags); + +xa_lock_irqsave (&adev->userq_doorbell_xa, flags); +__xa_erase (&adev->userq_doorbell_xa, queue->doorbell_index); +xa_unlock_irqrestore (&adev->userq_doorbell_xa, flags); + + queue->userq_mgr =NULL ;kfree (queue); }static struct amdgpu_usermode_queue *amdgpu_userq_find (struct amdgpu_userq_mgr *uq_mgr,int qid) { -return idr_find (&uq_mgr->userq_idr, qid); +return xa_load (&uq_mgr->userq_mgr_xa, qid); }void @@ -462,8 +473,10 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)struct amdgpu_db_info db_info;char *queue_name;bool skip_map_queue; +unsigned long flags; +u32 qid;uint64_t index; -int qid, r =0 ; +int r =0 ;int priority = (args->in.flags &AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK ) >>AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT ;@@ -486,7 +499,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)* * This will also make sure we have a valid eviction fence ready to be used. */ -mutex_lock (&adev->userq_mutex);amdgpu_userq_ensure_ev_fence (&fpriv->userq_mgr, &fpriv->evf_mgr); uq_funcs = adev->userq_funcs[args->in.ip_type];@@ -546,9 +558,17 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) goto unlock;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 568
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8
-0} +xa_lock_irqsave (&adev->userq_doorbell_xa, flags); + r =xa_err (__xa_store (&adev->userq_doorbell_xa, index, queue,GFP_KERNEL )); +if (r) { +kfree (queue); +xa_unlock_irqrestore (&adev->userq_doorbell_xa, flags); + goto unlock; + } +xa_unlock_irqrestore (&adev->userq_doorbell_xa, flags);
You can use xa_store_irq() here.
Quoted 130 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +16
-30- qid =idr_alloc (&uq_mgr->userq_idr, queue,1 ,AMDGPU_MAX_USERQ_COUNT ,GFP_KERNEL ); -if (qid <0 ) { + r =xa_alloc (&uq_mgr->userq_mgr_xa, &qid, queue,XA_LIMIT (1 ,AMDGPU_MAX_USERQ_COUNT ),GFP_KERNEL ); +if (r) {drm_file_err (uq_mgr->file,"Failed to allocate a queue id\n" );amdgpu_userq_fence_driver_free (queue); uq_funcs->mqd_destroy (uq_mgr, queue);@@ -556,6 +576,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r = -ENOMEM ; goto unlock; } + queue->userq_mgr = uq_mgr;/* don't map the queue if scheduling is halted */ if (adev->userq_halt_for_enforce_isolation &&@@ -568,7 +589,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r =amdgpu_userq_map_helper (uq_mgr, queue);if (r) {drm_file_err (uq_mgr->file,"Failed to map Queue\n" ); -idr_remove (&uq_mgr->userq_idr, qid); +xa_erase (&uq_mgr->userq_mgr_xa, qid);amdgpu_userq_fence_driver_free (queue); uq_funcs->mqd_destroy (uq_mgr, queue);kfree (queue);@@ -591,7 +612,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) unlock:mutex_unlock (&uq_mgr->userq_mutex); -mutex_unlock (&adev->userq_mutex);return r; }@@ -689,11 +709,11 @@ static int amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr) {struct amdgpu_usermode_queue *queue; -int queue_id; +unsigned long queue_id;int ret =0 , r;/* Resume all the queues for this process */ -idr_for_each_entry (&uq_mgr->userq_idr, queue, queue_id) { +xa_for_each (&uq_mgr->userq_mgr_xa, queue_id, queue) { r =amdgpu_userq_restore_helper (uq_mgr, queue);if (r) ret = r;@@ -846,11 +866,11 @@ static int amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr) {struct amdgpu_usermode_queue *queue; -int queue_id; +unsigned long queue_id;int ret =0 , r;/* Try to unmap all the queues in this process ctx */ -idr_for_each_entry (&uq_mgr->userq_idr, queue, queue_id) { +xa_for_each (&uq_mgr->userq_mgr_xa, queue_id, queue) { r =amdgpu_userq_preempt_helper (uq_mgr, queue);if (r) ret = r;@@ -865,9 +885,10 @@ static int amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr *uq_mgr) {struct amdgpu_usermode_queue *queue; -int queue_id, ret; +unsigned long queue_id; +int ret; -idr_for_each_entry (&uq_mgr->userq_idr, queue, queue_id) { +xa_for_each (&uq_mgr->userq_mgr_xa, queue_id, queue) {struct dma_fence *f = queue->last_fence;if (!f ||dma_fence_is_signaled (f))@@ -920,44 +941,30 @@ int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f struct amdgpu_device *adev) {mutex_init (&userq_mgr->userq_mutex); -idr_init_base (&userq_mgr->userq_idr,1 ); +xa_init_flags (&userq_mgr->userq_mgr_xa,XA_FLAGS_ALLOC ); userq_mgr->adev = adev; userq_mgr->file = file_priv; -mutex_lock (&adev->userq_mutex); -list_add (&userq_mgr->list, &adev->userq_mgr_list); -mutex_unlock (&adev->userq_mutex); -INIT_DELAYED_WORK (&userq_mgr->resume_work, amdgpu_userq_restore_worker);return 0 ; }void amdgpu_userq_mgr_fini (struct amdgpu_userq_mgr *userq_mgr) { -struct amdgpu_device *adev = userq_mgr->adev;struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp; -uint32_t queue_id; +unsigned long queue_id;cancel_delayed_work_sync (&userq_mgr->resume_work); -mutex_lock (&adev->userq_mutex);mutex_lock (&userq_mgr->userq_mutex); -idr_for_each_entry (&userq_mgr->userq_idr, queue, queue_id) { +xa_for_each (&userq_mgr->userq_mgr_xa, queue_id, queue) {amdgpu_userq_wait_for_last_fence (userq_mgr, queue);amdgpu_userq_unmap_helper (userq_mgr, queue);amdgpu_userq_cleanup (userq_mgr, queue, queue_id); } -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { -if (uqm == userq_mgr) { -list_del (&uqm->list); -break ; - } - } -idr_destroy (&userq_mgr->userq_idr); +xa_destroy (&userq_mgr->userq_mgr_xa);mutex_unlock (&userq_mgr->userq_mutex); -mutex_unlock (&adev->userq_mutex);mutex_destroy (&userq_mgr->userq_mutex); }@@ -965,25 +972,23 @@ 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, *tmp; -int queue_id; +struct amdgpu_userq_mgr *uqm;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_suspend(struct amdgpu_device *adev) · line 983
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-2+unsigned long queue_id;int ret =0 , r;if (!ip_mask)return 0 ; -mutex_lock (&adev->userq_mutex); -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + uqm = queue->userq_mgr;
That function is used during reset, isn't it?
If yes we somehow need to make sure that neither queue nor userq_mgr is properly initialized and not freed up while the reset is ongoing.
The easiest way of doing that is to hold the read side of the reset lock while creating/destroying queues and the manager data structure.
Quoted 24 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-8cancel_delayed_work_sync (&uqm->resume_work);mutex_lock (&uqm->userq_mutex); -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { - r =amdgpu_userq_unmap_helper (uqm, queue); -if (r) - ret = r; - } + r =amdgpu_userq_unmap_helper (uqm, queue); +if (r) + ret = r;mutex_unlock (&uqm->userq_mutex); } -mutex_unlock (&adev->userq_mutex); +return ret; }@@ -991,24 +996,22 @@ 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, *tmp; -int queue_id; +struct amdgpu_userq_mgr *uqm;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_resume(struct amdgpu_device *adev) · line 1007
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-2+unsigned long queue_id;int ret =0 , r;if (!ip_mask)return 0 ; -mutex_lock (&adev->userq_mutex); -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + uqm = queue->userq_mgr;
Not sure about those ones here, but the same point that applies to the reset handling applies here as well.
Apart from that the patch looks good to me.
Regards,
Christian.
Quoted 183 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +20
-26mutex_lock (&uqm->userq_mutex); -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { - r =amdgpu_userq_map_helper (uqm, queue); -if (r) - ret = r; - } + r =amdgpu_userq_map_helper (uqm, queue); +if (r) + ret = r;mutex_unlock (&uqm->userq_mutex); } -mutex_unlock (&adev->userq_mutex); +return ret; }@@ -1017,33 +1020,31 @@ 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, *tmp; -int queue_id; +struct amdgpu_userq_mgr *uqm; +unsigned long queue_id;int ret =0 , r;/* only need to stop gfx/compute */ if (!(ip_mask & ((1 <<AMDGPU_HW_IP_GFX ) | (1 <<AMDGPU_HW_IP_COMPUTE ))))return 0 ; -mutex_lock (&adev->userq_mutex);if (adev->userq_halt_for_enforce_isolation)dev_warn (adev->dev,"userq scheduling already stopped!\n" ); adev->userq_halt_for_enforce_isolation = true; -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + uqm = queue->userq_mgr;cancel_delayed_work_sync (&uqm->resume_work);mutex_lock (&uqm->userq_mutex); -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { -if (((queue->queue_type ==AMDGPU_HW_IP_GFX ) || - (queue->queue_type ==AMDGPU_HW_IP_COMPUTE )) && - (queue->xcp_id == idx)) { - r =amdgpu_userq_preempt_helper (uqm, queue); -if (r) - ret = r; - } +if (((queue->queue_type ==AMDGPU_HW_IP_GFX ) || + (queue->queue_type ==AMDGPU_HW_IP_COMPUTE )) && + (queue->xcp_id == idx)) { + r =amdgpu_userq_preempt_helper (uqm, queue); +if (r) + ret = r; }mutex_unlock (&uqm->userq_mutex); } -mutex_unlock (&adev->userq_mutex); +return ret; }@@ -1052,21 +1053,20 @@ 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, *tmp; -int queue_id; +struct amdgpu_userq_mgr *uqm; +unsigned long queue_id;int ret =0 , r;/* only need to stop gfx/compute */ if (!(ip_mask & ((1 <<AMDGPU_HW_IP_GFX ) | (1 <<AMDGPU_HW_IP_COMPUTE ))))return 0 ; -mutex_lock (&adev->userq_mutex);if (!adev->userq_halt_for_enforce_isolation)dev_warn (adev->dev,"userq scheduling already started!\n" ); adev->userq_halt_for_enforce_isolation = false; -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + uqm = queue->userq_mgr;mutex_lock (&uqm->userq_mutex); -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) {if (((queue->queue_type ==AMDGPU_HW_IP_GFX ) || (queue->queue_type ==AMDGPU_HW_IP_COMPUTE )) && (queue->xcp_id == idx)) {@@ -1074,9 +1074,8 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,if (r) ret = r; } - }mutex_unlock (&uqm->userq_mutex); } -mutex_unlock (&adev->userq_mutex); +return ret; }drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6
-2@@ -88,11 +88,15 @@ struct amdgpu_userq_funcs {/* Usermode queues for gfx */ struct amdgpu_userq_mgr { -struct idr userq_idr; +/** +* @userq_mgr_xa: Per-process user queue map (queue ID → queue) +* Key: queue_id (unique ID within the process's userq manager) +* Value: struct amdgpu_usermode_queue +*/ +struct xarray userq_mgr_xa;struct mutex userq_mutex;struct amdgpu_device *adev;struct delayed_work resume_work; -struct list_head list;struct drm_file *file; };drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2
-2@@ -537,7 +537,7 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data, }/* Retrieve the user queue */ - queue =idr_find (&userq_mgr->userq_idr, args->queue_id); + queue =xa_load (&userq_mgr->userq_mgr_xa, args->queue_id);if (!queue) { r = -ENOENT ; goto put_gobj_write;@@ -899,7 +899,7 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev, void *data,*/ num_fences =dma_fence_dedup_array (fences, num_fences); - waitq =idr_find (&userq_mgr->userq_idr, wait_info->waitq_id); + waitq =xa_load (&userq_mgr->userq_mgr_xa, wait_info->waitq_id);if (!waitq) { r = -EINVAL ; goto free_fences;drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +10
-12@@ -205,9 +205,9 @@ static int mes_userq_detect_and_reset(struct amdgpu_device *adev,int db_array_size =amdgpu_mes_get_hung_queue_db_array_size (adev);struct mes_detect_and_reset_queue_input input;struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp;unsigned int hung_db_num =0 ; -int queue_id, r, i; +unsigned long queue_id; +int r, i;u32 db_array[4 ];if (db_array_size >4 ) {@@ -227,16 +227,14 @@ static int mes_userq_detect_and_reset(struct amdgpu_device *adev,if (r) {dev_err (adev->dev,"Failed to detect and reset queues, err (%d)\n" , r); }else if (hung_db_num) { -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { -if (queue->queue_type == queue_type) { -for (i =0 ; i < hung_db_num; i++) { -if (queue->doorbell_index == db_array[i]) { - queue->state =AMDGPU_USERQ_STATE_HUNG ; -atomic_inc (&adev->gpu_reset_counter); -amdgpu_userq_fence_driver_force_completion (queue); -drm_dev_wedged_event (adev_to_drm (adev),DRM_WEDGE_RECOVERY_NONE ,NULL ); - } +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { +if (queue->queue_type == queue_type) { +for (i =0 ; i < hung_db_num; i++) { +if (queue->doorbell_index == db_array[i]) { + queue->state =AMDGPU_USERQ_STATE_HUNG ; +atomic_inc (&adev->gpu_reset_counter); +amdgpu_userq_fence_driver_force_completion (queue); +drm_dev_wedged_event (adev_to_drm (adev),DRM_WEDGE_RECOVERY_NONE ,NULL ); } } }
[AMD Official Use Only - AMD Internal Distribution Only]
Quoted 158 lines
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, September 24, 2025 5:29 PM
To: Zhang, Jesse(Jie) <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [v6 1/2] drm/amdgpu: Convert amdgpu userqueue management from IDR to XArrayQuoted 9 lines · Jesse.Zhang
This commit refactors the AMDGPU userqueue management subsystem to replace IDR (ID Allocation) with XArray for improved performance, scalability, and maintainability. The changes address several issues with the previous IDR implementation and provide better locking semantics.
Key changes:
1. **Global XArray Introduction**:
- Addeduserq_doorbell_xatostruct amdgpu_devicefor global queuetracking
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static int amdgpu_runtime_idle_check_userq(struct
Quoted 103 lines
- Uses
doorbell_indexas key for efficient global lookup
- Replaces the previoususerq_mgr_listlinked list approach2. **Per-process XArray Conversion**:
- Replaceduserq_idrwithuserq_mgr_xainstruct amdgpu_userq_mgr
- Maintains per-process queue tracking withqueue_idas key
- UsesXA_FLAGS_ALLOCfor automatic ID allocation3. **Locking Improvements**:
- Removed globaluserq_mutexfromstruct amdgpu_device
- Replaced with fine-grained XArray locking using XArray's internal
spinlocks4. **Runtime Idle Check Optimization**:
- Updatedamdgpu_runtime_idle_check_userq()to usexa_empty5. **Queue Management Functions**:
- Converted all IDR operations to equivalent XArray functions:
-idr_alloc()→xa_alloc()
-idr_find()→xa_load()
-idr_remove()→xa_erase()
-idr_for_each()→xa_for_each()Benefits:
- **Performance**: XArray provides better scalability for large numbers of queues
- **Memory Efficiency**: Reduced memory overhead compared to IDR
- **Thread Safety**: Improved locking semantics with XArray's internal spinlocksv2: rename userq_global_xa/userq_xa to userq_doorbell_xa/userq_mgr_xa
Removexa_lockand use its own lock.v3: Set
queue->userq_mgr=uq_mgrinamdgpu_userq_create()Suggested-by Christian König <[email protected]>Signed-off-by Jesse Zhang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 8 +- drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 3 +- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 16 +- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 143 +++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 8 +- .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 4 +- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 22 ++- 7 files changed, 97 insertions(+), 107 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu.h +6
-2@@ -1174,6 +1174,12 @@ struct amdgpu_device {* queue fence. */ struct xarray userq_xa; +/** +* @userq_doorbell_xa: Global user queue map (doorbell index → queue) +* Key: doorbell_index (unique global identifier for the queue) +* Value: struct amdgpu_usermode_queue +*/ +struct xarray userq_doorbell_xa;/* df */ struct amdgpu_df df;@@ -1308,8 +1314,6 @@ struct amdgpu_device {*/ bool apu_prefer_gtt; -struct list_head userq_mgr_list; -struct mutex userq_mutex;bool userq_halt_for_enforce_isolation;struct amdgpu_uid *uid_info;drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1
-2@@ -4483,7 +4483,6 @@ int amdgpu_device_init(struct amdgpu_device *adev,mutex_init (&adev->gfx.userq_sch_mutex);mutex_init (&adev->gfx.workload_profile_mutex);mutex_init (&adev->vcn.workload_profile_mutex); -mutex_init (&adev->userq_mutex);amdgpu_device_init_apu_flags (adev);@@ -4511,7 +4510,7 @@ int amdgpu_device_init(struct amdgpu_device *adev,INIT_LIST_HEAD (&adev->pm.od_kobj_list); -INIT_LIST_HEAD (&adev->userq_mgr_list); +xa_init (&adev->userq_doorbell_xa); INIT_DELAYED_WORK(&adev->delayed_init_work,amdgpu_device_delayed_init_work_handler);drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +0
-0@@ -2771,22 +2771,8 @@ static int amdgpu_runtime_idle_check_userq(structdevice *dev)
Quoted 35 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1
-16struct pci_dev *pdev =to_pci_dev (dev);struct drm_device *drm_dev =pci_get_drvdata (pdev);struct amdgpu_device *adev =drm_to_adev (drm_dev); -struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp; -int queue_id; -int ret =0 ; - -mutex_lock (&adev->userq_mutex); -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { - ret = -EBUSY ; - goto done; - } - } -done: -mutex_unlock (&adev->userq_mutex); -return ret; +return xa_empty (&adev->userq_doorbell_xa) ?0 : -EBUSY ; } static int amdgpu_pmops_runtime_suspend(struct device *dev) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
indexb649ac0cedff..0712b88782c8100644drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0@@ -180,17 +180,28 @@ amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr, {struct amdgpu_device *adev = uq_mgr->adev;const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type]; +unsigned long flags;
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr,
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-1uq_funcs->mqd_destroy (uq_mgr, queue);amdgpu_userq_fence_driver_free (queue); -idr_remove (&uq_mgr->userq_idr, queue_id); + +/* Use interrupt-safe locking since IRQ handlers may access these +XArrays */ Wait a second where are IRQ handlers accessing those XAs?
[Zhang, Jesse(Jie)] gfx_v11_0_eop_irq on gfx11 also accesses these XAs. With lockdep enabled, a warning will appear without xa_lock_irqsave. We can replace it with xa_erase_irq, just like you suggested with xa_store_irq.
Quoted 197 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union
Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-1+xa_lock_irqsave (&uq_mgr->userq_mgr_xa, flags); +__xa_erase (&uq_mgr->userq_mgr_xa, (unsigned long )queue_id); +xa_unlock_irqrestore (&uq_mgr->userq_mgr_xa, flags); + +xa_lock_irqsave (&adev->userq_doorbell_xa, flags); +__xa_erase (&adev->userq_doorbell_xa, queue->doorbell_index); +xa_unlock_irqrestore (&adev->userq_doorbell_xa, flags); + + queue->userq_mgr =NULL ;kfree (queue); }static struct amdgpu_usermode_queue *amdgpu_userq_find (struct amdgpu_userq_mgr *uq_mgr,int qid) { -return idr_find (&uq_mgr->userq_idr, qid); +return xa_load (&uq_mgr->userq_mgr_xa, qid); }void @@ -462,8 +473,10 @@ amdgpu_userq_create(struct drm_file *filp, union
drm_amdgpu_userq*args)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-1struct amdgpu_db_info db_info;char *queue_name;bool skip_map_queue; +unsigned long flags; +u32 qid;uint64_t index; -int qid, r =0 ; +int r =0 ; int priority = (args->in.flags &
AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) >>Quoted 1 line
AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0@@ -486,7 +499,6 @@ amdgpu_userq_create(struct drm_file *filp, union
drm_amdgpu_userq*args)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-1* * This will also make sure we have a valid eviction fence ready to be used. */ -mutex_lock (&adev->userq_mutex);amdgpu_userq_ensure_ev_fence (&fpriv->userq_mgr, &fpriv->evf_mgr); uq_funcs = adev->userq_funcs[args->in.ip_type];@@ -546,9 +558,17 @@ amdgpu_userq_create(struct drm_file *filp, union
drm_amdgpu_userq*args)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0goto unlock; } +xa_lock_irqsave (&adev->userq_doorbell_xa, flags); + r =xa_err(__xa_store(&adev->userq_doorbell_xa, index, queue,
GFP_KERNEL));drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 568
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6
-0+if (r) { +kfree (queue); +xa_unlock_irqrestore (&adev->userq_doorbell_xa, flags); + goto unlock; + } +xa_unlock_irqrestore (&adev->userq_doorbell_xa, flags);You can use
xa_store_irq()here.drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union · line 549
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-1- qid = idr_alloc(&uq_mgr->userq_idr, queue, 1,
AMDGPU_MAX_USERQ_COUNT,GFP_KERNEL);drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union · line 569
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-1-if (qid <0 ) { + r = xa_alloc(&uq_mgr->userq_mgr_xa, &qid, queue, XA_LIMIT(1,
AMDGPU_MAX_USERQ_COUNT),GFP_KERNEL);Quoted 72 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+if (r) {drm_file_err (uq_mgr->file,"Failed to allocate a queue id\n" );amdgpu_userq_fence_driver_free (queue); uq_funcs->mqd_destroy(uq_mgr, queue); @@ -556,6 +576,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)r = -ENOMEM; goto unlock; }+
queue->userq_mgr=uq_mgr;/* don't map the queue if scheduling is halted */ if (adev->userq_halt_for_enforce_isolation && @@ -568,7 +589,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r = amdgpu_userq_map_helper(uq_mgr, queue); if (r) { drm_file_err(uq_mgr->file, "Failed to map Queue\n");drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-2-idr_remove (&uq_mgr->userq_idr, qid); +xa_erase (&uq_mgr->userq_mgr_xa, qid);amdgpu_userq_fence_driver_free (queue); uq_funcs->mqd_destroy (uq_mgr, queue);kfree (queue);@@ -591,7 +612,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) unlock: mutex_unlock(&uq_mgr->userq_mutex); -mutex_unlock (&adev->userq_mutex);return r;}
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7
-6@@ -689,11 +709,11 @@ static int amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr) {struct amdgpu_usermode_queue *queue; -int queue_id; +unsigned long queue_id;int ret =0 , r;/* Resume all the queues for this process */ -idr_for_each_entry (&uq_mgr->userq_idr, queue, queue_id) { +xa_for_each (&uq_mgr->userq_mgr_xa, queue_id, queue) { r =amdgpu_userq_restore_helper (uq_mgr, queue);if (r) ret = r;@@ -846,11 +866,11 @@ static int amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr) {struct amdgpu_usermode_queue *queue; -int queue_id; +unsigned long queue_id;int ret =0 , r;/* Try to unmap all the queues in this process ctx */ -idr_for_each_entry (&uq_mgr->userq_idr, queue, queue_id) { +xa_for_each (&uq_mgr->userq_mgr_xa, queue_id, queue) { r =amdgpu_userq_preempt_helper (uq_mgr, queue);if (r) ret = r;@@ -865,9 +885,10 @@ static int amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr *uq_mgr) {struct amdgpu_usermode_queue *queue; -int queue_id, ret; +unsigned long queue_id; +int ret; -idr_for_each_entry (&uq_mgr->userq_idr, queue, queue_id) { +xa_for_each (&uq_mgr->userq_mgr_xa, queue_id, queue) {struct dma_fence *f = queue->last_fence; if (!f || dma_fence_is_signaled(f)) @@ -920,44 +941,30 @@ intamdgpu_userq_mgr_init(struct
amdgpu_userq_mgr*userq_mgr, structdrm_file*f
structamdgpu_device*adev){ mutex_init(&userq_mgr->userq_mutex);drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-5-idr_init_base (&userq_mgr->userq_idr,1 ); +xa_init_flags (&userq_mgr->userq_mgr_xa,XA_FLAGS_ALLOC ); userq_mgr->adev = adev; userq_mgr->file = file_priv; -mutex_lock (&adev->userq_mutex); -list_add (&userq_mgr->list, &adev->userq_mgr_list); -mutex_unlock (&adev->userq_mutex); - INIT_DELAYED_WORK(&userq_mgr->resume_work,
amdgpu_userq_restore_worker);Quoted 47 lines
return 0;}
void
amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr){drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-15-struct amdgpu_device *adev = userq_mgr->adev;struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp; -uint32_t queue_id; +unsigned long queue_id;cancel_delayed_work_sync (&userq_mgr->resume_work); -mutex_lock (&adev->userq_mutex);mutex_lock (&userq_mgr->userq_mutex); -idr_for_each_entry (&userq_mgr->userq_idr, queue, queue_id) { +xa_for_each (&userq_mgr->userq_mgr_xa, queue_id, queue) {amdgpu_userq_wait_for_last_fence (userq_mgr, queue);amdgpu_userq_unmap_helper (userq_mgr, queue);amdgpu_userq_cleanup (userq_mgr, queue, queue_id); } -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { -if (uqm == userq_mgr) { -list_del (&uqm->list); -break ; - } - } -idr_destroy (&userq_mgr->userq_idr); +xa_destroy (&userq_mgr->userq_mgr_xa);mutex_unlock (&userq_mgr->userq_mutex); -mutex_unlock (&adev->userq_mutex);mutex_destroy (&userq_mgr->userq_mutex); }@@ -965,25 +972,23 @@ 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, *tmp; -int queue_id; +struct amdgpu_userq_mgr *uqm; +unsigned long queue_id;int ret =0 , r;if (!ip_mask)return 0 ;
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_suspend(struct amdgpu_device *adev) · line 983
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-2-mutex_lock (&adev->userq_mutex); -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + uqm = queue->userq_mgr;That function is used during reset, isn't it?
[Zhang, Jesse(Jie)] No, amdgpu_userq_suspend/amdgpu_userq_resume are not used during reset.
but They are called when executing the suspend command. I tried the suspend case, amdgpu_userq_suspend/amdgpu_userq_resume and no error occurred.
Quoted 257 lines
If yes we somehow need to make sure that neither queue nor
userq_mgris properly initialized and not freed up while the reset is ongoing.The easiest way of doing that is to hold the read side of the reset lock while creating/destroying queues and the manager data structure.
Quoted 24 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-8cancel_delayed_work_sync (&uqm->resume_work);mutex_lock (&uqm->userq_mutex); -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { - r =amdgpu_userq_unmap_helper (uqm, queue); -if (r) - ret = r; - } + r =amdgpu_userq_unmap_helper (uqm, queue); +if (r) + ret = r;mutex_unlock (&uqm->userq_mutex); } -mutex_unlock (&adev->userq_mutex); +return ret; }@@ -991,24 +996,22 @@ 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, *tmp; -int queue_id; +struct amdgpu_userq_mgr *uqm;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_resume(struct amdgpu_device · line 1007
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-2+unsigned long queue_id;int ret =0 , r;if (!ip_mask)return 0 ; -mutex_lock (&adev->userq_mutex); -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + uqm = queue->userq_mgr;Not sure about those ones here, but the same point that applies to the reset handling applies here as well.
Apart from that the patch looks good to me.
Regards,
Christian.Quoted 18 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-6mutex_lock (&uqm->userq_mutex); -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { - r =amdgpu_userq_map_helper (uqm, queue); -if (r) - ret = r; - } + r =amdgpu_userq_map_helper (uqm, queue); +if (r) + ret = r;mutex_unlock (&uqm->userq_mutex); } -mutex_unlock (&adev->userq_mutex); +return ret; }@@ -1017,33 +1020,31 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev, {drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int · line 1028
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-2u32 ip_mask =amdgpu_userq_get_supported_ip_mask (adev);struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp; -int queue_id; +struct amdgpu_userq_mgr *uqm; +unsigned long queue_id;int ret =0 , r;/* only need to stop gfx/compute */ if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 <<
AMDGPU_HW_IP_COMPUTE))))Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-1return 0 ; -mutex_lock (&adev->userq_mutex);if (adev->userq_halt_for_enforce_isolation)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-4dev_warn (adev->dev,"userq scheduling already stopped!\n" ); adev->userq_halt_for_enforce_isolation = true; -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + uqm = queue->userq_mgr;cancel_delayed_work_sync (&uqm->resume_work);mutex_lock (&uqm->userq_mutex); -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { -if (((queue->queue_type ==AMDGPU_HW_IP_GFX ) || - (queue->queue_type == AMDGPU_HW_IP_COMPUTE))&&
Quoted 20 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7
-6- (queue->xcp_id == idx)) { - r =amdgpu_userq_preempt_helper (uqm, queue); -if (r) - ret = r; - } +if (((queue->queue_type ==AMDGPU_HW_IP_GFX ) || + (queue->queue_type ==AMDGPU_HW_IP_COMPUTE )) && + (queue->xcp_id == idx)) { + r =amdgpu_userq_preempt_helper (uqm, queue); +if (r) + ret = r; }mutex_unlock (&uqm->userq_mutex); } -mutex_unlock (&adev->userq_mutex); +return ret; }drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int · line 1053
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0@@ -1052,21 +1053,20 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,{
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-2u32 ip_mask =amdgpu_userq_get_supported_ip_mask (adev);struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp; -int queue_id; +struct amdgpu_userq_mgr *uqm; +unsigned long queue_id;int ret =0 , r;/* only need to stop gfx/compute */ if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 <<
AMDGPU_HW_IP_COMPUTE))))drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-3return 0 ; -mutex_lock (&adev->userq_mutex);if (!adev->userq_halt_for_enforce_isolation)dev_warn (adev->dev,"userq scheduling already started!\n" ); adev->userq_halt_for_enforce_isolation = false; -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + uqm = queue->userq_mgr;mutex_lock (&uqm->userq_mutex); -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) {if (((queue->queue_type ==AMDGPU_HW_IP_GFX ) || (queue->queue_type == AMDGPU_HW_IP_COMPUTE))&&
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0(queue->xcp_id == idx)) {@@ -1074,9 +1074,8 @@ intamdgpu_userq_start_sched_for_enforce_isolation(struct
amdgpu_device*adev,drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_signal_ioctl(struct drm_device *dev,
Quoted 39 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-2if (r) ret = r; } - }mutex_unlock (&uqm->userq_mutex); } -mutex_unlock (&adev->userq_mutex); +return ret; }drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6
-2@@ -88,11 +88,15 @@ struct amdgpu_userq_funcs {/* Usermode queues for gfx */ struct amdgpu_userq_mgr { -struct idr userq_idr; +/** +* @userq_mgr_xa: Per-process user queue map (queue ID → queue) +* Key: queue_id (unique ID within the process's userq manager) +* Value: struct amdgpu_usermode_queue +*/ +struct xarray userq_mgr_xa;struct mutex userq_mutex;struct amdgpu_device *adev;struct delayed_work resume_work; -struct list_head list;struct drm_file *file; };drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0
-0@@ -537,7 +537,7 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev,void *data,
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_wait_ioctl(struct drm_device *dev,
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1
-1}/* Retrieve the user queue */ - queue =idr_find (&userq_mgr->userq_idr, args->queue_id); + queue =xa_load (&userq_mgr->userq_mgr_xa, args->queue_id);if (!queue) { r = -ENOENT ; goto put_gobj_write;@@ -899,7 +899,7 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev,void *data,
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct
Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1
-1*/ num_fences =dma_fence_dedup_array (fences, num_fences); - waitq =idr_find (&userq_mgr->userq_idr, wait_info->waitq_id); + waitq =xa_load (&userq_mgr->userq_mgr_xa, wait_info->waitq_id);if (!waitq) { r = -EINVAL ; goto free_fences;drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0@@ -205,9 +205,9 @@ static int mes_userq_detect_and_reset(struct
amdgpu_device*adev,drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-2int db_array_size =amdgpu_mes_get_hung_queue_db_array_size (adev);struct mes_detect_and_reset_queue_input input;struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp;unsigned int hung_db_num =0 ; -int queue_id, r, i; +unsigned long queue_id; +int r, i;u32 db_array[4 ];if (db_array_size >4 ) {@@ -227,16 +227,14 @@ static int mes_userq_detect_and_reset(struct
amdgpu_device*adev,drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 228
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0if (r) { dev_err(adev->dev, "Failed to detect and reset queues, err (%d)\n",r);
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct amdgpu_device *adev,
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-5}else if (hung_db_num) { -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { -if (queue->queue_type == queue_type) { -for (i =0 ; i < hung_db_num; i++) { - if (queue->doorbell_index ==db_array[i]) {
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 235
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-1- queue->state =
AMDGPU_USERQ_STATE_HUNG;drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 238
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-2-atomic_inc (&adev- gpu_reset_counter); -amdgpu_userq_fence_driver_force_completion(queue);drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 239
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-1-drm_dev_wedged_event(adev_to_drm(adev),
DRM_WEDGE_RECOVERY_NONE, NULL);drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct amdgpu_device *adev,
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +5
-1- } +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { +if (queue->queue_type == queue_type) { +for (i =0 ; i < hung_db_num; i++) { +if (queue->doorbell_index == db_array[i]) { + queue->state =
AMDGPU_USERQ_STATE_HUNG;drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 238
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-0+atomic_inc (&adev- gpu_reset_counter); +amdgpu_userq_fence_driver_force_completion(queue);drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 239
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-0+drm_dev_wedged_event(adev_to_drm(adev),
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-0+DRM_WEDGE_RECOVERY_NONE, NULL); } } }
Christian König
maintainerQuoted 168 lines · Jesse(Jie)
[AMD Official Use Only - AMD Internal Distribution Only]
Quoted 158 lines
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, September 24, 2025 5:29 PM
To: Zhang, Jesse(Jie) <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [v6 1/2] drm/amdgpu: Convert amdgpu userqueue management from IDR to XArrayQuoted 9 lines · Jesse.Zhang
This commit refactors the AMDGPU userqueue management subsystem to replace IDR (ID Allocation) with XArray for improved performance, scalability, and maintainability. The changes address several issues with the previous IDR implementation and provide better locking semantics.
Key changes:
1. **Global XArray Introduction**:
- Addeduserq_doorbell_xatostruct amdgpu_devicefor global queuetracking
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static int amdgpu_runtime_idle_check_userq(struct
Quoted 103 lines
- Uses
doorbell_indexas key for efficient global lookup
- Replaces the previoususerq_mgr_listlinked list approach2. **Per-process XArray Conversion**:
- Replaceduserq_idrwithuserq_mgr_xainstruct amdgpu_userq_mgr
- Maintains per-process queue tracking withqueue_idas key
- UsesXA_FLAGS_ALLOCfor automatic ID allocation3. **Locking Improvements**:
- Removed globaluserq_mutexfromstruct amdgpu_device
- Replaced with fine-grained XArray locking using XArray's internal
spinlocks4. **Runtime Idle Check Optimization**:
- Updatedamdgpu_runtime_idle_check_userq()to usexa_empty5. **Queue Management Functions**:
- Converted all IDR operations to equivalent XArray functions:
-idr_alloc()→xa_alloc()
-idr_find()→xa_load()
-idr_remove()→xa_erase()
-idr_for_each()→xa_for_each()Benefits:
- **Performance**: XArray provides better scalability for large numbers of queues
- **Memory Efficiency**: Reduced memory overhead compared to IDR
- **Thread Safety**: Improved locking semantics with XArray's internal spinlocksv2: rename userq_global_xa/userq_xa to userq_doorbell_xa/userq_mgr_xa
Removexa_lockand use its own lock.v3: Set
queue->userq_mgr=uq_mgrinamdgpu_userq_create()Suggested-by Christian König <[email protected]>Signed-off-by Jesse Zhang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 8 +- drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 3 +- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 16 +- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 143 +++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 8 +- .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 4 +- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 22 ++- 7 files changed, 97 insertions(+), 107 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu.h +6
-2@@ -1174,6 +1174,12 @@ struct amdgpu_device {* queue fence. */ struct xarray userq_xa; +/** +* @userq_doorbell_xa: Global user queue map (doorbell index → queue) +* Key: doorbell_index (unique global identifier for the queue) +* Value: struct amdgpu_usermode_queue +*/ +struct xarray userq_doorbell_xa;/* df */ struct amdgpu_df df;@@ -1308,8 +1314,6 @@ struct amdgpu_device {*/ bool apu_prefer_gtt; -struct list_head userq_mgr_list; -struct mutex userq_mutex;bool userq_halt_for_enforce_isolation;struct amdgpu_uid *uid_info;drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1
-2@@ -4483,7 +4483,6 @@ int amdgpu_device_init(struct amdgpu_device *adev,mutex_init (&adev->gfx.userq_sch_mutex);mutex_init (&adev->gfx.workload_profile_mutex);mutex_init (&adev->vcn.workload_profile_mutex); -mutex_init (&adev->userq_mutex);amdgpu_device_init_apu_flags (adev);@@ -4511,7 +4510,7 @@ int amdgpu_device_init(struct amdgpu_device *adev,INIT_LIST_HEAD (&adev->pm.od_kobj_list); -INIT_LIST_HEAD (&adev->userq_mgr_list); +xa_init (&adev->userq_doorbell_xa); INIT_DELAYED_WORK(&adev->delayed_init_work,amdgpu_device_delayed_init_work_handler);drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +0
-0@@ -2771,22 +2771,8 @@ static int amdgpu_runtime_idle_check_userq(structdevice *dev)
Quoted 35 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1
-16struct pci_dev *pdev =to_pci_dev (dev);struct drm_device *drm_dev =pci_get_drvdata (pdev);struct amdgpu_device *adev =drm_to_adev (drm_dev); -struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp; -int queue_id; -int ret =0 ; - -mutex_lock (&adev->userq_mutex); -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { - ret = -EBUSY ; - goto done; - } - } -done: -mutex_unlock (&adev->userq_mutex); -return ret; +return xa_empty (&adev->userq_doorbell_xa) ?0 : -EBUSY ; } static int amdgpu_pmops_runtime_suspend(struct device *dev) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
indexb649ac0cedff..0712b88782c8100644drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0@@ -180,17 +180,28 @@ amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr, {struct amdgpu_device *adev = uq_mgr->adev;const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type]; +unsigned long flags;Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr,
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-1uq_funcs->mqd_destroy (uq_mgr, queue);amdgpu_userq_fence_driver_free (queue); -idr_remove (&uq_mgr->userq_idr, queue_id); + +/* Use interrupt-safe locking since IRQ handlers may access these +XArrays */ Wait a second where are IRQ handlers accessing those XAs?
Quoted 1 line
[Zhang, Jesse(Jie)]
gfx_v11_0_eop_irqon gfx11 also accesses these XAs. With lockdep enabled, a warning will appear withoutxa_lock_irqsave.
Yeah, but only the fence_drv xa needs to be irq save.
If the uq_mgr->userq_mgr_xa is used in the irq handler then something seems to be wrong here.
Quoted 1 line
We can replace it with
xa_erase_irq, just like you suggested withxa_store_irq.
Yeah that's certainly a good idea.
Quoted 198 lines
Quoted 198 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union
Quoted 22 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-1+xa_lock_irqsave (&uq_mgr->userq_mgr_xa, flags); +__xa_erase (&uq_mgr->userq_mgr_xa, (unsigned long )queue_id); +xa_unlock_irqrestore (&uq_mgr->userq_mgr_xa, flags); + +xa_lock_irqsave (&adev->userq_doorbell_xa, flags); +__xa_erase (&adev->userq_doorbell_xa, queue->doorbell_index); +xa_unlock_irqrestore (&adev->userq_doorbell_xa, flags); + + queue->userq_mgr =NULL ;kfree (queue); }static struct amdgpu_usermode_queue *amdgpu_userq_find (struct amdgpu_userq_mgr *uq_mgr,int qid) { -return idr_find (&uq_mgr->userq_idr, qid); +return xa_load (&uq_mgr->userq_mgr_xa, qid); }void @@ -462,8 +473,10 @@ amdgpu_userq_create(struct drm_file *filp, union
drm_amdgpu_userq*args)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-1struct amdgpu_db_info db_info;char *queue_name;bool skip_map_queue; +unsigned long flags; +u32 qid;uint64_t index; -int qid, r =0 ; +int r =0 ; int priority = (args->in.flags &
AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) >>Quoted 1 line
AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0@@ -486,7 +499,6 @@ amdgpu_userq_create(struct drm_file *filp, union
drm_amdgpu_userq*args)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-1* * This will also make sure we have a valid eviction fence ready to be used. */ -mutex_lock (&adev->userq_mutex);amdgpu_userq_ensure_ev_fence (&fpriv->userq_mgr, &fpriv->evf_mgr); uq_funcs = adev->userq_funcs[args->in.ip_type];@@ -546,9 +558,17 @@ amdgpu_userq_create(struct drm_file *filp, union
drm_amdgpu_userq*args)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0goto unlock; } +xa_lock_irqsave (&adev->userq_doorbell_xa, flags); + r =xa_err(__xa_store(&adev->userq_doorbell_xa, index, queue,
GFP_KERNEL));drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 568
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6
-0+if (r) { +kfree (queue); +xa_unlock_irqrestore (&adev->userq_doorbell_xa, flags); + goto unlock; + } +xa_unlock_irqrestore (&adev->userq_doorbell_xa, flags);You can use
xa_store_irq()here.drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union · line 549
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-1- qid = idr_alloc(&uq_mgr->userq_idr, queue, 1,
AMDGPU_MAX_USERQ_COUNT,GFP_KERNEL);drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union · line 569
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-1-if (qid <0 ) { + r = xa_alloc(&uq_mgr->userq_mgr_xa, &qid, queue, XA_LIMIT(1,
AMDGPU_MAX_USERQ_COUNT),GFP_KERNEL);Quoted 72 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+if (r) {drm_file_err (uq_mgr->file,"Failed to allocate a queue id\n" );amdgpu_userq_fence_driver_free (queue); uq_funcs->mqd_destroy(uq_mgr, queue); @@ -556,6 +576,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)r = -ENOMEM; goto unlock; }+
queue->userq_mgr=uq_mgr;/* don't map the queue if scheduling is halted */ if (adev->userq_halt_for_enforce_isolation && @@ -568,7 +589,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r = amdgpu_userq_map_helper(uq_mgr, queue); if (r) { drm_file_err(uq_mgr->file, "Failed to map Queue\n");drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-2-idr_remove (&uq_mgr->userq_idr, qid); +xa_erase (&uq_mgr->userq_mgr_xa, qid);amdgpu_userq_fence_driver_free (queue); uq_funcs->mqd_destroy (uq_mgr, queue);kfree (queue);@@ -591,7 +612,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) unlock: mutex_unlock(&uq_mgr->userq_mutex); -mutex_unlock (&adev->userq_mutex);return r;}
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7
-6@@ -689,11 +709,11 @@ static int amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr) {struct amdgpu_usermode_queue *queue; -int queue_id; +unsigned long queue_id;int ret =0 , r;/* Resume all the queues for this process */ -idr_for_each_entry (&uq_mgr->userq_idr, queue, queue_id) { +xa_for_each (&uq_mgr->userq_mgr_xa, queue_id, queue) { r =amdgpu_userq_restore_helper (uq_mgr, queue);if (r) ret = r;@@ -846,11 +866,11 @@ static int amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr) {struct amdgpu_usermode_queue *queue; -int queue_id; +unsigned long queue_id;int ret =0 , r;/* Try to unmap all the queues in this process ctx */ -idr_for_each_entry (&uq_mgr->userq_idr, queue, queue_id) { +xa_for_each (&uq_mgr->userq_mgr_xa, queue_id, queue) { r =amdgpu_userq_preempt_helper (uq_mgr, queue);if (r) ret = r;@@ -865,9 +885,10 @@ static int amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr *uq_mgr) {struct amdgpu_usermode_queue *queue; -int queue_id, ret; +unsigned long queue_id; +int ret; -idr_for_each_entry (&uq_mgr->userq_idr, queue, queue_id) { +xa_for_each (&uq_mgr->userq_mgr_xa, queue_id, queue) {struct dma_fence *f = queue->last_fence; if (!f || dma_fence_is_signaled(f)) @@ -920,44 +941,30 @@ intamdgpu_userq_mgr_init(struct
amdgpu_userq_mgr*userq_mgr, structdrm_file*f
structamdgpu_device*adev){ mutex_init(&userq_mgr->userq_mutex);drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-5-idr_init_base (&userq_mgr->userq_idr,1 ); +xa_init_flags (&userq_mgr->userq_mgr_xa,XA_FLAGS_ALLOC ); userq_mgr->adev = adev; userq_mgr->file = file_priv; -mutex_lock (&adev->userq_mutex); -list_add (&userq_mgr->list, &adev->userq_mgr_list); -mutex_unlock (&adev->userq_mutex); - INIT_DELAYED_WORK(&userq_mgr->resume_work,
amdgpu_userq_restore_worker);Quoted 47 lines
return 0;}
void
amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr){drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-15-struct amdgpu_device *adev = userq_mgr->adev;struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp; -uint32_t queue_id; +unsigned long queue_id;cancel_delayed_work_sync (&userq_mgr->resume_work); -mutex_lock (&adev->userq_mutex);mutex_lock (&userq_mgr->userq_mutex); -idr_for_each_entry (&userq_mgr->userq_idr, queue, queue_id) { +xa_for_each (&userq_mgr->userq_mgr_xa, queue_id, queue) {amdgpu_userq_wait_for_last_fence (userq_mgr, queue);amdgpu_userq_unmap_helper (userq_mgr, queue);amdgpu_userq_cleanup (userq_mgr, queue, queue_id); } -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { -if (uqm == userq_mgr) { -list_del (&uqm->list); -break ; - } - } -idr_destroy (&userq_mgr->userq_idr); +xa_destroy (&userq_mgr->userq_mgr_xa);mutex_unlock (&userq_mgr->userq_mutex); -mutex_unlock (&adev->userq_mutex);mutex_destroy (&userq_mgr->userq_mutex); }@@ -965,25 +972,23 @@ 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, *tmp; -int queue_id; +struct amdgpu_userq_mgr *uqm; +unsigned long queue_id;int ret =0 , r;if (!ip_mask)return 0 ;
Quoted 8 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_suspend(struct amdgpu_device *adev) · line 983
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-2-mutex_lock (&adev->userq_mutex); -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + uqm = queue->userq_mgr;That function is used during reset, isn't it?
[Zhang, Jesse(Jie)] No, amdgpu_userq_suspend/amdgpu_userq_resume are not used during reset.
but They are called when executing the suspend command. I tried the suspend case, amdgpu_userq_suspend/amdgpu_userq_resume and no error occurred.
Testing can only disprove a design and not validate it.
During normal suspend/resume this isn't an issue because userspace is completely frozen.
But when this is used during reset as well we need to be super careful to not access fredd up memory.
Regards,
Christian.
Quoted 258 lines
Quoted 257 lines
If yes we somehow need to make sure that neither queue nor
userq_mgris properly initialized and not freed up while the reset is ongoing.The easiest way of doing that is to hold the read side of the reset lock while creating/destroying queues and the manager data structure.
Quoted 24 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-8cancel_delayed_work_sync (&uqm->resume_work);mutex_lock (&uqm->userq_mutex); -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { - r =amdgpu_userq_unmap_helper (uqm, queue); -if (r) - ret = r; - } + r =amdgpu_userq_unmap_helper (uqm, queue); +if (r) + ret = r;mutex_unlock (&uqm->userq_mutex); } -mutex_unlock (&adev->userq_mutex); +return ret; }@@ -991,24 +996,22 @@ 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, *tmp; -int queue_id; +struct amdgpu_userq_mgr *uqm;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_resume(struct amdgpu_device · line 1007
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-2+unsigned long queue_id;int ret =0 , r;if (!ip_mask)return 0 ; -mutex_lock (&adev->userq_mutex); -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + uqm = queue->userq_mgr;Not sure about those ones here, but the same point that applies to the reset handling applies here as well.
Apart from that the patch looks good to me.
Regards,
Christian.Quoted 18 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-6mutex_lock (&uqm->userq_mutex); -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { - r =amdgpu_userq_map_helper (uqm, queue); -if (r) - ret = r; - } + r =amdgpu_userq_map_helper (uqm, queue); +if (r) + ret = r;mutex_unlock (&uqm->userq_mutex); } -mutex_unlock (&adev->userq_mutex); +return ret; }@@ -1017,33 +1020,31 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev, {drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int · line 1028
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-2u32 ip_mask =amdgpu_userq_get_supported_ip_mask (adev);struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp; -int queue_id; +struct amdgpu_userq_mgr *uqm; +unsigned long queue_id;int ret =0 , r;/* only need to stop gfx/compute */ if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 <<
AMDGPU_HW_IP_COMPUTE))))Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-1return 0 ; -mutex_lock (&adev->userq_mutex);if (adev->userq_halt_for_enforce_isolation)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-4dev_warn (adev->dev,"userq scheduling already stopped!\n" ); adev->userq_halt_for_enforce_isolation = true; -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + uqm = queue->userq_mgr;cancel_delayed_work_sync (&uqm->resume_work);mutex_lock (&uqm->userq_mutex); -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { -if (((queue->queue_type ==AMDGPU_HW_IP_GFX ) || - (queue->queue_type == AMDGPU_HW_IP_COMPUTE))&&
Quoted 20 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7
-6- (queue->xcp_id == idx)) { - r =amdgpu_userq_preempt_helper (uqm, queue); -if (r) - ret = r; - } +if (((queue->queue_type ==AMDGPU_HW_IP_GFX ) || + (queue->queue_type ==AMDGPU_HW_IP_COMPUTE )) && + (queue->xcp_id == idx)) { + r =amdgpu_userq_preempt_helper (uqm, queue); +if (r) + ret = r; }mutex_unlock (&uqm->userq_mutex); } -mutex_unlock (&adev->userq_mutex); +return ret; }drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int · line 1053
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0@@ -1052,21 +1053,20 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,{
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-2u32 ip_mask =amdgpu_userq_get_supported_ip_mask (adev);struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp; -int queue_id; +struct amdgpu_userq_mgr *uqm; +unsigned long queue_id;int ret =0 , r;/* only need to stop gfx/compute */ if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 <<
AMDGPU_HW_IP_COMPUTE))))drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-3return 0 ; -mutex_lock (&adev->userq_mutex);if (!adev->userq_halt_for_enforce_isolation)dev_warn (adev->dev,"userq scheduling already started!\n" ); adev->userq_halt_for_enforce_isolation = false; -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + uqm = queue->userq_mgr;mutex_lock (&uqm->userq_mutex); -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) {if (((queue->queue_type ==AMDGPU_HW_IP_GFX ) || (queue->queue_type == AMDGPU_HW_IP_COMPUTE))&&
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0(queue->xcp_id == idx)) {@@ -1074,9 +1074,8 @@ intamdgpu_userq_start_sched_for_enforce_isolation(struct
amdgpu_device*adev,drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_signal_ioctl(struct drm_device *dev,
Quoted 39 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-2if (r) ret = r; } - }mutex_unlock (&uqm->userq_mutex); } -mutex_unlock (&adev->userq_mutex); +return ret; }drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6
-2@@ -88,11 +88,15 @@ struct amdgpu_userq_funcs {/* Usermode queues for gfx */ struct amdgpu_userq_mgr { -struct idr userq_idr; +/** +* @userq_mgr_xa: Per-process user queue map (queue ID → queue) +* Key: queue_id (unique ID within the process's userq manager) +* Value: struct amdgpu_usermode_queue +*/ +struct xarray userq_mgr_xa;struct mutex userq_mutex;struct amdgpu_device *adev;struct delayed_work resume_work; -struct list_head list;struct drm_file *file; };drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0
-0@@ -537,7 +537,7 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev,void *data,
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_wait_ioctl(struct drm_device *dev,
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1
-1}/* Retrieve the user queue */ - queue =idr_find (&userq_mgr->userq_idr, args->queue_id); + queue =xa_load (&userq_mgr->userq_mgr_xa, args->queue_id);if (!queue) { r = -ENOENT ; goto put_gobj_write;@@ -899,7 +899,7 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev,void *data,
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct
Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1
-1*/ num_fences =dma_fence_dedup_array (fences, num_fences); - waitq =idr_find (&userq_mgr->userq_idr, wait_info->waitq_id); + waitq =xa_load (&userq_mgr->userq_mgr_xa, wait_info->waitq_id);if (!waitq) { r = -EINVAL ; goto free_fences;drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0@@ -205,9 +205,9 @@ static int mes_userq_detect_and_reset(struct
amdgpu_device*adev,drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-2int db_array_size =amdgpu_mes_get_hung_queue_db_array_size (adev);struct mes_detect_and_reset_queue_input input;struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp;unsigned int hung_db_num =0 ; -int queue_id, r, i; +unsigned long queue_id; +int r, i;u32 db_array[4 ];if (db_array_size >4 ) {@@ -227,16 +227,14 @@ static int mes_userq_detect_and_reset(struct
amdgpu_device*adev,drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 228
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0if (r) { dev_err(adev->dev, "Failed to detect and reset queues, err (%d)\n",r);
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct amdgpu_device *adev,
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-5}else if (hung_db_num) { -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { -if (queue->queue_type == queue_type) { -for (i =0 ; i < hung_db_num; i++) { - if (queue->doorbell_index ==db_array[i]) {
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 235
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-1- queue->state =
AMDGPU_USERQ_STATE_HUNG;drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 238
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-2-atomic_inc (&adev- gpu_reset_counter); -amdgpu_userq_fence_driver_force_completion(queue);drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 239
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-1-drm_dev_wedged_event(adev_to_drm(adev),
DRM_WEDGE_RECOVERY_NONE, NULL);drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct amdgpu_device *adev,
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +5
-1- } +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { +if (queue->queue_type == queue_type) { +for (i =0 ; i < hung_db_num; i++) { +if (queue->doorbell_index == db_array[i]) { + queue->state =
AMDGPU_USERQ_STATE_HUNG;drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 238
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-0+atomic_inc (&adev- gpu_reset_counter); +amdgpu_userq_fence_driver_force_completion(queue);drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 239
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-0+drm_dev_wedged_event(adev_to_drm(adev),
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-0+DRM_WEDGE_RECOVERY_NONE, NULL); } } }
[AMD Official Use Only - AMD Internal Distribution Only]
Quoted 409 lines
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, September 24, 2025 8:18 PM
To: Zhang, Jesse(Jie) <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [v6 1/2] drm/amdgpu: Convert amdgpu userqueue management from IDR to XArrayQuoted 97 lines · Jesse(Jie)
[AMD Official Use Only - AMD Internal Distribution Only]
Quoted 95 lines
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, September 24, 2025 5:29 PM
To: Zhang, Jesse(Jie) <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [v6 1/2] drm/amdgpu: Convert amdgpu userqueue management from IDR to XArrayQuoted 10 lines · Jesse.Zhang
This commit refactors the AMDGPU userqueue management subsystem to replace IDR (ID Allocation) with XArray for improved performance, scalability, and maintainability. The changes address several issues with the previous IDR implementation and provide better locking semantics.
Key changes:
1. **Global XArray Introduction**:
- Addeduserq_doorbell_xatostruct amdgpu_devicefor global
queuetracking
Quoted 74 lines
- Uses
doorbell_indexas key for efficient global lookup
- Replaces the previoususerq_mgr_listlinked list approach2. **Per-process XArray Conversion**:
- Replaceduserq_idrwithuserq_mgr_xainstruct amdgpu_userq_mgr
- Maintains per-process queue tracking withqueue_idas key
- UsesXA_FLAGS_ALLOCfor automatic ID allocation3. **Locking Improvements**:
- Removed globaluserq_mutexfromstruct amdgpu_device
- Replaced with fine-grained XArray locking using XArray's
internal spinlocks4. **Runtime Idle Check Optimization**:
- Updatedamdgpu_runtime_idle_check_userq()to usexa_empty5. **Queue Management Functions**:
- Converted all IDR operations to equivalent XArray functions:
-idr_alloc()→xa_alloc()
-idr_find()→xa_load()
-idr_remove()→xa_erase()
-idr_for_each()→xa_for_each()Benefits:
- **Performance**: XArray provides better scalability for large numbers of queues
- **Memory Efficiency**: Reduced memory overhead compared to IDR
- **Thread Safety**: Improved locking semantics with XArray's internal spinlocksv2: rename userq_global_xa/userq_xa to userq_doorbell_xa/userq_mgr_xa
Removexa_lockand use its own lock.v3: Set
queue->userq_mgr=uq_mgrinamdgpu_userq_create()Suggested-by Christian König <[email protected]>Signed-off-by Jesse Zhang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 8 +- drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 3 +- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 16 +- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 143 +++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 8 +- .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 4 +- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 22 ++- 7 files changed, 97 insertions(+), 107 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu.h +6
-2@@ -1174,6 +1174,12 @@ struct amdgpu_device {* queue fence. */ struct xarray userq_xa; +/** +* @userq_doorbell_xa: Global user queue map (doorbell index → queue) +* Key: doorbell_index (unique global identifier for the queue) +* Value: struct amdgpu_usermode_queue +*/ +struct xarray userq_doorbell_xa;/* df */ struct amdgpu_df df;@@ -1308,8 +1314,6 @@ struct amdgpu_device {*/ bool apu_prefer_gtt; -struct list_head userq_mgr_list; -struct mutex userq_mutex;bool userq_halt_for_enforce_isolation;struct amdgpu_uid *uid_info;Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c int amdgpu_device_init(struct amdgpu_device
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0
-0@@ -4483,7 +4483,6 @@ int amdgpu_device_init(struct amdgpu_device*adev,
Quoted 68 lines
Quoted 60 lines
Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1
-2mutex_init (&adev->gfx.userq_sch_mutex);mutex_init (&adev->gfx.workload_profile_mutex);mutex_init (&adev->vcn.workload_profile_mutex); -mutex_init (&adev->userq_mutex);amdgpu_device_init_apu_flags (adev);@@ -4511,7 +4510,7 @@ int amdgpu_device_init(struct amdgpu_device *adev,INIT_LIST_HEAD (&adev->pm.od_kobj_list); -INIT_LIST_HEAD (&adev->userq_mgr_list); +xa_init (&adev->userq_doorbell_xa); INIT_DELAYED_WORK(&adev->delayed_init_work,amdgpu_device_delayed_init_work_handler);drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +0
-0@@ -2771,22 +2771,8 @@ static intQuoted 1 line
amdgpu_runtime_idle_check_userq(struct
device *dev)
Quoted 35 lines
struct pci_dev *pdev = to_pci_dev(dev); struct drm_device *drm_dev = pci_get_drvdata(pdev); struct amdgpu_device *adev = drm_to_adev(drm_dev);drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1
-16-struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp; -int queue_id; -int ret =0 ; - -mutex_lock (&adev->userq_mutex); -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { - ret = -EBUSY ; - goto done; - } - } -done: -mutex_unlock (&adev->userq_mutex); -return ret; +return xa_empty (&adev->userq_doorbell_xa) ?0 : -EBUSY ; } static int amdgpu_pmops_runtime_suspend(struct device *dev) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
indexb649ac0cedff..0712b88782c8100644drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0@@ -180,17 +180,28 @@ amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr, {struct amdgpu_device *adev = uq_mgr->adev;const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type]; +unsigned long flags;Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr,
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-1uq_funcs->mqd_destroy (uq_mgr, queue);amdgpu_userq_fence_driver_free (queue); -idr_remove (&uq_mgr->userq_idr, queue_id); + +/* Use interrupt-safe locking since IRQ handlers may access +these XArrays */ Wait a second where are IRQ handlers accessing those XAs?
Quoted 1 line
[Zhang, Jesse(Jie)]
gfx_v11_0_eop_irqon gfx11 also accesses these XAs. Withlockdep enabled, a warning will appear without
xa_lock_irqsave.Yeah, but only the
fence_drvxa needs to be irq save.If the
uq_mgr->userq_mgr_xais used in the irq handler then something seems to be wrong here.Quoted 1 line
We can replace it with
xa_erase_irq, just like you suggested withxa_store_irq.Yeah that's certainly a good idea.
Quoted 68 lines
Quoted 68 lines
Quoted 22 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-1+xa_lock_irqsave (&uq_mgr->userq_mgr_xa, flags); +__xa_erase (&uq_mgr->userq_mgr_xa, (unsigned long )queue_id); +xa_unlock_irqrestore (&uq_mgr->userq_mgr_xa, flags); + +xa_lock_irqsave (&adev->userq_doorbell_xa, flags); +__xa_erase (&adev->userq_doorbell_xa, queue->doorbell_index); +xa_unlock_irqrestore (&adev->userq_doorbell_xa, flags); + + queue->userq_mgr =NULL ;kfree (queue); }static struct amdgpu_usermode_queue *amdgpu_userq_find (struct amdgpu_userq_mgr *uq_mgr,int qid) { -return idr_find (&uq_mgr->userq_idr, qid); +return xa_load (&uq_mgr->userq_mgr_xa, qid); }void @@ -462,8 +473,10 @@ amdgpu_userq_create(struct drm_file *filp,Quoted 1 line
union
drm_amdgpu_userq*args)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
Quoted 10 lines
struct amdgpu_db_info db_info; char *queue_name; bool skip_map_queue;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-1+unsigned long flags; +u32 qid;uint64_t index; -int qid, r =0 ; +int r =0 ; int priority = (args->in.flags &
AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) >>Quoted 1 line
AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0@@ -486,7 +499,6 @@ amdgpu_userq_create(struct drm_file *filp, union
drm_amdgpu_userq*args)Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-1* * This will also make sure we have a valid eviction fence ready to be used. */ -mutex_lock (&adev->userq_mutex);amdgpu_userq_ensure_ev_fence (&fpriv->userq_mgr, &fpriv->evf_mgr);uq_funcs = adev->userq_funcs[args->in.ip_type];drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0@@ -546,9 +558,17 @@ amdgpu_userq_create(struct drm_file *filp,union
drm_amdgpu_userq*args)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
Quoted 5 lines
goto unlock; }drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0+xa_lock_irqsave (&adev->userq_doorbell_xa, flags); + r =xa_err(__xa_store(&adev->userq_doorbell_xa, index, queue,
GFP_KERNEL));drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 568
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6
-0+if (r) { +kfree (queue); +xa_unlock_irqrestore (&adev->userq_doorbell_xa, flags); + goto unlock; + } +xa_unlock_irqrestore (&adev->userq_doorbell_xa, flags);You can use
xa_store_irq()here.Quoted 2 lines
- qid = idr_alloc(&uq_mgr->userq_idr, queue, 1,
AMDGPU_MAX_USERQ_COUNT,GFP_KERNEL);Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-1-if (qid <0 ) { + r = xa_alloc(&uq_mgr->userq_mgr_xa, &qid, queue, XA_LIMIT(1,
AMDGPU_MAX_USERQ_COUNT),GFP_KERNEL);Quoted 5 lines
+ if (r) { drm_file_err(uq_mgr->file, "Failed to allocate a queue id\n"); amdgpu_userq_fence_driver_free(queue); uq_funcs->mqd_destroy(uq_mgr, queue); @@ -556,6 +576,7@@ amdgpu_userq_create(struct
drm_file*filp, uniondrm_amdgpu_userq*args)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
r = -ENOMEM; goto unlock; }+
queue->userq_mgr=uq_mgr;/* don't map the queue if scheduling is halted */ if (adev->userq_halt_for_enforce_isolation && @@ -568,7 +589,7@@ amdgpu_userq_create(struct
drm_file*filp, uniondrm_amdgpu_userq*args)
Quoted 50 lines
Quoted 50 lines
Quoted 50 lines
r = amdgpu_userq_map_helper(uq_mgr, queue); if (r) { drm_file_err(uq_mgr->file, "Failed to map Queue\n");drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-2-idr_remove (&uq_mgr->userq_idr, qid); +xa_erase (&uq_mgr->userq_mgr_xa, qid);amdgpu_userq_fence_driver_free (queue); uq_funcs->mqd_destroy (uq_mgr, queue);kfree (queue);@@ -591,7 +612,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) unlock: mutex_unlock(&uq_mgr->userq_mutex); -mutex_unlock (&adev->userq_mutex);return r;}
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6
-5@@ -689,11 +709,11 @@ static int amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr) {struct amdgpu_usermode_queue *queue; -int queue_id; +unsigned long queue_id;int ret =0 , r;/* Resume all the queues for this process */ -idr_for_each_entry (&uq_mgr->userq_idr, queue, queue_id) { +xa_for_each (&uq_mgr->userq_mgr_xa, queue_id, queue) { r =amdgpu_userq_restore_helper (uq_mgr, queue);if (r) ret = r;@@ -846,11 +866,11 @@ static int amdgpu_userq_evict_all(struct amdgpu_userq_mgr *uq_mgr) {struct amdgpu_usermode_queue *queue; -int queue_id; +unsigned long queue_id;int ret =0 , r;/* Try to unmap all the queues in this process ctx */ -idr_for_each_entry (&uq_mgr->userq_idr, queue, queue_id) { +xa_for_each (&uq_mgr->userq_mgr_xa, queue_id, queue) { r =amdgpu_userq_preempt_helper (uq_mgr, queue);if (r) ret = r;@@ -865,9 +885,10 @@ static int amdgpu_userq_wait_for_signal(struct amdgpu_userq_mgr *uq_mgr) {struct amdgpu_usermode_queue *queue; -int queue_id, ret; +unsigned long queue_id; +int ret;Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-1-idr_for_each_entry (&uq_mgr->userq_idr, queue, queue_id) { +xa_for_each (&uq_mgr->userq_mgr_xa, queue_id, queue) {struct dma_fence *f = queue->last_fence; if (!f || dma_fence_is_signaled(f)) @@ -920,44 +941,30@@ int amdgpu_userq_mgr_init(struct
amdgpu_userq_mgr*userq_mgr, struct
drm_file*fQuoted 60 lines
Quoted 60 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f
Quoted 12 lines
struct amdgpu_device *adev) { mutex_init(&userq_mgr->userq_mutex);drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-5-idr_init_base (&userq_mgr->userq_idr,1 ); +xa_init_flags (&userq_mgr->userq_mgr_xa,XA_FLAGS_ALLOC ); userq_mgr->adev = adev; userq_mgr->file = file_priv; -mutex_lock (&adev->userq_mutex); -list_add (&userq_mgr->list, &adev->userq_mgr_list); -mutex_unlock (&adev->userq_mutex); - INIT_DELAYED_WORK(&userq_mgr->resume_work,
amdgpu_userq_restore_worker);Quoted 47 lines
return 0;}
void
amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr){drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-15-struct amdgpu_device *adev = userq_mgr->adev;struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp; -uint32_t queue_id; +unsigned long queue_id;cancel_delayed_work_sync (&userq_mgr->resume_work); -mutex_lock (&adev->userq_mutex);mutex_lock (&userq_mgr->userq_mutex); -idr_for_each_entry (&userq_mgr->userq_idr, queue, queue_id) { +xa_for_each (&userq_mgr->userq_mgr_xa, queue_id, queue) {amdgpu_userq_wait_for_last_fence (userq_mgr, queue);amdgpu_userq_unmap_helper (userq_mgr, queue);amdgpu_userq_cleanup (userq_mgr, queue, queue_id); } -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { -if (uqm == userq_mgr) { -list_del (&uqm->list); -break ; - } - } -idr_destroy (&userq_mgr->userq_idr); +xa_destroy (&userq_mgr->userq_mgr_xa);mutex_unlock (&userq_mgr->userq_mutex); -mutex_unlock (&adev->userq_mutex);mutex_destroy (&userq_mgr->userq_mutex); }@@ -965,25 +972,23 @@ 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, *tmp; -int queue_id; +struct amdgpu_userq_mgr *uqm; +unsigned long queue_id;int ret =0 , r;if (!ip_mask)return 0 ;Quoted 7 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_suspend(struct amdgpu_device *adev) · line 983
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-2-mutex_lock (&adev->userq_mutex); -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + uqm = queue->userq_mgr;That function is used during reset, isn't it?
[Zhang, Jesse(Jie)] No, amdgpu_userq_suspend/amdgpu_userq_resume are not
used during reset.
Quoted 1 line
but They are called when executing the suspend command. I tried the suspend
case, amdgpu_userq_suspend/amdgpu_userq_resume and no error occurred.
Testing can only disprove a design and not validate it.
Quoted 14 lines
During normal suspend/resume this isn't an issue because userspace is completely frozen.
But when this is used during reset as well we need to be super careful to not access fredd up memory.
Regards,
Christian.Quoted 5 lines
Quoted 5 lines
If yes we somehow need to make sure that neither queue nor
userq_mgris properly initialized and not freed up while the reset is ongoing.The easiest way of doing that is to hold the read side of the reset lock while creating/destroying queues and the manager data structure.
[Zhang, Jesse(Jie)] thanks Christian, will update the patch, and hold the read side of reset lock by down_read(&adev->reset_domain->sem).
Thanks
Jesse
Quoted 261 lines
Quoted 60 lines
Quoted 60 lines
Quoted 25 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-8cancel_delayed_work_sync (&uqm->resume_work);mutex_lock (&uqm->userq_mutex); -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { - r =amdgpu_userq_unmap_helper (uqm, queue); -if (r) - ret = r; - } + r =amdgpu_userq_unmap_helper (uqm, queue); +if (r) + ret = r;mutex_unlock (&uqm->userq_mutex); } -mutex_unlock (&adev->userq_mutex); +return ret; }@@ -991,24 +996,22 @@ 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, *tmp; -int queue_id; +struct amdgpu_userq_mgr *uqm;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_resume(struct amdgpu_device · line 1007
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-2+unsigned long queue_id;int ret =0 , r;if (!ip_mask)return 0 ; -mutex_lock (&adev->userq_mutex); -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + uqm = queue->userq_mgr;Not sure about those ones here, but the same point that applies to the reset handling applies here as well.
Apart from that the patch looks good to me.
Regards,
Christian.Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-6mutex_lock (&uqm->userq_mutex); -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { - r =amdgpu_userq_map_helper (uqm, queue); -if (r) - ret = r; - } + r =amdgpu_userq_map_helper (uqm, queue); +if (r) + ret = r;mutex_unlock (&uqm->userq_mutex); } -mutex_unlock (&adev->userq_mutex); +return ret; }Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0@@ -1017,33 +1020,31 @@ intamdgpu_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, *tmp; >>> - int queue_id; >>> + struct amdgpu_userq_mgr *uqm; >>> + unsigned long queue_id; >>> int ret = 0, r;Quoted 191 lines
Quoted 190 lines
Quoted 3 lines
/* only need to stop gfx/compute */ if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 <<
AMDGPU_HW_IP_COMPUTE))))Quoted 4 lines
return 0;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-1-mutex_lock (&adev->userq_mutex);if (adev->userq_halt_for_enforce_isolation)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-4dev_warn (adev->dev,"userq scheduling already stopped!\n" ); adev->userq_halt_for_enforce_isolation = true; -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + uqm = queue->userq_mgr;cancel_delayed_work_sync (&uqm->resume_work);mutex_lock (&uqm->userq_mutex); -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { -if (((queue->queue_type ==AMDGPU_HW_IP_GFX ) || - (queue->queue_type == AMDGPU_HW_IP_COMPUTE))&&
Quoted 20 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7
-6- (queue->xcp_id == idx)) { - r =amdgpu_userq_preempt_helper (uqm, queue); -if (r) - ret = r; - } +if (((queue->queue_type ==AMDGPU_HW_IP_GFX ) || + (queue->queue_type ==AMDGPU_HW_IP_COMPUTE )) && + (queue->xcp_id == idx)) { + r =amdgpu_userq_preempt_helper (uqm, queue); +if (r) + ret = r; }mutex_unlock (&uqm->userq_mutex); } -mutex_unlock (&adev->userq_mutex); +return ret; }@@ -1052,21 +1053,20 @@ intQuoted 2 lines
amdgpu_userq_start_sched_for_enforce_isolation(struct
amdgpu_device*adev,{
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 10 lines
u32 ip_mask = amdgpu_userq_get_supported_ip_mask(adev); struct amdgpu_usermode_queue *queue;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-2-struct amdgpu_userq_mgr *uqm, *tmp; -int queue_id; +struct amdgpu_userq_mgr *uqm; +unsigned long queue_id;int ret =0 , r;/* only need to stop gfx/compute */ if (!(ip_mask & ((1 << AMDGPU_HW_IP_GFX) | (1 <<
AMDGPU_HW_IP_COMPUTE))))Quoted 13 lines
return 0;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-3-mutex_lock (&adev->userq_mutex);if (!adev->userq_halt_for_enforce_isolation)dev_warn (adev->dev,"userq scheduling already started!\n" ); adev->userq_halt_for_enforce_isolation = false; -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { + uqm = queue->userq_mgr;mutex_lock (&uqm->userq_mutex); -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { if (((queue->queue_type == AMDGPU_HW_IP_GFX) || (queue->queue_type ==Quoted 1 line
AMDGPU_HW_IP_COMPUTE))&&
Quoted 2 lines
(queue->xcp_id == idx)) { @@ -1074,9 +1074,8@@ int
amdgpu_userq_start_sched_for_enforce_isolation(struct
amdgpu_device*adev,Quoted 39 lines
if (r) ret = r; }drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-2- }mutex_unlock (&uqm->userq_mutex); } -mutex_unlock (&adev->userq_mutex); +return ret; }drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +6
-2@@ -88,11 +88,15 @@ struct amdgpu_userq_funcs {/* Usermode queues for gfx */ struct amdgpu_userq_mgr { -struct idr userq_idr; +/** +* @userq_mgr_xa: Per-process user queue map (queue ID → queue) +* Key: queue_id (unique ID within the process's userq manager) +* Value: struct amdgpu_usermode_queue +*/ +struct xarray userq_mgr_xa;struct mutex userq_mutex;struct amdgpu_device *adev;struct delayed_work resume_work; -struct list_head list;struct drm_file *file; };drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0
-0drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_signal_ioctl(struct drm_device · line 537
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0
-0@@ -537,7 +537,7 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev,void *data,
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c int amdgpu_userq_wait_ioctl(struct drm_device · line 899
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1
-1}/* Retrieve the user queue */ - queue =idr_find (&userq_mgr->userq_idr, args->queue_id); + queue =xa_load (&userq_mgr->userq_mgr_xa, args->queue_id);if (!queue) { r = -ENOENT ;goto put_gobj_write;drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0
-0@@ -899,7 +899,7 @@ int amdgpu_userq_wait_ioctl(struct drm_device *dev,void *data,
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct
Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2
-1*/ num_fences =dma_fence_dedup_array (fences, num_fences); - waitq =idr_find (&userq_mgr->userq_idr, wait_info->waitq_id); + waitq =xa_load (&userq_mgr->userq_mgr_xa, + wait_info->waitq_id);if (!waitq) { r = -EINVAL ; goto free_fences; diff --git
a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.cb/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
index2db9b2c63693..9e4229c79c69100644drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0@@ -205,9 +205,9 @@ static int mes_userq_detect_and_reset(struct
amdgpu_device*adev,drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-2int db_array_size =amdgpu_mes_get_hung_queue_db_array_size (adev);struct mes_detect_and_reset_queue_input input;struct amdgpu_usermode_queue *queue; -struct amdgpu_userq_mgr *uqm, *tmp;unsigned int hung_db_num =0 ; -int queue_id, r, i; +unsigned long queue_id; +int r, i;u32 db_array[4 ];if (db_array_size >4 ) {@@ -227,16 +227,14 @@ static int mes_userq_detect_and_reset(struct
amdgpu_device*adev,drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 229
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0if (r) { dev_err(adev->dev, "Failed to detect and reset queues, err (%d)\n",r);
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct amdgpu_device *adev,
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-5}else if (hung_db_num) { -list_for_each_entry_safe (uqm, tmp, &adev->userq_mgr_list, list) { -idr_for_each_entry (&uqm->userq_idr, queue, queue_id) { -if (queue->queue_type == queue_type) { -for (i =0 ; i < hung_db_num; i++) { - if (queue->doorbell_index ==db_array[i]) {
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 236
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-1- queue->state =
AMDGPU_USERQ_STATE_HUNG;drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 239
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-2-atomic_inc (&adev- gpu_reset_counter); -amdgpu_userq_fence_driver_force_completion(queue);drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 240
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-1-drm_dev_wedged_event(adev_to_drm(adev),
DRM_WEDGE_RECOVERY_NONE, NULL);drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct amdgpu_device *adev,
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +5
-1- } +xa_for_each (&adev->userq_doorbell_xa, queue_id, queue) { +if (queue->queue_type == queue_type) { +for (i =0 ; i < hung_db_num; i++) { +if (queue->doorbell_index == db_array[i]) { + queue->state =
AMDGPU_USERQ_STATE_HUNG;drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 239
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-0+atomic_inc (&adev- gpu_reset_counter); +amdgpu_userq_fence_driver_force_completion(queue);drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_detect_and_reset(struct · line 240
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-0+drm_dev_wedged_event(adev_to_drm(adev),
Quoted 4 lines
+DRM_WEDGE_RECOVERY_NONE, NULL); } } }