Add a safe method for copying data from user space to a system memory region that is represented by a `SysMem` view. The copy is byte-wise atomic, so the destination can be subject to concurrent accesses, such as the data of a page that is mapped to user space. The method builds on `UserSliceReader::read_raw()`. Signed-off-by: Andreas Hindborg --- rust/kernel/uaccess.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs index b1df79500b98..313479d80834 100644 --- a/rust/kernel/uaccess.rs +++ b/rust/kernel/uaccess.rs @@ -11,6 +11,7 @@ error::Result, ffi::{c_char, c_void}, fs::file, + io::SysMem, prelude::*, ptr::KnownSize, transmute::{AsBytes, FromBytes}, @@ -292,6 +293,37 @@ pub unsafe fn read_raw(&mut self, out: *mut u8, len: usize) -> Result { Ok(()) } + /// Reads raw data from the user slice into the system memory region `out`. + /// + /// Reads `out.len()` bytes. The writes to `out` are byte-wise atomic, so they are well-defined + /// even if the memory of `out` is concurrently accessed by hardware, by user space, or by + /// other kernel code using atomic or volatile accesses. + /// + /// Fails with [`EFAULT`] if the read happens on a bad address, or if the read goes out of + /// bounds of this [`UserSliceReader`]. This call may modify the memory of `out` even if it + /// returns an error. + /// + /// # Examples + /// + /// ``` + /// use kernel::{ + /// io::SysMem, + /// uaccess::UserSliceReader, // + /// }; + /// + /// fn fill_from_user(reader: &mut UserSliceReader, mem: SysMem<'_, [u8]>) -> Result { + /// reader.read_sysmem(mem) + /// } + /// ``` + pub fn read_sysmem(&mut self, out: SysMem<'_, [u8]>) -> Result { + let out = out.as_ptr(); + + // SAFETY: By the type invariant of `SysMem`, the memory of `out` is valid for writing + // `out.len()` bytes, and no plain access races with this write, so it is valid for atomic + // writes. + unsafe { self.read_raw(out.cast(), out.len()) } + } + /// Reads raw data from the user slice into a kernel buffer. /// /// Fails with [`EFAULT`] if the read happens on a bad address, or if the read goes out of -- 2.54.0