Skip to contentSkip to conversation

x86/fpu: Fix invalid FPU ptrace state after execve

PATCH Superseded Yu-cheng Yu 4 participants 5 messages Nov 16, 2016 – Nov 21, 2016 x86

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/fpu: Fix invalid FPU ptrace state after execve

Robert O'Callahan reported that after an execve PTRACE_GETREGSET NT_X86_XSTATE continues to return the pre-exec register values until the exec'ed task modifies FPU state. The test code is at
https://bugzilla.redhat.com/attachment.cgi?id=1164286.

What is happening is when eagerfpu is enabled, fpu__clear() did not properly clear fpstate. Fix it by doing just that.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc David Hansen <[email protected]>
Cc Fenghua Yu <[email protected]>
Cc "Ravi V. Shankar" <[email protected]>
---
 arch/x86/kernel/fpu/core.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
arch/x86/kernel/fpu/core.c +8 -8
index 4700401..4c203c4 100644
@@ -521,14 +521,14 @@ void fpu__clear(struct fpu *fpu)
 {
 	WARN_ON_FPU(fpu != &current->thread.fpu); /* Almost certainly an anomaly */
 
-	if (!use_eager_fpu() || !static_cpu_has(X86_FEATURE_FPU)) {
-		/* FPU state will be reallocated lazily at the first use. */
-		fpu__drop(fpu);
-	} else {
-		if (!fpu->fpstate_active) {
-			fpu__activate_curr(fpu);
-			user_fpu_begin();
-		}
+	fpu__drop(fpu);
+
+	/*
+	 * When eagerfpu is used, make sure fpstate is cleared and initialized.
+	 */
+	if (use_eager_fpu()) {
+		fpu__activate_curr(fpu);
+		user_fpu_begin();
 		copy_init_fpstate_to_fpregs();
 	}
 }
Signature · 1 line
1.9.1
#1 lore raw
Quoted 34 lines · Yu-cheng Yu

Robert O'Callahan reported that after an execve PTRACE_GETREGSET NT_X86_XSTATE continues to return the pre-exec register values until the exec'ed task modifies FPU state. The test code is at
https://bugzilla.redhat.com/attachment.cgi?id=1164286.

What is happening is when eagerfpu is enabled, fpu__clear() did not properly clear fpstate. Fix it by doing just that.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc David Hansen <[email protected]>
Cc Fenghua Yu <[email protected]>
Cc "Ravi V. Shankar" <[email protected]>
---
 arch/x86/kernel/fpu/core.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
arch/x86/kernel/fpu/core.c +0 -4
index 4700401..4c203c4 100644
@@ -521,14 +521,14 @@ void fpu__clear(struct fpu *fpu)
 {
 	WARN_ON_FPU(fpu != &current->thread.fpu); /* Almost certainly an anomaly */
 
-	if (!use_eager_fpu() || !static_cpu_has(X86_FEATURE_FPU)) {
-		/* FPU state will be reallocated lazily at the first use. */
-		fpu__drop(fpu);
-	} else {

arch/x86/kernel/fpu/core.c void fpu__clear(struct fpu *fpu) · line 529

Quoted 10 lines
arch/x86/kernel/fpu/core.c +6 -4
-		if (!fpu->fpstate_active) {
-			fpu__activate_curr(fpu);
-			user_fpu_begin();
-		}
+	fpu__drop(fpu);
+
+	/*
+	 * When eagerfpu is used, make sure fpstate is cleared and initialized.
+	 */
+	if (use_eager_fpu()) {

c592b5734706 ("x86/fpu: Remove use_eager_fpu()")

Please redo this patch against tip/master.

Signature · 5 lines
Regards/Gruss,
    Boris.

SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- 
maintainer
#2 lore raw
In reply to Yu-cheng Yu
Quoted 7 lines · Yu-cheng Yu

Robert O'Callahan reported that after an execve PTRACE_GETREGSET NT_X86_XSTATE continues to return the pre-exec register values until the exec'ed task modifies FPU state. The test code is at
https://bugzilla.redhat.com/attachment.cgi?id=1164286.

What is happening is when eagerfpu is enabled, fpu__clear() did not properly clear fpstate. Fix it by doing just that.

Functionally, I think the patch is fine. just a few comment/documentation nits.

I think fpu__clear()'s comments are a bit out of date. Could we make it clear that it is invalidating both fpregs *and* fpstate?

I also think the

	/* FPU state will be reallocated lazily at the first use. */"

comment was fairly valuable. Could we find some way to keep it?

The new comment:

arch/x86/kernel/fpu/core.c void fpu__clear(struct fpu *fpu) · line 528

Quoted 3 lines
arch/x86/kernel/fpu/core.c +3 -0
+	/*
+	 * When eagerfpu is used, make sure fpstate is cleared and initialized.
+	 */

also kinda implies that the if() block is only messing with fpstate. Could we make that more clear? Maybe by commenting the individual lines inside the if():

arch/x86/kernel/fpu/core.c void fpu__clear(struct fpu *fpu) · line 531

Quoted 3 lines
arch/x86/kernel/fpu/core.c +3 -0
+	if (use_eager_fpu()) {
+		fpu__activate_curr(fpu);
+		user_fpu_begin();

instead of having it above? Maybe something like:

	if (use_eager_fpu()) {
		/* activate and load init fpstate into 'fpu' */
		fpu__activate_curr(fpu);
		/* re-activate fpregs: */
		user_fpu_begin();
		/* take new init fpstate and place in fpregs: */

copy_init_fpstate_to_fpregs();
}

#3 lore raw
Quoted 2 lines · Dave Hansen

Functionally, I think the patch is fine. just a few comment/documentation nits.

Thanks! I will wait for a few days for any comments that might come up and fix all together.

Yu-cheng

#4 lore raw
In reply to Dave Hansen
Quoted 43 lines · Dave Hansen
Quoted 7 lines · Yu-cheng Yu

Robert O'Callahan reported that after an execve PTRACE_GETREGSET NT_X86_XSTATE continues to return the pre-exec register values until the exec'ed task modifies FPU state. The test code is at
https://bugzilla.redhat.com/attachment.cgi?id=1164286.

What is happening is when eagerfpu is enabled, fpu__clear() did not properly clear fpstate. Fix it by doing just that.

Functionally, I think the patch is fine. just a few comment/documentation nits.

I think fpu__clear()'s comments are a bit out of date. Could we make it clear that it is invalidating both fpregs *and* fpstate?

I also think the

	/* FPU state will be reallocated lazily at the first use. */"

comment was fairly valuable. Could we find some way to keep it?

The new comment:

arch/x86/kernel/fpu/core.c void fpu__clear(struct fpu *fpu) · line 528

Quoted 3 lines
arch/x86/kernel/fpu/core.c +3 -0
+	/*
+	 * When eagerfpu is used, make sure fpstate is cleared and initialized.
+	 */

also kinda implies that the if() block is only messing with fpstate. Could we make that more clear? Maybe by commenting the individual lines inside the if():

arch/x86/kernel/fpu/core.c void fpu__clear(struct fpu *fpu) · line 531

Quoted 3 lines
arch/x86/kernel/fpu/core.c +3 -0
+	if (use_eager_fpu()) {
+		fpu__activate_curr(fpu);
+		user_fpu_begin();

instead of having it above? Maybe something like:

	if (use_eager_fpu()) {
		/* activate and load init fpstate into 'fpu' */
		fpu__activate_curr(fpu);
		/* re-activate fpregs: */
		user_fpu_begin();
		/* take new init fpstate and place in fpregs: */

arch/x86/kernel/fpu/core.c void fpu__clear(struct fpu *fpu) · line 533

Quoted 2 lines
arch/x86/kernel/fpu/core.c +0 -0
 		copy_init_fpstate_to_fpregs();
 	}

I agree with these suggestions - but I'll apply the simple patch to x86/urgent - which can then be backported as far as necessary, and then resolve the conflict with the v4.10 tip:x86/fpu branch, and on top of that we can fix these details, ok?

In particular I don't like it how non-obvious the semantics are from the function names. I think we should try to improve the nomenclature instead of adding comments to every line.

Thanks,

Ingo