From: Greg Kroah-Hartman Date: Mon, 24 Feb 2025 05:47:36 +0000 (+0100) Subject: 6.13-stable patches X-Git-Tag: v6.6.80~26 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=77765910172bd9578e14490619372e5473f34abf;p=thirdparty%2Fkernel%2Fstable-queue.git 6.13-stable patches added patches: rust-cleanup-unnecessary-casts.patch rust-finish-using-custom-ffi-integer-types.patch rust-map-long-to-isize-and-char-to-u8.patch --- diff --git a/queue-6.13/rust-cleanup-unnecessary-casts.patch b/queue-6.13/rust-cleanup-unnecessary-casts.patch new file mode 100644 index 0000000000..89cb408713 --- /dev/null +++ b/queue-6.13/rust-cleanup-unnecessary-casts.patch @@ -0,0 +1,76 @@ +From 9b98be76855f14bd5180b59c1ac646b5add98f33 Mon Sep 17 00:00:00 2001 +From: Gary Guo +Date: Fri, 13 Sep 2024 22:29:25 +0100 +Subject: rust: cleanup unnecessary casts + +From: Gary Guo + +commit 9b98be76855f14bd5180b59c1ac646b5add98f33 upstream. + +With `long` mapped to `isize`, `size_t`/`__kernel_size_t` mapped to +`usize` and `char` mapped to `u8`, many of the existing casts are no +longer necessary. + +Signed-off-by: Gary Guo +Reviewed-by: Alice Ryhl +Link: https://lore.kernel.org/r/20240913213041.395655-6-gary@garyguo.net +[ Moved `uaccess` changes to the previous commit, since they were + irrefutable patterns that Rust >= 1.82.0 warns about. Removed a + couple casts that now use `c""` literals. Rebased on top of + `rust-next`. - Miguel ] +Signed-off-by: Miguel Ojeda +Signed-off-by: Greg Kroah-Hartman +--- + rust/kernel/print.rs | 4 ++-- + rust/kernel/str.rs | 6 +++--- + 2 files changed, 5 insertions(+), 5 deletions(-) + +--- a/rust/kernel/print.rs ++++ b/rust/kernel/print.rs +@@ -107,7 +107,7 @@ pub unsafe fn call_printk( + // SAFETY: TODO. + unsafe { + bindings::_printk( +- format_string.as_ptr() as _, ++ format_string.as_ptr(), + module_name.as_ptr(), + &args as *const _ as *const c_void, + ); +@@ -128,7 +128,7 @@ pub fn call_printk_cont(args: fmt::Argum + #[cfg(CONFIG_PRINTK)] + unsafe { + bindings::_printk( +- format_strings::CONT.as_ptr() as _, ++ format_strings::CONT.as_ptr(), + &args as *const _ as *const c_void, + ); + } +--- a/rust/kernel/str.rs ++++ b/rust/kernel/str.rs +@@ -189,7 +189,7 @@ impl CStr { + // to a `NUL`-terminated C string. + let len = unsafe { bindings::strlen(ptr) } + 1; + // SAFETY: Lifetime guaranteed by the safety precondition. +- let bytes = unsafe { core::slice::from_raw_parts(ptr as _, len as _) }; ++ let bytes = unsafe { core::slice::from_raw_parts(ptr as _, len) }; + // SAFETY: As `len` is returned by `strlen`, `bytes` does not contain interior `NUL`. + // As we have added 1 to `len`, the last byte is known to be `NUL`. + unsafe { Self::from_bytes_with_nul_unchecked(bytes) } +@@ -248,7 +248,7 @@ impl CStr { + /// Returns a C pointer to the string. + #[inline] + pub const fn as_char_ptr(&self) -> *const crate::ffi::c_char { +- self.0.as_ptr() as _ ++ self.0.as_ptr() + } + + /// Convert the string to a byte slice without the trailing `NUL` byte. +@@ -838,7 +838,7 @@ impl CString { + // SAFETY: The buffer is valid for read because `f.bytes_written()` is bounded by `size` + // (which the minimum buffer size) and is non-zero (we wrote at least the `NUL` terminator) + // so `f.bytes_written() - 1` doesn't underflow. +- let ptr = unsafe { bindings::memchr(buf.as_ptr().cast(), 0, (f.bytes_written() - 1) as _) }; ++ let ptr = unsafe { bindings::memchr(buf.as_ptr().cast(), 0, f.bytes_written() - 1) }; + if !ptr.is_null() { + return Err(EINVAL); + } diff --git a/queue-6.13/rust-finish-using-custom-ffi-integer-types.patch b/queue-6.13/rust-finish-using-custom-ffi-integer-types.patch new file mode 100644 index 0000000000..bc88b28265 --- /dev/null +++ b/queue-6.13/rust-finish-using-custom-ffi-integer-types.patch @@ -0,0 +1,116 @@ +From 27c7518e7f1ccaaa43eb5f25dc362779d2dc2ccb Mon Sep 17 00:00:00 2001 +From: Miguel Ojeda +Date: Sun, 15 Dec 2024 22:43:53 +0100 +Subject: rust: finish using custom FFI integer types + +From: Miguel Ojeda + +commit 27c7518e7f1ccaaa43eb5f25dc362779d2dc2ccb upstream. + +In the last kernel cycle we migrated most of the `core::ffi` cases in +commit d072acda4862 ("rust: use custom FFI integer types"): + + Currently FFI integer types are defined in libcore. This commit + creates the `ffi` crate and asks bindgen to use that crate for FFI + integer types instead of `core::ffi`. + + This commit is preparatory and no type changes are made in this + commit yet. + +Finish now the few remaining/new cases so that we perform the actual +remapping in the next commit as planned. + +Acked-by: Jocelyn Falempe # drm +Link: https://lore.kernel.org/rust-for-linux/CANiq72m_rg42SvZK=bF2f0yEoBLVA33UBhiAsv8THhVu=G2dPA@mail.gmail.com/ +Link: https://lore.kernel.org/all/cc9253fa-9d5f-460b-9841-94948fb6580c@redhat.com/ +Signed-off-by: Miguel Ojeda +Signed-off-by: Greg Kroah-Hartman +--- + drivers/gpu/drm/drm_panic_qr.rs | 2 +- + rust/kernel/device.rs | 4 ++-- + rust/kernel/miscdevice.rs | 8 ++------ + rust/kernel/security.rs | 2 +- + rust/kernel/seq_file.rs | 2 +- + samples/rust/rust_print_main.rs | 2 +- + 6 files changed, 8 insertions(+), 12 deletions(-) + +--- a/drivers/gpu/drm/drm_panic_qr.rs ++++ b/drivers/gpu/drm/drm_panic_qr.rs +@@ -931,7 +931,7 @@ impl QrImage<'_> { + /// They must remain valid for the duration of the function call. + #[no_mangle] + pub unsafe extern "C" fn drm_panic_qr_generate( +- url: *const i8, ++ url: *const kernel::ffi::c_char, + data: *mut u8, + data_len: usize, + data_size: usize, +--- a/rust/kernel/device.rs ++++ b/rust/kernel/device.rs +@@ -173,10 +173,10 @@ impl Device { + #[cfg(CONFIG_PRINTK)] + unsafe { + bindings::_dev_printk( +- klevel as *const _ as *const core::ffi::c_char, ++ klevel as *const _ as *const crate::ffi::c_char, + self.as_raw(), + c_str!("%pA").as_char_ptr(), +- &msg as *const _ as *const core::ffi::c_void, ++ &msg as *const _ as *const crate::ffi::c_void, + ) + }; + } +--- a/rust/kernel/miscdevice.rs ++++ b/rust/kernel/miscdevice.rs +@@ -11,16 +11,12 @@ + use crate::{ + bindings, + error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR}, ++ ffi::{c_int, c_long, c_uint, c_ulong}, + prelude::*, + str::CStr, + types::{ForeignOwnable, Opaque}, + }; +-use core::{ +- ffi::{c_int, c_long, c_uint, c_ulong}, +- marker::PhantomData, +- mem::MaybeUninit, +- pin::Pin, +-}; ++use core::{marker::PhantomData, mem::MaybeUninit, pin::Pin}; + + /// Options for creating a misc device. + #[derive(Copy, Clone)] +--- a/rust/kernel/security.rs ++++ b/rust/kernel/security.rs +@@ -19,7 +19,7 @@ use crate::{ + /// successful call to `security_secid_to_secctx`, that has not yet been destroyed by calling + /// `security_release_secctx`. + pub struct SecurityCtx { +- secdata: *mut core::ffi::c_char, ++ secdata: *mut crate::ffi::c_char, + seclen: usize, + } + +--- a/rust/kernel/seq_file.rs ++++ b/rust/kernel/seq_file.rs +@@ -36,7 +36,7 @@ impl SeqFile { + bindings::seq_printf( + self.inner.get(), + c_str!("%pA").as_char_ptr(), +- &args as *const _ as *const core::ffi::c_void, ++ &args as *const _ as *const crate::ffi::c_void, + ); + } + } +--- a/samples/rust/rust_print_main.rs ++++ b/samples/rust/rust_print_main.rs +@@ -83,7 +83,7 @@ impl Drop for RustPrint { + } + + mod trace { +- use core::ffi::c_int; ++ use kernel::ffi::c_int; + + kernel::declare_trace! { + /// # Safety diff --git a/queue-6.13/rust-map-long-to-isize-and-char-to-u8.patch b/queue-6.13/rust-map-long-to-isize-and-char-to-u8.patch new file mode 100644 index 0000000000..090b975876 --- /dev/null +++ b/queue-6.13/rust-map-long-to-isize-and-char-to-u8.patch @@ -0,0 +1,208 @@ +From 1bae8729e50a900f41e9a1c17ae81113e4cf62b8 Mon Sep 17 00:00:00 2001 +From: Gary Guo +Date: Fri, 13 Sep 2024 22:29:24 +0100 +Subject: rust: map `long` to `isize` and `char` to `u8` + +From: Gary Guo + +commit 1bae8729e50a900f41e9a1c17ae81113e4cf62b8 upstream. + +The following FFI types are replaced compared to `core::ffi`: + +1. `char` type is now always mapped to `u8`, since kernel uses + `-funsigned-char` on the C code. `core::ffi` maps it to platform + default ABI, which can be either signed or unsigned. + +2. `long` is now always mapped to `isize`. It's very common in the + kernel to use `long` to represent a pointer-sized integer, and in + fact `intptr_t` is a typedef of `long` in the kernel. Enforce this + mapping rather than mapping to `i32/i64` depending on platform can + save us a lot of unnecessary casts. + +Signed-off-by: Gary Guo +Reviewed-by: Alice Ryhl +Link: https://lore.kernel.org/r/20240913213041.395655-5-gary@garyguo.net +[ Moved `uaccess` changes from the next commit, since they were + irrefutable patterns that Rust >= 1.82.0 warns about. Reworded + slightly and reformatted a few documentation comments. Rebased on + top of `rust-next`. Added the removal of two casts to avoid Clippy + warnings. - Miguel ] +Signed-off-by: Miguel Ojeda +Signed-off-by: Greg Kroah-Hartman +--- + rust/ffi.rs | 37 ++++++++++++++++++++++++++++++++++++- + rust/kernel/error.rs | 5 +---- + rust/kernel/firmware.rs | 2 +- + rust/kernel/miscdevice.rs | 4 ++-- + rust/kernel/uaccess.rs | 27 +++++++-------------------- + 5 files changed, 47 insertions(+), 28 deletions(-) + +--- a/rust/ffi.rs ++++ b/rust/ffi.rs +@@ -10,4 +10,39 @@ + + #![no_std] + +-pub use core::ffi::*; ++macro_rules! alias { ++ ($($name:ident = $ty:ty;)*) => {$( ++ #[allow(non_camel_case_types, missing_docs)] ++ pub type $name = $ty; ++ ++ // Check size compatibility with `core`. ++ const _: () = assert!( ++ core::mem::size_of::<$name>() == core::mem::size_of::() ++ ); ++ )*} ++} ++ ++alias! { ++ // `core::ffi::c_char` is either `i8` or `u8` depending on architecture. In the kernel, we use ++ // `-funsigned-char` so it's always mapped to `u8`. ++ c_char = u8; ++ ++ c_schar = i8; ++ c_uchar = u8; ++ ++ c_short = i16; ++ c_ushort = u16; ++ ++ c_int = i32; ++ c_uint = u32; ++ ++ // In the kernel, `intptr_t` is defined to be `long` in all platforms, so we can map the type to ++ // `isize`. ++ c_long = isize; ++ c_ulong = usize; ++ ++ c_longlong = i64; ++ c_ulonglong = u64; ++} ++ ++pub use core::ffi::c_void; +--- a/rust/kernel/error.rs ++++ b/rust/kernel/error.rs +@@ -153,11 +153,8 @@ impl Error { + + /// Returns the error encoded as a pointer. + pub fn to_ptr(self) -> *mut T { +- #[cfg_attr(target_pointer_width = "32", allow(clippy::useless_conversion))] + // SAFETY: `self.0` is a valid error due to its invariant. +- unsafe { +- bindings::ERR_PTR(self.0.get().into()) as *mut _ +- } ++ unsafe { bindings::ERR_PTR(self.0.get() as _) as *mut _ } + } + + /// Returns a string representing the error, if one exists. +--- a/rust/kernel/firmware.rs ++++ b/rust/kernel/firmware.rs +@@ -12,7 +12,7 @@ use core::ptr::NonNull; + /// One of the following: `bindings::request_firmware`, `bindings::firmware_request_nowarn`, + /// `bindings::firmware_request_platform`, `bindings::request_firmware_direct`. + struct FwFunc( +- unsafe extern "C" fn(*mut *const bindings::firmware, *const i8, *mut bindings::device) -> i32, ++ unsafe extern "C" fn(*mut *const bindings::firmware, *const u8, *mut bindings::device) -> i32, + ); + + impl FwFunc { +--- a/rust/kernel/miscdevice.rs ++++ b/rust/kernel/miscdevice.rs +@@ -225,7 +225,7 @@ unsafe extern "C" fn fops_ioctl::borrow(private) }; + +- match T::ioctl(device, cmd, arg as usize) { ++ match T::ioctl(device, cmd, arg) { + Ok(ret) => ret as c_long, + Err(err) => err.to_errno() as c_long, + } +@@ -245,7 +245,7 @@ unsafe extern "C" fn fops_compat_ioctl::borrow(private) }; + +- match T::compat_ioctl(device, cmd, arg as usize) { ++ match T::compat_ioctl(device, cmd, arg) { + Ok(ret) => ret as c_long, + Err(err) => err.to_errno() as c_long, + } +--- a/rust/kernel/uaccess.rs ++++ b/rust/kernel/uaccess.rs +@@ -8,7 +8,7 @@ use crate::{ + alloc::Flags, + bindings, + error::Result, +- ffi::{c_ulong, c_void}, ++ ffi::c_void, + prelude::*, + transmute::{AsBytes, FromBytes}, + }; +@@ -224,13 +224,9 @@ impl UserSliceReader { + if len > self.length { + return Err(EFAULT); + } +- let Ok(len_ulong) = c_ulong::try_from(len) else { +- return Err(EFAULT); +- }; +- // SAFETY: `out_ptr` points into a mutable slice of length `len_ulong`, so we may write ++ // SAFETY: `out_ptr` points into a mutable slice of length `len`, so we may write + // that many bytes to it. +- let res = +- unsafe { bindings::copy_from_user(out_ptr, self.ptr as *const c_void, len_ulong) }; ++ let res = unsafe { bindings::copy_from_user(out_ptr, self.ptr as *const c_void, len) }; + if res != 0 { + return Err(EFAULT); + } +@@ -259,9 +255,6 @@ impl UserSliceReader { + if len > self.length { + return Err(EFAULT); + } +- let Ok(len_ulong) = c_ulong::try_from(len) else { +- return Err(EFAULT); +- }; + let mut out: MaybeUninit = MaybeUninit::uninit(); + // SAFETY: The local variable `out` is valid for writing `size_of::()` bytes. + // +@@ -272,7 +265,7 @@ impl UserSliceReader { + bindings::_copy_from_user( + out.as_mut_ptr().cast::(), + self.ptr as *const c_void, +- len_ulong, ++ len, + ) + }; + if res != 0 { +@@ -335,12 +328,9 @@ impl UserSliceWriter { + if len > self.length { + return Err(EFAULT); + } +- let Ok(len_ulong) = c_ulong::try_from(len) else { +- return Err(EFAULT); +- }; +- // SAFETY: `data_ptr` points into an immutable slice of length `len_ulong`, so we may read ++ // SAFETY: `data_ptr` points into an immutable slice of length `len`, so we may read + // that many bytes from it. +- let res = unsafe { bindings::copy_to_user(self.ptr as *mut c_void, data_ptr, len_ulong) }; ++ let res = unsafe { bindings::copy_to_user(self.ptr as *mut c_void, data_ptr, len) }; + if res != 0 { + return Err(EFAULT); + } +@@ -359,9 +349,6 @@ impl UserSliceWriter { + if len > self.length { + return Err(EFAULT); + } +- let Ok(len_ulong) = c_ulong::try_from(len) else { +- return Err(EFAULT); +- }; + // SAFETY: The reference points to a value of type `T`, so it is valid for reading + // `size_of::()` bytes. + // +@@ -372,7 +359,7 @@ impl UserSliceWriter { + bindings::_copy_to_user( + self.ptr as *mut c_void, + (value as *const T).cast::(), +- len_ulong, ++ len, + ) + }; + if res != 0 { diff --git a/queue-6.13/series b/queue-6.13/series index b089b05c8f..c22fe2c415 100644 --- a/queue-6.13/series +++ b/queue-6.13/series @@ -53,3 +53,6 @@ net-make-netdev_lock-protect-netdev-reg_state.patch net-add-netdev-up-protected-by-netdev_lock.patch net-protect-netdev-napi_list-with-netdev_lock.patch revert-net-skb-introduce-and-use-a-single-page-frag-.patch +rust-finish-using-custom-ffi-integer-types.patch +rust-map-long-to-isize-and-char-to-u8.patch +rust-cleanup-unnecessary-casts.patch