From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796383707; b=1GmhjZ0c6tR7tpVZ3CwQabrbQ8gwV5Rau80SM/rToLl4WJN41frUei+Xb6+4mOGNxpn+ PAOIuu9AqxS7x6F54fjJRJwDtC1ivEZYp8q1hbR58vxggwVbrlivoPn7V5EN9y2us/QUR hBosUXXndhau5N+Vf+Zb3vbPAyLA8G9wVS4nZFsJx0raKnxxUpcMyl/7tRH2rcK7oass1 FgtTs6QQ2D2RmW300G3/ffZoe2Bo0dZs/4n+tmQc7RznCTYzRoqw80gVClZsCsoJE/Kig LJe7NAASF0FzLwI62rPCbT8FY1O/22cegl5KeVPQe/PwbgNcVF5W1UWj7Sw7X7d2XtA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383707; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=C9UNnM6eK+KP66er6lq7W/YYHHVTEJKb59PbQAoOzf0=; b=CpTYScNh8VQHTx8wTtR+3pUvUMtaEdYIOl/u6Ha6y5CwJW8i0WXwKlV8+Xb6iNU0Ajfj 4XqBhBl4Ubm/ZhbP+Beqjsr0nuhVIqeXRHRywBkbHfpfY/gHVEaG00Emb0ImCTItOfAOA 4fzZIrqocrwExLLNZEfSyKRZ6t2bwSJ2kBaLY45WjAkvxXe2CMdxz/EHRirt1SXK1qxUQ E7ObC5lvU4b4NYOPWLdFjHOLcjEAECpJi9yCMWezi7xqRjRf/M+aSJxehfOPj1KrCWFJg ZQawYW4bzrsJTOwyJfFhRHKu6SMZbeQXrCHOd4mDmoOw9mFZvrc6h/RVGxIDxj0DSBA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=x4wxCoQc; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 05130606A322; Fri, 4 Dec 2026 11:28:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=x4wxCoQc Received: by smtp.kernel.org (Postfix) with ESMTPSA id 03FF71F000CE; Fri, 4 Dec 2026 11:28:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383706; bh=C9UNnM6eK+KP66er6lq7W/YYHHVTEJKb59PbQAoOzf0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=x4wxCoQck49RiRWl8SExE3KhXXp7Jm3YHRiWC+nemZX2+WppiT4rDz7+ERV4i1CuJ +jenTOsbo8IN9Dtbl5rIC8Y3bKKefcC3GuDJwzpi9MB/1eH37V71xvkbUUD9ChpNWB fcvoDvWUGshJx8Wolw08QUt0JDdKWND4Yesq0IwM= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Qiang Han , Philip Yang , Felix Kuehling , Alex Deucher Subject: [PATCH 6.12 338/555] drm/amdkfd: wait for the queue quiesce in concurrent SVM evictions Date: Fri, 4 Dec 2026 19:06:21 +0900 Message-ID: <20261204100622.819648772@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100603.988357525@linuxfoundation.org> References: <20261204100603.988357525@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Qiang Han commit 8c417209754a9d2cd8dc1e78c0dfadefd0ad0e82 upstream. With XNACK off, svm_range_evict() leaves the GPU page tables in place and relies on the process's queues being stopped before the MMU notifier returns. Only the first eviction (evicted_ranges == 1) calls kgd2kfd_quiesce_mm(). The quiesce preempts every GPU the process has opened and takes milliseconds. Any other range invalidated in the meantime sees evicted_ranges > 1 and returns at once. The core mm then frees, migrates or reuses the page while the queues still run, and an in-flight SDMA or blit copy accesses it through the old GPU PTE. The result is silent corruption of the copy, or of whatever process now owns the page. Hold a new svms->evict_lock across the first evictor's quiesce, so that concurrent evictions return only after the queues have stopped. This is the scheme amdgpu_amdkfd_evict_userptr() uses with process_info->notifier_lock. evict_lock nests inside prange->lock and is taken nowhere else. Under it, the quiesce path takes the dqm lock, which is already used in MMU notifiers (memalloc_noreclaim). One way to reach this: a pageable hipMemcpy() larger than 1 MiB. ROCclr pins the buffer with hsa_amd_memory_lock(), libhsakmt registers it as a KFD SVM range, and host memory pressure (reclaim, compaction, allocator purge) invalidates the range. Fixes: 8a7c184a16d7 ("drm/amdkfd: svm range eviction and restore") Signed-off-by: Qiang Han Signed-off-by: Philip Yang Reviewed-by: Felix Kuehling Signed-off-by: Alex Deucher (cherry picked from commit d443bd2512c04a5d34c4473ce9ce2f2a6f7bf8f5) Cc: stable@vger.kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 + drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 35 ++++++++++++++++++++++------------ 2 files changed, 25 insertions(+), 12 deletions(-) --- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h +++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h @@ -882,6 +882,8 @@ struct svm_range_list { struct list_head deferred_range_list; struct list_head criu_svm_metadata_list; spinlock_t deferred_list_lock; + /* serializes XNACK-off evictions with the queue quiesce */ + struct mutex evict_lock; atomic_t evicted_ranges; atomic_t drain_pagefaults; struct delayed_work restore_work; --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c @@ -2022,21 +2022,30 @@ svm_range_evict(struct svm_range *prange if (prange->start <= last && prange->last >= start) atomic_inc(&prange->invalid); + /* The MMU notifier must not return before the GPU has stopped + * accessing the invalidated pages. Only the first eviction stops + * the queues, which takes milliseconds (HWS preemption on every + * GPU of the process). Serialize all evictions of this process so + * that a concurrent invalidation of another range waits until + * that quiesce has completed instead of returning while queues + * still run on stale PTEs. + */ + mutex_lock(&svms->evict_lock); evicted_ranges = atomic_inc_return(&svms->evicted_ranges); - if (evicted_ranges != 1) - return r; + if (evicted_ranges == 1) { + pr_debug("evicting svms 0x%p range [0x%lx 0x%lx]\n", + prange->svms, prange->start, prange->last); - pr_debug("evicting svms 0x%p range [0x%lx 0x%lx]\n", - prange->svms, prange->start, prange->last); - - /* First eviction, stop the queues */ - r = kgd2kfd_quiesce_mm(mm, KFD_QUEUE_EVICTION_TRIGGER_SVM); - if (r) - pr_debug("failed to quiesce KFD\n"); + /* First eviction, stop the queues */ + r = kgd2kfd_quiesce_mm(mm, KFD_QUEUE_EVICTION_TRIGGER_SVM); + 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)); + 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)); + } + mutex_unlock(&svms->evict_lock); } else { unsigned long s, l; uint32_t trigger; @@ -3332,6 +3341,7 @@ void svm_range_list_fini(struct kfd_proc svm_range_free(prange, true); } + mutex_destroy(&p->svms.evict_lock); mutex_destroy(&p->svms.lock); pr_debug("pasid 0x%x svms 0x%p done\n", p->pasid, &p->svms); @@ -3344,6 +3354,7 @@ int svm_range_list_init(struct kfd_proce svms->objects = RB_ROOT_CACHED; mutex_init(&svms->lock); + mutex_init(&svms->evict_lock); INIT_LIST_HEAD(&svms->list); atomic_set(&svms->evicted_ranges, 0); atomic_set(&svms->drain_pagefaults, 0);