KCSAN: data-race in bio_put / bio_put

Status: moderation: reported on 2026/11/17 20:29
Subsystems: block
Labels: prio:low race:benign
[Documentation on labels]
Reported-by: syzbot+24d1caad1fd562617683@syzkaller.appspotmail.com
First crash: 1h56m, last: 1h56m
✨ AI cause bisection: candidate commits (most relevant first) (AI job):
  b99182c501c3 bio: add pcpu caching for non-polling bio_put
  c9f5f3aa19c6 block: extend bio caching to task context
  48f22f80938d block: enable per-cpu bio cache by default
  
✨ AI-generated root cause summary:

A data race occurs in block/bio.c:bio_put_percpu_cache() on cache->nr between task context and interrupt context on the same CPU. Task context enters bio_put_percpu_cache() with only preemption disabled (get_cpu()) without disabling interrupts or softirqs, and updates cache->nr at line 834. When an interrupt fires and runs either softirq (e.g. blk_done_softirq) or hardirq, bio_put() calls bio_put_percpu_cache(), which unconditionally reads cache->nr at line 825 before checking execution context, racing with the interrupted task's write.

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

Root Cause Analysis

1. Relevant Files, Functions, and Data Structures

  • Source File: block/bio.c
  • Functions Involved:
  • Data Structures & Fields:
    • struct bio_alloc_cache (block/bio.c:31):
      • struct bio *free_list: list of cached bios recycled from task context.
      • struct bio *free_list_irq: list of cached bios recycled from hardirq context.
      • unsigned int nr: number of bios currently in free_list (offset 16 / 0x10).
      • unsigned int nr_irq: number of bios currently in free_list_irq (offset 20 / 0x14).

2. Mechanism and Lifecycle Rules

In block/bio.c, struct bio_alloc_cache provides a per-CPU cache for allocated bios when REQ_ALLOC_CACHE is used. Because bio_put() can be called from different execution contexts:

  • Task context (in_task()) recycles bios onto cache->free_list and increments cache->nr.
  • Hardirq context (in_hardirq()) recycles bios onto cache->free_list_irq and increments cache->nr_irq.
  • Any other context (such as softirq context, where !in_task() && !in_hardirq()) cannot use the per-CPU cache and must fall back to freeing the bio via bio_free(bio).

In bio_put_percpu_cache(struct bio *bio):

static inline void bio_put_percpu_cache(struct bio *bio)
{
	struct bio_alloc_cache *cache;

	cache = per_cpu_ptr(bio->bi_pool->cache, get_cpu());
	if (READ_ONCE(cache->nr_irq) + cache->nr > ALLOC_CACHE_MAX)
		goto out_free;

	if (in_task()) {
		bio_uninit(bio);
		bio->bi_next = cache->free_list;
		/* Not necessary but helps not to iopoll already freed bios */
		bio->bi_bdev = NULL;
		cache->free_list = bio;
		cache->nr++;
		kmemleak_free(bio_slab_addr(bio));
	} else if (in_hardirq()) {
		lockdep_assert_irqs_disabled();

		bio_uninit(bio);
		bio->bi_next = cache->free_list_irq;
		cache->free_list_irq = bio;
		cache->nr_irq++;
		kmemleak_free(bio_slab_addr(bio));
	} else {
		goto out_free;
	}
	put_cpu();
	return;
out_free:
	put_cpu();
	bio_free(bio);
}

The function obtains the per-CPU cache via get_cpu(), which disables preemption (preempt_disable()), but does not disable local interrupts (local_irq_save()) or softirqs (local_bh_disable()).


3. How the Data Race is Triggered

  1. Task Context Write: A thread running in task context on CPU 1 (in the report, task 3788 executing an I/O completion or failure path via submit_bio_noacct -> bio_endio -> mpage_read_end_io -> bio_put) calls bio_put() on a bio with REQ_ALLOC_CACHE. It enters bio_put_percpu_cache(), matches if (in_task()), puts the bio on cache->free_list, and performs cache->nr++ at line 834 without disabling interrupts.

  2. Interrupt Interruption on the Same CPU: While task context is executing around line 834, a hardware interrupt fires on CPU 1. When the hardware interrupt finishes, __irq_exit_rcu() invokes pending softirqs because irq_count() == HARDIRQ_OFFSET (preemption disablement via get_cpu() increments PREEMPT_MASK, but does not set SOFTIRQ_MASK, allowing softirqs to execute on the same CPU on the interrupt exit path).

  3. Softirq Execution and Read Race: Softirq processing runs blk_done_softirq (e.g., servicing SCSI completion scsi_end_request -> bio_endio -> journal_end_buffer_io_sync -> bio_endio_bh -> bio_put). bio_put() is called for another bio that also has REQ_ALLOC_CACHE set. bio_put() calls bio_put_percpu_cache().

  4. Data Race on cache->nr: Before checking whether the caller context can even use the cache (in_task() or in_hardirq()), line 825 unconditionally evaluates:

    if (READ_ONCE(cache->nr_irq) + cache->nr > ALLOC_CACHE_MAX)
    

    This performs an uninstrumented, plain read of cache->nr from interrupt context on CPU 1, directly racing with the write to cache->nr (cache->nr++) by the interrupted task context on the same CPU.

Additionally, softirq context cannot use the cache at all (lines 828 and 836 evaluate to false, falling through to goto out_free;), making the access to cache->nr in softirq context completely unnecessary. A similar unsynchronized read of cache->nr also occurs when bio_put_percpu_cache() is invoked from hardirq context without READ_ONCE(cache->nr).

▶ ▼ ✨ AI Jobs (3)
ID Workflow Result Correct Bug Created Started Finished Revision
ab69c1e6 bisect-cause ❓ KCSAN: data-race in bio_put / bio_put 2026/11/17 20:21 2026/11/17 20:21 2026/11/17 21:05 ca622473
2a765e7c assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ✅ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ✅ RemoteTrigger: ❌ Unprivileged: ✅ UserNamespace: ✅ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in bio_put / bio_put 2026/11/17 20:18 2026/11/17 20:18 2026/11/17 20:28 ca622473
bbe1eb7f assessment-kcsan Benign: ✅ ❓ KCSAN: data-race in bio_put / bio_put 2026/11/17 20:13 2026/11/17 20:13 2026/11/17 21:17 ca622473

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

read to 0xffff888237d323f0 of 4 bytes by interrupt on cpu 1:
 bio_put_percpu_cache block/bio.c:825 [inline]
 bio_put+0xd0/0x490 block/bio.c:870
 bio_endio_bh+0x74/0xb0 fs/buffer.c:151
 journal_end_buffer_io_sync+0x3f/0x100 fs/jbd2/commit.c:35
 bio_endio+0x3ef/0x5e0 block/bio.c:1908
 blk_update_request+0x365/0x780 block/blk-mq.c:1036
 scsi_end_request+0x48/0x3a0 drivers/scsi/scsi_lib.c:671
 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
 rdtsc_ordered arch/x86/include/asm/tsc.h:57 [inline]
 delay_tsc+0x5b/0xa0 arch/x86/lib/delay.c:70
 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
 bio_put_percpu_cache block/bio.c:834 [inline]
 bio_put+0x2c9/0x490 block/bio.c:870
 mpage_read_end_io+0x44c/0x480 fs/mpage.c:53
 bio_endio+0x3ef/0x5e0 block/bio.c:1908
 bio_endio_status include/linux/bio.h:427 [inline]
 submit_bio_noacct+0x840/0xa80 block/blk-core.c:935
 submit_bio+0x2a4/0x2c0 block/blk-core.c:970
 mpage_bio_submit_read fs/mpage.c:74 [inline]
 mpage_read_folio+0x106/0x140 fs/mpage.c:396
 fat_read_folio+0x1c/0x30 fs/fat/inode.c:206
 filemap_read_folio+0x40/0x150 mm/filemap.c:2549
 filemap_fault+0x66b/0xab0 mm/filemap.c:3706
 __do_fault mm/memory.c:5677 [inline]
 do_read_fault mm/memory.c:6112 [inline]
 do_fault mm/memory.c:6246 [inline]
 do_pte_missing mm/memory.c:4778 [inline]
 handle_pte_fault mm/memory.c:6665 [inline]
 __handle_mm_fault mm/memory.c:6817 [inline]
 handle_mm_fault+0x2183/0x3880 mm/memory.c:6984
 faultin_page mm/gup.c:1130 [inline]
 __get_user_pages+0xf69/0x19e0 mm/gup.c:1432
 populate_vma_page_range+0x16b/0x1b0 mm/gup.c:1868
 __mm_populate+0x1b0/0x2c0 mm/gup.c:1971
 mm_populate include/linux/mm.h:4451 [inline]
 vm_mmap_pgoff+0x272/0x320 mm/util.c:586
 ksys_mmap_pgoff+0x250/0x310 mm/mmap.c:619
 x64_sys_call+0x23b3/0x2550 arch/x86/include/generated/asm/syscalls_64.h:10
 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

read-write to 0xffff888237d323f0 of 4 bytes by task 3788 on cpu 1:
 bio_put_percpu_cache block/bio.c:834 [inline]
 bio_put+0x2c9/0x490 block/bio.c:870
 mpage_read_end_io+0x44c/0x480 fs/mpage.c:53
 bio_endio+0x3ef/0x5e0 block/bio.c:1908
 bio_endio_status include/linux/bio.h:427 [inline]
 submit_bio_noacct+0x840/0xa80 block/blk-core.c:935
 submit_bio+0x2a4/0x2c0 block/blk-core.c:970
 mpage_bio_submit_read fs/mpage.c:74 [inline]
 mpage_read_folio+0x106/0x140 fs/mpage.c:396
 fat_read_folio+0x1c/0x30 fs/fat/inode.c:206
 filemap_read_folio+0x40/0x150 mm/filemap.c:2549
 filemap_fault+0x66b/0xab0 mm/filemap.c:3706
 __do_fault mm/memory.c:5677 [inline]
 do_read_fault mm/memory.c:6112 [inline]
 do_fault mm/memory.c:6246 [inline]
 do_pte_missing mm/memory.c:4778 [inline]
 handle_pte_fault mm/memory.c:6665 [inline]
 __handle_mm_fault mm/memory.c:6817 [inline]
 handle_mm_fault+0x2183/0x3880 mm/memory.c:6984
 faultin_page mm/gup.c:1130 [inline]
 __get_user_pages+0xf69/0x19e0 mm/gup.c:1432
 populate_vma_page_range+0x16b/0x1b0 mm/gup.c:1868
 __mm_populate+0x1b0/0x2c0 mm/gup.c:1971
 mm_populate include/linux/mm.h:4451 [inline]
 vm_mmap_pgoff+0x272/0x320 mm/util.c:586
 ksys_mmap_pgoff+0x250/0x310 mm/mmap.c:619
 x64_sys_call+0x23b3/0x2550 arch/x86/include/generated/asm/syscalls_64.h:10
 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: 3788 Comm: syz.4.46 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 19:52 upstream 666539eecdf4 122f94d5 .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in bio_put / bio_put
* Struck through repros no longer work on HEAD.