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 reporting. |
| 4 | * |
| 5 | * Copyright (C) 2019, Google LLC. |
| 6 | */ |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 7 | |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 8 | #include <linux/debug_locks.h> |
| 9 | #include <linux/delay.h> |
Marco Elver | 05f9a40 | 2020-01-10 19:48:34 +0100 | [diff] [blame] | 10 | #include <linux/jiffies.h> |
Marco Elver | 6c65eb7 | 2021-08-09 13:25:14 +0200 | [diff] [blame] | 11 | #include <linux/kallsyms.h> |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Marco Elver | f1bc962 | 2020-01-15 17:25:12 +0100 | [diff] [blame] | 13 | #include <linux/lockdep.h> |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 14 | #include <linux/preempt.h> |
| 15 | #include <linux/printk.h> |
| 16 | #include <linux/sched.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/stacktrace.h> |
| 19 | |
| 20 | #include "kcsan.h" |
| 21 | #include "encoding.h" |
| 22 | |
| 23 | /* |
| 24 | * Max. number of stack entries to show in the report. |
| 25 | */ |
| 26 | #define NUM_STACK_ENTRIES 64 |
| 27 | |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 28 | /* Common access info. */ |
| 29 | struct access_info { |
| 30 | const volatile void *ptr; |
| 31 | size_t size; |
| 32 | int access_type; |
| 33 | int task_pid; |
| 34 | int cpu_id; |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 35 | unsigned long ip; |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 36 | }; |
| 37 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 38 | /* |
| 39 | * Other thread info: communicated from other racing thread to thread that set |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 40 | * up the watchpoint, which then prints the complete report atomically. |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 41 | */ |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 42 | struct other_info { |
| 43 | struct access_info ai; |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 44 | unsigned long stack_entries[NUM_STACK_ENTRIES]; |
| 45 | int num_stack_entries; |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Optionally pass @current. Typically we do not need to pass @current |
| 49 | * via @other_info since just @task_pid is sufficient. Passing @current |
| 50 | * has additional overhead. |
| 51 | * |
| 52 | * To safely pass @current, we must either use get_task_struct/ |
| 53 | * put_task_struct, or stall the thread that populated @other_info. |
| 54 | * |
| 55 | * We cannot rely on get_task_struct/put_task_struct in case |
| 56 | * release_report() races with a task being released, and would have to |
| 57 | * free it in release_report(). This may result in deadlock if we want |
| 58 | * to use KCSAN on the allocators. |
| 59 | * |
| 60 | * Since we also want to reliably print held locks for |
| 61 | * CONFIG_KCSAN_VERBOSE, the current implementation stalls the thread |
| 62 | * that populated @other_info until it has been consumed. |
| 63 | */ |
| 64 | struct task_struct *task; |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 65 | }; |
| 66 | |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 67 | /* |
| 68 | * To never block any producers of struct other_info, we need as many elements |
| 69 | * as we have watchpoints (upper bound on concurrent races to report). |
| 70 | */ |
| 71 | static struct other_info other_infos[CONFIG_KCSAN_NUM_WATCHPOINTS + NUM_SLOTS-1]; |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 72 | |
| 73 | /* |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 74 | * Information about reported races; used to rate limit reporting. |
Marco Elver | 05f9a40 | 2020-01-10 19:48:34 +0100 | [diff] [blame] | 75 | */ |
| 76 | struct report_time { |
| 77 | /* |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 78 | * The last time the race was reported. |
Marco Elver | 05f9a40 | 2020-01-10 19:48:34 +0100 | [diff] [blame] | 79 | */ |
| 80 | unsigned long time; |
| 81 | |
| 82 | /* |
| 83 | * The frames of the 2 threads; if only 1 thread is known, one frame |
| 84 | * will be 0. |
| 85 | */ |
| 86 | unsigned long frame1; |
| 87 | unsigned long frame2; |
| 88 | }; |
| 89 | |
| 90 | /* |
| 91 | * Since we also want to be able to debug allocators with KCSAN, to avoid |
| 92 | * deadlock, report_times cannot be dynamically resized with krealloc in |
| 93 | * rate_limit_report. |
| 94 | * |
| 95 | * Therefore, we use a fixed-size array, which at most will occupy a page. This |
| 96 | * still adequately rate limits reports, assuming that a) number of unique data |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 97 | * races is not excessive, and b) occurrence of unique races within the |
Marco Elver | 05f9a40 | 2020-01-10 19:48:34 +0100 | [diff] [blame] | 98 | * same time window is limited. |
| 99 | */ |
| 100 | #define REPORT_TIMES_MAX (PAGE_SIZE / sizeof(struct report_time)) |
| 101 | #define REPORT_TIMES_SIZE \ |
| 102 | (CONFIG_KCSAN_REPORT_ONCE_IN_MS > REPORT_TIMES_MAX ? \ |
| 103 | REPORT_TIMES_MAX : \ |
| 104 | CONFIG_KCSAN_REPORT_ONCE_IN_MS) |
| 105 | static struct report_time report_times[REPORT_TIMES_SIZE]; |
| 106 | |
| 107 | /* |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 108 | * Spinlock serializing report generation, and access to @other_infos. Although |
| 109 | * it could make sense to have a finer-grained locking story for @other_infos, |
| 110 | * report generation needs to be serialized either way, so not much is gained. |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 111 | */ |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 112 | static DEFINE_RAW_SPINLOCK(report_lock); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 113 | |
| 114 | /* |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 115 | * Checks if the race identified by thread frames frame1 and frame2 has |
Marco Elver | 05f9a40 | 2020-01-10 19:48:34 +0100 | [diff] [blame] | 116 | * been reported since (now - KCSAN_REPORT_ONCE_IN_MS). |
| 117 | */ |
| 118 | static bool rate_limit_report(unsigned long frame1, unsigned long frame2) |
| 119 | { |
| 120 | struct report_time *use_entry = &report_times[0]; |
| 121 | unsigned long invalid_before; |
| 122 | int i; |
| 123 | |
| 124 | BUILD_BUG_ON(CONFIG_KCSAN_REPORT_ONCE_IN_MS != 0 && REPORT_TIMES_SIZE == 0); |
| 125 | |
| 126 | if (CONFIG_KCSAN_REPORT_ONCE_IN_MS == 0) |
| 127 | return false; |
| 128 | |
| 129 | invalid_before = jiffies - msecs_to_jiffies(CONFIG_KCSAN_REPORT_ONCE_IN_MS); |
| 130 | |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 131 | /* Check if a matching race report exists. */ |
Marco Elver | 05f9a40 | 2020-01-10 19:48:34 +0100 | [diff] [blame] | 132 | for (i = 0; i < REPORT_TIMES_SIZE; ++i) { |
| 133 | struct report_time *rt = &report_times[i]; |
| 134 | |
| 135 | /* |
| 136 | * Must always select an entry for use to store info as we |
| 137 | * cannot resize report_times; at the end of the scan, use_entry |
| 138 | * will be the oldest entry, which ideally also happened before |
| 139 | * KCSAN_REPORT_ONCE_IN_MS ago. |
| 140 | */ |
| 141 | if (time_before(rt->time, use_entry->time)) |
| 142 | use_entry = rt; |
| 143 | |
| 144 | /* |
| 145 | * Initially, no need to check any further as this entry as well |
| 146 | * as following entries have never been used. |
| 147 | */ |
| 148 | if (rt->time == 0) |
| 149 | break; |
| 150 | |
| 151 | /* Check if entry expired. */ |
| 152 | if (time_before(rt->time, invalid_before)) |
| 153 | continue; /* before KCSAN_REPORT_ONCE_IN_MS ago */ |
| 154 | |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 155 | /* Reported recently, check if race matches. */ |
Marco Elver | 05f9a40 | 2020-01-10 19:48:34 +0100 | [diff] [blame] | 156 | if ((rt->frame1 == frame1 && rt->frame2 == frame2) || |
| 157 | (rt->frame1 == frame2 && rt->frame2 == frame1)) |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | use_entry->time = jiffies; |
| 162 | use_entry->frame1 = frame1; |
| 163 | use_entry->frame2 = frame2; |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | /* |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 168 | * Special rules to skip reporting. |
| 169 | */ |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 170 | static bool |
Marco Elver | b738f61 | 2020-02-11 17:04:21 +0100 | [diff] [blame] | 171 | skip_report(enum kcsan_value_change value_change, unsigned long top_frame) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 172 | { |
Marco Elver | 81af89e | 2020-02-11 17:04:22 +0100 | [diff] [blame] | 173 | /* Should never get here if value_change==FALSE. */ |
| 174 | WARN_ON_ONCE(value_change == KCSAN_VALUE_CHANGE_FALSE); |
| 175 | |
Marco Elver | ad4f8ee | 2020-01-29 16:01:02 +0100 | [diff] [blame] | 176 | /* |
Marco Elver | b738f61 | 2020-02-11 17:04:21 +0100 | [diff] [blame] | 177 | * The first call to skip_report always has value_change==TRUE, since we |
Marco Elver | ad4f8ee | 2020-01-29 16:01:02 +0100 | [diff] [blame] | 178 | * cannot know the value written of an instrumented access. For the 2nd |
| 179 | * call there are 6 cases with CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY: |
| 180 | * |
Marco Elver | b738f61 | 2020-02-11 17:04:21 +0100 | [diff] [blame] | 181 | * 1. read watchpoint, conflicting write (value_change==TRUE): report; |
| 182 | * 2. read watchpoint, conflicting write (value_change==MAYBE): skip; |
| 183 | * 3. write watchpoint, conflicting write (value_change==TRUE): report; |
| 184 | * 4. write watchpoint, conflicting write (value_change==MAYBE): skip; |
| 185 | * 5. write watchpoint, conflicting read (value_change==MAYBE): skip; |
| 186 | * 6. write watchpoint, conflicting read (value_change==TRUE): report; |
Marco Elver | ad4f8ee | 2020-01-29 16:01:02 +0100 | [diff] [blame] | 187 | * |
| 188 | * Cases 1-4 are intuitive and expected; case 5 ensures we do not report |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 189 | * data races where the write may have rewritten the same value; case 6 |
| 190 | * is possible either if the size is larger than what we check value |
| 191 | * changes for or the access type is KCSAN_ACCESS_ASSERT. |
Marco Elver | ad4f8ee | 2020-01-29 16:01:02 +0100 | [diff] [blame] | 192 | */ |
Marco Elver | b738f61 | 2020-02-11 17:04:21 +0100 | [diff] [blame] | 193 | if (IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY) && |
| 194 | value_change == KCSAN_VALUE_CHANGE_MAYBE) { |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 195 | /* |
| 196 | * The access is a write, but the data value did not change. |
| 197 | * |
| 198 | * We opt-out of this filter for certain functions at request of |
| 199 | * maintainers. |
| 200 | */ |
| 201 | char buf[64]; |
Marco Elver | f770ed1 | 2020-04-10 18:44:17 +0200 | [diff] [blame] | 202 | int len = scnprintf(buf, sizeof(buf), "%ps", (void *)top_frame); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 203 | |
Marco Elver | f770ed1 | 2020-04-10 18:44:17 +0200 | [diff] [blame] | 204 | if (!strnstr(buf, "rcu_", len) && |
| 205 | !strnstr(buf, "_rcu", len) && |
| 206 | !strnstr(buf, "_srcu", len)) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 207 | return true; |
| 208 | } |
| 209 | |
| 210 | return kcsan_skip_report_debugfs(top_frame); |
| 211 | } |
| 212 | |
Marco Elver | 47144ec | 2020-01-10 19:48:33 +0100 | [diff] [blame] | 213 | static const char *get_access_type(int type) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 214 | { |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 215 | if (type & KCSAN_ACCESS_ASSERT) { |
| 216 | if (type & KCSAN_ACCESS_SCOPED) { |
| 217 | if (type & KCSAN_ACCESS_WRITE) |
Marco Elver | 3cc21a5 | 2021-11-30 12:44:15 +0100 | [diff] [blame] | 218 | return "assert no accesses (reordered)"; |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 219 | else |
Marco Elver | 3cc21a5 | 2021-11-30 12:44:15 +0100 | [diff] [blame] | 220 | return "assert no writes (reordered)"; |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 221 | } else { |
| 222 | if (type & KCSAN_ACCESS_WRITE) |
| 223 | return "assert no accesses"; |
| 224 | else |
| 225 | return "assert no writes"; |
| 226 | } |
| 227 | } |
| 228 | |
Marco Elver | 47144ec | 2020-01-10 19:48:33 +0100 | [diff] [blame] | 229 | switch (type) { |
| 230 | case 0: |
| 231 | return "read"; |
| 232 | case KCSAN_ACCESS_ATOMIC: |
| 233 | return "read (marked)"; |
| 234 | case KCSAN_ACCESS_WRITE: |
| 235 | return "write"; |
| 236 | case KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC: |
| 237 | return "write (marked)"; |
Marco Elver | 14e2ac8 | 2020-07-24 09:00:01 +0200 | [diff] [blame] | 238 | case KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE: |
| 239 | return "read-write"; |
| 240 | case KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC: |
| 241 | return "read-write (marked)"; |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 242 | case KCSAN_ACCESS_SCOPED: |
Marco Elver | 3cc21a5 | 2021-11-30 12:44:15 +0100 | [diff] [blame] | 243 | return "read (reordered)"; |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 244 | case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_ATOMIC: |
Marco Elver | 3cc21a5 | 2021-11-30 12:44:15 +0100 | [diff] [blame] | 245 | return "read (marked, reordered)"; |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 246 | case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_WRITE: |
Marco Elver | 3cc21a5 | 2021-11-30 12:44:15 +0100 | [diff] [blame] | 247 | return "write (reordered)"; |
Marco Elver | 757a4ce | 2020-03-25 17:41:56 +0100 | [diff] [blame] | 248 | case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC: |
Marco Elver | 3cc21a5 | 2021-11-30 12:44:15 +0100 | [diff] [blame] | 249 | return "write (marked, reordered)"; |
Marco Elver | d627c53 | 2021-08-09 13:25:15 +0200 | [diff] [blame] | 250 | case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE: |
Marco Elver | 3cc21a5 | 2021-11-30 12:44:15 +0100 | [diff] [blame] | 251 | return "read-write (reordered)"; |
Marco Elver | d627c53 | 2021-08-09 13:25:15 +0200 | [diff] [blame] | 252 | case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC: |
Marco Elver | 3cc21a5 | 2021-11-30 12:44:15 +0100 | [diff] [blame] | 253 | return "read-write (marked, reordered)"; |
Marco Elver | 47144ec | 2020-01-10 19:48:33 +0100 | [diff] [blame] | 254 | default: |
| 255 | BUG(); |
| 256 | } |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 259 | static const char *get_bug_type(int type) |
| 260 | { |
| 261 | return (type & KCSAN_ACCESS_ASSERT) != 0 ? "assert: race" : "data-race"; |
| 262 | } |
| 263 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 264 | /* Return thread description: in task or interrupt. */ |
| 265 | static const char *get_thread_desc(int task_id) |
| 266 | { |
| 267 | if (task_id != -1) { |
| 268 | static char buf[32]; /* safe: protected by report_lock */ |
| 269 | |
| 270 | snprintf(buf, sizeof(buf), "task %i", task_id); |
| 271 | return buf; |
| 272 | } |
| 273 | return "interrupt"; |
| 274 | } |
| 275 | |
| 276 | /* Helper to skip KCSAN-related functions in stack-trace. */ |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 277 | static int get_stack_skipnr(const unsigned long stack_entries[], int num_entries) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 278 | { |
| 279 | char buf[64]; |
Marco Elver | cdb9b07 | 2020-04-10 18:44:18 +0200 | [diff] [blame] | 280 | char *cur; |
| 281 | int len, skip; |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 282 | |
Marco Elver | cdb9b07 | 2020-04-10 18:44:18 +0200 | [diff] [blame] | 283 | for (skip = 0; skip < num_entries; ++skip) { |
Marco Elver | f770ed1 | 2020-04-10 18:44:17 +0200 | [diff] [blame] | 284 | len = scnprintf(buf, sizeof(buf), "%ps", (void *)stack_entries[skip]); |
Marco Elver | cdb9b07 | 2020-04-10 18:44:18 +0200 | [diff] [blame] | 285 | |
| 286 | /* Never show tsan_* or {read,write}_once_size. */ |
| 287 | if (strnstr(buf, "tsan_", len) || |
| 288 | strnstr(buf, "_once_size", len)) |
| 289 | continue; |
| 290 | |
| 291 | cur = strnstr(buf, "kcsan_", len); |
| 292 | if (cur) { |
Marco Elver | a4e74fa | 2020-07-31 10:17:20 +0200 | [diff] [blame] | 293 | cur += strlen("kcsan_"); |
| 294 | if (!str_has_prefix(cur, "test")) |
Marco Elver | cdb9b07 | 2020-04-10 18:44:18 +0200 | [diff] [blame] | 295 | continue; /* KCSAN runtime function. */ |
| 296 | /* KCSAN related test. */ |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * No match for runtime functions -- @skip entries to skip to |
| 301 | * get to first frame of interest. |
| 302 | */ |
| 303 | break; |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 304 | } |
Marco Elver | cdb9b07 | 2020-04-10 18:44:18 +0200 | [diff] [blame] | 305 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 306 | return skip; |
| 307 | } |
| 308 | |
Marco Elver | 6c65eb7 | 2021-08-09 13:25:14 +0200 | [diff] [blame] | 309 | /* |
| 310 | * Skips to the first entry that matches the function of @ip, and then replaces |
Marco Elver | be3f696 | 2021-11-30 12:44:16 +0100 | [diff] [blame] | 311 | * that entry with @ip, returning the entries to skip with @replaced containing |
| 312 | * the replaced entry. |
Marco Elver | 6c65eb7 | 2021-08-09 13:25:14 +0200 | [diff] [blame] | 313 | */ |
| 314 | static int |
Marco Elver | be3f696 | 2021-11-30 12:44:16 +0100 | [diff] [blame] | 315 | replace_stack_entry(unsigned long stack_entries[], int num_entries, unsigned long ip, |
| 316 | unsigned long *replaced) |
Marco Elver | 6c65eb7 | 2021-08-09 13:25:14 +0200 | [diff] [blame] | 317 | { |
| 318 | unsigned long symbolsize, offset; |
| 319 | unsigned long target_func; |
| 320 | int skip; |
| 321 | |
| 322 | if (kallsyms_lookup_size_offset(ip, &symbolsize, &offset)) |
| 323 | target_func = ip - offset; |
| 324 | else |
| 325 | goto fallback; |
| 326 | |
| 327 | for (skip = 0; skip < num_entries; ++skip) { |
| 328 | unsigned long func = stack_entries[skip]; |
| 329 | |
| 330 | if (!kallsyms_lookup_size_offset(func, &symbolsize, &offset)) |
| 331 | goto fallback; |
| 332 | func -= offset; |
| 333 | |
| 334 | if (func == target_func) { |
Marco Elver | be3f696 | 2021-11-30 12:44:16 +0100 | [diff] [blame] | 335 | *replaced = stack_entries[skip]; |
Marco Elver | 6c65eb7 | 2021-08-09 13:25:14 +0200 | [diff] [blame] | 336 | stack_entries[skip] = ip; |
| 337 | return skip; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | fallback: |
| 342 | /* Should not happen; the resulting stack trace is likely misleading. */ |
| 343 | WARN_ONCE(1, "Cannot find frame for %pS in stack trace", (void *)ip); |
| 344 | return get_stack_skipnr(stack_entries, num_entries); |
| 345 | } |
| 346 | |
| 347 | static int |
Marco Elver | be3f696 | 2021-11-30 12:44:16 +0100 | [diff] [blame] | 348 | sanitize_stack_entries(unsigned long stack_entries[], int num_entries, unsigned long ip, |
| 349 | unsigned long *replaced) |
Marco Elver | 6c65eb7 | 2021-08-09 13:25:14 +0200 | [diff] [blame] | 350 | { |
Marco Elver | be3f696 | 2021-11-30 12:44:16 +0100 | [diff] [blame] | 351 | return ip ? replace_stack_entry(stack_entries, num_entries, ip, replaced) : |
Marco Elver | 6c65eb7 | 2021-08-09 13:25:14 +0200 | [diff] [blame] | 352 | get_stack_skipnr(stack_entries, num_entries); |
| 353 | } |
| 354 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 355 | /* Compares symbolized strings of addr1 and addr2. */ |
| 356 | static int sym_strcmp(void *addr1, void *addr2) |
| 357 | { |
| 358 | char buf1[64]; |
| 359 | char buf2[64]; |
| 360 | |
| 361 | snprintf(buf1, sizeof(buf1), "%pS", addr1); |
| 362 | snprintf(buf2, sizeof(buf2), "%pS", addr2); |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 363 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 364 | return strncmp(buf1, buf2, sizeof(buf1)); |
| 365 | } |
| 366 | |
Marco Elver | be3f696 | 2021-11-30 12:44:16 +0100 | [diff] [blame] | 367 | static void |
| 368 | print_stack_trace(unsigned long stack_entries[], int num_entries, unsigned long reordered_to) |
| 369 | { |
| 370 | stack_trace_print(stack_entries, num_entries, 0); |
| 371 | if (reordered_to) |
| 372 | pr_err(" |\n +-> reordered to: %pS\n", (void *)reordered_to); |
| 373 | } |
| 374 | |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 375 | static void print_verbose_info(struct task_struct *task) |
| 376 | { |
| 377 | if (!task) |
| 378 | return; |
| 379 | |
Marco Elver | 92c209a | 2020-07-29 13:09:16 +0200 | [diff] [blame] | 380 | /* Restore IRQ state trace for printing. */ |
| 381 | kcsan_restore_irqtrace(task); |
| 382 | |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 383 | pr_err("\n"); |
| 384 | debug_show_held_locks(task); |
| 385 | print_irqtrace_events(task); |
| 386 | } |
| 387 | |
Mark Rutland | 97aa613 | 2021-04-14 13:28:20 +0200 | [diff] [blame] | 388 | static void print_report(enum kcsan_value_change value_change, |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 389 | const struct access_info *ai, |
Marco Elver | 6c65eb7 | 2021-08-09 13:25:14 +0200 | [diff] [blame] | 390 | struct other_info *other_info, |
Mark Rutland | 7bbe6dc | 2021-04-14 13:28:24 +0200 | [diff] [blame] | 391 | u64 old, u64 new, u64 mask) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 392 | { |
Marco Elver | be3f696 | 2021-11-30 12:44:16 +0100 | [diff] [blame] | 393 | unsigned long reordered_to = 0; |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 394 | unsigned long stack_entries[NUM_STACK_ENTRIES] = { 0 }; |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 395 | int num_stack_entries = stack_trace_save(stack_entries, NUM_STACK_ENTRIES, 1); |
Marco Elver | be3f696 | 2021-11-30 12:44:16 +0100 | [diff] [blame] | 396 | int skipnr = sanitize_stack_entries(stack_entries, num_stack_entries, ai->ip, &reordered_to); |
Marco Elver | 05f9a40 | 2020-01-10 19:48:34 +0100 | [diff] [blame] | 397 | unsigned long this_frame = stack_entries[skipnr]; |
Marco Elver | be3f696 | 2021-11-30 12:44:16 +0100 | [diff] [blame] | 398 | unsigned long other_reordered_to = 0; |
Marco Elver | 05f9a40 | 2020-01-10 19:48:34 +0100 | [diff] [blame] | 399 | unsigned long other_frame = 0; |
| 400 | int other_skipnr = 0; /* silence uninit warnings */ |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 401 | |
| 402 | /* |
| 403 | * Must check report filter rules before starting to print. |
| 404 | */ |
Marco Elver | b738f61 | 2020-02-11 17:04:21 +0100 | [diff] [blame] | 405 | if (skip_report(KCSAN_VALUE_CHANGE_TRUE, stack_entries[skipnr])) |
Mark Rutland | 97aa613 | 2021-04-14 13:28:20 +0200 | [diff] [blame] | 406 | return; |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 407 | |
Mark Rutland | 609f809 | 2021-04-14 13:28:23 +0200 | [diff] [blame] | 408 | if (other_info) { |
Marco Elver | 6c65eb7 | 2021-08-09 13:25:14 +0200 | [diff] [blame] | 409 | other_skipnr = sanitize_stack_entries(other_info->stack_entries, |
| 410 | other_info->num_stack_entries, |
Marco Elver | be3f696 | 2021-11-30 12:44:16 +0100 | [diff] [blame] | 411 | other_info->ai.ip, &other_reordered_to); |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 412 | other_frame = other_info->stack_entries[other_skipnr]; |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 413 | |
Ingo Molnar | 5cbaefe | 2019-11-20 10:41:43 +0100 | [diff] [blame] | 414 | /* @value_change is only known for the other thread */ |
Marco Elver | ad4f8ee | 2020-01-29 16:01:02 +0100 | [diff] [blame] | 415 | if (skip_report(value_change, other_frame)) |
Mark Rutland | 97aa613 | 2021-04-14 13:28:20 +0200 | [diff] [blame] | 416 | return; |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 417 | } |
| 418 | |
Marco Elver | 05f9a40 | 2020-01-10 19:48:34 +0100 | [diff] [blame] | 419 | if (rate_limit_report(this_frame, other_frame)) |
Mark Rutland | 97aa613 | 2021-04-14 13:28:20 +0200 | [diff] [blame] | 420 | return; |
Marco Elver | 05f9a40 | 2020-01-10 19:48:34 +0100 | [diff] [blame] | 421 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 422 | /* Print report header. */ |
| 423 | pr_err("==================================================================\n"); |
Mark Rutland | 609f809 | 2021-04-14 13:28:23 +0200 | [diff] [blame] | 424 | if (other_info) { |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 425 | int cmp; |
| 426 | |
| 427 | /* |
| 428 | * Order functions lexographically for consistent bug titles. |
| 429 | * Do not print offset of functions to keep title short. |
| 430 | */ |
Marco Elver | 05f9a40 | 2020-01-10 19:48:34 +0100 | [diff] [blame] | 431 | cmp = sym_strcmp((void *)other_frame, (void *)this_frame); |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 432 | pr_err("BUG: KCSAN: %s in %ps / %ps\n", |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 433 | get_bug_type(ai->access_type | other_info->ai.access_type), |
Marco Elver | 05f9a40 | 2020-01-10 19:48:34 +0100 | [diff] [blame] | 434 | (void *)(cmp < 0 ? other_frame : this_frame), |
| 435 | (void *)(cmp < 0 ? this_frame : other_frame)); |
Mark Rutland | 609f809 | 2021-04-14 13:28:23 +0200 | [diff] [blame] | 436 | } else { |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 437 | pr_err("BUG: KCSAN: %s in %pS\n", get_bug_type(ai->access_type), |
Marco Elver | d591ec3 | 2020-02-06 16:46:24 +0100 | [diff] [blame] | 438 | (void *)this_frame); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | pr_err("\n"); |
| 442 | |
| 443 | /* Print information about the racing accesses. */ |
Mark Rutland | 609f809 | 2021-04-14 13:28:23 +0200 | [diff] [blame] | 444 | if (other_info) { |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 445 | pr_err("%s to 0x%px of %zu bytes by %s on cpu %i:\n", |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 446 | get_access_type(other_info->ai.access_type), other_info->ai.ptr, |
| 447 | other_info->ai.size, get_thread_desc(other_info->ai.task_pid), |
| 448 | other_info->ai.cpu_id); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 449 | |
| 450 | /* Print the other thread's stack trace. */ |
Marco Elver | be3f696 | 2021-11-30 12:44:16 +0100 | [diff] [blame] | 451 | print_stack_trace(other_info->stack_entries + other_skipnr, |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 452 | other_info->num_stack_entries - other_skipnr, |
Marco Elver | be3f696 | 2021-11-30 12:44:16 +0100 | [diff] [blame] | 453 | other_reordered_to); |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 454 | if (IS_ENABLED(CONFIG_KCSAN_VERBOSE)) |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 455 | print_verbose_info(other_info->task); |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 456 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 457 | pr_err("\n"); |
| 458 | pr_err("%s to 0x%px of %zu bytes by %s on cpu %i:\n", |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 459 | get_access_type(ai->access_type), ai->ptr, ai->size, |
| 460 | get_thread_desc(ai->task_pid), ai->cpu_id); |
Mark Rutland | 609f809 | 2021-04-14 13:28:23 +0200 | [diff] [blame] | 461 | } else { |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 462 | pr_err("race at unknown origin, with %s to 0x%px of %zu bytes by %s on cpu %i:\n", |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 463 | get_access_type(ai->access_type), ai->ptr, ai->size, |
| 464 | get_thread_desc(ai->task_pid), ai->cpu_id); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 465 | } |
| 466 | /* Print stack trace of this thread. */ |
Marco Elver | be3f696 | 2021-11-30 12:44:16 +0100 | [diff] [blame] | 467 | print_stack_trace(stack_entries + skipnr, num_stack_entries - skipnr, reordered_to); |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 468 | if (IS_ENABLED(CONFIG_KCSAN_VERBOSE)) |
| 469 | print_verbose_info(current); |
| 470 | |
Mark Rutland | 7bbe6dc | 2021-04-14 13:28:24 +0200 | [diff] [blame] | 471 | /* Print observed value change. */ |
| 472 | if (ai->size <= 8) { |
| 473 | int hex_len = ai->size * 2; |
| 474 | u64 diff = old ^ new; |
| 475 | |
| 476 | if (mask) |
| 477 | diff &= mask; |
| 478 | if (diff) { |
| 479 | pr_err("\n"); |
| 480 | pr_err("value changed: 0x%0*llx -> 0x%0*llx\n", |
| 481 | hex_len, old, hex_len, new); |
| 482 | if (mask) { |
| 483 | pr_err(" bits changed: 0x%0*llx with mask 0x%0*llx\n", |
| 484 | hex_len, diff, hex_len, mask); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 489 | /* Print report footer. */ |
| 490 | pr_err("\n"); |
| 491 | pr_err("Reported by Kernel Concurrency Sanitizer on:\n"); |
| 492 | dump_stack_print_info(KERN_DEFAULT); |
| 493 | pr_err("==================================================================\n"); |
| 494 | |
Mark Rutland | 97aa613 | 2021-04-14 13:28:20 +0200 | [diff] [blame] | 495 | if (panic_on_warn) |
| 496 | panic("panic_on_warn set ...\n"); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 499 | static void release_report(unsigned long *flags, struct other_info *other_info) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 500 | { |
Mark Rutland | 19dfdc0 | 2021-04-14 13:28:22 +0200 | [diff] [blame] | 501 | /* |
| 502 | * Use size to denote valid/invalid, since KCSAN entirely ignores |
| 503 | * 0-sized accesses. |
| 504 | */ |
| 505 | other_info->ai.size = 0; |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 506 | raw_spin_unlock_irqrestore(&report_lock, *flags); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /* |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 510 | * Sets @other_info->task and awaits consumption of @other_info. |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 511 | * |
| 512 | * Precondition: report_lock is held. |
| 513 | * Postcondition: report_lock is held. |
| 514 | */ |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 515 | static void set_other_info_task_blocking(unsigned long *flags, |
| 516 | const struct access_info *ai, |
| 517 | struct other_info *other_info) |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 518 | { |
| 519 | /* |
| 520 | * We may be instrumenting a code-path where current->state is already |
| 521 | * something other than TASK_RUNNING. |
| 522 | */ |
Peter Zijlstra | b03fbd4 | 2021-06-11 10:28:12 +0200 | [diff] [blame] | 523 | const bool is_running = task_is_running(current); |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 524 | /* |
| 525 | * To avoid deadlock in case we are in an interrupt here and this is a |
| 526 | * race with a task on the same CPU (KCSAN_INTERRUPT_WATCHER), provide a |
| 527 | * timeout to ensure this works in all contexts. |
| 528 | * |
| 529 | * Await approximately the worst case delay of the reporting thread (if |
| 530 | * we are not interrupted). |
| 531 | */ |
| 532 | int timeout = max(kcsan_udelay_task, kcsan_udelay_interrupt); |
| 533 | |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 534 | other_info->task = current; |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 535 | do { |
| 536 | if (is_running) { |
| 537 | /* |
| 538 | * Let lockdep know the real task is sleeping, to print |
| 539 | * the held locks (recall we turned lockdep off, so |
| 540 | * locking/unlocking @report_lock won't be recorded). |
| 541 | */ |
| 542 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 543 | } |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 544 | raw_spin_unlock_irqrestore(&report_lock, *flags); |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 545 | /* |
| 546 | * We cannot call schedule() since we also cannot reliably |
| 547 | * determine if sleeping here is permitted -- see in_atomic(). |
| 548 | */ |
| 549 | |
| 550 | udelay(1); |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 551 | raw_spin_lock_irqsave(&report_lock, *flags); |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 552 | if (timeout-- < 0) { |
| 553 | /* |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 554 | * Abort. Reset @other_info->task to NULL, since it |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 555 | * appears the other thread is still going to consume |
| 556 | * it. It will result in no verbose info printed for |
| 557 | * this task. |
| 558 | */ |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 559 | other_info->task = NULL; |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 560 | break; |
| 561 | } |
| 562 | /* |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 563 | * If invalid, or @ptr nor @current matches, then @other_info |
| 564 | * has been consumed and we may continue. If not, retry. |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 565 | */ |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 566 | } while (other_info->ai.size && other_info->ai.ptr == ai->ptr && |
| 567 | other_info->task == current); |
Marco Elver | 2402d0e | 2020-02-22 00:10:27 +0100 | [diff] [blame] | 568 | if (is_running) |
| 569 | set_current_state(TASK_RUNNING); |
| 570 | } |
| 571 | |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 572 | /* Populate @other_info; requires that the provided @other_info not in use. */ |
| 573 | static void prepare_report_producer(unsigned long *flags, |
| 574 | const struct access_info *ai, |
| 575 | struct other_info *other_info) |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 576 | { |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 577 | raw_spin_lock_irqsave(&report_lock, *flags); |
| 578 | |
| 579 | /* |
| 580 | * The same @other_infos entry cannot be used concurrently, because |
| 581 | * there is a one-to-one mapping to watchpoint slots (@watchpoints in |
| 582 | * core.c), and a watchpoint is only released for reuse after reporting |
| 583 | * is done by the consumer of @other_info. Therefore, it is impossible |
| 584 | * for another concurrent prepare_report_producer() to set the same |
| 585 | * @other_info, and are guaranteed exclusivity for the @other_infos |
| 586 | * entry pointed to by @other_info. |
| 587 | * |
| 588 | * To check this property holds, size should never be non-zero here, |
| 589 | * because every consumer of struct other_info resets size to 0 in |
| 590 | * release_report(). |
| 591 | */ |
| 592 | WARN_ON(other_info->ai.size); |
| 593 | |
| 594 | other_info->ai = *ai; |
| 595 | other_info->num_stack_entries = stack_trace_save(other_info->stack_entries, NUM_STACK_ENTRIES, 2); |
| 596 | |
| 597 | if (IS_ENABLED(CONFIG_KCSAN_VERBOSE)) |
| 598 | set_other_info_task_blocking(flags, ai, other_info); |
| 599 | |
| 600 | raw_spin_unlock_irqrestore(&report_lock, *flags); |
| 601 | } |
| 602 | |
| 603 | /* Awaits producer to fill @other_info and then returns. */ |
| 604 | static bool prepare_report_consumer(unsigned long *flags, |
| 605 | const struct access_info *ai, |
| 606 | struct other_info *other_info) |
| 607 | { |
| 608 | |
| 609 | raw_spin_lock_irqsave(&report_lock, *flags); |
| 610 | while (!other_info->ai.size) { /* Await valid @other_info. */ |
| 611 | raw_spin_unlock_irqrestore(&report_lock, *flags); |
| 612 | cpu_relax(); |
| 613 | raw_spin_lock_irqsave(&report_lock, *flags); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 614 | } |
| 615 | |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 616 | /* Should always have a matching access based on watchpoint encoding. */ |
| 617 | if (WARN_ON(!matching_access((unsigned long)other_info->ai.ptr & WATCHPOINT_ADDR_MASK, other_info->ai.size, |
| 618 | (unsigned long)ai->ptr & WATCHPOINT_ADDR_MASK, ai->size))) |
| 619 | goto discard; |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 620 | |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 621 | if (!matching_access((unsigned long)other_info->ai.ptr, other_info->ai.size, |
| 622 | (unsigned long)ai->ptr, ai->size)) { |
| 623 | /* |
| 624 | * If the actual accesses to not match, this was a false |
| 625 | * positive due to watchpoint encoding. |
| 626 | */ |
Marco Elver | 2e986b8 | 2020-08-10 10:06:25 +0200 | [diff] [blame] | 627 | atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ENCODING_FALSE_POSITIVES]); |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 628 | goto discard; |
| 629 | } |
| 630 | |
| 631 | return true; |
| 632 | |
| 633 | discard: |
| 634 | release_report(flags, other_info); |
| 635 | return false; |
| 636 | } |
| 637 | |
Mark Rutland | 39b2e76 | 2021-04-14 13:28:21 +0200 | [diff] [blame] | 638 | static struct access_info prepare_access_info(const volatile void *ptr, size_t size, |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 639 | int access_type, unsigned long ip) |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 640 | { |
Mark Rutland | 39b2e76 | 2021-04-14 13:28:21 +0200 | [diff] [blame] | 641 | return (struct access_info) { |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 642 | .ptr = ptr, |
| 643 | .size = size, |
| 644 | .access_type = access_type, |
| 645 | .task_pid = in_task() ? task_pid_nr(current) : -1, |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 646 | .cpu_id = raw_smp_processor_id(), |
Marco Elver | 6c65eb7 | 2021-08-09 13:25:14 +0200 | [diff] [blame] | 647 | /* Only replace stack entry with @ip if scoped access. */ |
| 648 | .ip = (access_type & KCSAN_ACCESS_SCOPED) ? ip : 0, |
Marco Elver | 135c087 | 2020-03-18 18:38:44 +0100 | [diff] [blame] | 649 | }; |
Mark Rutland | 39b2e76 | 2021-04-14 13:28:21 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Mark Rutland | 793c257 | 2021-04-14 13:28:18 +0200 | [diff] [blame] | 652 | void kcsan_report_set_info(const volatile void *ptr, size_t size, int access_type, |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 653 | unsigned long ip, int watchpoint_idx) |
Mark Rutland | 793c257 | 2021-04-14 13:28:18 +0200 | [diff] [blame] | 654 | { |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 655 | const struct access_info ai = prepare_access_info(ptr, size, access_type, ip); |
Mark Rutland | 19dfdc0 | 2021-04-14 13:28:22 +0200 | [diff] [blame] | 656 | unsigned long flags; |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 657 | |
| 658 | kcsan_disable_current(); |
Mark Rutland | 19dfdc0 | 2021-04-14 13:28:22 +0200 | [diff] [blame] | 659 | lockdep_off(); /* See kcsan_report_known_origin(). */ |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 660 | |
Mark Rutland | 19dfdc0 | 2021-04-14 13:28:22 +0200 | [diff] [blame] | 661 | prepare_report_producer(&flags, &ai, &other_infos[watchpoint_idx]); |
| 662 | |
| 663 | lockdep_on(); |
| 664 | kcsan_enable_current(); |
Mark Rutland | 793c257 | 2021-04-14 13:28:18 +0200 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | void kcsan_report_known_origin(const volatile void *ptr, size_t size, int access_type, |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 668 | unsigned long ip, enum kcsan_value_change value_change, |
| 669 | int watchpoint_idx, u64 old, u64 new, u64 mask) |
Mark Rutland | 793c257 | 2021-04-14 13:28:18 +0200 | [diff] [blame] | 670 | { |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 671 | const struct access_info ai = prepare_access_info(ptr, size, access_type, ip); |
Mark Rutland | 19dfdc0 | 2021-04-14 13:28:22 +0200 | [diff] [blame] | 672 | struct other_info *other_info = &other_infos[watchpoint_idx]; |
| 673 | unsigned long flags = 0; |
Mark Rutland | 39b2e76 | 2021-04-14 13:28:21 +0200 | [diff] [blame] | 674 | |
Mark Rutland | 19dfdc0 | 2021-04-14 13:28:22 +0200 | [diff] [blame] | 675 | kcsan_disable_current(); |
Marco Elver | f1bc962 | 2020-01-15 17:25:12 +0100 | [diff] [blame] | 676 | /* |
Marco Elver | 248591f | 2020-06-24 13:32:46 +0200 | [diff] [blame] | 677 | * Because we may generate reports when we're in scheduler code, the use |
| 678 | * of printk() could deadlock. Until such time that all printing code |
| 679 | * called in print_report() is scheduler-safe, accept the risk, and just |
| 680 | * get our message out. As such, also disable lockdep to hide the |
| 681 | * warning, and avoid disabling lockdep for the rest of the kernel. |
Marco Elver | f1bc962 | 2020-01-15 17:25:12 +0100 | [diff] [blame] | 682 | */ |
| 683 | lockdep_off(); |
| 684 | |
Mark Rutland | 19dfdc0 | 2021-04-14 13:28:22 +0200 | [diff] [blame] | 685 | if (!prepare_report_consumer(&flags, &ai, other_info)) |
| 686 | goto out; |
| 687 | /* |
| 688 | * Never report if value_change is FALSE, only when it is |
| 689 | * either TRUE or MAYBE. In case of MAYBE, further filtering may |
| 690 | * be done once we know the full stack trace in print_report(). |
| 691 | */ |
| 692 | if (value_change != KCSAN_VALUE_CHANGE_FALSE) |
Mark Rutland | 7bbe6dc | 2021-04-14 13:28:24 +0200 | [diff] [blame] | 693 | print_report(value_change, &ai, other_info, old, new, mask); |
Marco Elver | 81af89e | 2020-02-11 17:04:22 +0100 | [diff] [blame] | 694 | |
Mark Rutland | 19dfdc0 | 2021-04-14 13:28:22 +0200 | [diff] [blame] | 695 | release_report(&flags, other_info); |
| 696 | out: |
| 697 | lockdep_on(); |
| 698 | kcsan_enable_current(); |
Mark Rutland | 793c257 | 2021-04-14 13:28:18 +0200 | [diff] [blame] | 699 | } |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 700 | |
Mark Rutland | 7bbe6dc | 2021-04-14 13:28:24 +0200 | [diff] [blame] | 701 | void kcsan_report_unknown_origin(const volatile void *ptr, size_t size, int access_type, |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 702 | unsigned long ip, u64 old, u64 new, u64 mask) |
Mark Rutland | 793c257 | 2021-04-14 13:28:18 +0200 | [diff] [blame] | 703 | { |
Marco Elver | 55a55fe | 2021-08-09 13:25:12 +0200 | [diff] [blame] | 704 | const struct access_info ai = prepare_access_info(ptr, size, access_type, ip); |
Mark Rutland | 19dfdc0 | 2021-04-14 13:28:22 +0200 | [diff] [blame] | 705 | unsigned long flags; |
Mark Rutland | 39b2e76 | 2021-04-14 13:28:21 +0200 | [diff] [blame] | 706 | |
Mark Rutland | 19dfdc0 | 2021-04-14 13:28:22 +0200 | [diff] [blame] | 707 | kcsan_disable_current(); |
| 708 | lockdep_off(); /* See kcsan_report_known_origin(). */ |
| 709 | |
| 710 | raw_spin_lock_irqsave(&report_lock, flags); |
Mark Rutland | 7bbe6dc | 2021-04-14 13:28:24 +0200 | [diff] [blame] | 711 | print_report(KCSAN_VALUE_CHANGE_TRUE, &ai, NULL, old, new, mask); |
Mark Rutland | 19dfdc0 | 2021-04-14 13:28:22 +0200 | [diff] [blame] | 712 | raw_spin_unlock_irqrestore(&report_lock, flags); |
Marco Elver | f1bc962 | 2020-01-15 17:25:12 +0100 | [diff] [blame] | 713 | |
| 714 | lockdep_on(); |
Marco Elver | 6119418 | 2020-03-18 18:38:45 +0100 | [diff] [blame] | 715 | kcsan_enable_current(); |
Marco Elver | dfd402a | 2019-11-14 19:02:54 +0100 | [diff] [blame] | 716 | } |