Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 011BF606A68F for ; Fri, 4 Dec 2026 13:56:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=simple-co.de header.i=@simple-co.de header.a=rsa-sha256 header.s=201801 header.b=XqJjGByl; dkim=pass (2048-bit key) header.d=simple-co.de header.i=@simple-co.de header.a=rsa-sha256 header.s=201801 header.b=WNqlmEow Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 83C19112F96; Fri, 4 Dec 2026 13:56:33 +0000 (UTC) Received: from mail.bin-co.de (mail.bin-co.de [37.120.183.211]) by gabe.freedesktop.org (Postfix) with ESMTPS id 9663510E7F2 for ; Fri, 4 Dec 2026 12:53:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simple-co.de; s=201801; t=1796388783; bh=mWDzG+8nzuTvvyEqCL+ugXqcim08CJLwQfFm1RViJ4s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XqJjGBylzophZphd9j28fRvYMGRWNfMIQBYVJ9J6rtW7x5zS3fh62TFZ9u3cqxgis UWqfJoNa+AFSfvBHgmO+rdvcAKql9LWbK6+EKUL37qdvu+FTrSuZDn2hj+EAHlzh/L ijGj7a6jJwSjWlopaALzrwcihWQgNCEOEQV2CaooSCGVAFploh1Q6JBO0bVX+7HXYQ s5a9Ur0mKwTwWO2tFjxKiqjE/pIQD/bSqQ+zGtCR7VUmWbMdyW1FBNLUWkMq0Kfq1a ZJ4hjtHoI8BpdiZe1piZ7eE0Qgbz/GRiSqpFlHG38b7jofIuXO0sH0gAkAAFKpApl6 cL0GgjeqCJkBw== Received: from localhost (localhost [127.0.0.1]) by mail.bin-co.de (Postfix) with ESMTP id A3B5217F369; Fri, 04 Dec 2026 13:53:03 +0100 (CET) X-Virus-Scanned: Debian amavis at v2202603343726440230.luckysrv.de Received: from mail.bin-co.de ([127.0.0.1]) by localhost (mail.bin-co.de [127.0.0.1]) (amavis, port 10024) with ESMTP id vKufeJS3LmK3; Fri, 4 Dec 2026 13:53:03 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simple-co.de; s=201801; t=1796388782; bh=mWDzG+8nzuTvvyEqCL+ugXqcim08CJLwQfFm1RViJ4s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WNqlmEoweOjeIfX3C+KU4WjDoOK21l39lXuVF2cL5BWMbSbOZ7TKve7c3GXBRBfuS NOlMZmTgQP1di50x5pzQ2/JTIkxKzfSVT8q4HVKofs/sVmRnNyWsTKGzYr1aFcM4v3 D1rzEb133XkWpen3UgMoHoUSJ8XNGIBjeW73XfEsAOQjGMfcd7PjQUlbt72ZxG8PLQ +K40AEk0gkiU1ePggEW6ND9tayP5HDID4Xy33Pw4FXwk1XYGrtIJcVn5rTT2FIHoQK uSdRD38UledAvFX/6WnudeZy8zR61FaVSD3eECTLZ+Uny7BP3hX7eYo2r8JGE+WEr7 aqqNEh48P9w4g== Received: from deepblue.zlan.intra (wan.zappel.net [IPv6:2a02:8106:4:c300::1]) (Authenticated sender: zappel@simple-co.de) by mail.bin-co.de (Postfix) with ESMTPSA id 42D7617F374; Fri, 04 Dec 2026 13:53:02 +0100 (CET) From: Armas Spann To: amd-gfx@lists.freedesktop.org Cc: alexander.deucher@amd.com, christian.koenig@amd.com, Felix.Kuehling@amd.com, Armas Spann Subject: [PATCH 5/8] drm/amdkfd: keep queue storage until MES confirmed retirement Date: Fri, 4 Dec 2026 13:52:47 +0100 Message-ID: <20261204125251.10703-6-zappel@simple-co.de> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204125251.10703-1-zappel@simple-co.de> References: <20261204125251.10703-1-zappel@simple-co.de> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailman-Approved-At: Fri, 04 Dec 2026 13:56:23 +0000 X-BeenThere: amd-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for AMD gfx List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "amd-gfx" X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 KFD frees and reuses the storage of a queue without requiring that MES confirmed the removal of the queue: - destroy_queue_cpsch() gives the doorbell id back before REMOVE_QUEUE is sent, and frees the MQD whatever the command returned. - pqm_destroy_queue() goes on to free the gang context, the write pointer mapping and the user buffer references on -ETIME and -EIO. On any other error it bails out and keeps the pqm node of a queue that the dqm has already unlinked and whose MQD is freed. A second destroy of that queue id corrupts the list and frees the MQD twice. - The error path of queue creation frees the MQD, the ids, the gang context and the write pointer mapping when ADD_QUEUE failed, although a command that timed out can still be executed. - process_termination_cpsch() logs REMOVE_QUEUE failures and frees everything. A REMOVE_QUEUE that completes late writes the queue state back into an MQD that has been given to, and zeroed for, the next queue. An ADD_QUEUE that is executed late maps a queue whose MQD and gang context are gone. Make all four paths ask kfd_mes_queue_retired() and hand the queue to the quarantine when the answer is no. Ids are released after the unmap and no longer before it. The pqm completes a destroy whenever the dqm has unlinked the queue. The user buffers of a queue go with it. pqm_destroy_queue() drops the unmap protection of the ring, pointers, EOP and CWSR buffers before it asks the dqm to destroy the queue. When the dqm refuses, for a queue of a process that is being debugged, or for a second destroy while the first one sleeps on a queue the debugger has suspended, the queue stays as it is, but its buffers can now be unmapped under it. Drop the references once, after the dqm has dequeued the queue. A quarantined queue keeps them until the reaper frees it. A quarantined queue from a failed create takes references of its own on the buffers, because the ioctl drops its set on the error path while the MQD keeps pointing at the buffers. If that fails, the queue remembers that it holds no reference on an SVM range for its CWSR area, and the reaper does not drop one: it would be the reference of another queue on the same range. process_termination_cpsch() takes the process off dqm->queues and then frees the MQDs one by one, dropping the dqm lock for each. A queue that MES has not let go of is moved to the quarantine before the lock is dropped for the first time, so that a per-queue reset that runs in between finds it on one list or the other. For user space a destroy that the dqm carried out now always removes the queue id; the value it returns is the one the dqm returned, as before. Two defects on the same paths are fixed along the way. Process termination and the cleanup of a failed create clear is_active when they deactivate a queue, so a per-queue reset later does not decrement the active count a second time, and kfd_dqm_suspend_bad_queue_mes() decrements only if the reset it asked for has not done so already. What the quarantine keeps is the storage of the queue: allocated, pinned where it was pinned, and referenced. It does not stop TTM from moving the user buffers of a process that has exited, the eviction fence is signalled by then. On devices without MES kfd_mes_queue_retired() is always true. What changes there is that ids and buffer references are released after the unmap, and that a destroy which failed after the queue was unlinked is completed. Signed-off-by: Armas Spann Co-Authored-By: Claude Fable 5.2 --- .../drm/amd/amdkfd/kfd_device_queue_manager.c | 124 ++++++++++++++---- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 4 + .../amd/amdkfd/kfd_process_queue_manager.c | 95 ++++++++++---- drivers/gpu/drm/amd/amdkfd/kfd_queue.c | 78 ++++++++++- 4 files changed, 248 insertions(+), 53 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 1e18bc63f..9c40a7cfd 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c @@ -1396,6 +1396,7 @@ static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm, qpd->reset_wavefronts = true; list_del(&q->list); + q->dqm_dequeued = true; if (list_empty(&qpd->queues_list)) { if (qpd->reset_wavefronts) { pr_warn("Resetting wave fronts (nocpsch) on dev %p\n", @@ -1832,9 +1833,9 @@ static int dqm_repin_mqd_bo(struct device_queue_manager *dqm, struct queue *q) */ /* dqm lock held. @q is off qpd->queues_list already. */ -static void __maybe_unused dqm_quarantine_queue(struct device_queue_manager *dqm, - struct qcm_process_device *qpd, - struct queue *q) +static void dqm_quarantine_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, + struct queue *q) { struct kfd_process_device *pdd = qpd_to_pdd(qpd); @@ -1876,7 +1877,8 @@ static void kfd_dqm_release_zombie(struct device_queue_manager *dqm, mutex_lock(&p->mutex); deallocate_doorbell(&pdd->qpd, q); kfd_queue_unref_bo_vas(pdd, &q->properties); - kfd_queue_release_buffers(pdd, &q->properties, false); + /* The last argument decides whether an SVM reference is dropped */ + kfd_queue_release_buffers(pdd, &q->properties, q->zombie_no_svm_ref); mutex_unlock(&p->mutex); /* Device side */ @@ -3033,8 +3035,21 @@ static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q, cleanup_queue: qpd->queue_count--; list_del(&q->list); - if (q->properties.is_active) + if (q->properties.is_active) { decrement_queue_count(dqm, qpd, q); + /* Not counted any more, a reset must not count it down again */ + q->properties.is_active = false; + } + if (!kfd_mes_queue_retired(dqm, q)) { + /* + * ADD_QUEUE was not confirmed, MES may still map the queue. + * The MQD and the doorbell and SDMA ids stay with it, the pqm + * takes care of the user buffers. + */ + dqm_quarantine_queue(dqm, qpd, q); + dqm_unlock(dqm); + return retval; + } mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj); dqm_unlock(dqm); out_deallocate_doorbell: @@ -3546,6 +3561,7 @@ static int destroy_queue_cpsch(struct device_queue_manager *dqm, uint64_t sdma_val = 0; struct kfd_process_device *pdd = qpd_to_pdd(qpd); struct device *dev = dqm->dev->adev->dev; + bool retired; /* Get the SDMA queue stats */ if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) || @@ -3588,13 +3604,9 @@ static int destroy_queue_cpsch(struct device_queue_manager *dqm, mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( q->properties.type)]; - deallocate_doorbell(qpd, q); - if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) || - (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) { - deallocate_sdma_queue(dqm, q); + (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) pdd->sdma_past_activity_counter += sdma_val; - } if (q->properties.is_active) { decrement_queue_count(dqm, qpd, q); @@ -3605,11 +3617,18 @@ static int destroy_queue_cpsch(struct device_queue_manager *dqm, USE_DEFAULT_GRACE_PERIOD); if (retval == -ETIME) qpd->reset_wavefronts = true; - } else { - retval = remove_queue_mes(dqm, q, qpd); } } + + /* + * Not only for active queues: an evicted queue can be inactive with + * a REMOVE_QUEUE that MES never confirmed. + */ + if (!kfd_mes_queue_retired(dqm, q)) + retval = remove_queue_mes(dqm, q, qpd); + list_del(&q->list); + q->dqm_dequeued = true; qpd->queue_count--; /* @@ -3620,6 +3639,21 @@ static int destroy_queue_cpsch(struct device_queue_manager *dqm, pr_debug("Total of %d queues are accountable so far\n", dqm->total_queue_count); + /* + * MES identifies the queue by its doorbell. The doorbell and SDMA + * ids can be handed out again only when MES has forgotten the queue. + * Until then the queue keeps them, together with its MQD. + */ + retired = kfd_mes_queue_retired(dqm, q); + if (retired) { + deallocate_doorbell(qpd, q); + if (q->properties.type == KFD_QUEUE_TYPE_SDMA || + q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) + deallocate_sdma_queue(dqm, q); + } else { + dqm_quarantine_queue(dqm, qpd, q); + } + dqm_unlock(dqm); /* @@ -3630,9 +3664,11 @@ static int destroy_queue_cpsch(struct device_queue_manager *dqm, qpd->pqm->process, q->device, -1, false, NULL, 0); - /* Repin the MQD BO if still evicted for hibernation, before it is freed. */ - dqm_repin_mqd_bo(dqm, q); - mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj); + if (retired) { + /* Repin the MQD BO if still evicted for hibernation, before it is freed. */ + dqm_repin_mqd_bo(dqm, q); + mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj); + } return retval; @@ -3816,7 +3852,7 @@ static int process_termination_cpsch(struct device_queue_manager *dqm, struct qcm_process_device *qpd) { int retval = 0; - struct queue *q; + struct queue *q, *q_next; struct device *dev = dqm->dev->adev->dev; struct kernel_queue *kq, *kq_next; struct mqd_manager *mqd_mgr; @@ -3838,20 +3874,18 @@ static int process_termination_cpsch(struct device_queue_manager *dqm, /* Clear all user mode queues */ list_for_each_entry(q, &qpd->queues_list, list) { - if (q->properties.type == KFD_QUEUE_TYPE_SDMA) - deallocate_sdma_queue(dqm, q); - else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) - deallocate_sdma_queue(dqm, q); - if (q->properties.is_active) { decrement_queue_count(dqm, qpd, q); + /* set_queue_as_reset() must not count it down again */ + q->properties.is_active = false; + } - if (dqm->dev->kfd->shared_resources.enable_mes) { - retval = remove_queue_mes(dqm, q, qpd); - if (retval) - dev_err(dev, "Failed to remove queue %d\n", - q->properties.queue_id); - } + /* Inactive queues can have an unconfirmed REMOVE_QUEUE too */ + if (!kfd_mes_queue_retired(dqm, q)) { + retval = remove_queue_mes(dqm, q, qpd); + if (retval) + dev_err(dev, "Failed to remove queue %d\n", + q->properties.queue_id); } dqm->total_queue_count--; @@ -3879,6 +3913,25 @@ static int process_termination_cpsch(struct device_queue_manager *dqm, up_read(&dqm->dev->adev->reset_domain->sem); } + /* + * Quarantine what MES has not let go of. All of it now, before the + * lock is dropped for the first time below: the process is off + * dqm->queues, so from here on the zombie list is the only place + * where a per-queue reset finds a queue that MES reports as hung. + * Whether MES has let go is decided once per queue. What is left on + * the list afterwards is retired and stays that way, and its SDMA id + * goes with its MQD. + */ + list_for_each_entry_safe(q, q_next, &qpd->queues_list, list) { + if (kfd_mes_queue_retired(dqm, q)) + continue; + + list_del(&q->list); + q->dqm_dequeued = true; + qpd->queue_count--; + dqm_quarantine_queue(dqm, qpd, q); + } + /* Lastly, free mqd resources. * Do free_mqd() after dqm_unlock to avoid circular locking. */ @@ -3887,7 +3940,13 @@ static int process_termination_cpsch(struct device_queue_manager *dqm, mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type( q->properties.type)]; list_del(&q->list); + q->dqm_dequeued = true; qpd->queue_count--; + + if (q->properties.type == KFD_QUEUE_TYPE_SDMA || + q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) + deallocate_sdma_queue(dqm, q); + dqm_unlock(dqm); /* Repin the MQD BO if still evicted for hibernation, before free. */ dqm_repin_mqd_bo(dqm, q); @@ -4172,8 +4231,15 @@ int kfd_dqm_suspend_bad_queue_mes(struct kfd_node *knode, u32 pasid, u32 doorbel kfd_hws_hang(dqm); } q->properties.is_evicted = true; - q->properties.is_active = false; - decrement_queue_count(dqm, qpd, q); + /* + * When MES reported the queue itself as hung, + * the per-queue reset has deactivated it and + * counted it down already. + */ + if (q->properties.is_active) { + q->properties.is_active = false; + decrement_queue_count(dqm, qpd, q); + } break; } } diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h index d9ab92647..f8ab1bde8 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h +++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h @@ -672,6 +672,8 @@ struct queue { bool zombie_ready; /* the dqm took the queue off its lists, the pqm has to finish */ bool dqm_dequeued; + /* no SVM reference on the CWSR area to drop. Set before zombie_ready */ + bool zombie_no_svm_ref; /* 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 @@ -1440,6 +1442,8 @@ int kfd_queue_release_buffers(struct kfd_process_device *pdd, struct queue_prope void kfd_queue_unref_bo_va(struct amdgpu_vm *vm, struct amdgpu_bo **bo); int kfd_queue_unref_bo_vas(struct kfd_process_device *pdd, struct queue_properties *properties); +int kfd_queue_hold_buffers(struct kfd_process_device *pdd, + struct queue_properties *properties); void kfd_queue_ctx_save_restore_size(struct kfd_topology_device *dev); int kfd_queue_buffer_svm_get(struct kfd_process_device *pdd, u64 addr, u64 size); 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 3c2aea628..b43b9f0eb 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c @@ -238,19 +238,31 @@ void pqm_uninit(struct process_queue_manager *pqm) list_for_each_entry_safe(pqn, next, &pqm->queues, process_queue_list) { if (pqn->q) { - struct kfd_process_device *pdd = kfd_get_process_device_data(pqn->q->device, - pqm->process); - if (pdd) { - kfd_queue_unref_bo_vas(pdd, &pqn->q->properties); - kfd_queue_release_buffers(pdd, &pqn->q->properties, false); + struct queue *q = pqn->q; + struct kfd_process_device *pdd = + kfd_get_process_device_data(q->device, + pqm->process); + + kfd_procfs_del_queue(q); + if (q->is_zombie) { + /* + * The dqm has quarantined the queue. All of + * its storage, the user buffers included, + * stays until MES has let go of it. + */ + kfd_dqm_zombie_ready(q->device->dqm, q); } else { - WARN_ON(!pdd); + if (pdd) { + kfd_queue_unref_bo_vas(pdd, &q->properties); + kfd_queue_release_buffers(pdd, &q->properties, false); + } else { + WARN_ON(!pdd); + } + pqm_clean_queue_resource(pqm, pqn); + uninit_queue(q); } - pqm_clean_queue_resource(pqm, pqn); } - kfd_procfs_del_queue(pqn->q); - uninit_queue(pqn->q); list_del(&pqn->process_queue_list); kfree(pqn); } @@ -502,9 +514,25 @@ int pqm_create_queue(struct process_queue_manager *pqm, return retval; err_create_queue: - if (q) - kfd_queue_free_mes_resources(dev, q); - uninit_queue(q); + if (q && q->is_zombie) { + /* + * The dqm still needs the MQD, the ids and what MES uses. The + * user buffers are part of that: MES may yet execute the + * ADD_QUEUE and run the queue from them. The ioctl drops its + * references on them when this returns, so take our own. The + * reaper drops them once MES has let go of the queue. + */ + if (kfd_queue_hold_buffers(pdd, &q->properties)) { + q->zombie_no_svm_ref = true; + pr_warn("process pid %d: quarantined queue could not hold all of its user buffers\n", + pqm->process->lead_thread->pid); + } + kfd_dqm_zombie_ready(dev->dqm, q); + } else { + if (q) + kfd_queue_free_mes_resources(dev, q); + uninit_queue(q); + } if (kq) kernel_queue_uninit(kq); kfree(pqn); @@ -557,23 +585,44 @@ int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid) } if (pqn->q) { - retval = kfd_queue_unref_bo_vas(pdd, &pqn->q->properties); - if (retval) - goto err_destroy_queue; + struct queue *q = pqn->q; - dqm = pqn->q->device->dqm; - retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q); + dqm = q->device->dqm; + retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, q); if (retval) { pr_err("Pasid 0x%x destroy queue %d failed, ret %d\n", pdd->pasid, - pqn->q->properties.queue_id, retval); - if (retval != -ETIME && retval != -EIO) + q->properties.queue_id, retval); + /* The dqm left the queue alone, it stays usable */ + if (!q->dqm_dequeued) goto err_destroy_queue; } - kfd_procfs_del_queue(pqn->q); - kfd_queue_release_buffers(pdd, &pqn->q->properties, false); - pqm_clean_queue_resource(pqm, pqn); - uninit_queue(pqn->q); + + /* + * The dqm has taken the queue off its lists. Whatever it + * returned, the destroy has to be completed from here on. + */ + kfd_procfs_del_queue(q); + if (q->is_zombie) { + /* What pqm_clean_queue_resource() does for GWS */ + if (q->gws) + pdd->qpd.num_gws = 0; + /* + * MES may still use the buffers. They keep their + * references, the reaper drops them once MES has let + * go. The queue belongs to the reaper after this call. + */ + kfd_dqm_zombie_ready(dqm, q); + } else { + /* + * The queue is off the hardware. Only now may its + * buffers be unmapped and freed. + */ + kfd_queue_unref_bo_vas(pdd, &q->properties); + kfd_queue_release_buffers(pdd, &q->properties, false); + pqm_clean_queue_resource(pqm, pqn); + uninit_queue(q); + } } list_del(&pqn->process_queue_list); diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_queue.c b/drivers/gpu/drm/amd/amdkfd/kfd_queue.c index 2b9044bf1..9b0817adc 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_queue.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_queue.c @@ -409,7 +409,8 @@ int kfd_queue_unref_bo_vas(struct kfd_process_device *pdd, int err; vm = drm_priv_to_vm(pdd->drm_priv); - err = amdgpu_bo_reserve(vm->root.bo, false); + /* Called once the queue is gone, so this cannot be interrupted */ + err = amdgpu_bo_reserve(vm->root.bo, true); if (err) return err; @@ -423,6 +424,81 @@ int kfd_queue_unref_bo_vas(struct kfd_process_device *pdd, return 0; } +static void kfd_queue_hold_bo(struct amdgpu_vm *vm, struct amdgpu_bo *bo) +{ + struct amdgpu_bo_va *bo_va; + + if (!bo) + return; + + amdgpu_bo_ref(bo); + bo_va = amdgpu_vm_bo_find(vm, bo); + if (bo_va) + bo_va->queue_refcount++; +} + +/* + * Take references of our own on the user buffers of a queue whose + * creation failed but that MES may still map: the ioctl drops its set + * on the error path, while the MQD keeps pointing at the buffers. What + * kfd_queue_acquire_buffers() takes, for a queue whose buffers are known + * already. Undone by kfd_queue_unref_bo_vas() and + * kfd_queue_release_buffers(). + * + * On failure no reference is held on an SVM range for the CWSR area, and + * the release must not drop one: it would be the reference of another + * queue that uses the range. + */ +int kfd_queue_hold_buffers(struct kfd_process_device *pdd, + struct queue_properties *properties) +{ + struct amdgpu_vm *vm = drm_priv_to_vm(pdd->drm_priv); + struct kfd_topology_device *topo_dev; + u64 total_cwsr_size; + int err; + + err = amdgpu_bo_reserve(vm->root.bo, true); + if (err) { + /* + * Nothing was taken. The pointers are copies of the ones + * the caller of the create owns, forget them so that the + * release has nothing to drop. + */ + properties->wptr_bo = NULL; + properties->rptr_bo = NULL; + properties->ring_bo = NULL; + properties->eop_buf_bo = NULL; + properties->cwsr_bo = NULL; + return err; + } + + kfd_queue_hold_bo(vm, properties->wptr_bo); + kfd_queue_hold_bo(vm, properties->rptr_bo); + kfd_queue_hold_bo(vm, properties->ring_bo); + kfd_queue_hold_bo(vm, properties->eop_buf_bo); + kfd_queue_hold_bo(vm, properties->cwsr_bo); + + amdgpu_bo_unreserve(vm->root.bo); + + /* The CWSR area of a compute queue is a BO, or an SVM range */ + if (properties->type != KFD_QUEUE_TYPE_COMPUTE || properties->cwsr_bo) + return 0; + + topo_dev = kfd_topology_device_by_id(pdd->dev->id); + if (!topo_dev) + return -EINVAL; + total_cwsr_size = (u64)properties->ctx_save_restore_area_size + + topo_dev->node_props.debug_memory_size; + if (check_mul_overflow(total_cwsr_size, + NUM_XCC(pdd->dev->xcc_mask), + &total_cwsr_size)) + return -EINVAL; + total_cwsr_size = ALIGN(total_cwsr_size, PAGE_SIZE); + + return kfd_queue_buffer_svm_get(pdd, properties->ctx_save_restore_area_address, + total_cwsr_size); +} + #define DEBUGGER_BYTES_ALIGN 64 #define DEBUGGER_BYTES_PER_WAVE 32 -- 2.47.3