Skip to contentSkip to conversation

rust: irq: add missing 'static bound to IRQ callback functions

PATCH In review Cole Leavitt 3 participants 3 messages Feb 17

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: irq: add missing 'static bound to IRQ callback functions

The Registration<T> and ThreadedRegistration<T> structs require
T: Handler + 'static and T: ThreadedHandler + 'static respectively. However, the callback functions that are used with these registrations were missing the corresponding 'static bound:

- handle_irq_callback<T: Handler>
- handle_threaded_irq_callback<T: ThreadedHandler>
- thread_fn_callback<T: ThreadedHandler>

This bound inconsistency was previously accepted by rustc, but recent compiler versions (1.93+) enforce E0310 more strictly when casting raw pointers to references in generic contexts. The callbacks cast a *mut c_void to a reference of the registration type, which requires T to be valid for 'static when the registration struct requires it.

Add the missing 'static bound to all three callback functions to match their registration struct requirements and fix compilation with recent rustc versions.

Fixes 135d40523244 ("rust: irq: add threaded handler support")
---
 rust/kernel/irq/request.rs | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
rust/kernel/irq/request.rs +9 -3
index 67769800117c..7a36f790593e 100644
@@ -260,7 +260,10 @@ pub fn synchronize(&self, dev: &Device<Bound>) -> Result {
 /// # Safety
 ///
 /// 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 {
+unsafe extern "C" fn handle_irq_callback<T: Handler + 'static>(
+    _irq: i32,
+    ptr: *mut c_void,
+) -> c_uint {
     // SAFETY: `ptr` is a pointer to `Registration<T>` set in `Registration::new`
     let registration = unsafe { &*(ptr as *const Registration<T>) };
     // SAFETY: The irq callback is removed before the device is unbound, so the fact that the irq
@@ -478,7 +481,7 @@ pub fn synchronize(&self, dev: &Device<Bound>) -> Result {
 /// # Safety
 ///
 /// This function should be only used as the callback in `request_threaded_irq`.
-unsafe extern "C" fn handle_threaded_irq_callback<T: ThreadedHandler>(
+unsafe extern "C" fn handle_threaded_irq_callback<T: ThreadedHandler + 'static>(
     _irq: i32,
     ptr: *mut c_void,
 ) -> c_uint {
@@ -494,7 +497,10 @@ pub fn synchronize(&self, dev: &Device<Bound>) -> Result {
 /// # 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 {
+unsafe extern "C" fn thread_fn_callback<T: ThreadedHandler + 'static>(
+    _irq: i32,
+    ptr: *mut c_void,
+) -> c_uint {
     // SAFETY: `ptr` is a pointer to `ThreadedRegistration<T>` set in `ThreadedRegistration::new`
     let registration = unsafe { &*(ptr as *const ThreadedRegistration<T>) };
     // SAFETY: The irq callback is removed before the device is unbound, so the fact that the irq
Signature · 1 line
2.52.0
maintainer Reviewed-by on v1 1/1
#1 lore raw

Hi Cole,

Quoted 68 lines · Cole Leavitt

The Registration<T> and ThreadedRegistration<T> structs require
T: Handler + 'static and T: ThreadedHandler + 'static respectively. However, the callback functions that are used with these registrations were missing the corresponding 'static bound:

- handle_irq_callback<T: Handler>
- handle_threaded_irq_callback<T: ThreadedHandler>
- thread_fn_callback<T: ThreadedHandler>

This bound inconsistency was previously accepted by rustc, but recent compiler versions (1.93+) enforce E0310 more strictly when casting raw pointers to references in generic contexts. The callbacks cast a *mut c_void to a reference of the registration type, which requires T to be valid for 'static when the registration struct requires it.

Add the missing 'static bound to all three callback functions to match their registration struct requirements and fix compilation with recent rustc versions.

Fixes 135d40523244 ("rust: irq: add threaded handler support")

---
rust/kernel/irq/request.rs | 12 +++++++++---

1 file changed, 9 insertions(+), 3 deletions(-)
rust/kernel/irq/request.rs +9 -3
index 67769800117c..7a36f790593e 100644
@@ -260,7 +260,10 @@ pub fn synchronize(&self, dev: &Device<Bound>) -> Result {
 /// # Safety
 ///
 /// 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 {
+unsafe extern "C" fn handle_irq_callback<T: Handler + 'static>(
+    _irq: i32,
+    ptr: *mut c_void,
+) -> c_uint {
    // SAFETY: `ptr` is a pointer to `Registration<T>` set in `Registration::new`
    let registration = unsafe { &*(ptr as *const Registration<T>) };
    // SAFETY: The irq callback is removed before the device is unbound, so the fact that the irq
@@ -478,7 +481,7 @@ pub fn synchronize(&self, dev: &Device<Bound>) -> Result {
 /// # Safety
 ///
 /// This function should be only used as the callback in `request_threaded_irq`.
-unsafe extern "C" fn handle_threaded_irq_callback<T: ThreadedHandler>(
+unsafe extern "C" fn handle_threaded_irq_callback<T: ThreadedHandler + 'static>(
    _irq: i32,
    ptr: *mut c_void,
 ) -> c_uint {
@@ -494,7 +497,10 @@ pub fn synchronize(&self, dev: &Device<Bound>) -> Result {
 /// # 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 {
+unsafe extern "C" fn thread_fn_callback<T: ThreadedHandler + 'static>(
+    _irq: i32,
+    ptr: *mut c_void,
+) -> c_uint {
    // SAFETY: `ptr` is a pointer to `ThreadedRegistration<T>` set in `ThreadedRegistration::new`
    let registration = unsafe { &*(ptr as *const ThreadedRegistration<T>) };
    // SAFETY: The irq callback is removed before the device is unbound, so the fact that the irq
Signature · 1 line
2.52.0

This looks fine to me. Sorry for the oversight and thanks for the fix.

maintainer
#2 lore raw
In reply to Cole Leavitt
Quoted 15 lines · Cole Leavitt

The Registration<T> and ThreadedRegistration<T> structs require
T: Handler + 'static and T: ThreadedHandler + 'static respectively. However, the callback functions that are used with these registrations were missing the corresponding 'static bound:

- handle_irq_callback<T: Handler>
- handle_threaded_irq_callback<T: ThreadedHandler>
- thread_fn_callback<T: ThreadedHandler>

This bound inconsistency was previously accepted by rustc, but recent compiler versions (1.93+) enforce E0310 more strictly when casting raw pointers to references in generic contexts. The callbacks cast a *mut c_void to a reference of the registration type, which requires T to be valid for 'static when the registration struct requires it.

Quoted 6 lines

Add the missing 'static bound to all three callback functions to match their registration struct requirements and fix compilation with recent rustc versions.

Fixes 135d40523244 ("rust: irq: add threaded handler support")

Thanks for the patch! "Unfortunately", there is already an equivalent fix for this on the list.