* Re: [PATCH 2/2] rust: device: implement Device::as_bound()
2025-07-13 18:26 ` [PATCH 2/2] rust: device: implement Device::as_bound() Danilo Krummrich
@ 2025-07-13 19:37 ` Danilo Krummrich
2025-07-13 19:39 ` Benno Lossin
` (2 subsequent siblings)
3 siblings, 0 replies; 12+ messages in thread
From: Danilo Krummrich @ 2025-07-13 19:37 UTC (permalink / raw)
To: gregkh, rafael, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
lossin, a.hindborg, aliceryhl, tmgross, daniel.almeida,
m.wilczynski
Cc: rust-for-linux, linux-kernel
On Sun Jul 13, 2025 at 8:26 PM CEST, Danilo Krummrich wrote:
> Provide an unsafe functions for abstractions to convert a regular
> &Device to a &Device<Bound>.
>
> This is useful for registrations that provide certain guarantees for the
> scope of their callbacks, such as IRQs or certain class device
> registrations (e.g. PWM, miscdevice).
>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
> rust/kernel/device.rs | 19 +++++++++++++++++++
> 1 file changed, 19 insertions(+)
>
> diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
> index d527ceef829e..4d9b052afe92 100644
> --- a/rust/kernel/device.rs
> +++ b/rust/kernel/device.rs
> @@ -60,6 +60,25 @@ pub unsafe fn get_device(ptr: *mut bindings::device) -> ARef<Self> {
> // SAFETY: By the safety requirements ptr is valid
> unsafe { Self::as_ref(ptr) }.into()
> }
> +
> + /// Convert a [`&Device`](Device) into a [`&Device<Bound>`](Device<Bound>).
> + ///
> + /// # Safety
> + ///
> + /// The caller is responsible to ensure that the returned [`&Device<Bound>`](Device<Bound>)
> + /// only lives as long as it can be guaranteed that the [`Device`] is actually bound.
> + pub unsafe fn as_bound(&self) -> &Device<Bound> {
> + let ptr = core::ptr::from_ref(self);
> +
> + // CAST: By the safety requirements the caller is responsible to guarantee that the
> + // returned reference only lives as long as the device is actually bound.
> + let ptr = ptr.cast::<Device<Bound>>();
> +
> + // SAFETY:
> + // - `ptr` comes from `from_ref(self)` above, hence it's guaranteed to be valid.
> + // - Any valid `Device` pointer is also a valid pointer for `Device<Bound>`.
> + unsafe { &*ptr.cast() }
> + }
> }
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index 4d9b052afe92..b6b81546a0da 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -72,12 +72,12 @@ pub unsafe fn as_bound(&self) -> &Device<Bound> {
// CAST: By the safety requirements the caller is responsible to guarantee that the
// returned reference only lives as long as the device is actually bound.
- let ptr = ptr.cast::<Device<Bound>>();
+ let ptr = ptr.cast();
// SAFETY:
// - `ptr` comes from `from_ref(self)` above, hence it's guaranteed to be valid.
// - Any valid `Device` pointer is also a valid pointer for `Device<Bound>`.
- unsafe { &*ptr.cast() }
+ unsafe { &*ptr }
}
}
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH 2/2] rust: device: implement Device::as_bound()
2025-07-13 18:26 ` [PATCH 2/2] rust: device: implement Device::as_bound() Danilo Krummrich
2025-07-13 19:37 ` Danilo Krummrich
@ 2025-07-13 19:39 ` Benno Lossin
2025-07-14 5:30 ` Greg KH
2025-07-15 20:30 ` Danilo Krummrich
3 siblings, 0 replies; 12+ messages in thread
From: Benno Lossin @ 2025-07-13 19:39 UTC (permalink / raw)
To: Danilo Krummrich, gregkh, rafael, ojeda, alex.gaynor, boqun.feng,
gary, bjorn3_gh, a.hindborg, aliceryhl, tmgross, daniel.almeida,
m.wilczynski
Cc: rust-for-linux, linux-kernel
On Sun Jul 13, 2025 at 8:26 PM CEST, Danilo Krummrich wrote:
> Provide an unsafe functions for abstractions to convert a regular
> &Device to a &Device<Bound>.
>
> This is useful for registrations that provide certain guarantees for the
> scope of their callbacks, such as IRQs or certain class device
> registrations (e.g. PWM, miscdevice).
>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Reviewed-by: Benno Lossin <lossin@kernel.org>
---
Cheers,
Benno
> ---
> rust/kernel/device.rs | 19 +++++++++++++++++++
> 1 file changed, 19 insertions(+)
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 2/2] rust: device: implement Device::as_bound()
2025-07-13 18:26 ` [PATCH 2/2] rust: device: implement Device::as_bound() Danilo Krummrich
2025-07-13 19:37 ` Danilo Krummrich
2025-07-13 19:39 ` Benno Lossin
@ 2025-07-14 5:30 ` Greg KH
2025-07-14 9:49 ` Danilo Krummrich
2025-07-15 20:30 ` Danilo Krummrich
3 siblings, 1 reply; 12+ messages in thread
From: Greg KH @ 2025-07-14 5:30 UTC (permalink / raw)
To: Danilo Krummrich
Cc: rafael, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross, daniel.almeida, m.wilczynski,
rust-for-linux, linux-kernel
On Sun, Jul 13, 2025 at 08:26:54PM +0200, Danilo Krummrich wrote:
> Provide an unsafe functions for abstractions to convert a regular
> &Device to a &Device<Bound>.
>
> This is useful for registrations that provide certain guarantees for the
> scope of their callbacks, such as IRQs or certain class device
> registrations (e.g. PWM, miscdevice).
Do we have an example where this can be used today in the tree, or is
this only for new stuff going forward that would use it?
thanks,
greg k-h
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 2/2] rust: device: implement Device::as_bound()
2025-07-14 5:30 ` Greg KH
@ 2025-07-14 9:49 ` Danilo Krummrich
2025-07-14 13:24 ` Greg KH
0 siblings, 1 reply; 12+ messages in thread
From: Danilo Krummrich @ 2025-07-14 9:49 UTC (permalink / raw)
To: Greg KH
Cc: rafael, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross, daniel.almeida, m.wilczynski,
rust-for-linux, linux-kernel
On Mon Jul 14, 2025 at 7:30 AM CEST, Greg KH wrote:
> On Sun, Jul 13, 2025 at 08:26:54PM +0200, Danilo Krummrich wrote:
>> Provide an unsafe functions for abstractions to convert a regular
>> &Device to a &Device<Bound>.
>>
>> This is useful for registrations that provide certain guarantees for the
>> scope of their callbacks, such as IRQs or certain class device
>> registrations (e.g. PWM, miscdevice).
>
> Do we have an example where this can be used today in the tree, or is
> this only for new stuff going forward that would use it?
There's miscdevice in tree, but to be fair, miscdevice doesn't need it without
my patch series [1] adding driver support for the existing miscdevice
abstractions; the patch in [2] out of this series would use it within
args_from_registration().
The PWM abstractions [3] need it in bound_parent_device(). The use-case is the
same as everywhere else, PWM chips never out-live driver unbind, hence they can
provide the corresponding bus device as &Device<Bound>.
The same is true for IRQ registrations [4]. free_irq() is guaranteed to be
called before driver unbind, hence we can provide a &Device<Bound> in the IRQ
callbacks.
Ultimately, we want to provide this "cookie" in any driver scope that can be
proven to be lifetime wise limited to device / driver unbind, so there'll be
much more users.
[1] https://lore.kernel.org/lkml/20250530142447.166524-1-dakr@kernel.org/
[2] https://lore.kernel.org/lkml/20250530142447.166524-7-dakr@kernel.org/
[3] https://lore.kernel.org/lkml/20250710-rust-next-pwm-working-fan-for-sending-v11-3-93824a16f9ec@samsung.com/
[4] https://lore.kernel.org/lkml/20250703-topics-tyr-request_irq-v6-3-74103bdc7c52@collabora.com/
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 2/2] rust: device: implement Device::as_bound()
2025-07-14 9:49 ` Danilo Krummrich
@ 2025-07-14 13:24 ` Greg KH
2025-07-14 13:26 ` Danilo Krummrich
0 siblings, 1 reply; 12+ messages in thread
From: Greg KH @ 2025-07-14 13:24 UTC (permalink / raw)
To: Danilo Krummrich
Cc: rafael, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross, daniel.almeida, m.wilczynski,
rust-for-linux, linux-kernel
On Mon, Jul 14, 2025 at 11:49:44AM +0200, Danilo Krummrich wrote:
> On Mon Jul 14, 2025 at 7:30 AM CEST, Greg KH wrote:
> > On Sun, Jul 13, 2025 at 08:26:54PM +0200, Danilo Krummrich wrote:
> >> Provide an unsafe functions for abstractions to convert a regular
> >> &Device to a &Device<Bound>.
> >>
> >> This is useful for registrations that provide certain guarantees for the
> >> scope of their callbacks, such as IRQs or certain class device
> >> registrations (e.g. PWM, miscdevice).
> >
> > Do we have an example where this can be used today in the tree, or is
> > this only for new stuff going forward that would use it?
>
> There's miscdevice in tree, but to be fair, miscdevice doesn't need it without
> my patch series [1] adding driver support for the existing miscdevice
> abstractions; the patch in [2] out of this series would use it within
> args_from_registration().
>
> The PWM abstractions [3] need it in bound_parent_device(). The use-case is the
> same as everywhere else, PWM chips never out-live driver unbind, hence they can
> provide the corresponding bus device as &Device<Bound>.
>
> The same is true for IRQ registrations [4]. free_irq() is guaranteed to be
> called before driver unbind, hence we can provide a &Device<Bound> in the IRQ
> callbacks.
>
> Ultimately, we want to provide this "cookie" in any driver scope that can be
> proven to be lifetime wise limited to device / driver unbind, so there'll be
> much more users.
Ok, so we should probably merge it now for 6.17-rc1, no objection from
me!
thanks,
greg k-h
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 2/2] rust: device: implement Device::as_bound()
2025-07-14 13:24 ` Greg KH
@ 2025-07-14 13:26 ` Danilo Krummrich
0 siblings, 0 replies; 12+ messages in thread
From: Danilo Krummrich @ 2025-07-14 13:26 UTC (permalink / raw)
To: Greg KH
Cc: rafael, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross, daniel.almeida, m.wilczynski,
rust-for-linux, linux-kernel
On 7/14/25 3:24 PM, Greg KH wrote:
> On Mon, Jul 14, 2025 at 11:49:44AM +0200, Danilo Krummrich wrote:
>> On Mon Jul 14, 2025 at 7:30 AM CEST, Greg KH wrote:
>>> On Sun, Jul 13, 2025 at 08:26:54PM +0200, Danilo Krummrich wrote:
>>>> Provide an unsafe functions for abstractions to convert a regular
>>>> &Device to a &Device<Bound>.
>>>>
>>>> This is useful for registrations that provide certain guarantees for the
>>>> scope of their callbacks, such as IRQs or certain class device
>>>> registrations (e.g. PWM, miscdevice).
>>>
>>> Do we have an example where this can be used today in the tree, or is
>>> this only for new stuff going forward that would use it?
>>
>> There's miscdevice in tree, but to be fair, miscdevice doesn't need it without
>> my patch series [1] adding driver support for the existing miscdevice
>> abstractions; the patch in [2] out of this series would use it within
>> args_from_registration().
>>
>> The PWM abstractions [3] need it in bound_parent_device(). The use-case is the
>> same as everywhere else, PWM chips never out-live driver unbind, hence they can
>> provide the corresponding bus device as &Device<Bound>.
>>
>> The same is true for IRQ registrations [4]. free_irq() is guaranteed to be
>> called before driver unbind, hence we can provide a &Device<Bound> in the IRQ
>> callbacks.
>>
>> Ultimately, we want to provide this "cookie" in any driver scope that can be
>> proven to be lifetime wise limited to device / driver unbind, so there'll be
>> much more users.
>
> Ok, so we should probably merge it now for 6.17-rc1, no objection from
> me!
Yup, gonna pick it up soon (with the casts fixed up as noted in a previous
reply). :)
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH 2/2] rust: device: implement Device::as_bound()
2025-07-13 18:26 ` [PATCH 2/2] rust: device: implement Device::as_bound() Danilo Krummrich
` (2 preceding siblings ...)
2025-07-14 5:30 ` Greg KH
@ 2025-07-15 20:30 ` Danilo Krummrich
3 siblings, 0 replies; 12+ messages in thread
From: Danilo Krummrich @ 2025-07-15 20:30 UTC (permalink / raw)
To: gregkh, rafael, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
lossin, a.hindborg, aliceryhl, tmgross, daniel.almeida,
m.wilczynski
Cc: rust-for-linux, linux-kernel
On Sun Jul 13, 2025 at 8:26 PM CEST, Danilo Krummrich wrote:
> Provide an unsafe functions for abstractions to convert a regular
> &Device to a &Device<Bound>.
>
> This is useful for registrations that provide certain guarantees for the
> scope of their callbacks, such as IRQs or certain class device
> registrations (e.g. PWM, miscdevice).
>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Applied to driver-core-testing, thanks!
[ Remove unnecessary cast(). - Danilo ]
^ permalink raw reply [flat|nested] 12+ messages in thread