Auxiliary bus Rust abstractions
Subsystems (2)
Danilo Krummrich
maintainerv2 cover Auxiliary bus Rust abstractions
This series adds Rust abstractions for the auxiliary bus.
It implements the required abstractions to write auxiliary drivers and create auxiliary device registrations. A driver sample illustrates how the corresponding abstractions work.
The auxiliary abstractions are required for the Nova driver project, in order to connect nova-core with the nova-drm driver.
This patch series depends on [1] (taken through the nova tree for v6.15) and [2]; a branch containing the patches, including the dependency can be found in [3].
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://web.git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=rust/auxiliary
Changes in v2:
- implement bus device soundness improvements from [2]
Danilo Krummrich (4):
rust: types: add Opaque::zeroed
rust: auxiliary: add auxiliary device / driver abstractions
rust: auxiliary: add auxiliary registration
samples: rust: add Rust auxiliary driver sample
MAINTAINERS | 2 + rust/bindings/bindings_helper.h | 1 + rust/helpers/auxiliary.c | 23 ++ rust/helpers/helpers.c | 1 + rust/kernel/auxiliary.rs | 345 ++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + rust/kernel/types.rs | 8 + samples/rust/Kconfig | 12 + samples/rust/Makefile | 1 + samples/rust/rust_driver_auxiliary.rs | 99 ++++++++ 10 files changed, 494 insertions(+) create mode 100644 rust/helpers/auxiliary.c create mode 100644 rust/kernel/auxiliary.rs create mode 100644 samples/rust/rust_driver_auxiliary.rs
base-commit: 31cc40e864273b384f4e640ba3f11add432b78e0
Signature · 1 line
2.48.1
Danilo Krummrich
maintainerPATCH v2 1/4 rust: types: add `Opaque::zeroed`
Analogous to Opaque::uninit add Opaque::zeroed, which sets the corresponding memory to zero. In contrast to Opaque::uninit, the corresponding value, depending on its type, may be initialized.
--- rust/kernel/types.rs | 8 ++++++++ 1 file changed, 8 insertions(+)
rust/kernel/types.rs +8 -0
@@ -326,6 +326,14 @@ pub const fn uninit() -> Self { } } +/// Creates a new zeroed opaque value. +pub const fn zeroed () ->Self { +Self { + value:UnsafeCell ::new (MaybeUninit ::zeroed ()), + _pin:PhantomPinned , + } + } +/// Create an opaque pin-initializer from the given pin-initializer. pub fn pin_init (slot:impl PinInit <T >) ->impl PinInit <Self > {Self ::ffi_init (|ptr: *mut T | {
Signature · 1 line
2.48.1
Danilo Krummrich
maintainerPATCH v2 2/4 rust: auxiliary: add auxiliary device / driver abstractions
In reply to Danilo KrummrichImplement the basic auxiliary abstractions required to implement a driver matching an auxiliary device.
The design and implementation is analogous to PCI and platform and is based on the generic device / driver abstractions.
--- MAINTAINERS | 1 + rust/bindings/bindings_helper.h | 1 + rust/helpers/auxiliary.c | 23 +++ rust/helpers/helpers.c | 1 + rust/kernel/auxiliary.rs | 267 ++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 6 files changed, 295 insertions(+) create mode 100644 rust/helpers/auxiliary.c create mode 100644 rust/kernel/auxiliary.rs
MAINTAINERS +1 -0
@@ -7114,6 +7114,7 @@ F: include/linux/kobj* F: include/linux/property.h F: include/linux/sysfs.h F: lib/kobj* +F: rust/kernel/auxiliary.rs F: rust/kernel/device.rs F: rust/kernel/device_id.rs F: rust/kernel/devres.rs
rust/bindings/bindings_helper.h +1 -0
@@ -7,6 +7,7 @@*/ #include <kunit/test.h> +#include <linux/auxiliary_bus.h> #include <linux/blk-mq.h> #include <linux/blk_types.h> #include <linux/blkdev.h>
rust/helpers/auxiliary.c new file +23 -0
@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/auxiliary_bus.h> + +void rust_helper_auxiliary_set_drvdata (struct auxiliary_device *adev,void *data) +{ +auxiliary_set_drvdata (adev, data); +} + +void *rust_helper_auxiliary_get_drvdata (struct auxiliary_device *adev) +{ +return auxiliary_get_drvdata (adev); +} + +void rust_helper_auxiliary_device_uninit (struct auxiliary_device *adev) +{ +return auxiliary_device_uninit (adev); +} + +void rust_helper_auxiliary_device_delete (struct auxiliary_device *adev) +{ +return auxiliary_device_delete (adev); +}
rust/helpers/helpers.c +1 -0
@@ -7,6 +7,7 @@* Sorted alphabetically. */ +#include "auxiliary.c" #include "blk.c" #include "bug.c" #include "build_assert.c"
rust/kernel/auxiliary.rs new file +267 -0
267 lines not on this page · raw message
rust/kernel/lib.rs +2 -0
@@ -36,6 +36,8 @@pub use ffi;pub mod alloc; +#[cfg( CONFIG_AUXILIARY_BUS )] +pub mod auxiliary;#[cfg( CONFIG_BLOCK )] pub mod block;#[doc(hidden)]
Signature · 1 line
2.48.1
Danilo Krummrich
maintainerPATCH v2 3/4 rust: auxiliary: add auxiliary registration
In reply to Danilo KrummrichImplement the auxiliary::Registration type, which provides an API to create and register new auxiliary devices in the system.
--- rust/kernel/auxiliary.rs | 80 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-)
rust/kernel/auxiliary.rs +79 -1
@@ -5,7 +5,7 @@//! C header: [`include/linux/auxiliary_bus.h`](srctree/include/linux/auxiliary_bus.h) use crate ::{ - bindings, device, + bindings, container_of, device, device_id::RawDeviceId , driver, error::{to_result,Result },
@@ -219,6 +219,16 @@ pub fn id(&self) -> u32 {// `struct auxiliary_device`. unsafe { (*self.as_raw ()).id } } + +extern "C" fn release (dev: *mut bindings::device ) { +// SAFETY: By the type invariant `self.0.as_raw` is a pointer to the `struct device` +// embedded in `struct auxiliary_device`. +let adev =unsafe {container_of! (dev, bindings::auxiliary_device, dev) }.cast_mut (); + +// SAFETY: `adev` points to the memory that has been allocated in `Registration::new`, via +// `KBox::new(Opaque::<bindings::auxiliary_device>::zeroed(), GFP_KERNEL)`. +let _ =unsafe {KBox ::<Opaque <bindings::auxiliary_device >>::from_raw (adev.cast ()) }; + } }impl Deref for Device <device::Core > {
@@ -265,3 +275,71 @@ fn as_ref(&self) -> &device::Device {unsafe { device::Device ::as_ref (dev) } } } + +/// The registration of an auxiliary device. +/// +/// This type represents the registration of a [`struct auxiliary_device`]. When an instance of this +/// type is dropped, its respective auxiliary device will be unregistered from the system. +/// +/// # Invariants +/// +/// `self.0` always holds a valid pointer to an initialized and registered +/// [`struct auxiliary_device`]. +pub struct Registration (NonNull <bindings::auxiliary_device >); + +impl Registration { +/// Create and register a new auxiliary device. +pub fn new (parent: &device::Device , name: &CStr , id:u32 , modname: &CStr ) ->Result <Self > { +let boxed =KBox ::new (Opaque ::<bindings::auxiliary_device >::zeroed (),GFP_KERNEL )?; +let adev = boxed.get (); + +// SAFETY: It's safe to set the fields of `struct auxiliary_device` on initialization. +unsafe { + (*adev).dev.parent = parent.as_raw (); + (*adev).dev.release =Some (Device ::release); + (*adev).name = name.as_char_ptr (); + (*adev).id = id; + } + +// SAFETY: `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`, +// which has not been initialized yet. +unsafe { bindings::auxiliary_device_init (adev) }; + +// Now that `adev` is initialized, leak the `Box`; the corresponding memory will be freed +// by `Device::release` when the last reference to the `struct auxiliary_device` is dropped. +let _ =KBox ::into_raw (boxed); + +// SAFETY: +// - `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`, which has +// been initialialized, +// - `modname.as_char_ptr()` is a NULL terminated string. +let ret =unsafe { bindings::__auxiliary_device_add (adev, modname.as_char_ptr ()) }; +if ret !=0 { +// SAFETY: `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`, +// which has been initialialized. +unsafe { bindings::auxiliary_device_uninit (adev) }; + +return Err (Error ::from_errno (ret)); + } + +// SAFETY: `adev` is guaranteed to be non-null, since the `KBox` was allocated successfully. +// +// INVARIANT: The device will remain registered until `auxiliary_device_delete()` is called, +// which happens in `Self::drop()`. +Ok (Self (unsafe {NonNull ::new_unchecked (adev) })) + } +} + +impl Drop for Registration { +fn drop (&mut self) { +// SAFETY: By the type invariant of `Self`, `self.0.as_ptr()` is a valid registered +// `struct auxiliary_device`. +unsafe { bindings::auxiliary_device_delete (self.0 .as_ptr ()) }; + +// This drops the reference we acquired through `auxiliary_device_init()`. +// +// SAFETY: By the type invariant of `Self`, `self.0.as_ptr()` is a valid registered +// `struct auxiliary_device`. +unsafe { bindings::auxiliary_device_uninit (self.0 .as_ptr ()) }; + } +}
Signature · 1 line
2.48.1
Re: [PATCH v2 3/4] rust: auxiliary: add auxiliary registration
Quoted 48 lines · On Thu, 13 Mar 2025 03:23:52 +0100 Danilo Krummrich <[email protected]> wrote:
Implement the
auxiliary::Registrationtype, which provides an API to create and register new auxiliary devices in the system.Signed-off-by Danilo Krummrich <[email protected]>--- rust/kernel/auxiliary.rs | 80 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-)rust/kernel/auxiliary.rs +17
-1@@ -5,7 +5,7 @@//! C header: [`include/linux/auxiliary_bus.h`](srctree/include/linux/auxiliary_bus.h) use crate ::{ - bindings, device, + bindings, container_of, device, device_id::RawDeviceId , driver, error::{to_result,Result },@@ -219,6 +219,16 @@ pub fn id(&self) -> u32 {// `struct auxiliary_device`. unsafe { (*self.as_raw ()).id } } + +extern "C" fn release (dev: *mut bindings::device ) { +// SAFETY: By the type invariant `self.0.as_raw` is a pointer to the `struct device` +// embedded in `struct auxiliary_device`. +let adev =unsafe {container_of! (dev, bindings::auxiliary_device, dev) }.cast_mut (); + +// SAFETY: `adev` points to the memory that has been allocated in `Registration::new`, via +// `KBox::new(Opaque::<bindings::auxiliary_device>::zeroed(), GFP_KERNEL)`. +let _ =unsafe {KBox ::<Opaque <bindings::auxiliary_device >>::from_raw (adev.cast ()) }; + } }impl Deref for Device <device::Core > {@@ -265,3 +275,71 @@ fn as_ref(&self) -> &device::Device {unsafe { device::Device ::as_ref (dev) } } } + +/// The registration of an auxiliary device. +/// +/// This type represents the registration of a [`struct auxiliary_device`]. When an instance of this +/// type is dropped, its respective auxiliary device will be unregistered from the system. +///
rust/kernel/auxiliary.rs fn as_ref(&self) -> &device::Device { · line 293
Quoted 10 lines
rust/kernel/auxiliary.rs +10
-0+/// # Invariants +/// +/// `self.0` always holds a valid pointer to an initialized and registered +/// [`struct auxiliary_device`]. +pub struct Registration (NonNull <bindings::auxiliary_device >); + +impl Registration { +/// Create and register a new auxiliary device. +pub fn new (parent: &device::Device , name: &CStr , id:u32 , modname: &CStr ) ->Result <Self > { +let boxed =KBox ::new (Opaque ::<bindings::auxiliary_device >::zeroed (),GFP_KERNEL )?;
You can use KBox::init(kernel::init::zeroed(), GFP_KERNEL) here. It avoids the need for the first patch.
Quoted 56 lines
rust/kernel/auxiliary.rs +52
-0+let adev = boxed.get (); + +// SAFETY: It's safe to set the fields of `struct auxiliary_device` on initialization. +unsafe { + (*adev).dev.parent = parent.as_raw (); + (*adev).dev.release =Some (Device ::release); + (*adev).name = name.as_char_ptr (); + (*adev).id = id; + } + +// SAFETY: `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`, +// which has not been initialized yet. +unsafe { bindings::auxiliary_device_init (adev) }; + +// Now that `adev` is initialized, leak the `Box`; the corresponding memory will be freed +// by `Device::release` when the last reference to the `struct auxiliary_device` is dropped. +let _ =KBox ::into_raw (boxed); + +// SAFETY: +// - `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`, which has +// been initialialized, +// - `modname.as_char_ptr()` is a NULL terminated string. +let ret =unsafe { bindings::__auxiliary_device_add (adev, modname.as_char_ptr ()) }; +if ret !=0 { +// SAFETY: `adev` is guaranteed to be a valid pointer to a `struct auxiliary_device`, +// which has been initialialized. +unsafe { bindings::auxiliary_device_uninit (adev) }; + +return Err (Error ::from_errno (ret)); + } + +// SAFETY: `adev` is guaranteed to be non-null, since the `KBox` was allocated successfully. +// +// INVARIANT: The device will remain registered until `auxiliary_device_delete()` is called, +// which happens in `Self::drop()`. +Ok (Self (unsafe {NonNull ::new_unchecked (adev) })) + } +} + +impl Drop for Registration { +fn drop (&mut self) { +// SAFETY: By the type invariant of `Self`, `self.0.as_ptr()` is a valid registered +// `struct auxiliary_device`. +unsafe { bindings::auxiliary_device_delete (self.0 .as_ptr ()) }; + +// This drops the reference we acquired through `auxiliary_device_init()`. +// +// SAFETY: By the type invariant of `Self`, `self.0.as_ptr()` is a valid registered +// `struct auxiliary_device`. +unsafe { bindings::auxiliary_device_uninit (self.0 .as_ptr ()) }; + } +}Signature · 1 line
2.48.1
Benoît du Garreau
Danilo Krummrich
maintainerRe: [PATCH v2 3/4] rust: auxiliary: add auxiliary registration
Quoted 9 lines · Benoît du Garreau
rust/kernel/auxiliary.rs fn as_ref(&self) -> &device::Device { · line 293
Quoted 4 lines · On Thu, 13 Mar 2025 03:23:52 +0100 Danilo Krummrich <[email protected]> wrote:
rust/kernel/auxiliary.rs +4
-0+impl Registration { +/// Create and register a new auxiliary device. +pub fn new (parent: &device::Device , name: &CStr , id:u32 , modname: &CStr ) ->Result <Self > { +let boxed =KBox ::new (Opaque ::<bindings::auxiliary_device >::zeroed (),GFP_KERNEL )?;You can use
KBox::init(kernel::init::zeroed(), GFP_KERNEL)here. It avoids the need for the first patch.
You're right, that works indeed, the full call looks like this.
let boxed = KBox::init(kernel::init::zeroed::<Opaque::<bindings::auxiliary_device>>(), GFP_KERNEL)?;
However, I think Opaque::zeroed() reads a bit better. Unless anything speaks against adding it, I think I prefer it as it is.
Re: [PATCH v2 3/4] rust: auxiliary: add auxiliary registration
In reply to Benoît du GarreauQuoted 9 lines · Benoît du Garreau
rust/kernel/auxiliary.rs fn as_ref(&self) -> &device::Device { · line 293
Quoted 5 lines · On Thu, 13 Mar 2025 03:23:52 +0100 Danilo Krummrich <[email protected]> wrote:
Implement the
auxiliary::Registrationtype, which provides an API torust/kernel/auxiliary.rs +4
-0+impl Registration { +/// Create and register a new auxiliary device. +pub fn new (parent: &device::Device , name: &CStr , id:u32 , modname: &CStr ) ->Result <Self > { +let boxed =KBox ::new (Opaque ::<bindings::auxiliary_device >::zeroed (),GFP_KERNEL )?;You can use
KBox::init(kernel::init::zeroed(), GFP_KERNEL)here. It avoids the need for the first patch.
We probably should have the zeroed function on the Zeroable trait...
---
Cheers,
Benno
Danilo Krummrich
maintainerPATCH v2 4/4 samples: rust: add Rust auxiliary driver sample
In reply to Danilo KrummrichAdd a sample Rust auxiliary driver based on a PCI driver for QEMU's "pci-testdev" device.
The PCI driver only registers an auxiliary device, in order to make the corresponding auxiliary driver probe.
--- MAINTAINERS | 1 + samples/rust/Kconfig | 12 ++++ samples/rust/Makefile | 1 + samples/rust/rust_driver_auxiliary.rs | 99 +++++++++++++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 samples/rust/rust_driver_auxiliary.rs
MAINTAINERS +1 -0
@@ -7121,6 +7121,7 @@ F: rust/kernel/devres.rs F: rust/kernel/driver.rs F: rust/kernel/faux.rs F: rust/kernel/platform.rs +F: samples/rust/rust_driver_auxiliary.rs F: samples/rust/rust_driver_platform.rs F: samples/rust/rust_driver_faux.rs
samples/rust/Kconfig +12 -0
@@ -71,6 +71,18 @@ config SAMPLE_RUST_DRIVER_FAUX If unsure, say N. +config SAMPLE_RUST_DRIVER_AUXILIARY + tristate "Auxiliary Driver" + depends on AUXILIARY_BUS + depends on PCI + help + This option builds the Rust auxiliary driver sample. + + To compile this as a module, choose M here: + the module will be called rust_driver_auxiliary. + + If unsure, say N. + config SAMPLE_RUST_HOSTPROGS bool "Host programs" help
samples/rust/Makefile +1 -0
@@ -7,6 +7,7 @@ obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) += rust_driver_pci.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) += rust_driver_faux.o +obj-$(CONFIG_SAMPLE_RUST_DRIVER_AUXILIARY) += rust_driver_auxiliary.o rust_print-y := rust_print_main.o rust_print_events.o
samples/rust/rust_driver_auxiliary.rs new file +99 -0
99 lines not on this page · raw message
Signature · 1 line
2.48.1
Quoted 9 lines · Danilo Krummrich
This series adds Rust abstractions for the auxiliary bus.
It implements the required abstractions to write auxiliary drivers and create auxiliary device registrations. A driver sample illustrates how the corresponding abstractions work.
The auxiliary abstractions are required for the Nova driver project, in order to connect nova-core with the nova-drm driver.
Quoted 7 lines
This patch series depends on [1] (taken through the nova tree for v6.15) and [2]; a branch containing the patches, including the dependency can be found in [3].
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://web.git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=rust/auxiliary
As this seems to depend on different branches in different trees, I'll hold off on applying these to my tree now. But if you want to take them sooner (i.e. before 6.15-rc1), that's fine with me:
Danilo Krummrich
maintainerQuoted 18 lines · Greg KH
Quoted 9 lines · Danilo Krummrich
This series adds Rust abstractions for the auxiliary bus.
It implements the required abstractions to write auxiliary drivers and create auxiliary device registrations. A driver sample illustrates how the corresponding abstractions work.
The auxiliary abstractions are required for the Nova driver project, in order to connect nova-core with the nova-drm driver.
Quoted 7 lines
This patch series depends on [1] (taken through the nova tree for v6.15) and [2]; a branch containing the patches, including the dependency can be found in [3].
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://web.git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=rust/auxiliary
Quoted 5 lines
As this seems to depend on different branches in different trees, I'll hold off on applying these to my tree now. But if you want to take them sooner (i.e. before 6.15-rc1), that's fine with me:
Thanks! I wouldn't take them for the upcoming merge window, but maybe for the next one through the nova tree.