What happens when we do insmod on a module


Kernel module is a piece of kernel code which can be added to the running kernel when loaded and can be removed from the kernel when the functionality is removed.

When we do insmod on a module, it performs a series of steps:

  1. It calls init_module() to intimate the kernel that a module is attempted to be loaded and transfers the control to the kernel
  2. In kernel, sys_init_module() is run. It does a sequence of operations as follows:
    1. Verifies if the user who attempts to load the module has the permission to do so or not
    2. After verification, load_module function is called.
      1. The load_module function assigns temporary memory and copies the elf module from user space to kernel memory using copy_from_user
      2. It then checks the sanity of the ELF file ( Verification if it is a proper ELF file )
      3. Then based on the ELF file interpretation, it generates offset in the temporary memory space allocated. This is called the convenience variables
      4. User arguments to the module are also copied to the kernel memory
      5. Symbol resolution is done
      6. The load_module function returns a reference to the kernel module.
    3. The reference to the module returned by load_module is added to a doubly linked list that has a list of all the modules loaded in the system
    4. Then the module_init function in the module code is called

Comments

  1. Great post
    Steps are explained in depth also
    Thanks

    ReplyDelete
  2. Very good explanation.
    Useful information .
    Thanks

    ReplyDelete
  3. What is elf module here
    Which will be copied from user space

    ReplyDelete

Post a Comment

Popular posts from this blog

bb.utils.contains yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig

PR, PN and PV Variable in Yocto