xref: /openbmc/linux/kernel/kcsan/core.c (revision d088d6b6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * KCSAN core runtime.
4  *
5  * Copyright (C) 2019, Google LLC.
6  */
7 
8 #define pr_fmt(fmt) "kcsan: " fmt
9 
10 #include <linux/atomic.h>
11 #include <linux/bug.h>
12 #include <linux/delay.h>
13 #include <linux/export.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/minmax.h>
18 #include <linux/moduleparam.h>
19 #include <linux/percpu.h>
20 #include <linux/preempt.h>
21 #include <linux/sched.h>
22 #include <linux/string.h>
23 #include <linux/uaccess.h>
24 
25 #include "encoding.h"
26 #include "kcsan.h"
27 #include "permissive.h"
28 
29 static bool kcsan_early_enable = IS_ENABLED(CONFIG_KCSAN_EARLY_ENABLE);
30 unsigned int kcsan_udelay_task = CONFIG_KCSAN_UDELAY_TASK;
31 unsigned int kcsan_udelay_interrupt = CONFIG_KCSAN_UDELAY_INTERRUPT;
32 static long kcsan_skip_watch = CONFIG_KCSAN_SKIP_WATCH;
33 static bool kcsan_interrupt_watcher = IS_ENABLED(CONFIG_KCSAN_INTERRUPT_WATCHER);
34 
35 #ifdef MODULE_PARAM_PREFIX
36 #undef MODULE_PARAM_PREFIX
37 #endif
38 #define MODULE_PARAM_PREFIX "kcsan."
39 module_param_named(early_enable, kcsan_early_enable, bool, 0);
40 module_param_named(udelay_task, kcsan_udelay_task, uint, 0644);
41 module_param_named(udelay_interrupt, kcsan_udelay_interrupt, uint, 0644);
42 module_param_named(skip_watch, kcsan_skip_watch, long, 0644);
43 module_param_named(interrupt_watcher, kcsan_interrupt_watcher, bool, 0444);
44 
45 #ifdef CONFIG_KCSAN_WEAK_MEMORY
46 static bool kcsan_weak_memory = true;
47 module_param_named(weak_memory, kcsan_weak_memory, bool, 0644);
48 #else
49 #define kcsan_weak_memory false
50 #endif
51 
52 bool kcsan_enabled;
53 
54 /* Per-CPU kcsan_ctx for interrupts */
55 static DEFINE_PER_CPU(struct kcsan_ctx, kcsan_cpu_ctx) = {
56 	.scoped_accesses	= {LIST_POISON1, NULL},
57 };
58 
59 /*
60  * Helper macros to index into adjacent slots, starting from address slot
61  * itself, followed by the right and left slots.
62  *
63  * The purpose is 2-fold:
64  *
65  *	1. if during insertion the address slot is already occupied, check if
66  *	   any adjacent slots are free;
67  *	2. accesses that straddle a slot boundary due to size that exceeds a
68  *	   slot's range may check adjacent slots if any watchpoint matches.
69  *
70  * Note that accesses with very large size may still miss a watchpoint; however,
71  * given this should be rare, this is a reasonable trade-off to make, since this
72  * will avoid:
73  *
74  *	1. excessive contention between watchpoint checks and setup;
75  *	2. larger number of simultaneous watchpoints without sacrificing
76  *	   performance.
77  *
78  * Example: SLOT_IDX values for KCSAN_CHECK_ADJACENT=1, where i is [0, 1, 2]:
79  *
80  *   slot=0:  [ 1,  2,  0]
81  *   slot=9:  [10, 11,  9]
82  *   slot=63: [64, 65, 63]
83  */
84 #define SLOT_IDX(slot, i) (slot + ((i + KCSAN_CHECK_ADJACENT) % NUM_SLOTS))
85 
86 /*
87  * SLOT_IDX_FAST is used in the fast-path. Not first checking the address's primary
88  * slot (middle) is fine if we assume that races occur rarely. The set of
89  * indices {SLOT_IDX(slot, i) | i in [0, NUM_SLOTS)} is equivalent to
90  * {SLOT_IDX_FAST(slot, i) | i in [0, NUM_SLOTS)}.
91  */
92 #define SLOT_IDX_FAST(slot, i) (slot + i)
93 
94 /*
95  * Watchpoints, with each entry encoded as defined in encoding.h: in order to be
96  * able to safely update and access a watchpoint without introducing locking
97  * overhead, we encode each watchpoint as a single atomic long. The initial
98  * zero-initialized state matches INVALID_WATCHPOINT.
99  *
100  * Add NUM_SLOTS-1 entries to account for overflow; this helps avoid having to
101  * use more complicated SLOT_IDX_FAST calculation with modulo in the fast-path.
102  */
103 static atomic_long_t watchpoints[CONFIG_KCSAN_NUM_WATCHPOINTS + NUM_SLOTS-1];
104 
105 /*
106  * Instructions to skip watching counter, used in should_watch(). We use a
107  * per-CPU counter to avoid excessive contention.
108  */
109 static DEFINE_PER_CPU(long, kcsan_skip);
110 
111 /* For kcsan_prandom_u32_max(). */
112 static DEFINE_PER_CPU(u32, kcsan_rand_state);
113 
114 static __always_inline atomic_long_t *find_watchpoint(unsigned long addr,
115 						      size_t size,
116 						      bool expect_write,
117 						      long *encoded_watchpoint)
118 {
119 	const int slot = watchpoint_slot(addr);
120 	const unsigned long addr_masked = addr & WATCHPOINT_ADDR_MASK;
121 	atomic_long_t *watchpoint;
122 	unsigned long wp_addr_masked;
123 	size_t wp_size;
124 	bool is_write;
125 	int i;
126 
127 	BUILD_BUG_ON(CONFIG_KCSAN_NUM_WATCHPOINTS < NUM_SLOTS);
128 
129 	for (i = 0; i < NUM_SLOTS; ++i) {
130 		watchpoint = &watchpoints[SLOT_IDX_FAST(slot, i)];
131 		*encoded_watchpoint = atomic_long_read(watchpoint);
132 		if (!decode_watchpoint(*encoded_watchpoint, &wp_addr_masked,
133 				       &wp_size, &is_write))
134 			continue;
135 
136 		if (expect_write && !is_write)
137 			continue;
138 
139 		/* Check if the watchpoint matches the access. */
140 		if (matching_access(wp_addr_masked, wp_size, addr_masked, size))
141 			return watchpoint;
142 	}
143 
144 	return NULL;
145 }
146 
147 static inline atomic_long_t *
148 insert_watchpoint(unsigned long addr, size_t size, bool is_write)
149 {
150 	const int slot = watchpoint_slot(addr);
151 	const long encoded_watchpoint = encode_watchpoint(addr, size, is_write);
152 	atomic_long_t *watchpoint;
153 	int i;
154 
155 	/* Check slot index logic, ensuring we stay within array bounds. */
156 	BUILD_BUG_ON(SLOT_IDX(0, 0) != KCSAN_CHECK_ADJACENT);
157 	BUILD_BUG_ON(SLOT_IDX(0, KCSAN_CHECK_ADJACENT+1) != 0);
158 	BUILD_BUG_ON(SLOT_IDX(CONFIG_KCSAN_NUM_WATCHPOINTS-1, KCSAN_CHECK_ADJACENT) != ARRAY_SIZE(watchpoints)-1);
159 	BUILD_BUG_ON(SLOT_IDX(CONFIG_KCSAN_NUM_WATCHPOINTS-1, KCSAN_CHECK_ADJACENT+1) != ARRAY_SIZE(watchpoints) - NUM_SLOTS);
160 
161 	for (i = 0; i < NUM_SLOTS; ++i) {
162 		long expect_val = INVALID_WATCHPOINT;
163 
164 		/* Try to acquire this slot. */
165 		watchpoint = &watchpoints[SLOT_IDX(slot, i)];
166 		if (atomic_long_try_cmpxchg_relaxed(watchpoint, &expect_val, encoded_watchpoint))
167 			return watchpoint;
168 	}
169 
170 	return NULL;
171 }
172 
173 /*
174  * Return true if watchpoint was successfully consumed, false otherwise.
175  *
176  * This may return false if:
177  *
178  *	1. another thread already consumed the watchpoint;
179  *	2. the thread that set up the watchpoint already removed it;
180  *	3. the watchpoint was removed and then re-used.
181  */
182 static __always_inline bool
183 try_consume_watchpoint(atomic_long_t *watchpoint, long encoded_watchpoint)
184 {
185 	return atomic_long_try_cmpxchg_relaxed(watchpoint, &encoded_watchpoint, CONSUMED_WATCHPOINT);
186 }
187 
188 /* Return true if watchpoint was not touched, false if already consumed. */
189 static inline bool consume_watchpoint(atomic_long_t *watchpoint)
190 {
191 	return atomic_long_xchg_relaxed(watchpoint, CONSUMED_WATCHPOINT) != CONSUMED_WATCHPOINT;
192 }
193 
194 /* Remove the watchpoint -- its slot may be reused after. */
195 static inline void remove_watchpoint(atomic_long_t *watchpoint)
196 {
197 	atomic_long_set(watchpoint, INVALID_WATCHPOINT);
198 }
199 
200 static __always_inline struct kcsan_ctx *get_ctx(void)
201 {
202 	/*
203 	 * In interrupts, use raw_cpu_ptr to avoid unnecessary checks, that would
204 	 * also result in calls that generate warnings in uaccess regions.
205 	 */
206 	return in_task() ? &current->kcsan_ctx : raw_cpu_ptr(&kcsan_cpu_ctx);
207 }
208 
209 static __always_inline void
210 check_access(const volatile void *ptr, size_t size, int type, unsigned long ip);
211 
212 /* Check scoped accesses; never inline because this is a slow-path! */
213 static noinline void kcsan_check_scoped_accesses(void)
214 {
215 	struct kcsan_ctx *ctx = get_ctx();
216 	struct kcsan_scoped_access *scoped_access;
217 
218 	if (ctx->disable_scoped)
219 		return;
220 
221 	ctx->disable_scoped++;
222 	list_for_each_entry(scoped_access, &ctx->scoped_accesses, list) {
223 		check_access(scoped_access->ptr, scoped_access->size,
224 			     scoped_access->type, scoped_access->ip);
225 	}
226 	ctx->disable_scoped--;
227 }
228 
229 /* Rules for generic atomic accesses. Called from fast-path. */
230 static __always_inline bool
231 is_atomic(struct kcsan_ctx *ctx, const volatile void *ptr, size_t size, int type)
232 {
233 	if (type & KCSAN_ACCESS_ATOMIC)
234 		return true;
235 
236 	/*
237 	 * Unless explicitly declared atomic, never consider an assertion access
238 	 * as atomic. This allows using them also in atomic regions, such as
239 	 * seqlocks, without implicitly changing their semantics.
240 	 */
241 	if (type & KCSAN_ACCESS_ASSERT)
242 		return false;
243 
244 	if (IS_ENABLED(CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC) &&
245 	    (type & KCSAN_ACCESS_WRITE) && size <= sizeof(long) &&
246 	    !(type & KCSAN_ACCESS_COMPOUND) && IS_ALIGNED((unsigned long)ptr, size))
247 		return true; /* Assume aligned writes up to word size are atomic. */
248 
249 	if (ctx->atomic_next > 0) {
250 		/*
251 		 * Because we do not have separate contexts for nested
252 		 * interrupts, in case atomic_next is set, we simply assume that
253 		 * the outer interrupt set atomic_next. In the worst case, we
254 		 * will conservatively consider operations as atomic. This is a
255 		 * reasonable trade-off to make, since this case should be
256 		 * extremely rare; however, even if extremely rare, it could
257 		 * lead to false positives otherwise.
258 		 */
259 		if ((hardirq_count() >> HARDIRQ_SHIFT) < 2)
260 			--ctx->atomic_next; /* in task, or outer interrupt */
261 		return true;
262 	}
263 
264 	return ctx->atomic_nest_count > 0 || ctx->in_flat_atomic;
265 }
266 
267 static __always_inline bool
268 should_watch(struct kcsan_ctx *ctx, const volatile void *ptr, size_t size, int type)
269 {
270 	/*
271 	 * Never set up watchpoints when memory operations are atomic.
272 	 *
273 	 * Need to check this first, before kcsan_skip check below: (1) atomics
274 	 * should not count towards skipped instructions, and (2) to actually
275 	 * decrement kcsan_atomic_next for consecutive instruction stream.
276 	 */
277 	if (is_atomic(ctx, ptr, size, type))
278 		return false;
279 
280 	if (this_cpu_dec_return(kcsan_skip) >= 0)
281 		return false;
282 
283 	/*
284 	 * NOTE: If we get here, kcsan_skip must always be reset in slow path
285 	 * via reset_kcsan_skip() to avoid underflow.
286 	 */
287 
288 	/* this operation should be watched */
289 	return true;
290 }
291 
292 /*
293  * Returns a pseudo-random number in interval [0, ep_ro). Simple linear
294  * congruential generator, using constants from "Numerical Recipes".
295  */
296 static u32 kcsan_prandom_u32_max(u32 ep_ro)
297 {
298 	u32 state = this_cpu_read(kcsan_rand_state);
299 
300 	state = 1664525 * state + 1013904223;
301 	this_cpu_write(kcsan_rand_state, state);
302 
303 	return state % ep_ro;
304 }
305 
306 static inline void reset_kcsan_skip(void)
307 {
308 	long skip_count = kcsan_skip_watch -
309 			  (IS_ENABLED(CONFIG_KCSAN_SKIP_WATCH_RANDOMIZE) ?
310 				   kcsan_prandom_u32_max(kcsan_skip_watch) :
311 				   0);
312 	this_cpu_write(kcsan_skip, skip_count);
313 }
314 
315 static __always_inline bool kcsan_is_enabled(struct kcsan_ctx *ctx)
316 {
317 	return READ_ONCE(kcsan_enabled) && !ctx->disable_count;
318 }
319 
320 /* Introduce delay depending on context and configuration. */
321 static void delay_access(int type)
322 {
323 	unsigned int delay = in_task() ? kcsan_udelay_task : kcsan_udelay_interrupt;
324 	/* For certain access types, skew the random delay to be longer. */
325 	unsigned int skew_delay_order =
326 		(type & (KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_ASSERT)) ? 1 : 0;
327 
328 	delay -= IS_ENABLED(CONFIG_KCSAN_DELAY_RANDOMIZE) ?
329 			       kcsan_prandom_u32_max(delay >> skew_delay_order) :
330 			       0;
331 	udelay(delay);
332 }
333 
334 /*
335  * Reads the instrumented memory for value change detection; value change
336  * detection is currently done for accesses up to a size of 8 bytes.
337  */
338 static __always_inline u64 read_instrumented_memory(const volatile void *ptr, size_t size)
339 {
340 	/*
341 	 * In the below we don't necessarily need the read of the location to
342 	 * be atomic, and we don't use READ_ONCE(), since all we need for race
343 	 * detection is to observe 2 different values.
344 	 *
345 	 * Furthermore, on certain architectures (such as arm64), READ_ONCE()
346 	 * may turn into more complex instructions than a plain load that cannot
347 	 * do unaligned accesses.
348 	 */
349 	switch (size) {
350 	case 1:  return *(const volatile u8 *)ptr;
351 	case 2:  return *(const volatile u16 *)ptr;
352 	case 4:  return *(const volatile u32 *)ptr;
353 	case 8:  return *(const volatile u64 *)ptr;
354 	default: return 0; /* Ignore; we do not diff the values. */
355 	}
356 }
357 
358 void kcsan_save_irqtrace(struct task_struct *task)
359 {
360 #ifdef CONFIG_TRACE_IRQFLAGS
361 	task->kcsan_save_irqtrace = task->irqtrace;
362 #endif
363 }
364 
365 void kcsan_restore_irqtrace(struct task_struct *task)
366 {
367 #ifdef CONFIG_TRACE_IRQFLAGS
368 	task->irqtrace = task->kcsan_save_irqtrace;
369 #endif
370 }
371 
372 static __always_inline int get_kcsan_stack_depth(void)
373 {
374 #ifdef CONFIG_KCSAN_WEAK_MEMORY
375 	return current->kcsan_stack_depth;
376 #else
377 	BUILD_BUG();
378 	return 0;
379 #endif
380 }
381 
382 static __always_inline void add_kcsan_stack_depth(int val)
383 {
384 #ifdef CONFIG_KCSAN_WEAK_MEMORY
385 	current->kcsan_stack_depth += val;
386 #else
387 	BUILD_BUG();
388 #endif
389 }
390 
391 static __always_inline struct kcsan_scoped_access *get_reorder_access(struct kcsan_ctx *ctx)
392 {
393 #ifdef CONFIG_KCSAN_WEAK_MEMORY
394 	return ctx->disable_scoped ? NULL : &ctx->reorder_access;
395 #else
396 	return NULL;
397 #endif
398 }
399 
400 static __always_inline bool
401 find_reorder_access(struct kcsan_ctx *ctx, const volatile void *ptr, size_t size,
402 		    int type, unsigned long ip)
403 {
404 	struct kcsan_scoped_access *reorder_access = get_reorder_access(ctx);
405 
406 	if (!reorder_access)
407 		return false;
408 
409 	/*
410 	 * Note: If accesses are repeated while reorder_access is identical,
411 	 * never matches the new access, because !(type & KCSAN_ACCESS_SCOPED).
412 	 */
413 	return reorder_access->ptr == ptr && reorder_access->size == size &&
414 	       reorder_access->type == type && reorder_access->ip == ip;
415 }
416 
417 static inline void
418 set_reorder_access(struct kcsan_ctx *ctx, const volatile void *ptr, size_t size,
419 		   int type, unsigned long ip)
420 {
421 	struct kcsan_scoped_access *reorder_access = get_reorder_access(ctx);
422 
423 	if (!reorder_access || !kcsan_weak_memory)
424 		return;
425 
426 	/*
427 	 * To avoid nested interrupts or scheduler (which share kcsan_ctx)
428 	 * reading an inconsistent reorder_access, ensure that the below has
429 	 * exclusive access to reorder_access by disallowing concurrent use.
430 	 */
431 	ctx->disable_scoped++;
432 	barrier();
433 	reorder_access->ptr		= ptr;
434 	reorder_access->size		= size;
435 	reorder_access->type		= type | KCSAN_ACCESS_SCOPED;
436 	reorder_access->ip		= ip;
437 	reorder_access->stack_depth	= get_kcsan_stack_depth();
438 	barrier();
439 	ctx->disable_scoped--;
440 }
441 
442 /*
443  * Pull everything together: check_access() below contains the performance
444  * critical operations; the fast-path (including check_access) functions should
445  * all be inlinable by the instrumentation functions.
446  *
447  * The slow-path (kcsan_found_watchpoint, kcsan_setup_watchpoint) are
448  * non-inlinable -- note that, we prefix these with "kcsan_" to ensure they can
449  * be filtered from the stacktrace, as well as give them unique names for the
450  * UACCESS whitelist of objtool. Each function uses user_access_save/restore(),
451  * since they do not access any user memory, but instrumentation is still
452  * emitted in UACCESS regions.
453  */
454 
455 static noinline void kcsan_found_watchpoint(const volatile void *ptr,
456 					    size_t size,
457 					    int type,
458 					    unsigned long ip,
459 					    atomic_long_t *watchpoint,
460 					    long encoded_watchpoint)
461 {
462 	const bool is_assert = (type & KCSAN_ACCESS_ASSERT) != 0;
463 	struct kcsan_ctx *ctx = get_ctx();
464 	unsigned long flags;
465 	bool consumed;
466 
467 	/*
468 	 * We know a watchpoint exists. Let's try to keep the race-window
469 	 * between here and finally consuming the watchpoint below as small as
470 	 * possible -- avoid unneccessarily complex code until consumed.
471 	 */
472 
473 	if (!kcsan_is_enabled(ctx))
474 		return;
475 
476 	/*
477 	 * The access_mask check relies on value-change comparison. To avoid
478 	 * reporting a race where e.g. the writer set up the watchpoint, but the
479 	 * reader has access_mask!=0, we have to ignore the found watchpoint.
480 	 *
481 	 * reorder_access is never created from an access with access_mask set.
482 	 */
483 	if (ctx->access_mask && !find_reorder_access(ctx, ptr, size, type, ip))
484 		return;
485 
486 	/*
487 	 * If the other thread does not want to ignore the access, and there was
488 	 * a value change as a result of this thread's operation, we will still
489 	 * generate a report of unknown origin.
490 	 *
491 	 * Use CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=n to filter.
492 	 */
493 	if (!is_assert && kcsan_ignore_address(ptr))
494 		return;
495 
496 	/*
497 	 * Consuming the watchpoint must be guarded by kcsan_is_enabled() to
498 	 * avoid erroneously triggering reports if the context is disabled.
499 	 */
500 	consumed = try_consume_watchpoint(watchpoint, encoded_watchpoint);
501 
502 	/* keep this after try_consume_watchpoint */
503 	flags = user_access_save();
504 
505 	if (consumed) {
506 		kcsan_save_irqtrace(current);
507 		kcsan_report_set_info(ptr, size, type, ip, watchpoint - watchpoints);
508 		kcsan_restore_irqtrace(current);
509 	} else {
510 		/*
511 		 * The other thread may not print any diagnostics, as it has
512 		 * already removed the watchpoint, or another thread consumed
513 		 * the watchpoint before this thread.
514 		 */
515 		atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_REPORT_RACES]);
516 	}
517 
518 	if (is_assert)
519 		atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]);
520 	else
521 		atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_DATA_RACES]);
522 
523 	user_access_restore(flags);
524 }
525 
526 static noinline void
527 kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type, unsigned long ip)
528 {
529 	const bool is_write = (type & KCSAN_ACCESS_WRITE) != 0;
530 	const bool is_assert = (type & KCSAN_ACCESS_ASSERT) != 0;
531 	atomic_long_t *watchpoint;
532 	u64 old, new, diff;
533 	enum kcsan_value_change value_change = KCSAN_VALUE_CHANGE_MAYBE;
534 	bool interrupt_watcher = kcsan_interrupt_watcher;
535 	unsigned long ua_flags = user_access_save();
536 	struct kcsan_ctx *ctx = get_ctx();
537 	unsigned long access_mask = ctx->access_mask;
538 	unsigned long irq_flags = 0;
539 	bool is_reorder_access;
540 
541 	/*
542 	 * Always reset kcsan_skip counter in slow-path to avoid underflow; see
543 	 * should_watch().
544 	 */
545 	reset_kcsan_skip();
546 
547 	if (!kcsan_is_enabled(ctx))
548 		goto out;
549 
550 	/*
551 	 * Check to-ignore addresses after kcsan_is_enabled(), as we may access
552 	 * memory that is not yet initialized during early boot.
553 	 */
554 	if (!is_assert && kcsan_ignore_address(ptr))
555 		goto out;
556 
557 	if (!check_encodable((unsigned long)ptr, size)) {
558 		atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_UNENCODABLE_ACCESSES]);
559 		goto out;
560 	}
561 
562 	/*
563 	 * The local CPU cannot observe reordering of its own accesses, and
564 	 * therefore we need to take care of 2 cases to avoid false positives:
565 	 *
566 	 *	1. Races of the reordered access with interrupts. To avoid, if
567 	 *	   the current access is reorder_access, disable interrupts.
568 	 *	2. Avoid races of scoped accesses from nested interrupts (below).
569 	 */
570 	is_reorder_access = find_reorder_access(ctx, ptr, size, type, ip);
571 	if (is_reorder_access)
572 		interrupt_watcher = false;
573 	/*
574 	 * Avoid races of scoped accesses from nested interrupts (or scheduler).
575 	 * Assume setting up a watchpoint for a non-scoped (normal) access that
576 	 * also conflicts with a current scoped access. In a nested interrupt,
577 	 * which shares the context, it would check a conflicting scoped access.
578 	 * To avoid, disable scoped access checking.
579 	 */
580 	ctx->disable_scoped++;
581 
582 	/*
583 	 * Save and restore the IRQ state trace touched by KCSAN, since KCSAN's
584 	 * runtime is entered for every memory access, and potentially useful
585 	 * information is lost if dirtied by KCSAN.
586 	 */
587 	kcsan_save_irqtrace(current);
588 	if (!interrupt_watcher)
589 		local_irq_save(irq_flags);
590 
591 	watchpoint = insert_watchpoint((unsigned long)ptr, size, is_write);
592 	if (watchpoint == NULL) {
593 		/*
594 		 * Out of capacity: the size of 'watchpoints', and the frequency
595 		 * with which should_watch() returns true should be tweaked so
596 		 * that this case happens very rarely.
597 		 */
598 		atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_NO_CAPACITY]);
599 		goto out_unlock;
600 	}
601 
602 	atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_SETUP_WATCHPOINTS]);
603 	atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_USED_WATCHPOINTS]);
604 
605 	/*
606 	 * Read the current value, to later check and infer a race if the data
607 	 * was modified via a non-instrumented access, e.g. from a device.
608 	 */
609 	old = is_reorder_access ? 0 : read_instrumented_memory(ptr, size);
610 
611 	/*
612 	 * Delay this thread, to increase probability of observing a racy
613 	 * conflicting access.
614 	 */
615 	delay_access(type);
616 
617 	/*
618 	 * Re-read value, and check if it is as expected; if not, we infer a
619 	 * racy access.
620 	 */
621 	if (!is_reorder_access) {
622 		new = read_instrumented_memory(ptr, size);
623 	} else {
624 		/*
625 		 * Reordered accesses cannot be used for value change detection,
626 		 * because the memory location may no longer be accessible and
627 		 * could result in a fault.
628 		 */
629 		new = 0;
630 		access_mask = 0;
631 	}
632 
633 	diff = old ^ new;
634 	if (access_mask)
635 		diff &= access_mask;
636 
637 	/*
638 	 * Check if we observed a value change.
639 	 *
640 	 * Also check if the data race should be ignored (the rules depend on
641 	 * non-zero diff); if it is to be ignored, the below rules for
642 	 * KCSAN_VALUE_CHANGE_MAYBE apply.
643 	 */
644 	if (diff && !kcsan_ignore_data_race(size, type, old, new, diff))
645 		value_change = KCSAN_VALUE_CHANGE_TRUE;
646 
647 	/* Check if this access raced with another. */
648 	if (!consume_watchpoint(watchpoint)) {
649 		/*
650 		 * Depending on the access type, map a value_change of MAYBE to
651 		 * TRUE (always report) or FALSE (never report).
652 		 */
653 		if (value_change == KCSAN_VALUE_CHANGE_MAYBE) {
654 			if (access_mask != 0) {
655 				/*
656 				 * For access with access_mask, we require a
657 				 * value-change, as it is likely that races on
658 				 * ~access_mask bits are expected.
659 				 */
660 				value_change = KCSAN_VALUE_CHANGE_FALSE;
661 			} else if (size > 8 || is_assert) {
662 				/* Always assume a value-change. */
663 				value_change = KCSAN_VALUE_CHANGE_TRUE;
664 			}
665 		}
666 
667 		/*
668 		 * No need to increment 'data_races' counter, as the racing
669 		 * thread already did.
670 		 *
671 		 * Count 'assert_failures' for each failed ASSERT access,
672 		 * therefore both this thread and the racing thread may
673 		 * increment this counter.
674 		 */
675 		if (is_assert && value_change == KCSAN_VALUE_CHANGE_TRUE)
676 			atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]);
677 
678 		kcsan_report_known_origin(ptr, size, type, ip,
679 					  value_change, watchpoint - watchpoints,
680 					  old, new, access_mask);
681 	} else if (value_change == KCSAN_VALUE_CHANGE_TRUE) {
682 		/* Inferring a race, since the value should not have changed. */
683 
684 		atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN]);
685 		if (is_assert)
686 			atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]);
687 
688 		if (IS_ENABLED(CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN) || is_assert) {
689 			kcsan_report_unknown_origin(ptr, size, type, ip,
690 						    old, new, access_mask);
691 		}
692 	}
693 
694 	/*
695 	 * Remove watchpoint; must be after reporting, since the slot may be
696 	 * reused after this point.
697 	 */
698 	remove_watchpoint(watchpoint);
699 	atomic_long_dec(&kcsan_counters[KCSAN_COUNTER_USED_WATCHPOINTS]);
700 
701 out_unlock:
702 	if (!interrupt_watcher)
703 		local_irq_restore(irq_flags);
704 	kcsan_restore_irqtrace(current);
705 	ctx->disable_scoped--;
706 
707 	/*
708 	 * Reordered accesses cannot be used for value change detection,
709 	 * therefore never consider for reordering if access_mask is set.
710 	 * ASSERT_EXCLUSIVE are not real accesses, ignore them as well.
711 	 */
712 	if (!access_mask && !is_assert)
713 		set_reorder_access(ctx, ptr, size, type, ip);
714 out:
715 	user_access_restore(ua_flags);
716 }
717 
718 static __always_inline void
719 check_access(const volatile void *ptr, size_t size, int type, unsigned long ip)
720 {
721 	atomic_long_t *watchpoint;
722 	long encoded_watchpoint;
723 
724 	/*
725 	 * Do nothing for 0 sized check; this comparison will be optimized out
726 	 * for constant sized instrumentation (__tsan_{read,write}N).
727 	 */
728 	if (unlikely(size == 0))
729 		return;
730 
731 again:
732 	/*
733 	 * Avoid user_access_save in fast-path: find_watchpoint is safe without
734 	 * user_access_save, as the address that ptr points to is only used to
735 	 * check if a watchpoint exists; ptr is never dereferenced.
736 	 */
737 	watchpoint = find_watchpoint((unsigned long)ptr, size,
738 				     !(type & KCSAN_ACCESS_WRITE),
739 				     &encoded_watchpoint);
740 	/*
741 	 * It is safe to check kcsan_is_enabled() after find_watchpoint in the
742 	 * slow-path, as long as no state changes that cause a race to be
743 	 * detected and reported have occurred until kcsan_is_enabled() is
744 	 * checked.
745 	 */
746 
747 	if (unlikely(watchpoint != NULL))
748 		kcsan_found_watchpoint(ptr, size, type, ip, watchpoint, encoded_watchpoint);
749 	else {
750 		struct kcsan_ctx *ctx = get_ctx(); /* Call only once in fast-path. */
751 
752 		if (unlikely(should_watch(ctx, ptr, size, type))) {
753 			kcsan_setup_watchpoint(ptr, size, type, ip);
754 			return;
755 		}
756 
757 		if (!(type & KCSAN_ACCESS_SCOPED)) {
758 			struct kcsan_scoped_access *reorder_access = get_reorder_access(ctx);
759 
760 			if (reorder_access) {
761 				/*
762 				 * reorder_access check: simulates reordering of
763 				 * the access after subsequent operations.
764 				 */
765 				ptr = reorder_access->ptr;
766 				type = reorder_access->type;
767 				ip = reorder_access->ip;
768 				/*
769 				 * Upon a nested interrupt, this context's
770 				 * reorder_access can be modified (shared ctx).
771 				 * We know that upon return, reorder_access is
772 				 * always invalidated by setting size to 0 via
773 				 * __tsan_func_exit(). Therefore we must read
774 				 * and check size after the other fields.
775 				 */
776 				barrier();
777 				size = READ_ONCE(reorder_access->size);
778 				if (size)
779 					goto again;
780 			}
781 		}
782 
783 		/*
784 		 * Always checked last, right before returning from runtime;
785 		 * if reorder_access is valid, checked after it was checked.
786 		 */
787 		if (unlikely(ctx->scoped_accesses.prev))
788 			kcsan_check_scoped_accesses();
789 	}
790 }
791 
792 /* === Public interface ===================================================== */
793 
794 void __init kcsan_init(void)
795 {
796 	int cpu;
797 
798 	BUG_ON(!in_task());
799 
800 	for_each_possible_cpu(cpu)
801 		per_cpu(kcsan_rand_state, cpu) = (u32)get_cycles();
802 
803 	/*
804 	 * We are in the init task, and no other tasks should be running;
805 	 * WRITE_ONCE without memory barrier is sufficient.
806 	 */
807 	if (kcsan_early_enable) {
808 		pr_info("enabled early\n");
809 		WRITE_ONCE(kcsan_enabled, true);
810 	}
811 
812 	if (IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY) ||
813 	    IS_ENABLED(CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC) ||
814 	    IS_ENABLED(CONFIG_KCSAN_PERMISSIVE) ||
815 	    IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) {
816 		pr_warn("non-strict mode configured - use CONFIG_KCSAN_STRICT=y to see all data races\n");
817 	} else {
818 		pr_info("strict mode configured\n");
819 	}
820 }
821 
822 /* === Exported interface =================================================== */
823 
824 void kcsan_disable_current(void)
825 {
826 	++get_ctx()->disable_count;
827 }
828 EXPORT_SYMBOL(kcsan_disable_current);
829 
830 void kcsan_enable_current(void)
831 {
832 	if (get_ctx()->disable_count-- == 0) {
833 		/*
834 		 * Warn if kcsan_enable_current() calls are unbalanced with
835 		 * kcsan_disable_current() calls, which causes disable_count to
836 		 * become negative and should not happen.
837 		 */
838 		kcsan_disable_current(); /* restore to 0, KCSAN still enabled */
839 		kcsan_disable_current(); /* disable to generate warning */
840 		WARN(1, "Unbalanced %s()", __func__);
841 		kcsan_enable_current();
842 	}
843 }
844 EXPORT_SYMBOL(kcsan_enable_current);
845 
846 void kcsan_enable_current_nowarn(void)
847 {
848 	if (get_ctx()->disable_count-- == 0)
849 		kcsan_disable_current();
850 }
851 EXPORT_SYMBOL(kcsan_enable_current_nowarn);
852 
853 void kcsan_nestable_atomic_begin(void)
854 {
855 	/*
856 	 * Do *not* check and warn if we are in a flat atomic region: nestable
857 	 * and flat atomic regions are independent from each other.
858 	 * See include/linux/kcsan.h: struct kcsan_ctx comments for more
859 	 * comments.
860 	 */
861 
862 	++get_ctx()->atomic_nest_count;
863 }
864 EXPORT_SYMBOL(kcsan_nestable_atomic_begin);
865 
866 void kcsan_nestable_atomic_end(void)
867 {
868 	if (get_ctx()->atomic_nest_count-- == 0) {
869 		/*
870 		 * Warn if kcsan_nestable_atomic_end() calls are unbalanced with
871 		 * kcsan_nestable_atomic_begin() calls, which causes
872 		 * atomic_nest_count to become negative and should not happen.
873 		 */
874 		kcsan_nestable_atomic_begin(); /* restore to 0 */
875 		kcsan_disable_current(); /* disable to generate warning */
876 		WARN(1, "Unbalanced %s()", __func__);
877 		kcsan_enable_current();
878 	}
879 }
880 EXPORT_SYMBOL(kcsan_nestable_atomic_end);
881 
882 void kcsan_flat_atomic_begin(void)
883 {
884 	get_ctx()->in_flat_atomic = true;
885 }
886 EXPORT_SYMBOL(kcsan_flat_atomic_begin);
887 
888 void kcsan_flat_atomic_end(void)
889 {
890 	get_ctx()->in_flat_atomic = false;
891 }
892 EXPORT_SYMBOL(kcsan_flat_atomic_end);
893 
894 void kcsan_atomic_next(int n)
895 {
896 	get_ctx()->atomic_next = n;
897 }
898 EXPORT_SYMBOL(kcsan_atomic_next);
899 
900 void kcsan_set_access_mask(unsigned long mask)
901 {
902 	get_ctx()->access_mask = mask;
903 }
904 EXPORT_SYMBOL(kcsan_set_access_mask);
905 
906 struct kcsan_scoped_access *
907 kcsan_begin_scoped_access(const volatile void *ptr, size_t size, int type,
908 			  struct kcsan_scoped_access *sa)
909 {
910 	struct kcsan_ctx *ctx = get_ctx();
911 
912 	check_access(ptr, size, type, _RET_IP_);
913 
914 	ctx->disable_count++; /* Disable KCSAN, in case list debugging is on. */
915 
916 	INIT_LIST_HEAD(&sa->list);
917 	sa->ptr = ptr;
918 	sa->size = size;
919 	sa->type = type;
920 	sa->ip = _RET_IP_;
921 
922 	if (!ctx->scoped_accesses.prev) /* Lazy initialize list head. */
923 		INIT_LIST_HEAD(&ctx->scoped_accesses);
924 	list_add(&sa->list, &ctx->scoped_accesses);
925 
926 	ctx->disable_count--;
927 	return sa;
928 }
929 EXPORT_SYMBOL(kcsan_begin_scoped_access);
930 
931 void kcsan_end_scoped_access(struct kcsan_scoped_access *sa)
932 {
933 	struct kcsan_ctx *ctx = get_ctx();
934 
935 	if (WARN(!ctx->scoped_accesses.prev, "Unbalanced %s()?", __func__))
936 		return;
937 
938 	ctx->disable_count++; /* Disable KCSAN, in case list debugging is on. */
939 
940 	list_del(&sa->list);
941 	if (list_empty(&ctx->scoped_accesses))
942 		/*
943 		 * Ensure we do not enter kcsan_check_scoped_accesses()
944 		 * slow-path if unnecessary, and avoids requiring list_empty()
945 		 * in the fast-path (to avoid a READ_ONCE() and potential
946 		 * uaccess warning).
947 		 */
948 		ctx->scoped_accesses.prev = NULL;
949 
950 	ctx->disable_count--;
951 
952 	check_access(sa->ptr, sa->size, sa->type, sa->ip);
953 }
954 EXPORT_SYMBOL(kcsan_end_scoped_access);
955 
956 void __kcsan_check_access(const volatile void *ptr, size_t size, int type)
957 {
958 	check_access(ptr, size, type, _RET_IP_);
959 }
960 EXPORT_SYMBOL(__kcsan_check_access);
961 
962 #define DEFINE_MEMORY_BARRIER(name, order_before_cond)				\
963 	void __kcsan_##name(void)						\
964 	{									\
965 		struct kcsan_scoped_access *sa = get_reorder_access(get_ctx());	\
966 		if (!sa)							\
967 			return;							\
968 		if (order_before_cond)						\
969 			sa->size = 0;						\
970 	}									\
971 	EXPORT_SYMBOL(__kcsan_##name)
972 
973 DEFINE_MEMORY_BARRIER(mb, true);
974 DEFINE_MEMORY_BARRIER(wmb, sa->type & (KCSAN_ACCESS_WRITE | KCSAN_ACCESS_COMPOUND));
975 DEFINE_MEMORY_BARRIER(rmb, !(sa->type & KCSAN_ACCESS_WRITE) || (sa->type & KCSAN_ACCESS_COMPOUND));
976 DEFINE_MEMORY_BARRIER(release, true);
977 
978 /*
979  * KCSAN uses the same instrumentation that is emitted by supported compilers
980  * for ThreadSanitizer (TSAN).
981  *
982  * When enabled, the compiler emits instrumentation calls (the functions
983  * prefixed with "__tsan" below) for all loads and stores that it generated;
984  * inline asm is not instrumented.
985  *
986  * Note that, not all supported compiler versions distinguish aligned/unaligned
987  * accesses, but e.g. recent versions of Clang do. We simply alias the unaligned
988  * version to the generic version, which can handle both.
989  */
990 
991 #define DEFINE_TSAN_READ_WRITE(size)                                           \
992 	void __tsan_read##size(void *ptr);                                     \
993 	void __tsan_read##size(void *ptr)                                      \
994 	{                                                                      \
995 		check_access(ptr, size, 0, _RET_IP_);                          \
996 	}                                                                      \
997 	EXPORT_SYMBOL(__tsan_read##size);                                      \
998 	void __tsan_unaligned_read##size(void *ptr)                            \
999 		__alias(__tsan_read##size);                                    \
1000 	EXPORT_SYMBOL(__tsan_unaligned_read##size);                            \
1001 	void __tsan_write##size(void *ptr);                                    \
1002 	void __tsan_write##size(void *ptr)                                     \
1003 	{                                                                      \
1004 		check_access(ptr, size, KCSAN_ACCESS_WRITE, _RET_IP_);         \
1005 	}                                                                      \
1006 	EXPORT_SYMBOL(__tsan_write##size);                                     \
1007 	void __tsan_unaligned_write##size(void *ptr)                           \
1008 		__alias(__tsan_write##size);                                   \
1009 	EXPORT_SYMBOL(__tsan_unaligned_write##size);                           \
1010 	void __tsan_read_write##size(void *ptr);                               \
1011 	void __tsan_read_write##size(void *ptr)                                \
1012 	{                                                                      \
1013 		check_access(ptr, size,                                        \
1014 			     KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE,       \
1015 			     _RET_IP_);                                        \
1016 	}                                                                      \
1017 	EXPORT_SYMBOL(__tsan_read_write##size);                                \
1018 	void __tsan_unaligned_read_write##size(void *ptr)                      \
1019 		__alias(__tsan_read_write##size);                              \
1020 	EXPORT_SYMBOL(__tsan_unaligned_read_write##size)
1021 
1022 DEFINE_TSAN_READ_WRITE(1);
1023 DEFINE_TSAN_READ_WRITE(2);
1024 DEFINE_TSAN_READ_WRITE(4);
1025 DEFINE_TSAN_READ_WRITE(8);
1026 DEFINE_TSAN_READ_WRITE(16);
1027 
1028 void __tsan_read_range(void *ptr, size_t size);
1029 void __tsan_read_range(void *ptr, size_t size)
1030 {
1031 	check_access(ptr, size, 0, _RET_IP_);
1032 }
1033 EXPORT_SYMBOL(__tsan_read_range);
1034 
1035 void __tsan_write_range(void *ptr, size_t size);
1036 void __tsan_write_range(void *ptr, size_t size)
1037 {
1038 	check_access(ptr, size, KCSAN_ACCESS_WRITE, _RET_IP_);
1039 }
1040 EXPORT_SYMBOL(__tsan_write_range);
1041 
1042 /*
1043  * Use of explicit volatile is generally disallowed [1], however, volatile is
1044  * still used in various concurrent context, whether in low-level
1045  * synchronization primitives or for legacy reasons.
1046  * [1] https://lwn.net/Articles/233479/
1047  *
1048  * We only consider volatile accesses atomic if they are aligned and would pass
1049  * the size-check of compiletime_assert_rwonce_type().
1050  */
1051 #define DEFINE_TSAN_VOLATILE_READ_WRITE(size)                                  \
1052 	void __tsan_volatile_read##size(void *ptr);                            \
1053 	void __tsan_volatile_read##size(void *ptr)                             \
1054 	{                                                                      \
1055 		const bool is_atomic = size <= sizeof(long long) &&            \
1056 				       IS_ALIGNED((unsigned long)ptr, size);   \
1057 		if (IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS) && is_atomic)      \
1058 			return;                                                \
1059 		check_access(ptr, size, is_atomic ? KCSAN_ACCESS_ATOMIC : 0,   \
1060 			     _RET_IP_);                                        \
1061 	}                                                                      \
1062 	EXPORT_SYMBOL(__tsan_volatile_read##size);                             \
1063 	void __tsan_unaligned_volatile_read##size(void *ptr)                   \
1064 		__alias(__tsan_volatile_read##size);                           \
1065 	EXPORT_SYMBOL(__tsan_unaligned_volatile_read##size);                   \
1066 	void __tsan_volatile_write##size(void *ptr);                           \
1067 	void __tsan_volatile_write##size(void *ptr)                            \
1068 	{                                                                      \
1069 		const bool is_atomic = size <= sizeof(long long) &&            \
1070 				       IS_ALIGNED((unsigned long)ptr, size);   \
1071 		if (IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS) && is_atomic)      \
1072 			return;                                                \
1073 		check_access(ptr, size,                                        \
1074 			     KCSAN_ACCESS_WRITE |                              \
1075 				     (is_atomic ? KCSAN_ACCESS_ATOMIC : 0),    \
1076 			     _RET_IP_);                                        \
1077 	}                                                                      \
1078 	EXPORT_SYMBOL(__tsan_volatile_write##size);                            \
1079 	void __tsan_unaligned_volatile_write##size(void *ptr)                  \
1080 		__alias(__tsan_volatile_write##size);                          \
1081 	EXPORT_SYMBOL(__tsan_unaligned_volatile_write##size)
1082 
1083 DEFINE_TSAN_VOLATILE_READ_WRITE(1);
1084 DEFINE_TSAN_VOLATILE_READ_WRITE(2);
1085 DEFINE_TSAN_VOLATILE_READ_WRITE(4);
1086 DEFINE_TSAN_VOLATILE_READ_WRITE(8);
1087 DEFINE_TSAN_VOLATILE_READ_WRITE(16);
1088 
1089 /*
1090  * Function entry and exit are used to determine the validty of reorder_access.
1091  * Reordering of the access ends at the end of the function scope where the
1092  * access happened. This is done for two reasons:
1093  *
1094  *	1. Artificially limits the scope where missing barriers are detected.
1095  *	   This minimizes false positives due to uninstrumented functions that
1096  *	   contain the required barriers but were missed.
1097  *
1098  *	2. Simplifies generating the stack trace of the access.
1099  */
1100 void __tsan_func_entry(void *call_pc);
1101 noinline void __tsan_func_entry(void *call_pc)
1102 {
1103 	if (!IS_ENABLED(CONFIG_KCSAN_WEAK_MEMORY))
1104 		return;
1105 
1106 	add_kcsan_stack_depth(1);
1107 }
1108 EXPORT_SYMBOL(__tsan_func_entry);
1109 
1110 void __tsan_func_exit(void);
1111 noinline void __tsan_func_exit(void)
1112 {
1113 	struct kcsan_scoped_access *reorder_access;
1114 
1115 	if (!IS_ENABLED(CONFIG_KCSAN_WEAK_MEMORY))
1116 		return;
1117 
1118 	reorder_access = get_reorder_access(get_ctx());
1119 	if (!reorder_access)
1120 		goto out;
1121 
1122 	if (get_kcsan_stack_depth() <= reorder_access->stack_depth) {
1123 		/*
1124 		 * Access check to catch cases where write without a barrier
1125 		 * (supposed release) was last access in function: because
1126 		 * instrumentation is inserted before the real access, a data
1127 		 * race due to the write giving up a c-s would only be caught if
1128 		 * we do the conflicting access after.
1129 		 */
1130 		check_access(reorder_access->ptr, reorder_access->size,
1131 			     reorder_access->type, reorder_access->ip);
1132 		reorder_access->size = 0;
1133 		reorder_access->stack_depth = INT_MIN;
1134 	}
1135 out:
1136 	add_kcsan_stack_depth(-1);
1137 }
1138 EXPORT_SYMBOL(__tsan_func_exit);
1139 
1140 void __tsan_init(void);
1141 void __tsan_init(void)
1142 {
1143 }
1144 EXPORT_SYMBOL(__tsan_init);
1145 
1146 /*
1147  * Instrumentation for atomic builtins (__atomic_*, __sync_*).
1148  *
1149  * Normal kernel code _should not_ be using them directly, but some
1150  * architectures may implement some or all atomics using the compilers'
1151  * builtins.
1152  *
1153  * Note: If an architecture decides to fully implement atomics using the
1154  * builtins, because they are implicitly instrumented by KCSAN (and KASAN,
1155  * etc.), implementing the ARCH_ATOMIC interface (to get instrumentation via
1156  * atomic-instrumented) is no longer necessary.
1157  *
1158  * TSAN instrumentation replaces atomic accesses with calls to any of the below
1159  * functions, whose job is to also execute the operation itself.
1160  */
1161 
1162 static __always_inline void kcsan_atomic_builtin_memorder(int memorder)
1163 {
1164 	if (memorder == __ATOMIC_RELEASE ||
1165 	    memorder == __ATOMIC_SEQ_CST ||
1166 	    memorder == __ATOMIC_ACQ_REL)
1167 		__kcsan_release();
1168 }
1169 
1170 #define DEFINE_TSAN_ATOMIC_LOAD_STORE(bits)                                                        \
1171 	u##bits __tsan_atomic##bits##_load(const u##bits *ptr, int memorder);                      \
1172 	u##bits __tsan_atomic##bits##_load(const u##bits *ptr, int memorder)                       \
1173 	{                                                                                          \
1174 		kcsan_atomic_builtin_memorder(memorder);                                           \
1175 		if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) {                                    \
1176 			check_access(ptr, bits / BITS_PER_BYTE, KCSAN_ACCESS_ATOMIC, _RET_IP_);    \
1177 		}                                                                                  \
1178 		return __atomic_load_n(ptr, memorder);                                             \
1179 	}                                                                                          \
1180 	EXPORT_SYMBOL(__tsan_atomic##bits##_load);                                                 \
1181 	void __tsan_atomic##bits##_store(u##bits *ptr, u##bits v, int memorder);                   \
1182 	void __tsan_atomic##bits##_store(u##bits *ptr, u##bits v, int memorder)                    \
1183 	{                                                                                          \
1184 		kcsan_atomic_builtin_memorder(memorder);                                           \
1185 		if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) {                                    \
1186 			check_access(ptr, bits / BITS_PER_BYTE,                                    \
1187 				     KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC, _RET_IP_);          \
1188 		}                                                                                  \
1189 		__atomic_store_n(ptr, v, memorder);                                                \
1190 	}                                                                                          \
1191 	EXPORT_SYMBOL(__tsan_atomic##bits##_store)
1192 
1193 #define DEFINE_TSAN_ATOMIC_RMW(op, bits, suffix)                                                   \
1194 	u##bits __tsan_atomic##bits##_##op(u##bits *ptr, u##bits v, int memorder);                 \
1195 	u##bits __tsan_atomic##bits##_##op(u##bits *ptr, u##bits v, int memorder)                  \
1196 	{                                                                                          \
1197 		kcsan_atomic_builtin_memorder(memorder);                                           \
1198 		if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) {                                    \
1199 			check_access(ptr, bits / BITS_PER_BYTE,                                    \
1200 				     KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE |                  \
1201 					     KCSAN_ACCESS_ATOMIC, _RET_IP_);                       \
1202 		}                                                                                  \
1203 		return __atomic_##op##suffix(ptr, v, memorder);                                    \
1204 	}                                                                                          \
1205 	EXPORT_SYMBOL(__tsan_atomic##bits##_##op)
1206 
1207 /*
1208  * Note: CAS operations are always classified as write, even in case they
1209  * fail. We cannot perform check_access() after a write, as it might lead to
1210  * false positives, in cases such as:
1211  *
1212  *	T0: __atomic_compare_exchange_n(&p->flag, &old, 1, ...)
1213  *
1214  *	T1: if (__atomic_load_n(&p->flag, ...)) {
1215  *		modify *p;
1216  *		p->flag = 0;
1217  *	    }
1218  *
1219  * The only downside is that, if there are 3 threads, with one CAS that
1220  * succeeds, another CAS that fails, and an unmarked racing operation, we may
1221  * point at the wrong CAS as the source of the race. However, if we assume that
1222  * all CAS can succeed in some other execution, the data race is still valid.
1223  */
1224 #define DEFINE_TSAN_ATOMIC_CMPXCHG(bits, strength, weak)                                           \
1225 	int __tsan_atomic##bits##_compare_exchange_##strength(u##bits *ptr, u##bits *exp,          \
1226 							      u##bits val, int mo, int fail_mo);   \
1227 	int __tsan_atomic##bits##_compare_exchange_##strength(u##bits *ptr, u##bits *exp,          \
1228 							      u##bits val, int mo, int fail_mo)    \
1229 	{                                                                                          \
1230 		kcsan_atomic_builtin_memorder(mo);                                                 \
1231 		if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) {                                    \
1232 			check_access(ptr, bits / BITS_PER_BYTE,                                    \
1233 				     KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE |                  \
1234 					     KCSAN_ACCESS_ATOMIC, _RET_IP_);                       \
1235 		}                                                                                  \
1236 		return __atomic_compare_exchange_n(ptr, exp, val, weak, mo, fail_mo);              \
1237 	}                                                                                          \
1238 	EXPORT_SYMBOL(__tsan_atomic##bits##_compare_exchange_##strength)
1239 
1240 #define DEFINE_TSAN_ATOMIC_CMPXCHG_VAL(bits)                                                       \
1241 	u##bits __tsan_atomic##bits##_compare_exchange_val(u##bits *ptr, u##bits exp, u##bits val, \
1242 							   int mo, int fail_mo);                   \
1243 	u##bits __tsan_atomic##bits##_compare_exchange_val(u##bits *ptr, u##bits exp, u##bits val, \
1244 							   int mo, int fail_mo)                    \
1245 	{                                                                                          \
1246 		kcsan_atomic_builtin_memorder(mo);                                                 \
1247 		if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) {                                    \
1248 			check_access(ptr, bits / BITS_PER_BYTE,                                    \
1249 				     KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE |                  \
1250 					     KCSAN_ACCESS_ATOMIC, _RET_IP_);                       \
1251 		}                                                                                  \
1252 		__atomic_compare_exchange_n(ptr, &exp, val, 0, mo, fail_mo);                       \
1253 		return exp;                                                                        \
1254 	}                                                                                          \
1255 	EXPORT_SYMBOL(__tsan_atomic##bits##_compare_exchange_val)
1256 
1257 #define DEFINE_TSAN_ATOMIC_OPS(bits)                                                               \
1258 	DEFINE_TSAN_ATOMIC_LOAD_STORE(bits);                                                       \
1259 	DEFINE_TSAN_ATOMIC_RMW(exchange, bits, _n);                                                \
1260 	DEFINE_TSAN_ATOMIC_RMW(fetch_add, bits, );                                                 \
1261 	DEFINE_TSAN_ATOMIC_RMW(fetch_sub, bits, );                                                 \
1262 	DEFINE_TSAN_ATOMIC_RMW(fetch_and, bits, );                                                 \
1263 	DEFINE_TSAN_ATOMIC_RMW(fetch_or, bits, );                                                  \
1264 	DEFINE_TSAN_ATOMIC_RMW(fetch_xor, bits, );                                                 \
1265 	DEFINE_TSAN_ATOMIC_RMW(fetch_nand, bits, );                                                \
1266 	DEFINE_TSAN_ATOMIC_CMPXCHG(bits, strong, 0);                                               \
1267 	DEFINE_TSAN_ATOMIC_CMPXCHG(bits, weak, 1);                                                 \
1268 	DEFINE_TSAN_ATOMIC_CMPXCHG_VAL(bits)
1269 
1270 DEFINE_TSAN_ATOMIC_OPS(8);
1271 DEFINE_TSAN_ATOMIC_OPS(16);
1272 DEFINE_TSAN_ATOMIC_OPS(32);
1273 #ifdef CONFIG_64BIT
1274 DEFINE_TSAN_ATOMIC_OPS(64);
1275 #endif
1276 
1277 void __tsan_atomic_thread_fence(int memorder);
1278 void __tsan_atomic_thread_fence(int memorder)
1279 {
1280 	kcsan_atomic_builtin_memorder(memorder);
1281 	__atomic_thread_fence(memorder);
1282 }
1283 EXPORT_SYMBOL(__tsan_atomic_thread_fence);
1284 
1285 /*
1286  * In instrumented files, we emit instrumentation for barriers by mapping the
1287  * kernel barriers to an __atomic_signal_fence(), which is interpreted specially
1288  * and otherwise has no relation to a real __atomic_signal_fence(). No known
1289  * kernel code uses __atomic_signal_fence().
1290  *
1291  * Since fsanitize=thread instrumentation handles __atomic_signal_fence(), which
1292  * are turned into calls to __tsan_atomic_signal_fence(), such instrumentation
1293  * can be disabled via the __no_kcsan function attribute (vs. an explicit call
1294  * which could not). When __no_kcsan is requested, __atomic_signal_fence()
1295  * generates no code.
1296  *
1297  * Note: The result of using __atomic_signal_fence() with KCSAN enabled is
1298  * potentially limiting the compiler's ability to reorder operations; however,
1299  * if barriers were instrumented with explicit calls (without LTO), the compiler
1300  * couldn't optimize much anyway. The result of a hypothetical architecture
1301  * using __atomic_signal_fence() in normal code would be KCSAN false negatives.
1302  */
1303 void __tsan_atomic_signal_fence(int memorder);
1304 noinline void __tsan_atomic_signal_fence(int memorder)
1305 {
1306 	switch (memorder) {
1307 	case __KCSAN_BARRIER_TO_SIGNAL_FENCE_mb:
1308 		__kcsan_mb();
1309 		break;
1310 	case __KCSAN_BARRIER_TO_SIGNAL_FENCE_wmb:
1311 		__kcsan_wmb();
1312 		break;
1313 	case __KCSAN_BARRIER_TO_SIGNAL_FENCE_rmb:
1314 		__kcsan_rmb();
1315 		break;
1316 	case __KCSAN_BARRIER_TO_SIGNAL_FENCE_release:
1317 		__kcsan_release();
1318 		break;
1319 	default:
1320 		break;
1321 	}
1322 }
1323 EXPORT_SYMBOL(__tsan_atomic_signal_fence);
1324 
1325 #ifdef __HAVE_ARCH_MEMSET
1326 void *__tsan_memset(void *s, int c, size_t count);
1327 noinline void *__tsan_memset(void *s, int c, size_t count)
1328 {
1329 	/*
1330 	 * Instead of not setting up watchpoints where accessed size is greater
1331 	 * than MAX_ENCODABLE_SIZE, truncate checked size to MAX_ENCODABLE_SIZE.
1332 	 */
1333 	size_t check_len = min_t(size_t, count, MAX_ENCODABLE_SIZE);
1334 
1335 	check_access(s, check_len, KCSAN_ACCESS_WRITE, _RET_IP_);
1336 	return memset(s, c, count);
1337 }
1338 #else
1339 void *__tsan_memset(void *s, int c, size_t count) __alias(memset);
1340 #endif
1341 EXPORT_SYMBOL(__tsan_memset);
1342 
1343 #ifdef __HAVE_ARCH_MEMMOVE
1344 void *__tsan_memmove(void *dst, const void *src, size_t len);
1345 noinline void *__tsan_memmove(void *dst, const void *src, size_t len)
1346 {
1347 	size_t check_len = min_t(size_t, len, MAX_ENCODABLE_SIZE);
1348 
1349 	check_access(dst, check_len, KCSAN_ACCESS_WRITE, _RET_IP_);
1350 	check_access(src, check_len, 0, _RET_IP_);
1351 	return memmove(dst, src, len);
1352 }
1353 #else
1354 void *__tsan_memmove(void *dst, const void *src, size_t len) __alias(memmove);
1355 #endif
1356 EXPORT_SYMBOL(__tsan_memmove);
1357 
1358 #ifdef __HAVE_ARCH_MEMCPY
1359 void *__tsan_memcpy(void *dst, const void *src, size_t len);
1360 noinline void *__tsan_memcpy(void *dst, const void *src, size_t len)
1361 {
1362 	size_t check_len = min_t(size_t, len, MAX_ENCODABLE_SIZE);
1363 
1364 	check_access(dst, check_len, KCSAN_ACCESS_WRITE, _RET_IP_);
1365 	check_access(src, check_len, 0, _RET_IP_);
1366 	return memcpy(dst, src, len);
1367 }
1368 #else
1369 void *__tsan_memcpy(void *dst, const void *src, size_t len) __alias(memcpy);
1370 #endif
1371 EXPORT_SYMBOL(__tsan_memcpy);
1372