12eecbab8SDwaipayan Ray.. SPDX-License-Identifier: GPL-2.0-only
22eecbab8SDwaipayan Ray
32eecbab8SDwaipayan Ray==========
42eecbab8SDwaipayan RayCheckpatch
52eecbab8SDwaipayan Ray==========
62eecbab8SDwaipayan Ray
72eecbab8SDwaipayan RayCheckpatch (scripts/checkpatch.pl) is a perl script which checks for trivial
82eecbab8SDwaipayan Raystyle violations in patches and optionally corrects them.  Checkpatch can
92eecbab8SDwaipayan Rayalso be run on file contexts and without the kernel tree.
102eecbab8SDwaipayan Ray
112eecbab8SDwaipayan RayCheckpatch is not always right. Your judgement takes precedence over checkpatch
122eecbab8SDwaipayan Raymessages.  If your code looks better with the violations, then its probably
132eecbab8SDwaipayan Raybest left alone.
142eecbab8SDwaipayan Ray
152eecbab8SDwaipayan Ray
162eecbab8SDwaipayan RayOptions
172eecbab8SDwaipayan Ray=======
182eecbab8SDwaipayan Ray
192eecbab8SDwaipayan RayThis section will describe the options checkpatch can be run with.
202eecbab8SDwaipayan Ray
212eecbab8SDwaipayan RayUsage::
222eecbab8SDwaipayan Ray
232eecbab8SDwaipayan Ray  ./scripts/checkpatch.pl [OPTION]... [FILE]...
242eecbab8SDwaipayan Ray
252eecbab8SDwaipayan RayAvailable options:
262eecbab8SDwaipayan Ray
272eecbab8SDwaipayan Ray - -q,  --quiet
282eecbab8SDwaipayan Ray
292eecbab8SDwaipayan Ray   Enable quiet mode.
302eecbab8SDwaipayan Ray
312eecbab8SDwaipayan Ray - -v,  --verbose
322eecbab8SDwaipayan Ray   Enable verbose mode.  Additional verbose test descriptions are output
332eecbab8SDwaipayan Ray   so as to provide information on why that particular message is shown.
342eecbab8SDwaipayan Ray
352eecbab8SDwaipayan Ray - --no-tree
362eecbab8SDwaipayan Ray
372eecbab8SDwaipayan Ray   Run checkpatch without the kernel tree.
382eecbab8SDwaipayan Ray
392eecbab8SDwaipayan Ray - --no-signoff
402eecbab8SDwaipayan Ray
412eecbab8SDwaipayan Ray   Disable the 'Signed-off-by' line check.  The sign-off is a simple line at
422eecbab8SDwaipayan Ray   the end of the explanation for the patch, which certifies that you wrote it
432eecbab8SDwaipayan Ray   or otherwise have the right to pass it on as an open-source patch.
442eecbab8SDwaipayan Ray
452eecbab8SDwaipayan Ray   Example::
462eecbab8SDwaipayan Ray
472eecbab8SDwaipayan Ray	 Signed-off-by: Random J Developer <[email protected]>
482eecbab8SDwaipayan Ray
492eecbab8SDwaipayan Ray   Setting this flag effectively stops a message for a missing signed-off-by
502eecbab8SDwaipayan Ray   line in a patch context.
512eecbab8SDwaipayan Ray
522eecbab8SDwaipayan Ray - --patch
532eecbab8SDwaipayan Ray
542eecbab8SDwaipayan Ray   Treat FILE as a patch.  This is the default option and need not be
552eecbab8SDwaipayan Ray   explicitly specified.
562eecbab8SDwaipayan Ray
572eecbab8SDwaipayan Ray - --emacs
582eecbab8SDwaipayan Ray
592eecbab8SDwaipayan Ray   Set output to emacs compile window format.  This allows emacs users to jump
602eecbab8SDwaipayan Ray   from the error in the compile window directly to the offending line in the
612eecbab8SDwaipayan Ray   patch.
622eecbab8SDwaipayan Ray
632eecbab8SDwaipayan Ray - --terse
642eecbab8SDwaipayan Ray
652eecbab8SDwaipayan Ray   Output only one line per report.
662eecbab8SDwaipayan Ray
672eecbab8SDwaipayan Ray - --showfile
682eecbab8SDwaipayan Ray
692eecbab8SDwaipayan Ray   Show the diffed file position instead of the input file position.
702eecbab8SDwaipayan Ray
712eecbab8SDwaipayan Ray - -g,  --git
722eecbab8SDwaipayan Ray
732eecbab8SDwaipayan Ray   Treat FILE as a single commit or a git revision range.
742eecbab8SDwaipayan Ray
752eecbab8SDwaipayan Ray   Single commit with:
762eecbab8SDwaipayan Ray
772eecbab8SDwaipayan Ray   - <rev>
782eecbab8SDwaipayan Ray   - <rev>^
792eecbab8SDwaipayan Ray   - <rev>~n
802eecbab8SDwaipayan Ray
812eecbab8SDwaipayan Ray   Multiple commits with:
822eecbab8SDwaipayan Ray
832eecbab8SDwaipayan Ray   - <rev1>..<rev2>
842eecbab8SDwaipayan Ray   - <rev1>...<rev2>
852eecbab8SDwaipayan Ray   - <rev>-<count>
862eecbab8SDwaipayan Ray
872eecbab8SDwaipayan Ray - -f,  --file
882eecbab8SDwaipayan Ray
892eecbab8SDwaipayan Ray   Treat FILE as a regular source file.  This option must be used when running
902eecbab8SDwaipayan Ray   checkpatch on source files in the kernel.
912eecbab8SDwaipayan Ray
922eecbab8SDwaipayan Ray - --subjective,  --strict
932eecbab8SDwaipayan Ray
942eecbab8SDwaipayan Ray   Enable stricter tests in checkpatch.  By default the tests emitted as CHECK
952eecbab8SDwaipayan Ray   do not activate by default.  Use this flag to activate the CHECK tests.
962eecbab8SDwaipayan Ray
972eecbab8SDwaipayan Ray - --list-types
982eecbab8SDwaipayan Ray
992eecbab8SDwaipayan Ray   Every message emitted by checkpatch has an associated TYPE.  Add this flag
1002eecbab8SDwaipayan Ray   to display all the types in checkpatch.
1012eecbab8SDwaipayan Ray
1022eecbab8SDwaipayan Ray   Note that when this flag is active, checkpatch does not read the input FILE,
1032eecbab8SDwaipayan Ray   and no message is emitted.  Only a list of types in checkpatch is output.
1042eecbab8SDwaipayan Ray
1052eecbab8SDwaipayan Ray - --types TYPE(,TYPE2...)
1062eecbab8SDwaipayan Ray
1072eecbab8SDwaipayan Ray   Only display messages with the given types.
1082eecbab8SDwaipayan Ray
1092eecbab8SDwaipayan Ray   Example::
1102eecbab8SDwaipayan Ray
1112eecbab8SDwaipayan Ray     ./scripts/checkpatch.pl mypatch.patch --types EMAIL_SUBJECT,BRACES
1122eecbab8SDwaipayan Ray
1132eecbab8SDwaipayan Ray - --ignore TYPE(,TYPE2...)
1142eecbab8SDwaipayan Ray
1152eecbab8SDwaipayan Ray   Checkpatch will not emit messages for the specified types.
1162eecbab8SDwaipayan Ray
1172eecbab8SDwaipayan Ray   Example::
1182eecbab8SDwaipayan Ray
1192eecbab8SDwaipayan Ray     ./scripts/checkpatch.pl mypatch.patch --ignore EMAIL_SUBJECT,BRACES
1202eecbab8SDwaipayan Ray
1212eecbab8SDwaipayan Ray - --show-types
1222eecbab8SDwaipayan Ray
1232eecbab8SDwaipayan Ray   By default checkpatch doesn't display the type associated with the messages.
1242eecbab8SDwaipayan Ray   Set this flag to show the message type in the output.
1252eecbab8SDwaipayan Ray
1262eecbab8SDwaipayan Ray - --max-line-length=n
1272eecbab8SDwaipayan Ray
1282eecbab8SDwaipayan Ray   Set the max line length (default 100).  If a line exceeds the specified
1292eecbab8SDwaipayan Ray   length, a LONG_LINE message is emitted.
1302eecbab8SDwaipayan Ray
1312eecbab8SDwaipayan Ray
1322eecbab8SDwaipayan Ray   The message level is different for patch and file contexts.  For patches,
1332eecbab8SDwaipayan Ray   a WARNING is emitted.  While a milder CHECK is emitted for files.  So for
1342eecbab8SDwaipayan Ray   file contexts, the --strict flag must also be enabled.
1352eecbab8SDwaipayan Ray
1362eecbab8SDwaipayan Ray - --min-conf-desc-length=n
1372eecbab8SDwaipayan Ray
1382eecbab8SDwaipayan Ray   Set the Kconfig entry minimum description length, if shorter, warn.
1392eecbab8SDwaipayan Ray
1402eecbab8SDwaipayan Ray - --tab-size=n
1412eecbab8SDwaipayan Ray
1422eecbab8SDwaipayan Ray   Set the number of spaces for tab (default 8).
1432eecbab8SDwaipayan Ray
1442eecbab8SDwaipayan Ray - --root=PATH
1452eecbab8SDwaipayan Ray
1462eecbab8SDwaipayan Ray   PATH to the kernel tree root.
1472eecbab8SDwaipayan Ray
1482eecbab8SDwaipayan Ray   This option must be specified when invoking checkpatch from outside
1492eecbab8SDwaipayan Ray   the kernel root.
1502eecbab8SDwaipayan Ray
1512eecbab8SDwaipayan Ray - --no-summary
1522eecbab8SDwaipayan Ray
1532eecbab8SDwaipayan Ray   Suppress the per file summary.
1542eecbab8SDwaipayan Ray
1552eecbab8SDwaipayan Ray - --mailback
1562eecbab8SDwaipayan Ray
1572eecbab8SDwaipayan Ray   Only produce a report in case of Warnings or Errors.  Milder Checks are
1582eecbab8SDwaipayan Ray   excluded from this.
1592eecbab8SDwaipayan Ray
1602eecbab8SDwaipayan Ray - --summary-file
1612eecbab8SDwaipayan Ray
1622eecbab8SDwaipayan Ray   Include the filename in summary.
1632eecbab8SDwaipayan Ray
1642eecbab8SDwaipayan Ray - --debug KEY=[0|1]
1652eecbab8SDwaipayan Ray
1662eecbab8SDwaipayan Ray   Turn on/off debugging of KEY, where KEY is one of 'values', 'possible',
1672eecbab8SDwaipayan Ray   'type', and 'attr' (default is all off).
1682eecbab8SDwaipayan Ray
1692eecbab8SDwaipayan Ray - --fix
1702eecbab8SDwaipayan Ray
1716fca09b6SThorsten Blum   This is an EXPERIMENTAL feature.  If correctable errors exist, a file
1722eecbab8SDwaipayan Ray   <inputfile>.EXPERIMENTAL-checkpatch-fixes is created which has the
1732eecbab8SDwaipayan Ray   automatically fixable errors corrected.
1742eecbab8SDwaipayan Ray
1752eecbab8SDwaipayan Ray - --fix-inplace
1762eecbab8SDwaipayan Ray
1772eecbab8SDwaipayan Ray   EXPERIMENTAL - Similar to --fix but input file is overwritten with fixes.
1782eecbab8SDwaipayan Ray
1792eecbab8SDwaipayan Ray   DO NOT USE this flag unless you are absolutely sure and you have a backup
1802eecbab8SDwaipayan Ray   in place.
1812eecbab8SDwaipayan Ray
1822eecbab8SDwaipayan Ray - --ignore-perl-version
1832eecbab8SDwaipayan Ray
1842eecbab8SDwaipayan Ray   Override checking of perl version.  Runtime errors may be encountered after
1852eecbab8SDwaipayan Ray   enabling this flag if the perl version does not meet the minimum specified.
1862eecbab8SDwaipayan Ray
1872eecbab8SDwaipayan Ray - --codespell
1882eecbab8SDwaipayan Ray
1892eecbab8SDwaipayan Ray   Use the codespell dictionary for checking spelling errors.
1902eecbab8SDwaipayan Ray
1912eecbab8SDwaipayan Ray - --codespellfile
1922eecbab8SDwaipayan Ray
1932eecbab8SDwaipayan Ray   Use the specified codespell file.
1942eecbab8SDwaipayan Ray   Default is '/usr/share/codespell/dictionary.txt'.
1952eecbab8SDwaipayan Ray
1962eecbab8SDwaipayan Ray - --typedefsfile
1972eecbab8SDwaipayan Ray
1982eecbab8SDwaipayan Ray   Read additional types from this file.
1992eecbab8SDwaipayan Ray
2002eecbab8SDwaipayan Ray - --color[=WHEN]
2012eecbab8SDwaipayan Ray
2022eecbab8SDwaipayan Ray   Use colors 'always', 'never', or only when output is a terminal ('auto').
2032eecbab8SDwaipayan Ray   Default is 'auto'.
2042eecbab8SDwaipayan Ray
2052eecbab8SDwaipayan Ray - --kconfig-prefix=WORD
2062eecbab8SDwaipayan Ray
2072eecbab8SDwaipayan Ray   Use WORD as a prefix for Kconfig symbols (default is `CONFIG_`).
2082eecbab8SDwaipayan Ray
2092eecbab8SDwaipayan Ray - -h, --help, --version
2102eecbab8SDwaipayan Ray
2112eecbab8SDwaipayan Ray   Display the help text.
2122eecbab8SDwaipayan Ray
2132eecbab8SDwaipayan RayMessage Levels
2142eecbab8SDwaipayan Ray==============
2152eecbab8SDwaipayan Ray
2162eecbab8SDwaipayan RayMessages in checkpatch are divided into three levels. The levels of messages
2172eecbab8SDwaipayan Rayin checkpatch denote the severity of the error. They are:
2182eecbab8SDwaipayan Ray
2192eecbab8SDwaipayan Ray - ERROR
2202eecbab8SDwaipayan Ray
2212eecbab8SDwaipayan Ray   This is the most strict level.  Messages of type ERROR must be taken
2222eecbab8SDwaipayan Ray   seriously as they denote things that are very likely to be wrong.
2232eecbab8SDwaipayan Ray
2242eecbab8SDwaipayan Ray - WARNING
2252eecbab8SDwaipayan Ray
2262eecbab8SDwaipayan Ray   This is the next stricter level.  Messages of type WARNING requires a
2272eecbab8SDwaipayan Ray   more careful review.  But it is milder than an ERROR.
2282eecbab8SDwaipayan Ray
2292eecbab8SDwaipayan Ray - CHECK
2302eecbab8SDwaipayan Ray
2312eecbab8SDwaipayan Ray   This is the mildest level.  These are things which may require some thought.
2322eecbab8SDwaipayan Ray
2332eecbab8SDwaipayan RayType Descriptions
2342eecbab8SDwaipayan Ray=================
2352eecbab8SDwaipayan Ray
2362eecbab8SDwaipayan RayThis section contains a description of all the message types in checkpatch.
2372eecbab8SDwaipayan Ray
2382eecbab8SDwaipayan Ray.. Types in this section are also parsed by checkpatch.
2392eecbab8SDwaipayan Ray.. The types are grouped into subsections based on use.
2402eecbab8SDwaipayan Ray
2412eecbab8SDwaipayan Ray
2422eecbab8SDwaipayan RayAllocation style
2432eecbab8SDwaipayan Ray----------------
2442eecbab8SDwaipayan Ray
2452eecbab8SDwaipayan Ray  **ALLOC_ARRAY_ARGS**
2462eecbab8SDwaipayan Ray    The first argument for kcalloc or kmalloc_array should be the
2472eecbab8SDwaipayan Ray    number of elements.  sizeof() as the first argument is generally
2482eecbab8SDwaipayan Ray    wrong.
24976001b8bSDwaipayan Ray
2502eecbab8SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/core-api/memory-allocation.html
2512eecbab8SDwaipayan Ray
2522eecbab8SDwaipayan Ray  **ALLOC_SIZEOF_STRUCT**
2532eecbab8SDwaipayan Ray    The allocation style is bad.  In general for family of
2542eecbab8SDwaipayan Ray    allocation functions using sizeof() to get memory size,
2552eecbab8SDwaipayan Ray    constructs like::
2562eecbab8SDwaipayan Ray
2572eecbab8SDwaipayan Ray      p = alloc(sizeof(struct foo), ...)
2582eecbab8SDwaipayan Ray
2592eecbab8SDwaipayan Ray    should be::
2602eecbab8SDwaipayan Ray
2612eecbab8SDwaipayan Ray      p = alloc(sizeof(*p), ...)
2622eecbab8SDwaipayan Ray
2632eecbab8SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#allocating-memory
2642eecbab8SDwaipayan Ray
2652eecbab8SDwaipayan Ray  **ALLOC_WITH_MULTIPLY**
2662eecbab8SDwaipayan Ray    Prefer kmalloc_array/kcalloc over kmalloc/kzalloc with a
2672eecbab8SDwaipayan Ray    sizeof multiply.
26876001b8bSDwaipayan Ray
2692eecbab8SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/core-api/memory-allocation.html
2702eecbab8SDwaipayan Ray
2712eecbab8SDwaipayan Ray
2722eecbab8SDwaipayan RayAPI usage
2732eecbab8SDwaipayan Ray---------
2742eecbab8SDwaipayan Ray
2752eecbab8SDwaipayan Ray  **ARCH_DEFINES**
2762eecbab8SDwaipayan Ray    Architecture specific defines should be avoided wherever
2772eecbab8SDwaipayan Ray    possible.
2782eecbab8SDwaipayan Ray
2792eecbab8SDwaipayan Ray  **ARCH_INCLUDE_LINUX**
2802eecbab8SDwaipayan Ray    Whenever asm/file.h is included and linux/file.h exists, a
2812eecbab8SDwaipayan Ray    conversion can be made when linux/file.h includes asm/file.h.
2822eecbab8SDwaipayan Ray    However this is not always the case (See signal.h).
2832eecbab8SDwaipayan Ray    This message type is emitted only for includes from arch/.
2842eecbab8SDwaipayan Ray
2852eecbab8SDwaipayan Ray  **AVOID_BUG**
2862eecbab8SDwaipayan Ray    BUG() or BUG_ON() should be avoided totally.
2872eecbab8SDwaipayan Ray    Use WARN() and WARN_ON() instead, and handle the "impossible"
2882eecbab8SDwaipayan Ray    error condition as gracefully as possible.
28976001b8bSDwaipayan Ray
2902eecbab8SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/deprecated.html#bug-and-bug-on
2912eecbab8SDwaipayan Ray
2922eecbab8SDwaipayan Ray  **CONSIDER_KSTRTO**
2932eecbab8SDwaipayan Ray    The simple_strtol(), simple_strtoll(), simple_strtoul(), and
2942eecbab8SDwaipayan Ray    simple_strtoull() functions explicitly ignore overflows, which
2952eecbab8SDwaipayan Ray    may lead to unexpected results in callers.  The respective kstrtol(),
2962eecbab8SDwaipayan Ray    kstrtoll(), kstrtoul(), and kstrtoull() functions tend to be the
2972eecbab8SDwaipayan Ray    correct replacements.
29876001b8bSDwaipayan Ray
2992eecbab8SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/deprecated.html#simple-strtol-simple-strtoll-simple-strtoul-simple-strtoull
3002eecbab8SDwaipayan Ray
30191a1265cSDwaipayan Ray  **CONSTANT_CONVERSION**
30291a1265cSDwaipayan Ray    Use of __constant_<foo> form is discouraged for the following functions::
30391a1265cSDwaipayan Ray
30491a1265cSDwaipayan Ray      __constant_cpu_to_be[x]
30591a1265cSDwaipayan Ray      __constant_cpu_to_le[x]
30691a1265cSDwaipayan Ray      __constant_be[x]_to_cpu
30791a1265cSDwaipayan Ray      __constant_le[x]_to_cpu
30891a1265cSDwaipayan Ray      __constant_htons
30991a1265cSDwaipayan Ray      __constant_ntohs
31091a1265cSDwaipayan Ray
31191a1265cSDwaipayan Ray    Using any of these outside of include/uapi/ is not preferred as using the
31291a1265cSDwaipayan Ray    function without __constant_ is identical when the argument is a
31391a1265cSDwaipayan Ray    constant.
31491a1265cSDwaipayan Ray
31591a1265cSDwaipayan Ray    In big endian systems, the macros like __constant_cpu_to_be32(x) and
31691a1265cSDwaipayan Ray    cpu_to_be32(x) expand to the same expression::
31791a1265cSDwaipayan Ray
31891a1265cSDwaipayan Ray      #define __constant_cpu_to_be32(x) ((__force __be32)(__u32)(x))
31991a1265cSDwaipayan Ray      #define __cpu_to_be32(x)          ((__force __be32)(__u32)(x))
32091a1265cSDwaipayan Ray
32191a1265cSDwaipayan Ray    In little endian systems, the macros __constant_cpu_to_be32(x) and
32291a1265cSDwaipayan Ray    cpu_to_be32(x) expand to __constant_swab32 and __swab32.  __swab32
32391a1265cSDwaipayan Ray    has a __builtin_constant_p check::
32491a1265cSDwaipayan Ray
32591a1265cSDwaipayan Ray      #define __swab32(x)				\
32691a1265cSDwaipayan Ray        (__builtin_constant_p((__u32)(x)) ?	\
32791a1265cSDwaipayan Ray        ___constant_swab32(x) :			\
32891a1265cSDwaipayan Ray        __fswab32(x))
32991a1265cSDwaipayan Ray
33091a1265cSDwaipayan Ray    So ultimately they have a special case for constants.
33191a1265cSDwaipayan Ray    Similar is the case with all of the macros in the list.  Thus
33291a1265cSDwaipayan Ray    using the __constant_... forms are unnecessarily verbose and
33391a1265cSDwaipayan Ray    not preferred outside of include/uapi.
33491a1265cSDwaipayan Ray
33591a1265cSDwaipayan Ray    See: https://lore.kernel.org/lkml/1400106425.12666.6.camel@joe-AO725/
33691a1265cSDwaipayan Ray
33791a1265cSDwaipayan Ray  **DEPRECATED_API**
33891a1265cSDwaipayan Ray    Usage of a deprecated RCU API is detected.  It is recommended to replace
33991a1265cSDwaipayan Ray    old flavourful RCU APIs by their new vanilla-RCU counterparts.
34091a1265cSDwaipayan Ray
34191a1265cSDwaipayan Ray    The full list of available RCU APIs can be viewed from the kernel docs.
34291a1265cSDwaipayan Ray
34391a1265cSDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/RCU/whatisRCU.html#full-list-of-rcu-apis
34491a1265cSDwaipayan Ray
34591a1265cSDwaipayan Ray  **DEVICE_ATTR_FUNCTIONS**
34691a1265cSDwaipayan Ray    The function names used in DEVICE_ATTR is unusual.
34791a1265cSDwaipayan Ray    Typically, the store and show functions are used with <attr>_store and
34891a1265cSDwaipayan Ray    <attr>_show, where <attr> is a named attribute variable of the device.
34991a1265cSDwaipayan Ray
35091a1265cSDwaipayan Ray    Consider the following examples::
35191a1265cSDwaipayan Ray
35291a1265cSDwaipayan Ray      static DEVICE_ATTR(type, 0444, type_show, NULL);
35391a1265cSDwaipayan Ray      static DEVICE_ATTR(power, 0644, power_show, power_store);
35491a1265cSDwaipayan Ray
35591a1265cSDwaipayan Ray    The function names should preferably follow the above pattern.
35691a1265cSDwaipayan Ray
35791a1265cSDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/driver-api/driver-model/device.html#attributes
35891a1265cSDwaipayan Ray
35991a1265cSDwaipayan Ray  **DEVICE_ATTR_RO**
36091a1265cSDwaipayan Ray    The DEVICE_ATTR_RO(name) helper macro can be used instead of
36191a1265cSDwaipayan Ray    DEVICE_ATTR(name, 0444, name_show, NULL);
36291a1265cSDwaipayan Ray
36391a1265cSDwaipayan Ray    Note that the macro automatically appends _show to the named
36491a1265cSDwaipayan Ray    attribute variable of the device for the show method.
36591a1265cSDwaipayan Ray
36691a1265cSDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/driver-api/driver-model/device.html#attributes
36791a1265cSDwaipayan Ray
36891a1265cSDwaipayan Ray  **DEVICE_ATTR_RW**
36991a1265cSDwaipayan Ray    The DEVICE_ATTR_RW(name) helper macro can be used instead of
37091a1265cSDwaipayan Ray    DEVICE_ATTR(name, 0644, name_show, name_store);
37191a1265cSDwaipayan Ray
37291a1265cSDwaipayan Ray    Note that the macro automatically appends _show and _store to the
37391a1265cSDwaipayan Ray    named attribute variable of the device for the show and store methods.
37491a1265cSDwaipayan Ray
37591a1265cSDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/driver-api/driver-model/device.html#attributes
37691a1265cSDwaipayan Ray
37791a1265cSDwaipayan Ray  **DEVICE_ATTR_WO**
37891a1265cSDwaipayan Ray    The DEVICE_AATR_WO(name) helper macro can be used instead of
37991a1265cSDwaipayan Ray    DEVICE_ATTR(name, 0200, NULL, name_store);
38091a1265cSDwaipayan Ray
38191a1265cSDwaipayan Ray    Note that the macro automatically appends _store to the
38291a1265cSDwaipayan Ray    named attribute variable of the device for the store method.
38391a1265cSDwaipayan Ray
38491a1265cSDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/driver-api/driver-model/device.html#attributes
38591a1265cSDwaipayan Ray
38691a1265cSDwaipayan Ray  **DUPLICATED_SYSCTL_CONST**
38791a1265cSDwaipayan Ray    Commit d91bff3011cf ("proc/sysctl: add shared variables for range
38891a1265cSDwaipayan Ray    check") added some shared const variables to be used instead of a local
38991a1265cSDwaipayan Ray    copy in each source file.
39091a1265cSDwaipayan Ray
39191a1265cSDwaipayan Ray    Consider replacing the sysctl range checking value with the shared
39291a1265cSDwaipayan Ray    one in include/linux/sysctl.h.  The following conversion scheme may
39391a1265cSDwaipayan Ray    be used::
39491a1265cSDwaipayan Ray
39591a1265cSDwaipayan Ray      &zero     ->  SYSCTL_ZERO
39691a1265cSDwaipayan Ray      &one      ->  SYSCTL_ONE
39791a1265cSDwaipayan Ray      &int_max  ->  SYSCTL_INT_MAX
39891a1265cSDwaipayan Ray
39991a1265cSDwaipayan Ray    See:
40091a1265cSDwaipayan Ray
40191a1265cSDwaipayan Ray      1. https://lore.kernel.org/lkml/[email protected]/
40291a1265cSDwaipayan Ray      2. https://lore.kernel.org/lkml/[email protected]/
40391a1265cSDwaipayan Ray
40491a1265cSDwaipayan Ray  **ENOSYS**
40591a1265cSDwaipayan Ray    ENOSYS means that a nonexistent system call was called.
40691a1265cSDwaipayan Ray    Earlier, it was wrongly used for things like invalid operations on
40791a1265cSDwaipayan Ray    otherwise valid syscalls.  This should be avoided in new code.
40891a1265cSDwaipayan Ray
40991a1265cSDwaipayan Ray    See: https://lore.kernel.org/lkml/5eb299021dec23c1a48fa7d9f2c8b794e967766d.1408730669.git.luto@amacapital.net/
41091a1265cSDwaipayan Ray
41191a1265cSDwaipayan Ray  **ENOTSUPP**
41291a1265cSDwaipayan Ray    ENOTSUPP is not a standard error code and should be avoided in new patches.
41391a1265cSDwaipayan Ray    EOPNOTSUPP should be used instead.
41491a1265cSDwaipayan Ray
41591a1265cSDwaipayan Ray    See: https://lore.kernel.org/netdev/[email protected]/
41691a1265cSDwaipayan Ray
41791a1265cSDwaipayan Ray  **EXPORT_SYMBOL**
41891a1265cSDwaipayan Ray    EXPORT_SYMBOL should immediately follow the symbol to be exported.
41991a1265cSDwaipayan Ray
42076001b8bSDwaipayan Ray  **IN_ATOMIC**
42176001b8bSDwaipayan Ray    in_atomic() is not for driver use so any such use is reported as an ERROR.
42291a1265cSDwaipayan Ray    Also in_atomic() is often used to determine if sleeping is permitted,
42391a1265cSDwaipayan Ray    but it is not reliable in this use model.  Therefore its use is
42491a1265cSDwaipayan Ray    strongly discouraged.
42576001b8bSDwaipayan Ray
42676001b8bSDwaipayan Ray    However, in_atomic() is ok for core kernel use.
42776001b8bSDwaipayan Ray
42876001b8bSDwaipayan Ray    See: https://lore.kernel.org/lkml/[email protected]/
42976001b8bSDwaipayan Ray
4303337c3a1SDwaipayan Ray  **LOCKDEP**
4313337c3a1SDwaipayan Ray    The lockdep_no_validate class was added as a temporary measure to
4323337c3a1SDwaipayan Ray    prevent warnings on conversion of device->sem to device->mutex.
4333337c3a1SDwaipayan Ray    It should not be used for any other purpose.
43476001b8bSDwaipayan Ray
4353337c3a1SDwaipayan Ray    See: https://lore.kernel.org/lkml/1268959062.9440.467.camel@laptop/
4363337c3a1SDwaipayan Ray
4373337c3a1SDwaipayan Ray  **MALFORMED_INCLUDE**
4383337c3a1SDwaipayan Ray    The #include statement has a malformed path.  This has happened
4393337c3a1SDwaipayan Ray    because the author has included a double slash "//" in the pathname
4403337c3a1SDwaipayan Ray    accidentally.
4413337c3a1SDwaipayan Ray
4423337c3a1SDwaipayan Ray  **USE_LOCKDEP**
4433337c3a1SDwaipayan Ray    lockdep_assert_held() annotations should be preferred over
4443337c3a1SDwaipayan Ray    assertions based on spin_is_locked()
44576001b8bSDwaipayan Ray
4463337c3a1SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/locking/lockdep-design.html#annotations
4473337c3a1SDwaipayan Ray
4483337c3a1SDwaipayan Ray  **UAPI_INCLUDE**
4493337c3a1SDwaipayan Ray    No #include statements in include/uapi should use a uapi/ path.
4503337c3a1SDwaipayan Ray
45176001b8bSDwaipayan Ray  **USLEEP_RANGE**
45276001b8bSDwaipayan Ray    usleep_range() should be preferred over udelay(). The proper way of
45376001b8bSDwaipayan Ray    using usleep_range() is mentioned in the kernel docs.
45476001b8bSDwaipayan Ray
4552eecbab8SDwaipayan Ray
45691a1265cSDwaipayan RayComments
45791a1265cSDwaipayan Ray--------
4582eecbab8SDwaipayan Ray
4592eecbab8SDwaipayan Ray  **BLOCK_COMMENT_STYLE**
4602eecbab8SDwaipayan Ray    The comment style is incorrect.  The preferred style for multi-
4612eecbab8SDwaipayan Ray    line comments is::
4622eecbab8SDwaipayan Ray
4632eecbab8SDwaipayan Ray      /*
4642eecbab8SDwaipayan Ray      * This is the preferred style
4652eecbab8SDwaipayan Ray      * for multi line comments.
4662eecbab8SDwaipayan Ray      */
4672eecbab8SDwaipayan Ray
4682eecbab8SDwaipayan Ray    The networking comment style is a bit different, with the first line
4692eecbab8SDwaipayan Ray    not empty like the former::
4702eecbab8SDwaipayan Ray
4712eecbab8SDwaipayan Ray      /* This is the preferred comment style
4722eecbab8SDwaipayan Ray      * for files in net/ and drivers/net/
4732eecbab8SDwaipayan Ray      */
4742eecbab8SDwaipayan Ray
4752eecbab8SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#commenting
4762eecbab8SDwaipayan Ray
4772eecbab8SDwaipayan Ray  **C99_COMMENTS**
4782eecbab8SDwaipayan Ray    C99 style single line comments (//) should not be used.
4792eecbab8SDwaipayan Ray    Prefer the block comment style instead.
48076001b8bSDwaipayan Ray
4812eecbab8SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#commenting
4822eecbab8SDwaipayan Ray
48391a1265cSDwaipayan Ray  **DATA_RACE**
48491a1265cSDwaipayan Ray    Applications of data_race() should have a comment so as to document the
48591a1265cSDwaipayan Ray    reasoning behind why it was deemed safe.
48691a1265cSDwaipayan Ray
48791a1265cSDwaipayan Ray    See: https://lore.kernel.org/lkml/[email protected]/
48891a1265cSDwaipayan Ray
48991a1265cSDwaipayan Ray  **FSF_MAILING_ADDRESS**
49091a1265cSDwaipayan Ray    Kernel maintainers reject new instances of the GPL boilerplate paragraph
49191a1265cSDwaipayan Ray    directing people to write to the FSF for a copy of the GPL, since the
49291a1265cSDwaipayan Ray    FSF has moved in the past and may do so again.
49391a1265cSDwaipayan Ray    So do not write paragraphs about writing to the Free Software Foundation's
49491a1265cSDwaipayan Ray    mailing address.
49591a1265cSDwaipayan Ray
49691a1265cSDwaipayan Ray    See: https://lore.kernel.org/lkml/20131006222342.GT19510@leaf/
49791a1265cSDwaipayan Ray
4982eecbab8SDwaipayan Ray
4992eecbab8SDwaipayan RayCommit message
5002eecbab8SDwaipayan Ray--------------
5012eecbab8SDwaipayan Ray
5022eecbab8SDwaipayan Ray  **BAD_SIGN_OFF**
5032eecbab8SDwaipayan Ray    The signed-off-by line does not fall in line with the standards
5042eecbab8SDwaipayan Ray    specified by the community.
50576001b8bSDwaipayan Ray
5062eecbab8SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#developer-s-certificate-of-origin-1-1
5072eecbab8SDwaipayan Ray
5082eecbab8SDwaipayan Ray  **BAD_STABLE_ADDRESS_STYLE**
5092eecbab8SDwaipayan Ray    The email format for stable is incorrect.
5102eecbab8SDwaipayan Ray    Some valid options for stable address are::
5112eecbab8SDwaipayan Ray
5122eecbab8SDwaipayan Ray      1. [email protected]
5132eecbab8SDwaipayan Ray      2. [email protected]
5142eecbab8SDwaipayan Ray
5152eecbab8SDwaipayan Ray    For adding version info, the following comment style should be used::
5162eecbab8SDwaipayan Ray
5172eecbab8SDwaipayan Ray      [email protected] # version info
5182eecbab8SDwaipayan Ray
5192eecbab8SDwaipayan Ray  **COMMIT_COMMENT_SYMBOL**
5202eecbab8SDwaipayan Ray    Commit log lines starting with a '#' are ignored by git as
5212eecbab8SDwaipayan Ray    comments.  To solve this problem addition of a single space
5222eecbab8SDwaipayan Ray    infront of the log line is enough.
5232eecbab8SDwaipayan Ray
5242eecbab8SDwaipayan Ray  **COMMIT_MESSAGE**
5252eecbab8SDwaipayan Ray    The patch is missing a commit description.  A brief
5262eecbab8SDwaipayan Ray    description of the changes made by the patch should be added.
52776001b8bSDwaipayan Ray
5282eecbab8SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
5292eecbab8SDwaipayan Ray
53091a1265cSDwaipayan Ray  **EMAIL_SUBJECT**
53191a1265cSDwaipayan Ray    Naming the tool that found the issue is not very useful in the
53291a1265cSDwaipayan Ray    subject line.  A good subject line summarizes the change that
53391a1265cSDwaipayan Ray    the patch brings.
53491a1265cSDwaipayan Ray
53591a1265cSDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
53691a1265cSDwaipayan Ray
53776001b8bSDwaipayan Ray  **FROM_SIGN_OFF_MISMATCH**
53876001b8bSDwaipayan Ray    The author's email does not match with that in the Signed-off-by:
53976001b8bSDwaipayan Ray    line(s). This can be sometimes caused due to an improperly configured
54076001b8bSDwaipayan Ray    email client.
54176001b8bSDwaipayan Ray
54276001b8bSDwaipayan Ray    This message is emitted due to any of the following reasons::
54376001b8bSDwaipayan Ray
54476001b8bSDwaipayan Ray      - The email names do not match.
54576001b8bSDwaipayan Ray      - The email addresses do not match.
54676001b8bSDwaipayan Ray      - The email subaddresses do not match.
54776001b8bSDwaipayan Ray      - The email comments do not match.
54876001b8bSDwaipayan Ray
5492eecbab8SDwaipayan Ray  **MISSING_SIGN_OFF**
5502eecbab8SDwaipayan Ray    The patch is missing a Signed-off-by line.  A signed-off-by
5512eecbab8SDwaipayan Ray    line should be added according to Developer's certificate of
5522eecbab8SDwaipayan Ray    Origin.
55376001b8bSDwaipayan Ray
5542eecbab8SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#sign-your-work-the-developer-s-certificate-of-origin
5552eecbab8SDwaipayan Ray
5562eecbab8SDwaipayan Ray  **NO_AUTHOR_SIGN_OFF**
5572eecbab8SDwaipayan Ray    The author of the patch has not signed off the patch.  It is
5582eecbab8SDwaipayan Ray    required that a simple sign off line should be present at the
5592eecbab8SDwaipayan Ray    end of explanation of the patch to denote that the author has
5602eecbab8SDwaipayan Ray    written it or otherwise has the rights to pass it on as an open
5612eecbab8SDwaipayan Ray    source patch.
56276001b8bSDwaipayan Ray
5632eecbab8SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#sign-your-work-the-developer-s-certificate-of-origin
5642eecbab8SDwaipayan Ray
5653337c3a1SDwaipayan Ray  **DIFF_IN_COMMIT_MSG**
5663337c3a1SDwaipayan Ray    Avoid having diff content in commit message.
5673337c3a1SDwaipayan Ray    This causes problems when one tries to apply a file containing both
5683337c3a1SDwaipayan Ray    the changelog and the diff because patch(1) tries to apply the diff
5693337c3a1SDwaipayan Ray    which it found in the changelog.
57076001b8bSDwaipayan Ray
5713337c3a1SDwaipayan Ray    See: https://lore.kernel.org/lkml/[email protected]/
5723337c3a1SDwaipayan Ray
5733337c3a1SDwaipayan Ray  **GERRIT_CHANGE_ID**
5743337c3a1SDwaipayan Ray    To be picked up by gerrit, the footer of the commit message might
5753337c3a1SDwaipayan Ray    have a Change-Id like::
5763337c3a1SDwaipayan Ray
5773337c3a1SDwaipayan Ray      Change-Id: Ic8aaa0728a43936cd4c6e1ed590e01ba8f0fbf5b
5783337c3a1SDwaipayan Ray      Signed-off-by: A. U. Thor <[email protected]>
5793337c3a1SDwaipayan Ray
5803337c3a1SDwaipayan Ray    The Change-Id line must be removed before submitting.
5813337c3a1SDwaipayan Ray
5823337c3a1SDwaipayan Ray  **GIT_COMMIT_ID**
5833337c3a1SDwaipayan Ray    The proper way to reference a commit id is:
5843337c3a1SDwaipayan Ray    commit <12+ chars of sha1> ("<title line>")
5853337c3a1SDwaipayan Ray
5863337c3a1SDwaipayan Ray    An example may be::
5873337c3a1SDwaipayan Ray
5883337c3a1SDwaipayan Ray      Commit e21d2170f36602ae2708 ("video: remove unnecessary
5893337c3a1SDwaipayan Ray      platform_set_drvdata()") removed the unnecessary
5903337c3a1SDwaipayan Ray      platform_set_drvdata(), but left the variable "dev" unused,
5913337c3a1SDwaipayan Ray      delete it.
5923337c3a1SDwaipayan Ray
5933337c3a1SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
5943337c3a1SDwaipayan Ray
595bd17e036SNiklas Söderlund  **BAD_FIXES_TAG**
596bd17e036SNiklas Söderlund    The Fixes: tag is malformed or does not follow the community conventions.
597bd17e036SNiklas Söderlund    This can occur if the tag have been split into multiple lines (e.g., when
598bd17e036SNiklas Söderlund    pasted in an email program with word wrapping enabled).
599bd17e036SNiklas Söderlund
600bd17e036SNiklas Söderlund    See: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#describe-your-changes
601bd17e036SNiklas Söderlund
6022eecbab8SDwaipayan Ray
6032eecbab8SDwaipayan RayComparison style
6042eecbab8SDwaipayan Ray----------------
6052eecbab8SDwaipayan Ray
6062eecbab8SDwaipayan Ray  **ASSIGN_IN_IF**
6072eecbab8SDwaipayan Ray    Do not use assignments in if condition.
6082eecbab8SDwaipayan Ray    Example::
6092eecbab8SDwaipayan Ray
6102eecbab8SDwaipayan Ray      if ((foo = bar(...)) < BAZ) {
6112eecbab8SDwaipayan Ray
6122eecbab8SDwaipayan Ray    should be written as::
6132eecbab8SDwaipayan Ray
6142eecbab8SDwaipayan Ray      foo = bar(...);
6152eecbab8SDwaipayan Ray      if (foo < BAZ) {
6162eecbab8SDwaipayan Ray
6172eecbab8SDwaipayan Ray  **BOOL_COMPARISON**
6182eecbab8SDwaipayan Ray    Comparisons of A to true and false are better written
6192eecbab8SDwaipayan Ray    as A and !A.
62076001b8bSDwaipayan Ray
6212eecbab8SDwaipayan Ray    See: https://lore.kernel.org/lkml/1365563834.27174.12.camel@joe-AO722/
6222eecbab8SDwaipayan Ray
6232eecbab8SDwaipayan Ray  **COMPARISON_TO_NULL**
6242eecbab8SDwaipayan Ray    Comparisons to NULL in the form (foo == NULL) or (foo != NULL)
6252eecbab8SDwaipayan Ray    are better written as (!foo) and (foo).
6262eecbab8SDwaipayan Ray
6272eecbab8SDwaipayan Ray  **CONSTANT_COMPARISON**
6282eecbab8SDwaipayan Ray    Comparisons with a constant or upper case identifier on the left
6292eecbab8SDwaipayan Ray    side of the test should be avoided.
6302eecbab8SDwaipayan Ray
6312eecbab8SDwaipayan Ray
63291a1265cSDwaipayan RayIndentation and Line Breaks
63391a1265cSDwaipayan Ray---------------------------
63491a1265cSDwaipayan Ray
63591a1265cSDwaipayan Ray  **CODE_INDENT**
63691a1265cSDwaipayan Ray    Code indent should use tabs instead of spaces.
63791a1265cSDwaipayan Ray    Outside of comments, documentation and Kconfig,
63891a1265cSDwaipayan Ray    spaces are never used for indentation.
63991a1265cSDwaipayan Ray
64091a1265cSDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#indentation
64191a1265cSDwaipayan Ray
64291a1265cSDwaipayan Ray  **DEEP_INDENTATION**
64391a1265cSDwaipayan Ray    Indentation with 6 or more tabs usually indicate overly indented
64491a1265cSDwaipayan Ray    code.
64591a1265cSDwaipayan Ray
64691a1265cSDwaipayan Ray    It is suggested to refactor excessive indentation of
64791a1265cSDwaipayan Ray    if/else/for/do/while/switch statements.
64891a1265cSDwaipayan Ray
64991a1265cSDwaipayan Ray    See: https://lore.kernel.org/lkml/1328311239.21255.24.camel@joe2Laptop/
65091a1265cSDwaipayan Ray
65191a1265cSDwaipayan Ray  **SWITCH_CASE_INDENT_LEVEL**
65291a1265cSDwaipayan Ray    switch should be at the same indent as case.
65391a1265cSDwaipayan Ray    Example::
65491a1265cSDwaipayan Ray
65591a1265cSDwaipayan Ray      switch (suffix) {
65691a1265cSDwaipayan Ray      case 'G':
65791a1265cSDwaipayan Ray      case 'g':
65891a1265cSDwaipayan Ray              mem <<= 30;
65991a1265cSDwaipayan Ray              break;
66091a1265cSDwaipayan Ray      case 'M':
66191a1265cSDwaipayan Ray      case 'm':
66291a1265cSDwaipayan Ray              mem <<= 20;
66391a1265cSDwaipayan Ray              break;
66491a1265cSDwaipayan Ray      case 'K':
66591a1265cSDwaipayan Ray      case 'k':
66691a1265cSDwaipayan Ray              mem <<= 10;
66791a1265cSDwaipayan Ray              fallthrough;
66891a1265cSDwaipayan Ray      default:
66991a1265cSDwaipayan Ray              break;
67091a1265cSDwaipayan Ray      }
67191a1265cSDwaipayan Ray
67291a1265cSDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#indentation
67391a1265cSDwaipayan Ray
67491a1265cSDwaipayan Ray  **LONG_LINE**
67591a1265cSDwaipayan Ray    The line has exceeded the specified maximum length.
67691a1265cSDwaipayan Ray    To use a different maximum line length, the --max-line-length=n option
67791a1265cSDwaipayan Ray    may be added while invoking checkpatch.
67891a1265cSDwaipayan Ray
67991a1265cSDwaipayan Ray    Earlier, the default line length was 80 columns.  Commit bdc48fa11e46
68091a1265cSDwaipayan Ray    ("checkpatch/coding-style: deprecate 80-column warning") increased the
68191a1265cSDwaipayan Ray    limit to 100 columns.  This is not a hard limit either and it's
68291a1265cSDwaipayan Ray    preferable to stay within 80 columns whenever possible.
68391a1265cSDwaipayan Ray
68491a1265cSDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#breaking-long-lines-and-strings
68591a1265cSDwaipayan Ray
68691a1265cSDwaipayan Ray  **LONG_LINE_STRING**
68791a1265cSDwaipayan Ray    A string starts before but extends beyond the maximum line length.
68891a1265cSDwaipayan Ray    To use a different maximum line length, the --max-line-length=n option
68991a1265cSDwaipayan Ray    may be added while invoking checkpatch.
69091a1265cSDwaipayan Ray
69191a1265cSDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#breaking-long-lines-and-strings
69291a1265cSDwaipayan Ray
69391a1265cSDwaipayan Ray  **LONG_LINE_COMMENT**
69491a1265cSDwaipayan Ray    A comment starts before but extends beyond the maximum line length.
69591a1265cSDwaipayan Ray    To use a different maximum line length, the --max-line-length=n option
69691a1265cSDwaipayan Ray    may be added while invoking checkpatch.
69791a1265cSDwaipayan Ray
69891a1265cSDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#breaking-long-lines-and-strings
69991a1265cSDwaipayan Ray
700d9548979SUtkarsh Verma  **SPLIT_STRING**
701d9548979SUtkarsh Verma    Quoted strings that appear as messages in userspace and can be
702d9548979SUtkarsh Verma    grepped, should not be split across multiple lines.
703d9548979SUtkarsh Verma
704d9548979SUtkarsh Verma    See: https://lore.kernel.org/lkml/20120203052727.GA15035@leaf/
705d9548979SUtkarsh Verma
706250a0a5bSUtkarsh Verma  **MULTILINE_DEREFERENCE**
707250a0a5bSUtkarsh Verma    A single dereferencing identifier spanned on multiple lines like::
708250a0a5bSUtkarsh Verma
709250a0a5bSUtkarsh Verma      struct_identifier->member[index].
710250a0a5bSUtkarsh Verma      member = <foo>;
711250a0a5bSUtkarsh Verma
712250a0a5bSUtkarsh Verma    is generally hard to follow. It can easily lead to typos and so makes
713250a0a5bSUtkarsh Verma    the code vulnerable to bugs.
714250a0a5bSUtkarsh Verma
715250a0a5bSUtkarsh Verma    If fixing the multiple line dereferencing leads to an 80 column
716250a0a5bSUtkarsh Verma    violation, then either rewrite the code in a more simple way or if the
717250a0a5bSUtkarsh Verma    starting part of the dereferencing identifier is the same and used at
718250a0a5bSUtkarsh Verma    multiple places then store it in a temporary variable, and use that
719250a0a5bSUtkarsh Verma    temporary variable only at all the places. For example, if there are
720250a0a5bSUtkarsh Verma    two dereferencing identifiers::
721250a0a5bSUtkarsh Verma
722250a0a5bSUtkarsh Verma      member1->member2->member3.foo1;
723250a0a5bSUtkarsh Verma      member1->member2->member3.foo2;
724250a0a5bSUtkarsh Verma
725250a0a5bSUtkarsh Verma    then store the member1->member2->member3 part in a temporary variable.
726250a0a5bSUtkarsh Verma    It not only helps to avoid the 80 column violation but also reduces
727250a0a5bSUtkarsh Verma    the program size by removing the unnecessary dereferences.
728250a0a5bSUtkarsh Verma
729250a0a5bSUtkarsh Verma    But if none of the above methods work then ignore the 80 column
730250a0a5bSUtkarsh Verma    violation because it is much easier to read a dereferencing identifier
731250a0a5bSUtkarsh Verma    on a single line.
732250a0a5bSUtkarsh Verma
73391a1265cSDwaipayan Ray  **TRAILING_STATEMENTS**
73491a1265cSDwaipayan Ray    Trailing statements (for example after any conditional) should be
73591a1265cSDwaipayan Ray    on the next line.
73691a1265cSDwaipayan Ray    Statements, such as::
73791a1265cSDwaipayan Ray
73891a1265cSDwaipayan Ray      if (x == y) break;
73991a1265cSDwaipayan Ray
74091a1265cSDwaipayan Ray    should be::
74191a1265cSDwaipayan Ray
74291a1265cSDwaipayan Ray      if (x == y)
74391a1265cSDwaipayan Ray              break;
74491a1265cSDwaipayan Ray
74591a1265cSDwaipayan Ray
7463337c3a1SDwaipayan RayMacros, Attributes and Symbols
7473337c3a1SDwaipayan Ray------------------------------
7483337c3a1SDwaipayan Ray
7493337c3a1SDwaipayan Ray  **ARRAY_SIZE**
7503337c3a1SDwaipayan Ray    The ARRAY_SIZE(foo) macro should be preferred over
7513337c3a1SDwaipayan Ray    sizeof(foo)/sizeof(foo[0]) for finding number of elements in an
7523337c3a1SDwaipayan Ray    array.
7533337c3a1SDwaipayan Ray
7543337c3a1SDwaipayan Ray    The macro is defined in include/linux/kernel.h::
7553337c3a1SDwaipayan Ray
7563337c3a1SDwaipayan Ray      #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
7573337c3a1SDwaipayan Ray
7583337c3a1SDwaipayan Ray  **AVOID_EXTERNS**
7593337c3a1SDwaipayan Ray    Function prototypes don't need to be declared extern in .h
7603337c3a1SDwaipayan Ray    files.  It's assumed by the compiler and is unnecessary.
7613337c3a1SDwaipayan Ray
7623337c3a1SDwaipayan Ray  **AVOID_L_PREFIX**
7633337c3a1SDwaipayan Ray    Local symbol names that are prefixed with `.L` should be avoided,
7643337c3a1SDwaipayan Ray    as this has special meaning for the assembler; a symbol entry will
7653337c3a1SDwaipayan Ray    not be emitted into the symbol table.  This can prevent `objtool`
7663337c3a1SDwaipayan Ray    from generating correct unwind info.
7673337c3a1SDwaipayan Ray
7683337c3a1SDwaipayan Ray    Symbols with STB_LOCAL binding may still be used, and `.L` prefixed
7693337c3a1SDwaipayan Ray    local symbol names are still generally usable within a function,
7703337c3a1SDwaipayan Ray    but `.L` prefixed local symbol names should not be used to denote
7713337c3a1SDwaipayan Ray    the beginning or end of code regions via
7723337c3a1SDwaipayan Ray    `SYM_CODE_START_LOCAL`/`SYM_CODE_END`
7733337c3a1SDwaipayan Ray
7743337c3a1SDwaipayan Ray  **BIT_MACRO**
7753337c3a1SDwaipayan Ray    Defines like: 1 << <digit> could be BIT(digit).
7760e7c52daSAndrew Jeffery    The BIT() macro is defined via include/linux/bits.h::
7773337c3a1SDwaipayan Ray
7783337c3a1SDwaipayan Ray      #define BIT(nr)         (1UL << (nr))
7793337c3a1SDwaipayan Ray
7803337c3a1SDwaipayan Ray  **CONST_READ_MOSTLY**
7813337c3a1SDwaipayan Ray    When a variable is tagged with the __read_mostly annotation, it is a
7823337c3a1SDwaipayan Ray    signal to the compiler that accesses to the variable will be mostly
7833337c3a1SDwaipayan Ray    reads and rarely(but NOT never) a write.
7843337c3a1SDwaipayan Ray
7853337c3a1SDwaipayan Ray    const __read_mostly does not make any sense as const data is already
7863337c3a1SDwaipayan Ray    read-only.  The __read_mostly annotation thus should be removed.
7873337c3a1SDwaipayan Ray
7883337c3a1SDwaipayan Ray  **DATE_TIME**
7893337c3a1SDwaipayan Ray    It is generally desirable that building the same source code with
7903337c3a1SDwaipayan Ray    the same set of tools is reproducible, i.e. the output is always
7913337c3a1SDwaipayan Ray    exactly the same.
7923337c3a1SDwaipayan Ray
7933337c3a1SDwaipayan Ray    The kernel does *not* use the ``__DATE__`` and ``__TIME__`` macros,
7943337c3a1SDwaipayan Ray    and enables warnings if they are used as they can lead to
7953337c3a1SDwaipayan Ray    non-deterministic builds.
79676001b8bSDwaipayan Ray
7973337c3a1SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/kbuild/reproducible-builds.html#timestamps
7983337c3a1SDwaipayan Ray
7993337c3a1SDwaipayan Ray  **DEFINE_ARCH_HAS**
8003337c3a1SDwaipayan Ray    The ARCH_HAS_xyz and ARCH_HAVE_xyz patterns are wrong.
8013337c3a1SDwaipayan Ray
8023337c3a1SDwaipayan Ray    For big conceptual features use Kconfig symbols instead.  And for
8033337c3a1SDwaipayan Ray    smaller things where we have compatibility fallback functions but
8043337c3a1SDwaipayan Ray    want architectures able to override them with optimized ones, we
8053337c3a1SDwaipayan Ray    should either use weak functions (appropriate for some cases), or
8063337c3a1SDwaipayan Ray    the symbol that protects them should be the same symbol we use.
80776001b8bSDwaipayan Ray
8083337c3a1SDwaipayan Ray    See: https://lore.kernel.org/lkml/CA+55aFycQ9XJvEOsiM3txHL5bjUc8CeKWJNR_H+MiicaddB42Q@mail.gmail.com/
8093337c3a1SDwaipayan Ray
81091a1265cSDwaipayan Ray  **DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON**
81191a1265cSDwaipayan Ray    do {} while(0) macros should not have a trailing semicolon.
81291a1265cSDwaipayan Ray
8133337c3a1SDwaipayan Ray  **INIT_ATTRIBUTE**
8143337c3a1SDwaipayan Ray    Const init definitions should use __initconst instead of
8153337c3a1SDwaipayan Ray    __initdata.
8163337c3a1SDwaipayan Ray
8173337c3a1SDwaipayan Ray    Similarly init definitions without const require a separate
8183337c3a1SDwaipayan Ray    use of const.
8193337c3a1SDwaipayan Ray
8203337c3a1SDwaipayan Ray  **INLINE_LOCATION**
8213337c3a1SDwaipayan Ray    The inline keyword should sit between storage class and type.
8223337c3a1SDwaipayan Ray
8233337c3a1SDwaipayan Ray    For example, the following segment::
8243337c3a1SDwaipayan Ray
8253337c3a1SDwaipayan Ray      inline static int example_function(void)
8263337c3a1SDwaipayan Ray      {
8273337c3a1SDwaipayan Ray              ...
8283337c3a1SDwaipayan Ray      }
8293337c3a1SDwaipayan Ray
8303337c3a1SDwaipayan Ray    should be::
8313337c3a1SDwaipayan Ray
8323337c3a1SDwaipayan Ray      static inline int example_function(void)
8333337c3a1SDwaipayan Ray      {
8343337c3a1SDwaipayan Ray              ...
8353337c3a1SDwaipayan Ray      }
8363337c3a1SDwaipayan Ray
83776001b8bSDwaipayan Ray  **MISPLACED_INIT**
83876001b8bSDwaipayan Ray    It is possible to use section markers on variables in a way
83976001b8bSDwaipayan Ray    which gcc doesn't understand (or at least not the way the
84076001b8bSDwaipayan Ray    developer intended)::
84176001b8bSDwaipayan Ray
84276001b8bSDwaipayan Ray      static struct __initdata samsung_pll_clock exynos4_plls[nr_plls] = {
84376001b8bSDwaipayan Ray
84476001b8bSDwaipayan Ray    does not put exynos4_plls in the .initdata section. The __initdata
84576001b8bSDwaipayan Ray    marker can be virtually anywhere on the line, except right after
84676001b8bSDwaipayan Ray    "struct". The preferred location is before the "=" sign if there is
84776001b8bSDwaipayan Ray    one, or before the trailing ";" otherwise.
84876001b8bSDwaipayan Ray
84976001b8bSDwaipayan Ray    See: https://lore.kernel.org/lkml/1377655732.3619.19.camel@joe-AO722/
85076001b8bSDwaipayan Ray
8513337c3a1SDwaipayan Ray  **MULTISTATEMENT_MACRO_USE_DO_WHILE**
8523337c3a1SDwaipayan Ray    Macros with multiple statements should be enclosed in a
8533337c3a1SDwaipayan Ray    do - while block.  Same should also be the case for macros
8543337c3a1SDwaipayan Ray    starting with `if` to avoid logic defects::
8553337c3a1SDwaipayan Ray
8563337c3a1SDwaipayan Ray      #define macrofun(a, b, c)                 \
8573337c3a1SDwaipayan Ray        do {                                    \
8583337c3a1SDwaipayan Ray                if (a == 5)                     \
8593337c3a1SDwaipayan Ray                        do_this(b, c);          \
8603337c3a1SDwaipayan Ray        } while (0)
8613337c3a1SDwaipayan Ray
8623337c3a1SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#macros-enums-and-rtl
8633337c3a1SDwaipayan Ray
86476001b8bSDwaipayan Ray  **PREFER_FALLTHROUGH**
86576001b8bSDwaipayan Ray    Use the `fallthrough;` pseudo keyword instead of
86676001b8bSDwaipayan Ray    `/* fallthrough */` like comments.
86776001b8bSDwaipayan Ray
86829bd0cacSUtkarsh Verma  **TRAILING_SEMICOLON**
86929bd0cacSUtkarsh Verma    Macro definition should not end with a semicolon. The macro
87029bd0cacSUtkarsh Verma    invocation style should be consistent with function calls.
87129bd0cacSUtkarsh Verma    This can prevent any unexpected code paths::
87229bd0cacSUtkarsh Verma
87329bd0cacSUtkarsh Verma      #define MAC do_something;
87429bd0cacSUtkarsh Verma
87529bd0cacSUtkarsh Verma    If this macro is used within a if else statement, like::
87629bd0cacSUtkarsh Verma
87729bd0cacSUtkarsh Verma      if (some_condition)
87829bd0cacSUtkarsh Verma              MAC;
87929bd0cacSUtkarsh Verma
88029bd0cacSUtkarsh Verma      else
88129bd0cacSUtkarsh Verma              do_something;
88229bd0cacSUtkarsh Verma
88329bd0cacSUtkarsh Verma    Then there would be a compilation error, because when the macro is
88429bd0cacSUtkarsh Verma    expanded there are two trailing semicolons, so the else branch gets
88529bd0cacSUtkarsh Verma    orphaned.
88629bd0cacSUtkarsh Verma
88729bd0cacSUtkarsh Verma    See: https://lore.kernel.org/lkml/1399671106.2912.21.camel@joe-AO725/
88829bd0cacSUtkarsh Verma
889*b1be5844SXining Xu  **MACRO_ARG_UNUSED**
890*b1be5844SXining Xu    If function-like macros do not utilize a parameter, it might result
891*b1be5844SXining Xu    in a build warning. We advocate for utilizing static inline functions
892*b1be5844SXining Xu    to replace such macros.
893*b1be5844SXining Xu    For example, for a macro such as the one below::
894*b1be5844SXining Xu
895*b1be5844SXining Xu      #define test(a) do { } while (0)
896*b1be5844SXining Xu
897*b1be5844SXining Xu    there would be a warning like below::
898*b1be5844SXining Xu
899*b1be5844SXining Xu      WARNING: Argument 'a' is not used in function-like macro.
900*b1be5844SXining Xu
901*b1be5844SXining Xu    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#macros-enums-and-rtl
902*b1be5844SXining Xu
903250a0a5bSUtkarsh Verma  **SINGLE_STATEMENT_DO_WHILE_MACRO**
904250a0a5bSUtkarsh Verma    For the multi-statement macros, it is necessary to use the do-while
905250a0a5bSUtkarsh Verma    loop to avoid unpredictable code paths. The do-while loop helps to
906250a0a5bSUtkarsh Verma    group the multiple statements into a single one so that a
907250a0a5bSUtkarsh Verma    function-like macro can be used as a function only.
908250a0a5bSUtkarsh Verma
909250a0a5bSUtkarsh Verma    But for the single statement macros, it is unnecessary to use the
910250a0a5bSUtkarsh Verma    do-while loop. Although the code is syntactically correct but using
911250a0a5bSUtkarsh Verma    the do-while loop is redundant. So remove the do-while loop for single
912250a0a5bSUtkarsh Verma    statement macros.
913250a0a5bSUtkarsh Verma
9143337c3a1SDwaipayan Ray  **WEAK_DECLARATION**
9153337c3a1SDwaipayan Ray    Using weak declarations like __attribute__((weak)) or __weak
9163337c3a1SDwaipayan Ray    can have unintended link defects.  Avoid using them.
9173337c3a1SDwaipayan Ray
9183337c3a1SDwaipayan Ray
9193337c3a1SDwaipayan RayFunctions and Variables
9203337c3a1SDwaipayan Ray-----------------------
9213337c3a1SDwaipayan Ray
9223337c3a1SDwaipayan Ray  **CAMELCASE**
9233337c3a1SDwaipayan Ray    Avoid CamelCase Identifiers.
92476001b8bSDwaipayan Ray
9253337c3a1SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#naming
9263337c3a1SDwaipayan Ray
92791a1265cSDwaipayan Ray  **CONST_CONST**
92891a1265cSDwaipayan Ray    Using `const <type> const *` is generally meant to be
92991a1265cSDwaipayan Ray    written `const <type> * const`.
93091a1265cSDwaipayan Ray
93191a1265cSDwaipayan Ray  **CONST_STRUCT**
93291a1265cSDwaipayan Ray    Using const is generally a good idea.  Checkpatch reads
93391a1265cSDwaipayan Ray    a list of frequently used structs that are always or
93491a1265cSDwaipayan Ray    almost always constant.
93591a1265cSDwaipayan Ray
93691a1265cSDwaipayan Ray    The existing structs list can be viewed from
93791a1265cSDwaipayan Ray    `scripts/const_structs.checkpatch`.
93891a1265cSDwaipayan Ray
93991a1265cSDwaipayan Ray    See: https://lore.kernel.org/lkml/alpine.DEB.2.10.1608281509480.3321@hadrien/
94091a1265cSDwaipayan Ray
94191a1265cSDwaipayan Ray  **EMBEDDED_FUNCTION_NAME**
94291a1265cSDwaipayan Ray    Embedded function names are less appropriate to use as
94391a1265cSDwaipayan Ray    refactoring can cause function renaming.  Prefer the use of
94491a1265cSDwaipayan Ray    "%s", __func__ to embedded function names.
94591a1265cSDwaipayan Ray
94691a1265cSDwaipayan Ray    Note that this does not work with -f (--file) checkpatch option
94791a1265cSDwaipayan Ray    as it depends on patch context providing the function name.
94891a1265cSDwaipayan Ray
94991a1265cSDwaipayan Ray  **FUNCTION_ARGUMENTS**
95091a1265cSDwaipayan Ray    This warning is emitted due to any of the following reasons:
95191a1265cSDwaipayan Ray
95291a1265cSDwaipayan Ray      1. Arguments for the function declaration do not follow
95391a1265cSDwaipayan Ray         the identifier name.  Example::
95491a1265cSDwaipayan Ray
95591a1265cSDwaipayan Ray           void foo
95691a1265cSDwaipayan Ray           (int bar, int baz)
95791a1265cSDwaipayan Ray
95891a1265cSDwaipayan Ray         This should be corrected to::
95991a1265cSDwaipayan Ray
96091a1265cSDwaipayan Ray           void foo(int bar, int baz)
96191a1265cSDwaipayan Ray
96291a1265cSDwaipayan Ray      2. Some arguments for the function definition do not
96391a1265cSDwaipayan Ray         have an identifier name.  Example::
96491a1265cSDwaipayan Ray
96591a1265cSDwaipayan Ray           void foo(int)
96691a1265cSDwaipayan Ray
96791a1265cSDwaipayan Ray         All arguments should have identifier names.
96891a1265cSDwaipayan Ray
9693337c3a1SDwaipayan Ray  **FUNCTION_WITHOUT_ARGS**
9703337c3a1SDwaipayan Ray    Function declarations without arguments like::
9713337c3a1SDwaipayan Ray
9723337c3a1SDwaipayan Ray      int foo()
9733337c3a1SDwaipayan Ray
9743337c3a1SDwaipayan Ray    should be::
9753337c3a1SDwaipayan Ray
9763337c3a1SDwaipayan Ray      int foo(void)
9773337c3a1SDwaipayan Ray
9783337c3a1SDwaipayan Ray  **GLOBAL_INITIALISERS**
9793337c3a1SDwaipayan Ray    Global variables should not be initialized explicitly to
9803337c3a1SDwaipayan Ray    0 (or NULL, false, etc.).  Your compiler (or rather your
9813337c3a1SDwaipayan Ray    loader, which is responsible for zeroing out the relevant
9823337c3a1SDwaipayan Ray    sections) automatically does it for you.
9833337c3a1SDwaipayan Ray
9843337c3a1SDwaipayan Ray  **INITIALISED_STATIC**
9853337c3a1SDwaipayan Ray    Static variables should not be initialized explicitly to zero.
9863337c3a1SDwaipayan Ray    Your compiler (or rather your loader) automatically does
9873337c3a1SDwaipayan Ray    it for you.
9883337c3a1SDwaipayan Ray
989250a0a5bSUtkarsh Verma  **MULTIPLE_ASSIGNMENTS**
990250a0a5bSUtkarsh Verma    Multiple assignments on a single line makes the code unnecessarily
991250a0a5bSUtkarsh Verma    complicated. So on a single line assign value to a single variable
992250a0a5bSUtkarsh Verma    only, this makes the code more readable and helps avoid typos.
993250a0a5bSUtkarsh Verma
9943337c3a1SDwaipayan Ray  **RETURN_PARENTHESES**
9953337c3a1SDwaipayan Ray    return is not a function and as such doesn't need parentheses::
9963337c3a1SDwaipayan Ray
9973337c3a1SDwaipayan Ray      return (bar);
9983337c3a1SDwaipayan Ray
9993337c3a1SDwaipayan Ray    can simply be::
10003337c3a1SDwaipayan Ray
10013337c3a1SDwaipayan Ray      return bar;
10023337c3a1SDwaipayan Ray
10033337c3a1SDwaipayan Ray
100476001b8bSDwaipayan RayPermissions
100576001b8bSDwaipayan Ray-----------
100676001b8bSDwaipayan Ray
100791a1265cSDwaipayan Ray  **DEVICE_ATTR_PERMS**
100891a1265cSDwaipayan Ray    The permissions used in DEVICE_ATTR are unusual.
100991a1265cSDwaipayan Ray    Typically only three permissions are used - 0644 (RW), 0444 (RO)
101091a1265cSDwaipayan Ray    and 0200 (WO).
101191a1265cSDwaipayan Ray
101291a1265cSDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/filesystems/sysfs.html#attributes
101391a1265cSDwaipayan Ray
101476001b8bSDwaipayan Ray  **EXECUTE_PERMISSIONS**
101576001b8bSDwaipayan Ray    There is no reason for source files to be executable.  The executable
101676001b8bSDwaipayan Ray    bit can be removed safely.
101776001b8bSDwaipayan Ray
101876001b8bSDwaipayan Ray  **EXPORTED_WORLD_WRITABLE**
101976001b8bSDwaipayan Ray    Exporting world writable sysfs/debugfs files is usually a bad thing.
102076001b8bSDwaipayan Ray    When done arbitrarily they can introduce serious security bugs.
102176001b8bSDwaipayan Ray    In the past, some of the debugfs vulnerabilities would seemingly allow
102276001b8bSDwaipayan Ray    any local user to write arbitrary values into device registers - a
102376001b8bSDwaipayan Ray    situation from which little good can be expected to emerge.
102476001b8bSDwaipayan Ray
102576001b8bSDwaipayan Ray    See: https://lore.kernel.org/linux-arm-kernel/[email protected]/
102676001b8bSDwaipayan Ray
102776001b8bSDwaipayan Ray  **NON_OCTAL_PERMISSIONS**
102876001b8bSDwaipayan Ray    Permission bits should use 4 digit octal permissions (like 0700 or 0444).
102976001b8bSDwaipayan Ray    Avoid using any other base like decimal.
103076001b8bSDwaipayan Ray
10313454cd56SUtkarsh Verma  **SYMBOLIC_PERMS**
10323454cd56SUtkarsh Verma    Permission bits in the octal form are more readable and easier to
10333454cd56SUtkarsh Verma    understand than their symbolic counterparts because many command-line
10343454cd56SUtkarsh Verma    tools use this notation. Experienced kernel developers have been using
10353454cd56SUtkarsh Verma    these traditional Unix permission bits for decades and so they find it
10363454cd56SUtkarsh Verma    easier to understand the octal notation than the symbolic macros.
10373454cd56SUtkarsh Verma    For example, it is harder to read S_IWUSR|S_IRUGO than 0644, which
10383454cd56SUtkarsh Verma    obscures the developer's intent rather than clarifying it.
10393454cd56SUtkarsh Verma
10403454cd56SUtkarsh Verma    See: https://lore.kernel.org/lkml/CA+55aFw5v23T-zvDZp-MmD_EYxF8WbafwwB59934FV7g21uMGQ@mail.gmail.com/
10413454cd56SUtkarsh Verma
104276001b8bSDwaipayan Ray
10432eecbab8SDwaipayan RaySpacing and Brackets
10442eecbab8SDwaipayan Ray--------------------
10452eecbab8SDwaipayan Ray
10462eecbab8SDwaipayan Ray  **ASSIGNMENT_CONTINUATIONS**
10472eecbab8SDwaipayan Ray    Assignment operators should not be written at the start of a
10482eecbab8SDwaipayan Ray    line but should follow the operand at the previous line.
10492eecbab8SDwaipayan Ray
10502eecbab8SDwaipayan Ray  **BRACES**
10512eecbab8SDwaipayan Ray    The placement of braces is stylistically incorrect.
10522eecbab8SDwaipayan Ray    The preferred way is to put the opening brace last on the line,
10532eecbab8SDwaipayan Ray    and put the closing brace first::
10542eecbab8SDwaipayan Ray
10552eecbab8SDwaipayan Ray      if (x is true) {
10562eecbab8SDwaipayan Ray              we do y
10572eecbab8SDwaipayan Ray      }
10582eecbab8SDwaipayan Ray
10592eecbab8SDwaipayan Ray    This applies for all non-functional blocks.
10602eecbab8SDwaipayan Ray    However, there is one special case, namely functions: they have the
10612eecbab8SDwaipayan Ray    opening brace at the beginning of the next line, thus::
10622eecbab8SDwaipayan Ray
10632eecbab8SDwaipayan Ray      int function(int x)
10642eecbab8SDwaipayan Ray      {
10652eecbab8SDwaipayan Ray              body of function
10662eecbab8SDwaipayan Ray      }
10672eecbab8SDwaipayan Ray
10682eecbab8SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#placing-braces-and-spaces
10692eecbab8SDwaipayan Ray
10702eecbab8SDwaipayan Ray  **BRACKET_SPACE**
10712eecbab8SDwaipayan Ray    Whitespace before opening bracket '[' is prohibited.
10722eecbab8SDwaipayan Ray    There are some exceptions:
10732eecbab8SDwaipayan Ray
10742eecbab8SDwaipayan Ray    1. With a type on the left::
10752eecbab8SDwaipayan Ray
107676001b8bSDwaipayan Ray        int [] a;
10772eecbab8SDwaipayan Ray
10782eecbab8SDwaipayan Ray    2. At the beginning of a line for slice initialisers::
10792eecbab8SDwaipayan Ray
10802eecbab8SDwaipayan Ray        [0...10] = 5,
10812eecbab8SDwaipayan Ray
10822eecbab8SDwaipayan Ray    3. Inside a curly brace::
10832eecbab8SDwaipayan Ray
10842eecbab8SDwaipayan Ray        = { [0...10] = 5 }
10852eecbab8SDwaipayan Ray
10862eecbab8SDwaipayan Ray  **CONCATENATED_STRING**
10872eecbab8SDwaipayan Ray    Concatenated elements should have a space in between.
10882eecbab8SDwaipayan Ray    Example::
10892eecbab8SDwaipayan Ray
10902eecbab8SDwaipayan Ray      printk(KERN_INFO"bar");
10912eecbab8SDwaipayan Ray
10922eecbab8SDwaipayan Ray    should be::
10932eecbab8SDwaipayan Ray
10942eecbab8SDwaipayan Ray      printk(KERN_INFO "bar");
10952eecbab8SDwaipayan Ray
10963337c3a1SDwaipayan Ray  **ELSE_AFTER_BRACE**
10973337c3a1SDwaipayan Ray    `else {` should follow the closing block `}` on the same line.
109876001b8bSDwaipayan Ray
10993337c3a1SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#placing-braces-and-spaces
11003337c3a1SDwaipayan Ray
11012eecbab8SDwaipayan Ray  **LINE_SPACING**
11022eecbab8SDwaipayan Ray    Vertical space is wasted given the limited number of lines an
11032eecbab8SDwaipayan Ray    editor window can display when multiple blank lines are used.
110476001b8bSDwaipayan Ray
11052eecbab8SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#spaces
11062eecbab8SDwaipayan Ray
11073337c3a1SDwaipayan Ray  **OPEN_BRACE**
11083337c3a1SDwaipayan Ray    The opening brace should be following the function definitions on the
11093337c3a1SDwaipayan Ray    next line.  For any non-functional block it should be on the same line
11103337c3a1SDwaipayan Ray    as the last construct.
111176001b8bSDwaipayan Ray
11123337c3a1SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#placing-braces-and-spaces
11133337c3a1SDwaipayan Ray
11143337c3a1SDwaipayan Ray  **POINTER_LOCATION**
11153337c3a1SDwaipayan Ray    When using pointer data or a function that returns a pointer type,
11163337c3a1SDwaipayan Ray    the preferred use of * is adjacent to the data name or function name
11173337c3a1SDwaipayan Ray    and not adjacent to the type name.
11183337c3a1SDwaipayan Ray    Examples::
11193337c3a1SDwaipayan Ray
11203337c3a1SDwaipayan Ray      char *linux_banner;
11213337c3a1SDwaipayan Ray      unsigned long long memparse(char *ptr, char **retptr);
11223337c3a1SDwaipayan Ray      char *match_strdup(substring_t *s);
11233337c3a1SDwaipayan Ray
11243337c3a1SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#spaces
11253337c3a1SDwaipayan Ray
11262eecbab8SDwaipayan Ray  **SPACING**
11272eecbab8SDwaipayan Ray    Whitespace style used in the kernel sources is described in kernel docs.
112876001b8bSDwaipayan Ray
11292eecbab8SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#spaces
11302eecbab8SDwaipayan Ray
11312eecbab8SDwaipayan Ray  **TRAILING_WHITESPACE**
11322eecbab8SDwaipayan Ray    Trailing whitespace should always be removed.
11332eecbab8SDwaipayan Ray    Some editors highlight the trailing whitespace and cause visual
11342eecbab8SDwaipayan Ray    distractions when editing files.
113576001b8bSDwaipayan Ray
11362eecbab8SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#spaces
11372eecbab8SDwaipayan Ray
113876001b8bSDwaipayan Ray  **UNNECESSARY_PARENTHESES**
113991a1265cSDwaipayan Ray    Parentheses are not required in the following cases:
114076001b8bSDwaipayan Ray
114176001b8bSDwaipayan Ray      1. Function pointer uses::
114276001b8bSDwaipayan Ray
114376001b8bSDwaipayan Ray          (foo->bar)();
114476001b8bSDwaipayan Ray
114576001b8bSDwaipayan Ray        could be::
114676001b8bSDwaipayan Ray
114776001b8bSDwaipayan Ray          foo->bar();
114876001b8bSDwaipayan Ray
114976001b8bSDwaipayan Ray      2. Comparisons in if::
115076001b8bSDwaipayan Ray
115176001b8bSDwaipayan Ray          if ((foo->bar) && (foo->baz))
115276001b8bSDwaipayan Ray          if ((foo == bar))
115376001b8bSDwaipayan Ray
115476001b8bSDwaipayan Ray        could be::
115576001b8bSDwaipayan Ray
115676001b8bSDwaipayan Ray          if (foo->bar && foo->baz)
115776001b8bSDwaipayan Ray          if (foo == bar)
115876001b8bSDwaipayan Ray
115976001b8bSDwaipayan Ray      3. addressof/dereference single Lvalues::
116076001b8bSDwaipayan Ray
116176001b8bSDwaipayan Ray          &(foo->bar)
116276001b8bSDwaipayan Ray          *(foo->bar)
116376001b8bSDwaipayan Ray
116476001b8bSDwaipayan Ray        could be::
116576001b8bSDwaipayan Ray
116676001b8bSDwaipayan Ray          &foo->bar
116776001b8bSDwaipayan Ray          *foo->bar
116876001b8bSDwaipayan Ray
11693337c3a1SDwaipayan Ray  **WHILE_AFTER_BRACE**
11703337c3a1SDwaipayan Ray    while should follow the closing bracket on the same line::
11713337c3a1SDwaipayan Ray
11723337c3a1SDwaipayan Ray      do {
11733337c3a1SDwaipayan Ray              ...
11743337c3a1SDwaipayan Ray      } while(something);
11753337c3a1SDwaipayan Ray
11763337c3a1SDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/coding-style.html#placing-braces-and-spaces
11773337c3a1SDwaipayan Ray
11782eecbab8SDwaipayan Ray
11792eecbab8SDwaipayan RayOthers
11802eecbab8SDwaipayan Ray------
11812eecbab8SDwaipayan Ray
11822eecbab8SDwaipayan Ray  **CONFIG_DESCRIPTION**
11832eecbab8SDwaipayan Ray    Kconfig symbols should have a help text which fully describes
11842eecbab8SDwaipayan Ray    it.
11853337c3a1SDwaipayan Ray
11863337c3a1SDwaipayan Ray  **CORRUPTED_PATCH**
11873337c3a1SDwaipayan Ray    The patch seems to be corrupted or lines are wrapped.
11883337c3a1SDwaipayan Ray    Please regenerate the patch file before sending it to the maintainer.
11893337c3a1SDwaipayan Ray
119091a1265cSDwaipayan Ray  **CVS_KEYWORD**
119191a1265cSDwaipayan Ray    Since linux moved to git, the CVS markers are no longer used.
119291a1265cSDwaipayan Ray    So, CVS style keywords ($Id$, $Revision$, $Log$) should not be
119391a1265cSDwaipayan Ray    added.
119491a1265cSDwaipayan Ray
119591a1265cSDwaipayan Ray  **DEFAULT_NO_BREAK**
119691a1265cSDwaipayan Ray    switch default case is sometimes written as "default:;".  This can
119791a1265cSDwaipayan Ray    cause new cases added below default to be defective.
119891a1265cSDwaipayan Ray
119991a1265cSDwaipayan Ray    A "break;" should be added after empty default statement to avoid
120091a1265cSDwaipayan Ray    unwanted fallthrough.
120191a1265cSDwaipayan Ray
12023337c3a1SDwaipayan Ray  **DOS_LINE_ENDINGS**
12033337c3a1SDwaipayan Ray    For DOS-formatted patches, there are extra ^M symbols at the end of
12043337c3a1SDwaipayan Ray    the line.  These should be removed.
12053337c3a1SDwaipayan Ray
120691a1265cSDwaipayan Ray  **DT_SCHEMA_BINDING_PATCH**
120791a1265cSDwaipayan Ray    DT bindings moved to a json-schema based format instead of
120891a1265cSDwaipayan Ray    freeform text.
12093337c3a1SDwaipayan Ray
121091a1265cSDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/devicetree/bindings/writing-schema.html
121176001b8bSDwaipayan Ray
121291a1265cSDwaipayan Ray  **DT_SPLIT_BINDING_PATCH**
121391a1265cSDwaipayan Ray    Devicetree bindings should be their own patch.  This is because
121491a1265cSDwaipayan Ray    bindings are logically independent from a driver implementation,
121591a1265cSDwaipayan Ray    they have a different maintainer (even though they often
121691a1265cSDwaipayan Ray    are applied via the same tree), and it makes for a cleaner history in the
121791a1265cSDwaipayan Ray    DT only tree created with git-filter-branch.
121876001b8bSDwaipayan Ray
121991a1265cSDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/devicetree/bindings/submitting-patches.html#i-for-patch-submitters
122076001b8bSDwaipayan Ray
122191a1265cSDwaipayan Ray  **EMBEDDED_FILENAME**
122291a1265cSDwaipayan Ray    Embedding the complete filename path inside the file isn't particularly
122391a1265cSDwaipayan Ray    useful as often the path is moved around and becomes incorrect.
122476001b8bSDwaipayan Ray
122591a1265cSDwaipayan Ray  **FILE_PATH_CHANGES**
122691a1265cSDwaipayan Ray    Whenever files are added, moved, or deleted, the MAINTAINERS file
122791a1265cSDwaipayan Ray    patterns can be out of sync or outdated.
122876001b8bSDwaipayan Ray
122991a1265cSDwaipayan Ray    So MAINTAINERS might need updating in these cases.
123076001b8bSDwaipayan Ray
123176001b8bSDwaipayan Ray  **MEMSET**
123276001b8bSDwaipayan Ray    The memset use appears to be incorrect.  This may be caused due to
123376001b8bSDwaipayan Ray    badly ordered parameters.  Please recheck the usage.
12343337c3a1SDwaipayan Ray
12353337c3a1SDwaipayan Ray  **NOT_UNIFIED_DIFF**
12363337c3a1SDwaipayan Ray    The patch file does not appear to be in unified-diff format.  Please
12373337c3a1SDwaipayan Ray    regenerate the patch file before sending it to the maintainer.
12383337c3a1SDwaipayan Ray
12393337c3a1SDwaipayan Ray  **PRINTF_0XDECIMAL**
12403337c3a1SDwaipayan Ray    Prefixing 0x with decimal output is defective and should be corrected.
12413337c3a1SDwaipayan Ray
124276001b8bSDwaipayan Ray  **SPDX_LICENSE_TAG**
124376001b8bSDwaipayan Ray    The source file is missing or has an improper SPDX identifier tag.
124476001b8bSDwaipayan Ray    The Linux kernel requires the precise SPDX identifier in all source files,
124576001b8bSDwaipayan Ray    and it is thoroughly documented in the kernel docs.
124676001b8bSDwaipayan Ray
124776001b8bSDwaipayan Ray    See: https://www.kernel.org/doc/html/latest/process/license-rules.html
124876001b8bSDwaipayan Ray
124976001b8bSDwaipayan Ray  **TYPO_SPELLING**
125076001b8bSDwaipayan Ray    Some words may have been misspelled.  Consider reviewing them.
1251