]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
sched: Rework block_task so it can be directly called
authorJohn Stultz <jstultz@google.com>
Tue, 12 May 2026 02:56:14 +0000 (02:56 +0000)
committerPeter Zijlstra <peterz@infradead.org>
Tue, 2 Jun 2026 10:26:06 +0000 (12:26 +0200)
Pull most of the logic out of try_to_block_task() and put it
into block_task() directly, so that we can call block_task() and
not have to worry about the failing cases in try_to_block_task()

Suggested-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: John Stultz <jstultz@google.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/20260512025635.2840817-5-jstultz@google.com
kernel/sched/core.c

index a9c9b89260cd44a5ef103522901cc5479e970d16..2f1e85d09b94f692d5d7675d1b011244b4d82360 100644 (file)
@@ -2236,8 +2236,29 @@ void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
        dequeue_task(rq, p, flags);
 }
 
-static void block_task(struct rq *rq, struct task_struct *p, int flags)
+static void block_task(struct rq *rq, struct task_struct *p, unsigned long task_state)
 {
+       int flags = DEQUEUE_NOCLOCK;
+
+       p->sched_contributes_to_load =
+               (task_state & TASK_UNINTERRUPTIBLE) &&
+               !(task_state & TASK_NOLOAD) &&
+               !(task_state & TASK_FROZEN);
+
+       if (unlikely(is_special_task_state(task_state)))
+               flags |= DEQUEUE_SPECIAL;
+
+       /*
+        * __schedule()                 ttwu()
+        *   prev_state = prev->state;    if (p->on_rq && ...)
+        *   if (prev_state)                goto out;
+        *     p->on_rq = 0;              smp_acquire__after_ctrl_dep();
+        *                                p->state = TASK_WAKING
+        *
+        * Where __schedule() and ttwu() have matching control dependencies.
+        *
+        * After this, schedule() must not care about p->state any more.
+        */
        if (dequeue_task(rq, p, DEQUEUE_SLEEP | flags))
                __block_task(rq, p);
 }
@@ -6587,7 +6608,6 @@ static bool try_to_block_task(struct rq *rq, struct task_struct *p,
                              unsigned long *task_state_p, bool should_block)
 {
        unsigned long task_state = *task_state_p;
-       int flags = DEQUEUE_NOCLOCK;
 
        if (signal_pending_state(task_state, p)) {
                WRITE_ONCE(p->__state, TASK_RUNNING);
@@ -6607,26 +6627,7 @@ static bool try_to_block_task(struct rq *rq, struct task_struct *p,
        if (!should_block)
                return false;
 
-       p->sched_contributes_to_load =
-               (task_state & TASK_UNINTERRUPTIBLE) &&
-               !(task_state & TASK_NOLOAD) &&
-               !(task_state & TASK_FROZEN);
-
-       if (unlikely(is_special_task_state(task_state)))
-               flags |= DEQUEUE_SPECIAL;
-
-       /*
-        * __schedule()                 ttwu()
-        *   prev_state = prev->state;    if (p->on_rq && ...)
-        *   if (prev_state)                goto out;
-        *     p->on_rq = 0;              smp_acquire__after_ctrl_dep();
-        *                                p->state = TASK_WAKING
-        *
-        * Where __schedule() and ttwu() have matching control dependencies.
-        *
-        * After this, schedule() must not care about p->state any more.
-        */
-       block_task(rq, p, flags);
+       block_task(rq, p, task_state);
        return true;
 }