From: "Gary Guo" <gary@garyguo.net>
To: "Danilo Krummrich" <dakr@kernel.org>, "Gary Guo" <gary@garyguo.net>
Cc: <abdiel.janulgue@gmail.com>, <daniel.almeida@collabora.com>,
<robin.murphy@arm.com>, <a.hindborg@kernel.org>,
<gregkh@linuxfoundation.org>, <rafael@kernel.org>,
<aliceryhl@google.com>, <acourbot@nvidia.com>, <ojeda@kernel.org>,
<boqun@kernel.org>, <bjorn3_gh@protonmail.com>,
<lossin@kernel.org>, <tmgross@umich.edu>, <tamird@kernel.org>,
<work@onurozkan.dev>, <mmaurer@google.com>,
<driver-core@lists.linux.dev>, <nova-gpu@lists.linux.dev>,
<dri-devel@lists.freedesktop.org>, <linux-kernel@vger.kernel.org>,
<rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH 4/4] rust: dma: tie Coherent and CoherentBox to the device's bound lifetime
Date: Thu, 03 Sep 2026 16:42:03 +0100 [thread overview]
Message-ID: <DL5SNOAXWIPE.1WPMZGTWV7DZT@garyguo.net> (raw)
In-Reply-To: <DL5S8GFKETAW.1UD50XK77ZPA6@kernel.org>
On Thu Sep 3, 2026 at 4:22 PM BST, Danilo Krummrich wrote:
> On Thu Sep 3, 2026 at 3:20 PM CEST, Gary Guo wrote:
>> The rust/kernel code looks good to me. Haven't checked nova part in detail, but
>> it looks like a mechanical conversion, so would be fine if it builds.
>>
>> Reviewed-by: Gary Guo <gary@garyguo.net>
>>
>> Sashiko points out that the `Coherent` could be leaked -- what's the implication
>> when that happens? I think it's not going to be as problematic like
>> registrations because coherent allocation carries no callbacks, so we probably
>> don't need this to be unsafe, but I do wonder how'd DMA subsystem handle this.
>
> The implication if leaked is the same as if it is kept alive past driver unbind,
> which is why I changed the TODO comment accordingly in the hunk below.
>
> If you look for a specific example, there's [1] for instance. So, it is
> problematic, which is why I added the TODO comment back then.
Right, so despite that `Coherent` itself not having callbacks, IOMMU side can
have callback that associated with `Coherent` instance and thus require device
to be bound.
>
> But, we did accept this soundness hole from the get-go for both, keeping a
> coherent allocation alive beyond driver unbind and for leaking it.
>
> With this patch it is now impossible to keep it alive beyond driver unbind, so
> switching to unsafe now would be a bit odd. :)
No, I don't want this to be unsafe.
I just wonder if it could be made not unsound at all. I think in this case at
least it is possible with a revocation mechanism -- the `Coherent` itself is
just a piece of memory and does not reference other resoruces, so the
destructing it late does not matter (unlike registration). So at least it is
*possible* to close the hole.
> (The fact that we did accept this for coherent allocations is also one reason
> why I was recently arguing that we can also make the forget() issue an accepted
> soundness hole for registrations.)
>
For registration because it references other resources so I think we cannot
close the hole with changes internal to each subsystem.
Best,
Gary
> [1] https://lore.kernel.org/all/6a7910da.9c11d2ce.289b96.00da.GAE@google.com/
>
> @@ -588,26 +587,20 @@ fn from(value: CoherentBox<T>) -> Self {
> /// to an allocated region of coherent memory and `dma_addr` is the DMA address base of the
> /// region.
> /// - The size in bytes of the allocation is equal to size information via pointer.
> -// TODO
> //
> -// DMA allocations potentially carry device resources (e.g.IOMMU mappings), hence for soundness
> -// reasons DMA allocation would need to be embedded in a `Devres` container, in order to ensure
> -// that device resources can never survive device unbind.
> -//
> -// However, it is neither desirable nor necessary to protect the allocated memory of the DMA
> -// allocation from surviving device unbind; it would require RCU read side critical sections to
> -// access the memory, which may require subsequent unnecessary copies.
> -//
> -// Hence, find a way to revoke the device resources of a `Coherent`, but not the
> -// entire `Coherent` including the allocated memory itself.
> -pub struct Coherent<T: KnownSize + ?Sized> {
> - dev: ARef<device::Device>,
> +// The lifetime parameter ties DMA allocations to the device's bound scope, ensuring they are freed
> +// before the device is unbound under normal circumstances. However, if a `Coherent` is leaked (e.g.
> +// via `mem::forget`), device resources such as IOMMU mappings will not be released. Making all
> +// constructors `unsafe` to prevent this is considered too restrictive for the common case; this
> +// soundness hole is accepted for now.
> +pub struct Coherent<'a, T: KnownSize + ?Sized> {
> + dev: &'a device::Device<Bound>,
> dma_addr: DmaAddress,
> cpu_addr: NonNull<T>,
> dma_attrs: Attrs,
> }
prev parent reply other threads:[~2026-09-03 15:42 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-30 19:37 [PATCH 0/4] rust: dma: tie DMA allocations to the device's bound lifetime Danilo Krummrich
2026-08-30 19:37 ` [PATCH 1/4] rust: debugfs: drop 'static bound from ScopedDir file creation methods Danilo Krummrich
2026-08-30 19:53 ` sashiko-bot
2026-09-03 13:12 ` Gary Guo
2026-09-03 15:07 ` Danilo Krummrich
2026-09-03 15:16 ` Gary Guo
2026-08-30 19:37 ` [PATCH 2/4] rust: dma: tie CoherentHandle to the device's bound lifetime Danilo Krummrich
2026-09-03 13:12 ` Gary Guo
2026-08-30 19:37 ` [PATCH 3/4] samples: rust_dma: separate driver type from driver data Danilo Krummrich
2026-08-30 19:57 ` sashiko-bot
2026-09-03 13:13 ` Gary Guo
2026-08-30 19:37 ` [PATCH 4/4] rust: dma: tie Coherent and CoherentBox to the device's bound lifetime Danilo Krummrich
2026-08-30 19:47 ` sashiko-bot
2026-09-03 13:20 ` Gary Guo
2026-09-03 15:22 ` Danilo Krummrich
2026-09-03 15:42 ` Gary Guo [this message]
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=DL5SNOAXWIPE.1WPMZGTWV7DZT@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=abdiel.janulgue@gmail.com \
--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=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=mmaurer@google.com \
--cc=nova-gpu@lists.linux.dev \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=robin.murphy@arm.com \
--cc=rust-for-linux@vger.kernel.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