summaryrefslogtreecommitdiff
path: root/rust/kernel/transmute.rs
diff options
context:
space:
mode:
authorDave Airlie <airlied@redhat.com>2025-11-20 10:42:50 +1000
committerDave Airlie <airlied@redhat.com>2025-11-20 10:44:50 +1000
commitf0ded972d37150f9f889de75c9eecc5cb0730013 (patch)
tree1cbf2650fd192cd2b10d497ada78dc55721d842a /rust/kernel/transmute.rs
parentf3a1d69f9b388271986f4efe1fd775df15b443c1 (diff)
parent77b686f688126a5f758b51441a03186e9eb1b0f1 (diff)
Merge tag 'drm-rust-next-2025-11-18' of https://gitlab.freedesktop.org/drm/rust/kernel into drm-next
Cross-subsystem Changes: Rust - Make slice::as_flattened usable on all supported versions of rustc. - Add FromBytes::from_bytes_prefix() method. Core Changes: - Update Tyr in MAINTAINERS file. - Remove redundant device ptr from Rust GEM object. - Change how AlwaysRefCounted is implemented for GEM objects. - Add deferred vm_bo cleanup to GPUVM and use it in Panthor. Driver Changes: Nova Core - Introduction of bitfield! macro, with support for different storage sizes and custom visibility. - Introduction of safe converters between integer types for which the conversion is lossless. - GSP initialized up to fully booted state on Ampere. - Use more future-proof register for GPU identification. - Various simplifications and optimizations. Nova - Select NOVA_CORE. - Depend on CONFIG_64BIT. Signed-off-by: Dave Airlie <airlied@redhat.com> From: Alice Ryhl <aliceryhl@google.com> Link: https://patch.msgid.link/aRxtJC0D1pQUepF4@google.com
Diffstat (limited to 'rust/kernel/transmute.rs')
-rw-r--r--rust/kernel/transmute.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs
index cfc37d81adf2..be5dbf3829e2 100644
--- a/rust/kernel/transmute.rs
+++ b/rust/kernel/transmute.rs
@@ -58,6 +58,27 @@ pub unsafe trait FromBytes {
}
}
+ /// Converts the beginning of `bytes` to a reference to `Self`.
+ ///
+ /// This method is similar to [`Self::from_bytes`], with the difference that `bytes` does not
+ /// need to be the same size of `Self` - the appropriate portion is cut from the beginning of
+ /// `bytes`, and the remainder returned alongside `Self`.
+ fn from_bytes_prefix(bytes: &[u8]) -> Option<(&Self, &[u8])>
+ where
+ Self: Sized,
+ {
+ if bytes.len() < size_of::<Self>() {
+ None
+ } else {
+ // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at` cannot
+ // panic.
+ // TODO: replace with `split_at_checked` once the MSRV is >= 1.80.
+ let (prefix, remainder) = bytes.split_at(size_of::<Self>());
+
+ Self::from_bytes(prefix).map(|s| (s, remainder))
+ }
+ }
+
/// Converts a mutable slice of bytes to a reference to `Self`.
///
/// Succeeds if the reference is properly aligned, and the size of `bytes` is equal to that of
@@ -80,6 +101,27 @@ pub unsafe trait FromBytes {
}
}
+ /// Converts the beginning of `bytes` to a mutable reference to `Self`.
+ ///
+ /// This method is similar to [`Self::from_bytes_mut`], with the difference that `bytes` does
+ /// not need to be the same size of `Self` - the appropriate portion is cut from the beginning
+ /// of `bytes`, and the remainder returned alongside `Self`.
+ fn from_bytes_mut_prefix(bytes: &mut [u8]) -> Option<(&mut Self, &mut [u8])>
+ where
+ Self: AsBytes + Sized,
+ {
+ if bytes.len() < size_of::<Self>() {
+ None
+ } else {
+ // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at_mut` cannot
+ // panic.
+ // TODO: replace with `split_at_mut_checked` once the MSRV is >= 1.80.
+ let (prefix, remainder) = bytes.split_at_mut(size_of::<Self>());
+
+ Self::from_bytes_mut(prefix).map(|s| (s, remainder))
+ }
+ }
+
/// Creates an owned instance of `Self` by copying `bytes`.
///
/// Unlike [`FromBytes::from_bytes`], which requires aligned input, this method can be used on
@@ -97,6 +139,27 @@ pub unsafe trait FromBytes {
None
}
}
+
+ /// Creates an owned instance of `Self` from the beginning of `bytes`.
+ ///
+ /// This method is similar to [`Self::from_bytes_copy`], with the difference that `bytes` does
+ /// not need to be the same size of `Self` - the appropriate portion is cut from the beginning
+ /// of `bytes`, and the remainder returned alongside `Self`.
+ fn from_bytes_copy_prefix(bytes: &[u8]) -> Option<(Self, &[u8])>
+ where
+ Self: Sized,
+ {
+ if bytes.len() < size_of::<Self>() {
+ None
+ } else {
+ // PANIC: We checked that `bytes.len() >= size_of::<Self>`, thus `split_at` cannot
+ // panic.
+ // TODO: replace with `split_at_checked` once the MSRV is >= 1.80.
+ let (prefix, remainder) = bytes.split_at(size_of::<Self>());
+
+ Self::from_bytes_copy(prefix).map(|s| (s, remainder))
+ }
+ }
}
macro_rules! impl_frombytes {