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=1796476397; b=CPZ/yIyOk6wsFD2e+XgmoRBxZu7fVqIzQAVTGqtcxBkFwEXp7hIQnpc0mXwyfYM15+55 kw6qAL02lJzBJloFcsiZ+ptCd97NMI5xlw4DiZnKMWwk3736oxjux+G6xGoGdcK7RBjXv kdCz3CVhZGmr+kOCtfBKPrRbQoAUkmFhY+6D5q4HJ5m3kAYaUOFp+UxsWnDFUgcKrN8gK kls5CFehi31R6NtJliGtgLf+lLNicjhOcZEdRyW8xy7ko23p4AMFpoaqeC26jBcfk8YGo IZHWJn8eJkysFxlDqBvXm0HWvUl3fWaCYhn+MJEimxmSv8xm0sQjnZ78J6MrM/BaYrA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796476397; h=Received:DKIM-Signature:Received:Received:X-ME-Sender: X-ME-Proxy-Cause:X-ME-Proxy:Feedback-ID:Received:X-Mailer: MIME-Version:X-ThreadId:Date:From:To:Cc:Message-Id:In-Reply-To: References:Subject:Content-Type:Content-Transfer-Encoding; bh=LXPfI1Q272rtoi7yKEE1dz6V8Yicp0sAfWfj5aH0snY=; b=VzcXgUxvYjAqvkoaXQ9oEHuOJQl0l0F7+k3svb/P19L+FvI1JbSwcAbO3MOolcew95VW tQmWEw41nND0WJY/E/2WnSP1iD4snlgeNd7VW5ea0V/2mfeSEO9xwV7EWy4tYDokjGWRI hnFkfp1P3dZ/XcIgqVFsz7+g9XQdwUw+hGmCvqOlb/aeqRGGD0laDHqVcYkSAiuQn2Es7 O4QIzRoKUqsX21PXPr87X38H/H6fZaaYGTHIgfp4t8dNqpe8rl5sOrkteCCPEMe0hziPb lhVYDxCxBXHzoRtuUkSk8dspCZFUGWCEHXgJ33dlGCjOyTW+PfkP4v+r0GA1sfOxAWQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=guFhN6Ak; 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 639BE606A32F; Sat, 5 Dec 2026 13:13:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=guFhN6Ak Received: by smtp.kernel.org (Postfix) with ESMTPSA id CB3D51F00A07; Sat, 5 Dec 2026 13:13:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796476397; bh=LXPfI1Q272rtoi7yKEE1dz6V8Yicp0sAfWfj5aH0snY=; h=Date:From:To:Cc:In-Reply-To:References:Subject; b=guFhN6AkmjZqCSvK/b5fVbGr2SC585B4hwfdkFtyFcy4u1BKsR5cq6vaHVGzp7cER L2sG1A0GNHORQ6wZN5gRVXvB0XnD0x5j65G5mBSVMWP+5J9f5IQHCELJKM6RY4dEEm rRCCc5MssJc57ZEmCSa13Q1Jij1DxoG2J5J7HVK/RNmE1GRlZvRUNir94Nkpmcs4wf psGXyVFs/okAURJbEp0iQ4XFy65o0FPdJb7jR9rnv2yGSK12Jg+sAOEXAIaP2lvaOc IAnDvOfzYJoBZ3/A+HjS0bHKiBu8RG3+6tDaM6ug6/NKPuHOwqhBeerAKreb0qzlHl 6oADddgW0vCdQ== Received: from stl-compute-04.internal (stl-compute-04.internal [10.204.2.64]) by mailfauth.stl.internal (Postfix) with ESMTP id 54DE6780043; Sat, 5 Dec 2026 08:13:16 -0500 (EST) Received: from stl-imap-13 ([10.204.2.104]) by stl-compute-04.internal (MEProxy); Sat, 05 Dec 2026 08:13:16 -0500 X-ME-Sender: X-ME-Proxy-Cause: dmFkZTEsd1M+GZ5rDBTwEIZ8rQm416jL/+bHMYH94OxzkFdUw5DQTQL/lV3FMCnsg8zSrD VE8MGU6p6U91FXN8LPPIYOq918fdeweFb4HPEm3r2kKktcBZp6vlajoQKME2qNA3Rb+evH KtSDd1rfB5A6GCmcKZq/d57+OEsHoxIz0dhWzk1hSrxvIJ61K6ZSPE1P6TUimm3PEnRYr1 mwnhBUs977s4Ec9wBHLjHzQaJpDCSLslld7nqJMNNshc4LLMkND9un038QEbIR5SWbhq2U iw0dqBsfhsTl2KkE2lrnYYOvk+w0OKS4M0EN9jhrT2xhTWLOXqY1TgYjuSQG9tlcEzNrDS AGvpAF1xvNFSanEr71zTuZUzxRJd9UDW8scckCEV4hI5OpmFAtINqVQNcqxHNKIXVbZpbZ pjd8EO6nd4Z3iOrnV2yNABjMQ5WAdYdDPZ3o9AnqkQR0HbvX8T4GzyEwDrhlT4/vS9Fp/S Nzr71lpE83gAs3I3m1dCcsUc/m/Sb27E7C+japn3vYEkh2gudMa5mJfqFRfKXJ41l34dTy n+F2Ncm5ykB5OGWdMK46kqDP/xMHIaghzLFlt+nLayJha/rdOJMvPOh7IGD5I5M9vAFWbZ t5WCWR3gfgAE+rk+YMyqTldf4n4/B9odE+AVaHwDiBISwM0IlBSMY3/wq5Bg X-ME-Proxy: Feedback-ID: iccae4b50:Fastmail Received: by mailuser.stl.internal (Postfix, from userid 501) id 1B44DF60088; Sat, 5 Dec 2026 08:13:16 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-ThreadId: AuvvtD4j5FdZ Date: Sat, 05 Dec 2026 08:12:55 -0500 From: "Chris Mason" To: brauner@kernel.org, oleg@redhat.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Felix Kuehling" , amd-gfx@lists.freedesktop.org, akpm@linux-foundation.org, peterz@infradead.org, mingo@redhat.com, kees@kernel.org, viro@zeniv.linux.org.uk, jack@suse.cz, tj@kernel.org, hannes@cmpxchg.org, mkoutny@suse.com, bsingharora@gmail.com, eddyz87@gmail.com, memxor@gmail.com, yonghong.song@linux.dev, martin.lau@linux.dev, song@kernel.org, jolsa@kernel.org, emil@etsalapatis.com, ihor.solodrai@linux.dev, sched-ext@lists.linux.dev, cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, mason@kernel.org Message-Id: <20261204233428.14861-7-mason@kernel.org> In-Reply-To: <20261204233428.14861-1-mason@kernel.org> References: <20261204233428.14861-1-mason@kernel.org> Subject: [PATCH v2 6/8] drm/amdkfd: fix crashes if a debugged process exits Content-Type: text/plain Content-Transfer-Encoding: 7bit kfd_create_process() hits a NULL dereference or a use-after-free if a process exits during a KFD_IOC_DBG_TRAP_ENABLE for it. It takes a reference on thread->mm, but it and create_process() read the pointer again: kfd_create_process(thread) thread exits ========================== ============ mmget_not_zero(thread->mm) exit_mm() /* thread->mm = NULL */ mmput(thread->mm) /* NULL dereference */ create_process() takes a reference on the group leader without RCU or a pid_alive() check: create_process(thread) process exits ====================== ============= leader = thread->group_leader release_task(leader) /* freed after a grace period */ get_task_struct(leader) /* use-after-free */ The fix used here is to remember the mm, and to take the leader's reference under RCU if the thread is still alive. Fixes: 0ab2d7532b05 ("drm/amdkfd: prepare per-process debug enable and disable") Signed-off-by: Chris Mason --- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 2 +- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 3 ++- drivers/gpu/drm/amd/amdkfd/kfd_process.c | 33 +++++++++++++++--------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c index 3984b2c3bf45d..a46a4e498ac51 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c @@ -3363,7 +3363,7 @@ static int kfd_ioctl_create_process(struct file *filep, struct kfd_process *p, v return -EINVAL; } - process = create_process(current, false); + process = create_process(current, current->mm, false); if (IS_ERR(process)) { mutex_unlock(&kfd_processes_mutex); return PTR_ERR(process); diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h index 70d67f9d8cd75..e70d47ec8617d 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h +++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h @@ -1113,7 +1113,8 @@ struct amdkfd_ioctl_desc { }; bool kfd_dev_is_large_bar(struct kfd_node *dev); -struct kfd_process *create_process(const struct task_struct *thread, bool primary); +struct kfd_process *create_process(const struct task_struct *thread, + struct mm_struct *mm, bool primary); int kfd_process_create_wq(void); void kfd_process_destroy_wq(void); void kfd_cleanup_processes(void); diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c index 336c66d9c5a73..458637a81f66c 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c @@ -66,8 +66,6 @@ static struct workqueue_struct *kfd_process_wq; */ static struct workqueue_struct *kfd_restore_wq; -static struct kfd_process *find_process(const struct task_struct *thread, - bool ref); static void kfd_process_ref_release(struct kref *ref); static void evict_process_worker(struct work_struct *work); @@ -966,9 +964,11 @@ static void kfd_process_free_id(struct kfd_process *process) struct kfd_process *kfd_create_process(struct task_struct *thread) { struct kfd_process *process; + struct mm_struct *mm; int ret; - if (!(thread->mm && mmget_not_zero(thread->mm))) + mm = get_task_mm(thread); + if (!mm) return ERR_PTR(-EINVAL); /* If the process just called exec(3), it is possible that the @@ -999,13 +999,13 @@ struct kfd_process *kfd_create_process(struct task_struct *thread) } /* A prior open of /dev/kfd could have already created the process. - * find_process will increase process kref in this case + * kfd_lookup_process_by_mm will increase process kref in this case */ - process = find_process(thread, true); + process = kfd_lookup_process_by_mm(mm); if (process) { pr_debug("Process already found\n"); } else { - process = create_process(thread, true); + process = create_process(thread, mm, true); if (IS_ERR(process)) goto out; @@ -1025,7 +1025,7 @@ struct kfd_process *kfd_create_process(struct task_struct *thread) } out: mutex_unlock(&kfd_processes_mutex); - mmput(thread->mm); + mmput(mm); return process; } @@ -1680,7 +1680,8 @@ void kfd_process_set_trap_debug_flag(struct qcm_process_device *qpd, * On return the kfd_process is fully operational and will be freed when the * mm is released */ -struct kfd_process *create_process(const struct task_struct *thread, bool primary) +struct kfd_process *create_process(const struct task_struct *thread, + struct mm_struct *mm, bool primary) { struct kfd_process *process; struct mmu_notifier *mn; @@ -1692,13 +1693,20 @@ struct kfd_process *create_process(const struct task_struct *thread, bool primar kref_init(&process->ref); mutex_init(&process->mutex); - process->mm = thread->mm; - process->lead_thread = thread->group_leader; + process->mm = mm; /* * Take the reference on the lead thread before the process can be * released through kfd_process_wq_release(), which drops it again. + * A process that's being debugged can exit meanwhile. */ - get_task_struct(process->lead_thread); + rcu_read_lock(); + if (pid_alive(thread)) + process->lead_thread = get_task_struct(thread->group_leader); + rcu_read_unlock(); + if (!process->lead_thread) { + err = -ESRCH; + goto err_lead_thread; + } process->n_pdds = 0; process->queues_paused = false; @@ -1816,8 +1824,9 @@ struct kfd_process *create_process(const struct task_struct *thread, bool primar err_process_pqm_init: kfd_event_free_process(process); err_event_init: - mutex_destroy(&process->mutex); put_task_struct(process->lead_thread); +err_lead_thread: + mutex_destroy(&process->mutex); kfree(process); err_alloc_process: return ERR_PTR(err);