Article View: linux.kernel.netdev
Article #23593Re: [PATCH net-next v4 1/2] rust: net: phy: take &self in methods that do not need phy_device->lock
From: netdev-bot+sashiko
Date: Fri, 27 Nov 2026 15:01
Date: Fri, 27 Nov 2026 15:01
136 lines
14175 bytes
14175 bytes
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
Technical Details
Message-ID:
<179579171493.3135.13012003515691317246@kernel.org>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-372792-linux-kernel-netdev=dewy.ruReferences:
<20261126-exp-phy-rs-maybelocked-v4-1-16f9a10bb579@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-372792-linux-kernel-netdev=dewy.ru From: netdev-bot+sashiko@kernel.org Newsgroups: linux.kernel.netdev 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 Organization: Usenet Network Message-ID: <179579171493.3135.13012003515691317246@kernel.org> References: <20261126-exp-phy-rs-maybelocked-v4-1-16f9a10bb579@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="756140"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:dyy36kzs2WjolUzCuvU9iZTFn68= sha256:D6zuAJKawzLRuVk0868nfEvyrMU5DD9tpM0nT1B31Pw= sha1:Bl1UutJ0qrV2jz4FD7quw5V7JSY= sha256:Aoqd/BNfTw8KmKrBIX2qjT7ZU+/EaOH1Jq9ewyXPixA= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id E7779125F60; Fri, 27 Nov 2026 18:02:36 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.401 X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ppmf92-kMoPa; Fri, 27 Nov 2026 18:02:33 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-372792-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id C8B1B125F5E; Fri, 27 Nov 2026 18:02:19 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 6CE75371D5; Fri, 27 Nov 2026 15:02:08 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795791728; b=yzjSkonCL1Qsm0HbVOGMjfOvN27PXi2jCHvwzPrrwg140COTm17DeMXjjo875749aUYy B0IP/mzOwPLod8yziA1mcIASo0Yh8l7FpBAoY7wLbNa8s93KUG8GS47GubtF0cTtIAOXx Q8UjqhuZYImwLH6fkSRLwyMJPu91BictTBLBcLv7VKhlMc2gsIT/3u7NLwqEiapByQuAe mqDY+VkF866xRisJ1R0RnC+Dei/IZLJCD7w8+0OFe0ACC40niug6hDCFSJc4v7tdku1EN jgjvmiZT6fbxBRHNsM8rCDzZ2nsnydcUN6X51A+ABxQ5fX0vRMFwPJppjXJ289hgLow== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795791728; h=X-Original-To:Received:Received:DKIM-Signature:Subject:From:To:Cc: Date:Message-ID:In-Reply-To:References:X-sashiko-severity: Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id: List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner: Precedence:MIME-Version; bh=wL/VA5+pGxzKnAxonRgHumCPvJPgs/jaOjXoLgiZ+Vw=; b=zouy5osc/p8EwgjaushM9jO6KaZMGfD0Xxh6txYN6u8YM+zCvv0aUG9VilKkXV3DNiqU mJbsbyOv4pgwYAaiRYmcO8mXjd9NnIN5bP+wbipWS+44ItjiH9JTuzTWo+b0A89pE3W00 9M4CuHwOGU9piQN2vYG9yxiuT7MGMbIgn2LmwPT3rZWlxNxbxUMmy5V/F7ApV3QbJiB8V FkU67MbIpXIiOsd3dSPNRyq8dwj9ToBNaiBrCv3sTHHT+gu5RLFfRBeTTUQ+Ogrc8kquP f2kUwQWeGWIxx6ShsO6LGbDsFzlU12JvzYkC2zk22XT2fHTjzO5h4jd/TMKp/HjNxTQ== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ddSTOj3O; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 455AE606A31F; Fri, 27 Nov 2026 15:02:08 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ddSTOj3O X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795791717; b=puiHipnnlH6t7Op8HZR0dP99sX9H5zAAXt+kQKQRmS2swzsVLO/290rxPJgua1q5x+Mv q+2IX1hWYLK8lms2rSBtE4GO8hlCnxoCyn9zq29T2p0Nu1pE2U6nAR+h7bQyUy2iKtjM+ +orWTGq+uhlL9VZl5DSa6gHGCOLA7Y9EMqCGRlECjBug9c/uD5piVjOj2ZiCVP10WOJ7U AKwHZDCrVvyUJjdFgTC4fr1PyLPyP0Q92LDSA82Bqw3l5rxoNXOSnc/knHZq1ro2Q2T9M pMKdz354fOmOCrAAWMHD6Uy//KqNCGyAu3B8BTwHLa8+ImozrB56rYlnGECbdiki10w== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795791717; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=wL/VA5+pGxzKnAxonRgHumCPvJPgs/jaOjXoLgiZ+Vw=; b=m9c8LX9VxI8tcVd12P2+wXKlJIiZSsBMWNI3Wigzyyh8H5dz0JAn/Hiz1CR+86OTykUQ CJZdK2ICwE0gyCV+61yBRO7dENdouiV608BhmkutJvWIPuBy9uZkYXhHjfWeP2UqqhEBB xvfa70ZibX9WhbgjoNDy2+lb7HeaxhafIRUA0lm4tN0I5GQokky02GiKFUfk9MILTYhTF dqisVZWDF6phSao14mPo5sDkQrOvnU1pSOrCrW+20M1NTJjlle5otqN8wOmSWTc9VJ05p RzOcTh4oZ2LuJiaa++VhPDA8V89LQfbHV3zSmnhhFEmGAQ+htYDMG1Am/FFibOUhDbA== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ddSTOj3O; arc=none smtp.remote-ip=100.103.45.18 X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62392606A32C; Fri, 27 Nov 2026 15:01:57 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 66BCC1F00923; Fri, 27 Nov 2026 15:01:55 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795791716; bh=wL/VA5+pGxzKnAxonRgHumCPvJPgs/jaOjXoLgiZ+Vw=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=ddSTOj3OxVWqZmub1TD+JsvwA2uhTOSggOX01VbFvjJ+uRln8f9Ij/mHNAkl48mdo Z7qSr4g+S+pVfmfjH8iIVDgy5So3+lHZavJV1RxUfY7GbdSQGjRFlG6bIIQjgG/GSJ Lu4ekBtjzRCup+QmTA2wT05AqxhmOEz/qaAQMVPLls8C6kG17YBv2Sc44vAIaYL0vf Xt7FIIP9v13zRScpU8IixSPq2hfj3ZPIPMVJPfaJ58kR8Ei4LMxkMV6heuLBX2symV nhfYALkYNSjPzfitYeTCRbhS2X9M6+e/kFo5k6WK3+YIKC4FNhDD96y9SpWME5SZg8 ZmTgTqRD09nsg== X-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 X-In-Reply-To: <20261126-exp-phy-rs-maybelocked-v4-1-16f9a10bb579@gmail.com> X-sashiko-severity: Medium X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list