* [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
* Re: [PATCH] drm/amdkfd: unlink destroyed event waiters from the wait queue
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
0 siblings, 1 reply; 3+ messages in thread
From: Felix Kuehling @ 2026-12-02 19:20 UTC (permalink / raw)
To: Yaohan Li, alexander.deucher, christian.koenig
Cc: airlied, simona, oded.gabbay, kent.russell, amd-gfx, dri-devel,
linux-kernel, stable
On 2026-11-17 09:54, Yaohan Li wrote:
> [You don't often get email from shunsheng1218@gmail.com. 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@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
* [PATCH v2] drm/amdkfd: mark destroyed events invalid for RCU readers
2026-12-02 19:20 ` Felix Kuehling
@ 2026-12-03 8:52 ` Yaohan Li
0 siblings, 0 replies; 3+ messages in thread
From: Yaohan Li @ 2026-12-03 8:52 UTC (permalink / raw)
To: Felix Kuehling, Alex Deucher, Christian Koenig
Cc: David Airlie, Simona Vetter, Philip Yang, Oded Gabbay,
Kent Russell, amd-gfx, dri-devel, linux-kernel
destroy_event() wakes waiters on ev->wq, drops ev->lock, removes ev from
p->event_idr, and schedules ev for RCU freeing. If a waiter is already
runnable when wake_up_all() runs, autoremove_wake_function() leaves it on
ev->wq, and free_waiters() later skips remove_wait_queue() because
waiter->event was cleared to NULL.
That became reachable once commit 5273e82c5f47 ("drm/amdkfd: Improve
concurrency of event handling") switched event signaling from
p->event_mutex to RCU lookup plus ev->lock, and commit 34d292d57973
("drm/amdkfd: Asynchronously free events") replaced synchronize_rcu() in
destroy_event() with kfree_rcu(). During that RCU grace period, a reader
that looked up ev before idr_remove() can still enter set_event() and
walk freed waiters on ev->wq. While kfd_set_event() and
kfd_reset_event() check ev->type under ev->lock,
set_event_from_interrupt() checks event_can_be_gpu_signaled() and
acknowledges the interrupt slot outside ev->lock, and the non-signal RCU
event paths only check ev->type before taking ev->lock.
Introduce an internal KFD_EVENT_TYPE_INVALID state (rejected by
kfd_event_create()) and set ev->type to KFD_EVENT_TYPE_INVALID under
ev->lock in destroy_event(), keeping the previous type for
p->signal_event_count accounting. Re-check ev->type inside ev->lock
across the RCU signaling paths so torn-down events are ignored before
acknowledging signals, updating exception data, clearing send_signal, or
calling set_event().
Fixes: 5273e82c5f47 ("drm/amdkfd: Improve concurrency of event handling")
Fixes: 34d292d57973 ("drm/amdkfd: Asynchronously free events")
Suggested-by: Felix Kuehling <felix.kuehling@amd.com>
Cc: stable@vger.kernel.org
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
Changes in v2:
- Implement Felix Kuehling's suggested KFD_EVENT_TYPE_INVALID approach under
ev->lock instead of unlinking wait-queue entries in destroy_event().
- Save the original ev->type before invalidation so signal_event_count is
still decremented for SIGNAL and DEBUG events, and reject
KFD_EVENT_TYPE_INVALID in kfd_event_create().
- Move the type check and acknowledge_signal() inside ev->lock in
set_event_from_interrupt(), and re-check ev->type under ev->lock across
lookup_events_by_type_and_signal(), kfd_signal_vm_fault_event(),
kfd_signal_reset_event(), kfd_signal_poison_consumed_event(), and
kfd_signal_process_terminate_event().
- Update Fixes: from 74e4071665da to 5273e82c5f47 and 34d292d57973.
- v1: https://lore.kernel.org/r/20261117145406.1092636-1-shunsheng1218@gmail.com/
I don't have AMD GPU hardware available, so I tested this in QEMU (x86_64,
KASAN + PROVE_LOCKING) using a test module that links the real kfd_events.c
with stubbed KFD process/GPUVM helpers and simulated
kfd_signal_event_interrupt() calls. Unmodified kfd_events.c hits KASAN
slab-use-after-free in set_event(); with this patch applied, all 8 RCU
signaling paths, reused-ID slot/waitqueue checks, SIGTERM handling, and
signal_event_count checks pass with 0 KASAN or lockdep warnings. Also
build-tested amdkfd with W=1 on x86_64 and arm64 (0 warnings).
AI assistance: Claude and Gemini helped check the RCU signaling paths, put
together the QEMU test harness, and draft the v2 changelog. I reviewed the
locking, event accounting, test results, and final patch myself.
drivers/gpu/drm/amd/amdkfd/kfd_events.c | 87 ++++++++++++++++---------
drivers/gpu/drm/amd/amdkfd/kfd_events.h | 3 +
2 files changed, 58 insertions(+), 32 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_events.c b/drivers/gpu/drm/amd/amdkfd/kfd_events.c
index 6e32a892d56f0..a0a24db5c526b 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_events.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_events.c
@@ -220,16 +220,19 @@ 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;
+ int type;
/* Wake up pending waiters. They will return failure */
spin_lock(&ev->lock);
+ type = ev->type;
+ WRITE_ONCE(ev->type, KFD_EVENT_TYPE_INVALID);
list_for_each_entry(waiter, &ev->wq.head, wait.entry)
WRITE_ONCE(waiter->event, NULL);
wake_up_all(&ev->wq);
spin_unlock(&ev->lock);
- if (ev->type == KFD_EVENT_TYPE_SIGNAL ||
- ev->type == KFD_EVENT_TYPE_DEBUG)
+ if (type == KFD_EVENT_TYPE_SIGNAL ||
+ type == KFD_EVENT_TYPE_DEBUG)
p->signal_event_count--;
idr_remove(&p->event_idr, ev->event_id);
@@ -339,8 +342,12 @@ int kfd_event_create(struct file *devkfd, struct kfd_process *p,
uint64_t *event_page_offset, uint32_t *event_slot_index)
{
int ret = 0;
- struct kfd_event *ev = kzalloc_obj(*ev);
+ struct kfd_event *ev;
+
+ if ((int)event_type == KFD_EVENT_TYPE_INVALID)
+ return -EINVAL;
+ ev = kzalloc_obj(*ev);
if (!ev)
return -ENOMEM;
@@ -671,12 +678,15 @@ static void acknowledge_signal(struct kfd_process *p, struct kfd_event *ev)
static void set_event_from_interrupt(struct kfd_process *p,
struct kfd_event *ev)
{
- if (ev && event_can_be_gpu_signaled(ev)) {
+ if (!ev)
+ return;
+
+ spin_lock(&ev->lock);
+ if (event_can_be_gpu_signaled(ev)) {
acknowledge_signal(p, ev);
- spin_lock(&ev->lock);
set_event(ev);
- spin_unlock(&ev->lock);
}
+ spin_unlock(&ev->lock);
}
void kfd_signal_event_interrupt(u32 pasid, uint32_t partial_id,
@@ -1033,15 +1043,17 @@ static void lookup_events_by_type_and_signal(struct kfd_process *p,
id = KFD_FIRST_NONSIGNAL_EVENT_ID;
idr_for_each_entry_continue(&p->event_idr, ev, id)
- if (ev->type == type) {
- send_signal = false;
- dev_dbg(kfd_device,
+ if (READ_ONCE(ev->type) == type) {
+ spin_lock(&ev->lock);
+ if (ev->type == type) {
+ send_signal = false;
+ dev_dbg(kfd_device,
"Event found: id %X type %d",
ev->event_id, ev->type);
- spin_lock(&ev->lock);
- set_event(ev);
- if (ev->type == KFD_EVENT_TYPE_MEMORY && ev_data)
- ev->memory_exception_data = *ev_data;
+ set_event(ev);
+ if (ev->type == KFD_EVENT_TYPE_MEMORY && ev_data)
+ ev->memory_exception_data = *ev_data;
+ }
spin_unlock(&ev->lock);
}
@@ -1145,11 +1157,13 @@ void kfd_signal_vm_fault_event(struct kfd_process_device *pdd,
id = KFD_FIRST_NONSIGNAL_EVENT_ID;
idr_for_each_entry_continue(&p->event_idr, ev, id)
- if (ev->type == KFD_EVENT_TYPE_MEMORY) {
+ if (READ_ONCE(ev->type) == KFD_EVENT_TYPE_MEMORY) {
spin_lock(&ev->lock);
- ev->memory_exception_data = data ? *data :
+ if (ev->type == KFD_EVENT_TYPE_MEMORY) {
+ ev->memory_exception_data = data ? *data :
memory_exception_data;
- set_event(ev);
+ set_event(ev);
+ }
spin_unlock(&ev->lock);
}
@@ -1218,19 +1232,23 @@ void kfd_signal_reset_event(struct kfd_node *dev)
id = KFD_FIRST_NONSIGNAL_EVENT_ID;
idr_for_each_entry_continue(&p->event_idr, ev, id) {
- if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {
+ if (READ_ONCE(ev->type) == KFD_EVENT_TYPE_HW_EXCEPTION) {
spin_lock(&ev->lock);
- ev->hw_exception_data = hw_exception_data;
- ev->hw_exception_data.gpu_id = user_gpu_id;
- set_event(ev);
+ if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {
+ ev->hw_exception_data = hw_exception_data;
+ ev->hw_exception_data.gpu_id = user_gpu_id;
+ set_event(ev);
+ }
spin_unlock(&ev->lock);
}
- if (ev->type == KFD_EVENT_TYPE_MEMORY &&
+ if (READ_ONCE(ev->type) == KFD_EVENT_TYPE_MEMORY &&
reset_cause == KFD_HW_EXCEPTION_ECC) {
spin_lock(&ev->lock);
- ev->memory_exception_data = memory_exception_data;
- ev->memory_exception_data.gpu_id = user_gpu_id;
- set_event(ev);
+ if (ev->type == KFD_EVENT_TYPE_MEMORY) {
+ ev->memory_exception_data = memory_exception_data;
+ ev->memory_exception_data.gpu_id = user_gpu_id;
+ set_event(ev);
+ }
spin_unlock(&ev->lock);
}
}
@@ -1339,17 +1357,21 @@ void kfd_signal_poison_consumed_event(struct kfd_node *dev, u32 pasid)
rcu_read_lock();
idr_for_each_entry_continue(&p->event_idr, ev, id) {
- if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {
+ if (READ_ONCE(ev->type) == KFD_EVENT_TYPE_HW_EXCEPTION) {
spin_lock(&ev->lock);
- ev->hw_exception_data = hw_exception_data;
- set_event(ev);
+ if (ev->type == KFD_EVENT_TYPE_HW_EXCEPTION) {
+ ev->hw_exception_data = hw_exception_data;
+ set_event(ev);
+ }
spin_unlock(&ev->lock);
}
- if (ev->type == KFD_EVENT_TYPE_MEMORY) {
+ if (READ_ONCE(ev->type) == KFD_EVENT_TYPE_MEMORY) {
spin_lock(&ev->lock);
- ev->memory_exception_data = memory_exception_data;
- set_event(ev);
+ if (ev->type == KFD_EVENT_TYPE_MEMORY) {
+ ev->memory_exception_data = memory_exception_data;
+ set_event(ev);
+ }
spin_unlock(&ev->lock);
}
}
@@ -1377,9 +1399,10 @@ void kfd_signal_process_terminate_event(struct kfd_process *p)
/* iterate from id 1 for KFD_EVENT_TYPE_SIGNAL events */
id = 1;
idr_for_each_entry_continue(&p->event_idr, ev, id)
- if (ev->type == KFD_EVENT_TYPE_SIGNAL) {
+ if (READ_ONCE(ev->type) == KFD_EVENT_TYPE_SIGNAL) {
spin_lock(&ev->lock);
- set_event(ev);
+ if (ev->type == KFD_EVENT_TYPE_SIGNAL)
+ set_event(ev);
spin_unlock(&ev->lock);
}
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_events.h b/drivers/gpu/drm/amd/amdkfd/kfd_events.h
index 827a2c7d7721b..6ea9a47827455 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_events.h
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_events.h
@@ -80,6 +80,9 @@ struct kfd_event {
#define KFD_EVENT_TYPE_DEBUG 5
#define KFD_EVENT_TYPE_MEMORY 8
+/* Internal state for events being torn down */
+#define KFD_EVENT_TYPE_INVALID (-1)
+
extern void kfd_signal_event_interrupt(u32 pasid, uint32_t partial_id,
uint32_t valid_id_bits,
bool signal_mailbox_updated);
--
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®