Skip to content

Commit

Permalink
mips: Call lose_fpu(0) before initializing fcr31 in mips_set_personal…
Browse files Browse the repository at this point in the history
…ity_nan

commit 59be5c35850171e307ca5d3d703ee9ff4096b948 upstream.

If we still own the FPU after initializing fcr31, when we are preempted
the dirty value in the FPU will be read out and stored into fcr31,
clobbering our setting.  This can cause an improper floating-point
environment after execve().  For example:

    zsh% cat measure.c
    #include <fenv.h>
    int main() { return fetestexcept(FE_INEXACT); }
    zsh% cc measure.c -o measure -lm
    zsh% echo $((1.0/3)) # raising FE_INEXACT
    0.33333333333333331
    zsh% while ./measure; do ; done
    (stopped in seconds)

Call lose_fpu(0) before setting fcr31 to prevent this.

Closes: https://lore.kernel.org/linux-mips/[email protected]/
Fixes: 9b26616 ("MIPS: Respect the ISA level in FCSR handling")
Cc: [email protected]
Signed-off-by: Xi Ruoyao <[email protected]>
Signed-off-by: Thomas Bogendoerfer <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
xry111 authored and gregkh committed Feb 23, 2024
1 parent 9bc5b61 commit 4f5ce94
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions arch/mips/kernel/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <asm/cpu-features.h>
#include <asm/cpu-info.h>
#include <asm/fpu.h>

/* Whether to accept legacy-NaN and 2008-NaN user binaries. */
bool mips_use_nan_legacy;
Expand Down Expand Up @@ -311,6 +312,11 @@ void mips_set_personality_nan(struct arch_elf_state *state)
struct cpuinfo_mips *c = &boot_cpu_data;
struct task_struct *t = current;

/* Do this early so t->thread.fpu.fcr31 won't be clobbered in case
* we are preempted before the lose_fpu(0) in start_thread.
*/
lose_fpu(0);

t->thread.fpu.fcr31 = c->fpu_csr31;
switch (state->nan_2008) {
case 0:
Expand Down

0 comments on commit 4f5ce94

Please sign in to comment.