Root Cause Analysis
The KCSAN report is triggered by an unmarked plain load of struct work_struct::data in process_one_work() racing with a marked atomic read-modify-write (test_and_set_bit()) on the same data field in queue_work_on().
Involved Entities
Mechanism
- On CPU 1,
kworker/1:4 runs worker_thread() -> process_scheduled_works() -> process_one_work() to execute set_printk_work while holding pool->lock with IRQs disabled.
- At
kernel/workqueue.c:3419, WORK_STRUCT_PENDING_BIT (bit 0 of work->data) is still set; it is not cleared until set_work_pool_and_clear_pending(work, ...) is called later at line 3456.
- While
get_work_pwq(work) at line 3388 reads work->data using atomic_long_read(&work->data), line 3419 performs an unmarked plain dereference *work_data_bits(work) to extract the flush color and WORK_STRUCT_INACTIVE flag for worker->current_color and pwq_dec_nr_in_flight().
- Concurrently on CPU 0, another task verifying a BPF program that uses
bpf_trace_printk calls bpf_get_trace_printk_proto() -> schedule_work(&set_printk_work) -> queue_work_on(). Without acquiring pool->lock, queue_work_on() executes test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)), which KCSAN instruments as an 8-byte atomic read-write (instrument_atomic_read_write()).
- Because
queue_work_on() performs a marked read-write concurrently with the unmarked read *work_data_bits(work) in process_one_work(), KCSAN reports a data race on set_printk_work.data.
Invariant Analysis
The race is benign at the algorithmic level:
- At
kernel/workqueue.c:3419, pool->lock is held and WORK_STRUCT_PENDING_BIT is guaranteed to be 1 until set_work_pool_and_clear_pending() at line 3456 (which is bracketed by smp_wmb() and smp_mb() barriers). No other path can modify work->data while WORK_STRUCT_PENDING_BIT is set without acquiring pool->lock.
test_and_set_bit(WORK_STRUCT_PENDING_BIT, ...) in queue_work_on() atomically observes that bit 0 is already 1, leaves all bits of work->data unchanged, and returns true, causing queue_work_on() to return false without enqueueing or modifying the work item.
- Replacing
*work_data_bits(work) at kernel/workqueue.c:3419 (and similar unmarked reads such as try_to_grab_pending() at line 2265) with atomic_long_read(&work->data) or READ_ONCE(*work_data_bits(work)) would make the access marked and silence the KCSAN report.