版本 v7.1 · 协议 GPL-2.0 WITH Linux-syscall-note · 仓库 torvalds/linux · 在线文档 kernel.org/doc/html/latest
总览:内核贡献与普通开源项目的区别
Linux 内核的贡献流程与大多数 GitHub 开源项目根本不同:
| 维度 | 普通 GitHub 项目 | Linux 内核 |
|---|---|---|
| 提交方式 | Pull Request | 邮件(git send-email 发 patch) |
| 代码托管 | GitHub | git.kernel.org + lore.kernel.org 邮件列表存档 |
| 签署 | 无 / CLA | DCO(Developer Certificate of Origin,Signed-off-by) |
| 审查 | PR review | 邮件列表 review,Acked-by/Reviewed-by 标签 |
| 合入 | maintainer merge | 子系统维护者收 → linux-next → pull request 给 Linus |
内核不接受 GitHub PR——所有 patch 通过邮件列表提交。这是理解整个流程的第一前提。参考 README 的 Quick Start 节:“Join the community: https://lore.kernel.org/”。
本文基于 Linux kernel 仓库内的 README 与 Documentation/process/ 系列权威文档,结合一个真实的 i2c: qcom-cci: fix device_node refcount leak 修复案例(经两轮 review 迭代,最终被 i2c 维护者收进 i2c/i2c-fixes),完整讲解从零到提交、到 review 迭代、到最终合入的全流程。案例技术细节见修复 qcom-CCI 中 i2c_del_adapter 清零 of_node 引发的引用泄漏。
第一步:环境准备与获取源码
1.1 构建依赖
参考 Documentation/process/changes.rst(“Building requirements”)和 Documentation/admin-guide/quickly-build-trimmed-linux.rst。以 Ubuntu/Debian 为例:
sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev
README的 Essential Documentation 节明确要求:“Building requirements: Documentation/process/changes.rst”。
1.2 获取源码
# 完整 clone(不要用 --depth 1,否则无法获取 git tag 历史)
git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
cd linux
Documentation/process/submitting-patches.rst 第 28-44 行(“Obtain a current source tree”)要求:基于当前主线(mainline)开发,不要基于 release tarball。
1.3 git 身份配置(DCO 前置)
内核要求每个 patch 带 Signed-off-by(即签署 DCO)。配置 git 自动加签:
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
参考
Documentation/maintainer/configure-git.rst。git commit -s会据此自动追加Signed-off-by: Your Name <your@email.com>。
第二步:阅读必读文档
README 的 “Who Are You?” 节按角色列出文档入口。作为New Kernel Developer,必读:
| 文档 | 路径 | 内容 |
|---|---|---|
| 开发流程指南 | Documentation/process/development-process.rst |
从 idea 到合入的完整流程 |
| 提交 patch 指南 | Documentation/process/submitting-patches.rst |
patch 格式、commit message、DCO、发送方式 |
| 代码风格 | Documentation/process/coding-style.rst |
强制风格(checkpatch.pl 检查) |
| 构建系统 | Documentation/kbuild/index.rst |
Kbuild/Makefile/Kconfig |
| 开发工具 | Documentation/dev-tools/index.rst |
checkpatch/sparse/coccinelle/smatch |
| 内核 Hacking | Documentation/kernel-hacking/hacking.rst |
内核编程入门 |
| 核心 API | Documentation/core-api/index.rst |
内核核心 API 参考 |
README第 47-58 行原文:“Welcome! Start your kernel development journey here: Getting Started: Documentation/process/development-process.rst, Your First Patch: Documentation/process/submitting-patches.rst …”。
AI 辅助贡献者额外必读 Documentation/process/coding-assistants.rst(见第七步)。
第三步:找一个问题来修
3.1 新手最佳起点:drivers/staging/
drivers/staging/ 是”待完善”代码区,质量门槛低,专门接纳新人 patch。README 的 Hardware Vendor 角色指向 Documentation/driver-api/index.rst。
scripts/checkpatch.pl --no-tree -f drivers/staging/<某文件>
# 修掉每个警告,每个修复合成一个 patch
3.2 贡献类型与难度
| 类型 | 入口 | 难度 |
|---|---|---|
| 修复 checkpatch 警告 | scripts/checkpatch.pl -f 扫 staging |
⭐ |
| 修复编译警告 | make W=1 / sparse / smatch |
⭐ |
| 文档改进 | Documentation/ 下修错别字/补充说明 |
⭐ |
| 修复 bug | bugzilla.kernel.org / 邮件列表报告 | ⭐⭐ |
| 驱动小修 | drivers/ 下熟悉的硬件驱动 |
⭐⭐ |
| 新功能/子系统 | 需先在邮件列表讨论设计 | ⭐⭐⭐⭐ |
3.3 真实案例:定位 i2c-qcom-cci refcount leak
以我们实际定位并提交的一个 bug 为例——通过分析近期 Fixes commit 发现 refcount leak 是高频类别:
git log -2000 --format="%s" | grep -iE "fix|leak|race|null|deref" | \
sed -E 's/^[a-z]+: //' | sort | uniq -c | sort -rn | head
输出显示 refcount leak(引用计数泄漏)频繁出现。进一步用 git log -S(pickaxe)审查 drivers/i2c/busses/i2c-qcom-cci.c 的 of_node 历史,发现 commit 02a4a69667a2(2022 年的”修复”)虽然加了 of_node_get() 和配对的 of_node_put(),但把 of_node_put() 放在了 i2c_del_adapter() 之后——而 i2c_del_adapter() 末尾会 memset(&adap->dev, 0, ...)(i2c-core-base.c:1849,commit bd4bc3dbded9 引入)把 adap->dev.of_node 清零,导致 of_node_put(NULL) 变成 no-op,引用照漏。这是一个隐藏在”已修复”代码里 4 年未被发现的真实 bug。
参考
Documentation/dev-tools/coccinelle.rst、Documentation/dev-tools/sparse.rst——这些静态分析工具能自动检出此类泄漏模式。本案例通过git log -Spickaxe 审查 + 源码链路追踪定位。
第四步:改代码与写 commit message
4.1 在独立分支上开发
不要直接在 master 上 commit——master 跟踪 Linus 的 origin/master,应保持干净:
git checkout -b fix-<subsystem>-<short-description>
# 例:git checkout -b fix-i2c-qcom-cci-ofnode-leak
4.2 commit message 规则
submitting-patches.rst 第 45-200 行(“Describe your changes”)的核心规则:
| 规则 | 要求 | 参考 |
|---|---|---|
| 祈使语气 | "make xyzzy do frotz" 而非 "this patch makes..." |
submitting-patches.rst:94 |
| 一 patch 一问题 | 逻辑变更分离,bug fix 和性能优化不能混 | submitting-patches.rst:168-200 |
| 描述用户可见影响 | crash/锁死要说明触发条件 | submitting-patches.rst:54 |
| 量化优化 | 性能改进要给数据和 trade-off | submitting-patches.rst:64 |
| 修 bug 引用 commit | Fixes: <12位SHA> ("oneline summary") |
submitting-patches.rst:146-151 |
| 链接讨论 | 用 lore.kernel.org 链接 |
submitting-patches.rst:120 |
Fixes: 标签的格式(submitting-patches.rst:151):
Fixes: 54a4f0239f2e ("KVM: MMU: make kvm_mmu_zap_page() return the number of pages it actually freed")
为什么用 Fixes::submitting-patches.rst:606 说明 “A Fixes: tag indicates that the patch fixes a bug in a previous commit.”,它让
git bisect能追踪,并触发 stable kernel 自动回 port(见Documentation/process/stable-kernel-rules.rst)。
4.3 Subject 行格式
submitting-patches.rst:377-393:
[PATCH Vx RESEND] sub/sys: Condensed patch summary
sub/sys:— 子系统前缀(如i2c: qcom-cci:)[PATCH]—git send-email自动加V2/V3— 修订版(首次不加)RESEND— 仅未修改的重发(submitting-patches.rst:380-388 明确:修改后重发不加 RESEND,改加 V2)
4.4 真实案例的 commit
git add drivers/i2c/busses/i2c-qcom-cci.c
git commit -s -F commit-msg.txt
commit message 内容(v1 首发版;v2 的演进见 9.4 节):
i2c: qcom-cci: fix device_node refcount leak in cci_probe()/cci_remove()
cci_probe() calls of_node_get() to take an extra reference on the
child device_node when assigning it to the adapter device. The
matching of_node_put() calls exist in both the error cleanup path
and cci_remove(), but they are placed after i2c_del_adapter().
i2c_del_adapter() clears adap->dev with memset() at the end (commit
bd4bc3dbded9 ("i2c: Clear i2c_adapter.dev on adapter removal")), which
zeroes adap->dev.of_node before of_node_put() runs, turning it into a
no-op. The reference taken by of_node_get() is never released, leaking
the device_node on every cleanup of already-registered adapters and
every adapter removal.
The commit that added of_node_get() and the matching of_node_put()
calls placed the puts after i2c_del_adapter(), so the bug has been
present since the fix was introduced.
Cache the pointer before calling i2c_del_adapter(), the same approach
used in i2c-mux (i2c_mux_del_adapters) and mtd (commit 56570bdad5e3
("mtd: core: Fix refcount error in del_mtd_device()")).
The of_node_put() in the i2c_add_adapter() failure path (before any
i2c_del_adapter() runs) is correct and left unchanged.
Fixes: 02a4a69667a2 ("i2c: qcom-cci: don't put a device tree node before i2c_add_adapter()")
Cc: stable@vger.kernel.org
Assisted-by: Claude:claude-opus-5
Signed-off-by: Liu Zhenlong <dragonliu2018@gmail.com>
关键点:
Fixes: 02a4a69667a2指向引入 bug 的 commit(2022 年那个”修复”放错了 of_node_put 位置),含 12+ 字符 SHA + 标题——这是 submitting-patches 规范要求的格式。Cc: stable@vger.kernel.org请求回 port 到 stable 内核(stable-kernel-rules.rst规定,带Fixes:的 bug fix 可通过此标签请求 stable 回 port)。- 引用其他 commit 用
commit <SHA> ("title")格式(bd4bc3dbded9/56570bdad5e3),这是 checkpatch 强制要求的——裸 SHA 会报 ERROR。- 参照同模式已有做法(
i2c-mux的i2c_mux_del_adapters、mtd的56570bdad5e3),让 reviewer 容易信服。
第五步:Signed-off-by 与 DCO
submitting-patches.rst 第 425-470 行(“Sign your work - the Developer Certificate of Origin”)规定:每个 patch 必须有 Signed-off-by,即签署 DCO——声明你对代码拥有合法贡献权。
Signed-off-by: Random J Developer <random@developer.example.org>
git commit -s 自动追加。DCO 的法律含义(submitting-patches.rst:459):
“The Signed-off-by: tag indicates that the signer was involved in the development of the patch, and has the right to submit it under the applicable license.”
多人协作的标签链
submitting-patches.rst:505-536 规定了 Co-developed-by + From: + Signed-off-by 的排序规则:最后一个 Signed-off-by 必须是提交 patch 的人。
第六步:风格检查与找对维护者
6.1 checkpatch 风格检查
scripts/checkpatch.pl 是强制风格检查器。参考 Documentation/dev-tools/checkpatch.rst:
# 检查生成的 patch 文件(必须无 ERROR/WARNING)
scripts/checkpatch.pl outgoing/0001-*.patch
# 检查单个源文件
scripts/checkpatch.pl -f --no-tree drivers/i2c/busses/i2c-qcom-cci.c
checkpatch.rst 原文:“Checkpatch (scripts/checkpatch.pl) is a perl script which checks for trivial style violations in patches and optionally corrects them.”。注意:“Checkpatch is not always right. Your judgement takes precedence.”——但提交时仍应力求零警告。
真实案例的 checkpatch 陷阱:本案例初版 commit message 引用其他 commit 时用了裸 SHA(如 commit bd4bc3dbded9),checkpatch 报 3 个 ERROR:
ERROR: Please use git commit description style 'commit <12+ chars of sha1> ("<title line>")' - ie: 'commit bd4bc3dbded9 ("i2c: Clear i2c_adapter.dev on adapter removal")'
修法:把所有裸 SHA 引用补全为 commit <SHA> ("title") 格式。修正后 0 errors, 0 warnings。
Documentation/process/submit-checklist.rst 是提交前的完整核对清单,建议逐条过。
6.2 找对维护者(关键!发错人会石沉大海)
scripts/get_maintainer.pl outgoing/0001-*.patch
输出(以 i2c-qcom-cci 为例):
Loic Poulain <loic.poulain@oss.qualcomm.com> (maintainer:QUALCOMM I2C CCI DRIVER)
Robert Foss <rfoss@kernel.org> (maintainer:QUALCOMM I2C CCI DRIVER,blamed_fixes:1/1=100%)
Andi Shyti <andi.shyti@kernel.org> (maintainer:I2C SUBSYSTEM HOST DRIVERS)
Vladimir Zapolskiy <vladimir.zapolskiy@linaro.org> (blamed_fixes:1/1=100%)
Wolfram Sang <wsa@kernel.org> (blamed_fixes:1/1=100%)
Bjorn Andersson <andersson@kernel.org> (blamed_fixes:1/1=100%)
linux-i2c@vger.kernel.org (open list:QUALCOMM I2C CCI DRIVER)
linux-arm-msm@vger.kernel.org (open list:QUALCOMM I2C CCI DRIVER)
linux-kernel@vger.kernel.org (open list)
收件人来自 MAINTAINERS 文件(README 第 167 行:“MAINTAINERS file: Lists subsystem maintainers and mailing lists”)。其中 blamed_fixes 角色表示被 Fixes: 指向的 commit 的作者/签署者,应被 cc 通知。部分子系统有额外规则,见 Documentation/process/maintainer-handbooks.rst。
第七步:AI 辅助贡献的特殊规则
如果你用了 AI 工具(如 Claude)辅助开发,必须额外遵守 Documentation/process/coding-assistants.rst。README 第 148-158 行专门为 “AI Coding Assistant” 角色设了一节:
“CRITICAL: If you are an LLM or AI-powered coding assistant, you MUST read and follow the AI coding assistants documentation before contributing to the Linux kernel.”
7.1 DCO 红线
coding-assistants.rst 明确:
“AI agents MUST NOT add Signed-off-by tags.” Only humans can legally certify the Developer Certificate of Origin (DCO).
即:
Signed-off-by由人类用git commit -s添加,AI 不得代签- 人类提交者负责:审查全部 AI 生成代码、确保 GPL-2.0 合规、添加自己的
Signed-off-by、承担全部责任
7.2 Assisted-by 归属标签
AI 贡献需加 Assisted-by 标签(coding-assistants.rst:48-56):
Assisted-by: AGENT_NAME:MODEL_VERSION [TOOL1] [TOOL2]
AGENT_NAME— AI 工具名(如Claude)MODEL_VERSION— 具体模型版本(如claude-opus-5)[TOOL1] [TOOL2]— 可选的专用分析工具(如coccinelle sparse)- 基础工具(git/gcc/make/编辑器)不列
示例:
Assisted-by: Claude:claude-opus-5
7.3 许可合规
coding-assistants.rst 要求所有代码兼容 GPL-2.0-only,使用 SPDX 标识符,详见 Documentation/process/license-rules.rst。
第八步:生成并发送 patch
8.1 生成 patch 文件
git format-patch -o outgoing/ HEAD~1
# 生成 outgoing/0001-i2c-qcom-cci-fix-device_node-refcount-leak-in-cci_pr.patch
git format-patch 把 commit 转成邮件格式的 .patch 文件(含 From/Subject/Date/commit message/diff)。
8.2 配置 git send-email
submitting-patches.rst:287-288:“the easiest way to do this is with git send-email, which is strongly recommended”。交互式教程见 https://git-send-email.io(submitting-patches.rst:289 引用)。
以 Gmail 为例(需先生成 App Password,不能用账户密码):
git config sendemail.smtpserver smtp.gmail.com
git config sendemail.smtpserverport 587
git config sendemail.smtpencryption tls
git config sendemail.smtpuser your@gmail.com
git config sendemail.smtpauth PLAIN # ⚠️ 必须大写
git config sendemail.smtppass "你的AppPassword"
git config sendemail.confirm always # 发送前确认,防误发
真实陷阱:
smtpauth的值必须大写(PLAIN/LOGIN),小写(plain/login)会报invalid smtp auth错误——git send-email的正则校验/^(\b[A-Z0-9-_]{1,20}\s*)*$/只接受大写。本案例初版用小写login报错,改为大写PLAIN后成功。⚠️
smtppass明文存在.git/config里,发送后建议清理:git config --unset sendemail.smtppass。
submitting-patches.rst:291-302 警告:如果不用
git send-email,需手动把 patch 作为纯文本邮件内联发送,不能用富文本/HTML,且编辑器的 word-wrap 会破坏 patch——这是强烈推荐git send-email的原因。参考Documentation/process/email-clients.rst配置邮件客户端。
8.3 发送
git send-email outgoing/0001-*.patch \
--to loic.poulain@oss.qualcomm.com \
--to rfoss@kernel.org \
--to andi.shyti@kernel.org \
--cc vladimir.zapolskiy@linaro.org \
--cc wsa@kernel.org \
--cc andersson@kernel.org \
--cc linux-i2c@vger.kernel.org \
--cc linux-arm-msm@vger.kernel.org \
--cc linux-kernel@vger.kernel.org
git send-email 自动:
- 在 Subject 加
[PATCH]前缀 - 把 patch 内联到邮件正文(不作为附件)
- 生成正确的邮件头(From/Date/Message-ID)
- 从 commit message 的
Cc:标签自动追加stable@vger.kernel.org到收件人
本案例实际发送结果:SMTP 返回
250 OK,Message-ID<20260815140931.53297-1-dragonliu2018@gmail.com>,patch 成功投递至 linux-i2c 邮件列表。几分钟后可在 lore.kernel.org/linux-i2c 搜到。注意:
git send-email会忽略Assisted-by标签不作为 cc 地址(输出Ignoring Assisted-by),但该标签作为 patch 正文的一部分发送,收件人能看到——这是正常行为。
8.4 发送系列 patch(patch series)
当多个 patch 属于同一主题(如同类 bug 批量修复),应作为系列 patch 一次发送,而非逐个发。系列 patch 带 cover letter,形成邮件线程,便于维护者批量 review 和合入。
何时用系列 vs 单个
| 场景 | 做法 |
|---|---|
| 同一 bug 模式影响多个驱动 | 系列(如 5 个 i2c 驱动的 of_node refcount leak) |
| 根因不同、修法独立 | 单独发(即使主题相关) |
| 一个功能分多步实现 | 系列(patch 1/N 到 N/N 有依赖顺序) |
| 单个 bug fix | 单个 patch |
本案例的实际经验:i2c 子系统有 6 个驱动犯同类 of_node refcount leak,但根因分两类——5 个是”完全漏 of_node_put”,1 个(qcom-CCI)是”of_node_put 放在 i2c_del_adapter 之后被 memset 清零”。根因表述不同的 patch 不混在一个系列里,所以 qcom-CCI 单独发,其余 5 个作为系列。
生成系列 patch + cover letter
# 在特性分支上,生成从 master 到分支顶端的全部 patch
git format-patch -o outgoing/ --cover-letter master..fix-<subsystem>-<desc>
--cover-letter 额外生成 0000-cover-letter.patch,但只生成骨架:
Subject: [PATCH 0/5] *** SUBJECT HERE ***
*** BLURB HERE ***
Liu Zhenlong (5):
i2c: mpc: fix ...
i2c: cpm: fix ...
...
5 files changed, 29 insertions(+), 5 deletions(-)
需要人工填写 *** SUBJECT HERE ***(Subject)和 *** BLURB HERE ***(正文)。git format-patch 自动生成 commit 列表摘要和 diffstat。
cover letter 正文写什么
cover letter 是给维护者的”系列说明”,应包含:
- 统一 bug 模式描述(5 个驱动犯了什么同样的错)
- 根因解释(
of_node_get后缺of_node_put+i2c_del_adapter的memset清零导致 no-op) - 修法说明(probe error path 加
of_node_put+ remove 路径先缓存指针) - 参照已有做法(
i2c-mux、mtd的同模式 commit) - 关联 patch 链接(引用已单独发的 qcom-CCI patch 的 lore URL)
- 测试声明(
Compile-tested with gcc on arm64 defconfig using COMPILE_TEST) - 范围说明(这 5 个是剩余全部有此模式的驱动,其他驱动用安全写法)
Subject: [PATCH 0/5] i2c: fix device_node refcount leaks in 5 bus drivers
This series fixes device_node refcount leaks in 5 i2c bus drivers that
share the same bug pattern:
adap->dev.of_node = of_node_get(pdev->dev.of_node);
Each driver calls of_node_get() ... but none of them drop it in the
probe error paths nor in the remove() callback.
Additionally, in the remove() callbacks, i2c_del_adapter() clears
adap->dev with memset() at the end (commit bd4bc3dbded9) ...
A related fix for the i2c-qcom-cci driver has already been submitted
separately:
https://lore.kernel.org/linux-i2c/20260815140931.53297-1-dragonliu2018@gmail.com/
These 5 drivers are the remaining i2c bus drivers that use of_node_get()
on the adapter device ...
收件人:get_maintainer 合并查
系列 patch 涉及多个子系统,收件人比单个 patch 多。传全部 patch 文件给 get_maintainer,它合并去重:
scripts/get_maintainer.pl --rolestats outgoing/000[1-5]*.patch
⚠️
get_maintainer输出的所有人都应收到,不能主观挑选。本案例初版漏了 4 个blamed_fixes角色(被Fixes:指向的 commit 的作者),后补全到 17 个收件人。blamed_fixes按内核惯例应 cc 通知。
发送系列
git send-email outgoing/*.patch \
--to <维护者1> --to <维护者2> ... \
--cc <reviewer1> --cc <列表1> ...
git send-email 对系列的行为:
- 每个 patch 文件发一封独立邮件(
0000到0005共 6 封) - 5 个 patch 的
In-Reply-To头指向 cover letter 的Message-ID,形成嵌套邮件线程 - Subject 自动编号
[PATCH 0/5]到[PATCH 5/5] - 全部发给相同的收件人
[PATCH 0/5] i2c: fix device_node refcount leaks in 5 bus drivers ← cover letter
├─ [PATCH 1/5] i2c: mpc: fix ... ← In-Reply-To 指向 0/5
├─ [PATCH 2/5] i2c: cpm: fix ...
├─ [PATCH 3/5] i2c: ibm_iic: fix ...
├─ [PATCH 4/5] i2c: opal: fix ...
└─ [PATCH 5/5] i2c: pnx: fix ...
本案例实际发送结果:6 封邮件全部成功投递(SMTP 250 OK),几分钟后在 lore.kernel.org/linux-i2c 搜到完整线程。
confirm: always配置下,发送前会让你逐封确认(或按a全部确认)。
系列发 V2
如果维护者要求修改某个 patch,改后整个系列重发 V2:
# 改代码 + git commit --amend(对应 patch)
git format-patch -o outgoing/ -v2 --cover-letter master..fix-<subsystem>-<desc>
# 生成 outgoing/v2-0000-cover-letter.patch 到 v2-0005-*.patch
# cover letter 里加 changelog 说明 V1→V2 改了什么
git send-email outgoing/v2-*.patch --to ... --cc ...
第九步:Review 与迭代
9.1 等待与礼节
submitting-patches.rst:370-372:“Wait for a minimum of one week before resubmitting or pinging reviewers - possibly longer during busy times like merge windows.”
9.2 Review 标签
| 标签 | 含义 | 参考 |
|---|---|---|
Acked-by |
认可(不如 SoB 正式) | submitting-patches.rst:477 |
Reviewed-by |
审查通过(较正式) | submitting-patches.rst:485 |
Tested-by |
测试过 | submitting-patches.rst:539 |
Suggested-by |
建议者 | submitting-patches.rst:539 |
Reported-by |
报告者 | submitting-patches.rst:539 |
使用规则(submitting-patches.rst:477-635):
Acked-by/Reviewed-by/Tested-by只能由对方本人给出,作者不能代加。Suggested-by/Reported-by/Cc:无需对方许可即可添加(前提:建议/报告在公开场合提出)。- 版本迭代后代码有实质改动时,
Acked-by/Reviewed-by需请对方对新版本重新表态。
9.3 发 V2
改后发 [PATCH v2]——版本间靠 [PATCH v2] 前缀 + changelog + cover letter 里的 lore 链接 关联,不靠邮件线程头:
git commit --amend # fold 修改进原 commit
git format-patch -o outgoing/ -v2 HEAD~1
git send-email outgoing/v2-0001-*.patch --to ... --cc ...
# ⚠️ 不要加 --in-reply-to,V2 是新的顶层线程
submitting-patches.rst:380-388:修改后重发加 V2,不加 RESEND;RESEND 仅用于未修改的重发。
版本线程规则
submitting-patches.rst:837-848:
| 场景 | 能否用 --in-reply-to |
|---|---|
| patch 关联相关讨论(如 bug 报告邮件) | ✅ 正当用途 |
| v2 挂到 v1 的线程 | ❌ 禁止——多版本与 review 交织成 “unmanageable forest of references” |
| 引用旧版本 | 用 lore 链接写在 cover letter / changelog 正文里 |
git send-email 默认就不跨版本串线程——什么都不加即正确,加 --in-reply-to 反而违规。
changelog 写在 --- 之后
版本差异写在 patch 的 --- 分隔线之后、diffstat 之前:这段区域 git am 应用时自动剥掉,不进 commit——它是给 reviewer 看的版本说明,与永久 commit message 各司其职(submitting-patches.rst:806-814)。格式:
---
Changes in v2:
- <第一条改动>
- <第二条改动>
<diffstat>
9.4 迭代经验
- review 建议重构时优先采纳:v1 的”缓存指针”方案被 review 指出脆弱(易被后续”简化”还原),v2 重构为 devm action——把正确性从顺序依赖变成存储隔离。技术上更稳的写法值得推倒重来。
- commit message 按 review 反馈精简:v1 的 5 段正文被批 “too verbose”,v2 压到两段。核心 bug 修复的 message 保住”根因 + 修法”两段即可。
- v2 发送前验证:
git diff HEAD(无未 fold 残留)→ 编译零告警 →checkpatch.pl零错误 →git apply --check(pristine base)→git send-email --dry-run(收件人解析)。
第十步:合入主线
10.1 合入路径
你的邮件 patch
→ 维护者 review + Acked-by/Reviewed-by
→ 维护者收进子系统子树
→ 进入 linux-next 测试树(集成测试)
→ 维护者发 pull request 给 Linus
→ Linus 合入主线(merge window 期间)
参考 Documentation/maintainer/pull-requests.rst(维护者视角的 pull request 格式)和 Documentation/process/development-process.rst(完整开发周期)。
本案例的实际落地:v1 → review → v2 → 2026-09-16 维护者回复 “pushed to i2c/i2c-fixes”——进入子系统 fixes 分支,随 fixes PR 发给 Linus,2026-09-20 进 master(commit 7362a1553eb0),首发 v7.3-rc4,Cc: stable 标签随后触发 stable 回 port(# v5.17+)。全程约一个月,无需催促。
10.2 时间周期
- merge window:约 2 周(Linus 接受新特性)
- rc 阶段:约 6-7 周(只接受 bug fix)
- 整个周期约 2-3 个月
10.3 Stable 回 port
带 Fixes: 标签的 bug fix 在合入主线后,可能被自动回 port 到 stable 内核(如 7.1.x)。规则见 Documentation/process/stable-kernel-rules.rst。
完整流程速查
单个 patch
# 1. 环境准备
git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
cd linux
git config user.name "Your Name" && git config user.email "your@email.com"
# 2. 开分支
git checkout -b fix-<subsystem>-<desc>
# 3. 改代码
# ... 编辑源码 ...
# 4. commit(-s 加 Signed-off-by)
git add <改动的文件> # 只 add 目标文件,不要 git add -A
git commit -s -F commit-msg.txt
# 5. 生成 patch
git format-patch -o outgoing/ HEAD~1
# 6. 风格检查(必须 0 errors 0 warnings)
scripts/checkpatch.pl outgoing/0001-*.patch
# 7. 找收件人
scripts/get_maintainer.pl outgoing/0001-*.patch
# 8. 发送
git send-email outgoing/0001-*.patch --to <维护者> --cc <邮件列表>
# 9. 等 review → 改 → 发 V2(新顶层线程,绝不 --in-reply-to 挂 v1)
git commit --amend
git format-patch -o outgoing/ -v2 HEAD~1
# changelog(Changes in v2: ...)写在 patch 的 --- 之后、diffstat 之前
git send-email outgoing/v2-0001-*.patch --to ... --cc ... # 不加 --in-reply-to
系列 patch(多个同主题 patch)
# 1-3. 同上(环境/分支/改代码,每个 patch 一个 commit)
# 4. 每个改动单独 commit(-s)
git add <文件1> && git commit -s -F msg1.txt
git add <文件2> && git commit -s -F msg2.txt
# ...
# 5. 生成系列 patch + cover letter 骨架
git format-patch -o outgoing/ --cover-letter master..fix-<subsystem>-<desc>
# 生成 0000-cover-letter.patch(骨架)+ 0001~000N-*.patch
# 6. 编辑 cover letter(填 Subject 和正文)
# ... 编辑 outgoing/0000-cover-letter.patch ...
# 7. 风格检查全部 patch
scripts/checkpatch.pl outgoing/000[1-N]*.patch
# 8. 找收件人(传全部 patch,合并查 MAINTAINERS)
scripts/get_maintainer.pl --rolestats outgoing/000[1-N]*.patch
# 9. 发送(含 cover letter,形成邮件线程)
git send-email outgoing/*.patch --to <维护者...> --cc <列表...>
# 10. 等 review → 改 → 发 V2(整个系列重发)
git format-patch -o outgoing/ -v2 --cover-letter master..fix-<subsystem>-<desc>
git send-email outgoing/v2-*.patch --to ... --cc ...
参考文档索引
以下是本文引用的全部 Linux kernel 仓库内文档(路径相对仓库根,在线版可在 kernel.org/doc/html/latest 浏览):
| 文档 | 路径 | 用途 |
|---|---|---|
| README | README |
角色导航 + Quick Start |
| 开发流程 | Documentation/process/development-process.rst |
完整开发周期 |
| 提交指南 | Documentation/process/submitting-patches.rst |
patch 格式/DCO/发送 |
| 提交清单 | Documentation/process/submit-checklist.rst |
提交前核对 |
| 代码风格 | Documentation/process/coding-style.rst |
强制风格 |
| 构建要求 | Documentation/process/changes.rst |
工具链版本 |
| 行为准则 | Documentation/process/code-of-conduct.rst |
社区行为 |
| AI 贡献规则 | Documentation/process/coding-assistants.rst |
AI 辅助开发合规 |
| 安全 bug | Documentation/process/security-bugs.rst |
漏洞报告流程 |
| 许可规则 | Documentation/process/license-rules.rst |
SPDX 标识 |
| Stable 规则 | Documentation/process/stable-kernel-rules.rst |
stable 回 port |
| 邮件客户端 | Documentation/process/email-clients.rst |
配置邮件工具 |
| 维护者手册 | Documentation/process/maintainer-handbooks.rst |
子系统额外规则 |
| 快速构建 | Documentation/admin-guide/quickly-build-trimmed-linux.rst |
精简内核构建 |
| 报告 bug | Documentation/admin-guide/reporting-issues.rst |
bug 报告流程 |
| checkpatch | Documentation/dev-tools/checkpatch.rst |
风格检查工具 |
| coccinelle | Documentation/dev-tools/coccinelle.rst |
模式匹配分析 |
| sparse | Documentation/dev-tools/sparse.rst |
静态分析 |
| Kbuild | Documentation/kbuild/index.rst |
构建系统 |
| 开发工具 | Documentation/dev-tools/index.rst |
工具总览 |
| Hacking 指南 | Documentation/kernel-hacking/hacking.rst |
内核编程入门 |
| 核心 API | Documentation/core-api/index.rst |
API 参考 |
| 维护者指南 | Documentation/maintainer/index.rst |
维护者操作 |
| Pull Request | Documentation/maintainer/pull-requests.rst |
PR 格式 |
| git 配置 | Documentation/maintainer/configure-git.rst |
维护者 git 设置 |
| 驱动 API | Documentation/driver-api/index.rst |
驱动开发 |
| 维护者清单 | MAINTAINERS |
子系统维护者+邮件列表 |
| 报告问题 | Documentation/admin-guide/reporting-issues.rst |
用户报 bug |
外部资源:
- lore.kernel.org — 邮件列表存档与搜索
- git-send-email.io —
git send-email交互式教程(submitting-patches.rst:289 引用) - kernel.org — 最新内核源码
- bugzilla.kernel.org — bug 跟踪
- #kernelnewbies @ irc.oftc.net — 新手 IRC(README:165)