Linux Media Controller development
 help / color / mirror / Atom feed
From: "Danilo Krummrich" <dakr@kernel.org>
To: "Christian König" <christian.koenig@amd.com>
Cc: phasta@kernel.org, "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 13:35:28 +0100	[thread overview]
Message-ID: <DMZ6WRFGHVQI.22OJY1Z1QP2MF@kernel.org> (raw)
In-Reply-To: <a7f9e0f3-b299-4d40-9230-106003725dc2@amd.com>

On Thu Nov 19, 2026 at 10:32 AM CET, 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.

I don't think there are good reasons for that.

Given the below, I assume that you refer to a teardown situation where you
instruct the hardware to stop and then destroy the representing software
structure once that actually happened.

But this can't work without synchronization. Drivers live within a bounded
lifetime model, where they must not keep device resources alive beyond driver
unbind. If they do so regardless, that risks UB.

IOW, without synchronization you are trading one potential UB against another.

The solution is not to reference count device resources and potentially keep
them alive beyond the kernel's lifetime constraints, but wait for the hardware
to stop in your teardown path before destroying device resources.

>> 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.
>
> I have worked extensively with OCaml which to be honest looks like the
> grandfather of Rust, but Rust does a lot of things better in my opinion.
>
> See I have like over 30 years of experience with that stuff.

[...]

>> For instance, think about how your logic would apply to e.g. DMA coherent
>> allocations.
>> 
>> Those are allocated with e.g. dma::Coherent::zeroed() and are freed on drop().
>> They are then owned by whatever object represents the lifetime of the HW using
>> this buffer.
>> 
>> Following your logic, we'd need to conclude that a dma::Coherent allocation
>> needs an explicit free() method independent of its destructor as well, and have
>> the destructor print a warning and then call free() anyway.
>
> Yes correct.

[...]

>> Now, eventually drivers would just call free() from the destructor of the object
>> owning the dma::Coherent allocation, so we wouldn't win anything.
>
> No, exactly that's incorrect.
>
> See the problem is that you rarely can tell HW in a linear or synchronous
> fashion to stop DMA operations.
>
> What drivers do instead is to send a request to the HW saying "Please stop the
> DMA operation" and the HW answering with a response which takes the same path
> as the DMA operation itself.
>
> Only this way you can be sure that there are no more DMA operations in flight
> between the HW and system memory.

Sure, but then you have to synchronize on the software side, nothing stops you
to block the teardown until remaining transfers have stopped. And this is
exactly what should be done.

Freeing resources underneath the ownership model from asynchronous scopes is
clearly not the solution.

>> More in general, in Rust the object taking ownership of a device resource is
>> responsible to deal with the device related state that makes use of this
>> resource regardless.
>> 
>> 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.

This is reasonable if there is an asynchronous response.

However, note that the destructor of the object representing the HW state has to
wait for all this to finish regardless, as it also owns the actual underlying
device resources.

There are also cases where there's no asynchronous response; the drivers for
NVIDIA GPUs for instance do synchronous RPCs to tear down runlists, where
pending jobs don't receive completion events anymore.

(The same is true for cases where jobs are queued in a software queue and never
reached the hardware in the first place.)

I get your point that the DMA fence kinda is the synchronization primitive to
wait for, but it can't be generalized that way; the producer (which eventually
is the driver that only signals it on behalf of the hardware) can't always rely
on that.

> 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.

That's the point, the destructor of an object that holds device resources can't
just tell the HW to stop and go ahead without making sure that the HW is
actually stopped once the destructor returns regardless.

  parent reply	other threads:[~2026-11-19 12:35 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 10:10 [PATCH] rust: DamFence: Remove warning about unsignaled drop 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
     [not found]       ` <ab99758408df682c68a3792cc3014b77d6e1c8ab.camel@mailbox.org>
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
2026-11-19 12:35                   ` Danilo Krummrich [this message]
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=DMZ6WRFGHVQI.22OJY1Z1QP2MF@kernel.org \
    --to=dakr@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=christian.koenig@amd.com \
    --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