Building the Linux kernel with Clang and LLVM
 help / color / mirror / Atom feed
From: "Christian König" <christian.koenig@amd.com>
To: phasta@kernel.org, Boqun Feng <boqun.feng@gmail.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Ingo Molnar" <mingo@redhat.com>, "Will Deacon" <will@kernel.org>,
	"Waiman Long" <longman@redhat.com>,
	"Nathan Chancellor" <nathan@kernel.org>,
	"Nick Desaulniers" <nick.desaulniers+lkml@gmail.com>,
	"Bill Wendling" <morbo@google.com>,
	"Justin Stitt" <justinstitt@google.com>,
	"Sumit Semwal" <sumit.semwal@linaro.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Viresh Kumar" <viresh.kumar@linaro.org>,
	"Asahi Lina" <lina+kernel@asahilina.net>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"FUJITA Tomonori" <fujita.tomonori@gmail.com>,
	"Krishna Ketan Rai" <prafulrai522@gmail.com>,
	"Lyude Paul" <lyude@redhat.com>,
	"Mitchell Levy" <levymitchell0@gmail.com>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	llvm@lists.linux.dev, dri-devel@lists.freedesktop.org
Subject: Re: [RFC PATCH] rust: sync: Add dma_fence abstractions
Date: Sun, 28 Sep 2025 17:57:29 +0200	[thread overview]
Message-ID: <3d3304ea-e229-4fca-bea2-5675d24684bc@amd.com> (raw)
In-Reply-To: <63b1274d05f9ad307f29dd0276f0f3014235225e.camel@mailbox.org>

On 28.09.25 17:26, Philipp Stanner wrote:
> On Sun, 2025-09-28 at 16:34 +0200, Christian König wrote:
>> On 27.09.25 11:01, Philipp Stanner wrote:
>>> On Fri, 2025-09-26 at 09:10 -0700, Boqun Feng wrote:
>>>> On Thu, Sep 18, 2025 at 02:30:59PM +0200, Philipp Stanner wrote:
>>>>> dma_fence is a synchronization mechanism which is needed by virtually
>>>>> all GPU drivers.
>>>>>
>>>>> A dma_fence offers many features, among which the most important ones
>>>>> are registering callbacks (for example to kick off a work item) which
>>>>> get executed once a fence gets signalled.
>>>>>
>>>>> dma_fence has a number of callbacks. Only the two most basic ones
>>>>> (get_driver_name(), get_timeline_name() are abstracted since they are
>>>>> enough to enable the basic functionality.
>>>>>
>>>>> Callbacks in Rust are registered by passing driver data which implements
>>>>> the Rust callback trait, whose function will be called by the C backend.
>>>>>
>>>>> dma_fence's are always refcounted, so implement AlwaysRefcounted for
>>>>> them. Once a reference drops to zero, the C backend calls a release
>>>>> function, where we implement drop_in_place() to conveniently marry that
>>>>> C-cleanup mechanism with Rust's ownership concepts.
>>>>>
>>>>> This patch provides basic functionality, but is still missing:
>>>>>   - An implementation of PinInit<T, Error> for all driver data.
>>>>>   - A clever implementation for working dma_fence_begin_signalling()
>>>>>     guards. See the corresponding TODO in the code.
>>>>>   - Additional useful helper functions such as dma_fence_is_signaled().
>>>>>     These _should_ be relatively trivial to implement, though.
>>>>>
>>>>> Signed-off-by: Philipp Stanner <phasta@kernel.org>
>>>>> ---
>>>>> So. ¡Hola!
>>>>>
>>>>> This is a highly WIP RFC. It's obviously at many places not yet
>>>>> conforming very well to Rust's standards.
>>>>>
>>>>> Nevertheless, it has progressed enough that I want to request comments
>>>>> from the community.
>>>>>
>>>>> There are a number of TODOs in the code to which I need input.
>>>>>
>>>>> Notably, it seems (half-)illegal to use a shared static reference to an
>>>>> Atomic, which I currently use for the dma_fence unit test / docstring
>>>>> test. I'm willing to rework that if someone suggests how.
>>>>> (Still, shouldn't changing a global Atomic always be legal? It can race,
>>>>> of course. But that's kind of the point of an atomic)
>>>>>
>>>>> What I want comments on the most is the design of the callbacks. I think
>>>>> it's a great opportunity to provide Rust drivers with rust-only
>>>>> callbacks, so that they don't have to bother about the C functions.
>>>>>
>>>>> dma_fence wise, only the most basic callbacks currently get implemented.
>>>>> For Nova, AFAICS, we don't need much more than signalling fences and
>>>>> registering callbacks.
>>>>>
>>>>>
>>>>> Another, solvable, issue I'm having is designing the
>>>>> dma_fence_begin_signallin() abstractions. There are TODOs about that in
>>>>> the code. That should ideally be robust and not racy. So we might want
>>>>> some sort of synchronized (locked?) way for using that abstraction.
>>>>>
>>>>>
>>>>> Regarding the manually created spinlock of mine: I so far never need
>>>>> that spinlock anywhere in Rust and wasn't sure what's then the best way
>>>>> to pass a "raw" spinlock to C.
>>>>>
>>>>>
>>>>> So much from my side. Hope to hear from you.
>>>>>
>>>>> (I've compiled and tested this with the unit test on the current -rc3)
>>>>>
>>>>> Philipp
>>>>> ---
>>>>>  rust/bindings/bindings_helper.h |   1 +
>>>>>  rust/helpers/dma_fence.c        |  23 ++
>>>>>  rust/helpers/helpers.c          |   1 +
>>>>>  rust/helpers/spinlock.c         |   5 +
>>>>>  rust/kernel/sync.rs             |   2 +
>>>>>  rust/kernel/sync/dma_fence.rs   | 388 ++++++++++++++++++++++++++++++++
>>>>
>>>> I missed this part, and I don't think kernel::sync is where dma_fence
>>>> should be, as kernel::sync is mostly for the basic synchronization
>>>> between threads/irqs. dma_fence is probably better to be grouped with
>>>> dma-buf and other dma related primitives. Maybe in kernel::dma? Like:
>>>>
>>>> rust/kernel/dma.rs
>>>> rust/kernel/dma/dma_buf.rs
>>>> rust/kernel/dma/dma_fence.rs
>>>>
>>>> Thoughts? Miguel, Greg, Danilo and Lyude, any idea or suggestion?
>>>
>>> @Christian König's opinion would be valuable, too.
>>
>> Oh yes, please don't mix dma_fences into SW synchronization, it's a HW synchronization primitive.
> 
> So do you agree that it should be regarded as a part of DMA_BUF?

That works for me.

> 
>>
>>> I'm not super convinced of that because dma_fence has not really much
>>> to do with DMA. They're not very different from completions and are a
>>> mechanism to synchronize consumers and producers.
>>
>> That we mixed up fences and completion events caused a lot of trouble in the past.
>>
>> It's astonishing how often Sima and I had to reject peoples ideas to use the dma_fence as SW sync.
>>
>>> Actually, before f54d1867005c3 they were just called "fence" and then
>>> renamed to "dma_fence" because someone wanted that name.
>>
>> That was also partially done to make sure that people understand that this is not for inter SW sync.
>>
>> On the other hand the rename didn't helped much :/
> 
> gpu_fence might have been appropriate?

No, it's also used for think like RDMA, V4L etc...

There was also a proposal for HW to HW synchronization at some point, but we abandoned that after we found how problematic HW to SW sync already is.

Regards,
Christian.

> 
> 
> Grüße
> P.
> 
> 
>>
>> Regards,
>> Christian.
>>
>>>
>>>
>>> Anyways, I don't have strong objections and mostly care about having
>>> them available somewhere.
>>>
>>> P.
>>>
>>>>
>>>> Regards,
>>>> Boqun
>>>>
>>>>>  6 files changed, 420 insertions(+)
>>>>>  create mode 100644 rust/helpers/dma_fence.c
>>>>>  create mode 100644 rust/kernel/sync/dma_fence.rs
>>>>>
>>>> [...]
>>>
>>
> 


  reply	other threads:[~2025-09-28 15:57 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-18 12:30 [RFC PATCH] rust: sync: Add dma_fence abstractions Philipp Stanner
2025-09-18 13:11 ` Christian König
2025-09-18 13:53   ` Philipp Stanner
2025-09-18 13:52 ` Boqun Feng
2025-09-26  8:48   ` Philipp Stanner
2025-09-26 15:44     ` Boqun Feng
2025-09-26 16:10 ` Boqun Feng
2025-09-27  9:01   ` Philipp Stanner
2025-09-27 13:33     ` Boqun Feng
2025-09-28 14:34     ` Christian König
2025-09-28 15:26       ` Philipp Stanner
2025-09-28 15:57         ` Christian König [this message]
2025-09-27 12:14   ` Miguel Ojeda
2025-09-27 14:03     ` Boqun Feng
2025-09-28 15:20   ` Danilo Krummrich

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=3d3304ea-e229-4fca-bea2-5675d24684bc@amd.com \
    --to=christian.koenig@amd.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=fujita.tomonori@gmail.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=justinstitt@google.com \
    --cc=levymitchell0@gmail.com \
    --cc=lina+kernel@asahilina.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=longman@redhat.com \
    --cc=lossin@kernel.org \
    --cc=lyude@redhat.com \
    --cc=mingo@redhat.com \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=nick.desaulniers+lkml@gmail.com \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=phasta@kernel.org \
    --cc=prafulrai522@gmail.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sumit.semwal@linaro.org \
    --cc=tamird@gmail.com \
    --cc=tmgross@umich.edu \
    --cc=viresh.kumar@linaro.org \
    --cc=wedsonaf@gmail.com \
    --cc=will@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