---
tags: linux kernel
---
# 第一次發 patch 到 [LKML](https://lkml.org/)
contributed by < [Risheng1128](https://github.com/Risheng1128) >
## 前言
這次發 patch 的機會主要是在修 jserv 老師的 Linux 核心實作課程,寫 [quiz3](https://hackmd.io/@Risheng/linux2022-quiz3) 的作業時,我有對第一題的巨集 `GENMASK` 做一些的分析,也從老師得知在 kernel 裡其實有很多同名且功能相似甚至相同的函式或巨集,因此老師鼓勵我可以試著發送 patch 看看,我就想說來既然機會來了就嘗試看看,也就有了這次的提交,提交紀錄在[這裡](https://lkml.org/lkml/2022/6/10/933)
## 準備提交 Patch 的 Email
git 有提供一個命令 `git send-email` 來做到發出信件的功能,或是也有很多人會使用 mutt 來幫忙發 plain text mail。我自己是 send patches 時會使用 git send-email,如果只是回覆 comment 之類就會用 mutt
### 安裝 `git send-email`
參考 [How to configure and use git send-email with gmail](https://stackoverflow.com/questions/68238912/how-to-configure-and-use-git-send-email-to-work-with-gmail-to-email-patches-to) 使用以下命令安裝 `git send-email`
```shell
$ sudo apt update
$ sudo apt install git-email
```
接著設定 smtp 的資料,輸入命令 `vi ~/.gitconfig` ,並且在最後加上以下的資料
```shell
[sendemail]
smtpencryption=tls
smtpserver=smtp.gmail.com
smtpuser=<你的信箱@xxxx.com>
smtpserverport=587
smtpPass=<你的APP密碼>
```
接著開始設定上述提到的 APP 密碼,這裡使用 gmail 做設定,可以參考 [Sign in with App Passwords](https://support.google.com/mail/answer/185833?hl=en)
1. 到 [google 帳戶安全性](https://myaccount.google.com/security?rapt=AEjHL4Ma9vPuhKss9SEbsfcQQAYyuW0pwsA_mMKLb2w6XyJJucvD-5MgBKAW5_O8KOD4tnetMuUtSxfGiGvzFqhnkC-wnUkwpA) 的「登入 Google」點選兩步驟驗證,並且完成設定
2. 「開啟兩步驟驗證」的下方有一個「應用程式密碼」,點進去後選擇「郵件」跟「其他」,名字就取一個自己會認得的就可以,完成後會顯示密碼,複製貼到 `.gitconfig` 或是 `.muttrc` 就可以了
3. 目前的 APP 密碼: `ynphwrmqsqwgtfpu`
### 安裝 `mutt`
## 在本地端建立一個 Repo
我是直接 clone 最新的 Linux kernel 使用命令 `git clone https://github.com/torvalds/linux.git`
接著輸入以下命令設定 git
```shell
$ cd linux
$ git init
$ git add .
$ git commit -m ""
$ git checkout -b my-patch
```
做到這裡後,就可以開始修改程式啦!
## 新增 Commit Messgae
完成自己的修改後就可以新增 commit:
```shell
$ git commit -asv
```
> `-a`:加入剛剛修改並且有 track 的檔案
> `-s`:commit 會加上 Signed-off-by: <你的名字> <你的信箱@XXX.com>
> `-v`:verbose,會列出你的修改,看著寫 commit message 會比較方便
## commit message 格式
Patch 的格式都有嚴格的限制,簡單來說就是會分成:標題、內容描述、自己的簽名,中間都會空一行:
```shell
subsystemName: short description
# blank line
patch content description
# blank line
Signed-off-by: "name" <email>
```
接著可以開始輸入 commit 訊息,以下為這次修改的訊息
```shell
tools/power/x86/intel-speed-select: Remove duplicate macro
There are some macros such as `GENMASK` and `GENMASK_ULL` are redefined in
`include/linux/bits.h`. Simultaneously, the `GENMASK` in
`include/linux/bits.h` is more secure and prevents the following situation.
1. [net: stmmac: Fix misuses of GENMASK macro](https://reurl.cc/loMWvl)
2. [clocksource/drivers/npcm: Fix misuse of GENMASK macro](
https://reurl.cc/b2yr96)
Therefore, I think these macro could be removed.
Signed-off-by: Risheng1128 <hi4u29ck02@gmail.com>
```
## 建立 patch
完成 commit 就可以建立等等要寄出去的 patch ,使用命令 `git format-patch` ,在建立 patch 時可以先使用 `git log` 找到目標 commit 的編號,接著給個這次使用的例子
```shell
git format-patch 763c7f453702e48d875131bf64519766a1a18856 -1
```
如上所示, 763c7... 為要建立 patch 的 commit ,而 -1 則表示包含自己本身的前 1 個 commit ,在這個範例就是表示為自己本身,如此一來便會建立指定 commit 的 patch 檔
接著建立好的 patch 要先通過一些 scripts 裡面的檢查,輸入以下命令
```shell
$ ./scripts/checkpatch.pl ./patch_name.patch
```
如果你的 patch 有問題,會有提示訊息跟你說是哪裡需要修改,修改可以使用以下命令
```shell
$ git commit -a --amend
```
## 寄送 patch
接著就要找出 maintainer 是誰,並且將 patch 寄給他,使用以下命令
```shell
$ ./scripts/get_maintainer.pl ./patch_name.patch
```
並在這次的發送中有以下的結果
```shell
Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com> (maintainer:INTEL SPEED SELECT TECHNOLOGY)
platform-driver-x86@vger.kernel.org (open list:INTEL SPEED SELECT TECHNOLOGY)
linux-kernel@vger.kernel.org (open list)
```
最後就可以寄信給維護人並且記得要副本給其他的人,以上述的範例可以對應到以下的命令
```shell
git send-email --to srinivas.pandruvada@linux.intel.com
--cc platform-driver-x86@vger.kernel.org
--cc linux-kernel@vger.kernel.org
./0001-tools-power-x86-intel-speed-select-Remove-duplicate-.patch
```
送出之後就可以在 [LKML](https://lkml.org/) 找到自己的信件
## 參考資料
[第一次給 Linux Kernel 發 patch](https://hackmd.io/@steven1lung/submitting-patches)