diff mbox series

[net-next,v2,3/6] rust: net::phy implement AsRef<kernel::device::Device> trait

Message ID 20240731042136.201327-4-fujita.tomonori@gmail.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series net: phy: add Applied Micro QT2025 PHY driver | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 7 this patch: 7
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 7 maintainers not CCed: aliceryhl@google.com alex.gaynor@gmail.com boqun.feng@gmail.com gary@garyguo.net a.hindborg@samsung.com bjorn3_gh@protonmail.com wedsonaf@gmail.com
netdev/build_clang success Errors and warnings before: 7 this patch: 7
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 7 this patch: 7
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 15 lines checked
netdev/build_clang_rust fail Link
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-07-31--12-00 (tests: 707)

Commit Message

FUJITA Tomonori July 31, 2024, 4:21 a.m. UTC
Implement AsRef<kernel::device::Device> trait for Device. A PHY driver
needs a reference to device::Device to call the firmware API.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
 rust/kernel/net/phy.rs | 9 +++++++++
 1 file changed, 9 insertions(+)

Comments

Alice Ryhl July 31, 2024, 8:38 a.m. UTC | #1
On Wed, Jul 31, 2024 at 6:22 AM FUJITA Tomonori
<fujita.tomonori@gmail.com> wrote:
>
> Implement AsRef<kernel::device::Device> trait for Device. A PHY driver
> needs a reference to device::Device to call the firmware API.
>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> ---
>  rust/kernel/net/phy.rs | 9 +++++++++
>  1 file changed, 9 insertions(+)
>
> diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
> index 99a142348a34..561f0e357f31 100644
> --- a/rust/kernel/net/phy.rs
> +++ b/rust/kernel/net/phy.rs
> @@ -302,6 +302,15 @@ pub fn genphy_read_abilities(&mut self) -> Result {
>      }
>  }
>
> +impl AsRef<kernel::device::Device> for Device {
> +    fn as_ref(&self) -> &kernel::device::Device {
> +        let phydev = self.0.get();
> +        // SAFETY: The struct invariant ensures that we may access
> +        // this field without additional synchronization.
> +        unsafe { kernel::device::Device::as_ref(&mut (*phydev).mdio.dev) }

This is a case where I would recommend use of the `addr_of_mut!`
macro. The type of the mutable reference will be `&mut
bindings::device` without a `Opaque` wrapper around the referent, and
the lack of `Opaque` raises questions about uniqueness guarantees.
Using `addr_of_mut!` sidesteps those questions.

With `addr_of_mut!` and no intermediate mutable reference you may add:

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

Alice
FUJITA Tomonori Aug. 1, 2024, 1:03 a.m. UTC | #2
On Wed, 31 Jul 2024 10:38:11 +0200
Alice Ryhl <aliceryhl@google.com> wrote:

>> +impl AsRef<kernel::device::Device> for Device {
>> +    fn as_ref(&self) -> &kernel::device::Device {
>> +        let phydev = self.0.get();
>> +        // SAFETY: The struct invariant ensures that we may access
>> +        // this field without additional synchronization.
>> +        unsafe { kernel::device::Device::as_ref(&mut (*phydev).mdio.dev) }
> 
> This is a case where I would recommend use of the `addr_of_mut!`
> macro. The type of the mutable reference will be `&mut
> bindings::device` without a `Opaque` wrapper around the referent, and
> the lack of `Opaque` raises questions about uniqueness guarantees.
> Using `addr_of_mut!` sidesteps those questions.
> 
> With `addr_of_mut!` and no intermediate mutable reference you may add:
> 
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>

Understood. I will use addr_of_mut! in v3.

Thanks a lot!
diff mbox series

Patch

diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
index 99a142348a34..561f0e357f31 100644
--- a/rust/kernel/net/phy.rs
+++ b/rust/kernel/net/phy.rs
@@ -302,6 +302,15 @@  pub fn genphy_read_abilities(&mut self) -> Result {
     }
 }
 
+impl AsRef<kernel::device::Device> for Device {
+    fn as_ref(&self) -> &kernel::device::Device {
+        let phydev = self.0.get();
+        // SAFETY: The struct invariant ensures that we may access
+        // this field without additional synchronization.
+        unsafe { kernel::device::Device::as_ref(&mut (*phydev).mdio.dev) }
+    }
+}
+
 /// Defines certain other features this PHY supports (like interrupts).
 ///
 /// These flag values are used in [`Driver::FLAGS`].