Skip to content

x86/pkeys: PKRU manipulation bug fixes and cleanups

Dave Hansen linux-mm lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest5 patches 10 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/5x86/pkeys: move read_pkru() and write_pkru()arch/x86/include/asm/fpu/xstate.h arch/x86/include/asm/pgtable.h arch/x86/kernel/process_64.c +3 more+33 -290 replies
2/5x86/pkeys: rename write_pkru()arch/x86/include/asm/fpu/xstate.h arch/x86/kernel/fpu/xstate.c arch/x86/mm/pkeys.c+7 -30 replies
3/5x86/pkeys: skip 'init_pkru' debugfs file creation when pkeys not supportedarch/x86/mm/pkeys.c+4 -00 replies
4/5x86/pkeys: replace PKRU modification infrastructurearch/x86/include/asm/fpu/internal.h arch/x86/include/asm/fpu/xstate.h arch/x86/include/asm/processor.h +2 more+115 -235 replies
5/5selftests/vm/pkeys: exercise x86 XSAVE init statetools/testing/selftests/vm/Makefile tools/testing/selftests/vm/pkey-x86.h tools/testing/selftests/vm/protection_keys.c+74 -20 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/5] x86/pkeys: PKRU manipulation bug fixes and cleanups

Andy Lutomirski recently noted a probable bug in write_pkru(), but it was unclear if it was user-visible. A recent bug report in related code[1] forced me to take a look.

Basically, manipulation of XSAVE state is too unstructured. get_xsave_addr() gives callers the impression they can read and write XSAVE state when there are a lot of pitfalls, like updates to xstate.features bits.

As a result, more than one call site screws up the modification of PKRU in the XSAVE buffer. This series fixes that problem up and also hopefully carves out a less error-prone path that can be reused for other XSAVE features.

This series:
* Moves the PKRU manipulation to a more appropriate location, away from the page table code
* Wraps get_xsave_addr() with more structured, less error-prone interfaces.
* Conditionally hides a pkey debugfs file, eliminating the need for new runtime checks to work with the new interface.
* Add a selftest to make it more likely to catch bugs like this in the future. This improved selftest catches this issue on Intel CPUs. Without the improvement, it only triggers on AMD.

This has been lightly tested so far. It probably needs a tested-by from at least the AMD folks before anyone applies it.

1. https://lore.kernel.org/linux-kselftest/[email protected]/

--

 arch/x86/include/asm/fpu/internal.h          |    8 -
 arch/x86/include/asm/fpu/xstate.h            |  130 +++++++++++++++++++++++++++
 arch/x86/include/asm/pgtable.h               |   29 ------
 arch/x86/include/asm/processor.h             |    7 +
 arch/x86/kernel/cpu/common.c                 |    6 -
 arch/x86/kernel/fpu/xstate.c                 |    2 
 arch/x86/kernel/process_64.c                 |    1 
 arch/x86/kvm/svm/sev.c                       |    1 
 arch/x86/kvm/x86.c                           |    1 
 arch/x86/mm/pkeys.c                          |   13 +-
 tools/testing/selftests/vm/Makefile          |    4 
 tools/testing/selftests/vm/pkey-x86.h        |    1 
 tools/testing/selftests/vm/protection_keys.c |   73 +++++++++++++++
 13 files changed, 227 insertions(+), 49 deletions(-)
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Babu Moger <[email protected]>
Cc Dave Kleikamp <[email protected]>
Cc Ram Pai <[email protected]>
Cc Thiago Jung Bauermann <[email protected]>