From: Wedson Almeida Filho <wedsonaf@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Asahi Lina" <lina@asahilina.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>,
"Will Deacon" <will@kernel.org>,
"Robin Murphy" <robin.murphy@arm.com>,
"Joerg Roedel" <joro@8bytes.org>,
"Hector Martin" <marcan@marcan.st>,
"Sven Peter" <sven@svenpeter.dev>,
"Arnd Bergmann" <arnd@arndb.de>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"Alyssa Rosenzweig" <alyssa@rosenzweig.io>,
"Neal Gompa" <neal@gompa.dev>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
asahi@lists.linux.dev
Subject: Re: [PATCH 5/5] rust: device: Add a stub abstraction for devices
Date: Thu, 9 Mar 2023 13:46:39 -0300 [thread overview]
Message-ID: <CANeycqrtjKMfpae_DUp-VrDZugJVO7mcbBvUBB+zAc=E6aU4tw@mail.gmail.com> (raw)
In-Reply-To: <ZAnB/DozWsir1cIE@kroah.com>
On Thu, 9 Mar 2023 at 08:24, Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
>
> On Sun, Mar 05, 2023 at 03:39:25AM -0300, Wedson Almeida Filho wrote:
> > > > + /// Returns the name of the device.
> > > > + fn name(&self) -> &CStr {
> > > > + let ptr = self.raw_device();
> > > > +
> > > > + // SAFETY: `ptr` is valid because `self` keeps it alive.
> > > > + let name = unsafe { bindings::dev_name(ptr) };
> > > > +
> > > > + // SAFETY: The name of the device remains valid while it is alive (because the device is
> > > > + // never renamed, per the safety requirement of this trait). This is guaranteed to be the
> > > > + // case because the reference to `self` outlives the one of the returned `CStr` (enforced
> > > > + // by the compiler because of their lifetimes).
> > > > + unsafe { CStr::from_char_ptr(name) }
> > >
> > > Why can the device never be renamed? Devices are renamed all the time,
> > > sometimes when you least expect it (i.e. by userspace). So how is this
> > > considered "safe"? and actually correct?
> > >
> > > Again, maybe seeing a real user of this might make more sense, but
> > > as-is, this feels wrong and not needed at all.
> >
> > This requirement is to allow callers to use the string without having
> > to make a copy of it.
> >
> > If subsystems/buses are not following what the C documentation says,
> > as you point out in another thread, we have a several options: (a)
> > remove access to names altogether, (b) leave things as they are, then
> > those subsystems wouldn't be able to honour the safety requirements of
> > this trait therefore they wouldn't implement it, (c) make a copy of
> > the string, etc.
>
> How about we fix the documentation in the .c code and also drop this as
> you really don't need it now.
>
> Want to send a patch for the driver core code fix?
Sure, will do.
> > > > + // owns a reference. This is satisfied by the call to `get_device` above.
> > > > + Self { ptr }
> > > > + }
> > > > +
> > > > + /// Creates a new device instance from an existing [`RawDevice`] instance.
> > > > + pub fn from_dev(dev: &dyn RawDevice) -> Self {
> > >
> > > I am a rust newbie, but I don't understand this "RawDevice" here at all.
> >
> > Different buses will have their own Rust "Device" type, for example,
> > pci::Device, amba::Device, platform::Device that wrap their C
> > counterparts pci_dev, amba_device, platform_device.
> >
> > "RawDevice" is a trait for functionality that is common to all
> > devices. It exposes the "struct device" of each bus/subsystem so that
> > functions that work on any "struct device", for example, `clk_get`,
> > `pr_info`. will automatically work on all subsystems.
>
> Why is this being called "Raw" then? Why not just "Device" to follow
> along with the naming scheme that the kernel already uses?
Because it gives us access to underlying raw `struct device` pointer,
in Rust raw pointers are those unsafe `*mut T` or `*const T`. I'm not
married to the name though, we should probably look for a better one
if this one is confusing.
Just "Device" is already taken. It's a ref-counted `struct device` (it
calls get_device/put_device in the right places automatically,
guarantees no dandling pointers); it is meant to be used by code that
needs to hold on to devices when they don't care about the bus. (It in
fact implements `RawDevice`.)
How about `IsDevice`?
Then, for example, the platform bus would implement `IsDevice` for
`plaform::Device`.
> > For example, as part writing Rust abstractions for a platform devices,
> > we have a platform::Device type, which is wrapper around `struct
> > platform_device`. It has a bunch of associated functions that do
> > things that are specific to the platform bus. But then they also
> > implement the `RawDevice` trait (by implementing `raw_device` that
> > returns &pdev->dev), which allows drivers to call `clk_get` and the
> > printing functions directly.
> >
> > Let's say `pdev` is a platform device; if we wanted to call `clk_get`
> > in C, we'd do something like:
> >
> > clk = clk_get(&pdev->dev, NULL);
> >
> > In Rust, we'd do:
> >
> > clk = pdev.clk_get(None);
> >
> > (Note that we didn't have to know that pdev had a field whose type is
> > a `struct device` that we could use to call clk_get on; `RawDevice`
> > encoded this information.)
> >
> > Does the intent of the abstraction make sense to you now?
>
> A bit more, yes. But I want to see some real users before agreeing that
> it is sane :)
Fair enough.
Cheers,
-Wedson
next prev parent reply other threads:[~2023-03-09 16:55 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-24 10:53 [PATCH 0/5] rust: Add io_pgtable and RTKit abstractions Asahi Lina
2023-02-24 10:53 ` [PATCH 1/5] rust: Add a Sealed trait Asahi Lina
2023-02-24 10:53 ` [PATCH 2/5] rust: device: Add a minimal RawDevice trait Asahi Lina
2023-02-24 11:23 ` Greg Kroah-Hartman
2023-02-24 13:15 ` Asahi Lina
2023-02-24 14:11 ` Greg Kroah-Hartman
2023-02-24 14:19 ` Greg Kroah-Hartman
2023-02-24 14:44 ` Asahi Lina
2023-02-24 15:25 ` Greg Kroah-Hartman
2023-02-24 15:45 ` Asahi Lina
2023-02-25 17:07 ` alyssa
2023-02-24 14:32 ` Robin Murphy
2023-02-24 14:48 ` Asahi Lina
2023-02-24 15:14 ` Robin Murphy
2023-02-24 16:23 ` Asahi Lina
2023-02-24 19:22 ` Miguel Ojeda
2023-03-05 6:52 ` Wedson Almeida Filho
2023-02-24 15:32 ` Greg Kroah-Hartman
2023-02-24 18:52 ` Robin Murphy
2023-02-25 7:00 ` Greg Kroah-Hartman
2023-02-24 14:43 ` Asahi Lina
2023-02-24 15:24 ` Greg Kroah-Hartman
2023-02-24 15:42 ` Asahi Lina
2023-02-24 10:53 ` [PATCH 3/5] rust: io_pgtable: Add io_pgtable abstraction Asahi Lina
2023-02-24 10:53 ` [PATCH 4/5] rust: soc: apple: rtkit: Add Apple RTKit abstraction Asahi Lina
2023-02-24 10:53 ` [PATCH 5/5] rust: device: Add a stub abstraction for devices Asahi Lina
2023-02-24 11:19 ` Greg Kroah-Hartman
2023-02-24 15:10 ` Asahi Lina
2023-02-24 15:34 ` Greg Kroah-Hartman
2023-02-24 15:51 ` Asahi Lina
2023-02-24 16:31 ` Greg Kroah-Hartman
2023-02-24 16:53 ` Asahi Lina
2023-03-05 6:39 ` Wedson Almeida Filho
2023-03-09 11:24 ` Greg Kroah-Hartman
2023-03-09 16:46 ` Wedson Almeida Filho [this message]
2023-03-09 17:11 ` Greg Kroah-Hartman
2023-03-09 19:06 ` Wedson Almeida Filho
2023-03-13 17:01 ` Gary Guo
2023-03-09 19:43 ` Miguel Ojeda
2023-03-13 17:52 ` Gary Guo
2023-03-13 18:05 ` Boqun Feng
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='CANeycqrtjKMfpae_DUp-VrDZugJVO7mcbBvUBB+zAc=E6aU4tw@mail.gmail.com' \
--to=wedsonaf@gmail.com \
--cc=alex.gaynor@gmail.com \
--cc=alyssa@rosenzweig.io \
--cc=arnd@arndb.de \
--cc=asahi@lists.linux.dev \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=joro@8bytes.org \
--cc=lina@asahilina.net \
--cc=linux-kernel@vger.kernel.org \
--cc=marcan@marcan.st \
--cc=neal@gompa.dev \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=robin.murphy@arm.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=sven@svenpeter.dev \
--cc=will@kernel.org \
/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).