Reapply "arm64: fpsimd: Implement lazy restore for kernel mode FPSIMD"
authorWill Deacon <will@kernel.org>
Wed, 22 May 2024 10:53:05 +0000 (11:53 +0100)
committerWill Deacon <will@kernel.org>
Wed, 22 May 2024 10:55:00 +0000 (11:55 +0100)
This reverts commit b8995a18417088bb53f87c49d200ec72a9dd4ec1.

Ard managed to reproduce the dm-crypt corruption problem and got to the
bottom of it, so re-apply the problematic patch in preparation for
fixing things properly.

Cc: stable@vger.kernel.org
Signed-off-by: Will Deacon <will@kernel.org>
arch/arm64/include/asm/processor.h
arch/arm64/kernel/fpsimd.c

index 4f122b932ccaccde0530b5abf9fc2bc9ee129226..f77371232d8c6d542c7df057feea2e21752f34f2 100644 (file)
@@ -171,6 +171,7 @@ struct thread_struct {
        struct debug_info       debug;          /* debugging */
 
        struct user_fpsimd_state        kernel_fpsimd_state;
+       unsigned int                    kernel_fpsimd_cpu;
 #ifdef CONFIG_ARM64_PTR_AUTH
        struct ptrauth_keys_user        keys_user;
 #ifdef CONFIG_ARM64_PTR_AUTH_KERNEL
index e9d70dc915117e545fbdec2b527339a1f1245637..ebb0158997cab5e32fcefe7ed50a043095ee9262 100644 (file)
@@ -1509,12 +1509,30 @@ void do_fpsimd_exc(unsigned long esr, struct pt_regs *regs)
 
 static void fpsimd_load_kernel_state(struct task_struct *task)
 {
+       struct cpu_fp_state *last = this_cpu_ptr(&fpsimd_last_state);
+
+       /*
+        * Elide the load if this CPU holds the most recent kernel mode
+        * FPSIMD context of the current task.
+        */
+       if (last->st == &task->thread.kernel_fpsimd_state &&
+           task->thread.kernel_fpsimd_cpu == smp_processor_id())
+               return;
+
        fpsimd_load_state(&task->thread.kernel_fpsimd_state);
 }
 
 static void fpsimd_save_kernel_state(struct task_struct *task)
 {
+       struct cpu_fp_state cpu_fp_state = {
+               .st             = &task->thread.kernel_fpsimd_state,
+               .to_save        = FP_STATE_FPSIMD,
+       };
+
        fpsimd_save_state(&task->thread.kernel_fpsimd_state);
+       fpsimd_bind_state_to_cpu(&cpu_fp_state);
+
+       task->thread.kernel_fpsimd_cpu = smp_processor_id();
 }
 
 void fpsimd_thread_switch(struct task_struct *next)