mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Christian König" <christian.koenig@amd.com>
To: phasta@kernel.org, Danilo Krummrich <dakr@kernel.org>
Cc: "Sumit Semwal" <sumit.semwal@linaro.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: DamFence: Remove warning about unsignaled drop
Date: Thu, 19 Nov 2026 11:47:34 +0100	[thread overview]
Message-ID: <141892f8-180c-4157-8c59-6e69057334f2@amd.com> (raw)
In-Reply-To: <202dd0c3d515ddfa77a8ec8751badadd31b8a7b0.camel@mailbox.org>

On 11/19/26 10:58, Philipp Stanner wrote:
> On Thu, 2026-11-19 at 10:32 +0100, Christian König wrote:
>> On 11/17/26 17:36, Danilo Krummrich wrote:
>>> Drivers often reference count representations of hardware resources and hardware
>>> states and hence keep them alive beyond what their actual lifetime should be --
>>> even beyond driver unbind.
>>
>> Yeah, I agree 100% with that. I know why drivers do this and there are unfortunately really good reason for that.
> 
> To me it seems more that we are often working around fundamental design
> errors. What good reasons come to mind?

Because of the different requirements.

You have one arrow of dependencies pointing down the SW stack, e.g from the application over libraries, uAPI, drivers all the way down to the HW.

But you also have the different direction, where your HW has dependencies on signaling both completion and abortion from way down the stack all the way up to the application.

Now at least permanent references can point only in one direction or otherwise you have a circle dependencies.

BTW that is the reason why dma_fences are RCU protected as well. That is not because of performance but rather to be able to do a try get on a reference to solve reference direction problems.

>>
>>> This causes lot's of issues and I'm looking into fixing this up, see my talk in
>>> [1] and an example from Xe [2] for reference.
>>>
>>> That said, in Rust we don't have this kind of ambiguity, lifetime and ownership
>>> of resources are well defined and the core infrastructure guides drivers
>>> accordingly.
>>
>> I disagree, you do have that in Rust as well.
>>
>> Background is that the fundamental problem of having two signaling
>> directions which want to define lifetime differently doesn't go away
>> because you switch the programming language.
> 
> For Rust DmaFence, this is addressed by having two distinct objects
> sharing the same refcounted memory, DriverFence and Fence.
> 
> Dropping / signaling DriverFence is the decoupling point.
> 
> Which is by the way why I am trying to convince you so hard to give me
> the spinlock, to make this simpler and fully sync ;)

Yeah I perfectly understand that :) I just disagree that this approach is a good idea.

>>> Consequently, it is perfectly reasonable to assume that a driver object that
>>> owns device resources (such as DMA memory) will, in its destructor, tear down
>>> the hardware to not access any of the resources it owns anymore.
>>
>> Exactly that is a pattern I have seen many many times before and I
>> can say from experience that it is very prone to human errors as
>> well.
> 
> Danilo's argument is that the object that halts the hardware is the
> same that owns the fences.
> 
> You drop the hardware-ring-object, this halts the ring, waits for it
> synchronously,

Well how do you wait for the HW ring synchronously?

My point is that this waiting should be done by blocking for the fence to signal and NOT by any other funky method the driver may come up with.

> then the fences drop and signal. Of course, you can
> additionally signal them manually before their drop with another error
> code than the default.
> 
>>> Insisting on an explicit method call is not improving anything in this model.
>>
>> I'm not insisting on an explicit method call.
>>
>> What I'm insisting on is that the response from the HW that the DMA
>> operation has stopped is signaling the fence.
> 
> That is effectively the case, see above.
> 
>> And not that driver tell the HW stop DMA and then continue and dma
>> fence is automatically signaled without explicitly saying that is the
>> signal that DMA operations are stopped.
> 
> Again, the main point here is that the object layout ensures *exactly*
> what you want, Christian
> 
> Like this, pseudocode
> 
> // Drops first, stops hardware
> struct exec_context {
> 	// drops second
> 	jq: JobQueue, // signals remaining fences when it drops
> }
> 
> It's maybe more implicit, but ensures exactly what needs to be ensured.

No it doesn't.

What I want to ensure is that drivers don't come up with funky methods to signal fences.

The cleanup part you want to have here is clearly good to get that defined as well, but to be honest that is peanuts compared to what you get when you have the dma_fence signaling wrong.

> 
> That's how teardown is done in Rust. You drop the lock, it unlocks
> itself. You drop the memory, it frees itself. You drop the fence, it
> signals itself.
> 
> That's by design because the object lifetime model falls naturally into
> place.
> 
>> I think the most defensive way would be as last resort scream loud in
>> the logs that the driver is somehow buggy and hasn't signaled it's
>> fence, wait for a certain amount of time (100ms?) as safeguard and
>> then signal the fence with an error.
> 
> Now that doesn't make any sense at all, either the hardware is stopped
> or it isn't.

There is a huge window of state in between.

> Having some random delay is probably even worse because
> the problem is then more difficult to even encounter and thus fix.

Well the whole point of the delay is to at least have some kind of save guard that makes follow up problems less likely to encounter in production.

That the underlying problem is fixed should be triggered by the big warning in the system log and not because I notice that my valuable data is corrupted because a DMA fence signaled to early and caused random corruption.

> 
>> Exactly that is what I disagree on.
>>
>> Stopping the HW is what makes fences signal and that is what this
>> tear down process should block for.
> 
> Our point is that dropping stuff stops the hardware, and *then* signals
> the fences. Exactly what you want! :)

No, that is completely backwards.

A dma_fence should *NEVER* signal because you waited for something. Instead the dma_fence is what is waited on.

>> Well the question is can we enforce this?
>>
>> I mean there are usually only limited options from the compiler side
>> to enforce things, but we should still push developers to use well
>> proven patterns and coding practices.
> 
> You currently cannot prevent people from refcounting stuff if they
> really want to. The Rust language is working on enforcing that, too,
> though
> 
> https://goals.rust-lang.org/2026/move-trait.html
> 
> 
> btw the best feature that Rust offers IMO is that the refcounting is at
> least always right. That's quite cool. A great many hacks I've seen
> stem from the fact that programmers are not disciplined when it comes
> to copying pointers and their refcount.

Yeah completely agree as well. I mean OCaml already got that point right.

The problem with OCaml was always that you had to distinct between strong and weak references to avoid circle dependencies and therefore memory leaks.

The ownership model from Rust seems to be a big step into the right direction to also solve such issues.

Regards,
Christian.

> 
> 
> P.


  reply	other threads:[~2026-11-19 10:47 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 10:10 Philipp Stanner
2026-11-12 10:29 ` Christian König
2026-11-17  8:24   ` Philipp Stanner
2026-11-17  9:51     ` Christian König
2026-11-17 10:54       ` Philipp Stanner
2026-11-17 13:40         ` Christian König
2026-11-17 13:50           ` Danilo Krummrich
2026-11-17 14:50             ` Christian König
2026-11-17 16:26               ` Gary Guo
2026-11-17 16:36               ` Danilo Krummrich
2026-11-19  9:32                 ` Christian König
2026-11-19  9:58                   ` Philipp Stanner
2026-11-19 10:47                     ` Christian König [this message]
2026-11-19 12:35                   ` Danilo Krummrich
2026-11-17 14:06       ` 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=141892f8-180c-4157-8c59-6e69057334f2@amd.com \
    --to=christian.koenig@amd.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=phasta@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sumit.semwal@linaro.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /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®