[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Xen-devel] [PATCHv3] x86, fpu: remove the logic of non-eager fpu mem allocation at the first usage

On Tue, Sep 2, 2014 at 12:06 PM, David Vrabel <david.vrabel@xxxxxxxxxx> wrote:
From: Suresh Siddha <sbsiddha@xxxxxxxxx>

For non-eager fpu mode, thread's fpu state is allocated during the first
fpu usage (in the context of device not available exception). This can be
a blocking call and hence we enable interrupts (which were originally
disabled when the exception happened), allocate memory and disable
interrupts etc. While this saves 512 bytes or so per-thread, there
are some issues in general.

a. Most of the future cases will be anyway using eager
FPU (because of processor features like xsaveopt, LWP, MPX etc) and
they do the allocation at the thread creation itself. Nice to have
one common mechanism as all the state save/restore code is
shared. Avoids the confusion and minimizes the subtle bugs
in the core piece involved with context-switch.

b. If a parent thread uses FPU, during fork() we allocate
the FPU state in the child and copy the state etc. Shortly after this,
during exec() we free it up, so that we can later allocate during
the first usage of FPU. So this free/allocate might be slower
for some workloads.

c. math_state_restore() is called from multiple places
and it is error pone if the caller expects interrupts to be disabled
throughout the execution of math_state_restore(). Can lead to subtle
bugs like Ubuntu bug #1265841.

Memory savings will be small anyways and the code complexity
introducing subtle bugs is not worth it. So remove
the logic of non-eager fpu mem allocation at the first usage.


Signed-off-by: Suresh Siddha <sbsiddha@xxxxxxxxx>
Signed-off-by: David Vrabel <david.vrabel@xxxxxxxxxx>
- Rebase on 3.17-rc3.

- Tweak condition for allocating the first thread's FPU state.
Âarch/x86/kernel/i387.c  | Â20 +++++++++++---------
Âarch/x86/kernel/process.c |Â Â 6 ------
Âarch/x86/kernel/traps.c Â| Â16 ++--------------
Âarch/x86/kernel/xsave.c Â|  2 --
Â4 files changed, 13 insertions(+), 31 deletions(-)

diff --git a/arch/x86/kernel/i387.c b/arch/x86/kernel/i387.c
index a9a4229..956ea86 100644
--- a/arch/x86/kernel/i387.c
+++ b/arch/x86/kernel/i387.c
@@ -5,6 +5,7 @@
 * General FPU state handling cleanups
 *  ÂGareth Hughes <gareth@xxxxxxxxxxx>, May 2000
+#include <linux/bootmem.h>
Â#include <linux/module.h>
Â#include <linux/regset.h>
Â#include <linux/sched.h>
@@ -197,6 +198,16 @@ void fpu_init(void)

+Â Â Â Â/*
+Â Â Â Â * Now we know the final size of the xsave data, allocate the
+Â Â Â Â * FPU state area for the first task. All other tasks have
+Â Â Â Â * this allocated in arch_dup_task_struct().
+Â Â Â Â */
+Â Â Â Âif (!current->thread.fpu.state)
+Â Â Â Â Â Â Â Âcurrent->thread.fpu.state = alloc_bootmem_align(xstate_size,
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â__alignof__(struct xsave_struct));

@@ -228,8 +239,6 @@ EXPORT_SYMBOL_GPL(fpu_finit);
Âint init_fpu(struct task_struct *tsk)
-Â Â Â Âint ret;
    if (tsk_used_math(tsk)) {
        if (cpu_has_fpu && tsk == current)
@@ -237,13 +246,6 @@ int init_fpu(struct task_struct *tsk)
        return 0;

-Â Â Â Â/*
-Â Â Â Â * Memory allocation at the first usage of the FPU and other state.
-Â Â Â Â */
-Â Â Â Âret = fpu_alloc(&tsk->thread.fpu);
-Â Â Â Âif (ret)
-Â Â Â Â Â Â Â Âreturn ret;

diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index f804dc9..4137f81 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -129,12 +129,6 @@ void flush_thread(void)
    memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
-Â Â Â Â/*
-Â Â Â Â * Free the FPU state for non xsave platforms. They get reallocated
-Â Â Â Â * lazily at the first use.
-Â Â Â Â */
-Â Â Â Âif (!use_eager_fpu())
-Â Â Â Â Â Â Â Âfree_thread_xstate(tsk);

Âstatic void hard_disable_TSC(void)
diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c
index 0d0e922..36fc898 100644
--- a/arch/x86/kernel/traps.c
+++ b/arch/x86/kernel/traps.c
@@ -652,20 +652,8 @@ void math_state_restore(void)
    struct task_struct *tsk = current;

-Â Â Â Âif (!tsk_used_math(tsk)) {
-Â Â Â Â Â Â Â Âlocal_irq_enable();
-Â Â Â Â Â Â Â Â/*
-Â Â Â Â Â Â Â Â * does a slab alloc which can sleep
-Â Â Â Â Â Â Â Â */
-Â Â Â Â Â Â Â Âif (init_fpu(tsk)) {
-Â Â Â Â Â Â Â Â Â Â Â Â/*
-Â Â Â Â Â Â Â Â Â Â Â Â * ran out of memory!
-Â Â Â Â Â Â Â Â Â Â Â Â */
-Â Â Â Â Â Â Â Â Â Â Â Âdo_group_exit(SIGKILL);
-Â Â Â Â Â Â Â Â Â Â Â Âreturn;
-Â Â Â Â Â Â Â Â}
-Â Â Â Â Â Â Â Âlocal_irq_disable();
-Â Â Â Â}
+Â Â Â Âif (!tsk_used_math(tsk))
+Â Â Â Â Â Â Â Âinit_fpu(tsk);


diff --git a/arch/x86/kernel/xsave.c b/arch/x86/kernel/xsave.c
index 940b142..eed95d6 100644
--- a/arch/x86/kernel/xsave.c
+++ b/arch/x86/kernel/xsave.c
@@ -677,8 +677,6 @@ void xsave_init(void)

Âstatic inline void __init eager_fpu_init_bp(void)
-Â Â Â Âcurrent->thread.fpu.state =
-Â Â Â Â Â Âalloc_bootmem_align(xstate_size, __alignof__(struct xsave_struct));
    if (!init_xstate_buf)

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Xen-devel mailing list



Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.