[PATCH 01/11] rust: page: tighten safety requirements of `SysMem` copy methods
From: Andreas Hindborg
Date: Fri Nov 20 2026 - 05:29:14 EST
`Page::read_sysmem` and `Page::write_sysmem` are available for all page
kinds, so they can be called on a `Page<Exclusive>`, whose data can be
referenced as a slice. Add the requirements that the raw accessors of
`Page` have for this case: no mutable reference to the data that is read
may exist, no reference to the data that is written may exist, and data
written to a `Page<Exclusive>` must be initialized.
Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
---
rust/kernel/page.rs | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
index 4ea7368b4699..3c48ecde4d14 100644
--- a/rust/kernel/page.rs
+++ b/rust/kernel/page.rs
@@ -374,6 +374,8 @@ pub unsafe fn read_raw(&self, dst: *mut u8, offset: usize, len: usize) -> Result
/// Callers must ensure that:
///
/// - This call does not race with a write to the source page that overlaps with this read.
+ /// - No mutable reference to the data of the page that overlaps with this read exists during
+ /// this call.
/// - The memory region of `dst` does not overlap with the memory of the page.
///
/// # Examples
@@ -391,7 +393,8 @@ pub unsafe fn read_raw(&self, dst: *mut u8, offset: usize, len: usize) -> Result
/// // way while `dst` is alive.
/// let dst = unsafe { SysMem::new(&raw mut buf as *mut [u8]) };
///
- /// // SAFETY: `page` is not written to during this call, and `buf` is not part of the page.
+ /// // SAFETY: `page` is not written to during this call, no reference to the data of `page`
+ /// // exists, and `buf` is not part of the page.
/// unsafe { page.read_sysmem(dst, 0)? };
/// assert_eq!(buf, [0u8; 16]);
/// # Ok::<(), Error>(())
@@ -453,6 +456,10 @@ pub unsafe fn write_raw(&self, src: *const u8, offset: usize, len: usize) -> Res
///
/// - This call does not race with a read or write to the destination page that overlaps with
/// this write.
+ /// - No reference to the data of the page that overlaps with this write exists during this
+ /// call. Such references can be obtained from a [`Page<Exclusive>`], see
+ /// [`Page::as_slice`].
+ /// - If the page is a [`Page<Exclusive>`], the bytes of `src` are initialized.
/// - The memory region of `src` does not overlap with the memory of the page.
///
/// # Examples
@@ -476,8 +483,8 @@ pub unsafe fn write_raw(&self, src: *const u8, offset: usize, len: usize) -> Res
/// )
/// };
///
- /// // SAFETY: `page` is not accessed in any other way during these calls, and neither `data`
- /// // nor `buf` is part of the page.
+ /// // SAFETY: `page` is not accessed in any other way during these calls, no reference to the
+ /// // data of `page` exists, and neither `data` nor `buf` is part of the page.
/// unsafe {
/// page.write_sysmem(src, 0)?;
/// page.read_sysmem(dst, 0)?;
--
2.54.0