Skip to contentSkip to conversation

MIPS: Don't BUG_ON(!is_fpu_owner()) in do_ade() when preemptible

PATCH Superseded Huacai Chen 4 participants 10 messages Jul 11, 2014 – Aug 19, 2014 MIPS

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 MIPS: Don't BUG_ON(!is_fpu_owner()) in do_ade() when preemptible

In do_ade(), is_fpu_owner() isn't preempt-safe. For example, when an unaligned ldc1 is executed, do_cpu() is called and then FPU is enabled (TIF_USEDFPU is set for the current process). Then, do_ade() is called because the access is unaligned. If the current process is preempted at this time, TIF_USEDFPU will be cleard. When the process is scheduled again, BUG_ON(!is_fpu_owner()) is triggered.

This small program can trigger this BUG in a preemptible kernel:
---
int main (int argc, char *argv[])

{
        double u64[2];

        while (1) {
                asm volatile (
                        ".set push \n\t"
                        ".set noreorder \n\t"
                        "ldc1 $f3, 4(%0) \n\t"
                        ".set pop \n\t"
                        ::"r"(u64):
                );
        }

        return 0;
}

---

---
 arch/mips/kernel/unaligned.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/mips/kernel/unaligned.c +2 -1
index 2b35172..a6ff3c2 100644
@@ -690,7 +690,8 @@ static void emulate_load_store_insn(struct pt_regs *regs,
 	case sdc1_op:
 		die_if_kernel("Unaligned FP access in kernel code", regs);
 		BUG_ON(!used_math());
-		BUG_ON(!is_fpu_owner());
+		if (!preemptible())
+			BUG_ON(!is_fpu_owner());
 
 		lose_fpu(1);	/* Save FPU state for the emulator. */
 		res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
Signature · 1 line
1.9.0
#1 lore raw

PATCH 1/1 Not preempt in CP1 exception handling

do_ade may be invoked with preempt enabled. do_cpu will be invoked with preempt enabled. When it's preempted(in do_ade/do_cpu), TIF_USEDFPU will be cleared, when it returns to do_ade/do_cpu, the fpu is actually disabled.

e.g.
In do_ade()
emulate_load_store_insn():
BUG_ON(!is_fpu_owner()); <-- This assertion may be breaked.

In do_cpu()
enable_restore_fp_context():

    was_fpu_owner = is_fpu_owner();

This patch simply disables interrupts in related handlers, and disable preempt/enable interrupts in do_ade/do_cpu.

---
 arch/mips/kernel/genex.S | 4 ++--
 arch/mips/kernel/traps.c | 4 ++++
 2 files changed, 6 insertions(+), 2 deletions(-)
arch/mips/kernel/genex.S +2 -2
index ac35e12..a5c6931 100644
@@ -370,7 +370,7 @@ NESTED(nmi_handler, PT_SIZE, sp)
 	.macro	__build_clear_ade
 	MFC0	t0, CP0_BADVADDR
 	PTR_S	t0, PT_BVADDR(sp)
-	KMODE
+	CLI
 	.endm
 
 	.macro	__BUILD_silent exception
@@ -422,7 +422,7 @@ NESTED(nmi_handler, PT_SIZE, sp)
 	BUILD_HANDLER dbe be cli silent			/* #7  */
 	BUILD_HANDLER bp bp sti silent			/* #9  */
 	BUILD_HANDLER ri ri sti silent			/* #10 */
-	BUILD_HANDLER cpu cpu sti silent		/* #11 */
+	BUILD_HANDLER cpu cpu cli silent		/* #11 */
 	BUILD_HANDLER ov ov sti silent			/* #12 */
 	BUILD_HANDLER tr tr sti silent			/* #13 */
 	BUILD_HANDLER msa_fpe msa_fpe sti silent	/* #14 */
arch/mips/kernel/traps.c +4 -0
index 51706d6..0e0f7de 100644
@@ -1166,6 +1166,9 @@ asmlinkage void do_cpu(struct pt_regs *regs)
 	int status, err;
 	unsigned long __maybe_unused flags;
 
+	preempt_disable();
+	local_irq_enable();
+
 	prev_state = exception_enter();
 	cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
 
@@ -1258,6 +1261,7 @@ asmlinkage void do_cpu(struct pt_regs *regs)
 
 out:
 	exception_exit(prev_state);
+	preempt_enable();
 }
 
 asmlinkage void do_msa_fpe(struct pt_regs *regs)
Signature · 1 line
1.9.0
#3 lore raw

Re: [PATCH] Not preempt in CP1 exception handling

In reply to chenj
Quoted 4 lines · chenj

do_ade may be invoked with preempt enabled. do_cpu will be invoked with preempt enabled. When it's preempted(in do_ade/do_cpu), TIF_USEDFPU will be cleared, when it returns to do_ade/do_cpu, the fpu is actually disabled.

Quoted 8 lines

e.g.
In do_ade()
emulate_load_store_insn():
BUG_ON(!is_fpu_owner()); <-- This assertion may be breaked.

In do_cpu()
enable_restore_fp_context():

    was_fpu_owner = is_fpu_owner();

Preemption should indeed be disabled around the assignment & use of the was_fpu_owner variable, but note that you can only hit the problem if using MSA. One of the MSA fixes I just submitted also fixes this along with another instance of the problem:

http://patchwork.linux-mips.org/patch/7307/

I prefer my patch to this since it disables preemption for less time, in addition to fixing the !used_math() case.

In emulate_load_store_insn I believe the correct fix is simply to remove that BUG_ON. The code is about to give up FPU ownership anyway, so it's not like there is any requirement being violated if it was already lost.

Thanks,
Paul

Quoted 55 lines

This patch simply disables interrupts in related handlers, and disable preempt/enable interrupts in do_ade/do_cpu.

---
 arch/mips/kernel/genex.S | 4 ++--
 arch/mips/kernel/traps.c | 4 ++++
 2 files changed, 6 insertions(+), 2 deletions(-)
arch/mips/kernel/genex.S +2 -2
index ac35e12..a5c6931 100644
@@ -370,7 +370,7 @@ NESTED(nmi_handler, PT_SIZE, sp)
 	.macro	__build_clear_ade
 	MFC0	t0, CP0_BADVADDR
 	PTR_S	t0, PT_BVADDR(sp)
-	KMODE
+	CLI
 	.endm
 
 	.macro	__BUILD_silent exception
@@ -422,7 +422,7 @@ NESTED(nmi_handler, PT_SIZE, sp)
 	BUILD_HANDLER dbe be cli silent			/* #7  */
 	BUILD_HANDLER bp bp sti silent			/* #9  */
 	BUILD_HANDLER ri ri sti silent			/* #10 */
-	BUILD_HANDLER cpu cpu sti silent		/* #11 */
+	BUILD_HANDLER cpu cpu cli silent		/* #11 */
 	BUILD_HANDLER ov ov sti silent			/* #12 */
 	BUILD_HANDLER tr tr sti silent			/* #13 */
 	BUILD_HANDLER msa_fpe msa_fpe sti silent	/* #14 */
arch/mips/kernel/traps.c +4 -0
index 51706d6..0e0f7de 100644
@@ -1166,6 +1166,9 @@ asmlinkage void do_cpu(struct pt_regs *regs)
 	int status, err;
 	unsigned long __maybe_unused flags;
 
+	preempt_disable();
+	local_irq_enable();
+
 	prev_state = exception_enter();
 	cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
 
@@ -1258,6 +1261,7 @@ asmlinkage void do_cpu(struct pt_regs *regs)
 
 out:
 	exception_exit(prev_state);
+	preempt_enable();
 }
 
 asmlinkage void do_msa_fpe(struct pt_regs *regs)
Signature · 1 line
1.9.0
#4 lore raw

Re: [PATCH] Not preempt in CP1 exception handling

2014-07-11 23:56 GMT+08:00 Paul Burton <[email protected]>:

Quoted 19 lines · chenj
Quoted 4 lines

do_ade may be invoked with preempt enabled. do_cpu will be invoked with preempt enabled. When it's preempted(in do_ade/do_cpu), TIF_USEDFPU will be cleared, when it returns to do_ade/do_cpu, the fpu is actually disabled.

Quoted 8 lines

e.g.
In do_ade()
emulate_load_store_insn():
BUG_ON(!is_fpu_owner()); <-- This assertion may be breaked.

In do_cpu()
enable_restore_fp_context():

    was_fpu_owner = is_fpu_owner();

Preemption should indeed be disabled around the assignment & use of the was_fpu_owner variable, but note that you can only hit the problem if using MSA. One of the MSA fixes I just submitted also fixes this along with another instance of the problem:

Quoted 8 lines

http://patchwork.linux-mips.org/patch/7307/

I prefer my patch to this since it disables preemption for less time, in addition to fixing the !used_math() case.

In emulate_load_store_insn I believe the correct fix is simply to remove that BUG_ON. The code is about to give up FPU ownership anyway, so it's not like there is any requirement being violated if it was already lost.

Yes, you're right.

""" /* arch/mips/kernel/unaligned.c */
lose_fpu(1); /* Save FPU state for the emulator. */
res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1, &fault_addr);
own_fpu(1); /* Restore FPU state. */
"""

Going deep into the code, I find lost_fpu(1) will save fpu context if owns fpu (otherwise, if preempted, the fpu context will be saved in process switch), then fpu_emulator_cop1Handler manipulates the saved fpu context, own_fpu(1) restores it.

So, remove "BUG_ON(!is_fpu_owner())" is OK.

#5 lore raw

Re: [PATCH] Not preempt in CP1 exception handling

Hi, Paul,

You means my patch (http://patchwork.linux-mips.org/patch/7297/) is the correct way?

Another question: Your patch
(http://patchwork.linux-mips.org/patch/7307/) remove preempt_disable()/preempt_enable() in init_fpu(). It will cause problems if there is another function call init_fpu() because it is previously preempt-safe. Maybe introduce a new function (e.g. __init_fpu()) is a better way?

Huacai

Quoted 43 lines · Chen Jie

2014-07-11 23:56 GMT+08:00 Paul Burton <[email protected]>:

Quoted 19 lines · chenj
Quoted 4 lines

do_ade may be invoked with preempt enabled. do_cpu will be invoked with preempt enabled. When it's preempted(in do_ade/do_cpu), TIF_USEDFPU will be cleared, when it returns to do_ade/do_cpu, the fpu is actually disabled.

Quoted 8 lines

e.g.
In do_ade()
emulate_load_store_insn():
BUG_ON(!is_fpu_owner()); <-- This assertion may be breaked.

In do_cpu()
enable_restore_fp_context():

    was_fpu_owner = is_fpu_owner();

Preemption should indeed be disabled around the assignment & use of the was_fpu_owner variable, but note that you can only hit the problem if using MSA. One of the MSA fixes I just submitted also fixes this along with another instance of the problem:

Quoted 8 lines

http://patchwork.linux-mips.org/patch/7307/

I prefer my patch to this since it disables preemption for less time, in addition to fixing the !used_math() case.

In emulate_load_store_insn I believe the correct fix is simply to remove that BUG_ON. The code is about to give up FPU ownership anyway, so it's not like there is any requirement being violated if it was already lost.

Yes, you're right.

""" /* arch/mips/kernel/unaligned.c */
lose_fpu(1); /* Save FPU state for the emulator. */
res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1, &fault_addr);
own_fpu(1); /* Restore FPU state. */
"""

Going deep into the code, I find lost_fpu(1) will save fpu context if owns fpu (otherwise, if preempted, the fpu context will be saved in process switch), then fpu_emulator_cop1Handler manipulates the saved fpu context, own_fpu(1) restores it.

So, remove "BUG_ON(!is_fpu_owner())" is OK.

#6 lore raw

Re: [PATCH] Not preempt in CP1 exception handling

Quoted 4 lines · Huacai Chen

Hi, Paul,

You means my patch (http://patchwork.linux-mips.org/patch/7297/) is the correct way?

I believe you patch will fix the problem, but I think it would be better to remove the check for !preemptible() & the BUG_ON entirely.

Quoted 6 lines

Another question: Your patch
(http://patchwork.linux-mips.org/patch/7307/) remove preempt_disable()/preempt_enable() in init_fpu(). It will cause problems if there is another function call init_fpu() because it is previously preempt-safe. Maybe introduce a new function (e.g. __init_fpu()) is a better way?

It may cause a problem if there were other callers, but there is only one caller of init_fpu (enable_restore_fp_context) and it needs to disable preemption for longer than the init_fpu function anyway. I see no value in keeping init_fpu as a wrapper that disables preemption when there would be nothing calling it.

Thanks,
Paul

Quoted 46 lines

Huacai

Quoted 43 lines · Chen Jie

2014-07-11 23:56 GMT+08:00 Paul Burton <[email protected]>:

Quoted 19 lines · chenj
Quoted 4 lines

do_ade may be invoked with preempt enabled. do_cpu will be invoked with preempt enabled. When it's preempted(in do_ade/do_cpu), TIF_USEDFPU will be cleared, when it returns to do_ade/do_cpu, the fpu is actually disabled.

Quoted 8 lines

e.g.
In do_ade()
emulate_load_store_insn():
BUG_ON(!is_fpu_owner()); <-- This assertion may be breaked.

In do_cpu()
enable_restore_fp_context():

    was_fpu_owner = is_fpu_owner();

Preemption should indeed be disabled around the assignment & use of the was_fpu_owner variable, but note that you can only hit the problem if using MSA. One of the MSA fixes I just submitted also fixes this along with another instance of the problem:

Quoted 8 lines

http://patchwork.linux-mips.org/patch/7307/

I prefer my patch to this since it disables preemption for less time, in addition to fixing the !used_math() case.

In emulate_load_store_insn I believe the correct fix is simply to remove that BUG_ON. The code is about to give up FPU ownership anyway, so it's not like there is any requirement being violated if it was already lost.

Yes, you're right.

""" /* arch/mips/kernel/unaligned.c */
lose_fpu(1); /* Save FPU state for the emulator. */
res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1, &fault_addr);
own_fpu(1); /* Restore FPU state. */
"""

Going deep into the code, I find lost_fpu(1) will save fpu context if owns fpu (otherwise, if preempted, the fpu context will be saved in process switch), then fpu_emulator_cop1Handler manipulates the saved fpu context, own_fpu(1) restores it.

So, remove "BUG_ON(!is_fpu_owner())" is OK.

#7 lore raw

Re: [PATCH] Not preempt in CP1 exception handling

Hi, Ralf,

What do you think about? If you also prefer to totally remove the BUG_ON(), I will send a new patch.

Quoted 72 lines · Paul Burton
Quoted 4 lines · Huacai Chen

Hi, Paul,

You means my patch (http://patchwork.linux-mips.org/patch/7297/) is the correct way?

I believe you patch will fix the problem, but I think it would be better to remove the check for !preemptible() & the BUG_ON entirely.

Quoted 6 lines

Another question: Your patch
(http://patchwork.linux-mips.org/patch/7307/) remove preempt_disable()/preempt_enable() in init_fpu(). It will cause problems if there is another function call init_fpu() because it is previously preempt-safe. Maybe introduce a new function (e.g. __init_fpu()) is a better way?

It may cause a problem if there were other callers, but there is only one caller of init_fpu (enable_restore_fp_context) and it needs to disable preemption for longer than the init_fpu function anyway. I see no value in keeping init_fpu as a wrapper that disables preemption when there would be nothing calling it.

Thanks,
Paul

Quoted 46 lines

Huacai

Quoted 43 lines · Chen Jie

2014-07-11 23:56 GMT+08:00 Paul Burton <[email protected]>:

Quoted 19 lines · chenj
Quoted 4 lines

do_ade may be invoked with preempt enabled. do_cpu will be invoked with preempt enabled. When it's preempted(in do_ade/do_cpu), TIF_USEDFPU will be cleared, when it returns to do_ade/do_cpu, the fpu is actually disabled.

Quoted 8 lines

e.g.
In do_ade()
emulate_load_store_insn():
BUG_ON(!is_fpu_owner()); <-- This assertion may be breaked.

In do_cpu()
enable_restore_fp_context():

    was_fpu_owner = is_fpu_owner();

Preemption should indeed be disabled around the assignment & use of the was_fpu_owner variable, but note that you can only hit the problem if using MSA. One of the MSA fixes I just submitted also fixes this along with another instance of the problem:

Quoted 8 lines

http://patchwork.linux-mips.org/patch/7307/

I prefer my patch to this since it disables preemption for less time, in addition to fixing the !used_math() case.

In emulate_load_store_insn I believe the correct fix is simply to remove that BUG_ON. The code is about to give up FPU ownership anyway, so it's not like there is any requirement being violated if it was already lost.

Yes, you're right.

""" /* arch/mips/kernel/unaligned.c */
lose_fpu(1); /* Save FPU state for the emulator. */
res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1, &fault_addr);
own_fpu(1); /* Restore FPU state. */
"""

Going deep into the code, I find lost_fpu(1) will save fpu context if owns fpu (otherwise, if preempted, the fpu context will be saved in process switch), then fpu_emulator_cop1Handler manipulates the saved fpu context, own_fpu(1) restores it.

So, remove "BUG_ON(!is_fpu_owner())" is OK.

#8 lore raw

Re: [PATCH] Not preempt in CP1 exception handling

Quoted 2 lines · Huacai Chen

What do you think about? If you also prefer to totally remove the BUG_ON(), I will send a new patch.

I agree, removing the BUG_ON() is the right way.

Ralf

#9 lore raw

Re: [PATCH] Not preempt in CP1 exception handling

Please mark this patch as "Superseded", since patch "MIPS: Don't BUG_ON(!is_fpu_owner()) in do_ade()"[1] was accepted :)

2014-08-02 0:48 GMT+08:00 Ralf Baechle <[email protected]>:

Quoted 8 lines · Huacai Chen
Quoted 3 lines

What do you think about? If you also prefer to totally remove the BUG_ON(), I will send a new patch.

I agree, removing the BUG_ON() is the right way.

Ralf

---
1. http://patchwork.linux-mips.org/patch/7354/