[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250627-linux-miscident-v1-1-d37c870550ef@google.com>
Date: Fri, 27 Jun 2025 23:42:38 +0000
From: Matthew Maurer <mmaurer@...gle.com>
To: Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
"Björn Roy Baron" <bjorn3_gh@...tonmail.com>, Benno Lossin <lossin@...nel.org>,
Andreas Hindborg <a.hindborg@...nel.org>, Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>, Danilo Krummrich <dakr@...nel.org>,
"Isaac J. Manjarres" <isaacmanjarres@...gle.com>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
Matthew Maurer <mmaurer@...gle.com>
Subject: [PATCH] rust: miscdevice: Export vtable testing
A common pattern in the kernel is to test whether a file belongs to a
particular driver by checking its `f_op` struct against an expected
value. This provides a safe way to perform that test for `MiscDevice`
implementations without needing to directly expose the vtable.
Signed-off-by: Matthew Maurer <mmaurer@...gle.com>
---
rust/kernel/miscdevice.rs | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs
index 939278bc7b03489a647b697012e09223871c90cd..5f59eda57c38be5f0d54fa9692fe5b2819e31480 100644
--- a/rust/kernel/miscdevice.rs
+++ b/rust/kernel/miscdevice.rs
@@ -177,6 +177,14 @@ fn show_fdinfo(
}
}
+/// Determines whether a given `File` is backed by the `T` `MiscDevice` based on vtable matching.
+pub fn is_miscdevice_file<T: MiscDevice>(file: &File) -> bool {
+ let vtable = core::ptr::from_ref(&MiscdeviceVTable::<T>::VTABLE);
+ // SAFETY: `f_op` is not mutated after file creation
+ let file_vtable = unsafe { (*file.as_ptr()).f_op };
+ vtable == file_vtable
+}
+
/// A vtable for the file operations of a Rust miscdevice.
struct MiscdeviceVTable<T: MiscDevice>(PhantomData<T>);
---
base-commit: 86731a2a651e58953fc949573895f2fa6d456841
change-id: 20250627-linux-miscident-7b67db234a5c
Best regards,
--
Matthew Maurer <mmaurer@...gle.com>
Powered by blists - more mailing lists