From: SeungJong Ha <engineer.jjhama@gmail.com>
To: "FUJITA Tomonori" <fujita.tomonori@gmail.com>,
"Trevor Gross" <tmgross@umich.edu>,
"David S. Miller" <davem@davemloft.net>,
"Eric Dumazet" <edumazet@kernel.org>,
"Jakub Kicinski" <kuba@kernel.org>,
"Paolo Abeni" <pabeni@redhat.com>,
"Simon Horman" <horms@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>, "Andrew Lunn" <andrew@lunn.ch>,
"Heiner Kallweit" <hkallweit1@gmail.com>,
"Russell King" <linux@armlinux.org.uk>
Cc: netdev@vger.kernel.org, rust-for-linux@vger.kernel.org,
linux-kernel@vger.kernel.org,
SeungJong Ha <engineer.jjhama@gmail.com>
Subject: [PATCH net-next v4 2/2] rust: net: phy: pass &Device to suspend, read_mmd and write_mmd
Date: Thu, 26 Nov 2026 23:08:33 +0900 [thread overview]
Message-ID: <20261126-exp-phy-rs-maybelocked-v4-2-16f9a10bb579@gmail.com> (raw)
In-Reply-To: <20261126-exp-phy-rs-maybelocked-v4-0-16f9a10bb579@gmail.com>
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@gmail.com>
Signed-off-by: SeungJong Ha <engineer.jjhama@gmail.com>
---
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
next prev parent reply other threads:[~2026-11-26 14:09 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 14:08 [PATCH net-next v4 0/2] rust: net: phy: pass &Device to callbacks without exclusive access SeungJong Ha
2026-11-26 14:08 ` [PATCH net-next v4 1/2] rust: net: phy: take &self in methods that do not need phy_device->lock SeungJong Ha
2026-11-27 15:01 ` netdev-bot+sashiko
2026-11-26 14:08 ` SeungJong Ha [this message]
2026-11-27 15:01 ` [PATCH net-next v4 2/2] rust: net: phy: pass &Device to suspend, read_mmd and write_mmd netdev-bot+sashiko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261126-exp-phy-rs-maybelocked-v4-2-16f9a10bb579@gmail.com \
--to=engineer.jjhama@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=andrew@lunn.ch \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=hkallweit1@gmail.com \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=lossin@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=pabeni@redhat.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®