Skip to contentSkip to conversation

rust: task: use `as_char_ptr` instead of `as_ptr().cast()`

PATCH Superseded Gary Guo 5 participants 8 messages Jan 22 – Jan 26 Rust

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Gary Guo

author
#0 lore raw

PATCH v2 1/2 rust: task: use `as_char_ptr` instead of `as_ptr().cast()`

From: Gary Guo <[email protected]>

as_char_ptr would provide the correct (unsigned char) type without needing to convert to an intermediate type and cast the pointer.

The as_ptr() function is going to be disallowed by clippy warning, so fix this usage.

---
 rust/kernel/task.rs | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
rust/kernel/task.rs +2 -1
index cc907fb531bc..7ccb20a8f813 100644
@@ -9,6 +9,7 @@
     ffi::{c_int, c_long, c_uint},
     mm::MmWithUser,
     pid_namespace::PidNamespace,
+    str::CStrExt,
     sync::aref::ARef,
     types::{NotThreadSafe, Opaque},
 };
@@ -419,7 +420,7 @@ pub fn might_sleep() {
         let file = kernel::file_from_location(loc);
 
         // SAFETY: `file.as_ptr()` is valid for reading and guaranteed to be nul-terminated.
-        unsafe { crate::bindings::__might_sleep(file.as_ptr().cast(), loc.line() as i32) }
+        unsafe { crate::bindings::__might_sleep(file.as_char_ptr(), loc.line() as i32) }
     }
 
     // SAFETY: Always safe to call.

base-commit: 053966c344dbd346e71305f530e91ea77916189f

Signature · 1 line
2.51.2
author
#1 lore raw

PATCH v2 2/2 rust: disallow use of `CStr::as_ptr`

From: Gary Guo <[email protected]>

As kernel always use unsigned char and not the platform ABI's default, an user should always use as_char_ptr provided via CStrExt instead. Therefore configure disallow-methods feature of clippy to catch incorrect usage.

---
 .clippy.toml       | 5 +++++
 rust/kernel/str.rs | 2 ++
 2 files changed, 7 insertions(+)
.clippy.toml +5 -0
index 137f41d203de..4654b522ae03 100644
@@ -9,3 +9,8 @@ disallowed-macros = [
     # it here, see: https://github.com/rust-lang/rust-clippy/issues/11303.
     { path = "kernel::dbg", reason = "the `dbg!` macro is intended as a debugging tool", allow-invalid = true },
 ]
+
+[[disallowed-methods]]
+path = "core::ffi::CStr::as_ptr"
+replacement = "kernel::prelude::CStrExt::as_char_ptr"
+reason = "kernel's `char` is always unsigned, use `as_char_ptr` instead"
rust/kernel/str.rs +2 -0
index fa87779d2253..08b8e2ebc8ad 100644
@@ -189,6 +189,7 @@ macro_rules! b_str {
 //
 // - error[E0379]: functions in trait impls cannot be declared const
 #[inline]
+#[expect(clippy::disallowed_methods, reason = "internal implementation")]
 pub const fn as_char_ptr_in_const_context(c_str: &CStr) -> *const c_char {
     c_str.as_ptr().cast()
 }
@@ -334,6 +335,7 @@ unsafe fn from_bytes_with_nul_unchecked_mut(bytes: &mut [u8]) -> &mut Self {
     }
 
     #[inline]
+    #[expect(clippy::disallowed_methods, reason = "internal implementation")]
     fn as_char_ptr(&self) -> *const c_char {
         self.as_ptr().cast()
     }
Signature · 1 line
2.51.2
reviewer
#2 lore raw
In reply to Gary Guo
Quoted 11 lines · Gary Guo

From: Gary Guo <[email protected]>

as_char_ptr would provide the correct (unsigned char) type without needing to convert to an intermediate type and cast the pointer.

The as_ptr() function is going to be disallowed by clippy warning, so fix this usage.

Oops, I am trying out new workflow of using "git notes" for changelog, but I forget to put set format.notes to true...

Here's the change log:

v1 -> v2:
- fix one existing usage of as_ptr() (kernel test robot)
- styling of message (Tamir)
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/T/#m8ca5da7e22ff7ac6caff83326f4b81e0e9e3536f

Quoted 27 lines
---
 rust/kernel/task.rs | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
rust/kernel/task.rs +2 -1
index cc907fb531bc..7ccb20a8f813 100644
@@ -9,6 +9,7 @@
     ffi::{c_int, c_long, c_uint},
     mm::MmWithUser,
     pid_namespace::PidNamespace,
+    str::CStrExt,
     sync::aref::ARef,
     types::{NotThreadSafe, Opaque},
 };
@@ -419,7 +420,7 @@ pub fn might_sleep() {
         let file = kernel::file_from_location(loc);
 
         // SAFETY: `file.as_ptr()` is valid for reading and guaranteed to be nul-terminated.
-        unsafe { crate::bindings::__might_sleep(file.as_ptr().cast(), loc.line() as i32) }
+        unsafe { crate::bindings::__might_sleep(file.as_char_ptr(), loc.line() as i32) }
     }
 
     // SAFETY: Always safe to call.

base-commit: 053966c344dbd346e71305f530e91ea77916189f

reviewer Reviewed-by
#3 lore raw
Quoted 7 lines · Gary Guo
Quoted 5 lines · Gary Guo

From: Gary Guo <[email protected]>

as_char_ptr would provide the correct (unsigned char) type without needing to convert to an intermediate type and cast the pointer.

Quoted 6 lines
Quoted 6 lines

The as_ptr() function is going to be disallowed by clippy warning, so fix this usage.

#4 lore raw
In reply to Gary Guo
kernel test robot noticed the following build warnings:

Hi Gary,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 053966c344dbd346e71305f530e91ea77916189f]

url:    https://github.com/intel-lab-lkp/linux/commits/Gary-Guo/rust-disallow-use-of-CStr-as_ptr/20260122-224657
base:   053966c344dbd346e71305f530e91ea77916189f
patch link:    https://lore.kernel.org/r/20260122144444.265412-1-gary%40kernel.org
patch subject: [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()`
config: x86_64-buildonly-randconfig-004-20260125 (https://download.01.org/0day-ci/archive/20260125/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
rustc: rustc 1.88.0 (6b00bc388 2025-06-23)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260125/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> warning: unused import: `str::CStrExt`

--> rust/kernel/task.rs:12:5
|
12 | str::CStrExt,
| ^^^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#5 lore raw
Quoted 23 lines · kernel test robot

Hi Gary,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 053966c344dbd346e71305f530e91ea77916189f]

url:    https://github.com/intel-lab-lkp/linux/commits/Gary-Guo/rust-disallow-use-of-CStr-as_ptr/20260122-224657
base:   053966c344dbd346e71305f530e91ea77916189f
patch link:    https://lore.kernel.org/r/20260122144444.265412-1-gary%40kernel.org
patch subject: [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()`
config: x86_64-buildonly-randconfig-004-20260125 (https://download.01.org/0day-ci/archive/20260125/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
rustc: rustc 1.88.0 (6b00bc388 2025-06-23)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260125/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

Quoted 7 lines
Quoted 1 line
Quoted 1 line

warning: unused import: str::CStrExt

--> rust/kernel/task.rs:12:5
|
12 | str::CStrExt,
| ^^^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default

Seems like the import needs to be conditional on CONFIG_DEBUG_ATOMIC_SLEEP. Aside: it can also be as _ since it isn't named directly.

Quoted 5 lines

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

reviewer
#6 lore raw
Quoted 32 lines · Tamir Duberstein
Quoted 23 lines · kernel test robot

Hi Gary,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 053966c344dbd346e71305f530e91ea77916189f]

url:    https://github.com/intel-lab-lkp/linux/commits/Gary-Guo/rust-disallow-use-of-CStr-as_ptr/20260122-224657
base:   053966c344dbd346e71305f530e91ea77916189f
patch link:    https://lore.kernel.org/r/20260122144444.265412-1-gary%40kernel.org
patch subject: [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()`
config: x86_64-buildonly-randconfig-004-20260125 (https://download.01.org/0day-ci/archive/20260125/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
rustc: rustc 1.88.0 (6b00bc388 2025-06-23)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260125/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

Quoted 7 lines
Quoted 1 line
Quoted 1 line

warning: unused import: str::CStrExt

--> rust/kernel/task.rs:12:5
|
12 | str::CStrExt,
| ^^^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default

Quoted 3 lines

Seems like the import needs to be conditional on CONFIG_DEBUG_ATOMIC_SLEEP. Aside: it can also be as _ since it isn't named directly.

Can we just import the prelude instead of having a conditional import?

Alice

reviewer
#7 lore raw
Quoted 33 lines · Alice Ryhl
Quoted 32 lines · Tamir Duberstein
Quoted 30 lines · kernel test robot

Hi Gary,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 053966c344dbd346e71305f530e91ea77916189f]

url:    https://github.com/intel-lab-lkp/linux/commits/Gary-Guo/rust-disallow-use-of-CStr-as_ptr/20260122-224657
base:   053966c344dbd346e71305f530e91ea77916189f
patch link:    https://lore.kernel.org/r/20260122144444.265412-1-gary%40kernel.org
patch subject: [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()`
config: x86_64-buildonly-randconfig-004-20260125 (https://download.01.org/0day-ci/archive/20260125/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
rustc: rustc 1.88.0 (6b00bc388 2025-06-23)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260125/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

Quoted 1 line
Quoted 1 line

warning: unused import: str::CStrExt

--> rust/kernel/task.rs:12:5
|
12 | str::CStrExt,
| ^^^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default

Quoted 7 lines
Quoted 3 lines

Seems like the import needs to be conditional on CONFIG_DEBUG_ATOMIC_SLEEP. Aside: it can also be as _ since it isn't named directly.

Can we just import the prelude instead of having a conditional import?

Alice

I'll do that in v2.

Best,
Gary