Skip to contentSkip to conversation

sched/fair: Make SCHED_IDLE se be preempted in strict hierarchy

PATCH Superseded Tianchen Ding 3 participants 3 messages Jun 24, 2024 – Jun 25, 2024 Scheduler

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched/fair: Make SCHED_IDLE se be preempted in strict hierarchy

Consider the following cgroup:

                       root
                        |
             ------------------------
             |                      |
       normal_cgroup            idle_cgroup
             |                      |

SCHED_IDLE task_A SCHED_NORMAL task_B

According to the cgroup hierarchy, A should preempt B. But current check_preempt_wakeup_fair() treats cgroup se and task separately, so B will preempt A unexpectedly.
Unify the wakeup logic by {p}se_is_idle only.

Also fix a bug about se_is_idle() definition when
!CONFIG_FAIR_GROUP_SCHED.

Fixes 304000390f88 ("sched: Cgroup SCHED_IDLE support")
---
 kernel/sched/fair.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)
kernel/sched/fair.c +8 -11
index 41b58387023d..c91cfaa7d9ee 100644
@@ -511,7 +511,7 @@ static int cfs_rq_is_idle(struct cfs_rq *cfs_rq)
 
 static int se_is_idle(struct sched_entity *se)
 {
-	return 0;
+	return task_has_idle_policy(task_of(se));
 }
 
 #endif	/* CONFIG_FAIR_GROUP_SCHED */
@@ -8382,16 +8382,7 @@ static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int
 	if (test_tsk_need_resched(curr))
 		return;
 
-	/* Idle tasks are by definition preempted by non-idle tasks. */
-	if (unlikely(task_has_idle_policy(curr)) &&
-	    likely(!task_has_idle_policy(p)))
-		goto preempt;
-
-	/*
-	 * Batch and idle tasks do not preempt non-idle tasks (their preemption
-	 * is driven by the tick):
-	 */
-	if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION))
+	if (!sched_feat(WAKEUP_PREEMPTION))
 		return;
 
 	find_matching_se(&se, &pse);
@@ -8408,6 +8399,12 @@ static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int
 		goto preempt;
 	if (cse_is_idle != pse_is_idle)
 		return;
+	/*
+	 * Batch tasks do not preempt non-idle tasks (their preemption
+	 * is driven by the tick):
+	 */
+	if (unlikely(pse == &p->se && p->policy == SCHED_BATCH))
+		return;
 
 	cfs_rq = cfs_rq_of(se);
 	update_curr(cfs_rq);
Signature · 1 line
2.39.3
Reviewed-by on v1 1/1
#1 lore raw

Hi Tianchen,

Thanks for these fixes.

Quoted 20 lines

#endif /* CONFIG_FAIR_GROUP_SCHED */

kernel/sched/fair.c +1 -10
@@ -8382,16 +8382,7 @@ static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int
        if (test_tsk_need_resched(curr))
                return;
 
-       /* Idle tasks are by definition preempted by non-idle tasks. */
-       if (unlikely(task_has_idle_policy(curr)) &&
-           likely(!task_has_idle_policy(p)))
-               goto preempt;
-
-       /*
-        * Batch and idle tasks do not preempt non-idle tasks (their preemption
-        * is driven by the tick):
-        */
-       if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION))
+       if (!sched_feat(WAKEUP_PREEMPTION))
                return;
 
        find_matching_se(&se, &pse);

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

Quoted 3 lines
kernel/sched/fair.c +0 -0
@@ -8408,6 +8399,12 @@ static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int
                goto preempt;
        if (cse_is_idle != pse_is_idle)
                return;

nit: add newline

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

Quoted 6 lines
kernel/sched/fair.c +6 -0
+       /*
+        * Batch tasks do not preempt non-idle tasks (their preemption
+        * is driven by the tick):
+        */
+       if (unlikely(pse == &p->se && p->policy == SCHED_BATCH))
+               return;

I think it is worth extending that comment to explain why we don't also check policy for SCHED_IDLE here (since the cse_is_idle checks above boil down to checking for task idle policy in this special case where pse is a task entity). The comment above the pse_is_idle checks only talks about idle group preemption, when here we're relying on it for idle task preemption.

Quoted 6 lines
kernel/sched/fair.c +0 -1
        cfs_rq = cfs_rq_of(se);
        update_curr(cfs_rq);
--

2.39.3

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Tianchen Ding

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

Quoted 56 lines · Tianchen Ding

Consider the following cgroup:

                       root
                        |
             ------------------------
             |                      |
       normal_cgroup            idle_cgroup
             |                      |

SCHED_IDLE task_A SCHED_NORMAL task_B

According to the cgroup hierarchy, A should preempt B. But current check_preempt_wakeup_fair() treats cgroup se and task separately, so B will preempt A unexpectedly.
Unify the wakeup logic by {p}se_is_idle only.

Also fix a bug about se_is_idle() definition when
!CONFIG_FAIR_GROUP_SCHED.

Fixes 304000390f88 ("sched: Cgroup SCHED_IDLE support")
---
 kernel/sched/fair.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)
kernel/sched/fair.c +2 -11
index 41b58387023d..c91cfaa7d9ee 100644
@@ -511,7 +511,7 @@ static int cfs_rq_is_idle(struct cfs_rq *cfs_rq)
 
 static int se_is_idle(struct sched_entity *se)
 {
-       return 0;
+       return task_has_idle_policy(task_of(se));
 }
 
 #endif /* CONFIG_FAIR_GROUP_SCHED */
@@ -8382,16 +8382,7 @@ static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int
        if (test_tsk_need_resched(curr))
                return;
 
-       /* Idle tasks are by definition preempted by non-idle tasks. */
-       if (unlikely(task_has_idle_policy(curr)) &&
-           likely(!task_has_idle_policy(p)))
-               goto preempt;
-
-       /*
-        * Batch and idle tasks do not preempt non-idle tasks (their preemption
-        * is driven by the tick):
-        */
-       if (unlikely(p->policy != SCHED_NORMAL) || !sched_feat(WAKEUP_PREEMPTION))
+       if (!sched_feat(WAKEUP_PREEMPTION))
                return;
 
        find_matching_se(&se, &pse);
@@ -8408,6 +8399,12 @@ static void check_preempt_wakeup_fair(struct rq *rq, struct task_struct *p, int

Replace "group" by "entity" in the comment above as it is not only group but also entity

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

Quoted 8 lines
kernel/sched/fair.c +5 -0
                goto preempt;
        if (cse_is_idle != pse_is_idle)
                return;
+       /*
+        * Batch tasks do not preempt non-idle tasks (their preemption
+        * is driven by the tick):
+        */
+       if (unlikely(pse == &p->se && p->policy == SCHED_BATCH))

I think I would prefer entity_is_task() which makes easier to understand that the condition is about task

+ if (unlikely(entity_is_task(pse) && p->policy == SCHED_BATCH))

other than the 2 comments above

Quoted 7 lines
kernel/sched/fair.c +1 -1
+               return;
 
        cfs_rq = cfs_rq_of(se);
        update_curr(cfs_rq);
--

2.39.3