Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp112028img; Thu, 21 Mar 2019 15:25:21 -0700 (PDT) X-Google-Smtp-Source: APXvYqx68cvXxC3FRIT0n2rgPGOW1O/8d2TIezUFmhj4z/lm/YKCcYxaP/+R499Md3dL15aukVNK X-Received: by 2002:a62:39d6:: with SMTP id u83mr5747631pfj.161.1553207121276; Thu, 21 Mar 2019 15:25:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553207121; cv=none; d=google.com; s=arc-20160816; b=pEofMX8wBzaeElEqLb28DVowecLmAJtLiJS94c13hTbqhJ9hMHCmAmhF8a939utt7S Nnt7leQc0eKt2MxUTlCN1/P9naZAvqRtHMc7KwPBuqqDjFnwhf3aKw5D/2czZ9DEYM3y gH0u84ZwDNpT8bnq9vxhOnyrDjeHTdXZQqlUlN3eWT94RAyCbKAY+6b7+DMVQ46X3j7r uVm6tIfZD71F7p0nZTRLrFtyVBHLJbtez8bPbsr8xJvU8e7f8bDGz/8upu9lfexQXjuB Sd783G+Ei1M4FHln1mjBRpfbKAd8eOtMaHnh7Z201FTIo6HuuZR7Gd6fLzZWel27YNDK kS1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=tzwBP5IgENMH8EV7+CPlX97pIJwGtGZ9wm7XZjrhSLY=; b=hxkJvSv5hKcrt3xPTERQNj/MTdLBi1MFKxBvJrVXQa1MU/oK+D22FtUPT1awEm8nI+ mMG6lLHI1viC9ZFX8yqsYwcpvoyhTdJu5G6Tuj+569ZuJodX2N7pBSTFgnCfNJoMPRMs 4egDMao4KEYm068a3Xq7wXBjUyWaGKERAPZP0oY00H3D3gNzjSnOcW+QEpI/hzflU+Yd FxQmAAATbHoV9LRQCcM5OdW1HK7s7B6p0YIoaU1CZ4wl9bqh/rDwgyYgF8uLA3wZM59o tIZKrVT7JHDikUGWt32teeE4yVX/EJF0bpgGPTHRYI6VZq8N6UQuvzuvXEbI/nwRVKVr 4c7A== 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 w8si4946755pgs.302.2019.03.21.15.25.03; Thu, 21 Mar 2019 15:25:21 -0700 (PDT) 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 S1726695AbfCUWWb (ORCPT + 99 others); Thu, 21 Mar 2019 18:22:31 -0400 Received: from cloudserver094114.home.pl ([79.96.170.134]:65434 "EHLO cloudserver094114.home.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725999AbfCUWWb (ORCPT ); Thu, 21 Mar 2019 18:22:31 -0400 Received: from 79.184.255.210.ipv4.supernova.orange.pl (79.184.255.210) (HELO aspire.rjw.lan) by serwer1319399.home.pl (79.96.170.134) with SMTP (IdeaSmtpServer 0.83.213) id e9394ffb1e0d2d7d; Thu, 21 Mar 2019 23:22:29 +0100 From: "Rafael J. Wysocki" To: x86 Cc: LKML , Len Brown , Linux PM , Srinivas Pandruvada , Laura Abbott , Thomas Gleixner , Peter Zijlstra , Ingo Molnar , Simon Schricker , Borislav Petkov , Hannes Reinecke Subject: [PATCH 2/2] PM / arch: x86: MSR_IA32_ENERGY_PERF_BIAS sysfs interface Date: Thu, 21 Mar 2019 23:20:17 +0100 Message-ID: <1762575.ER2xjzr9E1@aspire.rjw.lan> In-Reply-To: <1637073.gl2OfxWTjI@aspire.rjw.lan> References: <1637073.gl2OfxWTjI@aspire.rjw.lan> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rafael J. Wysocki The Performance and Energy Bias Hint (EPB) is expected to be set by user space through the generic MSR interface, but that interface is not particularly nice and there are security concerns regarding it, so it is not always available. For this reason, add a sysfs interface for reading and updating the EPB, in the form of a new attribute, energy_perf_bias, located under /sys/devices/system/cpu/cpu#/power/ for online CPUs that support the EPB feature. Signed-off-by: Rafael J. Wysocki --- Documentation/ABI/testing/sysfs-devices-system-cpu | 18 ++++ Documentation/admin-guide/pm/intel_epb.rst | 27 ++++++ arch/x86/kernel/cpu/intel_epb.c | 93 ++++++++++++++++++++- 3 files changed, 134 insertions(+), 4 deletions(-) Index: linux-pm/arch/x86/kernel/cpu/intel_epb.c =================================================================== --- linux-pm.orig/arch/x86/kernel/cpu/intel_epb.c +++ linux-pm/arch/x86/kernel/cpu/intel_epb.c @@ -9,8 +9,12 @@ */ #include +#include +#include #include +#include #include +#include #include #include @@ -20,9 +24,9 @@ * * The Performance and Energy Bias Hint (EPB) allows software to specify its * preference with respect to the power-performance tradeoffs present in the - * processor. Generally, the EPB is expected to be set by user space through - * the generic MSR interface (with the help of the x86_energy_perf_policy tool), - * but there are two reasons for the kernel to touch it. + * processor. Generally, the EPB is expected to be set by user space (directly + * via sysfs or with the help of the x86_energy_perf_policy tool), but there are + * two reasons for the kernel to update it. * * First, there are systems where the platform firmware resets the EPB during * system-wide transitions from sleep states back into the working state @@ -52,6 +56,7 @@ static DEFINE_PER_CPU(u8, saved_epb); #define EPB_MASK 0x0fULL #define EPB_SAVED 0x10ULL +#define MAX_EPB EPB_MASK static int intel_epb_save(void) { @@ -97,15 +102,95 @@ static struct syscore_ops intel_epb_sysc .resume = intel_epb_restore, }; +static const char * const energy_perf_strings[] = { + "performance", + "balance-performance", + "normal", + "balance-power", + "power" +}; +static const u8 energ_perf_values[] = { + ENERGY_PERF_BIAS_PERFORMANCE, + ENERGY_PERF_BIAS_BALANCE_PERFORMANCE, + ENERGY_PERF_BIAS_NORMAL, + ENERGY_PERF_BIAS_BALANCE_POWERSAVE, + ENERGY_PERF_BIAS_POWERSAVE +}; + +static ssize_t energy_perf_bias_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + unsigned int cpu = dev->id; + u64 epb; + int ret; + + ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb); + if (ret < 0) + return ret; + + return sprintf(buf, "%llu\n", epb); +} + +static ssize_t energy_perf_bias_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + unsigned int cpu = dev->id; + u64 epb, val; + int ret; + + ret = __sysfs_match_string(energy_perf_strings, + ARRAY_SIZE(energy_perf_strings), buf); + if (ret >= 0) + val = energ_perf_values[ret]; + else if (kstrtou64(buf, 0, &val) || val > MAX_EPB) + return -EINVAL; + + ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb); + if (ret < 0) + return ret; + + ret = wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, + (epb & ~EPB_MASK) | val); + if (ret < 0) + return ret; + + return count; +} + +static DEVICE_ATTR_RW(energy_perf_bias); + +static struct attribute *intel_epb_attrs[] = { + &dev_attr_energy_perf_bias.attr, + NULL +}; + +static const struct attribute_group intel_epb_attr_group = { + .name = power_group_name, + .attrs = intel_epb_attrs +}; + static int intel_epb_online(unsigned int cpu) { + struct device *cpu_dev = get_cpu_device(cpu); + intel_epb_restore(); + if (!cpuhp_tasks_frozen) + sysfs_merge_group(&cpu_dev->kobj, &intel_epb_attr_group); + return 0; } static int intel_epb_offline(unsigned int cpu) { - return intel_epb_save(); + struct device *cpu_dev = get_cpu_device(cpu); + + if (!cpuhp_tasks_frozen) + sysfs_unmerge_group(&cpu_dev->kobj, &intel_epb_attr_group); + + intel_epb_save(); + return 0; } static __init int intel_epb_init(void) Index: linux-pm/Documentation/admin-guide/pm/intel_epb.rst =================================================================== --- linux-pm.orig/Documentation/admin-guide/pm/intel_epb.rst +++ linux-pm/Documentation/admin-guide/pm/intel_epb.rst @@ -4,3 +4,30 @@ Intel Performance and Energy Bias Hint .. kernel-doc:: arch/x86/kernel/cpu/intel_epb.c :doc: overview + +Intel Performance and Energy Bias Attribute in ``sysfs`` +======================================================== + +The Intel Performance and Energy Bias Hint (EPB) value for a given (logical) CPU +can be checked or updated through a ``sysfs`` attribute (file) under +:file:`/sys/devices/system/cpu/cpu/power/`, where the CPU number ```` +is allocated at the system initialization time: + +``energy_perf_bias`` + Shows the current EPB value for the CPU in a sliding scale 0 - 15, where + a value of 0 corresponds to a hint preference for highest performance + and a value of 15 corresponds to the maximum energy savings. + + In order to update the EPB value for the CPU, this attribute can be + written to, either with a number in the 0 - 15 sliding scale above, or + with one of the strings: "performance", "balance-performance", "normal", + "balance-power", "power" that represent values reflected by their + meaning. + + This attribute is present for all online CPUs supporting the EPB + feature. + +Note that while the EPB interface to the processor is defined at the logical CPU +level, the physical register backing it may be shared by multiple CPUs (for +example, SMT siblings or cores in one package). For this reason, updating the +EPB value for one CPU may cause the EPB values for other CPUs to change. Index: linux-pm/Documentation/ABI/testing/sysfs-devices-system-cpu =================================================================== --- linux-pm.orig/Documentation/ABI/testing/sysfs-devices-system-cpu +++ linux-pm/Documentation/ABI/testing/sysfs-devices-system-cpu @@ -518,3 +518,21 @@ Description: Control Symetric Multi Thre If control status is "forceoff" or "notsupported" writes are rejected. + +What: /sys/devices/system/cpu/cpu#/power/energy_perf_bias +Date: March 2019 +Contact: linux-pm@vger.kernel.org +Description: Intel Energy and Performance Bias Hint (EPB) + + EPB for the given CPU in a sliding scale 0 - 15, where a value + of 0 corresponds to a hint preference for highest performance + and a value of 15 corresponds to the maximum energy savings. + + In order to change the EPB value for the CPU, write either + a number in the 0 - 15 sliding scale above, or one of the + strings: "performance", "balance-performance", "normal", + "balance-power", "power" (that represent values reflected by + their meaning), to this attribute. + + This attribute is present for all online CPUs supporting the + Intel EPB feature.