[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <D9VPA1M45WBK.1TB4KOUXD24BD@kernel.org>
Date: Wed, 14 May 2025 09:20:49 +0200
From: "Benno Lossin" <lossin@...nel.org>
To: "Matthew Maurer" <mmaurer@...gle.com>, "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"
<benno.lossin@...ton.me>, "Andreas Hindborg" <a.hindborg@...nel.org>,
"Alice Ryhl" <aliceryhl@...gle.com>, "Trevor Gross" <tmgross@...ch.edu>,
"Danilo Krummrich" <dakr@...nel.org>, "Greg Kroah-Hartman"
<gregkh@...uxfoundation.org>, "Rafael J. Wysocki" <rafael@...nel.org>,
"Sami Tolvanen" <samitolvanen@...gle.com>, "Timur Tabi" <ttabi@...dia.com>
Cc: <linux-kernel@...r.kernel.org>, <rust-for-linux@...r.kernel.org>
Subject: Re: [PATCH v5 4/4] rust: samples: Add debugfs sample
On Tue May 6, 2025 at 1:51 AM CEST, Matthew Maurer wrote:
> +impl kernel::Module for RustDebugFs {
> + fn init(_this: &'static ThisModule) -> Result<Self> {
> + // Create a debugfs directory in the root of the filesystem called "sample_debugfs".
> + let debugfs = Dir::new(c_str!("sample_debugfs"));
> +
> + {
> + // Create a subdirectory, so "sample_debugfs/subdir" now exists.
> + // We wrap it in `ManuallyDrop` so that the subdirectory is not automatically discarded
> + // at the end of the scope - it will be cleaned up when `debugfs` is.
> + let sub = ManuallyDrop::new(debugfs.subdir(c_str!("subdir")));
I dislike the direct usage of `ManuallyDrop`. To me the usage of
`ManuallyDrop` signifies that one has to opt out of `Drop` without the
support of the wrapped type. But in this case, `Dir` is sometimes
intended to not be dropped, so I'd rather have a `.keep()` function I
saw mentioned somewhere.
> +
> + // Create a single file in the subdirectory called "example" that will read from the
> + // `EXAMPLE` atomic variable.
> + // We `forget` the result to avoid deleting the file at the end of the scope.
> + forget(sub.fmt_file(c_str!("example"), &EXAMPLE, &|example, f| {
> + writeln!(f, "Reading atomic: {}", example.load(Ordering::Relaxed))
> + }));
Similarly here, I'd rather have a `.keep()` function than people start
using `forget` all over the place.
---
Cheers,
Benno
> + // Now, "sample_debugfs/subdir/example" will print "Reading atomic: 8\n" when read.
> + }
> +
> + // Change the value in the variable displayed by the file. This is intended to demonstrate
> + // that the module can continue to change the value used by the file.
> + EXAMPLE.store(10, Ordering::Relaxed);
> + // Now, "sample_debugfs/subdir/example" will print "Reading atomic: 10\n" when read.
> +
> + // Save the root debugfs directory we created to our module object. It will be
> + // automatically cleaned up when our module is unloaded because dropping the module object
> + // will drop the `Dir` handle. The base directory, the subdirectory, and the file will all
> + // continue to exist until the module is unloaded.
> + Ok(Self { _debugfs: debugfs })
> + }
> +}
Powered by blists - more mailing lists