Skip to contentSkip to conversation

Defer throttle when task exits to user

PATCH Applied Aaron Lu 7 participants 44 messages Aug 29, 2025 – Dec 2, 2025 Scheduler

lore
Share & tools
lore

Aaron Lu

author
#0 lore raw

v4 cover Defer throttle when task exits to user

v4:
- Add cfs_bandwidth_used() in task_is_throttled() and remove unlikely for task_is_throttled(), suggested by Valetin Schneider;
- Add a warn for non empty throttle_node in enqueue_throttled_task(), suggested by Valetin Schneider;
- Improve comments in enqueue_throttled_task() by Valetin Schneider;
- Clear throttled for to-be-unthrottled tasks in tg_unthrottle_up();
- Change throttled and pelt_clock_throttled fields in cfs_rq from int to bool, reported by LKP;
- Improve changelog for patch4 by Valetin Schneider.

Thanks a lot for all the reviews and tests, I hope I didn't miss any of them but if I do, please let me know. I've also run Jan's rt reproducer and songtang's stress test and didn't notice any problem.

Apply on top of sched/core, head commit 1b5f1454091e("sched/idle: Remove play_idle()").

v3:
- Keep throttled cfs_rq's PELT clock run as long as it still has entity queued, suggested by Benjamin Segall; I've folded this change into patch3;
- Rebased on top of tip/sched/core, commit 2885daf47081 ("lib/smp_processor_id: Make migration check unconditional of SMP").

Hi Prateek,
I've kept your tested-by tag(Thanks!) for v2 since I believe this pelt clock change should not affect things much, but let me know if you don't think that is appropriate.

Tests I've done:
- Jan's rt deadlock reproducer[1]. Without this series, I saw rcu-stalls within 2 minutes and with this series, I do not see rcu-stalls after 10 minutes.
- A stress test that creates a lot of pressure on fork/exit path and cgroup_threadgroup_rwsem. Without this series, the test will cause task hung in about 5 minutes and with this series, no problem found after several hours. Songtang wrote this test script and I've used it to verify the patches, thanks Songtang.

[1]: https://lore.kernel.org/all/[email protected]/

Below is previous changelogs:

v2:
- Re-org the patchset to use a single patch to implement throttle related changes, suggested by Chengming;
- Use check_cfs_rq_runtime()'s return value in pick_task_fair() to decide if throttle task work is needed instead of checking throttled_hierarchy(), suggested by Peter;
- Simplify throttle_count check in tg_throtthe_down() and tg_unthrottle_up(), suggested by Peter;
- Add enqueue_throttled_task() to speed up enqueuing a throttled task to a throttled cfs_rq, suggested by Peter;
- Address the missing of detach_task_cfs_rq() for throttled tasks that get migrated to a new rq, pointed out by Chengming;
- Remove cond_resched_tasks_rcu_qs() in throttle_cfs_rq_work() as cond_resched*() is going away, pointed out by Peter.
I hope I didn't miss any comments and suggestions for v1 and if I do, please kindly let me know, thanks!

Base: tip/sched/core commit dabe1be4e84c("sched/smp: Use the SMP version of double_rq_clock_clear_update()")

cover letter of v1:

This is a continuous work based on Valentin Schneider's posting here:
Subject: [RFC PATCH v3 00/10] sched/fair: Defer CFS throttle to user entry
https://lore.kernel.org/lkml/[email protected]/

Valentin has described the problem very well in the above link and I quote:
"
CFS tasks can end up throttled while holding locks that other, non-throttled tasks are blocking on.

For !PREEMPT_RT, this can be a source of latency due to the throttling causing a resource acquisition denial.

For PREEMPT_RT, this is worse and can lead to a deadlock:
o A CFS task p0 gets throttled while holding read_lock(&lock)
o A task p1 blocks on write_lock(&lock), making further readers enter the slowpath
o A ktimers or ksoftirqd task blocks on read_lock(&lock)

If the cfs_bandwidth.period_timer to replenish p0's runtime is enqueued on the same CPU as one where ktimers/ksoftirqd is blocked on read_lock(&lock), this creates a circular dependency.

This has been observed to happen with:
o fs/eventpoll.c::ep->lock
o net/netlink/af_netlink.c::nl_table_lock (after hand-fixing the above) but can trigger with any rwlock that can be acquired in both process and softirq contexts.

The linux-rt tree has had
1ea50f9636f0 ("softirq: Use a dedicated thread for timer wakeups.")
which helped this scenario for non-rwlock locks by ensuring the throttled task would get PI'd to FIFO1 (ktimers' default priority). Unfortunately, rwlocks cannot sanely do PI as they allow multiple readers.
"

Jan Kiszka has posted an reproducer regarding this PREEMPT_RT problem :
https://lore.kernel.org/r/[email protected]/
and K Prateek Nayak has an detailed analysis of how deadlock happened:
https://lore.kernel.org/r/[email protected]/

To fix this issue for PREEMPT_RT and improve latency situation for !PREEMPT_RT, change the throttle model to task based, i.e. when a cfs_rq is throttled, mark its throttled status but do not remove it from cpu's rq. Instead, for tasks that belong to this cfs_rq, when they get picked, add a task work to them so that when they return to user, they can be dequeued. In this way, tasks throttled will not hold any kernel resources. When cfs_rq gets unthrottled, enqueue back those throttled tasks.

There are consequences because of this new throttle model, e.g. for a cfs_rq that has 3 tasks attached, when 2 tasks are throttled on their return2user path, one task still running in kernel mode, this cfs_rq is in a partial throttled state:
- Should its pelt clock be frozen?
- Should this state be accounted into throttled_time?

For pelt clock, I chose to keep the current behavior to freeze it on cfs_rq's throttle time. The assumption is that tasks running in kernel mode should not last too long, freezing the cfs_rq's pelt clock can keep its load and its corresponding sched_entity's weight. Hopefully, this can result in a stable situation for the remaining running tasks to quickly finish their jobs in kernel mode.

For throttle time accounting, according to RFC v2's feedback, rework throttle time accounting for a cfs_rq as follows:
- start accounting when the first task gets throttled in its hierarchy;
- stop accounting on unthrottle.

There is also the concern of increased duration of (un)throttle operations in RFC v1. I've done some tests and with a 2000 cgroups/20K runnable tasks setup on a 2sockets/384cpus AMD server, the longest duration of distribute_cfs_runtime() is in the 2ms-4ms range. For details, please see:
https://lore.kernel.org/lkml/20250324085822.GA732629@bytedance/
For throttle path, with Chengming's suggestion to move "task work setup" from throttle time to pick time, it's not an issue anymore.

Aaron Lu (2):
sched/fair: Task based throttle time accounting
sched/fair: Get rid of throttled_lb_pair()

Valentin Schneider (3):
sched/fair: Add related data structure for task based throttle sched/fair: Implement throttle task work and related helpers sched/fair: Switch to task based throttle model

 include/linux/sched.h |   5 +
 kernel/sched/core.c   |   3 +
 kernel/sched/fair.c   | 458 ++++++++++++++++++++++++------------------
 kernel/sched/pelt.h   |   4 +-
 kernel/sched/sched.h  |   7 +-
 5 files changed, 280 insertions(+), 197 deletions(-)

base-commit: 1b5f1454091e9e9fb5c944b3161acf4ec0894d0d

Signature · 1 line
2.39.5
author
#1 lore raw

PATCH v4 1/5 sched/fair: Add related data structure for task based throttle

From: Valentin Schneider <[email protected]>

Add related data structures for this new throttle functionality.

---
 include/linux/sched.h |  5 +++++
 kernel/sched/core.c   |  3 +++
 kernel/sched/fair.c   | 13 +++++++++++++
 kernel/sched/sched.h  |  3 +++
 4 files changed, 24 insertions(+)
include/linux/sched.h +5 -0
index 5b4e1cd52e27a..e0cc1d6df8122 100644
@@ -880,6 +880,11 @@ struct task_struct {
 
 #ifdef CONFIG_CGROUP_SCHED
 	struct task_group		*sched_task_group;
+#ifdef CONFIG_CFS_BANDWIDTH
+	struct callback_head		sched_throttle_work;
+	struct list_head		throttle_node;
+	bool				throttled;
+#endif
 #endif
 
 
kernel/sched/core.c +3 -0
index f7f576ad9b223..3f7925b216911 100644
@@ -4484,6 +4484,9 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
 
 #ifdef CONFIG_FAIR_GROUP_SCHED
 	p->se.cfs_rq			= NULL;
+#ifdef CONFIG_CFS_BANDWIDTH
+	init_cfs_throttle_work(p);
+#endif
 #endif
 
 #ifdef CONFIG_SCHEDSTATS
kernel/sched/fair.c +13 -0
index b173a059315c2..8fff40fcbc425 100644
@@ -5748,6 +5748,18 @@ static inline int throttled_lb_pair(struct task_group *tg,
 	       throttled_hierarchy(dest_cfs_rq);
 }
 
+static void throttle_cfs_rq_work(struct callback_head *work)
+{
+}
+
+void init_cfs_throttle_work(struct task_struct *p)
+{
+	init_task_work(&p->sched_throttle_work, throttle_cfs_rq_work);
+	/* Protect against double add, see throttle_cfs_rq() and throttle_cfs_rq_work() */
+	p->sched_throttle_work.next = &p->sched_throttle_work;
+	INIT_LIST_HEAD(&p->throttle_node);
+}
+
 static int tg_unthrottle_up(struct task_group *tg, void *data)
 {
 	struct rq *rq = data;
@@ -6472,6 +6484,7 @@ static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
 	cfs_rq->runtime_enabled = 0;
 	INIT_LIST_HEAD(&cfs_rq->throttled_list);
 	INIT_LIST_HEAD(&cfs_rq->throttled_csd_list);
+	INIT_LIST_HEAD(&cfs_rq->throttled_limbo_list);
 }
 
 void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
kernel/sched/sched.h +3 -0
index d3f33d10c58c9..06cc7722226b4 100644
@@ -742,6 +742,7 @@ struct cfs_rq {
 	int			throttle_count;
 	struct list_head	throttled_list;
 	struct list_head	throttled_csd_list;
+	struct list_head        throttled_limbo_list;
 #endif /* CONFIG_CFS_BANDWIDTH */
 #endif /* CONFIG_FAIR_GROUP_SCHED */
 };
@@ -2661,6 +2662,8 @@ extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
 
 extern void init_dl_entity(struct sched_dl_entity *dl_se);
 
+extern void init_cfs_throttle_work(struct task_struct *p);
+
 #define BW_SHIFT		20
 #define BW_UNIT			(1 << BW_SHIFT)
 #define RATIO_SHIFT		8
Signature · 1 line
2.39.5
#2 lore raw

[tip: sched/core] sched/fair: Add related data structure for task based throttle

The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     2cd571245b43492867bf1b4252485f3e6647b643
Gitweb:        https://git.kernel.org/tip/2cd571245b43492867bf1b4252485f3e6647b643
Author:        Valentin Schneider <[email protected]>
AuthorDate:    Fri, 29 Aug 2025 16:11:16 +08:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 03 Sep 2025 10:03:13 +02:00

sched/fair: Add related data structure for task based throttle

Add related data structures for this new throttle functionality.

---
 include/linux/sched.h |  5 +++++
 kernel/sched/core.c   |  3 +++
 kernel/sched/fair.c   | 13 +++++++++++++
 kernel/sched/sched.h  |  3 +++
 4 files changed, 24 insertions(+)
include/linux/sched.h +5 -0
index f8188b8..644a01b 100644
@@ -883,6 +883,11 @@ struct task_struct {
 
 #ifdef CONFIG_CGROUP_SCHED
 	struct task_group		*sched_task_group;
+#ifdef CONFIG_CFS_BANDWIDTH
+	struct callback_head		sched_throttle_work;
+	struct list_head		throttle_node;
+	bool				throttled;
+#endif
 #endif
 
 
kernel/sched/core.c +3 -0
index be00629..feb750a 100644
@@ -4490,6 +4490,9 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
 
 #ifdef CONFIG_FAIR_GROUP_SCHED
 	p->se.cfs_rq			= NULL;
+#ifdef CONFIG_CFS_BANDWIDTH
+	init_cfs_throttle_work(p);
+#endif
 #endif
 
 #ifdef CONFIG_SCHEDSTATS
kernel/sched/fair.c +13 -0
index b173a05..8fff40f 100644
@@ -5748,6 +5748,18 @@ static inline int throttled_lb_pair(struct task_group *tg,
 	       throttled_hierarchy(dest_cfs_rq);
 }
 
+static void throttle_cfs_rq_work(struct callback_head *work)
+{
+}
+
+void init_cfs_throttle_work(struct task_struct *p)
+{
+	init_task_work(&p->sched_throttle_work, throttle_cfs_rq_work);
+	/* Protect against double add, see throttle_cfs_rq() and throttle_cfs_rq_work() */
+	p->sched_throttle_work.next = &p->sched_throttle_work;
+	INIT_LIST_HEAD(&p->throttle_node);
+}
+
 static int tg_unthrottle_up(struct task_group *tg, void *data)
 {
 	struct rq *rq = data;
@@ -6472,6 +6484,7 @@ static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
 	cfs_rq->runtime_enabled = 0;
 	INIT_LIST_HEAD(&cfs_rq->throttled_list);
 	INIT_LIST_HEAD(&cfs_rq->throttled_csd_list);
+	INIT_LIST_HEAD(&cfs_rq->throttled_limbo_list);
 }
 
 void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
kernel/sched/sched.h +3 -0
index be9745d..a6493d2 100644
@@ -739,6 +739,7 @@ struct cfs_rq {
 	int			throttle_count;
 	struct list_head	throttled_list;
 	struct list_head	throttled_csd_list;
+	struct list_head        throttled_limbo_list;
 #endif /* CONFIG_CFS_BANDWIDTH */
 #endif /* CONFIG_FAIR_GROUP_SCHED */
 };
@@ -2658,6 +2659,8 @@ extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
 
 extern void init_dl_entity(struct sched_dl_entity *dl_se);
 
+extern void init_cfs_throttle_work(struct task_struct *p);
+
 #define BW_SHIFT		20
 #define BW_UNIT			(1 << BW_SHIFT)
 #define RATIO_SHIFT		8
author
#3 lore raw

PATCH v4 2/5 sched/fair: Implement throttle task work and related helpers

In reply to Aaron Lu

From: Valentin Schneider <[email protected]>

Implement throttle_cfs_rq_work() task work which gets executed on task's ret2user path where the task is dequeued and marked as throttled.

---
 kernel/sched/fair.c | 65 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
kernel/sched/fair.c +65 -0
index 8fff40fcbc425..dab4ed86d0c82 100644
@@ -5748,8 +5748,51 @@ static inline int throttled_lb_pair(struct task_group *tg,
 	       throttled_hierarchy(dest_cfs_rq);
 }
 
+static inline bool task_is_throttled(struct task_struct *p)
+{
+	return cfs_bandwidth_used() && p->throttled;
+}
+
+static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags);
 static void throttle_cfs_rq_work(struct callback_head *work)
 {
+	struct task_struct *p = container_of(work, struct task_struct, sched_throttle_work);
+	struct sched_entity *se;
+	struct cfs_rq *cfs_rq;
+	struct rq *rq;
+
+	WARN_ON_ONCE(p != current);
+	p->sched_throttle_work.next = &p->sched_throttle_work;
+
+	/*
+	 * If task is exiting, then there won't be a return to userspace, so we
+	 * don't have to bother with any of this.
+	 */
+	if ((p->flags & PF_EXITING))
+		return;
+
+	scoped_guard(task_rq_lock, p) {
+		se = &p->se;
+		cfs_rq = cfs_rq_of(se);
+
+		/* Raced, forget */
+		if (p->sched_class != &fair_sched_class)
+			return;
+
+		/*
+		 * If not in limbo, then either replenish has happened or this
+		 * task got migrated out of the throttled cfs_rq, move along.
+		 */
+		if (!cfs_rq->throttle_count)
+			return;
+		rq = scope.rq;
+		update_rq_clock(rq);
+		WARN_ON_ONCE(p->throttled || !list_empty(&p->throttle_node));
+		dequeue_task_fair(rq, p, DEQUEUE_SLEEP | DEQUEUE_SPECIAL);
+		list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list);
+		p->throttled = true;
+		resched_curr(rq);
+	}
 }
 
 void init_cfs_throttle_work(struct task_struct *p)
@@ -5789,6 +5832,26 @@ static int tg_unthrottle_up(struct task_group *tg, void *data)
 	return 0;
 }
 
+static inline bool task_has_throttle_work(struct task_struct *p)
+{
+	return p->sched_throttle_work.next != &p->sched_throttle_work;
+}
+
+static inline void task_throttle_setup_work(struct task_struct *p)
+{
+	if (task_has_throttle_work(p))
+		return;
+
+	/*
+	 * Kthreads and exiting tasks don't return to userspace, so adding the
+	 * work is pointless
+	 */
+	if ((p->flags & (PF_EXITING | PF_KTHREAD)))
+		return;
+
+	task_work_add(p, &p->sched_throttle_work, TWA_RESUME);
+}
+
 static int tg_throttle_down(struct task_group *tg, void *data)
 {
 	struct rq *rq = data;
@@ -6652,6 +6715,8 @@ static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; }
 static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
 static inline void sync_throttle(struct task_group *tg, int cpu) {}
 static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
+static void task_throttle_setup_work(struct task_struct *p) {}
+static bool task_is_throttled(struct task_struct *p) { return false; }
 
 static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 {
Signature · 1 line
2.39.5
#4 lore raw

[tip: sched/core] sched/fair: Implement throttle task work and related helpers

The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     7fc2d14392475e368a2a7be458aba4eecdf2439b
Gitweb:        https://git.kernel.org/tip/7fc2d14392475e368a2a7be458aba4eecdf2439b
Author:        Valentin Schneider <[email protected]>
AuthorDate:    Fri, 29 Aug 2025 16:11:17 +08:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 03 Sep 2025 10:03:13 +02:00

sched/fair: Implement throttle task work and related helpers

Implement throttle_cfs_rq_work() task work which gets executed on task's ret2user path where the task is dequeued and marked as throttled.

---
 kernel/sched/fair.c | 65 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 65 insertions(+)
kernel/sched/fair.c +65 -0
index 8fff40f..dab4ed8 100644
@@ -5748,8 +5748,51 @@ static inline int throttled_lb_pair(struct task_group *tg,
 	       throttled_hierarchy(dest_cfs_rq);
 }
 
+static inline bool task_is_throttled(struct task_struct *p)
+{
+	return cfs_bandwidth_used() && p->throttled;
+}
+
+static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags);
 static void throttle_cfs_rq_work(struct callback_head *work)
 {
+	struct task_struct *p = container_of(work, struct task_struct, sched_throttle_work);
+	struct sched_entity *se;
+	struct cfs_rq *cfs_rq;
+	struct rq *rq;
+
+	WARN_ON_ONCE(p != current);
+	p->sched_throttle_work.next = &p->sched_throttle_work;
+
+	/*
+	 * If task is exiting, then there won't be a return to userspace, so we
+	 * don't have to bother with any of this.
+	 */
+	if ((p->flags & PF_EXITING))
+		return;
+
+	scoped_guard(task_rq_lock, p) {
+		se = &p->se;
+		cfs_rq = cfs_rq_of(se);
+
+		/* Raced, forget */
+		if (p->sched_class != &fair_sched_class)
+			return;
+
+		/*
+		 * If not in limbo, then either replenish has happened or this
+		 * task got migrated out of the throttled cfs_rq, move along.
+		 */
+		if (!cfs_rq->throttle_count)
+			return;
+		rq = scope.rq;
+		update_rq_clock(rq);
+		WARN_ON_ONCE(p->throttled || !list_empty(&p->throttle_node));
+		dequeue_task_fair(rq, p, DEQUEUE_SLEEP | DEQUEUE_SPECIAL);
+		list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list);
+		p->throttled = true;
+		resched_curr(rq);
+	}
 }
 
 void init_cfs_throttle_work(struct task_struct *p)
@@ -5789,6 +5832,26 @@ static int tg_unthrottle_up(struct task_group *tg, void *data)
 	return 0;
 }
 
+static inline bool task_has_throttle_work(struct task_struct *p)
+{
+	return p->sched_throttle_work.next != &p->sched_throttle_work;
+}
+
+static inline void task_throttle_setup_work(struct task_struct *p)
+{
+	if (task_has_throttle_work(p))
+		return;
+
+	/*
+	 * Kthreads and exiting tasks don't return to userspace, so adding the
+	 * work is pointless
+	 */
+	if ((p->flags & (PF_EXITING | PF_KTHREAD)))
+		return;
+
+	task_work_add(p, &p->sched_throttle_work, TWA_RESUME);
+}
+
 static int tg_throttle_down(struct task_group *tg, void *data)
 {
 	struct rq *rq = data;
@@ -6652,6 +6715,8 @@ static bool check_cfs_rq_runtime(struct cfs_rq *cfs_rq) { return false; }
 static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
 static inline void sync_throttle(struct task_group *tg, int cpu) {}
 static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
+static void task_throttle_setup_work(struct task_struct *p) {}
+static bool task_is_throttled(struct task_struct *p) { return false; }
 
 static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 {
author
#5 lore raw

PATCH v4 3/5 sched/fair: Switch to task based throttle model

In reply to Aaron Lu

From: Valentin Schneider <[email protected]>

In current throttle model, when a cfs_rq is throttled, its entity will be dequeued from cpu's rq, making tasks attached to it not able to run, thus achiveing the throttle target.

This has a drawback though: assume a task is a reader of percpu_rwsem and is waiting. When it gets woken, it can not run till its task group's next period comes, which can be a relatively long time. Waiting writer will have to wait longer due to this and it also makes further reader build up and eventually trigger task hung.

To improve this situation, change the throttle model to task based, i.e. when a cfs_rq is throttled, record its throttled status but do not remove it from cpu's rq. Instead, for tasks that belong to this cfs_rq, when they get picked, add a task work to them so that when they return to user, they can be dequeued there. In this way, tasks throttled will not hold any kernel resources. And on unthrottle, enqueue back those tasks so they can continue to run.

Throttled cfs_rq's PELT clock is handled differently now: previously the cfs_rq's PELT clock is stopped once it entered throttled state but since now tasks(in kernel mode) can continue to run, change the behaviour to stop PELT clock when the throttled cfs_rq has no tasks left.

---
 kernel/sched/fair.c  | 341 ++++++++++++++++++++++---------------------
 kernel/sched/pelt.h  |   4 +-
 kernel/sched/sched.h |   3 +-
 3 files changed, 181 insertions(+), 167 deletions(-)
kernel/sched/fair.c +177 -164
index dab4ed86d0c82..25b1014d4ef86 100644
@@ -5291,18 +5291,23 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 
 	if (cfs_rq->nr_queued == 1) {
 		check_enqueue_throttle(cfs_rq);
-		if (!throttled_hierarchy(cfs_rq)) {
-			list_add_leaf_cfs_rq(cfs_rq);
-		} else {
+		list_add_leaf_cfs_rq(cfs_rq);
 #ifdef CONFIG_CFS_BANDWIDTH
+		if (throttled_hierarchy(cfs_rq)) {
 			struct rq *rq = rq_of(cfs_rq);
 
 			if (cfs_rq_throttled(cfs_rq) && !cfs_rq->throttled_clock)
 				cfs_rq->throttled_clock = rq_clock(rq);
 			if (!cfs_rq->throttled_clock_self)
 				cfs_rq->throttled_clock_self = rq_clock(rq);
-#endif
+
+			if (cfs_rq->pelt_clock_throttled) {
+				cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) -
+					cfs_rq->throttled_clock_pelt;
+				cfs_rq->pelt_clock_throttled = 0;
+			}
 		}
+#endif
 	}
 }
 
@@ -5341,8 +5346,6 @@ static void set_delayed(struct sched_entity *se)
 		struct cfs_rq *cfs_rq = cfs_rq_of(se);
 
 		cfs_rq->h_nr_runnable--;
-		if (cfs_rq_throttled(cfs_rq))
-			break;
 	}
 }
 
@@ -5363,8 +5366,6 @@ static void clear_delayed(struct sched_entity *se)
 		struct cfs_rq *cfs_rq = cfs_rq_of(se);
 
 		cfs_rq->h_nr_runnable++;
-		if (cfs_rq_throttled(cfs_rq))
-			break;
 	}
 }
 
@@ -5450,8 +5451,18 @@ dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 	if (flags & DEQUEUE_DELAYED)
 		finish_delayed_dequeue_entity(se);
 
-	if (cfs_rq->nr_queued == 0)
+	if (cfs_rq->nr_queued == 0) {
 		update_idle_cfs_rq_clock_pelt(cfs_rq);
+#ifdef CONFIG_CFS_BANDWIDTH
+		if (throttled_hierarchy(cfs_rq)) {
+			struct rq *rq = rq_of(cfs_rq);
+
+			list_del_leaf_cfs_rq(cfs_rq);
+			cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq);
+			cfs_rq->pelt_clock_throttled = 1;
+		}
+#endif
+	}
 
 	return true;
 }
@@ -5790,6 +5801,10 @@ static void throttle_cfs_rq_work(struct callback_head *work)
 		WARN_ON_ONCE(p->throttled || !list_empty(&p->throttle_node));
 		dequeue_task_fair(rq, p, DEQUEUE_SLEEP | DEQUEUE_SPECIAL);
 		list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list);
+		/*
+		 * Must not set throttled before dequeue or dequeue will
+		 * mistakenly regard this task as an already throttled one.
+		 */
 		p->throttled = true;
 		resched_curr(rq);
 	}
@@ -5803,32 +5818,124 @@ void init_cfs_throttle_work(struct task_struct *p)
 	INIT_LIST_HEAD(&p->throttle_node);
 }
 
+/*
+ * Task is throttled and someone wants to dequeue it again:
+ * it could be sched/core when core needs to do things like
+ * task affinity change, task group change, task sched class
+ * change etc. and in these cases, DEQUEUE_SLEEP is not set;
+ * or the task is blocked after throttled due to freezer etc.
+ * and in these cases, DEQUEUE_SLEEP is set.
+ */
+static void detach_task_cfs_rq(struct task_struct *p);
+static void dequeue_throttled_task(struct task_struct *p, int flags)
+{
+	WARN_ON_ONCE(p->se.on_rq);
+	list_del_init(&p->throttle_node);
+
+	/* task blocked after throttled */
+	if (flags & DEQUEUE_SLEEP) {
+		p->throttled = false;
+		return;
+	}
+
+	/*
+	 * task is migrating off its old cfs_rq, detach
+	 * the task's load from its old cfs_rq.
+	 */
+	if (task_on_rq_migrating(p))
+		detach_task_cfs_rq(p);
+}
+
+static bool enqueue_throttled_task(struct task_struct *p)
+{
+	struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
+
+	/* @p should have gone through dequeue_throttled_task() first */
+	WARN_ON_ONCE(!list_empty(&p->throttle_node));
+
+	/*
+	 * If the throttled task @p is enqueued to a throttled cfs_rq,
+	 * take the fast path by directly putting the task on the
+	 * target cfs_rq's limbo list.
+	 *
+	 * Do not do that when @p is current because the following race can
+	 * cause @p's group_node to be incorectly re-insterted in its rq's
+	 * cfs_tasks list, despite being throttled:
+	 *
+	 *     cpuX                       cpuY
+	 *   p ret2user
+	 *  throttle_cfs_rq_work()  sched_move_task(p)
+	 *  LOCK task_rq_lock
+	 *  dequeue_task_fair(p)
+	 *  UNLOCK task_rq_lock
+	 *                          LOCK task_rq_lock
+	 *                          task_current_donor(p) == true
+	 *                          task_on_rq_queued(p) == true
+	 *                          dequeue_task(p)
+	 *                          put_prev_task(p)
+	 *                          sched_change_group()
+	 *                          enqueue_task(p) -> p's new cfs_rq
+	 *                                             is throttled, go
+	 *                                             fast path and skip
+	 *                                             actual enqueue
+	 *                          set_next_task(p)
+	 *                    list_move(&se->group_node, &rq->cfs_tasks); // bug
+	 *  schedule()
+	 *
+	 * In the above race case, @p current cfs_rq is in the same rq as
+	 * its previous cfs_rq because sched_move_task() only moves a task
+	 * to a different group from the same rq, so we can use its current
+	 * cfs_rq to derive rq and test if the task is current.
+	 */
+	if (throttled_hierarchy(cfs_rq) &&
+	    !task_current_donor(rq_of(cfs_rq), p)) {
+		list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list);
+		return true;
+	}
+
+	/* we can't take the fast path, do an actual enqueue*/
+	p->throttled = false;
+	return false;
+}
+
+static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags);
 static int tg_unthrottle_up(struct task_group *tg, void *data)
 {
 	struct rq *rq = data;
 	struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
+	struct task_struct *p, *tmp;
+
+	if (--cfs_rq->throttle_count)
+		return 0;
 
-	cfs_rq->throttle_count--;
-	if (!cfs_rq->throttle_count) {
+	if (cfs_rq->pelt_clock_throttled) {
 		cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) -
 					     cfs_rq->throttled_clock_pelt;
+		cfs_rq->pelt_clock_throttled = 0;
+	}
 
-		/* Add cfs_rq with load or one or more already running entities to the list */
-		if (!cfs_rq_is_decayed(cfs_rq))
-			list_add_leaf_cfs_rq(cfs_rq);
+	if (cfs_rq->throttled_clock_self) {
+		u64 delta = rq_clock(rq) - cfs_rq->throttled_clock_self;
 
-		if (cfs_rq->throttled_clock_self) {
-			u64 delta = rq_clock(rq) - cfs_rq->throttled_clock_self;
+		cfs_rq->throttled_clock_self = 0;
 
-			cfs_rq->throttled_clock_self = 0;
+		if (WARN_ON_ONCE((s64)delta < 0))
+			delta = 0;
 
-			if (WARN_ON_ONCE((s64)delta < 0))
-				delta = 0;
+		cfs_rq->throttled_clock_self_time += delta;
+	}
 
-			cfs_rq->throttled_clock_self_time += delta;
-		}
+	/* Re-enqueue the tasks that have been throttled at this level. */
+	list_for_each_entry_safe(p, tmp, &cfs_rq->throttled_limbo_list, throttle_node) {
+		list_del_init(&p->throttle_node);
+		p->throttled = false;
+		enqueue_task_fair(rq_of(cfs_rq), p, ENQUEUE_WAKEUP);
 	}
 
+	/* Add cfs_rq with load or one or more already running entities to the list */
+	if (!cfs_rq_is_decayed(cfs_rq))
+		list_add_leaf_cfs_rq(cfs_rq);
+
 	return 0;
 }
 
@@ -5857,17 +5964,25 @@ static int tg_throttle_down(struct task_group *tg, void *data)
 	struct rq *rq = data;
 	struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
 
+	if (cfs_rq->throttle_count++)
+		return 0;
+
+
 	/* group is entering throttled state, stop time */
-	if (!cfs_rq->throttle_count) {
-		cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq);
+	WARN_ON_ONCE(cfs_rq->throttled_clock_self);
+	if (cfs_rq->nr_queued)
+		cfs_rq->throttled_clock_self = rq_clock(rq);
+	else {
+		/*
+		 * For cfs_rqs that still have entities enqueued, PELT clock
+		 * stop happens at dequeue time when all entities are dequeued.
+		 */
 		list_del_leaf_cfs_rq(cfs_rq);
-
-		WARN_ON_ONCE(cfs_rq->throttled_clock_self);
-		if (cfs_rq->nr_queued)
-			cfs_rq->throttled_clock_self = rq_clock(rq);
+		cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq);
+		cfs_rq->pelt_clock_throttled = 1;
 	}
-	cfs_rq->throttle_count++;
 
+	WARN_ON_ONCE(!list_empty(&cfs_rq->throttled_limbo_list));
 	return 0;
 }
 
@@ -5875,8 +5990,7 @@ static bool throttle_cfs_rq(struct cfs_rq *cfs_rq)
 {
 	struct rq *rq = rq_of(cfs_rq);
 	struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
-	struct sched_entity *se;
-	long queued_delta, runnable_delta, idle_delta, dequeue = 1;
+	int dequeue = 1;
 
 	raw_spin_lock(&cfs_b->lock);
 	/* This will start the period timer if necessary */
@@ -5899,68 +6013,11 @@ static bool throttle_cfs_rq(struct cfs_rq *cfs_rq)
 	if (!dequeue)
 		return false;  /* Throttle no longer required. */
 
-	se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
-
 	/* freeze hierarchy runnable averages while throttled */
 	rcu_read_lock();
 	walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
 	rcu_read_unlock();
 
-	queued_delta = cfs_rq->h_nr_queued;
-	runnable_delta = cfs_rq->h_nr_runnable;
-	idle_delta = cfs_rq->h_nr_idle;
-	for_each_sched_entity(se) {
-		struct cfs_rq *qcfs_rq = cfs_rq_of(se);
-		int flags;
-
-		/* throttled entity or throttle-on-deactivate */
-		if (!se->on_rq)
-			goto done;
-
-		/*
-		 * Abuse SPECIAL to avoid delayed dequeue in this instance.
-		 * This avoids teaching dequeue_entities() about throttled
-		 * entities and keeps things relatively simple.
-		 */
-		flags = DEQUEUE_SLEEP | DEQUEUE_SPECIAL;
-		if (se->sched_delayed)
-			flags |= DEQUEUE_DELAYED;
-		dequeue_entity(qcfs_rq, se, flags);
-
-		if (cfs_rq_is_idle(group_cfs_rq(se)))
-			idle_delta = cfs_rq->h_nr_queued;
-
-		qcfs_rq->h_nr_queued -= queued_delta;
-		qcfs_rq->h_nr_runnable -= runnable_delta;
-		qcfs_rq->h_nr_idle -= idle_delta;
-
-		if (qcfs_rq->load.weight) {
-			/* Avoid re-evaluating load for this entity: */
-			se = parent_entity(se);
-			break;
-		}
-	}
-
-	for_each_sched_entity(se) {
-		struct cfs_rq *qcfs_rq = cfs_rq_of(se);
-		/* throttled entity or throttle-on-deactivate */
-		if (!se->on_rq)
-			goto done;
-
-		update_load_avg(qcfs_rq, se, 0);
-		se_update_runnable(se);
-
-		if (cfs_rq_is_idle(group_cfs_rq(se)))
-			idle_delta = cfs_rq->h_nr_queued;
-
-		qcfs_rq->h_nr_queued -= queued_delta;
-		qcfs_rq->h_nr_runnable -= runnable_delta;
-		qcfs_rq->h_nr_idle -= idle_delta;
-	}
-
-	/* At this point se is NULL and we are at root level*/
-	sub_nr_running(rq, queued_delta);
-done:
 	/*
 	 * Note: distribution will already see us throttled via the
 	 * throttled-list.  rq->lock protects completion.
@@ -5976,9 +6033,20 @@ void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
 {
 	struct rq *rq = rq_of(cfs_rq);
 	struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
-	struct sched_entity *se;
-	long queued_delta, runnable_delta, idle_delta;
-	long rq_h_nr_queued = rq->cfs.h_nr_queued;
+	struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)];
+
+	/*
+	 * It's possible we are called with !runtime_remaining due to things
+	 * like user changed quota setting(see tg_set_cfs_bandwidth()) or async
+	 * unthrottled us with a positive runtime_remaining but other still
+	 * running entities consumed those runtime before we reached here.
+	 *
+	 * Anyway, we can't unthrottle this cfs_rq without any runtime remaining
+	 * because any enqueue in tg_unthrottle_up() will immediately trigger a
+	 * throttle, which is not supposed to happen on unthrottle path.
+	 */
+	if (cfs_rq->runtime_enabled && cfs_rq->runtime_remaining <= 0)
+		return;
 
 	se = cfs_rq->tg->se[cpu_of(rq)];
 
@@ -6008,62 +6076,8 @@ void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
 			if (list_add_leaf_cfs_rq(cfs_rq_of(se)))
 				break;
 		}
-		goto unthrottle_throttle;
-	}
-
-	queued_delta = cfs_rq->h_nr_queued;
-	runnable_delta = cfs_rq->h_nr_runnable;
-	idle_delta = cfs_rq->h_nr_idle;
-	for_each_sched_entity(se) {
-		struct cfs_rq *qcfs_rq = cfs_rq_of(se);
-
-		/* Handle any unfinished DELAY_DEQUEUE business first. */
-		if (se->sched_delayed) {
-			int flags = DEQUEUE_SLEEP | DEQUEUE_DELAYED;
-
-			dequeue_entity(qcfs_rq, se, flags);
-		} else if (se->on_rq)
-			break;
-		enqueue_entity(qcfs_rq, se, ENQUEUE_WAKEUP);
-
-		if (cfs_rq_is_idle(group_cfs_rq(se)))
-			idle_delta = cfs_rq->h_nr_queued;
-
-		qcfs_rq->h_nr_queued += queued_delta;
-		qcfs_rq->h_nr_runnable += runnable_delta;
-		qcfs_rq->h_nr_idle += idle_delta;
-
-		/* end evaluation on encountering a throttled cfs_rq */
-		if (cfs_rq_throttled(qcfs_rq))
-			goto unthrottle_throttle;
 	}
 
-	for_each_sched_entity(se) {
-		struct cfs_rq *qcfs_rq = cfs_rq_of(se);
-
-		update_load_avg(qcfs_rq, se, UPDATE_TG);
-		se_update_runnable(se);
-
-		if (cfs_rq_is_idle(group_cfs_rq(se)))
-			idle_delta = cfs_rq->h_nr_queued;
-
-		qcfs_rq->h_nr_queued += queued_delta;
-		qcfs_rq->h_nr_runnable += runnable_delta;
-		qcfs_rq->h_nr_idle += idle_delta;
-
-		/* end evaluation on encountering a throttled cfs_rq */
-		if (cfs_rq_throttled(qcfs_rq))
-			goto unthrottle_throttle;
-	}
-
-	/* Start the fair server if un-throttling resulted in new runnable tasks */
-	if (!rq_h_nr_queued && rq->cfs.h_nr_queued)
-		dl_server_start(&rq->fair_server);
-
-	/* At this point se is NULL and we are at root level*/
-	add_nr_running(rq, queued_delta);
-
-unthrottle_throttle:
 	assert_list_leaf_cfs_rq(rq);
 
 	/* Determine whether we need to wake up potentially idle CPU: */
@@ -6717,6 +6731,8 @@ static inline void sync_throttle(struct task_group *tg, int cpu) {}
 static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
 static void task_throttle_setup_work(struct task_struct *p) {}
 static bool task_is_throttled(struct task_struct *p) { return false; }
+static void dequeue_throttled_task(struct task_struct *p, int flags) {}
+static bool enqueue_throttled_task(struct task_struct *p) { return false; }
 
 static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 {
@@ -6909,6 +6925,9 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 	int rq_h_nr_queued = rq->cfs.h_nr_queued;
 	u64 slice = 0;
 
+	if (task_is_throttled(p) && enqueue_throttled_task(p))
+		return;
+
 	/*
 	 * The code below (indirectly) updates schedutil which looks at
 	 * the cfs_rq utilization to select a frequency.
@@ -6961,10 +6980,6 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 		if (cfs_rq_is_idle(cfs_rq))
 			h_nr_idle = 1;
 
-		/* end evaluation on encountering a throttled cfs_rq */
-		if (cfs_rq_throttled(cfs_rq))
-			goto enqueue_throttle;
-
 		flags = ENQUEUE_WAKEUP;
 	}
 
@@ -6986,10 +7001,6 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 
 		if (cfs_rq_is_idle(cfs_rq))
 			h_nr_idle = 1;
-
-		/* end evaluation on encountering a throttled cfs_rq */
-		if (cfs_rq_throttled(cfs_rq))
-			goto enqueue_throttle;
 	}
 
 	if (!rq_h_nr_queued && rq->cfs.h_nr_queued) {
@@ -7019,7 +7030,6 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 	if (!task_new)
 		check_update_overutilized_status(rq);
 
-enqueue_throttle:
 	assert_list_leaf_cfs_rq(rq);
 
 	hrtick_update(rq);
@@ -7074,10 +7084,6 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
 		if (cfs_rq_is_idle(cfs_rq))
 			h_nr_idle = h_nr_queued;
 
-		/* end evaluation on encountering a throttled cfs_rq */
-		if (cfs_rq_throttled(cfs_rq))
-			return 0;
-
 		/* Don't dequeue parent if it has other entities besides us */
 		if (cfs_rq->load.weight) {
 			slice = cfs_rq_min_slice(cfs_rq);
@@ -7114,10 +7120,6 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
 
 		if (cfs_rq_is_idle(cfs_rq))
 			h_nr_idle = h_nr_queued;
-
-		/* end evaluation on encountering a throttled cfs_rq */
-		if (cfs_rq_throttled(cfs_rq))
-			return 0;
 	}
 
 	sub_nr_running(rq, h_nr_queued);
@@ -7151,6 +7153,11 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
  */
 static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 {
+	if (task_is_throttled(p)) {
+		dequeue_throttled_task(p, flags);
+		return true;
+	}
+
 	if (!p->se.sched_delayed)
 		util_est_dequeue(&rq->cfs, p);
 
@@ -8819,19 +8826,22 @@ static struct task_struct *pick_task_fair(struct rq *rq)
 {
 	struct sched_entity *se;
 	struct cfs_rq *cfs_rq;
+	struct task_struct *p;
+	bool throttled;
 
 again:
 	cfs_rq = &rq->cfs;
 	if (!cfs_rq->nr_queued)
 		return NULL;
 
+	throttled = false;
+
 	do {
 		/* Might not have done put_prev_entity() */
 		if (cfs_rq->curr && cfs_rq->curr->on_rq)
 			update_curr(cfs_rq);
 
-		if (unlikely(check_cfs_rq_runtime(cfs_rq)))
-			goto again;
+		throttled |= check_cfs_rq_runtime(cfs_rq);
 
 		se = pick_next_entity(rq, cfs_rq);
 		if (!se)
@@ -8839,7 +8849,10 @@ static struct task_struct *pick_task_fair(struct rq *rq)
 		cfs_rq = group_cfs_rq(se);
 	} while (cfs_rq);
 
-	return task_of(se);
+	p = task_of(se);
+	if (unlikely(throttled))
+		task_throttle_setup_work(p);
+	return p;
 }
 
 static void __set_next_task_fair(struct rq *rq, struct task_struct *p, bool first);
kernel/sched/pelt.h +2 -2
index 62c3fa543c0f2..f921302dc40fb 100644
@@ -162,7 +162,7 @@ static inline void update_idle_cfs_rq_clock_pelt(struct cfs_rq *cfs_rq)
 {
 	u64 throttled;
 
-	if (unlikely(cfs_rq->throttle_count))
+	if (unlikely(cfs_rq->pelt_clock_throttled))
 		throttled = U64_MAX;
 	else
 		throttled = cfs_rq->throttled_clock_pelt_time;
@@ -173,7 +173,7 @@ static inline void update_idle_cfs_rq_clock_pelt(struct cfs_rq *cfs_rq)
 /* rq->task_clock normalized against any time this cfs_rq has spent throttled */
 static inline u64 cfs_rq_clock_pelt(struct cfs_rq *cfs_rq)
 {
-	if (unlikely(cfs_rq->throttle_count))
+	if (unlikely(cfs_rq->pelt_clock_throttled))
 		return cfs_rq->throttled_clock_pelt - cfs_rq->throttled_clock_pelt_time;
 
 	return rq_clock_pelt(rq_of(cfs_rq)) - cfs_rq->throttled_clock_pelt_time;
kernel/sched/sched.h +2 -1
index 06cc7722226b4..528af3ef8fc1e 100644
@@ -738,7 +738,8 @@ struct cfs_rq {
 	u64			throttled_clock_pelt_time;
 	u64			throttled_clock_self;
 	u64			throttled_clock_self_time;
-	int			throttled;
+	bool			throttled:1;
+	bool			pelt_clock_throttled:1;
 	int			throttle_count;
 	struct list_head	throttled_list;
 	struct list_head	throttled_csd_list;
Signature · 1 line
2.39.5
#6 lore raw

[tip: sched/core] sched/fair: Switch to task based throttle model

The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     e1fad12dcb66b7f35573c52b665830a1538f9886
Gitweb:        https://git.kernel.org/tip/e1fad12dcb66b7f35573c52b665830a1538f9886
Author:        Valentin Schneider <[email protected]>
AuthorDate:    Fri, 29 Aug 2025 16:11:18 +08:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 03 Sep 2025 10:03:14 +02:00

sched/fair: Switch to task based throttle model

In current throttle model, when a cfs_rq is throttled, its entity will be dequeued from cpu's rq, making tasks attached to it not able to run, thus achiveing the throttle target.

This has a drawback though: assume a task is a reader of percpu_rwsem and is waiting. When it gets woken, it can not run till its task group's next period comes, which can be a relatively long time. Waiting writer will have to wait longer due to this and it also makes further reader build up and eventually trigger task hung.

To improve this situation, change the throttle model to task based, i.e. when a cfs_rq is throttled, record its throttled status but do not remove it from cpu's rq. Instead, for tasks that belong to this cfs_rq, when they get picked, add a task work to them so that when they return to user, they can be dequeued there. In this way, tasks throttled will not hold any kernel resources. And on unthrottle, enqueue back those tasks so they can continue to run.

Throttled cfs_rq's PELT clock is handled differently now: previously the cfs_rq's PELT clock is stopped once it entered throttled state but since now tasks(in kernel mode) can continue to run, change the behaviour to stop PELT clock when the throttled cfs_rq has no tasks left.

---
 kernel/sched/fair.c  | 341 +++++++++++++++++++++---------------------
 kernel/sched/pelt.h  |   4 +-
 kernel/sched/sched.h |   3 +-
 3 files changed, 181 insertions(+), 167 deletions(-)
kernel/sched/fair.c +177 -164
index dab4ed8..25b1014 100644
@@ -5291,18 +5291,23 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 
 	if (cfs_rq->nr_queued == 1) {
 		check_enqueue_throttle(cfs_rq);
-		if (!throttled_hierarchy(cfs_rq)) {
-			list_add_leaf_cfs_rq(cfs_rq);
-		} else {
+		list_add_leaf_cfs_rq(cfs_rq);
 #ifdef CONFIG_CFS_BANDWIDTH
+		if (throttled_hierarchy(cfs_rq)) {
 			struct rq *rq = rq_of(cfs_rq);
 
 			if (cfs_rq_throttled(cfs_rq) && !cfs_rq->throttled_clock)
 				cfs_rq->throttled_clock = rq_clock(rq);
 			if (!cfs_rq->throttled_clock_self)
 				cfs_rq->throttled_clock_self = rq_clock(rq);
-#endif
+
+			if (cfs_rq->pelt_clock_throttled) {
+				cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) -
+					cfs_rq->throttled_clock_pelt;
+				cfs_rq->pelt_clock_throttled = 0;
+			}
 		}
+#endif
 	}
 }
 
@@ -5341,8 +5346,6 @@ static void set_delayed(struct sched_entity *se)
 		struct cfs_rq *cfs_rq = cfs_rq_of(se);
 
 		cfs_rq->h_nr_runnable--;
-		if (cfs_rq_throttled(cfs_rq))
-			break;
 	}
 }
 
@@ -5363,8 +5366,6 @@ static void clear_delayed(struct sched_entity *se)
 		struct cfs_rq *cfs_rq = cfs_rq_of(se);
 
 		cfs_rq->h_nr_runnable++;
-		if (cfs_rq_throttled(cfs_rq))
-			break;
 	}
 }
 
@@ -5450,8 +5451,18 @@ dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 	if (flags & DEQUEUE_DELAYED)
 		finish_delayed_dequeue_entity(se);
 
-	if (cfs_rq->nr_queued == 0)
+	if (cfs_rq->nr_queued == 0) {
 		update_idle_cfs_rq_clock_pelt(cfs_rq);
+#ifdef CONFIG_CFS_BANDWIDTH
+		if (throttled_hierarchy(cfs_rq)) {
+			struct rq *rq = rq_of(cfs_rq);
+
+			list_del_leaf_cfs_rq(cfs_rq);
+			cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq);
+			cfs_rq->pelt_clock_throttled = 1;
+		}
+#endif
+	}
 
 	return true;
 }
@@ -5790,6 +5801,10 @@ static void throttle_cfs_rq_work(struct callback_head *work)
 		WARN_ON_ONCE(p->throttled || !list_empty(&p->throttle_node));
 		dequeue_task_fair(rq, p, DEQUEUE_SLEEP | DEQUEUE_SPECIAL);
 		list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list);
+		/*
+		 * Must not set throttled before dequeue or dequeue will
+		 * mistakenly regard this task as an already throttled one.
+		 */
 		p->throttled = true;
 		resched_curr(rq);
 	}
@@ -5803,32 +5818,124 @@ void init_cfs_throttle_work(struct task_struct *p)
 	INIT_LIST_HEAD(&p->throttle_node);
 }
 
+/*
+ * Task is throttled and someone wants to dequeue it again:
+ * it could be sched/core when core needs to do things like
+ * task affinity change, task group change, task sched class
+ * change etc. and in these cases, DEQUEUE_SLEEP is not set;
+ * or the task is blocked after throttled due to freezer etc.
+ * and in these cases, DEQUEUE_SLEEP is set.
+ */
+static void detach_task_cfs_rq(struct task_struct *p);
+static void dequeue_throttled_task(struct task_struct *p, int flags)
+{
+	WARN_ON_ONCE(p->se.on_rq);
+	list_del_init(&p->throttle_node);
+
+	/* task blocked after throttled */
+	if (flags & DEQUEUE_SLEEP) {
+		p->throttled = false;
+		return;
+	}
+
+	/*
+	 * task is migrating off its old cfs_rq, detach
+	 * the task's load from its old cfs_rq.
+	 */
+	if (task_on_rq_migrating(p))
+		detach_task_cfs_rq(p);
+}
+
+static bool enqueue_throttled_task(struct task_struct *p)
+{
+	struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
+
+	/* @p should have gone through dequeue_throttled_task() first */
+	WARN_ON_ONCE(!list_empty(&p->throttle_node));
+
+	/*
+	 * If the throttled task @p is enqueued to a throttled cfs_rq,
+	 * take the fast path by directly putting the task on the
+	 * target cfs_rq's limbo list.
+	 *
+	 * Do not do that when @p is current because the following race can
+	 * cause @p's group_node to be incorectly re-insterted in its rq's
+	 * cfs_tasks list, despite being throttled:
+	 *
+	 *     cpuX                       cpuY
+	 *   p ret2user
+	 *  throttle_cfs_rq_work()  sched_move_task(p)
+	 *  LOCK task_rq_lock
+	 *  dequeue_task_fair(p)
+	 *  UNLOCK task_rq_lock
+	 *                          LOCK task_rq_lock
+	 *                          task_current_donor(p) == true
+	 *                          task_on_rq_queued(p) == true
+	 *                          dequeue_task(p)
+	 *                          put_prev_task(p)
+	 *                          sched_change_group()
+	 *                          enqueue_task(p) -> p's new cfs_rq
+	 *                                             is throttled, go
+	 *                                             fast path and skip
+	 *                                             actual enqueue
+	 *                          set_next_task(p)
+	 *                    list_move(&se->group_node, &rq->cfs_tasks); // bug
+	 *  schedule()
+	 *
+	 * In the above race case, @p current cfs_rq is in the same rq as
+	 * its previous cfs_rq because sched_move_task() only moves a task
+	 * to a different group from the same rq, so we can use its current
+	 * cfs_rq to derive rq and test if the task is current.
+	 */
+	if (throttled_hierarchy(cfs_rq) &&
+	    !task_current_donor(rq_of(cfs_rq), p)) {
+		list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list);
+		return true;
+	}
+
+	/* we can't take the fast path, do an actual enqueue*/
+	p->throttled = false;
+	return false;
+}
+
+static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags);
 static int tg_unthrottle_up(struct task_group *tg, void *data)
 {
 	struct rq *rq = data;
 	struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
+	struct task_struct *p, *tmp;
+
+	if (--cfs_rq->throttle_count)
+		return 0;
 
-	cfs_rq->throttle_count--;
-	if (!cfs_rq->throttle_count) {
+	if (cfs_rq->pelt_clock_throttled) {
 		cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) -
 					     cfs_rq->throttled_clock_pelt;
+		cfs_rq->pelt_clock_throttled = 0;
+	}
 
-		/* Add cfs_rq with load or one or more already running entities to the list */
-		if (!cfs_rq_is_decayed(cfs_rq))
-			list_add_leaf_cfs_rq(cfs_rq);
+	if (cfs_rq->throttled_clock_self) {
+		u64 delta = rq_clock(rq) - cfs_rq->throttled_clock_self;
 
-		if (cfs_rq->throttled_clock_self) {
-			u64 delta = rq_clock(rq) - cfs_rq->throttled_clock_self;
+		cfs_rq->throttled_clock_self = 0;
 
-			cfs_rq->throttled_clock_self = 0;
+		if (WARN_ON_ONCE((s64)delta < 0))
+			delta = 0;
 
-			if (WARN_ON_ONCE((s64)delta < 0))
-				delta = 0;
+		cfs_rq->throttled_clock_self_time += delta;
+	}
 
-			cfs_rq->throttled_clock_self_time += delta;
-		}
+	/* Re-enqueue the tasks that have been throttled at this level. */
+	list_for_each_entry_safe(p, tmp, &cfs_rq->throttled_limbo_list, throttle_node) {
+		list_del_init(&p->throttle_node);
+		p->throttled = false;
+		enqueue_task_fair(rq_of(cfs_rq), p, ENQUEUE_WAKEUP);
 	}
 
+	/* Add cfs_rq with load or one or more already running entities to the list */
+	if (!cfs_rq_is_decayed(cfs_rq))
+		list_add_leaf_cfs_rq(cfs_rq);
+
 	return 0;
 }
 
@@ -5857,17 +5964,25 @@ static int tg_throttle_down(struct task_group *tg, void *data)
 	struct rq *rq = data;
 	struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
 
+	if (cfs_rq->throttle_count++)
+		return 0;
+
+
 	/* group is entering throttled state, stop time */
-	if (!cfs_rq->throttle_count) {
-		cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq);
+	WARN_ON_ONCE(cfs_rq->throttled_clock_self);
+	if (cfs_rq->nr_queued)
+		cfs_rq->throttled_clock_self = rq_clock(rq);
+	else {
+		/*
+		 * For cfs_rqs that still have entities enqueued, PELT clock
+		 * stop happens at dequeue time when all entities are dequeued.
+		 */
 		list_del_leaf_cfs_rq(cfs_rq);
-
-		WARN_ON_ONCE(cfs_rq->throttled_clock_self);
-		if (cfs_rq->nr_queued)
-			cfs_rq->throttled_clock_self = rq_clock(rq);
+		cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq);
+		cfs_rq->pelt_clock_throttled = 1;
 	}
-	cfs_rq->throttle_count++;
 
+	WARN_ON_ONCE(!list_empty(&cfs_rq->throttled_limbo_list));
 	return 0;
 }
 
@@ -5875,8 +5990,7 @@ static bool throttle_cfs_rq(struct cfs_rq *cfs_rq)
 {
 	struct rq *rq = rq_of(cfs_rq);
 	struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
-	struct sched_entity *se;
-	long queued_delta, runnable_delta, idle_delta, dequeue = 1;
+	int dequeue = 1;
 
 	raw_spin_lock(&cfs_b->lock);
 	/* This will start the period timer if necessary */
@@ -5899,68 +6013,11 @@ static bool throttle_cfs_rq(struct cfs_rq *cfs_rq)
 	if (!dequeue)
 		return false;  /* Throttle no longer required. */
 
-	se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
-
 	/* freeze hierarchy runnable averages while throttled */
 	rcu_read_lock();
 	walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
 	rcu_read_unlock();
 
-	queued_delta = cfs_rq->h_nr_queued;
-	runnable_delta = cfs_rq->h_nr_runnable;
-	idle_delta = cfs_rq->h_nr_idle;
-	for_each_sched_entity(se) {
-		struct cfs_rq *qcfs_rq = cfs_rq_of(se);
-		int flags;
-
-		/* throttled entity or throttle-on-deactivate */
-		if (!se->on_rq)
-			goto done;
-
-		/*
-		 * Abuse SPECIAL to avoid delayed dequeue in this instance.
-		 * This avoids teaching dequeue_entities() about throttled
-		 * entities and keeps things relatively simple.
-		 */
-		flags = DEQUEUE_SLEEP | DEQUEUE_SPECIAL;
-		if (se->sched_delayed)
-			flags |= DEQUEUE_DELAYED;
-		dequeue_entity(qcfs_rq, se, flags);
-
-		if (cfs_rq_is_idle(group_cfs_rq(se)))
-			idle_delta = cfs_rq->h_nr_queued;
-
-		qcfs_rq->h_nr_queued -= queued_delta;
-		qcfs_rq->h_nr_runnable -= runnable_delta;
-		qcfs_rq->h_nr_idle -= idle_delta;
-
-		if (qcfs_rq->load.weight) {
-			/* Avoid re-evaluating load for this entity: */
-			se = parent_entity(se);
-			break;
-		}
-	}
-
-	for_each_sched_entity(se) {
-		struct cfs_rq *qcfs_rq = cfs_rq_of(se);
-		/* throttled entity or throttle-on-deactivate */
-		if (!se->on_rq)
-			goto done;
-
-		update_load_avg(qcfs_rq, se, 0);
-		se_update_runnable(se);
-
-		if (cfs_rq_is_idle(group_cfs_rq(se)))
-			idle_delta = cfs_rq->h_nr_queued;
-
-		qcfs_rq->h_nr_queued -= queued_delta;
-		qcfs_rq->h_nr_runnable -= runnable_delta;
-		qcfs_rq->h_nr_idle -= idle_delta;
-	}
-
-	/* At this point se is NULL and we are at root level*/
-	sub_nr_running(rq, queued_delta);
-done:
 	/*
 	 * Note: distribution will already see us throttled via the
 	 * throttled-list.  rq->lock protects completion.
@@ -5976,9 +6033,20 @@ void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
 {
 	struct rq *rq = rq_of(cfs_rq);
 	struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
-	struct sched_entity *se;
-	long queued_delta, runnable_delta, idle_delta;
-	long rq_h_nr_queued = rq->cfs.h_nr_queued;
+	struct sched_entity *se = cfs_rq->tg->se[cpu_of(rq)];
+
+	/*
+	 * It's possible we are called with !runtime_remaining due to things
+	 * like user changed quota setting(see tg_set_cfs_bandwidth()) or async
+	 * unthrottled us with a positive runtime_remaining but other still
+	 * running entities consumed those runtime before we reached here.
+	 *
+	 * Anyway, we can't unthrottle this cfs_rq without any runtime remaining
+	 * because any enqueue in tg_unthrottle_up() will immediately trigger a
+	 * throttle, which is not supposed to happen on unthrottle path.
+	 */
+	if (cfs_rq->runtime_enabled && cfs_rq->runtime_remaining <= 0)
+		return;
 
 	se = cfs_rq->tg->se[cpu_of(rq)];
 
@@ -6008,62 +6076,8 @@ void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
 			if (list_add_leaf_cfs_rq(cfs_rq_of(se)))
 				break;
 		}
-		goto unthrottle_throttle;
-	}
-
-	queued_delta = cfs_rq->h_nr_queued;
-	runnable_delta = cfs_rq->h_nr_runnable;
-	idle_delta = cfs_rq->h_nr_idle;
-	for_each_sched_entity(se) {
-		struct cfs_rq *qcfs_rq = cfs_rq_of(se);
-
-		/* Handle any unfinished DELAY_DEQUEUE business first. */
-		if (se->sched_delayed) {
-			int flags = DEQUEUE_SLEEP | DEQUEUE_DELAYED;
-
-			dequeue_entity(qcfs_rq, se, flags);
-		} else if (se->on_rq)
-			break;
-		enqueue_entity(qcfs_rq, se, ENQUEUE_WAKEUP);
-
-		if (cfs_rq_is_idle(group_cfs_rq(se)))
-			idle_delta = cfs_rq->h_nr_queued;
-
-		qcfs_rq->h_nr_queued += queued_delta;
-		qcfs_rq->h_nr_runnable += runnable_delta;
-		qcfs_rq->h_nr_idle += idle_delta;
-
-		/* end evaluation on encountering a throttled cfs_rq */
-		if (cfs_rq_throttled(qcfs_rq))
-			goto unthrottle_throttle;
 	}
 
-	for_each_sched_entity(se) {
-		struct cfs_rq *qcfs_rq = cfs_rq_of(se);
-
-		update_load_avg(qcfs_rq, se, UPDATE_TG);
-		se_update_runnable(se);
-
-		if (cfs_rq_is_idle(group_cfs_rq(se)))
-			idle_delta = cfs_rq->h_nr_queued;
-
-		qcfs_rq->h_nr_queued += queued_delta;
-		qcfs_rq->h_nr_runnable += runnable_delta;
-		qcfs_rq->h_nr_idle += idle_delta;
-
-		/* end evaluation on encountering a throttled cfs_rq */
-		if (cfs_rq_throttled(qcfs_rq))
-			goto unthrottle_throttle;
-	}
-
-	/* Start the fair server if un-throttling resulted in new runnable tasks */
-	if (!rq_h_nr_queued && rq->cfs.h_nr_queued)
-		dl_server_start(&rq->fair_server);
-
-	/* At this point se is NULL and we are at root level*/
-	add_nr_running(rq, queued_delta);
-
-unthrottle_throttle:
 	assert_list_leaf_cfs_rq(rq);
 
 	/* Determine whether we need to wake up potentially idle CPU: */
@@ -6717,6 +6731,8 @@ static inline void sync_throttle(struct task_group *tg, int cpu) {}
 static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
 static void task_throttle_setup_work(struct task_struct *p) {}
 static bool task_is_throttled(struct task_struct *p) { return false; }
+static void dequeue_throttled_task(struct task_struct *p, int flags) {}
+static bool enqueue_throttled_task(struct task_struct *p) { return false; }
 
 static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 {
@@ -6909,6 +6925,9 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 	int rq_h_nr_queued = rq->cfs.h_nr_queued;
 	u64 slice = 0;
 
+	if (task_is_throttled(p) && enqueue_throttled_task(p))
+		return;
+
 	/*
 	 * The code below (indirectly) updates schedutil which looks at
 	 * the cfs_rq utilization to select a frequency.
@@ -6961,10 +6980,6 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 		if (cfs_rq_is_idle(cfs_rq))
 			h_nr_idle = 1;
 
-		/* end evaluation on encountering a throttled cfs_rq */
-		if (cfs_rq_throttled(cfs_rq))
-			goto enqueue_throttle;
-
 		flags = ENQUEUE_WAKEUP;
 	}
 
@@ -6986,10 +7001,6 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 
 		if (cfs_rq_is_idle(cfs_rq))
 			h_nr_idle = 1;
-
-		/* end evaluation on encountering a throttled cfs_rq */
-		if (cfs_rq_throttled(cfs_rq))
-			goto enqueue_throttle;
 	}
 
 	if (!rq_h_nr_queued && rq->cfs.h_nr_queued) {
@@ -7019,7 +7030,6 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 	if (!task_new)
 		check_update_overutilized_status(rq);
 
-enqueue_throttle:
 	assert_list_leaf_cfs_rq(rq);
 
 	hrtick_update(rq);
@@ -7074,10 +7084,6 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
 		if (cfs_rq_is_idle(cfs_rq))
 			h_nr_idle = h_nr_queued;
 
-		/* end evaluation on encountering a throttled cfs_rq */
-		if (cfs_rq_throttled(cfs_rq))
-			return 0;
-
 		/* Don't dequeue parent if it has other entities besides us */
 		if (cfs_rq->load.weight) {
 			slice = cfs_rq_min_slice(cfs_rq);
@@ -7114,10 +7120,6 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
 
 		if (cfs_rq_is_idle(cfs_rq))
 			h_nr_idle = h_nr_queued;
-
-		/* end evaluation on encountering a throttled cfs_rq */
-		if (cfs_rq_throttled(cfs_rq))
-			return 0;
 	}
 
 	sub_nr_running(rq, h_nr_queued);
@@ -7151,6 +7153,11 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
  */
 static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 {
+	if (task_is_throttled(p)) {
+		dequeue_throttled_task(p, flags);
+		return true;
+	}
+
 	if (!p->se.sched_delayed)
 		util_est_dequeue(&rq->cfs, p);
 
@@ -8819,19 +8826,22 @@ static struct task_struct *pick_task_fair(struct rq *rq)
 {
 	struct sched_entity *se;
 	struct cfs_rq *cfs_rq;
+	struct task_struct *p;
+	bool throttled;
 
 again:
 	cfs_rq = &rq->cfs;
 	if (!cfs_rq->nr_queued)
 		return NULL;
 
+	throttled = false;
+
 	do {
 		/* Might not have done put_prev_entity() */
 		if (cfs_rq->curr && cfs_rq->curr->on_rq)
 			update_curr(cfs_rq);
 
-		if (unlikely(check_cfs_rq_runtime(cfs_rq)))
-			goto again;
+		throttled |= check_cfs_rq_runtime(cfs_rq);
 
 		se = pick_next_entity(rq, cfs_rq);
 		if (!se)
@@ -8839,7 +8849,10 @@ again:
 		cfs_rq = group_cfs_rq(se);
 	} while (cfs_rq);
 
-	return task_of(se);
+	p = task_of(se);
+	if (unlikely(throttled))
+		task_throttle_setup_work(p);
+	return p;
 }
 
 static void __set_next_task_fair(struct rq *rq, struct task_struct *p, bool first);
kernel/sched/pelt.h +2 -2
index 62c3fa5..f921302 100644
@@ -162,7 +162,7 @@ static inline void update_idle_cfs_rq_clock_pelt(struct cfs_rq *cfs_rq)
 {
 	u64 throttled;
 
-	if (unlikely(cfs_rq->throttle_count))
+	if (unlikely(cfs_rq->pelt_clock_throttled))
 		throttled = U64_MAX;
 	else
 		throttled = cfs_rq->throttled_clock_pelt_time;
@@ -173,7 +173,7 @@ static inline void update_idle_cfs_rq_clock_pelt(struct cfs_rq *cfs_rq)
 /* rq->task_clock normalized against any time this cfs_rq has spent throttled */
 static inline u64 cfs_rq_clock_pelt(struct cfs_rq *cfs_rq)
 {
-	if (unlikely(cfs_rq->throttle_count))
+	if (unlikely(cfs_rq->pelt_clock_throttled))
 		return cfs_rq->throttled_clock_pelt - cfs_rq->throttled_clock_pelt_time;
 
 	return rq_clock_pelt(rq_of(cfs_rq)) - cfs_rq->throttled_clock_pelt_time;
kernel/sched/sched.h +2 -1
index a6493d2..6e1b37b 100644
@@ -735,7 +735,8 @@ struct cfs_rq {
 	u64			throttled_clock_pelt_time;
 	u64			throttled_clock_self;
 	u64			throttled_clock_self_time;
-	int			throttled;
+	bool			throttled:1;
+	bool			pelt_clock_throttled:1;
 	int			throttle_count;
 	struct list_head	throttled_list;
 	struct list_head	throttled_csd_list;
maintainer
#7 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

In reply to Aaron Lu
Quoted 127 lines · Aaron Lu
kernel/sched/fair.c +25 -22
index dab4ed86d0c82..25b1014d4ef86 100644
@@ -5291,18 +5291,23 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 
 	if (cfs_rq->nr_queued == 1) {
 		check_enqueue_throttle(cfs_rq);
-		if (!throttled_hierarchy(cfs_rq)) {
-			list_add_leaf_cfs_rq(cfs_rq);
-		} else {
+		list_add_leaf_cfs_rq(cfs_rq);
 #ifdef CONFIG_CFS_BANDWIDTH
+		if (throttled_hierarchy(cfs_rq)) {
 			struct rq *rq = rq_of(cfs_rq);
 
 			if (cfs_rq_throttled(cfs_rq) && !cfs_rq->throttled_clock)
 				cfs_rq->throttled_clock = rq_clock(rq);
 			if (!cfs_rq->throttled_clock_self)
 				cfs_rq->throttled_clock_self = rq_clock(rq);
-#endif
+
+			if (cfs_rq->pelt_clock_throttled) {
+				cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) -
+					cfs_rq->throttled_clock_pelt;
+				cfs_rq->pelt_clock_throttled = 0;
+			}
 		}
+#endif
 	}
 }
 
@@ -5450,8 +5451,18 @@ dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 	if (flags & DEQUEUE_DELAYED)
 		finish_delayed_dequeue_entity(se);
 
-	if (cfs_rq->nr_queued == 0)
+	if (cfs_rq->nr_queued == 0) {
 		update_idle_cfs_rq_clock_pelt(cfs_rq);
+#ifdef CONFIG_CFS_BANDWIDTH
+		if (throttled_hierarchy(cfs_rq)) {
+			struct rq *rq = rq_of(cfs_rq);
+
+			list_del_leaf_cfs_rq(cfs_rq);
+			cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq);
+			cfs_rq->pelt_clock_throttled = 1;
+		}
+#endif
+	}
 
 	return true;
 }
@@ -6717,6 +6731,8 @@ static inline void sync_throttle(struct task_group *tg, int cpu) {}
 static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
 static void task_throttle_setup_work(struct task_struct *p) {}
 static bool task_is_throttled(struct task_struct *p) { return false; }
+static void dequeue_throttled_task(struct task_struct *p, int flags) {}
+static bool enqueue_throttled_task(struct task_struct *p) { return false; }
 
 static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 {
@@ -6909,6 +6925,9 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 	int rq_h_nr_queued = rq->cfs.h_nr_queued;
 	u64 slice = 0;
 
+	if (task_is_throttled(p) && enqueue_throttled_task(p))
+		return;
+
 	/*
 	 * The code below (indirectly) updates schedutil which looks at
 	 * the cfs_rq utilization to select a frequency.
@@ -6961,10 +6980,6 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 		if (cfs_rq_is_idle(cfs_rq))
 			h_nr_idle = 1;
 
-		/* end evaluation on encountering a throttled cfs_rq */
-		if (cfs_rq_throttled(cfs_rq))
-			goto enqueue_throttle;
-
 		flags = ENQUEUE_WAKEUP;
 	}
 
@@ -6986,10 +7001,6 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 
 		if (cfs_rq_is_idle(cfs_rq))
 			h_nr_idle = 1;
-
-		/* end evaluation on encountering a throttled cfs_rq */
-		if (cfs_rq_throttled(cfs_rq))
-			goto enqueue_throttle;
 	}
 
 	if (!rq_h_nr_queued && rq->cfs.h_nr_queued) {
@@ -7019,7 +7030,6 @@ enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 	if (!task_new)
 		check_update_overutilized_status(rq);
 
-enqueue_throttle:
 	assert_list_leaf_cfs_rq(rq);
 
 	hrtick_update(rq);
@@ -7074,10 +7084,6 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
 		if (cfs_rq_is_idle(cfs_rq))
 			h_nr_idle = h_nr_queued;
 
-		/* end evaluation on encountering a throttled cfs_rq */
-		if (cfs_rq_throttled(cfs_rq))
-			return 0;
-
 		/* Don't dequeue parent if it has other entities besides us */
 		if (cfs_rq->load.weight) {
 			slice = cfs_rq_min_slice(cfs_rq);
@@ -7114,10 +7120,6 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
 
 		if (cfs_rq_is_idle(cfs_rq))
 			h_nr_idle = h_nr_queued;
-
-		/* end evaluation on encountering a throttled cfs_rq */
-		if (cfs_rq_throttled(cfs_rq))
-			return 0;
 	}
 
 	sub_nr_running(rq, h_nr_queued);
@@ -7151,6 +7153,11 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
  */

kernel/sched/fair.c static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags) · line 7163

Quoted 10 lines
kernel/sched/fair.c +5 -0
 static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
 {
+	if (task_is_throttled(p)) {
+		dequeue_throttled_task(p, flags);
+		return true;
+	}
+
 	if (!p->se.sched_delayed)
 		util_est_dequeue(&rq->cfs, p);
 

OK, so this makes it so that either a task is fully enqueued (all cfs_rq's) or full not. A group cfs_rq is only marked throttled when all its tasks are gone, and unthrottled when a task gets added. Right?

But propagate_entity_cfs_rq() is still doing the old thing, and has a if (cfs_rq_throttled(cfs_rq)) break; inside the for_each_sched_entity() iteration.

This seems somewhat inconsistent; or am I missing something ?

#8 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

Hello Peter,

Quoted 6 lines · Peter Zijlstra
Quoted 6 lines

static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) {

(quoted diff) +4 -0
+	if (task_is_throttled(p)) {
+		dequeue_throttled_task(p, flags);
+		return true;
+	}
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +1 -0
+
 	if (!p->se.sched_delayed)
 		util_est_dequeue(&rq->cfs, p);

OK, so this makes it so that either a task is fully enqueued (all cfs_rq's) or full not. A group cfs_rq is only marked throttled when all its tasks are gone, and unthrottled when a task gets added. Right?

cfs_rq (and the hierarchy below) is marked throttled when the quota has elapsed. Tasks on the throttled hierarchies will dequeue themselves completely via task work added during pick. When the last task leaves on a cfs_rq of throttled hierarchy, PELT is frozen for that cfs_rq.

When a new task is added on the hierarchy, the PELT is unfrozen and the task becomes runnable. The cfs_rq and the hierarchy is still marked throttled.

Unthrottling of hierarchy is only done at distribution.

Quoted 6 lines

But propagate_entity_cfs_rq() is still doing the old thing, and has a if (cfs_rq_throttled(cfs_rq)) break; inside the for_each_sched_entity() iteration.

This seems somewhat inconsistent; or am I missing something ?

Probably an oversight. But before that, what was the reason to have stopped this propagation at throttled_cfs_rq() before the changes?

Looking at commit 09a43ace1f98 ("sched/fair: Propagate load during synchronous attach/detach") was it because the update_load_avg() from enqueue_entity() loop previously in unthrottle_cfs_rq() would have propagated the PELT to root on unthrottle?

If that was the intention, perhaps something like:

kernel/sched/fair.c +13 -2
index df8dc389af8e..4b32785231bf 100644
@@ -5234,6 +5234,7 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 
 static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
 static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq);
+static inline int cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq);
 
 static void
 requeue_delayed_entity(struct sched_entity *se);
@@ -5729,6 +5730,11 @@ static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 	return cfs_bandwidth_used() && cfs_rq->throttled;
 }
 
+static inline int cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
+{
+	return cfs_bandwidth_used() && cfs_rq->pelt_clock_throttled;
+}
+
 /* check whether cfs_rq, or any parent, is throttled */
 static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
 {
@@ -6721,6 +6727,11 @@ static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 	return 0;
 }
 
+static inline int cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
+{
+	return 0;
+}
+
 static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
 {
 	return 0;
@@ -13151,7 +13162,7 @@ static void propagate_entity_cfs_rq(struct sched_entity *se)
 {
 	struct cfs_rq *cfs_rq = cfs_rq_of(se);
 
-	if (cfs_rq_throttled(cfs_rq))
+	if (cfs_rq_pelt_clock_throttled(cfs_rq))
 		return;
 
 	if (!throttled_hierarchy(cfs_rq))
@@ -13165,7 +13176,7 @@ static void propagate_entity_cfs_rq(struct sched_entity *se)
 
 		update_load_avg(cfs_rq, se, UPDATE_TG);
 
-		if (cfs_rq_throttled(cfs_rq))
+		if (cfs_rq_pelt_clock_throttled(cfs_rq))
 			break;
 
 		if (!throttled_hierarchy(cfs_rq))

---

The PELT will then be propagated to root either via the update_load_avg() call when PELT unfreezes or through the __update_blocked_fair() path after unthrottle. Thoughts?

Signature · 2 lines
Thanks and Regards,
Prateek
maintainer
#9 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

Quoted 18 lines · K Prateek Nayak

Hello Peter,

Quoted 6 lines · Peter Zijlstra
Quoted 6 lines

static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) {

(quoted diff) +4 -0
+	if (task_is_throttled(p)) {
+		dequeue_throttled_task(p, flags);
+		return true;
+	}
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +1 -0
+
 	if (!p->se.sched_delayed)
 		util_est_dequeue(&rq->cfs, p);

OK, so this makes it so that either a task is fully enqueued (all cfs_rq's) or full not. A group cfs_rq is only marked throttled when all its tasks are gone, and unthrottled when a task gets added. Right?

Quoted 5 lines

cfs_rq (and the hierarchy below) is marked throttled when the quota has elapsed. Tasks on the throttled hierarchies will dequeue themselves completely via task work added during pick. When the last task leaves on a cfs_rq of throttled hierarchy, PELT is frozen for that cfs_rq.

Ah, right.

Quoted 16 lines

When a new task is added on the hierarchy, the PELT is unfrozen and the task becomes runnable. The cfs_rq and the hierarchy is still marked throttled.

Unthrottling of hierarchy is only done at distribution.

Quoted 6 lines

But propagate_entity_cfs_rq() is still doing the old thing, and has a if (cfs_rq_throttled(cfs_rq)) break; inside the for_each_sched_entity() iteration.

This seems somewhat inconsistent; or am I missing something ?

Probably an oversight. But before that, what was the reason to have stopped this propagation at throttled_cfs_rq() before the changes?

Quoted 6 lines

Looking at commit 09a43ace1f98 ("sched/fair: Propagate load during synchronous attach/detach") was it because the update_load_avg() from enqueue_entity() loop previously in unthrottle_cfs_rq() would have propagated the PELT to root on unthrottle?

If that was the intention, perhaps something like:

So this is mostly tasks leaving/joining the class/cgroup. And its purpose seems to be to remove/add the blocked load component.

Previously throttle/unthrottle would {de,en}queue the whole subtree from PELT, see how {en,de}queue would also stop at throttle.

But now none of that is done; PELT is fully managed by the tasks {de,en}queueing.

So I'm thinking that when a task joins fair (deboost from RT or whatever), we add the blocking load and fully propagate it. If the task is subject to throttling, that will then happen 'naturally' and it will dequeue itself again.

#10 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

Quoted 6 lines · Peter Zijlstra

So this is mostly tasks leaving/joining the class/cgroup. And its purpose seems to be to remove/add the blocked load component.

Previously throttle/unthrottle would {de,en}queue the whole subtree from PELT, see how {en,de}queue would also stop at throttle.

Quoted 7 lines

But now none of that is done; PELT is fully managed by the tasks {de,en}queueing.

So I'm thinking that when a task joins fair (deboost from RT or whatever), we add the blocking load and fully propagate it. If the task is subject to throttling, that will then happen 'naturally' and it will dequeue itself again.

That seems like the correct thing to do yes. Those throttled_cfs_rq() checks in propagate_entity_cfs_rq() can be removed then.

Signature · 2 lines
Thanks and Regards,
Prateek
author
#11 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

Quoted 10 lines · K Prateek Nayak
Quoted 9 lines · Peter Zijlstra

So this is mostly tasks leaving/joining the class/cgroup. And its purpose seems to be to remove/add the blocked load component.

Previously throttle/unthrottle would {de,en}queue the whole subtree from PELT, see how {en,de}queue would also stop at throttle.

But now none of that is done; PELT is fully managed by the tasks {de,en}queueing.

Quoted 8 lines
Quoted 4 lines

So I'm thinking that when a task joins fair (deboost from RT or whatever), we add the blocking load and fully propagate it. If the task is subject to throttling, that will then happen 'naturally' and it will dequeue itself again.

That seems like the correct thing to do yes. Those throttled_cfs_rq() checks in propagate_entity_cfs_rq() can be removed then.

Not sure if I understand correctly, I've come to the below code according to your discussion:

kernel/sched/fair.c +13 -8
index 3e927b9b7eeb6..97ae561c60f5b 100644
@@ -5234,6 +5234,7 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 
 static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
 static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq);
+static inline int cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq);
 
 static void
 requeue_delayed_entity(struct sched_entity *se);
@@ -5729,6 +5730,11 @@ static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 	return cfs_bandwidth_used() && cfs_rq->throttled;
 }
 
+static inline int cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
+{
+	return cfs_bandwidth_used() && cfs_rq->pelt_clock_throttled;
+}
+
 /* check whether cfs_rq, or any parent, is throttled */
 static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
 {
@@ -6721,6 +6727,11 @@ static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 	return 0;
 }
 
+static inline int cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
+{
+	return 0;
+}
+
 static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
 {
 	return 0;
@@ -13154,10 +13165,7 @@ static void propagate_entity_cfs_rq(struct sched_entity *se)
 {
 	struct cfs_rq *cfs_rq = cfs_rq_of(se);
 
-	if (cfs_rq_throttled(cfs_rq))
-		return;
-
-	if (!throttled_hierarchy(cfs_rq))
+	if (!cfs_rq_pelt_clock_throttled(cfs_rq))
 		list_add_leaf_cfs_rq(cfs_rq);
 
 	/* Start to propagate at parent */
@@ -13168,10 +13176,7 @@ static void propagate_entity_cfs_rq(struct sched_entity *se)
 
 		update_load_avg(cfs_rq, se, UPDATE_TG);
 
-		if (cfs_rq_throttled(cfs_rq))
-			break;
-
-		if (!throttled_hierarchy(cfs_rq))
+		if (!cfs_rq_pelt_clock_throttled(cfs_rq))
 			list_add_leaf_cfs_rq(cfs_rq);
 	}
 }

So this means when a task left/joined a cfs_rq, we will do propagate immediately, no matter if the cfs_rq is throttled or has its pelt clock stopped or not; if cfs_rq still has pelt clock running, it will be added to leaf cfs_rq list to make sure its load can be decayed. If cfs_rq's pelt clock is stopped, it will be added to leaf cfs_rq list if necessary by enqueue_task_fair() or when it's unthrottled.

author
#12 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

Hi Peter,

Quoted 16 lines · Aaron Lu
Quoted 15 lines · K Prateek Nayak
Quoted 13 lines · Peter Zijlstra

So this is mostly tasks leaving/joining the class/cgroup. And its purpose seems to be to remove/add the blocked load component.

Previously throttle/unthrottle would {de,en}queue the whole subtree from PELT, see how {en,de}queue would also stop at throttle.

But now none of that is done; PELT is fully managed by the tasks {de,en}queueing.

So I'm thinking that when a task joins fair (deboost from RT or whatever), we add the blocking load and fully propagate it. If the task is subject to throttling, that will then happen 'naturally' and it will dequeue itself again.

Quoted 7 lines
Quoted 3 lines

That seems like the correct thing to do yes. Those throttled_cfs_rq() checks in propagate_entity_cfs_rq() can be removed then.

Not sure if I understand correctly, I've come to the below code according to your discussion:

Does the below diff look sane to you? If so, shall I send a separate patch on top or fold it in patch3 and send an updated patch3?

Thanks.

Quoted 67 lines
kernel/sched/fair.c +13 -8
index 3e927b9b7eeb6..97ae561c60f5b 100644
@@ -5234,6 +5234,7 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 
 static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
 static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq);
+static inline int cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq);
 
 static void
 requeue_delayed_entity(struct sched_entity *se);
@@ -5729,6 +5730,11 @@ static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 	return cfs_bandwidth_used() && cfs_rq->throttled;
 }
 
+static inline int cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
+{
+	return cfs_bandwidth_used() && cfs_rq->pelt_clock_throttled;
+}
+
 /* check whether cfs_rq, or any parent, is throttled */
 static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
 {
@@ -6721,6 +6727,11 @@ static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 	return 0;
 }
 
+static inline int cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
+{
+	return 0;
+}
+
 static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
 {
 	return 0;
@@ -13154,10 +13165,7 @@ static void propagate_entity_cfs_rq(struct sched_entity *se)
 {
 	struct cfs_rq *cfs_rq = cfs_rq_of(se);
 
-	if (cfs_rq_throttled(cfs_rq))
-		return;
-
-	if (!throttled_hierarchy(cfs_rq))
+	if (!cfs_rq_pelt_clock_throttled(cfs_rq))
 		list_add_leaf_cfs_rq(cfs_rq);
 
 	/* Start to propagate at parent */
@@ -13168,10 +13176,7 @@ static void propagate_entity_cfs_rq(struct sched_entity *se)
 
 		update_load_avg(cfs_rq, se, UPDATE_TG);
 
-		if (cfs_rq_throttled(cfs_rq))
-			break;
-
-		if (!throttled_hierarchy(cfs_rq))
+		if (!cfs_rq_pelt_clock_throttled(cfs_rq))
 			list_add_leaf_cfs_rq(cfs_rq);
 	}
 }

So this means when a task left/joined a cfs_rq, we will do propagate immediately, no matter if the cfs_rq is throttled or has its pelt clock stopped or not; if cfs_rq still has pelt clock running, it will be added to leaf cfs_rq list to make sure its load can be decayed. If cfs_rq's pelt clock is stopped, it will be added to leaf cfs_rq list if necessary by enqueue_task_fair() or when it's unthrottled.

maintainer
#13 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

Quoted 23 lines · Aaron Lu

Hi Peter,

Quoted 20 lines · Aaron Lu
Quoted 18 lines · K Prateek Nayak
Quoted 6 lines · Peter Zijlstra

So this is mostly tasks leaving/joining the class/cgroup. And its purpose seems to be to remove/add the blocked load component.

Previously throttle/unthrottle would {de,en}queue the whole subtree from PELT, see how {en,de}queue would also stop at throttle.

Quoted 7 lines

But now none of that is done; PELT is fully managed by the tasks {de,en}queueing.

So I'm thinking that when a task joins fair (deboost from RT or whatever), we add the blocking load and fully propagate it. If the task is subject to throttling, that will then happen 'naturally' and it will dequeue itself again.

That seems like the correct thing to do yes. Those throttled_cfs_rq() checks in propagate_entity_cfs_rq() can be removed then.

Quoted 6 lines
Quoted 3 lines

Not sure if I understand correctly, I've come to the below code according to your discussion:

Does the below diff look sane to you? If so, shall I send a separate patch on top or fold it in patch3 and send an updated patch3?

Yeah, I suppose that works. Please send a follow up patch. It would also be good to have a comment that explains why we need that list_add_leaf thing. I think I see, but I'm sure I'll have forgotten all next time I see this code.

author
#14 lore raw

PATCH 1/1 sched/fair: Propagate load for throttled cfs_rq

Quoted 28 lines · Peter Zijlstra
Quoted 27 lines · Aaron Lu

Hi Peter,

Quoted 23 lines · Aaron Lu
Quoted 10 lines · K Prateek Nayak
Quoted 9 lines · Peter Zijlstra

So this is mostly tasks leaving/joining the class/cgroup. And its purpose seems to be to remove/add the blocked load component.

Previously throttle/unthrottle would {de,en}queue the whole subtree from PELT, see how {en,de}queue would also stop at throttle.

But now none of that is done; PELT is fully managed by the tasks {de,en}queueing.

Quoted 8 lines
Quoted 4 lines

So I'm thinking that when a task joins fair (deboost from RT or whatever), we add the blocking load and fully propagate it. If the task is subject to throttling, that will then happen 'naturally' and it will dequeue itself again.

That seems like the correct thing to do yes. Those throttled_cfs_rq() checks in propagate_entity_cfs_rq() can be removed then.

Not sure if I understand correctly, I've come to the below code according to your discussion:

Quoted 7 lines
Quoted 2 lines

Does the below diff look sane to you? If so, shall I send a separate patch on top or fold it in patch3 and send an updated patch3?

Yeah, I suppose that works. Please send a follow up patch. It would also be good to have a comment that explains why we need that list_add_leaf thing. I think I see, but I'm sure I'll have forgotten all next time I see this code.

Here it is.

From d88c2d2be31bb3970708f9b78e1725b0e25824be Mon Sep 17 00:00:00 2001
From: Aaron Lu <[email protected]>
Date: Fri, 5 Sep 2025 14:21:50 +0800
Subject: [PATCH] sched/fair: Propagate load for throttled cfs_rq

Before task based throttle model, propagating load will stop at a throttled cfs_rq and that propagate will happen on unthrottle time by update_load_avg().

Now that there is no update_load_avg() on unthrottle for throttled cfs_rq and all load tracking is done by task related operations, let the propagate happen immediately.

While at it, add a comment to explain why cfs_rqs that are not affected by throttle have to be added to leaf cfs_rq list in propagate_entity_cfs_rq() per my understanding of commit 0258bdfaff5b ("sched/fair: Fix unfairness caused by missing load decay").

---
 kernel/sched/fair.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
kernel/sched/fair.c +18 -8
index df8dc389af8e1..03f16f70bff8a 100644
@@ -5729,6 +5729,11 @@ static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 	return cfs_bandwidth_used() && cfs_rq->throttled;
 }
 
+static inline bool cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
+{
+	return cfs_bandwidth_used() && cfs_rq->pelt_clock_throttled;
+}
+
 /* check whether cfs_rq, or any parent, is throttled */
 static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
 {
@@ -6721,6 +6726,11 @@ static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 	return 0;
 }
 
++static inline bool cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
+{
+	return false;
+}
+
 static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
 {
 	return 0;
@@ -13151,10 +13161,13 @@ static void propagate_entity_cfs_rq(struct sched_entity *se)
 {
 	struct cfs_rq *cfs_rq = cfs_rq_of(se);
 
-	if (cfs_rq_throttled(cfs_rq))
-		return;
-
-	if (!throttled_hierarchy(cfs_rq))
+	/*
+	 * If a task gets attached to this cfs_rq and before being queued,
+	 * it gets migrated to another CPU due to reasons like cpuset change,
+	 * we need to make sure this cfs_rq stays on leaf cfs_rq list to
+	 * have that removed load decayed or it can cause faireness problem.
+	 */
+	if(!cfs_rq_pelt_clock_throttled(cfs_rq))
 		list_add_leaf_cfs_rq(cfs_rq);
 
 	/* Start to propagate at parent */
@@ -13165,10 +13178,7 @@ static void propagate_entity_cfs_rq(struct sched_entity *se)
 
 		update_load_avg(cfs_rq, se, UPDATE_TG);
 
-		if (cfs_rq_throttled(cfs_rq))
-			break;
-
-		if (!throttled_hierarchy(cfs_rq))
+		if (!cfs_rq_pelt_clock_throttled(cfs_rq))
 			list_add_leaf_cfs_rq(cfs_rq);
 	}
 }
Signature · 1 line
2.39.5
#15 lore raw

Re: [PATCH] sched/fair: Propagate load for throttled cfs_rq

kernel test robot noticed the following build errors:

Hi Aaron,

kernel test robot noticed the following build errors:

url:    https://github.com/intel-lab-lkp/linux/commits/UPDATE-20250908-190724/Aaron-Lu/sched-fair-Add-related-data-structure-for-task-based-throttle/20250829-161501
base:   the 3th patch of https://lore.kernel.org/r/20250829081120.806-4-ziqianlu%40bytedance.com
patch link:    https://lore.kernel.org/r/20250908110548.GA35%40bytedance
patch subject: [PATCH] sched/fair: Propagate load for throttled cfs_rq
config: i386-buildonly-randconfig-001-20250909 (https://download.01.org/0day-ci/archive/20250909/[email protected]/config)
compiler: gcc-13 (Debian 13.3.0-16) 13.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250909/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> kernel/sched/fair.c:6747:1: error: expected identifier or '(' before '+' token

6747 | +static inline bool cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
| ^

vim +6747 kernel/sched/fair.c

6746

> 6747	+static inline bool cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)

6748 {
6749 return false;
6750 }
6751

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author
#16 lore raw

Re: [PATCH] sched/fair: Propagate load for throttled cfs_rq

Quoted 19 lines · kernel test robot

Hi Aaron,

kernel test robot noticed the following build errors:

url:    https://github.com/intel-lab-lkp/linux/commits/UPDATE-20250908-190724/Aaron-Lu/sched-fair-Add-related-data-structure-for-task-based-throttle/20250829-161501
base:   the 3th patch of https://lore.kernel.org/r/20250829081120.806-4-ziqianlu%40bytedance.com
patch link:    https://lore.kernel.org/r/20250908110548.GA35%40bytedance
patch subject: [PATCH] sched/fair: Propagate load for throttled cfs_rq
config: i386-buildonly-randconfig-001-20250909 (https://download.01.org/0day-ci/archive/20250909/[email protected]/config)
compiler: gcc-13 (Debian 13.3.0-16) 13.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250909/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

Quoted 5 lines

All errors (new ones prefixed by >>):

Quoted 1 line
Quoted 1 line

kernel/sched/fair.c:6747:1: error: expected identifier or '(' before '+' token

6747 | +static inline bool cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
| ^

Sigh, I remembered I did a build test with !CFS_BANDWIDTH and now I went to check that build directory and noticed I didn't have CFS_BANDWIDTH disabled...

Sorry for the trouble, will send an updated patch later.

And thanks for the report, lkp.

Quoted 14 lines

vim +6747 kernel/sched/fair.c

6746

Quoted 1 line

6747 +static inline bool cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)

6748 {
6749 return false;
6750 }
6751

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#17 lore raw

Re: [PATCH] sched/fair: Propagate load for throttled cfs_rq

Hello Aaron,

Quoted 9 lines · Aaron Lu
Quoted 3 lines
Quoted 1 line
Quoted 1 line

kernel/sched/fair.c:6747:1: error: expected identifier or '(' before '+' token

6747 | +static inline bool cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
| ^

Sigh, I remembered I did a build test with !CFS_BANDWIDTH and now I went to check that build directory and noticed I didn't have CFS_BANDWIDTH disabled...

Sorry for the trouble, will send an updated patch later.

While at it, another nit.

Quoted 5 lines · Aaron Lu
kernel/sched/fair.c +0 -1
@@ -13151,10 +13161,13 @@ static void propagate_entity_cfs_rq(struct sched_entity *se)
 {
 	struct cfs_rq *cfs_rq = cfs_rq_of(se);
 
-	if (cfs_rq_throttled(cfs_rq))

kernel/sched/fair.c static void propagate_entity_cfs_rq(struct sched_entity *se) · line 13170

Quoted 10 lines
kernel/sched/fair.c +7 -3
-		return;
-
-	if (!throttled_hierarchy(cfs_rq))
+	/*
+	 * If a task gets attached to this cfs_rq and before being queued,
+	 * it gets migrated to another CPU due to reasons like cpuset change,
+	 * we need to make sure this cfs_rq stays on leaf cfs_rq list to
+	 * have that removed load decayed or it can cause faireness problem.
+	 */
+	if(!cfs_rq_pelt_clock_throttled(cfs_rq))

^ Can you also add a space after the "if" here.

Quoted 1 line
(quoted diff) +0 -0
 		list_add_leaf_cfs_rq(cfs_rq);
Signature · 2 lines
Thanks and Regards,
Prateek
author
#18 lore raw

Re: [PATCH] sched/fair: Propagate load for throttled cfs_rq

Quoted 20 lines · K Prateek Nayak

Hello Aaron,

Quoted 9 lines · Aaron Lu
Quoted 3 lines
Quoted 1 line
Quoted 1 line

kernel/sched/fair.c:6747:1: error: expected identifier or '(' before '+' token

6747 | +static inline bool cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
| ^

Sigh, I remembered I did a build test with !CFS_BANDWIDTH and now I went to check that build directory and noticed I didn't have CFS_BANDWIDTH disabled...

Sorry for the trouble, will send an updated patch later.

While at it, another nit.

Quoted 4 lines · Aaron Lu
kernel/sched/fair.c +0 -0
@@ -13151,10 +13161,13 @@ static void propagate_entity_cfs_rq(struct sched_entity *se)
 {
 	struct cfs_rq *cfs_rq = cfs_rq_of(se);
 
Quoted 14 lines

kernel/sched/fair.c static void propagate_entity_cfs_rq(struct sched_entity *se) · line 13170

Quoted 11 lines
kernel/sched/fair.c +7 -4
-	if (cfs_rq_throttled(cfs_rq))
-		return;
-
-	if (!throttled_hierarchy(cfs_rq))
+	/*
+	 * If a task gets attached to this cfs_rq and before being queued,
+	 * it gets migrated to another CPU due to reasons like cpuset change,
+	 * we need to make sure this cfs_rq stays on leaf cfs_rq list to
+	 * have that removed load decayed or it can cause faireness problem.
+	 */
+	if(!cfs_rq_pelt_clock_throttled(cfs_rq))

^ Can you also add a space after the "if" here.

Yeah, I definitely should do that, thanks for catching this.

Quoted 2 lines
Quoted 1 line
(quoted diff) +0 -0
 		list_add_leaf_cfs_rq(cfs_rq);
author
#19 lore raw

Re: [PATCH] sched/fair: Propagate load for throttled cfs_rq

Quoted 36 lines · Aaron Lu
Quoted 20 lines · K Prateek Nayak

Hello Aaron,

Quoted 9 lines · Aaron Lu
Quoted 3 lines
Quoted 1 line
Quoted 1 line

kernel/sched/fair.c:6747:1: error: expected identifier or '(' before '+' token

6747 | +static inline bool cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
| ^

Sigh, I remembered I did a build test with !CFS_BANDWIDTH and now I went to check that build directory and noticed I didn't have CFS_BANDWIDTH disabled...

Sorry for the trouble, will send an updated patch later.

While at it, another nit.

Quoted 4 lines · Aaron Lu
kernel/sched/fair.c +0 -0
@@ -13151,10 +13161,13 @@ static void propagate_entity_cfs_rq(struct sched_entity *se)
 {
 	struct cfs_rq *cfs_rq = cfs_rq_of(se);
 
Quoted 14 lines

kernel/sched/fair.c static void propagate_entity_cfs_rq(struct sched_entity *se) · line 13170

Quoted 11 lines
kernel/sched/fair.c +7 -4
-	if (cfs_rq_throttled(cfs_rq))
-		return;
-
-	if (!throttled_hierarchy(cfs_rq))
+	/*
+	 * If a task gets attached to this cfs_rq and before being queued,
+	 * it gets migrated to another CPU due to reasons like cpuset change,
+	 * we need to make sure this cfs_rq stays on leaf cfs_rq list to
+	 * have that removed load decayed or it can cause faireness problem.
+	 */
+	if(!cfs_rq_pelt_clock_throttled(cfs_rq))

^ Can you also add a space after the "if" here.

Quoted 4 lines

Yeah, I definitely should do that, thanks for catching this.

Quoted 2 lines
Quoted 1 line
(quoted diff) +0 -0
 		list_add_leaf_cfs_rq(cfs_rq);

Just a note that the updated patch is sent here:
https://lore.kernel.org/lkml/[email protected]/

#20 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

In reply to K Prateek Nayak
Quoted 31 lines · K Prateek Nayak

Hello Peter,

Quoted 6 lines · Peter Zijlstra
Quoted 6 lines

static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) {

(quoted diff) +4 -0
+	if (task_is_throttled(p)) {
+		dequeue_throttled_task(p, flags);
+		return true;
+	}
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +1 -0
+
 	if (!p->se.sched_delayed)
 		util_est_dequeue(&rq->cfs, p);

OK, so this makes it so that either a task is fully enqueued (all cfs_rq's) or full not. A group cfs_rq is only marked throttled when all its tasks are gone, and unthrottled when a task gets added. Right?

cfs_rq (and the hierarchy below) is marked throttled when the quota has elapsed. Tasks on the throttled hierarchies will dequeue themselves completely via task work added during pick. When the last task leaves on a cfs_rq of throttled hierarchy, PELT is frozen for that cfs_rq.

When a new task is added on the hierarchy, the PELT is unfrozen and the task becomes runnable. The cfs_rq and the hierarchy is still marked throttled.

Unthrottling of hierarchy is only done at distribution.

Quoted 1 line
Quoted 9 lines
Quoted 5 lines

But propagate_entity_cfs_rq() is still doing the old thing, and has a if (cfs_rq_throttled(cfs_rq)) break; inside the for_each_sched_entity() iteration.

This seems somewhat inconsistent; or am I missing something ?

Probably an oversight. But before that, what was the reason to have stopped this propagation at throttled_cfs_rq() before the changes?

Yeah, this was one of the things I was (slowly) looking at - with this series we currently still abort in:

1) update_cfs_group
2) dequeue_entities's set_next_buddy
3) check_preempt_fair
4) yield_to
5) propagate_entity_cfs_rq

In the old design on throttle immediately remove the entire cfs_rq, freeze time for it, and stop adjusting load. In the new design we still pick from it, so we definitely don't want to stop time (and don't). I'm guessing we probably also want to now adjust load for it, but it is arguable - since all the cfs_rqs for the tg are likely to throttle at the same time, so we might not want to mess with the shares distribution, since when unthrottle comes around the most likely correct distribution is the distribution we had at the time of throttle.

Assuming we do want to adjust load for a throttle then we probably want to remove the aborts from update_cfs_group and propagate_entity_cfs_rq. I'm guessing that we need the list_add_leaf_cfs_rq from propagate, but I'm not 100% sure when they are actually doing something in propagate as opposed to enqueue.

The other 3 are the same sort of thing - scheduling pick heuristics which imo are pretty arbitrary to keep. We can reasonably say that "the most likely thing a task in a throttled hierarchy will do is just go throttle itself, so we shouldn't buddy it or let it preempt", but it would also be reasonable to let them preempt/buddy normally, in case they hold locks or such.

yield_to is used by kvm and st-dma-fence-chain.c. Yielding to a throttle-on-exit kvm cpu thread isn't useful (so no need to remove the abort there). The dma code is just yielding to a just-spawned kthread, so it should be fine either way.

#21 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

Hello Ben,

Quoted 52 lines · Benjamin Segall
Quoted 31 lines · K Prateek Nayak

Hello Peter,

Quoted 6 lines · Peter Zijlstra
Quoted 6 lines

static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) {

(quoted diff) +4 -0
+	if (task_is_throttled(p)) {
+		dequeue_throttled_task(p, flags);
+		return true;
+	}
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +1 -0
+
 	if (!p->se.sched_delayed)
 		util_est_dequeue(&rq->cfs, p);

OK, so this makes it so that either a task is fully enqueued (all cfs_rq's) or full not. A group cfs_rq is only marked throttled when all its tasks are gone, and unthrottled when a task gets added. Right?

cfs_rq (and the hierarchy below) is marked throttled when the quota has elapsed. Tasks on the throttled hierarchies will dequeue themselves completely via task work added during pick. When the last task leaves on a cfs_rq of throttled hierarchy, PELT is frozen for that cfs_rq.

When a new task is added on the hierarchy, the PELT is unfrozen and the task becomes runnable. The cfs_rq and the hierarchy is still marked throttled.

Unthrottling of hierarchy is only done at distribution.

Quoted 1 line
Quoted 9 lines
Quoted 5 lines

But propagate_entity_cfs_rq() is still doing the old thing, and has a if (cfs_rq_throttled(cfs_rq)) break; inside the for_each_sched_entity() iteration.

This seems somewhat inconsistent; or am I missing something ?

Probably an oversight. But before that, what was the reason to have stopped this propagation at throttled_cfs_rq() before the changes?

Yeah, this was one of the things I was (slowly) looking at - with this series we currently still abort in:

1) update_cfs_group
2) dequeue_entities's set_next_buddy
3) check_preempt_fair
4) yield_to
5) propagate_entity_cfs_rq

Quoted 8 lines

In the old design on throttle immediately remove the entire cfs_rq, freeze time for it, and stop adjusting load. In the new design we still pick from it, so we definitely don't want to stop time (and don't). I'm guessing we probably also want to now adjust load for it, but it is arguable - since all the cfs_rqs for the tg are likely to throttle at the same time, so we might not want to mess with the shares distribution, since when unthrottle comes around the most likely correct distribution is the distribution we had at the time of throttle.

So we were having a discussion in the parallel thread here
https://lore.kernel.org/lkml/20250903101102.GB42@bytedance/ on whether we should allow tasks on throttled hierarchies to be load balanced or not.

If we do want them to be migrated, I think we need update_cfs_group() cause otherwise we might pick off most task from the hierarchy but the sched entity of the cfs_rq will still be contributing the same amount of weight to the root making the CPU look busier than it actually is.

The alternate is to ensure we don't migrate the tasks on throttled hierarchies and let them exit to userspace in-place on the same CPU but that too is less than ideal.

Quoted 6 lines

Assuming we do want to adjust load for a throttle then we probably want to remove the aborts from update_cfs_group and propagate_entity_cfs_rq. I'm guessing that we need the list_add_leaf_cfs_rq from propagate, but I'm not 100% sure when they are actually doing something in propagate as opposed to enqueue.

I don't think this is required since the leaf cfs_rq removal is now done via the dequeue path and if the cfs_rq is not on the list, then PELT for the hierarchy below is frozen anyway.

Quoted 7 lines

The other 3 are the same sort of thing - scheduling pick heuristics which imo are pretty arbitrary to keep. We can reasonably say that "the most likely thing a task in a throttled hierarchy will do is just go throttle itself, so we shouldn't buddy it or let it preempt", but it would also be reasonable to let them preempt/buddy normally, in case they hold locks or such.

We can (and should?) consider it as any other runnable task until it isn't IMO just to reduce the amount of special handling in those cases.

Quoted 5 lines

yield_to is used by kvm and st-dma-fence-chain.c. Yielding to a throttle-on-exit kvm cpu thread isn't useful (so no need to remove the abort there). The dma code is just yielding to a just-spawned kthread, so it should be fine either way.

Yes, this one can stay.

Signature · 2 lines
Thanks and Regards,
Prateek
#22 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

Quoted 75 lines · K Prateek Nayak

Hello Ben,

Quoted 52 lines · Benjamin Segall
Quoted 31 lines · K Prateek Nayak

Hello Peter,

Quoted 6 lines · Peter Zijlstra
Quoted 6 lines

static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) {

(quoted diff) +4 -0
+	if (task_is_throttled(p)) {
+		dequeue_throttled_task(p, flags);
+		return true;
+	}
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +1 -0
+
 	if (!p->se.sched_delayed)
 		util_est_dequeue(&rq->cfs, p);

OK, so this makes it so that either a task is fully enqueued (all cfs_rq's) or full not. A group cfs_rq is only marked throttled when all its tasks are gone, and unthrottled when a task gets added. Right?

cfs_rq (and the hierarchy below) is marked throttled when the quota has elapsed. Tasks on the throttled hierarchies will dequeue themselves completely via task work added during pick. When the last task leaves on a cfs_rq of throttled hierarchy, PELT is frozen for that cfs_rq.

When a new task is added on the hierarchy, the PELT is unfrozen and the task becomes runnable. The cfs_rq and the hierarchy is still marked throttled.

Unthrottling of hierarchy is only done at distribution.

Quoted 1 line
Quoted 9 lines
Quoted 5 lines

But propagate_entity_cfs_rq() is still doing the old thing, and has a if (cfs_rq_throttled(cfs_rq)) break; inside the for_each_sched_entity() iteration.

This seems somewhat inconsistent; or am I missing something ?

Probably an oversight. But before that, what was the reason to have stopped this propagation at throttled_cfs_rq() before the changes?

Yeah, this was one of the things I was (slowly) looking at - with this series we currently still abort in:

1) update_cfs_group
2) dequeue_entities's set_next_buddy
3) check_preempt_fair
4) yield_to
5) propagate_entity_cfs_rq

Quoted 8 lines

In the old design on throttle immediately remove the entire cfs_rq, freeze time for it, and stop adjusting load. In the new design we still pick from it, so we definitely don't want to stop time (and don't). I'm guessing we probably also want to now adjust load for it, but it is arguable - since all the cfs_rqs for the tg are likely to throttle at the same time, so we might not want to mess with the shares distribution, since when unthrottle comes around the most likely correct distribution is the distribution we had at the time of throttle.

So we were having a discussion in the parallel thread here
https://lore.kernel.org/lkml/20250903101102.GB42@bytedance/ on whether we should allow tasks on throttled hierarchies to be load balanced or not.

If we do want them to be migrated, I think we need update_cfs_group() cause otherwise we might pick off most task from the hierarchy but the sched entity of the cfs_rq will still be contributing the same amount of weight to the root making the CPU look busier than it actually is.

Quoted 4 lines

The alternate is to ensure we don't migrate the tasks on throttled hierarchies and let them exit to userspace in-place on the same CPU but that too is less than ideal.

Yeah - if we don't update group se load then we shouldn't load balance throttled-hierarchy because the amount of root load migrated in the moment is always 0. Once we do all of that properly we should be fine to migrate in/out of a throttled hierarchy.

Much like wakeup there's an argument for not migrating into a throttled hierarchy, at least from an unthrottled one, where there's presumably a high likelyhood of the thread just being preempted in userspace. (And my gut feeling is that this case is probably even more common than wakeup, that need_rescheds land in (return to) userspace more often than wakeups go straight to userspace with no significant kernel work)

author
#23 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

In reply to Benjamin Segall
Quoted 52 lines · Benjamin Segall
Quoted 31 lines · K Prateek Nayak

Hello Peter,

Quoted 6 lines · Peter Zijlstra
Quoted 6 lines

static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) {

(quoted diff) +4 -0
+	if (task_is_throttled(p)) {
+		dequeue_throttled_task(p, flags);
+		return true;
+	}
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +1 -0
+
 	if (!p->se.sched_delayed)
 		util_est_dequeue(&rq->cfs, p);

OK, so this makes it so that either a task is fully enqueued (all cfs_rq's) or full not. A group cfs_rq is only marked throttled when all its tasks are gone, and unthrottled when a task gets added. Right?

cfs_rq (and the hierarchy below) is marked throttled when the quota has elapsed. Tasks on the throttled hierarchies will dequeue themselves completely via task work added during pick. When the last task leaves on a cfs_rq of throttled hierarchy, PELT is frozen for that cfs_rq.

When a new task is added on the hierarchy, the PELT is unfrozen and the task becomes runnable. The cfs_rq and the hierarchy is still marked throttled.

Unthrottling of hierarchy is only done at distribution.

Quoted 1 line
Quoted 9 lines
Quoted 5 lines

But propagate_entity_cfs_rq() is still doing the old thing, and has a if (cfs_rq_throttled(cfs_rq)) break; inside the for_each_sched_entity() iteration.

This seems somewhat inconsistent; or am I missing something ?

Probably an oversight. But before that, what was the reason to have stopped this propagation at throttled_cfs_rq() before the changes?

Yeah, this was one of the things I was (slowly) looking at - with this series we currently still abort in:

1) update_cfs_group
2) dequeue_entities's set_next_buddy
3) check_preempt_fair
4) yield_to
5) propagate_entity_cfs_rq

Quoted 9 lines

In the old design on throttle immediately remove the entire cfs_rq, freeze time for it, and stop adjusting load. In the new design we still pick from it, so we definitely don't want to stop time (and don't). I'm guessing we probably also want to now adjust load for it, but it is arguable - since all the cfs_rqs for the tg are likely to throttle at the same time, so we might not want to mess with the shares distribution, since when unthrottle comes around the most likely correct distribution is the distribution we had at the time of throttle.

I can give it a test to see how things change by adjusting load and share distribution using my previous performance tests.

Quoted 6 lines

Assuming we do want to adjust load for a throttle then we probably want to remove the aborts from update_cfs_group and propagate_entity_cfs_rq. I'm guessing that we need the list_add_leaf_cfs_rq from propagate, but I'm not 100% sure when they are actually doing something in propagate as opposed to enqueue.

Yes, commit 0258bdfaff5bd("sched/fair: Fix unfairness caused by missing load decay") added that list_add_leaf_cfs_rq() in propagate_entity_cfs_rq() to fix a problem.

Quoted 6 lines

The other 3 are the same sort of thing - scheduling pick heuristics which imo are pretty arbitrary to keep. We can reasonably say that "the most likely thing a task in a throttled hierarchy will do is just go throttle itself, so we shouldn't buddy it or let it preempt", but it would also be reasonable to let them preempt/buddy normally, in case they hold locks or such.

I think we do not need to special case tasks in throttled hierarchy in check_preempt_wakeup_fair().

Quoted 5 lines

yield_to is used by kvm and st-dma-fence-chain.c. Yielding to a throttle-on-exit kvm cpu thread isn't useful (so no need to remove the abort there). The dma code is just yielding to a just-spawned kthread, so it should be fine either way.

Get it.

The cumulated diff I'm going to experiment is below, let me know if something is wrong, thanks.

kernel/sched/fair.c +14 -21
index 3e927b9b7eeb6..c2e46b8e5e3d9 100644
@@ -3957,9 +3957,6 @@ static void update_cfs_group(struct sched_entity *se)
 	if (!gcfs_rq || !gcfs_rq->load.weight)
 		return;
 
-	if (throttled_hierarchy(gcfs_rq))
-		return;
-
 	shares = calc_group_shares(gcfs_rq);
 	if (unlikely(se->load.weight != shares))
 		reweight_entity(cfs_rq_of(se), se, shares);
@@ -5234,6 +5231,7 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 
 static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
 static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq);
+static inline int cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq);
 
 static void
 requeue_delayed_entity(struct sched_entity *se);
@@ -5729,6 +5727,11 @@ static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 	return cfs_bandwidth_used() && cfs_rq->throttled;
 }
 
+static inline int cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
+{
+	return cfs_bandwidth_used() && cfs_rq->pelt_clock_throttled;
+}
+
 /* check whether cfs_rq, or any parent, is throttled */
 static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
 {
@@ -6721,6 +6724,11 @@ static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 	return 0;
 }
 
+static inline int cfs_rq_pelt_clock_throttled(struct cfs_rq *cfs_rq)
+{
+	return 0;
+}
+
 static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
 {
 	return 0;
@@ -7074,7 +7082,7 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
 			 * Bias pick_next to pick a task from this cfs_rq, as
 			 * p is sleeping when it is within its sched_slice.
 			 */
-			if (task_sleep && se && !throttled_hierarchy(cfs_rq))
+			if (task_sleep && se)
 				set_next_buddy(se);
 			break;
 		}
@@ -8722,15 +8730,6 @@ static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int
 	if (unlikely(se == pse))
 		return;
 
-	/*
-	 * This is possible from callers such as attach_tasks(), in which we
-	 * unconditionally wakeup_preempt() after an enqueue (which may have
-	 * lead to a throttle).  This both saves work and prevents false
-	 * next-buddy nomination below.
-	 */
-	if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
-		return;
-
 	if (sched_feat(NEXT_BUDDY) && !(wake_flags & WF_FORK) && !pse->sched_delayed) {
 		set_next_buddy(pse);
 	}
@@ -13154,10 +13153,7 @@ static void propagate_entity_cfs_rq(struct sched_entity *se)
 {
 	struct cfs_rq *cfs_rq = cfs_rq_of(se);
 
-	if (cfs_rq_throttled(cfs_rq))
-		return;
-
-	if (!throttled_hierarchy(cfs_rq))
+	if (!cfs_rq_pelt_clock_throttled(cfs_rq))
 		list_add_leaf_cfs_rq(cfs_rq);
 
 	/* Start to propagate at parent */
@@ -13168,10 +13164,7 @@ static void propagate_entity_cfs_rq(struct sched_entity *se)
 
 		update_load_avg(cfs_rq, se, UPDATE_TG);
 
-		if (cfs_rq_throttled(cfs_rq))
-			break;
-
-		if (!throttled_hierarchy(cfs_rq))
+		if (!cfs_rq_pelt_clock_throttled(cfs_rq))
 			list_add_leaf_cfs_rq(cfs_rq);
 	}
 }
Signature · 1 line
2.39.5
#24 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

Hello Aaron,

kernel/sched/fair.c static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int · line 8732

Quoted 12 lines · Aaron Lu
kernel/sched/fair.c +0 -8
@@ -8722,15 +8730,6 @@ static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int
 	if (unlikely(se == pse))
 		return;
 
-	/*
-	 * This is possible from callers such as attach_tasks(), in which we
-	 * unconditionally wakeup_preempt() after an enqueue (which may have
-	 * lead to a throttle).  This both saves work and prevents false
-	 * next-buddy nomination below.
-	 */
-	if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
-		return;

I think we should have a:

	if (task_is_throttled(p))
		return;

here. I can see at least one possibility via prio_changed_fair() where a throttled task might reach here. Rest looks good. I'll still wait on Ben for the update_cfs_group() bits :)

Quoted 4 lines
kernel/sched/fair.c +0 -1
-
 	if (sched_feat(NEXT_BUDDY) && !(wake_flags & WF_FORK) && !pse->sched_delayed) {
 		set_next_buddy(pse);
 	}
Signature · 2 lines
Thanks and Regards,
Prateek
author
#25 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

Quoted 18 lines · K Prateek Nayak

Hello Aaron,

Review static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int · line 8732

Quoted 12 lines · Aaron Lu
(quoted diff) +0 -8
@@ -8722,15 +8730,6 @@ static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int
 	if (unlikely(se == pse))
 		return;
 
-	/*
-	 * This is possible from callers such as attach_tasks(), in which we
-	 * unconditionally wakeup_preempt() after an enqueue (which may have
-	 * lead to a throttle).  This both saves work and prevents false
-	 * next-buddy nomination below.
-	 */
-	if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
-		return;

I think we should have a:

Quoted 4 lines
	if (task_is_throttled(p))
		return;

here. I can see at least one possibility via prio_changed_fair()

Ah right. I didn't realize wakeup_preempt() can be called for a throttled task, I think it is not expected. What about forbid that :) (not tested in anyway, just to show the idea and get feedback)

kernel/sched/fair.c +5 -1
index cb93e74a850e8..f1383aede764f 100644
@@ -13135,7 +13135,11 @@ static void task_fork_fair(struct task_struct *p)
 static void
 prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
 {
-	if (!task_on_rq_queued(p))
+	/*
+	 * p->on_rq can be set for throttled task but there is no need to
+	 * check wakeup preempt for throttled task, so use p->se.on_rq instead.
+	 */
+	if (!p->se.on_rq)
 		return;
 
 	if (rq->cfs.nr_queued == 1)
Quoted 7 lines

where a throttled task might reach here. Rest looks good. I'll still wait on Ben for the update_cfs_group() bits :)

Quoted 4 lines
(quoted diff) +0 -1
-
 	if (sched_feat(NEXT_BUDDY) && !(wake_flags & WF_FORK) && !pse->sched_delayed) {
 		set_next_buddy(pse);
 	}

Best regards,
Aaron

#26 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

Quoted 3 lines · Aaron Lu

Ah right. I didn't realize wakeup_preempt() can be called for a throttled task, I think it is not expected. What about forbid that :) (not tested in anyway, just to show the idea and get feedback)

Ack! Prevention is better than cure ;-)

Signature · 2 lines
Thanks and Regards,
Prateek
#27 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

In reply to Aaron Lu
Quoted 46 lines · Aaron Lu
Quoted 18 lines · K Prateek Nayak

Hello Aaron,

Review static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int · line 8732

Quoted 12 lines · Aaron Lu
(quoted diff) +0 -8
@@ -8722,15 +8730,6 @@ static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int
 	if (unlikely(se == pse))
 		return;
 
-	/*
-	 * This is possible from callers such as attach_tasks(), in which we
-	 * unconditionally wakeup_preempt() after an enqueue (which may have
-	 * lead to a throttle).  This both saves work and prevents false
-	 * next-buddy nomination below.
-	 */
-	if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
-		return;

I think we should have a:

Quoted 4 lines
	if (task_is_throttled(p))
		return;

here. I can see at least one possibility via prio_changed_fair()

Ah right. I didn't realize wakeup_preempt() can be called for a throttled task, I think it is not expected. What about forbid that :) (not tested in anyway, just to show the idea and get feedback)

kernel/sched/fair.c +5 -1
index cb93e74a850e8..f1383aede764f 100644
@@ -13135,7 +13135,11 @@ static void task_fork_fair(struct task_struct *p)
 static void
 prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
 {
-	if (!task_on_rq_queued(p))
+	/*
+	 * p->on_rq can be set for throttled task but there is no need to
+	 * check wakeup preempt for throttled task, so use p->se.on_rq instead.
+	 */
+	if (!p->se.on_rq)
 		return;
 
 	if (rq->cfs.nr_queued == 1)
Quoted 2 lines
Quoted 2 lines

where a throttled task might reach here. Rest looks good. I'll still wait on Ben for the update_cfs_group() bits :)

Yeah, I think I agree with all of these (this patch and the previous patch); the preempt ones are subjective but I'd probably default to "no special case needed for throttle". Removing the check in update_cfs_group() I think is correct, unless we want to freeze everything, yeah. (And that seems dangerous in its own way)

author
#28 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

Quoted 52 lines · Benjamin Segall
Quoted 46 lines · Aaron Lu
Quoted 18 lines · K Prateek Nayak

Hello Aaron,

Review static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int · line 8732

Quoted 12 lines · Aaron Lu
(quoted diff) +0 -8
@@ -8722,15 +8730,6 @@ static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int
 	if (unlikely(se == pse))
 		return;
 
-	/*
-	 * This is possible from callers such as attach_tasks(), in which we
-	 * unconditionally wakeup_preempt() after an enqueue (which may have
-	 * lead to a throttle).  This both saves work and prevents false
-	 * next-buddy nomination below.
-	 */
-	if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
-		return;

I think we should have a:

Quoted 4 lines
	if (task_is_throttled(p))
		return;

here. I can see at least one possibility via prio_changed_fair()

Ah right. I didn't realize wakeup_preempt() can be called for a throttled task, I think it is not expected. What about forbid that :) (not tested in anyway, just to show the idea and get feedback)

kernel/sched/fair.c +5 -1
index cb93e74a850e8..f1383aede764f 100644
@@ -13135,7 +13135,11 @@ static void task_fork_fair(struct task_struct *p)
 static void
 prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
 {
-	if (!task_on_rq_queued(p))
+	/*
+	 * p->on_rq can be set for throttled task but there is no need to
+	 * check wakeup preempt for throttled task, so use p->se.on_rq instead.
+	 */
+	if (!p->se.on_rq)
 		return;
 
 	if (rq->cfs.nr_queued == 1)
Quoted 2 lines
Quoted 2 lines

where a throttled task might reach here. Rest looks good. I'll still wait on Ben for the update_cfs_group() bits :)

Quoted 5 lines

Yeah, I think I agree with all of these (this patch and the previous patch); the preempt ones are subjective but I'd probably default to "no special case needed for throttle". Removing the check in update_cfs_group() I think is correct, unless we want to freeze everything, yeah. (And that seems dangerous in its own way)

Thanks for all these info, let me go ahead and send these changes for review then :)

author
#29 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

In reply to Aaron Lu
Quoted 19 lines · Aaron Lu
Quoted 18 lines · K Prateek Nayak

Hello Aaron,

Review static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int · line 8732

Quoted 12 lines · Aaron Lu
(quoted diff) +0 -8
@@ -8722,15 +8730,6 @@ static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int
 	if (unlikely(se == pse))
 		return;
 
-	/*
-	 * This is possible from callers such as attach_tasks(), in which we
-	 * unconditionally wakeup_preempt() after an enqueue (which may have
-	 * lead to a throttle).  This both saves work and prevents false
-	 * next-buddy nomination below.
-	 */
-	if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
-		return;

I think we should have a:

Quoted 9 lines
Quoted 4 lines
	if (task_is_throttled(p))
		return;

here. I can see at least one possibility via prio_changed_fair()

Ah right. I didn't realize wakeup_preempt() can be called for a throttled task, I think it is not expected. What about forbid that :) (not tested in anyway, just to show the idea and get feedback)

Turned out there are other places that also call wakeup_preempt() for a throttled task, like sched_setaffinity() -> move_queued_task() -> wakeup_preempt(), and it's not possible to change the logic there as it's outside of fair, so I'll go with your suggestion to add a task_is_throttled() check in check_preempt_wakeup_fair().

Quoted 25 lines
kernel/sched/fair.c +5 -1
index cb93e74a850e8..f1383aede764f 100644
@@ -13135,7 +13135,11 @@ static void task_fork_fair(struct task_struct *p)
 static void
 prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
 {
-	if (!task_on_rq_queued(p))
+	/*
+	 * p->on_rq can be set for throttled task but there is no need to
+	 * check wakeup preempt for throttled task, so use p->se.on_rq instead.
+	 */
+	if (!p->se.on_rq)
 		return;
 
 	if (rq->cfs.nr_queued == 1)
Quoted 8 lines

where a throttled task might reach here. Rest looks good. I'll still wait on Ben for the update_cfs_group() bits :)

Quoted 4 lines
(quoted diff) +0 -1
-
 	if (sched_feat(NEXT_BUDDY) && !(wake_flags & WF_FORK) && !pse->sched_delayed) {
 		set_next_buddy(pse);
 	}
author
#30 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

In reply to Aaron Lu
Quoted 53 lines · Aaron Lu
Quoted 52 lines · Benjamin Segall
Quoted 31 lines · K Prateek Nayak

Hello Peter,

Quoted 6 lines · Peter Zijlstra
Quoted 6 lines

static bool dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) {

(quoted diff) +4 -0
+	if (task_is_throttled(p)) {
+		dequeue_throttled_task(p, flags);
+		return true;
+	}
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +1 -0
+
 	if (!p->se.sched_delayed)
 		util_est_dequeue(&rq->cfs, p);

OK, so this makes it so that either a task is fully enqueued (all cfs_rq's) or full not. A group cfs_rq is only marked throttled when all its tasks are gone, and unthrottled when a task gets added. Right?

cfs_rq (and the hierarchy below) is marked throttled when the quota has elapsed. Tasks on the throttled hierarchies will dequeue themselves completely via task work added during pick. When the last task leaves on a cfs_rq of throttled hierarchy, PELT is frozen for that cfs_rq.

When a new task is added on the hierarchy, the PELT is unfrozen and the task becomes runnable. The cfs_rq and the hierarchy is still marked throttled.

Unthrottling of hierarchy is only done at distribution.

Quoted 1 line
Quoted 9 lines
Quoted 5 lines

But propagate_entity_cfs_rq() is still doing the old thing, and has a if (cfs_rq_throttled(cfs_rq)) break; inside the for_each_sched_entity() iteration.

This seems somewhat inconsistent; or am I missing something ?

Probably an oversight. But before that, what was the reason to have stopped this propagation at throttled_cfs_rq() before the changes?

Yeah, this was one of the things I was (slowly) looking at - with this series we currently still abort in:

1) update_cfs_group
2) dequeue_entities's set_next_buddy
3) check_preempt_fair
4) yield_to
5) propagate_entity_cfs_rq

Quoted 3 lines
Quoted 3 lines

In the old design on throttle immediately remove the entire cfs_rq, freeze time for it, and stop adjusting load. In the new design we still pick from it, so we definitely don't want to stop time (and don't). I'm

Per my understanding, we keep PELT clock running because we want the throttled cfs_rq's load to continue get update when it still has tasks running in kernel mode and have that up2date load could let it have a hopefully more accurate weight through update_cfs_group(). So it looks to me, if PELT clock should not be stopped, then we should not abort in propagate_entity_cfs_rq() and update_cfs_group(). I missed these two aborts in these two functions, but now you and Peter have pointed this out, I suppose there is no doubt we should not abort in update_cfs_group() and propagate_entity_cfs_rq()? If we should not mess with shares distribution, then the up2date load is not useful and why not simply freeze PELT clock on throttle :)

Quoted 10 lines
Quoted 6 lines

guessing we probably also want to now adjust load for it, but it is arguable - since all the cfs_rqs for the tg are likely to throttle at the same time, so we might not want to mess with the shares distribution, since when unthrottle comes around the most likely correct distribution is the distribution we had at the time of throttle.

I can give it a test to see how things change by adjusting load and share distribution using my previous performance tests.

Run hackbench and netperf on AMD Genoa and didn't notice any obvious difference with the cumulated diff.

Quoted 18 lines
Quoted 6 lines

Assuming we do want to adjust load for a throttle then we probably want to remove the aborts from update_cfs_group and propagate_entity_cfs_rq. I'm guessing that we need the list_add_leaf_cfs_rq from propagate, but I'm not 100% sure when they are actually doing something in propagate as opposed to enqueue.

Yes, commit 0258bdfaff5bd("sched/fair: Fix unfairness caused by missing load decay") added that list_add_leaf_cfs_rq() in propagate_entity_cfs_rq() to fix a problem.

Quoted 6 lines

The other 3 are the same sort of thing - scheduling pick heuristics which imo are pretty arbitrary to keep. We can reasonably say that "the most likely thing a task in a throttled hierarchy will do is just go throttle itself, so we shouldn't buddy it or let it preempt", but it would also be reasonable to let them preempt/buddy normally, in case they hold locks or such.

Quoted 3 lines

I think we do not need to special case tasks in throttled hierarchy in check_preempt_wakeup_fair().

Since there is pros and cons either way and consider the performance test result, I'm now feeling maybe we can leave these 3 as is and revisit them later when there is some clear case.

Quoted 10 lines
Quoted 5 lines

yield_to is used by kvm and st-dma-fence-chain.c. Yielding to a throttle-on-exit kvm cpu thread isn't useful (so no need to remove the abort there). The dma code is just yielding to a just-spawned kthread, so it should be fine either way.

Get it.

The cumulated diff I'm going to experiment is below, let me know if something is wrong, thanks.

author
#31 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

On Thu, Sep 04, 2025 at 08:04:01PM +0800, Aaron Lu wrote:
... ...

Quoted 11 lines

Per my understanding, we keep PELT clock running because we want the throttled cfs_rq's load to continue get update when it still has tasks running in kernel mode and have that up2date load could let it have a hopefully more accurate weight through update_cfs_group(). So it looks to me, if PELT clock should not be stopped, then we should not abort in propagate_entity_cfs_rq() and update_cfs_group(). I missed these two aborts in these two functions, but now you and Peter have pointed this out, I suppose there is no doubt we should not abort in update_cfs_group() and propagate_entity_cfs_rq()? If we should not mess with shares distribution, then the up2date load is not useful and why not simply freeze PELT clock on throttle :)

With more thinking, when PELT clock is running, root level cfs_rq's load can be updated and that is useful for things like load balance.

update_cfs_group() is a different story and as Ben said, there is cons and pros whether to abort there for throttled cfs_rq so there's no obvious thing to do for it.

#32 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

In reply to Aaron Lu
Quoted 42 lines · Aaron Lu
kernel/sched/fair.c +42 -0
+static bool enqueue_throttled_task(struct task_struct *p)
+{
+	struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
+
+	/* @p should have gone through dequeue_throttled_task() first */
+	WARN_ON_ONCE(!list_empty(&p->throttle_node));
+
+	/*
+	 * If the throttled task @p is enqueued to a throttled cfs_rq,
+	 * take the fast path by directly putting the task on the
+	 * target cfs_rq's limbo list.
+	 *
+	 * Do not do that when @p is current because the following race can
+	 * cause @p's group_node to be incorectly re-insterted in its rq's
+	 * cfs_tasks list, despite being throttled:
+	 *
+	 *     cpuX                       cpuY
+	 *   p ret2user
+	 *  throttle_cfs_rq_work()  sched_move_task(p)
+	 *  LOCK task_rq_lock
+	 *  dequeue_task_fair(p)
+	 *  UNLOCK task_rq_lock
+	 *                          LOCK task_rq_lock
+	 *                          task_current_donor(p) == true
+	 *                          task_on_rq_queued(p) == true
+	 *                          dequeue_task(p)
+	 *                          put_prev_task(p)
+	 *                          sched_change_group()
+	 *                          enqueue_task(p) -> p's new cfs_rq
+	 *                                             is throttled, go
+	 *                                             fast path and skip
+	 *                                             actual enqueue
+	 *                          set_next_task(p)
+	 *                    list_move(&se->group_node, &rq->cfs_tasks); // bug
+	 *  schedule()
+	 *
+	 * In the above race case, @p current cfs_rq is in the same rq as
+	 * its previous cfs_rq because sched_move_task() only moves a task
+	 * to a different group from the same rq, so we can use its current
+	 * cfs_rq to derive rq and test if the task is current.
+	 */
+	if (throttled_hierarchy(cfs_rq) &&

kernel/sched/fair.c void init_cfs_throttle_work(struct task_struct *p) · line 5900

Quoted 10 lines
kernel/sched/fair.c +10 -0
+	    !task_current_donor(rq_of(cfs_rq), p)) {
+		list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list);
+		return true;
+	}
+
+	/* we can't take the fast path, do an actual enqueue*/
+	p->throttled = false;
+	return false;
+}
+

Is there a reason that __set_next_task_fair cannot check p->se.on_rq as well as (or instead of) task_on_rq_queued()? All of the _entity parts of set_next/put_prev check se.on_rq for this sort of thing, so that seems fairly standard. And se.on_rq should exactly match if the task is on cfs_tasks since that add/remove is done in account_entity_{en,de}queue.

author
#33 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

Quoted 52 lines · Benjamin Segall
Quoted 50 lines · Aaron Lu
kernel/sched/fair.c +50 -0
+static bool enqueue_throttled_task(struct task_struct *p)
+{
+	struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
+
+	/* @p should have gone through dequeue_throttled_task() first */
+	WARN_ON_ONCE(!list_empty(&p->throttle_node));
+
+	/*
+	 * If the throttled task @p is enqueued to a throttled cfs_rq,
+	 * take the fast path by directly putting the task on the
+	 * target cfs_rq's limbo list.
+	 *
+	 * Do not do that when @p is current because the following race can
+	 * cause @p's group_node to be incorectly re-insterted in its rq's
+	 * cfs_tasks list, despite being throttled:
+	 *
+	 *     cpuX                       cpuY
+	 *   p ret2user
+	 *  throttle_cfs_rq_work()  sched_move_task(p)
+	 *  LOCK task_rq_lock
+	 *  dequeue_task_fair(p)
+	 *  UNLOCK task_rq_lock
+	 *                          LOCK task_rq_lock
+	 *                          task_current_donor(p) == true
+	 *                          task_on_rq_queued(p) == true
+	 *                          dequeue_task(p)
+	 *                          put_prev_task(p)
+	 *                          sched_change_group()
+	 *                          enqueue_task(p) -> p's new cfs_rq
+	 *                                             is throttled, go
+	 *                                             fast path and skip
+	 *                                             actual enqueue
+	 *                          set_next_task(p)
+	 *                    list_move(&se->group_node, &rq->cfs_tasks); // bug
+	 *  schedule()
+	 *
+	 * In the above race case, @p current cfs_rq is in the same rq as
+	 * its previous cfs_rq because sched_move_task() only moves a task
+	 * to a different group from the same rq, so we can use its current
+	 * cfs_rq to derive rq and test if the task is current.
+	 */
+	if (throttled_hierarchy(cfs_rq) &&
+	    !task_current_donor(rq_of(cfs_rq), p)) {
+		list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list);
+		return true;
+	}
+
+	/* we can't take the fast path, do an actual enqueue*/
+	p->throttled = false;
+	return false;
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+

Is there a reason that __set_next_task_fair cannot check p->se.on_rq as well as (or instead of) task_on_rq_queued()? All of the _entity parts of set_next/put_prev check se.on_rq for this sort of thing, so that seems fairly standard. And se.on_rq should exactly match if the task is on cfs_tasks since that add/remove is done in account_entity_{en,de}queue.

Makes sense to me.

Only thing that feels a little strange is, a throttled/dequeued task is set as next now. Maybe not a big deal. I booted a VM and run some tests, didn't notice anything wrong but I could very well miss some cases.

author
#34 lore raw

Re: [PATCH v4 3/5] sched/fair: Switch to task based throttle model

Quoted 62 lines · Aaron Lu
Quoted 52 lines · Benjamin Segall
Quoted 50 lines · Aaron Lu
(quoted diff) +50 -0
+static bool enqueue_throttled_task(struct task_struct *p)
+{
+	struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
+
+	/* @p should have gone through dequeue_throttled_task() first */
+	WARN_ON_ONCE(!list_empty(&p->throttle_node));
+
+	/*
+	 * If the throttled task @p is enqueued to a throttled cfs_rq,
+	 * take the fast path by directly putting the task on the
+	 * target cfs_rq's limbo list.
+	 *
+	 * Do not do that when @p is current because the following race can
+	 * cause @p's group_node to be incorectly re-insterted in its rq's
+	 * cfs_tasks list, despite being throttled:
+	 *
+	 *     cpuX                       cpuY
+	 *   p ret2user
+	 *  throttle_cfs_rq_work()  sched_move_task(p)
+	 *  LOCK task_rq_lock
+	 *  dequeue_task_fair(p)
+	 *  UNLOCK task_rq_lock
+	 *                          LOCK task_rq_lock
+	 *                          task_current_donor(p) == true
+	 *                          task_on_rq_queued(p) == true
+	 *                          dequeue_task(p)
+	 *                          put_prev_task(p)
+	 *                          sched_change_group()
+	 *                          enqueue_task(p) -> p's new cfs_rq
+	 *                                             is throttled, go
+	 *                                             fast path and skip
+	 *                                             actual enqueue
+	 *                          set_next_task(p)
+	 *                    list_move(&se->group_node, &rq->cfs_tasks); // bug
+	 *  schedule()
+	 *
+	 * In the above race case, @p current cfs_rq is in the same rq as
+	 * its previous cfs_rq because sched_move_task() only moves a task
+	 * to a different group from the same rq, so we can use its current
+	 * cfs_rq to derive rq and test if the task is current.
+	 */
+	if (throttled_hierarchy(cfs_rq) &&
+	    !task_current_donor(rq_of(cfs_rq), p)) {
+		list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list);
+		return true;
+	}
+
+	/* we can't take the fast path, do an actual enqueue*/
+	p->throttled = false;
+	return false;
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+

Is there a reason that __set_next_task_fair cannot check p->se.on_rq as well as (or instead of) task_on_rq_queued()? All of the _entity parts of set_next/put_prev check se.on_rq for this sort of thing, so that seems fairly standard. And se.on_rq should exactly match if the task is on cfs_tasks since that add/remove is done in account_entity_{en,de}queue.

Quoted 5 lines

Makes sense to me.

Only thing that feels a little strange is, a throttled/dequeued task is set as next now. Maybe not a big deal. I booted a VM and run some tests, didn't notice anything wrong but I could very well miss some cases.

Sorry, I should have added: the above test was done with following diff:

kernel/sched/fair.c +2 -32
index cb93e74a850e8..7a6782617c0e8 100644
@@ -5836,38 +5836,8 @@ static bool enqueue_throttled_task(struct task_struct *p)
 	 * If the throttled task @p is enqueued to a throttled cfs_rq,
 	 * take the fast path by directly putting the task on the
 	 * target cfs_rq's limbo list.
-	 *
-	 * Do not do that when @p is current because the following race can
-	 * cause @p's group_node to be incorectly re-insterted in its rq's
-	 * cfs_tasks list, despite being throttled:
-	 *
-	 *     cpuX                       cpuY
-	 *   p ret2user
-	 *  throttle_cfs_rq_work()  sched_move_task(p)
-	 *  LOCK task_rq_lock
-	 *  dequeue_task_fair(p)
-	 *  UNLOCK task_rq_lock
-	 *                          LOCK task_rq_lock
-	 *                          task_current_donor(p) == true
-	 *                          task_on_rq_queued(p) == true
-	 *                          dequeue_task(p)
-	 *                          put_prev_task(p)
-	 *                          sched_change_group()
-	 *                          enqueue_task(p) -> p's new cfs_rq
-	 *                                             is throttled, go
-	 *                                             fast path and skip
-	 *                                             actual enqueue
-	 *                          set_next_task(p)
-	 *                    list_move(&se->group_node, &rq->cfs_tasks); // bug
-	 *  schedule()
-	 *
-	 * In the above race case, @p current cfs_rq is in the same rq as
-	 * its previous cfs_rq because sched_move_task() only moves a task
-	 * to a different group from the same rq, so we can use its current
-	 * cfs_rq to derive rq and test if the task is current.
 	 */
-	if (throttled_hierarchy(cfs_rq) &&
-	    !task_current_donor(rq_of(cfs_rq), p)) {
+	if (throttled_hierarchy(cfs_rq)) {
 		list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list);
 		return true;
 	}
@@ -13256,7 +13226,7 @@ static void __set_next_task_fair(struct rq *rq, struct task_struct *p, bool firs
 {
 	struct sched_entity *se = &p->se;
 
-	if (task_on_rq_queued(p)) {
+	if (se->on_rq) {
 		/*
 		 * Move the next running task to the front of the list, so our
 		 * cfs_tasks list becomes MRU one.
author
#35 lore raw

PATCH v4 4/5 sched/fair: Task based throttle time accounting

In reply to Aaron Lu

With task based throttle model, the previous way to check cfs_rq's nr_queued to decide if throttled time should be accounted doesn't work as expected, e.g. when a cfs_rq which has a single task is throttled, that task could later block in kernel mode instead of being dequeued on limbo list and accounting this as throttled time is not accurate.

Rework throttle time accounting for a cfs_rq as follows:
- start accounting when the first task gets throttled in its hierarchy;
- stop accounting on unthrottle.

Note that there will be a time gap between when a cfs_rq is throttled and when a task in its hierarchy is actually throttled. This accounting mechanism only starts accounting in the latter case.

---
 kernel/sched/fair.c  | 56 ++++++++++++++++++++++++--------------------
 kernel/sched/sched.h |  1 +
 2 files changed, 32 insertions(+), 25 deletions(-)
kernel/sched/fair.c +31 -25
index 25b1014d4ef86..bdc9bfa0b9efa 100644
@@ -5293,19 +5293,12 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 		check_enqueue_throttle(cfs_rq);
 		list_add_leaf_cfs_rq(cfs_rq);
 #ifdef CONFIG_CFS_BANDWIDTH
-		if (throttled_hierarchy(cfs_rq)) {
+		if (cfs_rq->pelt_clock_throttled) {
 			struct rq *rq = rq_of(cfs_rq);
 
-			if (cfs_rq_throttled(cfs_rq) && !cfs_rq->throttled_clock)
-				cfs_rq->throttled_clock = rq_clock(rq);
-			if (!cfs_rq->throttled_clock_self)
-				cfs_rq->throttled_clock_self = rq_clock(rq);
-
-			if (cfs_rq->pelt_clock_throttled) {
-				cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) -
-					cfs_rq->throttled_clock_pelt;
-				cfs_rq->pelt_clock_throttled = 0;
-			}
+			cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) -
+				cfs_rq->throttled_clock_pelt;
+			cfs_rq->pelt_clock_throttled = 0;
 		}
 #endif
 	}
@@ -5393,7 +5386,7 @@ dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 		 * DELAY_DEQUEUE relies on spurious wakeups, special task
 		 * states must not suffer spurious wakeups, excempt them.
 		 */
-		if (flags & DEQUEUE_SPECIAL)
+		if (flags & (DEQUEUE_SPECIAL | DEQUEUE_THROTTLE))
 			delay = false;
 
 		WARN_ON_ONCE(delay && se->sched_delayed);
@@ -5799,7 +5792,7 @@ static void throttle_cfs_rq_work(struct callback_head *work)
 		rq = scope.rq;
 		update_rq_clock(rq);
 		WARN_ON_ONCE(p->throttled || !list_empty(&p->throttle_node));
-		dequeue_task_fair(rq, p, DEQUEUE_SLEEP | DEQUEUE_SPECIAL);
+		dequeue_task_fair(rq, p, DEQUEUE_SLEEP | DEQUEUE_THROTTLE);
 		list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list);
 		/*
 		 * Must not set throttled before dequeue or dequeue will
@@ -5959,6 +5952,17 @@ static inline void task_throttle_setup_work(struct task_struct *p)
 	task_work_add(p, &p->sched_throttle_work, TWA_RESUME);
 }
 
+static void record_throttle_clock(struct cfs_rq *cfs_rq)
+{
+	struct rq *rq = rq_of(cfs_rq);
+
+	if (cfs_rq_throttled(cfs_rq) && !cfs_rq->throttled_clock)
+		cfs_rq->throttled_clock = rq_clock(rq);
+
+	if (!cfs_rq->throttled_clock_self)
+		cfs_rq->throttled_clock_self = rq_clock(rq);
+}
+
 static int tg_throttle_down(struct task_group *tg, void *data)
 {
 	struct rq *rq = data;
@@ -5967,21 +5971,17 @@ static int tg_throttle_down(struct task_group *tg, void *data)
 	if (cfs_rq->throttle_count++)
 		return 0;
 
-
-	/* group is entering throttled state, stop time */
-	WARN_ON_ONCE(cfs_rq->throttled_clock_self);
-	if (cfs_rq->nr_queued)
-		cfs_rq->throttled_clock_self = rq_clock(rq);
-	else {
-		/*
-		 * For cfs_rqs that still have entities enqueued, PELT clock
-		 * stop happens at dequeue time when all entities are dequeued.
-		 */
+	/*
+	 * For cfs_rqs that still have entities enqueued, PELT clock
+	 * stop happens at dequeue time when all entities are dequeued.
+	 */
+	if (!cfs_rq->nr_queued) {
 		list_del_leaf_cfs_rq(cfs_rq);
 		cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq);
 		cfs_rq->pelt_clock_throttled = 1;
 	}
 
+	WARN_ON_ONCE(cfs_rq->throttled_clock_self);
 	WARN_ON_ONCE(!list_empty(&cfs_rq->throttled_limbo_list));
 	return 0;
 }
@@ -6024,8 +6024,6 @@ static bool throttle_cfs_rq(struct cfs_rq *cfs_rq)
 	 */
 	cfs_rq->throttled = 1;
 	WARN_ON_ONCE(cfs_rq->throttled_clock);
-	if (cfs_rq->nr_queued)
-		cfs_rq->throttled_clock = rq_clock(rq);
 	return true;
 }
 
@@ -6733,6 +6731,7 @@ static void task_throttle_setup_work(struct task_struct *p) {}
 static bool task_is_throttled(struct task_struct *p) { return false; }
 static void dequeue_throttled_task(struct task_struct *p, int flags) {}
 static bool enqueue_throttled_task(struct task_struct *p) { return false; }
+static void record_throttle_clock(struct cfs_rq *cfs_rq) {}
 
 static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 {
@@ -7051,6 +7050,7 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
 	bool was_sched_idle = sched_idle_rq(rq);
 	bool task_sleep = flags & DEQUEUE_SLEEP;
 	bool task_delayed = flags & DEQUEUE_DELAYED;
+	bool task_throttled = flags & DEQUEUE_THROTTLE;
 	struct task_struct *p = NULL;
 	int h_nr_idle = 0;
 	int h_nr_queued = 0;
@@ -7084,6 +7084,9 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
 		if (cfs_rq_is_idle(cfs_rq))
 			h_nr_idle = h_nr_queued;
 
+		if (throttled_hierarchy(cfs_rq) && task_throttled)
+			record_throttle_clock(cfs_rq);
+
 		/* Don't dequeue parent if it has other entities besides us */
 		if (cfs_rq->load.weight) {
 			slice = cfs_rq_min_slice(cfs_rq);
@@ -7120,6 +7123,9 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
 
 		if (cfs_rq_is_idle(cfs_rq))
 			h_nr_idle = h_nr_queued;
+
+		if (throttled_hierarchy(cfs_rq) && task_throttled)
+			record_throttle_clock(cfs_rq);
 	}
 
 	sub_nr_running(rq, h_nr_queued);
kernel/sched/sched.h +1 -0
index 528af3ef8fc1e..c56a939c73fc4 100644
@@ -2347,6 +2347,7 @@ extern const u32		sched_prio_to_wmult[40];
 #define DEQUEUE_SPECIAL		0x10
 #define DEQUEUE_MIGRATING	0x100 /* Matches ENQUEUE_MIGRATING */
 #define DEQUEUE_DELAYED		0x200 /* Matches ENQUEUE_DELAYED */
+#define DEQUEUE_THROTTLE	0x800
 
 #define ENQUEUE_WAKEUP		0x01
 #define ENQUEUE_RESTORE		0x02
Signature · 1 line
2.39.5
merge bot Applied
#36 lore raw

[tip: sched/core] sched/fair: Task based throttle time accounting

The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     eb962f251fbba251a0d34897d6170f7616d70c52
Gitweb:        https://git.kernel.org/tip/eb962f251fbba251a0d34897d6170f7616d70c52
Author:        Aaron Lu <[email protected]>
AuthorDate:    Fri, 29 Aug 2025 16:11:19 +08:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 03 Sep 2025 10:03:14 +02:00

sched/fair: Task based throttle time accounting

With task based throttle model, the previous way to check cfs_rq's nr_queued to decide if throttled time should be accounted doesn't work as expected, e.g. when a cfs_rq which has a single task is throttled, that task could later block in kernel mode instead of being dequeued on limbo list and accounting this as throttled time is not accurate.

Rework throttle time accounting for a cfs_rq as follows:
- start accounting when the first task gets throttled in its hierarchy;
- stop accounting on unthrottle.

Note that there will be a time gap between when a cfs_rq is throttled and when a task in its hierarchy is actually throttled. This accounting mechanism only starts accounting in the latter case.

---
 kernel/sched/fair.c  | 56 +++++++++++++++++++++++--------------------
 kernel/sched/sched.h |  1 +-
 2 files changed, 32 insertions(+), 25 deletions(-)
kernel/sched/fair.c +31 -25
index 25b1014..bdc9bfa 100644
@@ -5293,19 +5293,12 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 		check_enqueue_throttle(cfs_rq);
 		list_add_leaf_cfs_rq(cfs_rq);
 #ifdef CONFIG_CFS_BANDWIDTH
-		if (throttled_hierarchy(cfs_rq)) {
+		if (cfs_rq->pelt_clock_throttled) {
 			struct rq *rq = rq_of(cfs_rq);
 
-			if (cfs_rq_throttled(cfs_rq) && !cfs_rq->throttled_clock)
-				cfs_rq->throttled_clock = rq_clock(rq);
-			if (!cfs_rq->throttled_clock_self)
-				cfs_rq->throttled_clock_self = rq_clock(rq);
-
-			if (cfs_rq->pelt_clock_throttled) {
-				cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) -
-					cfs_rq->throttled_clock_pelt;
-				cfs_rq->pelt_clock_throttled = 0;
-			}
+			cfs_rq->throttled_clock_pelt_time += rq_clock_pelt(rq) -
+				cfs_rq->throttled_clock_pelt;
+			cfs_rq->pelt_clock_throttled = 0;
 		}
 #endif
 	}
@@ -5393,7 +5386,7 @@ dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 		 * DELAY_DEQUEUE relies on spurious wakeups, special task
 		 * states must not suffer spurious wakeups, excempt them.
 		 */
-		if (flags & DEQUEUE_SPECIAL)
+		if (flags & (DEQUEUE_SPECIAL | DEQUEUE_THROTTLE))
 			delay = false;
 
 		WARN_ON_ONCE(delay && se->sched_delayed);
@@ -5799,7 +5792,7 @@ static void throttle_cfs_rq_work(struct callback_head *work)
 		rq = scope.rq;
 		update_rq_clock(rq);
 		WARN_ON_ONCE(p->throttled || !list_empty(&p->throttle_node));
-		dequeue_task_fair(rq, p, DEQUEUE_SLEEP | DEQUEUE_SPECIAL);
+		dequeue_task_fair(rq, p, DEQUEUE_SLEEP | DEQUEUE_THROTTLE);
 		list_add(&p->throttle_node, &cfs_rq->throttled_limbo_list);
 		/*
 		 * Must not set throttled before dequeue or dequeue will
@@ -5959,6 +5952,17 @@ static inline void task_throttle_setup_work(struct task_struct *p)
 	task_work_add(p, &p->sched_throttle_work, TWA_RESUME);
 }
 
+static void record_throttle_clock(struct cfs_rq *cfs_rq)
+{
+	struct rq *rq = rq_of(cfs_rq);
+
+	if (cfs_rq_throttled(cfs_rq) && !cfs_rq->throttled_clock)
+		cfs_rq->throttled_clock = rq_clock(rq);
+
+	if (!cfs_rq->throttled_clock_self)
+		cfs_rq->throttled_clock_self = rq_clock(rq);
+}
+
 static int tg_throttle_down(struct task_group *tg, void *data)
 {
 	struct rq *rq = data;
@@ -5967,21 +5971,17 @@ static int tg_throttle_down(struct task_group *tg, void *data)
 	if (cfs_rq->throttle_count++)
 		return 0;
 
-
-	/* group is entering throttled state, stop time */
-	WARN_ON_ONCE(cfs_rq->throttled_clock_self);
-	if (cfs_rq->nr_queued)
-		cfs_rq->throttled_clock_self = rq_clock(rq);
-	else {
-		/*
-		 * For cfs_rqs that still have entities enqueued, PELT clock
-		 * stop happens at dequeue time when all entities are dequeued.
-		 */
+	/*
+	 * For cfs_rqs that still have entities enqueued, PELT clock
+	 * stop happens at dequeue time when all entities are dequeued.
+	 */
+	if (!cfs_rq->nr_queued) {
 		list_del_leaf_cfs_rq(cfs_rq);
 		cfs_rq->throttled_clock_pelt = rq_clock_pelt(rq);
 		cfs_rq->pelt_clock_throttled = 1;
 	}
 
+	WARN_ON_ONCE(cfs_rq->throttled_clock_self);
 	WARN_ON_ONCE(!list_empty(&cfs_rq->throttled_limbo_list));
 	return 0;
 }
@@ -6024,8 +6024,6 @@ static bool throttle_cfs_rq(struct cfs_rq *cfs_rq)
 	 */
 	cfs_rq->throttled = 1;
 	WARN_ON_ONCE(cfs_rq->throttled_clock);
-	if (cfs_rq->nr_queued)
-		cfs_rq->throttled_clock = rq_clock(rq);
 	return true;
 }
 
@@ -6733,6 +6731,7 @@ static void task_throttle_setup_work(struct task_struct *p) {}
 static bool task_is_throttled(struct task_struct *p) { return false; }
 static void dequeue_throttled_task(struct task_struct *p, int flags) {}
 static bool enqueue_throttled_task(struct task_struct *p) { return false; }
+static void record_throttle_clock(struct cfs_rq *cfs_rq) {}
 
 static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
 {
@@ -7051,6 +7050,7 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
 	bool was_sched_idle = sched_idle_rq(rq);
 	bool task_sleep = flags & DEQUEUE_SLEEP;
 	bool task_delayed = flags & DEQUEUE_DELAYED;
+	bool task_throttled = flags & DEQUEUE_THROTTLE;
 	struct task_struct *p = NULL;
 	int h_nr_idle = 0;
 	int h_nr_queued = 0;
@@ -7084,6 +7084,9 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
 		if (cfs_rq_is_idle(cfs_rq))
 			h_nr_idle = h_nr_queued;
 
+		if (throttled_hierarchy(cfs_rq) && task_throttled)
+			record_throttle_clock(cfs_rq);
+
 		/* Don't dequeue parent if it has other entities besides us */
 		if (cfs_rq->load.weight) {
 			slice = cfs_rq_min_slice(cfs_rq);
@@ -7120,6 +7123,9 @@ static int dequeue_entities(struct rq *rq, struct sched_entity *se, int flags)
 
 		if (cfs_rq_is_idle(cfs_rq))
 			h_nr_idle = h_nr_queued;
+
+		if (throttled_hierarchy(cfs_rq) && task_throttled)
+			record_throttle_clock(cfs_rq);
 	}
 
 	sub_nr_running(rq, h_nr_queued);
kernel/sched/sched.h +1 -0
index 6e1b37b..b5367c5 100644
@@ -2344,6 +2344,7 @@ extern const u32		sched_prio_to_wmult[40];
 #define DEQUEUE_SPECIAL		0x10
 #define DEQUEUE_MIGRATING	0x100 /* Matches ENQUEUE_MIGRATING */
 #define DEQUEUE_DELAYED		0x200 /* Matches ENQUEUE_DELAYED */
+#define DEQUEUE_THROTTLE	0x800
 
 #define ENQUEUE_WAKEUP		0x01
 #define ENQUEUE_RESTORE		0x02
author
#37 lore raw

PATCH v4 5/5 sched/fair: Get rid of throttled_lb_pair()

In reply to Aaron Lu

Now that throttled tasks are dequeued and can not stay on rq's cfs_tasks list, there is no need to take special care of these throttled tasks anymore in load balance.

---
 kernel/sched/fair.c | 35 ++++-------------------------------
 1 file changed, 4 insertions(+), 31 deletions(-)
kernel/sched/fair.c +4 -31
index bdc9bfa0b9efa..df8dc389af8e1 100644
@@ -5735,23 +5735,6 @@ static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
 	return cfs_bandwidth_used() && cfs_rq->throttle_count;
 }
 
-/*
- * Ensure that neither of the group entities corresponding to src_cpu or
- * dest_cpu are members of a throttled hierarchy when performing group
- * load-balance operations.
- */
-static inline int throttled_lb_pair(struct task_group *tg,
-				    int src_cpu, int dest_cpu)
-{
-	struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
-
-	src_cfs_rq = tg->cfs_rq[src_cpu];
-	dest_cfs_rq = tg->cfs_rq[dest_cpu];
-
-	return throttled_hierarchy(src_cfs_rq) ||
-	       throttled_hierarchy(dest_cfs_rq);
-}
-
 static inline bool task_is_throttled(struct task_struct *p)
 {
 	return cfs_bandwidth_used() && p->throttled;
@@ -6743,12 +6726,6 @@ static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
 	return 0;
 }
 
-static inline int throttled_lb_pair(struct task_group *tg,
-				    int src_cpu, int dest_cpu)
-{
-	return 0;
-}
-
 #ifdef CONFIG_FAIR_GROUP_SCHED
 void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth *parent) {}
 static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
@@ -9385,18 +9362,14 @@ int can_migrate_task(struct task_struct *p, struct lb_env *env)
 	/*
 	 * We do not migrate tasks that are:
 	 * 1) delayed dequeued unless we migrate load, or
-	 * 2) throttled_lb_pair, or
-	 * 3) cannot be migrated to this CPU due to cpus_ptr, or
-	 * 4) running (obviously), or
-	 * 5) are cache-hot on their current CPU, or
-	 * 6) are blocked on mutexes (if SCHED_PROXY_EXEC is enabled)
+	 * 2) cannot be migrated to this CPU due to cpus_ptr, or
+	 * 3) running (obviously), or
+	 * 4) are cache-hot on their current CPU, or
+	 * 5) are blocked on mutexes (if SCHED_PROXY_EXEC is enabled)
 	 */
 	if ((p->se.sched_delayed) && (env->migration_type != migrate_load))
 		return 0;
 
-	if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
-		return 0;
-
 	/*
 	 * We want to prioritize the migration of eligible tasks.
 	 * For ineligible tasks we soft-limit them and only allow
Signature · 1 line
2.39.5
merge bot Applied
#38 lore raw

[tip: sched/core] sched/fair: Get rid of throttled_lb_pair()

The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     5b726e9bf9544a349090879a513a5e00da486c14
Gitweb:        https://git.kernel.org/tip/5b726e9bf9544a349090879a513a5e00da486c14
Author:        Aaron Lu <[email protected]>
AuthorDate:    Fri, 29 Aug 2025 16:11:20 +08:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Wed, 03 Sep 2025 10:03:14 +02:00

sched/fair: Get rid of throttled_lb_pair()

Now that throttled tasks are dequeued and can not stay on rq's cfs_tasks list, there is no need to take special care of these throttled tasks anymore in load balance.

---
 kernel/sched/fair.c | 35 ++++-------------------------------
 1 file changed, 4 insertions(+), 31 deletions(-)
kernel/sched/fair.c +4 -31
index bdc9bfa..df8dc38 100644
@@ -5735,23 +5735,6 @@ static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
 	return cfs_bandwidth_used() && cfs_rq->throttle_count;
 }
 
-/*
- * Ensure that neither of the group entities corresponding to src_cpu or
- * dest_cpu are members of a throttled hierarchy when performing group
- * load-balance operations.
- */
-static inline int throttled_lb_pair(struct task_group *tg,
-				    int src_cpu, int dest_cpu)
-{
-	struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
-
-	src_cfs_rq = tg->cfs_rq[src_cpu];
-	dest_cfs_rq = tg->cfs_rq[dest_cpu];
-
-	return throttled_hierarchy(src_cfs_rq) ||
-	       throttled_hierarchy(dest_cfs_rq);
-}
-
 static inline bool task_is_throttled(struct task_struct *p)
 {
 	return cfs_bandwidth_used() && p->throttled;
@@ -6743,12 +6726,6 @@ static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
 	return 0;
 }
 
-static inline int throttled_lb_pair(struct task_group *tg,
-				    int src_cpu, int dest_cpu)
-{
-	return 0;
-}
-
 #ifdef CONFIG_FAIR_GROUP_SCHED
 void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth *parent) {}
 static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
@@ -9385,18 +9362,14 @@ int can_migrate_task(struct task_struct *p, struct lb_env *env)
 	/*
 	 * We do not migrate tasks that are:
 	 * 1) delayed dequeued unless we migrate load, or
-	 * 2) throttled_lb_pair, or
-	 * 3) cannot be migrated to this CPU due to cpus_ptr, or
-	 * 4) running (obviously), or
-	 * 5) are cache-hot on their current CPU, or
-	 * 6) are blocked on mutexes (if SCHED_PROXY_EXEC is enabled)
+	 * 2) cannot be migrated to this CPU due to cpus_ptr, or
+	 * 3) running (obviously), or
+	 * 4) are cache-hot on their current CPU, or
+	 * 5) are blocked on mutexes (if SCHED_PROXY_EXEC is enabled)
 	 */
 	if ((p->se.sched_delayed) && (env->migration_type != migrate_load))
 		return 0;
 
-	if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
-		return 0;
-
 	/*
 	 * We want to prioritize the migration of eligible tasks.
 	 * For ineligible tasks we soft-limit them and only allow
#40 lore raw
In reply to Aaron Lu
Quoted 11 lines · Aaron Lu

v4:
- Add cfs_bandwidth_used() in task_is_throttled() and remove unlikely for task_is_throttled(), suggested by Valetin Schneider;
- Add a warn for non empty throttle_node in enqueue_throttled_task(), suggested by Valetin Schneider;
- Improve comments in enqueue_throttled_task() by Valetin Schneider;
- Clear throttled for to-be-unthrottled tasks in tg_unthrottle_up();
- Change throttled and pelt_clock_throttled fields in cfs_rq from int to bool, reported by LKP;
- Improve changelog for patch4 by Valetin Schneider.

Quoted 7 lines

Thanks a lot for all the reviews and tests, I hope I didn't miss any of them but if I do, please let me know. I've also run Jan's rt reproducer and songtang's stress test and didn't notice any problem.

Apply on top of sched/core, head commit 1b5f1454091e("sched/idle: Remove play_idle()").

Hi all,

as this all has arrived in 6.18 now - thanks for all the work - I would like to start a discussion about backporting this series - and some more related work, see below - to older stable releases. Especially PREEMPT_RT enabled systems are of interest as this series fixes a serious system freeze.

Has someone already looked into the backporting topic?

I can remember from the previous discussion that everything below 6.12 is hard, as scheduler internals have changed (EEVDF, vlag). Still, 6.12 would be valuable.

I have the following commits on my radar:

This series:

2cd571245b43 ("sched/fair: Add related data structure for task based throttle") 7fc2d1439247 ("sched/fair: Implement throttle task work and related helpers") e1fad12dcb66 ("sched/fair: Switch to task based throttle model") eb962f251fbb ("sched/fair: Task based throttle time accounting") 5b726e9bf954 ("sched/fair: Get rid of throttled_lb_pair()")

Follow up series:
https://lore.kernel.org/all/[email protected]/

fe8d238e646e ("sched/fair: Propagate load for throttled cfs_rq") fcd394866e3d ("sched/fair: update_cfs_group() for throttled cfs_rqs") 253b3f587241 ("sched/fair: Do not special case tasks in throttled hierarchy") 0d4eaf8caf8c ("sched/fair: Do not balance task to a throttled cfs_rq")

Another follow up:
https://lore.kernel.org/all/[email protected]/

956dfda6a708 ("sched/fair: Prevent cfs_rq from being unthrottled with zero runtime_remaining")

That should hopefully be enough, right?

Any concerns, additional thoughts, missing peaces? Please let me know!

Best regards,
Florian

Signature · 2 lines
Siemens AG, Foundational Technologies
Linux Expert Center
author
#41 lore raw
Quoted 44 lines · Florian
Quoted 11 lines · Aaron Lu

v4:
- Add cfs_bandwidth_used() in task_is_throttled() and remove unlikely for task_is_throttled(), suggested by Valetin Schneider;
- Add a warn for non empty throttle_node in enqueue_throttled_task(), suggested by Valetin Schneider;
- Improve comments in enqueue_throttled_task() by Valetin Schneider;
- Clear throttled for to-be-unthrottled tasks in tg_unthrottle_up();
- Change throttled and pelt_clock_throttled fields in cfs_rq from int to bool, reported by LKP;
- Improve changelog for patch4 by Valetin Schneider.

Quoted 7 lines

Thanks a lot for all the reviews and tests, I hope I didn't miss any of them but if I do, please let me know. I've also run Jan's rt reproducer and songtang's stress test and didn't notice any problem.

Apply on top of sched/core, head commit 1b5f1454091e("sched/idle: Remove play_idle()").

Hi all,

as this all has arrived in 6.18 now - thanks for all the work - I would like to start a discussion about backporting this series - and some more related work, see below - to older stable releases. Especially PREEMPT_RT enabled systems are of interest as this series fixes a serious system freeze.

Has someone already looked into the backporting topic?

I can remember from the previous discussion that everything below 6.12 is hard, as scheduler internals have changed (EEVDF, vlag). Still, 6.12 would be valuable.

I have the following commits on my radar:

This series:

2cd571245b43 ("sched/fair: Add related data structure for task based throttle") 7fc2d1439247 ("sched/fair: Implement throttle task work and related helpers") e1fad12dcb66 ("sched/fair: Switch to task based throttle model") eb962f251fbb ("sched/fair: Task based throttle time accounting") 5b726e9bf954 ("sched/fair: Get rid of throttled_lb_pair()")

Quoted 8 lines

Follow up series:
https://lore.kernel.org/all/[email protected]/

fe8d238e646e ("sched/fair: Propagate load for throttled cfs_rq") fcd394866e3d ("sched/fair: update_cfs_group() for throttled cfs_rqs") 253b3f587241 ("sched/fair: Do not special case tasks in throttled hierarchy") 0d4eaf8caf8c ("sched/fair: Do not balance task to a throttled cfs_rq")

There is one more fix before the next fix:
https://lore.kernel.org/all/[email protected]/

0e4a169d1a2b ("sched/fair: Start a cfs_rq on throttled hierarchy with PELT clock throttled")

Quoted 8 lines

Another follow up:
https://lore.kernel.org/all/[email protected]/

956dfda6a708 ("sched/fair: Prevent cfs_rq from being unthrottled with zero runtime_remaining")

That should hopefully be enough, right?

I think so.

Quoted 1 line

Any concerns, additional thoughts, missing peaces? Please let me know!

1 if the base does not have Josh's async unthrottle:
8ad075c2eb1f ("sched: Async unthrottling for cfs bandwidth"), make sure to backport that too or the distribute runtime timer handler can be time consuming.

2 if the base uses cfs, in dequeue_throttled_task(), the task's vruntime
has to be adjusted like below:

static void dequeue_throttled_task(struct task_struct *p, int flags)

{
	WARN_ON_ONCE(p->se.on_rq);
	list_del_init(&p->throttle_node);

	/* task blocked after throttled */
	if (flags & DEQUEUE_SLEEP)
		p->throttled = false;
	else {
		struct sched_entity *se = &p->se;
		struct cfs_rq *cfs_rq;

		/*
		 * We are leaving this cfs_rq but our vruntime is not
		 * normalized yet as that is only done for tasks dequeued
		 * with !DEQUEUE_SLEEP in dequeue_entity(), so we have to:
		 * Fix up our vruntime so that the current sleep doesn't
		 * cause 'unlimited' sleep bonus.
		 */
		cfs_rq = cfs_rq_of(se);
		place_entity(cfs_rq, se, 0);
		se->vruntime -= cfs_rq->min_vruntime;
	}
}

3 Also in this dequeue_throttled_task() function, if the base doesn't
have commit e1f078f50478("sched/fair: Combine detach into dequeue when migrating task"), then it's not necessary to do the following because migrate_task_rq_fair() have already dealed with that:

	/*
	 * task is migrating off its old cfs_rq, detach
	 * the task's load from its old cfs_rq.
	 */
	if (task_on_rq_migrating(p))
		detach_task_cfs_rq(p);

That's what I can think of right now.

I did a backport for 5.15 based kernel, I can probably post it somewhere if it is useful, just let me know.

#42 lore raw

On Tue Dec 2, 2025 at 10:43 AM CET, Aaron Lu wrote:

[snip]

Quoted 33 lines
Quoted 24 lines

Hi all,

as this all has arrived in 6.18 now - thanks for all the work - I would like to start a discussion about backporting this series - and some more related work, see below - to older stable releases. Especially PREEMPT_RT enabled systems are of interest as this series fixes a serious system freeze.

Has someone already looked into the backporting topic?

I can remember from the previous discussion that everything below 6.12 is hard, as scheduler internals have changed (EEVDF, vlag). Still, 6.12 would be valuable.

I have the following commits on my radar:

This series:

2cd571245b43 ("sched/fair: Add related data structure for task based throttle") 7fc2d1439247 ("sched/fair: Implement throttle task work and related helpers") e1fad12dcb66 ("sched/fair: Switch to task based throttle model") eb962f251fbb ("sched/fair: Task based throttle time accounting") 5b726e9bf954 ("sched/fair: Get rid of throttled_lb_pair()")

Quoted 8 lines

Follow up series:
https://lore.kernel.org/all/[email protected]/

fe8d238e646e ("sched/fair: Propagate load for throttled cfs_rq") fcd394866e3d ("sched/fair: update_cfs_group() for throttled cfs_rqs") 253b3f587241 ("sched/fair: Do not special case tasks in throttled hierarchy") 0d4eaf8caf8c ("sched/fair: Do not balance task to a throttled cfs_rq")

Quoted 6 lines

There is one more fix before the next fix:
https://lore.kernel.org/all/[email protected]/

0e4a169d1a2b ("sched/fair: Start a cfs_rq on throttled hierarchy with PELT clock throttled")

Thanks for the heads-up! Highly appreciated.

Quoted 9 lines
Quoted 8 lines

Another follow up:
https://lore.kernel.org/all/[email protected]/

956dfda6a708 ("sched/fair: Prevent cfs_rq from being unthrottled with zero runtime_remaining")

That should hopefully be enough, right?

Quoted 8 lines

I think so.

Quoted 1 line

Any concerns, additional thoughts, missing pieces? Please let me know!

1 if the base does not have Josh's async unthrottle:
8ad075c2eb1f ("sched: Async unthrottling for cfs bandwidth"), make sure to backport that too or the distribute runtime timer handler can be time consuming.

Thanks, noted.

Quoted 40 lines

2 if the base uses cfs, in dequeue_throttled_task(), the task's vruntime
has to be adjusted like below:

static void dequeue_throttled_task(struct task_struct *p, int flags)

{
	WARN_ON_ONCE(p->se.on_rq);
	list_del_init(&p->throttle_node);

	/* task blocked after throttled */
	if (flags & DEQUEUE_SLEEP)
		p->throttled = false;
	else {
		struct sched_entity *se = &p->se;
		struct cfs_rq *cfs_rq;

		/*
		 * We are leaving this cfs_rq but our vruntime is not
		 * normalized yet as that is only done for tasks dequeued
		 * with !DEQUEUE_SLEEP in dequeue_entity(), so we have to:
		 * Fix up our vruntime so that the current sleep doesn't
		 * cause 'unlimited' sleep bonus.
		 */
		cfs_rq = cfs_rq_of(se);
		place_entity(cfs_rq, se, 0);
		se->vruntime -= cfs_rq->min_vruntime;
	}
}

3 Also in this dequeue_throttled_task() function, if the base doesn't
have commit e1f078f50478("sched/fair: Combine detach into dequeue when migrating task"), then it's not necessary to do the following because migrate_task_rq_fair() have already dealed with that:

	/*
	 * task is migrating off its old cfs_rq, detach
	 * the task's load from its old cfs_rq.
	 */
	if (task_on_rq_migrating(p))
		detach_task_cfs_rq(p);
Quoted 4 lines

That's what I can think of right now.

I did a backport for 5.15 based kernel, I can probably post it somewhere if it is useful, just let me know.

So you backported the entire logic back to older releases already. Wow. Would 6.1 be possible as well, or any other blockers ahead? Asking because 6.1 is the baseline for the affected systems.

I think 6.12 should be doable with the patches mentioned above (your comments included). If your backport would help us to come down to 6.1 that would be even better.

author
#43 lore raw
Quoted 4 lines · Florian Bezdeka

So you backported the entire logic back to older releases already. Wow. Would 6.1 be possible as well, or any other blockers ahead? Asking because 6.1 is the baseline for the affected systems.

Yes I think 6.1 is possible.

Quoted 3 lines

I think 6.12 should be doable with the patches mentioned above (your comments included). If your backport would help us to come down to 6.1 that would be even better.

I attached the patches for 5.15 tree for your reference, hope it's useful.

Best wishes,
Aaron