Re: [PATCH v3 1/2] rust: irq: add support for request_irq()

From: Alice Ryhl
Date: Mon Jun 02 2025 - 11:48:31 EST


On Wed, May 14, 2025 at 10:04:43PM +0200, Benno Lossin wrote:
> On Wed May 14, 2025 at 9:20 PM CEST, Daniel Almeida wrote:
> > + )
> > + });
> > +
> > + if res.is_err() {
> > + // SAFETY: We are returning an error, so we can destroy the slot.
> > + unsafe { core::ptr::drop_in_place(addr_of_mut!((*slot).handler)) };
> > + }
> > +
> > + res
> > + };
> > +
> > + // SAFETY:
> > + // - if this returns Ok, then every field of `slot` is fully
> > + // initialized.
> > + // - if this returns an error, then the slot does not need to remain
> > + // valid.
> > + unsafe { pin_init_from_closure(closure) }
>
> Please don't use `pin_init_from_closure`, instead do this:
>
> pin_init!(Self {
> irq,
> handler,
> _pin: PhantomPinned
> })
> .pin_chain(|this| {
> // SAFETY: TODO: correct FFI safety requirements
> to_result(unsafe {
> bindings::request_irq(...)
> })
> })
>
> The `pin_chain` function is exactly for this use-case, doing some
> operation that might fail after initializing & it will drop the value
> when the closure fails.

No, that doesn't work. Using pin_chain will call free_irq if the call to
request_irq fails, which is incorrect.

Alice