Skip to contentSkip to conversation

x86/fpu: a few code cleanup

PATCH New Chang S. Bae 1 participant 5 messages Jun 21, 2023 x86

lore
Share & tools
lore
#0 lore raw

cover x86/fpu: a few code cleanup

Hi all,

This series intends to clean up the x86 FPU code. They do *not* deal with any urgent issues but considerably apparent cleanups:

- Simplify xstate_calculate_size() and print_xstate_features() as both have some inefficient duplications.
- Convert get_xsave_addr() to a local function because it is not invoked anywhere else.
- Clean up the unnecessary fpstate_reset() since the invocation makes no change in the fpstate container.

The last one might have an unknown reason behind it. Then, I will revise it with the clarification instead of removing the code.

The series can be found in this repo:
git://github.com/intel/amx-linux.git x86-fpu-cleanup

Thanks,
Chang

Chang S. Bae (4):
x86/fpu/xstate: Convert get_xsave_addr() to a static function x86/fpu/xstate: Simplify xstate_calculate_size() x86/fpu/xstate: Simplify print_xstate_features() x86/fpu: Remove the unnecessary fpstate_reset() invocation

 arch/x86/kernel/fpu/init.c   |  1 -
 arch/x86/kernel/fpu/xstate.c | 36 +++++++++++-------------------------
 arch/x86/kernel/fpu/xstate.h |  2 --
 3 files changed, 11 insertions(+), 28 deletions(-)

base-commit: 162d2c2297b731a9918bd26db5a9f779de259c09

Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/4 x86/fpu/xstate: Convert get_xsave_addr() to a static function

The function is defined as global but is not invoked anywhere else. Switch it a local (static) function.

---
 arch/x86/kernel/fpu/xstate.c | 2 +-
 arch/x86/kernel/fpu/xstate.h | 2 --
 2 files changed, 1 insertion(+), 3 deletions(-)
arch/x86/kernel/fpu/xstate.c +1 -1
index 0bab497c9436..2f03acc47f62 100644
@@ -956,7 +956,7 @@ static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr)
  *	address of the state in the xsave area, or NULL if the
  *	field is not present in the xsave buffer.
  */
-void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr)
+static void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr)
 {
 	/*
 	 * Do we even *have* xsave state?
arch/x86/kernel/fpu/xstate.h +0 -2
index a4ecb04d8d64..82f2d3c41298 100644
@@ -53,8 +53,6 @@ extern int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, const void
 extern void fpu__init_cpu_xstate(void);
 extern void fpu__init_system_xstate(unsigned int legacy_size);
 
-extern void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr);
-
 static inline u64 xfeatures_mask_supervisor(void)
 {
 	return fpu_kernel_cfg.max_features & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/4 x86/fpu/xstate: Simplify xstate_calculate_size()

In reply to Chang S. Bae

This size calculation code uses xfeatures_get_offset() only for the compacted format. But, it is capable of handling both formats. Use it for simpler and more concise code.

---
 arch/x86/kernel/fpu/xstate.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
arch/x86/kernel/fpu/xstate.c +1 -4
index 2f03acc47f62..d488621b280e 100644
@@ -563,14 +563,11 @@ static bool __init check_xstate_against_struct(int nr)
 static unsigned int xstate_calculate_size(u64 xfeatures, bool compacted)
 {
 	unsigned int topmost = fls64(xfeatures) -  1;
-	unsigned int offset = xstate_offsets[topmost];
 
 	if (topmost <= XFEATURE_SSE)
 		return sizeof(struct xregs_state);
 
-	if (compacted)
-		offset = xfeature_get_offset(xfeatures, topmost);
-	return offset + xstate_sizes[topmost];
+	return xfeature_get_offset(xfeatures, topmost) + xstate_sizes[topmost];
 }
 
 /*
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/4 x86/fpu/xstate: Simplify print_xstate_features()

In reply to Chang S. Bae

The print_xstate_features() invokes print_xstate_feature() multiple times in separate lines which can be replaced with a loop.

The print_xstate_feature() function already checks the feature enabling status, and it is only called from within print_xstate_features(). So the code can be relocated.

Move the code exactly to print_xstate_features() and wrap it with a loop there.

---
 arch/x86/kernel/fpu/xstate.c | 29 +++++++++--------------------
 1 file changed, 9 insertions(+), 20 deletions(-)
arch/x86/kernel/fpu/xstate.c +9 -20
index d488621b280e..6d72498ea0bc 100644
@@ -253,31 +253,20 @@ static void __init setup_xstate_cache(void)
 	}
 }
 
-static void __init print_xstate_feature(u64 xstate_mask)
-{
-	const char *feature_name;
-
-	if (cpu_has_xfeatures(xstate_mask, &feature_name))
-		pr_info("x86/fpu: Supporting XSAVE feature 0x%03Lx: '%s'\n", xstate_mask, feature_name);
-}
-
 /*
  * Print out all the supported xstate features:
  */
 static void __init print_xstate_features(void)
 {
-	print_xstate_feature(XFEATURE_MASK_FP);
-	print_xstate_feature(XFEATURE_MASK_SSE);
-	print_xstate_feature(XFEATURE_MASK_YMM);
-	print_xstate_feature(XFEATURE_MASK_BNDREGS);
-	print_xstate_feature(XFEATURE_MASK_BNDCSR);
-	print_xstate_feature(XFEATURE_MASK_OPMASK);
-	print_xstate_feature(XFEATURE_MASK_ZMM_Hi256);
-	print_xstate_feature(XFEATURE_MASK_Hi16_ZMM);
-	print_xstate_feature(XFEATURE_MASK_PKRU);
-	print_xstate_feature(XFEATURE_MASK_PASID);
-	print_xstate_feature(XFEATURE_MASK_XTILE_CFG);
-	print_xstate_feature(XFEATURE_MASK_XTILE_DATA);
+	int i;
+
+	for (i = 0; i < XFEATURE_MAX; i++) {
+		u64 mask = BIT_ULL(i);
+		const char *name;
+
+		if (cpu_has_xfeatures(mask, &name))
+			pr_info("x86/fpu: Supporting XSAVE feature 0x%03Lx: '%s'\n", mask, name);
+	}
 }
 
 /*
Signature · 1 line
2.34.1
#4 lore raw

PATCH 4/4 x86/fpu: Remove the unnecessary fpstate_reset() invocation

In reply to Chang S. Bae

fpstate_reset() initializes the fpstate container. But, when no state has been established yet, calling this is not meaningful. In the fpu__init_system() function, it is called immediately after the legacy FPU size is determined. So, remove it as not causing any issues.

---

The change made by fpstate_reset() can be measured with this:

static struct fpstate fps;

void __init fpu__init_system(struct cpuinfo_x86 *c)

{
	memcpy(&fps, &current->thread.fpu.__fpstate, sizeof(struct fpstate));
	fpstate_reset(&current->thread.fpu);
	if (memcmp(&fps, &current->thread.fpu.__fpstate, sizeof(struct fpstate)))
		pr_info("fpstate has reason to be inited at %s\n", __func__);
	else
		pr_info("fpstate has no reason to be inited at %s\n", __func__);
	...
}

Although it looks to be a no-op, there might be some unknown intention behind it. If there is a specific reason for invoking fpstate_reset() at that point, this should be documented properly instead of removing it.

---
 arch/x86/kernel/fpu/init.c | 1 -
 1 file changed, 1 deletion(-)
arch/x86/kernel/fpu/init.c +0 -1
index 998a08f17e33..95af3084c4cf 100644
@@ -213,7 +213,6 @@ static void __init fpu__init_system_xstate_size_legacy(void)
  */
 void __init fpu__init_system(void)
 {
-	fpstate_reset(&current->thread.fpu);
 	fpu__init_system_early_generic();
 
 	/*
Signature · 1 line
2.34.1