Linux memory Management (x86-32 bit system)

Source: Internet
Author: User

The management of Linux memory is mainly divided into two parts, address management and storage device management. Here are some of my understanding of memory management for these two sections.

Basic concepts of hardware addresses
    • DRAM domain Address: is a collection of address spaces that the DRAM controller can access.
    • PCI bus domain address: is a collection of address spaces that can be accessed directly by a PCI device.
    • Memory Domain Address: Is the collection of address spaces that the CPU can access.

The above concepts are explained in combination:

When the CPU accesses the DRAM domain or PCI bus domain address space, address translation is required (converting the memory domain address to the address of the corresponding domain). For example, when the CPU accesses the DRAM domain, it needs to convert the memory domain address space to the DRAM domain address space (which is done by the DRAM controller); When the CPU accesses the PCI bus domain, it needs to convert the memory domain address space to the PCI bus domain address space (completed by host Main bridge).

In a x86 processor system, the DRAM domain and the PCI bus domain are mapped to the memory domain space, and the addresses in most of the DRAM domains correspond to and are equal to address one by one in the memory domain, and the PCI address of the memory domain corresponds to and is equal to the address of the PCI bus domain. Their mapping in the memory domain space is independent of each other, and the mapping relationship is provided by the BIOS (e820 address mapping table).

When the PCI device accesses the DRAM domain address space, the PCI bus domain address is first converted to the memory domain address by the host main bridge, and then the memory domain address is converted by the DRAM controller to the DRAM domain address.

Basic concepts of software addresses
    • logical Address , which is a 32-bit long address. All process addresses use logical addresses.

    • A linear address (also known as a virtual address)is a 32-bit long address that can be used to represent addresses up to 4G, with values ranging from 0x00000000 to 0xFFFFFFFF. It is through the hardware circuit of the segmented unit to convert the logical address to a linear address, the logical address plus a segment start address to get a linear address. In Linux, all segments start with 0x00000000, so the logical address in Linux is equal to the linear address, that is, the process address is also a linear address. We no longer discuss logical addresses, but use linear addresses to represent the addresses used by the process.

    • The Physical Address is the conversion of a linear address to a physical address through the hardware circuitry of the paging unit. the address is the memory domain address mentioned above and is the collection of address spaces that the CPU can access .

The above concepts are explained in the following diagram:

In the X86 system, all processes use a logical address, which is converted to a linear address through a segmented unit hardware circuit, and then converted to a physical address (that is, the memory domain address) through the paging unit hardware circuit.

The logic of the Fragment cell is as shown (converting the logical address to a linear address)

The logic of the paging unit as shown (converting a linear address to a physical address)

Address Management

Linux divides the linear address space into user and kernel two spaces

    • The user linear address space , which refers to a linear address space that can be accessed by the application layer. Its size (32-bit processor) = 4g-kernel linear address space size.

    • kernel linear address space , which refers to a linear address space that can be used by the kernel. Its size = Lowmem linear address space size + vmalloc linear address space size.

      • Lowmem linear address space, which occupies a portion of the kernel linear address space. Its size = kernel linear address space size-Vmalloc linear address space size.
      • Lowmem function, this address space at the start of the address map is good, its mapped physical memory is continuous, and the physical memory address one by one corresponds.
      • Vmalloc a linear address space that occupies another part of the kernel linear address space. its size = (kernel linear address space size-Physical memory size) > vmalloc_reserve? Kernel linear address space-physical memory size: Vmalloc_reserve
      • The Vmalloc function is used to assign linear address continuity in the kernel, but the physical address does not require a contiguous large memory area. This address space is initialized without any address mapping.

Meaning of linear address space variables in Linux kernel

  • Page_offset = __page_offset = Config_page_offset: This value is the cutoff point for dividing the user linear address and the kernel linear address.

  • __vmalloc_reserve: Represents the minimum amount of space that should be reserved for vmalloc linear address space, which is 128M by default. This value can be changed by the kernel parameter Vmalloc when the system starts.

  • Vmalloc_end:vmalloc the end address of the linear address space. Its value = Maximum linear address – other ROM memory space size

  • Vmalloc_start:vmalloc The starting address of the linear address space. Its value = Vmalloc_end-vmalloc linear size (min. vmalloc_reserve)

  • Vmalloc_offset: Fixed size of 8M, used to lowmem linear address space and vmalloc linear address space between the establishment of a separation barrier, to prevent them from affecting each other.

  • MAXMEM: The value is ( VMALLOC_END - PAGE_OFFSET - __VMALLOC_RESERVE ), which indicates the maximum amount of RAM that the kernel can directly map, which is also relatively fixed (not related to the actual size of physical memory) because VMALLOC_END、PAGE_OFFSET、 __VMALLOC_RESERVE it is relatively fixed.

  • MAXMEM_PFN: The value is Pfn_down (MAXMEM), which indicates the number of memory pages that the kernel can manage directly.

  • MAX_ARCH_PFN: Represents the maximum number of memory pages accessible by the CPU. Corresponds to a 32-bit CPU, if it has PAE enabled, then max_arch_pfn= (1ULL? ( 36-page_shift)). If PAE is not turned on, then max_arch_pfn= (1ULL? ( 32-page_shift))

  • MAX_PFN: The maximum number of memory pages that can be managed from the BIOS, related to the actual memory size, but not larger than MAX_ARCH_PFN.
    When Max_pfn <= MAXMEM_PFN, the actual physical memory is smaller than the amount of memory that the kernel can linearly map, and Highmem is not required to manage it at this time. At this point, even if the Highmem,zone_highmem area can be managed memory size is 0.
    When Max_pfn > MAXMEM_PFN, the actual physical memory is larger than the amount of memory that the kernel can map linearly, so that the extra portion of physical memory cannot be linearly mapped and must be managed by a page table map (that is, vmalloc), which should be controlled by the Zone_ Highmem area to manage. If the HIGHMEM option is not turned on, this part of the memory is lost and is no longer managed.

  • MAX_LOW_PFN: The maximum number of low-end memory (relative to Highmem), which is the maximum number of pages that can be managed in the Zone_normal area.
    If the actual physical memory is smaller than the amount of memory that the kernel can linearly map, MAX_LOW_PFN = MAX_PFN, that is, all memory is managed by the low-end memory area.
    If the actual physical memory is larger than the amount of memory that the kernel can map linearly, MAX_LOW_PFN = MAXMEM_PFN, the maximum value that the low-end memory area can manage is the maximum value that the kernel can map linearly.

  • Highmem_pages: The maximum number of high-end memory, this is the maximum number of pages in ZONE_HIGHMEM area management. Its value is (MAX_FN–MAX_LOW_PFN).

Storage Device Management (can use the management of physical memory space)

The Linux kernel uses the physical memory space for functional zoning (refer to the allocation of linear addresses in address management) and is managed through the page structure

    • ZONE_DMA physical memory space, which is the memory range of 0~16m on the x86 architecture. Managed by the page structure, this area contains pages that can be DMA-operated by older devices. The

    • Zone_normal The physical memory space, which is the memory range of the 16m~kernel->lowmem linear address on the x86 system. Managed by the page structure.
      (at boot time, the kernel has mapped the memory space of the above two extents into the kernel linear address space.) So the kernel can directly access the physical memory without any action. However, to ensure that the pages that are accessed are not used by other features, use functions such as get_free_page () to get the free pages and mark the page as used before accessing the memory. The physical page returned does not need to do any address mapping, only the physical address plus the kernel space offset to obtain its linear address)

    • Zone_highmem physical memory space, its size = actual physical memory size –kernel-> Lowmem linear address space size. Managed by the page structure to manage physical memory larger than the size of the KERNEL->LOWMEM linear address space. This memory space is not mapped to the kernel linear address space and, when used, is assigned a page page by Alloc_page (), which is then mapped to a linear address space so that it can be accessed through a linear address. The kernel (usually with the Vmalloc () function) maps Highmem physical memory pages to Vmalloc linear address space, so the HIGHMEM physical memory size that the kernel can use is related to the size of the vmalloc linear address space. In addition, Highmem physical memory pages are typically mapped to the user linear address space. The main difference between

    • Zone_normal and Zone_highmem is that physical memory in Zone_normal does not need to be directly used for page table mappings, and the maximum value of its linear address is maxmem. While the physical memory in Zone_highmem needs to be used as a page table map, the range of its linear address is vmalloc_start~vmalloc_end. So the memory that cannot do linear mapping is managed by ZONE_HIGHMEM. If the HIGHMEM option is turned on, then the size of the ZONE_HIGHMEM is the remaining physical memory, and if not, then this part of the physical memory will not be managed.

Linux memory Management (x86-32 bit system)

Related Article

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.