Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp909724imb; Fri, 1 Mar 2019 18:54:09 -0800 (PST) X-Google-Smtp-Source: APXvYqxkySHNz93oZXtW9y4huwqnGfxfxQmHw4+Sx82iaVyAlir+ijlz73+Y/+Wcc/bDFgl29KW7 X-Received: by 2002:a17:902:9341:: with SMTP id g1mr9013187plp.80.1551495249818; Fri, 01 Mar 2019 18:54:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551495249; cv=none; d=google.com; s=arc-20160816; b=v/EMxL5EqDK+Su3q4m6P+2jRlPyV2MDbYIW+RfW6SD1mRE5vOGmfVrlMkGLuyCmpZq 677vJkubG8Idc7u9ePKU2BYykDuhcfMahN99v3hDU+Viuk4UpxqA6Ay6uKrMqPMoyMFH mnHar/PL8Auj7SZTlJhNP1E+sb8xCv/VP+t+R/jidetsyg4kfpDmhrPHda5gUiKMZ5/O QBq3rZ939GvFGKo1Uzaz+KNgn5yfFxxUPRO9f0mc52hr55TOO1mZD4qkpJyS2xA3uHcV qE8at24sLLt8fWREiW/JOa65GrMF04cvmw2GmAxBNIucCYf1Cgap6RIGPT4IYUHxQsvK zGYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=VkaCN7rLQ8SWh1F0A00AVqUSpiA1TaPG9z+noNqTXWM=; b=alZsJhVbxMhCvzN6Awnheh1u1Xjui4F3oXqFN7I6BhO4er38XLqAT/rzC9WGYiIqip BA9KJrm/894bsg9+P5T+OeE4pJU2eTnQe+RjTxWKthw0sBr+pqxQ1OwMXAXagupB7a6m y1Kr3Ap8bknjEvUqlBGscD8bpzip8Ls6gQxk4luTN16+PBK/jSeDGa+bOMC05y1DYvLN sz4Zf0MxXHnE2vZKj4mCFN1Fu6rMw7K164BvKIEsbUFU4WyVjZX/ONHz1S31GJ4B1m9F pwIfUCkJ5nqjFEBaur/+FEchin9YXbPsWwKfPw4Oh5h5e5jJ2zOLzKcABXmOLhOCRbSY +96w== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 1si9968304plj.313.2019.03.01.18.53.54; Fri, 01 Mar 2019 18:54:09 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728060AbfCBCwz (ORCPT + 99 others); Fri, 1 Mar 2019 21:52:55 -0500 Received: from mga04.intel.com ([192.55.52.120]:49778 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727898AbfCBCwd (ORCPT ); Fri, 1 Mar 2019 21:52:33 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 01 Mar 2019 18:52:29 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,430,1544515200"; d="scan'208";a="148572628" Received: from romley-ivt3.sc.intel.com ([172.25.110.60]) by fmsmga004.fm.intel.com with ESMTP; 01 Mar 2019 18:52:29 -0800 From: Fenghua Yu To: "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "H Peter Anvin" , "Paolo Bonzini" , "Dave Hansen" , "Ashok Raj" , "Peter Zijlstra" , "Ravi V Shankar" , "Xiaoyao Li " Cc: "linux-kernel" , "x86" , kvm@vger.kernel.org, Fenghua Yu Subject: [PATCH v4 14/17] x86/split_lock: Add a sysfs interface to allow user to enable or disable split lock detection on all CPUs during run time Date: Fri, 1 Mar 2019 18:45:08 -0800 Message-Id: <1551494711-213533-15-git-send-email-fenghua.yu@intel.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1551494711-213533-1-git-send-email-fenghua.yu@intel.com> References: <1551494711-213533-1-git-send-email-fenghua.yu@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The interface /sys/device/system/cpu/split_lock_detect is added to allow user to control split lock detection and show current split lock detection setting. Writing 1 to the file enables split lock detection and writing 0 disables split lock detection. Split lock detection is enabled or disabled on all CPUs. Reading the file shows current global split lock detection setting: disabled when 0 and enabled when 1. Please note the interface only shows global setting. During run time, split lock detection on one CPU may be disabled if split lock in kernel code happens on the CPU. The interface doesn't show split lock detection status on individual CPU. User can run rdmsr on 0x33 to check split lock detection on each CPU. Signed-off-by: Fenghua Yu --- arch/x86/kernel/cpu/intel.c | 96 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c index 1c1ec413a9a9..1512e96287b8 100644 --- a/arch/x86/kernel/cpu/intel.c +++ b/arch/x86/kernel/cpu/intel.c @@ -33,6 +33,12 @@ #include #endif +#define DISABLE_SPLIT_LOCK_DETECT 0 +#define ENABLE_SPLIT_LOCK_DETECT 1 + +static DEFINE_MUTEX(split_lock_detect_mutex); +static int split_lock_detect_val; + /* * Just in case our CPU detection goes bad, or you have a weird system, * allow a way to override the automatic disabling of MPX. @@ -163,10 +169,35 @@ static bool bad_spectre_microcode(struct cpuinfo_x86 *c) return false; } +static u32 new_sp_test_ctl_val(u32 test_ctl_val) +{ + /* Change the split lock setting. */ + if (split_lock_detect_val == DISABLE_SPLIT_LOCK_DETECT) + test_ctl_val &= ~TEST_CTL_ENABLE_SPLIT_LOCK_DETECT; + else + test_ctl_val |= TEST_CTL_ENABLE_SPLIT_LOCK_DETECT; + + return test_ctl_val; +} + +static void init_split_lock_detect(struct cpuinfo_x86 *c) +{ + if (cpu_has(c, X86_FEATURE_SPLIT_LOCK_DETECT)) { + u32 l, h; + + rdmsr(MSR_TEST_CTL, l, h); + l = new_sp_test_ctl_val(l); + wrmsr(MSR_TEST_CTL, l, h); + pr_info_once("#AC for split lock is enabled\n"); + } +} + static void early_init_intel(struct cpuinfo_x86 *c) { u64 misc_enable; + init_split_lock_detect(c); + /* Unmask CPUID levels if masked: */ if (c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xd)) { if (msr_clear_bit(MSR_IA32_MISC_ENABLE, @@ -1095,7 +1126,70 @@ void init_core_capability(struct cpuinfo_x86 *c) rdmsrl(MSR_IA32_CORE_CAPABILITY, val); - if (val & CORE_CAP_SPLIT_LOCK_DETECT) + if (val & CORE_CAP_SPLIT_LOCK_DETECT) { setup_force_cpu_cap(X86_FEATURE_SPLIT_LOCK_DETECT); + split_lock_detect_val = 1; + } } } + +static ssize_t +split_lock_detect_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + return sprintf(buf, "%u\n", READ_ONCE(split_lock_detect_val)); +} + +static ssize_t +split_lock_detect_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + u32 val, l, h; + int cpu, ret; + + ret = kstrtou32(buf, 10, &val); + if (ret) + return ret; + + if (val != DISABLE_SPLIT_LOCK_DETECT && val != ENABLE_SPLIT_LOCK_DETECT) + return -EINVAL; + + /* No need to update MSR if new setting is the same as old one. */ + if (val == split_lock_detect_val) + return count; + + /* Only one write can happen at the same time. */ + mutex_lock(&split_lock_detect_mutex); + + WRITE_ONCE(split_lock_detect_val, val); + + /* Get MSR_TEST_CTL on this CPU, assuming all CPUs have same value. */ + rdmsr(MSR_TEST_CTL, l, h); + l = new_sp_test_ctl_val(l); + /* Update the split lock detection setting on all online CPUs. */ + for_each_online_cpu(cpu) + wrmsr_on_cpu(cpu, MSR_TEST_CTL, l, h); + + mutex_unlock(&split_lock_detect_mutex); + + return count; +} + +static DEVICE_ATTR_RW(split_lock_detect); + +static int __init split_lock_init(void) +{ + int ret; + + if (!boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) + return -ENODEV; + + ret = device_create_file(cpu_subsys.dev_root, + &dev_attr_split_lock_detect); + if (ret) + return ret; + + return 0; +} + +subsys_initcall(split_lock_init); -- 2.7.4