[PATCH rust-next v2 2/3] rust: miscdevice: add llseek support

Ryosuke Yasuoka
2025-10-15T04:03:30Z

Add the ability to write a file_operations->llseek hook in Rust when
using the miscdevice abstraction.

Signed-off-by: Ryosuke Yasuoka <ryasuoka@redhat.com>
---
rust/kernel/miscdevice.rs | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)

diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs
index d698cddcb4a5..2efd0847cde1 100644
--- a/rust/kernel/miscdevice.rs
+++ b/rust/kernel/miscdevice.rs
@@ -126,6 +126,16 @@ fn release(device: Self::Ptr, _file: &File) {
drop(device);
}

+ /// Handler for llseek.
+ fn llseek(
+ _device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>,
+ _file: &mut 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
@@ -296,6 +306,27 @@ impl<T: MiscDevice> MiscdeviceVTable<T> {
}
}

+ /// # 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_mut(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>`.
@@ -391,6 +422,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 },
read_iter: if T::HAS_READ_ITER {
Some(Self::read_iter)
--
2.51.0