From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751902AbbCFNlf (ORCPT ); Fri, 6 Mar 2015 08:41:35 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50341 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750810AbbCFNld (ORCPT ); Fri, 6 Mar 2015 08:41:33 -0500 Date: Fri, 6 Mar 2015 14:39:26 +0100 From: Oleg Nesterov To: Ingo Molnar Cc: Dave Hansen , Borislav Petkov , Andy Lutomirski , Linus Torvalds , Pekka Riikonen , Rik van Riel , Suresh Siddha , LKML , "Yu, Fenghua" , Quentin Casasnovas Subject: Re: [PATCH 1/1] x86/fpu: math_state_restore() should not blindly disable irqs Message-ID: <20150306133926.GA22064@redhat.com> References: <54F74F59.5070107@intel.com> <20150305195127.GA12657@redhat.com> <20150305195149.GB12657@redhat.com> <20150305201101.GA21571@gmail.com> <20150305212532.GA16890@redhat.com> <20150306075833.GA623@gmail.com> <20150306132634.GA20693@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20150306132634.GA20693@redhat.com> User-Agent: Mutt/1.5.18 (2008-05-17) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 03/06, Oleg Nesterov wrote: > > OK, but please note that this patch is not beckportable. If you think > that -stable doesn't need this fix, then I agree. > > If the caller is do_device_not_available(), then we can not enable > irqs before __thread_fpu_begin() + restore_fpu_checking(). > > 1. Preemption in between can destroy ->fpu.state initialized by > fpu_finit(), __switch_to() will save the live (wrong) FPU state > again. > > 2. kernel_fpu_begin() from irq right after __thread_fpu_begin() is > not nice too. It will do __save_init_fpu() and this overwrites > ->fpu.state too. > > Starting from v4.0 it does kernel_fpu_disable(), but the older kernels > do not. > > Ingo, this code is really horrible and fragile. We need to cleanup it > step-by-step, imho. Forgot to mention... And, otoh, if we are not going to backport this change, then I think this irq_enable() should be called by do_device_not_available(). > > > ======================> > > From: Ingo Molnar > > Date: Fri, 6 Mar 2015 08:37:57 +0100 > > Subject: [PATCH] x86/fpu: Don't disable irqs in math_state_restore() > > > > math_state_restore() was historically called with irqs disabled, > > because that's how the hardware generates the trap, and also because > > back in the days it was possible for it to be an asynchronous > > interrupt and interrupt handlers run with irqs off. > > > > These days it's always an instruction trap, and furthermore it does > > inevitably complex things such as memory allocation and signal > > processing, which is not done with irqs disabled. > > > > So keep irqs enabled. > > > > This might surprise in-kernel FPU users that somehow relied on > > interrupts being disabled across FPU usage - but that's > > fundamentally fragile anyway due to the inatomicity of FPU state > > restores. The trap return will restore interrupts to its previous > > state, but if FPU ops trigger math_state_restore() there's no > > guarantee of atomicity anymore. > > > > To warn about in-kernel irqs-off users of FPU state we might want to > > pass 'struct pt_regs' to math_state_restore() and check the trapped > > state for irqs disabled (flags has IF cleared) and kernel context - > > but that's for a later patch. > > > > Cc: Andy Lutomirski > > Cc: Borislav Petkov > > Cc: Fenghua Yu > > Cc: H. Peter Anvin > > Cc: Linus Torvalds > > Cc: Oleg Nesterov > > Cc: Quentin Casasnovas > > Cc: Thomas Gleixner > > Signed-off-by: Ingo Molnar > > --- > > arch/x86/kernel/traps.c | 4 ++-- > > 1 file changed, 2 insertions(+), 2 deletions(-) > > > > diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c > > index 950815a138e1..52f9e4057cee 100644 > > --- a/arch/x86/kernel/traps.c > > +++ b/arch/x86/kernel/traps.c > > @@ -844,8 +844,9 @@ void math_state_restore(void) > > { > > struct task_struct *tsk = current; > > > > + local_irq_enable(); > > + > > if (!tsk_used_math(tsk)) { > > - local_irq_enable(); > > /* > > * does a slab alloc which can sleep > > */ > > @@ -856,7 +857,6 @@ void math_state_restore(void) > > do_group_exit(SIGKILL); > > return; > > } > > - local_irq_disable(); > > } > > > > /* Avoid __kernel_fpu_begin() right after __thread_fpu_begin() */