phy_suspend() calls `suspend` without `phy_device->lock`, also right after the state machine drops it, so another path may hold the lock meanwhile. Yet the callback gets a `Locked` device, and can call methods such as set_speed() that rely on the lock. Pass a `&mut Device` and correct the SAFETY comment. Update ax88796b_rust, the only user; it only calls genphy_suspend(). Signed-off-by: SeungJong Ha --- drivers/net/phy/ax88796b_rust.rs | 4 ++-- rust/kernel/net/phy.rs | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/drivers/net/phy/ax88796b_rust.rs b/drivers/net/phy/ax88796b_rust.rs index 2d24628a4e58..498704f1ef9d 100644 --- a/drivers/net/phy/ax88796b_rust.rs +++ b/drivers/net/phy/ax88796b_rust.rs @@ -77,7 +77,7 @@ fn read_status(dev: &mut phy::Device) -> Result { Ok(0) } - fn suspend(dev: &mut phy::Device) -> Result { + fn suspend(dev: &mut phy::Device) -> Result { dev.genphy_suspend() } @@ -107,7 +107,7 @@ impl Driver for PhyAX88772C { const NAME: &'static CStr = c"Asix Electronics AX88772C"; const PHY_DEVICE_ID: DeviceId = DeviceId::new_with_exact_mask(0x003b1881); - fn suspend(dev: &mut phy::Device) -> Result { + fn suspend(dev: &mut phy::Device) -> Result { dev.genphy_suspend() } diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs index 8cbb4c19dac1..4b7b3ad09059 100644 --- a/rust/kernel/net/phy.rs +++ b/rust/kernel/net/phy.rs @@ -395,10 +395,9 @@ impl Adapter { /// `phydev` must be passed by the corresponding callback in `phy_driver`. unsafe extern "C" fn suspend_callback(phydev: *mut bindings::phy_device) -> c_int { from_result(|| { - // SAFETY: The C core code ensures that the accessors on - // `Device` are okay to call even though `phy_device->lock` - // might not be held. - let dev = unsafe { Device::from_raw(phydev) }; + // SAFETY: `phy_suspend()` does not take `phy_device->lock`, and `phy_state_machine()` + // calls it right after dropping the lock. This is the `MaybeLocked` context. + let dev = unsafe { Device::::from_raw(phydev) }; T::suspend(dev)?; Ok(0) }) @@ -572,7 +571,7 @@ fn read_status(_dev: &mut Device) -> Result { } /// Suspends the hardware, saving state if needed. - fn suspend(_dev: &mut Device) -> Result { + fn suspend(_dev: &mut Device) -> Result { build_error!(VTABLE_DEFAULT_ERROR) } -- 2.54.0