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