From: Yaohan Li <shunsheng1218@gmail.com>
To: Felix Kuehling <felix.kuehling@amd.com>,
Alex Deucher <alexander.deucher@amd.com>,
Christian Koenig <christian.koenig@amd.com>
Cc: David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
Philip Yang <Philip.Yang@amd.com>,
Oded Gabbay <oded.gabbay@gmail.com>,
Kent Russell <kent.russell@amd.com>,
amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v2] drm/amdkfd: mark destroyed events invalid for RCU readers
Date: Thu, 3 Dec 2026 16:52:36 +0800 [thread overview]
Message-ID: <20261203085236.2391950-1-shunsheng1218@gmail.com> (raw)
In-Reply-To: <4311f8c2-0802-4fdb-a3b0-de9925c90b16@amd.com>
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
prev parent reply other threads:[~2026-12-03 8:52 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Yaohan Li [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203085236.2391950-1-shunsheng1218@gmail.com \
--to=shunsheng1218@gmail.com \
--cc=Philip.Yang@amd.com \
--cc=airlied@gmail.com \
--cc=alexander.deucher@amd.com \
--cc=amd-gfx@lists.freedesktop.org \
--cc=christian.koenig@amd.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=felix.kuehling@amd.com \
--cc=kent.russell@amd.com \
--cc=linux-kernel@vger.kernel.org \
--cc=oded.gabbay@gmail.com \
--cc=simona@ffwll.ch \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®