Commit 92c209ac authored by Marco Elver's avatar Marco Elver Committed by Ingo Molnar

kcsan: Improve IRQ state trace reporting

To improve the general usefulness of the IRQ state trace events with
KCSAN enabled, save and restore the trace information when entering and
exiting the KCSAN runtime as well as when generating a KCSAN report.

Without this, reporting the IRQ trace events (whether via a KCSAN report
or outside of KCSAN via a lockdep report) is rather useless due to
continuously being touched by KCSAN. This is because if KCSAN is
enabled, every instrumented memory access causes changes to IRQ trace
events (either by KCSAN disabling/enabling interrupts or taking
report_lock when generating a report).

Before "lockdep: Prepare for NMI IRQ state tracking", KCSAN avoided
touching the IRQ trace events via raw_local_irq_save/restore() and
lockdep_off/on().

Fixes: 248591f5 ("kcsan: Make KCSAN compatible with new IRQ state tracking")
Signed-off-by: default avatarMarco Elver <elver@google.com>
Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
Link: https://lore.kernel.org/r/20200729110916.3920464-2-elver@google.comSigned-off-by: default avatarIngo Molnar <mingo@kernel.org>
parent 0584df9c
...@@ -1184,8 +1184,12 @@ struct task_struct { ...@@ -1184,8 +1184,12 @@ struct task_struct {
#ifdef CONFIG_KASAN #ifdef CONFIG_KASAN
unsigned int kasan_depth; unsigned int kasan_depth;
#endif #endif
#ifdef CONFIG_KCSAN #ifdef CONFIG_KCSAN
struct kcsan_ctx kcsan_ctx; struct kcsan_ctx kcsan_ctx;
#ifdef CONFIG_TRACE_IRQFLAGS
struct irqtrace_events kcsan_save_irqtrace;
#endif
#endif #endif
#ifdef CONFIG_FUNCTION_GRAPH_TRACER #ifdef CONFIG_FUNCTION_GRAPH_TRACER
......
...@@ -291,6 +291,20 @@ static inline unsigned int get_delay(void) ...@@ -291,6 +291,20 @@ static inline unsigned int get_delay(void)
0); 0);
} }
void kcsan_save_irqtrace(struct task_struct *task)
{
#ifdef CONFIG_TRACE_IRQFLAGS
task->kcsan_save_irqtrace = task->irqtrace;
#endif
}
void kcsan_restore_irqtrace(struct task_struct *task)
{
#ifdef CONFIG_TRACE_IRQFLAGS
task->irqtrace = task->kcsan_save_irqtrace;
#endif
}
/* /*
* Pull everything together: check_access() below contains the performance * Pull everything together: check_access() below contains the performance
* critical operations; the fast-path (including check_access) functions should * critical operations; the fast-path (including check_access) functions should
...@@ -336,9 +350,11 @@ static noinline void kcsan_found_watchpoint(const volatile void *ptr, ...@@ -336,9 +350,11 @@ static noinline void kcsan_found_watchpoint(const volatile void *ptr,
flags = user_access_save(); flags = user_access_save();
if (consumed) { if (consumed) {
kcsan_save_irqtrace(current);
kcsan_report(ptr, size, type, KCSAN_VALUE_CHANGE_MAYBE, kcsan_report(ptr, size, type, KCSAN_VALUE_CHANGE_MAYBE,
KCSAN_REPORT_CONSUMED_WATCHPOINT, KCSAN_REPORT_CONSUMED_WATCHPOINT,
watchpoint - watchpoints); watchpoint - watchpoints);
kcsan_restore_irqtrace(current);
} else { } else {
/* /*
* The other thread may not print any diagnostics, as it has * The other thread may not print any diagnostics, as it has
...@@ -396,6 +412,12 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type) ...@@ -396,6 +412,12 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
goto out; goto out;
} }
/*
* Save and restore the IRQ state trace touched by KCSAN, since KCSAN's
* runtime is entered for every memory access, and potentially useful
* information is lost if dirtied by KCSAN.
*/
kcsan_save_irqtrace(current);
if (!kcsan_interrupt_watcher) if (!kcsan_interrupt_watcher)
local_irq_save(irq_flags); local_irq_save(irq_flags);
...@@ -539,6 +561,7 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type) ...@@ -539,6 +561,7 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
out_unlock: out_unlock:
if (!kcsan_interrupt_watcher) if (!kcsan_interrupt_watcher)
local_irq_restore(irq_flags); local_irq_restore(irq_flags);
kcsan_restore_irqtrace(current);
out: out:
user_access_restore(ua_flags); user_access_restore(ua_flags);
} }
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#define _KERNEL_KCSAN_KCSAN_H #define _KERNEL_KCSAN_KCSAN_H
#include <linux/kcsan.h> #include <linux/kcsan.h>
#include <linux/sched.h>
/* The number of adjacent watchpoints to check. */ /* The number of adjacent watchpoints to check. */
#define KCSAN_CHECK_ADJACENT 1 #define KCSAN_CHECK_ADJACENT 1
...@@ -22,6 +23,12 @@ extern unsigned int kcsan_udelay_interrupt; ...@@ -22,6 +23,12 @@ extern unsigned int kcsan_udelay_interrupt;
*/ */
extern bool kcsan_enabled; extern bool kcsan_enabled;
/*
* Save/restore IRQ flags state trace dirtied by KCSAN.
*/
void kcsan_save_irqtrace(struct task_struct *task);
void kcsan_restore_irqtrace(struct task_struct *task);
/* /*
* Initialize debugfs file. * Initialize debugfs file.
*/ */
......
...@@ -308,6 +308,9 @@ static void print_verbose_info(struct task_struct *task) ...@@ -308,6 +308,9 @@ static void print_verbose_info(struct task_struct *task)
if (!task) if (!task)
return; return;
/* Restore IRQ state trace for printing. */
kcsan_restore_irqtrace(task);
pr_err("\n"); pr_err("\n");
debug_show_held_locks(task); debug_show_held_locks(task);
print_irqtrace_events(task); print_irqtrace_events(task);
......
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