Introduce an internal device context, which is semantically equivalent
to the Core device context, but reserved for bus abstractions.
This allows implementing methods for the Device type, which are limited
to be used within the core context of bus abstractions, i.e. restrict
the availability for drivers.
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
rust/kernel/device.rs | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index 665f5ceadecc..e9094d8322d5 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -261,6 +261,10 @@ pub trait DeviceContext: private::Sealed {}
/// any of the bus callbacks, such as `probe()`.
pub struct Core;
+/// Semantically the same as [`Core`] but reserved for internal usage of the corresponding bus
+/// abstraction.
+pub struct Internal;
+
/// The [`Bound`] context is the context of a bus specific device reference when it is guaranteed to
/// be bound for the duration of its lifetime.
pub struct Bound;
@@ -270,11 +274,13 @@ pub trait Sealed {}
impl Sealed for super::Bound {}
impl Sealed for super::Core {}
+ impl Sealed for super::Internal {}
impl Sealed for super::Normal {}
}
impl DeviceContext for Bound {}
impl DeviceContext for Core {}
+impl DeviceContext for Internal {}
impl DeviceContext for Normal {}
/// # Safety
@@ -312,6 +318,13 @@ fn deref(&self) -> &Self::Target {
#[macro_export]
macro_rules! impl_device_context_deref {
(unsafe { $device:ident }) => {
+ // SAFETY: This macro has the exact same safety requirement as
+ // `__impl_device_context_deref!`.
+ ::kernel::__impl_device_context_deref!(unsafe {
+ $device,
+ $crate::device::Internal => $crate::device::Core
+ });
+
// SAFETY: This macro has the exact same safety requirement as
// `__impl_device_context_deref!`.
::kernel::__impl_device_context_deref!(unsafe {
@@ -345,6 +358,7 @@ fn from(dev: &$device<$src>) -> Self {
#[macro_export]
macro_rules! impl_device_context_into_aref {
($device:tt) => {
+ ::kernel::__impl_device_context_into_aref!($crate::device::Internal, $device);
::kernel::__impl_device_context_into_aref!($crate::device::Core, $device);
::kernel::__impl_device_context_into_aref!($crate::device::Bound, $device);
};
--
2.49.0
On Sat, Jun 21, 2025 at 09:43:27PM +0200, Danilo Krummrich wrote: > Introduce an internal device context, which is semantically equivalent > to the Core device context, but reserved for bus abstractions. > > This allows implementing methods for the Device type, which are limited > to be used within the core context of bus abstractions, i.e. restrict > the availability for drivers. > > Signed-off-by: Danilo Krummrich <dakr@kernel.org> > --- > rust/kernel/device.rs | 14 ++++++++++++++ > 1 file changed, 14 insertions(+) > > diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs > index 665f5ceadecc..e9094d8322d5 100644 > --- a/rust/kernel/device.rs > +++ b/rust/kernel/device.rs > @@ -261,6 +261,10 @@ pub trait DeviceContext: private::Sealed {} > /// any of the bus callbacks, such as `probe()`. > pub struct Core; > > +/// Semantically the same as [`Core`] but reserved for internal usage of the corresponding bus > +/// abstraction. > +pub struct Internal; Naming is hard :) As this is ONLY for the bus code to touch, why not call it Bus_Internal? And can a driver touch this, or only the bus owner? thanks, greg k-h
On Tue, Jul 01, 2025 at 11:26:47AM +0200, Greg KH wrote: > On Sat, Jun 21, 2025 at 09:43:27PM +0200, Danilo Krummrich wrote: > > Introduce an internal device context, which is semantically equivalent > > to the Core device context, but reserved for bus abstractions. > > > > This allows implementing methods for the Device type, which are limited > > to be used within the core context of bus abstractions, i.e. restrict > > the availability for drivers. > > > > Signed-off-by: Danilo Krummrich <dakr@kernel.org> > > --- > > rust/kernel/device.rs | 14 ++++++++++++++ > > 1 file changed, 14 insertions(+) > > > > diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs > > index 665f5ceadecc..e9094d8322d5 100644 > > --- a/rust/kernel/device.rs > > +++ b/rust/kernel/device.rs > > @@ -261,6 +261,10 @@ pub trait DeviceContext: private::Sealed {} > > /// any of the bus callbacks, such as `probe()`. > > pub struct Core; > > > > +/// Semantically the same as [`Core`] but reserved for internal usage of the corresponding bus > > +/// abstraction. > > +pub struct Internal; > > Naming is hard :) > > As this is ONLY for the bus code to touch, why not call it Bus_Internal? BusInternal is better indeed! > And can a driver touch this, or only the bus owner? It is to prevent drivers from getting access to functions implemented for &Device<BusInternal>.
On Tue, Jul 01, 2025 at 12:41:53PM +0200, Danilo Krummrich wrote: > On Tue, Jul 01, 2025 at 11:26:47AM +0200, Greg KH wrote: > > On Sat, Jun 21, 2025 at 09:43:27PM +0200, Danilo Krummrich wrote: > > > Introduce an internal device context, which is semantically equivalent > > > to the Core device context, but reserved for bus abstractions. > > > > > > This allows implementing methods for the Device type, which are limited > > > to be used within the core context of bus abstractions, i.e. restrict > > > the availability for drivers. > > > > > > Signed-off-by: Danilo Krummrich <dakr@kernel.org> > > > --- > > > rust/kernel/device.rs | 14 ++++++++++++++ > > > 1 file changed, 14 insertions(+) > > > > > > diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs > > > index 665f5ceadecc..e9094d8322d5 100644 > > > --- a/rust/kernel/device.rs > > > +++ b/rust/kernel/device.rs > > > @@ -261,6 +261,10 @@ pub trait DeviceContext: private::Sealed {} > > > /// any of the bus callbacks, such as `probe()`. > > > pub struct Core; > > > > > > +/// Semantically the same as [`Core`] but reserved for internal usage of the corresponding bus > > > +/// abstraction. > > > +pub struct Internal; > > > > Naming is hard :) > > > > As this is ONLY for the bus code to touch, why not call it Bus_Internal? > > BusInternal is better indeed! I now remember that I first wanted to go for CoreInternal, but then went for just Internal, since it thought it was unnecessary to be more specific. But I now think CoreInternal would have been the correct pick. > > And can a driver touch this, or only the bus owner? > > It is to prevent drivers from getting access to functions implemented for > &Device<BusInternal>.
On Tue, Jul 01, 2025 at 02:32:48PM +0200, Danilo Krummrich wrote: > On Tue, Jul 01, 2025 at 12:41:53PM +0200, Danilo Krummrich wrote: > > On Tue, Jul 01, 2025 at 11:26:47AM +0200, Greg KH wrote: > > > On Sat, Jun 21, 2025 at 09:43:27PM +0200, Danilo Krummrich wrote: > > > > Introduce an internal device context, which is semantically equivalent > > > > to the Core device context, but reserved for bus abstractions. > > > > > > > > This allows implementing methods for the Device type, which are limited > > > > to be used within the core context of bus abstractions, i.e. restrict > > > > the availability for drivers. > > > > > > > > Signed-off-by: Danilo Krummrich <dakr@kernel.org> > > > > --- > > > > rust/kernel/device.rs | 14 ++++++++++++++ > > > > 1 file changed, 14 insertions(+) > > > > > > > > diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs > > > > index 665f5ceadecc..e9094d8322d5 100644 > > > > --- a/rust/kernel/device.rs > > > > +++ b/rust/kernel/device.rs > > > > @@ -261,6 +261,10 @@ pub trait DeviceContext: private::Sealed {} > > > > /// any of the bus callbacks, such as `probe()`. > > > > pub struct Core; > > > > > > > > +/// Semantically the same as [`Core`] but reserved for internal usage of the corresponding bus > > > > +/// abstraction. > > > > +pub struct Internal; > > > > > > Naming is hard :) > > > > > > As this is ONLY for the bus code to touch, why not call it Bus_Internal? > > > > BusInternal is better indeed! > > I now remember that I first wanted to go for CoreInternal, but then went for > just Internal, since it thought it was unnecessary to be more specific. But I > now think CoreInternal would have been the correct pick. Thanks for the long explainations that helped out a lot. As I said on chat earlier, I agree with you now. Can you respin this with CoreInternal and we can queue it up? Worst thing that happens is the api doesn't work out and we rework it based on real users :) thanks, greg k-h
© 2016 - 2025 Red Hat, Inc.