Linux memory management-kernel user space "go"

Source: Internet
Author: User

transferred from: http://blog.chinaunix.net/uid-25909619-id-4491362.html1,linux several important structures and arrays in memory management page

unsigned long flags

A set of flags that also number the admin area of the page box

atomic_t _count

The number of times the page was referenced

atomic_t _mapcount

The number of page table entries in the page box, or 1 if not

struct List_head LRU

Manage page Busy/idle list (inactive_list/active_list), protected by Zone->lru_lock!

Zone

struct Free_area Free_area[max_order]

Identify free page box blocks in the admin area (Buddy system)

struct Pglist_data *zone_pgdat

Some properties of the zone, including pointers to each node_zone pointer, etc.

unsigned long ZONE_START_PFN

Zone Start Page Frame number,zone The number of the beginning of the Mem_map array. /* ZONE_START_PFN = = zone_start_paddr >> Page_shift */

Pglist_data

struct Zone Node_zones[max_nr_zones]

Node in the Admin area descriptor array

struct Zonelist node_zonelists[max_zonelists]

An array of zonelist data structures used by the page allocator

int Nr_zones

Number of management areas in a node

struct page *node_mem_map

An array of page descriptors in the node

unsigned long NODE_START_PFN

Subscript for the first page box in a node

Mem_map

Initialize the call path:

Free_area_init_node ()

Alloc_node_mem_map ()

Mem_map = Node_data (0)->node_mem_map;

.

2, the relationship between the various structures

3, the main memory allocation function Vmalloc

The 1,vmalloc application returns the address between Vmalloc_start to Vmalloc_end. Where, Vmalloc_start in virtual address:

3g+physic Memory length + 8G (GAP)

Vmalloc_end location in virtual address:

4g-128k (Dedicated page map)

The 2,kmalloc corresponds to the kfree and can allocate continuous physical memory;

3,vmalloc takes precedence over high-end physical memory, but has a discount on performance.

The physical pages assigned by Vmalloc are not swapped out;

Vmalloc uses a vmlist linked list, which differs from the vm_area_struct that manages user processes, and the latter swapped.

Kmalloc

1,kmalloc application returns the kernel virtual address, the virtual address and the actual physical address there is an offset between 0xc000_0000, can be used with Virt_to_phys () to obtain the corresponding physical address.

3g~physic

The 2,vmalloc corresponds to the Vfree, allocating contiguous virtual memory, but not necessarily physically contiguous.

3,kmalloc allocated memory is based on slab, so some features of slab, including coloring, alignment, etc., have good performance. Both the physical address and the logical address are contiguous.

4, Kmalloc () is the most common memory allocation method in the kernel, it eventually calls the partner system's __get_free_pages () function assignment, according to the flags parameter passed to the function, determines what occasion this function is allocated for, if the flag is Gfp_ Kernel can be used only in the context of a process, if the flag gfp_atomic can be used to break the context or to hold a lock in the code snippet.

The line address returned by Kmalloc is mapped directly, and the allocation request is satisfied with a contiguous physical page, with the maximum number of requests built in (2**5=32 page).

The minimum allocation that 5,KMALLOC can handle is 32 or 64 bytes (the page size used by system dependent systems), less than 128K

Kmap

Kmap () is primarily used in kernel mappings for high-end memory page frames, which are generally used:

Use Alloc_pages () to get the struct page structure in the high-end memory area, and then call Kmap (struct *page) in the address space after the kernel address space page_offset+896m (Pkmap_base to Fixaddr_ STAR) Establish a permanent mapping (if the page structure corresponds to a low-end physical memory, the function simply returns the corresponding virtual address of the page)

Kmap () may also cause sleep, so it cannot be used in code that interrupts and holds locks

However, Kmap can only allocate one physical page, so try to use less.

Reasons to use Kmap:

For high-end physical memory (after 896M), there is no relationship with the kernel address space to establish one by one (that is, the virtual address = Physical Address +page_offset Such a relationship), so you cannot use get_free_pages () such a page allocator for memory allocation, Instead of using the interface of the partner system algorithm such as Alloc_pages () to get the struct *page structure, and then map it to the kernel address space, note that this time the mapped address is not the same as the physical address page_offset.

Get_user_pages

Used to get the buffer address (page alignment) from the user space, directly to the IO operation. Operations that are typically used for large data volumes, such as DMA.

The access process is as follows:

4, some other miscellaneous gfp_mask

There are three functions:

1, behavior modification allocates memory using the specified method. such as gpf_wait, Sleep, gpf_io, bootable disk.

2, zone modifier identifies the partition from which the memory is allocated

3, type decoration such as Gfp_kernel=>__fgp_wait | __gfp_io | __GFP_FS, specify the desired behavior and the zone descriptor.

Migrate_type

#define MIGRATE_UNMOVABLE 0

#define MIGRATE_RECLAIMABLE 1

#define Migrate_movable 2

#define MIGRATE_PCPTYPES 3/* The number of types on the PCP lists */

#define Migrate_reserve 3

#define MIGRATE_ISOLATE 4/* can ' t allocate from here */

#define MIGRATE_TYPES 5

Improve the partner system to reduce system fragmentation.

struct Free_area {

struct List_head free_list[migrate_types];

unsigned long nr_free;

};

Each free_area contains multiple linked lists, in which the memory pages in each list are grouped according to whether they can be freed or migrated, so that all allocation requests for "non-migrated" pages are allocated on the list of free_list[migrate_unmovable] , as in the old version, there are 10 free_area in the system that represent a collection of different pages with a size of n power of 2. This collation minimizes memory fragmentation.

Linux memory Management (2)-User space

1. Some knowledge of compiling links

First, let's write a simple program, the sample code is as follows

[CPP]View Plaincopyprint?
    1. #include "stdio.h"
    2. #include "string.h"
    3. #include "Stdlib.h"
    4. int i=3;
    5. int j=4;
    6. int main ()
    7. {
    8. printf ("Value I is%d\n,address of I was 0x%lx\naddress of J is 0x%lx\n", I, (unsigned int) &i, (unsigned int)  &J);
    9. }

Program Fragment 1

Program Fragment 1 defines two variables, a and B, and assigns a value of 3, 4, respectively. Then in main function main, the values of I and J and their corresponding virtual addresses are printed. The running results of the program are as follows:

[CPP]View Plaincopyprint?
    1. Value I is 3
    2. Address of I is 0x80495e0,
    3. Address of J is 0x80495e4

Episode 1

The 0x80495e0,0x80495e4 shown in program fragment 2 are the virtual addresses of I and J respectively, so how is this address determined? Who's to be sure?

By looking at the list of symbols for the compiled executable program, the addresses of I and J are actually determined during the linking process and are determined by the compiler.

[CPP]View Plaincopyprint?
  1. Objdump-t a.out
  2. A.out:file format elf32-i386
  3. SYMBOL TABLE:
  4. ......
  5. 00000000 W *und* 00000000 _jv_registerclasses
  6. 08048494 g O. Rodata 00000004 _FP_HW
  7. 08048478 g F. Fini 00000000 _fini
  8. 00000000 F *und* 0000019f [email protected] @GLIBC_2.0
  9. 08048498 g O. Rodata 00000004 _io_stdin_used
  10. 080495DC g. Data 00000000 __data_start
  11. 080495E0 g O. Data 00000004 I
  12. 0804849c g O. Rodata 00000000. Hidden __dso_handle
  13. 080494f0 g O. Dtors 00000000. Hidden __dtor_end__
  14. 080483e0 g F. Text 00000069 __libc_csu_init
  15. 00000000 F *und* 00000039 [email protected] @GLIBC_2.0
  16. 080495e8 g *abs* 00000000 __bss_start
  17. 080495e4 g O. Data 00000004 J
  18. 080495f0 g *abs* 00000000 _end
  19. 080495e8 g *abs* 00000000 _edata
  20. 08048449 g F. Text 00000000. Hidden __I686.GET_PC_THUNK.BX
  21. 08048384 g F. Text 00000044 Main
  22. 08048250 g F. Init 00000000 _init

2. Structure for managing user-space process memory

When a program is loaded into memory, the kernel creates a struct called task_struct to manage the process.

[CPP]View Plaincopyprint?
    1. struct Task_struct {
    2. struct list_head tasks;
    3. struct mm_struct *mm, *active_mm;  //field mm is the memory management for the process.
    4. pid_t pid;
    5. struct fs_struct *fs;
    6. struct files_struct *files;
    7. ......
    8. };

Task_struct

[CPP]View Plaincopyprint?
    1. struct Mm_struct {
    2. struct vm_area_struct * MMAP; / * List of VMAs, a process has a mm_struct, multiple
    3. vm_area_struct*/
    4. pgd_t * PGD;
    5. int map_count; /* Number of VMAs * /
    6. ......
    7. };

Mm_struct

[CPP]View Plaincopyprint?
    1. struct Vm_area_struct {
    2. struct mm_struct * VM_MM; / * The address space we belong to. * /
    3. unsigned long vm_start; / * Our start address within VM_MM. * *
    4. unsigned long vm_end; /* The first byte after our end address
    5. Within VM_MM. * *
    6. }

Vm_area_struct

Summary: The whole memory management system can be so understood, on 32-bit systems, all the addresses (0-4g), page tables, page boxes, etc. are unified by the kernel management. The service in the kernel (where the service consists of processes running inside the kernel, kernel variables, etc.) just takes up the 3-4g address space segment. For user space, the address of the process is determined by the compiler, and the compiler uses the 0-3G address when linking individual library files.

For the hardware MMU module, it does not care what the Linux address belongs to, and the MMU only finds the corresponding physical address based on the rules (Memory mapping table) that it sets.

Diagram of each structure

3. Implementation of the user space malloc

The previous summary knows that memory management is done in kernel space. The same is true for a piece of memory for malloc applications, first look at the diagram of the malloc invocation:

MALLOC->BRK ()->syscall_define1 (BRK, unsigned long, BRK)->__get_free_pages ()

Syscall_define1 This system call, the state of execution is shifted from user space to kernel space.

Linux memory management-kernel user space "go"

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.