rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Benno Lossin <benno.lossin@proton.me>
Cc: FUJITA Tomonori <fujita.tomonori@gmail.com>,
	tmgross@umich.edu, netdev@vger.kernel.org,
	rust-for-linux@vger.kernel.org, andrew@lunn.ch,
	miguel.ojeda.sandonis@gmail.com, greg@kroah.com
Subject: Re: [PATCH net-next v3 1/3] rust: core abstractions for network PHY drivers
Date: Thu, 12 Oct 2023 21:17:14 -0700	[thread overview]
Message-ID: <ZSjEyn-YNJiXPT4I@Boquns-Mac-mini.home> (raw)
In-Reply-To: <f3fa33f8-f4b0-463c-8ba3-5f0a8b8f6788@proton.me>

On Thu, Oct 12, 2023 at 09:10:41AM +0000, Benno Lossin wrote:
> On 12.10.23 09:58, FUJITA Tomonori wrote:
> > On Thu, 12 Oct 2023 03:32:44 -0400
> > Trevor Gross <tmgross@umich.edu> wrote:
> > 
> >> On Thu, Oct 12, 2023 at 3:13 AM Boqun Feng <boqun.feng@gmail.com> wrote:
> >>
> >>> 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
> >>
> >> That is correct to my understanding, the core handles
> >> locking/unlocking and no driver functions are called if the core
> >> doesn't hold an exclusive lock first. Which also means the wrapper
> >> type can't be `Sync`.
> >>
> >> Andrew said a bit about it in the second comment here:
> >> https://lore.kernel.org/rust-for-linux/ec6d8479-f893-4a3f-bf3e-aa0c81c4adad@lunn.ch/
> > 
> > resume/suspend are called without the mutex hold but we don't need the
> > details. PHYLIB guarantees the exclusive access inside the
> > callbacks. I updated the comment and drop mut in Device's methods.
> 
> The details about this stuff are _extremely_ important, if there is a
> mistake with the way `unsafe` requirements are written/interpreted, then
> the Rust guarantee of "memory safety in safe code" flies out the window.
> The whole idea is to offload all the dangerous stuff into smaller regions
> that can be scrutinized more easily and for that we need all of the details.
> 

Thank Benno for calling this out.

After re-read my email exchange with Tomo, I realised I need to explain
this a little bit. The minimal requirement of a Rust binding is
soundness: it means if one only uses safe APIs, one cannot introduce
memory/type safety issue (i.e. cannot have an object in an invalid
state), this is a tall task, because you can have zero assumption of the
API users, you can only encode the usage requirement in the type system.

Of course the type system doesn't always work, hence we have unsafe API,
but still the soundness of Rust bindings means using safe APIs +
*correctly* using unsafe APIs cannot introduce memory/type safety
issues.

Tomo, this is why we gave you a hard time here ;-) Unsafe Rust APIs must
be very clear on the correct usage and safe Rust APIs must not assume
how users would call it. Hope this help explain a little bit, we are not
poking random things here, soundness is the team effort from everyone
;-)

Regards,
Boqun

> What would be really helpful for me, as I have extremely limited
> knowledge of the C side, would be an explaining comment in the phy
> abstractions that explains the way the C side phy abstractions work. So
> for example it would say that locking is handled by the phy core and (at
> the moment) neither the Rust abstractions nor the driver code needs to
> concern itself with locking. There you could also write that `resume` and
> `suspend` are called without the mutex being held. We don't really have a
> precedent for this (as there have been no drivers merged), but it would be
> really helpful for me. If this exists in some other documentation, feel
> free to just link that.
> 
> -- 
> Cheers,
> Benno
> 
> 

  reply	other threads:[~2023-10-13  4:17 UTC|newest]

Thread overview: 91+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-09  1:39 [PATCH net-next v3 0/3] Rust abstractions for network PHY drivers FUJITA Tomonori
2023-10-09  1:39 ` [PATCH net-next v3 1/3] rust: core " FUJITA Tomonori
2023-10-09  3:17   ` Trevor Gross
2023-10-09 12:19   ` Benno Lossin
2023-10-09 13:02     ` Andrew Lunn
2023-10-09 13:56       ` Benno Lossin
2023-10-09 14:13         ` Andrew Lunn
2023-10-11 14:16     ` FUJITA Tomonori
2023-10-09 12:59   ` Miguel Ojeda
2023-10-09 13:49     ` FUJITA Tomonori
2023-10-09 14:32       ` Miguel Ojeda
2023-10-09 15:15         ` FUJITA Tomonori
2023-10-09 15:19           ` Miguel Ojeda
2023-10-09 15:11       ` Greg KH
2023-10-09 15:24         ` FUJITA Tomonori
2023-10-09 15:39           ` Miguel Ojeda
2023-10-09 15:50             ` FUJITA Tomonori
2023-10-11  9:59               ` Miguel Ojeda
2023-10-11 23:18                 ` FUJITA Tomonori
2023-10-13 11:59                   ` Miguel Ojeda
2023-10-13 15:15                     ` FUJITA Tomonori
2023-10-13 18:33                       ` Miguel Ojeda
2023-10-14 12:31                         ` FUJITA Tomonori
2023-10-14 16:19                           ` Miguel Ojeda
2023-10-12  0:29                 ` FUJITA Tomonori
2023-10-09 21:07           ` Trevor Gross
2023-10-09 21:21             ` Andrew Lunn
2023-10-11  7:04             ` FUJITA Tomonori
2023-10-09 13:54     ` Andrew Lunn
2023-10-09 14:48       ` Miguel Ojeda
2023-10-09 17:04         ` Andrew Lunn
2023-10-12  3:59     ` FUJITA Tomonori
2023-10-12  4:43       ` Trevor Gross
2023-10-12  7:09         ` FUJITA Tomonori
2023-10-11 18:29   ` Boqun Feng
2023-10-12  5:58     ` FUJITA Tomonori
2023-10-12  6:34       ` Boqun Feng
2023-10-12  6:44         ` FUJITA Tomonori
2023-10-12  7:02           ` FUJITA Tomonori
2023-10-12  7:13             ` Boqun Feng
2023-10-12  7:32               ` Trevor Gross
2023-10-12  7:58                 ` FUJITA Tomonori
2023-10-12  9:10                   ` Benno Lossin
2023-10-13  4:17                     ` Boqun Feng [this message]
2023-10-13  5:45                       ` FUJITA Tomonori
2023-10-13  7:56                         ` Benno Lossin
2023-10-13  9:53                           ` FUJITA Tomonori
2023-10-13 10:03                             ` Benno Lossin
2023-10-13 10:53                               ` FUJITA Tomonori
2023-10-14  7:47                                 ` Benno Lossin
2023-10-14 21:55                                   ` Andrew Lunn
2023-10-14 22:18                                     ` Benno Lossin
2023-10-14 22:33                                       ` Andrew Lunn
2023-10-14  4:11                             ` Boqun Feng
2023-10-14 11:59                             ` Miguel Ojeda
2023-10-12  7:07           ` Boqun Feng
2023-10-09  1:39 ` [PATCH net-next v3 2/3] MAINTAINERS: add Rust PHY abstractions to the ETHERNET PHY LIBRARY FUJITA Tomonori
2023-10-09  1:39 ` [PATCH net-next v3 3/3] net: phy: add Rust Asix PHY driver FUJITA Tomonori
2023-10-09  3:22   ` Trevor Gross
2023-10-09  7:23   ` Jiri Pirko
2023-10-09 10:58     ` Miguel Ojeda
2023-10-09 11:41     ` FUJITA Tomonori
2023-10-09 12:32     ` Andrew Lunn
2023-10-09 14:01       ` Miguel Ojeda
2023-10-09 14:31         ` Andrew Lunn
2023-10-09 15:27           ` Miguel Ojeda
2023-10-09 15:35             ` Miguel Ojeda
2023-10-09 16:09               ` Andrew Lunn
2023-10-09 10:10   ` Greg KH
2023-10-12 11:57     ` FUJITA Tomonori
2023-10-09 12:42   ` Benno Lossin
2023-10-09 13:15     ` Andrew Lunn
2023-10-09 13:45       ` Benno Lossin
2023-10-09 12:48 ` [PATCH net-next v3 0/3] Rust abstractions for network PHY drivers Andrew Lunn
2023-10-09 12:53   ` Miguel Ojeda
2023-10-09 13:06     ` Greg KH
2023-10-09 14:13       ` Miguel Ojeda
2023-10-09 14:52         ` Greg KH
2023-10-09 15:06           ` Miguel Ojeda
2023-10-09 15:14             ` Greg KH
2023-10-09 15:15               ` Miguel Ojeda
2023-10-09 13:24     ` Andrew Lunn
2023-10-09 13:36       ` Miguel Ojeda
2023-10-09 14:21     ` Andrea Righi
2023-10-09 14:22       ` Miguel Ojeda
2023-10-09 14:56       ` Andrew Lunn
2023-10-09 15:04         ` Greg KH
2023-10-09 15:10           ` Miguel Ojeda
2023-10-09 15:15             ` Miguel Ojeda
2023-10-09 14:56       ` Greg KH
2023-10-09 15:09         ` Andrea Righi

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=ZSjEyn-YNJiXPT4I@Boquns-Mac-mini.home \
    --to=boqun.feng@gmail.com \
    --cc=andrew@lunn.ch \
    --cc=benno.lossin@proton.me \
    --cc=fujita.tomonori@gmail.com \
    --cc=greg@kroah.com \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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).