[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251105002346.53119-3-dakr@kernel.org>
Date: Wed, 5 Nov 2025 01:22:50 +0100
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,
viro@...iv.linux.org.uk,
brauner@...nel.org,
jack@...e.cz,
arnd@...db.de
Cc: rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org,
linux-fsdevel@...r.kernel.org,
Danilo Krummrich <dakr@...nel.org>
Subject: [PATCH 3/3] rust: iov: take advantage from file::Offset
Make use of file::Offset, now that we have a dedicated type for it.
Suggested-by: Alice Ryhl <aliceryhl@...gle.com>
Signed-off-by: Danilo Krummrich <dakr@...nel.org>
---
rust/kernel/iov.rs | 13 +++++++++----
samples/rust/rust_misc_device.rs | 2 +-
2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/rust/kernel/iov.rs b/rust/kernel/iov.rs
index 43bae8923c46..a3b1233441b6 100644
--- a/rust/kernel/iov.rs
+++ b/rust/kernel/iov.rs
@@ -10,6 +10,7 @@
use crate::{
alloc::{Allocator, Flags},
bindings,
+ fs::file,
prelude::*,
types::Opaque,
};
@@ -292,8 +293,12 @@ pub fn copy_to_iter(&mut self, input: &[u8]) -> usize {
/// that the file will appear to contain `contents` even if takes multiple reads to read the
/// entire file.
#[inline]
- pub fn simple_read_from_buffer(&mut self, ppos: &mut i64, contents: &[u8]) -> Result<usize> {
- if *ppos < 0 {
+ pub fn simple_read_from_buffer(
+ &mut self,
+ ppos: &mut file::Offset,
+ contents: &[u8],
+ ) -> Result<usize> {
+ if ppos.is_negative() {
return Err(EINVAL);
}
let Ok(pos) = usize::try_from(*ppos) else {
@@ -306,8 +311,8 @@ pub fn simple_read_from_buffer(&mut self, ppos: &mut i64, contents: &[u8]) -> Re
// BOUNDS: We just checked that `pos < contents.len()` above.
let num_written = self.copy_to_iter(&contents[pos..]);
- // OVERFLOW: `pos+num_written <= contents.len() <= isize::MAX <= i64::MAX`.
- *ppos = (pos + num_written) as i64;
+ // OVERFLOW: `pos+num_written <= contents.len() <= isize::MAX <= file::Offset::MAX`.
+ *ppos += num_written as isize;
Ok(num_written)
}
diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_device.rs
index 6a9f59ccf71d..edbcc761b4a2 100644
--- a/samples/rust/rust_misc_device.rs
+++ b/samples/rust/rust_misc_device.rs
@@ -187,7 +187,7 @@ fn read_iter(mut kiocb: Kiocb<'_, Self::Ptr>, iov: &mut IovIterDest<'_>) -> Resu
let inner = me.inner.lock();
// Read the buffer contents, taking the file position into account.
- let read = iov.simple_read_from_buffer(&mut kiocb.ki_pos_mut().0, &inner.buffer)?;
+ let read = iov.simple_read_from_buffer(kiocb.ki_pos_mut(), &inner.buffer)?;
Ok(read)
}
--
2.51.2
Powered by blists - more mailing lists