1. Linux version
1. Stable and development versions
Linux
The kernel is mainly divided into two versions:
- Stable version (long-term support version): The stable version of the kernel has industrial strength and can be widely used and deployed. Most of the newly released stable kernels of each generation only fix some bugs or add some new device drivers.
- Development version: The kernel developers in the development version of the kernel are constantly experimenting with new solutions, so the code in the development version of the kernel changes very quickly
These two versions Linux
are distinguished by version numbers.
2. Linux version number
Linux
The version number of is mainly composed of three groups of numbers xxx.yyy.zzz
:
xxx
: major version number (Major Release
)yyy
: minor version number (Minor Release
)zzz
: revision number (Revision Count
)
in:
- Add new drivers and fix bugs between minor versions, for example:
3.21.x
and3.22.x
- When the minor version has accumulated to a certain extent, a new major version will be released, for example:
5.x.x
and6.x.x
- After each minor version is released, people may find BUG in the process of using it, so a revised version of the minor version will be released, such as
5.6.13
and5.6.14
The Linux
stable version and the development version are distinguished by the minor version number:
- An even version number indicates that the version of the kernel is a stable version
- An odd version number indicates that the version of the kernel is a beta version
For example, Linux 3.6.34
:
主版本号
:3次版本号
:6, stable version修订版本号
:34, indicating that the current version isLinux 3.6
revision 34 of
However, the combination of the major version number and the minor version number can actually describe a version of the kernel, because subsequent revisions are all bug fixes and do not involve the release of new features.
Two, Linux kernel source code organization structure
Linux
The kernel source code is huge. The 2020 version of the kernel source code already has 27.8 million lines, and these codes are scattered in 66,492 C files. Linux
The source files of the code are stored in different directories according to a certain organizational structure, and the functions of the source code in each directory have the same point in logic .
In addition to directories, there are also some files, which have special roles and purposes.
1. Directory
Linux
A series of directories are stored under the root directory of the source code, and there are many C codes in each directory. According to the functional division of these C codes logically, put them into the same folder.
A. arch
folder
arch
The code related to the architecture is stored in the directory, which is used to shield the differences between different systems and platforms. For example:
RISC-V
The address translation function of the architecture thatCPU
opens the virtual address needs to read and writesatp
registers andmstatus
registersx86
The address translation function of theCPU
open virtual address needs to read and writecr3
registers andcr0
registers
Linux
The function to open the virtual address translation function in is , switch_mm
and because different architectures require different operations to open the virtual address translation, the switch_mm
function is placed arch
in the directory.
In fact, switch_mm
there are many similar functions, such as interrupt processing functions... So, Linux
the code that depends on each system is placed in arch
the directory in the kernel
B. block
folder
block
The folder stores Linux
the kernel's block device drivers. A block device refers to storing information in fixed-size blocks, each block has its own address, and then CPU
you can read a certain length of data at any location on the device by specifying the address.
A block device actually refers to a type of device, specifically I/O
a device, that is, a device that stores data, rather than CPU
a computing device such as a graphics card, such as: 硬盘
, U盘
, SD卡
.
Note that there is a directory Linux
under the root directory of the kernel source code that will be mentioned later, and the drivers for various devices are placed in this directory. drivers
Logically speaking, the block device driver should also be a kind of driver, but why pull it block
out separately instead of putting it drivers
under the directory?
This is actually because drivers
the drivers of various devices are actually placed under the directory, for example:
driver/cdrom
The driver of the CD-ROM is stored in the folderdriver/usb
The driver of the device is stored in the folderUSB
, such as a U disk
In fact, both read-only CDs and CD-ROMs USB
are block devices, so the block operations, such as reading blocks and writing blocks, are the same, but the operations of reading blocks are different for specific devices.
Therefore, Linux
the block
general block device driver is stored in the directory, that is, the functions of writing blocks and reading blocks, and the functions of specific devices reading blocks are in the directory driver/设备
. So Linux
put block
the directory in the root directory.
In terms of implementation, except for block
the directory, it is the same ipc
as the directory. net
It just stores general ipc
device drivers and general network drivers.
C. certs
Directory
certs
Linux
Codes related to authentication and signing are stored in the directory . This directory contains some pre-installed digital certificates, which can be used to verify signed modules, kernel code and user space applications, etc. These certificates can be used to ensure that these components come from trusted sources, thereby improving System security.
The reason why this directory is needed is because Linux
the kernel 2.2
supports dynamic loading since the version 内核模块
.
kernel module
When compiling the kernel, all driver, file system or network protocol codes will be compiled into the kernel. In the end, the kernel will be full of drivers for various devices. Maybe there are only more than 10 kinds of devices on your computer's hardware system, but there are more than 400 drivers for devices in the running kernel.
It is precisely because of this that the concept proposed after the version
Linux
is to separate the file system, driver, etc. from the binary program of the kernel, and generate a separate file during the compilation phase. Which program is needed in the future, just load the corresponding file.2.2
内核模块
.so
.so
For example, suppose we are now a researcher of file system and storage. We
ext
have improved the shortcomings of the file system and proposed our own file systemIron
file system. We want to test our file system, so at this time we can内核模块
writeIron
the program of the file system in the form of , and the kernel can load the file system we wrote into the memoryLinux
when it is runningIron
内核模块
内核模块
They can be dynamically loaded and unloaded while the system is running, which means they are not necessarily loaded at system startup, but are loaded when needed. This dynamic loading method brings great benefits to the flexibility and scalability of the system, but it also brings some potential security risks to the system.
For example, an attacker can add some malicious code on top of the code that implements normal kernel module functions, such as stealing sensitive information, denial of service attacks, and privilege escalation. Finally, it is disguised as a legitimate kernel module, and the compiled .so
file is replaced by the original normal one 内核模块
. Finally, when the user loads these malicious 内核模块
kernel modules, these malicious kernel modules start to damage the system.
Because of this, multiple components Linux
included need to 内核模块
be verified to prevent the source code of an open source, verified Linux
kernel from being inserted with malicious code, or loaded with a malicious kernel module.
D. crypto
directory
crypto
Linux
The compression and encryption algorithms commonly used by the kernel are stored in the directory .
1. Encryption algorithm in the kernel
Linux
The kernel needs to use encryption algorithms in many places. Basically, Linux
various security functions are implemented in the system, such as: encrypted file system, network transmission encryption, digital signature, secure login, etc. where passwords need to be saved, all need to be encrypted.
Therefore, the directories Linux
in the kernel crypto
contain the implementations of commonly used encryption algorithms, including: AES
, DES
, SHA1
, SHA256
and so on.
In addition, because encryption and decryption are used very frequently, Linux
the kernel crypto
directory also provides drivers for hardware that accelerates encryption/decryption, such as hardware-based AES
accelerators. Using these drivers to call these hardware can improve the performance Linux
of kernel encryption and decryption. performance.
2. Kernel compression algorithm
Linux
The source code of the kernel is currently 1.1G, and the final compiled binary format kernel will only be larger. Before booting, Linux
the kernel exists on the disk in the form of a file, and it needs to be loaded into the memory if it wants to run. Therefore, in order to reduce startup time and memory usage, Linux
the kernel will compress itself, and then decompress the corresponding code when needed.
Therefore, the main purpose of compression algorithms in the kernel is to reduce the size of the kernel image . Without compression, it will take longer to load and execute the kernel image and take up more memory space.
By using the compression algorithm, the size of the kernel image can be reduced to half or even smaller, thereby improving the startup speed and saving memory space . In addition, some file systems also use compression algorithms to reduce storage space usage and improve file system performance.
Of course, both compression and decompression are overhead. Although the compression algorithm can reduce the size of the kernel image, it will also increase the decompression time when the kernel starts. Therefore, Linux
when the kernel selects a specific compression algorithm, it will choose a compression algorithm according to the need to balance the compression ratio and decompression time, or directly use the compression algorithm specified by the user to obtain the best performance.
Specifically, the various compression algorithms implemented crypto
in the directory include: , , , …Linux
LZO
LZ4
Zlib
Deflate
E. Documentation
Directory
Documentation
The directory is Linux
the document of the kernel, which mainly describes the functions of various modules and defines some specifications
For example:
cat Documentation/riscv/boot-image-header.rst | less
F. drivers
Directory
drivers
The directory stores Linux
the drivers for various hardware of the kernel. Example GPIO
device:
ls drivers/gpio | less
Different CPU
models GPIO
have different settings and initialization methods, so the codes for different chips are stored in the Linux
directory .drivers/gpio
GPIO
G. fs
Directory
fs
Linux
The code of the virtual file system and the codes of various types of file systems are stored in the directory .
For example, the implementation of Windows
the more common ntfs
file system is in fs/ntfs
the directory
ls fs/ntfs | less
H. include
Directory
include
Linux
Most of the header files that the kernel source code depends on are stored in the directory . Each header file contains various definitions and declarations of the kernel, providing the necessary support for the construction and development of the kernel.
I. init
Directory
init
The code for kernel initialization is stored in the directory.
The entire running process of the kernel can actually be regarded as two processes:
- Press the power button to start the kernel, and then the kernel is ready to wait for the user to use this process. This process is called
初始化
- The user starts to use the kernel, and the kernel works normally until it is shut down. This process is the normal operation process of the kernel
During the initialization phase, the kernel needs to do many things, such as:
- Count available physical memory, enable virtual address translation, initialize memory management module...
- Initialize the thread management module, build the kernel thread, create the init thread to run and user interactive shell...
- ……
Since the kernel is composed of multiple components, the code in the kernel initialization phase actually enters each component of the kernel to complete the initialization of each component .
J. ipc
Directory
ipc
The directory is the implementation of inter-process communication, and will be compiled into the inter-process communication module of the kernel in the future. Linux
Various commonly used inter-process communication mechanisms, such as:
- amount of signal
- Shared memory
- anonymous pipe
- ……
The implementation codes are all ipc
under this directory.
ls ipc
K. kernel
Directory
kernel
The directory is the core code of the kernel, including:
- process management
- interrupt management
- clock
- ……
These are the core functional components of the kernel, so they are all placed kernel
under this directory.
L. lib
Directory
lib
The directory contains some common library functions, such as: memset
, strlen
... These functions can be used by other parts of the kernel, thus simplifying the development of the kernel. Note that we can also adapt #include <stdlib.h>
to the user program written by ourselves , but this is different from the one in the kernel directory.#include<stdio.h>
memset
memset
lib
memset
We use the functions memset
defined C标准库
in , but C标准库
actually need the support of the operating system, so when we write the source code of the kernel, they are not C标准库
used for us. We have to manually implement some C标准库
functions by ourselves.
Therefore, from this perspective, the directory in the kernel source code lib
is actually C标准库
a subset of the implementation.
In addition, lib
there are implementations of some common data structures and algorithms in the directory, such as linked list, hash table, red-black tree, bitmap, etc.
M. mm
Catalog
mm
The directory is Linux
the implementation related to kernel memory management, including:
- physical memory management
- Page Allocation Algorithm
- Page fault interrupt, page change algorithm
- ……
N. net
directory
net
The directory block
is similar to the directory, because there are many kinds of network devices, such as: wireless network card (that is WiFi
), Ethernet (that is, wired), and 4G... and the driver codes of these specific network devices are placed in drivers
the directory.
No matter how many types of devices there are, the network protocol stack is actually the same. For example, wireless network cards and Ethernet are used to send and receive data packets. Although the devices used to send and receive data packets are different, they all follow the IPv4 network protocol. Therefore, Linux
the kernel puts the code implemented by the network protocol in net
the directory.
net
Network protocols implemented in the catalog include:
TCP
IPv6
DNS
- ……
O. samples
Directory
samples
Some kernel sample codes and programs are stored in the directory Linux
. These codes and programs can help novice kernel engineers better understand the Linux
working principle and implementation details of the kernel.
samples
The sample codes and programs in the directory can help developers learn how to use Linux
functions implemented by the kernel, such as functions related to network protocol stacks, file systems, drivers, schedulers, and other related functions in functional modules.
Linux
At the same time, these sample codes and application programs can also be used as references and examples for developers to develop their own kernel modules and application programs.
Finally, samples
there are some programs in the directory as test cases, which can be used to test Linux
the correctness and performance of various functions and interfaces of the kernel.
Basically, when we first started to learn Linux
kernel development, or Hacking Linux Kernel
when, in order to avoid directly modifying the code in other directories to insert our own code would make it Linux
impossible to run and difficult to debug, we generally put the code in samples
the directory for testing. When we improve our skills in the future, we will modify the code in other directories.
P. scripts
catalog
Linux
The directories in the kernel source directory scripts
contain some scripting tools that can help kernel developers perform kernel compilation, debugging, analysis, and optimization.
Specifically, scripts
the directory contains the following categories of tools:
- Compilation tools :
scripts
The directory contains some tool scripts for compiling the kernel, such asmake
,gcc
,ld
and other tool scripts. These tool scripts can help developers compile kernel source code and generate executable kernel image files. - Debugging tools :
scripts
The directory contains some scripting tools for debugging the kernel, such as scriptsgdb
for ,kgdb
,kdb
and other tools. These scripts can help developers debug the kernel, locate and solve various problems in the kernel. - Analysis tools :
scripts
The directory contains some script tools for analyzing the kernel, such asperf
, ,trace-cmd
and so on. These tools can help developers perform performance analysis, trace and statistics on the kernel to find performance bottlenecks and optimization points in the kernel. - Code inspection tools :
scripts
The directory contains some script tools for code inspection, such ascheckpatch.pl
, ,sparse
and so on. These scripting tools can help developers check code style, syntax errors, memory leaks and other issues in the kernel source code to improve code quality and maintainability.
Q. security
Catalog
Linux
Directories within the kernel source directory security
provide Linux
implementations of kernel security mechanisms such as: Access Contol List
(i.e. ACL
),SELinux
…
The code in this directory mainly implements security-related functions, so this directory provides security-related modules and interfaces, which can help developers enhance the security performance of the system Linux
.
Specifically, security
the directory contains the following types of modules and interfaces:
- Security modules :
security
The directory contains some security modules, such asSELinux
,AppArmor
etc. These security modules can help developers implement access control and security strategy management for various resources in the system, thereby improving the security performance of the system. - Security interface :
security
The directory also contains some security interfaces, such assecurity_inode_permission
,security_file_permission
etc. These security interfaces can help developers implement access control and rights management to various resources in the system, thereby protecting sensitive data and applications in the system. - Security policy :
security
The directory also contains some security policies, such ascapability
, ,posix_acl
and so on. These security policies can help developers implement access control and rights management to various resources in the system, thereby protecting sensitive data and applications in the system.
R. sound
Directory
sound
The directory is Linux
the directory in the kernel source that contains sound-related drivers and modules. Similar to the net
directory block
, the driver of a specific type of sound card device is placed drivers
in the directory, and sound
the code in the directory focuses on the general sound playback, recording and processing functions.
Therefore, sound
the directory calls drivers
the drivers of different types of sound cards in the directory, so that the kernel Linux
can support the use of multiple sound card devices to play, record and process sounds, and provides a series of interfaces and functions for user programs.
Specifically, sound
what the directory mainly implements, or Linux
the architecture of the module that currently processes sound , is the abbreviation for , ALSA
which is an advanced architecture for sound processing in the system. The specification defines a series of drivers and libraries, enabling the system to support a variety of sound card devices, and provides a series of interfaces and functions, enabling applications to conveniently record, play, and process sounds.Advanced Linux Sound Architecture
Linux
alsa
Linux
In alsa
addition, sound
the directory also contains some other sound drivers and modules, such as oss
the subdirectory, which Open Sound System
is an acronym for the old Linux
sound architecture, which has gradually been alsa
replaced.
In addition, sound
the directory also contains some other sound drivers and modules, such as USB sound card driver, Bluetooth headset driver, etc. These drivers and modules can support a variety of sound card devices, and provide a series of interfaces and functions, enabling the Linux system to play, record and process sound.
S. tools
Directory
In the Linux kernel source directory, tools
the directory contains the source code of some tools and utilities, which are usually used for kernel development and debugging. Some of these tools are written in C, while others are Python
written in or other scripting languages. Specifically:
- These tools include some programs for system debugging and performance analysis, such as
perf
andftrace
- Also includes some tools for kernel construction and compilation, such as
kconfig
andkbuild
- Also contains some programs for simulation and testing, such as
ktest
andkvm
etc.
tools
The tools and utilities in the directory are very important for kernel development and debugging. Usually, a senior Linux
kernel developer needs to be familiar with the usage and implementation principles of these tools.
T. usr
Catalog
usr
The directory is the source code for user-packaged and compressed kernel implementations.
U. virt
Directory
virt
The directory provides Linux
the code implementation of the kernel's support for virtualization.
For example, virt/kvm
the directory contains the source code of the virtualization module Linux
in the kernel , which allows the Linux kernel to act as a virtual machine monitor ( ) to run virtual machinesKVM
VMM
W. LICENSE
Directory
Linux
The directories in the kernel source directory LICENSES
contain Linux
the text of the various licenses used in the kernel source, including GPL
, LGPL
, BSD
, MIT
and so on.
The purpose of this directory is to allow Linux
developers of the kernel source code to easily view and understand the specific content and restrictions of each license.
This directory is to ensure Linux
the openness and transparency of the kernel source code, so that everyone can understand Linux
the usage conditions and restrictions of the kernel source code, so as to better comply with these licenses and regulations.
2. Documentation
In addition to the directory, Linux
there are several files stored in the kernel source code directory, and these files have different usages.
A. COPYING
Documents
Linux
The files in the kernel source code directory COPYING
are copyright notice files, ie licenses, which stipulate Linux
the conditions and restrictions on the use of the kernel source code.
Linux
The license used by the kernel source code is GPLv2
. GPLv2
The open source license states that anyone is free to use, copy, distribute, and modify Linux
the kernel source code, but the results obtained are also GPL
open sourced using the license.
It should be noted that Linux
most of the source code of the kernel uses the same GPLv2
, but there are some exceptions, such as some system calls, the corresponding authorization statement is LICENSES/exceptions/Linux-syscall-note
in
b. CREDIT
file
Linux
The files in the kernel source directory CREDIT
list all Linux
people who have contributed to the kernel, including:
- kernel developer
- maintainer
- Testers
- ……
CREDIT
The purpose of the file is to recognize and thank all Linux
those who have contributed to the kernel, documenting their contributions and achievements.
At the same time, CREDIT
the document is also Linux
a part of the community culture, which emphasizes Linux
the openness, cooperation and community spirit of kernel development, so that everyone can participate in Linux
the development and maintenance of the kernel.
c. Kbuild
file
Linux
The files in the kernel source directory Kbuild
are used to build Linux
the kernel Makefile
. Note that Kbuild
the file mainly defines the configuration for building Linux
the kernel Makefile
. The commands to run when compiling the kernel are mainly defined in Makefile
the file
Kbuild
The function of the file is to automate the process of building Linux
the kernel, so that developers can easily compile, build and install Linux
the kernel.
At the same time, Kbuild
the file also provides some advanced compilation functions, such as:
- Support modular compilation
- cross compile
- parallel compilation
- ……
These features make Linux
the construction of the kernel more flexible and efficient.
D. MAINTAINERS
file
Linux
The files in the kernel source directory MAINTAINERS
keep a list of maintainers for all current kernels. It records Linux
the maintainers and contributors of various subsystems in the kernel.
MAINTAINERS
The role of the file is to help developers quickly find the maintainer or contributor responsible for a subsystem, so as to facilitate code submission, repair, or collaborative development.
MAINTAINERS
The file lists Linux
the names, email addresses, companies, responsible subsystems, and other information of the maintainers and contributors of each subsystem in the kernel. Through this file, developers can find the maintainers or contributors who are responsible for the subsystems they care about, and submit code or report problems to them.
In addition to being a reference for developers, MAINTAINERS
the documentation also serves as Linux
an organizational and management tool for the kernel community. Through this file, Linux
the kernel community can manage and coordinate the maintainers and contributors of each subsystem to ensure the Linux
good development and stability of the kernel.
E. Makefile
Documentation
Linux
The files in the kernel source directory Makefile
are used to compile the kernel. It contains a series of commands that should be executed when compiling the kernel, including:
- Commands for generating kernel images
- Commands to generate kernel modules
- ……
Makefile
The file will read Kbuild
the configuration information in the file to generate compilation commands to compile the kernel source code.
In addition to compiling kernel images and modules, Makefile
other operations are defined in the file, such as:
- install kernel
- packaged kernel
- Clear compiled files
- ……
Makefile
The file make
performs different operations according to specifying different targets when calling the command, such as make all
compiling the kernel and modules, make install
installing the kernel, make clean
clearing files generated by compilation, and so on.
F. README
Documentation
Linux
The files in the kernel source directory README
provide some basic information about that particular version of the kernel, such as:
- version number of the kernel
- Supported hardware platforms
- Installation and Configuration Guide
- ……
Additionally, README
the file may contain other useful information such as:
- Known Issues, Limitations
- BUGfix
Typically, README
files provide basic information about the kernel to help users get started quickly.