Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1739381imu; Wed, 28 Nov 2018 14:23:53 -0800 (PST) X-Google-Smtp-Source: AFSGD/WLM4QrrvZq6ETWvfW5To29soqVroQOOKbguRNmjHEpcMZ42NPo8IVVhZ1b/S4Dg9a4e0A1 X-Received: by 2002:a65:4049:: with SMTP id h9mr34387684pgp.304.1543443833050; Wed, 28 Nov 2018 14:23:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543443833; cv=none; d=google.com; s=arc-20160816; b=nYuHs2m260wTmus/CGw1K5wsmyWzpQfRB1qWDQB+J5/qfW2Oo2ZO5k/RbUF26yryrk fNdZq9RmWEj2IxJJQDldr71XIQ4BYJ5IoBjuGqfeWE2P7HBVFGSSdYqi5eodWJiIqwsb XvWEWVJ0Z5oF64812mfk8XAoTs1SC2CJKfkiqVhTFaf2UpCjFfdguYWCilin4l0rYuYU qXNdGkTupLpEweVYxiLITUQaR6QEUbNjo2I3Ux8B/dZ653er7NaqIe6UQ51BXbuJ0CeW 716qihFRnNNVpUGzRhJ8w6kbopi1A2q/T8J/w4iL0DEa70dDjenoEapcJf9Xov4RvZnL lALQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=n3FfoeWLG/98FXHfvooPQ7D1JZtw0AYbVeuijknPKho=; b=cE41hcrrnaPTftfkfM7+51b3OwkAfctDZMoZ5diZ1jWKKMOp+mjlgFiJRMIL2yhOfD WfQhnT3G+FJ6AM9RBZJn5tLYlbRZXEEusxjjwSuRThABVe8gnoVIUjv/kgYx8ndyVvtT A4UjqMXAZTBJdOecZdZSnQOzhBVhKZpMsiB9EOdisChkmmZGiVnOGTV4u2rIn+4rqDMB Ts/Lg5DBVxAl79bWCPxLhioZbUzltDXNuyTUxEZdbopEzupQr2q/Y4A0p73VjZWkfDzn F51l995MbmMBT5a/JFDxAbFLmSwqf0TaQzAxeHJnVhcIk1vscKLcKM3CfAvvDBMO7Q81 dK+Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g2si8243666pgk.497.2018.11.28.14.23.38; Wed, 28 Nov 2018 14:23:53 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727127AbeK2JYa (ORCPT + 99 others); Thu, 29 Nov 2018 04:24:30 -0500 Received: from Galois.linutronix.de ([146.0.238.70]:33178 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726870AbeK2JY3 (ORCPT ); Thu, 29 Nov 2018 04:24:29 -0500 Received: from localhost ([127.0.0.1] helo=bazinga.breakpoint.cc) by Galois.linutronix.de with esmtp (Exim 4.80) (envelope-from ) id 1gS8Co-0001GX-CB; Wed, 28 Nov 2018 23:21:14 +0100 From: Sebastian Andrzej Siewior To: linux-kernel@vger.kernel.org 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 Subject: [PATCH 19/29] x86/fpu: Only write PKRU if it is different from current Date: Wed, 28 Nov 2018 23:20:25 +0100 Message-Id: <20181128222035.2996-20-bigeasy@linutronix.de> X-Mailer: git-send-email 2.20.0.rc1 In-Reply-To: <20181128222035.2996-1-bigeasy@linutronix.de> References: <20181128222035.2996-1-bigeasy@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/special_insns.h b/arch/x86/include/asm/special_insns.h index 43c029cdc3fe8..c2ccf71b22dd6 100644 --- a/arch/x86/include/asm/special_insns.h +++ b/arch/x86/include/asm/special_insns.h @@ -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) { -- 2.20.0.rc1