Skip to content

Commit f9fc8ca

Browse files
author
Peter Zijlstra
committed
sched: Add TASK_ANY for wait_task_inactive()
Now that wait_task_inactive()'s @match_state argument is a mask (like ttwu()) it is possible to replace the special !match_state case with an 'all-states' value such that any blocked state will match. Suggested-by: Ingo Molnar (mingo@kernel.org> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lkml.kernel.org/r/YxhkzfuFTvRnpUaH@hirez.programming.kicks-ass.net
1 parent 9204a97 commit f9fc8ca

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

drivers/powercap/idle_inject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ void idle_inject_stop(struct idle_inject_device *ii_dev)
254254
iit = per_cpu_ptr(&idle_inject_thread, cpu);
255255
iit->should_run = 0;
256256

257-
wait_task_inactive(iit->tsk, 0);
257+
wait_task_inactive(iit->tsk, TASK_ANY);
258258
}
259259

260260
cpu_hotplug_enable();

fs/coredump.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ static int coredump_wait(int exit_code, struct core_state *core_state)
412412
*/
413413
ptr = core_state->dumper.next;
414414
while (ptr != NULL) {
415-
wait_task_inactive(ptr->task, 0);
415+
wait_task_inactive(ptr->task, TASK_ANY);
416416
ptr = ptr->next;
417417
}
418418
}

include/linux/sched.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ struct task_group;
101101
#define TASK_RTLOCK_WAIT 0x1000
102102
#define TASK_STATE_MAX 0x2000
103103

104+
#define TASK_ANY (TASK_STATE_MAX-1)
105+
104106
/* Convenience macros for the sake of set_current_state: */
105107
#define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
106108
#define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED)

kernel/sched/core.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3253,12 +3253,12 @@ int migrate_swap(struct task_struct *cur, struct task_struct *p,
32533253
/*
32543254
* wait_task_inactive - wait for a thread to unschedule.
32553255
*
3256-
* If @match_state is nonzero, it's the @p->state value just checked and
3257-
* not expected to change. If it changes, i.e. @p might have woken up,
3258-
* then return zero. When we succeed in waiting for @p to be off its CPU,
3259-
* we return a positive number (its total switch count). If a second call
3260-
* a short while later returns the same number, the caller can be sure that
3261-
* @p has remained unscheduled the whole time.
3256+
* Wait for the thread to block in any of the states set in @match_state.
3257+
* If it changes, i.e. @p might have woken up, then return zero. When we
3258+
* succeed in waiting for @p to be off its CPU, we return a positive number
3259+
* (its total switch count). If a second call a short while later returns the
3260+
* same number, the caller can be sure that @p has remained unscheduled the
3261+
* whole time.
32623262
*
32633263
* The caller must ensure that the task *will* unschedule sometime soon,
32643264
* else this function might spin for a *long* time. This function can't
@@ -3294,7 +3294,7 @@ unsigned long wait_task_inactive(struct task_struct *p, unsigned int match_state
32943294
* is actually now running somewhere else!
32953295
*/
32963296
while (task_on_cpu(rq, p)) {
3297-
if (match_state && !(READ_ONCE(p->__state) & match_state))
3297+
if (!(READ_ONCE(p->__state) & match_state))
32983298
return 0;
32993299
cpu_relax();
33003300
}
@@ -3309,7 +3309,7 @@ unsigned long wait_task_inactive(struct task_struct *p, unsigned int match_state
33093309
running = task_on_cpu(rq, p);
33103310
queued = task_on_rq_queued(p);
33113311
ncsw = 0;
3312-
if (!match_state || (READ_ONCE(p->__state) & match_state))
3312+
if (READ_ONCE(p->__state) & match_state)
33133313
ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
33143314
task_rq_unlock(rq, p, &rf);
33153315

0 commit comments

Comments
 (0)