[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251020222722.240473-3-dakr@kernel.org>
Date: Tue, 21 Oct 2025 00:26:14 +0200
From: Danilo Krummrich <dakr@...nel.org>
To: gregkh@...uxfoundation.org,
rafael@...nel.org,
ojeda@...nel.org,
alex.gaynor@...il.com,
boqun.feng@...il.com,
gary@...yguo.net,
bjorn3_gh@...tonmail.com,
lossin@...nel.org,
a.hindborg@...nel.org,
aliceryhl@...gle.com,
tmgross@...ch.edu,
mmaurer@...gle.com
Cc: rust-for-linux@...r.kernel.org,
linux-fsdevel@...r.kernel.org,
linux-kernel@...r.kernel.org,
Danilo Krummrich <dakr@...nel.org>
Subject: [PATCH v2 2/8] rust: uaccess: add UserSliceReader::read_slice_partial()
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@...nel.org>
---
rust/kernel/uaccess.rs | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
index a8fb4764185a..2061a7e10c65 100644
--- a/rust/kernel/uaccess.rs
+++ b/rust/kernel/uaccess.rs
@@ -9,6 +9,7 @@
bindings,
error::Result,
ffi::{c_char, c_void},
+ fs::file,
prelude::*,
transmute::{AsBytes, FromBytes},
};
@@ -287,6 +288,30 @@ 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: file::Offset) -> Result<usize> {
+ if offset < 0 {
+ return Err(EINVAL);
+ }
+
+ let Ok(offset) = usize::try_from(offset) else {
+ return Ok(0);
+ };
+
+ let end = offset
+ .checked_add(self.len())
+ .unwrap_or(out.len())
+ .min(out.len());
+
+ out.get_mut(offset..end)
+ .map_or(Ok(0), |dst| self.read_slice(dst).map(|()| dst.len()))
+ }
+
/// Reads a value of the specified type.
///
/// Fails with [`EFAULT`] if the read happens on a bad address, or if the read goes out of
--
2.51.0
Powered by blists - more mailing lists