From: netdev-bot+sashiko@kernel.org
To: engineer.jjhama@gmail.com
Cc: fujita.tomonori@gmail.com, tmgross@umich.edu,
davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
pabeni@redhat.com, horms@kernel.org, ojeda@kernel.org,
boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com,
lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com,
dakr@kernel.org, daniel.almeida@collabora.com, tamird@kernel.org,
acourbot@nvidia.com, work@onurozkan.dev, andrew@lunn.ch,
hkallweit1@gmail.com, linux@armlinux.org.uk,
netdev@vger.kernel.org, rust-for-linux@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH net-next v3 2/3] rust: net: phy: make Device generic over a lock context
Date: Fri, 20 Nov 2026 01:37:42 +0000 [thread overview]
Message-ID: <179513866237.3136.3197734576083995986@kernel.org> (raw)
In-Reply-To: <20261117-exp-phy-rs-maybelocked-v3-2-4e183a7366a7@gmail.com>
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
next prev parent reply other threads:[~2026-11-20 1:37 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 10:36 [PATCH net-next v3 0/3] rust: net: phy: suspend may run without phy_device->lock SeungJong Ha
2026-11-17 10:36 ` [PATCH net-next v3 1/3] rust: net: phy: drop unused read_mmd, write_mmd and match_phy_device SeungJong Ha
2026-11-17 10:36 ` [PATCH net-next v3 2/3] rust: net: phy: make Device generic over a lock context SeungJong Ha
2026-11-20 1:37 ` netdev-bot+sashiko [this message]
2026-11-17 10:36 ` [PATCH net-next v3 3/3] rust: net: phy: pass a MaybeLocked device to suspend SeungJong Ha
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=179513866237.3136.3197734576083995986@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--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=engineer.jjhama@gmail.com \
--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®