From db4482ccd66545981f3ce7ddb935150dd5895b26 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 31 Mar 2017 10:32:28 +0100 Subject: [PATCH] Simpler reads and writes --- 4.9.11/LKMPG-4.9.11.html | 545 ++++++++++++++++++++------------------- 4.9.11/LKMPG-4.9.11.org | 1 + 2 files changed, 275 insertions(+), 271 deletions(-) diff --git a/4.9.11/LKMPG-4.9.11.html b/4.9.11/LKMPG-4.9.11.html index 876369e..605f9b2 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,113 +264,113 @@ 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 3.0.

@@ -392,18 +392,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, added the sysfs chapter and modified or updated other 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.

@@ -414,18 +414,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.

@@ -436,9 +436,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.

@@ -463,9 +463,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.

@@ -495,33 +495,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.

    @@ -535,9 +535,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:

@@ -567,9 +567,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:

@@ -585,13 +585,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.

@@ -735,8 +735,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.

    @@ -751,8 +751,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.

    @@ -771,9 +771,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:

@@ -825,9 +825,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.

@@ -872,9 +872,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:

@@ -926,9 +926,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.

@@ -1078,9 +1078,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.

@@ -1151,9 +1151,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.

@@ -1245,13 +1245,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.

@@ -1266,9 +1266,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.

@@ -1306,9 +1306,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'.

@@ -1319,9 +1319,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.

@@ -1336,9 +1336,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.

@@ -1357,17 +1357,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:

    @@ -1432,13 +1432,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.

@@ -1523,9 +1523,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.

@@ -1540,9 +1540,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.

@@ -1570,9 +1570,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.

@@ -1592,9 +1592,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.

@@ -1782,9 +1782,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.

@@ -1808,9 +1808,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.

@@ -1900,9 +1900,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)

@@ -2019,9 +2019,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.

@@ -2131,9 +2131,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 @@ -2323,9 +2323,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 from userspace by reading or setting variables inside of modules. This can be useful for debugging purposes, or just as an interface for applications or scripts. You can find sysfs directories and files under the sys directory on your system.

@@ -2460,9 +2460,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.

@@ -2943,9 +2943,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.

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

Blocking Processes and threads

-
+
+

Blocking Processes and threads

+
-
-

Sleep

-
+
+

Sleep

+

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).

@@ -3547,9 +3547,9 @@ DECLARE_WAIT_QUEUE_HEAD(WaitQ);
-
-

Completions

-
+
+

Completions

+

Sometimes one thing should happen before another within a module having multiple threads. Rather than using /proc/sleep commands the kernel has another way to do this which allows timeouts or interrupts to also happen.

@@ -3653,16 +3653,16 @@ There are other variations upon the wait_for_completion function, which i
-
-

Avoiding Deadlock

-
+
+

Avoiding Deadlock

+

If processes running on different CPUs or in different threads try to access the same memory then it's possible that strange things can happen or your system can lock up. To avoid this various types of mutual exclusion kernel functions are possible. These indicate if a section of code is "locked" or "unlocked" so that simultaneous attempts to run it can't happen.

-
-

Spinlocks

-
+
+

Spinlocks

+

As the name suggests, spinlocks lock up the CPU that the code is running on, taking 100% of its resources. Because of this you should only use the spinlock mechanism around code which is likely to take no more than a few milliseconds to run and so won't noticably slow anything down from the user's point of view.

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

Read and write locks

-
+
+

Read and write locks

+

Read and write locks are specialised kinds of spinlocks so that you can exclusively read from something or write to something. Like the earlier spinlocks example the one below shows an "irq safe" situation in which if other functions were triggered from irqs which might also read and write to whatever you are concerned with then they wouldn't disrupt the logic. As before it's a good idea to keep anything done within the lock as short as possible so that it doesn't hang up the system and cause users to start revolting against the tyranny of your module.

@@ -3803,18 +3803,21 @@ MODULE_DESCRIPTION("Read/Write locks example"); MODULE_LICENSE("GPL");
-
-
-
-
-
-

Replacing Printks

-
+

+Of course if you know for sure that there are no functions triggered by irqs which could possibly interfere with your logic then you can use the simpler read_lock(&myrwlock) and read_unlock(&myrwlock) or the corresponding write functions. +

-
-

Replacing printk

-
+
+
+
+
+

Replacing Printks

+
+
+
+

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.

@@ -3937,9 +3940,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.

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

Scheduling Tasks

-
+
+

Scheduling Tasks

+

There are two main ways of running tasks: tasklets and work queues. Tasklets are a quick and easy way of scheduling a single function to be run, for example when triggered from an interrupt, whereas work queues are more complicated but also better suited to running multiple things in a sequence.

-
-

Tasklets

-
+
+

Tasklets

+

Here's an example tasklet module. The tasklet_fn function runs for a few seconds and in the mean time execution of the example_tasklet_init function continues to the exit point.

@@ -4125,9 +4128,9 @@ Example tasklet ends
-
-

Work queues

-
+
+

Work queues

+

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.

@@ -4311,13 +4314,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.

@@ -4340,9 +4343,9 @@ The way to implement this is to call request_irq() to get your interrupt
-
-

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.

@@ -4509,17 +4512,17 @@ MODULE_DESCRIPTION("Handle some GPIO interrupts"
-
-

Crypto

-
+
+

Crypto

+

At the dawn of the internet everybody trusted everybody completely…but that didn't work out so well. When this guide was originally written it was a more innocent era in which almost nobody actually gave a damn about crypto - least of all kernel developers. That's certainly no longer the case now. To handle crypto stuff the kernel has its own API enabling common methods of encryption, decryption and your favourite hash functions.

-
-

Hash functions

-
+
+

Hash functions

+

Calculating and checking the hashes of things is a common operation. Here is a demonstration of how to calculate a sha256 hash within a kernel module.

@@ -4617,9 +4620,9 @@ Finally, remove the test module:
-
-

Symmetric key encryption

-
+
+

Symmetric key encryption

+

Here is an example of symmetrically encrypting a string using the AES algorithm and a password.

@@ -4810,9 +4813,9 @@ MODULE_LICENSE("GPL");
-
-

Standardising the interfaces: The Device Model

-
+
+

Standardising the interfaces: The Device Model

+

Up to this point we've seen all kinds of modules doing all kinds of things, but there was no consistency in their interfaces with the rest of the kernel. To impose some consistency such that there is at minimum a standardised way to start, suspend and resume a device a device model was added. An example is show below, and you can use this as a template to add your own suspend, resume or other interface functions.

@@ -4918,35 +4921,35 @@ module_exit(devicemodel_exit);
-
-

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.

@@ -4954,9 +4957,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 4eb836a..733464c 100644 --- a/4.9.11/LKMPG-4.9.11.org +++ b/4.9.11/LKMPG-4.9.11.org @@ -2802,6 +2802,7 @@ MODULE_DESCRIPTION("Read/Write locks example"); MODULE_LICENSE("GPL"); #+end_src +Of course if you know for sure that there are no functions triggered by irqs which could possibly interfere with your logic then you can use the simpler /read_lock(&myrwlock)/ and /read_unlock(&myrwlock)/ or the corresponding write functions. * Replacing Printks ** 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 tty[fn:15] the command to load the module came from.