Skip to content

Commit

Permalink
x86/mm/pat: Don't report PAT on CPUs that don't support it
Browse files Browse the repository at this point in the history
commit 99c13b8 upstream.

The pat_enabled() logic is broken on CPUs which do not support PAT and
where the initialization code fails to call pat_init(). Due to that the
enabled flag stays true and pat_enabled() returns true wrongfully.

As a consequence the mappings, e.g. for Xorg, are set up with the wrong
caching mode and the required MTRR setups are omitted.

To cure this the following changes are required:

  1) Make pat_enabled() return true only if PAT initialization was
     invoked and successful.

  2) Invoke init_cache_modes() unconditionally in setup_arch() and
     remove the extra callsites in pat_disable() and the pat disabled
     code path in pat_init().

Also rename __pat_enabled to pat_disabled to reflect the real purpose of
this variable.

Fixes: 9cd25aa ("x86/mm/pat: Emulate PAT when it is disabled")
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Bernhard Held <berny156@gmx.de>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: "Luis R. Rodriguez" <mcgrof@suse.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/alpine.LRH.2.02.1707041749300.3456@file01.intranet.prod.int.rdu2.redhat.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Mikulas Patocka authored and Greg Kroah-Hartman committed Jul 15, 2017
1 parent 20417e9 commit 2607611
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
1 change: 1 addition & 0 deletions arch/x86/include/asm/pat.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
bool pat_enabled(void);
void pat_disable(const char *reason);
extern void pat_init(void);
extern void init_cache_modes(void);

extern int reserve_memtype(u64 start, u64 end,
enum page_cache_mode req_pcm, enum page_cache_mode *ret_pcm);
Expand Down
7 changes: 7 additions & 0 deletions arch/x86/kernel/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,13 @@ void __init setup_arch(char **cmdline_p)

max_possible_pfn = max_pfn;

/*
* This call is required when the CPU does not support PAT. If
* mtrr_bp_init() invoked it already via pat_init() the call has no
* effect.
*/
init_cache_modes();

/*
* Define random base addresses for memory sections after max_pfn is
* defined and before each memory section base is used.
Expand Down
28 changes: 12 additions & 16 deletions arch/x86/mm/pat.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,23 @@
#undef pr_fmt
#define pr_fmt(fmt) "" fmt

static bool boot_cpu_done;

static int __read_mostly __pat_enabled = IS_ENABLED(CONFIG_X86_PAT);
static void init_cache_modes(void);
static bool __read_mostly boot_cpu_done;
static bool __read_mostly pat_disabled = !IS_ENABLED(CONFIG_X86_PAT);
static bool __read_mostly pat_initialized;
static bool __read_mostly init_cm_done;

void pat_disable(const char *reason)
{
if (!__pat_enabled)
if (pat_disabled)
return;

if (boot_cpu_done) {
WARN_ONCE(1, "x86/PAT: PAT cannot be disabled after initialization\n");
return;
}

__pat_enabled = 0;
pat_disabled = true;
pr_info("x86/PAT: %s\n", reason);

init_cache_modes();
}

static int __init nopat(char *str)
Expand All @@ -67,7 +65,7 @@ early_param("nopat", nopat);

bool pat_enabled(void)
{
return !!__pat_enabled;
return pat_initialized;
}
EXPORT_SYMBOL_GPL(pat_enabled);

Expand Down Expand Up @@ -205,6 +203,8 @@ static void __init_cache_modes(u64 pat)
update_cache_mode_entry(i, cache);
}
pr_info("x86/PAT: Configuration [0-7]: %s\n", pat_msg);

init_cm_done = true;
}

#define PAT(x, y) ((u64)PAT_ ## y << ((x)*8))
Expand All @@ -225,6 +225,7 @@ static void pat_bsp_init(u64 pat)
}

wrmsrl(MSR_IA32_CR_PAT, pat);
pat_initialized = true;

__init_cache_modes(pat);
}
Expand All @@ -242,10 +243,9 @@ static void pat_ap_init(u64 pat)
wrmsrl(MSR_IA32_CR_PAT, pat);
}

static void init_cache_modes(void)
void init_cache_modes(void)
{
u64 pat = 0;
static int init_cm_done;

if (init_cm_done)
return;
Expand Down Expand Up @@ -287,8 +287,6 @@ static void init_cache_modes(void)
}

__init_cache_modes(pat);

init_cm_done = 1;
}

/**
Expand All @@ -306,10 +304,8 @@ void pat_init(void)
u64 pat;
struct cpuinfo_x86 *c = &boot_cpu_data;

if (!pat_enabled()) {
init_cache_modes();
if (pat_disabled)
return;
}

if ((c->x86_vendor == X86_VENDOR_INTEL) &&
(((c->x86 == 0x6) && (c->x86_model <= 0xd)) ||
Expand Down

0 comments on commit 2607611

Please sign in to comment.