checkpatch.rst 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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 <[email protected]>
  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 exists, 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 maybe 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. **DEPRECATED_VARIABLE**
  215. EXTRA_{A,C,CPP,LD}FLAGS are deprecated and should be replaced by the new
  216. flags added via commit f77bf01425b1 ("kbuild: introduce ccflags-y,
  217. asflags-y and ldflags-y").
  218. The following conversion scheme maybe used::
  219. EXTRA_AFLAGS -> asflags-y
  220. EXTRA_CFLAGS -> ccflags-y
  221. EXTRA_CPPFLAGS -> cppflags-y
  222. EXTRA_LDFLAGS -> ldflags-y
  223. See:
  224. 1. https://lore.kernel.org/lkml/[email protected]/
  225. 2. https://lore.kernel.org/lkml/[email protected]/
  226. 3. https://www.kernel.org/doc/html/latest/kbuild/makefiles.html#compilation-flags
  227. **DEVICE_ATTR_FUNCTIONS**
  228. The function names used in DEVICE_ATTR is unusual.
  229. Typically, the store and show functions are used with <attr>_store and
  230. <attr>_show, where <attr> is a named attribute variable of the device.
  231. Consider the following examples::
  232. static DEVICE_ATTR(type, 0444, type_show, NULL);
  233. static DEVICE_ATTR(power, 0644, power_show, power_store);
  234. The function names should preferably follow the above pattern.
  235. See: https://www.kernel.org/doc/html/latest/driver-api/driver-model/device.html#attributes
  236. **DEVICE_ATTR_RO**
  237. The DEVICE_ATTR_RO(name) helper macro can be used instead of
  238. DEVICE_ATTR(name, 0444, name_show, NULL);
  239. Note that the macro automatically appends _show to the named
  240. attribute variable of the device for the show method.
  241. See: https://www.kernel.org/doc/html/latest/driver-api/driver-model/device.html#attributes
  242. **DEVICE_ATTR_RW**
  243. The DEVICE_ATTR_RW(name) helper macro can be used instead of
  244. DEVICE_ATTR(name, 0644, name_show, name_store);
  245. Note that the macro automatically appends _show and _store to the
  246. named attribute variable of the device for the show and store methods.
  247. See: https://www.kernel.org/doc/html/latest/driver-api/driver-model/device.html#attributes
  248. **DEVICE_ATTR_WO**
  249. The DEVICE_AATR_WO(name) helper macro can be used instead of
  250. DEVICE_ATTR(name, 0200, NULL, name_store);
  251. Note that the macro automatically appends _store to the
  252. named attribute variable of the device for the store method.
  253. See: https://www.kernel.org/doc/html/latest/driver-api/driver-model/device.html#attributes
  254. **DUPLICATED_SYSCTL_CONST**
  255. Commit d91bff3011cf ("proc/sysctl: add shared variables for range
  256. check") added some shared const variables to be used instead of a local
  257. copy in each source file.
  258. Consider replacing the sysctl range checking value with the shared
  259. one in include/linux/sysctl.h. The following conversion scheme may
  260. be used::
  261. &zero -> SYSCTL_ZERO
  262. &one -> SYSCTL_ONE
  263. &int_max -> SYSCTL_INT_MAX
  264. See:
  265. 1. https://lore.kernel.org/lkml/[email protected]/
  266. 2. https://lore.kernel.org/lkml/[email protected]/
  267. **ENOSYS**
  268. ENOSYS means that a nonexistent system call was called.
  269. Earlier, it was wrongly used for things like invalid operations on
  270. otherwise valid syscalls. This should be avoided in new code.
  271. See: https://lore.kernel.org/lkml/5eb299021dec23c1a48fa7d9f2c8b794e967766d.1408730669.git.luto@amacapital.net/
  272. **ENOTSUPP**
  273. ENOTSUPP is not a standard error code and should be avoided in new patches.
  274. EOPNOTSUPP should be used instead.
  275. See: https://lore.kernel.org/netdev/[email protected]/
  276. **EXPORT_SYMBOL**
  277. EXPORT_SYMBOL should immediately follow the symbol to be exported.
  278. **IN_ATOMIC**
  279. in_atomic() is not for driver use so any such use is reported as an ERROR.
  280. Also in_atomic() is often used to determine if sleeping is permitted,
  281. but it is not reliable in this use model. Therefore its use is
  282. strongly discouraged.
  283. However, in_atomic() is ok for core kernel use.
  284. See: https://lore.kernel.org/lkml/[email protected]/
  285. **LOCKDEP**
  286. The lockdep_no_validate class was added as a temporary measure to
  287. prevent warnings on conversion of device->sem to device->mutex.
  288. It should not be used for any other purpose.
  289. See: https://lore.kernel.org/lkml/1268959062.9440.467.camel@laptop/
  290. **MALFORMED_INCLUDE**
  291. The #include statement has a malformed path. This has happened
  292. because the author has included a double slash "//" in the pathname
  293. accidentally.
  294. **USE_LOCKDEP**
  295. lockdep_assert_held() annotations should be preferred over
  296. assertions based on spin_is_locked()
  297. See: https://www.kernel.org/doc/html/latest/locking/lockdep-design.html#annotations
  298. **UAPI_INCLUDE**
  299. No #include statements in include/uapi should use a uapi/ path.
  300. **USLEEP_RANGE**
  301. usleep_range() should be preferred over udelay(). The proper way of
  302. using usleep_range() is mentioned in the kernel docs.
  303. See: https://www.kernel.org/doc/html/latest/timers/timers-howto.html#delays-information-on-the-various-kernel-delay-sleep-mechanisms
  304. Comments
  305. --------
  306. **BLOCK_COMMENT_STYLE**
  307. The comment style is incorrect. The preferred style for multi-
  308. line comments is::
  309. /*
  310. * This is the preferred style
  311. * for multi line comments.
  312. */
  313. The networking comment style is a bit different, with the first line
  314. not empty like the former::
  315. /* This is the preferred comment style
  316. * for files in net/ and drivers/net/
  317. */
  318. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#commenting
  319. **C99_COMMENTS**
  320. C99 style single line comments (//) should not be used.
  321. Prefer the block comment style instead.
  322. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#commenting
  323. **DATA_RACE**
  324. Applications of data_race() should have a comment so as to document the
  325. reasoning behind why it was deemed safe.
  326. See: https://lore.kernel.org/lkml/[email protected]/
  327. **FSF_MAILING_ADDRESS**
  328. Kernel maintainers reject new instances of the GPL boilerplate paragraph
  329. directing people to write to the FSF for a copy of the GPL, since the
  330. FSF has moved in the past and may do so again.
  331. So do not write paragraphs about writing to the Free Software Foundation's
  332. mailing address.
  333. See: https://lore.kernel.org/lkml/20131006222342.GT19510@leaf/
  334. Commit message
  335. --------------
  336. **BAD_SIGN_OFF**
  337. The signed-off-by line does not fall in line with the standards
  338. specified by the community.
  339. See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#developer-s-certificate-of-origin-1-1
  340. **BAD_STABLE_ADDRESS_STYLE**
  341. The email format for stable is incorrect.
  342. Some valid options for stable address are::
  343. 1. [email protected]
  344. 2. [email protected]
  345. For adding version info, the following comment style should be used::
  346. [email protected] # version info
  347. **COMMIT_COMMENT_SYMBOL**
  348. Commit log lines starting with a '#' are ignored by git as
  349. comments. To solve this problem addition of a single space
  350. infront of the log line is enough.
  351. **COMMIT_MESSAGE**
  352. The patch is missing a commit description. A brief
  353. description of the changes made by the patch should be added.
  354. See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
  355. **EMAIL_SUBJECT**
  356. Naming the tool that found the issue is not very useful in the
  357. subject line. A good subject line summarizes the change that
  358. the patch brings.
  359. See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
  360. **FROM_SIGN_OFF_MISMATCH**
  361. The author's email does not match with that in the Signed-off-by:
  362. line(s). This can be sometimes caused due to an improperly configured
  363. email client.
  364. This message is emitted due to any of the following reasons::
  365. - The email names do not match.
  366. - The email addresses do not match.
  367. - The email subaddresses do not match.
  368. - The email comments do not match.
  369. **MISSING_SIGN_OFF**
  370. The patch is missing a Signed-off-by line. A signed-off-by
  371. line should be added according to Developer's certificate of
  372. Origin.
  373. See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#sign-your-work-the-developer-s-certificate-of-origin
  374. **NO_AUTHOR_SIGN_OFF**
  375. The author of the patch has not signed off the patch. It is
  376. required that a simple sign off line should be present at the
  377. end of explanation of the patch to denote that the author has
  378. written it or otherwise has the rights to pass it on as an open
  379. source patch.
  380. See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#sign-your-work-the-developer-s-certificate-of-origin
  381. **DIFF_IN_COMMIT_MSG**
  382. Avoid having diff content in commit message.
  383. This causes problems when one tries to apply a file containing both
  384. the changelog and the diff because patch(1) tries to apply the diff
  385. which it found in the changelog.
  386. See: https://lore.kernel.org/lkml/[email protected]/
  387. **GERRIT_CHANGE_ID**
  388. To be picked up by gerrit, the footer of the commit message might
  389. have a Change-Id like::
  390. Change-Id: Ic8aaa0728a43936cd4c6e1ed590e01ba8f0fbf5b
  391. Signed-off-by: A. U. Thor <[email protected]>
  392. The Change-Id line must be removed before submitting.
  393. **GIT_COMMIT_ID**
  394. The proper way to reference a commit id is:
  395. commit <12+ chars of sha1> ("<title line>")
  396. An example may be::
  397. Commit e21d2170f36602ae2708 ("video: remove unnecessary
  398. platform_set_drvdata()") removed the unnecessary
  399. platform_set_drvdata(), but left the variable "dev" unused,
  400. delete it.
  401. See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
  402. **BAD_FIXES_TAG**
  403. The Fixes: tag is malformed or does not follow the community conventions.
  404. This can occur if the tag have been split into multiple lines (e.g., when
  405. pasted in an email program with word wrapping enabled).
  406. See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
  407. Comparison style
  408. ----------------
  409. **ASSIGN_IN_IF**
  410. Do not use assignments in if condition.
  411. Example::
  412. if ((foo = bar(...)) < BAZ) {
  413. should be written as::
  414. foo = bar(...);
  415. if (foo < BAZ) {
  416. **BOOL_COMPARISON**
  417. Comparisons of A to true and false are better written
  418. as A and !A.
  419. See: https://lore.kernel.org/lkml/1365563834.27174.12.camel@joe-AO722/
  420. **COMPARISON_TO_NULL**
  421. Comparisons to NULL in the form (foo == NULL) or (foo != NULL)
  422. are better written as (!foo) and (foo).
  423. **CONSTANT_COMPARISON**
  424. Comparisons with a constant or upper case identifier on the left
  425. side of the test should be avoided.
  426. Indentation and Line Breaks
  427. ---------------------------
  428. **CODE_INDENT**
  429. Code indent should use tabs instead of spaces.
  430. Outside of comments, documentation and Kconfig,
  431. spaces are never used for indentation.
  432. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#indentation
  433. **DEEP_INDENTATION**
  434. Indentation with 6 or more tabs usually indicate overly indented
  435. code.
  436. It is suggested to refactor excessive indentation of
  437. if/else/for/do/while/switch statements.
  438. See: https://lore.kernel.org/lkml/1328311239.21255.24.camel@joe2Laptop/
  439. **SWITCH_CASE_INDENT_LEVEL**
  440. switch should be at the same indent as case.
  441. Example::
  442. switch (suffix) {
  443. case 'G':
  444. case 'g':
  445. mem <<= 30;
  446. break;
  447. case 'M':
  448. case 'm':
  449. mem <<= 20;
  450. break;
  451. case 'K':
  452. case 'k':
  453. mem <<= 10;
  454. fallthrough;
  455. default:
  456. break;
  457. }
  458. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#indentation
  459. **LONG_LINE**
  460. The line has exceeded the specified maximum length.
  461. To use a different maximum line length, the --max-line-length=n option
  462. may be added while invoking checkpatch.
  463. Earlier, the default line length was 80 columns. Commit bdc48fa11e46
  464. ("checkpatch/coding-style: deprecate 80-column warning") increased the
  465. limit to 100 columns. This is not a hard limit either and it's
  466. preferable to stay within 80 columns whenever possible.
  467. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#breaking-long-lines-and-strings
  468. **LONG_LINE_STRING**
  469. A string starts before but extends beyond the maximum line length.
  470. To use a different maximum line length, the --max-line-length=n option
  471. may be added while invoking checkpatch.
  472. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#breaking-long-lines-and-strings
  473. **LONG_LINE_COMMENT**
  474. A comment starts before but extends beyond the maximum line length.
  475. To use a different maximum line length, the --max-line-length=n option
  476. may be added while invoking checkpatch.
  477. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#breaking-long-lines-and-strings
  478. **SPLIT_STRING**
  479. Quoted strings that appear as messages in userspace and can be
  480. grepped, should not be split across multiple lines.
  481. See: https://lore.kernel.org/lkml/20120203052727.GA15035@leaf/
  482. **MULTILINE_DEREFERENCE**
  483. A single dereferencing identifier spanned on multiple lines like::
  484. struct_identifier->member[index].
  485. member = <foo>;
  486. is generally hard to follow. It can easily lead to typos and so makes
  487. the code vulnerable to bugs.
  488. If fixing the multiple line dereferencing leads to an 80 column
  489. violation, then either rewrite the code in a more simple way or if the
  490. starting part of the dereferencing identifier is the same and used at
  491. multiple places then store it in a temporary variable, and use that
  492. temporary variable only at all the places. For example, if there are
  493. two dereferencing identifiers::
  494. member1->member2->member3.foo1;
  495. member1->member2->member3.foo2;
  496. then store the member1->member2->member3 part in a temporary variable.
  497. It not only helps to avoid the 80 column violation but also reduces
  498. the program size by removing the unnecessary dereferences.
  499. But if none of the above methods work then ignore the 80 column
  500. violation because it is much easier to read a dereferencing identifier
  501. on a single line.
  502. **TRAILING_STATEMENTS**
  503. Trailing statements (for example after any conditional) should be
  504. on the next line.
  505. Statements, such as::
  506. if (x == y) break;
  507. should be::
  508. if (x == y)
  509. break;
  510. Macros, Attributes and Symbols
  511. ------------------------------
  512. **ARRAY_SIZE**
  513. The ARRAY_SIZE(foo) macro should be preferred over
  514. sizeof(foo)/sizeof(foo[0]) for finding number of elements in an
  515. array.
  516. The macro is defined in include/linux/kernel.h::
  517. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  518. **AVOID_EXTERNS**
  519. Function prototypes don't need to be declared extern in .h
  520. files. It's assumed by the compiler and is unnecessary.
  521. **AVOID_L_PREFIX**
  522. Local symbol names that are prefixed with `.L` should be avoided,
  523. as this has special meaning for the assembler; a symbol entry will
  524. not be emitted into the symbol table. This can prevent `objtool`
  525. from generating correct unwind info.
  526. Symbols with STB_LOCAL binding may still be used, and `.L` prefixed
  527. local symbol names are still generally usable within a function,
  528. but `.L` prefixed local symbol names should not be used to denote
  529. the beginning or end of code regions via
  530. `SYM_CODE_START_LOCAL`/`SYM_CODE_END`
  531. **BIT_MACRO**
  532. Defines like: 1 << <digit> could be BIT(digit).
  533. The BIT() macro is defined via include/linux/bits.h::
  534. #define BIT(nr) (1UL << (nr))
  535. **CONST_READ_MOSTLY**
  536. When a variable is tagged with the __read_mostly annotation, it is a
  537. signal to the compiler that accesses to the variable will be mostly
  538. reads and rarely(but NOT never) a write.
  539. const __read_mostly does not make any sense as const data is already
  540. read-only. The __read_mostly annotation thus should be removed.
  541. **DATE_TIME**
  542. It is generally desirable that building the same source code with
  543. the same set of tools is reproducible, i.e. the output is always
  544. exactly the same.
  545. The kernel does *not* use the ``__DATE__`` and ``__TIME__`` macros,
  546. and enables warnings if they are used as they can lead to
  547. non-deterministic builds.
  548. See: https://www.kernel.org/doc/html/latest/kbuild/reproducible-builds.html#timestamps
  549. **DEFINE_ARCH_HAS**
  550. The ARCH_HAS_xyz and ARCH_HAVE_xyz patterns are wrong.
  551. For big conceptual features use Kconfig symbols instead. And for
  552. smaller things where we have compatibility fallback functions but
  553. want architectures able to override them with optimized ones, we
  554. should either use weak functions (appropriate for some cases), or
  555. the symbol that protects them should be the same symbol we use.
  556. See: https://lore.kernel.org/lkml/CA+55aFycQ9XJvEOsiM3txHL5bjUc8CeKWJNR_H+MiicaddB42Q@mail.gmail.com/
  557. **DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON**
  558. do {} while(0) macros should not have a trailing semicolon.
  559. **INIT_ATTRIBUTE**
  560. Const init definitions should use __initconst instead of
  561. __initdata.
  562. Similarly init definitions without const require a separate
  563. use of const.
  564. **INLINE_LOCATION**
  565. The inline keyword should sit between storage class and type.
  566. For example, the following segment::
  567. inline static int example_function(void)
  568. {
  569. ...
  570. }
  571. should be::
  572. static inline int example_function(void)
  573. {
  574. ...
  575. }
  576. **MISPLACED_INIT**
  577. It is possible to use section markers on variables in a way
  578. which gcc doesn't understand (or at least not the way the
  579. developer intended)::
  580. static struct __initdata samsung_pll_clock exynos4_plls[nr_plls] = {
  581. does not put exynos4_plls in the .initdata section. The __initdata
  582. marker can be virtually anywhere on the line, except right after
  583. "struct". The preferred location is before the "=" sign if there is
  584. one, or before the trailing ";" otherwise.
  585. See: https://lore.kernel.org/lkml/1377655732.3619.19.camel@joe-AO722/
  586. **MULTISTATEMENT_MACRO_USE_DO_WHILE**
  587. Macros with multiple statements should be enclosed in a
  588. do - while block. Same should also be the case for macros
  589. starting with `if` to avoid logic defects::
  590. #define macrofun(a, b, c) \
  591. do { \
  592. if (a == 5) \
  593. do_this(b, c); \
  594. } while (0)
  595. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#macros-enums-and-rtl
  596. **PREFER_FALLTHROUGH**
  597. Use the `fallthrough;` pseudo keyword instead of
  598. `/* fallthrough */` like comments.
  599. **TRAILING_SEMICOLON**
  600. Macro definition should not end with a semicolon. The macro
  601. invocation style should be consistent with function calls.
  602. This can prevent any unexpected code paths::
  603. #define MAC do_something;
  604. If this macro is used within a if else statement, like::
  605. if (some_condition)
  606. MAC;
  607. else
  608. do_something;
  609. Then there would be a compilation error, because when the macro is
  610. expanded there are two trailing semicolons, so the else branch gets
  611. orphaned.
  612. See: https://lore.kernel.org/lkml/1399671106.2912.21.camel@joe-AO725/
  613. **SINGLE_STATEMENT_DO_WHILE_MACRO**
  614. For the multi-statement macros, it is necessary to use the do-while
  615. loop to avoid unpredictable code paths. The do-while loop helps to
  616. group the multiple statements into a single one so that a
  617. function-like macro can be used as a function only.
  618. But for the single statement macros, it is unnecessary to use the
  619. do-while loop. Although the code is syntactically correct but using
  620. the do-while loop is redundant. So remove the do-while loop for single
  621. statement macros.
  622. **WEAK_DECLARATION**
  623. Using weak declarations like __attribute__((weak)) or __weak
  624. can have unintended link defects. Avoid using them.
  625. Functions and Variables
  626. -----------------------
  627. **CAMELCASE**
  628. Avoid CamelCase Identifiers.
  629. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#naming
  630. **CONST_CONST**
  631. Using `const <type> const *` is generally meant to be
  632. written `const <type> * const`.
  633. **CONST_STRUCT**
  634. Using const is generally a good idea. Checkpatch reads
  635. a list of frequently used structs that are always or
  636. almost always constant.
  637. The existing structs list can be viewed from
  638. `scripts/const_structs.checkpatch`.
  639. See: https://lore.kernel.org/lkml/alpine.DEB.2.10.1608281509480.3321@hadrien/
  640. **EMBEDDED_FUNCTION_NAME**
  641. Embedded function names are less appropriate to use as
  642. refactoring can cause function renaming. Prefer the use of
  643. "%s", __func__ to embedded function names.
  644. Note that this does not work with -f (--file) checkpatch option
  645. as it depends on patch context providing the function name.
  646. **FUNCTION_ARGUMENTS**
  647. This warning is emitted due to any of the following reasons:
  648. 1. Arguments for the function declaration do not follow
  649. the identifier name. Example::
  650. void foo
  651. (int bar, int baz)
  652. This should be corrected to::
  653. void foo(int bar, int baz)
  654. 2. Some arguments for the function definition do not
  655. have an identifier name. Example::
  656. void foo(int)
  657. All arguments should have identifier names.
  658. **FUNCTION_WITHOUT_ARGS**
  659. Function declarations without arguments like::
  660. int foo()
  661. should be::
  662. int foo(void)
  663. **GLOBAL_INITIALISERS**
  664. Global variables should not be initialized explicitly to
  665. 0 (or NULL, false, etc.). Your compiler (or rather your
  666. loader, which is responsible for zeroing out the relevant
  667. sections) automatically does it for you.
  668. **INITIALISED_STATIC**
  669. Static variables should not be initialized explicitly to zero.
  670. Your compiler (or rather your loader) automatically does
  671. it for you.
  672. **MULTIPLE_ASSIGNMENTS**
  673. Multiple assignments on a single line makes the code unnecessarily
  674. complicated. So on a single line assign value to a single variable
  675. only, this makes the code more readable and helps avoid typos.
  676. **RETURN_PARENTHESES**
  677. return is not a function and as such doesn't need parentheses::
  678. return (bar);
  679. can simply be::
  680. return bar;
  681. Permissions
  682. -----------
  683. **DEVICE_ATTR_PERMS**
  684. The permissions used in DEVICE_ATTR are unusual.
  685. Typically only three permissions are used - 0644 (RW), 0444 (RO)
  686. and 0200 (WO).
  687. See: https://www.kernel.org/doc/html/latest/filesystems/sysfs.html#attributes
  688. **EXECUTE_PERMISSIONS**
  689. There is no reason for source files to be executable. The executable
  690. bit can be removed safely.
  691. **EXPORTED_WORLD_WRITABLE**
  692. Exporting world writable sysfs/debugfs files is usually a bad thing.
  693. When done arbitrarily they can introduce serious security bugs.
  694. In the past, some of the debugfs vulnerabilities would seemingly allow
  695. any local user to write arbitrary values into device registers - a
  696. situation from which little good can be expected to emerge.
  697. See: https://lore.kernel.org/linux-arm-kernel/[email protected]/
  698. **NON_OCTAL_PERMISSIONS**
  699. Permission bits should use 4 digit octal permissions (like 0700 or 0444).
  700. Avoid using any other base like decimal.
  701. **SYMBOLIC_PERMS**
  702. Permission bits in the octal form are more readable and easier to
  703. understand than their symbolic counterparts because many command-line
  704. tools use this notation. Experienced kernel developers have been using
  705. these traditional Unix permission bits for decades and so they find it
  706. easier to understand the octal notation than the symbolic macros.
  707. For example, it is harder to read S_IWUSR|S_IRUGO than 0644, which
  708. obscures the developer's intent rather than clarifying it.
  709. See: https://lore.kernel.org/lkml/CA+55aFw5v23T-zvDZp-MmD_EYxF8WbafwwB59934FV7g21uMGQ@mail.gmail.com/
  710. Spacing and Brackets
  711. --------------------
  712. **ASSIGNMENT_CONTINUATIONS**
  713. Assignment operators should not be written at the start of a
  714. line but should follow the operand at the previous line.
  715. **BRACES**
  716. The placement of braces is stylistically incorrect.
  717. The preferred way is to put the opening brace last on the line,
  718. and put the closing brace first::
  719. if (x is true) {
  720. we do y
  721. }
  722. This applies for all non-functional blocks.
  723. However, there is one special case, namely functions: they have the
  724. opening brace at the beginning of the next line, thus::
  725. int function(int x)
  726. {
  727. body of function
  728. }
  729. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#placing-braces-and-spaces
  730. **BRACKET_SPACE**
  731. Whitespace before opening bracket '[' is prohibited.
  732. There are some exceptions:
  733. 1. With a type on the left::
  734. int [] a;
  735. 2. At the beginning of a line for slice initialisers::
  736. [0...10] = 5,
  737. 3. Inside a curly brace::
  738. = { [0...10] = 5 }
  739. **CONCATENATED_STRING**
  740. Concatenated elements should have a space in between.
  741. Example::
  742. printk(KERN_INFO"bar");
  743. should be::
  744. printk(KERN_INFO "bar");
  745. **ELSE_AFTER_BRACE**
  746. `else {` should follow the closing block `}` on the same line.
  747. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#placing-braces-and-spaces
  748. **LINE_SPACING**
  749. Vertical space is wasted given the limited number of lines an
  750. editor window can display when multiple blank lines are used.
  751. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#spaces
  752. **OPEN_BRACE**
  753. The opening brace should be following the function definitions on the
  754. next line. For any non-functional block it should be on the same line
  755. as the last construct.
  756. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#placing-braces-and-spaces
  757. **POINTER_LOCATION**
  758. When using pointer data or a function that returns a pointer type,
  759. the preferred use of * is adjacent to the data name or function name
  760. and not adjacent to the type name.
  761. Examples::
  762. char *linux_banner;
  763. unsigned long long memparse(char *ptr, char **retptr);
  764. char *match_strdup(substring_t *s);
  765. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#spaces
  766. **SPACING**
  767. Whitespace style used in the kernel sources is described in kernel docs.
  768. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#spaces
  769. **TRAILING_WHITESPACE**
  770. Trailing whitespace should always be removed.
  771. Some editors highlight the trailing whitespace and cause visual
  772. distractions when editing files.
  773. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#spaces
  774. **UNNECESSARY_PARENTHESES**
  775. Parentheses are not required in the following cases:
  776. 1. Function pointer uses::
  777. (foo->bar)();
  778. could be::
  779. foo->bar();
  780. 2. Comparisons in if::
  781. if ((foo->bar) && (foo->baz))
  782. if ((foo == bar))
  783. could be::
  784. if (foo->bar && foo->baz)
  785. if (foo == bar)
  786. 3. addressof/dereference single Lvalues::
  787. &(foo->bar)
  788. *(foo->bar)
  789. could be::
  790. &foo->bar
  791. *foo->bar
  792. **WHILE_AFTER_BRACE**
  793. while should follow the closing bracket on the same line::
  794. do {
  795. ...
  796. } while(something);
  797. See: https://www.kernel.org/doc/html/latest/process/coding-style.html#placing-braces-and-spaces
  798. Others
  799. ------
  800. **CONFIG_DESCRIPTION**
  801. Kconfig symbols should have a help text which fully describes
  802. it.
  803. **CORRUPTED_PATCH**
  804. The patch seems to be corrupted or lines are wrapped.
  805. Please regenerate the patch file before sending it to the maintainer.
  806. **CVS_KEYWORD**
  807. Since linux moved to git, the CVS markers are no longer used.
  808. So, CVS style keywords ($Id$, $Revision$, $Log$) should not be
  809. added.
  810. **DEFAULT_NO_BREAK**
  811. switch default case is sometimes written as "default:;". This can
  812. cause new cases added below default to be defective.
  813. A "break;" should be added after empty default statement to avoid
  814. unwanted fallthrough.
  815. **DOS_LINE_ENDINGS**
  816. For DOS-formatted patches, there are extra ^M symbols at the end of
  817. the line. These should be removed.
  818. **DT_SCHEMA_BINDING_PATCH**
  819. DT bindings moved to a json-schema based format instead of
  820. freeform text.
  821. See: https://www.kernel.org/doc/html/latest/devicetree/bindings/writing-schema.html
  822. **DT_SPLIT_BINDING_PATCH**
  823. Devicetree bindings should be their own patch. This is because
  824. bindings are logically independent from a driver implementation,
  825. they have a different maintainer (even though they often
  826. are applied via the same tree), and it makes for a cleaner history in the
  827. DT only tree created with git-filter-branch.
  828. See: https://www.kernel.org/doc/html/latest/devicetree/bindings/submitting-patches.html#i-for-patch-submitters
  829. **EMBEDDED_FILENAME**
  830. Embedding the complete filename path inside the file isn't particularly
  831. useful as often the path is moved around and becomes incorrect.
  832. **FILE_PATH_CHANGES**
  833. Whenever files are added, moved, or deleted, the MAINTAINERS file
  834. patterns can be out of sync or outdated.
  835. So MAINTAINERS might need updating in these cases.
  836. **MEMSET**
  837. The memset use appears to be incorrect. This may be caused due to
  838. badly ordered parameters. Please recheck the usage.
  839. **NOT_UNIFIED_DIFF**
  840. The patch file does not appear to be in unified-diff format. Please
  841. regenerate the patch file before sending it to the maintainer.
  842. **PRINTF_0XDECIMAL**
  843. Prefixing 0x with decimal output is defective and should be corrected.
  844. **SPDX_LICENSE_TAG**
  845. The source file is missing or has an improper SPDX identifier tag.
  846. The Linux kernel requires the precise SPDX identifier in all source files,
  847. and it is thoroughly documented in the kernel docs.
  848. See: https://www.kernel.org/doc/html/latest/process/license-rules.html
  849. **TYPO_SPELLING**
  850. Some words may have been misspelled. Consider reviewing them.