rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Matthew Maurer <mmaurer@google.com>
Cc: "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>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Sami Tolvanen" <samitolvanen@google.com>,
	"Timur Tabi" <ttabi@nvidia.com>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v2 4/4] rust: samples: Add debugfs sample
Date: Thu, 1 May 2025 09:40:10 +0200	[thread overview]
Message-ID: <2025050137-ongoing-such-46f6@gregkh> (raw)
In-Reply-To: <20250430-debugfs-rust-v2-4-2e8d3985812b@google.com>

On Wed, Apr 30, 2025 at 11:31:59PM +0000, Matthew Maurer wrote:
> Provides an example of using the Rust DebugFS bindings.
> 
> Signed-off-by: Matthew Maurer <mmaurer@google.com>

Much nicer, many thanks for this!

Some minor comments on the sample code here.  As someone coming from C
with limited Rust experience, I think I do understand it, but I think it
could use a bunch of comments to make it more "obvious" what is
happening, see below.

> +static EXAMPLE: AtomicU32 = AtomicU32::new(8);

Wait, why is this set to 8 and then you automatically set it to 10 after
you create the file?  No one is ever going to see 8 as a valid value
unless they really race to read the file, right?

> +impl kernel::Module for RustDebugFs {
> +    fn init(_this: &'static ThisModule) -> Result<Self> {
> +        let debugfs = Dir::new(c_str!("sample_debugfs"));
> +        debugfs
> +            .fmt_file(c_str!("example"), &EXAMPLE, &|example, f| {
> +                writeln!(f, "Reading atomic: {}", example.load(Ordering::Relaxed))
> +            })
> +            .keep();
> +        EXAMPLE.store(10, Ordering::Relaxed);
> +        Ok(Self { _debugfs: debugfs })
> +    }
> +}


How about this rewrite with comments added to help make things more
obvious:

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 single file in the directory called "example" that
        // allows to read from the EXAMPLE atomic variable, and make
        // sure it lives past the scope of this function by calling
        // .keep() on it.
        debugfs
            .fmt_file(c_str!("example"), &EXAMPLE, &|example, f| {
                writeln!(f, "Reading atomic: {}", example.load(Ordering::Relaxed))
            })
            .keep();

        // Change the value of EXAMPLE to be 10 so that will be the
        // value read from the file.  Note, the original value 8 will be
        // read if the file is read right before this is called.
        EXAMPLE.store(10, Ordering::Relaxed);

        // Create our module object and save off the pointer to the
        // debugfs directory we created.  It will be automatically
        // removed when the module is unloaded by virtue of the
        // reference count to the structure being dropped at that point
        // in time.
        Ok(Self { _debugfs: debugfs })
    }
}

thanks,

greg k-h

  reply	other threads:[~2025-05-01  7:40 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-30 23:31 [PATCH v2 0/4] rust: DebugFS Bindings Matthew Maurer
2025-04-30 23:31 ` [PATCH v2 1/4] rust: debugfs: Bind DebugFS directory creation Matthew Maurer
2025-05-01 10:00   ` Miguel Ojeda
2025-05-01 10:16   ` Danilo Krummrich
2025-05-01 16:02     ` Matthew Maurer
2025-05-01 16:35       ` Danilo Krummrich
2025-04-30 23:31 ` [PATCH v2 2/4] rust: debugfs: Bind file creation for long-lived Display Matthew Maurer
2025-05-01 10:37   ` Danilo Krummrich
2025-05-01 16:09     ` Matthew Maurer
2025-05-01 17:32       ` Danilo Krummrich
2025-04-30 23:31 ` [PATCH v2 3/4] rust: debugfs: Support format hooks Matthew Maurer
2025-05-01 10:00   ` Miguel Ojeda
2025-04-30 23:31 ` [PATCH v2 4/4] rust: samples: Add debugfs sample Matthew Maurer
2025-05-01  7:40   ` Greg Kroah-Hartman [this message]
2025-05-01 16:44     ` Timur Tabi

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=2025050137-ongoing-such-46f6@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mmaurer@google.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).