From: Benno Lossin <benno.lossin@proton.me>
To: Andrew Lunn <andrew@lunn.ch>
Cc: FUJITA Tomonori <fujita.tomonori@gmail.com>,
boqun.feng@gmail.com, alice@ryhl.io, netdev@vger.kernel.org,
rust-for-linux@vger.kernel.org, tmgross@umich.edu,
miguel.ojeda.sandonis@gmail.com, wedsonaf@gmail.com,
aliceryhl@google.com
Subject: Re: [PATCH net-next v10 1/4] rust: core abstractions for network PHY drivers
Date: Wed, 13 Dec 2023 12:14:55 +0000 [thread overview]
Message-ID: <3d6e1af3-9d86-4058-914f-ea3d7115e0db@proton.me> (raw)
In-Reply-To: <79abc99b-a0f2-48c6-ba68-b72dcf5f7254@lunn.ch>
On 12/13/23 11:28, Andrew Lunn wrote:
>> I still think you need to justify why `mdio.bus` is a pointer that you
>> can give to `midobus_read`.
>
> Maybe a dumb question. Why are you limiting this to just a few members
> of struct phy_device? It has ~50 members, any of which can be used by
> the C side when Rust calls into C.
I limited it to those few members, because the Rust side only uses
those.
Theoretically one could specify all invariants for all members, but that
seems like a *lot* of work.
In Rust everything [1] has to be initialized with a valid value.
Contrast this with C where everything is potentially uninitialized. As
an example, let's look at the first few fields of `PhyDevice`:
struct PhyDevice {
mdio: MdioDevice,
drv: Box<PhyDriver>,
devlink: Box<DeviceLink>,
phy_id: u32,
c45_ids: PhyC45DeviceIds,
// ...
}
Note that in Rust we now do not need to write down any invariants, since
they are all implicitly encoded. For example the `drv` field is of type
`Box<PhyDriver>`, it *always* is a pointer to an allocation that
contains a valid `PhyDriver`. So while on the C side you would have to
state this, on the Rust side we get it for free.
Rust function also make use of the "everything is in a valid state"
rule, they know that the fields are valid and thus the Rust equivalent
of `phy_read` could be safe and without any comments:
impl PhyDevice {
fn phy_read(&self, regnum: u32) -> i32 {
self.mdio.bus.mdiobus_read(self.mdio.addr, regnum)
}
}
All of this only applies to safe code, `unsafe` code is allowed to
violate all of these things temporarily. However, when it gives a value
back to safe code [2], that value needs to be valid.
I think that specifying all of these implicit invariants in C will be
extremely laborious. Especially since the usual way of doing things in C
is not considering these invariants (at least not consciously), but
rather "just do the thing".
The way we do the interoperability is to just fully trust the C side to
produce valid values that we can feed back to the C side. Of course
there are caveats, so e.g. one needs to initialize a `struct mutex`
before it can be used, but that is what we need to capture with the
safety comments.
[1]: There are exceptions for this, but for the purposes of this
discussion, they can be ignored. If you want to know more, you can
read this article in the nomicon:
https://doc.rust-lang.org/nomicon/unchecked-uninit.html
[2]: There are also exceptions, but I will omit them here.
--
Cheers,
Benno
next prev parent reply other threads:[~2023-12-13 12:15 UTC|newest]
Thread overview: 49+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-10 23:49 [PATCH net-next v10 0/4] Rust abstractions for network PHY drivers FUJITA Tomonori
2023-12-10 23:49 ` [PATCH net-next v10 1/4] rust: core " FUJITA Tomonori
2023-12-11 14:01 ` Andrew Lunn
2023-12-11 19:49 ` [net-next PATCH] rust: net: phy: Correct the safety comment for impl Sync Boqun Feng
2023-12-11 20:23 ` Boqun Feng
2023-12-11 21:50 ` Alice Ryhl
2023-12-11 23:22 ` Boqun Feng
2023-12-11 23:55 ` FUJITA Tomonori
2023-12-12 9:17 ` Alice Ryhl
2023-12-12 10:36 ` FUJITA Tomonori
2023-12-11 21:46 ` [PATCH net-next v10 1/4] rust: core abstractions for network PHY drivers Alice Ryhl
2023-12-11 23:15 ` FUJITA Tomonori
2023-12-11 23:40 ` Boqun Feng
2023-12-11 23:47 ` FUJITA Tomonori
2023-12-12 0:49 ` Boqun Feng
2023-12-12 1:46 ` FUJITA Tomonori
2023-12-12 2:30 ` Boqun Feng
2023-12-12 4:04 ` FUJITA Tomonori
2023-12-12 6:11 ` Boqun Feng
2023-12-12 13:02 ` FUJITA Tomonori
2023-12-12 17:35 ` Benno Lossin
2023-12-12 20:23 ` Boqun Feng
2023-12-12 22:40 ` Benno Lossin
2023-12-12 23:27 ` FUJITA Tomonori
2023-12-13 0:02 ` Benno Lossin
2023-12-12 23:31 ` FUJITA Tomonori
2023-12-13 0:01 ` Benno Lossin
2023-12-12 23:01 ` FUJITA Tomonori
2023-12-12 23:15 ` Benno Lossin
2023-12-13 10:28 ` Andrew Lunn
2023-12-13 12:14 ` Benno Lossin [this message]
2023-12-13 10:24 ` Andrew Lunn
2023-12-13 16:43 ` Boqun Feng
2023-12-13 17:12 ` Boqun Feng
2023-12-13 21:48 ` Andrew Lunn
2023-12-13 23:40 ` Benno Lossin
2023-12-13 23:51 ` Benno Lossin
2023-12-14 9:26 ` Andrew Lunn
2023-12-13 23:59 ` Boqun Feng
2023-12-12 12:55 ` Miguel Ojeda
2023-12-12 9:23 ` Alice Ryhl
2023-12-12 10:56 ` FUJITA Tomonori
2023-12-10 23:49 ` [PATCH net-next v10 2/4] rust: net::phy add module_phy_driver macro FUJITA Tomonori
2023-12-11 14:01 ` Andrew Lunn
2023-12-12 22:52 ` Trevor Gross
2023-12-10 23:49 ` [PATCH net-next v10 3/4] MAINTAINERS: add Rust PHY abstractions for ETHERNET PHY LIBRARY FUJITA Tomonori
2023-12-10 23:49 ` [PATCH net-next v10 4/4] net: phy: add Rust Asix PHY driver FUJITA Tomonori
2023-12-11 14:01 ` Andrew Lunn
2023-12-11 21:52 ` Alice Ryhl
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=3d6e1af3-9d86-4058-914f-ea3d7115e0db@proton.me \
--to=benno.lossin@proton.me \
--cc=alice@ryhl.io \
--cc=aliceryhl@google.com \
--cc=andrew@lunn.ch \
--cc=boqun.feng@gmail.com \
--cc=fujita.tomonori@gmail.com \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=netdev@vger.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;
as well as URLs for NNTP newsgroup(s).