Re: [PATCH] gpu: nova-core: use CStr::from_bytes_until_nul() and remove util.rs

From: John Hubbard

Date: Tue Jan 06 2026 - 17:09:57 EST


On 1/6/26 4:02 AM, Danilo Krummrich wrote:
> On Sat Jan 3, 2026 at 2:34 AM CET, John Hubbard wrote:
>> @@ -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.

> perhaps it can be addressed with a subsequent patch?
>
> [1] https://lore.kernel.org/lkml/DFEVITW4O9DW.P4ITE1PWIDY6@xxxxxxxxxx/

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

diff --git a/drivers/gpu/nova-core/gsp/boot.rs b/drivers/gpu/nova-core/gsp/boot.rs
index a53d80620468..71fca7350b94 100644
--- a/drivers/gpu/nova-core/gsp/boot.rs
+++ b/drivers/gpu/nova-core/gsp/boot.rs
@@ -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(())
}
diff --git a/drivers/gpu/nova-core/gsp/commands.rs b/drivers/gpu/nova-core/gsp/commands.rs
index a11fe6018091..426441978b4b 100644
--- a/drivers/gpu/nova-core/gsp/commands.rs
+++ b/drivers/gpu/nova-core/gsp/commands.rs
@@ -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,
--
John Hubbard