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: Properly synchronize dma_fence->signaled bit (Was: Re: [RFC PATCH] dma-fence: Fix races of fence callbacks versus destructors by locking)
Date: Mon, 15 Jun 2026 11:57:58 +0200 [thread overview]
Message-ID: <6ea6c373-c6cb-4221-ad9d-e60b04d1368f@amd.com> (raw)
In-Reply-To: <fa0dc9757bf8343516c4b156a2b70ec91b64ef8f.camel@mailbox.org>
On 6/15/26 10:29, Philipp Stanner wrote:
> On Mon, 2026-06-08 at 20:32 +0200, Christian König wrote:
>> On 6/8/26 19:59, Danilo Krummrich wrote:
>>> On Mon Jun 8, 2026 at 7:34 PM CEST, Christian König wrote:
>>>> That's why we need the RCU grace period to make sure that nobody
>>>> is
>>>> referencing the driver stuff any more.
>>>
>>> Right, and that's what Philipp tries to address, the requirement to
>>> wait for an
>>> RCU grace period is perfectly fine if it is only about freeing
>>> memory, but it
>>> can become painful if the fence private data contains data also
>>> needs to be
>>> destructed in some way.
>>
>> Yeah that makes sense.
>>
>>> IOW, if a driver signals a fence, it is lifecycle-wise reasonable
>>> to destruct
>>> the private data that is no longer needed (remaining users only
>>> deal with struct
>>> dma_fence) and having to wait for a full grace period adds sublety
>>> and
>>> complication that can be avoided with the proposed approach.
>>
>> Yeah, I've run into that when I tried to make the amdgpu fences
>> independent as well.
>>> That said, I'd like to ask the opposite question: What are the
>>> concerns with the
>>> proposed approach over (pure) RCU?
>>
>> For example the reason why we have the dma_fence_is_signaled() and
>> dma_fence_is_signaled_locked() variants is because there is a
>> measurable difference in some specific use cases for not grabbing the
>> locks.
>
> Yeah, certainly, not taking locks makes your code go faster. drm_sched
> can sing a song about that -.-
>
>>
>> I personally find those micro-optimizations rather questionable, but
>> the community agreement is that we should have them.
>
> Yeah so this is most definitely broken and needs to be removed. Proof
> is that various parties already need to work around that issue. You
> just never know what drivers do. It's very conceivable that the Nouveau
> case will often happen:
>
> if (dma_fence_is_signaled(f))
> dma_fence_put(f);
>
>
> This fast path check in my mind certainly breaks the intended dma_fence
> design:
>
> void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
> ktime_t timestamp)
> {
> const struct dma_fence_ops *ops;
> struct dma_fence_cb *cur, *tmp;
> struct list_head cb_list;
>
> dma_fence_assert_held(fence);
>
> if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
> &fence->flags)))
> return;
>
>
> Modifying the bit is consistently done under lock protection, so
> reading must be done, too.
>
> Do you remember who wanted those fast path checks? Who is spinning on
> that lock?
Simona Vetter and basically the rest of the community.
And I can clearly say even if I don't like them that those optimizations are a must have.
> In any case, that needs to be repaired.
No, see my discussion with Simona on the mailing list. I need to dig that up as well, but it was around the time I added the same workaround to amdgpu.
You are basically trying what I have been suggesting as well, but there is a very wide agreement that the current design is a must have.
Regards,
Christian.
> A hacky lockless way might
> hypothetically be doable by setting barriers, as I suggest here:
>
> https://lore.kernel.org/dri-devel/20260612104251.2264707-2-phasta@kernel.org/
>
> But note that the ops-decoupling in e.g. dma_fence_timeline_name()
> already depends on lockless ordering mechanisms in
> dma_fence_signal_timestamp_locked(). So we're already a bit fragile
> here.
>
>>
>> So my take would rather be that the dma_fence_is_signaled_locked()
>> variant goes away and we consistently call the ops pointers without
>> holding the dma_fence lock and the driver implementations can then
>> optionally take it if necessary.
>
> That might work.
>
>>
>> I think for this we would just need to replace most calls to
>> dma_fence_is_signaled_locked() with dma_fence_test_signaled().
>
> That would not fix the cleanup race in Nouveau.
>
> I do get the idealistic idea of fence->signaled really just
> representing whether the hardware is done, without any further
> guarantees, but having this fast-path lockless magic in functions
> checking the signaled state is really asking for trouble.
>
> So it would seem that both dma_fence_is_signaled() and
> dma_fence_test_signaled_flag() need to be properly synchronized.
>
>
> P.
>
>>
>> In the long term that would also allow cleaning up the container
>> handling and simplifying the DRM scheduler a bit.
>>
>> Regards,
>> Christian.
next prev parent reply other threads:[~2026-06-15 9:58 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
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 [this message]
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=6ea6c373-c6cb-4221-ad9d-e60b04d1368f@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