KCSAN: data-race in data_alloc / desc_read

Status: moderation: reported on 2026/11/19 00:08
Subsystems: ext4
Labels: race:benign prio:low
[Documentation on labels]
Reported-by: syzbot+317b3a69b2fca8438554@syzkaller.appspotmail.com
First crash: 22h50m, last: 11h07m
✨ AI-generated root cause summary:

KCSAN reported a data race between an unannotated plain read in desc_read() and plain writes in data_alloc(). desc_read() optimistically copies descriptor metadata (desc->text_blk_lpos) using memcpy() between an initial load and a final verification of desc->state_var. Concurrently, a writer advancing the ringbuffer in prb_reserve() recycles the descriptor slot via desc_reserve() and writes new logical block positions in data_alloc(). Because desc_read() and data_alloc() do not use data_race() or READ_ONCE()/WRITE_ONCE() annotations, KCSAN detects the concurrent plain memory access, although the ringbuffer's memory barriers ensure torn reads are safely detected and discarded.

▶ ▼ ✨ Full AI root cause analysis
Based on kernel commit 20eef29b7c84:

Root Cause Analysis

1. Functions, Structs, and Files Involved


2. Detailed Technical Mechanism

The Linux printk_ringbuffer implementation uses a lockless memory model where descriptors (struct prb_desc) are indexed in a circular descriptor ring (prb_desc_ring). Descriptors are tracked and validated using an atomic state variable (desc->state_var), which encodes both the descriptor ID (including wrap generation counts) and its state (desc_reserved, desc_committed, desc_finalized, or desc_reusable).

  1. Optimistic Read in desc_read(): When a caller such as desc_push_tail() needs to inspect a descriptor (here the descriptor at tail_id), it invokes desc_read():

    • Line 460: Reads state_val = atomic_long_read(state_var); (desc_read:A).
    • Line 462: Checks if the state is consistent (d_state != desc_miss && d_state != desc_reserved).
    • Line 487: Executes smp_rmb() (desc_read:B).
    • Lines 494–497: Performs an optimistic copy of desc->text_blk_lpos into the caller-provided local descriptor desc_out:
      if (desc_out) {
          memcpy(&desc_out->text_blk_lpos, &desc->text_blk_lpos,
                 sizeof(desc_out->text_blk_lpos)); /* LMM(desc_read:C) */
      }
      
    • Line 541: Executes smp_rmb() (desc_read:D).
    • Lines 547–548: Re-reads state_val = atomic_long_read(state_var); (desc_read:E) and re-queries the state to confirm whether the descriptor was modified or recycled while the copy took place.
  2. Concurrent Recycling and Allocation in data_alloc(): Concurrently on another CPU, a task executing prb_reserve() is allocating space for a new printk record and recycles the oldest descriptor slot:

    • In desc_reserve(), the writer transitions desc->state_var to desc_reserved with the new wrap ID via atomic_long_try_cmpxchg(&desc->state_var, ...) (desc_reserve:F), which acts as a full memory barrier.
    • The writer then enters data_alloc(), where it assigns the logical positions for the newly reserved block using plain C assignments (lines 1128–1129):
      blk_lpos->begin = begin_lpos;
      blk_lpos->next = next_lpos;
      
      where blk_lpos points to &d->text_blk_lpos.
  3. Data-Race Manifestation: If desc_read() completes its initial atomic_long_read() (desc_read:A) just before the writer executes desc_reserve:F, desc_read() enters the memcpy() (desc_read:C) at the exact time data_alloc() writes to blk_lpos->begin and blk_lpos->next. Because memcpy() reads desc->text_blk_lpos without READ_ONCE() or data_race() annotations and data_alloc() performs plain stores without WRITE_ONCE() or data_race() annotations, KCSAN detects and reports a concurrent plain read/write data race on the 16-byte text_blk_lpos structure.


3. Correctness Guarantees Under the Ringbuffer Design

Although flagged by KCSAN as an unannotated data race, this scenario was anticipated by the ringbuffer design:

  • The full memory barrier in atomic_long_try_cmpxchg() (desc_reserve:F) pairs with smp_rmb() (desc_read:D).
  • If desc_read() reads during or after the writer changes desc->text_blk_lpos, the subsequent atomic_long_read() at desc_read:E is guaranteed to observe the new state_var (which contains either a different wrap ID or the desc_reserved state).
  • When desc_read() detects this state change, it returns desc_miss (or desc_reserved). Callers such as desc_push_tail() immediately branch away without accessing or utilizing desc_out->text_blk_lpos, safely discarding any torn or intermediate values copied during the race.
▶ ▼ ✨ AI Jobs (2)
ID Workflow Result Correct Bug Created Started Finished Revision
844d25d6 assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ✅ UserNamespace: ✅ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in data_alloc / desc_read 2026/11/18 22:06 2026/11/18 22:06 2026/11/18 22:15 2006bdcc
22615a2b assessment-kcsan Benign: ✅ ❓ KCSAN: data-race in data_alloc / desc_read 2026/11/18 21:54 2026/11/18 21:54 2026/11/18 22:09 2006bdcc

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

write to 0xffffffff86e6a390 of 8 bytes by task 18079 on cpu 0:
 data_alloc+0x326/0x3b0 kernel/printk/printk_ringbuffer.c:1128
 prb_reserve+0x7da/0x990 kernel/printk/printk_ringbuffer.c:1723
 vprintk_store+0x54e/0x910 kernel/printk/printk.c:2354
 vprintk_emit+0x1a4/0x580 kernel/printk/printk.c:2455
 vprintk_default+0x26/0x30 kernel/printk/printk.c:2494
 vprintk+0x1d/0x30 kernel/printk/printk_safe.c:82
 _printk+0x79/0xa0 kernel/printk/printk.c:2504
 set_capacity_and_notify+0x1d5/0x230 block/genhd.c:93
 loop_set_size+0x2e/0x70 drivers/block/loop.c:221
 loop_configure+0x826/0x9d0 drivers/block/loop.c:1000
 lo_ioctl+0x1e1/0xd60 drivers/block/loop.c:1454
 blkdev_ioctl+0x387/0x460 block/ioctl.c:798
 vfs_ioctl fs/ioctl.c:52 [inline]
 __do_sys_ioctl fs/ioctl.c:598 [inline]
 __se_sys_ioctl+0xce/0x140 fs/ioctl.c:584
 __x64_sys_ioctl+0x43/0x50 fs/ioctl.c:584
 x64_sys_call+0x239d/0x2550 arch/x86/include/generated/asm/syscalls_64.h:17
 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 to 0xffffffff86e6a390 of 16 bytes by task 18104 on cpu 1:
 desc_read+0xfe/0x220 kernel/printk/printk_ringbuffer.c:495
 desc_push_tail kernel/printk/printk_ringbuffer.c:791 [inline]
 desc_reserve kernel/printk/printk_ringbuffer.c:937 [inline]
 prb_reserve+0x1b1/0x990 kernel/printk/printk_ringbuffer.c:1673
 vprintk_store+0x54e/0x910 kernel/printk/printk.c:2354
 vprintk_emit+0x1a4/0x580 kernel/printk/printk.c:2455
 vprintk_default+0x26/0x30 kernel/printk/printk.c:2494
 vprintk+0x1d/0x30 kernel/printk/printk_safe.c:82
 _printk+0x79/0xa0 kernel/printk/printk.c:2504
 set_capacity_and_notify+0x1d5/0x230 block/genhd.c:93
 loop_set_size+0x2e/0x70 drivers/block/loop.c:221
 loop_configure+0x826/0x9d0 drivers/block/loop.c:1000
 lo_ioctl+0x1e1/0xd60 drivers/block/loop.c:1454
 blkdev_ioctl+0x387/0x460 block/ioctl.c:798
 vfs_ioctl fs/ioctl.c:52 [inline]
 __do_sys_ioctl fs/ioctl.c:598 [inline]
 __se_sys_ioctl+0xce/0x140 fs/ioctl.c:584
 __x64_sys_ioctl+0x43/0x50 fs/ioctl.c:584
 x64_sys_call+0x239d/0x2550 arch/x86/include/generated/asm/syscalls_64.h:17
 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: 18104 Comm: syz.7.3102 Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
==================================================================
loop7: detected capacity change from 0 to 33280
EXT4-fs error (device loop7): ext4_orphan_get:1404: inode #15: comm syz.7.3102: iget: bad i_size value: 38620345925642
loop7: lost file I/O error report for ino 15 type 5 pos 0x0 len 0x0 error -117
EXT4-fs error (device loop7): ext4_orphan_get:1409: comm syz.7.3102: couldn't read orphan inode 15 (err -117)
loop7: lost filesystem error report for type 5 error -117
EXT4-fs (loop7): mounted filesystem 00000000-0000-0000-0000-000000000000 r/w without journal. Quota mode: writeback.

Crashes (2):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/18 13:51 upstream 20eef29b7c84 eb4895d7 .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in data_alloc / desc_read
2026/11/18 02:07 upstream 63fdb511c27a 122f94d5 .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in data_alloc / desc_read
* Struck through repros no longer work on HEAD.