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 158CB606A685 for ; Fri, 4 Dec 2026 13:56:46 +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=t+fUDx5R; dkim=pass (2048-bit key) header.d=simple-co.de header.i=@simple-co.de header.a=rsa-sha256 header.s=201801 header.b=Z3aw2xV3 Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9E5D2112F9A; Fri, 4 Dec 2026 13:56:35 +0000 (UTC) Received: from mail.bin-co.de (mail.bin-co.de [37.120.183.211]) by gabe.freedesktop.org (Postfix) with ESMTPS id A38A810E9EB 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=1796388784; bh=Ki6Z7TBgj2+pnab/pewGqvyJDWzpteATjEbRB3KiIvE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=t+fUDx5RCrHWWZ9OD/vRtVJ196tKOzV8BJDm+DM1IombsxefcLBLTJ3OwZiMzODs5 nlbY5eXtfwR3Yyfkr17c8o3VvNkvYyRaLEpPMZPO6gXTkPyX4RhBzP/dH4xsNaKpPP WAnWQMbBNjwLDQy+aLTx0s2CkYuWLUeNkJ2xR0R4Me9O/h58wOQxStlmoJw3+dbZka DN0ibHdAo2nX9YSj/Dtsw+pSIRNHhJf3N7VR9IjJUYWffGo+/wo8uvPYB6BYXbQjvx eYmbTc2TE4bd3RoT5yTzwSeYXLfcOXyO0Fi+dCxD01nnbmqOo6+GJ740brUyUq0RR5 TcZDkb1c5hO6g== Received: from localhost (localhost [127.0.0.1]) by mail.bin-co.de (Postfix) with ESMTP id 047F717F374; Fri, 04 Dec 2026 13:53:04 +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 szPHDa79vjK8; 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=Ki6Z7TBgj2+pnab/pewGqvyJDWzpteATjEbRB3KiIvE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z3aw2xV3lGQDPnI/7yO4tnvDq+epE20Nb7sH7LYWyfkJ9+3Elh3PHFjUyPsgr56iW bClMAT73qwcqu5d9fn+aA2jf26pmA9LSaK4n4c6sEK/fue6RyDrp9aHvAa9v7YPgnh arcbE0MW1UpqrIJl8wLA4jes5mxcKiyEZJNTF8cM8agGAc0dX1h6YJMFmcclelCFSD 7vuZcornah4gqd38jOPkfDdLiR5aCNsN3XagI5m3D+Go4W/VSURmRkwS9AAPNLdTKg zBtMAaWk9mxixrdm1UcNhamUdvcOcC7AgLFfepMp2LNFxAcMCa2tHTAE4c/yqmNH7w WuazGGcDeowxw== 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 7BBE617F36C; 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 6/8] drm/amdkfd: keep eviction and restore consistent when MES fails Date: Fri, 4 Dec 2026 13:52:48 +0100 Message-ID: <20261204125251.10703-7-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 evict_process_queues_cpsch() returns at the first REMOVE_QUEUE that fails. The remaining queues are neither removed nor marked inactive and the TLB flush is skipped, while qpd->evicted is already raised. During a GPU reset every REMOVE_QUEUE fails with -EIO, so all queues but the first stay marked active until the reset is over. kfd_process_evict_queues() returns the result of the last device. When an earlier device reported -EIO, the caller is told that everything was saved, signals the eviction fence and schedules a restore for a process that still has a queue on the hardware of that device. restore_process_queues_cpsch() marks a queue active and counts it before ADD_QUEUE is sent. When a later queue fails, qpd->evicted stays set, and the retry counts the queues that were restored already and adds them again. Until the retry, the process is half restored: an eviction that comes first finds qpd->evicted set and leaves the restored queues running while their buffers are moved. Evict every queue and report MES failures as -EIO, which kfd_process_evict_queues() already takes to mean "evicted, do not add back", and let it keep that answer when any device gave it. On a failed restore, take the queues restored in this attempt off the hardware again, so that the process is fully evicted and the retry starts from a known state. A queue is counted before ADD_QUEUE is sent, because the oversubscription check in add_queue_mes() wants to see it, and taken out of the count again when the command fails. The rollback ends with the TLB flush that an eviction ends with: the queues it takes off have been running, and the next eviction finds the process evicted already and does nothing. That leaves the queues whose removal could not be sent, at the eviction or in the rollback. They stay on the hardware while the process counts as evicted, and two places take that for a clean state: - An eviction that finds qpd->evicted set returns 0 without looking. Make it return -EIO as long as MES has a queue of the process, so that a second eviction does not signal the fence the first one held back. - A restore finds such a queue mapped and leaves it as it is, with the page table base from before the eviction. Make it remove the queue first and fail, to be retried, if that is still not possible. One case is left as it is: a queue that the bad queue interrupt path has deactivated and that MES would not give up is skipped by the first eviction of its process, which only looks at active queues. Its storage is safe, the destroy and termination paths ask kfd_mes_queue_retired() whatever is_active says. Signed-off-by: Armas Spann Co-Authored-By: Claude Fable 5.2 --- .../drm/amd/amdkfd/kfd_device_queue_manager.c | 114 ++++++++++++++++-- drivers/gpu/drm/amd/amdkfd/kfd_process.c | 7 +- 2 files changed, 108 insertions(+), 13 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 9c40a7cfd..468e22d18 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c @@ -2017,6 +2017,23 @@ static int evict_process_queues_nocpsch(struct device_queue_manager *dqm, return ret; } +/* + * dqm lock held. True if MES still has a queue of a process that counts as + * evicted: a removal could not be sent, or was not confirmed. + */ +static bool kfd_mes_has_queue_of(struct device_queue_manager *dqm, + struct qcm_process_device *qpd) +{ + struct queue *q; + + list_for_each_entry(q, &qpd->queues_list, list) { + if (!kfd_mes_queue_retired(dqm, q)) + return true; + } + + return false; +} + static int evict_process_queues_cpsch(struct device_queue_manager *dqm, struct qcm_process_device *qpd) { @@ -2026,8 +2043,18 @@ static int evict_process_queues_cpsch(struct device_queue_manager *dqm, int retval = 0; dqm_lock(dqm); - if (qpd->evicted++ > 0) /* already evicted, do nothing */ + if (qpd->evicted++ > 0) { /* already evicted, do nothing */ + /* + * Nothing to do is not the same as nothing left running. If + * the eviction that came first, or the rollback of a failed + * restore, could not get a queue off the hardware, this + * caller must not be told that its memory is safe to move + * either. + */ + if (kfd_mes_has_queue_of(dqm, qpd)) + retval = -EIO; goto out; + } pdd = qpd_to_pdd(qpd); @@ -2056,11 +2083,24 @@ static int evict_process_queues_cpsch(struct device_queue_manager *dqm, decrement_queue_count(dqm, qpd, q); if (dqm->dev->kfd->shared_resources.enable_mes) { - retval = remove_queue_mes(dqm, q, qpd); - if (retval) { - dev_err(dev, "Failed to evict queue %d\n", - q->properties.queue_id); - goto out; + int r = remove_queue_mes(dqm, q, qpd); + + if (r) { + /* + * -EIO means a GPU reset is under way or MES + * is fenced off, both reported already. + */ + if (r != -EIO) + dev_err(dev, "Failed to evict queue %d\n", + q->properties.queue_id); + /* + * Keep going so that every queue ends up in + * the evicted state. The caller treats -EIO + * as evicted, not to be added back. MES may + * still use this MQD, so it stays pinned. + */ + retval = -EIO; + continue; } } @@ -2270,25 +2310,48 @@ static int restore_process_queues_cpsch(struct device_queue_manager *dqm, /* activate all active queues on the qpd */ list_for_each_entry(q, &qpd->queues_list, list) { q->properties.is_evicted = false; + + /* + * The eviction, or the rollback of an earlier restore, could + * not take this queue off the hardware (-EIO). MES still has + * it, with the page table base it was added with, and + * add_queue_mes() would leave it at that. Remove it first, + * now that MES can be talked to again. A removal that was + * sent and failed is not repeated; add_queue_mes() refuses + * such a queue below. + */ + if (q->mes_state == KFD_MES_QUEUE_MAPPED && + !kfd_mes_queue_retired(dqm, q)) { + retval = remove_queue_mes(dqm, q, qpd); + if (retval) { + dev_err(dev, "Failed to evict queue %d before its restore\n", + q->properties.queue_id); + goto rollback; + } + } + if (!QUEUE_IS_ACTIVE(q->properties)) continue; - q->properties.is_active = true; - increment_queue_count(dqm, &pdd->qpd, q); - retval = dqm_repin_mqd_bo(dqm, q); if (retval) { dev_err(dev, "Failed to repin MQD for queue %d\n", q->properties.queue_id); - goto out; + goto rollback; } + /* Counted first: add_queue_mes() looks at the active count */ + q->properties.is_active = true; + increment_queue_count(dqm, &pdd->qpd, q); + if (dqm->dev->kfd->shared_resources.enable_mes) { retval = add_queue_mes(dqm, q, qpd); if (retval) { dev_err(dev, "Failed to restore queue %d\n", q->properties.queue_id); - goto out; + q->properties.is_active = false; + decrement_queue_count(dqm, &pdd->qpd, q); + goto rollback; } } } @@ -2302,6 +2365,35 @@ static int restore_process_queues_cpsch(struct device_queue_manager *dqm, out: dqm_unlock(dqm); return retval; + +rollback: + /* + * A half restored process is a problem for the next eviction: it + * finds qpd->evicted set and leaves the queues restored so far + * running. Take those off the hardware again, so that the process + * is fully evicted and the retry starts from a known state. A + * removal that cannot be sent (-EIO) leaves the queue mapped and + * marked evicted, as evict_process_queues_cpsch() does. + */ + list_for_each_entry(q, &qpd->queues_list, list) { + q->properties.is_evicted = true; + if (!q->properties.is_active) + continue; + + q->properties.is_active = false; + decrement_queue_count(dqm, qpd, q); + if (dqm->dev->kfd->shared_resources.enable_mes) + remove_queue_mes(dqm, q, qpd); + } + + /* + * As at the end of an eviction: the queues that were taken off + * above have been running, MES does not flush the TLB for them, and + * the next eviction finds qpd->evicted set and does nothing. + */ + if (dqm->dev->kfd->shared_resources.enable_mes) + kfd_flush_tlb(pdd); + goto out; } static int register_process(struct device_queue_manager *dqm, diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c index f1b30d186..d2e349ae5 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c @@ -2256,7 +2256,7 @@ struct kfd_process *kfd_lookup_process_by_id(const struct mm_struct *mm, u16 id) */ int kfd_process_evict_queues(struct kfd_process *p, uint32_t trigger) { - int r = 0; + int r = 0, ret = 0; int i; unsigned int n_evicted = 0; @@ -2277,12 +2277,15 @@ int kfd_process_evict_queues(struct kfd_process *p, uint32_t trigger) dev_err(dev, "Failed to evict process queues\n"); goto fail; } + /* One device that could not save its queues is enough */ + if (r) + ret = r; n_evicted++; pdd->dev->dqm->is_hws_hang = false; } - return r; + return ret; fail: /* To keep state consistent, roll back partial eviction by -- 2.47.3