Re: [PATCH] rust: irq: add support for request_irq()

From: Alice Ryhl
Date: Tue Jan 14 2025 - 13:37:18 EST


On Tue, Jan 14, 2025 at 7:35 PM Daniel Almeida
<daniel.almeida@xxxxxxxxxxxxx> wrote:
>
> Hi Alice,
>
> > On 13 Jan 2025, at 11:42, Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:
> >
> > On Thu, Oct 24, 2024 at 4:20 PM Daniel Almeida
> > <daniel.almeida@xxxxxxxxxxxxx> wrote:
> >>
> >> +impl<T: Handler> Registration<T> {
> >> + /// Registers the IRQ handler with the system for the given IRQ number. The
> >> + /// handler must be able to be called as soon as this function returns.
> >> + pub fn register(
> >> + irq: u32,
> >> + flags: Flags,
> >> + name: &'static CStr,
> >> + handler: T,
> >> + ) -> impl PinInit<Self, Error> {
> >> + try_pin_init!(Self {
> >> + irq,
> >> + handler: Opaque::new(handler)
> >> + })
> >> + .pin_chain(move |slot| {
> >> + // SAFETY:
> >> + // - `handler` points to a valid function defined below.
> >> + // - only valid flags can be constructed using the `flags` module.
> >> + // - `devname` is a nul-terminated string with a 'static lifetime.
> >> + // - `ptr` is a cookie used to identify the handler. The same cookie is
> >> + // passed back when the system calls the handler.
> >> + to_result(unsafe {
> >> + bindings::request_irq(
> >> + irq,
> >> + Some(handle_irq_callback::<T>),
> >> + flags.0,
> >> + name.as_char_ptr(),
> >> + &*slot as *const _ as *mut core::ffi::c_void,
> >> + )
> >> + })?;
> >> +
> >> + Ok(())
> >> + })
> >
> > I think this does not run the destructor of `handler` when
> > `request_irq` returns an error.
> >
> > Alice
>
> How? T is passed by value, so if request_irq() fails and thus Registration::register() returns Err,
> I assume T is dropped.
>
> Or is the pin_init! stuff somehow special here?

It's not the pin_init! stuff, but the Opaque stuff. If it fails, then
it runs the destructor of Opaque<T>, which does *not* run the
destructor of T.

Alice