Re: [PATCH v3 02/10] rust: uaccess: add UserSliceReader::read_slice_partial()
From: Alexandre Courbot
Date: Sat Nov 01 2025 - 10:17:45 EST
On Wed Oct 22, 2025 at 11:30 PM JST, 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.
>
> Reviewed-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
> Reviewed-by: Matthew Maurer <mmaurer@xxxxxxxxxx>
> Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
> ---
> rust/kernel/uaccess.rs | 16 ++++++++++++++++
> 1 file changed, 16 insertions(+)
>
> diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
> index a8fb4764185a..c1cd3a76cff8 100644
> --- a/rust/kernel/uaccess.rs
> +++ b/rust/kernel/uaccess.rs
> @@ -287,6 +287,22 @@ 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())
> + .unwrap_or(out.len())
> + .min(out.len());
IIUC you should be able to replace the `checked_add().unwrap_or()` with
`saturating_add()` and end up with the same result.
Reviewed-by: Alexandre Courbot <acourbot@xxxxxxxxxx>