Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Marco Elver | bd0ccc4 | 2021-01-15 18:09:53 +0100 | [diff] [blame] | 2 | /* |
| 3 | * KCSAN core runtime. |
| 4 | * |
| 5 | * Copyright (C) 2019, Google LLC. |
| 6 | */ |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 7 | |
Marco Elver | 2778793 | 2020-07-31 10:17:22 +0200 | [diff] [blame] | 8 | #define pr_fmt(fmt) "kcsan: " fmt |
| 9 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 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> |
Marco Elver | 1e6ee2f | 2020-02-04 18:21:10 +0100 | [diff] [blame] | 15 | #include <linux/kernel.h> |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 16 | #include <linux/list.h> |
Marco Elver | 80d4c47 | 2020-02-07 19:59:10 +0100 | [diff] [blame] | 17 | #include <linux/moduleparam.h> |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 18 | #include <linux/percpu.h> |
| 19 | #include <linux/preempt.h> |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 20 | #include <linux/sched.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 23 | #include "encoding.h" |
| 24 | #include "kcsan.h" |
Marco Elver | 49f72d5 | 2021-06-07 14:56:51 +0200 | [diff] [blame] | 25 | #include "permissive.h" |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 26 | |
Marco Elver | 80d4c47 | 2020-02-07 19:59:10 +0100 | [diff] [blame] | 27 | static bool kcsan_early_enable = IS_ENABLED(CONFIG_KCSAN_EARLY_ENABLE); |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 28 | unsigned int kcsan_udelay_task = CONFIG_KCSAN_UDELAY_TASK; |
| 29 | unsigned int kcsan_udelay_interrupt = CONFIG_KCSAN_UDELAY_INTERRUPT; |
Marco Elver | 80d4c47 | 2020-02-07 19:59:10 +0100 | [diff] [blame] | 30 | static long kcsan_skip_watch = CONFIG_KCSAN_SKIP_WATCH; |
Marco Elver | 48b1fc1 | 2020-02-21 23:02:09 +0100 | [diff] [blame] | 31 | static bool kcsan_interrupt_watcher = IS_ENABLED(CONFIG_KCSAN_INTERRUPT_WATCHER); |
Marco Elver | 80d4c47 | 2020-02-07 19:59:10 +0100 | [diff] [blame] | 32 | |
| 33 | #ifdef MODULE_PARAM_PREFIX |
| 34 | #undef MODULE_PARAM_PREFIX |
| 35 | #endif |
| 36 | #define MODULE_PARAM_PREFIX "kcsan." |
| 37 | module_param_named(early_enable, kcsan_early_enable, bool, 0); |
| 38 | module_param_named(udelay_task, kcsan_udelay_task, uint, 0644); |
| 39 | module_param_named(udelay_interrupt, kcsan_udelay_interrupt, uint, 0644); |
| 40 | module_param_named(skip_watch, kcsan_skip_watch, long, 0644); |
Marco Elver | 48b1fc1 | 2020-02-21 23:02:09 +0100 | [diff] [blame] | 41 | module_param_named(interrupt_watcher, kcsan_interrupt_watcher, bool, 0444); |
Marco Elver | 80d4c47 | 2020-02-07 19:59:10 +0100 | [diff] [blame] | 42 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 43 | bool kcsan_enabled; |
| 44 | |
| 45 | /* Per-CPU kcsan_ctx for interrupts */ |
| 46 | static DEFINE_PER_CPU(struct kcsan_ctx, kcsan_cpu_ctx) = { |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 47 | .disable_count = 0, |
| 48 | .atomic_next = 0, |
| 49 | .atomic_nest_count = 0, |
| 50 | .in_flat_atomic = false, |
Marco Elver | 81af89e | 2020-02-11 17:04:22 +0100 | [diff] [blame] | 51 | .access_mask = 0, |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 52 | .scoped_accesses = {LIST_POISON1, NULL}, |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /* |
Qiujun Huang | e7b3410 | 2020-03-05 15:21:07 +0100 | [diff] [blame] | 56 | * Helper macros to index into adjacent slots, starting from address slot |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 57 | * itself, followed by the right and left slots. |
| 58 | * |
| 59 | * The purpose is 2-fold: |
| 60 | * |
| 61 | * 1. if during insertion the address slot is already occupied, check if |
| 62 | * any adjacent slots are free; |
| 63 | * 2. accesses that straddle a slot boundary due to size that exceeds a |
| 64 | * slot's range may check adjacent slots if any watchpoint matches. |
| 65 | * |
| 66 | * Note that accesses with very large size may still miss a watchpoint; however, |
| 67 | * given this should be rare, this is a reasonable trade-off to make, since this |
| 68 | * will avoid: |
| 69 | * |
| 70 | * 1. excessive contention between watchpoint checks and setup; |
| 71 | * 2. larger number of simultaneous watchpoints without sacrificing |
| 72 | * performance. |
| 73 | * |
| 74 | * Example: SLOT_IDX values for KCSAN_CHECK_ADJACENT=1, where i is [0, 1, 2]: |
| 75 | * |
| 76 | * slot=0: [ 1, 2, 0] |
| 77 | * slot=9: [10, 11, 9] |
| 78 | * slot=63: [64, 65, 63] |
| 79 | */ |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 80 | #define SLOT_IDX(slot, i) (slot + ((i + KCSAN_CHECK_ADJACENT) % NUM_SLOTS)) |
| 81 | |
| 82 | /* |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 83 | * SLOT_IDX_FAST is used in the fast-path. Not first checking the address's primary |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 84 | * slot (middle) is fine if we assume that races occur rarely. The set of |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 85 | * indices {SLOT_IDX(slot, i) | i in [0, NUM_SLOTS)} is equivalent to |
| 86 | * {SLOT_IDX_FAST(slot, i) | i in [0, NUM_SLOTS)}. |
| 87 | */ |
| 88 | #define SLOT_IDX_FAST(slot, i) (slot + i) |
| 89 | |
| 90 | /* |
| 91 | * Watchpoints, with each entry encoded as defined in encoding.h: in order to be |
| 92 | * able to safely update and access a watchpoint without introducing locking |
| 93 | * overhead, we encode each watchpoint as a single atomic long. The initial |
| 94 | * zero-initialized state matches INVALID_WATCHPOINT. |
| 95 | * |
| 96 | * Add NUM_SLOTS-1 entries to account for overflow; this helps avoid having to |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 97 | * use more complicated SLOT_IDX_FAST calculation with modulo in the fast-path. |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 98 | */ |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 99 | static atomic_long_t watchpoints[CONFIG_KCSAN_NUM_WATCHPOINTS + NUM_SLOTS-1]; |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 100 | |
| 101 | /* |
| 102 | * Instructions to skip watching counter, used in should_watch(). We use a |
| 103 | * per-CPU counter to avoid excessive contention. |
| 104 | */ |
| 105 | static DEFINE_PER_CPU(long, kcsan_skip); |
| 106 | |
Marco Elver | cd290ec | 2020-08-21 14:31:26 +0200 | [diff] [blame] | 107 | /* For kcsan_prandom_u32_max(). */ |
Marco Elver | 71a076f | 2020-11-24 12:02:09 +0100 | [diff] [blame] | 108 | static DEFINE_PER_CPU(u32, kcsan_rand_state); |
Marco Elver | cd290ec | 2020-08-21 14:31:26 +0200 | [diff] [blame] | 109 | |
Marco Elver | 5c36142 | 2020-01-07 17:31:04 +0100 | [diff] [blame] | 110 | static __always_inline atomic_long_t *find_watchpoint(unsigned long addr, |
| 111 | size_t size, |
| 112 | bool expect_write, |
| 113 | long *encoded_watchpoint) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 114 | { |
| 115 | const int slot = watchpoint_slot(addr); |
| 116 | const unsigned long addr_masked = addr & WATCHPOINT_ADDR_MASK; |
| 117 | atomic_long_t *watchpoint; |
| 118 | unsigned long wp_addr_masked; |
| 119 | size_t wp_size; |
| 120 | bool is_write; |
| 121 | int i; |
| 122 | |
| 123 | BUILD_BUG_ON(CONFIG_KCSAN_NUM_WATCHPOINTS < NUM_SLOTS); |
| 124 | |
| 125 | for (i = 0; i < NUM_SLOTS; ++i) { |
| 126 | watchpoint = &watchpoints[SLOT_IDX_FAST(slot, i)]; |
| 127 | *encoded_watchpoint = atomic_long_read(watchpoint); |
| 128 | if (!decode_watchpoint(*encoded_watchpoint, &wp_addr_masked, |
| 129 | &wp_size, &is_write)) |
| 130 | continue; |
| 131 | |
| 132 | if (expect_write && !is_write) |
| 133 | continue; |
| 134 | |
| 135 | /* Check if the watchpoint matches the access. */ |
| 136 | if (matching_access(wp_addr_masked, wp_size, addr_masked, size)) |
| 137 | return watchpoint; |
| 138 | } |
| 139 | |
| 140 | return NULL; |
| 141 | } |
| 142 | |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 143 | static inline atomic_long_t * |
| 144 | insert_watchpoint(unsigned long addr, size_t size, bool is_write) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 145 | { |
| 146 | const int slot = watchpoint_slot(addr); |
| 147 | const long encoded_watchpoint = encode_watchpoint(addr, size, is_write); |
| 148 | atomic_long_t *watchpoint; |
| 149 | int i; |
| 150 | |
| 151 | /* Check slot index logic, ensuring we stay within array bounds. */ |
| 152 | BUILD_BUG_ON(SLOT_IDX(0, 0) != KCSAN_CHECK_ADJACENT); |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 153 | BUILD_BUG_ON(SLOT_IDX(0, KCSAN_CHECK_ADJACENT+1) != 0); |
| 154 | BUILD_BUG_ON(SLOT_IDX(CONFIG_KCSAN_NUM_WATCHPOINTS-1, KCSAN_CHECK_ADJACENT) != ARRAY_SIZE(watchpoints)-1); |
| 155 | BUILD_BUG_ON(SLOT_IDX(CONFIG_KCSAN_NUM_WATCHPOINTS-1, KCSAN_CHECK_ADJACENT+1) != ARRAY_SIZE(watchpoints) - NUM_SLOTS); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 156 | |
| 157 | for (i = 0; i < NUM_SLOTS; ++i) { |
| 158 | long expect_val = INVALID_WATCHPOINT; |
| 159 | |
| 160 | /* Try to acquire this slot. */ |
| 161 | watchpoint = &watchpoints[SLOT_IDX(slot, i)]; |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 162 | if (atomic_long_try_cmpxchg_relaxed(watchpoint, &expect_val, encoded_watchpoint)) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 163 | return watchpoint; |
| 164 | } |
| 165 | |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Return true if watchpoint was successfully consumed, false otherwise. |
| 171 | * |
| 172 | * This may return false if: |
| 173 | * |
| 174 | * 1. another thread already consumed the watchpoint; |
| 175 | * 2. the thread that set up the watchpoint already removed it; |
| 176 | * 3. the watchpoint was removed and then re-used. |
| 177 | */ |
Marco Elver | 5c36142 | 2020-01-07 17:31:04 +0100 | [diff] [blame] | 178 | static __always_inline bool |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 179 | try_consume_watchpoint(atomic_long_t *watchpoint, long encoded_watchpoint) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 180 | { |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 181 | return atomic_long_try_cmpxchg_relaxed(watchpoint, &encoded_watchpoint, CONSUMED_WATCHPOINT); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 184 | /* Return true if watchpoint was not touched, false if already consumed. */ |
| 185 | static inline bool consume_watchpoint(atomic_long_t *watchpoint) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 186 | { |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 187 | return atomic_long_xchg_relaxed(watchpoint, CONSUMED_WATCHPOINT) != CONSUMED_WATCHPOINT; |
| 188 | } |
| 189 | |
| 190 | /* Remove the watchpoint -- its slot may be reused after. */ |
| 191 | static inline void remove_watchpoint(atomic_long_t *watchpoint) |
| 192 | { |
| 193 | atomic_long_set(watchpoint, INVALID_WATCHPOINT); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Marco Elver | 5c36142 | 2020-01-07 17:31:04 +0100 | [diff] [blame] | 196 | static __always_inline struct kcsan_ctx *get_ctx(void) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 197 | { |
| 198 | /* |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 199 | * In interrupts, use raw_cpu_ptr to avoid unnecessary checks, that would |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 200 | * also result in calls that generate warnings in uaccess regions. |
| 201 | */ |
| 202 | return in_task() ? ¤t->kcsan_ctx : raw_cpu_ptr(&kcsan_cpu_ctx); |
| 203 | } |
| 204 | |
Marco Elver | f4c87db | 2021-08-09 13:25:13 +0200 | [diff] [blame] | 205 | static __always_inline void |
| 206 | check_access(const volatile void *ptr, size_t size, int type, unsigned long ip); |
| 207 | |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 208 | /* Check scoped accesses; never inline because this is a slow-path! */ |
| 209 | static noinline void kcsan_check_scoped_accesses(void) |
| 210 | { |
| 211 | struct kcsan_ctx *ctx = get_ctx(); |
| 212 | struct list_head *prev_save = ctx->scoped_accesses.prev; |
| 213 | struct kcsan_scoped_access *scoped_access; |
| 214 | |
| 215 | ctx->scoped_accesses.prev = NULL; /* Avoid recursion. */ |
Marco Elver | f4c87db | 2021-08-09 13:25:13 +0200 | [diff] [blame] | 216 | list_for_each_entry(scoped_access, &ctx->scoped_accesses, list) { |
| 217 | check_access(scoped_access->ptr, scoped_access->size, |
| 218 | scoped_access->type, scoped_access->ip); |
| 219 | } |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 220 | ctx->scoped_accesses.prev = prev_save; |
| 221 | } |
| 222 | |
Marco Elver | 44656d3 | 2020-02-25 15:32:58 +0100 | [diff] [blame] | 223 | /* Rules for generic atomic accesses. Called from fast-path. */ |
Marco Elver | 1e6ee2f | 2020-02-04 18:21:10 +0100 | [diff] [blame] | 224 | static __always_inline bool |
Marco Elver | 78c3d95 | 2021-08-09 13:25:16 +0200 | [diff] [blame^] | 225 | is_atomic(struct kcsan_ctx *ctx, const volatile void *ptr, size_t size, int type) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 226 | { |
Marco Elver | 44656d3 | 2020-02-25 15:32:58 +0100 | [diff] [blame] | 227 | if (type & KCSAN_ACCESS_ATOMIC) |
Marco Elver | 1e6ee2f | 2020-02-04 18:21:10 +0100 | [diff] [blame] | 228 | return true; |
| 229 | |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 230 | /* |
| 231 | * Unless explicitly declared atomic, never consider an assertion access |
| 232 | * as atomic. This allows using them also in atomic regions, such as |
| 233 | * seqlocks, without implicitly changing their semantics. |
| 234 | */ |
Marco Elver | 44656d3 | 2020-02-25 15:32:58 +0100 | [diff] [blame] | 235 | if (type & KCSAN_ACCESS_ASSERT) |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 236 | return false; |
| 237 | |
Marco Elver | 1e6ee2f | 2020-02-04 18:21:10 +0100 | [diff] [blame] | 238 | if (IS_ENABLED(CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC) && |
Marco Elver | 44656d3 | 2020-02-25 15:32:58 +0100 | [diff] [blame] | 239 | (type & KCSAN_ACCESS_WRITE) && size <= sizeof(long) && |
Marco Elver | 14e2ac8 | 2020-07-24 09:00:01 +0200 | [diff] [blame] | 240 | !(type & KCSAN_ACCESS_COMPOUND) && IS_ALIGNED((unsigned long)ptr, size)) |
Marco Elver | 1e6ee2f | 2020-02-04 18:21:10 +0100 | [diff] [blame] | 241 | return true; /* Assume aligned writes up to word size are atomic. */ |
| 242 | |
Marco Elver | 44656d3 | 2020-02-25 15:32:58 +0100 | [diff] [blame] | 243 | if (ctx->atomic_next > 0) { |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 244 | /* |
| 245 | * Because we do not have separate contexts for nested |
| 246 | * interrupts, in case atomic_next is set, we simply assume that |
| 247 | * the outer interrupt set atomic_next. In the worst case, we |
| 248 | * will conservatively consider operations as atomic. This is a |
| 249 | * reasonable trade-off to make, since this case should be |
| 250 | * extremely rare; however, even if extremely rare, it could |
| 251 | * lead to false positives otherwise. |
| 252 | */ |
| 253 | if ((hardirq_count() >> HARDIRQ_SHIFT) < 2) |
| 254 | --ctx->atomic_next; /* in task, or outer interrupt */ |
| 255 | return true; |
| 256 | } |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 257 | |
Marco Elver | 44656d3 | 2020-02-25 15:32:58 +0100 | [diff] [blame] | 258 | return ctx->atomic_nest_count > 0 || ctx->in_flat_atomic; |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 259 | } |
| 260 | |
Marco Elver | 1e6ee2f | 2020-02-04 18:21:10 +0100 | [diff] [blame] | 261 | static __always_inline bool |
Marco Elver | 78c3d95 | 2021-08-09 13:25:16 +0200 | [diff] [blame^] | 262 | should_watch(struct kcsan_ctx *ctx, const volatile void *ptr, size_t size, int type) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 263 | { |
| 264 | /* |
| 265 | * Never set up watchpoints when memory operations are atomic. |
| 266 | * |
| 267 | * Need to check this first, before kcsan_skip check below: (1) atomics |
| 268 | * should not count towards skipped instructions, and (2) to actually |
| 269 | * decrement kcsan_atomic_next for consecutive instruction stream. |
| 270 | */ |
Marco Elver | 78c3d95 | 2021-08-09 13:25:16 +0200 | [diff] [blame^] | 271 | if (is_atomic(ctx, ptr, size, type)) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 272 | return false; |
| 273 | |
| 274 | if (this_cpu_dec_return(kcsan_skip) >= 0) |
| 275 | return false; |
| 276 | |
| 277 | /* |
| 278 | * NOTE: If we get here, kcsan_skip must always be reset in slow path |
| 279 | * via reset_kcsan_skip() to avoid underflow. |
| 280 | */ |
| 281 | |
| 282 | /* this operation should be watched */ |
| 283 | return true; |
| 284 | } |
| 285 | |
Marco Elver | cd290ec | 2020-08-21 14:31:26 +0200 | [diff] [blame] | 286 | /* |
Marco Elver | 71a076f | 2020-11-24 12:02:09 +0100 | [diff] [blame] | 287 | * Returns a pseudo-random number in interval [0, ep_ro). Simple linear |
| 288 | * congruential generator, using constants from "Numerical Recipes". |
Marco Elver | cd290ec | 2020-08-21 14:31:26 +0200 | [diff] [blame] | 289 | */ |
| 290 | static u32 kcsan_prandom_u32_max(u32 ep_ro) |
| 291 | { |
Marco Elver | 71a076f | 2020-11-24 12:02:09 +0100 | [diff] [blame] | 292 | u32 state = this_cpu_read(kcsan_rand_state); |
Marco Elver | cd290ec | 2020-08-21 14:31:26 +0200 | [diff] [blame] | 293 | |
Marco Elver | 71a076f | 2020-11-24 12:02:09 +0100 | [diff] [blame] | 294 | state = 1664525 * state + 1013904223; |
| 295 | this_cpu_write(kcsan_rand_state, state); |
| 296 | |
| 297 | return state % ep_ro; |
Marco Elver | cd290ec | 2020-08-21 14:31:26 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 300 | static inline void reset_kcsan_skip(void) |
| 301 | { |
Marco Elver | 80d4c47 | 2020-02-07 19:59:10 +0100 | [diff] [blame] | 302 | long skip_count = kcsan_skip_watch - |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 303 | (IS_ENABLED(CONFIG_KCSAN_SKIP_WATCH_RANDOMIZE) ? |
Marco Elver | cd290ec | 2020-08-21 14:31:26 +0200 | [diff] [blame] | 304 | kcsan_prandom_u32_max(kcsan_skip_watch) : |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 305 | 0); |
| 306 | this_cpu_write(kcsan_skip, skip_count); |
| 307 | } |
| 308 | |
Marco Elver | 08cac60 | 2021-06-07 14:56:50 +0200 | [diff] [blame] | 309 | static __always_inline bool kcsan_is_enabled(struct kcsan_ctx *ctx) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 310 | { |
Marco Elver | 08cac60 | 2021-06-07 14:56:50 +0200 | [diff] [blame] | 311 | return READ_ONCE(kcsan_enabled) && !ctx->disable_count; |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 312 | } |
| 313 | |
Marco Elver | cd290ec | 2020-08-21 14:31:26 +0200 | [diff] [blame] | 314 | /* Introduce delay depending on context and configuration. */ |
| 315 | static void delay_access(int type) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 316 | { |
Marco Elver | 80d4c47 | 2020-02-07 19:59:10 +0100 | [diff] [blame] | 317 | unsigned int delay = in_task() ? kcsan_udelay_task : kcsan_udelay_interrupt; |
Marco Elver | 106a307 | 2020-07-24 09:00:03 +0200 | [diff] [blame] | 318 | /* For certain access types, skew the random delay to be longer. */ |
| 319 | unsigned int skew_delay_order = |
| 320 | (type & (KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_ASSERT)) ? 1 : 0; |
| 321 | |
Marco Elver | cd290ec | 2020-08-21 14:31:26 +0200 | [diff] [blame] | 322 | delay -= IS_ENABLED(CONFIG_KCSAN_DELAY_RANDOMIZE) ? |
| 323 | kcsan_prandom_u32_max(delay >> skew_delay_order) : |
| 324 | 0; |
| 325 | udelay(delay); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 326 | } |
| 327 | |
Marco Elver | 92c209a | 2020-07-29 13:09:16 +0200 | [diff] [blame] | 328 | void kcsan_save_irqtrace(struct task_struct *task) |
| 329 | { |
| 330 | #ifdef CONFIG_TRACE_IRQFLAGS |
| 331 | task->kcsan_save_irqtrace = task->irqtrace; |
| 332 | #endif |
| 333 | } |
| 334 | |
| 335 | void kcsan_restore_irqtrace(struct task_struct *task) |
| 336 | { |
| 337 | #ifdef CONFIG_TRACE_IRQFLAGS |
| 338 | task->irqtrace = task->kcsan_save_irqtrace; |
| 339 | #endif |
| 340 | } |
| 341 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 342 | /* |
| 343 | * Pull everything together: check_access() below contains the performance |
| 344 | * critical operations; the fast-path (including check_access) functions should |
| 345 | * all be inlinable by the instrumentation functions. |
| 346 | * |
| 347 | * The slow-path (kcsan_found_watchpoint, kcsan_setup_watchpoint) are |
| 348 | * non-inlinable -- note that, we prefix these with "kcsan_" to ensure they can |
| 349 | * be filtered from the stacktrace, as well as give them unique names for the |
| 350 | * UACCESS whitelist of objtool. Each function uses user_access_save/restore(), |
| 351 | * since they do not access any user memory, but instrumentation is still |
| 352 | * emitted in UACCESS regions. |
| 353 | */ |
| 354 | |
| 355 | static noinline void kcsan_found_watchpoint(const volatile void *ptr, |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 356 | size_t size, |
Marco Elver | 47144ec | 2020-01-10 19:48:33 +0100 | [diff] [blame] | 357 | int type, |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 358 | unsigned long ip, |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 359 | atomic_long_t *watchpoint, |
| 360 | long encoded_watchpoint) |
| 361 | { |
Marco Elver | 49f72d5 | 2021-06-07 14:56:51 +0200 | [diff] [blame] | 362 | const bool is_assert = (type & KCSAN_ACCESS_ASSERT) != 0; |
Marco Elver | 08cac60 | 2021-06-07 14:56:50 +0200 | [diff] [blame] | 363 | struct kcsan_ctx *ctx = get_ctx(); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 364 | unsigned long flags; |
| 365 | bool consumed; |
| 366 | |
Marco Elver | 08cac60 | 2021-06-07 14:56:50 +0200 | [diff] [blame] | 367 | /* |
| 368 | * We know a watchpoint exists. Let's try to keep the race-window |
| 369 | * between here and finally consuming the watchpoint below as small as |
| 370 | * possible -- avoid unneccessarily complex code until consumed. |
| 371 | */ |
| 372 | |
| 373 | if (!kcsan_is_enabled(ctx)) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 374 | return; |
Marco Elver | 81af89e | 2020-02-11 17:04:22 +0100 | [diff] [blame] | 375 | |
| 376 | /* |
| 377 | * The access_mask check relies on value-change comparison. To avoid |
| 378 | * reporting a race where e.g. the writer set up the watchpoint, but the |
| 379 | * reader has access_mask!=0, we have to ignore the found watchpoint. |
| 380 | */ |
Marco Elver | 08cac60 | 2021-06-07 14:56:50 +0200 | [diff] [blame] | 381 | if (ctx->access_mask) |
Marco Elver | 81af89e | 2020-02-11 17:04:22 +0100 | [diff] [blame] | 382 | return; |
| 383 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 384 | /* |
Marco Elver | 49f72d5 | 2021-06-07 14:56:51 +0200 | [diff] [blame] | 385 | * If the other thread does not want to ignore the access, and there was |
| 386 | * a value change as a result of this thread's operation, we will still |
| 387 | * generate a report of unknown origin. |
| 388 | * |
| 389 | * Use CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=n to filter. |
| 390 | */ |
| 391 | if (!is_assert && kcsan_ignore_address(ptr)) |
| 392 | return; |
| 393 | |
| 394 | /* |
Marco Elver | 08cac60 | 2021-06-07 14:56:50 +0200 | [diff] [blame] | 395 | * Consuming the watchpoint must be guarded by kcsan_is_enabled() to |
| 396 | * avoid erroneously triggering reports if the context is disabled. |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 397 | */ |
| 398 | consumed = try_consume_watchpoint(watchpoint, encoded_watchpoint); |
| 399 | |
| 400 | /* keep this after try_consume_watchpoint */ |
| 401 | flags = user_access_save(); |
| 402 | |
| 403 | if (consumed) { |
Marco Elver | 92c209a | 2020-07-29 13:09:16 +0200 | [diff] [blame] | 404 | kcsan_save_irqtrace(current); |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 405 | kcsan_report_set_info(ptr, size, type, ip, watchpoint - watchpoints); |
Marco Elver | 92c209a | 2020-07-29 13:09:16 +0200 | [diff] [blame] | 406 | kcsan_restore_irqtrace(current); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 407 | } else { |
| 408 | /* |
| 409 | * The other thread may not print any diagnostics, as it has |
| 410 | * already removed the watchpoint, or another thread consumed |
| 411 | * the watchpoint before this thread. |
| 412 | */ |
Marco Elver | 2e986b8 | 2020-08-10 10:06:25 +0200 | [diff] [blame] | 413 | atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_REPORT_RACES]); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 414 | } |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 415 | |
Marco Elver | 49f72d5 | 2021-06-07 14:56:51 +0200 | [diff] [blame] | 416 | if (is_assert) |
Marco Elver | 2e986b8 | 2020-08-10 10:06:25 +0200 | [diff] [blame] | 417 | atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]); |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 418 | else |
Marco Elver | 2e986b8 | 2020-08-10 10:06:25 +0200 | [diff] [blame] | 419 | atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_DATA_RACES]); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 420 | |
| 421 | user_access_restore(flags); |
| 422 | } |
| 423 | |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 424 | static noinline void |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 425 | kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type, unsigned long ip) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 426 | { |
Marco Elver | 47144ec | 2020-01-10 19:48:33 +0100 | [diff] [blame] | 427 | const bool is_write = (type & KCSAN_ACCESS_WRITE) != 0; |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 428 | const bool is_assert = (type & KCSAN_ACCESS_ASSERT) != 0; |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 429 | atomic_long_t *watchpoint; |
Mark Rutland | 6f2d981 | 2021-04-14 13:28:17 +0200 | [diff] [blame] | 430 | u64 old, new, diff; |
Marco Elver | 81af89e | 2020-02-11 17:04:22 +0100 | [diff] [blame] | 431 | unsigned long access_mask; |
Marco Elver | b738f61 | 2020-02-11 17:04:21 +0100 | [diff] [blame] | 432 | enum kcsan_value_change value_change = KCSAN_VALUE_CHANGE_MAYBE; |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 433 | unsigned long ua_flags = user_access_save(); |
Marco Elver | 08cac60 | 2021-06-07 14:56:50 +0200 | [diff] [blame] | 434 | struct kcsan_ctx *ctx = get_ctx(); |
Marco Elver | 48b1fc1 | 2020-02-21 23:02:09 +0100 | [diff] [blame] | 435 | unsigned long irq_flags = 0; |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * Always reset kcsan_skip counter in slow-path to avoid underflow; see |
| 439 | * should_watch(). |
| 440 | */ |
| 441 | reset_kcsan_skip(); |
| 442 | |
Marco Elver | 08cac60 | 2021-06-07 14:56:50 +0200 | [diff] [blame] | 443 | if (!kcsan_is_enabled(ctx)) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 444 | goto out; |
| 445 | |
Marco Elver | 44656d3 | 2020-02-25 15:32:58 +0100 | [diff] [blame] | 446 | /* |
Marco Elver | 49f72d5 | 2021-06-07 14:56:51 +0200 | [diff] [blame] | 447 | * Check to-ignore addresses after kcsan_is_enabled(), as we may access |
| 448 | * memory that is not yet initialized during early boot. |
Marco Elver | 44656d3 | 2020-02-25 15:32:58 +0100 | [diff] [blame] | 449 | */ |
Marco Elver | 49f72d5 | 2021-06-07 14:56:51 +0200 | [diff] [blame] | 450 | if (!is_assert && kcsan_ignore_address(ptr)) |
Marco Elver | 44656d3 | 2020-02-25 15:32:58 +0100 | [diff] [blame] | 451 | goto out; |
| 452 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 453 | if (!check_encodable((unsigned long)ptr, size)) { |
Marco Elver | 2e986b8 | 2020-08-10 10:06:25 +0200 | [diff] [blame] | 454 | atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_UNENCODABLE_ACCESSES]); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 455 | goto out; |
| 456 | } |
| 457 | |
Marco Elver | 92c209a | 2020-07-29 13:09:16 +0200 | [diff] [blame] | 458 | /* |
| 459 | * Save and restore the IRQ state trace touched by KCSAN, since KCSAN's |
| 460 | * runtime is entered for every memory access, and potentially useful |
| 461 | * information is lost if dirtied by KCSAN. |
| 462 | */ |
| 463 | kcsan_save_irqtrace(current); |
Marco Elver | 48b1fc1 | 2020-02-21 23:02:09 +0100 | [diff] [blame] | 464 | if (!kcsan_interrupt_watcher) |
Marco Elver | 248591f | 2020-06-24 13:32:46 +0200 | [diff] [blame] | 465 | local_irq_save(irq_flags); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 466 | |
| 467 | watchpoint = insert_watchpoint((unsigned long)ptr, size, is_write); |
| 468 | if (watchpoint == NULL) { |
| 469 | /* |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 470 | * Out of capacity: the size of 'watchpoints', and the frequency |
| 471 | * with which should_watch() returns true should be tweaked so |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 472 | * that this case happens very rarely. |
| 473 | */ |
Marco Elver | 2e986b8 | 2020-08-10 10:06:25 +0200 | [diff] [blame] | 474 | atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_NO_CAPACITY]); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 475 | goto out_unlock; |
| 476 | } |
| 477 | |
Marco Elver | 2e986b8 | 2020-08-10 10:06:25 +0200 | [diff] [blame] | 478 | atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_SETUP_WATCHPOINTS]); |
| 479 | atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_USED_WATCHPOINTS]); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | * Read the current value, to later check and infer a race if the data |
| 483 | * was modified via a non-instrumented access, e.g. from a device. |
| 484 | */ |
Mark Rutland | 6f2d981 | 2021-04-14 13:28:17 +0200 | [diff] [blame] | 485 | old = 0; |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 486 | switch (size) { |
| 487 | case 1: |
Mark Rutland | 6f2d981 | 2021-04-14 13:28:17 +0200 | [diff] [blame] | 488 | old = READ_ONCE(*(const u8 *)ptr); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 489 | break; |
| 490 | case 2: |
Mark Rutland | 6f2d981 | 2021-04-14 13:28:17 +0200 | [diff] [blame] | 491 | old = READ_ONCE(*(const u16 *)ptr); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 492 | break; |
| 493 | case 4: |
Mark Rutland | 6f2d981 | 2021-04-14 13:28:17 +0200 | [diff] [blame] | 494 | old = READ_ONCE(*(const u32 *)ptr); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 495 | break; |
| 496 | case 8: |
Mark Rutland | 6f2d981 | 2021-04-14 13:28:17 +0200 | [diff] [blame] | 497 | old = READ_ONCE(*(const u64 *)ptr); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 498 | break; |
| 499 | default: |
| 500 | break; /* ignore; we do not diff the values */ |
| 501 | } |
| 502 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 503 | /* |
| 504 | * Delay this thread, to increase probability of observing a racy |
| 505 | * conflicting access. |
| 506 | */ |
Marco Elver | cd290ec | 2020-08-21 14:31:26 +0200 | [diff] [blame] | 507 | delay_access(type); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 508 | |
| 509 | /* |
| 510 | * Re-read value, and check if it is as expected; if not, we infer a |
| 511 | * racy access. |
| 512 | */ |
Marco Elver | 08cac60 | 2021-06-07 14:56:50 +0200 | [diff] [blame] | 513 | access_mask = ctx->access_mask; |
Mark Rutland | 6f2d981 | 2021-04-14 13:28:17 +0200 | [diff] [blame] | 514 | new = 0; |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 515 | switch (size) { |
| 516 | case 1: |
Mark Rutland | 6f2d981 | 2021-04-14 13:28:17 +0200 | [diff] [blame] | 517 | new = READ_ONCE(*(const u8 *)ptr); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 518 | break; |
| 519 | case 2: |
Mark Rutland | 6f2d981 | 2021-04-14 13:28:17 +0200 | [diff] [blame] | 520 | new = READ_ONCE(*(const u16 *)ptr); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 521 | break; |
| 522 | case 4: |
Mark Rutland | 6f2d981 | 2021-04-14 13:28:17 +0200 | [diff] [blame] | 523 | new = READ_ONCE(*(const u32 *)ptr); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 524 | break; |
| 525 | case 8: |
Mark Rutland | 6f2d981 | 2021-04-14 13:28:17 +0200 | [diff] [blame] | 526 | new = READ_ONCE(*(const u64 *)ptr); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 527 | break; |
| 528 | default: |
| 529 | break; /* ignore; we do not diff the values */ |
| 530 | } |
| 531 | |
Mark Rutland | 6f2d981 | 2021-04-14 13:28:17 +0200 | [diff] [blame] | 532 | diff = old ^ new; |
| 533 | if (access_mask) |
| 534 | diff &= access_mask; |
| 535 | |
Marco Elver | 49f72d5 | 2021-06-07 14:56:51 +0200 | [diff] [blame] | 536 | /* |
| 537 | * Check if we observed a value change. |
| 538 | * |
| 539 | * Also check if the data race should be ignored (the rules depend on |
| 540 | * non-zero diff); if it is to be ignored, the below rules for |
| 541 | * KCSAN_VALUE_CHANGE_MAYBE apply. |
| 542 | */ |
| 543 | if (diff && !kcsan_ignore_data_race(size, type, old, new, diff)) |
Marco Elver | b738f61 | 2020-02-11 17:04:21 +0100 | [diff] [blame] | 544 | value_change = KCSAN_VALUE_CHANGE_TRUE; |
| 545 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 546 | /* Check if this access raced with another. */ |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 547 | if (!consume_watchpoint(watchpoint)) { |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 548 | /* |
Marco Elver | b738f61 | 2020-02-11 17:04:21 +0100 | [diff] [blame] | 549 | * Depending on the access type, map a value_change of MAYBE to |
Marco Elver | 81af89e | 2020-02-11 17:04:22 +0100 | [diff] [blame] | 550 | * TRUE (always report) or FALSE (never report). |
Marco Elver | b738f61 | 2020-02-11 17:04:21 +0100 | [diff] [blame] | 551 | */ |
Marco Elver | 81af89e | 2020-02-11 17:04:22 +0100 | [diff] [blame] | 552 | if (value_change == KCSAN_VALUE_CHANGE_MAYBE) { |
| 553 | if (access_mask != 0) { |
| 554 | /* |
| 555 | * For access with access_mask, we require a |
| 556 | * value-change, as it is likely that races on |
| 557 | * ~access_mask bits are expected. |
| 558 | */ |
| 559 | value_change = KCSAN_VALUE_CHANGE_FALSE; |
| 560 | } else if (size > 8 || is_assert) { |
| 561 | /* Always assume a value-change. */ |
| 562 | value_change = KCSAN_VALUE_CHANGE_TRUE; |
| 563 | } |
Marco Elver | b738f61 | 2020-02-11 17:04:21 +0100 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /* |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 567 | * No need to increment 'data_races' counter, as the racing |
| 568 | * thread already did. |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 569 | * |
| 570 | * Count 'assert_failures' for each failed ASSERT access, |
| 571 | * therefore both this thread and the racing thread may |
| 572 | * increment this counter. |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 573 | */ |
Marco Elver | b738f61 | 2020-02-11 17:04:21 +0100 | [diff] [blame] | 574 | if (is_assert && value_change == KCSAN_VALUE_CHANGE_TRUE) |
Marco Elver | 2e986b8 | 2020-08-10 10:06:25 +0200 | [diff] [blame] | 575 | atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]); |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 576 | |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 577 | kcsan_report_known_origin(ptr, size, type, ip, |
| 578 | value_change, watchpoint - watchpoints, |
Mark Rutland | 7bbe6dc | 2021-04-14 13:28:24 +0200 | [diff] [blame] | 579 | old, new, access_mask); |
Marco Elver | b738f61 | 2020-02-11 17:04:21 +0100 | [diff] [blame] | 580 | } else if (value_change == KCSAN_VALUE_CHANGE_TRUE) { |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 581 | /* Inferring a race, since the value should not have changed. */ |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 582 | |
Marco Elver | 2e986b8 | 2020-08-10 10:06:25 +0200 | [diff] [blame] | 583 | atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN]); |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 584 | if (is_assert) |
Marco Elver | 2e986b8 | 2020-08-10 10:06:25 +0200 | [diff] [blame] | 585 | atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]); |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 586 | |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 587 | if (IS_ENABLED(CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN) || is_assert) { |
| 588 | kcsan_report_unknown_origin(ptr, size, type, ip, |
| 589 | old, new, access_mask); |
| 590 | } |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 593 | /* |
| 594 | * Remove watchpoint; must be after reporting, since the slot may be |
| 595 | * reused after this point. |
| 596 | */ |
| 597 | remove_watchpoint(watchpoint); |
Marco Elver | 2e986b8 | 2020-08-10 10:06:25 +0200 | [diff] [blame] | 598 | atomic_long_dec(&kcsan_counters[KCSAN_COUNTER_USED_WATCHPOINTS]); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 599 | out_unlock: |
Marco Elver | 48b1fc1 | 2020-02-21 23:02:09 +0100 | [diff] [blame] | 600 | if (!kcsan_interrupt_watcher) |
Marco Elver | 248591f | 2020-06-24 13:32:46 +0200 | [diff] [blame] | 601 | local_irq_restore(irq_flags); |
Marco Elver | 92c209a | 2020-07-29 13:09:16 +0200 | [diff] [blame] | 602 | kcsan_restore_irqtrace(current); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 603 | out: |
| 604 | user_access_restore(ua_flags); |
| 605 | } |
| 606 | |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 607 | static __always_inline void |
| 608 | check_access(const volatile void *ptr, size_t size, int type, unsigned long ip) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 609 | { |
| 610 | const bool is_write = (type & KCSAN_ACCESS_WRITE) != 0; |
| 611 | atomic_long_t *watchpoint; |
| 612 | long encoded_watchpoint; |
| 613 | |
| 614 | /* |
Marco Elver | ed95f95 | 2020-02-05 11:14:19 +0100 | [diff] [blame] | 615 | * Do nothing for 0 sized check; this comparison will be optimized out |
| 616 | * for constant sized instrumentation (__tsan_{read,write}N). |
| 617 | */ |
| 618 | if (unlikely(size == 0)) |
| 619 | return; |
| 620 | |
| 621 | /* |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 622 | * Avoid user_access_save in fast-path: find_watchpoint is safe without |
| 623 | * user_access_save, as the address that ptr points to is only used to |
| 624 | * check if a watchpoint exists; ptr is never dereferenced. |
| 625 | */ |
| 626 | watchpoint = find_watchpoint((unsigned long)ptr, size, !is_write, |
| 627 | &encoded_watchpoint); |
| 628 | /* |
| 629 | * It is safe to check kcsan_is_enabled() after find_watchpoint in the |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 630 | * slow-path, as long as no state changes that cause a race to be |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 631 | * detected and reported have occurred until kcsan_is_enabled() is |
| 632 | * checked. |
| 633 | */ |
| 634 | |
| 635 | if (unlikely(watchpoint != NULL)) |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 636 | kcsan_found_watchpoint(ptr, size, type, ip, watchpoint, encoded_watchpoint); |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 637 | else { |
| 638 | struct kcsan_ctx *ctx = get_ctx(); /* Call only once in fast-path. */ |
| 639 | |
Marco Elver | 78c3d95 | 2021-08-09 13:25:16 +0200 | [diff] [blame^] | 640 | if (unlikely(should_watch(ctx, ptr, size, type))) |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 641 | kcsan_setup_watchpoint(ptr, size, type, ip); |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 642 | else if (unlikely(ctx->scoped_accesses.prev)) |
| 643 | kcsan_check_scoped_accesses(); |
| 644 | } |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /* === Public interface ===================================================== */ |
| 648 | |
| 649 | void __init kcsan_init(void) |
| 650 | { |
Marco Elver | 71a076f | 2020-11-24 12:02:09 +0100 | [diff] [blame] | 651 | int cpu; |
| 652 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 653 | BUG_ON(!in_task()); |
| 654 | |
Marco Elver | 71a076f | 2020-11-24 12:02:09 +0100 | [diff] [blame] | 655 | for_each_possible_cpu(cpu) |
| 656 | per_cpu(kcsan_rand_state, cpu) = (u32)get_cycles(); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 657 | |
| 658 | /* |
| 659 | * We are in the init task, and no other tasks should be running; |
| 660 | * WRITE_ONCE without memory barrier is sufficient. |
| 661 | */ |
Marco Elver | 2778793 | 2020-07-31 10:17:22 +0200 | [diff] [blame] | 662 | if (kcsan_early_enable) { |
| 663 | pr_info("enabled early\n"); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 664 | WRITE_ONCE(kcsan_enabled, true); |
Marco Elver | 2778793 | 2020-07-31 10:17:22 +0200 | [diff] [blame] | 665 | } |
Marco Elver | 9c827cd | 2021-06-07 14:56:52 +0200 | [diff] [blame] | 666 | |
| 667 | if (IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY) || |
| 668 | IS_ENABLED(CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC) || |
| 669 | IS_ENABLED(CONFIG_KCSAN_PERMISSIVE) || |
| 670 | IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) { |
| 671 | pr_warn("non-strict mode configured - use CONFIG_KCSAN_STRICT=y to see all data races\n"); |
| 672 | } else { |
| 673 | pr_info("strict mode configured\n"); |
| 674 | } |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /* === Exported interface =================================================== */ |
| 678 | |
| 679 | void kcsan_disable_current(void) |
| 680 | { |
| 681 | ++get_ctx()->disable_count; |
| 682 | } |
| 683 | EXPORT_SYMBOL(kcsan_disable_current); |
| 684 | |
| 685 | void kcsan_enable_current(void) |
| 686 | { |
| 687 | if (get_ctx()->disable_count-- == 0) { |
| 688 | /* |
| 689 | * Warn if kcsan_enable_current() calls are unbalanced with |
| 690 | * kcsan_disable_current() calls, which causes disable_count to |
| 691 | * become negative and should not happen. |
| 692 | */ |
| 693 | kcsan_disable_current(); /* restore to 0, KCSAN still enabled */ |
| 694 | kcsan_disable_current(); /* disable to generate warning */ |
| 695 | WARN(1, "Unbalanced %s()", __func__); |
| 696 | kcsan_enable_current(); |
| 697 | } |
| 698 | } |
| 699 | EXPORT_SYMBOL(kcsan_enable_current); |
| 700 | |
Marco Elver | 19acd03 | 2020-04-24 17:47:29 +0200 | [diff] [blame] | 701 | void kcsan_enable_current_nowarn(void) |
| 702 | { |
| 703 | if (get_ctx()->disable_count-- == 0) |
| 704 | kcsan_disable_current(); |
| 705 | } |
| 706 | EXPORT_SYMBOL(kcsan_enable_current_nowarn); |
| 707 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 708 | void kcsan_nestable_atomic_begin(void) |
| 709 | { |
| 710 | /* |
| 711 | * Do *not* check and warn if we are in a flat atomic region: nestable |
| 712 | * and flat atomic regions are independent from each other. |
| 713 | * See include/linux/kcsan.h: struct kcsan_ctx comments for more |
| 714 | * comments. |
| 715 | */ |
| 716 | |
| 717 | ++get_ctx()->atomic_nest_count; |
| 718 | } |
| 719 | EXPORT_SYMBOL(kcsan_nestable_atomic_begin); |
| 720 | |
| 721 | void kcsan_nestable_atomic_end(void) |
| 722 | { |
| 723 | if (get_ctx()->atomic_nest_count-- == 0) { |
| 724 | /* |
| 725 | * Warn if kcsan_nestable_atomic_end() calls are unbalanced with |
| 726 | * kcsan_nestable_atomic_begin() calls, which causes |
| 727 | * atomic_nest_count to become negative and should not happen. |
| 728 | */ |
| 729 | kcsan_nestable_atomic_begin(); /* restore to 0 */ |
| 730 | kcsan_disable_current(); /* disable to generate warning */ |
| 731 | WARN(1, "Unbalanced %s()", __func__); |
| 732 | kcsan_enable_current(); |
| 733 | } |
| 734 | } |
| 735 | EXPORT_SYMBOL(kcsan_nestable_atomic_end); |
| 736 | |
| 737 | void kcsan_flat_atomic_begin(void) |
| 738 | { |
| 739 | get_ctx()->in_flat_atomic = true; |
| 740 | } |
| 741 | EXPORT_SYMBOL(kcsan_flat_atomic_begin); |
| 742 | |
| 743 | void kcsan_flat_atomic_end(void) |
| 744 | { |
| 745 | get_ctx()->in_flat_atomic = false; |
| 746 | } |
| 747 | EXPORT_SYMBOL(kcsan_flat_atomic_end); |
| 748 | |
| 749 | void kcsan_atomic_next(int n) |
| 750 | { |
| 751 | get_ctx()->atomic_next = n; |
| 752 | } |
| 753 | EXPORT_SYMBOL(kcsan_atomic_next); |
| 754 | |
Marco Elver | 81af89e | 2020-02-11 17:04:22 +0100 | [diff] [blame] | 755 | void kcsan_set_access_mask(unsigned long mask) |
| 756 | { |
| 757 | get_ctx()->access_mask = mask; |
| 758 | } |
| 759 | EXPORT_SYMBOL(kcsan_set_access_mask); |
| 760 | |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 761 | struct kcsan_scoped_access * |
| 762 | kcsan_begin_scoped_access(const volatile void *ptr, size_t size, int type, |
| 763 | struct kcsan_scoped_access *sa) |
| 764 | { |
| 765 | struct kcsan_ctx *ctx = get_ctx(); |
| 766 | |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 767 | check_access(ptr, size, type, _RET_IP_); |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 768 | |
| 769 | ctx->disable_count++; /* Disable KCSAN, in case list debugging is on. */ |
| 770 | |
| 771 | INIT_LIST_HEAD(&sa->list); |
| 772 | sa->ptr = ptr; |
| 773 | sa->size = size; |
| 774 | sa->type = type; |
Marco Elver | f4c87db | 2021-08-09 13:25:13 +0200 | [diff] [blame] | 775 | sa->ip = _RET_IP_; |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 776 | |
| 777 | if (!ctx->scoped_accesses.prev) /* Lazy initialize list head. */ |
| 778 | INIT_LIST_HEAD(&ctx->scoped_accesses); |
| 779 | list_add(&sa->list, &ctx->scoped_accesses); |
| 780 | |
| 781 | ctx->disable_count--; |
| 782 | return sa; |
| 783 | } |
| 784 | EXPORT_SYMBOL(kcsan_begin_scoped_access); |
| 785 | |
| 786 | void kcsan_end_scoped_access(struct kcsan_scoped_access *sa) |
| 787 | { |
| 788 | struct kcsan_ctx *ctx = get_ctx(); |
| 789 | |
| 790 | if (WARN(!ctx->scoped_accesses.prev, "Unbalanced %s()?", __func__)) |
| 791 | return; |
| 792 | |
| 793 | ctx->disable_count++; /* Disable KCSAN, in case list debugging is on. */ |
| 794 | |
| 795 | list_del(&sa->list); |
| 796 | if (list_empty(&ctx->scoped_accesses)) |
| 797 | /* |
| 798 | * Ensure we do not enter kcsan_check_scoped_accesses() |
| 799 | * slow-path if unnecessary, and avoids requiring list_empty() |
| 800 | * in the fast-path (to avoid a READ_ONCE() and potential |
| 801 | * uaccess warning). |
| 802 | */ |
| 803 | ctx->scoped_accesses.prev = NULL; |
| 804 | |
| 805 | ctx->disable_count--; |
| 806 | |
Marco Elver | f4c87db | 2021-08-09 13:25:13 +0200 | [diff] [blame] | 807 | check_access(sa->ptr, sa->size, sa->type, sa->ip); |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 808 | } |
| 809 | EXPORT_SYMBOL(kcsan_end_scoped_access); |
| 810 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 811 | void __kcsan_check_access(const volatile void *ptr, size_t size, int type) |
| 812 | { |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 813 | check_access(ptr, size, type, _RET_IP_); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 814 | } |
| 815 | EXPORT_SYMBOL(__kcsan_check_access); |
| 816 | |
| 817 | /* |
| 818 | * KCSAN uses the same instrumentation that is emitted by supported compilers |
| 819 | * for ThreadSanitizer (TSAN). |
| 820 | * |
| 821 | * When enabled, the compiler emits instrumentation calls (the functions |
| 822 | * prefixed with "__tsan" below) for all loads and stores that it generated; |
| 823 | * inline asm is not instrumented. |
| 824 | * |
| 825 | * Note that, not all supported compiler versions distinguish aligned/unaligned |
| 826 | * accesses, but e.g. recent versions of Clang do. We simply alias the unaligned |
| 827 | * version to the generic version, which can handle both. |
| 828 | */ |
| 829 | |
| 830 | #define DEFINE_TSAN_READ_WRITE(size) \ |
Marco Elver | 9dd979b | 2020-06-16 14:36:22 +0200 | [diff] [blame] | 831 | void __tsan_read##size(void *ptr); \ |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 832 | void __tsan_read##size(void *ptr) \ |
| 833 | { \ |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 834 | check_access(ptr, size, 0, _RET_IP_); \ |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 835 | } \ |
| 836 | EXPORT_SYMBOL(__tsan_read##size); \ |
| 837 | void __tsan_unaligned_read##size(void *ptr) \ |
| 838 | __alias(__tsan_read##size); \ |
| 839 | EXPORT_SYMBOL(__tsan_unaligned_read##size); \ |
Marco Elver | 9dd979b | 2020-06-16 14:36:22 +0200 | [diff] [blame] | 840 | void __tsan_write##size(void *ptr); \ |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 841 | void __tsan_write##size(void *ptr) \ |
| 842 | { \ |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 843 | check_access(ptr, size, KCSAN_ACCESS_WRITE, _RET_IP_); \ |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 844 | } \ |
| 845 | EXPORT_SYMBOL(__tsan_write##size); \ |
| 846 | void __tsan_unaligned_write##size(void *ptr) \ |
| 847 | __alias(__tsan_write##size); \ |
Marco Elver | 14e2ac8 | 2020-07-24 09:00:01 +0200 | [diff] [blame] | 848 | EXPORT_SYMBOL(__tsan_unaligned_write##size); \ |
| 849 | void __tsan_read_write##size(void *ptr); \ |
| 850 | void __tsan_read_write##size(void *ptr) \ |
| 851 | { \ |
| 852 | check_access(ptr, size, \ |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 853 | KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE, \ |
| 854 | _RET_IP_); \ |
Marco Elver | 14e2ac8 | 2020-07-24 09:00:01 +0200 | [diff] [blame] | 855 | } \ |
| 856 | EXPORT_SYMBOL(__tsan_read_write##size); \ |
| 857 | void __tsan_unaligned_read_write##size(void *ptr) \ |
| 858 | __alias(__tsan_read_write##size); \ |
| 859 | EXPORT_SYMBOL(__tsan_unaligned_read_write##size) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 860 | |
| 861 | DEFINE_TSAN_READ_WRITE(1); |
| 862 | DEFINE_TSAN_READ_WRITE(2); |
| 863 | DEFINE_TSAN_READ_WRITE(4); |
| 864 | DEFINE_TSAN_READ_WRITE(8); |
| 865 | DEFINE_TSAN_READ_WRITE(16); |
| 866 | |
Marco Elver | 9dd979b | 2020-06-16 14:36:22 +0200 | [diff] [blame] | 867 | void __tsan_read_range(void *ptr, size_t size); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 868 | void __tsan_read_range(void *ptr, size_t size) |
| 869 | { |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 870 | check_access(ptr, size, 0, _RET_IP_); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 871 | } |
| 872 | EXPORT_SYMBOL(__tsan_read_range); |
| 873 | |
Marco Elver | 9dd979b | 2020-06-16 14:36:22 +0200 | [diff] [blame] | 874 | void __tsan_write_range(void *ptr, size_t size); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 875 | void __tsan_write_range(void *ptr, size_t size) |
| 876 | { |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 877 | check_access(ptr, size, KCSAN_ACCESS_WRITE, _RET_IP_); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 878 | } |
| 879 | EXPORT_SYMBOL(__tsan_write_range); |
| 880 | |
| 881 | /* |
Marco Elver | 75d75b7 | 2020-05-21 16:20:39 +0200 | [diff] [blame] | 882 | * Use of explicit volatile is generally disallowed [1], however, volatile is |
| 883 | * still used in various concurrent context, whether in low-level |
| 884 | * synchronization primitives or for legacy reasons. |
| 885 | * [1] https://lwn.net/Articles/233479/ |
| 886 | * |
| 887 | * We only consider volatile accesses atomic if they are aligned and would pass |
| 888 | * the size-check of compiletime_assert_rwonce_type(). |
| 889 | */ |
| 890 | #define DEFINE_TSAN_VOLATILE_READ_WRITE(size) \ |
Marco Elver | 9dd979b | 2020-06-16 14:36:22 +0200 | [diff] [blame] | 891 | void __tsan_volatile_read##size(void *ptr); \ |
Marco Elver | 75d75b7 | 2020-05-21 16:20:39 +0200 | [diff] [blame] | 892 | void __tsan_volatile_read##size(void *ptr) \ |
| 893 | { \ |
| 894 | const bool is_atomic = size <= sizeof(long long) && \ |
| 895 | IS_ALIGNED((unsigned long)ptr, size); \ |
| 896 | if (IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS) && is_atomic) \ |
| 897 | return; \ |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 898 | check_access(ptr, size, is_atomic ? KCSAN_ACCESS_ATOMIC : 0, \ |
| 899 | _RET_IP_); \ |
Marco Elver | 75d75b7 | 2020-05-21 16:20:39 +0200 | [diff] [blame] | 900 | } \ |
| 901 | EXPORT_SYMBOL(__tsan_volatile_read##size); \ |
| 902 | void __tsan_unaligned_volatile_read##size(void *ptr) \ |
| 903 | __alias(__tsan_volatile_read##size); \ |
| 904 | EXPORT_SYMBOL(__tsan_unaligned_volatile_read##size); \ |
Marco Elver | 9dd979b | 2020-06-16 14:36:22 +0200 | [diff] [blame] | 905 | void __tsan_volatile_write##size(void *ptr); \ |
Marco Elver | 75d75b7 | 2020-05-21 16:20:39 +0200 | [diff] [blame] | 906 | void __tsan_volatile_write##size(void *ptr) \ |
| 907 | { \ |
| 908 | const bool is_atomic = size <= sizeof(long long) && \ |
| 909 | IS_ALIGNED((unsigned long)ptr, size); \ |
| 910 | if (IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS) && is_atomic) \ |
| 911 | return; \ |
| 912 | check_access(ptr, size, \ |
| 913 | KCSAN_ACCESS_WRITE | \ |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 914 | (is_atomic ? KCSAN_ACCESS_ATOMIC : 0), \ |
| 915 | _RET_IP_); \ |
Marco Elver | 75d75b7 | 2020-05-21 16:20:39 +0200 | [diff] [blame] | 916 | } \ |
| 917 | EXPORT_SYMBOL(__tsan_volatile_write##size); \ |
| 918 | void __tsan_unaligned_volatile_write##size(void *ptr) \ |
| 919 | __alias(__tsan_volatile_write##size); \ |
| 920 | EXPORT_SYMBOL(__tsan_unaligned_volatile_write##size) |
| 921 | |
| 922 | DEFINE_TSAN_VOLATILE_READ_WRITE(1); |
| 923 | DEFINE_TSAN_VOLATILE_READ_WRITE(2); |
| 924 | DEFINE_TSAN_VOLATILE_READ_WRITE(4); |
| 925 | DEFINE_TSAN_VOLATILE_READ_WRITE(8); |
| 926 | DEFINE_TSAN_VOLATILE_READ_WRITE(16); |
| 927 | |
| 928 | /* |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 929 | * The below are not required by KCSAN, but can still be emitted by the |
| 930 | * compiler. |
| 931 | */ |
Marco Elver | 9dd979b | 2020-06-16 14:36:22 +0200 | [diff] [blame] | 932 | void __tsan_func_entry(void *call_pc); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 933 | void __tsan_func_entry(void *call_pc) |
| 934 | { |
| 935 | } |
| 936 | EXPORT_SYMBOL(__tsan_func_entry); |
Marco Elver | 9dd979b | 2020-06-16 14:36:22 +0200 | [diff] [blame] | 937 | void __tsan_func_exit(void); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 938 | void __tsan_func_exit(void) |
| 939 | { |
| 940 | } |
| 941 | EXPORT_SYMBOL(__tsan_func_exit); |
Marco Elver | 9dd979b | 2020-06-16 14:36:22 +0200 | [diff] [blame] | 942 | void __tsan_init(void); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 943 | void __tsan_init(void) |
| 944 | { |
| 945 | } |
| 946 | EXPORT_SYMBOL(__tsan_init); |
Marco Elver | 0f8ad5f | 2020-07-03 15:40:29 +0200 | [diff] [blame] | 947 | |
| 948 | /* |
| 949 | * Instrumentation for atomic builtins (__atomic_*, __sync_*). |
| 950 | * |
| 951 | * Normal kernel code _should not_ be using them directly, but some |
| 952 | * architectures may implement some or all atomics using the compilers' |
| 953 | * builtins. |
| 954 | * |
| 955 | * Note: If an architecture decides to fully implement atomics using the |
| 956 | * builtins, because they are implicitly instrumented by KCSAN (and KASAN, |
| 957 | * etc.), implementing the ARCH_ATOMIC interface (to get instrumentation via |
| 958 | * atomic-instrumented) is no longer necessary. |
| 959 | * |
| 960 | * TSAN instrumentation replaces atomic accesses with calls to any of the below |
| 961 | * functions, whose job is to also execute the operation itself. |
| 962 | */ |
| 963 | |
| 964 | #define DEFINE_TSAN_ATOMIC_LOAD_STORE(bits) \ |
| 965 | u##bits __tsan_atomic##bits##_load(const u##bits *ptr, int memorder); \ |
| 966 | u##bits __tsan_atomic##bits##_load(const u##bits *ptr, int memorder) \ |
| 967 | { \ |
Marco Elver | 9d1335c | 2020-07-24 09:00:04 +0200 | [diff] [blame] | 968 | if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) { \ |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 969 | check_access(ptr, bits / BITS_PER_BYTE, KCSAN_ACCESS_ATOMIC, _RET_IP_); \ |
Marco Elver | 9d1335c | 2020-07-24 09:00:04 +0200 | [diff] [blame] | 970 | } \ |
Marco Elver | 0f8ad5f | 2020-07-03 15:40:29 +0200 | [diff] [blame] | 971 | return __atomic_load_n(ptr, memorder); \ |
| 972 | } \ |
| 973 | EXPORT_SYMBOL(__tsan_atomic##bits##_load); \ |
| 974 | void __tsan_atomic##bits##_store(u##bits *ptr, u##bits v, int memorder); \ |
| 975 | void __tsan_atomic##bits##_store(u##bits *ptr, u##bits v, int memorder) \ |
| 976 | { \ |
Marco Elver | 9d1335c | 2020-07-24 09:00:04 +0200 | [diff] [blame] | 977 | if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) { \ |
| 978 | check_access(ptr, bits / BITS_PER_BYTE, \ |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 979 | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC, _RET_IP_); \ |
Marco Elver | 9d1335c | 2020-07-24 09:00:04 +0200 | [diff] [blame] | 980 | } \ |
Marco Elver | 0f8ad5f | 2020-07-03 15:40:29 +0200 | [diff] [blame] | 981 | __atomic_store_n(ptr, v, memorder); \ |
| 982 | } \ |
| 983 | EXPORT_SYMBOL(__tsan_atomic##bits##_store) |
| 984 | |
| 985 | #define DEFINE_TSAN_ATOMIC_RMW(op, bits, suffix) \ |
| 986 | u##bits __tsan_atomic##bits##_##op(u##bits *ptr, u##bits v, int memorder); \ |
| 987 | u##bits __tsan_atomic##bits##_##op(u##bits *ptr, u##bits v, int memorder) \ |
| 988 | { \ |
Marco Elver | 9d1335c | 2020-07-24 09:00:04 +0200 | [diff] [blame] | 989 | if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) { \ |
| 990 | check_access(ptr, bits / BITS_PER_BYTE, \ |
| 991 | KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | \ |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 992 | KCSAN_ACCESS_ATOMIC, _RET_IP_); \ |
Marco Elver | 9d1335c | 2020-07-24 09:00:04 +0200 | [diff] [blame] | 993 | } \ |
Marco Elver | 0f8ad5f | 2020-07-03 15:40:29 +0200 | [diff] [blame] | 994 | return __atomic_##op##suffix(ptr, v, memorder); \ |
| 995 | } \ |
| 996 | EXPORT_SYMBOL(__tsan_atomic##bits##_##op) |
| 997 | |
| 998 | /* |
| 999 | * Note: CAS operations are always classified as write, even in case they |
| 1000 | * fail. We cannot perform check_access() after a write, as it might lead to |
| 1001 | * false positives, in cases such as: |
| 1002 | * |
| 1003 | * T0: __atomic_compare_exchange_n(&p->flag, &old, 1, ...) |
| 1004 | * |
| 1005 | * T1: if (__atomic_load_n(&p->flag, ...)) { |
| 1006 | * modify *p; |
| 1007 | * p->flag = 0; |
| 1008 | * } |
| 1009 | * |
| 1010 | * The only downside is that, if there are 3 threads, with one CAS that |
| 1011 | * succeeds, another CAS that fails, and an unmarked racing operation, we may |
| 1012 | * point at the wrong CAS as the source of the race. However, if we assume that |
| 1013 | * all CAS can succeed in some other execution, the data race is still valid. |
| 1014 | */ |
| 1015 | #define DEFINE_TSAN_ATOMIC_CMPXCHG(bits, strength, weak) \ |
| 1016 | int __tsan_atomic##bits##_compare_exchange_##strength(u##bits *ptr, u##bits *exp, \ |
| 1017 | u##bits val, int mo, int fail_mo); \ |
| 1018 | int __tsan_atomic##bits##_compare_exchange_##strength(u##bits *ptr, u##bits *exp, \ |
| 1019 | u##bits val, int mo, int fail_mo) \ |
| 1020 | { \ |
Marco Elver | 9d1335c | 2020-07-24 09:00:04 +0200 | [diff] [blame] | 1021 | if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) { \ |
| 1022 | check_access(ptr, bits / BITS_PER_BYTE, \ |
| 1023 | KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | \ |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 1024 | KCSAN_ACCESS_ATOMIC, _RET_IP_); \ |
Marco Elver | 9d1335c | 2020-07-24 09:00:04 +0200 | [diff] [blame] | 1025 | } \ |
Marco Elver | 0f8ad5f | 2020-07-03 15:40:29 +0200 | [diff] [blame] | 1026 | return __atomic_compare_exchange_n(ptr, exp, val, weak, mo, fail_mo); \ |
| 1027 | } \ |
| 1028 | EXPORT_SYMBOL(__tsan_atomic##bits##_compare_exchange_##strength) |
| 1029 | |
| 1030 | #define DEFINE_TSAN_ATOMIC_CMPXCHG_VAL(bits) \ |
| 1031 | u##bits __tsan_atomic##bits##_compare_exchange_val(u##bits *ptr, u##bits exp, u##bits val, \ |
| 1032 | int mo, int fail_mo); \ |
| 1033 | u##bits __tsan_atomic##bits##_compare_exchange_val(u##bits *ptr, u##bits exp, u##bits val, \ |
| 1034 | int mo, int fail_mo) \ |
| 1035 | { \ |
Marco Elver | 9d1335c | 2020-07-24 09:00:04 +0200 | [diff] [blame] | 1036 | if (!IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) { \ |
| 1037 | check_access(ptr, bits / BITS_PER_BYTE, \ |
| 1038 | KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | \ |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 1039 | KCSAN_ACCESS_ATOMIC, _RET_IP_); \ |
Marco Elver | 9d1335c | 2020-07-24 09:00:04 +0200 | [diff] [blame] | 1040 | } \ |
Marco Elver | 0f8ad5f | 2020-07-03 15:40:29 +0200 | [diff] [blame] | 1041 | __atomic_compare_exchange_n(ptr, &exp, val, 0, mo, fail_mo); \ |
| 1042 | return exp; \ |
| 1043 | } \ |
| 1044 | EXPORT_SYMBOL(__tsan_atomic##bits##_compare_exchange_val) |
| 1045 | |
| 1046 | #define DEFINE_TSAN_ATOMIC_OPS(bits) \ |
| 1047 | DEFINE_TSAN_ATOMIC_LOAD_STORE(bits); \ |
| 1048 | DEFINE_TSAN_ATOMIC_RMW(exchange, bits, _n); \ |
| 1049 | DEFINE_TSAN_ATOMIC_RMW(fetch_add, bits, ); \ |
| 1050 | DEFINE_TSAN_ATOMIC_RMW(fetch_sub, bits, ); \ |
| 1051 | DEFINE_TSAN_ATOMIC_RMW(fetch_and, bits, ); \ |
| 1052 | DEFINE_TSAN_ATOMIC_RMW(fetch_or, bits, ); \ |
| 1053 | DEFINE_TSAN_ATOMIC_RMW(fetch_xor, bits, ); \ |
| 1054 | DEFINE_TSAN_ATOMIC_RMW(fetch_nand, bits, ); \ |
| 1055 | DEFINE_TSAN_ATOMIC_CMPXCHG(bits, strong, 0); \ |
| 1056 | DEFINE_TSAN_ATOMIC_CMPXCHG(bits, weak, 1); \ |
| 1057 | DEFINE_TSAN_ATOMIC_CMPXCHG_VAL(bits) |
| 1058 | |
| 1059 | DEFINE_TSAN_ATOMIC_OPS(8); |
| 1060 | DEFINE_TSAN_ATOMIC_OPS(16); |
| 1061 | DEFINE_TSAN_ATOMIC_OPS(32); |
| 1062 | DEFINE_TSAN_ATOMIC_OPS(64); |
| 1063 | |
| 1064 | void __tsan_atomic_thread_fence(int memorder); |
| 1065 | void __tsan_atomic_thread_fence(int memorder) |
| 1066 | { |
| 1067 | __atomic_thread_fence(memorder); |
| 1068 | } |
| 1069 | EXPORT_SYMBOL(__tsan_atomic_thread_fence); |
| 1070 | |
| 1071 | void __tsan_atomic_signal_fence(int memorder); |
| 1072 | void __tsan_atomic_signal_fence(int memorder) { } |
| 1073 | EXPORT_SYMBOL(__tsan_atomic_signal_fence); |