From 37d0472c8ac441af8bc10fc4959ad9d62dd5fa4c Mon Sep 17 00:00:00 2001 From: Boqun Feng Date: Tue, 21 Oct 2025 23:42:37 -0400 Subject: rust: debugfs: Implement Reader for Mutex only when T is Unpin Since we are going to make `Mutex` structurally pin the data (i.e. `T`), therefore `.lock()` function only returns a `Guard` that can dereference a mutable reference to `T` if only `T` is `Unpin`, therefore restrict the impl `Reader` block of `Mutex` to that. Signed-off-by: Boqun Feng Signed-off-by: Peter Zijlstra (Intel) Acked-by: Danilo Krummrich Link: https://patch.msgid.link/20251022034237.70431-1-boqun.feng@gmail.com --- rust/kernel/debugfs/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'rust/kernel/debugfs/traits.rs') diff --git a/rust/kernel/debugfs/traits.rs b/rust/kernel/debugfs/traits.rs index ab009eb254b3..ba7ec5a900b8 100644 --- a/rust/kernel/debugfs/traits.rs +++ b/rust/kernel/debugfs/traits.rs @@ -50,7 +50,7 @@ pub trait Reader { fn read_from_slice(&self, reader: &mut UserSliceReader) -> Result; } -impl Reader for Mutex { +impl Reader for Mutex { fn read_from_slice(&self, reader: &mut UserSliceReader) -> Result { let mut buf = [0u8; 128]; if reader.len() > buf.len() { -- cgit v1.2.3 From f74cf399e02e24c544b0bd4b1fe8fa2c5ae30b18 Mon Sep 17 00:00:00 2001 From: Boqun Feng Date: Tue, 21 Oct 2025 23:53:24 -0400 Subject: rust: debugfs: Replace the usage of Rust native atomics Rust native atomics are not allowed to use in kernel due to the mismatch of memory model with Linux kernel memory model, hence remove the usage of Rust native atomics in debufs. Reviewed-by: Matthew Maurer Acked-by: Danilo Krummrich Tested-by: David Gow Acked-by: Greg Kroah-Hartman Signed-off-by: Boqun Feng Link: https://patch.msgid.link/20251022035324.70785-4-boqun.feng@gmail.com --- rust/kernel/debugfs/traits.rs | 53 ++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 36 deletions(-) (limited to 'rust/kernel/debugfs/traits.rs') diff --git a/rust/kernel/debugfs/traits.rs b/rust/kernel/debugfs/traits.rs index ba7ec5a900b8..92054fed2136 100644 --- a/rust/kernel/debugfs/traits.rs +++ b/rust/kernel/debugfs/traits.rs @@ -4,14 +4,11 @@ //! Traits for rendering or updating values exported to DebugFS. use crate::prelude::*; +use crate::sync::atomic::{Atomic, AtomicBasicOps, AtomicType, Relaxed}; use crate::sync::Mutex; use crate::uaccess::UserSliceReader; use core::fmt::{self, Debug, Formatter}; use core::str::FromStr; -use core::sync::atomic::{ - AtomicI16, AtomicI32, AtomicI64, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU64, - AtomicU8, AtomicUsize, Ordering, -}; /// A trait for types that can be written into a string. /// @@ -66,37 +63,21 @@ impl Reader for Mutex { } } -macro_rules! impl_reader_for_atomic { - ($(($atomic_type:ty, $int_type:ty)),*) => { - $( - impl Reader for $atomic_type { - fn read_from_slice(&self, reader: &mut UserSliceReader) -> Result { - let mut buf = [0u8; 21]; // Enough for a 64-bit number. - if reader.len() > buf.len() { - return Err(EINVAL); - } - let n = reader.len(); - reader.read_slice(&mut buf[..n])?; +impl Reader for Atomic +where + T::Repr: AtomicBasicOps, +{ + fn read_from_slice(&self, reader: &mut UserSliceReader) -> Result { + let mut buf = [0u8; 21]; // Enough for a 64-bit number. + if reader.len() > buf.len() { + return Err(EINVAL); + } + let n = reader.len(); + reader.read_slice(&mut buf[..n])?; - let s = core::str::from_utf8(&buf[..n]).map_err(|_| EINVAL)?; - let val = s.trim().parse::<$int_type>().map_err(|_| EINVAL)?; - self.store(val, Ordering::Relaxed); - Ok(()) - } - } - )* - }; + let s = core::str::from_utf8(&buf[..n]).map_err(|_| EINVAL)?; + let val = s.trim().parse::().map_err(|_| EINVAL)?; + self.store(val, Relaxed); + Ok(()) + } } - -impl_reader_for_atomic!( - (AtomicI16, i16), - (AtomicI32, i32), - (AtomicI64, i64), - (AtomicI8, i8), - (AtomicIsize, isize), - (AtomicU16, u16), - (AtomicU32, u32), - (AtomicU64, u64), - (AtomicU8, u8), - (AtomicUsize, usize) -); -- cgit v1.2.3