1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2019, Google LLC.
8 #define pr_fmt(fmt) "kcsan: " fmt
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>
27 #include "permissive.h"
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);
35 #ifdef MODULE_PARAM_PREFIX
36 #undef MODULE_PARAM_PREFIX
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);
45 #ifdef CONFIG_KCSAN_WEAK_MEMORY
46 static bool kcsan_weak_memory = true;
47 module_param_named(weak_memory, kcsan_weak_memory, bool, 0644);
49 #define kcsan_weak_memory false
54 /* Per-CPU kcsan_ctx for interrupts */
55 static DEFINE_PER_CPU(struct kcsan_ctx, kcsan_cpu_ctx) = {
56 .scoped_accesses = {LIST_POISON1, NULL},
60 * Helper macros to index into adjacent slots, starting from address slot
61 * itself, followed by the right and left slots.
63 * The purpose is 2-fold:
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.
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
74 * 1. excessive contention between watchpoint checks and setup;
75 * 2. larger number of simultaneous watchpoints without sacrificing
78 * Example: SLOT_IDX values for KCSAN_CHECK_ADJACENT=1, where i is [0, 1, 2]:
82 * slot=63: [64, 65, 63]
84 #define SLOT_IDX(slot, i) (slot + ((i + KCSAN_CHECK_ADJACENT) % NUM_SLOTS))
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)}.
92 #define SLOT_IDX_FAST(slot, i) (slot + i)
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.
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.
103 static atomic_long_t watchpoints[CONFIG_KCSAN_NUM_WATCHPOINTS + NUM_SLOTS-1];
106 * Instructions to skip watching counter, used in should_watch(). We use a
107 * per-CPU counter to avoid excessive contention.
109 static DEFINE_PER_CPU(long, kcsan_skip);
111 /* For kcsan_prandom_u32_max(). */
112 static DEFINE_PER_CPU(u32, kcsan_rand_state);
114 static __always_inline atomic_long_t *find_watchpoint(unsigned long addr,
117 long *encoded_watchpoint)
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;
127 BUILD_BUG_ON(CONFIG_KCSAN_NUM_WATCHPOINTS < NUM_SLOTS);
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))
136 if (expect_write && !is_write)
139 /* Check if the watchpoint matches the access. */
140 if (matching_access(wp_addr_masked, wp_size, addr_masked, size))
147 static inline atomic_long_t *
148 insert_watchpoint(unsigned long addr, size_t size, bool is_write)
150 const int slot = watchpoint_slot(addr);
151 const long encoded_watchpoint = encode_watchpoint(addr, size, is_write);
152 atomic_long_t *watchpoint;
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);
161 for (i = 0; i < NUM_SLOTS; ++i) {
162 long expect_val = INVALID_WATCHPOINT;
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))
174 * Return true if watchpoint was successfully consumed, false otherwise.
176 * This may return false if:
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.
182 static __always_inline bool
183 try_consume_watchpoint(atomic_long_t *watchpoint, long encoded_watchpoint)
185 return atomic_long_try_cmpxchg_relaxed(watchpoint, &encoded_watchpoint, CONSUMED_WATCHPOINT);
188 /* Return true if watchpoint was not touched, false if already consumed. */
189 static inline bool consume_watchpoint(atomic_long_t *watchpoint)
191 return atomic_long_xchg_relaxed(watchpoint, CONSUMED_WATCHPOINT) != CONSUMED_WATCHPOINT;
194 /* Remove the watchpoint -- its slot may be reused after. */
195 static inline void remove_watchpoint(atomic_long_t *watchpoint)
197 atomic_long_set(watchpoint, INVALID_WATCHPOINT);
200 static __always_inline struct kcsan_ctx *get_ctx(void)
203 * In interrupts, use raw_cpu_ptr to avoid unnecessary checks, that would
204 * also result in calls that generate warnings in uaccess regions.
206 return in_task() ? ¤t->kcsan_ctx : raw_cpu_ptr(&kcsan_cpu_ctx);
209 static __always_inline void
210 check_access(const volatile void *ptr, size_t size, int type, unsigned long ip);
212 /* Check scoped accesses; never inline because this is a slow-path! */
213 static noinline void kcsan_check_scoped_accesses(void)
215 struct kcsan_ctx *ctx = get_ctx();
216 struct kcsan_scoped_access *scoped_access;
218 if (ctx->disable_scoped)
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);
226 ctx->disable_scoped--;
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)
233 if (type & KCSAN_ACCESS_ATOMIC)
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.
241 if (type & KCSAN_ACCESS_ASSERT)
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. */
249 if (ctx->atomic_next > 0) {
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.
259 if ((hardirq_count() >> HARDIRQ_SHIFT) < 2)
260 --ctx->atomic_next; /* in task, or outer interrupt */
264 return ctx->atomic_nest_count > 0 || ctx->in_flat_atomic;
267 static __always_inline bool
268 should_watch(struct kcsan_ctx *ctx, const volatile void *ptr, size_t size, int type)
271 * Never set up watchpoints when memory operations are atomic.
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.
277 if (is_atomic(ctx, ptr, size, type))
280 if (this_cpu_dec_return(kcsan_skip) >= 0)
284 * NOTE: If we get here, kcsan_skip must always be reset in slow path
285 * via reset_kcsan_skip() to avoid underflow.
288 /* this operation should be watched */
293 * Returns a pseudo-random number in interval [0, ep_ro). Simple linear
294 * congruential generator, using constants from "Numerical Recipes".
296 static u32 kcsan_prandom_u32_max(u32 ep_ro)
298 u32 state = this_cpu_read(kcsan_rand_state);
300 state = 1664525 * state + 1013904223;
301 this_cpu_write(kcsan_rand_state, state);
303 return state % ep_ro;
306 static inline void reset_kcsan_skip(void)
308 long skip_count = kcsan_skip_watch -
309 (IS_ENABLED(CONFIG_KCSAN_SKIP_WATCH_RANDOMIZE) ?
310 kcsan_prandom_u32_max(kcsan_skip_watch) :
312 this_cpu_write(kcsan_skip, skip_count);
315 static __always_inline bool kcsan_is_enabled(struct kcsan_ctx *ctx)
317 return READ_ONCE(kcsan_enabled) && !ctx->disable_count;
320 /* Introduce delay depending on context and configuration. */
321 static void delay_access(int type)
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;
328 delay -= IS_ENABLED(CONFIG_KCSAN_DELAY_RANDOMIZE) ?
329 kcsan_prandom_u32_max(delay >> skew_delay_order) :
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.
338 static __always_inline u64 read_instrumented_memory(const volatile void *ptr, size_t size)
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.
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.
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. */
358 void kcsan_save_irqtrace(struct task_struct *task)
360 #ifdef CONFIG_TRACE_IRQFLAGS
361 task->kcsan_save_irqtrace = task->irqtrace;
365 void kcsan_restore_irqtrace(struct task_struct *task)
367 #ifdef CONFIG_TRACE_IRQFLAGS
368 task->irqtrace = task->kcsan_save_irqtrace;
372 static __always_inline int get_kcsan_stack_depth(void)
374 #ifdef CONFIG_KCSAN_WEAK_MEMORY
375 return current->kcsan_stack_depth;
382 static __always_inline void add_kcsan_stack_depth(int val)
384 #ifdef CONFIG_KCSAN_WEAK_MEMORY
385 current->kcsan_stack_depth += val;
391 static __always_inline struct kcsan_scoped_access *get_reorder_access(struct kcsan_ctx *ctx)
393 #ifdef CONFIG_KCSAN_WEAK_MEMORY
394 return ctx->disable_scoped ? NULL : &ctx->reorder_access;
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)
404 struct kcsan_scoped_access *reorder_access = get_reorder_access(ctx);
410 * Note: If accesses are repeated while reorder_access is identical,
411 * never matches the new access, because !(type & KCSAN_ACCESS_SCOPED).
413 return reorder_access->ptr == ptr && reorder_access->size == size &&
414 reorder_access->type == type && reorder_access->ip == ip;
418 set_reorder_access(struct kcsan_ctx *ctx, const volatile void *ptr, size_t size,
419 int type, unsigned long ip)
421 struct kcsan_scoped_access *reorder_access = get_reorder_access(ctx);
423 if (!reorder_access || !kcsan_weak_memory)
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.
431 ctx->disable_scoped++;
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();
439 ctx->disable_scoped--;
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.
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.
455 static noinline void kcsan_found_watchpoint(const volatile void *ptr,
459 atomic_long_t *watchpoint,
460 long encoded_watchpoint)
462 const bool is_assert = (type & KCSAN_ACCESS_ASSERT) != 0;
463 struct kcsan_ctx *ctx = get_ctx();
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.
473 if (!kcsan_is_enabled(ctx))
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.
481 * reorder_access is never created from an access with access_mask set.
483 if (ctx->access_mask && !find_reorder_access(ctx, ptr, size, type, ip))
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.
491 * Use CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=n to filter.
493 if (!is_assert && kcsan_ignore_address(ptr))
497 * Consuming the watchpoint must be guarded by kcsan_is_enabled() to
498 * avoid erroneously triggering reports if the context is disabled.
500 consumed = try_consume_watchpoint(watchpoint, encoded_watchpoint);
502 /* keep this after try_consume_watchpoint */
503 flags = user_access_save();
506 kcsan_save_irqtrace(current);
507 kcsan_report_set_info(ptr, size, type, ip, watchpoint - watchpoints);
508 kcsan_restore_irqtrace(current);
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.
515 atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_REPORT_RACES]);
519 atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]);
521 atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_DATA_RACES]);
523 user_access_restore(flags);
527 kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type, unsigned long ip)
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;
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;
542 * Always reset kcsan_skip counter in slow-path to avoid underflow; see
547 if (!kcsan_is_enabled(ctx))
551 * Check to-ignore addresses after kcsan_is_enabled(), as we may access
552 * memory that is not yet initialized during early boot.
554 if (!is_assert && kcsan_ignore_address(ptr))
557 if (!check_encodable((unsigned long)ptr, size)) {
558 atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_UNENCODABLE_ACCESSES]);
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:
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).
570 is_reorder_access = find_reorder_access(ctx, ptr, size, type, ip);
571 if (is_reorder_access)
572 interrupt_watcher = false;
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.
580 ctx->disable_scoped++;
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.
587 kcsan_save_irqtrace(current);
588 if (!interrupt_watcher)
589 local_irq_save(irq_flags);
591 watchpoint = insert_watchpoint((unsigned long)ptr, size, is_write);
592 if (watchpoint == NULL) {
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.
598 atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_NO_CAPACITY]);
602 atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_SETUP_WATCHPOINTS]);
603 atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_USED_WATCHPOINTS]);
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.
609 old = is_reorder_access ? 0 : read_instrumented_memory(ptr, size);
612 * Delay this thread, to increase probability of observing a racy
613 * conflicting access.
618 * Re-read value, and check if it is as expected; if not, we infer a
621 if (!is_reorder_access) {
622 new = read_instrumented_memory(ptr, size);
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.
638 * Check if we observed a value change.
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.
644 if (diff && !kcsan_ignore_data_race(size, type, old, new, diff))
645 value_change = KCSAN_VALUE_CHANGE_TRUE;
647 /* Check if this access raced with another. */
648 if (!consume_watchpoint(watchpoint)) {
650 * Depending on the access type, map a value_change of MAYBE to
651 * TRUE (always report) or FALSE (never report).
653 if (value_change == KCSAN_VALUE_CHANGE_MAYBE) {
654 if (access_mask != 0) {
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.
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;
668 * No need to increment 'data_races' counter, as the racing
669 * thread already did.
671 * Count 'assert_failures' for each failed ASSERT access,
672 * therefore both this thread and the racing thread may
673 * increment this counter.
675 if (is_assert && value_change == KCSAN_VALUE_CHANGE_TRUE)
676 atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]);
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. */
684 atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN]);
686 atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]);
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);
695 * Remove watchpoint; must be after reporting, since the slot may be
696 * reused after this point.
698 remove_watchpoint(watchpoint);
699 atomic_long_dec(&kcsan_counters[KCSAN_COUNTER_USED_WATCHPOINTS]);
702 if (!interrupt_watcher)
703 local_irq_restore(irq_flags);
704 kcsan_restore_irqtrace(current);
705 ctx->disable_scoped--;
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.
712 if (!access_mask && !is_assert)
713 set_reorder_access(ctx, ptr, size, type, ip);
715 user_access_restore(ua_flags);
718 static __always_inline void
719 check_access(const volatile void *ptr, size_t size, int type, unsigned long ip)
721 atomic_long_t *watchpoint;
722 long encoded_watchpoint;
725 * Do nothing for 0 sized check; this comparison will be optimized out
726 * for constant sized instrumentation (__tsan_{read,write}N).
728 if (unlikely(size == 0))
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.
737 watchpoint = find_watchpoint((unsigned long)ptr, size,
738 !(type & KCSAN_ACCESS_WRITE),
739 &encoded_watchpoint);
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
747 if (unlikely(watchpoint != NULL))
748 kcsan_found_watchpoint(ptr, size, type, ip, watchpoint, encoded_watchpoint);
750 struct kcsan_ctx *ctx = get_ctx(); /* Call only once in fast-path. */
752 if (unlikely(should_watch(ctx, ptr, size, type))) {
753 kcsan_setup_watchpoint(ptr, size, type, ip);
757 if (!(type & KCSAN_ACCESS_SCOPED)) {
758 struct kcsan_scoped_access *reorder_access = get_reorder_access(ctx);
760 if (reorder_access) {
762 * reorder_access check: simulates reordering of
763 * the access after subsequent operations.
765 ptr = reorder_access->ptr;
766 type = reorder_access->type;
767 ip = reorder_access->ip;
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.
777 size = READ_ONCE(reorder_access->size);
784 * Always checked last, right before returning from runtime;
785 * if reorder_access is valid, checked after it was checked.
787 if (unlikely(ctx->scoped_accesses.prev))
788 kcsan_check_scoped_accesses();
792 /* === Public interface ===================================================== */
794 void __init kcsan_init(void)
800 for_each_possible_cpu(cpu)
801 per_cpu(kcsan_rand_state, cpu) = (u32)get_cycles();
804 * We are in the init task, and no other tasks should be running;
805 * WRITE_ONCE without memory barrier is sufficient.
807 if (kcsan_early_enable) {
808 pr_info("enabled early\n");
809 WRITE_ONCE(kcsan_enabled, true);
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");
818 pr_info("strict mode configured\n");
822 /* === Exported interface =================================================== */
824 void kcsan_disable_current(void)
826 ++get_ctx()->disable_count;
828 EXPORT_SYMBOL(kcsan_disable_current);
830 void kcsan_enable_current(void)
832 if (get_ctx()->disable_count-- == 0) {
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.
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();
844 EXPORT_SYMBOL(kcsan_enable_current);
846 void kcsan_enable_current_nowarn(void)
848 if (get_ctx()->disable_count-- == 0)
849 kcsan_disable_current();
851 EXPORT_SYMBOL(kcsan_enable_current_nowarn);
853 void kcsan_nestable_atomic_begin(void)
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
862 ++get_ctx()->atomic_nest_count;
864 EXPORT_SYMBOL(kcsan_nestable_atomic_begin);
866 void kcsan_nestable_atomic_end(void)
868 if (get_ctx()->atomic_nest_count-- == 0) {
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.
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();
880 EXPORT_SYMBOL(kcsan_nestable_atomic_end);
882 void kcsan_flat_atomic_begin(void)
884 get_ctx()->in_flat_atomic = true;
886 EXPORT_SYMBOL(kcsan_flat_atomic_begin);
888 void kcsan_flat_atomic_end(void)
890 get_ctx()->in_flat_atomic = false;
892 EXPORT_SYMBOL(kcsan_flat_atomic_end);
894 void kcsan_atomic_next(int n)
896 get_ctx()->atomic_next = n;
898 EXPORT_SYMBOL(kcsan_atomic_next);
900 void kcsan_set_access_mask(unsigned long mask)
902 get_ctx()->access_mask = mask;
904 EXPORT_SYMBOL(kcsan_set_access_mask);
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)
910 struct kcsan_ctx *ctx = get_ctx();
912 check_access(ptr, size, type, _RET_IP_);
914 ctx->disable_count++; /* Disable KCSAN, in case list debugging is on. */
916 INIT_LIST_HEAD(&sa->list);
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);
926 ctx->disable_count--;
929 EXPORT_SYMBOL(kcsan_begin_scoped_access);
931 void kcsan_end_scoped_access(struct kcsan_scoped_access *sa)
933 struct kcsan_ctx *ctx = get_ctx();
935 if (WARN(!ctx->scoped_accesses.prev, "Unbalanced %s()?", __func__))
938 ctx->disable_count++; /* Disable KCSAN, in case list debugging is on. */
941 if (list_empty(&ctx->scoped_accesses))
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
948 ctx->scoped_accesses.prev = NULL;
950 ctx->disable_count--;
952 check_access(sa->ptr, sa->size, sa->type, sa->ip);
954 EXPORT_SYMBOL(kcsan_end_scoped_access);
956 void __kcsan_check_access(const volatile void *ptr, size_t size, int type)
958 check_access(ptr, size, type, _RET_IP_);
960 EXPORT_SYMBOL(__kcsan_check_access);
962 #define DEFINE_MEMORY_BARRIER(name, order_before_cond) \
963 void __kcsan_##name(void) \
965 struct kcsan_scoped_access *sa = get_reorder_access(get_ctx()); \
968 if (order_before_cond) \
971 EXPORT_SYMBOL(__kcsan_##name)
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);
979 * KCSAN uses the same instrumentation that is emitted by supported compilers
980 * for ThreadSanitizer (TSAN).
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.
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.
991 #define DEFINE_TSAN_READ_WRITE(size) \
992 void __tsan_read##size(void *ptr); \
993 void __tsan_read##size(void *ptr) \
995 check_access(ptr, size, 0, _RET_IP_); \
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) \
1004 check_access(ptr, size, KCSAN_ACCESS_WRITE, _RET_IP_); \
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) \
1013 check_access(ptr, size, \
1014 KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE, \
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)
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);
1028 void __tsan_read_range(void *ptr, size_t size);
1029 void __tsan_read_range(void *ptr, size_t size)
1031 check_access(ptr, size, 0, _RET_IP_);
1033 EXPORT_SYMBOL(__tsan_read_range);
1035 void __tsan_write_range(void *ptr, size_t size);
1036 void __tsan_write_range(void *ptr, size_t size)
1038 check_access(ptr, size, KCSAN_ACCESS_WRITE, _RET_IP_);
1040 EXPORT_SYMBOL(__tsan_write_range);
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/
1048 * We only consider volatile accesses atomic if they are aligned and would pass
1049 * the size-check of compiletime_assert_rwonce_type().
1051 #define DEFINE_TSAN_VOLATILE_READ_WRITE(size) \
1052 void __tsan_volatile_read##size(void *ptr); \
1053 void __tsan_volatile_read##size(void *ptr) \
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) \
1059 check_access(ptr, size, is_atomic ? KCSAN_ACCESS_ATOMIC : 0, \
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) \
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) \
1073 check_access(ptr, size, \
1074 KCSAN_ACCESS_WRITE | \
1075 (is_atomic ? KCSAN_ACCESS_ATOMIC : 0), \
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)
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);
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:
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.
1098 * 2. Simplifies generating the stack trace of the access.
1100 void __tsan_func_entry(void *call_pc);
1101 noinline void __tsan_func_entry(void *call_pc)
1103 if (!IS_ENABLED(CONFIG_KCSAN_WEAK_MEMORY))
1106 add_kcsan_stack_depth(1);
1108 EXPORT_SYMBOL(__tsan_func_entry);
1110 void __tsan_func_exit(void);
1111 noinline void __tsan_func_exit(void)
1113 struct kcsan_scoped_access *reorder_access;
1115 if (!IS_ENABLED(CONFIG_KCSAN_WEAK_MEMORY))
1118 reorder_access = get_reorder_access(get_ctx());
1119 if (!reorder_access)
1122 if (get_kcsan_stack_depth() <= reorder_access->stack_depth) {
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.
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;
1136 add_kcsan_stack_depth(-1);
1138 EXPORT_SYMBOL(__tsan_func_exit);
1140 void __tsan_init(void);
1141 void __tsan_init(void)
1144 EXPORT_SYMBOL(__tsan_init);
1147 * Instrumentation for atomic builtins (__atomic_*, __sync_*).
1149 * Normal kernel code _should not_ be using them directly, but some
1150 * architectures may implement some or all atomics using the compilers'
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.
1158 * TSAN instrumentation replaces atomic accesses with calls to any of the below
1159 * functions, whose job is to also execute the operation itself.
1162 static __always_inline void kcsan_atomic_builtin_memorder(int memorder)
1164 if (memorder == __ATOMIC_RELEASE ||
1165 memorder == __ATOMIC_SEQ_CST ||
1166 memorder == __ATOMIC_ACQ_REL)
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) \
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_); \
1178 return __atomic_load_n(ptr, memorder); \
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) \
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_); \
1189 __atomic_store_n(ptr, v, memorder); \
1191 EXPORT_SYMBOL(__tsan_atomic##bits##_store)
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) \
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_); \
1203 return __atomic_##op##suffix(ptr, v, memorder); \
1205 EXPORT_SYMBOL(__tsan_atomic##bits##_##op)
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:
1212 * T0: __atomic_compare_exchange_n(&p->flag, &old, 1, ...)
1214 * T1: if (__atomic_load_n(&p->flag, ...)) {
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.
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) \
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_); \
1236 return __atomic_compare_exchange_n(ptr, exp, val, weak, mo, fail_mo); \
1238 EXPORT_SYMBOL(__tsan_atomic##bits##_compare_exchange_##strength)
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) \
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_); \
1252 __atomic_compare_exchange_n(ptr, &exp, val, 0, mo, fail_mo); \
1255 EXPORT_SYMBOL(__tsan_atomic##bits##_compare_exchange_val)
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)
1270 DEFINE_TSAN_ATOMIC_OPS(8);
1271 DEFINE_TSAN_ATOMIC_OPS(16);
1272 DEFINE_TSAN_ATOMIC_OPS(32);
1274 DEFINE_TSAN_ATOMIC_OPS(64);
1277 void __tsan_atomic_thread_fence(int memorder);
1278 void __tsan_atomic_thread_fence(int memorder)
1280 kcsan_atomic_builtin_memorder(memorder);
1281 __atomic_thread_fence(memorder);
1283 EXPORT_SYMBOL(__tsan_atomic_thread_fence);
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().
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.
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.
1303 void __tsan_atomic_signal_fence(int memorder);
1304 noinline void __tsan_atomic_signal_fence(int memorder)
1307 case __KCSAN_BARRIER_TO_SIGNAL_FENCE_mb:
1310 case __KCSAN_BARRIER_TO_SIGNAL_FENCE_wmb:
1313 case __KCSAN_BARRIER_TO_SIGNAL_FENCE_rmb:
1316 case __KCSAN_BARRIER_TO_SIGNAL_FENCE_release:
1323 EXPORT_SYMBOL(__tsan_atomic_signal_fence);
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)
1330 * Instead of not setting up watchpoints where accessed size is greater
1331 * than MAX_ENCODABLE_SIZE, truncate checked size to MAX_ENCODABLE_SIZE.
1333 size_t check_len = min_t(size_t, count, MAX_ENCODABLE_SIZE);
1335 check_access(s, check_len, KCSAN_ACCESS_WRITE, _RET_IP_);
1336 return memset(s, c, count);
1339 void *__tsan_memset(void *s, int c, size_t count) __alias(memset);
1341 EXPORT_SYMBOL(__tsan_memset);
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)
1347 size_t check_len = min_t(size_t, len, MAX_ENCODABLE_SIZE);
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);
1354 void *__tsan_memmove(void *dst, const void *src, size_t len) __alias(memmove);
1356 EXPORT_SYMBOL(__tsan_memmove);
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)
1362 size_t check_len = min_t(size_t, len, MAX_ENCODABLE_SIZE);
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);
1369 void *__tsan_memcpy(void *dst, const void *src, size_t len) __alias(memcpy);
1371 EXPORT_SYMBOL(__tsan_memcpy);