Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/amdgpu/userq: unsynchronized xa_for_each() UAF and race in HQD helpers --
commit f5832ab9e726e4c6325d50c80fa806f8412f97f7 Author: Jesse Zhang <[email protected]> drm/amdgpu/userq: add helpers to keep the user queues of one engine across a reset This commit adds helpers to save, restore, and clear the HQDs of user queues belonging to a single engine across a soft reset. > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c > b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c > index ed6f3b67e8720..495198ba1b0b0 100644 > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c > @@ -1594,3 +1594,110 @@ int amdgpu_userq_post_reset(struct amdgpu_device > *adev, bool vram_lost) > > return ret; > } > + > +/** > + * amdgpu_userq_save_hqds - save the HQDs of the user queues before an > engine reset > + * @adev: amdgpu device > + * @queue_type: AMDGPU_HW_IP_* type of the queues on the reset engine > + */ > +void amdgpu_userq_save_hqds(struct amdgpu_device *adev, u32 queue_type) > +{ > + struct amdgpu_gfx_saved_hqd hqd; > + struct amdgpu_usermode_queue *queue; > + unsigned long queue_id; > + > + adev->gfx.num_saved_gfx_hqds = 0; > + if (!adev->gfx.funcs->save_hqd) > + return; > + > + xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { > + if (adev->gfx.num_saved_gfx_hqds >= AMDGPU_MAX_GFX_QUEUES) > + break; > + if (queue->queue_type != queue_type || > + queue->state != AMDGPU_USERQ_STATE_MAPPED) > + continue; > + if (adev->gfx.funcs->save_hqd(adev, queue->doorbell_index, > &hqd) && > + hqd.on_hqd) > + > adev->gfx.saved_gfx_hqds[adev->gfx.num_saved_gfx_hqds++] = hqd; > + } > +} > + > +static struct amdgpu_gfx_saved_hqd * > +amdgpu_userq_find_saved_hqd(struct amdgpu_device *adev, > + struct amdgpu_usermode_queue *queue) > +{ > + struct amdgpu_gfx_saved_hqd *hqd; > + u32 i; > + > + for (i = 0; i < adev->gfx.num_saved_gfx_hqds; i++) { > + hqd = &adev->gfx.saved_gfx_hqds[i]; > + /* Check the MQD too: a new queue may reuse the doorbell. */ > + if (hqd->doorbell_index == queue->doorbell_index && > + hqd->mqd_addr == queue->mqd.gpu_addr) > + return hqd; > + } > + return NULL; > +} > + > +/** > + * amdgpu_userq_restore_hqds - restore the user queues after an engine reset > + * @adev: amdgpu device > + * @queue_type: AMDGPU_HW_IP_* type of the queues on the reset engine > + * @guilty: the queue that hung, or NULL > + * > + * MES doesn't map a queue whose HQD the reset cleared again on its own: > unmap > + * it from MES, restore its MQD, and map it again. > + */ > +void amdgpu_userq_restore_hqds(struct amdgpu_device *adev, u32 queue_type, > + struct amdgpu_usermode_queue *guilty) > +{ > + const struct amdgpu_userq_funcs *userq_funcs = > adev->userq_funcs[queue_type]; > + struct amdgpu_usermode_queue *queue; > + struct amdgpu_gfx_saved_hqd *hqd; > + unsigned long queue_id; > + int r; > + > + xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) { > + if (queue->queue_type != queue_type || > + queue->state != AMDGPU_USERQ_STATE_MAPPED) > + continue; > + > + if (queue == guilty) { > + userq_funcs->unmap(queue); > + amdgpu_userq_fence_driver_force_completion(queue); > + trace_amdgpu_userq_state_changed(queue, > AMDGPU_USERQ_STATE_HUNG); > + queue->state = AMDGPU_USERQ_STATE_HUNG; > + continue; > + } > + > + hqd = amdgpu_userq_find_saved_hqd(adev, queue); > + if (!hqd) > + continue; > + > + userq_funcs->unmap(queue); > + if (adev->gfx.funcs->restore_hqd(adev, queue->mqd.cpu_ptr, > + queue->userq_prop->queue_size, > + hqd)) > + > amdgpu_userq_fence_driver_force_completion_with_seqno(queue, > + > hqd->wptr); > + r = userq_funcs->map(queue); [Severity: High] Can amdgpu_userq_save_hqds() and amdgpu_userq_restore_hqds() race with amdgpu_userq_destroy() and cause a use-after-free on queue? Unlike a full GPU reset, which holds down_write(&adev->reset_domain->sem), an engine soft reset in amdgpu_device_ip_soft_reset() only acquires down_read(&adev->reset_domain->sem). amdgpu_userq_detach_doorbell() and amdgpu_userq_unmap_helper() in the queue destruction path also only acquire down_read(&adev->reset_domain->sem), so queue destruction is not blocked while these helpers walk userq_doorbell_xa (as the comment in amdgpu_userq_find_saved_hqd() also notes). Because xa_for_each() releases rcu_read_lock() before entering the loop body and nothing pins queue or holds queue->userq_mgr->userq_mutex: CPU0 CPU1 amdgpu_userq_restore_hqds() xa_for_each(..., queue) amdgpu_userq_destroy() amdgpu_userq_detach_doorbell(queue) ... uq_funcs->mqd_destroy(queue) kfree(queue) if (queue->queue_type != ...) userq_funcs->unmap(queue) <-- use-after-free Even when the queue is not destroyed, iterating and calling userq_funcs->unmap() and userq_funcs->map() without holding queue->userq_mgr->userq_mutex races with concurrent eviction and restore workers that also run under down_read(&adev->reset_domain->sem), which can corrupt queue->state or double-unmap and double-free queue->gang_ctx_array_index. In addition, if a queue is destroyed and a new queue is created between amdgpu_userq_save_hqds() and amdgpu_userq_restore_hqds(), both the freed doorbell index and the freed GTT MQD address (queue->mqd.gpu_addr) can be reallocated to the new queue, causing amdgpu_userq_find_saved_hqd() to match and overwrite the new queue's MQD with the old queue's saved HQD state. -- Sashiko AI review ยท https://sashiko.dev/#/patchset/[email protected]?part=8
