[PATCH net-next v4 0/2] rust: net: phy: pass &Device to callbacks without exclusive access
From: SeungJong Ha
Date: Thu Nov 26 2026 - 09:08:56 EST
The Rust PHY abstraction gives every driver callback a `&mut Device` and
says that PHYLIB holds `phy_device->lock`, but phy_suspend() calls
`suspend` without it, also right after the state machine drops the
lock, and read_mmd and write_mmd run with the MDIO bus lock held
instead. This was found while auditing the SAFETY comments of Rust
abstractions against their C callers, and confirmed with Clang's
context analysis [1].
As FUJITA suggested on v3, the series uses `&Device` and `&mut Device`
for this instead of new context types. Patch 1 makes `&mut Device` mean
exclusive access to the state that `phy_device->lock` protects, fits the
method receivers to that, and documents when the `&mut Device` callbacks
run without the lock. Patch 2 passes a `&Device` to suspend, read_mmd
and write_mmd. The generated code of ax88796b_rust and qt2025 does not
change.
The audit, the patches and their changelogs were done with an AI coding
assistant (Claude Code, Claude Opus 5.5) and reviewed line by line by
me. Compile-tested only.
[1] https://docs.kernel.org/dev-tools/context-analysis.html
---
Changes in v4:
- Use `&Device` and `&mut Device` instead of the `Locked` and
`MaybeLocked` context types (FUJITA)
- Keep `read_mmd`, `write_mmd` and `match_phy_device`. Pass `&Device` to
`read_mmd` and `write_mmd`, and document that read() and write()
deadlock there (FUJITA)
- Make read(), write() and genphy_suspend() take `&self`.
`match_phy_device` can access registers as C implementations do,
while read_paged() stays `&mut self`
- Make the getters of the state that `phy_device->lock` protects take
`&mut self`
- Name the fields that `phy_device->lock` protects in the `Device`
documentation
- Say that the phy_mii_ioctl() path is outside the guarantee (Andrew,
FUJITA)
- Link to v3: https://lore.kernel.org/r/20261117-exp-phy-rs-maybelocked-v3-0-4e183a7366a7@xxxxxxxxx
Changes in v3:
- Give `suspend` the `MaybeLocked` context, and make `genphy_suspend()`
available in both contexts (netdev AI review)
- Move `soft_reset` and `genphy_soft_reset()` back to `Locked`, since
`genphy_soft_reset()` in `MaybeLocked` could race with lock holders
in `suspend`, and document phy_mii_ioctl() as an existing PHYLIB race
instead (netdev AI review)
- Drop `match_phy_device` as well (netdev AI review)
- Fix the `get_features`, `soft_reset`, `read_status` and `resume`
SAFETY comments, and name the context at each `from_raw()` call
- List the exceptions in the `Locked` documentation, including MAC
drivers that call phy_init_hw() or phy_read_status() directly
- Link to v2: https://lore.kernel.org/r/20261115-exp-phy-rs-maybelocked-v2-0-8fa7897a0352@xxxxxxxxx
Changes in v2:
- Keep `&mut self` on the moved methods, so that `match_phy_device` does
not gain register access (netdev AI review)
- Give only `soft_reset` the `MaybeLocked` context
- Drop `read_mmd` and `write_mmd` instead of leaving them `Locked`
- Document how PHYLIB calls the `Locked` callbacks, and fix the
`from_raw()`, `phy_id()` and `genphy_soft_reset()` SAFETY comments
- Link to v1: https://lore.kernel.org/r/20261110-exp-phy-rs-maybelocked-v1-0-3023cc9055e9@xxxxxxxxx
---
SeungJong Ha (2):
rust: net: phy: take &self in methods that do not need phy_device->lock
rust: net: phy: pass &Device to suspend, read_mmd and write_mmd
drivers/net/phy/ax88796b_rust.rs | 4 +-
rust/kernel/net/phy.rs | 165 +++++++++++++++++++++------------------
rust/kernel/net/phy/reg.rs | 12 +--
3 files changed, 99 insertions(+), 82 deletions(-)
---
base-commit: 5f049e811762d6784392d9984ce910257224824b
change-id: 20261110-exp-phy-rs-maybelocked-ddc59993efba
Best regards,
--
SeungJong Ha <engineer.jjhama@xxxxxxxxx>