Skip to contentSkip to conversation

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

PATCH Applied John Hubbard 2 participants 4 messages Jan 6 – Jan 7

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

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

Alex Courbot's "[PATCH v2 00/10] gpu: nova-core: miscellaneous improvements" series [1] includes a similar patch as my patch 1 here, but this one here goes further and deletes the entire util.rs file.

This is based upon the latest drm-rust-next.

Changes in v2:

1) added a second patch, as suggested by Joel Fernandes, to do a little more cleanup.

2) Added Joel's reviewed-by tag to the first patch.

v1 is here:

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

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

John Hubbard (2):
gpu: nova-core: use CStr::from_bytes_until_nul() and remove util.rs
gpu: nova-core: use CStr::from_bytes_until_nul() in elf64_section()

John Hubbard (2):
gpu: nova-core: use CStr::from_bytes_until_nul() and remove util.rs
gpu: nova-core: use CStr::from_bytes_until_nul() in elf64_section()

 drivers/gpu/nova-core/firmware/gsp.rs |  5 +----
 drivers/gpu/nova-core/gsp/commands.rs |  5 +++--
 drivers/gpu/nova-core/nova_core.rs    |  1 -
 drivers/gpu/nova-core/util.rs         | 16 ----------------
 4 files changed, 4 insertions(+), 23 deletions(-)
 delete mode 100644 drivers/gpu/nova-core/util.rs

base-commit: 7acc70476f14661149774ab88d3fe23d83ba4249
prerequisite-patch-id: df9c0cfe56901c27064ac88972dffe3a4314c2f8 --
2.52.0

#1 lore raw

PATCH v2 1/2 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())
-}
Signature · 1 line
2.52.0
#2 lore raw

PATCH v2 2/2 gpu: nova-core: use CStr::from_bytes_until_nul() in elf64_section()

In reply to John Hubbard

Instead of open-coding the steps for extracting a null-terminated string, use the newly available CStr::from_bytes_until_nul().

---
 drivers/gpu/nova-core/firmware/gsp.rs | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/gpu/nova-core/firmware/gsp.rs +1 -4
index da97814cf859..1025b7f746eb 100644
@@ -93,10 +93,7 @@ pub(super) fn elf64_section<'a, 'b>(elf: &'a [u8], name: &'b str) -> Option<&'a
 
             // Get the start of the name.
             elf.get(name_idx..)
-                // Stop at the first `0`.
-                .and_then(|nstr| nstr.get(0..=nstr.iter().position(|b| *b == 0)?))
-                // Convert into CStr. This should never fail because of the line above.
-                .and_then(|nstr| CStr::from_bytes_with_nul(nstr).ok())
+                .and_then(|nstr| CStr::from_bytes_until_nul(nstr).ok())
                 // Convert into str.
                 .and_then(|c_str| c_str.to_str().ok())
                 // Check that the name matches.
Signature · 1 line
2.52.0
maintainer Applied
#3 lore raw
In reply to John Hubbard
Quoted 3 lines · John Hubbard

John Hubbard (2):
gpu: nova-core: use CStr::from_bytes_until_nul() and remove util.rs
gpu: nova-core: use CStr::from_bytes_until_nul() in elf64_section()

Applied to drm-rust-next, thanks!