Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1398587 > unrolled thread

[PATCH v6 01/13] x86/xsaves: Define and use fpu_user_xstate_size

Started byYu-cheng Yu <yu-cheng.yu@intel.com>
First post2016-05-11 01:40 +0200
Last post2016-05-12 08:40 +0200
Articles 5 — 4 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH v6 01/13] x86/xsaves: Define and use fpu_user_xstate_size Yu-cheng Yu <yu-cheng.yu@intel.com> - 2016-05-11 01:40 +0200
    Re: [PATCH v6 01/13] x86/xsaves: Define and use fpu_user_xstate_size Borislav Petkov <bp@suse.de> - 2016-05-11 19:30 +0200
      RE: [PATCH v6 01/13] x86/xsaves: Define and use fpu_user_xstate_size "Yu, Fenghua" <fenghua.yu@intel.com> - 2016-05-11 19:30 +0200
        Re: [PATCH v6 01/13] x86/xsaves: Define and use fpu_user_xstate_size Borislav Petkov <bp@suse.de> - 2016-05-11 19:40 +0200
        Re: [PATCH v6 01/13] x86/xsaves: Define and use fpu_user_xstate_size Ingo Molnar <mingo@kernel.org> - 2016-05-12 08:40 +0200

#1398587 — [PATCH v6 01/13] x86/xsaves: Define and use fpu_user_xstate_size

FromYu-cheng Yu <yu-cheng.yu@intel.com>
Date2016-05-11 01:40 +0200
Subject[PATCH v6 01/13] x86/xsaves: Define and use fpu_user_xstate_size
Message-ID<rxppV-4nc-33@gated-at.bofh.it>
The XSAVE area of kernel can be in standard or compacted format;
it is always in standard format for user mode. When XSAVES is
enabled, the kernel uses the compacted format and it is necessary
to use a separate fpu_user_xstate_size for signal/ptrace frames.

Based on an earlier patch from Fenghua Yu <fenghua.yu@intel.com>

Signed-off-by: Fenghua Yu <fenghua.yu@intel.com>
[yu-cheng.yu@intel.com: rebase to current, rename to fpu_user_xstate_size]
Signed-off-by: Yu-cheng Yu <yu-cheng.yu@intel.com>
Reviewed-by: Dave Hansen <dave.hansen@intel.com>
---
 arch/x86/include/asm/fpu/xstate.h |  1 -
 arch/x86/include/asm/processor.h  |  1 +
 arch/x86/kernel/fpu/init.c        |  5 ++-
 arch/x86/kernel/fpu/signal.c      | 27 ++++++++++----
 arch/x86/kernel/fpu/xstate.c      | 76 ++++++++++++++++++++++++---------------
 5 files changed, 73 insertions(+), 37 deletions(-)

diff --git a/arch/x86/include/asm/fpu/xstate.h b/arch/x86/include/asm/fpu/xstate.h
index 38951b0..16df2c4 100644
--- a/arch/x86/include/asm/fpu/xstate.h
+++ b/arch/x86/include/asm/fpu/xstate.h
@@ -39,7 +39,6 @@
 #define REX_PREFIX
 #endif
 
-extern unsigned int xstate_size;
 extern u64 xfeatures_mask;
 extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
 
diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h
index 9264476..8d5df3f 100644
--- a/arch/x86/include/asm/processor.h
+++ b/arch/x86/include/asm/processor.h
@@ -368,6 +368,7 @@ DECLARE_PER_CPU(struct irq_stack *, softirq_stack);
 #endif	/* X86_64 */
 
 extern unsigned int xstate_size;
+extern unsigned int fpu_user_xstate_size;
 
 struct perf_event;
 
diff --git a/arch/x86/kernel/fpu/init.c b/arch/x86/kernel/fpu/init.c
index 54c86ff..824d422 100644
--- a/arch/x86/kernel/fpu/init.c
+++ b/arch/x86/kernel/fpu/init.c
@@ -195,7 +195,7 @@ static void __init fpu__init_task_struct_size(void)
 }
 
 /*
- * Set up the xstate_size based on the legacy FPU context size.
+ * Set up the user and kernel xstate_size based on the legacy FPU context size.
  *
  * We set this up first, and later it will be overwritten by
  * fpu__init_system_xstate() if the CPU knows about xstates.
@@ -226,6 +226,9 @@ static void __init fpu__init_system_xstate_size_legacy(void)
 		else
 			xstate_size = sizeof(struct fregs_state);
 	}
+
+	fpu_user_xstate_size = xstate_size;
+
 	/*
 	 * Quirk: we don't yet handle the XSAVES* instructions
 	 * correctly, as we don't correctly convert between
diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c
index 31c6a60..23572a9 100644
--- a/arch/x86/kernel/fpu/signal.c
+++ b/arch/x86/kernel/fpu/signal.c
@@ -31,7 +31,7 @@ static inline int check_for_xstate(struct fxregs_state __user *buf,
 	/* Check for the first magic field and other error scenarios. */
 	if (fx_sw->magic1 != FP_XSTATE_MAGIC1 ||
 	    fx_sw->xstate_size < min_xstate_size ||
-	    fx_sw->xstate_size > xstate_size ||
+	    fx_sw->xstate_size > fpu_user_xstate_size ||
 	    fx_sw->xstate_size > fx_sw->extended_size)
 		return -1;
 
@@ -88,7 +88,8 @@ static inline int save_xstate_epilog(void __user *buf, int ia32_frame)
 	if (!use_xsave())
 		return err;
 
-	err |= __put_user(FP_XSTATE_MAGIC2, (__u32 *)(buf + xstate_size));
+	err |= __put_user(FP_XSTATE_MAGIC2,
+			  (__u32 *)(buf + fpu_user_xstate_size));
 
 	/*
 	 * Read the xfeatures which we copied (directly from the cpu or
@@ -125,7 +126,7 @@ static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf)
 	else
 		err = copy_fregs_to_user((struct fregs_state __user *) buf);
 
-	if (unlikely(err) && __clear_user(buf, xstate_size))
+	if (unlikely(err) && __clear_user(buf, fpu_user_xstate_size))
 		err = -EFAULT;
 	return err;
 }
@@ -175,8 +176,19 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
 		if (ia32_fxstate)
 			copy_fxregs_to_kernel(&tsk->thread.fpu);
 	} else {
+		/*
+		 * It is a *bug* if kernel uses compacted-format for xsave
+		 * area and we copy it out directly to a signal frame. It
+		 * should have been handled above by saving the registers
+		 * directly.
+		 */
+		if (boot_cpu_has(X86_FEATURE_XSAVES)) {
+			WARN_ONCE(1, "x86/fpu: saving compacted-format xsave area to a signal frame!\n");
+			return -1;
+		}
+
 		fpstate_sanitize_xstate(&tsk->thread.fpu);
-		if (__copy_to_user(buf_fx, xsave, xstate_size))
+		if (__copy_to_user(buf_fx, xsave, fpu_user_xstate_size))
 			return -1;
 	}
 
@@ -341,7 +353,8 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size)
 
 static inline int xstate_sigframe_size(void)
 {
-	return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
+	return use_xsave() ? fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE :
+			fpu_user_xstate_size;
 }
 
 /*
@@ -385,12 +398,12 @@ fpu__alloc_mathframe(unsigned long sp, int ia32_frame,
  */
 void fpu__init_prepare_fx_sw_frame(void)
 {
-	int size = xstate_size + FP_XSTATE_MAGIC2_SIZE;
+	int size = fpu_user_xstate_size + FP_XSTATE_MAGIC2_SIZE;
 
 	fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
 	fx_sw_reserved.extended_size = size;
 	fx_sw_reserved.xfeatures = xfeatures_mask;
-	fx_sw_reserved.xstate_size = xstate_size;
+	fx_sw_reserved.xstate_size = fpu_user_xstate_size;
 
 	if (config_enabled(CONFIG_IA32_EMULATION) ||
 	    config_enabled(CONFIG_X86_32)) {
diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c
index b48ef35..dfac87d 100644
--- a/arch/x86/kernel/fpu/xstate.c
+++ b/arch/x86/kernel/fpu/xstate.c
@@ -44,6 +44,13 @@ static unsigned int xstate_sizes[XFEATURE_MAX]   = { [ 0 ... XFEATURE_MAX - 1] =
 static unsigned int xstate_comp_offsets[sizeof(xfeatures_mask)*8];
 
 /*
+ * The XSAVE area of kernel can be in standard or compacted format;
+ * it is always in standard format for user mode. This is the user
+ * mode standard format size used for signal and ptrace frames.
+ */
+unsigned int fpu_user_xstate_size;
+
+/*
  * Clear all of the X86_FEATURE_* bits that are unavailable
  * when the CPU has no XSAVE support.
  */
@@ -171,7 +178,7 @@ void fpstate_sanitize_xstate(struct fpu *fpu)
 	 */
 	while (xfeatures) {
 		if (xfeatures & 0x1) {
-			int offset = xstate_offsets[feature_bit];
+			int offset = xstate_comp_offsets[feature_bit];
 			int size = xstate_sizes[feature_bit];
 
 			memcpy((void *)fx + offset,
@@ -533,8 +540,9 @@ static void do_extra_xstate_size_checks(void)
 	XSTATE_WARN_ON(paranoid_xstate_size != xstate_size);
 }
 
+
 /*
- * Calculate total size of enabled xstates in XCR0/xfeatures_mask.
+ * Get total size of enabled xstates in XCR0/xfeatures_mask.
  *
  * Note the SDM's wording here.  "sub-function 0" only enumerates
  * the size of the *user* states.  If we use it to size a buffer
@@ -544,34 +552,33 @@ static void do_extra_xstate_size_checks(void)
  * Note that we do not currently set any bits on IA32_XSS so
  * 'XCR0 | IA32_XSS == XCR0' for now.
  */
-static unsigned int __init calculate_xstate_size(void)
+static unsigned int __init get_xsaves_size(void)
 {
 	unsigned int eax, ebx, ecx, edx;
-	unsigned int calculated_xstate_size;
+	/*
+	 * - CPUID function 0DH, sub-function 1:
+	 *    EBX enumerates the size (in bytes) required by
+	 *    the XSAVES instruction for an XSAVE area
+	 *    containing all the state components
+	 *    corresponding to bits currently set in
+	 *    XCR0 | IA32_XSS.
+	 */
+	cpuid_count(XSTATE_CPUID, 1, &eax, &ebx, &ecx, &edx);
+	return ebx;
+}
 
-	if (!cpu_has_xsaves) {
-		/*
-		 * - CPUID function 0DH, sub-function 0:
-		 *    EBX enumerates the size (in bytes) required by
-		 *    the XSAVE instruction for an XSAVE area
-		 *    containing all the *user* state components
-		 *    corresponding to bits currently set in XCR0.
-		 */
-		cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
-		calculated_xstate_size = ebx;
-	} else {
-		/*
-		 * - CPUID function 0DH, sub-function 1:
-		 *    EBX enumerates the size (in bytes) required by
-		 *    the XSAVES instruction for an XSAVE area
-		 *    containing all the state components
-		 *    corresponding to bits currently set in
-		 *    XCR0 | IA32_XSS.
-		 */
-		cpuid_count(XSTATE_CPUID, 1, &eax, &ebx, &ecx, &edx);
-		calculated_xstate_size = ebx;
-	}
-	return calculated_xstate_size;
+static unsigned int __init get_xsave_size(void)
+{
+	unsigned int eax, ebx, ecx, edx;
+	/*
+	 * - CPUID function 0DH, sub-function 0:
+	 *    EBX enumerates the size (in bytes) required by
+	 *    the XSAVE instruction for an XSAVE area
+	 *    containing all the *user* state components
+	 *    corresponding to bits currently set in XCR0.
+	 */
+	cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
+	return ebx;
 }
 
 /*
@@ -591,7 +598,15 @@ static bool is_supported_xstate_size(unsigned int test_xstate_size)
 static int init_xstate_size(void)
 {
 	/* Recompute the context size for enabled features: */
-	unsigned int possible_xstate_size = calculate_xstate_size();
+	unsigned int possible_xstate_size;
+	unsigned int xsave_size;
+
+	xsave_size = get_xsave_size();
+
+	if (cpu_has_xsaves)
+		possible_xstate_size = get_xsaves_size();
+	else
+		possible_xstate_size = xsave_size;
 
 	/* Ensure we have the space to store all enabled: */
 	if (!is_supported_xstate_size(possible_xstate_size))
@@ -603,6 +618,11 @@ static int init_xstate_size(void)
 	 */
 	xstate_size = possible_xstate_size;
 	do_extra_xstate_size_checks();
+
+	/*
+	 * User space is always in standard format.
+	 */
+	fpu_user_xstate_size = xsave_size;
 	return 0;
 }
 
-- 
1.9.1

[toc] | [next] | [standalone]


#1399379

FromBorislav Petkov <bp@suse.de>
Date2016-05-11 19:30 +0200
Message-ID<rxG7o-4gN-9@gated-at.bofh.it>
In reply to#1398587
On Tue, May 10, 2016 at 04:29:53PM -0700, Yu-cheng Yu wrote:
> The XSAVE area of kernel can be in standard or compacted format;

"The kernel xstate area... "

and can we call it the xstate area as there are a bunch of XSAVE*
insns touching it. The file which deals with it is even called that:
arch/x86/kernel/fpu/xstate.c

> it is always in standard format for user mode. When XSAVES is
> enabled, the kernel uses the compacted format and it is necessary
> to use a separate fpu_user_xstate_size for signal/ptrace frames.
> 
> Based on an earlier patch from Fenghua Yu <fenghua.yu@intel.com>
> 
> Signed-off-by: Fenghua Yu <fenghua.yu@intel.com>
> [yu-cheng.yu@intel.com: rebase to current, rename to fpu_user_xstate_size]
> Signed-off-by: Yu-cheng Yu <yu-cheng.yu@intel.com>
> Reviewed-by: Dave Hansen <dave.hansen@intel.com>

Maybe I wasn't as clear as I hoped to be. Let me be more specific:

So you either need to do:

---
From: Fenghua

...

Signed-off-by: Fenghua
Signed-off-by: You
...
---

or

---

Based on an earlier patch from Fenghua Yu <fenghua.yu@intel.com>.

Signed-off-by: You

---

with the second variant making you the author implicitly because you're
the sender.

Makes more sense this way?

> ---
>  arch/x86/include/asm/fpu/xstate.h |  1 -
>  arch/x86/include/asm/processor.h  |  1 +
>  arch/x86/kernel/fpu/init.c        |  5 ++-
>  arch/x86/kernel/fpu/signal.c      | 27 ++++++++++----
>  arch/x86/kernel/fpu/xstate.c      | 76 ++++++++++++++++++++++++---------------
>  5 files changed, 73 insertions(+), 37 deletions(-)

...

> diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c
> index b48ef35..dfac87d 100644
> --- a/arch/x86/kernel/fpu/xstate.c
> +++ b/arch/x86/kernel/fpu/xstate.c
> @@ -44,6 +44,13 @@ static unsigned int xstate_sizes[XFEATURE_MAX]   = { [ 0 ... XFEATURE_MAX - 1] =
>  static unsigned int xstate_comp_offsets[sizeof(xfeatures_mask)*8];
>  
>  /*
> + * The XSAVE area of kernel can be in standard or compacted format;

"The kernel xstate area... "

> + * it is always in standard format for user mode. This is the user
> + * mode standard format size used for signal and ptrace frames.
> + */

But yes, that's very nice commenting.

> +unsigned int fpu_user_xstate_size;
> +
> +/*
>   * Clear all of the X86_FEATURE_* bits that are unavailable
>   * when the CPU has no XSAVE support.
>   */

...

> @@ -591,7 +598,15 @@ static bool is_supported_xstate_size(unsigned int test_xstate_size)
>  static int init_xstate_size(void)
>  {
>  	/* Recompute the context size for enabled features: */
> -	unsigned int possible_xstate_size = calculate_xstate_size();
> +	unsigned int possible_xstate_size;
> +	unsigned int xsave_size;
> +
> +	xsave_size = get_xsave_size();
> +
> +	if (cpu_has_xsaves)

So next time you submit patches against -tip, please merge them with
tip/master and see if they still build, at least.

During last review I told you to use boot_cpu_has(X86_FEATURE_XSAVES)
because those cpu_has_XXX things are going away and are gone in tip
already.

Please be more careful when incorporating review comments - otherwise it
is waste of both yours and reviewer's time.

Thanks.

-- 
Regards/Gruss,
    Boris.

SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- 

[toc] | [prev] | [next] | [standalone]


#1399381

From"Yu, Fenghua" <fenghua.yu@intel.com>
Date2016-05-11 19:30 +0200
Message-ID<rxG7o-4gN-7@gated-at.bofh.it>
In reply to#1399379
> From: Borislav Petkov [mailto:bp@suse.de]
> Sent: Wednesday, May 11, 2016 10:21 AM
> To: Yu, Yu-cheng <yu-cheng.yu@intel.com>
> Cc: linux-kernel@vger.kernel.org; x86@kernel.org; H. Peter Anvin
> <hpa@zytor.com>; Thomas Gleixner <tglx@linutronix.de>; Ingo Molnar
> <mingo@redhat.com>; Dave Hansen <dave.hansen@linux.intel.com>; Andy
> Lutomirski <luto@kernel.org>; Prakhya, Sai Praneeth
> <sai.praneeth.prakhya@intel.com>; Shankar, Ravi V
> <ravi.v.shankar@intel.com>; Yu, Fenghua <fenghua.yu@intel.com>
> Subject: Re: [PATCH v6 01/13] x86/xsaves: Define and use
> fpu_user_xstate_size
> 
> On Tue, May 10, 2016 at 04:29:53PM -0700, Yu-cheng Yu wrote:
> > The XSAVE area of kernel can be in standard or compacted format;
> 
> "The kernel xstate area... "
> 
> and can we call it the xstate area as there are a bunch of XSAVE* insns
> touching it. The file which deals with it is even called that:
> arch/x86/kernel/fpu/xstate.c
> 
> > it is always in standard format for user mode. When XSAVES is enabled,
> > the kernel uses the compacted format and it is necessary to use a
> > separate fpu_user_xstate_size for signal/ptrace frames.
> >
> > Based on an earlier patch from Fenghua Yu <fenghua.yu@intel.com>
> >
> > Signed-off-by: Fenghua Yu <fenghua.yu@intel.com>
> > [yu-cheng.yu@intel.com: rebase to current, rename to
> > fpu_user_xstate_size]
> > Signed-off-by: Yu-cheng Yu <yu-cheng.yu@intel.com>
> > Reviewed-by: Dave Hansen <dave.hansen@intel.com>
> 
> Maybe I wasn't as clear as I hoped to be. Let me be more specific:
> 
> So you either need to do:
> 
> ---
> From: Fenghua
> 
> ...
> 
> Signed-off-by: Fenghua
> Signed-off-by: You
> ...
> ---
> 
> or
> 
> ---
> 
> Based on an earlier patch from Fenghua Yu <fenghua.yu@intel.com>.
> 
> Signed-off-by: You
> 
> ---
> 
> with the second variant making you the author implicitly because you're the
> sender.
> 
> Makes more sense this way?

Is this possible to have the third one?

From: Yu-cheng

Signed-off-by: Yu-cheng
Signed-off-by: Fenghua

Thanks.

-Fenghua

[toc] | [prev] | [next] | [standalone]


#1399384

FromBorislav Petkov <bp@suse.de>
Date2016-05-11 19:40 +0200
Message-ID<rxGh4-4oy-15@gated-at.bofh.it>
In reply to#1399381
On Wed, May 11, 2016 at 05:25:17PM +0000, Yu, Fenghua wrote:
> Is this possible to have the third one?
> 
> From: Yu-cheng
> 
> Signed-off-by: Yu-cheng
> Signed-off-by: Fenghua

This SOB chain says that Yu-cheng is the author and then you've received
it from him, have done something to it (which we sometimes put in [] to
denote it) and then you've sent it out.

The tip maintainer which applies it will add his SOB too, at the end.

Let me ask you this: what exactly do you want to express with the SOB
chain?

Just a reminder: the SOB chain should give a clear and precise
representation of the patch path from author to upstream.

Section

11) Sign your work
------------------

in Documentation/SubmittingPatches explains the whole story.

-- 
Regards/Gruss,
    Boris.

SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- 

[toc] | [prev] | [next] | [standalone]


#1399687

FromIngo Molnar <mingo@kernel.org>
Date2016-05-12 08:40 +0200
Message-ID<rxSrT-bI-3@gated-at.bofh.it>
In reply to#1399381
* Yu, Fenghua <fenghua.yu@intel.com> wrote:

> Is this possible to have the third one?
> 
> From: Yu-cheng
> 
> Signed-off-by: Yu-cheng
> Signed-off-by: Fenghua

Yeah, that's a valid SOB chain if Yu-cheng wrote much of the patch and you just 
forwarded it along with some fixes/enhancements. As Boris mentioned, you can 
further improve on this via:

> Signed-off-by: Yu-cheng
> [ Fixed bug X, cleaned up Y. ]
> Signed-off-by: Fenghua

to signal that you made changes over Yu-cheng's version.

Thanks,

	Ingo

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web