Re: [PATCH 3/3] rust: add abstraction for `struct page`

From: Boqun Feng
Date: Thu Jan 25 2024 - 19:47:15 EST


On Wed, Jan 24, 2024 at 11:20:23AM +0000, Alice Ryhl wrote:
[...]
> + /// Runs a piece of code with a raw pointer to a slice of this page, with
> + /// bounds checking.
> + ///
> + /// If `f` is called, then it will be called with a pointer that points at
> + /// `off` bytes into the page, and the pointer will be valid for at least
> + /// `len` bytes. The pointer is only valid on this task, as this method uses
> + /// a local mapping.
> + ///
> + /// If `off` and `len` refers to a region outside of this page, then this
> + /// method returns `EINVAL` and does not call `f`.
> + pub fn with_pointer_into_page<T>(

Name it as `with_slice_in_page` maybe?

> + &self,
> + off: usize,
> + len: usize,
> + f: impl FnOnce(*mut u8) -> Result<T>,
> + ) -> Result<T> {
> + let bounds_ok = off <= PAGE_SIZE && len <= PAGE_SIZE && (off + len) <= PAGE_SIZE;
> +
> + if bounds_ok {
> + self.with_page_mapped(move |page_addr| {
> + // SAFETY: The `off` integer is at most `PAGE_SIZE`, so this pointer offset will
> + // result in a pointer that is in bounds or one off the end of the page.
> + f(unsafe { page_addr.cast::<u8>().add(off) })
> + })
> + } else {
> + Err(EINVAL)
> + }
> + }
> +
> + /// Maps the page and reads from it into the given buffer.
> + ///
> + /// # Safety
> + ///
> + /// Callers must ensure that `dest` is valid for writing `len` bytes.
> + pub unsafe fn read(&self, dest: *mut u8, offset: usize, len: usize) -> Result {
> + self.with_pointer_into_page(offset, len, move |from_ptr| {
> + // SAFETY: If `with_pointer_into_page` calls into this closure, then
> + // it has performed a bounds check and guarantees that `from_ptr` is
> + // valid for `len` bytes.
> + unsafe { ptr::copy(from_ptr, dest, len) };
> + Ok(())
> + })
> + }
> +
> + /// Maps the page and writes into it from the given buffer.
> + ///
> + /// # Safety
> + ///
> + /// Callers must ensure that `src` is valid for reading `len` bytes.
> + pub unsafe fn write(&self, src: *const u8, offset: usize, len: usize) -> Result {

Use a slice like type as `src` maybe? Then the function can be safe:

pub fn write<S: AsRef<[u8]>>(&self, src: S, offset: usize) -> Result

Besides, since `Page` impl `Sync`, shouldn't this `write` and the
`fill_zero` be a `&mut self` function? Or make them both `unsafe`
because of potential race and add some safety requirement?

Regards,
Boqun

> + self.with_pointer_into_page(offset, len, move |to_ptr| {
> + // SAFETY: If `with_pointer_into_page` calls into this closure, then
> + // it has performed a bounds check and guarantees that `to_ptr` is
> + // valid for `len` bytes.
> + unsafe { ptr::copy(src, to_ptr, len) };
> + Ok(())
> + })
> + }
> +
> + /// Maps the page and zeroes the given slice.
> + pub fn fill_zero(&self, offset: usize, len: usize) -> Result {
> + self.with_pointer_into_page(offset, len, move |to_ptr| {
> + // SAFETY: If `with_pointer_into_page` calls into this closure, then
> + // it has performed a bounds check and guarantees that `to_ptr` is
> + // valid for `len` bytes.
> + unsafe { ptr::write_bytes(to_ptr, 0u8, len) };
> + Ok(())
> + })
> + }
> +
> + /// Copies data from userspace into this page.
> + pub fn copy_into_page(
> + &self,
> + reader: &mut UserSlicePtrReader,
> + offset: usize,
> + len: usize,
> + ) -> Result {
> + self.with_pointer_into_page(offset, len, move |to_ptr| {
> + // SAFETY: If `with_pointer_into_page` calls into this closure, then
> + // it has performed a bounds check and guarantees that `to_ptr` is
> + // valid for `len` bytes.
> + unsafe { reader.read_raw(to_ptr, len) }
> + })
> + }
> +}
> +
> +impl Drop for Page {
> + fn drop(&mut self) {
> + // SAFETY: By the type invariants, we have ownership of the page and can
> + // free it.
> + unsafe { bindings::__free_pages(self.page.as_ptr(), 0) };
> + }
> +}
>
> --
> 2.43.0.429.g432eaa2c6b-goog
>