Part 2: Kernel Fundamentals
This part covers the essential concepts every kernel developer must understand: architecture, module lifecycle, coding conventions, and error handling patterns.
What You’ll Learn
- Kernel vs user space architecture
- Module initialization and cleanup
- Module parameters and symbol exporting
- Kernel coding style and conventions
- Kernel data types and endianness
- Error handling patterns
Prerequisites
Complete Part 1: Getting Started or have a working development environment.
Chapter Overview
| Chapter | Topic | Description |
|---|---|---|
| 2.1 | Kernel Architecture | User space vs kernel space, privilege levels |
| 2.2 | Module Lifecycle | init/exit, module_param, EXPORT_SYMBOL |
| 2.3 | Coding Style | Kernel conventions, checkpatch.pl |
| 2.4 | Data Types | Fixed-width types, endianness handling |
| 2.5 | Error Handling | ERR_PTR, goto cleanup, return conventions |
| 2.6 | Kernel API | Common macros and helper functions |
| 2.7 | Data Structures | Linked lists, hash tables, red-black trees |
Key Concepts
The Kernel Environment
The kernel environment differs significantly from user space:
| Aspect | User Space | Kernel Space |
|---|---|---|
| Memory | Virtual, protected | Direct physical access |
| Stack | Large (MB) | Small (8-16 KB) |
| Libraries | Full C library | Kernel-only APIs |
| Errors | Exceptions possible | Must handle explicitly |
| Floating point | Available | Generally unavailable |
Why This Matters
Understanding these fundamentals helps you:
- Write stable, crash-free drivers
- Avoid common kernel programming pitfalls
- Follow kernel conventions for upstream acceptance
- Debug issues more effectively
Examples
This part includes two example modules:
examples/part2/module-params/- Module parameters demonstrationexamples/part2/export-symbol/- Symbol exporting between modulesexamples/part2/data-structures/- Linked list and hash table operations
Further Reading
- Kernel Coding Style - Official style guide
- Kernel API Documentation - Core kernel APIs
- Driver API - Driver development APIs
Keep the kernel coding style guide bookmarked: Documentation/process/coding-style.rst