KCSAN: data-race in __free_by_rcu / unit_free_rcu

Status: moderation: reported on 2026/11/20 00:09
Subsystems: bpf
Labels: race:benign prio:low
[Documentation on labels]
Reported-by: syzbot+c4bf3ea5e84bca4f8665@syzkaller.appspotmail.com
First crash: 2d20h, last: 2d05h
✨ AI-generated root cause summary:

In kernel/bpf/memalloc.c, the per-CPU struct bpf_mem_cache uses the field 'tgt' as an approximate cache hint to guide freed memory units back to the allocating CPU's cache. In unit_free_rcu() (line 907) and unit_free() (line 874), c->tgt is updated using an unsynchronized, plain write outside interrupt-disabled sections. Concurrently, an in-flight RCU callback __free_by_rcu() (line 394) reads c->tgt using a plain load without synchronization or marked access. In the reported scenario, task context executing unit_free_rcu() on CPU 0 was interrupted by a timer interrupt whose softirq executed __free_by_rcu(), resulting in a KCSAN data race on c->tgt.

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

Root Cause Analysis

1. Involved Entities


2. Background and Object Lifecycle

The BPF memory allocator (struct bpf_mem_alloc) allocates memory units through per-CPU cache buckets (struct bpf_mem_cache *c).

  1. When an object is allocated via unit_alloc() (kernel/bpf/memalloc.c:819) or bpf_mem_cache_alloc_flags() (kernel/bpf/memalloc.c:1022), the memory unit prepends an 8-byte header (struct llist_node *llnode = ptr - LLIST_NODE_SZ;) storing a pointer to the allocating per-CPU cache:
    *(struct bpf_mem_cache **)llnode = c;
    
  2. When an object is freed on another (or the same) CPU using unit_free() or unit_free_rcu(), the allocator reads back this pointer and records it in c->tgt:
    /*
     * Remember bpf_mem_cache that allocated this object.
     * The hint is not accurate.
     */
    c->tgt = *(struct bpf_mem_cache **)llnode;
    
    This hint is designed to return freed objects back to the cache of the CPU that originally allocated them when draining local lists, preventing cross-CPU out-of-memory (OOM) imbalances while avoiding atomic overhead in fast paths.
  3. In unit_free_rcu(), after updating c->tgt, the freed node is enqueued onto c->free_by_rcu. When check_free_by_rcu(c) runs, pending nodes are transferred to c->waiting_for_gp, c->call_rcu_in_progress is set to 1, and an RCU grace period callback is scheduled via:
    call_rcu_hurry(&c->rcu, __free_by_rcu);
    
  4. Once the regular RCU grace period expires, RCU invokes __free_by_rcu(&c->rcu) in softirq context. __free_by_rcu() reads c->tgt to forward the grace-period-expired nodes into tgt->free_by_rcu_ttrace:
    static void __free_by_rcu(struct rcu_head *head)
    {
        struct bpf_mem_cache *c = container_of(head, struct bpf_mem_cache, rcu);
        struct bpf_mem_cache *tgt = c->tgt;
        ...
        llist_add_batch(llnode, c->waiting_for_gp_tail, &tgt->free_by_rcu_ttrace);
        do_call_rcu_ttrace(tgt);
        ...
        atomic_set(&c->call_rcu_in_progress, 0);
    }
    

3. Technical Mechanism of the Data Race

The data race reported by KCSAN occurs because c->tgt is updated and read using plain C memory accesses without memory barriers, concurrency annotations (READ_ONCE() / WRITE_ONCE()), or synchronization across execution contexts:

  1. Unsynchronized Write Outside IRQ-Disabled Section: In unit_free_rcu():

    static void notrace unit_free_rcu(struct bpf_mem_cache *c, void *ptr)
    {
        struct llist_node *llnode = ptr - LLIST_NODE_SZ;
        unsigned long flags;
    
        c->tgt = *(struct bpf_mem_cache **)llnode; // Line 907: Plain store with IRQs enabled
    
        local_irq_save(flags);
        ...
    

    c->tgt is written while interrupts are enabled in task context.

  2. Concurrent Read in Softirq Context: While an RCU grace period is in progress (c->call_rcu_in_progress == 1) or being dispatched, task context (e.g., executing bpf_map_update_value() -> trie_update_elem() -> bpf_mem_cache_free_rcu() -> unit_free_rcu()) writes to c->tgt. If a timer interrupt fires on the local CPU right around line 907, the interrupt return path (__irq_exit_rcu()) invokes pending softirqs via handle_softirqs() -> rcu_core() -> rcu_do_batch(). The RCU batch processes __free_by_rcu(), which executes line 394:

    struct bpf_mem_cache *tgt = c->tgt; // Line 394: Plain load
    

    This load runs concurrently with or directly interrupts the store in unit_free_rcu().

  3. Cross-CPU / RCU Thread Concurrency: Additionally, because RCU callbacks can execute on other CPUs (such as under CONFIG_RCU_NOCB_CPU kthreads or after CPU-hotplug offline events), __free_by_rcu() can run concurrently on another CPU while CPU 0's task context or IRQ work updates c->tgt.

Because both operations are plain, unannotated memory accesses to the shared field c->tgt, KCSAN flags this as a data race between task context and softirq context.

▶ ▼ ✨ AI Jobs (2)
ID Workflow Result Correct Bug Created Started Finished Revision
8d0c69d6 assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ❌ UserNamespace: ❌ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in __free_by_rcu / unit_free_rcu 2026/11/19 01:21 2026/11/19 01:21 2026/11/19 01:46 2006bdcc
531f4ed0 assessment-kcsan Benign: ✅ ❓ KCSAN: data-race in __free_by_rcu / unit_free_rcu 2026/11/19 01:15 2026/11/19 01:15 2026/11/19 01:29 2006bdcc

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

read to 0xffffe8ffffc37c98 of 8 bytes by interrupt on cpu 0:
 __free_by_rcu+0x30/0x1e0 kernel/bpf/memalloc.c:394
 rcu_do_batch kernel/rcu/tree.c:2650 [inline]
 rcu_core+0x76e/0xcb0 kernel/rcu/tree.c:2919
 rcu_core_si+0xd/0x20 kernel/rcu/tree.c:2936
 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
 instr_sysvec_apic_timer_interrupt arch/x86/kernel/apic/apic.c:1062 [inline]
 sysvec_apic_timer_interrupt+0x74/0x80 arch/x86/kernel/apic/apic.c:1062
 asm_sysvec_apic_timer_interrupt+0x1a/0x20 arch/x86/include/asm/idtentry.h:674
 __preempt_count_dec_and_test arch/x86/include/asm/preempt.h:123 [inline]
 __raw_spin_unlock_irqrestore include/linux/spinlock_api_smp.h:211 [inline]
 _raw_spin_unlock_irqrestore+0x1a/0x30 kernel/locking/spinlock.c:221
 spin_unlock_irqrestore include/linux/spinlock.h:425 [inline]
 lruvec_unlock_irqrestore include/linux/memcontrol.h:1577 [inline]
 folio_batch_move_lru+0x384/0x420 mm/folio.c:200
 __folio_batch_add_and_move+0x163/0x1e0 mm/folio.c:225
 folio_add_lru+0x9f/0x190 mm/folio.c:492
 folio_add_lru_vma+0x5d/0x80 mm/folio.c:511
 map_anon_folio_pte_nopf+0x1ef/0x3f0 mm/memory.c:5518
 map_anon_folio_pte_pf+0x7d/0x180 mm/memory.c:5528
 do_anonymous_page mm/memory.c:5631 [inline]
 do_pte_missing mm/memory.c:4776 [inline]
 handle_pte_fault mm/memory.c:6665 [inline]
 __handle_mm_fault mm/memory.c:6817 [inline]
 handle_mm_fault+0x37f1/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

write to 0xffffe8ffffc37c98 of 8 bytes by task 4568 on cpu 0:
 unit_free_rcu+0x3f/0x1f0 kernel/bpf/memalloc.c:907
 bpf_mem_cache_free_rcu+0x3e/0x50 kernel/bpf/memalloc.c:997
 trie_update_elem+0x8cc/0x950 kernel/bpf/lpm_trie.c:451
 bpf_map_update_value+0x4f7/0x570 kernel/bpf/syscall.c:297
 generic_map_update_batch+0x4e6/0x700 kernel/bpf/syscall.c:2125
 bpf_map_do_batch+0x26a/0x3b0 kernel/bpf/syscall.c:5849
 __sys_bpf+0x784/0xc50 kernel/bpf/syscall.c:-1
 __do_sys_bpf kernel/bpf/syscall.c:6582 [inline]
 __se_sys_bpf kernel/bpf/syscall.c:6579 [inline]
 __x64_sys_bpf+0x69/0x80 kernel/bpf/syscall.c:6579
 x64_sys_call+0xf26/0x2550 arch/x86/include/generated/asm/syscalls_64.h:322
 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: 0 UID: 0 PID: 4568 Comm: syz.6.98 Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
==================================================================

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