Skip to contentSkip to conversation

gpu: nova-core: use CStr::from_bytes_until_nul() and remove util.rs

PATCH Superseded John Hubbard 3 participants 8 messages Jan 3 – Jan 7

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpu: nova-core: use CStr::from_bytes_until_nul() and remove util.rs

The util.rs module contained a single helper function, str_from_null_terminated(), which duplicated functionality that is now available in core::ffi::CStr.

Specifically, CStr::from_bytes_until_nul() is available in the kernel's minimum supported Rust version (1.78.0), so it time to stop using this custom workaround.

---
 drivers/gpu/nova-core/gsp/commands.rs |  5 +++--
 drivers/gpu/nova-core/nova_core.rs    |  1 -
 drivers/gpu/nova-core/util.rs         | 16 ----------------
 3 files changed, 3 insertions(+), 19 deletions(-)
 delete mode 100644 drivers/gpu/nova-core/util.rs
drivers/gpu/nova-core/gsp/commands.rs +3 -2
index 0425c65b5d6f..a11fe6018091 100644
@@ -30,7 +30,6 @@
         },
     },
     sbuffer::SBufferIter,
-    util,
 };
 
 /// The `GspSetSystemInfo` command.
@@ -209,7 +208,9 @@ impl GetGspStaticInfoReply {
     /// Returns the name of the GPU as a string, or `None` if the string given by the GSP was
     /// invalid.
     pub(crate) fn gpu_name(&self) -> Option<&str> {
-        util::str_from_null_terminated(&self.gpu_name)
+        CStr::from_bytes_until_nul(&self.gpu_name)
+            .ok()
+            .and_then(|cstr| cstr.to_str().ok())
     }
 }
 
drivers/gpu/nova-core/nova_core.rs +0 -1
index b98a1c03f13d..c1121e7c64c5 100644
@@ -16,7 +16,6 @@
 mod num;
 mod regs;
 mod sbuffer;
-mod util;
 mod vbios;
 
 pub(crate) const MODULE_NAME: &kernel::str::CStr = <LocalModule as kernel::ModuleMetadata>::NAME;
drivers/gpu/nova-core/util.rs deleted +0 -16
deleted file mode 100644
index 4b503249a3ef..000000000000
@@ -1,16 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-
-/// Converts a null-terminated byte slice to a string, or `None` if the array does not
-/// contains any null byte or contains invalid characters.
-///
-/// Contrary to [`kernel::str::CStr::from_bytes_with_nul`], the null byte can be anywhere in the
-/// slice, and not only in the last position.
-pub(crate) fn str_from_null_terminated(bytes: &[u8]) -> Option<&str> {
-    use kernel::str::CStr;
-
-    bytes
-        .iter()
-        .position(|&b| b == 0)
-        .and_then(|null_pos| CStr::from_bytes_with_nul(&bytes[..=null_pos]).ok())
-        .and_then(|cstr| cstr.to_str().ok())
-}

base-commit: 7acc70476f14661149774ab88d3fe23d83ba4249

Signature · 1 line
2.52.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · John Hubbard

The util.rs module contained a single helper function, str_from_null_terminated(), which duplicated functionality that is now available in core::ffi::CStr.

Specifically, CStr::from_bytes_until_nul() is available in the kernel's minimum supported Rust version (1.78.0), so it time to stop using this custom workaround.

Probably elf64_section() should also use CStr::from_bytes_until_nul() in gsp.rs?

Right now it does:

         elf.get(name_idx..)
             .and_then(|nstr| nstr.get(0..=nstr.iter().position(|b| *b == 0)?))
             .and_then(|nstr| CStr::from_bytes_with_nul(nstr).ok())

thanks,

- Joel

Quoted 66 lines
---
 drivers/gpu/nova-core/gsp/commands.rs |  5 +++--
 drivers/gpu/nova-core/nova_core.rs    |  1 -
 drivers/gpu/nova-core/util.rs         | 16 ----------------
 3 files changed, 3 insertions(+), 19 deletions(-)
 delete mode 100644 drivers/gpu/nova-core/util.rs
drivers/gpu/nova-core/gsp/commands.rs +3 -2
index 0425c65b5d6f..a11fe6018091 100644
@@ -30,7 +30,6 @@
         },
     },
     sbuffer::SBufferIter,
-    util,
 };
 
 /// The `GspSetSystemInfo` command.
@@ -209,7 +208,9 @@ impl GetGspStaticInfoReply {
     /// Returns the name of the GPU as a string, or `None` if the string given by the GSP was
     /// invalid.
     pub(crate) fn gpu_name(&self) -> Option<&str> {
-        util::str_from_null_terminated(&self.gpu_name)
+        CStr::from_bytes_until_nul(&self.gpu_name)
+            .ok()
+            .and_then(|cstr| cstr.to_str().ok())
     }
 }
 
drivers/gpu/nova-core/nova_core.rs +0 -1
index b98a1c03f13d..c1121e7c64c5 100644
@@ -16,7 +16,6 @@
 mod num;
 mod regs;
 mod sbuffer;
-mod util;
 mod vbios;
 
 pub(crate) const MODULE_NAME: &kernel::str::CStr = <LocalModule as kernel::ModuleMetadata>::NAME;
drivers/gpu/nova-core/util.rs deleted +0 -16
deleted file mode 100644
index 4b503249a3ef..000000000000
@@ -1,16 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-
-/// Converts a null-terminated byte slice to a string, or `None` if the array does not
-/// contains any null byte or contains invalid characters.
-///
-/// Contrary to [`kernel::str::CStr::from_bytes_with_nul`], the null byte can be anywhere in the
-/// slice, and not only in the last position.
-pub(crate) fn str_from_null_terminated(bytes: &[u8]) -> Option<&str> {
-    use kernel::str::CStr;
-
-    bytes
-        .iter()
-        .position(|&b| b == 0)
-        .and_then(|null_pos| CStr::from_bytes_with_nul(&bytes[..=null_pos]).ok())
-        .and_then(|cstr| cstr.to_str().ok())
-}

base-commit: 7acc70476f14661149774ab88d3fe23d83ba4249

#2 lore raw
Quoted 7 lines · Joel Fernandes
Quoted 4 lines · John Hubbard

The util.rs module contained a single helper function, str_from_null_terminated(), which duplicated functionality that is now available in core::ffi::CStr.

Quoted 7 lines
Quoted 5 lines

Specifically, CStr::from_bytes_until_nul() is available in the kernel's minimum supported Rust version (1.78.0), so it time to stop using this custom workaround.

Thanks for the review!

Quoted 8 lines

Probably elf64_section() should also use CStr::from_bytes_until_nul() in gsp.rs?

Right now it does:

         elf.get(name_idx..)
             .and_then(|nstr| nstr.get(0..=nstr.iter().position(|b| *b == 0)?))
             .and_then(|nstr| CStr::from_bytes_with_nul(nstr).ok())

Yes, absolutely! Thanks for spotting that. I'll re-send this out as a two-patch series that includes that change too.

thanks,

Signature · 1 line
John Hubbard
Quoted 68 lines
Quoted 67 lines
---
 drivers/gpu/nova-core/gsp/commands.rs |  5 +++--
 drivers/gpu/nova-core/nova_core.rs    |  1 -
 drivers/gpu/nova-core/util.rs         | 16 ----------------
 3 files changed, 3 insertions(+), 19 deletions(-)
 delete mode 100644 drivers/gpu/nova-core/util.rs
drivers/gpu/nova-core/gsp/commands.rs +3 -2
index 0425c65b5d6f..a11fe6018091 100644
@@ -30,7 +30,6 @@
         },
     },
     sbuffer::SBufferIter,
-    util,
 };
 
 /// The `GspSetSystemInfo` command.
@@ -209,7 +208,9 @@ impl GetGspStaticInfoReply {
     /// Returns the name of the GPU as a string, or `None` if the string given by the GSP was
     /// invalid.
     pub(crate) fn gpu_name(&self) -> Option<&str> {
-        util::str_from_null_terminated(&self.gpu_name)
+        CStr::from_bytes_until_nul(&self.gpu_name)
+            .ok()
+            .and_then(|cstr| cstr.to_str().ok())
     }
 }
 
drivers/gpu/nova-core/nova_core.rs +0 -1
index b98a1c03f13d..c1121e7c64c5 100644
@@ -16,7 +16,6 @@
 mod num;
 mod regs;
 mod sbuffer;
-mod util;
 mod vbios;
 
 pub(crate) const MODULE_NAME: &kernel::str::CStr = <LocalModule as kernel::ModuleMetadata>::NAME;
drivers/gpu/nova-core/util.rs deleted +0 -16
deleted file mode 100644
index 4b503249a3ef..000000000000
@@ -1,16 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-
-/// Converts a null-terminated byte slice to a string, or `None` if the array does not
-/// contains any null byte or contains invalid characters.
-///
-/// Contrary to [`kernel::str::CStr::from_bytes_with_nul`], the null byte can be anywhere in the
-/// slice, and not only in the last position.
-pub(crate) fn str_from_null_terminated(bytes: &[u8]) -> Option<&str> {
-    use kernel::str::CStr;
-
-    bytes
-        .iter()
-        .position(|&b| b == 0)
-        .and_then(|null_pos| CStr::from_bytes_with_nul(&bytes[..=null_pos]).ok())
-        .and_then(|cstr| cstr.to_str().ok())
-}

base-commit: 7acc70476f14661149774ab88d3fe23d83ba4249

maintainer
#3 lore raw
In reply to John Hubbard

drivers/gpu/nova-core/gsp/commands.rs impl GetGspStaticInfoReply { · line 215

Quoted 10 lines · John Hubbard
drivers/gpu/nova-core/gsp/commands.rs +3 -1
@@ -209,7 +208,9 @@ impl GetGspStaticInfoReply {
     /// Returns the name of the GPU as a string, or `None` if the string given by the GSP was
     /// invalid.
     pub(crate) fn gpu_name(&self) -> Option<&str> {
-        util::str_from_null_terminated(&self.gpu_name)
+        CStr::from_bytes_until_nul(&self.gpu_name)
+            .ok()
+            .and_then(|cstr| cstr.to_str().ok())
     }
 }

Did you see my reply in [1]? The question is orthogonal to this change, but perhaps it can be addressed with a subsequent patch?

[1] https://lore.kernel.org/lkml/[email protected]/

#4 lore raw
Quoted 13 lines · Danilo Krummrich

Review impl GetGspStaticInfoReply { · line 215

Quoted 10 lines · John Hubbard
(quoted diff) +3 -1
@@ -209,7 +208,9 @@ impl GetGspStaticInfoReply {
     /// Returns the name of the GPU as a string, or `None` if the string given by the GSP was
     /// invalid.
     pub(crate) fn gpu_name(&self) -> Option<&str> {
-        util::str_from_null_terminated(&self.gpu_name)
+        CStr::from_bytes_until_nul(&self.gpu_name)
+            .ok()
+            .and_then(|cstr| cstr.to_str().ok())
     }
 }

Did you see my reply in [1]? The question is orthogonal to this change, but

I did, but momentarily forgot it during a miserable day in which Thunderbird and Wayland started violently disagreeing, and every email turned into a troubleshooting effort. :) Glad to be past that.

Quoted 3 lines

perhaps it can be addressed with a subsequent patch?

[1] https://lore.kernel.org/lkml/[email protected]/

Yes, so that would look approximately like this, I can send this as another patch if it looks reasonable:

drivers/gpu/nova-core/gsp/boot.rs +5 -5
index a53d80620468..71fca7350b94 100644
@@ -238,11 +238,11 @@ pub(crate) fn boot(
 
         // Obtain and display basic GPU information.
         let info = commands::get_gsp_info(&mut self.cmdq, bar)?;
-        dev_info!(
-            pdev.as_ref(),
-            "GPU name: {}\n",
-            info.gpu_name().unwrap_or("invalid GPU name")
-        );
+        let gpu_name = info
+            .gpu_name()
+            .inspect_err(|e| dev_warn!(pdev.as_ref(), "GPU name: {}\n", e))
+            .unwrap_or("<unavailable>");
+        dev_info!(pdev.as_ref(), "GPU name: {}\n", gpu_name);
 
         Ok(())
     }
drivers/gpu/nova-core/gsp/commands.rs +30 -6
index a11fe6018091..426441978b4b 100644
@@ -2,7 +2,9 @@
 
 use core::{
     array,
-    convert::Infallible, //
+    convert::Infallible,
+    ffi::FromBytesUntilNulError,
+    str::Utf8Error, //
 };
 
 use kernel::{
@@ -204,13 +206,35 @@ fn read(
     }
 }
 
+/// Error type for [`GetGspStaticInfoReply::gpu_name`].
+#[derive(Debug)]
+pub(crate) enum GpuNameError {
+    /// The GPU name string does not contain a null terminator.
+    NoNullTerminator(FromBytesUntilNulError),
+
+    /// The GPU name string contains invalid UTF-8.
+    InvalidUtf8(Utf8Error),
+}
+
+impl kernel::fmt::Display for GpuNameError {
+    fn fmt(&self, f: &mut kernel::fmt::Formatter<'_>) -> kernel::fmt::Result {
+        match self {
+            Self::NoNullTerminator(_) => write!(f, "no null terminator"),
+            Self::InvalidUtf8(e) => write!(f, "invalid UTF-8 at byte {}", e.valid_up_to()),
+        }
+    }
+}
+
 impl GetGspStaticInfoReply {
-    /// Returns the name of the GPU as a string, or `None` if the string given by the GSP was
-    /// invalid.
-    pub(crate) fn gpu_name(&self) -> Option<&str> {
+    /// Returns the name of the GPU as a string.
+    ///
+    /// Returns an error if the string given by the GSP does not contain a null terminator or
+    /// contains invalid UTF-8.
+    pub(crate) fn gpu_name(&self) -> core::result::Result<&str, GpuNameError> {
         CStr::from_bytes_until_nul(&self.gpu_name)
-            .ok()
-            .and_then(|cstr| cstr.to_str().ok())
+            .map_err(GpuNameError::NoNullTerminator)?
+            .to_str()
+            .map_err(GpuNameError::InvalidUtf8)
     }
 }
 

thanks,

Signature · 1 line
John Hubbard
maintainer
#5 lore raw
Quoted 2 lines · John Hubbard

Yes, so that would look approximately like this, I can send this as another patch if it looks reasonable:

Thanks, looks good! Two comments below.

Quoted 8 lines
drivers/gpu/nova-core/gsp/boot.rs +0 -0
index a53d80620468..71fca7350b94 100644
@@ -238,11 +238,11 @@ pub(crate) fn boot(
 
         // Obtain and display basic GPU information.
         let info = commands::get_gsp_info(&mut self.cmdq, bar)?;

drivers/gpu/nova-core/gsp/boot.rs pub(crate) fn boot( · line 245

Quoted 10 lines
drivers/gpu/nova-core/gsp/boot.rs +5 -5
-        dev_info!(
-            pdev.as_ref(),
-            "GPU name: {}\n",
-            info.gpu_name().unwrap_or("invalid GPU name")
-        );
+        let gpu_name = info
+            .gpu_name()
+            .inspect_err(|e| dev_warn!(pdev.as_ref(), "GPU name: {}\n", e))
+            .unwrap_or("<unavailable>");
+        dev_info!(pdev.as_ref(), "GPU name: {}\n", gpu_name);

I'd probably only print one or the other. Also, I think this should be dev_dbg!() instead of dev_info!().

Quoted 8 lines
drivers/gpu/nova-core/gsp/boot.rs +8 -0
+/// Error type for [`GetGspStaticInfoReply::gpu_name`].
+#[derive(Debug)]
+pub(crate) enum GpuNameError {
+    /// The GPU name string does not contain a null terminator.
+    NoNullTerminator(FromBytesUntilNulError),
+
+    /// The GPU name string contains invalid UTF-8.
+    InvalidUtf8(Utf8Error),

drivers/gpu/nova-core/gsp/boot.rs pub(crate) fn boot(

Quoted 10 lines
drivers/gpu/nova-core/gsp/boot.rs +10 -0
+}
+
+impl kernel::fmt::Display for GpuNameError {
+    fn fmt(&self, f: &mut kernel::fmt::Formatter<'_>) -> kernel::fmt::Result {
+        match self {
+            Self::NoNullTerminator(_) => write!(f, "no null terminator"),
+            Self::InvalidUtf8(e) => write!(f, "invalid UTF-8 at byte {}", e.valid_up_to()),
+        }
+    }
+}

Do we need this Display impl, or is the derive(Debug) you have already good enough for the warning print?

#6 lore raw
Quoted 1 line · Danilo Krummrich

On 1/6/26 11:09 PM, John Hubbard wrote:

...

Quoted 7 lines

drivers/gpu/nova-core/gsp/boot.rs pub(crate) fn boot( · line 245

Quoted 5 lines
drivers/gpu/nova-core/gsp/boot.rs +5 -0
+        let gpu_name = info
+            .gpu_name()
+            .inspect_err(|e| dev_warn!(pdev.as_ref(), "GPU name: {}\n", e))
+            .unwrap_or("<unavailable>");
+        dev_info!(pdev.as_ref(), "GPU name: {}\n", gpu_name);

I'd probably only print one or the other. Also, I think this should be

Done.

Quoted 1 line

dev_dbg!() instead of dev_info!().

We have been *very* sparing with the dev_info(), and at this point, there are precisely two places where Nova logs at info level: at first probe, and after finding the true GPU marketing name (buried in the firmware).

I think we've found a nice balance now. The output looks like this:

$  dmesg -t --level=info|grep NovaCore
NovaCore 0000:e1:00.0: NVIDIA (Chipset: GA104, Architecture: Ampere, Revision: a.1)
NovaCore 0000:e1:00.0: GPU name: NVIDIA RTX A4000
[drm] Initialized nova 0.0.0 for NovaCore.nova-drm.0 on minor 0

So I'd love to leave the GPU name at info level, if you agree that this is about right.

Quoted 14 lines
Quoted 14 lines
drivers/gpu/nova-core/gsp/commands.rs +13 -0
+/// Error type for [`GetGspStaticInfoReply::gpu_name`].
+#[derive(Debug)]
+pub(crate) enum GpuNameError {
+    /// The GPU name string does not contain a null terminator.
+    NoNullTerminator(FromBytesUntilNulError),
+
+    /// The GPU name string contains invalid UTF-8.
+    InvalidUtf8(Utf8Error),
+}
+
+impl kernel::fmt::Display for GpuNameError {
+    fn fmt(&self, f: &mut kernel::fmt::Formatter<'_>) -> kernel::fmt::Result {
+        match self {
Quoted 9 lines

drivers/gpu/nova-core/gsp/commands.rs fn read( · line 226

Quoted 5 lines
drivers/gpu/nova-core/gsp/commands.rs +5 -0
+            Self::NoNullTerminator(_) => write!(f, "no null terminator"),
+            Self::InvalidUtf8(e) => write!(f, "invalid UTF-8 at byte {}", e.valid_up_to()),
+        }
+    }
+}

Do we need this Display impl, or is the derive(Debug) you have already good enough for the warning print?

Good point. Prettier printing is not worth it for such a rare corner case. The Debug printer still provides the key information that one would need.

thanks,

Signature · 1 line
John Hubbard
maintainer
#7 lore raw
Quoted 7 lines · John Hubbard
$  dmesg -t --level=info|grep NovaCore
NovaCore 0000:e1:00.0: NVIDIA (Chipset: GA104, Architecture: Ampere, Revision: a.1)
NovaCore 0000:e1:00.0: GPU name: NVIDIA RTX A4000
[drm] Initialized nova 0.0.0 for NovaCore.nova-drm.0 on minor 0

So I'd love to leave the GPU name at info level, if you agree that this is about right.

Since it adds additional information about the specific board, which is not covered by the chipset information already I think it's fine.