aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Martin <Dave.Martin@arm.com>2017-12-06 11:45:47 -0500
committerWill Deacon <will.deacon@arm.com>2017-12-06 14:08:05 -0500
commitcb968afc789821cdf9e17e79ef08ab90e5bae0f2 (patch)
treebfebdce5e2dd33cc1c05aa9dbd574bd8cc933b6b
parentd96cc49bff5a7735576cc6f6f111f875d101cec8 (diff)
arm64/sve: Avoid dereference of dead task_struct in KVM guest entry
When deciding whether to invalidate FPSIMD state cached in the cpu, the backend function sve_flush_cpu_state() attempts to dereference __this_cpu_read(fpsimd_last_state). However, this is not safe: there is no guarantee that this task_struct pointer is still valid, because the task could have exited in the meantime. This means that we need another means to get the appropriate value of TIF_SVE for the associated task. This patch solves this issue by adding a cached copy of the TIF_SVE flag in fpsimd_last_state, which we can check without dereferencing the task pointer. In particular, although this patch is not a KVM fix per se, this means that this check is now done safely in the KVM world switch path (which is currently the only user of this code). Signed-off-by: Dave Martin <Dave.Martin@arm.com> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org> Cc: Christoffer Dall <christoffer.dall@linaro.org> Cc: Marc Zyngier <marc.zyngier@arm.com> Signed-off-by: Will Deacon <will.deacon@arm.com>
-rw-r--r--arch/arm64/kernel/fpsimd.c28
1 files changed, 16 insertions, 12 deletions
diff --git a/arch/arm64/kernel/fpsimd.c b/arch/arm64/kernel/fpsimd.c
index e330cc0b573d..540a1e010eb5 100644
--- a/arch/arm64/kernel/fpsimd.c
+++ b/arch/arm64/kernel/fpsimd.c
@@ -114,7 +114,12 @@
114 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so 114 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
115 * whatever is in the FPSIMD registers is not saved to memory, but discarded. 115 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
116 */ 116 */
117static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state); 117struct fpsimd_last_state_struct {
118 struct fpsimd_state *st;
119 bool sve_in_use;
120};
121
122static DEFINE_PER_CPU(struct fpsimd_last_state_struct, fpsimd_last_state);
118 123
119/* Default VL for tasks that don't set it explicitly: */ 124/* Default VL for tasks that don't set it explicitly: */
120static int sve_default_vl = -1; 125static int sve_default_vl = -1;
@@ -905,7 +910,7 @@ void fpsimd_thread_switch(struct task_struct *next)
905 */ 910 */
906 struct fpsimd_state *st = &next->thread.fpsimd_state; 911 struct fpsimd_state *st = &next->thread.fpsimd_state;
907 912
908 if (__this_cpu_read(fpsimd_last_state) == st 913 if (__this_cpu_read(fpsimd_last_state.st) == st
909 && st->cpu == smp_processor_id()) 914 && st->cpu == smp_processor_id())
910 clear_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE); 915 clear_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE);
911 else 916 else
@@ -997,9 +1002,12 @@ void fpsimd_signal_preserve_current_state(void)
997 */ 1002 */
998static void fpsimd_bind_to_cpu(void) 1003static void fpsimd_bind_to_cpu(void)
999{ 1004{
1005 struct fpsimd_last_state_struct *last =
1006 this_cpu_ptr(&fpsimd_last_state);
1000 struct fpsimd_state *st = &current->thread.fpsimd_state; 1007 struct fpsimd_state *st = &current->thread.fpsimd_state;
1001 1008
1002 __this_cpu_write(fpsimd_last_state, st); 1009 last->st = st;
1010 last->sve_in_use = test_thread_flag(TIF_SVE);
1003 st->cpu = smp_processor_id(); 1011 st->cpu = smp_processor_id();
1004} 1012}
1005 1013
@@ -1057,7 +1065,7 @@ void fpsimd_flush_task_state(struct task_struct *t)
1057 1065
1058static inline void fpsimd_flush_cpu_state(void) 1066static inline void fpsimd_flush_cpu_state(void)
1059{ 1067{
1060 __this_cpu_write(fpsimd_last_state, NULL); 1068 __this_cpu_write(fpsimd_last_state.st, NULL);
1061} 1069}
1062 1070
1063/* 1071/*
@@ -1070,14 +1078,10 @@ static inline void fpsimd_flush_cpu_state(void)
1070#ifdef CONFIG_ARM64_SVE 1078#ifdef CONFIG_ARM64_SVE
1071void sve_flush_cpu_state(void) 1079void sve_flush_cpu_state(void)
1072{ 1080{
1073 struct fpsimd_state *const fpstate = __this_cpu_read(fpsimd_last_state); 1081 struct fpsimd_last_state_struct const *last =
1074 struct task_struct *tsk; 1082 this_cpu_ptr(&fpsimd_last_state);
1075
1076 if (!fpstate)
1077 return;
1078 1083
1079 tsk = container_of(fpstate, struct task_struct, thread.fpsimd_state); 1084 if (last->st && last->sve_in_use)
1080 if (test_tsk_thread_flag(tsk, TIF_SVE))
1081 fpsimd_flush_cpu_state(); 1085 fpsimd_flush_cpu_state();
1082} 1086}
1083#endif /* CONFIG_ARM64_SVE */ 1087#endif /* CONFIG_ARM64_SVE */
@@ -1272,7 +1276,7 @@ static inline void fpsimd_pm_init(void) { }
1272#ifdef CONFIG_HOTPLUG_CPU 1276#ifdef CONFIG_HOTPLUG_CPU
1273static int fpsimd_cpu_dead(unsigned int cpu) 1277static int fpsimd_cpu_dead(unsigned int cpu)
1274{ 1278{
1275 per_cpu(fpsimd_last_state, cpu) = NULL; 1279 per_cpu(fpsimd_last_state.st, cpu) = NULL;
1276 return 0; 1280 return 0;
1277} 1281}
1278 1282