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

From: Alice Ryhl
Date: Thu Jan 23 2025 - 04:08:06 EST


On Thu, Jan 23, 2025 at 8:18 AM Boqun Feng <boqun.feng@xxxxxxxxx> wrote:
>
> On Wed, Jan 22, 2025 at 01:39:30PM -0300, Daniel Almeida wrote:
> > Add support for registering IRQ handlers in Rust.
> >
> > IRQ handlers are extensively used in drivers when some peripheral wants to
> > obtain the CPU attention. Registering a handler will make the system invoke the
> > passed-in function whenever the chosen IRQ line is triggered.
> >
> > Both regular and threaded IRQ handlers are supported through a Handler (or
> > ThreadedHandler) trait that is meant to be implemented by a type that:
> >
> > a) provides a function to be run by the system when the IRQ fires and,
> >
> > b) holds the shared data (i.e.: `T`) between process and IRQ contexts.
> >
> > The requirement that T is Sync derives from the fact that handlers might run
> > concurrently with other processes executing the same driver, creating the
> > potential for data races.
> >
> > Ideally, some interior mutability must be in place if T is to be mutated. This
> > should usually be done through the in-flight SpinLockIrq type.
> >
> > Co-developed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> > Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> > Signed-off-by: Daniel Almeida <daniel.almeida@xxxxxxxxxxxxx>
> > ---
> >
> > Changes from v1:
> >
> > - Added Co-developed-by tag to account for the work that Alice did in order to
> > figure out how to do this without Opaque<T> (Thanks!)
> > - Removed Opaque<T> in favor of plain T
>
> Hmmm...
>
> [...]
>
> > +#[pin_data(PinnedDrop)]
> > +pub struct Registration<T: Handler> {
> > + irq: u32,
> > + #[pin]
> > + handler: T,
>
> I think you still need to make `handler` as `!Unpin` because compilers
> can assume a `&mut T` from a `Pin<&mut Registration>`, am I missing
> something here?

The current version operates under the assumption that PhantomPinned
is enough. But I'm happy to add Aliased here.

Alice