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 A2AB2606A682 for ; Fri, 4 Dec 2026 13:56:39 +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=W21embUz; dkim=pass (2048-bit key) header.d=simple-co.de header.i=@simple-co.de header.a=rsa-sha256 header.s=201801 header.b=NauAVi4T Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 3D919112F8E; Fri, 4 Dec 2026 13:56:29 +0000 (UTC) Received: from mail.bin-co.de (mail.bin-co.de [37.120.183.211]) by gabe.freedesktop.org (Postfix) with ESMTPS id D01A610EB36 for ; Fri, 4 Dec 2026 12:53:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simple-co.de; s=201801; t=1796388783; bh=bJU0HKeybKk2Awf2yAfkGhby+F8EkW9MmiPr2zzmktU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W21embUz5ik68SB5ucQ8LCeisr140UZmg1pZ/2mDe1W8Eg+qGhlWpR5tlrQRO5C2P zMKfa5oMj0ZXNkMYhngzJLnMbKZu4myDHEOBr2WtCAPNwRuOfnJXHuHTh8kW7sBcXv aF+ijfKKNp/8Wt8YA/hdtUIo18SoAzu5Vzp9HWedbIO0Gj+mzm2EhiQox4/UOxaC/B wVWHQR9QtTHCYIsuRTZwFEoDJU9FFJWEbJz+cov4XJwn64VKCq36x1RMga7Z5F1fk3 J19rkOp7uzl3nX93TfcmFIg4I72jTj9XwPUiwxODt/wEC6MeBCGFu97HC/KJUxSmuD P9ZbBWUPm2lxg== Received: from localhost (localhost [127.0.0.1]) by mail.bin-co.de (Postfix) with ESMTP id 7B15017F36F; 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 9hpeZXsTlWhV; Fri, 4 Dec 2026 13:53:02 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simple-co.de; s=201801; t=1796388782; bh=bJU0HKeybKk2Awf2yAfkGhby+F8EkW9MmiPr2zzmktU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NauAVi4TRWMAX5wYukrtlpODrbBQjr6YGwSKp84blSsOW6B3AXdPUoSrlsyTgbJAA Hd1Yqc7W622HASZmPw3ITuZeioS5AwG6F65Rlgd8Dvp9rOW/Zv14eGCywDBzYBWkSV d1RIBpsBP9iu4+kOsbdlh4C86tw1VEAPcSNEFG1xe410AtQBD6EnZXRmO9bObGcvlg XkYOyfWRd8FrG3v4PpMLfYxpLZVytaGuAikeaJvrXVGCRHxbxMfb7Gh0qhuykwsMdA KEwIZ7Tlp1KldtNG0vTDDwJApdSctVNSIctslV+8cGAU/OmkcnWV6k7yZN8pHQssgp YFibF2nFgNc4g== 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 0BD2B17F369; 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 4/8] drm/amdkfd: add a quarantine for queues MES has not retired Date: Fri, 4 Dec 2026 13:52:46 +0100 Message-ID: <20261204125251.10703-5-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 A queue that is destroyed while MES may still reference it has to keep its MQD, gang context, write pointer mapping, user buffers and its doorbell and SDMA ids. On gfx12 the MQD comes from a first fit sub-allocator and the doorbell id from a first fit bitmap, so both are normally handed to the very next queue the process creates. Add the machinery to park such a queue: a per-device list of zombies and a delayed work that frees a zombie once kfd_mes_queue_retired() is true for it. The usual way out is the MES restart done by the GPU reset that the failed command asked for. start_cpsch() kicks the reaper for that case. While the scheduler is stopped, for a suspend or a GPU reset, the reaper leaves the list alone: nothing can be sent to MES then, and it is not the time to unmap and free buffers. If the removal was skipped and did not fail (scheduler halted or stopped, GPU reset in progress), the reaper sends it once that becomes possible. MES can report a quarantined queue as hung. The per-queue reset then does the MES side for it, the reset and the removal, and leaves the process alone: the queue is not counted any more, and the process is not marked as having a reset queue for one it no longer owns. A zombie uses the pdd, the process context, the doorbell page and the GPU VM of its process, so it takes a hold on the process. The release worker frees those only when the last hold is gone. It must not block for that, because kfd_create_process() flushes the release workqueue and every new KFD process would hang behind it. The worker removes the debugfs and sysfs entries and the context id, which is what a new process with the same pid needs gone, and returns. The reaper queues the work again when it frees the last zombie of the process. The eviction fence is signalled in the first run, where it always was: holding it back protects nothing, because a waiter that enables signaling after the process left the table gets the fence signalled anyway, and it has to be signalled before the context id is free for reuse. The SVM restore work of the process is cancelled in the release worker and the userptr restore work when the last GPU VM goes away, both of which can now happen an unbounded time after the notifier release. Each looks up the mm of the task, which is a new one after an execve, fails on the old one and queues itself again. Stop both in the notifier release with the other works of the process. An eviction that found the process before it left the table is waited for; later ones find no process, and svm_range_evict() does not queue the restore work then, as the userptr path already does not. The SVM side goes through a helper in kfd_svm.c that is a stub without CONFIG_HSA_AMD_SVM, where svms.restore_work is never initialised. The userptr side takes the amdkfd_process_info from a GPU VM of the process, which is valid for as long as the VM is acquired. Device removal must not wait for MES. kgd2kfd_teardown_processes() declares every queue of the device retired and waits for the processes to go, whether the process table still lists any or not: a process that has exited is off the table while its zombies keep it alive. At that point KFD has been suspended on a device that is still there, so the scheduler is stopped and no removal could be sent any more, or the device is gone; MES is shut down right after. The hold is per process, not per device. A process that has exited with a zombie on one GPU therefore also keeps its pdds on every other GPU, and the removal of one of those waits until the zombie is retired, which is not for the departing device to decide. The wait says so after ten seconds. Releasing the pdds device by device would lift this and is left for a follow-up, it means taking the release worker apart. The zombie list is also searched when a per-queue reset looks up a queue by its doorbell. The reset that KGD starts does that lookup without the dqm lock, as it does today for the queue lists of the processes. Nothing is quarantined yet. The next patch converts the paths that free queue storage. Signed-off-by: Armas Spann Co-Authored-By: Claude Fable 5.2 --- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 1 + .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 30 +++ drivers/gpu/drm/amd/amdkfd/kfd_device.c | 36 +++- .../drm/amd/amdkfd/kfd_device_queue_manager.c | 179 +++++++++++++++++- .../drm/amd/amdkfd/kfd_device_queue_manager.h | 14 ++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 25 +++ drivers/gpu/drm/amd/amdkfd/kfd_process.c | 58 ++++++ drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 38 +++- drivers/gpu/drm/amd/amdkfd/kfd_svm.h | 7 + 9 files changed, 378 insertions(+), 10 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h index 74dc307e5..734497081 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h @@ -370,6 +370,7 @@ void amdgpu_amdkfd_ras_pasid_poison_consumption_handler(struct amdgpu_device *ad bool amdgpu_amdkfd_is_fed(struct amdgpu_device *adev); bool amdgpu_amdkfd_bo_mapped_to_dev(void *drm_priv, struct kgd_mem *mem); void amdgpu_amdkfd_block_mmu_notifications(void *p); +void amdgpu_amdkfd_gpuvm_stop_userptr_restore(void *drm_priv); int amdgpu_amdkfd_criu_resume(void *p); int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev, uint64_t size, u32 alloc_flag, int8_t xcp_id); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c index a6616447a..719484220 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c @@ -1644,6 +1644,36 @@ uint64_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *drm_priv) return avm->pd_phys_addr; } +/** + * amdgpu_amdkfd_gpuvm_stop_userptr_restore - stop the userptr restore work + * @drm_priv: drm private of a compute VM of a KFD process that has left the + * process table + * + * The work is cancelled for good when the last VM of the process goes away. + * KFD can keep the VMs long after the process is gone, and until then the + * work would look up the task by pid, find the mm of a new program after an + * execve, fail on the user pages of the old one and queue itself again. + * + * All compute VMs of a process share one amdkfd_process_info, so one call + * with any of them is enough. An eviction that found the process before it + * left the table holds notifier_lock until it has queued the work, so wait + * for that one. A later first eviction gets -ESRCH from the quiesce and does + * not queue it. + */ +void amdgpu_amdkfd_gpuvm_stop_userptr_restore(void *drm_priv) +{ + struct amdgpu_vm *avm = drm_priv_to_vm(drm_priv); + struct amdkfd_process_info *pinfo = avm->process_info; + + if (!pinfo) + return; + + mutex_lock(&pinfo->notifier_lock); + mutex_unlock(&pinfo->notifier_lock); + + cancel_delayed_work_sync(&pinfo->restore_userptr_work); +} + void amdgpu_amdkfd_block_mmu_notifications(void *p) { struct amdkfd_process_info *pinfo = (struct amdkfd_process_info *)p; diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_device.c b/drivers/gpu/drm/amd/amdkfd/kfd_device.c index 2200f5c7c..faf882986 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_device.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device.c @@ -1817,15 +1817,11 @@ void kgd2kfd_teardown_processes(struct amdgpu_device *adev) struct hlist_node *p_temp; struct kfd_process *p; struct kfd_node *dev; + unsigned long warn_at; unsigned int temp; mutex_lock(&kfd_processes_mutex); - if (hash_empty(kfd_processes_table)) { - mutex_unlock(&kfd_processes_mutex); - return; - } - hash_for_each_safe(kfd_processes_table, temp, p_temp, p, kfd_processes) { for (int i = 0; i < p->n_pdds; i++) { dev = p->pdds[i]->dev; @@ -1840,12 +1836,40 @@ void kgd2kfd_teardown_processes(struct amdgpu_device *adev) if (!adev->kfd.dev) return; + /* + * Queues that MES never confirmed as removed keep their process + * alive, and such a process is counted in kfd_processes_count after + * it has left the table. The device is going away, so stop waiting + * for MES, whether the table is empty or not. The caller has + * suspended KFD on a device that is still there, so the queues that + * MES would give up are off the hardware and the scheduler is + * stopped; nothing more could be sent for the others. + */ + if (adev->kfd.dev->init_complete) { + for (unsigned int i = 0; i < adev->kfd.dev->num_nodes; i++) + kfd_dqm_retire_all(adev->kfd.dev->nodes[i]->dqm); + } + /* * Wait for all KFD processes using adev to terminate. Must be * unbounded: device state is freed once they are gone. + * + * A process holds every device until it is freed, and one that has + * exited is not freed while a queue of it is quarantined on another + * device. That queue is not ours to retire, the firmware it belongs + * to keeps running. Say what the wait is for; a GPU reset of that + * device ends it. */ - while (!!atomic_read(&adev->kfd.dev->kfd_processes_count)) + warn_at = jiffies + 10 * HZ; + while (!!atomic_read(&adev->kfd.dev->kfd_processes_count)) { + if (warn_at && time_after(jiffies, warn_at)) { + dev_warn(adev->dev, + "waiting for %d KFD process(es) to release the device, exited ones are kept by queues quarantined on other GPUs until those GPUs are reset\n", + atomic_read(&adev->kfd.dev->kfd_processes_count)); + warn_at = 0; + } cond_resched(); + } } int kgd2kfd_reset_mes_queue(struct kfd_dev *kfd, uint32_t node_id, 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 3767c4465..1e18bc63f 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c @@ -249,6 +249,10 @@ static bool kfd_mes_queue_retired(struct device_queue_manager *dqm, if (q->mes_state == KFD_MES_QUEUE_UNMAPPED) return true; + /* The device is being removed and MES is shut down next */ + if (dqm->mes_retire_all) + goto retired; + /* The firmware instance that knew the queue is gone */ if (q->mes_fw_gen != kfd_mes_gen(dqm)) goto retired; @@ -731,7 +735,13 @@ static int reset_queue_mes(struct device_queue_manager *dqm, struct queue *q, * its storage until MES is restarted. */ r = remove_queue_mes_on_reset_option(dqm, q, &pdd->qpd, true, true); - set_queue_as_reset(dqm, q, &pdd->qpd, true); + /* + * A quarantined queue is destroyed as far as its process is + * concerned: it is not counted, and the process is not punished + * for a queue it no longer owns. Only the MES side was needed. + */ + if (!q->is_zombie) + set_queue_as_reset(dqm, q, &pdd->qpd, true); if (r == -ETIMEDOUT) return r; if (r) @@ -1803,6 +1813,158 @@ static int dqm_repin_mqd_bo(struct device_queue_manager *dqm, struct queue *q) return 0; } +/* + * Quarantine + * + * A queue that is destroyed before MES has confirmed its removal keeps its + * MQD, gang context, wptr mapping, user buffers and its doorbell and SDMA + * ids. It is taken off the lists of its process, so user space sees it + * destroyed, and parked on dqm->zombie_queues. The reaper frees it once + * kfd_mes_queue_retired() says MES has let go, which normally means the + * firmware was restarted by the GPU reset the failed command asked for. + * + * A zombie points at its kfd_process_device. It takes a hold on the + * process (p->mes_hold), so the pdd, the process context, the doorbell + * page and the GPU VM outlive every zombie of the process. + * + * The reaper takes p->mutex and the dqm lock one after the other and never + * nested. It frees buffer objects, so it does that outside the dqm lock. + */ + +/* 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) +{ + struct kfd_process_device *pdd = qpd_to_pdd(qpd); + + dev_warn(dqm->dev->adev->dev, + "MES did not confirm retirement of queue doorbell=0x%x, keeping its storage\n", + q->properties.doorbell_off); + + q->is_zombie = true; + q->zombie_pdd = pdd; + atomic_inc(&pdd->process->mes_hold); + list_add_tail(&q->zombie_node, &dqm->zombie_queues); +} + +/* + * Called by the pqm when it holds no more references to a quarantined + * queue. The queue belongs to the reaper afterwards and the caller must + * not touch it again. + */ +void kfd_dqm_zombie_ready(struct device_queue_manager *dqm, struct queue *q) +{ + dqm_lock(dqm); + q->zombie_ready = true; + dqm_unlock(dqm); + + mod_delayed_work(system_dfl_wq, &dqm->zombie_work, 0); +} + +/* No locks held. @q is off dqm->zombie_queues and retired. */ +static void kfd_dqm_release_zombie(struct device_queue_manager *dqm, + struct queue *q) +{ + struct kfd_process_device *pdd = q->zombie_pdd; + struct kfd_process *p = pdd->process; + struct kfd_node *dev = q->device; + struct mqd_manager *mqd_mgr = + dqm->mqd_mgrs[get_mqd_type_from_queue_type(q->properties.type)]; + + /* Process side. The pdd is alive, the queue has a hold on it. */ + 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); + mutex_unlock(&p->mutex); + + /* Device side */ + if (q->properties.type == KFD_QUEUE_TYPE_SDMA || + q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { + dqm_lock(dqm); + deallocate_sdma_queue(dqm, q); + dqm_unlock(dqm); + } + dqm_repin_mqd_bo(dqm, q); + mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj); + kfd_queue_free_mes_resources(dev, q); + uninit_queue(q); + + /* Last access to the pdd, the process may be freed after this */ + kfd_process_put_mes_hold(p); +} + +static void kfd_dqm_zombie_worker(struct work_struct *work) +{ + struct device_queue_manager *dqm = + container_of(to_delayed_work(work), + struct device_queue_manager, zombie_work); + struct queue *q, *tmp; + LIST_HEAD(retired); + bool more; + + dqm_lock(dqm); + /* + * The scheduler is stopped for a suspend or a GPU reset. Nothing can + * be sent to MES then, and the device is in no state to have buffers + * unmapped and freed. start_cpsch() kicks the reaper when that is + * over. A device that is being removed stays stopped, and its + * zombies have to go now. + */ + if (!dqm->sched_running && !dqm->mes_retire_all) { + dqm_unlock(dqm); + return; + } + + list_for_each_entry_safe(q, tmp, &dqm->zombie_queues, zombie_node) { + if (!q->zombie_ready) + continue; + + /* + * No REMOVE_QUEUE was sent for this queue because the + * scheduler was stopped or halted, or a reset held it off. + * Send it once that is possible. A failure leaves the queue + * UNKNOWN, so this is not repeated. + */ + if (q->mes_state == KFD_MES_QUEUE_MAPPED && + !kfd_mes_queue_retired(dqm, q)) + remove_queue_mes(dqm, q, &q->zombie_pdd->qpd); + + if (kfd_mes_queue_retired(dqm, q)) + list_move_tail(&q->zombie_node, &retired); + } + more = !list_empty(&dqm->zombie_queues); + dqm_unlock(dqm); + + list_for_each_entry_safe(q, tmp, &retired, zombie_node) { + list_del(&q->zombie_node); + kfd_dqm_release_zombie(dqm, q); + } + + if (more) + queue_delayed_work(system_dfl_wq, &dqm->zombie_work, + msecs_to_jiffies(KFD_MES_ZOMBIE_POLL_MS)); +} + +/* + * The device is being removed. KFD has been suspended by then, which took + * every queue off the hardware that MES would let go of and stopped the + * scheduler, or the device is gone altogether. Either way no command can be + * sent for what is left, and MES is shut down right after, so nothing a + * queue holds can be handed to a new owner that MES would then corrupt. + * Treat every queue as retired and release all zombies, so that process + * teardown can finish. + */ +void kfd_dqm_retire_all(struct device_queue_manager *dqm) +{ + dqm_lock(dqm); + dqm->mes_retire_all = true; + dqm_unlock(dqm); + + mod_delayed_work(system_dfl_wq, &dqm->zombie_work, 0); +} + static int evict_process_queues_nocpsch(struct device_queue_manager *dqm, struct qcm_process_device *qpd) { @@ -2672,6 +2834,10 @@ static int start_cpsch(struct device_queue_manager *dqm) hqd_info_size * sizeof(struct amdgpu_mes_hung_queue_hqd_info), GFP_KERNEL); + /* A restart of the MES firmware retires every quarantined queue */ + if (!list_empty(&dqm->zombie_queues)) + mod_delayed_work(system_dfl_wq, &dqm->zombie_work, 0); + dqm_unlock(dqm); return 0; @@ -3051,6 +3217,12 @@ static struct queue *find_queue_by_doorbell_offset(struct device_queue_manager * } } + /* MES may report a quarantined queue as hung, it still has it */ + list_for_each_entry(q, &dqm->zombie_queues, zombie_node) { + if (doorbell_offset == q->properties.doorbell_off) + return q; + } + return NULL; } @@ -3812,6 +3984,9 @@ struct device_queue_manager *device_queue_manager_init(struct kfd_node *dev) if (!dqm) return NULL; + INIT_LIST_HEAD(&dqm->zombie_queues); + INIT_DELAYED_WORK(&dqm->zombie_work, kfd_dqm_zombie_worker); + switch (dev->adev->asic_type) { /* HWS is not available on Hawaii. */ case CHIP_HAWAII: @@ -3950,6 +4125,8 @@ void device_queue_manager_uninit(struct device_queue_manager *dqm) { cancel_delayed_work_sync(&dqm->notify_unmap_work); dqm->ops.stop(dqm); + cancel_delayed_work_sync(&dqm->zombie_work); + WARN_ON(!list_empty(&dqm->zombie_queues)); dqm->ops.uninitialize(dqm); if (!dqm->dev->kfd->shared_resources.enable_mes) deallocate_hiq_sdma_mqd(dqm->dev, &dqm->hiq_sdma_mqd); diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h index 964588d15..aeb807f1d 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h @@ -44,6 +44,9 @@ #define KFD_MES_REMOVE_GRACE_MS 4000 #define KFD_MES_REMOVE_POLL_US 2000 +/* How often the reaper looks at quarantined queues */ +#define KFD_MES_ZOMBIE_POLL_MS 500 + struct device_process_node { struct qcm_process_device *qpd; struct list_head list; @@ -309,6 +312,15 @@ struct device_queue_manager { u32 mes_fence_gen; /* copy of the unanswered ticket, never released through this copy */ struct amdgpu_mes_cmd_ticket mes_fence_ticket; + + /* + * Destroyed queues that MES has not confirmed as removed, and the + * reaper that frees them. The list is protected by the dqm lock. + */ + struct list_head zombie_queues; + struct delayed_work zombie_work; + /* the device is going away, treat every queue as retired */ + bool mes_retire_all; }; void device_queue_manager_init_cik( @@ -358,6 +370,8 @@ bool kfd_dqm_is_queue_in_process(struct device_queue_manager *dqm, void kfd_dqm_set_queues_as_reset(struct device_queue_manager *dqm); int kfd_reset_queue_mes(struct device_queue_manager *dqm, int queue_type, int pipe, int queue, unsigned int db); +void kfd_dqm_zombie_ready(struct device_queue_manager *dqm, struct queue *q); +void kfd_dqm_retire_all(struct device_queue_manager *dqm); size_t mqd_size_from_queue_type(struct device_queue_manager *dqm, enum kfd_queue_type type); diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h index 76cbab619..d9ab92647 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h +++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h @@ -660,6 +660,19 @@ struct queue { /* MES reset the queue in the recovery that reset_queues_mes() runs */ bool mes_reset_seen; + /* + * Quarantine. A queue that is destroyed while MES may still use it + * keeps all of its storage and waits on dqm->zombie_queues. + */ + struct list_head zombie_node; /* dqm lock */ + struct kfd_process_device *zombie_pdd; + /* written once under the dqm lock, stable afterwards */ + bool is_zombie; + /* the pqm is done with the queue, the reaper may free it. dqm lock */ + bool zombie_ready; + /* the dqm took the queue off its lists, the pqm has to finish */ + bool dqm_dequeued; + /* 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. @@ -965,6 +978,17 @@ struct kfd_process { struct kref ref; struct work_struct release_work; + /* + * Holds on the storage of the process: one for the process itself, + * one for each of its queues on a dqm->zombie_queues list. A zombie + * uses the pdd, the process context, the doorbell page and the GPU + * VM, so the release worker frees those only when the last hold is + * gone. + */ + atomic_t mes_hold; + /* the release worker has dropped the hold of the process itself */ + bool mes_hold_dropped; + struct mutex mutex; /* @@ -1452,6 +1476,7 @@ struct process_queue_node { void kfd_process_dequeue_from_device(struct kfd_process_device *pdd); void kfd_process_dequeue_from_all_devices(struct kfd_process *p); +void kfd_process_put_mes_hold(struct kfd_process *p); int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p); void pqm_uninit(struct process_queue_manager *pqm); int pqm_create_queue(struct process_queue_manager *pqm, diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c index 5dc657f32..f1b30d186 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c @@ -1372,6 +1372,10 @@ static void kfd_process_wq_release(struct work_struct *work) release_work); struct dma_fence *ef; + /* Second run, queued when the last quarantined queue was freed */ + if (p->mes_hold_dropped) + goto release_storage; + /* * If GPU in reset, user queues may still running, wait for reset complete. */ @@ -1380,6 +1384,13 @@ static void kfd_process_wq_release(struct work_struct *work) /* Signal the eviction fence after user mode queues are * destroyed. This allows any BOs to be freed without * triggering pointless evictions or waiting for fences. + * + * Not put off for quarantined queues. A waiter that enabled + * signaling while the process was still findable would wait for as + * long as MES keeps a queue, and one that comes later gets the fence + * signalled anyway, because the process is not found any more. It + * also has to happen before the context id can be handed out again: + * the fence is looked up by that id. */ synchronize_rcu(); ef = rcu_access_pointer(p->ef); @@ -1400,6 +1411,26 @@ static void kfd_process_wq_release(struct work_struct *work) */ kfd_process_remove_sysfs(p); + /* + * Queues that MES has not let go of still use the pdds, the process + * context, the doorbell page and the GPU VM. Everything that carries + * the pid is gone at this point, so stop here. The reaper queues + * this work again when it frees the last of those queues. Do not + * wait instead: kfd_create_process() flushes this workqueue. A work + * item never runs twice at the same time, so the second run cannot + * free the process under us. + */ + p->mes_hold_dropped = true; + if (!atomic_dec_and_test(&p->mes_hold)) { + pr_warn("Process pid %d: release waits for MES to retire %d queue(s), a GPU reset retires them\n", + p->lead_thread->pid, atomic_read(&p->mes_hold)); + return; + } + +release_storage: + /* Signalled in the first run. The reference is dropped below. */ + ef = rcu_access_pointer(p->ef); + kfd_process_kunmap_signal_bo(p); kfd_process_free_outstanding_kfd_bos(p); svm_range_list_fini(p); @@ -1416,6 +1447,17 @@ static void kfd_process_wq_release(struct work_struct *work) kfree(p); } +/* + * Drop the hold a quarantined queue had on its process. Called by the dqm + * when the queue has been freed. The last one finishes a release that + * kfd_process_wq_release() had to put off. + */ +void kfd_process_put_mes_hold(struct kfd_process *p) +{ + if (atomic_dec_and_test(&p->mes_hold)) + queue_work(kfd_process_wq, &p->release_work); +} + static void kfd_process_ref_release(struct kref *ref) { struct kfd_process *p = container_of(ref, struct kfd_process, ref); @@ -1463,6 +1505,21 @@ void kfd_process_notifier_release_internal(struct kfd_process *p) kfd_process_table_remove(p); cancel_delayed_work_sync(&p->eviction_work); cancel_delayed_work_sync(&p->restore_work); + /* + * Not left to svm_range_list_fini() and to the destruction of the + * GPU VMs: the release worker may now run long after this, and the + * SVM and userptr restore works look up the mm of the task, which + * can be a new one by then. Both keep queueing themselves as long + * as they fail. + */ + svm_range_list_stop_restore(p); + for (i = 0; i < p->n_pdds; i++) { + /* One call covers the process, its VMs share the work */ + if (p->pdds[i]->drm_priv) { + amdgpu_amdkfd_gpuvm_stop_userptr_restore(p->pdds[i]->drm_priv); + break; + } + } /* * If work pending, cancel it and drop the extra ref */ @@ -1748,6 +1805,7 @@ struct kfd_process *create_process(const struct task_struct *thread, bool primar goto err_alloc_process; kref_init(&process->ref); + atomic_set(&process->mes_hold, 1); mutex_init(&process->mutex); process->mm = thread->mm; process->lead_thread = thread->group_leader; diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c index 9affc79eb..e36bfba0f 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c @@ -2150,9 +2150,19 @@ svm_range_evict(struct svm_range *prange, struct mm_struct *mm, if (r) pr_debug("failed to quiesce KFD\n"); - pr_debug("schedule to restore svm %p ranges\n", svms); - queue_delayed_work(system_freezable_wq, &svms->restore_work, - msecs_to_jiffies(AMDGPU_SVM_RANGE_RESTORE_DELAY_MS)); + /* + * -ESRCH: the process is being released, there are + * no queues to restore, and the restore work has + * been stopped by svm_range_list_stop_restore(). + */ + if (r != -ESRCH) { + unsigned long delay = + msecs_to_jiffies(AMDGPU_SVM_RANGE_RESTORE_DELAY_MS); + + pr_debug("schedule to restore svm %p ranges\n", svms); + queue_delayed_work(system_freezable_wq, + &svms->restore_work, delay); + } } mutex_unlock(&svms->evict_lock); } else { @@ -3410,6 +3420,28 @@ svm_range_switch_xnack_reserve_mem(struct kfd_process *p, bool xnack_enabled) return r; } +/** + * svm_range_list_stop_restore - stop the restore work of a released process + * @p: process that has been removed from the process table + * + * The ranges and their notifiers stay until svm_range_list_fini(), which can + * be much later. The restore work must not run in between: it looks up the + * mm of the lead thread, which is a different one after an execve. + * + * An eviction that found the process before it left the table holds + * evict_lock until it has queued the work, so wait for that one. Every later + * eviction gets -ESRCH from the quiesce and leaves the work alone. + */ +void svm_range_list_stop_restore(struct kfd_process *p) +{ + struct svm_range_list *svms = &p->svms; + + mutex_lock(&svms->evict_lock); + mutex_unlock(&svms->evict_lock); + + cancel_delayed_work_sync(&svms->restore_work); +} + void svm_range_list_fini(struct kfd_process *p) { struct svm_range *prange; diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.h b/drivers/gpu/drm/amd/amdkfd/kfd_svm.h index 38744e716..c229072ef 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.h +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.h @@ -164,6 +164,7 @@ static inline struct svm_range_bo *svm_range_bo_ref(struct svm_range_bo *svm_bo) } int svm_range_list_init(struct kfd_process *p); +void svm_range_list_stop_restore(struct kfd_process *p); void svm_range_list_fini(struct kfd_process *p); int svm_ioctl(struct kfd_process *p, enum kfd_ioctl_svm_op op, uint64_t start, uint64_t size, uint32_t nattrs, @@ -222,6 +223,12 @@ static inline int svm_range_list_init(struct kfd_process *p) { return 0; } + +static inline void svm_range_list_stop_restore(struct kfd_process *p) +{ + /* empty */ +} + static inline void svm_range_list_fini(struct kfd_process *p) { /* empty */ -- 2.47.3