mirror of
https://github.com/torvalds/linux.git
synced 2025-12-07 20:06:24 +00:00
rust: debugfs: support blobs from smart pointers
Extend Rust debugfs binary support to allow exposing data stored in common smart pointers and heap-allocated collections. - Implement BinaryWriter for Box<T>, Pin<Box<T>>, Arc<T>, and Vec<T>. - Introduce BinaryReaderMut for mutable binary access with outer locks. - Implement BinaryReaderMut for Box<T>, Vec<T>, and base types. - Update BinaryReader to delegate to BinaryReaderMut for Mutex<T>, Box<T>, Pin<Box<T>> and Arc<T>. This enables debugfs files to directly expose or update data stored inside heap-allocated, reference-counted, or lock-protected containers without manual dereferencing or locking. Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Matthew Maurer <mmaurer@google.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
@@ -21,7 +21,7 @@ use core::mem::ManuallyDrop;
|
||||
use core::ops::Deref;
|
||||
|
||||
mod traits;
|
||||
pub use traits::{BinaryReader, BinaryWriter, Reader, Writer};
|
||||
pub use traits::{BinaryReader, BinaryReaderMut, BinaryWriter, Reader, Writer};
|
||||
|
||||
mod callback_adapters;
|
||||
use callback_adapters::{FormatAdapter, NoWriter, WritableAdapter};
|
||||
|
||||
@@ -3,12 +3,15 @@
|
||||
|
||||
//! Traits for rendering or updating values exported to DebugFS.
|
||||
|
||||
use crate::alloc::Allocator;
|
||||
use crate::fs::file;
|
||||
use crate::prelude::*;
|
||||
use crate::sync::Arc;
|
||||
use crate::sync::Mutex;
|
||||
use crate::transmute::{AsBytes, FromBytes};
|
||||
use crate::uaccess::{UserSliceReader, UserSliceWriter};
|
||||
use core::fmt::{self, Debug, Formatter};
|
||||
use core::ops::{Deref, DerefMut};
|
||||
use core::str::FromStr;
|
||||
use core::sync::atomic::{
|
||||
AtomicI16, AtomicI32, AtomicI64, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU64,
|
||||
@@ -79,6 +82,72 @@ impl<T: BinaryWriter> BinaryWriter for Mutex<T> {
|
||||
}
|
||||
}
|
||||
|
||||
// Delegate for `Box<T, A>`: Support a `Box<T, A>` with no lock or an inner lock.
|
||||
impl<T, A> BinaryWriter for Box<T, A>
|
||||
where
|
||||
T: BinaryWriter,
|
||||
A: Allocator,
|
||||
{
|
||||
fn write_to_slice(
|
||||
&self,
|
||||
writer: &mut UserSliceWriter,
|
||||
offset: &mut file::Offset,
|
||||
) -> Result<usize> {
|
||||
self.deref().write_to_slice(writer, offset)
|
||||
}
|
||||
}
|
||||
|
||||
// Delegate for `Pin<Box<T, A>>`: Support a `Pin<Box<T, A>>` with no lock or an inner lock.
|
||||
impl<T, A> BinaryWriter for Pin<Box<T, A>>
|
||||
where
|
||||
T: BinaryWriter,
|
||||
A: Allocator,
|
||||
{
|
||||
fn write_to_slice(
|
||||
&self,
|
||||
writer: &mut UserSliceWriter,
|
||||
offset: &mut file::Offset,
|
||||
) -> Result<usize> {
|
||||
self.deref().write_to_slice(writer, offset)
|
||||
}
|
||||
}
|
||||
|
||||
// Delegate for `Arc<T>`: Support a `Arc<T>` with no lock or an inner lock.
|
||||
impl<T> BinaryWriter for Arc<T>
|
||||
where
|
||||
T: BinaryWriter,
|
||||
{
|
||||
fn write_to_slice(
|
||||
&self,
|
||||
writer: &mut UserSliceWriter,
|
||||
offset: &mut file::Offset,
|
||||
) -> Result<usize> {
|
||||
self.deref().write_to_slice(writer, offset)
|
||||
}
|
||||
}
|
||||
|
||||
// Delegate for `Vec<T, A>`.
|
||||
impl<T, A> BinaryWriter for Vec<T, A>
|
||||
where
|
||||
T: AsBytes,
|
||||
A: Allocator,
|
||||
{
|
||||
fn write_to_slice(
|
||||
&self,
|
||||
writer: &mut UserSliceWriter,
|
||||
offset: &mut file::Offset,
|
||||
) -> Result<usize> {
|
||||
let slice = self.as_slice();
|
||||
|
||||
// SAFETY: `T: AsBytes` allows us to treat `&[T]` as `&[u8]`.
|
||||
let buffer = unsafe {
|
||||
core::slice::from_raw_parts(slice.as_ptr().cast(), core::mem::size_of_val(slice))
|
||||
};
|
||||
|
||||
writer.write_slice_file(buffer, offset)
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait for types that can be updated from a user slice.
|
||||
///
|
||||
/// This works similarly to `FromStr`, but operates on a `UserSliceReader` rather than a &str.
|
||||
@@ -107,6 +176,73 @@ impl<T: FromStr> Reader for Mutex<T> {
|
||||
}
|
||||
|
||||
/// Trait for types that can be constructed from a binary representation.
|
||||
///
|
||||
/// See also [`BinaryReader`] for interior mutability.
|
||||
pub trait BinaryReaderMut {
|
||||
/// Reads the binary form of `self` from `reader`.
|
||||
///
|
||||
/// Same as [`BinaryReader::read_from_slice`], but takes a mutable reference.
|
||||
///
|
||||
/// `offset` is the requested offset into the binary representation of `self`.
|
||||
///
|
||||
/// On success, returns the number of bytes read from `reader`.
|
||||
fn read_from_slice_mut(
|
||||
&mut self,
|
||||
reader: &mut UserSliceReader,
|
||||
offset: &mut file::Offset,
|
||||
) -> Result<usize>;
|
||||
}
|
||||
|
||||
// Base implementation for any `T: AsBytes + FromBytes`.
|
||||
impl<T: AsBytes + FromBytes> BinaryReaderMut for T {
|
||||
fn read_from_slice_mut(
|
||||
&mut self,
|
||||
reader: &mut UserSliceReader,
|
||||
offset: &mut file::Offset,
|
||||
) -> Result<usize> {
|
||||
reader.read_slice_file(self.as_bytes_mut(), offset)
|
||||
}
|
||||
}
|
||||
|
||||
// Delegate for `Box<T, A>`: Support a `Box<T, A>` with an outer lock.
|
||||
impl<T: ?Sized + BinaryReaderMut, A: Allocator> BinaryReaderMut for Box<T, A> {
|
||||
fn read_from_slice_mut(
|
||||
&mut self,
|
||||
reader: &mut UserSliceReader,
|
||||
offset: &mut file::Offset,
|
||||
) -> Result<usize> {
|
||||
self.deref_mut().read_from_slice_mut(reader, offset)
|
||||
}
|
||||
}
|
||||
|
||||
// Delegate for `Vec<T, A>`: Support a `Vec<T, A>` with an outer lock.
|
||||
impl<T, A> BinaryReaderMut for Vec<T, A>
|
||||
where
|
||||
T: AsBytes + FromBytes,
|
||||
A: Allocator,
|
||||
{
|
||||
fn read_from_slice_mut(
|
||||
&mut self,
|
||||
reader: &mut UserSliceReader,
|
||||
offset: &mut file::Offset,
|
||||
) -> Result<usize> {
|
||||
let slice = self.as_mut_slice();
|
||||
|
||||
// SAFETY: `T: AsBytes + FromBytes` allows us to treat `&mut [T]` as `&mut [u8]`.
|
||||
let buffer = unsafe {
|
||||
core::slice::from_raw_parts_mut(
|
||||
slice.as_mut_ptr().cast(),
|
||||
core::mem::size_of_val(slice),
|
||||
)
|
||||
};
|
||||
|
||||
reader.read_slice_file(buffer, offset)
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for types that can be constructed from a binary representation.
|
||||
///
|
||||
/// See also [`BinaryReaderMut`] for the mutable version.
|
||||
pub trait BinaryReader {
|
||||
/// Reads the binary form of `self` from `reader`.
|
||||
///
|
||||
@@ -120,7 +256,8 @@ pub trait BinaryReader {
|
||||
) -> Result<usize>;
|
||||
}
|
||||
|
||||
impl<T: AsBytes + FromBytes> BinaryReader for Mutex<T> {
|
||||
// Delegate for `Mutex<T>`: Support a `T` with an outer `Mutex`.
|
||||
impl<T: BinaryReaderMut> BinaryReader for Mutex<T> {
|
||||
fn read_from_slice(
|
||||
&self,
|
||||
reader: &mut UserSliceReader,
|
||||
@@ -128,7 +265,40 @@ impl<T: AsBytes + FromBytes> BinaryReader for Mutex<T> {
|
||||
) -> Result<usize> {
|
||||
let mut this = self.lock();
|
||||
|
||||
reader.read_slice_file(this.as_bytes_mut(), offset)
|
||||
this.read_from_slice_mut(reader, offset)
|
||||
}
|
||||
}
|
||||
|
||||
// Delegate for `Box<T, A>`: Support a `Box<T, A>` with an inner lock.
|
||||
impl<T: ?Sized + BinaryReader, A: Allocator> BinaryReader for Box<T, A> {
|
||||
fn read_from_slice(
|
||||
&self,
|
||||
reader: &mut UserSliceReader,
|
||||
offset: &mut file::Offset,
|
||||
) -> Result<usize> {
|
||||
self.deref().read_from_slice(reader, offset)
|
||||
}
|
||||
}
|
||||
|
||||
// Delegate for `Pin<Box<T, A>>`: Support a `Pin<Box<T, A>>` with an inner lock.
|
||||
impl<T: ?Sized + BinaryReader, A: Allocator> BinaryReader for Pin<Box<T, A>> {
|
||||
fn read_from_slice(
|
||||
&self,
|
||||
reader: &mut UserSliceReader,
|
||||
offset: &mut file::Offset,
|
||||
) -> Result<usize> {
|
||||
self.deref().read_from_slice(reader, offset)
|
||||
}
|
||||
}
|
||||
|
||||
// Delegate for `Arc<T>`: Support an `Arc<T>` with an inner lock.
|
||||
impl<T: ?Sized + BinaryReader> BinaryReader for Arc<T> {
|
||||
fn read_from_slice(
|
||||
&self,
|
||||
reader: &mut UserSliceReader,
|
||||
offset: &mut file::Offset,
|
||||
) -> Result<usize> {
|
||||
self.deref().read_from_slice(reader, offset)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user