lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <6801369a.050a0220.2b2efe.0e72@mx.google.com>
Date: Thu, 17 Apr 2025 10:12:55 -0700
From: Boqun Feng <boqun.feng@...il.com>
To: Tamir Duberstein <tamird@...il.com>
Cc: Masahiro Yamada <masahiroy@...nel.org>,
	Nathan Chancellor <nathan@...nel.org>,	Miguel Ojeda <ojeda@...nel.org>,
	Alex Gaynor <alex.gaynor@...il.com>, Gary Guo <gary@...yguo.net>,
	Björn Roy Baron <bjorn3_gh@...tonmail.com>,
	Benno Lossin <benno.lossin@...ton.me>,
	Andreas Hindborg <a.hindborg@...nel.org>,
	Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>,
	Danilo Krummrich <dakr@...nel.org>,
	Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
	"Rafael J. Wysocki" <rafael@...nel.org>,
	Brendan Higgins <brendan.higgins@...ux.dev>,
	David Gow <davidgow@...gle.com>, Rae Moar <rmoar@...gle.com>,
	Bjorn Helgaas <bhelgaas@...gle.com>,
	Luis Chamberlain <mcgrof@...nel.org>,
	Russ Weight <russ.weight@...ux.dev>, Rob Herring <robh@...nel.org>,
	Saravana Kannan <saravanak@...gle.com>,
	Abdiel Janulgue <abdiel.janulgue@...il.com>,
	Daniel Almeida <daniel.almeida@...labora.com>,
	Robin Murphy <robin.murphy@....com>,
	Maarten Lankhorst <maarten.lankhorst@...ux.intel.com>,
	Maxime Ripard <mripard@...nel.org>,
	Thomas Zimmermann <tzimmermann@...e.de>,
	David Airlie <airlied@...il.com>, Simona Vetter <simona@...ll.ch>,
	FUJITA Tomonori <fujita.tomonori@...il.com>,
	Nicolas Schier <nicolas.schier@...ux.dev>,
	Frederic Weisbecker <frederic@...nel.org>,	Lyude Paul <lyude@...hat.com>,
 Thomas Gleixner <tglx@...utronix.de>,
	Anna-Maria Behnsen <anna-maria@...utronix.de>,
	linux-kbuild@...r.kernel.org, linux-kernel@...r.kernel.org,
	rust-for-linux@...r.kernel.org, linux-kselftest@...r.kernel.org,
	kunit-dev@...glegroups.com, linux-pci@...r.kernel.org,
	linux-block@...r.kernel.org, devicetree@...r.kernel.org,
	dri-devel@...ts.freedesktop.org, netdev@...r.kernel.org
Subject: Re: [PATCH v9 2/6] rust: enable `clippy::ptr_cast_constness` lint

On Wed, Apr 16, 2025 at 01:36:06PM -0400, Tamir Duberstein wrote:
> In Rust 1.72.0, Clippy introduced the `ptr_cast_constness` lint [1]:
> 
> > Though `as` casts between raw pointers are not terrible,
> > `pointer::cast_mut` and `pointer::cast_const` are safer because they
> > cannot accidentally cast the pointer to another type.
> 
> There are only 2 affected sites:
> - `*mut T as *const U as *mut U` becomes `(*mut T).cast()`
> - `&self as *const Self as *mut Self` becomes
>   `core::ptr::from_ref(self).cast_mut()`.
> 
> Apply these changes and enable the lint -- no functional change
> intended.
> 
> Link: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_cast_constness [1]
> Reviewed-by: Benno Lossin <benno.lossin@...ton.me>
> Signed-off-by: Tamir Duberstein <tamird@...il.com>
> ---
>  Makefile                        | 1 +
>  rust/kernel/block/mq/request.rs | 4 ++--
>  rust/kernel/dma.rs              | 2 +-
>  3 files changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index 5d2931344490..7b85b2a8d371 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -481,6 +481,7 @@ export rust_common_flags := --edition=2021 \
>  			    -Aclippy::needless_lifetimes \
>  			    -Wclippy::no_mangle_with_rust_abi \
>  			    -Wclippy::ptr_as_ptr \
> +			    -Wclippy::ptr_cast_constness \
>  			    -Wclippy::undocumented_unsafe_blocks \
>  			    -Wclippy::unnecessary_safety_comment \
>  			    -Wclippy::unnecessary_safety_doc \
> diff --git a/rust/kernel/block/mq/request.rs b/rust/kernel/block/mq/request.rs
> index 4a5b7ec914ef..af5c9ac94f36 100644
> --- a/rust/kernel/block/mq/request.rs
> +++ b/rust/kernel/block/mq/request.rs
> @@ -69,7 +69,7 @@ pub(crate) unsafe fn aref_from_raw(ptr: *mut bindings::request) -> ARef<Self> {
>          // INVARIANT: By the safety requirements of this function, invariants are upheld.
>          // SAFETY: By the safety requirement of this function, we own a
>          // reference count that we can pass to `ARef`.
> -        unsafe { ARef::from_raw(NonNull::new_unchecked(ptr as *const Self as *mut Self)) }
> +        unsafe { ARef::from_raw(NonNull::new_unchecked(ptr.cast())) }
>      }
>  
>      /// Notify the block layer that a request is going to be processed now.
> @@ -155,7 +155,7 @@ pub(crate) fn wrapper_ref(&self) -> &RequestDataWrapper {
>          // the private data associated with this request is initialized and
>          // valid. The existence of `&self` guarantees that the private data is
>          // valid as a shared reference.
> -        unsafe { Self::wrapper_ptr(self as *const Self as *mut Self).as_ref() }
> +        unsafe { Self::wrapper_ptr(core::ptr::from_ref(self).cast_mut()).as_ref() }
>      }
>  }
>  
> diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
> index f395d1a6fe48..43ecf3c2e860 100644
> --- a/rust/kernel/dma.rs
> +++ b/rust/kernel/dma.rs
> @@ -186,7 +186,7 @@ pub fn alloc_attrs(
>              dev: dev.into(),
>              dma_handle,
>              count,
> -            cpu_addr: ret.cast(),
> +            cpu_addr: ret.cast::<T>(),

Is this change necessary? The rest looks good to me.

Regards,
Boqun

>              dma_attrs,
>          })
>      }
> 
> -- 
> 2.49.0
> 

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ