Root Cause Analysis
1. Functions, Structs, and Fields Involved
- File:
block/blk.h
- Function:
static inline u64 blk_time_get_ns(void)
- File:
block/blk-core.c
- Function:
void blk_finish_plug(struct blk_plug *plug)
- Structs and Fields:
2. Detailed Technical Mechanism
blk_time_get_ns() is a block-layer helper designed to return the current timestamp in nanoseconds, optimizing frequent timestamp fetches during request batching by caching ktime_get_ns() inside current->plug->cur_ktime for the duration of a plugged sequence.
Because plugging is per-task (task_struct::plug) and only valid in task context, blk_time_get_ns() is intended to bypass caching and directly call ktime_get_ns() whenever executed in non-task (interrupt/softirq) context:
// block/blk.h:739
static inline u64 blk_time_get_ns(void)
{
struct blk_plug *plug = current->plug; // Line 741: Unconditional read of current->plug
u64 now;
if (!plug || !in_task()) // Line 744: Checks !plug before !in_task()
return ktime_get_ns();
...
However, the local assignment struct blk_plug *plug = current->plug; at line 741 is performed unconditionally before testing in_task(). Furthermore, in the condition if (!plug || !in_task()), !plug is evaluated first.
When a task finishes a plug sequence in blk_finish_plug():
// block/blk-core.c:1298
void blk_finish_plug(struct blk_plug *plug)
{
if (plug == current->plug) {
__blk_flush_plug(plug, false);
current->plug = NULL; // Line 1302: Plain store to task_struct::plug
}
}
The task writes NULL to current->plug at line 1302 via a plain store.
If a hardware interrupt arrives on the same CPU while blk_finish_plug() is executing this store (or during KCSAN watchpoint setup on that store), the interrupt handler runs. Upon completing hardware IRQ processing, softirqs are invoked on that CPU (__irq_exit_rcu $\to$ invoke_softirq $\to$ __do_softirq $\to$ handle_softirqs $\to$ blk_done_softirq).
In the softirq context (blk_done_softirq $\to$ scsi_complete $\to$ __blk_mq_end_request), blk_time_get_ns() is called to record completion timestamps. Because the softirq interrupted the task running on that CPU, current still references the interrupted task (task_struct). blk_time_get_ns() then executes struct blk_plug *plug = current->plug; at line 741, reading the exact same field (current->plug) that task context was in the process of clearing.
Because both accesses are plain C memory accesses without atomic primitives (READ_ONCE/WRITE_ONCE) or interrupt disabling, KCSAN detects a data race between:
3. Invariant Violation and Fix
The block layer assumes struct task_struct::plug is private to task context and is never accessed across threads or from interrupt contexts. Reading current->plug in interrupt or softirq context violates this invariant.
Although the value read by blk_time_get_ns() in softirq context is completely discarded (because in_task() is false and the function immediately returns ktime_get_ns()), the plain load constitutes a data race.
The fix is to test !in_task() before accessing current->plug:
--- a/block/blk.h
+++ b/block/blk.h
@@ -738,10 +738,13 @@ static inline int req_ref_read(struct request *req)
static inline u64 blk_time_get_ns(void)
{
- struct blk_plug *plug = current->plug;
+ struct blk_plug *plug;
u64 now;
- if (!plug || !in_task())
+ if (!in_task())
+ return ktime_get_ns();
+
+ plug = current->plug;
+ if (!plug)
return ktime_get_ns();
Checking !in_task() first avoids dereferencing current->plug in interrupt and softirq contexts altogether, eliminating the data race and saving redundant cache accesses in interrupt handlers.