Guided Generation of Linux Kernel Semantic Patches with Small Local Language Models

Mohanned AhmedarXiv 2026 · arXiv

TL;DR

Researchers have developed a framework that allows small, 7B-parameter language models running on a standard laptop to generate complex Linux kernel "semantic patches" (Coccinelle rules). By decomposing the task into smaller variants and using a "compile critic" for feedback, the Qwen2.5-Coder 7B model improved from solving just 1.0 conversion to 35.3 on average, achieving higher precision than even frontier models like Claude.

What Problem Does This Paper Address?

When a core Linux kernel API changes, hundreds of drivers must be updated to match. This "collateral evolution" is a massive manual effort that can span months. Kernel maintainers often use Coccinelle, a tool that applies "semantic patches" (written in a language called SmPL) to automate these changes across the entire tree.

However, writing SmPL rules is difficult and error-prone. While large "frontier" LLMs (like GPT-4 or Claude) can write them, they pose privacy risks, are expensive, and—crucially—may have already seen the specific migrations in their training data, leading to "cheating" in evaluations. This paper investigates whether small, open-source models (4–8B parameters) can do the job locally, provided they are given the right structural guidance.

Methods

The authors adapted a framework called Guided Code Generation (GCG) to help small models handle the complexities of SmPL. The process follows four distinct stages:

  1. Decomposition (Variant Tree): Instead of asking the model to write one giant rule, a script breaks the training examples into "hunks" of code. These are grouped into "leaf tasks" based on the specific API being changed or recurring code patterns.
  2. Leaf Rules with Feedback: The model writes a small rule for each leaf task. It receives a short SmPL primer and specific examples. If the rule fails to parse or produces the wrong output, the framework provides feedback (compiler errors or diffs) for up to three rounds of refinement.
  3. Greedy Composition: The individual leaf rules are combined into a single patch. The framework ensures that rules don't collide and only keeps those that improve the overall "score" (more correct hits, fewer extra edits).
  4. Compile Critic: The final patch is applied to a real Linux kernel tree and compiled. If it fails to build, the model is asked to repair it. If it still fails, the framework abstains rather than providing a broken patch.
Fig. 1. The four stages. Shaded boxes are deterministic scripts; blue boxes call the local model. Only the training siblings are read; held-out targets are never seen.

The framework also includes deterministic "auto-repair" scripts to fix common mechanical SmPL syntax errors that often trip up smaller models.

Datasets

The study uses two main sets of API migrations:

  • Development Set: 5 migrations from Linux v5.10–v6.10 used to tune the framework.
  • Test Set (Pre-registered): 7 recent migrations where the new API appeared after the training cutoffs of all tested models (post-September 2024). This ensures the models are truly generalizing rather than memorizing.
Table I: Details of the development and test sets, including the number of training "siblings" and target conversions.

Key Insights and Ideas

  • Decomposition is Essential: Small models are "one-shot" failures for this task. Without decomposition, Qwen2.5-Coder solved almost nothing. Breaking the problem into variants was the single biggest driver of success.
  • Precision Over Recall: The "Compile Critic" didn't necessarily increase the number of solved cases, but it significantly boosted precision. For a kernel maintainer, a tool that does nothing is better than a tool that introduces a subtle compilation error.
  • Domain Specialization Matters: The code-specific model (Qwen2.5-Coder) vastly outperformed general-purpose models like Llama 3.1 and Gemma 3, which struggled even to write syntactically valid SmPL.
  • Deterministic Guardrails: Simple scripts to fix missing "@@" markers or undeclared variables in SmPL were vital. They allowed the model to focus on the logic of the code change rather than the pedantry of the DSL syntax.

Key Results

The framework transformed the local 7B model from a toy into a competitive tool:

  • Qwen2.5-Coder 7B (Full Framework): Solved 35.3 out of 122 conversions on the test set, compared to just 1.0 when prompted directly (one-shot).
  • Precision: Qwen achieved a precision of 0.74, outperforming the specialized tool Spinfer (0.55) and even the Frontier LLM agent (0.70).
  • Comparison: While the Frontier LLM (Claude) solved more total conversions (66), the local model closed a significant portion of the gap and provided more "auditable" and precise results.
Table II: Comparison of solved conversions across different models and baselines.

Limitations and Future Work

  • Coupled Changes: The framework fails when a change spans a declaration and its uses (e.g., changing a callback's return type). These require multiple rules to "agree" on a function name across a file, which the current interface struggles to coordinate.
  • Semantic Blind Spots: While the compile critic catches syntax and type errors, it doesn't catch logical errors that still compile, such as using sizeof() on a pointer instead of an array.
  • Future Directions: The authors suggest developing "framework-written skeletons" to handle shared metavariables and adding semantic checks beyond simple compilation to catch logic bugs.

Summary updated

Abstract

When a Linux kernel API changes, hundreds of drivers are converted by hand over months, and the earlier conversions are reviewed examples for the later ones. We ask whether small open language models running on a laptop can turn such examples into Coccinelle semantic patches, the rules kernel maintainers already use for tree-wide changes. We adapt guided code generation to this task: the training examples are decomposed by script into a tree of change variants, the model writes one small rule per variant under feedback from applying it to the examples, deterministic components repair SmPL syntax and generalise driver-specific names, rules are composed greedily, and a compile critic repairs or rejects a rule whose output does not build. We evaluate on a pre-registered test set of 7 migrations whose new APIs appeared after the training cutoffs of all three models (Qwen2.5-Coder 7B, Llama 3.1 8B, Gemma 3 4B), with 122 held-out driver conversions, counting a conversion as solved only if the rule's output matches the human fix and compiles. With the framework, Qwen2.5-Coder solves 35.3 conversions on average over three seeds (range 32-41), against 1.0 when prompted directly; Spinfer solves 41 and a frontier LLM agent 66. The local model solves fewer conversions than Spinfer but its rules are the most precise on the test set (0.74 of the files they change are solved, against 0.55 for Spinfer and 0.70 for the frontier agent), and the compile critic removes every output that matches the human fix but does not build. The two general-purpose models stay weak even with guidance (at most 12 solved). All code, prompts, model responses and results are released.
BibTeX
@misc{ahmed2026guided,
  author = {Mohanned Ahmed},
  title = {{Guided Generation of Linux Kernel Semantic Patches with Small Local Language Models}},
  year = {2026},
  eprint = {2611.16215},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.16215},
}
Figure