[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250818135846.133722-2-ryasuoka@redhat.com>
Date: Mon, 18 Aug 2025 22:58:38 +0900
From: Ryosuke Yasuoka <ryasuoka@...hat.com>
To: arnd@...db.de,
gregkh@...uxfoundation.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,
dakr@...nel.org,
lee@...nel.org
Cc: Ryosuke Yasuoka <ryasuoka@...hat.com>,
rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: [PATCH rust-next 1/2] rust: miscdevice: add llseek support
Add the ability to write a file_operations->llseek hook in Rust when
using the miscdevice abstraction.
Signed-off-by: Ryosuke Yasuoka <ryasuoka@...hat.com>
---
rust/kernel/miscdevice.rs | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs
index 6373fe183b27..597e7b66e493 100644
--- a/rust/kernel/miscdevice.rs
+++ b/rust/kernel/miscdevice.rs
@@ -125,6 +125,16 @@ fn release(device: Self::Ptr, _file: &File) {
drop(device);
}
+ /// Handler for llseek.
+ fn llseek(
+ _device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>,
+ _file: &File,
+ _offset: i64,
+ _whence: i32,
+ ) -> Result<isize> {
+ build_error!(VTABLE_DEFAULT_ERROR)
+ }
+
/// Handle for mmap.
///
/// This function is invoked when a user space process invokes the `mmap` system call on
@@ -245,6 +255,27 @@ impl<T: MiscDevice> MiscdeviceVTable<T> {
0
}
+ /// # Safety
+ ///
+ /// `file` must be a valid file that is associated with a `MiscDeviceRegistration<T>`.
+ unsafe extern "C" fn llseek(file: *mut bindings::file, offset: i64, whence: c_int) -> i64 {
+ // SAFETY: The llseek call of a file can access the private data.
+ let private = unsafe { (*file).private_data };
+ // SAFETY: This is a Rust Miscdevice, so we call `into_foreign` in `open` and
+ // `from_foreign` in `release`, and `fops_llseek` is guaranteed to be called between those
+ // two operations.
+ let device = unsafe { <T::Ptr as ForeignOwnable>::borrow(private) };
+ // SAFETY:
+ // * The file is valid for the duration of this call.
+ // * There is no active fdget_pos region on the file on this thread.
+ let file = unsafe { File::from_raw_file(file) };
+
+ match T::llseek(device, file, offset, whence) {
+ Ok(res) => res as i64,
+ Err(err) => i64::from(err.to_errno()),
+ }
+ }
+
/// # Safety
///
/// `file` must be a valid file that is associated with a `MiscDeviceRegistration<T>`.
@@ -340,6 +371,11 @@ impl<T: MiscDevice> MiscdeviceVTable<T> {
const VTABLE: bindings::file_operations = bindings::file_operations {
open: Some(Self::open),
release: Some(Self::release),
+ llseek: if T::HAS_LLSEEK {
+ Some(Self::llseek)
+ } else {
+ None
+ },
mmap: if T::HAS_MMAP { Some(Self::mmap) } else { None },
unlocked_ioctl: if T::HAS_IOCTL {
Some(Self::ioctl)
--
2.50.1
Powered by blists - more mailing lists