The Linux Kernel Module Programming Guide (updated for 5.0+ kernels)
Go to file
fennecJ 870b26fa2d Update several example code for newer kernel
Known issues with current example code:
If you using newer kernel(e.g linux 5.11.x) to compile the example code,
you may meet following error:
1. syscall.c:83:50: error: ‘ksys_close’ undeclared;
2. cryptosk.c:17:24: error: field ‘sg’ has incomplete type
3. cryptosk.c:143:9: error: implicit declaration of function
‘get_random_bytes’
4. error: macro "DECLARE_TASKLET" passed 3 arguments, but takes just 2

Solutions/workaround:
1. In syscall.c, replace #include <linux/syscalls.h> with
#include <linux/fdtable.h> and replace  ksys_close with close_fd
if the kernel version >= 5.11. [1][2]
2. Add #include <linux/scatterlist.h> into cryptosk.c
3. Add #include <linux/random.h> into cryptosk.c
4. In bottomhalf.c and example_tasklet.c, replace DECLARE_TASKLET
with DECLARE_TASKLET_OLD and dispose third argument(0L). [3]

[1] - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1572bfdf21d4d50e51941498ffe0b56c2289f783
[2] - https://www.mail-archive.com/meta-arago@arago-project.org//msg11939.html
[3] - https://patchwork.kernel.org/project/kernel-hardening/patch/20200716030847.1564131-3-keescook@chromium.org/
2021-08-23 21:30:43 +08:00
.ci Check coding style in CI 2021-08-17 13:07:06 +08:00
.github/workflows Check coding style in CI 2021-08-17 13:07:06 +08:00
examples Update several example code for newer kernel 2021-08-23 21:30:43 +08:00
lib Introduce \src to generate Linux kernel source hyperlinks 2021-08-09 23:01:34 +08:00
.gitignore Update several example code for newer kernel 2021-08-23 21:30:43 +08:00
GPL-2 Initial import 2021-07-22 06:34:19 +08:00
html.cfg Add PDF download link in html only 2021-08-17 17:35:36 +08:00
LICENSE Initial import 2021-07-22 06:34:19 +08:00
lkmpg.tex Update several example code for newer kernel 2021-08-23 21:30:43 +08:00
Makefile Update several example code for newer kernel 2021-08-23 21:30:43 +08:00
README.md Neither latexmk nor dvipng is required 2021-08-18 20:43:51 +08:00

The Linux Kernel Module Programming Guide

This project keeps the Linux Kernel Module Programming Guide reasonably up to date, with working examples for recent 5.x kernel versions.
The guide has been around since 2001 and most copies of it on the web only describe old 2.6.x kernels.

The book can be freely accessed via https://sysprog21.github.io/lkmpg/ or latest PDF file.
The original guide may be found at Linux Documentation Project.

Getting Started

Compile on Local Machine

To prepare for build this book on your local machine, we're going to install TeXLive (MacTeX). On various Unix/Linux operating systems, this can be done simply by:

# Debian / Ubuntu
$ sudo apt install make texlive-full

# Arch / Manjaro
$ sudo pacman -S make texlive-most texlive-bin

# macOS
$ brew install --cask mactex
$ sudo tlmgr update --self

Now we could build document with following commands:

# download project
$ git clone https://github.com/sysprog21/lkmpg.git && cd lkmpg

# run commands
$ make all              # Generate PDFdocument
$ make html             # Convert TeX to HTML
$ make clean            # Delete generated files

Compile with Docker

The compilation could be completed flawlessly using Docker. **Using Docker is recommended, as it guarantees the same dependencies with our GitHub Actions wokrflow.

After install docker engine on your machine, pulling the docker image twtug/lkmpg and compile with it.

Execute followings

# download project
$ git clone https://github.com/sysprog21/lkmpg.git && cd lkmpg

# pull docker image and run it as container
$ docker pull twtug/lkmpg
$ docker run --rm -it -v $(pwd):/workdir twtug/lkmpg

# run commands
$ make all              # Generate PDF document
$ make html             # Convert TeX to HTML
$ make clean            # Delete generated files

License

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.
Use of this work is governed by a copyleft license that can be found in the LICENSE file.

The complementary sample code is licensed under GNU GPL version 2, as same as Linux kernel.