Re: [PATCH 1/2] lockdep: improve current->(hard|soft)irqs_enabled synchronisation with actual irq state

From: peterz
Date: Sun Jul 26 2020 - 08:11:50 EST


On Sun, Jul 26, 2020 at 02:14:34PM +1000, Nicholas Piggin wrote:
> Excerpts from Peter Zijlstra's message of July 26, 2020 6:26 am:

> > Which is 'funny' when it interleaves like:
> >
> > local_irq_disable();
> > ...
> > local_irq_enable()
> > trace_hardirqs_on();
> > <NMI/>
> > raw_local_irq_enable();
> >
> > Because then it will undo the trace_hardirqs_on() we just did. With the
> > result that both tracing and lockdep will see a hardirqs-disable without
> > a matching enable, while the hardware state is enabled.
>
> Seems like an arch problem -- why not disable if it was enabled only?
> I guess the local_irq tracing calls are a mess so maybe they copied
> those.

Because, as I wrote earlier, then we can miss updating software state.
So your proposal has:

raw_local_irq_disable()
<NMI>
if (!arch_irqs_disabled(regs->flags) // false
trace_hardirqs_off();

// tracing/lockdep still think IRQs are enabled
// hardware IRQ state is disabled.

With the current code we have:

local_irq_enable()
trace_hardirqs_on();
<NMI>
trace_hardirqs_off();
...
if (!arch_irqs_disabled(regs->flags)) // false
trace_hardirqs_on();
</NMI>
// and now the NMI disabled software state again
// while we're about to enable the hardware state
raw_local_irq_enable();

> > Which is exactly the state Alexey seems to have ran into.
>
> No his was what I said, the interruptee's trace_hardirqs_on() in
> local_irq_enable getting lost because the NMI's local_irq_disable
> always disables, but the enable doesn't re-enable.

That's _exactly_ the case above. It doesn't re-enable because hardirqs
are actually still disabled. You _cannot_ rely on hardirq state for
NMIs, that'll get you wrong state.

> It's all just weird asymmetrical special case hacks AFAIKS, the
> code should just be symmetric and lockdep handle it's own weirdness.

It's for non-maskable exceptions/interrupts, because there the hardware
and software state changes non-atomically. For maskable interrupts doing
the software state transitions inside the disabled region makes perfect
sense, because that keeps it atomic.