kernel/fork.c: detect early free of a live mm
authorMark Rutland <>
Thu, 5 Apr 2018 23:25:12 +0000 (16:25 -0700)
committerLinus Torvalds <>
Fri, 6 Apr 2018 04:36:27 +0000 (21:36 -0700)
KASAN splats indicate that in some cases we free a live mm, then
continue to access it, with potentially disastrous results.  This is
likely due to a mismatched mmdrop() somewhere in the kernel, but so far
the culprit remains elusive.

Let's have __mmdrop() verify that the mm isn't live for the current
task, similar to the existing check for init_mm.  This way, we can catch
this class of issue earlier, and without requiring KASAN.

Currently, idle_task_exit() leaves active_mm stale after it switches to
init_mm.  This isn't harmful, but will trigger the new assertions, so we
must adjust idle_task_exit() to update active_mm.

Signed-off-by: Mark Rutland <>
Reviewed-by: Andrew Morton <>
Cc: Ingo Molnar <>
Cc: Mathieu Desnoyers <>
Cc: Michal Hocko <>
Cc: Peter Zijlstra <>
Cc: Rik van Riel <>
Cc: Will Deacon <>
Signed-off-by: Andrew Morton <>
Signed-off-by: Linus Torvalds <>

index f71b67dc156de26e288933525cb16dc0806056c4..242c8c93d285d08a50b484b8e5e78d43dce5bbd4 100644 (file)
@@ -595,6 +595,8 @@ static void check_mm(struct mm_struct *mm)
 void __mmdrop(struct mm_struct *mm)
        BUG_ON(mm == &init_mm);
+       WARN_ON_ONCE(mm == current->mm);
+       WARN_ON_ONCE(mm == current->active_mm);
index 28b68995a4172b4459b162b1544c06cd9fae36c2..e8afd6086f2317768fa1317f0d41887eac175c14 100644 (file)
@@ -5560,6 +5560,7 @@ void idle_task_exit(void)
        if (mm != &init_mm) {
                switch_mm(mm, &init_mm, current);
+               current->active_mm = &init_mm;