From: "Danilo Krummrich" <dakr@kernel.org>
To: "Daniel Almeida" <daniel.almeida@collabora.com>
Cc: <akpm@linux-foundation.org>, <ojeda@kernel.org>,
<alex.gaynor@gmail.com>, <boqun.feng@gmail.com>,
<gary@garyguo.net>, <bjorn3_gh@protonmail.com>,
<lossin@kernel.org>, <a.hindborg@kernel.org>,
<aliceryhl@google.com>, <tmgross@umich.edu>,
<abdiel.janulgue@gmail.com>, <acourbot@nvidia.com>,
<jgg@ziepe.ca>, <lyude@redhat.com>, <robin.murphy@arm.com>,
<rust-for-linux@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 2/4] rust: scatterlist: Add type-state abstraction for sg_table
Date: Wed, 20 Aug 2025 20:59:10 +0200 [thread overview]
Message-ID: <DC7HI4RCMJWG.12GHJUGAYZ3TA@kernel.org> (raw)
In-Reply-To: <70E4B722-E1A0-4D5C-A60B-24F8CD5C247D@collabora.com>
On Wed Aug 20, 2025 at 7:08 PM CEST, Daniel Almeida wrote:
>> +/// A scatter-gather table.
>> +///
>> +/// This struct is a wrapper around the kernel's `struct sg_table`. It manages a list of DMA-mapped
>> +/// memory segments that can be passed to a device for I/O operations.
>> +///
>> +/// The generic parameter `T` is used as a type state to distinguish between owned and borrowed
>> +/// tables.
>> +///
>> +/// - [`SGTable<Owned>`]: An owned table created and managed entirely by Rust code. It handles
>> +/// allocation, DMA mapping, and cleanup of all associated resources. See [`SGTable::new`].
>> +/// - [`SGTable<Borrowed>`} (or simply [`SGTable`]): Represents a table whose lifetime is managed
>> +/// externally. It can be used safely via a borrowed reference `&'a SGTable`, where `'a` is the
>> +/// external lifetime.
>> +///
>> +/// All [`SGTable`] variants can be iterated over the individual [`SGEntry`]s.
>> +#[repr(transparent)]
>> +#[pin_data]
>> +pub struct SGTable<T: private::Sealed = Borrowed> {
>
> Am I the only one that think we should have an actual trait here instead of
> using private::Sealed directly?
I don't know. :)
I think this case perfectly fits the Sealed pattern. There isn't any semantics
behind that, other than "we want that only the sealed ones work".
>
>> + #[pin]
>> + inner: T,
>> +}
>> +
>> +impl SGTable {
>> + /// Creates a borrowed `&'a SGTable` from a raw `struct sg_table` pointer.
>> + ///
>> + /// This allows safe access to an `sg_table` that is managed elsewhere (for example, in C code).
>> + ///
>> + /// # Safety
>> + ///
>> + /// Callers must ensure that the `struct sg_table` pointed to by `ptr` is valid for the entire
>> + /// lifetime of `'a`.
>> + pub unsafe fn as_ref<'a>(ptr: *mut bindings::sg_table) -> &'a Self {
>> + // SAFETY: The safety requirements of this function guarantee that `ptr` is a valid pointer
>> + // to a `struct sg_table` for the duration of `'a`.
>> + unsafe { &*ptr.cast() }
>> + }
>> +
>> + fn as_raw(&self) -> *mut bindings::sg_table {
>> + self.inner.0.get()
>> + }
>> +
>> + fn as_iter(&self) -> SGTableIter<'_> {
>
> Perhaps just "iter()” ?
No strong opinition, I'm fine with either.
>
>> + // SAFETY: `self.as_raw()` is a valid pointer to a `struct sg_table`.
>> + let ptr = unsafe { (*self.as_raw()).sgl };
>> +
>> + // SAFETY: `ptr` is guaranteed to be a valid pointer to a `struct scatterlist`.
>> + let pos = Some(unsafe { SGEntry::as_ref(ptr) });
>> +
>> + SGTableIter { pos }
>> + }
>> +}
>> +
>> +/// # Invariants
>> +///
>> +/// `sgt` is a valid pointer to a `struct sg_table` for the entire lifetime of an [`DmaMapSgt`].
>> +struct DmaMapSgt {
>
> This is private, but some extra docs here wouldn’t hurt?
The type just represents the DMA mapping state of the SGT, i.e. exists
corresponds to is mapped, is dropped corresponds to is unmapped.
I can probably add a few lines if it helps.
>> +impl<P> Owned<P>
>> +where
>> + for<'a> P: page::AsPageIter<Iter<'a> = VmallocPageIter<'a>> + 'static,
>> +{
>> + fn new(
>> + dev: &Device<Bound>,
>> + mut pages: P,
>> + dir: dma::DataDirection,
>> + flags: alloc::Flags,
>> + ) -> Result<impl PinInit<Self, Error> + use<'_, P>> {
>
> I confess I have no idea what “use<‘_, P>” is.
It's a feature called #![feature(precise_capturing)], which, unfortunately, does
not exist in 1.78, so I removed this syntax in v2. Luckly, we don't actually
need it in this case.
next prev parent reply other threads:[~2025-08-20 18:59 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-15 17:10 [PATCH 0/4] Rust infrastructure for sg_table and scatterlist Danilo Krummrich
2025-08-15 17:10 ` [PATCH 1/4] rust: dma: implement DataDirection Danilo Krummrich
2025-08-18 9:34 ` Alice Ryhl
2025-08-18 11:27 ` Danilo Krummrich
2025-08-18 11:56 ` Miguel Ojeda
2025-08-18 12:24 ` Miguel Ojeda
2025-08-18 20:42 ` Danilo Krummrich
2025-08-18 12:22 ` Alice Ryhl
2025-08-18 12:57 ` Danilo Krummrich
2025-08-18 14:00 ` Alice Ryhl
2025-08-18 17:23 ` Danilo Krummrich
2025-08-18 18:47 ` Alice Ryhl
2025-08-18 21:03 ` Danilo Krummrich
2025-08-20 13:17 ` Daniel Almeida
2025-08-20 13:40 ` Danilo Krummrich
2025-08-15 17:10 ` [PATCH 2/4] rust: scatterlist: Add type-state abstraction for sg_table Danilo Krummrich
2025-08-18 9:52 ` Alice Ryhl
2025-08-18 11:16 ` Danilo Krummrich
2025-08-18 12:21 ` Danilo Krummrich
2025-08-18 13:12 ` Danilo Krummrich
2025-08-18 12:27 ` Alice Ryhl
2025-08-18 12:37 ` Danilo Krummrich
2025-08-20 17:08 ` Daniel Almeida
2025-08-20 18:59 ` Danilo Krummrich [this message]
2025-08-15 17:10 ` [PATCH 3/4] samples: rust: dma: add sample code for SGTable Danilo Krummrich
2025-08-15 17:10 ` [PATCH 4/4] MAINTAINERS: rust: dma: add scatterlist files 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=DC7HI4RCMJWG.12GHJUGAYZ3TA@kernel.org \
--to=dakr@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=abdiel.janulgue@gmail.com \
--cc=acourbot@nvidia.com \
--cc=akpm@linux-foundation.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=jgg@ziepe.ca \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=ojeda@kernel.org \
--cc=robin.murphy@arm.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.