x86: Remove direct use of MSR_IA32_ENERGY_PERF_BIAS
Subsystems (4)
Borislav Petkov
maintainercover x86: Remove direct use of MSR_IA32_ENERGY_PERF_BIAS
From: Borislav Petkov <[email protected]>
Hi,
here's v2 with some of Shuah's comments integrated.
If no one has anything against it, I'll route them all through tip.
Thx.
---
Changelog:
v0:
--
here's something from my todo list: remove all in-kernel tools use of that MSR and lastly drop it from the allowed-MSRs-list in the filtering.
Out-of-tree tools should do a similar, trivial conversion.
Constructive comments are, as always, appreciated.
Borislav Petkov (4):
tools/power/cpupower: Read energy_perf_bias from sysfs tools/power/turbostat: Read energy_perf_bias from sysfs tools/power/x86_energy_perf_policy: Read energy_perf_bias from sysfs x86/msr: Do not allow writes to MSR_IA32_ENERGY_PERF_BIAS
arch/x86/kernel/msr.c | 3 - tools/power/cpupower/lib/cpupower.c | 23 +++- tools/power/cpupower/lib/cpupower_intern.h | 5 + tools/power/cpupower/utils/cpupower-info.c | 2 +- tools/power/cpupower/utils/cpupower-set.c | 2 +- tools/power/cpupower/utils/helpers/helpers.h | 8 +- tools/power/cpupower/utils/helpers/misc.c | 48 ++++++++ tools/power/cpupower/utils/helpers/msr.c | 28 ----- tools/power/x86/turbostat/turbostat.c | 29 ++++- .../x86_energy_perf_policy.c | 109 ++++++++++++++++-- 10 files changed, 204 insertions(+), 53 deletions(-)
Signature · 1 line
2.21.0
Borislav Petkov
maintainerPATCH 1/4 tools/power/cpupower: Read energy_perf_bias from sysfs
From: Borislav Petkov <[email protected]>
... instead of poking at the MSR. For that, move the accessor functions to misc.c and add a sysfs-writing function too.
There should be no functional changes resulting from this.
--- tools/power/cpupower/lib/cpupower.c | 23 +++++++++- tools/power/cpupower/lib/cpupower_intern.h | 5 ++ tools/power/cpupower/utils/cpupower-info.c | 2 +- tools/power/cpupower/utils/cpupower-set.c | 2 +- tools/power/cpupower/utils/helpers/helpers.h | 8 ++-- tools/power/cpupower/utils/helpers/misc.c | 48 ++++++++++++++++++++ tools/power/cpupower/utils/helpers/msr.c | 28 ------------ 7 files changed, 81 insertions(+), 35 deletions(-)
tools/power/cpupower/lib/cpupower.c +22 -1
@@ -16,8 +16,8 @@unsigned int cpupower_read_sysfs (const char *path,char *buf,size_t buflen) { -int fd;ssize_t numread; +int fd; fd =open (path,O_RDONLY );if (fd ==-1 )
@@ -35,6 +35,27 @@ unsigned int cpupower_read_sysfs(const char *path, char *buf, size_t buflen)return (unsigned int ) numread; } +unsigned int cpupower_write_sysfs (const char *path,char *buf,size_t buflen) +{ +ssize_t numwritten; +int fd; + + fd =open (path,O_WRONLY ); +if (fd ==-1 ) +return 0 ; + + numwritten =write (fd, buf, buflen -1 ); +if (numwritten <1 ) { +perror (path); +close (fd); +return -1 ; + } + +close (fd); + +return (unsigned int ) numwritten; +} +/* * Detect whether a CPU is online *
tools/power/cpupower/lib/cpupower_intern.h +5 -0
@@ -1,6 +1,11 @@/* SPDX-License-Identifier: GPL-2.0 */ #define PATH_TO_CPU "/sys/devices/system/cpu/" + +#ifndef MAX_LINE_LEN #define MAX_LINE_LEN 4096 +#endif +#define SYSFS_PATH_MAX 255unsigned int cpupower_read_sysfs (const char *path,char *buf,size_t buflen); +unsigned int cpupower_write_sysfs (const char *path,char *buf,size_t buflen);
tools/power/cpupower/utils/cpupower-info.c +1 -1
@@ -101,7 +101,7 @@ int cmd_info(int argc, char **argv) }if (params.perf_bias) { - ret =msr_intel_get_perf_bias (cpu); + ret =cpupower_intel_get_perf_bias (cpu);if (ret <0 ) {fprintf (stderr,_ ("Could not read perf-bias value[%d]\n" ), ret);
tools/power/cpupower/utils/cpupower-set.c +1 -1
@@ -95,7 +95,7 @@ int cmd_set(int argc, char **argv) }if (params.perf_bias) { - ret =msr_intel_set_perf_bias (cpu, perf_bias); + ret =cpupower_intel_set_perf_bias (cpu, perf_bias);if (ret) {fprintf (stderr,_ ("Error setting perf-bias " "value on CPU %d\n" ), cpu);
tools/power/cpupower/utils/helpers/helpers.h +4 -4
@@ -105,8 +105,8 @@ extern struct cpupower_cpu_info cpupower_cpu_info;extern int read_msr (int cpu,unsigned int idx,unsigned long long *val);extern int write_msr (int cpu,unsigned int idx,unsigned long long val); -extern int msr_intel_set_perf_bias (unsigned int cpu,unsigned int val); -extern int msr_intel_get_perf_bias (unsigned int cpu); +extern int cpupower_intel_set_perf_bias (unsigned int cpu,unsigned int val); +extern int cpupower_intel_get_perf_bias (unsigned int cpu);extern unsigned long long msr_intel_get_turbo_ratio (unsigned int cpu);/* Read/Write msr ****************************/
@@ -150,9 +150,9 @@ static inline int read_msr(int cpu, unsigned int idx, unsigned long long *val) {return -1 ; };static inline int write_msr (int cpu,unsigned int idx,unsigned long long val) {return -1 ; }; -static inline int msr_intel_set_perf_bias (unsigned int cpu,unsigned int val) +static inline int cpupower_intel_set_perf_bias (unsigned int cpu,unsigned int val) {return -1 ; }; -static inline int msr_intel_get_perf_bias (unsigned int cpu) +static inline int cpupower_intel_get_perf_bias (unsigned int cpu) {return -1 ; };static inline unsigned long long msr_intel_get_turbo_ratio (unsigned int cpu) {return 0 ; };
tools/power/cpupower/utils/helpers/misc.c +48 -0
59 lines not on this page · raw message
tools/power/cpupower/utils/helpers/msr.c +0 -28
@@ -11,7 +11,6 @@/* Intel specific MSRs */ #define MSR_IA32_PERF_STATUS 0x198#define MSR_IA32_MISC_ENABLES 0x1a0 -#define MSR_IA32_ENERGY_PERF_BIAS 0x1b0#define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1ad/*
@@ -73,33 +72,6 @@ int write_msr(int cpu, unsigned int idx, unsigned long long val)return -1 ; } -int msr_intel_get_perf_bias (unsigned int cpu) -{ -unsigned long long val; -int ret; - -if (!(cpupower_cpu_info.caps &CPUPOWER_CAP_PERF_BIAS )) -return -1 ; - - ret =read_msr (cpu,MSR_IA32_ENERGY_PERF_BIAS , &val); -if (ret) -return ret; -return val; -} - -int msr_intel_set_perf_bias (unsigned int cpu,unsigned int val) -{ -int ret; - -if (!(cpupower_cpu_info.caps &CPUPOWER_CAP_PERF_BIAS )) -return -1 ; - - ret =write_msr (cpu,MSR_IA32_ENERGY_PERF_BIAS , val); -if (ret) -return ret; -return 0 ; -} -unsigned long long msr_intel_get_turbo_ratio (unsigned int cpu) {unsigned long long val;
Signature · 1 line
2.21.0
Borislav Petkov
maintainerPATCH 4/4 x86/msr: Do not allow writes to MSR_IA32_ENERGY_PERF_BIAS
In reply to Borislav PetkovFrom: Borislav Petkov <[email protected]>
Now that all in-kernel-tree users are converted to using the sysfs file, remove the MSR from the "allowlist".
--- arch/x86/kernel/msr.c | 3 --- 1 file changed, 3 deletions(-)
arch/x86/kernel/msr.c +0 -3
@@ -99,9 +99,6 @@ static int filter_write(u32 reg)if (!__ratelimit (&fw_rs))return 0 ; -if (reg ==MSR_IA32_ENERGY_PERF_BIAS ) -return 0 ; -pr_err ("Write to unrecognized MSR 0x%x by %s (pid: %d). Please report to [email protected].\n" , reg, current->comm, current->pid);
Signature · 1 line
2.21.0
Borislav Petkov
maintainerPATCH 2/4 tools/power/turbostat: Read energy_perf_bias from sysfs
In reply to Borislav PetkovFrom: Borislav Petkov <[email protected]>
... instead of poking at the MSR directly.
--- tools/power/x86/turbostat/turbostat.c | 29 ++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-)
tools/power/x86/turbostat/turbostat.c +24 -5
@@ -1721,6 +1721,25 @@ int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)return 0 ; } +int get_epb (int cpu) +{ +char path[128 +PATH_BYTES ]; +int ret, epb =-1 ; +FILE *fp; + +sprintf (path,"/sys/devices/system/cpu/cpu%d/power/energy_perf_bias" , cpu); + + fp =fopen_or_die (path,"r" ); + + ret =fscanf (fp,"%d" , &epb); +if (ret !=1 ) +err (1 ,"%s(%s)" , __func__, path); + +fclose (fp); + +return epb; +} +void get_apic_id (struct thread_data *t) {unsigned int eax, ebx, ecx, edx;
@@ -3631,9 +3650,8 @@ dump_sysfs_pstate_config(void)*/ int print_epb (struct thread_data *t,struct core_data *c,struct pkg_data *p) { -unsigned long long msr;char *epb_string; -int cpu; +int cpu, epb;if (!has_epb)return 0 ;
@@ -3649,10 +3667,11 @@ int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)return -1 ; } -if (get_msr (cpu,MSR_IA32_ENERGY_PERF_BIAS , &msr)) + epb =get_epb (cpu); +if (epb <0 )return 0 ; -switch (msr &0xF ) { +switch (epb) {case ENERGY_PERF_BIAS_PERFORMANCE : epb_string ="performance" ;break ;
@@ -3666,7 +3685,7 @@ int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) epb_string ="custom" ;break ; } -fprintf (outf,"cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n" , cpu, msr, epb_string); +fprintf (outf,"cpu%d: EPB: %d (%s)\n" , cpu, epb, epb_string);return 0 ; }
Signature · 1 line
2.21.0
Borislav Petkov
maintainerPATCH 3/4 tools/power/x86_energy_perf_policy: Read energy_perf_bias from sysfs
In reply to Borislav PetkovFrom: Borislav Petkov <[email protected]>
... and stop poking at the MSR directly.
--- .../x86_energy_perf_policy.c | 109 ++++++++++++++++-- 1 file changed, 99 insertions(+), 10 deletions(-)
tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c +99 -10
@@ -91,6 +91,9 @@ unsigned int has_hwp_request_pkg; /* IA32_HWP_REQUEST_PKG */unsigned int bdx_highest_ratio; +#define PATH_TO_CPU "/sys/devices/system/cpu/" +#define SYSFS_PATH_MAX 255 +/* * maintain compatibility with original implementation, but don't document it: */
@@ -668,6 +671,48 @@ int put_msr(int cpu, int offset, unsigned long long new_msr)return 0 ; } +static unsigned int read_sysfs (const char *path,char *buf,size_t buflen) +{ +ssize_t numread; +int fd; + + fd =open (path,O_RDONLY ); +if (fd ==-1 ) +return 0 ; + + numread =read (fd, buf, buflen -1 ); +if (numread <1 ) { +close (fd); +return 0 ; + } + + buf[numread] ='\0' ; +close (fd); + +return (unsigned int ) numread; +} + +static unsigned int write_sysfs (const char *path,char *buf,size_t buflen) +{ +ssize_t numwritten; +int fd; + + fd =open (path,O_WRONLY ); +if (fd ==-1 ) +return 0 ; + + numwritten =write (fd, buf, buflen -1 ); +if (numwritten <1 ) { +perror ("write failed\n" ); +close (fd); +return -1 ; + } + +close (fd); + +return (unsigned int ) numwritten; +} +void print_hwp_cap (int cpu,struct msr_hwp_cap *cap,char *str) {if (cpu !=-1 )
@@ -745,17 +790,61 @@ void write_hwp_request(int cpu, struct msr_hwp_request *hwp_req, unsigned int msput_msr (cpu, msr_offset, msr); } +static int get_epb (int cpu) +{ +char path[SYSFS_PATH_MAX ]; +char linebuf[3 ]; +char *endp; +long val; + +if (!has_epb) +return -1 ; + +snprintf (path,sizeof (path),PATH_TO_CPU "cpu%u/power/energy_perf_bias" , cpu); + +if (!read_sysfs (path, linebuf,3 )) +return -1 ; + + val =strtol (linebuf, &endp,0 ); +if (endp == linebuf || errno ==ERANGE ) +return -1 ; + +return (int )val; +} + +static int set_epb (int cpu,int val) +{ +char path[SYSFS_PATH_MAX ]; +char linebuf[3 ]; +char *endp; +int ret; + +if (!has_epb) +return -1 ; + +snprintf (path,sizeof (path),PATH_TO_CPU "cpu%u/power/energy_perf_bias" , cpu); +snprintf (linebuf,sizeof (linebuf),"%d" , val); + + ret =write_sysfs (path, linebuf,3 ); +if (ret <=0 ) +return -1 ; + + val =strtol (linebuf, &endp,0 ); +if (endp == linebuf || errno ==ERANGE ) +return -1 ; + +return (int )val; +} +int print_cpu_msrs (int cpu) { -unsigned long long msr;struct msr_hwp_request req;struct msr_hwp_cap cap; +int epb; -if (has_epb) { -get_msr (cpu,MSR_IA32_ENERGY_PERF_BIAS , &msr); - -printf ("cpu%d: EPB %u\n" , cpu, (unsigned int ) msr); - } + epb =get_epb (cpu); +if (epb >=0 ) +printf ("cpu%d: EPB %u\n" , cpu, (unsigned int ) epb);if (!has_hwp)return 0 ;
@@ -1038,15 +1127,15 @@ int enable_hwp_on_cpu(int cpu)int update_cpu_msrs (int cpu) {unsigned long long msr; - +int epb;if (update_epb) { -get_msr (cpu,MSR_IA32_ENERGY_PERF_BIAS , &msr); -put_msr (cpu,MSR_IA32_ENERGY_PERF_BIAS , new_epb); + epb =get_epb (cpu); +set_epb (cpu, new_epb);if (verbose)printf ("cpu%d: ENERGY_PERF_BIAS old: %d new: %d\n" , - cpu, (unsigned int ) msr, (unsigned int ) new_epb); + cpu, epb, (unsigned int ) new_epb); }if (update_turbo) {
Signature · 1 line
2.21.0
Re: [PATCH v1 4/4] x86/msr: Do not allow writes to MSR_IA32_ENERGY_PERF_BIAS
In reply to Borislav PetkovQuoted 16 lines · Borislav Petkov
From: Borislav Petkov <[email protected]>
Now that all in-kernel-tree users are converted to using the sysfs file, remove the MSR from the "allowlist".
Signed-off-by Borislav Petkov <[email protected]>--- arch/x86/kernel/msr.c | 3 --- 1 file changed, 3 deletions(-)arch/x86/kernel/msr.c +0
-0
arch/x86/kernel/msr.c static int filter_write(u32 reg) · line 104
Quoted 9 lines
arch/x86/kernel/msr.c +0
-3@@ -99,9 +99,6 @@ static int filter_write(u32 reg)if (!__ratelimit (&fw_rs))return 0 ; -if (reg ==MSR_IA32_ENERGY_PERF_BIAS ) -return 0 ; -pr_err ("Write to unrecognized MSR 0x%x by %s (pid: %d). Please report to [email protected].\n" , reg, current->comm, current->pid);
Thanks. Looks good to me.
thanks,
-- Shuah
Re: [PATCH v1 1/4] tools/power/cpupower: Read energy_perf_bias from sysfs
In reply to Borislav PetkovQuoted 10 lines · Borislav Petkov
From: Borislav Petkov <[email protected]>
... instead of poking at the MSR. For that, move the accessor functions to
misc.cand add a sysfs-writing function too.There should be no functional changes resulting from this.
Signed-off-by Borislav Petkov <[email protected]>Cc Thomas Renninger <[email protected]>Cc Shuah Khan <[email protected]>
Quoted 10 lines
--- tools/power/cpupower/lib/cpupower.c | 23 +++++++++- tools/power/cpupower/lib/cpupower_intern.h | 5 ++ tools/power/cpupower/utils/cpupower-info.c | 2 +- tools/power/cpupower/utils/cpupower-set.c | 2 +- tools/power/cpupower/utils/helpers/helpers.h | 8 ++-- tools/power/cpupower/utils/helpers/misc.c | 48 ++++++++++++++++++++ tools/power/cpupower/utils/helpers/msr.c | 28 ------------ 7 files changed, 81 insertions(+), 35 deletions(-)
Thanks. Looks good to me.
thanks,
-- Shuah
[tip: x86/misc] x86/msr: Do not allow writes to MSR_IA32_ENERGY_PERF_BIAS
In reply to Borislav PetkovThe following commit has been merged into the x86/misc branch of tip:
The following commit has been merged into the x86/misc branch of tip:
Commit-ID: 18741a5251d018094536a2dffe284d269ebb07fe Gitweb: https://git.kernel.org/tip/18741a5251d018094536a2dffe284d269ebb07fe Author: Borislav Petkov <[email protected]> AuthorDate: Thu, 15 Oct 2020 15:00:31 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Mon, 16 Nov 2020 17:44:04 +01:00
x86/msr: Do not allow writes to MSR_IA32_ENERGY_PERF_BIAS
Now that all in-kernel-tree users are converted to using the sysfs file, remove the MSR from the "allowlist".
--- arch/x86/kernel/msr.c | 3 --- 1 file changed, 3 deletions(-)
arch/x86/kernel/msr.c +0 -3
@@ -99,9 +99,6 @@ static int filter_write(u32 reg)if (!__ratelimit (&fw_rs))return 0 ; -if (reg ==MSR_IA32_ENERGY_PERF_BIAS ) -return 0 ; -pr_err ("Write to unrecognized MSR 0x%x by %s (pid: %d). Please report to [email protected].\n" , reg, current->comm, current->pid);
[tip: x86/misc] tools/power/cpupower: Read energy_perf_bias from sysfs
In reply to Borislav PetkovThe following commit has been merged into the x86/misc branch of tip:
The following commit has been merged into the x86/misc branch of tip:
Commit-ID: 8113ab20e850491b4144a1a64246f07a2d737a49 Gitweb: https://git.kernel.org/tip/8113ab20e850491b4144a1a64246f07a2d737a49 Author: Borislav Petkov <[email protected]> AuthorDate: Thu, 15 Oct 2020 12:28:58 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Mon, 16 Nov 2020 17:42:12 +01:00
tools/power/cpupower: Read energy_perf_bias from sysfs
... instead of poking at the MSR. For that, move the accessor functions to misc.c and add a sysfs-writing function too.
There should be no functional changes resulting from this.
--- tools/power/cpupower/lib/cpupower.c | 23 ++++++++- tools/power/cpupower/lib/cpupower_intern.h | 5 ++- tools/power/cpupower/utils/cpupower-info.c | 2 +- tools/power/cpupower/utils/cpupower-set.c | 2 +- tools/power/cpupower/utils/helpers/helpers.h | 8 +-- tools/power/cpupower/utils/helpers/misc.c | 48 +++++++++++++++++++- tools/power/cpupower/utils/helpers/msr.c | 28 +----------- 7 files changed, 81 insertions(+), 35 deletions(-)
tools/power/cpupower/lib/cpupower.c +22 -1
@@ -16,8 +16,8 @@unsigned int cpupower_read_sysfs (const char *path,char *buf,size_t buflen) { -int fd;ssize_t numread; +int fd; fd =open (path,O_RDONLY );if (fd ==-1 )
@@ -35,6 +35,27 @@ unsigned int cpupower_read_sysfs(const char *path, char *buf, size_t buflen)return (unsigned int ) numread; } +unsigned int cpupower_write_sysfs (const char *path,char *buf,size_t buflen) +{ +ssize_t numwritten; +int fd; + + fd =open (path,O_WRONLY ); +if (fd ==-1 ) +return 0 ; + + numwritten =write (fd, buf, buflen -1 ); +if (numwritten <1 ) { +perror (path); +close (fd); +return -1 ; + } + +close (fd); + +return (unsigned int ) numwritten; +} +/* * Detect whether a CPU is online *
tools/power/cpupower/lib/cpupower_intern.h +5 -0
@@ -1,6 +1,11 @@/* SPDX-License-Identifier: GPL-2.0 */ #define PATH_TO_CPU "/sys/devices/system/cpu/" + +#ifndef MAX_LINE_LEN #define MAX_LINE_LEN 4096 +#endif +#define SYSFS_PATH_MAX 255unsigned int cpupower_read_sysfs (const char *path,char *buf,size_t buflen); +unsigned int cpupower_write_sysfs (const char *path,char *buf,size_t buflen);
tools/power/cpupower/utils/cpupower-info.c +1 -1
@@ -101,7 +101,7 @@ int cmd_info(int argc, char **argv) }if (params.perf_bias) { - ret =msr_intel_get_perf_bias (cpu); + ret =cpupower_intel_get_perf_bias (cpu);if (ret <0 ) {fprintf (stderr,_ ("Could not read perf-bias value[%d]\n" ), ret);
tools/power/cpupower/utils/cpupower-set.c +1 -1
@@ -95,7 +95,7 @@ int cmd_set(int argc, char **argv) }if (params.perf_bias) { - ret =msr_intel_set_perf_bias (cpu, perf_bias); + ret =cpupower_intel_set_perf_bias (cpu, perf_bias);if (ret) {fprintf (stderr,_ ("Error setting perf-bias " "value on CPU %d\n" ), cpu);
tools/power/cpupower/utils/helpers/helpers.h +4 -4
@@ -105,8 +105,8 @@ extern struct cpupower_cpu_info cpupower_cpu_info;extern int read_msr (int cpu,unsigned int idx,unsigned long long *val);extern int write_msr (int cpu,unsigned int idx,unsigned long long val); -extern int msr_intel_set_perf_bias (unsigned int cpu,unsigned int val); -extern int msr_intel_get_perf_bias (unsigned int cpu); +extern int cpupower_intel_set_perf_bias (unsigned int cpu,unsigned int val); +extern int cpupower_intel_get_perf_bias (unsigned int cpu);extern unsigned long long msr_intel_get_turbo_ratio (unsigned int cpu);/* Read/Write msr ****************************/
@@ -150,9 +150,9 @@ static inline int read_msr(int cpu, unsigned int idx, unsigned long long *val) {return -1 ; };static inline int write_msr (int cpu,unsigned int idx,unsigned long long val) {return -1 ; }; -static inline int msr_intel_set_perf_bias (unsigned int cpu,unsigned int val) +static inline int cpupower_intel_set_perf_bias (unsigned int cpu,unsigned int val) {return -1 ; }; -static inline int msr_intel_get_perf_bias (unsigned int cpu) +static inline int cpupower_intel_get_perf_bias (unsigned int cpu) {return -1 ; };static inline unsigned long long msr_intel_get_turbo_ratio (unsigned int cpu) {return 0 ; };
tools/power/cpupower/utils/helpers/misc.c +48 -0
59 lines not on this page · raw message
tools/power/cpupower/utils/helpers/msr.c +0 -28
@@ -11,7 +11,6 @@/* Intel specific MSRs */ #define MSR_IA32_PERF_STATUS 0x198#define MSR_IA32_MISC_ENABLES 0x1a0 -#define MSR_IA32_ENERGY_PERF_BIAS 0x1b0#define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1ad/*
@@ -73,33 +72,6 @@ int write_msr(int cpu, unsigned int idx, unsigned long long val)return -1 ; } -int msr_intel_get_perf_bias (unsigned int cpu) -{ -unsigned long long val; -int ret; - -if (!(cpupower_cpu_info.caps &CPUPOWER_CAP_PERF_BIAS )) -return -1 ; - - ret =read_msr (cpu,MSR_IA32_ENERGY_PERF_BIAS , &val); -if (ret) -return ret; -return val; -} - -int msr_intel_set_perf_bias (unsigned int cpu,unsigned int val) -{ -int ret; - -if (!(cpupower_cpu_info.caps &CPUPOWER_CAP_PERF_BIAS )) -return -1 ; - - ret =write_msr (cpu,MSR_IA32_ENERGY_PERF_BIAS , val); -if (ret) -return ret; -return 0 ; -} -unsigned long long msr_intel_get_turbo_ratio (unsigned int cpu) {unsigned long long val;
[tip: x86/misc] tools/power/turbostat: Read energy_perf_bias from sysfs
In reply to Borislav PetkovThe following commit has been merged into the x86/misc branch of tip:
The following commit has been merged into the x86/misc branch of tip:
Commit-ID: 6d6501d912a9a5e1b73d7fbf419b90a8ec11ed7a Gitweb: https://git.kernel.org/tip/6d6501d912a9a5e1b73d7fbf419b90a8ec11ed7a Author: Borislav Petkov <[email protected]> AuthorDate: Thu, 15 Oct 2020 14:50:16 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Mon, 16 Nov 2020 17:42:46 +01:00
tools/power/turbostat: Read energy_perf_bias from sysfs
... instead of poking at the MSR directly.
--- tools/power/x86/turbostat/turbostat.c | 29 +++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-)
tools/power/x86/turbostat/turbostat.c +24 -5
@@ -1721,6 +1721,25 @@ int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)return 0 ; } +int get_epb (int cpu) +{ +char path[128 +PATH_BYTES ]; +int ret, epb =-1 ; +FILE *fp; + +sprintf (path,"/sys/devices/system/cpu/cpu%d/power/energy_perf_bias" , cpu); + + fp =fopen_or_die (path,"r" ); + + ret =fscanf (fp,"%d" , &epb); +if (ret !=1 ) +err (1 ,"%s(%s)" , __func__, path); + +fclose (fp); + +return epb; +} +void get_apic_id (struct thread_data *t) {unsigned int eax, ebx, ecx, edx;
@@ -3631,9 +3650,8 @@ dump_sysfs_pstate_config(void)*/ int print_epb (struct thread_data *t,struct core_data *c,struct pkg_data *p) { -unsigned long long msr;char *epb_string; -int cpu; +int cpu, epb;if (!has_epb)return 0 ;
@@ -3649,10 +3667,11 @@ int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)return -1 ; } -if (get_msr (cpu,MSR_IA32_ENERGY_PERF_BIAS , &msr)) + epb =get_epb (cpu); +if (epb <0 )return 0 ; -switch (msr &0xF ) { +switch (epb) {case ENERGY_PERF_BIAS_PERFORMANCE : epb_string ="performance" ;break ;
@@ -3666,7 +3685,7 @@ int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p) epb_string ="custom" ;break ; } -fprintf (outf,"cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n" , cpu, msr, epb_string); +fprintf (outf,"cpu%d: EPB: %d (%s)\n" , cpu, epb, epb_string);return 0 ; }
[tip: x86/misc] tools/power/x86_energy_perf_policy: Read energy_perf_bias from sysfs
In reply to Borislav PetkovThe following commit has been merged into the x86/misc branch of tip:
The following commit has been merged into the x86/misc branch of tip:
Commit-ID: fe0a5788624c8b8f113a35bbe4636e37f9321241 Gitweb: https://git.kernel.org/tip/fe0a5788624c8b8f113a35bbe4636e37f9321241 Author: Borislav Petkov <[email protected]> AuthorDate: Thu, 15 Oct 2020 14:58:48 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Mon, 16 Nov 2020 17:43:28 +01:00
tools/power/x86_energy_perf_policy: Read energy_perf_bias from sysfs
... and stop poking at the MSR directly.
--- tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 99 insertions(+), 10 deletions(-)
tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c +99 -10
@@ -91,6 +91,9 @@ unsigned int has_hwp_request_pkg; /* IA32_HWP_REQUEST_PKG */unsigned int bdx_highest_ratio; +#define PATH_TO_CPU "/sys/devices/system/cpu/" +#define SYSFS_PATH_MAX 255 +/* * maintain compatibility with original implementation, but don't document it: */
@@ -668,6 +671,48 @@ int put_msr(int cpu, int offset, unsigned long long new_msr)return 0 ; } +static unsigned int read_sysfs (const char *path,char *buf,size_t buflen) +{ +ssize_t numread; +int fd; + + fd =open (path,O_RDONLY ); +if (fd ==-1 ) +return 0 ; + + numread =read (fd, buf, buflen -1 ); +if (numread <1 ) { +close (fd); +return 0 ; + } + + buf[numread] ='\0' ; +close (fd); + +return (unsigned int ) numread; +} + +static unsigned int write_sysfs (const char *path,char *buf,size_t buflen) +{ +ssize_t numwritten; +int fd; + + fd =open (path,O_WRONLY ); +if (fd ==-1 ) +return 0 ; + + numwritten =write (fd, buf, buflen -1 ); +if (numwritten <1 ) { +perror ("write failed\n" ); +close (fd); +return -1 ; + } + +close (fd); + +return (unsigned int ) numwritten; +} +void print_hwp_cap (int cpu,struct msr_hwp_cap *cap,char *str) {if (cpu !=-1 )
@@ -745,17 +790,61 @@ void write_hwp_request(int cpu, struct msr_hwp_request *hwp_req, unsigned int msput_msr (cpu, msr_offset, msr); } +static int get_epb (int cpu) +{ +char path[SYSFS_PATH_MAX ]; +char linebuf[3 ]; +char *endp; +long val; + +if (!has_epb) +return -1 ; + +snprintf (path,sizeof (path),PATH_TO_CPU "cpu%u/power/energy_perf_bias" , cpu); + +if (!read_sysfs (path, linebuf,3 )) +return -1 ; + + val =strtol (linebuf, &endp,0 ); +if (endp == linebuf || errno ==ERANGE ) +return -1 ; + +return (int )val; +} + +static int set_epb (int cpu,int val) +{ +char path[SYSFS_PATH_MAX ]; +char linebuf[3 ]; +char *endp; +int ret; + +if (!has_epb) +return -1 ; + +snprintf (path,sizeof (path),PATH_TO_CPU "cpu%u/power/energy_perf_bias" , cpu); +snprintf (linebuf,sizeof (linebuf),"%d" , val); + + ret =write_sysfs (path, linebuf,3 ); +if (ret <=0 ) +return -1 ; + + val =strtol (linebuf, &endp,0 ); +if (endp == linebuf || errno ==ERANGE ) +return -1 ; + +return (int )val; +} +int print_cpu_msrs (int cpu) { -unsigned long long msr;struct msr_hwp_request req;struct msr_hwp_cap cap; +int epb; -if (has_epb) { -get_msr (cpu,MSR_IA32_ENERGY_PERF_BIAS , &msr); - -printf ("cpu%d: EPB %u\n" , cpu, (unsigned int ) msr); - } + epb =get_epb (cpu); +if (epb >=0 ) +printf ("cpu%d: EPB %u\n" , cpu, (unsigned int ) epb);if (!has_hwp)return 0 ;
@@ -1038,15 +1127,15 @@ int enable_hwp_on_cpu(int cpu)int update_cpu_msrs (int cpu) {unsigned long long msr; - +int epb;if (update_epb) { -get_msr (cpu,MSR_IA32_ENERGY_PERF_BIAS , &msr); -put_msr (cpu,MSR_IA32_ENERGY_PERF_BIAS , new_epb); + epb =get_epb (cpu); +set_epb (cpu, new_epb);if (verbose)printf ("cpu%d: ENERGY_PERF_BIAS old: %d new: %d\n" , - cpu, (unsigned int ) msr, (unsigned int ) new_epb); + cpu, epb, (unsigned int ) new_epb); }if (update_turbo) {