1 /* SPDX-License-Identifier: GPL-2.0 */
3 * The Kernel Concurrency Sanitizer (KCSAN) infrastructure. For more info please
4 * see Documentation/dev-tools/kcsan.rst.
6 * Copyright (C) 2019, Google LLC.
9 #ifndef _KERNEL_KCSAN_KCSAN_H
10 #define _KERNEL_KCSAN_KCSAN_H
12 #include <linux/atomic.h>
13 #include <linux/kcsan.h>
14 #include <linux/sched.h>
16 /* The number of adjacent watchpoints to check. */
17 #define KCSAN_CHECK_ADJACENT 1
18 #define NUM_SLOTS (1 + 2*KCSAN_CHECK_ADJACENT)
20 extern unsigned int kcsan_udelay_task;
21 extern unsigned int kcsan_udelay_interrupt;
24 * Globally enable and disable KCSAN.
26 extern bool kcsan_enabled;
29 * Save/restore IRQ flags state trace dirtied by KCSAN.
31 void kcsan_save_irqtrace(struct task_struct *task);
32 void kcsan_restore_irqtrace(struct task_struct *task);
35 * Statistics counters displayed via debugfs; should only be modified in
38 enum kcsan_counter_id {
40 * Number of watchpoints currently in use.
42 KCSAN_COUNTER_USED_WATCHPOINTS,
45 * Total number of watchpoints set up.
47 KCSAN_COUNTER_SETUP_WATCHPOINTS,
50 * Total number of data races.
52 KCSAN_COUNTER_DATA_RACES,
55 * Total number of ASSERT failures due to races. If the observed race is
56 * due to two conflicting ASSERT type accesses, then both will be
59 KCSAN_COUNTER_ASSERT_FAILURES,
62 * Number of times no watchpoints were available.
64 KCSAN_COUNTER_NO_CAPACITY,
67 * A thread checking a watchpoint raced with another checking thread;
68 * only one will be reported.
70 KCSAN_COUNTER_REPORT_RACES,
73 * Observed data value change, but writer thread unknown.
75 KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN,
78 * The access cannot be encoded to a valid watchpoint.
80 KCSAN_COUNTER_UNENCODABLE_ACCESSES,
83 * Watchpoint encoding caused a watchpoint to fire on mismatching
86 KCSAN_COUNTER_ENCODING_FALSE_POSITIVES,
88 KCSAN_COUNTER_COUNT, /* number of counters */
90 extern atomic_long_t kcsan_counters[KCSAN_COUNTER_COUNT];
93 * Returns true if data races in the function symbol that maps to func_addr
94 * (offsets are ignored) should *not* be reported.
96 extern bool kcsan_skip_report_debugfs(unsigned long func_addr);
99 * Value-change states.
101 enum kcsan_value_change {
103 * Did not observe a value-change, however, it is valid to report the
104 * race, depending on preferences.
106 KCSAN_VALUE_CHANGE_MAYBE,
109 * Did not observe a value-change, and it is invalid to report the race.
111 KCSAN_VALUE_CHANGE_FALSE,
114 * The value was observed to change, and the race should be reported.
116 KCSAN_VALUE_CHANGE_TRUE,
119 enum kcsan_report_type {
121 * The thread that set up the watchpoint and briefly stalled was
122 * signalled that another thread triggered the watchpoint.
124 KCSAN_REPORT_RACE_SIGNAL,
127 * A thread found and consumed a matching watchpoint.
129 KCSAN_REPORT_CONSUMED_WATCHPOINT,
132 * No other thread was observed to race with the access, but the data
133 * value before and after the stall differs.
135 KCSAN_REPORT_RACE_UNKNOWN_ORIGIN,
139 * Print a race report from thread that encountered the race.
141 extern void kcsan_report(const volatile void *ptr, size_t size, int access_type,
142 enum kcsan_value_change value_change,
143 enum kcsan_report_type type, int watchpoint_idx);
145 #endif /* _KERNEL_KCSAN_KCSAN_H */