Re: [PATCH RFC 3/3] x86/fpu: defer FPU state load until return to userspace

From: Rik van Riel
Date: Mon Oct 17 2016 - 20:06:18 EST


On Mon, 2016-10-17 at 13:58 -0700, Andy Lutomirski wrote:
> On Mon, Oct 17, 2016 at 1:09 PM,ÂÂ<riel@xxxxxxxxxx> wrote:
> >
> > From: Rik van Riel <riel@xxxxxxxxxx>
> >
> > Defer loading of FPU state until return to userspace. This gives
> > the kernel the potential to skip loading FPU state for tasks that
> > stay in kernel mode, or for tasks that end up with repeated
> > invocations of kernel_fpu_begin.

> > Â#define CREATE_TRACE_POINTS
> > Â#include <trace/events/syscalls.h>
> > @@ -189,6 +190,14 @@ __visible inline void
> > prepare_exit_to_usermode(struct pt_regs *regs)
> > ÂÂÂÂÂÂÂÂif (unlikely(cached_flags & EXIT_TO_USERMODE_LOOP_FLAGS))
> > ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂexit_to_usermode_loop(regs, cached_flags);
> >
> > +ÂÂÂÂÂÂÂ/* Reload ti->flags; we may have rescheduled above. */
> > +ÂÂÂÂÂÂÂcached_flags = READ_ONCE(ti->flags);
>
> Stick this bit in the "if" above, please.

Will do.

> But I still don't see how this can work correctly with PKRU.

OK, Andy and I talked on IRC, and we have some ideas on how
to fix & improve this series:

1) pin/unpin_fpregs_active to prevent leaking of other
 Âusers' fpregs contents to userspace (patch 1)
2) eagerly switch PKRU state (only), at task switch time,
 Âif the incoming task has different protection keys from
 Âthe outgoing task (somewhat unlikely), just like the
 ÂKVM vcpu entry & exit code is already doing
3) remove stts from the KVM VMX code (Andy may get
 Âto this before me)
4) enhance __kernel_fpu_begin() to take an fpu argument,
 Âand let the caller (really just kvm_load_guest_fpu)
 Âknow whether that fpu state is still present in the
 Âregisters, allowing it to skip __copy_kernel_to_fpregs

--
All Rights Reversed.

Attachment: signature.asc
Description: This is a digitally signed message part