Skip to contentSkip to conversation

sched/fair: Rework pick_task_fair() control flow

PATCH In review Yury Norov 2 participants 3 messages Sep 9 – Nov 18 Scheduler

lore
Share & tools
lore

Yury Norov

author
#0 lore raw

PATCH 1/1 sched/fair: Rework pick_task_fair() control flow

Split out the logic picking a task from rq to a separate helper, and get rid of the gotos.

With GCC 15.2.0 and x86_64_defconfig, the new version saves 96 bytes, and with defconfig + CONFIG_SCHED_CORE + CONFIG_CFS_BANDWIDTH it saves 124 bytes.

No functional changes intended.

---
 kernel/sched/fair.c | 51 ++++++++++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 24 deletions(-)
kernel/sched/fair.c +27 -24
index ade1eceb39b8..ad0c477e58a8 100644
@@ -10043,39 +10043,42 @@ static void wakeup_preempt_fair(struct rq *rq, struct task_struct *p, int wake_f
 	resched_curr_lazy(rq);
 }
 
-struct task_struct *pick_task_fair(struct rq *rq, struct rq_flags *rf)
-	__must_hold(__rq_lockp(rq))
+static struct task_struct *pick_task_fair_rq(struct rq *rq)
 {
 	struct cfs_rq *cfs_rq = &rq->cfs;
 	struct sched_entity *se;
-	struct task_struct *p;
-	int new_tasks;
 
-again:
-	if (!cfs_rq->h_nr_queued)
-		goto idle;
+	while (cfs_rq->h_nr_queued) {
+		/* Might not have done put_prev_entity() */
+		if (cfs_rq->curr && cfs_rq->curr->on_rq)
+			update_curr_eevdf(cfs_rq);
 
-	/* Might not have done put_prev_entity() */
-	if (cfs_rq->curr && cfs_rq->curr->on_rq)
-		update_curr_eevdf(cfs_rq);
+		se = pick_next_entity(rq, true);
+		if (se)
+			return task_of(se);
+	}
 
-	se = pick_next_entity(rq, true);
-	if (!se)
-		goto again;
+	return NULL;
+}
 
-	p = task_of(se);
-	return p;
+struct task_struct *pick_task_fair(struct rq *rq, struct rq_flags *rf)
+	__must_hold(__rq_lockp(rq))
+{
+	struct task_struct *p;
+	int new_tasks;
 
-idle:
-	if (sched_core_enabled(rq))
-		return NULL;
+	do {
+		p = pick_task_fair_rq(rq);
+		if (p)
+			return p;
 
-	new_tasks = sched_balance_newidle(rq, rf);
-	if (new_tasks < 0)
-		return RETRY_TASK;
-	if (new_tasks > 0)
-		goto again;
-	return NULL;
+		if (sched_core_enabled(rq))
+			return NULL;
+
+		new_tasks = sched_balance_newidle(rq, rf);
+	} while (new_tasks > 0);
+
+	return new_tasks ? RETRY_TASK : NULL;
 }
 
 static struct task_struct *
Signature · 1 line
2.53.0
#1 lore raw

Ping?

Quoted 87 lines · Yury Norov

Split out the logic picking a task from rq to a separate helper, and get rid of the gotos.

With GCC 15.2.0 and x86_64_defconfig, the new version saves 96 bytes, and with defconfig + CONFIG_SCHED_CORE + CONFIG_CFS_BANDWIDTH it saves 124 bytes.

No functional changes intended.

---
 kernel/sched/fair.c | 51 ++++++++++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 24 deletions(-)
kernel/sched/fair.c +27 -24
index ade1eceb39b8..ad0c477e58a8 100644
@@ -10043,39 +10043,42 @@ static void wakeup_preempt_fair(struct rq *rq, struct task_struct *p, int wake_f
 	resched_curr_lazy(rq);
 }
 
-struct task_struct *pick_task_fair(struct rq *rq, struct rq_flags *rf)
-	__must_hold(__rq_lockp(rq))
+static struct task_struct *pick_task_fair_rq(struct rq *rq)
 {
 	struct cfs_rq *cfs_rq = &rq->cfs;
 	struct sched_entity *se;
-	struct task_struct *p;
-	int new_tasks;
 
-again:
-	if (!cfs_rq->h_nr_queued)
-		goto idle;
+	while (cfs_rq->h_nr_queued) {
+		/* Might not have done put_prev_entity() */
+		if (cfs_rq->curr && cfs_rq->curr->on_rq)
+			update_curr_eevdf(cfs_rq);
 
-	/* Might not have done put_prev_entity() */
-	if (cfs_rq->curr && cfs_rq->curr->on_rq)
-		update_curr_eevdf(cfs_rq);
+		se = pick_next_entity(rq, true);
+		if (se)
+			return task_of(se);
+	}
 
-	se = pick_next_entity(rq, true);
-	if (!se)
-		goto again;
+	return NULL;
+}
 
-	p = task_of(se);
-	return p;
+struct task_struct *pick_task_fair(struct rq *rq, struct rq_flags *rf)
+	__must_hold(__rq_lockp(rq))
+{
+	struct task_struct *p;
+	int new_tasks;
 
-idle:
-	if (sched_core_enabled(rq))
-		return NULL;
+	do {
+		p = pick_task_fair_rq(rq);
+		if (p)
+			return p;
 
-	new_tasks = sched_balance_newidle(rq, rf);
-	if (new_tasks < 0)
-		return RETRY_TASK;
-	if (new_tasks > 0)
-		goto again;
-	return NULL;
+		if (sched_core_enabled(rq))
+			return NULL;
+
+		new_tasks = sched_balance_newidle(rq, rf);
+	} while (new_tasks > 0);
+
+	return new_tasks ? RETRY_TASK : NULL;
 }
 
 static struct task_struct *
Signature · 1 line
2.53.0
#2 lore raw

Ping again

Quoted 90 lines · Yury Norov

Ping?

Quoted 87 lines · Yury Norov

Split out the logic picking a task from rq to a separate helper, and get rid of the gotos.

With GCC 15.2.0 and x86_64_defconfig, the new version saves 96 bytes, and with defconfig + CONFIG_SCHED_CORE + CONFIG_CFS_BANDWIDTH it saves 124 bytes.

No functional changes intended.

---
 kernel/sched/fair.c | 51 ++++++++++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 24 deletions(-)
kernel/sched/fair.c +27 -24
index ade1eceb39b8..ad0c477e58a8 100644
@@ -10043,39 +10043,42 @@ static void wakeup_preempt_fair(struct rq *rq, struct task_struct *p, int wake_f
 	resched_curr_lazy(rq);
 }
 
-struct task_struct *pick_task_fair(struct rq *rq, struct rq_flags *rf)
-	__must_hold(__rq_lockp(rq))
+static struct task_struct *pick_task_fair_rq(struct rq *rq)
 {
 	struct cfs_rq *cfs_rq = &rq->cfs;
 	struct sched_entity *se;
-	struct task_struct *p;
-	int new_tasks;
 
-again:
-	if (!cfs_rq->h_nr_queued)
-		goto idle;
+	while (cfs_rq->h_nr_queued) {
+		/* Might not have done put_prev_entity() */
+		if (cfs_rq->curr && cfs_rq->curr->on_rq)
+			update_curr_eevdf(cfs_rq);
 
-	/* Might not have done put_prev_entity() */
-	if (cfs_rq->curr && cfs_rq->curr->on_rq)
-		update_curr_eevdf(cfs_rq);
+		se = pick_next_entity(rq, true);
+		if (se)
+			return task_of(se);
+	}
 
-	se = pick_next_entity(rq, true);
-	if (!se)
-		goto again;
+	return NULL;
+}
 
-	p = task_of(se);
-	return p;
+struct task_struct *pick_task_fair(struct rq *rq, struct rq_flags *rf)
+	__must_hold(__rq_lockp(rq))
+{
+	struct task_struct *p;
+	int new_tasks;
 
-idle:
-	if (sched_core_enabled(rq))
-		return NULL;
+	do {
+		p = pick_task_fair_rq(rq);
+		if (p)
+			return p;
 
-	new_tasks = sched_balance_newidle(rq, rf);
-	if (new_tasks < 0)
-		return RETRY_TASK;
-	if (new_tasks > 0)
-		goto again;
-	return NULL;
+		if (sched_core_enabled(rq))
+			return NULL;
+
+		new_tasks = sched_balance_newidle(rq, rf);
+	} while (new_tasks > 0);
+
+	return new_tasks ? RETRY_TASK : NULL;
 }
 
 static struct task_struct *
Signature · 1 line
2.53.0