Skip to contentSkip to conversation

x86/fpu: Clean up "dynamic" APIs

PATCH Applied Andy Lutomirski 4 participants 6 messages Jun 11, 2021 – Jun 23, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover x86/fpu: Clean up "dynamic" APIs

"Dynamic" XSTATEs are not dynamic. They are just managed separately from the normal struct fpu states. Rename the functions and make their interfaces sane.

Andy Lutomirski (2):
x86/fpu: Rename "dynamic" XSTATEs to "independent" x86/fpu: Improve FPU APIs for independent features

 arch/x86/events/intel/lbr.c       |  6 +--
 arch/x86/include/asm/fpu/xstate.h | 19 +++++----
 arch/x86/kernel/fpu/core.c        |  3 ++
 arch/x86/kernel/fpu/xstate.c      | 67 +++++++++++++------------------
 4 files changed, 46 insertions(+), 49 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/2 x86/fpu: Rename "dynamic" XSTATEs to "independent"

The salient feature of "dynamic" XSTATEs is that they are not part of the main task XSTATE buffer. The fact that they are dynamically allocated is irrelevant and will become quite confusing when user math XSTATEs start being dynamically allocated. Rename them to "independent" because they are independent of the main XSTATE code.

This is just a search-and-replace with some whitespace updates to keep things aligned.

---
 arch/x86/events/intel/lbr.c       |  6 +--
 arch/x86/include/asm/fpu/xstate.h | 14 +++----
 arch/x86/kernel/fpu/xstate.c      | 62 +++++++++++++++----------------
 3 files changed, 41 insertions(+), 41 deletions(-)
arch/x86/events/intel/lbr.c +3 -3
index 76dbab6ac9fb..0189807fc3c1 100644
@@ -491,7 +491,7 @@ static void intel_pmu_arch_lbr_xrstors(void *ctx)
 {
 	struct x86_perf_task_context_arch_lbr_xsave *task_ctx = ctx;
 
-	copy_kernel_to_dynamic_supervisor(&task_ctx->xsave, XFEATURE_MASK_LBR);
+	copy_kernel_to_independent_supervisor(&task_ctx->xsave, XFEATURE_MASK_LBR);
 }
 
 static __always_inline bool lbr_is_reset_in_cstate(void *ctx)
@@ -576,7 +576,7 @@ static void intel_pmu_arch_lbr_xsaves(void *ctx)
 {
 	struct x86_perf_task_context_arch_lbr_xsave *task_ctx = ctx;
 
-	copy_dynamic_supervisor_to_kernel(&task_ctx->xsave, XFEATURE_MASK_LBR);
+	copy_independent_supervisor_to_kernel(&task_ctx->xsave, XFEATURE_MASK_LBR);
 }
 
 static void __intel_pmu_lbr_save(void *ctx)
@@ -978,7 +978,7 @@ static void intel_pmu_arch_lbr_read_xsave(struct cpu_hw_events *cpuc)
 		intel_pmu_store_lbr(cpuc, NULL);
 		return;
 	}
-	copy_dynamic_supervisor_to_kernel(&xsave->xsave, XFEATURE_MASK_LBR);
+	copy_independent_supervisor_to_kernel(&xsave->xsave, XFEATURE_MASK_LBR);
 
 	intel_pmu_store_lbr(cpuc, xsave->lbr.entries);
 }
arch/x86/include/asm/fpu/xstate.h +7 -7
index 47a92232d595..5802b1715c7f 100644
@@ -56,7 +56,7 @@
  * - Don't set the bit corresponding to the dynamic supervisor feature in
  *   IA32_XSS at run time, since it has been set at boot time.
  */
-#define XFEATURE_MASK_DYNAMIC (XFEATURE_MASK_LBR)
+#define XFEATURE_MASK_INDEPENDENT (XFEATURE_MASK_LBR)
 
 /*
  * Unsupported supervisor features. When a supervisor feature in this mask is
@@ -66,7 +66,7 @@
 
 /* All supervisor states including supported and unsupported states. */
 #define XFEATURE_MASK_SUPERVISOR_ALL (XFEATURE_MASK_SUPERVISOR_SUPPORTED | \
-				      XFEATURE_MASK_DYNAMIC | \
+				      XFEATURE_MASK_INDEPENDENT | \
 				      XFEATURE_MASK_SUPERVISOR_UNSUPPORTED)
 
 #ifdef CONFIG_X86_64
@@ -87,12 +87,12 @@ static inline u64 xfeatures_mask_user(void)
 	return xfeatures_mask_all & XFEATURE_MASK_USER_SUPPORTED;
 }
 
-static inline u64 xfeatures_mask_dynamic(void)
+static inline u64 xfeatures_mask_independent(void)
 {
 	if (!boot_cpu_has(X86_FEATURE_ARCH_LBR))
-		return XFEATURE_MASK_DYNAMIC & ~XFEATURE_MASK_LBR;
+		return XFEATURE_MASK_INDEPENDENT & ~XFEATURE_MASK_LBR;
 
-	return XFEATURE_MASK_DYNAMIC;
+	return XFEATURE_MASK_INDEPENDENT;
 }
 
 extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
@@ -109,8 +109,8 @@ void copy_xstate_to_kernel(struct membuf to, struct xregs_state *xsave);
 int copy_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf);
 int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf);
 void copy_supervisor_to_kernel(struct xregs_state *xsave);
-void copy_dynamic_supervisor_to_kernel(struct xregs_state *xstate, u64 mask);
-void copy_kernel_to_dynamic_supervisor(struct xregs_state *xstate, u64 mask);
+void copy_independent_supervisor_to_kernel(struct xregs_state *xstate, u64 mask);
+void copy_kernel_to_independent_supervisor(struct xregs_state *xstate, u64 mask);
 
 
 /* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
arch/x86/kernel/fpu/xstate.c +31 -31

152 lines not on this page · raw message

Signature · 1 line
2.31.1
#5 lore raw

PATCH 2/2 x86/fpu: Improve FPU APIs for independent features

In reply to Andy Lutomirski

Having the functions that save and restore independent features accept, but ignore, non-independent features is confusing, so instead require that only independent features be independently saved and restored.

Remove a bunch of nonsense comments from the save and restore functions:
saving and restoring the XSAVE header makes no sense.

Document that fpu__clear_all() does not clear independent features.

---
 arch/x86/include/asm/fpu/xstate.h |  5 ++++-
 arch/x86/kernel/fpu/core.c        |  3 +++
 arch/x86/kernel/fpu/xstate.c      | 33 +++++++++++--------------------
 3 files changed, 19 insertions(+), 22 deletions(-)
arch/x86/include/asm/fpu/xstate.h +4 -1
index 5802b1715c7f..3bc5e6c9e47a 100644
@@ -34,7 +34,10 @@
 				      XFEATURE_MASK_BNDREGS | \
 				      XFEATURE_MASK_BNDCSR)
 
-/* All currently supported supervisor features */
+/*
+ * All currently supported supervisor features that are saved and
+ * restored as part of the main task XSAVE buffers.
+ */
 #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID)
 
 /*
arch/x86/kernel/fpu/core.c +3 -0
index 571220ac8bea..9af361464c66 100644
@@ -390,6 +390,9 @@ void fpu__clear_user_states(struct fpu *fpu)
 	fpu__clear(fpu, true);
 }
 
+/*
+ * This does not affect independent features -- see XFEATURE_MASK_INDEPENDENT.
+ */
 void fpu__clear_all(struct fpu *fpu)
 {
 	fpu__clear(fpu, false);
arch/x86/kernel/fpu/xstate.c +12 -21
index d582275164ad..15bb87f4f510 100644
@@ -1292,29 +1292,26 @@ void copy_supervisor_to_kernel(struct xregs_state *xstate)
  * @xstate: A pointer to an xsave area
  * @mask: Represent the independent supervisor features saved into the xsave area
  *
- * Only the independent supervisor states sets in the mask are saved into the xsave
- * area (See the comment in XFEATURE_MASK_INDEPENDENT for the details of independent
- * supervisor feature). Besides the independent supervisor states, the legacy
- * region and XSAVE header are also saved into the xsave area. The supervisor
- * features in the XFEATURE_MASK_SUPERVISOR_SUPPORTED and
- * XFEATURE_MASK_SUPERVISOR_UNSUPPORTED are not saved.
+ * The XSAVE header in the target buffer must already be initialized, as the
+ * XSAVES instruction may not fully initialize the XSAVE header.
  *
  * The xsave area must be 64-bytes aligned.
  */
 void copy_independent_supervisor_to_kernel(struct xregs_state *xstate, u64 mask)
 {
-	u64 independent_mask = xfeatures_mask_independent() & mask;
 	u32 lmask, hmask;
 	int err;
 
+	WARN_ON_FPU(mask & ~xfeatures_mask_independent());
+
 	if (WARN_ON_FPU(!boot_cpu_has(X86_FEATURE_XSAVES)))
 		return;
 
-	if (WARN_ON_FPU(!independent_mask))
+	if (WARN_ON_FPU(!mask))
 		return;
 
-	lmask = independent_mask;
-	hmask = independent_mask >> 32;
+	lmask = mask;
+	hmask = mask >> 32;
 
 	XSTATE_OP(XSAVES, xstate, lmask, hmask, err);
 
@@ -1328,29 +1325,23 @@ void copy_independent_supervisor_to_kernel(struct xregs_state *xstate, u64 mask)
  * @xstate: A pointer to an xsave area
  * @mask: Represent the independent supervisor features restored from the xsave area
  *
- * Only the independent supervisor states sets in the mask are restored from the
- * xsave area (See the comment in XFEATURE_MASK_INDEPENDENT for the details of
- * independent supervisor feature). Besides the independent supervisor states, the
- * legacy region and XSAVE header are also restored from the xsave area. The
- * supervisor features in the XFEATURE_MASK_SUPERVISOR_SUPPORTED and
- * XFEATURE_MASK_SUPERVISOR_UNSUPPORTED are not restored.
- *
  * The xsave area must be 64-bytes aligned.
  */
 void copy_kernel_to_independent_supervisor(struct xregs_state *xstate, u64 mask)
 {
-	u64 independent_mask = xfeatures_mask_independent() & mask;
 	u32 lmask, hmask;
 	int err;
 
+	WARN_ON_FPU(mask & ~xfeatures_mask_independent());
+
 	if (WARN_ON_FPU(!boot_cpu_has(X86_FEATURE_XSAVES)))
 		return;
 
-	if (WARN_ON_FPU(!independent_mask))
+	if (WARN_ON_FPU(!mask))
 		return;
 
-	lmask = independent_mask;
-	hmask = independent_mask >> 32;
+	lmask = mask;
+	hmask = mask >> 32;
 
 	XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
 
Signature · 1 line
2.31.1
maintainer
#2 lore raw

Re: [PATCH 1/2] x86/fpu: Rename "dynamic" XSTATEs to "independent"

In reply to Andy Lutomirski
Quoted 39 lines · Andy Lutomirski

The salient feature of "dynamic" XSTATEs is that they are not part of the main task XSTATE buffer. The fact that they are dynamically allocated is irrelevant and will become quite confusing when user math XSTATEs start being dynamically allocated. Rename them to "independent" because they are independent of the main XSTATE code.

This is just a search-and-replace with some whitespace updates to keep things aligned.

---
 arch/x86/events/intel/lbr.c       |  6 +--
 arch/x86/include/asm/fpu/xstate.h | 14 +++----
 arch/x86/kernel/fpu/xstate.c      | 62 +++++++++++++++----------------
 3 files changed, 41 insertions(+), 41 deletions(-)
arch/x86/events/intel/lbr.c +2 -2
index 76dbab6ac9fb..0189807fc3c1 100644
@@ -491,7 +491,7 @@ static void intel_pmu_arch_lbr_xrstors(void *ctx)
 {
 	struct x86_perf_task_context_arch_lbr_xsave *task_ctx = ctx;
 
-	copy_kernel_to_dynamic_supervisor(&task_ctx->xsave, XFEATURE_MASK_LBR);
+	copy_kernel_to_independent_supervisor(&task_ctx->xsave, XFEATURE_MASK_LBR);
 }
 
 static __always_inline bool lbr_is_reset_in_cstate(void *ctx)
@@ -576,7 +576,7 @@ static void intel_pmu_arch_lbr_xsaves(void *ctx)
 {
 	struct x86_perf_task_context_arch_lbr_xsave *task_ctx = ctx;
 
-	copy_dynamic_supervisor_to_kernel(&task_ctx->xsave, XFEATURE_MASK_LBR);
+	copy_independent_supervisor_to_kernel(&task_ctx->xsave, XFEATURE_MASK_LBR);
 }
 
 static void __intel_pmu_lbr_save(void *ctx)

arch/x86/events/intel/lbr.c static void intel_pmu_arch_lbr_read_xsave(struct cpu_hw_events *cpuc) · line 984

Quoted 8 lines
arch/x86/events/intel/lbr.c +1 -1
@@ -978,7 +978,7 @@ static void intel_pmu_arch_lbr_read_xsave(struct cpu_hw_events *cpuc)
 		intel_pmu_store_lbr(cpuc, NULL);
 		return;
 	}
-	copy_dynamic_supervisor_to_kernel(&xsave->xsave, XFEATURE_MASK_LBR);
+	copy_independent_supervisor_to_kernel(&xsave->xsave, XFEATURE_MASK_LBR);
 
 	intel_pmu_store_lbr(cpuc, xsave->lbr.entries);
 }

Yesterday tglx proposed the *save*_to_{user,kernel}() and *rstor*_from_{user,kernel}() namespace for pretty much every other such function.

And while I agree that independent_supervisor beats dynamic_supervisor for a name, they're both stupid long :-(

I don't suppose we can simply use xsaves_to_kernel() xstrors_from_kernel() and add some magic to their respective mask handling to ensure that a mask belongs to only 1 (of 3) types.

	int types = 0;

	if (mask & xfeatures_mask_user())
		type++;
	if (mask & xfeatures_mask_supervisor())
		types++;
	if (mask & xfeatures_mask_independent())
		types++;

	if (WARN_ON_ONCE(type != 1))
		return;

?

#3 lore raw

Re: [PATCH 1/2] x86/fpu: Rename "dynamic" XSTATEs to "independent"

Quoted 12 lines · Peter Zijlstra

On Thu, Jun 10, 2021 at 10:13:36PM -0700, Andy Lutomirski wrote:
Yesterday tglx proposed the *save*_to_{user,kernel}() and *rstor*_from_{user,kernel}() namespace for pretty much every other such function.

And while I agree that independent_supervisor beats dynamic_supervisor for a name, they're both stupid long :-(

I don't suppose we can simply use xsaves_to_kernel() xstrors_from_kernel() and add some magic to their respective mask handling to ensure that a mask belongs to only 1 (of 3) types.

Quoted 11 lines
	int types = 0;

	if (mask & xfeatures_mask_user())
		type++;
	if (mask & xfeatures_mask_supervisor())
		types++;
	if (mask & xfeatures_mask_independent())
		types++;

	if (WARN_ON_ONCE(type != 1))
		return;

We basically have only two sets which are exclusive:

Features which end up in task->fpu.state.xstate and those independent ones. Let me add something like this to the pile I have anyway.

I pick up the rename of the mask bits though, as this dynamic naming is really bad. I'm not too happy with that independent name either, but it's at least better.

Thanks,

tglx

#4 lore raw

[tip: x86/fpu] x86/fpu: Rename "dynamic" XSTATEs to "independent"

In reply to Andy Lutomirski
The following commit has been merged into the x86/fpu branch of tip:

The following commit has been merged into the x86/fpu branch of tip:

Commit-ID:     01707b66535872f7a0d87f66078fd018d1814be0
Gitweb:        https://git.kernel.org/tip/01707b66535872f7a0d87f66078fd018d1814be0
Author:        Andy Lutomirski <[email protected]>
AuthorDate:    Wed, 23 Jun 2021 14:02:03 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 23 Jun 2021 18:42:11 +02:00

x86/fpu: Rename "dynamic" XSTATEs to "independent"

The salient feature of "dynamic" XSTATEs is that they are not part of the main task XSTATE buffer. The fact that they are dynamically allocated is irrelevant and will become quite confusing when user math XSTATEs start being dynamically allocated. Rename them to "independent" because they are independent of the main XSTATE code.

This is just a search-and-replace with some whitespace updates to keep things aligned.

---
 arch/x86/events/intel/lbr.c       |  6 +--
 arch/x86/include/asm/fpu/xstate.h | 22 +++++------
 arch/x86/kernel/fpu/xstate.c      | 62 +++++++++++++++---------------
 3 files changed, 45 insertions(+), 45 deletions(-)
arch/x86/events/intel/lbr.c +3 -3
index 4409d2c..2fa2df3 100644
@@ -491,7 +491,7 @@ static void intel_pmu_arch_lbr_xrstors(void *ctx)
 {
 	struct x86_perf_task_context_arch_lbr_xsave *task_ctx = ctx;
 
-	copy_kernel_to_dynamic_supervisor(&task_ctx->xsave, XFEATURE_MASK_LBR);
+	copy_kernel_to_independent_supervisor(&task_ctx->xsave, XFEATURE_MASK_LBR);
 }
 
 static __always_inline bool lbr_is_reset_in_cstate(void *ctx)
@@ -576,7 +576,7 @@ static void intel_pmu_arch_lbr_xsaves(void *ctx)
 {
 	struct x86_perf_task_context_arch_lbr_xsave *task_ctx = ctx;
 
-	copy_dynamic_supervisor_to_kernel(&task_ctx->xsave, XFEATURE_MASK_LBR);
+	copy_independent_supervisor_to_kernel(&task_ctx->xsave, XFEATURE_MASK_LBR);
 }
 
 static void __intel_pmu_lbr_save(void *ctx)
@@ -992,7 +992,7 @@ static void intel_pmu_arch_lbr_read_xsave(struct cpu_hw_events *cpuc)
 		intel_pmu_store_lbr(cpuc, NULL);
 		return;
 	}
-	copy_dynamic_supervisor_to_kernel(&xsave->xsave, XFEATURE_MASK_LBR);
+	copy_independent_supervisor_to_kernel(&xsave->xsave, XFEATURE_MASK_LBR);
 
 	intel_pmu_store_lbr(cpuc, xsave->lbr.entries);
 }
arch/x86/include/asm/fpu/xstate.h +11 -11
index 00e1a2a..a55bd5c 100644
@@ -42,21 +42,21 @@
  * and its size may be huge. Saving/restoring such supervisor state components
  * at each context switch can cause high CPU and space overhead, which should
  * be avoided. Such supervisor state components should only be saved/restored
- * on demand. The on-demand dynamic supervisor features are set in this mask.
+ * on demand. The on-demand supervisor features are set in this mask.
  *
- * Unlike the existing supported supervisor features, a dynamic supervisor
+ * Unlike the existing supported supervisor features, an independent supervisor
  * feature does not allocate a buffer in task->fpu, and the corresponding
  * supervisor state component cannot be saved/restored at each context switch.
  *
- * To support a dynamic supervisor feature, a developer should follow the
+ * To support an independent supervisor feature, a developer should follow the
  * dos and don'ts as below:
  * - Do dynamically allocate a buffer for the supervisor state component.
  * - Do manually invoke the XSAVES/XRSTORS instruction to save/restore the
  *   state component to/from the buffer.
- * - Don't set the bit corresponding to the dynamic supervisor feature in
+ * - Don't set the bit corresponding to the independent supervisor feature in
  *   IA32_XSS at run time, since it has been set at boot time.
  */
-#define XFEATURE_MASK_DYNAMIC (XFEATURE_MASK_LBR)
+#define XFEATURE_MASK_INDEPENDENT (XFEATURE_MASK_LBR)
 
 /*
  * Unsupported supervisor features. When a supervisor feature in this mask is
@@ -66,7 +66,7 @@
 
 /* All supervisor states including supported and unsupported states. */
 #define XFEATURE_MASK_SUPERVISOR_ALL (XFEATURE_MASK_SUPERVISOR_SUPPORTED | \
-				      XFEATURE_MASK_DYNAMIC | \
+				      XFEATURE_MASK_INDEPENDENT | \
 				      XFEATURE_MASK_SUPERVISOR_UNSUPPORTED)
 
 #ifdef CONFIG_X86_64
@@ -87,12 +87,12 @@ static inline u64 xfeatures_mask_user(void)
 	return xfeatures_mask_all & XFEATURE_MASK_USER_SUPPORTED;
 }
 
-static inline u64 xfeatures_mask_dynamic(void)
+static inline u64 xfeatures_mask_independent(void)
 {
 	if (!boot_cpu_has(X86_FEATURE_ARCH_LBR))
-		return XFEATURE_MASK_DYNAMIC & ~XFEATURE_MASK_LBR;
+		return XFEATURE_MASK_INDEPENDENT & ~XFEATURE_MASK_LBR;
 
-	return XFEATURE_MASK_DYNAMIC;
+	return XFEATURE_MASK_INDEPENDENT;
 }
 
 extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
@@ -104,8 +104,8 @@ void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr);
 int xfeature_size(int xfeature_nr);
 int copy_uabi_from_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf);
 int copy_sigframe_from_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf);
-void copy_dynamic_supervisor_to_kernel(struct xregs_state *xstate, u64 mask);
-void copy_kernel_to_dynamic_supervisor(struct xregs_state *xstate, u64 mask);
+void copy_independent_supervisor_to_kernel(struct xregs_state *xstate, u64 mask);
+void copy_kernel_to_independent_supervisor(struct xregs_state *xstate, u64 mask);
 
 enum xstate_copy_mode {
 	XSTATE_COPY_FP,
arch/x86/kernel/fpu/xstate.c +31 -31

152 lines not on this page · raw message