From: Boqun Feng <boqun.feng@gmail.com>
To: Andrew Lunn <andrew@lunn.ch>
Cc: FUJITA Tomonori <fujita.tomonori@gmail.com>,
rust-for-linux@vger.kernel.org
Subject: Re: [RFC PATCH v1 1/4] rust: core abstractions for network PHY drivers
Date: Wed, 13 Sep 2023 14:32:48 -0700 [thread overview]
Message-ID: <ZQIqgEHPQCfVfjDd@boqun-archlinux> (raw)
In-Reply-To: <0a986803-e02d-4f7a-8938-fab89af6a655@lunn.ch>
On Wed, Sep 13, 2023 at 11:05:31PM +0200, Andrew Lunn wrote:
> > > > +impl Device {
> > > > + /// Creates a new [`Device`] instance from a raw pointer.
> > > > + ///
> > > > + /// # Safety
> > > > + ///
> > > > + /// For the duration of the lifetime 'a, the pointer must be valid for writing and nobody else
> > > > + /// may read or write to the `phy_device` object.
> > >
> > > Well that is untrue. phylib passes phydev to the MAC driver, in its
> >
> > Note that a "# Safety" section before an unsafe function is the "safety
> > requirement", i.e. the things that callers must obey the requirement in
> > order to call these unsafe functions.
>
> So by lifetime, it means while it is inside this function?
>
No ;-) Let's take a look at the function:
pub unsafe fn from_raw<'a>(ptr: *mut bindings::phy_device) -> &'a mut Self {
unsafe { &mut *(ptr as *mut Self) }
}
"'a" is a lifetime notation in Rust, so usually we call it
lifetime 'a
now, you can see the function returns a "&'a mut Self", (Self is Device
here), so it's: a mutable reference to Device whose lifetime is 'a,
until now 'a is still a lifetime variable. When the function is called,
you can think as a concrete lifetime is bound to that variable, for
example:
{
// create a mutable reference whose lifetime is '1
// '1 is a concrete lifetime starts at here.
let dev: &'1 mut Device = unsafe { Device::from_raw(..) };
// '1 continues here
<use dev as a mutable reference to Device>
// '1 continues here
} // "dev" is out of scope, so '1 ends here.
in above case, '1 is bound to 'a. A concrete lifetime can usually be
thought of as a region in the code.
Back to the safety requirement, it basically says the caller should
guarantee that as long as the return mutable reference is alive, the
underlying object (phy_device in memory) should be exclusively
accessable. For example, in the above case, it's the caller's
responsiblitly to guarantee "dev" is always a valid reference (during
lifetime '1).
Others may explain better than me, but the above is the conceptual model
that I use, hope it helps.
> > > > + /// Executes software reset the PHY via BMCR_RESET bit.
> > > > + pub fn soft_reset(&mut self) -> Result {
> > >
> > > I suggest you keep to the genphy_ naming if possible, to make it clear
> > > this is using a generic PHY method.
> > >
> >
> > My opinion may not matter, but this function is actually
> >
> > net::phy::Device::soft_reset()
> >
> > , so it could be a step backwards if we use a prefix in a language that
> > support namespace ;-) But I'll leave it the people who will read the
> > code day-to-day.
>
> So a bit of background. IEEE 802.3 defines how a well behaved PHY
> should work. We have a number of helper functions based on that
> specification, all starting with the prefix genphy_. We expect PHY
> drivers to use these helpers where possible. It helps reviewing driver
> submissions, in that if we see genphy_foo we know it is good quality
> core code. If it is not genphy_foo, we need to review it closely.
>
> When reviewing a Rust PHY driver, i want the same hint. This is a
> wrapper around a trusted genphy_foo C function, so i don't need to
> look too closely at it.
>
Understood ;-)
> > Agreed, these functions have no users. Also the wrappers doesn't reflect
> > the lock requirements, at least not in comments, after a quick look, I
> > suppose all the functions should be under phy_device->lock?
>
> The core will take that locking before calling into the driver. So the
> driver never should need to touch phydev->lock. It is one of those
> rules of thumb:
>
> Driver writers often don't understand locking, so try to
> ensure the core does all the locking, not the driver.
Make sense. I brought that up because I noticed that Fujita used all
"&mut self" (i.e. mutable references) for all the functions, and mutable
references mean exclusive accesses, which can usually be thought as
"must be called with locked held" (if the underlying object is avaiable
for multipl threads).
Regards,
Boqun
>
> Andrew
next prev parent reply other threads:[~2023-09-13 21:32 UTC|newest]
Thread overview: 79+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-13 13:36 [RFC PATCH v1 0/4] Rust abstractions for network PHY drivers FUJITA Tomonori
2023-09-13 13:36 ` [RFC PATCH v1 1/4] rust: core " FUJITA Tomonori
2023-09-13 20:11 ` Andrew Lunn
2023-09-13 20:49 ` Boqun Feng
2023-09-13 21:05 ` Andrew Lunn
2023-09-13 21:32 ` Boqun Feng [this message]
2023-09-14 4:10 ` Trevor Gross
2023-09-13 22:14 ` Miguel Ojeda
2023-09-14 0:30 ` Andrew Lunn
2023-09-14 11:03 ` Miguel Ojeda
2023-09-14 12:24 ` Andrew Lunn
2023-09-17 9:44 ` FUJITA Tomonori
2023-09-17 10:17 ` FUJITA Tomonori
2023-09-17 15:06 ` Andrew Lunn
2023-09-17 18:42 ` Trevor Gross
2023-09-17 19:08 ` Andrew Lunn
2023-09-18 0:49 ` Trevor Gross
2023-09-18 1:18 ` Andrew Lunn
2023-09-18 2:22 ` Trevor Gross
2023-09-18 3:44 ` FUJITA Tomonori
2023-09-18 13:13 ` Andrew Lunn
2023-09-18 6:01 ` FUJITA Tomonori
2023-09-14 5:47 ` Trevor Gross
2023-09-14 10:17 ` Jarkko Sakkinen
2023-09-14 19:46 ` Trevor Gross
2023-09-14 12:39 ` Andrew Lunn
2023-09-14 19:42 ` Trevor Gross
2023-09-14 19:53 ` Trevor Gross
2023-09-18 9:56 ` Finn Behrens
2023-09-18 13:22 ` Andrew Lunn
2023-09-18 10:22 ` Benno Lossin
2023-09-18 13:09 ` FUJITA Tomonori
2023-09-18 15:20 ` Benno Lossin
2023-09-19 10:26 ` FUJITA Tomonori
2023-09-20 13:24 ` Benno Lossin
2023-09-13 13:36 ` [RFC PATCH v1 2/4] rust: phy: add module device table support FUJITA Tomonori
2023-09-14 6:26 ` Trevor Gross
2023-09-14 7:23 ` Trevor Gross
2023-09-17 6:30 ` FUJITA Tomonori
2023-09-17 15:13 ` Andrew Lunn
2023-09-13 13:36 ` [RFC PATCH v1 3/4] MAINTAINERS: add Rust PHY abstractions file to the ETHERNET PHY LIBRARY FUJITA Tomonori
2023-09-13 18:57 ` Andrew Lunn
2023-09-17 12:32 ` FUJITA Tomonori
2023-09-19 12:06 ` Miguel Ojeda
2023-09-19 16:33 ` Andrew Lunn
2023-09-22 23:17 ` Trevor Gross
2023-09-23 0:05 ` Miguel Ojeda
2023-09-23 1:36 ` Andrew Lunn
2023-09-23 10:19 ` Miguel Ojeda
2023-09-23 14:42 ` Andrew Lunn
2023-10-09 12:26 ` Miguel Ojeda
2023-09-13 13:36 ` [RFC PATCH v1 4/4] sample: rust: add Asix PHY driver FUJITA Tomonori
2023-09-13 14:11 ` Andrew Lunn
2023-09-13 16:53 ` Greg KH
2023-09-13 18:50 ` Andrew Lunn
2023-09-13 18:59 ` Greg KH
2023-09-13 20:20 ` Andrew Lunn
2023-09-13 20:38 ` Greg KH
2023-09-13 16:56 ` Greg KH
2023-09-13 18:43 ` Andrew Lunn
2023-09-13 19:15 ` Andrew Lunn
2023-09-17 11:28 ` FUJITA Tomonori
2023-09-17 15:46 ` Andrew Lunn
2023-09-18 8:35 ` FUJITA Tomonori
2023-09-18 13:37 ` Andrew Lunn
2023-09-24 9:12 ` FUJITA Tomonori
2023-09-24 9:59 ` Miguel Ojeda
2023-09-24 15:31 ` Andrew Lunn
2023-09-24 17:31 ` Miguel Ojeda
2023-09-24 17:44 ` Andrew Lunn
2023-09-24 18:44 ` Miguel Ojeda
2023-09-18 22:23 ` [RFC PATCH v1 0/4] Rust abstractions for network PHY drivers Trevor Gross
2023-09-18 22:48 ` Andrew Lunn
2023-09-18 23:46 ` Trevor Gross
2023-09-19 6:24 ` FUJITA Tomonori
2023-09-19 7:41 ` Trevor Gross
2023-09-19 16:12 ` Andrew Lunn
2023-09-19 6:16 ` FUJITA Tomonori
2023-09-19 8:05 ` Trevor Gross
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=ZQIqgEHPQCfVfjDd@boqun-archlinux \
--to=boqun.feng@gmail.com \
--cc=andrew@lunn.ch \
--cc=fujita.tomonori@gmail.com \
--cc=rust-for-linux@vger.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).