[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20231028.182723.123878459003900402.fujita.tomonori@gmail.com>
Date: Sat, 28 Oct 2023 18:27:23 +0900 (JST)
From: FUJITA Tomonori <fujita.tomonori@...il.com>
To: benno.lossin@...ton.me
Cc: boqun.feng@...il.com, fujita.tomonori@...il.com,
netdev@...r.kernel.org, rust-for-linux@...r.kernel.org, andrew@...n.ch,
tmgross@...ch.edu, miguel.ojeda.sandonis@...il.com, wedsonaf@...il.com
Subject: Re: [PATCH net-next v7 1/5] rust: core abstractions for network
PHY drivers
On Fri, 27 Oct 2023 21:19:38 +0000
Benno Lossin <benno.lossin@...ton.me> wrote:
> On 10/27/23 21:59, Boqun Feng wrote:
>> On Thu, Oct 26, 2023 at 09:10:46AM +0900, FUJITA Tomonori wrote:
>> [...]
>>> + /// Gets the current link state.
>>> + ///
>>> + /// It returns true if the link is up.
>>> + pub fn is_link_up(&self) -> bool {
>>> + const LINK_IS_UP: u32 = 1;
>>> + // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
>>> + let phydev = unsafe { *self.0.get() };
>>
>> Tomo, FWIW, the above line means *copying* the content pointed by
>> `self.0.get()` into `phydev`, i.e. `phydev` is the semantically a copy
>> of the `phy_device` instead of an alias. In C code, it means you did:
>
> Good catch. `phy_device` is rather large (did not look at the exact
> size) and this will not be optimized on debug builds, so it could lead
> to stackoverflows.
>
>> struct phy_device phydev = *ptr;
>>
>> Sure, both compilers can figure this out, therefore no extra copy is
>> done, but still it's better to avoid this copy semantics by doing:
>>
>> let phydev = unsafe { &*self.0.get() };
>
> We need to be careful here, since doing this creates a reference
> `&bindings::phy_device` which asserts that it is immutable. That is not
> the case, since the C side might change it at any point (this is the
> reason we wrap things in `Opaque`, since that allows mutatation even
> through sharde references).
You meant that the C code might modify it independently anytime, not
the C code called the Rust abstractions might modify it, right?
> I did not notice this before, but this means we cannot use the `link`
> function from bindgen, since that takes `&self`. We would need a
> function that takes `*const Self` instead.
Implementing functions to access to a bitfield looks tricky so we need
to add such feature to bindgen or we add getters to the C side?
Powered by blists - more mailing lists