[RFC PATCH 05/10] x86/fpu: Cache xstate_is_aligned

From: Jiaxun Yang
Date: Thu Dec 02 2021 - 19:37:00 EST


As we're now calculating xsate addr on fly, we are going to
access xfeature_is_aligned frequently. Cache it to speed up
calculation.

Signed-off-by: Jiaxun Yang <j.yang-87@xxxxxxxxxxxx>
---
arch/x86/kernel/fpu/xstate.c | 25 ++++++++++++++++++-------
1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c
index e3ed3d0f3741..df8a70a055a9 100644
--- a/arch/x86/kernel/fpu/xstate.c
+++ b/arch/x86/kernel/fpu/xstate.c
@@ -83,6 +83,8 @@ static unsigned int xstate_sizes[XFEATURE_MAX] __ro_after_init =
{ [ 0 ... XFEATURE_MAX - 1] = -1};
static unsigned int xstate_supervisor_only_offsets[XFEATURE_MAX] __ro_after_init =
{ [ 0 ... XFEATURE_MAX - 1] = -1};
+static bool xstate_is_aligned[XFEATURE_MAX] __ro_after_init =
+ { [ 0 ... XFEATURE_MAX - 1] = -1};

/*
* Return whether the system supports a given xfeature.
@@ -261,10 +263,6 @@ static void __init print_xstate_features(void)
WARN_ON(nr >= XFEATURE_MAX); \
} while (0)

-/*
- * We could cache this like xstate_size[], but we only use
- * it here, so it would be a waste of space.
- */
static int xfeature_is_aligned(int xfeature_nr)
{
u32 eax, ebx, ecx, edx;
@@ -286,6 +284,18 @@ static int xfeature_is_aligned(int xfeature_nr)
return !!(ecx & 2);
}

+/*
+ * Setup a cache to tell if a xstate needs to care alignment:
+ */
+static void __init setup_is_aligned(void)
+{
+ int i;
+
+ for_each_extended_xfeature(i, fpu_kernel_cfg.max_features) {
+ xstate_is_aligned[i] = xfeature_is_aligned(i);
+ }
+}
+
/*
* Setup offsets of a supervisor-state-only XSAVES buffer:
*
@@ -302,7 +312,7 @@ static void __init setup_supervisor_only_offsets(void)
if (!xfeature_is_supervisor(i))
continue;

- if (xfeature_is_aligned(i))
+ if (xstate_is_aligned[i])
next_offset = ALIGN(next_offset, 64);

xstate_supervisor_only_offsets[i] = next_offset;
@@ -595,7 +605,7 @@ static unsigned int xstate_calculate_size(u64 xfeatures, bool compacted)

for_each_extended_xfeature(i, xfeatures) {
/* Align from the end of the previous feature */
- if (xfeature_is_aligned(i))
+ if (xstate_is_aligned[i])
size = ALIGN(size, 64);
/*
* In compacted format the enabled features are packed,
@@ -881,6 +891,7 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)

/* Enable xstate instructions to be able to continue with initialization: */
fpu__init_cpu_xstate();
+ setup_is_aligned();
err = init_xstate_size();
if (err)
goto out_disable;
@@ -966,7 +977,7 @@ static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr)
/* Calculate compact ext state offsets */
offset = FXSAVE_SIZE + XSAVE_HDR_SIZE;
for_each_extended_xfeature(i, xsave->header.xcomp_bv) {
- if (xfeature_is_aligned(i))
+ if (xstate_is_aligned[i])
offset = ALIGN(offset, 64);

if (i == xfeature_nr)
--
2.30.2