public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()`
@ 2026-01-22 14:44 Gary Guo
  2026-01-22 14:44 ` [PATCH v2 2/2] rust: disallow use of `CStr::as_ptr` Gary Guo
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Gary Guo @ 2026-01-22 14:44 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Panagiotis Foliadis, Shankari Anand,
	FUJITA Tomonori
  Cc: kernel test robot, rust-for-linux, linux-kernel

From: Gary Guo <gary@garyguo.net>

`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.

Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202601221157.89t3Sqbl-lkp@intel.com/
Signed-off-by: Gary Guo <gary@garyguo.net>
---
 rust/kernel/task.rs | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs
index cc907fb531bc..7ccb20a8f813 100644
--- a/rust/kernel/task.rs
+++ b/rust/kernel/task.rs
@@ -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
-- 
2.51.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 2/2] rust: disallow use of `CStr::as_ptr`
  2026-01-22 14:44 [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()` Gary Guo
@ 2026-01-22 14:44 ` Gary Guo
  2026-01-22 14:48 ` [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()` Gary Guo
  2026-01-24 23:33 ` kernel test robot
  2 siblings, 0 replies; 8+ messages in thread
From: Gary Guo @ 2026-01-22 14:44 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Panagiotis Foliadis, Shankari Anand,
	FUJITA Tomonori, Tamir Duberstein
  Cc: kernel test robot, rust-for-linux, linux-kernel, Tamir Duberstein

From: Gary Guo <gary@garyguo.net>

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.

Acked-by: Tamir Duberstein <tamird@kernel.org>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Gary Guo <gary@garyguo.net>
---
 .clippy.toml       | 5 +++++
 rust/kernel/str.rs | 2 ++
 2 files changed, 7 insertions(+)

diff --git a/.clippy.toml b/.clippy.toml
index 137f41d203de..4654b522ae03 100644
--- a/.clippy.toml
+++ b/.clippy.toml
@@ -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"
diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index fa87779d2253..08b8e2ebc8ad 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -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()
     }
-- 
2.51.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()`
  2026-01-22 14:44 [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()` Gary Guo
  2026-01-22 14:44 ` [PATCH v2 2/2] rust: disallow use of `CStr::as_ptr` Gary Guo
@ 2026-01-22 14:48 ` Gary Guo
  2026-01-22 22:33   ` Tamir Duberstein
  2026-01-24 23:33 ` kernel test robot
  2 siblings, 1 reply; 8+ messages in thread
From: Gary Guo @ 2026-01-22 14:48 UTC (permalink / raw)
  To: Gary Guo, Miguel Ojeda, Boqun Feng, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Panagiotis Foliadis, Shankari Anand,
	FUJITA Tomonori
  Cc: kernel test robot, rust-for-linux, linux-kernel

On Thu Jan 22, 2026 at 2:44 PM GMT, Gary Guo wrote:
> From: Gary Guo <gary@garyguo.net>
>
> `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.
>
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202601221157.89t3Sqbl-lkp@intel.com/
> Signed-off-by: Gary Guo <gary@garyguo.net>

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/202601221157.89t3Sqbl-lkp@intel.com/T/#m8ca5da7e22ff7ac6caff83326f4b81e0e9e3536f

> ---
>  rust/kernel/task.rs | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs
> index cc907fb531bc..7ccb20a8f813 100644
> --- a/rust/kernel/task.rs
> +++ b/rust/kernel/task.rs
> @@ -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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()`
  2026-01-22 14:48 ` [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()` Gary Guo
@ 2026-01-22 22:33   ` Tamir Duberstein
  0 siblings, 0 replies; 8+ messages in thread
From: Tamir Duberstein @ 2026-01-22 22:33 UTC (permalink / raw)
  To: Gary Guo
  Cc: Miguel Ojeda, Boqun Feng, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Panagiotis Foliadis, Shankari Anand, FUJITA Tomonori,
	kernel test robot, rust-for-linux, linux-kernel

On Thu, Jan 22, 2026 at 9:56 AM Gary Guo <gary@garyguo.net> wrote:
>
> On Thu Jan 22, 2026 at 2:44 PM GMT, Gary Guo wrote:
> > From: Gary Guo <gary@garyguo.net>
> >
> > `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.
> >
> > Reported-by: kernel test robot <lkp@intel.com>
> > Closes: https://lore.kernel.org/oe-kbuild-all/202601221157.89t3Sqbl-lkp@intel.com/
> > Signed-off-by: Gary Guo <gary@garyguo.net>

Reviewed-by: Tamir Duberstein <tamird@kernel.org>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()`
  2026-01-22 14:44 [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()` Gary Guo
  2026-01-22 14:44 ` [PATCH v2 2/2] rust: disallow use of `CStr::as_ptr` Gary Guo
  2026-01-22 14:48 ` [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()` Gary Guo
@ 2026-01-24 23:33 ` kernel test robot
  2026-01-25 20:19   ` Tamir Duberstein
  2 siblings, 1 reply; 8+ messages in thread
From: kernel test robot @ 2026-01-24 23:33 UTC (permalink / raw)
  To: Gary Guo, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Panagiotis Foliadis,
	Shankari Anand, FUJITA Tomonori
  Cc: llvm, oe-kbuild-all, kernel test robot, rust-for-linux,
	linux-kernel

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/202601250701.wXxtGd9z-lkp@intel.com/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/202601250701.wXxtGd9z-lkp@intel.com/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 <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202601250701.wXxtGd9z-lkp@intel.com/

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

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()`
  2026-01-24 23:33 ` kernel test robot
@ 2026-01-25 20:19   ` Tamir Duberstein
  2026-01-26  9:22     ` Alice Ryhl
  0 siblings, 1 reply; 8+ messages in thread
From: Tamir Duberstein @ 2026-01-25 20:19 UTC (permalink / raw)
  To: kernel test robot
  Cc: Gary Guo, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Panagiotis Foliadis,
	Shankari Anand, FUJITA Tomonori, llvm, oe-kbuild-all,
	rust-for-linux, linux-kernel

On Sat, Jan 24, 2026 at 6:34 PM kernel test robot <lkp@intel.com> wrote:
>
> 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/202601250701.wXxtGd9z-lkp@intel.com/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/202601250701.wXxtGd9z-lkp@intel.com/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 <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202601250701.wXxtGd9z-lkp@intel.com/
>
> 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

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.

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()`
  2026-01-25 20:19   ` Tamir Duberstein
@ 2026-01-26  9:22     ` Alice Ryhl
  2026-01-26 12:25       ` Gary Guo
  0 siblings, 1 reply; 8+ messages in thread
From: Alice Ryhl @ 2026-01-26  9:22 UTC (permalink / raw)
  To: Tamir Duberstein
  Cc: kernel test robot, Gary Guo, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Danilo Krummrich, Panagiotis Foliadis,
	Shankari Anand, FUJITA Tomonori, llvm, oe-kbuild-all,
	rust-for-linux, linux-kernel

On Sun, Jan 25, 2026 at 03:19:53PM -0500, Tamir Duberstein wrote:
> On Sat, Jan 24, 2026 at 6:34 PM kernel test robot <lkp@intel.com> wrote:
> >
> > 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/202601250701.wXxtGd9z-lkp@intel.com/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/202601250701.wXxtGd9z-lkp@intel.com/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 <lkp@intel.com>
> > | Closes: https://lore.kernel.org/oe-kbuild-all/202601250701.wXxtGd9z-lkp@intel.com/
> >
> > 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
> 
> 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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()`
  2026-01-26  9:22     ` Alice Ryhl
@ 2026-01-26 12:25       ` Gary Guo
  0 siblings, 0 replies; 8+ messages in thread
From: Gary Guo @ 2026-01-26 12:25 UTC (permalink / raw)
  To: Alice Ryhl, Tamir Duberstein
  Cc: kernel test robot, Gary Guo, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Danilo Krummrich, Panagiotis Foliadis,
	Shankari Anand, FUJITA Tomonori, llvm, oe-kbuild-all,
	rust-for-linux, linux-kernel

On Mon Jan 26, 2026 at 9:22 AM GMT, Alice Ryhl wrote:
> On Sun, Jan 25, 2026 at 03:19:53PM -0500, Tamir Duberstein wrote:
>> On Sat, Jan 24, 2026 at 6:34 PM kernel test robot <lkp@intel.com> wrote:
>> >
>> > 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/202601250701.wXxtGd9z-lkp@intel.com/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/202601250701.wXxtGd9z-lkp@intel.com/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 <lkp@intel.com>
>> > | Closes: https://lore.kernel.org/oe-kbuild-all/202601250701.wXxtGd9z-lkp@intel.com/
>> >
>> > 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
>> 
>> 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

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-01-26 12:25 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-22 14:44 [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()` Gary Guo
2026-01-22 14:44 ` [PATCH v2 2/2] rust: disallow use of `CStr::as_ptr` Gary Guo
2026-01-22 14:48 ` [PATCH v2 1/2] rust: task: use `as_char_ptr` instead of `as_ptr().cast()` Gary Guo
2026-01-22 22:33   ` Tamir Duberstein
2026-01-24 23:33 ` kernel test robot
2026-01-25 20:19   ` Tamir Duberstein
2026-01-26  9:22     ` Alice Ryhl
2026-01-26 12:25       ` Gary Guo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox