cpufreq: schedutil: Fix sugov_start versus sugov_update_shared race
Subsystems (2)
PATCH 1/1 cpufreq: schedutil: Fix sugov_start versus sugov_update_shared race
With a shared policy in place, when one of the CPUs in the policy is hotplugged out and then brought back online, sugov_stop and sugov_start are called in order.
sugov_stop removes utilization hooks for each CPU in the policy and does nothing else in the for_each_cpu loop. sugov_start on the other hand iterates through the CPUs in the policy and re-initializes the per-cpu structure _and_ adds the utilization hook. This implies that the scheduler is allowed to invoke a CPU's utilization update hook when the rest of the per-cpu structures have yet to be re-inited.
Apart from some strange values in tracepoints this doesn't cause a problem, but if we do end up accessing a pointer from the per-cpu sugov_cpu structure somewhere in the sugov_update_shared path, we will likely see crashes since the memset for another CPU in the policy is free to race with sugov_update_shared from the CPU that is ready to go. So let's fix this now to first init all per-cpu structures, and then add the per-cpu utilization update hooks all at once.
--- kernel/sched/cpufreq_schedutil.c | 5 +++++ 1 file changed, 5 insertions(+)
kernel/sched/cpufreq_schedutil.c +5 -0
@@ -610,6 +610,11 @@ static int sugov_start(struct cpufreq_policy *policy) sg_cpu->sg_policy = sg_policy; sg_cpu->flags =SCHED_CPUFREQ_RT ; sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq; + } + +for_each_cpu (cpu, policy->cpus) { +struct sugov_cpu *sg_cpu = &per_cpu (sugov_cpu, cpu); + cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util, policy_is_shared(policy) ? sugov_update_shared :
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project
Quoted 20 lines · Vikram Mulukutla
With a shared policy in place, when one of the CPUs in the policy is hotplugged out and then brought back online,
sugov_stopandsugov_startare called in order.
sugov_stopremoves utilization hooks for each CPU in the policy and does nothing else in thefor_each_cpuloop.sugov_starton the other hand iterates through the CPUs in the policy and re-initializes the per-cpu structure _and_ adds the utilization hook. This implies that the scheduler is allowed to invoke a CPU's utilization update hook when the rest of the per-cpu structures have yet to be re-inited.Apart from some strange values in tracepoints this doesn't cause a problem, but if we do end up accessing a pointer from the per-cpu
sugov_cpustructure somewhere in thesugov_update_sharedpath, we will likely see crashes since the memset for another CPU in the policy is free to race withsugov_update_sharedfrom the CPU that is ready to go. So let's fix this now to first init all per-cpu structures, and then add the per-cpu utilization update hooks all at once.
Quoted 1 line
Signed-off-by Vikram Mulukutla <[email protected]>
OK
Any objections anyone?
Quoted 21 lines
--- kernel/sched/cpufreq_schedutil.c | 5 +++++ 1 file changed, 5 insertions(+)kernel/sched/cpufreq_schedutil.c +5
-0@@ -610,6 +610,11 @@ static int sugov_start(struct cpufreq_policy *policy) sg_cpu->sg_policy = sg_policy; sg_cpu->flags =SCHED_CPUFREQ_RT ; sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq; + } + +for_each_cpu (cpu, policy->cpus) { +struct sugov_cpu *sg_cpu = &per_cpu (sugov_cpu, cpu); + cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util, policy_is_shared(policy) ? sugov_update_shared :
Quoted 31 lines · Vikram Mulukutla
With a shared policy in place, when one of the CPUs in the policy is hotplugged out and then brought back online,
sugov_stopandsugov_startare called in order.
sugov_stopremoves utilization hooks for each CPU in the policy and does nothing else in thefor_each_cpuloop.sugov_starton the other hand iterates through the CPUs in the policy and re-initializes the per-cpu structure _and_ adds the utilization hook. This implies that the scheduler is allowed to invoke a CPU's utilization update hook when the rest of the per-cpu structures have yet to be re-inited.Apart from some strange values in tracepoints this doesn't cause a problem, but if we do end up accessing a pointer from the per-cpu
sugov_cpustructure somewhere in thesugov_update_sharedpath, we will likely see crashes since the memset for another CPU in the policy is free to race withsugov_update_sharedfrom the CPU that is ready to go. So let's fix this now to first init all per-cpu structures, and then add the per-cpu utilization update hooks all at once.Signed-off-by Vikram Mulukutla <[email protected]>--- kernel/sched/cpufreq_schedutil.c | 5 +++++ 1 file changed, 5 insertions(+)kernel/sched/cpufreq_schedutil.c +0
-0@@ -610,6 +610,11 @@ static int sugov_start(struct cpufreq_policy *policy) sg_cpu->sg_policy = sg_policy;
kernel/sched/cpufreq_schedutil.c static int sugov_start(struct cpufreq_policy *policy) · line 620
Quoted 10 lines
kernel/sched/cpufreq_schedutil.c +5
-0sg_cpu->flags =SCHED_CPUFREQ_RT ; sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq; + } + +for_each_cpu (cpu, policy->cpus) { +struct sugov_cpu *sg_cpu = &per_cpu (sugov_cpu, cpu); + cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util, policy_is_shared(policy) ? sugov_update_shared :
Signature · 1 line
viresh
Rafael J. Wysocki
AppliedQuoted 36 lines · Viresh Kumar
Quoted 35 lines · Vikram Mulukutla
With a shared policy in place, when one of the CPUs in the policy is hotplugged out and then brought back online,
sugov_stopandsugov_startare called in order.
sugov_stopremoves utilization hooks for each CPU in the policy and does nothing else in thefor_each_cpuloop.sugov_starton the other hand iterates through the CPUs in the policy and re-initializes the per-cpu structure _and_ adds the utilization hook. This implies that the scheduler is allowed to invoke a CPU's utilization update hook when the rest of the per-cpu structures have yet to be re-inited.Apart from some strange values in tracepoints this doesn't cause a problem, but if we do end up accessing a pointer from the per-cpu
sugov_cpustructure somewhere in thesugov_update_sharedpath, we will likely see crashes since the memset for another CPU in the policy is free to race withsugov_update_sharedfrom the CPU that is ready to go. So let's fix this now to first init all per-cpu structures, and then add the per-cpu utilization update hooks all at once.Signed-off-by Vikram Mulukutla <[email protected]>--- kernel/sched/cpufreq_schedutil.c | 5 +++++ 1 file changed, 5 insertions(+)kernel/sched/cpufreq_schedutil.c +2
-0@@ -610,6 +610,11 @@ static int sugov_start(struct cpufreq_policy *policy) sg_cpu->sg_policy = sg_policy; sg_cpu->flags =SCHED_CPUFREQ_RT ; sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq; + } +
Quoted 8 lines
kernel/sched/cpufreq_schedutil.c static int sugov_start(struct cpufreq_policy *policy) · line 620
Quoted 6 lines
kernel/sched/cpufreq_schedutil.c +3
-0+for_each_cpu (cpu, policy->cpus) { +struct sugov_cpu *sg_cpu = &per_cpu (sugov_cpu, cpu); + cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util, policy_is_shared(policy) ? sugov_update_shared :Acked-by Viresh Kumar <[email protected]>
Applied, thanks!