Skip to contentSkip to conversation

gpu: nova-core: use a single try_pin_init!() block in probe()

PATCH Applied Danilo Krummrich 5 participants 13 messages Sep 27 – Sep 29

Subsystems (6)
lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

PATCH 1/1 gpu: nova-core: use a single try_pin_init!() block in probe()

Just like in Gpu::new() a single try_pin_init!() block makes the code more readable.

Besides that, it prepares the code for a proper PCI device enable guard that we will get soon.

---
 drivers/gpu/nova-core/driver.rs | 105 ++++++++++++++++----------------
 1 file changed, 54 insertions(+), 51 deletions(-)
drivers/gpu/nova-core/driver.rs +54 -51
index bdaed5408a00..623d59dcde6e 100644
@@ -100,57 +100,60 @@ fn probe<'bound>(
         pdev: &'bound pci::Device<Core<'_>>,
         _info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
-        pin_init::pin_init_scope(move || {
-            dev_dbg!(pdev, "Probe Nova Core GPU driver.\n");
-
-            pdev.enable_device_mem()?;
-            pdev.set_master();
-
-            Ok(try_pin_init!(NovaCore {
-                bar: pdev.iomap_region_sized::<BAR0_SIZE>(0, c"nova-core/bar0")?,
-                bar1: {
-                    let bar1_idx = bar1_resource_index(pdev)?;
-                    pdev.iomap_region(bar1_idx, c"nova-core/bar1")?
-                },
-                // TODO: Use self-referential pin-init syntax once available.
-                gpu <- Gpu::new(
-                    pdev,
-                    // SAFETY: `bar` is initialized above, pinned, and outlives `gpu`.
-                    unsafe { &*core::ptr::from_ref(bar) },
-                    // SAFETY: `bar1` is initialized above, pinned, and outlives `gpu`.
-                    unsafe { &*core::ptr::from_ref(bar1) },
-                ).pin_chain(|_gpu| {
-                    #[cfg(CONFIG_NOVA_CORE_SELFTESTS)]
-                    _gpu.run_selftests(pdev);
-                    Ok(())
-                }),
-                _reg: {
-                    // TODO: Use `&gpu` self-referential pin-init syntax once available.
-                    //
-                    // SAFETY: `gpu` is initialized before this expression is evaluated
-                    // (`try_pin_init!()` initializes fields in initializer order), lives at
-                    // a pinned stable address, and is dropped after `_reg` (struct field
-                    // drop order).
-                    let gpu = unsafe {
-                        Pin::new_unchecked(&*core::ptr::from_ref(gpu.as_ref().get_ref()))
-                    };
-
-                    // SAFETY: `NovaCore` is dropped when the device is unbound;
-                    // i.e. `mem::forget()` is never called on it.
-                    unsafe {
-                        auxiliary::Registration::new_with_lt(
-                            pdev.as_ref(),
-                            c"nova-drm",
-                            // TODO[XARR]: Use XArray or perhaps IDA for proper ID
-                            // allocation/recycling. For now, use a simple atomic counter that
-                            // never recycles IDs.
-                            AUXILIARY_ID_COUNTER.fetch_add(1, Relaxed),
-                            crate::MODULE_NAME,
-                            NovaCoreApi { gpu, pdev },
-                        )?
-                    }
-                },
-            }))
+        dev_dbg!(pdev, "Probe Nova Core GPU driver.\n");
+
+        try_pin_init!(NovaCore {
+            _: {
+                pdev.enable_device_mem()?;
+                pdev.set_master();
+            },
+
+            bar: pdev.iomap_region_sized::<BAR0_SIZE>(0, c"nova-core/bar0")?,
+
+            bar1: {
+                let bar1_idx = bar1_resource_index(pdev)?;
+                pdev.iomap_region(bar1_idx, c"nova-core/bar1")?
+            },
+
+            // TODO: Use self-referential pin-init syntax once available.
+            gpu <- Gpu::new(
+                pdev,
+                // SAFETY: `bar` is initialized above, pinned, and outlives `gpu`.
+                unsafe { &*core::ptr::from_ref(bar) },
+                // SAFETY: `bar1` is initialized above, pinned, and outlives `gpu`.
+                unsafe { &*core::ptr::from_ref(bar1) },
+            ).pin_chain(|_gpu| {
+                #[cfg(CONFIG_NOVA_CORE_SELFTESTS)]
+                _gpu.run_selftests(pdev);
+                Ok(())
+            }),
+
+            _reg: {
+                // TODO: Use `&gpu` self-referential pin-init syntax once available.
+                //
+                // SAFETY: `gpu` is initialized before this expression is evaluated
+                // (`try_pin_init!()` initializes fields in initializer order), lives at
+                // a pinned stable address, and is dropped after `_reg` (struct field
+                // drop order).
+                let gpu = unsafe {
+                    Pin::new_unchecked(&*core::ptr::from_ref(gpu.as_ref().get_ref()))
+                };
+
+                // SAFETY: `NovaCore` is dropped when the device is unbound;
+                // i.e. `mem::forget()` is never called on it.
+                unsafe {
+                    auxiliary::Registration::new_with_lt(
+                        pdev.as_ref(),
+                        c"nova-drm",
+                        // TODO[XARR]: Use XArray or perhaps IDA for proper ID
+                        // allocation/recycling. For now, use a simple atomic counter that
+                        // never recycles IDs.
+                        AUXILIARY_ID_COUNTER.fetch_add(1, Relaxed),
+                        crate::MODULE_NAME,
+                        NovaCoreApi { gpu, pdev },
+                    )?
+                }
+            },
         })
     }
 }

base-commit: 10a6623a24a85708650efad7be15182289403cd7

Signature · 1 line
2.55.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 61 lines · Danilo Krummrich

Just like in Gpu::new() a single try_pin_init!() block makes the code more readable.

Besides that, it prepares the code for a proper PCI device enable guard that we will get soon.

---
 drivers/gpu/nova-core/driver.rs | 105 ++++++++++++++++----------------
 1 file changed, 54 insertions(+), 51 deletions(-)
drivers/gpu/nova-core/driver.rs +0 -42
index bdaed5408a00..623d59dcde6e 100644
@@ -100,57 +100,60 @@ fn probe<'bound>(
         pdev: &'bound pci::Device<Core<'_>>,
         _info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
-        pin_init::pin_init_scope(move || {
-            dev_dbg!(pdev, "Probe Nova Core GPU driver.\n");
-
-            pdev.enable_device_mem()?;
-            pdev.set_master();
-
-            Ok(try_pin_init!(NovaCore {
-                bar: pdev.iomap_region_sized::<BAR0_SIZE>(0, c"nova-core/bar0")?,
-                bar1: {
-                    let bar1_idx = bar1_resource_index(pdev)?;
-                    pdev.iomap_region(bar1_idx, c"nova-core/bar1")?
-                },
-                // TODO: Use self-referential pin-init syntax once available.
-                gpu <- Gpu::new(
-                    pdev,
-                    // SAFETY: `bar` is initialized above, pinned, and outlives `gpu`.
-                    unsafe { &*core::ptr::from_ref(bar) },
-                    // SAFETY: `bar1` is initialized above, pinned, and outlives `gpu`.
-                    unsafe { &*core::ptr::from_ref(bar1) },
-                ).pin_chain(|_gpu| {
-                    #[cfg(CONFIG_NOVA_CORE_SELFTESTS)]
-                    _gpu.run_selftests(pdev);
-                    Ok(())
-                }),
-                _reg: {
-                    // TODO: Use `&gpu` self-referential pin-init syntax once available.
-                    //
-                    // SAFETY: `gpu` is initialized before this expression is evaluated
-                    // (`try_pin_init!()` initializes fields in initializer order), lives at
-                    // a pinned stable address, and is dropped after `_reg` (struct field
-                    // drop order).
-                    let gpu = unsafe {
-                        Pin::new_unchecked(&*core::ptr::from_ref(gpu.as_ref().get_ref()))
-                    };
-
-                    // SAFETY: `NovaCore` is dropped when the device is unbound;
-                    // i.e. `mem::forget()` is never called on it.
-                    unsafe {
-                        auxiliary::Registration::new_with_lt(
-                            pdev.as_ref(),
-                            c"nova-drm",
-                            // TODO[XARR]: Use XArray or perhaps IDA for proper ID

drivers/gpu/nova-core/driver.rs fn probe<'bound>( · line 103

Quoted 10 lines
drivers/gpu/nova-core/driver.rs +1 -9
-                            // allocation/recycling. For now, use a simple atomic counter that
-                            // never recycles IDs.
-                            AUXILIARY_ID_COUNTER.fetch_add(1, Relaxed),
-                            crate::MODULE_NAME,
-                            NovaCoreApi { gpu, pdev },
-                        )?
-                    }
-                },
-            }))
+        dev_dbg!(pdev, "Probe Nova Core GPU driver.\n");

Don't you want to put this in the _: block below, so it printed at the actual initialization time instead of the time the initializer is created?

Not that it matters much, but it sounds slightly more consistent.

Regardless,

author maintainer
#2 lore raw
Quoted 4 lines · Alexandre Courbot
Quoted 1 line

+ dev_dbg!(pdev, "Probe Nova Core GPU driver.\n");

Don't you want to put this in the _: block below, so it printed at the actual initialization time instead of the time the initializer is created?

No, because the :_ block below will become the PCI device enable guard soon. We could put it in a separate :_ block, but that seemed odd to me.

In any case, we probably should drop this dev_dbg!() entirely, as its value is quite limited.

#3 lore raw
Quoted 11 lines · Danilo Krummrich
Quoted 4 lines · Alexandre Courbot
Quoted 1 line

+ dev_dbg!(pdev, "Probe Nova Core GPU driver.\n");

Don't you want to put this in the _: block below, so it printed at the actual initialization time instead of the time the initializer is created?

No, because the :_ block below will become the PCI device enable guard soon. We could put it in a separate :_ block, but that seemed odd to me.

In any case, we probably should drop this dev_dbg!() entirely, as its value is quite limited.

I missed this comment until just now, when I went to test with the final merged patch, and the "Probe..." debug print for each GPU was missing.

I really would like this restored. This is a high value thing to print, because it acts as kind of a bookend, showing the beginning of trying to boot up.

In fact, we even have some local scripts that look for it (not that that matters, but just to indicate how we think about its usefulness).

I can post a trivial patch to restore it, if desired.

thanks,

Signature · 1 line
John Hubbard
author maintainer
#4 lore raw
Quoted 3 lines · John Hubbard

I really would like this restored. This is a high value thing to print, because it acts as kind of a bookend, showing the beginning of trying to boot up.

Hm...the next print in the boot process is

	nova-core 0000:01:00.0: NVIDIA (Chipset: GA106, Architecture: Ampere, Revision: a.1)

which is at the very beginning of probe and to me seems sufficient.

But a debug print doesn't hurt, so if you prefer to keep it, no concerns from my end.

Quoted 1 line

I can post a trivial patch to restore it, if desired.

That'd be great, thanks!

maintainer
#6 lore raw
In reply to Danilo Krummrich
Quoted 87 lines · Danilo Krummrich

Just like in Gpu::new() a single try_pin_init!() block makes the code more readable.

Besides that, it prepares the code for a proper PCI device enable guard that we will get soon.

---
 drivers/gpu/nova-core/driver.rs | 105 ++++++++++++++++----------------
 1 file changed, 54 insertions(+), 51 deletions(-)
drivers/gpu/nova-core/driver.rs +17 -51
index bdaed5408a00..623d59dcde6e 100644
@@ -100,57 +100,60 @@ fn probe<'bound>(
         pdev: &'bound pci::Device<Core<'_>>,
         _info: Option<&'bound Self::IdInfo>,
     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
-        pin_init::pin_init_scope(move || {
-            dev_dbg!(pdev, "Probe Nova Core GPU driver.\n");
-
-            pdev.enable_device_mem()?;
-            pdev.set_master();
-
-            Ok(try_pin_init!(NovaCore {
-                bar: pdev.iomap_region_sized::<BAR0_SIZE>(0, c"nova-core/bar0")?,
-                bar1: {
-                    let bar1_idx = bar1_resource_index(pdev)?;
-                    pdev.iomap_region(bar1_idx, c"nova-core/bar1")?
-                },
-                // TODO: Use self-referential pin-init syntax once available.
-                gpu <- Gpu::new(
-                    pdev,
-                    // SAFETY: `bar` is initialized above, pinned, and outlives `gpu`.
-                    unsafe { &*core::ptr::from_ref(bar) },
-                    // SAFETY: `bar1` is initialized above, pinned, and outlives `gpu`.
-                    unsafe { &*core::ptr::from_ref(bar1) },
-                ).pin_chain(|_gpu| {
-                    #[cfg(CONFIG_NOVA_CORE_SELFTESTS)]
-                    _gpu.run_selftests(pdev);
-                    Ok(())
-                }),
-                _reg: {
-                    // TODO: Use `&gpu` self-referential pin-init syntax once available.
-                    //
-                    // SAFETY: `gpu` is initialized before this expression is evaluated
-                    // (`try_pin_init!()` initializes fields in initializer order), lives at
-                    // a pinned stable address, and is dropped after `_reg` (struct field
-                    // drop order).
-                    let gpu = unsafe {
-                        Pin::new_unchecked(&*core::ptr::from_ref(gpu.as_ref().get_ref()))
-                    };
-
-                    // SAFETY: `NovaCore` is dropped when the device is unbound;
-                    // i.e. `mem::forget()` is never called on it.
-                    unsafe {
-                        auxiliary::Registration::new_with_lt(
-                            pdev.as_ref(),
-                            c"nova-drm",
-                            // TODO[XARR]: Use XArray or perhaps IDA for proper ID
-                            // allocation/recycling. For now, use a simple atomic counter that
-                            // never recycles IDs.
-                            AUXILIARY_ID_COUNTER.fetch_add(1, Relaxed),
-                            crate::MODULE_NAME,
-                            NovaCoreApi { gpu, pdev },
-                        )?
-                    }
-                },
-            }))
+        dev_dbg!(pdev, "Probe Nova Core GPU driver.\n");
+
+        try_pin_init!(NovaCore {
+            _: {
+                pdev.enable_device_mem()?;
+                pdev.set_master();
+            },
+
+            bar: pdev.iomap_region_sized::<BAR0_SIZE>(0, c"nova-core/bar0")?,
+
+            bar1: {
+                let bar1_idx = bar1_resource_index(pdev)?;
+                pdev.iomap_region(bar1_idx, c"nova-core/bar1")?
+            },
+
+            // TODO: Use self-referential pin-init syntax once available.
+            gpu <- Gpu::new(

drivers/gpu/nova-core/driver.rs fn probe<'bound>( · line 129

Quoted 10 lines
drivers/gpu/nova-core/driver.rs +10 -0
+                pdev,
+                // SAFETY: `bar` is initialized above, pinned, and outlives `gpu`.
+                unsafe { &*core::ptr::from_ref(bar) },
+                // SAFETY: `bar1` is initialized above, pinned, and outlives `gpu`.
+                unsafe { &*core::ptr::from_ref(bar1) },
+            ).pin_chain(|_gpu| {
+                #[cfg(CONFIG_NOVA_CORE_SELFTESTS)]
+                _gpu.run_selftests(pdev);
+                Ok(())
+            }),

I think this doesn't need to use pin_chain? You can do

    gpu <- ...,

    _: {
        #[cfg(...)]
        gpu.run_self_tests(pdev);
    }

I know this is pre-existing but given that you're tidying it up this might do as well.

Best,
Gary

Quoted 32 lines
drivers/gpu/nova-core/driver.rs +27 -0
+
+            _reg: {
+                // TODO: Use `&gpu` self-referential pin-init syntax once available.
+                //
+                // SAFETY: `gpu` is initialized before this expression is evaluated
+                // (`try_pin_init!()` initializes fields in initializer order), lives at
+                // a pinned stable address, and is dropped after `_reg` (struct field
+                // drop order).
+                let gpu = unsafe {
+                    Pin::new_unchecked(&*core::ptr::from_ref(gpu.as_ref().get_ref()))
+                };
+
+                // SAFETY: `NovaCore` is dropped when the device is unbound;
+                // i.e. `mem::forget()` is never called on it.
+                unsafe {
+                    auxiliary::Registration::new_with_lt(
+                        pdev.as_ref(),
+                        c"nova-drm",
+                        // TODO[XARR]: Use XArray or perhaps IDA for proper ID
+                        // allocation/recycling. For now, use a simple atomic counter that
+                        // never recycles IDs.
+                        AUXILIARY_ID_COUNTER.fetch_add(1, Relaxed),
+                        crate::MODULE_NAME,
+                        NovaCoreApi { gpu, pdev },
+                    )?
+                }
+            },
         })
     }
 }

base-commit: 10a6623a24a85708650efad7be15182289403cd7

author maintainer
#7 lore raw
Quoted 8 lines · Gary Guo

I think this doesn't need to use pin_chain? You can do

    gpu <- ...,

    _: {
        #[cfg(...)]
        gpu.run_self_tests(pdev);
    }

Unfortunately, this produces the following error.

	error[E0382]: borrow of moved value: `gpu`
	   --> drivers/gpu/nova-core/driver.rs:140:62
	    |
	119 |             gpu <- Gpu::new(
	    |             --- move occurs because `gpu` has type `core::pin::Pin<&mut gpu::Gpu<'_>>`, which does not implement the `Copy` trait
	...
	129 |                 gpu.run_selftests(pdev);
	    |                 --- value moved here
	...
	140 |                     Pin::new_unchecked(&*core::ptr::from_ref(gpu.as_ref().get_ref()))
	    |                                                              ^^^ value borrowed here after move
	
	error: aborting due to 1 previous error
maintainer
#8 lore raw
Quoted 12 lines · Danilo Krummrich
Quoted 8 lines · Gary Guo

I think this doesn't need to use pin_chain? You can do

    gpu <- ...,

    _: {
        #[cfg(...)]
        gpu.run_self_tests(pdev);
    }

Unfortunately, this produces the following error.

Quoted 8 lines
	error[E0382]: borrow of moved value: `gpu`
	   --> drivers/gpu/nova-core/driver.rs:140:62
	    |
	119 |             gpu <- Gpu::new(
	    |             --- move occurs because `gpu` has type `core::pin::Pin<&mut gpu::Gpu<'_>>`, which does not implement the `Copy` trait
	...
	129 |                 gpu.run_selftests(pdev);
	    |                 --- value moved here
    gpu.as_mut().run_selftests(pdev);

should work?

Best,
Gary

Quoted 5 lines
	...
	140 |                     Pin::new_unchecked(&*core::ptr::from_ref(gpu.as_ref().get_ref()))
	    |                                                              ^^^ value borrowed here after move
	
	error: aborting due to 1 previous error
author maintainer
#9 lore raw
Quoted 22 lines · Gary Guo
Quoted 12 lines · Danilo Krummrich
Quoted 8 lines · Gary Guo

I think this doesn't need to use pin_chain? You can do

    gpu <- ...,

    _: {
        #[cfg(...)]
        gpu.run_self_tests(pdev);
    }

Unfortunately, this produces the following error.

Quoted 8 lines
	error[E0382]: borrow of moved value: `gpu`
	   --> drivers/gpu/nova-core/driver.rs:140:62
	    |
	119 |             gpu <- Gpu::new(
	    |             --- move occurs because `gpu` has type `core::pin::Pin<&mut gpu::Gpu<'_>>`, which does not implement the `Copy` trait
	...
	129 |                 gpu.run_selftests(pdev);
	    |                 --- value moved here
Quoted 3 lines
    gpu.as_mut().run_selftests(pdev);

should work?

I tried that previously, that gives another error. I didn't dig in further though.

	error[E0596]: cannot borrow `gpu` as mutable, as it is not declared as mutable
	   --> drivers/gpu/nova-core/driver.rs:129:17
	    |
	129 |                 gpu.as_mut().run_selftests(pdev);
	    |                 ^^^ cannot borrow as mutable
	    |
	help: consider changing this to be mutable
	    |
	119 |             mut gpu <- Gpu::new(
	    |             +++
	
	error: aborting due to 1 previous error
maintainer
#10 lore raw
Quoted 30 lines · Danilo Krummrich
Quoted 22 lines · Gary Guo
Quoted 12 lines · Danilo Krummrich
Quoted 8 lines · Gary Guo

I think this doesn't need to use pin_chain? You can do

    gpu <- ...,

    _: {
        #[cfg(...)]
        gpu.run_self_tests(pdev);
    }

Unfortunately, this produces the following error.

Quoted 8 lines
	error[E0382]: borrow of moved value: `gpu`
	   --> drivers/gpu/nova-core/driver.rs:140:62
	    |
	119 |             gpu <- Gpu::new(
	    |             --- move occurs because `gpu` has type `core::pin::Pin<&mut gpu::Gpu<'_>>`, which does not implement the `Copy` trait
	...
	129 |                 gpu.run_selftests(pdev);
	    |                 --- value moved here
Quoted 3 lines
    gpu.as_mut().run_selftests(pdev);

should work?

I tried that previously, that gives another error. I didn't dig in further though.

Quoted 12 lines
	error[E0596]: cannot borrow `gpu` as mutable, as it is not declared as mutable
	   --> drivers/gpu/nova-core/driver.rs:129:17
	    |
	129 |                 gpu.as_mut().run_selftests(pdev);
	    |                 ^^^ cannot borrow as mutable
	    |
	help: consider changing this to be mutable
	    |
	119 |             mut gpu <- Gpu::new(
	    |             +++
	
	error: aborting due to 1 previous error

Right, we didn't create the let binding with mut.

Does this fix it for you?

rust/pin-init/internal/src/init.rs +1 -1
index c5b5a0ce5c4e..031adb46cefe 100644
@@ -446,7 +446,7 @@ fn init_fields(fields: &Punctuated<InitializerField, Token![,]>, pinned: bool) -
                 // Allow `non_snake_case` since the same warning is going to be reported for the
                 // struct field.
                 #[allow(unused_variables, non_snake_case)]
-                let #ident = #guard.let_binding();
+                let mut #ident = #guard.let_binding();
             },
             Member::Unnamed(_) => quote!(),
         };

Best,
Gary

author maintainer
#11 lore raw
Quoted 1 line · Gary Guo

Does this fix it for you?

Yes, it does, thanks!

author maintainer Applied
#12 lore raw
In reply to Danilo Krummrich
Quoted 1 line · Danilo Krummrich

[PATCH] gpu: nova-core: use a single try_pin_init!() block in probe()

Applied, thanks!

Branch: drm-rust-next
Tree: https://gitlab.freedesktop.org/drm/rust/kernel.git

[1/1] gpu: nova-core: use a single try_pin_init!() block in probe()
commit: 658c5f0042e6

[ Drop useless dev_dbg!() in probe(). - Danilo ]

The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patch is queued up for the upcoming merge window for the next major kernel release.