mirror of
https://github.com/torvalds/linux.git
synced 2026-04-18 14:53:58 -04:00
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 <mmaurer@google.com> Acked-by: Danilo Krummrich <dakr@kernel.org> Tested-by: David Gow <davidgow@google.com> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Link: https://patch.msgid.link/20251022035324.70785-4-boqun.feng@gmail.com
This commit is contained in:
@@ -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<T: FromStr + Unpin> Reader for Mutex<T> {
|
||||
}
|
||||
}
|
||||
|
||||
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<T: AtomicType + FromStr> Reader for Atomic<T>
|
||||
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::<T>().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)
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user