rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 0/3] Implement TryFrom<&Device> for bus specific devices
@ 2025-03-21 21:47 Danilo Krummrich
  2025-03-21 21:47 ` [PATCH v4 1/3] rust: device: implement bus_type_raw() Danilo Krummrich
                   ` (3 more replies)
  0 siblings, 4 replies; 20+ messages in thread
From: Danilo Krummrich @ 2025-03-21 21:47 UTC (permalink / raw)
  To: bhelgaas, gregkh, rafael, ojeda, alex.gaynor, boqun.feng, gary,
	bjorn3_gh, benno.lossin, a.hindborg, aliceryhl, tmgross
  Cc: linux-pci, rust-for-linux, linux-kernel, Danilo Krummrich

This series provides a mechanism to safely convert a struct device into its
corresponding bus specific device instance, if any.

In C a generic struct device is typically converted to a specific bus device
with container_of(). This requires the caller to know whether the generic struct
device is indeed embedded within the expected bus specific device type.

In Rust we can do the same thing by implementing the TryFrom trait, e.g.

        impl TryFrom<&Device> for pci::Device

This is a safe operation, since we can check whether dev->bus equals the the
expected struct bus_type.

A branch containing the patches can be found in [1].

This is needed for the auxiliary bus abstractions and connecting nova-core with
nova-drm. [2]

[1] https://web.git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=rust/device
[2] https://gitlab.freedesktop.org/drm/nova/-/tree/staging/nova-drm

Changes in v4:
  - work around a minor issue in rustc < 1.82
    - https://blog.rust-lang.org/2024/10/17/Rust-1.82.0.html#safely-addressing-unsafe-statics

Changes in v3:
  - drop patch to add Device::parent(), will make it crate private and part of
    the auxbus series
  - safety comment: clarify that a device' bus type is guaranteed to be set
    correctly by the corresponding C code

Changes in v2:
  - s/unsafe { *self.as_raw() }.parent/unsafe { (*self.as_raw()).parent }/
  - expand safety comment on Device::bus_type_raw()

Danilo Krummrich (3):
  rust: device: implement bus_type_raw()
  rust: pci: impl TryFrom<&Device> for &pci::Device
  rust: platform: impl TryFrom<&Device> for &platform::Device

 rust/kernel/device.rs   |  9 +++++++++
 rust/kernel/pci.rs      | 25 +++++++++++++++++++++++--
 rust/kernel/platform.rs | 25 +++++++++++++++++++++++--
 3 files changed, 55 insertions(+), 4 deletions(-)


base-commit: 51d0de7596a458096756c895cfed6bc4a7ecac10
-- 
2.48.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2025-04-19 12:57 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-21 21:47 [PATCH v4 0/3] Implement TryFrom<&Device> for bus specific devices Danilo Krummrich
2025-03-21 21:47 ` [PATCH v4 1/3] rust: device: implement bus_type_raw() Danilo Krummrich
2025-03-22  3:10   ` Greg KH
2025-03-21 21:47 ` [PATCH v4 2/3] rust: pci: impl TryFrom<&Device> for &pci::Device Danilo Krummrich
2025-03-22  3:25   ` Greg KH
2025-03-22 10:10     ` Danilo Krummrich
2025-03-23 22:10       ` Danilo Krummrich
2025-03-24 13:54         ` Greg KH
2025-03-24 17:05           ` Danilo Krummrich
2025-03-24 16:39         ` Benno Lossin
2025-03-24 16:49           ` Danilo Krummrich
2025-03-24 17:36             ` Benno Lossin
2025-03-24 18:13               ` Danilo Krummrich
2025-03-24 18:32                 ` Benno Lossin
2025-04-01 13:51                   ` Danilo Krummrich
2025-04-02  0:05                     ` Benno Lossin
2025-04-02  9:06                       ` Danilo Krummrich
2025-03-21 21:47 ` [PATCH v4 3/3] rust: platform: impl TryFrom<&Device> for &platform::Device Danilo Krummrich
2025-03-22  3:25   ` Greg KH
2025-04-19 12:57 ` [PATCH v4 0/3] Implement TryFrom<&Device> for bus specific devices Danilo Krummrich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).