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=1796382327; b=aO4d2YDAbgHY975t/ej9JORrWbGuq2qMC9aGq/rNLCpffs6x2CNMaLDhCaLH1XITxofq olq5K6wU0XqretlEIlGNamI4fdTr2uPcIPgPkc0/AZGF4WvqWiKt7G3UbVfBfmHrtZ85s gL59evvF6/LNxbahgJOJpujYqLt8686J0lRpkw2915cbj3CxZckDTFjHhaXSiD+Vuim4m /Ef/C+7MD5ATNlZfJ1ZBI5kALPNPhIgJFtF9VKjUz1QndMh+aHwbDcTrJw6YdoiEVkQbA ajVPSeS0WcKFddVeKP3EMujjTbmWazazHyAOY1VthHIcm13WJFKWk9+fvBGElqZrvOg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382327; 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=9ZWLmSoxRYskV2bh2Om8TYHPUy3kRqXW6XQ8MmthIbI=; b=F0D023dL5hp/2Z+oUJDjSOGD5EVBHRQEC3ukcppfZROeHjaxC8yuP5Szz3+2oV7XoLgF rDd8Madd91EBEMbYFMQCUmkuFVUmXc/zWdSEjKkx1qb3/lSHt5hGld7PFIdTaYN0qnXaw q7WGV+xw5wdH+X33ytTLJfiQgZrrrTxicU2dXb+sgpz2GoO8X7MJyPF1CVCReS0j2okcq OghSbLAqGIcPZDuz8KodyNL4+AD+Zm1tja8Zsuqe6Ry4pFbzSIk9teU0Rpy1JYwDagRAl BSnkYUl9ac5B8x74Wytfu0S5H0NCNDWrZyn8eyt5iB8xD81fMsf0HTD0bmvFUTSmSSg== 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=tE0nuQ6J; 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 6EE58606A334; Fri, 4 Dec 2026 11:05:27 +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=tE0nuQ6J Received: by smtp.kernel.org (Postfix) with ESMTPSA id BDA801F000C2; Fri, 4 Dec 2026 11:05:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382327; bh=9ZWLmSoxRYskV2bh2Om8TYHPUy3kRqXW6XQ8MmthIbI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=tE0nuQ6JiGYGsfz5zy4/1ZIC7LuGqIHs1sfZA1fkAlJDzsqJgWRyzaEKS60wMAC0E 63pnmQ+SN6c/fm8ev4BKG/mc8mb6bKakNXsy9HRJl5tMU/uY5Dhx6HZqfq9w2QwDz7 0C/rUqM1nhee3AYlBqq2DQw2MwvgcRrZIyVX25Ig= 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.18 365/703] drm/amdkfd: wait for the queue quiesce in concurrent SVM evictions Date: Fri, 4 Dec 2026 19:04:24 +0900 Message-ID: <20261204100637.523077666@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@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.18-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 @@ -887,6 +887,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 @@ -2023,21 +2023,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; @@ -3335,6 +3344,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("process pid %d svms 0x%p done\n", @@ -3348,6 +3358,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);