From: Danilo Krummrich <dakr@kernel.org>
To: Lyude Paul <lyude@redhat.com>
Cc: rust-for-linux@vger.kernel.org,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Maíra Canal" <mairacanal@riseup.net>,
"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>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Mika Westerberg" <mika.westerberg@linux.intel.com>,
"Xiangfei Ding" <dingxiangfei2009@gmail.com>,
"open list" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v2] rust/kernel: Add faux device bindings
Date: Fri, 7 Feb 2025 19:17:20 +0100 [thread overview]
Message-ID: <Z6ZOMD__u4VbH8ME@pollux> (raw)
In-Reply-To: <20250207004049.178049-1-lyude@redhat.com>
On Thu, Feb 06, 2025 at 07:40:45PM -0500, Lyude Paul wrote:
>
> +/// The registration of a faux device.
> +///
> +/// This type represents the registration of a [`struct faux_device`]. When an instance of this type
> +/// is dropped, its respective faux device will be unregistered from the system.
> +///
> +/// # Invariants
> +///
> +/// `self.0` always holds a valid pointer to an initialized and registered [`struct faux_device`].
> +///
> +/// [`struct faux_device`]: srctree/include/linux/device/faux.h
> +#[repr(transparent)]
> +pub struct Registration(NonNull<bindings::faux_device>);
> +
> +impl Registration {
> + /// Create and register a new faux device with the given name.
> + pub fn new(name: &CStr) -> Result<Self> {
> + // SAFETY:
> + // - `name` is copied by this function into its own storage
> + // - `faux_ops` is safe to leave NULL according to the C API
> + let dev = unsafe { bindings::faux_device_create(name.as_char_ptr(), null()) };
> +
> + // The above function will return either a valid device, or NULL on failure
> + Ok(Self(NonNull::new(dev).ok_or(ENOMEM)?))
Since the type has an "Invariants" section, you need to add "INVARIANT:" to this
comment.
> + }
...
> diff --git a/samples/rust/rust_driver_faux.rs b/samples/rust/rust_driver_faux.rs
> new file mode 100644
> index 0000000000000..81ec465d52651
> --- /dev/null
> +++ b/samples/rust/rust_driver_faux.rs
> @@ -0,0 +1,29 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Rust faux device sample.
> +
> +use kernel::{c_str, faux::*, prelude::*, Module};
> +
> +module! {
> + type: SampleModule,
> + name: "rust_faux_driver",
> + author: "Lyude Paul",
> + description: "Rust faux device sample",
> + license: "GPL",
> +}
> +
> +struct SampleModule {
> + _dev: Registration,
Probably forgot to change this to "_reg" or similar.
Also, maybe faux::Registration here and below, personally I tend to find it a
bit more obvious. But of course totally up to you.
> +}
> +
> +impl Module for SampleModule {
> + fn init(_module: &'static ThisModule) -> Result<Self> {
> + pr_info!("Initialising Rust Faux Device Sample\n");
> +
> + let dev = Registration::new(c_str!("rust-faux-sample-device"))?;
> +
> + dev_info!(dev.as_ref(), "Hello from faux device!");
> +
> + Ok(Self { _dev: dev })
> + }
> +}
prev parent reply other threads:[~2025-02-07 18:17 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-07 0:40 [PATCH v2] rust/kernel: Add faux device bindings Lyude Paul
2025-02-07 9:25 ` Greg Kroah-Hartman
2025-02-07 9:32 ` Alice Ryhl
2025-02-07 15:22 ` Greg Kroah-Hartman
2025-02-07 12:17 ` Danilo Krummrich
2025-02-07 15:15 ` Greg Kroah-Hartman
2025-02-07 11:42 ` Miguel Ojeda
2025-02-07 12:16 ` Greg Kroah-Hartman
2025-02-07 22:10 ` Lyude Paul
2025-02-09 11:10 ` Miguel Ojeda
2025-02-09 15:43 ` Greg Kroah-Hartman
2025-02-09 23:04 ` Benno Lossin
2025-02-07 22:29 ` Lyude Paul
2025-02-09 11:11 ` Miguel Ojeda
2025-02-07 18:17 ` Danilo Krummrich [this message]
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=Z6ZOMD__u4VbH8ME@pollux \
--to=dakr@kernel.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=dingxiangfei2009@gmail.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lyude@redhat.com \
--cc=mairacanal@riseup.net \
--cc=mika.westerberg@linux.intel.com \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=wedsonaf@gmail.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