Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kernel/sched: Fix preemption logic #8077

Merged
merged 1 commit into from
May 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
kernel/sched: Fix preemption logic
The should_preempt() code was catching some of the "unrunnable" cases
but not all of them, opening the possibility of failing to preempt a
just-pended thread and thus waking it up synchronously.  There are
reports of this causing spin loops over k_poll() in the network stack
work queues (see #8049).

Note that the previous _is_dummy() call is folded into (the somewhat
verbosely named) _is_thread_prevented_from_running(), and that the
order of tests has been changed/optimized to hopefully catch common
cases earlier.

Suggested-by: Michael Scott <michael@opensourcefoundries.com>
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
  • Loading branch information
Andy Ross committed May 31, 2018
commit 749b4ff87744c8c306613c9aab82739dce8e99d8
26 changes: 13 additions & 13 deletions kernel/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,15 @@ int _is_t1_higher_prio_than_t2(struct k_thread *t1, struct k_thread *t2)

static int should_preempt(struct k_thread *th, int preempt_ok)
{
/* System initialization states can have dummy threads, never
* refuse to swap those
/* Preemption is OK if it's being explicitly allowed by
* software state (e.g. the thread called k_yield())
*/
if (!_current || _is_thread_dummy(_current)) {
return 1;
}

/* The idle threads can look "cooperative" if there are no
* preemptible priorities (this is sort of an API glitch).
* They must always be preemptible.
*/
if (_is_idle(_current)) {
if (preempt_ok) {
return 1;
}

/* Preemption is OK if it's being explicitly allowed */
if (preempt_ok) {
/* Or if we're pended/suspended/dummy (duh) */
if (!_current || _is_thread_prevented_from_running(_current)) {
return 1;
}

Expand All @@ -141,6 +133,14 @@ static int should_preempt(struct k_thread *th, int preempt_ok)
return 1;
}

/* The idle threads can look "cooperative" if there are no
* preemptible priorities (this is sort of an API glitch).
* They must always be preemptible.
*/
if (_is_idle(_current)) {
return 1;
}

return 0;
}

Expand Down