The Linux Kernel Module Programming Guide

Peter Jay Salzman
Ori Pomerantz

The Linux Kernel Module Programming Guide is a free book; you may reproduce and/or modify it under the terms of the Open Software License, version 1.1. You can obtain a copy of this license at

This book is distributed in the hope it will be useful, but without any warranty, without even the implied warranty of merchantability or fitness for a particular purpose.

The author encourages wide distribution of this book for personal or commercial use, provided the above copyright notice remains intact and the method adheres to the provisions of the Open Software License. In summary, you may copy and distribute this book free of charge or for a profit. No explicit permission is required from the author for reproduction of this book in any medium, physical or electronic.

Derivative works and translations of this document must be placed under the Open Software License, and the original copyright notice must remain intact. If you have contributed new material to this book, you must make the material and source code available for your revisions. Please make revisions and updates available directly to the document maintainer, Peter Jay Salzman . This will allow for the merging of updates and provide consistent revisions to the Linux community.

If you publish or distribute this book commercially, donations, royalties, and/or printed copies are greatly appreciated by the author and the Linux Documentation Project (LDP). Contributing in this way shows your support for free software and the LDP. If you have questions or comments, please contact the address above.

Table of Contents
Authorship And Copyright
Nota Bene
1. Introduction
What Is A Kernel Module?
How Do Modules Get Into The Kernel?
Before We Begin
2. Hello World
Hello, World (part 1): The Simplest Module
Introducing printk()
Compiling Kernel Modules
Hello World (part 2)
Hello World (part 3): The __init and __exit Macros
Hello World (part 4): Licensing and Module Documentation
Passing Command Line Arguments to a Module
Modules Spanning Multiple Files
3. Preliminaries
Modules vs Programs
How modules begin and end
Functions available to modules
User Space vs Kernel Space
Name Space
Code space
Device Drivers
4. Character Device Files
Character Device Drivers
The file_operations Structure
The file structure
Registering A Device
Unregistering A Device
Writing Modules for Multiple Kernel Versions
5. The /proc File System
The /proc File System
6. Using /proc For Input
Using /proc For Input
7. Talking To Device Files
Talking to Device Files (writes and IOCTLs)}
8. System Calls
System Calls
9. Blocking Processes
Blocking Processes
Replacing printk
10. Replacing Printks
Replacing printk
11. Scheduling Tasks
Scheduling Tasks
12. Interrupt Handlers
Interrupt Handlers
Interrupt Handlers
Keyboards on the Intel Architecture
13. Symmetric Multi Processing
Symmetrical Multi-Processing
14. Common Pitfalls
Common Pitfalls
A. Changes: 2.0 To 2.2
Changes between 2.0 and 2.2
Changes between 2.0 and 2.2
B. Where To Go From Here
Where From Here?
List of Examples
2-1. hello-1.c
2-2. Makefile for a basic kernel module
2-3. hello-2.c
2-4. Makefile for both our modules
2-5. hello-3.c
2-6. hello-4.c
2-7. hello-5.c
2-8. start.c
2-9. stop.c
2-10. Makefile for a multi-filed module
4-1. chardev.c
4-2. some title
5-1. procfs.c
6-1. procfs.c
7-1. chardev.c
7-2. chardev.h
7-3. ioctl.c
8-1. syscall.c
9-1. sleep.c
10-1. print_string.c
11-1. sched.c
12-1. intrpt.c