From: "Danilo Krummrich" <dakr@kernel.org>
To: "Alice Ryhl" <aliceryhl@google.com>
Cc: <gregkh@linuxfoundation.org>, <rafael@kernel.org>,
<ojeda@kernel.org>, <alex.gaynor@gmail.com>,
<boqun.feng@gmail.com>, <gary@garyguo.net>,
<bjorn3_gh@protonmail.com>, <lossin@kernel.org>,
<a.hindborg@kernel.org>, <tmgross@umich.edu>,
<mmaurer@google.com>, <rust-for-linux@vger.kernel.org>,
<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 1/7] rust: uaccess: add UserSliceReader::read_slice_partial()
Date: Fri, 17 Oct 2025 13:50:38 +0200 [thread overview]
Message-ID: <DDKKPM9ICZRG.2S66MFUQJ38KC@kernel.org> (raw)
In-Reply-To: <aPIkTuGpR7VX_HoD@google.com>
On Fri Oct 17, 2025 at 1:11 PM CEST, Alice Ryhl wrote:
> On Sat, Oct 04, 2025 at 12:26:38AM +0200, Danilo Krummrich wrote:
>> The existing read_slice() method is a wrapper around copy_from_user()
>> and expects the user buffer to be larger than the destination buffer.
>>
>> However, userspace may split up writes in multiple partial operations
>> providing an offset into the destination buffer and a smaller user
>> buffer.
>>
>> In order to support this common case, provide a helper for partial
>> reads.
>>
>> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
>> ---
>> rust/kernel/uaccess.rs | 13 +++++++++++++
>> 1 file changed, 13 insertions(+)
>>
>> diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
>> index a8fb4764185a..1b0b57e855c9 100644
>> --- a/rust/kernel/uaccess.rs
>> +++ b/rust/kernel/uaccess.rs
>> @@ -287,6 +287,19 @@ pub fn read_slice(&mut self, out: &mut [u8]) -> Result {
>> self.read_raw(out)
>> }
>>
>> + /// Reads raw data from the user slice into a kernel buffer partially.
>> + ///
>> + /// This is the same as [`Self::read_slice`] but considers the given `offset` into `out` and
>> + /// truncates the read to the boundaries of `self` and `out`.
>> + ///
>> + /// On success, returns the number of bytes read.
>> + pub fn read_slice_partial(&mut self, out: &mut [u8], offset: usize) -> Result<usize> {
>> + let end = offset.checked_add(self.len()).ok_or(EINVAL)?.min(out.len());
>
> Should this be?
> let end = offset.checked_add(self.len()).unwrap_or(out.len()).min(out.len());
Yes, that seems reasonable.
>> + out.get_mut(offset..end)
>> + .map_or(Ok(0), |dst| self.read_slice(dst).map(|()| dst.len()))
>
> So if out.len() < offset, then we return Ok(0)?
Yes, because it tells userspace that there are no more bytes left to read.
next prev parent reply other threads:[~2025-10-17 11:50 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-03 22:26 [PATCH 0/7] Binary Large Objects for Rust DebugFS Danilo Krummrich
2025-10-03 22:26 ` [PATCH 1/7] rust: uaccess: add UserSliceReader::read_slice_partial() Danilo Krummrich
2025-10-17 11:11 ` Alice Ryhl
2025-10-17 11:50 ` Danilo Krummrich [this message]
2025-10-03 22:26 ` [PATCH 2/7] rust: uaccess: add UserSliceWriter::write_slice_partial() Danilo Krummrich
2025-10-03 22:26 ` [PATCH 3/7] rust: debugfs: support for binary large objects Danilo Krummrich
2025-10-17 12:59 ` Alice Ryhl
2025-10-17 14:37 ` Danilo Krummrich
2025-10-17 14:53 ` Danilo Krummrich
2025-10-19 9:44 ` Alice Ryhl
2025-10-19 12:01 ` Danilo Krummrich
2025-10-20 8:12 ` Alice Ryhl
2025-10-20 9:40 ` Danilo Krummrich
2025-10-20 9:42 ` Alice Ryhl
2025-10-20 9:49 ` Danilo Krummrich
2025-10-19 9:50 ` Alice Ryhl
2025-10-19 11:24 ` Danilo Krummrich
2025-10-20 8:13 ` Alice Ryhl
2025-10-20 9:35 ` Danilo Krummrich
2025-10-20 9:39 ` Alice Ryhl
2025-10-20 9:41 ` Danilo Krummrich
2025-10-03 22:26 ` [PATCH 4/7] rust: debugfs: support blobs from smart pointers Danilo Krummrich
2025-10-03 23:12 ` Matthew Maurer
2025-10-03 23:26 ` Danilo Krummrich
2025-10-03 23:36 ` Matthew Maurer
2025-10-03 22:26 ` [PATCH 5/7] samples: rust: debugfs: add example for blobs Danilo Krummrich
2025-10-20 10:01 ` Alice Ryhl
2025-10-03 22:26 ` [PATCH 6/7] rust: debugfs: support binary large objects for ScopedDir Danilo Krummrich
2025-10-17 13:00 ` Alice Ryhl
2025-10-17 14:55 ` Danilo Krummrich
2025-10-03 22:26 ` [PATCH 7/7] samples: rust: debugfs_scoped: add example for blobs Danilo Krummrich
2025-10-20 10:02 ` 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=DDKKPM9ICZRG.2S66MFUQJ38KC@kernel.org \
--to=dakr@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=mmaurer@google.com \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/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).