From: Matthew Maurer <mmaurer@google.com>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"Sami Tolvanen" <samitolvanen@google.com>,
"Timur Tabi" <ttabi@nvidia.com>,
"Benno Lossin" <lossin@kernel.org>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
Matthew Maurer <mmaurer@google.com>,
Dirk Behme <dirk.behme@de.bosch.com>
Subject: [PATCH v8 4/6] rust: debugfs: Support arbitrary owned backing for File
Date: Fri, 27 Jun 2025 23:18:27 +0000 [thread overview]
Message-ID: <20250627-debugfs-rust-v8-4-c6526e413d40@google.com> (raw)
In-Reply-To: <20250627-debugfs-rust-v8-0-c6526e413d40@google.com>
This allows `File`s to be backed by `ForeignOwnable` rather than just
`&'static T`. This means that dynamically allocated objects can be
attached to `File`s without needing to take extra steps to create a
pinned reference that's guaranteed to live long enough.
Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
Signed-off-by: Matthew Maurer <mmaurer@google.com>
---
rust/kernel/debugfs.rs | 99 +++++++++++++++++++++++++++++++------
rust/kernel/debugfs/display_file.rs | 49 +++++++++++-------
2 files changed, 115 insertions(+), 33 deletions(-)
diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs
index 1f20d85da56fcb89476552feefc9d97fab43cc04..929e55ee5629f6888edf29997b9ed77d274e11c8 100644
--- a/rust/kernel/debugfs.rs
+++ b/rust/kernel/debugfs.rs
@@ -5,11 +5,11 @@
//!
//! C header: [`include/linux/debugfs.h`](srctree/include/linux/debugfs.h)
-#[cfg(CONFIG_DEBUG_FS)]
-use crate::prelude::GFP_KERNEL;
+use crate::prelude::*;
use crate::str::CStr;
#[cfg(CONFIG_DEBUG_FS)]
use crate::sync::Arc;
+use crate::types::ForeignOwnable;
use core::fmt::Display;
#[cfg(CONFIG_DEBUG_FS)]
@@ -63,40 +63,52 @@ fn create(_name: &CStr, _parent: Option<&Dir>) -> Self {
}
#[cfg(CONFIG_DEBUG_FS)]
- fn create_file<T: Display + Sized>(&self, name: &CStr, data: &'static T) -> File {
+ fn create_file<D: ForeignOwnable + Send + Sync>(&self, name: &CStr, data: D) -> File
+ where
+ for<'a> D::Borrowed<'a>: Display,
+ {
+ let mut file = File {
+ _entry: Entry::empty(),
+ _foreign: ForeignHolder::new(data),
+ };
+
let Some(parent) = &self.0 else {
- return File {
- _entry: Entry::empty(),
- };
+ return file;
};
+
// SAFETY:
// * `name` is a NUL-terminated C string, living across the call, by `CStr` invariant.
// * `parent` is a live `dentry` since we have a reference to it.
// * `vtable` is all stock `seq_file` implementations except for `open`.
// `open`'s only requirement beyond what is provided to all open functions is that the
// inode's data pointer must point to a `T` that will outlive it, which we know because
- // we have a static reference.
+ // we have an owning `D` in the `File`, and we tear down the file during `Drop`.
let ptr = unsafe {
bindings::debugfs_create_file_full(
name.as_char_ptr(),
0o444,
parent.as_ptr(),
- data as *const _ as *mut _,
+ file._foreign.data,
core::ptr::null(),
- &<T as display_file::DisplayFile>::VTABLE,
+ &<D as display_file::DisplayFile>::VTABLE,
)
};
// SAFETY: `debugfs_create_file_full` either returns an error code or a legal
// dentry pointer, so `Entry::new` is safe to call here.
- let entry = unsafe { Entry::new(ptr, Some(parent.clone())) };
+ file._entry = unsafe { Entry::new(ptr, Some(parent.clone())) };
- File { _entry: entry }
+ file
}
#[cfg(not(CONFIG_DEBUG_FS))]
- fn create_file<T: Display + Sized>(&self, _name: &CStr, _data: &'static T) -> File {
- File {}
+ fn create_file<D: ForeignOwnable>(&self, _name: &CStr, data: D) -> File
+ where
+ for<'a> D::Borrowed<'a>: Display,
+ {
+ File {
+ _foreign: ForeignHolder::new(data),
+ }
}
/// Create a DebugFS subdirectory.
@@ -127,7 +139,21 @@ pub fn subdir(&self, name: &CStr) -> Self {
/// dir.display_file(c_str!("foo"), &200);
/// // "my_debugfs_dir/foo" now contains the number 200.
/// ```
- pub fn display_file<T: Display + Sized>(&self, name: &CStr, data: &'static T) -> File {
+ ///
+ /// ```
+ /// # use kernel::c_str;
+ /// # use kernel::debugfs::Dir;
+ /// # use kernel::prelude::*;
+ /// let val = KBox::new(300, GFP_KERNEL)?;
+ /// let dir = Dir::new(c_str!("my_debugfs_dir"));
+ /// dir.display_file(c_str!("foo"), val);
+ /// // "my_debugfs_dir/foo" now contains the number 300.
+ /// # Ok::<(), Error>(())
+ /// ```
+ pub fn display_file<D: ForeignOwnable + Send + Sync>(&self, name: &CStr, data: D) -> File
+ where
+ for<'a> D::Borrowed<'a>: Display,
+ {
self.create_file(name, data)
}
@@ -147,6 +173,51 @@ pub fn new(name: &CStr) -> Self {
/// Handle to a DebugFS file.
pub struct File {
+ // This order is load-bearing for drops - `_entry` must be dropped before `_foreign`
#[cfg(CONFIG_DEBUG_FS)]
_entry: Entry,
+ _foreign: ForeignHolder,
+}
+
+struct ForeignHolder {
+ data: *mut c_void,
+ drop_hook: unsafe fn(*mut c_void),
+}
+
+// SAFETY: We only construct `ForeignHolder` using a pointer from a `ForeignOwnable` which
+// is also `Sync`.
+unsafe impl Sync for ForeignHolder {}
+// SAFETY: We only construct `ForeignHolder` using a pointer from a `ForeignOwnable` which
+// is also `Send`.
+unsafe impl Send for ForeignHolder {}
+
+/// Helper function to drop a `D`-typed foreign ownable from its foreign representation, useful for
+/// cases where you want the type erased.
+/// # Safety
+/// * The foreign pointer passed in must have come from `D`'s `ForeignOwnable::into_foreign`
+/// * There must be no outstanding `ForeignOwnable::borrow{,mut}`
+/// * The pointer must not have been `ForeignOwnable::from_foreign`'d
+unsafe fn drop_helper<D: ForeignOwnable>(foreign: *mut c_void) {
+ // SAFETY: By safetydocs, we meet the requirements for `from_foreign`
+ drop(unsafe { D::from_foreign(foreign as _) })
+}
+
+impl ForeignHolder {
+ fn new<D: ForeignOwnable>(data: D) -> Self {
+ Self {
+ data: data.into_foreign() as _,
+ drop_hook: drop_helper::<D>,
+ }
+ }
+}
+
+impl Drop for ForeignHolder {
+ fn drop(&mut self) {
+ // SAFETY: `drop_hook` corresponds to the original `ForeignOwnable` instance's `drop`.
+ // This is only used in the case of `File`, so the only place borrows occur is through the
+ // DebugFS file owned by `_entry`. Since `_entry` occurs earlier in the struct, it will be
+ // dropped first, so no borrows will be ongoing. We know no `from_foreign` has occurred
+ // because this pointer is not exposed anywhere that is called.
+ unsafe { (self.drop_hook)(self.data) }
+ }
}
diff --git a/rust/kernel/debugfs/display_file.rs b/rust/kernel/debugfs/display_file.rs
index e4b551f7092884ad12e18a32cc243d0d037931a6..0c2dd756fa866425d1b7771beceaa2fb43bf11e5 100644
--- a/rust/kernel/debugfs/display_file.rs
+++ b/rust/kernel/debugfs/display_file.rs
@@ -4,42 +4,48 @@
use crate::prelude::*;
use crate::seq_file::SeqFile;
use crate::seq_print;
+use crate::types::ForeignOwnable;
use core::fmt::Display;
/// Implements `open` for `file_operations` via `single_open` to fill out a `seq_file`.
///
/// # Safety
///
-/// * `inode`'s private pointer must point to a value of type `T` which will outlive the `inode`
-/// and will not be mutated during this call.
+/// * `inode`'s private pointer must be the foreign representation of `D`, and no mutable borrows
+/// are outstanding.
/// * `file` must point to a live, not-yet-initialized file object.
-pub(crate) unsafe extern "C" fn display_open<T: Display>(
+pub(crate) unsafe extern "C" fn display_open<D: ForeignOwnable + Sync>(
inode: *mut bindings::inode,
file: *mut bindings::file,
-) -> c_int {
+) -> c_int
+where
+ for<'a> D::Borrowed<'a>: Display,
+{
// SAFETY:
// * `file` is acceptable by caller precondition.
// * `print_act` will be called on a `seq_file` with private data set to the third argument,
// so we meet its safety requirements.
- // * The `data` pointer passed in the third argument is a valid `T` pointer that outlives
- // this call by caller preconditions.
- unsafe { bindings::single_open(file, Some(display_act::<T>), (*inode).i_private) }
+ // * The `data` pointer passed in the third argument is valid by caller preconditions.
+ unsafe { bindings::single_open(file, Some(display_act::<D>), (*inode).i_private) }
}
/// Prints private data stashed in a seq_file to that seq file.
///
/// # Safety
///
-/// `seq` must point to a live `seq_file` whose private data is a live pointer to a `T` which is
-/// not being mutated.
-pub(crate) unsafe extern "C" fn display_act<T: Display>(
+/// `seq` must point to a live `seq_file` whose private data is the foreign representation of `D`,
+/// and no mutable borrows are outsstanding.
+pub(crate) unsafe extern "C" fn display_act<D: ForeignOwnable + Sync>(
seq: *mut bindings::seq_file,
_: *mut c_void,
-) -> c_int {
- // SAFETY: By caller precondition, this pointer is live, points to a value of type `T`, and
- // is not being mutated.
- let data = unsafe { &*((*seq).private as *mut T) };
- // SAFETY: By caller precondition, `seq_file` points to a live `seq_file`, so we can lift
+) -> c_int
+where
+ for<'a> D::Borrowed<'a>: Display,
+{
+ // SAFETY: By caller precondition, this pointer is live, has the right type, and has no mutable
+ // borrows outstanding.
+ let data = unsafe { D::borrow((*seq).private as _) };
+ // SAFETY: By caller precondition, `seq` points to a live `seq_file`, so we can lift
// it.
let seq_file = unsafe { SeqFile::from_raw(seq) };
seq_print!(seq_file, "{}", data);
@@ -47,15 +53,20 @@
}
// Work around lack of generic const items.
-pub(crate) trait DisplayFile: Display + Sized {
+pub(crate) trait DisplayFile {
+ const VTABLE: bindings::file_operations;
+}
+
+impl<D: ForeignOwnable + Sync> DisplayFile for D
+where
+ for<'a> D::Borrowed<'a>: Display,
+{
const VTABLE: bindings::file_operations = bindings::file_operations {
read: Some(bindings::seq_read),
llseek: Some(bindings::seq_lseek),
release: Some(bindings::single_release),
- open: Some(display_open::<Self>),
+ open: Some(display_open::<D>),
// SAFETY: `file_operations` supports zeroes in all fields.
..unsafe { core::mem::zeroed() }
};
}
-
-impl<T: Display + Sized> DisplayFile for T {}
--
2.50.0.727.gbf7dc18ff4-goog
next prev parent reply other threads:[~2025-06-27 23:19 UTC|newest]
Thread overview: 49+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-27 23:18 [PATCH v8 0/6] rust: DebugFS Bindings Matthew Maurer
2025-06-27 23:18 ` [PATCH v8 1/6] rust: debugfs: Bind DebugFS directory creation Matthew Maurer
2025-06-27 23:18 ` [PATCH v8 2/6] rust: debugfs: Bind file creation for long-lived Display Matthew Maurer
2025-06-27 23:18 ` [PATCH v8 3/6] rust: types: Support &'static and &'static mut ForeignOwnable Matthew Maurer
2025-07-01 11:41 ` Dirk Behme
2025-07-01 11:46 ` Danilo Krummrich
2025-06-27 23:18 ` Matthew Maurer [this message]
2025-06-30 17:29 ` [PATCH v8 4/6] rust: debugfs: Support arbitrary owned backing for File Danilo Krummrich
2025-06-30 17:34 ` Matthew Maurer
2025-06-30 17:36 ` Matthew Maurer
2025-06-30 17:39 ` Danilo Krummrich
2025-06-30 17:49 ` Matthew Maurer
2025-06-30 18:16 ` Danilo Krummrich
2025-07-01 13:58 ` Greg Kroah-Hartman
2025-07-01 14:13 ` Danilo Krummrich
2025-07-01 14:21 ` Greg Kroah-Hartman
2025-07-01 15:10 ` Danilo Krummrich
2025-07-01 18:11 ` Matthew Maurer
2025-07-01 19:21 ` Danilo Krummrich
2025-07-01 19:46 ` Benno Lossin
2025-07-01 19:58 ` Danilo Krummrich
2025-07-01 20:03 ` Benno Lossin
2025-07-01 20:09 ` Benno Lossin
2025-07-01 20:16 ` Danilo Krummrich
2025-07-01 21:53 ` Matthew Maurer
2025-07-01 22:26 ` Danilo Krummrich
2025-07-01 20:07 ` Benno Lossin
2025-07-03 10:02 ` Alice Ryhl
2025-07-03 10:33 ` Benno Lossin
2025-07-03 10:54 ` Alice Ryhl
2025-07-03 11:41 ` Greg Kroah-Hartman
2025-07-03 12:29 ` Danilo Krummrich
2025-07-03 12:50 ` Greg Kroah-Hartman
2025-07-03 14:00 ` Danilo Krummrich
2025-07-03 13:34 ` Benno Lossin
2025-07-03 14:04 ` Danilo Krummrich
2025-07-03 13:35 ` Benno Lossin
2025-07-03 13:38 ` Alice Ryhl
2025-07-03 12:34 ` Benno Lossin
2025-07-03 12:45 ` Greg Kroah-Hartman
2025-07-03 11:00 ` Danilo Krummrich
2025-06-27 23:18 ` [PATCH v8 5/6] rust: debugfs: Support format hooks Matthew Maurer
2025-06-27 23:18 ` [PATCH v8 6/6] rust: samples: Add debugfs sample Matthew Maurer
2025-07-01 14:03 ` Greg Kroah-Hartman
2025-07-01 17:24 ` Matthew Maurer
2025-07-01 17:34 ` Danilo Krummrich
2025-07-01 18:32 ` Matthew Maurer
2025-07-01 19:40 ` Danilo Krummrich
2025-07-01 10:57 ` [PATCH v8 0/6] rust: DebugFS Bindings Alice Ryhl
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250627-debugfs-rust-v8-4-c6526e413d40@google.com \
--to=mmaurer@google.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=dirk.behme@de.bosch.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=samitolvanen@google.com \
--cc=tmgross@umich.edu \
--cc=ttabi@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.