PKRU is a component of the XSAVE state and was handled as such: saved
on context switch only for the thread that owns the FPU, and loaded on
#NM or on the next switch. But RDPKRU and WRPKRU work with CR0.TS set,
and PKRU applies to the kernel mode accesses to user memory, so the
register has to hold the value of the running thread at all times.
It did not:
- after fpu_kern_enter(FPU_KERN_NOCTX), which loads the initial state, PKRU was zero for the rest of the system call and in user mode;
- a value written with WRPKRU while the thread did not own the FPU, for instance in the parent right after fork(), was replaced by the old one from the save area on the next context switch;
- after sigreturn() the interrupted code ran with the PKRU of the signal handler;
- kernel threads ran with the PKRU of the thread that used the CPU before, and a kernel thread completing an aio request on a socket could fail with EFAULT.
Keep the PKRU of a thread that is not running in struct mdthread.
cpu_switch() saves the register there and fpu_activate_sw() loads it,
regardless of the FPU ownership; kernel threads get zero. The XSAVE
instructions executed by the kernel no longer include PKRU in the
mask, so nothing loads it as a side effect. XCR0 and the layout of
the save area do not change.
The PKRU component of the save area remains the format in which the
value is passed to and from userspace: fpugetregs() fills it in, and
fpusetxstate() takes the value from it. This covers signal frames,
getcontext(), ptrace(), core dumps and sysarch(). fpstate_drop()
resets PKRU together with the rest of the state, as execve() and
signal delivery expect. The Linux ABI code is reduced to reading and
writing the register.
The new member of struct mdthread fills a hole, the layout of struct
thread does not change.
Fixes: e7a9df16e654 ("Add kernel support for Intel userspace...")
Sponsored by: Rubicon Communications, LLC ("Netgate")