Skip to contentSkip to conversation

sched/deadline: Fix zero-CPU DL bandwidth handling

PATCH In review Hui Su 2 participants 5 messages Sep 19 – Sep 22 Scheduler

lore
Share & tools
lore

Hui Su

author
#0 lore raw

v2 cover sched/deadline: Fix zero-CPU DL bandwidth handling

During CPU hot-unplug, a CPU is removed from cpu_active_mask before it becomes offline. If it is the last active CPU in a root domain, dl_bw_cpus() can therefore return zero while paths associated with the CPU are still reachable.

v1 addressed this in the DL server parameter-update path. Mikhail Zaslonko reported the same divide-by-zero on s390x through sched_setscheduler(), showing that the problem is more general, and pointed out several other callers of __dl_sub() and __dl_add().

This version moves the zero-CPU handling into those common helpers. The debugfs cpu_active() check is kept as a separate follow-up because it completes the offline-CPU check added by 4043f5498416, while the generic helper issue dates back much further. Keeping them separate also gives the two fixes the appropriate stable backport scopes.

Patch 1 fixes the generic divide-by-zero while retaining the existing total_bw update when no active CPU remains.

Patch 2 rejects per-CPU DL server debugfs writes once the CPU becomes inactive instead of waiting until it is fully offline.

Validation on x86_64 included:
- cpus=0/1/2/4 DL bandwidth accounting
- targeted task_non_contending() and inactive_task_timer() zero-CPU paths
- 100 root-domain rebuild/offline-online cycles with accounting checks
- 200 cross-root-domain SCHED_DEADLINE cpuset moves
- SCHED_FLAG_RECLAIM policy transitions
- concurrent SCHED_DEADLINE/SCHED_OTHER transitions and CPU hotplug
- 100 CPU hotplug/debugfs update cycles
- A/B validation of the inactive-but-online debugfs window: the write was accepted and changed runtime with patch 1 only, but was rejected and left runtime unchanged with both patches

The final series was also built and boot-smoke-tested from its exact HEAD.

Changes since v1:
- Handle cpus == 0 in __dl_sub() and __dl_add(), as suggested by Mikhail.
- Split the generic bandwidth fix from the DL server debugfs check.
- Add the s390x sched_setscheduler() report.
- Extend validation to the other DL bandwidth paths and accounting cases.

Hui Su (2):
sched/deadline: Fix divide-by-zero in DL bandwidth accounting sched/deadline: Reject debugfs dl_server writes for inactive CPUs

 kernel/sched/deadline.c | 6 ++++--
 kernel/sched/debug.c    | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)

base-commit: f259f446f5198d98e13756d2cd531812a0ad3064

Signature · 1 line
2.55.0
author
#1 lore raw

PATCH v2 1/2 sched/deadline: Fix divide-by-zero in DL bandwidth accounting

During CPU hot-unplug, a CPU can be cleared from cpu_active_mask before it is marked offline. This can leave a window where dl_bw_cpus() returns zero for a root domain with no remaining active CPU.

DL bandwidth paths can pass this zero CPU count to __dl_sub() and __dl_add(), which then divide by zero while distributing the bandwidth update to active runqueues.

This is not specific to the DL server debugfs path. Mikhail Zaslonko reported the same failure on s390x through sched_setscheduler(), where task_non_contending() reached __dl_sub() with no active CPU in the root domain. Other DL bandwidth paths use the same helpers.

Keep updating dl_bw::total_bw when the CPU count is zero, but skip __dl_update(), since there are no active runqueues to receive an extra_bw adjustment. This avoids the division by zero in all callers while retaining the existing total_bw update.

Fixes daec57983670 ("sched/deadline: Reclaim bandwidth not used by dl tasks")
Assisted-by LLM
---
 kernel/sched/deadline.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/sched/deadline.c +4 -2
index 0663c00c41c0..840240cff7b9 100644
@@ -193,14 +193,16 @@ static inline
 void __dl_sub(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
 {
 	dl_b->total_bw -= tsk_bw;
-	__dl_update(dl_b, (s32)tsk_bw / cpus);
+	if (cpus)
+		__dl_update(dl_b, (s32)tsk_bw / cpus);
 }
 
 static inline
 void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
 {
 	dl_b->total_bw += tsk_bw;
-	__dl_update(dl_b, -((s32)tsk_bw / cpus));
+	if (cpus)
+		__dl_update(dl_b, -((s32)tsk_bw / cpus));
 }
 
 static inline bool
Signature · 1 line
2.55.0
maintainer Acked-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] sched/deadline: Fix divide-by-zero in DL bandwidth accounting

Hello,

Quoted 18 lines · Hui Su

During CPU hot-unplug, a CPU can be cleared from cpu_active_mask before it is marked offline. This can leave a window where dl_bw_cpus() returns zero for a root domain with no remaining active CPU.

DL bandwidth paths can pass this zero CPU count to __dl_sub() and __dl_add(), which then divide by zero while distributing the bandwidth update to active runqueues.

This is not specific to the DL server debugfs path. Mikhail Zaslonko reported the same failure on s390x through sched_setscheduler(), where task_non_contending() reached __dl_sub() with no active CPU in the root domain. Other DL bandwidth paths use the same helpers.

Keep updating dl_bw::total_bw when the CPU count is zero, but skip __dl_update(), since there are no active runqueues to receive an extra_bw adjustment. This avoids the division by zero in all callers while retaining the existing total_bw update.

Quoted 8 lines
Fixes daec57983670 ("sched/deadline: Reclaim bandwidth not used by dl tasks")
Assisted-by LLM

---

Looks good to me.

Thanks,
Juri

author
#3 lore raw

PATCH v2 2/2 sched/deadline: Reject debugfs dl_server writes for inactive CPUs

In reply to Hui Su

Commit 4043f5498416 ("sched/deadline: Reject debugfs dl_server writes for offline CPUs") rejects per-CPU DL server parameter updates once the target CPU is offline.

During CPU hot-unplug, however, the CPU is cleared from cpu_active_mask before it is marked offline. This leaves a window where the CPU is no longer active but cpu_online() still returns true, so sched_server_write_common() continues to accept DL server updates.

There is no meaningful reason to reconfigure a per-CPU DL server once the CPU is being deactivated. Reject the write when the target CPU is inactive rather than only after it becomes offline.

Fixes d741f297bcea ("sched/fair: Fair server interface")
Assisted-by LLM
---
 kernel/sched/debug.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/debug.c +1 -1
index 72236db67983..0e1642adb58c 100644
@@ -416,7 +416,7 @@ static ssize_t sched_server_write_common(struct file *filp, const char __user *u
 			return  -EINVAL;
 		}
 
-		if (!cpu_online(cpu_of(rq)))
+		if (!cpu_active(cpu_of(rq)))
 			return -EBUSY;
 
 		update_rq_clock(rq);
Signature · 1 line
2.55.0
maintainer Acked-by on v2 2/2
#4 lore raw

Re: [PATCH v2 2/2] sched/deadline: Reject debugfs dl_server writes for inactive CPUs

Hello,

Quoted 9 lines · Hui Su

Commit 4043f5498416 ("sched/deadline: Reject debugfs dl_server writes for offline CPUs") rejects per-CPU DL server parameter updates once the target CPU is offline.

During CPU hot-unplug, however, the CPU is cleared from cpu_active_mask before it is marked offline. This leaves a window where the CPU is no longer active but cpu_online() still returns true, so sched_server_write_common() continues to accept DL server updates.

Quoted 5 lines

There is no meaningful reason to reconfigure a per-CPU DL server once the CPU is being deactivated. Reject the write when the target CPU is inactive rather than only after it becomes offline.

Fixes d741f297bcea ("sched/fair: Fair server interface")

Isn't 4043f5498416 ("sched/deadline: Reject debugfs dl_server writes for offline CPUs") more appropriate?

Quoted 6 lines

---

Apart from that the fix looks good to me.

Thanks,
Juri