$ show sysprog21/lkmpg
The Linux Kernel Module Programming Guide (LKMPG) is a comprehensive, community-maintained guide to writing Linux kernel modules. It consists of a LaTeX book and a large collection of example kernel modules that demonstrate a wide range of kernel APIs and programming patterns, including character devices, the /proc filesystem, sysfs, debugfs, system call interception, synchronization primitives (mutex, spinlock, rwlock, RCU), interrupt handling, timers, workqueues, block devices, network drivers, DMA, device tree, GPIO, and more. The repository also includes a QEMU-based development environment (devtools) that automates building a minimal kernel and initramfs, allowing developers to compile and test modules inside a virtual machine without affecting their host system. The code is well-structured, follows kernel coding conventions, and includes proper error handling and cleanup. Many examples include version guards for compatibility across different kernel versions (5.10 through 6.17+). The devtools scripts are robust, with caching, idempotency, and prebuilt binary support to speed up setup. The CI pipeline builds the book, compiles all examples, runs static analysis (cppcheck, sparse, gcc, smatch), and tests module loading/unloading in both bare-metal and QEMU environments. While primarily educational, the example modules are self-contained and can serve as templates for real kernel module development. The devtools provide a reproducible environment for kernel module testing, which is valuable for developers working on out-of-tree modules. The repository is actively maintained and targets recent Linux 5.x and 6.x kernels.
The LLM identified 8 files worth highlighting for reuse:
Demonstrates a modern RAM-backed block device using the blk-mq multi-queue block layer. Includes version guards for API changes across Linux 5.10-6.17+, proper error handling, and clean teardown. Reusable as a template for block drivers.
Shows the DMA API setup path: setting DMA mask, allocating coherent memory, creating streaming mappings, and tearing down. Uses device-managed resources and synthetic platform device. Reusable for DMA-capable driver development.
Implements a virtual Ethernet device that loops transmitted packets back into the receive path. Demonstrates net_device_ops, skb handling, statistics with u64_stats_sync, and proper interface lifecycle. Reusable as a template for network drivers.
Provides a framework for virtual input devices with sysfs export/unexport, class registration, and input subsystem integration. Includes device lifecycle management and per-type operations. Reusable for creating virtual input drivers.
Demonstrates Read-Copy-Update (RCU) synchronization with a reader thread and writer updates. Shows rcu_read_lock/unlock, rcu_dereference, rcu_assign_pointer, and synchronize_rcu. Reusable for read-mostly data protection.
Implements a character device with ioctl commands using the cdev interface. Includes proper read/write locking, error handling, and user-space data copying. Reusable as a template for ioctl-based drivers.
Demonstrates static keys for runtime branch optimization using self-modifying code. Includes device interface to enable/disable the key. Reusable for performance-sensitive kernel code paths.
Shows the slab allocator API: creating a kmem_cache, allocating and freeing objects, and destroying the cache. Reusable for efficient allocation of same-sized objects.