Re: [PATCH v2 3/6] x86/mtrr: Fix Xorg crashes in Qemu sessions

From: Toshi Kani
Date: Tue Mar 22 2016 - 17:01:14 EST


On Tue, 2016-03-22 at 18:00 +0100, Borislav Petkov wrote:
> On Wed, Mar 16, 2016 at 06:46:56PM -0600, Toshi Kani wrote:
> > A Xorg failure on qemu32 was reported as a regression caused
> > by 'commit 9cd25aac1f44 ("x86/mm/pat: Emulate PAT when it is
> > disabled")'. [1]ÂÂThis patch fixes the regression.
>
> I hope so.
>
> > Negative effects of this regression were two failures in Xorg
> > on qemu32 env, which were triggered by the fact that its virtual
>
> "... with QEMU CPU model "qemu32" (-cpu qemu32) ... "

Will add this description.

> > CPU does not support MTRR. [2]
> >
> > Â#1. copy_process() failed in the check in reserve_pfn_range()
> >
> > ÂÂÂÂcopy_process
> > ÂÂÂÂÂcopy_mm
> > ÂÂÂÂÂÂdup_mm
> > ÂÂÂÂÂÂÂdup_mmap
> > ÂÂÂÂÂÂÂÂcopy_page_range
> > ÂÂÂÂÂÂÂÂÂtrack_pfn_copy
> > ÂÂÂÂÂÂÂÂÂÂreserve_pfn_range
>
> Here's where you describe why it failed the check and which check.

Will do.

> > Â#2. error path in copy_process() then hit WARN_ON_ONCE in
> > ÂÂÂÂÂuntrack_pfn().
> >
> > ÂÂÂÂÂx86/PAT: Xorg:509 map pfn expected mapping type uncached-
> > ÂÂÂÂÂminus for [mem 0xfd000000-0xfdffffff], got write-combining
> > ÂÂÂÂÂÂCall Trace:
> > ÂÂÂÂÂdump_stack+0x58/0x79
> > ÂÂÂÂÂwarn_slowpath_common+0x8b/0xc0
> > ÂÂÂÂÂ? untrack_pfn+0x9f/0xb0
> > ÂÂÂÂÂ? untrack_pfn+0x9f/0xb0
> > ÂÂÂÂÂwarn_slowpath_null+0x22/0x30
> > ÂÂÂÂÂuntrack_pfn+0x9f/0xb0
> > ÂÂÂÂÂ? __kunmap_atomic+0x54/0x110
> > ÂÂÂÂÂunmap_single_vma+0x56f/0x580
> > ÂÂÂÂÂ? pagevec_move_tail_fn+0xa0/0xa0
> > ÂÂÂÂÂunmap_vmas+0x43/0x60
> > ÂÂÂÂÂexit_mmap+0x5f/0xf0
> > ÂÂÂÂÂmmput+0x2d/0xa0
> > ÂÂÂÂÂcopy_process.part.47+0x1229/0x1430
> > ÂÂÂÂÂ_do_fork+0xb4/0x3b0
> > ÂÂÂÂÂSyS_clone+0x2c/0x30
> > ÂÂÂÂÂdo_syscall_32_irqs_on+0x54/0xb0
> > ÂÂÂÂÂentry_INT80_32+0x2a/0x2a
>
> You can delete the offsets after the "+" - they're useless.

Will do.

> > These negative effects are caused by two separate bugs, but they
> > can be dealt in lower priority.
>
> ??

Will change to "they can be addressed in separate patches."

> > Fixing the pat_init() issue below
> > avoids Xorg to hit these cases.
> >
> > When the CPU does not support MTRR, MTRR does not call pat_init(),
> > which leaves PAT enabled without initializing PAT.ÂÂThis pat_init()
> > issue is a long-standing issue, but manifested as issue #1 (and then
> > hit issue #2) with the commit
>
> commit 9cd25aac1f44 ?

Yes. I had to remove this number since checkpatch complained that I needed
to quote the whole patch tile again. ÂI will ignore this checkpatch error
and add this commit number here.

> > because the memtype now tracks cache
> > attribute with 'page_cache_mode'.ÂÂA WC map request is tracked as WC
> > in memtype, but sets a PTE as UC (pgprot) per __cachemode2pte_tbl[].
> > This caused the error in reserve_pfn_range() when it was called from
> > track_pfn_copy(), which obtained pgprot from a PTE.ÂÂIt converts
> > pgprot to page_cache_mode, which does not necessarily result in
> > the original page_cache_mode since __cachemode2pte_tbl[] redirects
> > multiple types to UC.ÂÂThis is a separate issue in reserve_pfn_range().
>
> Good.
>
> > This pat_init() issue existed before the commit, but we used pgprot
> > in memtype.ÂÂHence, we did not have issue #1 before.ÂÂBut WC request
> > resulted in WT in effect because WC pgrot is actually WT when PAT
> > is not initialized.ÂÂThis is not how it was designed to work.ÂÂWhen
> > PAT is set to disable properly, WC is converted to UC.ÂÂThe use of
> > WT can result in a system crash if the target range does not support
> > WT.ÂÂFortunately, nobody ran into such issue before.
> >
> > To fix this pat_init() issue, PAT code has been enhanced to provide
> > pat_disable() interface, which disables the OS to initialize PAT MSR,
>
> ... prevents the OS from initializing the
> PAT MSR.

Will do.

> > and sets PAT table to the BIOS handoff state.
>
> > This patch changes
> > MTRR code to call pat_disable() when MTRR is disabled as PAT cannot
> > be initialized in this case.ÂÂThis sets PAT to disable properly, and
> > makes PAT code to bypass the memtype check.ÂÂThis avoids issue #1
> > (which can be dealt in lower priority).
>
> You don't need all that text from "This patch ..." on - we can see that
> in the diff. The commit message needs to contain "why" not "what".

OK.

> > Â
> > Â/*
> > @@ -83,9 +84,12 @@ static inline int mtrr_trim_uncached_memory(unsigned
> > long end_pfn)
> > Âstatic inline void mtrr_centaur_report_mcr(int mcr, u32 lo, u32 hi)
> > Â{
> > Â}
> > +static inline void mtrr_bp_init(void)
> > +{
> > + pat_disable("Skip PAT initialization");
>
> Make that more user-friendly:
>
> ÂÂÂ"MTRRs disabled, skipping PAT initialization too."

Agreed. Will do.

> > +}
> > Â
> > Â#define mtrr_ap_init() do {} while (0)
> > -#define mtrr_bp_init() do {} while (0)
> > Â#define set_mtrr_aps_delayed_init() do {} while (0)
> > Â#define mtrr_aps_init() do {} while (0)
> > Â#define mtrr_bp_restore() do {} while (0)
> > diff --git a/arch/x86/kernel/cpu/mtrr/main.c
> > b/arch/x86/kernel/cpu/mtrr/main.c
> > index 10f8d47..2d7d8d7 100644
> > --- a/arch/x86/kernel/cpu/mtrr/main.c
> > +++ b/arch/x86/kernel/cpu/mtrr/main.c
> > @@ -759,8 +759,16 @@ void __init mtrr_bp_init(void)
> > Â }
> > Â }
> > Â
> > - if (!mtrr_enabled())
> > + if (!mtrr_enabled()) {
> > Â pr_info("MTRR: Disabled\n");
> > +
> > + /*
> > + Â* PAT initialization relies on MTRR's rendezvous
> > handler.
> > + Â* Skip PAT init until the handler can initialize both
> > + Â* features independently.
> > + Â*/
> > + pat_disable("Skip PAT initialization");
>
> Ditto: you can merge the pr_info text with the pat_disable() string.

Will do.

Thanks,
-Toshi