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 v4 1/2] rust: net: phy: take &self in methods that do not need phy_device->lock
Date: Fri, 27 Nov 2026 15:01:54 +0000 [thread overview]
Message-ID: <179579171493.3135.13012003515691317246@kernel.org> (raw)
In-Reply-To: <20261126-exp-phy-rs-maybelocked-v4-1-16f9a10bb579@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] In rust/kernel/net/phy.rs, `Adapter::soft_reset_callback`
still builds a `&mut Device` with `Device::from_raw_mut()` on a path…
--
rust: net: phy: take &self in methods that do not need phy_device->lock
This commit splits Device::from_raw() into a shared from_raw() and a new
from_raw_mut(). It redefines &mut Device to mean exclusive access to the
phy_device state that phy_device->lock protects. The getters for that state
now take &mut self, and phy_id(), read(), write() and genphy_suspend() now
take &self. The callback SAFETY comments are updated to say when PHYLIB
calls the callbacks without the lock.
> diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
> index c4e7b1d6c6f4a..bb2cc6b3c94c8 100644
> --- a/rust/kernel/net/phy.rs
> +++ b/rust/kernel/net/phy.rs
> @@ -57,50 +57,71 @@ pub enum DuplexMode {
[ ... ]
> +/// PHYLIB calls the callbacks that get a `&mut Device` with `phy_device->lock` held, except:
> +///
> +/// - [`Driver::probe`] and [`Driver::get_features`], which `phy_probe()` calls without the lock
> +/// before the device is attached.
> +/// - [`Driver::soft_reset`], which `phy_init_hw()` calls without the lock while the state machine
> +/// is not running, or from a MAC driver that then ensures exclusive access. `phy_mii_ioctl()`,
> +/// which PHYLIB documents as "use at own risk", is outside this guarantee.
> +/// - [`Driver::read_status`], when a MAC driver calls `phy_read_status()` directly. The MAC
> +/// driver is then responsible for exclusive access.
> +///
> /// # Invariants
> ///
> -/// - Referencing a `phy_device` using this struct asserts that you are in
> -/// a context where all methods defined on this struct are safe to call.
> -/// - This struct always has a valid `self.0.mdio.dev`.
> +/// - `self.0` is a valid `phy_device`, and `self.0.mdio.dev` is a valid `struct device`.
> +/// - For the lifetime of a `&mut Device`, no other thread accesses the state of the `phy_device`
> +/// that `phy_device->lock` protects.
[ ... ]
> + /// Creates a new mutable [`Device`] reference from a raw pointer.
> + ///
> + /// # Safety
> + ///
> + /// For the duration of `'a`,
> + /// - the pointer must point at a valid `phy_device`.
> + /// - `(*ptr).mdio.dev` must be valid.
> + /// - the caller must have the exclusive access that [`Device`] describes.
> + unsafe fn from_raw_mut<'a>(ptr: *mut bindings::phy_device) -> &'a mut Self {
[ ... ]
> @@ -309,10 +331,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. The
> + // `phy_mii_ioctl()` path is outside this guarantee, see `Device`.
> + let dev = unsafe { Device::from_raw_mut(phydev) };
[Severity: Medium]
Is it sound to call Device::from_raw_mut() in soft_reset_callback() when
the SAFETY comment says the phy_mii_ioctl() path does not give the
exclusive access that from_raw_mut() now requires?
The new invariant on Device says that for the lifetime of a &mut Device,
no other thread accesses the phy_device state that phy_device->lock
protects. However, phy_mii_ioctl() reaches this callback without
phydev->lock, and the PHY state machine may be running at that point:
phy_mii_ioctl() /* SIOCSMIIREG, BMCR write with BMCR_RESET */
phy_init_hw(phydev)
phydev->drv->soft_reset(phydev)
Adapter::soft_reset_callback()
Device::from_raw_mut(phydev)
T::soft_reset(dev)
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 run
read_status_callback(). That creates a second &mut Device and writes link,
speed and duplex.
Safe code in a driver's soft_reset can call state(), is_link_up(),
is_autoneg_enabled(), is_autoneg_completed(), set_speed() and set_duplex().
Wouldn't that be a data race reachable from safe Rust?
The commit message says the goal is "so that safe code cannot use that
state without the access". Documenting phy_mii_ioctl() as "outside this
guarantee" does not seem to enforce that.
soft_reset already got a &mut Device before this patch. This patch states
the exclusivity precondition explicitly, though, and the new SAFETY comment
says it is not met on this path. The only in-tree user, asix_soft_reset()
in ax88796b_rust, calls only dev.write() and genphy_soft_reset(). The last
patch in the series, "rust: net: phy: pass &Device to suspend, read_mmd and
write_mmd", leaves soft_reset_callback() unchanged.
Could Driver::soft_reset take a &Device instead, so this path does not
create a &mut Device?
> T::soft_reset(dev)?;
> Ok(0)
> })
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126-exp-phy-rs-maybelocked-v4-0-16f9a10bb579%40gmail.com
next prev parent reply other threads:[~2026-11-27 15:01 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 [this message]
2026-11-26 14:08 ` [PATCH net-next v4 2/2] rust: net: phy: pass &Device to suspend, read_mmd and write_mmd SeungJong Ha
2026-11-27 15:01 ` 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=179579171493.3135.13012003515691317246@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®