diff --git a/index.html b/index.html index b4d05f0..24bfcb9 100644 --- a/index.html +++ b/index.html @@ -18,7 +18,7 @@

The Linux Kernel Module Programming Guide

Peter Jay Salzman, Michael Burian, Ori Pomerantz, Bob Mottram, Jim Huang

-
January 3, 2024
+
April 9, 2024
@@ -5042,26 +5042,34 @@ need to solve another truckload of problems. It is not enough to know if a certain IRQs has happened, it’s also important to know what CPU(s) it was for. People still interested in more details, might want to refer to "APIC" now. -

This function receives the IRQ number, the name of the function, -flags, a name for /proc/interrupts and a parameter to be passed 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 +

This function receives the IRQ number, the name of the function, flags, a name +for /proc/interrupts and a parameter to be passed to the interrupt handler. +Usually there is a certain number of IRQs available. How many IRQs there are is +hardware-dependent. +

The flags can be used for specify behaviors of the IRQ. For example, use + IRQF_SHARED to indicate you are 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 is not -already a handler on this IRQ, or if you are both willing to share. -

+(usually because a number of hardware devices sit on the same IRQ); use the + IRQF_ONESHOT + to indicate that the IRQ is not reenabled after the handler finished. It should be +noted that in some materials, you may encouter another set of IRQ flags named with +the SA + prefix. For example, the SA_SHIRQ + and the SA_INTERRUPT +. Those are the the IRQ flags in the older kernels. They have been removed completely. Today +only the IRQF + flags are in use. This function will only succeed if there is not already a handler on +this IRQ, or if you are both willing to share. +

15.2 Detecting button presses

-

Many popular single board computers, such as Raspberry Pi or Beagleboards, have a +

Many popular single board computers, such as Raspberry Pi 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 is better for the input to trigger the CPU to then run a particular handling function. -

Here is an example where buttons are connected to GPIO numbers 17 and 18 and +

Here is an example where buttons are connected to GPIO numbers 17 and 18 and an LED is connected to GPIO 4. You can change those numbers to whatever is appropriate for your board.

@@ -5211,19 +5219,19 @@ appropriate for your board. 143 144MODULE_LICENSE("GPL"); 145MODULE_DESCRIPTION("Handle some GPIO interrupts"); -

+

-

15.3 Bottom Half

-

Suppose you want to do a bunch of stuff inside of an interrupt routine. A common -way to do that without rendering the interrupt unavailable for a significant duration -is to combine it with a tasklet. This pushes the bulk of the work off into the -scheduler. -

The example below modifies the previous example to also run an additional task -when an interrupt is triggered. -

+

15.3 Bottom Half

+

Suppose you want to do a bunch of stuff inside of an interrupt routine. A common +way to do that without rendering the interrupt unavailable for a significant duration +is to combine it with a tasklet. This pushes the bulk of the work off into the +scheduler. +

The example below modifies the previous example to also run an additional task +when an interrupt is triggered. +

1/* 
 2 * bottomhalf.c - Top and bottom half interrupt handling 
@@ -5393,19 +5401,19 @@ when an interrupt is triggered.
 166 
 167MODULE_LICENSE("GPL"); 
 168MODULE_DESCRIPTION("Interrupt with top and bottom half");
-

+

16 Crypto

-

At the dawn of the internet, everybody trusted everybody completely…but that did +

At the dawn of the internet, everybody trusted everybody completely…but that did not 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 is 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. -

+

16.1 Hash functions

-

Calculating and checking the hashes of things is a common operation. +

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. To provide the sha256 algorithm support, make sure CONFIG_CRYPTO_SHA256 @@ -5480,23 +5488,23 @@ kernel module. To provide the sha256 algorithm support, make sure 66 67MODULE_DESCRIPTION("sha256 hash test"); 68MODULE_LICENSE("GPL"); -

Install the module: +

Install the module:

1sudo insmod cryptosha256.ko 
 2sudo dmesg
-

And you should see that the hash was calculated for the test string. -

Finally, remove the test module: +

And you should see that the hash was calculated for the test string. +

Finally, remove the test module:

-

-
1sudo rmmod cryptosha256
-

+

+
1sudo rmmod cryptosha256
+

16.2 Symmetric key encryption

-

Here is an example of symmetrically encrypting a string using the AES algorithm +

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

@@ -5699,10 +5707,10 @@ and a password. 197 198MODULE_DESCRIPTION("Symmetric key encryption example"); 199MODULE_LICENSE("GPL"); -

+

17 Virtual Input Device Driver

-

The input device driver is a module that provides a way to communicate +

The input device driver is a module that provides a way to communicate with the interaction device via the event. For example, the keyboard can send the press or release event to tell the kernel what we want to do. The input device driver will allocate a new input structure with @@ -5710,7 +5718,7 @@ do. The input device driver will allocate a new input structure with and sets up input bitfields, device id, version, etc. After that, registers it by calling input_register_device() . -

Here is an example, vinput, It is an API to allow easy +

Here is an example, vinput, It is an API to allow easy development of virtual input drivers. The drivers needs to export a vinput_device() that contains the virtual device name and @@ -5726,13 +5734,13 @@ development of virtual input drivers. The drivers needs to export a

  • the readback function: read()
  • -

    Then using vinput_register_device() +

    Then using vinput_register_device() and vinput_unregister_device() will add a new device to the list of support virtual input devices.

    1int init(struct vinput *);
    -

    This function is passed a struct vinput +

    This function is passed a struct vinput already initialized with an allocated struct input_dev . The init() function is responsible for initializing the capabilities of the input device and register @@ -5740,20 +5748,20 @@ it.

    1int send(struct vinput *, char *, int);
    -

    This function will receive a user string to interpret and inject the event using the +

    This function will receive a user string to interpret and inject the event using the input_report_XXXX or input_event call. The string is already copied from user.

    1int read(struct vinput *, char *, int);
    -

    This function is used for debugging and should fill the buffer parameter with the +

    This function is used for debugging and should fill the buffer parameter with the last event sent in the virtual input device format. The buffer will then be copied to user. -

    vinput devices are created and destroyed using sysfs. And, event injection is done +

    vinput devices are created and destroyed using sysfs. And, event injection is done through a /dev node. The device name will be used by the userland to export a new virtual input device. -

    The class_attribute +

    The class_attribute structure is similar to other attribute types we talked about in section 8:

    @@ -5764,24 +5772,24 @@ virtual input device. 5    ssize_t (*store)(struct class *class, struct class_attribute *attr, 6                    const char *buf, size_t count); 7}; -

    In vinput.c, the macro CLASS_ATTR_WO(export/unexport) + + + +

    In vinput.c, the macro CLASS_ATTR_WO(export/unexport) defined in include/linux/device.h (in this case, device.h is included in include/linux/input.h) will generate the class_attribute structures which are named class_attr_export/unexport. Then, put them into vinput_class_attrs array and the macro ATTRIBUTE_GROUPS(vinput_class) will generate the struct attribute_group vinput_class_group - - - that should be assigned in vinput_class . Finally, call class_register(&vinput_class) to create attributes in sysfs. -

    To create a vinputX sysfs entry and /dev node. +

    To create a vinputX sysfs entry and /dev node.

    1echo "vkbd" | sudo tee /sys/class/vinput/export
    -

    To unexport the device, just echo its id in unexport: +

    To unexport the device, just echo its id in unexport:

    1echo "0" | sudo tee /sys/class/vinput/unexport
    @@ -6243,7 +6251,7 @@ will generate the class_attribute 401 402MODULE_LICENSE("GPL"); 403MODULE_DESCRIPTION("Emulate input events"); -

    Here the virtual keyboard is one of example to use vinput. It supports all +

    Here the virtual keyboard is one of example to use vinput. It supports all KEY_MAX keycodes. The injection format is the KEY_CODE such as defined in include/linux/input.h. A positive value means @@ -6251,14 +6259,17 @@ will generate the class_attribute while a negative value is a KEY_RELEASE . The keyboard supports repetition when the key stays pressed for too long. The following demonstrates how simulation work. -

    Simulate a key press on "g" ( KEY_G +

    Simulate a key press on "g" ( KEY_G = 34):

    1echo "+34" | sudo tee /dev/vinput0
    -

    Simulate a key release on "g" ( KEY_G +

    Simulate a key release on "g" ( KEY_G = 34):

    + + +

    1echo "-34" | sudo tee /dev/vinput0
    @@ -6374,13 +6385,10 @@ following demonstrates how simulation work. 108 109MODULE_LICENSE("GPL"); 110MODULE_DESCRIPTION("Emulate keyboard input events through /dev/vinput"); - - - -

    +

    18 Standardizing the interfaces: The Device Model

    -

    Up to this point we have seen all kinds of modules doing all kinds of things, but there +

    Up to this point we have 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 standardized way to start, suspend and resume a device model was added. An example is shown below, and you can @@ -6486,13 +6494,13 @@ functions. 96 97MODULE_LICENSE("GPL"); 98MODULE_DESCRIPTION("Linux Device Model example"); -

    +

    19 Optimizations

    -

    +

    19.1 Likely and Unlikely conditions

    -

    Sometimes you might want your code to run as quickly as possible, +

    Sometimes you might want your code to run as quickly as possible, especially if it is handling an interrupt or doing something which might cause noticeable latency. If your code contains boolean conditions and if you know that the conditions are almost always likely to evaluate as either @@ -6504,6 +6512,9 @@ you know that the conditions are almost always likely to evaluate as either macros. For example, when allocating memory you are almost always expecting this to succeed.

    + + +

    1bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx); 
     2if (unlikely(!bvl)) { 
    @@ -6511,19 +6522,16 @@ to succeed.
     4    bio = NULL; 
     5    goto out; 
     6}
    -

    When the unlikely +

    When the unlikely macro is used, the compiler alters its machine instruction output, so that it continues along the false branch and only jumps if the condition is true. That avoids flushing the processor pipeline. The opposite happens if you use the likely macro. - - - -

    +

    19.2 Static keys

    -

    Static keys allow us to enable or disable kernel code paths based on the runtime state +

    Static keys allow us to enable or disable kernel code paths based on the runtime state of key. Its APIs have been available since 2010 (most architectures are already supported), use self-modifying code to eliminate the overhead of cache and branch prediction. The most typical use case of static keys is for performance-sensitive kernel @@ -6537,7 +6545,7 @@ Before we can use static keys in the kernel, we need to make sure that gcc suppo

    1CONFIG_JUMP_LABEL=y 
     2CONFIG_HAVE_ARCH_JUMP_LABEL=y 
     3CONFIG_HAVE_ARCH_JUMP_LABEL_RELATIVE=y
    -

    To declare a static key, we need to define a global variable using the +

    To declare a static key, we need to define a global variable using the DEFINE_STATIC_KEY_FALSE or DEFINE_STATIC_KEY_TRUE macro defined in include/linux/jump_label.h. This macro initializes the key with @@ -6547,7 +6555,7 @@ code:

    1DEFINE_STATIC_KEY_FALSE(fkey);
    -

    Once the static key has been declared, we need to add branching code to the +

    Once the static key has been declared, we need to add branching code to the module that uses the static key. For example, the code includes a fastpath, where a no-op instruction will be generated at compile time as the key is initialized to false and the branch is unlikely to be taken. @@ -6557,12 +6565,15 @@ and the branch is unlikely to be taken. 2if (static_branch_unlikely(&fkey)) 3    pr_alert("do unlikely thing\n"); 4pr_info("fastpath 2\n"); -

    If the key is enabled at runtime by calling + + + +

    If the key is enabled at runtime by calling static_branch_enable(&fkey) , the fastpath will be patched with an unconditional jump instruction to the slowpath code pr_alert , so the branch will always be taken until the key is disabled again. -

    The following kernel module derived from chardev.c, demonstrates how the +

    The following kernel module derived from chardev.c, demonstrates how the static key works.

    @@ -6754,60 +6765,57 @@ static key works. 186module_exit(chardev_exit); 187 188MODULE_LICENSE("GPL"); - - - -

    To check the state of the static key, we can use the /dev/key_state +

    To check the state of the static key, we can use the /dev/key_state interface.

    1cat /dev/key_state
    -

    This will display the current state of the key, which is disabled by default. -

    To change the state of the static key, we can perform a write operation on the +

    This will display the current state of the key, which is disabled by default. +

    To change the state of the static key, we can perform a write operation on the file:

    1echo enable > /dev/key_state
    -

    This will enable the static key, causing the code path to switch from the fastpath +

    This will enable the static key, causing the code path to switch from the fastpath to the slowpath. -

    In some cases, the key is enabled or disabled at initialization and never changed, +

    In some cases, the key is enabled or disabled at initialization and never changed, we can declare a static key as read-only, which means that it can only be toggled in the module init function. To declare a read-only static key, we can use the DEFINE_STATIC_KEY_FALSE_RO or DEFINE_STATIC_KEY_TRUE_RO macro instead. Attempts to change the key at runtime will result in a page fault. For more information, see Static keys -

    +

    20 Common Pitfalls

    -

    +

    -

    20.1 Using standard libraries

    -

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

    -

    -

    20.2 Disabling interrupts

    -

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

    +

    20.1 Using standard libraries

    +

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

    +

    +

    20.2 Disabling interrupts

    +

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

    21 Where To Go From Here?

    -

    For those deeply interested in kernel programming, kernelnewbies.org and the +

    For those deeply interested in kernel programming, kernelnewbies.org and the Documentation subdirectory within the kernel source code are highly recommended. Although the latter may not always be straightforward, it serves as a valuable initial step for further exploration. Echoing Linus Torvalds’ perspective, the most effective method to understand the kernel is through personal examination of the source code. -

    Contributions to this guide are welcome, especially if there are any significant +

    Contributions to this guide are welcome, especially if there are any significant inaccuracies identified. To contribute or report an issue, please initiate an issue at https://github.com/sysprog21/lkmpg. Pull requests are greatly appreciated. -

    Happy hacking! +

    Happy hacking!

    1The goal of threaded interrupts is to push more of the work to separate threads, so that the minimum needed for acknowledging an interrupt is reduced, and therefore the time spent handling diff --git a/lkmpg-for-ht.html b/lkmpg-for-ht.html index b4d05f0..24bfcb9 100644 --- a/lkmpg-for-ht.html +++ b/lkmpg-for-ht.html @@ -18,7 +18,7 @@

    The Linux Kernel Module Programming Guide

    Peter Jay Salzman, Michael Burian, Ori Pomerantz, Bob Mottram, Jim Huang

    -
    January 3, 2024
    +
    April 9, 2024
    @@ -5042,26 +5042,34 @@ need to solve another truckload of problems. It is not enough to know if a certain IRQs has happened, it’s also important to know what CPU(s) it was for. People still interested in more details, might want to refer to "APIC" now. -

    This function receives the IRQ number, the name of the function, -flags, a name for /proc/interrupts and a parameter to be passed 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 +

    This function receives the IRQ number, the name of the function, flags, a name +for /proc/interrupts and a parameter to be passed to the interrupt handler. +Usually there is a certain number of IRQs available. How many IRQs there are is +hardware-dependent. +

    The flags can be used for specify behaviors of the IRQ. For example, use + IRQF_SHARED to indicate you are 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 is not -already a handler on this IRQ, or if you are both willing to share. -

    +(usually because a number of hardware devices sit on the same IRQ); use the + IRQF_ONESHOT + to indicate that the IRQ is not reenabled after the handler finished. It should be +noted that in some materials, you may encouter another set of IRQ flags named with +the SA + prefix. For example, the SA_SHIRQ + and the SA_INTERRUPT +. Those are the the IRQ flags in the older kernels. They have been removed completely. Today +only the IRQF + flags are in use. This function will only succeed if there is not already a handler on +this IRQ, or if you are both willing to share. +

    15.2 Detecting button presses

    -

    Many popular single board computers, such as Raspberry Pi or Beagleboards, have a +

    Many popular single board computers, such as Raspberry Pi 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 is better for the input to trigger the CPU to then run a particular handling function. -

    Here is an example where buttons are connected to GPIO numbers 17 and 18 and +

    Here is an example where buttons are connected to GPIO numbers 17 and 18 and an LED is connected to GPIO 4. You can change those numbers to whatever is appropriate for your board.

    @@ -5211,19 +5219,19 @@ appropriate for your board. 143 144MODULE_LICENSE("GPL"); 145MODULE_DESCRIPTION("Handle some GPIO interrupts"); -

    +

    -

    15.3 Bottom Half

    -

    Suppose you want to do a bunch of stuff inside of an interrupt routine. A common -way to do that without rendering the interrupt unavailable for a significant duration -is to combine it with a tasklet. This pushes the bulk of the work off into the -scheduler. -

    The example below modifies the previous example to also run an additional task -when an interrupt is triggered. -

    +

    15.3 Bottom Half

    +

    Suppose you want to do a bunch of stuff inside of an interrupt routine. A common +way to do that without rendering the interrupt unavailable for a significant duration +is to combine it with a tasklet. This pushes the bulk of the work off into the +scheduler. +

    The example below modifies the previous example to also run an additional task +when an interrupt is triggered. +

    1/* 
     2 * bottomhalf.c - Top and bottom half interrupt handling 
    @@ -5393,19 +5401,19 @@ when an interrupt is triggered.
     166 
     167MODULE_LICENSE("GPL"); 
     168MODULE_DESCRIPTION("Interrupt with top and bottom half");
    -

    +

    16 Crypto

    -

    At the dawn of the internet, everybody trusted everybody completely…but that did +

    At the dawn of the internet, everybody trusted everybody completely…but that did not 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 is 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. -

    +

    16.1 Hash functions

    -

    Calculating and checking the hashes of things is a common operation. +

    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. To provide the sha256 algorithm support, make sure CONFIG_CRYPTO_SHA256 @@ -5480,23 +5488,23 @@ kernel module. To provide the sha256 algorithm support, make sure 66 67MODULE_DESCRIPTION("sha256 hash test"); 68MODULE_LICENSE("GPL"); -

    Install the module: +

    Install the module:

    1sudo insmod cryptosha256.ko 
     2sudo dmesg
    -

    And you should see that the hash was calculated for the test string. -

    Finally, remove the test module: +

    And you should see that the hash was calculated for the test string. +

    Finally, remove the test module:

    -

    -
    1sudo rmmod cryptosha256
    -

    +

    +
    1sudo rmmod cryptosha256
    +

    16.2 Symmetric key encryption

    -

    Here is an example of symmetrically encrypting a string using the AES algorithm +

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

    @@ -5699,10 +5707,10 @@ and a password. 197 198MODULE_DESCRIPTION("Symmetric key encryption example"); 199MODULE_LICENSE("GPL"); -

    +

    17 Virtual Input Device Driver

    -

    The input device driver is a module that provides a way to communicate +

    The input device driver is a module that provides a way to communicate with the interaction device via the event. For example, the keyboard can send the press or release event to tell the kernel what we want to do. The input device driver will allocate a new input structure with @@ -5710,7 +5718,7 @@ do. The input device driver will allocate a new input structure with and sets up input bitfields, device id, version, etc. After that, registers it by calling input_register_device() . -

    Here is an example, vinput, It is an API to allow easy +

    Here is an example, vinput, It is an API to allow easy development of virtual input drivers. The drivers needs to export a vinput_device() that contains the virtual device name and @@ -5726,13 +5734,13 @@ development of virtual input drivers. The drivers needs to export a

  • the readback function: read()
  • -

    Then using vinput_register_device() +

    Then using vinput_register_device() and vinput_unregister_device() will add a new device to the list of support virtual input devices.

    1int init(struct vinput *);
    -

    This function is passed a struct vinput +

    This function is passed a struct vinput already initialized with an allocated struct input_dev . The init() function is responsible for initializing the capabilities of the input device and register @@ -5740,20 +5748,20 @@ it.

    1int send(struct vinput *, char *, int);
    -

    This function will receive a user string to interpret and inject the event using the +

    This function will receive a user string to interpret and inject the event using the input_report_XXXX or input_event call. The string is already copied from user.

    1int read(struct vinput *, char *, int);
    -

    This function is used for debugging and should fill the buffer parameter with the +

    This function is used for debugging and should fill the buffer parameter with the last event sent in the virtual input device format. The buffer will then be copied to user. -

    vinput devices are created and destroyed using sysfs. And, event injection is done +

    vinput devices are created and destroyed using sysfs. And, event injection is done through a /dev node. The device name will be used by the userland to export a new virtual input device. -

    The class_attribute +

    The class_attribute structure is similar to other attribute types we talked about in section 8:

    @@ -5764,24 +5772,24 @@ virtual input device. 5    ssize_t (*store)(struct class *class, struct class_attribute *attr, 6                    const char *buf, size_t count); 7}; -

    In vinput.c, the macro CLASS_ATTR_WO(export/unexport) + + + +

    In vinput.c, the macro CLASS_ATTR_WO(export/unexport) defined in include/linux/device.h (in this case, device.h is included in include/linux/input.h) will generate the class_attribute structures which are named class_attr_export/unexport. Then, put them into vinput_class_attrs array and the macro ATTRIBUTE_GROUPS(vinput_class) will generate the struct attribute_group vinput_class_group - - - that should be assigned in vinput_class . Finally, call class_register(&vinput_class) to create attributes in sysfs. -

    To create a vinputX sysfs entry and /dev node. +

    To create a vinputX sysfs entry and /dev node.

    1echo "vkbd" | sudo tee /sys/class/vinput/export
    -

    To unexport the device, just echo its id in unexport: +

    To unexport the device, just echo its id in unexport:

    1echo "0" | sudo tee /sys/class/vinput/unexport
    @@ -6243,7 +6251,7 @@ will generate the class_attribute 401 402MODULE_LICENSE("GPL"); 403MODULE_DESCRIPTION("Emulate input events"); -

    Here the virtual keyboard is one of example to use vinput. It supports all +

    Here the virtual keyboard is one of example to use vinput. It supports all KEY_MAX keycodes. The injection format is the KEY_CODE such as defined in include/linux/input.h. A positive value means @@ -6251,14 +6259,17 @@ will generate the class_attribute while a negative value is a KEY_RELEASE . The keyboard supports repetition when the key stays pressed for too long. The following demonstrates how simulation work. -

    Simulate a key press on "g" ( KEY_G +

    Simulate a key press on "g" ( KEY_G = 34):

    1echo "+34" | sudo tee /dev/vinput0
    -

    Simulate a key release on "g" ( KEY_G +

    Simulate a key release on "g" ( KEY_G = 34):

    + + +

    1echo "-34" | sudo tee /dev/vinput0
    @@ -6374,13 +6385,10 @@ following demonstrates how simulation work. 108 109MODULE_LICENSE("GPL"); 110MODULE_DESCRIPTION("Emulate keyboard input events through /dev/vinput"); - - - -

    +

    18 Standardizing the interfaces: The Device Model

    -

    Up to this point we have seen all kinds of modules doing all kinds of things, but there +

    Up to this point we have 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 standardized way to start, suspend and resume a device model was added. An example is shown below, and you can @@ -6486,13 +6494,13 @@ functions. 96 97MODULE_LICENSE("GPL"); 98MODULE_DESCRIPTION("Linux Device Model example"); -

    +

    19 Optimizations

    -

    +

    19.1 Likely and Unlikely conditions

    -

    Sometimes you might want your code to run as quickly as possible, +

    Sometimes you might want your code to run as quickly as possible, especially if it is handling an interrupt or doing something which might cause noticeable latency. If your code contains boolean conditions and if you know that the conditions are almost always likely to evaluate as either @@ -6504,6 +6512,9 @@ you know that the conditions are almost always likely to evaluate as either macros. For example, when allocating memory you are almost always expecting this to succeed.

    + + +

    1bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx); 
     2if (unlikely(!bvl)) { 
    @@ -6511,19 +6522,16 @@ to succeed.
     4    bio = NULL; 
     5    goto out; 
     6}
    -

    When the unlikely +

    When the unlikely macro is used, the compiler alters its machine instruction output, so that it continues along the false branch and only jumps if the condition is true. That avoids flushing the processor pipeline. The opposite happens if you use the likely macro. - - - -

    +

    19.2 Static keys

    -

    Static keys allow us to enable or disable kernel code paths based on the runtime state +

    Static keys allow us to enable or disable kernel code paths based on the runtime state of key. Its APIs have been available since 2010 (most architectures are already supported), use self-modifying code to eliminate the overhead of cache and branch prediction. The most typical use case of static keys is for performance-sensitive kernel @@ -6537,7 +6545,7 @@ Before we can use static keys in the kernel, we need to make sure that gcc suppo

    1CONFIG_JUMP_LABEL=y 
     2CONFIG_HAVE_ARCH_JUMP_LABEL=y 
     3CONFIG_HAVE_ARCH_JUMP_LABEL_RELATIVE=y
    -

    To declare a static key, we need to define a global variable using the +

    To declare a static key, we need to define a global variable using the DEFINE_STATIC_KEY_FALSE or DEFINE_STATIC_KEY_TRUE macro defined in include/linux/jump_label.h. This macro initializes the key with @@ -6547,7 +6555,7 @@ code:

    1DEFINE_STATIC_KEY_FALSE(fkey);
    -

    Once the static key has been declared, we need to add branching code to the +

    Once the static key has been declared, we need to add branching code to the module that uses the static key. For example, the code includes a fastpath, where a no-op instruction will be generated at compile time as the key is initialized to false and the branch is unlikely to be taken. @@ -6557,12 +6565,15 @@ and the branch is unlikely to be taken. 2if (static_branch_unlikely(&fkey)) 3    pr_alert("do unlikely thing\n"); 4pr_info("fastpath 2\n"); -

    If the key is enabled at runtime by calling + + + +

    If the key is enabled at runtime by calling static_branch_enable(&fkey) , the fastpath will be patched with an unconditional jump instruction to the slowpath code pr_alert , so the branch will always be taken until the key is disabled again. -

    The following kernel module derived from chardev.c, demonstrates how the +

    The following kernel module derived from chardev.c, demonstrates how the static key works.

    @@ -6754,60 +6765,57 @@ static key works. 186module_exit(chardev_exit); 187 188MODULE_LICENSE("GPL"); - - - -

    To check the state of the static key, we can use the /dev/key_state +

    To check the state of the static key, we can use the /dev/key_state interface.

    1cat /dev/key_state
    -

    This will display the current state of the key, which is disabled by default. -

    To change the state of the static key, we can perform a write operation on the +

    This will display the current state of the key, which is disabled by default. +

    To change the state of the static key, we can perform a write operation on the file:

    1echo enable > /dev/key_state
    -

    This will enable the static key, causing the code path to switch from the fastpath +

    This will enable the static key, causing the code path to switch from the fastpath to the slowpath. -

    In some cases, the key is enabled or disabled at initialization and never changed, +

    In some cases, the key is enabled or disabled at initialization and never changed, we can declare a static key as read-only, which means that it can only be toggled in the module init function. To declare a read-only static key, we can use the DEFINE_STATIC_KEY_FALSE_RO or DEFINE_STATIC_KEY_TRUE_RO macro instead. Attempts to change the key at runtime will result in a page fault. For more information, see Static keys -

    +

    20 Common Pitfalls

    -

    +

    -

    20.1 Using standard libraries

    -

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

    -

    -

    20.2 Disabling interrupts

    -

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

    +

    20.1 Using standard libraries

    +

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

    +

    +

    20.2 Disabling interrupts

    +

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

    21 Where To Go From Here?

    -

    For those deeply interested in kernel programming, kernelnewbies.org and the +

    For those deeply interested in kernel programming, kernelnewbies.org and the Documentation subdirectory within the kernel source code are highly recommended. Although the latter may not always be straightforward, it serves as a valuable initial step for further exploration. Echoing Linus Torvalds’ perspective, the most effective method to understand the kernel is through personal examination of the source code. -

    Contributions to this guide are welcome, especially if there are any significant +

    Contributions to this guide are welcome, especially if there are any significant inaccuracies identified. To contribute or report an issue, please initiate an issue at https://github.com/sysprog21/lkmpg. Pull requests are greatly appreciated. -

    Happy hacking! +

    Happy hacking!

    1The goal of threaded interrupts is to push more of the work to separate threads, so that the minimum needed for acknowledging an interrupt is reduced, and therefore the time spent handling