mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] drm/amdkfd: unlink destroyed event waiters from the wait queue
@ 2026-11-17 14:54 Yaohan Li
  2026-12-02 19:20 ` Felix Kuehling
  0 siblings, 1 reply; 3+ messages in thread
From: Yaohan Li @ 2026-11-17 14:54 UTC (permalink / raw)
  To: Felix.Kuehling, alexander.deucher, christian.koenig
  Cc: airlied, simona, oded.gabbay, kent.russell, amd-gfx, dri-devel,
	linux-kernel, stable

destroy_event() clears waiter->event and calls wake_up_all() before the
event is removed from the IDR.  The waiters use init_wait(), so a
successful wake-up already unlinks the wait_queue_entry through
autoremove_wake_function().  A waiter that is already runnable, though,
is not woken again and stays linked to ev->wq after destroy_event()
returns.

free_waiters() only calls remove_wait_queue() while waiter->event is
still set.  destroy_event() has already cleared that field, so a waiter
that is still linked there is freed without being unlinked.

ev itself is released with kfree_rcu(), so a reader that reaches it
through RCU -- kfd_set_event() or the GPU signal interrupt path -- can
take ev->lock afterwards and walk ev->wq in set_event(), writing
waiter->activated into memory that has already been kfreed.  The
waitqueue_active() test at the top of set_event() reads the same list.

destroy_event() used to unlink the waiters here.  That unlink was lost
when the code was converted to wait_queue_entry_t by 74e4071665da,
which moved the removal into free_waiters() and left the destroyed
path without one.

Unlink whatever is left on the wait queue after wake_up_all(), while
still holding ev->lock, so the wake-up is delivered first and the
remainder can no longer outlive its storage.

Fixes: 74e4071665da ("drm/amdkfd: Use wait_queue_t to implement event waiting")
Cc: stable@vger.kernel.org
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
Prepared against v7.4-rc1-1969-g666539eecdf4.

AI assistance: the analysis, the change and its review notes were
prepared with Claude (Anthropic) as a coding assistant.  The author
reviewed the reasoning and takes responsibility for the content.

History: fe528c13acc7 unlinked the waiters here.  That unlink was lost
when the code moved to wait_queue_entry_t (74e4071665da).

Testing: compile tested only (x86_64, gcc 11.4).  No AMD GPU was
available, so the race was not run.
---
 drivers/gpu/drm/amd/amdkfd/kfd_events.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_events.c b/drivers/gpu/drm/amd/amdkfd/kfd_events.c
index 6e32a892d56f..adc27f25af4d 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_events.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_events.c
@@ -219,13 +219,25 @@ int kfd_event_init_process(struct kfd_process *p)
 
 static void destroy_event(struct kfd_process *p, struct kfd_event *ev)
 {
-	struct kfd_event_waiter *waiter;
+	struct kfd_event_waiter *waiter, *tmp;
 
 	/* Wake up pending waiters. They will return failure */
 	spin_lock(&ev->lock);
 	list_for_each_entry(waiter, &ev->wq.head, wait.entry)
 		WRITE_ONCE(waiter->event, NULL);
 	wake_up_all(&ev->wq);
+
+	/*
+	 * A wake-up that actually wakes a task detaches its entry through
+	 * autoremove_wake_function(), but a waiter that was already runnable
+	 * stays on the list.  free_waiters() only unlinks a waiter while
+	 * waiter->event is still set, so once that field has been cleared
+	 * above nothing else will detach the remainder.  Leaving them linked
+	 * would let a concurrent set_event(), which reaches ev through RCU,
+	 * walk a list pointing at already freed waiters.
+	 */
+	list_for_each_entry_safe(waiter, tmp, &ev->wq.head, wait.entry)
+		remove_wait_queue(&ev->wq, &waiter->wait);
 	spin_unlock(&ev->lock);
 
 	if (ev->type == KFD_EVENT_TYPE_SIGNAL ||
-- 
2.34.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-03  8:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 14:54 [PATCH] drm/amdkfd: unlink destroyed event waiters from the wait queue Yaohan Li
2026-12-02 19:20 ` Felix Kuehling
2026-12-03  8:52   ` [PATCH v2] drm/amdkfd: mark destroyed events invalid for RCU readers Yaohan Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®