coding style of linux kernel
TRANSCRIPT
![Page 1: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/1.jpg)
Coding Style in Linux kernelPeter Chang2012/06/17
![Page 2: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/2.jpg)
Reference
“Linux Kernel Coding Style”, Linus Torvals, https://computing.llnl.gov/linux/slurm/coding_style.pdf
“Documentation / Coding-Style”, Greg Kroah-Hartman, IBM, Linux Symposium 2002
(Slide version: http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/index.html)
http://www.kernel.org/doc/Documentation/CodingStyle
![Page 3: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/3.jpg)
“First off, I'd suggest printing out a copy of the GNU coding standards, and NOT read it.
Burn them, it's a great symbolic gesture.”~ Linus Torvalds
![Page 4: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/4.jpg)
Indentation
![Page 5: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/5.jpg)
Indentation
All tabs are 8 characters.
Use tabs.
If your code need 3 layers of indentation, it’s your problem. Solve it!
![Page 6: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/6.jpg)
Indentation
Don’t put two statement in the same line.
Use a good editor. Don’t put andy empty line in the end of any file.
![Page 7: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/7.jpg)
Indentation
Put switch and case into the same layer of indentation
![Page 8: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/8.jpg)
Indentation
![Page 9: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/9.jpg)
Indentation
Good examples:
fs/*
kernel/*
Bad examples:
drivers/scsi/sg.c
![Page 10: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/10.jpg)
Break long lines & strings
![Page 11: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/11.jpg)
Break long lines & strings
There are only 80 char. in a line.Because we hate this:
![Page 12: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/12.jpg)
Break long lines & strings
![Page 13: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/13.jpg)
Braces
![Page 14: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/14.jpg)
Braces
![Page 15: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/15.jpg)
Braces
![Page 16: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/16.jpg)
Braces
![Page 17: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/17.jpg)
Braces
![Page 18: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/18.jpg)
Braces
![Page 19: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/19.jpg)
Braces
![Page 20: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/20.jpg)
Braces
![Page 21: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/21.jpg)
Braces
Good examples:
drivers/scsi/qla1280.c
kernel/*.c
Bad examples:
fs/devfs/*
![Page 22: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/22.jpg)
Spaces
![Page 23: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/23.jpg)
Spaces
“To be, or not to be”
![Page 24: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/24.jpg)
Spaces
Use a space after these keywords:
if, switch, case, for, do, while
Do not add spaces after these keywords:
sizeof, typeof, alignof, __attribute__
![Page 25: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/25.jpg)
Spaces
Good example:
s = sizeof(struct file);
Suck example:
s=sizeof( struct file );
![Page 26: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/26.jpg)
Spaces
Add a space before and after the following binary and ternary operators:
= , + , - , * , /
< , > , % , & , | , ^
<= , >= , == , != , ? , :
![Page 27: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/27.jpg)
Spaces
DO NOT Add a space after the following operators:
&, *, +, -, ~, !, sizeof, typeof, alignof, __attribute__, define
![Page 28: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/28.jpg)
Spaces
DO NOT add a space before and after the following operators:
++,--
![Page 29: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/29.jpg)
Spaces
DO NOT add a space before and after the following operators:
.
->
![Page 30: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/30.jpg)
Naming
![Page 31: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/31.jpg)
Naming
Be descriptive
Be concise
Good example:
void enable_mem_mailbox();
![Page 32: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/32.jpg)
Naming
No MiXedCaSe
Bad examples:
int CommandAllocationGroupSize;
void DAC960_V1_EnableMemoryMailboxInterface();
![Page 33: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/33.jpg)
Naming
Global variable should be use only if they are absolutely necessary.
![Page 34: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/34.jpg)
Naming
Local variable should be short and the point
Good example:
i,j ( as a counter for a loop )
Bad example:
loop_counter
![Page 35: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/35.jpg)
Naming
Don’t encoding the type of a function into the name (a.k.a. Hungarian notation).
“It’s brain damaged” ~ Linus Torvalds
![Page 36: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/36.jpg)
Functions
![Page 37: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/37.jpg)
Functions
Do one thing, and do it well
Short, one or two screens of text
![Page 38: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/38.jpg)
Functions
Okay to have longer function doing small different things
![Page 39: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/39.jpg)
Functions
“如果你寫出了⼀一個很複雜的function,表示你可能程度比高中⼀一年級的學生還差,因為你不會用function”~ Linus Torvalds
![Page 40: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/40.jpg)
Functions
If more than 10 local variables, it’s too complex
![Page 41: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/41.jpg)
Functions
“人最多同時只能記7件不同的事情。如果你覺得你是天才,那或許你兩個星期後就還看的懂你那複雜的函式”~ Linus Torvalds
![Page 42: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/42.jpg)
Functions
Separate different functions with a blank line
If your function will be exported, use EXPORT* macro
![Page 43: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/43.jpg)
Functions
![Page 44: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/44.jpg)
Indentation
Good examples:
fs/*.c
Bad examples:
drivers/hotplug/ibmphp_res.c
include WTF 370 lines
drivers/usb/serial/usbserials.c
use WTF 21 local variables
![Page 45: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/45.jpg)
GOTOCentralize exiting of functions
![Page 46: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/46.jpg)
被封印的GOTO
“危險,不要用”
~ 好像小時候聽大人講過
“下⼀一節是要講Goto,但我們跳過,你們寫程式也不要用Goto”
~ 好像當初上大⼀一程設上課有講過
![Page 47: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/47.jpg)
只是歷史遺跡?
組合語言發展過來的遺跡?
jump?
branch?
![Page 48: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/48.jpg)
難道GOTO錯了嗎?
![Page 49: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/49.jpg)
As the matter of fact, ...
The equivalent to GOTO statement is used frequently by compiler.
For what?
Unconditional jump instruction
![Page 50: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/50.jpg)
Timing of using GOTO
Let function from multiple exit to only one exit
![Page 51: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/51.jpg)
Reason of using GOTO
Unconditional statements are easier to understand and follow
nested is reduced!!!
errors by not updating individual exit points when making modifications are prevented
saves the compiler work to optimize redundant code away ;)
![Page 52: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/52.jpg)
Example of using GOTO
![Page 53: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/53.jpg)
Comment
![Page 54: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/54.jpg)
Comments
Bad comments
explain how code works
say who wrote this function
have last modified date
have other trivial things
![Page 55: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/55.jpg)
Comments
Good comments
explain what
explain why
should be at the beginning of function
![Page 56: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/56.jpg)
Comments
Do not use:
// statement of comment ( C99-style )
Do use:
/* statement of comment ( C89-style) */
![Page 57: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/57.jpg)
Comments
![Page 58: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/58.jpg)
Comments
![Page 59: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/59.jpg)
Comments
![Page 60: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/60.jpg)
Kconfig
![Page 61: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/61.jpg)
Indentation of Kconfig
Lines under a "config" definition are indented with one tab
help text is indented an additional two spaces
![Page 62: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/62.jpg)
Indentation of Kconfig
![Page 63: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/63.jpg)
Unstable Features in Kconfig
Features that might still be considered unstable should be defined as dependent on "EXPERIMENTAL"
![Page 64: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/64.jpg)
Unstable Features in Kconfig
![Page 65: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/65.jpg)
Dangerous feature in Kconfig
seriously dangerous features should advertise this prominently in their prompt string
![Page 66: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/66.jpg)
Dangerous feature in Kconfig
![Page 67: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/67.jpg)
Macro, enum, RTL
![Page 68: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/68.jpg)
CAPITALIZE
Names of macros defining constants
labels in enums
For defining several related constants
![Page 69: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/69.jpg)
CAPITALIZE
Example:
#define CONSTANT 0xffff0000
![Page 70: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/70.jpg)
LOOOOONG Macro
Macros with multiple statements should be enclosed in a do - while block
![Page 71: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/71.jpg)
LOOOOONG Macro
![Page 72: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/72.jpg)
Don’t use macro in these cases
1. macros that affect control flow
It looks like a function call but exits the "calling" function; don't break the internal parsers of those who will read the code.
![Page 73: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/73.jpg)
Don’t use macro in these cases
![Page 74: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/74.jpg)
Don’t use macro in these cases
2. macros that depend on having a local variable with a magic name
might look like a good thing, but it's confusing as hell when one reads the code and it's prone to breakage from seemingly innocent changes.
![Page 75: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/75.jpg)
Don’t use macro in these cases
Example for 2:
#define FOO(val) bar(index, val)
![Page 76: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/76.jpg)
Don’t use macro in these cases
3. macros with arguments that are used as l-values
Ex: FOO(x) = y;
will bite you if somebody e.g. turns FOO into an inline function
![Page 77: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/77.jpg)
Don’t use macro in these cases
4. forgetting about precedence
macros defining constants using expressions must enclose the expression in parentheses.
Beware of similar issues with macros using parameters.
![Page 78: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/78.jpg)
Don’t use macro in these cases
![Page 79: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/79.jpg)
Allocate memory
![Page 80: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/80.jpg)
Function return value and names
![Page 81: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/81.jpg)
Unwritten rules
![Page 82: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/82.jpg)
Unwritten rules
Use code that is already present
string
byte order functions
linked lists
![Page 83: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/83.jpg)
typedef is evil
![Page 84: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/84.jpg)
![Page 85: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/85.jpg)
EVIL! EVIL! EVIL!It hides the real type of the variable
Allows programmers to get into trouble
large struct. on the stack
large struct. passed as return value
Can hid e long structure definition
pick a better name
typedef just signify a pointer type
could you be lazier?
![Page 86: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/86.jpg)
Well, mostly...
Base system types
list_t
u8, u16, u64
Function pointer
![Page 87: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/87.jpg)
No #ifdef in .c files
#ifdef belongs in .h file
Let your compiler do its work
![Page 88: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/88.jpg)
Labeled elements in Initializers
![Page 89: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/89.jpg)
Labeled elements in Initializers
![Page 90: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/90.jpg)
Labeled elements in Initializers
![Page 91: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/91.jpg)
Labeled elements in Initializers
![Page 92: Coding style of Linux Kernel](https://reader037.vdocument.in/reader037/viewer/2022102621/555a276fd8b42a900d8b495f/html5/thumbnails/92.jpg)
Labeled elements in Initializers