Technical Analysis & Root Cause
1. Bug Report & Context Breakdown
The reported KCSAN bug is:
BUG: KCSAN: data-race in memchr / vsnprintf
process_one_work kernel/workqueue.c:3495 [inline]
process_scheduled_works+0x582/0xab0 kernel/workqueue.c:3578
2. KCSAN Leaf Functions: Writer vs. Reader Mechanism
- KCSAN reports data races between two leaf access sites, sorting function names alphabetically (
memchr before vsnprintf).
- At least one access in a KCSAN data race must be a write:
memchr() (lib/string.c:787–796, fortified inline wrapper in include/linux/fortify-string.h:679–688) takes const void *s and performs byte reads (*p++ for at most n bytes) searching for character c. It never performs write accesses. Inspection of include/linux/fortify-string.h and lib/string.c also confirms that fortified string functions like sized_strscpy(), strlen(), and strnlen() use their own byte loops and do not lower to memchr().
vsnprintf() (lib/vsprintf.c) formats a string into a caller-provided destination buffer [buf, buf + size). It performs memory writes to that buffer.
- Consequently,
vsnprintf() is the writer formatting data into a shared buffer, while memchr() is the concurrent reader scanning that same buffer without adequate synchronization.
3. Investigation of Shared Buffers in kernel/workqueue.c
Because kernel/workqueue.c is the only file named in the trace, its own internal string buffers were audited:
worker->desc[WORKER_DESC_LEN] is initialized in process_one_work() via strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN) (line 3426).
- Running work functions can call
set_worker_desc(fmt, ...) (lines 6628–6639), which invokes vsnprintf(worker->desc, sizeof(worker->desc), fmt, args) without holding pool->lock.
- Concurrent readers of
worker->desc:
wq_worker_comm() (kernel/workqueue.c:6977–7011, invoked via proc_task_name() for /proc/[pid]/{comm,stat,status}) holds pool->lock and formats the string via scnprintf() (string_nocheck()), not memchr().
print_worker_info() (kernel/workqueue.c:6654–6688) copies via copy_from_kernel_nofault().
- Neither workqueue reader calls
memchr(). Thus, the racing shared buffer is an object managed by the specific subsystem callback invoked at worker->current_func(work) (kernel/workqueue.c:3495) rather than worker->desc itself.
4. Summary of Invariant Violation
The data race occurs when a subsystem work item executing under worker->current_func(work) (or concurrently with it) invokes vsnprintf() (or a wrapper such as sprintf(), snprintf(), or scnprintf()) to format text into a shared buffer in-place while another thread concurrently invokes memchr() to scan or parse the same buffer without proper locking (such as a mutex, spinlock, or RCU protection).