mirror of https://lore.kernel.org/lkml/
 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; 5+ 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	[flat|nested] 5+ 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; 5+ 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] 5+ 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; 5+ 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] 5+ 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
  0 siblings, 1 reply; 5+ 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] 5+ 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
  0 siblings, 0 replies; 5+ 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] 5+ messages in thread

end of thread, other threads:[~2026-11-17 10:54 UTC | newest]

Thread overview: 5+ 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

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®