From mboxrd@z Thu Jan 1 00:00:00 1970 From: Sebastian Andrzej Siewior Subject: [PATCH 13/27] x86/fpu: Only write PKRU if it is different from current Date: Wed, 3 Apr 2019 18:41:42 +0200 Message-ID: <20190403164156.19645-14-bigeasy@linutronix.de> References: <20190403164156.19645-1-bigeasy@linutronix.de> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Cc: x86@kernel.org, Andy Lutomirski , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , kvm@vger.kernel.org, "Jason A. Donenfeld" , Rik van Riel , Dave Hansen , Sebastian Andrzej Siewior To: linux-kernel@vger.kernel.org Return-path: In-Reply-To: <20190403164156.19645-1-bigeasy@linutronix.de> Sender: linux-kernel-owner@vger.kernel.org List-Id: kvm.vger.kernel.org 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 --- arch/x86/include/asm/special_insns.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arch/x86/include/asm/special_insns.h b/arch/x86/include/asm/special_insns.h index 27328606ff687..28ffdf0c1add4 100644 --- a/arch/x86/include/asm/special_insns.h +++ b/arch/x86/include/asm/special_insns.h @@ -121,6 +121,12 @@ static inline void __write_pkru_ins(u32 pkru) static inline void __write_pkru(u32 pkru) { + /* + * WRPKRU is relatively expensive compared to RDPKRU. + * Avoid WRPKRU when it would not change the value. + */ + if (pkru == __read_pkru_ins()) + return; __write_pkru_ins(pkru); } -- 2.20.1