Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH] rust: DamFence: Remove warning about unsignaled drop
@ 2026-11-12 10:10 Philipp Stanner
  2026-11-12 10:29 ` Christian König
  0 siblings, 1 reply; 15+ messages in thread
From: Philipp Stanner @ 2026-11-12 10:10 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner,
	Danilo Krummrich, Alice Ryhl, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan
  Cc: linux-media, dri-devel, rust-for-linux, linux-kernel

DriverFence::drop() contains a pr_warn!() for when the fence drops
unsignaled. This warning gets into the way of canonical teardown in
Rust: when you drop a lock, it unlocks itself, when you drop a KBox, it
frees itself etc. Congruently, users should be able to tear down fences
they don't care about anymore with ECANCELED, which is already the
default error code.

Remove the warning.

Suggested-by: Danilo Krummrich <dakr@kernel.org>
Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
 rust/kernel/dma_buf/dma_fence.rs | 1 -
 1 file changed, 1 deletion(-)

diff --git a/rust/kernel/dma_buf/dma_fence.rs b/rust/kernel/dma_buf/dma_fence.rs
index a1d851656562..2c8001cb9a59 100644
--- a/rust/kernel/dma_buf/dma_fence.rs
+++ b/rust/kernel/dma_buf/dma_fence.rs
@@ -942,7 +942,6 @@ fn drop(&mut self) {
         // SAFETY: `guard` is valid until the `call_rcu()` below.
         let signaled: bool = unsafe { bindings::dma_fence_test_signaled_flag(guard.as_raw()) };
         if !signaled {
-            pr_err!("DriverFence drops unsignaled. Danger of memory corruption!\n");
             // SAFETY: `guard` is valid until the `call_rcu()` below. The fence
             // must not have been signaled yet, which we check directly above.
             unsafe { bindings::dma_fence_set_error(guard.as_raw(), ECANCELED.to_errno()) };
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [PATCH] rust: DamFence: Remove warning about unsignaled drop
  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
  0 siblings, 1 reply; 15+ messages in thread
From: Christian König @ 2026-11-12 10:29 UTC (permalink / raw)
  To: Philipp Stanner, Sumit Semwal, Danilo Krummrich, Alice Ryhl,
	Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Trevor Gross, Daniel Almeida,
	Tamir Duberstein, Alexandre Courbot, Onur Özkan
  Cc: linux-media, dri-devel, rust-for-linux, linux-kernel

On 11/12/26 11:10, Philipp Stanner wrote:
> DriverFence::drop() contains a pr_warn!() for when the fence drops
> unsignaled. This warning gets into the way of canonical teardown in
> Rust: when you drop a lock, it unlocks itself, when you drop a KBox, it
> frees itself etc. Congruently, users should be able to tear down fences
> they don't care about anymore with ECANCELED, which is already the
> default error code.

Well a dma_fence should never automatically signal when the last reference drops.

Leaving it unsignaled is the most defensive path we can go. That can lead to deadlocks but it can at least not lead to memory corruption.

Regards,
Christian.

> 
> Remove the warning.
> 
> Suggested-by: Danilo Krummrich <dakr@kernel.org>
> Signed-off-by: Philipp Stanner <phasta@kernel.org>
> ---
>  rust/kernel/dma_buf/dma_fence.rs | 1 -
>  1 file changed, 1 deletion(-)
> 
> diff --git a/rust/kernel/dma_buf/dma_fence.rs b/rust/kernel/dma_buf/dma_fence.rs
> index a1d851656562..2c8001cb9a59 100644
> --- a/rust/kernel/dma_buf/dma_fence.rs
> +++ b/rust/kernel/dma_buf/dma_fence.rs
> @@ -942,7 +942,6 @@ fn drop(&mut self) {
>          // SAFETY: `guard` is valid until the `call_rcu()` below.
>          let signaled: bool = unsafe { bindings::dma_fence_test_signaled_flag(guard.as_raw()) };
>          if !signaled {
> -            pr_err!("DriverFence drops unsignaled. Danger of memory corruption!\n");
>              // SAFETY: `guard` is valid until the `call_rcu()` below. The fence
>              // must not have been signaled yet, which we check directly above.
>              unsafe { bindings::dma_fence_set_error(guard.as_raw(), ECANCELED.to_errno()) };


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] rust: DamFence: Remove warning about unsignaled drop
  2026-11-12 10:29 ` Christian König
@ 2026-11-17  8:24   ` Philipp Stanner
  2026-11-17  9:51     ` Christian König
  0 siblings, 1 reply; 15+ messages in thread
From: Philipp Stanner @ 2026-11-17  8:24 UTC (permalink / raw)
  To: Christian König, Philipp Stanner, Sumit Semwal,
	Danilo Krummrich, Alice Ryhl, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan
  Cc: linux-media, dri-devel, rust-for-linux, linux-kernel

On Thu, 2026-11-12 at 11:29 +0100, Christian König wrote:
> On 11/12/26 11:10, Philipp Stanner wrote:
> > DriverFence::drop() contains a pr_warn!() for when the fence drops
> > unsignaled. This warning gets into the way of canonical teardown in
> > Rust: when you drop a lock, it unlocks itself, when you drop a KBox, it
> > frees itself etc. Congruently, users should be able to tear down fences
> > they don't care about anymore with ECANCELED, which is already the
> > default error code.
> 
> Well a dma_fence should never automatically signal when the last
> reference drops.
> 
> Leaving it unsignaled is the most defensive path we can go. That can
> lead to deadlocks but it can at least not lead to memory corruption.

True, you choose between the Unholy Trinity of DRM: deadlocks, UAF and
memory corruption :]

In this particular case, however, Rust does not allow for preventing
the user from signaling "forgotten" fences anyways, no matter how we
write the fence core:

struct DriverFoo {
	f: DriverFence<()>,
}

impl Drop for Driver Foo {
	pub fn drop(&mut self) {
		// Signals fence implicitly & automatically.
		self.f.signal(ECANCELED);
	}
}


So we decided to die this death because this way we get rid of the two
of the three problems that *can* be solved in the API.


But it will please you to hear that all users of drm::JobQueue do not
have access to fences directly anyways, they can only signal them
through JobQueue. Thus, they can also not forget them (signaled or
unsignaled), because they would have to forget / leak the entire
JobQueue as a whole, so effectively forgetting also their GPU ring. If
that would happen, the driver would be broken beyond repair anyways.

Regards
P.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] rust: DamFence: Remove warning about unsignaled drop
  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 14:06       ` Danilo Krummrich
  0 siblings, 2 replies; 15+ messages in thread
From: Christian König @ 2026-11-17  9:51 UTC (permalink / raw)
  To: phasta, Sumit Semwal, Danilo Krummrich, Alice Ryhl, Miguel Ojeda,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Trevor Gross, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: linux-media, dri-devel, rust-for-linux, linux-kernel

On 11/17/26 09:24, Philipp Stanner wrote:
> On Thu, 2026-11-12 at 11:29 +0100, Christian König wrote:
>> On 11/12/26 11:10, Philipp Stanner wrote:
>>> DriverFence::drop() contains a pr_warn!() for when the fence drops
>>> unsignaled. This warning gets into the way of canonical teardown in
>>> Rust: when you drop a lock, it unlocks itself, when you drop a KBox, it
>>> frees itself etc. Congruently, users should be able to tear down fences
>>> they don't care about anymore with ECANCELED, which is already the
>>> default error code.
>>
>> Well a dma_fence should never automatically signal when the last
>> reference drops.
>>
>> Leaving it unsignaled is the most defensive path we can go. That can
>> lead to deadlocks but it can at least not lead to memory corruption.
> 
> True, you choose between the Unholy Trinity of DRM: deadlocks, UAF and
> memory corruption :]
> 
> In this particular case, however, Rust does not allow for preventing
> the user from signaling "forgotten" fences anyways, no matter how we
> write the fence core:
> 
> struct DriverFoo {
> 	f: DriverFence<()>,
> }
> 
> impl Drop for Driver Foo {
> 	pub fn drop(&mut self) {
> 		// Signals fence implicitly & automatically.
> 		self.f.signal(ECANCELED);
> 	}
> }

Yeah, exactly that is still a bad idea :) See the HW eventually doesn't necessarily stop doing DMA if you unload the driver.

I even know about one case where a driver fully intentionally leaks memory on unload because the devs couldn't figure out which FW keeps writing into a buffer.

This was suggested as problem solving approach for AMDGPU as well, but we have been able to avoid it by clearing the DMA master flag in the PCI config dword.


The fundamental problem here is that a software object running out of scope is not an indicator that the HW stops doing something.

So just signaling all fences with ECANCELED is not enough. When the driver runs out of scope without all fences being signaling we seriously need at least some kind of warning in the system log, potentially even better call BUG() or maybe even panic().

Cheers,
Christian.

> 
> 
> So we decided to die this death because this way we get rid of the two
> of the three problems that *can* be solved in the API.
> 
> 
> But it will please you to hear that all users of drm::JobQueue do not
> have access to fences directly anyways, they can only signal them
> through JobQueue. Thus, they can also not forget them (signaled or
> unsignaled), because they would have to forget / leak the entire
> JobQueue as a whole, so effectively forgetting also their GPU ring. If
> that would happen, the driver would be broken beyond repair anyways.
> 
> Regards
> P.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] rust: DamFence: Remove warning about unsignaled drop
  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 14:06       ` Danilo Krummrich
  1 sibling, 1 reply; 15+ messages in thread
From: Philipp Stanner @ 2026-11-17 10:54 UTC (permalink / raw)
  To: Christian König, phasta, Sumit Semwal, Danilo Krummrich,
	Alice Ryhl, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan
  Cc: linux-media, dri-devel, rust-for-linux, linux-kernel

On Tue, 2026-11-17 at 10:51 +0100, Christian König wrote:
> On 11/17/26 09:24, Philipp Stanner wrote:
> > struct DriverFoo {
> > 	f: DriverFence<()>,
> > }
> > 
> > impl Drop for Driver Foo {
> > 	pub fn drop(&mut self) {
> > 		// Signals fence implicitly & automatically.
> > 		self.f.signal(ECANCELED);
> > 	}
> > }
> 
> Yeah, exactly that is still a bad idea :) See the HW eventually
> doesn't necessarily stop doing DMA if you unload the driver.

I know.

> So just signaling all fences with ECANCELED is not enough.


Yes. What I was trying to tell you as that you have no way to enforce
this in Rust or C.

dma_fence_signal(f);
free_driver_resources();
// Driver forgets to stop the hardware. Memory corruption occurs.

You see? Neither your BUG_ON() or WARN_ON() saves you if the driver
does it like that, no matter the programming language. You cannot
prevent it.

The only one who understands how the hardware works is the driver. The
only one who can stop it is the driver.

In Rust, the place where a driver drops its JobQueue is the same place
where it has to halt its GPU ring correctly. Enforcing this cannot be
done by dma_fence code.


P.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] rust: DamFence: Remove warning about unsignaled drop
  2026-11-17 10:54       ` Philipp Stanner
@ 2026-11-17 13:40         ` Christian König
  2026-11-17 13:50           ` Danilo Krummrich
  0 siblings, 1 reply; 15+ messages in thread
From: Christian König @ 2026-11-17 13:40 UTC (permalink / raw)
  To: phasta, Sumit Semwal, Danilo Krummrich, Alice Ryhl, Miguel Ojeda,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Trevor Gross, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: linux-media, dri-devel, rust-for-linux, linux-kernel

On 11/17/26 11:54, Philipp Stanner wrote:
> On Tue, 2026-11-17 at 10:51 +0100, Christian König wrote:
>> On 11/17/26 09:24, Philipp Stanner wrote:
>>> struct DriverFoo {
>>> 	f: DriverFence<()>,
>>> }
>>>
>>> impl Drop for Driver Foo {
>>> 	pub fn drop(&mut self) {
>>> 		// Signals fence implicitly & automatically.
>>> 		self.f.signal(ECANCELED);
>>> 	}
>>> }
>>
>> Yeah, exactly that is still a bad idea :) See the HW eventually
>> doesn't necessarily stop doing DMA if you unload the driver.
> 
> I know.
> 
>> So just signaling all fences with ECANCELED is not enough.
> 
> 
> Yes. What I was trying to tell you as that you have no way to enforce
> this in Rust or C.
> 
> dma_fence_signal(f);
> free_driver_resources();
> // Driver forgets to stop the hardware. Memory corruption occurs.
> 
> You see? Neither your BUG_ON() or WARN_ON() saves you if the driver
> does it like that, no matter the programming language. You cannot
> prevent it.
> 
> The only one who understands how the hardware works is the driver. The
> only one who can stop it is the driver.
> 
> In Rust, the place where a driver drops its JobQueue is the same place
> where it has to halt its GPU ring correctly. Enforcing this cannot be
> done by dma_fence code.

I completely agree that we can't fix things from the dma_fence code, but it is our obligation to tell the user that something very very bad happened.

I mean that is what WARN_ON(), BUG_ON() and panic() are essentially good for, they never fix anything but just point out that the system should probably be rebooted and driver developers informed.

Regards,
Christian.

> 
> 
> P.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] rust: DamFence: Remove warning about unsignaled drop
  2026-11-17 13:40         ` Christian König
@ 2026-11-17 13:50           ` Danilo Krummrich
  2026-11-17 14:50             ` Christian König
  0 siblings, 1 reply; 15+ messages in thread
From: Danilo Krummrich @ 2026-11-17 13:50 UTC (permalink / raw)
  To: Christian König
  Cc: phasta, Sumit Semwal, Alice Ryhl, Miguel Ojeda, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, linux-media, dri-devel, rust-for-linux,
	linux-kernel

On Tue Nov 17, 2026 at 2:40 PM CET, Christian König wrote:
> On 11/17/26 11:54, Philipp Stanner wrote:
>> On Tue, 2026-11-17 at 10:51 +0100, Christian König wrote:
>>> On 11/17/26 09:24, Philipp Stanner wrote:
>>>> struct DriverFoo {
>>>> 	f: DriverFence<()>,
>>>> }
>>>>
>>>> impl Drop for Driver Foo {
>>>> 	pub fn drop(&mut self) {
>>>> 		// Signals fence implicitly & automatically.
>>>> 		self.f.signal(ECANCELED);
>>>> 	}
>>>> }
>>>
>>> Yeah, exactly that is still a bad idea :) See the HW eventually
>>> doesn't necessarily stop doing DMA if you unload the driver.
>> 
>> I know.
>> 
>>> So just signaling all fences with ECANCELED is not enough.
>> 
>> 
>> Yes. What I was trying to tell you as that you have no way to enforce
>> this in Rust or C.
>> 
>> dma_fence_signal(f);
>> free_driver_resources();
>> // Driver forgets to stop the hardware. Memory corruption occurs.
>> 
>> You see? Neither your BUG_ON() or WARN_ON() saves you if the driver
>> does it like that, no matter the programming language. You cannot
>> prevent it.
>> 
>> The only one who understands how the hardware works is the driver. The
>> only one who can stop it is the driver.
>> 
>> In Rust, the place where a driver drops its JobQueue is the same place
>> where it has to halt its GPU ring correctly. Enforcing this cannot be
>> done by dma_fence code.
>
> I completely agree that we can't fix things from the dma_fence code, but it is our obligation to tell the user that something very very bad happened.

That's the point, we can't infer that "something very bad happened" from
dma::DriverFence being dropped unsignaled.

In Rust we use the RAII pattern, and drivers structurally embed a
dma::DriverFence within the struct that represents the corresponding hardware /
firmware queue (e.g. through Jobqueue).

Consequently, if dma::DriverFence drops unsignaled it means that the destructor
of the structure that embeds it (which does the required HW teardown) did run
already.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] rust: DamFence: Remove warning about unsignaled drop
  2026-11-17  9:51     ` Christian König
  2026-11-17 10:54       ` Philipp Stanner
@ 2026-11-17 14:06       ` Danilo Krummrich
  1 sibling, 0 replies; 15+ messages in thread
From: Danilo Krummrich @ 2026-11-17 14:06 UTC (permalink / raw)
  To: Christian König
  Cc: phasta, Sumit Semwal, Alice Ryhl, Miguel Ojeda, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, linux-media, dri-devel, rust-for-linux,
	linux-kernel

On Tue Nov 17, 2026 at 10:51 AM CET, Christian König wrote:
> See the HW eventually doesn't necessarily stop doing DMA if you unload the driver.

No, if you can't stop the underlying device you must not unload the driver and
leak resources. Device resources (such as DMA mappings) are not safe against
this and it can cause UB in many layers of kernel, e.g. IOMMU.

> I even know about one case where a driver fully intentionally leaks memory on
> unload because the devs couldn't figure out which FW keeps writing into a
> buffer.

Again, please don't finish unloading then; everything else will cause UB.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] rust: DamFence: Remove warning about unsignaled drop
  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
  0 siblings, 2 replies; 15+ messages in thread
From: Christian König @ 2026-11-17 14:50 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: phasta, Sumit Semwal, Alice Ryhl, Miguel Ojeda, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, linux-media, dri-devel, rust-for-linux,
	linux-kernel

On 11/17/26 14:50, Danilo Krummrich wrote:
> On Tue Nov 17, 2026 at 2:40 PM CET, Christian König wrote:
>> On 11/17/26 11:54, Philipp Stanner wrote:
>>> On Tue, 2026-11-17 at 10:51 +0100, Christian König wrote:
>>>> On 11/17/26 09:24, Philipp Stanner wrote:
>>>>> struct DriverFoo {
>>>>> 	f: DriverFence<()>,
>>>>> }
>>>>>
>>>>> impl Drop for Driver Foo {
>>>>> 	pub fn drop(&mut self) {
>>>>> 		// Signals fence implicitly & automatically.
>>>>> 		self.f.signal(ECANCELED);
>>>>> 	}
>>>>> }
>>>>
>>>> Yeah, exactly that is still a bad idea :) See the HW eventually
>>>> doesn't necessarily stop doing DMA if you unload the driver.
>>>
>>> I know.
>>>
>>>> So just signaling all fences with ECANCELED is not enough.
>>>
>>>
>>> Yes. What I was trying to tell you as that you have no way to enforce
>>> this in Rust or C.
>>>
>>> dma_fence_signal(f);
>>> free_driver_resources();
>>> // Driver forgets to stop the hardware. Memory corruption occurs.
>>>
>>> You see? Neither your BUG_ON() or WARN_ON() saves you if the driver
>>> does it like that, no matter the programming language. You cannot
>>> prevent it.
>>>
>>> The only one who understands how the hardware works is the driver. The
>>> only one who can stop it is the driver.
>>>
>>> In Rust, the place where a driver drops its JobQueue is the same place
>>> where it has to halt its GPU ring correctly. Enforcing this cannot be
>>> done by dma_fence code.
>>
>> I completely agree that we can't fix things from the dma_fence code, but it is our obligation to tell the user that something very very bad happened.
> 
> That's the point, we can't infer that "something very bad happened" from
> dma::DriverFence being dropped unsignaled.
> 
> In Rust we use the RAII pattern, and drivers structurally embed a
> dma::DriverFence within the struct that represents the corresponding hardware /
> firmware queue (e.g. through Jobqueue).
> 
> Consequently, if dma::DriverFence drops unsignaled it means that the destructor
> of the structure that embeds it (which does the required HW teardown) did run
> already.

Oh WOW! The signaling state of the fence should *NEVER* be bound to some object lifetime.

That sounds like an extremely bad practice to me. Just imagine anybody grabbing an extra reference to the HW state object to inspect it for debugging and the fence not signaling because of this.

Signaling a DMA-buf should always be an explicit operation drivers do because of some HW event and not something which happens as a side effect cleanup because your object runs out of scope.

Regards,
Christian.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] rust: DamFence: Remove warning about unsignaled drop
  2026-11-17 14:50             ` Christian König
@ 2026-11-17 16:26               ` Gary Guo
  2026-11-17 16:36               ` Danilo Krummrich
  1 sibling, 0 replies; 15+ messages in thread
From: Gary Guo @ 2026-11-17 16:26 UTC (permalink / raw)
  To: Christian König, Danilo Krummrich
  Cc: phasta, Sumit Semwal, Alice Ryhl, Miguel Ojeda, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, linux-media, dri-devel, rust-for-linux,
	linux-kernel

On Tue Nov 17, 2026 at 2:50 PM GMT, Christian König wrote:
> On 11/17/26 14:50, Danilo Krummrich wrote:
>> On Tue Nov 17, 2026 at 2:40 PM CET, Christian König wrote:
>>> On 11/17/26 11:54, Philipp Stanner wrote:
>>>> On Tue, 2026-11-17 at 10:51 +0100, Christian König wrote:
>>>>> On 11/17/26 09:24, Philipp Stanner wrote:
>>>>>> struct DriverFoo {
>>>>>> 	f: DriverFence<()>,
>>>>>> }
>>>>>>
>>>>>> impl Drop for Driver Foo {
>>>>>> 	pub fn drop(&mut self) {
>>>>>> 		// Signals fence implicitly & automatically.
>>>>>> 		self.f.signal(ECANCELED);
>>>>>> 	}
>>>>>> }
>>>>>
>>>>> Yeah, exactly that is still a bad idea :) See the HW eventually
>>>>> doesn't necessarily stop doing DMA if you unload the driver.
>>>>
>>>> I know.
>>>>
>>>>> So just signaling all fences with ECANCELED is not enough.
>>>>
>>>>
>>>> Yes. What I was trying to tell you as that you have no way to enforce
>>>> this in Rust or C.
>>>>
>>>> dma_fence_signal(f);
>>>> free_driver_resources();
>>>> // Driver forgets to stop the hardware. Memory corruption occurs.
>>>>
>>>> You see? Neither your BUG_ON() or WARN_ON() saves you if the driver
>>>> does it like that, no matter the programming language. You cannot
>>>> prevent it.
>>>>
>>>> The only one who understands how the hardware works is the driver. The
>>>> only one who can stop it is the driver.
>>>>
>>>> In Rust, the place where a driver drops its JobQueue is the same place
>>>> where it has to halt its GPU ring correctly. Enforcing this cannot be
>>>> done by dma_fence code.
>>>
>>> I completely agree that we can't fix things from the dma_fence code, but it is our obligation to tell the user that something very very bad happened.
>> 
>> That's the point, we can't infer that "something very bad happened" from
>> dma::DriverFence being dropped unsignaled.
>> 
>> In Rust we use the RAII pattern, and drivers structurally embed a
>> dma::DriverFence within the struct that represents the corresponding hardware /
>> firmware queue (e.g. through Jobqueue).
>> 
>> Consequently, if dma::DriverFence drops unsignaled it means that the destructor
>> of the structure that embeds it (which does the required HW teardown) did run
>> already.
>
> Oh WOW! The signaling state of the fence should *NEVER* be bound to some object lifetime.
>
> That sounds like an extremely bad practice to me. Just imagine anybody
> grabbing an extra reference to the HW state object to inspect it for debugging
> and the fence not signaling because of this.

We do not allow "grab a reference" to prevent HW state from going away in Rust.
All device resources are tied to the lifetime of bus device being bound. When
device is unbound (device going away, sysfs unbind, parent unbind, or whatever),
device private data goes it with it and all device resources must cease to be
alive before then.

If driver's fence callbacks can access HW state, it must also be teared down at
at that point and cannot live past that.

Best,
Gary

>
> Signaling a DMA-buf should always be an explicit operation drivers do because
> of some HW event and not something which happens as a side effect cleanup
> because your object runs out of scope.
>
> Regards,
> Christian.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] rust: DamFence: Remove warning about unsignaled drop
  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
  1 sibling, 1 reply; 15+ messages in thread
From: Danilo Krummrich @ 2026-11-17 16:36 UTC (permalink / raw)
  To: Christian König
  Cc: phasta, Sumit Semwal, Alice Ryhl, Miguel Ojeda, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, linux-media, dri-devel, rust-for-linux,
	linux-kernel

On Tue Nov 17, 2026 at 3:50 PM CET, Christian König wrote:
> On 11/17/26 14:50, Danilo Krummrich wrote:
>> On Tue Nov 17, 2026 at 2:40 PM CET, Christian König wrote:
>>> I completely agree that we can't fix things from the dma_fence code, but it
>>> is our obligation to tell the user that something very very bad happened.
>> 
>> That's the point, we can't infer that "something very bad happened" from
>> dma::DriverFence being dropped unsignaled.
>> 
>> In Rust we use the RAII pattern, and drivers structurally embed a
>> dma::DriverFence within the struct that represents the corresponding hardware /
>> firmware queue (e.g. through Jobqueue).
>> 
>> Consequently, if dma::DriverFence drops unsignaled it means that the destructor
>> of the structure that embeds it (which does the required HW teardown) did run
>> already.
>
> Oh WOW! The signaling state of the fence should *NEVER* be bound to some
> object lifetime.

Let's engage with the argument and understand things before throwing
overgeneralized statements like this around please.

Realitiy is usually much more nuanced and what you say doesn't hold in all
cases. The case described above (and clarified below) is one of them.

I assume the fundamental reason why you say things like "software object running
out of scope is not an indicator that the HW stops doing something" is because
the lifetime model in a lot of existing C DRM drivers is backwards.

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.

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.

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.

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.

Now, eventually drivers would just call free() from the destructor of the object
owning the dma::Coherent allocation, so we wouldn't win anything.

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.

To get back to dma::DriverFence: This is slightly different, as there's two
different signal states, but the fundamental guarantee is the same: If it is
dropped, the HW has stopped accessing the resource that it guards already *by
design*.

Signaling with an error on drop is arguably the correct thing to do, since if
the operation would have been completed successfully before already, it would
have been signaled already -- if it drops unsignaled it means the hardware was
forcefully torn down (e.g. on driver unbind).


[1] https://youtu.be/3A2vGbc_rxs?t=668
[2] https://lore.kernel.org/all/as07AdBFlOQZrH_4@intel.com/

> That sounds like an extremely bad practice to me. Just imagine anybody
> grabbing an extra reference to the HW state object to inspect it for debugging
> and the fence not signaling because of this.

That's not how it works.

Rust ownership rules will guarantee that the driver fence will be signaled with
an error (if not signaled already) if the HW state object is destroyed (i.e. its
destructor runs that tears down the HW). But that doesn't mean that the fence
can't be signaled (and subsequently be dropped) independently through the
completion of a job before that.

IOW, the HW state object (for instance) owns Jobqueue, Jobqueue owns jobs, jobs
own driver fences.

If the HW state object drops (e.g. on driver unbind), the Jobqueue drops, which
makes their jobs drop, which makes the driver fences drop.

At this point the driver fence either has been signaled already because the job
completed before teardown (but wasn't dropped yet), or it will be signaled with
an error in its drop(), which is fine since at this point the HW state's drop()
is completed already and hence the HW is stopped.

Besides that, reference counting HW state objects is bad practice. HW state
objects have a bounded lifetime, so there's no point in reference counting them.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] rust: DamFence: Remove warning about unsignaled drop
  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 12:35                   ` Danilo Krummrich
  0 siblings, 2 replies; 15+ messages in thread
From: Christian König @ 2026-11-19  9:32 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: phasta, Sumit Semwal, Alice Ryhl, Miguel Ojeda, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, linux-media, dri-devel, rust-for-linux,
	linux-kernel

On 11/17/26 17:36, Danilo Krummrich wrote:
> On Tue Nov 17, 2026 at 3:50 PM CET, Christian König wrote:
>> On 11/17/26 14:50, Danilo Krummrich wrote:
>>> On Tue Nov 17, 2026 at 2:40 PM CET, Christian König wrote:
>>>> I completely agree that we can't fix things from the dma_fence code, but it
>>>> is our obligation to tell the user that something very very bad happened.
>>>
>>> That's the point, we can't infer that "something very bad happened" from
>>> dma::DriverFence being dropped unsignaled.
>>>
>>> In Rust we use the RAII pattern, and drivers structurally embed a
>>> dma::DriverFence within the struct that represents the corresponding hardware /
>>> firmware queue (e.g. through Jobqueue).
>>>
>>> Consequently, if dma::DriverFence drops unsignaled it means that the destructor
>>> of the structure that embeds it (which does the required HW teardown) did run
>>> already.
>>
>> Oh WOW! The signaling state of the fence should *NEVER* be bound to some
>> object lifetime.
> 
> Let's engage with the argument and understand things before throwing
> overgeneralized statements like this around please.

well first of all I'm deeply sorry to be so bitchy about that and reacting emotionally.

It's just so massively frustrating to been proven right with design criticism over and over again and seeing nothing change in the long term.

> Realitiy is usually much more nuanced and what you say doesn't hold in all
> cases. The case described above (and clarified below) is one of them.
> 
> I assume the fundamental reason why you say things like "software object running
> out of scope is not an indicator that the HW stops doing something" is because
> the lifetime model in a lot of existing C DRM drivers is backwards.

I partially agree, yes.

HW drivers generally face the problem that managing lifetime have contradicting requirements.

So you often find a lifetime model which is SW stack centric with applications referencing device drivers, device drivers referencing HW resources, etc...

While from the perspective of the HW you want a lifetime model where HW events define when the lifetime of some object can end.

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

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

Rust still makes it much much better because the programming language has a well defined model instead of C where you can mess things up trivially and need to rely on developer discipline to get things right. 

But even in Rust there is still the need to give HW drivers something like a template in which is defined how things should be done and what things need to be avoided.

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

This is even nailed down in things like the PCI specification for example and it is something people get wrong over and over again.

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

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. 

> To get back to dma::DriverFence: This is slightly different, as there's two> different signal states, but the fundamental guarantee is the same: If it is
> dropped, the HW has stopped accessing the resource that it guards already *by
> design*.
> 
> Signaling with an error on drop is arguably the correct thing to do, since if
> the operation would have been completed successfully before already, it would
> have been signaled already -- if it drops unsignaled it means the hardware was
> forcefully torn down (e.g. on driver unbind).

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.

>> That sounds like an extremely bad practice to me. Just imagine anybody
>> grabbing an extra reference to the HW state object to inspect it for debugging
>> and the fence not signaling because of this.
> 
> That's not how it works.
> 
> Rust ownership rules will guarantee that the driver fence will be signaled with
> an error (if not signaled already) if the HW state object is destroyed (i.e. its
> destructor runs that tears down the HW). But that doesn't mean that the fence
> can't be signaled (and subsequently be dropped) independently through the
> completion of a job before that.

Yeah I have to admit that I misunderstood that part, it sounded to me like you want to always bind the fence signaling to some object lifetime.

Again sorry for that.

> 
> IOW, the HW state object (for instance) owns Jobqueue, Jobqueue owns jobs, jobs
> own driver fences.
> 
> If the HW state object drops (e.g. on driver unbind), the Jobqueue drops, which
> makes their jobs drop, which makes the driver fences drop.
> 
> At this point the driver fence either has been signaled already because the job
> completed before teardown (but wasn't dropped yet), or it will be signaled with
> an error in its drop(), which is fine since at this point the HW state's drop()
> is completed already and hence the HW is stopped.

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.

And not the other way around that the teardown process signals the fence assuming that the driver did everything right.

> Besides that, reference counting HW state objects is bad practice. HW state
> objects have a bounded lifetime, so there's no point in reference counting them.

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.

Regards,
Christian.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] rust: DamFence: Remove warning about unsignaled drop
  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
  1 sibling, 1 reply; 15+ messages in thread
From: Philipp Stanner @ 2026-11-19  9:58 UTC (permalink / raw)
  To: Christian König, Danilo Krummrich
  Cc: phasta, Sumit Semwal, Alice Ryhl, Miguel Ojeda, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, linux-media, dri-devel, rust-for-linux,
	linux-kernel

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?

> 
> > 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 ;)

> But even in Rust there is still the need to give HW drivers something
> like a template in which is defined how things should be done and
> what things need to be avoided.

see below

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

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. Having some random delay is probably even worse because
the problem is then more difficult to even encounter and thus fix.

> 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! :)

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


P.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] rust: DamFence: Remove warning about unsignaled drop
  2026-11-19  9:58                   ` Philipp Stanner
@ 2026-11-19 10:47                     ` Christian König
  0 siblings, 0 replies; 15+ messages in thread
From: Christian König @ 2026-11-19 10:47 UTC (permalink / raw)
  To: phasta, Danilo Krummrich
  Cc: Sumit Semwal, Alice Ryhl, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, linux-media, dri-devel, rust-for-linux,
	linux-kernel

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.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] rust: DamFence: Remove warning about unsignaled drop
  2026-11-19  9:32                 ` Christian König
  2026-11-19  9:58                   ` Philipp Stanner
@ 2026-11-19 12:35                   ` Danilo Krummrich
  1 sibling, 0 replies; 15+ messages in thread
From: Danilo Krummrich @ 2026-11-19 12:35 UTC (permalink / raw)
  To: Christian König
  Cc: phasta, Sumit Semwal, Alice Ryhl, Miguel Ojeda, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, linux-media, dri-devel, rust-for-linux,
	linux-kernel

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.

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2026-11-19 12:35 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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
2026-11-19 12:35                   ` Danilo Krummrich
2026-11-17 14:06       ` Danilo Krummrich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox