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() in the mqd_update_info of the
call 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. The properties travel with the call and
not in the queue, because set_perfcount() updates every queue of every
process under the dqm lock alone, and must not apply the properties of
an update that is in flight for one of them.

A queue that is updated while the scheduler is stopped, in the no-HWS
debug mode, now gets -EBUSY. The update is not applied in that state;
it returned success before.

Signed-off-by: Armas Spann <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
---
 .../drm/amd/amdkfd/kfd_device_queue_manager.c | 21 +++++-
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  6 ++
 .../amd/amdkfd/kfd_process_queue_manager.c    | 69 +++++++++++--------
 3 files changed, 68 insertions(+), 28 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 eb75858bd..a75c88356 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
@@ -1411,6 +1411,24 @@ 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. The
+        * new properties travel with the update that carries them, so an
+        * update of another kind (perfcount, debug) cannot apply them.
+        */
+       if (minfo && minfo->new_props) {
+               const struct queue_properties *np = minfo->new_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;
+               minfo->new_props = NULL;
+       }
+
        mqd_mgr->update_mqd(mqd_mgr, q->mqd, &q->properties, minfo);
 
        /*
@@ -1489,7 +1507,8 @@ static int update_queue_locked(struct 
device_queue_manager *dqm, struct queue *q
 
                if (!dqm->sched_running) {
                        WARN_ONCE(1, "Update non-HWS queue while stopped\n");
-                       return retval;
+                       /* Nothing was applied, say so */
+                       return -EBUSY;
                }
 
                retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h 
b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
index 23fc74aa0..081085f3a 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
@@ -581,6 +581,12 @@ struct mqd_update_info {
                } cu_mask;
        };
        enum mqd_update_flag update_flag;
+       /*
+        * New properties from pqm_update_queue_properties(). Applied by
+        * update_queue() under the dqm lock once the queue is unmapped, and
+        * cleared to report that it did.
+        */
+       const struct queue_properties *new_props;
 };
 
 /**
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 e44ccc803..b3705edcc 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c
@@ -654,60 +654,75 @@ 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 mqd_update_info minfo = {0};
+       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. The flag keeps the GWS state of the MQD
+        * as it is, see pqm_set_gws().
+        */
+       minfo.new_props = p;
+       minfo.update_flag = q->gws ? UPDATE_FLAG_IS_GWS : 0;
+       retval = q->device->dqm->ops.update_queue(q->device->dqm, q, &minfo);
+
+       if (minfo.new_props) {
+               /* Not applied. Drop the reference on the new ring. */
+               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.47.3

Reply via email to