[PATCH 1/5] x86/pkeys: move read_pkru() and write_pkru()

From: Dave Hansen
Date: Thu May 27 2021 - 19:58:28 EST



From: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>

write_pkru() was originally used just to write to the PKRU register. It
was mercifully short and sweet and was not out of place in pgtable.h with
some other pkey-related code.

But, later work included a requirement to also modify the task XSAVE
buffer when updating the register. This really is more related to the
XSAVE architecture than to paging.

Move the read/write_pkru() to asm/fpu/xstate.h. pgtable.h won't miss them.

Signed-off-by: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Borislav Petkov <bp@xxxxxxxxx>
Cc: x86@xxxxxxxxxx
Cc: Andy Lutomirski <luto@xxxxxxxxxx>
Cc: Shuah Khan <shuah@xxxxxxxxxx>
Cc: Babu Moger <babu.moger@xxxxxxx>
Cc: Dave Kleikamp <dave.kleikamp@xxxxxxxxxx>
Cc: Ram Pai <linuxram@xxxxxxxxxx>
Cc: Thiago Jung Bauermann <bauerman@xxxxxxxxxxxxx>
Cc: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
---

b/arch/x86/include/asm/fpu/xstate.h | 29 +++++++++++++++++++++++++++++
b/arch/x86/include/asm/pgtable.h | 29 -----------------------------
b/arch/x86/kernel/process_64.c | 1 +
b/arch/x86/kvm/svm/sev.c | 1 +
b/arch/x86/kvm/x86.c | 1 +
b/arch/x86/mm/pkeys.c | 1 +
6 files changed, 33 insertions(+), 29 deletions(-)

diff -puN arch/x86/include/asm/fpu/xstate.h~move-write_pkru arch/x86/include/asm/fpu/xstate.h
--- a/arch/x86/include/asm/fpu/xstate.h~move-write_pkru 2021-05-27 16:40:23.110705472 -0700
+++ b/arch/x86/include/asm/fpu/xstate.h 2021-05-27 16:40:23.132705472 -0700
@@ -6,6 +6,7 @@
#include <linux/types.h>

#include <asm/processor.h>
+#include <asm/fpu/api.h>
#include <asm/user.h>

/* Bit 63 of XCR0 is reserved for future expansion */
@@ -116,4 +117,32 @@ void copy_kernel_to_dynamic_supervisor(s
/* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
int validate_user_xstate_header(const struct xstate_header *hdr);

+static inline u32 read_pkru(void)
+{
+ if (boot_cpu_has(X86_FEATURE_OSPKE))
+ return rdpkru();
+ return 0;
+}
+
+static inline void write_pkru(u32 pkru)
+{
+ struct pkru_state *pk;
+
+ if (!boot_cpu_has(X86_FEATURE_OSPKE))
+ return;
+
+ pk = get_xsave_addr(&current->thread.fpu.state.xsave, XFEATURE_PKRU);
+
+ /*
+ * The PKRU value in xstate needs to be in sync with the value that is
+ * written to the CPU. The FPU restore on return to userland would
+ * otherwise load the previous value again.
+ */
+ fpregs_lock();
+ if (pk)
+ pk->pkru = pkru;
+ __write_pkru(pkru);
+ fpregs_unlock();
+}
+
#endif
diff -puN arch/x86/include/asm/pgtable.h~move-write_pkru arch/x86/include/asm/pgtable.h
--- a/arch/x86/include/asm/pgtable.h~move-write_pkru 2021-05-27 16:40:23.114705472 -0700
+++ b/arch/x86/include/asm/pgtable.h 2021-05-27 16:40:23.135705472 -0700
@@ -126,35 +126,6 @@ static inline int pte_dirty(pte_t pte)
return pte_flags(pte) & _PAGE_DIRTY;
}

-
-static inline u32 read_pkru(void)
-{
- if (boot_cpu_has(X86_FEATURE_OSPKE))
- return rdpkru();
- return 0;
-}
-
-static inline void write_pkru(u32 pkru)
-{
- struct pkru_state *pk;
-
- if (!boot_cpu_has(X86_FEATURE_OSPKE))
- return;
-
- pk = get_xsave_addr(&current->thread.fpu.state.xsave, XFEATURE_PKRU);
-
- /*
- * The PKRU value in xstate needs to be in sync with the value that is
- * written to the CPU. The FPU restore on return to userland would
- * otherwise load the previous value again.
- */
- fpregs_lock();
- if (pk)
- pk->pkru = pkru;
- __write_pkru(pkru);
- fpregs_unlock();
-}
-
static inline int pte_young(pte_t pte)
{
return pte_flags(pte) & _PAGE_ACCESSED;
diff -puN arch/x86/kernel/process_64.c~move-write_pkru arch/x86/kernel/process_64.c
--- a/arch/x86/kernel/process_64.c~move-write_pkru 2021-05-27 16:40:23.117705472 -0700
+++ b/arch/x86/kernel/process_64.c 2021-05-27 16:40:23.138705472 -0700
@@ -41,6 +41,7 @@
#include <linux/syscalls.h>

#include <asm/processor.h>
+#include <asm/fpu/xstate.h>
#include <asm/fpu/internal.h>
#include <asm/mmu_context.h>
#include <asm/prctl.h>
diff -puN arch/x86/kvm/svm/sev.c~move-write_pkru arch/x86/kvm/svm/sev.c
--- a/arch/x86/kvm/svm/sev.c~move-write_pkru 2021-05-27 16:40:23.121705472 -0700
+++ b/arch/x86/kvm/svm/sev.c 2021-05-27 16:40:23.142705472 -0700
@@ -16,6 +16,7 @@
#include <linux/swap.h>
#include <linux/processor.h>
#include <linux/trace_events.h>
+#include <asm/fpu/xstate.h>
#include <asm/fpu/internal.h>

#include <asm/trapnr.h>
diff -puN arch/x86/kvm/x86.c~move-write_pkru arch/x86/kvm/x86.c
--- a/arch/x86/kvm/x86.c~move-write_pkru 2021-05-27 16:40:23.125705472 -0700
+++ b/arch/x86/kvm/x86.c 2021-05-27 16:40:23.150705472 -0700
@@ -66,6 +66,7 @@
#include <asm/desc.h>
#include <asm/mce.h>
#include <linux/kernel_stat.h>
+#include <asm/fpu/xstate.h>
#include <asm/fpu/internal.h> /* Ugh! */
#include <asm/pvclock.h>
#include <asm/div64.h>
diff -puN arch/x86/mm/pkeys.c~move-write_pkru arch/x86/mm/pkeys.c
--- a/arch/x86/mm/pkeys.c~move-write_pkru 2021-05-27 16:40:23.128705472 -0700
+++ b/arch/x86/mm/pkeys.c 2021-05-27 16:40:23.151705472 -0700
@@ -10,6 +10,7 @@

#include <asm/cpufeature.h> /* boot_cpu_has, ... */
#include <asm/mmu_context.h> /* vma_pkey() */
+#include <asm/fpu/xstate.h> /* read/write_pkru() */
#include <asm/fpu/internal.h> /* init_fpstate */

int __execute_only_pkey(struct mm_struct *mm)
_