mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] exit: Move preemption fixup up, move blocking operations down
@ 2020-03-05 21:59 Jann Horn
  2020-03-05 22:01 ` Jann Horn
  0 siblings, 1 reply; 2+ messages in thread
From: Jann Horn @ 2020-03-05 21:59 UTC (permalink / raw)
  To: Peter Zijlstra, Thomas Gleixner, Ingo Molnar
  Cc: Andrew Morton, Frederic Weisbecker, Steven Rostedt, linux-kernel

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: 1dc0fffc48af ("sched/core: Robustify preemption leak checks")
Signed-off-by: Jann Horn <jannh@google.com>
---
As so often, I have no idea which tree this should go through. tip? mm?

 kernel/exit.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/kernel/exit.c b/kernel/exit.c
index 2833ffb0c211..db77c540aa92 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -713,8 +713,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));
 
@@ -732,6 +736,17 @@ 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);
@@ -749,13 +764,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);

base-commit: 9f65ed5fe41ce08ed1cb1f6a950f9ec694c142ad
-- 
2.25.0.265.gbab2e86ba0-goog


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] exit: Move preemption fixup up, move blocking operations down
  2020-03-05 21:59 [PATCH] exit: Move preemption fixup up, move blocking operations down Jann Horn
@ 2020-03-05 22:01 ` Jann Horn
  0 siblings, 0 replies; 2+ messages in thread
From: Jann Horn @ 2020-03-05 22:01 UTC (permalink / raw)
  To: Peter Zijlstra, Thomas Gleixner, Ingo Molnar
  Cc: Andrew Morton, Frederic Weisbecker, Steven Rostedt, kernel list

On Thu, Mar 5, 2020 at 11:00 PM Jann Horn <jannh@google.com> wrote:
> 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: 1dc0fffc48af ("sched/core: Robustify preemption leak checks")
> Signed-off-by: Jann Horn <jannh@google.com>
> ---
> As so often, I have no idea which tree this should go through. tip? mm?
>
>  kernel/exit.c | 26 +++++++++++++++++---------
>  1 file changed, 17 insertions(+), 9 deletions(-)
>
> diff --git a/kernel/exit.c b/kernel/exit.c
> index 2833ffb0c211..db77c540aa92 100644
> --- a/kernel/exit.c
> +++ b/kernel/exit.c
> @@ -713,8 +713,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));
>
> @@ -732,6 +736,17 @@ 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);

Ugh, I don't know where that extra whitespace comes from... I'll resend.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-03-05 22:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-05 21:59 [PATCH] exit: Move preemption fixup up, move blocking operations down Jann Horn
2020-03-05 22:01 ` Jann Horn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®