All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dave Hansen <dave.hansen@linux.intel.com>
To: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org,
	Dave Hansen <dave.hansen@linux.intel.com>,
	tglx@linutronix.de, mingo@redhat.com, bp@alien8.de,
	x86@kernel.org, luto@kernel.org
Subject: [RFC][PATCH 1/8] x86/pkeys: add PKRU storage outside of task XSAVE buffer
Date: Tue, 22 Jun 2021 15:24:57 -0700	[thread overview]
Message-ID: <20210622222457.C16E9CB5@viggo.jf.intel.com> (raw)
In-Reply-To: <20210622222455.E901B5AC@viggo.jf.intel.com>


From: Dave Hansen <dave.hansen@linux.intel.com>

PKRU has space in the task XSAVE buffer, but is not context-switched by
XSAVE/XRSTOR.  It is switched more eagerly than other FPU state because
PKRU affects things like copy_to/from_user().  This is because PKRU
affects user *PERMISSION* accesses, not just accesses made from user
*MODE* itself.

Prepare to move PKRU away from being XSAVE-managed.  Allocate space in
the thread_struct for it and save/restore it in the context-switch path
separately from the XSAVE-managed features.

Leave the XSAVE storage in place for now to ensure bisectability.

Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: x86@kernel.org
Cc: Andy Lutomirski <luto@kernel.org>
---

 b/arch/x86/include/asm/pkru.h  |    5 +++++
 b/arch/x86/kernel/cpu/common.c |    3 +++
 b/arch/x86/kernel/process_64.c |    9 ++++-----
 b/arch/x86/mm/pkeys.c          |    2 ++
 4 files changed, 14 insertions(+), 5 deletions(-)

diff -puN arch/x86/include/asm/pkru.h~pkru-stash-thread-value arch/x86/include/asm/pkru.h
--- a/arch/x86/include/asm/pkru.h~pkru-stash-thread-value	2021-06-22 14:49:06.594051763 -0700
+++ b/arch/x86/include/asm/pkru.h	2021-06-22 14:49:06.607051763 -0700
@@ -44,11 +44,16 @@ static inline void write_pkru(u32 pkru)
 	if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
 		return;
 	/*
+	 * Update the actual register.
+	 *
 	 * WRPKRU is relatively expensive compared to RDPKRU.
 	 * Avoid WRPKRU when it would not change the value.
 	 */
 	if (pkru != rdpkru())
 		wrpkru(pkru);
+
+	/* Update the thread-local, context-switched value: */
+	current->thread.pkru = pkru;
 }
 
 static inline void pkru_write_default(void)
diff -puN arch/x86/kernel/cpu/common.c~pkru-stash-thread-value arch/x86/kernel/cpu/common.c
--- a/arch/x86/kernel/cpu/common.c~pkru-stash-thread-value	2021-06-22 14:49:06.596051763 -0700
+++ b/arch/x86/kernel/cpu/common.c	2021-06-22 14:49:06.608051763 -0700
@@ -482,6 +482,9 @@ static __always_inline void setup_pku(st
 	cr4_set_bits(X86_CR4_PKE);
 	/* Load the default PKRU value */
 	pkru_write_default();
+
+	/* Establish the default value for future tasks: */
+	init_task.thread.pkru = init_pkru_value;
 }
 
 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
diff -puN arch/x86/kernel/process_64.c~pkru-stash-thread-value arch/x86/kernel/process_64.c
--- a/arch/x86/kernel/process_64.c~pkru-stash-thread-value	2021-06-22 14:49:06.599051763 -0700
+++ b/arch/x86/kernel/process_64.c	2021-06-22 14:49:06.608051763 -0700
@@ -349,15 +349,14 @@ static __always_inline void load_seg_leg
 static __always_inline void x86_pkru_load(struct thread_struct *prev,
 					  struct thread_struct *next)
 {
-	if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
-		return;
+	u32 pkru = read_pkru();
 
 	/* Stash the prev task's value: */
-	prev->pkru = rdpkru();
+	prev->pkru = pkru;
 
 	/*
-	 * PKRU writes are slightly expensive.  Avoid them when not
-	 * strictly necessary:
+	 * PKRU writes are slightly expensive.  Avoid
+	 * them when not strictly necessary:
 	 */
 	if (prev->pkru != next->pkru)
 		wrpkru(next->pkru);
diff -puN arch/x86/mm/pkeys.c~pkru-stash-thread-value arch/x86/mm/pkeys.c
--- a/arch/x86/mm/pkeys.c~pkru-stash-thread-value	2021-06-22 14:49:06.604051763 -0700
+++ b/arch/x86/mm/pkeys.c	2021-06-22 14:49:06.609051763 -0700
@@ -159,6 +159,8 @@ static ssize_t init_pkru_write_file(stru
 		return -EINVAL;
 
 	WRITE_ONCE(init_pkru_value, new_init_pkru);
+	WRITE_ONCE(init_task.thread.pkru, new_init_pkru);
+
 	return count;
 }
 
_

WARNING: multiple messages have this Message-ID (diff)
From: Dave Hansen <dave.hansen@linux.intel.com>
To: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org,Dave Hansen
	<dave.hansen@linux.intel.com>,tglx@linutronix.de,mingo@redhat.com,bp@alien8.de,x86@kernel.org,luto@kernel.org
Subject: [RFC][PATCH 1/8] x86/pkeys: add PKRU storage outside of task XSAVE buffer
Date: Tue, 22 Jun 2021 15:24:57 -0700	[thread overview]
Message-ID: <20210622222457.C16E9CB5@viggo.jf.intel.com> (raw)
In-Reply-To: <20210622222455.E901B5AC@viggo.jf.intel.com>


From: Dave Hansen <dave.hansen@linux.intel.com>

PKRU has space in the task XSAVE buffer, but is not context-switched by
XSAVE/XRSTOR.  It is switched more eagerly than other FPU state because
PKRU affects things like copy_to/from_user().  This is because PKRU
affects user *PERMISSION* accesses, not just accesses made from user
*MODE* itself.

Prepare to move PKRU away from being XSAVE-managed.  Allocate space in
the thread_struct for it and save/restore it in the context-switch path
separately from the XSAVE-managed features.

Leave the XSAVE storage in place for now to ensure bisectability.

Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: x86@kernel.org
Cc: Andy Lutomirski <luto@kernel.org>
---

 b/arch/x86/include/asm/pkru.h  |    5 +++++
 b/arch/x86/kernel/cpu/common.c |    3 +++
 b/arch/x86/kernel/process_64.c |    9 ++++-----
 b/arch/x86/mm/pkeys.c          |    2 ++
 4 files changed, 14 insertions(+), 5 deletions(-)

diff -puN arch/x86/include/asm/pkru.h~pkru-stash-thread-value arch/x86/include/asm/pkru.h
--- a/arch/x86/include/asm/pkru.h~pkru-stash-thread-value	2021-06-22 14:49:06.594051763 -0700
+++ b/arch/x86/include/asm/pkru.h	2021-06-22 14:49:06.607051763 -0700
@@ -44,11 +44,16 @@ static inline void write_pkru(u32 pkru)
 	if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
 		return;
 	/*
+	 * Update the actual register.
+	 *
 	 * WRPKRU is relatively expensive compared to RDPKRU.
 	 * Avoid WRPKRU when it would not change the value.
 	 */
 	if (pkru != rdpkru())
 		wrpkru(pkru);
+
+	/* Update the thread-local, context-switched value: */
+	current->thread.pkru = pkru;
 }
 
 static inline void pkru_write_default(void)
diff -puN arch/x86/kernel/cpu/common.c~pkru-stash-thread-value arch/x86/kernel/cpu/common.c
--- a/arch/x86/kernel/cpu/common.c~pkru-stash-thread-value	2021-06-22 14:49:06.596051763 -0700
+++ b/arch/x86/kernel/cpu/common.c	2021-06-22 14:49:06.608051763 -0700
@@ -482,6 +482,9 @@ static __always_inline void setup_pku(st
 	cr4_set_bits(X86_CR4_PKE);
 	/* Load the default PKRU value */
 	pkru_write_default();
+
+	/* Establish the default value for future tasks: */
+	init_task.thread.pkru = init_pkru_value;
 }
 
 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
diff -puN arch/x86/kernel/process_64.c~pkru-stash-thread-value arch/x86/kernel/process_64.c
--- a/arch/x86/kernel/process_64.c~pkru-stash-thread-value	2021-06-22 14:49:06.599051763 -0700
+++ b/arch/x86/kernel/process_64.c	2021-06-22 14:49:06.608051763 -0700
@@ -349,15 +349,14 @@ static __always_inline void load_seg_leg
 static __always_inline void x86_pkru_load(struct thread_struct *prev,
 					  struct thread_struct *next)
 {
-	if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
-		return;
+	u32 pkru = read_pkru();
 
 	/* Stash the prev task's value: */
-	prev->pkru = rdpkru();
+	prev->pkru = pkru;
 
 	/*
-	 * PKRU writes are slightly expensive.  Avoid them when not
-	 * strictly necessary:
+	 * PKRU writes are slightly expensive.  Avoid
+	 * them when not strictly necessary:
 	 */
 	if (prev->pkru != next->pkru)
 		wrpkru(next->pkru);
diff -puN arch/x86/mm/pkeys.c~pkru-stash-thread-value arch/x86/mm/pkeys.c
--- a/arch/x86/mm/pkeys.c~pkru-stash-thread-value	2021-06-22 14:49:06.604051763 -0700
+++ b/arch/x86/mm/pkeys.c	2021-06-22 14:49:06.609051763 -0700
@@ -159,6 +159,8 @@ static ssize_t init_pkru_write_file(stru
 		return -EINVAL;
 
 	WRITE_ONCE(init_pkru_value, new_init_pkru);
+	WRITE_ONCE(init_task.thread.pkru, new_init_pkru);
+
 	return count;
 }
 
_


  reply	other threads:[~2021-06-22 22:25 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-22 22:24 [RFC][PATCH 0/8] x86/pkeys: remove PKRU from kernel XSAVE buffer Dave Hansen
2021-06-22 22:24 ` Dave Hansen
2021-06-22 22:24 ` Dave Hansen [this message]
2021-06-22 22:24   ` [RFC][PATCH 1/8] x86/pkeys: add PKRU storage outside of task " Dave Hansen
2021-06-22 22:24 ` [RFC][PATCH 2/8] x86/fpu: hook up PKRU into signal user ABIs Dave Hansen
2021-06-22 22:24   ` Dave Hansen
2021-06-22 22:25 ` [RFC][PATCH 3/8] x86/fpu: separate the setup of xfeatures not in fpstate Dave Hansen
2021-06-22 22:25   ` Dave Hansen
2021-06-22 22:25 ` [RFC][PATCH 4/8] x86/fpu: remove PKRU from FPU user state clearing Dave Hansen
2021-06-22 22:25   ` Dave Hansen
2021-06-22 22:25 ` [RFC][PATCH 5/8] x86/fpu: XSAVE buffer access routine rename Dave Hansen
2021-06-22 22:25   ` Dave Hansen
2021-06-22 22:25 ` [RFC][PATCH 6/8] x86/fpu: update xstate size calculations for non-XSAVE-managed features Dave Hansen
2021-06-22 22:25   ` Dave Hansen
2021-06-22 22:25 ` [RFC][PATCH 7/8] x86/fpu: actually stop using XSAVE on PKRU Dave Hansen
2021-06-22 22:25   ` Dave Hansen
2021-06-22 22:25 ` [RFC][PATCH 8/8] x86/pkeys: remove init_pkru_value variable Dave Hansen
2021-06-22 22:25   ` Dave Hansen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210622222457.C16E9CB5@viggo.jf.intel.com \
    --to=dave.hansen@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=luto@kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.