Commit aed5ed47 authored by Frederic Weisbecker's avatar Frederic Weisbecker Committed by Ingo Molnar

context_tracking: Protect against recursion

Context tracking recursion can happen when an exception triggers
in the middle of a call to a context tracking probe.

This special case can be caused by vmalloc faults. If an access
to a memory area allocated by vmalloc happens in the middle of
context_tracking_enter(), we may run into an endless fault loop
because the exception in turn calls context_tracking_enter()
which faults on the same vmalloc'ed memory, triggering an
exception again, etc...

Some rare crashes have been reported so lets protect against
this with a recursion counter.
Reported-by: default avatarDave Jones <davej@redhat.com>
Signed-off-by: default avatarFrederic Weisbecker <fweisbec@gmail.com>
Reviewed-by: default avatarRik van Riel <riel@redhat.com>
Acked-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Chris Metcalf <cmetcalf@ezchip.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Mike Galbraith <umgwanakikbuti@gmail.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Rafael J . Wysocki <rafael.j.wysocki@intel.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/1430928266-24888-2-git-send-email-fweisbec@gmail.comSigned-off-by: default avatarIngo Molnar <mingo@kernel.org>
parent 0e1dc427
...@@ -12,6 +12,7 @@ struct context_tracking { ...@@ -12,6 +12,7 @@ struct context_tracking {
* may be further optimized using static keys. * may be further optimized using static keys.
*/ */
bool active; bool active;
int recursion;
enum ctx_state { enum ctx_state {
CONTEXT_KERNEL = 0, CONTEXT_KERNEL = 0,
CONTEXT_USER, CONTEXT_USER,
......
...@@ -38,6 +38,25 @@ void context_tracking_cpu_set(int cpu) ...@@ -38,6 +38,25 @@ void context_tracking_cpu_set(int cpu)
} }
} }
static bool context_tracking_recursion_enter(void)
{
int recursion;
recursion = __this_cpu_inc_return(context_tracking.recursion);
if (recursion == 1)
return true;
WARN_ONCE((recursion < 1), "Invalid context tracking recursion value %d\n", recursion);
__this_cpu_dec(context_tracking.recursion);
return false;
}
static void context_tracking_recursion_exit(void)
{
__this_cpu_dec(context_tracking.recursion);
}
/** /**
* context_tracking_enter - Inform the context tracking that the CPU is going * context_tracking_enter - Inform the context tracking that the CPU is going
* enter user or guest space mode. * enter user or guest space mode.
...@@ -75,6 +94,9 @@ void context_tracking_enter(enum ctx_state state) ...@@ -75,6 +94,9 @@ void context_tracking_enter(enum ctx_state state)
WARN_ON_ONCE(!current->mm); WARN_ON_ONCE(!current->mm);
local_irq_save(flags); local_irq_save(flags);
if (!context_tracking_recursion_enter())
goto out_irq_restore;
if ( __this_cpu_read(context_tracking.state) != state) { if ( __this_cpu_read(context_tracking.state) != state) {
if (__this_cpu_read(context_tracking.active)) { if (__this_cpu_read(context_tracking.active)) {
/* /*
...@@ -105,6 +127,8 @@ void context_tracking_enter(enum ctx_state state) ...@@ -105,6 +127,8 @@ void context_tracking_enter(enum ctx_state state)
*/ */
__this_cpu_write(context_tracking.state, state); __this_cpu_write(context_tracking.state, state);
} }
context_tracking_recursion_exit();
out_irq_restore:
local_irq_restore(flags); local_irq_restore(flags);
} }
NOKPROBE_SYMBOL(context_tracking_enter); NOKPROBE_SYMBOL(context_tracking_enter);
...@@ -139,6 +163,9 @@ void context_tracking_exit(enum ctx_state state) ...@@ -139,6 +163,9 @@ void context_tracking_exit(enum ctx_state state)
return; return;
local_irq_save(flags); local_irq_save(flags);
if (!context_tracking_recursion_enter())
goto out_irq_restore;
if (__this_cpu_read(context_tracking.state) == state) { if (__this_cpu_read(context_tracking.state) == state) {
if (__this_cpu_read(context_tracking.active)) { if (__this_cpu_read(context_tracking.active)) {
/* /*
...@@ -153,6 +180,8 @@ void context_tracking_exit(enum ctx_state state) ...@@ -153,6 +180,8 @@ void context_tracking_exit(enum ctx_state state)
} }
__this_cpu_write(context_tracking.state, CONTEXT_KERNEL); __this_cpu_write(context_tracking.state, CONTEXT_KERNEL);
} }
context_tracking_recursion_exit();
out_irq_restore:
local_irq_restore(flags); local_irq_restore(flags);
} }
NOKPROBE_SYMBOL(context_tracking_exit); NOKPROBE_SYMBOL(context_tracking_exit);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment