RFC: unlazy fpu for frequent fpu users
In-Reply-To: <[email protected]>
Quoted 7 lines · Arjan van de Ven
right now the kernel on x86-64 has a 100% lazy fpu behavior: after *every* context switch a trap is taken for the first FPU use to restore the FPU context lazily. This is of course great for applications that have very sporadic or no FPU use (since then you avoid doing the expensive save/restore all the time). However for very frequent FPU users... you take an extra trap every context switch.
Quoted 6 lines
The patch below adds a simple heuristic to this code: After 5 consecutive context switches of FPU use, the lazy behavior is disabled and the context gets restored every context switch. If the app indeed uses the FPU, the trap is avoided. (the chance of the 6th time slice using FPU after the previous 5 having done so are quite high obviously).
You can do better that that. FXSR doesn't destroy the FPU contents; if you track the context carefully you can completely avoid the restore. This requires keeping a per-cpu variable that holds a pointer to the thread that last used the FPU and a per-thread variable containing the CPU number on which the thread last used FP math. Unfortunately this won't work in x86_64 because of the 'fxsave information leak' workaround.
arch/x86_64/kernel/process.c __switch_to(struct task_struct *prev_p, · line 523
Quoted 12 lines
arch/x86_64/kernel/process.c +3
-0@@ -515,6 +515,9 @@ __switch_to(struct task_struct *prev_p,int cpu =smp_processor_id ();struct tss_struct *tss = &per_cpu (init_tss, cpu); +/* prefetch the fxsave area into the cache */ +prefetch (&next->i387.fxsave); +/* * Reload esp0, LDT and the page table pointer: */
This prefetch is probably a bad idea. I ported your patch to i386 and it was actually slower until I changed it:
+ if (next_p->fpu_counter > 5)
+ /* prefetch the fxsave area into the cache */
+ prefetch(&next->i387.fxsave);
+
Now it's ~.4% faster. The test was an FP program doing a simple benchmark while a non-FP program ran in a tight loop.
Signature · 2 lines
Chuck "You can't read a newspaper if you can't read." --George W. Bush
Quoted 4 lines
You can do better that that. FXSR doesn't destroy the FPU contents; if you track the context carefully you can completely avoid the restore. This requires keeping a per-cpu variable that holds a pointer to the
to be honest, while I like the idea, it does scare me from a security point of view, both in terms of leaks and in terms of injecting bad stuff.
Quoted 21 lines
arch/x86_64/kernel/process.c __switch_to(struct task_struct *prev_p, · line 523
Quoted 12 lines
arch/x86_64/kernel/process.c +3
-0@@ -515,6 +515,9 @@ __switch_to(struct task_struct *prev_p,int cpu =smp_processor_id ();struct tss_struct *tss = &per_cpu (init_tss, cpu); +/* prefetch the fxsave area into the cache */ +prefetch (&next->i387.fxsave); +/* * Reload esp0, LDT and the page table pointer: */ This prefetch is probably a bad idea. I ported your patch to i386 and it was actually slower until I changed it:
(quoted diff) +4
-0+ if (next_p->fpu_counter > 5) + /* prefetch the fxsave area into the cache */ + prefetch(&next->i387.fxsave); +
Quoted 2 lines
Now it's ~.4% faster. The test was an FP program doing a simple benchmark while a non-FP program ran in a tight loop.
nice! I sort of am not a big fan of if .. prefetch() but if it shows gain... then you convinced me. 0.4% is roughly the same order I saw. It's not gigantic but it's almost free to do so it may be worth it anyway...
Can you send me your patch so that I can integrate it (and I'll port your if() to the prefetch)...
Greetings,
Arjan van de Ven