• Dave Hansen's avatar
    x86/apic: Add extra serialization for non-serializing MSRs · 25a068b8
    Dave Hansen authored
    Jan Kiszka reported that the x2apic_wrmsr_fence() function uses a plain
    MFENCE while the Intel SDM (10.12.3 MSR Access in x2APIC Mode) calls for
    MFENCE; LFENCE.
    
    Short summary: we have special MSRs that have weaker ordering than all
    the rest. Add fencing consistent with current SDM recommendations.
    
    This is not known to cause any issues in practice, only in theory.
    
    Longer story below:
    
    The reason the kernel uses a different semantic is that the SDM changed
    (roughly in late 2017). The SDM changed because folks at Intel were
    auditing all of the recommended fences in the SDM and realized that the
    x2apic fences were insufficient.
    
    Why was the pain MFENCE judged insufficient?
    
    WRMSR itself is normally a serializing instruction. No fences are needed
    because the instruction itself serializes everything.
    
    But, there are explicit exceptions for this serializing behavior written
    into the WRMSR instruction documentation for two classes of MSRs:
    IA32_TSC_DEADLINE and the X2APIC MSRs.
    
    Back to x2apic: WRMSR is *not* serializing in this specific case.
    But why is MFENCE insufficient? MFENCE makes writes visible, but
    only affects load/store instructions. WRMSR is unfortunately not a
    load/store instruction and is unaffected by MFENCE. This means that a
    non-serializing WRMSR could be reordered by the CPU to execute before
    the writes made visible by the MFENCE have even occurred in the first
    place.
    
    This means that an x2apic IPI could theoretically be triggered before
    there is any (visible) data to process.
    
    Does this affect anything in practice? I honestly don't know. It seems
    quite possible that by the time an interrupt gets to consume the (not
    yet) MFENCE'd data, it has become visible, mostly by accident.
    
    To be safe, add the SDM-recommended fences for all x2apic WRMSRs.
    
    This also leaves open the question of the _other_ weakly-ordered WRMSR:
    MSR_IA32_TSC_DEADLINE. While it has the same ordering architecture as
    the x2APIC MSRs, it seems substantially less likely to be a problem in
    practice. While writes to the in-memory Local Vector Table (LVT) might
    theoretically be reordered with respect to a weakly-ordered WRMSR like
    TSC_DEADLINE, the SDM has this to say:
    
      In x2APIC mode, the WRMSR instruction is used to write to the LVT
      entry. The processor ensures the ordering of this write and any
      subsequent WRMSR to the deadline; no fencing is required.
    
    But, that might still leave xAPIC exposed. The safest thing to do for
    now is to add the extra, recommended LFENCE.
    
     [ bp: Massage commit message, fix typos, drop accidentally added
       newline to tools/arch/x86/include/asm/barrier.h. ]
    Reported-by: default avatarJan Kiszka <jan.kiszka@siemens.com>
    Signed-off-by: default avatarDave Hansen <dave.hansen@linux.intel.com>
    Signed-off-by: default avatarBorislav Petkov <bp@suse.de>
    Acked-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
    Acked-by: default avatarThomas Gleixner <tglx@linutronix.de>
    Cc: <stable@vger.kernel.org>
    Link: https://lkml.kernel.org/r/20200305174708.F77040DD@viggo.jf.intel.com
    25a068b8
x2apic_phys.c 4.59 KB