From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 20DD6C43381 for ; Sun, 31 Mar 2019 18:20:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E24CE20872 for ; Sun, 31 Mar 2019 18:20:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731443AbfCaSUc (ORCPT ); Sun, 31 Mar 2019 14:20:32 -0400 Received: from Galois.linutronix.de ([146.0.238.70]:57451 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726170AbfCaSUc (ORCPT ); Sun, 31 Mar 2019 14:20:32 -0400 Received: from p5492e2fc.dip0.t-ipconnect.de ([84.146.226.252] helo=nanos) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1hAf4E-0002iU-6e; Sun, 31 Mar 2019 20:20:26 +0200 Date: Sun, 31 Mar 2019 20:20:25 +0200 (CEST) From: Thomas Gleixner To: Sebastian Andrzej Siewior cc: linux-kernel@vger.kernel.org, x86@kernel.org, Andy Lutomirski , Paolo Bonzini , =?ISO-8859-2?Q?Radim_Kr=E8m=E1=F8?= , kvm@vger.kernel.org, "Jason A. Donenfeld" , Rik van Riel , Dave Hansen Subject: Re: [PATCH 18/24] x86/fpu: Prepare copy_fpstate_to_sigframe() for TIF_NEED_FPU_LOAD In-Reply-To: <20190321202632.16810-19-bigeasy@linutronix.de> Message-ID: References: <20190321202632.16810-1-bigeasy@linutronix.de> <20190321202632.16810-19-bigeasy@linutronix.de> User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 21 Mar 2019, Sebastian Andrzej Siewior wrote: > From: Rik van Riel > > The FPU registers need only to be saved if TIF_NEED_FPU_LOAD is not set. > Otherwise this has been already done and can be skipped. > > Signed-off-by: Rik van Riel > Signed-off-by: Sebastian Andrzej Siewior > --- > arch/x86/kernel/fpu/signal.c | 11 ++++++++++- > 1 file changed, 10 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c > index f55f16d9e7e4e..97ea6909ede1f 100644 > --- a/arch/x86/kernel/fpu/signal.c > +++ b/arch/x86/kernel/fpu/signal.c > @@ -155,7 +155,16 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size) > sizeof(struct user_i387_ia32_struct), NULL, > (struct _fpstate_32 __user *) buf) ? -1 : 1; > > - copy_fpregs_to_fpstate(fpu); > + fpregs_lock(); > + /* > + * If we do not need to load the FPU registers at return to userspace > + * then the CPU has the current state and we need to save it. Otherwise > + * it is already done and we can skip it. > + */ > + if (!test_thread_flag(TIF_NEED_FPU_LOAD)) > + copy_fpregs_to_fpstate(fpu); I think this should do the following: fpregs_lock(); if (!test_thread_flag(TIF_NEED_FPU_LOAD)) { pagefault_disable(); ret = copy_fpu_to_user(...); pagefault_enable(); if (!res) return 0; copy_fpregs_to_fpstate(fpu); } fpregs_unlock(); The point is that in most cases the direct store from the FPU registers to user space will succeed simply because the stack is accessible and you only do the store in kernel memory and copy when that fails. Thanks, tglx