Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] drm/amdkfd: race on q->pending_props between update_queue and
  set_perfcount
- [Medium] drm/amdkfd: update_queue_locked() mutates MQD while active
  queue is in MES
- [Medium] drm/amdkfd: update_queue_locked() drops update with 0 return
  on stopped NO_HWS
--

commit 04bc122a04713e23b8e6ce8e16afe2c0ab475963
Author: Armas Spann <[email protected]>

drm/amdkfd: apply queue property updates only after unmap

This commit defers applying new queue properties and swapping the ring
buffer BO reference until update_queue_mqd() runs after unmapping the
queue.

> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c 
> b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> index dbbc6c0794ed3..1c348f36cd3c3 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> @@ -1325,6 +1325,22 @@ static void update_queue_mqd(struct 
> device_queue_manager *dqm, struct queue *q,
>               dqm->mqd_mgrs[get_mqd_type_from_queue_type(q->properties.type)];
>       bool prev_active = q->properties.is_active;
>  
> +     /*
> +      * The queue is off the hardware now. Only from here on may its
> +      * properties change, otherwise a failed unmap would leave the
> +      * scheduler running the old ring under the new description.
> +      */
> +     if (q->pending_props) {
> +             const struct queue_properties *np = q->pending_props;
> +
> +             q->properties.queue_address = np->queue_address;
> +             q->properties.queue_size = np->queue_size;
> +             q->properties.queue_percent = np->queue_percent;
> +             q->properties.priority = np->priority;
> +             q->properties.pm4_target_xcc = np->pm4_target_xcc;
> +             q->pending_props = NULL;
> +     }
> +
>       mqd_mgr->update_mqd(mqd_mgr, q->mqd, &q->properties, minfo);
>  
>       /*

[Severity: Medium]
Can update_queue_mqd() still overwrite q->properties and q->mqd while an
active queue remains in MES?

In update_queue_locked(), the kfd_mes_queue_retired(dqm, q) check only
runs when !prev_active.  When prev_active is true and the scheduler is
halted or stopped (!dqm->sched_running || dqm->sched_halt),
remove_queue_mes() -> remove_queue_mes_on_reset_option() returns 0
without sending REMOVE_QUEUE to MES.  If halt_cpsch() or stop_cpsch()
left the queue unretired in MES, update_queue_locked() proceeds to
update_queue_mqd(), clears q->pending_props, and lets
pqm_update_queue_properties() drop the unmap protection and BO reference
on old_ring_bo while MES still holds the queue.

[ ... ]

> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c 
> b/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c
> index 29827a736f7d4..a7070e87305b0 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c
> @@ -644,60 +644,73 @@ int pqm_update_queue_properties(struct 
> process_queue_manager *pqm, unsigned int qid)
>  int pqm_update_queue_properties(struct process_queue_manager *pqm,
>                               unsigned int qid, struct queue_properties *p)
>  {

[ ... ]

> +     /*
> +      * The dqm applies the new properties once the queue is unmapped. If
> +      * it fails before that, the queue still runs on the old ring and
> +      * keeps its old properties.
> +      */
> +     q->pending_props = p;
> +     retval = q->device->dqm->ops.update_queue(q->device->dqm, q, NULL);
> +
> +     if (q->pending_props) {
> +             /* Not applied. Drop the reference on the new ring. */
> +             q->pending_props = NULL;
> +             old_ring_bo = new_ring_bo;
> +     } else if (new_ring_bo) {
> +             old_ring_bo = q->properties.ring_bo;
> +             q->properties.ring_bo = new_ring_bo;
> +     } else {
> +             old_ring_bo = NULL;
> +     }
>  
> -     retval = pqn->q->device->dqm->ops.update_queue(pqn->q->device->dqm,
> -                                                     pqn->q, NULL);
> -     if (retval != 0)
> -             return retval;
> +     /* There is an old ring to drop only if a new one was looked up */
> +     if (old_ring_bo) {
> +             if (!amdgpu_bo_reserve(vm->root.bo, true)) {
> +                     kfd_queue_unref_bo_va(vm, &old_ring_bo);
> +                     amdgpu_bo_unreserve(vm->root.bo);
> +             }
> +             kfd_queue_buffer_put(&old_ring_bo);
> +     }
>  
> -     return 0;
> +     return retval;
>  }

[Severity: High]
Can set_perfcount() race with pqm_update_queue_properties() on
q->pending_props and cause a stack use-after-return or map a queue with
an unreferenced ring buffer?

kfd_ioctl_update_queue() allocates struct queue_properties on its stack
and calls pqm_update_queue_properties() under p->mutex.  However,
q->pending_props is written before update_queue() acquires dqm_lock(dqm)
and tested/cleared after update_queue() releases dqm_lock(dqm).
Meanwhile, set_perfcount() iterates over all queues under dqm_lock(dqm)
without holding p->mutex and calls update_queue_mqd(), which
unconditionally consumes q->pending_props even when minfo != NULL:

CPU0                                   CPU1
kfd_ioctl_update_queue()
  lock(&p->mutex)
  pqm_update_queue_properties(..., &props)
    q->pending_props = &props
    update_queue()
      dqm_lock(dqm)
      /* fails before update_queue_mqd() */
      dqm_unlock(dqm)
                                       set_perfcount()
                                         dqm_lock(dqm)
                                         update_queue_mqd()
                                           np = q->pending_props
    if (q->pending_props)
      q->pending_props = NULL
      /* drops new_ring_bo ref */
  unlock(&p->mutex)
  return; /* pops props off stack */
                                           q->properties.queue_address =
                                             np->queue_address
                                             <-- stack use-after-return

If CPU1 clears q->pending_props before CPU0 checks it, CPU0 also sees
q->pending_props == NULL after a failed update_queue(), swaps in
new_ring_bo, drops old_ring_bo, and returns the error to userspace.  Or
if set_perfcount() runs after q->pending_props = p and before
update_queue() takes dqm_lock(dqm), set_perfcount() consumes
q->pending_props early; under KFD_SCHED_POLICY_NO_HWS, if that update
activates the queue from another process context, update_queue_locked()
triggers WARN(q->process->mm != current->mm) and skips load_mqd() while
leaving q->properties.is_active true.

Only consuming q->pending_props when update_queue_mqd() is called for a
property update (minfo == NULL), or setting and clearing
q->pending_props strictly under dqm_lock(dqm), would avoid this race.

[Severity: Medium]
Under KFD_SCHED_POLICY_NO_HWS, does updating an active queue while
!dqm->sched_running now silently discard the new properties while
returning 0?

In update_queue_locked(), the NO_HWS branch checks !dqm->sched_running
when prev_active is true and returns retval (which is 0) before calling
update_queue_mqd():

drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c:update_queue_locked() {
        ...
        } else if (prev_active &&
                   (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
                    q->properties.type == KFD_QUEUE_TYPE_SDMA ||
                    q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {

                if (!dqm->sched_running) {
                        WARN_ONCE(1, "Update non-HWS queue while stopped\n");
                        return retval;
                }
        ...
}

Because update_queue_mqd() is not reached, q->pending_props remains
non-NULL, so pqm_update_queue_properties() drops the reference on
new_ring_bo, leaves q->properties unchanged, and returns 0 to userspace.

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

Reply via email to