Skip to contentSkip to conversation

Possible race condition in i386 global_irq_lock handling.

DISCUSSION TeJun Huh 2 participants 3 messages Aug 21, 2003

lore
Share & tools
lore

TeJun Huh

author
#0 lore raw

I've been reading i386 interrupt handling code for a couple of days
and encountered something that looks like a race condition. It's between include/asm-i386/hardirq.h:irq_enter() and arch/i386/kernel/irq.c:get_irqlock(). They seem to be using lockless synchronization with local_irq_count of each cpu and global_irq_lock variable.

A. locking CPU

1. Do test_and_set_bit() on global_irq_lock, if fail, repeat.
2. If all local_irq_count's are zero, we're the winner. Check other stuff; otherwise, clear global_irq_lock and retry.

B. other CPUs

1. Increment local_irq_count
2. test_bit() on global_irq_lock, if zero, continue handling interrupt; otherwise, wait till it's cleared.

For this to work, the locking CPU should fetch the value of
local_irq_count after global_irq_lock value becomes visible to other CPUs, and other CPUs should fetch the value of global_irq_lock after making the incremented local_irq_count visible to other CPUs.

The locking CPU is OK because test_and_set_bit() forces ordering on
x86, but there should be a mb() betweewn step 1 and 2 for other CPUs because none of ++ and test_bit is ordering. The B part is irq_enter() in hardirq.h which looks like the following.

static inline void irq_enter(int cpu, int irq)

{
	++local_irq_count(cpu);

	while (test_bit(0,&global_irq_lock)) {
		cpu_relax();
	}
}

Is it a race condition or am I getting it horribly wrong? Thx in
advance.

#1 lore raw
Quoted 6 lines · TeJun Huh

I've been reading i386 interrupt handling code for a couple of days
and encountered something that looks like a race condition. It's between include/asm-i386/hardirq.h:irq_enter() and arch/i386/kernel/irq.c:get_irqlock(). They seem to be using lockless synchronization with local_irq_count of each cpu and global_irq_lock variable.

Ok 2.4 (but for future try and mention which kernel version). You'll have to forgive me if i misunderstand you..

Quoted 5 lines

A. locking CPU

1. Do test_and_set_bit() on global_irq_lock, if fail, repeat.
2. If all local_irq_count's are zero, we're the winner. Check other stuff; otherwise, clear global_irq_lock and retry.

Are you referring to hardirq_trylock()?

Quoted 10 lines

B. other CPUs

1. Increment local_irq_count
2. test_bit() on global_irq_lock, if zero, continue handling interrupt; otherwise, wait till it's cleared.

For this to work, the locking CPU should fetch the value of
local_irq_count after global_irq_lock value becomes visible to other CPUs, and other CPUs should fetch the value of global_irq_lock after making the incremented local_irq_count visible to other CPUs.

Why after? it's currently in an interrupt anyway, the local_irq_count is per cpu so it's not used on other cpus why do you need to make it visible on other processors? (save irqs_running() but even that's ok)

Quoted 14 lines

The locking CPU is OK because test_and_set_bit() forces ordering on
x86, but there should be a mb() betweewn step 1 and 2 for other CPUs because none of ++ and test_bit is ordering. The B part is irq_enter() in hardirq.h which looks like the following.

static inline void irq_enter(int cpu, int irq)

{
	++local_irq_count(cpu);

	while (test_bit(0,&global_irq_lock)) {
		cpu_relax();
	}
}
Quoted 2 lines

Is it a race condition or am I getting it horribly wrong? Thx in
advance.

I don't see or understand the race condition you're describing, local_irq_count is per cpu.

Zwane

author
#2 lore raw
Quoted 3 lines · Zwane Mwaikambo

Ok 2.4 (but for future try and mention which kernel version). You'll have to forgive me if i misunderstand you..

The version I'm looking at is 2.4.21. Sorry about forgetting to
mention.

Quoted 10 lines

Are you referring to hardirq_trylock()?
...cut...

Quoted 4 lines

For this to work, the locking CPU should fetch the value of
local_irq_count after global_irq_lock value becomes visible to other CPUs, and other CPUs should fetch the value of global_irq_lock after making the incremented local_irq_count visible to other CPUs.

Why after? it's currently in an interrupt anyway, the local_irq_count is per cpu so it's not used on other cpus why do you need to make it visible on other processors? (save irqs_running() but even that's ok)

I'm talking about global_irq_lock synchronization. local_irq_count
_is_ local but used to synchronize global irq lock. Sparc uses big reader lock for this purpose but x86 code seems to use memory-ordered lockless synchronization.

I'll describe it in more detail. On MP, cli() is __global_cli(),
which in turn calls get_irqlock(). get_irqlock() uses test_and_set_bit() and wait_on_irq() to achieve global irq locking. The counterpart of this locking is irq_enter() and irq_exit(). Simplified version of the mechanism is as following.

A. get_irqlock() -> wait_on_irq()

1. Repeat test_and_set_bit(0, &global_irq_lock) until we're the winner.
2. Test if all local_irq_count's are zero. If there is any non-zero value, the CPU might have entered interrupt handler already. Clear global_irq_lock and go back to step 1.

=> If the test succeeded, we should be sure that no other cpu is
running an interrupt handler and none will enter interrupt handler until global_irq_lock is cleared.

B. irq_enter()

1. Increment local_irq_count.
2. Do test_bit(0, &global_irq_lock). If it's set, someone is trying to grab or have grabbed global_irq_lock, loop until it gets cleared. If global_irq_lock is clear, the CPU enters interrupt handler.

The race condition occurs because there is no mb() between step 1 and
2 of irq_enter(). Example scenarios would be

[AM]: atomic & memory barrier
[L] : local to cpu (not yet visible to other cpus)
[G] : became global

	A				B

calls cli() Interrupt occurs
executing get_irqlock() executing irq_enter()

** Scenario #1

				[L]++local_irq_counter
				fetch global_irq_lock

[AM]set global_irq_lock test global_irq_lock
fetch local_irq_counter
test local_irq_counter [G]++local_irq_counter

** Scenario #2
fetch global_irq_lock
[AM]set global_irq_lock
fetch local_irq_counter
test local_irq_counter [L]++local_irq_counter

				[G]++local_irq_counter
				test global_irq_lock

On above scenarios, B enters interrupt handler and A returns
successfully from cli() - B will be executing an interrupt handler while A is inside cli(), sti() critical section. This occurs because there is nothing which forces fetching of global_irq_lock occur after making local_irq_counter increment visible to other cpus.

If I misunderstood the synchronization mechanism or architectural
characteristics, please point out.