Skip to contentSkip to conversation

x86/fpu/xstate: calculate the number by sizeof and offsetof

PATCH In review Yejune Deng 3 participants 4 messages Jan 20, 2021 – Jan 22, 2021 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/fpu/xstate: calculate the number by sizeof and offsetof

In fpstate_sanitize_xstate(), use memset and offsetof instead of '= 0', and use sizeof instead of a constant.

---
 arch/x86/kernel/fpu/xstate.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
arch/x86/kernel/fpu/xstate.c +3 -7
index 5d8047441a0a..2e75630c86e9 100644
@@ -161,20 +161,16 @@ void fpstate_sanitize_xstate(struct fpu *fpu)
 	 * FP is in init state
 	 */
 	if (!(xfeatures & XFEATURE_MASK_FP)) {
+		memset(fx, 0, offsetof(struct fxregs_state, mxcsr));
+		memset(fx->st_space, 0, sizeof(fx->st_space));
 		fx->cwd = 0x37f;
-		fx->swd = 0;
-		fx->twd = 0;
-		fx->fop = 0;
-		fx->rip = 0;
-		fx->rdp = 0;
-		memset(&fx->st_space[0], 0, 128);
 	}
 
 	/*
 	 * SSE is in init state
 	 */
 	if (!(xfeatures & XFEATURE_MASK_SSE))
-		memset(&fx->xmm_space[0], 0, 256);
+		memset(fx->xmm_space, 0, sizeof(fx->xmm_space));
 
 	/*
 	 * First two features are FPU and SSE, which above we handled
Signature · 1 line
2.29.0
#1 lore raw
Quoted 2 lines · Yejune Deng

In fpstate_sanitize_xstate(), use memset and offsetof instead of '= 0', and use sizeof instead of a constant.

What's the benefit to doing this? Saving 4 lines of code?

Your suggestions are not obviously wrong at a glance, but they're also not obviously right.

maintainer
#2 lore raw
In reply to Yejune Deng
Quoted 13 lines · Yejune Deng

In fpstate_sanitize_xstate(), use memset and offsetof instead of '= 0', and use sizeof instead of a constant.

---
 arch/x86/kernel/fpu/xstate.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
arch/x86/kernel/fpu/xstate.c +0 -0
index 5d8047441a0a..2e75630c86e9 100644

arch/x86/kernel/fpu/xstate.c void fpstate_sanitize_xstate(struct fpu *fpu) · line 164

Quoted 4 lines
arch/x86/kernel/fpu/xstate.c +1 -0
@@ -161,20 +161,16 @@ void fpstate_sanitize_xstate(struct fpu *fpu)
 	 * FP is in init state
 	 */
 	if (!(xfeatures & XFEATURE_MASK_FP)) {
+		memset(fx, 0, offsetof(struct fxregs_state, mxcsr));

I'd prefer the explicit zeroing instead of having to look at fxregs_state and where the offset of mxcsr is.

arch/x86/kernel/fpu/xstate.c void fpstate_sanitize_xstate(struct fpu *fpu) · line 165

Quoted 1 line
arch/x86/kernel/fpu/xstate.c +1 -0
+		memset(fx->st_space, 0, sizeof(fx->st_space));

This is ok I guess.

Quoted 5 lines
arch/x86/kernel/fpu/xstate.c +0 -4
 		fx->cwd = 0x37f;
-		fx->swd = 0;
-		fx->twd = 0;
-		fx->fop = 0;
-		fx->rip = 0;

arch/x86/kernel/fpu/xstate.c void fpstate_sanitize_xstate(struct fpu *fpu) · line 173

Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +1 -3
-		fx->rdp = 0;
-		memset(&fx->st_space[0], 0, 128);
 	}
 
 	/*
 	 * SSE is in init state
 	 */
 	if (!(xfeatures & XFEATURE_MASK_SSE))
-		memset(&fx->xmm_space[0], 0, 256);
+		memset(fx->xmm_space, 0, sizeof(fx->xmm_space));

This too.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#3 lore raw

Thank you for your suggestion!

Quoted 50 lines · Borislav Petkov
Quoted 13 lines · Yejune Deng

In fpstate_sanitize_xstate(), use memset and offsetof instead of '= 0', and use sizeof instead of a constant.

---
 arch/x86/kernel/fpu/xstate.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
arch/x86/kernel/fpu/xstate.c +0 -0
index 5d8047441a0a..2e75630c86e9 100644

arch/x86/kernel/fpu/xstate.c void fpstate_sanitize_xstate(struct fpu *fpu) · line 164

Quoted 4 lines
arch/x86/kernel/fpu/xstate.c +1 -0
@@ -161,20 +161,16 @@ void fpstate_sanitize_xstate(struct fpu *fpu)
       * FP is in init state
       */
      if (!(xfeatures & XFEATURE_MASK_FP)) {
+             memset(fx, 0, offsetof(struct fxregs_state, mxcsr));

I'd prefer the explicit zeroing instead of having to look at fxregs_state and where the offset of mxcsr is.

arch/x86/kernel/fpu/xstate.c void fpstate_sanitize_xstate(struct fpu *fpu) · line 165

Quoted 1 line
arch/x86/kernel/fpu/xstate.c +1 -0
+             memset(fx->st_space, 0, sizeof(fx->st_space));

This is ok I guess.

Quoted 5 lines
arch/x86/kernel/fpu/xstate.c +0 -4
              fx->cwd = 0x37f;
-             fx->swd = 0;
-             fx->twd = 0;
-             fx->fop = 0;
-             fx->rip = 0;

arch/x86/kernel/fpu/xstate.c void fpstate_sanitize_xstate(struct fpu *fpu) · line 173

Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +1 -3
-             fx->rdp = 0;
-             memset(&fx->st_space[0], 0, 128);
      }
 
      /*
       * SSE is in init state
       */
      if (!(xfeatures & XFEATURE_MASK_SSE))
-             memset(&fx->xmm_space[0], 0, 256);
+             memset(fx->xmm_space, 0, sizeof(fx->xmm_space));

This too.

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette