Skip to content

Commit bae7fc6

Browse files
bgaffgregkh
authored andcommitted
x86/fpu: Correct pkru/xstate inconsistency
When eagerly switching PKRU in switch_fpu_finish() it checks that current is not a kernel thread as kernel threads will never use PKRU. It's possible that this_cpu_read_stable() on current_task (ie. get_current()) is returning an old cached value. To resolve this reference next_p directly rather than relying on current. As written it's possible when switching from a kernel thread to a userspace thread to observe a cached PF_KTHREAD flag and never restore the PKRU. And as a result this issue only occurs when switching from a kernel thread to a userspace thread, switching from a non kernel thread works perfectly fine because all that is considered in that situation are the flags from some other non kernel task and the next fpu is passed in to switch_fpu_finish(). This behavior only exists between 5.2 and 5.13 when it was fixed by a rewrite decoupling PKRU from xstate, in: commit 9544369 ("x86/fpu: Remove PKRU handling from switch_fpu_finish()") Unfortunately backporting the fix from 5.13 is probably not realistic as it's part of a 60+ patch series which rewrites most of the PKRU handling. Fixes: 0cecca9 ("x86/fpu: Eager switch PKRU state") Signed-off-by: Brian Geffon <bgeffon@google.com> Signed-off-by: Willis Kung <williskung@google.com> Tested-by: Willis Kung <williskung@google.com> Cc: <stable@vger.kernel.org> # v5.4.x Cc: <stable@vger.kernel.org> # v5.10.x Acked-by: Dave Hansen <dave.hansen@linux.intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 68f1984 commit bae7fc6

3 files changed

Lines changed: 12 additions & 13 deletions

File tree

‎arch/x86/include/asm/fpu/internal.h‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,11 @@ static inline void __fpregs_load_activate(void)
531531
* The FPU context is only stored/restored for a user task and
532532
* PF_KTHREAD is used to distinguish between kernel and user threads.
533533
*/
534-
static inline void switch_fpu_prepare(struct fpu *old_fpu, int cpu)
534+
static inline void switch_fpu_prepare(struct task_struct *prev, int cpu)
535535
{
536-
if (static_cpu_has(X86_FEATURE_FPU) && !(current->flags & PF_KTHREAD)) {
536+
struct fpu *old_fpu = &prev->thread.fpu;
537+
538+
if (static_cpu_has(X86_FEATURE_FPU) && !(prev->flags & PF_KTHREAD)) {
537539
if (!copy_fpregs_to_fpstate(old_fpu))
538540
old_fpu->last_cpu = -1;
539541
else
@@ -552,10 +554,11 @@ static inline void switch_fpu_prepare(struct fpu *old_fpu, int cpu)
552554
* Load PKRU from the FPU context if available. Delay loading of the
553555
* complete FPU state until the return to userland.
554556
*/
555-
static inline void switch_fpu_finish(struct fpu *new_fpu)
557+
static inline void switch_fpu_finish(struct task_struct *next)
556558
{
557559
u32 pkru_val = init_pkru_value;
558560
struct pkru_state *pk;
561+
struct fpu *next_fpu = &next->thread.fpu;
559562

560563
if (!static_cpu_has(X86_FEATURE_FPU))
561564
return;
@@ -569,7 +572,7 @@ static inline void switch_fpu_finish(struct fpu *new_fpu)
569572
* PKRU state is switched eagerly because it needs to be valid before we
570573
* return to userland e.g. for a copy_to_user() operation.
571574
*/
572-
if (!(current->flags & PF_KTHREAD)) {
575+
if (!(next->flags & PF_KTHREAD)) {
573576
/*
574577
* If the PKRU bit in xsave.header.xfeatures is not set,
575578
* then the PKRU component was in init state, which means
@@ -578,7 +581,7 @@ static inline void switch_fpu_finish(struct fpu *new_fpu)
578581
* in memory is not valid. This means pkru_val has to be
579582
* set to 0 and not to init_pkru_value.
580583
*/
581-
pk = get_xsave_addr(&new_fpu->state.xsave, XFEATURE_PKRU);
584+
pk = get_xsave_addr(&next_fpu->state.xsave, XFEATURE_PKRU);
582585
pkru_val = pk ? pk->pkru : 0;
583586
}
584587
__write_pkru(pkru_val);

‎arch/x86/kernel/process_32.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,12 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
159159
{
160160
struct thread_struct *prev = &prev_p->thread,
161161
*next = &next_p->thread;
162-
struct fpu *prev_fpu = &prev->fpu;
163-
struct fpu *next_fpu = &next->fpu;
164162
int cpu = smp_processor_id();
165163

166164
/* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
167165

168166
if (!test_thread_flag(TIF_NEED_FPU_LOAD))
169-
switch_fpu_prepare(prev_fpu, cpu);
167+
switch_fpu_prepare(prev_p, cpu);
170168

171169
/*
172170
* Save away %gs. No need to save %fs, as it was saved on the
@@ -213,7 +211,7 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
213211

214212
this_cpu_write(current_task, next_p);
215213

216-
switch_fpu_finish(next_fpu);
214+
switch_fpu_finish(next_p);
217215

218216
/* Load the Intel cache allocation PQR MSR. */
219217
resctrl_sched_in();

‎arch/x86/kernel/process_64.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,15 +535,13 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
535535
{
536536
struct thread_struct *prev = &prev_p->thread;
537537
struct thread_struct *next = &next_p->thread;
538-
struct fpu *prev_fpu = &prev->fpu;
539-
struct fpu *next_fpu = &next->fpu;
540538
int cpu = smp_processor_id();
541539

542540
WARN_ON_ONCE(IS_ENABLED(CONFIG_DEBUG_ENTRY) &&
543541
this_cpu_read(irq_count) != -1);
544542

545543
if (!test_thread_flag(TIF_NEED_FPU_LOAD))
546-
switch_fpu_prepare(prev_fpu, cpu);
544+
switch_fpu_prepare(prev_p, cpu);
547545

548546
/* We must save %fs and %gs before load_TLS() because
549547
* %fs and %gs may be cleared by load_TLS().
@@ -595,7 +593,7 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
595593
this_cpu_write(current_task, next_p);
596594
this_cpu_write(cpu_current_top_of_stack, task_top_of_stack(next_p));
597595

598-
switch_fpu_finish(next_fpu);
596+
switch_fpu_finish(next_p);
599597

600598
/* Reload sp0. */
601599
update_task_stack(next_p);

0 commit comments

Comments
 (0)