// SPDX-License-Identifier: GPL-2.0
/*
* KCSAN test with various race scenarious to test runtime behaviour. Since the
* interface with which KCSAN's reports are obtained is via the console, this is
* the output we should verify. For each test case checks the presence (or
* absence) of generated reports. Relies on 'console' tracepoint to capture
* reports as they appear in the kernel log.
*
* Makes use of KUnit for test organization, and the Torture framework for test
* thread control.
*
* Copyright (C) 2020, Google LLC.
* Author: Marco Elver <elver@google.com>
*/
#include <kunit/test.h>
#include <linux/jiffies.h>
#include <linux/kcsan-checks.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/seqlock.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/torture.h>
#include <linux/tracepoint.h>
#include <linux/types.h>
#include <trace/events/printk.h>
#ifdef CONFIG_CC_HAS_TSAN_COMPOUND_READ_BEFORE_WRITE
#define __KCSAN_ACCESS_RW(alt) (KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE)
#else
#define __KCSAN_ACCESS_RW(alt) (alt)
#endif
/* Points to current test-case memory access "kernels". */
static void (*access_kernels[2])(void);
static struct task_struct **threads; /* Lists of threads. */
static unsigned long end_time; /* End time of test. */
/* Report as observed from console. */
static struct {
spinlock_t lock;
int nlines;
char lines[3][512];
} observed = {
.lock = __SPIN_LOCK_UNLOCKED(observed.lock),
};
/* Setup test checking loop. */
static __no_kcsan inline void
begin_test_checks(void (*func1)(void), void (*func2)(void))
{
kcsan_disable_current();
/*
* Require at least as long as KCSAN_REPORT_ONCE_IN_MS, to ensure at
* least one race is reported.
*/
end_time = jiffies + msecs_to_jiffies(CONFIG_KCSAN_REPORT_ONCE_IN_MS + 500);
/* Signal start; release potential initialization of shared data. */
smp_store_release(&access_kernels[0], func1);
smp_store_release(&access_kernels[1], func2);
}
/* End test checking loop. */
static __no_kcsan inline bool
end_test_checks(bool stop)
{
if (!stop && time_before(jiffies, end_time)) {
/* Continue checking */
might_sleep();
return false;
}
kcsan_enable_current();
return true;
}
/*
* Probe for console output: checks if a race was reported, and obtains observed
* lines of interest.
*/
__no_kcsan
static void probe_console(void *ignore, const char *buf, size_t len)
{
unsigned long flags;
int nlines;
/*
* Note that KCSAN reports under a global lock, so we do not risk the
* possibility of having multiple reports interleaved. If that were the
* case, we'd expect tests to fail.
*/
spin_lock_irqsave(&observed.lock, flags);
nlines = observed.nlines;
if (strnstr(buf, "BUG: KCSAN: ", len) && strnstr(buf, "test_", len)) {
/*
* KCSAN report and related to the test.
*
* The provided @buf is not NUL-terminated; copy no more than
* @len bytes and let strscpy() add the missing NUL-terminator.
*/
strscpy(observed.lines[0], buf, min(len + 1, sizeof(observed.lines[0])));
nlines = 1;
} else if ((nlines == 1 || nlines == 2) && strnstr(buf, "bytes by", len)) {
strscpy(observed.lines[nlines++], buf, min(len + 1, sizeof(observed.lines[0])));
if (strnstr(buf, "race at unknown origin", len)) {
if (WARN_ON(nlines != 2))
goto