KCSAN: data-race in _find_next_zero_bit / sbitmap_find_bit

Status: moderation: reported on 2026/11/17 07:48
Subsystems: scsi
Labels: prio:low race:benign
[Documentation on labels]
Reported-by: syzbot+f266e0fe6df87b348a47@syzkaller.appspotmail.com
First crash: 4h01m, last: 4h01m
✨ AI-generated root cause summary:

In lib/sbitmap.c:__sbitmap_get_word(), find_next_zero_bit() performs an unmarked plain load on struct sbitmap_word::word to optimistically locate a zero bit. Concurrently, another thread executing __sbitmap_get_word() modifies the same sbitmap_word::word via the marked atomic primitive test_and_set_bit_lock(). KCSAN flags this plain read racing against an atomic write as a data race, though the race is benign because allocation is strictly gated by the atomic test-and-set operation.

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

Technical Mechanism & Root Cause

The report describes a Kernel Concurrency Sanitizer (KCSAN) data-race warning between _find_next_zero_bit() and sbitmap_find_bit() / __sbitmap_get_word().

1. Functions, Structs, and Fields Involved

2. Detailed Bug Trigger and Concurrency Flow

  1. Thread 0 (task 6224):

    • Calls sbitmap_get() -> __sbitmap_get() -> sbitmap_find_bit() -> sbitmap_find_bit_in_word() -> __sbitmap_get_word(&map->word, ...).
    • At lib/sbitmap.c:167, it attempts to find an available clear bit in map->word by invoking:
      nr = find_next_zero_bit(word, depth, hint);
      
    • In _find_next_zero_bit() (lib/find_bit.c:215), the macro FIND_NEXT_BIT(~addr[idx], ...) performs a plain memory read of addr[idx] (i.e. map->word). This read is unmarked (not using READ_ONCE() or atomic access primitives).
  2. Thread 1 (task 6223):

    • Concurrently executes __sbitmap_get_word(&map->word, ...) on another CPU.
    • Having previously identified a candidate zero bit index nr, it attempts to claim the bit at lib/sbitmap.c:181:
      if (!test_and_set_bit_lock(nr, word))
          break;
      
    • test_and_set_bit_lock() performs a marked atomic read-modify-write operation (lock bts on x86, instrumented via instrument_atomic_read_write).
  3. KCSAN Flag:

    • KCSAN observes an unmarked plain read on map->word concurrently with an atomic read-modify-write modification to the exact same 8-byte location, flagging it as a data race.

3. Invariant Verification and Benign Nature

  • Design Pattern: This is a classic optimistic/speculative lookup pattern in lockless algorithms. find_next_zero_bit() is strictly used as an un-synchronized hint search to find a candidate zero bit in map->word.
  • Exclusion Guarantees: Exclusive bit reservation is enforced by test_and_set_bit_lock(nr, word). If another CPU (e.g., Task 6223) claimed that bit in the interim, test_and_set_bit_lock() returns non-zero (true), rejecting the claim and causing the loop to advance hint = nr + 1 and retry.
  • Exhaustion Handling: If no zero bits are found (nr >= depth), __sbitmap_get_word() returns -1, prompting sbitmap_find_bit_in_word() to invoke sbitmap_deferred_clear(), which flushes deferred cleared bits under map->swap_lock.
  • Eliminating the Warning: Similar to how __sbitmap_queue_get_batch() was cleaned up in commit 6ad0d7e0f4b6 ("sbitmap: use READ_ONCE to access map->word"), this KCSAN report can be eliminated by taking a marked local snapshot via unsigned long val = READ_ONCE(*word); and passing &val to find_next_zero_bit().
▶ ▼ ✨ AI Jobs (3)
ID Workflow Result Correct Bug Created Started Finished Revision
8784d020 bisect-cause 🏃 KCSAN: data-race in _find_next_zero_bit / sbitmap_find_bit 2026/11/17 07:47 2026/11/17 07:47 a3e64cfc
cd47f104 assessment-kcsan Benign: ✅ ❓ KCSAN: data-race in _find_next_zero_bit / sbitmap_find_bit 2026/11/17 07:44 2026/11/17 07:44 2026/11/17 07:49 a3e64cfc
838ff464 assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ✅ UserNamespace: ✅ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in _find_next_zero_bit / sbitmap_find_bit 2026/11/17 07:30 2026/11/17 07:30 2026/11/17 07:47 a3e64cfc

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

read-write (marked) to 0xffff8881003e0080 of 8 bytes by task 6223 on cpu 1:
 instrument_atomic_read_write include/linux/instrumented.h:113 [inline]
 test_and_set_bit_lock include/asm-generic/bitops/instrumented-lock.h:57 [inline]
 __sbitmap_get_word lib/sbitmap.c:181 [inline]
 sbitmap_find_bit_in_word lib/sbitmap.c:200 [inline]
 sbitmap_find_bit+0x240/0x420 lib/sbitmap.c:245
 __sbitmap_get lib/sbitmap.c:277 [inline]
 sbitmap_get+0xa3/0x1a0 lib/sbitmap.c:291
 scsi_dev_queue_ready drivers/scsi/scsi_lib.c:1414 [inline]
 scsi_mq_get_budget+0x40/0x280 drivers/scsi/scsi_lib.c:1836
 blk_mq_get_dispatch_budget block/blk-mq.h:276 [inline]
 __blk_mq_do_dispatch_sched block/blk-mq-sched.c:112 [inline]
 blk_mq_do_dispatch_sched block/blk-mq-sched.c:182 [inline]
 __blk_mq_sched_dispatch_requests+0x3bf/0xd00 block/blk-mq-sched.c:307
 blk_mq_sched_dispatch_requests+0xad/0x160 block/blk-mq-sched.c:329
 blk_mq_run_hw_queue+0x163/0x200 block/blk-mq.c:2380
 blk_mq_dispatch_list+0x8bc/0xac0 arch/x86/include/asm/bitops.h:-1
 blk_mq_flush_plug_list+0x2cd/0x320 block/blk-mq.c:2990
 __blk_flush_plug+0x22f/0x2b0 block/blk-core.c:1274
 blk_finish_plug+0x47/0x70 block/blk-core.c:1301
 swap_cluster_readahead+0x381/0x460 mm/swap_state.c:920
 swapin_readahead+0xf5/0x890 mm/swap_state.c:1061
 do_swap_page+0x3f6/0x21b0 mm/memory.c:5083
 handle_pte_fault mm/memory.c:6668 [inline]
 __handle_mm_fault mm/memory.c:6817 [inline]
 handle_mm_fault+0xb84/0x3880 mm/memory.c:6984
 do_user_addr_fault+0x3ce/0xde0 arch/x86/mm/fault.c:1343
 handle_page_fault arch/x86/mm/fault.c:1483 [inline]
 exc_page_fault+0x62/0xa0 arch/x86/mm/fault.c:1536
 asm_exc_page_fault+0x26/0x30 arch/x86/include/asm/idtentry.h:595

read to 0xffff8881003e0080 of 8 bytes by task 6224 on cpu 0:
 _find_next_zero_bit+0x29/0xa0 lib/find_bit.c:215
 find_next_zero_bit include/linux/find.h:190 [inline]
 __sbitmap_get_word lib/sbitmap.c:167 [inline]
 sbitmap_find_bit_in_word lib/sbitmap.c:200 [inline]
 sbitmap_find_bit+0x205/0x420 lib/sbitmap.c:245
 __sbitmap_get lib/sbitmap.c:277 [inline]
 sbitmap_get+0xa3/0x1a0 lib/sbitmap.c:291
 scsi_dev_queue_ready drivers/scsi/scsi_lib.c:1414 [inline]
 scsi_mq_get_budget+0x40/0x280 drivers/scsi/scsi_lib.c:1836
 blk_mq_get_dispatch_budget block/blk-mq.h:276 [inline]
 __blk_mq_do_dispatch_sched block/blk-mq-sched.c:112 [inline]
 blk_mq_do_dispatch_sched block/blk-mq-sched.c:182 [inline]
 __blk_mq_sched_dispatch_requests+0x3bf/0xd00 block/blk-mq-sched.c:307
 blk_mq_sched_dispatch_requests+0xad/0x160 block/blk-mq-sched.c:329
 blk_mq_run_hw_queue+0x163/0x200 block/blk-mq.c:2380
 blk_mq_dispatch_list+0x8bc/0xac0 arch/x86/include/asm/bitops.h:-1
 blk_mq_flush_plug_list+0x2cd/0x320 block/blk-mq.c:2990
 __blk_flush_plug+0x22f/0x2b0 block/blk-core.c:1274
 blk_finish_plug+0x47/0x70 block/blk-core.c:1301
 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

value changed: 0x00000003ffffffff -> 0x0000000fffffffff

Reported by Kernel Concurrency Sanitizer on:
CPU: 0 UID: 0 PID: 6224 Comm: syz.5.721 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:02 upstream 666539eecdf4 a3e64cfc .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in _find_next_zero_bit / sbitmap_find_bit
* Struck through repros no longer work on HEAD.