


Linux driver runs in the "kernel" space. Generally, drivers call kmalloc() to allocate memory for data structures, call vmalloc() to allocate data structures for active swap areas, allocate buffers for some I/O drivers, or allocate space for modules; kmalloc and vmalloc allocate kernel memory.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
Linux driver runs in the "kernel" space.
For generally written microcontroller programs, applications and drivers are often mixed. Microcontroller programmers with a certain level of ability can realize the layering of applications and drivers. In Linux systems, applications and drivers have been forced to be layered.
In the microcontroller program, the application can directly operate the underlying registers. However, such behavior is prohibited in Linux systems. For example: The writer of a Linux application deliberately calls the power management driver in the application and shuts down the system. Isn't it worth the gain?
The specific Linux application calls the driver as shown in the figure:
The application runs in the user space, and the driver runs in the kernel space. If an application in user space wants to operate the kernel, it must use a "system call" method to enter the kernel space from user space and operate the underlying layer.
Kernel space in Linux
The kernel is also a program and should also have its own virtual memory space. However, as a program that serves user programs, the kernel space has its own characteristics.
The relationship between kernel space and user space
In a 32-bit system, the maximum virtual space of a program can be 4GB, then the maximum The straightforward approach is to regard the kernel as a program so that it has the same 4GB space as other programs. However, this approach will cause the system to continuously switch the page table of the user program and the kernel page table, thus affecting the efficiency of the computer. The best way to solve this problem is to divide the 4GB space into two parts: one part is user space and the other part is kernel space. This can ensure that the kernel space is fixed and unchanged, and when the program switches, only the The program's page table. The only disadvantage of this approach is that both kernel space and user space become smaller.
For example: On a 32-bit hardware platform such as i386, Linux defines a constant PAGE_OFFSET in the file page.h:
#ifdef CONFIG_MMU #define __PAGE_OFFSET (0xC0000000) //0xC0000000为3GB #else #define __PAGE_OFFSET (0x00000000) #endif #define PAGE_OFFSET ((unsigned long)__PAGE_OFFSET)
Linux uses PAGE_OFFSET as the boundary The 4GB virtual memory space is divided into two parts: the low address space at addresses 0~3G-1 is user space, with a size of 3GB; the high address space at addresses 3GB~4GB-1 is kernel space, with a size of 1GB.
When multiple programs are running in the system, the relationship between multiple user spaces and kernel space can be expressed as follows:
As shown in the figure As shown, programs 1, 2...n share the kernel space. Of course, sharing here refers to time-sharing, because at any time, for a single-core processor system, only one program can be running.
The overall layout of the kernel space
In the development process of Linux, with the update of hardware equipment and the improvement of technical level, its kernel space The development of the layout is also a continuous patching process. The consequence of this is that the kernel space is divided into several different areas, and different areas have different mapping methods. Usually, people think that the Linux kernel space has three areas, namely DMA area (ZONE_DMA), normal area (ZONE_NORMAL) and high-end memory area (ZONE_HIGHMEM).
Direct mapping of kernel space when the actual physical memory is small
The actual physical memory configured in early computers is usually only a few MB, so In order to improve the speed of the kernel accessing the physical address memory through the virtual address, the virtual address and the physical memory address of the kernel space adopt a fixed mapping method that corresponds one to one from the low address to the high address. As shown in the figure below Display:
It can be seen that This fixed mapping method makes the relationship between virtual address and physical address very simple, that is, the kernel virtual address and the actual physical address There is only a fixed offset PAGE_OFFSET in value, so when the kernel uses the virtual address to access the physical page frame, it only needs to subtract PAGE_OFFSET from the virtual address to get the actual physical address, which is faster than using the page table. many!
Since this approach is almost to directly use the physical address, this kind of kernel space based on a fixed mapping method is also called "physical memory space", or physical memory for short. In addition, Since the fixed mapping method is a linear mapping, this area is also called the linear mapping area.
Of course, in this case (when the actual physical memory of the computer is small), the kernel fixed mapping space only occupies a part of the entire 1GB kernel space. For example: When configuring an x86 computer system with 32MB of actual physical memory, the fixed mapping area of the kernel is the 32MB space of PAGE_OFFSET~ (PAGE_OFFSET 0x02000000). So what to do with the remaining kernel virtual space in the kernel space?
Of course, physical memory is still used in the non-linear mapping of page tables according to the management method of ordinary virtual space. Specifically, the fixed mapping area is removed from the entire 1GB kernel space, and then an 8MB isolation area at the beginning is removed from the remaining part. The remainder is an ordinary virtual memory mapping area that is mapped in the same way as user space. In this area, there is not only no fixed mapping relationship between virtual addresses and physical addresses, but also dynamic memory is obtained by calling the kernel function vmalloc(), so this area is called the vmalloc allocation area, As shown in the figure below:
For an x86 computer system configured with 32MB of actual physical memory, the starting position of the vmalloc allocation area is PAGE_OFFSET 0x02000000 0x00800000.
Let me explain here: The fixed mapping between the kernel space and the physical page frame mentioned here is essentially a "predetermination" of the kernel page to the physical page frame. It does not mean that these pages "occupy" the physical page frame. ” these physical page frames. That is, the virtual page is bound to the physical page frame only when the virtual page actually needs to access the physical page frame. Normally, when a physical page frame is not used by its corresponding virtual page, the page frame can be used by user space and the kernel kmalloc allocation area introduced later.
In short, in a system with smaller actual physical memory, the size of the actual memory is the boundary between the physical memory area of the kernel space and the vmalloc allocation area.
ZONE_DMA area and ZONE_NORMAL area
For the entire 1GB kernel space, people also call the 16MB at the head of the space DMA Zone, that is, ZONE_DMA zone, because in the past hardware fixed the DMA space in the lower 16MB space of physical memory; the remaining zones are called normal zones, that is, ZONE_NORMAL.
High-end memory of the kernel space
With the development of computer technology, the actual physical memory of the computer is becoming more and more Large, so that the kernel fixed mapping area (linear area) becomes larger and larger. Obviously, if no restrictions are imposed, when the actual physical memory reaches 1GB, the vmalloc allocation area (non-linear area) will no longer exist. Therefore, the previously developed kernel code that called vmalloc() is no longer available. Obviously, in order to be compatible with earlier kernel codes, this is not allowed.
The following figure shows the situation faced by this kernel space:
Obviously, The reason for the above problems is that the actual situation was not anticipated Physical memory can exceed 1GB, so there is no limit set on the boundaries of the kernel's fixed mapping area, which is allowed to grow with the actual physical memory.
The way to solve the above problem is: Limit the upper limit of the fixed mapping area of the kernel space so that it cannot increase arbitrarily with the increase of physical memory. Linux stipulates that the upper boundary value of the kernel mapping area cannot be greater than a constant high_menory that is less than 1G. When the actual physical memory is large, 3G high_memory is used as the boundary to determine the physical memory area.
For example: For x86 systems, the value of high_memory is 896M, so the remaining 128MB of 1GB kernel space is a non-linear mapping area. This ensures that in any case, the kernel has enough non-linear mapping area to be compatible with early code and can access more than 1GB of actual physical memory in a normal virtual memory manner.
In other words, the most basic idea of high-end memory: borrow a section of address space to establish a temporary address mapping, and release it after use. When this address space is reached, it can be recycled and access all physical memory. When the computer has a large physical memory, the schematic diagram of the kernel space is as follows:
Traditionally, Linux calls this part of the kernel space 3G high_memory~4G-1 High-end memory zone (ZONE_HIGHMEM).
To summarize: In the kernel space of the x86 structure, the three types of areas (calculated from 3G) are as follows:
- ZONE_DMA: 16MB from the beginning of the kernel space
- ZONE_NORMAL: 16MB~896MB of the kernel space (fixed mapping)
- ZONE_HIGHMEM: Kernel space 896MB ~ end (1G)
According to different application targets, high-end memory is divided into vmalloc area, persistent mapping area and temporary mapping area. The layout of high-end memory in the kernel space is as shown below:
vmalloc mapping area
## The #vmalloc mapping area is the main part of high-end memory. There is an 8MB isolation area between the head of the interval and the kernel linear mapping space, and a 4KB isolation area between the tail and the subsequent persistent mapping area.The mapping method of vmalloc mapping area is exactly the same as that of user space. The kernel can obtain memory in this area by calling the function vmalloc(). The function of this function is equivalent to malloc() in user space. The provided memory space is continuous at the virtual address (note that the physical address is not guaranteed to be continuous).
Persistent kernel mapping area
If the page corresponding to the high-end memory is obtained through alloc_page(), how to find it? A linear space? The kernel specially sets aside a linear space for this purpose, starting from PKMAP_BASE, which is used to map high-end memory, which is the durable kernel mapping area.In the persistent kernel mapping area, you can establish a long-term mapping between the physical page frame and the kernel virtual page by calling the function kmap(). This space is usually 4MB and can map up to 1024 page frames. The number is relatively rare. Therefore, in order to enhance the turnover of page frames, the function kunmap() should be called in time to release the physical page frames that are no longer used.
Temporary mapping area
The temporary mapping area is also called the fixed mapping area and the reserved area. This area is mainly used in multi-processor systems. Because the memory space obtained in this area is not protected, the memory obtained must be used in time; otherwise, once there is a new request, the content on the page frame will be overwritten. , so this area is called the temporary mapping area. A very good article about the high-end memory area:linux user space and kernel space-detailed explanation of high-end memory.
Kernel memory allocation modifier gfp
#In order to make the necessary description of the request in the kernel memory request function, Linux defines many A memory allocation modifier gfp. They are behavior modifiers, area modifiers, and type modifiers.
Behavior modifiers
The behavior modifiers in the memory allocation function describe how the kernel should allocate memory. The main behavior modifiers are as follows:Description | |
The allocator can sleep | |
The allocator can access the emergency buffer pool | |
The allocator can start disk IO | |
The allocator can start file system IO | |
The allocator should use page frames that are about to be evicted from the cache | |
The allocator does not issue WARNING | |
Reallocation on allocation failure | |
Reallocation on allocation failure, Until successful | |
No reallocation of the |
area when allocation fails Modifier
#The area modifier indicates which area of the kernel space needs to allocate memory. By default, the memory allocator starts from ZONE_NORMAL in the kernel space and gradually allocates the memory area to the memory requester. If the user specifically needs to obtain memory from ZONE_DMA or ZONE_HOGNMEM, then the memory requester needs to use the following two in the memory request function. Area modifier description:
Description | ||
Allocate memory from ZONE_DMA area | ||
Allocate memory from ZONE_HIGHMEM area |
Kernel space (3G~4G) |
High-end memory (3G high_memory ~4G) ZONE_HIGHMEM Nonlinear mapping area |
Temporary mapping area |
Persistent mapping area | ||
vmalloc area | ||
Low-end memory (3G~3G high_memory-1) Linear mapping area (fixed mapping area) |
ZONE_NORMAL | |
ZONE_DMA | ||
Page Directory-- >Intermediate page directory-->Page table |
Linux Video Tutorial"
The above is the detailed content of In what space does the linux driver run?. For more information, please follow other related articles on the PHP Chinese website!

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud

When choosing a Hadoop version suitable for Debian system, the following key factors need to be considered: 1. Stability and long-term support: For users who pursue stability and security, it is recommended to choose a Debian stable version, such as Debian11 (Bullseye). This version has been fully tested and has a support cycle of up to five years, which can ensure the stable operation of the system. 2. Package update speed: If you need to use the latest Hadoop features and features, you can consider Debian's unstable version (Sid). However, it should be noted that unstable versions may have compatibility issues and stability risks. 3. Community support and resources: Debian has huge community support, which can provide rich documentation and

This article describes how to use TigerVNC to share files on Debian systems. You need to install the TigerVNC server first and then configure it. 1. Install the TigerVNC server and open the terminal. Update the software package list: sudoaptupdate to install TigerVNC server: sudoaptinstalltigervnc-standalone-servertigervnc-common 2. Configure TigerVNC server to set VNC server password: vncpasswd Start VNC server: vncserver:1-localhostno

Configuring a Debian mail server's firewall is an important step in ensuring server security. The following are several commonly used firewall configuration methods, including the use of iptables and firewalld. Use iptables to configure firewall to install iptables (if not already installed): sudoapt-getupdatesudoapt-getinstalliptablesView current iptables rules: sudoiptables-L configuration

The steps to install an SSL certificate on the Debian mail server are as follows: 1. Install the OpenSSL toolkit First, make sure that the OpenSSL toolkit is already installed on your system. If not installed, you can use the following command to install: sudoapt-getupdatesudoapt-getinstallopenssl2. Generate private key and certificate request Next, use OpenSSL to generate a 2048-bit RSA private key and a certificate request (CSR): openss

Configuring a virtual host for mail servers on a Debian system usually involves installing and configuring mail server software (such as Postfix, Exim, etc.) rather than Apache HTTPServer, because Apache is mainly used for web server functions. The following are the basic steps for configuring a mail server virtual host: Install Postfix Mail Server Update System Package: sudoaptupdatesudoaptupgrade Install Postfix: sudoapt

To configure the DNS settings for the Debian mail server, you can follow these steps: Open the network configuration file: Use a text editor (such as vi or nano) to open the network configuration file /etc/network/interfaces. sudonano/etc/network/interfaces Find network interface configuration: Find the network interface to be modified in the configuration file. Normally, the configuration of the Ethernet interface is located in the ifeth0 block.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.