Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757508AbeAHWJr (ORCPT + 1 other); Mon, 8 Jan 2018 17:09:47 -0500 Received: from mail-cys01nam02on0050.outbound.protection.outlook.com ([104.47.37.50]:17664 "EHLO NAM02-CY1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1756948AbeAHWJo (ORCPT ); Mon, 8 Jan 2018 17:09:44 -0500 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Thomas.Lendacky@amd.com; From: Tom Lendacky Subject: [PATCH v2 2/2] x86/cpu/AMD: Use LFENCE_RDTSC in preference to MFENCE_RDTSC To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: Peter Zijlstra , Linus Torvalds , Dan Williams , Dave Hansen , Borislav Petkov , Thomas Gleixner , Tim Chen , Greg Kroah-Hartman , David Woodhouse , Paul Turner Date: Mon, 08 Jan 2018 16:09:32 -0600 Message-ID: <20180108220932.12580.52458.stgit@tlendack-t1.amdoffice.net> In-Reply-To: <20180108220912.12580.82330.stgit@tlendack-t1.amdoffice.net> References: <20180108220912.12580.82330.stgit@tlendack-t1.amdoffice.net> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: MWHPR1601CA0011.namprd16.prod.outlook.com (10.172.93.21) To BN6PR12MB1138.namprd12.prod.outlook.com (10.168.226.140) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 8977b015-bc75-49ba-d5b6-08d556e482be X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(48565401081)(4534020)(4602075)(4627115)(201703031133081)(201702281549075)(5600026)(4604075)(2017052603307)(7153060)(7193020);SRVR:BN6PR12MB1138; X-Microsoft-Exchange-Diagnostics: 1;BN6PR12MB1138;3:hspo7YuYLlBhjoRIh/lmI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eSavqtvGSrQiQrT1cOjPaJN/QV5bAFbCLguebnpjS9OQPyiEft2GbdCZijfa3d/jfq5jXDHfcJtzBrs+H36rqei7LMR6BHMlS1NnXxUq5nZIBIKi2+mwMe6F4o4AxqQfQcqTIgqPPv6XBstQsorUPq5nadaYDeGAbXLGOvcCHMJzoBk3+MlQe0RVymS1uhJba6yYhKtwQsQ58vdGTPGg2sVoiv9mH3m29FNjF1GcSb1jybHxRBPCChEJnpRxg75IdlE82fs0GPebQqHBXncrq/QRC0QPDS0XUPySVks1be7nKPRdRA;4:AvGKuCiN1pntyZbgG/0lo50FJUTNmY17rtkZVlS7kaxNBV0NzT5bEEoXUtpHchBw6uSW3ErFG5Y/6YgyhrrJvHvne7wbtzNaYUnAO+oEwcacyYJ0f7M75ezjP7D/EPw3G/mnQQ34wKund1EN3v/Fdx9v9k/7XPCrFOkToFohXrrUTfykGZe3WvhGkIAAl2pNYEptrOkM4nozM0FZmvSbsudB546ypqiSxZMIWAnny7m7kTczwbEdaKKeuVxz52X90+FQlQIcK/04680EpAMSBBEfHXlJOpPz4IzOz/pyHWul1H5yPBqoGrahMKLBQHbW X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040470)(2401047)(8121501046)(5005006)(3231023)(944501075)(3002001)(10201501046)(93006095)(93001095)(6055026)(6041268)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(20161123558120)(20161123564045)(6072148)(201708071742011);SRVR:BN6PR12MB1138;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:BN6PR12MB1138; X-Forefront-PRVS: 054642504A X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(396003)(366004)(346002)(39860400002)(376002)(39380400002)(199004)(189003)(53936002)(50466002)(47776003)(305945005)(7736002)(103116003)(478600001)(66066001)(72206003)(8936002)(1076002)(230700001)(2906002)(69596002)(86362001)(55016002)(68736007)(5660300001)(52116002)(9686003)(6116002)(58126008)(2486003)(53416004)(105586002)(76176011)(386003)(6506007)(97746001)(59450400001)(4326008)(316002)(8676002)(54906003)(2950100002)(6666003)(106356001)(23676004)(25786009)(83506002)(97736004)(7416002)(81166006)(81156014)(16526018)(7696005)(3846002);DIR:OUT;SFP:1101;SCL:1;SRVR:BN6PR12MB1138;H:tlendack-t1.amdoffice.net;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtCTjZQUjEyTUIxMTM4OzIzOkdSa0xLR0dxUVduYjE4ZFdHVCs1Tmxya1V5?= =?utf-8?B?REdoNTR0TlRKS01uTmFsMWU0SjZlaHR4aWFoWWJCQ0dkbHZOY1N0Q1hybWFD?= =?utf-8?B?d2pVU3VrRmxsVnRWQjZ4elI2YmVzVHBFazk5YlZqVHgyamtHZ2JhYnh2QmQx?= =?utf-8?B?eDIwaHhRakp2dGpWcU42alRNR2NsZDhtVHRFem01VUZRMEppVlJPOWFjOVNn?= =?utf-8?B?ZHJXZUJIN0VVS3RneGdKaG9VL2RlcHIyVE9LY2s2YnA1Ni9RMkJ1VEg3ckJL?= =?utf-8?B?VEZPdGNBeXdJSDRlcXYrSEQxUEhkbVJhZHIzeHhQNmNmekxROGdsdm5tYXFu?= =?utf-8?B?S0U0U1dGVFFuZ010UzB3T1FkbWFLRldTOFEzYjJvazFQaitWQlErb1hDUzZi?= =?utf-8?B?UlFoWnB3Yk0zNDhIUVVaenFmM1ZpNDBGZCtYZUZNOVNHdkdSaWFVSjZya2VQ?= =?utf-8?B?SG5SQjcvYnN3OFZEbHRJOGZnQml3MzgvbHl6a3lKUzNvUFk5ZDlUaW1YcTBK?= =?utf-8?B?a2NpVXYzTEhlRktueW0ycGk4clRSRWdjcVNlR1Y0QlNMNzlNRzR3UGQ4ZE9Z?= =?utf-8?B?MjY5djBvbEpxelZRTDd4TERaMzZaQk4yaW1Bc1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1fwZwuDE5K1HYIFP3LMuQ5N5D9KvbFU+JZ3isVXj12+8ns3kap/+VsUbb1ifuhXgzxEIY0oauBgjlLe0fSndrNtDf3pL9j4gW82/Xl1RAHBbFJ SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;BN6PR12MB1138;20:IWhIsp6cCznC5LyO2FGNDYxTcHVlSirSuuPYjkBoELR4pJmw94jgNzjyvzmcp2GdJt0v0/Qpb4TiSP6kaHJNr5bnkp69yLpA1NAGmtsLZVM3fXDjpxvsyYiy68wg28EguARy+PtOsk+nHJayUGFKRp69myXCRtL6sTDbfUCHUkb4XdMuCF0zGXpxHdDWFUd3fx2cY2RcDvyY9EMXT/xmVesvgMClhiM+S8iird4Fa5hwiEtEUspDCMoimsH2X/hP X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jan 2018 22:09:37.0588 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8977b015-bc75-49ba-d5b6-08d556e482be X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR12MB1138 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: With LFENCE now a serializing instruction, use LFENCE_RDTSC in preference to MFENCE_RDTSC. However, since the kernel could be running under a hypervisor that does not support writing that MSR, read the MSR back and verify that the bit has been set successfully. If the MSR can be read and the bit is set, then set the LFENCE_RDTSC feature, otherwise set the MFENCE_RDTSC feature. Signed-off-by: Tom Lendacky --- arch/x86/include/asm/msr-index.h | 1 + arch/x86/kernel/cpu/amd.c | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index 1e7d710..fa11fb1 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -354,6 +354,7 @@ #define MSR_FAM10H_NODE_ID 0xc001100c #define MSR_F10H_DECFG 0xc0011029 #define MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT 1 +#define MSR_F10H_DECFG_LFENCE_SERIALIZE BIT_ULL(MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT) /* K8 MSRs */ #define MSR_K8_TOP_MEM1 0xc001001a diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c index 5b438d8..053f6c7 100644 --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -829,6 +829,9 @@ static void init_amd(struct cpuinfo_x86 *c) set_cpu_cap(c, X86_FEATURE_K8); if (cpu_has(c, X86_FEATURE_XMM2)) { + unsigned long long val; + int ret; + /* * A serializing LFENCE has less overhead than MFENCE, so * use it for execution serialization. On families which @@ -839,8 +842,18 @@ static void init_amd(struct cpuinfo_x86 *c) msr_set_bit(MSR_F10H_DECFG, MSR_F10H_DECFG_LFENCE_SERIALIZE_BIT); - /* MFENCE stops RDTSC speculation */ - set_cpu_cap(c, X86_FEATURE_MFENCE_RDTSC); + /* + * Verify that the MSR write was successful (could be running + * under a hypervisor) and only then assume that LFENCE is + * serializing. + */ + ret = rdmsrl_safe(MSR_F10H_DECFG, &val); + if (!ret && (val & MSR_F10H_DECFG_LFENCE_SERIALIZE)) + /* A serializing LFENCE stops RDTSC speculation */ + set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC); + else + /* MFENCE stops RDTSC speculation */ + set_cpu_cap(c, X86_FEATURE_MFENCE_RDTSC); } /*