[PATCH] sched/fair: Rework pick_task_fair() control flow

From: Yury Norov

Date: Wed Sep 09 2026 - 14:55:36 EST


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.

Signed-off-by: Yury Norov <ynorov@xxxxxxxxxx>
---
kernel/sched/fair.c | 51 ++++++++++++++++++++++++---------------------
1 file changed, 27 insertions(+), 24 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index ade1eceb39b8..ad0c477e58a8 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -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 *
--
2.53.0