From: "Christian König" <christian.koenig@amd.com>
To: phasta@kernel.org, Danilo Krummrich <dakr@kernel.org>
Cc: Sumit Semwal <sumit.semwal@linaro.org>,
Boris Brezillon <boris.brezillon@collabora.com>,
Alice Ryhl <aliceryhl@google.com>,
Daniel Almeida <dwlsalmeida@gmail.com>,
Gary Guo <gary@garyguo.net>,
Tvrtko Ursulin <tvrtko.ursulin@igalia.com>,
linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org,
linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH] dma-fence: Fix races of fence callbacks versus destructors by locking
Date: Tue, 9 Jun 2026 12:53:50 +0200 [thread overview]
Message-ID: <1bb5efeb-a5d3-4d0b-ae69-8dc8620604d4@amd.com> (raw)
In-Reply-To: <11d7c83185a18d13760b6e77275e97c110dcddcc.camel@mailbox.org>
On 6/9/26 12:42, Philipp Stanner wrote:
> On Tue, 2026-06-09 at 12:26 +0200, Christian König wrote:
>> On 6/9/26 07:52, Philipp Stanner wrote:
>> ...
>>>>
>>>> In detail calling the callbacks without holding locks allows all implementations who need it to explicitly take locks in the order they want.
>>>
>>> Didn't you say a few mails above that the implementation should not use
>>> the fence lock for its own purposes?
>>
>> The usual use case the drivers have is this here:
>>
>> dma_fence_lock_irqsave(fence, flags);
>> was_signaled = dma_fence_test_signaled(fence);
>> if (!was_signaled)
>> dma_fence_signal(fence);
>> dma_fence_unlock_irqrestore(fence, flags);
>>
>> if (!was_signaled)
>
> If cleanup() touches fence data, this is now exactly the race that I am
> concerned with.
>
> With the current design, you'd actually need a synchronize_rcu() here,
> which you definitely do not want in a hot path :(
>
>> cleanup();
>>
>> This is actually what you and me came up with for the KFD when we
>> removed the return code for dma_fence_signal().
>
> Well, what we came up with for that rare case was
>
> was_signaled = dma_fence_check_and_signal(fence);
> if (!was_signaled)
> cleanup();
>
> It seems that many of the other use cases where the fence lock is taken
> by drivers ultimately are rooted in the (IMO) design mistake that
> dma_fence_set_error() exists.
>
> There should just be
>
> void
> dma_fence_signal(struct dma_fence *f, int err);
>
> which also conveniently forces all signalers to really carefully think
> about whether the fence's associated operations succeeded. Correctly
> representing the "true fence error state" to all consumers is vital for
> sane system behavior, as you continuously have to point out.
Yeah that came to my mind before as well.
>>
>> Taking the lock around the enable_signaling() callback has the exact
>> same reason, preventing the fence from signaling between testing and
>> calling dma_fence_signal(). The problem with that approach is that
>> cleanup() now suddenly runs under the fence lock as well.
>
> That problem does not exist if we re-design dma_fence like that:
>
> // driver
> dma_fence_signal(f); // revokes all accesses to our driver through backend_ops
> // synchronize_rcu() now unnecessary \o/
> cleanup(f); // We know that all accessors are gone
> dma_fence_put(f);
Yeah and exactly that doesn't work.
Just think about the Nouveau case when you have your fences on a double linked list.
When the fence lock is independent, e.g. have a separate lock for each fence then this lock can't protect this double linked list.
So your cleanup path needs to take a lock which protects the list, but you then run into lock inversion.
>>
>> So you are left with few options: Either the fence lock is external,
>> which we don't want because that make the fence non-independent, or
>> cleanup() defers work to irq_work or work_structs, which creates
>> numerous lifetime issues.
>
> Yup, this is uncool and we want to avoid that.
>
> But these seem to be the options
>
> 1. Ensure proper synchronization
> 2. Wait for a grace period in a hot path
> 3. Defer cleanup() with some delay mechanism
>
> #1 is by far the cleanest approach. I still cannot see any downside,
> and quite a few upsides.
>
> https://elixir.bootlin.com/linux/v7.1-rc6/source/drivers/dma-buf/dma-fence.c#L1025
>
> ^ is already racing with the signaled check.
Yeah so what? That is just an opportunistic check.
> I'm going through the users right now, and it seems all we need to
> ensure to implement this change is to audit all ops->signaled() for
> usage of the fence_lock and see if we can port them.
>
>>
>> When enable_signaling would be independent of the fence lock we could
>> avoid all of this and just use a normal spin_lock() for the cleanup.
>
> How's that related to our problem?
See the explanation above for the Nouveau example.
As far as I can see the approach you want to implement here is a NO-GO from my side.
Regards,
Christian.
>
> P.
next prev parent reply other threads:[~2026-06-09 10:54 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-08 14:24 [RFC PATCH] dma-fence: Fix races of fence callbacks versus destructors by locking Philipp Stanner
2026-06-08 15:01 ` Boris Brezillon
2026-06-08 15:17 ` Philipp Stanner
2026-06-08 15:23 ` Danilo Krummrich
2026-06-08 15:30 ` Boris Brezillon
2026-06-08 15:30 ` Philipp Stanner
2026-06-08 16:16 ` Boris Brezillon
2026-06-09 8:02 ` Christian König
2026-06-09 8:54 ` Philipp Stanner
2026-06-09 8:43 ` Philipp Stanner
2026-06-09 8:47 ` Christian König
2026-06-09 9:00 ` Philipp Stanner
2026-06-08 15:07 ` Tvrtko Ursulin
2026-06-08 15:15 ` Philipp Stanner
2026-06-08 15:35 ` Christian König
2026-06-08 15:41 ` Philipp Stanner
2026-06-08 17:34 ` Christian König
2026-06-08 17:59 ` Danilo Krummrich
2026-06-08 18:32 ` Christian König
2026-06-08 18:39 ` Danilo Krummrich
2026-06-08 18:47 ` Christian König
2026-06-08 19:25 ` Danilo Krummrich
2026-06-09 8:17 ` Christian König
2026-06-09 5:52 ` Philipp Stanner
2026-06-09 10:26 ` Christian König
2026-06-09 10:42 ` Philipp Stanner
2026-06-09 10:53 ` Christian König [this message]
2026-06-09 11:39 ` Philipp Stanner
2026-06-09 13:19 ` Philipp Stanner
2026-06-09 13:34 ` Christian König
2026-06-10 14:25 ` Philipp Stanner
2026-06-10 15:15 ` Christian König
2026-06-11 8:35 ` Philipp Stanner
2026-06-11 9:14 ` Christian König
2026-06-11 9:50 ` Philipp Stanner
2026-06-11 11:06 ` Christian König
2026-06-09 13:36 ` Tvrtko Ursulin
2026-06-09 13:57 ` Philipp Stanner
2026-06-09 14:03 ` Christian König
2026-06-15 8:29 ` Properly synchronize dma_fence->signaled bit (Was: Re: [RFC PATCH] dma-fence: Fix races of fence callbacks versus destructors by locking) Philipp Stanner
2026-06-15 9:57 ` Christian König
2026-06-16 11:25 ` Philipp Stanner
2026-06-17 9:46 ` Christian König
2026-06-17 10:16 ` Philipp Stanner
2026-06-17 13:03 ` Christian König
2026-06-17 13:21 ` Philipp Stanner
2026-06-17 13:50 ` Gary Guo
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=1bb5efeb-a5d3-4d0b-ae69-8dc8620604d4@amd.com \
--to=christian.koenig@amd.com \
--cc=aliceryhl@google.com \
--cc=boris.brezillon@collabora.com \
--cc=dakr@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=dwlsalmeida@gmail.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=phasta@kernel.org \
--cc=sumit.semwal@linaro.org \
--cc=tvrtko.ursulin@igalia.com \
/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