Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp5568253ybc; Wed, 27 Nov 2019 06:09:35 -0800 (PST) X-Google-Smtp-Source: APXvYqwQJhRtW9LZXgp7sXhRnDJgHm12WLkSKDg28rsXtpLWFVZbD8I5aNftOQHLgA1jvJ8jEmCy X-Received: by 2002:aa7:c0c8:: with SMTP id j8mr31823055edp.235.1574863775750; Wed, 27 Nov 2019 06:09:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574863775; cv=none; d=google.com; s=arc-20160816; b=ERZqA2QdqG458KpxEI0meVfEd3x9ZGLxPQKI62Hzp7LKjBXDNozNP0/8il6LoxRoH9 aRpghm54azYDlFI2mXA5B7q4JOTUz6e5XkH+GVRlkf8dD2Hpha8x/MUPWZkwk0SCPBiL fmHYmt3qA2UTQBYlA+/JEcJGBUCLFE5Jj7SVreCE7FAN1onoGyxruk9XtOn7MMTxXOgT j1lVUBW3PwvuFKwjL5/HgcMuRIGCOQXGq5i2uVP8G4jUyEL0Rgpy9YCUgdyMAcP/ibzc N39RkZDiNqMC57YLZZI36GHJok22WBQ8UFpeHUJnpm/2fV1ZBjnknyQRohRlLs59zl0L znxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=5j8VPYCmYHx3fh+OtrTcC93rJVI3yBgKJ8qLj1fRrSs=; b=j1miMO0f5pG+MMPflw4RvLXsuYHke5Yvq7eTcCeedErhRlavPJzukerZL1bhKOLGff 2TuI3hWR6R/0RPRJ5IlEeIyKuYNNXE9yntOzZ0HAauEkfQsRgkndKZkvquVodIExKsBX +lmytcRnWTPi24cjfMfL3bRNZgAAtwT2T8eVCYX09sfKAJ+v6CD0qjpRSCtpG2G7D70C NNfG/nXYTUUM4ldYdNKJUeZGb4wwQ+uAQosIikNBdKvQ2MkTQ01lc2dGoN5hQnAsPaCU vjw+7GNdXzTZ5IFointr2YFEo/hRRp7bpVrLi0kTiQalZprjf+d7r57VYmxxJov3nByx d/LA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b=ln3XC+Nf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k17si9719033ejd.264.2019.11.27.06.09.07; Wed, 27 Nov 2019 06:09:35 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b=ln3XC+Nf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726937AbfK0OIH (ORCPT + 99 others); Wed, 27 Nov 2019 09:08:07 -0500 Received: from mail.skyhub.de ([5.9.137.197]:50186 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726655AbfK0OIH (ORCPT ); Wed, 27 Nov 2019 09:08:07 -0500 Received: from zn.tnic (p200300EC2F0F3700E96120E3EC041D57.dip0.t-ipconnect.de [IPv6:2003:ec:2f0f:3700:e961:20e3:ec04:1d57]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 035F61EC0CEA; Wed, 27 Nov 2019 15:08:01 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1574863682; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5j8VPYCmYHx3fh+OtrTcC93rJVI3yBgKJ8qLj1fRrSs=; b=ln3XC+NfbK8QFPfrdEV18IAvxwZJbr0P4+Kcb1FRSLOm+zvBq7s5CxjAT9lcyXlGuR8a8b QLYDVej6L5k+mB9Bf/K+QMBPMmqckk/rLXCGWWSyFa6FCEfw/qkWYg5QS/iqwXnbJVm2bz gNsMwrG0Pt1lD1xtk8oygsZLUg4r4Y0= Date: Wed, 27 Nov 2019 15:07:54 +0100 From: Borislav Petkov To: Sebastian Andrzej Siewior Cc: Barret Rhoden , Josh Bleecher Snyder , "Rik van Riel\"" , x86@kernel.org, linux-kernel@vger.kernel.org, Thomas Gleixner , Ingo Molnar , ian@airs.com Subject: Re: [PATCH] x86/fpu: Don't cache access to fpu_fpregs_owner_ctx Message-ID: <20191127140754.GB3812@zn.tnic> References: <20191126202026.csrmjre6vn2nxq7c@linutronix.de> <20191127124243.u74osvlkhcmsskng@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20191127124243.u74osvlkhcmsskng@linutronix.de> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 27, 2019 at 01:42:43PM +0100, Sebastian Andrzej Siewior wrote: > The state/owner of FPU is saved fpu_fpregs_owner_ctx by pointing to the ^ to > context that is currently loaded. It never changed during the life time > of a task and remained stable/constant. > > Since we deferred loading the FPU registers on return to userland, the Drop those "we"s :) > content of fpu_fpregs_owner_ctx may change during preemption and must > not be cached. > This went unnoticed for some time and was now noticed, in particular > gcc-9 is able to cache that load in copy_fpstate_to_sigframe() and reuse > it in the retry loop: > > copy_fpstate_to_sigframe() > load fpu_fpregs_owner_ctx and save on stack > fpregs_lock() > copy_fpregs_to_sigframe() /* failed */ > fpregs_unlock() > *** PREEMPTION, another uses FPU, changes fpu_fpregs_owner_ctx *** > > fault_in_pages_writeable() /* succeed, retry */ > > fpregs_lock() > __fpregs_load_activate() > fpregs_state_valid() /* uses fpu_fpregs_owner_ctx from stack */ > copy_fpregs_to_sigframe() /* succeeds, random FPU content */ > > This is a comparison of the assembly of gcc-9, without vs with this > patch: > > | # arch/x86/kernel/fpu/signal.c:173: if (!access_ok(buf, size)) > | cmpq %rdx, %rax # tmp183, _4 > | jb .L190 #, > |-# arch/x86/include/asm/fpu/internal.h:512: return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu; > |-#APP > |-# 512 "arch/x86/include/asm/fpu/internal.h" 1 > |- movq %gs:fpu_fpregs_owner_ctx,%rax #, pfo_ret__ > |-# 0 "" 2 > |-#NO_APP > |- movq %rax, -88(%rbp) # pfo_ret__, %sfp > … > |-# arch/x86/include/asm/fpu/internal.h:512: return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu; > |- movq -88(%rbp), %rcx # %sfp, pfo_ret__ > |- cmpq %rcx, -64(%rbp) # pfo_ret__, %sfp > |+# arch/x86/include/asm/fpu/internal.h:512: return fpu == this_cpu_read(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu; > |+#APP > |+# 512 "arch/x86/include/asm/fpu/internal.h" 1 > |+ movq %gs:fpu_fpregs_owner_ctx(%rip),%rax # fpu_fpregs_owner_ctx, pfo_ret__ > |+# 0 "" 2 > |+# arch/x86/include/asm/fpu/internal.h:512: return fpu == this_cpu_read(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu; > |+#NO_APP > |+ cmpq %rax, -64(%rbp) # pfo_ret__, %sfp > > Use this_cpu_read() instead this_cpu_read_stable() to avoid caching of > fpu_fpregs_owner_ctx during preemption points. > > Fixes: 5f409e20b7945 ("x86/fpu: Defer FPU state load until return to userspace") Or a352a3b7b792 ("x86/fpu: Prepare copy_fpstate_to_sigframe() for TIF_NEED_FPU_LOAD") maybe, which adds the fpregs_unlock() ? > --- > > There is no Sign-off by here. Could this please be verified by the > reporter? Not the reporter, but I just tested it successfully too: Tested-by: Borislav Petkov > Also I would like to add > Debugged-by: Ian Lance Taylor Yes, pls. CCed. > > but I lack the complete address also I'm not sure if he wants to. > Also please send a Reported-by line since I'm not sure who started this. > > arch/x86/include/asm/fpu/internal.h | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/arch/x86/include/asm/fpu/internal.h b/arch/x86/include/asm/fpu/internal.h > index 4c95c365058aa..44c48e34d7994 100644 > --- a/arch/x86/include/asm/fpu/internal.h > +++ b/arch/x86/include/asm/fpu/internal.h > @@ -509,7 +509,7 @@ static inline void __fpu_invalidate_fpregs_state(struct fpu *fpu) > > static inline int fpregs_state_valid(struct fpu *fpu, unsigned int cpu) > { > - return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu; > + return fpu == this_cpu_read(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu; > } And to add one more data point from IRC: this is also documented: /* * this_cpu_read() makes gcc load the percpu variable every time it is * accessed while this_cpu_read_stable() allows the value to be cached. ^^^^^^^^^^^^^^^ * this_cpu_read_stable() is more efficient and can be used if its value * is guaranteed to be valid across cpus. The current users include * get_current() and get_thread_info() both of which are actually * per-thread variables implemented as per-cpu variables and thus * stable for the duration of the respective task. */ #define this_cpu_read_stable(var) percpu_stable_op("mov", var) -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette