[PATCH 01/17] x86, fpu: wrap get_xsave_addr() to make it safer
From: Dave Hansen
Date: Wed Apr 22 2015 - 14:27:27 EST
From: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
Changes from "v19":
* remove 'tsk' argument to get_xsave_addr() since the code
can only realistically work on 'current', and fix up the
comment a bit to match.
Changes from "v17":
* fix s/xstate/xsave_field/ in the function comment
* remove EXPORT_SYMBOL_GPL()
---
From: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
The MPX code appears to be saving off the FPU in an unsafe
way. It does not disable preemption or ensure that the
FPU state has been allocated.
This patch introduces a new helper which will do both of
those things internally.
Note that this requires a patch from Oleg in order to work
properly. It is currently in tip/x86/fpu.
> commit f893959b0898bd876673adbeb6798bdf25c034d7
> Author: Oleg Nesterov <oleg@xxxxxxxxxx>
> Date: Fri Mar 13 18:30:30 2015 +0100
>
> x86/fpu: Don't abuse drop_init_fpu() in flush_thread()
Signed-off-by: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
Cc: Oleg Nesterov <oleg@xxxxxxxxxx>
Cc: bp@xxxxxxxxx
Cc: Rik van Riel <riel@xxxxxxxxxx>
Cc: Suresh Siddha <sbsiddha@xxxxxxxxx>
Cc: Andy Lutomirski <luto@xxxxxxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: "H. Peter Anvin" <hpa@xxxxxxxxx>
Cc: Fenghua Yu <fenghua.yu@xxxxxxxxx>
Cc: the arch/x86 maintainers <x86@xxxxxxxxxx>
---
b/arch/x86/include/asm/xsave.h | 1 +
b/arch/x86/kernel/xsave.c | 32 ++++++++++++++++++++++++++++++++
2 files changed, 33 insertions(+)
diff -puN arch/x86/include/asm/xsave.h~tsk_get_xsave_addr arch/x86/include/asm/xsave.h
--- a/arch/x86/include/asm/xsave.h~tsk_get_xsave_addr 2015-04-22 11:16:17.781800602 -0700
+++ b/arch/x86/include/asm/xsave.h 2015-04-22 11:16:17.786800827 -0700
@@ -252,6 +252,7 @@ static inline int xrestore_user(struct x
}
void *get_xsave_addr(struct xsave_struct *xsave, int xstate);
+void *get_xsave_field(int xstate_field);
void setup_xstate_comp(void);
#endif
diff -puN arch/x86/kernel/xsave.c~tsk_get_xsave_addr arch/x86/kernel/xsave.c
--- a/arch/x86/kernel/xsave.c~tsk_get_xsave_addr 2015-04-22 11:16:17.782800647 -0700
+++ b/arch/x86/kernel/xsave.c 2015-04-22 11:16:17.786800827 -0700
@@ -741,3 +741,35 @@ void *get_xsave_addr(struct xsave_struct
return (void *)xsave + xstate_comp_offsets[feature];
}
EXPORT_SYMBOL_GPL(get_xsave_addr);
+
+/*
+ * This wraps up the common operations that need to occur when retrieving
+ * data from xsave state. It first ensures that the current task was
+ * using the FPU and retrieves the data in to a buffer. It then calculates
+ * the offset of the requested field in the buffer.
+ *
+ * This function is safe to call whether the FPU is in use or not.
+ *
+ * Note that this only works on the current task.
+ *
+ * Inputs:
+ * @xsave_field: state which is defined in xsave.h (e.g. XSTATE_FP,
+ * XSTATE_SSE, etc...)
+ * Output:
+ * address of the state in the xsave area.
+ */
+void *get_xsave_field(int xsave_field)
+{
+ union thread_xstate *xstate;
+
+ if (!tsk_used_math(current))
+ return NULL;
+ /*
+ * unlazy_fpu() is poorly named and will actually
+ * save the xstate off in to the memory buffer.
+ */
+ unlazy_fpu(current);
+ xstate = current->thread.fpu.state;
+
+ return get_xsave_addr(&xstate->xsave, xsave_field);
+}
_
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/