@@ -107,7 +107,7 @@ static inline u32 __read_pkru(void)
return pkru;
}
-static inline void __write_pkru(u32 pkru)
+static inline void __write_pkru_insn(u32 pkru)
{
u32 ecx = 0, edx = 0;
@@ -118,6 +118,17 @@ static inline void __write_pkru(u32 pkru)
asm volatile(".byte 0x0f,0x01,0xef\n\t"
: : "a" (pkru), "c"(ecx), "d"(edx));
}
+
+static inline void __write_pkru(u32 pkru)
+{
+ /*
+ * Writting PKRU is expensive. Only write the PKRU value if it is
+ * different from the current one.
+ */
+ if (pkru == __read_pkru())
+ return;
+ __write_pkru_insn(pkru);
+}
#else
static inline u32 __read_pkru(void)
{
Dave Hansen says that the `wrpkru' is more expensive than `rdpkru'. It has a higher cycle cost and it's also practically a (light) speculation barrier. As an optimisation read the current PKRU value and only write the new one if it is different. Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> --- arch/x86/include/asm/special_insns.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)