Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] drm/amdgpu/gfx12: race and UAF on user queues in
  gfx_v12_0_soft_reset_engine()
--

commit 1eb59ae54b68b8bc41a2294b6011763c46222bdb
Author: Jesse Zhang <[email protected]>

drm/amdgpu/gfx12: add a gfx engine soft reset

This commit implements soft_reset_engine() for the GFX12 graphics engine
to reset CPF/CPG, PFP/ME, and the GFX HQDs while saving and restoring
user queues around the reset.

> diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c 
> b/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c
> index 8ca8e3f8392d3..69bdae805597c 100644
> --- a/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c
> +++ b/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c
> @@ -3868,6 +3868,137 @@ static int gfx_v12_0_wait_for_idle(struct 
> amdgpu_ip_block *ip_block)

[ ... ]

> +static int gfx_v12_0_soft_reset_engine(struct amdgpu_ip_block *ip_block,
> +                                    enum amdgpu_ring_type ring_type, u32 
> inst,
> +                                    struct amdgpu_usermode_queue 
> *guilty_queue)
> +{
> +     struct amdgpu_device *adev = ip_block->adev;
> +     int i, r;
> +
> +     if (ring_type != AMDGPU_RING_TYPE_GFX || inst)
> +             return -EOPNOTSUPP;
> +
> +     mutex_lock(&adev->gfx.mec.reset_mutex);
> +     amdgpu_gfx_off_ctrl(adev, false);
> +
> +     /* A hung gfx stream can keep MES from suspending: reset anyway. */
> +     amdgpu_mes_suspend(adev, 0);
> +     amdgpu_userq_save_hqds(adev, AMDGPU_HW_IP_GFX);
> +
> +     r = gfx_v12_0_soft_reset_gfx_cp(adev);
> +     if (r) {
> +             dev_err(adev->dev, "gfx CP still busy after reset, CP_STAT 
> 0x%08x\n",
> +                     RREG32_SOC15(GC, 0, regCP_STAT));
> +             goto fail;
> +     }
> +
> +     /* The reset cleared the HQD of the kernel gfx queue: map it again. */
> +     r = amdgpu_gfx_disable_kgq(adev, 0);
> +     if (r)
> +             goto fail;
> +     for (i = 0; i < adev->gfx.num_gfx_rings; i++)
> +             amdgpu_gfx_mqd_reset_restore(&adev->gfx.gfx_ring[i]);
> +     r = amdgpu_gfx_enable_kgq(adev, 0);
> +     if (!r)
> +             r = gfx_v12_0_cp_gfx_start(adev);
> +     if (r)
> +             goto fail;
> +
> +     amdgpu_userq_restore_hqds(adev, AMDGPU_HW_IP_GFX, guilty_queue);
> +     r = amdgpu_mes_resume(adev, 0);

[Severity: High]
Can amdgpu_userq_save_hqds() and amdgpu_userq_restore_hqds() race with
concurrent user queue destruction and trigger a use-after-free on queue?

When .soft_reset_engine is set, amdgpu_device_ip_soft_reset() only takes
down_read(&adev->reset_domain->sem) rather than the exclusive write lock,
and gfx_v12_0_soft_reset_engine() only holds adev->gfx.mec.reset_mutex.
Meanwhile, amdgpu_userq_destroy() and amdgpu_userq_detach_doorbell() also
use down_read(&adev->reset_domain->sem) to serialize against resets and
do not take adev->gfx.mec.reset_mutex.  Because xa_for_each() in
amdgpu_userq_save_hqds() and amdgpu_userq_restore_hqds() iterates
adev->userq_doorbell_xa without holding userq_mutex or a queue reference:

CPU0                                  CPU1
amdgpu_device_ip_soft_reset()
  down_read(&reset_domain->sem)
  gfx_v12_0_soft_reset_engine()
    amdgpu_userq_restore_hqds()
      xa_for_each(..., queue)
                                      amdgpu_userq_destroy()
                                        amdgpu_userq_detach_doorbell()
                                          down_read(&reset_domain->sem)
                                          xa_erase_irq(...)
                                          up_read(&reset_domain->sem)
                                        uq_funcs->mqd_destroy(queue)
                                        kfree(queue)
        restore_hqd(...,
          queue->mqd.cpu_ptr, ...)  <-- use-after-free

For the same reason, amdgpu_userq_map_helper() and
amdgpu_userq_unmap_helper() only take down_read(&adev->reset_domain->sem),
so they can run concurrently with gfx_v12_0_soft_reset_engine(), racing
on queue->state and issuing MES map/unmap commands while MES gangs are
suspended.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=9

Reply via email to