Re: [PATCH net-next v3 2/3] rust: net: phy: make Device generic over a lock context
From: netdev-bot+sashiko
Date: Thu Nov 19 2026 - 20:37:50 EST
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] This patch adds a formal `# Invariants` section to `Locked`:
"For the lifetime of a `&mut Device<Locked>`, no other thread accesses…
--
rust: net: phy: make Device generic over a lock context
This patch makes the Rust PHY Device type generic over a lock context.
The context is either Locked (the default) or MaybeLocked, and methods
that do not need phy_device->lock move to both contexts. The patch also
documents when the Locked callbacks run without the lock and rewrites
their SAFETY comments.
> diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
> index 4604a9c0a5111..8cbb4c19dac1d 100644
> --- a/rust/kernel/net/phy.rs
> +++ b/rust/kernel/net/phy.rs
> @@ -50,6 +50,55 @@ pub enum DuplexMode {
[ ... ]
> +/// - [`Driver::soft_reset`], which `phy_init_hw()` calls without the lock from
> +/// `phy_attach_direct()` and `mdio_bus_phy_resume()` while the state machine is not running, and
> +/// from MAC drivers that call `phy_init_hw()` directly, which are then responsible for exclusive
> +/// access. `phy_mii_ioctl()` also calls it without the lock while the state machine may run,
> +/// which is an existing PHYLIB race.
> +/// - [`Driver::read_status`], when a MAC driver calls `phy_read_status()` directly. The MAC
> +/// driver is then responsible for exclusive access.
> +///
> +/// # Invariants
> +///
> +/// For the lifetime of a `&mut Device<Locked>`, no other thread accesses the state of the
> +/// `phy_device` that `phy_device->lock` protects.
> +pub struct Locked;
[ ... ]
> @@ -309,10 +355,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.
> + // `phy_mii_ioctl()` is an exception, see `Locked`. This is the `Locked` context.
> + let dev = unsafe { Device::<Locked>::from_raw(phydev) };
[Severity: Medium]
Does this SAFETY comment justify the from_raw() call?
The new Locked invariant says that no other thread accesses the
lock-protected phy_device state for the lifetime of a &mut Device<Locked>.
from_raw() now also requires the caller to be in the Ctx context.
However, this comment and the Locked docs above both say that
phy_mii_ioctl() reaches this callback without phydev->lock while the
state machine may be running.
An SIOCSMIIREG write to MII_BMCR with BMCR_RESET set takes this path:
ndo_eth_ioctl
phy_mii_ioctl()
phy_init_hw() /* no phydev->lock */
drv->soft_reset()
Adapter::soft_reset_callback()
Device::<Locked>::from_raw()
drivers/net/phy/phy.c:phy_mii_ioctl() {
...
if (prtad == phydev->mdio.addr &&
devad == MII_BMCR &&
val & BMCR_RESET)
return phy_init_hw(phydev);
...
}
At the same time, phy_state_machine() can hold phydev->lock and write
speed, duplex, link, autoneg and state.
This affects an in-tree driver. asix_soft_reset() in
drivers/net/phy/ax88796b_rust.rs calls genphy_soft_reset(), which is
only available on Device<Locked>. It reads phydev->autoneg and writes the
phydev->suspended bitfield.
Can that race with the state machine's writes to the same fields and to
the neighbouring bitfields? That would be a data race reachable from safe
Rust code.
The C-side race in phy_mii_ioctl() was there before this patch. The
Locked invariant and the "This is the Locked context" justification are
new here, though.
The commit message says:
Document when the `Locked` callbacks run without the lock, including the
existing phy_mii_ioctl() race, and fix their SAFETY comments.
For soft_reset, the new comment itself says the precondition is not met.
The later patches in the series only move suspend to MaybeLocked.
Would one of these work instead?
- take phydev->lock around phy_init_hw() in phy_mii_ioctl()
- weaken the Locked invariant and drop the accessors that rely on it
- give soft_reset a context that does not claim exclusive access
> T::soft_reset(dev)?;
> Ok(0)
> })
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117-exp-phy-rs-maybelocked-v3-0-4e183a7366a7%40gmail.com