[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <ZSeckzvOTyre3SVM@boqun-archlinux>
Date: Thu, 12 Oct 2023 00:13:23 -0700
From: Boqun Feng <boqun.feng@...il.com>
To: FUJITA Tomonori <fujita.tomonori@...il.com>
Cc: netdev@...r.kernel.org, rust-for-linux@...r.kernel.org, andrew@...n.ch,
miguel.ojeda.sandonis@...il.com, greg@...ah.com, tmgross@...ch.edu
Subject: Re: [PATCH net-next v3 1/3] rust: core abstractions for network PHY
drivers
On Thu, Oct 12, 2023 at 04:02:46PM +0900, FUJITA Tomonori wrote:
> On Thu, 12 Oct 2023 15:44:44 +0900 (JST)
> FUJITA Tomonori <fujita.tomonori@...il.com> wrote:
>
> > On Wed, 11 Oct 2023 23:34:18 -0700
> > Boqun Feng <boqun.feng@...il.com> wrote:
> >
> >> On Thu, Oct 12, 2023 at 02:58:24PM +0900, FUJITA Tomonori wrote:
> >>> On Wed, 11 Oct 2023 11:29:45 -0700
> >>> Boqun Feng <boqun.feng@...il.com> wrote:
> >>>
> >>> > On Mon, Oct 09, 2023 at 10:39:10AM +0900, FUJITA Tomonori 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.
> >>> >> + pub unsafe fn from_raw<'a>(ptr: *mut bindings::phy_device) -> &'a mut Self {
> >>> >> + unsafe { &mut *ptr.cast() }
> >>> >> + }
> >>> >> +
> >>> >> + /// Gets the id of the PHY.
> >>> >> + pub fn phy_id(&mut self) -> u32 {
> >>> >
> >>> > This function doesn't modify the `self`, why does this need to be a
> >>> > `&mut self` function? Ditto for a few functions in this impl block.
> >>> >
> >>> > It seems you used `&mut self` for all the functions, which looks like
> >>> > more design work is required here.
> >>>
> >>> Ah, I can drop all the mut here.
> >>
> >> It may not be that easy... IIUC, most of the functions in the `impl`
> >> block can only be called correctly with phydev->lock held. In other
> >> words, their usage requires exclusive accesses. We should somehow
> >> express this in the type system, otherwise someone may lose track on
> >> this requirement in the future (for example, calling any function
> >> without the lock held).
> >>
> >> A simple type trick comes to me is that
> >>
> >> impl Device {
> >> // rename `from_raw` into `assume_locked`
> >> pub unsafe fn assume_locked<'a>(ptr: *mut bindings::phy_device) -> &'a LockedDevice {
> >> ...
> >> }
> >> }
> >
> > Hmm, the concept of PHYLIB is that a driver never play with a
> > lock. From the perspective of PHYLIB, this abstraction is a PHY
> > driver. The abstraction should not touch the lock.
> >
> > How can someone lose track on this requirement? The abstraction
> > creates a Device instance only inside the callbacks.
>
> Note `pub` isn't necessary here. I removed it.
>
> No chance to misuse a Device instance as explained above, but if we
> need to express the exclusive here, better to keep `mut`?
>
If `Device::from_raw`'s safety requirement is "only called in callbacks
with phydevice->lock held, etc.", then the exclusive access is
guaranteed by the safety requirement, therefore `mut` can be drop. It's
a matter of the exact semantics of the APIs.
Regards,
Boqun
Powered by blists - more mailing lists