Linux Kernel C Coding Practices
Application skill for kernel coding-style (archived awesome-guidelines capsules). For GNU userspace C, load gnu-c-coding-practices. Generic portable C: c-coding-practices. Apache httpd: httpd-c-coding-practices.
Core Principle
Kernel C quality is checkpatch-clean tab layout and maintainable control flow, 8-tab indent, 80 columns, descriptive global names, short functions, descriptive goto cleanup, kernel-doc on exports.
When to Use / NOT
- Linux kernel in-tree or out-of-tree modules targeting upstream.
- Reviewing patches before
linux-kernel@vger.kernel.org or maintainer lists.
NOT when:
- GNU userspace packages,
gnu-c-coding-practices.
- Generic userspace C without kernel tree rules.
- Rust kernel code, Rust kernel docs (style guide is C-centric).
Workflow
- Indent/braces, tabs, 80 cols, switch alignment (
linux-kernel-style-indent-braces.md).
- Naming/types, pointers, typedefs, terminology (
linux-kernel-style-naming-types.md).
- Functions/goto, size, prototypes, cleanup (
linux-kernel-style-functions-goto.md).
- Macros/verify, kernel-doc, alloc, checkpatch (
linux-kernel-style-macros-verify.md).
- Verify,
./scripts/checkpatch.pl --strict on changed files/patch; build + make targets for subsystem.
Red Flags
- Spaces used for code indentation
- Lines >80 without strong readability reason
- Function
{ on same line as signature
- Double-indented
case labels
char* p or spaces inside ( ) in sizeof/calls
- Global/function named
foo or cntusr
- New gratuitous
typedef struct ... foo_t
- New master/slave or blacklist/whitelist without ABI/spec excuse
- Kitchen-sink function with many unrelated locals
- Missing parameter names in prototypes
EXPORT_SYMBOL not immediately after function }
err: label freeing nullable nested pointers
- Control-flow macro with hidden
return
kmalloc(sizeof(struct x), ...) decoupled from pointer var
- Cast on
kmalloc return
- Broken/grep-hostile printk string wraps
- Exported API without kernel-doc update
- Skipping checkpatch on submitted patch
Verification
./scripts/checkpatch.pl --strict --file <changed.c> (or on .patch)
- Optional
scripts/Lindent on touched C (separate cleanup commit if mass reformat)
- Subsystem
make / make CHECK=1 / kselftest as applicable
- Capsule probes on new gotos, exports, and alloc calls
References
awesome-guidelines/references/linux-kernel-style-learning-note.md
awesome-guidelines/references/linux-kernel-style-indent-braces.md
awesome-guidelines/references/linux-kernel-style-naming-types.md
awesome-guidelines/references/linux-kernel-style-functions-goto.md
awesome-guidelines/references/linux-kernel-style-macros-verify.md
Related skills
c-coding-practices, portable C safety baseline
gnu-c-coding-practices, GNU userspace layout (not kernel tabs)
1---2name: linux-kernel-coding-practices3description: Use when authoring or reviewing Linux kernel C, 8-tab indent, 80-col K&R braces, pointer-on-name spacing, descriptive globals, goto cleanup, kernel-doc, kmalloc_obj, and scripts/checkpatch.pl.4---56# Linux Kernel C Coding Practices78Application skill for kernel coding-style (archived `awesome-guidelines` capsules). For GNU userspace C, load `gnu-c-coding-practices`. Generic portable C: `c-coding-practices`. Apache httpd: `httpd-c-coding-practices`.910## Core Principle1112Kernel C quality is **checkpatch-clean tab layout and maintainable control flow**, 8-tab indent, 80 columns, descriptive global names, short functions, descriptive `goto` cleanup, kernel-doc on exports.1314## When to Use / NOT1516- Linux kernel in-tree or out-of-tree modules targeting upstream.17- Reviewing patches before `linux-kernel@vger.kernel.org` or maintainer lists.1819**NOT when:**2021- GNU userspace packages, `gnu-c-coding-practices`.22- Generic userspace C without kernel tree rules.23- Rust kernel code, Rust kernel docs (style guide is C-centric).2425## Workflow26271. **Indent/braces**, tabs, 80 cols, switch alignment (`linux-kernel-style-indent-braces.md`).282. **Naming/types**, pointers, typedefs, terminology (`linux-kernel-style-naming-types.md`).293. **Functions/goto**, size, prototypes, cleanup (`linux-kernel-style-functions-goto.md`).304. **Macros/verify**, kernel-doc, alloc, checkpatch (`linux-kernel-style-macros-verify.md`).315. **Verify**, `./scripts/checkpatch.pl --strict` on changed files/patch; build + `make` targets for subsystem.3233## Red Flags3435- Spaces used for code indentation36- Lines >80 without strong readability reason37- Function `{` on same line as signature38- Double-indented `case` labels39- `char* p` or spaces inside `( )` in sizeof/calls40- Global/function named `foo` or `cntusr`41- New gratuitous `typedef struct ... foo_t`42- New master/slave or blacklist/whitelist without ABI/spec excuse43- Kitchen-sink function with many unrelated locals44- Missing parameter names in prototypes45- `EXPORT_SYMBOL` not immediately after function `}`46- `err:` label freeing nullable nested pointers47- Control-flow macro with hidden `return`48- `kmalloc(sizeof(struct x), ...)` decoupled from pointer var49- Cast on `kmalloc` return50- Broken/grep-hostile printk string wraps51- Exported API without kernel-doc update52- Skipping checkpatch on submitted patch5354## Verification5556- `./scripts/checkpatch.pl --strict --file <changed.c>` (or on `.patch`)57- Optional `scripts/Lindent` on touched C (separate cleanup commit if mass reformat)58- Subsystem `make` / `make CHECK=1` / `kselftest` as applicable59- Capsule probes on new gotos, exports, and alloc calls606162## References6364- `awesome-guidelines/references/linux-kernel-style-learning-note.md`65- `awesome-guidelines/references/linux-kernel-style-indent-braces.md`66- `awesome-guidelines/references/linux-kernel-style-naming-types.md`67- `awesome-guidelines/references/linux-kernel-style-functions-goto.md`68- `awesome-guidelines/references/linux-kernel-style-macros-verify.md`6970## Related skills7172- `c-coding-practices`, portable C safety baseline73- `gnu-c-coding-practices`, GNU userspace layout (not kernel tabs)