Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1066404imu; Wed, 16 Jan 2019 12:09:06 -0800 (PST) X-Google-Smtp-Source: ALg8bN6Qz4rdx5JwQxI8RSI39QPCY7EsW1PBl8C1BaujSHOFs0BSnfhV18OqTUouwI67I+yt9Af0 X-Received: by 2002:a17:902:8484:: with SMTP id c4mr11387033plo.59.1547669346678; Wed, 16 Jan 2019 12:09:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547669346; cv=none; d=google.com; s=arc-20160816; b=K0Ern3UzEUzHT6/yKZ4rRnfXFoMf8SHT5YHt4nbKJ1NHDD9gp+gO4r3kIuaWT6TlwO WgpJOmhSxqUS2ezbFvj9f0OdRG3dXUw7FR0h5vwnayHY1sSvrBVZqRBGlaxOCvK2Lvfr ZqFEBTbKFl+fphSGDkauuL0U9SYJUvpiDYTqjddsi/VvaU81BMIRVzUXHkJAK/7mXjbL aYDx921DvclZLJ+DKCSoMKUJ+j/s7xa0hr6K6zzyn81YT1fkh0Cybn3pEQmv/P9d+gfA M5LC1RADpuc/GAsWXCDlPkHzDXxDds8uabMR6FUcJFdX4e5dkbLGMQHzNUNK7fHeQBeU Tqgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date; bh=/foIuqlA9CtWYSJrtpX+SSQx/rjRlrCsNEGU6C4ThmQ=; b=0hFX25tVsU9vR8xUAYhgnzMDEwP43TtXA7WAAZLlbuHW8n8yaGifCQxQu0QiD8Nk7b oY5ofS/9NT6e55JyN1pZu4XqNhPMLgafHbh7ExchOE5Eqg3F9EyNxGbMFkxUM0OnyRCY fhwMVykpSa3ANaAb+dJZYJduhQQ1jvuzHb0PZPLh6NLylWr5xx88mq+Ee4HdYhM18jah /sfhEYcFlP0/trGKsjioVIVqhBN89+BEBJUlgJWzzUva0ci5yJHTHW4J3novM7OQXBfQ ZsMJeHvo+SEmz4j8uQDr2lX0E8FND5cLJ0JlZteqKeAkw3wpzbwlFukdv//aMmfKxu7t Hy3w== 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 e7si42647pgv.499.2019.01.16.12.08.47; Wed, 16 Jan 2019 12:09:06 -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 S2389912AbfAPMAA (ORCPT + 99 others); Wed, 16 Jan 2019 07:00:00 -0500 Received: from terminus.zytor.com ([198.137.202.136]:49979 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389800AbfAPL77 (ORCPT ); Wed, 16 Jan 2019 06:59:59 -0500 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTPS id x0GBxpNU253067 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Wed, 16 Jan 2019 03:59:51 -0800 Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id x0GBxosF253064; Wed, 16 Jan 2019 03:59:50 -0800 Date: Wed, 16 Jan 2019 03:59:50 -0800 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for Borislav Petkov Message-ID: Cc: mingo@kernel.org, luto@kernel.org, linux-kernel@vger.kernel.org, thomas.lendacky@amd.com, john.stultz@linaro.org, hpa@zytor.com, tglx@linutronix.de, bp@suse.de Reply-To: luto@kernel.org, mingo@kernel.org, linux-kernel@vger.kernel.org, thomas.lendacky@amd.com, john.stultz@linaro.org, hpa@zytor.com, bp@suse.de, tglx@linutronix.de In-Reply-To: <20181119184556.11479-1-bp@alien8.de> References: <20181119184556.11479-1-bp@alien8.de> To: linux-tip-commits@vger.kernel.org Subject: [tip:x86/alternatives] x86/TSC: Use RDTSCP Git-Commit-ID: 093ae8f9a86a974c920b613860f1f7fd5bbd70ab X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=0.3 required=5.0 tests=ALL_TRUSTED,BAYES_00, DATE_IN_FUTURE_12_24 autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 093ae8f9a86a974c920b613860f1f7fd5bbd70ab Gitweb: https://git.kernel.org/tip/093ae8f9a86a974c920b613860f1f7fd5bbd70ab Author: Borislav Petkov AuthorDate: Thu, 12 Apr 2018 13:11:36 +0200 Committer: Borislav Petkov CommitDate: Wed, 16 Jan 2019 12:43:08 +0100 x86/TSC: Use RDTSCP Currently, the kernel uses [LM]FENCE; RDTSC in the timekeeping code, to guarantee monotonicity of time where the *FENCE is selected based on vendor. Replace that sequence with RDTSCP which is faster or on-par and gives the same guarantees. A microbenchmark on Intel shows that the change is on-par. On AMD, the change is either on-par with the current LFENCE-prefixed RDTSC or slightly better with RDTSCP. The comparison is done with the LFENCE-prefixed RDTSC (and not with the MFENCE-prefixed one, as one would normally expect) because all modern AMD families make LFENCE serializing and thus avoid the heavy MFENCE by effectively enabling X86_FEATURE_LFENCE_RDTSC. Co-developed-by: Thomas Gleixner Signed-off-by: Thomas Gleixner Signed-off-by: Borislav Petkov Cc: Tom Lendacky Cc: Andy Lutomirski Cc: "H. Peter Anvin" Cc: John Stultz Cc: x86@kernel.org Link: https://lkml.kernel.org/r/20181119184556.11479-1-bp@alien8.de --- arch/x86/include/asm/msr.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/msr.h b/arch/x86/include/asm/msr.h index 91e4cf189914..5cc3930cb465 100644 --- a/arch/x86/include/asm/msr.h +++ b/arch/x86/include/asm/msr.h @@ -217,6 +217,8 @@ static __always_inline unsigned long long rdtsc(void) */ static __always_inline unsigned long long rdtsc_ordered(void) { + DECLARE_ARGS(val, low, high); + /* * The RDTSC instruction is not ordered relative to memory * access. The Intel SDM and the AMD APM are both vague on this @@ -227,9 +229,19 @@ static __always_inline unsigned long long rdtsc_ordered(void) * ordering guarantees as reading from a global memory location * that some other imaginary CPU is updating continuously with a * time stamp. + * + * Thus, use the preferred barrier on the respective CPU, aiming for + * RDTSCP as the default. */ - barrier_nospec(); - return rdtsc(); + asm volatile(ALTERNATIVE_3("rdtsc", + "mfence; rdtsc", X86_FEATURE_MFENCE_RDTSC, + "lfence; rdtsc", X86_FEATURE_LFENCE_RDTSC, + "rdtscp", X86_FEATURE_RDTSCP) + : EAX_EDX_RET(val, low, high) + /* RDTSCP clobbers ECX with MSR_TSC_AUX. */ + :: "ecx"); + + return EAX_EDX_VAL(val, low, high); } static inline unsigned long long native_read_pmc(int counter)