Skip to content

Commit

Permalink
selftests/vm/pkeys: exercise x86 XSAVE init state
Browse files Browse the repository at this point in the history
On x86, there is a set of instructions used to save and restore register
state collectively known as the XSAVE architecture.  There are about a
dozen different features managed with XSAVE.  The protection keys
register, PKRU, is one of those features.

The hardware optimizes XSAVE by tracking when the state has not changed
from its initial (init) state.  In this case, it can avoid the cost of
writing state to memory (it would usually just be a bunch of 0's).

When the pkey register is 0x0 the hardware optionally choose to track the
register as being in the init state (optimize away the writes).  AMD CPUs
do this more aggressively compared to Intel.

On x86, PKRU is rarely in its (very permissive) init state.  Instead, the
value defaults to something very restrictive.  It is not surprising that
bugs have popped up in the rare cases when PKRU reaches its init state.

Add a protection key selftest which gets the protection keys register into
its init state in a way that should work on Intel and AMD.  Then, do a
bunch of pkey register reads to watch for inadvertent changes.

This adds "-mxsave" to CFLAGS for all the x86 vm selftests in order to
allow use of the XSAVE instruction __builtin functions.  This will make
the builtins available on all of the vm selftests, but is expected to be
harmless.

Link: https://lkml.kernel.org/r/20210611164202.1849B712@viggo.jf.intel.com
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
Cc: Ram Pai <linuxram@us.ibm.com>
Cc: Sandipan Das <sandipan@linux.ibm.com>
Cc: Florian Weimer <fweimer@redhat.com>
Cc: "Desnes A. Nunes do Rosario" <desnesn@linux.vnet.ibm.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Thiago Jung Bauermann <bauerman@linux.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Michal Suchanek <msuchanek@suse.de>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
Dave Hansen authored and Linus Torvalds committed Jul 1, 2021
1 parent 6039ca2 commit d892454
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tools/testing/selftests/vm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ $(1) $(1)_64: $(OUTPUT)/$(1)_64
endef

ifeq ($(CAN_BUILD_I386),1)
$(BINARIES_32): CFLAGS += -m32
$(BINARIES_32): CFLAGS += -m32 -mxsave
$(BINARIES_32): LDLIBS += -lrt -ldl -lm
$(BINARIES_32): $(OUTPUT)/%_32: %.c
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(notdir $^) $(LDLIBS) -o $@
$(foreach t,$(TARGETS),$(eval $(call gen-target-rule-32,$(t))))
endif

ifeq ($(CAN_BUILD_X86_64),1)
$(BINARIES_64): CFLAGS += -m64
$(BINARIES_64): CFLAGS += -m64 -mxsave
$(BINARIES_64): LDLIBS += -lrt -ldl
$(BINARIES_64): $(OUTPUT)/%_64: %.c
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(notdir $^) $(LDLIBS) -o $@
Expand Down
1 change: 1 addition & 0 deletions tools/testing/selftests/vm/pkey-x86.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ static inline u32 pkey_bit_position(int pkey)

#define XSTATE_PKEY_BIT (9)
#define XSTATE_PKEY 0x200
#define XSTATE_BV_OFFSET 512

int pkey_reg_xstate_offset(void)
{
Expand Down
73 changes: 73 additions & 0 deletions tools/testing/selftests/vm/protection_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,78 @@ void test_pkey_alloc_exhaust(int *ptr, u16 pkey)
}
}

void arch_force_pkey_reg_init(void)
{
#if defined(__i386__) || defined(__x86_64__) /* arch */
u64 *buf;

/*
* All keys should be allocated and set to allow reads and
* writes, so the register should be all 0. If not, just
* skip the test.
*/
if (read_pkey_reg())
return;

/*
* Just allocate an absurd about of memory rather than
* doing the XSAVE size enumeration dance.
*/
buf = mmap(NULL, 1*MB, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);

/* These __builtins require compiling with -mxsave */

/* XSAVE to build a valid buffer: */
__builtin_ia32_xsave(buf, XSTATE_PKEY);
/* Clear XSTATE_BV[PKRU]: */
buf[XSTATE_BV_OFFSET/sizeof(u64)] &= ~XSTATE_PKEY;
/* XRSTOR will likely get PKRU back to the init state: */
__builtin_ia32_xrstor(buf, XSTATE_PKEY);

munmap(buf, 1*MB);
#endif
}


/*
* This is mostly useless on ppc for now. But it will not
* hurt anything and should give some better coverage as
* a long-running test that continually checks the pkey
* register.
*/
void test_pkey_init_state(int *ptr, u16 pkey)
{
int err;
int allocated_pkeys[NR_PKEYS] = {0};
int nr_allocated_pkeys = 0;
int i;

for (i = 0; i < NR_PKEYS; i++) {
int new_pkey = alloc_pkey();

if (new_pkey < 0)
continue;
allocated_pkeys[nr_allocated_pkeys++] = new_pkey;
}

dprintf3("%s()::%d\n", __func__, __LINE__);

arch_force_pkey_reg_init();

/*
* Loop for a bit, hoping to get exercise the kernel
* context switch code.
*/
for (i = 0; i < 1000000; i++)
read_pkey_reg();

for (i = 0; i < nr_allocated_pkeys; i++) {
err = sys_pkey_free(allocated_pkeys[i]);
pkey_assert(!err);
read_pkey_reg(); /* for shadow checking */
}
}

/*
* pkey 0 is special. It is allocated by default, so you do not
* have to call pkey_alloc() to use it first. Make sure that it
Expand Down Expand Up @@ -1508,6 +1580,7 @@ void (*pkey_tests[])(int *ptr, u16 pkey) = {
test_implicit_mprotect_exec_only_memory,
test_mprotect_with_pkey_0,
test_ptrace_of_child,
test_pkey_init_state,
test_pkey_syscalls_on_non_allocated_pkey,
test_pkey_syscalls_bad_args,
test_pkey_alloc_exhaust,
Expand Down

0 comments on commit d892454

Please sign in to comment.