Documentation/process/coding-style.rst. Adhering to it is not optional — patches that violate these conventions are routinely rejected by maintainers before any functional review takes place.
Run
scripts/checkpatch.pl on your patch before submitting. It catches the majority of style violations automatically.Indentation
Tabs are 8 characters. Indentation is also 8 characters. There are no exceptions outside of Kconfig files.switch and its case labels align in the same column. Do not double-indent case labels.
If you need more than 3 levels of indentation, your code needs restructuring — the 8-character tab makes deep nesting visually painful by design.
Line length
The preferred limit is 80 columns. Statements longer than 80 columns should be broken into sensible chunks. The modern trend in the kernel allows up to 100 columns when it significantly improves readability without hiding information.printk messages — doing so breaks the ability to grep for them in dmesg output.
Placing braces and spaces
The kernel follows K&R style. Opening braces go at the end of the line for all non-function statement blocks (if, switch, for, while, do):
Spaces
Use a space after these keywords:if, switch, case, for, do, while.
Do not use a space after sizeof, typeof, alignof, or __attribute__:
* is adjacent to the name, not the type:
=, +, -, <, >, *, /, %, |, &, ^, <=, >=, ==, !=, ?, :). No space after unary operators (&, *, +, -, ~, !).
Naming conventions
Global functions and variables
Descriptive names using
snake_case. count_active_users(), not cntusr(). Global functions named foo are a shooting offense.Local variables
Short and to the point. A loop counter is
i, not loop_counter. Temporary values are tmp. If you fear confusion, your function is too large.Macros and enum constants
UPPERCASE. For example, #define CONSTANT 0x12345 and enum members like KOBJ_ADD, KOBJ_REMOVE.Inclusive terminology
Avoid
master/slave and blacklist/whitelist. Use primary/secondary, controller/device, allowlist/denylist instead.Functions
Functions should be short and do one thing well. They should fit on one or two screenfuls of text (80×24). The maximum length is inversely proportional to the function’s complexity and indentation level. Keep local variables to 5–10 at most. If you need more, split the function. When a function is exported, theEXPORT_SYMBOL macro follows immediately after the closing brace:
Function prototypes
Include parameter names in function prototypes. Do not use theextern keyword with function declarations. The preferred order of elements:
- Storage class (
static __always_inline) - Storage class attributes (
__init,__cold) - Return type (
void *) - Return type attributes (
__must_check) - Function name
- Parameters (always include names)
- Function parameter attributes (
__printf(4, 5)) - Function behavior attributes (
__malloc)
Centralized exit using goto
Thegoto statement is idiomatic in the kernel for centralized cleanup on multiple exit paths:
out_free_buffer: not err1:. Use separate labels for separate cleanup steps to avoid null-pointer bugs.
Comments
Comments should say what code does, not how it works. If you need to explain how, your code is too complex. Do not put comments inside a function body unless noting something particularly clever or ugly. Put the comment at the function’s head, explaining what it does and why. The preferred multi-line comment style:Documentation/doc-guide/).
Typedefs
Do not usetypedef for structures and pointers. When you see:
a is. But:
(a) Totally opaque objects
(a) Totally opaque objects
When the typedef actively hides what the object is and you can only access it via accessor functions. Example:
pte_t and similar architecture-specific types.(b) Clear integer types that avoid int/long confusion
(b) Clear integer types that avoid int/long confusion
u8, u16, u32, u64 and their signed equivalents are fine. But typedef unsigned long myflags_t; has no justification unless the type might change between unsigned int and unsigned long across configurations.(c) Sparse type-checking
(c) Sparse type-checking
When you use
sparse to create a new type for type-checking purposes.(d) Standard C99 equivalents
(d) Standard C99 equivalents
Linux-specific
u8/u16/u32/u64 types are permitted alternatives to uint8_t etc., though not mandatory in new code.(e) Userspace-safe types
(e) Userspace-safe types
Structures shared with userspace use
__u32 and similar __ prefixed types, since userspace cannot use kernel-internal types.Macros and enums
Constants defined by macros and enum members areCAPITALIZED:
#define constants for related values.
Multi-statement macros must be wrapped in do { ... } while (0):
static inline functions:
- Affect control flow (contain hidden
returnorgoto) - Depend on a local variable with a magic name
- Have arguments used as l-values
- Define constants without parenthesizing expressions
- Introduce namespace collisions with local variable names
static inline functions over function-like macros — they solve the “expression with side effects evaluated more than once” problem and are properly type-checked by the compiler.
Printing kernel messages
Use the appropriate logging functions for the context:- Device-associated messages
- Non-device messages
<linux/dev_printk.h>. These tag messages with the device and driver name, and route them through the correct subsystem.dont. Messages do not need a trailing period. Do not break printk strings across lines — it prevents grepping dmesg.
Memory allocation
Prefer thesizeof(*p) form over naming the type explicitly:
void * return values — the C standard guarantees the implicit conversion. Do not use kmalloc + memset when kzalloc suffices.
BUG() and WARN() macros
Do not add new code that usesBUG(), BUG_ON(), or VM_BUG_ON(). These halt the kernel unconditionally. Instead:
- Use
WARN_ON_ONCE()(preferred) — fires only once to avoid log flooding - Use
WARN_ON()— fires on each occurrence - Use
WARN()— fires with a format string
WARN*() is for unexpected, should-never-happen situations only. Do not use it for conditions expected to trigger during normal operation or in response to user-space actions.
Editor configuration
The kernel ships an.editorconfig file and a .clang-format file. For clang-format, the key settings reflect kernel style: AfterFunction: true (brace on next line for functions), IndentWidth: 8, UseTab: Always.
(c-basic-offset . 8) and (indent-tabs-mode . t) to your dir-locals. For Vim, :set tabstop=8 shiftwidth=8 noexpandtab.