Skip to contentSkip to conversation

KVM: PPC: disable preemption when using hard_irq_disable()

PATCH In review Tiejun Chen 4 participants 17 messages Jul 10, 2013 – Jul 15, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: PPC: disable preemption when using hard_irq_disable()

We should ensure the preemption cannot occur while calling get_paca() insdide hard_irq_disable(), otherwise the paca_struct may be the wrong one just after. And btw, we may update timing stats in this case.

---
 arch/powerpc/kvm/booke.c |    2 ++
 1 file changed, 2 insertions(+)
arch/powerpc/kvm/booke.c +2 -0
index dcc94f0..9dae25d 100644
@@ -839,6 +839,7 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
 	WARN_ON(local_paca->irq_happened != 0);
 #endif
 
+	preempt_disable();
 	/*
 	 * We enter with interrupts disabled in hardware, but
 	 * we need to call hard_irq_disable anyway to ensure that
@@ -848,6 +849,7 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
 
 	/* update before a new last_exit_type is rewritten */
 	kvmppc_update_timing_stats(vcpu);
+	preempt_enable();
 
 	/* restart interrupts if they were meant for the host */
 	kvmppc_restart_interrupt(vcpu, exit_nr);
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 22 lines · Tiejun Chen

We should ensure the preemption cannot occur while calling get_paca() insdide hard_irq_disable(), otherwise the paca_struct may be the wrong one just after. And btw, we may update timing stats in this case.

---
arch/powerpc/kvm/booke.c | 2 ++

1 file changed, 2 insertions(+)
arch/powerpc/kvm/booke.c +1 -0
index dcc94f0..9dae25d 100644
@@ -839,6 +839,7 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
 	WARN_ON(local_paca->irq_happened != 0);
 #endif
 
+	preempt_disable();
 	/*
 	 * We enter with interrupts disabled in hardware, but
 	 * we need to call hard_irq_disable anyway to ensure that

arch/powerpc/kvm/booke.c int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, · line 852

Quoted 4 lines
arch/powerpc/kvm/booke.c +1 -0
@@ -848,6 +849,7 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
 
 	/* update before a new last_exit_type is rewritten */
 	kvmppc_update_timing_stats(vcpu);
+	preempt_enable();

All of the code here is already called with interrupts disabled. I don't see how we could preempt then?

Alex

Quoted 10 lines
arch/powerpc/kvm/booke.c +0 -0
 	/* restart interrupts if they were meant for the host */
 	kvmppc_restart_interrupt(vcpu, exit_nr);
Signature · 6 lines
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe kvm-ppc" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
Quoted 26 lines · Alexander Graf
Quoted 23 lines · Tiejun Chen

We should ensure the preemption cannot occur while calling get_paca() insdide hard_irq_disable(), otherwise the paca_struct may be the wrong one just after. And btw, we may update timing stats in this case.

---
arch/powerpc/kvm/booke.c | 2 ++

1 file changed, 2 insertions(+)
arch/powerpc/kvm/booke.c +1 -0
index dcc94f0..9dae25d 100644
@@ -839,6 +839,7 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
 	WARN_ON(local_paca->irq_happened != 0);
 #endif
 
+	preempt_disable();
 	/*
 	 * We enter with interrupts disabled in hardware, but
 	 * we need to call hard_irq_disable anyway to ensure that
@@ -848,6 +849,7 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
 
Quoted 5 lines

arch/powerpc/kvm/booke.c int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, · line 852

Quoted 3 lines
arch/powerpc/kvm/booke.c +1 -0
 	/* update before a new last_exit_type is rewritten */
 	kvmppc_update_timing_stats(vcpu);
+	preempt_enable();

All of the code here is already called with interrupts disabled. I don't see how we could preempt then?

But the kernel still check this in preempt case,

#define get_paca() ((void) debug_smp_processor_id(), local_paca)

then trigger that known call trace as I observed :)

BUG: using smp_processor_id() in preemptible [00000000] code: qemu-system-ppc/2065
caller is .kvmppc_handle_exit+0x48/0x810
CPU: 0 PID: 2065 Comm: qemu-system-ppc Not tainted 3.10.0-172036-ge2daa28-dirty #116
Call Trace:
[c0000001fc637570] [c00000000000835c] .show_stack+0x7c/0x1f0 (unreliable)
[c0000001fc637640] [c000000000673a0c] .dump_stack+0x28/0x3c
[c0000001fc6376b0] [c0000000002f04d8] .debug_smp_processor_id+0x108/0x120
[c0000001fc637740] [c0000000000444e8] .kvmppc_handle_exit+0x48/0x810
[c0000001fc6377f0] [c000000000047c80] .kvmppc_resume_host+0xa4/0xf8

Tiejun

#3 lore raw
Quoted 33 lines · tiejun.chen
Quoted 26 lines · Alexander Graf
Quoted 23 lines · Tiejun Chen

We should ensure the preemption cannot occur while calling get_paca() insdide hard_irq_disable(), otherwise the paca_struct may be the wrong one just after. And btw, we may update timing stats in this case.

---
arch/powerpc/kvm/booke.c | 2 ++

1 file changed, 2 insertions(+)
arch/powerpc/kvm/booke.c +1 -0
index dcc94f0..9dae25d 100644
@@ -839,6 +839,7 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
 	WARN_ON(local_paca->irq_happened != 0);
 #endif
 
+	preempt_disable();
 	/*
 	 * We enter with interrupts disabled in hardware, but
 	 * we need to call hard_irq_disable anyway to ensure that
@@ -848,6 +849,7 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
 
Quoted 5 lines

arch/powerpc/kvm/booke.c int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, · line 852

Quoted 3 lines
arch/powerpc/kvm/booke.c +1 -0
 	/* update before a new last_exit_type is rewritten */
 	kvmppc_update_timing_stats(vcpu);
+	preempt_enable();

All of the code here is already called with interrupts disabled. I don't see how we could preempt then?

Quoted 7 lines

But the kernel still check this in preempt case,

#define get_paca() ((void) debug_smp_processor_id(), local_paca)

then trigger that known call trace as I observed :)

BUG: using smp_processor_id() in preemptible [00000000] code: qemu-system-ppc/2065
# define preemptible()	(preempt_count() == 0 && !irqs_disabled())

So we are only hitting this BUG() when either preempt_count is 0 (which your patch is trying to fix) and at the same time interrupts are enabled. But wait - interrupts are disabled, aren't they? Let's check.

#define irqs_disabled()                                 \
        ({                                              \
                unsigned long _flags;                   \
                raw_local_save_flags(_flags);           \
                raw_irqs_disabled_flags(_flags);        \
        })
#define raw_irqs_disabled_flags(flags)                  \
        ({                                              \
                typecheck(unsigned long, flags);        \
                arch_irqs_disabled_flags(flags);        \
        })

static inline unsigned long arch_local_save_flags(void)

{
        unsigned long flags;

        asm volatile(
                "lbz %0,%1(13)"
                : "=r" (flags)
                : "i" (offsetof(struct paca_struct, soft_enabled)));

        return flags;
}

static inline bool arch_irqs_disabled_flags(unsigned long flags)

{
        return flags == 0;
}

So we're running with soft_enabled == 0 here which means that irqs_disabled() also returns 0.

Ben, is soft_enabled == 0; hard_enabled == 1 a valid combination that may ever occur?

Alex

Quoted 15 lines

caller is .kvmppc_handle_exit+0x48/0x810

CPU: 0 PID: 2065 Comm: qemu-system-ppc Not tainted 3.10.0-172036-ge2daa28-dirty #116
Call Trace:
[c0000001fc637570] [c00000000000835c] .show_stack+0x7c/0x1f0 (unreliable)
[c0000001fc637640] [c000000000673a0c] .dump_stack+0x28/0x3c
[c0000001fc6376b0] [c0000000002f04d8] .debug_smp_processor_id+0x108/0x120
[c0000001fc637740] [c0000000000444e8] .kvmppc_handle_exit+0x48/0x810
[c0000001fc6377f0] [c000000000047c80] .kvmppc_resume_host+0xa4/0xf8

Tiejun

--
To unsubscribe from this list: send the line "unsubscribe kvm-ppc" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw
Quoted 2 lines · Alexander Graf

Ben, is soft_enabled == 0; hard_enabled == 1 a valid combination that may ever occur?

Yes of course, that's what we call "soft disabled" :-) It's even the whole point of doing lazy disable...

Ben.

#5 lore raw
Quoted 6 lines · Benjamin Herrenschmidt
Quoted 2 lines · Alexander Graf

Ben, is soft_enabled = 0; hard_enabled = 1 a valid combination that may ever occur?

Yes of course, that's what we call "soft disabled" :-) It's even the whole point of doing lazy disable...

Meh. Of course it's soft_enabled = 1; hard_enabled = 0.

Alex

#6 lore raw
Quoted 4 lines · Alexander Graf
Quoted 2 lines

Yes of course, that's what we call "soft disabled" :-) It's even the whole point of doing lazy disable...

Meh. Of course it's soft_enabled = 1; hard_enabled = 0.

That doesn't happen in "normal" C code. It happens under very specific circumstances, such as in the guts of entry_64.S, in areas where we just want to temporarily mask HW interrupts without changing the SW state (and thus without having to deal with replays etc...).

We typically also do that right before going to idle on some processors where we come back from idle with interrupts hard enabled, possibly right in an interrupt vector.

Typically that's a state that makes some sense on KVM entry. From a Linux perspective, you enter KVM with interrupts enabled. But you temporarily hard disable on the way down while doing the low level context switch.

This works well as long as you know you have no pending replay event. That should be fine if you do a direct transition from soft+hard enabled to hard disabled (without soft disabling). In that case there should be nothing in irq_happened.

It's equivalent to returning to userspace from the kernel. We are soft-enabled, but the code in ret_from_except hard disables while mucking around with TIF_FLAGS etc... until the final rfid

Cheers,
Ben.

#7 lore raw
Quoted 20 lines · Benjamin Herrenschmidt
Quoted 4 lines · Alexander Graf
Quoted 2 lines

Yes of course, that's what we call "soft disabled" :-) It's even the whole point of doing lazy disable...

Meh. Of course it's soft_enabled = 1; hard_enabled = 0.

That doesn't happen in "normal" C code. It happens under very specific circumstances, such as in the guts of entry_64.S, in areas where we just want to temporarily mask HW interrupts without changing the SW state (and thus without having to deal with replays etc...).

We typically also do that right before going to idle on some processors where we come back from idle with interrupts hard enabled, possibly right in an interrupt vector.

Typically that's a state that makes some sense on KVM entry. From a Linux perspective, you enter KVM with interrupts enabled. But you temporarily hard disable on the way down while doing the low level context switch.

Quoted 8 lines

This works well as long as you know you have no pending replay event. That should be fine if you do a direct transition from soft+hard enabled to hard disabled (without soft disabling). In that case there should be nothing in irq_happened.

It's equivalent to returning to userspace from the kernel. We are soft-enabled, but the code in ret_from_except hard disables while mucking around with TIF_FLAGS etc... until the final rfid

Ok, let me quickly explain the problem.

We are leaving host context, switching slowly into guest context. During that transition we call get_paca() indirectly (apparently by another call to hard_disable() which sounds bogus, but that's another story).

get_paca() warns when we're preemptible. We're only not preemptible when either preempt is disabled or irqs are disabled. Irqs are disabled, but arch_irqs_disabled() doesn't know, because it only checks for soft disabled IRQs.

So we can fix this either by setting IRQs as soft disabled as well or by disabling preemption until we enter the guest for real. Any preferences?

Alex

#8 lore raw
Quoted 7 lines · Alexander Graf

Ok, let me quickly explain the problem.

We are leaving host context, switching slowly into guest context. During that transition we call get_paca() indirectly (apparently by another call to hard_disable() which sounds bogus, but that's another story).

Quoted 8 lines

get_paca() warns when we're preemptible. We're only not preemptible when either preempt is disabled or irqs are disabled. Irqs are disabled, but arch_irqs_disabled() doesn't know, because it only checks for soft disabled IRQs.

So we can fix this either by setting IRQs as soft disabled as well or by disabling preemption until we enter the guest for real. Any preferences?

Well, if you hard disable first (ie, direct transition from full enabled to hard disabled), you know you have nothing lazy pending in irq_pending, then it's ok to mess around with local_paca->soft_enabled to make it "look disabled".

IE. Call hard_irq_disable(), then only turn local_paca->soft_enabled back on late in the process, some time before the final rfi(d).

That works as long as you had a transition from full enabled to full disabled and don't hard re-enable in the process. IE, You are certain that there is nothing pending in irq_happened.

HOWEVER !

If you do that, you *ALSO* need to clear irq_happened. You must *NEVER* leave PACA_IRQ_HARD_DIS set in irq_happened if you are soft-enabled, and since the above means that you *will* be seen as soft enabled on the way out of the guest, you can kaboom.

BTW. I'm fine with a patch that does:

#define hard_irq_disable()	do {			\
	u8 _was_enabled = get_paca()->soft_enabled;	\
	__hard_irq_disable();				\

- get_paca()->soft_enabled = 0; \
+ local_paca->soft_enabled = 0; \

In fact we should probably do it anyway.

Cheers,
Ben.

#9 lore raw
Quoted 36 lines · Benjamin Herrenschmidt
Quoted 7 lines · Alexander Graf

Ok, let me quickly explain the problem.

We are leaving host context, switching slowly into guest context. During that transition we call get_paca() indirectly (apparently by another call to hard_disable() which sounds bogus, but that's another story).

Quoted 8 lines

get_paca() warns when we're preemptible. We're only not preemptible when either preempt is disabled or irqs are disabled. Irqs are disabled, but arch_irqs_disabled() doesn't know, because it only checks for soft disabled IRQs.

So we can fix this either by setting IRQs as soft disabled as well or by disabling preemption until we enter the guest for real. Any preferences?

Well, if you hard disable first (ie, direct transition from full enabled to hard disabled), you know you have nothing lazy pending in irq_pending, then it's ok to mess around with local_paca->soft_enabled to make it "look disabled".

IE. Call hard_irq_disable(), then only turn local_paca->soft_enabled back on late in the process, some time before the final rfi(d).

That works as long as you had a transition from full enabled to full disabled and don't hard re-enable in the process. IE, You are certain that there is nothing pending in irq_happened.

HOWEVER !

If you do that, you *ALSO* need to clear irq_happened. You must *NEVER* leave PACA_IRQ_HARD_DIS set in irq_happened if you are soft-enabled, and since the above means that you *will* be seen as soft enabled on the way out of the guest, you can kaboom.

Quoted 4 lines

BTW. I'm fine with a patch that does:

#define hard_irq_disable()	do {			\
	u8 _was_enabled = get_paca()->soft_enabled;	\

Current problem I met is issued from the above line.

Review

Quoted 2 lines
	__hard_irq_disable();				\
(quoted diff) +0 -1
-	get_paca()->soft_enabled = 0;			\

Not here.

If I'm misunderstanding what you guys means, please correct me since this is a long discussion thread. I have to reread that carefully.

Tiejun

Quoted 10 lines
(quoted diff) +1 -0
+	local_paca->soft_enabled = 0;			\

In fact we should probably do it anyway.

Cheers,
Ben.

#10 lore raw
Quoted 5 lines · tiejun.chen
Quoted 2 lines
#define hard_irq_disable()    do {                    \
      u8 _was_enabled = get_paca()->soft_enabled;     \

Current problem I met is issued from the above line.

Quoted 7 lines
Quoted 2 lines
      __hard_irq_disable();                           \

- get_paca()->soft_enabled = 0; \

Not here.

If I'm misunderstanding what you guys means, please correct me since this is a long discussion thread. I have to reread that carefully.

Then make it

	u8 _was_enabled;
	__hard_irq_disable();
	was_enabled = local_paca->....

Once you have hard disabled, using local_paca directly *should* be safe (minus that gcc problem I mentioned).

Cheers,
Ben.

#11 lore raw
Quoted 14 lines · Benjamin Herrenschmidt
Quoted 5 lines · tiejun.chen
Quoted 2 lines
#define hard_irq_disable()    do {                    \
       u8 _was_enabled = get_paca()->soft_enabled;     \

Current problem I met is issued from the above line.

Quoted 7 lines
Quoted 2 lines
       __hard_irq_disable();                           \

- get_paca()->soft_enabled = 0; \

Not here.

If I'm misunderstanding what you guys means, please correct me since this is a long discussion thread. I have to reread that carefully.

Quoted 7 lines

Then make it

	u8 _was_enabled;
	__hard_irq_disable();
	was_enabled = local_paca->....

Once you have hard disabled, using local_paca directly *should* be safe (minus that gcc problem I mentioned).

Is the following fine?

powerpc: to access local paca after hard irq disabled

We can access paca directly after hard interrupt disabled, and this can avoid accessing wrong paca when using get_paca() in preempt case.

---
 arch/powerpc/include/asm/hw_irq.h |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/hw_irq.h +4 -3
index ba713f1..10be1dd 100644
@@ -96,10 +96,11 @@ static inline bool arch_irqs_disabled(void)
 #endif
 
 #define hard_irq_disable()     do {                    \
-       u8 _was_enabled = get_paca()->soft_enabled;     \
+       u8 _was_enabled;                                \
        __hard_irq_disable();                           \
-       get_paca()->soft_enabled = 0;                   \
-       get_paca()->irq_happened |= PACA_IRQ_HARD_DIS;  \
+       _was_enabled = local_paca->soft_enabled;        \
+       local_paca->soft_enabled = 0;                   \
+       local_paca->irq_happened |= PACA_IRQ_HARD_DIS;  \
        if (_was_enabled)                               \
                trace_hardirqs_off();                   \
 } while(0)
Signature · 10 lines
1.7.9.5


Or what about that change to call SOFT_DISABLE_INTS only in KVM scenario? Which better?

Then I can send to review?

Thanks,

Tiejun
#12 lore raw
Quoted 9 lines · tiejun.chen

Is the following fine?

powerpc: to access local paca after hard irq disabled

We can access paca directly after hard interrupt disabled, and this can avoid accessing wrong paca when using get_paca() in preempt case.

Ack. We still have an unresolved problem where gcc decides to copy r13 to another register and then index from that, or even store and reload it, and this possibly accross preempt sections.

It's unclear to me in what circumstances it will do it and whether there's a case of us getting completely screwed over, I need to investigate. This is the reason why we originally made the accesses to soft_enabled be inline asm.

We might need to do a bulk conversion of all PACA accesses to either such inline asm or "hide" r13 behind asm (forcing essentially a copy to another register on each use) or a combination of both.

IE. inline asm for direct access of things like soft_enabled, and a get_paca/put_paca style interface that copies r13 and includes a preempt_disable/enable for the rest.

Cheers,
Ben.

#13 lore raw
Quoted 11 lines · Benjamin Herrenschmidt
Quoted 9 lines · tiejun.chen

Is the following fine?

powerpc: to access local paca after hard irq disabled

We can access paca directly after hard interrupt disabled, and this can avoid accessing wrong paca when using get_paca() in preempt case.

Quoted 8 lines

Ack. We still have an unresolved problem where gcc decides to copy r13 to another register and then index from that, or even store and reload it, and this possibly accross preempt sections.

It's unclear to me in what circumstances it will do it and whether there's a case of us getting completely screwed over, I need to investigate. This is the reason why we originally made the accesses to soft_enabled be inline asm.

Understood.

Quoted 9 lines

We might need to do a bulk conversion of all PACA accesses to either such inline asm or "hide" r13 behind asm (forcing essentially a copy to another register on each use) or a combination of both.

IE. inline asm for direct access of things like soft_enabled, and a get_paca/put_paca style interface that copies r13 and includes a preempt_disable/enable for the rest.

I'd like to check this possibility later.

Tiejun

#14 lore raw
In reply to Tiejun Chen
Quoted 3 lines · Tiejun Chen

We should ensure the preemption cannot occur while calling get_paca() insdide hard_irq_disable(), otherwise the paca_struct may be the wrong one just after. And btw, we may update timing stats in this case.

The soft-ee mechanism depends on accessing the PACA directly via r13 to avoid this. We probably should be using inline asm to read was_enabled rather than hoping the compiler doesn't do anything silly.

Plus what Alex said, regarding this patch specifically.

-Scott

#15 lore raw
Quoted 7 lines · Scott Wood
Quoted 3 lines · Tiejun Chen

We should ensure the preemption cannot occur while calling get_paca() insdide hard_irq_disable(), otherwise the paca_struct may be the wrong one just after. And btw, we may update timing stats in this case.

The soft-ee mechanism depends on accessing the PACA directly via r13 to avoid this. We probably should be using inline asm to read was_enabled rather than

Yes.

Quoted 1 line

hoping the compiler doesn't do anything silly.

Do you recommend I should directly replace get_paca() with local_paca inside hard_irq_disable()?

#define hard_irq_disable()      do {                    \
        u8 _was_enabled = get_paca()->soft_enabled;     \

-> u8 _was_enabled = local_paca->soft_enabled;

But is this safe for all scenarios?

Tiejun

#16 lore raw
Quoted 13 lines · tiejun.chen
Quoted 7 lines · Scott Wood
Quoted 3 lines · Tiejun Chen

We should ensure the preemption cannot occur while calling get_paca() insdide hard_irq_disable(), otherwise the paca_struct may be the wrong one just after. And btw, we may update timing stats in this case.

The soft-ee mechanism depends on accessing the PACA directly via r13 to avoid this. We probably should be using inline asm to read was_enabled rather than

Yes.

Quoted 1 line

hoping the compiler doesn't do anything silly.

Quoted 8 lines

Do you recommend I should directly replace get_paca() with local_paca inside hard_irq_disable()?

#define hard_irq_disable()      do {                    \
        u8 _was_enabled = get_paca()->soft_enabled;     \

-> u8 _was_enabled = local_paca->soft_enabled;

But is this safe for all scenarios?

get_paca() is just a #define for local_paca. It won't make a difference, other than to evade the debug check.

In practice, it's unlikely that GCC would do anything other than a load directly from r13, but to be sure we should use inline asm to do the load, just like arch_local_save_flags and arch_local_irq_disable do.

-Scott