KCSAN: data-race in shrink_folio_list / swap_cache_get_folio

Status: moderation: reported on 2026/11/17 15:28
Subsystems: mm
Labels: prio:low
[Documentation on labels]
Reported-by: syzbot+9729e31b04cba39101c0@syzkaller.appspotmail.com
First crash: 11h41m, last: 11h41m
✨ AI cause bisection: introduced by (AI job) :
commit 62629ae49baa70362125a02a488d885b3c17eab7
Author: Kairui Song <kasong@tencent.com>
Date: Tue Feb 17 20:06:31 2026 +0000

  mm, swap: implement helpers for reserving data in the swap table

  
✨ AI-generated root cause summary:

A data race occurs between lockless swap cache lookup in swap_cache_get_folio() and folio LRU list manipulation in shrink_folio_list(). In struct page and struct folio, folio->lru.next is unioned with page->compound_info at offset 8 bytes (FOLIO_MATCH(compound_info, lru)). When a folio in the swap cache fails or bypasses reclaim, shrink_folio_list() unlocks it and calls list_add(&folio->lru, &ret_folios), which performs an unmarked store to folio->lru.next. Concurrently, swap_cache_get_folio() invokes swp_tb_to_folio() -> pfn_folio() -> _compound_head(), which performs a READ_ONCE(page->compound_info) on the same union word without holding a lock or folio reference.

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

Root Cause Technical Analysis

1. Functions, Structs, and Fields Involved

  • swap_cache_get_folio() (mm/swap_state.c:101-105): Performs a lockless lookup of a swap entry in the swap cache/table (swap_table_get()) and attempts to obtain a folio reference via folio_try_get().
  • swp_tb_to_folio() (mm/swap_table.h:178-182): Converts a swap table entry (swp_tb) into a struct folio * by invoking pfn_folio().
  • _compound_head() (include/linux/page-flags.h:219-248): Called by pfn_folio() -> page_folio() on pfn_to_page(pfn). Performs READ_ONCE(page->compound_info) to determine whether the page is a tail page and locate its head folio.
  • shrink_folio_list() (mm/vmscan.c:1645-1647): Reclaim logic that unlocks folios that are not reclaimed and adds them to ret_folios using list_add(&folio->lru, &ret_folios).
  • __list_add() (include/linux/list.h:165-176): Writes new->next = next, which corresponds to folio->lru.next.
  • struct page and struct folio (include/linux/mm_types.h:89-131, 404-450, 517): At offset +8 bytes within the struct descriptor, page->compound_info (used for identifying compound tail pages via bit 0) aliases folio->lru.next via an anonymous union (FOLIO_MATCH(compound_info, lru)).

2. Detailed Mechanism and Trigger

  1. Unmapped or Kept Folios in Reclaim: A swap-backed folio resides in the swap cache. During page reclaim, shrink_folio_list() isolates the folio from its LRU list. If the folio cannot be reclaimed (for example, if unmapping failed, if it is pinned, or if it is dirty/locked), control jumps to keep_locked:, where folio_unlock(folio) is executed. At keep:, while still holding the isolation reference, shrink_folio_list() executes:

    list_add(&folio->lru, &ret_folios);
    

    Inside __list_add(), this performs an unmarked store (new->next = next) to folio->lru.next at offset 8 bytes of the folio.

  2. Concurrent Lockless Swap Cache Lookup: Concurrently, another task experiences a swap page fault for this folio (do_swap_page()) and calls swap_cache_get_folio(entry):

    swp_tb = swap_table_get(ci, ci_off);
    if (!swp_tb_is_folio(swp_tb))
        return NULL;
    folio = swp_tb_to_folio(swp_tb);
    if (!folio_try_get(folio))
        continue;
    

    swap_cache_get_folio() does not hold the cluster lock or any folio locks during the initial probe. When it calls swp_tb_to_folio(swp_tb), it invokes pfn_folio(), which calls page_folio(pfn_to_page(pfn)). This leads into _compound_head(), which executes:

    unsigned long info = READ_ONCE(page->compound_info);
    

    Since page->compound_info and folio->lru.next occupy the exact same memory word (offset 8), this READ_ONCE() reads from the exact same address (0xffffea00047ae9c8 in the report) that is concurrently modified by list_add().

  3. Concurrency Rule Violation: Because one side performs a concurrent unmarked write (new->next = next) while the other side performs a marked read (READ_ONCE(page->compound_info)) without synchronization (locks or RCU grace periods protecting list membership), KCSAN detects a data race under the Linux Kernel Memory Model (LKMM).

  4. Safety Analysis in Practice: Both lru.next pointers and head page addresses are naturally aligned kernel virtual pointers, meaning bit 0 is strictly 0. As documented in include/linux/mm_types.h:85-87, non-tail users of this union word must keep bit 0 clear to prevent collision with PageTail(). Both before and after the list_add() store, (info & 1) evaluates to 0, causing _compound_head() to return the correct head page (struct folio *)page. After folio_try_get(), swap_cache_get_folio() re-checks swap_table_get() to ensure the folio was not removed and replaced, making this race benign in practice on modern 64-bit architectures.

▶ ▼ ✨ AI Jobs (3)
ID Workflow Result Correct Bug Created Started Finished Revision
f7138757 bisect-cause ❓ KCSAN: data-race in shrink_folio_list / swap_cache_get_folio 2026/11/17 15:26 2026/11/17 15:26 2026/11/17 15:44 ca622473
332d6e4f assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ✅ UserNamespace: ✅ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in shrink_folio_list / swap_cache_get_folio 2026/11/17 15:18 2026/11/17 15:18 2026/11/17 15:27 ca622473
0c4404bb assessment-kcsan 🏃 KCSAN: data-race in shrink_folio_list / swap_cache_get_folio 2026/11/17 15:11 2026/11/17 15:11 ca622473

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

read (marked) to 0xffffea00047ae9c8 of 8 bytes by task 8191 on cpu 1:
 _compound_head include/linux/page-flags.h:221 [inline]
 pfn_folio include/linux/mm.h:2831 [inline]
 swp_tb_to_folio mm/swap_table.h:181 [inline]
 swap_cache_get_folio+0x102/0x2d0 mm/swap_state.c:104
 do_swap_page+0x26b/0x21b0 mm/memory.c:5073
 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

write to 0xffffea00047ae9c8 of 8 bytes by task 8197 on cpu 1:
 __list_add include/linux/list.h:173 [inline]
 list_add include/linux/list.h:192 [inline]
 shrink_folio_list+0x1526/0x2d70 mm/vmscan.c:1647
 evict_folios+0x1fa4/0x2990 mm/vmscan.c:5097
 try_to_shrink_lruvec+0x9fb/0xd60 mm/vmscan.c:5271
 lru_gen_shrink_lruvec mm/vmscan.c:5422 [inline]
 shrink_lruvec mm/vmscan.c:6199 [inline]
 shrink_node_memcgs mm/vmscan.c:6438 [inline]
 shrink_node+0x97e/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: 8197 Comm: syz.0.1371 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:05 upstream 666539eecdf4 a3e64cfc .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in shrink_folio_list / swap_cache_get_folio
* Struck through repros no longer work on HEAD.