Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1214015imu; Tue, 11 Dec 2018 15:01:10 -0800 (PST) X-Google-Smtp-Source: AFSGD/WYI/jcXqD14Bb1+KABNAEdsLpc83sJizUxw3WmetwM42ZT7owJkuwsHeyfaNipupVsZDLQ X-Received: by 2002:a63:4f20:: with SMTP id d32mr16274215pgb.47.1544569270666; Tue, 11 Dec 2018 15:01:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544569270; cv=none; d=google.com; s=arc-20160816; b=RNcbTruLLPlUlwnpHoQ0SjS8cz+zspXgnm35TZnAWrBxXtgPtZIq/Td3CxYKNMKF8U TiMQUXyoy83K/3q/NV30bNbRQuVWt9GJoehYC6wIy7DUOqXVWrrNznl5t7/Pv93ykEiA KEYXDGlmJfb5qi3Yxwp8pm/R6B8dZ0yT1uLiMh0T7rNgsJE6fd1hZH1i+vyI8YLIX3ag zXSM5a50bVyJ+/+Jp5JL3pgN1JtIXD3JvSWVHunYYEMmuWO3TFFgWFx7hSoWG4Ic3Hcz lAt+LZapyf3Pn21cCIaqQzdVgzm6LV4pErgrDYp2Idu+7dpKMRFP65srOHNLsRRukqbk xu0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=lrtE4fsdTwpWh7uYKptppLDCLroIfC6m/UnV7Zbl3uM=; b=vp3WFRsrJ4ybIv6Rl5BChxbszLf099kApFSHamh8r0X3HvyDIKmMQTvxC8rr173+OR zM+Cr8Uci3XgQlopy15vyxtfGsbmMaln/sbln0mE41z4QXJLhG/fEgC5n7h6wCQv7bw3 P96O0hpy7gNxjrx78ywlh4Mcdzl/XhmcHPDv10NJzJzVbMlfR9ukA1HaShp9S/9QXlVB N/gNYFEVQleeUzW+YmYXeGOplLv75QJB59qTlwAu8C/E+F7yovRjdROez+oDJhXj3H8Y RtKwUC3VVWfkHs6t0iUKSUd3flLe3lR2f3fOoMhEhI6H+sqXENpZI9cC70FCzjHFvCb5 bNbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=x+IGJcdp; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e3si13971997pfe.203.2018.12.11.15.00.32; Tue, 11 Dec 2018 15:01:10 -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; dkim=pass header.i=@kernel.org header.s=default header.b=x+IGJcdp; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726241AbeLKW7l (ORCPT + 99 others); Tue, 11 Dec 2018 17:59:41 -0500 Received: from mail.kernel.org ([198.145.29.99]:46118 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726211AbeLKW7k (ORCPT ); Tue, 11 Dec 2018 17:59:40 -0500 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9EBBB208E7 for ; Tue, 11 Dec 2018 22:59:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544569179; bh=B9K8JF1KEzLFQkdF+d99IKGOrMfXbxoV91CfNwK6mB4=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=x+IGJcdpD1VVeRM5WhLODI27IQcV8w7lF+Pb9DlThZ9HXwIToVLYTg/38lhihXWlM TnFXBTJHECEN0My4OkliOG8+shHROyb8WtxcJrQXd9FNUS7F7XVK/G4s/jUTa5YpIV jPOrUgojPfoieFf9NiLNhzICPo0dVvnBrLueAROg= Received: by mail-wr1-f47.google.com with SMTP id v13so15799654wrw.5 for ; Tue, 11 Dec 2018 14:59:39 -0800 (PST) X-Gm-Message-State: AA+aEWaweWVQ7TGtLWy/JF+TCOmAMf+j/LVIcWmQIemzk9wQCcxwzc0y xeHpD52v60TJmT1hZXziY9vlevym7CxVLg1OIPRXOA== X-Received: by 2002:a5d:5502:: with SMTP id b2mr15854756wrv.330.1544569178088; Tue, 11 Dec 2018 14:59:38 -0800 (PST) MIME-Version: 1.0 References: <20181211222326.14581-1-bp@alien8.de> <20181211222326.14581-5-bp@alien8.de> In-Reply-To: <20181211222326.14581-5-bp@alien8.de> From: Andy Lutomirski Date: Tue, 11 Dec 2018 14:59:26 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC PATCH 4/4] x86/TSC: Use RDTSCP To: Borislav Petkov Cc: LKML , X86 ML , "H. Peter Anvin" , Josh Poimboeuf , Peter Zijlstra , Tom Lendacky , Andrew Lutomirski , John Stultz Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Dec 11, 2018 at 2:23 PM Borislav Petkov wrote: > > From: Borislav Petkov > > 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 and some are 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); > } This whole series seems reasonable, except that it caused me to look at barrier_nospec(). And I had a bit of a WTF moment, as in "WTF does RDTSC have to do with a speculation protection barrier". Does it actually make sense?