All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: Andreas Hindborg <a.hindborg@kernel.org>
Cc: Daniel Almeida <daniel.almeida@collabora.com>,
	ojeda@kernel.org, alex.gaynor@gmail.com,  boqun.feng@gmail.com,
	gary@garyguo.net, bjorn3_gh@protonmail.com,
	 benno.lossin@proton.me, tmgross@umich.edu,
	rust-for-linux@vger.kernel.org,  linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] rust: irq: add support for request_irq()
Date: Mon, 17 Mar 2025 14:58:48 +0000	[thread overview]
Message-ID: <Z9g4qDd7qp2t_e9u@google.com> (raw)
In-Reply-To: <87cyewhpxj.fsf@kernel.org>

On Tue, Mar 04, 2025 at 02:43:20PM +0100, Andreas Hindborg wrote:
> "Daniel Almeida" <daniel.almeida@collabora.com> writes:
> > +    /// handler after suspending interrupts. For system wakeup devices users
> > +    /// need to implement wakeup detection in their interrupt handlers.
> > +    pub const COND_SUSPEND: Flags = Flags(bindings::IRQF_COND_SUSPEND as _);
> > +
> > +    /// Don't enable IRQ or NMI automatically when users request it. Users will
> > +    /// enable it explicitly by `enable_irq` or `enable_nmi` later.
> > +    pub const NO_AUTOEN: Flags = Flags(bindings::IRQF_NO_AUTOEN as _);
> > +
> > +    /// Exclude from runnaway detection for IPI and similar handlers, depends on
> > +    /// `PERCPU`.
> > +    pub const NO_DEBUG: Flags = Flags(bindings::IRQF_NO_DEBUG as _);
> > +}
> > +
> > +/// The value that can be returned from an IrqHandler or a ThreadedIrqHandler.
> > +pub enum IrqReturn {
> 
> I learned recently that if you choose the right representation here, you
> don't need to cast here and when you call `Handler::handle_irq`. I think
> `#[repr(u32)]` is the one to use here.

I wonder if we can get it to use the repr of the same size as
irqreturn_t?

> > +    /// The interrupt was not from this device or was not handled.
> > +    None = bindings::irqreturn_IRQ_NONE as _,
> > +
> > +    /// The interrupt was handled by this device.
> > +    Handled = bindings::irqreturn_IRQ_HANDLED as _,
> > +}
> > +
> > +/// Callbacks for an IRQ handler.
> > +pub trait Handler: Sync {
> > +    /// The actual handler function. As usual, sleeps are not allowed in IRQ
> > +    /// context.
> > +    fn handle_irq(&self) -> IrqReturn;
> > +}
> 
> What is the reason for moving away from the following:
> 
> 
>     pub trait Handler {
>         /// The context data associated with and made available to the handler.
>         type Data: ForeignOwnable;
> 
>         /// Called from interrupt context when the irq happens.
>         fn handle_irq(data: <Self::Data as ForeignOwnable>::Borrowed<'_>) -> Return;
>     }
> 
> 
> I think we will run into problems if we want to pass `Arc<Foo>` as the
> handler. I don't think we can `impl Handler for Arc<Foo>` in a driver
> crate, since both `Handler` and `Arc` are defined in external crates

My understanding is that since the data is not stored behind a
private_data void pointer, we don't need ForeignOwnable. I think we
should avoid using ForeignOwnable when it's not necessary.

We can support the Arc / Box case by adding

impl<T: ?Sized + Handler> Handler for Arc<T> {
    fn handle_irq(&self) -> IrqReturn {
        T::handle_irq(self)
    }
}

This way, the user implements it for their struct and then it works with
Arc<MyStruct> too.

This kind of blanket impl for Arc/Box is very common in userspace Rust
too. For example, the Tokio traits AsyncRead/AsyncWrite have blanket
impls for Box.

> > +#[pin_data(PinnedDrop)]
> > +pub struct ThreadedRegistration<T: ThreadedHandler> {
> > +    irq: u32,
> > +    #[pin]
> > +    handler: T,
> > +    #[pin]
> > +    /// Pinned because we need address stability so that we can pass a pointer
> > +    /// to the callback.
> > +    _pin: PhantomPinned,
> > +}
> 
> As others have mentioned, I wonder if we can avoid the code duplication
> that makes up most of the rest of this patch.

I'm worried that getting rid of duplication makes the code too complex
in this case. I could be wrong, but it seems difficult to deduplicate in
a simple way.

Alice

  parent reply	other threads:[~2025-03-17 14:58 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <u-vC1KbeOK3Fd2PClzinb8LmqS_dntOW-pOSmZIFWotCZeTOg30xR_GYUc4oReAKZeuuu7ZaXWzfeTkpGMlr0A==@protonmail.internalid>
2025-01-22 16:39 ` [PATCH v2] rust: irq: add support for request_irq() Daniel Almeida
2025-01-23  7:17   ` Boqun Feng
2025-01-23  9:07     ` Alice Ryhl
2025-05-14 14:44       ` Daniel Almeida
2025-05-15 17:17         ` Christian Schrefl
2025-01-23 16:00   ` Christian Schrefl
2025-01-23 16:27     ` Daniel Almeida
2025-01-23 17:09       ` Christian Schrefl
2025-03-04 13:05       ` Andreas Hindborg
2025-02-10  8:41   ` Daniel Almeida
2025-02-10 16:41     ` Guangbo Cui
2025-03-04 13:11     ` Andreas Hindborg
2025-03-04 13:43   ` Andreas Hindborg
2025-03-04 16:48     ` Daniel Almeida
2025-03-17 14:58     ` Alice Ryhl [this message]
2025-05-09 13:58     ` Daniel Almeida

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Z9g4qDd7qp2t_e9u@google.com \
    --to=aliceryhl@google.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.