Re: [PATCH net-next v4 1/2] rust: net: phy: take &self in methods that do not need phy_device->lock
From: SeungJong Ha
Date: Fri Nov 27 2026 - 23:35:11 EST
2026년 11월 28일 (토) 오전 12:01, <netdev-bot+sashiko@xxxxxxxxxx>님이 작성:
> rust: net: phy: take &self in methods that do not need phy_device->lock
>
> This commit splits Device::from_raw() into a shared from_raw() and a new
> from_raw_mut(). It redefines &mut Device to mean exclusive access to the
> phy_device state that phy_device->lock protects. The getters for that state
> now take &mut self, and phy_id(), read(), write() and genphy_suspend() now
> take &self. The callback SAFETY comments are updated to say when PHYLIB
> calls the callbacks without the lock.
>
> > diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
> > index c4e7b1d6c6f4a..bb2cc6b3c94c8 100644
> > --- a/rust/kernel/net/phy.rs
> > +++ b/rust/kernel/net/phy.rs
> > @@ -57,50 +57,71 @@ pub enum DuplexMode {
>
> [ ... ]
>
> > +/// PHYLIB calls the callbacks that get a `&mut Device` with `phy_device->lock` held, except:
> > +///
> > +/// - [`Driver::probe`] and [`Driver::get_features`], which `phy_probe()` calls without the lock
> > +/// before the device is attached.
> > +/// - [`Driver::soft_reset`], which `phy_init_hw()` calls without the lock while the state machine
> > +/// is not running, or from a MAC driver that then ensures exclusive access. `phy_mii_ioctl()`,
> > +/// which PHYLIB documents as "use at own risk", is outside this guarantee.
> > +/// - [`Driver::read_status`], when a MAC driver calls `phy_read_status()` directly. The MAC
> > +/// driver is then responsible for exclusive access.
> > +///
> > /// # Invariants
> > ///
> > -/// - Referencing a `phy_device` using this struct asserts that you are in
> > -/// a context where all methods defined on this struct are safe to call.
> > -/// - This struct always has a valid `self.0.mdio.dev`.
> > +/// - `self.0` is a valid `phy_device`, and `self.0.mdio.dev` is a valid `struct device`.
> > +/// - For the lifetime of a `&mut Device`, no other thread accesses the state of the `phy_device`
> > +/// that `phy_device->lock` protects.
>
> [ ... ]
>
> > + /// Creates a new mutable [`Device`] reference from a raw pointer.
> > + ///
> > + /// # Safety
> > + ///
> > + /// For the duration of `'a`,
> > + /// - the pointer must point at a valid `phy_device`.
> > + /// - `(*ptr).mdio.dev` must be valid.
> > + /// - the caller must have the exclusive access that [`Device`] describes.
> > + unsafe fn from_raw_mut<'a>(ptr: *mut bindings::phy_device) -> &'a mut Self {
>
> [ ... ]
>
> > @@ -309,10 +331,10 @@ impl<T: Driver> Adapter<T> {
> > /// `phydev` must be passed by the corresponding callback in `phy_driver`.
> > unsafe extern "C" fn soft_reset_callback(phydev: *mut bindings::phy_device) -> c_int {
> > from_result(|| {
> > - // SAFETY: This callback is called only in contexts
> > - // where we hold `phy_device->lock`, so the accessors on
> > - // `Device` are okay to call.
> > - let dev = unsafe { Device::from_raw(phydev) };
> > + // SAFETY: `phy_init_hw()` calls this with `phy_device->lock` held, or without it while
> > + // the state machine is not running or a MAC driver ensures exclusive access. The
> > + // `phy_mii_ioctl()` path is outside this guarantee, see `Device`.
> > + let dev = unsafe { Device::from_raw_mut(phydev) };
>
> [Severity: Medium]
> Is it sound to call Device::from_raw_mut() in soft_reset_callback() when
> the SAFETY comment says the phy_mii_ioctl() path does not give the
> exclusive access that from_raw_mut() now requires?
>
> The new invariant on Device says that for the lifetime of a &mut Device,
> no other thread accesses the phy_device state that phy_device->lock
> protects. However, phy_mii_ioctl() reaches this callback without
> phydev->lock, and the PHY state machine may be running at that point:
As discussed on v3 [1], this is documented as an exception, see the
Device docs above. Passing &Device to soft_reset would drop
genphy_soft_reset(), which ax88796b_rust needs, and FUJITA agreed on
keeping &mut Device there [2].
[1] https://lore.kernel.org/all/bfd7a44f-2bba-4aee-9758-1dd88f94566f@xxxxxxx/
[2] https://lore.kernel.org/all/20261126.081155.1605990248496749195.tomo@xxxxxxxxxxxx/