Skip to content

Commit

Permalink
exit: Move preemption fixup up, move blocking operations down
Browse files Browse the repository at this point in the history
With CONFIG_DEBUG_ATOMIC_SLEEP=y and CONFIG_CGROUPS=y, kernel oopses in
non-preemptible context look untidy; after the main oops, the kernel prints
a "sleeping function called from invalid context" report because
exit_signals() -> cgroup_threadgroup_change_begin() -> percpu_down_read()
can sleep, and that happens before the preempt_count_set(PREEMPT_ENABLED)
fixup.

It looks like the same thing applies to profile_task_exit() and
kcov_task_exit().

Fix it by moving the preemption fixup up and the calls to
profile_task_exit() and kcov_task_exit() down.

Fixes: 1dc0fff ("sched/core: Robustify preemption leak checks")
Signed-off-by: Jann Horn <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Link: https://lkml.kernel.org/r/[email protected]
  • Loading branch information
thejh authored and Peter Zijlstra committed Apr 30, 2020
1 parent 64297f2 commit 586b58c
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions kernel/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,12 @@ void __noreturn do_exit(long code)
struct task_struct *tsk = current;
int group_dead;

profile_task_exit(tsk);
kcov_task_exit(tsk);
/*
* We can get here from a kernel oops, sometimes with preemption off.
* Start by checking for critical errors.
* Then fix up important state like USER_DS and preemption.
* Then do everything else.
*/

WARN_ON(blk_needs_flush_plug(tsk));

Expand All @@ -727,6 +731,16 @@ void __noreturn do_exit(long code)
*/
set_fs(USER_DS);

if (unlikely(in_atomic())) {
pr_info("note: %s[%d] exited with preempt_count %d\n",
current->comm, task_pid_nr(current),
preempt_count());
preempt_count_set(PREEMPT_ENABLED);
}

profile_task_exit(tsk);
kcov_task_exit(tsk);

ptrace_event(PTRACE_EVENT_EXIT, code);

validate_creds_for_do_exit(tsk);
Expand All @@ -744,13 +758,6 @@ void __noreturn do_exit(long code)

exit_signals(tsk); /* sets PF_EXITING */

if (unlikely(in_atomic())) {
pr_info("note: %s[%d] exited with preempt_count %d\n",
current->comm, task_pid_nr(current),
preempt_count());
preempt_count_set(PREEMPT_ENABLED);
}

/* sync mm's RSS info before statistics gathering */
if (tsk->mm)
sync_mm_rss(tsk->mm);
Expand Down

0 comments on commit 586b58c

Please sign in to comment.