AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Armas Spann" <zappel@simple-co.de>, amd-gfx@lists.freedesktop.org
Subject: Re: [PATCH v1 7/9] drm/amdkfd: apply queue property updates only after unmap
Date: Tue, 24 Nov 2026 16:42:35 +0000	[thread overview]
Message-ID: <sashiko-outbox-231581@kernel.org> (raw)
In-Reply-To: <20261124143433.49736-8-zappel@simple-co.de>

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 <zappel@simple-co.de>

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/20261124143433.49736-1-zappel@simple-co.de?part=7

  reply	other threads:[~2026-11-24 16:42 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 14:34 [PATCH v1 0/9] drm/amdgpu/amdkfd: fix MES queue lifecycle on multi-GPU Armas Spann
2026-11-24 14:34 ` [PATCH v1 1/9] drm/amdgpu/mes: distinguish rejected from timed out commands Armas Spann
2026-11-24 16:19   ` sashiko-bot
2026-11-24 14:34 ` [PATCH v1 2/9] drm/amdkfd: factor out freeing of per-queue MES resources Armas Spann
2026-11-24 16:09   ` sashiko-bot
2026-11-24 14:34 ` [PATCH v1 3/9] drm/amdkfd: track whether MES knows each queue Armas Spann
2026-11-24 16:30   ` sashiko-bot
2026-11-24 14:34 ` [PATCH v1 4/9] drm/amdkfd: add a quarantine for queues MES has not retired Armas Spann
2026-11-24 16:29   ` sashiko-bot
2026-11-24 14:34 ` [PATCH v1 5/9] drm/amdkfd: keep queue storage until MES confirmed retirement Armas Spann
2026-11-24 16:29   ` sashiko-bot
2026-11-24 14:34 ` [PATCH v1 6/9] drm/amdkfd: keep eviction and restore consistent when MES fails Armas Spann
2026-11-24 16:30   ` sashiko-bot
2026-11-24 14:34 ` [PATCH v1 7/9] drm/amdkfd: apply queue property updates only after unmap Armas Spann
2026-11-24 16:42   ` sashiko-bot [this message]
2026-11-24 14:34 ` [PATCH v1 8/9] drm/amdkfd: reconcile queue state with MES after a restart Armas Spann
2026-11-24 16:53   ` sashiko-bot
2026-11-24 14:34 ` [PATCH v1 9/9] drm/amdkfd: force CWSR on with more than one MES GPU Armas Spann
2026-11-25 14:15 ` [PATCH v1 0/9] drm/amdgpu/amdkfd: fix MES queue lifecycle on multi-GPU Armas Spann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-231581@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=zappel@simple-co.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox