x86/cpufreq: Add Kconfig option for CPU frequency scaling
PATCH v4 1/1 x86/cpufreq: Add Kconfig option for CPU frequency scaling
Add CONFIG_CPUFREQ to allow CPU frequency scaling support to be disabled at build time. When disabled, this removes cpufreq code from the build.
Introduce IS_ENABLED(CONFIG_CPUFREQ) checks for relevant do_pm_op and do_platform_op subops and other functions that require CONFIG_CPUFREQ to work.
Add stubs where necessary.
---
Changes in v4:
- fix IS_ENABLED(CPUFREQ)
- remove #ifdef in platform_hypercall.c and use DCE
- move cpufreq_controller enum out of #ifdef
--- xen/arch/x86/acpi/Makefile | 2 +- xen/arch/x86/platform_hypercall.c | 6 ++++-- xen/drivers/Makefile | 2 +- xen/drivers/acpi/pm-op.c | 3 ++- xen/drivers/acpi/pmstat.c | 8 ++++++-- xen/drivers/cpufreq/Kconfig | 14 +++++++++++++- xen/include/acpi/cpufreq/cpufreq.h | 13 ++++++++++++- xen/include/acpi/cpufreq/processor_perf.h | 3 --- xen/include/xen/acpi.h | 8 ++++++++ xen/include/xen/pmstat.h | 15 ++++++++++++++- xen/include/xen/sched.h | 7 +++++++ 11 files changed, 68 insertions(+), 13 deletions(-)
xen/arch/x86/acpi/Makefile +1 -1
@@ -1,4 +1,4 @@ -obj-y += cpufreq/ +obj-$(CONFIG_CPUFREQ) += cpufreq/ obj-y += lib.o power.o cpu_idle.o cpuidle_menu.o obj-bin-y += boot.init.o wakeup_prot.o
xen/arch/x86/platform_hypercall.c +4 -2
@@ -487,7 +487,8 @@ ret_t do_platform_op(case XENPF_change_freq: ret = -ENOSYS ; -if ( cpufreq_controller != FREQCTL_dom0_kernel ) +if ( !IS_ENABLED (CONFIG_CPUFREQ ) || + cpufreq_controller != FREQCTL_dom0_kernel )break ; ret = -EINVAL ;if ( op->u.change_freq.flags || !cpu_online (op->u.change_freq.cpu) )
@@ -507,7 +508,8 @@ ret_t do_platform_op(XEN_GUEST_HANDLE (uint64) idletimes; ret = -ENOSYS ; -if ( cpufreq_controller != FREQCTL_dom0_kernel ) +if ( !IS_ENABLED (CONFIG_CPUFREQ ) || + cpufreq_controller != FREQCTL_dom0_kernel )break ; ctlmap.nr_bits = op->u.getidletime.cpumap_nr_cpus;
xen/drivers/Makefile +1 -1
@@ -1,5 +1,5 @@ obj-y += char/ -obj-$(CONFIG_HAS_CPUFREQ) += cpufreq/ +obj-$(CONFIG_CPUFREQ) += cpufreq/ obj-$(CONFIG_HAS_PCI) += pci/ obj-$(CONFIG_HAS_VPCI) += vpci/ obj-$(CONFIG_HAS_PASSTHROUGH) += passthrough/
xen/drivers/acpi/pm-op.c +2 -1
@@ -367,7 +367,8 @@ int do_pm_op(struct xen_sysctl_pm_op *op)return ret; } -if ( op->cpuid >= nr_cpu_ids || !cpu_online (op->cpuid) ) +if ( op->cpuid >= nr_cpu_ids || !cpu_online (op->cpuid) || + !IS_ENABLED (CONFIG_CPUFREQ ) )return -EINVAL ; pmpt = processor_pminfo[op->cpuid];
xen/drivers/acpi/pmstat.c +6 -2
@@ -76,6 +76,9 @@ void cpufreq_statistic_update(unsigned int cpu, uint8_t from, uint8_t to)spinlock_t *cpufreq_statistic_lock = &per_cpu (cpufreq_statistic_lock, cpu); +if ( !IS_ENABLED (CONFIG_CPUFREQ ) ) +return ; +spin_lock (cpufreq_statistic_lock); pxpt =per_cpu (cpufreq_statistic_data, cpu);
@@ -105,7 +108,7 @@ int cpufreq_statistic_init(unsigned int cpu)spin_lock_init (cpufreq_statistic_lock); -if ( !pmpt ) +if ( !pmpt || !IS_ENABLED (CONFIG_CPUFREQ ) )return -EINVAL ;/* Only need to initialize in Px mode */
@@ -225,7 +228,8 @@ int do_get_pm_info(struct xen_sysctl_get_pmstat *op)int ret =0 ;const struct processor_pminfo *pmpt; -if ( !op || (op->cpuid >= nr_cpu_ids) || !cpu_online (op->cpuid) ) +if ( !IS_ENABLED (CONFIG_CPUFREQ ) || !op || (op->cpuid >= nr_cpu_ids) || + !cpu_online (op->cpuid) )return -EINVAL ; pmpt = processor_pminfo[op->cpuid];
xen/drivers/cpufreq/Kconfig +13 -1
@@ -1,3 +1,15 @@ - config HAS_CPUFREQ bool + +config CPUFREQ + bool "CPU Frequency scaling" + default y + depends on HAS_CPUFREQ + help + Enable CPU frequency scaling and power management governors. + This allows Xen to dynamically adjust CPU P-states (performance + states) based on system load. + + Disabling this option removes all cpufreq governors and power + management interfaces. This is useful for real-time systems or + minimal hypervisor builds.
xen/include/acpi/cpufreq/cpufreq.h +12 -1
@@ -381,8 +381,19 @@ int write_ondemand_up_threshold(unsigned int up_threshold);int write_userspace_scaling_setspeed (unsigned int cpu,unsigned int freq); +#ifdef CONFIG_CPUFREQ +int cpufreq_add_cpu (unsigned int cpu); +int cpufreq_del_cpu (unsigned int cpu); +void cpufreq_dbs_timer_suspend (void );void cpufreq_dbs_timer_resume (void ); +#else +static inline int cpufreq_add_cpu (unsigned int cpu) {return -EOPNOTSUPP ; } +static inline int cpufreq_del_cpu (unsigned int cpu) {return -EOPNOTSUPP ; } + +static inline void cpufreq_dbs_timer_suspend (void ) {} +static inline void cpufreq_dbs_timer_resume (void ) {} +#endif void intel_feature_detect (struct cpufreq_policy *policy);
@@ -398,7 +409,7 @@ void intel_feature_detect(struct cpufreq_policy *policy);int hwp_cmdline_parse (const char *s,const char *e);int hwp_register_driver (void ); -#ifdef CONFIG_INTEL +#if defined(CONFIG_INTEL ) && defined(CONFIG_CPUFREQ )bool hwp_active (void );#else static inline bool hwp_active (void ) {return false; }
xen/include/acpi/cpufreq/processor_perf.h +0 -3
@@ -36,9 +36,6 @@ static inline void cpufreq_statistic_exit(unsigned int cpu) {}int cpufreq_limit_change (unsigned int cpu); -int cpufreq_add_cpu (unsigned int cpu); -int cpufreq_del_cpu (unsigned int cpu); -struct processor_performance {uint32_t state;uint32_t platform_limit;
xen/include/xen/acpi.h +8 -0
@@ -186,7 +186,15 @@ static inline void acpi_set_csubstate_limit(unsigned int new_limit) { return; }#endif #ifdef XEN_GUEST_HANDLE +#ifdef CONFIG_CPUFREQ int acpi_set_pdc_bits (unsigned int acpi_id,XEN_GUEST_HANDLE (uint32 )); +#else +static inline int acpi_set_pdc_bits (unsigned int acpi_id, +XEN_GUEST_HANDLE (uint32) pdc) +{ +return -EOPNOTSUPP ; +} +#endif #endif int arch_acpi_set_pdc_bits (u32 acpi_id,u32 *,u32 mask);
xen/include/xen/pmstat.h +14 -1
@@ -5,10 +5,23 @@#include <public/platform.h> /* for struct xen_processor_power */ #include <public/sysctl.h> /* for struct pm_cx_stat */ +#ifdef CONFIG_CPUFREQ int set_px_pminfo (uint32_t acpi_id,struct xen_processor_performance *perf); -long set_cx_pminfo (uint32_t acpi_id,struct xen_processor_power *power);int set_cppc_pminfo (unsigned int acpi_id,const struct xen_processor_cppc *cppc_data); +#else +static inline int set_px_pminfo (uint32_t acpi_id, +struct xen_processor_performance *perf) +{ +return -EOPNOTSUPP ; +} +static inline int set_cppc_pminfo (unsigned int acpi_id, +const struct xen_processor_cppc *cppc_data) +{ +return -EOPNOTSUPP ; +} +#endif +long set_cx_pminfo (uint32_t acpi_id,struct xen_processor_power *power);#ifdef CONFIG_COMPAT struct compat_processor_performance ;
xen/include/xen/sched.h +7 -0
@@ -1259,6 +1259,7 @@ extern enum cpufreq_controller { FREQCTL_none, FREQCTL_dom0_kernel, FREQCTL_xen } cpufreq_controller; +#ifdef CONFIG_CPUFREQ static always_inline bool is_cpufreq_controller(const struct domain *d) {/*
@@ -1274,6 +1275,12 @@ static always_inline bool is_cpufreq_controller(const struct domain *d)return (is_pv_domain (d) &&is_hardware_domain (d) && cpufreq_controller == FREQCTL_dom0_kernel); } +#else +static always_inline bool is_cpufreq_controller(const struct domain *d) +{ +return false; +} +#endif int cpupool_move_domain (struct domain *d,struct cpupool *c);int cpupool_do_sysctl (struct xen_sysctl_cpupool_op *op);
Signature · 1 line
2.25.1
xen/drivers/acpi/pm-op.c int do_pm_op(struct xen_sysctl_pm_op *op) · line 371
Quoted 3 lines · Stefano Stabellini
xen/drivers/acpi/pm-op.c +2
-1-if ( op->cpuid >= nr_cpu_ids || !cpu_online (op->cpuid) ) +if ( op->cpuid >= nr_cpu_ids || !cpu_online (op->cpuid) || + !IS_ENABLED (CONFIG_CPUFREQ ) )
Largely inconsequential here (and other places in the patch), but I think IS_ENABLED(foo) should go first in all conditional chains so that any invoked function in the OR list is skipped. It's likely in this case it doesn't matter, because cpu_onlin() is a macro that calls a static inline so the whole AST is known to the compiler, but it's trickier when there's non-obvious extern functions at play. In those cases the compiler will still make the calls because it can't know the call won't have side effects.
Thus, as a matter of principle, I think IS_ENABLED() checks should always come first.
Cheers,
Alejandro
Quoted 8 lines · Stefano Stabellini
Add
CONFIG_CPUFREQto allow CPU frequency scaling support to be disabled at build time. When disabled, this removes cpufreq code from the build.Introduce
IS_ENABLED(CONFIG_CPUFREQ)checks for relevantdo_pm_opanddo_platform_opsubops and other functions that requireCONFIG_CPUFREQto work.
Quoted 8 lines
Add stubs where necessary.
Signed-off-by Stefano Stabellini <[email protected]>---
Changes in v4:
- fixIS_ENABLED(CPUFREQ)
- remove #ifdef inplatform_hypercall.cand use DCE
- movecpufreq_controllerenum out of #ifdef
Where did the v3 revlog go? Especially when submit patches faster than people can look at them, they might skip a version (or more), while still having looked at a yet earlier one.
xen/drivers/acpi/pm-op.c int do_pm_op(struct xen_sysctl_pm_op *op) · line 371
Quoted 9 lines
xen/drivers/acpi/pm-op.c +2
-1@@ -367,7 +367,8 @@ int do_pm_op(struct xen_sysctl_pm_op *op)return ret; } -if ( op->cpuid >= nr_cpu_ids || !cpu_online (op->cpuid) ) +if ( op->cpuid >= nr_cpu_ids || !cpu_online (op->cpuid) || + !IS_ENABLED (CONFIG_CPUFREQ ) )
Have the compile time constant part first? Else any possible side effects of the other expressions may prevent the compiler from fully dropping all code here.
Quoted 5 lines
xen/include/acpi/cpufreq/cpufreq.h +0
-0@@ -381,8 +381,19 @@ int write_ondemand_up_threshold(unsigned int up_threshold);int write_userspace_scaling_setspeed (unsigned int cpu,unsigned int freq);
xen/include/acpi/cpufreq/cpufreq.h int write_ondemand_up_threshold(unsigned int up_threshold); · line 392
Quoted 10 lines
xen/include/acpi/cpufreq/cpufreq.h +7
-0+#ifdef CONFIG_CPUFREQ +int cpufreq_add_cpu (unsigned int cpu); +int cpufreq_del_cpu (unsigned int cpu); +void cpufreq_dbs_timer_suspend (void );void cpufreq_dbs_timer_resume (void ); +#else +static inline int cpufreq_add_cpu (unsigned int cpu) {return -EOPNOTSUPP ; } +static inline int cpufreq_del_cpu (unsigned int cpu) {return -EOPNOTSUPP ; }
Returning an error here looks wrong, even if technically it is benign right now (as the one [each] remaining call doesn't check the error code).
xen/include/xen/acpi.h static inline void acpi_set_csubstate_limit(unsigned int new_limit) { return; } · line 195
Quoted 13 lines
xen/include/xen/acpi.h +6
-0@@ -186,7 +186,15 @@ static inline void acpi_set_csubstate_limit(unsigned int new_limit) { return; }#endif #ifdef XEN_GUEST_HANDLE +#ifdef CONFIG_CPUFREQ int acpi_set_pdc_bits(unsigned int acpi_id, XEN_GUEST_HANDLE(uint32)); +#else +static inline int acpi_set_pdc_bits(unsigned int acpi_id, + XEN_GUEST_HANDLE(uint32) pdc) +{ + return -EOPNOTSUPP;
Here use of an error indicator would result in the XENPF_set_processor_pminfo sub-op failing. That's not correct, as this is a notification from Dom0 to us. If we can't make use of the provided data, we should silently ignore it.
Quoted 12 lines
xen/include/xen/pmstat.h +2
-1@@ -5,10 +5,23 @@#include <public/platform.h> /* for struct xen_processor_power */ #include <public/sysctl.h> /* for struct pm_cx_stat */ +#ifdef CONFIG_CPUFREQ int set_px_pminfo (uint32_t acpi_id,struct xen_processor_performance *perf); -long set_cx_pminfo (uint32_t acpi_id,struct xen_processor_power *power);int set_cppc_pminfo (unsigned int acpi_id,const struct xen_processor_cppc *cppc_data); +#else
xen/include/xen/pmstat.h line 22
Quoted 10 lines
xen/include/xen/pmstat.h +10
-0+static inline int set_px_pminfo (uint32_t acpi_id, +struct xen_processor_performance *perf) +{ +return -EOPNOTSUPP ; +} +static inline int set_cppc_pminfo (unsigned int acpi_id, +const struct xen_processor_cppc *cppc_data) +{ +return -EOPNOTSUPP ; +}
Same here, I suppose.
xen/include/xen/sched.h extern enum cpufreq_controller { · line 1260
Quoted 5 lines
xen/include/xen/sched.h +0
-0@@ -1259,6 +1259,7 @@ extern enum cpufreq_controller { FREQCTL_none, FREQCTL_dom0_kernel, FREQCTL_xen } cpufreq_controller;
As previously indicated, these might better be invisible when CPUFREQ=n. But see also below.
How about (name subject to improvement)
static inline is_hwdom_cpufreq_controller(void)
{
#ifdef CONFIG_CPUFREQ
return cpufreq_controller == FREQCTL_dom0_kernel;
#else
return false;#endif
}
for use both ...
xen/include/xen/sched.h static always_inline bool is_cpufreq_controller(const struct domain *d) · line 1276
Quoted 7 lines
xen/include/xen/sched.h +1
-0+#ifdef CONFIG_CPUFREQ static always_inline bool is_cpufreq_controller(const struct domain *d) {/* @@ -1274,6 +1275,12 @@ static always_inline bool is_cpufreq_controller(const struct domain *d)return (is_pv_domain (d) &&is_hardware_domain (d) && cpufreq_controller == FREQCTL_dom0_kernel);
... here and in do_platform_op()? Or, keeping the enum visible,
static inline is_hwdom_cpufreq_controller(void)
{
return IS_ENABLED(CONFIG_CPUFREQ) &&
cpufreq_controller == FREQCTL_dom0_kernel;
}Of course the possibly-compile-time-constant part of the expression would then want to move first, to allow the fencing in is_pv_domain() to also be dropped.
xen/include/xen/sched.h static always_inline bool is_cpufreq_controller(const struct domain *d) · line 1283
Quoted 7 lines
xen/include/xen/sched.h +6
-0} +#else +static always_inline bool is_cpufreq_controller(const struct domain *d) +{ +return false; +} +#endif
Too much redundancy: The #ifdef would better be in the function body. But with the other adjustment the need for an #ifdef would disappear here anyway.
Jan