Skip to content

Commit 0697694

Browse files
hansendcIngo Molnar
authored andcommitted
x86/mm/pkeys: Actually enable Memory Protection Keys in the CPU
This sets the bit in 'cr4' to actually enable the protection keys feature. We also include a boot-time disable for the feature "nopku". Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE cpuid bit to appear set. At this point in boot, identify_cpu() has already run the actual CPUID instructions and populated the "cpu features" structures. We need to go back and re-run identify_cpu() to make sure it gets updated values. We *could* simply re-populate the 11th word of the cpuid data, but this is probably quick enough. Also note that with the cpu_has() check and X86_FEATURE_PKU present in disabled-features.h, we do not need an #ifdef for setup_pku(). Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Andy Lutomirski <luto@amacapital.net> Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: Dave Hansen <dave@sr71.net> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Rik van Riel <riel@redhat.com> Cc: linux-mm@kvack.org Link: http://lkml.kernel.org/r/20160212210229.6708027C@viggo.jf.intel.com [ Small readability edits. ] Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 284244a commit 0697694

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Documentation/kernel-parameters.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,9 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
976976
See Documentation/x86/intel_mpx.txt for more
977977
information about the feature.
978978

979+
nopku [X86] Disable Memory Protection Keys CPU feature found
980+
in some Intel CPUs.
981+
979982
eagerfpu= [X86]
980983
on enable eager fpu restore
981984
off disable eager fpu restore

arch/x86/kernel/cpu/common.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,48 @@ static __always_inline void setup_smap(struct cpuinfo_x86 *c)
303303
}
304304
}
305305

306+
/*
307+
* Protection Keys are not available in 32-bit mode.
308+
*/
309+
static bool pku_disabled;
310+
311+
static __always_inline void setup_pku(struct cpuinfo_x86 *c)
312+
{
313+
if (!cpu_has(c, X86_FEATURE_PKU))
314+
return;
315+
if (pku_disabled)
316+
return;
317+
318+
cr4_set_bits(X86_CR4_PKE);
319+
/*
320+
* Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE
321+
* cpuid bit to be set. We need to ensure that we
322+
* update that bit in this CPU's "cpu_info".
323+
*/
324+
get_cpu_cap(c);
325+
}
326+
327+
#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
328+
static __init int setup_disable_pku(char *arg)
329+
{
330+
/*
331+
* Do not clear the X86_FEATURE_PKU bit. All of the
332+
* runtime checks are against OSPKE so clearing the
333+
* bit does nothing.
334+
*
335+
* This way, we will see "pku" in cpuinfo, but not
336+
* "ospke", which is exactly what we want. It shows
337+
* that the CPU has PKU, but the OS has not enabled it.
338+
* This happens to be exactly how a system would look
339+
* if we disabled the config option.
340+
*/
341+
pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
342+
pku_disabled = true;
343+
return 1;
344+
}
345+
__setup("nopku", setup_disable_pku);
346+
#endif /* CONFIG_X86_64 */
347+
306348
/*
307349
* Some CPU features depend on higher CPUID levels, which may not always
308350
* be available due to CPUID level capping or broken virtualization
@@ -960,6 +1002,7 @@ static void identify_cpu(struct cpuinfo_x86 *c)
9601002
init_hypervisor(c);
9611003
x86_init_rdrand(c);
9621004
x86_init_cache_qos(c);
1005+
setup_pku(c);
9631006

9641007
/*
9651008
* Clear/Set all flags overriden by options, need do it

0 commit comments

Comments
 (0)