Re: [PATCH v6 3/6] rust: irq: add support for non-threaded IRQs and handlers

Benno Lossin
2025-07-08T14:33:27Z

On Tue Jul 8, 2025 at 1:49 PM CEST, Alice Ryhl wrote:
> On Mon, Jul 07, 2025 at 10:30:27PM +0200, Benno Lossin wrote:
>> On Mon Jul 7, 2025 at 6:18 PM CEST, Daniel Almeida wrote:
>> > Alice,
>> >
>> > […]
>> >
>> >>> +/// The value that can be returned from an IrqHandler or a ThreadedIrqHandler.
>> >>> +pub enum IrqReturn {
>> >>> + /// The interrupt was not from this device or was not handled.
>> >>> + None,
>> >>> +
>> >>> + /// The interrupt was handled by this device.
>> >>> + Handled,
>> >>> +}
>> >>> +
>> >>> +impl IrqReturn {
>> >>> + fn into_inner(self) -> u32 {
>> >>> + match self {
>> >>> + IrqReturn::None => bindings::irqreturn_IRQ_NONE,
>> >>> + IrqReturn::Handled => bindings::irqreturn_IRQ_HANDLED,
>> >>
>> >> One option is to specify these in the enum:
>> >>
>> >> /// The value that can be returned from an IrqHandler or a ThreadedIrqHandler.
>> >> pub enum IrqReturn {
>> >> /// The interrupt was not from this device or was not handled.
>> >> None = bindings::irqreturn_IRQ_NONE,
>> >>
>> >> /// The interrupt was handled by this device.
>> >> Handled = bindings::irqreturn_IRQ_HANDLED,
>> >> }
>> >
>> > This requires explicitly setting #[repr(u32)], which is something that was
>> > reverted at an earlier iteration of the series on Benno’s request.
>>
>> Yeah I requested this, because it increases the size of the enum to 4
>> bytes and I think we should try to make rust enums as small as possible.
>>
>> @Alice what's the benefit of doing it directly in the enum?
>
> Basically all uses of this enum are going to look like this:
>
> fn inner() -> IrqReturn {
> if !should_handle() {
> return IrqReturn::None;
> }
> // .. handle the irq
> IrqReturn::Handled
> }
>
> fn outer() -> u32 {
> match inner() {
> IrqReturn::None => bindings::irqreturn_IRQ_NONE,
> IrqReturn::Handled => bindings::irqreturn_IRQ_HANDLED,
> }
> }
>
> Setting the discriminant to the values ensures that the match in outer()
> is a no-op. The enum is never going to be stored long-term anywhere so
> its size doesn't matter.

Hmm in this particular case, I think the optimizer will be able to
remove the additional branch too. But I haven't checked.

I usually give the advice to not explicitly set the discriminants and
let the compiler do it. I don't have a strong opinion on this, but we
should figure out which one is better in which cases.

---
Cheers,
Benno