powerpc/64: Fix msr_check_and_set/clear MSR[EE] race
authorNicholas Piggin <npiggin@gmail.com>
Tue, 4 Oct 2022 05:11:57 +0000 (15:11 +1000)
committerMichael Ellerman <mpe@ellerman.id.au>
Tue, 4 Oct 2022 12:16:20 +0000 (23:16 +1100)
irq soft-masking means that when Linux irqs are disabled, the MSR[EE]
value can change from 1 to 0 asynchronously: if a masked interrupt of
the PACA_IRQ_MUST_HARD_MASK variety fires while irqs are disabled,
the masked handler will return with MSR[EE]=0.

This means a sequence like mtmsr(mfmsr() | MSR_FP) is racy if it can
be called with local irqs disabled, unless a hard_irq_disable has been
done.

Reported-by: Sachin Sant <sachinp@linux.ibm.com>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20221004051157.308999-2-npiggin@gmail.com
arch/powerpc/include/asm/hw_irq.h
arch/powerpc/kernel/process.c

index e8de249..77fa88c 100644 (file)
@@ -471,6 +471,30 @@ static inline void irq_soft_mask_regs_set_state(struct pt_regs *regs, unsigned l
 }
 #endif /* CONFIG_PPC64 */
 
+static inline unsigned long mtmsr_isync_irqsafe(unsigned long msr)
+{
+#ifdef CONFIG_PPC64
+       if (arch_irqs_disabled()) {
+               /*
+                * With soft-masking, MSR[EE] can change from 1 to 0
+                * asynchronously when irqs are disabled, and we don't want to
+                * set MSR[EE] back to 1 here if that has happened. A race-free
+                * way to do this is ensure EE is already 0. Another way it
+                * could be done is with a RESTART_TABLE handler, but that's
+                * probably overkill here.
+                */
+               msr &= ~MSR_EE;
+               mtmsr_isync(msr);
+               irq_soft_mask_set(IRQS_ALL_DISABLED);
+               local_paca->irq_happened |= PACA_IRQ_HARD_DIS;
+       } else
+#endif
+               mtmsr_isync(msr);
+
+       return msr;
+}
+
+
 #define ARCH_IRQ_INIT_FLAGS    IRQ_NOREQUEST
 
 #endif  /* __ASSEMBLY__ */
index 0fbda89..37df042 100644 (file)
@@ -127,7 +127,7 @@ unsigned long notrace msr_check_and_set(unsigned long bits)
                newmsr |= MSR_VSX;
 
        if (oldmsr != newmsr)
-               mtmsr_isync(newmsr);
+               newmsr = mtmsr_isync_irqsafe(newmsr);
 
        return newmsr;
 }
@@ -145,7 +145,7 @@ void notrace __msr_check_and_clear(unsigned long bits)
                newmsr &= ~MSR_VSX;
 
        if (oldmsr != newmsr)
-               mtmsr_isync(newmsr);
+               mtmsr_isync_irqsafe(newmsr);
 }
 EXPORT_SYMBOL(__msr_check_and_clear);