From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2D0462222A5; Thu, 3 Apr 2025 19:17:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743707862; cv=none; b=WYxmeNaEgLTdgZdVQVdZxBDQzY14HycNrgJ+5TSRhWlrrHiyjeC9iBDqLEsewVpXoNpwUMB4JVIEbO1w6tE5o1P+Cc5RRz+5KWvOD4e++rJGw8bXnLnPuhxiJMhuDTCWj6GPsL/c7rukVYNYhxJqN/GtzdX8nOYUfQVmLSFVsi4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743707862; c=relaxed/simple; bh=V2QfzJ5PfDCaznAZ99tBpVRciwqHabaoEc+N3czYPMI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YFYgWNBtJWrSI57g+BL/yAXlLONJ3HmdNVjrTFQE2wvtmNTtdzW9bZZ8ngBGRQxPHcmULAFW2C3yk9ezezwTgUiq8YYDi1htMG4aXazhiZjS1RJGYQ274w1PgYMAMZmN8IAQ+svwxBJ5rOXLvKBmGbqykEOAQkcH9nvnJbaLFXk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rujuOujt; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rujuOujt" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4E0C8C4CEEB; Thu, 3 Apr 2025 19:17:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1743707861; bh=V2QfzJ5PfDCaznAZ99tBpVRciwqHabaoEc+N3czYPMI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rujuOujtdTNPv7mDOIeYbYHC0qnRKZfIrr9ID/OHcKHO3v1Z+NDhio39W1j6DgGib Cn38rzJ0ryaJsyFdk8RKN6EP+CCTOe5mTc9jWz4pQHbNuSHM2w7gLYy4cIZ5/XCxGU v2AahV/s3vIwD8qEFve6i759h7lo/TfAnFLSj28a8iIfVNndiQCC1nQNac1V3+PZ+K ozhH5PixaVLcpFD4OpMKynKuStGfj7Urot5SM9kXeR2qXE4aiChY41RuvYbibz8UUL StJRpYuvuVprfOZd8oukA4c+oD0mSVF8UwaMOPIF5C9riKnFrdZmMxx114lbvqYYLf GxxccsR/N8nlQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Philip Yang , Lijo Lazar , Felix Kuehling , Alex Deucher , Sasha Levin , Felix.Kuehling@amd.com, christian.koenig@amd.com, airlied@gmail.com, simona@ffwll.ch, amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org Subject: [PATCH AUTOSEL 6.12 16/33] drm/amdkfd: Fix mode1 reset crash issue Date: Thu, 3 Apr 2025 15:16:39 -0400 Message-Id: <20250403191656.2680995-16-sashal@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250403191656.2680995-1-sashal@kernel.org> References: <20250403191656.2680995-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.12.21 Content-Transfer-Encoding: 8bit From: Philip Yang [ Upstream commit f0b4440cdc1807bb6ec3dce0d6de81170803569b ] If HW scheduler hangs and mode1 reset is used to recover GPU, KFD signal user space to abort the processes. After process abort exit, user queues still use the GPU to access system memory before h/w is reset while KFD cleanup worker free system memory and free VRAM. There is use-after-free race bug that KFD allocate and reuse the freed system memory, and user queue write to the same system memory to corrupt the data structure and cause driver crash. To fix this race, KFD cleanup worker terminate user queues, then flush reset_domain wq to wait for any GPU ongoing reset complete, and then free outstanding BOs. Signed-off-by: Philip Yang Reviewed-by: Lijo Lazar Reviewed-by: Felix Kuehling Signed-off-by: Alex Deucher Signed-off-by: Sasha Levin --- drivers/gpu/drm/amd/amdkfd/kfd_process.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c index 264bd764f6f27..0ec8b457494bd 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c @@ -35,6 +35,7 @@ #include #include "amdgpu_amdkfd.h" #include "amdgpu.h" +#include "amdgpu_reset.h" struct mm_struct; @@ -1140,6 +1141,17 @@ static void kfd_process_remove_sysfs(struct kfd_process *p) p->kobj = NULL; } +/* + * If any GPU is ongoing reset, wait for reset complete. + */ +static void kfd_process_wait_gpu_reset_complete(struct kfd_process *p) +{ + int i; + + for (i = 0; i < p->n_pdds; i++) + flush_workqueue(p->pdds[i]->dev->adev->reset_domain->wq); +} + /* No process locking is needed in this function, because the process * is not findable any more. We must assume that no other thread is * using it any more, otherwise we couldn't safely free the process @@ -1154,6 +1166,11 @@ static void kfd_process_wq_release(struct work_struct *work) kfd_process_dequeue_from_all_devices(p); pqm_uninit(&p->pqm); + /* + * If GPU in reset, user queues may still running, wait for reset complete. + */ + kfd_process_wait_gpu_reset_complete(p); + /* 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. -- 2.39.5