hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/Documentation/process/coding-style.rst
....@@ -56,7 +56,7 @@
5656 case 'K':
5757 case 'k':
5858 mem <<= 10;
59
- /* fall through */
59
+ fallthrough;
6060 default:
6161 break;
6262 }
....@@ -84,15 +84,20 @@
8484 Coding style is all about readability and maintainability using commonly
8585 available tools.
8686
87
-The limit on the length of lines is 80 columns and this is a strongly
88
-preferred limit.
87
+The preferred limit on the length of a single line is 80 columns.
8988
90
-Statements longer than 80 columns will be broken into sensible chunks, unless
91
-exceeding 80 columns significantly increases readability and does not hide
92
-information. Descendants are always substantially shorter than the parent and
93
-are placed substantially to the right. The same applies to function headers
94
-with a long argument list. However, never break user-visible strings such as
95
-printk messages, because that breaks the ability to grep for them.
89
+Statements longer than 80 columns should be broken into sensible chunks,
90
+unless exceeding 80 columns significantly increases readability and does
91
+not hide information.
92
+
93
+Descendants are always substantially shorter than the parent and
94
+are placed substantially to the right. A very commonly used style
95
+is to align descendants to a function open parenthesis.
96
+
97
+These same rules are applied to function headers with a long argument list.
98
+
99
+However, never break user-visible strings such as printk messages because
100
+that breaks the ability to grep for them.
96101
97102
98103 3) Placing Braces and Spaces
....@@ -284,9 +289,9 @@
284289 4) Naming
285290 ---------
286291
287
-C is a Spartan language, and so should your naming be. Unlike Modula-2
288
-and Pascal programmers, C programmers do not use cute names like
289
-ThisVariableIsATemporaryCounter. A C programmer would call that
292
+C is a Spartan language, and your naming conventions should follow suit.
293
+Unlike Modula-2 and Pascal programmers, C programmers do not use cute
294
+names like ThisVariableIsATemporaryCounter. A C programmer would call that
290295 variable ``tmp``, which is much easier to write, and not the least more
291296 difficult to understand.
292297
....@@ -300,9 +305,9 @@
300305 ``count_active_users()`` or similar, you should **not** call it ``cntusr()``.
301306
302307 Encoding the type of a function into the name (so-called Hungarian
303
-notation) is brain damaged - the compiler knows the types anyway and can
304
-check those, and it only confuses the programmer. No wonder MicroSoft
305
-makes buggy programs.
308
+notation) is asinine - the compiler knows the types anyway and can check
309
+those, and it only confuses the programmer. No wonder Microsoft makes buggy
310
+programs.
306311
307312 LOCAL variable names should be short, and to the point. If you have
308313 some random integer loop counter, it should probably be called ``i``.
....@@ -314,6 +319,26 @@
314319 problem, which is called the function-growth-hormone-imbalance syndrome.
315320 See chapter 6 (Functions).
316321
322
+For symbol names and documentation, avoid introducing new usage of
323
+'master / slave' (or 'slave' independent of 'master') and 'blacklist /
324
+whitelist'.
325
+
326
+Recommended replacements for 'master / slave' are:
327
+ '{primary,main} / {secondary,replica,subordinate}'
328
+ '{initiator,requester} / {target,responder}'
329
+ '{controller,host} / {device,worker,proxy}'
330
+ 'leader / follower'
331
+ 'director / performer'
332
+
333
+Recommended replacements for 'blacklist/whitelist' are:
334
+ 'denylist / allowlist'
335
+ 'blocklist / passlist'
336
+
337
+Exceptions for introducing new usage is to maintain a userspace ABI/API,
338
+or when updating code for an existing (as of 2020) hardware or protocol
339
+specification that mandates those terms. For new specifications
340
+translate specification usage of the terminology to the kernel coding
341
+standard where possible.
317342
318343 5) Typedefs
319344 -----------
....@@ -442,6 +467,9 @@
442467 In function prototypes, include parameter names with their data types.
443468 Although this is not required by the C language, it is preferred in Linux
444469 because it is a simple way to add valuable information for the reader.
470
+
471
+Do not use the ``extern`` keyword with function prototypes as this makes
472
+lines longer and isn't strictly necessary.
445473
446474
447475 7) Centralized exiting of functions
....@@ -592,26 +620,43 @@
592620 (* (max steps 1)
593621 c-basic-offset)))
594622
595
- (add-hook 'c-mode-common-hook
596
- (lambda ()
597
- ;; Add kernel style
598
- (c-add-style
599
- "linux-tabs-only"
600
- '("linux" (c-offsets-alist
601
- (arglist-cont-nonempty
602
- c-lineup-gcc-asm-reg
603
- c-lineup-arglist-tabs-only))))))
623
+ (dir-locals-set-class-variables
624
+ 'linux-kernel
625
+ '((c-mode . (
626
+ (c-basic-offset . 8)
627
+ (c-label-minimum-indentation . 0)
628
+ (c-offsets-alist . (
629
+ (arglist-close . c-lineup-arglist-tabs-only)
630
+ (arglist-cont-nonempty .
631
+ (c-lineup-gcc-asm-reg c-lineup-arglist-tabs-only))
632
+ (arglist-intro . +)
633
+ (brace-list-intro . +)
634
+ (c . c-lineup-C-comments)
635
+ (case-label . 0)
636
+ (comment-intro . c-lineup-comment)
637
+ (cpp-define-intro . +)
638
+ (cpp-macro . -1000)
639
+ (cpp-macro-cont . +)
640
+ (defun-block-intro . +)
641
+ (else-clause . 0)
642
+ (func-decl-cont . +)
643
+ (inclass . +)
644
+ (inher-cont . c-lineup-multi-inher)
645
+ (knr-argdecl-intro . 0)
646
+ (label . -1000)
647
+ (statement . 0)
648
+ (statement-block-intro . +)
649
+ (statement-case-intro . +)
650
+ (statement-cont . +)
651
+ (substatement . +)
652
+ ))
653
+ (indent-tabs-mode . t)
654
+ (show-trailing-whitespace . t)
655
+ ))))
604656
605
- (add-hook 'c-mode-hook
606
- (lambda ()
607
- (let ((filename (buffer-file-name)))
608
- ;; Enable kernel mode for the appropriate files
609
- (when (and filename
610
- (string-match (expand-file-name "~/src/linux-trees")
611
- filename))
612
- (setq indent-tabs-mode t)
613
- (setq show-trailing-whitespace t)
614
- (c-set-style "linux-tabs-only")))))
657
+ (dir-locals-set-directory-class
658
+ (expand-file-name "~/src/linux-trees")
659
+ 'linux-kernel)
615660
616661 This will make emacs go better with the kernel coding style for C
617662 files below ``~/src/linux-trees``.
....@@ -666,7 +711,7 @@
666711 ...
667712
668713 For full documentation on the configuration files, see the file
669
-Documentation/kbuild/kconfig-language.txt.
714
+Documentation/kbuild/kconfig-language.rst.
670715
671716
672717 11) Data structures
....@@ -786,9 +831,9 @@
786831 ----------------------------
787832
788833 Kernel developers like to be seen as literate. Do mind the spelling
789
-of kernel messages to make a good impression. Do not use crippled
790
-words like ``dont``; use ``do not`` or ``don't`` instead. Make the messages
791
-concise, clear, and unambiguous.
834
+of kernel messages to make a good impression. Do not use incorrect
835
+contractions like ``dont``; use ``do not`` or ``don't`` instead. Make the
836
+messages concise, clear, and unambiguous.
792837
793838 Kernel messages do not have to be terminated with a period.
794839
....@@ -823,7 +868,8 @@
823868 The kernel provides the following general purpose memory allocators:
824869 kmalloc(), kzalloc(), kmalloc_array(), kcalloc(), vmalloc(), and
825870 vzalloc(). Please refer to the API documentation for further information
826
-about them.
871
+about them. :ref:`Documentation/core-api/memory-allocation.rst
872
+<memory_allocation>`
827873
828874 The preferred form for passing a size of a struct is the following:
829875
....@@ -854,6 +900,9 @@
854900 Both forms check for overflow on the allocation size n * sizeof(...),
855901 and return NULL if that occurred.
856902
903
+These generic allocation functions all emit a stack dump on failure when used
904
+without __GFP_NOWARN so there is no use in emitting an additional failure
905
+message when NULL is returned.
857906
858907 15) The inline disease
859908 ----------------------
....@@ -918,7 +967,37 @@
918967 NULL or the ERR_PTR mechanism to report failure.
919968
920969
921
-17) Don't re-invent the kernel macros
970
+17) Using bool
971
+--------------
972
+
973
+The Linux kernel bool type is an alias for the C99 _Bool type. bool values can
974
+only evaluate to 0 or 1, and implicit or explicit conversion to bool
975
+automatically converts the value to true or false. When using bool types the
976
+!! construction is not needed, which eliminates a class of bugs.
977
+
978
+When working with bool values the true and false definitions should be used
979
+instead of 1 and 0.
980
+
981
+bool function return types and stack variables are always fine to use whenever
982
+appropriate. Use of bool is encouraged to improve readability and is often a
983
+better option than 'int' for storing boolean values.
984
+
985
+Do not use bool if cache line layout or size of the value matters, as its size
986
+and alignment varies based on the compiled architecture. Structures that are
987
+optimized for alignment and size should not use bool.
988
+
989
+If a structure has many true/false values, consider consolidating them into a
990
+bitfield with 1 bit members, or using an appropriate fixed width type, such as
991
+u8.
992
+
993
+Similarly for function arguments, many true/false values can be consolidated
994
+into a single bitwise 'flags' argument and 'flags' can often be a more
995
+readable alternative if the call-sites have naked true/false constants.
996
+
997
+Otherwise limited use of bool in structures and arguments can improve
998
+readability.
999
+
1000
+18) Don't re-invent the kernel macros
9221001 -------------------------------------
9231002
9241003 The header file include/linux/kernel.h contains a number of macros that
....@@ -934,14 +1013,14 @@
9341013
9351014 .. code-block:: c
9361015
937
- #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
1016
+ #define sizeof_field(t, f) (sizeof(((t*)0)->f))
9381017
9391018 There are also min() and max() macros that do strict type checking if you
9401019 need them. Feel free to peruse that header file to see what else is already
9411020 defined that you shouldn't reproduce in your code.
9421021
9431022
944
-18) Editor modelines and other cruft
1023
+19) Editor modelines and other cruft
9451024 ------------------------------------
9461025
9471026 Some editors can interpret configuration information embedded in source files,
....@@ -975,7 +1054,7 @@
9751054 work correctly.
9761055
9771056
978
-19) Inline assembly
1057
+20) Inline assembly
9791058 -------------------
9801059
9811060 In architecture-specific code, you may need to use inline assembly to interface
....@@ -1007,7 +1086,7 @@
10071086 : /* outputs */ : /* inputs */ : /* clobbers */);
10081087
10091088
1010
-20) Conditional Compilation
1089
+21) Conditional Compilation
10111090 ---------------------------
10121091
10131092 Wherever possible, don't use preprocessor conditionals (#if, #ifdef) in .c
....@@ -1070,10 +1149,10 @@
10701149 ISBN 0-201-61586-X.
10711150
10721151 GNU manuals - where in compliance with K&R and this text - for cpp, gcc,
1073
-gcc internals and indent, all available from http://www.gnu.org/manual/
1152
+gcc internals and indent, all available from https://www.gnu.org/manual/
10741153
10751154 WG14 is the international standardization working group for the programming
10761155 language C, URL: http://www.open-std.org/JTC1/SC22/WG14/
10771156
1078
-Kernel process/coding-style.rst, by greg@kroah.com at OLS 2002:
1157
+Kernel :ref:`process/coding-style.rst <codingstyle>`, by greg@kroah.com at OLS 2002:
10791158 http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/