Kernel module basic example: (from Linux Kernel Driver). Code from Douglas Su

hello_world.c

#include <linux/module.h>

static int __init m_init(void)
{
	printk(KERN_ALERT "Hello, world!\n");
	return 0;
}

static void __exit m_exit(void)
{
	printk(KERN_ALERT "Bye, world!\n");
}

module_init(m_init);
module_exit(m_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Douglas Su");
MODULE_DESCRIPTION("Hello World program");

Makefile

ifneq ($(KERNELRELEASE),)

# In kbuild context
module-objs := hello_world.o
obj-m := hello_world.o

CFLAGS_hello_world.o := -DDEBUG

else
# In normal make context
KDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

.PHONY: modules
modules:
	$(MAKE) -C $(KDIR) M=$(PWD) modules

.PHONY: clean
clean:
	$(MAKE) -C $(KDIR) M=$(PWD) clean

endif

Notes on the Makefile

  • To build a module from multiple source (/objects):

    obj-m := module.o
    module-objs := file1.o file2.o
    
  • the module code can't build on its own. It must be built in the kernel tree (where a configured makefile can be found) e.g.

    /lib/modules/6.6.20-1-lts/build
    

    or

    /usr/src/linux-lts
    
  • the module Makefile idiom

    # If KERNELRELEASE is defined, we've been invoked from the
    # kernel build system and can use its language.
    ifneq ($(KERNELRELEASE),)
        obj-m := hello.o
    # Otherwise we were called directly from the command
    # line; invoke the kernel build system.
    else
        KERNELDIR ?= /lib/modules/$(shell uname -r)/build
        PWD := $(shell pwd)
    default:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
    endif
    

Quick Reference (LDD)

insmod
modprobe
rmmod
    User-space utilities that load modules into the running kernels and remove
    them.

#include <linux/init.h>
module_init(init_function);
module_exit(cleanup_function);
    Macros that designate a module’s initialization and cleanup functions.
__init
__initdata
__exit
__exitdata
    Markers for functions (__init and __exit) and data (__initdata and __exitdata)
    that are only used at module initialization or cleanup time. Items marked for ini-
    tialization may be discarded once initialization completes; the exit items may be
    discarded if module unloading has not been configured into the kernel. These
    markers work by causing the relevant objects to be placed in a special ELF sec-
    tion in the executable file.

#include <linux/sched.h>
    One of the most important header files. This file contains definitions of much of
    the kernel API used by the driver, including functions for sleeping and numer-
    ous variable declarations.
struct task_struct *current;
    The current process.
current->pid
current->comm
    The process ID and command name for the current process.

obj-m
    A makefile symbol used by the kernel build system to determine which modules
    should be built in the current directory.

/sys/module
/proc/modules
    /sys/module is a sysfs directory hierarchy containing information on currently-
    loaded modules. /proc/modules is the older, single-file version of that informa-
    tion. Entries contain the module name, the amount of memory each module
    occupies, and the usage count. Extra strings are appended to each line to specify
    flags that are currently active for the module.

vermagic.o
    An object file from the kernel source directory that describes the environment a
    module was built for.

#include <linux/module.h>
    Required header. It must be included by a module source.

#include <linux/version.h>
    A header file containing information on the version of the kernel being built.

LINUX_VERSION_CODE
    Integer macro, useful to #ifdef version dependencies.

EXPORT_SYMBOL (symbol);
EXPORT_SYMBOL_GPL (symbol);
    Macro used to export a symbol to the kernel. The second form exports without
    using versioning information, and the third limits the export to GPL-licensed
    modules.

MODULE_AUTHOR(author);
MODULE_DESCRIPTION(description);
MODULE_VERSION(version_string);
MODULE_DEVICE_TABLE(table_info);
MODULE_ALIAS(alternate_name);
    Place documentation on the module in the object file.

module_init(init_function);
module_exit(exit_function);
    Macros that declare a module’s initialization and cleanup functions.
#include <linux/moduleparam.h>
module_param(variable, type, perm);
    Macro that creates a module parameter that can be adjusted by the user when
    the module is loaded (or at boot time for built-in code). The type can be one of
    bool, charp, int, invbool, long, short, ushort, uint, ulong, or intarray.

#include <linux/kernel.h>
int printk(const char * fmt, ...);
    The analogue of printf for kernel code.