[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20260101-this_module_fix-v1-5-46ae3e5605a0@gmail.com>
Date: Thu, 01 Jan 2026 07:20:49 +0200
From: Kari Argillander <kari.argillander@...il.com>
To: Miguel Ojeda <ojeda@...nel.org>, 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>, Alexandre Courbot <acourbot@...dia.com>
Cc: Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
linux-modules@...r.kernel.org, Luis Chamberlain <mcgrof@...nel.org>,
Petr Pavlu <petr.pavlu@...e.com>, Daniel Gomez <da.gomez@...nel.org>,
Sami Tolvanen <samitolvanen@...gle.com>, Aaron Tomlin <atomlin@...mlin.com>,
Jens Axboe <axboe@...nel.dk>, Kari Argillander <kari.argillander@...il.com>,
Andreas Hindborg <a.hindborg@...nel.org>
Subject: [PATCH RFC 5/6] rust: debugfs: WIP: Use owner for file_operations
---
rust/kernel/debugfs.rs | 79 ++++++++++++++++++++++-------------------
rust/kernel/debugfs/file_ops.rs | 50 +++++++++++++++++++-------
samples/rust/rust_debugfs.rs | 6 ++--
3 files changed, 83 insertions(+), 52 deletions(-)
diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs
index facad81e8290..03e22b1cfa2b 100644
--- a/rust/kernel/debugfs.rs
+++ b/rust/kernel/debugfs.rs
@@ -6,7 +6,8 @@
//! C header: [`include/linux/debugfs.h`](srctree/include/linux/debugfs.h)
// When DebugFS is disabled, many parameters are dead. Linting for this isn't helpful.
-#![cfg_attr(not(CONFIG_DEBUG_FS), allow(unused_variables))]
+// #![cfg_attr(not(CONFIG_DEBUG_FS), allow(unused_variables))]
+#![allow(unused_variables)]
use crate::fmt;
use crate::prelude::*;
@@ -46,27 +47,31 @@
// able to refer to us. In this case, we need to silently fail. All future child directories/files
// will silently fail as well.
#[derive(Clone)]
-pub struct Dir(#[cfg(CONFIG_DEBUG_FS)] Option<Arc<Entry<'static>>>);
+pub struct Dir<M: ThisModule>(
+ #[cfg(CONFIG_DEBUG_FS)] Option<Arc<Entry<'static>>>,
+ PhantomData<M>,
+);
-impl Dir {
+impl<M: ThisModule> Dir<M> {
/// Create a new directory in DebugFS. If `parent` is [`None`], it will be created at the root.
- fn create(name: &CStr, parent: Option<&Dir>) -> Self {
+ fn create(name: &CStr, parent: Option<&Self>) -> Self {
#[cfg(CONFIG_DEBUG_FS)]
{
let parent_entry = match parent {
// If the parent couldn't be allocated, just early-return
- Some(Dir(None)) => return Self(None),
- Some(Dir(Some(entry))) => Some(entry.clone()),
+ Some(Self(None, _)) => return Self(None, PhantomData),
+ Some(Self(Some(entry), _)) => Some(entry.clone()),
None => None,
};
Self(
// If Arc creation fails, the `Entry` will be dropped, so the directory will be
// cleaned up.
Arc::new(Entry::dynamic_dir(name, parent_entry), GFP_KERNEL).ok(),
+ PhantomData,
)
}
#[cfg(not(CONFIG_DEBUG_FS))]
- Self()
+ Self(PhantomData)
}
/// Creates a DebugFS file which will own the data produced by the initializer provided in
@@ -107,7 +112,7 @@ fn create_file<'a, T, E: 'a>(
/// let debugfs = Dir::new(c_str!("parent"));
/// ```
pub fn new(name: &CStr) -> Self {
- Dir::create(name, None)
+ Self::create(name, None)
}
/// Creates a subdirectory within this directory.
@@ -121,7 +126,7 @@ pub fn new(name: &CStr) -> Self {
/// let child = parent.subdir(c_str!("child"));
/// ```
pub fn subdir(&self, name: &CStr) -> Self {
- Dir::create(name, Some(self))
+ Self::create(name, Some(self))
}
/// Creates a read-only file in this directory.
@@ -149,7 +154,7 @@ pub fn read_only_file<'a, T, E: 'a>(
where
T: Writer + Send + Sync + 'static,
{
- let file_ops = &<T as ReadFile<_>>::FILE_OPS;
+ let file_ops = &<T as ReadFile<M, _>>::FILE_OPS;
self.create_file(name, data, file_ops)
}
@@ -176,7 +181,7 @@ pub fn read_binary_file<'a, T, E: 'a>(
where
T: BinaryWriter + Send + Sync + 'static,
{
- self.create_file(name, data, &T::FILE_OPS)
+ self.create_file(name, data, &<T as BinaryReadFile<M, _>>::FILE_OPS)
}
/// Creates a read-only file in this directory, with contents from a callback.
@@ -215,7 +220,7 @@ pub fn read_callback_file<'a, T, E: 'a, F>(
T: Send + Sync + 'static,
F: Fn(&T, &mut fmt::Formatter<'_>) -> fmt::Result + Send + Sync,
{
- let file_ops = <FormatAdapter<T, F>>::FILE_OPS.adapt();
+ let file_ops = <FormatAdapter<T, F> as ReadFile<M, FormatAdapter<T, F>>>::FILE_OPS.adapt();
self.create_file(name, data, file_ops)
}
@@ -231,7 +236,7 @@ pub fn read_write_file<'a, T, E: 'a>(
where
T: Writer + Reader + Send + Sync + 'static,
{
- let file_ops = &<T as ReadWriteFile<_>>::FILE_OPS;
+ let file_ops = &<T as ReadWriteFile<M, _>>::FILE_OPS;
self.create_file(name, data, file_ops)
}
@@ -247,7 +252,7 @@ pub fn read_write_binary_file<'a, T, E: 'a>(
where
T: BinaryWriter + BinaryReader + Send + Sync + 'static,
{
- let file_ops = &<T as BinaryReadWriteFile<_>>::FILE_OPS;
+ let file_ops = &<T as BinaryReadWriteFile<M, _>>::FILE_OPS;
self.create_file(name, data, file_ops)
}
@@ -270,7 +275,7 @@ pub fn read_write_callback_file<'a, T, E: 'a, F, W>(
W: Fn(&T, &mut UserSliceReader) -> Result + Send + Sync,
{
let file_ops =
- <WritableAdapter<FormatAdapter<T, F>, W> as file_ops::ReadWriteFile<_>>::FILE_OPS
+ <WritableAdapter<FormatAdapter<T, F>, W> as file_ops::ReadWriteFile<M, _>>::FILE_OPS
.adapt()
.adapt();
self.create_file(name, data, file_ops)
@@ -290,7 +295,7 @@ pub fn write_only_file<'a, T, E: 'a>(
where
T: Reader + Send + Sync + 'static,
{
- self.create_file(name, data, &T::FILE_OPS)
+ self.create_file(name, data, &<T as WriteFile<M, _>>::FILE_OPS)
}
/// Creates a write-only binary file in this directory.
@@ -307,7 +312,7 @@ pub fn write_binary_file<'a, T, E: 'a>(
where
T: BinaryReader + Send + Sync + 'static,
{
- self.create_file(name, data, &T::FILE_OPS)
+ self.create_file(name, data, &<T as BinaryWriteFile<M, _>>::FILE_OPS)
}
/// Creates a write-only file in this directory, with write logic from a callback.
@@ -324,7 +329,7 @@ pub fn write_callback_file<'a, T, E: 'a, W>(
T: Send + Sync + 'static,
W: Fn(&T, &mut UserSliceReader) -> Result + Send + Sync,
{
- let file_ops = <WritableAdapter<NoWriter<T>, W> as WriteFile<_>>::FILE_OPS
+ let file_ops = <WritableAdapter<NoWriter<T>, W> as WriteFile<M, _>>::FILE_OPS
.adapt()
.adapt();
self.create_file(name, data, file_ops)
@@ -527,7 +532,7 @@ fn create_file<T: Sync>(&self, name: &CStr, data: &'data T, vtable: &'static Fil
/// file is removed when the [`Scope`] that this directory belongs
/// to is dropped.
pub fn read_only_file<T: Writer + Send + Sync + 'static>(&self, name: &CStr, data: &'data T) {
- self.create_file(name, data, &T::FILE_OPS)
+ // self.create_file(name, data, &<T as ReadFile<M, T>>::FILE_OPS)
}
/// Creates a read-only binary file in this directory.
@@ -541,7 +546,7 @@ pub fn read_binary_file<T: BinaryWriter + Send + Sync + 'static>(
name: &CStr,
data: &'data T,
) {
- self.create_file(name, data, &T::FILE_OPS)
+ // self.create_file(name, data, &<T as ReadFile<M, T>>::FILE_OPS)
}
/// Creates a read-only file in this directory, with contents from a callback.
@@ -560,8 +565,8 @@ pub fn read_callback_file<T, F>(&self, name: &CStr, data: &'data T, _f: &'static
T: Send + Sync + 'static,
F: Fn(&T, &mut fmt::Formatter<'_>) -> fmt::Result + Send + Sync,
{
- let vtable = <FormatAdapter<T, F> as ReadFile<_>>::FILE_OPS.adapt();
- self.create_file(name, data, vtable)
+ // let vtable = <FormatAdapter<T, F> as ReadFile<M, _>>::FILE_OPS.adapt();
+ // self.create_file(name, data, vtable)
}
/// Creates a read-write file in this directory.
@@ -577,8 +582,8 @@ pub fn read_write_file<T: Writer + Reader + Send + Sync + 'static>(
name: &CStr,
data: &'data T,
) {
- let vtable = &<T as ReadWriteFile<_>>::FILE_OPS;
- self.create_file(name, data, vtable)
+ // let vtable = &<T as ReadWriteFile<_>>::FILE_OPS;
+ // self.create_file(name, data, vtable)
}
/// Creates a read-write binary file in this directory.
@@ -593,8 +598,8 @@ pub fn read_write_binary_file<T: BinaryWriter + BinaryReader + Send + Sync + 'st
name: &CStr,
data: &'data T,
) {
- let vtable = &<T as BinaryReadWriteFile<_>>::FILE_OPS;
- self.create_file(name, data, vtable)
+ // let vtable = &<T as BinaryReadWriteFile<_>>::FILE_OPS;
+ // self.create_file(name, data, vtable)
}
/// Creates a read-write file in this directory, with logic from callbacks.
@@ -618,10 +623,10 @@ pub fn read_write_callback_file<T, F, W>(
F: Fn(&T, &mut fmt::Formatter<'_>) -> fmt::Result + Send + Sync,
W: Fn(&T, &mut UserSliceReader) -> Result + Send + Sync,
{
- let vtable = <WritableAdapter<FormatAdapter<T, F>, W> as ReadWriteFile<_>>::FILE_OPS
- .adapt()
- .adapt();
- self.create_file(name, data, vtable)
+ // let vtable = <WritableAdapter<FormatAdapter<T, F>, W> as ReadWriteFile<_>>::FILE_OPS
+ // .adapt()
+ // .adapt();
+ // self.create_file(name, data, vtable)
}
/// Creates a write-only file in this directory.
@@ -632,8 +637,8 @@ pub fn read_write_callback_file<T, F, W>(
/// file is removed when the [`Scope`] that this directory belongs
/// to is dropped.
pub fn write_only_file<T: Reader + Send + Sync + 'static>(&self, name: &CStr, data: &'data T) {
- let vtable = &<T as WriteFile<_>>::FILE_OPS;
- self.create_file(name, data, vtable)
+ // let vtable = &<T as WriteFile<_>>::FILE_OPS;
+ // self.create_file(name, data, vtable)
}
/// Creates a write-only binary file in this directory.
@@ -647,7 +652,7 @@ pub fn write_binary_file<T: BinaryReader + Send + Sync + 'static>(
name: &CStr,
data: &'data T,
) {
- self.create_file(name, data, &T::FILE_OPS)
+ // self.create_file(name, data, &<T as ReadFile<M, T>>::FILE_OPS)
}
/// Creates a write-only file in this directory, with write logic from a callback.
@@ -665,10 +670,10 @@ pub fn write_only_callback_file<T, W>(&self, name: &CStr, data: &'data T, _w: &'
T: Send + Sync + 'static,
W: Fn(&T, &mut UserSliceReader) -> Result + Send + Sync,
{
- let vtable = &<WritableAdapter<NoWriter<T>, W> as WriteFile<_>>::FILE_OPS
- .adapt()
- .adapt();
- self.create_file(name, data, vtable)
+ // let vtable = &<WritableAdapter<NoWriter<T>, W> as WriteFile<_>>::FILE_OPS
+ // .adapt()
+ // .adapt();
+ // self.create_file(name, data, vtable)
}
fn empty() -> Self {
diff --git a/rust/kernel/debugfs/file_ops.rs b/rust/kernel/debugfs/file_ops.rs
index 8a0442d6dd7a..0e5059c044af 100644
--- a/rust/kernel/debugfs/file_ops.rs
+++ b/rust/kernel/debugfs/file_ops.rs
@@ -115,13 +115,14 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
}
// Work around lack of generic const items.
-pub(crate) trait ReadFile<T> {
+pub(crate) trait ReadFile<M, T> {
const FILE_OPS: FileOps<T>;
}
-impl<T: Writer + Sync> ReadFile<T> for T {
+impl<M: ThisModule, T: Writer + Sync> ReadFile<M, T> for T {
const FILE_OPS: FileOps<T> = {
let operations = bindings::file_operations {
+ owner: M::OWNER.as_ptr(),
read: Some(bindings::seq_read),
llseek: Some(bindings::seq_lseek),
release: Some(bindings::single_release),
@@ -167,13 +168,18 @@ fn read<T: Reader + Sync>(data: &T, buf: *const c_char, count: usize) -> isize {
}
// A trait to get the file operations for a type.
-pub(crate) trait ReadWriteFile<T> {
+pub(crate) trait ReadWriteFile<M, T> {
const FILE_OPS: FileOps<T>;
}
-impl<T: Writer + Reader + Sync> ReadWriteFile<T> for T {
+impl<M, T> ReadWriteFile<M, T> for T
+where
+ M: ThisModule,
+ T: Writer + Reader + Sync,
+{
const FILE_OPS: FileOps<T> = {
let operations = bindings::file_operations {
+ owner: M::OWNER.as_ptr(),
open: Some(writer_open::<T>),
read: Some(bindings::seq_read),
write: Some(write::<T>),
@@ -225,13 +231,18 @@ impl<T: Writer + Reader + Sync> ReadWriteFile<T> for T {
read(data, buf, count)
}
-pub(crate) trait WriteFile<T> {
+pub(crate) trait WriteFile<M, T> {
const FILE_OPS: FileOps<T>;
}
-impl<T: Reader + Sync> WriteFile<T> for T {
+impl<M, T> WriteFile<M, T> for T
+where
+ M: ThisModule,
+ T: Reader + Sync,
+{
const FILE_OPS: FileOps<T> = {
let operations = bindings::file_operations {
+ owner: M::OWNER.as_ptr(),
open: Some(write_only_open),
write: Some(write_only_write::<T>),
llseek: Some(bindings::noop_llseek),
@@ -278,13 +289,18 @@ extern "C" fn blob_read<T: BinaryWriter>(
}
/// Representation of [`FileOps`] for read only binary files.
-pub(crate) trait BinaryReadFile<T> {
+pub(crate) trait BinaryReadFile<M, T> {
const FILE_OPS: FileOps<T>;
}
-impl<T: BinaryWriter + Sync> BinaryReadFile<T> for T {
+impl<M, T> BinaryReadFile<M, T> for T
+where
+ M: ThisModule,
+ T: BinaryWriter + Sync,
+{
const FILE_OPS: FileOps<T> = {
let operations = bindings::file_operations {
+ owner: M::OWNER.as_ptr(),
read: Some(blob_read::<T>),
llseek: Some(bindings::default_llseek),
open: Some(bindings::simple_open),
@@ -333,13 +349,18 @@ extern "C" fn blob_write<T: BinaryReader>(
}
/// Representation of [`FileOps`] for write only binary files.
-pub(crate) trait BinaryWriteFile<T> {
+pub(crate) trait BinaryWriteFile<M, T> {
const FILE_OPS: FileOps<T>;
}
-impl<T: BinaryReader + Sync> BinaryWriteFile<T> for T {
+impl<M, T> BinaryWriteFile<M, T> for T
+where
+ M: ThisModule,
+ T: BinaryReader + Sync,
+{
const FILE_OPS: FileOps<T> = {
let operations = bindings::file_operations {
+ owner: M::OWNER.as_ptr(),
write: Some(blob_write::<T>),
llseek: Some(bindings::default_llseek),
open: Some(bindings::simple_open),
@@ -358,13 +379,18 @@ impl<T: BinaryReader + Sync> BinaryWriteFile<T> for T {
}
/// Representation of [`FileOps`] for read/write binary files.
-pub(crate) trait BinaryReadWriteFile<T> {
+pub(crate) trait BinaryReadWriteFile<M, T> {
const FILE_OPS: FileOps<T>;
}
-impl<T: BinaryWriter + BinaryReader + Sync> BinaryReadWriteFile<T> for T {
+impl<M, T> BinaryReadWriteFile<M, T> for T
+where
+ M: ThisModule,
+ T: BinaryWriter + BinaryReader + Sync,
+{
const FILE_OPS: FileOps<T> = {
let operations = bindings::file_operations {
+ owner: M::OWNER.as_ptr(),
read: Some(blob_read::<T>),
write: Some(blob_write::<T>),
llseek: Some(bindings::default_llseek),
diff --git a/samples/rust/rust_debugfs.rs b/samples/rust/rust_debugfs.rs
index 025e8f9d12de..85c3f93159fd 100644
--- a/samples/rust/rust_debugfs.rs
+++ b/samples/rust/rust_debugfs.rs
@@ -54,7 +54,7 @@ struct RustDebugFs {
pdev: ARef<platform::Device>,
// As we only hold these for drop effect (to remove the directory/files) we have a leading
// underscore to indicate to the compiler that we don't expect to use this field directly.
- _debugfs: Dir,
+ _debugfs: Dir<THIS_MODULE>,
#[pin]
_compatible: File<CString>,
#[pin]
@@ -124,11 +124,11 @@ fn probe(
}
impl RustDebugFs {
- fn build_counter(dir: &Dir) -> impl PinInit<File<Atomic<usize>>> + '_ {
+ fn build_counter<M: ThisModule>(dir: &Dir<M>) -> impl PinInit<File<Atomic<usize>>> + '_ {
dir.read_write_file(c_str!("counter"), Atomic::<usize>::new(0))
}
- fn build_inner(dir: &Dir) -> impl PinInit<File<Mutex<Inner>>> + '_ {
+ fn build_inner<M: ThisModule>(dir: &Dir<M>) -> impl PinInit<File<Mutex<Inner>>> + '_ {
dir.read_write_file(c_str!("pair"), new_mutex!(Inner { x: 3, y: 10 }))
}
--
2.43.0
Powered by blists - more mailing lists