Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp904340imu; Fri, 7 Dec 2018 10:41:47 -0800 (PST) X-Google-Smtp-Source: AFSGD/WNW1lJV70n/jbytcAVeUSUlrLseqr3pUpd0usYtVth0lrjN7Uxw/L/YaucYkDRKqMx6Wgd X-Received: by 2002:a17:902:29a7:: with SMTP id h36mr3226158plb.244.1544208107628; Fri, 07 Dec 2018 10:41:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544208107; cv=none; d=google.com; s=arc-20160816; b=r1SNX6vbKVRdT8THPjO5OZnrV4mxzQdH4+rsMT58KDrwX/tkhSHscOjrj2EODBVdT1 4N7/BSitPGEaRtyw5C0mYspQUfKHfjC14tXaXrli5qKpV2rxz3ss4LAuO8YDNYU7GPXy P3nHB13UC84M23AdeWPGP2u0W41cf6khS9xzf3BaFPZQzW7Hgu/aOXnMMK3uh0iPh+g3 zHw7hQmQG+BJ7Cz8TEoS8QUjJm8gUnijQwwgWLzjdp1hF3M+9OuALGEc9N/uis42t0Ew z3B+6LtqKBzfrQ/KqjHRKUc1iTYIfjsV5uskSi0BnAtBcBdc8pMPT80Et5+BufA6Hsb2 Z7kA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=XUKpLqj7/xXg9jrI/lIGAqGvEsgVQSKGIb3E3fRsWzM=; b=lZGvDLhpFVKhmdarY+C+NzdZRZE61/32AQksnPu8yeJavqVj4ArUlzZpExvsXCue/5 F5jEqQq0VWitfYWYuCbnl4ETvojTeOoQeAl8EiPdJgCtJ0E1jfcQqnCl27uQZr7eG5s+ OWPdQiXTJ7nGQkddKO2SSI7oryQkkQoBNuPmWLHErZG4cJGoxsHM1BK1KmiJCcjUsbNY Tg5aAduG/ryP+NtnMAq3tgxwLtdP6vqswLkM2z0F/KIYzM5v3tMAiMq7+Yv6WGRpUwf2 m11f51BxfTsZs8FskmCkXRCW58go7QySo4emzeQ0XRsDXOd1IWCldAZ0cyaQfVDX3th2 y7dQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b=b5wItN3S; 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=REJECT sp=REJECT dis=NONE) header.from=alien8.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h20si3464989pgm.366.2018.12.07.10.41.32; Fri, 07 Dec 2018 10:41:47 -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=@alien8.de header.s=dkim header.b=b5wItN3S; 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=REJECT sp=REJECT dis=NONE) header.from=alien8.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726120AbeLGSjn (ORCPT + 99 others); Fri, 7 Dec 2018 13:39:43 -0500 Received: from mail.skyhub.de ([5.9.137.197]:45374 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726011AbeLGSjn (ORCPT ); Fri, 7 Dec 2018 13:39:43 -0500 Received: from zn.tnic (p200300EC2BCEC900C13752FC2028AE4C.dip0.t-ipconnect.de [IPv6:2003:ec:2bce:c900:c137:52fc:2028:ae4c]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id A20BD1EC0BF1; Fri, 7 Dec 2018 19:39:41 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1544207981; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references; bh=XUKpLqj7/xXg9jrI/lIGAqGvEsgVQSKGIb3E3fRsWzM=; b=b5wItN3SwdasANuLmiz4/mUehomJUuovnbv3/wVQykDxDRROUyfxrQmt4PrEJWGl9M8o44 VFwCb17KwiL/D1Xkz/iOZBFHSCSG1I8/z/5mfI+xMa8k7iWYLoZtkoDmKar55jONfrS3r3 CLRZITscbBwkEVXEwOfi2SEKlR5Fg8c= Date: Fri, 7 Dec 2018 19:39:34 +0100 From: Borislav Petkov To: Guenter Roeck Cc: X86 ML , LKML , Andy Lutomirski , "H. Peter Anvin" , John Stultz , Thomas Lendacky Subject: Re: [PATCH] x86/TSC: Use RDTSCP Message-ID: <20181207183934.GF9385@zn.tnic> References: <20181119184556.11479-1-bp@alien8.de> <20181123200307.GA6223@roeck-us.net> <20181123204425.GN30697@zn.tnic> <901af2a0-e0d7-586d-5f04-2066cf1ac871@roeck-us.net> <20181123210739.GO30697@zn.tnic> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20181123210739.GO30697@zn.tnic> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Ok, I have something which looks like it works here, even with the pentium3 qemu CPU. I'll be hammering on it in the coming days but if you wanna give it a try, here's a conglomerate patch: --- From 0c6adce02d2e7f3b5bbdc4cbe3eb3dae99448def Mon Sep 17 00:00:00 2001 From: Borislav Petkov Date: Fri, 7 Dec 2018 18:54:23 +0100 Subject: [PATCH] WIP Signed-off-by: Borislav Petkov --- arch/x86/include/asm/alternative.h | 26 +++++++++++++++++++++++++- arch/x86/include/asm/msr.h | 16 ++++++++++++++-- arch/x86/kernel/alternative.c | 4 ++-- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alternative.h index ea9886651c39..db8ebe5dd5be 100644 --- a/arch/x86/include/asm/alternative.h +++ b/arch/x86/include/asm/alternative.h @@ -114,6 +114,16 @@ static inline int alternatives_text_reserved(void *start, void *end) "(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n" \ alt_end_marker ":\n" +#define OLDINSTR_3(oldinsn, n1, n2, n3) \ + "# ALT: oldinstr\n" \ + "661:\n\t" oldinsn "\n662:\n" \ + "# ALT: padding\n" \ + ".skip -((" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3)) \ + " - (" alt_slen ")) > 0) * " \ + "(" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3)) \ + " - (" alt_slen ")), 0x90\n" \ + alt_end_marker ":\n" + #define ALTINSTR_ENTRY(feature, num) \ " .long 661b - .\n" /* label */ \ " .long " b_replacement(num)"f - .\n" /* new instruction */ \ @@ -122,7 +132,8 @@ static inline int alternatives_text_reserved(void *start, void *end) " .byte " alt_rlen(num) "\n" /* replacement len */ \ " .byte " alt_pad_len "\n" /* pad len */ -#define ALTINSTR_REPLACEMENT(newinstr, feature, num) /* replacement */ \ +#define ALTINSTR_REPLACEMENT(newinstr, feature, num) /* replacement */ \ + "# ALT: replacement " #num "\n" \ b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n\t" /* alternative assembly primitive: */ @@ -146,6 +157,19 @@ static inline int alternatives_text_reserved(void *start, void *end) ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \ ".popsection\n" +#define ALTERNATIVE_3(oldinsn, newinsn1, feat1, newinsn2, feat2, newinsn3, feat3) \ + OLDINSTR_3(oldinsn, 1, 2, 3) \ + ".pushsection .altinstructions,\"a\"\n" \ + ALTINSTR_ENTRY(feat1, 1) \ + ALTINSTR_ENTRY(feat2, 2) \ + ALTINSTR_ENTRY(feat3, 3) \ + ".popsection\n" \ + ".pushsection .altinstr_replacement, \"ax\"\n" \ + ALTINSTR_REPLACEMENT(newinsn1, feat1, 1) \ + ALTINSTR_REPLACEMENT(newinsn2, feat2, 2) \ + ALTINSTR_REPLACEMENT(newinsn3, feat3, 3) \ + ".popsection\n" + /* * Alternative instructions for different CPU types or capabilities. * 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) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index ebeac487a20c..d458c7973c56 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -393,10 +393,10 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start, continue; } - DPRINTK("feat: %d*32+%d, old: (%px len: %d), repl: (%px, len: %d), pad: %d", + DPRINTK("feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d), pad: %d", a->cpuid >> 5, a->cpuid & 0x1f, - instr, a->instrlen, + instr, instr, a->instrlen, replacement, a->replacementlen, a->padlen); DUMP_BYTES(instr, a->instrlen, "%px: old_insn: ", instr); -- 2.19.1 -- Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.