Skip to contentSkip to conversation

sched/fair: Fix CPU bandwidth limit bypass during CPU hotplug

PATCH Superseded Vishal Chourasia 3 participants 4 messages Nov 26, 2024 – Dec 7, 2024 Scheduler

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched/fair: Fix CPU bandwidth limit bypass during CPU hotplug

CPU controller limits are not properly enforced during CPU hotplug operations, particularly during CPU offline. When a CPU goes offline, throttled processes are unintentionally being unthrottled across all CPUs in the system, allowing them to exceed their assigned quota limits.

Assigning 6.25% bandwidth limit to a cgroup in a 8 CPU system, where, workload is running 8 threads for 20 seconds at 100% CPU utilization, expected (user+sys) time = 10 seconds.

# cat /sys/fs/cgroup/test/cpu.max
50000 100000
# ./ebizzy -t 8 -S 20        // non-hotplug case
real 20.00 s
user 10.81 s                 // intented behaviour
sys   0.00 s
# ./ebizzy -t 8 -S 20        // hotplug case
real 20.00 s
user 14.43 s                 // Workload is able to run for 14 secs
sys   0.00 s                 // when it should have only run for 10 secs

During CPU hotplug, scheduler domains are rebuilt and cpu_attach_domain is called for every active CPU to update the root domain. That ends up calling rq_offline_fair which un-throttles any throttled hierarchies.

Unthrottling should only occur for the CPU being hotplugged to allow its throttled processes to become runnable and get migrated to other CPUs.

With current patch applied,

# ./ebizzy -t 8 -S 20        // hotplug case
real 21.00 s
user 10.16 s                 // intented behaviour
sys   0.00 s

Note: hotplug operation (online, offline) was performed in while(1) loop

---
 kernel/sched/fair.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/fair.c +2 -1
index fbdca89c677f..c436e2307e6f 100644
@@ -6684,7 +6684,8 @@ static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)
 	list_for_each_entry_rcu(tg, &task_groups, list) {
 		struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
 
-		if (!cfs_rq->runtime_enabled)
+		/* Don't unthrottle an active cfs_rq unnecessarily */
+		if (!cfs_rq->runtime_enabled || cpumask_test_cpu(cpu_of(rq), cpu_active_mask))
 			continue;
 
 		/*
Signature · 1 line
2.47.0
Tested-by on v1 1/1
#1 lore raw

Hi Vishal,

Quoted 29 lines · Vishal Chourasia

CPU controller limits are not properly enforced during CPU hotplug operations, particularly during CPU offline. When a CPU goes offline, throttled processes are unintentionally being unthrottled across all CPUs in the system, allowing them to exceed their assigned quota limits.

Assigning 6.25% bandwidth limit to a cgroup in a 8 CPU system, where, workload is running 8 threads for 20 seconds at 100% CPU utilization, expected (user+sys) time = 10 seconds.

# cat /sys/fs/cgroup/test/cpu.max
50000 100000
# ./ebizzy -t 8 -S 20        // non-hotplug case
real 20.00 s
user 10.81 s                 // intented behaviour
sys   0.00 s
# ./ebizzy -t 8 -S 20        // hotplug case
real 20.00 s
user 14.43 s                 // Workload is able to run for 14 secs
sys   0.00 s                 // when it should have only run for 10 secs

During CPU hotplug, scheduler domains are rebuilt and cpu_attach_domain is called for every active CPU to update the root domain. That ends up calling rq_offline_fair which un-throttles any throttled hierarchies.

Unthrottling should only occur for the CPU being hotplugged to allow its throttled processes to become runnable and get migrated to other CPUs.

Quoted 7 lines

With current patch applied,

# ./ebizzy -t 8 -S 20        // hotplug case
real 21.00 s
user 10.16 s                 // intented behaviour
sys   0.00 s

Note: hotplug operation (online, offline) was performed in while(1) loop

Tested with and without this patch for the ebizzy workload as mentioned.

Without the patch:
------------------
19376 records/s
real 20.00 s
user 12.49 s
sys 0.00 s

With the patch:
---------------
17708 records/s
real 20.00 s
user 10.07 s
sys 0.00 s

Hence,

Thanks,
Madadi Vineeth Reddy

Quoted 20 lines
---
 kernel/sched/fair.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/fair.c +2 -1
index fbdca89c677f..c436e2307e6f 100644
@@ -6684,7 +6684,8 @@ static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)
 	list_for_each_entry_rcu(tg, &task_groups, list) {
 		struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
 
-		if (!cfs_rq->runtime_enabled)
+		/* Don't unthrottle an active cfs_rq unnecessarily */
+		if (!cfs_rq->runtime_enabled || cpumask_test_cpu(cpu_of(rq), cpu_active_mask))
 			continue;
 
 		/*
#2 lore raw
In reply to Vishal Chourasia
Quoted 5 lines · Vishal Chourasia

CPU controller limits are not properly enforced during CPU hotplug operations, particularly during CPU offline. When a CPU goes offline, throttled processes are unintentionally being unthrottled across all CPUs in the system, allowing them to exceed their assigned quota limits.

<snip>

Quoted 10 lines
---
 kernel/sched/fair.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/fair.c +0 -0
index fbdca89c677f..c436e2307e6f 100644

kernel/sched/fair.c static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq) · line 6687

Quoted 5 lines
kernel/sched/fair.c +1 -1
@@ -6684,7 +6684,8 @@ static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)
 	list_for_each_entry_rcu(tg, &task_groups, list) {
 		struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
 
-		if (!cfs_rq->runtime_enabled)
+		/* Don't unthrottle an active cfs_rq unnecessarily */

Per the patch description, its not just unnecessary but unthrottling is buggy. Unnecessary would mean its just an overhead. Here we dont want to unthrottle. Other than that this seems to be fine to me.

Quoted 2 lines
kernel/sched/fair.c +1 -0
+		if (!cfs_rq->runtime_enabled || cpumask_test_cpu(cpu_of(rq), cpu_active_mask))
 			continue;
Signature · 2 lines
Thanks and Regards
Srikar Dronamraju
#3 lore raw
Quoted 22 lines · Srikar Dronamraju
Quoted 5 lines · Vishal Chourasia

CPU controller limits are not properly enforced during CPU hotplug operations, particularly during CPU offline. When a CPU goes offline, throttled processes are unintentionally being unthrottled across all CPUs in the system, allowing them to exceed their assigned quota limits.

<snip>

Quoted 13 lines
---
 kernel/sched/fair.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/sched/fair.c +0 -0
index fbdca89c677f..c436e2307e6f 100644
@@ -6684,7 +6684,8 @@ static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq)
 	list_for_each_entry_rcu(tg, &task_groups, list) {
 		struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
 
Quoted 6 lines

kernel/sched/fair.c static void __maybe_unused unthrottle_offline_cfs_rqs(struct rq *rq) · line 6687

Quoted 2 lines
kernel/sched/fair.c +1 -1
-		if (!cfs_rq->runtime_enabled)
+		/* Don't unthrottle an active cfs_rq unnecessarily */

Per the patch description, its not just unnecessary but unthrottling is buggy. Unnecessary would mean its just an overhead. Here we dont want to unthrottle. Other than that this seems to be fine to me.

sure.

Also, I missed running the patch against checkpatch.pl

Will be sending out v2 with the updates.

vishalc

Quoted 7 lines
Quoted 3 lines
kernel/sched/fair.c +1 -0
+		if (!cfs_rq->runtime_enabled || cpumask_test_cpu(cpu_of(rq), cpu_active_mask))
 			continue;
Signature · 2 lines
Thanks and Regards
Srikar Dronamraju