mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 RESEND net-next v2 3/3] rust: net: phy: pass a MaybeLocked device to soft_reset
Date: Mon, 16 Nov 2026 07:33:34 +0000	[thread overview]
Message-ID: <179481441455.3136.11985756118593625944@kernel.org> (raw)
In-Reply-To: <20261115-exp-phy-rs-maybelocked-v2-3-8fa7897a0352@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 1

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] The new SAFETY reasoning for the MaybeLocked soft_reset path
  misses the phy_mii_ioctl() path, where soft_reset runs at the same…
- [Low] Two sibling callbacks in rust/kernel/net/phy.rs still carry the
  false lock-held SAFETY comment that this patch removes from…

--

rust: net: phy: pass a MaybeLocked device to soft_reset

This changes the Rust PHY abstraction so that Driver::soft_reset gets a
Device<MaybeLocked> rather than a Locked device. The reason is that
phy_init_hw() calls soft_reset both with and without phydev->lock. The
SAFETY comment in soft_reset_callback is updated to match, and
ax88796b_rust is converted to the new signature.

> diff --git a/drivers/net/phy/ax88796b_rust.rs b/drivers/net/phy/ax88796b_rust.rs
> index 2d24628a4e581..88e6e3c88b448 100644
> --- a/drivers/net/phy/ax88796b_rust.rs
> +++ b/drivers/net/phy/ax88796b_rust.rs
> @@ -30,7 +30,7 @@
>  // BMCR_RESET bit and poll for the reset bit to be cleared.
>  // Toggle BMCR_RESET bit off to accommodate broken AX8796B PHY implementation
>  // such as used on the Individual Computers' X-Surf 100 Zorro card.
> -fn asix_soft_reset(dev: &mut phy::Device) -> Result {
> +fn asix_soft_reset(dev: &mut phy::Device<phy::MaybeLocked>) -> Result {
>      dev.write(C22::BMCR, 0)?;
>      dev.genphy_soft_reset()
>  }

[Severity: Medium]
Is it sound to call genphy_soft_reset() on a Device<MaybeLocked> when the
caller is phy_mii_ioctl()?

When SIOCSMIIREG writes BMCR_RESET to MII_BMCR, phy_mii_ioctl() calls
phy_init_hw() with only RTNL held. phydev->lock is not taken:

phy_mii_ioctl()
  phy_init_hw()
    phydev->drv->soft_reset()
      Adapter::soft_reset_callback()
        asix_soft_reset()
          genphy_soft_reset()

The C genphy_soft_reset() reads phydev->autoneg and writes
phydev->suspended. If autoneg is disabled it also calls
genphy_setup_forced(), which writes pause and asym_pause. After that,
phy_init_hw() writes phydev->suspended = 0 itself.

In struct phy_device, suspended, autoneg, link, autoneg_complete, pause
and asym_pause are bitfields in the same block. A write to suspended is
therefore a read-modify-write of the storage that also holds link and
autoneg_complete.

phy_state_machine() takes only phydev->lock, not RTNL:

    mutex_lock(&phydev->lock);
    state_work = _phy_state_machine(phydev);
    mutex_unlock(&phydev->lock);

Under that lock it runs Locked callbacks such as read_status and
config_aneg, and it updates link. __phy_resume() also writes suspended
under lockdep_assert_held(&phydev->lock).

Nothing keeps the ioctl path and the state machine from running at the
same time. Could a link or pause update be lost here?

This seems to contradict a few statements in the series:

- The Locked invariant says that while a &mut Device<Locked> is alive, no
  other thread touches the state that phy_device->lock protects.

- The SAFETY comment in Device::genphy_soft_reset says the C function
  does not need phy_device->lock because PHYLIB itself calls it without
  the lock. The unlocked PHYLIB callers, phy_attach_direct() and
  mdio_bus_phy_resume(), rely on the state machine not running. The ioctl
  caller has no such exclusion.

- The MaybeLocked documentation gives no invariants beyond those of
  Device, yet genphy_soft_reset() is available in every context.

The race itself is older than this series. C drivers that use
genphy_soft_reset() have it too, and phy_mii_ioctl() is documented as
incompatible with the PHYCONTROL layer. However, this patch is the first
to call genphy_soft_reset() on a MaybeLocked device based on the new
soundness claims.

Would it make sense to take genphy_soft_reset() out of the generic impl?
Alternatively, the MaybeLocked/Locked docs could describe the exclusion
that the unlocked callers rely on, and note that the phy_mii_ioctl() path
breaks it.

[ ... ]

> diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
> index 105a1b727a088..46602798e8aca 100644
> --- a/rust/kernel/net/phy.rs
> +++ b/rust/kernel/net/phy.rs
> @@ -358,9 +358,8 @@ 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.
> +            // SAFETY: `phydev` is valid for the callback. `phy_init_hw()` calls `soft_reset` both
> +            // with and without `phy_device->lock`, which is the `MaybeLocked` context.
>              let dev = unsafe { Device::from_raw(phydev) };
>              T::soft_reset(dev)?;
>              Ok(0)

[Severity: Low]
This fixes the lock-held SAFETY comment in soft_reset_callback(). Two
other callbacks in the same file, get_features_callback() and
match_phy_device_callback(), still have the same text:

    // 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) };

Is that still accurate for either of them? phy_probe() calls
get_features without taking phydev->lock:

    else if (phydrv->get_features)
        err = phydrv->get_features(phydev);

phy_bus_match() calls match_phy_device during driver-core matching. It
also runs without the lock, and the device may be unbound or bound to
another driver:

    if (phydrv->match_phy_device)
        return phydrv->match_phy_device(phydev, phydrv);

The Locked documentation added earlier in this series already says that
both callbacks run without the lock, so the file now contradicts itself.
The earlier commit "rust: net: phy: make Device generic over a lock
context" also says it will "Update the documentation and SAFETY comments
to match".

match_phy_device_callback() also still creates a &mut Device<Locked>,
which is only coerced to &Device when it is passed to
T::match_phy_device(). The Locked invariant cannot hold for a device that
is bound to another driver.

Could that SAFETY comment explain why the short-lived exclusive reference
is acceptable, rather than saying the lock is held?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261115-exp-phy-rs-maybelocked-v2-0-8fa7897a0352%40gmail.com

      reply	other threads:[~2026-11-16  7:33 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-15  6:47 [net-next PATCH RESEND v2 0/3] rust: net: phy: soft_reset may run without phy_device->lock SeungJong Ha
2026-11-15  6:47 ` [PATCH RESEND net-next v2 1/3] rust: net: phy: drop read_mmd and write_mmd from Driver SeungJong Ha
2026-11-15  6:47 ` [PATCH RESEND net-next v2 2/3] rust: net: phy: make Device generic over a lock context SeungJong Ha
2026-11-16  7:33   ` netdev-bot+sashiko
2026-11-15  6:47 ` [PATCH RESEND net-next v2 3/3] rust: net: phy: pass a MaybeLocked device to soft_reset SeungJong Ha
2026-11-16  7:33   ` netdev-bot+sashiko [this message]

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=179481441455.3136.11985756118593625944@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®