Linus用Claude Code写Linux补丁提23倍性能,人类审查揪出AI漏的bug

The creator and chief maintainer of Linux, Linus Torvalds, has collaborated with an AI coding tool to address a long-standing performance bottleneck in the kernel’s page cache system. The result? A set of patches that boosted read throughput by 23 times in synthetic benchmarks—though a human kernel reviewer caught a critical bug that even the AI and Torvalds himself missed, underscoring the irreplaceable role of human code review.
Torvalds had spent 18 months testing a flawed fix for the page cache’s reference count bottleneck, while other developers’ attempts also hit roadblocks. The new patches eliminate folio reference count operations from common buffered read paths, though Torvalds noted real-world performance gains would likely be more modest than benchmark results.
The breakthrough came when Torvalds used Claude Code, a tool from Anthropic, to develop the solution. The patches, part of the rcu-free-v3 series (10 commits based on Linux 7.3-rc6), introduce a new PG_rcu_free flag and RCU delayed recycling mechanism, designed to protect folios during user-space data transfers to avoid race conditions.
But the patches weren’t without flaws. During review, maintainer Kiryl Shutsemau identified a race condition in the key function filemap_read_rcu_batch(): when an old folio is replaced by a new one, and another process expands the file size, the read path could use the old folio’s snapshot information while calculating boundaries based on the new size, leading to returning unwritten data from the extended part of the file. Torvalds revised the patch to fix this by reordering operations: reading the file size first before accessing the folio, merging validity checks and boundary logic.
Performance tests showed dramatic improvements. On Torvalds’ own laptop with 8 threads, throughput tripled from 10 million to 33 million reads per second, with per-read kernel cycles dropping from approximately 2950 to 725. On a 64-thread Ryzen Threadripper, throughput surged from 13.3 million to 304 million reads per second, and CPU cycles per read fell from nearly 20,000 to 735. However, the delayed recycling mechanism introduced a tradeoff: single-threaded 16-byte reads saw latency rise from approximately 4150 to 6550 cycles, due to additional page faults when accessing unmapped user pages.
The patch design sparked debate among kernel developers. Matthew Wilcox questioned the PG_rcu_free flag, suggesting using page types instead and recycling folios from inactive lists after RCU grace periods. Torvalds rejected these proposals, arguing the flag must be set while the folio is still mapped to user space, and recycling only from inactive lists would miss truncate operations. Shutsemau proposed limiting the flag to 64-bit systems, a suggestion supported by Arm engineer and memory management maintainer David Hildenbrand.
Notably, Torvalds had previously criticized overhyped claims about AI in coding, calling out those who incorrectly attribute code to AI while downplaying compiler contributions. His use of Claude Code for this patch marks a shift in his approach to leveraging AI tools, even as the human review process remains critical to catching hidden bugs that automated systems and even the project’s lead might overlook.

