PromptZone - AI Prompts, Guides and Tools for Builders

Cover image for Linus Torvalds on AI Coding: The EXT4 Bug and How to Vet AI-Generated Code
Riya Morales
Riya Morales

Posted on

Linus Torvalds on AI Coding: The EXT4 Bug and How to Vet AI-Generated Code

Linus Torvalds called for developers to “tell your AI companion to get its act together” in a Linux kernel mailing list post on May 22, 2024, flagging a significant bug in AI-generated kernel code. The comment, noted in a Hacker News discussion, was a direct response to a patch for the EXT4 filesystem that introduced a regression due to incorrect locking logic.

The issue centered on the bigalloc feature. An AI-assisted patch incorrectly removed a lock needed when bigalloc is enabled, which could lead to data corruption or crashes. Torvalds’s critique wasn't just about one bug but a warning about the systemic risk of accepting AI-generated code without rigorous, domain-specific review.

What the AI Got Wrong with EXT4

The flawed patch altered how EXT4 handles block cloning. The AI's suggested code removed a critical ext4_inode_resume_unlocked_dio() call inside a conditional check for bigalloc. This function must be called while holding a lock (i_data_sem) to prevent race conditions. The AI failed to understand the locking semantics required for this specific filesystem feature, producing code that was syntactically valid but semantically dangerous.

The core failure was a lack of contextual reasoning. The model likely pattern-matched from other code but didn't comprehend the necessary preconditions for safe execution within the EXT4 subsystem.

Bottom line: The AI missed the hidden, non-local dependency between the bigalloc flag and a required locking protocol.

Linus Torvalds on AI Coding: The EXT4 Bug and How to Vet AI-Generated Code

How to Vet AI-Generated Code in Your Workflow

For developers using tools like GitHub Copilot, Cursor, or ChatGPT for coding, this incident provides a concrete checklist for review.

  1. Isolate and Test Concurrency. Any code touching locks, mutexes, or shared resources must be stress-tested. For kernel code, use tools like KCSAN (Kernel Concurrency Sanitizer). In user-space, employ thread sanitizers.
  2. Audit Conditional Logic. Scrutinize every if and else branch modified or suggested by the AI. Verify that state changes (like locking/unlocking) are consistent across all paths. The EXT4 bug existed in an else clause.
  3. Demand Code-Generation Prompts. When using an AI assistant, prompt it to generate the commit message and tests first. An AI that cannot articulate the change's purpose and edge cases likely doesn't understand it.
  4. Leverage Static Analysis. Run suggested code through stricter compilers (-Wall -Wextra -Werror) and static analyzers (Clang Static Analyzer, Coverity, smatch for kernel code). These can catch some classes of logical errors pattern-matching misses.

"Technical Deep Dive: The EXT4 bigalloc Locking Rule"
The bigalloc feature groups multiple blocks into a single cluster for efficiency. Operations on these clusters require holding i_data_sem because they affect multiple blocks' metadata simultaneously. The AI's patch saw that !ext4_has_inline_data(inode) was true and proceeded down a path that assumed standard (non-bigalloc) locking rules. The correct logic is:
if (ext4_has_inline_data(inode)) {
    // Handle inline data
} else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
    // Handle extent-based files
} else {
    // Handle traditional indirect-block files
}
// Locking for bigalloc must be considered AFTER this tree, not within it.
Enter fullscreen mode Exit fullscreen mode

The bug placed a lock-dependent function inside the else branch, breaking the rule that bigalloc locking is orthogonal to the file storage method.


Get the daily AI digest
One email each morning — what shipped in AI, and the prompts we tested. No filler.

The Pros and Cons of AI Coding Assistants

The Benefits:

  • Speed on Boilerplate: AI excels at generating repetitive code, API glue, and standard functions, potentially cutting initial draft time by 30-50%.
  • Documentation Drafting: Can quickly produce first drafts of docstrings and comments.
  • Alternative Exploration: Rapidly prototypes multiple approaches to a problem.

The Risks (The Torvalds Warning):

  • Illusory Competence: Code compiles but violates deep system invariants. The EXT4 bug passed basic testing until deeper review.
  • Context Amnesia: AI has limited "working memory" and fails to reason about constraints from earlier in a codebase or subsystem.
  • Architectural Decay: Over-reliance can lead to patchwork code that lacks a coherent design philosophy, making long-term maintenance harder.

Comparing AI Coding Tools and Their Guardrails

Different AI coding tools offer varying levels of integration and safety, but none fully eliminate the need for expert review.

Feature GitHub Copilot Cursor Traditional IDE + ChatGPT
Context Awareness File & open tabs Full repo, linked files Manual copy/paste only
Codebase Integration Moderate Deep (can reference other files) None
Built-in Vetting Basic duplicate detection Can run tests/commands in-agent None
Primary Risk Suggesting outdated APIs Making widespread, incorrect changes High effort to provide context

The critical commonality: None understand the why of your system's design. They are statistical engines, not engineers.

Who Should Heed This Warning (And Who Can Relax)

This is a critical alert for:

  • Kernel and Systems Programmers: Where incorrect code leads to security vulnerabilities and data loss.
  • Framework and Library Maintainers: Their code forms the foundation for others; bugs have multiplicative effects.
  • Teams Working on Legacy or Complex Domains: AI has poor training data for niche, old, or proprietary systems.

You can be more permissive for:

  • Frontend UI Boilerplate: Generating React components or CSS.
  • Scripting and One-Off Data Wrangling: Where the cost of failure is low and easily observed.
  • Initial Drafts of Well-Documented APIs: Using AI to scaffold a CRUD endpoint following a clear spec.

The Bottom Line on AI and Code Safety

Linus Torvalds's critique underscores that AI coding assistants are powerful amplifiers of developer intent, not replacements for expertise. The EXT4 bug is a canonical case study: the AI efficiently implemented a syntactic change that violated a semantic rule known only to domain specialists.

The path forward is to integrate these tools into a vetting pipeline, not the creative pipeline. Treat AI output as you would a junior developer's first commit: with specific, skeptical review focused on the assumptions behind the code, not just its appearance.

The industry needs to develop linters and static analyzers trained specifically to detect the characteristic failure modes of LLM-generated code—like missing lock guards or incorrect conditionals. Until then, the final, essential layer of verification remains a human who understands the system.

Get the daily AI digest

One email each morning — what shipped in AI, and the prompts we tested. No filler.

Top comments (0)