Auxiliary bus Rust abstractions
Subsystems (2)
Danilo Krummrich
maintainercover 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); a branch containing the patches, including the dependency can be found in [2].
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
[2] https://web.git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=rust/auxiliary
Danilo Krummrich (4):
rust: types: add Opaque::zeroed
rust: auxiliary: add auxiliary 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 | 311 ++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + rust/kernel/types.rs | 8 + samples/rust/Kconfig | 10 + samples/rust/Makefile | 1 + samples/rust/rust_driver_auxiliary.rs | 99 ++++++++ 10 files changed, 458 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: b28786b190d1ae2df5e6a5181ad78c6f226ea3e1
Signature · 1 line
2.48.1
Danilo Krummrich
maintainerPATCH 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 2/4 rust: auxiliary: add auxiliary 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 | 233 ++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 6 files changed, 261 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 +233 -0
233 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 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
@@ -14,7 +14,7 @@ types::{ARef, ForeignOwnable, Opaque}, ThisModule, }; -use core::ptr::addr_of_mut; +use core::ptr::{addr_of_mut,NonNull };/// An adapter for the registration of auxiliary drivers. pub struct Adapter <T :Driver >(T );
@@ -224,6 +224,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 AsRef <device::Device >for Device {
@@ -231,3 +241,71 @@ fn as_ref(&self) -> &device::Device { &self.0 } } + +/// 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
Danilo Krummrich
maintainerPATCH 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 | 10 +++ samples/rust/Makefile | 1 + samples/rust/rust_driver_auxiliary.rs | 99 +++++++++++++++++++++++++++ 4 files changed, 111 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 +10 -0
@@ -71,6 +71,16 @@ config SAMPLE_RUST_DRIVER_FAUX If unsure, say N. +config SAMPLE_RUST_DRIVER_AUXILIARY + tristate "Auxiliary Driver" + 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
Greg KH
maintainerQuoted 8 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.
Nice!
Quoted 5 lines
This patch series depends on [1] (taken through the nova tree for v6.15); a branch containing the patches, including the dependency can be found in [2].
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
[2] https://web.git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=rust/auxiliary
Do you want this to wait until that lands in 6.15-rc1 or do you want to take this now through the nova tree? Either is fine with me.
thanks,
greg k-h
Danilo Krummrich
maintainerQuoted 12 lines · Greg KH
Quoted 8 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.
Nice!
Quoted 8 lines
Quoted 5 lines
This patch series depends on [1] (taken through the nova tree for v6.15); a branch containing the patches, including the dependency can be found in [2].
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
[2] https://web.git.kernel.org/pub/scm/linux/kernel/git/dakr/linux.git/log/?h=rust/auxiliaryDo you want this to wait until that lands in 6.15-rc1 or do you want to take this now through the nova tree? Either is fine with me.
Thanks! Let's wait for things to land in Linus' tree and give people some time to review.
I will also send a v2 soon, fixing up a soundness issue I'm also working on a fix for platform and PCI.