Skip to contentSkip to conversation

preempt_count corruption across H_CEDE call with CONFIG_PREEMPT on pseries

RFC PATCH In review Darren Hart 9 participants 35 messages Jul 22, 2010 – Sep 3, 2010 PowerPC

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 preempt_count corruption across H_CEDE call with CONFIG_PREEMPT on pseries

While testing CPU offline/online, we hit various preempt_count related bugs. Various hacks have been employed for several theoretical corner cases. One situation however is perfectly repeatable on 2.6.33.6 with CONFIG_PREEMPT=y.

BUG: scheduling while atomic: swapper/0/0x00000065
Modules linked in: autofs4 sunrpc ipv6 dm_mirror dm_region_hash dm_log
dm_mod ehea sg ext4 jbd2 mbcache sd_mod crc_t10dif ibmvscsic
scsi_transport_srp scsi_tgt [last unloaded: scsi_wait_scan]
Call Trace:
[c00000010e9e39f0] [c0000000000144d4] .show_stack+0x74/0x1c0 (unreliable)
[c00000010e9e3aa0] [c00000000007a680] .__schedule_bug+0xa0/0xb0
[c00000010e9e3b30] [c00000000056dea4] .schedule+0x7a4/0xd60
[c00000010e9e3cd0] [c000000000016be8] .cpu_idle+0x1f8/0x220
[c00000010e9e3d80] [c00000000057d858] .start_secondary+0x388/0x3c0
[c00000010e9e3e30] [c000000000008278] .start_secondary_resume+0x10/0x14

With some instrumentation we were able to determine that the preempt_count() appears to change across the extended_cede_processor() call. Specifically across the plpar_hcall_norets(H_CEDE) call. On PREEMPT_RT we call this with preempt_count=1 and return with preempt_count=0xffffffff. On mainline with CONFIG_PREEMPT=y, the value is different (0x65) but is still incorrect.

Also of interest is that this path cpu_idle()->cpu_die()->pseries_mach_cpu_die() to start_secondary() enters with a preempt_count=1 if it wasn't corrupted across the hcall. The early boot path from _start however appears to call start_secondary() with a preempt_count of 0.

The following patch is most certainly not correct, but it does eliminate the situation on mainline 100% of the time (there is still a 25% reproduction rate on PREEMPT_RT). Can someone comment on:

1) How can the preempt_count() get mangled across the H_CEDE hcall?
2) Should we call preempt_enable() in cpu_idle() prior to cpu_die() ?

arch/powerpc/platforms/pseries/hotplug-cpu.c +1 -0
Index: linux-2.6.33.6/arch/powerpc/platforms/pseries/hotplug-cpu.c
===================================================================
@@ -138,6 +138,7 @@ static void pseries_mach_cpu_die(void)
 			 * Kernel stack will be reset and start_secondary()
 			 * will be called to continue the online operation.
 			 */
+			preempt_count() = 0;
 			start_secondary_resume();
 		}
 	}
Signature · 3 lines
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team
#1 lore raw
Quoted 3 lines · Darren Hart

The following patch is most certainly not correct, but it does eliminate the situation on mainline 100% of the time (there is still a 25% reproduction rate on PREEMPT_RT). Can someone comment on:

Apologies. This particular issue is also 100% eliminated on PREEMPT_RT. We hit another issue possibly unrelated to this 25% of time. Please disregard the comment regarding 25% failure on PREEMPT_RT.

Signature · 3 lines
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team
#2 lore raw
In reply to Darren Hart
Quoted 3 lines · Darren Hart

Also of interest is that this path cpu_idle()->cpu_die()->pseries_mach_cpu_die() to start_secondary() enters with a preempt_count=1 if it wasn't corrupted across the hcall.

That triggers the problem as well. preempt_count needs to be 0 when entering start_secondary(). So I really wonder how that ever worked.

Quoted 2 lines

The early boot path from _start however appears to call start_secondary() with a preempt_count of 0.

Which is correct.

Quoted 1 line

The following patch is most certainly not correct, but it does eliminate

It is correct, but i think it is incomplete as other portions of the thread_info on the stack might be in some weird state as well.

Quoted 2 lines

the situation on mainline 100% of the time (there is still a 25% reproduction rate on PREEMPT_RT).

But those are diffferent issues, for which we have reasonable explanations and patches/workarounds.

Quoted 1 line

2) Should we call preempt_enable() in cpu_idle() prior to cpu_die() ?

No

Thanks,

tglx

#3 lore raw
Quoted 12 lines · Thomas Gleixner
Quoted 3 lines · Darren Hart

Also of interest is that this path cpu_idle()->cpu_die()->pseries_mach_cpu_die() to start_secondary() enters with a preempt_count=1 if it wasn't corrupted across the hcall.

That triggers the problem as well. preempt_count needs to be 0 when entering start_secondary(). So I really wonder how that ever worked.

Quoted 2 lines

The early boot path from _start however appears to call start_secondary() with a preempt_count of 0.

Quoted 6 lines

Which is correct.

Quoted 1 line

The following patch is most certainly not correct, but it does eliminate

It is correct, but i think it is incomplete as other portions of the thread_info on the stack might be in some weird state as well.

Just FYI, I took a look at the stack pointers as well as all the fields in the thread_info struct. The only one that changes is preempt_count. The previous value of preempt_count doesn't impact the value after cede. An initial value of 0, 1, or 4 all result in an after-cede value of 0xffffffff. I also added 32 bits of padding on either side of the preempt_count in case the change was accidental - it wasnt', the padded values remained unchanged across the cede call while the preempt_count still changed to 0xffffffff.

Signature · 3 lines
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team
#4 lore raw
In reply to Darren Hart
Quoted 3 lines · Darren Hart

1) How can the preempt_count() get mangled across the H_CEDE hcall?
2) Should we call preempt_enable() in cpu_idle() prior to cpu_die() ?

The preempt count is on the thread info at the bottom of the stack.

Can you check the stack pointers ?

Cheers,
Ben.

#5 lore raw
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 3 lines · Darren Hart

1) How can the preempt_count() get mangled across the H_CEDE hcall?
2) Should we call preempt_enable() in cpu_idle() prior to cpu_die() ?

The preempt count is on the thread info at the bottom of the stack.

Can you check the stack pointers ?

Hi Ben, thanks for looking.

I instrumented the area around extended_cede_processor() as follows (please confirm I'm getting the stack pointer correctly).

while (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
	asm("mr %0,1" : "=r" (sp));
	printk("before H_CEDE current->stack: %lx, pcnt: %x\n", sp, preempt_count());  
	extended_cede_processor(cede_latency_hint);
	asm("mr %0,1" : "=r" (sp));
	printk("after H_CEDE current->stack: %lx, pcnt: %x\n", sp, preempt_count());

}

On Mainline (2.6.33.6, CONFIG_PREEMPT=y) I see this:

Jul 22 18:37:08 igoort1 kernel: before H_CEDE current->stack: c00000010e9e3ce0, pcnt: 1
Jul 22 18:37:08 igoort1 kernel: after H_CEDE current->stack:  c00000010e9e3ce0, pcnt: 1

This surprised me as preempt_count is 1 before and after, so no corruption appears to occur on mainline. This makes the pcnt of 65 I see without the preempt_count()=0 hack very strange. I ran several hundred off/on cycles. The issue of preempt_count being 1 is still addressed by this patch however.

On PREEMPT_RT (2.6.33.5-rt23 - tglx, sorry, rt/2.6.33 next time, promise):

Jul 22 18:51:11 igoort1 kernel: before H_CEDE current->stack: c000000089bcfcf0, pcnt: 1
Jul 22 18:51:11 igoort1 kernel: after H_CEDE current->stack:  c000000089bcfcf0, pcnt: ffffffff

In both cases the stack pointer appears unchanged.

Note: there is a BUG triggered in between these statements as the preempt_count causes the printk to trigger:
Badness at kernel/sched.c:5572

Thanks,

Signature · 3 lines
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team
#6 lore raw
Quoted 35 lines · Darren Hart
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 3 lines · Darren Hart

1) How can the preempt_count() get mangled across the H_CEDE hcall?
2) Should we call preempt_enable() in cpu_idle() prior to cpu_die() ?

The preempt count is on the thread info at the bottom of the stack.

Can you check the stack pointers ?

Hi Ben, thanks for looking.

I instrumented the area around extended_cede_processor() as follows (please confirm I'm getting the stack pointer correctly).

while (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
	asm("mr %0,1" : "=r" (sp));
	printk("before H_CEDE current->stack: %lx, pcnt: %x\n", sp, preempt_count());
	extended_cede_processor(cede_latency_hint);
	asm("mr %0,1" : "=r" (sp));
	printk("after H_CEDE current->stack: %lx, pcnt: %x\n", sp, preempt_count());

}

On Mainline (2.6.33.6, CONFIG_PREEMPT=y) I see this:

Jul 22 18:37:08 igoort1 kernel: before H_CEDE current->stack: c00000010e9e3ce0, pcnt: 1
Jul 22 18:37:08 igoort1 kernel: after H_CEDE current->stack:  c00000010e9e3ce0, pcnt: 1

This surprised me as preempt_count is 1 before and after, so no corruption appears to occur on mainline. This makes the pcnt of 65 I see without the preempt_count()=0 hack very strange. I ran several hundred off/on cycles. The issue of preempt_count being 1 is still addressed by this patch however.

On PREEMPT_RT (2.6.33.5-rt23 - tglx, sorry, rt/2.6.33 next time, promise):

Quoted 8 lines
Jul 22 18:51:11 igoort1 kernel: before H_CEDE current->stack: c000000089bcfcf0, pcnt: 1
Jul 22 18:51:11 igoort1 kernel: after H_CEDE current->stack:  c000000089bcfcf0, pcnt: ffffffff

In both cases the stack pointer appears unchanged.

Note: there is a BUG triggered in between these statements as the preempt_count causes the printk to trigger:
Badness at kernel/sched.c:5572

At Steven's suggestion I updated the instrumentation to display the local_save_flags and irqs_disabled_flags:

while (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
        local_save_flags(flags);
        printk("local flags: %lx, irqs disabled: %d\n", flags, irqs_disabled_flags(flags));
        asm("mr %0,1" : "=r" (sp));
        printk("before H_CEDE current->stack: %lx, pcnt: %x\n", sp, preempt_count());
        extended_cede_processor(cede_latency_hint);
        asm("mr %0,1" : "=r" (sp));
        printk("after H_CEDE current->stack: %lx, pcnt: %x\n", sp, preempt_count());
}
Jul 22 23:36:58 igoort1 kernel: local flags: 0, irqs disabled: 1
Jul 22 23:36:58 igoort1 kernel: before H_CEDE current->stack: c00000010e9e3ce0, pcnt: 1
Jul 22 23:36:58 igoort1 kernel: after H_CEDE current->stack: c00000010e9e3ce0, pcnt: 1

I'm not sure if I'm reading that right, but I believe interrupts are intended to be disabled here. If accomplished via the spin_lock_irqsave() this would behave differently on RT. However, this path disables the interrupts handled by xics, all but the IPIs anyway. On RT I disabled the decrementer as well.

Is it possible for RT to be receiving other interrupts here?

Signature · 3 lines
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team
#7 lore raw
Quoted 63 lines · Darren Hart
Quoted 35 lines · Darren Hart
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 3 lines · Darren Hart

1) How can the preempt_count() get mangled across the H_CEDE hcall?
2) Should we call preempt_enable() in cpu_idle() prior to cpu_die() ?

The preempt count is on the thread info at the bottom of the stack.

Can you check the stack pointers ?

Hi Ben, thanks for looking.

I instrumented the area around extended_cede_processor() as follows (please confirm I'm getting the stack pointer correctly).

while (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
	asm("mr %0,1" : "=r" (sp));
	printk("before H_CEDE current->stack: %lx, pcnt: %x\n", sp, preempt_count());
	extended_cede_processor(cede_latency_hint);
	asm("mr %0,1" : "=r" (sp));
	printk("after H_CEDE current->stack: %lx, pcnt: %x\n", sp, preempt_count());

}

On Mainline (2.6.33.6, CONFIG_PREEMPT=y) I see this:

Jul 22 18:37:08 igoort1 kernel: before H_CEDE current->stack: c00000010e9e3ce0, pcnt: 1
Jul 22 18:37:08 igoort1 kernel: after H_CEDE current->stack:  c00000010e9e3ce0, pcnt: 1

This surprised me as preempt_count is 1 before and after, so no corruption appears to occur on mainline. This makes the pcnt of 65 I see without the preempt_count()=0 hack very strange. I ran several hundred off/on cycles. The issue of preempt_count being 1 is still addressed by this patch however.

On PREEMPT_RT (2.6.33.5-rt23 - tglx, sorry, rt/2.6.33 next time, promise):

Quoted 8 lines
Jul 22 18:51:11 igoort1 kernel: before H_CEDE current->stack: c000000089bcfcf0, pcnt: 1
Jul 22 18:51:11 igoort1 kernel: after H_CEDE current->stack:  c000000089bcfcf0, pcnt: ffffffff

In both cases the stack pointer appears unchanged.

Note: there is a BUG triggered in between these statements as the preempt_count causes the printk to trigger:
Badness at kernel/sched.c:5572

At Steven's suggestion I updated the instrumentation to display the local_save_flags and irqs_disabled_flags:

while (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
        local_save_flags(flags);
        printk("local flags: %lx, irqs disabled: %d\n", flags, irqs_disabled_flags(flags));
        asm("mr %0,1" : "=r" (sp));
        printk("before H_CEDE current->stack: %lx, pcnt: %x\n", sp, preempt_count());
        extended_cede_processor(cede_latency_hint);
        asm("mr %0,1" : "=r" (sp));
        printk("after H_CEDE current->stack: %lx, pcnt: %x\n", sp, preempt_count());
}
Jul 22 23:36:58 igoort1 kernel: local flags: 0, irqs disabled: 1
Jul 22 23:36:58 igoort1 kernel: before H_CEDE current->stack: c00000010e9e3ce0, pcnt: 1
Jul 22 23:36:58 igoort1 kernel: after H_CEDE current->stack: c00000010e9e3ce0, pcnt: 1
Quoted 7 lines

I'm not sure if I'm reading that right, but I believe interrupts are intended to be disabled here. If accomplished via the spin_lock_irqsave() this would behave differently on RT. However, this path disables the interrupts handled by xics, all but the IPIs anyway. On RT I disabled the decrementer as well.

Is it possible for RT to be receiving other interrupts here?

Yes. extended_cede_processor() will return with interrupts enabled in the cpu. (This is done by the hypervisor). Under normal cases we cannot be interrupted because no IO interrupts are routed to us after xics_teardown_cpu() and since the CPU is out of the map, nobody will send us IPIs.

Though H_CEDE will return with interrupts enabled, it is unlikely that an interrupt can be delivered in this context.

--Vaidy

#8 lore raw
Quoted 5 lines · Vaidyanathan Srinivasan

Yes. extended_cede_processor() will return with interrupts enabled in the cpu. (This is done by the hypervisor). Under normal cases we cannot be interrupted because no IO interrupts are routed to us after xics_teardown_cpu() and since the CPU is out of the map, nobody will send us IPIs.

What about decrementer ?

Quoted 2 lines

Though H_CEDE will return with interrupts enabled, it is unlikely that an interrupt can be delivered in this context.

Well, if interrupts are soft-disabled, even if one occurs, we will just mask and return, so that at least should be ok.

Cheers,
Ben.

#9 lore raw
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 5 lines · Vaidyanathan Srinivasan

Yes. extended_cede_processor() will return with interrupts enabled in the cpu. (This is done by the hypervisor). Under normal cases we cannot be interrupted because no IO interrupts are routed to us after xics_teardown_cpu() and since the CPU is out of the map, nobody will send us IPIs.

What about decrementer ?

Decrementer expiry event handling is bit complex. The event as such may not bring back the extended_cede_processor() cpu, but may be marked pending when we get out of this state eventually. I will find more information on this event and update.

Quoted 5 lines
Quoted 2 lines

Though H_CEDE will return with interrupts enabled, it is unlikely that an interrupt can be delivered in this context.

Well, if interrupts are soft-disabled, even if one occurs, we will just mask and return, so that at least should be ok.

Yes. We will immediately return to the extended_cede_processor() in the while loop until the preferred_offline_state is changed.

--Vaidy

#10 lore raw
Quoted 9 lines · Vaidyanathan Srinivasan
Quoted 7 lines · Benjamin Herrenschmidt
Quoted 5 lines · Vaidyanathan Srinivasan

Yes. extended_cede_processor() will return with interrupts enabled in the cpu. (This is done by the hypervisor). Under normal cases we cannot be interrupted because no IO interrupts are routed to us after xics_teardown_cpu() and since the CPU is out of the map, nobody will send us IPIs.

Quoted 6 lines
Quoted 1 line

What about decrementer ?

Decrementer expiry event handling is bit complex. The event as such may not bring back the extended_cede_processor() cpu, but may be marked pending when we get out of this state eventually. I will find more information on this event and update.

Hi Vaidy, have you been able to dig anything up about the decrementer expiry?

Thanks,

Signature · 3 lines
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team
Quoted 11 lines
Quoted 6 lines
Quoted 3 lines

Though H_CEDE will return with interrupts enabled, it is unlikely that an interrupt can be delivered in this context.

Well, if interrupts are soft-disabled, even if one occurs, we will just mask and return, so that at least should be ok.

Yes. We will immediately return to the extended_cede_processor() in the while loop until the preferred_offline_state is changed.

--Vaidy

#11 lore raw
Quoted 12 lines · Darren Hart
Quoted 11 lines · Vaidyanathan Srinivasan
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 5 lines · Vaidyanathan Srinivasan

Yes. extended_cede_processor() will return with interrupts enabled in the cpu. (This is done by the hypervisor). Under normal cases we cannot be interrupted because no IO interrupts are routed to us after xics_teardown_cpu() and since the CPU is out of the map, nobody will send us IPIs.

What about decrementer ?

Quoted 7 lines
Quoted 4 lines

Decrementer expiry event handling is bit complex. The event as such may not bring back the extended_cede_processor() cpu, but may be marked pending when we get out of this state eventually. I will find more information on this event and update.

Hi Vaidy, have you been able to dig anything up about the decrementer expiry?

Hi Darren,

Yes, it is possible that the cpu in extended_cede_processor() can be woken up by the decrementer. But the expectation is that we will return back to this context since we will not have any pending timers.

Our stack should not change even if we get the decrementer interrupts.

--Vaidy

#12 lore raw
Quoted 19 lines · Vaidyanathan Srinivasan
Quoted 17 lines · Darren Hart
Quoted 15 lines · Vaidyanathan Srinivasan
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 5 lines · Vaidyanathan Srinivasan

Yes. extended_cede_processor() will return with interrupts enabled in the cpu. (This is done by the hypervisor). Under normal cases we cannot be interrupted because no IO interrupts are routed to us after xics_teardown_cpu() and since the CPU is out of the map, nobody will send us IPIs.

What about decrementer ?

Decrementer expiry event handling is bit complex. The event as such may not bring back the extended_cede_processor() cpu, but may be marked pending when we get out of this state eventually. I will find more information on this event and update.

Quoted 8 lines
Quoted 2 lines

Hi Vaidy, have you been able to dig anything up about the decrementer expiry?

Hi Darren,

Yes, it is possible that the cpu in extended_cede_processor() can be woken up by the decrementer. But the expectation is that we will return back to this context since we will not have any pending timers.

The problem is that you might get woken _before_ the timers have been migrated away.

Quoted 1 line

Our stack should not change even if we get the decrementer interrupts.

But you should not execute kernel code when you got offlined either.

Thanks,

tglx

#13 lore raw
In reply to Darren Hart
Quoted 7 lines · Darren Hart

suggestion I updated the instrumentation to display the
local_save_flags and irqs_disabled_flags:

Jul 22 23:36:58 igoort1 kernel: local flags: 0, irqs disabled: 1
Jul 22 23:36:58 igoort1 kernel: before H_CEDE current->stack: c00000010e9e3ce0, pcnt: 1
Jul 22 23:36:58 igoort1 kernel: after H_CEDE current->stack: c00000010e9e3ce0, pcnt: 1
Quoted 7 lines

I'm not sure if I'm reading that right, but I believe interrupts are intended to be disabled here. If accomplished via the spin_lock_irqsave() this would behave differently on RT. However, this path disables the interrupts handled by xics, all but the IPIs anyway. On RT I disabled the decrementer as well.

Is it possible for RT to be receiving other interrupts here?

Also you may want to call hard_irq_disable() to -really- disable interrupts ... since we do lazy-disable on powerpc

Cheers,
Ben.

#14 lore raw
Quoted 15 lines · Benjamin Herrenschmidt
Quoted 13 lines · Darren Hart

suggestion I updated the instrumentation to display the
local_save_flags and irqs_disabled_flags:

Jul 22 23:36:58 igoort1 kernel: local flags: 0, irqs disabled: 1
Jul 22 23:36:58 igoort1 kernel: before H_CEDE current->stack: c00000010e9e3ce0, pcnt: 1
Jul 22 23:36:58 igoort1 kernel: after H_CEDE current->stack: c00000010e9e3ce0, pcnt: 1

I'm not sure if I'm reading that right, but I believe interrupts are intended to be disabled here. If accomplished via the spin_lock_irqsave() this would behave differently on RT. However, this path disables the interrupts handled by xics, all but the IPIs anyway. On RT I disabled the decrementer as well.

Quoted 5 lines
Quoted 1 line

Is it possible for RT to be receiving other interrupts here?

Also you may want to call hard_irq_disable() to -really- disable interrupts ... since we do lazy-disable on powerpc

A quick update and request for direction wrt the cede hcall, interrupt handling, and the stack pointer.

I've added patches one at a time, eliminating bugs with preempt_rt (tip/rt/head). With my current patchset I have no crashes with a single run of random_online.sh (stress testing to hit the hang will sees is my todo for tomorrow).

Current patchset includes:
patches/0001-wms-fix01.patch # P7 lazy flushing thing

# next four are sent to / queued for mainline
patches/powerpc-increase-pseries_cpu_die-delay.patch
patches/powerpc-enable-preemption-before-cpu_die.patch
patches/powerpc-silence-__cpu_up-under-normal-operation.patch
patches/powerpc-silence-xics_migrate_irqs_away-during-cpu-offline.patch
# this one needs to be cleaned up and sent to mainline
patches/powerpc-wait-for-cpu-to-go-inactive.patch

patches/powerpc-disable-decrementer-on-offline.patch patches/powerpc-cpu_die-preempt-hack.patch # reset preempt_count to 0 after cede patches/powerpc-cede-processor-inst.patch # instrumentation patches/powerpc-hard_irq_disable.patch # hard_irq_disable before cede patches/powerpc-pad-thread_info.patch

I didn't include all the patches as the relevant bits are included below in code form.

With the instrumentation, it's clear the change to preempt_count() is targeted (since I added padding before and after preempt_count in the thread_info struct) and preempt_count is still changed. It is also clear that it isn't just a stray decrement since I set preempt_count() to 4 prior to calling cede and it still is 0xffffffff after the hcall. Also note that the stack pointer doesn't change across the cede call and neither does any other part of the thread_info structure.

Adding hard_irq_disable() did seem to affect things somewhat. Rather than a serialized list of before/after thread_info prints around cede, I see several befores then several afters. But the preempt_count is still modified.

The relevant code looks like this (from pseries_mach_cpu_die())

                        hard_irq_disable(); /* this doesn't fix things... but
			                       does change behavior a bit */
                        preempt_count() = 0x4; 
                        asm("mr %0,1" : "=r" (sp));  /* stack pointer is in R1 */
                        printk("before cede: sp=%lx pcnt=%x\n", sp, preempt_count()); 
                        print_thread_info(); 
                        extended_cede_processor(cede_latency_hint); 
                        asm("mr %0,1" : "=r" (sp)); 
                        printk("after cede: sp=%lx pcnt=%x\n", sp, preempt_count()); 
                        print_thread_info(); 
                        preempt_count() = 0; 

With these patches applied, the output across cede looks like:

before cede: sp=c000000000b57150 pcnt=4
*** current->thread_info ***
ti->task: c000000000aa1410
ti->exec_domain: c000000000a59958
ti->cpu: 0
ti->stomp_on_me: 57005 /* 0xDEAD - forgot to print in hex */ ti->preempt_count: 4
ti->stomp_on_me_too: 48879 /* 0xBEEF - forgot to print in hex */ ti->local_flags: 0
*** end thread_info ***
after cede: sp=c000000000b57150 pcnt=ffffffff
*** current->thread_info ***
ti->task: c000000000aa1410
ti->exec_domain: c000000000a59958
ti->cpu: 0
ti->stomp_on_me: 57005
ti->preempt_count: ffffffff
ti->stomp_on_me_too: 48879
ti->local_flags: 0
*** end thread_info ***

Are there any additional thoughts on what might be causing preempt_count to change? I was thinking that cede might somehow force it to 0 (or perhaps one of the preempt_count explicit value setters in irq.c) and then decrement it - but that dec should trigger an error in the dec_preempt_count() as I have CONFIG_DEBUG_PREEMPT=y.

...

Signature · 3 lines
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team
#15 lore raw
Quoted 79 lines · Darren Hart
Quoted 15 lines · Benjamin Herrenschmidt
Quoted 13 lines · Darren Hart

suggestion I updated the instrumentation to display the
local_save_flags and irqs_disabled_flags:

Jul 22 23:36:58 igoort1 kernel: local flags: 0, irqs disabled: 1
Jul 22 23:36:58 igoort1 kernel: before H_CEDE current->stack: c00000010e9e3ce0, pcnt: 1
Jul 22 23:36:58 igoort1 kernel: after H_CEDE current->stack: c00000010e9e3ce0, pcnt: 1

I'm not sure if I'm reading that right, but I believe interrupts are intended to be disabled here. If accomplished via the spin_lock_irqsave() this would behave differently on RT. However, this path disables the interrupts handled by xics, all but the IPIs anyway. On RT I disabled the decrementer as well.

Quoted 5 lines
Quoted 1 line

Is it possible for RT to be receiving other interrupts here?

Also you may want to call hard_irq_disable() to -really- disable interrupts ... since we do lazy-disable on powerpc

A quick update and request for direction wrt the cede hcall, interrupt handling, and the stack pointer.

I've added patches one at a time, eliminating bugs with preempt_rt (tip/rt/head). With my current patchset I have no crashes with a single run of random_online.sh (stress testing to hit the hang will sees is my todo for tomorrow).

Current patchset includes:
patches/0001-wms-fix01.patch # P7 lazy flushing thing

# next four are sent to / queued for mainline
patches/powerpc-increase-pseries_cpu_die-delay.patch
patches/powerpc-enable-preemption-before-cpu_die.patch
patches/powerpc-silence-__cpu_up-under-normal-operation.patch
patches/powerpc-silence-xics_migrate_irqs_away-during-cpu-offline.patch
# this one needs to be cleaned up and sent to mainline
patches/powerpc-wait-for-cpu-to-go-inactive.patch

patches/powerpc-disable-decrementer-on-offline.patch patches/powerpc-cpu_die-preempt-hack.patch # reset preempt_count to 0 after cede patches/powerpc-cede-processor-inst.patch # instrumentation patches/powerpc-hard_irq_disable.patch # hard_irq_disable before cede patches/powerpc-pad-thread_info.patch

I didn't include all the patches as the relevant bits are included below in code form.

With the instrumentation, it's clear the change to preempt_count() is targeted (since I added padding before and after preempt_count in the thread_info struct) and preempt_count is still changed. It is also clear that it isn't just a stray decrement since I set preempt_count() to 4 prior to calling cede and it still is 0xffffffff after the hcall. Also note that the stack pointer doesn't change across the cede call and neither does any other part of the thread_info structure.

Adding hard_irq_disable() did seem to affect things somewhat. Rather than a serialized list of before/after thread_info prints around cede, I see several befores then several afters. But the preempt_count is still modified.

The relevant code looks like this (from pseries_mach_cpu_die())

                        hard_irq_disable(); /* this doesn't fix things... but
			                       does change behavior a bit */
                        preempt_count() = 0x4; 
                        asm("mr %0,1" : "=r" (sp));  /* stack pointer is in R1 */
                        printk("before cede: sp=%lx pcnt=%x\n", sp, preempt_count()); 
                        print_thread_info(); 
                        extended_cede_processor(cede_latency_hint); 
                        asm("mr %0,1" : "=r" (sp)); 
                        printk("after cede: sp=%lx pcnt=%x\n", sp, preempt_count()); 
                        print_thread_info(); 
                        preempt_count() = 0; 

With these patches applied, the output across cede looks like:

Quoted 15 lines

before cede: sp=c000000000b57150 pcnt=4
*** current->thread_info ***
ti->task: c000000000aa1410
ti->exec_domain: c000000000a59958
ti->cpu: 0
ti->stomp_on_me: 57005 /* 0xDEAD - forgot to print in hex */ ti->preempt_count: 4
ti->stomp_on_me_too: 48879 /* 0xBEEF - forgot to print in hex */ ti->local_flags: 0
*** end thread_info ***
after cede: sp=c000000000b57150 pcnt=ffffffff
*** current->thread_info ***
ti->task: c000000000aa1410
ti->exec_domain: c000000000a59958
ti->cpu: 0

Is this print sufficient to prove that we did not jump CPU?

We agree that decrementer can possibly expire and we could have gone to the handler and come back just like we would do in an idle loop. This will not happen always, but I could see a window of time during which this may happen. But that should not change the preempt_count unconditionally to -1 with the same stack pointer.

Quoted 4 lines

ti->stomp_on_me: 57005
ti->preempt_count: ffffffff
ti->stomp_on_me_too: 48879
ti->local_flags: 0

Is there a method to identify whether we had executed any of the interrupt handler while in this code?

Quoted 6 lines

*** end thread_info ***

Are there any additional thoughts on what might be causing preempt_count to change? I was thinking that cede might somehow force it to 0 (or perhaps one of the preempt_count explicit value setters in irq.c) and then decrement it - but that dec should trigger an error in the dec_preempt_count() as I have CONFIG_DEBUG_PREEMPT=y.

Decrementer is only the suspect, we have not yet proved that the dec handler is being executed. Can somebody be using our stack? The pointer is same.. but do we still own it? I cannot think of how somebody else could be using this cpu's stack... but just a thought to explain this anomaly.

--Vaidy

#16 lore raw
Quoted 97 lines · Vaidyanathan Srinivasan
Quoted 79 lines · Darren Hart
Quoted 15 lines · Benjamin Herrenschmidt
Quoted 13 lines · Darren Hart

suggestion I updated the instrumentation to display the
local_save_flags and irqs_disabled_flags:

Jul 22 23:36:58 igoort1 kernel: local flags: 0, irqs disabled: 1
Jul 22 23:36:58 igoort1 kernel: before H_CEDE current->stack: c00000010e9e3ce0, pcnt: 1
Jul 22 23:36:58 igoort1 kernel: after H_CEDE current->stack: c00000010e9e3ce0, pcnt: 1

I'm not sure if I'm reading that right, but I believe interrupts are intended to be disabled here. If accomplished via the spin_lock_irqsave() this would behave differently on RT. However, this path disables the interrupts handled by xics, all but the IPIs anyway. On RT I disabled the decrementer as well.

Quoted 5 lines
Quoted 1 line

Is it possible for RT to be receiving other interrupts here?

Also you may want to call hard_irq_disable() to -really- disable interrupts ... since we do lazy-disable on powerpc

A quick update and request for direction wrt the cede hcall, interrupt handling, and the stack pointer.

I've added patches one at a time, eliminating bugs with preempt_rt (tip/rt/head). With my current patchset I have no crashes with a single run of random_online.sh (stress testing to hit the hang will sees is my todo for tomorrow).

Current patchset includes:
patches/0001-wms-fix01.patch # P7 lazy flushing thing

# next four are sent to / queued for mainline
patches/powerpc-increase-pseries_cpu_die-delay.patch
patches/powerpc-enable-preemption-before-cpu_die.patch
patches/powerpc-silence-__cpu_up-under-normal-operation.patch
patches/powerpc-silence-xics_migrate_irqs_away-during-cpu-offline.patch
# this one needs to be cleaned up and sent to mainline
patches/powerpc-wait-for-cpu-to-go-inactive.patch

patches/powerpc-disable-decrementer-on-offline.patch patches/powerpc-cpu_die-preempt-hack.patch # reset preempt_count to 0 after cede patches/powerpc-cede-processor-inst.patch # instrumentation patches/powerpc-hard_irq_disable.patch # hard_irq_disable before cede patches/powerpc-pad-thread_info.patch

I didn't include all the patches as the relevant bits are included below in code form.

With the instrumentation, it's clear the change to preempt_count() is targeted (since I added padding before and after preempt_count in the thread_info struct) and preempt_count is still changed. It is also clear that it isn't just a stray decrement since I set preempt_count() to 4 prior to calling cede and it still is 0xffffffff after the hcall. Also note that the stack pointer doesn't change across the cede call and neither does any other part of the thread_info structure.

Adding hard_irq_disable() did seem to affect things somewhat. Rather than a serialized list of before/after thread_info prints around cede, I see several befores then several afters. But the preempt_count is still modified.

The relevant code looks like this (from pseries_mach_cpu_die())

                         hard_irq_disable(); /* this doesn't fix things... but
			                       does change behavior a bit */
                         preempt_count() = 0x4;
                         asm("mr %0,1" : "=r" (sp));  /* stack pointer is in R1 */
                         printk("before cede: sp=%lx pcnt=%x\n", sp, preempt_count());
                         print_thread_info();
                         extended_cede_processor(cede_latency_hint);
                         asm("mr %0,1" : "=r" (sp));
                         printk("after cede: sp=%lx pcnt=%x\n", sp, preempt_count());
                         print_thread_info();
                         preempt_count() = 0;

With these patches applied, the output across cede looks like:

Quoted 15 lines

before cede: sp=c000000000b57150 pcnt=4
*** current->thread_info ***
ti->task: c000000000aa1410
ti->exec_domain: c000000000a59958
ti->cpu: 0
ti->stomp_on_me: 57005 /* 0xDEAD - forgot to print in hex */ ti->preempt_count: 4
ti->stomp_on_me_too: 48879 /* 0xBEEF - forgot to print in hex */ ti->local_flags: 0
*** end thread_info ***
after cede: sp=c000000000b57150 pcnt=ffffffff
*** current->thread_info ***
ti->task: c000000000aa1410
ti->exec_domain: c000000000a59958
ti->cpu: 0

Quoted 1 line

Is this print sufficient to prove that we did not jump CPU?

Probably not, but the following should be sufficient. Note that the dump occurs on "CPU: 6" which matches the CPU noted in the ti->cpu before and after the cede call.

*** current->thread_info ***
ti->task: c00000008e7b2240
ti->exec_domain: c000000000a59958
ti->cpu: 6
ti->stomp_on_me: 57005
ti->preempt_count: 4
ti->stomp_on_me_too: 48879
ti->local_flags: 0
*** end thread_info ***

------------[ cut here ]------------
Badness at kernel/sched.c:3698
NIP: c0000000006987e4 LR: c0000000006987c8 CTR: c00000000005c668
REGS: c00000010e713800 TRAP: 0700   Not tainted  (2.6.33-rt-dvhrt05-02358-g61223dd-dirty)
MSR: 8000000000021032 <ME,CE,IR,DR>  CR: 24000082  XER: 20000000

TASK = c00000008e7b2240[0] 'swapper' THREAD: c00000010e710000 CPU: 6
GPR00: 0000000000000000 c00000010e713a80 c000000000b54f88 0000000000000001
GPR04: c00000010e713d08 ffffffffffffffff 00000000000000e0 800000000d049138
GPR08: 0000000000000000 c000000000ca5420 0000000000000001 c000000000bc22e8
GPR12: 0000000000000002 c000000000ba5080 0000000000000000 000000000f394b6c
GPR16: 0000000000000000 0000000000000000 0000000000000000 0000000000000000
GPR20: c000000000ba50c0 0000000000000004 0000000000000002 0000000000000000
GPR24: 0000000000000004 c00000010e713cd0 c0000000009f1ecc c00000000088aaff
GPR28: 0000000000000000 0000000000000001 c000000000ad7610 c00000010e713a80 NIP [c0000000006987e4] .add_preempt_count+0x6c/0xe0
LR [c0000000006987c8] .add_preempt_count+0x50/0xe0

Call Trace:

[c00000010e713a80] [c00000010e713b30] 0xc00000010e713b30 (unreliable) [c00000010e713b10] [c0000000000824b4] .vprintk+0xac/0x480 [c00000010e713c40] [c00000000069bcbc] .printk+0x48/0x5c [c00000010e713cd0] [c00000000005c190] .pseries_mach_cpu_die+0x1c4/0x39c [c00000010e713db0] [c00000000001e7e0] .cpu_die+0x4c/0x68 [c00000010e713e30] [c000000000017de0] .cpu_idle+0x1f8/0x220 [c00000010e713ed0] [c0000000006a17c4] .start_secondary+0x394/0x3d4 [c00000010e713f90] [c000000000008264] .start_secondary_prolog+0x10/0x14 Instruction dump:
78290464 80090018 2f800000 40fc002c 4bd089c1 60000000 2fa30000 419e006c e93e87e0 80090000 2f800000 409e005c <0fe00000> 48000054 e97e8398 78290464 after cede: sp=c00000010e713cd0 pcnt=ffffffff
*** current->thread_info ***
ti->task: c00000008e7b2240
ti->exec_domain: c000000000a59958
ti->cpu: 6
ti->stomp_on_me: 57005
ti->preempt_count: fffffffe
ti->stomp_on_me_too: 48879
ti->local_flags: 0
*** end thread_info ***

Quoted 2 lines

We agree that decrementer can possibly expire and we could have gone to the handler and come back just like we would do in an idle loop.

I disable the decrementer in the stop_cpu path... that may not be sufficient for the pseries_mach_cpu_die() path, I'll have to experiment.

However, even if it did, it shouldn't be just changing the value, especially not to something illegal.

Quoted 11 lines

This will not happen always, but I could see a window of time during which this may happen. But that should not change the preempt_count unconditionally to -1 with the same stack pointer.

Quoted 4 lines

ti->stomp_on_me: 57005
ti->preempt_count: ffffffff
ti->stomp_on_me_too: 48879
ti->local_flags: 0

Is there a method to identify whether we had executed any of the interrupt handler while in this code?

I'd like to know as well. If anyone knows, please share. Otherwise I'll be trying to sort that out tomorrow.

Quoted 8 lines
Quoted 7 lines

*** end thread_info ***

Are there any additional thoughts on what might be causing preempt_count to change? I was thinking that cede might somehow force it to 0 (or perhaps one of the preempt_count explicit value setters in irq.c) and then decrement it - but that dec should trigger an error in the dec_preempt_count() as I have CONFIG_DEBUG_PREEMPT=y.

Quoted 7 lines

Decrementer is only the suspect, we have not yet proved that the dec handler is being executed. Can somebody be using our stack? The pointer is same.. but do we still own it? I cannot think of how somebody else could be using this cpu's stack... but just a thought to explain this anomaly.

--Vaidy

Thanks for the thoughts,

Signature · 3 lines
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team
#17 lore raw
In reply to Darren Hart

[email protected] wrote on 07/22/2010 06:57:18 PM:

Quoted 37 lines

Subject

Re: [PATCH][RFC] preempt_count corruption across H_CEDE call with CONFIG_PREEMPT on pseries

Quoted 8 lines · Benjamin Herrenschmidt
Quoted 3 lines · Darren Hart

1) How can the preempt_count() get mangled across the H_CEDE hcall?
2) Should we call preempt_enable() in cpu_idle() prior to cpu_die() ?

The preempt count is on the thread info at the bottom of the stack.

Can you check the stack pointers ?

Hi Ben, thanks for looking.

I instrumented the area around extended_cede_processor() as follows (please confirm I'm getting the stack pointer correctly).

while (get_preferred_offline_state(cpu) == CPU_STATE_INACTIVE) {
   asm("mr %0,1" : "=r" (sp));
   printk("before H_CEDE current->stack: %lx, pcnt: %x\n", sp,
preempt_count());
   extended_cede_processor(cede_latency_hint);
   asm("mr %0,1" : "=r" (sp));
   printk("after H_CEDE current->stack: %lx, pcnt: %x\n", sp,

preempt_count());
}

On Mainline (2.6.33.6, CONFIG_PREEMPT=y) I see this:

Jul 22 18:37:08 igoort1 kernel: before H_CEDE current->stack:
c00000010e9e3ce0, pcnt: 1
Jul 22 18:37:08 igoort1 kernel: after H_CEDE current->stack:

c00000010e9e3ce0, pcnt: 1

Quoted 7 lines

This surprised me as preempt_count is 1 before and after, so no corruption appears to occur on mainline. This makes the pcnt of 65 I see without the preempt_count()=0 hack very strange. I ran several hundred off/on cycles. The issue of preempt_count being 1 is still addressed by this patch however.

On PREEMPT_RT (2.6.33.5-rt23 - tglx, sorry, rt/2.6.33 next time,

promise):

Quoted 4 lines
Jul 22 18:51:11 igoort1 kernel: before H_CEDE current->stack:
c000000089bcfcf0, pcnt: 1
Jul 22 18:51:11 igoort1 kernel: after H_CEDE current->stack:

c000000089bcfcf0, pcnt: ffffffff

I'm not seeing the preempt_count value corrupted with my current set of debug, however, I have added buffers to the thread_info struct, so wonder if I've moved the preempt_count variable out of the way of the corruption. (Still investigating that point..)

<Why.. because I had been trying to set a DABR on the preempt_count
value to catch the corrupter, and due to hits on the nearby _flags fields, getting
false positives..>

struct thread_info {

|-------struct task_struct *task;|------|-------/* main task structure */
|-------struct exec_domain *exec_domain;|-------/* execution domain */
|-------int|----|-------cpu;|---|-------|-------/* cpu we're on */
|-------int|----|-------pad_buffer[64];
|-------int|----|-------preempt_count;|-|-------/* 0 => preemptable,
|-------|-------|-------|-------|-------|-------   <0 => BUG */
|-------int|----|-------pad_buffer2[256];
|-------struct restart_block restart_block;
|-------unsigned long|--local_flags;|---|-------/* private flags for thread

*/

|-------/* low level flags - has atomic operations done on it */ |-------unsigned long|--flags ____cacheline_aligned_in_smp; };

Quoted 13 lines

In both cases the stack pointer appears unchanged.

Note: there is a BUG triggered in between these statements as the preempt_count causes the printk to trigger:
Badness at kernel/sched.c:5572

Thanks,

--
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team

#18 lore raw
In reply to Benjamin Herrenschmidt
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 3 lines · Darren Hart

1) How can the preempt_count() get mangled across the H_CEDE hcall?
2) Should we call preempt_enable() in cpu_idle() prior to cpu_die() ?

The preempt count is on the thread info at the bottom of the stack.

Can you check the stack pointers ?

Hi Ben, sorry if I didn't get back to you on this already. I checked the stack pointer before and after the cede call and they match.

Signature · 3 lines
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team
#19 lore raw
In reply to Darren Hart

Hi Darren,

Quoted 7 lines · Darren Hart

With some instrumentation we were able to determine that the preempt_count() appears to change across the extended_cede_processor() call. Specifically across the plpar_hcall_norets(H_CEDE) call. On PREEMPT_RT we call this with preempt_count=1 and return with preempt_count=0xffffffff. On mainline with CONFIG_PREEMPT=y, the value is different (0x65) but is still incorrect.

I was trying to reproduce this issue on a 2.6.33.7-rt29 kernel. I could easily reproduce this on the RT kernel and not the non-RT kernel. However, I hit it every single time I did a cpu online operation. I also noticed that the issue persists even when I disable H_CEDE by passing the "cede_offline=0" kernel commandline parameter. Could you pl confirm if you observe the same in your setup ?

However, the issue still remains. Will spend few cycles looking into this issue.

Quoted 30 lines

Also of interest is that this path cpu_idle()->cpu_die()->pseries_mach_cpu_die() to start_secondary() enters with a preempt_count=1 if it wasn't corrupted across the hcall. The early boot path from _start however appears to call start_secondary() with a preempt_count of 0.

The following patch is most certainly not correct, but it does eliminate the situation on mainline 100% of the time (there is still a 25% reproduction rate on PREEMPT_RT). Can someone comment on:

1) How can the preempt_count() get mangled across the H_CEDE hcall?
2) Should we call preempt_enable() in cpu_idle() prior to cpu_die() ?

Index: linux-2.6.33.6/arch/powerpc/platforms/pseries/hotplug-cpu.c
===================================================================

arch/powerpc/platforms/pseries/hotplug-cpu.c +1 -0
@@ -138,6 +138,7 @@ static void pseries_mach_cpu_die(void)
 			 * Kernel stack will be reset and start_secondary()
 			 * will be called to continue the online operation.
 			 */
+			preempt_count() = 0;
 			start_secondary_resume();
 		}
 	}
Signature · 5 lines
Regards,
Ankita Garg ([email protected])
Linux Technology Center
IBM India Systems & Technology Labs,
Bangalore, India
#20 lore raw

Ankita Garg <[email protected]> wrote on 08/19/2010 10:58:24 AM:

Quoted 15 lines

Subject
Re: [PATCH][RFC] preempt_count corruption across H_CEDE call with CONFIG_PREEMPT on pseries

Hi Darren,

Quoted 7 lines · Darren Hart

With some instrumentation we were able to determine that the preempt_count() appears to change across the extended_cede_processor() call. Specifically across the plpar_hcall_norets(H_CEDE) call. On PREEMPT_RT we call this with preempt_count=1 and return with preempt_count=0xffffffff. On mainline with CONFIG_PREEMPT=y, the value is different (0x65) but is still incorrect.

Quoted 6 lines

I was trying to reproduce this issue on a 2.6.33.7-rt29 kernel. I could easily reproduce this on the RT kernel and not the non-RT kernel. However, I hit it every single time I did a cpu online operation. I also noticed that the issue persists even when I disable H_CEDE by passing the "cede_offline=0" kernel commandline parameter. Could you pl confirm if you observe the same in your setup ?

If you see it every time, double-check that you have
http://patchwork.ozlabs.org/patch/60922/ or an equivalent in your tree. (The
patch moves the preempt_enable_no_resched() call up above a call to cpu_die (). An
earlier variation called a preempt_enable before calling start_secondary_resume()).

Darren and I have been seeing different problems (different dedicated-processor LPARS
on the same physical system). He's seeing scrambled preempt_count values, I'm tending
to see a system hang/death [with processor backtraces
showing .pseries_mach_cpu_die or
.pseries_dedicated_idle_sleep as expected, but no processes running] .

I'll be curious which you end up seeing. With 2.6.33.7-rt27 as of a few
minutes ago,
I'm still seeing a system hang/death.

Quoted 12 lines

However, the issue still remains. Will spend few cycles looking into this issue.

Quoted 8 lines

Also of interest is that this path cpu_idle()->cpu_die()->pseries_mach_cpu_die() to start_secondary() enters with a preempt_count=1 if it wasn't corrupted across the hcall. The early boot path from _start however appears to call start_secondary() with a preempt_count of 0.

The following patch is most certainly not correct, but it does

eliminate

Quoted 29 lines
Quoted 15 lines

the situation on mainline 100% of the time (there is still a 25% reproduction rate on PREEMPT_RT). Can someone comment on:

1) How can the preempt_count() get mangled across the H_CEDE hcall?
2) Should we call preempt_enable() in cpu_idle() prior to cpu_die() ?

Index: linux-2.6.33.6/arch/powerpc/platforms/pseries/hotplug-cpu.c
===================================================================

arch/powerpc/platforms/pseries/hotplug-cpu.c static void pseries_mach_cpu_die(void) · line 144

Quoted 7 lines
arch/powerpc/platforms/pseries/hotplug-cpu.c +1 -0
@@ -138,6 +138,7 @@ static void pseries_mach_cpu_die(void)
           * Kernel stack will be reset and start_secondary()
           * will be called to continue the online operation.
           */
+         preempt_count() = 0;
          start_secondary_resume();
       }
    }

--
Regards,
Ankita Garg ([email protected])
Linux Technology Center
IBM India Systems & Technology Labs,
Bangalore, India

author Applied
#21 lore raw
In reply to Ankita Garg
Quoted 11 lines · Ankita Garg

Hi Darren,

Quoted 7 lines · Darren Hart

With some instrumentation we were able to determine that the preempt_count() appears to change across the extended_cede_processor() call. Specifically across the plpar_hcall_norets(H_CEDE) call. On PREEMPT_RT we call this with preempt_count=1 and return with preempt_count=0xffffffff. On mainline with CONFIG_PREEMPT=y, the value is different (0x65) but is still incorrect.

Quoted 6 lines

I was trying to reproduce this issue on a 2.6.33.7-rt29 kernel. I could easily reproduce this on the RT kernel and not the non-RT kernel. However, I hit it every single time I did a cpu online operation. I also noticed that the issue persists even when I disable H_CEDE by passing the "cede_offline=0" kernel commandline parameter. Could you pl confirm if you observe the same in your setup ?

with the following patches:
[root@igoort1 linux-2.6-combined]# quilt applied patches/0001-wms-fix01.patch patches/powerpc-increase-pseries_cpu_die-delay.patch patches/powerpc-enable-preemption-before-cpu_die.patch patches/powerpc-silence-__cpu_up-under-normal-operation.patch patches/powerpc-silence-xics_migrate_irqs_away-during-cpu-offline.patch patches/powerpc-wait-for-cpu-to-go-inactive.patch patches/powerpc-disable-decrementer-on-offline.patch patches/powerpc-cpu_die-preempt-hack.patch patches/powerpc-cede-processor-inst.patch patches/irq-preempt-inst.patch patches/disable-decrementer-in-cpu_die.patch patches/powerpc-hard_irq_disable.patch
[root@igoort1 linux-2.6-combined]# quilt unapplied patches/powerpc-debug-replace-cede-with-mdelay.patch patches/powerpc-pad-thread_info.patch

applied to tip/rt/head (2.6.33-rt ish) I will see the following crash after 3 or 4 runs:

<3>Badness at kernel/sched.c:3720
<4>NIP: c0000000006986f8 LR: c0000000006986dc CTR: c00000000006ec34
<4>REGS: c00000008e7a7e50 TRAP: 0700 Not tainted (2.6.33-rt-dvhrt08-02358-g61223dd-dirty)
<4>MSR: 8000000000021032 <ME,CE,IR,DR> CR: 28000022 XER: 00000000 <4>TASK = c00000010e7080c0[0] 'swapper' THREAD: c00000008e7a8000 CPU: 3 <4>GPR00: 0000000000000000 c00000008e7a80d0 c000000000b54fa0 0000000000000001 <4>GPR04: 0000000000000000 0000000000000032 0000000000000000 000000000000000f <4>GPR08: c00000008eb68d00 c000000000ca5420 0000000000000001 c000000000bc22e8 <4>GPR12: 8000000000009032 c000000000ba4a80 c00000008e7a8a70 0000000000000003 <4>GPR16: fffffffffffff9ba c00000010e7080c0 0000000000000000 7fffffffffffffff <4>GPR20: 0000000000000000 0000000000000001 0000000000000003 c000000001042c80 <4>GPR24: 0000000000000000 c00000008eb686a0 0000000000000003 0000000000000001 <4>GPR28: 0000000000000000 0000000000000001 c000000000ad7628 c00000008e7a80d0 <4>NIP [c0000000006986f8] .sub_preempt_count+0x6c/0xdc
<4>LR [c0000000006986dc] .sub_preempt_count+0x50/0xdc
<4>Call Trace:
<4>Instruction dump:
<4>78290464 80090014 7f80e800 40fc002c 4bd08a99 60000000 2fa30000 419e0068 <4>e93e87e0 80090000 2f800000 409e0058 <0fe00000> 48000050 2b9d00fe 41fd0038 <1>Unable to handle kernel paging request for data at address 0xc000018000ba44c0 <1>Faulting instruction address: 0xc00000000006aae4

This occurs with or without the cede_offline=0 parameter.

Also, in a similar experiment which seems to corroborate your results, suggesting the HCEDE call is not necessarily to blame here.

I had replaced the HCEDE call with a mdelay(2) and still ran into issues. I didn't see the preempt count change, but I do see the rtmutex.c:684 bug.

cpu 0x0: Vector: 300 (Data Access) at [c000000000b53ef0]

    pc: c00000000006aa54: .resched_task+0x48/0xd8
    lr: c00000000006ac44: .check_preempt_curr_idle+0x2c/0x44
    sp: c000000000b54170

msr: 8000000000001032
dar: c000018000ba44c0
dsisr: 40000000
current = 0xc000000000aa1410
paca = 0xc000000000ba4480
pid = 0, comm = swapper
enter ? for help
[c000000000b54200] c00000000006ac44 .check_preempt_curr_idle+0x2c/0x44 [c000000000b54290] c00000000007b494 .try_to_wake_up+0x430/0x540 [c000000000b54360] c00000000007b754 .wake_up_process+0x34/0x48 [c000000000b543f0] c000000000089fa8 .wakeup_softirqd+0x78/0x9c [c000000000b54480] c00000000008a2c4 .raise_softirq+0x7c/0xb8 [c000000000b54520] c0000000000977b0 .run_local_timers+0x2c/0x4c [c000000000b545a0] c000000000097828 .update_process_times+0x58/0x9c [c000000000b54640] c0000000000beb3c .tick_sched_timer+0xd0/0x120 [c000000000b546f0] c0000000000b08b8 .__run_hrtimer+0x1a0/0x29c [c000000000b547a0] c0000000000b1258 .hrtimer_interrupt+0x21c/0x394 [c000000000b548d0] c0000000000304c4 .timer_interrupt+0x1ec/0x2f8 [c000000000b54980] c000000000003700 decrementer_common+0x100/0x180
--- Exception: 901 (Decrementer) at c0000000000100f8 .raw_local_irq_restore+0x74/0x8c [c000000000b54d00] c000000000017d14 .cpu_idle+0x12c/0x220 [c000000000b54da0] c0000000006a1768 .start_secondary+0x3d8/0x418 [c000000000b54e60] c00000000005c1f0 .pseries_mach_cpu_die+0x244/0x318 [c000000000b54f10] c00000000001e7e0 .cpu_die+0x4c/0x68 [c000000000b54f90] c000000000017de0 .cpu_idle+0x1f8/0x220
... repeated multiple times ...

Quoted 2 lines

However, the issue still remains. Will spend few cycles looking into this issue.

Appreciate it, the more eyes the better on this one.

--
Darren

Quoted 31 lines
Quoted 30 lines

Also of interest is that this path cpu_idle()->cpu_die()->pseries_mach_cpu_die() to start_secondary() enters with a preempt_count=1 if it wasn't corrupted across the hcall. The early boot path from _start however appears to call start_secondary() with a preempt_count of 0.

The following patch is most certainly not correct, but it does eliminate the situation on mainline 100% of the time (there is still a 25% reproduction rate on PREEMPT_RT). Can someone comment on:

1) How can the preempt_count() get mangled across the H_CEDE hcall?
2) Should we call preempt_enable() in cpu_idle() prior to cpu_die() ?

Index: linux-2.6.33.6/arch/powerpc/platforms/pseries/hotplug-cpu.c
===================================================================

arch/powerpc/platforms/pseries/hotplug-cpu.c +1 -0
@@ -138,6 +138,7 @@ static void pseries_mach_cpu_die(void)
  			 * Kernel stack will be reset and start_secondary()
  			 * will be called to continue the online operation.
  			 */
+			preempt_count() = 0;
  			start_secondary_resume();
  		}
  	}
Signature · 3 lines
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team
#22 lore raw
In reply to Ankita Garg
Quoted 11 lines · Ankita Garg

Hi Darren,

Quoted 7 lines · Darren Hart

With some instrumentation we were able to determine that the preempt_count() appears to change across the extended_cede_processor() call. Specifically across the plpar_hcall_norets(H_CEDE) call. On PREEMPT_RT we call this with preempt_count=1 and return with preempt_count=0xffffffff. On mainline with CONFIG_PREEMPT=y, the value is different (0x65) but is still incorrect.

Quoted 2 lines

I was trying to reproduce this issue on a 2.6.33.7-rt29 kernel. I could easily reproduce this on the RT kernel and not the non-RT kernel.

This matches my experience.

Quoted 4 lines

However, I hit it every single time I did a cpu online operation. I also noticed that the issue persists even when I disable H_CEDE by passing the "cede_offline=0" kernel commandline parameter. Could you pl confirm if you observe the same in your setup ?

Yes, this is my experience as well.

Quoted 4 lines

However, the issue still remains. Will spend few cycles looking into this issue.

I've spent today trying to collect some useful traces. I consistently see the preempt_count() change to 0xffffffff across the H_CEDE call, but the irq and sched events (to ftrace) do not indicate anything else running on the CPU that could change that.

  <idle>-0       1d.h1. 11408us : irq_handler_entry: irq=16 name=IPI
  <idle>-0       1d.h1. 11411us : irq_handler_exit: irq=16 ret=handled
  ...            <other cpus>
  <idle>-0       1d.... 22101us : .pseries_mach_cpu_die: start
  <idle>-0       1d.... 22108us : .pseries_mach_cpu_die: cpu 1 (hwid 1) ceding for offline with hint 2
  ...            <other cpus>
  <idle>-0       1d.Hff. 33804us : .pseries_mach_cpu_die: returned from cede with pcnt: ffffffff
  <idle>-0       1d.Hff. 33805us : .pseries_mach_cpu_die: forcing pcnt to 0
  <idle>-0       1d.... 33807us : .pseries_mach_cpu_die: cpu 1 (hwid 1) returned from cede.
  <idle>-0       1d.... 33808us : .pseries_mach_cpu_die: Decrementer value = 7fa49474 Timebase value = baefee6c88113
  <idle>-0       1d.... 33809us : .pseries_mach_cpu_die: cpu 1 (hwid 1) got prodded to go online
  <idle>-0       1d.... 33816us : .pseries_mach_cpu_die: calling start_seconday, pcnt: 0
  <idle>-0       1d.... 33816us : .pseries_mach_cpu_die: forcing pcnt to 0

---------------------------------

Modules linked in: autofs4 binfmt_misc dm_mirror dm_region_hash dm_log [last unloaded: scsi_wait_scan]
NIP: c00000000006aa50 LR: c00000000006ac40 CTR: c00000000006ac14
REGS: c00000008e79ffc0 TRAP: 0300   Not tainted  (2.6.33-rt-dvhrt16-02358-g61223dd-dirty)
MSR: 8000000000001032 <ME,IR,DR>  CR: 28000022  XER: 00000000
DAR: c000018000ba44c0, DSISR: 0000000040000000

TASK = c00000010e6de040[0] 'swapper' THREAD: c00000008e7a0000 CPU: 1
GPR00: 0000018000000040 c00000008e7a0240 c000000000b55008 c00000010e6de040
GPR04: c000000085d800c0 0000000000000000 0000000000000000 000000000000000f
GPR08: 0000018000000000 c00000008e7a0000 c000000000ba4480 c000000000a32c80
GPR12: 8000000000009032 c000000000ba4680 c00000008e7a08f0 0000000000000001
GPR16: fffffffffffff2fa c00000010e6de040 0000000000000000 7fffffffffffffff
GPR20: 0000000000000000 0000000000000001 0000000000000001 c000000000f42c80
GPR24: c0000000850b7638 0000000000000000 0000000000000000 0000000000000001
GPR28: c000000000f42c80 c00000010e6de040 c000000000ad7698 c00000008e7a0240 NIP [c00000000006aa50] .resched_task+0x48/0xd8
LR [c00000000006ac40] .check_preempt_curr_idle+0x2c/0x44

Call Trace:
Instruction dump:
f821ff71 7c3f0b78 ebc2ab28 7c7d1b78 60000000 60000000 e95e8008 e97e8000
e93d0008 81090010 79084da4 38080040 <7d4a002a> 7c0b502e 7c000074 7800d182
---[ end trace 6d3f1cddaa17382c ]---
Kernel panic - not syncing: Attempted to kill the idle task!
Call Trace:

Rebooting in 180 seconds..

When running with the function plugin I had to stop the trace immediately before entering start_secondary after an online or my traces would not include the pseries_mach_cpu_die function, nor the tracing I added there (possibly buffer size, I am using 2048). The following trace was collected using "trace-cmd record -p function -e irq -e sched" and has been filtered to only show CPU [001] (the CPU undergoing the offline/online test, and the one seeing preempt_count (pcnt) go to ffffffff after cede. The function tracer does not indicate anything running on the CPU other than the HCALL - unless the __trace_hcall* commands might be to blame. Can a POWER guru comment?

          <idle>-0     [001]   417.625286: function:             .cpu_die
          <idle>-0     [001]   417.625287: function:                .pseries_mach_cpu_die
          <idle>-0     [001]   417.625290: bprint:               .pseries_mach_cpu_die : start
          <idle>-0     [001]   417.625291: function:                   .idle_task_exit
          <idle>-0     [001]   417.625292: function:                      .switch_slb
          <idle>-0     [001]   417.625294: function:                   .xics_teardown_cpu
          <idle>-0     [001]   417.625294: function:                      .xics_set_cpu_priority
          <idle>-0     [001]   417.625295: function:             .__trace_hcall_entry
          <idle>-0     [001]   417.625296: function:                .probe_hcall_entry
          <idle>-0     [001]   417.625297: function:             .__trace_hcall_exit
          <idle>-0     [001]   417.625298: function:                .probe_hcall_exit
          <idle>-0     [001]   417.625299: function:             .__trace_hcall_entry
          <idle>-0     [001]   417.625300: function:                .probe_hcall_entry
          <idle>-0     [001]   417.625301: function:             .__trace_hcall_exit
          <idle>-0     [001]   417.625302: function:                .probe_hcall_exit
          <idle>-0     [001]   417.625305: bprint:               .pseries_mach_cpu_die : cpu 1 (hwid 1) ceding for offline with hint 2
          <idle>-0     [001]   417.625307: bprint:               .pseries_mach_cpu_die : calling extended cede, pcnt: 0
          <idle>-0     [001]   417.625308: function:             .__trace_hcall_entry
          <idle>-0     [001]   417.625308: function:                .probe_hcall_entry
          <idle>-0     [001]   417.837734: function:             .__trace_hcall_exit
          <idle>-0     [001]   417.837736: function:                .probe_hcall_exit
          <idle>-0     [001]   417.837740: bprint:               .pseries_mach_cpu_die : returned from cede with pcnt: ffffffff
          <idle>-0     [001]   417.837742: bprint:               .pseries_mach_cpu_die : forcing pcnt to 0
          <idle>-0     [001]   417.837743: bprint:               .pseries_mach_cpu_die : cpu 1 (hwid 1) returned from cede.
          <idle>-0     [001]   417.837745: bprint:               .pseries_mach_cpu_die : Decrementer value = 79844cbf Timebase value = bb3cf7ab2771c
          <idle>-0     [001]   417.837747: bprint:               .pseries_mach_cpu_die : cpu 1 (hwid 1) got prodded to go online
          <idle>-0     [001]   417.837749: function:             .__trace_hcall_entry
          <idle>-0     [001]   417.837749: function:                .probe_hcall_entry
          <idle>-0     [001]   417.837755: function:             .__trace_hcall_exit
          <idle>-0     [001]   417.837755: function:                .probe_hcall_exit
          <idle>-0     [001]   417.837757: bprint:               .pseries_mach_cpu_die : calling start_seconday, pcnt: 0
          <idle>-0     [001]   417.837758: bprint:               .pseries_mach_cpu_die : forcing pcnt to 0

I replaced the extended_cede_processor() call with mdelay(2). When I do that, I don't see the preempt_count() change across the mdelay(2) call. However, the system still eventually crashes in sub_preempt_count(). This appears to be independent of if preempt_count changes across CEDE (since it doesn't occur across mdelay). I will try with larger values of mdelay tomorrow to see if a longer delay will experience the preempt_count value change.

Badness at kernel/sched.c:3726
NIP: c000000000698684 LR: c000000000698668 CTR: c00000000007a89c
REGS: c00000008e7a0170 TRAP: 0700 Not tainted (2.6.33-rt-dvhrt16-02358-g61223dd-dirty)
MSR: 8000000000021032 <ME,CE,IR,DR> CR: 28000022 XER: 00000000 TASK = c00000010e6de040[0] 'swapper' THREAD: c00000008e7a0000 CPU: 1
GPR00: 0000000000000000 c00000008e7a03f0 c000000000b55008 0000000000000001
GPR04: 0000000000000000 0000000000000000 0000000000000000 000000000000000f
GPR08: 0000000000000200 c000000000ca5420 0000000000000001 c000000000bc22f0
GPR12: 8000000000009032 c000000000ba4680 c00000008e7a0a10 0000000000000001
GPR16: fffffffffffff4d1 c00000010e6de040 0000000000000000 7fffffffffffffff
GPR20: 0000000000000000 0000000000000001 0000000000000001 c000000000f42c80
GPR24: 0000000000000001 0000000000000000 0000000000000000 0000000000000001
GPR28: c000000000f42c80 0000000000000001 c000000000ad7698 c00000008e7a03f0 NIP [c000000000698684] .sub_preempt_count+0xa8/0xdc
LR [c000000000698668] .sub_preempt_count+0x8c/0xdc

Call Trace:
Instruction dump:
41fd0038 78000620 2fa00000 40fe002c 4bd08a61 60000000 2fa30000 419e002c
e93e87e8 80090000 2f800000 409e001c <0fe00000> 48000014 78290464 80090014
Unable to handle kernel paging request for data at address 0xc000018000ba44c0
Faulting instruction address: 0xc00000000006aa1c
Oops: Kernel access of bad area, sig: 11 [#1]

PREEMPT SMP NR_CPUS=128 NUMA pSeries
last sysfs file:line

Signature · 3 lines
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team
#23 lore raw

On Tue, 2010-08-31 at 00:12 -0700, Darren Hart wrote:
..

Quoted 11 lines

When running with the function plugin I had to stop the trace immediately before entering start_secondary after an online or my traces would not include the pseries_mach_cpu_die function, nor the tracing I added there (possibly buffer size, I am using 2048). The following trace was collected using "trace-cmd record -p function -e irq -e sched" and has been filtered to only show CPU [001] (the CPU undergoing the offline/online test, and the one seeing preempt_count (pcnt) go to ffffffff after cede. The function tracer does not indicate anything running on the CPU other than the HCALL - unless the __trace_hcall* commands might be to blame.

It's not impossible. Though normally they're disabled right, so the only reason they're running is because you're tracing. So if they are causing the bug then that doesn't explain why you see it normally.

Still, might be worth disabling just the hcall tracepoints just to be 100% sure.

cheers

#24 lore raw
Quoted 14 lines · Michael Ellerman

On Tue, 2010-08-31 at 00:12 -0700, Darren Hart wrote:
..

Quoted 11 lines

When running with the function plugin I had to stop the trace immediately before entering start_secondary after an online or my traces would not include the pseries_mach_cpu_die function, nor the tracing I added there (possibly buffer size, I am using 2048). The following trace was collected using "trace-cmd record -p function -e irq -e sched" and has been filtered to only show CPU [001] (the CPU undergoing the offline/online test, and the one seeing preempt_count (pcnt) go to ffffffff after cede. The function tracer does not indicate anything running on the CPU other than the HCALL - unless the __trace_hcall* commands might be to blame.

Quoted 6 lines

It's not impossible. Though normally they're disabled right, so the only reason they're running is because you're tracing. So if they are causing the bug then that doesn't explain why you see it normally.

Still, might be worth disabling just the hcall tracepoints just to be 100% sure.

A couple of updates. I was working from tip/rt/head, which has been stale for some months now. I switched to tip/rt/2.6.33 and the preempt_count() change over cede went away. I now see the live hang that Will has reported.

Before I dive into the live hang, I want to understand what fixed the preempt_count() change. That might start pointing us in the right direction for the live hang.

I did an inverted git bisect between tip/rt/head and tip/rt/2.6.33 to try and locate the fix. I've narrowed it down to the 2.6.33.6 merge:

# git show 7e1af1172bbd4109d09ac515c5d376f633da7cff
commit 7e1af1172bbd4109d09ac515c5d376f633da7cff
Merge: d8e94db 9666790
Author: Thomas Gleixner <[email protected]>
Date:   Tue Jul 13 16:01:16 2010 +0200

Merge
git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-2.6.33.y

Conflicts:
Makefile

Visual inspection yields two patches of interest:

f8b67691828321f5c85bb853283aa101ae673130
powerpc/pseries: Make query-cpu-stopped callable outside hotplug cpu

aef40e87d866355ffd279ab21021de733242d0d5 powerpc/pseries: Only call start-cpu when a CPU is stopped

I'm going to try reverting these today and see if they addressed the issue indirectly.

Signature · 3 lines
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team
#25 lore raw
Quoted 51 lines · Darren Hart
Quoted 14 lines · Michael Ellerman

On Tue, 2010-08-31 at 00:12 -0700, Darren Hart wrote:
..

Quoted 11 lines

When running with the function plugin I had to stop the trace immediately before entering start_secondary after an online or my traces would not include the pseries_mach_cpu_die function, nor the tracing I added there (possibly buffer size, I am using 2048). The following trace was collected using "trace-cmd record -p function -e irq -e sched" and has been filtered to only show CPU [001] (the CPU undergoing the offline/online test, and the one seeing preempt_count (pcnt) go to ffffffff after cede. The function tracer does not indicate anything running on the CPU other than the HCALL - unless the __trace_hcall* commands might be to blame.

Quoted 6 lines

It's not impossible. Though normally they're disabled right, so the only reason they're running is because you're tracing. So if they are causing the bug then that doesn't explain why you see it normally.

Still, might be worth disabling just the hcall tracepoints just to be 100% sure.

A couple of updates. I was working from tip/rt/head, which has been stale for some months now. I switched to tip/rt/2.6.33 and the preempt_count() change over cede went away. I now see the live hang that Will has reported.

Before I dive into the live hang, I want to understand what fixed the preempt_count() change. That might start pointing us in the right direction for the live hang.

I did an inverted git bisect between tip/rt/head and tip/rt/2.6.33 to try and locate the fix. I've narrowed it down to the 2.6.33.6 merge:

# git show 7e1af1172bbd4109d09ac515c5d376f633da7cff
commit 7e1af1172bbd4109d09ac515c5d376f633da7cff
Merge: d8e94db 9666790
Author: Thomas Gleixner <[email protected]>
Date:   Tue Jul 13 16:01:16 2010 +0200

Merge
git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-2.6.33.y

Conflicts:
Makefile

Visual inspection yields two patches of interest:

Quoted 8 lines

f8b67691828321f5c85bb853283aa101ae673130
powerpc/pseries: Make query-cpu-stopped callable outside hotplug cpu

aef40e87d866355ffd279ab21021de733242d0d5 powerpc/pseries: Only call start-cpu when a CPU is stopped

I'm going to try reverting these today and see if they addressed the issue indirectly.

Removing:

aef40e87d866355ffd279ab21021de733242d0d5 powerpc/pseries: Only call start-cpu when a CPU is stopped

arch/powerpc/platforms/pseries/smp.c +1 -0
@@ -82,6 +82,12 @@ static inline int __devinit smp_startup_cpu(unsigned int lcpu)
 
        pcpu = get_hard_smp_processor_id(lcpu);
 
+       /* Check to see if the CPU out of FW already for kexec */
 

This comment is really confusing to me. I _think_ it is saying that this test determines if the CPU is done executing firmware code and has begun executing OS code.... Is that right?

+ if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
+ cpu_set(lcpu, of_spin_map);
+ return 1;
+ }
+

        /* Fixup atomic count: it exited inside IRQ handler. */
        task_thread_info(paca[lcpu].__current)->preempt_count   = 0;

from tip/rt/2.6.33 causes the preempt_count() to change across the cede call. This patch appears to prevents the proxy preempt_count assignment from happening. This non-local-cpu assignment to 0 would cause an underrun of preempt_count() if the local-cpu had disabled preemption prior to the assignment and then later tried to enable it. This appears to be the case with the stack of __trace_hcall* calls preceeding the return from extended_cede_processor() in the latency format trace-cmd report:

  <idle>-0       1d....   201.252737: function:             .cpu_die
  <idle>-0       1d....   201.252738: function:                .pseries_mach_cpu_die
  <idle>-0       1d....   201.252740: function:                   .idle_task_exit
  <idle>-0       1d....   201.252741: function:                      .switch_slb
  <idle>-0       1d....   201.252742: function:                   .xics_teardown_cpu
  <idle>-0       1d....   201.252743: function:                      .xics_set_cpu_priority
  <idle>-0       1d....   201.252744: function:             .__trace_hcall_entry
  <idle>-0       1d..1.   201.252745: function:                .probe_hcall_entry
  <idle>-0       1d..1.   201.252746: function:             .__trace_hcall_exit
  <idle>-0       1d..2.   201.252747: function:                .probe_hcall_exit
  <idle>-0       1d....   201.252748: function:             .__trace_hcall_entry
  <idle>-0       1d..1.   201.252748: function:                .probe_hcall_entry
  <idle>-0       1d..1.   201.252750: function:             .__trace_hcall_exit
  <idle>-0       1d..2.   201.252751: function:                .probe_hcall_exit
  <idle>-0       1d....   201.252752: function:             .__trace_hcall_entry
  <idle>-0       1d..1.   201.252753: function:                .probe_hcall_entry
  offon.sh-3684  6.....   201.466488: bprint:               .smp_pSeries_kick_cpu : resetting pcnt to 0 for cpu 1

preempt_count() is reset from 1 to 0 by smp_startup_cpu() without the QCSS_NOT_STOPPED check from the patch above.

  <idle>-0       1d....   201.466503: function:             .__trace_hcall_exit
  <idle>-0       1d..1.   201.466505: function:                .probe_hcall_exit
  <idle>-0       1d.Hff.   201.466507: bprint:               .pseries_mach_cpu_die : after cede: ffffffff

With the preempt_count() being one less than it should be, the final preempt_enable() in the trace_hcall path drops preempt_count to 0xffffffff, which of course is an illegal value and leads to a crash.

I don't know if the patch above is the "right fix" for this or not as don't yet understand why this occurs only with PREEMPT_RT and not in mainline. Once we sort that out, we may need a more specific fix.

Many thanks to Steven Rostedt for pouring over the trace and the trace code with me.

Thanks,

Signature · 3 lines
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team
#26 lore raw
Quoted 10 lines · Darren Hart

from tip/rt/2.6.33 causes the preempt_count() to change across the cede call. This patch appears to prevents the proxy preempt_count assignment from happening. This non-local-cpu assignment to 0 would cause an underrun of preempt_count() if the local-cpu had disabled preemption prior to the assignment and then later tried to enable it. This appears to be the case with the stack of __trace_hcall* calls preceeding the return from extended_cede_processor() in the latency format trace-cmd report:

<idle>-0 1d.... 201.252737: function: .cpu_die

Note, the above 1d.... is a series of values. The first being the CPU, the next if interrupts are disabled, the next if the NEED_RESCHED flag is set, the next is softirqs enabled or disabled, next the preempt_count, and finally the lockdepth count.

Here we only care about the preempt_count, which is zero when '.' and a number if it is something else. It is the second to last field in that list.

Quoted 7 lines
  <idle>-0       1d....   201.252738: function:                .pseries_mach_cpu_die
  <idle>-0       1d....   201.252740: function:                   .idle_task_exit
  <idle>-0       1d....   201.252741: function:                      .switch_slb
  <idle>-0       1d....   201.252742: function:                   .xics_teardown_cpu
  <idle>-0       1d....   201.252743: function:                      .xics_set_cpu_priority
  <idle>-0       1d....   201.252744: function:             .__trace_hcall_entry
  <idle>-0       1d..1.   201.252745: function:                .probe_hcall_entry

^
preempt_count set to 1

Quoted 8 lines
  <idle>-0       1d..1.   201.252746: function:             .__trace_hcall_exit
  <idle>-0       1d..2.   201.252747: function:                .probe_hcall_exit
  <idle>-0       1d....   201.252748: function:             .__trace_hcall_entry
  <idle>-0       1d..1.   201.252748: function:                .probe_hcall_entry
  <idle>-0       1d..1.   201.252750: function:             .__trace_hcall_exit
  <idle>-0       1d..2.   201.252751: function:                .probe_hcall_exit
  <idle>-0       1d....   201.252752: function:             .__trace_hcall_entry
  <idle>-0       1d..1.   201.252753: function:                .probe_hcall_entry
                   ^   ^
                  CPU  preempt_count

Entering the function probe_hcall_entry() the preempt_count is 1 (see below). But probe_hcall_entry does:

	h = &get_cpu_var(hcall_stats)[opcode / 4];

Without doing the put (which it does in probe_hcall_exit())

So exiting the probe_hcall_entry() the prempt_count is 2. The trace_hcall_entry() will do a preempt_enable() making it leave as 1.

Quoted 1 line

offon.sh-3684 6..... 201.466488: bprint: .smp_pSeries_kick_cpu : resetting pcnt to 0 for cpu 1

This is CPU 6, changing the preempt count from 1 to 0.

Quoted 5 lines

preempt_count() is reset from 1 to 0 by smp_startup_cpu() without the QCSS_NOT_STOPPED check from the patch above.

<idle>-0 1d.... 201.466503: function: .__trace_hcall_exit

Note: __trace_hcall_exit() and __trace_hcall_entry() basically do:

preempt_disable();
call probe();
preempt_enable();

Quoted 1 line

<idle>-0 1d..1. 201.466505: function: .probe_hcall_exit

The preempt_count of 1 entering the probe_hcall_exit() is because of the preempt_disable() shown above. It should have been entered as a 2.

But then it does:

	put_cpu_var(hcall_stats);

making preempt_count 0.

But the preempt_enable() in the trace_hcall_exit() causes this to be -1.

Quoted 5 lines

<idle>-0 1d.Hff. 201.466507: bprint: .pseries_mach_cpu_die : after cede: ffffffff

With the preempt_count() being one less than it should be, the final preempt_enable() in the trace_hcall path drops preempt_count to 0xffffffff, which of course is an illegal value and leads to a crash.

I'm confused to how this works in mainline?

-- Steve

Quoted 4 lines

I don't know if the patch above is the "right fix" for this or not as don't yet understand why this occurs only with PREEMPT_RT and not in mainline. Once we sort that out, we may need a more specific fix.

#27 lore raw
Quoted 88 lines · Steven Rostedt
Quoted 10 lines · Darren Hart

from tip/rt/2.6.33 causes the preempt_count() to change across the cede call. This patch appears to prevents the proxy preempt_count assignment from happening. This non-local-cpu assignment to 0 would cause an underrun of preempt_count() if the local-cpu had disabled preemption prior to the assignment and then later tried to enable it. This appears to be the case with the stack of __trace_hcall* calls preceeding the return from extended_cede_processor() in the latency format trace-cmd report:

<idle>-0 1d.... 201.252737: function: .cpu_die

Note, the above 1d.... is a series of values. The first being the CPU, the next if interrupts are disabled, the next if the NEED_RESCHED flag is set, the next is softirqs enabled or disabled, next the preempt_count, and finally the lockdepth count.

Here we only care about the preempt_count, which is zero when '.' and a number if it is something else. It is the second to last field in that list.

Quoted 7 lines
  <idle>-0       1d....   201.252738: function:                .pseries_mach_cpu_die
  <idle>-0       1d....   201.252740: function:                   .idle_task_exit
  <idle>-0       1d....   201.252741: function:                      .switch_slb
  <idle>-0       1d....   201.252742: function:                   .xics_teardown_cpu
  <idle>-0       1d....   201.252743: function:                      .xics_set_cpu_priority
  <idle>-0       1d....   201.252744: function:             .__trace_hcall_entry
  <idle>-0       1d..1.   201.252745: function:                .probe_hcall_entry

^
preempt_count set to 1

Quoted 8 lines
  <idle>-0       1d..1.   201.252746: function:             .__trace_hcall_exit
  <idle>-0       1d..2.   201.252747: function:                .probe_hcall_exit
  <idle>-0       1d....   201.252748: function:             .__trace_hcall_entry
  <idle>-0       1d..1.   201.252748: function:                .probe_hcall_entry
  <idle>-0       1d..1.   201.252750: function:             .__trace_hcall_exit
  <idle>-0       1d..2.   201.252751: function:                .probe_hcall_exit
  <idle>-0       1d....   201.252752: function:             .__trace_hcall_entry
  <idle>-0       1d..1.   201.252753: function:                .probe_hcall_entry
                   ^   ^
                  CPU  preempt_count

Entering the function probe_hcall_entry() the preempt_count is 1 (see below). But probe_hcall_entry does:

	h = &get_cpu_var(hcall_stats)[opcode / 4];

Without doing the put (which it does in probe_hcall_exit())

So exiting the probe_hcall_entry() the prempt_count is 2. The trace_hcall_entry() will do a preempt_enable() making it leave as 1.

Quoted 1 line

offon.sh-3684 6..... 201.466488: bprint: .smp_pSeries_kick_cpu : resetting pcnt to 0 for cpu 1

This is CPU 6, changing the preempt count from 1 to 0.

Quoted 5 lines

preempt_count() is reset from 1 to 0 by smp_startup_cpu() without the QCSS_NOT_STOPPED check from the patch above.

<idle>-0 1d.... 201.466503: function: .__trace_hcall_exit

Note: __trace_hcall_exit() and __trace_hcall_entry() basically do:

preempt_disable();
call probe();
preempt_enable();

Quoted 1 line

<idle>-0 1d..1. 201.466505: function: .probe_hcall_exit

The preempt_count of 1 entering the probe_hcall_exit() is because of the preempt_disable() shown above. It should have been entered as a 2.

But then it does:

	put_cpu_var(hcall_stats);

making preempt_count 0.

But the preempt_enable() in the trace_hcall_exit() causes this to be -1.

Quoted 7 lines
Quoted 5 lines

<idle>-0 1d.Hff. 201.466507: bprint: .pseries_mach_cpu_die : after cede: ffffffff

With the preempt_count() being one less than it should be, the final preempt_enable() in the trace_hcall path drops preempt_count to 0xffffffff, which of course is an illegal value and leads to a crash.

I'm confused to how this works in mainline?

Turns out it didn't. 2.6.33.5 with CONFIG_PREEMPT=y sees this exact same behavior. The following, part of the 2.6.33.6 stable release, prevents this from happening:

aef40e87d866355ffd279ab21021de733242d0d5 powerpc/pseries: Only call start-cpu when a CPU is stopped

arch/powerpc/platforms/pseries/smp.c +0 -0
@@ -82,6 +82,12 @@ static inline int __devinit smp_startup_cpu(unsigned

int lcpu)

        pcpu = get_hard_smp_processor_id(lcpu);

+ /* Check to see if the CPU out of FW already for kexec */
+ if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
+ cpu_set(lcpu, of_spin_map);
+ return 1;
+ }
+

        /* Fixup atomic count: it exited inside IRQ handler. */
        task_thread_info(paca[lcpu].__current)->preempt_count   = 0;

The question is now, Is this the right fix? If so, perhaps we can update the comment to be a bit more clear and not refer solely to kexec.

Michael Neuling, can you offer any thoughts here? We hit this EVERY TIME, which makes me wonder if the offline/online path could do this without calling smp_startup_cpu at all.

Signature · 3 lines
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team
#28 lore raw
Quoted 19 lines · In message <[email protected]> you wrote:
Quoted 18 lines · Steven Rostedt
Quoted 10 lines · Darren Hart

from tip/rt/2.6.33 causes the preempt_count() to change across the cede call. This patch appears to prevents the proxy preempt_count assignment from happening. This non-local-cpu assignment to 0 would cause an underrun of preempt_count() if the local-cpu had disabled preemption prior to the assignment and then later tried to enable it. This appears to be the case with the stack of __trace_hcall* calls preceeding the return from extended_cede_processor() in the latency format trace-cmd report:

<idle>-0 1d.... 201.252737: function: .cpu_die

Note, the above 1d.... is a series of values. The first being the CPU, the next if interrupts are disabled, the next if the NEED_RESCHED flag is set, the next is softirqs enabled or disabled, next the preempt_count, and finally the lockdepth count.

Quoted 6 lines
Quoted 6 lines

Here we only care about the preempt_count, which is zero when '.' and a number if it is something else. It is the second to last field in that list.

Quoted 1 line

<idle>-0 1d.... 201.252738: function: .pseries_ma

ch_cpu_die

Quoted 1 line
Quoted 1 line
Quoted 1 line

<idle>-0 1d.... 201.252740: function: .idle_ta

sk_exit

Quoted 1 line
Quoted 1 line
Quoted 1 line

<idle>-0 1d.... 201.252741: function: .swit

ch_slb

Quoted 1 line
Quoted 1 line
Quoted 1 line

<idle>-0 1d.... 201.252742: function: .xics_te

ardown_cpu

Quoted 1 line
Quoted 1 line
Quoted 1 line

<idle>-0 1d.... 201.252743: function: .xics

_set_cpu_priority

Quoted 1 line
Quoted 1 line
Quoted 1 line

<idle>-0 1d.... 201.252744: function: .__trace_hcall

_entry

Quoted 1 line
Quoted 1 line
Quoted 1 line

<idle>-0 1d..1. 201.252745: function: .probe_hcal

l_entry

Quoted 5 lines
Quoted 5 lines

^
preempt_count set to 1

Quoted 1 line

<idle>-0 1d..1. 201.252746: function: .__trace_hcall

_exit

Quoted 1 line
Quoted 1 line
Quoted 1 line

<idle>-0 1d..2. 201.252747: function: .probe_hcal

l_exit

Quoted 1 line
Quoted 1 line
Quoted 1 line

<idle>-0 1d.... 201.252748: function: .__trace_hcall

_entry

Quoted 1 line
Quoted 1 line
Quoted 1 line

<idle>-0 1d..1. 201.252748: function: .probe_hcal

l_entry

Quoted 1 line
Quoted 1 line
Quoted 1 line

<idle>-0 1d..1. 201.252750: function: .__trace_hcall

_exit

Quoted 1 line
Quoted 1 line
Quoted 1 line

<idle>-0 1d..2. 201.252751: function: .probe_hcal

l_exit

Quoted 1 line
Quoted 1 line
Quoted 1 line

<idle>-0 1d.... 201.252752: function: .__trace_hcall

_entry

Quoted 1 line
Quoted 1 line
Quoted 1 line

<idle>-0 1d..1. 201.252753: function: .probe_hcal

l_entry

Quoted 8 lines
Quoted 8 lines
                   ^   ^
                  CPU  preempt_count

Entering the function probe_hcall_entry() the preempt_count is 1 (see below). But probe_hcall_entry does:

	h = &get_cpu_var(hcall_stats)[opcode / 4];
Quoted 7 lines
Quoted 7 lines

Without doing the put (which it does in probe_hcall_exit())

So exiting the probe_hcall_entry() the prempt_count is 2. The trace_hcall_entry() will do a preempt_enable() making it leave as 1.

Quoted 1 line

offon.sh-3684 6..... 201.466488: bprint: .smp_pSeries_k

ick_cpu : resetting pcnt to 0 for cpu 1

Quoted 8 lines
Quoted 8 lines

This is CPU 6, changing the preempt count from 1 to 0.

Quoted 5 lines

preempt_count() is reset from 1 to 0 by smp_startup_cpu() without the QCSS_NOT_STOPPED check from the patch above.

<idle>-0 1d.... 201.466503: function: .__trace_hcall

_exit

Quoted 9 lines
Quoted 9 lines

Note: __trace_hcall_exit() and __trace_hcall_entry() basically do:

preempt_disable();
call probe();
preempt_enable();

Quoted 1 line

<idle>-0 1d..1. 201.466505: function: .probe_hcal

l_exit

Quoted 7 lines
Quoted 7 lines

The preempt_count of 1 entering the probe_hcall_exit() is because of the preempt_disable() shown above. It should have been entered as a 2.

But then it does:

Quoted 8 lines
Quoted 8 lines
	put_cpu_var(hcall_stats);

making preempt_count 0.

But the preempt_enable() in the trace_hcall_exit() causes this to be -1.

Quoted 1 line

<idle>-0 1d.Hff. 201.466507: bprint: .pseries_mach

_cpu_die : after cede: ffffffff

Quoted 30 lines
Quoted 6 lines
Quoted 4 lines

With the preempt_count() being one less than it should be, the final preempt_enable() in the trace_hcall path drops preempt_count to 0xffffffff, which of course is an illegal value and leads to a crash.

I'm confused to how this works in mainline?

Turns out it didn't. 2.6.33.5 with CONFIG_PREEMPT=y sees this exact same behavior. The following, part of the 2.6.33.6 stable release, prevents this from happening:

aef40e87d866355ffd279ab21021de733242d0d5 powerpc/pseries: Only call start-cpu when a CPU is stopped

arch/powerpc/platforms/pseries/smp.c +6 -0
@@ -82,6 +82,12 @@ static inline int __devinit smp_startup_cpu(unsigned
 int lcpu)
 
        pcpu = get_hard_smp_processor_id(lcpu);
 
+       /* Check to see if the CPU out of FW already for kexec */
+       if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
+               cpu_set(lcpu, of_spin_map);
+               return 1;
+       }
+
        /* Fixup atomic count: it exited inside IRQ handler. */
        task_thread_info(paca[lcpu].__current)->preempt_count   = 0;
Quoted 6 lines

The question is now, Is this the right fix? If so, perhaps we can update the comment to be a bit more clear and not refer solely to kexec.

Michael Neuling, can you offer any thoughts here? We hit this EVERY TIME, which makes me wonder if the offline/online path could do this without calling smp_startup_cpu at all.

We need to call smp_startup_cpu on boot when we the cpus are still in FW. smp_startup_cpu does this for us on boot.

I'm wondering if we just need to move the test down a bit to make sure the preempt_count is set. I've not been following this thread, but maybe this might work?

Untested patch below...

Mikey

arch/powerpc/platforms/pseries/smp.c +6 -6
index 0317cce..3afaba4 100644
@@ -104,18 +104,18 @@ static inline int __devinit smp_startup_cpu(unsigned int lcpu)
 
 	pcpu = get_hard_smp_processor_id(lcpu);
 
-	/* Check to see if the CPU out of FW already for kexec */
-	if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
-		cpumask_set_cpu(lcpu, of_spin_mask);
-		return 1;
-	}
-
 	/* Fixup atomic count: it exited inside IRQ handler. */
 	task_thread_info(paca[lcpu].__current)->preempt_count	= 0;
 
 	if (get_cpu_current_state(lcpu) == CPU_STATE_INACTIVE)
 		goto out;
 
+	/* Check to see if the CPU out of FW already for kexec */
+	if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
+		cpumask_set_cpu(lcpu, of_spin_mask);
+		return 1;
+	}
+
 	/* 
 	 * If the RTAS start-cpu token does not exist then presume the
 	 * cpu is already spinning.
#29 lore raw
Quoted 6 lines · Michael Neuling

We need to call smp_startup_cpu on boot when we the cpus are still in FW. smp_startup_cpu does this for us on boot.

I'm wondering if we just need to move the test down a bit to make sure the preempt_count is set. I've not been following this thread, but maybe this might work?

Egad no! Setting the preempt_count to zero _is_ the bug. I think Darren even said that adding the exit prevented the bug (although now he's hitting a hard lockup someplace else). The original code he was using did not have the condition to return for kexec. It was just a coincidence that this code helped in bringing a CPU back online.

Quoted 11 lines

Untested patch below...

Mikey

arch/powerpc/platforms/pseries/smp.c +0 -0
index 0317cce..3afaba4 100644
@@ -104,18 +104,18 @@ static inline int __devinit smp_startup_cpu(unsigned int lcpu)
 

arch/powerpc/platforms/pseries/smp.c static inline int __devinit smp_startup_cpu(unsigned int lcpu) · line 108

Quoted 10 lines
arch/powerpc/platforms/pseries/smp.c +0 -6
 	pcpu = get_hard_smp_processor_id(lcpu);
 
-	/* Check to see if the CPU out of FW already for kexec */
-	if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
-		cpumask_set_cpu(lcpu, of_spin_mask);
-		return 1;
-	}
-
 	/* Fixup atomic count: it exited inside IRQ handler. */
 	task_thread_info(paca[lcpu].__current)->preempt_count	= 0;

We DON'T want to do the above. It's nasty! This is one CPU's task touching an intimate part of another CPU's task. It's equivalent of me putting my hand down you wife's blouse. It's offensive, and rude.

OK, if the CPU was never online, then you can do what you want. But what we see is that this fails on CPU hotplug. You stop a CPU, and it goes into this cede_processor() call. When you wake it up, suddenly the task on that woken CPU has its preempt count fscked up. This was really really hard to debug. We thought it was stack corruption or something. But it ended up being that this code has one CPU touching the breasts of another CPU. This code is a pervert!

What the trace clearly showed, was that we take down a CPU, and in doing so, the code on that CPU set the preempt count to 1, and it expected to have it as 1 when it returned. But the code that kicked started this CPU back to life (bring the CPU back online), set the preempt count on the task of that CPU to 0, and screwed everything up.

-- Steve

Quoted 14 lines
arch/powerpc/platforms/pseries/smp.c +6 -0
 	if (get_cpu_current_state(lcpu) == CPU_STATE_INACTIVE)
 		goto out;
 
+	/* Check to see if the CPU out of FW already for kexec */
+	if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
+		cpumask_set_cpu(lcpu, of_spin_mask);
+		return 1;
+	}
+
 	/* 
 	 * If the RTAS start-cpu token does not exist then presume the
 	 * cpu is already spinning.
 
#30 lore raw
Quoted 49 lines · Steven Rostedt
Quoted 6 lines · Michael Neuling

We need to call smp_startup_cpu on boot when we the cpus are still in FW. smp_startup_cpu does this for us on boot.

I'm wondering if we just need to move the test down a bit to make sure the preempt_count is set. I've not been following this thread, but maybe this might work?

Egad no! Setting the preempt_count to zero _is_ the bug. I think Darren even said that adding the exit prevented the bug (although now he's hitting a hard lockup someplace else). The original code he was using did not have the condition to return for kexec. It was just a coincidence that this code helped in bringing a CPU back online.

Quoted 11 lines

Untested patch below...

Mikey

arch/powerpc/platforms/pseries/smp.c +0 -0
index 0317cce..3afaba4 100644
@@ -104,18 +104,18 @@ static inline int __devinit smp_startup_cpu(unsigned int lcpu)
 

arch/powerpc/platforms/pseries/smp.c static inline int __devinit smp_startup_cpu(unsigned int lcpu) · line 108

Quoted 10 lines
arch/powerpc/platforms/pseries/smp.c +0 -6
 	pcpu = get_hard_smp_processor_id(lcpu);
 
-	/* Check to see if the CPU out of FW already for kexec */
-	if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
-		cpumask_set_cpu(lcpu, of_spin_mask);
-		return 1;
-	}
-
 	/* Fixup atomic count: it exited inside IRQ handler. */
 	task_thread_info(paca[lcpu].__current)->preempt_count	= 0;

We DON'T want to do the above. It's nasty! This is one CPU's task touching an intimate part of another CPU's task. It's equivalent of me putting my hand down you wife's blouse. It's offensive, and rude.

OK, if the CPU was never online, then you can do what you want. But what we see is that this fails on CPU hotplug. You stop a CPU, and it goes into this cede_processor() call. When you wake it up, suddenly the task on that woken CPU has its preempt count fscked up. This was really really hard to debug. We thought it was stack corruption or something. But it ended up being that this code has one CPU touching the breasts of another CPU. This code is a pervert!

Quoted 5 lines

What the trace clearly showed, was that we take down a CPU, and in doing so, the code on that CPU set the preempt count to 1, and it expected to have it as 1 when it returned. But the code that kicked started this CPU back to life (bring the CPU back online), set the preempt count on the task of that CPU to 0, and screwed everything up.

Right, what Steve said.

This patch resolved the problem, but it did so inadvertently while trying to fix kexec. I'm wondering if the offline/online code needs to be updated to never call smp_startup_cpu(). Or perhaps this is the right fix, and the comment needs to be cleaned up so that it isn't only for kexec.

With this in place, we no longer see the preempt_count dropping below zero. However, if I offline/online a CPU about 246 times I hit the opposite problem, a preempt_count() overflow. There appears to be a missing preempt_enable() somewhere in the offline/online paths.

On 2.6.33.7 with CONFIG_PREEMPT=y I run the following test:

# cat offon_loop.sh 
#!/bin/sh
for i in `seq 100`; do
        echo "Iteration $i"
        echo 0 > /sys/devices/system/cpu/cpu1/online
        echo 1 > /sys/devices/system/cpu/cpu1/online
done

And see the overflow:

Iteration 238
Iteration 239
Iteration 240

Message from syslogd@igoort1 at Sep 1 17:36:45 ...
kernel:------------[ cut here ]------------
Iteration 241
Iteration 242
Iteration 243
Iteration 244
Iteration 245

With the following on the console. This is the:

	/*
	 * Spinlock count overflowing soon?
	 */
	DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
				PREEMPT_MASK - 10);

test. Max preempt count is 256.

Somewhere we are now MISSING a sub_preempt_count() or preempt_enable().

Badness at kernel/sched.c:5372
NIP: c000000000694f14 LR: c000000000694ef8 CTR: c00000000005b0a0
REGS: c00000008e776ae0 TRAP: 0700 Not tainted (2.6.33.7-dirty)
MSR: 8000000000021032 <ME,CE,IR,DR> CR: 28000082 XER: 00000000 TASK = c00000010e6fc040[0] 'swapper' THREAD: c00000008e774000 CPU: 1
GPR00: 0000000000000000 c00000008e776d60 c000000000af2ab8 0000000000000001
GPR04: c00000008e776fb8 0000000000000004 0000000000000001 ffffffffff676980
GPR08: 0000000000000400 c000000000bcd930 0000000000000001 c000000000b2d360
GPR12: 0000000000000002 c000000000b0f680 0000000000000000 000000000f394acc
GPR16: 0000000000000000 0000000000000000 0000000000000000 c00000008e777880
GPR20: c000000000e05160 c00000008e777870 7fffffffffffffff c000000000b0f480
GPR24: c0000000009da400 0000000000000002 0000000000000000 0000000000000001
GPR28: c00000008e776fb8 0000000000000001 c000000000a75bf0 c00000008e776d60 NIP [c000000000694f14] .add_preempt_count+0xc0/0xe0
LR [c000000000694ef8] .add_preempt_count+0xa4/0xe0

Call Trace:

[c00000008e776d60] [c00000008e776e00] 0xc00000008e776e00 (unreliable) [c00000008e776df0] [c00000000005b0d8] .probe_hcall_entry+0x38/0x94 [c00000008e776e80] [c00000000004ef70] .__trace_hcall_entry+0x80/0xd4 [c00000008e776f10] [c00000000004f96c] .plpar_hcall_norets+0x50/0xd0 [c00000008e776f80] [c000000000055044] .smp_xics_message_pass+0x110/0x244 [c00000008e777030] [c000000000034824] .smp_send_reschedule+0x5c/0x78 [c00000008e7770c0] [c00000000006a6bc] .resched_task+0xb4/0xd8 [c00000008e777150] [c00000000006a840] .check_preempt_curr_idle+0x2c/0x44 [c00000008e7771e0] [c00000000007a868] .try_to_wake_up+0x460/0x548 [c00000008e7772b0] [c00000000007a98c] .default_wake_function+0x3c/0x54 [c00000008e777350] [c0000000000ab3b0] .autoremove_wake_function+0x44/0x84 [c00000008e777400] [c00000000006449c] .__wake_up_common+0x7c/0xf4 [c00000008e7774c0] [c00000000006a5d8] .__wake_up+0x60/0x90 [c00000008e777570] [c0000000000810dc] .printk_tick+0x84/0xa8 [c00000008e777600] [c000000000095c90] .update_process_times+0x64/0xa4 [c00000008e7776a0] [c0000000000bdcec] .tick_sched_timer+0xd0/0x120 [c00000008e777750] [c0000000000afe7c] .__run_hrtimer+0x1a0/0x29c [c00000008e777800] [c0000000000b02a4] .hrtimer_interrupt+0x124/0x278 [c00000008e777900] [c00000000002ea90] .timer_interrupt+0x1dc/0x2e4 [c00000008e7779a0] [c000000000003700] decrementer_common+0x100/0x180 --- Exception: 901 at .raw_local_irq_restore+0x48/0x54

    LR = .cpu_idle+0x12c/0x208

[c00000008e777c90] [c000000000b21130] ppc_md+0x0/0x240 (unreliable) [c00000008e777cd0] [c000000000016a60] .cpu_idle+0x120/0x208 [c00000008e777d70] [c00000000069dbec] .start_secondary+0x394/0x3d4 [c00000008e777e30] [c000000000008278] .start_secondary_resume+0x10/0x14 Instruction dump:
409e0034 78630620 2ba300f4 40fd0028 4bd0b711 60000000 2fa30000 419e0018 e93e8880 80090000 2f800000 409e0008 <0fe00000> 383f0090 e8010010 7c0803a6

BUG: scheduling while atomic: swapper/0/0x000000f0
Modules linked in: autofs4 binfmt_misc dm_mirror dm_region_hash dm_log [last unloaded: scsi_wait_scan]
Call Trace:
[c00000008e7779a0] [c000000000014280] .show_stack+0xd8/0x218 (unreliable)
[c00000008e777a80] [c0000000006980b0] .dump_stack+0x28/0x3c
[c00000008e777b00] [c000000000071954] .__schedule_bug+0x94/0xb8
[c00000008e777b90] [c00000000068db40] .schedule+0xf8/0xbf4
[c00000008e777cd0] [c000000000016b34] .cpu_idle+0x1f4/0x208
[c00000008e777d70] [c00000000069dbec] .start_secondary+0x394/0x3d4
[c00000008e777e30] [c000000000008278] .start_secondary_resume+0x10/0x14

I'll spend tomorrow collecting traces and trying to see who's groping who this time...

--
Darren

Quoted 20 lines

-- Steve

Quoted 14 lines
arch/powerpc/platforms/pseries/smp.c +6 -0
 	if (get_cpu_current_state(lcpu) == CPU_STATE_INACTIVE)
 		goto out;
 
+	/* Check to see if the CPU out of FW already for kexec */
+	if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
+		cpumask_set_cpu(lcpu, of_spin_mask);
+		return 1;
+	}
+
 	/* 
 	 * If the RTAS start-cpu token does not exist then presume the
 	 * cpu is already spinning.
 
Signature · 3 lines
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team
#31 lore raw

[email protected] wrote on 09/02/2010 01:04:28 AM:

Quoted 9 lines

Subject

Re: [PATCH][RFC] preempt_count corruption across H_CEDE call with CONFIG_PREEMPT on pseries

With this in place, we no longer see the preempt_count dropping below zero. However, if I offline/online a CPU about 246 times I hit the opposite problem, a preempt_count() overflow. There appears to be a missing preempt_enable() somewhere in the offline/online paths.

This (preempt_count overflow) also occurred in mainline (with
CONFIG_PREEMPT=y) in 2.6.35, but not in 2.6.36-rc3. A
bisect seems to indicate it was fixed with
a7c2bb8279d20d853e43c34584eaf2b039de8026 "powerpc: Re-enable preemption
before cpu_die()".

Which may look familiar. :-)

It looks like this patch went to mainline (likely via the powerpc tree), but may
have not gotten back into the -rt branch.

-Will

Quoted 5 lines

--
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team

#32 lore raw
In reply to Steven Rostedt
Quoted 16 lines · In message <[email protected]> you wrote:
Quoted 6 lines · Michael Neuling

We need to call smp_startup_cpu on boot when we the cpus are still in FW. smp_startup_cpu does this for us on boot.

I'm wondering if we just need to move the test down a bit to make sure the preempt_count is set. I've not been following this thread, but maybe this might work?

Egad no! Setting the preempt_count to zero _is_ the bug. I think Darren even said that adding the exit prevented the bug (although now he's hitting a hard lockup someplace else). The original code he was using did not have the condition to return for kexec. It was just a coincidence that this code helped in bringing a CPU back online.

Quoted 1 line
Quoted 5 lines

arch/powerpc/platforms/

Quoted 5 lines

Untested patch below...

Mikey

arch/powerpc/platforms/ renamed from arch/powerpc/platforms/pseries/smp.c +0 -0

pseries/smp.c

Quoted 4 lines

arch/powerpc/platforms/pseries/smp.c static inline int __devinit smp_startup_cpu(unsigned

Quoted 4 lines

index 0317cce..3afaba4 100644

arch/powerpc/platforms/pseries/smp.c +0 -0
@@ -104,18 +104,18 @@ static inline int __devinit smp_startup_cpu(unsigned 

int lcpu)

Quoted 24 lines

arch/powerpc/platforms/pseries/smp.c static inline int __devinit smp_startup_cpu(unsigned int lcpu) · line 108

Quoted 11 lines
arch/powerpc/platforms/pseries/smp.c +0 -6
 	pcpu = get_hard_smp_processor_id(lcpu);
 
-	/* Check to see if the CPU out of FW already for kexec */
-	if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
-		cpumask_set_cpu(lcpu, of_spin_mask);
-		return 1;
-	}
-
 	/* Fixup atomic count: it exited inside IRQ handler. */
 	task_thread_info(paca[lcpu].__current)->preempt_count	= 0;

We DON'T want to do the above. It's nasty! This is one CPU's task touching an intimate part of another CPU's task. It's equivalent of me putting my hand down you wife's blouse. It's offensive, and rude.

OK, if the CPU was never online, then you can do what you want. But what we see is that this fails on CPU hotplug. You stop a CPU, and it goes into this cede_processor() call. When you wake it up, suddenly the task on that woken CPU has its preempt count fscked up. This was really really hard to debug. We thought it was stack corruption or something. But it ended up being that this code has one CPU touching the breasts of another CPU. This code is a pervert!

Quoted 5 lines

What the trace clearly showed, was that we take down a CPU, and in doing so, the code on that CPU set the preempt count to 1, and it expected to have it as 1 when it returned. But the code that kicked started this CPU back to life (bring the CPU back online), set the preempt count on the task of that CPU to 0, and screwed everything up.

/me goes to checks where this came from...

It's been in the kernel since hotplug CPU support was added to ppc64 back in 2004, so I guess we are all at fault for letting this pervert get away with this stuff for so long in plain sight. :-)

So I guess we should remove this but we need to audit all the different paths that go through here to make sure they are OK with preempt. Normal boot, kexec boot, hotplug with FW stop and hotplug with extended_cede all hit this.

Mikey

#33 lore raw
Quoted 64 lines · Michael Neuling
Quoted 16 lines · In message <[email protected]> you wrote:
Quoted 6 lines · Michael Neuling

We need to call smp_startup_cpu on boot when we the cpus are still in FW. smp_startup_cpu does this for us on boot.

I'm wondering if we just need to move the test down a bit to make sure the preempt_count is set. I've not been following this thread, but maybe this might work?

Egad no! Setting the preempt_count to zero _is_ the bug. I think Darren even said that adding the exit prevented the bug (although now he's hitting a hard lockup someplace else). The original code he was using did not have the condition to return for kexec. It was just a coincidence that this code helped in bringing a CPU back online.

Quoted 1 line
Quoted 5 lines

arch/powerpc/platforms/

Quoted 5 lines

Untested patch below...

Mikey

arch/powerpc/platforms/ renamed from arch/powerpc/platforms/pseries/smp.c +0 -0

pseries/smp.c

Quoted 4 lines

arch/powerpc/platforms/pseries/smp.c static inline int __devinit smp_startup_cpu(unsigned

Quoted 4 lines

index 0317cce..3afaba4 100644

arch/powerpc/platforms/pseries/smp.c +0 -0
@@ -104,18 +104,18 @@ static inline int __devinit smp_startup_cpu(unsigned 

int lcpu)

Quoted 24 lines

arch/powerpc/platforms/pseries/smp.c static inline int __devinit smp_startup_cpu(unsigned int lcpu) · line 108

Quoted 11 lines
arch/powerpc/platforms/pseries/smp.c +0 -6
 	pcpu = get_hard_smp_processor_id(lcpu);
 
-	/* Check to see if the CPU out of FW already for kexec */
-	if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
-		cpumask_set_cpu(lcpu, of_spin_mask);
-		return 1;
-	}
-
 	/* Fixup atomic count: it exited inside IRQ handler. */
 	task_thread_info(paca[lcpu].__current)->preempt_count	= 0;

We DON'T want to do the above. It's nasty! This is one CPU's task touching an intimate part of another CPU's task. It's equivalent of me putting my hand down you wife's blouse. It's offensive, and rude.

OK, if the CPU was never online, then you can do what you want. But what we see is that this fails on CPU hotplug. You stop a CPU, and it goes into this cede_processor() call. When you wake it up, suddenly the task on that woken CPU has its preempt count fscked up. This was really really hard to debug. We thought it was stack corruption or something. But it ended up being that this code has one CPU touching the breasts of another CPU. This code is a pervert!

Quoted 5 lines

What the trace clearly showed, was that we take down a CPU, and in doing so, the code on that CPU set the preempt count to 1, and it expected to have it as 1 when it returned. But the code that kicked started this CPU back to life (bring the CPU back online), set the preempt count on the task of that CPU to 0, and screwed everything up.

/me goes to checks where this came from...

It's been in the kernel since hotplug CPU support was added to ppc64 back in 2004, so I guess we are all at fault for letting this pervert get away with this stuff for so long in plain sight. :-)

Quoted 6 lines

So I guess we should remove this but we need to audit all the different paths that go through here to make sure they are OK with preempt. Normal boot, kexec boot, hotplug with FW stop and hotplug with extended_cede all hit this.

Mikey

CC'ing my alter ego.

Signature · 3 lines
Darren Hart
IBM Linux Technology Center
Real-Time Linux Team
#34 lore raw
In reply to Darren Hart
Quoted 1 line

+ /* Check to see if the CPU out of FW already for kexec */

Wow, that comment is shit. The checkin comment in aef40e87d866355ffd279ab21021de733242d0d5 is much better.

Quoted 3 lines

This comment is really confusing to me. I _think_ it is saying that this test determines if the CPU is done executing firmware code and has begun executing OS code.... Is that right?

Yeah.

It means for a normal boot, the CPU will not have started yet (still in firmware (FW)) so we have to call FW to bring it out. In the kexec case though, the CPU will have started already (it's spinning in the kernel) so we don't have to call FW to bring it back out again. To distinguish between these two cases, we ask FW if the CPU has started or not (via smp_query_cpu_stopped()) and if it's already start, don't restart it.

Originally, we could call FW to start a cpu that was already started, but FW changed recently and stopped allowing us to do this. Hence this patch.

Mikey