Some fixes for smart gird, as follows:
Hui Tang (5): sched: Fix timer storm for smart grid sched: fix dereference NULL pointers sched: Fix memory leak on error branch sched: clear credit count in error branch sched: Adjust few parameters range for smart grid
kernel/fork.c | 2 +- kernel/sched/core.c | 32 ++++++++++++++++++++++++++++---- kernel/sched/fair.c | 40 ++++++++++++++++++++-------------------- kernel/sysctl.c | 6 ++++-- 4 files changed, 53 insertions(+), 27 deletions(-)
From: Hui Tang tanghui20@huawei.com
hulk inclusion category: bugfix bugzilla: https://gitee.com/openeuler/kernel/issues/I7DSX6 CVE: NA
-------------------------------
Timer storm may be triggered if !cpumask_weight(ad->domains[i]) which is set in cpu offline.
Fixes: 713cfd2684fa ("sched: Introduce smart grid scheduling strategy for cfs") Signed-off-by: Hui Tang tanghui20@huawei.com Reviewed-by: Zhang Qiao zhangqiao22@huawei.com Signed-off-by: Zhang Changzhong zhangchangzhong@huawei.com --- kernel/sched/fair.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-)
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 3041652..a15fa5f 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -5343,15 +5343,13 @@ static void affinity_domain_up(struct task_group *tg) return;
while (level < ad->dcount) { - if (IS_DOMAIN_SET(level + 1, ad->domain_mask)) { + if (IS_DOMAIN_SET(level + 1, ad->domain_mask) && + cpumask_weight(ad->domains[level + 1]) > 0) { ad->curr_level = level + 1; - break; + return; } level++; } - - if (level == ad->dcount) - return; }
static void affinity_domain_down(struct task_group *tg) @@ -5363,15 +5361,15 @@ static void affinity_domain_down(struct task_group *tg) return;
while (level > 0) { + if (!cpumask_weight(ad->domains[level - 1])) + return; + if (IS_DOMAIN_SET(level - 1, ad->domain_mask)) { ad->curr_level = level - 1; - break; + return; } level--; } - - if (!level) - return; }
static enum hrtimer_restart sched_auto_affi_period_timer(struct hrtimer *timer) @@ -5391,11 +5389,8 @@ static enum hrtimer_restart sched_auto_affi_period_timer(struct hrtimer *timer) tg_capacity += capacity_of(cpu); }
- if (unlikely(!tg_capacity)) - return HRTIMER_RESTART; - raw_spin_lock_irqsave(&auto_affi->lock, flags); - if (util_avg_sum * 100 > tg_capacity * sysctl_sched_util_low_pct) { + if (util_avg_sum * 100 >= tg_capacity * sysctl_sched_util_low_pct) { affinity_domain_up(tg); } else if (util_avg_sum * 100 < tg_capacity * sysctl_sched_util_low_pct / 2) { @@ -5427,16 +5422,17 @@ static int tg_update_affinity_domain_down(struct task_group *tg, void *data) continue;
/* online */ - if (cpu_state[1]) + if (cpu_state[1]) { cpumask_set_cpu(cpu_state[0], ad->domains[i]); - else + } else { cpumask_clear_cpu(cpu_state[0], ad->domains[i]); + if (!cpumask_weight(ad->domains[i])) + affinity_domain_up(tg); + } + } raw_spin_unlock_irqrestore(&auto_affi->lock, flags);
- if (!smart_grid_used()) - return 0; - return 0; }
From: Hui Tang tanghui20@huawei.com
hulk inclusion category: bugfix bugzilla: https://gitee.com/openeuler/kernel/issues/I7EA1X CVE: NA
-------------------------------
tg->auto_affinity is NULL if init_auto_affinity() failed. So add checking for tg->auto_affinity before derefrence.
Fixes: 713cfd2684fa ("sched: Introduce smart grid scheduling strategy for cfs") Signed-off-by: Hui Tang tanghui20@huawei.com Reviewed-by: Zhang Qiao zhangqiao22@huawei.com Signed-off-by: Zhang Changzhong zhangchangzhong@huawei.com --- kernel/sched/core.c | 30 +++++++++++++++++++++++++++--- kernel/sched/fair.c | 3 +++ 2 files changed, 30 insertions(+), 3 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 8a7535c..7eea1f8 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -6989,6 +6989,9 @@ int tg_set_dynamic_affinity_mode(struct task_group *tg, u64 mode) { struct auto_affinity *auto_affi = tg->auto_affinity;
+ if (unlikely(!auto_affi)) + return -EPERM; + /* auto mode*/ if (mode == 1) { start_auto_affinity(auto_affi); @@ -7006,6 +7009,9 @@ static u64 cpu_affinity_mode_read_u64(struct cgroup_subsys_state *css, { struct task_group *tg = css_tg(css);
+ if (unlikely(!tg->auto_affinity)) + return -EPERM; + return tg->auto_affinity->mode; }
@@ -7017,6 +7023,9 @@ static int cpu_affinity_mode_write_u64(struct cgroup_subsys_state *css,
int tg_set_affinity_period(struct task_group *tg, u64 period_ms) { + if (unlikely(!tg->auto_affinity)) + return -EPERM; + if (period_ms > U64_MAX / NSEC_PER_MSEC) return -EINVAL;
@@ -7028,6 +7037,9 @@ int tg_set_affinity_period(struct task_group *tg, u64 period_ms)
u64 tg_get_affinity_period(struct task_group *tg) { + if (unlikely(!tg->auto_affinity)) + return -EPERM; + return ktime_to_ms(tg->auto_affinity->period); }
@@ -7048,9 +7060,14 @@ static int cpu_affinity_domain_mask_write_u64(struct cgroup_subsys_state *css, u64 mask) { struct task_group *tg = css_tg(css); - struct affinity_domain *ad = &tg->auto_affinity->ad; - u16 full = (1 << ad->dcount) - 1; + struct affinity_domain *ad; + u16 full; + + if (unlikely(!tg->auto_affinity)) + return -EPERM;
+ ad = &tg->auto_affinity->ad; + full = (1 << ad->dcount) - 1; if (mask > full) return -EINVAL;
@@ -7065,6 +7082,9 @@ static u64 cpu_affinity_domain_mask_read_u64(struct cgroup_subsys_state *css, { struct task_group *tg = css_tg(css);
+ if (unlikely(!tg->auto_affinity)) + return -EPERM; + return tg->auto_affinity->ad.domain_mask; }
@@ -7072,9 +7092,13 @@ static int cpu_affinity_stat_show(struct seq_file *sf, void *v) { struct task_group *tg = css_tg(seq_css(sf)); struct auto_affinity *auto_affi = tg->auto_affinity; - struct affinity_domain *ad = &auto_affi->ad; + struct affinity_domain *ad; int i;
+ if (unlikely(!auto_affi)) + return -EPERM; + + ad = &auto_affi->ad; seq_printf(sf, "period_active %d\n", auto_affi->period_active); seq_printf(sf, "dcount %d\n", ad->dcount); seq_printf(sf, "domain_mask 0x%x\n", ad->domain_mask); diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index a15fa5f..3c2bdbf 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -5697,6 +5697,9 @@ static void destroy_auto_affinity(struct task_group *tg) { struct auto_affinity *auto_affi = tg->auto_affinity;
+ if (unlikely(!auto_affi)) + return; + if (auto_affi->period_active) smart_grid_usage_dec();
From: Hui Tang tanghui20@huawei.com
hulk inclusion category: bugfix bugzilla: https://gitee.com/openeuler/kernel/issues/I7EBNA CVE: NA
-------------------------------
Fix memory leak on error branch for smart grid.
Fixes: 713cfd2684fa ("sched: Introduce smart grid scheduling strategy for cfs") Signed-off-by: Hui Tang tanghui20@huawei.com Reviewed-by: Zhang Qiao zhangqiao22@huawei.com Signed-off-by: Zhang Changzhong zhangchangzhong@huawei.com --- kernel/sched/fair.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 3c2bdbf..2115d79 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -5628,7 +5628,6 @@ static int init_affinity_domains(struct affinity_domain *ad) }
if (!sd) { - ad->dcount = 0; rcu_read_unlock(); return -EINVAL; } @@ -5636,8 +5635,10 @@ static int init_affinity_domains(struct affinity_domain *ad)
for (i = 0; i < dcount; i++) { ad->domains[i] = kmalloc(sizeof(cpumask_t), GFP_KERNEL); - if (!ad->domains[i]) + if (!ad->domains[i]) { + ad->dcount = i; goto err; + } }
rcu_read_lock();
From: Hui Tang tanghui20@huawei.com
hulk inclusion category: bugfix bugzilla: https://gitee.com/openeuler/kernel/issues/I7EBSH CVE: NA
-------------------------------
Clear credit count if sched_prefer_cpus_fork failed.
Fixes: 243865da2684 ("cpuset: Introduce new interface for scheduler dynamic affinity") Signed-off-by: Hui Tang tanghui20@huawei.com Reviewed-by: Zhang Qiao zhangqiao22@huawei.com Signed-off-by: Zhang Changzhong zhangchangzhong@huawei.com --- kernel/fork.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/kernel/fork.c b/kernel/fork.c index 8ca83c09..bfc4534 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1878,7 +1878,7 @@ static __latent_entropy struct task_struct *copy_process( #ifdef CONFIG_QOS_SCHED_DYNAMIC_AFFINITY retval = sched_prefer_cpus_fork(p, current); if (retval) - goto bad_fork_free; + goto bad_fork_cleanup_count; #endif #ifdef CONFIG_QOS_SCHED_SMART_GRID retval = sched_grid_qos_fork(p, current);
From: Hui Tang tanghui20@huawei.com
hulk inclusion category: bugfix bugzilla: https://gitee.com/openeuler/kernel/issues/I7EEF3 CVE: NA
-------------------------------
Adjust few parameters range for smart grid.
Fixes: 713cfd2684fa ("sched: Introduce smart grid scheduling strategy for cfs") Signed-off-by: Hui Tang tanghui20@huawei.com Reviewed-by: Zhang Qiao zhangqiao22@huawei.com Signed-off-by: Zhang Changzhong zhangchangzhong@huawei.com --- kernel/sched/core.c | 2 +- kernel/sysctl.c | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 7eea1f8..c7016a4 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -7026,7 +7026,7 @@ int tg_set_affinity_period(struct task_group *tg, u64 period_ms) if (unlikely(!tg->auto_affinity)) return -EPERM;
- if (period_ms > U64_MAX / NSEC_PER_MSEC) + if (!period_ms || period_ms > U64_MAX / NSEC_PER_MSEC) return -EINVAL;
raw_spin_lock_irq(&tg->auto_affinity->lock); diff --git a/kernel/sysctl.c b/kernel/sysctl.c index 592440a..c7064f6 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -139,7 +139,7 @@ static int one_thousand = 1000; #ifdef CONFIG_PRINTK static int ten_thousand = 10000; #endif -#ifdef CONFIG_QOS_SCHED +#if defined(CONFIG_QOS_SCHED) || defined(CONFIG_QOS_SCHED_SMART_GRID) static int hundred_thousand = 100000; #endif #ifdef CONFIG_PERF_EVENTS @@ -1343,7 +1343,9 @@ static struct ctl_table kern_table[] = { .data = &sysctl_affinity_adjust_delay_ms, .maxlen = sizeof(unsigned int), .mode = 0644, - .proc_handler = proc_dointvec, + .proc_handler = proc_dointvec_minmax, + .extra1 = &zero, + .extra2 = &hundred_thousand, }, #endif { }