Skip to contentSkip to conversation

rust: scatterlist: tie owned tables to the device's bound lifetime

PATCH In review Danilo Krummrich 3 participants 9 messages Nov 14 – Nov 16

Subsystems (7)
lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

PATCH 1/2 rust: scatterlist: tie owned tables to the device's bound lifetime

Add a lifetime parameter to Owned that ties owned scatter-gather tables to the device's bound scope, ensuring their DMA mappings are released before the device is unbound.

The DMA mapping currently holds a pointer into Owned, requiring the table to stay at a fixed address and its cleanup to be ordered around the device-managed mapping.

Let DmaMappedSgt own RawSGTable and borrow Device<Bound>. Its destructor unmaps the entries before RawSGTable frees them, and Owned releases the backing pages last. This removes the internal pointer and makes the mapping constructor safe without a self-reference.

Carry the device lifetime through Owned and its users. Remove pin initialization from Owned and SGTable, return tables by value, and update the callers accordingly.

---
 drivers/gpu/nova-core/firmware/radix3.rs |  29 +++--
 rust/kernel/scatterlist.rs               | 141 ++++++++---------------
 samples/rust/rust_dma.rs                 |  11 +-
 3 files changed, 62 insertions(+), 119 deletions(-)
drivers/gpu/nova-core/firmware/radix3.rs +13 -16
index 94235bd1209d..a3e43fc8ec86 100644
@@ -35,14 +35,11 @@
 #[pin_data]
 pub(crate) struct Radix3<'a> {
     /// The mapped data.
-    #[pin]
-    data: SGTable<Owned<VVec<u8>>>,
+    data: SGTable<Owned<'a, VVec<u8>>>,
     /// Level 2: one entry per page of `data`.
-    #[pin]
-    level2: SGTable<Owned<VVec<u8>>>,
+    level2: SGTable<Owned<'a, VVec<u8>>>,
     /// Level 1: one entry per page of `level2`.
-    #[pin]
-    level1: SGTable<Owned<VVec<u8>>>,
+    level1: SGTable<Owned<'a, VVec<u8>>>,
     /// Level 0: one page, whose single entry is the DMA address of the first `level1` page.
     level0: Coherent<'a, [u64]>,
     /// Length of `data`, in bytes.
@@ -59,14 +56,14 @@ pub(crate) fn new(
 
         pin_init::pin_init_scope(move || {
             Ok(try_pin_init!(Self {
-                data <- SGTable::new(dev, data, DataDirection::ToDevice, GFP_KERNEL),
-                level2 <- {
-                    build_lvl(&data)
-                        .map(|l2| SGTable::new(dev, l2, DataDirection::ToDevice, GFP_KERNEL))?
+                data: SGTable::new(dev, data, DataDirection::ToDevice, GFP_KERNEL)?,
+                level2: {
+                    let l2 = build_lvl(data)?;
+                    SGTable::new(dev, l2, DataDirection::ToDevice, GFP_KERNEL)?
                 },
-                level1 <- {
-                    build_lvl(&level2)
-                        .map(|l1| SGTable::new(dev, l1, DataDirection::ToDevice, GFP_KERNEL))?
+                level1: {
+                    let l1 = build_lvl(level2)?;
+                    SGTable::new(dev, l1, DataDirection::ToDevice, GFP_KERNEL)?
                 },
                 level0: {
                     // Allocate the level 0 page table as a device-visible DMA object, and map the
@@ -81,7 +78,7 @@ pub(crate) fn new(
                     let mut level0 = CoherentBox::<'_, [u64]>::zeroed_slice(
                         dev,
                         GSP_PAGE_SIZE / size_of::<u64>(),
-                        GFP_KERNEL
+                        GFP_KERNEL,
                     )?;
                     level0[0] = level1_entry_addr.to_le();
 
@@ -105,7 +102,7 @@ pub(crate) fn size(&self) -> usize {
 
 /// Returns the size of the level that maps `sg_table`: one `u64` entry per [`GSP_PAGE_SIZE`]
 /// page of each DMA-mapped region, rounded up to whole pages.
-fn lvl_size(sg_table: &SGTable<Owned<VVec<u8>>>) -> usize {
+fn lvl_size(sg_table: &SGTable<Owned<'_, VVec<u8>>>) -> usize {
     let entries: usize = sg_table
         .iter()
         .map(|sg_entry| usize::from_safe_cast(sg_entry.dma_len()).div_ceil(GSP_PAGE_SIZE))
@@ -122,7 +119,7 @@ fn lvl_size(sg_table: &SGTable<Owned<VVec<u8>>>) -> usize {
 ///
 /// - `ENOMEM` if the level cannot be allocated.
 /// - `EINVAL` if `sg_table` yields more entries than [`lvl_size`] sized the level for.
-fn build_lvl(sg_table: &SGTable<Owned<VVec<u8>>>) -> Result<VVec<u8>> {
+fn build_lvl(sg_table: &SGTable<Owned<'_, VVec<u8>>>) -> Result<VVec<u8>> {
     let mut dst = VVec::<u8>::zeroed(lvl_size(sg_table), GFP_KERNEL).map_err(|_| ENOMEM)?;
     let mut entries = dst.chunks_exact_mut(size_of::<u64>());
 
rust/kernel/scatterlist.rs +46 -95

265 lines not on this page · raw message

samples/rust/rust_dma.rs +3 -8
index ffb693544673..844e137cddc8 100644
@@ -35,8 +35,7 @@
 struct DmaSampleData<'bound> {
     pdev: &'bound pci::Device<Bound>,
     ca: Coherent<'bound, [MyStruct]>,
-    #[pin]
-    sgt: SGTable<Owned<VVec<u8>>>,
+    sgt: SGTable<Owned<'bound, VVec<u8>>>,
 }
 
 const TEST_VALUES: [(u32, u32); 5] = [
@@ -96,13 +95,9 @@ fn probe<'bound>(
             let size = 4 * page::PAGE_SIZE;
             let pages = VVec::with_capacity(size, GFP_KERNEL)?;
 
-            let sgt = SGTable::new(pdev.as_ref(), pages, DataDirection::ToDevice, GFP_KERNEL);
+            let sgt = SGTable::new(pdev.as_ref(), pages, DataDirection::ToDevice, GFP_KERNEL)?;
 
-            Ok(try_pin_init!(Self::Data {
-                pdev,
-                ca,
-                sgt <- sgt,
-            }))
+            Ok(try_pin_init!(Self::Data { pdev, ca, sgt }))
         })
     }
 }

base-commit: 2268cf8e3d7b4192629e4beff4a413efe5501f11

Signature · 1 line
2.56.1
author maintainer
#1 lore raw

PATCH 2/2 gpu: nova-core: remove unnecessary firmware pin-init

Now that SGTable no longer requires pinning, remove the pin-init machinery from Radix3 and GspFirmware.

---
 drivers/gpu/nova-core/firmware/gsp.rs    | 43 +++++--------
 drivers/gpu/nova-core/firmware/radix3.rs | 82 +++++++++++-------------
 drivers/gpu/nova-core/gsp/boot.rs        |  2 +-
 3 files changed, 56 insertions(+), 71 deletions(-)
drivers/gpu/nova-core/firmware/gsp.rs +17 -26
index 341a8b19aa38..ff92a12b2994 100644
@@ -25,10 +25,8 @@
 };
 
 /// The GSP firmware image, its signatures, and the GSP bootloader.
-#[pin_data]
 pub(crate) struct GspFirmware<'a> {
     /// The firmware image and the radix3 table that maps it.
-    #[pin]
     radix3: Radix3<'a>,
     /// Device-mapped GSP signatures matching the GPU's [`Chipset`].
     pub(crate) signatures: Coherent<'a, [u8]>,
@@ -39,34 +37,27 @@ pub(crate) struct GspFirmware<'a> {
 impl<'a> GspFirmware<'a> {
     /// Loads the GSP firmware binaries, map them into `dev`'s address-space, and creates the page
     /// tables expected by the GSP bootloader to load it.
-    pub(crate) fn new(
-        dev: &'a device::Device<device::Bound>,
-        chipset: Chipset,
-    ) -> impl PinInit<Self, Error> + 'a {
-        pin_init::pin_init_scope(move || {
-            let firmware = request_tlv(dev, chipset, "gsp")?;
-            let tlv = Tlv::new(firmware.data())?;
-            dev_dbg!(dev, "loaded gsp firmware v{}\n", tlv.get_string(b"VERS")?);
+    pub(crate) fn new(dev: &'a device::Device<device::Bound>, chipset: Chipset) -> Result<Self> {
+        let firmware = request_tlv(dev, chipset, "gsp")?;
+        let tlv = Tlv::new(firmware.data())?;
+        dev_dbg!(dev, "loaded gsp firmware v{}\n", tlv.get_string(b"VERS")?);
 
-            let size = usize::from_safe_cast(tlv.get_u32(b"SIZE")?);
-            let mut fw_vvec = VVec::zeroed(size, GFP_KERNEL).map_err(|_| ENOMEM)?;
+        let size = usize::from_safe_cast(tlv.get_u32(b"SIZE")?);
+        let mut fw_vvec = VVec::zeroed(size, GFP_KERNEL).map_err(|_| ENOMEM)?;
 
-            let chip_name = chipset.name();
-            let file = tlv.get_string(b"FILE")?;
-            let filename = CString::try_from_fmt(fmt!("nvidia/{chip_name}/gsp/{file}"))?;
-            firmware::request_into_buf(&filename, dev, fw_vvec.as_mut_slice())?;
+        let chip_name = chipset.name();
+        let file = tlv.get_string(b"FILE")?;
+        let filename = CString::try_from_fmt(fmt!("nvidia/{chip_name}/gsp/{file}"))?;
+        firmware::request_into_buf(&filename, dev, fw_vvec.as_mut_slice())?;
 
-            let signatures = Coherent::from_slice(dev, tlv.get_bytes(b"SIGN")?, GFP_KERNEL)?;
+        Ok(Self {
+            radix3: Radix3::new(dev, fw_vvec)?,
+            signatures: Coherent::from_slice(dev, tlv.get_bytes(b"SIGN")?, GFP_KERNEL)?,
+            bootloader: {
+                let bl = request_tlv(dev, chipset, "gsp_bootloader")?;
 
-            Ok(try_pin_init!(Self {
-                radix3 <- Radix3::new(dev, fw_vvec),
-                signatures,
-                bootloader: {
-                    let bl = request_tlv(dev, chipset, "gsp_bootloader")?;
-
-                    RiscvFirmware::new(dev, &bl)?
-                },
-            }))
+                RiscvFirmware::new(dev, &bl)?
+            },
         })
     }
 
drivers/gpu/nova-core/firmware/radix3.rs +38 -44

98 lines not on this page · raw message

drivers/gpu/nova-core/gsp/boot.rs +1 -1
index 4fb1b69ac9d5..ac09b40832aa 100644
@@ -40,7 +40,7 @@ pub(crate) fn boot(
         let dev = pdev.as_ref();
         let hal = super::hal::gsp_hal(chipset);
 
-        let gsp_fw = KBox::pin_init(GspFirmware::new(dev, chipset), GFP_KERNEL)?;
+        let gsp_fw = GspFirmware::new(dev, chipset)?;
 
         self.cmdq
             .send_command_no_wait(commands::SetSystemInfo::new(pdev, chipset))?;
Signature · 1 line
2.56.1
#2 lore raw

Re: [PATCH 2/2] gpu: nova-core: remove unnecessary firmware pin-init

Quoted 28 lines · Danilo Krummrich

Now that SGTable no longer requires pinning, remove the pin-init machinery from Radix3 and GspFirmware.

---
 drivers/gpu/nova-core/firmware/gsp.rs    | 43 +++++--------
 drivers/gpu/nova-core/firmware/radix3.rs | 82 +++++++++++-------------
 drivers/gpu/nova-core/gsp/boot.rs        |  2 +-
 3 files changed, 56 insertions(+), 71 deletions(-)
drivers/gpu/nova-core/firmware/gsp.rs +0 -2
index 341a8b19aa38..ff92a12b2994 100644
@@ -25,10 +25,8 @@
 };
 
 /// The GSP firmware image, its signatures, and the GSP bootloader.
-#[pin_data]
 pub(crate) struct GspFirmware<'a> {
     /// The firmware image and the radix3 table that maps it.
-    #[pin]
     radix3: Radix3<'a>,
     /// Device-mapped GSP signatures matching the GPU's [`Chipset`].
     pub(crate) signatures: Coherent<'a, [u8]>,
@@ -39,34 +37,27 @@ pub(crate) struct GspFirmware<'a> {
 impl<'a> GspFirmware<'a> {
     /// Loads the GSP firmware binaries, map them into `dev`'s address-space, and creates the page

drivers/gpu/nova-core/firmware/gsp.rs pub(crate) struct GspFirmware<'a> { · line 40

Quoted 10 lines
drivers/gpu/nova-core/firmware/gsp.rs +1 -8
     /// tables expected by the GSP bootloader to load it.
-    pub(crate) fn new(
-        dev: &'a device::Device<device::Bound>,
-        chipset: Chipset,
-    ) -> impl PinInit<Self, Error> + 'a {
-        pin_init::pin_init_scope(move || {
-            let firmware = request_tlv(dev, chipset, "gsp")?;
-            let tlv = Tlv::new(firmware.data())?;
-            dev_dbg!(dev, "loaded gsp firmware v{}\n", tlv.get_string(b"VERS")?);
+    pub(crate) fn new(dev: &'a device::Device<device::Bound>, chipset: Chipset) -> Result<Self> {

GspFirmware is 312 bytes, that's not enormous but that's also not small... Shall we keep returning an impl Pininit?

Quoted 26 lines
drivers/gpu/nova-core/firmware/radix3.rs +7 -8
index a3e43fc8ec86..2c8787d5930d 100644
@@ -32,60 +32,54 @@
 };
 
 /// A radix3 page table and the data it maps.
-#[pin_data]
 pub(crate) struct Radix3<'a> {
     /// The mapped data.
-    data: SGTable<Owned<'a, VVec<u8>>>,
-    /// Level 2: one entry per page of `data`.
-    level2: SGTable<Owned<'a, VVec<u8>>>,
-    /// Level 1: one entry per page of `level2`.
-    level1: SGTable<Owned<'a, VVec<u8>>>,
-    /// Level 0: one page, whose single entry is the DMA address of the first `level1` page.
+    _data: SGTable<Owned<'a, VVec<u8>>>,
+    /// Level 2: one entry per page of `_data`.
+    _level2: SGTable<Owned<'a, VVec<u8>>>,
+    /// Level 1: one entry per page of `_level2`.
+    _level1: SGTable<Owned<'a, VVec<u8>>>,
+    /// Level 0: one page, whose single entry is the DMA address of the first `_level1` page.
     level0: Coherent<'a, [u64]>,
-    /// Length of `data`, in bytes.
+    /// Length of `_data`, in bytes.

drivers/gpu/nova-core/firmware/radix3.rs line 50

Quoted 10 lines
drivers/gpu/nova-core/firmware/radix3.rs +1 -4
     size: usize,
 }
 
 impl<'a> Radix3<'a> {
     /// Builds a radix3 page table over `data`, mapped for `dev` to read. May sleep.
-    pub(crate) fn new(
-        dev: &'a device::Device<device::Bound>,
-        data: VVec<u8>,
-    ) -> impl PinInit<Self, Error> + 'a {
+    pub(crate) fn new(dev: &'a device::Device<device::Bound>, data: VVec<u8>) -> Result<Self> {

Similar comment as Radix3 is 216 bytes.

author maintainer
#3 lore raw

Re: [PATCH 2/2] gpu: nova-core: remove unnecessary firmware pin-init

Quoted 4 lines · Alexandre Courbot
Quoted 1 line

+ pub(crate) fn new(dev: &'a device::Device<device::Bound>, chipset: Chipset) -> Result<Self> {

GspFirmware is 312 bytes, that's not enormous but that's also not small... Shall we keep returning an impl Pininit?

I considered that, but it's only ever constructed in Gsp::boot() and does not outlive boot(), so I think it should be fine.

maintainer Reviewed-by
#4 lore raw

Re: [PATCH 2/2] gpu: nova-core: remove unnecessary firmware pin-init

Quoted 8 lines · Danilo Krummrich
Quoted 4 lines · Alexandre Courbot
Quoted 1 line

+ pub(crate) fn new(dev: &'a device::Device<device::Bound>, chipset: Chipset) -> Result<Self> {

GspFirmware is 312 bytes, that's not enormous but that's also not small... Shall we keep returning an impl Pininit?

I considered that, but it's only ever constructed in Gsp::boot() and does not outlive boot(), so I think it should be fine.

Sgtm.

#5 lore raw
In reply to Danilo Krummrich

On Sun Nov 15, 2026 at 8:42 AM JST, Danilo Krummrich wrote:
<...>

Quoted 45 lines
drivers/gpu/nova-core/firmware/radix3.rs +10 -13
index 94235bd1209d..a3e43fc8ec86 100644
@@ -35,14 +35,11 @@
 #[pin_data]
 pub(crate) struct Radix3<'a> {
     /// The mapped data.
-    #[pin]
-    data: SGTable<Owned<VVec<u8>>>,
+    data: SGTable<Owned<'a, VVec<u8>>>,
     /// Level 2: one entry per page of `data`.
-    #[pin]
-    level2: SGTable<Owned<VVec<u8>>>,
+    level2: SGTable<Owned<'a, VVec<u8>>>,
     /// Level 1: one entry per page of `level2`.
-    #[pin]
-    level1: SGTable<Owned<VVec<u8>>>,
+    level1: SGTable<Owned<'a, VVec<u8>>>,
     /// Level 0: one page, whose single entry is the DMA address of the first `level1` page.
     level0: Coherent<'a, [u64]>,
     /// Length of `data`, in bytes.
@@ -59,14 +56,14 @@ pub(crate) fn new(
 
         pin_init::pin_init_scope(move || {
             Ok(try_pin_init!(Self {
-                data <- SGTable::new(dev, data, DataDirection::ToDevice, GFP_KERNEL),
-                level2 <- {
-                    build_lvl(&data)
-                        .map(|l2| SGTable::new(dev, l2, DataDirection::ToDevice, GFP_KERNEL))?
+                data: SGTable::new(dev, data, DataDirection::ToDevice, GFP_KERNEL)?,
+                level2: {
+                    let l2 = build_lvl(data)?;
+                    SGTable::new(dev, l2, DataDirection::ToDevice, GFP_KERNEL)?
                 },
-                level1 <- {
-                    build_lvl(&level2)
-                        .map(|l1| SGTable::new(dev, l1, DataDirection::ToDevice, GFP_KERNEL))?
+                level1: {
+                    let l1 = build_lvl(level2)?;
+                    SGTable::new(dev, l1, DataDirection::ToDevice, GFP_KERNEL)?
                 },
                 level0: {
                     // Allocate the level 0 page table as a device-visible DMA object, and map the

drivers/gpu/nova-core/firmware/radix3.rs pub(crate) fn new( · line 81

Quoted 5 lines
drivers/gpu/nova-core/firmware/radix3.rs +1 -1
@@ -81,7 +78,7 @@ pub(crate) fn new(
                     let mut level0 = CoherentBox::<'_, [u64]>::zeroed_slice(
                         dev,
                         GSP_PAGE_SIZE / size_of::<u64>(),
-                        GFP_KERNEL
+                        GFP_KERNEL,

Stray hunk? Maybe we can move it to patch 2 since it reformats that part anyway.

Quoted 65 lines
drivers/gpu/nova-core/firmware/radix3.rs +2 -2
                     )?;
                     level0[0] = level1_entry_addr.to_le();
 
@@ -105,7 +102,7 @@ pub(crate) fn size(&self) -> usize {
 
 /// Returns the size of the level that maps `sg_table`: one `u64` entry per [`GSP_PAGE_SIZE`]
 /// page of each DMA-mapped region, rounded up to whole pages.
-fn lvl_size(sg_table: &SGTable<Owned<VVec<u8>>>) -> usize {
+fn lvl_size(sg_table: &SGTable<Owned<'_, VVec<u8>>>) -> usize {
     let entries: usize = sg_table
         .iter()
         .map(|sg_entry| usize::from_safe_cast(sg_entry.dma_len()).div_ceil(GSP_PAGE_SIZE))
@@ -122,7 +119,7 @@ fn lvl_size(sg_table: &SGTable<Owned<VVec<u8>>>) -> usize {
 ///
 /// - `ENOMEM` if the level cannot be allocated.
 /// - `EINVAL` if `sg_table` yields more entries than [`lvl_size`] sized the level for.
-fn build_lvl(sg_table: &SGTable<Owned<VVec<u8>>>) -> Result<VVec<u8>> {
+fn build_lvl(sg_table: &SGTable<Owned<'_, VVec<u8>>>) -> Result<VVec<u8>> {
     let mut dst = VVec::<u8>::zeroed(lvl_size(sg_table), GFP_KERNEL).map_err(|_| ENOMEM)?;
     let mut entries = dst.chunks_exact_mut(size_of::<u64>());
 
rust/kernel/scatterlist.rs +3 -6
index 6379a8748117..93f431d7f548 100644
@@ -19,7 +19,8 @@
 //! The main entry point is the [`SGTable`] type, which represents a complete scatter-gather table.
 //! It can be either:
 //!
-//! - An owned table ([`SGTable<Owned<P>>`]), created from a Rust memory buffer (e.g., [`VVec`]).
+//! - An owned table ([`SGTable<Owned<'a, P>>`]), created from a Rust memory buffer (e.g.,
+//!   [`VVec`]) and tied to the [`Device<Bound>`] lifetime.
 //!   This type manages the allocation of the `struct sg_table`, the DMA mapping of the buffer, and
 //!   the automatic cleanup of all resources.
 //! - A borrowed reference (&[`SGTable`]), which provides safe, read-only access to a table that was
@@ -33,15 +34,13 @@
     alloc::allocator::VmallocPageIter,
     bindings,
     device::{Bound, Device},
-    devres::Devres,
     dma,
     error,
     page,
     prelude::*,
-    sync::aref::ARef,
     types::Opaque, //
 };
-use core::{ops::Deref, ptr::NonNull};
+use core::ops::Deref;
 
 /// A single entry in a scatter-gather list.
 ///
@@ -123,9 +122,7 @@ unsafe impl Sync for Borrowed {}
 ///
 /// All [`SGTable`] variants can be iterated over the individual [`SGEntry`]s.
 #[repr(transparent)]
-#[pin_data]
 pub struct SGTable<T: private::Sealed = Borrowed> {
-    #[pin]
     inner: T,
 }
 
@@ -177,63 +174,41 @@ pub fn iter(&self) -> SGTableIter<'_> {
 ///
 /// # Invariants

rust/kernel/scatterlist.rs pub fn iter(&self) -> SGTableIter<'_> { · line 180

Quoted 10 lines
rust/kernel/scatterlist.rs +3 -5
 ///
-/// - `sgt` is a valid pointer to a `struct sg_table` for the entire lifetime of the
-///   [`DmaMappedSgt`].
 /// - `sgt` is always DMA mapped.
-struct DmaMappedSgt {
-    sgt: NonNull<bindings::sg_table>,
-    dev: ARef<Device>,
+struct DmaMappedSgt<'a> {
+    dev: &'a Device<Bound>,
+    sgt: RawSGTable,

The RawSGTable documentation still mentions Owned. We could adapt it or (maybe better) just drop that sentence.

Quoted 66 lines
rust/kernel/scatterlist.rs +11 -31
     dir: dma::DataDirection,
 }
 
-// SAFETY: `DmaMappedSgt` can be sent to any task.
-unsafe impl Send for DmaMappedSgt {}
-
-// SAFETY: `DmaMappedSgt` has no interior mutability and can be accessed concurrently.
-unsafe impl Sync for DmaMappedSgt {}
-
-impl DmaMappedSgt {
-    /// # Safety
-    ///
-    /// - `sgt` must be a valid pointer to a `struct sg_table` for the entire lifetime of the
-    ///   returned [`DmaMappedSgt`].
-    /// - The caller must guarantee that `sgt` remains DMA mapped for the entire lifetime of
-    ///   [`DmaMappedSgt`].
-    unsafe fn new(
-        sgt: NonNull<bindings::sg_table>,
-        dev: &Device<Bound>,
-        dir: dma::DataDirection,
-    ) -> Result<Self> {
+impl<'a> DmaMappedSgt<'a> {
+    /// Takes ownership of `sgt` and maps it for DMA.
+    fn new(dev: &'a Device<Bound>, sgt: RawSGTable, dir: dma::DataDirection) -> Result<Self> {
         // SAFETY:
         // - `dev.as_raw()` is a valid pointer to a `struct device`, which is guaranteed to be
         //   bound to a driver for the duration of this call.
-        // - `sgt` is a valid pointer to a `struct sg_table`.
+        // - `sgt.as_raw()` is a valid pointer to a `struct sg_table`.
         error::to_result(unsafe {
-            bindings::dma_map_sgtable(dev.as_raw(), sgt.as_ptr(), dir.into(), 0)
+            bindings::dma_map_sgtable(dev.as_raw(), sgt.as_raw(), dir.into(), 0)
         })?;
 
-        // INVARIANT: By the safety requirements of this function it is guaranteed that `sgt` is
-        // valid for the entire lifetime of this object instance.
-        Ok(Self {
-            sgt,
-            dev: dev.into(),
-            dir,
-        })
+        // INVARIANT: `sgt` was successfully DMA mapped and is owned by this object until drop.
+        Ok(Self { dev, sgt, dir })
     }
 }
 
-impl Drop for DmaMappedSgt {
+impl Drop for DmaMappedSgt<'_> {
     #[inline]
     fn drop(&mut self) {
         // SAFETY:
         // - `self.dev.as_raw()` is a pointer to a valid `struct device`.
         // - `self.dev` is the same device the mapping has been created for in `Self::new()`.
-        // - `self.sgt.as_ptr()` is a valid pointer to a `struct sg_table` by the type invariants
-        //   of `Self`.
+        // - `self.sgt.as_raw()` is a valid pointer to a `struct sg_table` for the lifetime of
+        //   `self`.
         // - `self.dir` is the same `dma::DataDirection` the mapping has been created with in
         //   `Self::new()`.
         unsafe {
-            bindings::dma_unmap_sgtable(self.dev.as_raw(), self.sgt.as_ptr(), self.dir.into(), 0)
+            bindings::dma_unmap_sgtable(self.dev.as_raw(), self.sgt.as_raw(), self.dir.into(), 0)
         };
     }
 }

rust/kernel/scatterlist.rs fn drop(&mut self) { · line 285

Quoted 3 lines
rust/kernel/scatterlist.rs +0 -0
@@ -308,38 +283,27 @@ fn drop(&mut self) {
 ///
 /// - The backing memory pages.
 /// - The `struct sg_table` allocation (`sgt`).

sgt is removed by this patch.

<...>

samples/rust/rust_dma.rs line 38

Quoted 11 lines
samples/rust/rust_dma.rs +1 -2
index ffb693544673..844e137cddc8 100644
@@ -35,8 +35,7 @@
 struct DmaSampleData<'bound> {
     pdev: &'bound pci::Device<Bound>,
     ca: Coherent<'bound, [MyStruct]>,
-    #[pin]
-    sgt: SGTable<Owned<VVec<u8>>>,
+    sgt: SGTable<Owned<'bound, VVec<u8>>>,

Since there is no more pin in DmaSampleData, can we also remove its pinned_drop attribute?

author maintainer
#6 lore raw
Quoted 9 lines · Alexandre Courbot

drivers/gpu/nova-core/firmware/radix3.rs pub(crate) fn new( · line 81

Quoted 6 lines
drivers/gpu/nova-core/firmware/radix3.rs +1 -1
@@ -81,7 +78,7 @@ pub(crate) fn new(
                     let mut level0 = CoherentBox::<'_, [u64]>::zeroed_slice(
                         dev,
                         GSP_PAGE_SIZE / size_of::<u64>(),
-                        GFP_KERNEL
+                        GFP_KERNEL,

Stray hunk? Maybe we can move it to patch 2 since it reformats that part anyway.

I noticed this before sending too, but for some odd reason rustfmt insists on this with this change.

When I noticed I was 'whatever', but now I got curious. It happens when

	level2 <- {
	    build_lvl(&data)
	        .map(|l2| SGTable::new(dev, l2, DataDirection::ToDevice, GFP_KERNEL))?
	},

is changed to

	level2: {
	    let l2 = build_lvl(data)?;
	    SGTable::new(dev, l2, DataDirection::ToDevice, GFP_KERNEL)?
	},
Quoted 6 lines

rust/kernel/scatterlist.rs fn drop(&mut self) { · line 285

Quoted 4 lines
rust/kernel/scatterlist.rs +0 -0
@@ -308,38 +283,27 @@ fn drop(&mut self) {
 ///
 /// - The backing memory pages.
 /// - The `struct sg_table` allocation (`sgt`).

sgt is removed by this patch.

Good catch, I will remove this and the other one.

Quoted 15 lines

<...>

samples/rust/rust_dma.rs line 38

Quoted 11 lines
samples/rust/rust_dma.rs +1 -2
index ffb693544673..844e137cddc8 100644
@@ -35,8 +35,7 @@
 struct DmaSampleData<'bound> {
     pdev: &'bound pci::Device<Bound>,
     ca: Coherent<'bound, [MyStruct]>,
-    #[pin]
-    sgt: SGTable<Owned<VVec<u8>>>,
+    sgt: SGTable<Owned<'bound, VVec<u8>>>,

Since there is no more pin in DmaSampleData, can we also remove its pinned_drop attribute?

Yes, I left it as is intentionally though as this patch is purely a mechanical conversion. I can send another one, analogous to the nova-core one.

reviewer
#7 lore raw
Quoted 21 lines · Danilo Krummrich
Quoted 9 lines · Alexandre Courbot

drivers/gpu/nova-core/firmware/radix3.rs pub(crate) fn new( · line 81

Quoted 6 lines
drivers/gpu/nova-core/firmware/radix3.rs +1 -1
@@ -81,7 +78,7 @@ pub(crate) fn new(
                     let mut level0 = CoherentBox::<'_, [u64]>::zeroed_slice(
                         dev,
                         GSP_PAGE_SIZE / size_of::<u64>(),
-                        GFP_KERNEL
+                        GFP_KERNEL,

Stray hunk? Maybe we can move it to patch 2 since it reformats that part anyway.

I noticed this before sending too, but for some odd reason rustfmt insists on this with this change.

When I noticed I was 'whatever', but now I got curious. It happens when

	level2 <- {
	    build_lvl(&data)
	        .map(|l2| SGTable::new(dev, l2, DataDirection::ToDevice, GFP_KERNEL))?
	},
Quoted 7 lines

is changed to

	level2: {
	    let l2 = build_lvl(data)?;
	    SGTable::new(dev, l2, DataDirection::ToDevice, GFP_KERNEL)?
	},

When you remove all <- rustfmt starts formatting macros again because it sees the macro invocation being a valid Rust expression.

I think you can also just remove the try_pin_init! here completely?

Best,
Gary

author maintainer
#8 lore raw
Quoted 24 lines · Gary Guo
Quoted 23 lines · Danilo Krummrich
Quoted 9 lines · Alexandre Courbot

drivers/gpu/nova-core/firmware/radix3.rs pub(crate) fn new( · line 81

Quoted 6 lines
drivers/gpu/nova-core/firmware/radix3.rs +1 -1
@@ -81,7 +78,7 @@ pub(crate) fn new(
                     let mut level0 = CoherentBox::<'_, [u64]>::zeroed_slice(
                         dev,
                         GSP_PAGE_SIZE / size_of::<u64>(),
-                        GFP_KERNEL
+                        GFP_KERNEL,

Stray hunk? Maybe we can move it to patch 2 since it reformats that part anyway.

I noticed this before sending too, but for some odd reason rustfmt insists on this with this change.

When I noticed I was 'whatever', but now I got curious. It happens when

	level2 <- {
	    build_lvl(&data)
	        .map(|l2| SGTable::new(dev, l2, DataDirection::ToDevice, GFP_KERNEL))?
	},

is changed to

Quoted 7 lines
Quoted 5 lines
	level2: {
	    let l2 = build_lvl(data)?;
	    SGTable::new(dev, l2, DataDirection::ToDevice, GFP_KERNEL)?
	},

When you remove all <- rustfmt starts formatting macros again because it sees the macro invocation being a valid Rust expression.

Not exactly, that's why I picked level2. If you remove the <- from level1, but not level2 it doesn't format, but if you do it the other way around, it does.

Quoted 1 line

I think you can also just remove the try_pin_init! here completely?

I remove all the pin-init remains in patch 2 as it propagates a bit, keeping this one purely mechanical.