All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5] x86: load FPU registers on return to userland
@ 2018-11-28 22:20 Sebastian Andrzej Siewior
  2018-11-28 22:20 ` [PATCH 01/29] x86/fpu: Use ULL for shift in xfeature_uncompacted_offset() Sebastian Andrzej Siewior
                   ` (29 more replies)
  0 siblings, 30 replies; 60+ messages in thread
From: Sebastian Andrzej Siewior @ 2018-11-28 22:20 UTC (permalink / raw)
  To: linux-kernel
  Cc: x86, Andy Lutomirski, Paolo Bonzini, Radim Krčmář,
	kvm, Jason A. Donenfeld, Rik van Riel, Dave Hansen

This is a refurbished series originally started by by Rik van Riel. The
goal is load the FPU registers on return to userland and not on every
context switch. By this optimisation we can:
- avoid loading the registers if the task stays in kernel and does
  not return to userland
- make kernel_fpu_begin() cheaper: it only saves the registers on the
  first invocation. The second invocation does not need save them again.

To access the FPU registers in kernel we need:
- disable preemption to avoid that the scheduler switches tasks. By
  doing so it would set TIF_NEED_FPU_LOAD and the FPU registers would be
  not valid.
- disable BH because the softirq might use kernel_fpu_begin() and then
  set TIF_NEED_FPU_LOAD instead loading the FPU registers on completion.

v4…v5:
Rebased on top of a fix, noticed a problem with XSAVES and then redid
the restore on sig return (patch #26 to #28).

I don't like very much the sig save+restore thing that we are doing. It
has been always like that. I *think* that this is just because we have
nowhere to stash the FPU state while we are handling the signal. We
could add another fpu->state for the signal handler and avoid the thing.
Debian code-search revealed that `criu' is using it (and I didn't
figure out why). Nothing else (that is packaged in Debian). Maybe we
could get rid of this and if `criu' would then use a dedicated interface
for its needs rather the signal interface that happen to do what it
wants :)

v3…v4:
It has been suggested to remove the `initialized' member of the struct
fpu because it should not required be needed with lazy-FPU-restore and
would make the review easier. This is the first part of the series, the
second is basically the rebase of the v3 queue. As a result, the
diffstat became negative (which wasn't the case in previous version) :)
I tried to incorporate all the review comments that came up, some of
them were "outdated" after the removal of the `initialized' member. I'm
sorry should I missed any.

v1…v3:
v2 was never posted. I followed the idea to completely decouple PKRU
from xstate. This didn't quite work and made a few things complicated. 
One obvious required fixup is copy_fpstate_to_sigframe() where the PKRU
state needs to be fiddled into xstate. This required another
xfeatures_mask so that the sanity checks were performed and
xstate_offsets would be computed. Additionally ptrace also reads/sets
xstate in order to get/set the register and PKRU is one of them. So this
would need some fiddle, too.
In v3 I dropped that decouple idea. I also learned that the wrpkru
instruction is not privileged and so caching it in kernel does not work.
Instead I keep PKRU in xstate area and load it at context switch time
while the remaining registers are deferred (until return to userland).
The offset of PKRU within xstate is enumerated at boot time so why not
use it.

Rik van Riel (5):
  x86/fpu: Add (__)make_fpregs_active helpers
  x86/fpu: Eager switch PKRU state
  x86/fpu: Always store the registers in copy_fpstate_to_sigframe()
  x86/fpu: Prepare copy_fpstate_to_sigframe() for TIF_NEED_FPU_LOAD
  x86/fpu: Defer FPU state load until return to userspace

Sebastian Andrzej Siewior (24):
  x86/fpu: Use ULL for shift in xfeature_uncompacted_offset()
  x86/entry/32: Remove asm/math_emu.h include
  x86/entry: Remove _TIF_ALLWORK_MASK
  x86/pkeys: Make init_pkru_value static
  x86/fpu: add might_fault() to user_insn()
  x86/fpu: Update comment for __raw_xsave_addr()
  x86/fpu: don't export __kernel_fpu_{begin|end}()
  x86/fpu: Remove fpu->initialized usage in __fpu__restore_sig()
  x86/fpu: Remove fpu__restore()
  x86/fpu: Remove preempt_disable() in fpu__clear()
  x86/fpu: Always init the `state' in fpu__clear()
  x86/fpu: Remove fpu->initialized usage in copy_fpstate_to_sigframe()
  x86/fpu: Don't save fxregs for ia32 frames in
    copy_fpstate_to_sigframe()
  x86/fpu: Remove fpu->initialized
  x86/fpu: Remove user_fpu_begin()
  x86/fpu: Make __raw_xsave_addr() use feature number instead of mask
  x86/fpu: Make get_xsave_field_ptr() and get_xsave_addr() use feature
    number instead of mask
  x86/fpu: Only write PKRU if it is different from current
  x86/pkeys: Don't check if PKRU is zero before writting it
  x86/entry: Add TIF_NEED_FPU_LOAD
  x86/fpu: Update xstate's PKRU value on write_pkru()
  x86/fpu: Inline copy_user_to_fpregs_zeroing()
  x86/fpu: Let __fpu__restore_sig() restore the !32bit+fxsr frame from
    kernel memory
  x86/fpu: Merge the two code paths in __fpu__restore_sig()

 Documentation/preempt-locking.txt    |   1 -
 arch/x86/entry/common.c              |   8 ++
 arch/x86/ia32/ia32_signal.c          |  17 +--
 arch/x86/include/asm/efi.h           |   6 +-
 arch/x86/include/asm/fpu/api.h       |  47 ++++--
 arch/x86/include/asm/fpu/internal.h  | 152 ++++++++------------
 arch/x86/include/asm/fpu/signal.h    |   2 +-
 arch/x86/include/asm/fpu/types.h     |   9 --
 arch/x86/include/asm/fpu/xstate.h    |   5 +-
 arch/x86/include/asm/pgtable.h       |  20 ++-
 arch/x86/include/asm/special_insns.h |  13 +-
 arch/x86/include/asm/thread_info.h   |  10 +-
 arch/x86/include/asm/trace/fpu.h     |   8 +-
 arch/x86/kernel/fpu/core.c           | 199 ++++++++++++--------------
 arch/x86/kernel/fpu/init.c           |   2 -
 arch/x86/kernel/fpu/regset.c         |  24 +---
 arch/x86/kernel/fpu/signal.c         | 206 ++++++++++++---------------
 arch/x86/kernel/fpu/xstate.c         |  47 +++---
 arch/x86/kernel/process.c            |   2 +-
 arch/x86/kernel/process_32.c         |  14 +-
 arch/x86/kernel/process_64.c         |  11 +-
 arch/x86/kernel/signal.c             |  17 +--
 arch/x86/kernel/traps.c              |   2 +-
 arch/x86/kvm/x86.c                   |  47 +++---
 arch/x86/math-emu/fpu_entry.c        |   3 -
 arch/x86/mm/mpx.c                    |   6 +-
 arch/x86/mm/pkeys.c                  |  15 +-
 27 files changed, 424 insertions(+), 469 deletions(-)

 git://git.kernel.org/pub/scm/linux/kernel/git/bigeasy/staging.git x86_fpu_rtu_v5

Sebastian


^ permalink raw reply	[flat|nested] 60+ messages in thread

end of thread, other threads:[~2018-12-13 14:36 UTC | newest]

Thread overview: 60+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-28 22:20 [PATCH v5] x86: load FPU registers on return to userland Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 01/29] x86/fpu: Use ULL for shift in xfeature_uncompacted_offset() Sebastian Andrzej Siewior
2018-11-29  1:52   ` Rik van Riel
2018-12-03 21:00   ` [tip:x86/fpu] x86/fpu: Use unsigned long long " tip-bot for Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 02/29] x86/entry/32: Remove asm/math_emu.h include Sebastian Andrzej Siewior
2018-11-29  1:52   ` Rik van Riel
2018-12-03 21:01   ` [tip:x86/fpu] x86/process/32: " tip-bot for Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 03/29] x86/entry: Remove _TIF_ALLWORK_MASK Sebastian Andrzej Siewior
2018-11-29  1:53   ` Rik van Riel
2018-12-03 21:02   ` [tip:x86/fpu] x86/thread_info: " tip-bot for Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 04/29] x86/pkeys: Make init_pkru_value static Sebastian Andrzej Siewior
2018-11-29  1:53   ` Rik van Riel
2018-12-03 21:02   ` [tip:x86/fpu] " tip-bot for Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 05/29] x86/fpu: add might_fault() to user_insn() Sebastian Andrzej Siewior
2018-11-29  1:54   ` Rik van Riel
2018-12-03 21:03   ` [tip:x86/fpu] x86/fpu: Add " tip-bot for Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 06/29] x86/fpu: Update comment for __raw_xsave_addr() Sebastian Andrzej Siewior
2018-11-29  1:56   ` Rik van Riel
2018-12-03 21:03   ` [tip:x86/fpu] " tip-bot for Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 07/29] x86/fpu: don't export __kernel_fpu_{begin|end}() Sebastian Andrzej Siewior
2018-11-29  2:00   ` Rik van Riel
2018-11-29 15:02     ` [PATCH 07/29 v2] " Sebastian Andrzej Siewior
2018-12-03 21:04       ` [tip:x86/fpu] x86/fpu: Don't export __kernel_fpu_{begin,end}() tip-bot for Sebastian Andrzej Siewior
2018-12-03 21:12         ` Ard Biesheuvel
2018-12-03 22:08           ` Borislav Petkov
2018-12-04 11:39             ` Borislav Petkov
2018-12-04 12:15             ` Sebastian Andrzej Siewior
2018-12-04 12:33               ` Borislav Petkov
2018-12-04 11:45       ` tip-bot for Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 08/29] x86/fpu: Remove fpu->initialized usage in __fpu__restore_sig() Sebastian Andrzej Siewior
2018-12-06 20:07   ` Borislav Petkov
2018-12-07  8:17     ` Sebastian Andrzej Siewior
2018-12-07 10:19       ` Borislav Petkov
2018-11-28 22:20 ` [PATCH 09/29] x86/fpu: Remove fpu__restore() Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 10/29] x86/fpu: Remove preempt_disable() in fpu__clear() Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 11/29] x86/fpu: Always init the `state' " Sebastian Andrzej Siewior
2018-12-12 17:11   ` Borislav Petkov
2018-12-13 14:35     ` Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 12/29] x86/fpu: Remove fpu->initialized usage in copy_fpstate_to_sigframe() Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 13/29] x86/fpu: Don't save fxregs for ia32 frames " Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 14/29] x86/fpu: Remove fpu->initialized Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 15/29] x86/fpu: Remove user_fpu_begin() Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 16/29] x86/fpu: Add (__)make_fpregs_active helpers Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 17/29] x86/fpu: Make __raw_xsave_addr() use feature number instead of mask Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 18/29] x86/fpu: Make get_xsave_field_ptr() and get_xsave_addr() " Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 19/29] x86/fpu: Only write PKRU if it is different from current Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 20/29] x86/pkeys: Don't check if PKRU is zero before writting it Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 21/29] x86/fpu: Eager switch PKRU state Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 22/29] x86/entry: Add TIF_NEED_FPU_LOAD Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 23/29] x86/fpu: Always store the registers in copy_fpstate_to_sigframe() Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 24/29] x86/fpu: Prepare copy_fpstate_to_sigframe() for TIF_NEED_FPU_LOAD Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 25/29] x86/fpu: Update xstate's PKRU value on write_pkru() Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 26/29] x86/fpu: Inline copy_user_to_fpregs_zeroing() Sebastian Andrzej Siewior
2018-11-28 23:09   ` Joey Pabalinas
2018-11-28 22:20 ` [PATCH 27/29] x86/fpu: Let __fpu__restore_sig() restore the !32bit+fxsr frame from kernel memory Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 28/29] x86/fpu: Merge the two code paths in __fpu__restore_sig() Sebastian Andrzej Siewior
2018-11-28 22:20 ` [PATCH 29/29] x86/fpu: Defer FPU state load until return to userspace Sebastian Andrzej Siewior
2018-11-29 15:00   ` Sebastian Andrzej Siewior
2018-12-10 14:41   ` Sebastian Andrzej Siewior
2018-11-30 11:52 ` [PATCH v5] x86: load FPU registers on return to userland Sebastian Andrzej Siewior

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.