From a1faca212ccff1a8ba0b761ab199fbe82c04d43c Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 11 Mar 2017 21:41:53 +0000 Subject: [PATCH] No work queue --- 4.9.11/LKMPG-4.9.11.html | 450 +++++++++++++++++++-------------------- 4.9.11/LKMPG-4.9.11.org | 2 - 2 files changed, 223 insertions(+), 229 deletions(-) diff --git a/4.9.11/LKMPG-4.9.11.html b/4.9.11/LKMPG-4.9.11.html index ba19bd7..abd84fe 100644 --- a/4.9.11/LKMPG-4.9.11.html +++ b/4.9.11/LKMPG-4.9.11.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + The Linux Kernel Module Programming Guide @@ -264,90 +264,90 @@ for the JavaScript code in this tag.

Table of Contents

-
-

Introduction

-
+
+

Introduction

+

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 http://opensource.org/licenses/osl.php.

@@ -369,18 +369,18 @@ If you publish or distribute this book commercially, donations, royalties, and/o

-
-

Authorship

-
+
+

Authorship

+

The Linux Kernel Module Programming Guide was originally written for the 2.2 kernels by Ori Pomerantz. Eventually, Ori no longer had time to maintain the document. After all, the Linux kernel is a fast moving target. Peter Jay Salzman took over maintenance and updated it for the 2.4 kernels. Eventually, Peter no longer had time to follow developments with the 2.6 kernel, so Michael Burian became a co-maintainer to update the document for the 2.6 kernels. Bob Mottram updated the examples for 3.8 and later kernels and modified or updated some chapters.

-
-

Versioning and Notes

-
+
+

Versioning and Notes

+

The Linux kernel is a moving target. There has always been a question whether the LKMPG should remove deprecated information or keep it around for historical sake. Michael Burian and I decided to create a new branch of the LKMPG for each new stable kernel version. So version LKMPG 4.9.x will address Linux kernel 4.9.x and LKMPG 2.6.x will address Linux kernel 2.6. No attempt will be made to archive historical information; a person wishing this information should read the appropriately versioned LKMPG.

@@ -391,18 +391,18 @@ The source code and discussions should apply to most architectures, but I can't
-
-

Acknowledgements

-
+
+

Acknowledgements

+

The following people have contributed corrections or good suggestions: Ignacio Martin, David Porter, Daniele Paolo Scarpazza, Dimo Velev, Francois Audeon, Horst Schirmeier, Bob Mottram and Roman Lakeev.

-
-

What Is A Kernel Module?

-
+
+

What Is A Kernel Module?

+

So, you want to write a kernel module. You know C, you've written a few normal programs to run as processes, and now you want to get to where the real action is, to where a single wild pointer can wipe out your file system and a core dump means a reboot.

@@ -413,9 +413,9 @@ What exactly is a kernel module? Modules are pieces of code that can be loaded a
-
-

Kernel module package

-
+
+

Kernel module package

+

Linux distros provide the commands modprobe, insmod and depmod within a package.

@@ -440,9 +440,9 @@ On Parabola:
-
-

What Modules are in my Kernel?

-
+
+

What Modules are in my Kernel?

+

To discover what modules are already loaded within your current kernel use the command lsmod.

@@ -472,33 +472,33 @@ This can be a long list, and you might prefer to search for something particular
-
-

Do I need to download and compile the kernel?

-
+
+

Do I need to download and compile the kernel?

+

For the purposes of following this guide you don't necessarily need to do that. However, it would be wise to run the examples within a test distro running on a virtual machine in order to avoid any possibility of messing up your system.

-
-

Before We Begin

-
+
+

Before We Begin

+

Before we delve into code, there are a few issues we need to cover. Everyone's system is different and everyone has their own groove. Getting your first "hello world" program to compile and load correctly can sometimes be a trick. Rest assured, after you get over the initial hurdle of doing it for the first time, it will be smooth sailing thereafter.

    -
  • Modversioning
    -
    +
  • Modversioning
    +

    A module compiled for one kernel won't load if you boot a different kernel unless you enable CONFIG_MODVERSIONS in the kernel. We won't go into module versioning until later in this guide. Until we cover modversions, the examples in the guide may not work if you're running a kernel with modversioning turned on. However, most stock Linux distro kernels come with it turned on. If you're having trouble loading the modules because of versioning errors, compile a kernel with modversioning turned off.

  • -
  • Using X
    -
    +
  • Using X
    +

    It is highly recommended that you type in, compile and load all the examples this guide discusses. It's also highly recommended you do this from a console. You should not be working on this stuff in X.

    @@ -512,9 +512,9 @@ Modules can't print to the screen like printf() can, but they can log informatio
-
-

Headers

-
+
+

Headers

+

Before you can build anything you'll need to install the header files for your kernel. On Parabola GNU/Linux:

@@ -544,9 +544,9 @@ This will tell you what kernel header files are available. Then for example:
-
-

Examples

-
+
+

Examples

+

All the examples from this document are available within the examples subdirectory. To test that they compile:

@@ -562,13 +562,13 @@ If there are any compile errors then you might have a more recent kernel version

-
-

Hello World

-
+
+

Hello World

+
-
-

The Simplest Module

-
+
+

The Simplest Module

+

Most people learning programming start out with some sort of "hello world" example. I don't know what happens to people who break with this tradition, but I think it's safer not to find out. We'll start with a series of hello world programs that demonstrate the different aspects of the basics of writing a kernel module.

@@ -712,8 +712,8 @@ Lastly, every kernel module needs to include linux/module.h. We needed to includ
    -
  • Introducing printk()
    -
    +
  • Introducing printk()
    +

    Despite what you might think, printk() was not meant to communicate information to the user, even though we used it for exactly this purpose in hello-1! It happens to be a logging mechanism for the kernel, and is used to log information or give warnings. Therefore, each printk() statement comes with a priority, which is the <1> and KERN_ALERT you see. There are 8 priorities and the kernel has macros for them, so you don't have to use cryptic numbers, and you can view them (and their meanings) in linux/kernel.h. If you don't specify a priority level, the default priority, DEFAULT_MESSAGE_LOGLEVEL, will be used.

    @@ -728,8 +728,8 @@ If the priority is less than int console_loglevel, the message is printed on you
  • -
  • About Compiling
    -
    +
  • About Compiling
    +

    Kernel modules need to be compiled a bit differently from regular userspace apps. Former kernel versions required us to care much about these settings, which are usually stored in Makefiles. Although hierarchically organized, many redundant settings accumulated in sublevel Makefiles and made them large and rather difficult to maintain. Fortunately, there is a new way of doing these things, called kbuild, and the build process for external loadable modules is now fully integrated into the standard kernel build mechanism. To learn more on how to compile modules which are not part of the official kernel (such as all the examples you'll find in this guide), see file linux/Documentation/kbuild/modules.txt.

    @@ -748,9 +748,9 @@ Here's another exercise for the reader. See that comment above the return statem
-
-

Hello and Goodbye

-
+
+

Hello and Goodbye

+

In early kernel versions you had to use the init_module and cleanup_module functions, as in the first hello world example, but these days you can name those anything you want by using the module_init and module_exit macros. These macros are defined in linux/init.h. The only requirement is that your init and cleanup functions must be defined before calling the those macros, otherwise you'll get compilation errors. Here's an example of this technique:

@@ -802,9 +802,9 @@ Now have a look at linux/drivers/char/Makefile for a real world example. As you
-
-

The __init and __exit Macros

-
+
+

The __init and __exit Macros

+

This demonstrates a feature of kernel 2.2 and later. Notice the change in the definitions of the init and cleanup functions. The __init macro causes the init function to be discarded and its memory freed once the init function finishes for built-in drivers, but not loadable modules. If you think about when the init function is invoked, this makes perfect sense.

@@ -849,9 +849,9 @@ module_exit(hello_3_exit);
-
-

Licensing and Module Documentation

-
+
+

Licensing and Module Documentation

+

Honestly, who loads or even cares about proprietary modules? If you do then you might have seen something like this:

@@ -903,9 +903,9 @@ module_exit(cleanup_hello_4);
-
-

Passing Command Line Arguments to a Module

-
+
+

Passing Command Line Arguments to a Module

+

Modules can take command line arguments, but not with the argc/argv you might be used to.

@@ -1055,9 +1055,9 @@ hello-5.o: invalid argument syntax for mylong: 'h'
-
-

Modules Spanning Multiple Files

-
+
+

Modules Spanning Multiple Files

+

Sometimes it makes sense to divide a kernel module between several source files.

@@ -1128,9 +1128,9 @@ This is the complete makefile for all the examples we've seen so far. The first
-
-

Building modules for a precompiled kernel

-
+
+

Building modules for a precompiled kernel

+

Obviously, we strongly suggest you to recompile your kernel, so that you can enable a number of useful debugging features, such as forced module unloading (MODULE_FORCE_UNLOAD): when this option is enabled, you can force the kernel to unload a module even when it believes it is unsafe, via a sudo rmmod -f module command. This option can save you a lot of time and a number of reboots during the development of a module. If you don't want to recompile your kernel then you should consider running the examples within a test distro on a virtual machine. If you mess anything up then you can easily reboot or restore the VM.

@@ -1222,13 +1222,13 @@ If you do not desire to actually compile the kernel, you can interrupt the build
-
-

Preliminaries

-
+
+

Preliminaries

+
-
-

How modules begin and end

-
+
+

How modules begin and end

+

A program usually begins with a main() function, executes a bunch of instructions and terminates upon completion of those instructions. Kernel modules work a bit differently. A module always begin with either the init_module or the function you specify with module_init call. This is the entry function for modules; it tells the kernel what functionality the module provides and sets up the kernel to run the module's functions when they're needed. Once it does this, entry function returns and the module does nothing until the kernel wants to do something with the code that the module provides.

@@ -1243,9 +1243,9 @@ Every module must have an entry function and an exit function. Since there's mor
-
-

Functions available to modules

-
+
+

Functions available to modules

+

Programmers use functions they don't define all the time. A prime example of this is printf(). You use these library functions which are provided by the standard C library, libc. The definitions for these functions don't actually enter your program until the linking stage, which insures that the code (for printf() for example) is available, and fixes the call instruction to point to that code.

@@ -1283,9 +1283,9 @@ You can even write modules to replace the kernel's system calls, which we'll do
-
-

User Space vs Kernel Space

-
+
+

User Space vs Kernel Space

+

A kernel is all about access to resources, whether the resource in question happens to be a video card, a hard drive or even memory. Programs often compete for the same resource. As I just saved this document, updatedb started updating the locate database. My vim session and updatedb are both using the hard drive concurrently. The kernel needs to keep things orderly, and not give users access to resources whenever they feel like it. To this end, a CPU can run in different modes. Each mode gives a different level of freedom to do what you want on the system. The Intel 80386 architecture had 4 of these modes, which were called rings. Unix uses only two rings; the highest ring (ring 0, also known as `supervisor mode' where everything is allowed to happen) and the lowest ring, which is called `user mode'.

@@ -1296,9 +1296,9 @@ Recall the discussion about library functions vs system calls. Typically, you us
-
-

Name Space

-
+
+

Name Space

+

When you write a small C program, you use variables which are convenient and make sense to the reader. If, on the other hand, you're writing routines which will be part of a bigger problem, any global variables you have are part of a community of other peoples' global variables; some of the variable names can clash. When a program has lots of global variables which aren't meaningful enough to be distinguished, you get namespace pollution. In large projects, effort must be made to remember reserved names, and to find ways to develop a scheme for naming unique variable names and symbols.

@@ -1313,9 +1313,9 @@ The file /proc/kallsyms holds all the symbols that the kernel knows about
-
-

Code space

-
+
+

Code space

+

Memory management is a very complicated subject and the majority of O'Reilly's "Understanding The Linux Kernel" exclusively covers memory management! We're not setting out to be experts on memory managements, but we do need to know a couple of facts to even begin worrying about writing real modules.

@@ -1334,17 +1334,17 @@ By the way, I would like to point out that the above discussion is true for any
-
-

Device Drivers

-
+
+

Device Drivers

+

One class of module is the device driver, which provides functionality for hardware like a serial port. On unix, each piece of hardware is represented by a file located in /dev named a device file which provides the means to communicate with the hardware. The device driver provides the communication on behalf of a user program. So the es1370.o sound card device driver might connect the /dev/sound device file to the Ensoniq IS1370 sound card. A userspace program like mp3blaster can use /dev/sound without ever knowing what kind of sound card is installed.

    -
  • Major and Minor Numbers
    -
    +
  • Major and Minor Numbers
    +

    Let's look at some device files. Here are device files which represent the first three partitions on the primary master IDE hard drive:

    @@ -1409,13 +1409,13 @@ By now you can look at these two device files and know instantly that they are b
-
-

Character Device drivers

-
+
+

Character Device drivers

+
-
-

The file_operations Structure

-
+
+

The file_operations Structure

+

The file_operations structure is defined in /usr/include/linux/fs.h, and holds pointers to functions defined by the driver that perform various operations on the device. Each field of the structure corresponds to the address of some function defined by the driver to handle a requested operation.

@@ -1500,9 +1500,9 @@ An instance of struct file_operations containing pointers to functions that are
-
-

The file structure

-
+
+

The file structure

+

Each device is represented in the kernel by a file structure, which is defined in linux/fs.h. Be aware that a file is a kernel level structure and never appears in a user space program. It's not the same thing as a FILE, which is defined by glibc and would never appear in a kernel space function. Also, its name is a bit misleading; it represents an abstract open `file', not a file on a disk, which is represented by a structure named inode.

@@ -1517,9 +1517,9 @@ Go ahead and look at the definition of file. Most of the entries you see, like s
-
-

Registering A Device

-
+
+

Registering A Device

+

As discussed earlier, char devices are accessed through device files, usually located in /dev4. The major number tells you which driver handles which device file. The minor number is used only by the driver itself to differentiate which device it's operating on, just in case the driver handles more than one device.

@@ -1547,9 +1547,9 @@ If you pass a major number of 0 to register_chrdev, the return value will be the
-
-

Unregistering A Device

-
+
+

Unregistering A Device

+

We can't allow the kernel module to be rmmod'ed whenever root feels like it. If the device file is opened by a process and then we remove the kernel module, using the file would cause a call to the memory location where the appropriate function (read/write) used to be. If we're lucky, no other code was loaded there, and we'll get an ugly error message. If we're unlucky, another kernel module was loaded into the same location, which means a jump into the middle of another function within the kernel. The results of this would be impossible to predict, but they can't be very positive.

@@ -1569,9 +1569,9 @@ It's important to keep the counter accurate; if you ever do lose track of the co
-
-

chardev.c

-
+
+

chardev.c

+

The next code sample creates a char driver named chardev. You can cat its device file.

@@ -1759,9 +1759,9 @@ The next code sample creates a char driver named chardev. You can cat its device
-
-

Writing Modules for Multiple Kernel Versions

-
+
+

Writing Modules for Multiple Kernel Versions

+

The system calls, which are the major interface the kernel shows to the processes, generally stay the same across versions. A new system call may be added, but usually the old ones will behave exactly like they used to. This is necessary for backward compatibility – a new kernel version is not supposed to break regular processes. In most cases, the device files will also remain the same. On the other hand, the internal interfaces within the kernel can and do change between versions.

@@ -1785,9 +1785,9 @@ You might already have noticed that recent kernels look different. In case you h
-
-

The /proc File System

-
+
+

The /proc File System

+

In Linux, there is an additional mechanism for the kernel and kernel modules to send information to processes — the /proc file system. Originally designed to allow easy access to information about processes (hence the name), it is now used by every bit of the kernel which has something interesting to report, such as /proc/modules which provides the list of modules and /proc/meminfo which stats memory usage statistics.

@@ -1877,9 +1877,9 @@ HelloWorld!
-
-

Read and Write a /proc File

-
+
+

Read and Write a /proc File

+

We have seen a very simple example for a /proc file where we only read the file /proc/helloworld. It's also possible to write in a /proc file. It works the same way as read, a function is called when the /proc file is written. But there is a little difference with read, data comes from user, so you have to import data from user space to kernel space (with copy_from_user or get_user)

@@ -1996,9 +1996,9 @@ The only memory segment accessible to a process is its own, so when writing regu
-
-

Manage /proc file with standard filesystem

-
+
+

Manage /proc file with standard filesystem

+

We have seen how to read and write a /proc file with the /proc interface. But it's also possible to manage /proc file with inodes. The main concern is to use advanced functions, like permissions.

@@ -2108,9 +2108,9 @@ Still hungry for procfs examples? Well, first of all keep in mind, there are rum
-
-

Manage /proc file with seq_file

-
+
+

Manage /proc file with seq_file

+

As we have seen, writing a /proc file may be quite "complex". So to help people writting /proc file, there is an API named seq_file that helps @@ -2300,9 +2300,9 @@ You can also read the code of fs/seq_file.c in the linux kernel.

-
-

sysfs: Interacting with your module

-
+
+

sysfs: Interacting with your module

+

sysfs allows you to interact with the running kernel by reading or setting variables inside of modules. This can be useful for debugging purposes, or just as an interface for userland applications or scripts.

@@ -2432,9 +2432,9 @@ Finally, remove the test module:
-
-

Talking To Device Files

-
+
+

Talking To Device Files

+

Device files are supposed to represent physical devices. Most physical devices are used for output as well as input, so there has to be some mechanism for device drivers in the kernel to get the output to send to the device from processes. This is done by opening the device file for output and writing to it, just like writing to a file. In the following example, this is implemented by device_write.

@@ -2915,9 +2915,9 @@ If you want to use ioctls in your own kernel modules, it is best to receive an o
-
-

System Calls

-
+
+

System Calls

+

So far, the only thing we've done was to use well defined kernel mechanisms to register /proc files and device handlers. This is fine if you want to do something the kernel programmers thought you'd want, such as write a device driver. But what if you want to do something unusual, to change the behavior of the system in some way? Then, you're mostly on your own.

@@ -3123,9 +3123,9 @@ MODULE_LICENSE("GPL");
-
-

Blocking Processes

-
+
+

Blocking Processes

+

What do you do when somebody asks you for something you can't do right away? If you're a human being and you're bothered by a human being, the only thing you can say is: "Not right now, I'm busy. Go away!". But if you're a kernel module and you're bothered by a process, you have another possibility. You can put the process to sleep until you can service it. After all, processes are being put to sleep by the kernel and woken up all the time (that's the way multiple processes appear to run on the same time on a single CPU).

@@ -3515,13 +3515,13 @@ DECLARE_WAIT_QUEUE_HEAD(WaitQ);
-
-

Replacing Printks

-
+
+

Replacing Printks

+
-
-

Replacing printk

-
+
+

Replacing printk

+

In Section 1.2.1.2, I said that X and kernel module programming don't mix. That's true for developing kernel modules, but in actual use, you want to be able to send messages to whichever tty12 the command to load the module came from.

@@ -3644,9 +3644,9 @@ module_exit(print_string_exit);
-
-

Flashing keyboard LEDs

-
+
+

Flashing keyboard LEDs

+

In certain conditions, you may desire a simpler and more direct way to communicate to the external world. Flashing keyboard LEDs can be such a solution: It is an immediate way to attract attention or to display a status condition. Keyboard LEDs are present on every hardware, they are always visible, they do not need any setup, and their use is rather simple and non-intrusive, compared to writing to a tty or a file.

@@ -3765,9 +3765,9 @@ While you have seen lots of stuff that can be used to aid debugging here, there
-
-

Scheduling Tasks

-
+
+

Scheduling Tasks

+

Very often, we have "housekeeping" tasks which have to be done at a certain time, or every so often. If the task is to be done by a process, we do it by putting it in the crontab file. If the task is to be done by a kernel module, we have two possibilities. The first is to put a process in the crontab file which will wake up the module by a system call when necessary, for example by opening a file. This is terribly inefficient, however – we run a new process off of crontab, read a new executable to memory, and all this just to wake up a kernel module which is in memory anyway.

@@ -3950,13 +3950,13 @@ MODULE_LICENSE("GPL");
-
-

Interrupt Handlers

-
+
+

Interrupt Handlers

+
-
-

Interrupt Handlers

-
+
+

Interrupt Handlers

+

Except for the last chapter, everything we did in the kernel so far we've done as a response to a process asking for it, either by dealing with a special file, sending an ioctl(), or issuing a system call. But the job of the kernel isn't just to respond to process requests. Another job, which is every bit as important, is to speak to the hardware connected to the machine.

@@ -3976,16 +3976,12 @@ When the CPU receives an interrupt, it stops whatever it's doing (unless it's pr

The way to implement this is to call request_irq() to get your interrupt handler called when the relevant IRQ is received. 14This function receives the IRQ number, the name of the function, flags, a name for /proc/interrupts and a parameter to pass to the interrupt handler. Usually there is a certain number of IRQs available. How many IRQs there are is hardware-dependent. The flags can include SA_SHIRQ to indicate you're willing to share the IRQ with other interrupt handlers (usually because a number of hardware devices sit on the same IRQ) and SA_INTERRUPT to indicate this is a fast interrupt. This function will only succeed if there isn't already a handler on this IRQ, or if you're both willing to share.

- -

-Then, from within the interrupt handler, we communicate with the hardware and then use queue_work() mark_bh(BH_IMMEDIATE) to schedule the bottom half. -

-
-

Detecting button presses

-
+
+

Detecting button presses

+

Many popular single board computers, such as Raspberry Pis or Beagleboards, have a bunch of GPIO pins. Attaching buttons to those and then having a button press do something is a classic case in which you might need to use interrupts so that instead of having the CPU waste time and battery power polling for a change in input state it's better for the input to trigger the CPU to then run a particular handling function.

@@ -4152,35 +4148,35 @@ MODULE_DESCRIPTION("Handle some GPIO interrupts"
-
-

Common Pitfalls

-
+
+

Common Pitfalls

+

Before I send you on your way to go out into the world and write kernel modules, there are a few things I need to warn you about. If I fail to warn you and something bad happens, please report the problem to me for a full refund of the amount I was paid for your copy of the book.

-
-

Using standard libraries

-
+
+

Using standard libraries

+

You can't do that. In a kernel module you can only use kernel functions, which are the functions you can see in /proc/kallsyms.

-
-

Disabling interrupts

-
+
+

Disabling interrupts

+

You might need to do this for a short time and that is OK, but if you don't enable them afterwards, your system will be stuck and you'll have to power it off.

-
-

Sticking your head inside a large carnivore

-
+
+

Sticking your head inside a large carnivore

+

I probably don't have to warn you about this, but I figured I will anyway, just in case.

@@ -4188,9 +4184,9 @@ I probably don't have to warn you about this, but I figured I will anyway, just
-
-

Where To Go From Here?

-
+
+

Where To Go From Here?

+

I could easily have squeezed a few more chapters into this book. I could have added a chapter about creating new file systems, or about adding new protocol stacks (as if there's a need for that – you'd have to dig underground to find a protocol stack not supported by Linux). I could have added explanations of the kernel mechanisms we haven't touched upon, such as bootstrapping or the disk interface.

diff --git a/4.9.11/LKMPG-4.9.11.org b/4.9.11/LKMPG-4.9.11.org index 6e096b2..2e8d6f2 100644 --- a/4.9.11/LKMPG-4.9.11.org +++ b/4.9.11/LKMPG-4.9.11.org @@ -2984,8 +2984,6 @@ When the CPU receives an interrupt, it stops whatever it's doing (unless it's pr The way to implement this is to call *request_irq()* to get your interrupt handler called when the relevant IRQ is received. [fn:17]This function receives the IRQ number, the name of the function, flags, a name for /proc/interrupts and a parameter to pass to the interrupt handler. Usually there is a certain number of IRQs available. How many IRQs there are is hardware-dependent. The flags can include SA_SHIRQ to indicate you're willing to share the IRQ with other interrupt handlers (usually because a number of hardware devices sit on the same IRQ) and SA_INTERRUPT to indicate this is a fast interrupt. This function will only succeed if there isn't already a handler on this IRQ, or if you're both willing to share. -Then, from within the interrupt handler, we communicate with the hardware and then use queue_work() mark_bh(BH_IMMEDIATE) to schedule the bottom half. - ** Detecting button presses Many popular single board computers, such as Raspberry Pis or Beagleboards, have a bunch of GPIO pins. Attaching buttons to those and then having a button press do something is a classic case in which you might need to use interrupts so that instead of having the CPU waste time and battery power polling for a change in input state it's better for the input to trigger the CPU to then run a particular handling function.