pqm_update_queue_properties() drops the references on the old ring buffer and overwrites the queue properties before update_queue() takes the queue off the hardware. If the unmap fails, the scheduler keeps running the old ring, which is no longer protected against being unmapped, while the driver describes the new one.
Hand the new properties to update_queue() and let it apply them at the point where the queue is unmapped, under the dqm lock. Swap the ring buffer reference afterwards, and only if the properties were applied. Signed-off-by: Armas Spann <[email protected]> Co-Authored-By: Claude Fable 5.2 <[email protected]> --- .../drm/amd/amdkfd/kfd_device_queue_manager.c | 16 +++++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 7 ++ .../amd/amdkfd/kfd_process_queue_manager.c | 67 +++++++++++-------- 3 files changed, 63 insertions(+), 27 deletions(-) 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 feb3062e6..b93870421 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); /* diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h index 13e9e3ed8..e2110e65e 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h +++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h @@ -673,6 +673,13 @@ struct queue { /* the dqm took the queue off its lists, the pqm has to finish */ bool dqm_dequeued; + /* + * New properties from pqm_update_queue_properties(). Set under + * p->mutex, applied and cleared by update_queue() under the dqm lock + * once the queue is unmapped. + */ + const struct queue_properties *pending_props; + /* The VRAM-resident MQD BO (mqd_on_vram()) is unpinned at S4 suspend so * TTM evicts it into the hibernation image, and repinned on resume. Set * while the BO is unpinned so the resume path knows to repin it. 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 29827a736..a7070e873 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_destroy_queue(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) { - int retval; + struct amdgpu_bo *new_ring_bo = NULL, *old_ring_bo; struct process_queue_node *pqn; + struct amdgpu_vm *vm = NULL; + struct queue *q; + int retval; pqn = get_queue_by_qid(pqm, qid); if (!pqn || !pqn->q) { pr_debug("No queue %d exists for update operation\n", qid); return -EFAULT; } + q = pqn->q; /* * Update with NULL ring address is used to disable the queue */ if (p->queue_address && p->queue_size) { + u64 size = p->queue_size + q->properties.metadata_queue_size; struct kfd_process_device *pdd; - struct amdgpu_vm *vm; - struct queue *q = pqn->q; - int err; pdd = kfd_get_process_device_data(q->device, q->process); if (!pdd) return -ENODEV; vm = drm_priv_to_vm(pdd->drm_priv); - err = amdgpu_bo_reserve(vm->root.bo, false); - if (err) - return err; + retval = amdgpu_bo_reserve(vm->root.bo, false); + if (retval) + return retval; - if (kfd_queue_buffer_get(vm, (void *)p->queue_address, &p->ring_bo, - p->queue_size + - pqn->q->properties.metadata_queue_size)) { + retval = kfd_queue_buffer_get(vm, (void *)p->queue_address, + &new_ring_bo, size); + amdgpu_bo_unreserve(vm->root.bo); + if (retval) { pr_debug("ring buf 0x%llx size 0x%llx not mapped on GPU\n", p->queue_address, p->queue_size); - amdgpu_bo_unreserve(vm->root.bo); return -EFAULT; } - - kfd_queue_unref_bo_va(vm, &pqn->q->properties.ring_bo); - kfd_queue_buffer_put(&pqn->q->properties.ring_bo); - amdgpu_bo_unreserve(vm->root.bo); - - pqn->q->properties.ring_bo = p->ring_bo; } - pqn->q->properties.queue_address = p->queue_address; - pqn->q->properties.queue_size = p->queue_size; - pqn->q->properties.queue_percent = p->queue_percent; - pqn->q->properties.priority = p->priority; - pqn->q->properties.pm4_target_xcc = p->pm4_target_xcc; + /* + * 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; } int pqm_update_mqd(struct process_queue_manager *pqm, -- 2.56.1
