Linux 内核补丁与 C 代码审查
lint-kernel
应用 Linux 内核补丁与 C 代码审查规范。
- License 许可
- MIT
- 最近更新
- 2026-11-13
- Stars
- ★ 65
- Forks
- 0
01 前言 Front Matter
- name
- lint-kernel
- description
- Apply Linux kernel patch and C review discipline.
02 资源文件 6
- 参考文档references/backport-review.md7.0 KB
- 参考文档references/email-plumbing.md1.4 KB
- 参考文档references/finding-discipline.md4.8 KB
- 参考文档references/semcode.md7.1 KB
- 参考文档references/submission-policy.md4.3 KB
- 参考文档references/tool-invocation.md8.0 KB
03 SKILL.md 正文
Objective
Lint and prepare Linux kernel C patches, in-tree or out-of-tree modules, to the standard of Documentation/process/submitting-patches.rst; read it. This skill is the lint gate; hand the commit and distribution steps to ship-commit. If a constraint below cannot be met, stop and name the blocker.
Scope
Apply when any of these hold:
- Top-level
MAINTAINERS,Kbuild, andDocumentation/process/submitting-patches.rstexist. scripts/checkpatch.plandscripts/get_maintainer.plexist.- The top-level
MakefiledeclaresVERSION =,PATCHLEVEL =, andSUBLEVEL =. - Sources include
<linux/...>headers and use kernel idioms (EXPORT_SYMBOL,module_init,MODULE_LICENSE).
Otherwise say no kernel lint applies and use the repository's own C tooling.
Procedure
-
List the touched
.c,.h,Kconfig, andMakefilefiles. For an existing series, checkoutgoing/*.patchinstead of the working tree. -
Select the checks this patch needs (a documentation-only change needs no smatch) and record one state per check:
PASS(ran, found nothing),FAIL(ran, found a defect),SKIP(does not apply; say why), orMISS(applies but could not run: tool absent, no built tree, no hardware; say why). AMISSnever counts as a pass or toward an "N/N" score, and it blocks only the claim its check supports: missing sparse blocks "no new type errors", not "checkpatch is clean". The patch states testing in one line; otherMISSentries stay in your report. A runtime result binds only the patch ID it ran; rerun it after any code change.# Style gate: --strict for new code; add --no-tree for a lone .patch outside a kernel tree. scripts/checkpatch.pl --strict --codespell <file-or-patch> # Sparse. C=2 checks every source; C=1 checks only recompiled files, often none on a warm tree. make C=2 W=1 <touched-subdir>/ # Smatch (null derefs, lock imbalance). Its cross-function database is optional and only widens cross-call reach. make CHECK=smatch C=2 W=1 <touched-subdir>/ # Coccinelle semantic patches for kernel anti-patterns. make coccicheck MODE=report COCCI=scripts/coccinelle/<area>/<rule>.cocci # kernel-doc warnings, when kernel-doc comments change. Capture first so make's status survives the filter. make htmldocs > /tmp/htmldocs.log 2>&1; rc=$? grep -E 'warning|error' /tmp/htmldocs.log; exit $rcReport how many files each analyzer processed; a pass over zero files is
MISS. Most of these tools exit 0 with findings; readreferences/tool-invocation.mdbefore trusting a status. An indexed navigator (references/semcode.md) is optional help for repeated lookups in a large tree, never a prerequisite and never a substitute for reading the source. -
Check every commit in the series against "Commit Message" below. When a review corrects one mail, check every unsent mail for the same defect. Draft the message but never add
Signed-off-by:or run the sign-off: only the human author certifies the DCO, so hand over the exactgit commit -s -m "subsystem: imperative subject" -m "Body.". -
Prepare recipients locally:
# Against the review base you established (often a subsystem tree; origin/master is not universal). git format-patch -M --base=<review-base> --cover-letter -o outgoing/ <review-base>.. # Keep the defaults: git fallback adds history-derived reviewers that MAINTAINERS alone does not list. scripts/get_maintainer.pl --no-rolestats outgoing/*.patch # MAINTAINERS-only inspection, never for deriving recipients: # scripts/get_maintainer.pl --nogit --no-git-fallback outgoing/*.patch echo "Proposed: git send-email --to=<maintainer> --cc=<list> outgoing/*.patch"Never pipe recipients into
git send-emailor run it: print the plan and stop. SMTP traffic needs explicit user authorization, and the user runs the send. A root.get_maintainer.confis prepended to the arguments, so explicit flags win; read it before trusting output whose flags you did not fully set. Splice notes below---into the exported files; never hand-build a mail. Compare recipient runs underPERL_HASH_SEED=0 PERL_PERTURB_KEYS=0; unseeded runs break history ties at random.K:patterns match all text before the diff, so rewording can change recipients. -
When the fix may reach stable, answer six questions separately: affected range, target branches, prerequisite investigation, application, build, behavioral evidence. History searches (
git log -S/-G,git describe --contains) give leads to read, not verdicts; they misreport renames, reverts, and substring matches. An affected branch that needs an adapted patch or a prerequisite is not an excluded branch. Procedure:references/backport-review.md. -
Before
git format-patchfor a public list:- Rerun
qa-tracewith the error path, not the happy path, as the entry point. - Have a fresh agent or reviewer review the patch without the earlier context; if the design survives only with the backstory, it is not ready.
- For each
(input × concurrent-op)quadrant no test reaches, state in your report why and how you verified it by inspection. - If an earlier version went public, write the vN changelog as a real diff of the design.
- Rerun
-
Read a subsystem CI reply (patchwork,
bluez.test.bot) per check. A crash inside the userspace test harness, such as a sanitizer report from BlueZsrc/shared/, is not a kernel finding, but every test after it never ran against the patch: check whether unrelated series fail the same way, then rerun the full suite locally on a harness revision without the breakage before calling those tests passed. -
Before reporting, apply
references/finding-discipline.md: each issue needs concrete evidence on at least one execution path, style points are capped and held to a higher bar than correctness, and you never assert authorship or comment on a disclosure trailer. Report in the Output format.
Never push to a branch named master, main, linus, or next.
Checklist
Style
Fix every checkpatch CHECK, WARNING, and ERROR.
- Tabs for indentation, 8 columns wide; lines at most 100 columns, without awkward splits of one logical statement.
- K&R braces: the opening brace on the
if/while/for/doline, on its own line for function definitions. /* … */comments (no//outsidetools/); kernel-doc on exported symbols.- No trailing whitespace, DOS line endings, or smart quotes.
Correctness
- No mixed pointer/integer arithmetic without an explicit cast and reason.
- Keep
__user,__kernel,__iomem, and__rcuon every pointer that needs them; act on sparse's warnings. - Use endianness types (
__le32,__be64) consistently, with no implicitcpu_to_*round-trips. - No sleeping in atomic context against the
gfp_tflags (smatch flags this). - Release every lock on every exit path; no double lock; document lock ordering.
- Balance
rcu_read_lock/rcu_read_unlock; free an RCU-protected pointer only withkfree_rcuor aftersynchronize_rcu. - Use
WARN_ON_ONCEplus an error return, notBUG_ON, for runtime-recoverable conditions.
Memory and API
- Pair
kmalloc/kzalloc/vmallocwith the matching free, check forNULL, and unwind through a cleanup pattern orgoto err_*ladder with no silent leak path. - Use
strscpy/scnprintf, neverstrcpy/sprintf. - Cross the user/kernel boundary with
copy_from_user/copy_to_userand check the return; no rawmemcpy. - Choose DMA mapping by lifetime:
dma_alloc_coherent/dma_poolfor long-lived structures the CPU and device touch in parallel (ring descriptors, mailboxes);dma_map_single/dma_map_sgwith the matchingdma_unmap_*for one-shot transfers. Coherent memory can be expensive and page-granular, so it is not the default. - Never DMA to or from stack, static or module-image, vmalloc, or kmap addresses: they may map differently from the rest of physical memory, and unaligned buffers corrupt shared cachelines on DMA-incoherent CPUs.
dma_map_singlebuffers come from a physically contiguous source (kmalloc or the page allocator). - Give every
printka level (pr_err,pr_warn,dev_err); rate-limit repeated noise with*_ratelimited.
Build and Modules
Kconfigentries havehelptext and a depends-on chain that matches the code; noselectof unselectable symbols.Makefileusesobj-$(CONFIG_FOO); build both built-in and as a module whenCONFIG_FOO=mis meaningful.- New modules declare
MODULE_LICENSE,MODULE_AUTHOR, andMODULE_DESCRIPTION. - No new
EXPORT_SYMBOLwithout justification; preferEXPORT_SYMBOL_GPL.
Concurrency
- A patch touching locking, IRQ context, RCU, atomics, or scheduler hooks needs a
qa-tracepass before commit (ship-commit"Review Once"). - Each added cancel, disable, or barrier needs a producer the caller or core does not already exclude; otherwise drop it and its race claim.
- Delete a flag or guard that existing state already implies; prove it through every caller's locking.
- Check any locking claim, yours or a reviewer's, against the target tree's version; lock types change across releases.
- Mark accesses per
tools/memory-model/Documentation/access-marking.txt, a four-way choice:- Plain C when a held lock or seqlock excludes the access, for init-before-publication or cleanup, for CPU- or task-private data, or with no concurrent store; marking these hides real bugs from KCSAN.
READ_ONCE/WRITE_ONCEwhen the race is intentional and the value is load-bearing for the concurrency design.data_race()(or__data_racy) when the race is intentional and the value is diagnostic, re-validated against a marked reload, or feeds an error-tolerant heuristic;data_race(READ_ONCE(x))when both apply.
- Comment each marking's reason and document the design with
ASSERT_EXCLUSIVE_WRITER/ASSERT_EXCLUSIVE_ACCESS. KCSAN is an input, not the arbiter: its silence is not correctness, and churning markings to quiet it does not help. - Order with
smp_*barriers, nevervolatile.
Error Paths and Cleanup
When a patch adds or changes out_*: labels, goto err_*, or if (failed) cleanup(...), run every check below; an "undo" helper is not automatically the inverse of the step it undoes.
- Read the cleanup helper end to end and list every side effect: locks taken and released, counters, list mutations, conditional state changes, the return value's meaning. Never infer them from its name (a
*_put_pageshelper may also restore reservations, drop a refcount, or free the object). - Search the file for
out_,goto err_,unwind, and the struct's other failure paths, and mirror the existing pattern instead of inventing one. - Enumerate the cross-product of every input that selects a path (e.g.
gbl_chg ∈ {0, >0}×map_chg ∈ {0, 1}×max_hpages ∈ {-1, finite}). Every cell needs a documented cleanup; "shouldn't happen" or "covered by the other branch" is a bug. - For each cell, list the concurrent operations on the same struct that can race with the failure (
*_put_pages,*_unreserve_pages, free to buddy, cgroup migration) and walk the cleanup as if each fires between the failed step and the unwind. It must stay correct and create no phantom state, such as incrementing a reserve count after a concurrent free drained the pool. - When one local pointer would span two charges (regular vs reserved, vmalloc vs kmalloc, DMA vs CPU), use separate locals per ownership domain; migration, preemption, or an interrupt between the charges can rebind ownership.
- Keep severity attached to the bug: if v1 was wrong, v2 has the same severity until proven otherwise.
- Do not bundle unrelated fixes, but bundle fixes that share the change's charge, failure, or commit window (the same
out_uncharge_*ladder); a fix in another function ships separately.
Commit Message
Follow Documentation/process/submitting-patches.rst and 5.Posting.rst.
Subject:
subsystem: imperative summaryof at most 70-75 characters, saying what changes and why; no period, no Conventional Commits prefix (feat:,fix:,chore:), no filename.- Give each patch in a series a unique summary; it becomes a global identifier.
- The version tag counts public sends: after a public v1 and v2, the next public revision is v3, even if an internal v2 was never sent.
Body:
- Lead with the user-visible impact: the affected workload or configuration and the observable failure, then the mechanism; for a fix, say why downstream users need the backport and how they recognize the problem.
- Imperative mood; no "this patch", "I"/"we", or past tense describing the change.
- Name the underlying invariant (e.g. "subpool used_hpages accounting"), not one triggering symptom.
- Wrap at 75 columns; never wrap or split a tag (
Fixes:,Closes:,Link:). - Make it self-contained for a reviewer who never saw earlier versions; one problem per patch, and past about three short paragraphs consider splitting.
- Cite commits as
12-hex ("oneline summary"), never a bare SHA. - No pointers to future RFCs; those belong in cover letters or follow-up threads.
- When a tool helped, end the body with "AI assistance was used." Never list what the tool did.
- Net-tree fixes also state discovery and testing in the body (
maintainer-netdev.rst, "Bug fixes"), e.g. "Found by code review and not reproduced; compile-tested only."
Trailers, in this order with no blank lines between:
Fixes: <12-hex> ("oneline")for bug fixes. Verify the SHA exists and is reachable from the base you actually target (often a subsystem tree;origin/masteris not universal). Non-ancestry is a lead (unfetched tree, rebased or cherry-picked history), not proof the tag is wrong; reportMISSwithout a tree to check.Reported-by:,Closes:,Link:only with real attribution; prefer lore.kernel.org URLs forLink:.Cc: <stable@vger.kernel.org> # 5.14.xonly when a backport is intended. AFixes:naming a commit in a released kernel needs that decision made explicitly: without theCc:, Greg's patch bot asks for a resend, and older releases get the fix only if AUTOSEL or the stable team's scripts select it; to keep it out of stable, addCc: <stable+noautosel@kernel.org> # reason, where the reason is mandatory. That reason says why no stable tree should take the fix (hardening, no reachable bug); a dependency or a conflict is never that reason. The single version already means "and newer", so a trailing+is redundant, and an absent annotation is not a defect whenFixes:implies the range. Give each prerequisite its own line,Cc: <stable@vger.kernel.org> # 5.7.x: <12-hex>: <subject>, after checking that the fix needs it to build or work, not just to apply; when a subsystem's gitlint caps line length (Bluetooth CI: 80 columns), shorten the subject, never the SHA. An in-series prerequisite already tagged for stable needs no line. When the prerequisites are refactors or features that stable would not take, write# needs adjustments for <= X.Yinstead of listing them. For an unpublished vulnerability usestable@kernel.org, after readingreferences/submission-policy.md: it protects one channel and does not make the mail private.Reviewed-by:,Tested-by:,Acked-by:only when real; never invent one.- No
Assisted-by:; mainline docs dropped it (references/submission-policy.md). Signed-off-by:is mandatory (DCO 1.1,git commit -s) and certified by a human; an agent never adds it.
Trailers live in the commit object, so correcting one rewrites every later hash and invalidates a prepared series: report trailer problems, never rewrite them silently.
Put Changes in vN: below ---, between Signed-off-by: and the diffstat, never in the commit body: a brief bullet list of the previous-to-current delta that omits designs never sent publicly.
Below ---, keep only what a maintainer acts on: test status, runtime results, base or dependency facts. No defensive caveats.
Inside the patch, place one short comment per branch where it explains the local action rather than one long composed comment above a multi-branch if; when a comment makes the reader track several counters and locks at once, split it or let the code structure carry the explanation.
Email: plain text through git send-email, a new top-level thread per revision, shallow threading within a series, and trimmed inline replies; checklist in references/email-plumbing.md.
Output
1. Understanding
Subsystem: [drivers/foo, mm/slub, fs/ext4, …]
Patch intent: [one sentence]
Series size: [N patches]; cover letter: [yes / no]
2. Toolchain
checkpatch.pl --strict: [PASS | FAIL: N CHECK / M WARNING / K ERROR | SKIP: reason | MISS: reason]
make C=2 W=1 (sparse): [PASS (f files) | FAIL: warnings | MISS: no built tree]
make CHECK=smatch C=2: [PASS (f files) | FAIL: findings | MISS: no built tree]
smatch coverage: [cross-function DB built? if not, inter-procedural findings are out of scope; a run limitation, not a MISS]
coccicheck: [PASS | FAIL: findings | MISS: ocaml unavailable]
htmldocs (if touched): [PASS | FAIL: kernel-doc warnings | SKIP: no kernel-doc touched]
Not run: [every MISS with its reason]
Claims blocked by the above: [conclusions left unsupported]
3. Subject and Trailers
Subject: [subsystem: imperative short description]
Sign-off: [Signed-off-by present? yes/no; human-certified]
Fixes: [hex: subject; present? yes/no/n-a; ancestor check PASS/MISS]
Cc stable: [yes # 6.1.x / noautosel # reason / no / n-a]
Disclosure: ["AI assistance was used." in body / n-a]
Other tags: [Reported-by, Reviewed-by, Tested-by, Acked-by; real ones only]
Trailer corrections needed: [report only; never rewrite]
4. Distribution Plan (local only, no network)
Patches: outgoing/0001-…patch outgoing/0002-…patch …
Cover: outgoing/0000-cover-letter.patch
Recipients (proposed by get_maintainer.pl):
--to: [maintainer1] [maintainer2]
--cc: [list1] [list2] linux-kernel@vger.kernel.org
Send command (not executed; awaiting explicit user authorization):
git send-email --to=<…> --cc=<…> outgoing/*.patch
5. Blocking Issues (if any)
- [BLOCKER] checkpatch ERROR at <file>:<line> — <issue>
- [BLOCKER] sparse: __user pointer dereferenced without copy_from_user at <file>:<line>
- [MUST-FIX] smatch: lock 'foo' acquired in <fn>, not released on error path <line>
04 同仓库更多 Skills
工作流运行时验证
workflow-runtime
在将 CLI、扩展、提供程序或模型分配给工作流角色之前,先验证技能或工作流的发现能力、完全自主的工具执行能力,以及任务形态的具体化。
工作流成果审查
workflow-reviewer
依据 BOSS 规格说明和证据,审查批量实现的输出。
工作流故障修复
workflow-rescue
通过一次果断的行动解决反复出现的工作流失败。
多智能体工作流编排
workflow-orchestrator
运行风险自适应的开发、评审、多智能体和发布工作流,具备运行时预检、有限重试、存活检查、写入隔离以及有证据支撑的交接能力。
工作流失败仲裁
workflow-judger
仲裁反复出现的任务失败,并决定是合并、返工还是升级处理。
工作流状态验证
workflow-doctor
在任何角色对活动工作流状态执行操作之前,先对其进行验证。