Re: [POC 1/6] irq & spin_lock: Add counted interrupt disabling/enabling

From: Thomas Gleixner
Date: Thu Oct 24 2024 - 04:20:57 EST


On Wed, Oct 23 2024 at 22:05, Boqun Feng wrote:
> On Wed, Oct 23, 2024 at 09:34:27PM +0200, Thomas Gleixner wrote:
>> local_interrupt_enable()
>> {
>> if ((preempt_count() & LOCALIRQ_MASK) == LOCALIRQ_OFFSET) {
>> local_irq_restore(this_cpu_read(...flags);
>> preempt_count_sub_test_resched(LOCALIRQ_OFFSET);
>> } else {
>> // Does not need a resched test because it's not going
>> // to 0
>> preempt_count_sub(LOCALIRQ_OFFSET);
>> }
>> }
>>
>
> Yes, this looks nice, one tiny problem is that it requires
> PREEMPT_COUNT=y ;-) Maybe we can do: if PREEMPT_COUNT=y, we use preempt
> count, otherwise use a percpu?
>
> Hmm... but this will essentially be: we have a irq_disable_count() which
> is always built-in, and we also uses it as preempt count if
> PREEMPT_COUNT=y. This doesn't look too bad to me.

The preempt counter is always there even when PREEMPT_COUNT=n. It's
required for tracking hard/soft interrupt and NMI context.

The only difference is that preempt_disable()/enable() are NOOPs. So in
that case preempt_count_sub_test_resched() becomes a plain preempt_count_sub().

>> and then the lock thing becomes
>>
>> spin_lock_irq_disable()
>> {
>> local_interrupt_disable();
>> lock();
>> }
>>
>> spin_unlock_irq_enable()
>> {
>> unlock();
>> local_interrupt_enable();
>> }
>>
>> instead having to do:
>>
>> spin_unlock_irq_enable()
>> {
>> unlock();
>> local_interrupt_enable();
>> preempt_enable();
>> }
>>
>> Which needs two distinct checks, one for the interrupt and one for the
>
> No? Because now since we fold the interrupt disable count into preempt
> count, so we don't need to care about preempt count any more if we we
> local_interrupt_{disable,enable}(). For example, in the above
> local_interrupt_enable(), interrupts are checked at local_irq_restore()
> and preemption is checked at preempt_count_sub_test_resched(). Right?

Correct. That's what I pointed out. By folding it into preempt count
this becomes one operation, while in your POC it's two distinct checks
and operations.

Thanks,

tglx