blob: b176400a2735e7dbd9e12bb01012122d433a50e4 [file] [log] [blame]
Marco Elverdfd402a2019-11-14 19:02:54 +01001// SPDX-License-Identifier: GPL-2.0
2
Marco Elver27787932020-07-31 10:17:22 +02003#define pr_fmt(fmt) "kcsan: " fmt
4
Marco Elverdfd402a2019-11-14 19:02:54 +01005#include <linux/atomic.h>
6#include <linux/bug.h>
7#include <linux/delay.h>
8#include <linux/export.h>
9#include <linux/init.h>
Marco Elver1e6ee2f2020-02-04 18:21:10 +010010#include <linux/kernel.h>
Marco Elver757a4ce2020-03-25 17:41:56 +010011#include <linux/list.h>
Marco Elver80d4c472020-02-07 19:59:10 +010012#include <linux/moduleparam.h>
Marco Elverdfd402a2019-11-14 19:02:54 +010013#include <linux/percpu.h>
14#include <linux/preempt.h>
15#include <linux/random.h>
16#include <linux/sched.h>
17#include <linux/uaccess.h>
18
19#include "atomic.h"
20#include "encoding.h"
21#include "kcsan.h"
22
Marco Elver80d4c472020-02-07 19:59:10 +010023static bool kcsan_early_enable = IS_ENABLED(CONFIG_KCSAN_EARLY_ENABLE);
Marco Elver2402d0e2020-02-22 00:10:27 +010024unsigned int kcsan_udelay_task = CONFIG_KCSAN_UDELAY_TASK;
25unsigned int kcsan_udelay_interrupt = CONFIG_KCSAN_UDELAY_INTERRUPT;
Marco Elver80d4c472020-02-07 19:59:10 +010026static long kcsan_skip_watch = CONFIG_KCSAN_SKIP_WATCH;
Marco Elver48b1fc12020-02-21 23:02:09 +010027static bool kcsan_interrupt_watcher = IS_ENABLED(CONFIG_KCSAN_INTERRUPT_WATCHER);
Marco Elver80d4c472020-02-07 19:59:10 +010028
29#ifdef MODULE_PARAM_PREFIX
30#undef MODULE_PARAM_PREFIX
31#endif
32#define MODULE_PARAM_PREFIX "kcsan."
33module_param_named(early_enable, kcsan_early_enable, bool, 0);
34module_param_named(udelay_task, kcsan_udelay_task, uint, 0644);
35module_param_named(udelay_interrupt, kcsan_udelay_interrupt, uint, 0644);
36module_param_named(skip_watch, kcsan_skip_watch, long, 0644);
Marco Elver48b1fc12020-02-21 23:02:09 +010037module_param_named(interrupt_watcher, kcsan_interrupt_watcher, bool, 0444);
Marco Elver80d4c472020-02-07 19:59:10 +010038
Marco Elverdfd402a2019-11-14 19:02:54 +010039bool kcsan_enabled;
40
41/* Per-CPU kcsan_ctx for interrupts */
42static DEFINE_PER_CPU(struct kcsan_ctx, kcsan_cpu_ctx) = {
Ingo Molnar5cbaefe2019-11-20 10:41:43 +010043 .disable_count = 0,
44 .atomic_next = 0,
45 .atomic_nest_count = 0,
46 .in_flat_atomic = false,
Marco Elver81af89e2020-02-11 17:04:22 +010047 .access_mask = 0,
Marco Elver757a4ce2020-03-25 17:41:56 +010048 .scoped_accesses = {LIST_POISON1, NULL},
Marco Elverdfd402a2019-11-14 19:02:54 +010049};
50
51/*
Qiujun Huange7b34102020-03-05 15:21:07 +010052 * Helper macros to index into adjacent slots, starting from address slot
Marco Elverdfd402a2019-11-14 19:02:54 +010053 * itself, followed by the right and left slots.
54 *
55 * The purpose is 2-fold:
56 *
57 * 1. if during insertion the address slot is already occupied, check if
58 * any adjacent slots are free;
59 * 2. accesses that straddle a slot boundary due to size that exceeds a
60 * slot's range may check adjacent slots if any watchpoint matches.
61 *
62 * Note that accesses with very large size may still miss a watchpoint; however,
63 * given this should be rare, this is a reasonable trade-off to make, since this
64 * will avoid:
65 *
66 * 1. excessive contention between watchpoint checks and setup;
67 * 2. larger number of simultaneous watchpoints without sacrificing
68 * performance.
69 *
70 * Example: SLOT_IDX values for KCSAN_CHECK_ADJACENT=1, where i is [0, 1, 2]:
71 *
72 * slot=0: [ 1, 2, 0]
73 * slot=9: [10, 11, 9]
74 * slot=63: [64, 65, 63]
75 */
Marco Elverdfd402a2019-11-14 19:02:54 +010076#define SLOT_IDX(slot, i) (slot + ((i + KCSAN_CHECK_ADJACENT) % NUM_SLOTS))
77
78/*
Ingo Molnar5cbaefe2019-11-20 10:41:43 +010079 * SLOT_IDX_FAST is used in the fast-path. Not first checking the address's primary
Marco Elverd591ec32020-02-06 16:46:24 +010080 * slot (middle) is fine if we assume that races occur rarely. The set of
Marco Elverdfd402a2019-11-14 19:02:54 +010081 * indices {SLOT_IDX(slot, i) | i in [0, NUM_SLOTS)} is equivalent to
82 * {SLOT_IDX_FAST(slot, i) | i in [0, NUM_SLOTS)}.
83 */
84#define SLOT_IDX_FAST(slot, i) (slot + i)
85
86/*
87 * Watchpoints, with each entry encoded as defined in encoding.h: in order to be
88 * able to safely update and access a watchpoint without introducing locking
89 * overhead, we encode each watchpoint as a single atomic long. The initial
90 * zero-initialized state matches INVALID_WATCHPOINT.
91 *
92 * Add NUM_SLOTS-1 entries to account for overflow; this helps avoid having to
Ingo Molnar5cbaefe2019-11-20 10:41:43 +010093 * use more complicated SLOT_IDX_FAST calculation with modulo in the fast-path.
Marco Elverdfd402a2019-11-14 19:02:54 +010094 */
Ingo Molnar5cbaefe2019-11-20 10:41:43 +010095static atomic_long_t watchpoints[CONFIG_KCSAN_NUM_WATCHPOINTS + NUM_SLOTS-1];
Marco Elverdfd402a2019-11-14 19:02:54 +010096
97/*
98 * Instructions to skip watching counter, used in should_watch(). We use a
99 * per-CPU counter to avoid excessive contention.
100 */
101static DEFINE_PER_CPU(long, kcsan_skip);
102
Marco Elver5c361422020-01-07 17:31:04 +0100103static __always_inline atomic_long_t *find_watchpoint(unsigned long addr,
104 size_t size,
105 bool expect_write,
106 long *encoded_watchpoint)
Marco Elverdfd402a2019-11-14 19:02:54 +0100107{
108 const int slot = watchpoint_slot(addr);
109 const unsigned long addr_masked = addr & WATCHPOINT_ADDR_MASK;
110 atomic_long_t *watchpoint;
111 unsigned long wp_addr_masked;
112 size_t wp_size;
113 bool is_write;
114 int i;
115
116 BUILD_BUG_ON(CONFIG_KCSAN_NUM_WATCHPOINTS < NUM_SLOTS);
117
118 for (i = 0; i < NUM_SLOTS; ++i) {
119 watchpoint = &watchpoints[SLOT_IDX_FAST(slot, i)];
120 *encoded_watchpoint = atomic_long_read(watchpoint);
121 if (!decode_watchpoint(*encoded_watchpoint, &wp_addr_masked,
122 &wp_size, &is_write))
123 continue;
124
125 if (expect_write && !is_write)
126 continue;
127
128 /* Check if the watchpoint matches the access. */
129 if (matching_access(wp_addr_masked, wp_size, addr_masked, size))
130 return watchpoint;
131 }
132
133 return NULL;
134}
135
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100136static inline atomic_long_t *
137insert_watchpoint(unsigned long addr, size_t size, bool is_write)
Marco Elverdfd402a2019-11-14 19:02:54 +0100138{
139 const int slot = watchpoint_slot(addr);
140 const long encoded_watchpoint = encode_watchpoint(addr, size, is_write);
141 atomic_long_t *watchpoint;
142 int i;
143
144 /* Check slot index logic, ensuring we stay within array bounds. */
145 BUILD_BUG_ON(SLOT_IDX(0, 0) != KCSAN_CHECK_ADJACENT);
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100146 BUILD_BUG_ON(SLOT_IDX(0, KCSAN_CHECK_ADJACENT+1) != 0);
147 BUILD_BUG_ON(SLOT_IDX(CONFIG_KCSAN_NUM_WATCHPOINTS-1, KCSAN_CHECK_ADJACENT) != ARRAY_SIZE(watchpoints)-1);
148 BUILD_BUG_ON(SLOT_IDX(CONFIG_KCSAN_NUM_WATCHPOINTS-1, KCSAN_CHECK_ADJACENT+1) != ARRAY_SIZE(watchpoints) - NUM_SLOTS);
Marco Elverdfd402a2019-11-14 19:02:54 +0100149
150 for (i = 0; i < NUM_SLOTS; ++i) {
151 long expect_val = INVALID_WATCHPOINT;
152
153 /* Try to acquire this slot. */
154 watchpoint = &watchpoints[SLOT_IDX(slot, i)];
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100155 if (atomic_long_try_cmpxchg_relaxed(watchpoint, &expect_val, encoded_watchpoint))
Marco Elverdfd402a2019-11-14 19:02:54 +0100156 return watchpoint;
157 }
158
159 return NULL;
160}
161
162/*
163 * Return true if watchpoint was successfully consumed, false otherwise.
164 *
165 * This may return false if:
166 *
167 * 1. another thread already consumed the watchpoint;
168 * 2. the thread that set up the watchpoint already removed it;
169 * 3. the watchpoint was removed and then re-used.
170 */
Marco Elver5c361422020-01-07 17:31:04 +0100171static __always_inline bool
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100172try_consume_watchpoint(atomic_long_t *watchpoint, long encoded_watchpoint)
Marco Elverdfd402a2019-11-14 19:02:54 +0100173{
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100174 return atomic_long_try_cmpxchg_relaxed(watchpoint, &encoded_watchpoint, CONSUMED_WATCHPOINT);
Marco Elverdfd402a2019-11-14 19:02:54 +0100175}
176
Marco Elver61194182020-03-18 18:38:45 +0100177/* Return true if watchpoint was not touched, false if already consumed. */
178static inline bool consume_watchpoint(atomic_long_t *watchpoint)
Marco Elverdfd402a2019-11-14 19:02:54 +0100179{
Marco Elver61194182020-03-18 18:38:45 +0100180 return atomic_long_xchg_relaxed(watchpoint, CONSUMED_WATCHPOINT) != CONSUMED_WATCHPOINT;
181}
182
183/* Remove the watchpoint -- its slot may be reused after. */
184static inline void remove_watchpoint(atomic_long_t *watchpoint)
185{
186 atomic_long_set(watchpoint, INVALID_WATCHPOINT);
Marco Elverdfd402a2019-11-14 19:02:54 +0100187}
188
Marco Elver5c361422020-01-07 17:31:04 +0100189static __always_inline struct kcsan_ctx *get_ctx(void)
Marco Elverdfd402a2019-11-14 19:02:54 +0100190{
191 /*
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100192 * In interrupts, use raw_cpu_ptr to avoid unnecessary checks, that would
Marco Elverdfd402a2019-11-14 19:02:54 +0100193 * also result in calls that generate warnings in uaccess regions.
194 */
195 return in_task() ? &current->kcsan_ctx : raw_cpu_ptr(&kcsan_cpu_ctx);
196}
197
Marco Elver757a4ce2020-03-25 17:41:56 +0100198/* Check scoped accesses; never inline because this is a slow-path! */
199static noinline void kcsan_check_scoped_accesses(void)
200{
201 struct kcsan_ctx *ctx = get_ctx();
202 struct list_head *prev_save = ctx->scoped_accesses.prev;
203 struct kcsan_scoped_access *scoped_access;
204
205 ctx->scoped_accesses.prev = NULL; /* Avoid recursion. */
206 list_for_each_entry(scoped_access, &ctx->scoped_accesses, list)
207 __kcsan_check_access(scoped_access->ptr, scoped_access->size, scoped_access->type);
208 ctx->scoped_accesses.prev = prev_save;
209}
210
Marco Elver44656d32020-02-25 15:32:58 +0100211/* Rules for generic atomic accesses. Called from fast-path. */
Marco Elver1e6ee2f2020-02-04 18:21:10 +0100212static __always_inline bool
Marco Elver757a4ce2020-03-25 17:41:56 +0100213is_atomic(const volatile void *ptr, size_t size, int type, struct kcsan_ctx *ctx)
Marco Elverdfd402a2019-11-14 19:02:54 +0100214{
Marco Elver44656d32020-02-25 15:32:58 +0100215 if (type & KCSAN_ACCESS_ATOMIC)
Marco Elver1e6ee2f2020-02-04 18:21:10 +0100216 return true;
217
Marco Elverd591ec32020-02-06 16:46:24 +0100218 /*
219 * Unless explicitly declared atomic, never consider an assertion access
220 * as atomic. This allows using them also in atomic regions, such as
221 * seqlocks, without implicitly changing their semantics.
222 */
Marco Elver44656d32020-02-25 15:32:58 +0100223 if (type & KCSAN_ACCESS_ASSERT)
Marco Elverd591ec32020-02-06 16:46:24 +0100224 return false;
225
Marco Elver1e6ee2f2020-02-04 18:21:10 +0100226 if (IS_ENABLED(CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC) &&
Marco Elver44656d32020-02-25 15:32:58 +0100227 (type & KCSAN_ACCESS_WRITE) && size <= sizeof(long) &&
Marco Elver14e2ac82020-07-24 09:00:01 +0200228 !(type & KCSAN_ACCESS_COMPOUND) && IS_ALIGNED((unsigned long)ptr, size))
Marco Elver1e6ee2f2020-02-04 18:21:10 +0100229 return true; /* Assume aligned writes up to word size are atomic. */
230
Marco Elver44656d32020-02-25 15:32:58 +0100231 if (ctx->atomic_next > 0) {
Marco Elverdfd402a2019-11-14 19:02:54 +0100232 /*
233 * Because we do not have separate contexts for nested
234 * interrupts, in case atomic_next is set, we simply assume that
235 * the outer interrupt set atomic_next. In the worst case, we
236 * will conservatively consider operations as atomic. This is a
237 * reasonable trade-off to make, since this case should be
238 * extremely rare; however, even if extremely rare, it could
239 * lead to false positives otherwise.
240 */
241 if ((hardirq_count() >> HARDIRQ_SHIFT) < 2)
242 --ctx->atomic_next; /* in task, or outer interrupt */
243 return true;
244 }
Marco Elverdfd402a2019-11-14 19:02:54 +0100245
Marco Elver44656d32020-02-25 15:32:58 +0100246 return ctx->atomic_nest_count > 0 || ctx->in_flat_atomic;
Marco Elverdfd402a2019-11-14 19:02:54 +0100247}
248
Marco Elver1e6ee2f2020-02-04 18:21:10 +0100249static __always_inline bool
Marco Elver757a4ce2020-03-25 17:41:56 +0100250should_watch(const volatile void *ptr, size_t size, int type, struct kcsan_ctx *ctx)
Marco Elverdfd402a2019-11-14 19:02:54 +0100251{
252 /*
253 * Never set up watchpoints when memory operations are atomic.
254 *
255 * Need to check this first, before kcsan_skip check below: (1) atomics
256 * should not count towards skipped instructions, and (2) to actually
257 * decrement kcsan_atomic_next for consecutive instruction stream.
258 */
Marco Elver757a4ce2020-03-25 17:41:56 +0100259 if (is_atomic(ptr, size, type, ctx))
Marco Elverdfd402a2019-11-14 19:02:54 +0100260 return false;
261
262 if (this_cpu_dec_return(kcsan_skip) >= 0)
263 return false;
264
265 /*
266 * NOTE: If we get here, kcsan_skip must always be reset in slow path
267 * via reset_kcsan_skip() to avoid underflow.
268 */
269
270 /* this operation should be watched */
271 return true;
272}
273
274static inline void reset_kcsan_skip(void)
275{
Marco Elver80d4c472020-02-07 19:59:10 +0100276 long skip_count = kcsan_skip_watch -
Marco Elverdfd402a2019-11-14 19:02:54 +0100277 (IS_ENABLED(CONFIG_KCSAN_SKIP_WATCH_RANDOMIZE) ?
Marco Elver80d4c472020-02-07 19:59:10 +0100278 prandom_u32_max(kcsan_skip_watch) :
Marco Elverdfd402a2019-11-14 19:02:54 +0100279 0);
280 this_cpu_write(kcsan_skip, skip_count);
281}
282
Marco Elver5c361422020-01-07 17:31:04 +0100283static __always_inline bool kcsan_is_enabled(void)
Marco Elverdfd402a2019-11-14 19:02:54 +0100284{
285 return READ_ONCE(kcsan_enabled) && get_ctx()->disable_count == 0;
286}
287
Marco Elver106a3072020-07-24 09:00:03 +0200288static inline unsigned int get_delay(int type)
Marco Elverdfd402a2019-11-14 19:02:54 +0100289{
Marco Elver80d4c472020-02-07 19:59:10 +0100290 unsigned int delay = in_task() ? kcsan_udelay_task : kcsan_udelay_interrupt;
Marco Elver106a3072020-07-24 09:00:03 +0200291 /* For certain access types, skew the random delay to be longer. */
292 unsigned int skew_delay_order =
293 (type & (KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_ASSERT)) ? 1 : 0;
294
Marco Elverdfd402a2019-11-14 19:02:54 +0100295 return delay - (IS_ENABLED(CONFIG_KCSAN_DELAY_RANDOMIZE) ?
Marco Elver106a3072020-07-24 09:00:03 +0200296 prandom_u32_max(delay >> skew_delay_order) :
Marco Elverdfd402a2019-11-14 19:02:54 +0100297 0);
298}
299
Marco Elver92c209a2020-07-29 13:09:16 +0200300void kcsan_save_irqtrace(struct task_struct *task)
301{
302#ifdef CONFIG_TRACE_IRQFLAGS
303 task->kcsan_save_irqtrace = task->irqtrace;
304#endif
305}
306
307void kcsan_restore_irqtrace(struct task_struct *task)
308{
309#ifdef CONFIG_TRACE_IRQFLAGS
310 task->irqtrace = task->kcsan_save_irqtrace;
311#endif
312}
313
Marco Elverdfd402a2019-11-14 19:02:54 +0100314/*
315 * Pull everything together: check_access() below contains the performance
316 * critical operations; the fast-path (including check_access) functions should
317 * all be inlinable by the instrumentation functions.
318 *
319 * The slow-path (kcsan_found_watchpoint, kcsan_setup_watchpoint) are
320 * non-inlinable -- note that, we prefix these with "kcsan_" to ensure they can
321 * be filtered from the stacktrace, as well as give them unique names for the
322 * UACCESS whitelist of objtool. Each function uses user_access_save/restore(),
323 * since they do not access any user memory, but instrumentation is still
324 * emitted in UACCESS regions.
325 */
326
327static noinline void kcsan_found_watchpoint(const volatile void *ptr,
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100328 size_t size,
Marco Elver47144ec2020-01-10 19:48:33 +0100329 int type,
Marco Elverdfd402a2019-11-14 19:02:54 +0100330 atomic_long_t *watchpoint,
331 long encoded_watchpoint)
332{
333 unsigned long flags;
334 bool consumed;
335
336 if (!kcsan_is_enabled())
337 return;
Marco Elver81af89e2020-02-11 17:04:22 +0100338
339 /*
340 * The access_mask check relies on value-change comparison. To avoid
341 * reporting a race where e.g. the writer set up the watchpoint, but the
342 * reader has access_mask!=0, we have to ignore the found watchpoint.
343 */
344 if (get_ctx()->access_mask != 0)
345 return;
346
Marco Elverdfd402a2019-11-14 19:02:54 +0100347 /*
348 * Consume the watchpoint as soon as possible, to minimize the chances
349 * of !consumed. Consuming the watchpoint must always be guarded by
350 * kcsan_is_enabled() check, as otherwise we might erroneously
351 * triggering reports when disabled.
352 */
353 consumed = try_consume_watchpoint(watchpoint, encoded_watchpoint);
354
355 /* keep this after try_consume_watchpoint */
356 flags = user_access_save();
357
358 if (consumed) {
Marco Elver92c209a2020-07-29 13:09:16 +0200359 kcsan_save_irqtrace(current);
Marco Elver135c0872020-03-18 18:38:44 +0100360 kcsan_report(ptr, size, type, KCSAN_VALUE_CHANGE_MAYBE,
Marco Elver61194182020-03-18 18:38:45 +0100361 KCSAN_REPORT_CONSUMED_WATCHPOINT,
362 watchpoint - watchpoints);
Marco Elver92c209a2020-07-29 13:09:16 +0200363 kcsan_restore_irqtrace(current);
Marco Elverdfd402a2019-11-14 19:02:54 +0100364 } else {
365 /*
366 * The other thread may not print any diagnostics, as it has
367 * already removed the watchpoint, or another thread consumed
368 * the watchpoint before this thread.
369 */
370 kcsan_counter_inc(KCSAN_COUNTER_REPORT_RACES);
371 }
Marco Elverd591ec32020-02-06 16:46:24 +0100372
373 if ((type & KCSAN_ACCESS_ASSERT) != 0)
374 kcsan_counter_inc(KCSAN_COUNTER_ASSERT_FAILURES);
375 else
376 kcsan_counter_inc(KCSAN_COUNTER_DATA_RACES);
Marco Elverdfd402a2019-11-14 19:02:54 +0100377
378 user_access_restore(flags);
379}
380
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100381static noinline void
Marco Elver47144ec2020-01-10 19:48:33 +0100382kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
Marco Elverdfd402a2019-11-14 19:02:54 +0100383{
Marco Elver47144ec2020-01-10 19:48:33 +0100384 const bool is_write = (type & KCSAN_ACCESS_WRITE) != 0;
Marco Elverd591ec32020-02-06 16:46:24 +0100385 const bool is_assert = (type & KCSAN_ACCESS_ASSERT) != 0;
Marco Elverdfd402a2019-11-14 19:02:54 +0100386 atomic_long_t *watchpoint;
387 union {
388 u8 _1;
389 u16 _2;
390 u32 _4;
391 u64 _8;
392 } expect_value;
Marco Elver81af89e2020-02-11 17:04:22 +0100393 unsigned long access_mask;
Marco Elverb738f612020-02-11 17:04:21 +0100394 enum kcsan_value_change value_change = KCSAN_VALUE_CHANGE_MAYBE;
Marco Elverdfd402a2019-11-14 19:02:54 +0100395 unsigned long ua_flags = user_access_save();
Marco Elver48b1fc12020-02-21 23:02:09 +0100396 unsigned long irq_flags = 0;
Marco Elverdfd402a2019-11-14 19:02:54 +0100397
398 /*
399 * Always reset kcsan_skip counter in slow-path to avoid underflow; see
400 * should_watch().
401 */
402 reset_kcsan_skip();
403
404 if (!kcsan_is_enabled())
405 goto out;
406
Marco Elver44656d32020-02-25 15:32:58 +0100407 /*
408 * Special atomic rules: unlikely to be true, so we check them here in
409 * the slow-path, and not in the fast-path in is_atomic(). Call after
410 * kcsan_is_enabled(), as we may access memory that is not yet
411 * initialized during early boot.
412 */
413 if (!is_assert && kcsan_is_atomic_special(ptr))
414 goto out;
415
Marco Elverdfd402a2019-11-14 19:02:54 +0100416 if (!check_encodable((unsigned long)ptr, size)) {
417 kcsan_counter_inc(KCSAN_COUNTER_UNENCODABLE_ACCESSES);
418 goto out;
419 }
420
Marco Elver92c209a2020-07-29 13:09:16 +0200421 /*
422 * Save and restore the IRQ state trace touched by KCSAN, since KCSAN's
423 * runtime is entered for every memory access, and potentially useful
424 * information is lost if dirtied by KCSAN.
425 */
426 kcsan_save_irqtrace(current);
Marco Elver48b1fc12020-02-21 23:02:09 +0100427 if (!kcsan_interrupt_watcher)
Marco Elver248591f2020-06-24 13:32:46 +0200428 local_irq_save(irq_flags);
Marco Elverdfd402a2019-11-14 19:02:54 +0100429
430 watchpoint = insert_watchpoint((unsigned long)ptr, size, is_write);
431 if (watchpoint == NULL) {
432 /*
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100433 * Out of capacity: the size of 'watchpoints', and the frequency
434 * with which should_watch() returns true should be tweaked so
Marco Elverdfd402a2019-11-14 19:02:54 +0100435 * that this case happens very rarely.
436 */
437 kcsan_counter_inc(KCSAN_COUNTER_NO_CAPACITY);
438 goto out_unlock;
439 }
440
441 kcsan_counter_inc(KCSAN_COUNTER_SETUP_WATCHPOINTS);
442 kcsan_counter_inc(KCSAN_COUNTER_USED_WATCHPOINTS);
443
444 /*
445 * Read the current value, to later check and infer a race if the data
446 * was modified via a non-instrumented access, e.g. from a device.
447 */
Marco Elverb738f612020-02-11 17:04:21 +0100448 expect_value._8 = 0;
Marco Elverdfd402a2019-11-14 19:02:54 +0100449 switch (size) {
450 case 1:
451 expect_value._1 = READ_ONCE(*(const u8 *)ptr);
452 break;
453 case 2:
454 expect_value._2 = READ_ONCE(*(const u16 *)ptr);
455 break;
456 case 4:
457 expect_value._4 = READ_ONCE(*(const u32 *)ptr);
458 break;
459 case 8:
460 expect_value._8 = READ_ONCE(*(const u64 *)ptr);
461 break;
462 default:
463 break; /* ignore; we do not diff the values */
464 }
465
466 if (IS_ENABLED(CONFIG_KCSAN_DEBUG)) {
467 kcsan_disable_current();
Marco Elver27787932020-07-31 10:17:22 +0200468 pr_err("watching %s, size: %zu, addr: %px [slot: %d, encoded: %lx]\n",
Marco Elverdfd402a2019-11-14 19:02:54 +0100469 is_write ? "write" : "read", size, ptr,
470 watchpoint_slot((unsigned long)ptr),
471 encode_watchpoint((unsigned long)ptr, size, is_write));
472 kcsan_enable_current();
473 }
474
475 /*
476 * Delay this thread, to increase probability of observing a racy
477 * conflicting access.
478 */
Marco Elver106a3072020-07-24 09:00:03 +0200479 udelay(get_delay(type));
Marco Elverdfd402a2019-11-14 19:02:54 +0100480
481 /*
482 * Re-read value, and check if it is as expected; if not, we infer a
483 * racy access.
484 */
Marco Elver81af89e2020-02-11 17:04:22 +0100485 access_mask = get_ctx()->access_mask;
Marco Elverdfd402a2019-11-14 19:02:54 +0100486 switch (size) {
487 case 1:
Marco Elverb738f612020-02-11 17:04:21 +0100488 expect_value._1 ^= READ_ONCE(*(const u8 *)ptr);
Marco Elver81af89e2020-02-11 17:04:22 +0100489 if (access_mask)
490 expect_value._1 &= (u8)access_mask;
Marco Elverdfd402a2019-11-14 19:02:54 +0100491 break;
492 case 2:
Marco Elverb738f612020-02-11 17:04:21 +0100493 expect_value._2 ^= READ_ONCE(*(const u16 *)ptr);
Marco Elver81af89e2020-02-11 17:04:22 +0100494 if (access_mask)
495 expect_value._2 &= (u16)access_mask;
Marco Elverdfd402a2019-11-14 19:02:54 +0100496 break;
497 case 4:
Marco Elverb738f612020-02-11 17:04:21 +0100498 expect_value._4 ^= READ_ONCE(*(const u32 *)ptr);
Marco Elver81af89e2020-02-11 17:04:22 +0100499 if (access_mask)
500 expect_value._4 &= (u32)access_mask;
Marco Elverdfd402a2019-11-14 19:02:54 +0100501 break;
502 case 8:
Marco Elverb738f612020-02-11 17:04:21 +0100503 expect_value._8 ^= READ_ONCE(*(const u64 *)ptr);
Marco Elver81af89e2020-02-11 17:04:22 +0100504 if (access_mask)
505 expect_value._8 &= (u64)access_mask;
Marco Elverdfd402a2019-11-14 19:02:54 +0100506 break;
507 default:
508 break; /* ignore; we do not diff the values */
509 }
510
Marco Elverb738f612020-02-11 17:04:21 +0100511 /* Were we able to observe a value-change? */
512 if (expect_value._8 != 0)
513 value_change = KCSAN_VALUE_CHANGE_TRUE;
514
Marco Elverdfd402a2019-11-14 19:02:54 +0100515 /* Check if this access raced with another. */
Marco Elver61194182020-03-18 18:38:45 +0100516 if (!consume_watchpoint(watchpoint)) {
Marco Elverdfd402a2019-11-14 19:02:54 +0100517 /*
Marco Elverb738f612020-02-11 17:04:21 +0100518 * Depending on the access type, map a value_change of MAYBE to
Marco Elver81af89e2020-02-11 17:04:22 +0100519 * TRUE (always report) or FALSE (never report).
Marco Elverb738f612020-02-11 17:04:21 +0100520 */
Marco Elver81af89e2020-02-11 17:04:22 +0100521 if (value_change == KCSAN_VALUE_CHANGE_MAYBE) {
522 if (access_mask != 0) {
523 /*
524 * For access with access_mask, we require a
525 * value-change, as it is likely that races on
526 * ~access_mask bits are expected.
527 */
528 value_change = KCSAN_VALUE_CHANGE_FALSE;
529 } else if (size > 8 || is_assert) {
530 /* Always assume a value-change. */
531 value_change = KCSAN_VALUE_CHANGE_TRUE;
532 }
Marco Elverb738f612020-02-11 17:04:21 +0100533 }
534
535 /*
Marco Elverdfd402a2019-11-14 19:02:54 +0100536 * No need to increment 'data_races' counter, as the racing
537 * thread already did.
Marco Elverd591ec32020-02-06 16:46:24 +0100538 *
539 * Count 'assert_failures' for each failed ASSERT access,
540 * therefore both this thread and the racing thread may
541 * increment this counter.
Marco Elverdfd402a2019-11-14 19:02:54 +0100542 */
Marco Elverb738f612020-02-11 17:04:21 +0100543 if (is_assert && value_change == KCSAN_VALUE_CHANGE_TRUE)
Marco Elverd591ec32020-02-06 16:46:24 +0100544 kcsan_counter_inc(KCSAN_COUNTER_ASSERT_FAILURES);
545
Marco Elver61194182020-03-18 18:38:45 +0100546 kcsan_report(ptr, size, type, value_change, KCSAN_REPORT_RACE_SIGNAL,
547 watchpoint - watchpoints);
Marco Elverb738f612020-02-11 17:04:21 +0100548 } else if (value_change == KCSAN_VALUE_CHANGE_TRUE) {
Marco Elverdfd402a2019-11-14 19:02:54 +0100549 /* Inferring a race, since the value should not have changed. */
Marco Elverd591ec32020-02-06 16:46:24 +0100550
Marco Elverdfd402a2019-11-14 19:02:54 +0100551 kcsan_counter_inc(KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN);
Marco Elverd591ec32020-02-06 16:46:24 +0100552 if (is_assert)
553 kcsan_counter_inc(KCSAN_COUNTER_ASSERT_FAILURES);
554
555 if (IS_ENABLED(CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN) || is_assert)
Marco Elverb738f612020-02-11 17:04:21 +0100556 kcsan_report(ptr, size, type, KCSAN_VALUE_CHANGE_TRUE,
Marco Elver61194182020-03-18 18:38:45 +0100557 KCSAN_REPORT_RACE_UNKNOWN_ORIGIN,
558 watchpoint - watchpoints);
Marco Elverdfd402a2019-11-14 19:02:54 +0100559 }
560
Marco Elver61194182020-03-18 18:38:45 +0100561 /*
562 * Remove watchpoint; must be after reporting, since the slot may be
563 * reused after this point.
564 */
565 remove_watchpoint(watchpoint);
Marco Elverdfd402a2019-11-14 19:02:54 +0100566 kcsan_counter_dec(KCSAN_COUNTER_USED_WATCHPOINTS);
567out_unlock:
Marco Elver48b1fc12020-02-21 23:02:09 +0100568 if (!kcsan_interrupt_watcher)
Marco Elver248591f2020-06-24 13:32:46 +0200569 local_irq_restore(irq_flags);
Marco Elver92c209a2020-07-29 13:09:16 +0200570 kcsan_restore_irqtrace(current);
Marco Elverdfd402a2019-11-14 19:02:54 +0100571out:
572 user_access_restore(ua_flags);
573}
574
575static __always_inline void check_access(const volatile void *ptr, size_t size,
576 int type)
577{
578 const bool is_write = (type & KCSAN_ACCESS_WRITE) != 0;
579 atomic_long_t *watchpoint;
580 long encoded_watchpoint;
581
582 /*
Marco Elvered95f952020-02-05 11:14:19 +0100583 * Do nothing for 0 sized check; this comparison will be optimized out
584 * for constant sized instrumentation (__tsan_{read,write}N).
585 */
586 if (unlikely(size == 0))
587 return;
588
589 /*
Marco Elverdfd402a2019-11-14 19:02:54 +0100590 * Avoid user_access_save in fast-path: find_watchpoint is safe without
591 * user_access_save, as the address that ptr points to is only used to
592 * check if a watchpoint exists; ptr is never dereferenced.
593 */
594 watchpoint = find_watchpoint((unsigned long)ptr, size, !is_write,
595 &encoded_watchpoint);
596 /*
597 * It is safe to check kcsan_is_enabled() after find_watchpoint in the
Marco Elverd591ec32020-02-06 16:46:24 +0100598 * slow-path, as long as no state changes that cause a race to be
Marco Elverdfd402a2019-11-14 19:02:54 +0100599 * detected and reported have occurred until kcsan_is_enabled() is
600 * checked.
601 */
602
603 if (unlikely(watchpoint != NULL))
Marco Elver47144ec2020-01-10 19:48:33 +0100604 kcsan_found_watchpoint(ptr, size, type, watchpoint,
Marco Elverdfd402a2019-11-14 19:02:54 +0100605 encoded_watchpoint);
Marco Elver757a4ce2020-03-25 17:41:56 +0100606 else {
607 struct kcsan_ctx *ctx = get_ctx(); /* Call only once in fast-path. */
608
609 if (unlikely(should_watch(ptr, size, type, ctx)))
610 kcsan_setup_watchpoint(ptr, size, type);
611 else if (unlikely(ctx->scoped_accesses.prev))
612 kcsan_check_scoped_accesses();
613 }
Marco Elverdfd402a2019-11-14 19:02:54 +0100614}
615
616/* === Public interface ===================================================== */
617
618void __init kcsan_init(void)
619{
620 BUG_ON(!in_task());
621
622 kcsan_debugfs_init();
623
624 /*
625 * We are in the init task, and no other tasks should be running;
626 * WRITE_ONCE without memory barrier is sufficient.
627 */
Marco Elver27787932020-07-31 10:17:22 +0200628 if (kcsan_early_enable) {
629 pr_info("enabled early\n");
Marco Elverdfd402a2019-11-14 19:02:54 +0100630 WRITE_ONCE(kcsan_enabled, true);
Marco Elver27787932020-07-31 10:17:22 +0200631 }
Marco Elverdfd402a2019-11-14 19:02:54 +0100632}
633
634/* === Exported interface =================================================== */
635
636void kcsan_disable_current(void)
637{
638 ++get_ctx()->disable_count;
639}
640EXPORT_SYMBOL(kcsan_disable_current);
641
642void kcsan_enable_current(void)
643{
644 if (get_ctx()->disable_count-- == 0) {
645 /*
646 * Warn if kcsan_enable_current() calls are unbalanced with
647 * kcsan_disable_current() calls, which causes disable_count to
648 * become negative and should not happen.
649 */
650 kcsan_disable_current(); /* restore to 0, KCSAN still enabled */
651 kcsan_disable_current(); /* disable to generate warning */
652 WARN(1, "Unbalanced %s()", __func__);
653 kcsan_enable_current();
654 }
655}
656EXPORT_SYMBOL(kcsan_enable_current);
657
Marco Elver19acd032020-04-24 17:47:29 +0200658void kcsan_enable_current_nowarn(void)
659{
660 if (get_ctx()->disable_count-- == 0)
661 kcsan_disable_current();
662}
663EXPORT_SYMBOL(kcsan_enable_current_nowarn);
664
Marco Elverdfd402a2019-11-14 19:02:54 +0100665void kcsan_nestable_atomic_begin(void)
666{
667 /*
668 * Do *not* check and warn if we are in a flat atomic region: nestable
669 * and flat atomic regions are independent from each other.
670 * See include/linux/kcsan.h: struct kcsan_ctx comments for more
671 * comments.
672 */
673
674 ++get_ctx()->atomic_nest_count;
675}
676EXPORT_SYMBOL(kcsan_nestable_atomic_begin);
677
678void kcsan_nestable_atomic_end(void)
679{
680 if (get_ctx()->atomic_nest_count-- == 0) {
681 /*
682 * Warn if kcsan_nestable_atomic_end() calls are unbalanced with
683 * kcsan_nestable_atomic_begin() calls, which causes
684 * atomic_nest_count to become negative and should not happen.
685 */
686 kcsan_nestable_atomic_begin(); /* restore to 0 */
687 kcsan_disable_current(); /* disable to generate warning */
688 WARN(1, "Unbalanced %s()", __func__);
689 kcsan_enable_current();
690 }
691}
692EXPORT_SYMBOL(kcsan_nestable_atomic_end);
693
694void kcsan_flat_atomic_begin(void)
695{
696 get_ctx()->in_flat_atomic = true;
697}
698EXPORT_SYMBOL(kcsan_flat_atomic_begin);
699
700void kcsan_flat_atomic_end(void)
701{
702 get_ctx()->in_flat_atomic = false;
703}
704EXPORT_SYMBOL(kcsan_flat_atomic_end);
705
706void kcsan_atomic_next(int n)
707{
708 get_ctx()->atomic_next = n;
709}
710EXPORT_SYMBOL(kcsan_atomic_next);
711
Marco Elver81af89e2020-02-11 17:04:22 +0100712void kcsan_set_access_mask(unsigned long mask)
713{
714 get_ctx()->access_mask = mask;
715}
716EXPORT_SYMBOL(kcsan_set_access_mask);
717
Marco Elver757a4ce2020-03-25 17:41:56 +0100718struct kcsan_scoped_access *
719kcsan_begin_scoped_access(const volatile void *ptr, size_t size, int type,
720 struct kcsan_scoped_access *sa)
721{
722 struct kcsan_ctx *ctx = get_ctx();
723
724 __kcsan_check_access(ptr, size, type);
725
726 ctx->disable_count++; /* Disable KCSAN, in case list debugging is on. */
727
728 INIT_LIST_HEAD(&sa->list);
729 sa->ptr = ptr;
730 sa->size = size;
731 sa->type = type;
732
733 if (!ctx->scoped_accesses.prev) /* Lazy initialize list head. */
734 INIT_LIST_HEAD(&ctx->scoped_accesses);
735 list_add(&sa->list, &ctx->scoped_accesses);
736
737 ctx->disable_count--;
738 return sa;
739}
740EXPORT_SYMBOL(kcsan_begin_scoped_access);
741
742void kcsan_end_scoped_access(struct kcsan_scoped_access *sa)
743{
744 struct kcsan_ctx *ctx = get_ctx();
745
746 if (WARN(!ctx->scoped_accesses.prev, "Unbalanced %s()?", __func__))
747 return;
748
749 ctx->disable_count++; /* Disable KCSAN, in case list debugging is on. */
750
751 list_del(&sa->list);
752 if (list_empty(&ctx->scoped_accesses))
753 /*
754 * Ensure we do not enter kcsan_check_scoped_accesses()
755 * slow-path if unnecessary, and avoids requiring list_empty()
756 * in the fast-path (to avoid a READ_ONCE() and potential
757 * uaccess warning).
758 */
759 ctx->scoped_accesses.prev = NULL;
760
761 ctx->disable_count--;
762
763 __kcsan_check_access(sa->ptr, sa->size, sa->type);
764}
765EXPORT_SYMBOL(kcsan_end_scoped_access);
766
Marco Elverdfd402a2019-11-14 19:02:54 +0100767void __kcsan_check_access(const volatile void *ptr, size_t size, int type)
768{
769 check_access(ptr, size, type);
770}
771EXPORT_SYMBOL(__kcsan_check_access);
772
773/*
774 * KCSAN uses the same instrumentation that is emitted by supported compilers
775 * for ThreadSanitizer (TSAN).
776 *
777 * When enabled, the compiler emits instrumentation calls (the functions
778 * prefixed with "__tsan" below) for all loads and stores that it generated;
779 * inline asm is not instrumented.
780 *
781 * Note that, not all supported compiler versions distinguish aligned/unaligned
782 * accesses, but e.g. recent versions of Clang do. We simply alias the unaligned
783 * version to the generic version, which can handle both.
784 */
785
786#define DEFINE_TSAN_READ_WRITE(size) \
Marco Elver9dd979b2020-06-16 14:36:22 +0200787 void __tsan_read##size(void *ptr); \
Marco Elverdfd402a2019-11-14 19:02:54 +0100788 void __tsan_read##size(void *ptr) \
789 { \
790 check_access(ptr, size, 0); \
791 } \
792 EXPORT_SYMBOL(__tsan_read##size); \
793 void __tsan_unaligned_read##size(void *ptr) \
794 __alias(__tsan_read##size); \
795 EXPORT_SYMBOL(__tsan_unaligned_read##size); \
Marco Elver9dd979b2020-06-16 14:36:22 +0200796 void __tsan_write##size(void *ptr); \
Marco Elverdfd402a2019-11-14 19:02:54 +0100797 void __tsan_write##size(void *ptr) \
798 { \
799 check_access(ptr, size, KCSAN_ACCESS_WRITE); \
800 } \
801 EXPORT_SYMBOL(__tsan_write##size); \
802 void __tsan_unaligned_write##size(void *ptr) \
803 __alias(__tsan_write##size); \
Marco Elver14e2ac82020-07-24 09:00:01 +0200804 EXPORT_SYMBOL(__tsan_unaligned_write##size); \
805 void __tsan_read_write##size(void *ptr); \
806 void __tsan_read_write##size(void *ptr) \
807 { \
808 check_access(ptr, size, \
809 KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE); \
810 } \
811 EXPORT_SYMBOL(__tsan_read_write##size); \
812 void __tsan_unaligned_read_write##size(void *ptr) \
813 __alias(__tsan_read_write##size); \
814 EXPORT_SYMBOL(__tsan_unaligned_read_write##size)
Marco Elverdfd402a2019-11-14 19:02:54 +0100815
816DEFINE_TSAN_READ_WRITE(1);
817DEFINE_TSAN_READ_WRITE(2);
818DEFINE_TSAN_READ_WRITE(4);
819DEFINE_TSAN_READ_WRITE(8);
820DEFINE_TSAN_READ_WRITE(16);
821
Marco Elver9dd979b2020-06-16 14:36:22 +0200822void __tsan_read_range(void *ptr, size_t size);
Marco Elverdfd402a2019-11-14 19:02:54 +0100823void __tsan_read_range(void *ptr, size_t size)
824{
825 check_access(ptr, size, 0);
826}
827EXPORT_SYMBOL(__tsan_read_range);
828
Marco Elver9dd979b2020-06-16 14:36:22 +0200829void __tsan_write_range(void *ptr, size_t size);
Marco Elverdfd402a2019-11-14 19:02:54 +0100830void __tsan_write_range(void *ptr, size_t size)
831{
832 check_access(ptr, size, KCSAN_ACCESS_WRITE);
833}
834EXPORT_SYMBOL(__tsan_write_range);
835
836/*
Marco Elver75d75b72020-05-21 16:20:39 +0200837 * Use of explicit volatile is generally disallowed [1], however, volatile is
838 * still used in various concurrent context, whether in low-level
839 * synchronization primitives or for legacy reasons.
840 * [1] https://lwn.net/Articles/233479/
841 *
842 * We only consider volatile accesses atomic if they are aligned and would pass
843 * the size-check of compiletime_assert_rwonce_type().
844 */
845#define DEFINE_TSAN_VOLATILE_READ_WRITE(size) \
Marco Elver9dd979b2020-06-16 14:36:22 +0200846 void __tsan_volatile_read##size(void *ptr); \
Marco Elver75d75b72020-05-21 16:20:39 +0200847 void __tsan_volatile_read##size(void *ptr) \
848 { \
849 const bool is_atomic = size <= sizeof(long long) && \
850 IS_ALIGNED((unsigned long)ptr, size); \
851 if (IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS) && is_atomic) \
852 return; \
853 check_access(ptr, size, is_atomic ? KCSAN_ACCESS_ATOMIC : 0); \
854 } \
855 EXPORT_SYMBOL(__tsan_volatile_read##size); \
856 void __tsan_unaligned_volatile_read##size(void *ptr) \
857 __alias(__tsan_volatile_read##size); \
858 EXPORT_SYMBOL(__tsan_unaligned_volatile_read##size); \
Marco Elver9dd979b2020-06-16 14:36:22 +0200859 void __tsan_volatile_write##size(void *ptr); \
Marco Elver75d75b72020-05-21 16:20:39 +0200860 void __tsan_volatile_write##size(void *ptr) \
861 { \
862 const bool is_atomic = size <= sizeof(long long) && \
863 IS_ALIGNED((unsigned long)ptr, size); \
864 if (IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS) && is_atomic) \
865 return; \
866 check_access(ptr, size, \
867 KCSAN_ACCESS_WRITE | \
868 (is_atomic ? KCSAN_ACCESS_ATOMIC : 0)); \
869 } \
870 EXPORT_SYMBOL(__tsan_volatile_write##size); \
871 void __tsan_unaligned_volatile_write##size(void *ptr) \
872 __alias(__tsan_volatile_write##size); \
873 EXPORT_SYMBOL(__tsan_unaligned_volatile_write##size)
874
875DEFINE_TSAN_VOLATILE_READ_WRITE(1);
876DEFINE_TSAN_VOLATILE_READ_WRITE(2);
877DEFINE_TSAN_VOLATILE_READ_WRITE(4);
878DEFINE_TSAN_VOLATILE_READ_WRITE(8);
879DEFINE_TSAN_VOLATILE_READ_WRITE(16);
880
881/*
Marco Elverdfd402a2019-11-14 19:02:54 +0100882 * The below are not required by KCSAN, but can still be emitted by the
883 * compiler.
884 */
Marco Elver9dd979b2020-06-16 14:36:22 +0200885void __tsan_func_entry(void *call_pc);
Marco Elverdfd402a2019-11-14 19:02:54 +0100886void __tsan_func_entry(void *call_pc)
887{
888}
889EXPORT_SYMBOL(__tsan_func_entry);
Marco Elver9dd979b2020-06-16 14:36:22 +0200890void __tsan_func_exit(void);
Marco Elverdfd402a2019-11-14 19:02:54 +0100891void __tsan_func_exit(void)
892{
893}
894EXPORT_SYMBOL(__tsan_func_exit);
Marco Elver9dd979b2020-06-16 14:36:22 +0200895void __tsan_init(void);
Marco Elverdfd402a2019-11-14 19:02:54 +0100896void __tsan_init(void)
897{
898}
899EXPORT_SYMBOL(__tsan_init);
Marco Elver0f8ad5f2020-07-03 15:40:29 +0200900
901/*
902 * Instrumentation for atomic builtins (__atomic_*, __sync_*).
903 *
904 * Normal kernel code _should not_ be using them directly, but some
905 * architectures may implement some or all atomics using the compilers'
906 * builtins.
907 *
908 * Note: If an architecture decides to fully implement atomics using the
909 * builtins, because they are implicitly instrumented by KCSAN (and KASAN,
910 * etc.), implementing the ARCH_ATOMIC interface (to get instrumentation via
911 * atomic-instrumented) is no longer necessary.
912 *
913 * TSAN instrumentation replaces atomic accesses with calls to any of the below
914 * functions, whose job is to also execute the operation itself.
915 */
916
917#define DEFINE_TSAN_ATOMIC_LOAD_STORE(bits) \
918 u##bits __tsan_atomic##bits##_load(const u##bits *ptr, int memorder); \
919 u##bits __tsan_atomic##bits##_load(const u##bits *ptr, int memorder) \
920 { \
Marco Elver9d1335c2020-07-24 09:00:04 +0200921 if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) { \
922 check_access(ptr, bits / BITS_PER_BYTE, KCSAN_ACCESS_ATOMIC); \
923 } \
Marco Elver0f8ad5f2020-07-03 15:40:29 +0200924 return __atomic_load_n(ptr, memorder); \
925 } \
926 EXPORT_SYMBOL(__tsan_atomic##bits##_load); \
927 void __tsan_atomic##bits##_store(u##bits *ptr, u##bits v, int memorder); \
928 void __tsan_atomic##bits##_store(u##bits *ptr, u##bits v, int memorder) \
929 { \
Marco Elver9d1335c2020-07-24 09:00:04 +0200930 if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) { \
931 check_access(ptr, bits / BITS_PER_BYTE, \
932 KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC); \
933 } \
Marco Elver0f8ad5f2020-07-03 15:40:29 +0200934 __atomic_store_n(ptr, v, memorder); \
935 } \
936 EXPORT_SYMBOL(__tsan_atomic##bits##_store)
937
938#define DEFINE_TSAN_ATOMIC_RMW(op, bits, suffix) \
939 u##bits __tsan_atomic##bits##_##op(u##bits *ptr, u##bits v, int memorder); \
940 u##bits __tsan_atomic##bits##_##op(u##bits *ptr, u##bits v, int memorder) \
941 { \
Marco Elver9d1335c2020-07-24 09:00:04 +0200942 if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) { \
943 check_access(ptr, bits / BITS_PER_BYTE, \
944 KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | \
945 KCSAN_ACCESS_ATOMIC); \
946 } \
Marco Elver0f8ad5f2020-07-03 15:40:29 +0200947 return __atomic_##op##suffix(ptr, v, memorder); \
948 } \
949 EXPORT_SYMBOL(__tsan_atomic##bits##_##op)
950
951/*
952 * Note: CAS operations are always classified as write, even in case they
953 * fail. We cannot perform check_access() after a write, as it might lead to
954 * false positives, in cases such as:
955 *
956 * T0: __atomic_compare_exchange_n(&p->flag, &old, 1, ...)
957 *
958 * T1: if (__atomic_load_n(&p->flag, ...)) {
959 * modify *p;
960 * p->flag = 0;
961 * }
962 *
963 * The only downside is that, if there are 3 threads, with one CAS that
964 * succeeds, another CAS that fails, and an unmarked racing operation, we may
965 * point at the wrong CAS as the source of the race. However, if we assume that
966 * all CAS can succeed in some other execution, the data race is still valid.
967 */
968#define DEFINE_TSAN_ATOMIC_CMPXCHG(bits, strength, weak) \
969 int __tsan_atomic##bits##_compare_exchange_##strength(u##bits *ptr, u##bits *exp, \
970 u##bits val, int mo, int fail_mo); \
971 int __tsan_atomic##bits##_compare_exchange_##strength(u##bits *ptr, u##bits *exp, \
972 u##bits val, int mo, int fail_mo) \
973 { \
Marco Elver9d1335c2020-07-24 09:00:04 +0200974 if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) { \
975 check_access(ptr, bits / BITS_PER_BYTE, \
976 KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | \
977 KCSAN_ACCESS_ATOMIC); \
978 } \
Marco Elver0f8ad5f2020-07-03 15:40:29 +0200979 return __atomic_compare_exchange_n(ptr, exp, val, weak, mo, fail_mo); \
980 } \
981 EXPORT_SYMBOL(__tsan_atomic##bits##_compare_exchange_##strength)
982
983#define DEFINE_TSAN_ATOMIC_CMPXCHG_VAL(bits) \
984 u##bits __tsan_atomic##bits##_compare_exchange_val(u##bits *ptr, u##bits exp, u##bits val, \
985 int mo, int fail_mo); \
986 u##bits __tsan_atomic##bits##_compare_exchange_val(u##bits *ptr, u##bits exp, u##bits val, \
987 int mo, int fail_mo) \
988 { \
Marco Elver9d1335c2020-07-24 09:00:04 +0200989 if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) { \
990 check_access(ptr, bits / BITS_PER_BYTE, \
991 KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | \
992 KCSAN_ACCESS_ATOMIC); \
993 } \
Marco Elver0f8ad5f2020-07-03 15:40:29 +0200994 __atomic_compare_exchange_n(ptr, &exp, val, 0, mo, fail_mo); \
995 return exp; \
996 } \
997 EXPORT_SYMBOL(__tsan_atomic##bits##_compare_exchange_val)
998
999#define DEFINE_TSAN_ATOMIC_OPS(bits) \
1000 DEFINE_TSAN_ATOMIC_LOAD_STORE(bits); \
1001 DEFINE_TSAN_ATOMIC_RMW(exchange, bits, _n); \
1002 DEFINE_TSAN_ATOMIC_RMW(fetch_add, bits, ); \
1003 DEFINE_TSAN_ATOMIC_RMW(fetch_sub, bits, ); \
1004 DEFINE_TSAN_ATOMIC_RMW(fetch_and, bits, ); \
1005 DEFINE_TSAN_ATOMIC_RMW(fetch_or, bits, ); \
1006 DEFINE_TSAN_ATOMIC_RMW(fetch_xor, bits, ); \
1007 DEFINE_TSAN_ATOMIC_RMW(fetch_nand, bits, ); \
1008 DEFINE_TSAN_ATOMIC_CMPXCHG(bits, strong, 0); \
1009 DEFINE_TSAN_ATOMIC_CMPXCHG(bits, weak, 1); \
1010 DEFINE_TSAN_ATOMIC_CMPXCHG_VAL(bits)
1011
1012DEFINE_TSAN_ATOMIC_OPS(8);
1013DEFINE_TSAN_ATOMIC_OPS(16);
1014DEFINE_TSAN_ATOMIC_OPS(32);
1015DEFINE_TSAN_ATOMIC_OPS(64);
1016
1017void __tsan_atomic_thread_fence(int memorder);
1018void __tsan_atomic_thread_fence(int memorder)
1019{
1020 __atomic_thread_fence(memorder);
1021}
1022EXPORT_SYMBOL(__tsan_atomic_thread_fence);
1023
1024void __tsan_atomic_signal_fence(int memorder);
1025void __tsan_atomic_signal_fence(int memorder) { }
1026EXPORT_SYMBOL(__tsan_atomic_signal_fence);