Re: [PATCH net-next v3 1/3] rust: core abstractions for network PHY drivers
From: Andrew Lunn <andrew@lunn.ch>
Date: 2023-10-09 13:02:36
Also in:
rust-for-linux
On Mon, Oct 09, 2023 at 12:19:54PM +0000, Benno Lossin wrote:
quoted
+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() }Missing `SAFETY` comment.
Hi Benno It is normal on Linux kernel mailing lists to trim the text to what is just relevant to the reply. Comments don't get lost that way.
quoted
+ /// Returns true if the link is up. + pub fn get_link(&mut self) -> bool {I would call this function `is_link_up`.
Please read the discussion on the previous versions of this patch. If you still want to change the name, please give a justification.
quoted
+ /// Reads a given C22 PHY register. + pub fn read(&mut self, regnum: u16) -> Result<u16> { + let phydev = self.0.get(); + // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`. + // So an FFI call with a valid pointer. + let ret = unsafe { + bindings::mdiobus_read((*phydev).mdio.bus, (*phydev).mdio.addr, regnum.into()) + };Just a general question, all of these unsafe calls do not have additional requirements? So aside from the pointers being valid, there are no timing/locking/other safety requirements for calling the functions?
Locking has been discussed a number of times already. What do you mean by timing?
quoted
+// SAFETY: `Registration` does not expose any of its state across threads. +unsafe impl Send for Registration {}Question: is it ok for two different threads to call `phy_drivers_register` and `phy_drivers_unregister`? If no, then `Send` must not be implemented.
The core phy_drivers_register() is thread safe. It boils down to a driver_register() which gets hammered every kernel boot, so locking issues would soon be found there.
quoted
+macro_rules! module_phy_driver { + (@replace_expr $_t:tt $sub:expr) => {$sub}; + + (@count_devices $($x:expr),*) => { + 0usize $(+ $crate::module_phy_driver!(@replace_expr $x 1usize))* + }; + + (@device_table [$($dev:expr),+]) => { + #[no_mangle] + static __mod_mdio__phydev_device_table: [Shouldn't this have a unique name? If we define two different phy drivers with this macro we would have a symbol collision?
I assume these are the equivalent of C static. It is not visible
outside the scope of this object file. The kernel has lots of tables
and they are mostly of very limited visibility scope, because only the
method registering/unregistering the table needs to see it.
Andrew