rust: net: phy: fix off-by-one bit positions in device status accessors
Subsystems (5)
PATCH v6 1/1 rust: net: phy: fix off-by-one bit positions in device status accessors
The hand-written bitfield offsets in is_link_up(), is_autoneg_enabled() and is_autoneg_completed() were correct when the abstraction was
merged: at that time autoneg, link, and autoneg_complete were at bits 13, 14, and 15 of struct phy_device's first bitfield unit. Commit 2796ff1e3dca ("net: phy: add flag is_genphy_driven to struct phy_device") later inserted is_genphy_driven just before autoneg, shifting the three fields up by one, so the accessors now read:
is_link_up() reads bit 14 = autoneg
is_autoneg_enabled() reads bit 13 = is_genphy_driven
is_autoneg_completed() reads bit 15 = link
The official ax88796b Rust driver uses all three accessors in its read_status() implementation, so it inherits the bug. phy_attach_direct() sets is_genphy_driven only when it falls back to the generic driver, and ax88796b has a real driver, so is_genphy_driven stays 0. The broken is_autoneg_enabled() therefore reads bit 13 as 0, compares it against AUTONEG_ENABLE (1), and always returns false, so read_status() never reaches the resolve_aneg_linkmode() call.
The ordinary bindgen accessors take &self. Calling them through (*phydev).link() would create a shared reference to the complete bindings::phy_device, which is not appropriate for an object wrapped in Opaque.
Use the bindgen-generated raw accessors (link_raw(), autoneg_raw(), and autoneg_complete_raw()) instead. They retain the bit positions and endianness handling generated from the C layout without creating a Rust reference to the complete phy_device. Drop the hand-written numbers together with the TODO comment that marked them as a stopgap.
The raw accessors are only emitted by bindgen 0.71 and later, and were added at the Rust-for-Linux project's request, so this fix can only be backported to stable branches whose minimum bindgen version is at least that, hence the scope on the Cc: stable line below.
Found by a static equivalence audit (C2RustDrv, a C-to-Rust driver migration tool) that compares hand-written bitfield offsets against the bindgen layout of struct phy_device. Verified by building the bindings and checking the generated accessors; no runtime testing was possible without PHY hardware.
---
v6:
- Name read_status() instead of the vague "link-change handling" in the commit message (FUJITA Tomonori).
- Correct the impact description: on ax88796b, which has its own driver, is_genphy_driven stays 0, so the broken is_autoneg_enabled() always returns false and read_status() never reaches the resolve_aneg_linkmode() call (FUJITA Tomonori).
- Rewrite the SAFETY comments to state why the raw accessors may be
called: the pointer is valid and there is no concurrent write to the field (FUJITA Tomonori).
v5: https://lore.kernel.org/r/[email protected]/
- Move the comparison out of the unsafe block so that only the raw accessor call stays inside (Gary Guo, Miguel Ojeda).
- Scope the Cc: stable backport to 7.1.y and later, since the raw accessors require bindgen 0.71 (Miguel Ojeda).
- Mention the bindgen 0.71 dependency in the commit message.
v4: https://lore.kernel.org/r/[email protected]/
- Use the bindgen-generated raw accessors to avoid creating a shared reference to the complete C phy_device, as suggested by Jakub Kicinski.
- Update the SAFETY comments to describe pointer validity and the callback-context preconditions.
- Add #[inline] to the three small forwarding methods.
v3: https://lore.kernel.org/r/[email protected]/
- Use the bindgen-generated accessors instead of hard-coded bit numbers.
- Update the Fixes tag and add the net tree subject prefix.
v2: https://lore.kernel.org/r/[email protected]/
- Use bindgen-generated accessors as suggested by Andrew Lunn.
- Explain that the offsets drifted after is_genphy_driven was added.
v1: https://lore.kernel.org/r/[email protected]/
rust/kernel/net/phy.rs | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-)
rust/kernel/net/phy.rs +18 -20
@@ -123,39 +123,37 @@ pub fn state(&self) -> DeviceState {/// Gets the current link state. /// /// It returns true if the link is up. +#[inline] pub fn is_link_up (&self) ->bool { -const LINK_IS_UP :u64 =1 ; -// TODO: the code to access to the bit field will be replaced with automatically -// generated code by bindgen when it becomes possible. -// SAFETY: The struct invariant ensures that we may access -// this field without additional synchronization. -let bit_field =unsafe { &(*self.0 .get ())._bitfield_1 }; - bit_field.get (14 ,1 ) ==LINK_IS_UP +let phydev = self.0 .get ().cast_const (); +// SAFETY: By the type invariant of `Device`, `phydev` points to a valid +// `struct phy_device`, and there is no concurrent write to this field. +let link =unsafe { bindings::phy_device::link_raw (phydev) }; + link ==1 }/// Gets the current auto-negotiation configuration. /// /// It returns true if auto-negotiation is enabled. +#[inline] pub fn is_autoneg_enabled (&self) ->bool { -// TODO: the code to access to the bit field will be replaced with automatically -// generated code by bindgen when it becomes possible. -// SAFETY: The struct invariant ensures that we may access -// this field without additional synchronization. -let bit_field =unsafe { &(*self.0 .get ())._bitfield_1 }; - bit_field.get (13 ,1 ) == u64::from (bindings::AUTONEG_ENABLE ) +let phydev = self.0 .get ().cast_const (); +// SAFETY: By the type invariant of `Device`, `phydev` points to a valid +// `struct phy_device`, and there is no concurrent write to this field. +let autoneg =unsafe { bindings::phy_device::autoneg_raw (phydev) }; + autoneg == bindings::AUTONEG_ENABLE }/// Gets the current auto-negotiation state. /// /// It returns true if auto-negotiation is completed. +#[inline] pub fn is_autoneg_completed (&self) ->bool { -const AUTONEG_COMPLETED :u64 =1 ; -// TODO: the code to access to the bit field will be replaced with automatically -// generated code by bindgen when it becomes possible. -// SAFETY: The struct invariant ensures that we may access -// this field without additional synchronization. -let bit_field =unsafe { &(*self.0 .get ())._bitfield_1 }; - bit_field.get (15 ,1 ) ==AUTONEG_COMPLETED +let phydev = self.0 .get ().cast_const (); +// SAFETY: By the type invariant of `Device`, `phydev` points to a valid +// `struct phy_device`, and there is no concurrent write to this field. +let completed =unsafe { bindings::phy_device::autoneg_complete_raw (phydev) }; + completed ==1 }/// Sets the speed of the PHY.
base-commit: 2709dd5ae32f0828f386327c76bba9f39f63a1c6
Signature · 1 line
2.43.0
Quoted 43 lines · On Thu, 10 Sep 2026 05:51:10 +0000 Chunfeng Song <[email protected]>…
The hand-written bitfield offsets in
is_link_up(),is_autoneg_enabled()andis_autoneg_completed()were correct when the abstraction was
merged: at that time autoneg, link, andautoneg_completewere at bits 13, 14, and 15 of struct phy_device's first bitfield unit. Commit2796ff1e3dca("net: phy: add flagis_genphy_drivento structphy_device") later insertedis_genphy_drivenjust before autoneg, shifting the three fields up by one, so the accessors now read:
is_link_up()reads bit 14 = autoneg
is_autoneg_enabled()reads bit 13 =is_genphy_driven
is_autoneg_completed()reads bit 15 = linkThe official ax88796b Rust driver uses all three accessors in its
read_status()implementation, so it inherits the bug.phy_attach_direct()setsis_genphy_drivenonly when it falls back to the generic driver, and ax88796b has a real driver, sois_genphy_drivenstays 0. The brokenis_autoneg_enabled()therefore reads bit 13 as 0, compares it againstAUTONEG_ENABLE(1), and always returns false, soread_status()never reaches theresolve_aneg_linkmode()call.The ordinary bindgen accessors take &self. Calling them through (*phydev).link() would create a shared reference to the complete
bindings::phy_device, which is not appropriate for an object wrapped in Opaque.Use the bindgen-generated raw accessors (
link_raw(),autoneg_raw(), andautoneg_complete_raw()) instead. They retain the bit positions and endianness handling generated from the C layout without creating a Rust reference to the completephy_device. Drop the hand-written numbers together with the TODO comment that marked them as a stopgap.The raw accessors are only emitted by bindgen 0.71 and later, and were added at the Rust-for-Linux project's request, so this fix can only be backported to stable branches whose minimum bindgen version is at least that, hence the scope on the Cc: stable line below.
Found by a static equivalence audit (C2RustDrv, a C-to-Rust driver migration tool) that compares hand-written bitfield offsets against the bindgen layout of struct
phy_device. Verified by building the bindings and checking the generated accessors; no runtime testing was possible without PHY hardware.
Quoted 4 lines
Fixes 2796ff1e3dca ("net: phy: add flag is_genphy_driven to struct phy_device")Cc [email protected] # Only 7.1.y and later (requires bindgen's raw pointer accessors).Signed-off-by Chunfeng Song <[email protected]>
Thanks,
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Thu, 10 Sep 2026 05:51:10 +0000 you wrote:
The hand-written bitfield offsets in
is_link_up(),is_autoneg_enabled()andis_autoneg_completed()were correct when the abstraction was
merged: at that time autoneg, link, andautoneg_completewere at bits 13, 14, and 15 of struct phy_device's first bitfield unit. Commit2796ff1e3dca("net: phy: add flagis_genphy_drivento structphy_device") later insertedis_genphy_drivenjust before autoneg, shifting the three fields up by one, so the accessors now read:[...]
Here is the summary with links:
- [net,v6] rust: net: phy: fix off-by-one bit positions in device status accessors
https://git.kernel.org/netdev/net/c/6fb0a9d9071f
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html