Re: [PATCH] drm/amdkfd: unlink destroyed event waiters from the wait queue

From: Felix Kuehling

Date: Wed Dec 02 2026 - 14:21:01 EST


On 2026-11-17 09:54, Yaohan Li wrote:
[You don't often get email from shunsheng1218@xxxxxxxxx. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

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.

Hi Yaohan,

Thank you for the patch. The problem and explanation is pretty complicated and left me a bit worried that there may be a bigger problem hiding underneath. As I understand it, there is an issue where an event can be looked up from the IDR (partly due to RCU) after the event was torn down. At that time the wait queue is in an inconsistent state where there may be entries in the list that have already been freed.

Your description implies an assumption that the wake_up_all call was meant to clear that list. I'm not convinced that this was ever the intention.

That said, I think your patch fixed the very specific problem. But I'd be more comfortable with a more general solution that would completely avoid touching the internals of the struct kfd_event after it has been torn down and is only being kept alive due to the RCU grace period. I believe a more general solution would be to mark the struct kfd_event as "invalid" in destroy_event and check that state in all RCU reader code paths inside the event's spinlock before touching the internals of the struct.

These code paths already check ev->type through event_can_be_cpu_signaled and event_can_be_gpu_signaled. It would be easy to introduce a new ev->type KFD_EVENT_TYPE_INVALID that wouldn't be exposed through the uAPI. Use that to mark the event as invalid in destroy_event. That would prevent any of the event signaling functions from touching an event that's about to be freed and would also prevent the traversal of the wait queue in that case.

Regards,
  Felix



Fixes: 74e4071665da ("drm/amdkfd: Use wait_queue_t to implement event waiting")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Yaohan Li <shunsheng1218@xxxxxxxxx>
---
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