Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] drm/amdgpu/userq: standalone secondary queue ops skip
  set_gang() and VA checks
- [High] drm/amdgpu/userq: out-of-bounds gang context GPU address in
  mes_set_gang()
- [Medium] drm/amdgpu/userq: missing error return in
  amdgpu_userq_restore_helper()
- [Medium] drm/amdgpu/userq: missing set_gang() call in
  amdgpu_userq_post_reset()
--

commit ed0b4bb2f9bf97208327d7b1d43ea3cfbbf1f688
Author: Alex Deucher <[email protected]>

drm/amdgpu/userq: add handling for set_gang

This commit updates the usermode queue preempt, restore, unmap, and map
helpers to operate on a ganged secondary queue and invoke the set_gang
callback when mapping or restoring a gang.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c 
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
> index ed6f3b67e8720..e41f0e10c8a36 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
[ ... ]
> @@ -348,11 +371,27 @@ static int amdgpu_userq_restore_helper(struct 
> amdgpu_usermode_queue *queue)
>               }
>       }
>  
> +     if (secondary_queue &&
> +         secondary_queue->state == AMDGPU_USERQ_STATE_PREEMPTED) {
> +             trace_amdgpu_userq_state_start(secondary_queue);
> +
> +             r = userq_funcs->restore(secondary_queue);
> +             if (r) {
> +                     trace_amdgpu_userq_state_changed(secondary_queue, 
> AMDGPU_USERQ_STATE_HUNG);
> +                     secondary_queue->state = AMDGPU_USERQ_STATE_HUNG;
> +             } else {
> +                     trace_amdgpu_userq_state_changed(secondary_queue, 
> AMDGPU_USERQ_STATE_MAPPED);
> +                     secondary_queue->state = AMDGPU_USERQ_STATE_MAPPED;
> +                     r = userq_funcs->set_gang(queue, secondary_queue);
> +             }
> +     }
> +
>       return r;
>  }

[Severity: Medium]
Should amdgpu_userq_restore_helper() return immediately when restoring the
primary queue fails, as the other helpers do?

Without an early return in the primary queue error branch, a failed
userq_funcs->restore(queue) sets queue->state to AMDGPU_USERQ_STATE_HUNG
and falls through to the secondary_queue block.  Restoring secondary_queue
then overwrites r, calls userq_funcs->set_gang() with a hung primary queue,
and returns 0 to the caller if set_gang() succeeds.

[ ... ]

> @@ -403,7 +466,23 @@ static int amdgpu_userq_map_helper(struct 
> amdgpu_usermode_queue *queue)
>               }
>       }
>  
> -     return 0;
> +     if (secondary_queue &&
> +         secondary_queue->state == AMDGPU_USERQ_STATE_UNMAPPED) {
> +             trace_amdgpu_userq_state_start(secondary_queue);
> +
> +             r = userq_funcs->map(secondary_queue);
> +             if (r) {
> +                     trace_amdgpu_userq_state_changed(secondary_queue, 
> AMDGPU_USERQ_STATE_HUNG);
> +                     secondary_queue->state = AMDGPU_USERQ_STATE_HUNG;
> +                     return r;
> +             } else {
> +                     trace_amdgpu_userq_state_changed(secondary_queue, 
> AMDGPU_USERQ_STATE_MAPPED);
> +                     secondary_queue->state = AMDGPU_USERQ_STATE_MAPPED;
> +                     r = userq_funcs->set_gang(queue, secondary_queue);
> +             }
> +     }
> +
> +     return r;
>  }

[Severity: High]
Does calling userq_funcs->set_gang() here pass out-of-bounds gang context
GPU addresses to MES?

mes_userq_create_ctx_space() allocates queue->fw_obj with only one page
(AMDGPU_USERQ_GANG_CTX_SZ), and mes_userq_map() registers ctx->gpu_addr at
offset 0 as gang_context_addr.  However, mes_set_gang() adds
AMDGPU_USERQ_PROC_CTX_SZ (PAGE_SIZE) to both GPU addresses:

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c:mes_set_gang() {
    ...
        input.primary_gang_context_addr =
                primary_ctx->gpu_addr + AMDGPU_USERQ_PROC_CTX_SZ;
        input.secondary_gang_context_addr =
                secondary_ctx->gpu_addr + AMDGPU_USERQ_PROC_CTX_SZ;
    ...
}

This points one page past the end of each 1-page fw_obj BO and does not
match the gang_context_addr registered with MES.

[Severity: Medium]
Should amdgpu_userq_post_reset() also call userq_funcs->set_gang() when
remapping ganged queues after a GPU reset?

When VRAM is not lost, amdgpu_userq_post_reset() remaps hung queues
directly via userq_funcs->map(queue) without using
amdgpu_userq_map_helper() or calling userq_funcs->set_gang(), leaving
ganged queues unlinked in MES after recovery.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h 
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
> index efaa658a26a9a..3dd63d6b9c445 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
> @@ -102,6 +102,9 @@ struct amdgpu_usermode_queue {
>       } userq_vas;
>  
>       uint32_t        gang_ctx_array_index;
> +     bool                    in_gang;
> +     bool                    gang_primary;
> +     struct amdgpu_usermode_queue *gang_secondary;
>  };

[Severity: High]
What prevents callers iterating uq_mgr->userq_xa or
adev->userq_doorbell_xa from operating on a secondary gang queue directly?

Both queues remain in the XArrays, and neither the helpers nor the
xa_for_each() loops check queue->in_gang && !queue->gang_primary.

If xa_for_each() in amdgpu_userq_restore_all(), amdgpu_userq_resume(), or
amdgpu_userq_start_sched_for_enforce_isolation() visits secondary_queue
before primary_queue, secondary_queue is mapped or restored to
AMDGPU_USERQ_STATE_MAPPED on its own.  When primary_queue is visited next,
secondary_queue->state is no longer UNMAPPED or PREEMPTED, so
userq_funcs->set_gang() is skipped.

Conversely, if primary_queue is visited first in
amdgpu_userq_restore_all(), amdgpu_userq_map_helper(primary_queue) maps
secondary_queue in MES before amdgpu_userq_buffer_vas_mapped() is checked
on secondary_queue:

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c:amdgpu_userq_restore_all() {
    ...
        xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
                if (!amdgpu_userq_buffer_vas_mapped(queue)) {
                        drm_file_err(uq_mgr->file,
                                     "trying restore queue without va 
mapping\n");
                        trace_amdgpu_userq_state_changed(queue, 
AMDGPU_USERQ_STATE_INVALID_VA);
                        queue->state = AMDGPU_USERQ_STATE_INVALID_VA;
                        continue;
                }

                r = amdgpu_userq_map_helper(queue);
    ...
}

If secondary_queue has unmapped VAs, the later loop iteration sets
secondary_queue->state to AMDGPU_USERQ_STATE_INVALID_VA while it remains
mapped in MES, so subsequent unmap and queue destruction skip unmapping it
from hardware before freeing its MQD and context BOs.

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

Reply via email to