Skip to content

Commit f433cf2

Browse files
committed
Merge tag 'kcsan.2022.12.02a' of git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu
Pull KCSAN updates from Paul McKenney: - Add instrumentation for memcpy(), memset(), and memmove() for Clang v16+'s new function names that are used when the -fsanitize=thread argument is given - Fix objtool warnings from KCSAN's volatile instrumentation, and typos in a pair of Kconfig options' help clauses * tag 'kcsan.2022.12.02a' of git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu: kcsan: Fix trivial typo in Kconfig help comments objtool, kcsan: Add volatile read/write instrumentation to whitelist kcsan: Instrument memcpy/memset/memmove with newer Clang
2 parents 5517a2e + 144b915 commit f433cf2

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

kernel/kcsan/core.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
#include <linux/init.h>
1515
#include <linux/kernel.h>
1616
#include <linux/list.h>
17+
#include <linux/minmax.h>
1718
#include <linux/moduleparam.h>
1819
#include <linux/percpu.h>
1920
#include <linux/preempt.h>
2021
#include <linux/sched.h>
22+
#include <linux/string.h>
2123
#include <linux/uaccess.h>
2224

2325
#include "encoding.h"
@@ -1308,3 +1310,51 @@ noinline void __tsan_atomic_signal_fence(int memorder)
13081310
}
13091311
}
13101312
EXPORT_SYMBOL(__tsan_atomic_signal_fence);
1313+
1314+
#ifdef __HAVE_ARCH_MEMSET
1315+
void *__tsan_memset(void *s, int c, size_t count);
1316+
noinline void *__tsan_memset(void *s, int c, size_t count)
1317+
{
1318+
/*
1319+
* Instead of not setting up watchpoints where accessed size is greater
1320+
* than MAX_ENCODABLE_SIZE, truncate checked size to MAX_ENCODABLE_SIZE.
1321+
*/
1322+
size_t check_len = min_t(size_t, count, MAX_ENCODABLE_SIZE);
1323+
1324+
check_access(s, check_len, KCSAN_ACCESS_WRITE, _RET_IP_);
1325+
return memset(s, c, count);
1326+
}
1327+
#else
1328+
void *__tsan_memset(void *s, int c, size_t count) __alias(memset);
1329+
#endif
1330+
EXPORT_SYMBOL(__tsan_memset);
1331+
1332+
#ifdef __HAVE_ARCH_MEMMOVE
1333+
void *__tsan_memmove(void *dst, const void *src, size_t len);
1334+
noinline void *__tsan_memmove(void *dst, const void *src, size_t len)
1335+
{
1336+
size_t check_len = min_t(size_t, len, MAX_ENCODABLE_SIZE);
1337+
1338+
check_access(dst, check_len, KCSAN_ACCESS_WRITE, _RET_IP_);
1339+
check_access(src, check_len, 0, _RET_IP_);
1340+
return memmove(dst, src, len);
1341+
}
1342+
#else
1343+
void *__tsan_memmove(void *dst, const void *src, size_t len) __alias(memmove);
1344+
#endif
1345+
EXPORT_SYMBOL(__tsan_memmove);
1346+
1347+
#ifdef __HAVE_ARCH_MEMCPY
1348+
void *__tsan_memcpy(void *dst, const void *src, size_t len);
1349+
noinline void *__tsan_memcpy(void *dst, const void *src, size_t len)
1350+
{
1351+
size_t check_len = min_t(size_t, len, MAX_ENCODABLE_SIZE);
1352+
1353+
check_access(dst, check_len, KCSAN_ACCESS_WRITE, _RET_IP_);
1354+
check_access(src, check_len, 0, _RET_IP_);
1355+
return memcpy(dst, src, len);
1356+
}
1357+
#else
1358+
void *__tsan_memcpy(void *dst, const void *src, size_t len) __alias(memcpy);
1359+
#endif
1360+
EXPORT_SYMBOL(__tsan_memcpy);

lib/Kconfig.kcsan

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ config KCSAN_SKIP_WATCH
125125
default 4000
126126
help
127127
The number of per-CPU memory operations to skip, before another
128-
watchpoint is set up, i.e. one in KCSAN_WATCH_SKIP per-CPU
128+
watchpoint is set up, i.e. one in KCSAN_SKIP_WATCH per-CPU
129129
memory operations are used to set up a watchpoint. A smaller value
130130
results in more aggressive race detection, whereas a larger value
131131
improves system performance at the cost of missing some races.
@@ -135,8 +135,8 @@ config KCSAN_SKIP_WATCH_RANDOMIZE
135135
default y
136136
help
137137
If instruction skip count should be randomized, where the maximum is
138-
KCSAN_WATCH_SKIP. If false, the chosen value is always
139-
KCSAN_WATCH_SKIP.
138+
KCSAN_SKIP_WATCH. If false, the chosen value is always
139+
KCSAN_SKIP_WATCH.
140140

141141
config KCSAN_INTERRUPT_WATCHER
142142
bool "Interruptible watchers" if !KCSAN_STRICT

tools/objtool/check.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,16 @@ static const char *uaccess_safe_builtin[] = {
999999
"__tsan_read_write4",
10001000
"__tsan_read_write8",
10011001
"__tsan_read_write16",
1002+
"__tsan_volatile_read1",
1003+
"__tsan_volatile_read2",
1004+
"__tsan_volatile_read4",
1005+
"__tsan_volatile_read8",
1006+
"__tsan_volatile_read16",
1007+
"__tsan_volatile_write1",
1008+
"__tsan_volatile_write2",
1009+
"__tsan_volatile_write4",
1010+
"__tsan_volatile_write8",
1011+
"__tsan_volatile_write16",
10021012
"__tsan_atomic8_load",
10031013
"__tsan_atomic16_load",
10041014
"__tsan_atomic32_load",

0 commit comments

Comments
 (0)