hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/I8WMOG CVE: NA
----------------------------------------
We need a cmdline to decide whether smart_grid is enabled.
Need to add "smart_grid" string in kernel boot cmdline, if we want to enable the feature.
Signed-off-by: Yipeng Zou zouyipeng@huawei.com --- drivers/cpufreq/cpufreq.c | 8 ++++++++ fs/proc/base.c | 6 ++++++ include/linux/sched.h | 14 ++++++++++++++ kernel/fork.c | 11 +++++++---- kernel/sched/core.c | 16 ++++++++-------- kernel/sched/fair.c | 20 +++++++++++++++----- 6 files changed, 58 insertions(+), 17 deletions(-)
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index 240f8c08dbdd..2322f6647372 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -2840,6 +2840,10 @@ static DEFINE_MUTEX(sg_zone_lock);
void cpufreq_smart_grid_start_sync(void) { + /* No need sync when smart grid disabled */ + if (!smart_grid_enabled()) + return; + if (likely(sg_zone.is_init)) irq_work_queue(&sg_zone.irq_work); } @@ -3038,6 +3042,10 @@ static int create_smart_grid_sysfs_file(void) { int ret;
+ /* No need init when smart grid disabled */ + if (!smart_grid_enabled()) + return 0; + ret = sysfs_create_file(cpufreq_global_kobject, &smart_grid_governor.attr); if (ret) pr_err("%s: cannot register global smart_grid_governor sysfs file\n", diff --git a/fs/proc/base.c b/fs/proc/base.c index f7d89d5575bc..0d9fbcd315ec 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -3323,6 +3323,9 @@ static int smart_grid_level_show(struct seq_file *m, void *v) struct inode *inode = m->private; struct task_struct *p;
+ if (!smart_grid_enabled()) + return -EPERM; + p = get_proc_task(inode); if (!p) return -ESRCH; @@ -3350,6 +3353,9 @@ static ssize_t smart_grid_level_write(struct file *file, const char __user *buf, unsigned int level = SCHED_GRID_QOS_TASK_LEVEL_MAX; int ret = 0;
+ if (!smart_grid_enabled()) + return -EPERM; + memset(buffer, 0, sizeof(buffer)); if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count)) return -EFAULT; diff --git a/include/linux/sched.h b/include/linux/sched.h index 569653f9b420..dafcae3e0ec9 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -2519,8 +2519,22 @@ static inline bool dynamic_affinity_enabled(void)
#ifdef CONFIG_QOS_SCHED_SMART_GRID extern struct static_key __smart_grid_used; +extern struct static_key_false __smart_grid_switch; + +static inline bool smart_grid_enabled(void) +{ + /* smart grid need dynamic affinity enabled first */ + if (!static_branch_unlikely(&__dynamic_affinity_switch)) + return false; + + return static_branch_unlikely(&__smart_grid_switch); +} + static inline bool smart_grid_used(void) { + if (!smart_grid_enabled()) + return false; + return static_key_false(&__smart_grid_used); } #else diff --git a/kernel/fork.c b/kernel/fork.c index 72247526a384..fd0405523f07 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -633,7 +633,8 @@ void free_task(struct task_struct *tsk) sched_prefer_cpus_free(tsk); #endif #ifdef CONFIG_QOS_SCHED_SMART_GRID - sched_grid_qos_free(tsk); + if (smart_grid_enabled()) + sched_grid_qos_free(tsk); #endif free_task_struct(tsk); } @@ -2396,9 +2397,11 @@ __latent_entropy struct task_struct *copy_process( current->flags &= ~PF_NPROC_EXCEEDED;
#ifdef CONFIG_QOS_SCHED_SMART_GRID - retval = sched_grid_qos_fork(p, current); - if (retval) - goto bad_fork_cleanup_count; + if (smart_grid_enabled()) { + retval = sched_grid_qos_fork(p, current); + if (retval) + goto bad_fork_cleanup_count; + } #endif
/* diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 76892d4bb260..44bdd78dc1c7 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -11360,7 +11360,7 @@ static u64 cpu_affinity_mode_read_u64(struct cgroup_subsys_state *css, { struct task_group *tg = css_tg(css);
- if (!dynamic_affinity_enabled()) + if (!smart_grid_enabled()) return -EPERM;
if (unlikely(!tg->auto_affinity)) @@ -11372,7 +11372,7 @@ static u64 cpu_affinity_mode_read_u64(struct cgroup_subsys_state *css, static int cpu_affinity_mode_write_u64(struct cgroup_subsys_state *css, struct cftype *cftype, u64 mode) { - if (!dynamic_affinity_enabled()) + if (!smart_grid_enabled()) return -EPERM;
return tg_set_dynamic_affinity_mode(css_tg(css), mode); @@ -11403,7 +11403,7 @@ u64 tg_get_affinity_period(struct task_group *tg) static int cpu_affinity_period_write_uint(struct cgroup_subsys_state *css, struct cftype *cftype, u64 period) { - if (!dynamic_affinity_enabled()) + if (!smart_grid_enabled()) return -EPERM;
return tg_set_affinity_period(css_tg(css), period); @@ -11412,7 +11412,7 @@ static int cpu_affinity_period_write_uint(struct cgroup_subsys_state *css, static u64 cpu_affinity_period_read_uint(struct cgroup_subsys_state *css, struct cftype *cft) { - if (!dynamic_affinity_enabled()) + if (!smart_grid_enabled()) return -EPERM;
return tg_get_affinity_period(css_tg(css)); @@ -11426,7 +11426,7 @@ static int cpu_affinity_domain_mask_write_u64(struct cgroup_subsys_state *css, struct affinity_domain *ad; u16 full;
- if (!dynamic_affinity_enabled()) + if (!smart_grid_enabled()) return -EPERM;
if (unlikely(!tg->auto_affinity)) @@ -11448,7 +11448,7 @@ static u64 cpu_affinity_domain_mask_read_u64(struct cgroup_subsys_state *css, { struct task_group *tg = css_tg(css);
- if (!dynamic_affinity_enabled()) + if (!smart_grid_enabled()) return -EPERM;
if (unlikely(!tg->auto_affinity)) @@ -11464,8 +11464,8 @@ static int cpu_affinity_stat_show(struct seq_file *sf, void *v) struct affinity_domain *ad; int i;
- /* No stat when dynamic affinity disabled */ - if (!dynamic_affinity_enabled()) + /* No stat when smart grid disabled */ + if (!smart_grid_enabled()) return -EPERM;
if (unlikely(!auto_affi)) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 1fe65776dd86..80c7232b2aaf 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -6799,6 +6799,16 @@ static inline void sched_fair_update_stop_tick(struct rq *rq, struct task_struct #endif
#ifdef CONFIG_QOS_SCHED_SMART_GRID + +DEFINE_STATIC_KEY_FALSE(__smart_grid_switch); + +static int __init smart_grid_switch_setup(char *__unused) +{ + static_branch_enable(&__smart_grid_switch); + return 1; +} +__setup("smart_grid", smart_grid_switch_setup); + #define AUTO_AFFINITY_DEFAULT_PERIOD_MS 2000 #define IS_DOMAIN_SET(level, mask) ((1 << (level)) & (mask))
@@ -6955,8 +6965,8 @@ void tg_update_affinity_domains(int cpu, int online) { int cpu_state[2];
- /* No need update when dynamic affinity disabled */ - if (!dynamic_affinity_enabled()) + /* No need update when smart gird disabled */ + if (!smart_grid_enabled()) return;
cpu_state[0] = cpu; @@ -7188,8 +7198,8 @@ int init_auto_affinity(struct task_group *tg) struct auto_affinity *auto_affi; int ret;
- /* No need init auto affinity when dynamic affinity disabled */ - if (!dynamic_affinity_enabled()) + /* No need init auto affinity when smart grid disabled */ + if (!smart_grid_enabled()) return 0;
auto_affi = kzalloc(sizeof(*auto_affi), GFP_KERNEL); @@ -7225,7 +7235,7 @@ static void destroy_auto_affinity(struct task_group *tg) { struct auto_affinity *auto_affi = tg->auto_affinity;
- if (!dynamic_affinity_enabled()) + if (!smart_grid_enabled()) return;
if (unlikely(!auto_affi))