mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Felix Kuehling <felix.kuehling@amd.com>
To: Yaohan Li <shunsheng1218@gmail.com>,
	alexander.deucher@amd.com, christian.koenig@amd.com
Cc: airlied@gmail.com, simona@ffwll.ch, oded.gabbay@gmail.com,
	kent.russell@amd.com, amd-gfx@lists.freedesktop.org,
	dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [PATCH] drm/amdkfd: unlink destroyed event waiters from the wait queue
Date: Wed, 2 Dec 2026 14:20:47 -0500	[thread overview]
Message-ID: <4311f8c2-0802-4fdb-a3b0-de9925c90b16@amd.com> (raw)
In-Reply-To: <20261117145406.1092636-1-shunsheng1218@gmail.com>

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

  reply	other threads:[~2026-12-02 19:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 14:54 Yaohan Li
2026-12-02 19:20 ` Felix Kuehling [this message]
2026-12-03  8:52   ` [PATCH v2] drm/amdkfd: mark destroyed events invalid for RCU readers Yaohan Li

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=4311f8c2-0802-4fdb-a3b0-de9925c90b16@amd.com \
    --to=felix.kuehling@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=kent.russell@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oded.gabbay@gmail.com \
    --cc=shunsheng1218@gmail.com \
    --cc=simona@ffwll.ch \
    --cc=stable@vger.kernel.org \
    /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®