checkpatch.rst 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. .. SPDX-License-Identifier: GPL-2.0-only
  2. ==========
  3. Checkpatch
  4. ==========
  5. Checkpatch (scripts/checkpatch.pl) is a perl script which checks for trivial
  6. style violations in patches and optionally corrects them. Checkpatch can
  7. also be run on file contexts and without the kernel tree.
  8. Checkpatch is not always right. Your judgement takes precedence over checkpatch
  9. messages. If your code looks better with the violations, then its probably
  10. best left alone.
  11. Options
  12. =======
  13. This section will describe the options checkpatch can be run with.
  14. Usage::
  15. ./scripts/checkpatch.pl [OPTION]... [FILE]...
  16. Available options:
  17. - -q, --quiet
  18. Enable quiet mode.
  19. - -v, --verbose
  20. Enable verbose mode. Additional verbose test descriptions are output
  21. so as to provide information on why that particular message is shown.
  22. - --no-tree
  23. Run checkpatch without the kernel tree.
  24. - --no-signoff
  25. Disable the 'Signed-off-by' line check. The sign-off is a simple line at
  26. the end of the explanation for the patch, which certifies that you wrote it
  27. or otherwise have the right to pass it on as an open-source patch.
  28. Example::
  29. Signed-off-by: Random J Developer <random@developer.example.org>
  30. Setting this flag effectively stops a message for a missing signed-off-by
  31. line in a patch context.
  32. - --patch
  33. Treat FILE as a patch. This is the default option and need not be
  34. explicitly specified.
  35. - --emacs
  36. Set output to emacs compile window format. This allows emacs users to jump
  37. from the error in the compile window directly to the offending line in the
  38. patch.
  39. - --terse
  40. Output only one line per report.
  41. - --showfile
  42. Show the diffed file position instead of the input file position.
  43. - -g, --git
  44. Treat FILE as a single commit or a git revision range.
  45. Single commit with:
  46. - <rev>
  47. - <rev>^
  48. - <rev>~n
  49. Multiple commits with:
  50. - <rev1>..<rev2>
  51. - <rev1>...<rev2>
  52. - <rev>-<count>
  53. - -f, --file
  54. Treat FILE as a regular source file. This option must be used when running
  55. checkpatch on source files in the kernel.
  56. - --subjective, --strict
  57. Enable stricter tests in checkpatch. By default the tests emitted as CHECK
  58. do not activate by default. Use this flag to activate the CHECK tests.
  59. - --list-types
  60. Every message emitted by checkpatch has an associated TYPE. Add this flag
  61. to display all the types in checkpatch.
  62. Note that when this flag is active, checkpatch does not read the input FILE,
  63. and no message is emitted. Only a list of types in checkpatch is output.
  64. - --types TYPE(,TYPE2...)
  65. Only display messages with the given types.
  66. Example::
  67. ./scripts/checkpatch.pl mypatch.patch --types EMAIL_SUBJECT,BRACES
  68. - --ignore TYPE(,TYPE2...)
  69. Checkpatch will not emit messages for the specified types.
  70. Example::
  71. ./scripts/checkpatch.pl mypatch.patch --ignore EMAIL_SUBJECT,BRACES
  72. - --show-types
  73. By default checkpatch doesn't display the type associated with the messages.
  74. Set this flag to show the message type in the output.
  75. - --max-line-length=n
  76. Set the max line length (default 100). If a line exceeds the specified
  77. length, a LONG_LINE message is emitted.
  78. The message level is different for patch and file contexts. For patches,
  79. a WARNING is emitted. While a milder CHECK is emitted for files. So for
  80. file contexts, the --strict flag must also be enabled.
  81. - --min-conf-desc-length=n
  82. Set the Kconfig entry minimum description length, if shorter, warn.
  83. - --tab-size=n
  84. Set the number of spaces for tab (default 8).
  85. - --root=PATH
  86. PATH to the kernel tree root.
  87. This option must be specified when invoking checkpatch from outside
  88. the kernel root.
  89. - --no-summary
  90. Suppress the per file summary.
  91. - --mailback
  92. Only produce a report in case of Warnings or Errors. Milder Checks are
  93. excluded from this.
  94. - --summary-file
  95. Include the filename in summary.
  96. - --debug KEY=[0|1]
  97. Turn on/off debugging of KEY, where KEY is one of 'values', 'possible',
  98. 'type', and 'attr' (default is all off).
  99. - --fix
  100. This is an EXPERIMENTAL feature. If correctable errors exist, a file
  101. <inputfile>.EXPERIMENTAL-checkpatch-fixes is created which has the
  102. automatically fixable errors corrected.
  103. - --fix-inplace
  104. EXPERIMENTAL - Similar to --fix but input file is overwritten with fixes.
  105. DO NOT USE this flag unless you are absolutely sure and you have a backup
  106. in place.
  107. - --ignore-perl-version
  108. Override checking of perl version. Runtime errors may be encountered after
  109. enabling this flag if the perl version does not meet the minimum specified.
  110. - --codespell
  111. Use the codespell dictionary for checking spelling errors.
  112. - --codespellfile
  113. Use the specified codespell file.
  114. Default is '/usr/share/codespell/dictionary.txt'.
  115. - --typedefsfile
  116. Read additional types from this file.
  117. - --color[=WHEN]
  118. Use colors 'always', 'never', or only when output is a terminal ('auto').
  119. Default is 'auto'.
  120. - --kconfig-prefix=WORD
  121. Use WORD as a prefix for Kconfig symbols (default is `CONFIG_`).
  122. - -h, --help, --version
  123. Display the help text.
  124. Message Levels
  125. ==============
  126. Messages in checkpatch are divided into three levels. The levels of messages
  127. in checkpatch denote the severity of the error. They are:
  128. - ERROR
  129. This is the most strict level. Messages of type ERROR must be taken
  130. seriously as they denote things that are very likely to be wrong.
  131. - WARNING
  132. This is the next stricter level. Messages of type WARNING requires a
  133. more careful review. But it is milder than an ERROR.
  134. - CHECK
  135. This is the mildest level. These are things which may require some thought.
  136. Type Descriptions
  137. =================
  138. This section contains a description of all the message types in checkpatch.
  139. .. Types in this section are also parsed by checkpatch.
  140. .. The types are grouped into subsections based on use.
  141. Allocation style
  142. ----------------
  143. **ALLOC_ARRAY_ARGS**
  144. The first argument for kcalloc or kmalloc_array should be the
  145. number of elements. sizeof() as the first argument is generally
  146. wrong.
  147. See: https://www.kernel.org/doc/html/latest/core-api/memory-allocation.html
  148. **ALLOC_SIZEOF_STRUCT**
  149. The allocation style is bad. In general for family of
  150. allocation functions using sizeof() to get memory size,
  151. constructs like::
  152. p = alloc(sizeof(struct foo), ...)
  153. should be::
  154. p = alloc(sizeof(*p), ...)
  155. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#allocating-memory
  156. **ALLOC_WITH_MULTIPLY**
  157. Prefer kmalloc_array/kcalloc over kmalloc/kzalloc with a
  158. sizeof multiply.
  159. See: https://www.kernel.org/doc/html/latest/core-api/memory-allocation.html
  160. API usage
  161. ---------
  162. **ARCH_DEFINES**
  163. Architecture specific defines should be avoided wherever
  164. possible.
  165. **ARCH_INCLUDE_LINUX**
  166. Whenever asm/file.h is included and linux/file.h exists, a
  167. conversion can be made when linux/file.h includes asm/file.h.
  168. However this is not always the case (See signal.h).
  169. This message type is emitted only for includes from arch/.
  170. **AVOID_BUG**
  171. BUG() or BUG_ON() should be avoided totally.
  172. Use WARN() and WARN_ON() instead, and handle the "impossible"
  173. error condition as gracefully as possible.
  174. See: https://www.kernel.org/doc/html/latest/process/deprecated.html#bug-and-bug-on
  175. **CONSIDER_KSTRTO**
  176. The simple_strtol(), simple_strtoll(), simple_strtoul(), and
  177. simple_strtoull() functions explicitly ignore overflows, which
  178. may lead to unexpected results in callers. The respective kstrtol(),
  179. kstrtoll(), kstrtoul(), and kstrtoull() functions tend to be the
  180. correct replacements.
  181. See: https://www.kernel.org/doc/html/latest/process/deprecated.html#simple-strtol-simple-strtoll-simple-strtoul-simple-strtoull
  182. **CONSTANT_CONVERSION**
  183. Use of __constant_<foo> form is discouraged for the following functions::
  184. __constant_cpu_to_be[x]
  185. __constant_cpu_to_le[x]
  186. __constant_be[x]_to_cpu
  187. __constant_le[x]_to_cpu
  188. __constant_htons
  189. __constant_ntohs
  190. Using any of these outside of include/uapi/ is not preferred as using the
  191. function without __constant_ is identical when the argument is a
  192. constant.
  193. In big endian systems, the macros like __constant_cpu_to_be32(x) and
  194. cpu_to_be32(x) expand to the same expression::
  195. #define __constant_cpu_to_be32(x) ((__force __be32)(__u32)(x))
  196. #define __cpu_to_be32(x) ((__force __be32)(__u32)(x))
  197. In little endian systems, the macros __constant_cpu_to_be32(x) and
  198. cpu_to_be32(x) expand to __constant_swab32 and __swab32. __swab32
  199. has a __builtin_constant_p check::
  200. #define __swab32(x) \
  201. (__builtin_constant_p((__u32)(x)) ? \
  202. ___constant_swab32(x) : \
  203. __fswab32(x))
  204. So ultimately they have a special case for constants.
  205. Similar is the case with all of the macros in the list. Thus
  206. using the __constant_... forms are unnecessarily verbose and
  207. not preferred outside of include/uapi.
  208. See: https://lore.kernel.org/lkml/1400106425.12666.6.camel@joe-AO725/
  209. **DEPRECATED_API**
  210. Usage of a deprecated RCU API is detected. It is recommended to replace
  211. old flavourful RCU APIs by their new vanilla-RCU counterparts.
  212. The full list of available RCU APIs can be viewed from the kernel docs.
  213. See: https://www.kernel.org/doc/html/latest/RCU/whatisRCU.html#full-list-of-rcu-apis
  214. **DEVICE_ATTR_FUNCTIONS**
  215. The function names used in DEVICE_ATTR is unusual.
  216. Typically, the store and show functions are used with <attr>_store and
  217. <attr>_show, where <attr> is a named attribute variable of the device.
  218. Consider the following examples::
  219. static DEVICE_ATTR(type, 0444, type_show, NULL);
  220. static DEVICE_ATTR(power, 0644, power_show, power_store);
  221. The function names should preferably follow the above pattern.
  222. See: https://www.kernel.org/doc/html/latest/driver-api/driver-model/device.html#attributes
  223. **DEVICE_ATTR_RO**
  224. The DEVICE_ATTR_RO(name) helper macro can be used instead of
  225. DEVICE_ATTR(name, 0444, name_show, NULL);
  226. Note that the macro automatically appends _show to the named
  227. attribute variable of the device for the show method.
  228. See: https://www.kernel.org/doc/html/latest/driver-api/driver-model/device.html#attributes
  229. **DEVICE_ATTR_RW**
  230. The DEVICE_ATTR_RW(name) helper macro can be used instead of
  231. DEVICE_ATTR(name, 0644, name_show, name_store);
  232. Note that the macro automatically appends _show and _store to the
  233. named attribute variable of the device for the show and store methods.
  234. See: https://www.kernel.org/doc/html/latest/driver-api/driver-model/device.html#attributes
  235. **DEVICE_ATTR_WO**
  236. The DEVICE_AATR_WO(name) helper macro can be used instead of
  237. DEVICE_ATTR(name, 0200, NULL, name_store);
  238. Note that the macro automatically appends _store to the
  239. named attribute variable of the device for the store method.
  240. See: https://www.kernel.org/doc/html/latest/driver-api/driver-model/device.html#attributes
  241. **DUPLICATED_SYSCTL_CONST**
  242. Commit d91bff3011cf ("proc/sysctl: add shared variables for range
  243. check") added some shared const variables to be used instead of a local
  244. copy in each source file.
  245. Consider replacing the sysctl range checking value with the shared
  246. one in include/linux/sysctl.h. The following conversion scheme may
  247. be used::
  248. &zero -> SYSCTL_ZERO
  249. &one -> SYSCTL_ONE
  250. &int_max -> SYSCTL_INT_MAX
  251. See:
  252. 1. https://lore.kernel.org/lkml/20190430180111.10688-1-mcroce@redhat.com/
  253. 2. https://lore.kernel.org/lkml/20190531131422.14970-1-mcroce@redhat.com/
  254. **ENOSYS**
  255. ENOSYS means that a nonexistent system call was called.
  256. Earlier, it was wrongly used for things like invalid operations on
  257. otherwise valid syscalls. This should be avoided in new code.
  258. See: https://lore.kernel.org/lkml/5eb299021dec23c1a48fa7d9f2c8b794e967766d.1408730669.git.luto@amacapital.net/
  259. **ENOTSUPP**
  260. ENOTSUPP is not a standard error code and should be avoided in new patches.
  261. EOPNOTSUPP should be used instead.
  262. See: https://lore.kernel.org/netdev/20200510182252.GA411829@lunn.ch/
  263. **EXPORT_SYMBOL**
  264. EXPORT_SYMBOL should immediately follow the symbol to be exported.
  265. **IN_ATOMIC**
  266. in_atomic() is not for driver use so any such use is reported as an ERROR.
  267. Also in_atomic() is often used to determine if sleeping is permitted,
  268. but it is not reliable in this use model. Therefore its use is
  269. strongly discouraged.
  270. However, in_atomic() is ok for core kernel use.
  271. See: https://lore.kernel.org/lkml/20080320201723.b87b3732.akpm@linux-foundation.org/
  272. **LOCKDEP**
  273. The lockdep_no_validate class was added as a temporary measure to
  274. prevent warnings on conversion of device->sem to device->mutex.
  275. It should not be used for any other purpose.
  276. See: https://lore.kernel.org/lkml/1268959062.9440.467.camel@laptop/
  277. **MALFORMED_INCLUDE**
  278. The #include statement has a malformed path. This has happened
  279. because the author has included a double slash "//" in the pathname
  280. accidentally.
  281. **USE_LOCKDEP**
  282. lockdep_assert_held() annotations should be preferred over
  283. assertions based on spin_is_locked()
  284. See: https://www.kernel.org/doc/html/latest/locking/lockdep-design.html#annotations
  285. **UAPI_INCLUDE**
  286. No #include statements in include/uapi should use a uapi/ path.
  287. **USLEEP_RANGE**
  288. usleep_range() should be preferred over udelay(). The proper way of
  289. using usleep_range() is mentioned in the kernel docs.
  290. Comments
  291. --------
  292. **BLOCK_COMMENT_STYLE**
  293. The comment style is incorrect. The preferred style for multi-
  294. line comments is::
  295. /*
  296. * This is the preferred style
  297. * for multi line comments.
  298. */
  299. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#commenting
  300. **C99_COMMENTS**
  301. C99 style single line comments (//) should not be used.
  302. Prefer the block comment style instead.
  303. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#commenting
  304. **DATA_RACE**
  305. Applications of data_race() should have a comment so as to document the
  306. reasoning behind why it was deemed safe.
  307. See: https://lore.kernel.org/lkml/20200401101714.44781-1-elver@google.com/
  308. **FSF_MAILING_ADDRESS**
  309. Kernel maintainers reject new instances of the GPL boilerplate paragraph
  310. directing people to write to the FSF for a copy of the GPL, since the
  311. FSF has moved in the past and may do so again.
  312. So do not write paragraphs about writing to the Free Software Foundation's
  313. mailing address.
  314. See: https://lore.kernel.org/lkml/20131006222342.GT19510@leaf/
  315. **UNCOMMENTED_RGMII_MODE**
  316. Historically, the RGMII PHY modes specified in Device Trees have been
  317. used inconsistently, often referring to the usage of delays on the PHY
  318. side rather than describing the board.
  319. PHY modes "rgmii", "rgmii-rxid" and "rgmii-txid" modes require the clock
  320. signal to be delayed on the PCB; this unusual configuration should be
  321. described in a comment. If they are not (meaning that the delay is realized
  322. internally in the MAC or PHY), "rgmii-id" is the correct PHY mode.
  323. Commit message
  324. --------------
  325. **BAD_SIGN_OFF**
  326. The signed-off-by line does not fall in line with the standards
  327. specified by the community.
  328. See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#developer-s-certificate-of-origin-1-1
  329. **BAD_STABLE_ADDRESS_STYLE**
  330. The email format for stable is incorrect.
  331. Some valid options for stable address are::
  332. 1. stable@vger.kernel.org
  333. 2. stable@kernel.org
  334. For adding version info, the following comment style should be used::
  335. stable@vger.kernel.org # version info
  336. **COMMIT_COMMENT_SYMBOL**
  337. Commit log lines starting with a '#' are ignored by git as
  338. comments. To solve this problem addition of a single space
  339. infront of the log line is enough.
  340. **COMMIT_MESSAGE**
  341. The patch is missing a commit description. A brief
  342. description of the changes made by the patch should be added.
  343. See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
  344. **EMAIL_SUBJECT**
  345. Naming the tool that found the issue is not very useful in the
  346. subject line. A good subject line summarizes the change that
  347. the patch brings.
  348. See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
  349. **FROM_SIGN_OFF_MISMATCH**
  350. The author's email does not match with that in the Signed-off-by:
  351. line(s). This can be sometimes caused due to an improperly configured
  352. email client.
  353. This message is emitted due to any of the following reasons::
  354. - The email names do not match.
  355. - The email addresses do not match.
  356. - The email subaddresses do not match.
  357. - The email comments do not match.
  358. **MISSING_SIGN_OFF**
  359. The patch is missing a Signed-off-by line. A signed-off-by
  360. line should be added according to Developer's certificate of
  361. Origin.
  362. See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#sign-your-work-the-developer-s-certificate-of-origin
  363. **NO_AUTHOR_SIGN_OFF**
  364. The author of the patch has not signed off the patch. It is
  365. required that a simple sign off line should be present at the
  366. end of explanation of the patch to denote that the author has
  367. written it or otherwise has the rights to pass it on as an open
  368. source patch.
  369. See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#sign-your-work-the-developer-s-certificate-of-origin
  370. **DIFF_IN_COMMIT_MSG**
  371. Avoid having diff content in commit message.
  372. This causes problems when one tries to apply a file containing both
  373. the changelog and the diff because patch(1) tries to apply the diff
  374. which it found in the changelog.
  375. See: https://lore.kernel.org/lkml/20150611134006.9df79a893e3636019ad2759e@linux-foundation.org/
  376. **GERRIT_CHANGE_ID**
  377. To be picked up by gerrit, the footer of the commit message might
  378. have a Change-Id like::
  379. Change-Id: Ic8aaa0728a43936cd4c6e1ed590e01ba8f0fbf5b
  380. Signed-off-by: A. U. Thor <author@example.com>
  381. The Change-Id line must be removed before submitting.
  382. **GIT_COMMIT_ID**
  383. The proper way to reference a commit id is:
  384. commit <12+ chars of sha1> ("<title line>")
  385. An example may be::
  386. Commit e21d2170f36602ae2708 ("video: remove unnecessary
  387. platform_set_drvdata()") removed the unnecessary
  388. platform_set_drvdata(), but left the variable "dev" unused,
  389. delete it.
  390. See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
  391. **BAD_FIXES_TAG**
  392. The Fixes: tag is malformed or does not follow the community conventions.
  393. This can occur if the tag have been split into multiple lines (e.g., when
  394. pasted in an email program with word wrapping enabled).
  395. See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
  396. **BAD_COMMIT_SEPARATOR**
  397. The commit separator is a single line with 3 dashes.
  398. The regex match is '^---$'
  399. Lines that start with 3 dashes and have more content on the same line
  400. may confuse tools that apply patches.
  401. Comparison style
  402. ----------------
  403. **ASSIGN_IN_IF**
  404. Do not use assignments in if condition.
  405. Example::
  406. if ((foo = bar(...)) < BAZ) {
  407. should be written as::
  408. foo = bar(...);
  409. if (foo < BAZ) {
  410. **BOOL_COMPARISON**
  411. Comparisons of A to true and false are better written
  412. as A and !A.
  413. See: https://lore.kernel.org/lkml/1365563834.27174.12.camel@joe-AO722/
  414. **COMPARISON_TO_NULL**
  415. Comparisons to NULL in the form (foo == NULL) or (foo != NULL)
  416. are better written as (!foo) and (foo).
  417. **CONSTANT_COMPARISON**
  418. Comparisons with a constant or upper case identifier on the left
  419. side of the test should be avoided.
  420. Indentation and Line Breaks
  421. ---------------------------
  422. **CODE_INDENT**
  423. Code indent should use tabs instead of spaces.
  424. Outside of comments, documentation and Kconfig,
  425. spaces are never used for indentation.
  426. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#indentation
  427. **DEEP_INDENTATION**
  428. Indentation with 6 or more tabs usually indicate overly indented
  429. code.
  430. It is suggested to refactor excessive indentation of
  431. if/else/for/do/while/switch statements.
  432. See: https://lore.kernel.org/lkml/1328311239.21255.24.camel@joe2Laptop/
  433. **SWITCH_CASE_INDENT_LEVEL**
  434. switch should be at the same indent as case.
  435. Example::
  436. switch (suffix) {
  437. case 'G':
  438. case 'g':
  439. mem <<= 30;
  440. break;
  441. case 'M':
  442. case 'm':
  443. mem <<= 20;
  444. break;
  445. case 'K':
  446. case 'k':
  447. mem <<= 10;
  448. fallthrough;
  449. default:
  450. break;
  451. }
  452. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#indentation
  453. **LONG_LINE**
  454. The line has exceeded the specified maximum length.
  455. To use a different maximum line length, the --max-line-length=n option
  456. may be added while invoking checkpatch.
  457. Earlier, the default line length was 80 columns. Commit bdc48fa11e46
  458. ("checkpatch/coding-style: deprecate 80-column warning") increased the
  459. limit to 100 columns. This is not a hard limit either and it's
  460. preferable to stay within 80 columns whenever possible.
  461. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#breaking-long-lines-and-strings
  462. **LONG_LINE_STRING**
  463. A string starts before but extends beyond the maximum line length.
  464. To use a different maximum line length, the --max-line-length=n option
  465. may be added while invoking checkpatch.
  466. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#breaking-long-lines-and-strings
  467. **LONG_LINE_COMMENT**
  468. A comment starts before but extends beyond the maximum line length.
  469. To use a different maximum line length, the --max-line-length=n option
  470. may be added while invoking checkpatch.
  471. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#breaking-long-lines-and-strings
  472. **SPLIT_STRING**
  473. Quoted strings that appear as messages in userspace and can be
  474. grepped, should not be split across multiple lines.
  475. See: https://lore.kernel.org/lkml/20120203052727.GA15035@leaf/
  476. **MULTILINE_DEREFERENCE**
  477. A single dereferencing identifier spanned on multiple lines like::
  478. struct_identifier->member[index].
  479. member = <foo>;
  480. is generally hard to follow. It can easily lead to typos and so makes
  481. the code vulnerable to bugs.
  482. If fixing the multiple line dereferencing leads to an 80 column
  483. violation, then either rewrite the code in a more simple way or if the
  484. starting part of the dereferencing identifier is the same and used at
  485. multiple places then store it in a temporary variable, and use that
  486. temporary variable only at all the places. For example, if there are
  487. two dereferencing identifiers::
  488. member1->member2->member3.foo1;
  489. member1->member2->member3.foo2;
  490. then store the member1->member2->member3 part in a temporary variable.
  491. It not only helps to avoid the 80 column violation but also reduces
  492. the program size by removing the unnecessary dereferences.
  493. But if none of the above methods work then ignore the 80 column
  494. violation because it is much easier to read a dereferencing identifier
  495. on a single line.
  496. **TRAILING_STATEMENTS**
  497. Trailing statements (for example after any conditional) should be
  498. on the next line.
  499. Statements, such as::
  500. if (x == y) break;
  501. should be::
  502. if (x == y)
  503. break;
  504. Macros, Attributes and Symbols
  505. ------------------------------
  506. **ARRAY_SIZE**
  507. The ARRAY_SIZE(foo) macro should be preferred over
  508. sizeof(foo)/sizeof(foo[0]) for finding number of elements in an
  509. array.
  510. The macro is defined in include/linux/array_size.h::
  511. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  512. **AVOID_EXTERNS**
  513. Function prototypes don't need to be declared extern in .h
  514. files. It's assumed by the compiler and is unnecessary.
  515. **AVOID_L_PREFIX**
  516. Local symbol names that are prefixed with `.L` should be avoided,
  517. as this has special meaning for the assembler; a symbol entry will
  518. not be emitted into the symbol table. This can prevent `objtool`
  519. from generating correct unwind info.
  520. Symbols with STB_LOCAL binding may still be used, and `.L` prefixed
  521. local symbol names are still generally usable within a function,
  522. but `.L` prefixed local symbol names should not be used to denote
  523. the beginning or end of code regions via
  524. `SYM_CODE_START_LOCAL`/`SYM_CODE_END`
  525. **BIT_MACRO**
  526. Defines like: 1 << <digit> could be BIT(digit).
  527. The BIT() macro is defined via include/linux/bits.h::
  528. #define BIT(nr) (1UL << (nr))
  529. **CONST_READ_MOSTLY**
  530. When a variable is tagged with the __read_mostly annotation, it is a
  531. signal to the compiler that accesses to the variable will be mostly
  532. reads and rarely(but NOT never) a write.
  533. const __read_mostly does not make any sense as const data is already
  534. read-only. The __read_mostly annotation thus should be removed.
  535. **DATE_TIME**
  536. It is generally desirable that building the same source code with
  537. the same set of tools is reproducible, i.e. the output is always
  538. exactly the same.
  539. The kernel does *not* use the ``__DATE__`` and ``__TIME__`` macros,
  540. and enables warnings if they are used as they can lead to
  541. non-deterministic builds.
  542. See: https://www.kernel.org/doc/html/latest/kbuild/reproducible-builds.html#timestamps
  543. **DEFINE_ARCH_HAS**
  544. The ARCH_HAS_xyz and ARCH_HAVE_xyz patterns are wrong.
  545. For big conceptual features use Kconfig symbols instead. And for
  546. smaller things where we have compatibility fallback functions but
  547. want architectures able to override them with optimized ones, we
  548. should either use weak functions (appropriate for some cases), or
  549. the symbol that protects them should be the same symbol we use.
  550. See: https://lore.kernel.org/lkml/CA+55aFycQ9XJvEOsiM3txHL5bjUc8CeKWJNR_H+MiicaddB42Q@mail.gmail.com/
  551. **DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON**
  552. do {} while(0) macros should not have a trailing semicolon.
  553. **INIT_ATTRIBUTE**
  554. Const init definitions should use __initconst instead of
  555. __initdata.
  556. Similarly init definitions without const require a separate
  557. use of const.
  558. **INLINE_LOCATION**
  559. The inline keyword should sit between storage class and type.
  560. For example, the following segment::
  561. inline static int example_function(void)
  562. {
  563. ...
  564. }
  565. should be::
  566. static inline int example_function(void)
  567. {
  568. ...
  569. }
  570. **MISPLACED_INIT**
  571. It is possible to use section markers on variables in a way
  572. which gcc doesn't understand (or at least not the way the
  573. developer intended)::
  574. static struct __initdata samsung_pll_clock exynos4_plls[nr_plls] = {
  575. does not put exynos4_plls in the .initdata section. The __initdata
  576. marker can be virtually anywhere on the line, except right after
  577. "struct". The preferred location is before the "=" sign if there is
  578. one, or before the trailing ";" otherwise.
  579. See: https://lore.kernel.org/lkml/1377655732.3619.19.camel@joe-AO722/
  580. **MULTISTATEMENT_MACRO_USE_DO_WHILE**
  581. Macros with multiple statements should be enclosed in a
  582. do - while block. Same should also be the case for macros
  583. starting with `if` to avoid logic defects::
  584. #define macrofun(a, b, c) \
  585. do { \
  586. if (a == 5) \
  587. do_this(b, c); \
  588. } while (0)
  589. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#macros-enums-and-rtl
  590. **PREFER_FALLTHROUGH**
  591. Use the `fallthrough;` pseudo keyword instead of
  592. `/* fallthrough */` like comments.
  593. **TRAILING_SEMICOLON**
  594. Macro definition should not end with a semicolon. The macro
  595. invocation style should be consistent with function calls.
  596. This can prevent any unexpected code paths::
  597. #define MAC do_something;
  598. If this macro is used within a if else statement, like::
  599. if (some_condition)
  600. MAC;
  601. else
  602. do_something;
  603. Then there would be a compilation error, because when the macro is
  604. expanded there are two trailing semicolons, so the else branch gets
  605. orphaned.
  606. See: https://lore.kernel.org/lkml/1399671106.2912.21.camel@joe-AO725/
  607. **MACRO_ARG_UNUSED**
  608. If function-like macros do not utilize a parameter, it might result
  609. in a build warning. We advocate for utilizing static inline functions
  610. to replace such macros.
  611. For example, for a macro such as the one below::
  612. #define test(a) do { } while (0)
  613. there would be a warning like below::
  614. WARNING: Argument 'a' is not used in function-like macro.
  615. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#macros-enums-and-rtl
  616. **SINGLE_STATEMENT_DO_WHILE_MACRO**
  617. For the multi-statement macros, it is necessary to use the do-while
  618. loop to avoid unpredictable code paths. The do-while loop helps to
  619. group the multiple statements into a single one so that a
  620. function-like macro can be used as a function only.
  621. But for the single statement macros, it is unnecessary to use the
  622. do-while loop. Although the code is syntactically correct but using
  623. the do-while loop is redundant. So remove the do-while loop for single
  624. statement macros.
  625. **WEAK_DECLARATION**
  626. Using weak declarations like __attribute__((weak)) or __weak
  627. can have unintended link defects. Avoid using them.
  628. Functions and Variables
  629. -----------------------
  630. **CAMELCASE**
  631. Avoid CamelCase Identifiers.
  632. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#naming
  633. **CONST_CONST**
  634. Using `const <type> const *` is generally meant to be
  635. written `const <type> * const`.
  636. **CONST_STRUCT**
  637. Using const is generally a good idea. Checkpatch reads
  638. a list of frequently used structs that are always or
  639. almost always constant.
  640. The existing structs list can be viewed from
  641. `scripts/const_structs.checkpatch`.
  642. See: https://lore.kernel.org/lkml/alpine.DEB.2.10.1608281509480.3321@hadrien/
  643. **EMBEDDED_FUNCTION_NAME**
  644. Embedded function names are less appropriate to use as
  645. refactoring can cause function renaming. Prefer the use of
  646. "%s", __func__ to embedded function names.
  647. Note that this does not work with -f (--file) checkpatch option
  648. as it depends on patch context providing the function name.
  649. **FUNCTION_ARGUMENTS**
  650. This warning is emitted due to any of the following reasons:
  651. 1. Arguments for the function declaration do not follow
  652. the identifier name. Example::
  653. void foo
  654. (int bar, int baz)
  655. This should be corrected to::
  656. void foo(int bar, int baz)
  657. 2. Some arguments for the function definition do not
  658. have an identifier name. Example::
  659. void foo(int)
  660. All arguments should have identifier names.
  661. **FUNCTION_WITHOUT_ARGS**
  662. Function declarations without arguments like::
  663. int foo()
  664. should be::
  665. int foo(void)
  666. **GLOBAL_INITIALISERS**
  667. Global variables should not be initialized explicitly to
  668. 0 (or NULL, false, etc.). Your compiler (or rather your
  669. loader, which is responsible for zeroing out the relevant
  670. sections) automatically does it for you.
  671. **INITIALISED_STATIC**
  672. Static variables should not be initialized explicitly to zero.
  673. Your compiler (or rather your loader) automatically does
  674. it for you.
  675. **MULTIPLE_ASSIGNMENTS**
  676. Multiple assignments on a single line makes the code unnecessarily
  677. complicated. So on a single line assign value to a single variable
  678. only, this makes the code more readable and helps avoid typos.
  679. **RETURN_PARENTHESES**
  680. return is not a function and as such doesn't need parentheses::
  681. return (bar);
  682. can simply be::
  683. return bar;
  684. **UNINITIALIZED_PTR_WITH_FREE**
  685. Pointers with __free attribute should be declared at the place of use
  686. and initialized (see include/linux/cleanup.h). In this case
  687. declarations at the top of the function rule can be relaxed. Not doing
  688. so may lead to undefined behavior as the memory assigned (garbage,
  689. in case not initialized) to the pointer is freed automatically when
  690. the pointer goes out of scope.
  691. Also see: https://lore.kernel.org/lkml/58fd478f408a34b578ee8d949c5c4b4da4d4f41d.camel@HansenPartnership.com/
  692. Example::
  693. type var __free(free_func);
  694. ... // var not used, but, in future someone might add a return here
  695. var = malloc(var_size);
  696. ...
  697. should be initialized as::
  698. ...
  699. type var __free(free_func) = malloc(var_size);
  700. ...
  701. Permissions
  702. -----------
  703. **DEVICE_ATTR_PERMS**
  704. The permissions used in DEVICE_ATTR are unusual.
  705. Typically only three permissions are used - 0644 (RW), 0444 (RO)
  706. and 0200 (WO).
  707. See: https://www.kernel.org/doc/html/latest/filesystems/sysfs.html#attributes
  708. **EXECUTE_PERMISSIONS**
  709. There is no reason for source files to be executable. The executable
  710. bit can be removed safely.
  711. **EXPORTED_WORLD_WRITABLE**
  712. Exporting world writable sysfs/debugfs files is usually a bad thing.
  713. When done arbitrarily they can introduce serious security bugs.
  714. In the past, some of the debugfs vulnerabilities would seemingly allow
  715. any local user to write arbitrary values into device registers - a
  716. situation from which little good can be expected to emerge.
  717. See: https://lore.kernel.org/linux-arm-kernel/cover.1296818921.git.segoon@openwall.com/
  718. **NON_OCTAL_PERMISSIONS**
  719. Permission bits should use 4 digit octal permissions (like 0700 or 0444).
  720. Avoid using any other base like decimal.
  721. **SYMBOLIC_PERMS**
  722. Permission bits in the octal form are more readable and easier to
  723. understand than their symbolic counterparts because many command-line
  724. tools use this notation. Experienced kernel developers have been using
  725. these traditional Unix permission bits for decades and so they find it
  726. easier to understand the octal notation than the symbolic macros.
  727. For example, it is harder to read S_IWUSR|S_IRUGO than 0644, which
  728. obscures the developer's intent rather than clarifying it.
  729. See: https://lore.kernel.org/lkml/CA+55aFw5v23T-zvDZp-MmD_EYxF8WbafwwB59934FV7g21uMGQ@mail.gmail.com/
  730. Spacing and Brackets
  731. --------------------
  732. **ASSIGNMENT_CONTINUATIONS**
  733. Assignment operators should not be written at the start of a
  734. line but should follow the operand at the previous line.
  735. **BRACES**
  736. The placement of braces is stylistically incorrect.
  737. The preferred way is to put the opening brace last on the line,
  738. and put the closing brace first::
  739. if (x is true) {
  740. we do y
  741. }
  742. This applies for all non-functional blocks.
  743. However, there is one special case, namely functions: they have the
  744. opening brace at the beginning of the next line, thus::
  745. int function(int x)
  746. {
  747. body of function
  748. }
  749. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#placing-braces-and-spaces
  750. **BRACKET_SPACE**
  751. Whitespace before opening bracket '[' is prohibited.
  752. There are some exceptions:
  753. 1. With a type on the left::
  754. int [] a;
  755. 2. At the beginning of a line for slice initialisers::
  756. [0...10] = 5,
  757. 3. Inside a curly brace::
  758. = { [0...10] = 5 }
  759. **CONCATENATED_STRING**
  760. Concatenated elements should have a space in between.
  761. Example::
  762. printk(KERN_INFO"bar");
  763. should be::
  764. printk(KERN_INFO "bar");
  765. **ELSE_AFTER_BRACE**
  766. `else {` should follow the closing block `}` on the same line.
  767. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#placing-braces-and-spaces
  768. **LINE_SPACING**
  769. Vertical space is wasted given the limited number of lines an
  770. editor window can display when multiple blank lines are used.
  771. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#spaces
  772. **OPEN_BRACE**
  773. The opening brace should be following the function definitions on the
  774. next line. For any non-functional block it should be on the same line
  775. as the last construct.
  776. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#placing-braces-and-spaces
  777. **POINTER_LOCATION**
  778. When using pointer data or a function that returns a pointer type,
  779. the preferred use of * is adjacent to the data name or function name
  780. and not adjacent to the type name.
  781. Examples::
  782. char *linux_banner;
  783. unsigned long long memparse(char *ptr, char **retptr);
  784. char *match_strdup(substring_t *s);
  785. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#spaces
  786. **SPACING**
  787. Whitespace style used in the kernel sources is described in kernel docs.
  788. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#spaces
  789. **TRAILING_WHITESPACE**
  790. Trailing whitespace should always be removed.
  791. Some editors highlight the trailing whitespace and cause visual
  792. distractions when editing files.
  793. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#spaces
  794. **UNNECESSARY_PARENTHESES**
  795. Parentheses are not required in the following cases:
  796. 1. Function pointer uses::
  797. (foo->bar)();
  798. could be::
  799. foo->bar();
  800. 2. Comparisons in if::
  801. if ((foo->bar) && (foo->baz))
  802. if ((foo == bar))
  803. could be::
  804. if (foo->bar && foo->baz)
  805. if (foo == bar)
  806. 3. addressof/dereference single Lvalues::
  807. &(foo->bar)
  808. *(foo->bar)
  809. could be::
  810. &foo->bar
  811. *foo->bar
  812. **WHILE_AFTER_BRACE**
  813. while should follow the closing bracket on the same line::
  814. do {
  815. ...
  816. } while(something);
  817. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#placing-braces-and-spaces
  818. Others
  819. ------
  820. **CONFIG_DESCRIPTION**
  821. Kconfig symbols should have a help text which fully describes
  822. it.
  823. **CORRUPTED_PATCH**
  824. The patch seems to be corrupted or lines are wrapped.
  825. Please regenerate the patch file before sending it to the maintainer.
  826. **CVS_KEYWORD**
  827. Since linux moved to git, the CVS markers are no longer used.
  828. So, CVS style keywords ($Id$, $Revision$, $Log$) should not be
  829. added.
  830. **DEFAULT_NO_BREAK**
  831. switch default case is sometimes written as "default:;". This can
  832. cause new cases added below default to be defective.
  833. A "break;" should be added after empty default statement to avoid
  834. unwanted fallthrough.
  835. **DOS_LINE_ENDINGS**
  836. For DOS-formatted patches, there are extra ^M symbols at the end of
  837. the line. These should be removed.
  838. **DT_SCHEMA_BINDING_PATCH**
  839. DT bindings moved to a json-schema based format instead of
  840. freeform text.
  841. See: https://www.kernel.org/doc/html/latest/devicetree/bindings/writing-schema.html
  842. **DT_SPLIT_BINDING_PATCH**
  843. Devicetree bindings should be their own patch. This is because
  844. bindings are logically independent from a driver implementation,
  845. they have a different maintainer (even though they often
  846. are applied via the same tree), and it makes for a cleaner history in the
  847. DT only tree created with git-filter-branch.
  848. See: https://www.kernel.org/doc/html/latest/devicetree/bindings/submitting-patches.html#i-for-patch-submitters
  849. **EMBEDDED_FILENAME**
  850. Embedding the complete filename path inside the file isn't particularly
  851. useful as often the path is moved around and becomes incorrect.
  852. **FILE_PATH_CHANGES**
  853. Whenever files are added, moved, or deleted, the MAINTAINERS file
  854. patterns can be out of sync or outdated.
  855. So MAINTAINERS might need updating in these cases.
  856. **MEMSET**
  857. The memset use appears to be incorrect. This may be caused due to
  858. badly ordered parameters. Please recheck the usage.
  859. **NOT_UNIFIED_DIFF**
  860. The patch file does not appear to be in unified-diff format. Please
  861. regenerate the patch file before sending it to the maintainer.
  862. **PLACEHOLDER_USE**
  863. Detects unhandled placeholder text left in cover letters or commit headers/logs.
  864. Common placeholders include lines like::
  865. *** SUBJECT HERE ***
  866. *** BLURB HERE ***
  867. These typically come from autogenerated templates. Replace them with a proper
  868. subject and description before sending.
  869. **PRINTF_0XDECIMAL**
  870. Prefixing 0x with decimal output is defective and should be corrected.
  871. **SPDX_LICENSE_TAG**
  872. The source file is missing or has an improper SPDX identifier tag.
  873. The Linux kernel requires the precise SPDX identifier in all source files,
  874. and it is thoroughly documented in the kernel docs.
  875. See: https://www.kernel.org/doc/html/latest/process/license-rules.html
  876. **TYPO_SPELLING**
  877. Some words may have been misspelled. Consider reviewing them.