Skip to content

RFC move FPU context loading to userspace switch

[email protected] lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest11 patches 35 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/11x86,fpu: document the data structures a littlearch/x86/include/asm/processor.h arch/x86/kernel/cpu/common.c+8 -24 replies
2/11x86,fpu: replace fpu_switch_t with a thread flagarch/x86/include/asm/fpu-internal.h arch/x86/include/asm/thread_info.h arch/x86/kernel/process_32.c +1 more+20 -323 replies
3/11x86,fpu: move __thread_fpu_begin to when the task has the fpuarch/x86/include/asm/fpu-internal.h+1 -22 replies
4/11x86,fpu: defer FPU restore until return to userspacearch/x86/include/asm/fpu-internal.h arch/x86/kernel/process_32.c arch/x86/kernel/process_64.c +1 more+30 -1615 replies
5/11x86,fpu: ensure FPU state is reloaded from memory if task is tracedarch/x86/include/asm/fpu-internal.h+7 -14 replies
6/11x86,fpu: lazily skip fpu restore with eager fpu mode, tooarch/x86/include/asm/fpu-internal.h+6 -65 replies
7/11x86,fpu: store current fpu pointer, instead of fpu_owner_taskarch/x86/include/asm/fpu-internal.h arch/x86/kernel/cpu/common.c arch/x86/kernel/i387.c+12 -100 replies
8/11x86,fpu: restore user FPU state lazily after __kernel_fpu_endarch/x86/kernel/i387.c+3 -52 replies
9/11x86,fpu,kvm: keep vcpu FPU active as long as it is residentarch/x86/kvm/x86.c include/linux/kvm_host.h+20 -80 replies
10/11x86,fpu: fix fpu_copy to deal with not-loaded fpuarch/x86/include/asm/fpu-internal.h+3 -10 replies
11/11(BROKEN) x86,fpu: broken signal handler stack setuparch/x86/include/asm/fpu-internal.h arch/x86/kernel/xsave.c+39 -330 replies

Reviews trailers per patch

None of the 11 patches has a Reviewed-by or Acked-by of their own; the series has one for all of them.

PatchRik van Riel
SeriesR-b R-b A-b R-b R-b R-b
1/11
2/11
3/11
4/11
5/11
6/11
7/11
8/11
9/11
10/11
11/11Not-signed-off-by

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [RFC PATCH 0/11 BROKEN] move FPU context loading to userspace switch

Currently the kernel will always load the FPU context, even when switching to a kernel thread, or to an idle thread. In the case of a task on a KVM VCPU going idle for a bit, and waking up again later, this creates a vastly inefficient chain of FPU context saves & loads:

1) save task FPU context, load idle task FPU context (in KVM guest)
2) trap to host
3) save VCPU guest FPU context, load VCPU userspace context (__kernel_fpu_end)
4) save VCPU userspace context, load idle thread FPU context
5) save idle thread FPU context, load VCPU userspace FPU context
6) save VCPU userspace FPU context, load guest FPU context (__kernel_fpu_begin)
7) enter guest
8) save idle task FPU context, load task FPU context (in KVM guest)

This is a total of 6 FPU saves and 6 restores, touching 4 different FPU contexts, only one of which is ever used. The hardware optimizes FPU load and restore pretty well, but 12 operations involving 384 bytes of data adds substantial overhead. Additionally, the XSTOROPT optimization does not work across VMENTER / VMEXIT boundaries, so things are slower than they would be on bare metal.

This patch series reduces it to two saves (1) and (3), and one load (6), if the VCPU and the task inside the guest both stay on the same CPU. The load could be optimized away in a subsequent series, by recognizing that the emulator did not touch the in-memory FPU state for the guest.

This could also give a small performance gain for bare metal applications that wake up and go idle repeatedly, staying on the same CPU.

Where it all falls apart (probably due to a stupid mistake on my end) is the signal handling code.

In the signal handling code, the registers (including FPU state) are all saved to the user space stack, and on sigreturn they are loaded back in. The signal handler setup code needs to be fixed to deal with the other changes, but I am apparently doing that incorrectly.

I have been staring at the code for a few weeks now, and do not appear to be any closer to figuring out what I did wrong in the last patch of this series.

I would really appreciate it if people with better knowledge of the signal handler and/or FPU code could take a look :)