[PATCH v6 0/3] Fix uclamp race against non atomic static key enable

Qais Yousef posted 3 patches 3 days, 8 hours ago
kernel/sched/core.c    | 49 +++++++++++++++---------------------------
kernel/sched/ext/ext.c |  6 ++----
kernel/sched/fair.c    |  6 ++----
kernel/sched/idle.c    | 19 ++++++++++++++++
kernel/sched/rt.c      | 10 +++++----
kernel/sched/sched.h   | 21 ++++++++++++++----
6 files changed, 63 insertions(+), 48 deletions(-)
[PATCH v6 0/3] Fix uclamp race against non atomic static key enable
Posted by Qais Yousef 3 days, 8 hours ago
Latest version drops the partial revert (patch 3 in v5) as RT group
throttling is going to be marked as broken and anyway a proper fix will
require non trivial rewrite how dequeue happens for throttled tasks or
full revert of subtracting nr_running added 10 years ago. Both of which
is not appealing and with the proposal to mark this feature as broken,
better leave it and hopefully just delete the problem area.

So in summary RT throttling is not fixed and being ignored. Fair
throttling is fixed by pairing uclamp_rq_inc/dec with
add/sub_nr_running. For RT, we do that in inc/dec_tasks() which are
called when tasks are actually enqueued/dequeued - as stated will fail
in RT throttling case. Given the minor severity of the problem and
effort required to restructure part of RT group throttling that is being
marked broken as we speak, I think this should do.

Changes since v5:
	- Drop patch 3 to partial revert subtracting rq->nr_running for
	  RT throttling.
	- Drop fixes tag from patch 1 as the series now is partially
	  fixed and not suitable for LTS.

Changes since v4:
	- Fix problems in fair/RT throttling logic that were exposed by
	  the original fix.

Changes since v3:
	- Fix Sashiko report about force_idle paths where
	  rq->nr_running is not 0 highlighted by Christian
	- Update patch 1 commit message to reflect jump_label call
	  sites on the same CPU might not be updated atomically too

Changes since v2:
	- Use uclamp_rq_is_idle() instead of scanning buckets to detect
	  imbalance
	- Do some cleanup on accessing the flag as an additional patch

Changes since v1:
	- Fix kernel test robot report due to missing static inline in dummy
	  definition of init_uclamp_rq()

Tested with the patch below to force imbalance via sched_feat. I could see the
warning appearing and frequencies spiking during imbalance. Correct behavior is
restored when sched_feat is disabled where the idle entry reset fixes things
up.

I had a mix of uclamp_max mainly used in the system via schedqosd.

	diff --git a/kernel/sched/core.c b/kernel/sched/core.c
	index a2035d66270b9..14f9f1e419e02 100644
	--- a/kernel/sched/core.c
	+++ b/kernel/sched/core.c
	@@ -1889,6 +1889,11 @@ static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p, int flags
		for_each_clamp_id(clamp_id)
			uclamp_rq_inc_id(rq, p, clamp_id);

	+       if (sched_feat(UCLAMP_IMBALANCED)) {
	+               for_each_clamp_id(clamp_id)
	+                       uclamp_rq_inc_id(rq, p, clamp_id);
	+       }
	+
		/* Reset clamp idle holding when there is one RUNNABLE task */
		if (uclamp_rq_is_idle(rq))
			uclamp_rq_clear_idle(rq);
	diff --git a/kernel/sched/features.h b/kernel/sched/features.h
	index 8f0dee8fc4755..b59e532500fd9 100644
	--- a/kernel/sched/features.h
	+++ b/kernel/sched/features.h
	@@ -142,3 +142,5 @@ SCHED_FEAT(LATENCY_WARN, false)
	  */
	 SCHED_FEAT(NI_RANDOM, true)
	 SCHED_FEAT(NI_RATE, true)
	+
	+SCHED_FEAT(UCLAMP_IMBALANCED, false)
	diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c
	index 84301fc49e699..dbc3f5e1a7e01 100644
	--- a/kernel/sched/idle.c
	+++ b/kernel/sched/idle.c
	@@ -519,7 +519,7 @@ static void set_next_task_idle(struct rq *rq, struct task_struct *next, enum snt
		 * are still queued.
		 */
		if (uclamp_is_used() &&
	-           !rq->nr_running && unlikely(!uclamp_rq_is_idle(rq)))
	+           !rq->nr_running && unlikely(WARN_ON(!uclamp_rq_is_idle(rq))))
			init_uclamp_rq(rq);
	 }

Qais Yousef (3):
  sched/uclamp: Fix race condition leading to imbalanced uclamp_rq
  sched/uclamp: Use wrappers instead of open coding UCLAMP_FLAG_IDLE
  sched/uclamp: Move uclamp_rq_{inc, dec}() from core.c

 kernel/sched/core.c    | 49 +++++++++++++++---------------------------
 kernel/sched/ext/ext.c |  6 ++----
 kernel/sched/fair.c    |  6 ++----
 kernel/sched/idle.c    | 19 ++++++++++++++++
 kernel/sched/rt.c      | 10 +++++----
 kernel/sched/sched.h   | 21 ++++++++++++++----
 6 files changed, 63 insertions(+), 48 deletions(-)

-- 
2.53.0
Re: [PATCH v6 0/3] Fix uclamp race against non atomic static key enable
Posted by Christian Loehle 1 day, 3 hours ago
On 11/21/26 04:15, Qais Yousef wrote:
> Latest version drops the partial revert (patch 3 in v5) as RT group
> throttling is going to be marked as broken and anyway a proper fix will
> require non trivial rewrite how dequeue happens for throttled tasks or
> full revert of subtracting nr_running added 10 years ago. Both of which
> is not appealing and with the proposal to mark this feature as broken,
> better leave it and hopefully just delete the problem area.
> 
> So in summary RT throttling is not fixed and being ignored. Fair
> throttling is fixed by pairing uclamp_rq_inc/dec with
> add/sub_nr_running. For RT, we do that in inc/dec_tasks() which are
> called when tasks are actually enqueued/dequeued - as stated will fail
> in RT throttling case. Given the minor severity of the problem and
> effort required to restructure part of RT group throttling that is being
> marked broken as we speak, I think this should do.
> 
> Changes since v5:
> 	- Drop patch 3 to partial revert subtracting rq->nr_running for
> 	  RT throttling.
> 	- Drop fixes tag from patch 1 as the series now is partially
> 	  fixed and not suitable for LTS.

Is this how this is done? I guess we could also NAK the backports?
Also presumably this was observed in live systems, so is not-backporting
an option?

> 
> Changes since v4:
> 	- Fix problems in fair/RT throttling logic that were exposed by
> 	  the original fix.
> 
> Changes since v3:
> 	- Fix Sashiko report about force_idle paths where
> 	  rq->nr_running is not 0 highlighted by Christian
> 	- Update patch 1 commit message to reflect jump_label call
> 	  sites on the same CPU might not be updated atomically too
> 
> Changes since v2:
> 	- Use uclamp_rq_is_idle() instead of scanning buckets to detect
> 	  imbalance
> 	- Do some cleanup on accessing the flag as an additional patch
> 
> Changes since v1:
> 	- Fix kernel test robot report due to missing static inline in dummy
> 	  definition of init_uclamp_rq()
> 
> Tested with the patch below to force imbalance via sched_feat. I could see the
> warning appearing and frequencies spiking during imbalance. Correct behavior is
> restored when sched_feat is disabled where the idle entry reset fixes things
> up.
> 
> I had a mix of uclamp_max mainly used in the system via schedqosd.
> 
> 	diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> 	index a2035d66270b9..14f9f1e419e02 100644
> 	--- a/kernel/sched/core.c
> 	+++ b/kernel/sched/core.c
> 	@@ -1889,6 +1889,11 @@ static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p, int flags
> 		for_each_clamp_id(clamp_id)
> 			uclamp_rq_inc_id(rq, p, clamp_id);
> 
> 	+       if (sched_feat(UCLAMP_IMBALANCED)) {
> 	+               for_each_clamp_id(clamp_id)
> 	+                       uclamp_rq_inc_id(rq, p, clamp_id);
> 	+       }
> 	+
> 		/* Reset clamp idle holding when there is one RUNNABLE task */
> 		if (uclamp_rq_is_idle(rq))
> 			uclamp_rq_clear_idle(rq);
> 	diff --git a/kernel/sched/features.h b/kernel/sched/features.h
> 	index 8f0dee8fc4755..b59e532500fd9 100644
> 	--- a/kernel/sched/features.h
> 	+++ b/kernel/sched/features.h
> 	@@ -142,3 +142,5 @@ SCHED_FEAT(LATENCY_WARN, false)
> 	  */
> 	 SCHED_FEAT(NI_RANDOM, true)
> 	 SCHED_FEAT(NI_RATE, true)
> 	+
> 	+SCHED_FEAT(UCLAMP_IMBALANCED, false)
> 	diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c
> 	index 84301fc49e699..dbc3f5e1a7e01 100644
> 	--- a/kernel/sched/idle.c
> 	+++ b/kernel/sched/idle.c
> 	@@ -519,7 +519,7 @@ static void set_next_task_idle(struct rq *rq, struct task_struct *next, enum snt
> 		 * are still queued.
> 		 */
> 		if (uclamp_is_used() &&
> 	-           !rq->nr_running && unlikely(!uclamp_rq_is_idle(rq)))
> 	+           !rq->nr_running && unlikely(WARN_ON(!uclamp_rq_is_idle(rq))))
> 			init_uclamp_rq(rq);
> 	 }
> 
> Qais Yousef (3):
>   sched/uclamp: Fix race condition leading to imbalanced uclamp_rq
>   sched/uclamp: Use wrappers instead of open coding UCLAMP_FLAG_IDLE
>   sched/uclamp: Move uclamp_rq_{inc, dec}() from core.c
> 
>  kernel/sched/core.c    | 49 +++++++++++++++---------------------------
>  kernel/sched/ext/ext.c |  6 ++----
>  kernel/sched/fair.c    |  6 ++----
>  kernel/sched/idle.c    | 19 ++++++++++++++++
>  kernel/sched/rt.c      | 10 +++++----
>  kernel/sched/sched.h   | 21 ++++++++++++++----
>  6 files changed, 63 insertions(+), 48 deletions(-)
> 

FWIW I could apply this series on base b99b7f60c5b4 ("sched: Replace nr_iowait_cpu() with a bool accessor")
but it would be nice to have this series rebased or base-commit specified,
even for sashiko reviews.
I'd be lying if I claimed all the interactions with idle/enqueue/dequeue
transitions and the edge-cases fit in my head...
Re: [PATCH v6 0/3] Fix uclamp race against non atomic static key enable
Posted by Qais Yousef 5 hours ago
On 11/23/26 09:19, Christian Loehle wrote:
> On 11/21/26 04:15, Qais Yousef wrote:
> > Latest version drops the partial revert (patch 3 in v5) as RT group
> > throttling is going to be marked as broken and anyway a proper fix will
> > require non trivial rewrite how dequeue happens for throttled tasks or
> > full revert of subtracting nr_running added 10 years ago. Both of which
> > is not appealing and with the proposal to mark this feature as broken,
> > better leave it and hopefully just delete the problem area.
> > 
> > So in summary RT throttling is not fixed and being ignored. Fair
> > throttling is fixed by pairing uclamp_rq_inc/dec with
> > add/sub_nr_running. For RT, we do that in inc/dec_tasks() which are
> > called when tasks are actually enqueued/dequeued - as stated will fail
> > in RT throttling case. Given the minor severity of the problem and
> > effort required to restructure part of RT group throttling that is being
> > marked broken as we speak, I think this should do.
> > 
> > Changes since v5:
> > 	- Drop patch 3 to partial revert subtracting rq->nr_running for
> > 	  RT throttling.
> > 	- Drop fixes tag from patch 1 as the series now is partially
> > 	  fixed and not suitable for LTS.
> 
> Is this how this is done? I guess we could also NAK the backports?
> Also presumably this was observed in live systems, so is not-backporting
> an option?

Is this the best way you could phrase your question? I asked clearly for
feedback on v5 and you kept quite. If you have such strong opinion, speak up
next team at the right moment instead of trolling like this.

FWIW except for RT group throttling breakage, patch 1 and
3 could go to stable. Not sure what would be the right fixes tag for patch 3.

FWIW I work with Wei who reported this and this is not a wide spread disaster
walking on earth. It has taken more effort than it is truly worth actually.
Re: [PATCH v6 0/3] Fix uclamp race against non atomic static key enable
Posted by Christian Loehle 5 hours ago
On 11/24/26 07:21, Qais Yousef wrote:
> On 11/23/26 09:19, Christian Loehle wrote:
>> On 11/21/26 04:15, Qais Yousef wrote:
>>> Latest version drops the partial revert (patch 3 in v5) as RT group
>>> throttling is going to be marked as broken and anyway a proper fix will
>>> require non trivial rewrite how dequeue happens for throttled tasks or
>>> full revert of subtracting nr_running added 10 years ago. Both of which
>>> is not appealing and with the proposal to mark this feature as broken,
>>> better leave it and hopefully just delete the problem area.
>>>
>>> So in summary RT throttling is not fixed and being ignored. Fair
>>> throttling is fixed by pairing uclamp_rq_inc/dec with
>>> add/sub_nr_running. For RT, we do that in inc/dec_tasks() which are
>>> called when tasks are actually enqueued/dequeued - as stated will fail
>>> in RT throttling case. Given the minor severity of the problem and
>>> effort required to restructure part of RT group throttling that is being
>>> marked broken as we speak, I think this should do.
>>>
>>> Changes since v5:
>>> 	- Drop patch 3 to partial revert subtracting rq->nr_running for
>>> 	  RT throttling.
>>> 	- Drop fixes tag from patch 1 as the series now is partially
>>> 	  fixed and not suitable for LTS.
>>
>> Is this how this is done? I guess we could also NAK the backports?
>> Also presumably this was observed in live systems, so is not-backporting
>> an option?
> 
> Is this the best way you could phrase your question? I asked clearly for
> feedback on v5 and you kept quite. If you have such strong opinion, speak up
> next team at the right moment instead of trolling like this.

Qais, I don't know which part of my phrasing came off badly here but it
clearly did.
The above questions are genuine, I was just wondering what the correct
way of 'fixes' are that we don't want backported (yet?), afaics the obvious
options are:
- Omit the fixes tag (like $SUBJECT does), but that obviously loses the
information that this is a fix.
- Keep the fixes tag, which most likely leads to stable AUTOSEL picking it
up, it lands in stable review and we can NAK it there to prevent backporting.

I'm not sure which one is preferred, that is why I phrased it as a question.
I don't have a strong opinion that is why I hadn't responded to v5.
That is all.

> 
> FWIW except for RT group throttling breakage, patch 1 and
> 3 could go to stable. Not sure what would be the right fixes tag for patch 3.
> 
> FWIW I work with Wei who reported this and this is not a wide spread disaster
> walking on earth. It has taken more effort than it is truly worth actually.
Re: [PATCH v6 0/3] Fix uclamp race against non atomic static key enable
Posted by Qais Yousef 4 hours ago
On 11/24/26 07:35, Christian Loehle wrote:
> On 11/24/26 07:21, Qais Yousef wrote:
> > On 11/23/26 09:19, Christian Loehle wrote:
> >> On 11/21/26 04:15, Qais Yousef wrote:
> >>> Latest version drops the partial revert (patch 3 in v5) as RT group
> >>> throttling is going to be marked as broken and anyway a proper fix will
> >>> require non trivial rewrite how dequeue happens for throttled tasks or
> >>> full revert of subtracting nr_running added 10 years ago. Both of which
> >>> is not appealing and with the proposal to mark this feature as broken,
> >>> better leave it and hopefully just delete the problem area.
> >>>
> >>> So in summary RT throttling is not fixed and being ignored. Fair
> >>> throttling is fixed by pairing uclamp_rq_inc/dec with
> >>> add/sub_nr_running. For RT, we do that in inc/dec_tasks() which are
> >>> called when tasks are actually enqueued/dequeued - as stated will fail
> >>> in RT throttling case. Given the minor severity of the problem and
> >>> effort required to restructure part of RT group throttling that is being
> >>> marked broken as we speak, I think this should do.
> >>>
> >>> Changes since v5:
> >>> 	- Drop patch 3 to partial revert subtracting rq->nr_running for
> >>> 	  RT throttling.
> >>> 	- Drop fixes tag from patch 1 as the series now is partially
> >>> 	  fixed and not suitable for LTS.
> >>
> >> Is this how this is done? I guess we could also NAK the backports?
> >> Also presumably this was observed in live systems, so is not-backporting
> >> an option?
> > 
> > Is this the best way you could phrase your question? I asked clearly for
> > feedback on v5 and you kept quite. If you have such strong opinion, speak up
> > next team at the right moment instead of trolling like this.
> 
> Qais, I don't know which part of my phrasing came off badly here but it
> clearly did.
> The above questions are genuine, I was just wondering what the correct
> way of 'fixes' are that we don't want backported (yet?), afaics the obvious
> options are:
> - Omit the fixes tag (like $SUBJECT does), but that obviously loses the
> information that this is a fix.
> - Keep the fixes tag, which most likely leads to stable AUTOSEL picking it
> up, it lands in stable review and we can NAK it there to prevent backporting.
> 
> I'm not sure which one is preferred, that is why I phrased it as a question.
> I don't have a strong opinion that is why I hadn't responded to v5.
> That is all.

Okay nevermind. It did read badly for me.

I have no idea how to deal with this tbh. This snowballed and sometimes you
just need to let go and move on. I doubt either way many (or any) would be
impacted badly.

Thanks.
Re: [PATCH v6 0/3] Fix uclamp race against non atomic static key enable
Posted by Qais Yousef 1 day, 19 hours ago
On 11/21/26 04:15, Qais Yousef wrote:

> So in summary RT throttling is not fixed and being ignored.

For the sake of completeness, the below patch (only compile tested) should fix
it. What I was tripping at before is that tasks don't get dequeued on
throttling, but only the group. This requires a bit of rework to walk the tasks
that are being dequeued and perform full dequeue instead. As stated in the
cover letter, given this is already being discussed for removal and being
marked as broken, not worth the effort. But in case we need to come back to it,
hope the below can remind us and give us head start.

--->8---

From 411ce77117253e5eac301b0acf67e266363c7630 Mon Sep 17 00:00:00 2001
From: Qais Yousef <qyousef@layalina.io>
Date: Sun, 1 Nov 2026 00:10:12 +0000
Subject: [RFC PATCH] sched/rt: Rework how rq->nr_running is manged

This partially reverts commit f4ebcbc0d7e009783256c9daf76bc4b90e645c14.

While trying to address uclamp static key race condition the throttling
logic in RT and fair has highlighted that both perform incomplete
dequeue since they don't go through core. RT logic seemed to diverge
from fair pattern where rq->nr_running was management oddly.

This is due to the fact that tasks are not really dequeued on
throttling, but only the group rt_se; leading to the oddity of how
nr_running is managed.

Bring the logic more inline with the rest of the classes by
adding/subtracting nr_running on inc/dec_tasks() if it is a task. And
add extra logic to walk the group and do rq accounting for nr_running
and uclamp in case of throttling. Less efficient, but more obvious and
robust.

Replace usage of rt_queued with rt_rq_throttled().

Signed-off-by: Qais Yousef <qyousef@layalina.io>
---
 kernel/sched/rt.c    | 141 ++++++++++++++++++++-----------------------
 kernel/sched/sched.h |  20 ++++--
 2 files changed, 80 insertions(+), 81 deletions(-)

diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
index fd116139ac9bf..b12f5502a4c54 100644
--- a/kernel/sched/rt.c
+++ b/kernel/sched/rt.c
@@ -82,12 +82,11 @@ void init_rt_rq(struct rt_rq *rt_rq)
 	rt_rq->highest_prio.next = MAX_RT_PRIO-1;
 	rt_rq->overloaded = 0;
 	plist_head_init(&rt_rq->pushable_tasks);
-	/* We start is dequeued state, because no RT tasks are queued */
-	rt_rq->rt_queued = 0;
 
 #ifdef CONFIG_RT_GROUP_SCHED
 	rt_rq->rt_time = 0;
 	rt_rq->rt_throttled = 0;
+	rt_rq->tasks_throttled = false;
 	rt_rq->rt_runtime = 0;
 	raw_spin_lock_init(&rt_rq->rt_runtime_lock);
 	rt_rq->tg = &root_task_group;
@@ -429,8 +428,6 @@ static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
 	}
 }
 
-static void enqueue_top_rt_rq(struct rt_rq *rt_rq);
-static void dequeue_top_rt_rq(struct rt_rq *rt_rq, unsigned int count);
 
 static inline int on_rt_rq(struct sched_rt_entity *rt_se)
 {
@@ -524,6 +521,37 @@ static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
 static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
 static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
 
+static void walk_rt_tasks(struct rt_rq *rt_rq, bool throttle)
+{
+	struct rq *rq = rq_of_rt_rq(rt_rq);
+	struct rt_prio_array *array = &rt_rq->active;
+	struct sched_rt_entity *rt_se;
+	int idx;
+
+	for_each_set_bit(idx, array->bitmap, MAX_RT_PRIO) {
+		struct list_head *queue = array->queue + idx;
+
+		list_for_each_entry(rt_se, queue, run_list) {
+			struct rt_rq *child_rt_rq = group_rt_rq(rt_se);
+
+			if (child_rt_rq) {
+				if (!child_rt_rq->tasks_throttled)
+					walk_rt_tasks(child_rt_rq, throttle);
+			} else {
+				struct task_struct *p = rt_task_of(rt_se);
+
+				if (throttle) {
+					sub_nr_running(rq, 1);
+					uclamp_rq_dec(rq, p);
+				} else {
+					add_nr_running(rq, 1);
+					uclamp_rq_inc(rq, p);
+				}
+			}
+		}
+	}
+}
+
 static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
 {
 	struct task_struct *donor = rq_of_rt_rq(rt_rq)->donor;
@@ -535,9 +563,12 @@ static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
 	rt_se = rt_rq->tg->rt_se[cpu];
 
 	if (rt_rq->rt_nr_running) {
-		if (!rt_se)
-			enqueue_top_rt_rq(rt_rq);
-		else if (!on_rt_rq(rt_se))
+		if (rt_rq->tasks_throttled) {
+			rt_rq->tasks_throttled = false;
+			walk_rt_tasks(rt_rq, false);
+		}
+
+		if (rt_se && !on_rt_rq(rt_se))
 			enqueue_rt_entity(rt_se, 0);
 
 		if (rt_rq->highest_prio.curr < donor->prio)
@@ -552,8 +583,12 @@ static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
 
 	rt_se = rt_rq->tg->rt_se[cpu];
 
+	if (!rt_rq->tasks_throttled) {
+		walk_rt_tasks(rt_rq, true);
+		rt_rq->tasks_throttled = true;
+	}
+
 	if (!rt_se) {
-		dequeue_top_rt_rq(rt_rq, rt_rq->rt_nr_running);
 		/* Kick cpufreq (see the comment in kernel/sched/sched.h). */
 		cpufreq_update_util(rq_of_rt_rq(rt_rq), 0);
 	}
@@ -561,11 +596,6 @@ static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
 		dequeue_rt_entity(rt_se, 0);
 }
 
-static inline int rt_rq_throttled(struct rt_rq *rt_rq)
-{
-	return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
-}
-
 static int rt_se_boosted(struct sched_rt_entity *rt_se)
 {
 	struct rt_rq *rt_rq = group_rt_rq(rt_se);
@@ -920,23 +950,12 @@ static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
 
 static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
 {
-	struct rq *rq = rq_of_rt_rq(rt_rq);
-
-	if (!rt_rq->rt_nr_running)
-		return;
-
-	enqueue_top_rt_rq(rt_rq);
-	resched_curr(rq);
+	if (rt_rq->rt_nr_running)
+		resched_curr(rq_of_rt_rq(rt_rq));
 }
 
 static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
 {
-	dequeue_top_rt_rq(rt_rq, rt_rq->rt_nr_running);
-}
-
-static inline int rt_rq_throttled(struct rt_rq *rt_rq)
-{
-	return false;
 }
 
 static inline const struct cpumask *sched_rt_period_mask(void)
@@ -1007,44 +1026,6 @@ static void update_curr_rt(struct rq *rq)
 #endif /* CONFIG_RT_GROUP_SCHED */
 }
 
-static void
-dequeue_top_rt_rq(struct rt_rq *rt_rq, unsigned int count)
-{
-	struct rq *rq = rq_of_rt_rq(rt_rq);
-
-	BUG_ON(&rq->rt != rt_rq);
-
-	if (!rt_rq->rt_queued)
-		return;
-
-	BUG_ON(!rq->nr_running);
-
-	sub_nr_running(rq, count);
-	rt_rq->rt_queued = 0;
-
-}
-
-static void
-enqueue_top_rt_rq(struct rt_rq *rt_rq)
-{
-	struct rq *rq = rq_of_rt_rq(rt_rq);
-
-	BUG_ON(&rq->rt != rt_rq);
-
-	if (rt_rq->rt_queued)
-		return;
-
-	if (rt_rq_throttled(rt_rq))
-		return;
-
-	if (rt_rq->rt_nr_running) {
-		add_nr_running(rq, rt_rq->rt_nr_running);
-		rt_rq->rt_queued = 1;
-	}
-
-	/* Kick cpufreq (see the comment in kernel/sched/sched.h). */
-	cpufreq_update_util(rq, 0);
-}
 
 static void
 inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
@@ -1171,6 +1152,19 @@ unsigned int rt_se_rr_nr_running(struct sched_rt_entity *rt_se)
 	return (tsk->policy == SCHED_RR) ? 1 : 0;
 }
 
+static inline bool rt_se_throttled(struct sched_rt_entity *rt_se)
+{
+#ifdef CONFIG_RT_GROUP_SCHED
+	for_each_sched_rt_entity(rt_se) {
+		struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
+
+		if (rt_rq->tasks_throttled)
+			return true;
+	}
+#endif
+	return false;
+}
+
 static inline
 void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
 {
@@ -1183,8 +1177,10 @@ void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
 	inc_rt_prio(rt_rq, prio);
 	inc_rt_group(rt_se, rt_rq);
 
-	if (rt_entity_is_task(rt_se))
+	if (rt_entity_is_task(rt_se)) {
+		add_nr_running(rq_of_rt_rq(rt_rq), 1);
 		uclamp_rq_inc(rq_of_rt_rq(rt_rq), rt_task_of(rt_se));
+	}
 }
 
 static inline
@@ -1198,8 +1194,10 @@ void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
 	dec_rt_prio(rt_rq, rt_se_prio(rt_se));
 	dec_rt_group(rt_se, rt_rq);
 
-	if (rt_entity_is_task(rt_se))
+	if (rt_entity_is_task(rt_se)) {
 		uclamp_rq_dec(rq_of_rt_rq(rt_rq), rt_task_of(rt_se));
+		sub_nr_running(rq_of_rt_rq(rt_rq), 1);
+	}
 }
 
 /*
@@ -1389,39 +1387,29 @@ static void __dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flag
 static void dequeue_rt_stack(struct sched_rt_entity *rt_se, unsigned int flags)
 {
 	struct sched_rt_entity *back = NULL;
-	unsigned int rt_nr_running;
 
 	for_each_sched_rt_entity(rt_se) {
 		rt_se->back = back;
 		back = rt_se;
 	}
 
-	rt_nr_running = rt_rq_of_se(back)->rt_nr_running;
-
 	for (rt_se = back; rt_se; rt_se = rt_se->back) {
 		if (on_rt_rq(rt_se))
 			__dequeue_rt_entity(rt_se, flags);
 	}
-
-	dequeue_top_rt_rq(rt_rq_of_se(back), rt_nr_running);
 }
 
 static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
 {
-	struct rq *rq = rq_of_rt_se(rt_se);
-
 	update_stats_enqueue_rt(rt_rq_of_se(rt_se), rt_se, flags);
 
 	dequeue_rt_stack(rt_se, flags);
 	for_each_sched_rt_entity(rt_se)
 		__enqueue_rt_entity(rt_se, flags);
-	enqueue_top_rt_rq(&rq->rt);
 }
 
 static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
 {
-	struct rq *rq = rq_of_rt_se(rt_se);
-
 	update_stats_dequeue_rt(rt_rq_of_se(rt_se), rt_se, flags);
 
 	dequeue_rt_stack(rt_se, flags);
@@ -1432,7 +1420,6 @@ static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
 		if (rt_rq && rt_rq->rt_nr_running)
 			__enqueue_rt_entity(rt_se, flags);
 	}
-	enqueue_top_rt_rq(&rq->rt);
 }
 
 /*
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 5bbed5ff2d957..f35ec835c6d68 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -872,8 +872,6 @@ struct rt_rq {
 	bool			overloaded;
 	struct plist_head	pushable_tasks;
 
-	int			rt_queued;
-
 #ifdef CONFIG_RT_GROUP_SCHED
 	int			rt_throttled;
 	u64			rt_time; /* consumed RT time, goes up in update_curr_rt */
@@ -883,6 +881,8 @@ struct rt_rq {
 
 	unsigned int		rt_nr_boosted;
 
+	bool			tasks_throttled;
+
 	struct rq		*rq; /* this is always top-level rq, cache? */
 #endif
 #ifdef CONFIG_CGROUP_SCHED
@@ -890,9 +890,21 @@ struct rt_rq {
 #endif
 };
 
+#ifdef CONFIG_RT_GROUP_SCHED
+static inline bool rt_rq_throttled(struct rt_rq *rt_rq)
+{
+	return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
+}
+#else
+static inline bool rt_rq_throttled(struct rt_rq *rt_rq)
+{
+	return false;
+}
+#endif
+
 static inline bool rt_rq_is_runnable(struct rt_rq *rt_rq)
 {
-	return rt_rq->rt_queued && rt_rq->rt_nr_running;
+	return !rt_rq_throttled(rt_rq) && rt_rq->rt_nr_running;
 }
 
 /* Deadline class' related fields in a runqueue */
@@ -2914,7 +2926,7 @@ static inline bool sched_dl_runnable(struct rq *rq)
 
 static inline bool sched_rt_runnable(struct rq *rq)
 {
-	return rq->rt.rt_queued > 0;
+	return rt_rq_is_runnable(&rq->rt);
 }
 
 static inline bool sched_fair_runnable(struct rq *rq)
-- 
2.53.0