rust: irq: pass RegistrationInner as cookie to request_irq
Subsystems (4)
Alice Ryhl
maintainerPATCH 1/1 rust: irq: pass RegistrationInner as cookie to request_irq
With commit ba268514ea14 ("rust: devres: fix race condition due to nesting"), Devres::new() returns Result<Self> by value instead of initializing in-place via PinInit. Because request_irq() is called inside Devres::new(), registration.inner is still uninitialized when the IRQ is enabled, so accessing registration.inner.device() in the IRQ callback can read uninitialized memory.
Fix this by storing the Device and handler pointer in RegistrationInner and passing RegistrationInner as the IRQ cookie after its fields are initialized.
This is not needed in mainline because commit 98c63ce4d760 ("rust: irq:
make Registration compatible with lifetime-bound drivers") removed Devres from irq::Registration and moved request_irq() after all fields are initialized.
---
This is the 7.2 version of:
https://lore.kernel.org/all/[email protected]/
--- rust/kernel/irq/request.rs | 89 ++++++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 35 deletions(-)
rust/kernel/irq/request.rs +54 -35
@@ -14,7 +14,7 @@use crate ::irq::flags::Flags ;use crate ::prelude::*;use crate ::str::CStr ; -use crate ::sync::Arc ; +use crate ::sync::{aref::ARef ,Arc };/// The value that can be returned from a [`Handler`] or a [`ThreadedHandler`]. #[repr( u32 )]
@@ -54,13 +54,18 @@ fn handle(&self, device: &Device<Bound>) -> IrqReturn {/// # Invariants /// /// - `self.irq` is the same as the one passed to `request_{threaded}_irq`. -/// - `cookie` was passed to `request_{threaded}_irq` as the cookie. It is guaranteed to be unique +/// - `&self` was passed to `request_{threaded}_irq` as the cookie. It is guaranteed to be unique /// by the type system, since each call to `new` will return a different instance of /// `Registration`. +/// - `self.handler` points to a valid instance of the handler `T` that lives at least until +/// `Self::drop` completes. #[pin_data( PinnedDrop )] struct RegistrationInner { irq:u32 , - cookie: *mut c_void , + dev:ARef <Device >, + handler: *const c_void , +#[pin] + _pin:PhantomPinned , }impl RegistrationInner {
@@ -77,18 +82,22 @@ fn drop(self: Pin<&mut Self>) {// // Safe as per the invariants of `RegistrationInner` and: // -// - The containing struct is `!Unpin` and was initialized using +// - `RegistrationInner` is `!Unpin` and was initialized using // pin-init, so it occupied the same memory location for the entirety of // its lifetime. // // Notice that this will block until all handlers finish executing, // i.e.: at no point will &self be invalid while the handler is running. -unsafe { bindings::free_irq (self.irq, self.cookie) }; +unsafe { + bindings::free_irq ( + self.irq, + core::ptr::from_mut ::<Self >(self.get_unchecked_mut ()).cast ::<c_void >(), + ) + }; } } -// SAFETY: We only use `inner` on drop, which called at most once with no -// concurrent access. +// SAFETY: `RegistrationInner` has no interior mutability and `handler` points to a `Sync` handler. unsafe impl Sync for RegistrationInner {}// SAFETY: It is safe to send `RegistrationInner` across threads.
@@ -179,7 +188,7 @@ pub fn irq(&self) -> u32 {/// /// # Invariants /// -/// * We own an irq handler whose cookie is a pointer to `Self`. +/// * We own an irq handler whose cookie is a pointer to `Self::inner`. #[pin_data] pub struct Registration <T :Handler > { #[pin]
@@ -206,10 +215,13 @@ pub fn new<'a>( handler <- handler, inner <-Devres ::new ( request.dev, - try_pin_init!(RegistrationInner { - // INVARIANT: `this` is a valid pointer to the `Registration` instance - cookie: this.as_ptr().cast::<c_void>(), - irq: { + try_pin_init!(&inner_this in RegistrationInner { + irq: request.irq, + dev: request.dev.into(), + // SAFETY: `this` is a valid pointer to the `Registration` instance. + handler: unsafe { &raw const (*this.as_ptr()).handler }.cast(), + _pin: PhantomPinned, + _: { // SAFETY: // - The callbacks are valid for use with request_irq. // - If this succeeds, the slot is guaranteed to be valid until the
@@ -224,11 +236,10 @@ pub fn new<'a>( Some(handle_irq_callback::<T>), flags.into_inner(), name.as_char_ptr(), - this.as_ptr().cast::<c_void>(), + inner_this.as_ptr().cast::<c_void>(), ) })?; - request.irq - } + }, }) ), _pin: PhantomPinned,
@@ -261,13 +272,15 @@ pub fn synchronize(&self, dev: &Device<Bound>) -> Result {/// /// This function should be only used as the callback in `request_irq`. unsafe extern "C" fn handle_irq_callback <T :Handler >(_irq:i32 , ptr: *mut c_void ) ->c_uint { -// SAFETY: `ptr` is a pointer to `Registration<T>` set in `Registration::new` -let registration =unsafe { &*(ptras *const Registration <T >) }; +// SAFETY: `ptr` is a pointer to `RegistrationInner` set in `Registration::new` +let inner =unsafe { &*(ptras *const RegistrationInner ) }; +// SAFETY: `inner.handler` is a pointer to `T` set in `Registration::new` +let handler =unsafe { &*inner.handler.cast ::<T >() };// SAFETY: The irq callback is removed before the device is unbound, so the fact that the irq // callback is running implies that the device has not yet been unbound. -let device =unsafe { registration.inner.device ().as_bound () }; +let device =unsafe { inner.dev.as_bound () }; -T ::handle (®istration.handler, device)as c_uint +T ::handle (handler, device)as c_uint }/// The value that can be returned from [`ThreadedHandler::handle`].
@@ -396,7 +409,7 @@ fn handle_threaded(&self, device: &Device<Bound>) -> IrqReturn {/// /// # Invariants /// -/// * We own an irq handler whose cookie is a pointer to `Self`. +/// * We own an irq handler whose cookie is a pointer to `Self::inner`. #[pin_data] pub struct ThreadedRegistration <T :ThreadedHandler > { #[pin]
@@ -423,10 +436,13 @@ pub fn new<'a>( handler <- handler, inner <-Devres ::new ( request.dev, - try_pin_init!(RegistrationInner { - // INVARIANT: `this` is a valid pointer to the `ThreadedRegistration` instance. - cookie: this.as_ptr().cast::<c_void>(), - irq: { + try_pin_init!(&inner_this in RegistrationInner { + irq: request.irq, + dev: request.dev.into(), + // SAFETY: `this` is a valid pointer to the `ThreadedRegistration` instance. + handler: unsafe { &raw const (*this.as_ptr()).handler }.cast(), + _pin: PhantomPinned, + _: { // SAFETY: // - The callbacks are valid for use with request_threaded_irq. // - If this succeeds, the slot is guaranteed to be valid until the
@@ -442,11 +458,10 @@ pub fn new<'a>( Some(thread_fn_callback::<T>), flags.into_inner(), name.as_char_ptr(), - this.as_ptr().cast::<c_void>(), + inner_this.as_ptr().cast::<c_void>(), ) })?; - request.irq - } + }, }) ), _pin: PhantomPinned,
@@ -482,24 +497,28 @@ pub fn synchronize(&self, dev: &Device<Bound>) -> Result { _irq: i32, ptr: *mut c_void, ) -> c_uint { -// SAFETY: `ptr` is a pointer to `ThreadedRegistration<T>` set in `ThreadedRegistration::new` -let registration =unsafe { &*(ptras *const ThreadedRegistration <T >) }; +// SAFETY: `ptr` is a pointer to `RegistrationInner` set in `ThreadedRegistration::new` +let inner =unsafe { &*(ptras *const RegistrationInner ) }; +// SAFETY: `inner.handler` is a pointer to `T` set in `ThreadedRegistration::new` +let handler =unsafe { &*inner.handler.cast ::<T >() };// SAFETY: The irq callback is removed before the device is unbound, so the fact that the irq // callback is running implies that the device has not yet been unbound. -let device =unsafe { registration.inner.device ().as_bound () }; +let device =unsafe { inner.dev.as_bound () }; -T ::handle (®istration.handler, device)as c_uint +T ::handle (handler, device)as c_uint }/// # Safety /// /// This function should be only used as the callback in `request_threaded_irq`. unsafe extern "C" fn thread_fn_callback <T :ThreadedHandler >(_irq:i32 , ptr: *mut c_void ) ->c_uint { -// SAFETY: `ptr` is a pointer to `ThreadedRegistration<T>` set in `ThreadedRegistration::new` -let registration =unsafe { &*(ptras *const ThreadedRegistration <T >) }; +// SAFETY: `ptr` is a pointer to `RegistrationInner` set in `ThreadedRegistration::new` +let inner =unsafe { &*(ptras *const RegistrationInner ) }; +// SAFETY: `inner.handler` is a pointer to `T` set in `ThreadedRegistration::new` +let handler =unsafe { &*inner.handler.cast ::<T >() };// SAFETY: The irq callback is removed before the device is unbound, so the fact that the irq // callback is running implies that the device has not yet been unbound. -let device =unsafe { registration.inner.device ().as_bound () }; +let device =unsafe { inner.dev.as_bound () }; -T ::handle_threaded (®istration.handler, device)as c_uint +T ::handle_threaded (handler, device)as c_uint }
---
base-commit: 5fce161649b4d779d1b76d9fcd52dc77779774b8
change-id: 20261006-irq-7-2-backport-117b7044b052
Best regards,
Signature · 1 line
Alice Ryhl <[email protected]>
Patch "rust: irq: pass RegistrationInner as cookie to request_irq" has been added to the 7.2-stable tree
This is a note to let you know that I've just added the patch titled
rust: irq: pass RegistrationInner as cookie to request_irq
to the 7.2-stable tree which can be found at:
http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary
The filename of the patch is:
rust-irq-pass-registrationinner-as-cookie-to-request_irq.patch
and it can be found in the queue-7.2 subdirectory.
If you, or anyone else, feels it should not be added to the stable tree, please let <[email protected]> know about it.
From [email protected] Wed Oct 7 08:24:34 2026
From: Alice Ryhl <[email protected]>
Date: Wed, 07 Oct 2026 06:01:58 +0000
Subject: rust: irq: pass RegistrationInner as cookie to request_irq
To: [email protected], Greg Kroah-Hartman <[email protected]>, Sasha Levin <[email protected]>, Danilo Krummrich <[email protected]>, Daniel Almeida <[email protected]>
Cc: "Alexandre Courbot" <[email protected]>, "Andreas Hindborg" <[email protected]>, "Benno Lossin" <[email protected]>, "Björn Roy Baron" <[email protected]>, "Boqun Feng" <[email protected]>, "Boris Brezillon" <[email protected]>, "Eliot Courtney" <[email protected]>, "Gary Guo" <[email protected]>, "Markus Probst" <[email protected]>, "Miguel Ojeda" <[email protected]>, "Onur Özkan" <[email protected]>, "Rafael J. Wysocki" <[email protected]>, "Tamir Duberstein" <[email protected]>, "Trevor Gross" <[email protected]>, [email protected], [email protected], [email protected], "Alice Ryhl" <[email protected]>
Message-ID: <[email protected]>
From: Alice Ryhl <[email protected]>
With commit ba268514ea14 ("rust: devres: fix race condition due to nesting"), Devres::new() returns Result<Self> by value instead of initializing in-place via PinInit. Because request_irq() is called inside Devres::new(), registration.inner is still uninitialized when the IRQ is enabled, so accessing registration.inner.device() in the IRQ callback can read uninitialized memory.
Fix this by storing the Device and handler pointer in RegistrationInner and passing RegistrationInner as the IRQ cookie after its fields are initialized.
This is not needed in mainline because commit 98c63ce4d760 ("rust: irq:
make Registration compatible with lifetime-bound drivers") removed Devres from irq::Registration and moved request_irq() after all fields are initialized.
--- rust/kernel/irq/request.rs | 89 +++++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 35 deletions(-)
rust/kernel/irq/request.rs +54 -35
@@ -14,7 +14,7 @@ use crate::error::to_result;use crate ::irq::flags::Flags ;use crate ::prelude::*;use crate ::str::CStr ; -use crate ::sync::Arc ; +use crate ::sync::{aref::ARef ,Arc };/// The value that can be returned from a [`Handler`] or a [`ThreadedHandler`]. #[repr( u32 )]
@@ -54,13 +54,18 @@ impl<T: ?Sized + Handler, A: Allocator +/// # Invariants /// /// - `self.irq` is the same as the one passed to `request_{threaded}_irq`. -/// - `cookie` was passed to `request_{threaded}_irq` as the cookie. It is guaranteed to be unique +/// - `&self` was passed to `request_{threaded}_irq` as the cookie. It is guaranteed to be unique /// by the type system, since each call to `new` will return a different instance of /// `Registration`. +/// - `self.handler` points to a valid instance of the handler `T` that lives at least until +/// `Self::drop` completes. #[pin_data( PinnedDrop )] struct RegistrationInner { irq:u32 , - cookie: *mut c_void , + dev:ARef <Device >, + handler: *const c_void , +#[pin] + _pin:PhantomPinned , }impl RegistrationInner {
@@ -77,18 +82,22 @@ impl PinnedDrop for RegistrationInner {// // Safe as per the invariants of `RegistrationInner` and: // -// - The containing struct is `!Unpin` and was initialized using +// - `RegistrationInner` is `!Unpin` and was initialized using // pin-init, so it occupied the same memory location for the entirety of // its lifetime. // // Notice that this will block until all handlers finish executing, // i.e.: at no point will &self be invalid while the handler is running. -unsafe { bindings::free_irq (self.irq, self.cookie) }; +unsafe { + bindings::free_irq ( + self.irq, + core::ptr::from_mut ::<Self >(self.get_unchecked_mut ()).cast ::<c_void >(), + ) + }; } } -// SAFETY: We only use `inner` on drop, which called at most once with no -// concurrent access. +// SAFETY: `RegistrationInner` has no interior mutability and `handler` points to a `Sync` handler. unsafe impl Sync for RegistrationInner {}// SAFETY: It is safe to send `RegistrationInner` across threads.
@@ -179,7 +188,7 @@ impl<'a> IrqRequest<'a> {/// /// # Invariants /// -/// * We own an irq handler whose cookie is a pointer to `Self`. +/// * We own an irq handler whose cookie is a pointer to `Self::inner`. #[pin_data] pub struct Registration <T :Handler > { #[pin]
@@ -206,10 +215,13 @@ impl<T: Handler> Registration<T> { handler <- handler, inner <-Devres ::new ( request.dev, - try_pin_init!(RegistrationInner { - // INVARIANT: `this` is a valid pointer to the `Registration` instance - cookie: this.as_ptr().cast::<c_void>(), - irq: { + try_pin_init!(&inner_this in RegistrationInner { + irq: request.irq, + dev: request.dev.into(), + // SAFETY: `this` is a valid pointer to the `Registration` instance. + handler: unsafe { &raw const (*this.as_ptr()).handler }.cast(), + _pin: PhantomPinned, + _: { // SAFETY: // - The callbacks are valid for use with request_irq. // - If this succeeds, the slot is guaranteed to be valid until the
@@ -224,11 +236,10 @@ impl<T: Handler> Registration<T> { Some(handle_irq_callback::<T>), flags.into_inner(), name.as_char_ptr(), - this.as_ptr().cast::<c_void>(), + inner_this.as_ptr().cast::<c_void>(), ) })?; - request.irq - } + }, }) ), _pin: PhantomPinned,
@@ -261,13 +272,15 @@ impl<T: Handler> Registration<T> {/// /// This function should be only used as the callback in `request_irq`. unsafe extern "C" fn handle_irq_callback <T :Handler >(_irq:i32 , ptr: *mut c_void ) ->c_uint { -// SAFETY: `ptr` is a pointer to `Registration<T>` set in `Registration::new` -let registration =unsafe { &*(ptras *const Registration <T >) }; +// SAFETY: `ptr` is a pointer to `RegistrationInner` set in `Registration::new` +let inner =unsafe { &*(ptras *const RegistrationInner ) }; +// SAFETY: `inner.handler` is a pointer to `T` set in `Registration::new` +let handler =unsafe { &*inner.handler.cast ::<T >() };// SAFETY: The irq callback is removed before the device is unbound, so the fact that the irq // callback is running implies that the device has not yet been unbound. -let device =unsafe { registration.inner.device ().as_bound () }; +let device =unsafe { inner.dev.as_bound () }; -T ::handle (®istration.handler, device)as c_uint +T ::handle (handler, device)as c_uint }/// The value that can be returned from [`ThreadedHandler::handle`].
@@ -396,7 +409,7 @@ impl<T: ?Sized + ThreadedHandler, A: All/// /// # Invariants /// -/// * We own an irq handler whose cookie is a pointer to `Self`. +/// * We own an irq handler whose cookie is a pointer to `Self::inner`. #[pin_data] pub struct ThreadedRegistration <T :ThreadedHandler > { #[pin]
@@ -423,10 +436,13 @@ impl<T: ThreadedHandler> ThreadedRegistr handler <- handler, inner <-Devres ::new ( request.dev, - try_pin_init!(RegistrationInner { - // INVARIANT: `this` is a valid pointer to the `ThreadedRegistration` instance. - cookie: this.as_ptr().cast::<c_void>(), - irq: { + try_pin_init!(&inner_this in RegistrationInner { + irq: request.irq, + dev: request.dev.into(), + // SAFETY: `this` is a valid pointer to the `ThreadedRegistration` instance. + handler: unsafe { &raw const (*this.as_ptr()).handler }.cast(), + _pin: PhantomPinned, + _: { // SAFETY: // - The callbacks are valid for use with request_threaded_irq. // - If this succeeds, the slot is guaranteed to be valid until the
@@ -442,11 +458,10 @@ impl<T: ThreadedHandler> ThreadedRegistr Some(thread_fn_callback::<T>), flags.into_inner(), name.as_char_ptr(), - this.as_ptr().cast::<c_void>(), + inner_this.as_ptr().cast::<c_void>(), ) })?; - request.irq - } + }, }) ), _pin: PhantomPinned,
@@ -482,24 +497,28 @@ unsafe extern "C" fn handle_threaded_irq _irq: i32, ptr: *mut c_void, ) -> c_uint { -// SAFETY: `ptr` is a pointer to `ThreadedRegistration<T>` set in `ThreadedRegistration::new` -let registration =unsafe { &*(ptras *const ThreadedRegistration <T >) }; +// SAFETY: `ptr` is a pointer to `RegistrationInner` set in `ThreadedRegistration::new` +let inner =unsafe { &*(ptras *const RegistrationInner ) }; +// SAFETY: `inner.handler` is a pointer to `T` set in `ThreadedRegistration::new` +let handler =unsafe { &*inner.handler.cast ::<T >() };// SAFETY: The irq callback is removed before the device is unbound, so the fact that the irq // callback is running implies that the device has not yet been unbound. -let device =unsafe { registration.inner.device ().as_bound () }; +let device =unsafe { inner.dev.as_bound () }; -T ::handle (®istration.handler, device)as c_uint +T ::handle (handler, device)as c_uint }/// # Safety /// /// This function should be only used as the callback in `request_threaded_irq`. unsafe extern "C" fn thread_fn_callback <T :ThreadedHandler >(_irq:i32 , ptr: *mut c_void ) ->c_uint { -// SAFETY: `ptr` is a pointer to `ThreadedRegistration<T>` set in `ThreadedRegistration::new` -let registration =unsafe { &*(ptras *const ThreadedRegistration <T >) }; +// SAFETY: `ptr` is a pointer to `RegistrationInner` set in `ThreadedRegistration::new` +let inner =unsafe { &*(ptras *const RegistrationInner ) }; +// SAFETY: `inner.handler` is a pointer to `T` set in `ThreadedRegistration::new` +let handler =unsafe { &*inner.handler.cast ::<T >() };// SAFETY: The irq callback is removed before the device is unbound, so the fact that the irq // callback is running implies that the device has not yet been unbound. -let device =unsafe { registration.inner.device ().as_bound () }; +let device =unsafe { inner.dev.as_bound () }; -T ::handle_threaded (®istration.handler, device)as c_uint +T ::handle_threaded (handler, device)as c_uint }
Patches currently in stable-queue which might be from [email protected] are
queue-7.2/rust_binder-cancel-deferred-work-items-in-thread-exit.patch queue-7.2/rust-irq-pass-registrationinner-as-cookie-to-request_irq.patch queue-7.2/rust_binder-reschedule-node-refcount-update-on-thread-exit.patch queue-7.2/binder-fix-leaked-fd-fixups-on-tf_update_txn-supersede.patch