2017-2018-1 20179205 "Linux kernel Principles and Design" Tenth week assignment

Source: Internet
Author: User
Tags diff git commands

"Linux kernel Principles and Design" Tenth week work Materials 17, 19, 20 chapters study and Harvest

1, in Linux and all UNIX systems, the device is divided into the following three types: Block devices (Blkdev) are addressed in blocks, accessed through block device nodes, character devices (Cdev), non-addressable, accessed via character device nodes, network devices: Access to the network, Access through physical adapters and protocols.

2. loadable Kernel modules: A separate binary image allows the kernel to dynamically insert or delete code at runtime. If compiled into the kernel, the entry point init is stored in the kernel image, and the Exit function is not included and called.

3, export the symbol table: only the kernel function is exported, it can be called by the module, the code is configured as a module must ensure that the interface is all exported. The exported kernel symbol table is considered an exported kernel interface, the kernel API. Export directive: Export_symbol ()

Kernel Module related operations:

  • Module installation

    make modules_install  //把随内核编译出来的模块安装到合适的目录/lib/modules/version/kernel下

  • Module dependencies (Linux auto Generation)

    depmod     //产生内核依赖关系信息depmod -A  // 只为新模块生成依赖信息(速度更快)
  • Loading of modules

    insmod module.ko  //以root身份运行命令modprobe module [module parameters] //modprobe提供了模块依赖性分析、错误智能检查、错误报告以及许多其他功能和选项,需以root身份运行,强烈建议使用
  • Unloading of modules

    rmmod module.ko     //以root身份运行命令modprobe -r module  // 同模块的加载类似,modprobe命令也会卸载给定模块所依赖的相关模块
  • Module Export Symbol table

    EXPORT_SYMBOL(函数名)      //接在要导出的函数后面即可EXPORT_SYMBOL_GPL(函数名)  //和EXPORT_SYMBOL一样,区别在于只对标记为GPL协议的模块可见

    Examples of kernel module export symbols:

    Write a module that exports a function module_a:test_module_a.c

      #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h>module_ LICENSE ("Dual BSD/GPL"); static int test_export_a_init (void) {/* Enter kernel module */PRINTK (kern_alert "************************* \ n ");p rintk (kern_alert" ENTRY test_export_a!\n ");p rintk (kern_alert" *************************\n ");p RINTK (KERN_ ALERT "\n\n\n\n\n"); return 0;} static void Test_export_a_exit (void) {/* Exits kernel module */PRINTK (kern_alert "*************************\n");p RINTK (Kern_alert "EXIT test_export_a!\n");p rintk (kern_alert "*************************\n");p rintk (kern_alert "\n\n\n\n\n");} /* The function to be exported */int export_add10 (int param) {printk (kern_alert "param from the other module is:%d\n", param); return param + 10;} Export_symbol (EXPORT_ADD10); Module_init (Test_export_a_init); Module_exit (test_export_a_exit);  

    4, in the installation and unloading of multiple modules, to install module a first, then install module B; First unload module B, and then unload module A in the order.

    5. The original motive of the unified device model is to realize intelligent power Management, the Linux kernel needs to establish a tree structure that represents the topological relationships of all the devices in the system in order to realize intelligent power management, so that when power is turned off, it can be closed from the node of the tree. The core part of the unified device model is kobject, which enables each physical device to be organized in the form of a tree structure. Kset is a collection of Kobject objects that can be placed in a kset for all relevant kobject, such as all "block devices" that can be placed in a kset that represents a block device.

    5, Kset,ktype and SYSFS are closely related to Kobject.

    6. When coding portability codes, use opaque data structures to not assume the length of the type, as they may change in different architectures, and the length-determined type can only be used in kernel space, and user space cannot be used. The user space has a corresponding variable type, with 2 more underscores before the name. This is very error-prone when hitting the code, this week's Linux experiment when compiling kernel modules I only hit an underscore, resulting in an error.

    7, while coordinating the alignment of data, although the order of elements in the structure can be adjusted to reduce the amount of bytes filled, thus reducing memory consumption. However, for those structures that are already in the kernel, you must not arbitrarily adjust the order of their elements, because many of the existing methods in the kernel acquire elements through their position offsets in the struct.

    8. Use a memory barrier such as RMB () WMB () to ensure the processor's order of execution.
    9, the program at the beginning of the process will often appear bug or imperfect place, this can be done by submitting patches to improve. There are 2 ways to generate bugs or improve code patches:

      • Create a patch with the diff command

    Generate Patches

    diff -urN linux-old/ linux-new/ > my-patchCompared to the entire kernel code folder

    OR

    diff -u linux-old/some/file linux-new/some/file > my-patchCompared to a file

    After patches are applied to patches , the code in Linux-old and Linux-new is the same.

    cd linux-old

    patch -p1 < ../my-patchThis command is executed in the root directory of the Linux kernel code.

    diffstat -p1 my-patchDiffstat tool that lists the statistics of changes caused by patches (lines of code added or removed)

      • Create patches with GIT commands

    Commit a modified or new code

    git commit -aCommit all the modified code

    OR

    git commit linux-src/some/file.cTo commit a modified code

    OR

    git add linux-src/some/new-file.cAdd new files to the repository

    git commit -aSubmit a new file

    Generate Patches

    git format-patch -Nn is a positive integer, this command generates a patch from the last n commits

    OR

    git format-patch -1Patch generated by last 1 commits

    Apply Patches

    Same as the first method

    Summarize:

    After learning the Linux kernel design and implementation of the book, want to use the Linux system to do some practice, such as tapping some code, and so on, it is necessary to join the Linux community, because you can get the kernel of the latest information in the community, but also in the community with experienced kernel developers to exchange experience. It is also a place to commit code and discuss code, understand the rules of the community, integrate into the community environment, to better learn the kernel, experience the fun and sense of accomplishment of kernel development.

    Problem:

    Data alignment is an important aspect of enhancing portability, meaning that the memory address of a data structure can be divisible by 4. The char type only accounts for 1 bytes, its address does not necessarily divisible by 4, then converted to 4 bytes or 8 bytes of usigned long, why causes the unsigned long appears the data is not aligned phenomenon?

  • 2017-2018-1 20179205 "Linux kernel Principles and Design" Tenth week assignment

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.