rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
To: "Alice Ryhl" <aliceryhl@google.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@samsung.com>,
	"Kees Cook" <keescook@chromium.org>,
	"Al Viro" <viro@zeniv.linux.org.uk>,
	"Andrew Morton" <akpm@linux-foundation.org>
Cc: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Arve Hjønnevåg" <arve@android.com>,
	"Todd Kjos" <tkjos@android.com>,
	"Martijn Coenen" <maco@android.com>,
	"Joel Fernandes" <joel@joelfernandes.org>,
	"Carlos Llamas" <cmllamas@google.com>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Arnd Bergmann" <arnd@arndb.de>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	rust-for-linux@vger.kernel.org,
	"Christian Brauner" <brauner@kernel.org>
Subject: Re: [PATCH v2 4/4] rust: add abstraction for `struct page`
Date: Sat, 10 Feb 2024 01:23:38 -0300	[thread overview]
Message-ID: <d35a656b-b802-4f1e-90d6-7320d61ed818@gmail.com> (raw)
In-Reply-To: <20240208-alice-mm-v2-4-d821250204a6@google.com>

On 2/8/24 12:47, Alice Ryhl wrote:
> [...]
> +    /// Maps the page and reads from it into the given buffer.
> +    ///
> +    /// This method will perform bounds checks on the page offset. If `offset ..
> +    /// offset+len` goes outside ot the page, then this call returns `EINVAL`.
> +    ///
> +    /// # Safety
> +    ///
> +    /// * Callers must ensure that `dst` is valid for writing `len` bytes.
> +    /// * Callers must ensure that this call does not race with a write to the
> +    ///   same page that overlaps with this read.

This safety section says that a call mustn't race with a page that
overlaps this read, hmmmmm.

> +    pub unsafe fn read_raw(&self, dst: *mut u8, offset: usize, len: usize) -> Result {
> +        self.with_pointer_into_page(offset, len, move |src| {
> +            // SAFETY: If `with_pointer_into_page` calls into this closure, then
> +            // it has performed a bounds check and guarantees that `src` is
> +            // valid for `len` bytes.
> +            //
> +            // There caller guarantees that there is no data race.
> +            unsafe { ptr::copy(src, dst, len) };

If `src` and `dst` overlap then wouldn't that be a bad idea? If so then
how about mentioning that callers have to ensure that `dst` does not
overlap with the page that's being read and use
`core::ptr::copy_nonoverlapping` instead, otherwise the doc comment
could mention that `dst` can overlap.

> +            Ok(())
> +        })
> +    }
> +
> +    /// Maps the page and writes into it from the given buffer.
> +    ///
> +    /// This method will perform bounds checks on the page offset. If `offset ..
> +    /// offset+len` goes outside ot the page, then this call returns `EINVAL`.
> +    ///
> +    /// # Safety
> +    ///
> +    /// * Callers must ensure that `src` is valid for reading `len` bytes.
> +    /// * Callers must ensure that this call does not race with a read or write
> +    ///   to the same page that overlaps with this write.
> +    pub unsafe fn write_raw(&self, src: *const u8, offset: usize, len: usize) -> Result {
> +        self.with_pointer_into_page(offset, len, move |dst| {
> +            // SAFETY: If `with_pointer_into_page` calls into this closure, then
> +            // it has performed a bounds check and guarantees that `dst` is
> +            // valid for `len` bytes.
> +            //
> +            // There caller guarantees that there is no data race.
> +            unsafe { ptr::copy(src, dst, len) };

Same as above

> +            Ok(())
> +        })
> +    }
> [...]

  reply	other threads:[~2024-02-10  4:23 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-08 15:47 [PATCH v2 0/4] Memory management patches needed by Rust Binder Alice Ryhl
2024-02-08 15:47 ` [PATCH v2 1/4] rust: uaccess: add userspace pointers Alice Ryhl
2024-02-08 22:54   ` Valentin Obst
2024-02-09 11:15     ` Alice Ryhl
2024-02-21 11:47   ` Alice Ryhl
2024-02-27 10:05   ` Carlos López
2024-02-27 13:12     ` Alice Ryhl
2024-02-08 15:47 ` [PATCH v2 2/4] uaccess: always export _copy_[from|to]_user with CONFIG_RUST Alice Ryhl
2024-02-08 22:56   ` Valentin Obst
2024-02-09 14:41     ` Arnd Bergmann
2024-02-09 16:45       ` Valentin Obst
2024-02-10  0:15   ` Kees Cook
2024-02-10 11:07     ` Arnd Bergmann
2024-02-14 10:51     ` Alice Ryhl
2024-02-08 15:47 ` [PATCH v2 3/4] rust: uaccess: add typed accessors for userspace pointers Alice Ryhl
2024-02-08 22:57   ` Valentin Obst
2024-02-09 10:40     ` Alice Ryhl
2024-02-09 17:18       ` Valentin Obst
2024-02-08 15:47 ` [PATCH v2 4/4] rust: add abstraction for `struct page` Alice Ryhl
2024-02-10  4:23   ` Martin Rodriguez Reboredo [this message]
2024-02-12  9:36     ` Alice Ryhl
2024-02-12 18:11       ` Martin Rodriguez Reboredo
2024-02-27  8:32   ` Andreas Hindborg
2024-02-27 15:37   ` Matthew Wilcox
2024-02-27 15:56     ` Alice Ryhl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d35a656b-b802-4f1e-90d6-7320d61ed818@gmail.com \
    --to=yakoyoku@gmail.com \
    --cc=a.hindborg@samsung.com \
    --cc=akpm@linux-foundation.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=arnd@arndb.de \
    --cc=arve@android.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=brauner@kernel.org \
    --cc=cmllamas@google.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=joel@joelfernandes.org \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=maco@android.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=surenb@google.com \
    --cc=tkjos@android.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=wedsonaf@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).