Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758566AbcJQUKp (ORCPT ); Mon, 17 Oct 2016 16:10:45 -0400 Received: from shelob.surriel.com ([74.92.59.67]:60231 "EHLO shelob.surriel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755852AbcJQUJv (ORCPT ); Mon, 17 Oct 2016 16:09:51 -0400 From: riel@redhat.com To: linux-kernel@vger.kernel.org Cc: mingo@kernel.org, bp@alien8.de, torvalds@linux-foundation.org, luto@kernel.org, dave.hansen@intel.linux.com, tglx@linutronix.de, hpa@zytor.com Subject: [PATCH RFC 1/3] fpu/x86: add make_fpregs_active(_newstate) helper functions Date: Mon, 17 Oct 2016 16:09:42 -0400 Message-Id: <1476734984-13839-2-git-send-email-riel@redhat.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1476734984-13839-1-git-send-email-riel@redhat.com> References: <1476734984-13839-1-git-send-email-riel@redhat.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3209 Lines: 113 From: Rik van Riel Add helper functions that ensure a task's floating point registers are set up the way they need to be - either with the task's floating point state loaded in, or ready to accept a task's new floating point state. These helpers can be called from code that accesses the floating point state from a preemptible state, in preparation for the lazier floating point loading code, using loops like this: do { make_fpregs_active(); ... } while (unlikely(!fpregs_active())); This way a task can safely do things like saving the floating point state of a task to user space memory (the signal handling code does this), without requiring that the floating point state is restored at every context switch. If the floating point registers are still active when leaving the loop, the floating point state has ended up in its destination (registers or memory) in one piece, and will be saved at the next context switch. When the floating point state is already present, these functions do nothing. Signed-off-by: Rik van Riel --- arch/x86/include/asm/fpu/internal.h | 53 +++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/fpu/internal.h b/arch/x86/include/asm/fpu/internal.h index 621ba3bfa2a7..d40deb337807 100644 --- a/arch/x86/include/asm/fpu/internal.h +++ b/arch/x86/include/asm/fpu/internal.h @@ -473,6 +473,14 @@ static inline void copy_kernel_to_fpregs(union fpregs_state *fpstate) extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fp, int size); /* + * Is this thread using floating point state. + */ +static inline int fpstate_active(void) +{ + return current->thread.fpu.fpstate_active; +} + +/* * FPU context switch related helper methods: */ @@ -548,6 +556,44 @@ static inline int fpregs_active(void) } /* + * Ensure the floating point registers are ready for this task, and + * contain the task's floating point state. + * + * The loadnew variant can be used when loading new floating point + * state, and the old floating point register state does not matter. + */ +static inline void __make_fpregs_active(struct fpu *fpu, int cpu) +{ + if (!fpregs_state_valid(fpu, cpu)) + copy_kernel_to_fpregs(&fpu->state); + fpregs_activate(fpu); +} + +static inline void make_fpregs_active(void) +{ + struct fpu *fpu = ¤t->thread.fpu; + + if (fpregs_active()) + return; + + preempt_disable(); + __make_fpregs_active(fpu, raw_smp_processor_id()); + preempt_enable(); +} + +static inline void make_fpregs_active_loadnew(void) +{ + struct fpu *fpu = ¤t->thread.fpu; + + if (fpregs_active()) + return; + + preempt_disable(); + fpregs_activate(fpu); + preempt_enable(); +} + +/* * FPU state switching for scheduling. * * This is a two-stage process: @@ -587,11 +633,8 @@ static inline void switch_fpu_finish(struct fpu *new_fpu, int cpu) bool preload = static_cpu_has(X86_FEATURE_FPU) && new_fpu->fpstate_active; - if (preload) { - if (!fpregs_state_valid(new_fpu, cpu)) - copy_kernel_to_fpregs(&new_fpu->state); - fpregs_activate(new_fpu); - } + if (preload) + __make_fpregs_active(new_fpu, cpu); } /* -- 2.7.4