From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Lyude Paul" <lyude@redhat.com>,
rust-for-linux@vger.kernel.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>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"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 16:22:40 +0100 [thread overview]
Message-ID: <2025020706-procurer-snowfield-b4ea@gregkh> (raw)
In-Reply-To: <CAH5fLgixWd862P_mma0BObtT=gm9fTzQOZot62aet1qx4mai+g@mail.gmail.com>
On Fri, Feb 07, 2025 at 10:32:16AM +0100, Alice Ryhl wrote:
> On Fri, Feb 7, 2025 at 10:25 AM Greg Kroah-Hartman
> <gregkh@linuxfoundation.org> wrote:
> >
> > On Thu, Feb 06, 2025 at 07:40:45PM -0500, Lyude Paul wrote:
> > > This introduces a crate for working with faux devices in rust, along with
> > > adding sample code to show how the API is used. Unlike other types of
> > > devices, we don't provide any hooks for device probe/removal - since these
> > > are optional for the faux API and are unnecessary in rust.
> > >
> > > Signed-off-by: Lyude Paul <lyude@redhat.com>
> > > Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > > Cc: Maíra Canal <mairacanal@riseup.net>
>
> > > +impl AsRef<device::Device> for Registration {
> > > + fn as_ref(&self) -> &device::Device {
> > > + // SAFETY: The underlying `device` in `faux_device` is guaranteed by the C API to be
> > > + // a valid initialized `device`.
> > > + unsafe { device::Device::as_ref(addr_of_mut!((*self.as_raw()).dev)) }
> >
> > Just curious, this is returning an incremented "struct device" to the
> > caller, right? And then when it goes out of scope it will have the
> > reference decremented? And do you need a wrapper in C to get to ".dev"
> > of the faux_device structure or are you ok doing it like this?
>
> This uses the &_ pointer type which does not involve any refcount
> increments or decrements. What you describe only happens if the
> ARef<_> pointer type is used instead.
>
> Safety is ensured by the borrow checker that fails compilation if the
> returned reference is used after the Registration object is destroyed
> - i.e. it's assumed that the value is safe to access without refcount
> increments as long as the Registration is still alive.
Thanks for the explanation, my rust-foo is very very very basic, so
much appreciated.
greg k-h
next prev parent reply other threads:[~2025-02-07 15:22 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 [this message]
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
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=2025020706-procurer-snowfield-b4ea@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=dingxiangfei2009@gmail.com \
--cc=gary@garyguo.net \
--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