Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the significance of THIS_MODULE in Linux kernel module drivers?

In Linux device driver development, the file_operations structure uses struct module *owner.

  • What is the use of this structure when we always initialize it with THIS_MODULE?
  • When can this field be set to NULL?
like image 662
Anup Warnulkar Avatar asked Oct 19 '13 14:10

Anup Warnulkar


People also ask

What is This_module?

What is THIS_MODULE? Whenever you create a kernel module, the kernel's build machinery generates a struct module object for you, and makes THIS_MODULE point to it. This struct contains many fields, some of which can be set with module macros such as MODULE_VERSION.

What is the purpose of a Linux kernel module?

Linux Kernel Modules. Kernel modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system. A module can be configured as built-in or loadable.

What does struct Cdev Cdev represent in kernel space?

struct cdev is the kernel's internal structure that represents char devices; this field contains a pointer to that structure when the inode refers to a char device file.

What is the purpose of the Module_init and Module_exit macros for loadable kernel modules?

The module_init() and module_exit() macros are used to specify the functions that should be called when the module is loaded and unloaded, respectively.


2 Answers

Minimal runnable example

Whenever you create a kernel module, the kernel's build machinery generates a struct module object for you, and makes THIS_MODULE point to it.

This struct contains many fields, some of which can be set with module macros such as MODULE_VERSION.

This example shows how to access that information: module_info.c:

#include <linux/module.h>
#include <linux/kernel.h>

static int myinit(void)
{
    /* Set by default based on the module file name. */
    pr_info("name    = %s\n", THIS_MODULE->name);
    pr_info("version = %s\n", THIS_MODULE->version);
    return 0;
}

static void myexit(void) {}

module_init(myinit)
module_exit(myexit)
MODULE_VERSION("1.0");
MODULE_LICENSE("GPL");

Dmesg outputs:

name    = module_info
version = 1.0

Some MODULE_INFO fields can also be "accessed" in the following ways:

  • cat /sys/module/module_info/version
  • modinfo /module_info.ko | grep -E '^version:'

Since the address of that struct module object must be unique across all modules, it serves as a good argument for fops.owner as mentioned at: https://stackoverflow.com/a/19468893/895245. Here is a minimal example of that usage.

Tested in Linux kernel 4.16 with this QEMU + Buildroot setup.


[1] struct module *owner is commonly used at some structures and is not an operation at all; it is a pointer to the module that "owns"the structure. This field is used to prevent the module from being unloaded while its operations are in use. Almost all the time, it is simply initialized to THIS_MODULE, a macro defined in < linux/module.h> .

.

[2] I would not recommend you to set it to null, because it may lead to driver malfunction and other problems. Instead, use the good practices of linux kernel development.

In some architectures the ".owner" was removed, so, make sure your distro and architecture still using it.

I hope it helps your understanding.

References: LDD3, kernel newbies.

like image 39
Pavel Teixeira Avatar answered Sep 21 '22 13:09

Pavel Teixeira