KCSAN: data-race in __blk_mq_end_request / blk_finish_plug

Status: moderation: reported on 2026/11/17 09:46
Subsystems: block
Labels: prio:low race:benign
[Documentation on labels]
Reported-by: syzbot+18a9c0f809bf0e80076a@syzkaller.appspotmail.com
First crash: 7h07m, last: 7h07m
✨ AI cause bisection: candidate commits (most relevant first) (AI job):
  da4c8c3d0975 block: cache current nsec time in struct blk_plug
  b874d4aae58b block: limit block time caching to in_task() context
  
✨ AI-generated root cause summary:

In blk_time_get_ns() (block/blk.h), current->plug is unconditionally read before checking !in_task(). When a task calls blk_finish_plug() (block/blk-core.c), it performs a plain store current->plug = NULL;. If a hardware interrupt hits the same CPU during this operation and subsequently runs a block request completion softirq (blk_done_softirq), blk_time_get_ns() executes in interrupt context where current still refers to the interrupted task, performing a concurrent plain read of current->plug. The read value is immediately discarded because !in_task() evaluates to true, rendering this data race benign in production.

▶ ▼ ✨ Full AI root cause analysis
Based on kernel commit 666539eecdf4:

Root Cause Analysis

1. Functions, Structs, and Fields Involved


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.

▶ ▼ ✨ AI Jobs (3)
ID Workflow Result Correct Bug Created Started Finished Revision
6be42be8 bisect-cause ❓ KCSAN: data-race in __blk_mq_end_request / blk_finish_plug 2026/11/17 09:46 2026/11/17 09:46 2026/11/17 10:50 a3e64cfc
a22e19ed assessment-kcsan Benign: ✅ ❓ KCSAN: data-race in __blk_mq_end_request / blk_finish_plug 2026/11/17 09:44 2026/11/17 09:44 2026/11/17 09:59 a3e64cfc
0a3cd20e assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ✅ UserNamespace: ✅ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in __blk_mq_end_request / blk_finish_plug 2026/11/17 09:36 2026/11/17 09:36 2026/11/17 09:46 a3e64cfc

Sample crash report:
==================================================================
BUG: KCSAN: data-race in __blk_mq_end_request / blk_finish_plug

read to 0xffff888104738940 of 8 bytes by interrupt on cpu 1:
 blk_time_get_ns block/blk.h:741 [inline]
 __blk_mq_end_request+0x58/0x380 block/blk-mq.c:1151
 scsi_end_request+0x28e/0x3a0 drivers/scsi/scsi_lib.c:704
 scsi_io_completion+0x7f/0x1d0 drivers/scsi/scsi_lib.c:1125
 scsi_finish_command+0x233/0x250 drivers/scsi/scsi.c:198
 scsi_complete+0x165/0x660 drivers/scsi/scsi_lib.c:1604
 blk_complete_reqs block/blk-mq.c:1242 [inline]
 blk_done_softirq+0x8d/0xc0 block/blk-mq.c:1247
 handle_softirqs+0x109/0x310 kernel/softirq.c:669
 __do_softirq kernel/softirq.c:703 [inline]
 invoke_softirq kernel/softirq.c:542 [inline]
 __irq_exit_rcu+0x40/0xf0 kernel/softirq.c:786
 common_interrupt+0x83/0x90 arch/x86/kernel/irq.c:273
 asm_common_interrupt+0x26/0x40 arch/x86/include/asm/idtentry.h:665
 native_pause arch/x86/include/asm/vdso/processor.h:13 [inline]
 delay_tsc+0x43/0xa0 arch/x86/lib/delay.c:76
 udelay include/asm-generic/delay.h:64 [inline]
 delay_access kernel/kcsan/core.c:332 [inline]
 kcsan_setup_watchpoint+0x255/0x3f0 kernel/kcsan/core.c:622
 blk_finish_plug+0x4f/0x70 block/blk-core.c:1302
 lru_gen_shrink_lruvec mm/vmscan.c:5427 [inline]
 shrink_lruvec mm/vmscan.c:6199 [inline]
 shrink_node_memcgs mm/vmscan.c:6438 [inline]
 shrink_node+0x9f6/0x4600 mm/vmscan.c:6482
 shrink_zones mm/vmscan.c:6701 [inline]
 do_try_to_free_pages+0x310/0xb80 mm/vmscan.c:6763
 try_to_free_mem_cgroup_pages+0x201/0x450 mm/vmscan.c:7044
 mem_cgroup_force_empty mm/memcontrol-v1.c:1502 [inline]
 mem_cgroup_force_empty_write+0x6b/0x160 mm/memcontrol-v1.c:1518
 cgroup_file_write+0x197/0x350 kernel/cgroup/cgroup.c:4426
 kernfs_fop_write_iter+0x1d2/0x2e0 fs/kernfs/file.c:345
 new_sync_write fs/read_write.c:575 [inline]
 vfs_write+0x57a/0x9b0 fs/read_write.c:667
 ksys_write+0xdc/0x1a0 fs/read_write.c:719
 __do_sys_write fs/read_write.c:730 [inline]
 __se_sys_write fs/read_write.c:727 [inline]
 __x64_sys_write+0x40/0x50 fs/read_write.c:727
 x64_sys_call+0x154e/0x2550 arch/x86/include/generated/asm/syscalls_64.h:2
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x11e/0x3a0 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

write to 0xffff888104738940 of 8 bytes by task 4672 on cpu 1:
 blk_finish_plug+0x4f/0x70 block/blk-core.c:1302
 lru_gen_shrink_lruvec mm/vmscan.c:5427 [inline]
 shrink_lruvec mm/vmscan.c:6199 [inline]
 shrink_node_memcgs mm/vmscan.c:6438 [inline]
 shrink_node+0x9f6/0x4600 mm/vmscan.c:6482
 shrink_zones mm/vmscan.c:6701 [inline]
 do_try_to_free_pages+0x310/0xb80 mm/vmscan.c:6763
 try_to_free_mem_cgroup_pages+0x201/0x450 mm/vmscan.c:7044
 mem_cgroup_force_empty mm/memcontrol-v1.c:1502 [inline]
 mem_cgroup_force_empty_write+0x6b/0x160 mm/memcontrol-v1.c:1518
 cgroup_file_write+0x197/0x350 kernel/cgroup/cgroup.c:4426
 kernfs_fop_write_iter+0x1d2/0x2e0 fs/kernfs/file.c:345
 new_sync_write fs/read_write.c:575 [inline]
 vfs_write+0x57a/0x9b0 fs/read_write.c:667
 ksys_write+0xdc/0x1a0 fs/read_write.c:719
 __do_sys_write fs/read_write.c:730 [inline]
 __se_sys_write fs/read_write.c:727 [inline]
 __x64_sys_write+0x40/0x50 fs/read_write.c:727
 x64_sys_call+0x154e/0x2550 arch/x86/include/generated/asm/syscalls_64.h:2
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x11e/0x3a0 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 4672 Comm: syz.4.293 Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
==================================================================

Crashes (1):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/17 04:00 upstream 666539eecdf4 a3e64cfc .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in __blk_mq_end_request / blk_finish_plug
* Struck through repros no longer work on HEAD.