[PATCH net-next v4 2/2] rust: net: phy: pass &Device to suspend, read_mmd and write_mmd
From: SeungJong Ha
Date: Thu Nov 26 2026 - 09:09:16 EST
phy_suspend() calls `suspend` without `phy_device->lock`, also right
after the state machine drops it, and __phy_read_mmd() and
__phy_write_mmd() call `read_mmd` and `write_mmd` with the MDIO bus lock
held but not necessarily `phy_device->lock`. Yet these callbacks get a
`&mut Device` and can call methods such as set_speed() that rely on the
lock.
Pass a `&Device` instead, and document that read() and write() deadlock
in `read_mmd` and `write_mmd`. Update ax88796b_rust, the only user of
`suspend`; it only calls genphy_suspend().
Suggested-by: FUJITA Tomonori <fujita.tomonori@xxxxxxxxx>
Signed-off-by: SeungJong Ha <engineer.jjhama@xxxxxxxxx>
---
drivers/net/phy/ax88796b_rust.rs | 4 ++--
rust/kernel/net/phy.rs | 36 +++++++++++++++++++++---------------
2 files changed, 23 insertions(+), 17 deletions(-)
diff --git a/drivers/net/phy/ax88796b_rust.rs b/drivers/net/phy/ax88796b_rust.rs
index 2d24628a4e58..890ae79d8561 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<u16> {
Ok(0)
}
- fn suspend(dev: &mut phy::Device) -> Result {
+ fn suspend(dev: &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: &phy::Device) -> Result {
dev.genphy_suspend()
}
diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
index bb2cc6b3c94c..d04a651ae62b 100644
--- a/rust/kernel/net/phy.rs
+++ b/rust/kernel/net/phy.rs
@@ -371,10 +371,10 @@ impl<T: Driver> Adapter<T> {
/// `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_mut(phydev) };
+ // SAFETY: `phydev` is valid for the duration of the callback. `phy_suspend()` does not
+ // take `phy_device->lock`, and `phy_state_machine()` calls it right after dropping the
+ // lock, so this gets a `&Device`.
+ let dev = unsafe { Device::from_raw(phydev) };
T::suspend(dev)?;
Ok(0)
})
@@ -439,10 +439,10 @@ impl<T: Driver> Adapter<T> {
regnum: u16,
) -> i32 {
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_mut(phydev) };
+ // SAFETY: `phydev` is valid for the duration of the callback. PHYLIB calls this with
+ // the MDIO bus lock held but not necessarily `phy_device->lock`, so this gets a
+ // `&Device`.
+ let dev = unsafe { Device::from_raw(phydev) };
// CAST: the C side verifies devnum < 32.
let ret = T::read_mmd(dev, devnum as u8, regnum)?;
Ok(ret.into())
@@ -459,10 +459,10 @@ impl<T: Driver> Adapter<T> {
val: u16,
) -> i32 {
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_mut(phydev) };
+ // SAFETY: `phydev` is valid for the duration of the callback. PHYLIB calls this with
+ // the MDIO bus lock held but not necessarily `phy_device->lock`, so this gets a
+ // `&Device`.
+ let dev = unsafe { Device::from_raw(phydev) };
T::write_mmd(dev, devnum as u8, regnum, val)?;
Ok(0)
})
@@ -617,7 +617,7 @@ fn read_status(_dev: &mut Device) -> Result<u16> {
}
/// Suspends the hardware, saving state if needed.
- fn suspend(_dev: &mut Device) -> Result {
+ fn suspend(_dev: &Device) -> Result {
build_error!(VTABLE_DEFAULT_ERROR)
}
@@ -627,12 +627,18 @@ fn resume(_dev: &mut Device) -> Result {
}
/// Overrides the default MMD read function for reading a MMD register.
- fn read_mmd(_dev: &mut Device, _devnum: u8, _regnum: u16) -> Result<u16> {
+ ///
+ /// PHYLIB calls this with the MDIO bus lock held, so calling [`Device::read`] or
+ /// [`Device::write`] here deadlocks.
+ fn read_mmd(_dev: &Device, _devnum: u8, _regnum: u16) -> Result<u16> {
build_error!(VTABLE_DEFAULT_ERROR)
}
/// Overrides the default MMD write function for writing a MMD register.
- fn write_mmd(_dev: &mut Device, _devnum: u8, _regnum: u16, _val: u16) -> Result {
+ ///
+ /// PHYLIB calls this with the MDIO bus lock held, so calling [`Device::read`] or
+ /// [`Device::write`] here deadlocks.
+ fn write_mmd(_dev: &Device, _devnum: u8, _regnum: u16, _val: u16) -> Result {
build_error!(VTABLE_DEFAULT_ERROR)
}
--
2.54.0