The Linux kernel has a well-defined coding style described inDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/DeelerDev/linux/llms.txt
Use this file to discover all available pages before exploring further.
Documentation/process/coding-style.rst. Reviewers routinely reject patches that do not follow it, and many will not even read the code until the style is corrected. The rules below come directly from the kernel’s own documentation.
Indentation
Tabs are 8 characters wide. Indentation uses tabs, not spaces.switch and its case labels align at the same column. Never double-indent case labels.
8-character indentation is intentional. If your code reaches more than three levels of nesting, that is a signal to refactor — not a reason to switch to 4-space indents.
printk messages) across lines, because that prevents grepping for them.
Placing braces
The kernel follows K&R style. For all non-function statement blocks (if, switch, for, while, do), the opening brace goes at the end of the line:
else, while in a do-while):
Spaces
Use a space after keywords:if, switch, case, for, do, while. Do not add a space after sizeof, typeof, alignof, or __attribute__.
* adjacent to the variable or function name, not the type:
Naming
Global functions and variables need descriptive names. A function that counts active users should be namedcount_active_users(), not cntusr(). Local variables can be short — a loop counter is i, a temporary value is tmp.
CamelCase is not used. Use lowercase_with_underscores for all identifiers. Hungarian notation (encoding types into names) is explicitly discouraged — the compiler already knows the types.
For new symbol names, avoid the terms master/slave and blacklist/whitelist. Preferred replacements include:
primary/secondaryorinitiator/targetinstead ofmaster/slaveallowlist/denylistinstead ofwhitelist/blacklist
Typedefs
Avoidtypedef for structures and pointers. When code says vps_t a;, you cannot tell what a is. When it says struct virtual_container *a;, you can. Typedefs are acceptable only for:
- Truly opaque objects (accessed only through accessor functions)
- Fixed-width integer types where the abstraction is meaningful (
u8,u16,u32,u64) - Types created specifically for sparse type-checking
Functions
Functions should be short and do one thing. The guideline is one or two screenfuls of text at 80×24. If a function needs more than 5–10 local variables, it is too complex and should be split.EXPORT_SYMBOL immediately follows the closing brace of an exported function.
For centralized cleanup, goto is idiomatic in kernel code:
out_free_buffer: is good, err1: is not.
Comments
Comments should say what the code does and why, not how. Never use comments to explain badly-written code — fix the code instead. The preferred multi-line comment style is://) are not used. Use the block comment style consistently.
For API documentation, use the kernel-doc format (see Documentation/doc-guide/). Do not add boilerplate kernel-doc that simply restates what the function signature already says.
Macros
Macro names that define constants and enum labels are capitalized:#define CONSTANT 0x12345. Function-like macros may use lowercase.
Prefer static inline functions over function-like macros. Inline functions are type-safe, do not evaluate arguments multiple times, and are easier to debug.
Multi-statement macros must be enclosed in do { } while (0):
- Macros that affect control flow (hidden
returnstatements) - Macros that depend on a local variable with a magic name
- Macros with arguments used as l-values
Error handling
Functions that are commands (imperative names likeadd_work) return 0 on success and a negative errno value on failure. Functions that are predicates (like pci_dev_present) return a boolean where non-zero means success.
BUG() or BUG_ON() calls. Use WARN_ON_ONCE() instead, and include recovery code where possible.
Validating style with checkpatch.pl
Before submitting any patch, run it throughscripts/checkpatch.pl:
| Level | Meaning |
|---|---|
ERROR | Very likely wrong; must be addressed |
WARNING | Requires careful review |
CHECK | Requires thought; enabled with --strict |
clang-format with the kernel’s .clang-format file for automatic reformatting, and editors that support EditorConfig will pick up the kernel’s indentation settings automatically from the .editorconfig file in the tree root.