Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5455763imu; Sun, 20 Jan 2019 11:14:42 -0800 (PST) X-Google-Smtp-Source: ALg8bN61cS4E/g3/jCcoO3gyI2dAmRuoTuMVJHBM8bCpqjcatwiteQxgOdByfgaDcYYf4Yl8KMaT X-Received: by 2002:a17:902:1005:: with SMTP id b5mr27270557pla.310.1548011682296; Sun, 20 Jan 2019 11:14:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548011682; cv=none; d=google.com; s=arc-20160816; b=pUnhbI+ZqqWpS5KKHWj9htc/CL6QHsbmFZJyy5i7qTEQXUam5HuEkG9RikVyn+cU3X H/FCcrNsn7qPrkhmf7hRy6eiizWLlANWVSs7AjsQO89c7cJsx8sp1XWofoKynHPPSSqq wrOJ4qadgAdW/tnXWoSY93ERHFETXEMRadHnO4ykOrphsOg6AsDHWthOLuLsO4yukx1/ hC8Bp+lUWTlrdEJlgBNIhVfVCCRWVRpyhM1BUe2TyhYAvmuYDTQWbl8Lv8UYkkUFAE4Y 3VBm60yUtcxSTl+VHaa0qsBKW3dHfjokbjr+rdH0oiAiRpDvIMlBVrdtpqPAk/q3PJC3 iCRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:openpgp:from:references:cc:to:subject; bh=4i2igddJ7Fjpdjm+oeU7Vfmfk0HlJsQ7yBqf6Ehf/uo=; b=AlIbq2dP95bmqyJ5W7zx8O8OBJ/ffR9BOTy3KACmrjS8PLhFkq/lvKhdP7JmPZEYlW /BFcE8wfe0mCLj5HrQgeHoUNKIlhbO20coamDVWH1fo4BEQUF07Et9ebTmez3iDEwN84 r11XF+ENy54HoR8dG48PTdNh9eEhagOx4YTF+/mSloa4JZaayxJbAZsEEAyg0b8UuaDG IRsjYtE3HqZw43OHyqB1u5JU+NgJ6WIuFlOY4DCgPnrFFoDappucgDEGPJIb4fYOPVI4 eDwX0vaQo1MZ4oP0lnBodLyfQQNrDptrd6+npi7mLAzIC/0LIRHMN6e4quILVSqLSiwV D6EQ== 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 u123si10745891pgb.516.2019.01.20.11.14.26; Sun, 20 Jan 2019 11:14:42 -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 S1727749AbfATTMX (ORCPT + 99 others); Sun, 20 Jan 2019 14:12:23 -0500 Received: from smtp.ctxuk.citrix.com ([185.25.65.24]:19109 "EHLO SMTP.EU.CITRIX.COM" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727415AbfATTMX (ORCPT ); Sun, 20 Jan 2019 14:12:23 -0500 X-IronPort-AV: E=Sophos;i="5.56,499,1539648000"; d="scan'208";a="84843133" Subject: Re: [PATCH v2 3/3] x86/umwait: Control umwait maximum time To: Andy Lutomirski , Fenghua Yu CC: Thomas Gleixner , Borislav Petkov , Ingo Molnar , H Peter Anvin , Ashok Raj , Ravi V Shankar , linux-kernel , x86 References: <1547673522-226408-1-git-send-email-fenghua.yu@intel.com> <1547673522-226408-4-git-send-email-fenghua.yu@intel.com> From: Andrew Cooper Openpgp: preference=signencrypt Autocrypt: addr=andrew.cooper3@citrix.com; prefer-encrypt=mutual; keydata= mQINBFLhNn8BEADVhE+Hb8i0GV6mihnnr/uiQQdPF8kUoFzC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JaL9xXedMcAEQEAAYkC HwQYAQgACQUCUuE2fwIbDAAKCRBlw/kGpdefoG4XEACD1Qf/er8EA7g23HMxYWd3FXHThrVQ HgiGdk5Yh632vjOm9L4sd/GCEACVQKjsu98e8o3ysitFlznEns5EAAXEbITrgKWXDDUWGYxd pnjj2u+GkVdsOAGk0kxczX6s+VRBhpbBI2PWnOsRJgU2n10PZ3mZD4Xu9kU2IXYmuW+e5KCA vTArRUdCrAtIa1k01sPipPPw6dfxx2e5asy21YOytzxuWFfJTGnVxZZSCyLUO83sh6OZhJkk b9rxL9wPmpN/t2IPaEKoAc0FTQZS36wAMOXkBh24PQ9gaLJvfPKpNzGD8XWR5HHF0NLIJhgg 4ZlEXQ2fVp3XrtocHqhu4UZR4koCijgB8sB7Tb0GCpwK+C4UePdFLfhKyRdSXuvY3AHJd4CP 4JzW0Bzq/WXY3XMOzUTYApGQpnUpdOmuQSfpV9MQO+/jo7r6yPbxT7CwRS5dcQPzUiuHLK9i nvjREdh84qycnx0/6dDroYhp0DFv4udxuAvt1h4wGwTPRQZerSm4xaYegEFusyhbZrI0U9tJ B8WrhBLXDiYlyJT6zOV2yZFuW47VrLsjYnHwn27hmxTC/7tvG3euCklmkn9Sl9IAKFu29RSo d5bD8kMSCYsTqtTfT6W4A3qHGvIDta3ptLYpIAOD2sY3GYq2nf3Bbzx81wZK14JdDDHUX2Rs 6+ahAA== Message-ID: Date: Sun, 20 Jan 2019 19:12:17 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Content-Language: en-GB X-ClientProxiedBy: AMSPEX02CAS01.citrite.net (10.69.22.112) To AMSPEX02CL02.citrite.net (10.69.22.126) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 17/01/2019 00:00, Andy Lutomirski wrote: > On Wed, Jan 16, 2019 at 1:24 PM Fenghua Yu wrote: >> IA32_UMWAIT_CONTROL[31:2] determines the maximum time in TSC-quanta >> that processor can stay in C0.1 or C0.2. >> >> The maximum time value in IA32_UMWAIT_CONTROL[31-2] is set as zero which >> means there is no global time limit for UMWAIT and TPAUSE instructions. >> Each process sets its own umwait maximum time as the instructions operand. >> >> User can specify global umwait maximum time through interface: >> /sys/devices/system/cpu/umwait_control/umwait_max_time >> The value in the interface is in decimal in TSC-quanta. Bits[1:0] >> are cleared when the value is stored. >> >> Signed-off-by: Fenghua Yu >> --- >> arch/x86/include/asm/msr-index.h | 2 ++ >> arch/x86/power/umwait.c | 42 +++++++++++++++++++++++++++++++- >> 2 files changed, 43 insertions(+), 1 deletion(-) >> >> diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h >> index b56bfecae0de..42b9104fc15b 100644 >> --- a/arch/x86/include/asm/msr-index.h >> +++ b/arch/x86/include/asm/msr-index.h >> @@ -62,6 +62,8 @@ >> #define MSR_IA32_UMWAIT_CONTROL 0xe1 >> #define UMWAIT_CONTROL_C02_BIT 0x0 >> #define UMWAIT_CONTROL_C02_MASK 0x00000001 >> +#define UMWAIT_CONTROL_MAX_TIME_BIT 0x2 >> +#define UMWAIT_CONTROL_MAX_TIME_MASK 0xfffffffc >> >> #define MSR_PKG_CST_CONFIG_CONTROL 0x000000e2 >> #define NHM_C3_AUTO_DEMOTE (1UL << 25) >> diff --git a/arch/x86/power/umwait.c b/arch/x86/power/umwait.c >> index 95b3867aac1e..4a1a507d3bb7 100644 >> --- a/arch/x86/power/umwait.c >> +++ b/arch/x86/power/umwait.c >> @@ -10,6 +10,7 @@ >> #include >> >> static int umwait_enable_c0_2 = 1; /* 0: disable C0.2. 1: enable C0.2. */ >> +static u32 umwait_max_time; /* In TSC-quanta. Only bits [31:2] are used. */ >> static DEFINE_MUTEX(umwait_lock); >> >> /* Return value that will be used to set umwait control MSR */ >> @@ -20,7 +21,8 @@ static inline u32 umwait_control_val(void) >> * When bit 0 is 1, C0.2 is disabled. Otherwise, C0.2 is enabled. >> * So value in bit 0 is opposite of umwait_enable_c0_2. >> */ >> - return ~umwait_enable_c0_2 & UMWAIT_CONTROL_C02_MASK; >> + return (~umwait_enable_c0_2 & UMWAIT_CONTROL_C02_MASK) | >> + umwait_max_time; >> } >> >> static ssize_t umwait_enable_c0_2_show(struct device *dev, >> @@ -61,8 +63,46 @@ static ssize_t umwait_enable_c0_2_store(struct device *dev, >> >> static DEVICE_ATTR_RW(umwait_enable_c0_2); >> >> +static ssize_t umwait_max_time_show(struct device *kobj, >> + struct device_attribute *attr, char *buf) >> +{ >> + return sprintf(buf, "%u\n", umwait_max_time); >> +} >> + >> +static ssize_t umwait_max_time_store(struct device *kobj, >> + struct device_attribute *attr, >> + const char *buf, size_t count) >> +{ >> + u32 msr_val, max_time; >> + int cpu, ret; >> + >> + ret = kstrtou32(buf, 10, &max_time); >> + if (ret) >> + return ret; >> + >> + mutex_lock(&umwait_lock); >> + >> + /* Only get max time value from bits [31:2] */ >> + max_time &= UMWAIT_CONTROL_MAX_TIME_MASK; >> + /* Update the max time value in memory */ >> + umwait_max_time = max_time; >> + msr_val = umwait_control_val(); >> + get_online_cpus(); >> + /* All CPUs have same umwait max time */ >> + for_each_online_cpu(cpu) >> + wrmsr_on_cpu(cpu, MSR_IA32_UMWAIT_CONTROL, msr_val, 0); >> + put_online_cpus(); >> + >> + mutex_unlock(&umwait_lock); >> + >> + return count; >> +} >> + >> +static DEVICE_ATTR_RW(umwait_max_time); >> + >> static struct attribute *umwait_attrs[] = { >> &dev_attr_umwait_enable_c0_2.attr, >> + &dev_attr_umwait_max_time.attr, >> NULL >> }; > You need something to make sure that newly onlined CPUs get the right > value in the MSR. You also need to make sure you restore it on resume > from suspend. Something like cpu_init() might be the right place. > > Also, as previously discussed, I think we should set the default to > something quite small, maybe 100 microseconds. IMO the goal is to > pick a value that is a high enough multiple of the C0.2 entry+exit > latency that we get most of the power and SMT resource savings while > being small enough that no one things that UMWAIT is more than a > glorified, slightly improved, and far more misleading version of REP > NOP. > > Andrew, would having Linux default to a small value do much to > mitigate your concerns that UMWAIT is problematic for hypervisors? Sadly no - not really. Being an MSR, there is no way the guest kernel is having unfiltered access, so the hypervisor can set whatever bound it wishes. For any non-trivial wait period, it would be better for the system as a whole to switch to a different vcpu, but the semantics don't allow for that.  Shortening the timeout just results in userspace taking over again, and most likely concluding that there was an early wakeup and going back to sleep. More useful semantics would be something similar to pause-loop-exiting so we can swap contexts while the processor is logically idle in userspace. ~Andrew