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: [email protected]
Signed-off-by: Yaohan Li <[email protected]>
---
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

Reply via email to