linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: zanussi@kernel.org
To: LKML <linux-kernel@vger.kernel.org>,
	linux-rt-users <linux-rt-users@vger.kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Carsten Emde <C.Emde@osadl.org>, John Kacur <jkacur@redhat.com>,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	Daniel Wagner <wagi@monom.org>, Tom Zanussi <zanussi@kernel.org>
Subject: [PATCH RT 17/25] x86/fpu: Don't cache access to fpu_fpregs_owner_ctx
Date: Fri, 21 Feb 2020 15:24:45 -0600	[thread overview]
Message-ID: <25549e4ff2e5d78e663cf6e5cd8ed108ef03ff44.1582320278.git.zanussi@kernel.org> (raw)
In-Reply-To: <cover.1582320278.git.zanussi@kernel.org>
In-Reply-To: <cover.1582320278.git.zanussi@kernel.org>

From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>

v4.14.170-rt75-rc1 stable review patch.
If anyone has any objections, please let me know.

-----------


[ Upstream commit eb46d70e4455e49928f136f768f1e54646ab4ff7 ]

The state/owner of the FPU is saved to fpu_fpregs_owner_ctx by pointing
to the context that is currently loaded. It never changed during the
lifetime of a task - it remained stable/constant.

After deferred FPU registers loading until return to userland was
implemented, the 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
since gcc 9 is caching that load in copy_fpstate_to_sigframe() and
reusing 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 produced by 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.

The Fixes: tag points to the commit where deferred FPU loading was
added. Since this commit, the compiler is no longer allowed to move the
load of fpu_fpregs_owner_ctx somewhere else / outside of the locked
section. A task preemption will change its value and stale content will
be observed.

 [ bp: Massage. ]

Debugged-by: Austin Clements <austin@google.com>
Debugged-by: David Chase <drchase@golang.org>
Debugged-by: Ian Lance Taylor <ian@airs.com>
Fixes: 5f409e20b7945 ("x86/fpu: Defer FPU state load until return to userspace")
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Borislav Petkov <bp@suse.de>
Reviewed-by: Rik van Riel <riel@surriel.com>
Tested-by: Borislav Petkov <bp@suse.de>
Cc: Aubrey Li <aubrey.li@intel.com>
Cc: Austin Clements <austin@google.com>
Cc: Barret Rhoden <brho@google.com>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: David Chase <drchase@golang.org>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: ian@airs.com
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Josh Bleecher Snyder <josharian@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: x86-ml <x86@kernel.org>
Cc: stable-rt@vger.kernel.org
Link: https://lkml.kernel.org/r/20191128085306.hxfa2o3knqtu4wfn@linutronix.de
Link: https://bugzilla.kernel.org/show_bug.cgi?id=205663
Signed-off-by: Tom Zanussi <zanussi@kernel.org>
---
 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 fa2c93cb42a2..92e12f5d0d64 100644
--- a/arch/x86/include/asm/fpu/internal.h
+++ b/arch/x86/include/asm/fpu/internal.h
@@ -498,7 +498,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;
 }
 
 /*
-- 
2.14.1


  parent reply	other threads:[~2020-02-21 21:26 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-21 21:24 [PATCH RT 00/25] Linux v4.14.170-rt75-rc1 zanussi
2020-02-21 21:24 ` [PATCH RT 01/25] Fix wrong-variable use in irq_set_affinity_notifier zanussi
2020-02-21 21:24 ` [PATCH RT 02/25] x86: preempt: Check preemption level before looking at lazy-preempt zanussi
2020-02-21 21:24 ` [PATCH RT 03/25] sched/deadline: Ensure inactive_timer runs in hardirq context zanussi
2020-02-24  8:33   ` Sebastian Andrzej Siewior
2020-02-25 14:50     ` Juri Lelli
2020-02-21 21:24 ` [PATCH RT 04/25] i2c: hix5hd2: Remove IRQF_ONESHOT zanussi
2020-02-21 21:24 ` [PATCH RT 05/25] i2c: exynos5: " zanussi
2020-02-21 21:24 ` [PATCH RT 06/25] sched: migrate_dis/enable: Use sleeping_lock…() to annotate sleeping points zanussi
2020-02-21 21:24 ` [PATCH RT 07/25] sched: __set_cpus_allowed_ptr: Check cpus_mask, not cpus_ptr zanussi
2020-02-21 21:24 ` [PATCH RT 08/25] sched: Remove dead __migrate_disabled() check zanussi
2020-02-21 21:24 ` [PATCH RT 09/25] sched: migrate disable: Protect cpus_ptr with lock zanussi
2020-02-21 21:24 ` [PATCH RT 10/25] lib/smp_processor_id: Don't use cpumask_equal() zanussi
2020-02-21 21:24 ` [PATCH RT 11/25] futex: Make the futex_hash_bucket spinlock_t again and bring back its old state zanussi
2020-02-21 21:24 ` [PATCH RT 12/25] locking/rtmutex: Clean ->pi_blocked_on in the error case zanussi
2020-02-21 21:24 ` [PATCH RT 13/25] lib/ubsan: Don't seralize UBSAN report zanussi
2020-02-21 21:24 ` [PATCH RT 14/25] kmemleak: Change the lock of kmemleak_object to raw_spinlock_t zanussi
2020-02-21 21:24 ` [PATCH RT 15/25] sched: migrate_enable: Use select_fallback_rq() zanussi
2020-02-24  9:43   ` Sebastian Andrzej Siewior
2020-02-24 15:31     ` Tom Zanussi
2020-02-24 16:05       ` Sebastian Andrzej Siewior
2020-02-24 22:15         ` Scott Wood
2020-02-21 21:24 ` [PATCH RT 16/25] Revert "ARM: Initialize split page table locks for vector page" zanussi
2020-02-21 21:24 ` zanussi [this message]
2020-02-24  8:55   ` [PATCH RT 17/25] x86/fpu: Don't cache access to fpu_fpregs_owner_ctx Sebastian Andrzej Siewior
2020-02-24 15:12     ` Tom Zanussi
2020-02-21 21:24 ` [PATCH RT 18/25] locking: Make spinlock_t and rwlock_t a RCU section on RT zanussi
2020-02-21 21:24 ` [PATCH RT 19/25] userfaultfd: Use a seqlock instead of seqcount zanussi
2020-02-24  9:03   ` Sebastian Andrzej Siewior
2020-02-24 15:14     ` Tom Zanussi
2020-02-24 16:17     ` Steven Rostedt
2020-02-21 21:24 ` [PATCH RT 20/25] kmemleak: Cosmetic changes zanussi
2020-02-24  9:12   ` Sebastian Andrzej Siewior
2020-02-24 15:18     ` Tom Zanussi
2020-02-24 15:52       ` Sebastian Andrzej Siewior
2020-02-21 21:24 ` [PATCH RT 21/25] smp: Use smp_cond_func_t as type for the conditional function zanussi
2020-02-24  9:52   ` Sebastian Andrzej Siewior
2020-02-24 15:34     ` Tom Zanussi
2020-02-21 21:24 ` [PATCH RT 22/25] mm/memcontrol: Move misplaced local_unlock_irqrestore() zanussi
2020-02-24  9:55   ` Sebastian Andrzej Siewior
2020-02-21 21:24 ` [PATCH RT 23/25] locallock: Include header for the `current' macro zanussi
2020-02-21 21:24 ` [PATCH RT 24/25] sched: Provide migrate_disable/enable() inlines zanussi
2020-02-21 21:24 ` [PATCH RT 25/25] Linux 4.14.170-rt75-rc1 zanussi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=25549e4ff2e5d78e663cf6e5cd8ed108ef03ff44.1582320278.git.zanussi@kernel.org \
    --to=zanussi@kernel.org \
    --cc=C.Emde@osadl.org \
    --cc=bigeasy@linutronix.de \
    --cc=jkacur@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rt-users@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    --cc=wagi@monom.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).