Skip to contentSkip to conversation

x86/fpu: set the xcomp_bv when we fake up a XSAVES area

PATCH In review Kevin Hao 5 participants 19 messages Jan 22, 2017 – Feb 14, 2017 x86

lore
Share & tools
lore

Kevin Hao

author
#0 lore raw

PATCH 1/1 x86/fpu: set the xcomp_bv when we fake up a XSAVES area

I got the following calltrace on a Apollo Lake SoC with 32bit kernel.

  WARNING: CPU: 2 PID: 261 at arch/x86/include/asm/fpu/internal.h:363 fpu__restore+0x1f5/0x260
  Modules linked in:
  CPU: 2 PID: 261 Comm: check_hostname. Not tainted 4.10.0-rc4-next-20170120 #90
  Hardware name: Intel Corp. Broxton P/NOTEBOOK, BIOS APLIRVPA.X64.0138.B35.1608091058 08/09/2016
  Call Trace:
   dump_stack+0x47/0x5f
   __warn+0xea/0x110
   ? fpu__restore+0x1f5/0x260
   warn_slowpath_null+0x2a/0x30
   fpu__restore+0x1f5/0x260
   __fpu__restore_sig+0x165/0x6b0
   fpu__restore_sig+0x2f/0x50
   restore_sigcontext.isra.9+0xe0/0xf0
   sys_sigreturn+0xaa/0xf0
   do_int80_syscall_32+0x59/0xb0
   entry_INT80_32+0x2a/0x2a
  EIP: 0xb77acc61
  EFLAGS: 00000246 CPU: 2
  EAX: 00000000 EBX: 00000003 ECX: 08151d38 EDX: 00000000
  ESI: bfa9ce20 EDI: 08151d38 EBP: 0000000c ESP: bfa9cdbc
   DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 007b

The reason is that a #GP occurs when executing XRSTORS. The root cause is that we forget to set the xcomp_bv when we fake up the XSAVES area in function copyin_to_xsaves().

---
 arch/x86/kernel/fpu/xstate.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kernel/fpu/xstate.c +2 -0
index 35f7024aace5..2c0df2681481 100644
@@ -1071,6 +1071,8 @@ int copyin_to_xsaves(const void *kbuf, const void __user *ubuf,
 	 * Add back in the features that came in from userspace:
 	 */
 	xsave->header.xfeatures |= xfeatures;
+	xsave->header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT |
+				 xsave->header.xfeatures;
 
 	return 0;
 }
Signature · 1 line
2.9.3
#1 lore raw

[tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

x86/fpu: Set the xcomp_bv when we fake up a XSAVES area
Commit-ID:  5fa356458b5c918bdf8307b070a3d74bc015d910
Gitweb:     http://git.kernel.org/tip/5fa356458b5c918bdf8307b070a3d74bc015d910
Author:     Kevin Hao <[email protected]>
AuthorDate: Sun, 22 Jan 2017 16:50:23 +0800
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 23 Jan 2017 09:03:03 +0100

x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

I got the following calltrace on a Apollo Lake SoC with 32-bit kernel:

  WARNING: CPU: 2 PID: 261 at arch/x86/include/asm/fpu/internal.h:363 fpu__restore+0x1f5/0x260
  [...]
  Hardware name: Intel Corp. Broxton P/NOTEBOOK, BIOS APLIRVPA.X64.0138.B35.1608091058 08/09/2016
  Call Trace:
   dump_stack()
   __warn()
   ? fpu__restore()
   warn_slowpath_null()
   fpu__restore()
   __fpu__restore_sig()
   fpu__restore_sig()
   restore_sigcontext.isra.9()
   sys_sigreturn()
   do_int80_syscall_32()
   entry_INT80_32()

The reason is that a #GP occurs when executing XRSTORS. The root cause is that we forget to set the xcomp_bv when we fake up the XSAVES area in the copyin_to_xsaves() function.

Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc Fenghua Yu <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Josh Poimboeuf <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Quentin Casasnovas <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Yu-cheng Yu <[email protected]>
---
 arch/x86/kernel/fpu/xstate.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kernel/fpu/xstate.c +1 -0
index 1d77704..e287b90 100644
@@ -1070,6 +1070,7 @@ int copyin_to_xsaves(const void *kbuf, const void __user *ubuf,
 	 * Add back in the features that came in from userspace:
 	 */
 	xsave->header.xfeatures |= xfeatures;
+	xsave->header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT | xsave->header.xfeatures;
 
 	return 0;
 }
maintainer
#2 lore raw

Re: [tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

Quoted 3 lines · tip-bot for Kevin Hao

x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

I got the following calltrace on a Apollo Lake SoC with 32-bit kernel:

...

arch/x86/kernel/fpu/xstate.c int copyin_to_xsaves(const void *kbuf, const void __user *ubuf, · line 1076

Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +1 -0
@@ -1070,6 +1070,7 @@ int copyin_to_xsaves(const void *kbuf, const void __user *ubuf,
 	 * Add back in the features that came in from userspace:
 	 */
 	xsave->header.xfeatures |= xfeatures;
+	xsave->header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT | xsave->header.xfeatures;
 
 	return 0;
 }

First of all, thanks for finding this! As you found, the 32-bit XSAVES code is a bit light on testing.

I don't doubt that we have a code path that was screwed up like this, but I don't think this is the right fix.

The kernel xsave buffer should *ALWAYS* have the XCOMP_BV_COMPACTED_FORMAT bit set. It should have been set before the copyin and it should be set when it's finished.

The best fix here would be not to paper over the issue in the copy function but find where it got clobbered, or where some initialization code failed to set it.

#3 lore raw

Re: [tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

Quoted 7 lines · Dave Hansen

The kernel xsave buffer should *ALWAYS* have the XCOMP_BV_COMPACTED_FORMAT bit set. It should have been set before the copyin and it should be set when it's finished.

The best fix here would be not to paper over the issue in the copy function but find where it got clobbered, or where some initialization code failed to set it.

Someone else reported different issues from the same bug and a different patch was just tested OK this morning. I think that adding xfeatures bits to xcomp_bv should have been done in fpstate_init().

Also, in copy_init_fpstate_to_fpregs(), we do:

	copy_kernel_to_xregs(&init_fpstate.xsave, -1).

That (-1) could mean (0) because the parameters are declared as:

copy_kernel_to_xregs(struct xregs_state *, u64)

Yu-cheng

maintainer
#4 lore raw

Re: [tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

Quoted 5 lines · Yu-cheng Yu
Quoted 4 lines · Dave Hansen

The kernel xsave buffer should *ALWAYS* have the XCOMP_BV_COMPACTED_FORMAT bit set. It should have been set before the copyin and it should be set when it's finished.

Quoted 7 lines
Quoted 3 lines

The best fix here would be not to paper over the issue in the copy function but find where it got clobbered, or where some initialization code failed to set it.

Someone else reported different issues from the same bug and a different patch was just tested OK this morning. I think that adding xfeatures bits to xcomp_bv should have been done in fpstate_init().

Right. So where did it get cleared out?

Quoted 7 lines

Also, in copy_init_fpstate_to_fpregs(), we do:

	copy_kernel_to_xregs(&init_fpstate.xsave, -1).

That (-1) could mean (0) because the parameters are declared as:

copy_kernel_to_xregs(struct xregs_state *, u64)

I'm not sure what you're saying. -1 just means "all 1's" when cast to an unsigned type. This shouldn't case any problems.

#5 lore raw

Re: [tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

Quoted 10 lines · Dave Hansen
Quoted 7 lines · Yu-cheng Yu
Quoted 3 lines

The best fix here would be not to paper over the issue in the copy function but find where it got clobbered, or where some initialization code failed to set it.

Someone else reported different issues from the same bug and a different patch was just tested OK this morning. I think that adding xfeatures bits to xcomp_bv should have been done in fpstate_init().

Right. So where did it get cleared out?

It is not set until a task triggers XSAVES. We did not set it in fpstate_init() because there is no valid data at the time. The problem happens when Linux copies data to the XSAVES area, like we see here; the kernel is not expected to change XSAVES format (xcomp_bv) but xcomp_bv is still blank (except bit 63).

Because XSAVES format is not changed after boot time and xcomp_bv does not affect INIT optimization, why don't we fix the problem in fpstate_init()?

Yu-cheng

maintainer
#6 lore raw

Re: [tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

Quoted 6 lines · Yu-cheng Yu
Quoted 5 lines · Dave Hansen
Quoted 4 lines · Yu-cheng Yu
Quoted 3 lines

The best fix here would be not to paper over the issue in the copy function but find where it got clobbered, or where some initialization code failed to set it.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Someone else reported different issues from the same bug and a different patch was just tested OK this morning. I think that adding xfeatures bits to xcomp_bv should have been done in fpstate_init().

Right. So where did it get cleared out?

It is not set until a task triggers XSAVES. We did not set it in fpstate_init() because there is no valid data at the time.

The code is:

Quoted 15 lines

void fpstate_init(union fpregs_state *state)

{
        if (!static_cpu_has(X86_FEATURE_FPU)) {
                fpstate_init_soft(&state->soft);
                return;
        }

        memset(state, 0, fpu_kernel_xstate_size);

        /*
         * XRSTORS requires that this bit is set in xcomp_bv, or
         * it will #GP. Make sure it is replaced after the memset().
         */
        if (static_cpu_has(X86_FEATURE_XSAVES))
                state->xsave.header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT;

That seems to set it unconditionally. What am I missing?

#7 lore raw

Re: [tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

Quoted 11 lines · Dave Hansen

The code is:

Quoted 9 lines

void fpstate_init(union fpregs_state *state)

{
        if (!static_cpu_has(X86_FEATURE_FPU)) {
                fpstate_init_soft(&state->soft);
                return;
        }

        memset(state, 0, fpu_kernel_xstate_size);
Quoted 8 lines
Quoted 6 lines
        /*
         * XRSTORS requires that this bit is set in xcomp_bv, or
         * it will #GP. Make sure it is replaced after the memset().
         */
        if (static_cpu_has(X86_FEATURE_XSAVES))
                state->xsave.header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT;

That seems to set it unconditionally. What am I missing?

The fix I am proposing is...

	state->xsave.header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT |
				       xfeatures_mask;
maintainer
#8 lore raw

Re: [tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

Quoted 19 lines · Yu-cheng Yu
Quoted 18 lines · Dave Hansen

The code is:

Quoted 15 lines

void fpstate_init(union fpregs_state *state)

{
        if (!static_cpu_has(X86_FEATURE_FPU)) {
                fpstate_init_soft(&state->soft);
                return;
        }

        memset(state, 0, fpu_kernel_xstate_size);

        /*
         * XRSTORS requires that this bit is set in xcomp_bv, or
         * it will #GP. Make sure it is replaced after the memset().
         */
        if (static_cpu_has(X86_FEATURE_XSAVES))
                state->xsave.header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT;
Quoted 6 lines
Quoted 1 line

That seems to set it unconditionally. What am I missing?

The fix I am proposing is...

	state->xsave.header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT |
				       xfeatures_mask;

Ahh, that makes sense. That does indeed look like a bug in fpstate_init().

author
#9 lore raw

Re: [tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

In reply to Yu-cheng Yu
Quoted 19 lines · Yu-cheng Yu
Quoted 18 lines · Dave Hansen

The code is:

Quoted 15 lines

void fpstate_init(union fpregs_state *state)

{
        if (!static_cpu_has(X86_FEATURE_FPU)) {
                fpstate_init_soft(&state->soft);
                return;
        }

        memset(state, 0, fpu_kernel_xstate_size);

        /*
         * XRSTORS requires that this bit is set in xcomp_bv, or
         * it will #GP. Make sure it is replaced after the memset().
         */
        if (static_cpu_has(X86_FEATURE_XSAVES))
                state->xsave.header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT;
Quoted 6 lines
Quoted 1 line

That seems to set it unconditionally. What am I missing?

The fix I am proposing is...

	state->xsave.header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT |
				       xfeatures_mask;

Actually I thought about this change before I made this patch, but I don't this is the right fix. It is always error prone to init the xcomp_bv to all the supported feature. In case like copyin_to_xsaves(), it is possible that the features which should be set in xcomp_bv do not equal to all the supported features. Please see the following codes in copyin_to_xsaves():

	/*
	 * The state that came in from userspace was user-state only.
	 * Mask all the user states out of 'xfeatures':
	 */
	xsave->header.xfeatures &= XFEATURE_MASK_SUPERVISOR;

	/*
	 * Add back in the features that came in from userspace:
	 */
	xsave->header.xfeatures |= xfeatures;

Thanks,
Kevin

maintainer
#10 lore raw

Re: [tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

Quoted 10 lines
Quoted 4 lines

The fix I am proposing is...

	state->xsave.header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT |
				       xfeatures_mask;

Actually I thought about this change before I made this patch, but I don't this is the right fix. It is always error prone to init the xcomp_bv to all the supported feature. In case like copyin_to_xsaves(), it is possible that the features which should be set in xcomp_bv do not equal to all the supported features. Please see the following codes in copyin_to_xsaves():

Quoted 10 lines
	/*
	 * The state that came in from userspace was user-state only.
	 * Mask all the user states out of 'xfeatures':
	 */
	xsave->header.xfeatures &= XFEATURE_MASK_SUPERVISOR;

	/*
	 * Add back in the features that came in from userspace:
	 */
	xsave->header.xfeatures |= xfeatures;

Hi Kevin,

I think you may be confusing 'xfeatures' with 'xcomp_bv'. xfeatures tells you what features are present in the buffer while xcomp_bv tells you what *format* the buffer is in.

Userspace never dictates the *format* of the kernel buffer, only the contents of each state. So, it makes sense that the copyin code would not (and should not) modify xcomp_bv.

We ensure that xcomp_bv has all supported states set all the time, or we're *supposed* to.

author
#11 lore raw

Re: [tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

Quoted 21 lines · Dave Hansen
Quoted 10 lines
Quoted 4 lines

The fix I am proposing is...

	state->xsave.header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT |
				       xfeatures_mask;

Actually I thought about this change before I made this patch, but I don't this is the right fix. It is always error prone to init the xcomp_bv to all the supported feature. In case like copyin_to_xsaves(), it is possible that the features which should be set in xcomp_bv do not equal to all the supported features. Please see the following codes in copyin_to_xsaves():

Quoted 10 lines
	/*
	 * The state that came in from userspace was user-state only.
	 * Mask all the user states out of 'xfeatures':
	 */
	xsave->header.xfeatures &= XFEATURE_MASK_SUPERVISOR;

	/*
	 * Add back in the features that came in from userspace:
	 */
	xsave->header.xfeatures |= xfeatures;
Quoted 5 lines

Hi Kevin,

I think you may be confusing 'xfeatures' with 'xcomp_bv'. xfeatures tells you what features are present in the buffer while xcomp_bv tells you what *format* the buffer is in.

According to the ISA manual, XSAVES also set the XCOMP_BV[62:0]. My code only
try to be compatible with what the cpu does when excuting XSAVES. The following
is quoted from 325462-sdm-vol-1-2abcd-3abcd.pdf.
The XSAVES instructions sets bit 63 of the XCOMP_BV field of the XSAVE header while writing RFBM[62:0] to XCOMP_BV[62:0]. The XSAVES instruction does not write any part of the XSAVE header other than the XSTATE_BV and XCOMP_BV fields.

Thanks,
Kevin

Quoted 7 lines

Userspace never dictates the *format* of the kernel buffer, only the contents of each state. So, it makes sense that the copyin code would not (and should not) modify xcomp_bv.

We ensure that xcomp_bv has all supported states set all the time, or we're *supposed* to.

maintainer
#12 lore raw

Re: [tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

Quoted 6 lines · Kevin Hao

According to the ISA manual, XSAVES also set the XCOMP_BV[62:0]. My code only
try to be compatible with what the cpu does when excuting XSAVES. The following
is quoted from 325462-sdm-vol-1-2abcd-3abcd.pdf.
The XSAVES instructions sets bit 63 of the XCOMP_BV field of the XSAVE header while writing RFBM[62:0] to XCOMP_BV[62:0]. The XSAVES instruction does not write any part of the XSAVE header other than the XSTATE_BV and XCOMP_BV fields.

What purpose does it serve to make copyin_to_xsaves() set that bit, other than helping to hide bugs?

author
#13 lore raw

Re: [tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

Quoted 9 lines · Dave Hansen
Quoted 6 lines · Kevin Hao

According to the ISA manual, XSAVES also set the XCOMP_BV[62:0]. My code only
try to be compatible with what the cpu does when excuting XSAVES. The following
is quoted from 325462-sdm-vol-1-2abcd-3abcd.pdf.
The XSAVES instructions sets bit 63 of the XCOMP_BV field of the XSAVE header while writing RFBM[62:0] to XCOMP_BV[62:0]. The XSAVES instruction does not write any part of the XSAVE header other than the XSTATE_BV and XCOMP_BV fields.

What purpose does it serve to make copyin_to_xsaves() set that bit,

We try to fake up a memory area which is supposed to be composed by XSAVES instruction. My code is just trying to do what the XSAVES do.

Quoted 1 line

other than helping to hide bugs?

Why do you think it hide the bug? In contrast, I think my patch fixes what the bug really is. The memory area we fake up is bug, we should fix it there.

Thanks,
Kevin

maintainer
#14 lore raw

Re: [tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

Quoted 11 lines · Kevin Hao
Quoted 8 lines · Dave Hansen
Quoted 6 lines · Kevin Hao

According to the ISA manual, XSAVES also set the XCOMP_BV[62:0]. My code only
try to be compatible with what the cpu does when excuting XSAVES. The following
is quoted from 325462-sdm-vol-1-2abcd-3abcd.pdf.
The XSAVES instructions sets bit 63 of the XCOMP_BV field of the XSAVE header while writing RFBM[62:0] to XCOMP_BV[62:0]. The XSAVES instruction does not write any part of the XSAVE header other than the XSTATE_BV and XCOMP_BV fields.

What purpose does it serve to make copyin_to_xsaves() set that bit,

We try to fake up a memory area which is supposed to be composed by XSAVES instruction. My code is just trying to do what the XSAVES do.

No. copyin_to_xsaves() copies data into an *existing* XSAVES-formatted buffer. If you want to change what it does, fine, but that's not what it does or tries to do today.

Quoted 3 lines
Quoted 1 line

other than helping to hide bugs?

Why do you think it hide the bug? In contrast, I think my patch fixes what the bug really is. The memory area we fake up is bug, we should fix it there.

Yu-cheng found the bug. That bug will probably manifest in other code paths than copyin_to_xsaves(). If we did your patch, it would hide the bug in those other code paths.

author
#15 lore raw

Re: [tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

Quoted 9 lines · Dave Hansen
Quoted 8 lines · Kevin Hao
Quoted 7 lines · Dave Hansen
Quoted 6 lines · Kevin Hao

According to the ISA manual, XSAVES also set the XCOMP_BV[62:0]. My code only
try to be compatible with what the cpu does when excuting XSAVES. The following
is quoted from 325462-sdm-vol-1-2abcd-3abcd.pdf.
The XSAVES instructions sets bit 63 of the XCOMP_BV field of the XSAVE header while writing RFBM[62:0] to XCOMP_BV[62:0]. The XSAVES instruction does not write any part of the XSAVE header other than the XSTATE_BV and XCOMP_BV fields.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

What purpose does it serve to make copyin_to_xsaves() set that bit,

We try to fake up a memory area which is supposed to be composed by XSAVES instruction. My code is just trying to do what the XSAVES do.

No. copyin_to_xsaves() copies data into an *existing* XSAVES-formatted buffer. If you want to change what it does, fine, but that's not what it does or tries to do today.

No, I didn't change what the function copyin_to_xsaves() does. I just tried to fix the bug in it.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

other than helping to hide bugs?

Why do you think it hide the bug? In contrast, I think my patch fixes what the bug really is. The memory area we fake up is bug, we should fix it there.

Yu-cheng found the bug. That bug will probably manifest in other code paths than copyin_to_xsaves(). If we did your patch, it would hide the bug in those other code paths.

The XCOMP_BV[62:0] is supposed to be updated by XSAVEC/XSAVES instructions. It should not be touched by the software in theory except some special cases like what we do in copyin_to_xsaves(). Trying to init the XCOMP_BV[62:0] to some assuming values in fpstate_init() should be error prone and just paper over the real bug.

Thanks,
Kevin

#16 lore raw

Re: [tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

In reply to Kevin Hao
Quoted 4 lines · Kevin Hao
Quoted 1 line

other than helping to hide bugs?

Why do you think it hide the bug? In contrast, I think my patch fixes what the bug really is. The memory area we fake up is bug, we should fix it there.

The intention is to have a single FPU format set at bootup and xcomp_bv is essentially an invariant (constant) inherited by all tasks from early boot. In that sense setting xsave.header.xcomp_bv in copyin_to_xsaves() is misplaced.

So I combined the two commits: I kept your original fix but applied Yu-cheng Yu's patch that moves the initialization from copyin_to_xsaves() to fpstate_init().

Thanks,

Ingo

#17 lore raw

[tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

In reply to Kevin Hao
x86/fpu: Set the xcomp_bv when we fake up a XSAVES area
Commit-ID:  4c833368f0bf748d4147bf301b1f95bc8eccb3c0
Gitweb:     http://git.kernel.org/tip/4c833368f0bf748d4147bf301b1f95bc8eccb3c0
Author:     Kevin Hao <[email protected]>
AuthorDate: Sun, 22 Jan 2017 16:50:23 +0800
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Mon, 23 Jan 2017 10:40:18 +0100

x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

I got the following calltrace on a Apollo Lake SoC with 32-bit kernel:

  WARNING: CPU: 2 PID: 261 at arch/x86/include/asm/fpu/internal.h:363 fpu__restore+0x1f5/0x260
  [...]
  Hardware name: Intel Corp. Broxton P/NOTEBOOK, BIOS APLIRVPA.X64.0138.B35.1608091058 08/09/2016
  Call Trace:
   dump_stack()
   __warn()
   ? fpu__restore()
   warn_slowpath_null()
   fpu__restore()
   __fpu__restore_sig()
   fpu__restore_sig()
   restore_sigcontext.isra.9()
   sys_sigreturn()
   do_int80_syscall_32()
   entry_INT80_32()

The reason is that a #GP occurs when executing XRSTORS. The root cause is that we forget to set the xcomp_bv when we fake up the XSAVES area in the copyin_to_xsaves() function.

Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc Fenghua Yu <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Josh Poimboeuf <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Quentin Casasnovas <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Yu-cheng Yu <[email protected]>
---
 arch/x86/kernel/fpu/xstate.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kernel/fpu/xstate.c +1 -0
index 1d77704..e287b90 100644
@@ -1070,6 +1070,7 @@ int copyin_to_xsaves(const void *kbuf, const void __user *ubuf,
 	 * Add back in the features that came in from userspace:
 	 */
 	xsave->header.xfeatures |= xfeatures;
+	xsave->header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT | xsave->header.xfeatures;
 
 	return 0;
 }
maintainer
#18 lore raw

Re: [tip:x86/urgent] x86/fpu: Set the xcomp_bv when we fake up a XSAVES area

arch/x86/kernel/fpu/xstate.c int copyin_to_xsaves(const void *kbuf, const void __user *ubuf, · line 1073

Quoted 9 lines · tip-bot for Kevin Hao
arch/x86/kernel/fpu/xstate.c +1 -0
index 1d77704..e287b90 100644
@@ -1070,6 +1070,7 @@ int copyin_to_xsaves(const void *kbuf, const void __user *ubuf,
 	 * Add back in the features that came in from userspace:
 	 */
 	xsave->header.xfeatures |= xfeatures;
+	xsave->header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT | xsave->header.xfeatures;

FYI, this commit bit me today. If userspace happens to have bits clear in the 'xfeatures' field, this will *CLEAR* bits in xcomp_bv, changing the format of the XSAVE buffer, and breaking anything that looks at the buffer that doesn't use the instructions.

Yu-cheng's dffba9a31c commit removed this line and fixed it up, but this might bite someone who is bisecting.