Linus Torvalds uses AI to fix Linux page cache, but reviewers find race condition
Linus Torvalds used Claude Code to draft a Linux kernel patch that boosted read throughput by 23x, but kernel maintainers identified a data race before merging.
Linux creator Linus Torvalds recently employed Anthropic’s Claude Code to address a persistent performance bottleneck in the Linux kernel page cache. The resulting patch series demonstrated massive throughput gains in synthetic benchmarks, yet kernel reviewers quickly identified a critical race condition that prevents immediate integration.
What happened
Torvalds submitted a ten-patch series aimed at resolving a reference-counting contention issue that has plagued kernel developers for over two years. He had personally struggled with a broken version of his own fix for eighteen months, while other engineers faced similar hurdles. The new approach, generated with the assistance of an AI coding tool, removed folio reference-count operations from a common buffered-read path. On his 64-thread AMD Ryzen Threadripper system, the patch increased read throughput from 13.3 million to 304 million reads per second.
Despite these impressive numbers, Torvalds cautioned that real-world workloads would see significantly smaller improvements. The benchmark environment was highly synthetic, designed to stress specific contention points rather than reflect typical user behavior. Nevertheless, the potential performance uplift drew immediate attention from the kernel community, prompting a rigorous review process that is standard for changes to core memory management subsystems.
Reviewers did not take long to find issues. Matthew Wilcox, a longtime kernel memory-management developer, raised concerns about the design of the new reclamation mechanism. Kiryl Shutsemau, another kernel developer who had previously worked on this bottleneck, identified a race condition in the code. As a result, Torvalds has handed further development of the patch to Shutsemau, acknowledging that the AI-generated solution requires significant refinement before it is safe for production use.
How it works
The core problem involved folio reference-count contention, which accounted for nearly all cross-core load hits in certain multi-threaded tests. Previous attempts to fix this involved copying data without holding a folio reference and checking validity afterward, which left the system vulnerable to races during memory reclamation. The new patch series introduces a PG_rcu_free flag and uses RCU-delayed reclamation. This method protects the folio while it is being copied to userspace, provided no page faults occur.
Claude Code wrote the implementation, tests, and initial documentation under Torvalds’ direction. The AI assistant helped navigate the complex logic required to eliminate the reference-count overhead. However, the design choices made by the AI, such as the specific flag implementation, sparked debate among human maintainers. Wilcox suggested using a page type instead of a flag and reclaiming inactive-list folios after an RCU grace period, but Torvalds rejected these ideas due to coverage gaps in truncate and hole-punch operations.
Key details
- Torvalds used Claude Code to generate the implementation, tests, and documentation for the page cache fix.
- Benchmarks on a 64-thread Threadripper showed a 23-fold increase in read throughput, rising from 13.3 million to 304 million reads per second.
- Kernel cycles per read dropped from nearly 20,000 to 735 in the high-contention benchmark scenario.
- Reviewer Kiryl Shutsemau found a race condition in
filemap_read_rcu_batch()where folio replacement could expose unwritten data. - The patch introduces a new
PG_rcu_freeflag, which Torvalds argues is necessary to handle mappings into userspace correctly. - Single-threaded performance regressed in early versions, with 16-byte reads increasing from roughly 4,150 to 6,550 cycles due to additional faults.
Why it matters
This incident serves as a realistic case study for engineers integrating AI assistants into complex systems programming. While the AI successfully produced code that achieved dramatic performance improvements in a controlled environment, it failed to account for subtle concurrency issues that experienced human reviewers spotted immediately. It highlights that AI can accelerate the drafting of solutions but cannot yet replace the deep architectural scrutiny required for kernel-level development.
For software builders, the story underscores the importance of maintaining rigorous review processes even when using advanced coding tools. The speed at which the AI generated the patch did not translate to readiness for merge. The human element remains critical for identifying edge cases, such as the race condition involving i_size extension, which could lead to data corruption if left unchecked.
What you can do
- Treat AI-generated code as a first draft that requires thorough manual review, especially in concurrent or low-level systems.
- Verify performance claims with realistic workloads, as synthetic benchmarks may not reflect real-world trade-offs like single-threaded regressions.
- Maintain strict code review standards for critical paths, ensuring that multiple experts validate changes to shared resources.
- Be prepared to iterate on AI suggestions, as initial implementations may contain design flaws that require architectural adjustments.
- Monitor for subtle race conditions in asynchronous operations, such as memory reclamation and page fault handling.
- Document the role of AI in your development process to ensure transparency and facilitate targeted reviews by team members.



