Skip to contentSkip to conversation

sched: Clean up newidle_balance() and pick_next_task()

PATCH Superseded Chen Yu 3 participants 7 messages Apr 20, 2020 – Apr 21, 2020 Scheduler

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Chen Yu

author
#0 lore raw

v2 cover sched: Clean up newidle_balance() and pick_next_task()

As Peter suggested, here are two minor code clean up in the scheduler:
1. turn newidle_balance() into a static function.
2. code extraction in pick_next_task() for future re-use.

Chen Yu (2):
sched: Make newidle_balance() static again
sched: Extract the task putting code from pick_next_task()

 kernel/sched/core.c  | 39 +++++++++++++++++++++++----------------
 kernel/sched/fair.c  |  6 ++++--
 kernel/sched/sched.h |  4 ----
 3 files changed, 27 insertions(+), 22 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH v2 1/2 sched: Make newidle_balance() static again

After Commit 6e2df0581f56 ("sched: Fix pick_next_task() vs 'change' pattern race"), there is no need to expose newidle_balance() as it is only used within fair.c file. Change this function back to static again.

No functional change.

---
v2: Rename the remaining idle_balance() to newidle_balance()
to fix an compile error when CONFIG_SMP is not set.

---
 kernel/sched/fair.c  | 6 ++++--
 kernel/sched/sched.h | 4 ----
 2 files changed, 4 insertions(+), 6 deletions(-)
kernel/sched/fair.c +4 -2
index 02f323b85b6d..cca5c9b7b5ae 100644
@@ -3873,6 +3873,8 @@ static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq)
 	return cfs_rq->avg.load_avg;
 }
 
+static int newidle_balance(struct rq *this_rq, struct rq_flags *rf);
+
 static inline unsigned long task_util(struct task_struct *p)
 {
 	return READ_ONCE(p->se.avg.util_avg);
@@ -4054,7 +4056,7 @@ attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
 static inline void
 detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
 
-static inline int idle_balance(struct rq *rq, struct rq_flags *rf)
+static inline int newidle_balance(struct rq *rq, struct rq_flags *rf)
 {
 	return 0;
 }
@@ -10425,7 +10427,7 @@ static inline void nohz_newidle_balance(struct rq *this_rq) { }
  *     0 - failed, no new tasks
  *   > 0 - success, new (fair) tasks present
  */
-int newidle_balance(struct rq *this_rq, struct rq_flags *rf)
+static int newidle_balance(struct rq *this_rq, struct rq_flags *rf)
 {
 	unsigned long next_balance = jiffies + HZ;
 	int this_cpu = this_rq->cpu;
kernel/sched/sched.h +0 -4
index db3a57675ccf..be83f88495fb 100644
@@ -1504,14 +1504,10 @@ static inline void unregister_sched_domain_sysctl(void)
 }
 #endif
 
-extern int newidle_balance(struct rq *this_rq, struct rq_flags *rf);
-
 #else
 
 static inline void sched_ttwu_pending(void) { }
 
-static inline int newidle_balance(struct rq *this_rq, struct rq_flags *rf) { return 0; }
-
 #endif /* CONFIG_SMP */
 
 #include "stats.h"
Signature · 1 line
2.17.1
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH 1/2][v2] sched: Make newidle_balance() static again

Quoted 6 lines · Chen Yu

After Commit 6e2df0581f56 ("sched: Fix pick_next_task() vs 'change' pattern race"), there is no need to expose newidle_balance() as it is only used within fair.c file. Change this function back to static again.

No functional change.

That derelict 'idle_balance()' is a funny one.

Quoted 6 lines

---
v2: Rename the remaining idle_balance() to newidle_balance()
to fix an compile error when CONFIG_SMP is not set.

maintainer Reviewed-by on v2 1/2
#3 lore raw

Re: [PATCH 1/2][v2] sched: Make newidle_balance() static again

In reply to Chen Yu
Quoted 10 lines · Chen Yu

After Commit 6e2df0581f56 ("sched: Fix pick_next_task() vs 'change' pattern race"), there is no need to expose newidle_balance() as it is only used within fair.c file. Change this function back to static again.

No functional change.

Quoted 61 lines

---
v2: Rename the remaining idle_balance() to newidle_balance()
to fix an compile error when CONFIG_SMP is not set.

---
 kernel/sched/fair.c  | 6 ++++--
 kernel/sched/sched.h | 4 ----
 2 files changed, 4 insertions(+), 6 deletions(-)
kernel/sched/fair.c +4 -2
index 02f323b85b6d..cca5c9b7b5ae 100644
@@ -3873,6 +3873,8 @@ static inline unsigned long cfs_rq_load_avg(struct cfs_rq *cfs_rq)
        return cfs_rq->avg.load_avg;
 }
 
+static int newidle_balance(struct rq *this_rq, struct rq_flags *rf);
+
 static inline unsigned long task_util(struct task_struct *p)
 {
        return READ_ONCE(p->se.avg.util_avg);
@@ -4054,7 +4056,7 @@ attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
 static inline void
 detach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) {}
 
-static inline int idle_balance(struct rq *rq, struct rq_flags *rf)
+static inline int newidle_balance(struct rq *rq, struct rq_flags *rf)
 {
        return 0;
 }
@@ -10425,7 +10427,7 @@ static inline void nohz_newidle_balance(struct rq *this_rq) { }
  *     0 - failed, no new tasks
  *   > 0 - success, new (fair) tasks present
  */
-int newidle_balance(struct rq *this_rq, struct rq_flags *rf)
+static int newidle_balance(struct rq *this_rq, struct rq_flags *rf)
 {
        unsigned long next_balance = jiffies + HZ;
        int this_cpu = this_rq->cpu;
kernel/sched/sched.h +0 -5
index db3a57675ccf..be83f88495fb 100644
@@ -1504,14 +1504,10 @@ static inline void unregister_sched_domain_sysctl(void)
 }
 #endif
 
-extern int newidle_balance(struct rq *this_rq, struct rq_flags *rf);
-
 #else
 
 static inline void sched_ttwu_pending(void) { }
 
-static inline int newidle_balance(struct rq *this_rq, struct rq_flags *rf) { return 0; }
-
 #endif /* CONFIG_SMP */
 
 #include "stats.h"
--

2.17.1

author
#4 lore raw

PATCH v2 2/2 sched: Extract the task putting code from pick_next_task()

In reply to Chen Yu

Introduce a new function finish_prev_task() to do the balance when necessary, and then put previous task back to the run queue. This function is extracted from pick_next_task() to prepare for future usage by other type of task picking logic.

No functional change.

---
 kernel/sched/core.c | 39 +++++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 16 deletions(-)
kernel/sched/core.c +23 -16
index 3a61a3b8eaa9..bf59a5cf030c 100644
@@ -3904,6 +3904,28 @@ static inline void schedule_debug(struct task_struct *prev, bool preempt)
 	schedstat_inc(this_rq()->sched_count);
 }
 
+static void finish_prev_task(struct rq *rq, struct task_struct *prev,
+			     struct rq_flags *rf)
+{
+	const struct sched_class *class;
+#ifdef CONFIG_SMP
+	/*
+	 * We must do the balancing pass before put_next_task(), such
+	 * that when we release the rq->lock the task is in the same
+	 * state as before we took rq->lock.
+	 *
+	 * We can terminate the balance pass as soon as we know there is
+	 * a runnable task of @class priority or higher.
+	 */
+	for_class_range(class, prev->sched_class, &idle_sched_class) {
+		if (class->balance(rq, prev, rf))
+			break;
+	}
+#endif
+
+	put_prev_task(rq, prev);
+}
+
 /*
  * Pick up the highest-prio task:
  */
@@ -3937,22 +3959,7 @@ pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
 	}
 
 restart:
-#ifdef CONFIG_SMP
-	/*
-	 * We must do the balancing pass before put_next_task(), such
-	 * that when we release the rq->lock the task is in the same
-	 * state as before we took rq->lock.
-	 *
-	 * We can terminate the balance pass as soon as we know there is
-	 * a runnable task of @class priority or higher.
-	 */
-	for_class_range(class, prev->sched_class, &idle_sched_class) {
-		if (class->balance(rq, prev, rf))
-			break;
-	}
-#endif
-
-	put_prev_task(rq, prev);
+	finish_prev_task(rq, prev, rf);
 
 	for_each_class(class) {
 		p = class->pick_next_task(rq);
Signature · 1 line
2.17.1
Reviewed-by on v2 2/2
#5 lore raw

Re: [PATCH 2/2][v2] sched: Extract the task putting code from pick_next_task()

Quoted 7 lines · Chen Yu

Introduce a new function finish_prev_task() to do the balance when necessary, and then put previous task back to the run queue. This function is extracted from pick_next_task() to prepare for future usage by other type of task picking logic.

No functional change.

With the absolutely tiny nit below:

Quoted 11 lines
---
 kernel/sched/core.c | 39 +++++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 16 deletions(-)
kernel/sched/core.c +0 -0
index 3a61a3b8eaa9..bf59a5cf030c 100644

kernel/sched/core.c static inline void schedule_debug(struct task_struct *prev, bool preempt) · line 3911

Quoted 8 lines
kernel/sched/core.c +5 -0
@@ -3904,6 +3904,28 @@ static inline void schedule_debug(struct task_struct *prev, bool preempt)
      schedstat_inc(this_rq()->sched_count);
 }
 
+static void finish_prev_task(struct rq *rq, struct task_struct *prev,
+			     struct rq_flags *rf)
+{
+	const struct sched_class *class;
+#ifdef CONFIG_SMP

Nit: I would've put that declaration within the ifdef, given it isn't used outside of it.

Quoted 20 lines
kernel/sched/core.c +17 -0
+	/*
+	 * We must do the balancing pass before put_next_task(), such
+	 * that when we release the rq->lock the task is in the same
+	 * state as before we took rq->lock.
+	 *
+	 * We can terminate the balance pass as soon as we know there is
+	 * a runnable task of @class priority or higher.
+	 */
+	for_class_range(class, prev->sched_class, &idle_sched_class) {
+		if (class->balance(rq, prev, rf))
+			break;
+	}
+#endif
+
+	put_prev_task(rq, prev);
+}
+
 /*
  * Pick up the highest-prio task:
  */
author
#6 lore raw

Re: [PATCH 2/2][v2] sched: Extract the task putting code from pick_next_task()

Hi,

Quoted 28 lines · Valentin Schneider
Quoted 7 lines · Chen Yu

Introduce a new function finish_prev_task() to do the balance when necessary, and then put previous task back to the run queue. This function is extracted from pick_next_task() to prepare for future usage by other type of task picking logic.

No functional change.

With the absolutely tiny nit below:

Quoted 14 lines
---
 kernel/sched/core.c | 39 +++++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 16 deletions(-)
kernel/sched/core.c +0 -0
index 3a61a3b8eaa9..bf59a5cf030c 100644
@@ -3904,6 +3904,28 @@ static inline void schedule_debug(struct task_struct *prev, bool preempt)
      schedstat_inc(this_rq()->sched_count);
 }
 
Quoted 9 lines

kernel/sched/core.c static inline void schedule_debug(struct task_struct *prev, bool preempt) · line 3911

Quoted 5 lines
kernel/sched/core.c +5 -0
+static void finish_prev_task(struct rq *rq, struct task_struct *prev,
+			     struct rq_flags *rf)
+{
+	const struct sched_class *class;
+#ifdef CONFIG_SMP

Nit: I would've put that declaration within the ifdef, given it isn't used outside of it.

Okay, I'll do.

Thanks,
Chenyu