History

coding-style: Clarify the expectations around bool

There has been some confusion since checkpatch started warning about bool use in structures, and people have been avoiding using it. Many people feel there is still a legitimate place for bool in structures, so provide some guidance on bool usage derived from the entire thread that spawned the checkpatch warning.

Jason Gunthorpe committed 7 years ago

Commit 79676561 parent b04c11c2 files · +34 −17Browse files

Unified · Split

2 files
  1. Documentation/process/coding-style.rst +34 −4
  2. scripts/checkpatch.pl +0 −13
Documentation/process/coding-style.rst+34−4
@@ -938,7 +938,37 @@ result. Typical examples would be functions that return pointers; they use · 937 unmodified lines
938938 NULL or the ERR_PTR mechanism to report failure.
939939
940940
941−17) Don't re-invent the kernel macros
941+17) Using bool
942+--------------
943+
944+The Linux kernel bool type is an alias for the C99 _Bool type. bool values can
945+only evaluate to 0 or 1, and implicit or explicit conversion to bool
946+automatically converts the value to true or false. When using bool types the
947+!! construction is not needed, which eliminates a class of bugs.
948+
949+When working with bool values the true and false definitions should be used
950+instead of 1 and 0.
951+
952+bool function return types and stack variables are always fine to use whenever
953+appropriate. Use of bool is encouraged to improve readability and is often a
954+better option than 'int' for storing boolean values.
955+
956+Do not use bool if cache line layout or size of the value matters, as its size
957+and alignment varies based on the compiled architecture. Structures that are
958+optimized for alignment and size should not use bool.
959+
960+If a structure has many true/false values, consider consolidating them into a
961+bitfield with 1 bit members, or using an appropriate fixed width type, such as
962+u8.
963+
964+Similarly for function arguments, many true/false values can be consolidated
965+into a single bitwise 'flags' argument and 'flags' can often be a more
966+readable alternative if the call-sites have naked true/false constants.
967+
968+Otherwise limited use of bool in structures and arguments can improve
969+readability.
970+
971+18) Don't re-invent the kernel macros
942972 -------------------------------------
943973
944974 The header file include/linux/kernel.h contains a number of macros that
@@ -961,7 +991,7 @@ need them. Feel free to peruse that header file to see what else is already · 16 unmodified lines
961991 defined that you shouldn't reproduce in your code.
962992
963993
964−18) Editor modelines and other cruft
994+19) Editor modelines and other cruft
965995 ------------------------------------
966996
967997 Some editors can interpret configuration information embedded in source files,
@@ -995,7 +1025,7 @@ own custom mode, or may have some other magic method for making indentation · 27 unmodified lines
9951025 work correctly.
9961026
9971027
998−19) Inline assembly
1028+20) Inline assembly
9991029 -------------------
10001030
10011031 In architecture-specific code, you may need to use inline assembly to interface
@@ -1027,7 +1057,7 @@ the next instruction in the assembly output: · 25 unmodified lines
10271057 : /* outputs */ : /* inputs */ : /* clobbers */);
10281058
10291059
1030−20) Conditional Compilation
1060+21) Conditional Compilation
10311061 ---------------------------
10321062
10331063 Wherever possible, don't use preprocessor conditionals (#if, #ifdef) in .c
scripts/checkpatch.pl+0−13
@@ -6368,19 +6368,6 @@ sub process { · 6367 unmodified lines
63686368 }
63696369 }
63706370
6371−# check for bool bitfields
6372− if ($sline =~ /^.\s+bool\s*$Ident\s*:\s*\d+\s*;/) {
6373− WARN("BOOL_BITFIELD",
6374− "Avoid using bool as bitfield. Prefer bool bitfields as unsigned int or u<8|16|32>\n" . $herecurr);
6375− }
6376−
6377−# check for bool use in .h files
6378− if ($realfile =~ /\.h$/ &&
6379− $sline =~ /^.\s+bool\s*$Ident\s*(?::\s*d+\s*)?;/) {
6380− CHK("BOOL_MEMBER",
6381− "Avoid using bool structure members because of possible alignment issues - see: https://lkml.org/lkml/2017/11/21/384\n" . $herecurr);
6382− }
6383−
63846371 # check for semaphores initialized locked
63856372 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
63866373 WARN("CONSIDER_COMPLETION",